From 37fd226836802f83872892b3840acd2c2f00fed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 27 Feb 2019 18:10:31 +0200 Subject: [PATCH] sdp: Add media iterators for bandwidths, formats, connections and attributes --- gstreamer-sdp/src/lib.rs | 2 +- gstreamer-sdp/src/sdp_media.rs | 103 +++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/gstreamer-sdp/src/lib.rs b/gstreamer-sdp/src/lib.rs index 4d1367d17..995186a10 100644 --- a/gstreamer-sdp/src/lib.rs +++ b/gstreamer-sdp/src/lib.rs @@ -57,7 +57,7 @@ pub use sdp_connection::*; mod sdp_key; pub use sdp_key::*; mod sdp_media; -pub use sdp_media::*; +pub use sdp_media::{SDPMedia, SDPMediaRef}; mod sdp_message; pub use sdp_message::*; mod sdp_origin; diff --git a/gstreamer-sdp/src/sdp_media.rs b/gstreamer-sdp/src/sdp_media.rs index adf6731f5..3d3f0bc17 100644 --- a/gstreamer-sdp/src/sdp_media.rs +++ b/gstreamer-sdp/src/sdp_media.rs @@ -169,6 +169,22 @@ impl SDPMediaRef { unsafe { from_glib_full(ffi::gst_sdp_media_as_text(&self.0)) } } + pub fn attributes(&self) -> AttributesIter { + AttributesIter::new(self) + } + + pub fn formats(&self) -> FormatsIter { + FormatsIter::new(self) + } + + pub fn bandwidths(&self) -> BandwidthsIter { + BandwidthsIter::new(self) + } + + pub fn connections(&self) -> ConnectionsIter { + ConnectionsIter::new(self) + } + pub fn attributes_len(&self) -> u32 { unsafe { ffi::gst_sdp_media_attributes_len(&self.0) } } @@ -514,3 +530,90 @@ impl SDPMediaRef { } } } + +macro_rules! define_iter( + ($name:ident, $typ:ty, $get_item:expr, $get_len:expr) => { + #[derive(Debug)] + pub struct $name<'a> { + media: &'a SDPMediaRef, + idx: u32, + len: u32, + } + + impl<'a> $name<'a> { + fn new(media: &'a SDPMediaRef) -> $name<'a> { + skip_assert_initialized!(); + let len = $get_len(media); + + $name { + media, + idx: 0, + len, + } + } + } + + impl<'a> Iterator for $name<'a> { + type Item = $typ; + + fn next(&mut self) -> Option { + if self.idx >= self.len { + return None; + } + + let item = $get_item(self.media, self.idx)?; + self.idx += 1; + Some(item) + } + + fn size_hint(&self) -> (usize, Option) { + if self.idx == self.len { + return (0, Some(0)) + } + + let remaining = (self.len - self.idx) as usize; + + (remaining, Some(remaining)) + } + } + + impl<'a> DoubleEndedIterator for $name<'a> { + fn next_back(&mut self) -> Option { + if self.idx == self.len { + return None; + } + + self.len -= 1; + + $get_item(self.media, self.len) + } + } + + impl<'a> ExactSizeIterator for $name<'a> {} + } +); + +define_iter!( + BandwidthsIter, + &'a SDPBandwidth, + |media: &'a SDPMediaRef, idx| media.get_bandwidth(idx), + |media: &SDPMediaRef| media.bandwidths_len() +); +define_iter!( + FormatsIter, + &'a str, + |media: &'a SDPMediaRef, idx| media.get_format(idx), + |media: &SDPMediaRef| media.formats_len() +); +define_iter!( + ConnectionsIter, + &'a SDPConnection, + |media: &'a SDPMediaRef, idx| media.get_connection(idx), + |media: &SDPMediaRef| media.connections_len() +); +define_iter!( + AttributesIter, + &'a SDPAttribute, + |media: &'a SDPMediaRef, idx| media.get_attribute(idx), + |media: &SDPMediaRef| media.attributes_len() +);