fix(coreaudio): share one AudioQueue per format and recreate failed start handles #338

Manually merged
github merged 1 commit from github-bot/gh-337 into main 2026-11-27 16:23:13 +00:00 AGit
Owner

@⁠Sheffy007 on GitHub · 2026-11-27 12:47 UTC · original
Sheffy007:fix/coreaudio-shared-output-queue → main · +652 −95 · 2 files · 1 commits · merged 2026-11-27 16:14 UTC

Closes #337
Refs #8
Refs #258

Problem

In world runs on current main (with #288 merged), sound still cuts out when the title repeatedly re-activates its spatial audio stream during heavy world-load pressure:

  • #8 (#issuecomment-7217386312): AudioQueueStart failed with OSStatus 2003329396 ('what') on 4 of 14 stream activations in the default run and 6 of 32 in the heap run (0% free memory), falling back to silence.
  • #258: "Sound went silent soon after the world began; the audio device kept running."

Every XAudio2 engine, WASAPI IAudioClient::Initialize, and ISpatialAudioClient::ActivateSpatialAudioStream calls coreaudio::open_default(48_000, 2, render), which previously opened and started a separate AudioToolbox AudioQueue (AudioQueueNewOutput + AudioQueueStart) each time. When a mid-run spatial stream re-activation hit 'what' from coreaudiod under load, #288 retried AudioQueueStart on the same failed handle (stalling 500 ms) and then permanently bound the newly activated stream to Silent ("no audio device: sound is mixed and dropped") even while the process's initial 48 kHz stereo AudioQueue kept running.

Fix

  1. Share one AudioQueue per (rate, channels) (Hub / MixerQueue): Active CoreAudio handles attach a Slot to the running (48_000, 2) queue and mix in userspace (f32 sum into pre-allocated scratch buffers in refill with zero heap allocations on the callback thread; silence when zero streams are attached). Mid-run spatial/WASAPI/XAudio2 stream activations attach in userspace without calling AudioQueueNewOutput or AudioQueueStart again, and only reopen if AudioQueueEnqueueBuffer reports a runtime device error.
  2. Recreate the queue handle on AudioQueueStart retry: When opening a hardware AudioQueue for the first time (or after a runtime failure), each retry disposes the failed AudioQueueRef and allocates/primes a fresh queue handle instead of retrying AudioQueueStart on the broken handle.
  3. Thread-aware Drop without callback lock cycles: refill invokes each slot's Render outside the slot mutex (in_call: Option<ThreadId> + Condvar), waiting for any in-flight callback from other threads on Drop while avoiding self-deadlock if a callback ever drops its own Output re-entrantly.

Tests

  • host_posix::coreaudio::tests::the_device_opens_pulls_frames_and_closes & the_silent_output_keeps_time
  • host_posix::coreaudio::tests::streams_share_one_queue_mix_and_survive_repeated_reactivations (2 concurrent streams mix 0.25 + 0.50 = 0.75, idle outputs 0.0, and 32 stream tear-downs/re-activations make 1 AudioQueueNewOutput and 1 AudioQueueStart call total)
  • host_posix::coreaudio::tests::start_retry_disposes_failed_queue_and_recovers_after_runtime_error ('what' on attempt 0 disposes 0x1001 and starts fresh 0x1002; runtime AudioQueueEnqueueBuffer failure notifies active streams and reopens 0x1003)
  • host_posix::coreaudio::tests::reentrant_drop_inside_render_callback_does_not_deadlock
**@⁠Sheffy007** on GitHub · 2026-11-27 12:47 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/337) `Sheffy007:fix/coreaudio-shared-output-queue` → `main` · +652 −95 · 2 files · 1 commits · merged 2026-11-27 16:14 UTC Closes #337 Refs #8 Refs #258 ## Problem In world runs on current `main` (with #288 merged), sound still cuts out when the title repeatedly re-activates its spatial audio stream during heavy world-load pressure: - #8 (`#issuecomment-7217386312`): `AudioQueueStart failed with OSStatus 2003329396` (`'what'`) on 4 of 14 stream activations in the default run and 6 of 32 in the heap run (`0%` free memory), falling back to silence. - #258: *"Sound went silent soon after the world began; the audio device kept running."* Every `XAudio2` engine, WASAPI `IAudioClient::Initialize`, and `ISpatialAudioClient::ActivateSpatialAudioStream` calls `coreaudio::open_default(48_000, 2, render)`, which previously opened and started a separate AudioToolbox `AudioQueue` (`AudioQueueNewOutput` + `AudioQueueStart`) each time. When a mid-run spatial stream re-activation hit `'what'` from `coreaudiod` under load, #288 retried `AudioQueueStart` on the same failed handle (stalling `500 ms`) and then permanently bound the newly activated stream to `Silent` ("no audio device: sound is mixed and dropped") even while the process's initial 48 kHz stereo `AudioQueue` kept running. ## Fix 1. **Share one `AudioQueue` per `(rate, channels)` (`Hub` / `MixerQueue`):** Active `CoreAudio` handles attach a `Slot` to the running `(48_000, 2)` queue and mix in userspace (`f32` sum into pre-allocated scratch buffers in `refill` with zero heap allocations on the callback thread; silence when zero streams are attached). Mid-run spatial/WASAPI/XAudio2 stream activations attach in userspace without calling `AudioQueueNewOutput` or `AudioQueueStart` again, and only reopen if `AudioQueueEnqueueBuffer` reports a runtime device error. 2. **Recreate the queue handle on `AudioQueueStart` retry:** When opening a hardware `AudioQueue` for the first time (or after a runtime failure), each retry disposes the failed `AudioQueueRef` and allocates/primes a fresh queue handle instead of retrying `AudioQueueStart` on the broken handle. 3. **Thread-aware `Drop` without callback lock cycles:** `refill` invokes each slot's `Render` outside the slot mutex (`in_call: Option<ThreadId>` + `Condvar`), waiting for any in-flight callback from other threads on `Drop` while avoiding self-deadlock if a callback ever drops its own `Output` re-entrantly. ## Tests - `host_posix::coreaudio::tests::the_device_opens_pulls_frames_and_closes` & `the_silent_output_keeps_time` - `host_posix::coreaudio::tests::streams_share_one_queue_mix_and_survive_repeated_reactivations` (2 concurrent streams mix `0.25 + 0.50 = 0.75`, idle outputs `0.0`, and 32 stream tear-downs/re-activations make `1` `AudioQueueNewOutput` and `1` `AudioQueueStart` call total) - `host_posix::coreaudio::tests::start_retry_disposes_failed_queue_and_recovers_after_runtime_error` (`'what'` on attempt 0 disposes `0x1001` and starts fresh `0x1002`; runtime `AudioQueueEnqueueBuffer` failure notifies active streams and reopens `0x1003`) - `host_posix::coreaudio::tests::reentrant_drop_inside_render_callback_does_not_deadlock`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 12:50:19 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-27 13:28 UTC · original

Check report: 45ed3281 checked together with #338 and #341 merged onto main 60baf130 (M5 Pro 48 GB, arm64, with the converter):

  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask: all pass (victory-hle 144 passed, main 141 with the new tests of both PRs; victory-gpu 347 passed).
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings: clean. cargo xtask policy: ok.
  • HLE only, so no sweep; not run in game.

Result: passes, and no review question is open. It can't be merged from my side (the branch rule requires an approving review), so it's ready for the maintainer. @⁠Sheffy007, if you get a world run with it, tools/macos/run-summary.sh prints the audio section (spatial stream activations, whether the device kept playing); posting that before/after would show #337 is fixed in game.

**@⁠Olima34** on GitHub · 2026-11-27 13:28 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/337#issuecomment-7219528649) Check report: `45ed3281` checked together with #338 and #341 merged onto main `60baf130` (M5 Pro 48 GB, arm64, with the converter): - `cargo test -p victory-hle -p victory -p victory-gpu -p xtask`: all pass (victory-hle 144 passed, main 141 with the new tests of both PRs; victory-gpu 347 passed). - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings`: clean. `cargo xtask policy`: ok. - HLE only, so no sweep; not run in game. Result: passes, and no review question is open. It can't be merged from my side (the branch rule requires an approving review), so it's ready for the maintainer. @⁠Sheffy007, if you get a world run with it, `tools/macos/run-summary.sh` prints the audio section (spatial stream activations, whether the device kept playing); posting that before/after would show #337 is fixed in game.
github manually merged commit 991f77d183 into main 2026-11-27 16:23:13 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 16:15 UTC · original

Merged in 4cf84c83. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. Audio kept playing through the run. Thanks.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 16:15 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/337#issuecomment-7223040721) Merged in 4cf84c83. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. Audio kept playing through the run. Thanks.
This discussion has been locked. Commenting is limited to contributors.
No description provided.