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>
20 enum pulse_state pulse_state;
21 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
23 clist pulse_client_list, pulse_sink_list, pulse_sink_input_list;
25 static pa_context *pulse_ctx;
26 static struct main_timer pulse_connect_timer;
28 /*** Tracking of currently running asynchronous operations ***/
36 static clist pulse_op_list;
38 static struct pulse_op *pulse_op_new(void)
40 struct pulse_op *op = xmalloc_zero(sizeof(*op));
41 clist_add_tail(&pulse_op_list, &op->n);
45 static void pulse_op_done(struct pulse_op *op)
48 pa_operation_unref(op->o);
53 static void pulse_op_cancel_all(void)
56 while (op = (struct pulse_op *) clist_head(&pulse_op_list))
58 DBG("Pulse: Cancelling pending operation");
59 pa_operation_cancel(op->o);
64 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
65 #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)
67 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
70 msg(L_ERROR, "Pulse: Failure reported");
71 pulse_op_done(userdata);
74 /*** Debugging dumps ***/
78 msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
80 CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
81 msg(L_DEBUG, "## Client #%d: %s host=%s", c->idx, c->name, c->host);
83 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
84 msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u",
85 s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute);
87 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
88 msg(L_DEBUG, "## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u",
89 s->idx, s->name, s->client_idx, s->sink_idx, s->channels, s->volume, s->mute);
92 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
95 void *iterator = NULL;
98 while (key = pa_proplist_iterate(pl, &iterator))
100 const char *val = pa_proplist_gets(pl, key);
101 DBG(" %s = %s", key, val);
106 /*** Server state ***/
108 char *pulse_default_sink_name;
110 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
112 struct pulse_op *op = userdata;
114 DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
115 SET_STRING(pulse_default_sink_name, i->default_sink_name);
119 PULSE_STATE(PS_ONLINE);
120 msg(L_INFO, "PulseAudio is ready");
126 void pulse_server_set_default_sink(const char *name)
128 PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
131 /*** Sink inputs ***/
133 #define HASH_NODE struct pulse_sink_input
134 #define HASH_PREFIX(x) pulse_sink_input_##x
135 #define HASH_KEY_ATOMIC idx
136 #define HASH_WANT_CLEANUP
137 #define HASH_WANT_LOOKUP
138 #define HASH_WANT_REMOVE
139 #define HASH_ZERO_FILL
140 #include <ucw/hashtable.h>
142 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
144 struct pulse_op *op = userdata;
150 PULSE_STATE(PS_GET_SERVER);
151 PULSE_ASYNC_INIT_RUN(pa_context_get_server_info, pulse_server_cb);
157 DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
158 i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
159 pulse_dump_proplist(i->proplist);
161 struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
162 if (!clist_is_linked(&s->n))
163 clist_add_tail(&pulse_sink_input_list, &s->n);
164 SET_STRING(s->name, i->name);
165 s->client_idx = i->client;
166 s->sink_idx = i->sink;
167 s->channels = i->channel_map.channels;
168 s->volume = pa_cvolume_avg(&i->volume);
173 static void pulse_sink_input_gone(int idx)
175 DBG("Pulse: REMOVE SINK INPUT #%d", idx);
176 struct pulse_sink_input *s = pulse_sink_input_lookup(idx);
178 pulse_sink_input_remove(s);
182 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
184 PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
187 void pulse_sink_input_set_mute(int idx, bool mute)
189 PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
192 void pulse_sink_input_move(int input_idx, int sink_idx)
194 PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
199 #define HASH_NODE struct pulse_sink
200 #define HASH_PREFIX(x) pulse_sink_##x
201 #define HASH_KEY_ATOMIC idx
202 #define HASH_WANT_CLEANUP
203 #define HASH_WANT_LOOKUP
204 #define HASH_WANT_REMOVE
205 #define HASH_ZERO_FILL
206 #include <ucw/hashtable.h>
208 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
210 struct pulse_op *op = userdata;
216 PULSE_STATE(PS_GET_SINK_INPUTS);
217 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
223 DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u",
224 i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state);
225 pulse_dump_proplist(i->proplist);
227 struct pulse_sink *s = pulse_sink_lookup(i->index);
228 if (!clist_is_linked(&s->n))
229 clist_add_tail(&pulse_sink_list, &s->n);
230 SET_STRING(s->name, i->name);
231 s->channels = i->channel_map.channels;
232 s->volume = pa_cvolume_avg(&i->volume);
233 s->base_volume = i->base_volume;
238 static void pulse_sink_gone(int idx)
240 DBG("Pulse: REMOVE SINK #%d", idx);
241 struct pulse_sink *s = pulse_sink_lookup(idx);
243 pulse_sink_remove(s);
247 struct pulse_sink *pulse_sink_by_name(const char *name)
249 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
250 if (!strcmp(s->name, name))
255 struct pulse_sink *pulse_sink_by_idx(int idx)
257 return pulse_sink_lookup(idx);
260 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
262 PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
265 void pulse_sink_set_mute(int idx, bool mute)
267 PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
272 #define HASH_NODE struct pulse_client
273 #define HASH_PREFIX(x) pulse_client_##x
274 #define HASH_KEY_ATOMIC idx
275 #define HASH_WANT_CLEANUP
276 #define HASH_WANT_LOOKUP
277 #define HASH_WANT_REMOVE
278 #define HASH_ZERO_FILL
279 #include <ucw/hashtable.h>
281 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
283 struct pulse_op *op = userdata;
289 PULSE_STATE(PS_GET_SINKS);
290 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
296 char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
297 DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
298 i->index, i->name, i->owner_module, i->driver, host);
299 pulse_dump_proplist(i->proplist);
301 struct pulse_client *c = pulse_client_lookup(i->index);
302 if (!clist_is_linked(&c->n))
303 clist_add_tail(&pulse_client_list, &c->n);
304 SET_STRING(c->name, i->name);
305 SET_STRING(c->host, host);
309 static void pulse_client_gone(int idx)
311 DBG("Pulse: REMOVE CLIENT #%d", idx);
312 struct pulse_client *c = pulse_client_lookup(idx);
314 pulse_client_remove(c);
318 struct pulse_client *pulse_client_by_idx(int idx)
320 return pulse_client_lookup(idx);
325 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
327 pulse_op_done(userdata);
330 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
332 PULSE_STATE(PS_GET_CLIENTS);
333 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
336 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
338 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
340 uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
341 uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
344 case PA_SUBSCRIPTION_EVENT_CLIENT:
345 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
346 PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
347 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
348 pulse_client_gone(idx);
350 case PA_SUBSCRIPTION_EVENT_SINK:
351 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
352 PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
353 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
354 pulse_sink_gone(idx);
356 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
357 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
358 PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
359 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
360 pulse_sink_input_gone(idx);
362 case PA_SUBSCRIPTION_EVENT_SERVER:
363 if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
364 PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
369 /*** Server state ***/
371 static void pulse_shutdown(void)
373 DBG("Pulse: Shutting down");
374 pulse_client_cleanup();
375 pulse_sink_cleanup();
376 pulse_sink_input_cleanup();
379 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
381 int state = pa_context_get_state(ctx);
382 DBG("Pulse: State callback, new state = %d", state);
383 if (state == PA_CONTEXT_READY)
385 if (pulse_state == PS_OFFLINE)
387 PULSE_STATE(PS_SUBSCRIBE);
388 pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
389 PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
394 if (pulse_state != PS_OFFLINE)
396 msg(L_INFO, "Lost connection to PulseAudio");
397 PULSE_STATE(PS_OFFLINE);
398 pulse_op_cancel_all();
402 if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
403 timer_add_rel(&pulse_connect_timer, 2000);
407 static void pulse_connect(struct main_timer *t)
409 msg(L_DEBUG, "Connecting to PulseAudio");
412 clist_init(&pulse_op_list);
413 clist_init(&pulse_client_list);
414 clist_init(&pulse_sink_list);
415 clist_init(&pulse_sink_input_list);
418 pulse_sink_input_init();
421 pa_context_unref(pulse_ctx);
422 pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
424 pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
425 pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
428 void pulse_init(void)
432 pulse_connect_timer.handler = pulse_connect;
433 timer_add_rel(&pulse_connect_timer, 0);