diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7271b04..9f6d3f8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,9 @@ name: CI -on: [push, pull_request, workflow_dispatch] +on: + push: + pull_request: + workflow_dispatch: jobs: pre_job: @@ -195,8 +198,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 +244,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 +290,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..b663cf2 100644 --- a/.github/workflows/dockerprune.yml +++ b/.github/workflows/dockerprune.yml @@ -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_PRUNE_TOKEN }} \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 2cfd870..5b23831 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" @@ -260,7 +260,7 @@ fn default_smoothing() -> bool { } fn default_buffer_size() -> usize { - 100 + 25 } pub(crate) static RESERVED_NAMES: &[&str] = &["anyone", "anonymous"]; diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index ad4ccc3..58af684 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() + HISTORICAL_BUFFER_SIZE / 2 } - 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,24 +221,30 @@ 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 + 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: {}", @@ -329,6 +268,8 @@ impl NeoMediaSenders { 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); } } @@ -465,9 +406,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 +450,6 @@ pub(super) struct NeoMediaSender { command_sender: Sender, inited: bool, playing: bool, - refilling: bool, use_smoothing: bool, } @@ -529,8 +466,7 @@ impl NeoMediaSender { command_sender: tx, inited: false, playing: true, - refilling: false, - use_smoothing, + use_smoothing: use_smoothing && buffer_size > 0, } } @@ -553,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 { @@ -568,12 +508,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 } } @@ -687,13 +622,13 @@ impl NeoMediaSender { self.start_time.reset_to(target_time as f64); // Send preprocess now - self.send_buffers(preprocess.as_slice()).await?; - trace!("Preprocessed"); + self.send_buffers(preprocess.as_slice(), true).await?; + 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 { @@ -737,22 +672,9 @@ 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.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 +685,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 +696,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 +707,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 +762,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, @@ -870,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()); @@ -913,7 +827,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 = @@ -925,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()); diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index dcb8ef0..43f7af0 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 - } => { - info!("Join Pause"); - v + // Wait for error + streaming.join().await + }, if ! active_tags.is_empty() => { + info!("{}: Join Pause", name); + Ok(StreamChange::StreamError(v)) }, + v = await_change( + streaming.get_camera(), + &streaming.shared, + &rtsp_thread, + &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) + } + }?; + + 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,45 @@ 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::>(); + trace!("inactive_tags: {:?}", inactive_tags); 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(())) => {}