From 84b4c0659a6eddd47fab1127435c312738a4230d Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 1 Jul 2020 11:56:25 +0700 Subject: [PATCH] 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();