Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get event from pulseaudio when lists of sinks or sources are changed?

He.

The program should constantly check incoming sound from a Bluetooth microphone. Bluetooth device can be connected/disconnected any time.

How to get event from Pulseaudio that list of sources changed?

I tried to use pa_context_set_event_callback (pa_ctx, pa_context_event_cb, &mydata);

But no one calls pa_context_event_cb when BT headset is connected/disconnected.

What is the good practice for pulseaudio?

like image 851
Hedgehog Avatar asked Sep 19 '25 17:09

Hedgehog


1 Answers

Ok. Figured it out.

1) Subscribe for context state changes: pa_context_set_state_callback(pa_ctx, pa_state_cb, &mydata);

2) In pa_state_cb:

void pa_state_cb(pa_context *c, void *userdata) {

    pa_context_state_t state;
    state = pa_context_get_state(c);
    switch  (state) {
            case PA_CONTEXT_READY: {
                     //set callback
                     pa_context_set_subscribe_callback(c, pa_context_subscribe_cb, &mydata);
                     //set events mask and enable event callback.
                     o = pa_context_subscribe(c, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE,
                     NULL, NULL);

                     if (o)
                     {
                       pa_operation_unref(o);
                     }

            }
                    break;
            case PA_CONTEXT_UNCONNECTED:
            case PA_CONTEXT_CONNECTING:
            case PA_CONTEXT_AUTHORIZING:
            case PA_CONTEXT_SETTING_NAME:
            case PA_CONTEXT_FAILED:
            case PA_CONTEXT_TERMINATED:
            default:
                    break;

}

3) Then handle mask event (pa_subscription_event_type_t) in the callback pa_context_subscribe_cb.

like image 184
Hedgehog Avatar answered Sep 23 '25 11:09

Hedgehog