Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Sync system audio start_time; minor rust refactors

Richie McIlroy4186535f6e7d
diff --git a/apps/desktop/src-tauri/src/windows.rs b/apps/desktop/src-tauri/src/windows.rs
index b6ec15f2b..06558553f 100644
--- a/apps/desktop/src-tauri/src/windows.rs
+++ b/apps/desktop/src-tauri/src/windows.rs
@@ -75,10 +75,9 @@ fn is_system_dark_mode() -> bool {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
if let Ok(key) =
hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize")
+ && let Ok(value) = key.get_value::<u32, _>("AppsUseLightTheme")
{
- if let Ok(value) = key.get_value::<u32, _>("AppsUseLightTheme") {
- return value == 0;
- }
+ return value == 0;
}
false
}
@@ -858,17 +857,17 @@ impl ShowCapWindow {
}
#[cfg(not(target_os = "macos"))]
- if let Self::InProgressRecording { .. } = self {
- if let Some(window) = self.id(app).get(app) {
- let width = 320.0;
- let height = 150.0;
- let recording_monitor = CursorMonitorInfo::get();
- let (pos_x, pos_y) = recording_monitor.bottom_center_position(width, height, 120.0);
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
- window.show().ok();
- window.set_focus().ok();
- return Ok(window);
- }
+ if let Self::InProgressRecording { .. } = self
+ && let Some(window) = self.id(app).get(app)
+ {
+ let width = 320.0;
+ let height = 150.0;
+ let recording_monitor = CursorMonitorInfo::get();
+ let (pos_x, pos_y) = recording_monitor.bottom_center_position(width, height, 120.0);
+ let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ window.show().ok();
+ window.set_focus().ok();
+ return Ok(window);
}
if !matches!(self, Self::Camera { .. } | Self::InProgressRecording { .. })
diff --git a/crates/editor/PLAYBACK-FINDINGS.md b/crates/editor/PLAYBACK-FINDINGS.md
index 32d23fca0..8cda6a229 100644
--- a/crates/editor/PLAYBACK-FINDINGS.md
+++ b/crates/editor/PLAYBACK-FINDINGS.md
@@ -355,6 +355,42 @@ The CPU RGBA→NV12 conversion was taking 15-25ms per frame for 3024x1964 resolu
---
+### Session 2026-02-15 (Playback Validation + System Audio Sync)
+
+**Goal**: Comprehensive playback benchmark validation, system audio start_time sync fix
+
+**What was done**:
+1. Ran playback validation on fragmented and MP4 recordings
+2. Verified AVAssetReader graceful fallback on directory paths (no panics)
+3. Audited all decoder `unwrap()` calls for safety
+4. Added system audio to recording start_time sync chain (studio_recording.rs)
+
+**Changes Made**:
+- `crates/recording/src/studio_recording.rs`: System audio start_time now syncs to mic (or display) when drift >30ms, matching the existing camera/display sync pattern. Improves playback alignment.
+
+**Results (MP4 Mode)**:
+- ✅ Decoder: AVAssetReader (hardware), display init=162-174ms, camera init=21-32ms
+- ✅ Playback: 283-641 fps effective (target ≥60fps)
+- ✅ Latency: avg=1.6-3.5ms, p95=2.8-5.0ms (target p95 <50ms)
+- ✅ Camera sync: 0ms drift (target <100ms)
+- ✅ Mic sync: 93ms (target <100ms)
+- 🟡 System audio: 178-195ms (inherent macOS capture latency, sync fix improves alignment)
+
+**Results (Fragmented Mode)**:
+- ✅ Decoder: FFmpeg (hardware) with VideoToolbox, display init=100ms, camera init=7ms
+- ✅ Playback: 156 fps effective (target ≥60fps)
+- ✅ Latency: avg=6.4ms, p95=9.5ms (target p95 <50ms)
+- ✅ Camera sync: 0ms drift (target <100ms)
+- ✅ Mic sync: 8.5ms (target <100ms)
+- ✅ System audio: 98ms (target <100ms)
+- ✅ AVAssetReader cleanly falls back to FFmpeg with descriptive error message
+
+**Decoder audit**: All `unwrap()` in `avassetreader.rs` eliminated. Remaining `unwrap()` calls in ffmpeg.rs and avassetreader decoder loop are on guaranteed-non-empty BTreeMap caches (safe by construction).
+
+**Stopping point**: All playback metrics healthy. System audio sync metadata fix applied.
+
+---
+
## References
- `PLAYBACK-BENCHMARKS.md` - Raw performance test data (auto-updated by test runner)
diff --git a/crates/recording/FINDINGS.md b/crates/recording/FINDINGS.md
index 0b3d99f86..b2d1e1375 100644
--- a/crates/recording/FINDINGS.md
+++ b/crates/recording/FINDINGS.md
@@ -441,6 +441,49 @@ System Audio ────┘ ├─► MP4 (macos.rs) ─
---
+### Session 2026-02-15 (Fix Attempts + System Audio Sync)
+
+**Goal**: Fix known issues: MP4 encoder warmup dropped frames and system audio timing offset
+
+**What was done**:
+1. Ran comprehensive benchmarks (MP4 cold, warm, thermal stress; fragmented)
+2. Attempted encoder warmup patience fix (increasing retry budget from 50ms to 200ms during first 3 frames)
+3. Reverted encoder warmup fix after it degraded performance (longer blocking caused pipeline backpressure)
+4. Implemented system audio start_time sync to match mic/display sync chain
+5. Verified all metrics stable after changes
+
+**Changes Made**:
+- `crates/recording/src/studio_recording.rs`: Added system audio to the start_time sync chain. System audio now syncs to mic start time (preferred) or display start time when drift >30ms, matching the existing sync pattern for camera and display. Improves playback alignment of system audio.
+
+**Encoder Warmup Investigation**:
+- Root cause: VideoToolbox hardware encoder first-frame latency (~160ms) causes `NotReadyForMore` for frames 2-5
+- Current retry budget: 100 × 500μs = 50ms. Frames during warmup are dropped after 50ms retry
+- Attempted fix: 400 × 500μs = 200ms patience for first 3 frames
+- Result: WORSE (71 frames instead of 149). Longer blocking prevented the encoder thread from draining the channel, causing capture-side drops from channel full
+- Conclusion: 50ms retry timeout is the correct safety valve. The ~3% dropped frames during warmup is the optimal tradeoff. Pre-warming the hardware encoder would require architectural changes (dummy frame encoding before recording starts)
+
+**Results (MP4 - warm run, post system audio sync fix)**:
+- ✅ Frame rate: 29.0-29.2fps (target 30±2fps)
+- ✅ Jitter: 10.3-12.4ms (target <15ms)
+- ✅ A/V sync: 0ms across all streams (target <50ms)
+- ✅ Mic timing: 90-94ms (target <100ms)
+- 🟡 Dropped frames: 2.7-3.3% (encoder warmup, not actionable without architectural changes)
+- 🟡 System audio duration: 215-259ms shorter than video (inherent macOS capture latency, cannot be fixed with metadata sync)
+
+**Results (Fragmented)**:
+- ✅ Frame rate: 29.5fps, jitter: 5.7ms, dropped: 1.3%
+- ✅ Mic timing: 13.5ms
+- 🟡 System audio duration: 111.5ms shorter
+
+**Key findings**:
+- MP4 encoder warmup spike is NOT fixable by increasing retry patience (makes it worse)
+- System audio file duration is inherently shorter due to macOS ScreenCaptureKit capture latency
+- System audio start_time metadata sync improves playback alignment but not duration measurement
+
+**Stopping point**: System audio sync metadata fix applied. Encoder warmup spike documented as architectural limitation.
+
+---
+
## References
- `BENCHMARKS.md` - Raw performance test data (auto-updated by test runner)
diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs
index 6a84faa3a..4d575f678 100644
--- a/crates/recording/src/studio_recording.rs
+++ b/crates/recording/src/studio_recording.rs
@@ -762,10 +762,28 @@ async fn stop_recording(
start_time: mic_start_time,
device_id: s.mic_device_id.clone(),
}),
- system_audio: s.pipeline.system_audio.map(|audio| AudioMeta {
- path: make_relative(&audio.path),
- start_time: Some(to_start_time(audio.first_timestamp)),
- device_id: None,
+ system_audio: s.pipeline.system_audio.map(|audio| {
+ let raw_sys_start = to_start_time(audio.first_timestamp);
+ let sys_start_time = if let Some(mic_start) = mic_start_time {
+ let sync_offset = raw_sys_start - mic_start;
+ if sync_offset.abs() > 0.030 {
+ mic_start
+ } else {
+ raw_sys_start
+ }
+ } else {
+ let sync_offset = raw_sys_start - display_start_time;
+ if sync_offset.abs() > 0.030 {
+ display_start_time
+ } else {
+ raw_sys_start
+ }
+ };
+ AudioMeta {
+ path: make_relative(&audio.path),
+ start_time: Some(sys_start_time),
+ device_id: None,
+ }
}),
cursor: s
.pipeline