int openMode = MAL_SND_PCM_NO_AUTO_RESAMPLE | MAL_SND_PCM_NO_AUTO_CHANNELS | MAL_SND_PCM_NO_AUTO_FORMAT;
if (pDeviceID == NULL) {
// We're opening the default device. I don't know if trying anything other than "default" is necessary, but it makes
// me feel better to try as hard as we can get to get _something_ working.
const char* defaultDeviceNames[] = {
"default",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
if (shareMode == mal_share_mode_exclusive) {
defaultDeviceNames[1] = "hw";
defaultDeviceNames[2] = "hw:0";
defaultDeviceNames[3] = "hw:0,0";
} else {
if (type == mal_device_type_playback) {
defaultDeviceNames[1] = "dmix";
defaultDeviceNames[2] = "dmix:0";
defaultDeviceNames[3] = "dmix:0,0";
} else {
defaultDeviceNames[1] = "dsnoop";
defaultDeviceNames[2] = "dsnoop:0";
defaultDeviceNames[3] = "dsnoop:0,0";
}
defaultDeviceNames[4] = "hw";
defaultDeviceNames[5] = "hw:0";
defaultDeviceNames[6] = "hw:0,0";
}
mal_bool32 isDeviceOpen = MAL_FALSE;
for (size_t i = 0; i < mal_countof(defaultDeviceNames); ++i) {
if (defaultDeviceNames[i] != NULL && defaultDeviceNames[i][0] != '\0') {
if (((mal_snd_pcm_open_proc)pContext->alsa.snd_pcm_open)(&pPCM, defaultDeviceNames[i], stream, openMode) == 0) {
isDeviceOpen = MAL_TRUE;
break;
}
}
}
if (!isDeviceOpen) {
return mal_context_post_error(pContext, NULL, "[ALSA] snd_pcm_open() failed when trying to open an appropriate default device.", MAL_FAILED_TO_OPEN_BACKEND_DEVICE);
}
} else {
// We're trying to open a specific device. There's a few things to consider here:
//
// mini_al recongnizes a special format of device id that excludes the "hw", "dmix", etc. prefix. It looks like this: ":0,0", ":0,1", etc. When
// an ID of this format is specified, it indicates to mini_al that it can try different combinations of plugins ("hw", "dmix", etc.) until it
// finds an appropriate one that works. This comes in very handy when trying to open a device in shared mode ("dmix"), vs exclusive mode ("hw").
// May end up needing to make small adjustments to the ID, so make a copy.
mal_device_id deviceID = *pDeviceID;
mal_bool32 isDeviceOpen = MAL_FALSE;
if (deviceID.alsa[0] != ':') {
// The ID is not in ":0,0" format. Use the ID exactly as-is.
if (((mal_snd_pcm_open_proc)pContext->alsa.snd_pcm_open)(&pPCM, deviceID.alsa, stream, openMode) == 0) {
isDeviceOpen = MAL_TRUE;
}
} else {
// The ID is in ":0,0" format. Try different plugins depending on the shared mode.
if (deviceID.alsa[1] == '\0') {
deviceID.alsa[0] = '\0'; // An ID of ":" should be converted to "".
}
char hwid[256];
if (shareMode == mal_share_mode_shared) {
if (type == mal_device_type_playback) {
mal_strcpy_s(hwid, sizeof(hwid), "dmix");
} else {
mal_strcpy_s(hwid, sizeof(hwid), "dsnoop");
}
if (mal_strcat_s(hwid, sizeof(hwid), deviceID.alsa) == 0) {
if (((mal_snd_pcm_open_proc)pContext->alsa.snd_pcm_open)(&pPCM, hwid, stream, openMode) == 0) {
isDeviceOpen = MAL_TRUE;
}
}
}
// If at this point we still don't have an open device it means we're either preferencing exclusive mode or opening with "dmix"/"dsnoop" failed.
if (!isDeviceOpen) {
mal_strcpy_s(hwid, sizeof(hwid), "hw");
if (mal_strcat_s(hwid, sizeof(hwid), deviceID.alsa) == 0) {
if (((mal_snd_pcm_open_proc)pContext->alsa.snd_pcm_open)(&pPCM, hwid, stream, openMode) == 0) {