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