Use an Actor for the Stream watching
All checks were successful
Core Repos/ImageApi/pipeline/pr-master This commit looks good

This commit is contained in:
Cameron Cordes
2021-02-11 20:39:07 -05:00
parent 45b4f0cd72
commit 11d1e9600a
4 changed files with 125 additions and 59 deletions

View File

@@ -1,36 +1,40 @@
use std::collections::HashMap;
use std::io::Result;
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::process::{Child, Command, ExitStatus, Stdio};
use actix::prelude::*;
// ffmpeg -i test.mp4 -c:v h264 -flags +cgop -g 30 -hls_time 3 out.m3u8
// ffmpeg -i "filename.mp4" -preset veryfast -c:v libx264 -f hls -hls_list_size 100 -hls_time 2 -crf 24 -vf scale=1080:-2,setsar=1:1 attempt/vid_out.m3u8
pub(crate) struct VideoStreamManager {
streams: HashMap<String, Child>,
pub struct StreamActor;
impl Actor for StreamActor {
type Context = Context<Self>;
}
impl VideoStreamManager {
pub(crate) fn new() -> VideoStreamManager {
VideoStreamManager {
streams: HashMap::new(),
}
}
pub struct ProcessMessage(pub String, pub Child);
pub fn track_stream(&mut self, playlist_path: &dyn ToString, child_process: Child) {
println!("Tracking process for: {:?}", playlist_path.to_string());
self.streams
.insert(playlist_path.to_string(), child_process);
}
impl Message for ProcessMessage {
type Result = Result<ExitStatus>;
}
pub fn check_for_finished_streams(&mut self) {
self.streams.retain(|playlist_path, process| {
let is_done = process.try_wait().map_or(false, |status| status.is_some());
if is_done {
println!("Removing process: {:?}", playlist_path)
}
!is_done
});
impl Handler<ProcessMessage> for StreamActor {
type Result = Result<ExitStatus>;
fn handle(&mut self, msg: ProcessMessage, _ctx: &mut Self::Context) -> Self::Result {
println!("Message received");
let mut process = msg.1;
let result = process.wait();
println!(
"Finished waiting for: {:?}. Code: {:?}",
msg.0,
result
.as_ref()
.map_or(-1, |status| status.code().unwrap_or(-1))
);
result
}
}