1 // Take a look at the license at the top of the repository in the LICENSE file. 2 3 use crate::structure::*; 4 use crate::GenericFormattedValue; 5 6 use std::ffi::CStr; 7 use std::fmt; 8 use std::mem; 9 use std::ops::{Deref, DerefMut}; 10 use std::ptr; 11 12 use glib::translate::*; 13 14 mini_object_wrapper!(Query, QueryRef, ffi::GstQuery, || { 15 ffi::gst_query_get_type() 16 }); 17 18 impl QueryRef { 19 #[doc(alias = "get_structure")] 20 #[doc(alias = "gst_query_get_structure")] structure(&self) -> Option<&StructureRef>21 pub fn structure(&self) -> Option<&StructureRef> { 22 unsafe { 23 let structure = ffi::gst_query_get_structure(self.as_mut_ptr()); 24 if structure.is_null() { 25 None 26 } else { 27 Some(StructureRef::from_glib_borrow(structure)) 28 } 29 } 30 } 31 32 #[doc(alias = "get_mut_structure")] structure_mut(&mut self) -> &mut StructureRef33 pub fn structure_mut(&mut self) -> &mut StructureRef { 34 unsafe { 35 let structure = ffi::gst_query_writable_structure(self.as_mut_ptr()); 36 StructureRef::from_glib_borrow_mut(structure) 37 } 38 } 39 is_downstream(&self) -> bool40 pub fn is_downstream(&self) -> bool { 41 unsafe { ((*self.as_ptr()).type_ as u32) & ffi::GST_QUERY_TYPE_DOWNSTREAM != 0 } 42 } 43 is_upstream(&self) -> bool44 pub fn is_upstream(&self) -> bool { 45 unsafe { ((*self.as_ptr()).type_ as u32) & ffi::GST_QUERY_TYPE_UPSTREAM != 0 } 46 } 47 is_serialized(&self) -> bool48 pub fn is_serialized(&self) -> bool { 49 unsafe { ((*self.as_ptr()).type_ as u32) & ffi::GST_QUERY_TYPE_SERIALIZED != 0 } 50 } 51 view(&self) -> QueryView<&Self>52 pub fn view(&self) -> QueryView<&Self> { 53 let type_ = unsafe { (*self.as_ptr()).type_ }; 54 55 match type_ { 56 ffi::GST_QUERY_POSITION => QueryView::Position(Position(self)), 57 ffi::GST_QUERY_DURATION => QueryView::Duration(Duration(self)), 58 ffi::GST_QUERY_LATENCY => QueryView::Latency(Latency(self)), 59 ffi::GST_QUERY_SEEKING => QueryView::Seeking(Seeking(self)), 60 ffi::GST_QUERY_SEGMENT => QueryView::Segment(Segment(self)), 61 ffi::GST_QUERY_CONVERT => QueryView::Convert(Convert(self)), 62 ffi::GST_QUERY_FORMATS => QueryView::Formats(Formats(self)), 63 ffi::GST_QUERY_BUFFERING => QueryView::Buffering(Buffering(self)), 64 ffi::GST_QUERY_CUSTOM => QueryView::Custom(Custom(self)), 65 ffi::GST_QUERY_URI => QueryView::Uri(Uri(self)), 66 ffi::GST_QUERY_ALLOCATION => QueryView::Allocation(Allocation(self)), 67 ffi::GST_QUERY_SCHEDULING => QueryView::Scheduling(Scheduling(self)), 68 ffi::GST_QUERY_ACCEPT_CAPS => QueryView::AcceptCaps(AcceptCaps(self)), 69 ffi::GST_QUERY_CAPS => QueryView::Caps(Caps(self)), 70 ffi::GST_QUERY_DRAIN => QueryView::Drain(Drain(self)), 71 ffi::GST_QUERY_CONTEXT => QueryView::Context(Context(self)), 72 ffi::GST_QUERY_BITRATE => QueryView::Bitrate(Bitrate(self)), 73 _ => QueryView::Other(Other(self)), 74 } 75 } 76 view_mut(&mut self) -> QueryView<&mut Self>77 pub fn view_mut(&mut self) -> QueryView<&mut Self> { 78 unsafe { mem::transmute(self.view()) } 79 } 80 } 81 82 impl fmt::Debug for Query { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result83 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 84 QueryRef::fmt(self, f) 85 } 86 } 87 88 impl fmt::Debug for QueryRef { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result89 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 90 f.debug_struct("Query") 91 .field("ptr", unsafe { &self.as_ptr() }) 92 .field("type", &unsafe { 93 let type_ = ffi::gst_query_type_get_name((*self.as_ptr()).type_); 94 CStr::from_ptr(type_).to_str().unwrap() 95 }) 96 .field("structure", &self.structure()) 97 .finish() 98 } 99 } 100 101 pub unsafe trait AsPtr { as_ptr(&self) -> *mut ffi::GstQuery102 unsafe fn as_ptr(&self) -> *mut ffi::GstQuery; 103 } 104 105 pub unsafe trait AsMutPtr: AsPtr { as_mut_ptr(&self) -> *mut ffi::GstQuery106 unsafe fn as_mut_ptr(&self) -> *mut ffi::GstQuery; 107 } 108 109 unsafe impl AsPtr for Query { as_ptr(&self) -> *mut ffi::GstQuery110 unsafe fn as_ptr(&self) -> *mut ffi::GstQuery { 111 QueryRef::as_ptr(self) as *mut ffi::GstQuery 112 } 113 } 114 115 unsafe impl AsMutPtr for Query { as_mut_ptr(&self) -> *mut ffi::GstQuery116 unsafe fn as_mut_ptr(&self) -> *mut ffi::GstQuery { 117 QueryRef::as_ptr(self) as *mut ffi::GstQuery 118 } 119 } 120 121 unsafe impl<'a> AsPtr for &'a QueryRef { as_ptr(&self) -> *mut ffi::GstQuery122 unsafe fn as_ptr(&self) -> *mut ffi::GstQuery { 123 QueryRef::as_ptr(self) as *mut ffi::GstQuery 124 } 125 } 126 127 unsafe impl<'a> AsPtr for &'a mut QueryRef { as_ptr(&self) -> *mut ffi::GstQuery128 unsafe fn as_ptr(&self) -> *mut ffi::GstQuery { 129 QueryRef::as_ptr(self) as *mut ffi::GstQuery 130 } 131 } 132 133 unsafe impl<'a> AsMutPtr for &'a mut QueryRef { as_mut_ptr(&self) -> *mut ffi::GstQuery134 unsafe fn as_mut_ptr(&self) -> *mut ffi::GstQuery { 135 QueryRef::as_ptr(self) as *mut ffi::GstQuery 136 } 137 } 138 139 #[derive(Debug)] 140 pub enum QueryView<T> { 141 Position(Position<T>), 142 Duration(Duration<T>), 143 Latency(Latency<T>), 144 Seeking(Seeking<T>), 145 Segment(Segment<T>), 146 Convert(Convert<T>), 147 Formats(Formats<T>), 148 Buffering(Buffering<T>), 149 Custom(Custom<T>), 150 Uri(Uri<T>), 151 Allocation(Allocation<T>), 152 Scheduling(Scheduling<T>), 153 AcceptCaps(AcceptCaps<T>), 154 Caps(Caps<T>), 155 Drain(Drain<T>), 156 Context(Context<T>), 157 Bitrate(Bitrate<T>), 158 Other(Other<T>), 159 __NonExhaustive, 160 } 161 162 macro_rules! declare_concrete_query( 163 ($name:ident, $param:ident) => { 164 #[derive(Debug)] 165 pub struct $name<$param>($param); 166 167 impl<'a> $name<&'a QueryRef> { 168 pub fn query(&self) -> &QueryRef { 169 self.0 170 } 171 } 172 173 impl<'a> Deref for $name<&'a QueryRef> { 174 type Target = QueryRef; 175 176 fn deref(&self) -> &Self::Target { 177 self.0 178 } 179 } 180 181 impl<'a> Deref for $name<&'a mut QueryRef> { 182 type Target = $name<&'a QueryRef>; 183 184 fn deref(&self) -> &Self::Target { 185 unsafe { 186 &*(self as *const $name<&'a mut QueryRef> as *const $name<&'a QueryRef>) 187 } 188 } 189 } 190 191 impl<'a> $name<&'a mut QueryRef> { 192 pub fn query_mut(&mut self) -> &mut QueryRef { 193 self.0 194 } 195 } 196 197 impl Deref for $name<Query> { 198 type Target = QueryRef; 199 200 fn deref(&self) -> &Self::Target { 201 &self.0 202 } 203 } 204 205 impl DerefMut for $name<Query> { 206 fn deref_mut(&mut self) -> &mut Self::Target { 207 self.0.get_mut().unwrap() 208 } 209 } 210 211 impl From<$name<Query>> for Query { 212 fn from(concrete: $name<Query>) -> Self { 213 skip_assert_initialized!(); 214 unsafe { from_glib_none(concrete.0.as_mut_ptr()) } 215 } 216 } 217 } 218 ); 219 220 declare_concrete_query!(Position, T); 221 impl Position<Query> { 222 #[doc(alias = "gst_query_new_position")] new(fmt: crate::Format) -> Self223 pub fn new(fmt: crate::Format) -> Self { 224 assert_initialized_main_thread!(); 225 unsafe { Self(from_glib_full(ffi::gst_query_new_position(fmt.into_glib()))) } 226 } 227 } 228 229 impl<T: AsPtr> Position<T> { 230 #[doc(alias = "get_result")] result(&self) -> GenericFormattedValue231 pub fn result(&self) -> GenericFormattedValue { 232 unsafe { 233 let mut fmt = mem::MaybeUninit::uninit(); 234 let mut pos = mem::MaybeUninit::uninit(); 235 236 ffi::gst_query_parse_position(self.0.as_ptr(), fmt.as_mut_ptr(), pos.as_mut_ptr()); 237 238 GenericFormattedValue::new(from_glib(fmt.assume_init()), pos.assume_init()) 239 } 240 } 241 242 #[doc(alias = "get_format")] format(&self) -> crate::Format243 pub fn format(&self) -> crate::Format { 244 unsafe { 245 let mut fmt = mem::MaybeUninit::uninit(); 246 247 ffi::gst_query_parse_position(self.0.as_ptr(), fmt.as_mut_ptr(), ptr::null_mut()); 248 249 from_glib(fmt.assume_init()) 250 } 251 } 252 } 253 254 impl<T: AsMutPtr> Position<T> { 255 #[doc(alias = "gst_query_set_position")] set<V: Into<GenericFormattedValue>>(&mut self, pos: V)256 pub fn set<V: Into<GenericFormattedValue>>(&mut self, pos: V) { 257 let pos = pos.into(); 258 assert_eq!(pos.format(), self.format()); 259 unsafe { 260 ffi::gst_query_set_position(self.0.as_mut_ptr(), pos.format().into_glib(), pos.value()); 261 } 262 } 263 } 264 265 declare_concrete_query!(Duration, T); 266 impl Duration<Query> { 267 #[doc(alias = "gst_query_new_duration")] new(fmt: crate::Format) -> Self268 pub fn new(fmt: crate::Format) -> Self { 269 assert_initialized_main_thread!(); 270 unsafe { Self(from_glib_full(ffi::gst_query_new_duration(fmt.into_glib()))) } 271 } 272 } 273 274 impl<T: AsPtr> Duration<T> { 275 #[doc(alias = "get_result")] result(&self) -> GenericFormattedValue276 pub fn result(&self) -> GenericFormattedValue { 277 unsafe { 278 let mut fmt = mem::MaybeUninit::uninit(); 279 let mut pos = mem::MaybeUninit::uninit(); 280 281 ffi::gst_query_parse_duration(self.0.as_ptr(), fmt.as_mut_ptr(), pos.as_mut_ptr()); 282 283 GenericFormattedValue::new(from_glib(fmt.assume_init()), pos.assume_init()) 284 } 285 } 286 287 #[doc(alias = "get_format")] format(&self) -> crate::Format288 pub fn format(&self) -> crate::Format { 289 unsafe { 290 let mut fmt = mem::MaybeUninit::uninit(); 291 292 ffi::gst_query_parse_duration(self.0.as_ptr(), fmt.as_mut_ptr(), ptr::null_mut()); 293 294 from_glib(fmt.assume_init()) 295 } 296 } 297 } 298 299 impl<T: AsMutPtr> Duration<T> { 300 #[doc(alias = "gst_query_set_duration")] set<V: Into<GenericFormattedValue>>(&mut self, dur: V)301 pub fn set<V: Into<GenericFormattedValue>>(&mut self, dur: V) { 302 let dur = dur.into(); 303 assert_eq!(dur.format(), self.format()); 304 unsafe { 305 ffi::gst_query_set_duration(self.0.as_mut_ptr(), dur.format().into_glib(), dur.value()); 306 } 307 } 308 } 309 310 declare_concrete_query!(Latency, T); 311 impl Latency<Query> { 312 #[doc(alias = "gst_query_new_latency")] new() -> Self313 pub fn new() -> Self { 314 assert_initialized_main_thread!(); 315 unsafe { Self(from_glib_full(ffi::gst_query_new_latency())) } 316 } 317 } 318 319 impl Default for Latency<Query> { default() -> Self320 fn default() -> Self { 321 Self::new() 322 } 323 } 324 325 impl<T: AsPtr> Latency<T> { 326 #[doc(alias = "get_result")] result(&self) -> (bool, crate::ClockTime, Option<crate::ClockTime>)327 pub fn result(&self) -> (bool, crate::ClockTime, Option<crate::ClockTime>) { 328 unsafe { 329 let mut live = mem::MaybeUninit::uninit(); 330 let mut min = mem::MaybeUninit::uninit(); 331 let mut max = mem::MaybeUninit::uninit(); 332 333 ffi::gst_query_parse_latency( 334 self.0.as_ptr(), 335 live.as_mut_ptr(), 336 min.as_mut_ptr(), 337 max.as_mut_ptr(), 338 ); 339 340 ( 341 from_glib(live.assume_init()), 342 try_from_glib(min.assume_init()).expect("undefined min latency"), 343 from_glib(max.assume_init()), 344 ) 345 } 346 } 347 } 348 349 impl<T: AsMutPtr> Latency<T> { 350 #[doc(alias = "gst_query_set_latency")] set( &mut self, live: bool, min: crate::ClockTime, max: impl Into<Option<crate::ClockTime>>, )351 pub fn set( 352 &mut self, 353 live: bool, 354 min: crate::ClockTime, 355 max: impl Into<Option<crate::ClockTime>>, 356 ) { 357 unsafe { 358 ffi::gst_query_set_latency( 359 self.0.as_mut_ptr(), 360 live.into_glib(), 361 min.into_glib(), 362 max.into().into_glib(), 363 ); 364 } 365 } 366 } 367 368 declare_concrete_query!(Seeking, T); 369 impl Seeking<Query> { 370 #[doc(alias = "gst_query_new_seeking")] new(fmt: crate::Format) -> Self371 pub fn new(fmt: crate::Format) -> Self { 372 assert_initialized_main_thread!(); 373 unsafe { Self(from_glib_full(ffi::gst_query_new_seeking(fmt.into_glib()))) } 374 } 375 } 376 377 impl<T: AsPtr> Seeking<T> { 378 #[doc(alias = "get_result")] result(&self) -> (bool, GenericFormattedValue, GenericFormattedValue)379 pub fn result(&self) -> (bool, GenericFormattedValue, GenericFormattedValue) { 380 unsafe { 381 let mut fmt = mem::MaybeUninit::uninit(); 382 let mut seekable = mem::MaybeUninit::uninit(); 383 let mut start = mem::MaybeUninit::uninit(); 384 let mut end = mem::MaybeUninit::uninit(); 385 ffi::gst_query_parse_seeking( 386 self.0.as_ptr(), 387 fmt.as_mut_ptr(), 388 seekable.as_mut_ptr(), 389 start.as_mut_ptr(), 390 end.as_mut_ptr(), 391 ); 392 393 ( 394 from_glib(seekable.assume_init()), 395 GenericFormattedValue::new(from_glib(fmt.assume_init()), start.assume_init()), 396 GenericFormattedValue::new(from_glib(fmt.assume_init()), end.assume_init()), 397 ) 398 } 399 } 400 401 #[doc(alias = "get_format")] format(&self) -> crate::Format402 pub fn format(&self) -> crate::Format { 403 unsafe { 404 let mut fmt = mem::MaybeUninit::uninit(); 405 ffi::gst_query_parse_seeking( 406 self.0.as_ptr(), 407 fmt.as_mut_ptr(), 408 ptr::null_mut(), 409 ptr::null_mut(), 410 ptr::null_mut(), 411 ); 412 413 from_glib(fmt.assume_init()) 414 } 415 } 416 } 417 418 impl<T: AsMutPtr> Seeking<T> { 419 #[doc(alias = "gst_query_set_seeking")] set<V: Into<GenericFormattedValue>>(&mut self, seekable: bool, start: V, end: V)420 pub fn set<V: Into<GenericFormattedValue>>(&mut self, seekable: bool, start: V, end: V) { 421 let start = start.into(); 422 let end = end.into(); 423 424 assert_eq!(self.format(), start.format()); 425 assert_eq!(start.format(), end.format()); 426 427 unsafe { 428 ffi::gst_query_set_seeking( 429 self.0.as_mut_ptr(), 430 start.format().into_glib(), 431 seekable.into_glib(), 432 start.value(), 433 end.value(), 434 ); 435 } 436 } 437 } 438 439 declare_concrete_query!(Segment, T); 440 impl Segment<Query> { 441 #[doc(alias = "gst_query_new_segment")] new(fmt: crate::Format) -> Self442 pub fn new(fmt: crate::Format) -> Self { 443 assert_initialized_main_thread!(); 444 unsafe { Self(from_glib_full(ffi::gst_query_new_segment(fmt.into_glib()))) } 445 } 446 } 447 448 impl<T: AsPtr> Segment<T> { 449 #[doc(alias = "get_result")] result(&self) -> (f64, GenericFormattedValue, GenericFormattedValue)450 pub fn result(&self) -> (f64, GenericFormattedValue, GenericFormattedValue) { 451 unsafe { 452 let mut rate = mem::MaybeUninit::uninit(); 453 let mut fmt = mem::MaybeUninit::uninit(); 454 let mut start = mem::MaybeUninit::uninit(); 455 let mut stop = mem::MaybeUninit::uninit(); 456 457 ffi::gst_query_parse_segment( 458 self.0.as_ptr(), 459 rate.as_mut_ptr(), 460 fmt.as_mut_ptr(), 461 start.as_mut_ptr(), 462 stop.as_mut_ptr(), 463 ); 464 ( 465 rate.assume_init(), 466 GenericFormattedValue::new(from_glib(fmt.assume_init()), start.assume_init()), 467 GenericFormattedValue::new(from_glib(fmt.assume_init()), stop.assume_init()), 468 ) 469 } 470 } 471 472 #[doc(alias = "get_format")] format(&self) -> crate::Format473 pub fn format(&self) -> crate::Format { 474 unsafe { 475 let mut fmt = mem::MaybeUninit::uninit(); 476 477 ffi::gst_query_parse_segment( 478 self.0.as_ptr(), 479 ptr::null_mut(), 480 fmt.as_mut_ptr(), 481 ptr::null_mut(), 482 ptr::null_mut(), 483 ); 484 from_glib(fmt.assume_init()) 485 } 486 } 487 } 488 489 impl<T: AsMutPtr> Segment<T> { 490 #[doc(alias = "gst_query_set_segment")] set<V: Into<GenericFormattedValue>>(&mut self, rate: f64, start: V, stop: V)491 pub fn set<V: Into<GenericFormattedValue>>(&mut self, rate: f64, start: V, stop: V) { 492 let start = start.into(); 493 let stop = stop.into(); 494 495 assert_eq!(start.format(), stop.format()); 496 497 unsafe { 498 ffi::gst_query_set_segment( 499 self.0.as_mut_ptr(), 500 rate, 501 start.format().into_glib(), 502 start.value(), 503 stop.value(), 504 ); 505 } 506 } 507 } 508 509 declare_concrete_query!(Convert, T); 510 impl Convert<Query> { 511 #[doc(alias = "gst_query_new_convert")] new<V: Into<GenericFormattedValue>>(value: V, dest_fmt: crate::Format) -> Self512 pub fn new<V: Into<GenericFormattedValue>>(value: V, dest_fmt: crate::Format) -> Self { 513 assert_initialized_main_thread!(); 514 let value = value.into(); 515 unsafe { 516 Self(from_glib_full(ffi::gst_query_new_convert( 517 value.format().into_glib(), 518 value.value(), 519 dest_fmt.into_glib(), 520 ))) 521 } 522 } 523 } 524 525 impl<T: AsPtr> Convert<T> { 526 #[doc(alias = "get_result")] result(&self) -> (GenericFormattedValue, GenericFormattedValue)527 pub fn result(&self) -> (GenericFormattedValue, GenericFormattedValue) { 528 unsafe { 529 let mut src_fmt = mem::MaybeUninit::uninit(); 530 let mut src = mem::MaybeUninit::uninit(); 531 let mut dest_fmt = mem::MaybeUninit::uninit(); 532 let mut dest = mem::MaybeUninit::uninit(); 533 534 ffi::gst_query_parse_convert( 535 self.0.as_ptr(), 536 src_fmt.as_mut_ptr(), 537 src.as_mut_ptr(), 538 dest_fmt.as_mut_ptr(), 539 dest.as_mut_ptr(), 540 ); 541 ( 542 GenericFormattedValue::new(from_glib(src_fmt.assume_init()), src.assume_init()), 543 GenericFormattedValue::new(from_glib(dest_fmt.assume_init()), dest.assume_init()), 544 ) 545 } 546 } 547 get(&self) -> (GenericFormattedValue, crate::Format)548 pub fn get(&self) -> (GenericFormattedValue, crate::Format) { 549 unsafe { 550 let mut src_fmt = mem::MaybeUninit::uninit(); 551 let mut src = mem::MaybeUninit::uninit(); 552 let mut dest_fmt = mem::MaybeUninit::uninit(); 553 554 ffi::gst_query_parse_convert( 555 self.0.as_ptr(), 556 src_fmt.as_mut_ptr(), 557 src.as_mut_ptr(), 558 dest_fmt.as_mut_ptr(), 559 ptr::null_mut(), 560 ); 561 ( 562 GenericFormattedValue::new(from_glib(src_fmt.assume_init()), src.assume_init()), 563 from_glib(dest_fmt.assume_init()), 564 ) 565 } 566 } 567 } 568 569 impl<T: AsMutPtr> Convert<T> { 570 #[doc(alias = "gst_query_set_convert")] set<V: Into<GenericFormattedValue>>(&mut self, src: V, dest: V)571 pub fn set<V: Into<GenericFormattedValue>>(&mut self, src: V, dest: V) { 572 let src = src.into(); 573 let dest = dest.into(); 574 575 unsafe { 576 ffi::gst_query_set_convert( 577 self.0.as_mut_ptr(), 578 src.format().into_glib(), 579 src.value(), 580 dest.format().into_glib(), 581 dest.value(), 582 ); 583 } 584 } 585 } 586 587 declare_concrete_query!(Formats, T); 588 impl Formats<Query> { 589 #[doc(alias = "gst_query_new_formats")] new() -> Self590 pub fn new() -> Self { 591 assert_initialized_main_thread!(); 592 unsafe { Self(from_glib_full(ffi::gst_query_new_formats())) } 593 } 594 } 595 596 impl Default for Formats<Query> { default() -> Self597 fn default() -> Self { 598 Self::new() 599 } 600 } 601 602 impl<T: AsPtr> Formats<T> { 603 #[doc(alias = "get_result")] result(&self) -> Vec<crate::Format>604 pub fn result(&self) -> Vec<crate::Format> { 605 unsafe { 606 let mut n = mem::MaybeUninit::uninit(); 607 ffi::gst_query_parse_n_formats(self.0.as_ptr(), n.as_mut_ptr()); 608 let n = n.assume_init(); 609 let mut res = Vec::with_capacity(n as usize); 610 611 for i in 0..n { 612 let mut fmt = mem::MaybeUninit::uninit(); 613 ffi::gst_query_parse_nth_format(self.0.as_ptr(), i, fmt.as_mut_ptr()); 614 res.push(from_glib(fmt.assume_init())); 615 } 616 617 res 618 } 619 } 620 } 621 622 impl<T: AsMutPtr> Formats<T> { 623 #[doc(alias = "gst_query_set_formatsv")] set(&mut self, formats: &[crate::Format])624 pub fn set(&mut self, formats: &[crate::Format]) { 625 unsafe { 626 let v: Vec<_> = formats.iter().map(|f| f.into_glib()).collect(); 627 ffi::gst_query_set_formatsv(self.0.as_mut_ptr(), v.len() as i32, v.as_ptr() as *mut _); 628 } 629 } 630 } 631 632 declare_concrete_query!(Buffering, T); 633 impl Buffering<Query> { 634 #[doc(alias = "gst_query_new_buffering")] new(fmt: crate::Format) -> Self635 pub fn new(fmt: crate::Format) -> Self { 636 assert_initialized_main_thread!(); 637 unsafe { 638 Self(from_glib_full(ffi::gst_query_new_buffering( 639 fmt.into_glib(), 640 ))) 641 } 642 } 643 } 644 645 impl<T: AsPtr> Buffering<T> { 646 #[doc(alias = "get_format")] format(&self) -> crate::Format647 pub fn format(&self) -> crate::Format { 648 unsafe { 649 let mut fmt = mem::MaybeUninit::uninit(); 650 651 ffi::gst_query_parse_buffering_range( 652 self.0.as_ptr(), 653 fmt.as_mut_ptr(), 654 ptr::null_mut(), 655 ptr::null_mut(), 656 ptr::null_mut(), 657 ); 658 659 from_glib(fmt.assume_init()) 660 } 661 } 662 663 #[doc(alias = "get_percent")] 664 #[doc(alias = "gst_query_parse_buffering_percent")] percent(&self) -> (bool, i32)665 pub fn percent(&self) -> (bool, i32) { 666 unsafe { 667 let mut busy = mem::MaybeUninit::uninit(); 668 let mut percent = mem::MaybeUninit::uninit(); 669 670 ffi::gst_query_parse_buffering_percent( 671 self.0.as_ptr(), 672 busy.as_mut_ptr(), 673 percent.as_mut_ptr(), 674 ); 675 676 (from_glib(busy.assume_init()), percent.assume_init()) 677 } 678 } 679 680 #[doc(alias = "get_range")] 681 #[doc(alias = "gst_query_parse_buffering_range")] range(&self) -> (GenericFormattedValue, GenericFormattedValue, i64)682 pub fn range(&self) -> (GenericFormattedValue, GenericFormattedValue, i64) { 683 unsafe { 684 let mut fmt = mem::MaybeUninit::uninit(); 685 let mut start = mem::MaybeUninit::uninit(); 686 let mut stop = mem::MaybeUninit::uninit(); 687 let mut estimated_total = mem::MaybeUninit::uninit(); 688 689 ffi::gst_query_parse_buffering_range( 690 self.0.as_ptr(), 691 fmt.as_mut_ptr(), 692 start.as_mut_ptr(), 693 stop.as_mut_ptr(), 694 estimated_total.as_mut_ptr(), 695 ); 696 ( 697 GenericFormattedValue::new(from_glib(fmt.assume_init()), start.assume_init()), 698 GenericFormattedValue::new(from_glib(fmt.assume_init()), stop.assume_init()), 699 estimated_total.assume_init(), 700 ) 701 } 702 } 703 704 #[doc(alias = "get_stats")] 705 #[doc(alias = "gst_query_parse_buffering_stats")] stats(&self) -> (crate::BufferingMode, i32, i32, i64)706 pub fn stats(&self) -> (crate::BufferingMode, i32, i32, i64) { 707 unsafe { 708 let mut mode = mem::MaybeUninit::uninit(); 709 let mut avg_in = mem::MaybeUninit::uninit(); 710 let mut avg_out = mem::MaybeUninit::uninit(); 711 let mut buffering_left = mem::MaybeUninit::uninit(); 712 713 ffi::gst_query_parse_buffering_stats( 714 self.0.as_ptr(), 715 mode.as_mut_ptr(), 716 avg_in.as_mut_ptr(), 717 avg_out.as_mut_ptr(), 718 buffering_left.as_mut_ptr(), 719 ); 720 721 ( 722 from_glib(mode.assume_init()), 723 avg_in.assume_init(), 724 avg_out.assume_init(), 725 buffering_left.assume_init(), 726 ) 727 } 728 } 729 730 #[doc(alias = "get_ranges")] 731 #[doc(alias = "gst_query_get_n_buffering_ranges")] ranges(&self) -> Vec<(GenericFormattedValue, GenericFormattedValue)>732 pub fn ranges(&self) -> Vec<(GenericFormattedValue, GenericFormattedValue)> { 733 unsafe { 734 let mut fmt = mem::MaybeUninit::uninit(); 735 ffi::gst_query_parse_buffering_range( 736 self.0.as_ptr(), 737 fmt.as_mut_ptr(), 738 ptr::null_mut(), 739 ptr::null_mut(), 740 ptr::null_mut(), 741 ); 742 let fmt = from_glib(fmt.assume_init()); 743 744 let n = ffi::gst_query_get_n_buffering_ranges(self.0.as_ptr()); 745 let mut res = Vec::with_capacity(n as usize); 746 for i in 0..n { 747 let mut start = mem::MaybeUninit::uninit(); 748 let mut stop = mem::MaybeUninit::uninit(); 749 let s: bool = from_glib(ffi::gst_query_parse_nth_buffering_range( 750 self.0.as_ptr(), 751 i, 752 start.as_mut_ptr(), 753 stop.as_mut_ptr(), 754 )); 755 if s { 756 res.push(( 757 GenericFormattedValue::new(fmt, start.assume_init()), 758 GenericFormattedValue::new(fmt, stop.assume_init()), 759 )); 760 } 761 } 762 763 res 764 } 765 } 766 } 767 768 impl<T: AsMutPtr> Buffering<T> { set_percent(&mut self, busy: bool, percent: i32)769 pub fn set_percent(&mut self, busy: bool, percent: i32) { 770 unsafe { 771 ffi::gst_query_set_buffering_percent(self.0.as_mut_ptr(), busy.into_glib(), percent); 772 } 773 } 774 set_range<V: Into<GenericFormattedValue>>( &mut self, start: V, stop: V, estimated_total: i64, )775 pub fn set_range<V: Into<GenericFormattedValue>>( 776 &mut self, 777 start: V, 778 stop: V, 779 estimated_total: i64, 780 ) { 781 let start = start.into(); 782 let stop = stop.into(); 783 784 assert_eq!(self.format(), start.format()); 785 assert_eq!(start.format(), stop.format()); 786 787 unsafe { 788 ffi::gst_query_set_buffering_range( 789 self.0.as_mut_ptr(), 790 start.format().into_glib(), 791 start.value(), 792 stop.value(), 793 estimated_total, 794 ); 795 } 796 } 797 set_stats( &mut self, mode: crate::BufferingMode, avg_in: i32, avg_out: i32, buffering_left: i64, )798 pub fn set_stats( 799 &mut self, 800 mode: crate::BufferingMode, 801 avg_in: i32, 802 avg_out: i32, 803 buffering_left: i64, 804 ) { 805 skip_assert_initialized!(); 806 unsafe { 807 ffi::gst_query_set_buffering_stats( 808 self.0.as_mut_ptr(), 809 mode.into_glib(), 810 avg_in, 811 avg_out, 812 buffering_left, 813 ); 814 } 815 } 816 add_buffering_ranges<V: Into<GenericFormattedValue> + Copy>( &mut self, ranges: &[(V, V)], )817 pub fn add_buffering_ranges<V: Into<GenericFormattedValue> + Copy>( 818 &mut self, 819 ranges: &[(V, V)], 820 ) { 821 unsafe { 822 let fmt = self.format(); 823 824 for &(start, stop) in ranges { 825 let start = start.into(); 826 let stop = stop.into(); 827 assert_eq!(start.format(), fmt); 828 assert_eq!(stop.format(), fmt); 829 ffi::gst_query_add_buffering_range( 830 self.0.as_mut_ptr(), 831 start.value(), 832 stop.value(), 833 ); 834 } 835 } 836 } 837 } 838 839 declare_concrete_query!(Custom, T); 840 impl Custom<Query> { 841 #[doc(alias = "gst_query_new_custom")] new(structure: crate::Structure) -> Self842 pub fn new(structure: crate::Structure) -> Self { 843 assert_initialized_main_thread!(); 844 unsafe { 845 Self(from_glib_full(ffi::gst_query_new_custom( 846 ffi::GST_QUERY_CUSTOM, 847 structure.into_ptr(), 848 ))) 849 } 850 } 851 } 852 853 declare_concrete_query!(Uri, T); 854 impl Uri<Query> { 855 #[doc(alias = "gst_query_new_uri")] new() -> Self856 pub fn new() -> Self { 857 assert_initialized_main_thread!(); 858 unsafe { Self(from_glib_full(ffi::gst_query_new_uri())) } 859 } 860 } 861 862 impl Default for Uri<Query> { default() -> Self863 fn default() -> Self { 864 Self::new() 865 } 866 } 867 868 impl<T: AsPtr> Uri<T> { 869 #[doc(alias = "get_uri")] 870 #[doc(alias = "gst_query_parse_uri")] uri(&self) -> Option<String>871 pub fn uri(&self) -> Option<String> { 872 unsafe { 873 let mut uri = ptr::null_mut(); 874 ffi::gst_query_parse_uri(self.0.as_ptr(), &mut uri); 875 from_glib_full(uri) 876 } 877 } 878 879 #[doc(alias = "get_redirection")] 880 #[doc(alias = "gst_query_parse_uri_redirection")] 881 #[doc(alias = "gst_query_parse_uri_redirection_permanent")] redirection(&self) -> (Option<String>, bool)882 pub fn redirection(&self) -> (Option<String>, bool) { 883 unsafe { 884 let mut uri = ptr::null_mut(); 885 ffi::gst_query_parse_uri_redirection(self.0.as_ptr(), &mut uri); 886 let mut permanent = mem::MaybeUninit::uninit(); 887 ffi::gst_query_parse_uri_redirection_permanent(self.0.as_ptr(), permanent.as_mut_ptr()); 888 889 (from_glib_full(uri), from_glib(permanent.assume_init())) 890 } 891 } 892 } 893 894 impl<T: AsMutPtr> Uri<T> { 895 #[doc(alias = "gst_query_set_uri")] set_uri<'b, U: Into<&'b str>>(&mut self, uri: U)896 pub fn set_uri<'b, U: Into<&'b str>>(&mut self, uri: U) { 897 let uri = uri.into(); 898 unsafe { 899 ffi::gst_query_set_uri(self.0.as_mut_ptr(), uri.to_glib_none().0); 900 } 901 } 902 set_redirection<'b, U: Into<&'b str>>(&mut self, uri: U, permanent: bool)903 pub fn set_redirection<'b, U: Into<&'b str>>(&mut self, uri: U, permanent: bool) { 904 let uri = uri.into(); 905 unsafe { 906 ffi::gst_query_set_uri_redirection(self.0.as_mut_ptr(), uri.to_glib_none().0); 907 ffi::gst_query_set_uri_redirection_permanent( 908 self.0.as_mut_ptr(), 909 permanent.into_glib(), 910 ); 911 } 912 } 913 } 914 915 declare_concrete_query!(Allocation, T); 916 impl Allocation<Query> { 917 #[doc(alias = "gst_query_new_allocation")] new(caps: &crate::Caps, need_pool: bool) -> Self918 pub fn new(caps: &crate::Caps, need_pool: bool) -> Self { 919 assert_initialized_main_thread!(); 920 unsafe { 921 Self(from_glib_full(ffi::gst_query_new_allocation( 922 caps.as_mut_ptr(), 923 need_pool.into_glib(), 924 ))) 925 } 926 } 927 } 928 929 impl<T: AsPtr> Allocation<T> { get(&self) -> (&crate::CapsRef, bool)930 pub fn get(&self) -> (&crate::CapsRef, bool) { 931 unsafe { 932 let mut caps = ptr::null_mut(); 933 let mut need_pool = mem::MaybeUninit::uninit(); 934 935 ffi::gst_query_parse_allocation(self.0.as_ptr(), &mut caps, need_pool.as_mut_ptr()); 936 ( 937 crate::CapsRef::from_ptr(caps), 938 from_glib(need_pool.assume_init()), 939 ) 940 } 941 } 942 get_owned(&self) -> (crate::Caps, bool)943 pub fn get_owned(&self) -> (crate::Caps, bool) { 944 unsafe { 945 let (caps, need_pool) = self.get(); 946 (from_glib_none(caps.as_ptr()), need_pool) 947 } 948 } 949 950 #[doc(alias = "get_allocation_pools")] 951 #[doc(alias = "gst_query_get_n_allocation_pools")] allocation_pools(&self) -> Vec<(Option<crate::BufferPool>, u32, u32, u32)>952 pub fn allocation_pools(&self) -> Vec<(Option<crate::BufferPool>, u32, u32, u32)> { 953 unsafe { 954 let n = ffi::gst_query_get_n_allocation_pools(self.0.as_ptr()); 955 let mut pools = Vec::with_capacity(n as usize); 956 for i in 0..n { 957 let mut pool = ptr::null_mut(); 958 let mut size = mem::MaybeUninit::uninit(); 959 let mut min_buffers = mem::MaybeUninit::uninit(); 960 let mut max_buffers = mem::MaybeUninit::uninit(); 961 962 ffi::gst_query_parse_nth_allocation_pool( 963 self.0.as_ptr(), 964 i, 965 &mut pool, 966 size.as_mut_ptr(), 967 min_buffers.as_mut_ptr(), 968 max_buffers.as_mut_ptr(), 969 ); 970 pools.push(( 971 from_glib_full(pool), 972 size.assume_init(), 973 min_buffers.assume_init(), 974 max_buffers.assume_init(), 975 )); 976 } 977 978 pools 979 } 980 } 981 982 #[doc(alias = "get_allocation_metas")] 983 #[doc(alias = "gst_query_get_n_allocation_metas")] allocation_metas(&self) -> Vec<(glib::Type, Option<&crate::StructureRef>)>984 pub fn allocation_metas(&self) -> Vec<(glib::Type, Option<&crate::StructureRef>)> { 985 unsafe { 986 let n = ffi::gst_query_get_n_allocation_metas(self.0.as_ptr()); 987 let mut metas = Vec::with_capacity(n as usize); 988 for i in 0..n { 989 let mut structure = ptr::null(); 990 991 let api = 992 ffi::gst_query_parse_nth_allocation_meta(self.0.as_ptr(), i, &mut structure); 993 metas.push(( 994 from_glib(api), 995 if structure.is_null() { 996 None 997 } else { 998 Some(crate::StructureRef::from_glib_borrow(structure)) 999 }, 1000 )); 1001 } 1002 1003 metas 1004 } 1005 } 1006 1007 #[doc(alias = "gst_query_find_allocation_meta")] find_allocation_meta<U: crate::MetaAPI>(&self) -> Option<u32>1008 pub fn find_allocation_meta<U: crate::MetaAPI>(&self) -> Option<u32> { 1009 unsafe { 1010 let mut idx = mem::MaybeUninit::uninit(); 1011 if ffi::gst_query_find_allocation_meta( 1012 self.0.as_ptr(), 1013 U::meta_api().into_glib(), 1014 idx.as_mut_ptr(), 1015 ) != glib::ffi::GFALSE 1016 { 1017 Some(idx.assume_init()) 1018 } else { 1019 None 1020 } 1021 } 1022 } 1023 } 1024 1025 impl<T: AsMutPtr> Allocation<T> { 1026 #[doc(alias = "gst_query_add_allocation_pool")] add_allocation_pool( &mut self, pool: Option<&crate::BufferPool>, size: u32, min_buffers: u32, max_buffers: u32, )1027 pub fn add_allocation_pool( 1028 &mut self, 1029 pool: Option<&crate::BufferPool>, 1030 size: u32, 1031 min_buffers: u32, 1032 max_buffers: u32, 1033 ) { 1034 unsafe { 1035 ffi::gst_query_add_allocation_pool( 1036 self.0.as_mut_ptr(), 1037 pool.to_glib_none().0, 1038 size, 1039 min_buffers, 1040 max_buffers, 1041 ); 1042 } 1043 } 1044 1045 #[doc(alias = "gst_query_set_nth_allocation_pool")] set_nth_allocation_pool( &mut self, idx: u32, pool: Option<&crate::BufferPool>, size: u32, min_buffers: u32, max_buffers: u32, )1046 pub fn set_nth_allocation_pool( 1047 &mut self, 1048 idx: u32, 1049 pool: Option<&crate::BufferPool>, 1050 size: u32, 1051 min_buffers: u32, 1052 max_buffers: u32, 1053 ) { 1054 unsafe { 1055 ffi::gst_query_set_nth_allocation_pool( 1056 self.0.as_mut_ptr(), 1057 idx, 1058 pool.to_glib_none().0, 1059 size, 1060 min_buffers, 1061 max_buffers, 1062 ); 1063 } 1064 } 1065 1066 #[doc(alias = "gst_query_remove_nth_allocation_pool")] remove_nth_allocation_pool(&mut self, idx: u32)1067 pub fn remove_nth_allocation_pool(&mut self, idx: u32) { 1068 unsafe { 1069 ffi::gst_query_remove_nth_allocation_pool(self.0.as_mut_ptr(), idx); 1070 } 1071 } 1072 1073 #[doc(alias = "gst_query_add_allocation_meta")] add_allocation_meta<U: crate::MetaAPI>( &mut self, structure: Option<&crate::StructureRef>, )1074 pub fn add_allocation_meta<U: crate::MetaAPI>( 1075 &mut self, 1076 structure: Option<&crate::StructureRef>, 1077 ) { 1078 unsafe { 1079 ffi::gst_query_add_allocation_meta( 1080 self.0.as_mut_ptr(), 1081 U::meta_api().into_glib(), 1082 if let Some(structure) = structure { 1083 structure.as_ptr() 1084 } else { 1085 ptr::null() 1086 }, 1087 ); 1088 } 1089 } 1090 1091 #[doc(alias = "gst_query_remove_nth_allocation_meta")] remove_nth_allocation_meta(&mut self, idx: u32)1092 pub fn remove_nth_allocation_meta(&mut self, idx: u32) { 1093 unsafe { 1094 ffi::gst_query_remove_nth_allocation_meta(self.0.as_mut_ptr(), idx); 1095 } 1096 } 1097 } 1098 1099 declare_concrete_query!(Scheduling, T); 1100 impl Scheduling<Query> { 1101 #[doc(alias = "gst_query_new_scheduling")] new() -> Self1102 pub fn new() -> Self { 1103 assert_initialized_main_thread!(); 1104 unsafe { Self(from_glib_full(ffi::gst_query_new_scheduling())) } 1105 } 1106 } 1107 1108 impl Default for Scheduling<Query> { default() -> Self1109 fn default() -> Self { 1110 Self::new() 1111 } 1112 } 1113 1114 impl<T: AsPtr> Scheduling<T> { 1115 #[doc(alias = "gst_query_has_scheduling_mode")] has_scheduling_mode(&self, mode: crate::PadMode) -> bool1116 pub fn has_scheduling_mode(&self, mode: crate::PadMode) -> bool { 1117 unsafe { 1118 from_glib(ffi::gst_query_has_scheduling_mode( 1119 self.0.as_ptr(), 1120 mode.into_glib(), 1121 )) 1122 } 1123 } 1124 1125 #[doc(alias = "gst_query_has_scheduling_mode_with_flags")] has_scheduling_mode_with_flags( &self, mode: crate::PadMode, flags: crate::SchedulingFlags, ) -> bool1126 pub fn has_scheduling_mode_with_flags( 1127 &self, 1128 mode: crate::PadMode, 1129 flags: crate::SchedulingFlags, 1130 ) -> bool { 1131 skip_assert_initialized!(); 1132 unsafe { 1133 from_glib(ffi::gst_query_has_scheduling_mode_with_flags( 1134 self.0.as_ptr(), 1135 mode.into_glib(), 1136 flags.into_glib(), 1137 )) 1138 } 1139 } 1140 1141 #[doc(alias = "get_scheduling_modes")] 1142 #[doc(alias = "gst_query_get_n_scheduling_modes")] scheduling_modes(&self) -> Vec<crate::PadMode>1143 pub fn scheduling_modes(&self) -> Vec<crate::PadMode> { 1144 unsafe { 1145 let n = ffi::gst_query_get_n_scheduling_modes(self.0.as_ptr()); 1146 let mut res = Vec::with_capacity(n as usize); 1147 for i in 0..n { 1148 res.push(from_glib(ffi::gst_query_parse_nth_scheduling_mode( 1149 self.0.as_ptr(), 1150 i, 1151 ))); 1152 } 1153 1154 res 1155 } 1156 } 1157 1158 #[doc(alias = "get_result")] result(&self) -> (crate::SchedulingFlags, i32, i32, i32)1159 pub fn result(&self) -> (crate::SchedulingFlags, i32, i32, i32) { 1160 unsafe { 1161 let mut flags = mem::MaybeUninit::uninit(); 1162 let mut minsize = mem::MaybeUninit::uninit(); 1163 let mut maxsize = mem::MaybeUninit::uninit(); 1164 let mut align = mem::MaybeUninit::uninit(); 1165 1166 ffi::gst_query_parse_scheduling( 1167 self.0.as_ptr(), 1168 flags.as_mut_ptr(), 1169 minsize.as_mut_ptr(), 1170 maxsize.as_mut_ptr(), 1171 align.as_mut_ptr(), 1172 ); 1173 1174 ( 1175 from_glib(flags.assume_init()), 1176 minsize.assume_init(), 1177 maxsize.assume_init(), 1178 align.assume_init(), 1179 ) 1180 } 1181 } 1182 } 1183 1184 impl<T: AsMutPtr> Scheduling<T> { add_scheduling_modes(&mut self, modes: &[crate::PadMode])1185 pub fn add_scheduling_modes(&mut self, modes: &[crate::PadMode]) { 1186 unsafe { 1187 for mode in modes { 1188 ffi::gst_query_add_scheduling_mode(self.0.as_mut_ptr(), mode.into_glib()); 1189 } 1190 } 1191 } 1192 1193 #[doc(alias = "gst_query_set_scheduling")] set(&mut self, flags: crate::SchedulingFlags, minsize: i32, maxsize: i32, align: i32)1194 pub fn set(&mut self, flags: crate::SchedulingFlags, minsize: i32, maxsize: i32, align: i32) { 1195 unsafe { 1196 ffi::gst_query_set_scheduling( 1197 self.0.as_mut_ptr(), 1198 flags.into_glib(), 1199 minsize, 1200 maxsize, 1201 align, 1202 ); 1203 } 1204 } 1205 } 1206 1207 declare_concrete_query!(AcceptCaps, T); 1208 impl AcceptCaps<Query> { 1209 #[doc(alias = "gst_query_new_accept_caps")] new(caps: &crate::Caps) -> Self1210 pub fn new(caps: &crate::Caps) -> Self { 1211 assert_initialized_main_thread!(); 1212 unsafe { 1213 Self(from_glib_full(ffi::gst_query_new_accept_caps( 1214 caps.as_mut_ptr(), 1215 ))) 1216 } 1217 } 1218 } 1219 1220 impl<T: AsPtr> AcceptCaps<T> { 1221 #[doc(alias = "get_caps")] 1222 #[doc(alias = "gst_query_parse_accept_caps")] caps(&self) -> &crate::CapsRef1223 pub fn caps(&self) -> &crate::CapsRef { 1224 unsafe { 1225 let mut caps = ptr::null_mut(); 1226 ffi::gst_query_parse_accept_caps(self.0.as_ptr(), &mut caps); 1227 crate::CapsRef::from_ptr(caps) 1228 } 1229 } 1230 1231 #[doc(alias = "get_caps_owned")] caps_owned(&self) -> crate::Caps1232 pub fn caps_owned(&self) -> crate::Caps { 1233 unsafe { from_glib_none(self.caps().as_ptr()) } 1234 } 1235 1236 #[doc(alias = "get_result")] 1237 #[doc(alias = "gst_query_parse_accept_caps_result")] result(&self) -> bool1238 pub fn result(&self) -> bool { 1239 unsafe { 1240 let mut accepted = mem::MaybeUninit::uninit(); 1241 ffi::gst_query_parse_accept_caps_result(self.0.as_ptr(), accepted.as_mut_ptr()); 1242 from_glib(accepted.assume_init()) 1243 } 1244 } 1245 } 1246 1247 impl<T: AsMutPtr> AcceptCaps<T> { set_result(&mut self, accepted: bool)1248 pub fn set_result(&mut self, accepted: bool) { 1249 unsafe { 1250 ffi::gst_query_set_accept_caps_result(self.0.as_mut_ptr(), accepted.into_glib()); 1251 } 1252 } 1253 } 1254 1255 declare_concrete_query!(Caps, T); 1256 impl Caps<Query> { 1257 #[doc(alias = "gst_query_new_caps")] new(filter: Option<&crate::Caps>) -> Self1258 pub fn new(filter: Option<&crate::Caps>) -> Self { 1259 assert_initialized_main_thread!(); 1260 unsafe { 1261 Self(from_glib_full(ffi::gst_query_new_caps( 1262 filter.to_glib_none().0, 1263 ))) 1264 } 1265 } 1266 } 1267 1268 impl<T: AsPtr> Caps<T> { 1269 #[doc(alias = "get_filter")] filter(&self) -> Option<&crate::CapsRef>1270 pub fn filter(&self) -> Option<&crate::CapsRef> { 1271 unsafe { 1272 let mut caps = ptr::null_mut(); 1273 ffi::gst_query_parse_caps(self.0.as_ptr(), &mut caps); 1274 if caps.is_null() { 1275 None 1276 } else { 1277 Some(crate::CapsRef::from_ptr(caps)) 1278 } 1279 } 1280 } 1281 1282 #[doc(alias = "get_filter_owned")] filter_owned(&self) -> Option<crate::Caps>1283 pub fn filter_owned(&self) -> Option<crate::Caps> { 1284 unsafe { self.filter().map(|caps| from_glib_none(caps.as_ptr())) } 1285 } 1286 1287 #[doc(alias = "get_result")] 1288 #[doc(alias = "gst_query_parse_caps_result")] result(&self) -> Option<&crate::CapsRef>1289 pub fn result(&self) -> Option<&crate::CapsRef> { 1290 unsafe { 1291 let mut caps = ptr::null_mut(); 1292 ffi::gst_query_parse_caps_result(self.0.as_ptr(), &mut caps); 1293 if caps.is_null() { 1294 None 1295 } else { 1296 Some(crate::CapsRef::from_ptr(caps)) 1297 } 1298 } 1299 } 1300 1301 #[doc(alias = "get_result_owned")] result_owned(&self) -> Option<crate::Caps>1302 pub fn result_owned(&self) -> Option<crate::Caps> { 1303 unsafe { self.result().map(|caps| from_glib_none(caps.as_ptr())) } 1304 } 1305 } 1306 1307 impl<T: AsMutPtr> Caps<T> { set_result(&mut self, caps: &crate::Caps)1308 pub fn set_result(&mut self, caps: &crate::Caps) { 1309 unsafe { 1310 ffi::gst_query_set_caps_result(self.0.as_mut_ptr(), caps.as_mut_ptr()); 1311 } 1312 } 1313 } 1314 1315 declare_concrete_query!(Drain, T); 1316 impl Drain<Query> { 1317 #[doc(alias = "gst_query_new_drain")] new() -> Self1318 pub fn new() -> Self { 1319 assert_initialized_main_thread!(); 1320 unsafe { Self(from_glib_full(ffi::gst_query_new_drain())) } 1321 } 1322 } 1323 1324 impl Default for Drain<Query> { default() -> Self1325 fn default() -> Self { 1326 Self::new() 1327 } 1328 } 1329 1330 declare_concrete_query!(Context, T); 1331 impl Context<Query> { 1332 #[doc(alias = "gst_query_new_context")] new(context_type: &str) -> Self1333 pub fn new(context_type: &str) -> Self { 1334 assert_initialized_main_thread!(); 1335 unsafe { 1336 Self(from_glib_full(ffi::gst_query_new_context( 1337 context_type.to_glib_none().0, 1338 ))) 1339 } 1340 } 1341 } 1342 1343 impl<T: AsPtr> Context<T> { 1344 #[doc(alias = "get_context")] 1345 #[doc(alias = "gst_query_parse_context")] context(&self) -> Option<&crate::ContextRef>1346 pub fn context(&self) -> Option<&crate::ContextRef> { 1347 unsafe { 1348 let mut context = ptr::null_mut(); 1349 ffi::gst_query_parse_context(self.0.as_ptr(), &mut context); 1350 if context.is_null() { 1351 None 1352 } else { 1353 Some(crate::ContextRef::from_ptr(context)) 1354 } 1355 } 1356 } 1357 1358 #[doc(alias = "get_context_owned")] context_owned(&self) -> Option<crate::Context>1359 pub fn context_owned(&self) -> Option<crate::Context> { 1360 unsafe { 1361 self.context() 1362 .map(|context| from_glib_none(context.as_ptr())) 1363 } 1364 } 1365 1366 #[doc(alias = "get_context_type")] 1367 #[doc(alias = "gst_query_parse_context_type")] context_type(&self) -> &str1368 pub fn context_type(&self) -> &str { 1369 unsafe { 1370 let mut context_type = ptr::null(); 1371 ffi::gst_query_parse_context_type(self.0.as_ptr(), &mut context_type); 1372 CStr::from_ptr(context_type).to_str().unwrap() 1373 } 1374 } 1375 } 1376 1377 impl<T: AsMutPtr> Context<T> { 1378 #[doc(alias = "gst_query_set_context")] set_context(&mut self, context: &crate::Context)1379 pub fn set_context(&mut self, context: &crate::Context) { 1380 unsafe { 1381 ffi::gst_query_set_context(self.0.as_mut_ptr(), context.as_mut_ptr()); 1382 } 1383 } 1384 } 1385 1386 declare_concrete_query!(Bitrate, T); 1387 1388 #[cfg(any(feature = "v1_16", feature = "dox"))] 1389 #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))] 1390 impl Bitrate<Query> { 1391 #[doc(alias = "gst_query_new_bitrate")] new() -> Self1392 pub fn new() -> Self { 1393 assert_initialized_main_thread!(); 1394 unsafe { Self(from_glib_full(ffi::gst_query_new_bitrate())) } 1395 } 1396 } 1397 1398 #[cfg(any(feature = "v1_16", feature = "dox"))] 1399 #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))] 1400 impl Default for Bitrate<Query> { default() -> Self1401 fn default() -> Self { 1402 Self::new() 1403 } 1404 } 1405 1406 impl<T: AsPtr> Bitrate<T> { 1407 #[cfg(any(feature = "v1_16", feature = "dox"))] 1408 #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))] 1409 #[doc(alias = "get_bitrate")] 1410 #[doc(alias = "gst_query_parse_bitrate")] bitrate(&self) -> u321411 pub fn bitrate(&self) -> u32 { 1412 unsafe { 1413 let mut bitrate = mem::MaybeUninit::uninit(); 1414 ffi::gst_query_parse_bitrate(self.0.as_ptr(), bitrate.as_mut_ptr()); 1415 bitrate.assume_init() 1416 } 1417 } 1418 } 1419 1420 impl<T: AsMutPtr> Bitrate<T> { 1421 #[cfg(any(feature = "v1_16", feature = "dox"))] 1422 #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))] 1423 #[doc(alias = "gst_query_set_bitrate")] set_bitrate(&mut self, bitrate: u32)1424 pub fn set_bitrate(&mut self, bitrate: u32) { 1425 unsafe { 1426 ffi::gst_query_set_bitrate(self.0.as_mut_ptr(), bitrate); 1427 } 1428 } 1429 } 1430 1431 declare_concrete_query!(Other, T); 1432 1433 #[cfg(test)] 1434 mod tests { 1435 use super::*; 1436 use crate::ClockTime; 1437 use std::convert::TryInto; 1438 1439 #[test] test_writability()1440 fn test_writability() { 1441 crate::init().unwrap(); 1442 1443 fn check_mut(query: &mut QueryRef) { 1444 skip_assert_initialized!(); 1445 match query.view_mut() { 1446 QueryView::Position(ref mut p) => { 1447 let pos = p.result(); 1448 assert_eq!(pos.try_into(), Ok(ClockTime::NONE)); 1449 p.set(Some(3 * ClockTime::SECOND)); 1450 let pos = p.result(); 1451 assert_eq!(pos.try_into(), Ok(Some(3 * ClockTime::SECOND))); 1452 } 1453 _ => panic!("Wrong concrete Query in Query"), 1454 } 1455 } 1456 1457 fn check_ref(query: &QueryRef) { 1458 skip_assert_initialized!(); 1459 match query.view() { 1460 QueryView::Position(ref p) => { 1461 let pos = p.result(); 1462 assert_eq!(pos.try_into(), Ok(Some(3 * ClockTime::SECOND))); 1463 unsafe { 1464 assert!(!p.as_mut_ptr().is_null()); 1465 } 1466 } 1467 _ => panic!("Wrong concrete Query in Query"), 1468 } 1469 } 1470 1471 let mut p = Position::new(crate::Format::Time); 1472 let pos = p.result(); 1473 assert_eq!(pos.try_into(), Ok(ClockTime::NONE)); 1474 1475 p.structure_mut().set("check_mut", &true); 1476 1477 // deref 1478 assert!(!p.is_serialized()); 1479 1480 { 1481 check_mut(&mut p); 1482 1483 let structure = p.structure(); 1484 structure.unwrap().has_field("check_mut"); 1485 1486 // Expected: cannot borrow `p` as mutable because it is also borrowed as immutable 1487 //check_mut(&mut p); 1488 } 1489 1490 check_ref(&p); 1491 } 1492 1493 #[test] test_into_query()1494 fn test_into_query() { 1495 crate::init().unwrap(); 1496 let d = Duration::new(crate::Format::Time); 1497 1498 let mut query: Query = d.into(); 1499 assert!(query.is_writable()); 1500 1501 let query = query.make_mut(); 1502 if let QueryView::Duration(d) = &mut query.view_mut() { 1503 d.set(Some(2 * ClockTime::SECOND)); 1504 } 1505 1506 if let QueryView::Duration(d) = &query.view() { 1507 let duration = d.result(); 1508 assert_eq!(duration.try_into(), Ok(Some(2 * ClockTime::SECOND))); 1509 } 1510 } 1511 1512 #[test] test_concrete_to_sys()1513 fn test_concrete_to_sys() { 1514 crate::init().unwrap(); 1515 1516 let p = Position::new(crate::Format::Time); 1517 unsafe { 1518 assert!(!p.as_mut_ptr().is_null()); 1519 } 1520 } 1521 } 1522