diff options
author | Jan Alexander Steffens (heftig) <jan.steffens@ltnglobal.com> | 2023-10-25 19:48:50 +0300 |
---|---|---|
committer | Sebastian Dröge <sebastian@centricular.com> | 2023-11-10 17:02:36 +0300 |
commit | d65504aeb88e5b01e4bf34c47ed988899d8449b1 (patch) | |
tree | 0a2809b147238546a958dbf40a1d647ae237dbaa | |
parent | 48020dfbf59a1e9e254a1a26223b1cb4b28d1fb0 (diff) |
livesync: Keep existing buffer duration in some cases
Resize a repeat buffer only if caps gave us a duration to use, or we
consider its current duration unreasonable.
In particular, for audio streams we should prefer reusing the buffer
size upstream gave us, as we did before 6633cc4046aa1f5a.
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1388>
-rw-r--r-- | utils/livesync/src/livesync/imp.rs | 55 |
1 files changed, 31 insertions, 24 deletions
diff --git a/utils/livesync/src/livesync/imp.rs b/utils/livesync/src/livesync/imp.rs index ce7a0364c..268eb1d7a 100644 --- a/utils/livesync/src/livesync/imp.rs +++ b/utils/livesync/src/livesync/imp.rs @@ -1302,36 +1302,43 @@ impl LiveSync { let buffer = out_buffer.make_mut(); if !duplicate { - let duration = state.out_duration.map_or(DEFAULT_DURATION, |dur| { - dur.clamp(MINIMUM_DURATION, MAXIMUM_DURATION) - }); + let duration_is_valid = + (MINIMUM_DURATION..=MAXIMUM_DURATION).contains(&buffer.duration().unwrap()); - if let Some(audio_info) = &state.out_audio_info { - let Some(size) = audio_info - .convert::<Option<gst::format::Bytes>>(duration) - .flatten() - .and_then(|bytes| usize::try_from(bytes).ok()) - else { - gst::error!(CAT, imp: self, "Failed to calculate size of repeat buffer"); - return Err(gst::FlowError::Error); - }; + if state.out_duration.is_some() || !duration_is_valid { + // Resize the buffer if caps gave us a duration + // or the current duration is unreasonable - let mut mapped_memory = gst::Memory::with_size(size) - .into_mapped_memory_writable() - .map_err(|_| { - gst::error!(CAT, imp: self, "Failed to map memory"); - gst::FlowError::Error - })?; + let duration = state.out_duration.map_or(DEFAULT_DURATION, |dur| { + dur.clamp(MINIMUM_DURATION, MAXIMUM_DURATION) + }); - audio_info - .format_info() - .fill_silence(mapped_memory.as_mut_slice()); + if let Some(audio_info) = &state.out_audio_info { + let Some(size) = audio_info + .convert::<Option<gst::format::Bytes>>(duration) + .flatten() + .and_then(|bytes| usize::try_from(bytes).ok()) + else { + gst::error!(CAT, imp: self, "Failed to calculate size of repeat buffer"); + return Err(gst::FlowError::Error); + }; + + buffer.replace_all_memory(gst::Memory::with_size(size)); + } - buffer.replace_all_memory(mapped_memory.into_memory()); + buffer.set_duration(duration); + gst::debug!(CAT, imp: self, "Patched output buffer duration to {duration}"); } - buffer.set_duration(duration); - gst::debug!(CAT, imp: self, "Patched output buffer duration to {duration}"); + if let Some(audio_info) = &state.out_audio_info { + let mut map_info = buffer.map_writable().map_err(|e| { + gst::error!(CAT, imp: self, "Failed to map buffer: {}", e); + gst::FlowError::Error + })?; + audio_info + .format_info() + .fill_silence(map_info.as_mut_slice()); + } } buffer.set_dts(dts); |