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 "adjust_time=<how often to readjust rates in s> "
74 "format=<sample format> "
76 "channels=<number of channels> "
77 "channel_map=<channel map> "
78 "aec_method=<implementation to use> "
79 "aec_args=<parameters for the AEC engine> "
80 "agc=<perform automagic gain control?> "
81 "denoise=<apply denoising?> "
82 "echo_suppress=<perform echo suppression? (only with the speex canceller)> "
83 "save_aec=<save AEC data in /tmp> "
84 "autoloaded=<set if this module is being loaded automatically> "
87 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
89 PA_ECHO_CANCELLER_INVALID
= -1,
90 PA_ECHO_CANCELLER_SPEEX
= 0,
91 PA_ECHO_CANCELLER_ADRIAN
,
92 } pa_echo_canceller_method_t
;
94 #define DEFAULT_ECHO_CANCELLER "speex"
96 static const pa_echo_canceller ec_table
[] = {
99 .init
= pa_speex_ec_init
,
100 .run
= pa_speex_ec_run
,
101 .done
= pa_speex_ec_done
,
104 /* Adrian Andre's NLMS implementation */
105 .init
= pa_adrian_ec_init
,
106 .run
= pa_adrian_ec_run
,
107 .done
= pa_adrian_ec_done
,
111 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
112 #define DEFAULT_AGC_ENABLED FALSE
113 #define DEFAULT_DENOISE_ENABLED FALSE
114 #define DEFAULT_ECHO_SUPPRESS_ENABLED FALSE
115 #define DEFAULT_SAVE_AEC 0
116 #define DEFAULT_AUTOLOADED FALSE
118 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
120 /* This module creates a new (virtual) source and sink.
122 * The data sent to the new sink is kept in a memblockq before being
123 * forwarded to the real sink_master.
125 * Data read from source_master is matched against the saved sink data and
126 * echo canceled data is then pushed onto the new source.
128 * Both source and sink masters have their own threads to push/pull data
129 * respectively. We however perform all our actions in the source IO thread.
130 * To do this we send all played samples to the source IO thread where they
131 * are then pushed into the memblockq.
133 * Alignment is performed in two steps:
135 * 1) when something happens that requires quick adjustement of the alignment of
136 * capture and playback samples, we perform a resync. This adjusts the
137 * position in the playback memblock to the requested sample. Quick
138 * adjustements include moving the playback samples before the capture
139 * samples (because else the echo canceler does not work) or when the
140 * playback pointer drifts too far away.
142 * 2) periodically check the difference between capture and playback. we use a
143 * low and high watermark for adjusting the alignment. playback should always
144 * be before capture and the difference should not be bigger than one frame
145 * size. We would ideally like to resample the sink_input but most driver
146 * don't give enough accuracy to be able to do that right now.
151 pa_usec_t sink_latency
;
153 int64_t send_counter
;
155 pa_usec_t source_now
;
156 pa_usec_t source_latency
;
158 int64_t recv_counter
;
167 pa_bool_t autoloaded
;
170 pa_echo_canceller
*ec
;
173 pa_bool_t need_realign
;
175 /* to wakeup the source I/O thread */
177 pa_asyncmsgq
*asyncmsgq
;
178 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
181 pa_bool_t source_auto_desc
;
182 pa_source_output
*source_output
;
183 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
187 pa_bool_t sink_auto_desc
;
188 pa_sink_input
*sink_input
;
189 pa_memblockq
*sink_memblockq
;
190 int64_t send_counter
; /* updated in sink IO thread */
191 int64_t recv_counter
;
194 pa_atomic_t request_resync
;
197 pa_time_event
*time_event
;
198 pa_usec_t adjust_time
;
205 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
207 static const char* const valid_modargs
[] = {
230 SOURCE_OUTPUT_MESSAGE_POST
= PA_SOURCE_OUTPUT_MESSAGE_MAX
,
231 SOURCE_OUTPUT_MESSAGE_REWIND
,
232 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
,
233 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
237 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
240 static int64_t calc_diff(struct userdata
*u
, struct snapshot
*snapshot
) {
241 int64_t buffer
, diff_time
, buffer_latency
;
243 /* get the number of samples between capture and playback */
244 if (snapshot
->plen
> snapshot
->rlen
)
245 buffer
= snapshot
->plen
- snapshot
->rlen
;
249 buffer
+= snapshot
->source_delay
+ snapshot
->sink_delay
;
251 /* add the amount of samples not yet transfered to the source context */
252 if (snapshot
->recv_counter
<= snapshot
->send_counter
)
253 buffer
+= (int64_t) (snapshot
->send_counter
- snapshot
->recv_counter
);
255 buffer
+= PA_CLIP_SUB(buffer
, (int64_t) (snapshot
->recv_counter
- snapshot
->send_counter
));
257 /* convert to time */
258 buffer_latency
= pa_bytes_to_usec(buffer
, &u
->source_output
->sample_spec
);
260 /* capture and playback samples are perfectly aligned when diff_time is 0 */
261 diff_time
= (snapshot
->sink_now
+ snapshot
->sink_latency
- buffer_latency
) -
262 (snapshot
->source_now
- snapshot
->source_latency
);
264 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time
,
265 (long long) snapshot
->sink_latency
,
266 (long long) buffer_latency
, (long long) snapshot
->source_latency
,
267 (long long) snapshot
->source_delay
, (long long) snapshot
->sink_delay
,
268 (long long) (snapshot
->send_counter
- snapshot
->recv_counter
),
269 (long long) (snapshot
->sink_now
- snapshot
->source_now
));
274 /* Called from main context */
275 static void time_callback(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
276 struct userdata
*u
= userdata
;
277 uint32_t old_rate
, base_rate
, new_rate
;
280 struct snapshot latency_snapshot
;
284 pa_assert(u
->time_event
== e
);
285 pa_assert_ctl_context();
287 if (u
->active_mask
!= 3)
290 /* update our snapshots */
291 pa_asyncmsgq_send(u
->source_output
->source
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
292 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
294 /* calculate drift between capture and playback */
295 diff_time
= calc_diff(u
, &latency_snapshot
);
297 /*fs = pa_frame_size(&u->source_output->sample_spec);*/
298 old_rate
= u
->sink_input
->sample_spec
.rate
;
299 base_rate
= u
->source_output
->sample_spec
.rate
;
302 /* recording before playback, we need to adjust quickly. The echo
303 * canceler does not work in this case. */
304 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
305 NULL
, diff_time
, NULL
, NULL
);
306 /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
307 new_rate
= base_rate
;
310 if (diff_time
> 1000) {
311 /* diff too big, quickly adjust */
312 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
313 NULL
, diff_time
, NULL
, NULL
);
316 /* recording behind playback, we need to slowly adjust the rate to match */
317 /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
319 /* assume equal samplerates for now */
320 new_rate
= base_rate
;
323 /* make sure we don't make too big adjustements because that sounds horrible */
324 if (new_rate
> base_rate
* 1.1 || new_rate
< base_rate
* 0.9)
325 new_rate
= base_rate
;
327 if (new_rate
!= old_rate
) {
328 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate
, (unsigned long) new_rate
);
330 pa_sink_input_set_rate(u
->sink_input
, new_rate
);
333 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
336 /* Called from source I/O thread context */
337 static int source_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
338 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
342 case PA_SOURCE_MESSAGE_GET_LATENCY
:
344 /* The source is _put() before the source output is, so let's
345 * make sure we don't access it in that time. Also, the
346 * source output is first shut down, the source second. */
347 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
348 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
)) {
349 *((pa_usec_t
*) data
) = 0;
353 *((pa_usec_t
*) data
) =
355 /* Get the latency of the master source */
356 pa_source_get_latency_within_thread(u
->source_output
->source
) +
357 /* Add the latency internal to our source output on top */
358 pa_bytes_to_usec(pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
), &u
->source_output
->source
->sample_spec
) +
359 /* and the buffering we do on the source */
360 pa_bytes_to_usec(u
->blocksize
, &u
->source_output
->source
->sample_spec
);
366 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
369 /* Called from sink I/O thread context */
370 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
371 struct userdata
*u
= PA_SINK(o
)->userdata
;
375 case PA_SINK_MESSAGE_GET_LATENCY
:
377 /* The sink is _put() before the sink input is, so let's
378 * make sure we don't access it in that time. Also, the
379 * sink input is first shut down, the sink second. */
380 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
381 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
382 *((pa_usec_t
*) data
) = 0;
386 *((pa_usec_t
*) data
) =
388 /* Get the latency of the master sink */
389 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
391 /* Add the latency internal to our sink input on top */
392 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
397 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
401 /* Called from main context */
402 static int source_set_state_cb(pa_source
*s
, pa_source_state_t state
) {
405 pa_source_assert_ref(s
);
406 pa_assert_se(u
= s
->userdata
);
408 if (!PA_SOURCE_IS_LINKED(state
) ||
409 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
412 pa_log_debug("Source state %d %d", state
, u
->active_mask
);
414 if (state
== PA_SOURCE_RUNNING
) {
415 /* restart timer when both sink and source are active */
417 if (u
->active_mask
== 3)
418 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
420 pa_atomic_store(&u
->request_resync
, 1);
421 pa_source_output_cork(u
->source_output
, FALSE
);
422 } else if (state
== PA_SOURCE_SUSPENDED
) {
423 u
->active_mask
&= ~1;
424 pa_source_output_cork(u
->source_output
, TRUE
);
429 /* Called from main context */
430 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
433 pa_sink_assert_ref(s
);
434 pa_assert_se(u
= s
->userdata
);
436 if (!PA_SINK_IS_LINKED(state
) ||
437 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
440 pa_log_debug("Sink state %d %d", state
, u
->active_mask
);
442 if (state
== PA_SINK_RUNNING
) {
443 /* restart timer when both sink and source are active */
445 if (u
->active_mask
== 3)
446 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
448 pa_atomic_store(&u
->request_resync
, 1);
449 pa_sink_input_cork(u
->sink_input
, FALSE
);
450 } else if (state
== PA_SINK_SUSPENDED
) {
451 u
->active_mask
&= ~2;
452 pa_sink_input_cork(u
->sink_input
, TRUE
);
457 /* Called from I/O thread context */
458 static void source_update_requested_latency_cb(pa_source
*s
) {
461 pa_source_assert_ref(s
);
462 pa_assert_se(u
= s
->userdata
);
464 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
465 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
))
468 pa_log_debug("Source update requested latency");
470 /* Just hand this one over to the master source */
471 pa_source_output_set_requested_latency_within_thread(
473 pa_source_get_requested_latency_within_thread(s
));
476 /* Called from I/O thread context */
477 static void sink_update_requested_latency_cb(pa_sink
*s
) {
480 pa_sink_assert_ref(s
);
481 pa_assert_se(u
= s
->userdata
);
483 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
484 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
487 pa_log_debug("Sink update requested latency");
489 /* Just hand this one over to the master sink */
490 pa_sink_input_set_requested_latency_within_thread(
492 pa_sink_get_requested_latency_within_thread(s
));
495 /* Called from I/O thread context */
496 static void sink_request_rewind_cb(pa_sink
*s
) {
499 pa_sink_assert_ref(s
);
500 pa_assert_se(u
= s
->userdata
);
502 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
503 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
506 pa_log_debug("Sink request rewind %lld", (long long) s
->thread_info
.rewind_nbytes
);
508 /* Just hand this one over to the master sink */
509 pa_sink_input_request_rewind(u
->sink_input
,
510 s
->thread_info
.rewind_nbytes
, TRUE
, FALSE
, FALSE
);
513 /* Called from main context */
514 static void source_set_volume_cb(pa_source
*s
) {
517 pa_source_assert_ref(s
);
518 pa_assert_se(u
= s
->userdata
);
520 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
521 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
524 /* FIXME, no volume control in source_output, set volume at the master */
525 pa_source_set_volume(u
->source_output
->source
, &s
->volume
, TRUE
);
528 /* Called from main context */
529 static void sink_set_volume_cb(pa_sink
*s
) {
532 pa_sink_assert_ref(s
);
533 pa_assert_se(u
= s
->userdata
);
535 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
536 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
539 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
542 static void source_get_volume_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, get the info from the master */
553 pa_source_get_volume(u
->source_output
->source
, TRUE
);
555 if (pa_cvolume_equal(&s
->volume
,&u
->source_output
->source
->volume
))
559 s
->volume
= u
->source_output
->source
->volume
;
560 pa_source_set_soft_volume(s
, NULL
);
564 /* Called from main context */
565 static void source_set_mute_cb(pa_source
*s
) {
568 pa_source_assert_ref(s
);
569 pa_assert_se(u
= s
->userdata
);
571 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
572 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
575 /* FIXME, no volume control in source_output, set mute at the master */
576 pa_source_set_mute(u
->source_output
->source
, TRUE
, TRUE
);
579 /* Called from main context */
580 static void sink_set_mute_cb(pa_sink
*s
) {
583 pa_sink_assert_ref(s
);
584 pa_assert_se(u
= s
->userdata
);
586 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
587 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
590 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
593 /* Called from main context */
594 static void source_get_mute_cb(pa_source
*s
) {
597 pa_source_assert_ref(s
);
598 pa_assert_se(u
= s
->userdata
);
600 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
601 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
604 /* FIXME, no volume control in source_output, get the info from the master */
605 pa_source_get_mute(u
->source_output
->source
, TRUE
);
608 /* must be called from the input thread context */
609 static void apply_diff_time(struct userdata
*u
, int64_t diff_time
) {
613 diff
= pa_usec_to_bytes(-diff_time
, &u
->source_output
->sample_spec
);
616 /* add some extra safety samples to compensate for jitter in the
618 diff
+= 10 * pa_frame_size (&u
->source_output
->sample_spec
);
620 pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time
, (long long) diff
);
625 } else if (diff_time
> 0) {
626 diff
= pa_usec_to_bytes(diff_time
, &u
->source_output
->sample_spec
);
629 pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time
, (long long) diff
);
631 u
->source_skip
= diff
;
637 /* must be called from the input thread */
638 static void do_resync(struct userdata
*u
) {
640 struct snapshot latency_snapshot
;
642 pa_log("Doing resync");
644 /* update our snapshot */
645 source_output_snapshot_within_thread(u
, &latency_snapshot
);
646 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
648 /* calculate drift between capture and playback */
649 diff_time
= calc_diff(u
, &latency_snapshot
);
651 /* and adjust for the drift */
652 apply_diff_time(u
, diff_time
);
655 /* Called from input thread context */
656 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
660 pa_source_output_assert_ref(o
);
661 pa_source_output_assert_io_context(o
);
662 pa_assert_se(u
= o
->userdata
);
664 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
665 pa_log("push when no link?");
669 /* handle queued messages */
671 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
675 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
679 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
681 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
682 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
684 while (rlen
>= u
->blocksize
) {
685 pa_memchunk rchunk
, pchunk
;
687 /* take fixed block from recorded samples */
688 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
690 if (plen
> u
->blocksize
&& u
->source_skip
== 0) {
691 uint8_t *rdata
, *pdata
, *cdata
;
697 if (u
->sink_skip
> plen
)
700 to_skip
= u
->sink_skip
;
702 pa_memblockq_drop(u
->sink_memblockq
, to_skip
);
705 u
->sink_skip
-= to_skip
;
708 if (plen
> u
->blocksize
&& u
->sink_skip
== 0) {
709 /* take fixed block from played samples */
710 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, u
->blocksize
, &pchunk
);
712 rdata
= pa_memblock_acquire(rchunk
.memblock
);
713 rdata
+= rchunk
.index
;
714 pdata
= pa_memblock_acquire(pchunk
.memblock
);
715 pdata
+= pchunk
.index
;
718 cchunk
.length
= u
->blocksize
;
719 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
720 cdata
= pa_memblock_acquire(cchunk
.memblock
);
723 if (u
->captured_file
)
724 fwrite(rdata
, 1, u
->blocksize
, u
->captured_file
);
726 fwrite(pdata
, 1, u
->blocksize
, u
->played_file
);
729 /* perform echo cancelation */
730 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
732 /* preprecessor is run after AEC. This is not a mistake! */
734 speex_preprocess_run(u
->ec
->pp_state
, (spx_int16_t
*) cdata
);
737 if (u
->canceled_file
)
738 fwrite(cdata
, 1, u
->blocksize
, u
->canceled_file
);
741 pa_memblock_release(cchunk
.memblock
);
742 pa_memblock_release(pchunk
.memblock
);
743 pa_memblock_release(rchunk
.memblock
);
745 /* drop consumed sink samples */
746 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
747 pa_memblock_unref(pchunk
.memblock
);
749 pa_memblock_unref(rchunk
.memblock
);
750 /* the filtered samples now become the samples from our
754 plen
-= u
->blocksize
;
758 /* forward the (echo-canceled) data to the virtual source */
759 pa_source_post(u
->source
, &rchunk
);
760 pa_memblock_unref(rchunk
.memblock
);
762 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
763 rlen
-= u
->blocksize
;
765 if (u
->source_skip
) {
766 if (u
->source_skip
> u
->blocksize
) {
767 u
->source_skip
-= u
->blocksize
;
770 u
->sink_skip
+= (u
->blocksize
- u
->source_skip
);
777 /* Called from I/O thread context */
778 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
781 pa_sink_input_assert_ref(i
);
783 pa_assert_se(u
= i
->userdata
);
785 if (u
->sink
->thread_info
.rewind_requested
)
786 pa_sink_process_rewind(u
->sink
, 0);
788 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
790 if (i
->thread_info
.underrun_for
> 0) {
791 pa_log_debug("Handling end of underrun.");
792 pa_atomic_store(&u
->request_resync
, 1);
795 /* let source thread handle the chunk. pass the sample count as well so that
796 * the source IO thread can update the right variables. */
797 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
798 NULL
, 0, chunk
, NULL
);
799 u
->send_counter
+= chunk
->length
;
804 /* Called from input thread context */
805 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
808 pa_source_output_assert_ref(o
);
809 pa_source_output_assert_io_context(o
);
810 pa_assert_se(u
= o
->userdata
);
812 pa_source_process_rewind(u
->source
, nbytes
);
814 /* go back on read side, we need to use older sink data for this */
815 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
817 /* manipulate write index */
818 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
820 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
821 (long long) pa_memblockq_get_length (u
->source_memblockq
));
824 /* Called from I/O thread context */
825 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
828 pa_sink_input_assert_ref(i
);
829 pa_assert_se(u
= i
->userdata
);
831 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
833 pa_sink_process_rewind(u
->sink
, nbytes
);
835 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
836 u
->send_counter
-= nbytes
;
839 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
840 size_t delay
, rlen
, plen
;
841 pa_usec_t now
, latency
;
843 now
= pa_rtclock_now();
844 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
845 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
847 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
848 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
849 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
851 snapshot
->source_now
= now
;
852 snapshot
->source_latency
= latency
;
853 snapshot
->source_delay
= delay
;
854 snapshot
->recv_counter
= u
->recv_counter
;
855 snapshot
->rlen
= rlen
+ u
->sink_skip
;
856 snapshot
->plen
= plen
+ u
->source_skip
;
860 /* Called from output thread context */
861 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
862 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
866 case SOURCE_OUTPUT_MESSAGE_POST
:
868 pa_source_output_assert_io_context(u
->source_output
);
870 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
871 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
873 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
875 u
->recv_counter
+= (int64_t) chunk
->length
;
879 case SOURCE_OUTPUT_MESSAGE_REWIND
:
880 pa_source_output_assert_io_context(u
->source_output
);
882 /* manipulate write index, never go past what we have */
883 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
884 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
886 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
888 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
890 u
->recv_counter
-= offset
;
894 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
895 struct snapshot
*snapshot
= (struct snapshot
*) data
;
897 source_output_snapshot_within_thread(u
, snapshot
);
901 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
902 apply_diff_time(u
, offset
);
907 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
910 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
911 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
915 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
917 pa_usec_t now
, latency
;
918 struct snapshot
*snapshot
= (struct snapshot
*) data
;
920 pa_sink_input_assert_io_context(u
->sink_input
);
922 now
= pa_rtclock_now();
923 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
924 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
926 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
928 snapshot
->sink_now
= now
;
929 snapshot
->sink_latency
= latency
;
930 snapshot
->sink_delay
= delay
;
931 snapshot
->send_counter
= u
->send_counter
;
936 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
939 /* Called from I/O thread context */
940 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
943 pa_sink_input_assert_ref(i
);
944 pa_assert_se(u
= i
->userdata
);
946 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
948 pa_memblockq_set_maxrewind(u
->sink_memblockq
, nbytes
);
949 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
952 /* Called from I/O thread context */
953 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
956 pa_source_output_assert_ref(o
);
957 pa_assert_se(u
= o
->userdata
);
959 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
961 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
964 /* Called from I/O thread context */
965 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
968 pa_sink_input_assert_ref(i
);
969 pa_assert_se(u
= i
->userdata
);
971 pa_log_debug("Sink input update max request %lld", (long long) nbytes
);
973 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
);
976 /* Called from I/O thread context */
977 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
981 pa_sink_input_assert_ref(i
);
982 pa_assert_se(u
= i
->userdata
);
984 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
986 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
989 /* Called from I/O thread context */
990 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
994 pa_source_output_assert_ref(o
);
995 pa_assert_se(u
= o
->userdata
);
997 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
999 pa_log_debug("source output update requested latency %lld", (long long) latency
);
1002 /* Called from I/O thread context */
1003 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
1006 pa_sink_input_assert_ref(i
);
1007 pa_assert_se(u
= i
->userdata
);
1009 pa_log_debug("Sink input update latency range %lld %lld",
1010 (long long) i
->sink
->thread_info
.min_latency
,
1011 (long long) i
->sink
->thread_info
.max_latency
);
1013 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1016 /* Called from I/O thread context */
1017 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
1020 pa_source_output_assert_ref(o
);
1021 pa_assert_se(u
= o
->userdata
);
1023 pa_log_debug("Source output update latency range %lld %lld",
1024 (long long) o
->source
->thread_info
.min_latency
,
1025 (long long) o
->source
->thread_info
.max_latency
);
1027 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1030 /* Called from I/O thread context */
1031 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
1034 pa_sink_input_assert_ref(i
);
1035 pa_assert_se(u
= i
->userdata
);
1037 pa_log_debug("Sink input update fixed latency %lld",
1038 (long long) i
->sink
->thread_info
.fixed_latency
);
1040 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1043 /* Called from I/O thread context */
1044 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1047 pa_source_output_assert_ref(o
);
1048 pa_assert_se(u
= o
->userdata
);
1050 pa_log_debug("Source output update fixed latency %lld",
1051 (long long) o
->source
->thread_info
.fixed_latency
);
1053 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1056 /* Called from output thread context */
1057 static void source_output_attach_cb(pa_source_output
*o
) {
1060 pa_source_output_assert_ref(o
);
1061 pa_source_output_assert_io_context(o
);
1062 pa_assert_se(u
= o
->userdata
);
1064 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1065 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1066 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1067 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1069 pa_log_debug("Source output %p attach", o
);
1071 pa_source_attach_within_thread(u
->source
);
1073 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1074 o
->source
->thread_info
.rtpoll
,
1079 /* Called from I/O thread context */
1080 static void sink_input_attach_cb(pa_sink_input
*i
) {
1083 pa_sink_input_assert_ref(i
);
1084 pa_assert_se(u
= i
->userdata
);
1086 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1087 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1089 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1090 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1091 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1093 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1094 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1096 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1097 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1099 pa_log_debug("Sink input %p attach", i
);
1101 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1102 i
->sink
->thread_info
.rtpoll
,
1106 pa_sink_attach_within_thread(u
->sink
);
1110 /* Called from output thread context */
1111 static void source_output_detach_cb(pa_source_output
*o
) {
1114 pa_source_output_assert_ref(o
);
1115 pa_source_output_assert_io_context(o
);
1116 pa_assert_se(u
= o
->userdata
);
1118 pa_source_detach_within_thread(u
->source
);
1119 pa_source_set_rtpoll(u
->source
, NULL
);
1121 pa_log_debug("Source output %p detach", o
);
1123 if (u
->rtpoll_item_read
) {
1124 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1125 u
->rtpoll_item_read
= NULL
;
1129 /* Called from I/O thread context */
1130 static void sink_input_detach_cb(pa_sink_input
*i
) {
1133 pa_sink_input_assert_ref(i
);
1134 pa_assert_se(u
= i
->userdata
);
1136 pa_sink_detach_within_thread(u
->sink
);
1138 pa_sink_set_rtpoll(u
->sink
, NULL
);
1140 pa_log_debug("Sink input %p detach", i
);
1142 if (u
->rtpoll_item_write
) {
1143 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1144 u
->rtpoll_item_write
= NULL
;
1148 /* Called from output thread context */
1149 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1152 pa_source_output_assert_ref(o
);
1153 pa_source_output_assert_io_context(o
);
1154 pa_assert_se(u
= o
->userdata
);
1156 pa_log_debug("Source output %p state %d", o
, state
);
1159 /* Called from IO thread context */
1160 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1163 pa_sink_input_assert_ref(i
);
1164 pa_assert_se(u
= i
->userdata
);
1166 pa_log_debug("Sink input %p state %d", i
, state
);
1168 /* If we are added for the first time, ask for a rewinding so that
1169 * we are heard right-away. */
1170 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1171 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1172 pa_log_debug("Requesting rewind due to state change.");
1173 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1177 /* Called from main thread */
1178 static void source_output_kill_cb(pa_source_output
*o
) {
1181 pa_source_output_assert_ref(o
);
1182 pa_assert_ctl_context();
1183 pa_assert_se(u
= o
->userdata
);
1185 /* The order here matters! We first kill the source output, followed
1186 * by the source. That means the source callbacks must be protected
1187 * against an unconnected source output! */
1188 pa_source_output_unlink(u
->source_output
);
1189 pa_source_unlink(u
->source
);
1191 pa_source_output_unref(u
->source_output
);
1192 u
->source_output
= NULL
;
1194 pa_source_unref(u
->source
);
1197 pa_log_debug("Source output kill %p", o
);
1199 pa_module_unload_request(u
->module
, TRUE
);
1202 /* Called from main context */
1203 static void sink_input_kill_cb(pa_sink_input
*i
) {
1206 pa_sink_input_assert_ref(i
);
1207 pa_assert_se(u
= i
->userdata
);
1209 /* The order here matters! We first kill the sink input, followed
1210 * by the sink. That means the sink callbacks must be protected
1211 * against an unconnected sink input! */
1212 pa_sink_input_unlink(u
->sink_input
);
1213 pa_sink_unlink(u
->sink
);
1215 pa_sink_input_unref(u
->sink_input
);
1216 u
->sink_input
= NULL
;
1218 pa_sink_unref(u
->sink
);
1221 pa_log_debug("Sink input kill %p", i
);
1223 pa_module_unload_request(u
->module
, TRUE
);
1226 /* Called from main thread */
1227 static pa_bool_t
source_output_may_move_to_cb(pa_source_output
*o
, pa_source
*dest
) {
1230 pa_source_output_assert_ref(o
);
1231 pa_assert_ctl_context();
1232 pa_assert_se(u
= o
->userdata
);
1234 return (u
->source
!= dest
) && (u
->sink
!= dest
->monitor_of
);
1237 /* Called from main context */
1238 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1241 pa_sink_input_assert_ref(i
);
1242 pa_assert_se(u
= i
->userdata
);
1244 return u
->sink
!= dest
;
1247 /* Called from main thread */
1248 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1251 pa_source_output_assert_ref(o
);
1252 pa_assert_ctl_context();
1253 pa_assert_se(u
= o
->userdata
);
1256 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1257 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1259 pa_source_set_asyncmsgq(u
->source
, NULL
);
1261 if (u
->source_auto_desc
&& dest
) {
1265 pl
= pa_proplist_new();
1266 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1267 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1268 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1270 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1271 pa_proplist_free(pl
);
1275 /* Called from main context */
1276 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1279 pa_sink_input_assert_ref(i
);
1280 pa_assert_se(u
= i
->userdata
);
1283 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1284 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1286 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1288 if (u
->sink_auto_desc
&& dest
) {
1292 pl
= pa_proplist_new();
1293 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1294 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1295 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1297 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1298 pa_proplist_free(pl
);
1302 /* Called from main context */
1303 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1306 pa_sink_input_assert_ref(i
);
1307 pa_assert_se(u
= i
->userdata
);
1309 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1312 /* Called from main context */
1313 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1316 pa_sink_input_assert_ref(i
);
1317 pa_assert_se(u
= i
->userdata
);
1319 pa_sink_mute_changed(u
->sink
, i
->muted
);
1322 static pa_echo_canceller_method_t
get_ec_method_from_string(const char *method
) {
1323 if (strcmp(method
, "speex") == 0)
1324 return PA_ECHO_CANCELLER_SPEEX
;
1325 else if (strcmp(method
, "adrian") == 0)
1326 return PA_ECHO_CANCELLER_ADRIAN
;
1328 return PA_ECHO_CANCELLER_INVALID
;
1331 int pa__init(pa_module
*m
) {
1333 pa_sample_spec source_ss
, sink_ss
;
1334 pa_channel_map source_map
, sink_map
;
1336 pa_source
*source_master
=NULL
;
1337 pa_sink
*sink_master
=NULL
;
1338 pa_source_output_new_data source_output_data
;
1339 pa_sink_input_new_data sink_input_data
;
1340 pa_source_new_data source_data
;
1341 pa_sink_new_data sink_data
;
1342 pa_memchunk silence
;
1343 pa_echo_canceller_method_t ec_method
;
1344 uint32_t adjust_time_sec
;
1348 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1349 pa_log("Failed to parse module arguments.");
1353 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1354 pa_log("Master source not found");
1357 pa_assert(source_master
);
1359 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1360 pa_log("Master sink not found");
1363 pa_assert(sink_master
);
1365 source_ss
= source_master
->sample_spec
;
1366 source_map
= source_master
->channel_map
;
1367 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &source_ss
, &source_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1368 pa_log("Invalid sample format specification or channel map");
1372 sink_ss
= sink_master
->sample_spec
;
1373 sink_map
= sink_master
->channel_map
;
1375 u
= pa_xnew0(struct userdata
, 1);
1377 pa_log("Failed to alloc userdata");
1384 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1386 pa_log("Failed to alloc echo canceller");
1390 if ((ec_method
= get_ec_method_from_string(pa_modargs_get_value(ma
, "aec_method", DEFAULT_ECHO_CANCELLER
))) < 0) {
1391 pa_log("Invalid echo canceller implementation");
1395 u
->ec
->init
= ec_table
[ec_method
].init
;
1396 u
->ec
->run
= ec_table
[ec_method
].run
;
1397 u
->ec
->done
= ec_table
[ec_method
].done
;
1399 adjust_time_sec
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1400 if (pa_modargs_get_value_u32(ma
, "adjust_time", &adjust_time_sec
) < 0) {
1401 pa_log("Failed to parse adjust_time value");
1405 if (adjust_time_sec
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1406 u
->adjust_time
= adjust_time_sec
* PA_USEC_PER_SEC
;
1408 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1410 u
->ec
->agc
= DEFAULT_AGC_ENABLED
;
1411 if (pa_modargs_get_value_boolean(ma
, "agc", &u
->ec
->agc
) < 0) {
1412 pa_log("Failed to parse agc value");
1416 u
->ec
->denoise
= DEFAULT_DENOISE_ENABLED
;
1417 if (pa_modargs_get_value_boolean(ma
, "denoise", &u
->ec
->denoise
) < 0) {
1418 pa_log("Failed to parse denoise value");
1422 u
->ec
->echo_suppress
= DEFAULT_ECHO_SUPPRESS_ENABLED
;
1423 if (pa_modargs_get_value_boolean(ma
, "echo_suppress", &u
->ec
->echo_suppress
) < 0) {
1424 pa_log("Failed to parse echo_suppress value");
1427 if (u
->ec
->echo_suppress
&& ec_method
!= PA_ECHO_CANCELLER_SPEEX
) {
1428 pa_log("Echo suppression is only useful with the speex canceller");
1432 u
->save_aec
= DEFAULT_SAVE_AEC
;
1433 if (pa_modargs_get_value_u32(ma
, "save_aec", &u
->save_aec
) < 0) {
1434 pa_log("Failed to parse save_aec value");
1438 u
->autoloaded
= DEFAULT_AUTOLOADED
;
1439 if (pa_modargs_get_value_boolean(ma
, "autoloaded", &u
->autoloaded
) < 0) {
1440 pa_log("Failed to parse autoloaded value");
1444 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1445 u
->need_realign
= TRUE
;
1447 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
))) {
1448 pa_log("Failed to init AEC engine");
1453 if (u
->ec
->agc
|| u
->ec
->denoise
|| u
->ec
->echo_suppress
) {
1454 if (source_ss
.channels
!= 1) {
1455 pa_log("AGC, denoising and echo suppression only work with channels=1");
1459 u
->ec
->pp_state
= speex_preprocess_state_init(u
->blocksize
, source_ss
.rate
);
1461 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_AGC
, &u
->ec
->agc
);
1462 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_DENOISE
, &u
->ec
->denoise
);
1463 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS
, &u
->ec
->echo_suppress
);
1464 if (u
->ec
->echo_suppress
)
1465 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_STATE
, u
->ec
->params
.priv
.speex
.state
);
1469 pa_source_new_data_init(&source_data
);
1470 source_data
.driver
= __FILE__
;
1471 source_data
.module
= m
;
1472 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1473 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1474 pa_source_new_data_set_sample_spec(&source_data
, &source_ss
);
1475 pa_source_new_data_set_channel_map(&source_data
, &source_map
);
1476 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1477 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1479 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1480 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1482 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1483 pa_log("Invalid properties");
1484 pa_source_new_data_done(&source_data
);
1488 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1491 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1492 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1495 u
->source
= pa_source_new(m
->core
, &source_data
,
1496 PA_SOURCE_HW_MUTE_CTRL
|PA_SOURCE_HW_VOLUME_CTRL
|PA_SOURCE_DECIBEL_VOLUME
|
1497 (source_master
->flags
& (PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
)));
1498 pa_source_new_data_done(&source_data
);
1501 pa_log("Failed to create source.");
1505 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1506 u
->source
->set_state
= source_set_state_cb
;
1507 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1508 u
->source
->set_volume
= source_set_volume_cb
;
1509 u
->source
->set_mute
= source_set_mute_cb
;
1510 u
->source
->get_volume
= source_get_volume_cb
;
1511 u
->source
->get_mute
= source_get_mute_cb
;
1512 u
->source
->userdata
= u
;
1514 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1517 pa_sink_new_data_init(&sink_data
);
1518 sink_data
.driver
= __FILE__
;
1519 sink_data
.module
= m
;
1520 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1521 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1522 pa_sink_new_data_set_sample_spec(&sink_data
, &sink_ss
);
1523 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
1524 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1525 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1527 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1528 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1530 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1531 pa_log("Invalid properties");
1532 pa_sink_new_data_done(&sink_data
);
1536 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1539 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1540 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1543 u
->sink
= pa_sink_new(m
->core
, &sink_data
,
1544 PA_SINK_HW_MUTE_CTRL
|PA_SINK_HW_VOLUME_CTRL
|PA_SINK_DECIBEL_VOLUME
|
1545 (sink_master
->flags
& (PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
)));
1546 pa_sink_new_data_done(&sink_data
);
1549 pa_log("Failed to create sink.");
1553 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1554 u
->sink
->set_state
= sink_set_state_cb
;
1555 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1556 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1557 u
->sink
->set_volume
= sink_set_volume_cb
;
1558 u
->sink
->set_mute
= sink_set_mute_cb
;
1559 u
->sink
->userdata
= u
;
1561 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1563 /* Create source output */
1564 pa_source_output_new_data_init(&source_output_data
);
1565 source_output_data
.driver
= __FILE__
;
1566 source_output_data
.module
= m
;
1567 source_output_data
.source
= source_master
;
1568 source_output_data
.destination_source
= u
->source
;
1570 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1572 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1573 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1574 pa_source_output_new_data_set_sample_spec(&source_output_data
, &source_ss
);
1575 pa_source_output_new_data_set_channel_map(&source_output_data
, &source_map
);
1577 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1578 pa_source_output_new_data_done(&source_output_data
);
1580 if (!u
->source_output
)
1583 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1584 u
->source_output
->push
= source_output_push_cb
;
1585 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1586 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1587 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1588 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1589 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1590 u
->source_output
->kill
= source_output_kill_cb
;
1591 u
->source_output
->attach
= source_output_attach_cb
;
1592 u
->source_output
->detach
= source_output_detach_cb
;
1593 u
->source_output
->state_change
= source_output_state_change_cb
;
1594 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1595 u
->source_output
->moving
= source_output_moving_cb
;
1596 u
->source_output
->userdata
= u
;
1598 u
->source
->output_from_master
= u
->source_output
;
1600 /* Create sink input */
1601 pa_sink_input_new_data_init(&sink_input_data
);
1602 sink_input_data
.driver
= __FILE__
;
1603 sink_input_data
.module
= m
;
1604 pa_sink_input_new_data_set_sink(&sink_input_data
, sink_master
, FALSE
);
1605 sink_input_data
.origin_sink
= u
->sink
;
1606 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1607 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1608 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_ss
);
1609 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_map
);
1610 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1612 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1613 pa_sink_input_new_data_done(&sink_input_data
);
1618 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1619 u
->sink_input
->pop
= sink_input_pop_cb
;
1620 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1621 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1622 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1623 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1624 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1625 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1626 u
->sink_input
->kill
= sink_input_kill_cb
;
1627 u
->sink_input
->attach
= sink_input_attach_cb
;
1628 u
->sink_input
->detach
= sink_input_detach_cb
;
1629 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1630 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1631 u
->sink_input
->moving
= sink_input_moving_cb
;
1632 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1633 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1634 u
->sink_input
->userdata
= u
;
1636 u
->sink
->input_to_master
= u
->sink_input
;
1638 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1640 u
->source_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1641 pa_frame_size(&source_ss
), 1, 1, 0, &silence
);
1642 u
->sink_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1643 pa_frame_size(&sink_ss
), 1, 1, 0, &silence
);
1645 pa_memblock_unref(silence
.memblock
);
1647 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1648 pa_log("Failed to create memblockq.");
1652 /* our source and sink are not suspended when we create them */
1655 if (u
->adjust_time
> 0)
1656 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1659 pa_log("Creating AEC files in /tmp");
1660 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1661 if (u
->captured_file
== NULL
)
1662 perror ("fopen failed");
1663 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1664 if (u
->played_file
== NULL
)
1665 perror ("fopen failed");
1666 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1667 if (u
->canceled_file
== NULL
)
1668 perror ("fopen failed");
1671 pa_sink_put(u
->sink
);
1672 pa_source_put(u
->source
);
1674 pa_sink_input_put(u
->sink_input
);
1675 pa_source_output_put(u
->source_output
);
1677 pa_modargs_free(ma
);
1683 pa_modargs_free(ma
);
1690 int pa__get_n_used(pa_module
*m
) {
1694 pa_assert_se(u
= m
->userdata
);
1696 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1699 void pa__done(pa_module
*m
) {
1704 if (!(u
= m
->userdata
))
1707 /* See comments in source_output_kill_cb() above regarding
1708 * destruction order! */
1711 u
->core
->mainloop
->time_free(u
->time_event
);
1713 if (u
->source_output
)
1714 pa_source_output_unlink(u
->source_output
);
1716 pa_sink_input_unlink(u
->sink_input
);
1719 pa_source_unlink(u
->source
);
1721 pa_sink_unlink(u
->sink
);
1723 if (u
->source_output
)
1724 pa_source_output_unref(u
->source_output
);
1726 pa_sink_input_unref(u
->sink_input
);
1729 pa_source_unref(u
->source
);
1731 pa_sink_unref(u
->sink
);
1733 if (u
->source_memblockq
)
1734 pa_memblockq_free(u
->source_memblockq
);
1735 if (u
->sink_memblockq
)
1736 pa_memblockq_free(u
->sink_memblockq
);
1738 if (u
->ec
->pp_state
)
1739 speex_preprocess_state_destroy(u
->ec
->pp_state
);
1749 pa_asyncmsgq_unref(u
->asyncmsgq
);