diff --git a/lib/src/utils/generator.rs b/lib/src/utils/generator.rs index d9977ad5..a0b8449e 100644 --- a/lib/src/utils/generator.rs +++ b/lib/src/utils/generator.rs @@ -19,20 +19,10 @@ use walkdir::WalkDir; use super::{folder::FolderSource, PlayerControl}; use crate::utils::{ folder::fill_filler_list, gen_dummy, get_date_range, include_file_extension, - json_serializer::JsonPlaylist, time_to_sec, Media, PlayoutConfig, Template, + json_serializer::JsonPlaylist, sum_durations, time_to_sec, Media, PlayoutConfig, Template, }; -fn sum_durations(clip_list: &Vec) -> f64 { - let mut list_duration = 0.0; - - for item in clip_list { - list_duration += item.out - } - - list_duration -} - -fn random_list(clip_list: Vec, total_length: f64) -> Vec { +pub fn random_list(clip_list: Vec, total_length: f64) -> Vec { let mut max_attempts = 10000; let mut randomized_clip_list: Vec = vec![]; let mut target_duration = 0.0; @@ -65,7 +55,7 @@ fn random_list(clip_list: Vec, total_length: f64) -> Vec { randomized_clip_list } -fn ordered_list(clip_list: Vec, total_length: f64) -> Vec { +pub fn ordered_list(clip_list: Vec, total_length: f64) -> Vec { let mut index = 0; let mut skip_count = 0; let mut ordered_clip_list: Vec = vec![]; @@ -96,7 +86,7 @@ fn ordered_list(clip_list: Vec, total_length: f64) -> Vec { ordered_clip_list } -fn filler_list(config: &PlayoutConfig, total_length: f64) -> Vec { +pub fn filler_list(config: &PlayoutConfig, total_length: f64) -> Vec { let filler_list = fill_filler_list(config, None); let mut index = 0; let mut filler_clip_list: Vec = vec![]; diff --git a/lib/src/utils/mod.rs b/lib/src/utils/mod.rs index 27fc9450..8a185b5e 100644 --- a/lib/src/utils/mod.rs +++ b/lib/src/utils/mod.rs @@ -24,7 +24,7 @@ use simplelog::*; pub mod config; pub mod controller; pub mod folder; -mod generator; +pub mod generator; pub mod import; pub mod json_serializer; mod json_validate; @@ -148,14 +148,15 @@ impl Media { pub fn add_probe(&mut self) { if self.probe.is_none() { let probe = MediaProbe::new(&self.source); - self.probe = Some(probe.clone()); if let Some(dur) = probe .format + .clone() .and_then(|f| f.duration) .map(|d| d.parse().unwrap()) .filter(|d| !is_close(*d, self.duration, 0.5)) { + self.probe = Some(probe); self.duration = dur; if self.out == 0.0 { @@ -409,6 +410,17 @@ pub fn is_close(a: f64, b: f64, to: f64) -> bool { false } +/// add duration from all media clips +pub fn sum_durations(clip_list: &Vec) -> f64 { + let mut list_duration = 0.0; + + for item in clip_list { + list_duration += item.out + } + + list_duration +} + /// Get delta between clip start and current time. This value we need to check, /// if we still in sync. /// diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 402caeca..7b6a2355 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -41,3 +41,7 @@ path = "src/engine_playlist.rs" [[test]] name = "engine_cmd" path = "src/engine_cmd.rs" + +[[test]] +name = "engine_generator" +path = "src/engine_generator.rs" diff --git a/tests/src/engine_generator.rs b/tests/src/engine_generator.rs new file mode 100644 index 00000000..3868bd41 --- /dev/null +++ b/tests/src/engine_generator.rs @@ -0,0 +1,23 @@ +use ffplayout_lib::utils::{generator::ordered_list, sum_durations, Media}; + +#[test] +fn test_ordered_list() { + let clip_list = vec![ + Media::new(0, "./assets/with_audio.mp4", true), // 30 seconds + Media::new(0, "./assets/dual_audio.mp4", true), // 30 seconds + Media::new(0, "./assets/av_sync.mp4", true), // 30 seconds + Media::new(0, "./assets/ad.mp4", true), // 25 seconds + ]; + + let result = ordered_list(clip_list.clone(), 85.0); + + assert_eq!(result.len(), 3); + assert_eq!(result[2].duration, 25.0); + assert_eq!(sum_durations(&result), 85.0); + + let result = ordered_list(clip_list, 120.0); + + assert_eq!(result.len(), 4); + assert_eq!(result[2].duration, 30.0); + assert_eq!(sum_durations(&result), 115.0); +}