Fix motion based pausing
This commit is contained in:
7 files changed
+102
-96
No files matched your search
@@ -1,5 +1,5 @@
|
||||
use super::BcSubscription;
|
||||
use crate::{bc::model::*, timeout, Error, Result};
|
||||
use crate::{bc::model::*, Error, Result};
|
||||
use futures::sink::{Sink, SinkExt};
|
||||
use futures::stream::{Stream, StreamExt};
|
||||
use log::*;
|
||||
@@ -64,9 +64,7 @@ impl BcConnection {
|
||||
}
|
||||
|
||||
pub(super) async fn send(&self, bc: Bc) -> crate::Result<()> {
|
||||
trace!("send Wait: {:?}", bc);
|
||||
timeout(self.sink.lock().await.send(bc)).await??;
|
||||
trace!("send Complete");
|
||||
self.sink.lock().await.send(bc).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::BcConnection;
|
||||
use crate::bcmedia::codex::BcMediaCodex;
|
||||
use crate::{bc::model::*, timeout, Error, Result};
|
||||
use crate::{bc::model::*, Error, Result};
|
||||
use futures::stream::{IntoAsyncRead, Stream, StreamExt, TryStreamExt};
|
||||
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
|
||||
use std::pin::Pin;
|
||||
@@ -95,9 +95,7 @@ impl<'a> BcSubscription<'a> {
|
||||
}
|
||||
|
||||
pub async fn recv(&mut self) -> Result<Bc> {
|
||||
let bc = timeout(self.rx.next())
|
||||
.await?
|
||||
.ok_or(Error::DroppedSubscriber)?;
|
||||
let bc = self.rx.next().await.ok_or(Error::DroppedSubscriber)?;
|
||||
if let Ok(bc) = &bc {
|
||||
assert!(bc.meta.msg_num as u32 == self.msg_num);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::{BcCamera, Error, Result};
|
||||
use crate::bc::{model::*, xml::*};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::mpsc::{channel, error::TryRecvError, Receiver};
|
||||
use tokio::task::{self, JoinHandle};
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
/// Motion Status that the callback can send
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
@@ -19,8 +19,8 @@ pub enum MotionStatus {
|
||||
///
|
||||
/// When this object is dropped the motion events are stopped
|
||||
pub struct MotionData {
|
||||
handle: Option<JoinHandle<Result<()>>>,
|
||||
rx: Receiver<MotionStatus>,
|
||||
handle: JoinSet<Result<()>>,
|
||||
rx: Receiver<Result<MotionStatus>>,
|
||||
last_update: MotionStatus,
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ impl MotionData {
|
||||
let mut results: Vec<MotionStatus> = vec![];
|
||||
loop {
|
||||
match self.rx.try_recv() {
|
||||
Ok(motion) => results.push(motion),
|
||||
Ok(motion) => results.push(motion?),
|
||||
Err(TryRecvError::Empty) => break,
|
||||
Err(e) => return Err(Error::from(e)),
|
||||
}
|
||||
@@ -77,6 +77,7 @@ impl MotionData {
|
||||
if let Some(last) = motions.last() {
|
||||
Ok(*last)
|
||||
} else if let Some(moition) = self.rx.recv().await {
|
||||
let moition = moition?;
|
||||
self.last_update = moition;
|
||||
Ok(moition)
|
||||
} else {
|
||||
@@ -97,8 +98,9 @@ impl MotionData {
|
||||
return Ok(());
|
||||
} else {
|
||||
// Schedule a sleep or wait for motion to start
|
||||
let remaining_sleep = duration - (Instant::now() - time);
|
||||
let result = tokio::select! {
|
||||
_ = tokio::time::sleep(duration - (Instant::now() - time)) => {None},
|
||||
_ = tokio::time::sleep(remaining_sleep) => {None},
|
||||
v = async {
|
||||
loop {
|
||||
match self.next_motion().await {
|
||||
@@ -157,18 +159,6 @@ impl MotionData {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MotionData {
|
||||
fn drop(&mut self) {
|
||||
if let Some(h) = self.handle.take() {
|
||||
let backoff = crossbeam::utils::Backoff::new();
|
||||
h.abort();
|
||||
while !h.is_finished() {
|
||||
backoff.snooze();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BcCamera {
|
||||
/// This message tells the camera to send the motion events to us
|
||||
/// Which are the recieved on msgid 33
|
||||
@@ -220,8 +210,9 @@ impl BcCamera {
|
||||
// when whenever motion is detected.
|
||||
let (tx, rx) = channel(20);
|
||||
|
||||
let mut set = JoinSet::new();
|
||||
let channel_id = self.channel_id;
|
||||
let handle = task::spawn(async move {
|
||||
set.spawn(async move {
|
||||
let mut sub = connection.subscribe(msg_num).await?;
|
||||
|
||||
loop {
|
||||
@@ -256,7 +247,7 @@ impl BcCamera {
|
||||
}
|
||||
// On connection drop we stop
|
||||
Err(e) => Err(e),
|
||||
}?;
|
||||
};
|
||||
|
||||
if tx.send(status).await.is_err() {
|
||||
// Motion reciever has been dropped
|
||||
@@ -267,9 +258,15 @@ impl BcCamera {
|
||||
});
|
||||
|
||||
Ok(MotionData {
|
||||
handle: Some(handle),
|
||||
handle: set,
|
||||
rx,
|
||||
last_update: MotionStatus::NoChange(Instant::now()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MotionData {
|
||||
fn drop(&mut self) {
|
||||
self.handle.abort_all();
|
||||
}
|
||||
}
|
||||
@@ -41,20 +41,3 @@ pub use bc_protocol::Error;
|
||||
pub(crate) use bc_protocol::{Credentials, Result};
|
||||
|
||||
pub(crate) type NomErrorType<'a> = nom::error::VerboseError<&'a [u8]>;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
/// This is a high level tiemout for the app as a whole. Not the lowlevel io ones
|
||||
///
|
||||
/// This is used to timeout when the primary send and receive channels of Bc packets
|
||||
/// is full for too long. This can happen if messages are not pulled from the endpoints
|
||||
/// fast enough or if there is an unexpected bug
|
||||
pub(crate) static ref TIMEOUT: tokio::time::Duration = tokio::time::Duration::from_secs(60);
|
||||
}
|
||||
|
||||
/// A convience future to timeout with the default timeout specified in [`TIMEOUT`]
|
||||
pub(crate) fn timeout<F>(future: F) -> tokio::time::Timeout<F>
|
||||
where
|
||||
F: std::future::Future,
|
||||
{
|
||||
tokio::time::timeout(*TIMEOUT, future)
|
||||
}
|
||||
@@ -87,7 +87,7 @@ impl EventCam {
|
||||
.await
|
||||
.context("Failed to send message from camera")
|
||||
} else {
|
||||
Err(anyhow!("Failed to send camera data over crossbeam"))
|
||||
Err(anyhow!("Failed to send camera data over channel"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+77
-50
@@ -33,10 +33,6 @@ struct StampedData {
|
||||
ms: u64,
|
||||
data: Vec<u8>,
|
||||
}
|
||||
struct ProcessData<'a> {
|
||||
live: StampedData,
|
||||
resend: &'a [StampedData],
|
||||
}
|
||||
|
||||
impl NeoMediaSender {
|
||||
pub(super) async fn run(&mut self) -> AnyResult<()> {
|
||||
@@ -46,6 +42,11 @@ impl NeoMediaSender {
|
||||
let mut buffer_b: Vec<StampedData> = Default::default();
|
||||
let buffer_read = &mut buffer_a;
|
||||
let buffer_write = &mut buffer_b;
|
||||
|
||||
let mut audbuffer_a: Vec<StampedData> = Default::default();
|
||||
let mut audbuffer_b: Vec<StampedData> = Default::default();
|
||||
let audbuffer_read = &mut audbuffer_a;
|
||||
let audbuffer_write = &mut audbuffer_b;
|
||||
loop {
|
||||
self.shared
|
||||
.number_of_clients
|
||||
@@ -63,40 +64,35 @@ impl NeoMediaSender {
|
||||
self.inspect_bcmedia(&bc_media).await?;
|
||||
match bc_media {
|
||||
BcMedia::Iframe(frame) => {
|
||||
std::mem::swap(buffer_read, buffer_write);
|
||||
buffer_write.clear();
|
||||
buffer_write.push(
|
||||
StampedData {
|
||||
ms: frame.microseconds as u64,
|
||||
data: frame.data.clone(),
|
||||
}
|
||||
);
|
||||
self.process_vidbuffer(&ProcessData {
|
||||
live: StampedData{
|
||||
if buffer_write.len() > 100 {
|
||||
std::mem::swap(buffer_read, buffer_write);
|
||||
buffer_write.clear();
|
||||
std::mem::swap(audbuffer_read, audbuffer_write);
|
||||
audbuffer_write.clear();
|
||||
}
|
||||
let new_data = StampedData {
|
||||
ms: frame.microseconds as u64,
|
||||
data: frame.data,
|
||||
},
|
||||
resend: buffer_read.as_slice()
|
||||
})?;
|
||||
};
|
||||
self.process_vidbuffer(&new_data)?;
|
||||
buffer_write.push(new_data);
|
||||
}
|
||||
BcMedia::Pframe(frame) => {
|
||||
buffer_write.push(
|
||||
StampedData {
|
||||
ms: frame.microseconds as u64,
|
||||
data: frame.data.clone(),
|
||||
}
|
||||
);
|
||||
self.process_vidbuffer(&ProcessData {
|
||||
live: StampedData{
|
||||
let new_data = StampedData {
|
||||
ms: frame.microseconds as u64,
|
||||
data: frame.data,
|
||||
},
|
||||
resend: buffer_read.as_slice()
|
||||
})?;
|
||||
};
|
||||
self.process_vidbuffer(&new_data)?;
|
||||
buffer_write.push(new_data);
|
||||
}
|
||||
BcMedia::Aac(aac) => {
|
||||
if let Some(last) = buffer_write.last().as_ref() {
|
||||
self.process_audbuffer(aac.data.as_slice(), last.ms)?;
|
||||
let new_data = StampedData {
|
||||
ms: last.ms,
|
||||
data: aac.data,
|
||||
};
|
||||
self.process_audbuffer(&new_data)?;
|
||||
audbuffer_write.push(new_data);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -108,7 +104,45 @@ impl NeoMediaSender {
|
||||
}
|
||||
}
|
||||
v = self.clientsource.next() => {
|
||||
if let Some(clientdata) = v {
|
||||
if let Some(mut clientdata) = v {
|
||||
// Resend the video and audio buffers
|
||||
if !clientdata.inited {
|
||||
clientdata.inited = true;
|
||||
let mut vid_resend = buffer_read.iter().chain(buffer_write.iter()).enumerate();
|
||||
let mut aud_resend = audbuffer_read.iter().chain(audbuffer_write.iter()).enumerate();
|
||||
loop {
|
||||
let next_vid = vid_resend.next();
|
||||
if let Some((idx, next_vid)) = next_vid.as_ref() {
|
||||
if let Some(src) = clientdata.vidsrc.as_ref() {
|
||||
if Self::send_buffer(
|
||||
src,
|
||||
next_vid.data.as_slice(),
|
||||
*idx as u64,
|
||||
0,
|
||||
).is_err() {
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
let next_aud = aud_resend.next();
|
||||
if let Some((idx, next_aud)) = next_aud.as_ref() {
|
||||
if let Some(src) = clientdata.audsrc.as_ref() {
|
||||
if Self::send_buffer(
|
||||
src,
|
||||
next_aud.data.as_slice(),
|
||||
*idx as u64,
|
||||
0,
|
||||
).is_err() {
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
if next_vid.is_none() && next_aud.is_none() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Save the client for future sends
|
||||
self.clientdata.insert(self.uid.fetch_add(1, Ordering::Relaxed) , clientdata);
|
||||
} else {
|
||||
break;
|
||||
@@ -127,30 +161,17 @@ impl NeoMediaSender {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_vidbuffer(&mut self, process_data: &ProcessData) -> AnyResult<()> {
|
||||
fn process_vidbuffer(&mut self, stamped_data: &StampedData) -> AnyResult<()> {
|
||||
self.clientdata.retain(|_, data| {
|
||||
if let Some(vidsrc) = data.vidsrc.as_ref() {
|
||||
// If ! inited then we just joined the stream
|
||||
// Send all data from the last iframe to catch up
|
||||
// This should eliminate visual artifact that arise from recieving a pframe
|
||||
// before an iframe
|
||||
if !data.inited {
|
||||
data.inited = true;
|
||||
for (idx, resend_buffer) in process_data.resend.iter().enumerate() {
|
||||
if Self::send_buffer(vidsrc, resend_buffer.data.as_slice(), idx as u64, 0)
|
||||
.is_err()
|
||||
{
|
||||
return false; // If fails then this appsrc is dead
|
||||
}
|
||||
}
|
||||
}
|
||||
Self::send_buffer(
|
||||
vidsrc,
|
||||
process_data.live.data.as_slice(),
|
||||
process_data.live.ms,
|
||||
stamped_data.data.as_slice(),
|
||||
stamped_data.ms,
|
||||
data.start_time,
|
||||
)
|
||||
.is_ok() // If ok retain is true
|
||||
.is_ok()
|
||||
// If ok retain is true
|
||||
} else {
|
||||
// Audio only
|
||||
true
|
||||
@@ -160,10 +181,16 @@ impl NeoMediaSender {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_audbuffer(&mut self, buf: &[u8], frame_ms: u64) -> AnyResult<()> {
|
||||
fn process_audbuffer(&mut self, stamped_data: &StampedData) -> AnyResult<()> {
|
||||
self.clientdata.retain(|_, data| {
|
||||
if let Some(audsrc) = data.audsrc.as_ref() {
|
||||
Self::send_buffer(audsrc, buf, frame_ms, data.start_time).is_ok()
|
||||
Self::send_buffer(
|
||||
audsrc,
|
||||
stamped_data.data.as_slice(),
|
||||
stamped_data.ms,
|
||||
data.start_time,
|
||||
)
|
||||
.is_ok()
|
||||
// If ok retain is true
|
||||
} else {
|
||||
// video only
|
||||
|
||||
@@ -182,6 +182,9 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
break;
|
||||
}
|
||||
}
|
||||
if streaming.get_config().pause.on_motion || streaming.get_config().pause.on_disconnect {
|
||||
log::info!("Pause buffer prepared");
|
||||
}
|
||||
|
||||
// tokio::task::spawn(async move {
|
||||
// let mut inter = tokio::time::interval(tokio::time::Duration::from_secs_f32(0.01));
|
||||
|
||||
Reference in new issue
Block a user