3

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?

Hedgehog
  • 479
  • 6
  • 16

1 Answers1

2

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.

Hedgehog
  • 479
  • 6
  • 16