hashmap: Add pa_hashmap_remove_all()
[platform/upstream/pulseaudio.git] / src / modules / module-intended-roles.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2009 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2.1 of the License,
9   or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/module.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/source-output.h>
34 #include <pulsecore/namereg.h>
35
36 #include "module-intended-roles-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Automatically set device of streams based of intended roles of devices");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(TRUE);
42 PA_MODULE_USAGE(
43         "on_hotplug=<When new device becomes available, recheck streams?> "
44         "on_rescue=<When device becomes unavailable, recheck streams?>");
45
46 static const char* const valid_modargs[] = {
47     "on_hotplug",
48     "on_rescue",
49     NULL
50 };
51
52 struct userdata {
53     pa_core *core;
54     pa_module *module;
55
56     pa_hook_slot
57         *sink_input_new_hook_slot,
58         *source_output_new_hook_slot,
59         *sink_put_hook_slot,
60         *source_put_hook_slot,
61         *sink_unlink_hook_slot,
62         *source_unlink_hook_slot;
63
64     pa_bool_t on_hotplug:1;
65     pa_bool_t on_rescue:1;
66 };
67
68 static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
69     return pa_str_in_list_spaces(pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES), role);
70 }
71
72 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
73     const char *role;
74     pa_sink *s, *def;
75     uint32_t idx;
76
77     pa_assert(c);
78     pa_assert(new_data);
79     pa_assert(u);
80
81     if (!new_data->proplist) {
82         pa_log_debug("New stream lacks property data.");
83         return PA_HOOK_OK;
84     }
85
86     if (new_data->sink) {
87         pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
88         return PA_HOOK_OK;
89     }
90
91     if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
92         pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
93         return PA_HOOK_OK;
94     }
95
96     /* Prefer the default sink over any other sink, just in case... */
97     if ((def = pa_namereg_get_default_sink(c)))
98         if (role_match(def->proplist, role) && pa_sink_input_new_data_set_sink(new_data, def, FALSE))
99             return PA_HOOK_OK;
100
101     /* @todo: favour the highest priority device, not the first one we find? */
102     PA_IDXSET_FOREACH(s, c->sinks, idx) {
103         if (s == def)
104             continue;
105
106         if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)))
107             continue;
108
109         if (role_match(s->proplist, role) && pa_sink_input_new_data_set_sink(new_data, s, FALSE))
110             return PA_HOOK_OK;
111     }
112
113     return PA_HOOK_OK;
114 }
115
116 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
117     const char *role;
118     pa_source *s, *def;
119     uint32_t idx;
120
121     pa_assert(c);
122     pa_assert(new_data);
123     pa_assert(u);
124
125     if (!new_data->proplist) {
126         pa_log_debug("New stream lacks property data.");
127         return PA_HOOK_OK;
128     }
129
130     if (new_data->source) {
131         pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
132         return PA_HOOK_OK;
133     }
134
135     if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
136         pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
137         return PA_HOOK_OK;
138     }
139
140     /* Prefer the default source over any other source, just in case... */
141     if ((def = pa_namereg_get_default_source(c)))
142         if (role_match(def->proplist, role)) {
143             pa_source_output_new_data_set_source(new_data, def, FALSE);
144             return PA_HOOK_OK;
145         }
146
147     PA_IDXSET_FOREACH(s, c->sources, idx) {
148         if (s->monitor_of)
149             continue;
150
151         if (s == def)
152             continue;
153
154         if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
155             continue;
156
157         /* @todo: favour the highest priority device, not the first one we find? */
158         if (role_match(s->proplist, role)) {
159             pa_source_output_new_data_set_source(new_data, s, FALSE);
160             return PA_HOOK_OK;
161         }
162     }
163
164     return PA_HOOK_OK;
165 }
166
167 static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
168     pa_sink_input *si;
169     uint32_t idx;
170
171     pa_assert(c);
172     pa_assert(sink);
173     pa_assert(u);
174     pa_assert(u->on_hotplug);
175
176     PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
177         const char *role;
178
179         if (si->sink == sink)
180             continue;
181
182         if (si->save_sink)
183             continue;
184
185         /* Skip this if it is already in the process of being moved
186          * anyway */
187         if (!si->sink)
188             continue;
189
190         /* It might happen that a stream and a sink are set up at the
191            same time, in which case we want to make sure we don't
192            interfere with that */
193         if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
194             continue;
195
196         if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
197             continue;
198
199         if (role_match(si->sink->proplist, role))
200             continue;
201
202         if (!role_match(sink->proplist, role))
203             continue;
204
205         pa_sink_input_move_to(si, sink, FALSE);
206     }
207
208     return PA_HOOK_OK;
209 }
210
211 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
212     pa_source_output *so;
213     uint32_t idx;
214
215     pa_assert(c);
216     pa_assert(source);
217     pa_assert(u);
218     pa_assert(u->on_hotplug);
219
220     if (source->monitor_of)
221         return PA_HOOK_OK;
222
223     PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
224         const char *role;
225
226         if (so->source == source)
227             continue;
228
229         if (so->save_source)
230             continue;
231
232         if (so->direct_on_input)
233             continue;
234
235         /* Skip this if it is already in the process of being moved
236          * anyway */
237         if (!so->source)
238             continue;
239
240         /* It might happen that a stream and a source are set up at the
241            same time, in which case we want to make sure we don't
242            interfere with that */
243         if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
244             continue;
245
246         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
247             continue;
248
249         if (role_match(so->source->proplist, role))
250             continue;
251
252         if (!role_match(source->proplist, role))
253             continue;
254
255         pa_source_output_move_to(so, source, FALSE);
256     }
257
258     return PA_HOOK_OK;
259 }
260
261 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
262     pa_sink_input *si;
263     uint32_t idx;
264     pa_sink *def;
265
266     pa_assert(c);
267     pa_assert(sink);
268     pa_assert(u);
269     pa_assert(u->on_rescue);
270
271     /* There's no point in doing anything if the core is shut down anyway */
272     if (c->state == PA_CORE_SHUTDOWN)
273         return PA_HOOK_OK;
274
275     /* If there not default sink, then there is no sink at all */
276     if (!(def = pa_namereg_get_default_sink(c)))
277         return PA_HOOK_OK;
278
279     PA_IDXSET_FOREACH(si, sink->inputs, idx) {
280         const char *role;
281         uint32_t jdx;
282         pa_sink *d;
283
284         if (!si->sink)
285             continue;
286
287         if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
288             continue;
289
290         /* Would the default sink fit? If so, let's use it */
291         if (def != sink && role_match(def->proplist, role))
292             if (pa_sink_input_move_to(si, def, FALSE) >= 0)
293                 continue;
294
295         /* Try to find some other fitting sink */
296         /* @todo: favour the highest priority device, not the first one we find? */
297         PA_IDXSET_FOREACH(d, c->sinks, jdx) {
298             if (d == def || d == sink)
299                 continue;
300
301             if (!PA_SINK_IS_LINKED(pa_sink_get_state(d)))
302                 continue;
303
304             if (role_match(d->proplist, role))
305                 if (pa_sink_input_move_to(si, d, FALSE) >= 0)
306                     break;
307         }
308     }
309
310     return PA_HOOK_OK;
311 }
312
313 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
314     pa_source_output *so;
315     uint32_t idx;
316     pa_source *def;
317
318     pa_assert(c);
319     pa_assert(source);
320     pa_assert(u);
321     pa_assert(u->on_rescue);
322
323     /* There's no point in doing anything if the core is shut down anyway */
324     if (c->state == PA_CORE_SHUTDOWN)
325         return PA_HOOK_OK;
326
327     /* If there not default source, then there is no source at all */
328     if (!(def = pa_namereg_get_default_source(c)))
329         return PA_HOOK_OK;
330
331     PA_IDXSET_FOREACH(so, source->outputs, idx) {
332         const char *role;
333         uint32_t jdx;
334         pa_source *d;
335
336         if (so->direct_on_input)
337             continue;
338
339         if (!so->source)
340             continue;
341
342         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
343             continue;
344
345         /* Would the default source fit? If so, let's use it */
346         if (def != source && role_match(def->proplist, role) && !source->monitor_of == !def->monitor_of) {
347             pa_source_output_move_to(so, def, FALSE);
348             continue;
349         }
350
351         /* Try to find some other fitting source */
352         /* @todo: favour the highest priority device, not the first one we find? */
353         PA_IDXSET_FOREACH(d, c->sources, jdx) {
354             if (d == def || d == source)
355                 continue;
356
357             if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
358                 continue;
359
360             /* If moving from a monitor, move to another monitor */
361             if (!source->monitor_of == !d->monitor_of && role_match(d->proplist, role)) {
362                 pa_source_output_move_to(so, d, FALSE);
363                 break;
364             }
365         }
366     }
367
368     return PA_HOOK_OK;
369 }
370
371 int pa__init(pa_module*m) {
372     pa_modargs *ma = NULL;
373     struct userdata *u;
374     pa_bool_t on_hotplug = TRUE, on_rescue = TRUE;
375
376     pa_assert(m);
377
378     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
379         pa_log("Failed to parse module arguments");
380         goto fail;
381     }
382
383     if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
384         pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
385         pa_log("on_hotplug= and on_rescue= expect boolean arguments");
386         goto fail;
387     }
388
389     m->userdata = u = pa_xnew0(struct userdata, 1);
390     u->core = m->core;
391     u->module = m;
392     u->on_hotplug = on_hotplug;
393     u->on_rescue = on_rescue;
394
395     /* A little bit later than module-stream-restore */
396     u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) sink_input_new_hook_callback, u);
397     u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) source_output_new_hook_callback, u);
398
399     if (on_hotplug) {
400         /* A little bit later than module-stream-restore */
401         u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_put_hook_callback, u);
402         u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) source_put_hook_callback, u);
403     }
404
405     if (on_rescue) {
406         /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */
407         u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_unlink_hook_callback, u);
408         u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) source_unlink_hook_callback, u);
409     }
410
411     pa_modargs_free(ma);
412     return 0;
413
414 fail:
415     pa__done(m);
416
417     if (ma)
418         pa_modargs_free(ma);
419
420     return -1;
421 }
422
423 void pa__done(pa_module*m) {
424     struct userdata* u;
425
426     pa_assert(m);
427
428     if (!(u = m->userdata))
429         return;
430
431     if (u->sink_input_new_hook_slot)
432         pa_hook_slot_free(u->sink_input_new_hook_slot);
433     if (u->source_output_new_hook_slot)
434         pa_hook_slot_free(u->source_output_new_hook_slot);
435
436     if (u->sink_put_hook_slot)
437         pa_hook_slot_free(u->sink_put_hook_slot);
438     if (u->source_put_hook_slot)
439         pa_hook_slot_free(u->source_put_hook_slot);
440
441     if (u->sink_unlink_hook_slot)
442         pa_hook_slot_free(u->sink_unlink_hook_slot);
443     if (u->source_unlink_hook_slot)
444         pa_hook_slot_free(u->source_unlink_hook_slot);
445
446     pa_xfree(u);
447 }