N-lane compositing in playback and export
Browse files at this commit08af6bc080fd66811a99a87ecc234bf7337c33bcParent: 563175c- feat
- N-lane compositing in playback and export
5 changed files +697 -23
- crates/engine/src/audio.rs +118 -0
- crates/engine/src/export.rs +35 -14
- crates/engine/src/playback.rs +9 -6
- crates/engine/src/project.rs +233 -3
- crates/engine/tests/lanes.rs +302 -0
crates/engine/src/audio.rsmodified+118 -0
| @@ -10,6 +10,11 @@ | ||
| 10 | 10 | //! same samples either way; only the packet copy asks which reader it came from, |
| 11 | 11 | //! because there is nothing to copy out of an mp3 that an mp4 can hold. |
| 12 | 12 | //! |
| 13 | +//! One output, however many lanes: a timeline's audio lanes are decoded side by | |
| 14 | +//! side and summed ([`AudioSession::open_mixed_streams`]), which is what both | |
| 15 | +//! the device and an audio-only export are handed. One lane skips the mixer | |
| 16 | +//! entirely and is the plain single-stream path it always was. | |
| 17 | +//! | |
| 13 | 18 | //! Both readers are addressed as `(path, stream)`. An mp4 may carry several |
| 14 | 19 | //! audio tracks and the caller picks one; a standalone audio file carries |
| 15 | 20 | //! exactly one, so any stream above 0 there is an `Err` rather than a silent |
| @@ -292,6 +297,53 @@ impl AudioSession { | ||
| 292 | 297 | Ok(Some((meta, rx))) |
| 293 | 298 | } |
| 294 | 299 | |
| 300 | + /// Every audio lane of a timeline, summed into one stream: `lanes` is one | |
| 301 | + /// play list per lane, each in the shape | |
| 302 | + /// [`open_multi_streams`](Self::open_multi_streams) takes, and what comes | |
| 303 | + /// out is what the device hears -- the lanes decoded side by side and added | |
| 304 | + /// sample for sample, clamped to `[-1, 1]` so a loud pair cannot wrap the | |
| 305 | + /// output. | |
| 306 | + /// | |
| 307 | + /// **One lane is not mixed at all**: it is `open_multi_streams` verbatim, so | |
| 308 | + /// a timeline with a single audio lane decodes exactly the samples it always | |
| 309 | + /// did, down to the bit and down to the thread count. | |
| 310 | + /// | |
| 311 | + /// Lengths need not agree: a lane that has run out contributes silence to | |
| 312 | + /// what is left, which is what a shorter lane means. Each lane keeps its own | |
| 313 | + /// gap rule -- a hole in it is real silence from its own worker, not a | |
| 314 | + /// skipped chunk -- so the sum feeds the master clock a sample per sample of | |
| 315 | + /// timeline however the lanes are arranged. `start_sample` numbers from the | |
| 316 | + /// first lane's own start (see [`AudioChunk`]). | |
| 317 | + pub fn open_mixed_streams( | |
| 318 | + sources: &[(PathBuf, usize)], | |
| 319 | + lanes: &[Vec<(Option<usize>, f64, f64)>], | |
| 320 | + ) -> crate::Result<Option<(AudioMeta, Receiver<AudioChunk>)>> { | |
| 321 | + let [first, rest @ ..] = lanes else { | |
| 322 | + return Ok(None); | |
| 323 | + }; | |
| 324 | + if rest.is_empty() { | |
| 325 | + return Self::open_multi_streams(sources, first); | |
| 326 | + } | |
| 327 | + let mut meta = None; | |
| 328 | + let mut rxs = Vec::with_capacity(lanes.len()); | |
| 329 | + for segs in lanes { | |
| 330 | + // Every lane probes the same source 0, so the metas agree by | |
| 331 | + // construction; `None` from any of them is a silent timeline. | |
| 332 | + let Some((lane_meta, rx)) = Self::open_multi_streams(sources, segs)? else { | |
| 333 | + return Ok(None); | |
| 334 | + }; | |
| 335 | + meta = Some(lane_meta); | |
| 336 | + rxs.push(rx); | |
| 337 | + } | |
| 338 | + let meta = meta.expect("at least two lanes opened above"); | |
| 339 | + let (tx, rx) = sync_channel(32); | |
| 340 | + let channels = usize::from(meta.channels); | |
| 341 | + thread::Builder::new() | |
| 342 | + .name("audio-mix".into()) | |
| 343 | + .spawn(move || mix(&rxs, channels, &tx))?; | |
| 344 | + Ok(Some((meta, rx))) | |
| 345 | + } | |
| 346 | + | |
| 295 | 347 | /// Why [`open`](Self::open) came back silent for a file that *has* sound: |
| 296 | 348 | /// `Some(reason)` when there is an audio track and it is not the AAC-LC |
| 297 | 349 | /// this decodes (an AC-3 remux, say). `None` when the file is simply silent |
| @@ -1170,6 +1222,72 @@ struct Worker { | ||
| 1170 | 1222 | tx: SyncSender<AudioChunk>, |
| 1171 | 1223 | } |
| 1172 | 1224 | |
| 1225 | +/// Sums one worker per lane into one stream (see | |
| 1226 | +/// [`AudioSession::open_mixed_streams`]). Every lane starts at the same timeline | |
| 1227 | +/// position and synthesises its own gaps, so the lanes are sample-aligned by | |
| 1228 | +/// construction and a mix is an add: no resampling, no positioning, no drift to | |
| 1229 | +/// correct. What is emitted at each turn is the shortest block every still-live | |
| 1230 | +/// lane can supply, which keeps a slow lane from being outrun and never buffers | |
| 1231 | +/// more than the chunks already in flight. | |
| 1232 | +/// | |
| 1233 | +/// A lane whose channel has closed has ended and simply stops being added -- | |
| 1234 | +/// that is what "shorter lane" means here. The mix ends when all of them have. | |
| 1235 | +/// The accumulator is reused across turns; the one allocation per chunk is the | |
| 1236 | +/// buffer the channel takes ownership of, and none of this is the RT path (the | |
| 1237 | +/// device callback reads the ring the feeder has already filled). | |
| 1238 | +fn mix(rxs: &[Receiver<AudioChunk>], channels: usize, tx: &SyncSender<AudioChunk>) { | |
| 1239 | + let mut pending: Vec<Vec<f32>> = rxs.iter().map(|_| Vec::new()).collect(); | |
| 1240 | + let mut live: Vec<bool> = rxs.iter().map(|_| true).collect(); | |
| 1241 | + // Numbering follows lane 0, whose first chunk says where it starts. | |
| 1242 | + let mut base = 0; | |
| 1243 | + let mut emitted = 0; | |
| 1244 | + let mut out: Vec<f32> = Vec::new(); | |
| 1245 | + loop { | |
| 1246 | + for (i, rx) in rxs.iter().enumerate() { | |
| 1247 | + while live[i] && pending[i].is_empty() { | |
| 1248 | + match rx.recv() { | |
| 1249 | + Ok(chunk) => { | |
| 1250 | + if i == 0 && emitted == 0 { | |
| 1251 | + base = chunk.start_sample; | |
| 1252 | + } | |
| 1253 | + pending[i] = chunk.samples; | |
| 1254 | + } | |
| 1255 | + Err(_) => live[i] = false, | |
| 1256 | + } | |
| 1257 | + } | |
| 1258 | + } | |
| 1259 | + // The block every lane still playing can cover; `None` once they are all | |
| 1260 | + // done, which is the only way out of this loop besides a gone consumer. | |
| 1261 | + let Some(frames) = pending | |
| 1262 | + .iter() | |
| 1263 | + .filter(|p| !p.is_empty()) | |
| 1264 | + .map(|p| p.len() / channels) | |
| 1265 | + .min() | |
| 1266 | + .filter(|&f| f > 0) | |
| 1267 | + else { | |
| 1268 | + return; | |
| 1269 | + }; | |
| 1270 | + let block = frames * channels; | |
| 1271 | + out.clear(); | |
| 1272 | + out.resize(block, 0.0); | |
| 1273 | + for lane in pending.iter_mut().filter(|p| !p.is_empty()) { | |
| 1274 | + for (sum, sample) in out.iter_mut().zip(lane.drain(..block)) { | |
| 1275 | + *sum += sample; | |
| 1276 | + } | |
| 1277 | + } | |
| 1278 | + let chunk = AudioChunk { | |
| 1279 | + start_sample: base + emitted, | |
| 1280 | + // Clamped, not wrapped: two lanes at full scale are what a mix has | |
| 1281 | + // to survive, and the device takes `[-1, 1]`. | |
| 1282 | + samples: out.iter().map(|s| s.clamp(-1.0, 1.0)).collect(), | |
| 1283 | + }; | |
| 1284 | + if tx.send(chunk).is_err() { | |
| 1285 | + return; // caller moved on | |
| 1286 | + } | |
| 1287 | + emitted += frames as u64; | |
| 1288 | + } | |
| 1289 | +} | |
| 1290 | + | |
| 1173 | 1291 | fn run(mut w: Worker) { |
| 1174 | 1292 | let mut interleaved = Vec::new(); |
| 1175 | 1293 | let channels = w.channels; |
crates/engine/src/export.rsmodified+35 -14
| @@ -31,7 +31,7 @@ use crate::audio::{AudioMeta, AudioSession}; | ||
| 31 | 31 | use crate::demux::{Demuxer, VideoMeta}; |
| 32 | 32 | use crate::hw::{HwEncoder, HwSession}; |
| 33 | 33 | use crate::mux::{AudioParams, Mp4Muxer, VideoParams}; |
| 34 | -use crate::project::{Lane, Project}; | |
| 34 | +use crate::project::Project; | |
| 35 | 35 | |
| 36 | 36 | /// Progress is reported in permille: an atomic integer the render loop can read |
| 37 | 37 | /// without a lock, fine enough for any progress bar. |
| @@ -51,7 +51,9 @@ const MAX_BITRATE: u64 = 20_000_000; | ||
| 51 | 51 | /// front-end says so rather than hiding the rows. |
| 52 | 52 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 53 | 53 | pub enum Format { |
| 54 | - /// Picture and sound: video re-encoded, AAC copied. | |
| 54 | + /// Picture and sound: video re-encoded, AAC copied. Copied, so it carries | |
| 55 | + /// **one** audio lane: a timeline whose sound is spread over two is refused | |
| 56 | + /// by name here and mixed by the other two formats, which decode. | |
| 55 | 57 | #[default] |
| 56 | 58 | Mp4, |
| 57 | 59 | /// The audio lane alone, 16-bit PCM. |
| @@ -210,6 +212,22 @@ fn run( | ||
| 210 | 212 | // The stream each source is copied from is the one it plays: `audio_sources` |
| 211 | 213 | // is the same list `PlaybackSession::seek` hands the decoder, so an export |
| 212 | 214 | // of a timeline playing a file's second audio track carries *that* track. |
| 215 | + // | |
| 216 | + // One lane, because this path *copies* AAC packets and a mix is not a copy: | |
| 217 | + // summing two lanes means decoding both and encoding the result, and there | |
| 218 | + // is no AAC encoder here to encode it with. Refused by name rather than | |
| 219 | + // silently exporting the first lane -- a file missing half its sound is the | |
| 220 | + // one failure a user would not notice. | |
| 221 | + let lanes = project.audio_segments_from(0, meta.frame_rate); | |
| 222 | + if lanes.len() > 1 { | |
| 223 | + return Err(format!( | |
| 224 | + "this timeline has {} audio lanes and an mp4 export copies one: \ | |
| 225 | + there is no AAC encoder here to mix them with. Export WAV or FLAC, \ | |
| 226 | + which are mixed, or put the sound on one lane", | |
| 227 | + lanes.len() | |
| 228 | + ) | |
| 229 | + .into()); | |
| 230 | + } | |
| 213 | 231 | let audio = AudioSession::copy_multi_streams( |
| 214 | 232 | &project.audio_sources(), |
| 215 | 233 | &project.segments_from(0, meta.frame_rate), |
| @@ -223,10 +241,12 @@ fn run( | ||
| 223 | 241 | let mut muxer = None; |
| 224 | 242 | let mut done = 0u32; |
| 225 | 243 | let black = Black::new(meta); |
| 226 | - // Spans, not clips: a gap in the video lane is part of the timeline and | |
| 227 | - // gets encoded too, as black frames. The picture count is therefore | |
| 228 | - // `timeline_frames` however the lane is arranged. | |
| 229 | - for span in project.spans_from(Lane::V1, 0) { | |
| 244 | + // Spans, not clips: a gap in the video is part of the timeline and gets | |
| 245 | + // encoded too, as black frames. The picture count is therefore | |
| 246 | + // `timeline_frames` however the lanes are arranged -- and *which* lane a | |
| 247 | + // span comes from is `composite_spans_from`'s answer, the same one playback | |
| 248 | + // shows, so an export is what was watched. | |
| 249 | + for span in project.composite_spans_from(0) { | |
| 230 | 250 | // Every clip reopens its own source file at its own in point; the |
| 231 | 251 | // encoder is *not* reopened, so the export is one continuous stream |
| 232 | 252 | // whose GOP boundaries need not line up with the cuts -- nor with the |
| @@ -279,13 +299,14 @@ fn run( | ||
| 279 | 299 | Ok(()) |
| 280 | 300 | } |
| 281 | 301 | |
| 282 | -/// The audio lane alone, as a WAV or a FLAC. | |
| 302 | +/// The audio lanes alone, summed, as a WAV or a FLAC. | |
| 283 | 303 | /// |
| 284 | -/// Nothing here re-derives the edit: the play list is | |
| 285 | -/// [`Project::segments_from`] and it is handed to the very | |
| 286 | -/// [`AudioSession::open_multi_streams`] playback feeds from, so what is written | |
| 287 | -/// is what was heard -- gaps included, which arrive as real silence from that | |
| 288 | -/// choke point rather than as a hole this would have to invent. | |
| 304 | +/// Nothing here re-derives the edit: the play lists are | |
| 305 | +/// [`Project::audio_segments_from`] and they are handed to the very | |
| 306 | +/// [`AudioSession::open_mixed_streams`] playback feeds from, so what is written | |
| 307 | +/// is what was heard -- every lane, mixed the same way, gaps included, which | |
| 308 | +/// arrive as real silence from that choke point rather than as a hole this would | |
| 309 | +/// have to invent. | |
| 289 | 310 | /// |
| 290 | 311 | /// The length is the *timeline's*: each segment resolves its window to whole |
| 291 | 312 | /// samples on its own, so the sum can miss the timeline by a sample or two, and |
| @@ -300,8 +321,8 @@ fn run_audio( | ||
| 300 | 321 | format: Format, |
| 301 | 322 | ) -> crate::Result<()> { |
| 302 | 323 | let sources = project.audio_sources(); |
| 303 | - let segs = project.segments_from(0, meta.frame_rate); | |
| 304 | - let Some((audio, chunks)) = AudioSession::open_multi_streams(&sources, &segs)? else { | |
| 324 | + let segs = project.audio_segments_from(0, meta.frame_rate); | |
| 325 | + let Some((audio, chunks)) = AudioSession::open_mixed_streams(&sources, &segs)? else { | |
| 305 | 326 | return Err("this timeline has no audio to export".into()); |
| 306 | 327 | }; |
| 307 | 328 | let channels = usize::from(audio.channels); |
crates/engine/src/playback.rsmodified+9 -6
| @@ -149,7 +149,7 @@ impl PlaybackSession { | ||
| 149 | 149 | // One clip per lane covering the file, so timeline == source until the |
| 150 | 150 | // first edit -- and the range opened above is exactly that clip's. |
| 151 | 151 | let project = Project::single(&path, meta.frame_count); |
| 152 | - let span = project.span_at(Lane::V1, 0).expect("never empty"); | |
| 152 | + let span = project.composite_span_at(0).expect("never empty"); | |
| 153 | 153 | Ok(Self { |
| 154 | 154 | meta, |
| 155 | 155 | frames: stream.frames, |
| @@ -247,7 +247,7 @@ impl PlaybackSession { | ||
| 247 | 247 | |
| 248 | 248 | let playhead = doc.playhead; |
| 249 | 249 | let project = Project::from_parts(doc.sources, doc.lanes)?; |
| 250 | - let span = project.span_at(Lane::V1, 0).expect("never empty"); | |
| 250 | + let span = project.composite_span_at(0).expect("never empty"); | |
| 251 | 251 | // Last, because it is the one thing here that cannot be taken back: the |
| 252 | 252 | // feeder thread outlives the `Audio` value (it holds its own clones) and |
| 253 | 253 | // only a session's `drop` retires it, so a refusal above this line would |
| @@ -364,7 +364,7 @@ impl PlaybackSession { | ||
| 364 | 364 | /// clip under the running worker and only the mapping stays true. |
| 365 | 365 | fn next_clip(&mut self) -> bool { |
| 366 | 366 | let next = self.span.end(); |
| 367 | - let Some(span) = self.project.span_at(Lane::V1, next) else { | |
| 367 | + let Some(span) = self.project.composite_span_at(next) else { | |
| 368 | 368 | return false; |
| 369 | 369 | }; |
| 370 | 370 | // We only get here on a disconnect, so the old worker has already |
| @@ -757,7 +757,7 @@ impl PlaybackSession { | ||
| 757 | 757 | // `target` is inside the timeline (never empty), so this always spans; |
| 758 | 758 | // the span runs from there to the end of whatever it landed in -- a |
| 759 | 759 | // clip, or a gap, which starts a black-frame worker instead. |
| 760 | - if let Some(span) = self.project.span_at(Lane::V1, target) { | |
| 760 | + if let Some(span) = self.project.composite_span_at(target) { | |
| 761 | 761 | self.start_span(span); |
| 762 | 762 | } |
| 763 | 763 | self.eos = false; |
| @@ -775,11 +775,14 @@ impl PlaybackSession { | ||
| 775 | 775 | // between clips are gapless: the video reopens at a boundary, the |
| 776 | 776 | // ear never hears it -- and a join between two *files* is just |
| 777 | 777 | // another segment, because every segment names its own source. |
| 778 | - let segs = self.project.segments_from(target, fps); | |
| 778 | + // One play list per audio lane, summed by the mixer: what the ear | |
| 779 | + // hears is every lane at once, and a lane's own gaps are silence in | |
| 780 | + // it rather than a hole in the count the master clock keeps. | |
| 781 | + let segs = self.project.audio_segments_from(target, fps); | |
| 779 | 782 | // Each source on the stream it was placed with: what plays is what |
| 780 | 783 | // the library row said, and what an export copies (`export::run`). |
| 781 | 784 | let sources = self.project.audio_sources(); |
| 782 | - audio_running = match AudioSession::open_multi_streams(&sources, &segs) { | |
| 785 | + audio_running = match AudioSession::open_mixed_streams(&sources, &segs) { | |
| 783 | 786 | Ok(Some((_, rx))) => audio.spawn_feeder(rx), |
| 784 | 787 | _ => false, |
| 785 | 788 | }; |
crates/engine/src/project.rsmodified+233 -3
| @@ -15,8 +15,10 @@ | ||
| 15 | 15 | //! splice of the list; |
| 16 | 16 | //! * the lanes are independent and are peers: deleting audio under a picture is |
| 17 | 17 | //! one lane's business, and the picture does not shift. No lane is special |
| 18 | -//! except by what a *caller* reads -- playback and export take `V1`, the audio | |
| 19 | -//! worker takes `A1`, until a compositing slice teaches them the rest. | |
| 18 | +//! except by what a *caller* reads -- and what playback and export read is the | |
| 19 | +//! whole stack: [`Project::composite_span_at`] resolves the video lanes to the | |
| 20 | +//! one picture that shows, [`Project::audio_segments_from`] hands every audio | |
| 21 | +//! lane over to be summed. | |
| 20 | 22 | //! |
| 21 | 23 | //! The price is one invariant this module enforces at every mutation and every |
| 22 | 24 | //! constructor: within a lane, clips are **sorted by `start` and never overlap** |
| @@ -532,6 +534,80 @@ impl Project { | ||
| 532 | 534 | out |
| 533 | 535 | } |
| 534 | 536 | |
| 537 | + /// What the *composite* shows from `timeline_frame` on -- the answer | |
| 538 | + /// [`span_at`](Project::span_at) gives for one lane, resolved across every | |
| 539 | + /// video lane by one rule: **the topmost lane with a clip there wins**, and | |
| 540 | + /// topmost is the *last* video lane in display order, so `V2` covers `V1` | |
| 541 | + /// (the usual overlay convention). A frame no video lane covers is a gap, | |
| 542 | + /// which is black however many lanes are above or below it. | |
| 543 | + /// | |
| 544 | + /// No blending, so exactly one lane is visible at a time and the composite | |
| 545 | + /// is itself a span list -- which is why one decoder plays it, the same one | |
| 546 | + /// a single-lane timeline uses, rather than N running at once. | |
| 547 | + /// | |
| 548 | + /// The span is cut short wherever a *higher* lane's clip starts, because | |
| 549 | + /// from that frame on a different lane wins; that is what makes walking | |
| 550 | + /// [`composite_spans_from`](Project::composite_spans_from) correct. | |
| 551 | + /// | |
| 552 | + /// ponytail: alpha, opacity or any blend mode makes this untrue -- two | |
| 553 | + /// lanes would then be visible in one frame and the caller needs a decoder | |
| 554 | + /// per lane plus a compositor. Upgrade path is a `Vec<Span>` per frame | |
| 555 | + /// (this same walk, per lane) feeding N decoders. | |
| 556 | + pub fn composite_span_at(&self, timeline_frame: u32) -> Option<Span> { | |
| 557 | + let total = self.timeline_frames(); | |
| 558 | + if timeline_frame >= total { | |
| 559 | + return None; | |
| 560 | + } | |
| 561 | + let video: Vec<Lane> = self | |
| 562 | + .lanes() | |
| 563 | + .into_iter() | |
| 564 | + .filter(|l| l.kind == LaneKind::Video) | |
| 565 | + .collect(); | |
| 566 | + // The last covering lane is the winner; only the lanes above it can | |
| 567 | + // interrupt what it shows. | |
| 568 | + let winner = video | |
| 569 | + .iter() | |
| 570 | + .rposition(|&lane| at(self.lane(lane), timeline_frame).is_some()); | |
| 571 | + let (from, until) = match winner { | |
| 572 | + Some(i) => { | |
| 573 | + let clips = self.lane(video[i]); | |
| 574 | + let idx = at(clips, timeline_frame).expect("just found above"); | |
| 575 | + ( | |
| 576 | + Some(( | |
| 577 | + clips[idx].source, | |
| 578 | + clips[idx].in_frame + (timeline_frame - clips[idx].start), | |
| 579 | + )), | |
| 580 | + clips[idx].end(), | |
| 581 | + ) | |
| 582 | + } | |
| 583 | + // Nothing covers it: black until something above the floor starts. | |
| 584 | + None => (None, total), | |
| 585 | + }; | |
| 586 | + let takeover = video[winner.map_or(0, |i| i + 1)..] | |
| 587 | + .iter() | |
| 588 | + .filter_map(|&lane| self.lane(lane).iter().find(|c| c.start > timeline_frame)) | |
| 589 | + .map(|c| c.start) | |
| 590 | + .min(); | |
| 591 | + Some(Span { | |
| 592 | + start: timeline_frame, | |
| 593 | + len: takeover.map_or(until, |t| t.min(until)) - timeline_frame, | |
| 594 | + from, | |
| 595 | + }) | |
| 596 | + } | |
| 597 | + | |
| 598 | + /// [`spans_from`](Project::spans_from) over the composite: every stretch the | |
| 599 | + /// viewer sees from `timeline_frame` to the end, in order. What an export | |
| 600 | + /// encodes and what playback walks at a clip boundary. | |
| 601 | + pub fn composite_spans_from(&self, timeline_frame: u32) -> Vec<Span> { | |
| 602 | + let mut out = Vec::new(); | |
| 603 | + let mut t = timeline_frame; | |
| 604 | + while let Some(span) = self.composite_span_at(t) { | |
| 605 | + t = span.end(); | |
| 606 | + out.push(span); | |
| 607 | + } | |
| 608 | + out | |
| 609 | + } | |
| 610 | + | |
| 535 | 611 | /// Split every lane at `timeline_frame`, so that frame becomes the first |
| 536 | 612 | /// frame of a new clip, and hand the two sides two fresh group ids. Refused |
| 537 | 613 | /// (`false`, no change) when no lane has a clip to split there -- at a |
| @@ -752,10 +828,20 @@ impl Project { | ||
| 752 | 828 | /// The first entry is partial when the position is mid-clip. Empty when the |
| 753 | 829 | /// position is past the end or `fps` is not usable. |
| 754 | 830 | pub fn segments_from(&self, timeline_frame: u32, fps: f64) -> Vec<(Option<usize>, f64, f64)> { |
| 831 | + self.lane_segments_from(Lane::A1, timeline_frame, fps) | |
| 832 | + } | |
| 833 | + | |
| 834 | + /// [`segments_from`](Project::segments_from) for a named lane. | |
| 835 | + pub fn lane_segments_from( | |
| 836 | + &self, | |
| 837 | + lane: Lane, | |
| 838 | + timeline_frame: u32, | |
| 839 | + fps: f64, | |
| 840 | + ) -> Vec<(Option<usize>, f64, f64)> { | |
| 755 | 841 | if !(fps.is_finite() && fps > 0.0) { |
| 756 | 842 | return Vec::new(); |
| 757 | 843 | } |
| 758 | - self.spans_from(Lane::A1, timeline_frame) | |
| 844 | + self.spans_from(lane, timeline_frame) | |
| 759 | 845 | .iter() |
| 760 | 846 | .map(|span| match span.from { |
| 761 | 847 | // A clip's window is in *source* seconds: where in the file it |
| @@ -771,6 +857,32 @@ impl Project { | ||
| 771 | 857 | .collect() |
| 772 | 858 | } |
| 773 | 859 | |
| 860 | + /// One play list per audio lane that *holds* something, in display order -- | |
| 861 | + /// what the mixer sums ([`crate::AudioSession::open_mixed_streams`]) and | |
| 862 | + /// what an export asks how many lanes it would have to mix. | |
| 863 | + /// | |
| 864 | + /// Never empty: a timeline whose audio lanes are all empty (or which has no | |
| 865 | + /// audio lane at all) still yields one all-gap list, so it plays silence | |
| 866 | + /// against a master clock exactly as it did before there were lanes to | |
| 867 | + /// count. Lanes with nothing on them are left out rather than mixed in as | |
| 868 | + /// silence: they would cost a decode thread each and add zero. | |
| 869 | + pub fn audio_segments_from( | |
| 870 | + &self, | |
| 871 | + timeline_frame: u32, | |
| 872 | + fps: f64, | |
| 873 | + ) -> Vec<Vec<(Option<usize>, f64, f64)>> { | |
| 874 | + let lists: Vec<_> = self | |
| 875 | + .lanes() | |
| 876 | + .into_iter() | |
| 877 | + .filter(|l| l.kind == LaneKind::Audio && !self.lane(*l).is_empty()) | |
| 878 | + .map(|lane| self.lane_segments_from(lane, timeline_frame, fps)) | |
| 879 | + .collect(); | |
| 880 | + match lists.is_empty() { | |
| 881 | + true => vec![self.lane_segments_from(Lane::A1, timeline_frame, fps)], | |
| 882 | + false => lists, | |
| 883 | + } | |
| 884 | + } | |
| 885 | + | |
| 774 | 886 | /// Pushes the undo snapshot. Every mutating method calls this once, *after* |
| 775 | 887 | /// it has decided it will succeed -- a refusal must not cost an undo step. |
| 776 | 888 | fn snapshot(&mut self) { |
| @@ -2152,4 +2264,122 @@ mod tests { | ||
| 2152 | 2264 | } |
| 2153 | 2265 | } |
| 2154 | 2266 | } |
| 2267 | + | |
| 2268 | + /// The compositing rule, in one walk: `V2` covers `V1` while it has a clip | |
| 2269 | + /// there, `V1` shows again on either side of it, and a frame no video lane | |
| 2270 | + /// covers is a gap however many lanes there are. | |
| 2271 | + #[test] | |
| 2272 | + fn the_composite_is_the_topmost_lane_with_a_clip() { | |
| 2273 | + let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; | |
| 2274 | + let lanes = vec![ | |
| 2275 | + // V1: the whole 30 frames of source 0. | |
| 2276 | + (LaneKind::Video, vec![clip(0, 0, 30, 0)]), | |
| 2277 | + // V2: source 1 over the middle third. | |
| 2278 | + (LaneKind::Video, vec![clip(10, 5, 15, 1)]), | |
| 2279 | + (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), | |
| 2280 | + ]; | |
| 2281 | + let p = Project::from_parts(sources, lanes).expect("valid parts"); | |
| 2282 | + assert_eq!( | |
| 2283 | + p.composite_spans_from(0), | |
| 2284 | + vec![ | |
| 2285 | + Span { | |
| 2286 | + start: 0, | |
| 2287 | + len: 10, | |
| 2288 | + from: Some((0, 0)) | |
| 2289 | + }, | |
| 2290 | + Span { | |
| 2291 | + start: 10, | |
| 2292 | + len: 10, | |
| 2293 | + from: Some((1, 5)) | |
| 2294 | + }, | |
| 2295 | + Span { | |
| 2296 | + start: 20, | |
| 2297 | + len: 10, | |
| 2298 | + from: Some((0, 20)) | |
| 2299 | + }, | |
| 2300 | + ], | |
| 2301 | + "V2 takes over for its own span and hands V1 back" | |
| 2302 | + ); | |
| 2303 | + // A mid-span ask resolves the same way, which is what a seek does. | |
| 2304 | + assert_eq!( | |
| 2305 | + p.composite_span_at(15), | |
| 2306 | + Some(Span { | |
| 2307 | + start: 15, | |
| 2308 | + len: 5, | |
| 2309 | + from: Some((1, 10)) | |
| 2310 | + }) | |
| 2311 | + ); | |
| 2312 | + assert_eq!(p.composite_span_at(30), None, "past the end"); | |
| 2313 | + | |
| 2314 | + // A hole in V1 under V2's clip: black between the two, and black after | |
| 2315 | + // both run out (the audio lane is still holding the timeline open). | |
| 2316 | + let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; | |
| 2317 | + let holed = vec![ | |
| 2318 | + (LaneKind::Video, vec![clip(0, 0, 5, 0)]), | |
| 2319 | + (LaneKind::Video, vec![clip(10, 0, 10, 1)]), | |
| 2320 | + (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), | |
| 2321 | + ]; | |
| 2322 | + let p = Project::from_parts(sources, holed).expect("valid parts"); | |
| 2323 | + assert_eq!( | |
| 2324 | + p.composite_spans_from(0), | |
| 2325 | + vec![ | |
| 2326 | + Span { | |
| 2327 | + start: 0, | |
| 2328 | + len: 5, | |
| 2329 | + from: Some((0, 0)) | |
| 2330 | + }, | |
| 2331 | + Span { | |
| 2332 | + start: 5, | |
| 2333 | + len: 5, | |
| 2334 | + from: None | |
| 2335 | + }, | |
| 2336 | + Span { | |
| 2337 | + start: 10, | |
| 2338 | + len: 10, | |
| 2339 | + from: Some((1, 0)) | |
| 2340 | + }, | |
| 2341 | + Span { | |
| 2342 | + start: 20, | |
| 2343 | + len: 10, | |
| 2344 | + from: None | |
| 2345 | + }, | |
| 2346 | + ] | |
| 2347 | + ); | |
| 2348 | + | |
| 2349 | + // One video lane: the composite *is* that lane, span for span -- the | |
| 2350 | + // promise every existing two-lane project rests on. | |
| 2351 | + let p = Project::single(FILE, 30); | |
| 2352 | + assert_eq!(p.composite_spans_from(0), p.spans_from(Lane::V1, 0)); | |
| 2353 | + assert_eq!(p.composite_span_at(7), p.span_at(Lane::V1, 7)); | |
| 2354 | + } | |
| 2355 | + | |
| 2356 | + /// Every audio lane holding something gets a play list, and only those. | |
| 2357 | + #[test] | |
| 2358 | + fn audio_segments_cover_every_lane_that_holds_something() { | |
| 2359 | + let sources = vec![Source::new(FILE, 0), Source::new(FILE2, 0)]; | |
| 2360 | + let lanes = vec![ | |
| 2361 | + (LaneKind::Video, vec![clip(0, 0, 30, 0)]), | |
| 2362 | + (LaneKind::Audio, vec![clip(0, 0, 30, 0)]), | |
| 2363 | + (LaneKind::Audio, vec![clip(15, 0, 15, 1)]), | |
| 2364 | + (LaneKind::Audio, Vec::new()), | |
| 2365 | + ]; | |
| 2366 | + let p = Project::from_parts(sources, lanes).expect("valid parts"); | |
| 2367 | + let lists = p.audio_segments_from(0, FPS); | |
| 2368 | + assert_eq!(lists.len(), 2, "the empty lane is not mixed in"); | |
| 2369 | + assert_eq!(lists[0], p.segments_from(0, FPS), "A1 is unchanged"); | |
| 2370 | + // A2 is a gap up to frame 15, then the whole of source 1. | |
| 2371 | + assert_eq!(lists[1], vec![(None, 0.0, 0.5), (Some(1), 0.0, 0.5)]); | |
| 2372 | + | |
| 2373 | + // No audio lane holds anything: one all-gap list, so such a timeline | |
| 2374 | + // still plays silence against a master clock. | |
| 2375 | + let p = Project::from_parts( | |
| 2376 | + vec![Source::new(FILE, 0)], | |
| 2377 | + vec![ | |
| 2378 | + (LaneKind::Video, vec![clip(0, 0, 30, 0)]), | |
| 2379 | + (LaneKind::Audio, Vec::new()), | |
| 2380 | + ], | |
| 2381 | + ) | |
| 2382 | + .expect("valid parts"); | |
| 2383 | + assert_eq!(p.audio_segments_from(0, FPS), vec![vec![(None, 0.0, 1.0)]]); | |
| 2384 | + } | |
| 2155 | 2385 | } |
crates/engine/tests/lanes.rsadded+302 -0
| @@ -0,0 +1,302 @@ | ||
| 1 | +//! N lanes, composited: what a timeline with a `V2` over its `V1` and an `A2` | |
| 2 | +//! over its `A1` plays, and that an export of it is the same thing. | |
| 3 | +//! | |
| 4 | +//! The fixtures are the two video assets, which differ picture for picture, so | |
| 5 | +//! "which lane is showing" is answered by comparing the decoded frame against | |
| 6 | +//! each source rather than by trusting the edit list. | |
| 7 | +//! | |
| 8 | +//! ```text | |
| 9 | +//! cargo test -p engine --release --test lanes -- --nocapture | |
| 10 | +//! ``` | |
| 11 | + | |
| 12 | +use std::path::{Path, PathBuf}; | |
| 13 | +use std::sync::Once; | |
| 14 | +use std::time::{Duration, Instant}; | |
| 15 | + | |
| 16 | +use engine::export::{ExportSettings, Format}; | |
| 17 | +use engine::project::{Clip, LaneKind, Source}; | |
| 18 | +use engine::{DecodeSession, ExportHandle, PlaybackSession, Project}; | |
| 19 | + | |
| 20 | +const FPS: f64 = 30.0; | |
| 21 | +/// `test_av.mp4` is 150 frames, `test_av2.mp4` 120; the top lane covers the | |
| 22 | +/// middle third of the first. | |
| 23 | +const TOTAL: u32 = 150; | |
| 24 | +const TOP_IN: u32 = 50; | |
| 25 | +const TOP_OUT: u32 = 100; | |
| 26 | + | |
| 27 | +fn asset(name: &str) -> PathBuf { | |
| 28 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 29 | + .join("../../assets") | |
| 30 | + .join(name) | |
| 31 | +} | |
| 32 | + | |
| 33 | +/// Software on both halves, so this suite proves the composite on any machine | |
| 34 | +/// rather than whatever the local VA-API driver does. | |
| 35 | +fn pin_software() { | |
| 36 | + static PIN: Once = Once::new(); | |
| 37 | + PIN.call_once(|| unsafe { | |
| 38 | + std::env::set_var("VE_SW", "1"); | |
| 39 | + std::env::set_var("VE_SW_ENC", "1"); | |
| 40 | + }); | |
| 41 | +} | |
| 42 | + | |
| 43 | +/// Per-run unique, like the export suite's: two suites at once must not write | |
| 44 | +/// each other's files. | |
| 45 | +fn out_path(name: &str, ext: &str) -> PathBuf { | |
| 46 | + let path = std::env::temp_dir().join(format!("ve_lanes_{name}_{}.{ext}", std::process::id())); | |
| 47 | + let _ = std::fs::remove_file(&path); | |
| 48 | + path | |
| 49 | +} | |
| 50 | + | |
| 51 | +/// A hand-written v4 project: `V1` the whole of `test_av`, `V2` the middle | |
| 52 | +/// third playing `test_av2` from its own frame 0, `A1` the whole of `test_av`. | |
| 53 | +/// Exactly the file a user would write by hand, parsed by the real loader. | |
| 54 | +fn three_lane_file() -> PathBuf { | |
| 55 | + let path = out_path("project", "edith"); | |
| 56 | + // `source <audio stream> <path>`: the number is which audio track of the | |
| 57 | + // file plays, not the source index -- both of these are on their first. | |
| 58 | + let text = format!( | |
| 59 | + "edith 4\nplayhead 0\nsource 0 {}\nsource 0 {}\n\ | |
| 60 | + video 1 0 0 {TOTAL} 0 -\naudio 1 0 0 {TOTAL} 0 -\n\ | |
| 61 | + video 2 {TOP_IN} 0 {} 1 -\n", | |
| 62 | + asset("test_av.mp4").display(), | |
| 63 | + asset("test_av2.mp4").display(), | |
| 64 | + TOP_OUT - TOP_IN, | |
| 65 | + ); | |
| 66 | + std::fs::write(&path, text).expect("write the project file"); | |
| 67 | + path | |
| 68 | +} | |
| 69 | + | |
| 70 | +/// One frame of `path` by absolute index, BGRA. | |
| 71 | +fn frame_at(path: &Path, index: u32) -> Vec<u8> { | |
| 72 | + let (_, frames, _) = DecodeSession::open_at(path, index).expect("open source"); | |
| 73 | + frames.recv().expect("frame present").bgra | |
| 74 | +} | |
| 75 | + | |
| 76 | +fn mean_abs_diff(a: &[u8], b: &[u8]) -> f64 { | |
| 77 | + assert_eq!(a.len(), b.len(), "frame sizes differ"); | |
| 78 | + a.iter() | |
| 79 | + .zip(b) | |
| 80 | + .map(|(x, y)| f64::from(x.abs_diff(*y))) | |
| 81 | + .sum::<f64>() | |
| 82 | + / a.len() as f64 | |
| 83 | +} | |
| 84 | + | |
| 85 | +/// Which of the two sources `frame` came from, by picture: the near one wins, | |
| 86 | +/// and the assertion is that the other is nowhere near. | |
| 87 | +fn assert_from(frame: &[u8], source: &str, source_frame: u32, timeline: u32) { | |
| 88 | + let other = match source { | |
| 89 | + "test_av.mp4" => "test_av2.mp4", | |
| 90 | + _ => "test_av.mp4", | |
| 91 | + }; | |
| 92 | + let mine = mean_abs_diff(frame, &frame_at(&asset(source), source_frame)); | |
| 93 | + let theirs = mean_abs_diff(frame, &frame_at(&asset(other), source_frame)); | |
| 94 | + println!("timeline {timeline}: {source} {source_frame} diff {mine:.2}, {other} {theirs:.2}"); | |
| 95 | + assert!( | |
| 96 | + mine < 6.0, | |
| 97 | + "timeline frame {timeline} drifted by {mine:.2} from {source} frame {source_frame}" | |
| 98 | + ); | |
| 99 | + assert!( | |
| 100 | + theirs > 4.0 * mine.max(0.5), | |
| 101 | + "the two sources are too alike at frame {source_frame} for this test to mean anything" | |
| 102 | + ); | |
| 103 | +} | |
| 104 | + | |
| 105 | +fn wait(handle: &ExportHandle, limit: Duration) -> engine::Result<()> { | |
| 106 | + let started = Instant::now(); | |
| 107 | + while !handle.is_finished() { | |
| 108 | + assert!( | |
| 109 | + started.elapsed() < limit, | |
| 110 | + "export did not finish in {limit:?}" | |
| 111 | + ); | |
| 112 | + std::thread::sleep(Duration::from_millis(20)); | |
| 113 | + } | |
| 114 | + handle.result().expect("a finished export has an outcome") | |
| 115 | +} | |
| 116 | + | |
| 117 | +/// Plays the session up to `timeline` and hands back the frame shown there. | |
| 118 | +/// Times it, because a lane change costs exactly one decoder reopen -- the | |
| 119 | +/// composite is one span list, so N video lanes never mean N decoders. | |
| 120 | +fn frame_of(session: &mut PlaybackSession, timeline: u32) -> Vec<u8> { | |
| 121 | + let started = Instant::now(); | |
| 122 | + session.seek(f64::from(timeline) / FPS); | |
| 123 | + let deadline = Instant::now() + Duration::from_secs(20); | |
| 124 | + loop { | |
| 125 | + session.tick(); | |
| 126 | + if let Some(frame) = session.try_frame() { | |
| 127 | + assert_eq!(frame.index, timeline, "a seek shows the frame it landed on"); | |
| 128 | + println!( | |
| 129 | + "frame {timeline} ready {:.0} ms after the seek", | |
| 130 | + started.elapsed().as_secs_f64() * 1000.0 | |
| 131 | + ); | |
| 132 | + return frame.bgra; | |
| 133 | + } | |
| 134 | + assert!(Instant::now() < deadline, "no frame at timeline {timeline}"); | |
| 135 | + std::thread::sleep(Duration::from_millis(5)); | |
| 136 | + } | |
| 137 | +} | |
| 138 | + | |
| 139 | +/// The slice, from the front door: a hand-written project with a second video | |
| 140 | +/// lane opens and *plays* the top lane where it has a clip, the one under it | |
| 141 | +/// everywhere else. | |
| 142 | +#[test] | |
| 143 | +fn playback_shows_the_topmost_video_lane() { | |
| 144 | + pin_software(); | |
| 145 | + let project = three_lane_file(); | |
| 146 | + let mut session = PlaybackSession::open_project(&project).expect("open the project"); | |
| 147 | + session.set_gain(0.0); | |
| 148 | + assert_eq!(session.timeline_duration(), f64::from(TOTAL) / FPS); | |
| 149 | + | |
| 150 | + // Before the top lane starts, inside it, and after it ends. | |
| 151 | + let head = frame_of(&mut session, 10); | |
| 152 | + assert_from(&head, "test_av.mp4", 10, 10); | |
| 153 | + let over = frame_of(&mut session, TOP_IN + 10); | |
| 154 | + assert_from(&over, "test_av2.mp4", 10, TOP_IN + 10); | |
| 155 | + let tail = frame_of(&mut session, TOP_OUT + 10); | |
| 156 | + assert_from(&tail, "test_av.mp4", TOP_OUT + 10, TOP_OUT + 10); | |
| 157 | + | |
| 158 | + std::fs::remove_file(&project).unwrap(); | |
| 159 | +} | |
| 160 | + | |
| 161 | +/// Export is playback: the same project written out and decoded back shows the | |
| 162 | +/// same lane at the same frame. | |
| 163 | +#[test] | |
| 164 | +fn export_writes_the_same_composite() { | |
| 165 | + pin_software(); | |
| 166 | + let file = three_lane_file(); | |
| 167 | + let session = PlaybackSession::open_project(&file).expect("open the project"); | |
| 168 | + let out = out_path("composite", "mp4"); | |
| 169 | + | |
| 170 | + let started = Instant::now(); | |
| 171 | + let handle = session.export_to(&out); | |
| 172 | + wait(&handle, Duration::from_secs(300)).expect("export"); | |
| 173 | + println!( | |
| 174 | + "composite export: {TOTAL} frames in {:.2} s", | |
| 175 | + started.elapsed().as_secs_f64() | |
| 176 | + ); | |
| 177 | + | |
| 178 | + let (meta, _) = engine::demux::Demuxer::open(&out).expect("reopen the export"); | |
| 179 | + assert_eq!(meta.frame_count, TOTAL, "timeline frames written"); | |
| 180 | + let (_, frames) = DecodeSession::open(&out).expect("decode the export"); | |
| 181 | + let frames: Vec<Vec<u8>> = frames.into_iter().map(|f| f.bgra).collect(); | |
| 182 | + assert_eq!(frames.len() as u32, TOTAL); | |
| 183 | + assert_from(&frames[10], "test_av.mp4", 10, 10); | |
| 184 | + assert_from( | |
| 185 | + &frames[(TOP_IN + 10) as usize], | |
| 186 | + "test_av2.mp4", | |
| 187 | + 10, | |
| 188 | + TOP_IN + 10, | |
| 189 | + ); | |
| 190 | + assert_from( | |
| 191 | + &frames[(TOP_OUT + 10) as usize], | |
| 192 | + "test_av.mp4", | |
| 193 | + TOP_OUT + 10, | |
| 194 | + TOP_OUT + 10, | |
| 195 | + ); | |
| 196 | + | |
| 197 | + std::fs::remove_file(&file).unwrap(); | |
| 198 | + std::fs::remove_file(&out).unwrap(); | |
| 199 | +} | |
| 200 | + | |
| 201 | +/// `A1` and `A2` playing the *same* source range at the same place: over that | |
| 202 | +/// stretch the mix is that audio twice over, so it measures at exactly double | |
| 203 | +/// the level the one-lane export of the same timeline has there -- and outside | |
| 204 | +/// it, at the same level. The comparison is against the same window of the same | |
| 205 | +/// content, because the fixture's own level moves and comparing two *stretches* | |
| 206 | +/// would measure the fixture rather than the mixer. | |
| 207 | +#[test] | |
| 208 | +fn two_audio_lanes_are_summed() { | |
| 209 | + pin_software(); | |
| 210 | + let one_lane = Project::single(asset("test_av.mp4"), TOTAL); | |
| 211 | + let mut project = one_lane.clone(); | |
| 212 | + let doubled = Clip { | |
| 213 | + start: TOP_IN, | |
| 214 | + in_frame: TOP_IN, | |
| 215 | + out_frame: TOP_OUT, | |
| 216 | + source: 0, | |
| 217 | + link: None, | |
| 218 | + }; | |
| 219 | + let a2 = project.add_lane(LaneKind::Audio); | |
| 220 | + assert!(project.place(a2, TOP_IN, doubled), "place the second lane"); | |
| 221 | + let (sources, lanes) = project.without_orphan_sources(); | |
| 222 | + assert_eq!(lanes.len(), 3, "V1, A1 and the new A2"); | |
| 223 | + let project = Project::from_parts(sources, lanes).expect("valid parts"); | |
| 224 | + | |
| 225 | + let (meta, _) = engine::demux::Demuxer::open(&asset("test_av.mp4")).unwrap(); | |
| 226 | + let wav = |name: &str, project: Project| { | |
| 227 | + let out = out_path(name, "wav"); | |
| 228 | + let settings = ExportSettings { | |
| 229 | + format: Format::Wav, | |
| 230 | + ..Default::default() | |
| 231 | + }; | |
| 232 | + let handle = engine::export::start(project, meta, &out, &settings); | |
| 233 | + wait(&handle, Duration::from_secs(120)).expect("wav export"); | |
| 234 | + let mut reader = hound::WavReader::open(&out).expect("reopen the wav"); | |
| 235 | + let spec = reader.spec(); | |
| 236 | + let samples: Vec<f64> = reader | |
| 237 | + .samples::<i16>() | |
| 238 | + .map(|s| f64::from(s.expect("sample"))) | |
| 239 | + .collect(); | |
| 240 | + std::fs::remove_file(&out).unwrap(); | |
| 241 | + (spec, samples) | |
| 242 | + }; | |
| 243 | + let (spec, plain) = wav("one_lane", one_lane); | |
| 244 | + let (_, mixed) = wav("mixed", project.clone()); | |
| 245 | + assert_eq!(plain.len(), mixed.len(), "both are the timeline's length"); | |
| 246 | + | |
| 247 | + let rms = |samples: &[f64], from: u32, to: u32| { | |
| 248 | + let per = f64::from(spec.sample_rate) / FPS * f64::from(spec.channels); | |
| 249 | + let (a, b) = ( | |
| 250 | + (f64::from(from) * per) as usize, | |
| 251 | + (f64::from(to) * per) as usize, | |
| 252 | + ); | |
| 253 | + let window = &samples[a..b.min(samples.len())]; | |
| 254 | + (window.iter().map(|s| s * s).sum::<f64>() / window.len() as f64).sqrt() | |
| 255 | + }; | |
| 256 | + // A margin inside each region: a segment boundary lands on a packet edge, | |
| 257 | + // and this measures the middle of a stretch, not the seam. | |
| 258 | + let (over_one, over_two) = ( | |
| 259 | + rms(&plain, TOP_IN + 5, TOP_OUT - 5), | |
| 260 | + rms(&mixed, TOP_IN + 5, TOP_OUT - 5), | |
| 261 | + ); | |
| 262 | + let (out_one, out_two) = ( | |
| 263 | + rms(&plain, TOP_OUT + 5, TOTAL - 5), | |
| 264 | + rms(&mixed, TOP_OUT + 5, TOTAL - 5), | |
| 265 | + ); | |
| 266 | + println!("rms under A2: one lane {over_one:.1}, mixed {over_two:.1}"); | |
| 267 | + println!("rms past A2: one lane {out_one:.1}, mixed {out_two:.1}"); | |
| 268 | + assert!(over_one > 100.0, "the fixture has to be audible to measure"); | |
| 269 | + assert!( | |
| 270 | + (over_two / over_one - 2.0).abs() < 0.05, | |
| 271 | + "the overlap is {:.3}x one lane, not 2x", | |
| 272 | + over_two / over_one | |
| 273 | + ); | |
| 274 | + assert!( | |
| 275 | + (out_two / out_one - 1.0).abs() < 0.05, | |
| 276 | + "past the second lane the level is back to one lane's ({:.3}x)", | |
| 277 | + out_two / out_one | |
| 278 | + ); | |
| 279 | + | |
| 280 | + // The mp4 path *copies* AAC packets and cannot mix: it refuses by name | |
| 281 | + // rather than writing a file missing half its sound. | |
| 282 | + let out = out_path("mixed", "mp4"); | |
| 283 | + let handle = engine::export::start(project, meta, &out, &ExportSettings::default()); | |
| 284 | + let err = wait(&handle, Duration::from_secs(120)).expect_err("mp4 cannot mix"); | |
| 285 | + println!("mp4 refusal: {err}"); | |
| 286 | + assert!(err.to_string().contains("2 audio lanes"), "{err}"); | |
| 287 | + assert!(!out.exists(), "a refused export leaves no file"); | |
| 288 | +} | |
| 289 | + | |
| 290 | +/// A project with one lane of each kind is what it always was: the composite of | |
| 291 | +/// a single video lane is that lane, and its audio is one un-mixed stream. | |
| 292 | +#[test] | |
| 293 | +fn one_lane_projects_are_untouched() { | |
| 294 | + let project = Project::single(asset("test_av.mp4"), TOTAL); | |
| 295 | + assert_eq!( | |
| 296 | + project.composite_spans_from(0), | |
| 297 | + project.spans_from(engine::project::Lane::V1, 0) | |
| 298 | + ); | |
| 299 | + let lists = project.audio_segments_from(0, FPS); | |
| 300 | + assert_eq!(lists, vec![project.segments_from(0, FPS)]); | |
| 301 | + assert_eq!(project.sources(), [Source::new(asset("test_av.mp4"), 0)]); | |
| 302 | +} |