]> mj.ucw.cz Git - misc.git/blob - ursaryd/pulse.c
Ursary: Split off pulse.c
[misc.git] / ursaryd / 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 <pulse/pulseaudio.h>
19
20 #include "ursaryd.h"
21
22 enum pulse_state pulse_state;
23 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
24
25 clist pulse_client_list, pulse_sink_list, pulse_sink_input_list;
26
27 static pa_context *pulse_ctx;
28 static struct main_timer pulse_connect_timer;
29
30 #define SET_STRING(_field, _val) do { if (!_field || strcmp(_field, _val)) { xfree(_field); _field = xstrdup(_val); } } while (0)
31
32 /*** Tracking of currently running asynchronous operations ***/
33
34 struct pulse_op {
35   cnode n;
36   pa_operation *o;
37   bool is_init;
38 };
39
40 static clist pulse_op_list;
41
42 static struct pulse_op *pulse_op_new(void)
43 {
44   struct pulse_op *op = xmalloc_zero(sizeof(*op));
45   clist_add_tail(&pulse_op_list, &op->n);
46   return op;
47 }
48
49 static void pulse_op_done(struct pulse_op *op)
50 {
51   if (op->o)
52     pa_operation_unref(op->o);
53   clist_remove(&op->n);
54   xfree(op);
55 }
56
57 static void pulse_op_cancel_all(void)
58 {
59   struct pulse_op *op;
60   while (op = (struct pulse_op *) clist_head(&pulse_op_list))
61     {
62       DBG("Pulse: Cancelling pending operation");
63       pa_operation_cancel(op->o);
64       pulse_op_done(op);
65     }
66 }
67
68 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(__VA_ARGS__, _op); } while (0)
69 #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)
70
71 static void pulse_success_cb(pa_context *ctx UNUSED, int success, void *userdata)
72 {
73   if (!success)
74     msg(L_ERROR, "Pulse: Failure reported");
75   pulse_op_done(userdata);
76 }
77
78 /*** Debugging dumps ***/
79
80 void pulse_dump(void)
81 {
82   CLIST_FOR_EACH(struct pulse_client *, c, pulse_client_list)
83     DBG("## Client #%d: %s host=%s", c->idx, c->name, c->host);
84
85   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
86     DBG("## 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     DBG("## 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 /*** Sink inputs ***/
109
110 #define HASH_NODE struct pulse_sink_input
111 #define HASH_PREFIX(x) pulse_sink_input_##x
112 #define HASH_KEY_ATOMIC idx
113 #define HASH_WANT_CLEANUP
114 #define HASH_WANT_LOOKUP
115 #define HASH_WANT_REMOVE
116 #define HASH_ZERO_FILL
117 #include <ucw/hashtable.h>
118
119 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
120 {
121   struct pulse_op *op = userdata;
122
123   if (eol)
124     {
125       if (op->is_init)
126         {
127           PULSE_STATE(PS_ONLINE);
128           schedule_update();
129         }
130       pulse_op_done(op);
131       return;
132     }
133
134   DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d chans=%d has_vol=%d vol_rw=%d volume=%u mute=%d",
135     i->index, i->name, i->client, i->sink, i->channel_map.channels, i->has_volume, i->volume_writable, i->volume.values[0], i->mute);
136   pulse_dump_proplist(i->proplist);
137
138   struct pulse_sink_input *s = pulse_sink_input_lookup(i->index);
139   if (!clist_is_linked(&s->n))
140     clist_add_tail(&pulse_sink_input_list, &s->n);
141   SET_STRING(s->name, i->name);
142   s->client_idx = i->client;
143   s->sink_idx = i->sink;
144   s->channels = i->channel_map.channels;
145   s->volume = pa_cvolume_avg(&i->volume);
146   s->mute = i->mute;
147   schedule_update();
148 }
149
150 static void pulse_sink_input_gone(int idx)
151 {
152   DBG("Pulse: REMOVE SINK INPUT #%d", idx);
153   struct pulse_sink_input *s = pulse_sink_input_lookup(idx);
154   clist_remove(&s->n);
155   pulse_sink_input_remove(s);
156   schedule_update();
157 }
158
159 void pulse_sink_input_set_volume(int idx, pa_cvolume *cvol)
160 {
161   PULSE_ASYNC_RUN(pa_context_set_sink_input_volume, pulse_ctx, idx, cvol, pulse_success_cb);
162 }
163
164 void pulse_sink_input_set_mute(int idx, bool mute)
165 {
166   PULSE_ASYNC_RUN(pa_context_set_sink_input_mute, pulse_ctx, idx, mute, pulse_success_cb);
167 }
168
169 /*** Sinks ***/
170
171 #define HASH_NODE struct pulse_sink
172 #define HASH_PREFIX(x) pulse_sink_##x
173 #define HASH_KEY_ATOMIC idx
174 #define HASH_WANT_CLEANUP
175 #define HASH_WANT_LOOKUP
176 #define HASH_WANT_REMOVE
177 #define HASH_ZERO_FILL
178 #include <ucw/hashtable.h>
179
180 static void pulse_sink_cb(pa_context *ctx, const pa_sink_info *i, int eol, void *userdata)
181 {
182   struct pulse_op *op = userdata;
183
184   if (eol)
185     {
186       if (op->is_init)
187         {
188           PULSE_STATE(PS_GET_SINK_INPUTS);
189           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, ctx, pulse_sink_input_cb);
190         }
191       pulse_op_done(op);
192       return;
193     }
194
195   DBG("Pulse: SINK #%u: %s (%s) flags=%08x channels=%u volume=%u mute=%d base_vol=%u state=%u",
196     i->index, i->name, i->description, i->flags, i->channel_map.channels, i->volume.values[0], i->mute, i->base_volume, i->state);
197   pulse_dump_proplist(i->proplist);
198
199   struct pulse_sink *s = pulse_sink_lookup(i->index);
200   if (!clist_is_linked(&s->n))
201     clist_add_tail(&pulse_sink_list, &s->n);
202   SET_STRING(s->name, i->name);
203   s->channels = i->channel_map.channels;
204   s->volume = pa_cvolume_avg(&i->volume);
205   s->base_volume = i->base_volume;
206   s->mute = i->mute;
207   schedule_update();
208 }
209
210 static void pulse_sink_gone(int idx)
211 {
212   DBG("Pulse: REMOVE SINK #%d", idx);
213   struct pulse_sink *s = pulse_sink_lookup(idx);
214   clist_remove(&s->n);
215   pulse_sink_remove(s);
216   schedule_update();
217 }
218
219 struct pulse_sink *pulse_sink_by_name(const char *name)
220 {
221   CLIST_FOR_EACH(struct pulse_sink *, s, pulse_sink_list)
222     if (!strcmp(s->name, name))
223       return s;
224   return NULL;
225 }
226
227 void pulse_sink_set_volume(int idx, pa_cvolume *cvol)
228 {
229   PULSE_ASYNC_RUN(pa_context_set_sink_volume_by_index, pulse_ctx, idx, cvol, pulse_success_cb);
230 }
231
232 void pulse_sink_set_mute(int idx, bool mute)
233 {
234   PULSE_ASYNC_RUN(pa_context_set_sink_mute_by_index, pulse_ctx, idx, mute, pulse_success_cb);
235 }
236
237 /*** Clients ***/
238
239 #define HASH_NODE struct pulse_client
240 #define HASH_PREFIX(x) pulse_client_##x
241 #define HASH_KEY_ATOMIC idx
242 #define HASH_WANT_CLEANUP
243 #define HASH_WANT_LOOKUP
244 #define HASH_WANT_REMOVE
245 #define HASH_ZERO_FILL
246 #include <ucw/hashtable.h>
247
248 static void pulse_client_cb(pa_context *ctx, const pa_client_info *i, int eol, void *userdata)
249 {
250   struct pulse_op *op = userdata;
251
252   if (eol)
253     {
254       if (op->is_init)
255         {
256           PULSE_STATE(PS_GET_SINKS);
257           PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, ctx, pulse_sink_cb);
258         }
259       pulse_op_done(op);
260       return;
261     }
262
263   char *host = stk_strdup(pa_proplist_gets(i->proplist, "application.process.host") ? : "?");
264   DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s host=%s",
265     i->index, i->name, i->owner_module, i->driver, host);
266   pulse_dump_proplist(i->proplist);
267
268   struct pulse_client *c = pulse_client_lookup(i->index);
269   if (!clist_is_linked(&c->n))
270     clist_add_tail(&pulse_client_list, &c->n);
271   SET_STRING(c->name, i->name);
272   SET_STRING(c->host, host);
273   schedule_update();
274 }
275
276 static void pulse_client_gone(int idx)
277 {
278   DBG("Pulse: REMOVE CLIENT #%d", idx);
279   struct pulse_client *c = pulse_client_lookup(idx);
280   clist_remove(&c->n);
281   pulse_client_remove(c);
282   schedule_update();
283 }
284
285 struct pulse_client *pulse_client_by_idx(int idx)
286 {
287   return pulse_client_lookup(idx);
288 }
289
290 /*** Events ***/
291
292 static void pulse_subscribe_done_cb(pa_context *ctx, int success, void *userdata)
293 {
294   pulse_op_done(userdata);
295
296   if (!success)
297     msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
298
299   PULSE_STATE(PS_GET_CLIENTS);
300   PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, ctx, pulse_client_cb);
301 }
302
303 static void pulse_event_cb(pa_context *ctx, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
304 {
305   DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
306
307   uns object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
308   uns action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
309   switch (object)
310     {
311     case PA_SUBSCRIPTION_EVENT_CLIENT:
312       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
313         PULSE_ASYNC_RUN(pa_context_get_client_info, ctx, idx, pulse_client_cb);
314       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
315         pulse_client_gone(idx);
316       break;
317     case PA_SUBSCRIPTION_EVENT_SINK:
318       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
319         PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, ctx, idx, pulse_sink_cb);
320       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
321         pulse_sink_gone(idx);
322       break;
323     case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
324       if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
325         PULSE_ASYNC_RUN(pa_context_get_sink_input_info, ctx, idx, pulse_sink_input_cb);
326       else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
327         pulse_sink_input_gone(idx);
328       break;
329     }
330 }
331
332 /*** Server state ***/
333
334 static void pulse_shutdown(void)
335 {
336   DBG("Pulse: Shutting down");
337   pulse_client_cleanup();
338   pulse_sink_cleanup();
339   pulse_sink_input_cleanup();
340 }
341
342 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
343 {
344   int state = pa_context_get_state(ctx);
345   DBG("Pulse: State callback, new state = %d", state);
346   if (state == PA_CONTEXT_READY)
347     {
348       if (pulse_state == PS_OFFLINE)
349         {
350           PULSE_STATE(PS_SUBSCRIBE);
351           pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
352           PULSE_ASYNC_INIT_RUN(pa_context_subscribe, ctx, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
353         }
354     }
355   else
356     {
357       if (pulse_state != PS_OFFLINE)
358         {
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   DBG("Pulse: Connecting");
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 }