mp4mux: Factor out running time to UTC time calculation into a function

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/979>
This commit is contained in:
Sebastian Dröge 2022-11-18 12:18:40 +02:00
parent aa7fd34097
commit 9903d536b5

View file

@ -44,6 +44,22 @@ fn get_utc_time_from_buffer(buffer: &gst::BufferRef) -> Option<gst::ClockTime> {
}) })
} }
/// Converts a running time to an UTC time.
fn running_time_to_utc_time(
running_time: impl Into<gst::Signed<gst::ClockTime>>,
running_time_utc_time_mapping: (
impl Into<gst::Signed<gst::ClockTime>>,
impl Into<gst::Signed<gst::ClockTime>>,
),
) -> Option<gst::ClockTime> {
running_time_utc_time_mapping
.1
.into()
.checked_sub(running_time_utc_time_mapping.0.into())
.and_then(|res| res.checked_add(running_time.into()))
.and_then(|res| res.positive())
}
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| { static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
gst::DebugCategory::new( gst::DebugCategory::new(
"mp4mux", "mp4mux",
@ -213,14 +229,12 @@ impl MP4Mux {
let utc_time = match get_utc_time_from_buffer(&buffer) { let utc_time = match get_utc_time_from_buffer(&buffer) {
None => { None => {
// Calculate from the mapping // Calculate from the mapping
gst::Signed::Positive(running_time_utc_time_mapping.1) running_time_to_utc_time(pts, running_time_utc_time_mapping).ok_or_else(
.checked_sub(running_time_utc_time_mapping.0) || {
.and_then(|res| res.checked_add(pts))
.and_then(|res| res.positive())
.ok_or_else(|| {
gst::error!(CAT, obj: sinkpad, "Stream has negative PTS UTC time"); gst::error!(CAT, obj: sinkpad, "Stream has negative PTS UTC time");
gst::FlowError::Error gst::FlowError::Error
})? },
)?
} }
Some(utc_time) => utc_time, Some(utc_time) => utc_time,
}; };
@ -236,11 +250,8 @@ impl MP4Mux {
buffer.set_pts(utc_time); buffer.set_pts(utc_time);
if let Some(dts) = dts { if let Some(dts) = dts {
let dts_utc_time = gst::Signed::Positive(utc_time) let dts_utc_time =
.checked_sub(pts) running_time_to_utc_time(dts, (pts, utc_time)).ok_or_else(|| {
.and_then(|res| res.checked_add(dts))
.and_then(|res| res.positive())
.ok_or_else(|| {
gst::error!(CAT, obj: sinkpad, "Stream has negative DTS UTC time"); gst::error!(CAT, obj: sinkpad, "Stream has negative DTS UTC time");
gst::FlowError::Error gst::FlowError::Error
})?; })?;
@ -338,7 +349,8 @@ impl MP4Mux {
"Got initial UTC time {utc_time} at PTS running time {running_time}", "Got initial UTC time {utc_time} at PTS running time {running_time}",
); );
*running_time_utc_time_mapping = Some((running_time, utc_time)); let mapping = (running_time, utc_time);
*running_time_utc_time_mapping = Some(mapping);
// Push the buffer onto the pre-queue and re-timestamp it and all other buffers // Push the buffer onto the pre-queue and re-timestamp it and all other buffers
// based on the mapping above. // based on the mapping above.
@ -348,11 +360,7 @@ impl MP4Mux {
let buffer = buffer.make_mut(); let buffer = buffer.make_mut();
let pts = segment.to_running_time_full(buffer.pts().unwrap()).unwrap(); let pts = segment.to_running_time_full(buffer.pts().unwrap()).unwrap();
let pts_utc_time = gst::Signed::Positive(utc_time) let pts_utc_time = running_time_to_utc_time(pts, mapping).ok_or_else(|| {
.checked_sub(running_time)
.and_then(|res| res.checked_add(pts))
.and_then(|res| res.positive())
.ok_or_else(|| {
gst::error!(CAT, obj: sinkpad, "Stream has negative PTS UTC time"); gst::error!(CAT, obj: sinkpad, "Stream has negative PTS UTC time");
gst::FlowError::Error gst::FlowError::Error
})?; })?;
@ -365,11 +373,7 @@ impl MP4Mux {
if let Some(dts) = buffer.dts() { if let Some(dts) = buffer.dts() {
let dts = segment.to_running_time_full(dts).unwrap(); let dts = segment.to_running_time_full(dts).unwrap();
let dts_utc_time = gst::Signed::Positive(pts_utc_time) let dts_utc_time = running_time_to_utc_time(dts, mapping).ok_or_else(|| {
.checked_sub(pts)
.and_then(|res| res.checked_add(dts))
.and_then(|res| res.positive())
.ok_or_else(|| {
gst::error!(CAT, obj: sinkpad, "Stream has negative DTS UTC time"); gst::error!(CAT, obj: sinkpad, "Stream has negative DTS UTC time");
gst::FlowError::Error gst::FlowError::Error
})?; })?;