From a787197254b37b5c3f6c7d79a437571806fda500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Laignel?= Date: Tue, 5 Jul 2022 13:13:50 +0200 Subject: [PATCH] Use CompatibleFormattedValue where applicable See https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1059 --- gstreamer/src/element.rs | 11 +- gstreamer/src/event.rs | 18 ++- gstreamer/src/message.rs | 8 +- gstreamer/src/query.rs | 36 +++-- gstreamer/src/segment.rs | 196 +++++++++---------------- tutorials/src/bin/basic-tutorial-13.rs | 2 +- 6 files changed, 120 insertions(+), 151 deletions(-) diff --git a/gstreamer/src/element.rs b/gstreamer/src/element.rs index 7b14ae084..d78dcf12b 100644 --- a/gstreamer/src/element.rs +++ b/gstreamer/src/element.rs @@ -14,7 +14,10 @@ use crate::Plugin; use crate::QueryRef; use crate::Rank; use crate::State; -use crate::{Format, FormattedValue, GenericFormattedValue, SpecificFormattedValueFullRange}; +use crate::{ + CompatibleFormattedValue, Format, FormattedValue, GenericFormattedValue, + SpecificFormattedValueFullRange, +}; use glib::translate::*; @@ -233,7 +236,7 @@ pub trait ElementExtManual: 'static { start_type: crate::SeekType, start: V, stop_type: crate::SeekType, - stop: V, + stop: impl CompatibleFormattedValue, ) -> Result<(), glib::error::BoolError>; #[doc(alias = "gst_element_seek_simple")] fn seek_simple( @@ -667,9 +670,9 @@ impl> ElementExtManual for O { start_type: crate::SeekType, start: V, stop_type: crate::SeekType, - stop: V, + stop: impl CompatibleFormattedValue, ) -> Result<(), glib::error::BoolError> { - assert_eq!(stop.format(), start.format()); + let stop = stop.try_into_checked(start).unwrap(); unsafe { glib::result_from_gboolean!( diff --git a/gstreamer/src/event.rs b/gstreamer/src/event.rs index 20a412154..9c637189d 100644 --- a/gstreamer/src/event.rs +++ b/gstreamer/src/event.rs @@ -2,7 +2,7 @@ use crate::structure::*; use crate::ClockTime; -use crate::{FormattedValue, GenericFormattedValue}; +use crate::{CompatibleFormattedValue, FormattedValue, GenericFormattedValue}; use std::borrow::Borrow; use std::cmp; @@ -642,18 +642,22 @@ declare_concrete_event!(@sticky Buffersize, T); impl Buffersize { #[doc(alias = "gst_event_new_buffer_size")] #[allow(clippy::new_ret_no_self)] - pub fn new(minsize: V, maxsize: V, r#async: bool) -> Event { + pub fn new( + minsize: V, + maxsize: impl CompatibleFormattedValue, + r#async: bool, + ) -> Event { skip_assert_initialized!(); Self::builder(minsize, maxsize, r#async).build() } pub fn builder<'a, V: FormattedValue>( minsize: V, - maxsize: V, + maxsize: impl CompatibleFormattedValue, r#async: bool, ) -> BuffersizeBuilder<'a> { assert_initialized_main_thread!(); - assert_eq!(minsize.format(), maxsize.format()); + let maxsize = maxsize.try_into_checked(minsize).unwrap(); BuffersizeBuilder::new(minsize.into(), maxsize.into(), r#async) } @@ -1033,7 +1037,7 @@ impl Seek { start_type: crate::SeekType, start: V, stop_type: crate::SeekType, - stop: V, + stop: impl CompatibleFormattedValue, ) -> Event { skip_assert_initialized!(); Self::builder(rate, flags, start_type, start, stop_type, stop).build() @@ -1045,10 +1049,10 @@ impl Seek { start_type: crate::SeekType, start: V, stop_type: crate::SeekType, - stop: V, + stop: impl CompatibleFormattedValue, ) -> SeekBuilder<'a> { assert_initialized_main_thread!(); - assert_eq!(start.format(), stop.format()); + let stop = stop.try_into_checked(start).unwrap(); SeekBuilder::new( rate, diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index f6d26fc86..718d813a9 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -2600,8 +2600,12 @@ impl<'a> QosBuilder<'a> { } } - pub fn stats(self, processed: V, dropped: V) -> Self { - assert_eq!(processed.format(), dropped.format()); + pub fn stats( + self, + processed: V, + dropped: impl CompatibleFormattedValue, + ) -> Self { + let dropped = dropped.try_into_checked(processed).unwrap(); Self { stats: Some((processed.into(), dropped.into())), ..self diff --git a/gstreamer/src/query.rs b/gstreamer/src/query.rs index c4b3154b9..24123142d 100644 --- a/gstreamer/src/query.rs +++ b/gstreamer/src/query.rs @@ -1,7 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. use crate::structure::*; -use crate::{FormattedValue, GenericFormattedValue}; +use crate::{CompatibleFormattedValue, FormattedValue, GenericFormattedValue}; use std::borrow::{Borrow, BorrowMut}; use std::ffi::CStr; @@ -488,9 +488,14 @@ impl Seeking { } #[doc(alias = "gst_query_set_seeking")] - pub fn set(&mut self, seekable: bool, start: V, end: V) { + pub fn set( + &mut self, + seekable: bool, + start: V, + end: impl CompatibleFormattedValue, + ) { assert_eq!(self.format(), start.format()); - assert_eq!(start.format(), end.format()); + let end = end.try_into_checked(start).unwrap(); unsafe { ffi::gst_query_set_seeking( @@ -556,8 +561,13 @@ impl Segment { } #[doc(alias = "gst_query_set_segment")] - pub fn set(&mut self, rate: f64, start: V, stop: V) { - assert_eq!(start.format(), stop.format()); + pub fn set( + &mut self, + rate: f64, + start: V, + stop: impl CompatibleFormattedValue, + ) { + let stop = stop.try_into_checked(start).unwrap(); unsafe { ffi::gst_query_set_segment( @@ -836,9 +846,14 @@ impl Buffering { } #[doc(alias = "gst_query_set_buffering_range")] - pub fn set_range(&mut self, start: V, stop: V, estimated_total: i64) { + pub fn set_range( + &mut self, + start: V, + stop: impl CompatibleFormattedValue, + estimated_total: i64, + ) { assert_eq!(self.format(), start.format()); - assert_eq!(start.format(), stop.format()); + let stop = stop.try_into_checked(start).unwrap(); unsafe { ffi::gst_query_set_buffering_range( @@ -872,13 +887,16 @@ impl Buffering { } #[doc(alias = "gst_query_add_buffering_range")] - pub fn add_buffering_ranges(&mut self, ranges: &[(V, V)]) { + pub fn add_buffering_ranges + Copy>( + &mut self, + ranges: &[(V, U)], + ) { unsafe { let fmt = self.format(); for &(start, stop) in ranges { assert_eq!(start.format(), fmt); - assert_eq!(stop.format(), fmt); + let stop = stop.try_into_checked(start).unwrap(); ffi::gst_query_add_buffering_range( self.as_mut_ptr(), start.into_raw_value(), diff --git a/gstreamer/src/segment.rs b/gstreamer/src/segment.rs index 573772aa1..fc970ef27 100644 --- a/gstreamer/src/segment.rs +++ b/gstreamer/src/segment.rs @@ -4,7 +4,9 @@ use crate::Format; use crate::GenericFormattedValue; use crate::SeekFlags; use crate::SeekType; -use crate::{FormattedValue, FormattedValueFullRange, FormattedValueIntrinsic}; +use crate::{ + CompatibleFormattedValue, FormattedValue, FormattedValueFullRange, FormattedValueIntrinsic, +}; use glib::translate::*; use glib::StaticType; use std::fmt; @@ -92,18 +94,13 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_clip")] - pub fn clip>( + pub fn clip( &self, - start: V, - stop: V, + start: impl CompatibleFormattedValue, + stop: impl CompatibleFormattedValue, ) -> Option<(T::FullRange, T::FullRange)> { - let start = start.into(); - let stop = stop.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), start.format()); - assert_eq!(self.format(), stop.format()); - } + let start = start.try_into_checked_explicit(self.format()).unwrap(); + let stop = stop.try_into_checked_explicit(self.format()).unwrap(); unsafe { let mut clip_start = mem::MaybeUninit::uninit(); @@ -129,23 +126,18 @@ impl FormattedSegment { #[allow(clippy::too_many_arguments)] #[doc(alias = "gst_segment_do_seek")] - pub fn do_seek>( + pub fn do_seek( &mut self, rate: f64, flags: SeekFlags, start_type: SeekType, - start: V, + start: impl CompatibleFormattedValue, stop_type: SeekType, - stop: V, + stop: impl CompatibleFormattedValue, ) -> Option { skip_assert_initialized!(); - let start = start.into(); - let stop = stop.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), start.format()); - assert_eq!(self.format(), stop.format()); - } + let start = start.try_into_checked_explicit(self.format()).unwrap(); + let stop = stop.try_into_checked_explicit(self.format()).unwrap(); unsafe { let mut update = mem::MaybeUninit::uninit(); @@ -183,15 +175,13 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_position_from_running_time")] - pub fn position_from_running_time>( + pub fn position_from_running_time( &self, - running_time: V, + running_time: impl CompatibleFormattedValue, ) -> T::FullRange { - let running_time = running_time.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), running_time.format()); - } + let running_time = running_time + .try_into_checked_explicit(self.format()) + .unwrap(); unsafe { T::FullRange::from_raw( @@ -206,15 +196,13 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_position_from_running_time_full")] - pub fn position_from_running_time_full>( + pub fn position_from_running_time_full( &self, - running_time: V, + running_time: impl CompatibleFormattedValue, ) -> (i32, T::FullRange) { - let running_time = running_time.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), running_time.format()); - } + let running_time = running_time + .try_into_checked_explicit(self.format()) + .unwrap(); unsafe { let mut position = mem::MaybeUninit::uninit(); @@ -232,12 +220,13 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_position_from_stream_time")] - pub fn position_from_stream_time>(&self, stream_time: V) -> T::FullRange { - let stream_time = stream_time.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), stream_time.format()); - } + pub fn position_from_stream_time( + &self, + stream_time: impl CompatibleFormattedValue, + ) -> T::FullRange { + let stream_time = stream_time + .try_into_checked_explicit(self.format()) + .unwrap(); unsafe { T::FullRange::from_raw( @@ -252,15 +241,13 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_position_from_stream_time_full")] - pub fn position_from_stream_time_full>( + pub fn position_from_stream_time_full( &self, - stream_time: V, + stream_time: impl CompatibleFormattedValue, ) -> (i32, T::FullRange) { - let stream_time = stream_time.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), stream_time.format()); - } + let stream_time = stream_time + .try_into_checked_explicit(self.format()) + .unwrap(); unsafe { let mut position = mem::MaybeUninit::uninit(); @@ -278,15 +265,13 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_set_running_time")] - pub fn set_running_time>( + pub fn set_running_time( &mut self, - running_time: V, + running_time: impl CompatibleFormattedValue, ) -> Result<(), glib::BoolError> { - let running_time = running_time.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), running_time.format()); - } + let running_time = running_time + .try_into_checked_explicit(self.format()) + .unwrap(); unsafe { glib::result_from_gboolean!( @@ -301,12 +286,8 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_to_running_time")] - pub fn to_running_time>(&self, position: V) -> T::FullRange { - let position = position.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), position.format()); - } + pub fn to_running_time(&self, position: impl CompatibleFormattedValue) -> T::FullRange { + let position = position.try_into_checked_explicit(self.format()).unwrap(); unsafe { T::FullRange::from_raw( @@ -321,12 +302,11 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_to_running_time_full")] - pub fn to_running_time_full>(&self, position: V) -> (i32, T::FullRange) { - let position = position.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), position.format()); - } + pub fn to_running_time_full( + &self, + position: impl CompatibleFormattedValue, + ) -> (i32, T::FullRange) { + let position = position.try_into_checked_explicit(self.format()).unwrap(); unsafe { let mut running_time = mem::MaybeUninit::uninit(); @@ -344,12 +324,8 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_to_stream_time")] - pub fn to_stream_time>(&self, position: V) -> T::FullRange { - let position = position.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), position.format()); - } + pub fn to_stream_time(&self, position: impl CompatibleFormattedValue) -> T::FullRange { + let position = position.try_into_checked_explicit(self.format()).unwrap(); unsafe { T::FullRange::from_raw( @@ -364,12 +340,11 @@ impl FormattedSegment { } #[doc(alias = "gst_segment_to_stream_time_full")] - pub fn to_stream_time_full>(&self, position: V) -> (i32, T::FullRange) { - let position = position.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), position.format()); - } + pub fn to_stream_time_full( + &self, + position: impl CompatibleFormattedValue, + ) -> (i32, T::FullRange) { + let position = position.try_into_checked_explicit(self.format()).unwrap(); unsafe { let mut stream_time = mem::MaybeUninit::uninit(); @@ -427,13 +402,8 @@ impl FormattedSegment { unsafe { T::FullRange::from_raw(self.format(), self.0.base as i64) } } - pub fn set_base>(&mut self, base: V) { - let base = base.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), base.format()); - } - + pub fn set_base(&mut self, base: impl CompatibleFormattedValue) { + let base = base.try_into_checked_explicit(self.format()).unwrap(); self.0.base = unsafe { base.into_raw_value() } as u64; } @@ -442,13 +412,8 @@ impl FormattedSegment { unsafe { T::FullRange::from_raw(self.format(), self.0.offset as i64) } } - pub fn set_offset>(&mut self, offset: V) { - let offset = offset.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), offset.format()); - } - + pub fn set_offset(&mut self, offset: impl CompatibleFormattedValue) { + let offset = offset.try_into_checked_explicit(self.format()).unwrap(); self.0.offset = unsafe { offset.into_raw_value() } as u64; } @@ -457,13 +422,8 @@ impl FormattedSegment { unsafe { T::FullRange::from_raw(self.format(), self.0.start as i64) } } - pub fn set_start>(&mut self, start: V) { - let start = start.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), start.format()); - } - + pub fn set_start(&mut self, start: impl CompatibleFormattedValue) { + let start = start.try_into_checked_explicit(self.format()).unwrap(); self.0.start = unsafe { start.into_raw_value() } as u64; } @@ -472,13 +432,8 @@ impl FormattedSegment { unsafe { T::FullRange::from_raw(self.format(), self.0.stop as i64) } } - pub fn set_stop>(&mut self, stop: V) { - let stop = stop.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), stop.format()); - } - + pub fn set_stop(&mut self, stop: impl CompatibleFormattedValue) { + let stop = stop.try_into_checked_explicit(self.format()).unwrap(); self.0.stop = unsafe { stop.into_raw_value() } as u64; } @@ -487,13 +442,8 @@ impl FormattedSegment { unsafe { T::FullRange::from_raw(self.format(), self.0.time as i64) } } - pub fn set_time>(&mut self, time: V) { - let time = time.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), time.format()); - } - + pub fn set_time(&mut self, time: impl CompatibleFormattedValue) { + let time = time.try_into_checked_explicit(self.format()).unwrap(); self.0.time = unsafe { time.into_raw_value() } as u64; } @@ -502,13 +452,8 @@ impl FormattedSegment { unsafe { T::FullRange::from_raw(self.format(), self.0.position as i64) } } - pub fn set_position>(&mut self, position: V) { - let position = position.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), position.format()); - } - + pub fn set_position(&mut self, position: impl CompatibleFormattedValue) { + let position = position.try_into_checked_explicit(self.format()).unwrap(); self.0.position = unsafe { position.into_raw_value() } as u64; } @@ -517,13 +462,8 @@ impl FormattedSegment { unsafe { T::FullRange::from_raw(self.format(), self.0.duration as i64) } } - pub fn set_duration>(&mut self, duration: V) { - let duration = duration.into(); - - if T::default_format() == Format::Undefined { - assert_eq!(self.format(), duration.format()); - } - + pub fn set_duration(&mut self, duration: impl CompatibleFormattedValue) { + let duration = duration.try_into_checked_explicit(self.format()).unwrap(); self.0.duration = unsafe { duration.into_raw_value() } as u64; } } diff --git a/tutorials/src/bin/basic-tutorial-13.rs b/tutorials/src/bin/basic-tutorial-13.rs index 3bf677b21..7ad59e67d 100644 --- a/tutorials/src/bin/basic-tutorial-13.rs +++ b/tutorials/src/bin/basic-tutorial-13.rs @@ -26,7 +26,7 @@ enum Command { fn send_seek_event(pipeline: &Element, rate: f64) -> bool { // Obtain the current position, needed for the seek event - let position = match pipeline.query_position() { + let position = match pipeline.query_position::() { Some(pos) => pos, None => { eprintln!("Unable to retrieve current position...\r");