Use CompatibleFormattedValue where applicable

See https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1059
This commit is contained in:
François Laignel 2022-07-05 13:13:50 +02:00
parent 865df62f8d
commit a787197254
6 changed files with 120 additions and 151 deletions

View file

@ -14,7 +14,10 @@ use crate::Plugin;
use crate::QueryRef; use crate::QueryRef;
use crate::Rank; use crate::Rank;
use crate::State; use crate::State;
use crate::{Format, FormattedValue, GenericFormattedValue, SpecificFormattedValueFullRange}; use crate::{
CompatibleFormattedValue, Format, FormattedValue, GenericFormattedValue,
SpecificFormattedValueFullRange,
};
use glib::translate::*; use glib::translate::*;
@ -233,7 +236,7 @@ pub trait ElementExtManual: 'static {
start_type: crate::SeekType, start_type: crate::SeekType,
start: V, start: V,
stop_type: crate::SeekType, stop_type: crate::SeekType,
stop: V, stop: impl CompatibleFormattedValue<V>,
) -> Result<(), glib::error::BoolError>; ) -> Result<(), glib::error::BoolError>;
#[doc(alias = "gst_element_seek_simple")] #[doc(alias = "gst_element_seek_simple")]
fn seek_simple( fn seek_simple(
@ -667,9 +670,9 @@ impl<O: IsA<Element>> ElementExtManual for O {
start_type: crate::SeekType, start_type: crate::SeekType,
start: V, start: V,
stop_type: crate::SeekType, stop_type: crate::SeekType,
stop: V, stop: impl CompatibleFormattedValue<V>,
) -> Result<(), glib::error::BoolError> { ) -> Result<(), glib::error::BoolError> {
assert_eq!(stop.format(), start.format()); let stop = stop.try_into_checked(start).unwrap();
unsafe { unsafe {
glib::result_from_gboolean!( glib::result_from_gboolean!(

View file

@ -2,7 +2,7 @@
use crate::structure::*; use crate::structure::*;
use crate::ClockTime; use crate::ClockTime;
use crate::{FormattedValue, GenericFormattedValue}; use crate::{CompatibleFormattedValue, FormattedValue, GenericFormattedValue};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::cmp; use std::cmp;
@ -642,18 +642,22 @@ declare_concrete_event!(@sticky Buffersize, T);
impl Buffersize<Event> { impl Buffersize<Event> {
#[doc(alias = "gst_event_new_buffer_size")] #[doc(alias = "gst_event_new_buffer_size")]
#[allow(clippy::new_ret_no_self)] #[allow(clippy::new_ret_no_self)]
pub fn new<V: FormattedValue>(minsize: V, maxsize: V, r#async: bool) -> Event { pub fn new<V: FormattedValue>(
minsize: V,
maxsize: impl CompatibleFormattedValue<V>,
r#async: bool,
) -> Event {
skip_assert_initialized!(); skip_assert_initialized!();
Self::builder(minsize, maxsize, r#async).build() Self::builder(minsize, maxsize, r#async).build()
} }
pub fn builder<'a, V: FormattedValue>( pub fn builder<'a, V: FormattedValue>(
minsize: V, minsize: V,
maxsize: V, maxsize: impl CompatibleFormattedValue<V>,
r#async: bool, r#async: bool,
) -> BuffersizeBuilder<'a> { ) -> BuffersizeBuilder<'a> {
assert_initialized_main_thread!(); 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) BuffersizeBuilder::new(minsize.into(), maxsize.into(), r#async)
} }
@ -1033,7 +1037,7 @@ impl Seek<Event> {
start_type: crate::SeekType, start_type: crate::SeekType,
start: V, start: V,
stop_type: crate::SeekType, stop_type: crate::SeekType,
stop: V, stop: impl CompatibleFormattedValue<V>,
) -> Event { ) -> Event {
skip_assert_initialized!(); skip_assert_initialized!();
Self::builder(rate, flags, start_type, start, stop_type, stop).build() Self::builder(rate, flags, start_type, start, stop_type, stop).build()
@ -1045,10 +1049,10 @@ impl Seek<Event> {
start_type: crate::SeekType, start_type: crate::SeekType,
start: V, start: V,
stop_type: crate::SeekType, stop_type: crate::SeekType,
stop: V, stop: impl CompatibleFormattedValue<V>,
) -> SeekBuilder<'a> { ) -> SeekBuilder<'a> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
assert_eq!(start.format(), stop.format()); let stop = stop.try_into_checked(start).unwrap();
SeekBuilder::new( SeekBuilder::new(
rate, rate,

View file

@ -2600,8 +2600,12 @@ impl<'a> QosBuilder<'a> {
} }
} }
pub fn stats<V: FormattedValue>(self, processed: V, dropped: V) -> Self { pub fn stats<V: FormattedValue>(
assert_eq!(processed.format(), dropped.format()); self,
processed: V,
dropped: impl CompatibleFormattedValue<V>,
) -> Self {
let dropped = dropped.try_into_checked(processed).unwrap();
Self { Self {
stats: Some((processed.into(), dropped.into())), stats: Some((processed.into(), dropped.into())),
..self ..self

View file

@ -1,7 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file. // Take a look at the license at the top of the repository in the LICENSE file.
use crate::structure::*; use crate::structure::*;
use crate::{FormattedValue, GenericFormattedValue}; use crate::{CompatibleFormattedValue, FormattedValue, GenericFormattedValue};
use std::borrow::{Borrow, BorrowMut}; use std::borrow::{Borrow, BorrowMut};
use std::ffi::CStr; use std::ffi::CStr;
@ -488,9 +488,14 @@ impl Seeking {
} }
#[doc(alias = "gst_query_set_seeking")] #[doc(alias = "gst_query_set_seeking")]
pub fn set<V: FormattedValue>(&mut self, seekable: bool, start: V, end: V) { pub fn set<V: FormattedValue>(
&mut self,
seekable: bool,
start: V,
end: impl CompatibleFormattedValue<V>,
) {
assert_eq!(self.format(), start.format()); assert_eq!(self.format(), start.format());
assert_eq!(start.format(), end.format()); let end = end.try_into_checked(start).unwrap();
unsafe { unsafe {
ffi::gst_query_set_seeking( ffi::gst_query_set_seeking(
@ -556,8 +561,13 @@ impl Segment {
} }
#[doc(alias = "gst_query_set_segment")] #[doc(alias = "gst_query_set_segment")]
pub fn set<V: FormattedValue>(&mut self, rate: f64, start: V, stop: V) { pub fn set<V: FormattedValue>(
assert_eq!(start.format(), stop.format()); &mut self,
rate: f64,
start: V,
stop: impl CompatibleFormattedValue<V>,
) {
let stop = stop.try_into_checked(start).unwrap();
unsafe { unsafe {
ffi::gst_query_set_segment( ffi::gst_query_set_segment(
@ -836,9 +846,14 @@ impl Buffering {
} }
#[doc(alias = "gst_query_set_buffering_range")] #[doc(alias = "gst_query_set_buffering_range")]
pub fn set_range<V: FormattedValue>(&mut self, start: V, stop: V, estimated_total: i64) { pub fn set_range<V: FormattedValue>(
&mut self,
start: V,
stop: impl CompatibleFormattedValue<V>,
estimated_total: i64,
) {
assert_eq!(self.format(), start.format()); assert_eq!(self.format(), start.format());
assert_eq!(start.format(), stop.format()); let stop = stop.try_into_checked(start).unwrap();
unsafe { unsafe {
ffi::gst_query_set_buffering_range( ffi::gst_query_set_buffering_range(
@ -872,13 +887,16 @@ impl Buffering {
} }
#[doc(alias = "gst_query_add_buffering_range")] #[doc(alias = "gst_query_add_buffering_range")]
pub fn add_buffering_ranges<V: FormattedValue + Copy>(&mut self, ranges: &[(V, V)]) { pub fn add_buffering_ranges<V: FormattedValue, U: CompatibleFormattedValue<V> + Copy>(
&mut self,
ranges: &[(V, U)],
) {
unsafe { unsafe {
let fmt = self.format(); let fmt = self.format();
for &(start, stop) in ranges { for &(start, stop) in ranges {
assert_eq!(start.format(), fmt); assert_eq!(start.format(), fmt);
assert_eq!(stop.format(), fmt); let stop = stop.try_into_checked(start).unwrap();
ffi::gst_query_add_buffering_range( ffi::gst_query_add_buffering_range(
self.as_mut_ptr(), self.as_mut_ptr(),
start.into_raw_value(), start.into_raw_value(),

View file

@ -4,7 +4,9 @@ use crate::Format;
use crate::GenericFormattedValue; use crate::GenericFormattedValue;
use crate::SeekFlags; use crate::SeekFlags;
use crate::SeekType; use crate::SeekType;
use crate::{FormattedValue, FormattedValueFullRange, FormattedValueIntrinsic}; use crate::{
CompatibleFormattedValue, FormattedValue, FormattedValueFullRange, FormattedValueIntrinsic,
};
use glib::translate::*; use glib::translate::*;
use glib::StaticType; use glib::StaticType;
use std::fmt; use std::fmt;
@ -92,18 +94,13 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_clip")] #[doc(alias = "gst_segment_clip")]
pub fn clip<V: Into<T::FullRange>>( pub fn clip(
&self, &self,
start: V, start: impl CompatibleFormattedValue<T>,
stop: V, stop: impl CompatibleFormattedValue<T>,
) -> Option<(T::FullRange, T::FullRange)> { ) -> Option<(T::FullRange, T::FullRange)> {
let start = start.into(); let start = start.try_into_checked_explicit(self.format()).unwrap();
let stop = stop.into(); let stop = stop.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), start.format());
assert_eq!(self.format(), stop.format());
}
unsafe { unsafe {
let mut clip_start = mem::MaybeUninit::uninit(); let mut clip_start = mem::MaybeUninit::uninit();
@ -129,23 +126,18 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
#[doc(alias = "gst_segment_do_seek")] #[doc(alias = "gst_segment_do_seek")]
pub fn do_seek<V: Into<T::FullRange>>( pub fn do_seek(
&mut self, &mut self,
rate: f64, rate: f64,
flags: SeekFlags, flags: SeekFlags,
start_type: SeekType, start_type: SeekType,
start: V, start: impl CompatibleFormattedValue<T>,
stop_type: SeekType, stop_type: SeekType,
stop: V, stop: impl CompatibleFormattedValue<T>,
) -> Option<bool> { ) -> Option<bool> {
skip_assert_initialized!(); skip_assert_initialized!();
let start = start.into(); let start = start.try_into_checked_explicit(self.format()).unwrap();
let stop = stop.into(); let stop = stop.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), start.format());
assert_eq!(self.format(), stop.format());
}
unsafe { unsafe {
let mut update = mem::MaybeUninit::uninit(); let mut update = mem::MaybeUninit::uninit();
@ -183,15 +175,13 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_position_from_running_time")] #[doc(alias = "gst_segment_position_from_running_time")]
pub fn position_from_running_time<V: Into<T::FullRange>>( pub fn position_from_running_time(
&self, &self,
running_time: V, running_time: impl CompatibleFormattedValue<T>,
) -> T::FullRange { ) -> T::FullRange {
let running_time = running_time.into(); let running_time = running_time
.try_into_checked_explicit(self.format())
if T::default_format() == Format::Undefined { .unwrap();
assert_eq!(self.format(), running_time.format());
}
unsafe { unsafe {
T::FullRange::from_raw( T::FullRange::from_raw(
@ -206,15 +196,13 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_position_from_running_time_full")] #[doc(alias = "gst_segment_position_from_running_time_full")]
pub fn position_from_running_time_full<V: Into<T::FullRange>>( pub fn position_from_running_time_full(
&self, &self,
running_time: V, running_time: impl CompatibleFormattedValue<T>,
) -> (i32, T::FullRange) { ) -> (i32, T::FullRange) {
let running_time = running_time.into(); let running_time = running_time
.try_into_checked_explicit(self.format())
if T::default_format() == Format::Undefined { .unwrap();
assert_eq!(self.format(), running_time.format());
}
unsafe { unsafe {
let mut position = mem::MaybeUninit::uninit(); let mut position = mem::MaybeUninit::uninit();
@ -232,12 +220,13 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_position_from_stream_time")] #[doc(alias = "gst_segment_position_from_stream_time")]
pub fn position_from_stream_time<V: Into<T::FullRange>>(&self, stream_time: V) -> T::FullRange { pub fn position_from_stream_time(
let stream_time = stream_time.into(); &self,
stream_time: impl CompatibleFormattedValue<T>,
if T::default_format() == Format::Undefined { ) -> T::FullRange {
assert_eq!(self.format(), stream_time.format()); let stream_time = stream_time
} .try_into_checked_explicit(self.format())
.unwrap();
unsafe { unsafe {
T::FullRange::from_raw( T::FullRange::from_raw(
@ -252,15 +241,13 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_position_from_stream_time_full")] #[doc(alias = "gst_segment_position_from_stream_time_full")]
pub fn position_from_stream_time_full<V: Into<T::FullRange>>( pub fn position_from_stream_time_full(
&self, &self,
stream_time: V, stream_time: impl CompatibleFormattedValue<T>,
) -> (i32, T::FullRange) { ) -> (i32, T::FullRange) {
let stream_time = stream_time.into(); let stream_time = stream_time
.try_into_checked_explicit(self.format())
if T::default_format() == Format::Undefined { .unwrap();
assert_eq!(self.format(), stream_time.format());
}
unsafe { unsafe {
let mut position = mem::MaybeUninit::uninit(); let mut position = mem::MaybeUninit::uninit();
@ -278,15 +265,13 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_set_running_time")] #[doc(alias = "gst_segment_set_running_time")]
pub fn set_running_time<V: Into<T::FullRange>>( pub fn set_running_time(
&mut self, &mut self,
running_time: V, running_time: impl CompatibleFormattedValue<T>,
) -> Result<(), glib::BoolError> { ) -> Result<(), glib::BoolError> {
let running_time = running_time.into(); let running_time = running_time
.try_into_checked_explicit(self.format())
if T::default_format() == Format::Undefined { .unwrap();
assert_eq!(self.format(), running_time.format());
}
unsafe { unsafe {
glib::result_from_gboolean!( glib::result_from_gboolean!(
@ -301,12 +286,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_to_running_time")] #[doc(alias = "gst_segment_to_running_time")]
pub fn to_running_time<V: Into<T::FullRange>>(&self, position: V) -> T::FullRange { pub fn to_running_time(&self, position: impl CompatibleFormattedValue<T>) -> T::FullRange {
let position = position.into(); let position = position.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), position.format());
}
unsafe { unsafe {
T::FullRange::from_raw( T::FullRange::from_raw(
@ -321,12 +302,11 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_to_running_time_full")] #[doc(alias = "gst_segment_to_running_time_full")]
pub fn to_running_time_full<V: Into<T::FullRange>>(&self, position: V) -> (i32, T::FullRange) { pub fn to_running_time_full(
let position = position.into(); &self,
position: impl CompatibleFormattedValue<T>,
if T::default_format() == Format::Undefined { ) -> (i32, T::FullRange) {
assert_eq!(self.format(), position.format()); let position = position.try_into_checked_explicit(self.format()).unwrap();
}
unsafe { unsafe {
let mut running_time = mem::MaybeUninit::uninit(); let mut running_time = mem::MaybeUninit::uninit();
@ -344,12 +324,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_to_stream_time")] #[doc(alias = "gst_segment_to_stream_time")]
pub fn to_stream_time<V: Into<T::FullRange>>(&self, position: V) -> T::FullRange { pub fn to_stream_time(&self, position: impl CompatibleFormattedValue<T>) -> T::FullRange {
let position = position.into(); let position = position.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), position.format());
}
unsafe { unsafe {
T::FullRange::from_raw( T::FullRange::from_raw(
@ -364,12 +340,11 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
} }
#[doc(alias = "gst_segment_to_stream_time_full")] #[doc(alias = "gst_segment_to_stream_time_full")]
pub fn to_stream_time_full<V: Into<T::FullRange>>(&self, position: V) -> (i32, T::FullRange) { pub fn to_stream_time_full(
let position = position.into(); &self,
position: impl CompatibleFormattedValue<T>,
if T::default_format() == Format::Undefined { ) -> (i32, T::FullRange) {
assert_eq!(self.format(), position.format()); let position = position.try_into_checked_explicit(self.format()).unwrap();
}
unsafe { unsafe {
let mut stream_time = mem::MaybeUninit::uninit(); let mut stream_time = mem::MaybeUninit::uninit();
@ -427,13 +402,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
unsafe { T::FullRange::from_raw(self.format(), self.0.base as i64) } unsafe { T::FullRange::from_raw(self.format(), self.0.base as i64) }
} }
pub fn set_base<V: Into<T::FullRange>>(&mut self, base: V) { pub fn set_base(&mut self, base: impl CompatibleFormattedValue<T>) {
let base = base.into(); let base = base.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), base.format());
}
self.0.base = unsafe { base.into_raw_value() } as u64; self.0.base = unsafe { base.into_raw_value() } as u64;
} }
@ -442,13 +412,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
unsafe { T::FullRange::from_raw(self.format(), self.0.offset as i64) } unsafe { T::FullRange::from_raw(self.format(), self.0.offset as i64) }
} }
pub fn set_offset<V: Into<T::FullRange>>(&mut self, offset: V) { pub fn set_offset(&mut self, offset: impl CompatibleFormattedValue<T>) {
let offset = offset.into(); let offset = offset.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), offset.format());
}
self.0.offset = unsafe { offset.into_raw_value() } as u64; self.0.offset = unsafe { offset.into_raw_value() } as u64;
} }
@ -457,13 +422,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
unsafe { T::FullRange::from_raw(self.format(), self.0.start as i64) } unsafe { T::FullRange::from_raw(self.format(), self.0.start as i64) }
} }
pub fn set_start<V: Into<T::FullRange>>(&mut self, start: V) { pub fn set_start(&mut self, start: impl CompatibleFormattedValue<T>) {
let start = start.into(); let start = start.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), start.format());
}
self.0.start = unsafe { start.into_raw_value() } as u64; self.0.start = unsafe { start.into_raw_value() } as u64;
} }
@ -472,13 +432,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
unsafe { T::FullRange::from_raw(self.format(), self.0.stop as i64) } unsafe { T::FullRange::from_raw(self.format(), self.0.stop as i64) }
} }
pub fn set_stop<V: Into<T::FullRange>>(&mut self, stop: V) { pub fn set_stop(&mut self, stop: impl CompatibleFormattedValue<T>) {
let stop = stop.into(); let stop = stop.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), stop.format());
}
self.0.stop = unsafe { stop.into_raw_value() } as u64; self.0.stop = unsafe { stop.into_raw_value() } as u64;
} }
@ -487,13 +442,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
unsafe { T::FullRange::from_raw(self.format(), self.0.time as i64) } unsafe { T::FullRange::from_raw(self.format(), self.0.time as i64) }
} }
pub fn set_time<V: Into<T::FullRange>>(&mut self, time: V) { pub fn set_time(&mut self, time: impl CompatibleFormattedValue<T>) {
let time = time.into(); let time = time.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), time.format());
}
self.0.time = unsafe { time.into_raw_value() } as u64; self.0.time = unsafe { time.into_raw_value() } as u64;
} }
@ -502,13 +452,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
unsafe { T::FullRange::from_raw(self.format(), self.0.position as i64) } unsafe { T::FullRange::from_raw(self.format(), self.0.position as i64) }
} }
pub fn set_position<V: Into<T::FullRange>>(&mut self, position: V) { pub fn set_position(&mut self, position: impl CompatibleFormattedValue<T>) {
let position = position.into(); let position = position.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), position.format());
}
self.0.position = unsafe { position.into_raw_value() } as u64; self.0.position = unsafe { position.into_raw_value() } as u64;
} }
@ -517,13 +462,8 @@ impl<T: FormattedValueIntrinsic> FormattedSegment<T> {
unsafe { T::FullRange::from_raw(self.format(), self.0.duration as i64) } unsafe { T::FullRange::from_raw(self.format(), self.0.duration as i64) }
} }
pub fn set_duration<V: Into<T::FullRange>>(&mut self, duration: V) { pub fn set_duration(&mut self, duration: impl CompatibleFormattedValue<T>) {
let duration = duration.into(); let duration = duration.try_into_checked_explicit(self.format()).unwrap();
if T::default_format() == Format::Undefined {
assert_eq!(self.format(), duration.format());
}
self.0.duration = unsafe { duration.into_raw_value() } as u64; self.0.duration = unsafe { duration.into_raw_value() } as u64;
} }
} }

View file

@ -26,7 +26,7 @@ enum Command {
fn send_seek_event(pipeline: &Element, rate: f64) -> bool { fn send_seek_event(pipeline: &Element, rate: f64) -> bool {
// Obtain the current position, needed for the seek event // Obtain the current position, needed for the seek event
let position = match pipeline.query_position() { let position = match pipeline.query_position::<gst::ClockTime>() {
Some(pos) => pos, Some(pos) => pos,
None => { None => {
eprintln!("Unable to retrieve current position...\r"); eprintln!("Unable to retrieve current position...\r");