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 "format=<sample format> "
71 "channels=<number of channels> "
72 "channel_map=<channel map> "
73 "aec_method=<implementation to use> "
74 "aec_args=<parameters for the AEC engine> "
75 "save_aec=<save AEC data in /tmp> "
76 "autoloaded=<set if this module is being loaded automatically> "
79 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
81 PA_ECHO_CANCELLER_INVALID
= -1,
82 PA_ECHO_CANCELLER_SPEEX
= 0,
83 PA_ECHO_CANCELLER_ADRIAN
,
84 } pa_echo_canceller_method_t
;
86 #define DEFAULT_ECHO_CANCELLER "speex"
88 static const pa_echo_canceller ec_table
[] = {
91 .init
= pa_speex_ec_init
,
92 .run
= pa_speex_ec_run
,
93 .done
= pa_speex_ec_done
,
96 /* Adrian Andre's NLMS implementation */
97 .init
= pa_adrian_ec_init
,
98 .run
= pa_adrian_ec_run
,
99 .done
= pa_adrian_ec_done
,
103 #define DEFAULT_RATE 32000
104 #define DEFAULT_CHANNELS 1
105 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
106 #define DEFAULT_SAVE_AEC 0
107 #define DEFAULT_AUTOLOADED FALSE
109 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
111 /* This module creates a new (virtual) source and sink.
113 * The data sent to the new sink is kept in a memblockq before being
114 * forwarded to the real sink_master.
116 * Data read from source_master is matched against the saved sink data and
117 * echo canceled data is then pushed onto the new source.
119 * Both source and sink masters have their own threads to push/pull data
120 * respectively. We however perform all our actions in the source IO thread.
121 * To do this we send all played samples to the source IO thread where they
122 * are then pushed into the memblockq.
124 * Alignment is performed in two steps:
126 * 1) when something happens that requires quick adjustment of the alignment of
127 * capture and playback samples, we perform a resync. This adjusts the
128 * position in the playback memblock to the requested sample. Quick
129 * adjustments include moving the playback samples before the capture
130 * samples (because else the echo canceler does not work) or when the
131 * playback pointer drifts too far away.
133 * 2) periodically check the difference between capture and playback. we use a
134 * low and high watermark for adjusting the alignment. playback should always
135 * be before capture and the difference should not be bigger than one frame
136 * size. We would ideally like to resample the sink_input but most driver
137 * don't give enough accuracy to be able to do that right now.
142 pa_usec_t sink_latency
;
144 int64_t send_counter
;
146 pa_usec_t source_now
;
147 pa_usec_t source_latency
;
149 int64_t recv_counter
;
158 pa_bool_t autoloaded
;
161 pa_echo_canceller
*ec
;
164 pa_bool_t need_realign
;
166 /* to wakeup the source I/O thread */
168 pa_asyncmsgq
*asyncmsgq
;
169 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
172 pa_bool_t source_auto_desc
;
173 pa_source_output
*source_output
;
174 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
178 pa_bool_t sink_auto_desc
;
179 pa_sink_input
*sink_input
;
180 pa_memblockq
*sink_memblockq
;
181 int64_t send_counter
; /* updated in sink IO thread */
182 int64_t recv_counter
;
185 pa_atomic_t request_resync
;
188 pa_time_event
*time_event
;
189 pa_usec_t adjust_time
;
196 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
198 static const char* const valid_modargs
[] = {
218 SOURCE_OUTPUT_MESSAGE_POST
= PA_SOURCE_OUTPUT_MESSAGE_MAX
,
219 SOURCE_OUTPUT_MESSAGE_REWIND
,
220 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
,
221 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
225 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
228 static int64_t calc_diff(struct userdata
*u
, struct snapshot
*snapshot
) {
229 int64_t buffer
, diff_time
, buffer_latency
;
231 /* get the number of samples between capture and playback */
232 if (snapshot
->plen
> snapshot
->rlen
)
233 buffer
= snapshot
->plen
- snapshot
->rlen
;
237 buffer
+= snapshot
->source_delay
+ snapshot
->sink_delay
;
239 /* add the amount of samples not yet transferred to the source context */
240 if (snapshot
->recv_counter
<= snapshot
->send_counter
)
241 buffer
+= (int64_t) (snapshot
->send_counter
- snapshot
->recv_counter
);
243 buffer
+= PA_CLIP_SUB(buffer
, (int64_t) (snapshot
->recv_counter
- snapshot
->send_counter
));
245 /* convert to time */
246 buffer_latency
= pa_bytes_to_usec(buffer
, &u
->source_output
->sample_spec
);
248 /* capture and playback samples are perfectly aligned when diff_time is 0 */
249 diff_time
= (snapshot
->sink_now
+ snapshot
->sink_latency
- buffer_latency
) -
250 (snapshot
->source_now
- snapshot
->source_latency
);
252 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time
,
253 (long long) snapshot
->sink_latency
,
254 (long long) buffer_latency
, (long long) snapshot
->source_latency
,
255 (long long) snapshot
->source_delay
, (long long) snapshot
->sink_delay
,
256 (long long) (snapshot
->send_counter
- snapshot
->recv_counter
),
257 (long long) (snapshot
->sink_now
- snapshot
->source_now
));
262 /* Called from main context */
263 static void time_callback(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
264 struct userdata
*u
= userdata
;
265 uint32_t old_rate
, base_rate
, new_rate
;
268 struct snapshot latency_snapshot
;
272 pa_assert(u
->time_event
== e
);
273 pa_assert_ctl_context();
275 if (u
->active_mask
!= 3)
278 /* update our snapshots */
279 pa_asyncmsgq_send(u
->source_output
->source
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
280 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
282 /* calculate drift between capture and playback */
283 diff_time
= calc_diff(u
, &latency_snapshot
);
285 /*fs = pa_frame_size(&u->source_output->sample_spec);*/
286 old_rate
= u
->sink_input
->sample_spec
.rate
;
287 base_rate
= u
->source_output
->sample_spec
.rate
;
290 /* recording before playback, we need to adjust quickly. The echo
291 * canceler does not work in this case. */
292 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
293 NULL
, diff_time
, NULL
, NULL
);
294 /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
295 new_rate
= base_rate
;
298 if (diff_time
> 1000) {
299 /* diff too big, quickly adjust */
300 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
301 NULL
, diff_time
, NULL
, NULL
);
304 /* recording behind playback, we need to slowly adjust the rate to match */
305 /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
307 /* assume equal samplerates for now */
308 new_rate
= base_rate
;
311 /* make sure we don't make too big adjustments because that sounds horrible */
312 if (new_rate
> base_rate
* 1.1 || new_rate
< base_rate
* 0.9)
313 new_rate
= base_rate
;
315 if (new_rate
!= old_rate
) {
316 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate
, (unsigned long) new_rate
);
318 pa_sink_input_set_rate(u
->sink_input
, new_rate
);
321 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
324 /* Called from source I/O thread context */
325 static int source_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
326 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
330 case PA_SOURCE_MESSAGE_GET_LATENCY
:
332 /* The source is _put() before the source output is, so let's
333 * make sure we don't access it in that time. Also, the
334 * source output is first shut down, the source second. */
335 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
336 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
)) {
337 *((pa_usec_t
*) data
) = 0;
341 *((pa_usec_t
*) data
) =
343 /* Get the latency of the master source */
344 pa_source_get_latency_within_thread(u
->source_output
->source
) +
345 /* Add the latency internal to our source output on top */
346 pa_bytes_to_usec(pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
), &u
->source_output
->source
->sample_spec
) +
347 /* and the buffering we do on the source */
348 pa_bytes_to_usec(u
->blocksize
, &u
->source_output
->source
->sample_spec
);
354 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
357 /* Called from sink I/O thread context */
358 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
359 struct userdata
*u
= PA_SINK(o
)->userdata
;
363 case PA_SINK_MESSAGE_GET_LATENCY
:
365 /* The sink is _put() before the sink input is, so let's
366 * make sure we don't access it in that time. Also, the
367 * sink input is first shut down, the sink second. */
368 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
369 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
370 *((pa_usec_t
*) data
) = 0;
374 *((pa_usec_t
*) data
) =
376 /* Get the latency of the master sink */
377 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
379 /* Add the latency internal to our sink input on top */
380 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
385 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
389 /* Called from main context */
390 static int source_set_state_cb(pa_source
*s
, pa_source_state_t state
) {
393 pa_source_assert_ref(s
);
394 pa_assert_se(u
= s
->userdata
);
396 if (!PA_SOURCE_IS_LINKED(state
) ||
397 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
400 pa_log_debug("Source state %d %d", state
, u
->active_mask
);
402 if (state
== PA_SOURCE_RUNNING
) {
403 /* restart timer when both sink and source are active */
405 if (u
->active_mask
== 3)
406 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
408 pa_atomic_store(&u
->request_resync
, 1);
409 pa_source_output_cork(u
->source_output
, FALSE
);
410 } else if (state
== PA_SOURCE_SUSPENDED
) {
411 u
->active_mask
&= ~1;
412 pa_source_output_cork(u
->source_output
, TRUE
);
417 /* Called from main context */
418 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
421 pa_sink_assert_ref(s
);
422 pa_assert_se(u
= s
->userdata
);
424 if (!PA_SINK_IS_LINKED(state
) ||
425 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
428 pa_log_debug("Sink state %d %d", state
, u
->active_mask
);
430 if (state
== PA_SINK_RUNNING
) {
431 /* restart timer when both sink and source are active */
433 if (u
->active_mask
== 3)
434 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
436 pa_atomic_store(&u
->request_resync
, 1);
437 pa_sink_input_cork(u
->sink_input
, FALSE
);
438 } else if (state
== PA_SINK_SUSPENDED
) {
439 u
->active_mask
&= ~2;
440 pa_sink_input_cork(u
->sink_input
, TRUE
);
445 /* Called from I/O thread context */
446 static void source_update_requested_latency_cb(pa_source
*s
) {
449 pa_source_assert_ref(s
);
450 pa_assert_se(u
= s
->userdata
);
452 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
453 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
))
456 pa_log_debug("Source update requested latency");
458 /* Just hand this one over to the master source */
459 pa_source_output_set_requested_latency_within_thread(
461 pa_source_get_requested_latency_within_thread(s
));
464 /* Called from I/O thread context */
465 static void sink_update_requested_latency_cb(pa_sink
*s
) {
468 pa_sink_assert_ref(s
);
469 pa_assert_se(u
= s
->userdata
);
471 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
472 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
475 pa_log_debug("Sink update requested latency");
477 /* Just hand this one over to the master sink */
478 pa_sink_input_set_requested_latency_within_thread(
480 pa_sink_get_requested_latency_within_thread(s
));
483 /* Called from I/O thread context */
484 static void sink_request_rewind_cb(pa_sink
*s
) {
487 pa_sink_assert_ref(s
);
488 pa_assert_se(u
= s
->userdata
);
490 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
491 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
494 pa_log_debug("Sink request rewind %lld", (long long) s
->thread_info
.rewind_nbytes
);
496 /* Just hand this one over to the master sink */
497 pa_sink_input_request_rewind(u
->sink_input
,
498 s
->thread_info
.rewind_nbytes
, TRUE
, FALSE
, FALSE
);
501 /* Called from main context */
502 static void source_set_volume_cb(pa_source
*s
) {
505 pa_source_assert_ref(s
);
506 pa_assert_se(u
= s
->userdata
);
508 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
509 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
512 pa_source_output_set_volume(u
->source_output
, &s
->real_volume
, s
->save_volume
, TRUE
);
515 /* Called from main context */
516 static void sink_set_volume_cb(pa_sink
*s
) {
519 pa_sink_assert_ref(s
);
520 pa_assert_se(u
= s
->userdata
);
522 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
523 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
526 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
529 static void source_get_volume_cb(pa_source
*s
) {
533 pa_source_assert_ref(s
);
534 pa_assert_se(u
= s
->userdata
);
536 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
537 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
540 pa_source_output_get_volume(u
->source_output
, &v
, TRUE
);
542 if (pa_cvolume_equal(&s
->real_volume
, &v
))
547 pa_source_set_soft_volume(s
, NULL
);
550 /* Called from main context */
551 static void source_set_mute_cb(pa_source
*s
) {
554 pa_source_assert_ref(s
);
555 pa_assert_se(u
= s
->userdata
);
557 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
558 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
561 pa_source_output_set_mute(u
->source_output
, s
->muted
, s
->save_muted
);
564 /* Called from main context */
565 static void sink_set_mute_cb(pa_sink
*s
) {
568 pa_sink_assert_ref(s
);
569 pa_assert_se(u
= s
->userdata
);
571 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
572 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
575 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
578 /* Called from main context */
579 static void source_get_mute_cb(pa_source
*s
) {
582 pa_source_assert_ref(s
);
583 pa_assert_se(u
= s
->userdata
);
585 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
586 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
589 pa_source_output_get_mute(u
->source_output
);
592 /* must be called from the input thread context */
593 static void apply_diff_time(struct userdata
*u
, int64_t diff_time
) {
597 diff
= pa_usec_to_bytes(-diff_time
, &u
->source_output
->sample_spec
);
600 /* add some extra safety samples to compensate for jitter in the
602 diff
+= 10 * pa_frame_size (&u
->source_output
->sample_spec
);
604 pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time
, (long long) diff
);
609 } else if (diff_time
> 0) {
610 diff
= pa_usec_to_bytes(diff_time
, &u
->source_output
->sample_spec
);
613 pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time
, (long long) diff
);
615 u
->source_skip
= diff
;
621 /* must be called from the input thread */
622 static void do_resync(struct userdata
*u
) {
624 struct snapshot latency_snapshot
;
626 pa_log("Doing resync");
628 /* update our snapshot */
629 source_output_snapshot_within_thread(u
, &latency_snapshot
);
630 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
632 /* calculate drift between capture and playback */
633 diff_time
= calc_diff(u
, &latency_snapshot
);
635 /* and adjust for the drift */
636 apply_diff_time(u
, diff_time
);
639 /* Called from input thread context */
640 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
644 pa_source_output_assert_ref(o
);
645 pa_source_output_assert_io_context(o
);
646 pa_assert_se(u
= o
->userdata
);
648 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
649 pa_log("push when no link?");
653 /* handle queued messages */
655 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
659 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
663 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
665 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
666 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
668 while (rlen
>= u
->blocksize
) {
669 pa_memchunk rchunk
, pchunk
;
671 /* take fixed block from recorded samples */
672 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
674 if (plen
> u
->blocksize
&& u
->source_skip
== 0) {
675 uint8_t *rdata
, *pdata
, *cdata
;
681 if (u
->sink_skip
> plen
)
684 to_skip
= u
->sink_skip
;
686 pa_memblockq_drop(u
->sink_memblockq
, to_skip
);
689 u
->sink_skip
-= to_skip
;
692 if (plen
> u
->blocksize
&& u
->sink_skip
== 0) {
693 /* take fixed block from played samples */
694 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, u
->blocksize
, &pchunk
);
696 rdata
= pa_memblock_acquire(rchunk
.memblock
);
697 rdata
+= rchunk
.index
;
698 pdata
= pa_memblock_acquire(pchunk
.memblock
);
699 pdata
+= pchunk
.index
;
702 cchunk
.length
= u
->blocksize
;
703 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
704 cdata
= pa_memblock_acquire(cchunk
.memblock
);
707 if (u
->captured_file
)
708 fwrite(rdata
, 1, u
->blocksize
, u
->captured_file
);
710 fwrite(pdata
, 1, u
->blocksize
, u
->played_file
);
713 /* perform echo cancellation */
714 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
717 if (u
->canceled_file
)
718 fwrite(cdata
, 1, u
->blocksize
, u
->canceled_file
);
721 pa_memblock_release(cchunk
.memblock
);
722 pa_memblock_release(pchunk
.memblock
);
723 pa_memblock_release(rchunk
.memblock
);
725 /* drop consumed sink samples */
726 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
727 pa_memblock_unref(pchunk
.memblock
);
729 pa_memblock_unref(rchunk
.memblock
);
730 /* the filtered samples now become the samples from our
734 plen
-= u
->blocksize
;
738 /* forward the (echo-canceled) data to the virtual source */
739 pa_source_post(u
->source
, &rchunk
);
740 pa_memblock_unref(rchunk
.memblock
);
742 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
743 rlen
-= u
->blocksize
;
745 if (u
->source_skip
) {
746 if (u
->source_skip
> u
->blocksize
) {
747 u
->source_skip
-= u
->blocksize
;
750 u
->sink_skip
+= (u
->blocksize
- u
->source_skip
);
757 /* Called from I/O thread context */
758 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
761 pa_sink_input_assert_ref(i
);
763 pa_assert_se(u
= i
->userdata
);
765 if (u
->sink
->thread_info
.rewind_requested
)
766 pa_sink_process_rewind(u
->sink
, 0);
768 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
770 if (i
->thread_info
.underrun_for
> 0) {
771 pa_log_debug("Handling end of underrun.");
772 pa_atomic_store(&u
->request_resync
, 1);
775 /* let source thread handle the chunk. pass the sample count as well so that
776 * the source IO thread can update the right variables. */
777 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
778 NULL
, 0, chunk
, NULL
);
779 u
->send_counter
+= chunk
->length
;
784 /* Called from input thread context */
785 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
788 pa_source_output_assert_ref(o
);
789 pa_source_output_assert_io_context(o
);
790 pa_assert_se(u
= o
->userdata
);
792 pa_source_process_rewind(u
->source
, nbytes
);
794 /* go back on read side, we need to use older sink data for this */
795 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
797 /* manipulate write index */
798 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
800 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
801 (long long) pa_memblockq_get_length (u
->source_memblockq
));
804 /* Called from I/O thread context */
805 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
808 pa_sink_input_assert_ref(i
);
809 pa_assert_se(u
= i
->userdata
);
811 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
813 pa_sink_process_rewind(u
->sink
, nbytes
);
815 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
816 u
->send_counter
-= nbytes
;
819 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
820 size_t delay
, rlen
, plen
;
821 pa_usec_t now
, latency
;
823 now
= pa_rtclock_now();
824 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
825 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
827 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
828 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
829 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
831 snapshot
->source_now
= now
;
832 snapshot
->source_latency
= latency
;
833 snapshot
->source_delay
= delay
;
834 snapshot
->recv_counter
= u
->recv_counter
;
835 snapshot
->rlen
= rlen
+ u
->sink_skip
;
836 snapshot
->plen
= plen
+ u
->source_skip
;
840 /* Called from output thread context */
841 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
842 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
846 case SOURCE_OUTPUT_MESSAGE_POST
:
848 pa_source_output_assert_io_context(u
->source_output
);
850 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
851 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
853 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
855 u
->recv_counter
+= (int64_t) chunk
->length
;
859 case SOURCE_OUTPUT_MESSAGE_REWIND
:
860 pa_source_output_assert_io_context(u
->source_output
);
862 /* manipulate write index, never go past what we have */
863 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
864 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
866 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
868 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
870 u
->recv_counter
-= offset
;
874 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
875 struct snapshot
*snapshot
= (struct snapshot
*) data
;
877 source_output_snapshot_within_thread(u
, snapshot
);
881 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
882 apply_diff_time(u
, offset
);
887 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
890 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
891 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
895 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
897 pa_usec_t now
, latency
;
898 struct snapshot
*snapshot
= (struct snapshot
*) data
;
900 pa_sink_input_assert_io_context(u
->sink_input
);
902 now
= pa_rtclock_now();
903 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
904 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
906 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
908 snapshot
->sink_now
= now
;
909 snapshot
->sink_latency
= latency
;
910 snapshot
->sink_delay
= delay
;
911 snapshot
->send_counter
= u
->send_counter
;
916 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
919 /* Called from I/O thread context */
920 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
923 pa_sink_input_assert_ref(i
);
924 pa_assert_se(u
= i
->userdata
);
926 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
928 pa_memblockq_set_maxrewind(u
->sink_memblockq
, nbytes
);
929 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
932 /* Called from I/O thread context */
933 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
936 pa_source_output_assert_ref(o
);
937 pa_assert_se(u
= o
->userdata
);
939 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
941 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
944 /* Called from I/O thread context */
945 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
948 pa_sink_input_assert_ref(i
);
949 pa_assert_se(u
= i
->userdata
);
951 pa_log_debug("Sink input update max request %lld", (long long) nbytes
);
953 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
);
956 /* Called from I/O thread context */
957 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
961 pa_sink_input_assert_ref(i
);
962 pa_assert_se(u
= i
->userdata
);
964 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
966 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
969 /* Called from I/O thread context */
970 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
974 pa_source_output_assert_ref(o
);
975 pa_assert_se(u
= o
->userdata
);
977 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
979 pa_log_debug("source output update requested latency %lld", (long long) latency
);
982 /* Called from I/O thread context */
983 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
986 pa_sink_input_assert_ref(i
);
987 pa_assert_se(u
= i
->userdata
);
989 pa_log_debug("Sink input update latency range %lld %lld",
990 (long long) i
->sink
->thread_info
.min_latency
,
991 (long long) i
->sink
->thread_info
.max_latency
);
993 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
996 /* Called from I/O thread context */
997 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
1000 pa_source_output_assert_ref(o
);
1001 pa_assert_se(u
= o
->userdata
);
1003 pa_log_debug("Source output update latency range %lld %lld",
1004 (long long) o
->source
->thread_info
.min_latency
,
1005 (long long) o
->source
->thread_info
.max_latency
);
1007 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1010 /* Called from I/O thread context */
1011 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
1014 pa_sink_input_assert_ref(i
);
1015 pa_assert_se(u
= i
->userdata
);
1017 pa_log_debug("Sink input update fixed latency %lld",
1018 (long long) i
->sink
->thread_info
.fixed_latency
);
1020 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1023 /* Called from I/O thread context */
1024 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1027 pa_source_output_assert_ref(o
);
1028 pa_assert_se(u
= o
->userdata
);
1030 pa_log_debug("Source output update fixed latency %lld",
1031 (long long) o
->source
->thread_info
.fixed_latency
);
1033 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1036 /* Called from output thread context */
1037 static void source_output_attach_cb(pa_source_output
*o
) {
1040 pa_source_output_assert_ref(o
);
1041 pa_source_output_assert_io_context(o
);
1042 pa_assert_se(u
= o
->userdata
);
1044 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1045 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1046 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1047 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1049 pa_log_debug("Source output %d attach", o
->index
);
1051 pa_source_attach_within_thread(u
->source
);
1053 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1054 o
->source
->thread_info
.rtpoll
,
1059 /* Called from I/O thread context */
1060 static void sink_input_attach_cb(pa_sink_input
*i
) {
1063 pa_sink_input_assert_ref(i
);
1064 pa_assert_se(u
= i
->userdata
);
1066 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1067 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1069 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1070 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1071 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1073 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1074 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1076 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1077 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1079 pa_log_debug("Sink input %d attach", i
->index
);
1081 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1082 i
->sink
->thread_info
.rtpoll
,
1086 pa_sink_attach_within_thread(u
->sink
);
1090 /* Called from output thread context */
1091 static void source_output_detach_cb(pa_source_output
*o
) {
1094 pa_source_output_assert_ref(o
);
1095 pa_source_output_assert_io_context(o
);
1096 pa_assert_se(u
= o
->userdata
);
1098 pa_source_detach_within_thread(u
->source
);
1099 pa_source_set_rtpoll(u
->source
, NULL
);
1101 pa_log_debug("Source output %d detach", o
->index
);
1103 if (u
->rtpoll_item_read
) {
1104 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1105 u
->rtpoll_item_read
= NULL
;
1109 /* Called from I/O thread context */
1110 static void sink_input_detach_cb(pa_sink_input
*i
) {
1113 pa_sink_input_assert_ref(i
);
1114 pa_assert_se(u
= i
->userdata
);
1116 pa_sink_detach_within_thread(u
->sink
);
1118 pa_sink_set_rtpoll(u
->sink
, NULL
);
1120 pa_log_debug("Sink input %d detach", i
->index
);
1122 if (u
->rtpoll_item_write
) {
1123 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1124 u
->rtpoll_item_write
= NULL
;
1128 /* Called from output thread context */
1129 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1132 pa_source_output_assert_ref(o
);
1133 pa_source_output_assert_io_context(o
);
1134 pa_assert_se(u
= o
->userdata
);
1136 pa_log_debug("Source output %d state %d", o
->index
, state
);
1139 /* Called from IO thread context */
1140 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1143 pa_sink_input_assert_ref(i
);
1144 pa_assert_se(u
= i
->userdata
);
1146 pa_log_debug("Sink input %d state %d", i
->index
, state
);
1148 /* If we are added for the first time, ask for a rewinding so that
1149 * we are heard right-away. */
1150 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1151 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1152 pa_log_debug("Requesting rewind due to state change.");
1153 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1157 /* Called from main thread */
1158 static void source_output_kill_cb(pa_source_output
*o
) {
1161 pa_source_output_assert_ref(o
);
1162 pa_assert_ctl_context();
1163 pa_assert_se(u
= o
->userdata
);
1165 /* The order here matters! We first kill the source output, followed
1166 * by the source. That means the source callbacks must be protected
1167 * against an unconnected source output! */
1168 pa_source_output_unlink(u
->source_output
);
1169 pa_source_unlink(u
->source
);
1171 pa_source_output_unref(u
->source_output
);
1172 u
->source_output
= NULL
;
1174 pa_source_unref(u
->source
);
1177 pa_log_debug("Source output kill %d", o
->index
);
1179 pa_module_unload_request(u
->module
, TRUE
);
1182 /* Called from main context */
1183 static void sink_input_kill_cb(pa_sink_input
*i
) {
1186 pa_sink_input_assert_ref(i
);
1187 pa_assert_se(u
= i
->userdata
);
1189 /* The order here matters! We first kill the sink input, followed
1190 * by the sink. That means the sink callbacks must be protected
1191 * against an unconnected sink input! */
1192 pa_sink_input_unlink(u
->sink_input
);
1193 pa_sink_unlink(u
->sink
);
1195 pa_sink_input_unref(u
->sink_input
);
1196 u
->sink_input
= NULL
;
1198 pa_sink_unref(u
->sink
);
1201 pa_log_debug("Sink input kill %d", i
->index
);
1203 pa_module_unload_request(u
->module
, TRUE
);
1206 /* Called from main thread */
1207 static pa_bool_t
source_output_may_move_to_cb(pa_source_output
*o
, pa_source
*dest
) {
1210 pa_source_output_assert_ref(o
);
1211 pa_assert_ctl_context();
1212 pa_assert_se(u
= o
->userdata
);
1214 return (u
->source
!= dest
) && (u
->sink
!= dest
->monitor_of
);
1217 /* Called from main context */
1218 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1221 pa_sink_input_assert_ref(i
);
1222 pa_assert_se(u
= i
->userdata
);
1224 return u
->sink
!= dest
;
1227 /* Called from main thread */
1228 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1231 pa_source_output_assert_ref(o
);
1232 pa_assert_ctl_context();
1233 pa_assert_se(u
= o
->userdata
);
1236 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1237 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1239 pa_source_set_asyncmsgq(u
->source
, NULL
);
1241 if (u
->source_auto_desc
&& dest
) {
1245 pl
= pa_proplist_new();
1246 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1247 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1248 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1250 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1251 pa_proplist_free(pl
);
1255 /* Called from main context */
1256 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1259 pa_sink_input_assert_ref(i
);
1260 pa_assert_se(u
= i
->userdata
);
1263 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1264 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1266 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1268 if (u
->sink_auto_desc
&& dest
) {
1272 pl
= pa_proplist_new();
1273 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1274 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1275 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1277 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1278 pa_proplist_free(pl
);
1282 /* Called from main context */
1283 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1286 pa_sink_input_assert_ref(i
);
1287 pa_assert_se(u
= i
->userdata
);
1289 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1292 /* Called from main context */
1293 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1296 pa_sink_input_assert_ref(i
);
1297 pa_assert_se(u
= i
->userdata
);
1299 pa_sink_mute_changed(u
->sink
, i
->muted
);
1302 static pa_echo_canceller_method_t
get_ec_method_from_string(const char *method
) {
1303 if (pa_streq(method
, "speex"))
1304 return PA_ECHO_CANCELLER_SPEEX
;
1305 else if (pa_streq(method
, "adrian"))
1306 return PA_ECHO_CANCELLER_ADRIAN
;
1308 return PA_ECHO_CANCELLER_INVALID
;
1311 int pa__init(pa_module
*m
) {
1313 pa_sample_spec source_ss
, sink_ss
;
1314 pa_channel_map source_map
, sink_map
;
1316 pa_source
*source_master
=NULL
;
1317 pa_sink
*sink_master
=NULL
;
1318 pa_source_output_new_data source_output_data
;
1319 pa_sink_input_new_data sink_input_data
;
1320 pa_source_new_data source_data
;
1321 pa_sink_new_data sink_data
;
1322 pa_memchunk silence
;
1323 pa_echo_canceller_method_t ec_method
;
1324 uint32_t adjust_time_sec
;
1328 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1329 pa_log("Failed to parse module arguments.");
1333 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1334 pa_log("Master source not found");
1337 pa_assert(source_master
);
1339 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1340 pa_log("Master sink not found");
1343 pa_assert(sink_master
);
1345 source_ss
= source_master
->sample_spec
;
1346 source_ss
.rate
= DEFAULT_RATE
;
1347 source_ss
.channels
= DEFAULT_CHANNELS
;
1348 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1349 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &source_ss
, &source_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1350 pa_log("Invalid sample format specification or channel map");
1354 sink_ss
= sink_master
->sample_spec
;
1355 sink_map
= sink_master
->channel_map
;
1357 u
= pa_xnew0(struct userdata
, 1);
1359 pa_log("Failed to alloc userdata");
1366 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1368 pa_log("Failed to alloc echo canceller");
1372 if ((ec_method
= get_ec_method_from_string(pa_modargs_get_value(ma
, "aec_method", DEFAULT_ECHO_CANCELLER
))) < 0) {
1373 pa_log("Invalid echo canceller implementation");
1377 u
->ec
->init
= ec_table
[ec_method
].init
;
1378 u
->ec
->run
= ec_table
[ec_method
].run
;
1379 u
->ec
->done
= ec_table
[ec_method
].done
;
1381 adjust_time_sec
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1382 if (pa_modargs_get_value_u32(ma
, "adjust_time", &adjust_time_sec
) < 0) {
1383 pa_log("Failed to parse adjust_time value");
1387 if (adjust_time_sec
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1388 u
->adjust_time
= adjust_time_sec
* PA_USEC_PER_SEC
;
1390 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1392 u
->save_aec
= DEFAULT_SAVE_AEC
;
1393 if (pa_modargs_get_value_u32(ma
, "save_aec", &u
->save_aec
) < 0) {
1394 pa_log("Failed to parse save_aec value");
1398 u
->autoloaded
= DEFAULT_AUTOLOADED
;
1399 if (pa_modargs_get_value_boolean(ma
, "autoloaded", &u
->autoloaded
) < 0) {
1400 pa_log("Failed to parse autoloaded value");
1404 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1405 u
->need_realign
= TRUE
;
1407 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
))) {
1408 pa_log("Failed to init AEC engine");
1414 pa_source_new_data_init(&source_data
);
1415 source_data
.driver
= __FILE__
;
1416 source_data
.module
= m
;
1417 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1418 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1419 pa_source_new_data_set_sample_spec(&source_data
, &source_ss
);
1420 pa_source_new_data_set_channel_map(&source_data
, &source_map
);
1421 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1422 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1424 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1425 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1427 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1428 pa_log("Invalid properties");
1429 pa_source_new_data_done(&source_data
);
1433 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1436 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1437 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1440 u
->source
= pa_source_new(m
->core
, &source_data
,
1441 (source_master
->flags
& (PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
)));
1442 pa_source_new_data_done(&source_data
);
1445 pa_log("Failed to create source.");
1449 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1450 u
->source
->set_state
= source_set_state_cb
;
1451 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1452 pa_source_enable_decibel_volume(u
->source
, TRUE
);
1453 pa_source_set_get_volume_callback(u
->source
, source_get_volume_cb
);
1454 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1455 pa_source_set_get_mute_callback(u
->source
, source_get_mute_cb
);
1456 pa_source_set_set_mute_callback(u
->source
, source_set_mute_cb
);
1457 u
->source
->userdata
= u
;
1459 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1462 pa_sink_new_data_init(&sink_data
);
1463 sink_data
.driver
= __FILE__
;
1464 sink_data
.module
= m
;
1465 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1466 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1467 pa_sink_new_data_set_sample_spec(&sink_data
, &sink_ss
);
1468 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
1469 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1470 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1472 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1473 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1475 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1476 pa_log("Invalid properties");
1477 pa_sink_new_data_done(&sink_data
);
1481 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1484 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1485 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1488 u
->sink
= pa_sink_new(m
->core
, &sink_data
,
1489 (sink_master
->flags
& (PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
)));
1490 pa_sink_new_data_done(&sink_data
);
1493 pa_log("Failed to create sink.");
1497 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1498 u
->sink
->set_state
= sink_set_state_cb
;
1499 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1500 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1501 pa_sink_enable_decibel_volume(u
->sink
, TRUE
);
1502 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1503 pa_sink_set_set_mute_callback(u
->sink
, sink_set_mute_cb
);
1504 u
->sink
->userdata
= u
;
1506 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1508 /* Create source output */
1509 pa_source_output_new_data_init(&source_output_data
);
1510 source_output_data
.driver
= __FILE__
;
1511 source_output_data
.module
= m
;
1512 pa_source_output_new_data_set_source(&source_output_data
, source_master
, FALSE
);
1513 source_output_data
.destination_source
= u
->source
;
1515 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1517 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1518 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1519 pa_source_output_new_data_set_sample_spec(&source_output_data
, &source_ss
);
1520 pa_source_output_new_data_set_channel_map(&source_output_data
, &source_map
);
1522 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1523 pa_source_output_new_data_done(&source_output_data
);
1525 if (!u
->source_output
)
1528 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1529 u
->source_output
->push
= source_output_push_cb
;
1530 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1531 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1532 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1533 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1534 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1535 u
->source_output
->kill
= source_output_kill_cb
;
1536 u
->source_output
->attach
= source_output_attach_cb
;
1537 u
->source_output
->detach
= source_output_detach_cb
;
1538 u
->source_output
->state_change
= source_output_state_change_cb
;
1539 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1540 u
->source_output
->moving
= source_output_moving_cb
;
1541 u
->source_output
->userdata
= u
;
1543 u
->source
->output_from_master
= u
->source_output
;
1545 /* Create sink input */
1546 pa_sink_input_new_data_init(&sink_input_data
);
1547 sink_input_data
.driver
= __FILE__
;
1548 sink_input_data
.module
= m
;
1549 pa_sink_input_new_data_set_sink(&sink_input_data
, sink_master
, FALSE
);
1550 sink_input_data
.origin_sink
= u
->sink
;
1551 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1552 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1553 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_ss
);
1554 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_map
);
1555 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1557 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1558 pa_sink_input_new_data_done(&sink_input_data
);
1563 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1564 u
->sink_input
->pop
= sink_input_pop_cb
;
1565 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1566 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1567 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1568 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1569 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1570 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1571 u
->sink_input
->kill
= sink_input_kill_cb
;
1572 u
->sink_input
->attach
= sink_input_attach_cb
;
1573 u
->sink_input
->detach
= sink_input_detach_cb
;
1574 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1575 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1576 u
->sink_input
->moving
= sink_input_moving_cb
;
1577 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1578 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1579 u
->sink_input
->userdata
= u
;
1581 u
->sink
->input_to_master
= u
->sink_input
;
1583 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1585 u
->source_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1586 pa_frame_size(&source_ss
), 1, 1, 0, &silence
);
1587 u
->sink_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1588 pa_frame_size(&sink_ss
), 1, 1, 0, &silence
);
1590 pa_memblock_unref(silence
.memblock
);
1592 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1593 pa_log("Failed to create memblockq.");
1597 /* our source and sink are not suspended when we create them */
1600 if (u
->adjust_time
> 0)
1601 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1604 pa_log("Creating AEC files in /tmp");
1605 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1606 if (u
->captured_file
== NULL
)
1607 perror ("fopen failed");
1608 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1609 if (u
->played_file
== NULL
)
1610 perror ("fopen failed");
1611 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1612 if (u
->canceled_file
== NULL
)
1613 perror ("fopen failed");
1616 pa_sink_put(u
->sink
);
1617 pa_source_put(u
->source
);
1619 pa_sink_input_put(u
->sink_input
);
1620 pa_source_output_put(u
->source_output
);
1622 pa_modargs_free(ma
);
1628 pa_modargs_free(ma
);
1635 int pa__get_n_used(pa_module
*m
) {
1639 pa_assert_se(u
= m
->userdata
);
1641 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1644 void pa__done(pa_module
*m
) {
1649 if (!(u
= m
->userdata
))
1652 /* See comments in source_output_kill_cb() above regarding
1653 * destruction order! */
1656 u
->core
->mainloop
->time_free(u
->time_event
);
1658 if (u
->source_output
)
1659 pa_source_output_unlink(u
->source_output
);
1661 pa_sink_input_unlink(u
->sink_input
);
1664 pa_source_unlink(u
->source
);
1666 pa_sink_unlink(u
->sink
);
1668 if (u
->source_output
)
1669 pa_source_output_unref(u
->source_output
);
1671 pa_sink_input_unref(u
->sink_input
);
1674 pa_source_unref(u
->source
);
1676 pa_sink_unref(u
->sink
);
1678 if (u
->source_memblockq
)
1679 pa_memblockq_free(u
->source_memblockq
);
1680 if (u
->sink_memblockq
)
1681 pa_memblockq_free(u
->sink_memblockq
);
1691 pa_asyncmsgq_unref(u
->asyncmsgq
);