Visual Studio Code, rebuilt in Rust
Rust
//! mediaCodecs.test.ts with the builders of mediaTestFiles.ts.
use super::*;
fn text(value: &str) -> Vec<u8> {
value.bytes().collect()
}
fn zeros(length: usize) -> Vec<u8> {
vec![0; length]
}
fn u16_le(value: u16) -> Vec<u8> {
value.to_le_bytes().to_vec()
}
fn u32_le(value: u32) -> Vec<u8> {
value.to_le_bytes().to_vec()
}
fn mp4_box(kind: &str, content: &[Vec<u8>]) -> Vec<u8> {
let body = content.concat();
[((8 + body.len()) as u32).to_be_bytes().to_vec(), text(kind), body].concat()
}
fn mp4_track(handler_type: &str, sample_entry: Vec<u8>) -> Vec<u8> {
let stbl = mp4_box("stbl", &[mp4_box("stsd", &[zeros(4), 1u32.to_be_bytes().to_vec(), sample_entry]), mp4_box("stsz", &[zeros(1024)])]);
let mdia = mp4_box("mdia", &[mp4_box("mdhd", &[zeros(24)]), mp4_box("hdlr", &[zeros(8), text(handler_type), zeros(13)]), mp4_box("minf", &[stbl])]);
mp4_box("trak", &[mp4_box("tkhd", &[zeros(84)]), mdia])
}
/// An elementary stream descriptor of MPEG-4 Audio with the given AudioSpecificConfig.
fn mpeg4_audio_esds(audio_specific_config: &[u8]) -> Vec<u8> {
let decoder_specific_info = [vec![0x05, audio_specific_config.len() as u8], audio_specific_config.to_vec()].concat();
let decoder_config = [vec![0x04, 13 + decoder_specific_info.len() as u8, 0x40, 0x15], zeros(11), decoder_specific_info].concat();
mp4_box("esds", &[zeros(4), [vec![0x03, 3 + decoder_config.len() as u8, 0, 1, 0], decoder_config].concat()])
}
fn ebml(id: &[u8], content: &[Vec<u8>]) -> Vec<u8> {
let body = content.concat();
[id.to_vec(), vec![0x40 | (body.len() >> 8) as u8, (body.len() & 0xFF) as u8], body].concat()
}
/// A WebM file with the given track types and codec ids, in a segment of unknown size as live recordings write.
fn webm(tracks: &[(u8, &str)]) -> Vec<u8> {
let entries: Vec<Vec<u8>> = tracks.iter().enumerate().map(|(index, (kind, codec_id))| ebml(&[0xAE], &[ebml(&[0xD7], &[vec![index as u8 + 1]]), ebml(&[0x83], &[vec![*kind]]), ebml(&[0x86], &[text(codec_id)])])).collect();
[
ebml(&[0x1A, 0x45, 0xDF, 0xA3], &[ebml(&[0x42, 0x82], &[text("webm")])]),
vec![0x18, 0x53, 0x80, 0x67, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
ebml(&[0x15, 0x49, 0xA9, 0x66], &[ebml(&[0x2A, 0xD7, 0xB1], &[vec![0x0F, 0x42, 0x40]])]),
ebml(&[0x16, 0x54, 0xAE, 0x6B], &entries),
ebml(&[0x1F, 0x43, 0xB6, 0x75], &[zeros(16)]),
]
.concat()
}
fn ogg_beginning_of_stream_page(packet: Vec<u8>) -> Vec<u8> {
[text("OggS"), vec![0, 0x02], zeros(20), vec![1, packet.len() as u8], packet].concat()
}
/// An odd sized chunk before the format checks that chunks are padded to an even size.
fn wav(format: Vec<u8>) -> Vec<u8> {
[text("RIFF"), zeros(4), text("WAVE"), text("LIST"), u32_le(3), zeros(4), text("fmt "), u32_le(format.len() as u32), format, text("data"), zeros(4)].concat()
}
fn info(bytes: &[u8]) -> Option<MediaInfo> {
read_media_info(bytes.len() as u64, |position, length| Ok(bytes[position as usize..(position + length) as usize].to_vec()))
}
fn audio(codec: &str, name: Option<&'static str>) -> Track {
Track { kind: "audio", codec: codec.into(), name }
}
#[test]
fn reads_the_container_and_track_codecs() {
let ftyp = mp4_box("ftyp", &[text("isom"), zeros(4)]);
// The media data comes before the track descriptions, which start past the first chunk and end in the second.
let aac_video = [
ftyp.clone(),
mp4_box("mdat", &[zeros(100_000)]),
mp4_box("moov", &[mp4_box("mvhd", &[zeros(100)]), mp4_track("vide", mp4_box("avc1", &[zeros(78)])), mp4_track("soun", mp4_box("mp4a", &[zeros(28), mpeg4_audio_esds(&[0x12, 0x10])]))]),
]
.concat();
assert_eq!(info(&aac_video), Some(MediaInfo { container: "mp4", tracks: vec![audio("mp4a.40.2", Some("AAC"))] }));
let ac3 = [ftyp.clone(), mp4_box("moov", &[mp4_track("soun", mp4_box("ac-3", &[zeros(28)]))])].concat();
assert_eq!(info(&ac3), Some(MediaInfo { container: "mp4", tracks: vec![audio("ac-3", Some("AC-3"))] }));
// MPEG-4 Audio that is not AAC: lossless ALS, whose object type 36 is escaped.
let als = [ftyp.clone(), mp4_box("moov", &[mp4_track("soun", mp4_box("mp4a", &[zeros(28), mpeg4_audio_esds(&[0xF8, 0x80])]))])].concat();
assert_eq!(info(&als), Some(MediaInfo { container: "mp4", tracks: vec![audio("mp4a.40.36", None)] }));
let silent = [ftyp, mp4_box("moov", &[mp4_track("vide", mp4_box("avc1", &[zeros(78)]))])].concat();
assert_eq!(info(&silent), Some(MediaInfo { container: "mp4", tracks: vec![] }));
// The codec id of a subtitle track that matches a video codec is ignored.
let vp9 = webm(&[(1, "V_VP9"), (2, "A_OPUS"), (2, "A_AAC/MPEG4/LC"), (17, "V_VP8")]);
let video = Track { kind: "video", codec: "vp9".into(), name: Some("VP9") };
assert_eq!(info(&vp9), Some(MediaInfo { container: "webm", tracks: vec![video, audio("opus", Some("Opus")), audio("mp4a.40.2", Some("AAC"))] }));
let speex = [ogg_beginning_of_stream_page([text("Speex "), zeros(72)].concat()), text("OggS"), zeros(23)].concat();
assert_eq!(info(&speex), Some(MediaInfo { container: "ogg", tracks: vec![audio("speex", Some("Speex"))] }));
let adpcm = wav([u16_le(0x0011), u16_le(1), u32_le(8000), u32_le(4055), u16_le(256), u16_le(4), u16_le(2), u16_le(505)].concat());
assert_eq!(info(&adpcm), Some(MediaInfo { container: "wav", tracks: vec![audio("17", Some("IMA ADPCM"))] }));
// The extensible format with an IEEE float sub format, which browsers decode.
let float = wav([u16_le(0xFFFE), u16_le(2), u32_le(48000), u32_le(384000), u16_le(8), u16_le(32), u16_le(22), u16_le(32), u32_le(3), u16_le(0x0003), zeros(14)].concat());
assert_eq!(info(&float), Some(MediaInfo { container: "wav", tracks: vec![] }));
assert_eq!(info(&text("not a media file")), None);
assert_eq!(read_file_media_info(std::path::Path::new("/missing.mp4")), None);
}
/// A track entry cut to 5 bytes, so its track type header runs past it: the field is empty, not a negative length.
#[test]
fn reads_a_field_that_starts_past_its_parent() {
let mut file = webm(&[(1, "V_VP9")]);
let entry = file.windows(3).position(|window| window == [0xAE, 0x40, 0x10]).expect("the track entry");
file[entry + 2] = 5;
assert_eq!(info(&file).map(|info| info.tracks), Some(vec![]));
}
/// The reader parses bytes from any file: every truncation of each container, and each with seeded random bytes
/// written over it, reads without a panic.
#[test]
fn reads_cut_and_corrupted_files_without_a_panic() {
let ftyp = mp4_box("ftyp", &[text("isom"), zeros(4)]);
let fixtures = [
[ftyp.clone(), mp4_box("moov", &[mp4_track("vide", mp4_box("avc1", &[zeros(78)])), mp4_track("soun", mp4_box("mp4a", &[zeros(28), mpeg4_audio_esds(&[0x12, 0x10])]))])].concat(),
[ftyp, mp4_box("moov", &[mp4_track("soun", mp4_box("mp4a", &[zeros(28), mpeg4_audio_esds(&[0xF8, 0x80])]))])].concat(),
webm(&[(1, "V_VP9"), (2, "A_OPUS"), (2, "A_AAC/MPEG4/LC")]),
[ogg_beginning_of_stream_page([text("Speex "), zeros(72)].concat()), text("OggS"), zeros(23)].concat(),
wav([u16_le(0xFFFE), u16_le(2), u32_le(48000), u32_le(384000), u16_le(8), u16_le(32), u16_le(22), u16_le(32), u32_le(3), u16_le(0x0003), zeros(14)].concat()),
];
let mut seed = 0x2545_F491_4F6C_DD1Du64;
let mut random = move || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
seed
};
for fixture in &fixtures {
for length in 0..fixture.len() {
info(&fixture[..length]);
}
for _ in 0..5000 {
let mut corrupted = fixture.clone();
for _ in 0..1 + random() % 8 {
let index = (random() % corrupted.len() as u64) as usize;
corrupted[index] = random() as u8;
}
info(&corrupted);
}
}
for length in [0, 1, 16, 4096] {
info(&(0..length).map(|_| random() as u8).collect::<Vec<_>>());
}
}
#[test]
fn stops_at_the_read_limit() {
// Box headers in 33 chunks before the track descriptions.
let free = mp4_box("free", &[zeros(CHUNK_SIZE as usize)]);
let late = [vec![mp4_box("ftyp", &[text("isom"), zeros(4)])], vec![free; MAX_CHUNKS + 1], vec![mp4_box("moov", &[mp4_track("soun", mp4_box("ac-3", &[zeros(28)]))])]].concat().concat();
assert_eq!(info(&late), None);
}