ALSA: Change to device enumeration.

In order to get detailed information about a device, that is supported
formats, channels and sample rates, the PCM needs to be opened. This
can fail sometimes, in which case enumeration would previously just not
enumerate the device.

This is OK, but it creates issues. The first is that the enumerated
devices will not be consistent with what's reported by `aplay`. The
other is that when a hardware device is opened, iteration will not
include that device because it'll be opened in exclusive mode. This
creates a practical issue when trying to get the name of an already
opened device.

This commit makes it so that these devices will still be enumerated,
only they'll be missing detailed format, channels and rate information.
This commit is contained in:
David Reid
2026-01-09 13:38:07 +10:00
parent 224a4c9d3a
commit 948967dcbb
+10 -4
View File
@@ -28912,11 +28912,16 @@ static ma_result ma_context_enumerate_devices__alsa(ma_context* pContext, ma_enu
ma_uint32 iChannel; ma_uint32 iChannel;
int openMode = 0; /*MA_SND_PCM_NO_AUTO_RESAMPLE | MA_SND_PCM_NO_AUTO_CHANNELS | MA_SND_PCM_NO_AUTO_FORMAT;*/ int openMode = 0; /*MA_SND_PCM_NO_AUTO_RESAMPLE | MA_SND_PCM_NO_AUTO_CHANNELS | MA_SND_PCM_NO_AUTO_FORMAT;*/
/* For detailed info we need to open the device. */ /*
if (pContextStateALSA->snd_pcm_open(&pPCM, deviceInfo.id.alsa, (deviceType == ma_device_type_playback) ? MA_SND_PCM_STREAM_PLAYBACK : MA_SND_PCM_STREAM_CAPTURE, openMode) != 0) { For detailed info we need to open the device. I can think of two reasons why opening might fail:
goto next_device;
}
1) The device is disabled or physically unplugged.
2) The device is a hardware device and is already opened in exclusive mode
When this happens we're still going to enumerate the device, but we're going to just not fill out
the detailed info.
*/
if (pContextStateALSA->snd_pcm_open(&pPCM, deviceInfo.id.alsa, (deviceType == ma_device_type_playback) ? MA_SND_PCM_STREAM_PLAYBACK : MA_SND_PCM_STREAM_CAPTURE, openMode) == 0) {
/* We need to initialize a HW parameters object in order to know what formats are supported. */ /* We need to initialize a HW parameters object in order to know what formats are supported. */
pHWParams = (ma_snd_pcm_hw_params_t*)ma_calloc(pContextStateALSA->snd_pcm_hw_params_sizeof(), ma_context_get_allocation_callbacks(pContext)); pHWParams = (ma_snd_pcm_hw_params_t*)ma_calloc(pContextStateALSA->snd_pcm_hw_params_sizeof(), ma_context_get_allocation_callbacks(pContext));
if (pHWParams == NULL) { if (pHWParams == NULL) {
@@ -29017,6 +29022,7 @@ static ma_result ma_context_enumerate_devices__alsa(ma_context* pContext, ma_enu
pContextStateALSA->snd_pcm_close(pPCM); pContextStateALSA->snd_pcm_close(pPCM);
} }
}
if (!ma_is_device_blacklisted__alsa(deviceType, NAME)) { if (!ma_is_device_blacklisted__alsa(deviceType, NAME)) {
cbResult = callback(deviceType, &deviceInfo, pUserData); cbResult = callback(deviceType, &deviceInfo, pUserData);