bluetooth: Support port availability flag
[profile/ivi/pulseaudio.git] / 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_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
59     const char *want;
60     pa_proplist *pl, *parent_pl;
61
62     if (is_sink_input) {
63         pl = PA_SINK_INPUT(o)->proplist;
64         parent_pl = PA_SINK_INPUT(o)->sink->proplist;
65     } else {
66         pl = PA_SOURCE_OUTPUT(o)->proplist;
67         parent_pl = PA_SOURCE_OUTPUT(o)->source->proplist;
68     }
69
70     /* If the stream already specifies what it must have, then let it be. */
71     if (!pa_proplist_gets(pl, PA_PROP_FILTER_HEURISTICS) && pa_proplist_gets(pl, PA_PROP_FILTER_APPLY))
72         return PA_HOOK_OK;
73
74     /* On phone sinks, make sure we're not applying echo cancellation */
75     if (pa_str_in_list_spaces(pa_proplist_gets(parent_pl, PA_PROP_DEVICE_INTENDED_ROLES), "phone")) {
76         const char *apply = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY);
77
78         if (apply && pa_streq(apply, "echo-cancel")) {
79             pa_proplist_unset(pl, PA_PROP_FILTER_APPLY);
80             pa_proplist_unset(pl, PA_PROP_FILTER_HEURISTICS);
81         }
82
83         return PA_HOOK_OK;
84     }
85
86     want = pa_proplist_gets(pl, PA_PROP_FILTER_WANT);
87
88     if (want) {
89         /* There's a filter that we want, ask module-filter-apply to apply it, and remember that we're managing filter.apply */
90         pa_proplist_sets(pl, PA_PROP_FILTER_APPLY, want);
91         pa_proplist_sets(pl, PA_PROP_FILTER_HEURISTICS, "1");
92     }
93
94     return PA_HOOK_OK;
95 }
96
97 static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
98     pa_core_assert_ref(core);
99     pa_sink_input_assert_ref(i);
100     pa_assert(u);
101
102     return process(u, PA_OBJECT(i), TRUE);
103 }
104
105 static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
106     pa_core_assert_ref(core);
107     pa_sink_input_assert_ref(i);
108     pa_assert(u);
109
110     /* module-filter-apply triggered this move, ignore */
111     if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
112         return PA_HOOK_OK;
113
114     return process(u, PA_OBJECT(i), TRUE);
115 }
116
117 static pa_hook_result_t source_output_put_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
118     pa_core_assert_ref(core);
119     pa_source_output_assert_ref(i);
120     pa_assert(u);
121
122     return process(u, PA_OBJECT(i), FALSE);
123 }
124
125 static pa_hook_result_t source_output_move_finish_cb(pa_core *core, pa_source_output *i, struct userdata *u) {
126     pa_core_assert_ref(core);
127     pa_source_output_assert_ref(i);
128     pa_assert(u);
129
130     /* module-filter-apply triggered this move, ignore */
131     if (pa_proplist_gets(i->proplist, PA_PROP_FILTER_APPLY_MOVING))
132         return PA_HOOK_OK;
133
134     return process(u, PA_OBJECT(i), FALSE);
135 }
136
137 int pa__init(pa_module *m) {
138     pa_modargs *ma = NULL;
139     struct userdata *u;
140
141     pa_assert(m);
142
143     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
144         pa_log("Failed to parse module arguments");
145         goto fail;
146     }
147
148     m->userdata = u = pa_xnew(struct userdata, 1);
149
150     u->core = m->core;
151
152     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);
153     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);
154     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);
155     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);
156
157     pa_modargs_free(ma);
158
159     return 0;
160
161 fail:
162     pa__done(m);
163
164     if (ma)
165         pa_modargs_free(ma);
166
167     return -1;
168
169
170 }
171
172 void pa__done(pa_module *m) {
173     struct userdata* u;
174
175     pa_assert(m);
176
177     if (!(u = m->userdata))
178         return;
179
180     if (u->sink_input_put_slot)
181         pa_hook_slot_free(u->sink_input_put_slot);
182     if (u->sink_input_move_finish_slot)
183         pa_hook_slot_free(u->sink_input_move_finish_slot);
184     if (u->source_output_put_slot)
185         pa_hook_slot_free(u->source_output_put_slot);
186     if (u->source_output_move_finish_slot)
187         pa_hook_slot_free(u->source_output_move_finish_slot);
188
189     pa_xfree(u);
190
191 }