fix(audio): avoid capture packet drops caused by queue contention - #16146
fix(audio): avoid capture packet drops caused by queue contention#16146fufesou wants to merge 1 commit into
Conversation
Use atomic buffer ownership transfers for the capture handoff. Preserve preallocation, packet ordering, and drop-oldest behavior. Extend existing tests for paused workers, saturation, and sequence wrap. Signed-off-by: fufesou <linlong1266@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (3)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughAudio capture buffering now uses an atomic-state ChangesAudio capture buffer pool
Estimated code review effort: 4 (Complex) | ~45 minutes Merge Risk: ⚪ Minimal · up to Audio capture now transfers pooled buffer ownership atomically to avoid queue-contention packet drops. The updated coverage validates handoff behavior, retention, loss accounting, and buffer recovery, with no remaining merge-blocking risk identified. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
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. Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
|
@codexrabbitai full review |
Related
Summary
The capture callback currently drops its new PCM packet if the encoder worker holds the shared queue mutex, even when free buffers are available. The single
try_lock()keeps the callback from waiting, but can discard audio without a backlog.Replace that shared queue lock with atomic buffer ownership transfers. A worker paused while consuming or recycling one buffer no longer prevents the callback from using the remaining capacity. Actual buffer exhaustion still uses the existing drop-oldest policy.
Fix details
AtomicU64. Claiming an index transfers exclusive ownership before either thread accesses its PCM.contention_droppedlog field at zero for comparison with older builds. Genuine capacity drops remain indropped.Testing
Use a Windows or macOS machine as the audio-sending host. Linux and Android do not use this capture handoff. Compare the fix with master at
c4221469d, using the same audio device, source, receiving machine, and network.Audio capture PCM handoff lossreportsdropped,contention_dropped,oversized, andrecycle_failures. The fixed build must not report contention drops. Real saturation can still increasedropped;oversizedandrecycle_failuresshould stay zero. Loss messages are only emitted when there is something to report.Audio capture PCM handoff stats.observed_max_queued_packetsmust not exceed the existing capacity of ten. This is queued PCM only, not a measurement of end-to-end latency. A zero contention counter alone does not prove that no audio was lost.For a deterministic developer check, suspend only the
audio-encoderthread after it claims a buffer or just before it publishes a recycled buffer. Keep the capture thread running and leave other buffers free. Submission should continue using that capacity before the worker resumes. If the pause lasts long enough to exhaust the pool, older queued packets may be dropped. Resume the worker and verify that delivery continues. Suspending the whole process does not exercise this case.Validation and scope
Native macOS audio tests passed on this commit with both backends: 36 with the default backend and 34 with
use_samplerate. The strengthened paused-worker tests fail with the old handoff and pass with this implementation.The changes are confined to capture queue construction, buffer transfer/recycling, and loss accounting. Playback, resampling, encoding, and stream lifecycle code are unchanged. The packed representation supports up to twelve buffers; production still uses ten.
Windows execution and the hardware checks above are still pending. A short software comparison under CPU load had no drops with either implementation, so it does not establish the frequency or audible impact of the old contention issue. This fix does not claim to eliminate all audio interruptions.
Summary by CodeRabbit