Seperate tasks into passive and active
This commit is contained in:
9 files changed
+225
-161
No files matched your search
@@ -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,
|
||||
|
||||
+41
-1
@@ -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<F, T>(&self, task: F) -> Result<T>
|
||||
where
|
||||
F: for<'a> Fn(
|
||||
&'a BcCamera,
|
||||
)
|
||||
-> std::pin::Pin<Box<dyn futures::Future<Output = Result<T>> + 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<F, T>(&self, task: F) -> Result<T>
|
||||
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<NeoCamThreadState> {
|
||||
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<Permit> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::GetPermit(instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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::*;
|
||||
+11
-1
@@ -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<WatchReceiver<CameraConfig>>),
|
||||
Disconnect(OneshotSender<()>),
|
||||
Connect(OneshotSender<()>),
|
||||
State(OneshotSender<NeoCamThreadState>),
|
||||
GetPermit(OneshotSender<Permit>),
|
||||
}
|
||||
/// 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");
|
||||
|
||||
+6
-153
@@ -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<u32>,
|
||||
notifier_tx: MpscSender<bool>,
|
||||
cancel: CancellationToken,
|
||||
set: JoinSet<AnyResult<()>>,
|
||||
}
|
||||
|
||||
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<CountUses> {
|
||||
let mut res = CountUses::new(self);
|
||||
res.activate().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub(crate) async fn create_deactivated(&self) -> Result<CountUses> {
|
||||
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<u32>,
|
||||
notifier: MpscSender<bool>,
|
||||
}
|
||||
|
||||
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<StampedData>,
|
||||
@@ -420,7 +273,7 @@ pub(crate) struct StreamInstance {
|
||||
pub(crate) aud: BroadcastReceiver<StampedData>,
|
||||
pub(crate) aud_history: WatchReceiver<VecDeque<StampedData>>,
|
||||
pub(crate) config: WatchReceiver<StreamConfig>,
|
||||
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();
|
||||
|
||||
@@ -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<u32>,
|
||||
notifier_tx: MpscSender<bool>,
|
||||
cancel: CancellationToken,
|
||||
set: JoinSet<AnyResult<()>>,
|
||||
}
|
||||
|
||||
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<Permit> {
|
||||
let mut res = Permit::new(self);
|
||||
res.activate().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub(crate) async fn create_deactivated(&self) -> Result<Permit> {
|
||||
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<u32>,
|
||||
notifier: MpscSender<bool>,
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -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)
|
||||
|
||||
+2
-2
@@ -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<String>,
|
||||
paths: &[String],
|
||||
client_count: CountUses,
|
||||
client_count: Permit,
|
||||
) -> AnyResult<()> {
|
||||
let vidstream = stream_instance.vid.resubscribe();
|
||||
let audstream = stream_instance.aud.resubscribe();
|
||||
|
||||
Reference in new issue
Block a user