diff --git a/include/k4ainternal/matroska_write.h b/include/k4ainternal/matroska_write.h index d32ca247f..96d390dc6 100644 --- a/include/k4ainternal/matroska_write.h +++ b/include/k4ainternal/matroska_write.h @@ -99,6 +99,7 @@ typedef struct _k4a_record_context_t std::mutex writer_lock; bool header_written, first_cluster_written; + bool use_system_time = false; } k4a_record_context_t; K4A_DECLARE_CONTEXT(k4a_record_t, k4a_record_context_t); diff --git a/include/k4arecord/record.h b/include/k4arecord/record.h index 998ce3d69..416cacfb0 100644 --- a/include/k4arecord/record.h +++ b/include/k4arecord/record.h @@ -105,6 +105,30 @@ K4ARECORD_EXPORT k4a_result_t k4a_record_create(const char *path, */ K4ARECORD_EXPORT k4a_result_t k4a_record_add_tag(k4a_record_t recording_handle, const char *name, const char *value); +/** Set recording time source + * + * \param recording_handle + * The handle of a new recording, obtained by k4a_record_create(). + * + * \param use_system_time + * Flag indicating time source + * + * \headerfile record.h + * + * \relates k4a_record_t + * + * \returns ::K4A_RESULT_SUCCEEDED is returned on success. + * + * \xmlonly + * + * record.h (include k4arecord/record.h) + * k4arecord.lib + * k4arecord.dll + * + * \endxmlonly + */ +K4ARECORD_EXPORT k4a_result_t k4a_set_time_source(const k4a_record_t recording_handle, const bool use_system_time); + /** Adds the track header for recording IMU. * * \param recording_handle diff --git a/src/record/internal/matroska_write.cpp b/src/record/internal/matroska_write.cpp index 1f0f96d0c..32b3bee33 100644 --- a/src/record/internal/matroska_write.cpp +++ b/src/record/internal/matroska_write.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -335,7 +336,15 @@ k4a_result_t write_cluster(k4a_record_context_t *context, cluster_t *cluster, ui if (!context->start_offset_tag_added) { std::ostringstream offset_str; - offset_str << context->start_timestamp_offset; + if (context->use_system_time) + { + auto epoch = std::chrono::system_clock::now().time_since_epoch(); + offset_str << std::chrono::duration_cast(epoch).count(); + } + else + { + offset_str << context->start_timestamp_offset; + } add_tag(context, "K4A_START_OFFSET_NS", offset_str.str().c_str()); context->start_offset_tag_added = true; } diff --git a/src/record/sdk/record.cpp b/src/record/sdk/record.cpp index 4cd5b2985..6f370bb1e 100644 --- a/src/record/sdk/record.cpp +++ b/src/record/sdk/record.cpp @@ -362,6 +362,17 @@ k4a_result_t k4a_record_create(const char *path, return result; } +k4a_result_t k4a_set_time_source(const k4a_record_t recording_handle, const bool use_system_time) +{ + RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, k4a_record_t, recording_handle); + + k4a_record_context_t *context = k4a_record_t_get_context(recording_handle); + RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, context == NULL); + context->use_system_time = use_system_time; + + return K4A_RESULT_SUCCEEDED; +} + k4a_result_t k4a_record_add_tag(const k4a_record_t recording_handle, const char *name, const char *value) { RETURN_VALUE_IF_HANDLE_INVALID(K4A_RESULT_FAILED, k4a_record_t, recording_handle); diff --git a/tools/k4arecorder/README.md b/tools/k4arecorder/README.md index 1eaa15b9c..5d7526a4d 100644 --- a/tools/k4arecorder/README.md +++ b/tools/k4arecorder/README.md @@ -29,5 +29,7 @@ k4arecorder [options] output.mkv --external-sync Set the external sync mode (Master, Subordinate, Standalone default: Standalone) --sync-delay Set the external sync delay off the master camera in microseconds (default: 0) This setting is only valid if the camera is in Subordinate mode. + --time-source Set the time source to synchronize the recording (DEVICE, SYSTEM, default: DEVICE) -e, --exposure-control Set manual exposure value (-11 to 1) for the RGB camera (default: auto exposure) + -g, --gain Set cameras manual gain. The valid range is 0 to 255. (default: auto) ``` diff --git a/tools/k4arecorder/main.cpp b/tools/k4arecorder/main.cpp index 9bcb83d4b..5bf6cd097 100644 --- a/tools/k4arecorder/main.cpp +++ b/tools/k4arecorder/main.cpp @@ -119,6 +119,7 @@ int main(int argc, char **argv) k4a_fps_t recording_rate = K4A_FRAMES_PER_SECOND_30; bool recording_rate_set = false; bool recording_imu_enabled = true; + bool recording_use_system_time = false; k4a_wired_sync_mode_t wired_sync_mode = K4A_WIRED_SYNC_MODE_STANDALONE; int32_t depth_delay_off_color_usec = 0; uint32_t subordinate_delay_off_master_usec = 0; @@ -326,6 +327,25 @@ int main(int argc, char **argv) } subordinate_delay_off_master_usec = (uint32_t)delay; }); + cmd_parser.RegisterOption("--time-source", + "Set the time source to synchronize the recording (DEVICE, SYSTEM, default: DEVICE)", + 1, + [&](const std::vector &args) { + if (string_compare(args[0], "device") == 0) + { + recording_use_system_time = false; + } + else if (string_compare(args[0], "system") == 0) + { + recording_use_system_time = true; + } + else + { + std::ostringstream str; + str << "Unknown time source specified: " << args[0]; + throw std::runtime_error(str.str()); + } + }); cmd_parser.RegisterOption("-e|--exposure-control", "Set manual exposure value from 2 us to 200,000us for the RGB camera (default: \n" "auto exposure). This control also supports MFC settings of -11 to 1).", @@ -433,5 +453,6 @@ int main(int argc, char **argv) &device_config, recording_imu_enabled, absoluteExposureValue, - gain); + gain, + recording_use_system_time); } diff --git a/tools/k4arecorder/recorder.cpp b/tools/k4arecorder/recorder.cpp index 163f620df..7bfd464a5 100644 --- a/tools/k4arecorder/recorder.cpp +++ b/tools/k4arecorder/recorder.cpp @@ -54,7 +54,8 @@ int do_recording(uint8_t device_index, k4a_device_configuration_t *device_config, bool record_imu, int32_t absoluteExposureValue, - int32_t gain) + int32_t gain, + bool use_system_time) { seconds recording_length_seconds(recording_length); const uint32_t installed_devices = k4a_device_get_installed_count(); @@ -141,6 +142,8 @@ int do_recording(uint8_t device_index, return 1; } + CHECK(k4a_set_time_source(recording, use_system_time), device); + if (record_imu) { CHECK(k4a_record_add_imu_track(recording), device); diff --git a/tools/k4arecorder/recorder.h b/tools/k4arecorder/recorder.h index c9db471d6..5b18c5b86 100644 --- a/tools/k4arecorder/recorder.h +++ b/tools/k4arecorder/recorder.h @@ -18,6 +18,7 @@ int do_recording(uint8_t device_index, k4a_device_configuration_t *device_config, bool record_imu, int32_t absoluteExposureValue, - int32_t gain); + int32_t gain, + bool use_system_time); #endif /* RECORDER_H */