livesync: Separate out_buffer duplicate status from GAP flag

Otherwise we might get confused by upstream GAP buffers.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1369>
This commit is contained in:
Jan Alexander Steffens (heftig) 2023-10-25 01:15:19 +02:00
parent 2f36bd5d77
commit 4ac7d0415b

View file

@ -127,6 +127,9 @@ struct State {
/// Current buffer of our srcpad /// Current buffer of our srcpad
out_buffer: Option<gst::Buffer>, out_buffer: Option<gst::Buffer>,
/// Whether our last output buffer was a duplicate
out_buffer_duplicate: bool,
/// Running timestamp of our sinkpad /// Running timestamp of our sinkpad
in_timestamp: Option<Timestamps>, in_timestamp: Option<Timestamps>,
@ -182,6 +185,7 @@ impl Default for State {
queue: VecDeque::with_capacity(32), queue: VecDeque::with_capacity(32),
buffer_queued: false, buffer_queued: false,
out_buffer: None, out_buffer: None,
out_buffer_duplicate: false,
in_timestamp: None, in_timestamp: None,
out_timestamp: None, out_timestamp: None,
num_in: 0, num_in: 0,
@ -504,6 +508,9 @@ impl State {
.filter(|&dur| dur > gst::ClockTime::ZERO) .filter(|&dur| dur > gst::ClockTime::ZERO)
// Safe default // Safe default
.unwrap_or(DEFAULT_DURATION); .unwrap_or(DEFAULT_DURATION);
// Change the duration of the next duplicate buffer
self.out_buffer_duplicate = false;
} }
fn pending_events(&self) -> bool { fn pending_events(&self) -> bool {
@ -586,6 +593,7 @@ impl LiveSync {
state.pending_caps = None; state.pending_caps = None;
state.out_audio_info = None; state.out_audio_info = None;
state.out_buffer = None; state.out_buffer = None;
state.out_buffer_duplicate = false;
state.out_timestamp = None; state.out_timestamp = None;
} }
@ -1055,6 +1063,7 @@ impl LiveSync {
gst::EventView::Eos(_) => { gst::EventView::Eos(_) => {
state.out_buffer = None; state.out_buffer = None;
state.out_buffer_duplicate = false;
state.out_timestamp = None; state.out_timestamp = None;
state.srcresult = Err(gst::FlowError::Eos); state.srcresult = Err(gst::FlowError::Eos);
} }
@ -1097,18 +1106,13 @@ impl LiveSync {
Some((mut buffer, BufferLateness::OnTime)) => { Some((mut buffer, BufferLateness::OnTime)) => {
state.num_in += 1; state.num_in += 1;
if state if state.out_buffer.is_none() || state.out_buffer_duplicate {
.out_buffer // We are just starting or done bridging a gap
.as_ref() buffer.make_mut().set_flags(gst::BufferFlags::DISCONT);
.map_or(false, |b| b.flags().contains(gst::BufferFlags::GAP))
{
// We are done bridging a gap, so mark it as DISCONT instead
let buf_mut = buffer.make_mut();
buf_mut.unset_flags(gst::BufferFlags::GAP);
buf_mut.set_flags(gst::BufferFlags::DISCONT);
} }
state.out_buffer = Some(buffer); state.out_buffer = Some(buffer);
state.out_buffer_duplicate = false;
state.out_timestamp = state.in_timestamp; state.out_timestamp = state.in_timestamp;
caps = state.pending_caps.take(); caps = state.pending_caps.take();
@ -1275,13 +1279,15 @@ impl LiveSync {
); );
} }
/// Patches the output buffer for repeating, setting out_buffer and out_timestamp /// Patches the output buffer for repeating, setting out_buffer, out_buffer_duplicate and
/// out_timestamp
fn patch_output_buffer( fn patch_output_buffer(
&self, &self,
state: &mut State, state: &mut State,
source: Option<gst::Buffer>, source: Option<gst::Buffer>,
) -> Result<(), gst::FlowError> { ) -> Result<(), gst::FlowError> {
let out_buffer = state.out_buffer.as_mut().unwrap(); let out_buffer = state.out_buffer.as_mut().unwrap();
let mut duplicate = state.out_buffer_duplicate;
let duration = out_buffer.duration().unwrap(); let duration = out_buffer.duration().unwrap();
let dts = out_buffer.dts().map(|t| t + duration); let dts = out_buffer.dts().map(|t| t + duration);
@ -1290,14 +1296,15 @@ impl LiveSync {
if let Some(source) = source { if let Some(source) = source {
gst::debug!(CAT, imp: self, "Repeating {:?} using {:?}", out_buffer, source); gst::debug!(CAT, imp: self, "Repeating {:?} using {:?}", out_buffer, source);
*out_buffer = source; *out_buffer = source;
duplicate = false;
} else { } else {
gst::debug!(CAT, imp: self, "Repeating {:?}", out_buffer); gst::debug!(CAT, imp: self, "Repeating {:?}", out_buffer);
} }
let buffer = out_buffer.make_mut(); let buffer = out_buffer.make_mut();
if let Some(audio_info) = &state.out_audio_info { if !duplicate {
if !buffer.flags().contains(gst::BufferFlags::GAP) { if let Some(audio_info) = &state.out_audio_info {
let mut map_info = buffer.map_writable().map_err(|e| { let mut map_info = buffer.map_writable().map_err(|e| {
gst::error!(CAT, imp: self, "Failed to map buffer: {}", e); gst::error!(CAT, imp: self, "Failed to map buffer: {}", e);
gst::FlowError::Error gst::FlowError::Error
@ -1305,9 +1312,11 @@ impl LiveSync {
audio_info audio_info
.format_info() .format_info()
.fill_silence(map_info.as_mut_slice()); .fill_silence(map_info.as_mut_slice());
} else {
let duration = state.fallback_duration;
buffer.set_duration(duration);
gst::debug!(CAT, imp: self, "Patched output buffer duration to {duration}");
} }
} else {
buffer.set_duration(state.fallback_duration);
} }
buffer.set_dts(dts); buffer.set_dts(dts);
@ -1315,6 +1324,7 @@ impl LiveSync {
buffer.set_flags(gst::BufferFlags::GAP); buffer.set_flags(gst::BufferFlags::GAP);
buffer.unset_flags(gst::BufferFlags::DISCONT); buffer.unset_flags(gst::BufferFlags::DISCONT);
state.out_buffer_duplicate = true;
state.out_timestamp = state.ts_range( state.out_timestamp = state.ts_range(
state.out_buffer.as_ref().unwrap(), state.out_buffer.as_ref().unwrap(),
state.out_segment.as_ref().unwrap(), state.out_segment.as_ref().unwrap(),