Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/silence various clippy warnings #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions src/ndi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::ndisys;
use crate::ndisys::*;
use std::ffi;
use std::fmt;
use std::mem;
use std::ptr;

Expand Down Expand Up @@ -226,11 +227,11 @@ impl<'a> RecvBuilder<'a> {
p_ndi_name: ndi_name
.as_ref()
.map(|s| s.as_ptr())
.unwrap_or_else(|| ptr::null_mut()),
.unwrap_or(ptr::null_mut()),
p_url_address: url_address
.as_ref()
.map(|s| s.as_ptr())
.unwrap_or_else(|| ptr::null_mut()),
.unwrap_or(ptr::null_mut()),
},
allow_video_fields: self.allow_video_fields,
bandwidth: self.bandwidth,
Expand All @@ -252,6 +253,17 @@ pub struct RecvInstance(ptr::NonNull<::std::os::raw::c_void>);

unsafe impl Send for RecvInstance {}

#[derive(Debug, Copy, Clone)]
pub struct ReceiveError;

impl fmt::Display for ReceiveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Receive error")
}
}

impl std::error::Error for ReceiveError {}

impl RecvInstance {
pub fn builder<'a>(
ndi_name: Option<&'a str>,
Expand Down Expand Up @@ -284,7 +296,7 @@ impl RecvInstance {
}
}

pub fn capture(&self, timeout_in_ms: u32) -> Result<Option<Frame>, ()> {
pub fn capture(&self, timeout_in_ms: u32) -> Result<Option<Frame>, ReceiveError> {
unsafe {
let ptr = self.0.as_ptr();

Expand All @@ -310,7 +322,7 @@ impl RecvInstance {
NDIlib_frame_type_e::NDIlib_frame_type_metadata => Ok(Some(Frame::Metadata(
MetadataFrame::Borrowed(metadata_frame, self),
))),
NDIlib_frame_type_e::NDIlib_frame_type_error => Err(()),
NDIlib_frame_type_e::NDIlib_frame_type_error => Err(ReceiveError),
_ => Ok(None),
}
}
Expand Down Expand Up @@ -444,6 +456,17 @@ pub enum VideoFrame<'a> {
),
}

#[derive(Debug, Copy, Clone)]
pub struct TryFromVideoFrameError;

impl fmt::Display for TryFromVideoFrameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Can't handle video frame")
}
}

impl std::error::Error for TryFromVideoFrameError {}

impl<'a> VideoFrame<'a> {
pub fn xres(&self) -> i32 {
match self {
Expand Down Expand Up @@ -702,7 +725,7 @@ impl<'a> VideoFrame<'a> {
pub fn try_from_video_frame(
frame: &'a gst_video::VideoFrameRef<&'a gst::BufferRef>,
timecode: i64,
) -> Result<Self, ()> {
) -> Result<Self, TryFromVideoFrameError> {
// Planar formats must be in contiguous memory
let format = match frame.format() {
gst_video::VideoFormat::Uyvy => ndisys::NDIlib_FourCC_video_type_UYVY,
Expand All @@ -711,14 +734,14 @@ impl<'a> VideoFrame<'a> {
.checked_sub(frame.plane_data(0).unwrap().as_ptr() as usize)
!= Some(frame.height() as usize * frame.plane_stride()[0] as usize)
{
return Err(());
return Err(TryFromVideoFrameError);
}

if (frame.plane_data(2).unwrap().as_ptr() as usize)
.checked_sub(frame.plane_data(1).unwrap().as_ptr() as usize)
!= Some((frame.height() as usize + 1) / 2 * frame.plane_stride()[1] as usize)
{
return Err(());
return Err(TryFromVideoFrameError);
}

ndisys::NDIlib_FourCC_video_type_I420
Expand All @@ -728,7 +751,7 @@ impl<'a> VideoFrame<'a> {
.checked_sub(frame.plane_data(0).unwrap().as_ptr() as usize)
!= Some(frame.height() as usize * frame.plane_stride()[0] as usize)
{
return Err(());
return Err(TryFromVideoFrameError);
}

ndisys::NDIlib_FourCC_video_type_NV12
Expand All @@ -738,7 +761,7 @@ impl<'a> VideoFrame<'a> {
.checked_sub(frame.plane_data(0).unwrap().as_ptr() as usize)
!= Some(frame.height() as usize * frame.plane_stride()[0] as usize)
{
return Err(());
return Err(TryFromVideoFrameError);
}

ndisys::NDIlib_FourCC_video_type_NV12
Expand All @@ -748,14 +771,14 @@ impl<'a> VideoFrame<'a> {
.checked_sub(frame.plane_data(0).unwrap().as_ptr() as usize)
!= Some(frame.height() as usize * frame.plane_stride()[0] as usize)
{
return Err(());
return Err(TryFromVideoFrameError);
}

if (frame.plane_data(2).unwrap().as_ptr() as usize)
.checked_sub(frame.plane_data(1).unwrap().as_ptr() as usize)
!= Some((frame.height() as usize + 1) / 2 * frame.plane_stride()[1] as usize)
{
return Err(());
return Err(TryFromVideoFrameError);
}

ndisys::NDIlib_FourCC_video_type_YV12
Expand All @@ -764,7 +787,7 @@ impl<'a> VideoFrame<'a> {
gst_video::VideoFormat::Bgrx => ndisys::NDIlib_FourCC_video_type_BGRX,
gst_video::VideoFormat::Rgba => ndisys::NDIlib_FourCC_video_type_RGBA,
gst_video::VideoFormat::Rgbx => ndisys::NDIlib_FourCC_video_type_RGBX,
_ => return Err(()),
_ => return Err(TryFromVideoFrameError),
};

let frame_format_type = match frame.info().interlace_mode() {
Expand All @@ -787,7 +810,7 @@ impl<'a> VideoFrame<'a> {
{
NDIlib_frame_format_type_e::NDIlib_frame_format_type_field_1
}
_ => return Err(()),
_ => return Err(TryFromVideoFrameError),
};

let picture_aspect_ratio =
Expand Down Expand Up @@ -835,6 +858,17 @@ pub enum AudioFrame<'a> {
BorrowedRecv(NDIlib_audio_frame_v3_t, &'a RecvInstance),
}

#[derive(Debug, Copy, Clone)]
pub struct TryFromAudioBufferError;

impl fmt::Display for TryFromAudioBufferError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Can't handle audio buffer")
}
}

impl std::error::Error for TryFromAudioBufferError {}

impl<'a> AudioFrame<'a> {
pub fn sample_rate(&self) -> i32 {
match self {
Expand Down Expand Up @@ -1012,13 +1046,15 @@ impl<'a> AudioFrame<'a> {
info: &gst_audio::AudioInfo,
buffer: &gst::BufferRef,
timecode: i64,
) -> Result<Self, ()> {
) -> Result<Self, TryFromAudioBufferError> {
if info.format() != gst_audio::AUDIO_FORMAT_F32 {
return Err(());
return Err(TryFromAudioBufferError);
}

let map = buffer.map_readable().map_err(|_| ())?;
let src_data = map.as_slice_of::<f32>().map_err(|_| ())?;
let map = buffer.map_readable().map_err(|_| TryFromAudioBufferError)?;
let src_data = map
.as_slice_of::<f32>()
.map_err(|_| TryFromAudioBufferError)?;

let no_samples = src_data.len() as i32 / info.channels() as i32;
let channel_stride_or_data_size_in_bytes = no_samples * mem::size_of::<f32>() as i32;
Expand Down
64 changes: 32 additions & 32 deletions src/ndisinkcombiner/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,41 +348,41 @@ impl AggregatorImpl for NdiSinkCombiner {
None => None,
};

let audio_buffer_segment_and_pad;
if let Some(audio_pad) = self.audio_pad.lock().unwrap().clone() {
audio_buffer_segment_and_pad = match audio_pad.peek_buffer() {
Some(audio_buffer) if audio_buffer.size() == 0 => {
// Skip empty/gap audio buffer
audio_pad.drop_buffer();
gst_trace!(CAT, obj: agg, "Empty audio buffer, waiting for next");
return Err(gst_base::AGGREGATOR_FLOW_NEED_DATA);
}
Some(audio_buffer) => {
let audio_segment = audio_pad.segment();
let audio_segment = match audio_segment.downcast::<gst::ClockTime>() {
Ok(audio_segment) => audio_segment,
Err(audio_segment) => {
gst_error!(
CAT,
obj: agg,
"Audio segment of wrong format {:?}",
audio_segment.format()
);
return Err(gst::FlowError::Error);
}
};
let audio_buffer_segment_and_pad =
if let Some(audio_pad) = self.audio_pad.lock().unwrap().clone() {
match audio_pad.peek_buffer() {
Some(audio_buffer) if audio_buffer.size() == 0 => {
// Skip empty/gap audio buffer
audio_pad.drop_buffer();
gst_trace!(CAT, obj: agg, "Empty audio buffer, waiting for next");
return Err(gst_base::AGGREGATOR_FLOW_NEED_DATA);
}
Some(audio_buffer) => {
let audio_segment = audio_pad.segment();
let audio_segment = match audio_segment.downcast::<gst::ClockTime>() {
Ok(audio_segment) => audio_segment,
Err(audio_segment) => {
gst_error!(
CAT,
obj: agg,
"Audio segment of wrong format {:?}",
audio_segment.format()
);
return Err(gst::FlowError::Error);
}
};

Some((audio_buffer, audio_segment, audio_pad))
}
None if !audio_pad.is_eos() => {
gst_trace!(CAT, obj: agg, "Waiting for audio buffer");
return Err(gst_base::AGGREGATOR_FLOW_NEED_DATA);
Some((audio_buffer, audio_segment, audio_pad))
}
None if !audio_pad.is_eos() => {
gst_trace!(CAT, obj: agg, "Waiting for audio buffer");
return Err(gst_base::AGGREGATOR_FLOW_NEED_DATA);
}
None => None,
}
None => None,
} else {
None
};
} else {
audio_buffer_segment_and_pad = None;
}

let mut state_storage = self.state.lock().unwrap();
let state = match &mut *state_storage {
Expand Down
19 changes: 9 additions & 10 deletions src/ndisrcdemux/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl ElementImpl for NdiSrcDemux {
PAD_TEMPLATES.as_ref()
}

#[allow(clippy::single_match)]
fn change_state(
&self,
element: &Self::Type,
Expand Down Expand Up @@ -158,10 +159,13 @@ impl NdiSrcDemux {
) -> Result<gst::FlowSuccess, gst::FlowError> {
gst_log!(CAT, obj: pad, "Handling buffer {:?}", buffer);

let meta = buffer.make_mut().meta_mut::<ndisrcmeta::NdiSrcMeta>().ok_or_else(|| {
gst_error!(CAT, obj: element, "Buffer without NDI source meta");
gst::FlowError::Error
})?;
let meta = buffer
.make_mut()
.meta_mut::<ndisrcmeta::NdiSrcMeta>()
.ok_or_else(|| {
gst_error!(CAT, obj: element, "Buffer without NDI source meta");
gst::FlowError::Error
})?;

let mut events = vec![];
let srcpad;
Expand Down Expand Up @@ -288,11 +292,7 @@ impl NdiSrcDemux {
state.combiner.update_pad_flow(&srcpad, res)
}

fn sink_event(&self,
pad: &gst::Pad,
element: &super::NdiSrcDemux,
event: gst::Event
) -> bool {
fn sink_event(&self, pad: &gst::Pad, element: &super::NdiSrcDemux, event: gst::Event) -> bool {
use gst::EventView;

gst_log!(CAT, obj: pad, "Handling event {:?}", event);
Expand All @@ -308,5 +308,4 @@ impl NdiSrcDemux {
}
pad.event_default(Some(element), event)
}

}
2 changes: 1 addition & 1 deletion src/ndisys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub const NDIlib_recv_color_format_ex_compressed_v5: NDIlib_recv_color_format_e
pub const NDIlib_recv_color_format_ex_compressed_v5_with_audio: NDIlib_recv_color_format_e = 308;

const fn make_fourcc(fourcc: &[u8; 4]) -> u32 {
((fourcc[0] as u32) << 0)
(fourcc[0] as u32)
| ((fourcc[1] as u32) << 8)
| ((fourcc[2] as u32) << 16)
| ((fourcc[3] as u32) << 24)
Expand Down
4 changes: 4 additions & 0 deletions src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
pub struct Receiver(Arc<ReceiverInner>);

#[derive(Debug, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum AudioInfo {
AudioInfo(gst_audio::AudioInfo),
#[cfg(feature = "advanced-sdk")]
Expand Down Expand Up @@ -169,12 +170,14 @@ impl VideoInfo {
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum Buffer {
Audio(gst::Buffer, AudioInfo),
Video(gst::Buffer, VideoInfo),
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
pub enum ReceiverItem {
Buffer(Buffer),
Flushing,
Expand Down Expand Up @@ -564,6 +567,7 @@ impl Receiver {
}
}

#[allow(clippy::too_many_arguments)]
pub fn connect(
element: &gst_base::BaseSrc,
ndi_name: Option<&str>,
Expand Down