]> mj.ucw.cz Git - ursary.git/blob - pulse.c
21005ba79a50f02bfad9fe8da0b4da5b3521c089
[ursary.git] / pulse.c
1 /*
2  *      Asynchronous Interface to PulseAudio
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #define 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 pulse_state;
21 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
22
23 clist pulse_client_list, pulse_sink_list, pulse_sink_input_list;
24
25 static pa_context *pulse_ctx;
26 static struct main_timer pulse_connect_timer;
27
28 /*** Tracking of currently running asynchronous operations ***/
29
30 struct pulse_op {
31   cnode n;
32   pa_operation *o;
33   bool is_init;
34 };
35
36 static clist pulse_op_list;
37
38 static struct pulse_op *pulse_op_new(void)
39 {
40   struct pulse_op *op = xmalloc_zero(sizeof(*op));
41   clist_add_tail(&pulse_op_list, &op->n);
42   return op;
43 }
44
45 static void pulse_op_done(struct pulse_op *op)
46 {
47   if (op->o)
48     pa_operation_unref(op->o);
49   clist_remove(&op->n);
50   xfree(op);
51 }
52
53 static void pulse_op_cancel_all(void)
54 {
55   struct pulse_op *op;
56   while (op = (struct pulse_op *) clist_head(&pulse_op_list))
57     {
58       DBG("Pulse: Cancelling pending operation");
59       pa_operation_cancel(op->o);
60       pulse_op_done(op);
61     }
62 }
63
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)
66
67 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
68 {
69   if (!success)
70     msg(L_ERROR, "Pulse: Failure reported");
71   pulse_op_done(userdata);
72 }
73
74 /*** Debugging dumps ***/
75
76 void pulse_dump(void)
77 {
78   msg(L_DEBUG, "## Server: default_sink=%s", pulse_default_sink_name);
79
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);
82
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);
86
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);
90 }
91
92 static void pulse_dump_proplist(pa_proplist *pl UNUSED)
93 {
94 #if 0
95   void *iterator = NULL;
96   const char *key;
97
98   while (key = pa_proplist_iterate(pl, &iterator))
99     {
100       const char *val = pa_proplist_gets(pl, key);
101       DBG("   %s = %s", key, val);
102     }
103 #endif
104 }
105
106 /*** Server state ***/
107
108 char *pulse_default_sink_name;
109
110 static void pulse_server_cb(pa_context *ctx UNUSED, const pa_server_info *i, void *userdata)
111 {
112   struct pulse_op *op = userdata;
113
114   DBG("Pulse: SERVER default_sink=%s", i->default_sink_name);
115   SET_STRING(pulse_default_sink_name, i->default_sink_name);
116
117   if (op->is_init)
118     {
119       PULSE_STATE(PS_ONLINE);
120       msg(L_INFO, "PulseAudio is ready");
121     }
122   pulse_op_done(op);
123   schedule_update();
124 }
125
126 void pulse_server_set_default_sink(const char *name)
127 {
128   PULSE_ASYNC_RUN(pa_context_set_default_sink, name, pulse_success_cb);
129 }
130
131 /*** Sink inputs ***/
132
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>
141
142 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
143 {
144   struct pulse_op *op = userdata;
145
146   if (eol)
147     {
148       if (op->is_init)
149         {
150           PULSE_STATE(PS_GET_SERVER);
151           PULSE_ASYNC_INIT_RUN(pa_context_get_server_info, pulse_server_cb);
152         }
153       pulse_op_done(op);
154       return;
155     }
156
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);
160
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);
169   s->mute = i->mute;
170   schedule_update();
171 }
172
173 static void pulse_sink_input_gone(int idx)
174 {
175   DBG("Pulse: REMOVE SINK INPUT #%d", idx);
176   struct pulse_sink_input *s = pulse_sink_input_lookup(idx);
177   clist_remove(&s->n);
178   pulse_sink_input_remove(s);
179   schedule_update();
180 }
181
182 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
183 {
184   PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, idx, cvol, pulse_success_cb);
185 }
186
187 void pulse_sink_input_set_mute(int idx, bool mute)
188 {
189   PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, idx, mute, pulse_success_cb);
190 }
191
192 void pulse_sink_input_move(int input_idx, int sink_idx)
193 {
194   PULSE_ASYNC_RUN(pa_context_move_sink_input_by_index, input_idx, sink_idx, pulse_success_cb);
195 }
196
197 /*** Sinks ***/
198
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>
207
208 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
209 {
210   struct pulse_op *op = userdata;
211
212   if (eol)
213     {
214       if (op->is_init)
215         {
216           PULSE_STATE(PS_GET_SINK_INPUTS);
217           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
218         }
219       pulse_op_done(op);
220       return;
221     }
222
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);
226
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;
234   s->mute = i->mute;
235   schedule_update();
236 }
237
238 static void pulse_sink_gone(int idx)
239 {
240   DBG("Pulse: REMOVE SINK #%d", idx);
241   struct pulse_sink *s = pulse_sink_lookup(idx);
242   clist_remove(&s->n);
243   pulse_sink_remove(s);
244   schedule_update();
245 }
246
247 struct pulse_sink *pulse_sink_by_name(const char *name)
248 {
249   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
250     if (!strcmp(s->name, name))
251       return s;
252   return NULL;
253 }
254
255 struct pulse_sink *pulse_sink_by_idx(int idx)
256 {
257   return pulse_sink_lookup(idx);
258 }
259
260 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
261 {
262   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
263 }
264
265 void pulse_sink_set_mute(int idx, bool mute)
266 {
267   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
268 }
269
270 /*** Clients ***/
271
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>
280
281 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
282 {
283   struct pulse_op *op = userdata;
284
285   if (eol)
286     {
287       if (op->is_init)
288         {
289           PULSE_STATE(PS_GET_SINKS);
290           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
291         }
292       pulse_op_done(op);
293       return;
294     }
295
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);
300
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);
306   schedule_update();
307 }
308
309 static void pulse_client_gone(int idx)
310 {
311   DBG("Pulse: REMOVE CLIENT #%d", idx);
312   struct pulse_client *c = pulse_client_lookup(idx);
313   clist_remove(&c->n);
314   pulse_client_remove(c);
315   schedule_update();
316 }
317
318 struct pulse_client *pulse_client_by_idx(int idx)
319 {
320   return pulse_client_lookup(idx);
321 }
322
323 /*** Events ***/
324
325 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
326 {
327   pulse_op_done(userdata);
328
329   if (!success)
330     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
331
332   PULSE_STATE(PS_GET_CLIENTS);
333   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
334 }
335
336 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
337 {
338   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
339
340   uint object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
341   uint action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
342   switch (object)
343     {
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);
349       break;
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);
355       break;
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);
361       break;
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);
365       break;
366     }
367 }
368
369 /*** Server state ***/
370
371 static void pulse_shutdown(void)
372 {
373   DBG("Pulse: Shutting down");
374   pulse_client_cleanup();
375   pulse_sink_cleanup();
376   pulse_sink_input_cleanup();
377 }
378
379 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
380 {
381   int state = pa_context_get_state(ctx);
382   DBG("Pulse: State callback, new state = %d", state);
383   if (state == PA_CONTEXT_READY)
384     {
385       if (pulse_state == PS_OFFLINE)
386         {
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);
390         }
391     }
392   else
393     {
394       if (pulse_state != PS_OFFLINE)
395         {
396           msg(L_INFO, "Lost connection to PulseAudio");
397           PULSE_STATE(PS_OFFLINE);
398           pulse_op_cancel_all();
399           pulse_shutdown();
400           schedule_update();
401         }
402       if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
403         timer_add_rel(&pulse_connect_timer, 2000);
404     }
405 }
406
407 static void pulse_connect(struct main_timer *t)
408 {
409   msg(L_DEBUG, "Connecting to PulseAudio");
410   timer_del(t);
411
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);
416   pulse_client_init();
417   pulse_sink_init();
418   pulse_sink_input_init();
419
420   if (pulse_ctx)
421     pa_context_unref(pulse_ctx);
422   pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
423
424   pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
425   pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
426 }
427
428 void pulse_init(void)
429 {
430   pmain_init();
431
432   pulse_connect_timer.handler = pulse_connect;
433   timer_add_rel(&pulse_connect_timer, 0);
434 }