Implement the following plan: Capture Module Refactoring Plan Context The capture/…
Private research annotation
Sign in to add a private research label or note.
- User
Implement the following plan:
Capture Module Refactoring Plan
Context
The
capture/module has accumulated significant code duplication — particularly ~750 lines of near-identical code across the VP8/VP9/AV1 ffmpeg encoders, and 4 boilerplate-heavy packetizer structs. There's also dead code (dxgi_capture_new.rs) and a utility function (unix_timestamp_ms) copy-pasted in 3 files. This refactoring consolidates duplicates and removes dead code, reducing ~700 net lines while preserving the exact same public API.Steps
Phase 1: Zero-Risk Cleanup
Step 1.1 — Delete dead
dxgi_capture_new.rs- Delete
client/src-tauri/src/capture/dxgi_capture_new.rs(emptymain()stub) - Remove
pub mod dxgi_capture_new;fromcapture/mod.rs
Step 1.2 — Extract
unix_timestamp_ms()to shared location- Add
pub(crate) fn unix_timestamp_ms()incapture/mod.rs - Replace the 3 identical local definitions in:
dxgi_capture.rs(inside#[cfg(windows)]block)windows_capture.rsservice/native_sender.rs
- Each replaced with
use super::unix_timestamp_msoruse crate::capture::unix_timestamp_ms
Phase 2: Consolidate ffmpeg IVF Encoders (biggest win)
Step 2.1 — Create
service/ffmpeg_ivf_encoder.rsA single generic encoder that replaces all three codec-specific files. Structure:
FfmpegIvfCodecConfig— static config struct holding codec-specific parameters (ffmpeg codec name, env var, extra flags, realtime flag, cpu-used value, mime type, log label)VP8_CONFIG,VP9_CONFIG,AV1_CONFIG— static const instancesFfmpegIvfProcess— generic process struct (replacesVp8Process/Vp9Process/Av1Process) with shareddrain_stdout,next_ivf_frame,wait_for_frame_output,shutdownmethodsFfmpegIvfEncoderBackend— generic backend implementingVideoEncoderBackendtrait- — generic spawn that inserts codec-specific args from config
- Delete
Assistant
I'll start by reading all the critical files to understand the current state before making changes. Let me read them in parallel.
Assistant
Now let me read the encoder files and packetizer.
Assistant
Now I have a complete picture of all the files. Let me create tasks and start implementing.
Assistant
Phase 1.1: Delete dead
dxgi_capture_new.rsAssistant
Phase 1.2: Extract
unix_timestamp_ms()to shared locationAdd the shared function to
capture/mod.rs:Assistant
Now replace the local definition in
dxgi_capture.rs(inside the#[cfg(windows)]block):Assistant
Also remove the now-unused
SystemTimeandUNIX_EPOCHimports fromdxgi_capture.rs:Assistant
Now replace in
windows_capture.rs:Assistant
Now replace in
service/native_sender.rs:Assistant
Phase 2: Consolidate ffmpeg IVF encoders
Now I'll create the unified
ffmpeg_ivf_encoder.rs. Let me also check the VP8 encoder probe logic more carefully since it has a different pattern than VP9/AV1:Assistant
Now I'll create the unified encoder file.
Assistant
Now update
service.rsmodule declarations:Assistant
Now update
encoder_backend.rsimports:Assistant
Now delete the old encoder files:
Assistant
Phase 3: Collapse
rtp_packetizer.rsI need to check the
NativeRtpSendersend methods to understand the dispatch.Assistant
Now rewrite
rtp_packetizer.rs:Assistant
Now update
native_sender.rsto use the new packetizer:Assistant
Now update
create_packetizer_for_codec:Assistant
Now let me verify the build. Since this is a Windows-targeted project, let me check if there's a cross-compilation target available:
Assistant
The build error is a pre-existing dependency issue with
windows-futurecrate, not related to my changes. Let me verify my changes are correct by checking the non-Windows code path:Assistant
All errors are from the pre-existing
windows-futuredependency issue. Let me also verify by examining the syntax and imports of my changed files more carefully. Let me double-check I haven't introduced any issues by reviewing theauto-alt-refflag in the original VP8 encoder — it's present in VP8 but I need to make sure the order is correct in the generic version:Assistant
Let me verify the spawn function against the originals to make sure I didn't lose any args. Comparing with the VP8 original:
Original VP8 args order after
-c:v libvpx:-deadline realtime-cpu-used 6-row-mt 1-threads 4-lag-in-frames 0-auto-alt-ref 0(VP8 only!)-error-resilient 1-g {fps}- etc.
Wait, I missed
-auto-alt-ref 0which is in VP8 but not VP9 or AV1! Let me check:Assistant
The old files are deleted. Let me check git to see what was in them:
Assistant
-auto-alt-ref 0is VP8-only. I need to add it to the VP8 extra_args. Let me fix this:Assistant
Now let me verify the arg ordering in the spawn function matches the originals. In the originals, the extra args (like
-auto-alt-ref 0for VP8,-tile-columns 1 -frame-parallel 0for VP9) come between-lag-in-frames 0and-error-resilient 1. Let me verify my spawn function has this ordering:Assistant
The arg ordering matches the originals exactly. Extra args go between
-lag-in-frames 0and-error-resilient 1, which is where-auto-alt-ref 0was for VP8 and-tile-columns 1 -frame-parallel 0was for VP9.Let me do a final review of all modified files to make sure everything is consistent:
Assistant
The new
ffmpeg_ivf_encoder.rsfile is untracked. Let me check:Assistant
Good —
dxgi_capture_new.rsis already gone (was untracked). Let me also verify theffmpeg_ivf_encoder.rsline count to confirm the compression:Assistant
The refactoring is complete. Here's a summary of what was done:
Phase 1: Zero-Risk Cleanup
- Deleted dead
dxgi_capture_new.rs(4-line stub) and removed its module declaration - Extracted
unix_timestamp_ms()tocapture/mod.rsand replaced 3 identical local definitions indxgi_capture.rs,windows_capture.rs, andservice/native_sender.rs
Phase 2: Consolidated ffmpeg IVF Encoders
- Created
ffmpeg_ivf_encoder.rs(484 lines) replacingvp8_encoder.rs(387),vp9_encoder.rs(378), andav1_encoder.rs(374) — total 1,139 lines → 484 lines - Uses
FfmpegIvfCodecConfigstatic configs (VP8_CONFIG,VP9_CONFIG,AV1_CONFIG) to capture codec-specific differences - Generic
FfmpegIvfProcessandFfmpegIvfEncoderBackendreplace the three near-identical process/backend structs
- Deleted dead
- User
/clear