From 8c6776607b93302d63340ad4087f01361ee30c79 Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Sat, 6 May 2023 07:39:51 -0500 Subject: [PATCH 01/13] Autobuild every couple of months if no other activity Builds expire after 90 days --- .github/workflows/build.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7b96f94..9eab65c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,11 @@ name: CI -on: [push, pull_request, workflow_dispatch] +on: + push: + pull_request: + schedule: + - cron: "* * * */2 *" + workflow_dispatch: jobs: native: From 4afa457eff5180750123e374a5aa685fd047a2e4 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sat, 20 May 2023 10:31:38 +0700 Subject: [PATCH 02/13] Rework pause to be per stream --- src/rtsp/mod.rs | 251 +++++++++++++++++++++++------------ src/rtsp/states/camera.rs | 1 + src/rtsp/states/loggedin.rs | 1 + src/rtsp/states/shared.rs | 6 + src/rtsp/states/streaming.rs | 167 +++++++++++++---------- 5 files changed, 273 insertions(+), 153 deletions(-) diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index dcb8ef0..3c63f7a 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -55,6 +55,8 @@ use anyhow::{anyhow, Context, Result}; use futures::stream::FuturesUnordered; use log::*; +use neolink_core::bc_protocol::{BcCamera, StreamKind}; +use std::collections::HashSet; use std::sync::Arc; use std::time::Duration; use tokio_stream::StreamExt; @@ -242,23 +244,153 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi .await; log::info!("{}: Buffers prepared", name); + let mut active_tags = streaming.shared.get_streams().clone(); + let mut motion_pause = false; loop { // Wait for error or reason to pause - tokio::select! { + let change = tokio::select! { v = async { - // Wait for error - streaming.join().await + // Wait for error + streaming.join().await } => { - info!("Join Pause"); - v + info!("{}: Join Pause", name); + Ok(StreamChange::StreamError(v)) }, + v = await_change( + streaming.get_camera(), + &streaming.shared, + &rtsp_thread, + &active_tags, + motion_pause, + &name, + ) => { + v.with_context(|| format!("{}: Error updating pause state", name)) + .map_err(CameraFailureKind::Retry) + } + }?; + + match change { + StreamChange::StreamError(res) => { + res.map_err(CameraFailureKind::Retry)?; + } + StreamChange::MotionStart => { + motion_pause = false; + let inactive_streams = streaming + .shared + .get_streams() + .iter() + .filter(|i| active_tags.contains(i)) + .copied() + .collect::>(); + + if streaming.shared.get_config().pause.on_disconnect { + // Pause on client is also on + // + // Only resume clients with active connections + for stream in inactive_streams.iter() { + if rtsp_thread + .get_number_of_clients(streaming.shared.get_tag_for_stream(stream)) + .await + .map(|n| n > 0) + .unwrap_or(false) + { + streaming + .start_stream(*stream) + .await + .map_err(CameraFailureKind::Retry)?; + active_tags.insert(*stream); + rtsp_thread + .resume(streaming.shared.get_tag_for_stream(stream)) + .await + .map_err(CameraFailureKind::Retry)?; + } + } + } else { + // Pause on client is not on + // + // Resume all + for stream in inactive_streams.iter() { + streaming + .start_stream(*stream) + .await + .map_err(CameraFailureKind::Retry)?; + active_tags.insert(*stream); + rtsp_thread + .resume(streaming.shared.get_tag_for_stream(stream)) + .await + .map_err(CameraFailureKind::Retry)?; + } + } + } + StreamChange::MotionStop => { + motion_pause = true; + // Clear all streams + for stream in active_tags.drain() { + rtsp_thread + .pause(streaming.shared.get_tag_for_stream(&stream)) + .await + .map_err(CameraFailureKind::Retry)?; + streaming + .stop_stream(stream) + .await + .map_err(CameraFailureKind::Retry)?; + } + } + StreamChange::ClientStart(stream) => { + if !streaming.shared.get_config().pause.on_motion || !motion_pause { + streaming + .start_stream(stream) + .await + .map_err(CameraFailureKind::Retry)?; + active_tags.insert(stream); + rtsp_thread + .resume(streaming.shared.get_tag_for_stream(&stream)) + .await + .map_err(CameraFailureKind::Retry)?; + } + } + StreamChange::ClientStop(stream) => { + if !streaming.shared.get_config().pause.on_motion || !motion_pause { + rtsp_thread + .pause(streaming.shared.get_tag_for_stream(&stream)) + .await + .map_err(CameraFailureKind::Retry)?; + streaming + .stop_stream(stream) + .await + .map_err(CameraFailureKind::Retry)?; + active_tags.remove(&stream); + } + } + } + } + // Ok(()) +} + +enum StreamChange { + StreamError(Result<()>), + MotionStart, + MotionStop, + ClientStart(StreamKind), + ClientStop(StreamKind), +} +async fn await_change( + camera: &BcCamera, + shared: &Shared, + rtsp_thread: &NeoRtspServer, + active_tags: &HashSet, + motion_pause: bool, + name: &str, +) -> Result { + tokio::select! { v = async { // Wait for motion stop - let mut motion = streaming.get_camera().listen_on_motion().await?; - motion.await_stop(Duration::from_secs_f64(streaming.get_config().pause.motion_timeout)).await - }, if streaming.get_config().pause.on_motion => { - info!("Motion Pause"); - v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e)) + let mut motion = camera.listen_on_motion().await?; + motion.await_stop(Duration::from_secs_f64(shared.get_config().pause.motion_timeout)).await + }, if motion_pause && shared.get_config().pause.on_motion => { + info!("{}: Motion Pause", name); + v.map_err(|e| anyhow!("Error while processing motion messages: {:?}", e))?; + Ok(StreamChange::MotionStop) }, v = async { // Wait for client to disconnect @@ -266,97 +398,44 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi loop { inter.tick().await; - let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await; - // info!("Num clients: {}", total_clients); - if total_clients == 0 { - return Ok(()) + for tag in active_tags.iter() { + if rtsp_thread.get_number_of_clients(shared.get_tag_for_stream(tag)).await.map(|n| n == 0).unwrap_or(true) { + return Result::<_,anyhow::Error>::Ok(StreamChange::ClientStop(*tag)) + } } } - }, if streaming.get_config().pause.on_disconnect => { - info!("Client Pause"); + }, if shared.get_config().pause.on_disconnect => { + if let Ok(StreamChange::ClientStop(tag)) = v.as_ref() { + info!("{}: Client Pause:: {:?}", name, tag); + } v }, - }.with_context(|| format!("{}: Error while streaming", name)) - .map_err(CameraFailureKind::Retry)?; - - tags.iter() - .map(|tag| rtsp_thread.pause(tag)) - .collect::>() - .collect::>() - .await; - - let paused = streaming - .stop() - .await - .with_context(|| format!("{}: Could not stop stream", name)) - .map_err(CameraFailureKind::Retry)?; - // Wait for reason to restart - tokio::select! { - v = async { - // Wait for motion start and client for start - let mut motion = paused.get_camera().listen_on_motion().await?; - let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01)); - loop { - motion.await_start(Duration::ZERO).await?; - let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await; - if total_clients > 0 { - return Result::<()>::Ok(()); - } - inter.tick().await; - } - }, if paused.get_config().pause.on_motion && paused.get_config().pause.on_disconnect => { - info!("Motion and Client Resume"); - v.with_context(|| "Error while processing motion/client messages") - }, v = async { // Wait for motion start - let mut motion = paused.get_camera().listen_on_motion().await?; + let mut motion = camera.listen_on_motion().await?; motion.await_start(Duration::ZERO).await - }, if paused.get_config().pause.on_motion && !paused.get_config().pause.on_disconnect => { - info!("Motion Resume"); - v.with_context(|| "Error while processing motion messages") + }, if ! motion_pause && shared.get_config().pause.on_motion => { + info!("{}: Motion Resume", name); + v.with_context(|| "Error while processing motion messages")?; + Ok(StreamChange::MotionStart) }, v = async { // Wait for client to connect let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01)); - + let inactive_tags = shared.get_streams().iter().filter(|i| active_tags.contains(i)).collect::>(); loop { inter.tick().await; - let total_clients = tags.iter().map(|tag| rtsp_thread.get_number_of_clients(tag)).collect::>().fold(0usize, |acc, f| acc + f.unwrap_or(0usize)).await; - if total_clients > 0 { - return Ok(()) + for tag in inactive_tags.iter() { + if rtsp_thread.get_number_of_clients(shared.get_tag_for_stream(tag)).await.map(|n| n > 0).unwrap_or(false) { + return Result::<_,anyhow::Error>::Ok(StreamChange::ClientStart(**tag)) + } } } - }, if paused.get_config().pause.on_disconnect && !paused.get_config().pause.on_motion => { - info!("Client Resume"); + }, if shared.get_config().pause.on_disconnect => { + if let Ok(StreamChange::ClientStart(tag)) = v.as_ref() { + info!("{}: Client Resume:: {:?}", name, tag); + } v }, - else => { - // No pause. This means that the stream stopped for some reason - // but not because of an error - info!("Generic Resume"); - Ok(()) - } - } - .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 - .with_context(|| format!("{}: Could not start stream", name)) - .map_err(CameraFailureKind::Retry)?; - - tags.iter() - .map(|tag| rtsp_thread.resume(tag)) - .collect::>() - .collect::>() - .await; - } + }.with_context(|| format!("{}: Error while streaming", name)) } diff --git a/src/rtsp/states/camera.rs b/src/rtsp/states/camera.rs index a5936a1..de11c92 100644 --- a/src/rtsp/states/camera.rs +++ b/src/rtsp/states/camera.rs @@ -15,6 +15,7 @@ impl Camera { self.shared.config.name.clone() } + #[allow(dead_code)] pub(crate) fn get_config(&self) -> &CameraConfig { &self.shared.config } diff --git a/src/rtsp/states/loggedin.rs b/src/rtsp/states/loggedin.rs index fef2b53..84c90a7 100644 --- a/src/rtsp/states/loggedin.rs +++ b/src/rtsp/states/loggedin.rs @@ -93,6 +93,7 @@ impl Camera { Ok(()) } + #[allow(dead_code)] pub(crate) fn get_camera(&self) -> &BcCamera { &self.state.camera } diff --git a/src/rtsp/states/shared.rs b/src/rtsp/states/shared.rs index 22409f5..9c5e782 100644 --- a/src/rtsp/states/shared.rs +++ b/src/rtsp/states/shared.rs @@ -102,6 +102,12 @@ impl Shared { .map(|k| self.get_tag_for_stream(k)) .collect() } + pub(crate) fn get_streams(&self) -> &HashSet { + &self.streams + } + pub(crate) fn get_config(&self) -> &CameraConfig { + &self.config + } pub(crate) fn get_tag_for_stream(&self, stream: &Stream) -> String { format!("{}::{:?}", self.config.name, stream) } diff --git a/src/rtsp/states/streaming.rs b/src/rtsp/states/streaming.rs index 44b53f6..01dbf11 100644 --- a/src/rtsp/states/streaming.rs +++ b/src/rtsp/states/streaming.rs @@ -2,11 +2,16 @@ // // Data is streamed into a gstreamer source +use std::collections::HashMap; + +use futures::stream::{FuturesUnordered, StreamExt}; + use anyhow::{anyhow, Context, Error, Result}; use log::*; use neolink_core::bcmedia::model::BcMedia; +use tokio::sync::RwLock; +use tokio::task::JoinHandle; use tokio::time::{timeout, Duration}; -use tokio::{sync::RwLock, task::JoinSet}; use neolink_core::bc_protocol::{BcCamera, StreamKind as Stream}; @@ -15,7 +20,7 @@ use crate::rtsp::gst::FactoryCommand; pub(crate) struct Streaming { pub(crate) camera: BcCamera, - set: RwLock>>, + stream_handles: RwLock>>>, } impl Camera { @@ -23,74 +28,98 @@ impl Camera { // Create new gst outputs // // Otherwise use those already present - let mut set: JoinSet> = Default::default(); - for stream in loggedin.shared.streams.iter() { - let tag = loggedin.shared.get_tag_for_stream(stream); - let sender = loggedin - .shared - .rtsp - .get_sender(&tag) - .await - .ok_or_else(|| anyhow!("Stream has not been created"))?; - - let stream_display_name = match stream { - Stream::Main => "Main Stream (Clear)", - Stream::Sub => "Sub Stream (Fluent)", - Stream::Extern => "Extern Stream (Balanced)", - }; - info!( - "{}: Starting video stream {}", - &loggedin.shared.config.name, stream_display_name - ); - - let stream_thead = *stream; - let strict_thread = loggedin.shared.config.strict; - let tag_thread = tag.clone(); - let mut stream_data = loggedin - .state - .camera - .start_video(stream_thead, 0, strict_thread) - .await?; - set.spawn(async move { - loop { - tokio::task::yield_now().await; - // debug!("Straming: Get"); - let data = timeout(Duration::from_secs(15), stream_data.get_data()) - .await - .with_context(|| "Timed out waiting for new Media Frame")??; - // debug!("Straming: Got"); - match &data { - Ok(BcMedia::InfoV1(_)) => trace!("{}: - InfoV1", &tag_thread), - Ok(BcMedia::InfoV2(_)) => trace!("{}: - InfoV2", &tag_thread), - Ok(BcMedia::Iframe(_)) => trace!("{}: - Iframe", &tag_thread), - Ok(BcMedia::Pframe(_)) => trace!("{}: - Pframe", &tag_thread), - Ok(BcMedia::Aac(_)) => trace!("{}: - Aac", &tag_thread), - Ok(BcMedia::Adpcm(_)) => trace!("{}: - Adpcm", &tag_thread), - Err(_) => trace!(" - Error"), - } - // debug!("Straming: Send"); - timeout( - Duration::from_secs(15), - sender.send(FactoryCommand::BcMedia(data?)), - ) - .await - .with_context(|| "Timed out waiting to send Media Frame")??; - // debug!("Straming: Sent"); - } - }); - } - - Ok(Camera { + let mut me = Camera { shared: loggedin.shared, state: Streaming { camera: loggedin.state.camera, - set: RwLock::new(set), + stream_handles: Default::default(), }, - }) + }; + + for stream in me.shared.streams.iter().copied().collect::>() { + me.start_stream(stream).await?; + } + + Ok(me) } + pub(crate) async fn start_stream(&mut self, stream: Stream) -> Result<()> { + if let Some(handle) = self.state.stream_handles.get_mut().get(&stream) { + if !handle.is_finished() { + return Ok(()); + } + } + let tag = self.shared.get_tag_for_stream(&stream); + let sender = self + .shared + .rtsp + .get_sender(&tag) + .await + .ok_or_else(|| anyhow!("Stream has not been created"))?; + + let stream_display_name = match stream { + Stream::Main => "Main Stream (Clear)", + Stream::Sub => "Sub Stream (Fluent)", + Stream::Extern => "Extern Stream (Balanced)", + }; + info!( + "{}: Starting video stream {}", + &self.shared.config.name, stream_display_name + ); + + let stream_thead = stream; + let strict_thread = self.shared.config.strict; + let tag_thread = tag.clone(); + let mut stream_data = self + .state + .camera + .start_video(stream_thead, 0, strict_thread) + .await?; + let handle = tokio::task::spawn(async move { + loop { + tokio::task::yield_now().await; + // debug!("Straming: Get"); + let data = timeout(Duration::from_secs(15), stream_data.get_data()) + .await + .with_context(|| "Timed out waiting for new Media Frame")??; + // debug!("Straming: Got"); + match &data { + Ok(BcMedia::InfoV1(_)) => trace!("{}: - InfoV1", &tag_thread), + Ok(BcMedia::InfoV2(_)) => trace!("{}: - InfoV2", &tag_thread), + Ok(BcMedia::Iframe(_)) => trace!("{}: - Iframe", &tag_thread), + Ok(BcMedia::Pframe(_)) => trace!("{}: - Pframe", &tag_thread), + Ok(BcMedia::Aac(_)) => trace!("{}: - Aac", &tag_thread), + Ok(BcMedia::Adpcm(_)) => trace!("{}: - Adpcm", &tag_thread), + Err(_) => trace!(" - Error"), + } + // debug!("Straming: Send"); + timeout( + Duration::from_secs(15), + sender.send(FactoryCommand::BcMedia(data?)), + ) + .await + .with_context(|| "Timed out waiting to send Media Frame")??; + // debug!("Straming: Sent"); + } + }); + self.state.stream_handles.get_mut().insert(stream, handle); + Ok(()) + } + + pub(crate) async fn stop_stream(&mut self, stream: Stream) -> Result<()> { + if let Some(handle) = self.state.stream_handles.get_mut().remove(&stream) { + handle.abort(); + } + Ok(()) + } + + #[allow(dead_code)] pub(crate) async fn stop(self) -> Result> { - self.state.set.into_inner().abort_all(); + self.state + .stream_handles + .into_inner() + .into_values() + .for_each(|h| h.abort()); Ok(Camera { shared: self.shared, state: LoggedIn { @@ -102,15 +131,19 @@ impl Camera { pub(crate) async fn join(&self) -> Result<()> { tokio::select! { v = async { - let mut locked_threads = self.state.set.write().await; - while let Some(res) = locked_threads.join_next().await { + let mut locked_threads = self.state.stream_handles.write().await; + let mut thread_joins = locked_threads.iter_mut().map(|(_, t)| t).collect::>(); + + while let Some(res) = thread_joins.next().await { match res { Err(e) => { - locked_threads.abort_all(); + drop(thread_joins); + locked_threads.iter_mut().for_each(|(_,h)| h.abort()); return Err(e.into()); } Ok(Err(e)) => { - locked_threads.abort_all(); + drop(thread_joins); + locked_threads.iter_mut().for_each(|(_,h)| h.abort()); return Err(e); } Ok(Ok(())) => {} From f42191973ae8f2dfa4ac60a7bb6e5e495fc0ab3d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 22 May 2023 13:32:15 +0700 Subject: [PATCH 03/13] Seperate quality stream pausing --- src/rtsp/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 3c63f7a..43f7af0 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -252,7 +252,7 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi v = async { // Wait for error streaming.join().await - } => { + }, if ! active_tags.is_empty() => { info!("{}: Join Pause", name); Ok(StreamChange::StreamError(v)) }, @@ -263,7 +263,7 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi &active_tags, motion_pause, &name, - ) => { + ), if streaming.shared.get_config().pause.on_motion || streaming.shared.get_config().pause.on_disconnect => { v.with_context(|| format!("{}: Error updating pause state", name)) .map_err(CameraFailureKind::Retry) } @@ -279,7 +279,7 @@ async fn camera_main(camera: Camera) -> Result<(), CameraFailureKi .shared .get_streams() .iter() - .filter(|i| active_tags.contains(i)) + .filter(|i| !active_tags.contains(i)) .copied() .collect::>(); @@ -422,7 +422,8 @@ async fn await_change( v = async { // Wait for client to connect let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01)); - let inactive_tags = shared.get_streams().iter().filter(|i| active_tags.contains(i)).collect::>(); + let inactive_tags = shared.get_streams().iter().filter(|i| !active_tags.contains(i)).collect::>(); + trace!("inactive_tags: {:?}", inactive_tags); loop { inter.tick().await; for tag in inactive_tags.iter() { From cf07304baeb4ad653ed25ce8ee15e23e98877784 Mon Sep 17 00:00:00 2001 From: Kevin David Date: Sat, 27 May 2023 13:39:28 -0400 Subject: [PATCH 04/13] Allow docker username to be specified, only use one token --- .github/workflows/build.yml | 21 ++++++++++++++++++--- .github/workflows/dockerprune.yml | 6 +++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7271b04..06f3a7c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -195,8 +195,23 @@ jobs: uses: docker/login-action@v2 with: registry: docker.io - username: ${{ steps.string_user.outputs.lowercase }} + username: ${{ vars.DOCKER_USERNAME || steps.string_user.outputs.lowercase }} password: ${{ secrets.DOCKER_TOKEN }} + - name: Set Docker name with owner (package name) depending on if DOCKER_USERNAME is set + if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }} + id: docker_repo + shell: bash + run: | + if [ -z $DOCKER_USERNAME ]; then + echo "DOCKER_NWO=${GITHUB_NWO}" >> $GITHUB_OUTPUT + else + IFS='/' read -ra NWO_SPLIT <<< "$GITHUB_NWO" + REPO_NAME=${NWO_SPLIT[1]} + echo "DOCKER_NWO=${DOCKER_USERNAME}/${REPO_NAME}" >> $GITHUB_OUTPUT + fi + env: + DOCKER_USERNAME: ${{ vars.DOCKER_USERNAME }} + GITHUB_NWO: ${{ steps.string_repo.outputs.lowercase }} - name: Get tag name if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }} id: tags @@ -226,7 +241,7 @@ jobs: echo "TAGS=${tagstr}" echo "TAGS=${tagstr}" >> "${GITHUB_OUTPUT}" env: - REPO_NAME: ${{ steps.string_repo.outputs.lowercase }} + REPO_NAME: ${{ steps.docker_repo.outputs.DOCKER_NWO }} - name: Install latest rust run: | rustup toolchain install stable @@ -272,7 +287,7 @@ jobs: push: true file: Dockerfile tags: ${{ steps.tags.outputs.TAGS }} - build-args: VERSION=${{ steps.toml.outputs.version }}, REPO=${{ github.repository }}, OWNER=${{ github.repository_owner }} + build-args: VERSION=${{ steps.toml.outputs.version }}, REPO=${{ github.repository }}, OWNER=${{ vars.DOCKER_USERNAME || github.repository_owner }} env: DOCKER_BUILDKIT: 1 diff --git a/.github/workflows/dockerprune.yml b/.github/workflows/dockerprune.yml index d932958..3c339dd 100644 --- a/.github/workflows/dockerprune.yml +++ b/.github/workflows/dockerprune.yml @@ -31,7 +31,7 @@ jobs: if [ -n $SECRET ]; then HAS_SECRET='true' ; fi echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT env: - SECRET: "${{ secrets.DOCKER_PRUNE_TOKEN }}" + SECRET: "${{ secrets.DOCKER_TOKEN }}" - name: Convert username to lower case for docker id: string_user if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }} @@ -143,5 +143,5 @@ jobs: env: DAYS: "100" REPO_NAME: ${{ steps.string_repo.outputs.lowercase }} - OWNER: ${{ steps.string_user.outputs.lowercase }} - TOKEN: ${{ secrets.DOCKER_PRUNE__TOKEN }} \ No newline at end of file + OWNER: ${{ vars.DOCKER_USERNAME || steps.string_user.outputs.lowercase }} + TOKEN: ${{ secrets.DOCKER_TOKEN }} \ No newline at end of file From a59dafab006c6a9b19bd19b6efad6ba2ae5620da Mon Sep 17 00:00:00 2001 From: Kevin David Date: Sun, 28 May 2023 16:41:30 -0400 Subject: [PATCH 05/13] Switch back to distinct `DOCKER_PRUNE_TOKEN` --- .github/workflows/dockerprune.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dockerprune.yml b/.github/workflows/dockerprune.yml index 3c339dd..b663cf2 100644 --- a/.github/workflows/dockerprune.yml +++ b/.github/workflows/dockerprune.yml @@ -31,7 +31,7 @@ jobs: if [ -n $SECRET ]; then HAS_SECRET='true' ; fi echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT env: - SECRET: "${{ secrets.DOCKER_TOKEN }}" + SECRET: "${{ secrets.DOCKER_PRUNE_TOKEN }}" - name: Convert username to lower case for docker id: string_user if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }} @@ -144,4 +144,4 @@ jobs: DAYS: "100" REPO_NAME: ${{ steps.string_repo.outputs.lowercase }} OWNER: ${{ vars.DOCKER_USERNAME || steps.string_user.outputs.lowercase }} - TOKEN: ${{ secrets.DOCKER_TOKEN }} \ No newline at end of file + TOKEN: ${{ secrets.DOCKER_PRUNE_TOKEN }} \ No newline at end of file From f7c16bf5a3fed147263e7dd911caa6b1b3935c7b Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 5 Jun 2023 07:53:25 +0700 Subject: [PATCH 06/13] Allow zero buffer and remove buffer refill --- crates/core/src/bc_protocol/ptz.rs | 12 +- src/rtsp/gst/sender.rs | 174 ++++------------------------- 2 files changed, 27 insertions(+), 159 deletions(-) diff --git a/crates/core/src/bc_protocol/ptz.rs b/crates/core/src/bc_protocol/ptz.rs index 0e08843..a90fb6e 100644 --- a/crates/core/src/bc_protocol/ptz.rs +++ b/crates/core/src/bc_protocol/ptz.rs @@ -139,12 +139,6 @@ impl BcCamera { let mut sub_set = connection.subscribe(msg_num).await?; let command = if name.is_some() { "setPos" } else { "toPos" }; - let preset = Preset { - id: preset_id, - name, - command: Some(command.to_string()), - ..Default::default() - }; let send = Bc { meta: BcMeta { msg_id: MSG_ID_PTZ_CONTROL_PRESET, @@ -163,7 +157,11 @@ impl BcCamera { payload: Some(BcPayloads::BcXml(BcXml { ptz_preset: Some(PtzPreset { preset_list: Some(PresetList { - preset: vec![preset], + preset: vec![Preset { + id: preset_id, + name, + command: Some(command.to_string()), + }], }), ..Default::default() }), diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index ad4ccc3..0bc2f33 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -43,6 +43,12 @@ struct NeoBuffer { max_size: usize, } +/// The historical buffer is how much we keep extra after live. +/// +/// This represents the upper limit we keep in storage after live +/// while waiting for the time to play arrives +const HISTORICAL_BUFFER_SIZE: usize = 100; + impl NeoBuffer { fn new(max_size: usize) -> Self { Self { @@ -98,65 +104,21 @@ impl NeoBuffer { last.data.push(media); } - while self.buf.len() > self.max_size { + while self.buf.len() > self.max_size + HISTORICAL_BUFFER_SIZE { if self.buf.pop_front().is_none() { break; } } } - fn prep_size(&self) -> usize { - self.max_size * 2 / 3 - } - fn live_size(&self) -> usize { - self.max_size / 3 + self.max_size } pub(crate) fn ready(&self) -> bool { - self.buf.len() > self.prep_size() + self.buf.len() >= self.live_size() } - pub(crate) fn ready_play(&self) -> bool { - self.buf.len() > self.live_size() - } - - // 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 { - // 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 { - // let (fronts, backs) = self.buf.as_slices(); - // fronts - // .iter() - // .chain(&mut backs.iter()) - // .map(|frame| frame.time) - // .next() - // } - fn end_time(&self) -> Option { let (fronts, backs) = self.buf.as_slices(); backs @@ -166,35 +128,6 @@ impl NeoBuffer { .map(|frame| frame.time) .next() } - - // fn min_time(&self) -> Option { - // let (fronts, backs) = self.buf.as_slices(); - // fronts - // .iter() - // .chain(&mut backs.iter()) - // .flat_map(|frame| match Arc::as_ref(frame) { - // BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => Some(*microseconds), - // BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => Some(*microseconds), - // _ => None, - // }) - // .min() - // .map(|i| i as FrameTime) - // } - - // fn max_time(&self) -> Option { - // let (fronts, backs) = self.buf.as_slices(); - // backs - // .iter() - // .rev() - // .chain(&mut fronts.iter().rev()) - // .flat_map(|frame| match Arc::as_ref(frame) { - // BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => Some(*microseconds), - // BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => Some(*microseconds), - // _ => None, - // }) - // .max() - // .map(|i| i as FrameTime) - // } } pub(super) struct NeoMediaSenders { @@ -288,52 +221,9 @@ impl NeoMediaSenders { async fn handle_new_data(&mut self, data: BcMedia) -> AnyResult<()> { // trace!("Handle new data"); self.update_mediatypes(&data).await; - let time = match &data { - BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => *microseconds as FrameTime, - BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => *microseconds as FrameTime, - _ => self.buffer.end_time().unwrap_or(0), - }; let data = Arc::new(data); - let end_time = self.buffer.end_time(); - let frame_time = time; - // Ocassionally the camera will make a jump in timestamps of about 15s (on sub 9s on main) - // This could mean that it runs on some fixed sized buffer - if let Some(end_time) = end_time { - let delta_frame = (self.buffer.buf.back().unwrap().time - - self.buffer.buf.front().unwrap().time) - / self.buffer.buf.len() as i64; - let delta_time = frame_time - end_time - delta_frame; - let delta_duration = Duration::from_micros(delta_time.unsigned_abs()); - if delta_duration > Duration::from_secs(1) { - trace!( - "Reschedule buffer due to jump: {:?}, Prev: {}, New: {}", - delta_duration, - end_time, - frame_time - ); - trace!("Adjusting master: {}", self.buffer.buf.len()); - for frame in self.buffer.buf.iter_mut() { - let old_frame_time = frame.time; - frame.time = frame.time.saturating_add(delta_time); - trace!( - " - New frame time: {} -> {} (target {})", - old_frame_time, - frame.time, - frame_time - ); - } - - for (_, client) in self.client_data.iter_mut() { - for frame in client.buffer.buf.iter_mut() { - frame.time = frame.time.saturating_add(delta_time); - } - client.start_time.mod_value(delta_time as f64); - } - } - } - for client_data in self.client_data.values_mut() { client_data.add_data(data.clone()).await?; } @@ -465,9 +355,6 @@ impl NeoMediaSenders { }, 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 let _ = client.jump_to_live().await; } }, @@ -512,7 +399,6 @@ pub(super) struct NeoMediaSender { command_sender: Sender, inited: bool, playing: bool, - refilling: bool, use_smoothing: bool, } @@ -529,7 +415,6 @@ impl NeoMediaSender { command_sender: tx, inited: false, playing: true, - refilling: false, use_smoothing, } } @@ -687,7 +572,7 @@ impl NeoMediaSender { self.start_time.reset_to(target_time as f64); // Send preprocess now - self.send_buffers(preprocess.as_slice()).await?; + self.send_buffers(preprocess.as_slice(), true).await?; trace!("Preprocessed"); // Send these later for frame in buffer.drain(..) { @@ -741,18 +626,9 @@ impl NeoMediaSender { debug!("Buffer overfull"); self.jump_to_live().await?; } - if self.refilling && self.buffer.ready_play() { - self.refilling = false; - self.jump_to_live().await?; - } else if self.refilling { - trace!( - "Refilling: {}/{} ({:.2}%)", - self.buffer.buf.len(), - self.buffer.live_size(), - self.buffer.buf.len() as f32 / (self.buffer.live_size()) as f32 * 100.0 - ); - } else if !self.refilling && self.inited && self.playing { + if self.inited && self.playing { self.update_starttime().await?; + let ignore_stamps = self.buffer.live_size() == 0; // Check app src is live if !self .vid @@ -763,17 +639,6 @@ impl NeoMediaSender { return Err(anyhow!("Vid src is closed")); } - // Check if buffers are ok - if self.buffer.buf.len() <= self.buffer.max_size / 10 { - warn!( - "Buffer exhausted. Not enough data from Camera. Pausing RTSP until refilled." - ); - info!("Try reducing your Max Bitrate using the offical app"); - self.refilling = true; - } else { - trace!("Buffer size: {}", self.buffer.buf.len()); - } - // Send buffers let mut buffers = vec![]; if let Some(buftime) = self.get_buftime() { @@ -785,7 +650,6 @@ impl NeoMediaSender { .map(|data| data.time <= buftime) .unwrap_or(false) { - tokio::task::yield_now().await; match self.buffer.buf.pop_front() { Some(frame) => { buffers.push(frame); @@ -797,13 +661,13 @@ impl NeoMediaSender { tokio::task::yield_now().await; // collect certain frames - self.send_buffers(&buffers).await?; + self.send_buffers(&buffers, ignore_stamps).await?; } Ok(()) } - async fn send_buffers(&mut self, medias: &[Stamped]) -> AnyResult<()> { + async fn send_buffers(&mut self, medias: &[Stamped], ignore_stamps: bool) -> AnyResult<()> { if medias.is_empty() { return Ok(()); } @@ -852,7 +716,10 @@ impl NeoMediaSender { let buffers_ref = buffers.get_mut().unwrap(); for (time, buf) in vid_buffers.drain(..) { tokio::task::yield_now().await; - let runtime = self.buftime_to_runtime(time); + let runtime = match (self.get_runtime(), ignore_stamps) { + (None, _) | (_, false) => self.buftime_to_runtime(time), + (Some(runtime), true) => runtime, + }; trace!( " - Sending vid frame at time {} ({:?} Expect: {:?})", time, @@ -913,7 +780,10 @@ impl NeoMediaSender { let buffers_ref = buffers.get_mut().unwrap(); for (time, buf) in aud_buffers.drain(..) { tokio::task::yield_now().await; - let runtime = self.buftime_to_runtime(time); + let runtime = match (self.get_runtime(), ignore_stamps) { + (None, _) | (_, false) => self.buftime_to_runtime(time), + (Some(runtime), true) => runtime, + }; let gst_buf = { let mut gst_buf = From a165377d7aaeb71a6a21d969121b3999574ab372 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 5 Jun 2023 07:58:16 +0700 Subject: [PATCH 07/13] Not smoothing when no buffer --- src/rtsp/gst/sender.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index 0bc2f33..cc913ed 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -415,7 +415,7 @@ impl NeoMediaSender { command_sender: tx, inited: false, playing: true, - use_smoothing, + use_smoothing: use_smoothing && buffer_size > 0, } } From 73e828f940099e06e1c1e5589a2bc1981705d645 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 6 Jun 2023 10:45:58 +0700 Subject: [PATCH 08/13] Restore stream timestamp stitching for the pause --- src/config.rs | 2 +- src/rtsp/gst/sender.rs | 62 +++++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index 2cfd870..3af8eee 100644 --- a/src/config.rs +++ b/src/config.rs @@ -104,7 +104,7 @@ pub(crate) struct CameraConfig { pub(crate) update_time: bool, #[validate(range( - min = 10, + min = 0, max = 500, message = "Invalid buffer size", code = "buffer_size" diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index cc913ed..b4bc9b9 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -224,6 +224,57 @@ impl NeoMediaSenders { let data = Arc::new(data); + let end_time = self.buffer.end_time(); + let frame_time = match data.as_ref() { + BcMedia::Iframe(BcMediaIframe { microseconds, .. }) => *microseconds as FrameTime, + BcMedia::Pframe(BcMediaPframe { microseconds, .. }) => *microseconds as FrameTime, + _ => self.buffer.end_time().unwrap_or(0), + }; + // If there is a jump in the time stamps for any reason + // (including a resume from a pause) + // Then we stitch the old and new frames together without + // waiting for the time to elapse + if let Some(end_time) = end_time { + // An approximate fps + let delta_frame = (self.buffer.buf.back().unwrap().time + - self.buffer.buf.front().unwrap().time) + / self.buffer.buf.len() as i64; + + // Shift current frames backwards from the new start - approx fps + let delta_time = frame_time - end_time - delta_frame; + let delta_duration = Duration::from_micros(delta_time.unsigned_abs()); + + // If that shift >1s then we stitch it together + if delta_duration > Duration::from_secs(1) { + trace!( + "Reschedule buffer due to jump: {:?}, Prev: {}, New: {}", + delta_duration, + end_time, + frame_time + ); + trace!("Adjusting master: {}", self.buffer.buf.len()); + for frame in self.buffer.buf.iter_mut() { + let old_frame_time = frame.time; + frame.time = frame.time.saturating_add(delta_time); + trace!( + " - New frame time: {} -> {} (target {})", + old_frame_time, + frame.time, + frame_time + ); + } + + for (_, client) in self.client_data.iter_mut() { + for frame in client.buffer.buf.iter_mut() { + frame.time = frame.time.saturating_add(delta_time); + } + // We also move the start time up so that the new frames will play + // with the stiching + client.start_time.mod_value(delta_time as f64); + } + } + } + for client_data in self.client_data.values_mut() { client_data.add_data(data.clone()).await?; } @@ -453,12 +504,7 @@ impl NeoMediaSender { } } else { trace!("Not enough timestamps for target live: {:?}", stamps); - if let Some(st) = stamps.first() { - trace!("Setting to 1s behind first frame in buffer"); - Some(st - Duration::from_secs(1).as_micros() as FrameTime) - } else { - None - } + None } } @@ -622,10 +668,6 @@ impl NeoMediaSender { } async fn update(&mut self) -> AnyResult<()> { - if self.buffer.buf.len() >= self.buffer.max_size * 9 / 10 { - debug!("Buffer overfull"); - self.jump_to_live().await?; - } if self.inited && self.playing { self.update_starttime().await?; let ignore_stamps = self.buffer.live_size() == 0; From 9e981df990ade9a2d7bf66934a9204ac68d3f465 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 6 Jun 2023 10:52:45 +0700 Subject: [PATCH 09/13] Fixes for zero buffer config --- src/rtsp/gst/sender.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index b4bc9b9..b223868 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -489,6 +489,10 @@ impl NeoMediaSender { fn target_live_for(buffer: &NeoBuffer) -> Option { let target_idx = buffer.live_size(); + if target_idx == 0 { + // No buffer needed grab the last + return buffer.buf.back().map(|item| item.time); + } let stamps = buffer.buf.iter().map(|item| item.time).collect::>(); if stamps.len() >= target_idx { From 60638441aaa8cea09f0d4448b51d0b7134efa438 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 6 Jun 2023 10:57:39 +0700 Subject: [PATCH 10/13] Reduce default buffer size --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 3af8eee..5b23831 100644 --- a/src/config.rs +++ b/src/config.rs @@ -260,7 +260,7 @@ fn default_smoothing() -> bool { } fn default_buffer_size() -> usize { - 100 + 25 } pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"]; From a08019bc0943a33b8a707ce93edd14ca0e7c10e2 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 6 Jun 2023 11:07:43 +0700 Subject: [PATCH 11/13] Ensure some buffer for pause before we declare ready --- src/rtsp/gst/sender.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index b223868..1fee3fb 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -116,7 +116,7 @@ impl NeoBuffer { } pub(crate) fn ready(&self) -> bool { - self.buf.len() >= self.live_size() + self.buf.len() >= self.live_size() + HISTORICAL_BUFFER_SIZE / 2 } fn end_time(&self) -> Option { @@ -623,12 +623,12 @@ impl NeoMediaSender { // Send preprocess now self.send_buffers(preprocess.as_slice(), true).await?; - trace!("Preprocessed"); + trace!("Preprocessed: {}", preprocess.len()); // Send these later for frame in buffer.drain(..) { self.buffer.buf.push_back(frame); } - trace!("Buffer filled"); + trace!("Buffer filled: {}", self.buffer.buf.len()); self.jump_to_live().await?; } else { From dee305710c2445a393394945dfebddcf791ecf75 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 6 Jun 2023 11:23:01 +0700 Subject: [PATCH 12/13] Not on a cron --- .github/workflows/build.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4b7f308..9f6d3f8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,8 +3,6 @@ name: CI on: push: pull_request: - schedule: - - cron: "* * * */2 *" workflow_dispatch: jobs: From de0528b1b871179cbf1543a5be856f98baf1bde7 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 6 Jun 2023 11:39:03 +0700 Subject: [PATCH 13/13] Set pts and dts --- src/rtsp/gst/sender.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index 1fee3fb..58af684 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -783,6 +783,7 @@ impl NeoMediaSender { runtime.try_into().unwrap(), ); gst_buf_mut.set_dts(time); + gst_buf_mut.set_pts(time); let mut gst_buf_data = gst_buf_mut.map_writable().unwrap(); gst_buf_data.copy_from_slice(buf.as_slice()); @@ -841,6 +842,7 @@ impl NeoMediaSender { runtime.try_into().unwrap(), ); gst_buf_mut.set_dts(time); + gst_buf_mut.set_pts(time); let mut gst_buf_data = gst_buf_mut.map_writable().unwrap(); gst_buf_data.copy_from_slice(buf.as_slice());