gstreamer-gl, gstreamer-pbutils, gstreamer-sdp: Change functions from returning Option to Result

Partial work for:
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/issues/216
This commit is contained in:
Tony Jinwoo Ahn 2019-12-17 08:20:47 +00:00 committed by Sebastian Dröge
parent 62c0b689a6
commit c3b7f0f353
5 changed files with 67 additions and 31 deletions

View file

@ -29,7 +29,7 @@ pub trait VideoFrameGLExt {
fn from_buffer_ref_readable_gl<'a, 'b>( fn from_buffer_ref_readable_gl<'a, 'b>(
buffer: &'a gst::BufferRef, buffer: &'a gst::BufferRef,
info: &'b VideoInfo, info: &'b VideoInfo,
) -> Option<VideoFrameRef<&'a gst::BufferRef>>; ) -> Result<VideoFrameRef<&'a gst::BufferRef>, glib::error::BoolError>;
fn get_texture_id(&self, idx: u32) -> Option<u32>; fn get_texture_id(&self, idx: u32) -> Option<u32>;
} }
@ -45,7 +45,7 @@ impl VideoFrameGLExt for VideoFrame<Readable> {
fn from_buffer_ref_readable_gl<'a, 'b>( fn from_buffer_ref_readable_gl<'a, 'b>(
buffer: &'a gst::BufferRef, buffer: &'a gst::BufferRef,
info: &'b VideoInfo, info: &'b VideoInfo,
) -> Option<VideoFrameRef<&'a gst::BufferRef>> { ) -> Result<VideoFrameRef<&'a gst::BufferRef>, glib::error::BoolError> {
VideoFrameRef::<&gst::BufferRef>::from_buffer_ref_readable_gl(buffer, info) VideoFrameRef::<&gst::BufferRef>::from_buffer_ref_readable_gl(buffer, info)
} }
@ -95,19 +95,21 @@ impl<'a> VideoFrameGLExt for VideoFrameRef<&'a gst::BufferRef> {
fn from_buffer_ref_readable_gl<'b, 'c>( fn from_buffer_ref_readable_gl<'b, 'c>(
buffer: &'b gst::BufferRef, buffer: &'b gst::BufferRef,
info: &'c VideoInfo, info: &'c VideoInfo,
) -> Option<VideoFrameRef<&'b gst::BufferRef>> { ) -> Result<VideoFrameRef<&'b gst::BufferRef>, glib::error::BoolError> {
skip_assert_initialized!(); skip_assert_initialized!();
let n_mem = match buffer_n_gl_memory(buffer) { let n_mem = match buffer_n_gl_memory(buffer) {
Some(n) => n, Some(n) => n,
None => return None, None => return Err(glib_bool_error!("Memory is not a GstGLMemory")),
}; };
// FIXME: planes are not memories, in multiview use case, // FIXME: planes are not memories, in multiview use case,
// number of memories = planes * views, but the raw memory is // number of memories = planes * views, but the raw memory is
// not exposed in videoframe // not exposed in videoframe
if n_mem != info.n_planes() { if n_mem != info.n_planes() {
return None; return Err(glib_bool_error!(
"Number of planes and memories is not matching"
));
} }
unsafe { unsafe {
@ -122,9 +124,11 @@ impl<'a> VideoFrameGLExt for VideoFrameRef<&'a gst::BufferRef> {
)); ));
if !res { if !res {
None Err(glib_bool_error!(
"Failed to fill in the values of GstVideoFrame"
))
} else { } else {
Some(VideoFrameRef::from_glib_borrow(&frame.assume_init())) Ok(VideoFrameRef::from_glib_borrow(&frame.assume_init()))
} }
} }
} }

View file

@ -56,29 +56,44 @@ pub fn pb_utils_add_codec_description_to_tag_list(
} }
} }
pub fn pb_utils_get_encoder_description(caps: &gst::CapsRef) -> Option<String> { pub fn pb_utils_get_encoder_description(
caps: &gst::CapsRef,
) -> Result<glib::GString, glib::error::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
from_glib_full(gst_pbutils_sys::gst_pb_utils_get_encoder_description( match from_glib_full(gst_pbutils_sys::gst_pb_utils_get_encoder_description(
caps.as_ptr(), caps.as_ptr(),
)) )) {
Some(s) => Ok(s),
None => Err(glib_bool_error!("Failed to get encoder description")),
}
} }
} }
pub fn pb_utils_get_decoder_description(caps: &gst::CapsRef) -> Option<String> { pub fn pb_utils_get_decoder_description(
caps: &gst::CapsRef,
) -> Result<glib::GString, glib::error::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
from_glib_full(gst_pbutils_sys::gst_pb_utils_get_decoder_description( match from_glib_full(gst_pbutils_sys::gst_pb_utils_get_decoder_description(
caps.as_ptr(), caps.as_ptr(),
)) )) {
Some(s) => Ok(s),
None => Err(glib_bool_error!("Failed to get decoder description")),
}
} }
} }
pub fn pb_utils_get_codec_description(caps: &gst::CapsRef) -> Option<String> { pub fn pb_utils_get_codec_description(
caps: &gst::CapsRef,
) -> Result<glib::GString, glib::error::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
from_glib_full(gst_pbutils_sys::gst_pb_utils_get_codec_description( match from_glib_full(gst_pbutils_sys::gst_pb_utils_get_codec_description(
caps.as_ptr(), caps.as_ptr(),
)) )) {
Some(s) => Ok(s),
None => Err(glib_bool_error!("Failed to get codec description")),
}
} }
} }

View file

@ -120,8 +120,8 @@ impl fmt::Debug for SDPMediaRef {
impl fmt::Display for SDPMediaRef { impl fmt::Display for SDPMediaRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.as_text() { match self.as_text() {
Some(text) => f.write_str(text.as_str()), Ok(text) => f.write_str(text.as_str()),
None => Err(fmt::Error), Err(_) => Err(fmt::Error),
} }
} }
} }
@ -171,8 +171,15 @@ impl SDPMediaRef {
unsafe { gst_sdp_sys::gst_sdp_media_add_format(&mut self.0, format.to_glib_none().0) }; unsafe { gst_sdp_sys::gst_sdp_media_add_format(&mut self.0, format.to_glib_none().0) };
} }
pub fn as_text(&self) -> Option<String> { pub fn as_text(&self) -> Result<String, glib::error::BoolError> {
unsafe { from_glib_full(gst_sdp_sys::gst_sdp_media_as_text(&self.0)) } unsafe {
match from_glib_full(gst_sdp_sys::gst_sdp_media_as_text(&self.0)) {
Some(s) => Ok(s),
None => Err(glib_bool_error!(
"Failed to convert the contents of media to a text string"
)),
}
}
} }
pub fn attributes(&self) -> AttributesIter { pub fn attributes(&self) -> AttributesIter {

View file

@ -159,8 +159,8 @@ impl fmt::Debug for SDPMessageRef {
impl fmt::Display for SDPMessageRef { impl fmt::Display for SDPMessageRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.as_text() { match self.as_text() {
Some(text) => f.write_str(text.as_str()), Ok(text) => f.write_str(text.as_str()),
None => Err(fmt::Error), Err(_) => Err(fmt::Error),
} }
} }
} }
@ -217,8 +217,15 @@ impl SDPMessageRef {
}; };
} }
pub fn as_text(&self) -> Option<String> { pub fn as_text(&self) -> Result<String, glib::error::BoolError> {
unsafe { from_glib_full(gst_sdp_sys::gst_sdp_message_as_text(&self.0)) } unsafe {
match from_glib_full(gst_sdp_sys::gst_sdp_message_as_text(&self.0)) {
Some(s) => Ok(s),
None => Err(glib_bool_error!(
"Failed to convert the contents of message to a text string"
)),
}
}
} }
pub fn attributes_len(&self) -> u32 { pub fn attributes_len(&self) -> u32 {
@ -851,13 +858,16 @@ impl SDPMessageRef {
unsafe { gst_sdp_sys::gst_sdp_message_zones_len(&self.0) } unsafe { gst_sdp_sys::gst_sdp_message_zones_len(&self.0) }
} }
pub fn as_uri(&self, scheme: &str) -> Option<String> { pub fn as_uri(&self, scheme: &str) -> Result<String, glib::error::BoolError> {
assert_initialized_main_thread!(); assert_initialized_main_thread!();
unsafe { unsafe {
from_glib_full(gst_sdp_sys::gst_sdp_message_as_uri( match from_glib_full(gst_sdp_sys::gst_sdp_message_as_uri(
scheme.to_glib_none().0, scheme.to_glib_none().0,
&self.0, &self.0,
)) )) {
Some(s) => Ok(s),
None => Err(glib_bool_error!("Failed to create an URI from message")),
}
} }
} }
@ -1084,7 +1094,7 @@ define_iter!(
define_iter_mut!( define_iter_mut!(
MediasIterMut, MediasIterMut,
&'a mut SDPMediaRef, &'a mut SDPMediaRef,
|message: &'a mut SDPMessageRef, idx| { message.get_media_mut(idx) }, |message: &'a mut SDPMessageRef, idx| message.get_media_mut(idx),
|message: &mut SDPMessageRef| message.medias_len() |message: &mut SDPMessageRef| message.medias_len()
); );
define_iter!( define_iter!(

View file

@ -28,12 +28,12 @@ fn print_stream_info(info: &DiscovererStreamInfo, depth: usize) {
let caps_str = if let Some(caps) = info.get_caps() { let caps_str = if let Some(caps) = info.get_caps() {
if caps.is_fixed() { if caps.is_fixed() {
gst_pbutils::pb_utils_get_codec_description(&caps) gst_pbutils::pb_utils_get_codec_description(&caps)
.unwrap_or_else(|| String::from("unknown codec")) .unwrap_or_else(|_| glib::GString::from("unknown codec"))
} else { } else {
caps.to_string() glib::GString::from(caps.to_string())
} }
} else { } else {
String::new() glib::GString::from("")
}; };
let stream_nick = info.get_stream_type_nick(); let stream_nick = info.get_stream_type_nick();