default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / modules / module-filter-heuristics.c
blob222787fc9c704a827654b84ad91482ec2318e900
1 /***
2 This file is part of PulseAudio.
4 Copyright 2011 Colin Guthrie
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <pulse/xmalloc.h>
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>
35 #include "module-filter-heuristics-symdef.h"
37 #define PA_PROP_FILTER_APPLY_MOVING "filter.apply.moving"
38 #define PA_PROP_FILTER_HEURISTICS "filter.heuristics"
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);
45 static const char* const valid_modargs[] = {
46 NULL
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;
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;
63 if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES)))
64 return FALSE;
66 while ((r = pa_split_spaces(ir, &state))) {
68 if (pa_streq(role, r)) {
69 pa_xfree(r);
70 return TRUE;
73 pa_xfree(r);
76 return FALSE;
79 static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
80 const char *want, *stream_role;
81 pa_proplist *pl, *parent_pl;
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;
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;
95 want = pa_proplist_gets(pl, PA_PROP_FILTER_WANT);
96 if (!want) {
97 /* This is a phone stream, maybe we want echo cancellation */
98 if ((stream_role = pa_proplist_gets(pl, PA_PROP_MEDIA_ROLE)) && pa_streq(stream_role, "phone"))
99 want = "echo-cancel";
102 /* On phone sinks, make sure we're not applying echo cancellation */
103 if (role_match(parent_pl, "phone")) {
104 const char *apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY);
106 if (apply && pa_streq(apply, "echo-cancel")) {
107 pa_proplist_unset(pl, PA_PROP_FILTER_APPLY);
108 pa_proplist_unset(pl, PA_PROP_FILTER_HEURISTICS);
111 return PA_HOOK_OK;
114 if (want) {
115 /* There's a filter that we want, ask module-filter-apply to apply it, and remember that we're managing filter.apply */
116 pa_proplist_sets(pl, PA_PROP_FILTER_APPLY, want);
117 pa_proplist_sets(pl, PA_PROP_FILTER_HEURISTICS, "1");
120 return PA_HOOK_OK;
123 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
124 pa_core_assert_ref(core);
125 pa_sink_input_assert_ref(i);
126 pa_assert(u);
128 return process(u, PA_OBJECT(i), TRUE);
131 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
132 pa_core_assert_ref(core);
133 pa_sink_input_assert_ref(i);
134 pa_assert(u);
136 /* module-filter-apply triggered this move, ignore */
137 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
138 return PA_HOOK_OK;
140 return process(u, PA_OBJECT(i), TRUE);
143 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
144 pa_core_assert_ref(core);
145 pa_source_output_assert_ref(i);
146 pa_assert(u);
148 return process(u, PA_OBJECT(i), FALSE);
151 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
152 pa_core_assert_ref(core);
153 pa_source_output_assert_ref(i);
154 pa_assert(u);
156 /* module-filter-apply triggered this move, ignore */
157 if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
158 return PA_HOOK_OK;
160 return process(u, PA_OBJECT(i), FALSE);
163 int pa__init(pa_module *m) {
164 pa_modargs *ma = NULL;
165 struct userdata *u;
167 pa_assert(m);
169 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
170 pa_log("Failed to parse module arguments");
171 goto fail;
174 m->userdata = u = pa_xnew(struct userdata, 1);
176 u->core = m->core;
178 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);
179 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);
180 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);
181 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);
183 pa_modargs_free(ma);
185 return 0;
187 fail:
188 pa__done(m);
190 if (ma)
191 pa_modargs_free(ma);
193 return -1;
198 void pa__done(pa_module *m) {
199 struct userdata* u;
201 pa_assert(m);
203 if (!(u = m->userdata))
204 return;
206 if (u->sink_input_put_slot)
207 pa_hook_slot_free(u->sink_input_put_slot);
208 if (u->sink_input_move_finish_slot)
209 pa_hook_slot_free(u->sink_input_move_finish_slot);
210 if (u->source_output_put_slot)
211 pa_hook_slot_free(u->source_output_put_slot);
212 if (u->source_output_move_finish_slot)
213 pa_hook_slot_free(u->source_output_move_finish_slot);
215 pa_xfree(u);