Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
*~
/.vscode/
24 changes: 23 additions & 1 deletion output/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* output.cpp - video stream output base class
*/

#include <chrono>
#include <cinttypes>
#include <stdexcept>

Expand Down Expand Up @@ -89,7 +90,28 @@ void Output::OutputReady(void *mem, size_t size, int64_t timestamp_us, bool keyf

void Output::timestampReady(int64_t timestamp)
{
fprintf(fp_timestamps_, "%" PRId64 ".%03" PRId64 "\n", timestamp / 1000, timestamp % 1000);
//fprintf(fp_timestamps_, "%" PRId64 ".%03" PRId64 "\n", timestamp / 1000, timestamp % 1000);
// timestamp is in us. the original output string contains stamps in ms
// now we want output timestamp to be in us, integers, unix time

int64_t timestamp_modified = timestamp;
if (time_offset_skip_frames_beginning_counter_< time_offset_skip_frames_beginning_)
{
time_offset_skip_frames_beginning_counter_++;
}
else if (time_offset_running_average_num_frames_counter_ < time_offset_running_average_num_frames_)
{
auto now_us = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
auto added_offset = static_cast<double>(now_us - timestamp) / static_cast<double>(time_offset_running_average_num_frames_);
unix_to_camera_time_offset_ += static_cast<int64_t>(added_offset);
time_offset_running_average_num_frames_counter_++;
}
else
{
timestamp_modified = timestamp + unix_to_camera_time_offset_;
}
fprintf(fp_timestamps_, "%" PRId64 "\n", timestamp_modified);

if (options_->flush)
fflush(fp_timestamps_);
}
Expand Down
12 changes: 11 additions & 1 deletion output/output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ class Output
State state_;
std::atomic<bool> enable_;
int64_t time_offset_;
int64_t last_timestamp_;
int64_t last_timestamp_; // in us

/* For aligning camera time with computer time */
int64_t unix_to_camera_time_offset_; // unix - camera, in us
// these frames won't be used for offset computation
size_t time_offset_skip_frames_beginning_ = 4;
size_t time_offset_skip_frames_beginning_counter_ = 0;
// after frame skipping, these frames will be used for offset computation
size_t time_offset_running_average_num_frames_ = 5;
size_t time_offset_running_average_num_frames_counter_ = 0;

std::streambuf *buf_metadata_;
std::ofstream of_metadata_;
bool metadata_started_ = false;
Expand Down
Loading