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/macro.h>
29 #include <pulsecore/hashmap.h>
30 #include <pulsecore/hook-list.h>
31 #include <pulsecore/core.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/sink-input.h>
34 #include <pulsecore/modargs.h>
36 #include "module-role-cork-symdef.h"
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Mute & cork streams with certain roles while others exist");
40 PA_MODULE_VERSION(PACKAGE_VERSION
);
41 PA_MODULE_LOAD_ONCE(true);
43 "trigger_roles=<Comma separated list of roles which will trigger a cork> "
44 "cork_roles=<Comma separated list of roles which will be corked> "
45 "global=<Should we operate globally or only inside the same device?>");
47 static const char* const valid_modargs
[] = {
56 pa_hashmap
*cork_state
;
57 pa_idxset
*trigger_roles
;
58 pa_idxset
*cork_roles
;
62 *sink_input_unlink_slot
,
63 *sink_input_move_start_slot
,
64 *sink_input_move_finish_slot
;
67 static bool shall_cork(struct userdata
*u
, pa_sink
*s
, pa_sink_input
*ignore
) {
69 uint32_t idx
, role_idx
;
70 const char *trigger_role
;
73 pa_sink_assert_ref(s
);
75 for (j
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); j
; j
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
81 if (!(role
= pa_proplist_gets(j
->proplist
, PA_PROP_MEDIA_ROLE
)))
84 PA_IDXSET_FOREACH(trigger_role
, u
->trigger_roles
, role_idx
) {
85 if (pa_streq(role
, trigger_role
)) {
86 pa_log_debug("Found a '%s' stream that will trigger the auto-cork.", trigger_role
);
95 static inline void apply_cork_to_sink(struct userdata
*u
, pa_sink
*s
, pa_sink_input
*ignore
, bool cork
) {
97 uint32_t idx
, role_idx
;
98 const char *cork_role
;
102 pa_sink_assert_ref(s
);
104 for (j
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); j
; j
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
105 bool corked
, corked_here
;
111 if (!(role
= pa_proplist_gets(j
->proplist
, PA_PROP_MEDIA_ROLE
)))
114 PA_IDXSET_FOREACH(cork_role
, u
->cork_roles
, role_idx
) {
115 if ((trigger
= pa_streq(role
, cork_role
)))
121 corked
= (pa_sink_input_get_state(j
) == PA_SINK_INPUT_CORKED
);
122 corked_here
= !!pa_hashmap_get(u
->cork_state
, j
);
124 if (cork
&& !corked
&& !j
->muted
) {
125 pa_log_debug("Found a '%s' stream that should be corked/muted.", cork_role
);
127 pa_hashmap_put(u
->cork_state
, j
, PA_INT_TO_PTR(1));
128 pa_sink_input_set_mute(j
, true, false);
129 pa_sink_input_send_event(j
, PA_STREAM_EVENT_REQUEST_CORK
, NULL
);
131 pa_hashmap_remove(u
->cork_state
, j
);
133 if (corked_here
&& (corked
|| j
->muted
)) {
134 pa_log_debug("Found a '%s' stream that should be uncorked/unmuted.", cork_role
);
136 pa_sink_input_set_mute(j
, false, false);
138 pa_sink_input_send_event(j
, PA_STREAM_EVENT_REQUEST_UNCORK
, NULL
);
144 static void apply_cork(struct userdata
*u
, pa_sink
*s
, pa_sink_input
*ignore
, bool cork
) {
149 PA_IDXSET_FOREACH(s
, u
->core
->sinks
, idx
)
150 apply_cork_to_sink(u
, s
, ignore
, cork
);
152 apply_cork_to_sink(u
, s
, ignore
, cork
);
155 static pa_hook_result_t
process(struct userdata
*u
, pa_sink_input
*i
, bool create
) {
160 pa_sink_input_assert_ref(i
);
163 pa_hashmap_remove(u
->cork_state
, i
);
165 if (!(role
= pa_proplist_gets(i
->proplist
, PA_PROP_MEDIA_ROLE
)))
171 cork
= shall_cork(u
, i
->sink
, create
? NULL
: i
);
172 apply_cork(u
, i
->sink
, create
? NULL
: i
, cork
);
177 static pa_hook_result_t
sink_input_put_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
178 pa_core_assert_ref(core
);
179 pa_sink_input_assert_ref(i
);
181 return process(u
, i
, true);
184 static pa_hook_result_t
sink_input_unlink_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
185 pa_sink_input_assert_ref(i
);
187 return process(u
, i
, false);
190 static pa_hook_result_t
sink_input_move_start_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
191 pa_core_assert_ref(core
);
192 pa_sink_input_assert_ref(i
);
194 return process(u
, i
, false);
197 static pa_hook_result_t
sink_input_move_finish_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
198 pa_core_assert_ref(core
);
199 pa_sink_input_assert_ref(i
);
201 return process(u
, i
, true);
204 int pa__init(pa_module
*m
) {
205 pa_modargs
*ma
= NULL
;
212 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
213 pa_log("Failed to parse module arguments");
217 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
220 u
->cork_state
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
222 u
->trigger_roles
= pa_idxset_new(NULL
, NULL
);
223 roles
= pa_modargs_get_value(ma
, "trigger_roles", NULL
);
225 const char *split_state
= NULL
;
227 while ((n
= pa_split(roles
, ",", &split_state
))) {
229 pa_idxset_put(u
->trigger_roles
, n
, NULL
);
234 if (pa_idxset_isempty(u
->trigger_roles
)) {
235 pa_log_debug("Using role 'phone' as trigger role.");
236 pa_idxset_put(u
->trigger_roles
, pa_xstrdup("phone"), NULL
);
239 u
->cork_roles
= pa_idxset_new(NULL
, NULL
);
240 roles
= pa_modargs_get_value(ma
, "cork_roles", NULL
);
242 const char *split_state
= NULL
;
244 while ((n
= pa_split(roles
, ",", &split_state
))) {
246 pa_idxset_put(u
->cork_roles
, n
, NULL
);
251 if (pa_idxset_isempty(u
->cork_roles
)) {
252 pa_log_debug("Using roles 'music' and 'video' as cork roles.");
253 pa_idxset_put(u
->cork_roles
, pa_xstrdup("music"), NULL
);
254 pa_idxset_put(u
->cork_roles
, pa_xstrdup("video"), NULL
);
257 if (pa_modargs_get_value_boolean(ma
, "global", &global
) < 0) {
258 pa_log("Invalid boolean parameter: global");
263 u
->sink_input_put_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PUT
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_put_cb
, u
);
264 u
->sink_input_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_unlink_cb
, u
);
265 u
->sink_input_move_start_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_START
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_move_start_cb
, u
);
266 u
->sink_input_move_finish_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_move_finish_cb
, u
);
282 void pa__done(pa_module
*m
) {
287 if (!(u
= m
->userdata
))
290 if (u
->trigger_roles
)
291 pa_idxset_free(u
->trigger_roles
, pa_xfree
);
294 pa_idxset_free(u
->cork_roles
, pa_xfree
);
296 if (u
->sink_input_put_slot
)
297 pa_hook_slot_free(u
->sink_input_put_slot
);
298 if (u
->sink_input_unlink_slot
)
299 pa_hook_slot_free(u
->sink_input_unlink_slot
);
300 if (u
->sink_input_move_start_slot
)
301 pa_hook_slot_free(u
->sink_input_move_start_slot
);
302 if (u
->sink_input_move_finish_slot
)
303 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
306 pa_hashmap_free(u
->cork_state
);