From 31d6c1ef629a57d409f9625737af89081de5948a Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 24 Sep 2023 09:14:08 +0700 Subject: [PATCH] Seperate tasks into passive and active --- src/common/camthread.rs | 1 + src/common/instance.rs | 42 +++++++++- src/common/mdthread.rs | 2 +- src/common/mod.rs | 2 + src/common/neocam.rs | 12 ++- src/common/streamthread.rs | 159 ++----------------------------------- src/common/usecounter.rs | 158 ++++++++++++++++++++++++++++++++++++ src/mqtt/mod.rs | 6 +- src/rtsp/mod.rs | 4 +- 9 files changed, 225 insertions(+), 161 deletions(-) create mode 100644 src/common/usecounter.rs diff --git a/src/common/camthread.rs b/src/common/camthread.rs index da5105d..435a0e3 100644 --- a/src/common/camthread.rs +++ b/src/common/camthread.rs @@ -8,6 +8,7 @@ use tokio_util::sync::CancellationToken; use crate::{config::CameraConfig, utils::connect_and_login, Result}; use neolink_core::bc_protocol::BcCamera; +#[derive(Eq, PartialEq, Copy, Clone)] pub(crate) enum NeoCamThreadState { Connected, Disconnected, diff --git a/src/common/instance.rs b/src/common/instance.rs index f30de7f..b6d0adc 100644 --- a/src/common/instance.rs +++ b/src/common/instance.rs @@ -11,7 +11,7 @@ use tokio::sync::{ }; use tokio_util::sync::CancellationToken; -use super::{MdState, NeoCamCommand, StreamInstance}; +use super::{MdState, NeoCamCommand, NeoCamThreadState, Permit, StreamInstance}; use crate::{config::CameraConfig, Result}; use neolink_core::bc_protocol::{BcCamera, StreamKind}; @@ -60,6 +60,30 @@ impl NeoInstance { /// - If the camera returns a retryable error, wait for camera instance to change then rerun /// - else return the result of the function pub(crate) async fn run_task(&self, task: F) -> Result + where + F: for<'a> Fn( + &'a BcCamera, + ) + -> std::pin::Pin> + Send + 'a>>, + { + let _permit = self.permit().await?; + self.run_passive_task(task).await + } + + /// This is a helpful convience function + /// + /// Given an async task it will: + /// - Run the task with a reference to a BcCamera + /// - If the camera instance is changed: Rerun the task with the new instance + /// - If the camera returns a retryable error, wait for camera instance to change then rerun + /// - else return the result of the function + /// + /// This variant does NOT take out a use permit so the camera can disconnect + /// for inactvitity during its call. It is meant to be used for non-critial + /// background tasks that we want to stop during certain times like low battery + /// + /// The streams and MD use this + pub(crate) async fn run_passive_task(&self, task: F) -> Result where F: for<'a> Fn( &'a BcCamera, @@ -187,4 +211,20 @@ impl NeoInstance { .await?; Ok(instance_rx.await?) } + + pub(crate) async fn get_state(&self) -> Result { + let (instance_tx, instance_rx) = oneshot(); + self.camera_control + .send(NeoCamCommand::State(instance_tx)) + .await?; + Ok(instance_rx.await?) + } + + pub(crate) async fn permit(&self) -> Result { + let (instance_tx, instance_rx) = oneshot(); + self.camera_control + .send(NeoCamCommand::GetPermit(instance_tx)) + .await?; + Ok(instance_rx.await?) + } } diff --git a/src/common/mdthread.rs b/src/common/mdthread.rs index fe6d122..f087a2a 100644 --- a/src/common/mdthread.rs +++ b/src/common/mdthread.rs @@ -64,7 +64,7 @@ impl NeoCamMdThread { } Ok(()) } => v, - v = md_instance.run_task(|cam| { + v = md_instance.run_passive_task(|cam| { let watcher = watcher.clone(); Box::pin( async move { diff --git a/src/common/mod.rs b/src/common/mod.rs index 2a5ece2..1c70218 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -4,6 +4,7 @@ mod mdthread; mod neocam; mod reactor; mod streamthread; +mod usecounter; pub(crate) use camthread::*; pub(crate) use instance::*; @@ -11,3 +12,4 @@ pub(crate) use mdthread::*; pub(crate) use neocam::*; pub(crate) use reactor::*; pub(crate) use streamthread::*; +pub(crate) use usecounter::*; diff --git a/src/common/neocam.rs b/src/common/neocam.rs index 70a00b2..cc61a5e 100644 --- a/src/common/neocam.rs +++ b/src/common/neocam.rs @@ -19,7 +19,7 @@ use tokio_util::sync::CancellationToken; use super::{ MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoCamThreadState, - NeoInstance, StreamInstance, StreamRequest, + NeoInstance, Permit, StreamInstance, StreamRequest, UseCounter, }; use crate::{config::CameraConfig, AnyResult, Result}; use neolink_core::bc_protocol::{BcCamera, StreamKind}; @@ -36,6 +36,8 @@ pub(crate) enum NeoCamCommand { Config(OneshotSender>), Disconnect(OneshotSender<()>), Connect(OneshotSender<()>), + State(OneshotSender), + GetPermit(OneshotSender), } /// The underlying camera binding pub(crate) struct NeoCam { @@ -56,6 +58,8 @@ impl NeoCam { let (state_tx, state_rx) = watch(NeoCamThreadState::Connected); let set = JoinSet::new(); + let users = UseCounter::new().await; + let mut me = Self { cancel: CancellationToken::new(), config_watch: watch_config_tx, @@ -151,6 +155,12 @@ impl NeoCam { } let _ = sender.send(()); } + NeoCamCommand::State(sender) => { + let _ = sender.send(*state_tx.borrow()); + } + NeoCamCommand::GetPermit(sender) => { + let _ = sender.send(users.create_activated().await?); + } } } log::debug!("Control thread Senders dropped"); diff --git a/src/common/streamthread.rs b/src/common/streamthread.rs index f1a0eb4..2def381 100644 --- a/src/common/streamthread.rs +++ b/src/common/streamthread.rs @@ -13,16 +13,16 @@ use tokio::{ broadcast::{ channel as broadcast, Receiver as BroadcastReceiver, Sender as BroadcastSender, }, - mpsc::{channel as mpsc, Receiver as MpscReceiver, Sender as MpscSender}, + mpsc::{channel as mpsc, Receiver as MpscReceiver}, oneshot::Sender as OneshotSender, watch::{channel as watch, Receiver as WatchReceiver, Sender as WatchSender}, }, - task::{JoinHandle, JoinSet}, + task::JoinHandle, time::{timeout, Duration}, }; use tokio_util::sync::CancellationToken; -use super::NeoInstance; +use super::{NeoInstance, Permit, UseCounter}; use crate::{AnyResult, Result}; use neolink_core::{bc_protocol::StreamKind, bcmedia::model::*}; @@ -223,153 +223,6 @@ pub(crate) enum StreamRequest { }, } -/// Counts the active users of the stream -pub(crate) struct UseCounter { - value: WatchReceiver, - notifier_tx: MpscSender, - cancel: CancellationToken, - set: JoinSet>, -} - -impl UseCounter { - pub(crate) async fn new() -> Self { - let (notifier_tx, mut notifier) = mpsc(100); - let (value_tx, value) = watch(0); - let cancel = CancellationToken::new(); - let mut set = JoinSet::new(); - - let thread_cancel = cancel.clone(); - set.spawn(async move { - let r = tokio::select! { - _ = thread_cancel.cancelled() => { - AnyResult::Ok(()) - }, - v = async { - while let Some(noti) = notifier.recv().await { - value_tx.send_modify(|value| { - if noti { - log::trace!("Usecounter: {}->{}", *value, (*value) + 1); - *value += 1; - } else { - log::trace!("Usecounter: {}->{}", *value, (*value) - 1); - *value -= 1; - } - }); - } - AnyResult::Ok(()) - } => v, - }; - log::trace!("End Use Counter: {r:?}"); - r - }); - Self { - value, - notifier_tx, - cancel, - set, - } - } - - pub(crate) async fn create_activated(&self) -> Result { - let mut res = CountUses::new(self); - res.activate().await?; - Ok(res) - } - - pub(crate) async fn create_deactivated(&self) -> Result { - Ok(CountUses::new(self)) - } -} - -impl Drop for UseCounter { - fn drop(&mut self) { - log::trace!("Drop UseCounter"); - self.cancel.cancel(); - tokio::task::block_in_place(move || { - let _ = tokio::runtime::Handle::current().block_on(async move { - while self.set.join_next().await.is_some() {} - AnyResult::Ok(()) - }); - }); - log::trace!("Dropped UseCounter"); - } -} - -pub(crate) struct CountUses { - is_active: bool, - value: WatchReceiver, - notifier: MpscSender, -} - -impl CountUses { - pub(crate) fn subscribe(&self) -> Self { - Self { - is_active: false, - value: self.value.clone(), - notifier: self.notifier.clone(), - } - } - - fn new(source: &UseCounter) -> Self { - Self { - is_active: false, - value: source.value.clone(), - notifier: source.notifier_tx.clone(), - } - } - - pub(crate) async fn activate(&mut self) -> Result<()> { - if !self.is_active { - self.is_active = true; - self.notifier.send(self.is_active).await?; - } - Ok(()) - } - - pub(crate) async fn deactivate(&mut self) -> Result<()> { - if self.is_active { - self.is_active = false; - self.notifier.send(self.is_active).await?; - } - Ok(()) - } - - pub(crate) async fn aquired_users(&self) -> Result<()> { - self.value - .clone() - .wait_for(|curr| { - log::trace!("aquired_users: {}", *curr); - *curr > 0 - }) - .await?; - Ok(()) - } - - pub(crate) async fn dropped_users(&self) -> Result<()> { - self.value - .clone() - .wait_for(|curr| { - log::trace!("dropped_users: {}", *curr); - *curr == 0 - }) - .await?; - Ok(()) - } -} - -impl Drop for CountUses { - fn drop(&mut self) { - if self.is_active { - self.is_active = false; - tokio::task::block_in_place(move || { - tokio::runtime::Handle::current().block_on(async move { - let _ = self.notifier.send(self.is_active).await; - }); - }); - } - } -} - /// The data of a running stream pub(crate) struct StreamData { vid: BroadcastSender, @@ -420,7 +273,7 @@ pub(crate) struct StreamInstance { pub(crate) aud: BroadcastReceiver, pub(crate) aud_history: WatchReceiver>, pub(crate) config: WatchReceiver, - in_use: CountUses, + in_use: Permit, } impl StreamInstance { @@ -442,7 +295,7 @@ impl StreamInstance { self.in_use.deactivate().await } - pub(crate) async fn activator_handle(&mut self) -> CountUses { + pub(crate) async fn activator_handle(&mut self) -> Permit { self.in_use.subscribe() } } @@ -544,7 +397,7 @@ impl StreamData { } } } => v, - result = instance.run_task(|camera| { + result = instance.run_passive_task(|camera| { let vid_tx = vid.clone(); let aud_tx = aud.clone(); let stream_config = config.clone(); diff --git a/src/common/usecounter.rs b/src/common/usecounter.rs new file mode 100644 index 0000000..1338a0b --- /dev/null +++ b/src/common/usecounter.rs @@ -0,0 +1,158 @@ +//! Used to track number of users of a service +use tokio::{ + sync::{ + mpsc::{channel as mpsc, Sender as MpscSender}, + watch::{channel as watch, Receiver as WatchReceiver}, + }, + task::JoinSet, +}; +use tokio_util::sync::CancellationToken; + +use crate::{AnyResult, Result}; + +/// Counts the active users of the stream +pub(crate) struct UseCounter { + value: WatchReceiver, + notifier_tx: MpscSender, + cancel: CancellationToken, + set: JoinSet>, +} + +impl UseCounter { + pub(crate) async fn new() -> Self { + let (notifier_tx, mut notifier) = mpsc(100); + let (value_tx, value) = watch(0); + let cancel = CancellationToken::new(); + let mut set = JoinSet::new(); + + let thread_cancel = cancel.clone(); + set.spawn(async move { + let r = tokio::select! { + _ = thread_cancel.cancelled() => { + AnyResult::Ok(()) + }, + v = async { + while let Some(noti) = notifier.recv().await { + value_tx.send_modify(|value| { + if noti { + log::trace!("Usecounter: {}->{}", *value, (*value) + 1); + *value += 1; + } else { + log::trace!("Usecounter: {}->{}", *value, (*value) - 1); + *value -= 1; + } + }); + } + AnyResult::Ok(()) + } => v, + }; + log::trace!("End Use Counter: {r:?}"); + r + }); + Self { + value, + notifier_tx, + cancel, + set, + } + } + + pub(crate) async fn create_activated(&self) -> Result { + let mut res = Permit::new(self); + res.activate().await?; + Ok(res) + } + + pub(crate) async fn create_deactivated(&self) -> Result { + Ok(Permit::new(self)) + } +} + +impl Drop for UseCounter { + fn drop(&mut self) { + log::trace!("Drop UseCounter"); + self.cancel.cancel(); + tokio::task::block_in_place(move || { + let _ = tokio::runtime::Handle::current().block_on(async move { + while self.set.join_next().await.is_some() {} + AnyResult::Ok(()) + }); + }); + log::trace!("Dropped UseCounter"); + } +} + +pub(crate) struct Permit { + is_active: bool, + value: WatchReceiver, + notifier: MpscSender, +} + +impl Permit { + pub(crate) fn subscribe(&self) -> Self { + Self { + is_active: false, + value: self.value.clone(), + notifier: self.notifier.clone(), + } + } + + fn new(source: &UseCounter) -> Self { + Self { + is_active: false, + value: source.value.clone(), + notifier: source.notifier_tx.clone(), + } + } + + pub(crate) async fn activate(&mut self) -> Result<()> { + if !self.is_active { + self.is_active = true; + self.notifier.send(self.is_active).await?; + } + Ok(()) + } + + pub(crate) async fn deactivate(&mut self) -> Result<()> { + if self.is_active { + self.is_active = false; + self.notifier.send(self.is_active).await?; + } + Ok(()) + } + + pub(crate) async fn aquired_users(&self) -> Result<()> { + self.value + .clone() + .wait_for(|curr| { + log::trace!("aquired_users: {}", *curr); + *curr > 0 + }) + .await?; + Ok(()) + } + + pub(crate) async fn dropped_users(&self) -> Result<()> { + self.value + .clone() + .wait_for(|curr| { + log::trace!("dropped_users: {}", *curr); + *curr == 0 + }) + .await?; + Ok(()) + } +} + +impl Drop for Permit { + fn drop(&mut self) { + if self.is_active { + self.is_active = false; + tokio::task::block_in_place(move || { + tokio::runtime::Handle::current().block_on(async move { + let _ = self.notifier.send(self.is_active).await; + }); + }); + } + } +} diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index b91a5fb..b1f52f1 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -348,7 +348,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R v = async { let (tx, mut rx) = mpsc(100); tokio::select! { - v = camera_floodlight.run_task(|cam| { + v = camera_floodlight.run_passive_task(|cam| { let tx = tx.clone(); Box::pin( async move { @@ -405,7 +405,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R i }); while wait.next().await.is_some() { - let image = camera_snap.run_task(|cam| { + let image = camera_snap.run_passive_task(|cam| { Box::pin(async move { let image = cam.get_snapshot().await?; AnyResult::Ok(image) @@ -439,7 +439,7 @@ async fn listen_on_camera(camera: NeoInstance, mqtt_instance: MqttInstance) -> R i }); while wait.next().await.is_some() { - let xml = camera_battery.run_task(|cam| { + let xml = camera_battery.run_passive_task(|cam| { Box::pin(async move { let xml = cam.battery_info().await?; AnyResult::Ok(xml) diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index 30e8305..1eac004 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -76,7 +76,7 @@ use tokio_util::sync::CancellationToken; mod cmdline; mod gst; -use crate::common::{CountUses, UseCounter}; +use crate::common::{Permit, UseCounter}; use crate::{ common::{AudFormat, NeoInstance, NeoReactor, StreamConfig, StreamInstance, VidFormat}, rtsp::gst::NeoMediaFactory, @@ -626,7 +626,7 @@ async fn stream_run( stream_config: &StreamConfig, users: &HashSet, paths: &[String], - client_count: CountUses, + client_count: Permit, ) -> AnyResult<()> { let vidstream = stream_instance.vid.resubscribe(); let audstream = stream_instance.aud.resubscribe();