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 #define SET_STRING(_field, _val) do { if (!_field || strcmp(_field, _val)) { xfree(_field); _field = xstrdup(_val); } } while (0)
30 /*** Tracking of currently running asynchronous operations ***/
38 static clist pulse_op_list;
40 static struct pulse_op *pulse_op_new(void)
42 struct pulse_op *op = xmalloc_zero(sizeof(*op));
43 clist_add_tail(&pulse_op_list, &op->n);
47 static void pulse_op_done(struct pulse_op *op)
50 pa_operation_unref(op->o);
55 static void pulse_op_cancel_all(void)
58 while (op = (struct pulse_op *) clist_head(&pulse_op_list))
60 DBG("Pulse: Cancelling pending operation");
61 pa_operation_cancel(op->o);
66 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(pulse_ctx, __VA_ARGS__, _op); } while (0)
67 #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)
69 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
72 msg(L_ERROR, "Pulse: Failure reported");
73 pulse_op_done(userdata);
76 /*** Debugging dumps ***/
80 msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
82 CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
83 msg(L_DEBUG, "## Client #%d: %s host=%s", c->idx, c->name, c->host);
85 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
86 msg(L_DEBUG, "## Sink #%d: %s channels=%u volume=%u base_vol=%u mute=%u",
87 s->idx, s->name, s->channels, s->volume, s->base_volume, s->mute);
89 CLIST_FOR_EACH(struct pulse_sink_input *, s, pulse_sink_input_list)
90 msg(L_DEBUG, "## Sink input #%d: %s client=%d sink=%d channels=%u volume=%u mute=%u",
91 s->idx, s->name, s->client_idx, s->sink_idx, s->channels, s->volume, s->mute);
94 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
97 void *iterator = NULL;
100 while (key = pa_proplist_iterate(pl, &iterator))
102 const char *val = pa_proplist_gets(pl, key);
103 DBG(" %s = %s", key, val);
108 /*** Server state ***/
110 char *pulse_default_sink_name;
112 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
114 struct pulse_op *op = userdata;
116 DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
117 SET_STRING(pulse_default_sink_name, i->default_sink_name);
121 PULSE_STATE(PS_ONLINE);
122 msg(L_INFO, "PulseAudio is ready");
128 void pulse_server_set_default_sink(const char *name)
130 PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
133 /*** Sink inputs ***/
135 #define HASH_NODE struct pulse_sink_input
136 #define HASH_PREFIX(x) pulse_sink_input_##x
137 #define HASH_KEY_ATOMIC idx
138 #define HASH_WANT_CLEANUP
139 #define HASH_WANT_LOOKUP
140 #define HASH_WANT_REMOVE
141 #define HASH_ZERO_FILL
142 #include <ucw/hashtable.h>
144 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
146 struct pulse_op *op = userdata;
152 PULSE_STATE(PS_GET_SERVER);
153 PULSE_ASYNC_INIT_RUN(pa_context_get_server_info, pulse_server_cb);
159 DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
160 i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
161 pulse_dump_proplist(i->proplist);
163 struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
164 if (!clist_is_linked(&s->n))
165 clist_add_tail(&pulse_sink_input_list, &s->n);
166 SET_STRING(s->name, i->name);
167 s->client_idx = i->client;
168 s->sink_idx = i->sink;
169 s->channels = i->channel_map.channels;
170 s->volume = pa_cvolume_avg(&i->volume);
175 static void pulse_sink_input_gone(int idx)
177 DBG("Pulse: REMOVE SINK INPUT #%d", idx);
178 struct pulse_sink_input *s = pulse_sink_input_lookup(idx);
180 pulse_sink_input_remove(s);
184 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
186 PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
189 void pulse_sink_input_set_mute(int idx, bool mute)
191 PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
194 void pulse_sink_input_move(int input_idx, int sink_idx)
196 PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
201 #define HASH_NODE struct pulse_sink
202 #define HASH_PREFIX(x) pulse_sink_##x
203 #define HASH_KEY_ATOMIC idx
204 #define HASH_WANT_CLEANUP
205 #define HASH_WANT_LOOKUP
206 #define HASH_WANT_REMOVE
207 #define HASH_ZERO_FILL
208 #include <ucw/hashtable.h>
210 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
212 struct pulse_op *op = userdata;
218 PULSE_STATE(PS_GET_SINK_INPUTS);
219 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
225 DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u",
226 i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state);
227 pulse_dump_proplist(i->proplist);
229 struct pulse_sink *s = pulse_sink_lookup(i->index);
230 if (!clist_is_linked(&s->n))
231 clist_add_tail(&pulse_sink_list, &s->n);
232 SET_STRING(s->name, i->name);
233 s->channels = i->channel_map.channels;
234 s->volume = pa_cvolume_avg(&i->volume);
235 s->base_volume = i->base_volume;
240 static void pulse_sink_gone(int idx)
242 DBG("Pulse: REMOVE SINK #%d", idx);
243 struct pulse_sink *s = pulse_sink_lookup(idx);
245 pulse_sink_remove(s);
249 struct pulse_sink *pulse_sink_by_name(const char *name)
251 CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
252 if (!strcmp(s->name, name))
257 struct pulse_sink *pulse_sink_by_idx(int idx)
259 return pulse_sink_lookup(idx);
262 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
264 PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
267 void pulse_sink_set_mute(int idx, bool mute)
269 PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
274 #define HASH_NODE struct pulse_client
275 #define HASH_PREFIX(x) pulse_client_##x
276 #define HASH_KEY_ATOMIC idx
277 #define HASH_WANT_CLEANUP
278 #define HASH_WANT_LOOKUP
279 #define HASH_WANT_REMOVE
280 #define HASH_ZERO_FILL
281 #include <ucw/hashtable.h>
283 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
285 struct pulse_op *op = userdata;
291 PULSE_STATE(PS_GET_SINKS);
292 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
298 char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
299 DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
300 i->index, i->name, i->owner_module, i->driver, host);
301 pulse_dump_proplist(i->proplist);
303 struct pulse_client *c = pulse_client_lookup(i->index);
304 if (!clist_is_linked(&c->n))
305 clist_add_tail(&pulse_client_list, &c->n);
306 SET_STRING(c->name, i->name);
307 SET_STRING(c->host, host);
311 static void pulse_client_gone(int idx)
313 DBG("Pulse: REMOVE CLIENT #%d", idx);
314 struct pulse_client *c = pulse_client_lookup(idx);
316 pulse_client_remove(c);
320 struct pulse_client *pulse_client_by_idx(int idx)
322 return pulse_client_lookup(idx);
327 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
329 pulse_op_done(userdata);
332 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
334 PULSE_STATE(PS_GET_CLIENTS);
335 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
338 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
340 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
342 uns object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
343 uns action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
346 case PA_SUBSCRIPTION_EVENT_CLIENT:
347 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
348 PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
349 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
350 pulse_client_gone(idx);
352 case PA_SUBSCRIPTION_EVENT_SINK:
353 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
354 PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
355 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
356 pulse_sink_gone(idx);
358 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
359 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
360 PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
361 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
362 pulse_sink_input_gone(idx);
364 case PA_SUBSCRIPTION_EVENT_SERVER:
365 if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
366 PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
371 /*** Server state ***/
373 static void pulse_shutdown(void)
375 DBG("Pulse: Shutting down");
376 pulse_client_cleanup();
377 pulse_sink_cleanup();
378 pulse_sink_input_cleanup();
381 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
383 int state = pa_context_get_state(ctx);
384 DBG("Pulse: State callback, new state = %d", state);
385 if (state == PA_CONTEXT_READY)
387 if (pulse_state == PS_OFFLINE)
389 PULSE_STATE(PS_SUBSCRIBE);
390 pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
391 PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
396 if (pulse_state != PS_OFFLINE)
398 msg(L_INFO, "Lost connection to PulseAudio");
399 PULSE_STATE(PS_OFFLINE);
400 pulse_op_cancel_all();
404 if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
405 timer_add_rel(&pulse_connect_timer, 2000);
409 static void pulse_connect(struct main_timer *t)
411 msg(L_DEBUG, "Connecting to PulseAudio");
414 clist_init(&pulse_op_list);
415 clist_init(&pulse_client_list);
416 clist_init(&pulse_sink_list);
417 clist_init(&pulse_sink_input_list);
420 pulse_sink_input_init();
423 pa_context_unref(pulse_ctx);
424 pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
426 pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
427 pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
430 void pulse_init(void)
434 pulse_connect_timer.handler = pulse_connect;
435 timer_add_rel(&pulse_connect_timer, 0);