]> code.delx.au - pulseaudio/blob - src/modules/module-role-cork.c
sink-input, source-output: Remove redundant get_mute() functions
[pulseaudio] / src / modules / module-role-cork.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
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/xmalloc.h>
27
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>
35
36 #include "module-role-cork-symdef.h"
37
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);
42 PA_MODULE_USAGE(
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?>");
46
47 static const char* const valid_modargs[] = {
48 "trigger_roles",
49 "cork_roles",
50 "global",
51 NULL
52 };
53
54 struct userdata {
55 pa_core *core;
56 pa_hashmap *cork_state;
57 pa_idxset *trigger_roles;
58 pa_idxset *cork_roles;
59 bool global:1;
60 pa_hook_slot
61 *sink_input_put_slot,
62 *sink_input_unlink_slot,
63 *sink_input_move_start_slot,
64 *sink_input_move_finish_slot;
65 };
66
67 static bool shall_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore) {
68 pa_sink_input *j;
69 uint32_t idx, role_idx;
70 const char *trigger_role;
71
72 pa_assert(u);
73 pa_sink_assert_ref(s);
74
75 for (j = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); j; j = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
76 const char *role;
77
78 if (j == ignore)
79 continue;
80
81 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
82 continue;
83
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);
87 return true;
88 }
89 }
90 }
91
92 return false;
93 }
94
95 static inline void apply_cork_to_sink(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool cork) {
96 pa_sink_input *j;
97 uint32_t idx, role_idx;
98 const char *cork_role;
99 bool trigger = false;
100
101 pa_assert(u);
102 pa_sink_assert_ref(s);
103
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;
106 const char *role;
107
108 if (j == ignore)
109 continue;
110
111 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
112 continue;
113
114 PA_IDXSET_FOREACH(cork_role, u->cork_roles, role_idx) {
115 if ((trigger = pa_streq(role, cork_role)))
116 break;
117 }
118 if (!trigger)
119 continue;
120
121 corked = (pa_sink_input_get_state(j) == PA_SINK_INPUT_CORKED);
122 corked_here = !!pa_hashmap_get(u->cork_state, j);
123
124 if (cork && !corked && !j->muted) {
125 pa_log_debug("Found a '%s' stream that should be corked/muted.", cork_role);
126 if (!corked_here)
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);
130 } else if (!cork) {
131 pa_hashmap_remove(u->cork_state, j);
132
133 if (corked_here && (corked || j->muted)) {
134 pa_log_debug("Found a '%s' stream that should be uncorked/unmuted.", cork_role);
135 if (j->muted)
136 pa_sink_input_set_mute(j, false, false);
137 if (corked)
138 pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_UNCORK, NULL);
139 }
140 }
141 }
142 }
143
144 static void apply_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool cork) {
145 pa_assert(u);
146
147 if (u->global) {
148 uint32_t idx;
149 PA_IDXSET_FOREACH(s, u->core->sinks, idx)
150 apply_cork_to_sink(u, s, ignore, cork);
151 } else
152 apply_cork_to_sink(u, s, ignore, cork);
153 }
154
155 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, bool create) {
156 bool cork = false;
157 const char *role;
158
159 pa_assert(u);
160 pa_sink_input_assert_ref(i);
161
162 if (!create)
163 pa_hashmap_remove(u->cork_state, i);
164
165 if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
166 return PA_HOOK_OK;
167
168 if (!i->sink)
169 return PA_HOOK_OK;
170
171 cork = shall_cork(u, i->sink, create ? NULL : i);
172 apply_cork(u, i->sink, create ? NULL : i, cork);
173
174 return PA_HOOK_OK;
175 }
176
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);
180
181 return process(u, i, true);
182 }
183
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);
186
187 return process(u, i, false);
188 }
189
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);
193
194 return process(u, i, false);
195 }
196
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);
200
201 return process(u, i, true);
202 }
203
204 int pa__init(pa_module *m) {
205 pa_modargs *ma = NULL;
206 struct userdata *u;
207 const char *roles;
208 bool global = false;
209
210 pa_assert(m);
211
212 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
213 pa_log("Failed to parse module arguments");
214 goto fail;
215 }
216
217 m->userdata = u = pa_xnew(struct userdata, 1);
218
219 u->core = m->core;
220 u->cork_state = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
221
222 u->trigger_roles = pa_idxset_new(NULL, NULL);
223 roles = pa_modargs_get_value(ma, "trigger_roles", NULL);
224 if (roles) {
225 const char *split_state = NULL;
226 char *n = NULL;
227 while ((n = pa_split(roles, ",", &split_state))) {
228 if (n[0] != '\0')
229 pa_idxset_put(u->trigger_roles, n, NULL);
230 else
231 pa_xfree(n);
232 }
233 }
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);
237 }
238
239 u->cork_roles = pa_idxset_new(NULL, NULL);
240 roles = pa_modargs_get_value(ma, "cork_roles", NULL);
241 if (roles) {
242 const char *split_state = NULL;
243 char *n = NULL;
244 while ((n = pa_split(roles, ",", &split_state))) {
245 if (n[0] != '\0')
246 pa_idxset_put(u->cork_roles, n, NULL);
247 else
248 pa_xfree(n);
249 }
250 }
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);
255 }
256
257 if (pa_modargs_get_value_boolean(ma, "global", &global) < 0) {
258 pa_log("Invalid boolean parameter: global");
259 goto fail;
260 }
261 u->global = global;
262
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);
267
268 pa_modargs_free(ma);
269
270 return 0;
271
272 fail:
273 pa__done(m);
274
275 if (ma)
276 pa_modargs_free(ma);
277
278 return -1;
279
280 }
281
282 void pa__done(pa_module *m) {
283 struct userdata* u;
284
285 pa_assert(m);
286
287 if (!(u = m->userdata))
288 return;
289
290 if (u->trigger_roles)
291 pa_idxset_free(u->trigger_roles, pa_xfree);
292
293 if (u->cork_roles)
294 pa_idxset_free(u->cork_roles, pa_xfree);
295
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);
304
305 if (u->cork_state)
306 pa_hashmap_free(u->cork_state);
307
308 pa_xfree(u);
309
310 }