diff --git a/Gir_GstSdp.toml b/Gir_GstSdp.toml index 3d8f00a72..d7de35e3a 100644 --- a/Gir_GstSdp.toml +++ b/Gir_GstSdp.toml @@ -55,9 +55,25 @@ status = "generate" name = "add_payload" ignore = true + [[object.function]] + name = "get_payload" + ignore = true + + [[object.function]] + name = "base64_encode" + ignore = true + [[object]] name = "GstSdp.MIKEYPayload" status = "generate" [[object.function]] name = "kemac_add_sub" ignore = true + + [[object.function]] + name = "kemac_get_sub" + ignore = true + + [[object.function]] + name = "key_data_set_interval" + ignore = true diff --git a/gstreamer-sdp/src/auto/m_i_k_e_y_message.rs b/gstreamer-sdp/src/auto/m_i_k_e_y_message.rs index 96e85ae16..ba4e0e3d7 100644 --- a/gstreamer-sdp/src/auto/m_i_k_e_y_message.rs +++ b/gstreamer-sdp/src/auto/m_i_k_e_y_message.rs @@ -85,12 +85,6 @@ impl MIKEYMessage { } } - pub fn base64_encode(&mut self) -> Option { - unsafe { - from_glib_full(ffi::gst_mikey_message_base64_encode(self.to_glib_none_mut().0)) - } - } - pub fn find_payload(&self, type_: MIKEYPayloadType, nth: u32) -> Option { unsafe { from_glib_none(ffi::gst_mikey_message_find_payload(self.to_glib_none().0, type_.to_glib(), nth)) @@ -113,12 +107,6 @@ impl MIKEYMessage { } } - pub fn get_payload(&self, idx: u32) -> Option { - unsafe { - from_glib_none(ffi::gst_mikey_message_get_payload(self.to_glib_none().0, idx)) - } - } - //pub fn insert_cs_srtp(&mut self, idx: i32, map: /*Ignored*/&MIKEYMapSRTP) -> bool { // unsafe { TODO: call ffi::gst_mikey_message_insert_cs_srtp() } //} diff --git a/gstreamer-sdp/src/auto/m_i_k_e_y_payload.rs b/gstreamer-sdp/src/auto/m_i_k_e_y_payload.rs index cdf8d5f5a..88eea58f4 100644 --- a/gstreamer-sdp/src/auto/m_i_k_e_y_payload.rs +++ b/gstreamer-sdp/src/auto/m_i_k_e_y_payload.rs @@ -39,12 +39,6 @@ impl MIKEYPayload { } } - pub fn kemac_get_sub(&self, idx: u32) -> Option { - unsafe { - from_glib_none(ffi::gst_mikey_payload_kemac_get_sub(self.to_glib_none().0, idx)) - } - } - pub fn kemac_remove_sub(&mut self, idx: u32) -> bool { unsafe { from_glib(ffi::gst_mikey_payload_kemac_remove_sub(self.to_glib_none_mut().0, idx)) @@ -57,14 +51,6 @@ impl MIKEYPayload { } } - pub fn key_data_set_interval(&mut self, vf_len: u8, vt_data: &[u8]) -> bool { - let vf_data = vf_data.len() as Vec; - let vt_len = vt_data.len() as u8; - unsafe { - from_glib(ffi::gst_mikey_payload_key_data_set_interval(self.to_glib_none_mut().0, vf_len, vf_data.to_glib_none().0, vt_len, vt_data.to_glib_none().0)) - } - } - pub fn key_data_set_key(&mut self, key_type: MIKEYKeyDataType, key_data: &[u8]) -> bool { let key_len = key_data.len() as u16; unsafe { diff --git a/gstreamer-sdp/src/lib.rs b/gstreamer-sdp/src/lib.rs index 59825ba2b..8ffe1a938 100644 --- a/gstreamer-sdp/src/lib.rs +++ b/gstreamer-sdp/src/lib.rs @@ -9,6 +9,7 @@ extern crate bitflags; extern crate libc; +#[macro_use] extern crate glib; extern crate glib_sys as glib_ffi; extern crate gobject_sys as gobject_ffi; @@ -46,14 +47,11 @@ mod s_d_p_key; mod s_d_p_origin; mod s_d_p_time; mod s_d_p_zone; -mod m_i_k_e_y_message; -mod m_i_k_e_y_payload; +mod m_i_k_e_y_payload_s_p_param; mod m_i_k_e_y_encrypt_info; mod m_i_k_e_y_decrypt_info; mod m_i_k_e_y_map_s_r_t_p; -pub use s_d_p_message::SDPMessage; - // Re-export all the traits in a prelude module, so that applications // can always "use gst::prelude::*" without getting conflicts pub mod prelude { diff --git a/gstreamer-sdp/src/m_i_k_e_y_decrypt_info.rs b/gstreamer-sdp/src/m_i_k_e_y_decrypt_info.rs index 5c9f77a18..c97077351 100644 --- a/gstreamer-sdp/src/m_i_k_e_y_decrypt_info.rs +++ b/gstreamer-sdp/src/m_i_k_e_y_decrypt_info.rs @@ -6,18 +6,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::mem; -use std::ptr; - use ffi; -use glib::translate::*; -use glib_ffi; -use libc::c_void; pub struct MIKEYDecryptInfo(ffi::GstMIKEYDecryptInfo); - -impl MIKEYDecryptInfo { - pub fn new() -> MIKEYDecryptInfo { - MIKEYDecryptInfo(ffi::GstMIKEYDecryptInfo(ptr::null_mut() as *mut c_void)) - } -} diff --git a/gstreamer-sdp/src/m_i_k_e_y_encrypt_info.rs b/gstreamer-sdp/src/m_i_k_e_y_encrypt_info.rs index 395a78cb9..cac799083 100644 --- a/gstreamer-sdp/src/m_i_k_e_y_encrypt_info.rs +++ b/gstreamer-sdp/src/m_i_k_e_y_encrypt_info.rs @@ -6,18 +6,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::mem; -use std::ptr; - use ffi; -use glib::translate::*; -use glib_ffi; -use libc::c_void; pub struct MIKEYEncryptInfo(ffi::GstMIKEYEncryptInfo); - -impl MIKEYEncryptInfo { - pub fn new() -> MIKEYEncryptInfo { - MIKEYEncryptInfo(ffi::GstMIKEYEncryptInfo(ptr::null_mut() as *mut c_void)) - } -} diff --git a/gstreamer-sdp/src/m_i_k_e_y_map_s_r_t_p.rs b/gstreamer-sdp/src/m_i_k_e_y_map_s_r_t_p.rs index 3cf92f21a..3b158e646 100644 --- a/gstreamer-sdp/src/m_i_k_e_y_map_s_r_t_p.rs +++ b/gstreamer-sdp/src/m_i_k_e_y_map_s_r_t_p.rs @@ -6,13 +6,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::mem; -use std::ptr; - use ffi; -use glib::translate::*; -use glib_ffi; +#[repr(C)] pub struct MIKEYMapSRTP(ffi::GstMIKEYMapSRTP); impl MIKEYMapSRTP { diff --git a/gstreamer-sdp/src/m_i_k_e_y_message.rs b/gstreamer-sdp/src/m_i_k_e_y_message.rs index ba3a9c183..8f6e2cc9e 100644 --- a/gstreamer-sdp/src/m_i_k_e_y_message.rs +++ b/gstreamer-sdp/src/m_i_k_e_y_message.rs @@ -25,22 +25,22 @@ use m_i_k_e_y_encrypt_info::MIKEYEncryptInfo; use m_i_k_e_y_map_s_r_t_p::MIKEYMapSRTP; impl MIKEYMessage { - pub fn new_from_bytes(bytes: glib::Bytes, info: MIKEYDecryptInfo) -> Result { + pub fn new_from_bytes<'a, P: Into>>(bytes: &glib::Bytes, info: P) -> Result { unsafe { let mut error = ptr::null_mut(); - let ret = ffi::gst_mikey_message_new_from_bytes(bytes.to_glib_full(), info.to_glib_full(), &mut error); + let ret = ffi::gst_mikey_message_new_from_bytes(bytes.to_glib_none(), info.to_glib_full(), &mut error); mem::forget(bytes); mem::forget(info); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error) } + if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } } } - pub fn new_from_data(data: &[u8], info: MIKEYDecryptInfo) -> Result { + pub fn new_from_data<'a, P: Into>>(data: &[u8], info: P) -> Result { unsafe { let mut error = ptr::null_mut(); let ret = ffi::gst_mikey_message_new_from_data(data.to_glib_none().0, data.len(), info.to_glib_full(), &mut err); mem::forget(info); - if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error) } + if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) } } } @@ -50,9 +50,9 @@ impl MIKEYMessage { } } - pub fn get_cs_srtp(&self, idx: u32) -> Option { + pub fn get_cs_srtp(&self, idx: u32) -> Option<&MIKEYMapSRTP> { unsafe { - from_glib_none(ffi::gst_mikey_message_get_cs_srtp(self.to_glib_none().0, idx)) + &*(from_glib_none(ffi::gst_mikey_message_get_cs_srtp(self.to_glib_none().0, idx)) as *mut MIKEYMapSRTP) } } @@ -64,9 +64,15 @@ impl MIKEYMessage { } } + pub fn get_payload(&self, idx: u32) -> Option<&MIKEYPayload> { + unsafe { + &*(from_glib_none(ffi::gst_mikey_message_get_payload(self.to_glib_none().0, idx)) as *mut MIKEYPayload) + } + } + pub fn add_payload(&mut self, payload: MIKEYPayload) -> bool { unsafe { - let ret = from_glib(ffi::gst_mikey_message_add_payload(self.to_glib_none_mut().0, payload.to_glib_full())) + let ret = from_glib(ffi::gst_mikey_message_add_payload(self.to_glib_none_mut().0, payload.to_glib_full())); mem::forget(payload); ret } @@ -95,4 +101,10 @@ impl MIKEYMessage { ret } } + + pub fn base64_encode(&self) -> Option { + unsafe { + from_glib_full(ffi::gst_mikey_message_base64_encode(self.to_glib_none_mut().0)) + } + } } diff --git a/gstreamer-sdp/src/m_i_k_e_y_payload.rs b/gstreamer-sdp/src/m_i_k_e_y_payload.rs index 7a7aef8a1..01020797f 100644 --- a/gstreamer-sdp/src/m_i_k_e_y_payload.rs +++ b/gstreamer-sdp/src/m_i_k_e_y_payload.rs @@ -15,6 +15,7 @@ use glib_ffi; use gobject_ffi; use auto::MIKEYPayload; +use m_i_k_e_y_map_s_r_t_p::MIKEYPayloadSPParam; impl MIKEYPayload { @@ -25,11 +26,30 @@ impl MIKEYPayload { mem::forget(newpay); } - //pub fn sp_get_param(&self, idx: u32) -> /*Ignored*/Option { - // unsafe { TODO: call ffi::gst_mikey_payload_sp_get_param() } - //} + pub fn kemac_get_sub(&self, idx: u32) -> Option { + unsafe { + &*(from_glib_none(ffi::gst_mikey_payload_kemac_get_sub(self.to_glib_none().0, idx)) as *mut MIKEYPayload) + } + } + + pub fn sp_get_param(&self, idx: u32) -> Option<&MIKEYPayloadSPParam> { + unsafe { + &*(from_glib_none(ffi::gst_mikey_payload_sp_get_param(self.to_glib_none().0, idx)) as *mut MIKEYPayloadSPParam) + } + } + + pub fn t_set(&mut self, type_: MIKEYTSType, ts_value: &[u8]) -> bool { + unsafe { + from_glib(ffi::gst_mikey_payload_t_set(self.to_glib_none_mut().0, type_.to_glib(), ts_value.to_glib_none().0)) + } + } + + pub fn key_data_set_interval(&mut self, vf_data: &[u8], vt_data: &[u8]) -> bool { + let vf_len = vf_data.len() as u8; + let vt_len = vt_data.len() as u8; + unsafe { + from_glib(ffi::gst_mikey_payload_key_data_set_interval(self.to_glib_none_mut().0, vf_len, vf_data.to_glib_none().0, vt_len, vt_data.to_glib_none().0)) + } + } - //pub fn t_set(&mut self, type_: MIKEYTSType, ts_value: &[u8]) -> bool { - // unsafe { TODO: call ffi::gst_mikey_payload_t_set() } - //} } diff --git a/gstreamer-sdp/src/m_i_k_e_y_payload_s_p_param.rs b/gstreamer-sdp/src/m_i_k_e_y_payload_s_p_param.rs new file mode 100644 index 000000000..1623f95b2 --- /dev/null +++ b/gstreamer-sdp/src/m_i_k_e_y_payload_s_p_param.rs @@ -0,0 +1,30 @@ +// Copyright (C) 2018 Sebastian Dröge +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::slice; + +use ffi; + +#[repr(C)] +pub struct MIKEYPayloadSPParam(ffi::GstMIKEYPayloadSPParam); + +impl MIKEYPayloadSPParam { + pub fn type_(&self) -> u8 { + self.0.type_ + } + + pub fn len(&self) -> u8 { + self.0.len + } + + pub fn val(&self) -> &[u8] { + unsafe { + slice::from_raw_parts(self.0.val as *const u8, self.0.len as usize) + } + } +} diff --git a/gstreamer-sdp/src/s_d_p_attribute.rs b/gstreamer-sdp/src/s_d_p_attribute.rs index d6244913d..158a925b3 100644 --- a/gstreamer-sdp/src/s_d_p_attribute.rs +++ b/gstreamer-sdp/src/s_d_p_attribute.rs @@ -10,35 +10,43 @@ use std::mem; use std::ffi::CStr; use ffi; +use glib::translate::*; use auto::SDPResult; -pub struct SDPAttribute(ffi::GstSDPAttribute); +#[repr(C)] +pub struct SDPAttribute(pub ffi::GstSDPAttribute); impl SDPAttribute { - pub fn new(key: &str, value: &str) -> Result { + pub fn new(key: &str, value: &str) -> Result { assert_initialized_main_thread!(); unsafe { - let mut attr = mem::uninitialized(); + let mut attr = mem::zeroed(); let result = from_glib(ffi::gst_sdp_attribute_set(&mut attr, key.to_glib_none().0, value.to_glib_none().0)); match result { SDPResult::Ok => Ok(SDPAttribute(attr)), - _ => Err(result), + _ => Err(()), } } } pub fn key(&self) -> &str { - CStr::from_ptr(self.0.key).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.key).to_str().unwrap() + } } pub fn value(&self) -> &str { - CStr::from_ptr(self.0.value).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.value).to_str().unwrap() + } } } impl Drop for SDPAttribute { fn drop(&mut self) { - ffi::gst_sdp_attribute_clear(self.to_glib_none_mut().0); + unsafe { + ffi::gst_sdp_attribute_clear(&mut self.0); + } } } diff --git a/gstreamer-sdp/src/s_d_p_bandwidth.rs b/gstreamer-sdp/src/s_d_p_bandwidth.rs index b1ec8d2e8..fb68505a0 100644 --- a/gstreamer-sdp/src/s_d_p_bandwidth.rs +++ b/gstreamer-sdp/src/s_d_p_bandwidth.rs @@ -10,26 +10,30 @@ use std::mem; use std::ffi::CStr; use ffi; +use glib::translate::*; use auto::SDPResult; -pub struct SDPBandwidth(ffi::GstSDPBandwidth); +#[repr(C)] +pub struct SDPBandwidth(pub ffi::GstSDPBandwidth); impl SDPBandwidth { - pub fn new(bwtype: &str, bandwidth: u32) -> Result { + pub fn new(bwtype: &str, bandwidth: u32) -> Result { assert_initialized_main_thread!(); unsafe { - let mut bw = mem::uninitialized(); + let mut bw = mem::zeroed(); let result = from_glib(ffi::gst_sdp_bandwidth_set(&mut bw, bwtype.to_glib_none().0, bandwidth)); match result { SDPResult::Ok => Ok(SDPBandwidth(bw)), - _ => Err(result), + _ => Err(()), } } } pub fn bwtype(&self) -> &str { - CStr::from_ptr(self.0.bwtype).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.bwtype).to_str().unwrap() + } } pub fn value(&self) -> u32 { @@ -39,6 +43,8 @@ impl SDPBandwidth { impl Drop for SDPBandwidth { fn drop(&mut self) { - ffi::gst_sdp_bandwidth_clear(self.to_glib_none_mut().0); + unsafe { + ffi::gst_sdp_bandwidth_clear(&mut self.0); + } } } diff --git a/gstreamer-sdp/src/s_d_p_connection.rs b/gstreamer-sdp/src/s_d_p_connection.rs index 78f0d1e8c..26bdd9147 100644 --- a/gstreamer-sdp/src/s_d_p_connection.rs +++ b/gstreamer-sdp/src/s_d_p_connection.rs @@ -10,17 +10,18 @@ use std::mem; use std::ffi::CStr; use ffi; +use glib::translate::*; use auto::SDPResult; - -pub struct SDPConnection(ffi::GstSDPConnection); +#[repr(C)] +pub struct SDPConnection(pub ffi::GstSDPConnection); impl SDPConnection { - pub fn new(nettype: &str, addrtype: &str, address: &str, ttl: u32, addr_number: u32) -> Result { + pub fn new(nettype: &str, addrtype: &str, address: &str, ttl: u32, addr_number: u32) -> Result { assert_initialized_main_thread!(); unsafe { - let mut conn = mem::uninitialized(); + let mut conn = mem::zeroed(); let result = from_glib(ffi::gst_sdp_connection_set( &mut conn, nettype.to_glib_none().0, @@ -31,21 +32,27 @@ impl SDPConnection { )); match result { SDPResult::Ok => Ok(SDPConnection(conn)), - _ => Err(result), + _ => Err(()), } } } pub fn nettype(&self) -> &str { - CStr::from_ptr(self.0.nettype).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.nettype).to_str().unwrap() + } } pub fn addrtype(&self) -> &str { - CStr::from_ptr(self.0.addrtype).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.addrtype).to_str().unwrap() + } } pub fn address(&self) -> &str { - CStr::from_ptr(self.0.address).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.address).to_str().unwrap() + } } pub fn ttl(&self) -> u32 { @@ -59,6 +66,8 @@ impl SDPConnection { impl Drop for SDPConnection { fn drop(&mut self) { - ffi::gst_sdp_connection_clear(self.to_glib_none_mut().0); + unsafe { + ffi::gst_sdp_connection_clear(&mut self.0); + } } } diff --git a/gstreamer-sdp/src/s_d_p_key.rs b/gstreamer-sdp/src/s_d_p_key.rs index 3669b6ffc..5f48bcfd9 100644 --- a/gstreamer-sdp/src/s_d_p_key.rs +++ b/gstreamer-sdp/src/s_d_p_key.rs @@ -13,11 +13,15 @@ use ffi; pub struct SDPKey(ffi::GstSDPKey); impl SDPKey { - pub fn type(&self) -> &str { - CStr::from_ptr(self.0.type_).to_str().unwrap() + pub fn type_(&self) -> &str { + unsafe { + CStr::from_ptr(self.0.type_).to_str().unwrap() + } } pub fn data(&self) -> &str { - CStr::from_ptr(self.0.data).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.data).to_str().unwrap() + } } } diff --git a/gstreamer-sdp/src/s_d_p_media.rs b/gstreamer-sdp/src/s_d_p_media.rs index f2cbdad71..8a1d0ec8b 100644 --- a/gstreamer-sdp/src/s_d_p_media.rs +++ b/gstreamer-sdp/src/s_d_p_media.rs @@ -8,14 +8,17 @@ use std::mem; use std::ptr; +use std::ffi::CStr; use ffi; use glib::translate::*; use glib_ffi; use gst; -use auto::SDPResult; -use m_i_k_e_y_message::MIKEYMessage; +use auto::{ + SDPResult, + MIKEYMessage, +}; use s_d_p_attribute::SDPAttribute; use s_d_p_bandwidth::SDPBandwidth; use s_d_p_connection::SDPConnection; @@ -25,55 +28,66 @@ glib_wrapper! { pub struct SDPMedia(Boxed); match fn { - copy => |ptr| ffi::gst_sdp_media_copy(mut_override(ptr)), - free => |ptr| ffi::gst_sdp_media_free(ptr), + copy => |ptr| { + let mut copy = ptr::null_mut(); + ffi::gst_sdp_media_copy(ptr as *const ffi::GstSDPMedia, &mut copy); + copy + }, + free => |ptr| { + ffi::gst_sdp_media_free(ptr); + }, } } impl SDPMedia { - pub fn add_attribute<'a, P: Into>>(&mut self, key: &str, value: P) -> Result<_, ()> { + pub fn new() -> (SDPResult, SDPMedia) { + assert_initialized_main_thread!(); + unsafe { + let mut media = ptr::null_mut(); + let ret = from_glib(ffi::gst_sdp_media_new(&mut media)); + (ret, from_glib_full(media)) + } + } + + pub fn add_attribute<'a, P: Into>>(&mut self, key: &str, value: P) -> Result<(), ()> { let value = value.into(); let value = value.to_glib_none(); let result = unsafe { from_glib(ffi::gst_sdp_media_add_attribute(self.to_glib_none_mut().0, key.to_glib_none().0, value.0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_bandwidth(&mut self, bwtype: &str, bandwidth: u32) -> Result<_, ()> { + pub fn add_bandwidth(&mut self, bwtype: &str, bandwidth: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_add_bandwidth(self.to_glib_none_mut().0, bwtype.to_glib_none().0, bandwidth)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_connection(&mut self, nettype: &str, addrtype: &str, address: &str, ttl: u32, addr_number: u32) -> Result<_, ()> { + pub fn add_connection(&mut self, nettype: &str, addrtype: &str, address: &str, ttl: u32, addr_number: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_add_connection(self.to_glib_none_mut().0, nettype.to_glib_none().0, addrtype.to_glib_none().0, address.to_glib_none().0, ttl, addr_number)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_format(&mut self, format: &str) -> Result<_, ()> { + pub fn add_format(&mut self, format: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_add_format(self.to_glib_none_mut().0, format.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -89,14 +103,13 @@ impl SDPMedia { } } - pub fn attributes_to_caps(&self, caps: &gst::Caps) -> Result<_, ()> { + pub fn attributes_to_caps(&self, caps: &gst::Caps) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_attributes_to_caps(self.to_glib_none().0, caps.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -118,27 +131,55 @@ impl SDPMedia { } } - pub fn get_attribute(&self, idx: u32) -> Option { + pub fn get_attribute(&self, idx: u32) -> Option<&SDPAttribute> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_attribute(self.to_glib_none().0, idx) ) + let ptr = ffi::gst_sdp_media_get_attribute(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPAttribute)) + } } } - pub fn get_attribute_val(&self, key: &str) -> Option { + pub fn get_attribute_val(&self, key: &str) -> Option<&str> { unsafe { - from_glib_none(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.to_glib_none().0, key.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_attribute_val_n(&self, key: &str, nth: u32) -> Option { + pub fn get_attribute_val_n(&self, key: &str, nth: u32) -> Option<&str> { unsafe { - from_glib_none(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.to_glib_none().0, key.to_glib_none().0, nth); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_bandwidth(&self, idx: u32) -> Option { + pub fn get_bandwidth(&self, idx: u32) -> Option<&SDPBandwidth> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_bandwidth(self.to_glib_none().0, idx)) + let ptr = ffi::gst_sdp_media_get_bandwidth(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPBandwidth)) + } } } @@ -148,33 +189,70 @@ impl SDPMedia { } } - pub fn get_connection(&self, idx: u32) -> Option { + pub fn get_connection(&self, idx: u32) -> Option<&SDPConnection> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_connection(self.to_glib_none().0, idx)) + let ptr = ffi::gst_sdp_media_get_connection(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPConnection)) + } } } - pub fn get_format(&self, idx: u32) -> Option { + pub fn get_format(&self, idx: u32) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_format(self.to_glib_none().0, idx)) + let ptr = ffi::gst_sdp_media_get_format(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_information(&self) -> Option { + pub fn get_information(&self) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_information(self.to_glib_none().0)) + let ptr = ffi::gst_sdp_media_get_information(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_key(&self) -> Option { + pub fn get_key(&self) -> Option<&SDPKey> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_key(self.to_glib_none().0)) + let ptr = ffi::gst_sdp_media_get_key(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPKey)) + } } } - pub fn get_media(&self) -> Option { + pub fn get_media(&self) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_media(self.to_glib_none().0)) + let ptr = ffi::gst_sdp_media_get_media(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } @@ -190,259 +268,218 @@ impl SDPMedia { } } - pub fn get_proto(&self) -> Option { + pub fn get_proto(&self) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_media_get_proto(self.to_glib_none().0)) - } - } - - pub fn init(&mut self) -> Result<_, ()> { - let result = unsafe { - from_glib(ffi::gst_sdp_media_init(self.to_glib_none_mut().0)) - }; - match result { - SDPResult::ok => Some(_), - _ => Err(()), + let ptr = ffi::gst_sdp_media_get_proto(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } } } } - pub fn insert_attribute(&mut self, idx: i32, attr: &mut SDPAttribute) -> Result<_, ()> { + pub fn insert_attribute(&mut self, idx: i32, mut attr: SDPAttribute) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_media_insert_attribute(self.to_glib_none_mut().0, idx, attr.to_glib_none_mut().0)) + from_glib(ffi::gst_sdp_media_insert_attribute(self.to_glib_none_mut().0, idx, &mut attr.0)) }; + mem::forget(attr); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_bandwidth(&mut self, idx: i32, bw: &mut SDPBandwidth) -> Result<_, ()> { + pub fn insert_bandwidth(&mut self, idx: i32, mut bw: SDPBandwidth) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_media_insert_bandwidth(self.to_glib_none_mut().0, idx, self.to_glib_none_mut().0)) + from_glib(ffi::gst_sdp_media_insert_bandwidth(self.to_glib_none_mut().0, idx, &mut bw.0)) }; + mem::forget(bw); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_connection(&mut self, idx: i32, conn: &mut SDPConnection) -> Result<_, ()> { + pub fn insert_connection(&mut self, idx: i32, mut conn: SDPConnection) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_media_insert_connection(self.to_glib_none_mut().0, idx, conn.to_glib_none_mut().0)) + from_glib(ffi::gst_sdp_media_insert_connection(self.to_glib_none_mut().0, idx, &mut conn.0)) }; + mem::forget(conn); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_format(&mut self, idx: i32, format: &str) -> Result<_, ()> { + pub fn insert_format(&mut self, idx: i32, format: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_insert_format(self.to_glib_none_mut().0, idx, format.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } #[cfg(any(feature = "v1_8_1", feature = "dox"))] - pub fn parse_keymgmt(&self) -> Result { + pub fn parse_keymgmt(&self) -> Result { unsafe { let mut mikey = ptr::null_mut(); let result = from_glib(ffi::gst_sdp_media_parse_keymgmt(self.to_glib_none().0, &mut mikey)); match result { - SDPResult::ok => Some(from_glib_full(mikey)), - _ => Err(result), + SDPResult::Ok => Ok(from_glib_full(mikey)), + _ => Err(()), } } } - pub fn remove_attribute(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_attribute(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_remove_attribute(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_bandwidth(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_bandwidth(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_remove_bandwidth(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_connection(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_connection(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_remove_connection(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_format(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_format(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_remove_format(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_attribute(&mut self, idx: u32, attr: &mut SDPAttribute) -> Result<_, ()> { + pub fn replace_attribute(&mut self, idx: u32, mut attr: SDPAttribute) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_media_replace_attribute(self.to_glib_none_mut().0, idx, attr.to_glib_none_mut().0)) + from_glib(ffi::gst_sdp_media_replace_attribute(self.to_glib_none_mut().0, idx, &mut attr.0)) }; + mem::forget(attr); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_bandwidth(&mut self, idx: u32, bw: &mut SDPBandwidth) -> Result<_, ()> { + pub fn replace_bandwidth(&mut self, idx: u32, mut bw: SDPBandwidth) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_media_replace_bandwidth(self.to_glib_none_mut().0, idx, bw.to_glib_none_mut().0)) + from_glib(ffi::gst_sdp_media_replace_bandwidth(self.to_glib_none_mut().0, idx, &mut bw.0)) }; + mem::forget(bw); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_connection(&mut self, idx: u32, conn: &mut SDPConnection) -> Result<_, ()> { + pub fn replace_connection(&mut self, idx: u32, mut conn: SDPConnection) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_media_replace_connection(self.to_glib_none_mut().0, idx, conn.to_glib_none_mut().0)) + from_glib(ffi::gst_sdp_media_replace_connection(self.to_glib_none_mut().0, idx, &mut conn.0)) }; + mem::forget(conn); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_format(&mut self, idx: u32, format: &str) -> Result<_, ()> { + pub fn replace_format(&mut self, idx: u32, format: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_replace_format(self.to_glib_none_mut().0, idx, format.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_information(&mut self, information: &str) -> Result<_, ()> { + pub fn set_information(&mut self, information: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_set_information(self.to_glib_none_mut().0, information.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_key(&mut self, type_: &str, data: &str) -> Result<_, ()> { + pub fn set_key(&mut self, type_: &str, data: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_set_key(self.to_glib_none_mut().0, type_.to_glib_none().0, data.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_media(&mut self, med: &str) -> Result<_, ()> { + pub fn set_media(&mut self, med: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_set_media(self.to_glib_none_mut().0, med.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_port_info(&mut self, port: u32, num_ports: u32) -> Result<_, ()> { + pub fn set_port_info(&mut self, port: u32, num_ports: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_set_port_info(self.to_glib_none_mut().0, port, num_ports)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_proto(&mut self, proto: &str) -> Result<_, ()> { + pub fn set_proto(&mut self, proto: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_media_set_proto(self.to_glib_none_mut().0, proto.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn uninit(&mut self) -> Result<_, ()> { - let result = unsafe { - from_glib(ffi::gst_sdp_media_uninit(self.to_glib_none_mut().0)) - }; - match result { - SDPResult::ok => Some(_), - _ => Err(()), - } - } - } - - pub fn new() -> (SDPResult, SDPMedia) { - assert_initialized_main_thread!(); - unsafe { - let mut media = ptr::null_mut(); - let ret = from_glib(ffi::gst_sdp_media_new(&mut media)); - (ret, from_glib_full(media)) - } - } - - pub fn set_media_from_caps(caps: &gst::Caps, media: &mut SDPMedia) -> Result<_, ()> { + pub fn set_media_from_caps(caps: &gst::Caps, media: &mut SDPMedia) -> Result<(), ()> { assert_initialized_main_thread!(); let result = unsafe { from_glib(ffi::gst_sdp_media_set_media_from_caps(caps.to_glib_none().0, media.to_glib_none_mut().0)) }; + mem::forget(media); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } } - -impl Default for SDPMedia { - fn default() -> Self { - Self::new() - } -} - -unsafe impl Send for SDPMedia {} diff --git a/gstreamer-sdp/src/s_d_p_message.rs b/gstreamer-sdp/src/s_d_p_message.rs index ca6a2a387..2281e61be 100644 --- a/gstreamer-sdp/src/s_d_p_message.rs +++ b/gstreamer-sdp/src/s_d_p_message.rs @@ -6,12 +6,21 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use ffi; -use glib; -use glib_ffi; +use std::mem; +use std::ptr; +use std::ffi::CStr; -use auto::SDPResult; -use m_i_k_e_y_message::MikeyMessage; +use ffi; +use gst; +use glib_ffi; +use gobject_ffi; +use glib::translate::*; +use gst::MiniObject; + +use auto::{ + SDPResult, + MIKEYMessage, +}; use s_d_p_media::SDPMedia; use s_d_p_attribute::SDPAttribute; use s_d_p_bandwidth::SDPBandwidth; @@ -35,87 +44,70 @@ impl SDPMessage { pub fn new() -> SDPMessage { assert_initialized_main_thread!(); unsafe { - let msg = glib_ffi::g_malloc(mem::size_of::()); - ffi::gst_sdp_message_new(msg); + let mut msg = mem::zeroed(); + ffi::gst_sdp_message_new(&mut msg); from_glib_full(msg) } } - pub fn add_attribute<'a, P: Into>>(&mut self, key: &str, value: P) -> Result<_, ()> { + pub fn add_attribute<'a, P: Into>>(&mut self, key: &str, value: P) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_add_attribute(self.to_glib_none_mut().0, key.to_glib_none().0, value.into().to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_bandwidth(&mut self, bwtype: &str, bandwidth: u32) -> Result<_, ()> { - let result = unsafe { - from_glib(ffi::gst_sdp_message_add_bandwidth(self.to_glib_none_mut().0, bwtype.to_glib_none().0, bandwidth)) - }; - match result { - SDPResult::ok => Some(_), - _ => Err(()), - } - } - } - - pub fn add_email(&mut self, email: &str) -> Result<_, ()> { + pub fn add_email(&mut self, email: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_add_email(self.to_glib_none_mut().0, email.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_media(&mut self, media: SDPMedia) -> Result<_, ()> { + pub fn add_media(&mut self, media: SDPMedia) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_add_media(self.to_glib_none_mut().0, media.to_glib_full())) + from_glib(ffi::gst_sdp_message_add_media(self.to_glib_none_mut().0, media.to_glib_full() as *mut ffi::GstSDPMedia)) }; mem::forget(media); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_phone(&mut self, phone: &str) -> Result<_, ()> { + pub fn add_phone(&mut self, phone: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_add_phone(self.to_glib_none_mut().0, phone.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_time(&mut self, start: &str, stop: &str, repeat: &[&str]) -> Result<_, ()> { + pub fn add_time(&mut self, start: &str, stop: &str, repeat: &[&str]) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_add_time(self.to_glib_none_mut().0, start.to_glib_none().0, stop.to_glib_none().0, repeat.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn add_zone(&mut self, adj_time: &str, typed_time: &str) -> Result<_, ()> { + pub fn add_zone(&mut self, adj_time: &str, typed_time: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_add_zone(self.to_glib_none_mut().0, adj_time.to_glib_none().0, typed_time.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -131,14 +123,13 @@ impl SDPMessage { } } - pub fn attributes_to_caps(&self, caps: &mut gst::CapsRef) -> Result<_, ()> { + pub fn attributes_to_caps(&self, caps: &mut gst::CapsRef) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_attributes_to_caps(self.to_glib_none().0, caps.to_glib_none_mut().0)) + from_glib(ffi::gst_sdp_message_attributes_to_caps(self.to_glib_none().0, caps.as_mut_ptr())) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -148,14 +139,13 @@ impl SDPMessage { } } - pub fn dump(&self) -> Result<_, ()> { + pub fn dump(&self) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_dump(self.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -165,169 +155,275 @@ impl SDPMessage { } } - pub fn get_attribute(&self, idx: u32) -> Option { + pub fn get_attribute(&self, idx: u32) -> Option<&SDPAttribute> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_attribute(self.to_glib_none().0, idx)) + let ptr = ffi::gst_sdp_message_get_attribute(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPAttribute)) + } } } - pub fn get_attribute_val(&self, key: &str) -> Option { + pub fn get_attribute_val(&self, key: &str) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_attribute_val(self.to_glib_none().0, key.to_glib_none().0)) + let ptr = ffi::gst_sdp_message_get_attribute_val(self.to_glib_none().0, key.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_attribute_val_n(&self, key: &str, nth: u32) -> Option { + pub fn get_attribute_val_n(&self, key: &str, nth: u32) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_attribute_val_n(self.to_glib_none().0, key.to_glib_none().0, nth)) + let ptr = ffi::gst_sdp_message_get_attribute_val_n(self.to_glib_none().0, key.to_glib_none().0, nth); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_bandwidth(&self, idx: u32) -> Option { + pub fn get_bandwidth(&self, idx: u32) -> Option<&SDPBandwidth> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_bandwidth()) + let ptr = ffi::gst_sdp_message_get_bandwidth(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPBandwidth)) + } } } - pub fn get_connection(&self) -> Option { + pub fn get_connection(&self) -> Option<&SDPConnection> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_connection()) + let ptr = ffi::gst_sdp_message_get_connection(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPConnection)) + } } } - pub fn get_email(&self, idx: u32) -> Option { + pub fn get_email(&self, idx: u32) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_email(self.to_glib_none().0, idx)) + let ptr = ffi::gst_sdp_message_get_email(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_information(&self) -> Option { + pub fn get_information(&self) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_information(self.to_glib_none().0)) + let ptr = ffi::gst_sdp_message_get_information(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_key(&self) -> Option { + pub fn get_key(&self) -> Option<&SDPKey> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_key()) + let ptr = ffi::gst_sdp_message_get_key(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPKey)) + } } } - pub fn get_media(&self, idx: u32) -> Option { + pub fn get_media(&self, idx: u32) -> Option<&SDPMedia> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_media(self.to_glib_none().0, idx)) + 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)) + } } } - pub fn get_origin(&self) -> Option { + pub fn get_origin(&self) -> Option<&SDPOrigin> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_origin()) + let ptr = ffi::gst_sdp_message_get_origin(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPOrigin)) + } } } - pub fn get_phone(&self, idx: u32) -> Option { + pub fn get_phone(&self, idx: u32) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_phone(self.to_glib_none().0, idx)) + let ptr = ffi::gst_sdp_message_get_phone(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_session_name(&self) -> Option { + pub fn get_session_name(&self) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_session_name(self.to_glib_none().0)) + let ptr = ffi::gst_sdp_message_get_session_name(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_time(&self, idx: u32) -> Option { + pub fn get_time(&self, idx: u32) -> Option<&SDPTime> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_time()) + let ptr = ffi::gst_sdp_message_get_time(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPTime)) + } } } - pub fn get_uri(&self) -> Option { + pub fn get_uri(&self) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_uri(self.to_glib_none().0)) + let ptr = ffi::gst_sdp_message_get_uri(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_version(&self) -> Option { + pub fn get_version(&self) -> Option<&str> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_version(self.to_glib_none().0)) + let ptr = ffi::gst_sdp_message_get_version(self.to_glib_none().0); + if ptr.is_null() { + None + } else { + let result = CStr::from_ptr(ptr).to_str(); + match result { + Ok(attr) => Some(attr), + Err(_) => None, + } + } } } - pub fn get_zone(&self, idx: u32) -> Option { + pub fn get_zone(&self, idx: u32) -> Option<&SDPZone> { unsafe { - from_glib_none(ffi::gst_sdp_message_get_zone()) + let ptr = ffi::gst_sdp_message_get_zone(self.to_glib_none().0, idx); + if ptr.is_null() { + None + } else { + Some(&*(ptr as *mut SDPZone)) + } } } - pub fn insert_attribute(&mut self, idx: i32, attr: SDPAttribute) -> Result<_, ()> { + pub fn insert_attribute(&mut self, idx: i32, mut attr: SDPAttribute) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_insert_attribute(self.to_glib_none_mut().0, idx, attr.to_glib_full())) + from_glib(ffi::gst_sdp_message_insert_attribute(self.to_glib_none_mut().0, idx, &mut attr.0)) }; mem::forget(attr); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_bandwidth(&mut self, idx: i32, bw: SDPBandwidth) -> Result<_, ()> { + pub fn insert_bandwidth(&mut self, idx: i32, mut bw: SDPBandwidth) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_insert_bandwidth(self.to_glib_none_mut().0, idx, bw.to_glib_full())) + from_glib(ffi::gst_sdp_message_insert_bandwidth(self.to_glib_none_mut().0, idx, &mut bw.0)) }; mem::forget(bw); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_email(&mut self, idx: i32, email: &str) -> Result<_, ()> { + pub fn insert_email(&mut self, idx: i32, email: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_insert_email(self.to_glib_none_mut().0, idx, email.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_phone(&mut self, idx: i32, phone: &str) -> Result<_, ()> { + pub fn insert_phone(&mut self, idx: i32, phone: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_insert_phone(self.to_glib_none_mut().0, idx, phone.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_time(&mut self, idx: i32, time: SDPTime) -> Result<_, ()> { + pub fn insert_time(&mut self, idx: i32, mut time: SDPTime) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_insert_time(self.to_glib_none_mut().0, idx, time.to_glib_full())) + from_glib(ffi::gst_sdp_message_insert_time(self.to_glib_none_mut().0, idx, &mut time.0)) }; mem::forget(time); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn insert_zone(&mut self, idx: i32, zone: SDPZone) -> Result<_, ()> { + pub fn insert_zone(&mut self, idx: i32, mut zone: SDPZone) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_insert_zone(self.to_glib_none_mut().0, idx, zone.to_glib_full())) + from_glib(ffi::gst_sdp_message_insert_zone(self.to_glib_none_mut().0, idx, &mut zone.0)) }; - mem:forget(zone); + mem::forget(zone); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -338,15 +434,14 @@ impl SDPMessage { } #[cfg(any(feature = "v1_8_1", feature = "dox"))] - pub fn parse_keymgmt(&self, mikey: MIKEYMessage) -> Result<_, ()> { + pub fn parse_keymgmt(&self, mikey: MIKEYMessage) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_parse_keymgmt(self.to_glib_none().0, &mut mikey)) }; mem::forget(mikey); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -356,216 +451,197 @@ impl SDPMessage { } } - pub fn remove_attribute(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_attribute(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_remove_attribute(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_bandwidth(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_bandwidth(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_remove_bandwidth(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_email(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_email(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_remove_email(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_phone(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_phone(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_remove_phone(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_time(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_time(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_remove_time(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn remove_zone(&mut self, idx: u32) -> Result<_, ()> { + pub fn remove_zone(&mut self, idx: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_remove_zone(self.to_glib_none_mut().0, idx)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_attribute(&mut self, idx: u32, attr: SDPAttribute) -> Result<_, ()> { + pub fn replace_attribute(&mut self, idx: u32, mut attr: SDPAttribute) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_replace_attribute(self.to_glib_none_mut().0, idx, attr.to_glib_full())) + from_glib(ffi::gst_sdp_message_replace_attribute(self.to_glib_none_mut().0, idx, &mut attr.0)) }; mem::forget(attr); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_bandwidth(&mut self, idx: u32, bw: SDPBandwidth) -> Result<_, ()> { + pub fn replace_bandwidth(&mut self, idx: u32, mut bw: SDPBandwidth) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_replace_bandwidth(self.to_glib_none_mut().0, idx, bw.to_glib_full())) + from_glib(ffi::gst_sdp_message_replace_bandwidth(self.to_glib_none_mut().0, idx, &mut bw.0)) }; mem::forget(bw); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_email(&mut self, idx: u32, email: &str) -> Result<_, ()> { + pub fn replace_email(&mut self, idx: u32, email: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_replace_email(self.to_glib_none_mut().0, idx, email.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_phone(&mut self, idx: u32, phone: &str) -> Result<_, ()> { + pub fn replace_phone(&mut self, idx: u32, phone: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_replace_phone(self.to_glib_none_mut().0, idx, phone.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_time(&mut self, idx: u32, time: SDPTime) -> Result<_, ()> { + pub fn replace_time(&mut self, idx: u32, mut time: SDPTime) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_replace_time(self.to_glib_none_mut().0, idx, time.to_glib_full())) + from_glib(ffi::gst_sdp_message_replace_time(self.to_glib_none_mut().0, idx, &mut time.0)) }; mem::forget(time); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn replace_zone(&mut self, idx: u32, zone: SDPZone) -> Result<_, ()> { + pub fn replace_zone(&mut self, idx: u32, mut zone: SDPZone) -> Result<(), ()> { let result = unsafe { - from_glib(ffi::gst_sdp_message_replace_zone(self.to_glib_none_mut().0, idx, zone.to_glib_full())) + from_glib(ffi::gst_sdp_message_replace_zone(self.to_glib_none_mut().0, idx, &mut zone.0)) }; mem::forget(zone); match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_connection(&mut self, nettype: &str, addrtype: &str, address: &str, ttl: u32, addr_number: u32) -> Result<_, ()> { + pub fn set_connection(&mut self, nettype: &str, addrtype: &str, address: &str, ttl: u32, addr_number: u32) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_set_connection(self.to_glib_none_mut().0, nettype.to_glib_none().0, addrtype.to_glib_none().0, address.to_glib_none().0, ttl, addr_number)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_information(&mut self, information: &str) -> Result<_, ()> { + pub fn set_information(&mut self, information: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_set_information(self.to_glib_none_mut().0, information.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_key(&mut self, type_: &str, data: &str) -> Result<_, ()> { + pub fn set_key(&mut self, type_: &str, data: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_set_key(self.to_glib_none_mut().0, type_.to_glib_none().0, data.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_origin(&mut self, username: &str, sess_id: &str, sess_version: &str, nettype: &str, addrtype: &str, addr: &str) -> Result<_, ()> { + pub fn set_origin(&mut self, username: &str, sess_id: &str, sess_version: &str, nettype: &str, addrtype: &str, addr: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_set_origin(self.to_glib_none_mut().0, username.to_glib_none().0, sess_id.to_glib_none().0, sess_version.to_glib_none().0, nettype.to_glib_none().0, addrtype.to_glib_none().0, addr.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_session_name(&mut self, session_name: &str) -> Result<_, ()> { + pub fn set_session_name(&mut self, session_name: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_set_session_name(self.to_glib_none_mut().0, session_name.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_uri(&mut self, uri: &str) -> Result<_, ()> { + pub fn set_uri(&mut self, uri: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_set_uri(self.to_glib_none_mut().0, uri.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } - pub fn set_version(&mut self, version: &str) -> Result<_, ()> { + pub fn set_version(&mut self, version: &str) -> Result<(), ()> { let result = unsafe { from_glib(ffi::gst_sdp_message_set_version(self.to_glib_none_mut().0, version.to_glib_none().0)) }; match result { - SDPResult::ok => Some(_), + SDPResult::Ok => Ok(()), _ => Err(()), - } } } @@ -588,27 +664,27 @@ impl SDPMessage { } } - pub fn parse_buffer(data: &[u8]) -> Result { + pub fn parse_buffer(data: &[u8]) -> Result { assert_initialized_main_thread!(); unsafe { let size = data.len() as u32; - let mut msg = glib_ffi::g_malloc(mem::size_of::()); - let result = from_glib(ffi::gst_sdp_message_parse_buffer(data.to_glib_none().0, size, &mut msg)); + let msg = glib_ffi::g_malloc0(mem::size_of::()) as *mut ffi::GstSDPMessage; + let result = from_glib(ffi::gst_sdp_message_parse_buffer(data.to_glib_none().0, size, msg)); match result { - SDPResult::ok => Some(from_glib_full(msg)), - _ => Err(result), + SDPResult::Ok => Ok(from_glib_full(msg)), + _ => Err(()), } } } - pub fn parse_uri(uri: &str, msg: &mut SDPMessage) -> Result { + pub fn parse_uri(uri: &str) -> Result { assert_initialized_main_thread!(); unsafe { - let mut msg = glib_ffi::g_malloc(mem::size_of::()); - let result = from_glib(ffi::gst_sdp_message_parse_uri(uri.to_glib_none().0, &mut msg)); + let msg = glib_ffi::g_malloc0(mem::size_of::()) as *mut ffi::GstSDPMessage; + let result = from_glib(ffi::gst_sdp_message_parse_uri(uri.to_glib_none().0, msg)); match result { - SDPResult::ok => Some(from_glib_full(msg)), - _ => Err(result), + SDPResult::Ok => Ok(from_glib_full(msg)), + _ => Err(()), } } } diff --git a/gstreamer-sdp/src/s_d_p_origin.rs b/gstreamer-sdp/src/s_d_p_origin.rs index 953987d33..d97590810 100644 --- a/gstreamer-sdp/src/s_d_p_origin.rs +++ b/gstreamer-sdp/src/s_d_p_origin.rs @@ -10,30 +10,43 @@ use std::ffi::CStr; use ffi; -pub struct SDPOrigin(ffi::GstSDPOrigin); +#[repr(C)] +pub struct SDPOrigin(pub ffi::GstSDPOrigin); impl SDPOrigin { pub fn username(&self) -> &str { - CStr::from_ptr(self.0.username).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.username).to_str().unwrap() + } } pub fn sess_id(&self) -> &str { - CStr::from_ptr(self.0.sess_id).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.sess_id).to_str().unwrap() + } } pub fn sess_version(&self) -> &str { - CStr::from_ptr(self.0.sess_version).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.sess_version).to_str().unwrap() + } } pub fn nettype(&self) -> &str { - CStr::from_ptr(self.0.nettype).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.nettype).to_str().unwrap() + } } pub fn addrtype(&self) -> &str { - CStr::from_ptr(self.0.addrtype).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.addrtype).to_str().unwrap() + } } pub fn addr(&self) -> &str { - CStr::from_ptr(self.0.addr).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.addr).to_str().unwrap() + } } } diff --git a/gstreamer-sdp/src/s_d_p_time.rs b/gstreamer-sdp/src/s_d_p_time.rs index aaaaf65cb..1cf2cb91d 100644 --- a/gstreamer-sdp/src/s_d_p_time.rs +++ b/gstreamer-sdp/src/s_d_p_time.rs @@ -7,20 +7,23 @@ // except according to those terms. use std::mem; -use std::ffi:CStr; +use std::os::raw::c_char; +use std::ffi::CStr; use ffi; +use glib::translate::*; use auto::SDPResult; -pub struct SDPTime(ffi::GstSDPTime); +#[repr(C)] +pub struct SDPTime(pub ffi::GstSDPTime); impl SDPTime { - pub fn new(start: &str, stop: &str, repeat: &[&str]) -> Result { + pub fn new(start: &str, stop: &str, repeat: &[&str]) -> Result { assert_initialized_main_thread!(); unsafe { - let mut time = mem::uninitialized(); - let result =from_glib(ffi::gst_sdp_time_set( + let mut time = mem::zeroed(); + let result = from_glib(ffi::gst_sdp_time_set( &mut time, start.to_glib_none().0, stop.to_glib_none().0, @@ -28,32 +31,40 @@ impl SDPTime { )); match result { SDPResult::Ok => Ok(SDPTime(time)), - _ => Err(result), + _ => Err(()), } } } pub fn start(&self) -> &str { - CStr::from_ptr(self.0.start).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.start).to_str().unwrap() + } } pub fn stop(&self) -> &str { - CStr::from_ptr(self.0.stop).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.stop).to_str().unwrap() + } } pub fn repeat(&self) -> Vec<&str> { - let arr = (*self.0.repeat).data as *const *const c_char; - let len = (*self.0.repeat).len as usize; - let vec = Vec::with_capacity(len); - for i in 0..len { - vec.push(CStr::from_ptr(arr.offset(i)).to_str().unwrap()); + unsafe { + let arr = (*self.0.repeat).data as *const *const c_char; + let len = (*self.0.repeat).len as usize; + let mut vec = Vec::with_capacity(len); + for i in 0..len { + vec.push(CStr::from_ptr(*arr.offset(i as isize)).to_str().unwrap()); + } + vec } - vec } } impl Drop for SDPTime { fn drop(&mut self) { - ffi::gst_sdp_time_clear(self.to_glib_none_mut().0); + unsafe { + ffi::gst_sdp_time_clear(&mut self.0); + } } } diff --git a/gstreamer-sdp/src/s_d_p_zone.rs b/gstreamer-sdp/src/s_d_p_zone.rs index ab30cf823..0fb50904a 100644 --- a/gstreamer-sdp/src/s_d_p_zone.rs +++ b/gstreamer-sdp/src/s_d_p_zone.rs @@ -7,38 +7,46 @@ // except according to those terms. use std::mem; -use std::ffi:CStr; +use std::ffi::CStr; + +use ffi; +use glib::translate::*; use auto::SDPResult; -use ffi; - -pub struct SDPZone(ffi::GstSDPZone); +#[repr(C)] +pub struct SDPZone(pub ffi::GstSDPZone); impl SDPZone { - pub fn new(time: &str, typed_time: &str) -> Result { + pub fn new(time: &str, typed_time: &str) -> Result { assert_initialized_main_thread!(); unsafe { - let mut zone = mem::uninitialized(); + let mut zone = mem::zeroed(); let result = from_glib(ffi::gst_sdp_zone_set(&mut zone, time.to_glib_none().0, typed_time.to_glib_none().0)); match result { SDPResult::Ok => Ok(SDPZone(zone)), - _ => Err(result), + _ => Err(()), } } } pub fn time(&self) -> &str { - CStr::from_ptr(self.0.time).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.time).to_str().unwrap() + } } pub fn typed_time(&self) -> &str { - CStr::from_ptr(self.0.typed_time).to_str().unwrap() + unsafe { + CStr::from_ptr(self.0.typed_time).to_str().unwrap() + } } } impl Drop for SDPZone { fn drop(&mut self) { - ffi::gst_sdp_zone_clear(self.to_glib_none_mut().0); + unsafe { + ffi::gst_sdp_zone_clear(&mut self.0); + } } }