fix(player): recreate CoreAudio exclusive streams

This commit is contained in:
Vudgekek committed 2026-05-06 23:26:15 -05:00
1 parent e0151af6d9
commit d4a51a5c63
2 files changed
+254 -89

No files matched your search

+107 -20
View File
@@ -465,10 +465,27 @@ fn create_output_stream_with_config(
/// Output stream type - either rodio or ALSA Direct
enum StreamType {
Rodio(MixerDeviceSink),
#[cfg(target_os = "macos")]
CoreAudio {
sink: MixerDeviceSink,
_exclusive_guard: Option<crate::audio::CoreAudioExclusiveGuard>,
},
#[cfg(target_os = "linux")]
AlsaDirect(Arc<crate::audio::AlsaDirectStream>),
}
impl StreamType {
fn rodio_sink(&self) -> Option<&MixerDeviceSink> {
match self {
StreamType::Rodio(sink) => Some(sink),
#[cfg(target_os = "macos")]
StreamType::CoreAudio { sink, .. } => Some(sink),
#[cfg(target_os = "linux")]
StreamType::AlsaDirect(_) => None,
}
}
}
/// Try to create output stream using the backend system (if configured)
/// Returns None if backend system is not configured (backend_type = None)
///
@@ -478,8 +495,16 @@ fn try_init_stream_with_backend(
sample_rate: u32,
channels: u16,
) -> Option<Result<StreamType, String>> {
// Check if backend system is configured
let backend_type = audio_settings.backend_type?;
// Check if backend system is configured. On macOS, default Auto to
// SystemDefault so CoreAudio device probing, rate switching, and Hog Mode
// are available to existing installs with no saved backend.
let backend_type = audio_settings.backend_type.or_else(|| {
if cfg!(target_os = "macos") {
Some(AudioBackendType::SystemDefault)
} else {
None
}
})?;
log::info!(
"Using backend system: {:?} (device: {:?}, plugin: {:?})",
@@ -541,14 +566,25 @@ fn try_init_stream_with_backend(
}
// Fallback to regular rodio stream (PipeWire, Pulse, ALSA via CPAL)
match backend.create_output_stream(&config) {
Ok(mixer_sink) => {
match backend.create_output_stream_with_exclusive_guard(&config) {
Ok((mixer_sink, exclusive_guard)) => {
log::info!(
"Stream created via {:?} backend at {}Hz",
backend_type,
sample_rate
);
Some(Ok(StreamType::Rodio(mixer_sink)))
#[cfg(target_os = "macos")]
let stream = if backend_type == AudioBackendType::SystemDefault {
StreamType::CoreAudio {
sink: mixer_sink,
_exclusive_guard: exclusive_guard,
}
} else {
StreamType::Rodio(mixer_sink)
};
#[cfg(not(target_os = "macos"))]
let stream = StreamType::Rodio(mixer_sink);
Some(Ok(stream))
}
Err(e) => {
log::error!("Backend stream creation failed: {}", e);
@@ -907,7 +943,7 @@ impl Player {
-> Option<StreamType> {
// Try backend system if configured
if let Ok(settings) = thread_settings.lock() {
if settings.backend_type.is_some() {
if settings.backend_type.is_some() || cfg!(target_os = "macos") {
// Use provided sample rate/channels to maintain DAC passthrough
log::info!(
"Initializing backend system with {}Hz/{}ch",
@@ -1078,7 +1114,7 @@ impl Player {
let dac_passthrough = thread_settings
.lock()
.ok()
.map(|s| s.dac_passthrough)
.map(|s| cfg!(target_os = "linux") && s.dac_passthrough)
.unwrap_or(false);
// Check if we need to recreate the stream
@@ -1093,15 +1129,32 @@ impl Player {
.and_then(|s| s.backend_type)
.map(|b| b == AudioBackendType::Alsa)
.unwrap_or(false);
let using_coreaudio_exclusive = thread_settings
.lock()
.ok()
.map(|s| {
cfg!(target_os = "macos")
&& s.backend_type.unwrap_or(AudioBackendType::SystemDefault)
== AudioBackendType::SystemDefault
&& s.exclusive_mode
})
.unwrap_or(false);
let needs_new_stream = stream_opt.is_none()
|| (dac_passthrough && format_changed)
|| (using_alsa_direct && format_changed);
|| (using_alsa_direct && format_changed)
|| (using_coreaudio_exclusive && format_changed);
if needs_new_stream {
if stream_opt.is_some() {
if (dac_passthrough || using_alsa_direct) && format_changed {
let mode = if using_alsa_direct {
if (dac_passthrough
|| using_alsa_direct
|| using_coreaudio_exclusive)
&& format_changed
{
let mode = if using_coreaudio_exclusive {
"CoreAudio exclusive"
} else if using_alsa_direct {
"ALSA Direct"
} else {
"DAC passthrough"
@@ -1122,9 +1175,10 @@ impl Player {
}
log::info!(
"DAC passthrough: {}, ALSA Direct: {}",
"DAC passthrough: {}, ALSA Direct: {}, CoreAudio exclusive: {}",
dac_passthrough,
using_alsa_direct
using_alsa_direct,
using_coreaudio_exclusive
);
// Try backend system first (if configured), then fall back to legacy CPAL
@@ -1334,7 +1388,8 @@ impl Player {
// Create PlaybackEngine from StreamType
let mut engine = match stream {
StreamType::Rodio(ref mixer_sink) => {
stream if stream.rodio_sink().is_some() => {
let mixer_sink = stream.rodio_sink().expect("rodio sink");
match PlaybackEngine::new_rodio(&mixer_sink.mixer()) {
Ok(e) => {
*consecutive_sink_failures = 0;
@@ -1398,6 +1453,9 @@ impl Player {
hardware_volume,
)
}
_ => unreachable!(
"non-rodio stream handled by platform-specific arms"
),
};
let volume = thread_state.volume.load(Ordering::SeqCst) as f32 / 100.0;
@@ -1519,7 +1577,7 @@ impl Player {
let dac_passthrough = thread_settings
.lock()
.ok()
.map(|s| s.dac_passthrough)
.map(|s| cfg!(target_os = "linux") && s.dac_passthrough)
.unwrap_or(false);
// Check if we need to recreate the stream
@@ -1532,15 +1590,32 @@ impl Player {
.and_then(|s| s.backend_type)
.map(|b| b == AudioBackendType::Alsa)
.unwrap_or(false);
let using_coreaudio_exclusive = thread_settings
.lock()
.ok()
.map(|s| {
cfg!(target_os = "macos")
&& s.backend_type.unwrap_or(AudioBackendType::SystemDefault)
== AudioBackendType::SystemDefault
&& s.exclusive_mode
})
.unwrap_or(false);
let needs_new_stream = stream_opt.is_none()
|| (dac_passthrough && format_changed)
|| (using_alsa_direct && format_changed);
|| (using_alsa_direct && format_changed)
|| (using_coreaudio_exclusive && format_changed);
if needs_new_stream {
if stream_opt.is_some() {
if (dac_passthrough || using_alsa_direct) && format_changed {
let mode = if using_alsa_direct {
if (dac_passthrough
|| using_alsa_direct
|| using_coreaudio_exclusive)
&& format_changed
{
let mode = if using_coreaudio_exclusive {
"CoreAudio exclusive"
} else if using_alsa_direct {
"ALSA Direct"
} else {
"DAC passthrough"
@@ -1670,7 +1745,8 @@ impl Player {
// Create PlaybackEngine
let mut engine = match stream {
StreamType::Rodio(ref mixer_sink) => {
stream if stream.rodio_sink().is_some() => {
let mixer_sink = stream.rodio_sink().expect("rodio sink");
match PlaybackEngine::new_rodio(&mixer_sink.mixer()) {
Ok(e) => {
*consecutive_sink_failures = 0;
@@ -1698,6 +1774,9 @@ impl Player {
hardware_volume,
)
}
_ => unreachable!(
"non-rodio stream handled by platform-specific arms"
),
};
let volume = thread_state.volume.load(Ordering::SeqCst) as f32 / 100.0;
@@ -1896,7 +1975,8 @@ impl Player {
};
let mut engine = match stream {
StreamType::Rodio(ref mixer_sink) => {
stream if stream.rodio_sink().is_some() => {
let mixer_sink = stream.rodio_sink().expect("rodio sink");
match PlaybackEngine::new_rodio(&mixer_sink.mixer()) {
Ok(e) => e,
Err(e) => {
@@ -1920,6 +2000,9 @@ impl Player {
hardware_volume,
)
}
_ => unreachable!(
"non-rodio stream handled by platform-specific arms"
),
};
let volume =
@@ -2031,7 +2114,8 @@ impl Player {
}
let mut engine = match stream {
StreamType::Rodio(ref mixer_sink) => {
stream if stream.rodio_sink().is_some() => {
let mixer_sink = stream.rodio_sink().expect("rodio sink");
match PlaybackEngine::new_rodio(&mixer_sink.mixer()) {
Ok(e) => e,
Err(e) => {
@@ -2055,6 +2139,9 @@ impl Player {
hardware_volume,
)
}
_ => unreachable!(
"non-rodio stream handled by platform-specific arms"
),
};
let volume = thread_state.volume.load(Ordering::SeqCst) as f32 / 100.0;