| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | use std::path::{Path, PathBuf}; |
| 10 | use std::process::Command; |
| 11 | |
| 12 | use ec_core::Demuxer as _; |
| 13 | use oxideav_core::{CodecId, CodecParameters, Muxer as _, Packet, StreamInfo, TimeBase}; |
| 14 | |
| 15 | fn fixture() -> PathBuf { |
| 16 | Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/audio/vorbis-ogg-stereo-44100.ogg") |
| 17 | } |
| 18 | |
| 19 | fn decode_pcm(path: &Path) -> Vec<u8> { |
| 20 | let out = Command::new("ffmpeg") |
| 21 | .args(["-v", "error", "-i"]) |
| 22 | .arg(path) |
| 23 | .args(["-f", "f32le", "-"]) |
| 24 | .output() |
| 25 | .expect("ffmpeg"); |
| 26 | assert!( |
| 27 | out.status.success(), |
| 28 | "ffmpeg failed on {}: {}", |
| 29 | path.display(), |
| 30 | String::from_utf8_lossy(&out.stderr) |
| 31 | ); |
| 32 | out.stdout |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn writes_a_vorbis_file_the_way_the_replica_does() { |
| 37 | let source = fixture(); |
| 38 | if !source.exists() { |
| 39 | eprintln!("fixtures/audio absent (gitignored) — skipping"); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | let mut demuxer = ec_ogg::OggDemuxer::open(std::fs::File::open(&source).unwrap()).unwrap(); |
| 46 | let info = demuxer.streams()[0].clone(); |
| 47 | let extradata = info.params.extradata.clone().unwrap(); |
| 48 | let headers = ec_ogg::xiph_unlace(&extradata).unwrap(); |
| 49 | let mut packets = Vec::new(); |
| 50 | while let Ok(packet) = demuxer.next_packet() { |
| 51 | packets.push(packet); |
| 52 | } |
| 53 | assert_eq!(headers.len(), 3, "a Vorbis stream is three headers"); |
| 54 | |
| 55 | |
| 56 | let laced = super_xiph_lace(&headers).expect("the three headers would not lace"); |
| 57 | assert_eq!(&laced[..], &extradata[..], "lacing must round-trip"); |
| 58 | |
| 59 | let rate = 44_100u32; |
| 60 | let time_base = TimeBase::new(1, i64::from(rate)); |
| 61 | let mut params = CodecParameters::audio(CodecId::new("vorbis")); |
| 62 | params.sample_rate = Some(rate); |
| 63 | params.channels = Some(2); |
| 64 | params.extradata = laced; |
| 65 | let stream = StreamInfo { |
| 66 | index: 0, |
| 67 | time_base, |
| 68 | duration: info.duration, |
| 69 | start_time: Some(0), |
| 70 | params, |
| 71 | }; |
| 72 | |
| 73 | let out_path = |
| 74 | std::env::temp_dir().join(format!("oxideav-ogg-shim-{}.ogg", std::process::id())); |
| 75 | let file = std::fs::File::create(&out_path).unwrap(); |
| 76 | let mut muxer = oxideav_ogg::mux::open_concrete(Box::new(file), &[stream]).unwrap(); |
| 77 | muxer.set_page_target_bytes(Some(4096)); |
| 78 | muxer.write_header().unwrap(); |
| 79 | for packet in &packets { |
| 80 | |
| 81 | |
| 82 | let mut out = Packet::new(0, time_base, packet.data.to_vec()); |
| 83 | out.pts = ec_ogg::granule_of(packet); |
| 84 | out.duration = packet.duration; |
| 85 | muxer.write_packet(&out).unwrap(); |
| 86 | } |
| 87 | muxer.write_trailer().unwrap(); |
| 88 | |
| 89 | |
| 90 | let probe = Command::new("ffprobe") |
| 91 | .args([ |
| 92 | "-v", |
| 93 | "error", |
| 94 | "-select_streams", |
| 95 | "a:0", |
| 96 | "-count_packets", |
| 97 | "-show_entries", |
| 98 | "stream=codec_name,sample_rate,channels,nb_read_packets", |
| 99 | "-of", |
| 100 | "csv=p=0", |
| 101 | ]) |
| 102 | .arg(&out_path) |
| 103 | .output() |
| 104 | .expect("ffprobe"); |
| 105 | let stderr = String::from_utf8_lossy(&probe.stderr); |
| 106 | assert!(stderr.trim().is_empty(), "ffprobe complained: {stderr}"); |
| 107 | assert_eq!( |
| 108 | String::from_utf8_lossy(&probe.stdout).trim(), |
| 109 | format!("vorbis,{rate},2,{}", packets.len()), |
| 110 | "ffprobe must see the stream the replica described" |
| 111 | ); |
| 112 | assert_eq!( |
| 113 | decode_pcm(&out_path), |
| 114 | decode_pcm(&source), |
| 115 | "the shim's file must decode to the samples that went into it" |
| 116 | ); |
| 117 | let _ = std::fs::remove_file(&out_path); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | fn super_xiph_lace(headers: &[&[u8]]) -> Option<Vec<u8>> { |
| 122 | oxideav_ogg::mux::xiph_lace(headers) |
| 123 | } |