use container_probe::{Confidence, Format, Probe};
use std::fs;
fn fixture(rel: &str) -> Vec<u8> {
fs::read(format!("{}/../{}", env!("CARGO_MANIFEST_DIR"), rel))
.unwrap_or_else(|e| panic!("failed to read fixture {rel}: {e}"))
}
fn assert_identified(rel: &str, format: Format) {
let p = container_probe::probe(&fixture(rel));
assert!(
matches!(p, Probe::Identified { format: f, .. } if f == format),
"{rel}: expected {format:?}, got {p:?}"
);
}
fn assert_identified_as(rel: &str, format: Format, confidence: Confidence) {
let p = container_probe::probe(&fixture(rel));
assert!(
matches!(
p,
Probe::Identified { format: f, confidence: c, .. } if f == format && c == confidence
),
"{rel}: expected {format:?} @ {confidence:?}, got {p:?}"
);
}
#[test]
fn flv_is_flv() {
assert_identified_as("fixtures/flv/av.flv", Format::Flv, Confidence::STRONG);
}
#[test]
fn wav_is_wav() {
assert_identified_as(
"fixtures/container-probe/pcm_s16le.wav",
Format::Wav,
Confidence::STRONG,
);
}
#[test]
fn ogg_is_ogg() {
assert_identified_as(
"fixtures/container-probe/opus.ogg",
Format::Ogg,
Confidence::STRONG,
);
}
#[test]
fn asf_is_asf() {
assert_identified_as(
"fixtures/container-probe/video.asf",
Format::Asf,
Confidence::STRONG,
);
}
#[test]
fn aac_adts_is_adts() {
assert_identified_as(
"fixtures/container-probe/aac.adts",
Format::AdtsAac,
Confidence::LATTICE_STRONG,
);
}
#[test]
fn mp3_is_mp3() {
assert_identified_as(
"fixtures/container-probe/audio.mp3",
Format::Mp3,
Confidence::LATTICE_STRONG,
);
}
#[test]
fn annexb_is_annexb() {
assert_identified_as(
"fixtures/container-probe/h264.annexb",
Format::AnnexB,
Confidence::LATTICE_STRONG,
);
}
#[test]
fn suppression_ts_carries_adts_and_mp3() {
assert_identified("fixtures/ts/h264_aac.ts", Format::MpegTs);
}
#[test]
fn suppression_big_ts() {
assert_identified("fixtures/ts/france2.ts", Format::MpegTs);
}
#[test]
fn suppression_mp4() {
assert_identified("fixtures/mp4/h264_high.mp4", Format::Isobmff);
}
#[test]
fn suppression_mkv() {
assert_identified("fixtures/mkv/h264_aac.mkv", Format::Matroska);
}
#[test]
fn suppression_ps_starts_with_start_code_but_is_not_annexb() {
assert_identified("fixtures/ps/h264_ac3.ps", Format::MpegPs);
}
#[test]
fn suppression_mxf_carries_annexb_start_codes() {
assert_identified("fixtures/mxf/op1a_mpeg2_pcm.mxf", Format::Mxf);
}
#[test]
fn suppression_synthetic_ts_carrying_adts() {
let data = synthetic_ts_carrying_adts(32);
let p = container_probe::probe(&data);
assert!(
matches!(
p,
Probe::Identified {
format: Format::MpegTs,
..
}
),
"synthetic TS+ADTS must be MpegTs under suppression, got {p:?}"
);
}
fn synthetic_ts_carrying_adts(frame_count: usize) -> Vec<u8> {
let aac_len = frame_count * 274usize;
let packets = std::cmp::max(12, aac_len.div_ceil(188) * 2);
let mut v = vec![0u8; packets * 188 + frame_count * 274];
for i in 0..packets {
v[i * 188] = 0x47; }
let base = packets * 188;
for k in 0..frame_count {
let o = base + k * 274;
let mut f = vec![0u8; 274];
f[0] = 0xFF;
f[1] = 0xF1;
f[3] = 0; f[4] = ((274u16 >> 3) & 0xFF) as u8;
f[5] = ((274u16 & 0x07) as u8) << 5;
v[o..o + 274].copy_from_slice(&f);
}
v
}