fix: make BT A2DP output exclusive to one process
The A2DP output is a single unmixed PCM stream. A second process opening audio while a stream was active would reconfigure the SBC encoder and media clock under the owner and interleave both apps' PCM into one ring, garbling playback (e.g. launching DOOM destabilized Music). Add ClaimOutput/ReleaseOutput pid ownership: the first opener gets the BT sink, later openers fall back to the HDA mixer, and only the owner can tear the stream down. The scheduler releases ownership on process exit so a killed app cannot leak the claim. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
9519e174c0
commit
928fe0bbed
5 files changed
+75
-14
No files matched your search
+27
-13
@@ -26,6 +26,10 @@ namespace montauk::abi {
|
||||
static constexpr int AUDIO_HANDLE_BT = 0x100;
|
||||
|
||||
static int64_t Sys_AudioOpen(uint32_t sampleRate, uint8_t channels, uint8_t bitsPerSample) {
|
||||
auto* proc = Sched::GetCurrentProcessPtr();
|
||||
int pid = proc ? proc->pid : -1;
|
||||
const char* name = proc ? proc->name : "?";
|
||||
|
||||
// Auto-switch: when a Bluetooth A2DP sink is connected and its stream is
|
||||
// set up (StartSource left it Configured/Open), route audio to the
|
||||
// headphones -- like a phone does when you plug in BT. Falls back to
|
||||
@@ -35,16 +39,25 @@ namespace montauk::abi {
|
||||
if (state == Drivers::USB::Bluetooth::A2dp::State::Open ||
|
||||
state == Drivers::USB::Bluetooth::A2dp::State::Streaming ||
|
||||
state == Drivers::USB::Bluetooth::A2dp::State::Configured) {
|
||||
Drivers::USB::Bluetooth::A2dp::ConfigureStream(sampleRate, channels, bitsPerSample);
|
||||
if (Drivers::USB::Bluetooth::A2dp::StartStream()) {
|
||||
return AUDIO_HANDLE_BT;
|
||||
// The BT output is a single unmixed stream, so only the first
|
||||
// opener gets it. Configuring it again while another stream
|
||||
// owns it would reset the SBC encoder and media clock under
|
||||
// that stream and interleave both apps' PCM into one ring
|
||||
// (garbled playback, dropouts persisting until the owner
|
||||
// reopens). Later openers fall through to the HDA mixer.
|
||||
if (Drivers::USB::Bluetooth::A2dp::ClaimOutput(pid)) {
|
||||
Drivers::USB::Bluetooth::A2dp::ConfigureStream(sampleRate, channels, bitsPerSample);
|
||||
if (Drivers::USB::Bluetooth::A2dp::StartStream()) {
|
||||
return AUDIO_HANDLE_BT;
|
||||
}
|
||||
// Stream would not start (sink unresponsive / state desync):
|
||||
// returning the BT handle anyway would make every write fail
|
||||
// with the app stuck reporting "playing" at 0:00. Fall through
|
||||
// to the speakers instead.
|
||||
Drivers::USB::Bluetooth::A2dp::ReleaseOutput(pid);
|
||||
Kt::KernelLogStream(Kt::WARNING, "Audio")
|
||||
<< "BT A2DP stream failed to start; falling back to HDA";
|
||||
}
|
||||
// Stream would not start (sink unresponsive / state desync):
|
||||
// returning the BT handle anyway would make every write fail
|
||||
// with the app stuck reporting "playing" at 0:00. Fall through
|
||||
// to the speakers instead.
|
||||
Kt::KernelLogStream(Kt::WARNING, "Audio")
|
||||
<< "BT A2DP stream failed to start; falling back to HDA";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,9 +65,6 @@ namespace montauk::abi {
|
||||
// hardware stream open across virtual streams, so multiple apps can
|
||||
// play simultaneously.
|
||||
if (Drivers::Audio::IntelHda::IsInitialized()) {
|
||||
auto* proc = Sched::GetCurrentProcessPtr();
|
||||
int pid = proc ? proc->pid : -1;
|
||||
const char* name = proc ? proc->name : "?";
|
||||
return (int64_t)Drivers::Audio::Mixer::Open(sampleRate, channels,
|
||||
bitsPerSample, pid, name);
|
||||
}
|
||||
@@ -64,7 +74,11 @@ namespace montauk::abi {
|
||||
|
||||
static int64_t Sys_AudioClose(int handle) {
|
||||
if (handle == AUDIO_HANDLE_BT) {
|
||||
Drivers::USB::Bluetooth::A2dp::StopStream(true); // drop queued tail
|
||||
// Stops the stream (dropping the queued tail) only when the caller
|
||||
// owns the BT output, so a stale handle held by another process
|
||||
// cannot tear down the owner's stream.
|
||||
auto* proc = Sched::GetCurrentProcessPtr();
|
||||
Drivers::USB::Bluetooth::A2dp::ReleaseOutput(proc ? proc->pid : -1);
|
||||
return 0;
|
||||
}
|
||||
Drivers::Audio::Mixer::Close(handle);
|
||||
|
||||
@@ -12,4 +12,4 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define MONTAUK_BUILD_NUMBER 3
|
||||
#define MONTAUK_BUILD_NUMBER 4
|
||||
Reference in new issue
Block a user