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 Cancellation");
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 "adjust_threshold=<how much drift to readjust after in ms> "
70 "format=<sample format> "
72 "channels=<number of channels> "
73 "channel_map=<channel map> "
74 "aec_method=<implementation to use> "
75 "aec_args=<parameters for the AEC engine> "
76 "save_aec=<save AEC data in /tmp> "
77 "autoloaded=<set if this module is being loaded automatically> "
78 "use_volume_sharing=<yes or no> "
81 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
83 PA_ECHO_CANCELLER_INVALID
= -1,
84 PA_ECHO_CANCELLER_SPEEX
= 0,
85 PA_ECHO_CANCELLER_ADRIAN
,
87 PA_ECHO_CANCELLER_WEBRTC
,
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 /* WebRTC's audio processing engine */
109 .init
= pa_webrtc_ec_init
,
110 .run
= pa_webrtc_ec_run
,
111 .done
= pa_webrtc_ec_done
,
116 #define DEFAULT_RATE 32000
117 #define DEFAULT_CHANNELS 1
118 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
119 #define DEFAULT_ADJUST_TOLERANCE (5*PA_USEC_PER_MSEC)
120 #define DEFAULT_SAVE_AEC FALSE
121 #define DEFAULT_AUTOLOADED FALSE
123 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
125 /* Can only be used in main context */
126 #define IS_ACTIVE(u) ((pa_source_get_state((u)->source) == PA_SOURCE_RUNNING) && \
127 (pa_sink_get_state((u)->sink) == PA_SINK_RUNNING))
129 /* This module creates a new (virtual) source and sink.
131 * The data sent to the new sink is kept in a memblockq before being
132 * forwarded to the real sink_master.
134 * Data read from source_master is matched against the saved sink data and
135 * echo canceled data is then pushed onto the new source.
137 * Both source and sink masters have their own threads to push/pull data
138 * respectively. We however perform all our actions in the source IO thread.
139 * To do this we send all played samples to the source IO thread where they
140 * are then pushed into the memblockq.
142 * Alignment is performed in two steps:
144 * 1) when something happens that requires quick adjustment of the alignment of
145 * capture and playback samples, we perform a resync. This adjusts the
146 * position in the playback memblock to the requested sample. Quick
147 * adjustments include moving the playback samples before the capture
148 * samples (because else the echo canceler does not work) or when the
149 * playback pointer drifts too far away.
151 * 2) periodically check the difference between capture and playback. we use a
152 * low and high watermark for adjusting the alignment. playback should always
153 * be before capture and the difference should not be bigger than one frame
154 * size. We would ideally like to resample the sink_input but most driver
155 * don't give enough accuracy to be able to do that right now.
160 pa_usec_t sink_latency
;
162 int64_t send_counter
;
164 pa_usec_t source_now
;
165 pa_usec_t source_latency
;
167 int64_t recv_counter
;
176 pa_bool_t autoloaded
;
180 pa_echo_canceller
*ec
;
183 pa_bool_t need_realign
;
185 /* to wakeup the source I/O thread */
186 pa_asyncmsgq
*asyncmsgq
;
187 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
190 pa_bool_t source_auto_desc
;
191 pa_source_output
*source_output
;
192 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
196 pa_bool_t sink_auto_desc
;
197 pa_sink_input
*sink_input
;
198 pa_memblockq
*sink_memblockq
;
199 int64_t send_counter
; /* updated in sink IO thread */
200 int64_t recv_counter
;
203 pa_atomic_t request_resync
;
205 pa_time_event
*time_event
;
206 pa_usec_t adjust_time
;
207 int adjust_threshold
;
214 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
216 static const char* const valid_modargs
[] = {
233 "use_volume_sharing",
238 SOURCE_OUTPUT_MESSAGE_POST
= PA_SOURCE_OUTPUT_MESSAGE_MAX
,
239 SOURCE_OUTPUT_MESSAGE_REWIND
,
240 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
,
241 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
245 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
248 static int64_t calc_diff(struct userdata
*u
, struct snapshot
*snapshot
) {
249 int64_t buffer
, diff_time
, buffer_latency
;
251 /* get the number of samples between capture and playback */
252 if (snapshot
->plen
> snapshot
->rlen
)
253 buffer
= snapshot
->plen
- snapshot
->rlen
;
257 buffer
+= snapshot
->source_delay
+ snapshot
->sink_delay
;
259 /* add the amount of samples not yet transferred to the source context */
260 if (snapshot
->recv_counter
<= snapshot
->send_counter
)
261 buffer
+= (int64_t) (snapshot
->send_counter
- snapshot
->recv_counter
);
263 buffer
+= PA_CLIP_SUB(buffer
, (int64_t) (snapshot
->recv_counter
- snapshot
->send_counter
));
265 /* convert to time */
266 buffer_latency
= pa_bytes_to_usec(buffer
, &u
->source_output
->sample_spec
);
268 /* capture and playback samples are perfectly aligned when diff_time is 0 */
269 diff_time
= (snapshot
->sink_now
+ snapshot
->sink_latency
- buffer_latency
) -
270 (snapshot
->source_now
- snapshot
->source_latency
);
272 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time
,
273 (long long) snapshot
->sink_latency
,
274 (long long) buffer_latency
, (long long) snapshot
->source_latency
,
275 (long long) snapshot
->source_delay
, (long long) snapshot
->sink_delay
,
276 (long long) (snapshot
->send_counter
- snapshot
->recv_counter
),
277 (long long) (snapshot
->sink_now
- snapshot
->source_now
));
282 /* Called from main context */
283 static void time_callback(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
284 struct userdata
*u
= userdata
;
285 uint32_t old_rate
, base_rate
, new_rate
;
288 struct snapshot latency_snapshot
;
292 pa_assert(u
->time_event
== e
);
293 pa_assert_ctl_context();
298 /* update our snapshots */
299 pa_asyncmsgq_send(u
->source_output
->source
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
300 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
302 /* calculate drift between capture and playback */
303 diff_time
= calc_diff(u
, &latency_snapshot
);
305 /*fs = pa_frame_size(&u->source_output->sample_spec);*/
306 old_rate
= u
->sink_input
->sample_spec
.rate
;
307 base_rate
= u
->source_output
->sample_spec
.rate
;
310 /* recording before playback, we need to adjust quickly. The echo
311 * canceler does not work in this case. */
312 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
313 NULL
, diff_time
, NULL
, NULL
);
314 /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
315 new_rate
= base_rate
;
318 if (diff_time
> u
->adjust_threshold
) {
319 /* diff too big, quickly adjust */
320 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
321 NULL
, diff_time
, NULL
, NULL
);
324 /* recording behind playback, we need to slowly adjust the rate to match */
325 /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
327 /* assume equal samplerates for now */
328 new_rate
= base_rate
;
331 /* make sure we don't make too big adjustments because that sounds horrible */
332 if (new_rate
> base_rate
* 1.1 || new_rate
< base_rate
* 0.9)
333 new_rate
= base_rate
;
335 if (new_rate
!= old_rate
) {
336 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate
, (unsigned long) new_rate
);
338 pa_sink_input_set_rate(u
->sink_input
, new_rate
);
341 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
344 /* Called from source I/O thread context */
345 static int source_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
346 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
350 case PA_SOURCE_MESSAGE_GET_LATENCY
:
352 /* The source is _put() before the source output is, so let's
353 * make sure we don't access it in that time. Also, the
354 * source output is first shut down, the source second. */
355 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
356 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
)) {
357 *((pa_usec_t
*) data
) = 0;
361 *((pa_usec_t
*) data
) =
363 /* Get the latency of the master source */
364 pa_source_get_latency_within_thread(u
->source_output
->source
) +
365 /* Add the latency internal to our source output on top */
366 pa_bytes_to_usec(pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
), &u
->source_output
->source
->sample_spec
) +
367 /* and the buffering we do on the source */
368 pa_bytes_to_usec(u
->blocksize
, &u
->source_output
->source
->sample_spec
);
374 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
377 /* Called from sink I/O thread context */
378 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
379 struct userdata
*u
= PA_SINK(o
)->userdata
;
383 case PA_SINK_MESSAGE_GET_LATENCY
:
385 /* The sink is _put() before the sink input is, so let's
386 * make sure we don't access it in that time. Also, the
387 * sink input is first shut down, the sink second. */
388 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
389 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
390 *((pa_usec_t
*) data
) = 0;
394 *((pa_usec_t
*) data
) =
396 /* Get the latency of the master sink */
397 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
399 /* Add the latency internal to our sink input on top */
400 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
405 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
409 /* Called from main context */
410 static int source_set_state_cb(pa_source
*s
, pa_source_state_t state
) {
413 pa_source_assert_ref(s
);
414 pa_assert_se(u
= s
->userdata
);
416 if (!PA_SOURCE_IS_LINKED(state
) ||
417 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
420 if (state
== PA_SOURCE_RUNNING
) {
421 /* restart timer when both sink and source are active */
422 if (IS_ACTIVE(u
) && u
->adjust_time
)
423 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
425 pa_atomic_store(&u
->request_resync
, 1);
426 pa_source_output_cork(u
->source_output
, FALSE
);
427 } else if (state
== PA_SOURCE_SUSPENDED
) {
428 pa_source_output_cork(u
->source_output
, TRUE
);
434 /* Called from main context */
435 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
438 pa_sink_assert_ref(s
);
439 pa_assert_se(u
= s
->userdata
);
441 if (!PA_SINK_IS_LINKED(state
) ||
442 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
445 if (state
== PA_SINK_RUNNING
) {
446 /* restart timer when both sink and source are active */
447 if (IS_ACTIVE(u
) && u
->adjust_time
)
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 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
) {
656 size_t rlen
, plen
, to_skip
;
657 pa_memchunk rchunk
, pchunk
;
659 pa_source_output_assert_ref(o
);
660 pa_source_output_assert_io_context(o
);
661 pa_assert_se(u
= o
->userdata
);
663 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
664 pa_log("push when no link?");
668 if (PA_UNLIKELY(u
->source
->thread_info
.state
!= PA_SOURCE_RUNNING
||
669 u
->sink
->thread_info
.state
!= PA_SINK_RUNNING
)) {
670 pa_source_post(u
->source
, chunk
);
674 /* handle queued messages, do any message sending of our own */
675 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
678 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
680 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
681 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
683 /* Let's not do anything else till we have enough data to process */
684 if (rlen
< u
->blocksize
)
687 /* See if we need to drop samples in order to sync */
688 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
692 /* Okay, skip cancellation for skipped source samples if needed. */
693 if (PA_UNLIKELY(u
->source_skip
)) {
694 /* The slightly tricky bit here is that we drop all but modulo
695 * blocksize bytes and then adjust for that last bit on the sink side.
696 * We do this because the source data is coming at a fixed rate, which
697 * means the only way to try to catch up is drop sink samples and let
698 * the canceller cope up with this. */
699 to_skip
= rlen
>= u
->source_skip
? u
->source_skip
: rlen
;
700 to_skip
-= to_skip
% u
->blocksize
;
703 pa_memblockq_peek_fixed_size(u
->source_memblockq
, to_skip
, &rchunk
);
704 pa_source_post(u
->source
, &rchunk
);
706 pa_memblock_unref(rchunk
.memblock
);
707 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
710 u
->source_skip
-= to_skip
;
713 if (rlen
&& u
->source_skip
% u
->blocksize
) {
714 u
->sink_skip
+= u
->blocksize
- (u
->source_skip
% u
->blocksize
);
715 u
->source_skip
-= (u
->source_skip
% u
->blocksize
);
719 /* And for the sink, these samples have been played back already, so we can
720 * just drop them and get on with it. */
721 if (PA_UNLIKELY(u
->sink_skip
)) {
722 to_skip
= plen
>= u
->sink_skip
? u
->sink_skip
: plen
;
724 pa_memblockq_drop(u
->sink_memblockq
, to_skip
);
727 u
->sink_skip
-= to_skip
;
730 while (rlen
>= u
->blocksize
) {
731 /* take fixed block from recorded samples */
732 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
734 if (plen
> u
->blocksize
) {
735 uint8_t *rdata
, *pdata
, *cdata
;
739 if (plen
> u
->blocksize
) {
740 /* take fixed block from played samples */
741 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, u
->blocksize
, &pchunk
);
743 rdata
= pa_memblock_acquire(rchunk
.memblock
);
744 rdata
+= rchunk
.index
;
745 pdata
= pa_memblock_acquire(pchunk
.memblock
);
746 pdata
+= pchunk
.index
;
749 cchunk
.length
= u
->blocksize
;
750 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
751 cdata
= pa_memblock_acquire(cchunk
.memblock
);
754 if (u
->captured_file
)
755 unused
= fwrite(rdata
, 1, u
->blocksize
, u
->captured_file
);
757 unused
= fwrite(pdata
, 1, u
->blocksize
, u
->played_file
);
760 /* perform echo cancellation */
761 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
764 if (u
->canceled_file
)
765 unused
= fwrite(cdata
, 1, u
->blocksize
, u
->canceled_file
);
768 pa_memblock_release(cchunk
.memblock
);
769 pa_memblock_release(pchunk
.memblock
);
770 pa_memblock_release(rchunk
.memblock
);
772 /* drop consumed sink samples */
773 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
774 pa_memblock_unref(pchunk
.memblock
);
776 pa_memblock_unref(rchunk
.memblock
);
777 /* the filtered samples now become the samples from our
781 plen
-= u
->blocksize
;
785 /* forward the (echo-canceled) data to the virtual source */
786 pa_source_post(u
->source
, &rchunk
);
787 pa_memblock_unref(rchunk
.memblock
);
789 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
790 rlen
-= u
->blocksize
;
794 /* Called from I/O thread context */
795 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
798 pa_sink_input_assert_ref(i
);
800 pa_assert_se(u
= i
->userdata
);
802 if (u
->sink
->thread_info
.rewind_requested
)
803 pa_sink_process_rewind(u
->sink
, 0);
805 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
807 if (i
->thread_info
.underrun_for
> 0) {
808 pa_log_debug("Handling end of underrun.");
809 pa_atomic_store(&u
->request_resync
, 1);
812 /* let source thread handle the chunk. pass the sample count as well so that
813 * the source IO thread can update the right variables. */
814 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
815 NULL
, 0, chunk
, NULL
);
816 u
->send_counter
+= chunk
->length
;
821 /* Called from input thread context */
822 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
825 pa_source_output_assert_ref(o
);
826 pa_source_output_assert_io_context(o
);
827 pa_assert_se(u
= o
->userdata
);
829 pa_source_process_rewind(u
->source
, nbytes
);
831 /* go back on read side, we need to use older sink data for this */
832 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
834 /* manipulate write index */
835 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
837 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
838 (long long) pa_memblockq_get_length (u
->source_memblockq
));
841 /* Called from I/O thread context */
842 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
845 pa_sink_input_assert_ref(i
);
846 pa_assert_se(u
= i
->userdata
);
848 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
850 pa_sink_process_rewind(u
->sink
, nbytes
);
852 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
853 u
->send_counter
-= nbytes
;
856 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
857 size_t delay
, rlen
, plen
;
858 pa_usec_t now
, latency
;
860 now
= pa_rtclock_now();
861 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
862 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
864 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
865 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
866 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
868 snapshot
->source_now
= now
;
869 snapshot
->source_latency
= latency
;
870 snapshot
->source_delay
= delay
;
871 snapshot
->recv_counter
= u
->recv_counter
;
872 snapshot
->rlen
= rlen
+ u
->sink_skip
;
873 snapshot
->plen
= plen
+ u
->source_skip
;
877 /* Called from output thread context */
878 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
879 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
883 case SOURCE_OUTPUT_MESSAGE_POST
:
885 pa_source_output_assert_io_context(u
->source_output
);
887 if (u
->source_output
->source
->thread_info
.state
== PA_SOURCE_RUNNING
)
888 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
890 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
892 u
->recv_counter
+= (int64_t) chunk
->length
;
896 case SOURCE_OUTPUT_MESSAGE_REWIND
:
897 pa_source_output_assert_io_context(u
->source_output
);
899 /* manipulate write index, never go past what we have */
900 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
901 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
903 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
905 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
907 u
->recv_counter
-= offset
;
911 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
912 struct snapshot
*snapshot
= (struct snapshot
*) data
;
914 source_output_snapshot_within_thread(u
, snapshot
);
918 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
919 apply_diff_time(u
, offset
);
924 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
927 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
928 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
932 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
934 pa_usec_t now
, latency
;
935 struct snapshot
*snapshot
= (struct snapshot
*) data
;
937 pa_sink_input_assert_io_context(u
->sink_input
);
939 now
= pa_rtclock_now();
940 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
941 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
943 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
945 snapshot
->sink_now
= now
;
946 snapshot
->sink_latency
= latency
;
947 snapshot
->sink_delay
= delay
;
948 snapshot
->send_counter
= u
->send_counter
;
953 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
956 /* Called from I/O thread context */
957 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
960 pa_sink_input_assert_ref(i
);
961 pa_assert_se(u
= i
->userdata
);
963 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
965 pa_memblockq_set_maxrewind(u
->sink_memblockq
, nbytes
);
966 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
969 /* Called from I/O thread context */
970 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
973 pa_source_output_assert_ref(o
);
974 pa_assert_se(u
= o
->userdata
);
976 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
978 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
981 /* Called from I/O thread context */
982 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
985 pa_sink_input_assert_ref(i
);
986 pa_assert_se(u
= i
->userdata
);
988 pa_log_debug("Sink input update max request %lld", (long long) nbytes
);
990 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
);
993 /* Called from I/O thread context */
994 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
998 pa_sink_input_assert_ref(i
);
999 pa_assert_se(u
= i
->userdata
);
1001 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
1003 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
1006 /* Called from I/O thread context */
1007 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
1011 pa_source_output_assert_ref(o
);
1012 pa_assert_se(u
= o
->userdata
);
1014 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
1016 pa_log_debug("source output update requested latency %lld", (long long) latency
);
1019 /* Called from I/O thread context */
1020 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
1023 pa_sink_input_assert_ref(i
);
1024 pa_assert_se(u
= i
->userdata
);
1026 pa_log_debug("Sink input update latency range %lld %lld",
1027 (long long) i
->sink
->thread_info
.min_latency
,
1028 (long long) i
->sink
->thread_info
.max_latency
);
1030 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1033 /* Called from I/O thread context */
1034 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
1037 pa_source_output_assert_ref(o
);
1038 pa_assert_se(u
= o
->userdata
);
1040 pa_log_debug("Source output update latency range %lld %lld",
1041 (long long) o
->source
->thread_info
.min_latency
,
1042 (long long) o
->source
->thread_info
.max_latency
);
1044 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1047 /* Called from I/O thread context */
1048 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
1051 pa_sink_input_assert_ref(i
);
1052 pa_assert_se(u
= i
->userdata
);
1054 pa_log_debug("Sink input update fixed latency %lld",
1055 (long long) i
->sink
->thread_info
.fixed_latency
);
1057 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1060 /* Called from I/O thread context */
1061 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1064 pa_source_output_assert_ref(o
);
1065 pa_assert_se(u
= o
->userdata
);
1067 pa_log_debug("Source output update fixed latency %lld",
1068 (long long) o
->source
->thread_info
.fixed_latency
);
1070 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1073 /* Called from output thread context */
1074 static void source_output_attach_cb(pa_source_output
*o
) {
1077 pa_source_output_assert_ref(o
);
1078 pa_source_output_assert_io_context(o
);
1079 pa_assert_se(u
= o
->userdata
);
1081 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1082 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1083 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1084 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1086 pa_log_debug("Source output %d attach", o
->index
);
1088 pa_source_attach_within_thread(u
->source
);
1090 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1091 o
->source
->thread_info
.rtpoll
,
1096 /* Called from I/O thread context */
1097 static void sink_input_attach_cb(pa_sink_input
*i
) {
1100 pa_sink_input_assert_ref(i
);
1101 pa_assert_se(u
= i
->userdata
);
1103 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1104 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1106 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1107 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1108 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1110 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1111 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1113 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1114 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1116 pa_log_debug("Sink input %d attach", i
->index
);
1118 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1119 i
->sink
->thread_info
.rtpoll
,
1123 pa_sink_attach_within_thread(u
->sink
);
1127 /* Called from output thread context */
1128 static void source_output_detach_cb(pa_source_output
*o
) {
1131 pa_source_output_assert_ref(o
);
1132 pa_source_output_assert_io_context(o
);
1133 pa_assert_se(u
= o
->userdata
);
1135 pa_source_detach_within_thread(u
->source
);
1136 pa_source_set_rtpoll(u
->source
, NULL
);
1138 pa_log_debug("Source output %d detach", o
->index
);
1140 if (u
->rtpoll_item_read
) {
1141 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1142 u
->rtpoll_item_read
= NULL
;
1146 /* Called from I/O thread context */
1147 static void sink_input_detach_cb(pa_sink_input
*i
) {
1150 pa_sink_input_assert_ref(i
);
1151 pa_assert_se(u
= i
->userdata
);
1153 pa_sink_detach_within_thread(u
->sink
);
1155 pa_sink_set_rtpoll(u
->sink
, NULL
);
1157 pa_log_debug("Sink input %d detach", i
->index
);
1159 if (u
->rtpoll_item_write
) {
1160 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1161 u
->rtpoll_item_write
= NULL
;
1165 /* Called from output thread context */
1166 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1169 pa_source_output_assert_ref(o
);
1170 pa_source_output_assert_io_context(o
);
1171 pa_assert_se(u
= o
->userdata
);
1173 pa_log_debug("Source output %d state %d", o
->index
, state
);
1176 /* Called from IO thread context */
1177 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1180 pa_sink_input_assert_ref(i
);
1181 pa_assert_se(u
= i
->userdata
);
1183 pa_log_debug("Sink input %d state %d", i
->index
, state
);
1185 /* If we are added for the first time, ask for a rewinding so that
1186 * we are heard right-away. */
1187 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1188 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1189 pa_log_debug("Requesting rewind due to state change.");
1190 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1194 /* Called from main thread */
1195 static void source_output_kill_cb(pa_source_output
*o
) {
1198 pa_source_output_assert_ref(o
);
1199 pa_assert_ctl_context();
1200 pa_assert_se(u
= o
->userdata
);
1204 /* The order here matters! We first kill the source output, followed
1205 * by the source. That means the source callbacks must be protected
1206 * against an unconnected source output! */
1207 pa_source_output_unlink(u
->source_output
);
1208 pa_source_unlink(u
->source
);
1210 pa_source_output_unref(u
->source_output
);
1211 u
->source_output
= NULL
;
1213 pa_source_unref(u
->source
);
1216 pa_log_debug("Source output kill %d", o
->index
);
1218 pa_module_unload_request(u
->module
, TRUE
);
1221 /* Called from main context */
1222 static void sink_input_kill_cb(pa_sink_input
*i
) {
1225 pa_sink_input_assert_ref(i
);
1226 pa_assert_se(u
= i
->userdata
);
1230 /* The order here matters! We first kill the sink input, followed
1231 * by the sink. That means the sink callbacks must be protected
1232 * against an unconnected sink input! */
1233 pa_sink_input_unlink(u
->sink_input
);
1234 pa_sink_unlink(u
->sink
);
1236 pa_sink_input_unref(u
->sink_input
);
1237 u
->sink_input
= NULL
;
1239 pa_sink_unref(u
->sink
);
1242 pa_log_debug("Sink input kill %d", i
->index
);
1244 pa_module_unload_request(u
->module
, TRUE
);
1247 /* Called from main thread */
1248 static pa_bool_t
source_output_may_move_to_cb(pa_source_output
*o
, pa_source
*dest
) {
1251 pa_source_output_assert_ref(o
);
1252 pa_assert_ctl_context();
1253 pa_assert_se(u
= o
->userdata
);
1258 return (u
->source
!= dest
) && (u
->sink
!= dest
->monitor_of
);
1261 /* Called from main context */
1262 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1265 pa_sink_input_assert_ref(i
);
1266 pa_assert_se(u
= i
->userdata
);
1271 return u
->sink
!= dest
;
1274 /* Called from main thread */
1275 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1278 pa_source_output_assert_ref(o
);
1279 pa_assert_ctl_context();
1280 pa_assert_se(u
= o
->userdata
);
1283 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1284 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1286 pa_source_set_asyncmsgq(u
->source
, NULL
);
1288 if (u
->source_auto_desc
&& dest
) {
1292 pl
= pa_proplist_new();
1293 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1294 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1295 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1297 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1298 pa_proplist_free(pl
);
1302 /* Called from main context */
1303 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1306 pa_sink_input_assert_ref(i
);
1307 pa_assert_se(u
= i
->userdata
);
1310 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1311 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1313 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1315 if (u
->sink_auto_desc
&& dest
) {
1319 pl
= pa_proplist_new();
1320 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1321 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1322 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1324 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1325 pa_proplist_free(pl
);
1329 /* Called from main context */
1330 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1333 pa_sink_input_assert_ref(i
);
1334 pa_assert_se(u
= i
->userdata
);
1336 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1339 /* Called from main context */
1340 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1343 pa_sink_input_assert_ref(i
);
1344 pa_assert_se(u
= i
->userdata
);
1346 pa_sink_mute_changed(u
->sink
, i
->muted
);
1349 static pa_echo_canceller_method_t
get_ec_method_from_string(const char *method
) {
1350 if (pa_streq(method
, "speex"))
1351 return PA_ECHO_CANCELLER_SPEEX
;
1352 else if (pa_streq(method
, "adrian"))
1353 return PA_ECHO_CANCELLER_ADRIAN
;
1355 else if (pa_streq(method
, "webrtc"))
1356 return PA_ECHO_CANCELLER_WEBRTC
;
1359 return PA_ECHO_CANCELLER_INVALID
;
1362 /* Common initialisation bits between module-echo-cancel and the standalone test program */
1363 static int init_common(pa_modargs
*ma
, struct userdata
*u
, pa_sample_spec
*source_ss
, pa_channel_map
*source_map
) {
1364 pa_echo_canceller_method_t ec_method
;
1366 if (pa_modargs_get_sample_spec_and_channel_map(ma
, source_ss
, source_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1367 pa_log("Invalid sample format specification or channel map");
1371 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1373 pa_log("Failed to alloc echo canceller");
1377 if ((ec_method
= get_ec_method_from_string(pa_modargs_get_value(ma
, "aec_method", DEFAULT_ECHO_CANCELLER
))) < 0) {
1378 pa_log("Invalid echo canceller implementation");
1382 u
->ec
->init
= ec_table
[ec_method
].init
;
1383 u
->ec
->run
= ec_table
[ec_method
].run
;
1384 u
->ec
->done
= ec_table
[ec_method
].done
;
1393 int pa__init(pa_module
*m
) {
1395 pa_sample_spec source_ss
, sink_ss
;
1396 pa_channel_map source_map
, sink_map
;
1398 pa_source
*source_master
=NULL
;
1399 pa_sink
*sink_master
=NULL
;
1400 pa_source_output_new_data source_output_data
;
1401 pa_sink_input_new_data sink_input_data
;
1402 pa_source_new_data source_data
;
1403 pa_sink_new_data sink_data
;
1404 pa_memchunk silence
;
1406 pa_bool_t use_volume_sharing
= TRUE
;
1410 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1411 pa_log("Failed to parse module arguments.");
1415 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1416 pa_log("Master source not found");
1419 pa_assert(source_master
);
1421 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1422 pa_log("Master sink not found");
1425 pa_assert(sink_master
);
1427 if (source_master
->monitor_of
== sink_master
) {
1428 pa_log("Can't cancel echo between a sink and its monitor");
1432 source_ss
= source_master
->sample_spec
;
1433 source_ss
.rate
= DEFAULT_RATE
;
1434 source_ss
.channels
= DEFAULT_CHANNELS
;
1435 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1437 sink_ss
= sink_master
->sample_spec
;
1438 sink_map
= sink_master
->channel_map
;
1440 if (pa_modargs_get_value_boolean(ma
, "use_volume_sharing", &use_volume_sharing
) < 0) {
1441 pa_log("use_volume_sharing= expects a boolean argument");
1445 u
= pa_xnew0(struct userdata
, 1);
1447 pa_log("Failed to alloc userdata");
1455 temp
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1456 if (pa_modargs_get_value_u32(ma
, "adjust_time", &temp
) < 0) {
1457 pa_log("Failed to parse adjust_time value");
1461 if (temp
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1462 u
->adjust_time
= temp
* PA_USEC_PER_SEC
;
1464 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1466 temp
= DEFAULT_ADJUST_TOLERANCE
/ PA_USEC_PER_MSEC
;
1467 if (pa_modargs_get_value_u32(ma
, "adjust_threshold", &temp
) < 0) {
1468 pa_log("Failed to parse adjust_threshold value");
1472 if (temp
!= DEFAULT_ADJUST_TOLERANCE
/ PA_USEC_PER_MSEC
)
1473 u
->adjust_threshold
= temp
* PA_USEC_PER_MSEC
;
1475 u
->adjust_threshold
= DEFAULT_ADJUST_TOLERANCE
;
1477 u
->save_aec
= DEFAULT_SAVE_AEC
;
1478 if (pa_modargs_get_value_boolean(ma
, "save_aec", &u
->save_aec
) < 0) {
1479 pa_log("Failed to parse save_aec value");
1483 u
->autoloaded
= DEFAULT_AUTOLOADED
;
1484 if (pa_modargs_get_value_boolean(ma
, "autoloaded", &u
->autoloaded
) < 0) {
1485 pa_log("Failed to parse autoloaded value");
1489 if (init_common(ma
, u
, &source_ss
, &source_map
))
1492 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1493 u
->need_realign
= TRUE
;
1496 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
))) {
1497 pa_log("Failed to init AEC engine");
1503 pa_source_new_data_init(&source_data
);
1504 source_data
.driver
= __FILE__
;
1505 source_data
.module
= m
;
1506 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1507 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1508 pa_source_new_data_set_sample_spec(&source_data
, &source_ss
);
1509 pa_source_new_data_set_channel_map(&source_data
, &source_map
);
1510 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1511 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1513 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1514 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1516 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1517 pa_log("Invalid properties");
1518 pa_source_new_data_done(&source_data
);
1522 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1525 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1526 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1529 u
->source
= pa_source_new(m
->core
, &source_data
, (source_master
->flags
& (PA_SOURCE_LATENCY
| PA_SOURCE_DYNAMIC_LATENCY
))
1530 | (use_volume_sharing
? PA_SOURCE_SHARE_VOLUME_WITH_MASTER
: 0));
1531 pa_source_new_data_done(&source_data
);
1534 pa_log("Failed to create source.");
1538 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1539 u
->source
->set_state
= source_set_state_cb
;
1540 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1541 pa_source_set_get_mute_callback(u
->source
, source_get_mute_cb
);
1542 pa_source_set_set_mute_callback(u
->source
, source_set_mute_cb
);
1543 if (!use_volume_sharing
) {
1544 pa_source_set_get_volume_callback(u
->source
, source_get_volume_cb
);
1545 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1546 pa_source_enable_decibel_volume(u
->source
, TRUE
);
1548 u
->source
->userdata
= u
;
1550 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1553 pa_sink_new_data_init(&sink_data
);
1554 sink_data
.driver
= __FILE__
;
1555 sink_data
.module
= m
;
1556 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1557 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1558 pa_sink_new_data_set_sample_spec(&sink_data
, &sink_ss
);
1559 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
1560 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1561 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1563 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1564 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1566 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1567 pa_log("Invalid properties");
1568 pa_sink_new_data_done(&sink_data
);
1572 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1575 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1576 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1579 u
->sink
= pa_sink_new(m
->core
, &sink_data
, (sink_master
->flags
& (PA_SINK_LATENCY
| PA_SINK_DYNAMIC_LATENCY
))
1580 | (use_volume_sharing
? PA_SINK_SHARE_VOLUME_WITH_MASTER
: 0));
1581 pa_sink_new_data_done(&sink_data
);
1584 pa_log("Failed to create sink.");
1588 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1589 u
->sink
->set_state
= sink_set_state_cb
;
1590 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1591 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1592 pa_sink_set_set_mute_callback(u
->sink
, sink_set_mute_cb
);
1593 if (!use_volume_sharing
) {
1594 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1595 pa_sink_enable_decibel_volume(u
->sink
, TRUE
);
1597 u
->sink
->userdata
= u
;
1599 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1601 /* Create source output */
1602 pa_source_output_new_data_init(&source_output_data
);
1603 source_output_data
.driver
= __FILE__
;
1604 source_output_data
.module
= m
;
1605 pa_source_output_new_data_set_source(&source_output_data
, source_master
, FALSE
);
1606 source_output_data
.destination_source
= u
->source
;
1608 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1610 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1611 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1612 pa_source_output_new_data_set_sample_spec(&source_output_data
, &source_ss
);
1613 pa_source_output_new_data_set_channel_map(&source_output_data
, &source_map
);
1615 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1616 pa_source_output_new_data_done(&source_output_data
);
1618 if (!u
->source_output
)
1621 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1622 u
->source_output
->push
= source_output_push_cb
;
1623 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1624 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1625 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1626 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1627 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1628 u
->source_output
->kill
= source_output_kill_cb
;
1629 u
->source_output
->attach
= source_output_attach_cb
;
1630 u
->source_output
->detach
= source_output_detach_cb
;
1631 u
->source_output
->state_change
= source_output_state_change_cb
;
1632 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1633 u
->source_output
->moving
= source_output_moving_cb
;
1634 u
->source_output
->userdata
= u
;
1636 u
->source
->output_from_master
= u
->source_output
;
1638 /* Create sink input */
1639 pa_sink_input_new_data_init(&sink_input_data
);
1640 sink_input_data
.driver
= __FILE__
;
1641 sink_input_data
.module
= m
;
1642 pa_sink_input_new_data_set_sink(&sink_input_data
, sink_master
, FALSE
);
1643 sink_input_data
.origin_sink
= u
->sink
;
1644 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1645 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1646 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_ss
);
1647 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_map
);
1648 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1650 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1651 pa_sink_input_new_data_done(&sink_input_data
);
1656 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1657 u
->sink_input
->pop
= sink_input_pop_cb
;
1658 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1659 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1660 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1661 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1662 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1663 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1664 u
->sink_input
->kill
= sink_input_kill_cb
;
1665 u
->sink_input
->attach
= sink_input_attach_cb
;
1666 u
->sink_input
->detach
= sink_input_detach_cb
;
1667 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1668 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1669 u
->sink_input
->moving
= sink_input_moving_cb
;
1670 if (!use_volume_sharing
)
1671 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1672 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1673 u
->sink_input
->userdata
= u
;
1675 u
->sink
->input_to_master
= u
->sink_input
;
1677 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1679 u
->source_memblockq
= pa_memblockq_new("module-echo-cancel source_memblockq", 0, MEMBLOCKQ_MAXLENGTH
, 0,
1680 &source_ss
, 1, 1, 0, &silence
);
1681 u
->sink_memblockq
= pa_memblockq_new("module-echo-cancel sink_memblockq", 0, MEMBLOCKQ_MAXLENGTH
, 0,
1682 &sink_ss
, 1, 1, 0, &silence
);
1684 pa_memblock_unref(silence
.memblock
);
1686 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1687 pa_log("Failed to create memblockq.");
1691 if (u
->adjust_time
> 0)
1692 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1695 pa_log("Creating AEC files in /tmp");
1696 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1697 if (u
->captured_file
== NULL
)
1698 perror ("fopen failed");
1699 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1700 if (u
->played_file
== NULL
)
1701 perror ("fopen failed");
1702 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1703 if (u
->canceled_file
== NULL
)
1704 perror ("fopen failed");
1707 pa_sink_put(u
->sink
);
1708 pa_source_put(u
->source
);
1710 pa_sink_input_put(u
->sink_input
);
1711 pa_source_output_put(u
->source_output
);
1713 pa_modargs_free(ma
);
1719 pa_modargs_free(ma
);
1726 int pa__get_n_used(pa_module
*m
) {
1730 pa_assert_se(u
= m
->userdata
);
1732 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1735 void pa__done(pa_module
*m
) {
1740 if (!(u
= m
->userdata
))
1745 /* See comments in source_output_kill_cb() above regarding
1746 * destruction order! */
1749 u
->core
->mainloop
->time_free(u
->time_event
);
1751 if (u
->source_output
)
1752 pa_source_output_unlink(u
->source_output
);
1754 pa_sink_input_unlink(u
->sink_input
);
1757 pa_source_unlink(u
->source
);
1759 pa_sink_unlink(u
->sink
);
1761 if (u
->source_output
)
1762 pa_source_output_unref(u
->source_output
);
1764 pa_sink_input_unref(u
->sink_input
);
1767 pa_source_unref(u
->source
);
1769 pa_sink_unref(u
->sink
);
1771 if (u
->source_memblockq
)
1772 pa_memblockq_free(u
->source_memblockq
);
1773 if (u
->sink_memblockq
)
1774 pa_memblockq_free(u
->sink_memblockq
);
1784 pa_asyncmsgq_unref(u
->asyncmsgq
);
1788 fclose(u
->played_file
);
1789 if (u
->captured_file
)
1790 fclose(u
->captured_file
);
1791 if (u
->canceled_file
)
1792 fclose(u
->canceled_file
);
1798 #ifdef ECHO_CANCEL_TEST
1800 * Stand-alone test program for running in the canceller on pre-recorded files.
1802 int main(int argc
, char* argv
[]) {
1804 pa_sample_spec source_ss
, sink_ss
;
1805 pa_channel_map source_map
, sink_map
;
1806 pa_modargs
*ma
= NULL
;
1807 uint8_t *rdata
= NULL
, *pdata
= NULL
, *cdata
= NULL
;
1808 int ret
= 0, unused
;
1810 pa_memzero(&u
, sizeof(u
));
1812 if (argc
< 4 || argc
> 6) {
1816 u
.ec
= pa_xnew0(pa_echo_canceller
, 1);
1818 pa_log("Failed to alloc echo canceller");
1822 u
.captured_file
= fopen(argv
[2], "r");
1823 if (u
.captured_file
== NULL
) {
1824 perror ("fopen failed");
1827 u
.played_file
= fopen(argv
[1], "r");
1828 if (u
.played_file
== NULL
) {
1829 perror ("fopen failed");
1832 u
.canceled_file
= fopen(argv
[3], "wb");
1833 if (u
.canceled_file
== NULL
) {
1834 perror ("fopen failed");
1838 u
.core
= pa_xnew0(pa_core
, 1);
1839 u
.core
->cpu_info
.cpu_type
= PA_CPU_X86
;
1840 u
.core
->cpu_info
.flags
.x86
|= PA_CPU_X86_SSE
;
1842 if (!(ma
= pa_modargs_new(argc
> 4 ? argv
[4] : NULL
, valid_modargs
))) {
1843 pa_log("Failed to parse module arguments.");
1847 source_ss
.format
= PA_SAMPLE_S16LE
;
1848 source_ss
.rate
= DEFAULT_RATE
;
1849 source_ss
.channels
= DEFAULT_CHANNELS
;
1850 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1852 init_common(ma
, &u
, &source_ss
, &source_map
);
1854 if (!u
.ec
->init(u
.core
, u
.ec
, &source_ss
, &source_map
, &sink_ss
, &sink_map
, &u
.blocksize
,
1855 (argc
> 4) ? argv
[5] : NULL
)) {
1856 pa_log("Failed to init AEC engine");
1860 rdata
= pa_xmalloc(u
.blocksize
);
1861 pdata
= pa_xmalloc(u
.blocksize
);
1862 cdata
= pa_xmalloc(u
.blocksize
);
1864 while (fread(rdata
, u
.blocksize
, 1, u
.captured_file
) > 0) {
1865 if (fread(pdata
, u
.blocksize
, 1, u
.played_file
) == 0) {
1866 perror("played file ended before captured file");
1870 u
.ec
->run(u
.ec
, rdata
, pdata
, cdata
);
1872 unused
= fwrite(cdata
, u
.blocksize
, 1, u
.canceled_file
);
1877 fclose(u
.captured_file
);
1878 fclose(u
.played_file
);
1879 fclose(u
.canceled_file
);
1890 pa_modargs_free(ma
);
1895 pa_log("Usage: %s play_file rec_file out_file [module args] [aec_args]",argv
[0]);
1901 #endif /* ECHO_CANCEL_TEST */