diff --git a/Cargo.lock b/Cargo.lock index fea8e19..d9dbc7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1236,6 +1236,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is_sorted" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357376465c37db3372ef6a00585d336ed3d0f11d4345eef77ebcb05865392b21" + [[package]] name = "itertools" version = "0.10.5" @@ -1415,6 +1421,7 @@ dependencies = [ "gstreamer-app", "gstreamer-rtsp", "gstreamer-rtsp-server", + "is_sorted", "itertools", "lazy_static", "log", diff --git a/Cargo.toml b/Cargo.toml index a51c927..fc30a56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ gstreamer = "0.20.3" gstreamer-app = "0.20.0" gstreamer-rtsp = "0.20.0" gstreamer-rtsp-server = { version = "0.20.3", features = ["v1_16"] } +is_sorted = "0.1.1" itertools = "0.10.5" lazy_static = "1.4.0" log = { version = "0.4.17", features = [ "release_max_level_debug" ] } diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index 188a1ab..c74eaa1 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -8,6 +8,7 @@ use futures::{ use gstreamer::{prelude::*, ClockTime}; use gstreamer_app::AppSrc; pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate}; +use is_sorted::IsSorted; use itertools::Itertools; use log::*; use neolink_core::bcmedia::model::*; @@ -74,10 +75,19 @@ impl NeoBuffer { }); } - for sorted_item in sorting_vec.drain(..) { - // debug!("Pushing frame with time: {}", sorted_item.time); + while let Some(sorted_item) = sorting_vec.pop() { + debug!("Pushing frame with time: {}", sorted_item.time); self.buf.push_back(sorted_item); } + + debug_assert!( + IsSorted::is_sorted(&mut self.buf.iter().map(|stamped| stamped.time)), + "{:?}", + self.buf + .iter() + .map(|stamped| stamped.time) + .collect::>() + ); } else if let Some(last) = self.buf.back_mut() { last.data.push(media); } @@ -93,6 +103,10 @@ impl NeoBuffer { self.buf.len() > BUFFER_SIZE * 2 / 3 } + pub(crate) fn ready_play(&self) -> bool { + self.buf.len() > BUFFER_SIZE / 3 + } + // fn last_iframe_time(&self) -> Option { // let (fronts, backs) = self.buf.as_slices(); // backs @@ -434,7 +448,7 @@ impl NeoMediaSenders { // Set them into the non init state // This will make them wait for the // buffer to be enough then jump to live - client.inited = false; + let _ = client.jump_to_live().await; } }, FactoryCommand::Pause => { @@ -636,6 +650,7 @@ impl NeoMediaSender { if let Some(target_time) = Self::target_live_for(buffer) { // Minimum buffer self.inited = true; + self.buffer.buf.clear(); let mut buffer_iter = buffer.buf.iter().cloned(); let preprocess = buffer_iter @@ -709,7 +724,7 @@ impl NeoMediaSender { debug!("Buffer overfull"); self.jump_to_live().await?; } - if self.refilling && self.buffer.ready() { + if self.refilling && self.buffer.ready_play() { self.refilling = false; self.jump_to_live().await?; } else if self.refilling { @@ -743,8 +758,7 @@ impl NeoMediaSender { // Send buffers let mut buffers = vec![]; - const LATENCY: FrameTime = Duration::from_millis(250).as_micros() as FrameTime; - if let Some(buftime) = self.get_buftime().map(|i| i.saturating_add(LATENCY)) { + if let Some(buftime) = self.get_buftime() { // debug!("Update: buftime: {}", buf time); while self .buffer @@ -821,14 +835,12 @@ impl NeoMediaSender { for (time, buf) in vid_buffers.drain(..) { tokio::task::yield_now().await; let runtime = self.buftime_to_runtime(time); - let _actual_runtime = - self.get_runtime().map(|i| Duration::from_micros(i as u64)); - // debug!( - // " - Sending vid frame at time {} ({:?} Expect: {:?})", - // time, - // Duration::from_micros(runtime as u64), - // actual_runtime - // ); + debug!( + " - Sending vid frame at time {} ({:?} Expect: {:?})", + time, + Duration::from_micros(runtime as u64), + self.get_runtime().map(|i| Duration::from_micros(i as u64)) + ); let gst_buf = { let mut gst_buf =