diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 4c53488..7baab61 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -2,7 +2,10 @@ use super::{BcCamera, BinarySubscriber, Result}; use crate::{ bc::{model::*, xml::*}, bcmedia::model::*, - Never, +}; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, }; /// Convience type for the error raised by the [StreamOutput] trait @@ -46,7 +49,12 @@ impl BcCamera { /// /// This will block forever or return an error when the camera connection is dropped /// - pub fn start_video(&self, data_outs: &mut Outputs, stream: Stream) -> Result + pub fn start_video( + &self, + data_outs: &mut Outputs, + stream: Stream, + abort_handle: Arc, + ) -> Result<()> where Outputs: StreamOutput, { @@ -117,11 +125,14 @@ impl BcCamera { let mut media_sub = BinarySubscriber::from_bc_sub(&sub_video); - loop { + while !abort_handle.load(Ordering::Relaxed) { let bc_media = BcMedia::deserialize(&mut media_sub)?; // We now have a complete interesting packet. Send it to on the callback data_outs.write(bc_media)?; } + + // Aborted + Ok(()) } /// Stop a camera from sending more stream data. diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 9ca3728..36164a1 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -31,9 +31,11 @@ use anyhow::{Context, Result}; use log::*; use neolink_core::bc_protocol::{BcCamera, Stream}; -use neolink_core::Never; use std::collections::HashSet; -use std::sync::{Arc, Mutex}; +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, Mutex, +}; use std::time::Duration; // mod adpcm; @@ -119,14 +121,14 @@ fn camera_loop( camera_config: &CameraConfig, end_points: Vec<(Stream, Arc>)>, manage: bool, -) -> Result { +) -> Result<()> { let min_backoff = Duration::from_secs(1); let max_backoff = Duration::from_secs(15); let mut current_backoff = min_backoff; loop { let cam_err = camera_main(camera_config, &end_points, manage).unwrap_err(); - for (_, outputs) in end_points { + for (_, outputs) in end_points.iter() { (*outputs.lock().unwrap()).vidsrc.on_stream_error(); (*outputs.lock().unwrap()).audsrc.on_stream_error(); } @@ -207,9 +209,9 @@ fn get_permitted_users<'a>( fn camera_main( camera_config: &CameraConfig, - end_points: &Vec<(Stream, Arc>)>, + end_points: &[(Stream, Arc>)], manage: bool, -) -> Result { +) -> Result<(), CameraErr> { let mut connected = false; let mut login_fail = false; (|| { @@ -251,10 +253,11 @@ fn camera_main( do_camera_management(&mut camera, camera_config).context("Failed to manage the camera settings")?; } - let arc_camera_results: Arc>>> = Default::default(); + let arc_camera_results: Arc>>> = Default::default(); let arc_camera = Arc::new(camera); crossbeam::scope(|s| { + let arc_abort_handle = Arc::new(AtomicBool::new(false)); for (stream_name, arc_outputs) in end_points.iter() { let stream_display_name = match stream_name { Stream::Main => "Main Stream (Clear)", @@ -262,7 +265,6 @@ fn camera_main( Stream::Extern => "Extern Stream (Balanced)", }; - info!( "{}: Starting video stream {}", camera_config.name, stream_display_name @@ -271,20 +273,21 @@ fn camera_main( let camera_results = arc_camera_results.clone(); let camera = arc_camera.clone(); let outputs = arc_outputs.clone(); + let abort_handle = arc_abort_handle.clone(); s.spawn(move |_| { - let camera_result = camera.start_video(&mut *outputs.lock().unwrap(), *stream_name).with_context(|| format!("Error while streaming {}", camera_config.name)); + let camera_result = camera.start_video(&mut *outputs.lock().unwrap(), *stream_name, abort_handle).with_context(|| format!("Error while streaming {}", camera_config.name)); (*camera_results.lock().unwrap()).push(camera_result); + + let _ = camera.stop_video(*stream_name); + abort_handle.store(true, Ordering::Relaxed); }); } }).unwrap(); let mut camera_results = arc_camera_results.lock().unwrap(); while camera_results.len() > 0 { - let result = camera_results.pop().unwrap(); // Need to take ownership - if result.is_err() { - return result; - } + camera_results.pop().unwrap()?; // Need to take ownership } unreachable!(); // Should always error before this