alsalibalsa

Is it possible to get information about a busy Alsa device?


Currently I am attempting to get information about an Alsa device by calling the snd_pcm_open function then passing that to snd_pcm_info to get information about the device. I plan specifically to get the channel information from snd_pcm_query_chmaps. The error code I am getting is: Device or resource busy. I was curious if there was another way to get this information or somehow open the pcm in a way that it won't ever be "busy" from other applications. How would I get this information, if at all possible, in a way that will work even if the device is busy?

The relevant snippet of my code is

snd_pcm_t* pcm;
int err;
if ((err = snd_pcm_open(&pcm, name, SND_PCM_STREAM_PLAYBACK, 0)) == 0 || (err = snd_pcm_open(&pcm, name, SND_PCM_STREAM_CAPTURE, 0)) == 0) {
    snd_pcm_info_t* pcm_info;
    if (snd_pcm_info_malloc(&pcm_info) == 0) {
        if (snd_pcm_info(pcm, pcm_info) == 0) {
            printf("Card number: %d\n", snd_pcm_info_get_card(pcm_info));
            printf("Device number: %d\n", snd_pcm_info_get_device(pcm_info));
            printf("Subdevice number: %d\n", snd_pcm_info_get_subdevice(pcm_info));
        }
        snd_pcm_info_free(pcm_info);
    }
    snd_pcm_close(pcm);
} else {
    printf("Erroring opening PCM device with error: %s\n", snd_strerror(err));
}

Solution

  • To get information about a PCM device without opening it, open the control device of its card and call snd_ctl_pcm_info(). (See the aplay source code for how to use it.)

    The channel map information is provided in the TLV information of some mixer controls; use the snd_pcm_query_chmaps_from_hw() helper function to read it.