From dbf21d9c909bcce106a8da2343a7a4d13b3080da Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 7 May 2023 09:29:47 +0700 Subject: [PATCH] Change how frames are popped from buffer. Change how target time is updated --- Cargo.lock | 1 + Cargo.toml | 3 +- src/rtsp/gst.rs | 2 +- src/rtsp/gst/factory.rs | 11 +- src/rtsp/gst/sender.rs | 387 +++++++++++++++++------------------ src/rtsp/gst/server.rs | 31 ++- src/rtsp/mod.rs | 53 +++-- src/rtsp/spring.rs | 88 ++++++++ src/rtsp/states/streaming.rs | 10 +- 9 files changed, 362 insertions(+), 224 deletions(-) create mode 100644 src/rtsp/spring.rs diff --git a/Cargo.lock b/Cargo.lock index ef39f9d..d17e923 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1415,6 +1415,7 @@ dependencies = [ "gstreamer-app", "gstreamer-rtsp", "gstreamer-rtsp-server", + "itertools", "lazy_static", "log", "neolink_core", diff --git a/Cargo.toml b/Cargo.toml index aa751d2..9d32046 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,8 +23,9 @@ gstreamer = "0.20.3" gstreamer-app = "0.20.0" gstreamer-rtsp = "0.20.0" gstreamer-rtsp-server = { version = "0.20.3", features = ["v1_16"] } +itertools = "0.10.5" lazy_static = "1.4.0" -log = "0.4.17" +log = { version = "0.4.17", features = [ "release_max_level_debug" ] } neolink_core = { path = "crates/core", version = "0.5.8" } regex = "1.7.3" rumqttc = "0.20.0" diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index f323e38..7fedf33 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -6,7 +6,7 @@ mod sender; mod server; mod shared; -use factory::*; +pub(crate) use factory::*; pub(crate) use self::server::NeoRtspServer; diff --git a/src/rtsp/gst/factory.rs b/src/rtsp/gst/factory.rs index 88b7af9..4642afc 100644 --- a/src/rtsp/gst/factory.rs +++ b/src/rtsp/gst/factory.rs @@ -56,7 +56,7 @@ impl NeoMediaFactory { factory } - pub(crate) fn get_sender(&self) -> Sender { + pub(crate) fn get_sender(&self) -> Sender { self.imp().sender.clone() } @@ -115,8 +115,15 @@ impl NeoMediaFactory { unsafe impl Send for NeoMediaFactory {} unsafe impl Sync for NeoMediaFactory {} +#[derive(Debug)] +pub(crate) enum FactoryCommand { + BcMedia(BcMedia), + ClearBuffer, + JumpToLive, +} + pub(crate) struct NeoMediaFactoryImpl { - sender: Sender, + sender: Sender, clientsender: Sender, shared: Arc, #[allow(dead_code)] // Not dead just need a handle to keep it alive and drop with this obj diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index 582738d..aacf495 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -5,6 +5,7 @@ use futures::stream::StreamExt; use gstreamer::{prelude::*, ClockTime}; use gstreamer_app::AppSrc; pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate}; +use itertools::Itertools; use log::*; use neolink_core::bcmedia::model::*; use std::sync::{ @@ -20,11 +21,12 @@ use std::{ }; use tokio::{ sync::mpsc::{channel, Receiver, Sender}, - time::{Duration, Instant}, + time::Duration, }; use tokio_stream::wrappers::ReceiverStream; -use super::{shared::*, AnyResult}; +use super::{factory::FactoryCommand, shared::*, AnyResult}; +use crate::rtsp::Spring; type FrameTime = i64; @@ -34,122 +36,106 @@ const MILLIS: FrameTime = 1000; const LATENCY: FrameTime = 20 * MILLIS; const BUFFER_TIME: FrameTime = 10 * SECS; -const BUFFER_SIZE: usize = 3; +const BUFFER_SIZE: usize = 100; #[derive(Default)] struct NeoBuffer { - buf: VecDeque>, + buf: VecDeque, } impl NeoBuffer { fn push(&mut self, item: BcMedia) { - let mut was_iframe = false; - match item { - BcMedia::Iframe(data) => { - debug!( - "Pushed iFrame to buffer at {:?}", - Duration::from_micros(data.microseconds.try_into().unwrap()) - ); - was_iframe = true; - self.buf.push_back(vec![BcMedia::Iframe(data)]) - } - pframe @ BcMedia::Pframe(_) => { - if let Some(last) = self.buf.back_mut().as_mut() { - last.push(pframe); + self.buf.push_back(item); + let (fronts, backs) = self.buf.as_slices(); + let last_frame = backs + .iter() + .rev() + .chain(&mut fronts.iter().rev()) + .flat_map(|frame| match frame { + BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => Some(*microseconds), + BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => Some(*microseconds), + _ => None, + }) + .next() + .map(|i| i as FrameTime); + if let Some(last_frame) = last_frame { + while match self.buf.front() { + Some(BcMedia::Iframe(BcMediaIframe { microseconds, .. })) => { + last_frame - *microseconds as FrameTime > BUFFER_TIME } - } - aac @ BcMedia::Aac(_) => { - if let Some(last) = self.buf.back_mut().as_mut() { - last.push(aac); + Some(BcMedia::Pframe(BcMediaPframe { microseconds, .. })) => { + last_frame - *microseconds as FrameTime > BUFFER_TIME } - } - adpcm @ BcMedia::Adpcm(_) => { - if let Some(last) = self.buf.back_mut().as_mut() { - last.push(adpcm); - } - } - BcMedia::InfoV1(_) | BcMedia::InfoV2(_) => {} - } - if was_iframe { - if let Some(last_frame_time) = self.last_iframe_time() { - let mut time_delta = self - .first_iframe_time() - .map(|ff| last_frame_time.saturating_sub(ff)); - while let Some(time) = time_delta { - if time > BUFFER_TIME && self.buf.len() > BUFFER_SIZE { - let _ = self.buf.pop_front(); - debug!( - "Popping frame with time difference of {:?}", - Duration::from_micros(time.try_into().unwrap_or(0)) - ); - time_delta = self - .first_iframe_time() - .map(|ff| last_frame_time.saturating_sub(ff)); - } else { - debug!("Iframes left in the buffer: {}", self.buf.len()); - break; - } + Some(_) => true, + None => false, + } && self.buf.len() > BUFFER_SIZE + { + if self.buf.pop_front().is_none() { + break; } } } } - fn last_iframe_time(&self) -> Option { - self.buf - .back() - .as_ref() - .and_then(|b| b.first()) - .and_then(|f| { - if let BcMedia::Iframe(frame) = f { - Some(frame.microseconds as FrameTime) - } else { - None - } - }) - } + // fn last_iframe_time(&self) -> Option { + // let (fronts, backs) = self.buf.as_slices(); + // backs + // .iter() + // .rev() + // .chain(&mut fronts.iter().rev()) + // .flat_map(|frame| match frame { + // BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => Some(*microseconds), + // _ => None, + // }) + // .next() + // .map(|i| i as FrameTime) + // } - fn first_iframe_time(&self) -> Option { - self.buf - .front() - .as_ref() - .and_then(|b| b.first()) - .and_then(|f| { - if let BcMedia::Iframe(frame) = f { - Some(frame.microseconds as FrameTime) - } else { - None - } - }) - } + // fn first_iframe_time(&self) -> Option { + // let (fronts, backs) = self.buf.as_slices(); + // fronts + // .iter() + // .chain(&mut backs.iter()) + // .flat_map(|frame| match frame { + // BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => Some(*microseconds), + // _ => None, + // }) + // .next() + // .map(|i| i as FrameTime) + // } fn start_time(&self) -> Option { - if let Some(BcMedia::Iframe(data)) = - self.buf.front().and_then(|inner_buf| inner_buf.first()) - { - Some(data.microseconds as FrameTime) - } else { - None - } + let (fronts, backs) = self.buf.as_slices(); + fronts + .iter() + .chain(&mut backs.iter()) + .flat_map(|frame| match frame { + BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => Some(*microseconds), + BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => Some(*microseconds), + _ => None, + }) + .next() + .map(|i| i as FrameTime) } fn end_time(&self) -> Option { - if let Some(innerbuffer) = self.buf.back() { - innerbuffer - .iter() - .flat_map(|frame| match frame { - BcMedia::Iframe(data) => Some(data.microseconds as FrameTime), - BcMedia::Pframe(data) => Some(data.microseconds as FrameTime), - _ => None, - }) - .max() - } else { - None - } + let (fronts, backs) = self.buf.as_slices(); + backs + .iter() + .rev() + .chain(&mut fronts.iter().rev()) + .flat_map(|frame| match frame { + BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => Some(*microseconds), + BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => Some(*microseconds), + _ => None, + }) + .next() + .map(|i| i as FrameTime) } } pub(super) struct NeoMediaSenders { - data_source: ReceiverStream, + data_source: ReceiverStream, client_source: ReceiverStream, shared: Arc, uid: AtomicU64, @@ -160,7 +146,7 @@ pub(super) struct NeoMediaSenders { impl NeoMediaSenders { pub(super) fn new( shared: Arc, - data_source: ReceiverStream, + data_source: ReceiverStream, client_source: ReceiverStream, ) -> Self { Self { @@ -317,16 +303,60 @@ impl NeoMediaSenders { Ok(()) } + async fn clear_buffer(&mut self) -> AnyResult<()> { + self.buffer.buf.clear(); + self.shared.buffer_ready.store(false, Ordering::Relaxed); + for client in self.client_data.values_mut() { + // 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; + } + Ok(()) + } + + async fn update_springs(&mut self) -> AnyResult<()> { + for client in self.client_data.values_mut() { + client.start_time.update().await; + if let Some(new_target) = client + .target_live_time(&self.buffer) + .await? + .map(|i| i as f64) + { + client.target_time.set_target(new_target); + } + client.target_time.update().await; + } + Ok(()) + } + pub(super) async fn run(&mut self) -> AnyResult<()> { let mut interval = tokio::time::interval(Duration::from_millis(20)); // 50 FPS loop { tokio::task::yield_now().await; + self.update_springs().await?; tokio::select! { _ = interval.tick() => { self.handle_buffer().await }, Some(v) = self.data_source.next() => { - self.handle_new_data(v).await + match v { + FactoryCommand::BcMedia(media) => { + self.handle_new_data(media).await?; + }, + FactoryCommand::ClearBuffer => { + self.clear_buffer().await?; + }, + FactoryCommand::JumpToLive => { + for client in self.client_data.values_mut() { + // 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; + } + }, + } + Ok(()) }, Some(v) = self.client_source.next() => { self.handle_new_client(v).await @@ -346,11 +376,8 @@ pub(super) enum NeoMediaSenderCommand { } #[derive(Debug)] pub(super) struct NeoMediaSender { - start_time: FrameTime, - target_start_time: FrameTime, - smoothed_start_time: f64, - start_time_v: f64, - start_time_update: Instant, + start_time: Spring, + target_time: Spring, last_sent_time: FrameTime, vid: Option, aud: Option, @@ -371,11 +398,8 @@ impl NeoMediaSender { pub(super) fn new() -> Self { let (tx, rx) = channel(30); Self { - start_time: 0, - target_start_time: 0, - smoothed_start_time: 0.0, - start_time_v: 0.0, - start_time_update: Instant::now(), + start_time: Spring::new(0.0, 0.0, 2.5), + target_time: Spring::new(0.0, 0.0, 0.25), last_sent_time: 0, vid: None, aud: None, @@ -405,8 +429,12 @@ impl NeoMediaSender { let jump_method = JumpMethod::BufferPerunit(0.75); Ok(match jump_method { JumpMethod::MiddleIFrame => { - let idx = frames.len().saturating_div(2); - frames.get(idx).and_then(|f| f.first()).and_then(|f| { + let iframes = frames + .iter() + .filter(|f| matches!(f, BcMedia::Iframe(_))) + .collect::>(); + let idx = iframes.len().saturating_div(2); + iframes.get(idx).and_then(|f| { if let BcMedia::Iframe(data) = f { Some(data.microseconds as FrameTime) } else { @@ -426,23 +454,25 @@ impl NeoMediaSender { async fn jump_to_live(&mut self, buffer: &NeoBuffer) -> AnyResult<()> { if self.inited { - let runtime = self.get_runtime().unwrap_or(0); - let target_time = self.target_live_time(buffer).await?; - if let Some(target_time) = target_time { + if let Some(new_target_time) = self.target_live_time(buffer).await? { + self.target_time.reset_to(new_target_time as f64); + + let runtime = self.get_runtime().unwrap_or(0); if let Some(et) = buffer.end_time() { - if let Ok(delta) = TryInto::::try_into(et - target_time) { + if let Ok(delta) = TryInto::::try_into(et - self.target_time.value_i64()) { debug!("Expected latency: {:?}", Duration::from_micros(delta)); } } - self.start_time = target_time - runtime; - self.target_start_time = self.start_time; - self.smoothed_start_time = self.start_time as f64; - self.start_time_v = 0.0; - self.last_sent_time = target_time; + self.start_time + .reset_to(self.target_time.value() - (runtime as f64)); + self.last_sent_time = self.target_time.value_i64(); debug!( "Target time: {:?}, New start time: {:?}, New Runtime: {:?}, Actual Runtime: {:?}", - Duration::from_micros(target_time.try_into().unwrap_or(0)), Duration::from_micros(self.start_time.try_into().unwrap_or(0)), Duration::from_micros(self.buftime_to_runtime(target_time).try_into().unwrap_or(0)), Duration::from_micros(runtime.try_into().unwrap_or(0)) + Duration::from_micros(self.target_time.value_u64()), + Duration::from_micros(self.start_time.value_u64()), + Duration::from_micros(self.target_time.value_u64()), + Duration::from_micros(runtime.try_into().unwrap_or(0)) ); } } @@ -451,39 +481,26 @@ impl NeoMediaSender { } async fn stretch_live(&mut self, buffer: &NeoBuffer) -> AnyResult<()> { - // Desired live time - let target_time = self.target_live_time(buffer).await?; // Actual live time let actual_time = self.get_buftime(); let st = buffer.start_time(); let et = buffer.end_time(); - if let (Some(target_time), Some(actual_time), Some(st), Some(et)) = - (target_time, actual_time, st, et) - { + if let (Some(actual_time), Some(st), Some(et)) = (actual_time, st, et) { if actual_time > st.saturating_sub(LATENCY * 2) && actual_time < et.saturating_add(LATENCY * 2) { // Only do this while inside the buffer - self.target_start_time = self.start_time + (target_time - actual_time); // Adjust + self.start_time.set_target( + self.start_time.value() + self.target_time.value() - (actual_time as f64), + ); // Adjust - // Now for the spring - let dt = (Instant::now() - self.start_time_update) - .min(Duration::from_millis(200)) - .as_secs_f64(); - spring_update( - &mut self.smoothed_start_time, - self.target_start_time as f64, - &mut self.start_time_v, - 2.5, - dt, - ); if let Some(new_actual_time) = self.get_buftime() { let expected_position = (new_actual_time - st) as f32 / (et - st) as f32; debug!("expected_position: {}", expected_position); } } } - self.start_time_update = Instant::now(); + self.start_time.update().await; Ok(()) } @@ -541,11 +558,11 @@ impl NeoMediaSender { } fn runtime_to_buftime(&self, runtime: FrameTime) -> FrameTime { - runtime.saturating_add(self.start_time) + runtime.saturating_add(self.start_time.value_i64()) } fn buftime_to_runtime(&self, buftime: FrameTime) -> FrameTime { - buftime.saturating_sub(self.start_time).max(0) + buftime.saturating_sub(self.start_time.value_i64()).max(0) } async fn process_buffer(&mut self, buf: &NeoBuffer) -> AnyResult<()> { @@ -581,47 +598,43 @@ impl NeoMediaSender { ); } let mut last_sent_time = min_time; - let mut found_start = false; - let mut buf_it = buf.buf.iter().peekable(); - while let Some(frames) = buf_it.next() { - tokio::task::yield_now().await; - if !found_start { - let next_frames = buf_it.peek(); - if let Some(BcMedia::Iframe(frame)) = next_frames.and_then(|b| b.first()) { - // Get time of next IFrame - // if it is after min_time, then the - // start shuld happen between frames and next_frames - if frame.microseconds as FrameTime > min_time { - found_start = true; - } - } - } + let mut buf_it = buf.buf.iter(); - if found_start { - // We have found the start send eveythin until we get passed the - // max time - for frame in frames { - let frame_time = match frame { - BcMedia::Iframe(data) => data.microseconds as FrameTime, - BcMedia::Pframe(data) => data.microseconds as FrameTime, - _ => last_sent_time, - }; - if frame_time > min_time && frame_time <= max_time { - if let (Some(start_time), Some(end_time)) = - (buf.start_time(), buf.end_time()) - { - debug!( - "Frame Run Position: {}", - (frame_time - start_time) as f32 / (end_time - start_time) as f32 - ); - } - self.send_buffer(frame).await?; - last_sent_time = frame_time; - } else if frame_time > max_time { - return Ok(last_sent_time); - } + // Remove before start frame + buf_it + .take_while_ref(|f| match f { + BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => { + *microseconds as FrameTime <= min_time } + BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => { + *microseconds as FrameTime <= min_time + } + _ => true, + }) + .for_each(drop); + + // Send frames afterwards + let frames_to_send = buf_it.take_while(|f| match f { + BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => { + *microseconds as FrameTime <= max_time } + BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => { + *microseconds as FrameTime <= max_time + } + _ => true, + }); + + for frame in frames_to_send { + match frame { + BcMedia::Iframe(data) => { + last_sent_time = data.microseconds as FrameTime; + } + BcMedia::Pframe(data) => { + last_sent_time = data.microseconds as FrameTime; + } + _ => {} + }; + self.send_buffer(frame).await?; } Ok(last_sent_time) } @@ -728,25 +741,3 @@ impl NeoMediaSender { Ok(true) } } - -const EPS: f64 = 1e-5; -fn halflife_to_damping(halflife: f64) -> f64 { - (4.0 * std::f64::consts::LN_2) / (halflife + EPS) -} - -// fn damping_to_halflife(damping: f32) -> f32 { -// (4.0f32 * std::f32::consts::LN_2) / (damping + EPS) -// } -fn fast_negexp(x: f64) -> f64 { - 1.0 / (1.0 + x + 0.48 * x * x + 0.235 * x * x * x) -} - -fn spring_update(value: &mut f64, target: f64, velocity: &mut f64, halflife: f64, dt: f64) { - let y = halflife_to_damping(halflife) / 2.0; - let j0 = *value - target; - let j1 = *velocity + j0 * y; - let eydt = fast_negexp(y * dt); - - *value = eydt * (j0 + j1 * dt) + target; - *velocity = eydt * (*velocity - j1 * y * dt); -} diff --git a/src/rtsp/gst/server.rs b/src/rtsp/gst/server.rs index e34a671..727b2fb 100644 --- a/src/rtsp/gst/server.rs +++ b/src/rtsp/gst/server.rs @@ -16,7 +16,6 @@ use gstreamer_rtsp_server::{ RTSPAuth, RTSPFilterResult, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE, }; use log::*; -use neolink_core::bcmedia::model::*; use std::{ collections::{hash_map::Entry, HashMap, HashSet}, fs, @@ -46,7 +45,10 @@ impl NeoRtspServer { Ok(factory) } - pub(crate) async fn get_sender>(&self, tag: T) -> Option> { + pub(crate) async fn get_sender>( + &self, + tag: T, + ) -> Option> { self.imp().get_sender(tag).await } @@ -216,6 +218,26 @@ impl NeoRtspServer { } })); } + + // Clear buffers on all senders of a tag + pub(crate) async fn clear_buffer>(&self, tag: T) -> AnyResult<()> { + if let Some(sender) = self.imp().get_sender(tag).await { + sender.send(FactoryCommand::ClearBuffer).await?; + Ok(()) + } else { + Err(anyhow!("No such tag")) + } + } + + // Jump to live on all senders of a tag + pub(crate) async fn jump_to_live>(&self, tag: T) -> AnyResult<()> { + if let Some(sender) = self.imp().get_sender(tag).await { + sender.send(FactoryCommand::JumpToLive).await?; + Ok(()) + } else { + Err(anyhow!("No such tag")) + } + } } unsafe impl Send for NeoRtspServer {} @@ -264,7 +286,10 @@ impl NeoRtspServerImpl { Ok(()) } - pub(crate) async fn get_sender>(&self, tag: T) -> Option> { + pub(crate) async fn get_sender>( + &self, + tag: T, + ) -> Option> { let key = tag.into(); self.medias .read() diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index dbd7018..8db2491 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -61,11 +61,13 @@ use tokio_stream::StreamExt; mod cmdline; mod gst; +mod spring; mod states; use super::config::Config; pub(crate) use cmdline::Opt; use gst::NeoRtspServer; +pub(crate) use spring::*; use states::*; /// Entry point for the rtsp subcommand @@ -182,7 +184,7 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi let tags = loggedin.shared.get_tags(); let rtsp_thread = loggedin.get_rtsp(); - // Check if buffers are already perpared (from say previous connection) + // Check if buffers were prepared before we clear let buffers_prepared = tags .iter() .map(|tag| rtsp_thread.buffer_ready(tag)) @@ -191,6 +193,13 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi .all(|a| a) .await; + // Clear all buffers present + tags.iter() + .map(|tag| rtsp_thread.clear_buffer(tag)) + .collect::>() + .collect::>() + .await; + // Start pulling data from the camera let mut streaming = loggedin .stream() @@ -198,22 +207,28 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi .with_context(|| format!("{}: Could not start stream", name)) .map_err(CameraFailureKind::Retry)?; - if !buffers_prepared { - // Wait for buffers to be prepared - let mut waiter = tokio::time::interval(Duration::from_micros(500)); - loop { - waiter.tick().await; - if tags - .iter() - .map(|tag| rtsp_thread.buffer_ready(tag)) - .collect::>() - .all(|f| f.unwrap_or(false)) - .await - { - break; - } + // Wait for buffers to be prepared + let mut waiter = tokio::time::interval(Duration::from_micros(500)); + loop { + waiter.tick().await; + if tags + .iter() + .map(|tag| rtsp_thread.buffer_ready(tag)) + .collect::>() + .all(|f| f.unwrap_or(false)) + .await + { + break; } + } + tags.iter() + .map(|tag| rtsp_thread.jump_to_live(tag)) + .collect::>() + .collect::>() + .await; + + if !buffers_prepared { // Clear all current media and force a reconnect // This shoudld stop them from watching the "Stream Not Ready Thing" tags.iter() @@ -221,8 +236,8 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi .collect::>() .collect::>() .await; - log::info!("{}: Buffers prepared", name); } + log::info!("{}: Buffers prepared", name); loop { // Wait for error or reason to pause @@ -302,6 +317,12 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi .with_context(|| format!("{}: Error while paused", name)) .map_err(CameraFailureKind::Retry)?; + tags.iter() + .map(|tag| rtsp_thread.jump_to_live(tag)) + .collect::>() + .collect::>() + .await; + streaming = paused .stream() .await diff --git a/src/rtsp/spring.rs b/src/rtsp/spring.rs new file mode 100644 index 0000000..3725cef --- /dev/null +++ b/src/rtsp/spring.rs @@ -0,0 +1,88 @@ +//! A critically damped spring +use tokio::time::Instant; + +#[derive(Debug)] +pub(crate) struct Spring { + last_update: Instant, + target: f64, + value: f64, + velocity: f64, + halflife: f64, +} + +impl Spring { + pub(crate) fn new(value: f64, target: f64, halflife: f64) -> Self { + Self { + value, + target, + halflife, + last_update: Instant::now(), + velocity: 0.0f64, + } + } + + pub(crate) async fn update(&mut self) { + let now = Instant::now(); + let dt = now - self.last_update; + self.last_update = now; + spring_update( + &mut self.value, + self.target, + &mut self.velocity, + self.halflife, + dt.as_secs_f64(), + ); + } + + pub(crate) fn value(&self) -> f64 { + self.value + } + + // pub(crate) fn value_u32(&self) -> u32 { + // self.value.max(std::u32::MIN as f64).floor().min(std::u32::MAX as f64) as u32 + // } + pub(crate) fn value_i64(&self) -> i64 { + self.value + .max(std::i64::MIN as f64) + .floor() + .min(std::i64::MAX as f64) as i64 + } + pub(crate) fn value_u64(&self) -> u64 { + self.value + .max(std::u64::MIN as f64) + .floor() + .min(std::u64::MAX as f64) as u64 + } + + pub(crate) fn set_target(&mut self, target: f64) { + self.target = target; + } + + pub(crate) fn reset_to(&mut self, target: f64) { + self.target = target; + self.value = target; + self.velocity = 0.0; + } +} + +const EPS: f64 = 1e-5; +fn halflife_to_damping(halflife: f64) -> f64 { + (4.0 * std::f64::consts::LN_2) / (halflife + EPS) +} + +// fn damping_to_halflife(damping: f32) -> f32 { +// (4.0f32 * std::f32::consts::LN_2) / (damping + EPS) +// } +fn fast_negexp(x: f64) -> f64 { + 1.0 / (1.0 + x + 0.48 * x * x + 0.235 * x * x * x) +} + +fn spring_update(value: &mut f64, target: f64, velocity: &mut f64, halflife: f64, dt: f64) { + let y = halflife_to_damping(halflife) / 2.0; + let j0 = *value - target; + let j1 = *velocity + j0 * y; + let eydt = fast_negexp(y * dt); + + *value = eydt * (j0 + j1 * dt) + target; + *velocity = eydt * (*velocity - j1 * y * dt); +} diff --git a/src/rtsp/states/streaming.rs b/src/rtsp/states/streaming.rs index 8ec31d4..1560a41 100644 --- a/src/rtsp/states/streaming.rs +++ b/src/rtsp/states/streaming.rs @@ -11,6 +11,7 @@ use tokio::{sync::RwLock, task::JoinSet}; use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream}; use super::{camera::Camera, LoggedIn}; +use crate::rtsp::gst::FactoryCommand; pub(crate) struct Streaming { pub(crate) camera: BcCamera, @@ -68,9 +69,12 @@ impl Camera { Err(_) => trace!(" - Error"), } // debug!("Straming: Send"); - timeout(Duration::from_secs(15), sender.send(data?)) - .await - .with_context(|| "Timed out waiting to send Media Frame")??; + timeout( + Duration::from_secs(15), + sender.send(FactoryCommand::BcMedia(data?)), + ) + .await + .with_context(|| "Timed out waiting to send Media Frame")??; // debug!("Straming: Sent"); } });