From d3c77f400de9112041d844ceb901d227837f809d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 22 Jan 2020 19:08:27 +0200 Subject: [PATCH] Don't derive Debug impls for generic types where the type parameters don't have to impl Debug themselves This allows to use MappedBuffer and similar types to be properly debug-printed. Also change VideoFrame/VideoFrameRef/RTPBuffer from a tuple struct to a struct with proper field names to make the code easier to understand. --- gstreamer-rtp/src/rtp_buffer.rs | 44 ++++-- gstreamer-video/src/video_frame.rs | 207 +++++++++++++++++++++-------- gstreamer/src/buffer.rs | 12 +- gstreamer/src/caps.rs | 22 ++- gstreamer/src/meta.rs | 21 ++- 5 files changed, 233 insertions(+), 73 deletions(-) diff --git a/gstreamer-rtp/src/rtp_buffer.rs b/gstreamer-rtp/src/rtp_buffer.rs index 9b0415e44..ffcfa7360 100644 --- a/gstreamer-rtp/src/rtp_buffer.rs +++ b/gstreamer-rtp/src/rtp_buffer.rs @@ -1,4 +1,5 @@ use glib::translate::{from_glib, FromGlibPtrFull}; +use std::fmt; use std::marker::PhantomData; use std::mem; @@ -8,12 +9,25 @@ use gst_rtp_sys; pub enum Readable {} pub enum Writable {} -#[repr(C)] -pub struct RTPBuffer<'a, T>(gst_rtp_sys::GstRTPBuffer, &'a gst::Buffer, PhantomData); +pub struct RTPBuffer<'a, T> { + rtp_buffer: gst_rtp_sys::GstRTPBuffer, + buffer: &'a gst::Buffer, + phantom: PhantomData, +} unsafe impl<'a, T> Send for RTPBuffer<'a, T> {} unsafe impl<'a, T> Sync for RTPBuffer<'a, T> {} +impl<'a, T> fmt::Debug for RTPBuffer<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("RTPBuffer") + .field("rtp_buffer", &self.rtp_buffer) + .field("buffer", &self.buffer) + .field("phantom", &self.phantom) + .finish() + } +} + impl<'a> RTPBuffer<'a, Readable> { pub fn from_buffer_readable( buffer: &gst::Buffer, @@ -27,7 +41,11 @@ impl<'a> RTPBuffer<'a, Readable> { )); if res { - Ok(RTPBuffer(rtp_buffer.assume_init(), buffer, PhantomData)) + Ok(RTPBuffer { + rtp_buffer: rtp_buffer.assume_init(), + buffer, + phantom: PhantomData, + }) } else { Err(glib_bool_error!("Failed to map RTP buffer readable")) } @@ -48,7 +66,11 @@ impl<'a> RTPBuffer<'a, Writable> { )); if res { - Ok(RTPBuffer(rtp_buffer.assume_init(), buffer, PhantomData)) + Ok(RTPBuffer { + rtp_buffer: rtp_buffer.assume_init(), + buffer, + phantom: PhantomData, + }) } else { Err(glib_bool_error!("Failed to map RTP buffer writable")) } @@ -57,41 +79,41 @@ impl<'a> RTPBuffer<'a, Writable> { pub fn set_seq(&mut self, seq: u16) { unsafe { - gst_rtp_sys::gst_rtp_buffer_set_seq(&mut self.0, seq); + gst_rtp_sys::gst_rtp_buffer_set_seq(&mut self.rtp_buffer, seq); } } pub fn set_payload_type(&mut self, pt: u8) { unsafe { - gst_rtp_sys::gst_rtp_buffer_set_payload_type(&mut self.0, pt); + gst_rtp_sys::gst_rtp_buffer_set_payload_type(&mut self.rtp_buffer, pt); } } pub fn set_timestamp(&mut self, rtptime: u32) { unsafe { - gst_rtp_sys::gst_rtp_buffer_set_timestamp(&mut self.0, rtptime); + gst_rtp_sys::gst_rtp_buffer_set_timestamp(&mut self.rtp_buffer, rtptime); } } } impl<'a, T> RTPBuffer<'a, T> { pub fn get_seq(&mut self) -> u16 { - unsafe { gst_rtp_sys::gst_rtp_buffer_get_seq(&mut self.0) } + unsafe { gst_rtp_sys::gst_rtp_buffer_get_seq(&mut self.rtp_buffer) } } pub fn get_payload_type(&mut self) -> u8 { - unsafe { gst_rtp_sys::gst_rtp_buffer_get_payload_type(&mut self.0) } + unsafe { gst_rtp_sys::gst_rtp_buffer_get_payload_type(&mut self.rtp_buffer) } } pub fn get_timestamp(&mut self) -> u32 { - unsafe { gst_rtp_sys::gst_rtp_buffer_get_timestamp(&mut self.0) } + unsafe { gst_rtp_sys::gst_rtp_buffer_get_timestamp(&mut self.rtp_buffer) } } } impl<'a, T> Drop for RTPBuffer<'a, T> { fn drop(&mut self) { unsafe { - gst_rtp_sys::gst_rtp_buffer_unmap(&mut self.0); + gst_rtp_sys::gst_rtp_buffer_unmap(&mut self.rtp_buffer); } } } diff --git a/gstreamer-video/src/video_frame.rs b/gstreamer-video/src/video_frame.rs index cd3fe5bdc..f0a7fe092 100644 --- a/gstreamer-video/src/video_frame.rs +++ b/gstreamer-video/src/video_frame.rs @@ -14,6 +14,7 @@ use glib::translate::{from_glib, ToGlibPtr}; use gst; use gst::miniobject::MiniObject; +use std::fmt; use std::marker::PhantomData; use std::mem; use std::ops; @@ -23,37 +24,50 @@ use std::slice; pub enum Readable {} pub enum Writable {} -#[derive(Debug)] -pub struct VideoFrame( - gst_video_sys::GstVideoFrame, - Option, - ::VideoInfo, - PhantomData, -); +pub struct VideoFrame { + frame: gst_video_sys::GstVideoFrame, + buffer: Option, + info: ::VideoInfo, + phantom: PhantomData, +} unsafe impl Send for VideoFrame {} unsafe impl Sync for VideoFrame {} +impl fmt::Debug for VideoFrame { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("VideoFrame") + .field("frame", &self.frame) + .field("buffer", &self.buffer) + .field("info", &self.info) + .field("phantom", &self.phantom) + .finish() + } +} + impl VideoFrame { pub fn info(&self) -> &::VideoInfo { - &self.2 + &self.info } pub fn flags(&self) -> ::VideoFrameFlags { - from_glib(self.0.flags) + from_glib(self.frame.flags) } pub fn id(&self) -> i32 { - self.0.id + self.frame.id } pub fn into_buffer(mut self) -> gst::Buffer { - self.1.take().unwrap() + self.buffer.take().unwrap() } pub fn copy(&self, dest: &mut VideoFrame) -> Result<(), glib::BoolError> { unsafe { - let res: bool = from_glib(gst_video_sys::gst_video_frame_copy(&mut dest.0, &self.0)); + let res: bool = from_glib(gst_video_sys::gst_video_frame_copy( + &mut dest.frame, + &self.frame, + )); if res { Ok(()) } else { @@ -71,8 +85,8 @@ impl VideoFrame { unsafe { let res: bool = from_glib(gst_video_sys::gst_video_frame_copy_plane( - &mut dest.0, - &self.0, + &mut dest.frame, + &self.frame, plane, )); if res { @@ -136,7 +150,7 @@ impl VideoFrame { } pub fn buffer(&self) -> &gst::BufferRef { - unsafe { gst::BufferRef::from_ptr(self.0.buffer) } + unsafe { gst::BufferRef::from_ptr(self.frame.buffer) } } pub fn plane_data(&self, plane: u32) -> Result<&[u8], glib::BoolError> { @@ -149,7 +163,10 @@ impl VideoFrame { // Just get the palette if format_info.has_palette() && plane == 1 { unsafe { - return Ok(slice::from_raw_parts(self.0.data[1] as *const u8, 256 * 4)); + return Ok(slice::from_raw_parts( + self.frame.data[1] as *const u8, + 256 * 4, + )); } } @@ -160,7 +177,7 @@ impl VideoFrame { unsafe { Ok(slice::from_raw_parts( - self.0.data[plane as usize] as *const u8, + self.frame.data[plane as usize] as *const u8, (w * h) as usize, )) } @@ -169,14 +186,19 @@ impl VideoFrame { pub unsafe fn from_glib_full(frame: gst_video_sys::GstVideoFrame) -> Self { let info = ::VideoInfo(ptr::read(&frame.info)); let buffer = gst::Buffer::from_glib_none(frame.buffer); - VideoFrame(frame, Some(buffer), info, PhantomData) + VideoFrame { + frame, + buffer: Some(buffer), + info, + phantom: PhantomData, + } } } impl Drop for VideoFrame { fn drop(&mut self) { unsafe { - gst_video_sys::gst_video_frame_unmap(&mut self.0); + gst_video_sys::gst_video_frame_unmap(&mut self.frame); } } } @@ -202,7 +224,12 @@ impl VideoFrame { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrame(frame, Some(buffer), info, PhantomData)) + Ok(VideoFrame { + frame, + buffer: Some(buffer), + info, + phantom: PhantomData, + }) } } } @@ -229,19 +256,29 @@ impl VideoFrame { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrame(frame, Some(buffer), info, PhantomData)) + Ok(VideoFrame { + frame, + buffer: Some(buffer), + info, + phantom: PhantomData, + }) } } } pub fn as_video_frame_ref(&self) -> VideoFrameRef<&gst::BufferRef> { - let vframe = unsafe { ptr::read(&self.0) }; - let info = self.2.clone(); - VideoFrameRef(vframe, Some(self.buffer()), info, true) + let frame = unsafe { ptr::read(&self.frame) }; + let info = self.info.clone(); + VideoFrameRef { + frame, + buffer: Some(self.buffer()), + info, + borrowed: true, + } } pub fn as_ptr(&self) -> *const gst_video_sys::GstVideoFrame { - &self.0 + &self.frame } } @@ -268,7 +305,12 @@ impl VideoFrame { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrame(frame, Some(buffer), info, PhantomData)) + Ok(VideoFrame { + frame, + buffer: Some(buffer), + info, + phantom: PhantomData, + }) } } } @@ -297,13 +339,18 @@ impl VideoFrame { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrame(frame, Some(buffer), info, PhantomData)) + Ok(VideoFrame { + frame, + buffer: Some(buffer), + info, + phantom: PhantomData, + }) } } } pub fn buffer_mut(&mut self) -> &mut gst::BufferRef { - unsafe { gst::BufferRef::from_mut_ptr(self.0.buffer) } + unsafe { gst::BufferRef::from_mut_ptr(self.frame.buffer) } } pub fn plane_data_mut(&mut self, plane: u32) -> Result<&mut [u8], glib::BoolError> { @@ -317,7 +364,7 @@ impl VideoFrame { if format_info.has_palette() && plane == 1 { unsafe { return Ok(slice::from_raw_parts_mut( - self.0.data[1] as *mut u8, + self.frame.data[1] as *mut u8, 256 * 4, )); } @@ -330,29 +377,39 @@ impl VideoFrame { unsafe { Ok(slice::from_raw_parts_mut( - self.0.data[plane as usize] as *mut u8, + self.frame.data[plane as usize] as *mut u8, (w * h) as usize, )) } } pub fn as_mut_video_frame_ref(&mut self) -> VideoFrameRef<&mut gst::BufferRef> { - let vframe = unsafe { ptr::read(&self.0) }; - let info = self.2.clone(); - VideoFrameRef(vframe, Some(self.buffer_mut()), info, true) + let frame = unsafe { ptr::read(&self.frame) }; + let info = self.info.clone(); + VideoFrameRef { + frame, + buffer: Some(self.buffer_mut()), + info, + borrowed: true, + } } pub fn as_mut_ptr(&mut self) -> *mut gst_video_sys::GstVideoFrame { - &mut self.0 + &mut self.frame } } #[derive(Debug)] -pub struct VideoFrameRef(gst_video_sys::GstVideoFrame, Option, ::VideoInfo, bool); +pub struct VideoFrameRef { + frame: gst_video_sys::GstVideoFrame, + buffer: Option, + info: ::VideoInfo, + borrowed: bool, +} impl<'a> VideoFrameRef<&'a gst::BufferRef> { pub fn as_ptr(&self) -> *const gst_video_sys::GstVideoFrame { - &self.0 + &self.frame } pub unsafe fn from_glib_borrow(frame: *const gst_video_sys::GstVideoFrame) -> Self { @@ -361,7 +418,12 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { let frame = ptr::read(frame); let info = ::VideoInfo(ptr::read(&frame.info)); let buffer = gst::BufferRef::from_ptr(frame.buffer); - VideoFrameRef(frame, Some(buffer), info, false) + VideoFrameRef { + frame, + buffer: Some(buffer), + info, + borrowed: false, + } } pub fn from_buffer_ref_readable<'b>( @@ -384,7 +446,12 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrameRef(frame, Some(buffer), info, false)) + Ok(VideoFrameRef { + frame, + buffer: Some(buffer), + info, + borrowed: false, + }) } } } @@ -411,21 +478,26 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrameRef(frame, Some(buffer), info, false)) + Ok(VideoFrameRef { + frame, + buffer: Some(buffer), + info, + borrowed: false, + }) } } } pub fn info(&self) -> &::VideoInfo { - &self.2 + &self.info } pub fn flags(&self) -> ::VideoFrameFlags { - from_glib(self.0.flags) + from_glib(self.frame.flags) } pub fn id(&self) -> i32 { - self.0.id + self.frame.id } pub fn copy( @@ -433,7 +505,10 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { dest: &mut VideoFrameRef<&mut gst::BufferRef>, ) -> Result<(), glib::BoolError> { unsafe { - let res: bool = from_glib(gst_video_sys::gst_video_frame_copy(&mut dest.0, &self.0)); + let res: bool = from_glib(gst_video_sys::gst_video_frame_copy( + &mut dest.frame, + &self.frame, + )); if res { Ok(()) } else { @@ -451,8 +526,8 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { unsafe { let res: bool = from_glib(gst_video_sys::gst_video_frame_copy_plane( - &mut dest.0, - &self.0, + &mut dest.frame, + &self.frame, plane, )); if res { @@ -516,7 +591,7 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { } pub fn buffer(&self) -> &gst::BufferRef { - self.1.as_ref().unwrap() + self.buffer.as_ref().unwrap() } pub fn plane_data(&self, plane: u32) -> Result<&[u8], glib::BoolError> { @@ -529,7 +604,10 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { // Just get the palette if format_info.has_palette() && plane == 1 { unsafe { - return Ok(slice::from_raw_parts(self.0.data[1] as *const u8, 256 * 4)); + return Ok(slice::from_raw_parts( + self.frame.data[1] as *const u8, + 256 * 4, + )); } } @@ -540,7 +618,7 @@ impl<'a> VideoFrameRef<&'a gst::BufferRef> { unsafe { Ok(slice::from_raw_parts( - self.0.data[plane as usize] as *const u8, + self.frame.data[plane as usize] as *const u8, (w * h) as usize, )) } @@ -554,7 +632,12 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { let frame = ptr::read(frame); let info = ::VideoInfo(ptr::read(&frame.info)); let buffer = gst::BufferRef::from_mut_ptr(frame.buffer); - VideoFrameRef(frame, Some(buffer), info, false) + VideoFrameRef { + frame, + buffer: Some(buffer), + info, + borrowed: false, + } } pub fn from_buffer_ref_writable<'b>( @@ -579,7 +662,12 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrameRef(frame, Some(buffer), info, false)) + Ok(VideoFrameRef { + frame, + buffer: Some(buffer), + info, + borrowed: false, + }) } } } @@ -608,13 +696,18 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { } else { let frame = frame.assume_init(); let info = ::VideoInfo(ptr::read(&frame.info)); - Ok(VideoFrameRef(frame, Some(buffer), info, false)) + Ok(VideoFrameRef { + frame, + buffer: Some(buffer), + info, + borrowed: false, + }) } } } pub fn buffer_mut(&mut self) -> &mut gst::BufferRef { - self.1.as_mut().unwrap() + self.buffer.as_mut().unwrap() } pub fn plane_data_mut(&mut self, plane: u32) -> Result<&mut [u8], glib::BoolError> { @@ -628,7 +721,7 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { if format_info.has_palette() && plane == 1 { unsafe { return Ok(slice::from_raw_parts_mut( - self.0.data[1] as *mut u8, + self.frame.data[1] as *mut u8, 256 * 4, )); } @@ -641,14 +734,14 @@ impl<'a> VideoFrameRef<&'a mut gst::BufferRef> { unsafe { Ok(slice::from_raw_parts_mut( - self.0.data[plane as usize] as *mut u8, + self.frame.data[plane as usize] as *mut u8, (w * h) as usize, )) } } pub fn as_mut_ptr(&mut self) -> *mut gst_video_sys::GstVideoFrame { - &mut self.0 + &mut self.frame } } @@ -668,9 +761,9 @@ unsafe impl Sync for VideoFrameRef {} impl Drop for VideoFrameRef { fn drop(&mut self) { - if !self.3 { + if !self.borrowed { unsafe { - gst_video_sys::gst_video_frame_unmap(&mut self.0); + gst_video_sys::gst_video_frame_unmap(&mut self.frame); } } } diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index b3c09a15e..759315487 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -596,7 +596,6 @@ impl BufferRef { macro_rules! define_iter( ($name:ident, $typ:ty, $mtyp:ty, $prepare_buffer:expr, $from_ptr:expr) => { - #[derive(Debug)] pub struct $name<'a, T: MetaAPI + 'a> { buffer: $typ, state: glib_sys::gpointer, @@ -607,6 +606,17 @@ macro_rules! define_iter( unsafe impl<'a, T: MetaAPI> Send for $name<'a, T> { } unsafe impl<'a, T: MetaAPI> Sync for $name<'a, T> { } + impl<'a, T: MetaAPI> fmt::Debug for $name<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct(stringify!($name)) + .field("buffer", &self.buffer) + .field("state", &self.state) + .field("meta_api", &self.meta_api) + .field("items", &self.items) + .finish() + } + } + impl<'a, T: MetaAPI> $name<'a, T> { fn new(buffer: $typ) -> $name<'a, T> { skip_assert_initialized!(); diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index db08cf514..a50640cf4 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -536,13 +536,22 @@ impl Eq for CapsRef {} pub enum NoFeature {} pub enum HasFeatures {} -#[derive(Debug)] pub struct Builder { s: ::Structure, features: Option, phantom: PhantomData, } +impl fmt::Debug for Builder { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Builder") + .field("s", &self.s) + .field("features", &self.features) + .field("phantom", &self.phantom) + .finish() + } +} + impl Builder { fn new(name: &str) -> Builder { Builder { @@ -588,13 +597,22 @@ impl Builder { pub enum AnyFeatures {} pub enum SomeFeatures {} -#[derive(Debug)] pub struct BuilderFull { caps: ::Caps, features: Option, phantom: PhantomData, } +impl fmt::Debug for BuilderFull { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Builder") + .field("caps", &self.caps) + .field("features", &self.features) + .field("phantom", &self.phantom) + .finish() + } +} + impl BuilderFull { fn new() -> Self { BuilderFull { diff --git a/gstreamer/src/meta.rs b/gstreamer/src/meta.rs index 1cebfee29..97faf6102 100644 --- a/gstreamer/src/meta.rs +++ b/gstreamer/src/meta.rs @@ -71,7 +71,6 @@ pub unsafe trait MetaAPI: Sync + Send + Sized { } } -#[derive(Debug)] pub struct MetaRef<'a, T: MetaAPI + 'a> { meta: &'a T, buffer: &'a BufferRef, @@ -80,13 +79,31 @@ pub struct MetaRef<'a, T: MetaAPI + 'a> { pub enum Standalone {} pub enum Iterated {} -#[derive(Debug)] pub struct MetaRefMut<'a, T: MetaAPI + 'a, U> { meta: &'a mut T, buffer: &'a mut BufferRef, mode: PhantomData, } +impl<'a, T: MetaAPI + fmt::Debug + 'a> fmt::Debug for MetaRef<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("MetaRef") + .field("meta", &self.meta) + .field("buffer", &self.buffer) + .finish() + } +} + +impl<'a, T: MetaAPI + fmt::Debug + 'a, U> fmt::Debug for MetaRefMut<'a, T, U> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("MetaRef") + .field("meta", &self.meta) + .field("buffer", &self.buffer) + .field("mode", &self.mode) + .finish() + } +} + impl<'a, T: MetaAPI> ops::Deref for MetaRef<'a, T> { type Target = T;