]> code.delx.au - pulseaudio/blob - src/modules/module-filter-heuristics.c
echo-cancel: Don't crash if adjust_time = 0
[pulseaudio] / src / modules / module-filter-heuristics.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2011 Colin Guthrie
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/hook-list.h>
30 #include <pulsecore/core.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/modargs.h>
34
35 #include "module-filter-heuristics-symdef.h"
36
37 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
38 #define PA_PROP_FILTER_HEURISTICS "filter.heuristics"
39
40 PA_MODULE_AUTHOR("Colin Guthrie");
41 PA_MODULE_DESCRIPTION("Detect when various filters are desirable");
42 PA_MODULE_VERSION(PACKAGE_VERSION);
43 PA_MODULE_LOAD_ONCE(TRUE);
44
45 static const char* const valid_modargs[] = {
46 NULL
47 };
48
49 struct userdata {
50 pa_core *core;
51 pa_hook_slot
52 *sink_input_put_slot,
53 *sink_input_move_finish_slot,
54 *source_output_put_slot,
55 *source_output_move_finish_slot;
56 };
57
58 static pa_bool_t role_match(pa_proplist *proplist, const char *role) {
59 const char *ir;
60 char *r;
61 const char *state = NULL;
62
63 if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES)))
64 return FALSE;
65
66 while ((r = pa_split_spaces(ir, &state))) {
67
68 if (pa_streq(role, r)) {
69 pa_xfree(r);
70 return TRUE;
71 }
72
73 pa_xfree(r);
74 }
75
76 return FALSE;
77 }
78
79 static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
80 const char *want;
81 pa_proplist *pl, *parent_pl;
82
83 if (is_sink_input) {
84 pl = PA_SINK_INPUT(o)->proplist;
85 parent_pl = PA_SINK_INPUT(o)->sink->proplist;
86 } else {
87 pl = PA_SOURCE_OUTPUT(o)->proplist;
88 parent_pl = PA_SOURCE_OUTPUT(o)->source->proplist;
89 }
90
91 /* If the stream already specifies what it must have, then let it be. */
92 if (!pa_proplist_gets(pl, PA_PROP_FILTER_HEURISTICS) && pa_proplist_gets(pl, PA_PROP_FILTER_APPLY))
93 return PA_HOOK_OK;
94
95 /* On phone sinks, make sure we're not applying echo cancellation */
96 if (role_match(parent_pl, "phone")) {
97 const char *apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY);
98
99 if (apply && pa_streq(apply, "echo-cancel")) {
100 pa_proplist_unset(pl, PA_PROP_FILTER_APPLY);
101 pa_proplist_unset(pl, PA_PROP_FILTER_HEURISTICS);
102 }
103
104 return PA_HOOK_OK;
105 }
106
107 want = pa_proplist_gets(pl, PA_PROP_FILTER_WANT);
108
109 if (want) {
110 /* There's a filter that we want, ask module-filter-apply to apply it, and remember that we're managing filter.apply */
111 pa_proplist_sets(pl, PA_PROP_FILTER_APPLY, want);
112 pa_proplist_sets(pl, PA_PROP_FILTER_HEURISTICS, "1");
113 }
114
115 return PA_HOOK_OK;
116 }
117
118 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
119 pa_core_assert_ref(core);
120 pa_sink_input_assert_ref(i);
121 pa_assert(u);
122
123 return process(u, PA_OBJECT(i), TRUE);
124 }
125
126 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
127 pa_core_assert_ref(core);
128 pa_sink_input_assert_ref(i);
129 pa_assert(u);
130
131 /* module-filter-apply triggered this move, ignore */
132 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
133 return PA_HOOK_OK;
134
135 return process(u, PA_OBJECT(i), TRUE);
136 }
137
138 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
139 pa_core_assert_ref(core);
140 pa_source_output_assert_ref(i);
141 pa_assert(u);
142
143 return process(u, PA_OBJECT(i), FALSE);
144 }
145
146 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
147 pa_core_assert_ref(core);
148 pa_source_output_assert_ref(i);
149 pa_assert(u);
150
151 /* module-filter-apply triggered this move, ignore */
152 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
153 return PA_HOOK_OK;
154
155 return process(u, PA_OBJECT(i), FALSE);
156 }
157
158 int pa__init(pa_module *m) {
159 pa_modargs *ma = NULL;
160 struct userdata *u;
161
162 pa_assert(m);
163
164 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
165 pa_log("Failed to parse module arguments");
166 goto fail;
167 }
168
169 m->userdata = u = pa_xnew(struct userdata, 1);
170
171 u->core = m->core;
172
173 u->sink_input_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE-1, (pa_hook_cb_t) sink_input_put_cb, u);
174 u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_LATE-1, (pa_hook_cb_t) sink_input_move_finish_cb, u);
175 u->source_output_put_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_LATE-1, (pa_hook_cb_t) source_output_put_cb, u);
176 u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_LATE-1, (pa_hook_cb_t) source_output_move_finish_cb, u);
177
178 pa_modargs_free(ma);
179
180 return 0;
181
182 fail:
183 pa__done(m);
184
185 if (ma)
186 pa_modargs_free(ma);
187
188 return -1;
189
190
191 }
192
193 void pa__done(pa_module *m) {
194 struct userdata* u;
195
196 pa_assert(m);
197
198 if (!(u = m->userdata))
199 return;
200
201 if (u->sink_input_put_slot)
202 pa_hook_slot_free(u->sink_input_put_slot);
203 if (u->sink_input_move_finish_slot)
204 pa_hook_slot_free(u->sink_input_move_finish_slot);
205 if (u->source_output_put_slot)
206 pa_hook_slot_free(u->source_output_put_slot);
207 if (u->source_output_move_finish_slot)
208 pa_hook_slot_free(u->source_output_move_finish_slot);
209
210 pa_xfree(u);
211
212 }