2 This file is part of PulseAudio.
4 Copyright 2010 Wim Taymans <wim.taymans@gmail.com>
6 Based on module-virtual-sink.c
7 module-virtual-source.c
10 Copyright 2010 Intel Corporation
11 Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
13 PulseAudio is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published
15 by the Free Software Foundation; either version 2.1 of the License,
16 or (at your option) any later version.
18 PulseAudio is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public License
24 along with PulseAudio; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include "echo-cancel.h"
37 #include <pulse/xmalloc.h>
38 #include <pulse/timeval.h>
39 #include <pulse/rtclock.h>
41 #include <pulsecore/i18n.h>
42 #include <pulsecore/atomic.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/namereg.h>
45 #include <pulsecore/sink.h>
46 #include <pulsecore/module.h>
47 #include <pulsecore/core-rtclock.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/log.h>
51 #include <pulsecore/rtpoll.h>
52 #include <pulsecore/sample-util.h>
53 #include <pulsecore/ltdl-helper.h>
55 #include "module-echo-cancel-symdef.h"
57 PA_MODULE_AUTHOR("Wim Taymans");
58 PA_MODULE_DESCRIPTION("Echo Cancelation");
59 PA_MODULE_VERSION(PACKAGE_VERSION
);
60 PA_MODULE_LOAD_ONCE(FALSE
);
62 _("source_name=<name for the source> "
63 "source_properties=<properties for the source> "
64 "source_master=<name of source to filter> "
65 "sink_name=<name for the sink> "
66 "sink_properties=<properties for the sink> "
67 "sink_master=<name of sink to filter> "
68 "adjust_time=<how often to readjust rates in s> "
69 "format=<sample format> "
71 "channels=<number of channels> "
72 "channel_map=<channel map> "
73 "aec_method=<implementation to use> "
74 "aec_args=<parameters for the AEC engine> "
75 "agc=<perform automagic gain control?> "
76 "denoise=<apply denoising?> "
77 "echo_suppress=<perform residual echo suppression? (only with the speex canceller)> "
78 "echo_suppress_attenuation=<dB value of residual echo attenuation> "
79 "echo_suppress_attenuation_active=<dB value of residual echo attenuation when near end is active> "
80 "save_aec=<save AEC data in /tmp> "
81 "autoloaded=<set if this module is being loaded automatically> "
84 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
86 PA_ECHO_CANCELLER_INVALID
= -1,
87 PA_ECHO_CANCELLER_SPEEX
= 0,
88 PA_ECHO_CANCELLER_ADRIAN
,
89 } pa_echo_canceller_method_t
;
91 #define DEFAULT_ECHO_CANCELLER "speex"
93 static const pa_echo_canceller ec_table
[] = {
96 .init
= pa_speex_ec_init
,
97 .run
= pa_speex_ec_run
,
98 .done
= pa_speex_ec_done
,
101 /* Adrian Andre's NLMS implementation */
102 .init
= pa_adrian_ec_init
,
103 .run
= pa_adrian_ec_run
,
104 .done
= pa_adrian_ec_done
,
108 #define DEFAULT_RATE 32000
109 #define DEFAULT_CHANNELS 1
110 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
111 #define DEFAULT_AGC_ENABLED TRUE
112 #define DEFAULT_DENOISE_ENABLED TRUE
113 #define DEFAULT_ECHO_SUPPRESS_ENABLED TRUE
114 #define DEFAULT_ECHO_SUPPRESS_ATTENUATION 0
115 #define DEFAULT_SAVE_AEC 0
116 #define DEFAULT_AUTOLOADED FALSE
118 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
120 /* This module creates a new (virtual) source and sink.
122 * The data sent to the new sink is kept in a memblockq before being
123 * forwarded to the real sink_master.
125 * Data read from source_master is matched against the saved sink data and
126 * echo canceled data is then pushed onto the new source.
128 * Both source and sink masters have their own threads to push/pull data
129 * respectively. We however perform all our actions in the source IO thread.
130 * To do this we send all played samples to the source IO thread where they
131 * are then pushed into the memblockq.
133 * Alignment is performed in two steps:
135 * 1) when something happens that requires quick adjustement of the alignment of
136 * capture and playback samples, we perform a resync. This adjusts the
137 * position in the playback memblock to the requested sample. Quick
138 * adjustements include moving the playback samples before the capture
139 * samples (because else the echo canceler does not work) or when the
140 * playback pointer drifts too far away.
142 * 2) periodically check the difference between capture and playback. we use a
143 * low and high watermark for adjusting the alignment. playback should always
144 * be before capture and the difference should not be bigger than one frame
145 * size. We would ideally like to resample the sink_input but most driver
146 * don't give enough accuracy to be able to do that right now.
151 pa_usec_t sink_latency
;
153 int64_t send_counter
;
155 pa_usec_t source_now
;
156 pa_usec_t source_latency
;
158 int64_t recv_counter
;
167 pa_bool_t autoloaded
;
170 pa_echo_canceller
*ec
;
173 pa_bool_t need_realign
;
175 /* to wakeup the source I/O thread */
177 pa_asyncmsgq
*asyncmsgq
;
178 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
181 pa_bool_t source_auto_desc
;
182 pa_source_output
*source_output
;
183 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
187 pa_bool_t sink_auto_desc
;
188 pa_sink_input
*sink_input
;
189 pa_memblockq
*sink_memblockq
;
190 int64_t send_counter
; /* updated in sink IO thread */
191 int64_t recv_counter
;
194 pa_atomic_t request_resync
;
197 pa_time_event
*time_event
;
198 pa_usec_t adjust_time
;
205 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
207 static const char* const valid_modargs
[] = {
224 "echo_suppress_attenuation",
225 "echo_suppress_attenuation_active",
232 SOURCE_OUTPUT_MESSAGE_POST
= PA_SOURCE_OUTPUT_MESSAGE_MAX
,
233 SOURCE_OUTPUT_MESSAGE_REWIND
,
234 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
,
235 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
239 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
242 static int64_t calc_diff(struct userdata
*u
, struct snapshot
*snapshot
) {
243 int64_t buffer
, diff_time
, buffer_latency
;
245 /* get the number of samples between capture and playback */
246 if (snapshot
->plen
> snapshot
->rlen
)
247 buffer
= snapshot
->plen
- snapshot
->rlen
;
251 buffer
+= snapshot
->source_delay
+ snapshot
->sink_delay
;
253 /* add the amount of samples not yet transfered to the source context */
254 if (snapshot
->recv_counter
<= snapshot
->send_counter
)
255 buffer
+= (int64_t) (snapshot
->send_counter
- snapshot
->recv_counter
);
257 buffer
+= PA_CLIP_SUB(buffer
, (int64_t) (snapshot
->recv_counter
- snapshot
->send_counter
));
259 /* convert to time */
260 buffer_latency
= pa_bytes_to_usec(buffer
, &u
->source_output
->sample_spec
);
262 /* capture and playback samples are perfectly aligned when diff_time is 0 */
263 diff_time
= (snapshot
->sink_now
+ snapshot
->sink_latency
- buffer_latency
) -
264 (snapshot
->source_now
- snapshot
->source_latency
);
266 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time
,
267 (long long) snapshot
->sink_latency
,
268 (long long) buffer_latency
, (long long) snapshot
->source_latency
,
269 (long long) snapshot
->source_delay
, (long long) snapshot
->sink_delay
,
270 (long long) (snapshot
->send_counter
- snapshot
->recv_counter
),
271 (long long) (snapshot
->sink_now
- snapshot
->source_now
));
276 /* Called from main context */
277 static void time_callback(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
278 struct userdata
*u
= userdata
;
279 uint32_t old_rate
, base_rate
, new_rate
;
282 struct snapshot latency_snapshot
;
286 pa_assert(u
->time_event
== e
);
287 pa_assert_ctl_context();
289 if (u
->active_mask
!= 3)
292 /* update our snapshots */
293 pa_asyncmsgq_send(u
->source_output
->source
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
294 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
296 /* calculate drift between capture and playback */
297 diff_time
= calc_diff(u
, &latency_snapshot
);
299 /*fs = pa_frame_size(&u->source_output->sample_spec);*/
300 old_rate
= u
->sink_input
->sample_spec
.rate
;
301 base_rate
= u
->source_output
->sample_spec
.rate
;
304 /* recording before playback, we need to adjust quickly. The echo
305 * canceler does not work in this case. */
306 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
307 NULL
, diff_time
, NULL
, NULL
);
308 /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
309 new_rate
= base_rate
;
312 if (diff_time
> 1000) {
313 /* diff too big, quickly adjust */
314 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
315 NULL
, diff_time
, NULL
, NULL
);
318 /* recording behind playback, we need to slowly adjust the rate to match */
319 /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
321 /* assume equal samplerates for now */
322 new_rate
= base_rate
;
325 /* make sure we don't make too big adjustements because that sounds horrible */
326 if (new_rate
> base_rate
* 1.1 || new_rate
< base_rate
* 0.9)
327 new_rate
= base_rate
;
329 if (new_rate
!= old_rate
) {
330 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate
, (unsigned long) new_rate
);
332 pa_sink_input_set_rate(u
->sink_input
, new_rate
);
335 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
338 /* Called from source I/O thread context */
339 static int source_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
340 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
344 case PA_SOURCE_MESSAGE_GET_LATENCY
:
346 /* The source is _put() before the source output is, so let's
347 * make sure we don't access it in that time. Also, the
348 * source output is first shut down, the source second. */
349 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
350 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
)) {
351 *((pa_usec_t
*) data
) = 0;
355 *((pa_usec_t
*) data
) =
357 /* Get the latency of the master source */
358 pa_source_get_latency_within_thread(u
->source_output
->source
) +
359 /* Add the latency internal to our source output on top */
360 pa_bytes_to_usec(pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
), &u
->source_output
->source
->sample_spec
) +
361 /* and the buffering we do on the source */
362 pa_bytes_to_usec(u
->blocksize
, &u
->source_output
->source
->sample_spec
);
368 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
371 /* Called from sink I/O thread context */
372 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
373 struct userdata
*u
= PA_SINK(o
)->userdata
;
377 case PA_SINK_MESSAGE_GET_LATENCY
:
379 /* The sink is _put() before the sink input is, so let's
380 * make sure we don't access it in that time. Also, the
381 * sink input is first shut down, the sink second. */
382 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
383 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
384 *((pa_usec_t
*) data
) = 0;
388 *((pa_usec_t
*) data
) =
390 /* Get the latency of the master sink */
391 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
393 /* Add the latency internal to our sink input on top */
394 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
399 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
403 /* Called from main context */
404 static int source_set_state_cb(pa_source
*s
, pa_source_state_t state
) {
407 pa_source_assert_ref(s
);
408 pa_assert_se(u
= s
->userdata
);
410 if (!PA_SOURCE_IS_LINKED(state
) ||
411 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
414 pa_log_debug("Source state %d %d", state
, u
->active_mask
);
416 if (state
== PA_SOURCE_RUNNING
) {
417 /* restart timer when both sink and source are active */
419 if (u
->active_mask
== 3)
420 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
422 pa_atomic_store(&u
->request_resync
, 1);
423 pa_source_output_cork(u
->source_output
, FALSE
);
424 } else if (state
== PA_SOURCE_SUSPENDED
) {
425 u
->active_mask
&= ~1;
426 pa_source_output_cork(u
->source_output
, TRUE
);
431 /* Called from main context */
432 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
435 pa_sink_assert_ref(s
);
436 pa_assert_se(u
= s
->userdata
);
438 if (!PA_SINK_IS_LINKED(state
) ||
439 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
442 pa_log_debug("Sink state %d %d", state
, u
->active_mask
);
444 if (state
== PA_SINK_RUNNING
) {
445 /* restart timer when both sink and source are active */
447 if (u
->active_mask
== 3)
448 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
450 pa_atomic_store(&u
->request_resync
, 1);
451 pa_sink_input_cork(u
->sink_input
, FALSE
);
452 } else if (state
== PA_SINK_SUSPENDED
) {
453 u
->active_mask
&= ~2;
454 pa_sink_input_cork(u
->sink_input
, TRUE
);
459 /* Called from I/O thread context */
460 static void source_update_requested_latency_cb(pa_source
*s
) {
463 pa_source_assert_ref(s
);
464 pa_assert_se(u
= s
->userdata
);
466 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
467 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
))
470 pa_log_debug("Source update requested latency");
472 /* Just hand this one over to the master source */
473 pa_source_output_set_requested_latency_within_thread(
475 pa_source_get_requested_latency_within_thread(s
));
478 /* Called from I/O thread context */
479 static void sink_update_requested_latency_cb(pa_sink
*s
) {
482 pa_sink_assert_ref(s
);
483 pa_assert_se(u
= s
->userdata
);
485 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
486 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
489 pa_log_debug("Sink update requested latency");
491 /* Just hand this one over to the master sink */
492 pa_sink_input_set_requested_latency_within_thread(
494 pa_sink_get_requested_latency_within_thread(s
));
497 /* Called from I/O thread context */
498 static void sink_request_rewind_cb(pa_sink
*s
) {
501 pa_sink_assert_ref(s
);
502 pa_assert_se(u
= s
->userdata
);
504 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
505 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
508 pa_log_debug("Sink request rewind %lld", (long long) s
->thread_info
.rewind_nbytes
);
510 /* Just hand this one over to the master sink */
511 pa_sink_input_request_rewind(u
->sink_input
,
512 s
->thread_info
.rewind_nbytes
, TRUE
, FALSE
, FALSE
);
515 /* Called from main context */
516 static void source_set_volume_cb(pa_source
*s
) {
519 pa_source_assert_ref(s
);
520 pa_assert_se(u
= s
->userdata
);
522 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
523 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
526 pa_source_output_set_volume(u
->source_output
, &s
->real_volume
, s
->save_volume
, TRUE
);
529 /* Called from main context */
530 static void sink_set_volume_cb(pa_sink
*s
) {
533 pa_sink_assert_ref(s
);
534 pa_assert_se(u
= s
->userdata
);
536 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
537 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
540 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
543 static void source_get_volume_cb(pa_source
*s
) {
547 pa_source_assert_ref(s
);
548 pa_assert_se(u
= s
->userdata
);
550 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
551 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
554 pa_source_output_get_volume(u
->source_output
, &v
, TRUE
);
556 if (pa_cvolume_equal(&s
->real_volume
, &v
))
561 pa_source_set_soft_volume(s
, NULL
);
564 /* Called from main context */
565 static void source_set_mute_cb(pa_source
*s
) {
568 pa_source_assert_ref(s
);
569 pa_assert_se(u
= s
->userdata
);
571 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
572 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
575 pa_source_output_set_mute(u
->source_output
, s
->muted
, s
->save_muted
);
578 /* Called from main context */
579 static void sink_set_mute_cb(pa_sink
*s
) {
582 pa_sink_assert_ref(s
);
583 pa_assert_se(u
= s
->userdata
);
585 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
586 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
589 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
592 /* Called from main context */
593 static void source_get_mute_cb(pa_source
*s
) {
596 pa_source_assert_ref(s
);
597 pa_assert_se(u
= s
->userdata
);
599 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
600 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
603 pa_source_output_get_mute(u
->source_output
);
606 /* must be called from the input thread context */
607 static void apply_diff_time(struct userdata
*u
, int64_t diff_time
) {
611 diff
= pa_usec_to_bytes(-diff_time
, &u
->source_output
->sample_spec
);
614 /* add some extra safety samples to compensate for jitter in the
616 diff
+= 10 * pa_frame_size (&u
->source_output
->sample_spec
);
618 pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time
, (long long) diff
);
623 } else if (diff_time
> 0) {
624 diff
= pa_usec_to_bytes(diff_time
, &u
->source_output
->sample_spec
);
627 pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time
, (long long) diff
);
629 u
->source_skip
= diff
;
635 /* must be called from the input thread */
636 static void do_resync(struct userdata
*u
) {
638 struct snapshot latency_snapshot
;
640 pa_log("Doing resync");
642 /* update our snapshot */
643 source_output_snapshot_within_thread(u
, &latency_snapshot
);
644 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
646 /* calculate drift between capture and playback */
647 diff_time
= calc_diff(u
, &latency_snapshot
);
649 /* and adjust for the drift */
650 apply_diff_time(u
, diff_time
);
653 /* Called from input thread context */
654 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
658 pa_source_output_assert_ref(o
);
659 pa_source_output_assert_io_context(o
);
660 pa_assert_se(u
= o
->userdata
);
662 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
663 pa_log("push when no link?");
667 /* handle queued messages */
669 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
673 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
677 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
679 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
680 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
682 while (rlen
>= u
->blocksize
) {
683 pa_memchunk rchunk
, pchunk
;
685 /* take fixed block from recorded samples */
686 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
688 if (plen
> u
->blocksize
&& u
->source_skip
== 0) {
689 uint8_t *rdata
, *pdata
, *cdata
;
695 if (u
->sink_skip
> plen
)
698 to_skip
= u
->sink_skip
;
700 pa_memblockq_drop(u
->sink_memblockq
, to_skip
);
703 u
->sink_skip
-= to_skip
;
706 if (plen
> u
->blocksize
&& u
->sink_skip
== 0) {
707 /* take fixed block from played samples */
708 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, u
->blocksize
, &pchunk
);
710 rdata
= pa_memblock_acquire(rchunk
.memblock
);
711 rdata
+= rchunk
.index
;
712 pdata
= pa_memblock_acquire(pchunk
.memblock
);
713 pdata
+= pchunk
.index
;
716 cchunk
.length
= u
->blocksize
;
717 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
718 cdata
= pa_memblock_acquire(cchunk
.memblock
);
721 if (u
->captured_file
)
722 fwrite(rdata
, 1, u
->blocksize
, u
->captured_file
);
724 fwrite(pdata
, 1, u
->blocksize
, u
->played_file
);
727 /* perform echo cancelation */
728 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
730 /* preprecessor is run after AEC. This is not a mistake! */
732 speex_preprocess_run(u
->ec
->pp_state
, (spx_int16_t
*) cdata
);
735 if (u
->canceled_file
)
736 fwrite(cdata
, 1, u
->blocksize
, u
->canceled_file
);
739 pa_memblock_release(cchunk
.memblock
);
740 pa_memblock_release(pchunk
.memblock
);
741 pa_memblock_release(rchunk
.memblock
);
743 /* drop consumed sink samples */
744 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
745 pa_memblock_unref(pchunk
.memblock
);
747 pa_memblock_unref(rchunk
.memblock
);
748 /* the filtered samples now become the samples from our
752 plen
-= u
->blocksize
;
756 /* forward the (echo-canceled) data to the virtual source */
757 pa_source_post(u
->source
, &rchunk
);
758 pa_memblock_unref(rchunk
.memblock
);
760 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
761 rlen
-= u
->blocksize
;
763 if (u
->source_skip
) {
764 if (u
->source_skip
> u
->blocksize
) {
765 u
->source_skip
-= u
->blocksize
;
768 u
->sink_skip
+= (u
->blocksize
- u
->source_skip
);
775 /* Called from I/O thread context */
776 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
779 pa_sink_input_assert_ref(i
);
781 pa_assert_se(u
= i
->userdata
);
783 if (u
->sink
->thread_info
.rewind_requested
)
784 pa_sink_process_rewind(u
->sink
, 0);
786 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
788 if (i
->thread_info
.underrun_for
> 0) {
789 pa_log_debug("Handling end of underrun.");
790 pa_atomic_store(&u
->request_resync
, 1);
793 /* let source thread handle the chunk. pass the sample count as well so that
794 * the source IO thread can update the right variables. */
795 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
796 NULL
, 0, chunk
, NULL
);
797 u
->send_counter
+= chunk
->length
;
802 /* Called from input thread context */
803 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
806 pa_source_output_assert_ref(o
);
807 pa_source_output_assert_io_context(o
);
808 pa_assert_se(u
= o
->userdata
);
810 pa_source_process_rewind(u
->source
, nbytes
);
812 /* go back on read side, we need to use older sink data for this */
813 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
815 /* manipulate write index */
816 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
818 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
819 (long long) pa_memblockq_get_length (u
->source_memblockq
));
822 /* Called from I/O thread context */
823 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
826 pa_sink_input_assert_ref(i
);
827 pa_assert_se(u
= i
->userdata
);
829 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
831 pa_sink_process_rewind(u
->sink
, nbytes
);
833 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
834 u
->send_counter
-= nbytes
;
837 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
838 size_t delay
, rlen
, plen
;
839 pa_usec_t now
, latency
;
841 now
= pa_rtclock_now();
842 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
843 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
845 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
846 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
847 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
849 snapshot
->source_now
= now
;
850 snapshot
->source_latency
= latency
;
851 snapshot
->source_delay
= delay
;
852 snapshot
->recv_counter
= u
->recv_counter
;
853 snapshot
->rlen
= rlen
+ u
->sink_skip
;
854 snapshot
->plen
= plen
+ u
->source_skip
;
858 /* Called from output thread context */
859 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
860 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
864 case SOURCE_OUTPUT_MESSAGE_POST
:
866 pa_source_output_assert_io_context(u
->source_output
);
868 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
869 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
871 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
873 u
->recv_counter
+= (int64_t) chunk
->length
;
877 case SOURCE_OUTPUT_MESSAGE_REWIND
:
878 pa_source_output_assert_io_context(u
->source_output
);
880 /* manipulate write index, never go past what we have */
881 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
882 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
884 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
886 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
888 u
->recv_counter
-= offset
;
892 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
893 struct snapshot
*snapshot
= (struct snapshot
*) data
;
895 source_output_snapshot_within_thread(u
, snapshot
);
899 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
900 apply_diff_time(u
, offset
);
905 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
908 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
909 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
913 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
915 pa_usec_t now
, latency
;
916 struct snapshot
*snapshot
= (struct snapshot
*) data
;
918 pa_sink_input_assert_io_context(u
->sink_input
);
920 now
= pa_rtclock_now();
921 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
922 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
924 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
926 snapshot
->sink_now
= now
;
927 snapshot
->sink_latency
= latency
;
928 snapshot
->sink_delay
= delay
;
929 snapshot
->send_counter
= u
->send_counter
;
934 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
937 /* Called from I/O thread context */
938 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
941 pa_sink_input_assert_ref(i
);
942 pa_assert_se(u
= i
->userdata
);
944 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
946 pa_memblockq_set_maxrewind(u
->sink_memblockq
, nbytes
);
947 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
950 /* Called from I/O thread context */
951 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
954 pa_source_output_assert_ref(o
);
955 pa_assert_se(u
= o
->userdata
);
957 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
959 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
962 /* Called from I/O thread context */
963 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
966 pa_sink_input_assert_ref(i
);
967 pa_assert_se(u
= i
->userdata
);
969 pa_log_debug("Sink input update max request %lld", (long long) nbytes
);
971 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
);
974 /* Called from I/O thread context */
975 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
979 pa_sink_input_assert_ref(i
);
980 pa_assert_se(u
= i
->userdata
);
982 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
984 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
987 /* Called from I/O thread context */
988 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
992 pa_source_output_assert_ref(o
);
993 pa_assert_se(u
= o
->userdata
);
995 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
997 pa_log_debug("source output update requested latency %lld", (long long) latency
);
1000 /* Called from I/O thread context */
1001 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
1004 pa_sink_input_assert_ref(i
);
1005 pa_assert_se(u
= i
->userdata
);
1007 pa_log_debug("Sink input update latency range %lld %lld",
1008 (long long) i
->sink
->thread_info
.min_latency
,
1009 (long long) i
->sink
->thread_info
.max_latency
);
1011 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1014 /* Called from I/O thread context */
1015 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
1018 pa_source_output_assert_ref(o
);
1019 pa_assert_se(u
= o
->userdata
);
1021 pa_log_debug("Source output update latency range %lld %lld",
1022 (long long) o
->source
->thread_info
.min_latency
,
1023 (long long) o
->source
->thread_info
.max_latency
);
1025 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1028 /* Called from I/O thread context */
1029 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
1032 pa_sink_input_assert_ref(i
);
1033 pa_assert_se(u
= i
->userdata
);
1035 pa_log_debug("Sink input update fixed latency %lld",
1036 (long long) i
->sink
->thread_info
.fixed_latency
);
1038 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1041 /* Called from I/O thread context */
1042 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1045 pa_source_output_assert_ref(o
);
1046 pa_assert_se(u
= o
->userdata
);
1048 pa_log_debug("Source output update fixed latency %lld",
1049 (long long) o
->source
->thread_info
.fixed_latency
);
1051 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1054 /* Called from output thread context */
1055 static void source_output_attach_cb(pa_source_output
*o
) {
1058 pa_source_output_assert_ref(o
);
1059 pa_source_output_assert_io_context(o
);
1060 pa_assert_se(u
= o
->userdata
);
1062 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1063 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1064 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1065 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1067 pa_log_debug("Source output %p attach", o
);
1069 pa_source_attach_within_thread(u
->source
);
1071 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1072 o
->source
->thread_info
.rtpoll
,
1077 /* Called from I/O thread context */
1078 static void sink_input_attach_cb(pa_sink_input
*i
) {
1081 pa_sink_input_assert_ref(i
);
1082 pa_assert_se(u
= i
->userdata
);
1084 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1085 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1087 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1088 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1089 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1091 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1092 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1094 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1095 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1097 pa_log_debug("Sink input %p attach", i
);
1099 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1100 i
->sink
->thread_info
.rtpoll
,
1104 pa_sink_attach_within_thread(u
->sink
);
1108 /* Called from output thread context */
1109 static void source_output_detach_cb(pa_source_output
*o
) {
1112 pa_source_output_assert_ref(o
);
1113 pa_source_output_assert_io_context(o
);
1114 pa_assert_se(u
= o
->userdata
);
1116 pa_source_detach_within_thread(u
->source
);
1117 pa_source_set_rtpoll(u
->source
, NULL
);
1119 pa_log_debug("Source output %p detach", o
);
1121 if (u
->rtpoll_item_read
) {
1122 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1123 u
->rtpoll_item_read
= NULL
;
1127 /* Called from I/O thread context */
1128 static void sink_input_detach_cb(pa_sink_input
*i
) {
1131 pa_sink_input_assert_ref(i
);
1132 pa_assert_se(u
= i
->userdata
);
1134 pa_sink_detach_within_thread(u
->sink
);
1136 pa_sink_set_rtpoll(u
->sink
, NULL
);
1138 pa_log_debug("Sink input %p detach", i
);
1140 if (u
->rtpoll_item_write
) {
1141 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1142 u
->rtpoll_item_write
= NULL
;
1146 /* Called from output thread context */
1147 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1150 pa_source_output_assert_ref(o
);
1151 pa_source_output_assert_io_context(o
);
1152 pa_assert_se(u
= o
->userdata
);
1154 pa_log_debug("Source output %p state %d", o
, state
);
1157 /* Called from IO thread context */
1158 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1161 pa_sink_input_assert_ref(i
);
1162 pa_assert_se(u
= i
->userdata
);
1164 pa_log_debug("Sink input %p state %d", i
, state
);
1166 /* If we are added for the first time, ask for a rewinding so that
1167 * we are heard right-away. */
1168 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1169 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1170 pa_log_debug("Requesting rewind due to state change.");
1171 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1175 /* Called from main thread */
1176 static void source_output_kill_cb(pa_source_output
*o
) {
1179 pa_source_output_assert_ref(o
);
1180 pa_assert_ctl_context();
1181 pa_assert_se(u
= o
->userdata
);
1183 /* The order here matters! We first kill the source output, followed
1184 * by the source. That means the source callbacks must be protected
1185 * against an unconnected source output! */
1186 pa_source_output_unlink(u
->source_output
);
1187 pa_source_unlink(u
->source
);
1189 pa_source_output_unref(u
->source_output
);
1190 u
->source_output
= NULL
;
1192 pa_source_unref(u
->source
);
1195 pa_log_debug("Source output kill %p", o
);
1197 pa_module_unload_request(u
->module
, TRUE
);
1200 /* Called from main context */
1201 static void sink_input_kill_cb(pa_sink_input
*i
) {
1204 pa_sink_input_assert_ref(i
);
1205 pa_assert_se(u
= i
->userdata
);
1207 /* The order here matters! We first kill the sink input, followed
1208 * by the sink. That means the sink callbacks must be protected
1209 * against an unconnected sink input! */
1210 pa_sink_input_unlink(u
->sink_input
);
1211 pa_sink_unlink(u
->sink
);
1213 pa_sink_input_unref(u
->sink_input
);
1214 u
->sink_input
= NULL
;
1216 pa_sink_unref(u
->sink
);
1219 pa_log_debug("Sink input kill %p", i
);
1221 pa_module_unload_request(u
->module
, TRUE
);
1224 /* Called from main thread */
1225 static pa_bool_t
source_output_may_move_to_cb(pa_source_output
*o
, pa_source
*dest
) {
1228 pa_source_output_assert_ref(o
);
1229 pa_assert_ctl_context();
1230 pa_assert_se(u
= o
->userdata
);
1232 return (u
->source
!= dest
) && (u
->sink
!= dest
->monitor_of
);
1235 /* Called from main context */
1236 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1239 pa_sink_input_assert_ref(i
);
1240 pa_assert_se(u
= i
->userdata
);
1242 return u
->sink
!= dest
;
1245 /* Called from main thread */
1246 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1249 pa_source_output_assert_ref(o
);
1250 pa_assert_ctl_context();
1251 pa_assert_se(u
= o
->userdata
);
1254 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1255 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1257 pa_source_set_asyncmsgq(u
->source
, NULL
);
1259 if (u
->source_auto_desc
&& dest
) {
1263 pl
= pa_proplist_new();
1264 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1265 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1266 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1268 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1269 pa_proplist_free(pl
);
1273 /* Called from main context */
1274 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1277 pa_sink_input_assert_ref(i
);
1278 pa_assert_se(u
= i
->userdata
);
1281 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1282 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1284 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1286 if (u
->sink_auto_desc
&& dest
) {
1290 pl
= pa_proplist_new();
1291 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1292 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1293 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1295 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1296 pa_proplist_free(pl
);
1300 /* Called from main context */
1301 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1304 pa_sink_input_assert_ref(i
);
1305 pa_assert_se(u
= i
->userdata
);
1307 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1310 /* Called from main context */
1311 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1314 pa_sink_input_assert_ref(i
);
1315 pa_assert_se(u
= i
->userdata
);
1317 pa_sink_mute_changed(u
->sink
, i
->muted
);
1320 static pa_echo_canceller_method_t
get_ec_method_from_string(const char *method
) {
1321 if (strcmp(method
, "speex") == 0)
1322 return PA_ECHO_CANCELLER_SPEEX
;
1323 else if (strcmp(method
, "adrian") == 0)
1324 return PA_ECHO_CANCELLER_ADRIAN
;
1326 return PA_ECHO_CANCELLER_INVALID
;
1329 int pa__init(pa_module
*m
) {
1331 pa_sample_spec source_ss
, sink_ss
;
1332 pa_channel_map source_map
, sink_map
;
1334 pa_source
*source_master
=NULL
;
1335 pa_sink
*sink_master
=NULL
;
1336 pa_source_output_new_data source_output_data
;
1337 pa_sink_input_new_data sink_input_data
;
1338 pa_source_new_data source_data
;
1339 pa_sink_new_data sink_data
;
1340 pa_memchunk silence
;
1341 pa_echo_canceller_method_t ec_method
;
1342 uint32_t adjust_time_sec
;
1346 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1347 pa_log("Failed to parse module arguments.");
1351 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1352 pa_log("Master source not found");
1355 pa_assert(source_master
);
1357 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1358 pa_log("Master sink not found");
1361 pa_assert(sink_master
);
1363 source_ss
= source_master
->sample_spec
;
1364 source_ss
.rate
= DEFAULT_RATE
;
1365 source_ss
.channels
= DEFAULT_CHANNELS
;
1366 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1367 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &source_ss
, &source_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1368 pa_log("Invalid sample format specification or channel map");
1372 sink_ss
= sink_master
->sample_spec
;
1373 sink_map
= sink_master
->channel_map
;
1375 u
= pa_xnew0(struct userdata
, 1);
1377 pa_log("Failed to alloc userdata");
1384 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1386 pa_log("Failed to alloc echo canceller");
1390 if ((ec_method
= get_ec_method_from_string(pa_modargs_get_value(ma
, "aec_method", DEFAULT_ECHO_CANCELLER
))) < 0) {
1391 pa_log("Invalid echo canceller implementation");
1395 u
->ec
->init
= ec_table
[ec_method
].init
;
1396 u
->ec
->run
= ec_table
[ec_method
].run
;
1397 u
->ec
->done
= ec_table
[ec_method
].done
;
1399 adjust_time_sec
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1400 if (pa_modargs_get_value_u32(ma
, "adjust_time", &adjust_time_sec
) < 0) {
1401 pa_log("Failed to parse adjust_time value");
1405 if (adjust_time_sec
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1406 u
->adjust_time
= adjust_time_sec
* PA_USEC_PER_SEC
;
1408 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1410 u
->ec
->agc
= DEFAULT_AGC_ENABLED
;
1411 if (pa_modargs_get_value_boolean(ma
, "agc", &u
->ec
->agc
) < 0) {
1412 pa_log("Failed to parse agc value");
1416 u
->ec
->denoise
= DEFAULT_DENOISE_ENABLED
;
1417 if (pa_modargs_get_value_boolean(ma
, "denoise", &u
->ec
->denoise
) < 0) {
1418 pa_log("Failed to parse denoise value");
1422 u
->ec
->echo_suppress
= DEFAULT_ECHO_SUPPRESS_ENABLED
;
1423 if (pa_modargs_get_value_boolean(ma
, "echo_suppress", &u
->ec
->echo_suppress
) < 0) {
1424 pa_log("Failed to parse echo_suppress value");
1427 if (u
->ec
->echo_suppress
&& ec_method
!= PA_ECHO_CANCELLER_SPEEX
) {
1428 pa_log("Echo suppression is only useful with the speex canceller");
1432 u
->ec
->echo_suppress_attenuation
= DEFAULT_ECHO_SUPPRESS_ATTENUATION
;
1433 if (pa_modargs_get_value_s32(ma
, "echo_suppress_attenuation", &u
->ec
->echo_suppress_attenuation
) < 0) {
1434 pa_log("Failed to parse echo_suppress_attenuation value");
1437 if (u
->ec
->echo_suppress_attenuation
> 0) {
1438 pa_log("echo_suppress_attenuation should be a negative dB value");
1442 u
->ec
->echo_suppress_attenuation_active
= DEFAULT_ECHO_SUPPRESS_ATTENUATION
;
1443 if (pa_modargs_get_value_s32(ma
, "echo_suppress_attenuation_active", &u
->ec
->echo_suppress_attenuation_active
) < 0) {
1444 pa_log("Failed to parse echo_supress_attenuation_active value");
1447 if (u
->ec
->echo_suppress_attenuation_active
> 0) {
1448 pa_log("echo_suppress_attenuation_active should be a negative dB value");
1452 u
->save_aec
= DEFAULT_SAVE_AEC
;
1453 if (pa_modargs_get_value_u32(ma
, "save_aec", &u
->save_aec
) < 0) {
1454 pa_log("Failed to parse save_aec value");
1458 u
->autoloaded
= DEFAULT_AUTOLOADED
;
1459 if (pa_modargs_get_value_boolean(ma
, "autoloaded", &u
->autoloaded
) < 0) {
1460 pa_log("Failed to parse autoloaded value");
1464 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1465 u
->need_realign
= TRUE
;
1467 if (!u
->ec
->init(u
->core
, u
->ec
, &source_ss
, &source_map
, &sink_ss
, &sink_map
, &u
->blocksize
, pa_modargs_get_value(ma
, "aec_args", NULL
))) {
1468 pa_log("Failed to init AEC engine");
1473 if (u
->ec
->agc
|| u
->ec
->denoise
|| u
->ec
->echo_suppress
) {
1476 if (source_ss
.channels
!= 1) {
1477 pa_log("AGC, denoising and echo suppression only work with channels=1");
1481 u
->ec
->pp_state
= speex_preprocess_state_init(u
->blocksize
/ pa_frame_size(&source_ss
), source_ss
.rate
);
1484 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_AGC
, &tmp
);
1485 tmp
= u
->ec
->denoise
;
1486 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_DENOISE
, &tmp
);
1487 if (u
->ec
->echo_suppress
) {
1488 if (u
->ec
->echo_suppress_attenuation
)
1489 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS
, &u
->ec
->echo_suppress_attenuation
);
1490 if (u
->ec
->echo_suppress_attenuation_active
) {
1491 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS_ACTIVE
,
1492 &u
->ec
->echo_suppress_attenuation_active
);
1494 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_STATE
, u
->ec
->params
.priv
.speex
.state
);
1499 pa_source_new_data_init(&source_data
);
1500 source_data
.driver
= __FILE__
;
1501 source_data
.module
= m
;
1502 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1503 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1504 pa_source_new_data_set_sample_spec(&source_data
, &source_ss
);
1505 pa_source_new_data_set_channel_map(&source_data
, &source_map
);
1506 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1507 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1509 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1510 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1512 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1513 pa_log("Invalid properties");
1514 pa_source_new_data_done(&source_data
);
1518 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1521 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1522 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1525 u
->source
= pa_source_new(m
->core
, &source_data
,
1526 (source_master
->flags
& (PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
)));
1527 pa_source_new_data_done(&source_data
);
1530 pa_log("Failed to create source.");
1534 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1535 u
->source
->set_state
= source_set_state_cb
;
1536 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1537 pa_source_enable_decibel_volume(u
->source
, TRUE
);
1538 pa_source_set_get_volume_callback(u
->source
, source_get_volume_cb
);
1539 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1540 pa_source_set_get_mute_callback(u
->source
, source_get_mute_cb
);
1541 pa_source_set_set_mute_callback(u
->source
, source_set_mute_cb
);
1542 u
->source
->userdata
= u
;
1544 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1547 pa_sink_new_data_init(&sink_data
);
1548 sink_data
.driver
= __FILE__
;
1549 sink_data
.module
= m
;
1550 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1551 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1552 pa_sink_new_data_set_sample_spec(&sink_data
, &sink_ss
);
1553 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
1554 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1555 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1557 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1558 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1560 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1561 pa_log("Invalid properties");
1562 pa_sink_new_data_done(&sink_data
);
1566 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1569 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1570 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1573 u
->sink
= pa_sink_new(m
->core
, &sink_data
,
1574 (sink_master
->flags
& (PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
)));
1575 pa_sink_new_data_done(&sink_data
);
1578 pa_log("Failed to create sink.");
1582 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1583 u
->sink
->set_state
= sink_set_state_cb
;
1584 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1585 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1586 pa_sink_enable_decibel_volume(u
->sink
, TRUE
);
1587 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1588 pa_sink_set_set_mute_callback(u
->sink
, sink_set_mute_cb
);
1589 u
->sink
->userdata
= u
;
1591 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1593 /* Create source output */
1594 pa_source_output_new_data_init(&source_output_data
);
1595 source_output_data
.driver
= __FILE__
;
1596 source_output_data
.module
= m
;
1597 pa_source_output_new_data_set_source(&source_output_data
, source_master
, FALSE
);
1598 source_output_data
.destination_source
= u
->source
;
1600 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1602 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1603 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1604 pa_source_output_new_data_set_sample_spec(&source_output_data
, &source_ss
);
1605 pa_source_output_new_data_set_channel_map(&source_output_data
, &source_map
);
1607 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1608 pa_source_output_new_data_done(&source_output_data
);
1610 if (!u
->source_output
)
1613 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1614 u
->source_output
->push
= source_output_push_cb
;
1615 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1616 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1617 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1618 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1619 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1620 u
->source_output
->kill
= source_output_kill_cb
;
1621 u
->source_output
->attach
= source_output_attach_cb
;
1622 u
->source_output
->detach
= source_output_detach_cb
;
1623 u
->source_output
->state_change
= source_output_state_change_cb
;
1624 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1625 u
->source_output
->moving
= source_output_moving_cb
;
1626 u
->source_output
->userdata
= u
;
1628 u
->source
->output_from_master
= u
->source_output
;
1630 /* Create sink input */
1631 pa_sink_input_new_data_init(&sink_input_data
);
1632 sink_input_data
.driver
= __FILE__
;
1633 sink_input_data
.module
= m
;
1634 pa_sink_input_new_data_set_sink(&sink_input_data
, sink_master
, FALSE
);
1635 sink_input_data
.origin_sink
= u
->sink
;
1636 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1637 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1638 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_ss
);
1639 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_map
);
1640 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1642 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1643 pa_sink_input_new_data_done(&sink_input_data
);
1648 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1649 u
->sink_input
->pop
= sink_input_pop_cb
;
1650 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1651 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1652 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1653 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1654 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1655 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1656 u
->sink_input
->kill
= sink_input_kill_cb
;
1657 u
->sink_input
->attach
= sink_input_attach_cb
;
1658 u
->sink_input
->detach
= sink_input_detach_cb
;
1659 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1660 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1661 u
->sink_input
->moving
= sink_input_moving_cb
;
1662 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1663 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1664 u
->sink_input
->userdata
= u
;
1666 u
->sink
->input_to_master
= u
->sink_input
;
1668 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1670 u
->source_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1671 pa_frame_size(&source_ss
), 1, 1, 0, &silence
);
1672 u
->sink_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1673 pa_frame_size(&sink_ss
), 1, 1, 0, &silence
);
1675 pa_memblock_unref(silence
.memblock
);
1677 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1678 pa_log("Failed to create memblockq.");
1682 /* our source and sink are not suspended when we create them */
1685 if (u
->adjust_time
> 0)
1686 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1689 pa_log("Creating AEC files in /tmp");
1690 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1691 if (u
->captured_file
== NULL
)
1692 perror ("fopen failed");
1693 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1694 if (u
->played_file
== NULL
)
1695 perror ("fopen failed");
1696 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1697 if (u
->canceled_file
== NULL
)
1698 perror ("fopen failed");
1701 pa_sink_put(u
->sink
);
1702 pa_source_put(u
->source
);
1704 pa_sink_input_put(u
->sink_input
);
1705 pa_source_output_put(u
->source_output
);
1707 pa_modargs_free(ma
);
1713 pa_modargs_free(ma
);
1720 int pa__get_n_used(pa_module
*m
) {
1724 pa_assert_se(u
= m
->userdata
);
1726 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1729 void pa__done(pa_module
*m
) {
1734 if (!(u
= m
->userdata
))
1737 /* See comments in source_output_kill_cb() above regarding
1738 * destruction order! */
1741 u
->core
->mainloop
->time_free(u
->time_event
);
1743 if (u
->source_output
)
1744 pa_source_output_unlink(u
->source_output
);
1746 pa_sink_input_unlink(u
->sink_input
);
1749 pa_source_unlink(u
->source
);
1751 pa_sink_unlink(u
->sink
);
1753 if (u
->source_output
)
1754 pa_source_output_unref(u
->source_output
);
1756 pa_sink_input_unref(u
->sink_input
);
1759 pa_source_unref(u
->source
);
1761 pa_sink_unref(u
->sink
);
1763 if (u
->source_memblockq
)
1764 pa_memblockq_free(u
->source_memblockq
);
1765 if (u
->sink_memblockq
)
1766 pa_memblockq_free(u
->sink_memblockq
);
1768 if (u
->ec
->pp_state
)
1769 speex_preprocess_state_destroy(u
->ec
->pp_state
);
1779 pa_asyncmsgq_unref(u
->asyncmsgq
);