diff --git a/crates/export/src/encode.rs b/crates/export/src/encode.rs index e229d40b5..68f7849f0 100644 --- a/crates/export/src/encode.rs +++ b/crates/export/src/encode.rs @@ -393,10 +393,13 @@ pub(crate) async fn movie(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut St st.advance(first_end)?; let path = job.place(job.path, 1); - let mut file = job.create(&path)?; + let mut file = job.timed(Stage::Write, || job.create(&path))?; let opts = WriterOptions::new(brand); let movie_ts = opts.movie_timescale.max(1) as i128; - let mut mux: Writer = Mp4Writer::new(&mut file, opts).map_err(mux_err)?; + let t = Instant::now(); + let mux = Mp4Writer::new(&mut file, opts).map_err(mux_err); + job.add_time(Stage::Write, t); + let mut mux: Writer = mux?; let mut vcfg = TrackConfig::new(venc.sample_entry(), rate.num as u32); if let Some(start) = venc.media_start() { // An explicit edit: the convenience `media_start` edit would subtract the B-frame delay @@ -491,18 +494,20 @@ pub(crate) async fn movie(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut St let tail = job.timed(Stage::Encode, || venc.flush())?; job.timed(Stage::Write, || write_video(&mut mux, tail))?; job.timed_if(with_audio, Stage::Audio, || write_audio_until(&mut mux, span_end, &mut cursor))?; + // Each write is timed before its error is passed on: a failed export's line keeps the time. if let Some((Audio::Aac(e), at)) = audio.as_mut() { - let t = Instant::now(); - for au in e.flush() { - mux.write_sample(*at, WriteSample { data: &au, duration: 1024, composition_offset: 0, is_sync: true }).map_err(mux_err)?; - } - job.add_time(Stage::Audio, t); + job.timed(Stage::Audio, || -> Result<()> { + for au in e.flush() { + mux.write_sample(*at, WriteSample { data: &au, duration: 1024, composition_offset: 0, is_sync: true }).map_err(mux_err)?; + } + Ok(()) + })?; } let t = Instant::now(); - let out = mux.finish().map_err(mux_err)?; - out.flush().map_err(mux_err)?; - let bytes = file.finish()?; + let finished = mux.finish().map_err(mux_err).and_then(|out| out.flush().map_err(mux_err)); job.add_time(Stage::Write, t); + finished?; + let bytes = job.timed(Stage::Write, || file.finish())?; Ok(Report { path, frames: 0, width: w, height: h, seconds: 0.0, bytes, audio: with_audio, log: None, overflow: vec![], timing: Default::default() }) } diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index 02bdbedc9..2e5327c21 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -455,8 +455,10 @@ async fn gif_export(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_ validate_gif_dimensions(w, h)?; let path = job.place(job.path, 1); - let mut file = job.create(&path)?; - let mut enc = AnimationWriter::new( + // Each write is timed before its error is passed on: a failed export's line keeps the time. + let mut file = job.timed(pipeline::Stage::EncodeWrite, || job.create(&path))?; + let t = Instant::now(); + let enc = AnimationWriter::new( &mut file, w, h, @@ -466,7 +468,9 @@ async fn gif_export(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_ speed: 10, }, ) - .map_err(|e| ExportError::Encode(e.to_string()))?; + .map_err(|e| ExportError::Encode(e.to_string())); + job.add_time(pipeline::Stage::EncodeWrite, t); + let mut enc = enc?; let rate = job.settings.rate(comp); // GIF delays are in 1/100 s: carry the rounding error so long animations keep their timing. let cs_per_frame = 100.0 * rate.den as f64 / rate.num as f64; @@ -480,22 +484,24 @@ async fn gif_export(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_ .frames(comp, (i..end).collect(), |_, img| craft_gif::write::prepare_frame(w, h, job.pixels_with(&img, comp, job.output.channels, w, h, false), 10)) .await; let t = Instant::now(); - for frame in frames { + let wrote = frames.into_iter().try_for_each(|frame| { clock += cs_per_frame; let d = (clock.round() as u64).saturating_sub(emitted).max(1); emitted += d; let frame = frame.map_err(|e| ExportError::Encode(e.to_string()))?; - enc.write_prepared_frame(frame, d.min(u16::MAX as u64) as u16).map_err(|e| ExportError::Encode(e.to_string()))?; - } + enc.write_prepared_frame(frame, d.min(u16::MAX as u64) as u16).map_err(|e| ExportError::Encode(e.to_string())) + }); // The GIF writer compresses as it writes: the two are not measured apart. job.add_time(pipeline::Stage::EncodeWrite, t); + wrote?; st.advance(end - i)?; i = end; } let t = Instant::now(); - let _ = enc.finish().map_err(|e| ExportError::Encode(e.to_string()))?; - let bytes = file.finish()?; + let finished = enc.finish().map(drop).map_err(|e| ExportError::Encode(e.to_string())); job.add_time(pipeline::Stage::EncodeWrite, t); + finished?; + let bytes = job.timed(pipeline::Stage::EncodeWrite, || file.finish())?; Ok(Report { path, frames: 0, width: w, height: h, seconds: 0.0, bytes, audio: false, log: None, overflow: vec![], timing: Default::default() }) } diff --git a/crates/export/src/webm.rs b/crates/export/src/webm.rs index 5db721bc7..b116ec198 100644 --- a/crates/export/src/webm.rs +++ b/crates/export/src/webm.rs @@ -170,7 +170,8 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta let duration_ms = frame_ms(total) as f64; let path = job.place(job.path, 1); - let mut file = job.create(&path)?; + // Each write is timed before its error is passed on: a failed export's line keeps the time. + let mut file = job.timed(Stage::Write, || job.create(&path))?; // EBML header. let mut head = vec![]; let mut eb = vec![]; @@ -229,7 +230,7 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta el(&mut tracks, TRACK_ENTRY, &a); } el(&mut head, TRACKS, &tracks); - file.write_all(&head).map_err(io)?; + job.timed(Stage::Write, || file.write_all(&head).map_err(io))?; let mut seg_len = head.len() as u64 - seg_start; // Audio: mixed in blocks, encoded in 20 ms packets. @@ -293,31 +294,37 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta job.add_time(Stage::Encode, t); let t_end = if end >= total { span_end } else { job.settings.frame_time(comp, end) }; job.timed_if(with_audio, Stage::Audio, || audio_until(t_end, &mut frames, end >= total))?; - let t = Instant::now(); - let c = cluster(&mut frames); - file.write_all(&c).map_err(io)?; - seg_len += c.len() as u64; - job.add_time(Stage::Write, t); + job.timed(Stage::Write, || -> Result<()> { + let c = cluster(&mut frames); + file.write_all(&c).map_err(io)?; + seg_len += c.len() as u64; + Ok(()) + })?; st.advance(end - i)?; i = end; } if total == 0 { let mut frames = vec![]; - audio_until(span_end, &mut frames, true)?; + job.timed_if(with_audio, Stage::Audio, || audio_until(span_end, &mut frames, true))?; if !frames.is_empty() { - let c = cluster(&mut frames); - file.write_all(&c).map_err(io)?; - seg_len += c.len() as u64; + job.timed(Stage::Write, || -> Result<()> { + let c = cluster(&mut frames); + file.write_all(&c).map_err(io)?; + seg_len += c.len() as u64; + Ok(()) + })?; } } // Patch the Segment size. let mut size = [0u8; 8]; size[0] = 0x01; size[1..].copy_from_slice(&seg_len.to_be_bytes()[1..]); - file.seek(SeekFrom::Start(size_pos)).map_err(io)?; - file.write_all(&size).map_err(io)?; - file.seek(SeekFrom::End(0)).map_err(io)?; - let bytes = file.finish()?; + let bytes = job.timed(Stage::Write, || -> Result { + file.seek(SeekFrom::Start(size_pos)).map_err(io)?; + file.write_all(&size).map_err(io)?; + file.seek(SeekFrom::End(0)).map_err(io)?; + file.finish() + })?; Ok(Report { path, frames: 0, width: w, height: h, seconds: 0.0, bytes, audio: with_audio, log: None, overflow: vec![], timing: Default::default() }) } @@ -434,12 +441,14 @@ pub(crate) fn audio_file(job: &Cx, comp: &Comp, aiff: bool, st: &mut State) -> R out.push(0); } } - let t = Instant::now(); - let path = job.place(job.path, out.len() as u64); - let mut file = job.create(&path)?; - file.write_all(&out).map_err(io)?; - let bytes = file.finish()?; - job.add_time(Stage::Write, t); + // Timed before the error is passed on: a failed export's line keeps the time. + let (path, bytes) = job.timed(Stage::Write, || -> Result<(String, u64)> { + let path = job.place(job.path, out.len() as u64); + let mut file = job.create(&path)?; + file.write_all(&out).map_err(io)?; + let bytes = file.finish()?; + Ok((path, bytes)) + })?; Ok(Report { path, frames: 0, width: 0, height: 0, seconds: 0.0, bytes, audio: true, log: None, overflow: vec![], timing: Default::default() }) } diff --git a/crates/export/src/webm_av1.rs b/crates/export/src/webm_av1.rs index a05115a8a..eb78af777 100644 --- a/crates/export/src/webm_av1.rs +++ b/crates/export/src/webm_av1.rs @@ -53,7 +53,8 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut let duration_ms = frame_ms(total) as f64; let path = job.place(job.path, 1); - let mut file = job.create(&path)?; + // Each write is timed before its error is passed on: a failed export's line keeps the time. + let mut file = job.timed(Stage::Write, || job.create(&path))?; let mut head = vec![]; let mut eb = vec![]; el_uint(&mut eb, 0x4286, 1); @@ -105,7 +106,7 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut el(&mut tracks, TRACK_ENTRY, &a); } el(&mut head, TRACKS, &tracks); - file.write_all(&head).map_err(io)?; + job.timed(Stage::Write, || file.write_all(&head).map_err(io))?; let mut seg_len = head.len() as u64 - seg_start; let (span_start, span_end) = job.settings.span(comp); @@ -167,15 +168,17 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut i = end; } if total == 0 { - audio_until(span_end, &mut blocks, true)?; - flush_blocks(&mut blocks, &mut file, &mut seg_len)?; + job.timed_if(with_audio, Stage::Audio, || audio_until(span_end, &mut blocks, true))?; + job.timed(Stage::Write, || flush_blocks(&mut blocks, &mut file, &mut seg_len))?; } let mut size = [0u8; 8]; size[0] = 0x01; size[1..].copy_from_slice(&seg_len.to_be_bytes()[1..]); - file.seek(SeekFrom::Start(size_pos)).map_err(io)?; - file.write_all(&size).map_err(io)?; - file.seek(SeekFrom::End(0)).map_err(io)?; - let bytes = file.finish()?; + let bytes = job.timed(Stage::Write, || -> Result { + file.seek(SeekFrom::Start(size_pos)).map_err(io)?; + file.write_all(&size).map_err(io)?; + file.seek(SeekFrom::End(0)).map_err(io)?; + file.finish() + })?; Ok(Report { path, frames: 0, width: w, height: h, seconds: 0.0, bytes, audio: with_audio, log: None, overflow: vec![], timing: Default::default() }) } diff --git a/crates/export/tests/it/export_timing.rs b/crates/export/tests/it/export_timing.rs index da93847b6..7315e18c1 100644 --- a/crates/export/tests/it/export_timing.rs +++ b/crates/export/tests/it/export_timing.rs @@ -134,6 +134,56 @@ fn a_failed_export_logs_its_timing_line_too() { assert!(log.contains("Timing: export failed after"), "{log}"); } +/// #3724 review: a write that fails still counts in the failed job's line. The output path is a +/// folder, so opening the output file fails; the log next to it names the writing stage. +#[test] +fn a_failed_write_keeps_its_time_in_the_log() { + use effectcraft_export::render_queue::WebmVideoCodec; + let (p, cid) = project(); + let settings = RenderSettings { time_span: TimeSpan::LengthOfComp, ..Default::default() }; + let dir = tempfile::tempdir().expect("temp dir"); + let vp9 = OutputModule::for_format(OutputFormat::WebM); + let av1 = OutputModule { webm_codec: WebmVideoCodec::Av1, ..OutputModule::for_format(OutputFormat::WebM) }; + let cases = [ + ("h264.mp4", OutputModule::for_format(OutputFormat::H264), "writing output"), + ("anim.gif", OutputModule::for_format(OutputFormat::Gif), "encoding and writing output measured together"), + ("vp9.webm", vp9, "writing output"), + ("av1.webm", av1, "writing output"), + ("wave.wav", OutputModule::for_format(OutputFormat::Wav), "writing output"), + ("aiff.aiff", OutputModule::for_format(OutputFormat::Aiff), "writing output"), + ]; + let mut misses = vec![]; + for (name, om, stage) in cases { + let out = dir.path().join(name); + std::fs::create_dir(&out).expect("a folder where the output should go"); + let path = out.to_string_lossy().into_owned(); + let job = Job { + project: &p, + footage: &NoFootage, + expr: None, + accel: None, + comp: cid, + settings: &settings, + output: &om, + path: &path, + sink: None, + options: Default::default(), + nested_switches: true, + }; + if export(&job, &mut |_| true).is_ok() { + misses.push(format!("{name}: the export did not fail")); + continue; + } + let stem = name.split('.').next().unwrap_or(name); + let log = std::fs::read_to_string(dir.path().join(format!("{stem}_RenderLog.txt"))).unwrap_or_default(); + let line = log.lines().find(|l| l.starts_with("Timing: export failed after")).unwrap_or_default(); + if !line.contains(stage) || line.contains("rendering, encoding and writing measured together") { + misses.push(format!("{name}: {stage:?} missing from {line:?}")); + } + } + assert!(misses.is_empty(), "{misses:#?}"); +} + #[test] fn a_stopped_export_has_a_summary_without_a_frame_count() { let t = effectcraft_export::render_queue::RenderTiming {