2 This file is part of PulseAudio.
4 Copyright 2012 Flavio Ceolin <flavio.ceolin@profusion.mobi>
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/volume.h>
27 #include <pulse/xmalloc.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/hashmap.h>
31 #include <pulsecore/hook-list.h>
32 #include <pulsecore/core.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/sink-input.h>
35 #include <pulsecore/modargs.h>
37 #include "module-role-ducking-symdef.h"
39 PA_MODULE_AUTHOR("Flavio Ceolin <flavio.ceolin@profusion.mobi>");
40 PA_MODULE_DESCRIPTION("Apply a ducking effect based on streams roles");
41 PA_MODULE_VERSION(PACKAGE_VERSION);
42 PA_MODULE_LOAD_ONCE(true);
44 "trigger_roles=<Comma separated list of roles which will trigger a ducking> "
45 "ducking_roles=<Comma separated list of roles which will be ducked> "
46 "global=<Should we operate globally or only inside the same device?>"
47 "volume=<Volume for the attenuated streams. Default: -20dB"
50 static const char* const valid_modargs[] = {
61 pa_idxset *trigger_roles;
62 pa_idxset *ducking_roles;
63 pa_idxset *ducked_inputs;
68 *sink_input_unlink_slot,
69 *sink_input_move_start_slot,
70 *sink_input_move_finish_slot;
73 static bool sink_has_trigger_streams(struct userdata *u, pa_sink *s, pa_sink_input *ignore) {
75 uint32_t idx, role_idx;
76 const char *trigger_role;
79 pa_sink_assert_ref(s);
81 PA_IDXSET_FOREACH(j, s->inputs, idx) {
87 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
90 PA_IDXSET_FOREACH(trigger_role, u->trigger_roles, role_idx) {
91 if (pa_streq(role, trigger_role)) {
92 pa_log_debug("Found a '%s' stream that will trigger the ducking.", trigger_role);
101 static void apply_ducking_to_sink(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool duck) {
103 uint32_t idx, role_idx;
104 const char *ducking_role;
105 bool trigger = false;
108 pa_sink_assert_ref(s);
110 PA_IDXSET_FOREACH(j, s->inputs, idx) {
117 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
120 PA_IDXSET_FOREACH(ducking_role, u->ducking_roles, role_idx) {
121 if ((trigger = pa_streq(role, ducking_role)))
127 i = pa_idxset_get_by_data(u->ducked_inputs, j, NULL);
131 vol.values[0] = u->volume;
133 pa_log_debug("Found a '%s' stream that should be ducked.", ducking_role);
134 pa_sink_input_add_volume_factor(j, u->name, &vol);
135 pa_idxset_put(u->ducked_inputs, j, NULL);
136 } else if (!duck && i) { /* This stream should not longer be ducked */
137 pa_log_debug("Found a '%s' stream that should be unducked", ducking_role);
138 pa_idxset_remove_by_data(u->ducked_inputs, j, NULL);
139 pa_sink_input_remove_volume_factor(j, u->name);
144 static void apply_ducking(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool duck) {
149 PA_IDXSET_FOREACH(s, u->core->sinks, idx)
150 apply_ducking_to_sink(u, s, ignore, duck);
152 apply_ducking_to_sink(u, s, ignore, duck);
155 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, bool duck) {
156 bool should_duck = false;
160 pa_sink_input_assert_ref(i);
162 if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
168 should_duck = sink_has_trigger_streams(u, i->sink, duck ? NULL : i);
169 apply_ducking(u, i->sink, duck ? NULL : i, should_duck);
174 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
175 pa_core_assert_ref(core);
176 pa_sink_input_assert_ref(i);
178 return process(u, i, true);
181 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
182 pa_sink_input_assert_ref(i);
184 pa_idxset_remove_by_data(u->ducked_inputs, i, NULL);
185 return process(u, i, false);
188 static pa_hook_result_t sink_input_move_start_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
189 pa_core_assert_ref(core);
190 pa_sink_input_assert_ref(i);
192 return process(u, i, false);
195 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
196 pa_core_assert_ref(core);
197 pa_sink_input_assert_ref(i);
199 return process(u, i, true);
202 int pa__init(pa_module *m) {
203 pa_modargs *ma = NULL;
209 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
210 pa_log("Failed to parse module arguments");
214 m->userdata = u = pa_xnew0(struct userdata, 1);
219 u->ducked_inputs = pa_idxset_new(NULL, NULL);
221 u->trigger_roles = pa_idxset_new(NULL, NULL);
222 roles = pa_modargs_get_value(ma, "trigger_roles", NULL);
224 const char *split_state = NULL;
226 while ((n = pa_split(roles, ",", &split_state))) {
228 pa_idxset_put(u->trigger_roles, n, NULL);
233 if (pa_idxset_isempty(u->trigger_roles)) {
234 pa_log_debug("Using role 'phone' as trigger role.");
235 pa_idxset_put(u->trigger_roles, pa_xstrdup("phone"), NULL);
238 u->ducking_roles = pa_idxset_new(NULL, NULL);
239 roles = pa_modargs_get_value(ma, "ducking_roles", NULL);
241 const char *split_state = NULL;
243 while ((n = pa_split(roles, ",", &split_state))) {
245 pa_idxset_put(u->ducking_roles, n, NULL);
250 if (pa_idxset_isempty(u->ducking_roles)) {
251 pa_log_debug("Using roles 'music' and 'video' as ducking roles.");
252 pa_idxset_put(u->ducking_roles, pa_xstrdup("music"), NULL);
253 pa_idxset_put(u->ducking_roles, pa_xstrdup("video"), NULL);
257 if (pa_modargs_get_value_boolean(ma, "global", &u->global) < 0) {
258 pa_log("Failed to parse a boolean parameter: global");
262 u->volume = pa_sw_volume_from_dB(-20);
263 if (pa_modargs_get_value_volume(ma, "volume", &u->volume) < 0) {
264 pa_log("Failed to parse a volume parameter: volume");
268 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);
269 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);
270 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);
271 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);
286 void pa__done(pa_module *m) {
292 if (!(u = m->userdata))
295 if (u->trigger_roles)
296 pa_idxset_free(u->trigger_roles, pa_xfree);
298 if (u->ducking_roles)
299 pa_idxset_free(u->ducking_roles, pa_xfree);
301 if (u->ducked_inputs) {
302 while ((i = pa_idxset_steal_first(u->ducked_inputs, NULL)))
303 pa_sink_input_remove_volume_factor(i, u->name);
305 pa_idxset_free(u->ducked_inputs, NULL);
308 if (u->sink_input_put_slot)
309 pa_hook_slot_free(u->sink_input_put_slot);
310 if (u->sink_input_unlink_slot)
311 pa_hook_slot_free(u->sink_input_unlink_slot);
312 if (u->sink_input_move_start_slot)
313 pa_hook_slot_free(u->sink_input_move_start_slot);
314 if (u->sink_input_move_finish_slot)
315 pa_hook_slot_free(u->sink_input_move_finish_slot);