cpal icon indicating copy to clipboard operation
cpal copied to clipboard

Add a (serialisable) means to uniquely identify a device

Open abey79 opened this issue 1 year ago • 2 comments

Motivating use case

I'm writing a GUI program where the user is offered a choice of input devices and can pick one. The program should "remember" the user choice across launch, so it must serialise some identifying data about the device such that it can be retrieved again at next launch (assuming it still exists).

As far as I understand, the only piece of identifying data available for a Device is its name().

Problem

Device names are not unique, at least on macOS if you have two identical (e.g.) monitors, both equipped with mics and/or speakers. I happen to be in this situation and this code returns the following result:

let devices: Vec<_> = cpal::default_host().input_devices().unwrap().collect();
for device in devices {
    println!("Input device: {}", device.name().unwrap());
}

Result:

Input device: <iphone name> Microphone
Input device: Studio Display Microphone
Input device: Studio Display Microphone
Input device: MacBook Pro Microphone
Input device: Microsoft Teams Audio
Input device: iPad audio
Input device: ZoomAudioDevice

Solution

Ideally, Devices should have some kind of id that can be stored and checked against when trying to retrieve the same device at a later stage.

Related

  • Rejected attempt at implementing Device::id(): https://github.com/RustAudio/cpal/pull/537

abey79 avatar Oct 20 '24 15:10 abey79

I simply add the index in the list to the device name when serializing.

dheijl avatar Oct 20 '24 17:10 dheijl

I'll probably have to do something like that, though I might save the (name, n), to refer to the nth device named name.

edit: this workaround precludes using the system-defined default input/output device, because if the default is one of the duplicated name, it's currently impossible to know which one it corresponds to.

abey79 avatar Oct 20 '24 19:10 abey79

Done in #1014.

roderickvd avatar Nov 14 '25 00:11 roderickvd