AV1Master/src/main.rs

169 lines
5.1 KiB
Rust
Raw Normal View History

2020-03-24 22:21:29 +01:00
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
2020-03-25 22:30:22 +01:00
use rocket::State;
use rocket::response::status::NotFound;
use rocket_contrib::json::Json;
use serde_json::Value;
2020-03-26 21:55:13 +01:00
use serde_json::json;
use serde::{Serialize, Deserialize};
2020-03-26 18:33:46 +01:00
use rocket_contrib::uuid::Uuid;
2020-03-24 22:21:29 +01:00
2020-03-25 22:30:22 +01:00
use std::sync::Mutex;
2020-03-26 21:55:13 +01:00
use std::collections::HashMap;
2020-03-25 02:14:49 +01:00
mod workunit;
use workunit::WUnit;
2020-03-26 01:39:22 +01:00
use workunit::EStatus;
2020-03-25 02:14:49 +01:00
2020-03-25 22:30:22 +01:00
const VERSION: &str = "0.1.0";
#[derive(Default, Debug)]
struct SharedState {
2020-03-26 21:55:13 +01:00
jobs: Mutex<HashMap<uuid::Uuid, WUnit>>
2020-03-25 22:30:22 +01:00
}
2020-03-24 22:21:29 +01:00
#[get("/")]
fn index() -> &'static str {
2020-03-27 01:50:01 +01:00
r#"#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash curl jq libaom ffmpeg-full
set -euo pipefail
IFS=$'\n\t'
base_url="http://localhost:8000"
version="0.1.0"
while true; do
sleep 1
upsteam_version=`curl -s "$base_url"/version`
if [[ $version != $upsteam_version ]]; then
break
fi
job=`curl -s "$base_url"/request_job | jq`
if [[ $job = "null" ]]; then
echo "No Jobs Available ¯\_(ツ)_/¯"
continue
fi
echo "Got new job!"
echo "$job" | jq
job_id=`echo "$job" | jq -r .id`
echo "Reserving Job"
curl -s "$base_url"/edit_status/"$job_id"/reserved
echo ""
source=`echo $job | jq -r .description.file_url`
sourceext=${source##*.}
echo "Downloading source file: $source"
source=`echo $job | jq -r .description.file_url`
name=`echo $job | jq -r .description.file_name`
input="$name.$job_id.$sourceext"
curl "$source" -o "$input"
echo ""
echo "Starting Encode"
target_bitrate=`echo $job | jq -r .description.options.mode.VBR`
width=`echo $job | jq -r .description.options.resolution.width`
height=`echo $job | jq -r .description.options.resolution.height`
color_depth=`echo $job | jq -r .description.options.color_depth`
kf_min_dist=`echo $job | jq -r .description.options.kf_min_dist`
kf_max_dist=`echo $job | jq -r .description.options.kf_max_dist`
speed=`echo $job | jq -r .description.options.speed`
ffmpeg -i "$input" -vf scale=$width:$height -f yuv4mpegpipe - | aomenc - --lag-in-frames=25 --tile-columns=0 --tile-rows=0 --enable-fwd-kf=1 \
--target-bitrate=$target_bitrate --width="$width" --height="$height" --bit-depth=$color_depth --kf-min-dist=$kf_min_dist --kf-max-dist=$kf_min_dist \
--cpu-used=$speed \
--pass=1 --passes=2 --fpf="$input.$target_bitrate.$width.$height.$color_depth.fpf" --webm -o "$input.$target_bitrate.$width.$height.$color_depth.webm"
ffmpeg -i "$input" -vf scale=$width:$height -f yuv4mpegpipe - | aomenc - --lag-in-frames=25 --tile-columns=0 --tile-rows=0 --enable-fwd-kf=1 \
--target-bitrate=$target_bitrate --width="$width" --height="$height" --bit-depth=$color_depth --kf-min-dist=$kf_min_dist --kf-max-dist=$kf_min_dist \
--cpu-used=$speed \
--pass=2 --passes=2 --fpf="$input.$target_bitrate.$width.$height.$color_depth.fpf" --webm -o "$input.$target_bitrate.$width.$height.$color_depth.webm"
done
"#
2020-03-25 02:14:49 +01:00
}
2020-03-25 22:30:22 +01:00
#[get("/version")]
fn version() -> &'static str {
"0.1.0"
}
#[get("/get_jobs")]
2020-03-26 01:39:22 +01:00
fn get_jobs(shared: State<SharedState>) -> Json<Value> {
2020-03-26 21:55:13 +01:00
let list = shared.jobs.lock().unwrap().clone();
println!("{:#?}", list);
//Json(json!("god hlep me"))
Json(serde_json::to_value(&list).unwrap())
2020-03-26 01:39:22 +01:00
}
2020-03-25 22:30:22 +01:00
2020-03-26 01:39:22 +01:00
#[get("/request_job")]
fn request_job(shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
2020-03-26 21:55:13 +01:00
let mut list: Vec<WUnit> = shared.jobs.lock().unwrap()
2020-03-26 23:03:56 +01:00
.values()
.filter(|x| x.status == EStatus::Queued).cloned()
2020-03-26 21:55:13 +01:00
.collect();
2020-03-25 22:30:22 +01:00
2020-03-26 18:33:46 +01:00
list.sort_by(|a, b| b.description.length.cmp(&a.description.length));
2020-03-26 01:39:22 +01:00
let job = list.get(0);
Ok(Json(serde_json::to_value(&job).unwrap()))
2020-03-25 22:30:22 +01:00
}
#[get("/get_job/<id>")]
2020-03-26 18:33:46 +01:00
fn get_job(id: Uuid, shared: State<SharedState>) -> Result<Json<Value>, NotFound<String>> {
2020-03-26 21:55:13 +01:00
let list = shared.jobs.lock().unwrap();
2020-03-25 22:30:22 +01:00
2020-03-26 21:55:13 +01:00
let job = list.get(&id).ok_or(NotFound(format!("Job not Found: {id}", id = id)));
2020-03-25 22:30:22 +01:00
match job {
2020-03-26 01:39:22 +01:00
Ok(j) => Ok(Json(serde_json::to_value(&j).unwrap())),
2020-03-25 22:30:22 +01:00
Err(e) => Err(e)
}
}
2020-03-26 23:03:56 +01:00
#[get("/edit_status/<id>/<status>")]
fn edit_status(id: Uuid, status: String, shared: State<SharedState>) -> Result<String, Box<std::error::Error>> {
let mut list = shared.jobs.lock().unwrap();
let job = list.get_mut(&id).ok_or("what")?;
let status = match status.as_str() {
"queued" => Ok(EStatus::Queued),
"reserved" => Ok(EStatus::Reserved),
"completed" => Ok(EStatus::Completed),
_ => Err("Not a valid status, valid statuses are queued, reserved, completed")
}?;
job.status = status;
Ok("Status changed".to_string())
}
2020-03-26 18:33:46 +01:00
#[post("/add_job", format = "json", data = "<message>")]
2020-03-26 23:03:56 +01:00
fn add_job(message: Json<workunit::WDesc>, shared: State<SharedState>) {
2020-03-26 18:33:46 +01:00
println!("{:#?}", message);
let job = message.into_inner();
2020-03-26 21:55:13 +01:00
let id = uuid::Uuid::new_v4();
shared.jobs.lock().unwrap().insert(id, WUnit::new(id, job));
2020-03-24 22:21:29 +01:00
}
fn main() {
2020-03-26 01:39:22 +01:00
rocket::ignite()
2020-03-25 22:30:22 +01:00
.manage(SharedState::default())
2020-03-26 23:03:56 +01:00
.mount("/", routes![index, version, get_jobs, get_job, request_job, edit_status, add_job])
2020-03-25 22:30:22 +01:00
.launch();
2020-03-24 22:21:29 +01:00
}