Skip to content

Commit

Permalink
Added drop implementations. Tidy up code.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantSparks committed Jul 23, 2024
1 parent d8b76fe commit 83dc4d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
let bindings = bindgen::Builder::default()
.header(main_header)
.clang_arg(format!("-I{}", ndi_include_path))
// .layout_tests(false)
//.layout_tests(false)
.derive_default(true)
.generate()
.expect("Unable to generate bindings");
Expand Down
1 change: 1 addition & 0 deletions examples/NDIlib_Find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn main() -> Result<(), Error> {
// Initialize the NDI library and ensure it's properly cleaned up
if let Ok(ndi) = NDI::new() {
// Create an NDI finder to locate sources on the network
// let finder = Finder::default();
let finder = Finder::new(false, None, Some("192.168.0.110"));
let ndi_find = Find::new(&ndi, finder)?;

Expand Down
53 changes: 40 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,16 @@ impl VideoFrame {
}
}

impl Drop for VideoFrame {
fn drop(&mut self) {
if !self.p_metadata.is_null() {
unsafe {
let _ = CString::from_raw(self.p_metadata as *mut c_char);
}
}
}
}

pub struct AudioFrame {
pub sample_rate: i32,
pub no_channels: i32,
Expand All @@ -587,13 +597,17 @@ pub struct AudioFrame {
pub fourcc: AudioType,
pub data: Vec<u8>,
pub channel_stride_in_bytes: i32,
pub metadata: Option<String>,
pub metadata: Option<CString>,
pub timestamp: i64,
}

impl Default for AudioFrame {
fn default() -> Self {
Self::new()
impl Drop for AudioFrame {
fn drop(&mut self) {
if let Some(metadata) = self.metadata.take() {
unsafe {
let _ = CString::from_raw(metadata.into_raw());
}
}
}
}

Expand Down Expand Up @@ -623,6 +637,9 @@ impl AudioFrame {
metadata: Option<String>,
timestamp: i64,
) -> Result<Self, Error> {
let metadata_cstring = metadata
.map(|m| CString::new(m).map_err(Error::InvalidCString))
.transpose()?;
Ok(AudioFrame {
sample_rate,
no_channels,
Expand All @@ -631,7 +648,7 @@ impl AudioFrame {
fourcc,
data,
channel_stride_in_bytes: no_samples * 4, // assuming 4 bytes per sample for float
metadata,
metadata: metadata_cstring,
timestamp,
})
}
Expand All @@ -647,13 +664,7 @@ impl AudioFrame {
__bindgen_anon_1: NDIlib_audio_frame_v3_t__bindgen_ty_1 {
channel_stride_in_bytes: self.channel_stride_in_bytes,
},
p_metadata: match &self.metadata {
Some(metadata) => CString::new(metadata.clone())
.map_err(Error::InvalidCString)
.unwrap()
.into_raw(),
None => ptr::null(),
},
p_metadata: self.metadata.as_ref().map_or(ptr::null(), |m| m.as_ptr()),
timestamp: self.timestamp,
}
}
Expand Down Expand Up @@ -718,12 +729,18 @@ impl AudioFrame {
},
data,
channel_stride_in_bytes: unsafe { raw.__bindgen_anon_1.channel_stride_in_bytes },
metadata,
metadata: metadata.map(|m| CString::new(m).unwrap()),
timestamp: raw.timestamp,
}
}
}

impl Default for AudioFrame {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AudioType {
FLTP,
Expand Down Expand Up @@ -787,6 +804,16 @@ impl Default for MetadataFrame {
}
}

impl Drop for MetadataFrame {
fn drop(&mut self) {
if !self.p_data.is_null() {
unsafe {
let _ = CString::from_raw(self.p_data);
}
}
}
}

#[derive(Debug, Clone, Copy)]
pub enum RecvColorFormat {
BGRX_BGRA,
Expand Down

0 comments on commit 83dc4d3

Please sign in to comment.