From c7466ef67f76efdd9e8a151e8d0f38281199c2a9 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 10 Dec 2016 12:10:04 +0100 Subject: [PATCH] Add timestamps on each received frame. Note that this breaks the ABI. --- Makefile | 4 ++-- bmusb.cpp | 5 +++++ bmusb/bmusb.h | 9 +++++++++ fake_capture.cpp | 4 ++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ee0ba10..e1c33ec 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,8 @@ INSTALL := install PREFIX := /usr/local LIB := libbmusb.a SODEV := libbmusb.so -SONAME := libbmusb.so.1 -SOLIB := libbmusb.so.1.0.0 +SONAME := libbmusb.so.2 +SOLIB := libbmusb.so.2.0.0 all: $(LIB) $(SOLIB) main diff --git a/bmusb.cpp b/bmusb.cpp index 2900709..9decbc9 100644 --- a/bmusb.cpp +++ b/bmusb.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,7 @@ #include using namespace std; +using namespace std::chrono; using namespace std::placeholders; #define USB_VENDOR_BLACKMAGIC 0x1edb @@ -362,6 +364,8 @@ void BMUSBCapture::start_new_frame(const uint8_t *start) uint16_t timecode = (start[1] << 8) | start[0]; if (current_video_frame.len > 0) { + current_video_frame.received_timestamp = steady_clock::now(); + // If format is 0x0800 (no signal), add a fake (empty) audio // frame to get it out of the queue. // TODO: Figure out if there are other formats that come with @@ -405,6 +409,7 @@ void BMUSBCapture::start_new_audio_block(const uint8_t *start) uint16_t format = (start[3] << 8) | start[2]; uint16_t timecode = (start[1] << 8) | start[0]; if (current_audio_frame.len > 0) { + current_audio_frame.received_timestamp = steady_clock::now(); //dump_audio_block(); queue_frame(format, timecode, current_audio_frame, &pending_audio_frames); } diff --git a/bmusb/bmusb.h b/bmusb/bmusb.h index f4af6aa..6aabd51 100644 --- a/bmusb/bmusb.h +++ b/bmusb/bmusb.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +40,14 @@ class FrameAllocator { // If so, and are still about the number of total bytes // so if size == 1024, there's 512 bytes in data and 512 in data2. bool interleaved = false; + + // At what point this frame was received. Note that this marks the + // _end_ of the frame being received, not the beginning. + // Thus, if you want to measure latency, you'll also need to include + // the time the frame actually took to transfer (usually 1/fps, + // ie., the frames are typically transferred in real time). + std::chrono::steady_clock::time_point received_timestamp = + std::chrono::steady_clock::time_point::min(); }; virtual ~FrameAllocator(); diff --git a/fake_capture.cpp b/fake_capture.cpp index 5f53e95..fbcfedd 100644 --- a/fake_capture.cpp +++ b/fake_capture.cpp @@ -13,6 +13,7 @@ #if __SSE2__ #include #endif +#include #include #include "bmusb/bmusb.h" @@ -26,6 +27,7 @@ constexpr uint8_t cbs[NUM_COLORS] = { 90, 54, 240, 128 }; constexpr uint8_t crs[NUM_COLORS] = { 240, 34, 110, 128 }; using namespace std; +using namespace std::chrono; namespace bmusb { namespace { @@ -255,6 +257,7 @@ void FakeCapture::producer_thread_func() memset4(video_frame.data, ycbcr, width * height / 2); } video_frame.len = width * height * 2; + video_frame.received_timestamp = steady_clock::now(); } AudioFormat audio_format; @@ -266,6 +269,7 @@ void FakeCapture::producer_thread_func() const unsigned num_stereo_samples = audio_sample_frequency / fps; assert(audio_frame.size >= audio_format.num_channels * sizeof(int32_t) * num_stereo_samples); audio_frame.len = audio_format.num_channels * sizeof(int32_t) * num_stereo_samples; + audio_frame.received_timestamp = steady_clock::now(); if (audio_sin == 0.0f) { // Silence. -- 2.39.2