2 * Asynchronous Interface to PulseAudio
4 * (c) 2014--2018 Martin Mares <mj@ucw.cz>
10 #include <ucw/clists.h>
11 #include <ucw/mainloop.h>
12 #include <ucw/stkstring.h>
30 enum pulse_state pulse_state;
31 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
33 clist pulse_client_list, pulse_sink_list, pulse_sink_input_list;
35 static pa_context *pulse_ctx;
36 static struct main_timer pulse_connect_timer;
38 /*** Tracking of currently running asynchronous operations ***/
46 static clist pulse_op_list;
48 static struct pulse_op *pulse_op_new(void)
50 struct pulse_op *op = xmalloc_zero(sizeof(*op));
51 clist_add_tail(&pulse_op_list, &op->n);
55 static void pulse_op_done(struct pulse_op *op)
58 pa_operation_unref(op->o);
63 static void pulse_op_cancel_all(void)
66 while (op = (struct pulse_op *) clist_head(&pulse_op_list))
68 DBG("Pulse: Cancelling pending operation");
69 pa_operation_cancel(op->o);
74 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
75 #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)
77 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
80 msg(L_ERROR, "Pulse: Failure reported");
81 pulse_op_done(userdata);
84 /*** Debugging dumps ***/
88 msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
90 CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
91 msg(L_DEBUG, "## Client #%d: %s host=%s", c->idx, c->name, c->host);
93 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
94 msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u suspended=%u port=%s",
95 s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute, s->suspended, s->active_port);
97 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
98 msg(L_DEBUG, "## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u",
99 s->idx, s->name, s->client_idx, s->sink_idx, s->channels, s->volume, s->mute);
102 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
105 void *iterator = NULL;
108 while (key = pa_proplist_iterate(pl, &iterator))
110 const char *val = pa_proplist_gets(pl, key);
111 DBG(" %s = %s", key, val);
116 /*** Server state ***/
118 char *pulse_default_sink_name;
120 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
122 struct pulse_op *op = userdata;
124 DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
125 SET_STRING(pulse_default_sink_name, i->default_sink_name);
129 PULSE_STATE(PS_ONLINE);
130 msg(L_INFO, "PulseAudio is ready");
136 void pulse_server_set_default_sink(const char *name)
138 PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
141 /*** Sink inputs ***/
143 #define HASH_NODE struct pulse_sink_input
144 #define HASH_PREFIX(x) pulse_sink_input_##x
145 #define HASH_KEY_ATOMIC idx
146 #define HASH_WANT_CLEANUP
147 #define HASH_WANT_FIND
148 #define HASH_WANT_LOOKUP
149 #define HASH_WANT_REMOVE
150 #define HASH_ZERO_FILL
151 #include <ucw/hashtable.h>
153 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_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: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
169 i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
170 pulse_dump_proplist(i->proplist);
172 struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
173 if (!clist_is_linked(&s->n))
174 clist_add_tail(&pulse_sink_input_list, &s->n);
175 SET_STRING(s->name, i->name);
176 s->client_idx = i->client;
177 s->sink_idx = i->sink;
178 s->channels = i->channel_map.channels;
179 s->volume = pa_cvolume_avg(&i->volume);
184 static void pulse_sink_input_gone(int idx)
186 DBG("Pulse: REMOVE SINK INPUT #%d", idx);
187 struct pulse_sink_input *s = pulse_sink_input_find(idx);
191 pulse_sink_input_remove(s);
194 DBG("Pulse: Removing sink which does not exist");
198 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
200 PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
203 void pulse_sink_input_set_mute(int idx, bool mute)
205 PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
208 void pulse_sink_input_move(int input_idx, int sink_idx)
210 PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
215 #define HASH_NODE struct pulse_sink
216 #define HASH_PREFIX(x) pulse_sink_##x
217 #define HASH_KEY_ATOMIC idx
218 #define HASH_WANT_CLEANUP
219 #define HASH_WANT_LOOKUP
220 #define HASH_WANT_REMOVE
221 #define HASH_ZERO_FILL
222 #include <ucw/hashtable.h>
224 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
226 struct pulse_op *op = userdata;
232 PULSE_STATE(PS_GET_SINK_INPUTS);
233 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
239 DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u port=%s",
240 i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state,
241 (i->active_port ? i->active_port->name : "none"));
242 pulse_dump_proplist(i->proplist);
244 struct pulse_sink *s = pulse_sink_lookup(i->index);
245 if (!clist_is_linked(&s->n))
246 clist_add_tail(&pulse_sink_list, &s->n);
247 SET_STRING(s->name, i->name);
248 s->channels = i->channel_map.channels;
249 s->volume = pa_cvolume_avg(&i->volume);
250 s->base_volume = i->base_volume;
252 s->suspended = (i->state == PA_SINK_SUSPENDED);
253 SET_STRING(s->active_port, (i->active_port ? i->active_port->name : "none"));
257 static void pulse_sink_gone(int idx)
259 DBG("Pulse: REMOVE SINK #%d", idx);
260 struct pulse_sink *s = pulse_sink_lookup(idx);
262 pulse_sink_remove(s);
266 struct pulse_sink *pulse_sink_by_name(const char *name)
268 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
269 if (!strcmp(s->name, name))
274 struct pulse_sink *pulse_sink_by_idx(int idx)
276 return pulse_sink_lookup(idx);
279 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
281 PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
284 void pulse_sink_set_mute(int idx, bool mute)
286 PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
289 void pulse_sink_set_port(int idx, const char *port)
291 PULSE_ASYNC_RUN(pa_context_set_sink_port_by_index, idx, port, pulse_success_cb);
296 #define HASH_NODE struct pulse_client
297 #define HASH_PREFIX(x) pulse_client_##x
298 #define HASH_KEY_ATOMIC idx
299 #define HASH_WANT_CLEANUP
300 #define HASH_WANT_FIND
301 #define HASH_WANT_LOOKUP
302 #define HASH_WANT_REMOVE
303 #define HASH_ZERO_FILL
304 #include <ucw/hashtable.h>
306 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
308 struct pulse_op *op = userdata;
314 PULSE_STATE(PS_GET_SINKS);
315 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
321 char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
322 DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
323 i->index, i->name, i->owner_module, i->driver, host);
324 pulse_dump_proplist(i->proplist);
326 struct pulse_client *c = pulse_client_lookup(i->index);
327 if (!clist_is_linked(&c->n))
328 clist_add_tail(&pulse_client_list, &c->n);
329 SET_STRING(c->name, i->name);
330 SET_STRING(c->host, host);
334 static void pulse_client_gone(int idx)
336 DBG("Pulse: REMOVE CLIENT #%d", idx);
337 struct pulse_client *c = pulse_client_find(idx);
341 pulse_client_remove(c);
346 struct pulse_client *pulse_client_by_idx(int idx)
348 return pulse_client_find(idx);
353 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
355 pulse_op_done(userdata);
358 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
360 PULSE_STATE(PS_GET_CLIENTS);
361 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
364 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
366 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
368 uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
369 uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
372 case PA_SUBSCRIPTION_EVENT_CLIENT:
373 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
374 PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
375 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
376 pulse_client_gone(idx);
378 case PA_SUBSCRIPTION_EVENT_SINK:
379 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
380 PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
381 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
382 pulse_sink_gone(idx);
384 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
385 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
386 PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
387 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
388 pulse_sink_input_gone(idx);
390 case PA_SUBSCRIPTION_EVENT_SERVER:
391 if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
392 PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
397 /*** Server state ***/
399 static void pulse_shutdown(void)
401 DBG("Pulse: Shutting down");
402 pulse_client_cleanup();
403 pulse_sink_cleanup();
404 pulse_sink_input_cleanup();
407 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
409 int state = pa_context_get_state(ctx);
410 DBG("Pulse: State callback, new state = %d", state);
411 if (state == PA_CONTEXT_READY)
413 if (pulse_state == PS_OFFLINE)
415 PULSE_STATE(PS_SUBSCRIBE);
416 pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
417 PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
422 if (pulse_state != PS_OFFLINE)
424 msg(L_INFO, "Lost connection to PulseAudio");
425 PULSE_STATE(PS_OFFLINE);
426 pulse_op_cancel_all();
430 if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
431 timer_add_rel(&pulse_connect_timer, 2000);
435 static void pulse_connect(struct main_timer *t)
437 msg(L_DEBUG, "Connecting to PulseAudio");
440 clist_init(&pulse_op_list);
441 clist_init(&pulse_client_list);
442 clist_init(&pulse_sink_list);
443 clist_init(&pulse_sink_input_list);
446 pulse_sink_input_init();
449 pa_context_unref(pulse_ctx);
450 pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
452 pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
453 pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
456 bool pulse_is_ready(void)
458 return (pulse_state == PS_ONLINE);
461 void pulse_init(void)
465 pulse_connect_timer.handler = pulse_connect;
466 timer_add_rel(&pulse_connect_timer, 0);