Merge pull request #63 from QuantumEntangledAndy/fix/bytes_remaining
Fix/bytes remaining and blocking
This commit is contained in:
20 files changed
+387
-140
No files matched your search
@@ -15,7 +15,7 @@ jobs:
|
||||
if [ -n $SECRET ]; then HAS_SECRET='true' ; fi
|
||||
echo "HAS_SECRET_TOKEN=${HAS_SECRET}" >> $GITHUB_OUTPUT
|
||||
env:
|
||||
SECRET: "${{ secrets.DOCKER_TOKEN }}"
|
||||
SECRET: "${{ secrets.DOCKER_PRUNE_TOKEN }}"
|
||||
- name: Convert username to lower case for docker
|
||||
id: string_user
|
||||
if: ${{ steps.vars.outputs.HAS_SECRET_TOKEN }}
|
||||
@@ -124,4 +124,4 @@ jobs:
|
||||
DAYS: "100"
|
||||
REPO_NAME: ${{ steps.string_repo.outputs.lowercase }}
|
||||
OWNER: ${{ steps.string_user.outputs.lowercase }}
|
||||
TOKEN: ${{ secrets.DOCKER_TOKEN }}
|
||||
TOKEN: ${{ secrets.DOCKER_PRUNE_TOKEN }}
|
||||
Generated
+1
@@ -209,6 +209,7 @@ dependencies = [
|
||||
"anstyle",
|
||||
"bitflags",
|
||||
"clap_lex",
|
||||
"once_cell",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ members = [
|
||||
[dependencies]
|
||||
anyhow = "1.0.70"
|
||||
byte-slice-cast = "1.2.2"
|
||||
clap = { version = "4.2.2", features = ["derive"] }
|
||||
clap = { version = "4.2.2", features = ["derive", "cargo"] }
|
||||
crossbeam-channel = "0.5.8"
|
||||
env_logger = "*"
|
||||
futures = "0.3.28"
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::bc::model::*;
|
||||
use crate::bc::xml::*;
|
||||
use crate::{Credentials, Error, Result};
|
||||
use bytes::BytesMut;
|
||||
use std::io;
|
||||
use tokio_util::codec::{Decoder, Encoder};
|
||||
|
||||
pub(crate) struct BcCodex {
|
||||
@@ -45,6 +46,24 @@ impl Encoder<Bc> for BcCodex {
|
||||
impl Decoder for BcCodex {
|
||||
type Item = Bc;
|
||||
type Error = Error;
|
||||
|
||||
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
match self.decode(buf)? {
|
||||
Some(frame) => Ok(Some(frame)),
|
||||
None => {
|
||||
if buf.is_empty() {
|
||||
Ok(None)
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("bytes remaining on BC stream: {:X?}", buf),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
// trace!("Decoding: {:X?}", src);
|
||||
let bc = Bc::deserialize(&self.context, src);
|
||||
|
||||
@@ -256,6 +256,7 @@ impl BcCamera {
|
||||
return Ok(CameraLocation::Tcp(addr));
|
||||
}
|
||||
}
|
||||
debug!("TCP fail");
|
||||
// Tcp failed see if Local faired any better
|
||||
// Wait for all local to finish
|
||||
while let Some(result) = local_set.join_next().await {
|
||||
@@ -263,6 +264,7 @@ impl BcCamera {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Local fail");
|
||||
// Local failed see if Remote faired any better
|
||||
// Wait for all remote to finish
|
||||
while let Some(result) = remote_set.join_next().await {
|
||||
@@ -270,6 +272,7 @@ impl BcCamera {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Remote fail");
|
||||
// Remote failed see if Map faired any better
|
||||
// Wait for all Map to finish
|
||||
while let Some(result) = map_set.join_next().await {
|
||||
@@ -277,6 +280,7 @@ impl BcCamera {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Map fail");
|
||||
// Map failed see if Relay faired any better
|
||||
// Wait for all Relay to finish
|
||||
while let Some(result) = relay_set.join_next().await {
|
||||
@@ -284,6 +288,7 @@ impl BcCamera {
|
||||
return Ok(CameraLocation::Udp(discovery));
|
||||
}
|
||||
}
|
||||
debug!("Relay (All) fail");
|
||||
// Nothing works
|
||||
Err(Error::CannotInitCamera)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ use std::collections::btree_map::Entry;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::{channel, Sender};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::task::yield_now;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
|
||||
use tokio::{sync::RwLock, task::JoinSet};
|
||||
|
||||
@@ -29,42 +30,42 @@ pub(crate) type BcConnSource = Box<dyn Stream<Item = Result<Bc>> + Send + Sync +
|
||||
///
|
||||
/// There can be only one subscriber per kind of message at a time.
|
||||
pub struct BcConnection {
|
||||
sink: Arc<Mutex<BcConnSink>>,
|
||||
sink: Sender<Result<Bc>>,
|
||||
subscribers: Arc<Subscriber>,
|
||||
#[allow(dead_code)] // Not dead we just need to hold a reference to keep it alive
|
||||
rx_thread: RwLock<JoinSet<Result<()>>>,
|
||||
}
|
||||
|
||||
impl BcConnection {
|
||||
pub async fn new(sink: BcConnSink, mut source: BcConnSource) -> Result<BcConnection> {
|
||||
pub async fn new(mut sink: BcConnSink, mut source: BcConnSource) -> Result<BcConnection> {
|
||||
let subscribers: Arc<Subscriber> = Default::default();
|
||||
let sink = Arc::new(Mutex::new(sink));
|
||||
|
||||
let subs = subscribers.clone();
|
||||
|
||||
let (sinker, sinker_rx) = channel::<Result<Bc>>(100);
|
||||
let mut rx_thread = JoinSet::new();
|
||||
let sink_thread = sink.clone();
|
||||
let sink_thread = sinker.clone();
|
||||
rx_thread.spawn(async move {
|
||||
loop {
|
||||
yield_now().await; // Give other tasks a chance to run before pulling next packet
|
||||
let packet = source.next().await;
|
||||
let bc = match packet {
|
||||
Some(res) => res,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
Self::poll(bc, &subs, &sink_thread).await?
|
||||
}
|
||||
});
|
||||
|
||||
rx_thread.spawn(async move { sink.send_all(&mut ReceiverStream::new(sinker_rx)).await });
|
||||
|
||||
Ok(BcConnection {
|
||||
sink,
|
||||
sink: sinker,
|
||||
subscribers,
|
||||
rx_thread: RwLock::new(rx_thread),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) async fn send(&self, bc: Bc) -> crate::Result<()> {
|
||||
self.sink.lock().await.send(bc).await?;
|
||||
self.sink.send(Ok(bc)).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -113,7 +114,7 @@ impl BcConnection {
|
||||
async fn poll(
|
||||
response: Result<Bc>,
|
||||
subscribers: &Subscriber,
|
||||
sink: &Mutex<BcConnSink>,
|
||||
sink: &Sender<Result<Bc>>,
|
||||
) -> Result<()> {
|
||||
match response {
|
||||
Ok(response) => {
|
||||
@@ -128,7 +129,7 @@ impl BcConnection {
|
||||
(Some(occ), _) => {
|
||||
if let Some(reply) = occ(&response) {
|
||||
assert!(reply.meta.msg_num == response.meta.msg_num);
|
||||
sink.lock().await.send(reply).await?;
|
||||
sink.send(Ok(reply)).await?;
|
||||
}
|
||||
}
|
||||
(None, Some(occ)) => {
|
||||
|
||||
@@ -48,6 +48,7 @@ impl<'a> Stream for BcPayloadStream<'a> {
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<IoResult<Vec<u8>>>> {
|
||||
// log::debug!("PayloadStream: Poll");
|
||||
match Pin::new(&mut self.rx).poll_next(cx) {
|
||||
Poll::Ready(Some(Ok(Bc {
|
||||
body:
|
||||
@@ -57,17 +58,27 @@ impl<'a> Stream for BcPayloadStream<'a> {
|
||||
}),
|
||||
..
|
||||
}))) => {
|
||||
// log::debug!("PayloadStream: Data");
|
||||
return Poll::Ready(Some(Ok(data)));
|
||||
}
|
||||
Poll::Ready(Some(Ok(_bc))) => {
|
||||
// trace!("Got other BC in payload stream");
|
||||
// log::debug!("PayloadStream: Other Data!");
|
||||
}
|
||||
Poll::Ready(Some(Err(e))) => {
|
||||
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))))
|
||||
// log::debug!("PayloadStream: Err");
|
||||
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))));
|
||||
}
|
||||
Poll::Ready(None) => {
|
||||
// log::debug!("PayloadStream: None");
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
Poll::Pending => {
|
||||
// log::debug!("PayloadStream: Pend");
|
||||
return Poll::Pending;
|
||||
}
|
||||
Poll::Ready(None) => return Poll::Ready(None),
|
||||
Poll::Pending => return Poll::Pending,
|
||||
}
|
||||
// log::debug!("PayloadStream: Default Pend and Wake");
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::bcudp::xml::*;
|
||||
use crate::{Error, Result};
|
||||
use futures::{
|
||||
sink::SinkExt,
|
||||
stream::{FuturesUnordered, SplitSink, StreamExt},
|
||||
stream::{FuturesUnordered, StreamExt},
|
||||
};
|
||||
use lazy_static::lazy_static;
|
||||
use local_ip_address::local_ip;
|
||||
@@ -24,7 +24,7 @@ use tokio::{
|
||||
net::UdpSocket,
|
||||
sync::{
|
||||
mpsc::{channel, Receiver, Sender},
|
||||
Mutex, RwLock,
|
||||
RwLock,
|
||||
},
|
||||
task::JoinSet,
|
||||
time::{interval, timeout, Duration},
|
||||
@@ -83,7 +83,7 @@ type ArcFramedSocket = UdpFramed<BcUdpCodex, Arc<UdpSocket>>;
|
||||
pub(crate) struct Discoverer {
|
||||
socket: Arc<UdpSocket>,
|
||||
handle: RwLock<JoinSet<()>>,
|
||||
writer: Mutex<SplitSink<ArcFramedSocket, (BcUdp, SocketAddr)>>,
|
||||
writer: Sender<Result<(BcUdp, SocketAddr)>>,
|
||||
subsribers: Subscriber,
|
||||
local_addr: SocketAddr,
|
||||
}
|
||||
@@ -94,7 +94,7 @@ impl Discoverer {
|
||||
let local_addr = socket.local_addr()?;
|
||||
let inner: ArcFramedSocket = UdpFramed::new(socket.clone(), BcUdpCodex::new());
|
||||
|
||||
let (writer, mut reader) = inner.split();
|
||||
let (mut writer, mut reader) = inner.split();
|
||||
let mut set = JoinSet::new();
|
||||
let subsribers: Subscriber = Default::default();
|
||||
|
||||
@@ -138,10 +138,15 @@ impl Discoverer {
|
||||
}
|
||||
});
|
||||
|
||||
let (sinker, sinker_rx) = channel(100);
|
||||
set.spawn(async move {
|
||||
let _ = writer.send_all(&mut ReceiverStream::new(sinker_rx)).await;
|
||||
});
|
||||
|
||||
Ok(Discoverer {
|
||||
socket,
|
||||
handle: RwLock::new(set),
|
||||
writer: Mutex::new(writer),
|
||||
writer: sinker,
|
||||
subsribers,
|
||||
local_addr,
|
||||
})
|
||||
@@ -192,7 +197,10 @@ impl Discoverer {
|
||||
}
|
||||
|
||||
async fn send(&self, disc: BcUdp, addr: SocketAddr) -> Result<()> {
|
||||
self.writer.lock().await.send((disc, addr)).await
|
||||
self.writer
|
||||
.send(Ok((disc, addr)))
|
||||
.await
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
fn local_addr(&self) -> &SocketAddr {
|
||||
|
||||
@@ -199,9 +199,10 @@ impl Sink<(BcUdp, SocketAddr)> for BcUdpSource {
|
||||
}
|
||||
|
||||
enum State {
|
||||
Normal,
|
||||
Flushing,
|
||||
Closed,
|
||||
Normal, // Normal recieve
|
||||
Flushing, // Used to send ack packets and things in the buffer
|
||||
Closed, // Used to shutdown
|
||||
YieldNow, // Used to ensure we rest between polling packets so as to not starve the runtime
|
||||
}
|
||||
|
||||
pub(crate) struct UdpPayloadSource {
|
||||
@@ -277,14 +278,12 @@ impl UdpPayloadSource {
|
||||
fn maintanence(&mut self, cx: &mut Context<'_>) {
|
||||
// Check for periodic resends
|
||||
if self.resend_interval.poll_tick(cx).is_ready() {
|
||||
trace!("UDPSource.RecievedPacket: Resend");
|
||||
for (_, resend) in self.sent.iter() {
|
||||
self.send_buffer.push_back(BcUdp::Data(resend.clone()));
|
||||
}
|
||||
}
|
||||
if self.ack_interval.poll_tick(cx).is_ready() {
|
||||
let ack = BcUdp::Ack(self.build_send_ack());
|
||||
trace!("UDPSource.RecievedPacket: QueueingAck");
|
||||
self.send_buffer.push_back(ack);
|
||||
self.state = State::Flushing;
|
||||
}
|
||||
@@ -295,15 +294,13 @@ impl Stream for UdpPayloadSource {
|
||||
type Item = IoResult<Vec<u8>>;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
trace!("UdpPayloadSource.poll_next");
|
||||
let camera_addr = self.inner.addr;
|
||||
let mut this = self.get_mut();
|
||||
match this.state {
|
||||
State::Normal => {
|
||||
trace!("UdpPayloadSource.State: Normal");
|
||||
this.state = State::YieldNow;
|
||||
// Data ready to go
|
||||
if let Some(payload) = this.recieved.remove(&this.packets_want) {
|
||||
trace!("UdpPayloadSource.Data: PreReady: {}", this.packets_want);
|
||||
this.packets_want += 1;
|
||||
// error!("packets_want: {}", this.packets_want);
|
||||
return Poll::Ready(Some(Ok(payload)));
|
||||
@@ -324,20 +321,12 @@ impl Stream for UdpPayloadSource {
|
||||
&& packet_id >= this.packets_want =>
|
||||
{
|
||||
if packet_id == this.packets_want {
|
||||
trace!(
|
||||
"UdpPayloadSource.RecievedPacker: NewData: {}",
|
||||
this.packets_want
|
||||
);
|
||||
this.packets_want += 1;
|
||||
// error!("packets_want: {}", this.packets_want);
|
||||
let ack = BcUdp::Ack(this.build_send_ack());
|
||||
this.send_buffer.push_back(ack);
|
||||
return Poll::Ready(Some(Ok(payload)));
|
||||
} else {
|
||||
trace!(
|
||||
"UdpPayloadSource.RecievedPacker: Future Data: {}",
|
||||
packet_id
|
||||
);
|
||||
this.recieved.insert(packet_id, payload);
|
||||
}
|
||||
}
|
||||
@@ -358,17 +347,14 @@ impl Stream for UdpPayloadSource {
|
||||
BcUdp::Ack(ack @ UdpAck { connection_id, .. }),
|
||||
addr,
|
||||
)))) if connection_id == this.client_id && addr == camera_addr => {
|
||||
trace!("UdpPayloadSource.RecievedPacket: Ack: {:?}", ack);
|
||||
this.handle_ack(ack);
|
||||
// Rather then immediatly flush wait for the next call
|
||||
// this.state = State::Flushing;
|
||||
}
|
||||
Poll::Ready(Some(Err(e))) => {
|
||||
trace!("UdpPayloadSource.RecievedPacker: Error");
|
||||
return Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e))));
|
||||
}
|
||||
Poll::Ready(None) => {
|
||||
trace!("UdpPayloadSource.RecievedPacker: Empty");
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
Poll::Pending => {
|
||||
@@ -386,24 +372,23 @@ impl Stream for UdpPayloadSource {
|
||||
// }
|
||||
}
|
||||
}
|
||||
State::Flushing => {
|
||||
trace!("UdpPayloadSource.State: Flushing");
|
||||
match this.poll_flush_unpin(cx) {
|
||||
Poll::Ready(Ok(())) => {
|
||||
this.state = State::Normal;
|
||||
}
|
||||
Poll::Ready(Err(e)) => {
|
||||
return Poll::Ready(Some(Err(e)));
|
||||
}
|
||||
Poll::Pending => {
|
||||
return Poll::Pending;
|
||||
}
|
||||
State::Flushing => match this.poll_flush_unpin(cx) {
|
||||
Poll::Ready(Ok(())) => {
|
||||
this.state = State::Normal;
|
||||
}
|
||||
}
|
||||
Poll::Ready(Err(e)) => {
|
||||
return Poll::Ready(Some(Err(e)));
|
||||
}
|
||||
Poll::Pending => {
|
||||
return Poll::Pending;
|
||||
}
|
||||
},
|
||||
State::Closed => {
|
||||
trace!("UdpPayloadSource.State: Closed");
|
||||
return Poll::Ready(Some(Err(IoError::from(ErrorKind::ConnectionAborted))));
|
||||
}
|
||||
State::YieldNow => {
|
||||
this.state = State::Normal;
|
||||
}
|
||||
}
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
@@ -419,7 +404,7 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
|
||||
) -> Poll<std::result::Result<(), Self::Error>> {
|
||||
let this = self.get_mut();
|
||||
match this.state {
|
||||
State::Normal => this
|
||||
State::Normal | State::YieldNow => this
|
||||
.inner
|
||||
.poll_ready_unpin(cx)
|
||||
.map_err(|e| IoError::new(ErrorKind::Other, e)),
|
||||
@@ -496,6 +481,7 @@ impl Sink<Vec<u8>> for UdpPayloadSource {
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
}
|
||||
|
||||
fn poll_close(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
|
||||
@@ -39,6 +39,10 @@ pub enum Error {
|
||||
#[error(display = "Camera responded with Service Unavaliable")]
|
||||
CameraServiceUnavaliable,
|
||||
|
||||
/// Raised when the camera responds with a status code over than OK during login
|
||||
#[error(display = "Camera responded with Err during login")]
|
||||
CameraLoginFail,
|
||||
|
||||
/// Raised when a connection is dropped.
|
||||
#[error(display = "Dropped connection")]
|
||||
DroppedConnection,
|
||||
@@ -51,6 +55,10 @@ pub enum Error {
|
||||
#[error(display = "Dropped connection")]
|
||||
BroadcastDroppedConnectionTry(#[error(source)] tokio::sync::broadcast::error::TryRecvError),
|
||||
|
||||
/// Raised when a connection is dropped during a TryRecv event
|
||||
#[error(display = "Send Error")]
|
||||
TokioBcSendError,
|
||||
|
||||
/// Raised when the TIMEOUT is reach
|
||||
#[error(display = "Timeout")]
|
||||
Timeout(#[error(source)] std::sync::Arc<tokio::time::error::Elapsed>),
|
||||
@@ -194,6 +202,12 @@ impl From<std::io::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
|
||||
fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
|
||||
Error::TokioBcSendError
|
||||
}
|
||||
}
|
||||
|
||||
impl From<cookie_factory::GenError> for Error {
|
||||
fn from(k: cookie_factory::GenError) -> Self {
|
||||
Error::GenError(std::sync::Arc::new(k))
|
||||
|
||||
@@ -136,7 +136,7 @@ impl BcCamera {
|
||||
sub_login.send(modern_login).await?;
|
||||
let modern_reply = sub_login.recv().await?;
|
||||
if modern_reply.meta.response_code != 200 {
|
||||
return Err(Error::CameraServiceUnavaliable);
|
||||
return Err(Error::CameraLoginFail);
|
||||
}
|
||||
|
||||
match modern_reply.body {
|
||||
|
||||
@@ -31,7 +31,6 @@ pub enum StreamKind {
|
||||
///
|
||||
/// When this object is dropped the streaming is stopped
|
||||
pub struct StreamData {
|
||||
#[allow(dead_code)]
|
||||
handle: Option<JoinHandle<Result<()>>>,
|
||||
rx: Receiver<Result<BcMedia>>,
|
||||
abort_handle: Arc<AtomicBool>,
|
||||
@@ -41,9 +40,27 @@ impl StreamData {
|
||||
/// Pull data from the camera's buffer
|
||||
/// This returns raw BcMedia packets
|
||||
pub async fn get_data(&mut self) -> Result<Result<BcMedia>> {
|
||||
if let Some(handle) = self.handle.as_mut() {
|
||||
if handle.is_finished() {
|
||||
self.abort_handle.store(true, Ordering::Relaxed);
|
||||
handle.await??;
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
} else {
|
||||
self.abort_handle.store(true, Ordering::Relaxed);
|
||||
return Err(Error::DroppedConnection);
|
||||
}
|
||||
// debug!("StreamData: Get");
|
||||
match self.rx.recv().await {
|
||||
Some(data) => Ok(data),
|
||||
None => Err(Error::DroppedConnection),
|
||||
Some(data) => {
|
||||
// debug!("StreamData: Got");
|
||||
Ok(data)
|
||||
}
|
||||
None => {
|
||||
// debug!("StreamData: Drop");
|
||||
self.abort_handle.store(true, Ordering::Relaxed);
|
||||
Err(Error::DroppedConnection)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,17 +181,23 @@ impl BcCamera {
|
||||
let mut media_sub = sub_video.bcmedia_stream(strict);
|
||||
|
||||
while !abort_handle_thread.load(Ordering::Relaxed) {
|
||||
// debug!("Stream: Get");
|
||||
if let Some(bc_media) = media_sub.next().await {
|
||||
// debug!("Stream: Got");
|
||||
// We now have a complete interesting packet. Send it to on the callback
|
||||
// debug!("Stream: Send");
|
||||
if tx.send(bc_media).await.is_err() {
|
||||
// debug!("Stream: Dropped");
|
||||
break; // Connection dropped
|
||||
}
|
||||
// debug!("Stream: Sent");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// debug!("Stream: Stopping");
|
||||
let stop_video = Bc::new_from_xml(
|
||||
BcMeta {
|
||||
msg_id: MSG_ID_VIDEO_STOP,
|
||||
@@ -194,8 +217,9 @@ impl BcCamera {
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
// debug!("Stream: Send Stop");
|
||||
sub_video.send(stop_video).await?;
|
||||
// debug!("Stream: Sent Stop");
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//!
|
||||
use crate::bcmedia::model::*;
|
||||
use crate::{Error, Result};
|
||||
use bytes::{Buf, BytesMut};
|
||||
use bytes::BytesMut;
|
||||
use log::*;
|
||||
use tokio_util::codec::{Decoder, Encoder};
|
||||
|
||||
@@ -36,6 +36,15 @@ impl Decoder for BcMediaCodex {
|
||||
type Item = BcMedia;
|
||||
type Error = Error;
|
||||
|
||||
/// Since frames can cross EOF boundaries we overload this so it dosen't error if
|
||||
/// there are bytes left on the stream
|
||||
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
match self.decode(buf)? {
|
||||
Some(frame) => Ok(Some(frame)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
let mut amount_skipped: usize = 0;
|
||||
loop {
|
||||
@@ -62,8 +71,9 @@ impl Decoder for BcMediaCodex {
|
||||
debug!("Error in stream attempting to restore");
|
||||
trace!(" Stream Error: {:?}", e);
|
||||
}
|
||||
src.advance(1);
|
||||
amount_skipped += 1;
|
||||
// Drop the whole packet and wait for a packet that starts with magic
|
||||
amount_skipped += src.len();
|
||||
src.clear();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,15 @@ impl Decoder for BcUdpCodex {
|
||||
type Item = BcUdp;
|
||||
type Error = Error;
|
||||
|
||||
/// Since frames can cross EOF boundaries we overload this so it dosen't error if
|
||||
/// there are bytes left on the stream
|
||||
fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
match self.decode(buf)? {
|
||||
Some(frame) => Ok(Some(frame)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>> {
|
||||
match { BcUdp::deserialize(src) } {
|
||||
Ok(BcUdp::Discovery(UdpDiscovery {
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
use clap::Parser;
|
||||
use clap::{crate_authors, crate_version, Parser};
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -7,7 +7,7 @@ use std::str::FromStr;
|
||||
/// Neolink is free software released under the GNU AGPL v3.
|
||||
/// You can find its source code at https://github.com/thirtythreeforty/neolink
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(name = "neolink", arg_required_else_help = true)]
|
||||
#[command(name = "neolink", arg_required_else_help = true, version = crate_version!(), author = crate_authors!("\n"))]
|
||||
pub struct Opt {
|
||||
#[arg(short, long, global = true, value_parser = PathBuf::from_str)]
|
||||
pub config: Option<PathBuf>,
|
||||
|
||||
+70
-7
@@ -12,7 +12,7 @@ use gstreamer::{
|
||||
Structure,
|
||||
};
|
||||
use gstreamer::{Bin, Caps, ClockTime, Element, ElementFactory};
|
||||
use gstreamer_app::{AppSrc, AppStreamType};
|
||||
use gstreamer_app::{AppSrc, AppSrcCallbacks, AppStreamType};
|
||||
use gstreamer_rtsp::RTSPUrl;
|
||||
use gstreamer_rtsp_server::prelude::*;
|
||||
use gstreamer_rtsp_server::subclass::prelude::*;
|
||||
@@ -124,7 +124,7 @@ impl Drop for NeoMediaFactoryImpl {
|
||||
|
||||
impl Default for NeoMediaFactoryImpl {
|
||||
fn default() -> Self {
|
||||
warn!("Constructing Factor Impl");
|
||||
debug!("Constructing Factor Impl");
|
||||
let (datasender, datarx) = channel(3);
|
||||
let (clientsender, rx_clientsender) = channel(3);
|
||||
let shared: Arc<NeoMediaShared> = Default::default();
|
||||
@@ -139,7 +139,20 @@ impl Default for NeoMediaFactoryImpl {
|
||||
clientdata: Default::default(),
|
||||
waiting_for_iframe: true,
|
||||
};
|
||||
threads.spawn(async move { sender.run().await });
|
||||
threads.spawn(async move {
|
||||
loop {
|
||||
match sender.run().await {
|
||||
Err(e) => {
|
||||
warn!("Media send thead failed... restarting. Cause: {:?}", e);
|
||||
}
|
||||
Ok(()) => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
sender.waiting_for_iframe = true;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
|
||||
Self {
|
||||
sender: datasender,
|
||||
@@ -209,6 +222,7 @@ impl NeoMediaFactoryImpl {
|
||||
let bin = media
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
let clock = bin.clock();
|
||||
// Clear the autogenerated ones
|
||||
for element in bin.iterate_elements().into_iter().flatten() {
|
||||
bin.remove(&element)?;
|
||||
@@ -216,6 +230,7 @@ impl NeoMediaFactoryImpl {
|
||||
|
||||
let mut client_data = ClientPipelineData {
|
||||
start_time: self.shared.microseconds.load(Ordering::Relaxed),
|
||||
clock,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -230,11 +245,30 @@ impl NeoMediaFactoryImpl {
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let enough_data_start = client_data.enough_data.clone();
|
||||
let enough_data_stop = client_data.enough_data.clone();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
if enough_data_stop.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is in need of data");
|
||||
}
|
||||
enough_data_stop.store(false, Ordering::Relaxed);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
if !enough_data_start.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is full of of data");
|
||||
}
|
||||
enough_data_start.store(true, Ordering::Relaxed);
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h265").field("parsed", false).build(),
|
||||
// ));
|
||||
@@ -242,6 +276,8 @@ impl NeoMediaFactoryImpl {
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_element("queue", "source_queue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
let parser = make_element("h265parse", "parser")?;
|
||||
let payload = make_element("rtph265pay", "pay0")?;
|
||||
bin.add_many(&[&source, &queue, &parser, &payload])?;
|
||||
@@ -261,11 +297,30 @@ impl NeoMediaFactoryImpl {
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
|
||||
let enough_data_start = client_data.enough_data.clone();
|
||||
let enough_data_stop = client_data.enough_data.clone();
|
||||
source.set_callbacks(
|
||||
AppSrcCallbacks::builder()
|
||||
.need_data(move |_appsrc, _amt| {
|
||||
if enough_data_stop.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is in need of data");
|
||||
}
|
||||
enough_data_stop.store(false, Ordering::Relaxed);
|
||||
})
|
||||
.enough_data(move |_appsrc| {
|
||||
if !enough_data_start.load(Ordering::Relaxed) {
|
||||
log::debug!("Client is full of of data");
|
||||
}
|
||||
enough_data_start.store(true, Ordering::Relaxed);
|
||||
})
|
||||
.build(),
|
||||
);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h264").field("parsed", false).build(),
|
||||
// ));
|
||||
@@ -273,6 +328,8 @@ impl NeoMediaFactoryImpl {
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_element("queue", "source_queue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
|
||||
let parser = make_element("h264parse", "parser")?;
|
||||
let payload = make_element("rtph264pay", "pay0")?;
|
||||
bin.add_many(&[&source, &queue, &parser, &payload])?;
|
||||
@@ -288,6 +345,8 @@ impl NeoMediaFactoryImpl {
|
||||
let source = make_element("videotestsrc", "vidsrc")?;
|
||||
source.set_property_from_str("pattern", "snow");
|
||||
let queue = make_element("queue", "queue0")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
let overlay = make_element("textoverlay", "overlay")?;
|
||||
overlay.set_property("text", "Stream not Ready");
|
||||
overlay.set_property_from_str("valignment", "top");
|
||||
@@ -324,7 +383,7 @@ impl NeoMediaFactoryImpl {
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
@@ -334,6 +393,8 @@ impl NeoMediaFactoryImpl {
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
|
||||
let queue = make_element("queue", "audqueue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
let parser = make_element("aacparse", "audparser")?;
|
||||
let decoder = make_element("decodebin", "auddecoder")?;
|
||||
let encoder = make_element("audioconvert", "audencoder")?;
|
||||
@@ -372,7 +433,7 @@ impl NeoMediaFactoryImpl {
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(true);
|
||||
source.set_block(false);
|
||||
source.set_property("emit-signals", false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
@@ -390,6 +451,8 @@ impl NeoMediaFactoryImpl {
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
|
||||
let queue = make_element("queue", "audqueue")?;
|
||||
queue.set_property_from_str("leaky", "downstream");
|
||||
queue.set_property("max-size-bytes", 104857600u32);
|
||||
let decoder = make_element("decodebin", "auddecoder")?;
|
||||
let encoder = make_element("audioconvert", "audencoder")?;
|
||||
let payload = make_element("rtpL16pay", "pay1")?;
|
||||
|
||||
+130
-53
@@ -2,11 +2,12 @@
|
||||
//! gstreamer media stream
|
||||
use anyhow::anyhow;
|
||||
use futures::stream::StreamExt;
|
||||
use gstreamer::ClockTime;
|
||||
use gstreamer::{prelude::*, ClockTime};
|
||||
use gstreamer_app::AppSrc;
|
||||
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::*;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{hash_map::Entry, HashMap};
|
||||
use std::sync::{
|
||||
atomic::{AtomicU64, Ordering},
|
||||
Arc,
|
||||
@@ -54,14 +55,16 @@ impl NeoMediaSender {
|
||||
self.shared
|
||||
.buffer_ready
|
||||
.store(!buffer_read.is_empty(), Ordering::Relaxed);
|
||||
// debug!("Sender: Get");
|
||||
tokio::select! {
|
||||
v = self.data_source.next() => {
|
||||
// debug!("Sender: Got Data");
|
||||
// debug!("data_source recieved");
|
||||
if let Some(bc_media) = v {
|
||||
if ! self.skip_bcmedia(&bc_media)? {
|
||||
if ! self.skip_bcmedia(&bc_media) {
|
||||
// debug!("Not skipped");
|
||||
// resend_pause.reset();
|
||||
self.inspect_bcmedia(&bc_media).await?;
|
||||
self.inspect_bcmedia(&bc_media).await;
|
||||
match bc_media {
|
||||
BcMedia::Iframe(frame) => {
|
||||
if buffer_write.len() > 100 {
|
||||
@@ -74,7 +77,7 @@ impl NeoMediaSender {
|
||||
ms: frame.microseconds as u64,
|
||||
data: frame.data,
|
||||
};
|
||||
self.process_vidbuffer(&new_data)?;
|
||||
self.process_vidbuffer(&new_data).await;
|
||||
buffer_write.push(new_data);
|
||||
}
|
||||
BcMedia::Pframe(frame) => {
|
||||
@@ -82,7 +85,7 @@ impl NeoMediaSender {
|
||||
ms: frame.microseconds as u64,
|
||||
data: frame.data,
|
||||
};
|
||||
self.process_vidbuffer(&new_data)?;
|
||||
self.process_vidbuffer(&new_data).await;
|
||||
buffer_write.push(new_data);
|
||||
}
|
||||
BcMedia::Aac(aac) => {
|
||||
@@ -91,7 +94,18 @@ impl NeoMediaSender {
|
||||
ms: last.ms,
|
||||
data: aac.data,
|
||||
};
|
||||
self.process_audbuffer(&new_data)?;
|
||||
self.process_audbuffer(&new_data).await;
|
||||
audbuffer_write.push(new_data);
|
||||
}
|
||||
|
||||
}
|
||||
BcMedia::Adpcm(adpcm) => {
|
||||
if let Some(last) = buffer_write.last().as_ref() {
|
||||
let new_data = StampedData {
|
||||
ms: last.ms,
|
||||
data: adpcm.data,
|
||||
};
|
||||
self.process_audbuffer(&new_data).await;
|
||||
audbuffer_write.push(new_data);
|
||||
}
|
||||
|
||||
@@ -104,6 +118,7 @@ impl NeoMediaSender {
|
||||
}
|
||||
}
|
||||
v = self.clientsource.next() => {
|
||||
// debug!("Sender: Got Client");
|
||||
if let Some(mut clientdata) = v {
|
||||
// Resend the video and audio buffers
|
||||
if !clientdata.inited {
|
||||
@@ -119,7 +134,7 @@ impl NeoMediaSender {
|
||||
next_vid.data.as_slice(),
|
||||
*idx as u64,
|
||||
0,
|
||||
).is_err() {
|
||||
).await.is_err() {
|
||||
break;
|
||||
};
|
||||
}
|
||||
@@ -132,7 +147,7 @@ impl NeoMediaSender {
|
||||
next_aud.data.as_slice(),
|
||||
*idx as u64,
|
||||
0,
|
||||
).is_err() {
|
||||
).await.is_err() {
|
||||
break;
|
||||
};
|
||||
}
|
||||
@@ -161,47 +176,96 @@ impl NeoMediaSender {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_vidbuffer(&mut self, stamped_data: &StampedData) -> AnyResult<()> {
|
||||
self.clientdata.retain(|_, data| {
|
||||
if let Some(vidsrc) = data.vidsrc.as_ref() {
|
||||
Self::send_buffer(
|
||||
vidsrc,
|
||||
stamped_data.data.as_slice(),
|
||||
stamped_data.ms,
|
||||
data.start_time,
|
||||
)
|
||||
.is_ok()
|
||||
// If ok retain is true
|
||||
} else {
|
||||
// Audio only
|
||||
true
|
||||
async fn process_vidbuffer(&mut self, stamped_data: &StampedData) {
|
||||
for key in self
|
||||
.clientdata
|
||||
.keys()
|
||||
.cloned()
|
||||
.collect::<Vec<_>>()
|
||||
.drain(..)
|
||||
{
|
||||
match self.clientdata.entry(key) {
|
||||
Entry::Occupied(data) => {
|
||||
if let Some(vidsrc) = data.get().vidsrc.as_ref() {
|
||||
if !data.get().enough_data.load(Ordering::Relaxed) {
|
||||
let ms = stamped_data.ms;
|
||||
let st = data.get().start_time;
|
||||
let micros = ms - st;
|
||||
if let Some(clock) = data.get().clock.as_ref() {
|
||||
if let Some(time) = clock.time() {
|
||||
let buffer_time = ClockTime::from_useconds(micros);
|
||||
log::debug!(
|
||||
"Buffer time: {:?}. Clock time: {:?}",
|
||||
buffer_time,
|
||||
time
|
||||
);
|
||||
}
|
||||
}
|
||||
debug!(
|
||||
"Pushed Video Buffer: ms: {}, st: {}, delta: {}",
|
||||
ms, st, micros
|
||||
);
|
||||
if Self::send_buffer(
|
||||
vidsrc,
|
||||
stamped_data.data.as_slice(),
|
||||
stamped_data.ms,
|
||||
data.get().start_time,
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
data.remove();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data.remove();
|
||||
}
|
||||
}
|
||||
Entry::Vacant(_) => {}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
stamped_data.data.as_slice(),
|
||||
stamped_data.ms,
|
||||
data.start_time,
|
||||
)
|
||||
.is_ok()
|
||||
// If ok retain is true
|
||||
} else {
|
||||
// video only
|
||||
true
|
||||
async fn process_audbuffer(&mut self, stamped_data: &StampedData) {
|
||||
for key in self
|
||||
.clientdata
|
||||
.keys()
|
||||
.cloned()
|
||||
.collect::<Vec<_>>()
|
||||
.drain(..)
|
||||
{
|
||||
match self.clientdata.entry(key) {
|
||||
Entry::Occupied(data) => {
|
||||
if let Some(audsrc) = data.get().audsrc.as_ref() {
|
||||
if !data.get().enough_data.load(Ordering::Relaxed) {
|
||||
debug!("Pushed Audio Buffer");
|
||||
if Self::send_buffer(
|
||||
audsrc,
|
||||
stamped_data.data.as_slice(),
|
||||
stamped_data.ms,
|
||||
data.get().start_time,
|
||||
)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
data.remove();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
data.remove();
|
||||
}
|
||||
}
|
||||
Entry::Vacant(_) => {}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn send_buffer(appsrc: &AppSrc, buf: &[u8], frame_ms: u64, start_time: u64) -> AnyResult<()> {
|
||||
async fn send_buffer(
|
||||
appsrc: &AppSrc,
|
||||
buf: &[u8],
|
||||
frame_ms: u64,
|
||||
start_time: u64,
|
||||
) -> AnyResult<()> {
|
||||
let micros = if frame_ms > start_time {
|
||||
frame_ms - start_time
|
||||
} else {
|
||||
@@ -222,25 +286,39 @@ impl NeoMediaSender {
|
||||
gst_buf_data.copy_from_slice(buf);
|
||||
}
|
||||
// debug!("Buffer pushed");
|
||||
appsrc
|
||||
.push_buffer(gst_buf.copy())
|
||||
.map(|_| ())
|
||||
.map_err(|_| anyhow!("Could not push buffer to appsrc"))
|
||||
let thread_appsrc = appsrc.clone(); // GObjects are refcounted
|
||||
let res = tokio::task::spawn_blocking(move || {
|
||||
thread_appsrc
|
||||
.push_buffer(gst_buf.copy())
|
||||
.map(|_| ())
|
||||
.map_err(|_| anyhow!("Could not push buffer to appsrc"))
|
||||
})
|
||||
.await;
|
||||
match &res {
|
||||
Err(e) => {
|
||||
debug!("Failed to send buffer: {:?}", e);
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
debug!("Failed to send buffer: {:?}", e);
|
||||
}
|
||||
Ok(Ok(_)) => {}
|
||||
};
|
||||
res?
|
||||
}
|
||||
|
||||
fn skip_bcmedia(&mut self, bc_media: &BcMedia) -> AnyResult<bool> {
|
||||
fn skip_bcmedia(&mut self, bc_media: &BcMedia) -> bool {
|
||||
if self.waiting_for_iframe {
|
||||
if let BcMedia::Iframe(_) = bc_media {
|
||||
self.waiting_for_iframe = false;
|
||||
} else {
|
||||
log::debug!("Skipping bcmedia");
|
||||
return Ok(true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
false
|
||||
}
|
||||
|
||||
async fn inspect_bcmedia(&mut self, bc_media: &BcMedia) -> AnyResult<()> {
|
||||
async fn inspect_bcmedia(&mut self, bc_media: &BcMedia) {
|
||||
let old_vid = *self.shared.vid_format.read().await;
|
||||
let old_aud = *self.shared.aud_format.read().await;
|
||||
match bc_media {
|
||||
@@ -294,6 +372,5 @@ impl NeoMediaSender {
|
||||
if old_aud != new_aud {
|
||||
log::debug!("Audio format set to: {:?} from {:?}", new_aud, old_aud);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
//! Data shared between the various
|
||||
//! components that manage a media stream
|
||||
use gstreamer::Clock;
|
||||
use gstreamer_app::AppSrc;
|
||||
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
@@ -45,4 +47,6 @@ pub(super) struct ClientPipelineData {
|
||||
pub(super) audsrc: Option<AppSrc>,
|
||||
pub(super) start_time: u64,
|
||||
pub(super) inited: bool,
|
||||
pub(super) clock: Option<Clock>,
|
||||
pub(super) enough_data: Arc<AtomicBool>,
|
||||
}
|
||||
+12
-1
@@ -95,6 +95,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
set.spawn(async move {
|
||||
let shared = camera.shared.clone();
|
||||
let name = camera.get_name();
|
||||
let mut backoff = Duration::from_micros(125);
|
||||
loop {
|
||||
let failure = camera_main(camera).await;
|
||||
match failure {
|
||||
@@ -104,6 +105,10 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
}
|
||||
Err(CameraFailureKind::Retry(e)) => {
|
||||
warn!("{}: Retryable error: {:X?}", name, e);
|
||||
tokio::time::sleep(backoff).await;
|
||||
if backoff < Duration::from_secs(5) {
|
||||
backoff *= 2;
|
||||
}
|
||||
camera = Camera {
|
||||
shared: shared.clone(),
|
||||
state: Disconnected {},
|
||||
@@ -162,7 +167,13 @@ async fn camera_main(camera: Camera<Disconnected>) -> Result<(), CameraFailureKi
|
||||
.login()
|
||||
.await
|
||||
.with_context(|| format!("{}: Could not login to camera", name))
|
||||
.map_err(CameraFailureKind::Fatal)?;
|
||||
.map_err(|e| {
|
||||
let e_inner = e.downcast_ref::<neolink_core::Error>().unwrap();
|
||||
match e_inner {
|
||||
neolink_core::Error::CameraLoginFail => CameraFailureKind::Fatal(e),
|
||||
_ => CameraFailureKind::Retry(e),
|
||||
}
|
||||
})?;
|
||||
|
||||
let _ = loggedin.manage().await;
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use anyhow::{anyhow, Error, Result};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::BcMedia;
|
||||
use tokio::time::{timeout, Duration};
|
||||
use tokio::{sync::RwLock, task::JoinSet};
|
||||
|
||||
@@ -43,7 +44,7 @@ impl Camera<Streaming> {
|
||||
|
||||
let stream_thead = *stream;
|
||||
let strict_thread = loggedin.shared.config.strict;
|
||||
// let tag_thread = tag.clone();
|
||||
let tag_thread = tag.clone();
|
||||
let mut stream_data = loggedin
|
||||
.state
|
||||
.camera
|
||||
@@ -51,18 +52,21 @@ impl Camera<Streaming> {
|
||||
.await?;
|
||||
set.spawn(async move {
|
||||
loop {
|
||||
// debug!("{}: BcMediaStreamRecv", &tag_thread);
|
||||
// debug!("Straming: Get");
|
||||
let data = timeout(Duration::from_secs(15), stream_data.get_data()).await??;
|
||||
// match &data {
|
||||
// Ok(BcMedia::InfoV1(_)) => debug!("{}: - InfoV1", &tag_thread),
|
||||
// Ok(BcMedia::InfoV2(_)) => debug!("{}: - InfoV2", &tag_thread),
|
||||
// Ok(BcMedia::Iframe(_)) => debug!("{}: - Iframe", &tag_thread),
|
||||
// Ok(BcMedia::Pframe(_)) => debug!("{}: - Pframe", &tag_thread),
|
||||
// Ok(BcMedia::Aac(_)) => debug!("{}: - Aac", &tag_thread),
|
||||
// Ok(BcMedia::Adpcm(_)) => debug!("{}: - Adpcm", &tag_thread),
|
||||
// Err(_) => debug!(" - Error"),
|
||||
// }
|
||||
// debug!("Straming: Got");
|
||||
match &data {
|
||||
Ok(BcMedia::InfoV1(_)) => trace!("{}: - InfoV1", &tag_thread),
|
||||
Ok(BcMedia::InfoV2(_)) => trace!("{}: - InfoV2", &tag_thread),
|
||||
Ok(BcMedia::Iframe(_)) => trace!("{}: - Iframe", &tag_thread),
|
||||
Ok(BcMedia::Pframe(_)) => trace!("{}: - Pframe", &tag_thread),
|
||||
Ok(BcMedia::Aac(_)) => trace!("{}: - Aac", &tag_thread),
|
||||
Ok(BcMedia::Adpcm(_)) => trace!("{}: - Adpcm", &tag_thread),
|
||||
Err(_) => trace!(" - Error"),
|
||||
}
|
||||
// debug!("Straming: Send");
|
||||
sender.send(data?).await?;
|
||||
// debug!("Straming: Sent");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user