]> mj.ucw.cz Git - ursary.git/blob - pulse.c
08f51be9b36c9138462cb6b46763afa22cd31968
[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 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(__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(__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   CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
81     DBG("## Client #%d: %s host=%s", c->idx, c->name, c->host);
82
83   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
84     DBG("## 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     DBG("## 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 /*** Sink inputs ***/
107
108 #define HASH_NODE struct pulse_sink_input
109 #define HASH_PREFIX(x) pulse_sink_input_##x
110 #define HASH_KEY_ATOMIC idx
111 #define HASH_WANT_CLEANUP
112 #define HASH_WANT_LOOKUP
113 #define HASH_WANT_REMOVE
114 #define HASH_ZERO_FILL
115 #include <ucw/hashtable.h>
116
117 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
118 {
119   struct pulse_op *op = userdata;
120
121   if (eol)
122     {
123       if (op->is_init)
124         {
125           PULSE_STATE(PS_ONLINE);
126           msg(L_INFO, "PulseAudio is ready");
127           schedule_update();
128         }
129       pulse_op_done(op);
130       return;
131     }
132
133   DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
134     i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
135   pulse_dump_proplist(i->proplist);
136
137   struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
138   if (!clist_is_linked(&s->n))
139     clist_add_tail(&pulse_sink_input_list, &s->n);
140   SET_STRING(s->name, i->name);
141   s->client_idx = i->client;
142   s->sink_idx = i->sink;
143   s->channels = i->channel_map.channels;
144   s->volume = pa_cvolume_avg(&i->volume);
145   s->mute = i->mute;
146   schedule_update();
147 }
148
149 static void pulse_sink_input_gone(int idx)
150 {
151   DBG("Pulse: REMOVE SINK INPUT #%d", idx);
152   struct pulse_sink_input *s = pulse_sink_input_lookup(idx);
153   clist_remove(&s->n);
154   pulse_sink_input_remove(s);
155   schedule_update();
156 }
157
158 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
159 {
160   PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, pulse_ctx, idx, cvol, pulse_success_cb);
161 }
162
163 void pulse_sink_input_set_mute(int idx, bool mute)
164 {
165   PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, pulse_ctx, idx, mute, pulse_success_cb);
166 }
167
168 /*** Sinks ***/
169
170 #define HASH_NODE struct pulse_sink
171 #define HASH_PREFIX(x) pulse_sink_##x
172 #define HASH_KEY_ATOMIC idx
173 #define HASH_WANT_CLEANUP
174 #define HASH_WANT_LOOKUP
175 #define HASH_WANT_REMOVE
176 #define HASH_ZERO_FILL
177 #include <ucw/hashtable.h>
178
179 static void pulse_sink_cb(pa_context *ctx, const pa_sink_info *i, int eol, void *userdata)
180 {
181   struct pulse_op *op = userdata;
182
183   if (eol)
184     {
185       if (op->is_init)
186         {
187           PULSE_STATE(PS_GET_SINK_INPUTS);
188           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, ctx, pulse_sink_input_cb);
189         }
190       pulse_op_done(op);
191       return;
192     }
193
194   DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u",
195     i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state);
196   pulse_dump_proplist(i->proplist);
197
198   struct pulse_sink *s = pulse_sink_lookup(i->index);
199   if (!clist_is_linked(&s->n))
200     clist_add_tail(&pulse_sink_list, &s->n);
201   SET_STRING(s->name, i->name);
202   s->channels = i->channel_map.channels;
203   s->volume = pa_cvolume_avg(&i->volume);
204   s->base_volume = i->base_volume;
205   s->mute = i->mute;
206   schedule_update();
207 }
208
209 static void pulse_sink_gone(int idx)
210 {
211   DBG("Pulse: REMOVE SINK #%d", idx);
212   struct pulse_sink *s = pulse_sink_lookup(idx);
213   clist_remove(&s->n);
214   pulse_sink_remove(s);
215   schedule_update();
216 }
217
218 struct pulse_sink *pulse_sink_by_name(const char *name)
219 {
220   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
221     if (!strcmp(s->name, name))
222       return s;
223   return NULL;
224 }
225
226 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
227 {
228   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, pulse_ctx, idx, cvol, pulse_success_cb);
229 }
230
231 void pulse_sink_set_mute(int idx, bool mute)
232 {
233   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, pulse_ctx, idx, mute, pulse_success_cb);
234 }
235
236 /*** Clients ***/
237
238 #define HASH_NODE struct pulse_client
239 #define HASH_PREFIX(x) pulse_client_##x
240 #define HASH_KEY_ATOMIC idx
241 #define HASH_WANT_CLEANUP
242 #define HASH_WANT_LOOKUP
243 #define HASH_WANT_REMOVE
244 #define HASH_ZERO_FILL
245 #include <ucw/hashtable.h>
246
247 static void pulse_client_cb(pa_context *ctx, const pa_client_info *i, int eol, void *userdata)
248 {
249   struct pulse_op *op = userdata;
250
251   if (eol)
252     {
253       if (op->is_init)
254         {
255           PULSE_STATE(PS_GET_SINKS);
256           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, ctx, pulse_sink_cb);
257         }
258       pulse_op_done(op);
259       return;
260     }
261
262   char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
263   DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
264     i->index, i->name, i->owner_module, i->driver, host);
265   pulse_dump_proplist(i->proplist);
266
267   struct pulse_client *c = pulse_client_lookup(i->index);
268   if (!clist_is_linked(&c->n))
269     clist_add_tail(&pulse_client_list, &c->n);
270   SET_STRING(c->name, i->name);
271   SET_STRING(c->host, host);
272   schedule_update();
273 }
274
275 static void pulse_client_gone(int idx)
276 {
277   DBG("Pulse: REMOVE CLIENT #%d", idx);
278   struct pulse_client *c = pulse_client_lookup(idx);
279   clist_remove(&c->n);
280   pulse_client_remove(c);
281   schedule_update();
282 }
283
284 struct pulse_client *pulse_client_by_idx(int idx)
285 {
286   return pulse_client_lookup(idx);
287 }
288
289 /*** Events ***/
290
291 static void pulse_subscribe_done_cb(pa_context *ctx, int success, void *userdata)
292 {
293   pulse_op_done(userdata);
294
295   if (!success)
296     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
297
298   PULSE_STATE(PS_GET_CLIENTS);
299   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, ctx, pulse_client_cb);
300 }
301
302 static void pulse_event_cb(pa_context *ctx, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
303 {
304   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
305
306   uns object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
307   uns action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
308   switch (object)
309     {
310     case PA_SUBSCRIPTION_EVENT_CLIENT:
311       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
312         PULSE_ASYNC_RUN(pa_context_get_client_info, ctx, idx, pulse_client_cb);
313       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
314         pulse_client_gone(idx);
315       break;
316     case PA_SUBSCRIPTION_EVENT_SINK:
317       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
318         PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, ctx, idx, pulse_sink_cb);
319       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
320         pulse_sink_gone(idx);
321       break;
322     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
323       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
324         PULSE_ASYNC_RUN(pa_context_get_sink_input_info, ctx, idx, pulse_sink_input_cb);
325       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
326         pulse_sink_input_gone(idx);
327       break;
328     }
329 }
330
331 /*** Server state ***/
332
333 static void pulse_shutdown(void)
334 {
335   DBG("Pulse: Shutting down");
336   pulse_client_cleanup();
337   pulse_sink_cleanup();
338   pulse_sink_input_cleanup();
339 }
340
341 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
342 {
343   int state = pa_context_get_state(ctx);
344   DBG("Pulse: State callback, new state = %d", state);
345   if (state == PA_CONTEXT_READY)
346     {
347       if (pulse_state == PS_OFFLINE)
348         {
349           PULSE_STATE(PS_SUBSCRIBE);
350           pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
351           PULSE_ASYNC_INIT_RUN(pa_context_subscribe, ctx, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
352         }
353     }
354   else
355     {
356       if (pulse_state != PS_OFFLINE)
357         {
358           msg(L_INFO, "Lost connection to PulseAudio");
359           PULSE_STATE(PS_OFFLINE);
360           pulse_op_cancel_all();
361           pulse_shutdown();
362           schedule_update();
363         }
364       if (state == PA_CONTEXT_FAILED && !timer_is_active(&pulse_connect_timer))
365         timer_add_rel(&pulse_connect_timer, 2000);
366     }
367 }
368
369 static void pulse_connect(struct main_timer *t)
370 {
371   msg(L_DEBUG, "Connecting to PulseAudio");
372   timer_del(t);
373
374   clist_init(&pulse_op_list);
375   clist_init(&pulse_client_list);
376   clist_init(&pulse_sink_list);
377   clist_init(&pulse_sink_input_list);
378   pulse_client_init();
379   pulse_sink_init();
380   pulse_sink_input_init();
381
382   if (pulse_ctx)
383     pa_context_unref(pulse_ctx);
384   pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
385
386   pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
387   pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
388 }
389
390 void pulse_init(void)
391 {
392   pmain_init();
393
394   pulse_connect_timer.handler = pulse_connect;
395   timer_add_rel(&pulse_connect_timer, 0);
396 }