diff --git a/core/src/ffmpeg_linux.rs b/core/src/ffmpeg_linux.rs index 2ddf76d..a692ada 100644 --- a/core/src/ffmpeg_linux.rs +++ b/core/src/ffmpeg_linux.rs @@ -138,6 +138,7 @@ impl Ffmpeg { }, ]); ffmpeg_command.overwrite(); + ffmpeg_command.print_command(); // Sleep for delay sleep(Duration::from_secs(self.record_delay as u64)); @@ -356,4 +357,42 @@ impl Ffmpeg { } Ok(()) } + + // Kill process + pub fn kill(&mut self) -> Result<()> { + if self.video_process.is_some() { + std::process::Command::new("kill") + .arg(format!( + "{}", + self.video_process + .clone() + .ok_or_else(|| anyhow!("Unable to kill the video recording process successfully."))? + .borrow_mut() + .as_inner().id() + )).output()?; + } + if self.input_audio_process.is_some() { + std::process::Command::new("kill") + .arg(format!( + "{}", + self.input_audio_process + .clone() + .ok_or_else(|| anyhow!("Unable to kill the intput audio recording process successfully."))? + .borrow_mut() + .as_inner().id() + )).output()?; + } + if self.output_audio_process.is_some() { + std::process::Command::new("kill") + .arg(format!( + "{}", + self.output_audio_process + .clone() + .ok_or_else(|| anyhow!("Unable to kill the output audio recording process successfully."))? + .borrow_mut() + .as_inner().id() + )).output()?; + } + Ok(()) + } } diff --git a/core/src/ffmpeg_windows.rs b/core/src/ffmpeg_windows.rs index b0201ee..69dfba5 100644 --- a/core/src/ffmpeg_windows.rs +++ b/core/src/ffmpeg_windows.rs @@ -343,4 +343,45 @@ impl Ffmpeg { } Ok(()) } + + // Kill process + pub fn kill(&mut self) -> Result<()> { + if self.video_process.is_some() { + let pid = self.video_process + .clone() + .ok_or_else(|| anyhow!("Unable to kill the video recording process successfully."))? + .borrow_mut() + .as_inner().id(); + std::process::Command::new("taskkill") + .arg("/PID") + .arg(pid.to_string()) + .arg("/F") + .output()?; + } + if self.input_audio_process.is_some() { + let pid = self.input_audio_process + .clone() + .ok_or_else(|| anyhow!("Unable to kill the input audio recording process successfully."))? + .borrow_mut() + .as_inner().id(); + std::process::Command::new("taskkill") + .arg("/PID") + .arg(pid.to_string()) + .arg("/F") + .output()?; + } + if self.output_audio_process.is_some() { + let pid = self.output_audio_process + .clone() + .ok_or_else(|| anyhow!("Unable to kill the output audio recording process successfully."))? + .borrow_mut() + .as_inner().id(); + std::process::Command::new("taskkill") + .arg("/PID") + .arg(pid.to_string()) + .arg("/F") + .output()?; + } + Ok(()) + } } diff --git a/gui/src/ui.rs b/gui/src/ui.rs index 8b62064..17217d9 100644 --- a/gui/src/ui.rs +++ b/gui/src/ui.rs @@ -918,6 +918,7 @@ fn build_ui(application: &Application, error_dialog: MessageDialog, error_messag // Do nothing }, Err(error) => { + _ffmpeg_record_interface.borrow_mut().kill().unwrap(); _audio_input_switch.set_sensitive(true); _audio_output_switch.set_sensitive(true); _video_switch.set_sensitive(true); @@ -936,6 +937,7 @@ fn build_ui(application: &Application, error_dialog: MessageDialog, error_messag // Do nothing }, Err(error) => { + _ffmpeg_record_interface.borrow_mut().kill().unwrap(); _audio_input_switch.set_sensitive(true); _audio_output_switch.set_sensitive(true); _video_switch.set_sensitive(true); @@ -954,6 +956,7 @@ fn build_ui(application: &Application, error_dialog: MessageDialog, error_messag // Do nothing }, Err(error) => { + _ffmpeg_record_interface.borrow_mut().kill().unwrap(); _audio_input_switch.set_sensitive(true); _audio_output_switch.set_sensitive(true); _video_switch.set_sensitive(true); @@ -1086,42 +1089,7 @@ fn build_ui(application: &Application, error_dialog: MessageDialog, error_messag // Stop recording before close the application let mut _ffmpeg_record_interface = ffmpeg_record_interface.clone(); main_window.connect_close_request(move |main_window| { - if _ffmpeg_record_interface.borrow_mut().video_process.is_some() { - std::process::Command::new("kill") - .arg(format!( - "{}", - _ffmpeg_record_interface.borrow_mut() - .video_process - .clone() - .unwrap() - .borrow_mut() - .as_inner().id() - )).output().unwrap(); - } - if _ffmpeg_record_interface.borrow_mut().input_audio_process.is_some() { - std::process::Command::new("kill") - .arg(format!( - "{}", - _ffmpeg_record_interface.borrow_mut() - .input_audio_process - .clone() - .unwrap() - .borrow_mut() - .as_inner().id() - )).output().unwrap(); - } - if _ffmpeg_record_interface.borrow_mut().output_audio_process.is_some() { - std::process::Command::new("kill") - .arg(format!( - "{}", - _ffmpeg_record_interface.borrow_mut() - .output_audio_process - .clone() - .unwrap() - .borrow_mut() - .as_inner().id() - )).output().unwrap(); - } + _ffmpeg_record_interface.borrow_mut().kill().unwrap(); main_window.destroy(); adw::gtk::Inhibit(true) });