diff --git a/crates/engine/src/audio.rs b/crates/engine/src/audio.rs index c81c8a0..d24c532 100644 --- a/crates/engine/src/audio.rs +++ b/crates/engine/src/audio.rs @@ -204,10 +204,14 @@ impl AudioSession { /// pause — the master clock counts what the device has been fed, so a hole /// that fed nothing would stall the whole timeline on it. /// + /// A segment naming a source that has **no track** is the same silence: a + /// clip of a silent file is a hole with a picture over it, and the timeline + /// let it in on exactly that footing ([`crate::PlaybackSession::import`]). + /// /// Only the sources some segment names are opened. `Ok(None)` when `sources` - /// is empty or its first entry has no AAC track; a later source that is - /// missing audio or disagrees on rate/layout is an `Err` — import refuses - /// those up front (one timeline, one output device), this is the backstop. + /// is empty or its first entry has no AAC track; a later source that + /// disagrees on rate/layout is an `Err` — import refuses those up front (one + /// timeline, one output device), this is the backstop. pub fn open_multi_segments( sources: &[PathBuf], segs: &[(Option, f64, f64)], @@ -286,11 +290,18 @@ impl AudioSession { first.check_decoder()?; let mut tracks: Vec> = sources.iter().map(|_| None).collect(); + // ...and which of them turned out to have no track at all, so a silent + // source is opened once like any other rather than once per segment + // naming it. + let mut silent: Vec = sources.iter().map(|_| false).collect(); tracks[0] = Some(first); for &(source, ..) in segs { let Some(source) = source else { continue; // a gap opens no file }; + if silent.get(source) == Some(&true) { + continue; + } let slot = tracks .get_mut(source) .ok_or_else(|| format!("segment names source {source} of {}", sources.len()))?; @@ -298,8 +309,14 @@ impl AudioSession { continue; // already opened, and the checks below already ran } let (path, stream) = &sources[source]; - let track = Track::open(path, *stream)? - .ok_or_else(|| format!("source {source} has no audio track"))?; + // A source with no audio track at all is not a failure: its + // segments are silence, exactly as a gap's are, and the slot stays + // empty to say so. That is what a silent clip on an audio lane + // means -- a picture over a hole. + let Some(track) = Track::open(path, *stream)? else { + silent[source] = true; + continue; + }; if (track.sample_rate(), track.channels()?) != (meta.sample_rate, meta.channels) { return Err(format!( "source {source} is {} Hz {} ch, the timeline is {} Hz {} ch", @@ -316,14 +333,15 @@ impl AudioSession { let segments: Vec = segs .iter() - .map(|&(source, start_secs, end_secs)| match source { - Some(source) => tracks[source] - .as_ref() - .expect("opened above") - .segment(source, start_secs, end_secs), - None => Segment::silence( - ((end_secs - start_secs).max(0.0) * f64::from(meta.sample_rate)) as u64, - ), + // A gap, and a source with no track to decode, are the same stretch + // of nothing: as many frames of silence as the window is long. + .map(|&(source, start_secs, end_secs)| { + match source.and_then(|source| Some((source, tracks[source].as_ref()?))) { + Some((source, track)) => track.segment(source, start_secs, end_secs), + None => Segment::silence( + ((end_secs - start_secs).max(0.0) * f64::from(meta.sample_rate)) as u64, + ), + } }) .collect(); // Chunk numbering is continuous across every join, counted from the first @@ -679,15 +697,18 @@ impl AudioSession { /// half a packet over the whole list however many files it spans, and the /// extra priming packet is still the very first segment's alone. /// - /// Every source must declare the same [`AacTrackParams`]: the copy becomes - /// one output track with one esds. Import refuses a mismatch up front; the - /// check here is the backstop, as is the missing-track `Err`. + /// Every source that *has* a track must declare the same + /// [`AacTrackParams`]: the copy becomes one output track with one esds. + /// Import refuses a mismatch up front; the check here is the backstop. /// /// A segment naming **no** source is a gap, and is copied as that many /// packets of [`silent_packet`] silence — the rounding debt carried through /// it like any other, so the hole occupies its exact duration and the audio /// after it stays in sync with the picture. A gap the track *opens* on gets - /// one silent packet more, which is the priming a reader drops. + /// one silent packet more, which is the priming a reader drops. A segment + /// naming a *silent* source is written the same way: a clip whose file has + /// no audio track is a hole with a picture over it, and the timeline it + /// joined already said so ([`crate::PlaybackSession::import`]). pub fn copy_multi_segments( sources: &[PathBuf], segs: &[(Option, f64, f64)], @@ -706,24 +727,34 @@ impl AudioSession { sources: &[(PathBuf, usize)], segs: &[(Option, f64, f64)], ) -> crate::Result)>> { - let Some(&(Some(first), ..)) = segs.iter().find(|s| s.0.is_some()) else { + // The output track is declared from the first source that has one -- + // not simply the first one named, which may be a silent clip. Such a + // clip defines no rate and no layout; it is written as silence below + // and the track goes on being the audible sources' track. + let mut tracks: Vec>> = sources.iter().map(|_| None).collect(); + let mut first = None; + for &(source, ..) in segs { + let Some(source) = source else { continue }; + if let Some(track) = source_at(&mut tracks, sources, source)? { + first = Some(track.track_params()); + break; + } + } + let Some(params) = first else { return Ok(None); // no segments, or nothing but silence }; - let (path, stream) = sources - .get(first) - .ok_or_else(|| format!("segment names source {first} of {}", sources.len()))?; - let Some(track) = copy_track(path, *stream)? else { - return Ok(None); // silent source, silent export - }; - let params = track.track_params(); - let mut tracks: Vec> = sources.iter().map(|_| None).collect(); - tracks[first] = Some(track); let sample_rate = params.sample_rate; let mut err = 0i64; let mut packets: Vec = Vec::new(); for &(source, start_secs, end_secs) in segs { - let Some(source) = source else { + // A gap, and a clip whose source carries no track, are the same hole + // to write: silence for as long as the window is. + let named = match source { + Some(source) => source_at(&mut tracks, sources, source)?.map(|t| (source, t)), + None => None, + }; + let Some((source, track)) = named else { let ideal = ((end_secs - start_secs).max(0.0) * f64::from(sample_rate)) as i64; let bytes = silent_packet(params.chan_conf)?; // A reader drops the first packet of an AAC track as the @@ -741,7 +772,6 @@ impl AudioSession { } continue; }; - let track = source_at(&mut tracks, sources, source)?; if track.track_params() != params { return Err(format!( "source {source} audio is {:?}, the timeline's is {params:?}", @@ -801,24 +831,25 @@ fn silent_packet(chan_conf: u8) -> crate::Result> { } } -/// The source at `index`, on the stream it names, opened on first use. Sources -/// a segment list never names are never touched: a project's list only grows. +/// The source at `index`, on the stream it names, opened on first use -- and +/// `None` for a source that has no track at all, which the caller writes as +/// silence exactly as it writes a gap. The outer `Option` is "has it been +/// opened", the inner one "was there anything in it", so a silent source is +/// opened once like any other. Sources a segment list never names are never +/// touched: a project's list only grows. fn source_at<'a>( - tracks: &'a mut [Option], + tracks: &'a mut [Option>], sources: &[(PathBuf, usize)], index: usize, -) -> crate::Result<&'a mut AacTrack> { +) -> crate::Result> { let slot = tracks .get_mut(index) .ok_or_else(|| format!("segment names source {index} of {}", sources.len()))?; if slot.is_none() { let (path, stream) = &sources[index]; - *slot = Some( - copy_track(path, *stream)? - .ok_or_else(|| format!("source {index} has no audio track"))?, - ); + *slot = Some(copy_track(path, *stream)?); } - Ok(slot.as_mut().expect("just opened")) + Ok(slot.as_mut().expect("just opened").as_mut()) } /// A source's track for the *copy* path, which needs raw AAC access units. diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 19aa574..553324d 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -1355,9 +1355,9 @@ impl PlaybackSession { /// nothing playable, so there is nothing for [`undo`](Self::undo) to take /// back, and nothing reseeks. /// - /// Refused unless it can join the timeline -- same codec, same frame rate, - /// same audio parameters or both silent; the `Err` names the property that - /// disagrees, for a caller to show. Nothing is changed by a refusal. A + /// Refused unless it can join the timeline -- same frame rate, a decoder + /// this machine can open, and audio parameters that agree or none at all; + /// the `Err` names the property that disagrees, for a caller to show. Nothing is changed by a refusal. A /// *resolution* of its own is not a refusal: the clip is placed on the /// project canvas by its fit policy ([`PlaybackSession::set_fit`]). pub fn import(&mut self, path: &Path) -> crate::Result { @@ -1661,16 +1661,18 @@ impl Drop for PlaybackSession { } /// Whether `path`, already demuxed to `meta`, may join a timeline whose -/// parameters are `timeline` and whose audio probes as `first`: same codec, -/// same frame rate, same audio parameters or both silent. +/// parameters are `timeline` and whose audio probes as `first`: same frame +/// rate, a decoder this machine can open, and audio parameters that agree (or +/// no audio at all). /// -/// *Not* the same size any more. A project has its own resolution -/// ([`PlaybackSession::set_resolution`]) and every clip is placed on it by its -/// own fit policy, so a 640x360 file joining a 1920x1080 timeline is a -/// letterboxed clip rather than a refusal. The frame rate stays refused on -/// purpose: mixing rates means resampling the *timeline*, which is a different -/// problem from resampling a picture, and a refusal is the honest answer until -/// it is solved. +/// *Not* the same size, and no longer the same codec either. A project has its +/// own resolution ([`PlaybackSession::set_resolution`]) and every clip is +/// placed on it by its own fit policy, so a 640x360 file joining a 1920x1080 +/// timeline is a letterboxed clip rather than a refusal; and every clip opens +/// its own decoder, so a mixed-codec timeline is a decoder each rather than a +/// refusal. The frame rate stays refused on purpose: mixing rates means +/// resampling the *timeline*, which is a different problem from resampling a +/// picture, and a refusal is the honest answer until it is solved. /// /// The `Err` names the property that disagrees, and those strings are what a /// front-end shows verbatim; [`PlaybackSession::import`] and @@ -1682,18 +1684,6 @@ fn matches_timeline( timeline: &VideoMeta, first: &Option, ) -> crate::Result<()> { - // Codec first: one timeline is one kind of source. Mixing them would decode - // (every clip opens its own decoder) but on a machine without the VA-API - // plugin the VP9 half would be black frames with the refusal only on - // stderr, which is exactly what a front-end cannot show. - if meta.codec != timeline.codec { - return Err(format!( - "{} does not match the timeline's {}", - meta.codec.name(), - timeline.codec.name() - ) - .into()); - } // Container frame rates are computed from timescales, so never `==`. if (meta.frame_rate - timeline.frame_rate).abs() > 0.01 { return Err(format!( @@ -1702,12 +1692,29 @@ fn matches_timeline( ) .into()); } + // The codec itself is *not* held to the timeline's any more -- every clip + // opens its own decoder, so an H.264 take and an HEVC one play on one + // timeline. What the codec gate was really protecting against stays, in the + // one place that can actually answer it: on a machine without the VA-API + // plugin an HEVC or VP9 clip would be black frames with the refusal on + // stderr alone, so the file is asked for a decoder *here*, at the door, + // where the `Err` is still something a front-end can show. A zero-length + // range probes and starts no worker, and H.264 costs nothing at all -- + // [`DecodeSession::open_worker`] only enters VA-API off that path. + DecodeSession::open_worker( + &source.path, + 0, + 0, + ColorParams::default(), + Composer::passthrough(), + )?; audio_matches(source, first) } /// Whether a candidate's audio may join a timeline whose first source probes as -/// `first`: same rate, same layout, or both silent. One output device and one -/// exported track is all there is, and no resampler. +/// `first`: same rate, same layout -- or no audio of its own at all, which is +/// silence over the clip's span and agrees with everything. One output device +/// and one exported track is all there is, and no resampler. /// /// The audio half of [`matches_timeline`], which a stream placed on its own /// ([`PlaybackSession::place_stream_at`]) has to pass while the picture it @@ -1725,9 +1732,9 @@ fn matches_timeline( /// `Ok(None)` for a timeline whose first such source is silent, and for one of /// nothing but stills. That is a silent timeline, and a file with sound is /// still refused by [`audio_matches`] in the words it always used ("the file -/// has audio, the timeline is silent") -- the same answer a silent *video* as -/// source 0 has always given. This function widens which source is asked, not -/// what the answer means. +/// has audio, the timeline is silent") -- the device was opened on source 0's +/// track and there is none to open. This function widens which source is asked, +/// not what the answer means. fn first_audio_of(sources: &[Source]) -> crate::Result> { match sources.iter().find(|s| !crate::is_image(&s.path)) { Some(first) => AudioSession::probe(&first.path, first.audio_stream), @@ -1742,12 +1749,19 @@ fn audio_matches(source: &Source, first: &Option) -> crate::R if probe == *first { return Ok(()); } - Err(match (probe, first) { - (None, _) => "the file is silent, the timeline has audio".to_string(), - (_, None) => "the file has audio, the timeline is silent".to_string(), - (Some(a), Some(b)) => format!( + // ...and a *silent* file joins whatever the timeline is: it contributes + // silence over its span, which is the very thing both audio paths already + // synthesise for a gap ([`AudioSession::open_multi_streams_speed`] plays + // it, `AudioSession::copy_multi_streams` writes it). There is no rate and + // no layout to disagree about, so there is nothing left to refuse. + let Some(probe) = probe else { + return Ok(()); + }; + Err(match first { + None => "the file has audio, the timeline is silent".to_string(), + Some(b) => format!( "audio {} Hz {} ch does not match the timeline's {} Hz {} ch", - a.sample_rate, a.channels, b.sample_rate, b.channels + probe.sample_rate, probe.channels, b.sample_rate, b.channels ), } .into()) diff --git a/crates/engine/tests/audio_export.rs b/crates/engine/tests/audio_export.rs index 416e4df..f72473d 100644 --- a/crates/engine/tests/audio_export.rs +++ b/crates/engine/tests/audio_export.rs @@ -281,6 +281,67 @@ fn a_cancelled_audio_export_leaves_no_file() { assert!(!part_path(&out).exists(), "no half-written .part either"); } +/// The samples a silent take contributes: a clip of a file with no audio track +/// is silence for exactly its span, between two clips that are heard. This is +/// what makes `PlaybackSession::import` letting such a file in honest -- the +/// export reads the same play list playback does, so a hole here is a hole in +/// the ear too. +#[test] +fn a_silent_clip_exports_as_silence_over_its_own_span() { + let clip = |start, source| Clip { + start, + in_frame: 0, + out_frame: 30, + source, + link: None, + eq: None, + color: None, + fit: FitPolicy::default(), + speed: Speed::NORMAL, + }; + let project = Project::from_parts( + vec![ + Source::new(asset("test_av.mp4"), 0), + // Video only, no audio track at all: the silent take. + Source::new(asset("test_mismatch.mp4"), 0), + ], + vec![ + (LaneKind::Video, vec![clip(0, 0), clip(30, 1), clip(60, 0)]), + (LaneKind::Audio, vec![clip(0, 0), clip(30, 1), clip(60, 0)]), + ], + Vec::new(), + Vec::new(), + ) + .expect("a project whose middle take is silent"); + + let out = out_path("silent_clip", "wav"); + let handle = engine::export::start( + project, + meta(), + &out, + &ExportSettings { + format: Format::Wav, + ..Default::default() + }, + ); + wait(&handle, Duration::from_secs(60)).expect("wav export"); + let (audio, samples) = decode(&out); + let channels = usize::from(audio.channels); + assert_eq!( + samples.len(), + (3.0 * f64::from(RATE)) as usize * channels, + "three seconds of timeline, silent take included" + ); + let heard = rms(&samples, channels, 0.05, 0.95); + let quiet = rms(&samples, channels, 1.0, 2.0); + let again = rms(&samples, channels, 2.05, 2.95); + println!("rms: heard {heard:.4} silent take {quiet:.6} heard {again:.4}"); + assert!(heard > 0.01, "the first take is audible"); + assert!(again > 0.01, "the third take is audible"); + assert_eq!(quiet, 0.0, "the silent take is exact silence, not a stall"); + std::fs::remove_file(&out).unwrap(); +} + /// A silent timeline cannot become an audio file, and says so rather than /// writing zero samples that a user would take for a broken microphone. #[test] diff --git a/crates/engine/tests/audio_multi.rs b/crates/engine/tests/audio_multi.rs index 63fd59e..6cb05aa 100644 --- a/crates/engine/tests/audio_multi.rs +++ b/crates/engine/tests/audio_multi.rs @@ -250,18 +250,22 @@ fn the_join_carries_no_second_priming_packet() { } #[test] -fn a_source_without_the_timeline_audio_is_refused() { - // Import refuses these up front (same rate, same layout, or both silent); +fn a_source_that_disagrees_on_rate_or_layout_is_refused() { + // Import refuses these up front (one output device, one set of parameters); // the copy and the decode paths refuse them again rather than mislabel one - // esds for two different tracks. - let mixed = [asset("test_av.mp4"), asset("test_mismatch.mp4")]; + // esds for two different tracks. Stream 1 of the multi-audio fixture is + // 22.05 kHz mono against the timeline's 44.1 kHz stereo. + let mixed = [ + (asset("test_av.mp4"), 0), + (asset("test_multiaudio.mp4"), 1), + ]; let segs = [(Some(0), 0.0, 1.0), (Some(1), 0.0, 1.0)]; // (`.err()`, not `unwrap_err`: neither Ok payload is `Debug`.) for e in [ - AudioSession::copy_multi_segments(&mixed, &segs).err(), - AudioSession::open_multi_segments(&mixed, &segs).err(), + AudioSession::copy_multi_streams(&mixed, &segs).err(), + AudioSession::open_multi_streams(&mixed, &segs).err(), ] - .map(|e| e.expect("a source with no audio track must be refused")) + .map(|e| e.expect("a source at another rate must be refused")) { let msg = e.to_string(); assert!(msg.contains("source 1"), "{msg}"); @@ -270,3 +274,52 @@ fn a_source_without_the_timeline_audio_is_refused() { assert!(AudioSession::copy_multi_segments(&sources(), &[(Some(2), 0.0, 1.0)]).is_err()); assert!(AudioSession::open_multi_segments(&sources(), &[(Some(2), 0.0, 1.0)]).is_err()); } + +/// A source with **no** track is not a refusal at all: its segments are the +/// same silence a gap is, in both paths. That is what a silent clip on an audio +/// lane is -- a picture over a hole -- and the timeline lets one in +/// (`PlaybackSession::import`) precisely because these two agree here. +#[test] +fn a_silent_source_is_silence_and_not_a_refusal() { + let mixed = [asset("test_av.mp4"), asset("test_mismatch.mp4")]; + let segs = [(Some(0), 0.0, 1.0), (Some(1), 0.0, 1.0), (Some(0), 0.0, 1.0)]; + + // Decoded: three seconds, the middle one exact zero, and the chunks still + // arrive contiguous (`drain` asserts that), so the clock is fed through it. + let (_, rx) = AudioSession::open_multi_segments(&mixed, &segs) + .expect("a silent source opens") + .expect("the first source has an audio track"); + let (samples, _, _) = drain(rx); + let frames = samples.len() / CHANNELS; + assert!( + (frames as i64 - 3 * RATE as i64).unsigned_abs() < PACKET, + "{frames} frames for three seconds" + ); + let second = |n: usize| { + let at = |s: usize| (s * RATE as usize * CHANNELS).min(samples.len()); + &samples[at(n)..at(n + 1)] + }; + assert!(second(0).iter().any(|s| *s != 0.0), "the first second plays"); + assert!( + second(1).iter().all(|s| *s == 0.0), + "the silent source is exact silence" + ); + assert!(second(2).iter().any(|s| *s != 0.0), "and it comes back"); + + // Copied: the same shape in packets, so an mp4 export of that timeline is + // as long as the timeline and quiet where the silent clip is. + let (params, packets) = AudioSession::copy_multi_segments(&mixed, &segs) + .expect("a silent source copies") + .expect("the timeline has an audible source"); + let copied = packets.len() as u64 * PACKET; + assert!( + (copied as i64 - 3 * RATE as i64).unsigned_abs() < 2 * PACKET, + "{copied} samples for three seconds, priming included" + ); + assert_eq!(params.sample_rate, RATE as u32, "the audible source's track"); + let silence: Vec<_> = packets[44..44 + 43].iter().map(|p| &p.bytes).collect(); + assert!( + silence.iter().all(|b| b.len() <= 7), + "the hole is written as the hand-made silent packet, not as audio" + ); +} diff --git a/crates/engine/tests/av1.rs b/crates/engine/tests/av1.rs index 7faf849..adb1296 100644 --- a/crates/engine/tests/av1.rs +++ b/crates/engine/tests/av1.rs @@ -147,17 +147,27 @@ fn the_software_path_refuses_av1_by_name() { assert!(refused.contains("plugin"), "{refused}"); } -/// A timeline is refused a source coded differently, in the same words a -/// resolution mismatch is refused in: a front-end shows the string as it is. +/// A timeline is *not* refused a source coded differently -- every clip opens +/// its own decoder. What survives is the reason the codec gate existed: on a +/// machine that cannot decode AV1 the file is refused at the door, by the +/// decoder's own name, rather than becoming a clip of black frames. #[test] -fn a_timeline_refuses_the_other_codec() { +fn a_timeline_takes_the_other_codec_or_names_the_missing_decoder() { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open test_av.mp4"); + // The no-plugin machine, forced. + // SAFETY: the suite is documented to run with --test-threads=1. + unsafe { std::env::set_var("VE_SW", "1") }; let refused = session .import(&asset("test_av1.mkv")) - .expect_err("AV1 must not join an H.264 timeline") + .expect_err("no AV1 decoder means no AV1 clip") .to_string(); - assert!(refused.contains("AV1"), "{refused}"); - assert!(refused.contains("H.264"), "{refused}"); + unsafe { std::env::remove_var("VE_SW") }; + assert_eq!(refused, Codec::Av1.needs_plugin()); + assert!( + !refused.contains("H.264"), + "the timeline's own codec is no longer a reason: {refused}" + ); + assert_eq!(session.sources().len(), 1, "a refusal left no row"); } /// The end-to-end user path: opening the file yields pictures, all of them, diff --git a/crates/engine/tests/hevc.rs b/crates/engine/tests/hevc.rs index a128ab6..a17bcd3 100644 --- a/crates/engine/tests/hevc.rs +++ b/crates/engine/tests/hevc.rs @@ -112,17 +112,35 @@ fn the_software_path_refuses_hevc_by_name() { assert!(h264, "software H.264 decode still opens"); } -/// A timeline is refused a source coded differently, in the same words a -/// resolution mismatch is refused in: a front-end shows the string as it is. +/// A timeline is *not* refused a source coded differently -- every clip opens +/// its own decoder, so H.264 and HEVC share one timeline. What survives is the +/// reason the codec gate existed: on a machine that cannot decode HEVC the file +/// is refused at the door, by the decoder's own name, rather than becoming a +/// clip of black frames with the complaint on stderr. #[test] -fn a_timeline_refuses_the_other_codec() { +fn a_timeline_takes_the_other_codec_or_names_the_missing_decoder() { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open test_av.mp4"); + // The no-plugin machine, forced: the refusal must still arrive, and it must + // be the decoder's words and not the timeline's. + // SAFETY: the suite is documented to run with --test-threads=1. + unsafe { std::env::set_var("VE_SW", "1") }; let refused = session .import(&asset("test_hevc.mp4")) - .expect_err("HEVC must not join an H.264 timeline") + .expect_err("no HEVC decoder means no HEVC clip") .to_string(); - assert!(refused.contains("HEVC"), "{refused}"); - assert!(refused.contains("H.264"), "{refused}"); + unsafe { std::env::remove_var("VE_SW") }; + assert_eq!(refused, Codec::Hevc.needs_plugin()); + assert!( + !refused.contains("H.264"), + "the timeline's own codec is no longer a reason: {refused}" + ); + assert_eq!(session.sources().len(), 1, "a refusal left no row"); + + // ...and where a decoder exists, it simply joins. + match session.import(&asset("test_hevc.mp4")) { + Ok(_) => assert_eq!(session.sources().len(), 2, "HEVC beside H.264"), + Err(e) => assert_eq!(e.to_string(), Codec::Hevc.needs_plugin()), + } } /// The end-to-end user path: opening the file yields pictures, all of them, diff --git a/crates/engine/tests/playback.rs b/crates/engine/tests/playback.rs index ea97884..06d9967 100644 --- a/crates/engine/tests/playback.rs +++ b/crates/engine/tests/playback.rs @@ -13,7 +13,7 @@ use std::time::{Duration, Instant}; use engine::export::{ExportSettings, Format}; use engine::project::Lane; -use engine::{DecodeSession, Frame, PlaybackSession}; +use engine::{Codec, DecodeSession, Frame, PlaybackSession}; fn asset(name: &str) -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -788,30 +788,31 @@ fn the_first_import_opens_a_library_over_an_empty_timeline() { assert_eq!(session.sources().len(), 1, "the row survives the undo"); } -/// One timeline, one set of parameters: everything else is refused by name and -/// changes nothing. +/// One timeline, one frame rate and one set of audio parameters: what is left +/// is refused by name and changes nothing. #[test] fn import_refuses_what_does_not_match() { let mut session = open(asset("test_av.mp4")); - // A different *codec* cannot join: one timeline is one kind of source. - let err = session - .import(&asset("test_vp9.mp4")) - .expect_err("VP9 must be refused") - .to_string(); - assert!(err.contains("VP9"), "refusal must name the codec: {err}"); + // A different *codec* is not among them any more -- every clip opens its + // own decoder. The only refusal left on that axis is a decoder this machine + // cannot open, and it is the plugin's own words, not the timeline's. + match session.import(&asset("test_vp9.mp4")) { + Ok(_) => assert_eq!(session.sources().len(), 2, "VP9 joined the H.264 timeline"), + Err(e) => assert_eq!( + e.to_string(), + Codec::Vp9.needs_plugin(), + "the only codec refusal left is the missing decoder" + ), + } + let rows = session.sources().len(); // A different resolution is no longer among the refusals -- it is placed on - // the project canvas instead -- but this file is also silent, and that is. - let err = session + // the project canvas instead -- and neither is a file with no sound: it + // contributes silence over its span. + session .import(&asset("test_mismatch.mp4")) - .expect_err("a silent file must be refused") - .to_string(); - assert!(err.contains("audio"), "refusal must name the audio: {err}"); - assert!( - !err.contains("640x360"), - "a resolution of its own is not a refusal any more: {err}" - ); + .expect("a silent 640x360 file joins a timeline with sound"); // Same codec, same audio, 25 fps: the one property left, named with both // rates. Mixing rates would mean retiming the timeline itself, and there is @@ -822,22 +823,21 @@ fn import_refuses_what_does_not_match() { .to_string(); assert_eq!(err, "25.000 fps does not match the timeline's 30.000 fps"); - // Same size and rate, no audio track: the timeline has one. - let err = session - .import(&asset("test_baseline.mp4")) - .expect_err("a silent file must be refused") - .to_string(); - assert!(err.contains("audio"), "refusal must name the audio: {err}"); - assert!( session.import(&asset("no_such_file.mp4")).is_err(), "a missing file is an error, not a panic" ); - assert_eq!(session.clip_spans().len(), 1, "a refusal changes nothing"); - assert_eq!(session.sources().len(), 1, "a refusal left a library row"); + assert_eq!(session.clip_spans().len(), 1, "an import places nothing"); + assert_eq!( + session.sources().len(), + rows + 1, + "only the silent file was taken in" + ); assert!((session.timeline_duration() - 5.0).abs() < 1e-9); - // The mirror: audio into a silent timeline is refused just as loudly. + // The mirror is still refused: the device was opened on source 0's track + // and a silent timeline has none to open, so a file with sound cannot be + // heard on one. let mut silent = open(asset("test_baseline.mp4")); let err = silent .import(&asset("test_av.mp4")) @@ -846,6 +846,30 @@ fn import_refuses_what_does_not_match() { assert!(err.contains("audio"), "refusal must name the audio: {err}"); } +/// The user path a silent take goes down: a file with no audio track lands on a +/// timeline that has one, is placed as the grouped take every video file is, +/// and plays -- pictures over its span, silence under them. The sound itself is +/// asserted on the samples in `tests/audio_export.rs`; what is asserted here is +/// that nothing about the *timeline* is refused or lost. +#[test] +fn a_silent_file_joins_a_timeline_with_sound_and_plays() { + let mut session = open(asset("test_av.mp4")); + import_and_place(&mut session, &asset("test_mismatch.mp4")); + // 150 frames of test_av, then 60 of the silent file, at 30 fps. + assert_eq!(session.clip_spans().len(), 2, "the silent take landed"); + assert!((session.timeline_duration() - 7.0).abs() < 1e-9); + // Both lanes carry it: a silent file is a normal grouped take, and its + // audio clip is the span the worker fills with silence. + assert_eq!(session.lane_clips(Lane::V1).len(), 2); + assert_eq!(session.lane_clips(Lane::A1).len(), 2); + + // ...and it decodes: a picture from inside the silent clip's span, at the + // project's resolution like every other clip. + session.seek(6.0); + let frame = next_frame(&mut session, "the silent clip"); + assert_eq!((frame.width, frame.height), session.resolution()); +} + /// The user-facing point of a project resolution: media of two sizes on one /// timeline, each composed onto the project's own canvas. /// diff --git a/crates/engine/tests/saveload.rs b/crates/engine/tests/saveload.rs index 3c287c1..5e4cf0c 100644 --- a/crates/engine/tests/saveload.rs +++ b/crates/engine/tests/saveload.rs @@ -733,7 +733,7 @@ fn malformed_files_are_numbered_errors_and_never_panics() { fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { let dir = scratch("mismatch"); copy_in(&dir, "test_av.mp4"); - std::fs::copy(asset("test_mismatch.mp4"), dir.join("test_av2.mp4")).expect("substitute"); + std::fs::copy(asset("test_25fps.mp4"), dir.join("test_av2.mp4")).expect("substitute"); let path = dir.join("swapped.edith"); std::fs::write( &path, @@ -744,13 +744,14 @@ fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { .err() .expect("a source that stopped matching must not open") .to_string(); - // The suffix is `import`'s own refusal, word for word. The substitute is - // 640x360 *and* silent; only the second of those is a refusal now -- a - // resolution of its own is placed on the project canvas. + // The suffix is `import`'s own refusal, word for word. The substitute runs + // at 25 fps, which is the property left: a resolution of its own is placed + // on the project canvas, a codec of its own opens its own decoder, and a + // file with no sound plays silence -- none of those is a refusal any more. assert_eq!( err, format!( - "source {}: the file is silent, the timeline has audio", + "source {}: 25.000 fps does not match the timeline's 30.000 fps", dir.join("test_av2.mp4").display() ) ); diff --git a/crates/engine/tests/vp9.rs b/crates/engine/tests/vp9.rs index 6ef227c..8db88fc 100644 --- a/crates/engine/tests/vp9.rs +++ b/crates/engine/tests/vp9.rs @@ -76,17 +76,33 @@ fn the_software_path_refuses_vp9_by_name() { assert!(h264, "software H.264 decode still opens"); } -/// A VP9 timeline is refused a source coded differently, in the same words a -/// resolution mismatch is refused in: a front-end shows the string as it is. +/// A timeline is *not* refused a source coded differently -- every clip opens +/// its own decoder. What survives is the reason the codec gate existed: on a +/// machine that cannot decode VP9 the file is refused at the door, by the +/// decoder's own name, rather than becoming a clip of black frames. #[test] -fn a_timeline_refuses_the_other_codec() { +fn a_timeline_takes_the_other_codec_or_names_the_missing_decoder() { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open test_av.mp4"); + // The no-plugin machine, forced. + // SAFETY: the suite is documented to run with --test-threads=1. + unsafe { std::env::set_var("VE_SW", "1") }; let refused = session .import(&asset("test_vp9.mp4")) - .expect_err("VP9 must not join an H.264 timeline") + .expect_err("no VP9 decoder means no VP9 clip") .to_string(); - assert!(refused.contains("VP9"), "{refused}"); - assert!(refused.contains("H.264"), "{refused}"); + unsafe { std::env::remove_var("VE_SW") }; + assert_eq!(refused, Codec::Vp9.needs_plugin()); + assert!( + !refused.contains("H.264"), + "the timeline's own codec is no longer a reason: {refused}" + ); + assert_eq!(session.sources().len(), 1, "a refusal left no row"); + + // ...and where a decoder exists, it simply joins. + match session.import(&asset("test_vp9.mp4")) { + Ok(_) => assert_eq!(session.sources().len(), 2, "VP9 beside H.264"), + Err(e) => assert_eq!(e.to_string(), Codec::Vp9.needs_plugin()), + } } /// The end-to-end user path: opening the file yields pictures, all of them,