Implement own gstremer media and server
This commit is contained in:
1 parent
b45aee252f
commit
8b825cc5fa
24 files changed
+1779
-1342
No files matched your search
Generated
+19
-6
@@ -102,6 +102,17 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic_enum"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6227a8d6fdb862bcb100c4314d0d9579e5cd73fa6df31a2e6f6e1acd3c5f1207"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic_refcell"
|
||||
version = "0.1.9"
|
||||
@@ -1154,9 +1165,9 @@ checksum = "df39d232f5c40b0891c10216992c2f250c054105cb1e56f0fc9032db6203ecc1"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.4.1"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
@@ -1211,6 +1222,7 @@ dependencies = [
|
||||
"aes",
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"atomic_enum",
|
||||
"byte-slice-cast",
|
||||
"cfb-mode",
|
||||
"cookie-factory",
|
||||
@@ -1238,6 +1250,7 @@ dependencies = [
|
||||
"structopt",
|
||||
"time",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"toml",
|
||||
"validator",
|
||||
"validator_derive",
|
||||
@@ -1479,9 +1492,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.9"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
|
||||
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
@@ -2112,9 +2125,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio-stream"
|
||||
version = "0.1.11"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d660770404473ccd7bc9f8b28494a811bc18542b915c0855c51e8f419d5223ce"
|
||||
checksum = "8fb52b74f05dbf495a8fba459fdc331812b96aa086d9eb78101fa0d4569c3313"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"pin-project-lite",
|
||||
|
||||
@@ -53,3 +53,5 @@ async-trait = "0.1.64"
|
||||
futures = "0.3.26"
|
||||
hex-string = "0.1.0"
|
||||
requestty = "0.5.0"
|
||||
atomic_enum = "0.2.0"
|
||||
tokio-stream = "0.1.12"
|
||||
@@ -299,12 +299,12 @@ impl BcCamera {
|
||||
///
|
||||
/// returns either an error or the camera
|
||||
///
|
||||
pub async fn new(options: BcCameraOpt) -> Result<Self> {
|
||||
pub async fn new(options: &BcCameraOpt) -> Result<Self> {
|
||||
let username: String = options.credentials.username.clone();
|
||||
let passwd: Option<String> = options.credentials.password.clone();
|
||||
|
||||
let (sink, source): (BcConnSink, BcConnSource) = {
|
||||
match BcCamera::find_camera(&options).await? {
|
||||
match BcCamera::find_camera(options).await? {
|
||||
CameraLocation::Tcp(addr) => {
|
||||
let (x, r) = TcpSource::new(addr, &username, passwd.as_ref())
|
||||
.await?
|
||||
|
||||
@@ -82,7 +82,7 @@ type Subscriber = Arc<RwLock<BTreeMap<u32, Sender<Result<(UdpDiscovery, SocketAd
|
||||
type ArcFramedSocket = UdpFramed<BcUdpCodex, Arc<UdpSocket>>;
|
||||
pub(crate) struct Discoverer {
|
||||
socket: Arc<UdpSocket>,
|
||||
handle: JoinSet<()>,
|
||||
handle: RwLock<JoinSet<()>>,
|
||||
writer: Mutex<SplitSink<ArcFramedSocket, (BcUdp, SocketAddr)>>,
|
||||
subsribers: Subscriber,
|
||||
local_addr: SocketAddr,
|
||||
@@ -140,18 +140,17 @@ impl Discoverer {
|
||||
|
||||
Ok(Discoverer {
|
||||
socket,
|
||||
handle: set,
|
||||
handle: RwLock::new(set),
|
||||
writer: Mutex::new(writer),
|
||||
subsribers,
|
||||
local_addr,
|
||||
})
|
||||
}
|
||||
|
||||
async fn into_socket(mut self) -> UdpSocket {
|
||||
async fn into_socket(self) -> (Arc<UdpSocket>, JoinSet<()>) {
|
||||
let socket = self.socket.clone();
|
||||
self.handle.shutdown().await;
|
||||
drop(self);
|
||||
Arc::try_unwrap(socket).expect("Should not be shared at this point")
|
||||
let handle = self.handle.into_inner();
|
||||
(socket, handle)
|
||||
}
|
||||
|
||||
async fn subscribe(&self, tid: u32) -> Result<Receiver<Result<(UdpDiscovery, SocketAddr)>>> {
|
||||
@@ -292,6 +291,55 @@ impl Discoverer {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn client_initiated_direct(
|
||||
&self,
|
||||
uid: &str,
|
||||
client_id: i32,
|
||||
addr: SocketAddr,
|
||||
) -> Result<ConnectResult> {
|
||||
let tid = generate_tid();
|
||||
|
||||
let port = self.local_addr().port();
|
||||
let msg = UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2d_c: Some(C2dC {
|
||||
uid: uid.to_string(),
|
||||
cli: ClientList { port: port as u32 },
|
||||
cid: client_id,
|
||||
mtu: MTU,
|
||||
debug: false,
|
||||
os: "MAC".to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
|
||||
let (camera_address, camera_id) = self
|
||||
.retry_send(msg, addr, |bc, addr| match bc {
|
||||
UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
d2c_c_r: Some(D2cCr { did, cid, .. }),
|
||||
..
|
||||
},
|
||||
} if cid == client_id => Some((addr, did)),
|
||||
_ => None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let result = ConnectResult {
|
||||
addr: camera_address,
|
||||
client_id,
|
||||
camera_id,
|
||||
sid: 0,
|
||||
};
|
||||
self.keep_alive_device(tid, &result).await;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// This function will contact the p2p relay servers
|
||||
///
|
||||
/// It will ask each of the servers for details on a specific UID
|
||||
@@ -529,6 +577,8 @@ impl Discoverer {
|
||||
|
||||
self.send_and_forget(msg, register_result.reg).await?;
|
||||
|
||||
self.keep_alive_device(local_tid, &result).await;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -641,6 +691,8 @@ impl Discoverer {
|
||||
|
||||
self.send_and_forget(msg, register_result.reg).await?;
|
||||
|
||||
self.keep_alive_device(local_tid, &result).await;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -704,6 +756,8 @@ impl Discoverer {
|
||||
|
||||
self.send_and_forget(msg, register_result.reg).await?;
|
||||
|
||||
self.keep_alive_device(tid, &result).await;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -751,16 +805,91 @@ impl Discoverer {
|
||||
_ => None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
||||
let result = ConnectResult {
|
||||
addr: final_addr,
|
||||
client_id: register_result.client_id,
|
||||
sid: register_result.sid,
|
||||
camera_id: local_did,
|
||||
};
|
||||
|
||||
// Confirm relay to register
|
||||
let msg = UdpDiscovery {
|
||||
tid: 0,
|
||||
payload: UdpXml {
|
||||
c2r_cfm: Some(C2rCfm {
|
||||
sid: result.sid,
|
||||
cid: result.client_id,
|
||||
did: result.camera_id,
|
||||
conn: "relay".to_string(),
|
||||
rsp: 0,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
|
||||
self.send_and_forget(msg, register_result.reg).await?;
|
||||
|
||||
self.keep_alive_device(tid, &result).await;
|
||||
// self.keep_alive_relay(tid, &result).await;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
async fn keep_alive_device(&self, tid: u32, connect_result: &ConnectResult) {
|
||||
let client_id = connect_result.client_id;
|
||||
let camera_id = connect_result.camera_id;
|
||||
let addr = connect_result.addr;
|
||||
let mut sender = ArcFramedSocket::new(self.socket.clone(), BcUdpCodex::new());
|
||||
let mut interval = interval(Duration::from_secs(1));
|
||||
self.handle.write().await.spawn(async move {
|
||||
loop {
|
||||
interval.tick().await;
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2d_hb: Some(C2dHb {
|
||||
cid: client_id,
|
||||
did: camera_id,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
if sender.send((msg, addr)).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // Haven't seen this in the wild yet it is just speculation
|
||||
async fn keep_alive_relay(&self, tid: u32, connect_result: &ConnectResult) {
|
||||
let client_id = connect_result.client_id;
|
||||
let camera_id = connect_result.camera_id;
|
||||
let sid = connect_result.sid;
|
||||
let addr = connect_result.addr;
|
||||
let mut sender = ArcFramedSocket::new(self.socket.clone(), BcUdpCodex::new());
|
||||
let mut interval = interval(Duration::from_secs(1));
|
||||
self.handle.write().await.spawn(async move {
|
||||
loop {
|
||||
interval.tick().await;
|
||||
let msg = BcUdp::Discovery(UdpDiscovery {
|
||||
tid,
|
||||
payload: UdpXml {
|
||||
c2r_hb: Some(C2rHb {
|
||||
sid,
|
||||
cid: client_id,
|
||||
did: camera_id,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
});
|
||||
if sender.send((msg, addr)).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Discovery {
|
||||
@@ -809,49 +938,46 @@ impl Discovery {
|
||||
let discoverer = Discoverer::new().await?;
|
||||
|
||||
let client_id = generate_cid();
|
||||
|
||||
let local_addr = discoverer.local_addr();
|
||||
let port = local_addr.port();
|
||||
|
||||
let msg = UdpDiscovery {
|
||||
tid: 0,
|
||||
payload: UdpXml {
|
||||
c2d_c: Some(C2dC {
|
||||
uid: uid.to_string(),
|
||||
cli: ClientList { port: port as u32 },
|
||||
cid: client_id,
|
||||
mtu: MTU,
|
||||
debug: false,
|
||||
os: "MAC".to_string(),
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
|
||||
let mut dests = get_broadcasts(&[2015, 2018])?;
|
||||
if let Some(mut optional_addrs) = optional_addrs.take() {
|
||||
trace!("Also sending to {:?}", optional_addrs);
|
||||
dests.append(&mut optional_addrs);
|
||||
}
|
||||
let (camera_address, camera_id) = discoverer
|
||||
.retry_send_multi(msg, &dests, |bc, addr| match bc {
|
||||
UdpDiscovery {
|
||||
tid: _,
|
||||
payload:
|
||||
UdpXml {
|
||||
d2c_c_r: Some(D2cCr { did, cid, .. }),
|
||||
..
|
||||
},
|
||||
} if cid == client_id => Some((addr, did)),
|
||||
_ => None,
|
||||
let discoverer_ref = &discoverer;
|
||||
let mut futures = FuturesUnordered::new();
|
||||
for addr in dests.iter().copied() {
|
||||
futures.push(async move {
|
||||
discoverer_ref
|
||||
.client_initiated_direct(uid, client_id, addr)
|
||||
.await
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
|
||||
let connect_result;
|
||||
loop {
|
||||
match futures.next().await {
|
||||
Some(Ok(good_result)) => {
|
||||
connect_result = good_result;
|
||||
break;
|
||||
}
|
||||
Some(Err(_)) => {
|
||||
continue;
|
||||
}
|
||||
None => {
|
||||
return Err(Error::DiscoveryTimeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
drop(futures);
|
||||
// drop(discoverer_ref);
|
||||
|
||||
let (socket, keep_alive_tasks) = discoverer.into_socket().await;
|
||||
Ok(DiscoveryResult {
|
||||
socket: discoverer.into_socket().await,
|
||||
addr: camera_address,
|
||||
camera_id,
|
||||
client_id,
|
||||
socket,
|
||||
keep_alive_tasks,
|
||||
addr: connect_result.addr,
|
||||
camera_id: connect_result.camera_id,
|
||||
client_id: connect_result.client_id,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -882,8 +1008,10 @@ impl Discovery {
|
||||
v = discoverer.device_initiated_dev(®_result) => {v},
|
||||
}?;
|
||||
|
||||
let (socket, keep_alive_tasks) = discoverer.into_socket().await;
|
||||
Ok(DiscoveryResult {
|
||||
socket: discoverer.into_socket().await,
|
||||
socket,
|
||||
keep_alive_tasks,
|
||||
addr: connect_result.addr,
|
||||
client_id,
|
||||
camera_id: connect_result.camera_id,
|
||||
@@ -913,8 +1041,10 @@ impl Discovery {
|
||||
let connect_result = discoverer.device_initiated_map(®_result).await?;
|
||||
trace!("connect_result: {:?}", connect_result);
|
||||
|
||||
let (socket, keep_alive_tasks) = discoverer.into_socket().await;
|
||||
Ok(DiscoveryResult {
|
||||
socket: discoverer.into_socket().await,
|
||||
socket,
|
||||
keep_alive_tasks,
|
||||
addr: connect_result.addr,
|
||||
client_id,
|
||||
camera_id: connect_result.camera_id,
|
||||
@@ -941,8 +1071,10 @@ impl Discovery {
|
||||
let connect_result = discoverer.client_initiated_relay(®_result).await?;
|
||||
trace!("connect_result: {:?}", connect_result);
|
||||
|
||||
let (socket, keep_alive_tasks) = discoverer.into_socket().await;
|
||||
Ok(DiscoveryResult {
|
||||
socket: discoverer.into_socket().await,
|
||||
socket,
|
||||
keep_alive_tasks,
|
||||
addr: connect_result.addr,
|
||||
client_id,
|
||||
camera_id: connect_result.camera_id,
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
//! in the bc packets.
|
||||
//!
|
||||
use std::net::SocketAddr;
|
||||
use tokio::net::UdpSocket;
|
||||
use std::sync::Arc;
|
||||
use tokio::{net::UdpSocket, task::JoinSet};
|
||||
|
||||
mod bcconn;
|
||||
mod bcsub;
|
||||
@@ -19,13 +20,19 @@ pub(crate) use self::{
|
||||
};
|
||||
|
||||
pub(crate) struct DiscoveryResult {
|
||||
socket: UdpSocket,
|
||||
socket: Arc<UdpSocket>,
|
||||
addr: SocketAddr,
|
||||
keep_alive_tasks: JoinSet<()>,
|
||||
client_id: i32,
|
||||
camera_id: i32,
|
||||
}
|
||||
|
||||
impl DiscoveryResult {
|
||||
/// Stop all keep alive and any remaining tasks that the discover is managing
|
||||
pub(crate) async fn shutdown(&mut self) {
|
||||
self.keep_alive_tasks.shutdown().await;
|
||||
}
|
||||
/// Get the address discovered
|
||||
pub(crate) fn get_addr(&self) -> &SocketAddr {
|
||||
&self.addr
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ use std::collections::VecDeque;
|
||||
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
|
||||
use std::net::SocketAddr;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::{Context, Poll};
|
||||
use tokio::{
|
||||
net::UdpSocket,
|
||||
@@ -43,15 +44,18 @@ impl UdpSource {
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
) -> Result<Self> {
|
||||
let stream = connect().await?;
|
||||
let stream = Arc::new(connect().await?);
|
||||
|
||||
Self::new_from_socket(stream, addr, client_id, camera_id, username, password).await
|
||||
}
|
||||
pub(crate) async fn new_from_discovery<T: Into<String>, U: Into<String>>(
|
||||
discovery: DiscoveryResult,
|
||||
mut discovery: DiscoveryResult,
|
||||
username: T,
|
||||
password: Option<U>,
|
||||
) -> Result<Self> {
|
||||
// Ensure that the discovery keep alive are all stopped here
|
||||
// We now handle all coms in UdpSource
|
||||
discovery.shutdown().await;
|
||||
Self::new_from_socket(
|
||||
discovery.socket,
|
||||
discovery.addr,
|
||||
@@ -64,7 +68,7 @@ impl UdpSource {
|
||||
}
|
||||
|
||||
pub(crate) async fn new_from_socket<T: Into<String>, U: Into<String>>(
|
||||
stream: UdpSocket,
|
||||
stream: Arc<UdpSocket>,
|
||||
addr: SocketAddr,
|
||||
client_id: i32,
|
||||
camera_id: i32,
|
||||
@@ -124,14 +128,14 @@ impl Sink<Bc> for UdpSource {
|
||||
}
|
||||
|
||||
pub(crate) struct BcUdpSource {
|
||||
inner: UdpFramed<BcUdpCodex, UdpSocket>,
|
||||
inner: UdpFramed<BcUdpCodex, Arc<UdpSocket>>,
|
||||
addr: SocketAddr,
|
||||
}
|
||||
|
||||
impl BcUdpSource {
|
||||
#[allow(unused)]
|
||||
pub(crate) async fn new(addr: SocketAddr) -> Result<Self> {
|
||||
let stream = connect().await?;
|
||||
let stream = Arc::new(connect().await?);
|
||||
|
||||
Self::new_from_socket(stream, addr).await
|
||||
}
|
||||
@@ -141,7 +145,7 @@ impl BcUdpSource {
|
||||
Self::new_from_socket(discovery.socket, discovery.addr).await
|
||||
}
|
||||
|
||||
pub(crate) async fn new_from_socket(stream: UdpSocket, addr: SocketAddr) -> Result<Self> {
|
||||
pub(crate) async fn new_from_socket(stream: Arc<UdpSocket>, addr: SocketAddr) -> Result<Self> {
|
||||
Ok(Self {
|
||||
inner: UdpFramed::new(stream, BcUdpCodex::new()),
|
||||
addr,
|
||||
@@ -509,7 +513,7 @@ impl futures::AsyncWrite for UdpPayloadSource {
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to create a TcpStream with a connect timeout
|
||||
/// Helper to create a UdpStream
|
||||
async fn connect() -> Result<UdpSocket> {
|
||||
let mut ports: Vec<u16> = (53500..54000).collect();
|
||||
let mut rng = thread_rng();
|
||||
|
||||
@@ -57,6 +57,12 @@ pub struct UdpXml {
|
||||
/// C2D_A xml client to device accept
|
||||
#[yaserde(rename = "C2D_A")]
|
||||
pub c2d_a: Option<C2dA>,
|
||||
/// C2D_HB xml client to device heartbeat. This is the keep alive
|
||||
#[yaserde(rename = "C2D_HB")]
|
||||
pub c2d_hb: Option<C2dHb>,
|
||||
/// C2D_HB xml client to device heartbeat. This is the keep alive
|
||||
#[yaserde(rename = "C2R_HB")]
|
||||
pub c2r_hb: Option<C2rHb>,
|
||||
}
|
||||
|
||||
impl UdpXml {
|
||||
@@ -352,3 +358,29 @@ pub struct C2dA {
|
||||
/// Maximum size in bytes of a transmission
|
||||
pub mtu: u32,
|
||||
}
|
||||
|
||||
/// C2D_HB xml
|
||||
///
|
||||
/// Client to device heart beat.
|
||||
/// Seems to act as a keep alive
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
|
||||
pub struct C2dHb {
|
||||
/// The client connection ID
|
||||
pub cid: i32,
|
||||
/// The camera connection ID
|
||||
pub did: i32,
|
||||
}
|
||||
|
||||
/// C2R_HB xml
|
||||
///
|
||||
/// Client to device heart beat.
|
||||
/// Seems to act as a keep alive
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize, Clone)]
|
||||
pub struct C2rHb {
|
||||
/// The connection ID
|
||||
pub sid: u32,
|
||||
/// The client connection ID
|
||||
pub cid: i32,
|
||||
/// The camera connection ID
|
||||
pub did: i32,
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#![warn(missing_docs)]
|
||||
#![warn(clippy::todo)]
|
||||
//!
|
||||
//! # Neolink
|
||||
//!
|
||||
|
||||
+8
-643
@@ -1,649 +1,14 @@
|
||||
//! This module provides an "RtspServer" abstraction that allows consumers of its API to feed it
|
||||
//! data using an ordinary std::io::Write interface.
|
||||
mod maybe_app_src;
|
||||
mod maybe_inputselect;
|
||||
|
||||
pub(crate) use self::maybe_app_src::MaybeAppSrc;
|
||||
pub(crate) use self::maybe_inputselect::MaybeInputSelect;
|
||||
mod factory;
|
||||
mod sender;
|
||||
mod server;
|
||||
mod shared;
|
||||
|
||||
// use super::adpcm::adpcm_to_pcm;
|
||||
// use super::errors::Error;
|
||||
use gstreamer::prelude::Cast;
|
||||
use gstreamer::{Bin, ElementFactory, Structure};
|
||||
use gstreamer_app::AppSrc;
|
||||
//use gstreamer_rtsp::RTSPLowerTrans;
|
||||
use anyhow::{anyhow, Context};
|
||||
use gstreamer_rtsp::RTSPAuthMethod;
|
||||
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
|
||||
use gstreamer_rtsp_server::glib;
|
||||
use gstreamer_rtsp_server::prelude::*;
|
||||
use gstreamer_rtsp_server::{
|
||||
RTSPAuth, RTSPMediaFactory, RTSPServer as GstRTSPServer, RTSPToken,
|
||||
RTSP_PERM_MEDIA_FACTORY_ACCESS, RTSP_PERM_MEDIA_FACTORY_CONSTRUCT,
|
||||
RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::*;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::sync::Arc;
|
||||
use factory::*;
|
||||
|
||||
pub(crate) use self::server::NeoRtspServer;
|
||||
pub(crate) use gstreamer_rtsp_server::gio::TlsAuthenticationMode;
|
||||
|
||||
type Result<T> = std::result::Result<T, ()>;
|
||||
type AnyResult<T> = std::result::Result<T, anyhow::Error>;
|
||||
|
||||
pub(crate) struct RtspServer {
|
||||
server: GstRTSPServer,
|
||||
main_loop: Arc<glib::MainLoop>,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
||||
pub(crate) enum InputMode {
|
||||
Live,
|
||||
Paused,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
||||
pub(crate) enum PausedSources {
|
||||
TestSrc,
|
||||
Still,
|
||||
Black,
|
||||
None,
|
||||
}
|
||||
|
||||
pub(crate) struct GstOutputs {
|
||||
pub(crate) audsrc: MaybeAppSrc,
|
||||
pub(crate) vidsrc: MaybeAppSrc,
|
||||
vid_inputselect: MaybeInputSelect,
|
||||
aud_inputselect: MaybeInputSelect,
|
||||
video_format: Option<StreamFormat>,
|
||||
audio_format: Option<StreamFormat>,
|
||||
factory: RTSPMediaFactory,
|
||||
when_paused: PausedSources,
|
||||
last_iframe: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
// The stream from the camera will be using one of these formats
|
||||
//
|
||||
// This is used as part of `StreamOutput` to give hints about
|
||||
// the format of the stream
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
enum StreamFormat {
|
||||
// H264 (AVC) video format
|
||||
H264,
|
||||
// H265 (HEVC) video format
|
||||
H265,
|
||||
// AAC audio
|
||||
Aac,
|
||||
// ADPCM in DVI-4 format
|
||||
Adpcm(u16),
|
||||
}
|
||||
|
||||
impl GstOutputs {
|
||||
pub(crate) fn from_appsrcs(
|
||||
vidsrc: MaybeAppSrc,
|
||||
audsrc: MaybeAppSrc,
|
||||
vid_inputselect: MaybeInputSelect,
|
||||
aud_inputselect: MaybeInputSelect,
|
||||
) -> GstOutputs {
|
||||
let result = GstOutputs {
|
||||
vidsrc,
|
||||
audsrc,
|
||||
vid_inputselect,
|
||||
aud_inputselect,
|
||||
video_format: None,
|
||||
audio_format: None,
|
||||
when_paused: PausedSources::None,
|
||||
factory: RTSPMediaFactory::new(),
|
||||
last_iframe: Default::default(),
|
||||
};
|
||||
result.apply_format();
|
||||
result
|
||||
}
|
||||
|
||||
pub(crate) fn stream_recv(&mut self, media: BcMedia) -> AnyResult<()> {
|
||||
// Ensure stream is on cam mode
|
||||
match media {
|
||||
BcMedia::Iframe(payload) => {
|
||||
let video_type = match payload.video_type {
|
||||
VideoType::H264 => StreamFormat::H264,
|
||||
VideoType::H265 => StreamFormat::H265,
|
||||
};
|
||||
self.set_format(Some(video_type));
|
||||
self.vidsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write IFrame to vidsrc")?;
|
||||
self.last_iframe = Some(payload.data);
|
||||
}
|
||||
BcMedia::Pframe(payload) => {
|
||||
let video_type = match payload.video_type {
|
||||
VideoType::H264 => StreamFormat::H264,
|
||||
VideoType::H265 => StreamFormat::H265,
|
||||
};
|
||||
self.set_format(Some(video_type));
|
||||
self.vidsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write PFrame to vidsrc")?;
|
||||
}
|
||||
BcMedia::Aac(payload) => {
|
||||
self.set_format(Some(StreamFormat::Aac));
|
||||
self.audsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write AAC to audsrc")?;
|
||||
}
|
||||
BcMedia::Adpcm(payload) => {
|
||||
self.set_format(Some(StreamFormat::Adpcm(payload.data.len() as u16)));
|
||||
self.audsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write ADPCM to audsrc")?;
|
||||
}
|
||||
_ => {
|
||||
//Ignore other BcMedia like InfoV1 and InfoV2
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_input_source(&mut self, input_source: InputMode) -> AnyResult<()> {
|
||||
// PausedSources::None is exceptional in that it dosen't swap the stream
|
||||
// to an alterntive source. It just repeats the last buffer meaning not re-encoding
|
||||
// at the cost some clients not handeling the repeating buffers well
|
||||
if self.when_paused != PausedSources::None {
|
||||
match &input_source {
|
||||
InputMode::Live => {
|
||||
self.vid_inputselect.set_input(0)?;
|
||||
self.aud_inputselect.set_input(0)?;
|
||||
}
|
||||
InputMode::Paused => {
|
||||
self.vid_inputselect.set_input(1)?;
|
||||
self.aud_inputselect.set_input(1)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// For PausedSources::None we still want to use the silence as we don't want to
|
||||
// repeat the audio buffers
|
||||
match &input_source {
|
||||
InputMode::Live => {
|
||||
self.aud_inputselect.set_input(0)?;
|
||||
}
|
||||
InputMode::Paused => {
|
||||
self.aud_inputselect.set_input(1)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_paused_source(&mut self, paused_source: PausedSources) {
|
||||
self.when_paused = paused_source;
|
||||
self.apply_format();
|
||||
}
|
||||
|
||||
pub(crate) fn has_last_iframe(&self) -> bool {
|
||||
self.last_iframe.is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn write_last_iframe(&mut self) -> AnyResult<()> {
|
||||
self.vidsrc.write_all(
|
||||
self.last_iframe
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("No iframe data avaliable"))?
|
||||
.as_slice(),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_format(&mut self, format: Option<StreamFormat>) {
|
||||
match format {
|
||||
Some(StreamFormat::H264) | Some(StreamFormat::H265) => {
|
||||
if format != self.video_format {
|
||||
self.video_format = format;
|
||||
self.apply_format();
|
||||
}
|
||||
}
|
||||
Some(StreamFormat::Aac) | Some(StreamFormat::Adpcm(_)) => {
|
||||
if format != self.audio_format {
|
||||
self.audio_format = format;
|
||||
self.apply_format();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_format(&self) {
|
||||
// This is the final sink prior to rtsp
|
||||
// - In the case of PausedSources::None we just pass through without encoding
|
||||
// - In the case of all other pause sources we need to recencode in order
|
||||
// to get smooth streams when the stream is paused/resumed
|
||||
let launch_vid_select = match self.when_paused {
|
||||
PausedSources::None => match self.video_format {
|
||||
Some(StreamFormat::H264) => "! rtph264pay name=pay0",
|
||||
Some(StreamFormat::H265) => "! rtph265pay name=pay0",
|
||||
_ => "! fakesink",
|
||||
},
|
||||
_ => match self.video_format {
|
||||
Some(StreamFormat::H264) => "! x264enc ! rtph264pay name=pay0",
|
||||
Some(StreamFormat::H265) => "! x265enc ! rtph265pay name=pay0",
|
||||
_ => "! fakesink",
|
||||
},
|
||||
};
|
||||
|
||||
// This is the part that deals with input from camera
|
||||
// - In the case of PausedSources::None it just passes the stream
|
||||
// - In the case of other Paused sources we decode it so that we can manipulate
|
||||
// the stream smoothly and swap the source on demand
|
||||
let launch_app_src = match self.when_paused {
|
||||
PausedSources::None => {
|
||||
match self.video_format {
|
||||
Some(StreamFormat::H264) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse"
|
||||
}
|
||||
Some(StreamFormat::H265) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse"
|
||||
}
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
match self.video_format {
|
||||
Some(StreamFormat::H264) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse ! avdec_h264"
|
||||
}
|
||||
Some(StreamFormat::H265) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse ! avdec_h265"
|
||||
}
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// This controls the alternaive source used when in paused state
|
||||
// PausedSources::None is the exception in that it is never swapped to this state
|
||||
let launch_alt_source = match self.when_paused {
|
||||
PausedSources::TestSrc => "videotestsrc ! video/x-raw,width=896,height=512,framerate=25/1",
|
||||
PausedSources::Black => "videotestsrc pattern=black ! video/x-raw,width=896,height=512,framerate=25/1",
|
||||
PausedSources::Still => "vid_src_tee. ! imagefreeze allow-replace=true is-live=true ! video/x-raw,framerate=25/1",
|
||||
PausedSources::None => "",
|
||||
};
|
||||
|
||||
// This is the final pipeline for the audio prior to rtsp
|
||||
let launch_aud_select = match self.audio_format {
|
||||
Some(StreamFormat::Adpcm(_)) | Some(StreamFormat::Aac) => {
|
||||
"! audioconvert ! rtpL16pay name=pay1"
|
||||
}
|
||||
_ => "! fakesink",
|
||||
};
|
||||
|
||||
// This is the audio source from the camera. We decode to xraw always as some clients
|
||||
// like blue iris only support raw audio
|
||||
let launch_aud = match self.audio_format {
|
||||
Some(StreamFormat::Adpcm(block_size)) => format!("caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000 ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! adpcmdec", block_size), // DVI4 is converted to pcm in the appsrc
|
||||
Some(StreamFormat::Aac) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aacparse ! decodebin".to_string(),
|
||||
_ => "".to_string(),
|
||||
};
|
||||
|
||||
// The alternaive audio source is silence
|
||||
let launch_aud_alt = "audiotestsrc wave=silence";
|
||||
|
||||
let launch_str = &vec![
|
||||
"( ",
|
||||
// Video out pipe
|
||||
"(",
|
||||
"input-selector name=vid_inputselect",
|
||||
launch_vid_select,
|
||||
")",
|
||||
// Camera vid source
|
||||
"(",
|
||||
"appsrc name=vidsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks
|
||||
launch_app_src,
|
||||
// Pipe it though a tee so that the image freeze can grab it
|
||||
"! tee name=vid_src_tee",
|
||||
"(",
|
||||
"vid_src_tee. ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! vid_inputselect.sink_0",
|
||||
")",
|
||||
|
||||
")",
|
||||
// Alternaive vid source
|
||||
//
|
||||
// Used during paused mode
|
||||
"(",
|
||||
launch_alt_source,
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! vid_inputselect.sink_1",
|
||||
")",
|
||||
// Image freeze
|
||||
// Audio pipe
|
||||
// Audio out pipe
|
||||
"(",
|
||||
"input-selector name=aud_inputselect",
|
||||
launch_aud_select,
|
||||
")",
|
||||
// Camera aud source
|
||||
"(",
|
||||
"appsrc name=audsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME",
|
||||
&launch_aud,
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aud_inputselect.sink_0",
|
||||
")",
|
||||
// Camera aud alt source
|
||||
"(",
|
||||
launch_aud_alt,
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aud_inputselect.sink_1",
|
||||
")",
|
||||
")"
|
||||
]
|
||||
.join(" ");
|
||||
debug!("Gstreamer launch str: {:?}", launch_str);
|
||||
self.factory.set_launch(launch_str);
|
||||
}
|
||||
|
||||
pub(crate) fn is_connected(&self) -> bool {
|
||||
self.vidsrc.is_connected() || self.audsrc.is_connected()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RtspServer {
|
||||
fn default() -> RtspServer {
|
||||
Self::new().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl RtspServer {
|
||||
pub(crate) fn new() -> AnyResult<RtspServer> {
|
||||
gstreamer::init().context("Gstreamer failed to initialise")?;
|
||||
let needed_elements = &[
|
||||
(
|
||||
"appsrc",
|
||||
"app (gst-plugins-base)",
|
||||
"Cannot run without an appsrc",
|
||||
),
|
||||
(
|
||||
"audioconvert",
|
||||
"audioconvert (gst-plugins-base)",
|
||||
"Required for audio",
|
||||
),
|
||||
("adpcmdec", "adpcmdec", "Required for audio"),
|
||||
(
|
||||
"h264parse",
|
||||
"videoparsersbad (gst-plugins-bad)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"h265parse",
|
||||
"videoparsersbad (gst-plugins-bad)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"rtph264pay",
|
||||
"rtp (gst-plugins-good)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"rtph265pay",
|
||||
"rtp (gst-plugins-good)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"aacparse",
|
||||
"audioparsers (gst-plugins-good)",
|
||||
"Required for certain types of camera's audio",
|
||||
),
|
||||
("rtpL16pay", "rtp (gst-plugins-good)", "Required for audio"),
|
||||
];
|
||||
let sometimes_needed_elements = &[
|
||||
(
|
||||
"x264enc",
|
||||
"x264 (gst-plugins-ugly)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"x265enc",
|
||||
"x265 (gst-plugins-bad)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"avdec_h264",
|
||||
"libav (gst-libav)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"avdec_h265",
|
||||
"libav (gst-libav)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"videotestsrc",
|
||||
"videotestsrc (gst-plugins-base)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"imagefreeze",
|
||||
"imagefreeze (gst-plugins-good)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"audiotestsrc",
|
||||
"audiotestsrc (gst-plugins-base)",
|
||||
"Required for pausing",
|
||||
),
|
||||
(
|
||||
"decodebin",
|
||||
"playback (gst-plugins-good)",
|
||||
"Required for pausing",
|
||||
),
|
||||
];
|
||||
let mut fatal = false;
|
||||
for (element, plugin, msg) in needed_elements.iter() {
|
||||
if ElementFactory::find(element).is_none() {
|
||||
error!(
|
||||
"Missing required gstreamer plugin `{}` for `{}` element. {}",
|
||||
plugin, element, msg
|
||||
);
|
||||
fatal = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (element, plugin, msg) in sometimes_needed_elements.iter() {
|
||||
if ElementFactory::find(element).is_none() {
|
||||
warn!(
|
||||
"Missing the gstreamer plugin `{}` for `{}` element. {}",
|
||||
plugin, element, msg
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if fatal {
|
||||
return Err(anyhow!(
|
||||
"Required Gstreamer Elements are missing. Ensure gstreamer is installed correctly"
|
||||
));
|
||||
}
|
||||
Ok(RtspServer {
|
||||
server: GstRTSPServer::new(),
|
||||
main_loop: Arc::new(glib::MainLoop::new(None, false)),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn add_stream<T: AsRef<str>>(
|
||||
&self,
|
||||
paths: &[&str],
|
||||
permitted_users: &HashSet<T>,
|
||||
) -> Result<GstOutputs> {
|
||||
let mounts = self
|
||||
.server
|
||||
.mount_points()
|
||||
.expect("The server should have mountpoints");
|
||||
|
||||
// Create a MaybeAppSrc: Write which we will give the caller. When the backing AppSrc is
|
||||
// created by the factory, fish it out and give it to the waiting MaybeAppSrc via the
|
||||
// channel it provided. This callback may be called more than once by Gstreamer if it is
|
||||
// unhappy with the pipeline, so keep updating the MaybeAppSrc.
|
||||
let (maybe_app_src, tx) = MaybeAppSrc::new_with_tx();
|
||||
let (maybe_app_src_aud, tx_aud) = MaybeAppSrc::new_with_tx();
|
||||
|
||||
let (maybe_vid_inputselect, tx_vid_inputselect) = MaybeInputSelect::new_with_tx();
|
||||
let (maybe_aud_inputselect, tx_aud_inputselect) = MaybeInputSelect::new_with_tx();
|
||||
|
||||
let outputs = GstOutputs::from_appsrcs(
|
||||
maybe_app_src,
|
||||
maybe_app_src_aud,
|
||||
maybe_vid_inputselect,
|
||||
maybe_aud_inputselect,
|
||||
);
|
||||
|
||||
let factory = &outputs.factory;
|
||||
|
||||
debug!(
|
||||
"Permitting {} to access {}",
|
||||
// This is hashmap or (iter) equivalent of join, it requres itertools
|
||||
itertools::Itertools::intersperse(permitted_users.iter().map(|i| i.as_ref()), ", ")
|
||||
.collect::<String>(),
|
||||
paths.join(", ")
|
||||
);
|
||||
self.add_permitted_roles(factory, permitted_users);
|
||||
|
||||
factory.set_shared(true);
|
||||
|
||||
factory.connect_media_configure(move |_factory, media| {
|
||||
debug!("RTSP: media was configured");
|
||||
let bin = media
|
||||
.element()
|
||||
// .expect("Media should have an element")
|
||||
.dynamic_cast::<Bin>()
|
||||
.expect("Media source's element should be a bin");
|
||||
let app_src = bin
|
||||
.by_name_recurse_up("vidsrc")
|
||||
.expect("vidsrc must be present in created bin")
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.expect("Source element is expected to be an appsrc!");
|
||||
let _ = tx.send(app_src); // Receiver may be dropped, don't panic if so
|
||||
|
||||
let app_src_aud = bin
|
||||
.by_name_recurse_up("audsrc")
|
||||
.expect("audsrc must be present in created bin")
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.expect("Source element is expected to be an appsrc!");
|
||||
let _ = tx_aud.send(app_src_aud); // Receiver may be dropped, don't panic if so
|
||||
|
||||
let maybe_vid_inputselect = bin
|
||||
.by_name_recurse_up("vid_inputselect")
|
||||
.expect("vid_inputselect must be present in created bin");
|
||||
let _ = tx_vid_inputselect.send(maybe_vid_inputselect); // Receiver may be dropped, don't panic if so
|
||||
|
||||
let maybe_aud_inputselect = bin
|
||||
.by_name_recurse_up("aud_inputselect")
|
||||
.expect("aud_inputselect must be present in created bin");
|
||||
let _ = tx_aud_inputselect.send(maybe_aud_inputselect); // Receiver may be dropped, don't panic if so
|
||||
});
|
||||
|
||||
for path in paths {
|
||||
mounts.add_factory(path, factory);
|
||||
}
|
||||
|
||||
Ok(outputs)
|
||||
}
|
||||
|
||||
pub(crate) fn remove_stream(&self, paths: &[&str]) -> Result<()> {
|
||||
let mounts = self
|
||||
.server
|
||||
.mount_points()
|
||||
.expect("The server should have mountpoints");
|
||||
for path in paths {
|
||||
mounts.remove_factory(path);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn add_permitted_roles<T: AsRef<str>>(
|
||||
&self,
|
||||
factory: &RTSPMediaFactory,
|
||||
permitted_roles: &HashSet<T>,
|
||||
) {
|
||||
for permitted_role in permitted_roles {
|
||||
factory.add_role_from_structure(&Structure::new(
|
||||
permitted_role.as_ref(),
|
||||
&[
|
||||
(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true),
|
||||
(*RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, &true),
|
||||
],
|
||||
));
|
||||
}
|
||||
// During auth, first it binds anonymously. At this point it checks
|
||||
// RTSP_PERM_MEDIA_FACTORY_ACCESS to see if anyone can connect
|
||||
// This is done before the auth token is loaded, possibliy an upstream bug there
|
||||
// After checking RTSP_PERM_MEDIA_FACTORY_ACCESS anonymously
|
||||
// It loads the auth token of the user and checks that users
|
||||
// RTSP_PERM_MEDIA_FACTORY_CONSTRUCT allowing them to play
|
||||
// As a result of this we must ensure that if anonymous is not granted RTSP_PERM_MEDIA_FACTORY_ACCESS
|
||||
// As a part of permitted users then we must allow it to access
|
||||
// at least RTSP_PERM_MEDIA_FACTORY_ACCESS but not RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// Watching Actually happens during RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// So this should be OK to do.
|
||||
// FYI: If no RTSP_PERM_MEDIA_FACTORY_ACCESS then server returns 404 not found
|
||||
// If yes RTSP_PERM_MEDIA_FACTORY_ACCESS but no RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// server returns 401 not authourised
|
||||
if !permitted_roles
|
||||
.iter()
|
||||
.map(|i| i.as_ref())
|
||||
.collect::<HashSet<&str>>()
|
||||
.contains(&"anonymous")
|
||||
{
|
||||
factory.add_role_from_structure(&Structure::new(
|
||||
"anonymous",
|
||||
&[(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true)],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_credentials(&self, credentials: &[(&str, &str)]) -> Result<()> {
|
||||
let auth = self.server.auth().unwrap_or_else(RTSPAuth::new);
|
||||
auth.set_supported_methods(RTSPAuthMethod::Basic);
|
||||
|
||||
let mut un_authtoken = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous")]);
|
||||
auth.set_default_token(Some(&mut un_authtoken));
|
||||
|
||||
for credential in credentials {
|
||||
let (user, pass) = credential;
|
||||
trace!("Setting credentials for user {}", user);
|
||||
let token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, user)]);
|
||||
let basic = RTSPAuth::make_basic(user, pass);
|
||||
auth.add_basic(basic.as_str(), &token);
|
||||
}
|
||||
|
||||
self.server.set_auth(Some(&auth));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_tls(
|
||||
&self,
|
||||
cert_file: &str,
|
||||
client_auth: TlsAuthenticationMode,
|
||||
) -> Result<()> {
|
||||
debug!("Setting up TLS using {}", cert_file);
|
||||
let auth = self.server.auth().unwrap_or_else(RTSPAuth::new);
|
||||
|
||||
// We seperate reading the file and changing to a PEM so that we get different error messages.
|
||||
let cert_contents = fs::read_to_string(cert_file).expect("TLS file not found");
|
||||
let cert = TlsCertificate::from_pem(&cert_contents).expect("Not a valid TLS certificate");
|
||||
auth.set_tls_certificate(Some(&cert));
|
||||
auth.set_tls_authentication_mode(client_auth);
|
||||
|
||||
self.server.set_auth(Some(&auth));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn run(&self, bind_addr: &str, bind_port: u16) {
|
||||
self.server.set_address(bind_addr);
|
||||
self.server.set_service(&format!("{}", bind_port));
|
||||
// Attach server to default Glib context
|
||||
let _ = self.server.attach(None);
|
||||
let main_loop = self.main_loop.clone();
|
||||
// Run the Glib main loop.
|
||||
let _ = tokio::task::spawn_blocking(move || main_loop.run()).await;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn quit(&self) {
|
||||
self.main_loop.quit();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
//! Attempts to subclass GstMediaFactory
|
||||
//!
|
||||
//! We are now messing with gstreamer glib objects
|
||||
//! expect issues
|
||||
|
||||
use super::{sender::*, shared::*, AnyResult};
|
||||
use anyhow::{anyhow, Context};
|
||||
use gstreamer::glib::object_subclass;
|
||||
use gstreamer::glib::subclass::types::ObjectSubclass;
|
||||
use gstreamer::{
|
||||
glib::{self, Object},
|
||||
Structure,
|
||||
};
|
||||
use gstreamer::{Bin, Caps, ClockTime, Element, ElementFactory};
|
||||
use gstreamer_app::{AppSrc, AppStreamType};
|
||||
use gstreamer_rtsp::RTSPUrl;
|
||||
use gstreamer_rtsp_server::prelude::*;
|
||||
use gstreamer_rtsp_server::subclass::prelude::*;
|
||||
use gstreamer_rtsp_server::RTSPMediaFactory;
|
||||
use gstreamer_rtsp_server::{RTSPSuspendMode, RTSPTransportMode};
|
||||
use gstreamer_rtsp_server::{RTSP_PERM_MEDIA_FACTORY_ACCESS, RTSP_PERM_MEDIA_FACTORY_CONSTRUCT};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::*;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
sync::{atomic::Ordering, Arc},
|
||||
};
|
||||
use tokio::{
|
||||
sync::mpsc::{channel, Sender},
|
||||
task::JoinSet,
|
||||
};
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
|
||||
glib::wrapper! {
|
||||
/// The wrapped RTSPMediaFactory
|
||||
pub(crate) struct NeoMediaFactory(ObjectSubclass<NeoMediaFactoryImpl>) @extends RTSPMediaFactory;
|
||||
}
|
||||
|
||||
impl Default for NeoMediaFactory {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl NeoMediaFactory {
|
||||
pub(crate) fn new() -> Self {
|
||||
let factory = Object::new::<NeoMediaFactory>(&[]);
|
||||
factory.set_shared(false);
|
||||
factory.set_do_retransmission(false);
|
||||
factory.set_launch("videotestsrc pattern=\"snow\" ! video/x-raw,width=896,height=512,framerate=25/1 ! textoverlay name=\"inittextoverlay\" text=\"Stream not Ready\" valignment=top halignment=left font-desc=\"Sans, 32\" ! jpegenc ! rtpjpegpay name=pay0");
|
||||
factory.set_suspend_mode(RTSPSuspendMode::None);
|
||||
factory.set_transport_mode(RTSPTransportMode::PLAY);
|
||||
factory
|
||||
}
|
||||
|
||||
pub(crate) fn get_sender(&self) -> Sender<BcMedia> {
|
||||
self.imp().sender.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn add_permitted_roles<T: AsRef<str>>(&self, permitted_roles: &HashSet<T>) {
|
||||
for permitted_role in permitted_roles {
|
||||
self.add_role_from_structure(&Structure::new(
|
||||
permitted_role.as_ref(),
|
||||
&[
|
||||
(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true),
|
||||
(*RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, &true),
|
||||
],
|
||||
));
|
||||
}
|
||||
// During auth, first it binds anonymously. At this point it checks
|
||||
// RTSP_PERM_MEDIA_FACTORY_ACCESS to see if anyone can connect
|
||||
// This is done before the auth token is loaded, possibliy an upstream bug there
|
||||
// After checking RTSP_PERM_MEDIA_FACTORY_ACCESS anonymously
|
||||
// It loads the auth token of the user and checks that users
|
||||
// RTSP_PERM_MEDIA_FACTORY_CONSTRUCT allowing them to play
|
||||
// As a result of this we must ensure that if anonymous is not granted RTSP_PERM_MEDIA_FACTORY_ACCESS
|
||||
// As a part of permitted users then we must allow it to access
|
||||
// at least RTSP_PERM_MEDIA_FACTORY_ACCESS but not RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// Watching Actually happens during RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// So this should be OK to do.
|
||||
// FYI: If no RTSP_PERM_MEDIA_FACTORY_ACCESS then server returns 404 not found
|
||||
// If yes RTSP_PERM_MEDIA_FACTORY_ACCESS but no RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// server returns 401 not authourised
|
||||
if !permitted_roles
|
||||
.iter()
|
||||
.map(|i| i.as_ref())
|
||||
.collect::<HashSet<&str>>()
|
||||
.contains(&"anonymous")
|
||||
{
|
||||
self.add_role_from_structure(&Structure::new(
|
||||
"anonymous",
|
||||
&[(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true)],
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for NeoMediaFactory {}
|
||||
unsafe impl Sync for NeoMediaFactory {}
|
||||
|
||||
pub(crate) struct NeoMediaFactoryImpl {
|
||||
sender: Sender<BcMedia>,
|
||||
appsender: Sender<AppSrc>,
|
||||
shared: Arc<NeoMediaShared>,
|
||||
#[allow(dead_code)] // Not dead just need a handle to keep it alive and drop with this obj
|
||||
threads: JoinSet<AnyResult<()>>,
|
||||
}
|
||||
|
||||
impl Default for NeoMediaFactoryImpl {
|
||||
fn default() -> Self {
|
||||
let (datasender, datarx) = channel(3);
|
||||
let (appsender, rx_appsender) = channel(3);
|
||||
let shared: Arc<NeoMediaShared> = Default::default();
|
||||
|
||||
// Prepare thread that sends data into the appsrcs
|
||||
let mut threads: JoinSet<AnyResult<()>> = Default::default();
|
||||
let mut sender = NeoMediaSender {
|
||||
data_source: ReceiverStream::new(datarx),
|
||||
app_source: ReceiverStream::new(rx_appsender),
|
||||
shared: shared.clone(),
|
||||
appsrcs: Default::default(),
|
||||
waiting_for_iframe: true,
|
||||
};
|
||||
threads.spawn(async move { sender.run().await });
|
||||
|
||||
Self {
|
||||
sender: datasender,
|
||||
appsender,
|
||||
shared,
|
||||
threads,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectImpl for NeoMediaFactoryImpl {}
|
||||
impl RTSPMediaFactoryImpl for NeoMediaFactoryImpl {
|
||||
fn create_element(&self, url: &RTSPUrl) -> Option<Element> {
|
||||
self.parent_create_element(url)
|
||||
.map(|orig| self.build_pipeline(orig).expect("Could not build pipeline"))
|
||||
}
|
||||
}
|
||||
|
||||
#[object_subclass]
|
||||
impl ObjectSubclass for NeoMediaFactoryImpl {
|
||||
const NAME: &'static str = "NeoMediaFactory";
|
||||
type Type = super::NeoMediaFactory;
|
||||
type ParentType = RTSPMediaFactory;
|
||||
}
|
||||
|
||||
// Convenice funcion to make an element or provide a message
|
||||
// about what plugin is missing
|
||||
fn make_element(kind: &str, name: &str) -> AnyResult<Element> {
|
||||
ElementFactory::make_with_name(kind, Some(name)).with_context(|| {
|
||||
let plugin = match kind {
|
||||
"appsrc" => "app (gst-plugins-base)",
|
||||
"audioconvert" => "audioconvert (gst-plugins-base)",
|
||||
"adpcmdec" => "Required for audio",
|
||||
"h264parse" => "videoparsersbad (gst-plugins-bad)",
|
||||
"h265parse" => "videoparsersbad (gst-plugins-bad)",
|
||||
"rtph264pay" => "rtp (gst-plugins-good)",
|
||||
"rtph265pay" => "rtp (gst-plugins-good)",
|
||||
"aacparse" => "audioparsers (gst-plugins-good)",
|
||||
"rtpL16pay" => "rtp (gst-plugins-good)",
|
||||
"x264enc" => "x264 (gst-plugins-ugly)",
|
||||
"x265enc" => "x265 (gst-plugins-bad)",
|
||||
"avdec_h264" => "libav (gst-libav)",
|
||||
"avdec_h265" => "libav (gst-libav)",
|
||||
"videotestsrc" => "videotestsrc (gst-plugins-base)",
|
||||
"imagefreeze" => "imagefreeze (gst-plugins-good)",
|
||||
"audiotestsrc" => "audiotestsrc (gst-plugins-base)",
|
||||
"decodebin" => "playback (gst-plugins-good)",
|
||||
_ => "Unknown",
|
||||
};
|
||||
format!(
|
||||
"Missing required gstreamer plugin `{}` for `{}` element",
|
||||
plugin, kind
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
impl NeoMediaFactoryImpl {
|
||||
fn build_pipeline(&self, media: Element) -> AnyResult<Element> {
|
||||
let bin = media
|
||||
.dynamic_cast::<Bin>()
|
||||
.map_err(|_| anyhow!("Media source's element should be a bin"))?;
|
||||
// Clear the autogenerated ones
|
||||
for element in bin.iterate_elements().into_iter().flatten() {
|
||||
bin.remove(&element)?;
|
||||
}
|
||||
// Now contruct the actual ones
|
||||
match self.shared.vid_format.load(Ordering::Relaxed) {
|
||||
VidFormats::H265 => {
|
||||
debug!("Building H265 Pipeline");
|
||||
let source = make_element("appsrc", "vidsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
source.set_base_time(ClockTime::from_mseconds(
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(true);
|
||||
source.set_property("emit-signals", &false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h265").field("parsed", false).build(),
|
||||
// ));
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_element("queue", "source_queue")?;
|
||||
let parser = make_element("h265parse", "parser")?;
|
||||
let payload = make_element("rtph265pay", "pay0")?;
|
||||
bin.add_many(&[&source, &queue, &parser, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &parser, &payload])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
self.appsender.blocking_send(source)?;
|
||||
}
|
||||
VidFormats::H264 => {
|
||||
debug!("Building H264 Pipeline");
|
||||
let source = make_element("appsrc", "vidsrc")?
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot cast to appsrc."))?;
|
||||
source.set_base_time(ClockTime::from_mseconds(
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
));
|
||||
source.set_is_live(true);
|
||||
source.set_block(true);
|
||||
source.set_property("emit-signals", &false);
|
||||
source.set_max_bytes(52428800);
|
||||
source.set_do_timestamp(false);
|
||||
source.set_stream_type(AppStreamType::Stream);
|
||||
// source.set_caps(Some(
|
||||
// &Caps::builder("video/x-h264").field("parsed", false).build(),
|
||||
// ));
|
||||
let source = source
|
||||
.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))?;
|
||||
let queue = make_element("queue", "source_queue")?;
|
||||
let parser = make_element("h264parse", "parser")?;
|
||||
let payload = make_element("rtph264pay", "pay0")?;
|
||||
bin.add_many(&[&source, &queue, &parser, &payload])?;
|
||||
Element::link_many(&[&source, &queue, &parser, &payload])?;
|
||||
|
||||
let source = source
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.map_err(|_| anyhow!("Cannot convert appsrc"))?;
|
||||
self.appsender.blocking_send(source)?;
|
||||
}
|
||||
VidFormats::Unknown => {
|
||||
debug!("Building Unknown Pipeline");
|
||||
let source = make_element("videotestsrc", "vidsrc")?;
|
||||
source.set_property_from_str("pattern", "snow");
|
||||
let queue = make_element("queue", "queue0")?;
|
||||
let overlay = make_element("textoverlay", "overlay")?;
|
||||
overlay.set_property("text", "Stream not Ready");
|
||||
overlay.set_property_from_str("valignment", "top");
|
||||
overlay.set_property_from_str("halignment", "left");
|
||||
overlay.set_property("font-desc", "Sans, 32");
|
||||
let encoder = make_element("jpegenc", "encoder")?;
|
||||
let payload = make_element("rtpjpegpay", "pay0")?;
|
||||
|
||||
bin.add_many(&[&source, &queue, &overlay, &encoder, &payload])?;
|
||||
source.link_filtered(
|
||||
&queue,
|
||||
&Caps::builder("video/x-raw")
|
||||
.field("format", "YUY2")
|
||||
.field("width", 896i32)
|
||||
.field("height", 512i32)
|
||||
.field("framerate", gstreamer::Fraction::new(25, 1))
|
||||
.build(),
|
||||
)?;
|
||||
// source.link(&queue)?;
|
||||
queue.link(&overlay)?;
|
||||
overlay.link(&encoder)?;
|
||||
encoder.link(&payload)?;
|
||||
}
|
||||
}
|
||||
|
||||
bin.dynamic_cast::<Element>()
|
||||
.map_err(|_| anyhow!("Cannot cast back"))
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
use super::*;
|
||||
use crossbeam_channel::{bounded, Receiver, Sender};
|
||||
|
||||
/// A Write implementation around AppSrc that also allows delaying the creation of the AppSrc
|
||||
/// until later, discarding written data until the AppSrc is provided.
|
||||
pub(crate) struct MaybeAppSrc {
|
||||
rx: Receiver<AppSrc>,
|
||||
app_src: Option<AppSrc>,
|
||||
}
|
||||
|
||||
impl MaybeAppSrc {
|
||||
/// Creates a MaybeAppSrc. Also returns a Sender that you must use to provide an AppSrc as
|
||||
/// soon as one is available. When it is received, the MaybeAppSrc will start pushing data
|
||||
/// into the AppSrc when write() is called.
|
||||
pub(crate) fn new_with_tx() -> (Self, Sender<AppSrc>) {
|
||||
let (tx, rx) = bounded(3); // The sender should not send very often
|
||||
(MaybeAppSrc { rx, app_src: None }, tx)
|
||||
}
|
||||
|
||||
/// Calls end of stream to Gstreamer, using during drop
|
||||
pub(crate) fn end_of_stream(&mut self) {
|
||||
if let Some(src) = self.try_get_src() {
|
||||
// Ignore "errors" from Gstreamer such as FLUSHING, which are not really errors.
|
||||
let _ = src.end_of_stream();
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to retrieve the AppSrc that should be passed in by the caller of new_with_tx
|
||||
/// at some point after this struct has been created. At that point, we swap over to
|
||||
/// owning the AppSrc directly. This function handles either case and returns the AppSrc,
|
||||
/// or None if the caller has not yet sent one.
|
||||
fn try_get_src(&mut self) -> Option<&AppSrc> {
|
||||
while let Ok(src) = self.rx.try_recv() {
|
||||
self.app_src = Some(src);
|
||||
}
|
||||
self.app_src.as_ref()
|
||||
}
|
||||
|
||||
pub(crate) fn is_connected(&self) -> bool {
|
||||
self.app_src.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for MaybeAppSrc {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
// If we have no AppSrc yet, throw away the data and claim that it was written
|
||||
let app_src = match self.try_get_src() {
|
||||
Some(src) => src,
|
||||
None => return Ok(buf.len()),
|
||||
};
|
||||
|
||||
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
|
||||
{
|
||||
let gst_buf_mut = gst_buf.get_mut().unwrap();
|
||||
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
|
||||
gst_buf_data.copy_from_slice(buf);
|
||||
}
|
||||
|
||||
let res = app_src.push_buffer(gst_buf); //.map_err(|e| io::Error::new(io::ErrorKind::Other, Box::new(e)))?;
|
||||
if res.is_err() {
|
||||
self.app_src = None;
|
||||
}
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MaybeAppSrc {
|
||||
fn drop(&mut self) {
|
||||
self.end_of_stream();
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
use anyhow::{anyhow, Error as Anyhow};
|
||||
use crossbeam_channel::{bounded, Receiver, Sender};
|
||||
use gstreamer::prelude::ElementExt;
|
||||
use gstreamer::prelude::ObjectExt;
|
||||
use gstreamer::{Element, Pad};
|
||||
use log::*;
|
||||
|
||||
pub(crate) struct MaybeInputSelect {
|
||||
rx: Receiver<Element>,
|
||||
typefind: Option<Element>,
|
||||
}
|
||||
|
||||
impl MaybeInputSelect {
|
||||
pub(crate) fn new_with_tx() -> (Self, Sender<Element>) {
|
||||
let (tx, rx) = bounded(3); // The sender should not send very often
|
||||
(MaybeInputSelect { rx, typefind: None }, tx)
|
||||
}
|
||||
|
||||
pub(crate) fn try_get_src(&mut self) -> Option<&Element> {
|
||||
while let Ok(src) = self.rx.try_recv() {
|
||||
self.typefind = Some(src);
|
||||
}
|
||||
self.typefind.as_ref()
|
||||
}
|
||||
|
||||
pub(crate) fn set_input(&mut self, path_num: u32) -> Result<(), Anyhow> {
|
||||
if let Some(element) = self.try_get_src() {
|
||||
let new_pad = element
|
||||
.static_pad(&format!("sink_{}", path_num))
|
||||
.ok_or_else(|| anyhow!("Unable to set input pad"))?;
|
||||
|
||||
match element.property_value("active-pad").get::<'_, Pad>() {
|
||||
Ok(active_pad) => {
|
||||
if active_pad != new_pad {
|
||||
debug!("Pad need changing");
|
||||
element.set_property("active-pad", new_pad);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Pad need changing: Active pad invalid: {:?}", e);
|
||||
element.set_property("active-pad", new_pad);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn clear(&mut self) {
|
||||
self.typefind = None;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
use gstreamer::Element;
|
||||
use crossbeam_channel::{sync_channel, Receiver, SyncSender};
|
||||
|
||||
pub(crate) struct MaybeOutputSelect {
|
||||
rx: Receiver<Element>,
|
||||
typefind: Option<Element>,
|
||||
}
|
||||
|
||||
impl MaybeOutputSelect {
|
||||
pub(crate) fn new_with_tx() -> (Self, SyncSender<Element>) {
|
||||
let (tx, rx) = sync_channel(3); // The sender should not send very often
|
||||
(MaybeOutputSelect { rx, typefind: None }, tx)
|
||||
}
|
||||
|
||||
pub(crate) fn try_get_src(&mut self) -> Option<&Element> {
|
||||
while let Some(src) = self.rx.try_recv().ok() {
|
||||
self.typefind = Some(src);
|
||||
}
|
||||
self.typefind.as_ref()
|
||||
}
|
||||
|
||||
pub(crate) fn clear(&mut self) {
|
||||
self.typefind = None;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
use gstreamer::Element;
|
||||
use crossbeam_channel::{sync_channel, Receiver, SyncSender};
|
||||
|
||||
pub(crate) struct MaybeTypeFind {
|
||||
rx: Receiver<Element>,
|
||||
typefind: Option<Element>,
|
||||
}
|
||||
|
||||
impl MaybeTypeFind {
|
||||
pub(crate) fn new_with_tx() -> (Self, SyncSender<Element>) {
|
||||
let (tx, rx) = sync_channel(3); // The sender should not send very often
|
||||
(MaybeTypeFind { rx, typefind: None }, tx)
|
||||
}
|
||||
|
||||
pub(crate) fn try_get_src(&mut self) -> Option<&Element> {
|
||||
while let Some(src) = self.rx.try_recv().ok() {
|
||||
self.typefind = Some(src);
|
||||
}
|
||||
self.typefind.as_ref()
|
||||
}
|
||||
|
||||
pub(crate) fn clear(&mut self) {
|
||||
self.typefind = None;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
//! The component that handles passing BcMedia into
|
||||
//! gstreamer media stream
|
||||
use gstreamer_app::AppSrc;
|
||||
|
||||
use futures::stream::StreamExt;
|
||||
use gstreamer::ClockTime;
|
||||
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::*;
|
||||
use std::sync::{atomic::Ordering, Arc};
|
||||
use tokio::time::{interval, Duration, Instant, MissedTickBehavior};
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
|
||||
use super::{shared::*, AnyResult};
|
||||
|
||||
pub(super) struct NeoMediaSender {
|
||||
pub(super) data_source: ReceiverStream<BcMedia>,
|
||||
pub(super) app_source: ReceiverStream<AppSrc>,
|
||||
pub(super) shared: Arc<NeoMediaShared>,
|
||||
pub(super) appsrcs: Vec<AppSrc>,
|
||||
pub(super) waiting_for_iframe: bool,
|
||||
}
|
||||
|
||||
impl NeoMediaSender {
|
||||
pub(super) async fn run(&mut self) -> AnyResult<()> {
|
||||
let mut resend_pause = interval(Duration::from_secs_f32(1.0f32 / 25.0f32));
|
||||
resend_pause.set_missed_tick_behavior(MissedTickBehavior::Skip);
|
||||
let mut last_iframe: Option<(Instant, u64, Vec<u8>)> = None;
|
||||
loop {
|
||||
tokio::select! {
|
||||
v = self.data_source.next() => {
|
||||
if let Some(bc_media) = v {
|
||||
if ! self.skip_bcmedia(&bc_media)? {
|
||||
resend_pause.reset();
|
||||
self.inspect_bcmedia(&bc_media)?;
|
||||
match bc_media {
|
||||
BcMedia::Iframe(frame) => {
|
||||
last_iframe = Some((
|
||||
Instant::now(),
|
||||
self.shared.microseconds.load(Ordering::Relaxed),
|
||||
frame.data.clone(),
|
||||
));
|
||||
self.process_buffer(&frame.data)?;
|
||||
}
|
||||
BcMedia::Pframe(frame) => {
|
||||
self.process_buffer(&frame.data)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
v = self.app_source.next() => {
|
||||
if let Some(app_src) = v {
|
||||
self.appsrcs.push(app_src);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
},
|
||||
v = resend_pause.tick() => {
|
||||
if let Some((time,microseconds, last_iframe)) = last_iframe.as_ref() {
|
||||
let passed: Duration = v - *time;
|
||||
let ms = *microseconds + passed.as_micros() as u64;
|
||||
self.shared.microseconds.store(ms, Ordering::Relaxed);
|
||||
self.process_buffer(last_iframe)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn process_buffer(&mut self, buf: &Vec<u8>) -> AnyResult<()> {
|
||||
let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap();
|
||||
{
|
||||
let gst_buf_mut = gst_buf.get_mut().unwrap();
|
||||
let time = ClockTime::from_useconds(self.shared.microseconds.load(Ordering::Relaxed));
|
||||
gst_buf_mut.set_pts(time);
|
||||
let mut gst_buf_data = gst_buf_mut.map_writable().unwrap();
|
||||
gst_buf_data.copy_from_slice(buf);
|
||||
}
|
||||
|
||||
for appsrc in self.appsrcs.iter() {
|
||||
appsrc.push_buffer(gst_buf.copy())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
fn skip_bcmedia(&mut self, bc_media: &BcMedia) -> AnyResult<bool> {
|
||||
if self.waiting_for_iframe {
|
||||
if let BcMedia::Iframe(frame) = bc_media {
|
||||
let ms = frame.microseconds as u64;
|
||||
let current_ms = self.shared.microseconds.load(Ordering::Relaxed);
|
||||
if ms > current_ms {
|
||||
self.waiting_for_iframe = false;
|
||||
} else {
|
||||
debug!(
|
||||
"Waiting on iframe but it was in the past: got {} but on {}",
|
||||
ms, current_ms
|
||||
);
|
||||
return Ok(true);
|
||||
}
|
||||
} else {
|
||||
debug!("Waaiting on iframe");
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
match bc_media {
|
||||
BcMedia::Iframe(frame) => {
|
||||
let ms = frame.microseconds as u64;
|
||||
if ms < self.shared.microseconds.load(Ordering::Relaxed) {
|
||||
debug!("Got iframe in the past");
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
BcMedia::Pframe(frame) => {
|
||||
let ms = frame.microseconds as u64;
|
||||
if ms < self.shared.microseconds.load(Ordering::Relaxed) {
|
||||
debug!("Got pframe in the past");
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
fn inspect_bcmedia(&mut self, bc_media: &BcMedia) -> AnyResult<()> {
|
||||
match bc_media {
|
||||
BcMedia::Iframe(frame) => {
|
||||
match frame.video_type {
|
||||
VideoType::H264 => {
|
||||
self.shared
|
||||
.vid_format
|
||||
.store(VidFormats::H264, Ordering::Relaxed);
|
||||
}
|
||||
VideoType::H265 => {
|
||||
self.shared
|
||||
.vid_format
|
||||
.store(VidFormats::H265, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
self.shared
|
||||
.microseconds
|
||||
.store(frame.microseconds as u64, Ordering::Relaxed);
|
||||
}
|
||||
BcMedia::Pframe(frame) => {
|
||||
match frame.video_type {
|
||||
VideoType::H264 => {
|
||||
self.shared
|
||||
.vid_format
|
||||
.store(VidFormats::H264, Ordering::Relaxed);
|
||||
}
|
||||
VideoType::H265 => {
|
||||
self.shared
|
||||
.vid_format
|
||||
.store(VidFormats::H265, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
self.shared
|
||||
.microseconds
|
||||
.store(frame.microseconds as u64, Ordering::Relaxed);
|
||||
}
|
||||
BcMedia::Aac(_aac) => {
|
||||
self.shared
|
||||
.aud_format
|
||||
.store(AudFormats::Aac, Ordering::Relaxed);
|
||||
}
|
||||
BcMedia::Adpcm(_) => {
|
||||
self.shared
|
||||
.aud_format
|
||||
.store(AudFormats::Adpcm, Ordering::Relaxed);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
//! Attempts to subclass RtspServer
|
||||
//!
|
||||
//! We are now messing with gstreamer glib objects
|
||||
//! expect issues
|
||||
|
||||
use super::{factory::*, AnyResult};
|
||||
use anyhow::{anyhow, Context};
|
||||
use gstreamer::glib::object_subclass;
|
||||
use gstreamer::glib::subclass::types::ObjectSubclass;
|
||||
use gstreamer::glib::{self, Object};
|
||||
use gstreamer_rtsp::RTSPAuthMethod;
|
||||
use gstreamer_rtsp_server::{
|
||||
gio::{TlsAuthenticationMode, TlsCertificate},
|
||||
prelude::*,
|
||||
subclass::prelude::*,
|
||||
RTSPAuth, RTSPServer, RTSPToken, RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::*;
|
||||
use std::{
|
||||
collections::{hash_map::Entry, HashMap, HashSet},
|
||||
fs,
|
||||
};
|
||||
use tokio::sync::{mpsc::Sender, RwLock};
|
||||
|
||||
glib::wrapper! {
|
||||
/// The wrapped RTSPServer
|
||||
pub(crate) struct NeoRtspServer(ObjectSubclass<NeoRtspServerImpl>) @extends RTSPServer;
|
||||
}
|
||||
|
||||
impl Default for NeoRtspServer {
|
||||
fn default() -> Self {
|
||||
Self::new().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl NeoRtspServer {
|
||||
pub(crate) fn new() -> AnyResult<Self> {
|
||||
gstreamer::init().context("Gstreamer failed to initialise")?;
|
||||
let factory = Object::new::<NeoRtspServer>(&[]);
|
||||
Ok(factory)
|
||||
}
|
||||
|
||||
pub(crate) async fn get_sender<T: Into<String>>(&self, tag: T) -> Option<Sender<BcMedia>> {
|
||||
self.imp().get_sender(tag).await
|
||||
}
|
||||
|
||||
pub(crate) fn set_credentials(&self, credentials: &[(&str, &str)]) -> AnyResult<()> {
|
||||
self.imp().set_credentials(credentials)
|
||||
}
|
||||
pub(crate) fn set_tls(
|
||||
&self,
|
||||
cert_file: &str,
|
||||
client_auth: TlsAuthenticationMode,
|
||||
) -> AnyResult<()> {
|
||||
self.imp().set_tls(cert_file, client_auth)
|
||||
}
|
||||
|
||||
pub(crate) async fn create_stream<U: Into<String>>(&self, tag: U) -> AnyResult<()> {
|
||||
self.imp().create_stream(tag).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn remove_stream<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
|
||||
self.imp().remove_stream(tag).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn remove_path<T: Into<String>>(
|
||||
&self,
|
||||
tag: T,
|
||||
paths: &[&str],
|
||||
) -> AnyResult<()> {
|
||||
self.imp().remove_path(tag, paths).await
|
||||
}
|
||||
|
||||
pub(crate) async fn add_path<T: Into<String>>(
|
||||
&self,
|
||||
tag: T,
|
||||
paths: &[String],
|
||||
) -> AnyResult<()> {
|
||||
self.imp().add_path(tag, paths).await
|
||||
}
|
||||
|
||||
pub(crate) async fn add_permitted_roles<T: Into<String>, U: AsRef<str>>(
|
||||
&self,
|
||||
tag: T,
|
||||
permitted_users: &HashSet<U>,
|
||||
) -> AnyResult<()> {
|
||||
self.imp().add_permitted_roles(tag, permitted_users).await
|
||||
}
|
||||
|
||||
pub(crate) async fn run(&self, bind_addr: &str, bind_port: u16) -> AnyResult<()> {
|
||||
let server = self;
|
||||
server.set_address(bind_addr);
|
||||
server.set_service(&format!("{}", bind_port));
|
||||
// Attach server to default Glib context
|
||||
let _ = server.attach(None);
|
||||
let main_loop = glib::MainLoop::new(None, false);
|
||||
// Run the Glib main loop.
|
||||
#[allow(clippy::unit_arg)]
|
||||
tokio::task::spawn_blocking(move || Ok(main_loop.run())).await?
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for NeoRtspServer {}
|
||||
unsafe impl Sync for NeoRtspServer {}
|
||||
|
||||
struct FactoryData {
|
||||
factory: NeoMediaFactory,
|
||||
paths: HashSet<String>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct NeoRtspServerImpl {
|
||||
medias: RwLock<HashMap<String, FactoryData>>,
|
||||
}
|
||||
|
||||
impl ObjectImpl for NeoRtspServerImpl {}
|
||||
impl RTSPServerImpl for NeoRtspServerImpl {}
|
||||
|
||||
#[object_subclass]
|
||||
impl ObjectSubclass for NeoRtspServerImpl {
|
||||
const NAME: &'static str = "NeoRtspServer";
|
||||
type Type = NeoRtspServer;
|
||||
type ParentType = RTSPServer;
|
||||
}
|
||||
|
||||
impl NeoRtspServerImpl {
|
||||
pub(crate) async fn create_stream<U: Into<String>>(&self, tag: U) -> AnyResult<()> {
|
||||
let key = tag.into();
|
||||
match self.medias.write().await.entry(key.clone()) {
|
||||
Entry::Occupied(_occ) => {}
|
||||
Entry::Vacant(vac) => {
|
||||
let media = NeoMediaFactory::new();
|
||||
vac.insert(FactoryData {
|
||||
factory: media,
|
||||
paths: Default::default(),
|
||||
});
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn get_sender<T: Into<String>>(&self, tag: T) -> Option<Sender<BcMedia>> {
|
||||
let key = tag.into();
|
||||
self.medias
|
||||
.read()
|
||||
.await
|
||||
.get(&key)
|
||||
.map(|k| k.factory.get_sender())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) async fn remove_stream<T: Into<String>>(&self, tag: T) -> AnyResult<()> {
|
||||
if let Some(mut media) = self.medias.write().await.remove(&tag.into()) {
|
||||
let mounts = self
|
||||
.obj()
|
||||
.mount_points()
|
||||
.expect("The server should have mountpoints");
|
||||
for path in media.paths.iter() {
|
||||
mounts.remove_factory(path);
|
||||
}
|
||||
media.paths.clear();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn add_permitted_roles<T: Into<String>, U: AsRef<str>>(
|
||||
&self,
|
||||
tag: T,
|
||||
permitted_users: &HashSet<U>,
|
||||
) -> AnyResult<()> {
|
||||
let tag: String = tag.into();
|
||||
if let Some(media) = self.medias.write().await.get_mut(&tag) {
|
||||
media.factory.add_permitted_roles(permitted_users);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("No media with tag {} to add users to", &tag))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn add_path<T: Into<String>>(
|
||||
&self,
|
||||
tag: T,
|
||||
paths: &[String],
|
||||
) -> AnyResult<()> {
|
||||
let tag = tag.into();
|
||||
if let Some(media) = self.medias.write().await.get_mut(&tag) {
|
||||
let mounts = self
|
||||
.obj()
|
||||
.mount_points()
|
||||
.expect("The server should have mountpoints");
|
||||
for path in paths {
|
||||
media.paths.insert(path.clone());
|
||||
mounts.add_factory(path, &media.factory);
|
||||
// debug!("Adding path: {}", path);
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"No media with tag {} to add the paths {:?} to",
|
||||
&tag,
|
||||
paths
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn remove_path<T: Into<String>>(
|
||||
&self,
|
||||
tag: T,
|
||||
paths: &[&str],
|
||||
) -> AnyResult<()> {
|
||||
let tag = tag.into();
|
||||
if let Some(media) = self.medias.write().await.get_mut(&tag) {
|
||||
let mounts = self
|
||||
.obj()
|
||||
.mount_points()
|
||||
.expect("The server should have mountpoints");
|
||||
for path in paths {
|
||||
if media.paths.contains(&path.to_string()) {
|
||||
media.paths.remove(&path.to_string());
|
||||
mounts.remove_factory(path);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"No media with tag {} to remove the paths from",
|
||||
&tag
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_credentials(&self, credentials: &[(&str, &str)]) -> AnyResult<()> {
|
||||
let auth = self.obj().auth().unwrap_or_else(RTSPAuth::new);
|
||||
auth.set_supported_methods(RTSPAuthMethod::Basic);
|
||||
|
||||
let mut un_authtoken = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous")]);
|
||||
auth.set_default_token(Some(&mut un_authtoken));
|
||||
|
||||
for credential in credentials {
|
||||
let (user, pass) = credential;
|
||||
trace!("Setting credentials for user {}", user);
|
||||
let token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, user)]);
|
||||
let basic = RTSPAuth::make_basic(user, pass);
|
||||
auth.add_basic(basic.as_str(), &token);
|
||||
}
|
||||
|
||||
self.obj().set_auth(Some(&auth));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_tls(
|
||||
&self,
|
||||
cert_file: &str,
|
||||
client_auth: TlsAuthenticationMode,
|
||||
) -> AnyResult<()> {
|
||||
debug!("Setting up TLS using {}", cert_file);
|
||||
let auth = self.obj().auth().unwrap_or_else(RTSPAuth::new);
|
||||
|
||||
// We seperate reading the file and changing to a PEM so that we get different error messages.
|
||||
let cert_contents = fs::read_to_string(cert_file).with_context(|| "TLS file not found")?;
|
||||
let cert = TlsCertificate::from_pem(&cert_contents)
|
||||
.with_context(|| "Not a valid TLS certificate")?;
|
||||
auth.set_tls_certificate(Some(&cert));
|
||||
auth.set_tls_authentication_mode(client_auth);
|
||||
|
||||
self.obj().set_auth(Some(&auth));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//! Data shared between the various
|
||||
//! components that manage a media stream
|
||||
use atomic_enum::atomic_enum;
|
||||
|
||||
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
|
||||
use std::sync::atomic::AtomicU64;
|
||||
|
||||
#[atomic_enum]
|
||||
#[derive(PartialEq)]
|
||||
pub(super) enum VidFormats {
|
||||
Unknown = 0,
|
||||
H264,
|
||||
H265,
|
||||
}
|
||||
|
||||
#[atomic_enum]
|
||||
#[derive(PartialEq)]
|
||||
pub(super) enum AudFormats {
|
||||
Unknown = 0,
|
||||
Aac,
|
||||
Adpcm,
|
||||
}
|
||||
|
||||
pub(super) struct NeoMediaShared {
|
||||
pub(super) vid_format: AtomicVidFormats,
|
||||
pub(super) aud_format: AtomicAudFormats,
|
||||
pub(super) microseconds: AtomicU64,
|
||||
}
|
||||
|
||||
impl Default for NeoMediaShared {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
vid_format: AtomicVidFormats::new(VidFormats::Unknown),
|
||||
aud_format: AtomicAudFormats::new(AudFormats::Unknown),
|
||||
microseconds: AtomicU64::new(0),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,649 @@
|
||||
//! This module provides an "RtspServer" abstraction that allows consumers of its API to feed it
|
||||
//! data using an ordinary std::io::Write interface.
|
||||
mod maybe_app_src;
|
||||
mod maybe_inputselect;
|
||||
|
||||
pub(crate) use self::maybe_app_src::MaybeAppSrc;
|
||||
pub(crate) use self::maybe_inputselect::MaybeInputSelect;
|
||||
|
||||
// use super::adpcm::adpcm_to_pcm;
|
||||
// use super::errors::Error;
|
||||
use gstreamer::prelude::Cast;
|
||||
use gstreamer::{Bin, ElementFactory, Structure};
|
||||
use gstreamer_app::AppSrc;
|
||||
//use gstreamer_rtsp::RTSPLowerTrans;
|
||||
use anyhow::{anyhow, Context};
|
||||
use gstreamer_rtsp::RTSPAuthMethod;
|
||||
pub use gstreamer_rtsp_server::gio::{TlsAuthenticationMode, TlsCertificate};
|
||||
use gstreamer_rtsp_server::glib;
|
||||
use gstreamer_rtsp_server::prelude::*;
|
||||
use gstreamer_rtsp_server::{
|
||||
RTSPAuth, RTSPMediaFactory, RTSPServer as GstRTSPServer, RTSPToken,
|
||||
RTSP_PERM_MEDIA_FACTORY_ACCESS, RTSP_PERM_MEDIA_FACTORY_CONSTRUCT,
|
||||
RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
};
|
||||
use log::*;
|
||||
use neolink_core::bcmedia::model::*;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::sync::Arc;
|
||||
|
||||
type Result<T> = std::result::Result<T, ()>;
|
||||
type AnyResult<T> = std::result::Result<T, anyhow::Error>;
|
||||
|
||||
pub(crate) struct RtspServer {
|
||||
server: GstRTSPServer,
|
||||
main_loop: Arc<glib::MainLoop>,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
||||
pub(crate) enum InputMode {
|
||||
Live,
|
||||
Paused,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
||||
pub(crate) enum PausedSources {
|
||||
TestSrc,
|
||||
Still,
|
||||
Black,
|
||||
None,
|
||||
}
|
||||
|
||||
pub(crate) struct GstOutputs {
|
||||
pub(crate) audsrc: MaybeAppSrc,
|
||||
pub(crate) vidsrc: MaybeAppSrc,
|
||||
vid_inputselect: MaybeInputSelect,
|
||||
aud_inputselect: MaybeInputSelect,
|
||||
video_format: Option<StreamFormat>,
|
||||
audio_format: Option<StreamFormat>,
|
||||
factory: RTSPMediaFactory,
|
||||
when_paused: PausedSources,
|
||||
last_iframe: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
// The stream from the camera will be using one of these formats
|
||||
//
|
||||
// This is used as part of `StreamOutput` to give hints about
|
||||
// the format of the stream
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
enum StreamFormat {
|
||||
// H264 (AVC) video format
|
||||
H264,
|
||||
// H265 (HEVC) video format
|
||||
H265,
|
||||
// AAC audio
|
||||
Aac,
|
||||
// ADPCM in DVI-4 format
|
||||
Adpcm(u16),
|
||||
}
|
||||
|
||||
impl GstOutputs {
|
||||
pub(crate) fn from_appsrcs(
|
||||
vidsrc: MaybeAppSrc,
|
||||
audsrc: MaybeAppSrc,
|
||||
vid_inputselect: MaybeInputSelect,
|
||||
aud_inputselect: MaybeInputSelect,
|
||||
) -> GstOutputs {
|
||||
let result = GstOutputs {
|
||||
vidsrc,
|
||||
audsrc,
|
||||
vid_inputselect,
|
||||
aud_inputselect,
|
||||
video_format: None,
|
||||
audio_format: None,
|
||||
when_paused: PausedSources::None,
|
||||
factory: RTSPMediaFactory::new(),
|
||||
last_iframe: Default::default(),
|
||||
};
|
||||
result.apply_format();
|
||||
result
|
||||
}
|
||||
|
||||
pub(crate) fn stream_recv(&mut self, media: BcMedia) -> AnyResult<()> {
|
||||
// Ensure stream is on cam mode
|
||||
match media {
|
||||
BcMedia::Iframe(payload) => {
|
||||
let video_type = match payload.video_type {
|
||||
VideoType::H264 => StreamFormat::H264,
|
||||
VideoType::H265 => StreamFormat::H265,
|
||||
};
|
||||
self.set_format(Some(video_type));
|
||||
self.vidsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write IFrame to vidsrc")?;
|
||||
self.last_iframe = Some(payload.data);
|
||||
}
|
||||
BcMedia::Pframe(payload) => {
|
||||
let video_type = match payload.video_type {
|
||||
VideoType::H264 => StreamFormat::H264,
|
||||
VideoType::H265 => StreamFormat::H265,
|
||||
};
|
||||
self.set_format(Some(video_type));
|
||||
self.vidsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write PFrame to vidsrc")?;
|
||||
}
|
||||
BcMedia::Aac(payload) => {
|
||||
self.set_format(Some(StreamFormat::Aac));
|
||||
self.audsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write AAC to audsrc")?;
|
||||
}
|
||||
BcMedia::Adpcm(payload) => {
|
||||
self.set_format(Some(StreamFormat::Adpcm(payload.data.len() as u16)));
|
||||
self.audsrc
|
||||
.write_all(&payload.data)
|
||||
.with_context(|| "Cannot write ADPCM to audsrc")?;
|
||||
}
|
||||
_ => {
|
||||
//Ignore other BcMedia like InfoV1 and InfoV2
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_input_source(&mut self, input_source: InputMode) -> AnyResult<()> {
|
||||
// PausedSources::None is exceptional in that it dosen't swap the stream
|
||||
// to an alterntive source. It just repeats the last buffer meaning not re-encoding
|
||||
// at the cost some clients not handeling the repeating buffers well
|
||||
if self.when_paused != PausedSources::None {
|
||||
match &input_source {
|
||||
InputMode::Live => {
|
||||
self.vid_inputselect.set_input(0)?;
|
||||
self.aud_inputselect.set_input(0)?;
|
||||
}
|
||||
InputMode::Paused => {
|
||||
self.vid_inputselect.set_input(1)?;
|
||||
self.aud_inputselect.set_input(1)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// For PausedSources::None we still want to use the silence as we don't want to
|
||||
// repeat the audio buffers
|
||||
match &input_source {
|
||||
InputMode::Live => {
|
||||
self.aud_inputselect.set_input(0)?;
|
||||
}
|
||||
InputMode::Paused => {
|
||||
self.aud_inputselect.set_input(1)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_paused_source(&mut self, paused_source: PausedSources) {
|
||||
self.when_paused = paused_source;
|
||||
self.apply_format();
|
||||
}
|
||||
|
||||
pub(crate) fn has_last_iframe(&self) -> bool {
|
||||
self.last_iframe.is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn write_last_iframe(&mut self) -> AnyResult<()> {
|
||||
self.vidsrc.write_all(
|
||||
self.last_iframe
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("No iframe data avaliable"))?
|
||||
.as_slice(),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_format(&mut self, format: Option<StreamFormat>) {
|
||||
match format {
|
||||
Some(StreamFormat::H264) | Some(StreamFormat::H265) => {
|
||||
if format != self.video_format {
|
||||
self.video_format = format;
|
||||
self.apply_format();
|
||||
}
|
||||
}
|
||||
Some(StreamFormat::Aac) | Some(StreamFormat::Adpcm(_)) => {
|
||||
if format != self.audio_format {
|
||||
self.audio_format = format;
|
||||
self.apply_format();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_format(&self) {
|
||||
// This is the final sink prior to rtsp
|
||||
// - In the case of PausedSources::None we just pass through without encoding
|
||||
// - In the case of all other pause sources we need to recencode in order
|
||||
// to get smooth streams when the stream is paused/resumed
|
||||
let launch_vid_select = match self.when_paused {
|
||||
PausedSources::None => match self.video_format {
|
||||
Some(StreamFormat::H264) => "! rtph264pay name=pay0",
|
||||
Some(StreamFormat::H265) => "! rtph265pay name=pay0",
|
||||
_ => "! fakesink",
|
||||
},
|
||||
_ => match self.video_format {
|
||||
Some(StreamFormat::H264) => "! x264enc ! rtph264pay name=pay0",
|
||||
Some(StreamFormat::H265) => "! x265enc ! rtph265pay name=pay0",
|
||||
_ => "! fakesink",
|
||||
},
|
||||
};
|
||||
|
||||
// This is the part that deals with input from camera
|
||||
// - In the case of PausedSources::None it just passes the stream
|
||||
// - In the case of other Paused sources we decode it so that we can manipulate
|
||||
// the stream smoothly and swap the source on demand
|
||||
let launch_app_src = match self.when_paused {
|
||||
PausedSources::None => {
|
||||
match self.video_format {
|
||||
Some(StreamFormat::H264) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse"
|
||||
}
|
||||
Some(StreamFormat::H265) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse"
|
||||
}
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
match self.video_format {
|
||||
Some(StreamFormat::H264) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h264parse ! avdec_h264"
|
||||
}
|
||||
Some(StreamFormat::H265) => {
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! h265parse ! avdec_h265"
|
||||
}
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// This controls the alternaive source used when in paused state
|
||||
// PausedSources::None is the exception in that it is never swapped to this state
|
||||
let launch_alt_source = match self.when_paused {
|
||||
PausedSources::TestSrc => "videotestsrc ! video/x-raw,width=896,height=512,framerate=25/1",
|
||||
PausedSources::Black => "videotestsrc pattern=black ! video/x-raw,width=896,height=512,framerate=25/1",
|
||||
PausedSources::Still => "vid_src_tee. ! imagefreeze allow-replace=true is-live=true ! video/x-raw,framerate=25/1",
|
||||
PausedSources::None => "",
|
||||
};
|
||||
|
||||
// This is the final pipeline for the audio prior to rtsp
|
||||
let launch_aud_select = match self.audio_format {
|
||||
Some(StreamFormat::Adpcm(_)) | Some(StreamFormat::Aac) => {
|
||||
"! audioconvert ! rtpL16pay name=pay1"
|
||||
}
|
||||
_ => "! fakesink",
|
||||
};
|
||||
|
||||
// This is the audio source from the camera. We decode to xraw always as some clients
|
||||
// like blue iris only support raw audio
|
||||
let launch_aud = match self.audio_format {
|
||||
Some(StreamFormat::Adpcm(block_size)) => format!("caps=audio/x-adpcm,layout=dvi,block_align={},channels=1,rate=8000 ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! adpcmdec", block_size), // DVI4 is converted to pcm in the appsrc
|
||||
Some(StreamFormat::Aac) => "! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aacparse ! decodebin".to_string(),
|
||||
_ => "".to_string(),
|
||||
};
|
||||
|
||||
// The alternaive audio source is silence
|
||||
let launch_aud_alt = "audiotestsrc wave=silence";
|
||||
|
||||
let launch_str = &vec![
|
||||
"( ",
|
||||
// Video out pipe
|
||||
"(",
|
||||
"input-selector name=vid_inputselect",
|
||||
launch_vid_select,
|
||||
")",
|
||||
// Camera vid source
|
||||
"(",
|
||||
"appsrc name=vidsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME", // 50MB max size so that it won't grow to infinite if the queue blocks
|
||||
launch_app_src,
|
||||
// Pipe it though a tee so that the image freeze can grab it
|
||||
"! tee name=vid_src_tee",
|
||||
"(",
|
||||
"vid_src_tee. ! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! vid_inputselect.sink_0",
|
||||
")",
|
||||
|
||||
")",
|
||||
// Alternaive vid source
|
||||
//
|
||||
// Used during paused mode
|
||||
"(",
|
||||
launch_alt_source,
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! vid_inputselect.sink_1",
|
||||
")",
|
||||
// Image freeze
|
||||
// Audio pipe
|
||||
// Audio out pipe
|
||||
"(",
|
||||
"input-selector name=aud_inputselect",
|
||||
launch_aud_select,
|
||||
")",
|
||||
// Camera aud source
|
||||
"(",
|
||||
"appsrc name=audsrc is-live=true block=true emit-signals=false max-bytes=52428800 do-timestamp=true format=GST_FORMAT_TIME",
|
||||
&launch_aud,
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aud_inputselect.sink_0",
|
||||
")",
|
||||
// Camera aud alt source
|
||||
"(",
|
||||
launch_aud_alt,
|
||||
"! queue silent=true max-size-bytes=10485760 min-threshold-bytes=1024 ! aud_inputselect.sink_1",
|
||||
")",
|
||||
")"
|
||||
]
|
||||
.join(" ");
|
||||
debug!("Gstreamer launch str: {:?}", launch_str);
|
||||
self.factory.set_launch(launch_str);
|
||||
}
|
||||
|
||||
pub(crate) fn is_connected(&self) -> bool {
|
||||
self.vidsrc.is_connected() || self.audsrc.is_connected()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RtspServer {
|
||||
fn default() -> RtspServer {
|
||||
Self::new().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl RtspServer {
|
||||
pub(crate) fn new() -> AnyResult<RtspServer> {
|
||||
gstreamer::init().context("Gstreamer failed to initialise")?;
|
||||
let needed_elements = &[
|
||||
(
|
||||
"appsrc",
|
||||
"app (gst-plugins-base)",
|
||||
"Cannot run without an appsrc",
|
||||
),
|
||||
(
|
||||
"audioconvert",
|
||||
"audioconvert (gst-plugins-base)",
|
||||
"Required for audio",
|
||||
),
|
||||
("adpcmdec", "adpcmdec", "Required for audio"),
|
||||
(
|
||||
"h264parse",
|
||||
"videoparsersbad (gst-plugins-bad)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"h265parse",
|
||||
"videoparsersbad (gst-plugins-bad)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"rtph264pay",
|
||||
"rtp (gst-plugins-good)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"rtph265pay",
|
||||
"rtp (gst-plugins-good)",
|
||||
"Required for certain types of camera",
|
||||
),
|
||||
(
|
||||
"aacparse",
|
||||
"audioparsers (gst-plugins-good)",
|
||||
"Required for certain types of camera's audio",
|
||||
),
|
||||
("rtpL16pay", "rtp (gst-plugins-good)", "Required for audio"),
|
||||
];
|
||||
let sometimes_needed_elements = &[
|
||||
(
|
||||
"x264enc",
|
||||
"x264 (gst-plugins-ugly)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"x265enc",
|
||||
"x265 (gst-plugins-bad)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"avdec_h264",
|
||||
"libav (gst-libav)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"avdec_h265",
|
||||
"libav (gst-libav)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"videotestsrc",
|
||||
"videotestsrc (gst-plugins-base)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"imagefreeze",
|
||||
"imagefreeze (gst-plugins-good)",
|
||||
"Required to paused certain cameras",
|
||||
),
|
||||
(
|
||||
"audiotestsrc",
|
||||
"audiotestsrc (gst-plugins-base)",
|
||||
"Required for pausing",
|
||||
),
|
||||
(
|
||||
"decodebin",
|
||||
"playback (gst-plugins-good)",
|
||||
"Required for pausing",
|
||||
),
|
||||
];
|
||||
let mut fatal = false;
|
||||
for (element, plugin, msg) in needed_elements.iter() {
|
||||
if ElementFactory::find(element).is_none() {
|
||||
error!(
|
||||
"Missing required gstreamer plugin `{}` for `{}` element. {}",
|
||||
plugin, element, msg
|
||||
);
|
||||
fatal = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (element, plugin, msg) in sometimes_needed_elements.iter() {
|
||||
if ElementFactory::find(element).is_none() {
|
||||
warn!(
|
||||
"Missing the gstreamer plugin `{}` for `{}` element. {}",
|
||||
plugin, element, msg
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if fatal {
|
||||
return Err(anyhow!(
|
||||
"Required Gstreamer Elements are missing. Ensure gstreamer is installed correctly"
|
||||
));
|
||||
}
|
||||
Ok(RtspServer {
|
||||
server: GstRTSPServer::new(),
|
||||
main_loop: Arc::new(glib::MainLoop::new(None, false)),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn add_stream<T: AsRef<str>>(
|
||||
&self,
|
||||
paths: &[&str],
|
||||
permitted_users: &HashSet<T>,
|
||||
) -> Result<GstOutputs> {
|
||||
let mounts = self
|
||||
.server
|
||||
.mount_points()
|
||||
.expect("The server should have mountpoints");
|
||||
|
||||
// Create a MaybeAppSrc: Write which we will give the caller. When the backing AppSrc is
|
||||
// created by the factory, fish it out and give it to the waiting MaybeAppSrc via the
|
||||
// channel it provided. This callback may be called more than once by Gstreamer if it is
|
||||
// unhappy with the pipeline, so keep updating the MaybeAppSrc.
|
||||
let (maybe_app_src, tx) = MaybeAppSrc::new_with_tx();
|
||||
let (maybe_app_src_aud, tx_aud) = MaybeAppSrc::new_with_tx();
|
||||
|
||||
let (maybe_vid_inputselect, tx_vid_inputselect) = MaybeInputSelect::new_with_tx();
|
||||
let (maybe_aud_inputselect, tx_aud_inputselect) = MaybeInputSelect::new_with_tx();
|
||||
|
||||
let outputs = GstOutputs::from_appsrcs(
|
||||
maybe_app_src,
|
||||
maybe_app_src_aud,
|
||||
maybe_vid_inputselect,
|
||||
maybe_aud_inputselect,
|
||||
);
|
||||
|
||||
let factory = &outputs.factory;
|
||||
|
||||
debug!(
|
||||
"Permitting {} to access {}",
|
||||
// This is hashmap or (iter) equivalent of join, it requres itertools
|
||||
itertools::Itertools::intersperse(permitted_users.iter().map(|i| i.as_ref()), ", ")
|
||||
.collect::<String>(),
|
||||
paths.join(", ")
|
||||
);
|
||||
self.add_permitted_roles(factory, permitted_users);
|
||||
|
||||
factory.set_shared(true);
|
||||
|
||||
factory.connect_media_configure(move |_factory, media| {
|
||||
debug!("RTSP: media was configured");
|
||||
let bin = media
|
||||
.element()
|
||||
// .expect("Media should have an element")
|
||||
.dynamic_cast::<Bin>()
|
||||
.expect("Media source's element should be a bin");
|
||||
let app_src = bin
|
||||
.by_name_recurse_up("vidsrc")
|
||||
.expect("vidsrc must be present in created bin")
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.expect("Source element is expected to be an appsrc!");
|
||||
let _ = tx.send(app_src); // Receiver may be dropped, don't panic if so
|
||||
|
||||
let app_src_aud = bin
|
||||
.by_name_recurse_up("audsrc")
|
||||
.expect("audsrc must be present in created bin")
|
||||
.dynamic_cast::<AppSrc>()
|
||||
.expect("Source element is expected to be an appsrc!");
|
||||
let _ = tx_aud.send(app_src_aud); // Receiver may be dropped, don't panic if so
|
||||
|
||||
let maybe_vid_inputselect = bin
|
||||
.by_name_recurse_up("vid_inputselect")
|
||||
.expect("vid_inputselect must be present in created bin");
|
||||
let _ = tx_vid_inputselect.send(maybe_vid_inputselect); // Receiver may be dropped, don't panic if so
|
||||
|
||||
let maybe_aud_inputselect = bin
|
||||
.by_name_recurse_up("aud_inputselect")
|
||||
.expect("aud_inputselect must be present in created bin");
|
||||
let _ = tx_aud_inputselect.send(maybe_aud_inputselect); // Receiver may be dropped, don't panic if so
|
||||
});
|
||||
|
||||
for path in paths {
|
||||
mounts.add_factory(path, factory);
|
||||
}
|
||||
|
||||
Ok(outputs)
|
||||
}
|
||||
|
||||
pub(crate) fn remove_stream(&self, paths: &[&str]) -> Result<()> {
|
||||
let mounts = self
|
||||
.server
|
||||
.mount_points()
|
||||
.expect("The server should have mountpoints");
|
||||
for path in paths {
|
||||
mounts.remove_factory(path);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn add_permitted_roles<T: AsRef<str>>(
|
||||
&self,
|
||||
factory: &RTSPMediaFactory,
|
||||
permitted_roles: &HashSet<T>,
|
||||
) {
|
||||
for permitted_role in permitted_roles {
|
||||
factory.add_role_from_structure(&Structure::new(
|
||||
permitted_role.as_ref(),
|
||||
&[
|
||||
(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true),
|
||||
(*RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, &true),
|
||||
],
|
||||
));
|
||||
}
|
||||
// During auth, first it binds anonymously. At this point it checks
|
||||
// RTSP_PERM_MEDIA_FACTORY_ACCESS to see if anyone can connect
|
||||
// This is done before the auth token is loaded, possibliy an upstream bug there
|
||||
// After checking RTSP_PERM_MEDIA_FACTORY_ACCESS anonymously
|
||||
// It loads the auth token of the user and checks that users
|
||||
// RTSP_PERM_MEDIA_FACTORY_CONSTRUCT allowing them to play
|
||||
// As a result of this we must ensure that if anonymous is not granted RTSP_PERM_MEDIA_FACTORY_ACCESS
|
||||
// As a part of permitted users then we must allow it to access
|
||||
// at least RTSP_PERM_MEDIA_FACTORY_ACCESS but not RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// Watching Actually happens during RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// So this should be OK to do.
|
||||
// FYI: If no RTSP_PERM_MEDIA_FACTORY_ACCESS then server returns 404 not found
|
||||
// If yes RTSP_PERM_MEDIA_FACTORY_ACCESS but no RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
|
||||
// server returns 401 not authourised
|
||||
if !permitted_roles
|
||||
.iter()
|
||||
.map(|i| i.as_ref())
|
||||
.collect::<HashSet<&str>>()
|
||||
.contains(&"anonymous")
|
||||
{
|
||||
factory.add_role_from_structure(&Structure::new(
|
||||
"anonymous",
|
||||
&[(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true)],
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_credentials(&self, credentials: &[(&str, &str)]) -> Result<()> {
|
||||
let auth = self.server.auth().unwrap_or_else(RTSPAuth::new);
|
||||
auth.set_supported_methods(RTSPAuthMethod::Basic);
|
||||
|
||||
let mut un_authtoken = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous")]);
|
||||
auth.set_default_token(Some(&mut un_authtoken));
|
||||
|
||||
for credential in credentials {
|
||||
let (user, pass) = credential;
|
||||
trace!("Setting credentials for user {}", user);
|
||||
let token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, user)]);
|
||||
let basic = RTSPAuth::make_basic(user, pass);
|
||||
auth.add_basic(basic.as_str(), &token);
|
||||
}
|
||||
|
||||
self.server.set_auth(Some(&auth));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_tls(
|
||||
&self,
|
||||
cert_file: &str,
|
||||
client_auth: TlsAuthenticationMode,
|
||||
) -> Result<()> {
|
||||
debug!("Setting up TLS using {}", cert_file);
|
||||
let auth = self.server.auth().unwrap_or_else(RTSPAuth::new);
|
||||
|
||||
// We seperate reading the file and changing to a PEM so that we get different error messages.
|
||||
let cert_contents = fs::read_to_string(cert_file).expect("TLS file not found");
|
||||
let cert = TlsCertificate::from_pem(&cert_contents).expect("Not a valid TLS certificate");
|
||||
auth.set_tls_certificate(Some(&cert));
|
||||
auth.set_tls_authentication_mode(client_auth);
|
||||
|
||||
self.server.set_auth(Some(&auth));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn run(&self, bind_addr: &str, bind_port: u16) {
|
||||
self.server.set_address(bind_addr);
|
||||
self.server.set_service(&format!("{}", bind_port));
|
||||
// Attach server to default Glib context
|
||||
let _ = self.server.attach(None);
|
||||
let main_loop = self.main_loop.clone();
|
||||
// Run the Glib main loop.
|
||||
let _ = tokio::task::spawn_blocking(move || main_loop.run()).await;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn quit(&self) {
|
||||
self.main_loop.quit();
|
||||
}
|
||||
}
|
||||
+15
-56
@@ -68,13 +68,13 @@ use states::{RtspCamera, StateInfo};
|
||||
|
||||
use super::config::{CameraConfig, Config, UserConfig};
|
||||
pub(crate) use cmdline::Opt;
|
||||
use gst::{RtspServer, TlsAuthenticationMode};
|
||||
use gst::{NeoRtspServer, TlsAuthenticationMode};
|
||||
|
||||
/// Entry point for the rtsp subcommand
|
||||
///
|
||||
/// Opt is the command line options
|
||||
pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
let rtsp = Arc::new(RtspServer::new()?);
|
||||
let rtsp = Arc::new(NeoRtspServer::new()?);
|
||||
|
||||
set_up_tls(&config, &rtsp);
|
||||
|
||||
@@ -94,6 +94,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
let user_config = arc_user_config.clone();
|
||||
let arc_rtsp = rtsp.clone();
|
||||
let abort_handle = abort.clone();
|
||||
|
||||
set.spawn(async move {
|
||||
let backoff = Backoff::new();
|
||||
|
||||
@@ -103,7 +104,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
match failure {
|
||||
Err(CameraFailureKind::Fatal(e)) => {
|
||||
error!("{}: Fatal error: {:?}", camera_config.name, e);
|
||||
break;
|
||||
return Err(e);
|
||||
}
|
||||
Err(CameraFailureKind::Retry(e)) => {
|
||||
warn!("{}: Retryable error: {:X?}", camera_config.name, e);
|
||||
@@ -115,6 +116,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
info!(
|
||||
@@ -127,7 +129,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> {
|
||||
set.spawn(async move { rtsp.run(&bind_addr, bind_port).await });
|
||||
|
||||
if let Some(joined) = set.join_next().await {
|
||||
joined?
|
||||
joined??
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -141,7 +143,7 @@ enum CameraFailureKind {
|
||||
async fn camera_main(
|
||||
config: &CameraConfig,
|
||||
user_config: &[UserConfig],
|
||||
rtsp: Arc<RtspServer>,
|
||||
rtsp: Arc<NeoRtspServer>,
|
||||
) -> Result<(), CameraFailureKind> {
|
||||
// Connect
|
||||
let mut camera = RtspCamera::new(config, user_config, rtsp)
|
||||
@@ -178,20 +180,13 @@ async fn camera_main(
|
||||
};
|
||||
|
||||
let motion_timeout: Duration = Duration::from_secs_f64(config.pause.motion_timeout);
|
||||
let mut ready_to_pause_print = false;
|
||||
loop {
|
||||
match camera.get_state() {
|
||||
StateInfo::Streaming => {
|
||||
let can_pause = camera.can_pause().await;
|
||||
if config.pause.on_disconnect {
|
||||
if let Some(false) = camera.client_connected().await {
|
||||
if can_pause {
|
||||
info!("Pause on disconnect");
|
||||
camera.pause().await.map_err(CameraFailureKind::Retry)?;
|
||||
} else if !ready_to_pause_print {
|
||||
ready_to_pause_print = true;
|
||||
warn!("Not ready to pause");
|
||||
}
|
||||
info!("Pause on disconnect");
|
||||
camera.login().await.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
}
|
||||
if config.pause.on_motion {
|
||||
@@ -202,22 +197,12 @@ async fn camera_main(
|
||||
.map_err(CameraFailureKind::Retry)?
|
||||
{
|
||||
Some(false) => {
|
||||
if can_pause {
|
||||
info!("Pause on motion");
|
||||
camera.pause().await.map_err(CameraFailureKind::Retry)?;
|
||||
} else if !ready_to_pause_print {
|
||||
ready_to_pause_print = true;
|
||||
warn!("Not ready to pause");
|
||||
}
|
||||
info!("Pause on motion");
|
||||
camera.login().await.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
None => {
|
||||
if can_pause {
|
||||
info!("Pause on motion (start)");
|
||||
camera.pause().await.map_err(CameraFailureKind::Retry)?;
|
||||
} else if !ready_to_pause_print {
|
||||
ready_to_pause_print = true;
|
||||
warn!("Not ready to pause");
|
||||
}
|
||||
info!("Pause on motion (start)");
|
||||
camera.login().await.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -230,32 +215,6 @@ async fn camera_main(
|
||||
)));
|
||||
}
|
||||
}
|
||||
StateInfo::Paused => {
|
||||
if config.pause.on_disconnect {
|
||||
if let Some(true) = camera.client_connected().await {
|
||||
info!("Resume on disconnect");
|
||||
camera.stream().await.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
}
|
||||
if config.pause.on_motion {
|
||||
if let Some(motion) = motion.as_mut() {
|
||||
if let Some(true) = motion
|
||||
.motion_detected_within(motion_timeout)
|
||||
.with_context(|| "Motion detection unexpectedly stopped")
|
||||
.map_err(CameraFailureKind::Retry)?
|
||||
{
|
||||
info!("Resume on motion");
|
||||
camera.stream().await.map_err(CameraFailureKind::Retry)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Err(e) = camera.is_running().await {
|
||||
return Err(CameraFailureKind::Retry(anyhow!(
|
||||
"Camera has unexpectanely stopped the paused state: {:?}",
|
||||
e
|
||||
)));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(CameraFailureKind::Retry(anyhow!(
|
||||
"Camera has unexpectanely stopped the paused state"
|
||||
@@ -266,7 +225,7 @@ async fn camera_main(
|
||||
}
|
||||
}
|
||||
|
||||
fn set_up_tls(config: &Config, rtsp: &RtspServer) {
|
||||
fn set_up_tls(config: &Config, rtsp: &NeoRtspServer) {
|
||||
let tls_client_auth = match &config.tls_client_auth as &str {
|
||||
"request" => TlsAuthenticationMode::Requested,
|
||||
"require" => TlsAuthenticationMode::Required,
|
||||
@@ -279,7 +238,7 @@ fn set_up_tls(config: &Config, rtsp: &RtspServer) {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_up_users(users: &[UserConfig], rtsp: &RtspServer) {
|
||||
fn set_up_users(users: &[UserConfig], rtsp: &NeoRtspServer) {
|
||||
// Setting up users
|
||||
let credentials: Vec<_> = users
|
||||
.iter()
|
||||
|
||||
+18
-76
@@ -9,18 +9,16 @@ use std::sync::Arc;
|
||||
use neolink_core::bc_protocol::{MaxEncryption, MotionData, StreamKind as Stream};
|
||||
|
||||
use crate::config::{CameraConfig, UserConfig};
|
||||
use crate::rtsp::gst::RtspServer;
|
||||
use crate::rtsp::gst::NeoRtspServer;
|
||||
use crate::utils::AddressOrUid;
|
||||
|
||||
mod connected;
|
||||
mod loggedin;
|
||||
mod paused;
|
||||
mod shared;
|
||||
mod streaming;
|
||||
|
||||
pub(crate) use connected::Connected;
|
||||
pub(crate) use loggedin::LoggedIn;
|
||||
pub(crate) use paused::Paused;
|
||||
pub(crate) use shared::{CameraState, Shared};
|
||||
pub(crate) use streaming::Streaming;
|
||||
|
||||
@@ -28,14 +26,12 @@ pub(crate) enum StateInfo {
|
||||
Connected,
|
||||
LoggedIn,
|
||||
Streaming,
|
||||
Paused,
|
||||
}
|
||||
|
||||
enum State {
|
||||
Connected(Connected),
|
||||
LoggedIn(LoggedIn),
|
||||
Streaming(Streaming),
|
||||
Paused(Paused),
|
||||
}
|
||||
|
||||
///
|
||||
@@ -50,7 +46,7 @@ impl RtspCamera {
|
||||
pub(crate) async fn new(
|
||||
config: &CameraConfig,
|
||||
users: &[UserConfig],
|
||||
rtsp: Arc<RtspServer>,
|
||||
rtsp: Arc<NeoRtspServer>,
|
||||
) -> Result<Self, Error> {
|
||||
let camera_addr =
|
||||
AddressOrUid::new(&config.camera_addr, &config.camera_uid, &config.discovery)
|
||||
@@ -91,7 +87,7 @@ impl RtspCamera {
|
||||
|
||||
// Used to build the list of strings to pass to gtreamer for the allowed users
|
||||
let all_users_hash = || users.iter().map(|u| u.name.clone()).collect();
|
||||
let permitted_users = match &config.permitted_users {
|
||||
let permitted_users: HashSet<String> = match &config.permitted_users {
|
||||
// If in the camera config there is the user "anyone", or if none is specified but users
|
||||
// are defined at all, then we add all users to the camera's allowed list.
|
||||
Some(p) if p.iter().any(|u| u == "anyone") => all_users_hash(),
|
||||
@@ -110,6 +106,7 @@ impl RtspCamera {
|
||||
"aes" => MaxEncryption::Aes,
|
||||
_ => MaxEncryption::Aes,
|
||||
};
|
||||
|
||||
let shared = Shared {
|
||||
camera: Arc::new(camera),
|
||||
addr: camera_addr,
|
||||
@@ -125,6 +122,19 @@ impl RtspCamera {
|
||||
strict: config.strict,
|
||||
};
|
||||
|
||||
// Set up streams on the RTSP camera
|
||||
for stream in shared.streams.iter() {
|
||||
let tag = shared.get_tag_for_stream(stream);
|
||||
shared.rtsp.create_stream(&tag).await?;
|
||||
shared
|
||||
.rtsp
|
||||
.add_permitted_roles(&tag, &shared.permitted_users)
|
||||
.await?;
|
||||
let paths: Vec<String> = shared.get_paths_for_stream(stream);
|
||||
debug!("Adding path {:?} for {}", paths, tag);
|
||||
shared.rtsp.add_path(&tag, &paths).await?;
|
||||
}
|
||||
|
||||
let mut state: Connected = Default::default();
|
||||
state.setup(&shared).await?;
|
||||
|
||||
@@ -139,7 +149,6 @@ impl RtspCamera {
|
||||
State::Connected(_) => StateInfo::Connected,
|
||||
State::LoggedIn(_) => StateInfo::LoggedIn,
|
||||
State::Streaming(_) => StateInfo::Streaming,
|
||||
State::Paused(_) => StateInfo::Paused,
|
||||
}
|
||||
}
|
||||
pub(crate) async fn login(&mut self) -> Result<()> {
|
||||
@@ -160,17 +169,6 @@ impl RtspCamera {
|
||||
self.state = State::LoggedIn(state);
|
||||
info!("{}: Successfully stopped straming", &self.shared.name);
|
||||
}
|
||||
State::Paused(ref mut connected) => {
|
||||
info!("{}: Logging in", &self.shared.name);
|
||||
connected.tear_down(&self.shared).await?;
|
||||
let state: LoggedIn = Default::default();
|
||||
// state.setup(&self.shared).await?;
|
||||
self.state = State::LoggedIn(state);
|
||||
info!(
|
||||
"{}: Successfully stopped paused streaming",
|
||||
&self.shared.name
|
||||
);
|
||||
}
|
||||
State::LoggedIn(_) => {
|
||||
info!("Already logged in");
|
||||
}
|
||||
@@ -186,19 +184,6 @@ impl RtspCamera {
|
||||
State::Streaming(_) => {
|
||||
info!("{}: Already streaming", &self.shared.name);
|
||||
}
|
||||
State::Paused(ref mut paused) => {
|
||||
info!("{}: Resuming stream", &self.shared.name);
|
||||
let outputs = paused.take_outputs().await?;
|
||||
|
||||
paused.tear_down(&self.shared).await?;
|
||||
let mut state: Streaming = Default::default();
|
||||
|
||||
state.insert_outputs(outputs)?;
|
||||
|
||||
state.setup(&self.shared).await?;
|
||||
self.state = State::Streaming(state);
|
||||
info!("{}: Successfully resumed streaming", &self.shared.name);
|
||||
}
|
||||
State::LoggedIn(ref mut _loggedin) => {
|
||||
info!("{}: Starting stream", &self.shared.name);
|
||||
// loggedin.tear_down(&self.shared).await?;
|
||||
@@ -211,43 +196,8 @@ impl RtspCamera {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn pause(&mut self) -> Result<()> {
|
||||
match &mut self.state {
|
||||
State::Connected(_) => {
|
||||
return Err(anyhow!("{}: Must login first", &self.shared.name));
|
||||
}
|
||||
State::Streaming(ref mut old_state) => {
|
||||
info!("{}: Pausing stream", &self.shared.name);
|
||||
let outputs = old_state.take_outputs().await?;
|
||||
|
||||
old_state.tear_down(&self.shared).await?;
|
||||
let mut state: Paused = Default::default();
|
||||
|
||||
state.insert_outputs(outputs)?;
|
||||
|
||||
state.setup(&self.shared).await?;
|
||||
self.state = State::Paused(state);
|
||||
info!("{}: Successfully paused streaming", &self.shared.name);
|
||||
}
|
||||
State::Paused(_) => {
|
||||
info!("{}: Already paused", &self.shared.name);
|
||||
}
|
||||
State::LoggedIn(_) => {
|
||||
info!(
|
||||
"{}: Cannot pause a stream that has not started",
|
||||
&self.shared.name
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn client_connected(&self) -> Option<bool> {
|
||||
match &self.state {
|
||||
State::Streaming(state) => Some(state.client_connected().await),
|
||||
State::Paused(state) => Some(state.client_connected().await),
|
||||
_ => None,
|
||||
}
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub(crate) async fn motion_data(&self) -> Result<MotionData> {
|
||||
@@ -261,18 +211,10 @@ impl RtspCamera {
|
||||
pub(crate) async fn is_running(&mut self) -> Result<()> {
|
||||
match &mut self.state {
|
||||
State::Streaming(state) => state.is_running().await,
|
||||
State::Paused(state) => state.is_running().await,
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn can_pause(&self) -> bool {
|
||||
match &self.state {
|
||||
State::Streaming(state) => state.can_pause().await,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn manage(&self) -> Result<()> {
|
||||
if let State::Connected(_) = self.state {
|
||||
return Err(anyhow!("Cannot manage a camera that is not logged in"));
|
||||
|
||||
@@ -1,191 +0,0 @@
|
||||
// This is the paused state
|
||||
//
|
||||
// Video data is not pulled from the camera
|
||||
// instead dummy data is sent into the gstreamer source
|
||||
|
||||
use anyhow::{anyhow, Context, Error, Result};
|
||||
use async_trait::async_trait;
|
||||
use crossbeam::utils::Backoff;
|
||||
use log::*;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::{
|
||||
sync::Mutex,
|
||||
task::{self, JoinHandle},
|
||||
};
|
||||
|
||||
use neolink_core::bc_protocol::StreamKind as Stream;
|
||||
|
||||
use super::{CameraState, Shared};
|
||||
|
||||
use crate::rtsp::{
|
||||
abort::AbortHandle,
|
||||
gst::{GstOutputs, InputMode, PausedSources},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Paused {
|
||||
handles: HashMap<Stream, JoinHandle<Result<(), anyhow::Error>>>,
|
||||
outputs: HashMap<Stream, Arc<Mutex<GstOutputs>>>,
|
||||
abort_handle: AbortHandle,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CameraState for Paused {
|
||||
async fn setup(&mut self, shared: &Shared) -> Result<(), Error> {
|
||||
self.abort_handle.reset();
|
||||
// Create new gst outputs
|
||||
//
|
||||
// Otherwise use those already present
|
||||
if self.outputs.is_empty() {
|
||||
let paused_source = match shared.pause.mode.as_str() {
|
||||
"test" => PausedSources::TestSrc,
|
||||
"still" => PausedSources::Still,
|
||||
"black" => PausedSources::Black,
|
||||
"none" => PausedSources::None,
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
};
|
||||
|
||||
for stream in shared.streams.iter() {
|
||||
self.outputs.entry(*stream).or_insert_with_key(|stream| {
|
||||
let paths = shared.get_paths(stream);
|
||||
let mut output = shared
|
||||
.rtsp
|
||||
.add_stream(
|
||||
paths
|
||||
.iter()
|
||||
.map(|s| s.as_str())
|
||||
.collect::<Vec<&str>>()
|
||||
.as_slice(),
|
||||
&shared.permitted_users,
|
||||
)
|
||||
.unwrap();
|
||||
output.set_paused_source(paused_source);
|
||||
Arc::new(Mutex::new(output))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Start the streams on their own thread with a shared abort handle
|
||||
let abort_handle = self.abort_handle.clone();
|
||||
|
||||
for (stream, output) in &self.outputs {
|
||||
let stream_display_name = match stream {
|
||||
Stream::Main => "Main Stream (Clear)",
|
||||
Stream::Sub => "Sub Stream (Fluent)",
|
||||
Stream::Extern => "Extern Stream (Balanced)",
|
||||
};
|
||||
|
||||
// Lock and setup output
|
||||
{
|
||||
let mut locked_output = output.lock().await;
|
||||
locked_output.set_input_source(InputMode::Paused)?;
|
||||
}
|
||||
|
||||
info!(
|
||||
"{}: Starting paused stream {}",
|
||||
&shared.name, stream_display_name
|
||||
);
|
||||
|
||||
let arc_abort_handle = abort_handle.clone();
|
||||
let output_thread = output.clone();
|
||||
|
||||
let handle = task::spawn(async move {
|
||||
let backoff = Backoff::new();
|
||||
while arc_abort_handle.is_live() {
|
||||
let mut locked_output = output_thread.lock().await;
|
||||
locked_output.write_last_iframe()?;
|
||||
backoff.spin();
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
|
||||
self.handles.entry(*stream).or_insert_with(|| handle);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn tear_down(&mut self, shared: &Shared) -> Result<(), Error> {
|
||||
self.abort_handle.abort();
|
||||
|
||||
if !self.handles.is_empty() {
|
||||
for path in shared.get_all_paths().iter() {
|
||||
if let Err(e) = shared.rtsp.remove_stream(&[path]) {
|
||||
return Err(anyhow!("Failed to shutdown RTSP Path {}: {:?}", path, e));
|
||||
}
|
||||
}
|
||||
|
||||
for (stream, handle) in self.handles.drain() {
|
||||
info!("{}: Stopping paused stream {:?}", &shared.name, stream);
|
||||
|
||||
match handle.await {
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)),
|
||||
Ok(Ok(_)) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Paused {
|
||||
fn drop(&mut self) {
|
||||
self.abort_handle.abort();
|
||||
}
|
||||
}
|
||||
|
||||
impl Paused {
|
||||
pub(crate) async fn is_running(&mut self) -> Result<()> {
|
||||
if self.handles.iter().all(|(_, h)| !h.is_finished()) && self.abort_handle.is_live() {
|
||||
return Ok(());
|
||||
}
|
||||
if !self.abort_handle.is_live() {
|
||||
return Err(anyhow!("Stream aborted"));
|
||||
}
|
||||
for (s, h) in self.handles.drain() {
|
||||
h.await?.context(format!("On stream: {:?}", s))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn take_outputs(&mut self) -> Result<HashMap<Stream, GstOutputs>> {
|
||||
self.abort_handle.abort();
|
||||
for (stream, handle) in self.handles.drain() {
|
||||
match handle.await {
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)),
|
||||
Ok(Ok(_)) => {}
|
||||
}
|
||||
}
|
||||
let mut result: HashMap<_, _> = Default::default();
|
||||
for (stream, arc_mutex_output) in self.outputs.drain() {
|
||||
let mutex_output =
|
||||
Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?;
|
||||
let output = mutex_output.into_inner();
|
||||
result.insert(stream, output);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn insert_outputs(&mut self, mut input: HashMap<Stream, GstOutputs>) -> Result<()> {
|
||||
self.outputs = input
|
||||
.drain()
|
||||
.map(|(s, o)| (s, Arc::new(Mutex::new(o))))
|
||||
.collect();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn client_connected(&self) -> bool {
|
||||
for (_, output) in self.outputs.iter() {
|
||||
if output.lock().await.is_connected() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
+43
-23
@@ -10,7 +10,7 @@ use std::sync::Arc;
|
||||
use neolink_core::bc_protocol::{BcCamera, MaxEncryption, StreamKind as Stream};
|
||||
|
||||
use crate::config::PauseConfig;
|
||||
use crate::rtsp::gst::RtspServer;
|
||||
use crate::rtsp::gst::NeoRtspServer;
|
||||
use crate::utils::AddressOrUid;
|
||||
|
||||
#[allow(dead_code)]
|
||||
@@ -22,7 +22,7 @@ pub(crate) struct Shared {
|
||||
pub(super) username: String,
|
||||
pub(super) password: Option<String>,
|
||||
pub(super) streams: HashSet<Stream>,
|
||||
pub(super) rtsp: Arc<RtspServer>,
|
||||
pub(super) rtsp: Arc<NeoRtspServer>,
|
||||
pub(super) permitted_users: HashSet<String>,
|
||||
pub(super) pause: PauseConfig,
|
||||
pub(super) max_encryption: MaxEncryption,
|
||||
@@ -30,28 +30,48 @@ pub(crate) struct Shared {
|
||||
}
|
||||
|
||||
impl Shared {
|
||||
pub(super) fn get_all_paths(&self) -> Vec<String> {
|
||||
self.streams
|
||||
.iter()
|
||||
.flat_map(|s| self.get_paths(s))
|
||||
.collect()
|
||||
pub(crate) fn get_tag_for_stream(&self, stream: &Stream) -> String {
|
||||
format!("{}::{:?}", self.name, stream)
|
||||
}
|
||||
|
||||
pub(super) fn get_paths(&self, stream: &Stream) -> Vec<String> {
|
||||
let mut streams = match stream {
|
||||
Stream::Main => vec![
|
||||
format!("/{}", &self.name),
|
||||
format!("/{}/mainStream", &self.name),
|
||||
],
|
||||
Stream::Sub => vec![format!("/{}/subStream", &self.name)],
|
||||
Stream::Extern => {
|
||||
vec![format!("/{}/externStream", &self.name)]
|
||||
}
|
||||
};
|
||||
// Later VLC clients seem to only support lower case streams
|
||||
let mut lowercase_streams: Vec<String> = streams.iter().map(|i| i.to_lowercase()).collect();
|
||||
streams.append(&mut lowercase_streams);
|
||||
streams
|
||||
pub(crate) fn get_paths_for_stream(&self, stream: &Stream) -> Vec<String> {
|
||||
vec![
|
||||
// Normal case
|
||||
format!("/{}/{:?}", self.name, stream),
|
||||
format!("/{}/{:?}Stream", self.name, stream),
|
||||
format!("/{}/{:?}stream", self.name, stream),
|
||||
// Lower case name
|
||||
format!("/{}/{:?}", self.name.to_lowercase(), stream),
|
||||
format!("/{}/{:?}Stream", self.name.to_lowercase(), stream),
|
||||
format!("/{}/{:?}stream", self.name.to_lowercase(), stream),
|
||||
// Lower case stream
|
||||
format!("/{}/{}", self.name, format!("{:?}", stream).to_lowercase()),
|
||||
format!(
|
||||
"/{}/{}Stream",
|
||||
self.name,
|
||||
format!("{:?}", stream).to_lowercase()
|
||||
),
|
||||
format!(
|
||||
"/{}/{}stream",
|
||||
self.name,
|
||||
format!("{:?}", stream).to_lowercase()
|
||||
),
|
||||
// Lower case both
|
||||
format!(
|
||||
"/{}/{}",
|
||||
self.name.to_lowercase(),
|
||||
format!("{:?}", stream).to_lowercase()
|
||||
),
|
||||
format!(
|
||||
"/{}/{}Stream",
|
||||
self.name.to_lowercase(),
|
||||
format!("{:?}", stream).to_lowercase()
|
||||
),
|
||||
format!(
|
||||
"/{}/{}stream",
|
||||
self.name.to_lowercase(),
|
||||
format!("{:?}", stream).to_lowercase()
|
||||
),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+15
-113
@@ -6,25 +6,17 @@ use anyhow::{anyhow, Context, Error, Result};
|
||||
use async_trait::async_trait;
|
||||
use log::*;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::{
|
||||
sync::Mutex,
|
||||
task::{self, JoinHandle},
|
||||
};
|
||||
use tokio::task::{self, JoinHandle};
|
||||
|
||||
use neolink_core::bc_protocol::StreamKind as Stream;
|
||||
|
||||
use super::{CameraState, Shared};
|
||||
|
||||
use crate::rtsp::{
|
||||
abort::AbortHandle,
|
||||
gst::{GstOutputs, InputMode, PausedSources},
|
||||
};
|
||||
use crate::rtsp::abort::AbortHandle;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Streaming {
|
||||
handles: HashMap<Stream, JoinHandle<Result<(), Error>>>,
|
||||
outputs: HashMap<Stream, Arc<Mutex<GstOutputs>>>,
|
||||
abort_handle: AbortHandle,
|
||||
}
|
||||
|
||||
@@ -35,73 +27,35 @@ impl CameraState for Streaming {
|
||||
// Create new gst outputs
|
||||
//
|
||||
// Otherwise use those already present
|
||||
if self.outputs.is_empty() {
|
||||
let paused_source = match shared.pause.mode.as_str() {
|
||||
"test" => PausedSources::TestSrc,
|
||||
"still" => PausedSources::Still,
|
||||
"black" => PausedSources::Black,
|
||||
"none" => PausedSources::None,
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
};
|
||||
for stream in shared.streams.iter() {
|
||||
let tag = shared.get_tag_for_stream(stream);
|
||||
let sender = shared
|
||||
.rtsp
|
||||
.get_sender(tag)
|
||||
.await
|
||||
.ok_or_else(|| anyhow!("Stream has not been created"))?;
|
||||
|
||||
for stream in shared.streams.iter() {
|
||||
self.outputs.entry(*stream).or_insert_with_key(|stream| {
|
||||
let paths = shared.get_paths(stream);
|
||||
let mut output = shared
|
||||
.rtsp
|
||||
.add_stream(
|
||||
paths
|
||||
.iter()
|
||||
.map(|s| s.as_str())
|
||||
.collect::<Vec<&str>>()
|
||||
.as_slice(),
|
||||
&shared.permitted_users,
|
||||
)
|
||||
.unwrap();
|
||||
output.set_paused_source(paused_source);
|
||||
Arc::new(Mutex::new(output))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Start the streams on their own thread with a shared abort handle
|
||||
let camera = &shared.camera;
|
||||
let abort_handle = self.abort_handle.clone();
|
||||
|
||||
for (stream, output) in &self.outputs {
|
||||
let stream_display_name = match stream {
|
||||
Stream::Main => "Main Stream (Clear)",
|
||||
Stream::Sub => "Sub Stream (Fluent)",
|
||||
Stream::Extern => "Extern Stream (Balanced)",
|
||||
};
|
||||
|
||||
// Lock and setup output
|
||||
{
|
||||
let mut locked_output = output.lock().await;
|
||||
locked_output.set_input_source(InputMode::Live)?;
|
||||
}
|
||||
|
||||
info!(
|
||||
"{}: Starting video stream {}",
|
||||
&shared.name, stream_display_name
|
||||
);
|
||||
|
||||
let arc_camera = camera.clone();
|
||||
let arc_abort_handle = abort_handle.clone();
|
||||
let output_thread = output.clone();
|
||||
|
||||
let thread_camera = shared.camera.clone();
|
||||
let thread_abort_handle = self.abort_handle.clone();
|
||||
let stream_thead = *stream;
|
||||
let strict_thread = shared.strict;
|
||||
let handle = task::spawn(async move {
|
||||
let mut stream_data = arc_camera
|
||||
let mut stream_data = thread_camera
|
||||
.start_video(stream_thead, 0, strict_thread)
|
||||
.await?;
|
||||
while arc_abort_handle.is_live() {
|
||||
while thread_abort_handle.is_live() {
|
||||
let data = stream_data.get_data().await?;
|
||||
let mut locked_output = output_thread.lock().await;
|
||||
locked_output.stream_recv(data?)?;
|
||||
sender.send(data?).await?;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
@@ -112,16 +66,10 @@ impl CameraState for Streaming {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn tear_down(&mut self, shared: &Shared) -> Result<(), Error> {
|
||||
async fn tear_down(&mut self, _shared: &Shared) -> Result<(), Error> {
|
||||
self.abort_handle.abort();
|
||||
|
||||
if !self.handles.is_empty() {
|
||||
for path in shared.get_all_paths().iter() {
|
||||
if let Err(e) = shared.rtsp.remove_stream(&[path]) {
|
||||
return Err(anyhow!("Failed to shutdown RTSP Path {}: {:?}", path, e));
|
||||
}
|
||||
}
|
||||
|
||||
for (stream, handle) in self.handles.drain() {
|
||||
match handle.await {
|
||||
Ok(Err(e)) => return Err(e),
|
||||
@@ -154,50 +102,4 @@ impl Streaming {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn take_outputs(&mut self) -> Result<HashMap<Stream, GstOutputs>> {
|
||||
info!("Streaming take_outputs");
|
||||
self.abort_handle.abort();
|
||||
for (stream, handle) in self.handles.drain() {
|
||||
match handle.await {
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(_) => return Err(anyhow!("Panicked while streaming {:?}", stream)),
|
||||
Ok(Ok(_)) => {}
|
||||
}
|
||||
}
|
||||
let mut result: HashMap<_, _> = Default::default();
|
||||
for (stream, arc_mutex_output) in self.outputs.drain() {
|
||||
let mutex_output =
|
||||
Arc::try_unwrap(arc_mutex_output).map_err(|_| anyhow!("Failed to unwrap ARC"))?;
|
||||
let output = mutex_output.into_inner();
|
||||
result.insert(stream, output);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub(crate) fn insert_outputs(&mut self, mut input: HashMap<Stream, GstOutputs>) -> Result<()> {
|
||||
self.outputs = input
|
||||
.drain()
|
||||
.map(|(s, o)| (s, Arc::new(Mutex::new(o))))
|
||||
.collect();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn client_connected(&self) -> bool {
|
||||
for (_, output) in self.outputs.iter() {
|
||||
if output.lock().await.is_connected() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub(crate) async fn can_pause(&self) -> bool {
|
||||
for (_, output) in self.outputs.iter() {
|
||||
if !output.lock().await.has_last_iframe() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -95,7 +95,7 @@ impl AddressOrUid {
|
||||
|
||||
trace!("Camera Info: {:?}", options);
|
||||
|
||||
Ok(BcCamera::new(options).await?)
|
||||
Ok(BcCamera::new(&options).await?)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user