Make streams abortable like motion
This commit is contained in:
4 files changed
+45
-38
No files matched your search
@@ -2,17 +2,23 @@ use super::{BcCamera, BinarySubscriber, Result};
|
||||
use crate::{
|
||||
bc::{model::*, xml::*},
|
||||
bcmedia::model::*,
|
||||
Never,
|
||||
};
|
||||
|
||||
/// Convience type for the error raised by the [StreamOutput] trait
|
||||
pub type StreamOutputError = Result<()>;
|
||||
pub type StreamOutputError = Result<bool>;
|
||||
|
||||
/// The method [`BcCamera::start_video()`] requires a structure with this trait to pass the
|
||||
/// audio and video data back to
|
||||
pub trait StreamOutput {
|
||||
/// This is the callback raised a complete media packet is received
|
||||
fn write(&mut self, media: BcMedia) -> StreamOutputError;
|
||||
///
|
||||
/// If result is `Ok(true)` more messages will be sent
|
||||
///
|
||||
/// If result if `Ok(false)` then message will be stopped
|
||||
///
|
||||
/// If result is `Err(E)` then messages be stopped
|
||||
/// and an error will be thrown
|
||||
fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError;
|
||||
}
|
||||
|
||||
impl BcCamera {
|
||||
@@ -29,7 +35,7 @@ impl BcCamera {
|
||||
///
|
||||
/// This will block forever or return an error when the camera connection is dropped
|
||||
///
|
||||
pub fn start_video<Outputs>(&self, data_outs: &mut Outputs, stream_name: &str) -> Result<Never>
|
||||
pub fn start_video<Outputs>(&self, data_outs: &mut Outputs, stream_name: &str) -> Result<()>
|
||||
where
|
||||
Outputs: StreamOutput,
|
||||
{
|
||||
@@ -72,7 +78,11 @@ impl BcCamera {
|
||||
loop {
|
||||
let bc_media = BcMedia::deserialize(&mut media_sub)?;
|
||||
// We now have a complete interesting packet. Send it to on the callback
|
||||
data_outs.write(bc_media)?;
|
||||
match data_outs.stream_recv(bc_media) {
|
||||
Ok(true) => {}
|
||||
Ok(false) => return Ok(()),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,11 +31,6 @@ pub mod bc_protocol;
|
||||
/// Contains low level structures and formats for the media substream
|
||||
pub mod bcmedia;
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Certain method just as `start_video` will block forever or return an error
|
||||
/// In such a case the return type is `Result<Never, Error>`
|
||||
pub enum Never {}
|
||||
|
||||
/// This is the top level error structure of the library
|
||||
///
|
||||
/// Most commands will either return their `Ok(result)` or this `Err(Error)`
|
||||
|
||||
+2
-2
@@ -57,7 +57,7 @@ enum StreamFormat {
|
||||
}
|
||||
|
||||
impl StreamOutput for GstOutputs {
|
||||
fn write(&mut self, media: BcMedia) -> StreamOutputError {
|
||||
fn stream_recv(&mut self, media: BcMedia) -> StreamOutputError {
|
||||
match media {
|
||||
BcMedia::Iframe(payload) => {
|
||||
let video_type = match payload.video_type {
|
||||
@@ -88,7 +88,7 @@ impl StreamOutput for GstOutputs {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+28
-26
@@ -30,7 +30,6 @@
|
||||
///
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::BcCamera;
|
||||
use neolink_core::Never;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
@@ -118,37 +117,40 @@ fn camera_loop(
|
||||
stream_name: &str,
|
||||
outputs: &mut GstOutputs,
|
||||
manage: bool,
|
||||
) -> Result<Never, Error> {
|
||||
) -> Result<(), Error> {
|
||||
let min_backoff = Duration::from_secs(1);
|
||||
let max_backoff = Duration::from_secs(15);
|
||||
let mut current_backoff = min_backoff;
|
||||
|
||||
loop {
|
||||
let cam_err = camera_main(camera_config, stream_name, outputs, manage).unwrap_err();
|
||||
outputs.vidsrc.on_stream_error();
|
||||
outputs.audsrc.on_stream_error();
|
||||
// Authentication failures are permanent; we retry everything else
|
||||
if cam_err.connected {
|
||||
current_backoff = min_backoff;
|
||||
}
|
||||
match cam_err.err {
|
||||
neolink_core::Error::AuthFailed => {
|
||||
error!(
|
||||
"Authentication failed to camera {}, not retrying",
|
||||
camera_config.name
|
||||
);
|
||||
return Err(cam_err.err.into());
|
||||
if let Err(cam_err) = camera_main(camera_config, stream_name, outputs, manage) {
|
||||
outputs.vidsrc.on_stream_error();
|
||||
outputs.audsrc.on_stream_error();
|
||||
// Authentication failures are permanent; we retry everything else
|
||||
if cam_err.connected {
|
||||
current_backoff = min_backoff;
|
||||
}
|
||||
match cam_err.err {
|
||||
neolink_core::Error::AuthFailed => {
|
||||
error!(
|
||||
"Authentication failed to camera {}, not retrying",
|
||||
camera_config.name
|
||||
);
|
||||
return Err(cam_err.err.into());
|
||||
}
|
||||
_ => error!(
|
||||
"Error streaming from camera {}, will retry in {}s: {:?}",
|
||||
camera_config.name,
|
||||
current_backoff.as_secs(),
|
||||
cam_err.err
|
||||
),
|
||||
}
|
||||
_ => error!(
|
||||
"Error streaming from camera {}, will retry in {}s: {:?}",
|
||||
camera_config.name,
|
||||
current_backoff.as_secs(),
|
||||
cam_err.err
|
||||
),
|
||||
}
|
||||
|
||||
std::thread::sleep(current_backoff);
|
||||
current_backoff = std::cmp::min(max_backoff, current_backoff * 2);
|
||||
std::thread::sleep(current_backoff);
|
||||
current_backoff = std::cmp::min(max_backoff, current_backoff * 2);
|
||||
} else {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +209,7 @@ fn camera_main(
|
||||
stream_name: &str,
|
||||
outputs: &mut GstOutputs,
|
||||
manage: bool,
|
||||
) -> Result<Never, CameraErr> {
|
||||
) -> Result<(), CameraErr> {
|
||||
let mut connected = false;
|
||||
(|| {
|
||||
let mut camera = BcCamera::new_with_addr(&camera_config.camera_addr, camera_config.channel_id)?;
|
||||
|
||||
Reference in new issue
Block a user