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