simplify json reader, fix playlist update check

This commit is contained in:
jb-alvarado 2022-06-02 22:08:02 +02:00
parent 704053e599
commit 63d88fbf97
2 changed files with 101 additions and 89 deletions

View File

@ -42,6 +42,10 @@ impl CurrentProgram {
) -> Self { ) -> Self {
let json = read_json(config, None, is_terminated.clone(), true, 0.0); let json = read_json(config, None, is_terminated.clone(), true, 0.0);
if let Some(file) = &json.current_file {
info!("Read Playlist: <b><magenta>{}</></b>", file);
}
*current_list.lock().unwrap() = json.program; *current_list.lock().unwrap() = json.program;
*playout_stat.current_date.lock().unwrap() = json.date.clone(); *playout_stat.current_date.lock().unwrap() = json.date.clone();
@ -74,6 +78,10 @@ impl CurrentProgram {
if self.json_path.is_none() { if self.json_path.is_none() {
let json = read_json(&self.config, None, self.is_terminated.clone(), seek, 0.0); let json = read_json(&self.config, None, self.is_terminated.clone(), seek, 0.0);
if let Some(file) = &json.current_file {
info!("Read Playlist: <b><magenta>{}</></b>", file);
}
self.json_path = json.current_file; self.json_path = json.current_file;
self.json_mod = json.modified; self.json_mod = json.modified;
*self.nodes.lock().unwrap() = json.program; *self.nodes.lock().unwrap() = json.program;
@ -100,8 +108,7 @@ impl CurrentProgram {
self.json_mod = json.modified; self.json_mod = json.modified;
*self.nodes.lock().unwrap() = json.program; *self.nodes.lock().unwrap() = json.program;
self.get_current_clip(); self.playout_stat.list_init.store(true, Ordering::SeqCst);
self.index.fetch_add(1, Ordering::SeqCst);
} }
} else { } else {
error!( error!(
@ -151,6 +158,10 @@ impl CurrentProgram {
next_start, next_start,
); );
if let Some(file) = &json.current_file {
info!("Read Playlist: <b><magenta>{}</></b>", file);
}
let data = json!({ let data = json!({
"time_shift": 0.0, "time_shift": 0.0,
"date": json.date, "date": json.date,
@ -251,9 +262,9 @@ impl Iterator for CurrentProgram {
type Item = Media; type Item = Media;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.playout_stat.list_init.load(Ordering::SeqCst) { self.check_update(self.playout_stat.list_init.load(Ordering::SeqCst));
self.check_update(true);
if self.playout_stat.list_init.load(Ordering::SeqCst) {
if self.json_path.is_some() { if self.json_path.is_some() {
self.init_clip(); self.init_clip();
} }
@ -294,9 +305,9 @@ impl Iterator for CurrentProgram {
media.out = duration; media.out = duration;
self.current_node = gen_source(&self.config, media); self.current_node = gen_source(&self.config, media);
self.nodes.lock().unwrap().push(self.current_node.clone()); let mut nodes = self.nodes.lock().unwrap();
self.index nodes.push(self.current_node.clone());
.store(self.nodes.lock().unwrap().len(), Ordering::SeqCst); self.index.store(nodes.len(), Ordering::SeqCst);
} }
} }
@ -309,23 +320,23 @@ impl Iterator for CurrentProgram {
self.check_for_next_playlist(); self.check_for_next_playlist();
let mut is_last = false; let mut is_last = false;
let index = self.index.load(Ordering::SeqCst); let index = self.index.load(Ordering::SeqCst);
let nodes = self.nodes.lock().unwrap();
if index == self.nodes.lock().unwrap().len() - 1 { if index == nodes.len() - 1 {
is_last = true is_last = true
} }
self.current_node = timed_source( self.current_node = timed_source(
self.nodes.lock().unwrap()[index].clone(), nodes[index].clone(),
&self.config, &self.config,
is_last, is_last,
&self.playout_stat, &self.playout_stat,
); );
drop(nodes);
self.last_next_ad(); self.last_next_ad();
self.index.fetch_add(1, Ordering::SeqCst); self.index.fetch_add(1, Ordering::SeqCst);
// update playlist should happen after current clip,
// to prevent unknown behaviors.
self.check_update(false);
Some(self.current_node.clone()) Some(self.current_node.clone())
} else { } else {
let last_playlist = self.json_path.clone(); let last_playlist = self.json_path.clone();

View File

@ -47,79 +47,7 @@ impl Playlist {
} }
} }
/// Read json playlist file, fills Playlist struct and set some extra values, fn set_defaults(mut playlist: Playlist, current_file: String, mut start_sec: f64) -> Playlist {
/// which we need to process.
pub fn read_json(
config: &GlobalConfig,
path: Option<String>,
is_terminated: Arc<AtomicBool>,
seek: bool,
next_start: f64,
) -> Playlist {
let config_clone = config.clone();
let mut playlist_path = Path::new(&config.playlist.path).to_owned();
let mut start_sec = config.playlist.start_sec.unwrap();
let date = get_date(seek, start_sec, next_start);
if playlist_path.is_dir() || is_remote(&config.playlist.path) {
let d: Vec<&str> = date.split('-').collect();
playlist_path = playlist_path
.join(d[0])
.join(d[1])
.join(date.clone())
.with_extension("json");
}
let mut current_file: String = playlist_path.as_path().display().to_string();
if let Some(p) = path {
playlist_path = Path::new(&p).to_owned();
current_file = p
}
let mut playlist = Playlist::new(date, start_sec);
if is_remote(&current_file) {
let response = reqwest::blocking::Client::new().get(&current_file).send();
if let Ok(resp) = response {
if resp.status().is_success() {
info!("Read Remote Playlist: <b><magenta>{current_file}</></b>");
let headers = resp.headers().clone();
if let Ok(body) = resp.text() {
playlist =
serde_json::from_str(&body).expect("Could't read remote json playlist.");
if let Some(time) = time_from_header(&headers) {
playlist.modified = Some(time.to_string());
}
}
}
} else {
error!("Can't read remote playlist <b><magenta>{current_file}</></b>");
return playlist;
}
} else {
if !playlist_path.is_file() {
error!("Playlist <b><magenta>{current_file}</></b> not exists!");
return playlist;
}
info!("Read Playlist: <b><magenta>{current_file}</></b>");
let f = File::options()
.read(true)
.write(false)
.open(&current_file)
.expect("Could not open json playlist file.");
playlist = serde_json::from_reader(f).expect("Could't read json playlist file.");
playlist.modified = modified_time(&current_file);
}
playlist.current_file = Some(current_file); playlist.current_file = Some(current_file);
playlist.start_sec = Some(start_sec); playlist.start_sec = Some(start_sec);
@ -135,9 +63,82 @@ pub fn read_json(
start_sec += item.out - item.seek; start_sec += item.out - item.seek;
} }
playlist
}
/// Read json playlist file, fills Playlist struct and set some extra values,
/// which we need to process.
pub fn read_json(
config: &GlobalConfig,
path: Option<String>,
is_terminated: Arc<AtomicBool>,
seek: bool,
next_start: f64,
) -> Playlist {
let config_clone = config.clone();
let mut playlist_path = Path::new(&config.playlist.path).to_owned();
let start_sec = config.playlist.start_sec.unwrap();
let date = get_date(seek, start_sec, next_start);
if playlist_path.is_dir() || is_remote(&config.playlist.path) {
let d: Vec<&str> = date.split('-').collect();
playlist_path = playlist_path
.join(d[0])
.join(d[1])
.join(date.clone())
.with_extension("json");
}
let mut current_file = playlist_path.as_path().display().to_string();
if let Some(p) = path {
playlist_path = Path::new(&p).to_owned();
current_file = p
}
if is_remote(&current_file) {
let response = reqwest::blocking::Client::new().get(&current_file).send();
if let Ok(resp) = response {
if resp.status().is_success() {
let headers = resp.headers().clone();
if let Ok(body) = resp.text() {
let mut playlist: Playlist =
serde_json::from_str(&body).expect("Could't read remote json playlist.");
if let Some(time) = time_from_header(&headers) {
playlist.modified = Some(time.to_string());
}
let list_clone = playlist.clone();
thread::spawn(move || {
validate_playlist(list_clone, is_terminated, config_clone)
});
return set_defaults(playlist, current_file, start_sec);
}
}
}
} else if playlist_path.is_file() {
let f = File::options()
.read(true)
.write(false)
.open(&current_file)
.expect("Could not open json playlist file.");
let mut playlist: Playlist =
serde_json::from_reader(f).expect("Could't read json playlist file.");
playlist.modified = modified_time(&current_file);
let list_clone = playlist.clone(); let list_clone = playlist.clone();
thread::spawn(move || validate_playlist(list_clone, is_terminated, config_clone)); thread::spawn(move || validate_playlist(list_clone, is_terminated, config_clone));
playlist return set_defaults(playlist, current_file, start_sec);
}
error!("Read playlist error, on: <b><magenta>{current_file}</></b>!");
Playlist::new(date, start_sec)
} }