From 10d0b6f86c4e6fb2f47290556440483eb972d533 Mon Sep 17 00:00:00 2001 From: Valmo Trindade Date: Mon, 7 Apr 2025 01:28:10 -0300 Subject: [PATCH] separed video stream oss in scopes --- src/video_stream.rs | 81 ++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/src/video_stream.rs b/src/video_stream.rs index 1ea7f4a..ba5ffdf 100644 --- a/src/video_stream.rs +++ b/src/video_stream.rs @@ -25,65 +25,56 @@ pub fn start_stream( ) -> &'static str { #[cfg(target_os = "linux")] { - return ctx.callback_null( + ctx.callback_null( "armatak_video_error", "Screen capture is only supported on Windows", ); } - let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel(); - let rtsp_url = format!( - "rtsp://{}:{}@{}:{}/{}", - username, password, address, port, stream_path - ); - let rtsp_url_clone = rtsp_url.clone(); + #[cfg(target_os = "windows")] + { + let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel(); + let rtsp_url = format!( + "rtsp://{}:{}@{}:{}/{}", + username, password, address, port, stream_path + ); + let rtsp_url_clone = rtsp_url.clone(); - thread::spawn(move || { - let mut cmd = Command::new("ffmpeg"); + thread::spawn(move || { + let mut cmd = Command::new("ffmpeg"); - cmd.args(&[ - "-f", - "gdigrab", - "-i", - "desktop", - "-f", - "rtsp", - "-rtsp_transport", - "tcp", - &rtsp_url_clone, - ]); + cmd.args(&[ + "-f", + "gdigrab", + "-i", + "desktop", + "-f", + "rtsp", + "-rtsp_transport", + "tcp", + &rtsp_url_clone, + ]); - #[cfg(target_os = "windows")] - let mut child = match cmd.creation_flags(CREATE_NO_WINDOW).spawn() { - Ok(child) => child, - Err(e) => { + let mut child = cmd.creation_flags(CREATE_NO_WINDOW).spawn().unwrap(); + + if rx.recv().is_err() { + let _ = ctx.callback_null("armatak_video_error", "Error receiving stop signal"); + } + + if let Err(e) = child.kill() { let _ = ctx.callback_data( "armatak_video_error", - "Failed to Start FFmpeg", + "Failed to Stop FFmpeg", e.to_string(), ); - return; } - }; + }); - if rx.recv().is_err() { - let _ = ctx.callback_null("armatak_video_error", "Error receiving stop signal"); - } - - #[cfg(target_os = "windows")] - if let Err(e) = child.kill() { - let _ = ctx.callback_data( - "armatak_video_error", - "Failed to Stop FFmpeg", - e.to_string(), - ); - } - }); - - match STREAM_CTRL.lock() { - Ok(mut lock) => *lock = Some(tx), - Err(e) => { - eprintln!("Failed to acquire lock: {}", e); + match STREAM_CTRL.lock() { + Ok(mut lock) => *lock = Some(tx), + Err(e) => { + eprintln!("Failed to acquire lock: {}", e); + } } }