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) }); }