From 826814abd76642debee5aefed0df0a100c11276a Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 30 Jun 2020 11:30:24 +0700 Subject: [PATCH 01/14] Allowing custom gst-launches and specification of format in the camera --- src/config.rs | 7 +++++++ src/gst.rs | 9 ++++----- src/main.rs | 14 +++++++++++++- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4173765..726ef08 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,8 +21,15 @@ pub struct CameraConfig { pub password: Option, pub timeout: Option, + + #[serde(default = "default_format")] + pub format: String, } fn default_bind_addr() -> String { "0.0.0.0".to_string() } + +fn default_format() -> String { + "h265".to_string() +} diff --git a/src/gst.rs b/src/gst.rs index f399f2e..ff6190a 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -26,7 +26,7 @@ impl RtspServer { } } - pub fn add_stream(&self, name: &str) -> Result { + pub fn add_stream(&self, name: &str, launch: &str) -> Result { let mounts = self .server .get_mount_points() @@ -34,11 +34,10 @@ impl RtspServer { let factory = RTSPMediaFactory::new(); //factory.set_protocols(RTSPLowerTrans::TCP); - factory.set_launch(concat!( + factory.set_launch(&format!("{}{}{}{}", "( ", - "appsrc name=writesrc is-live=true block=true emit-signals=false max-bytes=0", - " ! h265parse", - " ! rtph265pay name=pay0", + "appsrc name=writesrc is-live=true block=true emit-signals=false max-bytes=0 ", + launch, " )" )); factory.set_shared(true); diff --git a/src/main.rs b/src/main.rs index 61f15e2..fbe623a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,19 @@ fn main() -> Result<(), Error> { for camera in config.cameras { s.spawn(move |_| { // TODO handle these errors - let mut output = rtsp.add_stream(&camera.name).unwrap(); + let mut format :&str = &camera.format; + if format == "h265" { + format = "! h265parse ! rtph265pay name=pay0"; + } else if format == "h264" { + format = "! h264parse ! rtph264pay name=pay0"; + } else { + if format.trim().starts_with("!") { + warn!("Using custom format: {}.", format); + } else { + error!("Unrecognised format: {}.", format); + } + } + let mut output = rtsp.add_stream(&camera.name, &format).unwrap(); camera_loop(&camera, &mut output) }); } From 4ea20f05423d26622ff6f801e44dc0d551e983fc Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 30 Jun 2020 11:41:19 +0700 Subject: [PATCH 02/14] Allow stream specification in config --- src/bc_protocol.rs | 4 ++-- src/config.rs | 7 +++++++ src/main.rs | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index f1d61b8..e28817f 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -242,7 +242,7 @@ impl BcCamera { Ok(()) } - pub fn start_video(&self, data_out: &mut dyn Write) -> Result { + pub fn start_video(&self, data_out: &mut dyn Write, stream: &String) -> Result { let connection = self .connection .as_ref() @@ -261,7 +261,7 @@ impl BcCamera { version: xml_ver(), channel_id: 0, handle: 0, - stream_type: "mainStream".to_string(), + stream_type: stream.to_string(), }), ..Default::default() }, diff --git a/src/config.rs b/src/config.rs index 726ef08..99f7510 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,9 @@ pub struct CameraConfig { #[serde(default = "default_format")] pub format: String, + + #[serde(default = "default_stream")] + pub stream: String, } fn default_bind_addr() -> String { @@ -33,3 +36,7 @@ fn default_bind_addr() -> String { fn default_format() -> String { "h265".to_string() } + +fn default_stream() -> String { + "mainStream".to_string() +} diff --git a/src/main.rs b/src/main.rs index fbe623a..eb9148a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,7 +127,7 @@ fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result Date: Tue, 30 Jun 2020 11:48:58 +0700 Subject: [PATCH 03/14] Updated README with changes h264 and subStream instructions. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index bc54849..21f80e0 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ Copy and modify the `sample_config.toml` to specify the address, username, and p Each `[[cameras]]` block creates a new camera; the `name` determines the RTSP path you should connect your client to. Currently Neolink cannot auto-detect cameras like the official clients do; you must specify their IP addresses directly. +By default the h265 streaming format is used. Some cameras, for example E1, provide h264 streams, to use these you must specify `format = "h264"` in the `[[cameras]]` config. + +By default the HD stream is used. To swap to the SD stream add `stream = "subStream"` to the `[[cameras]]` config. You may also need to use `h264` format while using the SD stream. + By default Neolink serves on all IP addresses on port 8554. You can modify this by changing the `bind` parameter. From 83e25c86b256f410994139dd15801b3e3344e72f Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 30 Jun 2020 21:15:46 +0700 Subject: [PATCH 04/14] Info message when connecting to stream tells you which stream it is --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index eb9148a..b13986f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,8 +124,8 @@ fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result Date: Tue, 30 Jun 2020 21:16:33 +0700 Subject: [PATCH 05/14] Add timestamps to the stream --- src/gst.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gst.rs b/src/gst.rs index ff6190a..dc508ed 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -36,7 +36,7 @@ impl RtspServer { //factory.set_protocols(RTSPLowerTrans::TCP); factory.set_launch(&format!("{}{}{}{}", "( ", - "appsrc name=writesrc is-live=true block=true emit-signals=false max-bytes=0 ", + "appsrc name=writesrc is-live=true block=true emit-signals=false max-bytes=0 do-timestamp=true ", launch, " )" )); From 7ab104a4ac6b978faf5a78ed0335877e2b0bad4d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 1 Jul 2020 11:32:33 +0700 Subject: [PATCH 06/14] Renaming and type changes in sub stream selection --- src/bc_protocol.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index e28817f..ed2029b 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -242,7 +242,7 @@ impl BcCamera { Ok(()) } - pub fn start_video(&self, data_out: &mut dyn Write, stream: &String) -> Result { + pub fn start_video(&self, data_out: &mut dyn Write, stream_name: &str) -> Result { let connection = self .connection .as_ref() @@ -261,7 +261,7 @@ impl BcCamera { version: xml_ver(), channel_id: 0, handle: 0, - stream_type: stream.to_string(), + stream_type: stream_name.to_string(), }), ..Default::default() }, From c4e3745d2770b8ec27fe761e30451a210bf872bd Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 1 Jul 2020 11:34:19 +0700 Subject: [PATCH 07/14] Added validation to the config file --- Cargo.toml | 4 ++++ src/config.rs | 11 ++++++++++- src/main.rs | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef79e2f..05645c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,10 +17,12 @@ gstreamer = "0.15" gstreamer-app = "0.15" gstreamer-rtsp = "0.15" gstreamer-rtsp-server = "0.15" +lazy_static = "1.4" log = { version = "0.4" } indoc = "0.3" md5 = "0.7" nom = "5.1" +regex = "1" serde = { version = "1.0", features = ["derive"] } socket2 = "0.3" structopt = "0.3" @@ -28,3 +30,5 @@ toml = "0.5" yaserde = "0.3.16" yaserde_derive = "0.3.16" xml-rs = "0.8" +validator = "0.10" +validator_derive = "0.10" diff --git a/src/config.rs b/src/config.rs index 99f7510..7090573 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,13 @@ use serde::Deserialize; use std::net::SocketAddr; use std::time::Duration; +use validator::Validate; +use regex::Regex; + +lazy_static! { + static ref RE_STREAM_FORM: Regex = Regex::new(r"^([hH]26[45]|[ \t]*[!].*)$").unwrap(); + static ref RE_STREAM_SRC: Regex = Regex::new(r"^(mainStream|subStream)$").unwrap(); +} #[derive(Debug, Deserialize)] pub struct Config { @@ -10,7 +17,7 @@ pub struct Config { pub bind_addr: String, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Validate)] pub struct CameraConfig { pub name: String, @@ -22,9 +29,11 @@ pub struct CameraConfig { pub timeout: Option, + #[validate(regex(path = "RE_STREAM_FORM", message = "Incorrect stream format", code = "format"))] #[serde(default = "default_format")] pub format: String, + #[validate(regex(path = "RE_STREAM_SRC", message = "Incorrect stream source", code = "stream"))] #[serde(default = "default_stream")] pub stream: String, } diff --git a/src/main.rs b/src/main.rs index b13986f..5c32582 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,17 @@ +#[macro_use] extern crate validator_derive; +#[macro_use] extern crate lazy_static; + use env_logger::Env; use err_derive::Error; use log::*; use neolink::bc_protocol::BcCamera; -use neolink::gst::{MaybeAppSrc, RtspServer}; +use neolink::gst::{MaybeAppSrc, RtspServer, StreamFormat}; use neolink::Never; use std::fs; use std::io::Write; use std::time::Duration; use structopt::StructOpt; +use validator::Validate; mod cmdline; mod config; @@ -23,6 +27,8 @@ pub enum Error { ProtocolError(#[error(source)] neolink::Error), #[error(display = "I/O error")] IoError(#[error(source)] std::io::Error), + #[error(display = "Validation error")] + ValidationError(#[error(source)] validator::ValidationErrors), } fn main() -> Result<(), Error> { @@ -37,6 +43,13 @@ fn main() -> Result<(), Error> { let opt = Opt::from_args(); let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + for camera in &config.cameras { + match camera.validate() { + Ok(_) => (), + Err(e) => return Err(Error::ValidationError(e)), + }; + } + let rtsp = &RtspServer::new(); crossbeam::scope(|s| { From 3ba10ace5d20593889d5886a66168ca827e7b684 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 1 Jul 2020 11:34:58 +0700 Subject: [PATCH 08/14] Moved gst launch str to gst and using enums --- src/gst.rs | 16 ++++++++++++++-- src/main.rs | 20 +++++++------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/gst.rs b/src/gst.rs index dc508ed..51bea51 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -18,6 +18,12 @@ pub struct RtspServer { server: GstRTSPServer, } +pub enum StreamFormat { + H264, + H265, + Custom(String) +} + impl RtspServer { pub fn new() -> RtspServer { gstreamer::init().expect("Gstreamer should not explode"); @@ -26,18 +32,24 @@ impl RtspServer { } } - pub fn add_stream(&self, name: &str, launch: &str) -> Result { + pub fn add_stream(&self, name: &str, stream_format: &StreamFormat) -> Result { let mounts = self .server .get_mount_points() .expect("The server should have mountpoints"); + let launch_str = match stream_format { + StreamFormat::H264 => "! h264parse ! rtph264pay name=pay0", + StreamFormat::H265 => "! h265parse ! rtph265pay name=pay0", + StreamFormat::Custom(custom_format) => custom_format, + }; + let factory = RTSPMediaFactory::new(); //factory.set_protocols(RTSPLowerTrans::TCP); factory.set_launch(&format!("{}{}{}{}", "( ", "appsrc name=writesrc is-live=true block=true emit-signals=false max-bytes=0 do-timestamp=true ", - launch, + launch_str, " )" )); factory.set_shared(true); diff --git a/src/main.rs b/src/main.rs index 5c32582..4a125c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,19 +56,13 @@ fn main() -> Result<(), Error> { for camera in config.cameras { s.spawn(move |_| { // TODO handle these errors - let mut format :&str = &camera.format; - if format == "h265" { - format = "! h265parse ! rtph265pay name=pay0"; - } else if format == "h264" { - format = "! h264parse ! rtph264pay name=pay0"; - } else { - if format.trim().starts_with("!") { - warn!("Using custom format: {}.", format); - } else { - error!("Unrecognised format: {}.", format); - } - } - let mut output = rtsp.add_stream(&camera.name, &format).unwrap(); + let cam_format :&str = &camera.format; + let stream_format = match cam_format { + "h264"|"H264" => StreamFormat::H264, + "h265"|"H265" => StreamFormat::H265, + custom_format @ _ => StreamFormat::Custom(custom_format.to_string()) + }; + let mut output = rtsp.add_stream(&camera.name, &stream_format).unwrap(); camera_loop(&camera, &mut output) }); } From 84b4c0659a6eddd47fab1127435c312738a4230d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 1 Jul 2020 11:56:25 +0700 Subject: [PATCH 09/14] Added port specification --- README.md | 2 +- src/config.rs | 10 +++++++++- src/gst.rs | 4 ++-- src/main.rs | 6 +++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 21f80e0..33a7830 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ By default the h265 streaming format is used. Some cameras, for example E1, prov By default the HD stream is used. To swap to the SD stream add `stream = "subStream"` to the `[[cameras]]` config. You may also need to use `h264` format while using the SD stream. By default Neolink serves on all IP addresses on port 8554. -You can modify this by changing the `bind` parameter. +You can modify this by changing the `bind` and the `bind_port` parameter. You can change the Neolink log level by setting the `RUST_LOG` environment variable (not in the configuration file) to one of `error`, `warn`, `info`, `debug`, or `trace`: diff --git a/src/config.rs b/src/config.rs index 7090573..516d8d6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,12 +9,16 @@ lazy_static! { static ref RE_STREAM_SRC: Regex = Regex::new(r"^(mainStream|subStream)$").unwrap(); } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Validate)] pub struct Config { pub cameras: Vec, #[serde(rename = "bind", default = "default_bind_addr")] pub bind_addr: String, + + #[validate(range(min = 0, max = 65535, message = "Invalid port", code = "bind_port"))] + #[serde(default = "default_bind_port")] + pub bind_port: u16, } #[derive(Debug, Deserialize, Validate)] @@ -42,6 +46,10 @@ fn default_bind_addr() -> String { "0.0.0.0".to_string() } +fn default_bind_port() -> u16 { + 8554 +} + fn default_format() -> String { "h265".to_string() } diff --git a/src/gst.rs b/src/gst.rs index 51bea51..525ccfe 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -100,9 +100,9 @@ impl RtspServer { Ok(()) } - pub fn run(&self, bind_addr: &str) { + pub 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 self.server.attach(None); diff --git a/src/main.rs b/src/main.rs index 4a125c7..27ec0a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,10 @@ fn main() -> Result<(), Error> { let opt = Opt::from_args(); let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + match config.validate() { + Ok(_) => (), + Err(e) => return Err(Error::ValidationError(e)), + }; for camera in &config.cameras { match camera.validate() { Ok(_) => (), @@ -67,7 +71,7 @@ fn main() -> Result<(), Error> { }); } - rtsp.run(&config.bind_addr); + rtsp.run(&config.bind_addr, config.bind_port); }) .unwrap(); From 8d13f14bea71dfc5c495140e8ec5c938e610b5c4 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 1 Jul 2020 12:40:27 +0700 Subject: [PATCH 10/14] Add appropiate submounts --- src/config.rs | 9 +++++---- src/main.rs | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/config.rs b/src/config.rs index 516d8d6..230449f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,15 +1,16 @@ use serde::Deserialize; use std::net::SocketAddr; use std::time::Duration; +use std::clone::Clone; use validator::Validate; use regex::Regex; lazy_static! { static ref RE_STREAM_FORM: Regex = Regex::new(r"^([hH]26[45]|[ \t]*[!].*)$").unwrap(); - static ref RE_STREAM_SRC: Regex = Regex::new(r"^(mainStream|subStream)$").unwrap(); + static ref RE_STREAM_SRC: Regex = Regex::new(r"^(mainStream|subStream|both)$").unwrap(); } -#[derive(Debug, Deserialize, Validate)] +#[derive(Debug, Deserialize, Validate, Clone)] pub struct Config { pub cameras: Vec, @@ -21,7 +22,7 @@ pub struct Config { pub bind_port: u16, } -#[derive(Debug, Deserialize, Validate)] +#[derive(Debug, Deserialize, Validate, Clone)] pub struct CameraConfig { pub name: String, @@ -55,5 +56,5 @@ fn default_format() -> String { } fn default_stream() -> String { - "mainStream".to_string() + "both".to_string() } diff --git a/src/main.rs b/src/main.rs index 27ec0a9..9f74799 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ fn main() -> Result<(), Error> { ); let opt = Opt::from_args(); - let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + let mut config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; match config.validate() { Ok(_) => (), @@ -54,6 +54,32 @@ fn main() -> Result<(), Error> { }; } + // Setup auto sub streams, we do this by looping the cameras + // If the stream type is both we clone the config + // On one of the clones we set stream_source to "mainStream" on the other "subStream" + // We also change the mount name by appending "/mainStream" or "/subStream" + // On the original uncloned config we change the stream from "both" to "mainStream" and leave the mount name unchanged + let mut new_cam_configs = vec![]; + + for camera_config in &mut config.cameras { + if camera_config.stream == "both" { + let mut main_camera_config = camera_config.clone(); + let mut sub_camera_config = camera_config.clone(); + + camera_config.stream = "mainStream".to_string(); + + main_camera_config.stream = "mainStream".to_string(); + main_camera_config.name = format!("{}/{}", main_camera_config.name, main_camera_config.stream); + new_cam_configs.push(main_camera_config); + + sub_camera_config.stream = "subStream".to_string(); + sub_camera_config.name = format!("{}/{}", sub_camera_config.name, sub_camera_config.stream); + sub_camera_config.format = "h264".to_string(); // Assuming always H264 on subStream: TODO: Autodetect + new_cam_configs.push(sub_camera_config); + } + } + config.cameras.append(&mut new_cam_configs); + let rtsp = &RtspServer::new(); crossbeam::scope(|s| { From 698fc0841fcd7d41a24c074c24563efed769e581 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 2 Jul 2020 09:41:18 +0700 Subject: [PATCH 11/14] Updates to the readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 33a7830..b26233c 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Currently Neolink cannot auto-detect cameras like the official clients do; you m By default the h265 streaming format is used. Some cameras, for example E1, provide h264 streams, to use these you must specify `format = "h264"` in the `[[cameras]]` config. -By default the HD stream is used. To swap to the SD stream add `stream = "subStream"` to the `[[cameras]]` config. You may also need to use `h264` format while using the SD stream. +By default the HD stream is mounted at `name` and at `name/mainStream` and the SD stream is mounted at `name/subStream`. You can specify mounting only one by adding `stream = "subStream"` to the `[[cameras]]` config. You may also need to use `h264` format while using the SD stream. By default Neolink serves on all IP addresses on port 8554. You can modify this by changing the `bind` and the `bind_port` parameter. From 55555333ff9009dee1f36796dcd61b9f702a6bf7 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Thu, 2 Jul 2020 10:02:13 +0700 Subject: [PATCH 12/14] Do subvalidation on the config --- src/config.rs | 1 + src/main.rs | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 230449f..576ea99 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,7 @@ lazy_static! { #[derive(Debug, Deserialize, Validate, Clone)] pub struct Config { + #[validate] pub cameras: Vec, #[serde(rename = "bind", default = "default_bind_addr")] diff --git a/src/main.rs b/src/main.rs index 9f74799..ec7f9f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,12 +47,6 @@ fn main() -> Result<(), Error> { Ok(_) => (), Err(e) => return Err(Error::ValidationError(e)), }; - for camera in &config.cameras { - match camera.validate() { - Ok(_) => (), - Err(e) => return Err(Error::ValidationError(e)), - }; - } // Setup auto sub streams, we do this by looping the cameras // If the stream type is both we clone the config From 558331339447ef8385095f74445282f28c41a8ea Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Thu, 2 Jul 2020 08:44:03 -0500 Subject: [PATCH 13/14] Refactor creating multiple-path streams This way, we don't stream from the Reolink camera more than once, and simply create multiple Gstreamer mountpoints for the underlying factory. --- src/gst.rs | 6 +++-- src/main.rs | 67 +++++++++++++++++++++++------------------------------ 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/src/gst.rs b/src/gst.rs index 525ccfe..6c23509 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -32,7 +32,7 @@ impl RtspServer { } } - pub fn add_stream(&self, name: &str, stream_format: &StreamFormat) -> Result { + pub fn add_stream(&self, names: &[&str], stream_format: &StreamFormat) -> Result { let mounts = self .server .get_mount_points() @@ -77,7 +77,9 @@ impl RtspServer { let _ = tx.send(app_src); // Receiver may be dropped, don't panic if so }); - mounts.add_factory(&format!("/{}", name), &factory); + for name in names { + mounts.add_factory(&format!("/{}", name), &factory); + } Ok(maybe_app_src) } diff --git a/src/main.rs b/src/main.rs index ec7f9f1..d6cbc54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use neolink::gst::{MaybeAppSrc, RtspServer, StreamFormat}; use neolink::Never; use std::fs; use std::io::Write; +use std::sync::Arc; use std::time::Duration; use structopt::StructOpt; use validator::Validate; @@ -41,54 +42,44 @@ fn main() -> Result<(), Error> { ); let opt = Opt::from_args(); - let mut config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; match config.validate() { Ok(_) => (), Err(e) => return Err(Error::ValidationError(e)), }; - // Setup auto sub streams, we do this by looping the cameras - // If the stream type is both we clone the config - // On one of the clones we set stream_source to "mainStream" on the other "subStream" - // We also change the mount name by appending "/mainStream" or "/subStream" - // On the original uncloned config we change the stream from "both" to "mainStream" and leave the mount name unchanged - let mut new_cam_configs = vec![]; - - for camera_config in &mut config.cameras { - if camera_config.stream == "both" { - let mut main_camera_config = camera_config.clone(); - let mut sub_camera_config = camera_config.clone(); - - camera_config.stream = "mainStream".to_string(); - - main_camera_config.stream = "mainStream".to_string(); - main_camera_config.name = format!("{}/{}", main_camera_config.name, main_camera_config.stream); - new_cam_configs.push(main_camera_config); - - sub_camera_config.stream = "subStream".to_string(); - sub_camera_config.name = format!("{}/{}", sub_camera_config.name, sub_camera_config.stream); - sub_camera_config.format = "h264".to_string(); // Assuming always H264 on subStream: TODO: Autodetect - new_cam_configs.push(sub_camera_config); - } - } - config.cameras.append(&mut new_cam_configs); - let rtsp = &RtspServer::new(); crossbeam::scope(|s| { for camera in config.cameras { - s.spawn(move |_| { - // TODO handle these errors - let cam_format :&str = &camera.format; - let stream_format = match cam_format { - "h264"|"H264" => StreamFormat::H264, - "h265"|"H265" => StreamFormat::H265, - custom_format @ _ => StreamFormat::Custom(custom_format.to_string()) - }; - let mut output = rtsp.add_stream(&camera.name, &stream_format).unwrap(); - camera_loop(&camera, &mut output) - }); + let stream_format = match &*camera.format { + "h264"|"H264" => StreamFormat::H264, + "h265"|"H265" => StreamFormat::H265, + custom_format @ _ => StreamFormat::Custom(custom_format.to_string()) + }; + + // Let subthreads share the camera object; in principle I think they could share + // the object as it sits in the config.cameras block, but I have not figured out the + // syntax for that. + let arc_cam = Arc::new(camera); + + // Set up each main and substream according to all the RTSP mount paths we support + if arc_cam.stream == "both" || arc_cam.stream == "mainStream" { + let paths = &[ + &arc_cam.name, + &*format!("{}/mainStream", arc_cam.name), + ]; + let mut output = rtsp.add_stream(paths, &stream_format).unwrap(); + let main_camera = arc_cam.clone(); + s.spawn(move |_| camera_loop(&*main_camera, &mut output)); + } + if arc_cam.stream == "both" || arc_cam.stream == "subStream" { + let paths = &[&*format!("{}/subStream", arc_cam.name)]; + let mut output = rtsp.add_stream(paths, &stream_format).unwrap(); + let sub_camera = arc_cam.clone(); + s.spawn(move |_| camera_loop(&*sub_camera, &mut output)); + } } rtsp.run(&config.bind_addr, config.bind_port); From a562dcdba28a1b7f7571d500cfd8ad411d615b7e Mon Sep 17 00:00:00 2001 From: George Hilliard Date: Thu, 2 Jul 2020 08:49:00 -0500 Subject: [PATCH 14/14] Reword readme --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b26233c..aabf432 100644 --- a/README.md +++ b/README.md @@ -63,9 +63,12 @@ Copy and modify the `sample_config.toml` to specify the address, username, and p Each `[[cameras]]` block creates a new camera; the `name` determines the RTSP path you should connect your client to. Currently Neolink cannot auto-detect cameras like the official clients do; you must specify their IP addresses directly. -By default the h265 streaming format is used. Some cameras, for example E1, provide h264 streams, to use these you must specify `format = "h264"` in the `[[cameras]]` config. +By default the H265 video format is used. Some cameras, for example E1, provide H264 streams. To use these you must specify `format = "h264"` in the `[[cameras]]` config. +Soon this will be auto-detected, and you will not have to know or care about the format. -By default the HD stream is mounted at `name` and at `name/mainStream` and the SD stream is mounted at `name/subStream`. You can specify mounting only one by adding `stream = "subStream"` to the `[[cameras]]` config. You may also need to use `h264` format while using the SD stream. +By default, the HD stream is available at the RTSP path `/name` or `/name/mainStream`, and the SD stream is available at `/name/subStream`. +You can disable the HD stream by adding `stream = "subStream"` to the `[[cameras]]` config. +You may also need to use `h264` format while using the SD stream. By default Neolink serves on all IP addresses on port 8554. You can modify this by changing the `bind` and the `bind_port` parameter.