hashmap: Add pa_hashmap_remove_all()
[platform/upstream/pulseaudio.git] / src / modules / module-role-ducking.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2012 Flavio Ceolin <flavio.ceolin@profusion.mobi>
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/volume.h>
27 #include <pulse/xmalloc.h>
28
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>
36
37 #include "module-role-ducking-symdef.h"
38
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);
43 PA_MODULE_USAGE(
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"
48 );
49
50 static const char* const valid_modargs[] = {
51     "trigger_roles",
52     "ducking_roles",
53     "global",
54     "volume",
55     NULL
56 };
57
58 struct userdata {
59     pa_core *core;
60     const char *name;
61     pa_idxset *trigger_roles;
62     pa_idxset *ducking_roles;
63     pa_idxset *ducked_inputs;
64     bool global;
65     pa_volume_t volume;
66     pa_hook_slot
67         *sink_input_put_slot,
68         *sink_input_unlink_slot,
69         *sink_input_move_start_slot,
70         *sink_input_move_finish_slot;
71 };
72
73 static bool sink_has_trigger_streams(struct userdata *u, pa_sink *s, pa_sink_input *ignore) {
74     pa_sink_input *j;
75     uint32_t idx, role_idx;
76     const char *trigger_role;
77
78     pa_assert(u);
79     pa_sink_assert_ref(s);
80
81     PA_IDXSET_FOREACH(j, s->inputs, idx) {
82         const char *role;
83
84         if (j == ignore)
85             continue;
86
87         if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
88             continue;
89
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);
93                 return true;
94             }
95         }
96     }
97
98     return false;
99 }
100
101 static void apply_ducking_to_sink(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool duck) {
102     pa_sink_input *j;
103     uint32_t idx, role_idx;
104     const char *ducking_role;
105     bool trigger = false;
106
107     pa_assert(u);
108     pa_sink_assert_ref(s);
109
110     PA_IDXSET_FOREACH(j, s->inputs, idx) {
111         const char *role;
112         pa_sink_input *i;
113
114         if (j == ignore)
115             continue;
116
117         if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
118             continue;
119
120         PA_IDXSET_FOREACH(ducking_role, u->ducking_roles, role_idx) {
121             if ((trigger = pa_streq(role, ducking_role)))
122                 break;
123         }
124         if (!trigger)
125             continue;
126
127         i = pa_idxset_get_by_data(u->ducked_inputs, j, NULL);
128         if (duck && !i) {
129             pa_cvolume vol;
130             vol.channels = 1;
131             vol.values[0] = u->volume;
132
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);
140         }
141     }
142 }
143
144 static void apply_ducking(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool duck) {
145     pa_assert(u);
146
147     if (u->global) {
148         uint32_t idx;
149         PA_IDXSET_FOREACH(s, u->core->sinks, idx)
150             apply_ducking_to_sink(u, s, ignore, duck);
151     } else
152         apply_ducking_to_sink(u, s, ignore, duck);
153 }
154
155 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, bool duck) {
156     bool should_duck = false;
157     const char *role;
158
159     pa_assert(u);
160     pa_sink_input_assert_ref(i);
161
162     if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
163         return PA_HOOK_OK;
164
165     if (!i->sink)
166         return PA_HOOK_OK;
167
168     should_duck = sink_has_trigger_streams(u, i->sink, duck ? NULL : i);
169     apply_ducking(u, i->sink, duck ? NULL : i, should_duck);
170
171     return PA_HOOK_OK;
172 }
173
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);
177
178     return process(u, i, true);
179 }
180
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);
183
184     pa_idxset_remove_by_data(u->ducked_inputs, i, NULL);
185     return process(u, i, false);
186 }
187
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);
191
192     return process(u, i, false);
193 }
194
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);
198
199     return process(u, i, true);
200 }
201
202 int pa__init(pa_module *m) {
203     pa_modargs *ma = NULL;
204     struct userdata *u;
205     const char *roles;
206
207     pa_assert(m);
208
209     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
210         pa_log("Failed to parse module arguments");
211         goto fail;
212     }
213
214     m->userdata = u = pa_xnew0(struct userdata, 1);
215
216     u->core = m->core;
217     u->name = m->name;
218
219     u->ducked_inputs = pa_idxset_new(NULL, NULL);
220
221     u->trigger_roles = pa_idxset_new(NULL, NULL);
222     roles = pa_modargs_get_value(ma, "trigger_roles", NULL);
223     if (roles) {
224         const char *split_state = NULL;
225         char *n = NULL;
226         while ((n = pa_split(roles, ",", &split_state))) {
227             if (n[0] != '\0')
228                 pa_idxset_put(u->trigger_roles, n, NULL);
229             else
230                 pa_xfree(n);
231         }
232     }
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);
236     }
237
238     u->ducking_roles = pa_idxset_new(NULL, NULL);
239     roles = pa_modargs_get_value(ma, "ducking_roles", NULL);
240     if (roles) {
241         const char *split_state = NULL;
242         char *n = NULL;
243         while ((n = pa_split(roles, ",", &split_state))) {
244             if (n[0] != '\0')
245                 pa_idxset_put(u->ducking_roles, n, NULL);
246             else
247                 pa_xfree(n);
248         }
249     }
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);
254     }
255
256     u->global = false;
257     if (pa_modargs_get_value_boolean(ma, "global", &u->global) < 0) {
258         pa_log("Failed to parse a boolean parameter: global");
259         goto fail;
260     }
261
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");
265         goto fail;
266     }
267
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);
272
273     pa_modargs_free(ma);
274
275     return 0;
276
277 fail:
278     pa__done(m);
279
280     if (ma)
281         pa_modargs_free(ma);
282
283     return -1;
284 }
285
286 void pa__done(pa_module *m) {
287     struct userdata* u;
288     pa_sink_input *i;
289
290     pa_assert(m);
291
292     if (!(u = m->userdata))
293         return;
294
295     if (u->trigger_roles)
296         pa_idxset_free(u->trigger_roles, pa_xfree);
297
298     if (u->ducking_roles)
299         pa_idxset_free(u->ducking_roles, pa_xfree);
300
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);
304
305         pa_idxset_free(u->ducked_inputs, NULL);
306     }
307
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);
316
317     pa_xfree(u);
318 }