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
36 #include "echo-cancel.h"
38 #include <pulse/xmalloc.h>
39 #include <pulse/i18n.h>
40 #include <pulse/timeval.h>
41 #include <pulse/rtclock.h>
43 #include <pulsecore/atomic.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/core-error.h>
46 #include <pulsecore/namereg.h>
47 #include <pulsecore/sink.h>
48 #include <pulsecore/module.h>
49 #include <pulsecore/core-rtclock.h>
50 #include <pulsecore/core-util.h>
51 #include <pulsecore/core-error.h>
52 #include <pulsecore/modargs.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/thread.h>
55 #include <pulsecore/thread-mq.h>
56 #include <pulsecore/rtpoll.h>
57 #include <pulsecore/sample-util.h>
58 #include <pulsecore/ltdl-helper.h>
60 #include "module-echo-cancel-symdef.h"
62 PA_MODULE_AUTHOR("Wim Taymans");
63 PA_MODULE_DESCRIPTION("Echo Cancelation");
64 PA_MODULE_VERSION(PACKAGE_VERSION
);
65 PA_MODULE_LOAD_ONCE(FALSE
);
67 _("source_name=<name for the source> "
68 "source_properties=<properties for the source> "
69 "source_master=<name of source to filter> "
70 "sink_name=<name for the sink> "
71 "sink_properties=<properties for the sink> "
72 "sink_master=<name of sink to filter> "
73 "frame_size_ms=<amount of data to process at one time> "
74 "filter_size_ms=<amount of echo to cancel> "
75 "adjust_time=<how often to readjust rates in s> "
76 "format=<sample format> "
78 "channels=<number of channels> "
79 "channel_map=<channel map> "
80 "save_aec=<save AEC data in /tmp> "
83 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
85 PA_ECHO_CANCELLER_SPEEX
,
88 #define DEFAULT_ECHO_CANCELLER PA_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
,
96 .get_block_size
= pa_speex_ec_get_block_size
,
100 /* should be between 10-20 ms */
101 #define DEFAULT_FRAME_SIZE_MS 20
102 /* should be between 100-500 ms */
103 #define DEFAULT_FILTER_SIZE_MS 200
105 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
106 #define DEFAULT_SAVE_AEC 0
108 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
110 /* This module creates a new (virtual) source and sink.
112 * The data sent to the new sink is kept in a memblockq before being
113 * forwarded to the real sink_master.
115 * Data read from source_master is matched against the saved sink data and
116 * echo canceled data is then pushed onto the new source.
118 * Both source and sink masters have their own threads to push/pull data
119 * respectively. We however perform all our actions in the source IO thread.
120 * To do this we send all played samples to the source IO thread where they
121 * are then pushed into the memblockq.
123 * Alignment is performed in two steps:
125 * 1) when something happens that requires quick adjustement of the alignment of
126 * capture and playback samples, we perform a resync. This adjusts the
127 * position in the playback memblock to the requested sample. Quick
128 * adjustements include moving the playback samples before the capture
129 * samples (because else the echo canceler does not work) or when the
130 * playback pointer drifts too far away.
132 * 2) periodically check the difference between capture and playback. we use a
133 * low and high watermark for adjusting the alignment. playback should always
134 * be before capture and the difference should not be bigger than one frame
135 * size. We would ideally like to resample the sink_input but most driver
136 * don't give enough accuracy to be able to do that right now.
141 pa_usec_t sink_latency
;
143 int64_t send_counter
;
145 pa_usec_t source_now
;
146 pa_usec_t source_latency
;
148 int64_t recv_counter
;
157 uint32_t frame_size_ms
;
160 pa_echo_canceller
*ec
;
162 pa_bool_t need_realign
;
164 /* to wakeup the source I/O thread */
166 pa_asyncmsgq
*asyncmsgq
;
167 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
170 pa_bool_t source_auto_desc
;
171 pa_source_output
*source_output
;
172 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
173 pa_atomic_t source_active
;
176 pa_bool_t sink_auto_desc
;
177 pa_sink_input
*sink_input
;
178 pa_memblockq
*sink_memblockq
;
179 int64_t send_counter
; /* updated in sink IO thread */
180 int64_t recv_counter
;
181 pa_atomic_t sink_active
;
183 pa_atomic_t request_resync
;
185 pa_time_event
*time_event
;
186 pa_usec_t adjust_time
;
193 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
195 static const char* const valid_modargs
[] = {
214 SOURCE_OUTPUT_MESSAGE_POST
= PA_SOURCE_OUTPUT_MESSAGE_MAX
,
215 SOURCE_OUTPUT_MESSAGE_REWIND
,
216 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
,
217 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
221 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
224 static int64_t calc_diff(struct userdata
*u
, struct snapshot
*snapshot
) {
225 int64_t buffer
, diff_time
, buffer_latency
;
227 /* get the number of samples between capture and playback */
228 if (snapshot
->plen
> snapshot
->rlen
)
229 buffer
= snapshot
->plen
- snapshot
->rlen
;
233 buffer
+= snapshot
->source_delay
+ snapshot
->sink_delay
;
235 /* add the amount of samples not yet transfered to the source context */
236 if (snapshot
->recv_counter
<= snapshot
->send_counter
)
237 buffer
+= (int64_t) (snapshot
->send_counter
- snapshot
->recv_counter
);
239 buffer
+= PA_CLIP_SUB(buffer
, (int64_t) (snapshot
->recv_counter
- snapshot
->send_counter
));
241 /* convert to time */
242 buffer_latency
= pa_bytes_to_usec(buffer
, &u
->source_output
->sample_spec
);
244 /* capture and playback samples are perfectly aligned when diff_time is 0 */
245 diff_time
= (snapshot
->sink_now
+ snapshot
->sink_latency
- buffer_latency
) -
246 (snapshot
->source_now
- snapshot
->source_latency
);
248 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time
,
249 (long long) snapshot
->sink_latency
,
250 (long long) buffer_latency
, (long long) snapshot
->source_latency
,
251 (long long) snapshot
->source_delay
, (long long) snapshot
->sink_delay
,
252 (long long) (snapshot
->send_counter
- snapshot
->recv_counter
),
253 (long long) (snapshot
->sink_now
- snapshot
->source_now
));
258 /* Called from main context */
259 static void time_callback(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
260 struct userdata
*u
= userdata
;
261 uint32_t old_rate
, base_rate
, new_rate
;
264 struct snapshot latency_snapshot
;
268 pa_assert(u
->time_event
== e
);
269 pa_assert_ctl_context();
271 if (pa_atomic_load (&u
->sink_active
) == 0 || pa_atomic_load (&u
->source_active
) == 0)
274 /* update our snapshots */
275 pa_asyncmsgq_send(u
->source_output
->source
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
276 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
278 /* calculate drift between capture and playback */
279 diff_time
= calc_diff(u
, &latency_snapshot
);
281 fs
= pa_frame_size(&u
->source_output
->sample_spec
);
282 old_rate
= u
->sink_input
->sample_spec
.rate
;
283 base_rate
= u
->source_output
->sample_spec
.rate
;
286 /* recording before playback, we need to adjust quickly. The echo
287 * canceler does not work in this case. */
288 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
289 NULL
, diff_time
, NULL
, NULL
);
290 //new_rate = base_rate - ((pa_usec_to_bytes (-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;
291 new_rate
= base_rate
;
294 if (diff_time
> 4000) {
295 /* diff too big, quickly adjust */
296 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
297 NULL
, diff_time
, NULL
, NULL
);
300 /* recording behind playback, we need to slowly adjust the rate to match */
301 //new_rate = base_rate + ((pa_usec_to_bytes (diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;
303 /* assume equal samplerates for now */
304 new_rate
= base_rate
;
307 /* make sure we don't make too big adjustements because that sounds horrible */
308 if (new_rate
> base_rate
* 1.1 || new_rate
< base_rate
* 0.9)
309 new_rate
= base_rate
;
311 if (new_rate
!= old_rate
) {
312 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate
, (unsigned long) new_rate
);
314 pa_sink_input_set_rate(u
->sink_input
, new_rate
);
318 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
321 /* Called from source I/O thread context */
322 static int source_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
323 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
327 case PA_SOURCE_MESSAGE_GET_LATENCY
:
329 /* The source is _put() before the source output is, so let's
330 * make sure we don't access it in that time. Also, the
331 * source output is first shut down, the source second. */
332 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
333 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
)) {
334 *((pa_usec_t
*) data
) = 0;
338 *((pa_usec_t
*) data
) =
340 /* Get the latency of the master source */
341 pa_source_get_latency_within_thread(u
->source_output
->source
) +
342 /* Add the latency internal to our source output on top */
343 pa_bytes_to_usec(pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
), &u
->source_output
->source
->sample_spec
) +
344 /* and the buffering we do on the source */
345 pa_bytes_to_usec(u
->ec
->get_block_size(u
->ec
), &u
->source_output
->source
->sample_spec
);
351 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
354 /* Called from sink I/O thread context */
355 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
356 struct userdata
*u
= PA_SINK(o
)->userdata
;
360 case PA_SINK_MESSAGE_GET_LATENCY
:
362 /* The sink is _put() before the sink input is, so let's
363 * make sure we don't access it in that time. Also, the
364 * sink input is first shut down, the sink second. */
365 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
366 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
367 *((pa_usec_t
*) data
) = 0;
371 *((pa_usec_t
*) data
) =
373 /* Get the latency of the master sink */
374 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
376 /* Add the latency internal to our sink input on top */
377 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
382 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
386 /* Called from main context */
387 static int source_set_state_cb(pa_source
*s
, pa_source_state_t state
) {
390 pa_source_assert_ref(s
);
391 pa_assert_se(u
= s
->userdata
);
393 if (!PA_SOURCE_IS_LINKED(state
) ||
394 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
397 pa_log_debug("Source state %d", state
);
399 if (state
== PA_SOURCE_RUNNING
) {
400 pa_atomic_store (&u
->source_active
, 1);
401 pa_atomic_store (&u
->request_resync
, 1);
402 pa_source_output_cork(u
->source_output
, FALSE
);
403 } else if (state
== PA_SOURCE_SUSPENDED
) {
404 pa_atomic_store (&u
->source_active
, 0);
405 pa_source_output_cork(u
->source_output
, TRUE
);
410 /* Called from main context */
411 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
414 pa_sink_assert_ref(s
);
415 pa_assert_se(u
= s
->userdata
);
417 if (!PA_SINK_IS_LINKED(state
) ||
418 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
421 pa_log_debug("Sink state %d", state
);
423 if (state
== PA_SINK_RUNNING
) {
424 pa_atomic_store (&u
->sink_active
, 1);
425 pa_atomic_store (&u
->request_resync
, 1);
426 pa_sink_input_cork(u
->sink_input
, FALSE
);
427 } else if (state
== PA_SINK_SUSPENDED
) {
428 pa_atomic_store (&u
->sink_active
, 0);
429 pa_sink_input_cork(u
->sink_input
, TRUE
);
434 /* Called from I/O thread context */
435 static void source_update_requested_latency_cb(pa_source
*s
) {
438 pa_source_assert_ref(s
);
439 pa_assert_se(u
= s
->userdata
);
441 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
442 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
))
445 pa_log_debug("Source update requested latency");
447 /* Just hand this one over to the master source */
448 pa_source_output_set_requested_latency_within_thread(
450 pa_source_get_requested_latency_within_thread(s
));
453 /* Called from I/O thread context */
454 static void sink_update_requested_latency_cb(pa_sink
*s
) {
457 pa_sink_assert_ref(s
);
458 pa_assert_se(u
= s
->userdata
);
460 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
461 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
464 pa_log_debug("Sink update requested latency");
466 /* Just hand this one over to the master sink */
467 pa_sink_input_set_requested_latency_within_thread(
469 pa_sink_get_requested_latency_within_thread(s
));
472 /* Called from I/O thread context */
473 static void sink_request_rewind_cb(pa_sink
*s
) {
476 pa_sink_assert_ref(s
);
477 pa_assert_se(u
= s
->userdata
);
479 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
480 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
483 pa_log_debug("Sink request rewind %lld", (long long) s
->thread_info
.rewind_nbytes
);
485 /* Just hand this one over to the master sink */
486 pa_sink_input_request_rewind(u
->sink_input
,
487 s
->thread_info
.rewind_nbytes
, TRUE
, FALSE
, FALSE
);
490 /* Called from main context */
491 static void source_set_volume_cb(pa_source
*s
) {
494 pa_source_assert_ref(s
);
495 pa_assert_se(u
= s
->userdata
);
497 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
498 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
501 /* FIXME, no volume control in source_output, set volume at the master */
502 pa_source_set_volume(u
->source_output
->source
, &s
->volume
, TRUE
);
505 /* Called from main context */
506 static void sink_set_volume_cb(pa_sink
*s
) {
509 pa_sink_assert_ref(s
);
510 pa_assert_se(u
= s
->userdata
);
512 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
513 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
516 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
519 static void source_get_volume_cb(pa_source
*s
) {
522 pa_source_assert_ref(s
);
523 pa_assert_se(u
= s
->userdata
);
525 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
526 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
529 /* FIXME, no volume control in source_output, get the info from the master */
530 pa_source_get_volume(u
->source_output
->source
, TRUE
);
532 if (pa_cvolume_equal(&s
->volume
,&u
->source_output
->source
->volume
))
536 s
->volume
= u
->source_output
->source
->volume
;
537 pa_source_set_soft_volume(s
, NULL
);
541 /* Called from main context */
542 static void source_set_mute_cb(pa_source
*s
) {
545 pa_source_assert_ref(s
);
546 pa_assert_se(u
= s
->userdata
);
548 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
549 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
552 /* FIXME, no volume control in source_output, set mute at the master */
553 pa_source_set_mute(u
->source_output
->source
, TRUE
, TRUE
);
556 /* Called from main context */
557 static void sink_set_mute_cb(pa_sink
*s
) {
560 pa_sink_assert_ref(s
);
561 pa_assert_se(u
= s
->userdata
);
563 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
564 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
567 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
570 /* Called from main context */
571 static void source_get_mute_cb(pa_source
*s
) {
574 pa_source_assert_ref(s
);
575 pa_assert_se(u
= s
->userdata
);
577 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
578 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
581 /* FIXME, no volume control in source_output, get the info from the master */
582 pa_source_get_mute(u
->source_output
->source
, TRUE
);
585 /* must be called from the input thread context */
586 static void apply_diff_time(struct userdata
*u
, int64_t diff_time
) {
590 diff
= pa_usec_to_bytes (-diff_time
, &u
->source_output
->sample_spec
);
593 pa_log_info("Playback after capture (%lld), drop sink %lld", (long long) diff_time
, (long long) diff
);
595 /* go forwards on the read side */
596 pa_memblockq_drop(u
->sink_memblockq
, diff
);
598 } else if (diff_time
> 0) {
599 diff
= pa_usec_to_bytes (diff_time
, &u
->source_output
->sample_spec
);
602 pa_log_info("playback too far ahead (%lld), drop source %lld", (long long) diff_time
, (long long) diff
);
604 /* go back on the read side */
605 pa_memblockq_rewind(u
->sink_memblockq
, diff
);
610 /* must be called from the input thread */
611 static void do_resync(struct userdata
*u
) {
613 struct snapshot latency_snapshot
;
615 pa_log("Doing resync");
617 /* update our snapshot */
618 source_output_snapshot_within_thread(u
, &latency_snapshot
);
619 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
621 /* calculate drift between capture and playback */
622 diff_time
= calc_diff(u
, &latency_snapshot
);
624 /* and adjust for the drift */
625 apply_diff_time(u
, diff_time
);
628 /* Called from input thread context */
629 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
634 pa_source_output_assert_ref(o
);
635 pa_source_output_assert_io_context(o
);
636 pa_assert_se(u
= o
->userdata
);
638 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
639 pa_log("push when no link?");
643 /* handle queued messages */
645 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
649 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
653 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
655 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
656 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
658 blocksize
= u
->ec
->get_block_size(u
->ec
);
660 while (rlen
>= blocksize
) {
661 pa_memchunk rchunk
, pchunk
;
663 /* take fixed block from recorded samples */
664 pa_memblockq_peek_fixed_size(u
->source_memblockq
, blocksize
, &rchunk
);
666 if (plen
> blocksize
) {
667 uint8_t *rdata
, *pdata
, *cdata
;
670 /* take fixed block from played samples */
671 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, blocksize
, &pchunk
);
673 rdata
= pa_memblock_acquire(rchunk
.memblock
);
674 rdata
+= rchunk
.index
;
675 pdata
= pa_memblock_acquire(pchunk
.memblock
);
676 pdata
+= pchunk
.index
;
679 cchunk
.length
= blocksize
;
680 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
681 cdata
= pa_memblock_acquire(cchunk
.memblock
);
683 /* perform echo cancelation */
684 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
687 if (u
->captured_file
)
688 fwrite(rdata
, 1, blocksize
, u
->captured_file
);
690 fwrite(pdata
, 1, blocksize
, u
->played_file
);
691 if (u
->canceled_file
)
692 fwrite(cdata
, 1, blocksize
, u
->canceled_file
);
693 pa_log_debug("AEC frame saved.");
696 pa_memblock_release(cchunk
.memblock
);
697 pa_memblock_release(pchunk
.memblock
);
698 pa_memblock_release(rchunk
.memblock
);
700 /* drop consumed sink samples */
701 pa_memblockq_drop(u
->sink_memblockq
, blocksize
);
702 pa_memblock_unref(pchunk
.memblock
);
704 pa_memblock_unref(rchunk
.memblock
);
705 /* the filtered samples now become the samples from our
711 /* not enough played samples to perform echo cancelation,
712 * drop what we have */
713 pa_memblockq_drop(u
->sink_memblockq
, blocksize
- plen
);
717 /* forward the (echo-canceled) data to the virtual source */
718 pa_source_post(u
->source
, &rchunk
);
719 pa_memblock_unref(rchunk
.memblock
);
721 pa_memblockq_drop(u
->source_memblockq
, blocksize
);
727 /* Called from I/O thread context */
728 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
731 pa_sink_input_assert_ref(i
);
733 pa_assert_se(u
= i
->userdata
);
735 if (u
->sink
->thread_info
.rewind_requested
)
736 pa_sink_process_rewind(u
->sink
, 0);
738 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
740 if (i
->thread_info
.underrun_for
> 0) {
741 pa_log_debug("Handling end of underrun.");
742 pa_atomic_store (&u
->request_resync
, 1);
745 /* let source thread handle the chunk. pass the sample count as well so that
746 * the source IO thread can update the right variables. */
747 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
748 NULL
, 0, chunk
, NULL
);
749 u
->send_counter
+= chunk
->length
;
754 /* Called from input thread context */
755 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
758 pa_source_output_assert_ref(o
);
759 pa_source_output_assert_io_context(o
);
760 pa_assert_se(u
= o
->userdata
);
762 pa_source_process_rewind(u
->source
, nbytes
);
764 /* go back on read side, we need to use older sink data for this */
765 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
767 /* manipulate write index */
768 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
770 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
771 (long long) pa_memblockq_get_length (u
->source_memblockq
));
774 /* Called from I/O thread context */
775 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
779 pa_sink_input_assert_ref(i
);
780 pa_assert_se(u
= i
->userdata
);
782 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
784 if (u
->sink
->thread_info
.rewind_nbytes
> 0) {
785 amount
= PA_MIN(u
->sink
->thread_info
.rewind_nbytes
, nbytes
);
786 u
->sink
->thread_info
.rewind_nbytes
= 0;
789 pa_sink_process_rewind(u
->sink
, amount
);
791 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
792 u
->send_counter
-= nbytes
;
795 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
796 size_t delay
, rlen
, plen
;
797 pa_usec_t now
, latency
;
799 now
= pa_rtclock_now();
800 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
801 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
803 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
804 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
805 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
807 snapshot
->source_now
= now
;
808 snapshot
->source_latency
= latency
;
809 snapshot
->source_delay
= delay
;
810 snapshot
->recv_counter
= u
->recv_counter
;
811 snapshot
->rlen
= rlen
;
812 snapshot
->plen
= plen
;
816 /* Called from output thread context */
817 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
818 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
822 case SOURCE_OUTPUT_MESSAGE_POST
:
824 pa_source_output_assert_io_context(u
->source_output
);
826 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
827 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
829 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
831 u
->recv_counter
+= (int64_t) chunk
->length
;
835 case SOURCE_OUTPUT_MESSAGE_REWIND
:
836 pa_source_output_assert_io_context(u
->source_output
);
838 /* manipulate write index, never go past what we have */
839 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
840 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
842 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
844 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
846 u
->recv_counter
-= offset
;
850 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
851 struct snapshot
*snapshot
= (struct snapshot
*) data
;
853 source_output_snapshot_within_thread(u
, snapshot
);
857 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
858 apply_diff_time(u
, offset
);
863 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
866 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
867 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
871 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
873 pa_usec_t now
, latency
;
874 struct snapshot
*snapshot
= (struct snapshot
*) data
;
876 pa_sink_input_assert_io_context(u
->sink_input
);
878 now
= pa_rtclock_now();
879 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
880 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
882 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
884 snapshot
->sink_now
= now
;
885 snapshot
->sink_latency
= latency
;
886 snapshot
->sink_delay
= delay
;
887 snapshot
->send_counter
= u
->send_counter
;
892 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
895 /* Called from I/O thread context */
896 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
899 pa_sink_input_assert_ref(i
);
900 pa_assert_se(u
= i
->userdata
);
902 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
904 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
907 /* Called from I/O thread context */
908 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
911 pa_source_output_assert_ref(o
);
912 pa_assert_se(u
= o
->userdata
);
914 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
916 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
919 /* Called from I/O thread context */
920 static void sink_input_update_max_request_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_sink_set_max_request_within_thread(u
->sink
, nbytes
);
931 /* Called from I/O thread context */
932 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
936 pa_sink_input_assert_ref(i
);
937 pa_assert_se(u
= i
->userdata
);
939 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
941 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
944 /* Called from I/O thread context */
945 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
949 pa_source_output_assert_ref(o
);
950 pa_assert_se(u
= o
->userdata
);
952 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
954 pa_log_debug("source output update requested latency %lld", (long long) latency
);
957 /* Called from I/O thread context */
958 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
961 pa_sink_input_assert_ref(i
);
962 pa_assert_se(u
= i
->userdata
);
964 pa_log_debug("Sink input update latency range %lld %lld",
965 (long long) i
->sink
->thread_info
.min_latency
,
966 (long long) i
->sink
->thread_info
.max_latency
);
968 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
971 /* Called from I/O thread context */
972 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
975 pa_source_output_assert_ref(o
);
976 pa_assert_se(u
= o
->userdata
);
978 pa_log_debug("Source output update latency range %lld %lld",
979 (long long) o
->source
->thread_info
.min_latency
,
980 (long long) o
->source
->thread_info
.max_latency
);
982 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
985 /* Called from I/O thread context */
986 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
989 pa_sink_input_assert_ref(i
);
990 pa_assert_se(u
= i
->userdata
);
992 pa_log_debug("Sink input update fixed latency %lld",
993 (long long) i
->sink
->thread_info
.fixed_latency
);
995 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
998 /* Called from I/O thread context */
999 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1002 pa_source_output_assert_ref(o
);
1003 pa_assert_se(u
= o
->userdata
);
1005 pa_log_debug("Source output update fixed latency %lld",
1006 (long long) o
->source
->thread_info
.fixed_latency
);
1008 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1011 /* Called from output thread context */
1012 static void source_output_attach_cb(pa_source_output
*o
) {
1015 pa_source_output_assert_ref(o
);
1016 pa_source_output_assert_io_context(o
);
1017 pa_assert_se(u
= o
->userdata
);
1019 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1020 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1021 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1022 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1024 pa_log_debug("Source output %p attach", o
);
1026 pa_source_attach_within_thread(u
->source
);
1028 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1029 o
->source
->thread_info
.rtpoll
,
1034 /* Called from I/O thread context */
1035 static void sink_input_attach_cb(pa_sink_input
*i
) {
1038 pa_sink_input_assert_ref(i
);
1039 pa_assert_se(u
= i
->userdata
);
1041 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1042 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1044 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1045 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1046 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1048 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1049 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1051 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1052 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1054 pa_log_debug("Sink input %p attach", i
);
1056 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1057 i
->sink
->thread_info
.rtpoll
,
1061 pa_sink_attach_within_thread(u
->sink
);
1065 /* Called from output thread context */
1066 static void source_output_detach_cb(pa_source_output
*o
) {
1069 pa_source_output_assert_ref(o
);
1070 pa_source_output_assert_io_context(o
);
1071 pa_assert_se(u
= o
->userdata
);
1073 pa_source_detach_within_thread(u
->source
);
1074 pa_source_set_rtpoll(u
->source
, NULL
);
1076 pa_log_debug("Source output %p detach", o
);
1078 if (u
->rtpoll_item_read
) {
1079 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1080 u
->rtpoll_item_read
= NULL
;
1084 /* Called from I/O thread context */
1085 static void sink_input_detach_cb(pa_sink_input
*i
) {
1088 pa_sink_input_assert_ref(i
);
1089 pa_assert_se(u
= i
->userdata
);
1091 pa_sink_detach_within_thread(u
->sink
);
1093 pa_sink_set_rtpoll(u
->sink
, NULL
);
1095 pa_log_debug("Sink input %p detach", i
);
1097 if (u
->rtpoll_item_write
) {
1098 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1099 u
->rtpoll_item_write
= NULL
;
1103 /* Called from output thread context */
1104 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1107 pa_source_output_assert_ref(o
);
1108 pa_source_output_assert_io_context(o
);
1109 pa_assert_se(u
= o
->userdata
);
1111 pa_log_debug("Source output %p state %d", o
, state
);
1114 /* Called from IO thread context */
1115 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1118 pa_sink_input_assert_ref(i
);
1119 pa_assert_se(u
= i
->userdata
);
1121 pa_log_debug("Sink input %p state %d", i
, state
);
1123 /* If we are added for the first time, ask for a rewinding so that
1124 * we are heard right-away. */
1125 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1126 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1127 pa_log_debug("Requesting rewind due to state change.");
1128 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1132 /* Called from main thread */
1133 static void source_output_kill_cb(pa_source_output
*o
) {
1136 pa_source_output_assert_ref(o
);
1137 pa_assert_ctl_context();
1138 pa_assert_se(u
= o
->userdata
);
1140 /* The order here matters! We first kill the source output, followed
1141 * by the source. That means the source callbacks must be protected
1142 * against an unconnected source output! */
1143 pa_source_output_unlink(u
->source_output
);
1144 pa_source_unlink(u
->source
);
1146 pa_source_output_unref(u
->source_output
);
1147 u
->source_output
= NULL
;
1149 pa_source_unref(u
->source
);
1152 pa_log_debug("Source output kill %p", o
);
1154 pa_module_unload_request(u
->module
, TRUE
);
1157 /* Called from main context */
1158 static void sink_input_kill_cb(pa_sink_input
*i
) {
1161 pa_sink_input_assert_ref(i
);
1162 pa_assert_se(u
= i
->userdata
);
1164 /* The order here matters! We first kill the sink input, followed
1165 * by the sink. That means the sink callbacks must be protected
1166 * against an unconnected sink input! */
1167 pa_sink_input_unlink(u
->sink_input
);
1168 pa_sink_unlink(u
->sink
);
1170 pa_sink_input_unref(u
->sink_input
);
1171 u
->sink_input
= NULL
;
1173 pa_sink_unref(u
->sink
);
1176 pa_log_debug("Sink input kill %p", i
);
1178 pa_module_unload_request(u
->module
, TRUE
);
1181 /* Called from main thread */
1182 static pa_bool_t
source_output_may_move_to_cb(pa_source_output
*o
, pa_source
*dest
) {
1185 pa_source_output_assert_ref(o
);
1186 pa_assert_ctl_context();
1187 pa_assert_se(u
= o
->userdata
);
1192 /* Called from main context */
1193 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1196 pa_sink_input_assert_ref(i
);
1197 pa_assert_se(u
= i
->userdata
);
1199 return u
->sink
!= dest
;
1202 /* Called from main thread */
1203 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1206 pa_source_output_assert_ref(o
);
1207 pa_assert_ctl_context();
1208 pa_assert_se(u
= o
->userdata
);
1211 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1212 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1214 pa_source_set_asyncmsgq(u
->source
, NULL
);
1216 if (u
->source_auto_desc
&& dest
) {
1220 pl
= pa_proplist_new();
1221 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1222 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1223 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1225 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1226 pa_proplist_free(pl
);
1230 /* Called from main context */
1231 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1234 pa_sink_input_assert_ref(i
);
1235 pa_assert_se(u
= i
->userdata
);
1238 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1239 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1241 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1243 if (u
->sink_auto_desc
&& dest
) {
1247 pl
= pa_proplist_new();
1248 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1249 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1250 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1252 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1253 pa_proplist_free(pl
);
1257 /* Called from main context */
1258 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1261 pa_sink_input_assert_ref(i
);
1262 pa_assert_se(u
= i
->userdata
);
1264 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1267 /* Called from main context */
1268 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1271 pa_sink_input_assert_ref(i
);
1272 pa_assert_se(u
= i
->userdata
);
1274 pa_sink_mute_changed(u
->sink
, i
->muted
);
1278 int pa__init(pa_module
*m
) {
1283 pa_source
*source_master
=NULL
;
1284 pa_sink
*sink_master
=NULL
;
1285 pa_source_output_new_data source_output_data
;
1286 pa_sink_input_new_data sink_input_data
;
1287 pa_source_new_data source_data
;
1288 pa_sink_new_data sink_data
;
1289 pa_memchunk silence
;
1290 uint32_t frame_size_ms
, filter_size_ms
;
1291 uint32_t adjust_time_sec
;
1295 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1296 pa_log("Failed to parse module arguments.");
1300 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1301 pa_log("Master source not found");
1304 pa_assert(source_master
);
1306 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1307 pa_log("Master sink not found");
1310 pa_assert(sink_master
);
1312 frame_size_ms
= DEFAULT_FRAME_SIZE_MS
;
1313 if (pa_modargs_get_value_u32(ma
, "frame_size_ms", &frame_size_ms
) < 0 || frame_size_ms
< 1 || frame_size_ms
> 200) {
1314 pa_log("Invalid frame_size_ms specification");
1318 filter_size_ms
= DEFAULT_FILTER_SIZE_MS
;
1319 if (pa_modargs_get_value_u32(ma
, "filter_size_ms", &filter_size_ms
) < 0 || filter_size_ms
< 1 || filter_size_ms
> 2000) {
1320 pa_log("Invalid filter_size_ms specification");
1324 ss
= source_master
->sample_spec
;
1325 ss
.format
= PA_SAMPLE_S16LE
;
1326 map
= source_master
->channel_map
;
1327 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1328 pa_log("Invalid sample format specification or channel map");
1332 u
= pa_xnew0(struct userdata
, 1);
1334 pa_log("Failed to alloc userdata");
1340 u
->frame_size_ms
= frame_size_ms
;
1342 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1344 pa_log("Failed to alloc echo canceller");
1347 u
->ec
->init
= ec_table
[DEFAULT_ECHO_CANCELLER
].init
;
1348 u
->ec
->run
= ec_table
[DEFAULT_ECHO_CANCELLER
].run
;
1349 u
->ec
->done
= ec_table
[DEFAULT_ECHO_CANCELLER
].done
;
1350 u
->ec
->get_block_size
= ec_table
[DEFAULT_ECHO_CANCELLER
].get_block_size
;
1352 adjust_time_sec
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1353 if (pa_modargs_get_value_u32(ma
, "adjust_time", &adjust_time_sec
) < 0) {
1354 pa_log("Failed to parse adjust_time value");
1358 if (adjust_time_sec
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1359 u
->adjust_time
= adjust_time_sec
* PA_USEC_PER_SEC
;
1361 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1363 u
->save_aec
= DEFAULT_SAVE_AEC
;
1364 if (pa_modargs_get_value_u32(ma
, "save_aec", &u
->save_aec
) < 0) {
1365 pa_log("Failed to parse save_aec value");
1369 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1370 u
->need_realign
= TRUE
;
1372 if (!u
->ec
->init(u
->ec
, ss
, map
, filter_size_ms
, frame_size_ms
)) {
1373 pa_log("Failed to init AEC engine");
1379 pa_source_new_data_init(&source_data
);
1380 source_data
.driver
= __FILE__
;
1381 source_data
.module
= m
;
1382 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1383 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1384 pa_source_new_data_set_sample_spec(&source_data
, &ss
);
1385 pa_source_new_data_set_channel_map(&source_data
, &map
);
1386 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1387 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1388 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1389 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1391 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1392 pa_log("Invalid properties");
1393 pa_source_new_data_done(&source_data
);
1397 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1400 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1401 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1404 u
->source
= pa_source_new(m
->core
, &source_data
,
1405 PA_SOURCE_HW_MUTE_CTRL
|PA_SOURCE_HW_VOLUME_CTRL
|PA_SOURCE_DECIBEL_VOLUME
|
1406 (source_master
->flags
& (PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
)));
1407 pa_source_new_data_done(&source_data
);
1410 pa_log("Failed to create source.");
1414 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1415 u
->source
->set_state
= source_set_state_cb
;
1416 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1417 u
->source
->set_volume
= source_set_volume_cb
;
1418 u
->source
->set_mute
= source_set_mute_cb
;
1419 u
->source
->get_volume
= source_get_volume_cb
;
1420 u
->source
->get_mute
= source_get_mute_cb
;
1421 u
->source
->userdata
= u
;
1423 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1426 pa_sink_new_data_init(&sink_data
);
1427 sink_data
.driver
= __FILE__
;
1428 sink_data
.module
= m
;
1429 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1430 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1431 pa_sink_new_data_set_sample_spec(&sink_data
, &ss
);
1432 pa_sink_new_data_set_channel_map(&sink_data
, &map
);
1433 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1434 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1435 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1436 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1438 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1439 pa_log("Invalid properties");
1440 pa_sink_new_data_done(&sink_data
);
1444 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1447 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1448 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1451 u
->sink
= pa_sink_new(m
->core
, &sink_data
,
1452 PA_SINK_HW_MUTE_CTRL
|PA_SINK_HW_VOLUME_CTRL
|PA_SINK_DECIBEL_VOLUME
|
1453 (sink_master
->flags
& (PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
)));
1454 pa_sink_new_data_done(&sink_data
);
1457 pa_log("Failed to create sink.");
1461 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1462 u
->sink
->set_state
= sink_set_state_cb
;
1463 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1464 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1465 u
->sink
->set_volume
= sink_set_volume_cb
;
1466 u
->sink
->set_mute
= sink_set_mute_cb
;
1467 u
->sink
->userdata
= u
;
1469 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1471 /* Create source output */
1472 pa_source_output_new_data_init(&source_output_data
);
1473 source_output_data
.driver
= __FILE__
;
1474 source_output_data
.module
= m
;
1475 source_output_data
.source
= source_master
;
1477 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1479 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1480 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1481 pa_source_output_new_data_set_sample_spec(&source_output_data
, &ss
);
1482 pa_source_output_new_data_set_channel_map(&source_output_data
, &map
);
1484 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1485 pa_source_output_new_data_done(&source_output_data
);
1487 if (!u
->source_output
)
1490 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1491 u
->source_output
->push
= source_output_push_cb
;
1492 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1493 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1494 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1495 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1496 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1497 u
->source_output
->kill
= source_output_kill_cb
;
1498 u
->source_output
->attach
= source_output_attach_cb
;
1499 u
->source_output
->detach
= source_output_detach_cb
;
1500 u
->source_output
->state_change
= source_output_state_change_cb
;
1501 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1502 u
->source_output
->moving
= source_output_moving_cb
;
1503 u
->source_output
->userdata
= u
;
1505 /* Create sink input */
1506 pa_sink_input_new_data_init(&sink_input_data
);
1507 sink_input_data
.driver
= __FILE__
;
1508 sink_input_data
.module
= m
;
1509 sink_input_data
.sink
= sink_master
;
1510 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1511 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1512 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &ss
);
1513 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &map
);
1514 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1516 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1517 pa_sink_input_new_data_done(&sink_input_data
);
1522 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1523 u
->sink_input
->pop
= sink_input_pop_cb
;
1524 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1525 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1526 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1527 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1528 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1529 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1530 u
->sink_input
->kill
= sink_input_kill_cb
;
1531 u
->sink_input
->attach
= sink_input_attach_cb
;
1532 u
->sink_input
->detach
= sink_input_detach_cb
;
1533 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1534 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1535 u
->sink_input
->moving
= sink_input_moving_cb
;
1536 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1537 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1538 u
->sink_input
->userdata
= u
;
1540 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1542 u
->source_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1543 pa_frame_size(&ss
), 1, 1, 0, &silence
);
1544 u
->sink_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1545 pa_frame_size(&ss
), 1, 1, 0, &silence
);
1547 pa_memblock_unref(silence
.memblock
);
1549 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1550 pa_log("Failed to create memblockq.");
1554 if (u
->adjust_time
> 0)
1555 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1558 pa_log("Creating AEC files in /tmp");
1559 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1560 if (u
->captured_file
== NULL
)
1561 perror ("fopen failed");
1562 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1563 if (u
->played_file
== NULL
)
1564 perror ("fopen failed");
1565 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1566 if (u
->canceled_file
== NULL
)
1567 perror ("fopen failed");
1570 pa_sink_put(u
->sink
);
1571 pa_source_put(u
->source
);
1573 pa_sink_input_put(u
->sink_input
);
1574 pa_source_output_put(u
->source_output
);
1576 pa_modargs_free(ma
);
1582 pa_modargs_free(ma
);
1589 int pa__get_n_used(pa_module
*m
) {
1593 pa_assert_se(u
= m
->userdata
);
1595 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1598 void pa__done(pa_module
*m
) {
1603 if (!(u
= m
->userdata
))
1606 /* See comments in source_output_kill_cb() above regarding
1607 * destruction order! */
1609 if (u
->source_output
)
1610 pa_source_output_unlink(u
->source_output
);
1612 pa_sink_input_unlink(u
->sink_input
);
1615 pa_source_unlink(u
->source
);
1617 pa_sink_unlink(u
->sink
);
1619 if (u
->source_output
)
1620 pa_source_output_unref(u
->source_output
);
1622 pa_sink_input_unref(u
->sink_input
);
1625 pa_source_unref(u
->source
);
1627 pa_sink_unref(u
->sink
);
1630 u
->core
->mainloop
->time_free(u
->time_event
);
1632 if (u
->source_memblockq
)
1633 pa_memblockq_free(u
->source_memblockq
);
1634 if (u
->sink_memblockq
)
1635 pa_memblockq_free(u
->sink_memblockq
);
1645 pa_asyncmsgq_unref(u
->asyncmsgq
);