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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulse/xmalloc.h>
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>
36 #include "module-intended-roles-symdef.h"
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Automatically set device of streams based on intended roles of devices");
40 PA_MODULE_VERSION(PACKAGE_VERSION
);
41 PA_MODULE_LOAD_ONCE(true);
43 "on_hotplug=<When new device becomes available, recheck streams?> "
44 "on_rescue=<When device becomes unavailable, recheck streams?>");
46 static const char* const valid_modargs
[] = {
57 *sink_input_new_hook_slot
,
58 *source_output_new_hook_slot
,
60 *source_put_hook_slot
,
61 *sink_unlink_hook_slot
,
62 *source_unlink_hook_slot
;
68 static bool 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
);
72 static pa_hook_result_t
sink_input_new_hook_callback(pa_core
*c
, pa_sink_input_new_data
*new_data
, struct userdata
*u
) {
81 if (!new_data
->proplist
) {
82 pa_log_debug("New stream lacks property data.");
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
)));
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
)));
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))
101 /* @todo: favour the highest priority device, not the first one we find? */
102 PA_IDXSET_FOREACH(s
, c
->sinks
, idx
) {
106 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)))
109 if (role_match(s
->proplist
, role
) && pa_sink_input_new_data_set_sink(new_data
, s
, false))
116 static pa_hook_result_t
source_output_new_hook_callback(pa_core
*c
, pa_source_output_new_data
*new_data
, struct userdata
*u
) {
125 if (!new_data
->proplist
) {
126 pa_log_debug("New stream lacks property data.");
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
)));
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
)));
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);
147 PA_IDXSET_FOREACH(s
, c
->sources
, idx
) {
154 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)))
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);
167 static pa_hook_result_t
sink_put_hook_callback(pa_core
*c
, pa_sink
*sink
, struct userdata
*u
) {
174 pa_assert(u
->on_hotplug
);
176 PA_IDXSET_FOREACH(si
, c
->sink_inputs
, idx
) {
179 if (si
->sink
== sink
)
185 /* Skip this if it is already in the process of being moved
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
)))
196 if (!(role
= pa_proplist_gets(si
->proplist
, PA_PROP_MEDIA_ROLE
)))
199 if (role_match(si
->sink
->proplist
, role
))
202 if (!role_match(sink
->proplist
, role
))
205 pa_sink_input_move_to(si
, sink
, false);
211 static pa_hook_result_t
source_put_hook_callback(pa_core
*c
, pa_source
*source
, struct userdata
*u
) {
212 pa_source_output
*so
;
218 pa_assert(u
->on_hotplug
);
220 if (source
->monitor_of
)
223 PA_IDXSET_FOREACH(so
, c
->source_outputs
, idx
) {
226 if (so
->source
== source
)
232 if (so
->direct_on_input
)
235 /* Skip this if it is already in the process of being moved
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
)))
246 if (!(role
= pa_proplist_gets(so
->proplist
, PA_PROP_MEDIA_ROLE
)))
249 if (role_match(so
->source
->proplist
, role
))
252 if (!role_match(source
->proplist
, role
))
255 pa_source_output_move_to(so
, source
, false);
261 static pa_hook_result_t
sink_unlink_hook_callback(pa_core
*c
, pa_sink
*sink
, struct userdata
*u
) {
269 pa_assert(u
->on_rescue
);
271 /* There's no point in doing anything if the core is shut down anyway */
272 if (c
->state
== PA_CORE_SHUTDOWN
)
275 /* If there not default sink, then there is no sink at all */
276 if (!(def
= pa_namereg_get_default_sink(c
)))
279 PA_IDXSET_FOREACH(si
, sink
->inputs
, idx
) {
287 if (!(role
= pa_proplist_gets(si
->proplist
, PA_PROP_MEDIA_ROLE
)))
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)
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
)
301 if (!PA_SINK_IS_LINKED(pa_sink_get_state(d
)))
304 if (role_match(d
->proplist
, role
))
305 if (pa_sink_input_move_to(si
, d
, false) >= 0)
313 static pa_hook_result_t
source_unlink_hook_callback(pa_core
*c
, pa_source
*source
, struct userdata
*u
) {
314 pa_source_output
*so
;
321 pa_assert(u
->on_rescue
);
323 /* There's no point in doing anything if the core is shut down anyway */
324 if (c
->state
== PA_CORE_SHUTDOWN
)
327 /* If there not default source, then there is no source at all */
328 if (!(def
= pa_namereg_get_default_source(c
)))
331 PA_IDXSET_FOREACH(so
, source
->outputs
, idx
) {
336 if (so
->direct_on_input
)
342 if (!(role
= pa_proplist_gets(so
->proplist
, PA_PROP_MEDIA_ROLE
)))
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);
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
)
357 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d
)))
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);
371 int pa__init(pa_module
*m
) {
372 pa_modargs
*ma
= NULL
;
374 bool on_hotplug
= true, on_rescue
= true;
378 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
379 pa_log("Failed to parse module arguments");
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");
389 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
392 u
->on_hotplug
= on_hotplug
;
393 u
->on_rescue
= on_rescue
;
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
);
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
);
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
);
423 void pa__done(pa_module
*m
) {
428 if (!(u
= m
->userdata
))
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
);
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
);
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
);