| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::Command; |
| 10 | |
| 11 | use ec_core::registry::{MediaType, SeekMode}; |
| 12 | use ec_core::timebase::{TimeBase, Timestamp}; |
| 13 | use ec_probe::Reader; |
| 14 | |
| 15 | fn manifest() -> Vec<(PathBuf, String, String, f64)> { |
| 16 | let path = |
| 17 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/real-library-manifest.tsv"); |
| 18 | let Ok(text) = std::fs::read_to_string(path) else { |
| 19 | return Vec::new(); |
| 20 | }; |
| 21 | text.lines() |
| 22 | .skip(1) |
| 23 | .filter_map(|line| { |
| 24 | let f: Vec<&str> = line.split('\t').collect(); |
| 25 | let path = PathBuf::from(f.first()?); |
| 26 | Some(( |
| 27 | path, |
| 28 | (*f.get(1)?).to_string(), |
| 29 | (*f.get(7)?).to_string(), |
| 30 | f.get(8)?.parse().unwrap_or(0.0), |
| 31 | )) |
| 32 | }) |
| 33 | .collect() |
| 34 | } |
| 35 | |
| 36 | |
| 37 | fn decode_some(reader: &mut Reader, stream: u32, secs: f64) -> Result<u64, String> { |
| 38 | let mut decoder = reader.make_decoder(stream).map_err(|e| e.to_string())?; |
| 39 | let rate = f64::from(decoder.sample_rate().max(1)); |
| 40 | let want = (secs * rate) as u64; |
| 41 | let mut out = Vec::new(); |
| 42 | let mut frames = 0u64; |
| 43 | while frames < want { |
| 44 | let packet = match reader.next_packet() { |
| 45 | Ok(p) => p, |
| 46 | Err(e) if e.is_eof() => break, |
| 47 | Err(e) => return Err(format!("demux: {e}")), |
| 48 | }; |
| 49 | if packet.stream != stream { |
| 50 | continue; |
| 51 | } |
| 52 | decoder |
| 53 | .decode(&packet, &mut out) |
| 54 | .map_err(|e| format!("decode: {e}"))?; |
| 55 | frames += (out.len() / decoder.channels().max(1)) as u64; |
| 56 | } |
| 57 | Ok(frames) |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | #[test] |
| 63 | fn a_real_library_sweep() { |
| 64 | let files = manifest(); |
| 65 | if files.is_empty() { |
| 66 | eprintln!("no manifest — run scripts/scan-real-library.sh"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | let standalone = |p: &Path| { |
| 73 | let ext = p.extension().and_then(|e| e.to_str()).unwrap_or(""); |
| 74 | [ |
| 75 | "mp3", "flac", "wav", "ogg", "opus", "m4a", "aac", "mp4", "mov", |
| 76 | ] |
| 77 | .contains(&ext) |
| 78 | }; |
| 79 | let mut chosen: Vec<_> = Vec::new(); |
| 80 | let mut seen = Vec::new(); |
| 81 | for want_mkv in [false, true] { |
| 82 | let mut taken = 0; |
| 83 | for f in &files { |
| 84 | let (p, container, acodecs, _) = f; |
| 85 | if acodecs.is_empty() || !p.exists() || taken >= 5 { |
| 86 | continue; |
| 87 | } |
| 88 | let is_mkv = container.contains("matroska"); |
| 89 | if is_mkv != want_mkv || (!is_mkv && !standalone(p)) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | let key = format!("{is_mkv}:{acodecs}"); |
| 94 | if seen.contains(&key) { |
| 95 | continue; |
| 96 | } |
| 97 | seen.push(key); |
| 98 | chosen.push(f); |
| 99 | taken += 1; |
| 100 | } |
| 101 | } |
| 102 | assert!(!chosen.is_empty(), "no readable files in the manifest"); |
| 103 | |
| 104 | let mut rows = Vec::new(); |
| 105 | let mut failures = Vec::new(); |
| 106 | for (path, container, acodecs, duration) in &chosen { |
| 107 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 108 | let mut reader = match Reader::open(path) { |
| 109 | Ok(r) => r, |
| 110 | Err(e) => { |
| 111 | failures.push(format!("{name}: open failed: {e}")); |
| 112 | continue; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | let audio = reader |
| 118 | .streams() |
| 119 | .iter() |
| 120 | .find(|s| { |
| 121 | s.params.codec.media_type() == MediaType::Audio |
| 122 | && reader.make_decoder(s.index).is_ok() |
| 123 | }) |
| 124 | .or_else(|| reader.default_stream(MediaType::Audio)) |
| 125 | .cloned(); |
| 126 | let Some(audio) = audio else { |
| 127 | failures.push(format!("{name}: no audio stream, ffprobe says {acodecs}")); |
| 128 | continue; |
| 129 | }; |
| 130 | let unsupported = reader.unsupported(); |
| 131 | |
| 132 | let listed: Vec<String> = reader |
| 133 | .streams() |
| 134 | .iter() |
| 135 | .filter(|s| s.params.codec.media_type() == MediaType::Audio) |
| 136 | .map(|s| s.params.codec.name().to_string()) |
| 137 | .collect(); |
| 138 | let decoded = decode_some(&mut reader, audio.index, 1.0); |
| 139 | let rate = audio.params.audio().map_or(0, |a| a.sample_rate).max(1); |
| 140 | let landing = if *duration > 4.0 { |
| 141 | let ticks = (duration / 2.0 * f64::from(rate)) as i64; |
| 142 | reader |
| 143 | .seek( |
| 144 | audio.index, |
| 145 | Timestamp::new(ticks, TimeBase::from_rate(rate)), |
| 146 | SeekMode::SyncBefore, |
| 147 | ) |
| 148 | .map(|t| t.as_secs_f64()) |
| 149 | .map_err(|e| e.to_string()) |
| 150 | } else { |
| 151 | Ok(0.0) |
| 152 | }; |
| 153 | let after_seek = landing |
| 154 | .as_ref() |
| 155 | .ok() |
| 156 | .map(|_| decode_some(&mut reader, audio.index, 0.5)); |
| 157 | |
| 158 | let ok = decoded.is_ok() |
| 159 | && landing.is_ok() |
| 160 | && matches!(after_seek, Some(Ok(n)) if n > 0) |
| 161 | && listed.iter().any(|c| acodecs.split(',').any(|a| a == c)); |
| 162 | if !ok { |
| 163 | failures.push(format!( |
| 164 | "{name}: decode {decoded:?} seek {landing:?} after {after_seek:?} \ |
| 165 | codecs {listed:?} vs ffprobe {acodecs}" |
| 166 | )); |
| 167 | } |
| 168 | rows.push(format!( |
| 169 | "{:<52} {:<16} {:<14} {:>7} frames seek->{:>8} {}", |
| 170 | name.chars().take(52).collect::<String>(), |
| 171 | container.split(',').next().unwrap_or(container), |
| 172 | listed.join(","), |
| 173 | decoded.clone().unwrap_or(0), |
| 174 | landing |
| 175 | .as_ref() |
| 176 | .map(|s| format!("{s:.2}s")) |
| 177 | .unwrap_or_else(|e| e.clone()), |
| 178 | match unsupported.is_empty() { |
| 179 | true => "all decodable".to_string(), |
| 180 | false => unsupported |
| 181 | .iter() |
| 182 | .map(|u| format!("unsupported: {} ({})", u.codec.name(), u.reason)) |
| 183 | .collect::<Vec<_>>() |
| 184 | .join("; "), |
| 185 | } |
| 186 | )); |
| 187 | } |
| 188 | eprintln!( |
| 189 | "real-library sweep ({} files):\n{}", |
| 190 | rows.len(), |
| 191 | rows.join("\n") |
| 192 | ); |
| 193 | assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | #[test] |
| 200 | fn an_undecodable_track_is_named_not_hidden() { |
| 201 | let Some((path, ..)) = manifest() |
| 202 | .into_iter() |
| 203 | .find(|(p, _, acodecs, _)| acodecs.contains("truehd") && p.exists()) |
| 204 | else { |
| 205 | eprintln!("no TrueHD file in the manifest — skipping"); |
| 206 | return; |
| 207 | }; |
| 208 | let reader = Reader::open(&path).expect("open"); |
| 209 | let unsupported = reader.unsupported(); |
| 210 | let truehd = unsupported |
| 211 | .iter() |
| 212 | .find(|u| u.codec.name() == "truehd") |
| 213 | .expect("the TrueHD track is listed as unsupported"); |
| 214 | assert!( |
| 215 | truehd.reason.contains("truehd"), |
| 216 | "the reason names the codec: {}", |
| 217 | truehd.reason |
| 218 | ); |
| 219 | |
| 220 | let decodable: Vec<_> = reader |
| 221 | .streams() |
| 222 | .iter() |
| 223 | .filter(|s| s.params.codec.media_type() == MediaType::Audio) |
| 224 | .filter(|s| reader.make_decoder(s.index).is_ok()) |
| 225 | .map(|s| s.params.codec.name()) |
| 226 | .collect(); |
| 227 | assert!( |
| 228 | !decodable.is_empty(), |
| 229 | "a file with a TrueHD track and an AC-3 one came out with nothing to play" |
| 230 | ); |
| 231 | eprintln!( |
| 232 | "{}: {} listed unsupported ({}), decodable: {decodable:?}", |
| 233 | path.file_name().unwrap().to_string_lossy(), |
| 234 | truehd.codec.name(), |
| 235 | truehd.reason |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | #[allow(dead_code)] |
| 242 | fn ffprobe_audio_codecs(path: &Path) -> Vec<String> { |
| 243 | let out = Command::new("ffprobe") |
| 244 | .args([ |
| 245 | "-v", |
| 246 | "error", |
| 247 | "-select_streams", |
| 248 | "a", |
| 249 | "-show_entries", |
| 250 | "stream=codec_name", |
| 251 | "-of", |
| 252 | "default=nw=1:nk=1", |
| 253 | ]) |
| 254 | .arg(path) |
| 255 | .output() |
| 256 | .expect("ffprobe"); |
| 257 | String::from_utf8_lossy(&out.stdout) |
| 258 | .lines() |
| 259 | .map(|l| l.trim().to_string()) |
| 260 | .collect() |
| 261 | } |