[pulseaudio-discuss] "hello, world" volume control

Diogo F. S. Ramos diogofsr at gmail.com
Sat Apr 21 01:27:33 PDT 2012

Tanu Kaskinen <tanuk at iki.fi> writes:

> On Fri, 2012-04-20 at 17:29 -0300, Diogo F. S. Ramos wrote:
>> "Dalleau, Frederic" <frederic.dalleau at intel.com> writes:
>> > On Fri, Apr 20, 2012 at 10:04 AM, Diogo F. S. Ramos <diogofsr at gmail.com> wrote:
>> >> I'm trying to write a very simple volume control for pulseaudio but I'm
>> >> having a really hard time figuring out the API. Is there some kind of
>> >> "hello, world"? Something that just mute a sink and exit would be great.
>> Reading some of the sources I see the usage of sockets
>> (src/utils/pacmd.c) and not glib (src/utils/pactl.c).
> Don't look at pacmd, it doesn't use the normal API. pactl is probably a
> good example for muting a sink.

I've attached a program in the end which accomplishes the task I was
trying to do initially. Would you say it is the straight forward way to
do it?

Now I see libpulse is like GTK with regards of the main thread of the
program. This imposes a little challenge to interactive programs.

Is that the reason pacmd an pactl use different strategies to interact
with the server?

Also, if one uses threads to decouple the mainloop thread from the main
thread, how would you generate events that could make state changes,
like volume control?

#include <pulse/pulseaudio.h>

void mutecb(pa_context *c, int success, void *userdata)
    pa_mainloop_quit(userdata, 0);

void notifycb(pa_context *c, void *userdata)
    if (pa_context_get_state(c) == PA_CONTEXT_READY)
        pa_context_set_sink_mute_by_index(c, 0, 1, mutecb, userdata);

int main(int argc, char *argv[])
    pa_mainloop *m = pa_mainloop_new();
    pa_context *context = pa_context_new(pa_mainloop_get_api(m), NULL);
    pa_context_set_state_callback(context, notifycb, m);
    pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN|PA_CONTEXT_NOFAIL, NULL);

    pa_mainloop_run(m, 0);

    return 0;

Diogo F. S. Ramos

More information about the pulseaudio-discuss mailing list