diff --git a/src/battery/mod.rs b/src/battery/mod.rs index 1e76326..c2e08b1 100644 --- a/src/battery/mod.rs +++ b/src/battery/mod.rs @@ -25,6 +25,7 @@ pub(crate) use cmdline::Opt; pub(crate) async fn main(opt: Opt, config: Config, reactor: NeoReactor) -> Result<()> { let config = config.get_camera_config(&opt.camera)?; let camera = reactor.get_or_insert(config.clone()).await?; + log::debug!("Battery: Instance aquired"); let state = camera .run_task(|cam| { diff --git a/src/common/neocam.rs b/src/common/neocam.rs index b84958c..5725637 100644 --- a/src/common/neocam.rs +++ b/src/common/neocam.rs @@ -5,62 +5,167 @@ //! Common restart code //! Clonable interface to share amongst threads use crate::{config::CameraConfig, utils::connect_and_login, Result}; -use neolink_core::bc_protocol::BcCamera; +use neolink_core::bc_protocol::{BcCamera, StreamKind}; use anyhow::anyhow; use futures::stream::StreamExt; +use neolink_core::bcmedia::model::BcMedia; use std::collections::{hash_map::Entry, HashMap}; use std::sync::{Arc, Weak}; use tokio::sync::{ - mpsc::{channel as mpsc, Sender as MpscSender, WeakSender as MpscWeakSender}, + broadcast::{channel as broadcast, Sender as BroadcastSender}, + mpsc::{channel as mpsc, Receiver as MpscReceiver, Sender as MpscSender}, oneshot::{channel as oneshot, Sender as OneshotSender}, watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender}, }; use tokio::time::{interval, sleep, Duration, Instant}; -use tokio_stream::wrappers::ReceiverStream; +use tokio_stream::wrappers::{BroadcastStream, ReceiverStream}; use tokio_util::sync::CancellationToken; enum NeoCamCommand { HangUp, Instance(OneshotSender>), + Stream(StreamKind, OneshotSender>), } /// The underlying camera binding struct NeoCam { - config: WatchReceiver, cancel: CancellationToken, - camera_watch: WatchSender>, - commander: ReceiverStream, - sender: MpscWeakSender, + config_watch: WatchSender, + commander: MpscSender, + camera_watch: WatchReceiver>, } impl NeoCam { - async fn init(config: CameraConfig) -> Result<(WatchSender, NeoInstance)> { - let (watch_config_tx, watch_config_rx) = watch(config); - let (camera_watch_tx, _) = watch(Weak::new()); + async fn new(config: CameraConfig) -> Result { let (commander_tx, commander_rx) = mpsc(100); - let mut me = Self { - config: watch_config_rx, + let (watch_config_tx, watch_config_rx) = watch(config.clone()); + let (camera_watch_tx, camera_watch_rx) = watch(Weak::new()); + let (stream_request_tx, stream_request_rx) = mpsc(100); + + let me = Self { cancel: CancellationToken::new(), - camera_watch: camera_watch_tx, - commander: ReceiverStream::new(commander_rx), - sender: commander_tx.downgrade(), + config_watch: watch_config_tx, + commander: commander_tx.clone(), + camera_watch: camera_watch_rx.clone(), }; - tokio::task::spawn(async move { me.run().await }); + // This thread recieves messages from the instances + // and acts on it. + // + // This thread must be sta rted first so that we can begin creating instances for the + // other threads + let sender_cancel = me.cancel.clone(); + let mut commander_rx = ReceiverStream::new(commander_rx); + let strict = config.strict; + let thread_commander_tx = commander_tx.clone(); + tokio::task::spawn(async move { + let thread_cancel = sender_cancel.clone(); + let res = tokio::select! { + _ = sender_cancel.cancelled() => Result::Ok(()), + v = async { + while let Some(command) = commander_rx.next().await { + match command { + NeoCamCommand::HangUp => { + sender_cancel.cancel(); + log::debug!("Cancel:: NeoCamCommand::HangUp"); + return Result::<(), anyhow::Error>::Ok(()); + } + NeoCamCommand::Instance(result) => { + let instance = NeoInstance::new( + camera_watch_rx.clone(), + thread_commander_tx.clone(), + thread_cancel.clone(), + ); + let _ = result.send(instance); + } + NeoCamCommand::Stream(name, sender) => { + stream_request_tx.send( + StreamRequest { + name, + sender, + strict, + } + ).await?; + }, + } + } + Ok(()) + } => v + }; + log::debug!("Control thread terminated"); + res + }); + + let mut cam_thread = + NeoCamThread::new(watch_config_rx, camera_watch_tx, me.cancel.clone()).await; + + // This thread maintains the camera loop + // + // It will keep it logged and reconnect + tokio::task::spawn(async move { cam_thread.run().await }); let (instance_tx, instance_rx) = oneshot(); commander_tx .send(NeoCamCommand::Instance(instance_tx)) .await?; - Ok((watch_config_tx, instance_rx.await??)) + let instance = instance_rx.await??; + + // This thread maintains the streams + let stream_instance = instance.subscribe().await?; + let stream_cancel = me.cancel.clone(); + let mut stream_thread = + NeoCamStreamThread::new(stream_request_rx, stream_instance, stream_cancel).await; + + tokio::task::spawn(async move { stream_thread.run().await }); + + Ok(me) + } + + async fn subscribe(&self) -> Result { + NeoInstance::new( + self.camera_watch.clone(), + self.commander.clone(), + self.cancel.clone(), + ) + } + + pub(crate) async fn shutdown(&self) { + let _ = self.commander.send(NeoCamCommand::HangUp).await; + self.cancel.cancelled().await + } +} + +impl Drop for NeoCam { + fn drop(&mut self) { + log::debug!("Cancel:: NeoCam::drop"); + self.cancel.cancel(); + } +} + +struct NeoCamThread { + config: WatchReceiver, + cancel: CancellationToken, + camera_watch: WatchSender>, +} + +impl NeoCamThread { + async fn new( + watch_config_rx: WatchReceiver, + camera_watch_tx: WatchSender>, + cancel: CancellationToken, + ) -> Self { + Self { + config: watch_config_rx, + cancel, + camera_watch: camera_watch_tx, + } } async fn run_camera(&mut self, config: &CameraConfig) -> Result<()> { let camera = Arc::new(connect_and_login(config).await?); self.camera_watch.send_replace(Arc::downgrade(&camera)); - let cancel = self.cancel.clone(); let cancel_check = self.cancel.clone(); // Now we wait for a disconnect tokio::select! { @@ -82,21 +187,6 @@ impl NeoCam { } } } => v, - v = async { - while let Some(command) = self.commander.next().await { - match command { - NeoCamCommand::HangUp => { - cancel.cancel(); - return Ok(()); - } - NeoCamCommand::Instance(result) => { - let instance = NeoInstance::new(self); - let _ = result.send(instance); - } - } - } - Ok(()) - } => v }?; let _ = camera.logout().await; @@ -150,6 +240,7 @@ impl NeoCam { match result { Ok(()) => { // Normal shutdown + log::debug!("Cancel:: NeoCamThread::NormalShutdown"); self.cancel.cancel(); return Ok(()); } @@ -178,12 +269,125 @@ impl NeoCam { } } -impl Drop for NeoCam { +impl Drop for NeoCamThread { fn drop(&mut self) { + log::debug!("Cancel:: NeoCamThread::drop"); self.cancel.cancel(); } } +struct StreamRequest { + name: StreamKind, + sender: OneshotSender>, + strict: bool, +} + +struct StreamData { + sender: BroadcastSender, + name: StreamKind, + instance: NeoInstance, + strict: bool, + cancel: CancellationToken, +} + +impl StreamData { + async fn run(&self) -> Result<()> { + let thread_stream_tx = self.sender.clone(); + let cancel = self.cancel.clone(); + let instance = self.instance.subscribe().await?; + let name = self.name; + let strict = self.strict; + tokio::task::spawn(async move { + tokio::select! { + _ = cancel.cancelled() => { + Result::<(), anyhow::Error>::Ok(()) + }, + v = async { + loop { + instance.run_task(|camera| { + let stream_tx = thread_stream_tx.clone(); + Box::pin(async move { + let mut stream_data = camera.start_video(name, 0, strict).await?; + loop { + let data = stream_data.get_data().await??; + stream_tx.send(data)?; + } + }) + }).await?; + } + } => v, + } + }); + + Ok(()) + } +} + +impl Drop for StreamData { + fn drop(&mut self) { + log::debug!("Cancel:: StreamData::drop"); + self.cancel.cancel(); + } +} + +/// This thread will start and stop the camera +/// based on the number of listeners to the +/// async streams +struct NeoCamStreamThread { + streams: HashMap, + stream_request_rx: MpscReceiver, + cancel: CancellationToken, + instance: NeoInstance, +} + +impl NeoCamStreamThread { + async fn new( + stream_request_rx: MpscReceiver, + instance: NeoInstance, + cancel: CancellationToken, + ) -> Self { + Self { + streams: Default::default(), + stream_request_rx, + cancel, + instance, + } + } + async fn run(&mut self) -> Result<()> { + let thread_cancel = self.cancel.clone(); + tokio::select! { + _ = thread_cancel.cancelled() => Ok(()), + v = async { + while let Some(request) = self.stream_request_rx.recv().await { + match self.streams.entry(request.name) { + Entry::Occupied(occ) => { + let _ = request + .sender + .send(BroadcastStream::new(occ.get().sender.subscribe())); + } + Entry::Vacant(vac) => { + // Make a new streaming instance + + let (sender, stream_rx) = broadcast(1000); + let data = StreamData { + sender, + name: request.name, + instance: self.instance.subscribe().await?, + strict: request.strict, + cancel: CancellationToken::new(), + }; + data.run().await?; + vac.insert(data); + let _ = request.sender.send(BroadcastStream::new(stream_rx)); + } + } + } + Ok(()) + } => v, + } + } +} + /// The sharable instance /// /// This communicaes with the [`NeoCam`] over channels @@ -191,20 +395,21 @@ impl Drop for NeoCam { /// The camera watch is used as an event to be triggered /// whenever the camera is lost/updated pub(crate) struct NeoInstance { - camera_watch: tokio::sync::watch::Receiver>, + camera_watch: WatchReceiver>, camera_control: MpscSender, cancel: CancellationToken, } impl NeoInstance { - fn new(cam: &NeoCam) -> Result { + fn new( + camera_watch: WatchReceiver>, + camera_control: MpscSender, + cancel: CancellationToken, + ) -> Result { Ok(Self { - camera_watch: cam.camera_watch.subscribe(), - camera_control: cam - .sender - .upgrade() - .ok_or_else(|| anyhow!("Camera is shutting down"))?, - cancel: cam.cancel.clone(), + camera_watch, + camera_control, + cancel, }) } @@ -233,7 +438,8 @@ impl NeoInstance { where F: for<'a> Fn( &'a BcCamera, - ) -> std::pin::Pin> + 'a>>, + ) + -> std::pin::Pin> + Send + 'a>>, { let mut camera_watch = self.camera_watch.clone(); let mut camera = camera_watch.borrow_and_update().upgrade(); @@ -285,6 +491,14 @@ impl NeoInstance { } } } + + pub(crate) async fn stream(&self, name: StreamKind) -> Result> { + let (instance_tx, instance_rx) = oneshot(); + self.camera_control + .send(NeoCamCommand::Stream(name, instance_tx)) + .await?; + Ok(instance_rx.await?) + } } #[allow(clippy::large_enum_variant)] @@ -295,11 +509,6 @@ enum NeoReactorCommand { UpdateOrInsert(CameraConfig, OneshotSender>), } -struct NeoReactorData { - instance: NeoInstance, - config_sender: WatchSender, -} - /// Reactor handles the collection of cameras #[derive(Clone)] pub(crate) struct NeoReactor { @@ -318,12 +527,12 @@ impl NeoReactor { let cancel = me.cancel.clone(); let cancel2 = me.cancel.clone(); tokio::task::spawn(async move { - let mut instances: HashMap = Default::default(); + let mut instances: HashMap = Default::default(); tokio::select! { _ = cancel.cancelled() => { for instance in instances.values() { - instance.instance.shutdown().await; + instance.shutdown().await; } Ok(()) }, @@ -333,8 +542,9 @@ impl NeoReactor { match command { NeoReactorCommand::HangUp => { for instance in instances.values() { - instance.instance.shutdown().await; + instance.shutdown().await; } + log::debug!("Cancel:: NeoReactorCommand::HangUp"); cancel2.cancel(); return Result::<(), anyhow::Error>::Ok(()); } @@ -342,41 +552,38 @@ impl NeoReactor { let new = instances .get(&name) .ok_or_else(|| anyhow!("Camera not found")) - .map(|data| data.instance.subscribe())? + .map(|data| data.subscribe())? .await; let _ = sender.send(new); } NeoReactorCommand::GetOrInsert(config, sender) => { let name = config.name.clone(); let new = match instances.entry(name) { - Entry::Occupied(occ) => occ.get().instance.subscribe().await, + Entry::Occupied(occ) => occ.get().subscribe().await, Entry::Vacant(vac) => { - let (config_sender, instance) = NeoCam::init(config).await?; - vac.insert(NeoReactorData { - instance, - config_sender, - }) - .instance + log::debug!("Inserting new insance"); + let cam = NeoCam::new(config).await?; + log::debug!("New instance created"); + vac.insert( + cam, + ) .subscribe() .await } }; + log::debug!("Got instance from reactor"); let _ = sender.send(new); }, NeoReactorCommand::UpdateOrInsert(config, sender) => { let name = config.name.clone(); let new = match instances.entry(name) { Entry::Occupied(occ) => { - occ.get().config_sender.send(config)?; - occ.get().instance.subscribe().await + occ.get().config_watch.send(config)?; + occ.get().subscribe().await }, Entry::Vacant(vac) => { - let (config_sender, instance) = NeoCam::init(config).await?; - vac.insert(NeoReactorData { - instance, - config_sender, - }) - .instance + let cam = NeoCam::new(config).await?; + vac.insert(cam) .subscribe() .await } @@ -433,6 +640,7 @@ impl NeoReactor { impl Drop for NeoReactor { fn drop(&mut self) { + log::debug!("Cancel:: NeoReactor::drop"); self.cancel.cancel(); } } diff --git a/src/image/mod.rs b/src/image/mod.rs index c6a19ff..492fd6e 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -18,6 +18,7 @@ /// ``` /// use anyhow::{Context, Result}; +use futures::stream::StreamExt; use log::*; use neolink_core::{bc_protocol::*, bcmedia::model::*}; use tokio::{fs::File, io::AsyncWriteExt}; @@ -26,25 +27,19 @@ mod cmdline; mod gst; use super::config::Config; -use crate::utils::{connect_and_login, find_camera_by_name}; +use crate::common::neocam::NeoReactor; pub(crate) use cmdline::Opt; /// Entry point for the image subcommand /// /// Opt is the command line options -pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { - let camera_config = find_camera_by_name(&config, &opt.camera).context( - "failed to load config file or find the camera. Check the path and name of the camera.", - )?; - - // Set up camera to recieve the stream - let camera = connect_and_login(camera_config) - .await - .context("Failed to connect to the camera, check credentials and network")?; +pub(crate) async fn main(opt: Opt, config: Config, reactor: NeoReactor) -> Result<()> { + let config = config.get_camera_config(&opt.camera)?; + let camera = reactor.get_or_insert(config.clone()).await?; if opt.use_stream { let mut stream_data = camera - .start_video(StreamKind::Main, 0, camera_config.strict) + .stream(StreamKind::Main) .await .context("Failed to start video")?; @@ -52,7 +47,7 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { let buf; let vid_type; loop { - if let BcMedia::Iframe(frame) = stream_data.get_data().await?? { + if let Some(Ok(BcMedia::Iframe(frame))) = stream_data.next().await { vid_type = frame.video_type; buf = frame.data; break; @@ -64,9 +59,9 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { // Keep sending both IFrame or PFrame until finished while sender.is_finished().await.is_none() { - let buf = match stream_data.get_data().await?? { - BcMedia::Iframe(frame) => frame.data, - BcMedia::Pframe(frame) => frame.data, + let buf = match stream_data.next().await { + Some(Ok(BcMedia::Iframe(frame))) => frame.data, + Some(Ok(BcMedia::Pframe(frame))) => frame.data, _ => { continue; } @@ -82,19 +77,18 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { debug!("Sending EOS"); let _ = sender.eos().await; // Ignore return because if pipeline is finished this will error let _ = sender.join().await; - - let _ = stream_data.shutdown().await; } else { // Simply use the snap command debug!("Using the snap command"); let file_path = opt.file_path.with_extension("jpeg"); let mut buffer = File::create(file_path).await?; - let jpeg_data = camera.get_snapshot().await?; + let jpeg_data = camera + .run_task(|camera| Box::pin(async move { Ok(camera.get_snapshot().await?) })) + .await?; buffer.write_all(jpeg_data.as_slice()).await?; } - let _ = camera.logout().await; - camera.shutdown().await?; + camera.shutdown().await; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 29b7734..bbdba4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,7 +118,7 @@ async fn main() -> Result<()> { mqtt::main(opts, config).await?; } Some(Command::Image(opts)) => { - image::main(opts, config).await?; + image::main(opts, config, neo_reactor.clone()).await?; } Some(Command::Battery(opts)) => { battery::main(opts, config, neo_reactor.clone()).await?;