From d2c72d56fe0cc1cced14f8d1d1746f5224011499 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Sun, 13 Aug 2023 12:59:20 +0200 Subject: [PATCH] stop decoder with SIGTERM signal, instead of kill on non windows systems --- Cargo.lock | 7 +++++++ ffplayout-engine/src/output/mod.rs | 11 ++--------- lib/Cargo.toml | 3 +++ lib/src/utils/controller.rs | 13 ++++++++++--- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5540f905..ea21271f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1191,6 +1191,7 @@ dependencies = [ "serde_json", "serde_yaml", "shlex", + "signal-child", "simplelog", "time 0.3.23", "walkdir", @@ -2817,6 +2818,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +[[package]] +name = "signal-child" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a4eed4c5ae38438470ab8e0108bb751012f786f44ff585cfd837c9a5fe426f" + [[package]] name = "signal-hook-registry" version = "1.4.1" diff --git a/ffplayout-engine/src/output/mod.rs b/ffplayout-engine/src/output/mod.rs index 1b17a247..c34634d1 100644 --- a/ffplayout-engine/src/output/mod.rs +++ b/ffplayout-engine/src/output/mod.rs @@ -159,15 +159,11 @@ pub fn player( thread::spawn(move || stderr_reader(dec_err, Decoder, dec_p_ctl)); loop { - // when server is running, read from channel + // when server is running, read from it if proc_control.server_is_running.load(Ordering::SeqCst) { if !live_on { info!("Switch from {} to live ingest", config.processing.mode); - if let Err(e) = enc_writer.flush() { - error!("Encoder error: {e}") - } - if let Err(e) = proc_control.stop(Decoder) { error!("{e}") } @@ -188,11 +184,8 @@ pub fn player( if live_on { info!("Switch from live ingest to {}", config.processing.mode); - if let Err(e) = enc_writer.flush() { - error!("Encoder error: {e}") - } - live_on = false; + break; } let dec_bytes_len = match dec_reader.read(&mut buffer[..]) { diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 5319df87..83f361eb 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -32,3 +32,6 @@ features = ["shlobj", "std", "winerror"] [target.x86_64-unknown-linux-musl.dependencies] openssl = { version = "0.10", features = ["vendored"] } + +[target.'cfg(not(target_arch = "windows"))'.dependencies] +signal-child = "1" diff --git a/lib/src/utils/controller.rs b/lib/src/utils/controller.rs index 6c06ce33..a1a31714 100644 --- a/lib/src/utils/controller.rs +++ b/lib/src/utils/controller.rs @@ -1,5 +1,5 @@ use std::{ - fmt, + env, fmt, process::Child, sync::{ atomic::{AtomicBool, AtomicUsize, Ordering}, @@ -7,6 +7,9 @@ use std::{ }, }; +#[cfg(not(windows))] +use signal_child::Signalable; + use serde::{Deserialize, Serialize}; use simplelog::*; @@ -71,9 +74,13 @@ impl ProcessControl { match unit { Decoder => { if let Some(proc) = self.decoder_term.lock().unwrap().as_mut() { - if let Err(e) = proc.kill() { + if env::consts::OS != "windows" { + if let Err(e) = proc.term() { + return Err(format!("Decoder {e:?}")); + } + } else if let Err(e) = proc.kill() { return Err(format!("Decoder {e:?}")); - }; + } } } Encoder => {