call only one function for get current, next or updated palylist

This commit is contained in:
jb-alvarado 2024-02-01 22:39:04 +01:00
parent 2dc389c9f9
commit 831f867ec6
3 changed files with 37 additions and 66 deletions

View File

@ -60,9 +60,8 @@ impl CurrentProgram {
} }
// Check if playlist file got updated, and when yes we reload it and setup everything in place. // Check if playlist file got updated, and when yes we reload it and setup everything in place.
fn get_or_update_playlist(&mut self, seek: bool) { fn load_or_update_playlist(&mut self, seek: bool) {
let mut get_current = false; let mut get_current = false;
// let mut get_next = false;
let mut reload = false; let mut reload = false;
if let Some(path) = self.json_path.clone() { if let Some(path) = self.json_path.clone() {
@ -90,7 +89,7 @@ impl CurrentProgram {
if !reload { if !reload {
if let Some(file) = &json.current_file { if let Some(file) = &json.current_file {
info!("Read Playlist: <b><magenta>{file}</></b>"); info!("Read playlist: <b><magenta>{file}</></b>");
} }
} }
@ -108,28 +107,12 @@ impl CurrentProgram {
} }
} }
fn set_status(&mut self, date: String) {
*self.playout_stat.current_date.lock().unwrap() = date.clone();
*self.playout_stat.time_shift.lock().unwrap() = 0.0;
if let Err(e) = fs::write(
&self.config.general.stat_file,
serde_json::to_string(&json!({
"time_shift": 0.0,
"date": date,
}))
.unwrap(),
) {
error!("Unable to write status file: {e}");
};
}
// Check if day is past and it is time for a new playlist. // Check if day is past and it is time for a new playlist.
fn check_for_next_playlist(&mut self) -> bool { fn check_for_playlist(&mut self, seek: bool) -> bool {
let (delta, total_delta) = get_delta(&self.config, &time_in_seconds()); let (delta, total_delta) = get_delta(&self.config, &time_in_seconds());
let mut next = false; let mut next = false;
let duration = if self.current_node.duration > self.current_node.out { let duration = if self.current_node.duration >= self.current_node.out {
self.current_node.duration self.current_node.duration
} else { } else {
// maybe out is longer to be able to loop // maybe out is longer to be able to loop
@ -170,13 +153,10 @@ impl CurrentProgram {
); );
if let Some(file) = &json.current_file { if let Some(file) = &json.current_file {
info!("Read next Playlist: <b><magenta>{file}</></b>"); info!("Read next playlist: <b><magenta>{file}</></b>");
self.playout_stat.list_init.store(false, Ordering::SeqCst);
} else {
self.playout_stat.list_init.store(true, Ordering::SeqCst);
} }
self.playout_stat.list_init.store(false, Ordering::SeqCst);
self.set_status(json.date.clone()); self.set_status(json.date.clone());
self.json_path = json.current_file.clone(); self.json_path = json.current_file.clone();
@ -185,11 +165,29 @@ impl CurrentProgram {
*self.player_control.current_list.lock().unwrap() = json.program; *self.player_control.current_list.lock().unwrap() = json.program;
self.player_control.current_index.store(0, Ordering::SeqCst); self.player_control.current_index.store(0, Ordering::SeqCst);
} else {
self.load_or_update_playlist(seek)
} }
next next
} }
fn set_status(&mut self, date: String) {
*self.playout_stat.current_date.lock().unwrap() = date.clone();
*self.playout_stat.time_shift.lock().unwrap() = 0.0;
if let Err(e) = fs::write(
&self.config.general.stat_file,
serde_json::to_string(&json!({
"time_shift": 0.0,
"date": date,
}))
.unwrap(),
) {
error!("Unable to write status file: {e}");
};
}
// Check if last and/or next clip is a advertisement. // Check if last and/or next clip is a advertisement.
fn last_next_ad(&mut self) { fn last_next_ad(&mut self) {
let index = self.player_control.current_index.load(Ordering::SeqCst); let index = self.player_control.current_index.load(Ordering::SeqCst);
@ -299,10 +297,11 @@ impl Iterator for CurrentProgram {
type Item = Media; type Item = Media;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let list_init_seek = self.playout_stat.list_init.load(Ordering::SeqCst); self.last_json_path = self.json_path.clone();
self.get_or_update_playlist(list_init_seek); self.last_node_ad = self.current_node.last_ad;
self.check_for_playlist(self.playout_stat.list_init.load(Ordering::SeqCst));
if list_init_seek { if self.playout_stat.list_init.load(Ordering::SeqCst) {
trace!("Init playlist, from next iterator"); trace!("Init playlist, from next iterator");
let mut init_clip_is_filler = false; let mut init_clip_is_filler = false;
@ -346,35 +345,11 @@ impl Iterator for CurrentProgram {
self.current_node.index.unwrap_or_default() self.current_node.index.unwrap_or_default()
); );
let next_playlist = self.check_for_next_playlist();
if new_length if new_length
>= self.config.playlist.length_sec.unwrap() >= self.config.playlist.length_sec.unwrap()
+ self.config.playlist.start_sec.unwrap() + self.config.playlist.start_sec.unwrap()
{ {
self.init_clip(); self.init_clip();
} else if next_playlist
&& self.player_control.current_list.lock().unwrap().len() > 1
{
let index = self
.player_control
.current_index
.fetch_add(1, Ordering::SeqCst);
let nodes = self.player_control.current_list.lock().unwrap();
let node_clone = nodes[index].clone();
drop(nodes);
self.current_node = gen_source(
&self.config,
node_clone,
&self.playout_stat,
&self.player_control,
0,
);
return Some(self.current_node.clone());
} else if !init_clip_is_filler { } else if !init_clip_is_filler {
// fill missing length from playlist // fill missing length from playlist
let mut current_time = time_in_seconds(); let mut current_time = time_in_seconds();
@ -412,10 +387,6 @@ impl Iterator for CurrentProgram {
return Some(self.current_node.clone()); return Some(self.current_node.clone());
} }
self.last_json_path = self.json_path.clone();
self.last_node_ad = self.current_node.last_ad;
self.check_for_next_playlist();
if self.player_control.current_index.load(Ordering::SeqCst) if self.player_control.current_index.load(Ordering::SeqCst)
< self.player_control.current_list.lock().unwrap().len() < self.player_control.current_list.lock().unwrap().len()
{ {

BIN
tests/assets/filler.mp4 Normal file

Binary file not shown.

View File

@ -30,7 +30,7 @@ fn test_gen_source() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.logging.level = LevelFilter::Trace; config.logging.level = LevelFilter::Trace;
@ -89,7 +89,7 @@ fn test_gen_source() {
100, 100,
); );
assert_eq!(valid_media.source, "assets/with_audio.mp4"); assert_eq!(valid_media.source, "assets/filler.mp4");
} }
#[test] #[test]
@ -107,7 +107,7 @@ fn playlist_missing() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.logging.level = LevelFilter::Trace; config.logging.level = LevelFilter::Trace;
@ -150,7 +150,7 @@ fn playlist_next_missing() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.logging.level = LevelFilter::Trace; config.logging.level = LevelFilter::Trace;
@ -193,7 +193,7 @@ fn playlist_to_short() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.logging.level = log::LevelFilter::Trace; config.logging.level = log::LevelFilter::Trace;
@ -236,7 +236,7 @@ fn playlist_init_after_list_end() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.logging.level = log::LevelFilter::Trace; config.logging.level = log::LevelFilter::Trace;
@ -279,7 +279,7 @@ fn playlist_change_at_midnight() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.logging.level = LevelFilter::Trace; config.logging.level = LevelFilter::Trace;
@ -322,7 +322,7 @@ fn playlist_change_before_midnight() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.logging.level = LevelFilter::Trace; config.logging.level = LevelFilter::Trace;
@ -365,7 +365,7 @@ fn playlist_change_at_six() {
config.playlist.length = "24:00:00".into(); config.playlist.length = "24:00:00".into();
config.playlist.length_sec = Some(86400.0); config.playlist.length_sec = Some(86400.0);
config.playlist.path = "assets/playlists".into(); config.playlist.path = "assets/playlists".into();
config.storage.filler = "assets/with_audio.mp4".into(); config.storage.filler = "assets/filler.mp4".into();
config.logging.log_to_file = false; config.logging.log_to_file = false;
config.logging.timestamp = false; config.logging.timestamp = false;
config.out.mode = Null; config.out.mode = Null;