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_FIND
138 #define HASH_WANT_LOOKUP
139 #define HASH_WANT_REMOVE
140 #define HASH_ZERO_FILL
141 #include <ucw/hashtable.h>
143 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
145 struct pulse_op *op = userdata;
151 PULSE_STATE(PS_GET_SERVER);
152 PULSE_ASYNC_INIT_RUN(pa_context_get_server_info, pulse_server_cb);
158 DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
159 i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
160 pulse_dump_proplist(i->proplist);
162 struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
163 if (!clist_is_linked(&s->n))
164 clist_add_tail(&pulse_sink_input_list, &s->n);
165 SET_STRING(s->name, i->name);
166 s->client_idx = i->client;
167 s->sink_idx = i->sink;
168 s->channels = i->channel_map.channels;
169 s->volume = pa_cvolume_avg(&i->volume);
174 static void pulse_sink_input_gone(int idx)
176 DBG("Pulse: REMOVE SINK INPUT #%d", idx);
177 struct pulse_sink_input *s = pulse_sink_input_find(idx);
181 pulse_sink_input_remove(s);
184 DBG("Pulse: Removing sink which does not exist");
188 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
190 PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
193 void pulse_sink_input_set_mute(int idx, bool mute)
195 PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
198 void pulse_sink_input_move(int input_idx, int sink_idx)
200 PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
205 #define HASH_NODE struct pulse_sink
206 #define HASH_PREFIX(x) pulse_sink_##x
207 #define HASH_KEY_ATOMIC idx
208 #define HASH_WANT_CLEANUP
209 #define HASH_WANT_LOOKUP
210 #define HASH_WANT_REMOVE
211 #define HASH_ZERO_FILL
212 #include <ucw/hashtable.h>
214 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
216 struct pulse_op *op = userdata;
222 PULSE_STATE(PS_GET_SINK_INPUTS);
223 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
229 DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u",
230 i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state);
231 pulse_dump_proplist(i->proplist);
233 struct pulse_sink *s = pulse_sink_lookup(i->index);
234 if (!clist_is_linked(&s->n))
235 clist_add_tail(&pulse_sink_list, &s->n);
236 SET_STRING(s->name, i->name);
237 s->channels = i->channel_map.channels;
238 s->volume = pa_cvolume_avg(&i->volume);
239 s->base_volume = i->base_volume;
244 static void pulse_sink_gone(int idx)
246 DBG("Pulse: REMOVE SINK #%d", idx);
247 struct pulse_sink *s = pulse_sink_lookup(idx);
249 pulse_sink_remove(s);
253 struct pulse_sink *pulse_sink_by_name(const char *name)
255 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
256 if (!strcmp(s->name, name))
261 struct pulse_sink *pulse_sink_by_idx(int idx)
263 return pulse_sink_lookup(idx);
266 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
268 PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
271 void pulse_sink_set_mute(int idx, bool mute)
273 PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
278 #define HASH_NODE struct pulse_client
279 #define HASH_PREFIX(x) pulse_client_##x
280 #define HASH_KEY_ATOMIC idx
281 #define HASH_WANT_CLEANUP
282 #define HASH_WANT_LOOKUP
283 #define HASH_WANT_REMOVE
284 #define HASH_ZERO_FILL
285 #include <ucw/hashtable.h>
287 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
289 struct pulse_op *op = userdata;
295 PULSE_STATE(PS_GET_SINKS);
296 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
302 char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
303 DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
304 i->index, i->name, i->owner_module, i->driver, host);
305 pulse_dump_proplist(i->proplist);
307 struct pulse_client *c = pulse_client_lookup(i->index);
308 if (!clist_is_linked(&c->n))
309 clist_add_tail(&pulse_client_list, &c->n);
310 SET_STRING(c->name, i->name);
311 SET_STRING(c->host, host);
315 static void pulse_client_gone(int idx)
317 DBG("Pulse: REMOVE CLIENT #%d", idx);
318 struct pulse_client *c = pulse_client_lookup(idx);
320 pulse_client_remove(c);
324 struct pulse_client *pulse_client_by_idx(int idx)
326 return pulse_client_lookup(idx);
331 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
333 pulse_op_done(userdata);
336 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
338 PULSE_STATE(PS_GET_CLIENTS);
339 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
342 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
344 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
346 uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
347 uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
350 case PA_SUBSCRIPTION_EVENT_CLIENT:
351 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
352 PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
353 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
354 pulse_client_gone(idx);
356 case PA_SUBSCRIPTION_EVENT_SINK:
357 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
358 PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
359 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
360 pulse_sink_gone(idx);
362 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
363 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
364 PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
365 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
366 pulse_sink_input_gone(idx);
368 case PA_SUBSCRIPTION_EVENT_SERVER:
369 if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
370 PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
375 /*** Server state ***/
377 static void pulse_shutdown(void)
379 DBG("Pulse: Shutting down");
380 pulse_client_cleanup();
381 pulse_sink_cleanup();
382 pulse_sink_input_cleanup();
385 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
387 int state = pa_context_get_state(ctx);
388 DBG("Pulse: State callback, new state = %d", state);
389 if (state == PA_CONTEXT_READY)
391 if (pulse_state == PS_OFFLINE)
393 PULSE_STATE(PS_SUBSCRIBE);
394 pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
395 PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
400 if (pulse_state != PS_OFFLINE)
402 msg(L_INFO, "Lost connection to PulseAudio");
403 PULSE_STATE(PS_OFFLINE);
404 pulse_op_cancel_all();
408 if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
409 timer_add_rel(&pulse_connect_timer, 2000);
413 static void pulse_connect(struct main_timer *t)
415 msg(L_DEBUG, "Connecting to PulseAudio");
418 clist_init(&pulse_op_list);
419 clist_init(&pulse_client_list);
420 clist_init(&pulse_sink_list);
421 clist_init(&pulse_sink_input_list);
424 pulse_sink_input_init();
427 pa_context_unref(pulse_ctx);
428 pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
430 pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
431 pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
434 void pulse_init(void)
438 pulse_connect_timer.handler = pulse_connect;
439 timer_add_rel(&pulse_connect_timer, 0);