I'm stuck on a problem with VLC and alsa-lib.
The context is the following:
- C++ app
- Run on raspberry PI
- Version: VLC media player 2.0.3 Twoflower (revision 2.0.2-93-g77aa89e)
- First in the app, I set the global system volume using alsa-lib (code provided below)
- Fork the process to run VLC in console (cvlc)
- Set different volumes in later
My problem is the following: When in run my application, if I set the volume using alsa-lib before forking and launching VLC, VLC starts with a volume higher than expacted (i.e the one I set).
If I remove the code changing the volume from my program, VLC starts with the global system volume set (I tested with a volume set to 0).
When I run VLC from the console, I get the same behavior, it starts with the global system volume currently set.
To add more precision: When I set again a different volume with the same piece of code in my program, but after having forked and launched VLC, the volume level is properly applied. As I think the problem is related to the alsa-lib call, I tried several functions in alsa-lib to release memory but it didn't solve the problem.
Here is the code I use (remove error handling to be more readable) to set the volume with alsa-lib (inspired of Set ALSA master volume from C code):
// ALSA mixer handle
snd_mixer_t *m_handle;
snd_mixer_elem_t* m_elem;
long volume = -1000; // This volume is in the allowed range specified by "amixer"
// Open an empty mixer
snd_mixer_open(&m_handle), SND_MIXER_ELEM_SIMPLE);
snd_mixer_attach(m_handle, "hw:0");
snd_mixer_selem_register(m_handle, NULL, NULL);
// Load the mixer elements
snd_mixer_load(m_handle);
// Configure the simple element we are looking for
snd_mixer_selem_id_t *simpleElemId; // mixer simple element
snd_mixer_selem_id_alloca(&simpleElemId);
snd_mixer_selem_id_set_index(simpleElemId, 0);
snd_mixer_selem_id_set_name(simpleElemId, "PCM");
m_elem = snd_mixer_find_selem(m_handle, simpleElemId);
// Here is set the global system volume
snd_mixer_selem_set_playback_volume_all(m_elem, volume);
snd_mixer_detach(m_handle, "hw:0");
snd_mixer_close(m_handle);
I check with another opened console that the volume level is properly set with alsamixer command. So I can tell that the volume is properly set before running VLC. So I don't understand why it has trouble with its volume level when starting...
Am I OK with the code calling alsa-lib? Do I forget releasing or removing an element?
Note 1 : that alsamixer shows that VLC is not modifying the global system volume when launched.
Note 2: The verbose option (-vvv) gives the same trace whatever it is launched from my C++ program (with volume problem) or from console
Thanks for your help :)