]> mj.ucw.cz Git - ursary.git/blob - pulse.c
Default sink
[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 /*** Sinks ***/
195
196 #define HASH_NODE struct pulse_sink
197 #define HASH_PREFIX(x) pulse_sink_##x
198 #define HASH_KEY_ATOMIC idx
199 #define HASH_WANT_CLEANUP
200 #define HASH_WANT_LOOKUP
201 #define HASH_WANT_REMOVE
202 #define HASH_ZERO_FILL
203 #include <ucw/hashtable.h>
204
205 static void pulse_sink_cb(pa_context *ctx UNUSED, const pa_sink_info *i, int eol, void *userdata)
206 {
207   struct pulse_op *op = userdata;
208
209   if (eol)
210     {
211       if (op->is_init)
212         {
213           PULSE_STATE(PS_GET_SINK_INPUTS);
214           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, pulse_sink_input_cb);
215         }
216       pulse_op_done(op);
217       return;
218     }
219
220   DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u",
221     i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state);
222   pulse_dump_proplist(i->proplist);
223
224   struct pulse_sink *s = pulse_sink_lookup(i->index);
225   if (!clist_is_linked(&s->n))
226     clist_add_tail(&pulse_sink_list, &s->n);
227   SET_STRING(s->name, i->name);
228   s->channels = i->channel_map.channels;
229   s->volume = pa_cvolume_avg(&i->volume);
230   s->base_volume = i->base_volume;
231   s->mute = i->mute;
232   schedule_update();
233 }
234
235 static void pulse_sink_gone(int idx)
236 {
237   DBG("Pulse: REMOVE SINK #%d", idx);
238   struct pulse_sink *s = pulse_sink_lookup(idx);
239   clist_remove(&s->n);
240   pulse_sink_remove(s);
241   schedule_update();
242 }
243
244 struct pulse_sink *pulse_sink_by_name(const char *name)
245 {
246   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
247     if (!strcmp(s->name, name))
248       return s;
249   return NULL;
250 }
251
252 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
253 {
254   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, idx, cvol, pulse_success_cb);
255 }
256
257 void pulse_sink_set_mute(int idx, bool mute)
258 {
259   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, idx, mute, pulse_success_cb);
260 }
261
262 /*** Clients ***/
263
264 #define HASH_NODE struct pulse_client
265 #define HASH_PREFIX(x) pulse_client_##x
266 #define HASH_KEY_ATOMIC idx
267 #define HASH_WANT_CLEANUP
268 #define HASH_WANT_LOOKUP
269 #define HASH_WANT_REMOVE
270 #define HASH_ZERO_FILL
271 #include <ucw/hashtable.h>
272
273 static void pulse_client_cb(pa_context *ctx UNUSED, const pa_client_info *i, int eol, void *userdata)
274 {
275   struct pulse_op *op = userdata;
276
277   if (eol)
278     {
279       if (op->is_init)
280         {
281           PULSE_STATE(PS_GET_SINKS);
282           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, pulse_sink_cb);
283         }
284       pulse_op_done(op);
285       return;
286     }
287
288   char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
289   DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
290     i->index, i->name, i->owner_module, i->driver, host);
291   pulse_dump_proplist(i->proplist);
292
293   struct pulse_client *c = pulse_client_lookup(i->index);
294   if (!clist_is_linked(&c->n))
295     clist_add_tail(&pulse_client_list, &c->n);
296   SET_STRING(c->name, i->name);
297   SET_STRING(c->host, host);
298   schedule_update();
299 }
300
301 static void pulse_client_gone(int idx)
302 {
303   DBG("Pulse: REMOVE CLIENT #%d", idx);
304   struct pulse_client *c = pulse_client_lookup(idx);
305   clist_remove(&c->n);
306   pulse_client_remove(c);
307   schedule_update();
308 }
309
310 struct pulse_client *pulse_client_by_idx(int idx)
311 {
312   return pulse_client_lookup(idx);
313 }
314
315 /*** Events ***/
316
317 static void pulse_subscribe_done_cb(pa_context *ctx UNUSED, int success, void *userdata)
318 {
319   pulse_op_done(userdata);
320
321   if (!success)
322     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
323
324   PULSE_STATE(PS_GET_CLIENTS);
325   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, pulse_client_cb);
326 }
327
328 static void pulse_event_cb(pa_context *ctx UNUSED, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
329 {
330   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
331
332   uns object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
333   uns action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
334   switch (object)
335     {
336     case PA_SUBSCRIPTION_EVENT_CLIENT:
337       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
338         PULSE_ASYNC_RUN(pa_context_get_client_info, idx, pulse_client_cb);
339       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
340         pulse_client_gone(idx);
341       break;
342     case PA_SUBSCRIPTION_EVENT_SINK:
343       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
344         PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, idx, pulse_sink_cb);
345       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
346         pulse_sink_gone(idx);
347       break;
348     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
349       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
350         PULSE_ASYNC_RUN(pa_context_get_sink_input_info, idx, pulse_sink_input_cb);
351       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
352         pulse_sink_input_gone(idx);
353       break;
354     case PA_SUBSCRIPTION_EVENT_SERVER:
355       if (action == PA_SUBSCRIPTION_EVENT_CHANGE)
356         PULSE_ASYNC_RUN(pa_context_get_server_info, pulse_server_cb);
357       break;
358     }
359 }
360
361 /*** Server state ***/
362
363 static void pulse_shutdown(void)
364 {
365   DBG("Pulse: Shutting down");
366   pulse_client_cleanup();
367   pulse_sink_cleanup();
368   pulse_sink_input_cleanup();
369 }
370
371 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
372 {
373   int state = pa_context_get_state(ctx);
374   DBG("Pulse: State callback, new state = %d", state);
375   if (state == PA_CONTEXT_READY)
376     {
377       if (pulse_state == PS_OFFLINE)
378         {
379           PULSE_STATE(PS_SUBSCRIBE);
380           pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
381           PULSE_ASYNC_INIT_RUN(pa_context_subscribe, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
382         }
383     }
384   else
385     {
386       if (pulse_state != PS_OFFLINE)
387         {
388           msg(L_INFO, "Lost connection to PulseAudio");
389           PULSE_STATE(PS_OFFLINE);
390           pulse_op_cancel_all();
391           pulse_shutdown();
392           schedule_update();
393         }
394       if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
395         timer_add_rel(&pulse_connect_timer, 2000);
396     }
397 }
398
399 static void pulse_connect(struct main_timer *t)
400 {
401   msg(L_DEBUG, "Connecting to PulseAudio");
402   timer_del(t);
403
404   clist_init(&pulse_op_list);
405   clist_init(&pulse_client_list);
406   clist_init(&pulse_sink_list);
407   clist_init(&pulse_sink_input_list);
408   pulse_client_init();
409   pulse_sink_init();
410   pulse_sink_input_init();
411
412   if (pulse_ctx)
413     pa_context_unref(pulse_ctx);
414   pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
415
416   pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
417   pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
418 }
419
420 void pulse_init(void)
421 {
422   pmain_init();
423
424   pulse_connect_timer.handler = pulse_connect;
425   timer_add_rel(&pulse_connect_timer, 0);
426 }