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
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
[] = {
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
) {
61 const char *state
= NULL
;
63 if (!(ir
= pa_proplist_gets(proplist
, PA_PROP_DEVICE_INTENDED_ROLES
)))
66 while ((r
= pa_split_spaces(ir
, &state
))) {
68 if (pa_streq(role
, r
)) {
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
;
84 pl
= PA_SINK_INPUT(o
)->proplist
;
85 parent_pl
= PA_SINK_INPUT(o
)->sink
->proplist
;
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
))
95 want
= pa_proplist_gets(pl
, PA_PROP_FILTER_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"))
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
);
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");
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
);
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
);
136 /* module-filter-apply triggered this move, ignore */
137 if (pa_proplist_gets(i
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
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
);
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
);
156 /* module-filter-apply triggered this move, ignore */
157 if (pa_proplist_gets(i
->proplist
, PA_PROP_FILTER_APPLY_MOVING
))
160 return process(u
, PA_OBJECT(i
), FALSE
);
163 int pa__init(pa_module
*m
) {
164 pa_modargs
*ma
= NULL
;
169 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
170 pa_log("Failed to parse module arguments");
174 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
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
);
198 void pa__done(pa_module
*m
) {
203 if (!(u
= m
->userdata
))
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
);