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