]> code.delx.au - pulseaudio/blob - src/modules/module-role-cork.c
6573cd6a4ce65b05ce237c864481304281f69546
[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, muted, 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 muted = pa_sink_input_get_mute(j);
123 corked_here = !!pa_hashmap_get(u->cork_state, j);
124
125 if (cork && !corked && !muted) {
126 pa_log_debug("Found a '%s' stream that should be corked/muted.", cork_role);
127 if (!corked_here)
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);
131 } else if (!cork) {
132 pa_hashmap_remove(u->cork_state, j);
133
134 if (corked_here && (corked || muted)) {
135 pa_log_debug("Found a '%s' stream that should be uncorked/unmuted.", cork_role);
136 if (muted)
137 pa_sink_input_set_mute(j, false, false);
138 if (corked)
139 pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_UNCORK, NULL);
140 }
141 }
142 }
143 }
144
145 static void apply_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore, bool cork) {
146 pa_assert(u);
147
148 if (u->global) {
149 uint32_t idx;
150 PA_IDXSET_FOREACH(s, u->core->sinks, idx)
151 apply_cork_to_sink(u, s, ignore, cork);
152 } else
153 apply_cork_to_sink(u, s, ignore, cork);
154 }
155
156 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, bool create) {
157 bool cork = false;
158 const char *role;
159
160 pa_assert(u);
161 pa_sink_input_assert_ref(i);
162
163 if (!create)
164 pa_hashmap_remove(u->cork_state, i);
165
166 if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
167 return PA_HOOK_OK;
168
169 if (!i->sink)
170 return PA_HOOK_OK;
171
172 cork = shall_cork(u, i->sink, create ? NULL : i);
173 apply_cork(u, i->sink, create ? NULL : i, cork);
174
175 return PA_HOOK_OK;
176 }
177
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);
181
182 return process(u, i, true);
183 }
184
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);
187
188 return process(u, i, false);
189 }
190
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);
194
195 return process(u, i, false);
196 }
197
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);
201
202 return process(u, i, true);
203 }
204
205 int pa__init(pa_module *m) {
206 pa_modargs *ma = NULL;
207 struct userdata *u;
208 const char *roles;
209 bool global = false;
210
211 pa_assert(m);
212
213 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
214 pa_log("Failed to parse module arguments");
215 goto fail;
216 }
217
218 m->userdata = u = pa_xnew(struct userdata, 1);
219
220 u->core = m->core;
221 u->cork_state = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
222
223 u->trigger_roles = pa_idxset_new(NULL, NULL);
224 roles = pa_modargs_get_value(ma, "trigger_roles", NULL);
225 if (roles) {
226 const char *split_state = NULL;
227 char *n = NULL;
228 while ((n = pa_split(roles, ",", &split_state))) {
229 if (n[0] != '\0')
230 pa_idxset_put(u->trigger_roles, n, NULL);
231 else
232 pa_xfree(n);
233 }
234 }
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);
238 }
239
240 u->cork_roles = pa_idxset_new(NULL, NULL);
241 roles = pa_modargs_get_value(ma, "cork_roles", NULL);
242 if (roles) {
243 const char *split_state = NULL;
244 char *n = NULL;
245 while ((n = pa_split(roles, ",", &split_state))) {
246 if (n[0] != '\0')
247 pa_idxset_put(u->cork_roles, n, NULL);
248 else
249 pa_xfree(n);
250 }
251 }
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);
256 }
257
258 if (pa_modargs_get_value_boolean(ma, "global", &global) < 0) {
259 pa_log("Invalid boolean parameter: global");
260 goto fail;
261 }
262 u->global = global;
263
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);
268
269 pa_modargs_free(ma);
270
271 return 0;
272
273 fail:
274 pa__done(m);
275
276 if (ma)
277 pa_modargs_free(ma);
278
279 return -1;
280
281 }
282
283 void pa__done(pa_module *m) {
284 struct userdata* u;
285
286 pa_assert(m);
287
288 if (!(u = m->userdata))
289 return;
290
291 if (u->trigger_roles)
292 pa_idxset_free(u->trigger_roles, pa_xfree);
293
294 if (u->cork_roles)
295 pa_idxset_free(u->cork_roles, pa_xfree);
296
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);
305
306 if (u->cork_state)
307 pa_hashmap_free(u->cork_state);
308
309 pa_xfree(u);
310
311 }