]> code.delx.au - pulseaudio/blob - src/modules/module-role-cork.c
role-cork: Rename module-cork-music-on-phone to module-role-cork.
[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
43 static const char* const valid_modargs[] = {
44 NULL
45 };
46
47 struct userdata {
48 pa_core *core;
49 pa_hashmap *cork_state;
50 pa_hook_slot
51 *sink_input_put_slot,
52 *sink_input_unlink_slot,
53 *sink_input_move_start_slot,
54 *sink_input_move_finish_slot;
55 };
56
57 static pa_bool_t shall_cork(pa_sink *s, pa_sink_input *ignore) {
58 pa_sink_input *j;
59 uint32_t idx;
60 pa_sink_assert_ref(s);
61
62 for (j = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); j; j = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
63 const char *role;
64
65 if (j == ignore)
66 continue;
67
68 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
69 continue;
70
71 if (pa_streq(role, "phone")) {
72 pa_log_debug("Found a phone stream that will trigger the auto-cork.");
73 return TRUE;
74 }
75 }
76
77 return FALSE;
78 }
79
80 static void apply_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore, pa_bool_t cork) {
81 pa_sink_input *j;
82 uint32_t idx;
83
84 pa_assert(u);
85 pa_sink_assert_ref(s);
86
87 for (j = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); j; j = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
88 pa_bool_t corked, muted, corked_here;
89 const char *role;
90
91 if (j == ignore)
92 continue;
93
94 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
95 continue;
96
97 if (!pa_streq(role, "video") &&
98 !pa_streq(role, "music"))
99 continue;
100
101 corked = (pa_sink_input_get_state(j) == PA_SINK_INPUT_CORKED);
102 muted = pa_sink_input_get_mute(j);
103 corked_here = !!pa_hashmap_get(u->cork_state, j);
104
105 if (cork && !corked && !muted) {
106 pa_log_debug("Found a music/video stream that should be corked/muted.");
107 if (!corked_here)
108 pa_hashmap_put(u->cork_state, j, PA_INT_TO_PTR(1));
109 pa_sink_input_set_mute(j, TRUE, FALSE);
110 pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_CORK, NULL);
111 } else if (!cork) {
112 pa_hashmap_remove(u->cork_state, j);
113
114 if (corked_here && (corked || muted)) {
115 pa_log_debug("Found a music/video stream that should be uncorked/unmuted.");
116 if (muted)
117 pa_sink_input_set_mute(j, FALSE, FALSE);
118 if (corked)
119 pa_sink_input_send_event(j, PA_STREAM_EVENT_REQUEST_UNCORK, NULL);
120 }
121 }
122 }
123 }
124
125 static pa_hook_result_t process(struct userdata *u, pa_sink_input *i, pa_bool_t create) {
126 pa_bool_t cork = FALSE;
127 const char *role;
128
129 pa_assert(u);
130 pa_sink_input_assert_ref(i);
131
132 if (!create)
133 pa_hashmap_remove(u->cork_state, i);
134
135 if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
136 return PA_HOOK_OK;
137
138 if (!pa_streq(role, "phone") &&
139 !pa_streq(role, "music") &&
140 !pa_streq(role, "video"))
141 return PA_HOOK_OK;
142
143 if (!i->sink)
144 return PA_HOOK_OK;
145
146 cork = shall_cork(i->sink, create ? NULL : i);
147 apply_cork(u, i->sink, create ? NULL : i, cork);
148
149 return PA_HOOK_OK;
150 }
151
152 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
153 pa_core_assert_ref(core);
154 pa_sink_input_assert_ref(i);
155
156 return process(u, i, TRUE);
157 }
158
159 static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
160 pa_sink_input_assert_ref(i);
161
162 return process(u, i, FALSE);
163 }
164
165 static pa_hook_result_t sink_input_move_start_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
166 pa_core_assert_ref(core);
167 pa_sink_input_assert_ref(i);
168
169 return process(u, i, FALSE);
170 }
171
172 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
173 pa_core_assert_ref(core);
174 pa_sink_input_assert_ref(i);
175
176 return process(u, i, TRUE);
177 }
178
179 int pa__init(pa_module *m) {
180 pa_modargs *ma = NULL;
181 struct userdata *u;
182
183 pa_assert(m);
184
185 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
186 pa_log("Failed to parse module arguments");
187 goto fail;
188 }
189
190 m->userdata = u = pa_xnew(struct userdata, 1);
191
192 u->core = m->core;
193 u->cork_state = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
194
195 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);
196 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);
197 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);
198 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);
199
200 pa_modargs_free(ma);
201
202 return 0;
203
204 fail:
205 pa__done(m);
206
207 if (ma)
208 pa_modargs_free(ma);
209
210 return -1;
211
212
213 }
214
215 void pa__done(pa_module *m) {
216 struct userdata* u;
217
218 pa_assert(m);
219
220 if (!(u = m->userdata))
221 return;
222
223 if (u->sink_input_put_slot)
224 pa_hook_slot_free(u->sink_input_put_slot);
225 if (u->sink_input_unlink_slot)
226 pa_hook_slot_free(u->sink_input_unlink_slot);
227 if (u->sink_input_move_start_slot)
228 pa_hook_slot_free(u->sink_input_move_start_slot);
229 if (u->sink_input_move_finish_slot)
230 pa_hook_slot_free(u->sink_input_move_finish_slot);
231
232 if (u->cork_state)
233 pa_hashmap_free(u->cork_state, NULL, NULL);
234
235 pa_xfree(u);
236
237 }