correct starting and ending from processes and threads

This commit is contained in:
jb-alvarado 2022-03-01 21:07:54 +01:00
parent f34099d230
commit 67bfa3af15

View File

@ -6,7 +6,7 @@ use std::{
process::{Command, Stdio}, process::{Command, Stdio},
sync::{ sync::{
mpsc::channel, mpsc::channel,
{Arc, Mutex}, Arc, Mutex,
}, },
thread::sleep, thread::sleep,
time::Duration, time::Duration,
@ -19,8 +19,12 @@ use simplelog::*;
use crate::utils::{sec_to_time, watch_folder, Config, CurrentProgram, Media, Source}; use crate::utils::{sec_to_time, watch_folder, Config, CurrentProgram, Media, Source};
pub fn play(config: Config) { pub fn play(config: Config) {
let stop = Arc::new(Mutex::new(false));
let dec_pid: Arc<Mutex<u32>> = Arc::new(Mutex::new(0)); let dec_pid: Arc<Mutex<u32>> = Arc::new(Mutex::new(0));
let runtime = Builder::new_multi_thread()
.worker_threads(1)
.enable_all()
.build()
.unwrap();
let get_source = match config.processing.mode.clone().as_str() { let get_source = match config.processing.mode.clone().as_str() {
"folder" => { "folder" => {
@ -30,29 +34,27 @@ pub fn play(config: Config) {
process::exit(0x0100); process::exit(0x0100);
} }
let runtime = Builder::new_multi_thread() info!("Playout in folder mode.");
.worker_threads(1)
.enable_all()
.build()
.unwrap();
let folder_source = Source::new(config.clone()); let folder_source = Source::new(config.clone());
let (sender, receiver) = channel(); let (sender, receiver) = channel();
let mut watcher = watcher(sender, Duration::from_secs(2)).unwrap(); let mut watcher = watcher(sender, Duration::from_secs(2)).unwrap();
watcher watcher
.watch(path.clone(), RecursiveMode::Recursive) .watch(path.clone(), RecursiveMode::Recursive)
.unwrap(); .unwrap();
debug!("Monitor folder: <b><magenta>{}</></b>", path);
runtime.spawn(watch_folder( runtime.spawn(watch_folder(
receiver, receiver,
Arc::clone(&stop),
Arc::clone(&folder_source.nodes), Arc::clone(&folder_source.nodes),
)); ));
Box::new(folder_source) as Box<dyn Iterator<Item = Media>> Box::new(folder_source) as Box<dyn Iterator<Item = Media>>
} }
"playlist" => { "playlist" => {
info!("Playout in playlist mode.");
Box::new(CurrentProgram::new(config.clone())) as Box<dyn Iterator<Item = Media>> Box::new(CurrentProgram::new(config.clone())) as Box<dyn Iterator<Item = Media>>
} }
_ => { _ => {
@ -92,7 +94,7 @@ pub fn play(config: Config) {
let mut enc_proc = match Command::new("ffplay") let mut enc_proc = match Command::new("ffplay")
.args(enc_cmd) .args(enc_cmd)
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stderr(Stdio::piped()) // .stderr(Stdio::piped())
.spawn() .spawn()
{ {
Err(e) => { Err(e) => {
@ -104,13 +106,17 @@ pub fn play(config: Config) {
for node in get_source { for node in get_source {
// println!("Node begin: {:?}", sec_to_time(node.begin.unwrap())); // println!("Node begin: {:?}", sec_to_time(node.begin.unwrap()));
let cmd = match node.cmd {
Some(cmd) => cmd,
None => break
};
info!( info!(
"Play for <yellow>{}</>: <b><magenta>{}</></b>", "Play for <yellow>{}</>: <b><magenta>{}</></b>",
sec_to_time(node.out - node.seek), sec_to_time(node.out - node.seek),
node.source node.source
); );
let cmd = node.cmd.unwrap();
let filter = node.filter.unwrap(); let filter = node.filter.unwrap();
let mut dec_cmd = vec!["-v", ff_log_format.as_str(), "-hide_banner", "-nostats"]; let mut dec_cmd = vec!["-v", ff_log_format.as_str(), "-hide_banner", "-nostats"];
@ -127,7 +133,7 @@ pub fn play(config: Config) {
let mut dec_proc = match Command::new("ffmpeg") let mut dec_proc = match Command::new("ffmpeg")
.args(dec_cmd) .args(dec_cmd)
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()) // .stderr(Stdio::piped())
.spawn() .spawn()
{ {
Err(e) => { Err(e) => {
@ -160,16 +166,14 @@ pub fn play(config: Config) {
} }
if let Err(e) = dec_proc.wait() { if let Err(e) = dec_proc.wait() {
panic!("Enc error: {:?}", e) panic!("Decoder error: {:?}", e)
}; };
} }
*stop.lock().unwrap() = true;
sleep(Duration::from_secs(1)); sleep(Duration::from_secs(1));
match enc_proc.kill() { match enc_proc.kill() {
Ok(_) => info!("Playout done..."), Ok(_) => info!("Playout done..."),
Err(e) => panic!("Enc error: {:?}", e), Err(e) => panic!("Encoder error: {:?}", e),
} }
} }