From 3d6efeba5eb41f48ab9213a3e4d4f75eb9928dba Mon Sep 17 00:00:00 2001 From: tilpner Date: Tue, 29 Aug 2023 15:45:33 +0200 Subject: [PATCH] fix(codec, format)!: detect unset `time_base`s, allow unsetting `time_base` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of silently passing possibly unset `time_base`s back into ffmpeg, force users to explicitly handle (even if just by early panic) their absence. BREAKING CHANGE: `time_base` and `set_time_base` functions now return/accept Option (±Into). `unwrap`/`expect` is a valid way to surface this unwanted state early in user code. For symmetry, `set_time_base` also allows unsetting by passing a `None`, so all existing calls (with non-zero timebases) need to wrap the argument in `Some`. --- src/codec/context.rs | 8 ++++---- src/format/chapter/chapter.rs | 4 ++-- src/format/stream/stream.rs | 4 ++-- src/format/stream/stream_mut.rs | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/codec/context.rs b/src/codec/context.rs index 41dcae7..58448b3 100644 --- a/src/codec/context.rs +++ b/src/codec/context.rs @@ -125,13 +125,13 @@ impl Context { Parameters::from(self) } - pub fn time_base(&self) -> Rational { - unsafe { Rational::from((*self.as_ptr()).time_base) } + pub fn time_base(&self) -> Option { + unsafe { Rational::from((*self.as_ptr()).time_base).non_zero() } } - pub fn set_time_base>(&mut self, value: R) { + pub fn set_time_base>(&mut self, value: Option) { unsafe { - (*self.as_mut_ptr()).time_base = value.into().into(); + (*self.as_mut_ptr()).time_base = value.map(Into::into).unwrap_or(Rational::ZERO).into(); } } diff --git a/src/format/chapter/chapter.rs b/src/format/chapter/chapter.rs index d5f7d56..00f19cd 100644 --- a/src/format/chapter/chapter.rs +++ b/src/format/chapter/chapter.rs @@ -26,8 +26,8 @@ impl<'a> Chapter<'a> { unsafe { (*self.as_ptr()).id } } - pub fn time_base(&self) -> Rational { - unsafe { Rational::from((*self.as_ptr()).time_base) } + pub fn time_base(&self) -> Option { + unsafe { Rational::from((*self.as_ptr()).time_base).non_zero() } } pub fn start(&self) -> i64 { diff --git a/src/format/stream/stream.rs b/src/format/stream/stream.rs index 60bbd5f..b4f9114 100644 --- a/src/format/stream/stream.rs +++ b/src/format/stream/stream.rs @@ -53,8 +53,8 @@ impl<'a> Stream<'a> { unsafe { (*self.as_ptr()).index as usize } } - pub fn time_base(&self) -> Rational { - unsafe { Rational::from((*self.as_ptr()).time_base) } + pub fn time_base(&self) -> Option { + unsafe { Rational::from((*self.as_ptr()).time_base).non_zero() } } pub fn start_time(&self) -> Option { diff --git a/src/format/stream/stream_mut.rs b/src/format/stream/stream_mut.rs index 077cf64..902df39 100644 --- a/src/format/stream/stream_mut.rs +++ b/src/format/stream/stream_mut.rs @@ -26,9 +26,9 @@ impl<'a> StreamMut<'a> { } impl<'a> StreamMut<'a> { - pub fn set_time_base>(&mut self, value: R) { + pub fn set_time_base>(&mut self, value: Option) { unsafe { - (*self.as_mut_ptr()).time_base = value.into().into(); + (*self.as_mut_ptr()).time_base = value.map(Into::into).unwrap_or(Rational::ZERO).into(); } }