Skip to content

fix(mac): align native audio startup timestamps - #343

Open
vitaligusatinsky wants to merge 4 commits into
getopenscreen:mainfrom
vitaligusatinsky:agent/fix-macos-audio-start-delay
Open

fix(mac): align native audio startup timestamps#343
vitaligusatinsky wants to merge 4 commits into
getopenscreen:mainfrom
vitaligusatinsky:agent/fix-macos-audio-start-delay

Conversation

@vitaligusatinsky

@vitaligusatinsky vitaligusatinsky commented Aug 11, 2026

Copy link
Copy Markdown

What changed

  • Rebase the first microphone and system-audio samples onto the capture timeline when the native macOS source starts 0–250 ms late.
  • Keep negative offsets and delays above 250 ms untouched so a failed or intentionally late source is not hidden.
  • Add focused Swift tests for zero, negative, bounded, and excessive startup offsets.

Why

Real native macOS recordings showed audio beginning 159–166 ms after video even though packet cadence was stable. The offset came from per-source startup warm-up, not ongoing drift.

Impact

New native macOS recordings start audio and video on the same timeline. The correction is deliberately bounded and does not alter spacing between audio packets.

Validation

  • 4 AudioStartAlignment Swift tests pass.
  • Before: audio started 159–166 ms after video in two real recordings.
  • After: an instrumented recording starts both tracks at 0.000000.
  • AAC packet spacing remains exactly 1024/48000 (21.333 ms).

Summary by CodeRabbit

  • Bug Fixes

    • Improved audio synchronization at recording startup by compensating for brief initial delays from each audio source.
    • Preserved meaningful timing gaps and delays beyond the startup window.
    • Improved handling of multiple audio sources and late-arriving audio.
    • Added fallback behavior when an enabled source does not provide audio.
    • Improved microphone gain handling to prevent invalid audio levels.
  • Tests

    • Added automated coverage for startup alignment, source timing, invalid inputs, longer delays, and mixed audio output.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 42c0e5d9-61e6-4371-8af0-594bb5c90fa0

📥 Commits

Reviewing files that changed from the base of the PR and between 027f93b and 7a8619d.

📒 Files selected for processing (2)
  • electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift
  • electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift
🚧 Files skipped from review as they are similar to previous changes (1)
  • electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift

📝 Walkthrough

Walkthrough

The audio mixer aligns each source’s bounded startup delay to the writer session start. It waits for enabled sources before draining, supports stall fallback, and writes through readiness and append callbacks. XCTest coverage validates alignment, gain handling, and mixing behavior.

Changes

Audio start alignment

Layer / File(s) Summary
Session-based alignment implementation
electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift
The mixer validates source timestamps and indices, aligns bounded startup delays, anchors the timeline to the writer session start, and preserves later timing deltas.
Mixer draining and output writing
electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift
The mixer wires readiness and append callbacks, clamps finite microphone gain, waits for enabled sources, marks stalled sources after the tolerance period, and flushes pending output.
Alignment tests and test target
electron/native/screencapturekit/Package.swift, electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift
The package adds the XCTest target. Tests cover source waiting, stall fallback, per-source alignment, invalid indices, delayed sources, gain clamping, and sample-buffer helpers.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: 🟡 Moderate · up to 7a861

The PR changes macOS audio startup timestamps, but its focused Swift tests are not executed by the current CI path, leaving the core platform-specific behavior unverified; run those tests in CI or explicitly accept the merge-readiness gap.

Sequence Diagram(s)

sequenceDiagram
  participant AudioSource
  participant AudioTrackMixer
  participant WriterInput
  AudioSource->>AudioTrackMixer: provide audio buffer
  AudioTrackMixer->>AudioTrackMixer: align timestamp to session start
  AudioTrackMixer->>AudioTrackMixer: wait for enabled sources or stall tolerance
  AudioTrackMixer->>WriterInput: check readiness
  AudioTrackMixer->>WriterInput: append mixed output
Loading

Suggested reviewers: etiennelescot

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the change and validation but omits the template's required issue, change type, release impact, desktop impact, and Testing sections. Use the repository template and complete each required section, including issue status, change type, release impact, desktop impact, and testing details.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main macOS audio timestamp alignment change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@vitaligusatinsky
vitaligusatinsky marked this pull request as ready for review August 11, 2026 14:45
@vitaligusatinsky

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift (1)

7-76: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover the lower and inclusive warm-up boundaries.

The tests cover 160 ms and 500 ms startup delays. They do not cover a zero offset, a negative offset, or the inclusive 250 ms limit. Add cases that compensate zero and exactly 250 ms, and preserve a negative offset.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift`
around lines 7 - 76, Add boundary-focused tests alongside the existing
AudioStartAlignment tests: verify zero offset and exactly 250 ms after
sessionStart are aligned to sessionStart, while a negative offset remains
unchanged. Use AudioStartAlignment.align and retain the existing assertions for
subsequent deltas and out-of-window behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift`:
- Around line 156-169: Update the mixer’s drain readiness flow so emission and
cursor advancement remain blocked until every enabled source has supplied and
aligned its first buffer. Add a bounded fallback for an enabled source that
never delivers, then preserve the existing drain behavior once readiness is
satisfied; ensure SourceTimeline.dropFrames does not discard a late source’s
frame-zero samples. Add a mixer-level regression test covering system audio
arriving before microphone audio and verifying the microphone’s initial samples
remain at frame zero.

---

Nitpick comments:
In
`@electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift`:
- Around line 7-76: Add boundary-focused tests alongside the existing
AudioStartAlignment tests: verify zero offset and exactly 250 ms after
sessionStart are aligned to sessionStart, while a negative offset remains
unchanged. Use AudioStartAlignment.align and retain the existing assertions for
subsequent deltas and out-of-window behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 52c54a76-ec0a-4516-90ef-a1993fb44543

📥 Commits

Reviewing files that changed from the base of the PR and between a6795d2 and 00b18d1.

📒 Files selected for processing (3)
  • electron/native/screencapturekit/Package.swift
  • electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift
  • electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@vitaligusatinsky

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift (1)

6-54: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add negative-offset and exact-limit alignment tests.

Add coverage for a source timestamp before sessionStart. Add coverage for a first timestamp exactly 250 ms after sessionStart. These cases protect the lower bound and inclusive upper bound in AudioStartAlignment.align.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift`
around lines 6 - 54, Extend AudioStartAlignmentTests with cases where the first
source timestamp precedes sessionStart and where it is exactly 250 ms after
sessionStart. Assert AudioStartAlignment.align handles the negative offset and
accepts the inclusive 250 ms upper limit, matching the expected alignment
behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift`:
- Around line 123-127: The microphoneGain sanitization in both AudioTrackMixer
initializers must clamp the finite Double to the Float-representable range
before converting to Float, while retaining the existing non-finite and negative
handling. Add a regression test using Double.greatestFiniteMagnitude that
verifies initialization and mixing do not produce invalid samples or trap, and
manually smoke-test on macOS or Windows.

---

Nitpick comments:
In
`@electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift`:
- Around line 6-54: Extend AudioStartAlignmentTests with cases where the first
source timestamp precedes sessionStart and where it is exactly 250 ms after
sessionStart. Assert AudioStartAlignment.align handles the negative offset and
accepts the inclusive 250 ms upper limit, matching the expected alignment
behavior.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 32b9736a-1303-4843-a61d-eadc1049f72d

📥 Commits

Reviewing files that changed from the base of the PR and between 00b18d1 and 027f93b.

📒 Files selected for processing (2)
  • electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/AudioTrackMixer.swift
  • electron/native/screencapturekit/Tests/OpenScreenScreenCaptureKitHelperTests/AudioStartAlignmentTests.swift

@vitaligusatinsky

Copy link
Copy Markdown
Author

Follow-up commit 27a256b adds the requested startup-boundary coverage: zero offset, negative offset, and the inclusive 250 ms compensation boundary. Clean isolated SwiftPM verification passed all 9 AudioStartAlignment tests, and an independent verifier passed the exact diff.

@vitaligusatinsky
vitaligusatinsky force-pushed the agent/fix-macos-audio-start-delay branch from b843c1b to 7a8619d Compare August 14, 2026 12:41
@vitaligusatinsky
vitaligusatinsky force-pushed the agent/fix-macos-audio-start-delay branch from 7a8619d to 98af270 Compare August 14, 2026 13:09
@vitaligusatinsky

Copy link
Copy Markdown
Author

Exact-head readiness checkpoint: 98af270 is rebased onto canonical main fa9719a (0 behind / 4 feature commits ahead). Fresh Swift verification passed 10/10, the branch diff is mergeable, and independent implementation review returned GO. Landing remains externally blocked by the upstream maintainer-approval policy and Actions jobs that the repository does not authorize/run for this fork; no merge was attempted. The local task worktree/build cache has been removed because the exact branch is remote-backed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant