From 246a54368dd46901e3e1fbad2be3ea10c4e9bba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sat, 11 Nov 2017 12:22:31 +0100 Subject: [PATCH] Clean up Query API a bit There's now get_result() instead of get(), and separate getters for only getting the constructor arguments of each query (otherwise query handlers will get useless values when trying to answer a query). --- examples/src/bin/queries.rs | 4 +- gstreamer/src/query.rs | 126 +++++++++++++++++++++----- tutorials/src/bin/basic-tutorial-4.rs | 2 +- 3 files changed, 107 insertions(+), 25 deletions(-) diff --git a/examples/src/bin/queries.rs b/examples/src/bin/queries.rs index cfcbab298..e283f24fb 100644 --- a/examples/src/bin/queries.rs +++ b/examples/src/bin/queries.rs @@ -32,7 +32,7 @@ fn main() { let mut q = gst::Query::new_position(gst::Format::Time); if pipeline.query(q.get_mut().unwrap()) { match q.view() { - QueryView::Position(ref p) => Some(p.get()), + QueryView::Position(ref p) => Some(p.get_result()), _ => None, } } else { @@ -45,7 +45,7 @@ fn main() { let mut q = gst::Query::new_duration(gst::Format::Time); if pipeline.query(q.get_mut().unwrap()) { match q.view() { - QueryView::Duration(ref p) => Some(p.get()), + QueryView::Duration(ref p) => Some(p.get_result()), _ => None, } } else { diff --git a/gstreamer/src/query.rs b/gstreamer/src/query.rs index fb5816b7d..748523898 100644 --- a/gstreamer/src/query.rs +++ b/gstreamer/src/query.rs @@ -207,7 +207,7 @@ pub enum QueryView { pub struct Position(T); impl<'a> Position<&'a QueryRef> { - pub fn get(&self) -> ::FormatValue { + pub fn get_result(&self) -> ::FormatValue { unsafe { let mut fmt = mem::uninitialized(); let mut pos = mem::uninitialized(); @@ -218,6 +218,16 @@ impl<'a> Position<&'a QueryRef> { } } + pub fn get_format(&self) -> ::Format { + unsafe { + let mut fmt = mem::uninitialized(); + + ffi::gst_query_parse_position(self.0.as_mut_ptr(), &mut fmt, ptr::null_mut()); + + from_glib(fmt) + } + } + pub fn get_query(&self) -> &QueryRef { self.0 } @@ -226,6 +236,7 @@ impl<'a> Position<&'a QueryRef> { impl<'a> Position<&'a mut QueryRef> { pub fn set>(&mut self, pos: V) { let pos = pos.into(); + assert_eq!(pos.to_format(), self.get_format()); unsafe { ffi::gst_query_set_position( self.0.as_mut_ptr(), @@ -242,7 +253,7 @@ impl<'a> Position<&'a mut QueryRef> { pub struct Duration(T); impl<'a> Duration<&'a QueryRef> { - pub fn get(&self) -> ::FormatValue { + pub fn get_result(&self) -> ::FormatValue { unsafe { let mut fmt = mem::uninitialized(); let mut pos = mem::uninitialized(); @@ -253,6 +264,16 @@ impl<'a> Duration<&'a QueryRef> { } } + pub fn get_format(&self) -> ::Format { + unsafe { + let mut fmt = mem::uninitialized(); + + ffi::gst_query_parse_duration(self.0.as_mut_ptr(), &mut fmt, ptr::null_mut()); + + from_glib(fmt) + } + } + pub fn get_query(&self) -> &QueryRef { self.0 } @@ -261,6 +282,7 @@ impl<'a> Duration<&'a QueryRef> { impl<'a> Duration<&'a mut QueryRef> { pub fn set>(&mut self, dur: V) { let dur = dur.into(); + assert_eq!(dur.to_format(), self.get_format()); unsafe { ffi::gst_query_set_duration( self.0.as_mut_ptr(), @@ -277,7 +299,7 @@ impl<'a> Duration<&'a mut QueryRef> { pub struct Latency(T); impl<'a> Latency<&'a QueryRef> { - pub fn get(&self) -> (bool, u64, u64) { + pub fn get_result(&self) -> (bool, u64, u64) { unsafe { let mut live = mem::uninitialized(); let mut min = mem::uninitialized(); @@ -334,7 +356,7 @@ impl<'a> Rate<&'a mut QueryRef> { pub struct Seeking(T); impl<'a> Seeking<&'a QueryRef> { - pub fn get(&self) -> (bool, ::FormatValue, ::FormatValue) { + pub fn get_result(&self) -> (bool, ::FormatValue, ::FormatValue) { unsafe { let mut fmt = mem::uninitialized(); let mut seekable = mem::uninitialized(); @@ -356,6 +378,21 @@ impl<'a> Seeking<&'a QueryRef> { } } + pub fn get_format(&self) -> ::Format { + unsafe { + let mut fmt = mem::uninitialized(); + ffi::gst_query_parse_seeking( + self.0.as_mut_ptr(), + &mut fmt, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + ); + + from_glib(fmt) + } + } + pub fn get_query(&self) -> &QueryRef { self.0 } @@ -366,6 +403,7 @@ impl<'a> Seeking<&'a mut QueryRef> { let start = start.into(); let end = end.into(); + assert_eq!(self.get_format(), start.to_format()); assert_eq!(start.to_format(), end.to_format()); unsafe { @@ -386,7 +424,7 @@ impl<'a> Seeking<&'a mut QueryRef> { pub struct Segment(T); impl<'a> Segment<&'a QueryRef> { - pub fn get(&self) -> (f64, ::FormatValue, ::FormatValue) { + pub fn get_result(&self) -> (f64, ::FormatValue, ::FormatValue) { unsafe { let mut rate = mem::uninitialized(); let mut fmt = mem::uninitialized(); @@ -408,6 +446,21 @@ impl<'a> Segment<&'a QueryRef> { } } + pub fn get_format(&self) -> ::Format { + unsafe { + let mut fmt = mem::uninitialized(); + + ffi::gst_query_parse_segment( + self.0.as_mut_ptr(), + ptr::null_mut(), + &mut fmt, + ptr::null_mut(), + ptr::null_mut(), + ); + from_glib(fmt) + } + } + pub fn get_query(&self) -> &QueryRef { self.0 } @@ -438,7 +491,7 @@ impl<'a> Segment<&'a mut QueryRef> { pub struct Convert(T); impl<'a> Convert<&'a QueryRef> { - pub fn get(&self) -> (::FormatValue, ::FormatValue) { + pub fn get_result(&self) -> (::FormatValue, ::FormatValue) { unsafe { let mut src_fmt = mem::uninitialized(); let mut src = mem::uninitialized(); @@ -459,6 +512,26 @@ impl<'a> Convert<&'a QueryRef> { } } + pub fn get(&self) -> (::FormatValue, ::Format) { + unsafe { + let mut src_fmt = mem::uninitialized(); + let mut src = mem::uninitialized(); + let mut dest_fmt = mem::uninitialized(); + + ffi::gst_query_parse_convert( + self.0.as_mut_ptr(), + &mut src_fmt, + &mut src, + &mut dest_fmt, + ptr::null_mut(), + ); + ( + ::FormatValue::new(from_glib(src_fmt), src), + from_glib(dest_fmt), + ) + } + } + pub fn get_query(&self) -> &QueryRef { self.0 } @@ -487,7 +560,7 @@ impl<'a> Convert<&'a mut QueryRef> { pub struct Formats(T); impl<'a> Formats<&'a QueryRef> { - pub fn get(&self) -> Vec<::Format> { + pub fn get_result(&self) -> Vec<::Format> { unsafe { let mut n = mem::uninitialized(); ffi::gst_query_parse_n_formats(self.0.as_mut_ptr(), &mut n); @@ -523,6 +596,22 @@ impl<'a> Formats<&'a mut QueryRef> { pub struct Buffering(T); impl<'a> Buffering<&'a QueryRef> { + pub fn get_format(&self) -> ::Format { + unsafe { + let mut fmt = mem::uninitialized(); + + ffi::gst_query_parse_buffering_range( + self.0.as_mut_ptr(), + &mut fmt, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + ); + + from_glib(fmt) + } + } + pub fn get_percent(&self) -> (bool, i32) { unsafe { let mut busy = mem::uninitialized(); @@ -626,6 +715,7 @@ impl<'a> Buffering<&'a mut QueryRef> { let start = start.into(); let stop = stop.into(); + assert_eq!(self.get_format(), start.to_format()); assert_eq!(start.to_format(), stop.to_format()); unsafe { @@ -660,15 +750,7 @@ impl<'a> Buffering<&'a mut QueryRef> { pub fn add_buffering_ranges + Copy>(&mut self, ranges: &[(V, V)]) { unsafe { - let mut fmt = mem::uninitialized(); - ffi::gst_query_parse_buffering_range( - self.0.as_mut_ptr(), - &mut fmt, - ptr::null_mut(), - ptr::null_mut(), - ptr::null_mut(), - ); - let fmt = from_glib(fmt); + let fmt = self.get_format(); for &(start, stop) in ranges { let start = start.into(); @@ -804,7 +886,7 @@ impl<'a> Scheduling<&'a QueryRef> { } } - pub fn get(&self) -> (::SchedulingFlags, i32, i32, i32) { + pub fn get_result(&self) -> (::SchedulingFlags, i32, i32, i32) { unsafe { let mut flags = mem::uninitialized(); let mut minsize = mem::uninitialized(); @@ -856,7 +938,7 @@ impl<'a> Scheduling<&'a mut QueryRef> { pub struct AcceptCaps(T); impl<'a> AcceptCaps<&'a QueryRef> { - pub fn get(&self) -> &::CapsRef { + pub fn get_caps(&self) -> &::CapsRef { unsafe { let mut caps = ptr::null_mut(); ffi::gst_query_parse_accept_caps(self.0.as_mut_ptr(), &mut caps); @@ -1035,15 +1117,15 @@ mod tests { match q.view() { QueryView::Position(ref p) => { - let pos = p.get(); - assert_eq!(pos.try_to_time(), Some(::CLOCK_TIME_NONE)); + let fmt = p.get_format(); + assert_eq!(fmt, ::Format::Time); } _ => (), } match q.get_mut().unwrap().view_mut() { QueryView::Position(ref mut p) => { - let pos = p.get(); + let pos = p.get_result(); assert_eq!(pos.try_to_time(), Some(::CLOCK_TIME_NONE)); p.set(2 * ::SECOND); } @@ -1052,7 +1134,7 @@ mod tests { match q.view() { QueryView::Position(ref p) => { - let pos = p.get(); + let pos = p.get_result(); assert_eq!(pos.try_to_time(), Some(2 * ::SECOND)); } _ => (), diff --git a/tutorials/src/bin/basic-tutorial-4.rs b/tutorials/src/bin/basic-tutorial-4.rs index cce6ae5c7..0761b9d88 100644 --- a/tutorials/src/bin/basic-tutorial-4.rs +++ b/tutorials/src/bin/basic-tutorial-4.rs @@ -129,7 +129,7 @@ fn handle_message(custom_data: &mut CustomData, msg: &gst::GstRc { - let (seekable, start, end) = seek.get(); + let (seekable, start, end) = seek.get_result(); custom_data.seek_enabled = seekable; if seekable { println!("Seeking is ENABLED from {:?} to {:?}", start, end)