Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.
Closed
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 include/k4ainternal/matroska_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions include/k4arecord/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <k4arecord/record.h>
*
* \relates k4a_record_t
*
* \returns ::K4A_RESULT_SUCCEEDED is returned on success.
*
* \xmlonly
* <requirements>
* <requirement name="Header">record.h (include k4arecord/record.h)</requirement>
* <requirement name="Library">k4arecord.lib</requirement>
* <requirement name="DLL">k4arecord.dll</requirement>
* </requirements>
* \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
Expand Down
11 changes: 10 additions & 1 deletion src/record/internal/matroska_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include <algorithm>
#include <sstream>
#include <chrono>

#include <k4a/k4a.h>
#include <k4ainternal/matroska_write.h>
Expand Down Expand Up @@ -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<std::chrono::nanoseconds>(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;
}
Expand Down
11 changes: 11 additions & 0 deletions src/record/sdk/record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tools/k4arecorder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
23 changes: 22 additions & 1 deletion tools/k4arecorder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<char *> &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).",
Expand Down Expand Up @@ -433,5 +453,6 @@ int main(int argc, char **argv)
&device_config,
recording_imu_enabled,
absoluteExposureValue,
gain);
gain,
recording_use_system_time);
}
5 changes: 4 additions & 1 deletion tools/k4arecorder/recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion tools/k4arecorder/recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */