2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
24 #include <pulse/xmalloc.h>
26 #include <pulsecore/module.h>
27 #include <pulsecore/core-util.h>
28 #include <pulsecore/modargs.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/source-output.h>
32 #include <pulsecore/namereg.h>
34 PA_MODULE_AUTHOR("Lennart Poettering");
35 PA_MODULE_DESCRIPTION("Automatically set device of streams based on intended roles of devices");
36 PA_MODULE_VERSION(PACKAGE_VERSION);
37 PA_MODULE_LOAD_ONCE(true);
39 "on_hotplug=<When new device becomes available, recheck streams?> "
40 "on_rescue=<When device becomes unavailable, recheck streams?>");
42 static const char* const valid_modargs[] = {
53 *sink_input_new_hook_slot,
54 *source_output_new_hook_slot,
56 *source_put_hook_slot,
57 *sink_unlink_hook_slot,
58 *source_unlink_hook_slot;
64 static bool role_match(pa_proplist *proplist, const char *role) {
65 return pa_str_in_list_spaces(pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES), role);
68 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
77 if (!new_data->proplist) {
78 pa_log_debug("New stream lacks property data.");
83 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
87 if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
88 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)));
92 /* Prefer the default sink over any other sink, just in case... */
94 if (role_match(c->default_sink->proplist, role) && pa_sink_input_new_data_set_sink(new_data, c->default_sink, false, false))
97 /* @todo: favour the highest priority device, not the first one we find? */
98 PA_IDXSET_FOREACH(s, c->sinks, idx) {
99 if (s == c->default_sink)
102 if (!PA_SINK_IS_LINKED(s->state))
105 if (role_match(s->proplist, role) && pa_sink_input_new_data_set_sink(new_data, s, false, false))
112 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
121 if (!new_data->proplist) {
122 pa_log_debug("New stream lacks property data.");
126 if (new_data->source) {
127 pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME)));
131 if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) {
132 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)));
136 /* Prefer the default source over any other source, just in case... */
137 if (c->default_source)
138 if (role_match(c->default_source->proplist, role)) {
139 pa_source_output_new_data_set_source(new_data, c->default_source, false, false);
143 PA_IDXSET_FOREACH(s, c->sources, idx) {
147 if (s == c->default_source)
150 if (!PA_SOURCE_IS_LINKED(s->state))
153 /* @todo: favour the highest priority device, not the first one we find? */
154 if (role_match(s->proplist, role)) {
155 pa_source_output_new_data_set_source(new_data, s, false, false);
163 static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
170 pa_assert(u->on_hotplug);
172 PA_IDXSET_FOREACH(si, c->sink_inputs, idx) {
175 if (si->sink == sink)
178 /* Skip this if it is already in the process of being moved
183 if (pa_safe_streq(si->sink->name, si->preferred_sink))
186 /* It might happen that a stream and a sink are set up at the
187 same time, in which case we want to make sure we don't
188 interfere with that */
189 if (!PA_SINK_INPUT_IS_LINKED(si->state))
192 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
195 if (role_match(si->sink->proplist, role))
198 if (!role_match(sink->proplist, role))
201 pa_sink_input_move_to(si, sink, false);
207 static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
208 pa_source_output *so;
214 pa_assert(u->on_hotplug);
216 if (source->monitor_of)
219 PA_IDXSET_FOREACH(so, c->source_outputs, idx) {
222 if (so->source == source)
225 if (so->direct_on_input)
228 /* Skip this if it is already in the process of being moved
233 if (pa_safe_streq(so->source->name, so->preferred_source))
236 /* It might happen that a stream and a source are set up at the
237 same time, in which case we want to make sure we don't
238 interfere with that */
239 if (!PA_SOURCE_OUTPUT_IS_LINKED(so->state))
242 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
245 if (role_match(so->source->proplist, role))
248 if (!role_match(source->proplist, role))
251 pa_source_output_move_to(so, source, false);
257 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) {
264 pa_assert(u->on_rescue);
266 /* There's no point in doing anything if the core is shut down anyway */
267 if (c->state == PA_CORE_SHUTDOWN)
270 /* If there not default sink, then there is no sink at all */
271 if (!c->default_sink)
274 PA_IDXSET_FOREACH(si, sink->inputs, idx) {
282 if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
285 /* Would the default sink fit? If so, let's use it */
286 if (c->default_sink != sink && role_match(c->default_sink->proplist, role))
287 if (pa_sink_input_move_to(si, c->default_sink, false) >= 0)
290 /* Try to find some other fitting sink */
291 /* @todo: favour the highest priority device, not the first one we find? */
292 PA_IDXSET_FOREACH(d, c->sinks, jdx) {
293 if (d == c->default_sink || d == sink)
296 if (!PA_SINK_IS_LINKED(d->state))
299 if (role_match(d->proplist, role))
300 if (pa_sink_input_move_to(si, d, false) >= 0)
308 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) {
309 pa_source_output *so;
315 pa_assert(u->on_rescue);
317 /* There's no point in doing anything if the core is shut down anyway */
318 if (c->state == PA_CORE_SHUTDOWN)
321 /* If there not default source, then there is no source at all */
322 if (!c->default_source)
325 PA_IDXSET_FOREACH(so, source->outputs, idx) {
330 if (so->direct_on_input)
336 if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
339 /* Would the default source fit? If so, let's use it */
340 if (c->default_source != source && role_match(c->default_source->proplist, role)
341 && !source->monitor_of == !c->default_source->monitor_of) {
342 pa_source_output_move_to(so, c->default_source, false);
346 /* Try to find some other fitting source */
347 /* @todo: favour the highest priority device, not the first one we find? */
348 PA_IDXSET_FOREACH(d, c->sources, jdx) {
349 if (d == c->default_source || d == source)
352 if (!PA_SOURCE_IS_LINKED(d->state))
355 /* If moving from a monitor, move to another monitor */
356 if (!source->monitor_of == !d->monitor_of && role_match(d->proplist, role)) {
357 pa_source_output_move_to(so, d, false);
366 int pa__init(pa_module*m) {
367 pa_modargs *ma = NULL;
369 bool on_hotplug = true, on_rescue = true;
373 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
374 pa_log("Failed to parse module arguments");
378 if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 ||
379 pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) {
380 pa_log("on_hotplug= and on_rescue= expect boolean arguments");
384 m->userdata = u = pa_xnew0(struct userdata, 1);
387 u->on_hotplug = on_hotplug;
388 u->on_rescue = on_rescue;
390 /* A little bit later than module-stream-restore */
391 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);
392 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);
395 /* A little bit later than module-stream-restore */
396 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);
397 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);
401 /* A little bit later than module-stream-restore, ... */
402 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);
403 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);
418 void pa__done(pa_module*m) {
423 if (!(u = m->userdata))
426 if (u->sink_input_new_hook_slot)
427 pa_hook_slot_free(u->sink_input_new_hook_slot);
428 if (u->source_output_new_hook_slot)
429 pa_hook_slot_free(u->source_output_new_hook_slot);
431 if (u->sink_put_hook_slot)
432 pa_hook_slot_free(u->sink_put_hook_slot);
433 if (u->source_put_hook_slot)
434 pa_hook_slot_free(u->source_put_hook_slot);
436 if (u->sink_unlink_hook_slot)
437 pa_hook_slot_free(u->sink_unlink_hook_slot);
438 if (u->source_unlink_hook_slot)
439 pa_hook_slot_free(u->source_unlink_hook_slot);