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
,
86 } pa_echo_canceller_method_t
;
88 #define DEFAULT_ECHO_CANCELLER "speex"
90 static const pa_echo_canceller ec_table
[] = {
93 .init
= pa_speex_ec_init
,
94 .run
= pa_speex_ec_run
,
95 .done
= pa_speex_ec_done
,
98 /* Adrian Andre's NLMS implementation */
99 .init
= pa_adrian_ec_init
,
100 .run
= pa_adrian_ec_run
,
101 .done
= pa_adrian_ec_done
,
105 #define DEFAULT_RATE 32000
106 #define DEFAULT_CHANNELS 1
107 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
108 #define DEFAULT_ADJUST_TOLERANCE (5*PA_USEC_PER_MSEC)
109 #define DEFAULT_SAVE_AEC FALSE
110 #define DEFAULT_AUTOLOADED FALSE
112 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
114 /* This module creates a new (virtual) source and sink.
116 * The data sent to the new sink is kept in a memblockq before being
117 * forwarded to the real sink_master.
119 * Data read from source_master is matched against the saved sink data and
120 * echo canceled data is then pushed onto the new source.
122 * Both source and sink masters have their own threads to push/pull data
123 * respectively. We however perform all our actions in the source IO thread.
124 * To do this we send all played samples to the source IO thread where they
125 * are then pushed into the memblockq.
127 * Alignment is performed in two steps:
129 * 1) when something happens that requires quick adjustment of the alignment of
130 * capture and playback samples, we perform a resync. This adjusts the
131 * position in the playback memblock to the requested sample. Quick
132 * adjustments include moving the playback samples before the capture
133 * samples (because else the echo canceler does not work) or when the
134 * playback pointer drifts too far away.
136 * 2) periodically check the difference between capture and playback. we use a
137 * low and high watermark for adjusting the alignment. playback should always
138 * be before capture and the difference should not be bigger than one frame
139 * size. We would ideally like to resample the sink_input but most driver
140 * don't give enough accuracy to be able to do that right now.
145 pa_usec_t sink_latency
;
147 int64_t send_counter
;
149 pa_usec_t source_now
;
150 pa_usec_t source_latency
;
152 int64_t recv_counter
;
161 pa_bool_t autoloaded
;
165 pa_echo_canceller
*ec
;
168 pa_bool_t need_realign
;
170 /* to wakeup the source I/O thread */
171 pa_asyncmsgq
*asyncmsgq
;
172 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
175 pa_bool_t source_auto_desc
;
176 pa_source_output
*source_output
;
177 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
181 pa_bool_t sink_auto_desc
;
182 pa_sink_input
*sink_input
;
183 pa_memblockq
*sink_memblockq
;
184 int64_t send_counter
; /* updated in sink IO thread */
185 int64_t recv_counter
;
188 pa_atomic_t request_resync
;
191 pa_time_event
*time_event
;
192 pa_usec_t adjust_time
;
193 int adjust_threshold
;
200 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
202 static const char* const valid_modargs
[] = {
219 "use_volume_sharing",
224 SOURCE_OUTPUT_MESSAGE_POST
= PA_SOURCE_OUTPUT_MESSAGE_MAX
,
225 SOURCE_OUTPUT_MESSAGE_REWIND
,
226 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
,
227 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
231 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
234 static int64_t calc_diff(struct userdata
*u
, struct snapshot
*snapshot
) {
235 int64_t buffer
, diff_time
, buffer_latency
;
237 /* get the number of samples between capture and playback */
238 if (snapshot
->plen
> snapshot
->rlen
)
239 buffer
= snapshot
->plen
- snapshot
->rlen
;
243 buffer
+= snapshot
->source_delay
+ snapshot
->sink_delay
;
245 /* add the amount of samples not yet transferred to the source context */
246 if (snapshot
->recv_counter
<= snapshot
->send_counter
)
247 buffer
+= (int64_t) (snapshot
->send_counter
- snapshot
->recv_counter
);
249 buffer
+= PA_CLIP_SUB(buffer
, (int64_t) (snapshot
->recv_counter
- snapshot
->send_counter
));
251 /* convert to time */
252 buffer_latency
= pa_bytes_to_usec(buffer
, &u
->source_output
->sample_spec
);
254 /* capture and playback samples are perfectly aligned when diff_time is 0 */
255 diff_time
= (snapshot
->sink_now
+ snapshot
->sink_latency
- buffer_latency
) -
256 (snapshot
->source_now
- snapshot
->source_latency
);
258 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time
,
259 (long long) snapshot
->sink_latency
,
260 (long long) buffer_latency
, (long long) snapshot
->source_latency
,
261 (long long) snapshot
->source_delay
, (long long) snapshot
->sink_delay
,
262 (long long) (snapshot
->send_counter
- snapshot
->recv_counter
),
263 (long long) (snapshot
->sink_now
- snapshot
->source_now
));
268 /* Called from main context */
269 static void time_callback(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
270 struct userdata
*u
= userdata
;
271 uint32_t old_rate
, base_rate
, new_rate
;
274 struct snapshot latency_snapshot
;
278 pa_assert(u
->time_event
== e
);
279 pa_assert_ctl_context();
281 if (u
->active_mask
!= 3)
284 /* update our snapshots */
285 pa_asyncmsgq_send(u
->source_output
->source
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
286 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
288 /* calculate drift between capture and playback */
289 diff_time
= calc_diff(u
, &latency_snapshot
);
291 /*fs = pa_frame_size(&u->source_output->sample_spec);*/
292 old_rate
= u
->sink_input
->sample_spec
.rate
;
293 base_rate
= u
->source_output
->sample_spec
.rate
;
296 /* recording before playback, we need to adjust quickly. The echo
297 * canceler does not work in this case. */
298 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
299 NULL
, diff_time
, NULL
, NULL
);
300 /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
301 new_rate
= base_rate
;
304 if (diff_time
> u
->adjust_threshold
) {
305 /* diff too big, quickly adjust */
306 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
307 NULL
, diff_time
, NULL
, NULL
);
310 /* recording behind playback, we need to slowly adjust the rate to match */
311 /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
313 /* assume equal samplerates for now */
314 new_rate
= base_rate
;
317 /* make sure we don't make too big adjustments because that sounds horrible */
318 if (new_rate
> base_rate
* 1.1 || new_rate
< base_rate
* 0.9)
319 new_rate
= base_rate
;
321 if (new_rate
!= old_rate
) {
322 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate
, (unsigned long) new_rate
);
324 pa_sink_input_set_rate(u
->sink_input
, new_rate
);
327 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
330 /* Called from source I/O thread context */
331 static int source_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
332 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
336 case PA_SOURCE_MESSAGE_GET_LATENCY
:
338 /* The source is _put() before the source output is, so let's
339 * make sure we don't access it in that time. Also, the
340 * source output is first shut down, the source second. */
341 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
342 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
)) {
343 *((pa_usec_t
*) data
) = 0;
347 *((pa_usec_t
*) data
) =
349 /* Get the latency of the master source */
350 pa_source_get_latency_within_thread(u
->source_output
->source
) +
351 /* Add the latency internal to our source output on top */
352 pa_bytes_to_usec(pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
), &u
->source_output
->source
->sample_spec
) +
353 /* and the buffering we do on the source */
354 pa_bytes_to_usec(u
->blocksize
, &u
->source_output
->source
->sample_spec
);
360 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
363 /* Called from sink I/O thread context */
364 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
365 struct userdata
*u
= PA_SINK(o
)->userdata
;
369 case PA_SINK_MESSAGE_GET_LATENCY
:
371 /* The sink is _put() before the sink input is, so let's
372 * make sure we don't access it in that time. Also, the
373 * sink input is first shut down, the sink second. */
374 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
375 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
376 *((pa_usec_t
*) data
) = 0;
380 *((pa_usec_t
*) data
) =
382 /* Get the latency of the master sink */
383 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
385 /* Add the latency internal to our sink input on top */
386 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
391 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
395 /* Called from main context */
396 static int source_set_state_cb(pa_source
*s
, pa_source_state_t state
) {
399 pa_source_assert_ref(s
);
400 pa_assert_se(u
= s
->userdata
);
402 if (!PA_SOURCE_IS_LINKED(state
) ||
403 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
406 pa_log_debug("Source state %d %d", state
, u
->active_mask
);
408 if (state
== PA_SOURCE_RUNNING
) {
409 /* restart timer when both sink and source are active */
411 if (u
->active_mask
== 3 && u
->adjust_time
)
412 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
414 pa_atomic_store(&u
->request_resync
, 1);
415 pa_source_output_cork(u
->source_output
, FALSE
);
416 } else if (state
== PA_SOURCE_SUSPENDED
) {
417 u
->active_mask
&= ~1;
418 pa_source_output_cork(u
->source_output
, TRUE
);
423 /* Called from main context */
424 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
427 pa_sink_assert_ref(s
);
428 pa_assert_se(u
= s
->userdata
);
430 if (!PA_SINK_IS_LINKED(state
) ||
431 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
434 pa_log_debug("Sink state %d %d", state
, u
->active_mask
);
436 if (state
== PA_SINK_RUNNING
) {
437 /* restart timer when both sink and source are active */
439 if (u
->active_mask
== 3 && u
->adjust_time
)
440 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
442 pa_atomic_store(&u
->request_resync
, 1);
443 pa_sink_input_cork(u
->sink_input
, FALSE
);
444 } else if (state
== PA_SINK_SUSPENDED
) {
445 u
->active_mask
&= ~2;
446 pa_sink_input_cork(u
->sink_input
, TRUE
);
451 /* Called from I/O thread context */
452 static void source_update_requested_latency_cb(pa_source
*s
) {
455 pa_source_assert_ref(s
);
456 pa_assert_se(u
= s
->userdata
);
458 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
459 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
))
462 pa_log_debug("Source update requested latency");
464 /* Just hand this one over to the master source */
465 pa_source_output_set_requested_latency_within_thread(
467 pa_source_get_requested_latency_within_thread(s
));
470 /* Called from I/O thread context */
471 static void sink_update_requested_latency_cb(pa_sink
*s
) {
474 pa_sink_assert_ref(s
);
475 pa_assert_se(u
= s
->userdata
);
477 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
478 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
481 pa_log_debug("Sink update requested latency");
483 /* Just hand this one over to the master sink */
484 pa_sink_input_set_requested_latency_within_thread(
486 pa_sink_get_requested_latency_within_thread(s
));
489 /* Called from I/O thread context */
490 static void sink_request_rewind_cb(pa_sink
*s
) {
493 pa_sink_assert_ref(s
);
494 pa_assert_se(u
= s
->userdata
);
496 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
497 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
500 pa_log_debug("Sink request rewind %lld", (long long) s
->thread_info
.rewind_nbytes
);
502 /* Just hand this one over to the master sink */
503 pa_sink_input_request_rewind(u
->sink_input
,
504 s
->thread_info
.rewind_nbytes
, TRUE
, FALSE
, FALSE
);
507 /* Called from main context */
508 static void source_set_volume_cb(pa_source
*s
) {
511 pa_source_assert_ref(s
);
512 pa_assert_se(u
= s
->userdata
);
514 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
515 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
518 pa_source_output_set_volume(u
->source_output
, &s
->real_volume
, s
->save_volume
, TRUE
);
521 /* Called from main context */
522 static void sink_set_volume_cb(pa_sink
*s
) {
525 pa_sink_assert_ref(s
);
526 pa_assert_se(u
= s
->userdata
);
528 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
529 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
532 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
535 static void source_get_volume_cb(pa_source
*s
) {
539 pa_source_assert_ref(s
);
540 pa_assert_se(u
= s
->userdata
);
542 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
543 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
546 pa_source_output_get_volume(u
->source_output
, &v
, TRUE
);
548 if (pa_cvolume_equal(&s
->real_volume
, &v
))
553 pa_source_set_soft_volume(s
, NULL
);
556 /* Called from main context */
557 static void source_set_mute_cb(pa_source
*s
) {
560 pa_source_assert_ref(s
);
561 pa_assert_se(u
= s
->userdata
);
563 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
564 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
567 pa_source_output_set_mute(u
->source_output
, s
->muted
, s
->save_muted
);
570 /* Called from main context */
571 static void sink_set_mute_cb(pa_sink
*s
) {
574 pa_sink_assert_ref(s
);
575 pa_assert_se(u
= s
->userdata
);
577 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
578 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
581 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
584 /* Called from main context */
585 static void source_get_mute_cb(pa_source
*s
) {
588 pa_source_assert_ref(s
);
589 pa_assert_se(u
= s
->userdata
);
591 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
592 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
595 pa_source_output_get_mute(u
->source_output
);
598 /* must be called from the input thread context */
599 static void apply_diff_time(struct userdata
*u
, int64_t diff_time
) {
603 diff
= pa_usec_to_bytes(-diff_time
, &u
->source_output
->sample_spec
);
606 /* add some extra safety samples to compensate for jitter in the
608 diff
+= 10 * pa_frame_size (&u
->source_output
->sample_spec
);
610 pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time
, (long long) diff
);
615 } else if (diff_time
> 0) {
616 diff
= pa_usec_to_bytes(diff_time
, &u
->source_output
->sample_spec
);
619 pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time
, (long long) diff
);
621 u
->source_skip
= diff
;
627 /* must be called from the input thread */
628 static void do_resync(struct userdata
*u
) {
630 struct snapshot latency_snapshot
;
632 pa_log("Doing resync");
634 /* update our snapshot */
635 source_output_snapshot_within_thread(u
, &latency_snapshot
);
636 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
638 /* calculate drift between capture and playback */
639 diff_time
= calc_diff(u
, &latency_snapshot
);
641 /* and adjust for the drift */
642 apply_diff_time(u
, diff_time
);
645 /* Called from input thread context */
646 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
650 pa_source_output_assert_ref(o
);
651 pa_source_output_assert_io_context(o
);
652 pa_assert_se(u
= o
->userdata
);
654 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
655 pa_log("push when no link?");
659 if (PA_UNLIKELY(u
->source
->thread_info
.state
!= PA_SOURCE_RUNNING
)) {
660 pa_source_post(u
->source
, chunk
);
664 /* handle queued messages, do any message sending of our own */
665 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
668 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
670 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
671 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
673 /* Let's not do anything else till we have enough data to process */
674 if (rlen
< u
->blocksize
)
677 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
681 while (rlen
>= u
->blocksize
) {
682 pa_memchunk rchunk
, pchunk
;
684 /* take fixed block from recorded samples */
685 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
687 if (plen
> u
->blocksize
&& u
->source_skip
== 0) {
688 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 unused
= fwrite(rdata
, 1, u
->blocksize
, u
->captured_file
);
724 unused
= fwrite(pdata
, 1, u
->blocksize
, u
->played_file
);
727 /* perform echo cancellation */
728 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
731 if (u
->canceled_file
)
732 unused
= fwrite(cdata
, 1, u
->blocksize
, u
->canceled_file
);
735 pa_memblock_release(cchunk
.memblock
);
736 pa_memblock_release(pchunk
.memblock
);
737 pa_memblock_release(rchunk
.memblock
);
739 /* drop consumed sink samples */
740 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
741 pa_memblock_unref(pchunk
.memblock
);
743 pa_memblock_unref(rchunk
.memblock
);
744 /* the filtered samples now become the samples from our
748 plen
-= u
->blocksize
;
752 /* forward the (echo-canceled) data to the virtual source */
753 pa_source_post(u
->source
, &rchunk
);
754 pa_memblock_unref(rchunk
.memblock
);
756 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
757 rlen
-= u
->blocksize
;
759 if (u
->source_skip
) {
760 if (u
->source_skip
> u
->blocksize
) {
761 u
->source_skip
-= u
->blocksize
;
764 u
->sink_skip
+= (u
->blocksize
- u
->source_skip
);
771 /* Called from I/O thread context */
772 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
775 pa_sink_input_assert_ref(i
);
777 pa_assert_se(u
= i
->userdata
);
779 if (u
->sink
->thread_info
.rewind_requested
)
780 pa_sink_process_rewind(u
->sink
, 0);
782 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
784 if (i
->thread_info
.underrun_for
> 0) {
785 pa_log_debug("Handling end of underrun.");
786 pa_atomic_store(&u
->request_resync
, 1);
789 /* let source thread handle the chunk. pass the sample count as well so that
790 * the source IO thread can update the right variables. */
791 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
792 NULL
, 0, chunk
, NULL
);
793 u
->send_counter
+= chunk
->length
;
798 /* Called from input thread context */
799 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
802 pa_source_output_assert_ref(o
);
803 pa_source_output_assert_io_context(o
);
804 pa_assert_se(u
= o
->userdata
);
806 pa_source_process_rewind(u
->source
, nbytes
);
808 /* go back on read side, we need to use older sink data for this */
809 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
811 /* manipulate write index */
812 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
814 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
815 (long long) pa_memblockq_get_length (u
->source_memblockq
));
818 /* Called from I/O thread context */
819 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
822 pa_sink_input_assert_ref(i
);
823 pa_assert_se(u
= i
->userdata
);
825 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
827 pa_sink_process_rewind(u
->sink
, nbytes
);
829 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
830 u
->send_counter
-= nbytes
;
833 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
834 size_t delay
, rlen
, plen
;
835 pa_usec_t now
, latency
;
837 now
= pa_rtclock_now();
838 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
839 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
841 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
842 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
843 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
845 snapshot
->source_now
= now
;
846 snapshot
->source_latency
= latency
;
847 snapshot
->source_delay
= delay
;
848 snapshot
->recv_counter
= u
->recv_counter
;
849 snapshot
->rlen
= rlen
+ u
->sink_skip
;
850 snapshot
->plen
= plen
+ u
->source_skip
;
854 /* Called from output thread context */
855 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
856 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
860 case SOURCE_OUTPUT_MESSAGE_POST
:
862 pa_source_output_assert_io_context(u
->source_output
);
864 if (u
->source_output
->source
->thread_info
.state
== PA_SOURCE_RUNNING
)
865 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
867 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
869 u
->recv_counter
+= (int64_t) chunk
->length
;
873 case SOURCE_OUTPUT_MESSAGE_REWIND
:
874 pa_source_output_assert_io_context(u
->source_output
);
876 /* manipulate write index, never go past what we have */
877 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
878 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
880 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
882 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
884 u
->recv_counter
-= offset
;
888 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
889 struct snapshot
*snapshot
= (struct snapshot
*) data
;
891 source_output_snapshot_within_thread(u
, snapshot
);
895 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
896 apply_diff_time(u
, offset
);
901 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
904 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
905 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
909 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
911 pa_usec_t now
, latency
;
912 struct snapshot
*snapshot
= (struct snapshot
*) data
;
914 pa_sink_input_assert_io_context(u
->sink_input
);
916 now
= pa_rtclock_now();
917 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
918 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
920 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
922 snapshot
->sink_now
= now
;
923 snapshot
->sink_latency
= latency
;
924 snapshot
->sink_delay
= delay
;
925 snapshot
->send_counter
= u
->send_counter
;
930 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
933 /* Called from I/O thread context */
934 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
937 pa_sink_input_assert_ref(i
);
938 pa_assert_se(u
= i
->userdata
);
940 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
942 pa_memblockq_set_maxrewind(u
->sink_memblockq
, nbytes
);
943 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
946 /* Called from I/O thread context */
947 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
950 pa_source_output_assert_ref(o
);
951 pa_assert_se(u
= o
->userdata
);
953 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
955 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
958 /* Called from I/O thread context */
959 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
962 pa_sink_input_assert_ref(i
);
963 pa_assert_se(u
= i
->userdata
);
965 pa_log_debug("Sink input update max request %lld", (long long) nbytes
);
967 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
);
970 /* Called from I/O thread context */
971 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
975 pa_sink_input_assert_ref(i
);
976 pa_assert_se(u
= i
->userdata
);
978 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
980 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
983 /* Called from I/O thread context */
984 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
988 pa_source_output_assert_ref(o
);
989 pa_assert_se(u
= o
->userdata
);
991 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
993 pa_log_debug("source output update requested latency %lld", (long long) latency
);
996 /* Called from I/O thread context */
997 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
1000 pa_sink_input_assert_ref(i
);
1001 pa_assert_se(u
= i
->userdata
);
1003 pa_log_debug("Sink input update latency range %lld %lld",
1004 (long long) i
->sink
->thread_info
.min_latency
,
1005 (long long) i
->sink
->thread_info
.max_latency
);
1007 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1010 /* Called from I/O thread context */
1011 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
1014 pa_source_output_assert_ref(o
);
1015 pa_assert_se(u
= o
->userdata
);
1017 pa_log_debug("Source output update latency range %lld %lld",
1018 (long long) o
->source
->thread_info
.min_latency
,
1019 (long long) o
->source
->thread_info
.max_latency
);
1021 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1024 /* Called from I/O thread context */
1025 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
1028 pa_sink_input_assert_ref(i
);
1029 pa_assert_se(u
= i
->userdata
);
1031 pa_log_debug("Sink input update fixed latency %lld",
1032 (long long) i
->sink
->thread_info
.fixed_latency
);
1034 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1037 /* Called from I/O thread context */
1038 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1041 pa_source_output_assert_ref(o
);
1042 pa_assert_se(u
= o
->userdata
);
1044 pa_log_debug("Source output update fixed latency %lld",
1045 (long long) o
->source
->thread_info
.fixed_latency
);
1047 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1050 /* Called from output thread context */
1051 static void source_output_attach_cb(pa_source_output
*o
) {
1054 pa_source_output_assert_ref(o
);
1055 pa_source_output_assert_io_context(o
);
1056 pa_assert_se(u
= o
->userdata
);
1058 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1059 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1060 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1061 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1063 pa_log_debug("Source output %d attach", o
->index
);
1065 pa_source_attach_within_thread(u
->source
);
1067 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1068 o
->source
->thread_info
.rtpoll
,
1073 /* Called from I/O thread context */
1074 static void sink_input_attach_cb(pa_sink_input
*i
) {
1077 pa_sink_input_assert_ref(i
);
1078 pa_assert_se(u
= i
->userdata
);
1080 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1081 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1083 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1084 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1085 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1087 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1088 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1090 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1091 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1093 pa_log_debug("Sink input %d attach", i
->index
);
1095 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1096 i
->sink
->thread_info
.rtpoll
,
1100 pa_sink_attach_within_thread(u
->sink
);
1104 /* Called from output thread context */
1105 static void source_output_detach_cb(pa_source_output
*o
) {
1108 pa_source_output_assert_ref(o
);
1109 pa_source_output_assert_io_context(o
);
1110 pa_assert_se(u
= o
->userdata
);
1112 pa_source_detach_within_thread(u
->source
);
1113 pa_source_set_rtpoll(u
->source
, NULL
);
1115 pa_log_debug("Source output %d detach", o
->index
);
1117 if (u
->rtpoll_item_read
) {
1118 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1119 u
->rtpoll_item_read
= NULL
;
1123 /* Called from I/O thread context */
1124 static void sink_input_detach_cb(pa_sink_input
*i
) {
1127 pa_sink_input_assert_ref(i
);
1128 pa_assert_se(u
= i
->userdata
);
1130 pa_sink_detach_within_thread(u
->sink
);
1132 pa_sink_set_rtpoll(u
->sink
, NULL
);
1134 pa_log_debug("Sink input %d detach", i
->index
);
1136 if (u
->rtpoll_item_write
) {
1137 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1138 u
->rtpoll_item_write
= NULL
;
1142 /* Called from output thread context */
1143 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1146 pa_source_output_assert_ref(o
);
1147 pa_source_output_assert_io_context(o
);
1148 pa_assert_se(u
= o
->userdata
);
1150 pa_log_debug("Source output %d state %d", o
->index
, state
);
1153 /* Called from IO thread context */
1154 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1157 pa_sink_input_assert_ref(i
);
1158 pa_assert_se(u
= i
->userdata
);
1160 pa_log_debug("Sink input %d state %d", i
->index
, state
);
1162 /* If we are added for the first time, ask for a rewinding so that
1163 * we are heard right-away. */
1164 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1165 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1166 pa_log_debug("Requesting rewind due to state change.");
1167 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1171 /* Called from main thread */
1172 static void source_output_kill_cb(pa_source_output
*o
) {
1175 pa_source_output_assert_ref(o
);
1176 pa_assert_ctl_context();
1177 pa_assert_se(u
= o
->userdata
);
1181 /* The order here matters! We first kill the source output, followed
1182 * by the source. That means the source callbacks must be protected
1183 * against an unconnected source output! */
1184 pa_source_output_unlink(u
->source_output
);
1185 pa_source_unlink(u
->source
);
1187 pa_source_output_unref(u
->source_output
);
1188 u
->source_output
= NULL
;
1190 pa_source_unref(u
->source
);
1193 pa_log_debug("Source output kill %d", o
->index
);
1195 pa_module_unload_request(u
->module
, TRUE
);
1198 /* Called from main context */
1199 static void sink_input_kill_cb(pa_sink_input
*i
) {
1202 pa_sink_input_assert_ref(i
);
1203 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 %d", i
->index
);
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
);
1235 return (u
->source
!= dest
) && (u
->sink
!= dest
->monitor_of
);
1238 /* Called from main context */
1239 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1242 pa_sink_input_assert_ref(i
);
1243 pa_assert_se(u
= i
->userdata
);
1248 return u
->sink
!= dest
;
1251 /* Called from main thread */
1252 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1255 pa_source_output_assert_ref(o
);
1256 pa_assert_ctl_context();
1257 pa_assert_se(u
= o
->userdata
);
1260 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1261 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1263 pa_source_set_asyncmsgq(u
->source
, NULL
);
1265 if (u
->source_auto_desc
&& dest
) {
1269 pl
= pa_proplist_new();
1270 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1271 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1272 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1274 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1275 pa_proplist_free(pl
);
1279 /* Called from main context */
1280 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1283 pa_sink_input_assert_ref(i
);
1284 pa_assert_se(u
= i
->userdata
);
1287 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1288 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1290 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1292 if (u
->sink_auto_desc
&& dest
) {
1296 pl
= pa_proplist_new();
1297 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1298 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1299 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1301 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1302 pa_proplist_free(pl
);
1306 /* Called from main context */
1307 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1310 pa_sink_input_assert_ref(i
);
1311 pa_assert_se(u
= i
->userdata
);
1313 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1316 /* Called from main context */
1317 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1320 pa_sink_input_assert_ref(i
);
1321 pa_assert_se(u
= i
->userdata
);
1323 pa_sink_mute_changed(u
->sink
, i
->muted
);
1326 static pa_echo_canceller_method_t
get_ec_method_from_string(const char *method
) {
1327 if (pa_streq(method
, "speex"))
1328 return PA_ECHO_CANCELLER_SPEEX
;
1329 else if (pa_streq(method
, "adrian"))
1330 return PA_ECHO_CANCELLER_ADRIAN
;
1332 return PA_ECHO_CANCELLER_INVALID
;
1335 /* Common initialisation bits between module-echo-cancel and the standalone test program */
1336 static int init_common(pa_modargs
*ma
, struct userdata
*u
, pa_sample_spec
*source_ss
, pa_channel_map
*source_map
) {
1337 pa_echo_canceller_method_t ec_method
;
1339 if (pa_modargs_get_sample_spec_and_channel_map(ma
, source_ss
, source_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1340 pa_log("Invalid sample format specification or channel map");
1344 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1346 pa_log("Failed to alloc echo canceller");
1350 if ((ec_method
= get_ec_method_from_string(pa_modargs_get_value(ma
, "aec_method", DEFAULT_ECHO_CANCELLER
))) < 0) {
1351 pa_log("Invalid echo canceller implementation");
1355 u
->ec
->init
= ec_table
[ec_method
].init
;
1356 u
->ec
->run
= ec_table
[ec_method
].run
;
1357 u
->ec
->done
= ec_table
[ec_method
].done
;
1366 int pa__init(pa_module
*m
) {
1368 pa_sample_spec source_ss
, sink_ss
;
1369 pa_channel_map source_map
, sink_map
;
1371 pa_source
*source_master
=NULL
;
1372 pa_sink
*sink_master
=NULL
;
1373 pa_source_output_new_data source_output_data
;
1374 pa_sink_input_new_data sink_input_data
;
1375 pa_source_new_data source_data
;
1376 pa_sink_new_data sink_data
;
1377 pa_memchunk silence
;
1379 pa_bool_t use_volume_sharing
= TRUE
;
1383 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1384 pa_log("Failed to parse module arguments.");
1388 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1389 pa_log("Master source not found");
1392 pa_assert(source_master
);
1394 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1395 pa_log("Master sink not found");
1398 pa_assert(sink_master
);
1400 if (source_master
->monitor_of
== sink_master
) {
1401 pa_log("Can't cancel echo between a sink and its monitor");
1405 source_ss
= source_master
->sample_spec
;
1406 source_ss
.rate
= DEFAULT_RATE
;
1407 source_ss
.channels
= DEFAULT_CHANNELS
;
1408 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1410 sink_ss
= sink_master
->sample_spec
;
1411 sink_map
= sink_master
->channel_map
;
1413 if (pa_modargs_get_value_boolean(ma
, "use_volume_sharing", &use_volume_sharing
) < 0) {
1414 pa_log("use_volume_sharing= expects a boolean argument");
1418 u
= pa_xnew0(struct userdata
, 1);
1420 pa_log("Failed to alloc userdata");
1428 temp
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1429 if (pa_modargs_get_value_u32(ma
, "adjust_time", &temp
) < 0) {
1430 pa_log("Failed to parse adjust_time value");
1434 if (temp
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1435 u
->adjust_time
= temp
* PA_USEC_PER_SEC
;
1437 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1439 temp
= DEFAULT_ADJUST_TOLERANCE
/ PA_USEC_PER_MSEC
;
1440 if (pa_modargs_get_value_u32(ma
, "adjust_threshold", &temp
) < 0) {
1441 pa_log("Failed to parse adjust_threshold value");
1445 if (temp
!= DEFAULT_ADJUST_TOLERANCE
/ PA_USEC_PER_MSEC
)
1446 u
->adjust_threshold
= temp
* PA_USEC_PER_MSEC
;
1448 u
->adjust_threshold
= DEFAULT_ADJUST_TOLERANCE
;
1450 u
->save_aec
= DEFAULT_SAVE_AEC
;
1451 if (pa_modargs_get_value_boolean(ma
, "save_aec", &u
->save_aec
) < 0) {
1452 pa_log("Failed to parse save_aec value");
1456 u
->autoloaded
= DEFAULT_AUTOLOADED
;
1457 if (pa_modargs_get_value_boolean(ma
, "autoloaded", &u
->autoloaded
) < 0) {
1458 pa_log("Failed to parse autoloaded value");
1462 if (init_common(ma
, u
, &source_ss
, &source_map
))
1465 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1466 u
->need_realign
= TRUE
;
1469 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
))) {
1470 pa_log("Failed to init AEC engine");
1476 pa_source_new_data_init(&source_data
);
1477 source_data
.driver
= __FILE__
;
1478 source_data
.module
= m
;
1479 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1480 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1481 pa_source_new_data_set_sample_spec(&source_data
, &source_ss
);
1482 pa_source_new_data_set_channel_map(&source_data
, &source_map
);
1483 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1484 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1486 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1487 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1489 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1490 pa_log("Invalid properties");
1491 pa_source_new_data_done(&source_data
);
1495 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1498 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1499 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1502 u
->source
= pa_source_new(m
->core
, &source_data
, (source_master
->flags
& (PA_SOURCE_LATENCY
| PA_SOURCE_DYNAMIC_LATENCY
))
1503 | (use_volume_sharing
? PA_SOURCE_SHARE_VOLUME_WITH_MASTER
: 0));
1504 pa_source_new_data_done(&source_data
);
1507 pa_log("Failed to create source.");
1511 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1512 u
->source
->set_state
= source_set_state_cb
;
1513 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1514 pa_source_set_get_mute_callback(u
->source
, source_get_mute_cb
);
1515 pa_source_set_set_mute_callback(u
->source
, source_set_mute_cb
);
1516 if (!use_volume_sharing
) {
1517 pa_source_set_get_volume_callback(u
->source
, source_get_volume_cb
);
1518 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1519 pa_source_enable_decibel_volume(u
->source
, TRUE
);
1521 u
->source
->userdata
= u
;
1523 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1526 pa_sink_new_data_init(&sink_data
);
1527 sink_data
.driver
= __FILE__
;
1528 sink_data
.module
= m
;
1529 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1530 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1531 pa_sink_new_data_set_sample_spec(&sink_data
, &sink_ss
);
1532 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
1533 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1534 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1536 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1537 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1539 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1540 pa_log("Invalid properties");
1541 pa_sink_new_data_done(&sink_data
);
1545 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1548 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1549 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1552 u
->sink
= pa_sink_new(m
->core
, &sink_data
, (sink_master
->flags
& (PA_SINK_LATENCY
| PA_SINK_DYNAMIC_LATENCY
))
1553 | (use_volume_sharing
? PA_SINK_SHARE_VOLUME_WITH_MASTER
: 0));
1554 pa_sink_new_data_done(&sink_data
);
1557 pa_log("Failed to create sink.");
1561 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1562 u
->sink
->set_state
= sink_set_state_cb
;
1563 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1564 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1565 pa_sink_set_set_mute_callback(u
->sink
, sink_set_mute_cb
);
1566 if (!use_volume_sharing
) {
1567 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1568 pa_sink_enable_decibel_volume(u
->sink
, TRUE
);
1570 u
->sink
->userdata
= u
;
1572 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1574 /* Create source output */
1575 pa_source_output_new_data_init(&source_output_data
);
1576 source_output_data
.driver
= __FILE__
;
1577 source_output_data
.module
= m
;
1578 pa_source_output_new_data_set_source(&source_output_data
, source_master
, FALSE
);
1579 source_output_data
.destination_source
= u
->source
;
1581 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1583 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1584 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1585 pa_source_output_new_data_set_sample_spec(&source_output_data
, &source_ss
);
1586 pa_source_output_new_data_set_channel_map(&source_output_data
, &source_map
);
1588 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1589 pa_source_output_new_data_done(&source_output_data
);
1591 if (!u
->source_output
)
1594 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1595 u
->source_output
->push
= source_output_push_cb
;
1596 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1597 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1598 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1599 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1600 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1601 u
->source_output
->kill
= source_output_kill_cb
;
1602 u
->source_output
->attach
= source_output_attach_cb
;
1603 u
->source_output
->detach
= source_output_detach_cb
;
1604 u
->source_output
->state_change
= source_output_state_change_cb
;
1605 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1606 u
->source_output
->moving
= source_output_moving_cb
;
1607 u
->source_output
->userdata
= u
;
1609 u
->source
->output_from_master
= u
->source_output
;
1611 /* Create sink input */
1612 pa_sink_input_new_data_init(&sink_input_data
);
1613 sink_input_data
.driver
= __FILE__
;
1614 sink_input_data
.module
= m
;
1615 pa_sink_input_new_data_set_sink(&sink_input_data
, sink_master
, FALSE
);
1616 sink_input_data
.origin_sink
= u
->sink
;
1617 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1618 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1619 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_ss
);
1620 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_map
);
1621 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1623 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1624 pa_sink_input_new_data_done(&sink_input_data
);
1629 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1630 u
->sink_input
->pop
= sink_input_pop_cb
;
1631 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1632 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1633 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1634 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1635 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1636 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1637 u
->sink_input
->kill
= sink_input_kill_cb
;
1638 u
->sink_input
->attach
= sink_input_attach_cb
;
1639 u
->sink_input
->detach
= sink_input_detach_cb
;
1640 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1641 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1642 u
->sink_input
->moving
= sink_input_moving_cb
;
1643 if (!use_volume_sharing
)
1644 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1645 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1646 u
->sink_input
->userdata
= u
;
1648 u
->sink
->input_to_master
= u
->sink_input
;
1650 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1652 u
->source_memblockq
= pa_memblockq_new("module-echo-cancel source_memblockq", 0, MEMBLOCKQ_MAXLENGTH
, 0,
1653 &source_ss
, 1, 1, 0, &silence
);
1654 u
->sink_memblockq
= pa_memblockq_new("module-echo-cancel sink_memblockq", 0, MEMBLOCKQ_MAXLENGTH
, 0,
1655 &sink_ss
, 1, 1, 0, &silence
);
1657 pa_memblock_unref(silence
.memblock
);
1659 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1660 pa_log("Failed to create memblockq.");
1664 /* our source and sink are not suspended when we create them */
1667 if (u
->adjust_time
> 0)
1668 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1671 pa_log("Creating AEC files in /tmp");
1672 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1673 if (u
->captured_file
== NULL
)
1674 perror ("fopen failed");
1675 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1676 if (u
->played_file
== NULL
)
1677 perror ("fopen failed");
1678 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1679 if (u
->canceled_file
== NULL
)
1680 perror ("fopen failed");
1683 pa_sink_put(u
->sink
);
1684 pa_source_put(u
->source
);
1686 pa_sink_input_put(u
->sink_input
);
1687 pa_source_output_put(u
->source_output
);
1689 pa_modargs_free(ma
);
1695 pa_modargs_free(ma
);
1702 int pa__get_n_used(pa_module
*m
) {
1706 pa_assert_se(u
= m
->userdata
);
1708 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1711 void pa__done(pa_module
*m
) {
1716 if (!(u
= m
->userdata
))
1721 /* See comments in source_output_kill_cb() above regarding
1722 * destruction order! */
1725 u
->core
->mainloop
->time_free(u
->time_event
);
1727 if (u
->source_output
)
1728 pa_source_output_unlink(u
->source_output
);
1730 pa_sink_input_unlink(u
->sink_input
);
1733 pa_source_unlink(u
->source
);
1735 pa_sink_unlink(u
->sink
);
1737 if (u
->source_output
)
1738 pa_source_output_unref(u
->source_output
);
1740 pa_sink_input_unref(u
->sink_input
);
1743 pa_source_unref(u
->source
);
1745 pa_sink_unref(u
->sink
);
1747 if (u
->source_memblockq
)
1748 pa_memblockq_free(u
->source_memblockq
);
1749 if (u
->sink_memblockq
)
1750 pa_memblockq_free(u
->sink_memblockq
);
1760 pa_asyncmsgq_unref(u
->asyncmsgq
);
1765 #ifdef ECHO_CANCEL_TEST
1767 * Stand-alone test program for running in the canceller on pre-recorded files.
1769 int main(int argc
, char* argv
[]) {
1771 pa_sample_spec source_ss
, sink_ss
;
1772 pa_channel_map source_map
, sink_map
;
1773 pa_modargs
*ma
= NULL
;
1774 uint8_t *rdata
= NULL
, *pdata
= NULL
, *cdata
= NULL
;
1775 int ret
= 0, unused
;
1777 pa_memzero(&u
, sizeof(u
));
1779 if (argc
< 4 || argc
> 6) {
1783 u
.ec
= pa_xnew0(pa_echo_canceller
, 1);
1785 pa_log("Failed to alloc echo canceller");
1789 u
.captured_file
= fopen(argv
[2], "r");
1790 if (u
.captured_file
== NULL
) {
1791 perror ("fopen failed");
1794 u
.played_file
= fopen(argv
[1], "r");
1795 if (u
.played_file
== NULL
) {
1796 perror ("fopen failed");
1799 u
.canceled_file
= fopen(argv
[3], "wb");
1800 if (u
.canceled_file
== NULL
) {
1801 perror ("fopen failed");
1805 u
.core
= pa_xnew0(pa_core
, 1);
1806 u
.core
->cpu_info
.cpu_type
= PA_CPU_X86
;
1807 u
.core
->cpu_info
.flags
.x86
|= PA_CPU_X86_SSE
;
1809 if (!(ma
= pa_modargs_new(argc
> 4 ? argv
[4] : NULL
, valid_modargs
))) {
1810 pa_log("Failed to parse module arguments.");
1814 source_ss
.format
= PA_SAMPLE_S16LE
;
1815 source_ss
.rate
= DEFAULT_RATE
;
1816 source_ss
.channels
= DEFAULT_CHANNELS
;
1817 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1819 init_common(ma
, &u
, &source_ss
, &source_map
);
1821 if (!u
.ec
->init(u
.core
, u
.ec
, &source_ss
, &source_map
, &sink_ss
, &sink_map
, &u
.blocksize
,
1822 (argc
> 4) ? argv
[5] : NULL
)) {
1823 pa_log("Failed to init AEC engine");
1827 rdata
= pa_xmalloc(u
.blocksize
);
1828 pdata
= pa_xmalloc(u
.blocksize
);
1829 cdata
= pa_xmalloc(u
.blocksize
);
1831 while (fread(rdata
, u
.blocksize
, 1, u
.captured_file
) > 0) {
1832 if (fread(pdata
, u
.blocksize
, 1, u
.played_file
) == 0) {
1833 perror("played file ended before captured file");
1837 u
.ec
->run(u
.ec
, rdata
, pdata
, cdata
);
1839 unused
= fwrite(cdata
, u
.blocksize
, 1, u
.canceled_file
);
1844 fclose(u
.captured_file
);
1845 fclose(u
.played_file
);
1846 fclose(u
.canceled_file
);
1857 pa_modargs_free(ma
);
1862 pa_log("Usage: %s play_file rec_file out_file [module args] [aec_args]",argv
[0]);
1868 #endif /* ECHO_CANCEL_TEST */