Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

clippy

Richie McIlroy5db251410f6f
diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rs
index a578103b3..9af7f85a0 100644
--- a/crates/export/src/mp4.rs
+++ b/crates/export/src/mp4.rs
@@ -346,269 +346,6 @@ impl Mp4ExportSettings {
Ok(output_path)
}
-
- async fn export_rgba(
- self,
- base: ExporterBase,
- output_size: (u32, u32),
- fps: u32,
- mut on_progress: impl FnMut(u32) -> bool + Send + 'static,
- ) -> Result<PathBuf, String> {
- let output_path = base.output_path.clone();
- let meta = &base.studio_meta;
-
- let (tx_image_data, mut video_rx) =
- tokio::sync::mpsc::channel::<(cap_rendering::RenderedFrame, u32)>(32);
- let (frame_tx, frame_rx) = std::sync::mpsc::sync_channel::<MP4Input>(32);
-
- let mut video_info =
- VideoInfo::from_raw(RawVideoFormat::Rgba, output_size.0, output_size.1, fps);
- video_info.time_base = ffmpeg::Rational::new(1, fps as i32);
-
- let audio_segments = get_audio_segments(&base.segments);
-
- let mut audio_renderer = audio_segments
- .first()
- .filter(|_| !base.project_config.audio.mute)
- .map(|_| AudioRenderer::new(audio_segments.clone()));
- let has_audio = audio_renderer.is_some();
-
- let encoder_thread = tokio::task::spawn_blocking(move || {
- trace!("Creating MP4File encoder (RGBA fallback)");
-
- let mut encoder = MP4File::init(
- "output",
- base.output_path.clone(),
- |o| {
- H264Encoder::builder(video_info)
- .with_bpp(self.effective_bpp())
- .with_export_priority()
- .with_export_settings()
- .build(o)
- },
- |o| {
- has_audio.then(|| {
- AACEncoder::init(AudioRenderer::info(), o)
- .map(|v| v.boxed())
- .map_err(Into::into)
- })
- },
- )
- .map_err(|v| v.to_string())?;
-
- info!("Created MP4File encoder (RGBA fallback, export settings)");
-
- let mut encoded_frames = 0u32;
- let encode_start = std::time::Instant::now();
-
- while let Ok(frame) = frame_rx.recv() {
- encoder
- .queue_video_frame(frame.video, Duration::MAX)
- .map_err(|err| err.to_string())?;
- if let Some(audio) = frame.audio {
- encoder.queue_audio_frame(audio);
- }
- encoded_frames += 1;
- }
-
- let encode_elapsed = encode_start.elapsed();
- if encoded_frames > 0 {
- let encode_fps = encoded_frames as f64 / encode_elapsed.as_secs_f64().max(0.001);
- info!(
- encoded_frames = encoded_frames,
- elapsed_secs = format!("{:.2}", encode_elapsed.as_secs_f64()),
- encode_fps = format!("{:.1}", encode_fps),
- "Encoder thread finished (RGBA)"
- );
- }
-
- let res = encoder
- .finish()
- .map_err(|e| format!("Failed to finish encoding: {e}"))?;
-
- if let Err(e) = res.video_finish {
- return Err(format!("Video encoding failed: {e}"));
- }
- if let Err(e) = res.audio_finish {
- return Err(format!("Audio encoding failed: {e}"));
- }
-
- Ok::<_, String>(base.output_path)
- })
- .then(|r| async { r.map_err(|e| e.to_string()).and_then(|v| v) });
-
- let render_task = tokio::spawn({
- let project = base.project_config.clone();
- let project_path = base.project_path.clone();
- async move {
- let mut frame_count = 0;
- let mut first_frame = None;
- let sample_rate = u64::from(AudioRenderer::SAMPLE_RATE);
- let fps_u64 = u64::from(fps);
- let mut audio_sample_cursor = 0u64;
-
- let mut consecutive_timeouts = 0u32;
- const MAX_CONSECUTIVE_TIMEOUTS: u32 = 3;
-
- loop {
- let timeout_secs = if frame_count == 0 { 120 } else { 90 };
- let (frame, frame_number) = match tokio::time::timeout(
- Duration::from_secs(timeout_secs),
- video_rx.recv(),
- )
- .await
- {
- Err(_) => {
- consecutive_timeouts += 1;
-
- if consecutive_timeouts >= MAX_CONSECUTIVE_TIMEOUTS {
- tracing::error!(
- frame_count = frame_count,
- timeout_secs = timeout_secs,
- consecutive_timeouts = consecutive_timeouts,
- "Export render_task timed out {} consecutive times - aborting",
- MAX_CONSECUTIVE_TIMEOUTS
- );
- return Err(format!(
- "Export timed out {MAX_CONSECUTIVE_TIMEOUTS} times consecutively after {timeout_secs}s each waiting for frame {frame_count} - GPU/decoder may be unresponsive"
- ));
- }
-
- tracing::warn!(
- frame_count = frame_count,
- timeout_secs = timeout_secs,
- consecutive_timeouts = consecutive_timeouts,
- "Frame receive timed out, waiting for next frame..."
- );
- continue;
- }
- Ok(Some(v)) => {
- consecutive_timeouts = 0;
- v
- }
- Ok(None) => {
- tracing::debug!(
- frame_count = frame_count,
- "Render channel closed - rendering complete"
- );
- break;
- }
- };
-
- if !(on_progress)(frame_count) {
- return Err("Export cancelled".to_string());
- }
-
- if frame_count == 0 {
- first_frame = Some(frame.clone());
- if let Some(audio) = &mut audio_renderer {
- audio.set_playhead(0.0, &project);
- }
- }
-
- let audio_frame = audio_renderer.as_mut().and_then(|audio| {
- let n = u64::from(frame_number);
- let end = ((n + 1) * sample_rate) / fps_u64;
- if end <= audio_sample_cursor {
- return None;
- }
- let pts = audio_sample_cursor as i64;
- let samples = (end - audio_sample_cursor) as usize;
- audio_sample_cursor = end;
- audio.render_frame(samples, &project).map(|mut frame| {
- frame.set_pts(Some(pts));
- frame
- })
- });
-
- if frame_tx
- .send(MP4Input {
- audio: audio_frame,
- video: video_info.wrap_frame(
- &frame.data,
- frame_number as i64,
- frame.padded_bytes_per_row as usize,
- ),
- })
- .is_err()
- {
- warn!("Renderer task sender dropped. Exiting");
- return Ok(());
- }
-
- frame_count += 1;
- }
-
- drop(frame_tx);
-
- if let Some(frame) = first_frame {
- let project_path = project_path.clone();
- let screenshot_task = tokio::task::spawn_blocking(move || {
- let rgb_img = ImageBuffer::<image::Rgb<u8>, Vec<u8>>::from_raw(
- frame.width,
- frame.height,
- frame
- .data
- .chunks(frame.padded_bytes_per_row as usize)
- .flat_map(|row| {
- row[0..(frame.width * 4) as usize]
- .chunks(4)
- .flat_map(|chunk| [chunk[0], chunk[1], chunk[2]])
- })
- .collect::<Vec<_>>(),
- );
-
- let Some(rgb_img) = rgb_img else {
- return;
- };
-
- let screenshots_dir = project_path.join("screenshots");
- if std::fs::create_dir_all(&screenshots_dir).is_err() {
- return;
- }
-
- let screenshot_path = screenshots_dir.join("display.jpg");
- let _ = rgb_img.save(&screenshot_path);
- });
-
- if let Err(e) = screenshot_task.await {
- warn!("Screenshot task failed: {e}");
- }
- } else {
- warn!("No frames were processed, cannot save screenshot or thumbnail");
- }
-
- Ok::<_, String>(())
- }
- })
- .then(|r| async {
- r.map_err(|e| e.to_string())
- .and_then(|v| v.map_err(|e| e.to_string()))
- });
-
- let render_video_task = cap_rendering::render_video_to_channel(
- &base.render_constants,
- &base.project_config,
- tx_image_data,
- &base.recording_meta,
- meta,
- base.segments
- .iter()
- .map(|s| RenderSegment {
- cursor: s.cursor.clone(),
- decoders: s.decoders.clone(),
- })
- .collect(),
- fps,
- self.resolution_base,
- &base.recordings,
- )
- .then(|v| async { v.map_err(|e| e.to_string()) });
-
- tokio::try_join!(encoder_thread, render_video_task, render_task)?;
-
- Ok(output_path)
- }
}
struct FirstFrameNv12 {