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, muted, 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 muted = pa_sink_input_get_mute(j);
123 corked_here = !!pa_hashmap_get(u->cork_state, j);
125 if (cork && !corked && !muted) {
126 pa_log_debug("Found a '%s' stream that should be corked/muted.", cork_role);
128 pa_hashmap_put(u->cork_state, j, PA_INT_TO_PTR(1));
129 pa_sink_input_set_mute(j, true, false);
130 pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_CORK, NULL);
132 pa_hashmap_remove(u->cork_state, j);
134 if (corked_here && (corked || muted)) {
135 pa_log_debug("Found a '%s' stream that should be uncorked/unmuted.", cork_role);
137 pa_sink_input_set_mute(j, false, false);
139 pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_UNCORK, NULL);
145 static void apply_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool cork) {
150 PA_IDXSET_FOREACH(s, u->core->sinks, idx)
151 apply_cork_to_sink(u, s, ignore, cork);
153 apply_cork_to_sink(u, s, ignore, cork);
156 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, bool create) {
161 pa_sink_input_assert_ref(i);
164 pa_hashmap_remove(u->cork_state, i);
166 if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
172 cork = shall_cork(u, i->sink, create ? NULL : i);
173 apply_cork(u, i->sink, create ? NULL : i, cork);
178 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
179 pa_core_assert_ref(core);
180 pa_sink_input_assert_ref(i);
182 return process(u, i, true);
185 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
186 pa_sink_input_assert_ref(i);
188 return process(u, i, false);
191 static pa_hook_result_t sink_input_move_start_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
192 pa_core_assert_ref(core);
193 pa_sink_input_assert_ref(i);
195 return process(u, i, false);
198 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
199 pa_core_assert_ref(core);
200 pa_sink_input_assert_ref(i);
202 return process(u, i, true);
205 int pa__init(pa_module *m) {
206 pa_modargs *ma = NULL;
213 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
214 pa_log("Failed to parse module arguments");
218 m->userdata = u = pa_xnew(struct userdata, 1);
221 u->cork_state = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
223 u->trigger_roles = pa_idxset_new(NULL, NULL);
224 roles = pa_modargs_get_value(ma, "trigger_roles", NULL);
226 const char *split_state = NULL;
228 while ((n = pa_split(roles, ",", &split_state))) {
230 pa_idxset_put(u->trigger_roles, n, NULL);
235 if (pa_idxset_isempty(u->trigger_roles)) {
236 pa_log_debug("Using role 'phone' as trigger role.");
237 pa_idxset_put(u->trigger_roles, pa_xstrdup("phone"), NULL);
240 u->cork_roles = pa_idxset_new(NULL, NULL);
241 roles = pa_modargs_get_value(ma, "cork_roles", NULL);
243 const char *split_state = NULL;
245 while ((n = pa_split(roles, ",", &split_state))) {
247 pa_idxset_put(u->cork_roles, n, NULL);
252 if (pa_idxset_isempty(u->cork_roles)) {
253 pa_log_debug("Using roles 'music' and 'video' as cork roles.");
254 pa_idxset_put(u->cork_roles, pa_xstrdup("music"), NULL);
255 pa_idxset_put(u->cork_roles, pa_xstrdup("video"), NULL);
258 if (pa_modargs_get_value_boolean(ma, "global", &global) < 0) {
259 pa_log("Invalid boolean parameter: global");
264 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);
265 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);
266 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);
267 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);
283 void pa__done(pa_module *m) {
288 if (!(u = m->userdata))
291 if (u->trigger_roles)
292 pa_idxset_free(u->trigger_roles, pa_xfree);
295 pa_idxset_free(u->cork_roles, pa_xfree);
297 if (u->sink_input_put_slot)
298 pa_hook_slot_free(u->sink_input_put_slot);
299 if (u->sink_input_unlink_slot)
300 pa_hook_slot_free(u->sink_input_unlink_slot);
301 if (u->sink_input_move_start_slot)
302 pa_hook_slot_free(u->sink_input_move_start_slot);
303 if (u->sink_input_move_finish_slot)
304 pa_hook_slot_free(u->sink_input_move_finish_slot);
307 pa_hashmap_free(u->cork_state, NULL);