From 63d88fbf97cf9d517f399784f9195c878a4c299a Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Thu, 2 Jun 2022 22:08:02 +0200 Subject: [PATCH] simplify json reader, fix playlist update check --- src/input/playlist.rs | 35 +++++--- src/utils/json_serializer.rs | 155 ++++++++++++++++++----------------- 2 files changed, 101 insertions(+), 89 deletions(-) diff --git a/src/input/playlist.rs b/src/input/playlist.rs index 3393a28a..c67e71ac 100644 --- a/src/input/playlist.rs +++ b/src/input/playlist.rs @@ -42,6 +42,10 @@ impl CurrentProgram { ) -> Self { let json = read_json(config, None, is_terminated.clone(), true, 0.0); + if let Some(file) = &json.current_file { + info!("Read Playlist: {}", file); + } + *current_list.lock().unwrap() = json.program; *playout_stat.current_date.lock().unwrap() = json.date.clone(); @@ -74,6 +78,10 @@ impl CurrentProgram { if self.json_path.is_none() { let json = read_json(&self.config, None, self.is_terminated.clone(), seek, 0.0); + if let Some(file) = &json.current_file { + info!("Read Playlist: {}", file); + } + self.json_path = json.current_file; self.json_mod = json.modified; *self.nodes.lock().unwrap() = json.program; @@ -100,8 +108,7 @@ impl CurrentProgram { self.json_mod = json.modified; *self.nodes.lock().unwrap() = json.program; - self.get_current_clip(); - self.index.fetch_add(1, Ordering::SeqCst); + self.playout_stat.list_init.store(true, Ordering::SeqCst); } } else { error!( @@ -151,6 +158,10 @@ impl CurrentProgram { next_start, ); + if let Some(file) = &json.current_file { + info!("Read Playlist: {}", file); + } + let data = json!({ "time_shift": 0.0, "date": json.date, @@ -251,9 +262,9 @@ impl Iterator for CurrentProgram { type Item = Media; fn next(&mut self) -> Option { - if self.playout_stat.list_init.load(Ordering::SeqCst) { - self.check_update(true); + self.check_update(self.playout_stat.list_init.load(Ordering::SeqCst)); + if self.playout_stat.list_init.load(Ordering::SeqCst) { if self.json_path.is_some() { self.init_clip(); } @@ -294,9 +305,9 @@ impl Iterator for CurrentProgram { media.out = duration; self.current_node = gen_source(&self.config, media); - self.nodes.lock().unwrap().push(self.current_node.clone()); - self.index - .store(self.nodes.lock().unwrap().len(), Ordering::SeqCst); + let mut nodes = self.nodes.lock().unwrap(); + nodes.push(self.current_node.clone()); + self.index.store(nodes.len(), Ordering::SeqCst); } } @@ -309,23 +320,23 @@ impl Iterator for CurrentProgram { self.check_for_next_playlist(); let mut is_last = false; 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 } self.current_node = timed_source( - self.nodes.lock().unwrap()[index].clone(), + nodes[index].clone(), &self.config, is_last, &self.playout_stat, ); + + drop(nodes); self.last_next_ad(); 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()) } else { let last_playlist = self.json_path.clone(); diff --git a/src/utils/json_serializer.rs b/src/utils/json_serializer.rs index fbc2931e..5408c6c4 100644 --- a/src/utils/json_serializer.rs +++ b/src/utils/json_serializer.rs @@ -47,79 +47,7 @@ impl 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, - is_terminated: Arc, - 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(¤t_file) { - let response = reqwest::blocking::Client::new().get(¤t_file).send(); - - if let Ok(resp) = response { - if resp.status().is_success() { - info!("Read Remote Playlist: {current_file}"); - - 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 {current_file}"); - - return playlist; - } - } else { - if !playlist_path.is_file() { - error!("Playlist {current_file} not exists!"); - - return playlist; - } - - info!("Read Playlist: {current_file}"); - - let f = File::options() - .read(true) - .write(false) - .open(¤t_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(¤t_file); - } - +fn set_defaults(mut playlist: Playlist, current_file: String, mut start_sec: f64) -> Playlist { playlist.current_file = Some(current_file); playlist.start_sec = Some(start_sec); @@ -135,9 +63,82 @@ pub fn read_json( start_sec += item.out - item.seek; } - let list_clone = playlist.clone(); - - thread::spawn(move || validate_playlist(list_clone, is_terminated, config_clone)); - 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, + is_terminated: Arc, + 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(¤t_file) { + let response = reqwest::blocking::Client::new().get(¤t_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(¤t_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(¤t_file); + + let list_clone = playlist.clone(); + + thread::spawn(move || validate_playlist(list_clone, is_terminated, config_clone)); + + return set_defaults(playlist, current_file, start_sec); + } + + error!("Read playlist error, on: {current_file}!"); + + Playlist::new(date, start_sec) +}