From 4338068b3c8fd41b0ed59bcae8ca9b5f7d2ca6bf Mon Sep 17 00:00:00 2001 From: Daniel Vilar Date: Mon, 20 Aug 2018 16:59:12 +0200 Subject: [PATCH] Use same start_pts for audio and video in the same ndi receiver --- gst-plugin-ndi/src/lib.rs | 3 ++- gst-plugin-ndi/src/ndiaudiosrc.rs | 16 ++++++++-------- gst-plugin-ndi/src/ndivideosrc.rs | 16 ++++++++-------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/gst-plugin-ndi/src/lib.rs b/gst-plugin-ndi/src/lib.rs index 4f7f1e1b1..7b51c53a4 100644 --- a/gst-plugin-ndi/src/lib.rs +++ b/gst-plugin-ndi/src/lib.rs @@ -52,6 +52,7 @@ struct ndi_receiver_info{ audio: bool, ndi_instance: NdiInstance, id: i8, + timestamp: u64, } lazy_static! { @@ -193,7 +194,7 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream NDIlib_recv_send_metadata(pNDI_recv, &enable_hw_accel); - receivers.insert(id_receiver, ndi_receiver_info{stream_name: source_name.clone(), ip: source_ip.clone(), video:video, audio: audio, ndi_instance: NdiInstance{recv: pNDI_recv}, id: id_receiver}); + receivers.insert(id_receiver, ndi_receiver_info{stream_name: source_name.clone(), ip: source_ip.clone(), video:video, audio: audio, ndi_instance: NdiInstance{recv: pNDI_recv}, id: id_receiver, timestamp: 0}); // let start = SystemTime::now(); // let since_the_epoch = start.duration_since(UNIX_EPOCH) diff --git a/gst-plugin-ndi/src/ndiaudiosrc.rs b/gst-plugin-ndi/src/ndiaudiosrc.rs index 07f907b5b..897b44844 100644 --- a/gst-plugin-ndi/src/ndiaudiosrc.rs +++ b/gst-plugin-ndi/src/ndiaudiosrc.rs @@ -73,7 +73,6 @@ impl Default for State { } struct TimestampData{ - pts: u64, offset: u64, } @@ -102,7 +101,6 @@ impl NdiAudioSrc { settings: Mutex::new(Default::default()), state: Mutex::new(Default::default()), timestamp_data: Mutex::new(TimestampData{ - pts: 0, offset: 0, }), }) @@ -327,14 +325,13 @@ impl NdiAudioSrc { fn fixate(&self, element: &BaseSrc, caps: gst::Caps) -> gst::Caps { //We need to set the correct caps resolution and framerate unsafe{ - let receivers = hashmap_receivers.lock().unwrap(); + let mut receivers = hashmap_receivers.lock().unwrap(); let settings = self.settings.lock().unwrap(); - let recv = &receivers.get(&settings.id_receiver).unwrap().ndi_instance; + let receiver = receivers.get_mut(&settings.id_receiver).unwrap(); + let recv = &receiver.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(); let mut frame_type: NDIlib_frame_type_e = NDIlib_frame_type_e::NDIlib_frame_type_none; @@ -342,7 +339,9 @@ impl NdiAudioSrc { frame_type = NDIlib_recv_capture_v2(pNDI_recv, ptr::null(), &audio_frame, ptr::null(), 1000); } - timestamp_data.pts = audio_frame.timecode as u64; + if receiver.timestamp >= audio_frame.timecode as u64 || receiver.timestamp == 0{ + receiver.timestamp = audio_frame.timecode as u64; + } let mut caps = gst::Caps::truncate(caps); { @@ -391,7 +390,8 @@ impl NdiAudioSrc { let audio_frame: NDIlib_audio_frame_v2_t = Default::default(); NDIlib_recv_capture_v2(pNDI_recv, ptr::null(), &audio_frame, ptr::null(), 1000,); - pts = (audio_frame.timecode as u64) - timestamp_data.pts; + let time = &receivers.get(&_settings.id_receiver).unwrap().timestamp; + pts = audio_frame.timecode as u64- time; 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 63d170924..49e5189e7 100644 --- a/gst-plugin-ndi/src/ndivideosrc.rs +++ b/gst-plugin-ndi/src/ndivideosrc.rs @@ -74,7 +74,6 @@ impl Default for State { } struct TimestampData{ - pts: u64, offset: u64, } @@ -103,7 +102,6 @@ impl NdiVideoSrc { settings: Mutex::new(Default::default()), state: Mutex::new(Default::default()), timestamp_data: Mutex::new(TimestampData{ - pts: 0, offset: 0, }), }) @@ -334,14 +332,13 @@ impl NdiVideoSrc { fn fixate(&self, element: &BaseSrc, caps: gst::Caps) -> gst::Caps { //We need to set the correct caps resolution and framerate unsafe{ - let receivers = hashmap_receivers.lock().unwrap(); + let mut receivers = hashmap_receivers.lock().unwrap(); let settings = self.settings.lock().unwrap(); - let recv = &receivers.get(&settings.id_receiver).unwrap().ndi_instance; + let receiver = receivers.get_mut(&settings.id_receiver).unwrap(); + let recv = &receiver.ndi_instance; let pNDI_recv = recv.recv; - let mut timestamp_data = self.timestamp_data.lock().unwrap(); - let video_frame: NDIlib_video_frame_v2_t = Default::default(); let mut frame_type: NDIlib_frame_type_e = NDIlib_frame_type_e::NDIlib_frame_type_none; @@ -350,7 +347,9 @@ impl NdiVideoSrc { } //TODO Check that this is working - timestamp_data.pts = video_frame.timecode as u64; + if receiver.timestamp >= video_frame.timecode as u64 || receiver.timestamp == 0{ + receiver.timestamp = video_frame.timecode as u64; + } let mut caps = gst::Caps::truncate(caps); { @@ -399,7 +398,8 @@ impl NdiVideoSrc { let video_frame: NDIlib_video_frame_v2_t = Default::default(); NDIlib_recv_capture_v2(pNDI_recv, &video_frame, ptr::null(), ptr::null(), 1000,); - pts = (video_frame.timecode as u64) - timestamp_data.pts; + let time = &receivers.get(&_settings.id_receiver).unwrap().timestamp; + pts = video_frame.timecode as u64 - time; let buff_size = (video_frame.yres * video_frame.line_stride_in_bytes) as usize; //println!("{:?}", buff_size);