diff --git a/gst-plugin-ndi/src/lib.rs b/gst-plugin-ndi/src/lib.rs index 69e921fb..4f7f1e1b 100644 --- a/gst-plugin-ndi/src/lib.rs +++ b/gst-plugin-ndi/src/lib.rs @@ -26,7 +26,6 @@ mod ndivideosrc; mod ndiaudiosrc; pub mod ndilib; -use std::ptr; use std::{thread, time}; //use std::time::{SystemTime, UNIX_EPOCH}; use std::ffi::{CStr, CString}; @@ -46,16 +45,6 @@ fn plugin_init(plugin: &gst::Plugin) -> bool { true } -struct Ndi{ - recv: Option, - start_pts: u64, -} - -static mut ndi_struct: Ndi = Ndi{ - recv: None, - start_pts: 0, -}; - struct ndi_receiver_info{ stream_name: String, ip: String, @@ -67,7 +56,7 @@ struct ndi_receiver_info{ lazy_static! { static ref hashmap_receivers: Mutex> = { - let mut m = HashMap::new(); + let m = HashMap::new(); Mutex::new(m) }; } @@ -84,7 +73,7 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream id_receiver += 1; //FIXME Search for another way to know if the source is an audio or a video source - if (element.get_name().contains("audiosrc")){ + if element.get_name().contains("audiosrc"){ audio = true; } else @@ -93,12 +82,12 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream } for val in receivers.values_mut(){ - if (val.ip == ip || val.stream_name == stream_name){ - if ((val.audio && val.video) || (val.audio && audio) || (val.video && video)){ + if val.ip == ip || val.stream_name == stream_name{ + if (val.audio && val.video) || (val.audio && audio) || (val.video && video){ break; } else { - if (video){ + if video { val.video = video; } else{ @@ -116,56 +105,53 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream return 0; } - let mut source: NDIlib_source_t = NDIlib_source_t{p_ndi_name: ptr::null(), - p_ip_address: ptr::null()}; + //TODO default values + let NDI_find_create_desc: NDIlib_find_create_t = Default::default(); + let pNDI_find = NDIlib_find_create_v2(&NDI_find_create_desc); + //let ip_ptr = CString::new(ip.clone()).unwrap(); + if pNDI_find.is_null() { + gst_element_error!(element, gst::CoreError::Negotiation, ["Cannot run NDI: NDIlib_find_create_v2 error"]); + // return false; + return 0; + } - //TODO default values - let NDI_find_create_desc: NDIlib_find_create_t = Default::default(); - let pNDI_find = NDIlib_find_create_v2(&NDI_find_create_desc); - let ip_ptr = CString::new(ip.clone()).unwrap(); - if pNDI_find.is_null() { - gst_element_error!(element, gst::CoreError::Negotiation, ["Cannot run NDI: NDIlib_find_create_v2 error"]); - // return false; - return 0; - } + let mut total_sources: u32 = 0; + let p_sources; - let mut total_sources: u32 = 0; - let p_sources; + // TODO Sleep 1s to wait for all sources + thread::sleep(time::Duration::from_millis(2000)); + p_sources = NDIlib_find_get_current_sources(pNDI_find, &mut total_sources as *mut u32); - // TODO Sleep 1s to wait for all sources - thread::sleep(time::Duration::from_millis(2000)); - p_sources = NDIlib_find_get_current_sources(pNDI_find, &mut total_sources as *mut u32); + // We need at least one source + if p_sources.is_null() { + gst_element_error!(element, gst::CoreError::Negotiation, ["Error getting NDIlib_find_get_current_sources"]); + // return false; + return 0; + } - // We need at least one source - if p_sources.is_null() { - gst_element_error!(element, gst::CoreError::Negotiation, ["Error getting NDIlib_find_get_current_sources"]); - // return false; - return 0; - } + let mut no_source: isize = -1; + for i in 0..total_sources as isize{ + if CStr::from_ptr((*p_sources.offset(i)).p_ndi_name).to_string_lossy().into_owned() == stream_name || + CStr::from_ptr((*p_sources.offset(i)).p_ip_address).to_string_lossy().into_owned() == ip{ + no_source = i; + break; + } + } + if no_source == -1 { + gst_element_error!(element, gst::CoreError::Negotiation, ["Stream name not found"]); + // return false; + return 0; + } - let mut no_source: isize = -1; - for i in 0..total_sources as isize{ - if (CStr::from_ptr((*p_sources.offset(i)).p_ndi_name).to_string_lossy().into_owned() == stream_name || - CStr::from_ptr((*p_sources.offset(i)).p_ip_address).to_string_lossy().into_owned() == ip){ - no_source = i; - break; - } - } - if no_source == -1 { - gst_element_error!(element, gst::CoreError::Negotiation, ["Stream name not found"]); - // return false; - return 0; - } + gst_debug!(cat, obj: element, "Total sources in network {}: Connecting to NDI source with name '{}' and address '{}'", total_sources, + CStr::from_ptr((*p_sources.offset(no_source)).p_ndi_name) + .to_string_lossy() + .into_owned(), + CStr::from_ptr((*p_sources.offset(no_source)).p_ip_address) + .to_string_lossy() + .into_owned()); - gst_debug!(cat, obj: element, "Total sources in network {}: Connecting to NDI source with name '{}' and address '{}'", total_sources, - CStr::from_ptr((*p_sources.offset(no_source)).p_ndi_name) - .to_string_lossy() - .into_owned(), - CStr::from_ptr((*p_sources.offset(no_source)).p_ip_address) - .to_string_lossy() - .into_owned()); - - source = *p_sources.offset(no_source).clone(); + let source = *p_sources.offset(no_source).clone(); let source_ip = CStr::from_ptr(source.p_ip_address).to_string_lossy().into_owned(); let source_name = CStr::from_ptr(source.p_ndi_name).to_string_lossy().into_owned(); @@ -228,7 +214,7 @@ fn stop_ndi(cat: gst::DebugCategory , element: &BaseSrc, id: i8) -> bool{ { let val = receivers.get_mut(&id).unwrap(); if val.video && val.audio{ - if (element.get_name().contains("audiosrc")){ + if element.get_name().contains("audiosrc"){ val.audio = false; } else{ diff --git a/gst-plugin-ndi/src/ndiaudiosrc.rs b/gst-plugin-ndi/src/ndiaudiosrc.rs index 0dbb7b2c..07f907b5 100644 --- a/gst-plugin-ndi/src/ndiaudiosrc.rs +++ b/gst-plugin-ndi/src/ndiaudiosrc.rs @@ -18,7 +18,6 @@ use std::ptr; use ndilib::*; use connect_ndi; -// use ndi_struct; use stop_ndi; use hashmap_receivers; @@ -302,7 +301,7 @@ impl NdiAudioSrc { //let settings = *self.settings.lock().unwrap(); let state = self.state.lock().unwrap(); - if let Some(ref info) = state.info { + if let Some(ref _info) = state.info { // let latency = gst::SECOND // .mul_div_floor(settings.samples_per_buffer as u64, info.rate() as u64) // .unwrap(); @@ -311,10 +310,9 @@ impl NdiAudioSrc { // .mul_div_floor(1 as u64, 30 as u64) // .unwrap(); // gst_debug!(self.cat, obj: element, "Returning latency {}", latency); - println!("/*/a*f/a*sd/f*ad/sf*ad/sf*ad/sf"); let max = latency * 1843200; - println!("{:?}", latency); - println!("{:?}",max); + // println!("{:?}", latency); + // println!("{:?}",max); q.set(true, latency, max); return true; } else { @@ -334,7 +332,7 @@ impl NdiAudioSrc { let recv = &receivers.get(&settings.id_receiver).unwrap().ndi_instance; let pNDI_recv = recv.recv; - + let mut timestamp_data = self.timestamp_data.lock().unwrap(); let audio_frame: NDIlib_audio_frame_v2_t = Default::default(); @@ -344,7 +342,6 @@ impl NdiAudioSrc { frame_type = NDIlib_recv_capture_v2(pNDI_recv, ptr::null(), &audio_frame, ptr::null(), 1000); } - //ndi_struct.start_pts = audio_frame.timecode as u64; timestamp_data.pts = audio_frame.timecode as u64; let mut caps = gst::Caps::truncate(caps); @@ -387,14 +384,7 @@ impl NdiAudioSrc { unsafe{ let receivers = hashmap_receivers.lock().unwrap(); - let mut id = &_settings.stream_name; - - if (&_settings.ip != ""){ - id = &_settings.ip; - } - let recv = &receivers.get(&_settings.id_receiver).unwrap().ndi_instance; - let pNDI_recv = recv.recv; let pts: u64; @@ -402,7 +392,6 @@ impl NdiAudioSrc { NDIlib_recv_capture_v2(pNDI_recv, ptr::null(), &audio_frame, ptr::null(), 1000,); pts = (audio_frame.timecode as u64) - timestamp_data.pts; - //pts = (audio_frame.timecode as u64) - ndi_struct.start_pts; let buff_size = ((audio_frame.channel_stride_in_bytes)) as usize; let mut buffer = gst::Buffer::with_size(buff_size).unwrap(); diff --git a/gst-plugin-ndi/src/ndivideosrc.rs b/gst-plugin-ndi/src/ndivideosrc.rs index 543b0efb..63d17092 100644 --- a/gst-plugin-ndi/src/ndivideosrc.rs +++ b/gst-plugin-ndi/src/ndivideosrc.rs @@ -19,7 +19,6 @@ use std::ptr; use ndilib::*; use connect_ndi; -// use ndi_struct; use stop_ndi; use hashmap_receivers; @@ -309,7 +308,7 @@ impl NdiVideoSrc { //let settings = *self.settings.lock().unwrap(); let state = self.state.lock().unwrap(); - if let Some(ref info) = state.info { + if let Some(ref _info) = state.info { // let latency = gst::SECOND // .mul_div_floor(settings.samples_per_buffer as u64, info.rate() as u64) // .unwrap(); @@ -318,10 +317,9 @@ impl NdiVideoSrc { // .mul_div_floor(1 as u64, 30 as u64) // .unwrap(); // gst_debug!(self.cat, obj: element, "Returning latency {}", latency); - println!("/*/a*f/a*sd/f*ad/sf*ad/sf*ad/sf"); let max = latency * 1843200; - println!("{:?}", latency); - println!("{:?}",max); + // println!("{:?}", latency); + // println!("{:?}",max); q.set(true, latency, max); return true; } else { @@ -353,7 +351,6 @@ impl NdiVideoSrc { //TODO Check that this is working timestamp_data.pts = video_frame.timecode as u64; - //ndi_struct.start_pts = video_frame.timecode as u64; let mut caps = gst::Caps::truncate(caps); { @@ -403,7 +400,6 @@ impl NdiVideoSrc { NDIlib_recv_capture_v2(pNDI_recv, &video_frame, ptr::null(), ptr::null(), 1000,); pts = (video_frame.timecode as u64) - timestamp_data.pts; - //pts = (video_frame.timecode as u64) - ndi_struct.start_pts; let buff_size = (video_frame.yres * video_frame.line_stride_in_bytes) as usize; //println!("{:?}", buff_size);