Add motion pause to rtsp
This commit is contained in:
4 files changed
+50
-3
No files matched your search
@@ -11,7 +11,7 @@ use tokio::sync::{
|
||||
};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::{NeoCamCommand, StreamInstance};
|
||||
use super::{MdState, NeoCamCommand, StreamInstance};
|
||||
use crate::{config::CameraConfig, Result};
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind};
|
||||
|
||||
@@ -157,6 +157,14 @@ impl NeoInstance {
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
|
||||
pub(crate) async fn motion(&self) -> Result<WatchReceiver<MdState>> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
.send(NeoCamCommand::Motion(instance_tx))
|
||||
.await?;
|
||||
Ok(instance_rx.await?)
|
||||
}
|
||||
|
||||
pub(crate) async fn config(&self) -> Result<WatchReceiver<CameraConfig>> {
|
||||
let (instance_tx, instance_rx) = oneshot();
|
||||
self.camera_control
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
mod camthread;
|
||||
mod instance;
|
||||
mod mdthread;
|
||||
mod neocam;
|
||||
mod reactor;
|
||||
mod streamthread;
|
||||
|
||||
pub(crate) use camthread::*;
|
||||
pub(crate) use instance::*;
|
||||
pub(crate) use mdthread::*;
|
||||
pub(crate) use neocam::*;
|
||||
pub(crate) use reactor::*;
|
||||
pub(crate) use streamthread::*;
|
||||
+19
-1
@@ -14,7 +14,10 @@ use tokio::sync::{
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use super::{NeoCamStreamThread, NeoCamThread, NeoInstance, StreamInstance, StreamRequest};
|
||||
use super::{
|
||||
MdRequest, MdState, NeoCamMdThread, NeoCamStreamThread, NeoCamThread, NeoInstance,
|
||||
StreamInstance, StreamRequest,
|
||||
};
|
||||
use crate::{config::CameraConfig, AnyResult, Result};
|
||||
use neolink_core::bc_protocol::{BcCamera, StreamKind};
|
||||
|
||||
@@ -26,6 +29,7 @@ pub(crate) enum NeoCamCommand {
|
||||
HighStream(OneshotSender<Option<StreamInstance>>),
|
||||
LowStream(OneshotSender<Option<StreamInstance>>),
|
||||
Streams(OneshotSender<Vec<StreamInstance>>),
|
||||
Motion(OneshotSender<WatchReceiver<MdState>>),
|
||||
Config(OneshotSender<WatchReceiver<CameraConfig>>),
|
||||
}
|
||||
/// The underlying camera binding
|
||||
@@ -42,6 +46,7 @@ impl NeoCam {
|
||||
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 (md_request_tx, md_request_rx) = mpsc(100);
|
||||
|
||||
let me = Self {
|
||||
cancel: CancellationToken::new(),
|
||||
@@ -110,6 +115,13 @@ impl NeoCam {
|
||||
}
|
||||
).await?;
|
||||
},
|
||||
NeoCamCommand::Motion(sender) => {
|
||||
md_request_tx.send(
|
||||
MdRequest::Get {
|
||||
sender,
|
||||
}
|
||||
).await?;
|
||||
},
|
||||
NeoCamCommand::Config(sender) => {
|
||||
let _ = sender.send(thread_watch_config_rx.clone());
|
||||
}
|
||||
@@ -144,6 +156,12 @@ impl NeoCam {
|
||||
NeoCamStreamThread::new(stream_request_rx, stream_instance, stream_cancel).await?;
|
||||
tokio::task::spawn(async move { stream_thread.run().await });
|
||||
|
||||
// This thread monitors the motion
|
||||
let md_instance = instance.subscribe().await?;
|
||||
let md_cancel = me.cancel.clone();
|
||||
let mut md_thread = NeoCamMdThread::new(md_request_rx, md_instance, md_cancel).await?;
|
||||
tokio::task::spawn(async move { md_thread.run().await });
|
||||
|
||||
// This thread just does a one time report on camera info
|
||||
let report_instance = instance.subscribe().await?;
|
||||
let report_cancel = me.cancel.clone();
|
||||
|
||||
+20
-1
@@ -66,7 +66,7 @@ use tokio::{
|
||||
mpsc::{channel as mpsc, Receiver as MpscReceiver, Sender as MpscSender},
|
||||
},
|
||||
task::JoinSet,
|
||||
time::{sleep, Duration},
|
||||
time::{sleep, Duration, Instant},
|
||||
};
|
||||
use tokio_stream::{wrappers::BroadcastStream, Stream, StreamExt};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
@@ -445,6 +445,25 @@ async fn stream_main(
|
||||
});
|
||||
}
|
||||
|
||||
// Handles on motion pausing
|
||||
if curr_pause.on_motion {
|
||||
// Take over activation
|
||||
let mut client_activator = stream_instance.activator_handle().await;
|
||||
stream_instance.deactivate().await?;
|
||||
let mut motion = camera.motion().await?;
|
||||
let delta = Duration::from_secs_f64(curr_pause.motion_timeout);
|
||||
set.spawn(async move {
|
||||
loop {
|
||||
log::trace!("Activating Motion");
|
||||
client_activator.activate().await?;
|
||||
motion.wait_for(|md| matches!(md, crate::common::MdState::Stop(_))).await?;
|
||||
log::trace!("Pausing Motion");
|
||||
client_activator.deactivate().await?;
|
||||
motion.wait_for(|md| matches!(md, crate::common::MdState::Start(n) if (*n - Instant::now())>delta)).await?;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// This select is for the streams config updates
|
||||
break tokio::select! {
|
||||
_ = thread_stream_config2.changed() => {
|
||||
|
||||
Reference in new issue
Block a user