2022-04-10 22:05:19 +02:00
|
|
|
extern crate log;
|
|
|
|
extern crate simplelog;
|
|
|
|
|
2022-04-07 15:01:09 +02:00
|
|
|
use std::{
|
|
|
|
path::PathBuf,
|
2022-04-13 17:40:47 +02:00
|
|
|
process::exit,
|
2022-04-07 15:01:09 +02:00
|
|
|
{fs, fs::File},
|
|
|
|
};
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::json;
|
2022-04-05 17:07:34 +02:00
|
|
|
use simplelog::*;
|
|
|
|
use tokio::runtime::Builder;
|
2022-03-28 15:52:03 +02:00
|
|
|
|
2022-02-20 22:08:17 +01:00
|
|
|
mod filter;
|
2022-03-18 17:04:43 +01:00
|
|
|
mod input;
|
2022-02-16 18:27:03 +01:00
|
|
|
mod output;
|
2022-04-08 12:18:29 +02:00
|
|
|
mod rpc;
|
2022-02-12 22:32:51 +01:00
|
|
|
mod utils;
|
2022-02-16 18:27:03 +01:00
|
|
|
|
2022-03-31 17:36:10 +02:00
|
|
|
use crate::output::{player, write_hls};
|
2022-04-06 18:10:57 +02:00
|
|
|
use crate::utils::{
|
2022-04-13 17:40:47 +02:00
|
|
|
generate_playlist, init_config, init_logging, validate_ffmpeg, GlobalConfig, PlayerControl,
|
|
|
|
PlayoutStatus, ProcessControl,
|
2022-04-06 18:10:57 +02:00
|
|
|
};
|
2022-04-08 12:18:29 +02:00
|
|
|
use rpc::json_rpc_server;
|
2022-02-12 22:32:51 +01:00
|
|
|
|
2022-04-07 15:01:09 +02:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct StatusData {
|
|
|
|
time_shift: f64,
|
|
|
|
date: String,
|
|
|
|
}
|
|
|
|
|
2022-02-12 22:32:51 +01:00
|
|
|
fn main() {
|
2022-03-13 22:18:53 +01:00
|
|
|
init_config();
|
2022-03-31 17:36:10 +02:00
|
|
|
let config = GlobalConfig::global();
|
2022-04-06 18:10:57 +02:00
|
|
|
let play_control = PlayerControl::new();
|
2022-04-07 15:01:09 +02:00
|
|
|
let playout_stat = PlayoutStatus::new();
|
2022-04-05 17:07:34 +02:00
|
|
|
let proc_control = ProcessControl::new();
|
2022-03-16 20:12:12 +01:00
|
|
|
|
2022-04-07 15:01:09 +02:00
|
|
|
if !PathBuf::from(config.general.stat_file.clone()).exists() {
|
|
|
|
let data = json!({
|
|
|
|
"time_shift": 0.0,
|
2022-04-07 17:44:46 +02:00
|
|
|
"date": String::new(),
|
2022-04-07 15:01:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let json: String = serde_json::to_string(&data).expect("Serialize status data failed");
|
|
|
|
fs::write(config.general.stat_file.clone(), &json).expect("Unable to write file");
|
|
|
|
} else {
|
|
|
|
let stat_file = File::options()
|
|
|
|
.read(true)
|
|
|
|
.write(false)
|
|
|
|
.open(&config.general.stat_file)
|
|
|
|
.expect("Could not open status file");
|
|
|
|
|
|
|
|
let data: StatusData =
|
|
|
|
serde_json::from_reader(stat_file).expect("Could not read status file.");
|
|
|
|
|
|
|
|
*playout_stat.time_shift.lock().unwrap() = data.time_shift;
|
|
|
|
*playout_stat.date.lock().unwrap() = data.date;
|
|
|
|
}
|
|
|
|
|
2022-03-31 17:36:10 +02:00
|
|
|
let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
|
2022-03-16 20:12:12 +01:00
|
|
|
let rt_handle = runtime.handle();
|
|
|
|
|
2022-04-05 17:07:34 +02:00
|
|
|
let logging = init_logging(rt_handle.clone(), proc_control.is_terminated.clone());
|
2022-02-25 16:22:29 +01:00
|
|
|
CombinedLogger::init(logging).unwrap();
|
2022-02-23 18:06:40 +01:00
|
|
|
|
2022-03-24 17:21:38 +01:00
|
|
|
validate_ffmpeg();
|
|
|
|
|
2022-04-13 17:40:47 +02:00
|
|
|
if config.general.generate.is_some() {
|
|
|
|
// generate playlist from given dates
|
|
|
|
generate_playlist();
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:07:34 +02:00
|
|
|
if config.rpc_server.enable {
|
2022-04-08 12:18:29 +02:00
|
|
|
rt_handle.spawn(json_rpc_server(
|
2022-04-07 15:01:09 +02:00
|
|
|
play_control.clone(),
|
|
|
|
playout_stat.clone(),
|
|
|
|
proc_control.clone(),
|
|
|
|
));
|
2022-04-05 17:07:34 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 12:18:29 +02:00
|
|
|
if &config.out.mode.to_lowercase() == "hls" {
|
2022-04-07 15:01:09 +02:00
|
|
|
write_hls(rt_handle, play_control, playout_stat, proc_control);
|
2022-03-31 17:36:10 +02:00
|
|
|
} else {
|
2022-04-07 15:01:09 +02:00
|
|
|
player(rt_handle, play_control, playout_stat, proc_control);
|
2022-03-31 17:36:10 +02:00
|
|
|
}
|
2022-04-05 17:07:34 +02:00
|
|
|
|
|
|
|
info!("Playout done...");
|
2022-02-12 22:32:51 +01:00
|
|
|
}
|