diff --git a/.gitignore b/.gitignore index dee53c53..dd371c44 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build *~ +/.vscode/ \ No newline at end of file diff --git a/output/output.cpp b/output/output.cpp index 1e080ee9..770ff06f 100644 --- a/output/output.cpp +++ b/output/output.cpp @@ -5,6 +5,7 @@ * output.cpp - video stream output base class */ +#include #include #include @@ -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::system_clock::now().time_since_epoch()).count(); + auto added_offset = static_cast(now_us - timestamp) / static_cast(time_offset_running_average_num_frames_); + unix_to_camera_time_offset_ += static_cast(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_); } diff --git a/output/output.hpp b/output/output.hpp index dfc1a032..35756a39 100644 --- a/output/output.hpp +++ b/output/output.hpp @@ -46,7 +46,17 @@ class Output State state_; std::atomic 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;