From bbf131086a34d2046fe679fe060a23967d8e65a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 26 Jun 2024 10:06:14 +0300 Subject: [PATCH] livesync: Synchronize on the first buffer too Previously the first buffer would be output immediately and synchronization would only happen from the second buffer onwards. This would mean that the first buffer would potentially be output too early. Instead, if there is no known output timestamp yet but a buffer with a timestamp, first of all take its start as the initial output timestamp and synchronize on that buffer. Part-of: --- utils/livesync/src/livesync/imp.rs | 18 +++++++++++++++--- utils/livesync/tests/livesync.rs | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/utils/livesync/src/livesync/imp.rs b/utils/livesync/src/livesync/imp.rs index 4d9f0e4d..96e8f291 100644 --- a/utils/livesync/src/livesync/imp.rs +++ b/utils/livesync/src/livesync/imp.rs @@ -1053,7 +1053,16 @@ impl LiveSync { None => None, Some(Item::Buffer(buffer, timestamp, lateness)) => { - if self.buffer_is_early(&state, timestamp) { + // Synchronize on the first buffer with timestamps to not output it too early + if let Some(Timestamps { start, .. }) = + state.out_timestamp.is_none().then_some(timestamp).flatten() + { + state.out_timestamp = Some(Timestamps { start, end: start }); + state + .queue + .push_front(Item::Buffer(buffer, timestamp, lateness)); + return Ok(gst::FlowSuccess::Ok); + } else if self.buffer_is_early(&state, timestamp) { // Try this buffer again on the next iteration state .queue @@ -1261,8 +1270,11 @@ impl LiveSync { return false; }; - // When out_timestamp is set, we also have an out_buffer - let slack = state.out_buffer.as_deref().unwrap().duration().unwrap(); + // When out_timestamp is set, we also have an out_buffer unless it is the first buffer + let Some(ref out_buffer) = state.out_buffer else { + return false; + }; + let slack = out_buffer.duration().unwrap(); if timestamp.start < out_timestamp.end + slack { return false; diff --git a/utils/livesync/tests/livesync.rs b/utils/livesync/tests/livesync.rs index 1f0e2f1f..d82f4ee9 100644 --- a/utils/livesync/tests/livesync.rs +++ b/utils/livesync/tests/livesync.rs @@ -138,6 +138,8 @@ fn test_livesync(h: &mut gst_check::Harness, o: u64, singlesegment: bool) { h.push_from_src().unwrap(); h.push_from_src().unwrap(); assert_eq!(h.pull_event().unwrap().type_(), gst::EventType::StreamStart); + // Caps are only output once waiting for the first buffer has finished + h.crank_single_clock_wait().unwrap(); assert_eq!(h.pull_event().unwrap().type_(), gst::EventType::Caps); assert_eq!(h.pull_event().unwrap().type_(), gst::EventType::Segment); assert_crank_pull(h, o, 0, 0, gst::BufferFlags::DISCONT, singlesegment);