2 * Asynchronous Interface to PulseAudio
4 * (c) 2014--2020 Martin Mares <mj@ucw.cz>
10 #include <ucw/clists.h>
11 #include <ucw/mainloop.h>
12 #include <ucw/stkstring.h>
31 enum pulse_state pulse_state;
32 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
34 clist pulse_client_list, pulse_source_list, pulse_sink_list, pulse_sink_input_list;
36 static pa_context *pulse_ctx;
37 static struct main_timer pulse_connect_timer;
39 /*** Tracking of currently running asynchronous operations ***/
47 static clist pulse_op_list;
49 static struct pulse_op *pulse_op_new(void)
51 struct pulse_op *op = xmalloc_zero(sizeof(*op));
52 clist_add_tail(&pulse_op_list, &op->n);
56 static void pulse_op_done(struct pulse_op *op)
59 pa_operation_unref(op->o);
64 static void pulse_op_cancel_all(void)
67 while (op = (struct pulse_op *) clist_head(&pulse_op_list))
69 DBG("Pulse: Cancelling pending operation");
70 pa_operation_cancel(op->o);
75 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
76 #define PULSE_ASYNC_INIT_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->is_init = 1; _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
78 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
81 msg(L_ERROR, "Pulse: Failure reported");
82 pulse_op_done(userdata);
85 /*** Debugging dumps ***/
89 msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
91 CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
92 msg(L_DEBUG, "## Client #%d: %s host=%s", c->idx, c->name, c->host);
94 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
95 msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u suspended=%u port=%s",
96 s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute, s->suspended, s->active_port);
98 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
99 msg(L_DEBUG, "## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u",
100 s->idx, s->name, s->client_idx, s->sink_idx, s->channels, s->volume, s->mute);
103 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
106 void *iterator = NULL;
109 while (key = pa_proplist_iterate(pl, &iterator))
111 const char *val = pa_proplist_gets(pl, key);
112 DBG(" %s = %s", key, val);
117 /*** Server state ***/
119 char *pulse_default_sink_name;
121 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
123 struct pulse_op *op = userdata;
125 DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
126 SET_STRING(pulse_default_sink_name, i->default_sink_name);
130 PULSE_STATE(PS_ONLINE);
131 msg(L_INFO, "PulseAudio is ready");
137 void pulse_server_set_default_sink(const char *name)
139 PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
144 #define HASH_NODE struct pulse_source
145 #define HASH_PREFIX(x) pulse_source_##x
146 #define HASH_KEY_ATOMIC idx
147 #define HASH_WANT_CLEANUP
148 #define HASH_WANT_LOOKUP
149 #define HASH_WANT_REMOVE
150 #define HASH_ZERO_FILL
151 #include <ucw/hashtable.h>
153 static void pulse_source_cb(pa_context *ctx UNUSED, const pa_source_info *i, int eol, void *userdata)
155 struct pulse_op *op = userdata;
161 PULSE_STATE(PS_GET_SERVER);
162 PULSE_ASYNC_INIT_RUN(pa_context_get_server_info, pulse_server_cb);
168 DBG("Pulse: SOURCE #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u port=%s",
169 i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state,
170 (i->active_port ? i->active_port->name : "none"));
171 pulse_dump_proplist(i->proplist);
173 struct pulse_source *s = pulse_source_lookup(i->index);
174 if (!clist_is_linked(&s->n))
175 clist_add_tail(&pulse_source_list, &s->n);
176 SET_STRING(s->name, i->name);
177 s->channels = i->channel_map.channels;
178 s->volume = pa_cvolume_avg(&i->volume);
179 s->base_volume = i->base_volume;
181 s->suspended = (i->state == PA_SOURCE_SUSPENDED);
182 SET_STRING(s->active_port, (i->active_port ? i->active_port->name : "none"));
186 static void pulse_source_gone(int idx)
188 DBG("Pulse: REMOVE SOURCE #%d", idx);
189 struct pulse_source *s = pulse_source_lookup(idx);
191 pulse_source_remove(s);
195 struct pulse_source *pulse_source_by_name(const char *name)
197 CLIST_FOR_EACH(struct pulse_source *, s, pulse_source_list)
198 if (!strcmp(s->name, name))
203 struct pulse_source *pulse_source_by_idx(int idx)
205 return pulse_source_lookup(idx);
208 void pulse_source_set_volume(int idx, pa_cvolume *cvol)
210 PULSE_ASYNC_RUN(pa_context_set_source_volume_by_index, idx, cvol, pulse_success_cb);
213 void pulse_source_set_mute(int idx, bool mute)
215 PULSE_ASYNC_RUN(pa_context_set_source_mute_by_index, idx, mute, pulse_success_cb);
218 void pulse_source_set_port(int idx, const char *port)
220 PULSE_ASYNC_RUN(pa_context_set_source_port_by_index, idx, port, pulse_success_cb);
223 /*** Sink inputs ***/
225 #define HASH_NODE struct pulse_sink_input
226 #define HASH_PREFIX(x) pulse_sink_input_##x
227 #define HASH_KEY_ATOMIC idx
228 #define HASH_WANT_CLEANUP
229 #define HASH_WANT_FIND
230 #define HASH_WANT_LOOKUP
231 #define HASH_WANT_REMOVE
232 #define HASH_ZERO_FILL
233 #include <ucw/hashtable.h>
235 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
237 struct pulse_op *op = userdata;
243 PULSE_STATE(PS_GET_SOURCES);
244 PULSE_ASYNC_INIT_RUN(pa_context_get_source_info_list, pulse_source_cb);
250 DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
251 i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
252 pulse_dump_proplist(i->proplist);
254 struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
255 if (!clist_is_linked(&s->n))
256 clist_add_tail(&pulse_sink_input_list, &s->n);
257 SET_STRING(s->name, i->name);
258 s->client_idx = i->client;
259 s->sink_idx = i->sink;
260 s->channels = i->channel_map.channels;
261 s->volume = pa_cvolume_avg(&i->volume);
266 static void pulse_sink_input_gone(int idx)
268 DBG("Pulse: REMOVE SINK INPUT #%d", idx);
269 struct pulse_sink_input *s = pulse_sink_input_find(idx);
273 pulse_sink_input_remove(s);
276 DBG("Pulse: Removing sink which does not exist");
280 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
282 PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
285 void pulse_sink_input_set_mute(int idx, bool mute)
287 PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
290 void pulse_sink_input_move(int input_idx, int sink_idx)
292 PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
297 #define HASH_NODE struct pulse_sink
298 #define HASH_PREFIX(x) pulse_sink_##x
299 #define HASH_KEY_ATOMIC idx
300 #define HASH_WANT_CLEANUP
301 #define HASH_WANT_LOOKUP
302 #define HASH_WANT_REMOVE
303 #define HASH_ZERO_FILL
304 #include <ucw/hashtable.h>
306 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
308 struct pulse_op *op = userdata;
314 PULSE_STATE(PS_GET_SINK_INPUTS);
315 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
321 DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u port=%s",
322 i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state,
323 (i->active_port ? i->active_port->name : "none"));
324 pulse_dump_proplist(i->proplist);
326 struct pulse_sink *s = pulse_sink_lookup(i->index);
327 if (!clist_is_linked(&s->n))
328 clist_add_tail(&pulse_sink_list, &s->n);
329 SET_STRING(s->name, i->name);
330 s->channels = i->channel_map.channels;
331 s->volume = pa_cvolume_avg(&i->volume);
332 s->base_volume = i->base_volume;
334 s->suspended = (i->state == PA_SINK_SUSPENDED);
335 SET_STRING(s->active_port, (i->active_port ? i->active_port->name : "none"));
339 static void pulse_sink_gone(int idx)
341 DBG("Pulse: REMOVE SINK #%d", idx);
342 struct pulse_sink *s = pulse_sink_lookup(idx);
344 pulse_sink_remove(s);
348 struct pulse_sink *pulse_sink_by_name(const char *name)
350 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
351 if (!strcmp(s->name, name))
356 struct pulse_sink *pulse_sink_by_idx(int idx)
358 return pulse_sink_lookup(idx);
361 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
363 PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
366 void pulse_sink_set_mute(int idx, bool mute)
368 PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
371 void pulse_sink_set_port(int idx, const char *port)
373 PULSE_ASYNC_RUN(pa_context_set_sink_port_by_index, idx, port, pulse_success_cb);
378 #define HASH_NODE struct pulse_client
379 #define HASH_PREFIX(x) pulse_client_##x
380 #define HASH_KEY_ATOMIC idx
381 #define HASH_WANT_CLEANUP
382 #define HASH_WANT_FIND
383 #define HASH_WANT_LOOKUP
384 #define HASH_WANT_REMOVE
385 #define HASH_ZERO_FILL
386 #include <ucw/hashtable.h>
388 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
390 struct pulse_op *op = userdata;
396 PULSE_STATE(PS_GET_SINKS);
397 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
403 char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
404 DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
405 i->index, i->name, i->owner_module, i->driver, host);
406 pulse_dump_proplist(i->proplist);
408 struct pulse_client *c = pulse_client_lookup(i->index);
409 if (!clist_is_linked(&c->n))
410 clist_add_tail(&pulse_client_list, &c->n);
411 SET_STRING(c->name, i->name);
412 SET_STRING(c->host, host);
416 static void pulse_client_gone(int idx)
418 DBG("Pulse: REMOVE CLIENT #%d", idx);
419 struct pulse_client *c = pulse_client_find(idx);
423 pulse_client_remove(c);
428 struct pulse_client *pulse_client_by_idx(int idx)
430 return pulse_client_find(idx);
435 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
437 pulse_op_done(userdata);
440 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
442 PULSE_STATE(PS_GET_CLIENTS);
443 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
446 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
448 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
450 uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
451 uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
454 case PA_SUBSCRIPTION_EVENT_CLIENT:
455 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
456 PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
457 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
458 pulse_client_gone(idx);
460 case PA_SUBSCRIPTION_EVENT_SOURCE:
461 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
462 PULSE_ASYNC_RUN(pa_context_get_source_info_by_index, idx, pulse_source_cb);
463 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
464 pulse_source_gone(idx);
466 case PA_SUBSCRIPTION_EVENT_SINK:
467 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
468 PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
469 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
470 pulse_sink_gone(idx);
472 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
473 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
474 PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
475 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
476 pulse_sink_input_gone(idx);
478 case PA_SUBSCRIPTION_EVENT_SERVER:
479 if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
480 PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
485 /*** Server state ***/
487 static void pulse_shutdown(void)
489 DBG("Pulse: Shutting down");
490 pulse_client_cleanup();
491 pulse_source_cleanup();
492 pulse_sink_cleanup();
493 pulse_sink_input_cleanup();
496 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
498 int state = pa_context_get_state(ctx);
499 DBG("Pulse: State callback, new state = %d", state);
500 if (state == PA_CONTEXT_READY)
502 if (pulse_state == PS_OFFLINE)
504 PULSE_STATE(PS_SUBSCRIBE);
505 pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
506 PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
511 if (pulse_state != PS_OFFLINE)
513 msg(L_INFO, "Lost connection to PulseAudio");
514 PULSE_STATE(PS_OFFLINE);
515 pulse_op_cancel_all();
519 if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
520 timer_add_rel(&pulse_connect_timer, 2000);
524 static void pulse_connect(struct main_timer *t)
526 msg(L_DEBUG, "Connecting to PulseAudio");
529 clist_init(&pulse_op_list);
530 clist_init(&pulse_client_list);
531 clist_init(&pulse_source_list);
532 clist_init(&pulse_sink_list);
533 clist_init(&pulse_sink_input_list);
537 pulse_sink_input_init();
540 pa_context_unref(pulse_ctx);
541 pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
543 pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
544 pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
547 bool pulse_is_ready(void)
549 return (pulse_state == PS_ONLINE);
552 void pulse_init(void)
556 pulse_connect_timer.handler = pulse_connect;
557 timer_add_rel(&pulse_connect_timer, 0);