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.
  • 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

Warning

Review limit reached

@vitaligusatinsky, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 14 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2e805cee-9730-45e1-a01a-b053187e6c61

📥 Commits

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

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

Walkthrough

The audio mixer now 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 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 waits for enabled sources, marks stalled sources after the tolerance period, and flushes output through readiness and append callbacks.
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, and sample-buffer helpers.

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

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, rationale, impact, and validation, but it omits the required template sections and issue reference. Use the repository template headings, select the change type, release impact, and desktop impact, and provide a related issue or state why none applies.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly identifies the macOS audio startup timestamp alignment fix.
✨ 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.

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