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