From 7342a0fb9895a06ed89dfc586f17d88248e083cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 29 Nov 2017 18:55:56 +0200 Subject: [PATCH] Implement some functions on EventType and implement PartialOrd for serialized events --- gstreamer/src/event.rs | 71 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/gstreamer/src/event.rs b/gstreamer/src/event.rs index 4a1623bda..36aa8b658 100644 --- a/gstreamer/src/event.rs +++ b/gstreamer/src/event.rs @@ -12,6 +12,7 @@ use structure::*; use std::ptr; use std::mem; +use std::cmp; use std::ffi::CStr; use glib; @@ -21,6 +22,8 @@ use glib::translate::{from_glib, from_glib_full, from_glib_none, ToGlib, ToGlibP #[cfg(any(feature = "v1_10", feature = "dox"))] use glib::translate::FromGlibPtrContainer; +use EventType; + #[repr(C)] pub struct EventRef(ffi::GstEvent); @@ -33,6 +36,60 @@ unsafe impl MiniObject for EventRef { type GstType = ffi::GstEvent; } +impl EventType { + pub fn is_upstream(&self) -> bool { + (self.to_glib() as u32) & (ffi::GST_EVENT_TYPE_UPSTREAM.bits()) != 0 + } + + pub fn is_downstream(&self) -> bool { + (self.to_glib() as u32) & (ffi::GST_EVENT_TYPE_DOWNSTREAM.bits()) != 0 + } + + pub fn is_serialized(&self) -> bool { + (self.to_glib() as u32) & (ffi::GST_EVENT_TYPE_SERIALIZED.bits()) != 0 + } + + pub fn is_sticky(&self) -> bool { + (self.to_glib() as u32) & (ffi::GST_EVENT_TYPE_STICKY.bits()) != 0 + } + + pub fn is_sticky_multi(&self) -> bool { + (self.to_glib() as u32) & (ffi::GST_EVENT_TYPE_STICKY_MULTI.bits()) != 0 + } +} + +impl PartialOrd for EventType { + fn partial_cmp(&self, other: &Self) -> Option { + if !self.is_serialized() || !other.is_serialized() { + return None; + } + + let v1 = self.to_glib() as u32; + let v2 = other.to_glib() as u32; + + let stream_start = ffi::GST_EVENT_STREAM_START as u32; + let segment = ffi::GST_EVENT_SEGMENT as u32; + let eos = ffi::GST_EVENT_EOS as u32; + + // Strictly ordered range between stream_start and segment, + // and EOS is bigger than everything else + if v1 >= stream_start && v1 <= segment || v2 >= stream_start && v2 <= segment { + Some(v1.cmp(&v2)) + // If one is EOS, the other is definitely less or equal + } else if v1 == eos || v2 == eos { + if v1 == v2 { + Some(cmp::Ordering::Equal) + } else if v1 == eos { + Some(cmp::Ordering::Greater) + } else { + Some(cmp::Ordering::Less) + } + } else { + None + } + } +} + impl EventRef { pub fn get_seqnum(&self) -> u32 { unsafe { ffi::gst_event_get_seqnum(self.as_mut_ptr()) } @@ -51,23 +108,27 @@ impl EventRef { } pub fn is_upstream(&self) -> bool { - unsafe { ((*self.as_ptr()).type_ as u32) & (ffi::GST_EVENT_TYPE_UPSTREAM.bits()) != 0 } + self.get_type().is_upstream() } pub fn is_downstream(&self) -> bool { - unsafe { ((*self.as_ptr()).type_ as u32) & (ffi::GST_EVENT_TYPE_DOWNSTREAM.bits()) != 0 } + self.get_type().is_downstream() } pub fn is_serialized(&self) -> bool { - unsafe { ((*self.as_ptr()).type_ as u32) & (ffi::GST_EVENT_TYPE_SERIALIZED.bits()) != 0 } + self.get_type().is_serialized() } pub fn is_sticky(&self) -> bool { - unsafe { ((*self.as_ptr()).type_ as u32) & (ffi::GST_EVENT_TYPE_STICKY.bits()) != 0 } + self.get_type().is_sticky() } pub fn is_sticky_multi(&self) -> bool { - unsafe { ((*self.as_ptr()).type_ as u32) & (ffi::GST_EVENT_TYPE_STICKY_MULTI.bits()) != 0 } + self.get_type().is_sticky_multi() + } + + pub fn get_type(&self) -> EventType { + unsafe { from_glib((*self.as_ptr()).type_) } } pub fn view(&self) -> EventView {