2 * Asynchronous Interface to PulseAudio
4 * (c) 2014 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",
95 s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute, s->suspended);
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",
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 pulse_dump_proplist(i->proplist);
243 struct pulse_sink *s = pulse_sink_lookup(i->index);
244 if (!clist_is_linked(&s->n))
245 clist_add_tail(&pulse_sink_list, &s->n);
246 SET_STRING(s->name, i->name);
247 s->channels = i->channel_map.channels;
248 s->volume = pa_cvolume_avg(&i->volume);
249 s->base_volume = i->base_volume;
251 s->suspended = (i->state == PA_SINK_SUSPENDED);
255 static void pulse_sink_gone(int idx)
257 DBG("Pulse: REMOVE SINK #%d", idx);
258 struct pulse_sink *s = pulse_sink_lookup(idx);
260 pulse_sink_remove(s);
264 struct pulse_sink *pulse_sink_by_name(const char *name)
266 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
267 if (!strcmp(s->name, name))
272 struct pulse_sink *pulse_sink_by_idx(int idx)
274 return pulse_sink_lookup(idx);
277 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
279 PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
282 void pulse_sink_set_mute(int idx, bool mute)
284 PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
289 #define HASH_NODE struct pulse_client
290 #define HASH_PREFIX(x) pulse_client_##x
291 #define HASH_KEY_ATOMIC idx
292 #define HASH_WANT_CLEANUP
293 #define HASH_WANT_LOOKUP
294 #define HASH_WANT_REMOVE
295 #define HASH_ZERO_FILL
296 #include <ucw/hashtable.h>
298 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
300 struct pulse_op *op = userdata;
306 PULSE_STATE(PS_GET_SINKS);
307 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
313 char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
314 DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
315 i->index, i->name, i->owner_module, i->driver, host);
316 pulse_dump_proplist(i->proplist);
318 struct pulse_client *c = pulse_client_lookup(i->index);
319 if (!clist_is_linked(&c->n))
320 clist_add_tail(&pulse_client_list, &c->n);
321 SET_STRING(c->name, i->name);
322 SET_STRING(c->host, host);
326 static void pulse_client_gone(int idx)
328 DBG("Pulse: REMOVE CLIENT #%d", idx);
329 struct pulse_client *c = pulse_client_lookup(idx);
331 pulse_client_remove(c);
335 struct pulse_client *pulse_client_by_idx(int idx)
337 return pulse_client_lookup(idx);
342 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
344 pulse_op_done(userdata);
347 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
349 PULSE_STATE(PS_GET_CLIENTS);
350 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
353 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
355 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
357 uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
358 uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
361 case PA_SUBSCRIPTION_EVENT_CLIENT:
362 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
363 PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
364 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
365 pulse_client_gone(idx);
367 case PA_SUBSCRIPTION_EVENT_SINK:
368 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
369 PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
370 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
371 pulse_sink_gone(idx);
373 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
374 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
375 PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
376 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
377 pulse_sink_input_gone(idx);
379 case PA_SUBSCRIPTION_EVENT_SERVER:
380 if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
381 PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
386 /*** Server state ***/
388 static void pulse_shutdown(void)
390 DBG("Pulse: Shutting down");
391 pulse_client_cleanup();
392 pulse_sink_cleanup();
393 pulse_sink_input_cleanup();
396 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
398 int state = pa_context_get_state(ctx);
399 DBG("Pulse: State callback, new state = %d", state);
400 if (state == PA_CONTEXT_READY)
402 if (pulse_state == PS_OFFLINE)
404 PULSE_STATE(PS_SUBSCRIBE);
405 pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
406 PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
411 if (pulse_state != PS_OFFLINE)
413 msg(L_INFO, "Lost connection to PulseAudio");
414 PULSE_STATE(PS_OFFLINE);
415 pulse_op_cancel_all();
419 if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
420 timer_add_rel(&pulse_connect_timer, 2000);
424 static void pulse_connect(struct main_timer *t)
426 msg(L_DEBUG, "Connecting to PulseAudio");
429 clist_init(&pulse_op_list);
430 clist_init(&pulse_client_list);
431 clist_init(&pulse_sink_list);
432 clist_init(&pulse_sink_input_list);
435 pulse_sink_input_init();
438 pa_context_unref(pulse_ctx);
439 pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
441 pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
442 pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
445 bool pulse_is_ready(void)
447 return (pulse_state == PS_ONLINE);
450 void pulse_init(void)
454 pulse_connect_timer.handler = pulse_connect;
455 timer_add_rel(&pulse_connect_timer, 0);