]> mj.ucw.cz Git - ursary.git/blob - pulse.c
d6a26b613c28bf488b433c6f465cd44bf7f7684a
[ursary.git] / pulse.c
1 /*
2  *      Asynchronous Interface to PulseAudio
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #undef LOCAL_DEBUG
8
9 #include <ucw/lib.h>
10 #include <ucw/clists.h>
11 #include <ucw/mainloop.h>
12 #include <ucw/stkstring.h>
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include "ursaryd.h"
19
20 enum pulse_state {
21   PS_OFFLINE,
22   PS_SUBSCRIBE,
23   PS_GET_CLIENTS,
24   PS_GET_SINKS,
25   PS_GET_SINK_INPUTS,
26   PS_GET_SERVER,
27   PS_ONLINE,
28 };
29
30 enum pulse_state pulse_state;
31 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
32
33 clist pulse_client_list, pulse_sink_list, pulse_sink_input_list;
34
35 static pa_context *pulse_ctx;
36 static struct main_timer pulse_connect_timer;
37
38 /*** Tracking of currently running asynchronous operations ***/
39
40 struct pulse_op {
41   cnode n;
42   pa_operation *o;
43   bool is_init;
44 };
45
46 static clist pulse_op_list;
47
48 static struct pulse_op *pulse_op_new(void)
49 {
50   struct pulse_op *op = xmalloc_zero(sizeof(*op));
51   clist_add_tail(&pulse_op_list, &op->n);
52   return op;
53 }
54
55 static void pulse_op_done(struct pulse_op *op)
56 {
57   if (op->o)
58     pa_operation_unref(op->o);
59   clist_remove(&op->n);
60   xfree(op);
61 }
62
63 static void pulse_op_cancel_all(void)
64 {
65   struct pulse_op *op;
66   while (op = (struct pulse_op *) clist_head(&pulse_op_list))
67     {
68       DBG("Pulse: Cancelling pending operation");
69       pa_operation_cancel(op->o);
70       pulse_op_done(op);
71     }
72 }
73
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)
76
77 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
78 {
79   if (!success)
80     msg(L_ERROR, "Pulse: Failure reported");
81   pulse_op_done(userdata);
82 }
83
84 /*** Debugging dumps ***/
85
86 void pulse_dump(void)
87 {
88   msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
89
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);
92
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);
96
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);
100 }
101
102 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
103 {
104 #if 0
105   void *iterator = NULL;
106   const char *key;
107
108   while (key = pa_proplist_iterate(pl, &iterator))
109     {
110       const char *val = pa_proplist_gets(pl, key);
111       DBG("   %s = %s", key, val);
112     }
113 #endif
114 }
115
116 /*** Server state ***/
117
118 char *pulse_default_sink_name;
119
120 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
121 {
122   struct pulse_op *op = userdata;
123
124   DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
125   SET_STRING(pulse_default_sink_name, i->default_sink_name);
126
127   if (op->is_init)
128     {
129       PULSE_STATE(PS_ONLINE);
130       msg(L_INFO, "PulseAudio is ready");
131     }
132   pulse_op_done(op);
133   schedule_update();
134 }
135
136 void pulse_server_set_default_sink(const char *name)
137 {
138   PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
139 }
140
141 /*** Sink inputs ***/
142
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>
152
153 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
154 {
155   struct pulse_op *op = userdata;
156
157   if (eol)
158     {
159       if (op->is_init)
160         {
161           PULSE_STATE(PS_GET_SERVER);
162           PULSE_ASYNC_INIT_RUN(pa_context_get_server_info, pulse_server_cb);
163         }
164       pulse_op_done(op);
165       return;
166     }
167
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);
171
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);
180   s->mute = i->mute;
181   schedule_update();
182 }
183
184 static void pulse_sink_input_gone(int idx)
185 {
186   DBG("Pulse: REMOVE SINK INPUT #%d", idx);
187   struct pulse_sink_input *s = pulse_sink_input_find(idx);
188   if (s)
189     {
190       clist_remove(&s->n);
191       pulse_sink_input_remove(s);
192     }
193   else
194     DBG("Pulse: Removing sink which does not exist");
195   schedule_update();
196 }
197
198 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
199 {
200   PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
201 }
202
203 void pulse_sink_input_set_mute(int idx, bool mute)
204 {
205   PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
206 }
207
208 void pulse_sink_input_move(int input_idx, int sink_idx)
209 {
210   PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
211 }
212
213 /*** Sinks ***/
214
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>
223
224 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
225 {
226   struct pulse_op *op = userdata;
227
228   if (eol)
229     {
230       if (op->is_init)
231         {
232           PULSE_STATE(PS_GET_SINK_INPUTS);
233           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
234         }
235       pulse_op_done(op);
236       return;
237     }
238
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);
242
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;
250   s->mute = i->mute;
251   s->suspended = (i->state == PA_SINK_SUSPENDED);
252   schedule_update();
253 }
254
255 static void pulse_sink_gone(int idx)
256 {
257   DBG("Pulse: REMOVE SINK #%d", idx);
258   struct pulse_sink *s = pulse_sink_lookup(idx);
259   clist_remove(&s->n);
260   pulse_sink_remove(s);
261   schedule_update();
262 }
263
264 struct pulse_sink *pulse_sink_by_name(const char *name)
265 {
266   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
267     if (!strcmp(s->name, name))
268       return s;
269   return NULL;
270 }
271
272 struct pulse_sink *pulse_sink_by_idx(int idx)
273 {
274   return pulse_sink_lookup(idx);
275 }
276
277 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
278 {
279   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
280 }
281
282 void pulse_sink_set_mute(int idx, bool mute)
283 {
284   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
285 }
286
287 /*** Clients ***/
288
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_FIND
294 #define HASH_WANT_LOOKUP
295 #define HASH_WANT_REMOVE
296 #define HASH_ZERO_FILL
297 #include <ucw/hashtable.h>
298
299 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
300 {
301   struct pulse_op *op = userdata;
302
303   if (eol)
304     {
305       if (op->is_init)
306         {
307           PULSE_STATE(PS_GET_SINKS);
308           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
309         }
310       pulse_op_done(op);
311       return;
312     }
313
314   char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
315   DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
316     i->index, i->name, i->owner_module, i->driver, host);
317   pulse_dump_proplist(i->proplist);
318
319   struct pulse_client *c = pulse_client_lookup(i->index);
320   if (!clist_is_linked(&c->n))
321     clist_add_tail(&pulse_client_list, &c->n);
322   SET_STRING(c->name, i->name);
323   SET_STRING(c->host, host);
324   schedule_update();
325 }
326
327 static void pulse_client_gone(int idx)
328 {
329   DBG("Pulse: REMOVE CLIENT #%d", idx);
330   struct pulse_client *c = pulse_client_find(idx);
331   if (c)
332     {
333       clist_remove(&c->n);
334       pulse_client_remove(c);
335       schedule_update();
336     }
337 }
338
339 struct pulse_client *pulse_client_by_idx(int idx)
340 {
341   return pulse_client_find(idx);
342 }
343
344 /*** Events ***/
345
346 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
347 {
348   pulse_op_done(userdata);
349
350   if (!success)
351     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
352
353   PULSE_STATE(PS_GET_CLIENTS);
354   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
355 }
356
357 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
358 {
359   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
360
361   uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
362   uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
363   switch (object)
364     {
365     case PA_SUBSCRIPTION_EVENT_CLIENT:
366       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
367         PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
368       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
369         pulse_client_gone(idx);
370       break;
371     case PA_SUBSCRIPTION_EVENT_SINK:
372       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
373         PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
374       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
375         pulse_sink_gone(idx);
376       break;
377     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
378       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
379         PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
380       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
381         pulse_sink_input_gone(idx);
382       break;
383     case PA_SUBSCRIPTION_EVENT_SERVER:
384       if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
385         PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
386       break;
387     }
388 }
389
390 /*** Server state ***/
391
392 static void pulse_shutdown(void)
393 {
394   DBG("Pulse: Shutting down");
395   pulse_client_cleanup();
396   pulse_sink_cleanup();
397   pulse_sink_input_cleanup();
398 }
399
400 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
401 {
402   int state = pa_context_get_state(ctx);
403   DBG("Pulse: State callback, new state = %d", state);
404   if (state == PA_CONTEXT_READY)
405     {
406       if (pulse_state == PS_OFFLINE)
407         {
408           PULSE_STATE(PS_SUBSCRIBE);
409           pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
410           PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
411         }
412     }
413   else
414     {
415       if (pulse_state != PS_OFFLINE)
416         {
417           msg(L_INFO, "Lost connection to PulseAudio");
418           PULSE_STATE(PS_OFFLINE);
419           pulse_op_cancel_all();
420           pulse_shutdown();
421           schedule_update();
422         }
423       if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
424         timer_add_rel(&pulse_connect_timer, 2000);
425     }
426 }
427
428 static void pulse_connect(struct main_timer *t)
429 {
430   msg(L_DEBUG, "Connecting to PulseAudio");
431   timer_del(t);
432
433   clist_init(&pulse_op_list);
434   clist_init(&pulse_client_list);
435   clist_init(&pulse_sink_list);
436   clist_init(&pulse_sink_input_list);
437   pulse_client_init();
438   pulse_sink_init();
439   pulse_sink_input_init();
440
441   if (pulse_ctx)
442     pa_context_unref(pulse_ctx);
443   pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
444
445   pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
446   pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
447 }
448
449 bool pulse_is_ready(void)
450 {
451   return (pulse_state == PS_ONLINE);
452 }
453
454 void pulse_init(void)
455 {
456   pmain_init();
457
458   pulse_connect_timer.handler = pulse_connect;
459   timer_add_rel(&pulse_connect_timer, 0);
460 }