open_default_sink fails on macOS when the device's max buffer size is below ~50 ms (e.g. AirPods: 15..=960)

Aperta Adatta ai principianti
#949 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
macos, rust

Direzione di ricerca

Inizia con DeviceSinkBuilder::from_device e DeviceSinkBuilder::open_default_sink, quindi esamina come vengono gestiti l’intervallo di buffer supportato del dispositivo e i metadati di driver(). Riproduci il problema con un’uscita macOS predefinita la cui dimensione massima del buffer è inferiore alla dimensione selezionata, come nell’esempio degli AirPods. Done significa che i dispositivi vincolati vengono aperti correttamente con una dimensione di buffer supportata e che il percorso di fallback può riprovare i dispositivi privi di metadati del driver.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

DeviceSinkBuilder::open_default_sink() fails on macOS when the default output device's supported buffer range doesn't include the ~50 ms size rodio picks, and the built-in fallback never tries another configuration on Core Audio.

BuildError(Error { kind: UnsupportedConfig, message: Some("Buffer size 2048 is not in the supported range 15..=960") })

Seen with rodio 0.23.0 (cpal 0.18) on macOS 27.2, with AirPods Pro 3 as the default output (48 kHz, buffer range 15..=960). The built-in speakers and wired headphones on the same Mac report 15..=4096 and open fine. The same code path is unchanged on master.

Cause

  1. DeviceSinkBuilder::from_device replaces BufferSize::Default with BufferSize::Fixed(nearest_multiple_of_two(sample_rate / 20)), which is 2048 frames at 48 kHz. It doesn't check the device's SupportedBufferSize, so any device whose range tops out below that (here 15..=960) is rejected by cpal.
  2. open_default_sink's fallback keeps only devices where description().driver() is Some and not "null". On Core Audio driver() is None for every device, so the fallback list is always empty and the original error comes back. open_sink_or_fallback, which would have retried with with_supported_config (and so BufferSize::Default), is never reached.

The newer speakers API already handles this: impl From<SupportedStreamConfig> for OutputConfig clamps the default buffer into min..=max. But speakers is behind experimental, and open_default_sink is still what the docs point to.

Reproduction

With a default output device whose supported buffer range has max < 2048 at 48 kHz:

use rodio::cpal::traits::{DeviceTrait, HostTrait};

fn main() {
    let host = rodio::cpal::default_host();
    let dev = host.default_output_device().unwrap();
    println!("{:?}", dev.default_output_config().unwrap().buffer_size()); // Range { min: 15, max: 960 }
    for d in host.output_devices().unwrap() {
        println!("driver: {:?}", d.description().unwrap().driver()); // None for every device
    }
    rodio::DeviceSinkBuilder::open_default_sink().unwrap(); // BuildError(UnsupportedConfig ...)
}

Suggested fix

In from_device, clamp the target size into the device's supported range, as OutputConfig::from does:

let buffer = match default_config.buffer_size() {
    cpal::SupportedBufferSize::Range { min, max } => safe_buffer_size.clamp(*min, *max),
    cpal::SupportedBufferSize::Unknown => safe_buffer_size,
};

It may also be worth dropping the driver() filter, or letting devices with driver() == None through, so the fallback can work on macOS.

As a workaround, we now read the range ourselves and call DeviceSinkBuilder::from_device(dev)?.with_buffer_size(BufferSize::Fixed(clamped)).open_stream(). Downstream report: trypsynth/fedra#205.

Lingua principale
Rust
Stelle
2.5k
Fork
338
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di RustAudio/rodio

Tutte le issue di RustAudio/rodio

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.