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, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <pulse/xmalloc.h>
25
26 #include <pulsecore/macro.h>
27 #include <pulsecore/hook-list.h>
28 #include <pulsecore/core.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/modargs.h>
32
33 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
34 #define PA_PROP_FILTER_HEURISTICS "filter.heuristics"
35
36 PA_MODULE_AUTHOR("Colin Guthrie");
37 PA_MODULE_DESCRIPTION("Detect when various filters are desirable");
38 PA_MODULE_VERSION(PACKAGE_VERSION);
39 PA_MODULE_LOAD_ONCE(true);
40
41 static const char* const valid_modargs[] = {
42 NULL
43 };
44
45 struct userdata {
46 pa_core *core;
47 };
48
process(struct userdata * u,pa_object * o,bool is_sink_input)49 static pa_hook_result_t process(struct userdata *u, pa_object *o, bool is_sink_input) {
50 const char *want;
51 pa_proplist *pl, *parent_pl;
52
53 if (is_sink_input) {
54 pl = PA_SINK_INPUT(o)->proplist;
55 parent_pl = PA_SINK_INPUT(o)->sink->proplist;
56 } else {
57 pl = PA_SOURCE_OUTPUT(o)->proplist;
58 parent_pl = PA_SOURCE_OUTPUT(o)->source->proplist;
59 }
60
61 /* If the stream already specifies what it must have, then let it be. */
62 if (!pa_proplist_gets(pl, PA_PROP_FILTER_HEURISTICS) && pa_proplist_gets(pl, PA_PROP_FILTER_APPLY))
63 return PA_HOOK_OK;
64
65 /* On phone sinks, make sure we're not applying echo cancellation */
66 if (pa_str_in_list_spaces(pa_proplist_gets(parent_pl, PA_PROP_DEVICE_INTENDED_ROLES), "phone")) {
67 const char *apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY);
68
69 if (apply && pa_streq(apply, "echo-cancel")) {
70 pa_proplist_unset(pl, PA_PROP_FILTER_APPLY);
71 pa_proplist_unset(pl, PA_PROP_FILTER_HEURISTICS);
72 }
73
74 return PA_HOOK_OK;
75 }
76
77 want = pa_proplist_gets(pl, PA_PROP_FILTER_WANT);
78
79 if (want) {
80 /* There's a filter that we want, ask module-filter-apply to apply it, and remember that we're managing filter.apply */
81 pa_proplist_sets(pl, PA_PROP_FILTER_APPLY, want);
82 pa_proplist_sets(pl, PA_PROP_FILTER_HEURISTICS, "1");
83 }
84
85 return PA_HOOK_OK;
86 }
87
sink_input_put_cb(pa_core * core,pa_sink_input * i,struct userdata * u)88 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
89 pa_core_assert_ref(core);
90 pa_sink_input_assert_ref(i);
91 pa_assert(u);
92
93 return process(u, PA_OBJECT(i), true);
94 }
95
sink_input_move_finish_cb(pa_core * core,pa_sink_input * i,struct userdata * u)96 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
97 pa_core_assert_ref(core);
98 pa_sink_input_assert_ref(i);
99 pa_assert(u);
100
101 /* module-filter-apply triggered this move, ignore */
102 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
103 return PA_HOOK_OK;
104
105 return process(u, PA_OBJECT(i), true);
106 }
107
source_output_put_cb(pa_core * core,pa_source_output * i,struct userdata * u)108 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
109 pa_core_assert_ref(core);
110 pa_source_output_assert_ref(i);
111 pa_assert(u);
112
113 return process(u, PA_OBJECT(i), false);
114 }
115
source_output_move_finish_cb(pa_core * core,pa_source_output * i,struct userdata * u)116 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
117 pa_core_assert_ref(core);
118 pa_source_output_assert_ref(i);
119 pa_assert(u);
120
121 /* module-filter-apply triggered this move, ignore */
122 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
123 return PA_HOOK_OK;
124
125 return process(u, PA_OBJECT(i), false);
126 }
127
pa__init(pa_module * m)128 int pa__init(pa_module *m) {
129 pa_modargs *ma = NULL;
130 struct userdata *u;
131
132 pa_assert(m);
133
134 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
135 pa_log("Failed to parse module arguments");
136 goto fail;
137 }
138
139 m->userdata = u = pa_xnew(struct userdata, 1);
140
141 u->core = m->core;
142
143 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], PA_HOOK_LATE-1, (pa_hook_cb_t) sink_input_put_cb, u);
144 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_LATE-1, (pa_hook_cb_t) sink_input_move_finish_cb, u);
145 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], PA_HOOK_LATE-1, (pa_hook_cb_t) source_output_put_cb, u);
146 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_LATE-1, (pa_hook_cb_t) source_output_move_finish_cb, u);
147
148 pa_modargs_free(ma);
149
150 return 0;
151
152 fail:
153 pa__done(m);
154
155 if (ma)
156 pa_modargs_free(ma);
157
158 return -1;
159
160 }
161
pa__done(pa_module * m)162 void pa__done(pa_module *m) {
163 struct userdata* u;
164
165 pa_assert(m);
166
167 if (!(u = m->userdata))
168 return;
169
170 pa_xfree(u);
171
172 }
173