sdp: Add a SDPMediaRef that allows borrowing of a media

Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/issues/189
This commit is contained in:
Sebastian Dröge 2019-02-27 18:04:12 +02:00
parent 066b22efc5
commit 20e10698ac
2 changed files with 127 additions and 103 deletions

View file

@ -7,12 +7,15 @@
// // except according to those terms.
use std::ffi::CStr;
use std::fmt;
use std::mem;
use std::ops;
use std::ptr;
use ffi;
use glib::translate::*;
use gst;
use gst::prelude::*;
use sdp_attribute::SDPAttribute;
use sdp_bandwidth::SDPBandwidth;
@ -23,7 +26,7 @@ use sdp_key::SDPKey;
use MIKEYMessage;
glib_wrapper! {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDPMedia(Boxed<ffi::GstSDPMedia>);
match fn {
@ -50,7 +53,61 @@ impl SDPMedia {
}
}
}
}
unsafe impl Send for SDPMedia {}
unsafe impl Sync for SDPMedia {}
impl ops::Deref for SDPMedia {
type Target = SDPMediaRef;
fn deref(&self) -> &SDPMediaRef {
unsafe { &*(self.to_glib_none().0 as *const SDPMediaRef) }
}
}
impl ops::DerefMut for SDPMedia {
fn deref_mut(&mut self) -> &mut SDPMediaRef {
unsafe { &mut *(self.to_glib_none_mut().0 as *mut SDPMediaRef) }
}
}
impl fmt::Debug for SDPMedia {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<SDPMediaRef as fmt::Debug>::fmt(&*self, f)
}
}
impl fmt::Display for SDPMedia {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<SDPMediaRef as fmt::Display>::fmt(&*self, f)
}
}
#[repr(C)]
pub struct SDPMediaRef(ffi::GstSDPMedia);
impl fmt::Debug for SDPMediaRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("SDPMedia")
// TODO
.finish()
}
}
impl fmt::Display for SDPMediaRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.as_text() {
Some(text) => f.write_str(text.as_str()),
None => Err(fmt::Error),
}
}
}
unsafe impl Send for SDPMediaRef {}
unsafe impl Sync for SDPMediaRef {}
impl SDPMediaRef {
pub fn add_attribute<'a, P: Into<Option<&'a str>>>(
&mut self,
key: &str,
@ -58,13 +115,8 @@ impl SDPMedia {
) -> Result<(), ()> {
let value = value.into();
let value = value.to_glib_none();
let result = unsafe {
ffi::gst_sdp_media_add_attribute(
self.to_glib_none_mut().0,
key.to_glib_none().0,
value.0,
)
};
let result =
unsafe { ffi::gst_sdp_media_add_attribute(&mut self.0, key.to_glib_none().0, value.0) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -73,11 +125,7 @@ impl SDPMedia {
pub fn add_bandwidth(&mut self, bwtype: &str, bandwidth: u32) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_add_bandwidth(
self.to_glib_none_mut().0,
bwtype.to_glib_none().0,
bandwidth,
)
ffi::gst_sdp_media_add_bandwidth(&mut self.0, bwtype.to_glib_none().0, bandwidth)
};
match result {
ffi::GST_SDP_OK => Ok(()),
@ -95,7 +143,7 @@ impl SDPMedia {
) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_add_connection(
self.to_glib_none_mut().0,
&mut self.0,
nettype.to_glib_none().0,
addrtype.to_glib_none().0,
address.to_glib_none().0,
@ -110,9 +158,7 @@ impl SDPMedia {
}
pub fn add_format(&mut self, format: &str) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_add_format(self.to_glib_none_mut().0, format.to_glib_none().0)
};
let result = unsafe { ffi::gst_sdp_media_add_format(&mut self.0, format.to_glib_none().0) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -120,16 +166,16 @@ impl SDPMedia {
}
pub fn as_text(&self) -> Option<String> {
unsafe { from_glib_full(ffi::gst_sdp_media_as_text(self.to_glib_none().0)) }
unsafe { from_glib_full(ffi::gst_sdp_media_as_text(&self.0)) }
}
pub fn attributes_len(&self) -> u32 {
unsafe { ffi::gst_sdp_media_attributes_len(self.to_glib_none().0) }
unsafe { ffi::gst_sdp_media_attributes_len(&self.0) }
}
pub fn attributes_to_caps(&self, caps: &gst::Caps) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_attributes_to_caps(self.to_glib_none().0, caps.to_glib_none().0)
ffi::gst_sdp_media_attributes_to_caps(&self.0, caps.to_glib_none().0)
};
match result {
ffi::GST_SDP_OK => Ok(()),
@ -138,20 +184,20 @@ impl SDPMedia {
}
pub fn bandwidths_len(&self) -> u32 {
unsafe { ffi::gst_sdp_media_bandwidths_len(self.to_glib_none().0) }
unsafe { ffi::gst_sdp_media_bandwidths_len(&self.0) }
}
pub fn connections_len(&self) -> u32 {
unsafe { ffi::gst_sdp_media_connections_len(self.to_glib_none().0) }
unsafe { ffi::gst_sdp_media_connections_len(&self.0) }
}
pub fn formats_len(&self) -> u32 {
unsafe { ffi::gst_sdp_media_formats_len(self.to_glib_none().0) }
unsafe { ffi::gst_sdp_media_formats_len(&self.0) }
}
pub fn get_attribute(&self, idx: u32) -> Option<&SDPAttribute> {
unsafe {
let ptr = ffi::gst_sdp_media_get_attribute(self.to_glib_none().0, idx);
let ptr = ffi::gst_sdp_media_get_attribute(&self.0, idx);
if ptr.is_null() {
None
} else {
@ -162,8 +208,7 @@ impl SDPMedia {
pub fn get_attribute_val(&self, key: &str) -> Option<&str> {
unsafe {
let ptr =
ffi::gst_sdp_media_get_attribute_val(self.to_glib_none().0, key.to_glib_none().0);
let ptr = ffi::gst_sdp_media_get_attribute_val(&self.0, key.to_glib_none().0);
if ptr.is_null() {
None
} else {
@ -178,11 +223,7 @@ impl SDPMedia {
pub fn get_attribute_val_n(&self, key: &str, nth: u32) -> Option<&str> {
unsafe {
let ptr = ffi::gst_sdp_media_get_attribute_val_n(
self.to_glib_none().0,
key.to_glib_none().0,
nth,
);
let ptr = ffi::gst_sdp_media_get_attribute_val_n(&self.0, key.to_glib_none().0, nth);
if ptr.is_null() {
None
} else {
@ -197,7 +238,7 @@ impl SDPMedia {
pub fn get_bandwidth(&self, idx: u32) -> Option<&SDPBandwidth> {
unsafe {
let ptr = ffi::gst_sdp_media_get_bandwidth(self.to_glib_none().0, idx);
let ptr = ffi::gst_sdp_media_get_bandwidth(&self.0, idx);
if ptr.is_null() {
None
} else {
@ -207,17 +248,12 @@ impl SDPMedia {
}
pub fn get_caps_from_media(&self, pt: i32) -> Option<gst::Caps> {
unsafe {
from_glib_full(ffi::gst_sdp_media_get_caps_from_media(
self.to_glib_none().0,
pt,
))
}
unsafe { from_glib_full(ffi::gst_sdp_media_get_caps_from_media(&self.0, pt)) }
}
pub fn get_connection(&self, idx: u32) -> Option<&SDPConnection> {
unsafe {
let ptr = ffi::gst_sdp_media_get_connection(self.to_glib_none().0, idx);
let ptr = ffi::gst_sdp_media_get_connection(&self.0, idx);
if ptr.is_null() {
None
} else {
@ -228,7 +264,7 @@ impl SDPMedia {
pub fn get_format(&self, idx: u32) -> Option<&str> {
unsafe {
let ptr = ffi::gst_sdp_media_get_format(self.to_glib_none().0, idx);
let ptr = ffi::gst_sdp_media_get_format(&self.0, idx);
if ptr.is_null() {
None
} else {
@ -243,7 +279,7 @@ impl SDPMedia {
pub fn get_information(&self) -> Option<&str> {
unsafe {
let ptr = ffi::gst_sdp_media_get_information(self.to_glib_none().0);
let ptr = ffi::gst_sdp_media_get_information(&self.0);
if ptr.is_null() {
None
} else {
@ -258,7 +294,7 @@ impl SDPMedia {
pub fn get_key(&self) -> Option<&SDPKey> {
unsafe {
let ptr = ffi::gst_sdp_media_get_key(self.to_glib_none().0);
let ptr = ffi::gst_sdp_media_get_key(&self.0);
if ptr.is_null() {
None
} else {
@ -269,7 +305,7 @@ impl SDPMedia {
pub fn get_media(&self) -> Option<&str> {
unsafe {
let ptr = ffi::gst_sdp_media_get_media(self.to_glib_none().0);
let ptr = ffi::gst_sdp_media_get_media(&self.0);
if ptr.is_null() {
None
} else {
@ -283,16 +319,16 @@ impl SDPMedia {
}
pub fn get_num_ports(&self) -> u32 {
unsafe { ffi::gst_sdp_media_get_num_ports(self.to_glib_none().0) }
unsafe { ffi::gst_sdp_media_get_num_ports(&self.0) }
}
pub fn get_port(&self) -> u32 {
unsafe { ffi::gst_sdp_media_get_port(self.to_glib_none().0) }
unsafe { ffi::gst_sdp_media_get_port(&self.0) }
}
pub fn get_proto(&self) -> Option<&str> {
unsafe {
let ptr = ffi::gst_sdp_media_get_proto(self.to_glib_none().0);
let ptr = ffi::gst_sdp_media_get_proto(&self.0);
if ptr.is_null() {
None
} else {
@ -306,9 +342,7 @@ impl SDPMedia {
}
pub fn insert_attribute(&mut self, idx: i32, mut attr: SDPAttribute) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_insert_attribute(self.to_glib_none_mut().0, idx, &mut attr.0)
};
let result = unsafe { ffi::gst_sdp_media_insert_attribute(&mut self.0, idx, &mut attr.0) };
mem::forget(attr);
match result {
ffi::GST_SDP_OK => Ok(()),
@ -317,9 +351,7 @@ impl SDPMedia {
}
pub fn insert_bandwidth(&mut self, idx: i32, mut bw: SDPBandwidth) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_insert_bandwidth(self.to_glib_none_mut().0, idx, &mut bw.0)
};
let result = unsafe { ffi::gst_sdp_media_insert_bandwidth(&mut self.0, idx, &mut bw.0) };
mem::forget(bw);
match result {
ffi::GST_SDP_OK => Ok(()),
@ -328,9 +360,7 @@ impl SDPMedia {
}
pub fn insert_connection(&mut self, idx: i32, mut conn: SDPConnection) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_insert_connection(self.to_glib_none_mut().0, idx, &mut conn.0)
};
let result = unsafe { ffi::gst_sdp_media_insert_connection(&mut self.0, idx, &mut conn.0) };
mem::forget(conn);
match result {
ffi::GST_SDP_OK => Ok(()),
@ -339,13 +369,8 @@ impl SDPMedia {
}
pub fn insert_format(&mut self, idx: i32, format: &str) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_insert_format(
self.to_glib_none_mut().0,
idx,
format.to_glib_none().0,
)
};
let result =
unsafe { ffi::gst_sdp_media_insert_format(&mut self.0, idx, format.to_glib_none().0) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -356,7 +381,7 @@ impl SDPMedia {
pub fn parse_keymgmt(&self) -> Result<MIKEYMessage, ()> {
unsafe {
let mut mikey = ptr::null_mut();
let result = ffi::gst_sdp_media_parse_keymgmt(self.to_glib_none().0, &mut mikey);
let result = ffi::gst_sdp_media_parse_keymgmt(&self.0, &mut mikey);
match result {
ffi::GST_SDP_OK => Ok(from_glib_full(mikey)),
_ => Err(()),
@ -365,7 +390,7 @@ impl SDPMedia {
}
pub fn remove_attribute(&mut self, idx: u32) -> Result<(), ()> {
let result = unsafe { ffi::gst_sdp_media_remove_attribute(self.to_glib_none_mut().0, idx) };
let result = unsafe { ffi::gst_sdp_media_remove_attribute(&mut self.0, idx) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -373,7 +398,7 @@ impl SDPMedia {
}
pub fn remove_bandwidth(&mut self, idx: u32) -> Result<(), ()> {
let result = unsafe { ffi::gst_sdp_media_remove_bandwidth(self.to_glib_none_mut().0, idx) };
let result = unsafe { ffi::gst_sdp_media_remove_bandwidth(&mut self.0, idx) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -381,8 +406,7 @@ impl SDPMedia {
}
pub fn remove_connection(&mut self, idx: u32) -> Result<(), ()> {
let result =
unsafe { ffi::gst_sdp_media_remove_connection(self.to_glib_none_mut().0, idx) };
let result = unsafe { ffi::gst_sdp_media_remove_connection(&mut self.0, idx) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -390,7 +414,7 @@ impl SDPMedia {
}
pub fn remove_format(&mut self, idx: u32) -> Result<(), ()> {
let result = unsafe { ffi::gst_sdp_media_remove_format(self.to_glib_none_mut().0, idx) };
let result = unsafe { ffi::gst_sdp_media_remove_format(&mut self.0, idx) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -398,9 +422,7 @@ impl SDPMedia {
}
pub fn replace_attribute(&mut self, idx: u32, mut attr: SDPAttribute) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_replace_attribute(self.to_glib_none_mut().0, idx, &mut attr.0)
};
let result = unsafe { ffi::gst_sdp_media_replace_attribute(&mut self.0, idx, &mut attr.0) };
mem::forget(attr);
match result {
ffi::GST_SDP_OK => Ok(()),
@ -409,9 +431,7 @@ impl SDPMedia {
}
pub fn replace_bandwidth(&mut self, idx: u32, mut bw: SDPBandwidth) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_replace_bandwidth(self.to_glib_none_mut().0, idx, &mut bw.0)
};
let result = unsafe { ffi::gst_sdp_media_replace_bandwidth(&mut self.0, idx, &mut bw.0) };
mem::forget(bw);
match result {
ffi::GST_SDP_OK => Ok(()),
@ -420,9 +440,8 @@ impl SDPMedia {
}
pub fn replace_connection(&mut self, idx: u32, mut conn: SDPConnection) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_replace_connection(self.to_glib_none_mut().0, idx, &mut conn.0)
};
let result =
unsafe { ffi::gst_sdp_media_replace_connection(&mut self.0, idx, &mut conn.0) };
mem::forget(conn);
match result {
ffi::GST_SDP_OK => Ok(()),
@ -431,13 +450,8 @@ impl SDPMedia {
}
pub fn replace_format(&mut self, idx: u32, format: &str) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_replace_format(
self.to_glib_none_mut().0,
idx,
format.to_glib_none().0,
)
};
let result =
unsafe { ffi::gst_sdp_media_replace_format(&mut self.0, idx, format.to_glib_none().0) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -446,10 +460,7 @@ impl SDPMedia {
pub fn set_information(&mut self, information: &str) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_set_information(
self.to_glib_none_mut().0,
information.to_glib_none().0,
)
ffi::gst_sdp_media_set_information(&mut self.0, information.to_glib_none().0)
};
match result {
ffi::GST_SDP_OK => Ok(()),
@ -459,11 +470,7 @@ impl SDPMedia {
pub fn set_key(&mut self, type_: &str, data: &str) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_set_key(
self.to_glib_none_mut().0,
type_.to_glib_none().0,
data.to_glib_none().0,
)
ffi::gst_sdp_media_set_key(&mut self.0, type_.to_glib_none().0, data.to_glib_none().0)
};
match result {
ffi::GST_SDP_OK => Ok(()),
@ -472,9 +479,7 @@ impl SDPMedia {
}
pub fn set_media(&mut self, med: &str) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_set_media(self.to_glib_none_mut().0, med.to_glib_none().0)
};
let result = unsafe { ffi::gst_sdp_media_set_media(&mut self.0, med.to_glib_none().0) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -482,8 +487,7 @@ impl SDPMedia {
}
pub fn set_port_info(&mut self, port: u32, num_ports: u32) -> Result<(), ()> {
let result =
unsafe { ffi::gst_sdp_media_set_port_info(self.to_glib_none_mut().0, port, num_ports) };
let result = unsafe { ffi::gst_sdp_media_set_port_info(&mut self.0, port, num_ports) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),
@ -491,9 +495,7 @@ impl SDPMedia {
}
pub fn set_proto(&mut self, proto: &str) -> Result<(), ()> {
let result = unsafe {
ffi::gst_sdp_media_set_proto(self.to_glib_none_mut().0, proto.to_glib_none().0)
};
let result = unsafe { ffi::gst_sdp_media_set_proto(&mut self.0, proto.to_glib_none().0) };
match result {
ffi::GST_SDP_OK => Ok(()),
_ => Err(()),

View file

@ -23,6 +23,7 @@ use sdp_bandwidth::SDPBandwidth;
use sdp_connection::SDPConnection;
use sdp_key::SDPKey;
use sdp_media::SDPMedia;
use sdp_media::SDPMediaRef;
use sdp_origin::SDPOrigin;
use sdp_time::SDPTime;
use sdp_zone::SDPZone;
@ -275,13 +276,13 @@ impl SDPMessage {
}
}
pub fn get_media(&self, idx: u32) -> Option<&SDPMedia> {
pub fn get_media(&self, idx: u32) -> Option<&SDPMediaRef> {
unsafe {
let ptr = ffi::gst_sdp_message_get_media(self.to_glib_none().0, idx);
if ptr.is_null() {
None
} else {
Some(&*(ptr as *mut SDPMedia))
Some(&*(ptr as *const SDPMediaRef))
}
}
}
@ -762,3 +763,24 @@ impl Default for SDPMessage {
}
unsafe impl Send for SDPMessage {}
unsafe impl Sync for SDPMessage {}
#[cfg(test)]
mod tests {
use super::*;
use SDPMessage;
fn init() {
gst::init().unwrap();
}
#[test]
fn media_from_message() {
init();
let sdp = "v=0\r\no=- 1938737043334325940 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=ice-options:trickle\r\nm=video 9 UDP/TLS/RTP/SAVPF 96\r\nc=IN IP4 0.0.0.0\r\na=setup:actpass\r\na=ice-ufrag:YZxU9JlWHzHcF6O2U09/q3PvBhbTPdZW\r\na=ice-pwd:fyrt730GWo5mFGc9m2z/vbUu3z1lewla\r\na=sendrecv\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:96 VP8/90000\r\na=rtcp-fb:96 nack\r\na=rtcp-fb:96 nack pli\r\na=framerate:30\r\na=mid:video0\r\na=fingerprint:sha-256 DB:48:8F:18:13:F3:AA:13:31:B3:75:3D:1A:D3:BA:88:4A:ED:1B:56:14:C3:09:CD:BC:4D:18:42:B9:6A:5F:98\r\nm=audio 9 UDP/TLS/RTP/SAVPF 97\r\nc=IN IP4 0.0.0.0\r\na=setup:actpass\r\na=ice-ufrag:04KZM9qE2S4r06AN6A9CeXOM6mzO0LZY\r\na=ice-pwd:cJTSfHF6hHDAcsTJXZVJeuYCC6rKqBvW\r\na=sendrecv\r\na=rtcp-mux\r\na=rtcp-rsize\r\na=rtpmap:97 OPUS/48000/2\r\na=rtcp-fb:97 nack\r\na=rtcp-fb:97 nack pli\r\na=mid:audio1\r\na=fingerprint:sha-256 DB:48:8F:18:13:F3:AA:13:31:B3:75:3D:1A:D3:BA:88:4A:ED:1B:56:14:C3:09:CD:BC:4D:18:42:B9:6A:5F:98\r\n";
let sdp = SDPMessage::parse_buffer(sdp.as_bytes()).unwrap();
let media = sdp.get_media(0).unwrap();
assert_eq!(media.formats_len(), 1);
}
}