From c3b7f0f3532e14c3af836bcfbe52988e4e426985 Mon Sep 17 00:00:00 2001 From: Tony Jinwoo Ahn Date: Tue, 17 Dec 2019 08:20:47 +0000 Subject: [PATCH] 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 --- gstreamer-gl/src/gl_video_frame.rs | 18 +++++++++------ gstreamer-pbutils/src/functions.rs | 33 +++++++++++++++++++-------- gstreamer-sdp/src/sdp_media.rs | 15 ++++++++---- gstreamer-sdp/src/sdp_message.rs | 26 ++++++++++++++------- tutorials/src/bin/basic-tutorial-9.rs | 6 ++--- 5 files changed, 67 insertions(+), 31 deletions(-) diff --git a/gstreamer-gl/src/gl_video_frame.rs b/gstreamer-gl/src/gl_video_frame.rs index 4f48fc67f..08085b8c1 100644 --- a/gstreamer-gl/src/gl_video_frame.rs +++ b/gstreamer-gl/src/gl_video_frame.rs @@ -29,7 +29,7 @@ pub trait VideoFrameGLExt { fn from_buffer_ref_readable_gl<'a, 'b>( buffer: &'a gst::BufferRef, info: &'b VideoInfo, - ) -> Option>; + ) -> Result, glib::error::BoolError>; fn get_texture_id(&self, idx: u32) -> Option; } @@ -45,7 +45,7 @@ impl VideoFrameGLExt for VideoFrame { fn from_buffer_ref_readable_gl<'a, 'b>( buffer: &'a gst::BufferRef, info: &'b VideoInfo, - ) -> Option> { + ) -> Result, glib::error::BoolError> { 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>( buffer: &'b gst::BufferRef, info: &'c VideoInfo, - ) -> Option> { + ) -> Result, glib::error::BoolError> { skip_assert_initialized!(); let n_mem = match buffer_n_gl_memory(buffer) { 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, // number of memories = planes * views, but the raw memory is // not exposed in videoframe if n_mem != info.n_planes() { - return None; + return Err(glib_bool_error!( + "Number of planes and memories is not matching" + )); } unsafe { @@ -122,9 +124,11 @@ impl<'a> VideoFrameGLExt for VideoFrameRef<&'a gst::BufferRef> { )); if !res { - None + Err(glib_bool_error!( + "Failed to fill in the values of GstVideoFrame" + )) } else { - Some(VideoFrameRef::from_glib_borrow(&frame.assume_init())) + Ok(VideoFrameRef::from_glib_borrow(&frame.assume_init())) } } } diff --git a/gstreamer-pbutils/src/functions.rs b/gstreamer-pbutils/src/functions.rs index 303801060..26ccea182 100644 --- a/gstreamer-pbutils/src/functions.rs +++ b/gstreamer-pbutils/src/functions.rs @@ -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 { +pub fn pb_utils_get_encoder_description( + caps: &gst::CapsRef, +) -> Result { assert_initialized_main_thread!(); 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(), - )) + )) { + 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 { +pub fn pb_utils_get_decoder_description( + caps: &gst::CapsRef, +) -> Result { assert_initialized_main_thread!(); 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(), - )) + )) { + 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 { +pub fn pb_utils_get_codec_description( + caps: &gst::CapsRef, +) -> Result { assert_initialized_main_thread!(); 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(), - )) + )) { + Some(s) => Ok(s), + None => Err(glib_bool_error!("Failed to get codec description")), + } } } diff --git a/gstreamer-sdp/src/sdp_media.rs b/gstreamer-sdp/src/sdp_media.rs index 1c21617ce..3a08e36b6 100644 --- a/gstreamer-sdp/src/sdp_media.rs +++ b/gstreamer-sdp/src/sdp_media.rs @@ -120,8 +120,8 @@ impl fmt::Debug for SDPMediaRef { 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), + Ok(text) => f.write_str(text.as_str()), + 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) }; } - pub fn as_text(&self) -> Option { - unsafe { from_glib_full(gst_sdp_sys::gst_sdp_media_as_text(&self.0)) } + pub fn as_text(&self) -> Result { + 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 { diff --git a/gstreamer-sdp/src/sdp_message.rs b/gstreamer-sdp/src/sdp_message.rs index 8da9b84f6..5064d8072 100644 --- a/gstreamer-sdp/src/sdp_message.rs +++ b/gstreamer-sdp/src/sdp_message.rs @@ -159,8 +159,8 @@ impl fmt::Debug for SDPMessageRef { impl fmt::Display for SDPMessageRef { 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), + Ok(text) => f.write_str(text.as_str()), + Err(_) => Err(fmt::Error), } } } @@ -217,8 +217,15 @@ impl SDPMessageRef { }; } - pub fn as_text(&self) -> Option { - unsafe { from_glib_full(gst_sdp_sys::gst_sdp_message_as_text(&self.0)) } + pub fn as_text(&self) -> Result { + 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 { @@ -851,13 +858,16 @@ impl SDPMessageRef { unsafe { gst_sdp_sys::gst_sdp_message_zones_len(&self.0) } } - pub fn as_uri(&self, scheme: &str) -> Option { + pub fn as_uri(&self, scheme: &str) -> Result { assert_initialized_main_thread!(); 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, &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!( MediasIterMut, &'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() ); define_iter!( diff --git a/tutorials/src/bin/basic-tutorial-9.rs b/tutorials/src/bin/basic-tutorial-9.rs index 7fedd6f3c..a1a3306ba 100644 --- a/tutorials/src/bin/basic-tutorial-9.rs +++ b/tutorials/src/bin/basic-tutorial-9.rs @@ -28,12 +28,12 @@ fn print_stream_info(info: &DiscovererStreamInfo, depth: usize) { let caps_str = if let Some(caps) = info.get_caps() { if caps.is_fixed() { gst_pbutils::pb_utils_get_codec_description(&caps) - .unwrap_or_else(|| String::from("unknown codec")) + .unwrap_or_else(|_| glib::GString::from("unknown codec")) } else { - caps.to_string() + glib::GString::from(caps.to_string()) } } else { - String::new() + glib::GString::from("") }; let stream_nick = info.get_stream_type_nick();