-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
101 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
use std::{error::Error, process::Stdio, sync::Arc, time::Duration}; | ||
use std::{error::Error, process::Stdio, time::Duration}; | ||
|
||
use tokio::{ | ||
process::{Child, Command}, | ||
sync::{watch::Receiver, RwLock}, | ||
sync::watch::Receiver, | ||
}; | ||
use tokio_util::sync::CancellationToken; | ||
use tracing::{info, warn}; | ||
|
@@ -23,26 +23,20 @@ pub async fn run_save_pipeline( | |
// ffmpeg -f video4linux2 -input_format mjpeg -s 1280x720 -i /dev/video0 -vf "drawtext=fontfile=FreeSerif.tff: \ | ||
//text='%{localtime\:%T}': [email protected]: x=7: y=700" -vcodec libx264 -preset veryfast -f mp4 -pix_fmt yuv420p -y output.mp4 | ||
|
||
//let mut res: Option<Child> = None; | ||
|
||
let cmd: Arc<RwLock<Child>> = Arc::new(RwLock::new( | ||
Command::new("sleep") | ||
.args(["2147483647"]) | ||
.stdin(Stdio::null()) | ||
.spawn()?, | ||
)); | ||
|
||
//let mut first_run = false; | ||
let mut cmd: Option<Child> = None; | ||
|
||
loop { | ||
tokio::select! { | ||
_ = cancel_token.cancelled() => { | ||
info!("Ffmpeg canceled"); | ||
(*cmd.write().await).wait().await.unwrap(); | ||
if cmd.as_ref().is_some() { | ||
cmd.unwrap().wait().await.unwrap(); | ||
} | ||
return Ok(()) | ||
}, | ||
new = hv_stat_recv.changed() => { | ||
new?; | ||
|
||
let curr_data = *hv_stat_recv.borrow_and_update(); | ||
match curr_data { | ||
HVTransition::TransitionOn(hvon_data) => { | ||
|
@@ -74,11 +68,15 @@ pub async fn run_save_pipeline( | |
"-y", | ||
&save_location | ||
]).stdin(Stdio::null()).spawn()?; | ||
*cmd.write().await = cmd_new; | ||
cmd = Some(cmd_new); | ||
|
||
}, | ||
HVTransition::TransitionOff => { | ||
let proc_id = cmd.read().await.id().unwrap_or_default(); | ||
if cmd.is_none() { | ||
continue; | ||
} | ||
if let Some(val) = cmd.as_mut() { | ||
let proc_id = val.id().unwrap_or_default(); | ||
// logic to safely shutdown since ffmpeg doesnt capture our fake ctrl+c from mqtt | ||
// first try and run a SIGTERM kill | ||
if let Ok(mut child) = Command::new("kill").args(["-SIGTERM".to_string(), proc_id.to_string(), ]).spawn() { | ||
|
@@ -92,13 +90,15 @@ pub async fn run_save_pipeline( | |
let mut tics_cnt = 0; | ||
while tics_cnt < 12 { | ||
tokio::time::sleep(Duration::from_secs(1)).await; | ||
if (*cmd.write().await).try_wait().is_ok_and(|f| f.is_some()){ | ||
if val.try_wait().is_ok_and(|f| f.is_some()){ | ||
break; | ||
} | ||
tics_cnt += 1; | ||
} | ||
// finally as a last sanity check kill the ffmpeg process, as worst case is leaving it dangling | ||
let _ = (*cmd.write().await).kill().await; | ||
let _ = val.kill().await; | ||
} | ||
|
||
}, | ||
} | ||
}, | ||
|