2022-04-07 15:01:09 +02:00
|
|
|
use std::{
|
2022-04-28 17:54:55 +02:00
|
|
|
fs::{self, File},
|
2022-04-07 15:01:09 +02:00
|
|
|
path::PathBuf,
|
2022-04-13 17:40:47 +02:00
|
|
|
process::exit,
|
2022-05-16 10:05:38 +02:00
|
|
|
sync::{Arc, Mutex},
|
2022-04-26 13:47:52 +02:00
|
|
|
thread,
|
2022-04-07 15:01:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::json;
|
2022-04-05 17:07:34 +02:00
|
|
|
use simplelog::*;
|
2022-03-28 15:52:03 +02:00
|
|
|
|
2022-06-21 23:10:38 +02:00
|
|
|
pub mod input;
|
|
|
|
pub mod output;
|
|
|
|
pub mod rpc;
|
|
|
|
// #[cfg(test)]
|
|
|
|
// mod tests;
|
|
|
|
pub mod utils;
|
|
|
|
|
|
|
|
use utils::{arg_parse::get_args, get_config};
|
|
|
|
|
|
|
|
use crate::{
|
2022-05-18 17:34:46 +02:00
|
|
|
output::{player, write_hls},
|
|
|
|
rpc::json_rpc_server,
|
2022-06-21 23:10:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
use ffplayout_lib::utils::{
|
|
|
|
generate_playlist, init_logging, send_mail, validate_ffmpeg, PlayerControl, PlayoutStatus,
|
|
|
|
ProcessControl,
|
2022-04-06 18:10:57 +02:00
|
|
|
};
|
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-04-28 17:54:55 +02:00
|
|
|
/// Here we create a status file in temp folder.
|
2022-04-28 17:55:35 +02:00
|
|
|
/// We need this for reading/saving program status.
|
2022-04-28 17:54:55 +02:00
|
|
|
/// For example when we skip a playing file,
|
|
|
|
/// we save the time difference, so we stay in sync.
|
|
|
|
///
|
|
|
|
/// When file not exists we create it, and when it exists we get its values.
|
|
|
|
fn status_file(stat_file: &str, playout_stat: &PlayoutStatus) {
|
|
|
|
if !PathBuf::from(stat_file).exists() {
|
2022-04-07 15:01:09 +02:00
|
|
|
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");
|
2022-06-19 12:03:00 +02:00
|
|
|
if let Err(e) = fs::write(stat_file, &json) {
|
|
|
|
error!("Unable to write status file: {e}");
|
|
|
|
};
|
2022-04-07 15:01:09 +02:00
|
|
|
} else {
|
|
|
|
let stat_file = File::options()
|
|
|
|
.read(true)
|
|
|
|
.write(false)
|
2022-04-28 17:54:55 +02:00
|
|
|
.open(&stat_file)
|
2022-04-07 15:01:09 +02:00
|
|
|
.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-04-28 17:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-06-06 23:07:11 +02:00
|
|
|
let args = get_args();
|
2022-06-21 23:10:38 +02:00
|
|
|
let config = get_config(args);
|
2022-05-12 21:17:26 +02:00
|
|
|
let config_clone = config.clone();
|
2022-04-28 17:54:55 +02:00
|
|
|
let play_control = PlayerControl::new();
|
|
|
|
let playout_stat = PlayoutStatus::new();
|
|
|
|
let proc_control = ProcessControl::new();
|
2022-05-25 14:54:16 +02:00
|
|
|
let play_ctl = play_control.clone();
|
|
|
|
let play_stat = playout_stat.clone();
|
|
|
|
let proc_ctl1 = proc_control.clone();
|
|
|
|
let proc_ctl2 = proc_control.clone();
|
2022-05-16 10:05:38 +02:00
|
|
|
let messages = Arc::new(Mutex::new(Vec::new()));
|
2022-04-07 15:01:09 +02:00
|
|
|
|
2022-06-06 23:07:11 +02:00
|
|
|
let logging = init_logging(&config, Some(proc_ctl1), Some(messages.clone()));
|
2022-02-25 16:22:29 +01:00
|
|
|
CombinedLogger::init(logging).unwrap();
|
2022-02-23 18:06:40 +01:00
|
|
|
|
2022-05-12 21:17:26 +02:00
|
|
|
validate_ffmpeg(&config);
|
2022-03-24 17:21:38 +01:00
|
|
|
|
2022-04-14 11:50:29 +02:00
|
|
|
if let Some(range) = config.general.generate.clone() {
|
2022-04-28 17:54:55 +02:00
|
|
|
// run a simple playlist generator and save them to disk
|
2022-06-21 17:56:10 +02:00
|
|
|
if let Err(e) = generate_playlist(&config, range, None) {
|
|
|
|
error!("{e}");
|
|
|
|
exit(1);
|
|
|
|
};
|
2022-04-13 17:40:47 +02:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:07:34 +02:00
|
|
|
if config.rpc_server.enable {
|
2022-04-28 17:54:55 +02:00
|
|
|
// If RPC server is enable we also fire up a JSON RPC server.
|
2022-05-25 14:54:16 +02:00
|
|
|
thread::spawn(move || json_rpc_server(config_clone, play_ctl, play_stat, proc_ctl2));
|
2022-04-05 17:07:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 14:54:16 +02:00
|
|
|
status_file(&config.general.stat_file, &playout_stat);
|
|
|
|
|
2022-06-27 10:55:07 +02:00
|
|
|
match config.out.mode.to_lowercase().as_str() {
|
2022-04-28 17:54:55 +02:00
|
|
|
// write files/playlist to HLS m3u8 playlist
|
2022-06-27 10:55:07 +02:00
|
|
|
"hls" => write_hls(&config, play_control, playout_stat, proc_control),
|
2022-04-28 17:54:55 +02:00
|
|
|
// play on desktop or stream to a remote target
|
2022-06-27 10:55:07 +02:00
|
|
|
_ => player(&config, play_control, playout_stat, proc_control),
|
2022-03-31 17:36:10 +02:00
|
|
|
}
|
2022-04-05 17:07:34 +02:00
|
|
|
|
2022-05-25 14:54:16 +02:00
|
|
|
info!("Playout done...");
|
|
|
|
|
2022-05-17 21:45:35 +02:00
|
|
|
let msg = messages.lock().unwrap();
|
|
|
|
|
|
|
|
if msg.len() > 0 {
|
|
|
|
send_mail(&config, msg.join("\n"));
|
2022-05-16 10:05:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 14:54:16 +02:00
|
|
|
drop(msg);
|
2022-02-12 22:32:51 +01:00
|
|
|
}
|