Use an Actor for the Stream watching
All checks were successful
Core Repos/ImageApi/pipeline/pr-master This commit looks good
All checks were successful
Core Repos/ImageApi/pipeline/pr-master This commit looks good
This commit is contained in:
55
src/main.rs
55
src/main.rs
@@ -5,23 +5,24 @@ extern crate rayon;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc};
|
||||
use std::sync::mpsc::channel;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use actix::{Actor, Addr};
|
||||
use actix_files::NamedFile;
|
||||
use actix_multipart as mp;
|
||||
use actix_web::{App, get, HttpServer, post, Responder, web};
|
||||
use actix_web::web::{HttpRequest, HttpResponse, Json};
|
||||
use actix_web::{get, post, web, App, HttpServer, Responder};
|
||||
use chrono::{Duration, Utc};
|
||||
use futures::stream::StreamExt;
|
||||
use jsonwebtoken::{encode, EncodingKey, Header};
|
||||
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
|
||||
use notify::{DebouncedEvent, RecursiveMode, watcher, Watcher};
|
||||
use rayon::prelude::*;
|
||||
use serde::Serialize;
|
||||
|
||||
use data::{AddFavoriteRequest, LoginRequest, ThumbnailRequest};
|
||||
|
||||
use crate::data::{secret_key, Claims, CreateAccountRequest, Token};
|
||||
use crate::data::{Claims, CreateAccountRequest, secret_key, Token};
|
||||
use crate::database::{add_favorite, create_user, get_favorites, get_user, user_exists};
|
||||
use crate::files::{is_valid_path, list_files};
|
||||
use crate::video::*;
|
||||
@@ -59,7 +60,7 @@ async fn login(creds: Json<LoginRequest>) -> impl Responder {
|
||||
&claims,
|
||||
&EncodingKey::from_secret(secret_key().as_bytes()),
|
||||
)
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
HttpResponse::Ok().json(Token { token: &token })
|
||||
} else {
|
||||
HttpResponse::NotFound().finish()
|
||||
@@ -185,12 +186,8 @@ async fn generate_video(
|
||||
let filename = name.to_str().expect("Filename should convert to string");
|
||||
let playlist = format!("tmp/{}.m3u8", filename);
|
||||
if let Some(path) = is_valid_path(&body.path) {
|
||||
if let Ok(mut stream) = data.stream_manager.lock() {
|
||||
if let Ok(child) = create_playlist(&path.to_str().unwrap(), &playlist) {
|
||||
stream.track_stream(&playlist, child)
|
||||
}
|
||||
} else {
|
||||
let _ = create_playlist(&path.to_str().unwrap(), &playlist);
|
||||
if let Ok(child) = create_playlist(&path.to_str().unwrap(), &playlist) {
|
||||
data.stream_manager.do_send(ProcessMessage(playlist.clone(), child));
|
||||
}
|
||||
} else {
|
||||
return HttpResponse::BadRequest().finish();
|
||||
@@ -259,7 +256,7 @@ async fn post_add_favorite(claims: Claims, body: web::Json<AddFavoriteRequest>)
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_thumbnails() {
|
||||
fn create_thumbnails() {
|
||||
let thumbs = &dotenv::var("THUMBNAILS").expect("THUMBNAILS not defined");
|
||||
let thumbnail_directory: &Path = Path::new(thumbs);
|
||||
|
||||
@@ -314,13 +311,10 @@ async fn create_thumbnails() {
|
||||
println!("Finished");
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
create_thumbnails().await;
|
||||
fn main() -> std::io::Result<()> {
|
||||
create_thumbnails();
|
||||
|
||||
let stream_manager = Arc::new(Mutex::new(VideoStreamManager::new()));
|
||||
let stream_manager_copy = stream_manager.clone();
|
||||
tokio::spawn(async move {
|
||||
std::thread::spawn(|| {
|
||||
let (wtx, wrx) = channel();
|
||||
let mut watcher = watcher(wtx, std::time::Duration::from_secs(10)).unwrap();
|
||||
watcher
|
||||
@@ -328,24 +322,24 @@ async fn main() -> std::io::Result<()> {
|
||||
.unwrap();
|
||||
|
||||
loop {
|
||||
if let Ok(mut manager) = stream_manager_copy.lock() {
|
||||
manager.check_for_finished_streams();
|
||||
}
|
||||
|
||||
let ev = wrx.recv_timeout(std::time::Duration::from_secs(10));
|
||||
if let Ok(event) = ev {
|
||||
match event {
|
||||
DebouncedEvent::Create(_) => create_thumbnails().await,
|
||||
DebouncedEvent::Rename(_, _) => create_thumbnails().await,
|
||||
DebouncedEvent::Create(_) => create_thumbnails(),
|
||||
DebouncedEvent::Rename(_, _) => create_thumbnails(),
|
||||
_ => continue,
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let system = actix::System::new("Fileserver");
|
||||
let act = StreamActor {}.start();
|
||||
|
||||
let app_data = web::Data::new(AppState {
|
||||
stream_manager: stream_manager.clone(),
|
||||
stream_manager: Arc::new(act),
|
||||
});
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.service(login)
|
||||
@@ -359,12 +353,13 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(post_add_favorite)
|
||||
.app_data(app_data.clone())
|
||||
})
|
||||
.bind(dotenv::var("BIND_URL").unwrap())?
|
||||
.bind("localhost:8088")?
|
||||
.run()
|
||||
.await
|
||||
.bind(dotenv::var("BIND_URL").unwrap())?
|
||||
.bind("localhost:8088")?
|
||||
.run();
|
||||
|
||||
system.run()
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
stream_manager: Arc<Mutex<VideoStreamManager>>,
|
||||
stream_manager: Arc<Addr<StreamActor>>,
|
||||
}
|
||||
|
||||
50
src/video.rs
50
src/video.rs
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user