c++clinuxalsa

ALSA: open a PCM device in shared mode


I want to playback some audio data using ALSA to a PCM device. As an example I have downloaded this sample example and run it in my PC. It works fine when no other process is currently using the sound card. But it doesn't play anything when some other process uses the audio device (i.e., media player playing songs) and shows following error -

Playback open error: Device or resource busy

Looking at the source code of this example I could say that the snd_pcm_open function at line 882 is throwing this error. It finds the device busy since another process is currently using it.

I also tried the reverse way - first started the example then tried to start a song. In this case the media player stays idle, showing "idle" just beside the progress bar (I am using Banshee). I am assuming that snd_pcm_open gains exclusive right of the device resource so that no other process can use it.

But I don't want that. I want to play sound to a audio device without requiring any exclusive rights so that other processes in the PC can share the same device for outputting audio data.

How can I do that? How can I open a PCM device so that other processes can also share the same device?


Solution

  • I couldn't find a way to share a device between multiple processes. I tried to use the dmix plugin to combine multiple playback streams using the .asoundrc configuration file but that didn't work for some reason. I tried to use the default device of my sound card but that didn't work too. According to a recommendation in ALSA mailing list I tried to open the default device using default:CARD=x as the device name parameter to the snd_pcm_open function which results in a device not found error in my system. Apparently there is no way to do this (if someone can find a way, please update this answer).

    Rather than using ALSA, I used PulseAudio next which solved my problem.