2 This file is part of PulseAudio.
4 Copyright 2010 Wim Taymans <wim.taymans@gmail.com>
6 Based on module-virtual-sink.c
7 module-virtual-source.c
10 Copyright 2010 Intel Corporation
11 Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
13 PulseAudio is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published
15 by the Free Software Foundation; either version 2.1 of the License,
16 or (at your option) any later version.
18 PulseAudio is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public License
24 along with PulseAudio; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
35 #include "echo-cancel.h"
37 #include <pulse/xmalloc.h>
38 #include <pulse/i18n.h>
39 #include <pulse/timeval.h>
40 #include <pulse/rtclock.h>
42 #include <pulsecore/atomic.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/namereg.h>
45 #include <pulsecore/sink.h>
46 #include <pulsecore/module.h>
47 #include <pulsecore/core-rtclock.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/log.h>
51 #include <pulsecore/rtpoll.h>
52 #include <pulsecore/sample-util.h>
53 #include <pulsecore/ltdl-helper.h>
55 #include "module-echo-cancel-symdef.h"
57 PA_MODULE_AUTHOR("Wim Taymans");
58 PA_MODULE_DESCRIPTION("Echo Cancelation");
59 PA_MODULE_VERSION(PACKAGE_VERSION
);
60 PA_MODULE_LOAD_ONCE(FALSE
);
62 _("source_name=<name for the source> "
63 "source_properties=<properties for the source> "
64 "source_master=<name of source to filter> "
65 "sink_name=<name for the sink> "
66 "sink_properties=<properties for the sink> "
67 "sink_master=<name of sink to filter> "
68 "adjust_time=<how often to readjust rates in s> "
69 "format=<sample format> "
71 "channels=<number of channels> "
72 "channel_map=<channel map> "
73 "aec_method=<implementation to use> "
74 "aec_args=<parameters for the AEC engine> "
75 "agc=<perform automagic gain control?> "
76 "denoise=<apply denoising?> "
77 "echo_suppress=<perform residual echo suppression? (only with the speex canceller)> "
78 "echo_suppress_attenuation=<dB value of residual echo attenuation> "
79 "echo_suppress_attenuation_active=<dB value of residual echo attenuation when near end is active> "
80 "save_aec=<save AEC data in /tmp> "
81 "autoloaded=<set if this module is being loaded automatically> "
84 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
86 PA_ECHO_CANCELLER_INVALID
= -1,
87 PA_ECHO_CANCELLER_SPEEX
= 0,
88 PA_ECHO_CANCELLER_ADRIAN
,
89 } pa_echo_canceller_method_t
;
91 #define DEFAULT_ECHO_CANCELLER "speex"
93 static const pa_echo_canceller ec_table
[] = {
96 .init
= pa_speex_ec_init
,
97 .run
= pa_speex_ec_run
,
98 .done
= pa_speex_ec_done
,
101 /* Adrian Andre's NLMS implementation */
102 .init
= pa_adrian_ec_init
,
103 .run
= pa_adrian_ec_run
,
104 .done
= pa_adrian_ec_done
,
108 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
109 #define DEFAULT_AGC_ENABLED FALSE
110 #define DEFAULT_DENOISE_ENABLED FALSE
111 #define DEFAULT_ECHO_SUPPRESS_ENABLED FALSE
112 #define DEFAULT_ECHO_SUPPRESS_ATTENUATION 0
113 #define DEFAULT_SAVE_AEC 0
114 #define DEFAULT_AUTOLOADED FALSE
116 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
118 /* This module creates a new (virtual) source and sink.
120 * The data sent to the new sink is kept in a memblockq before being
121 * forwarded to the real sink_master.
123 * Data read from source_master is matched against the saved sink data and
124 * echo canceled data is then pushed onto the new source.
126 * Both source and sink masters have their own threads to push/pull data
127 * respectively. We however perform all our actions in the source IO thread.
128 * To do this we send all played samples to the source IO thread where they
129 * are then pushed into the memblockq.
131 * Alignment is performed in two steps:
133 * 1) when something happens that requires quick adjustement of the alignment of
134 * capture and playback samples, we perform a resync. This adjusts the
135 * position in the playback memblock to the requested sample. Quick
136 * adjustements include moving the playback samples before the capture
137 * samples (because else the echo canceler does not work) or when the
138 * playback pointer drifts too far away.
140 * 2) periodically check the difference between capture and playback. we use a
141 * low and high watermark for adjusting the alignment. playback should always
142 * be before capture and the difference should not be bigger than one frame
143 * size. We would ideally like to resample the sink_input but most driver
144 * don't give enough accuracy to be able to do that right now.
149 pa_usec_t sink_latency
;
151 int64_t send_counter
;
153 pa_usec_t source_now
;
154 pa_usec_t source_latency
;
156 int64_t recv_counter
;
165 pa_bool_t autoloaded
;
168 pa_echo_canceller
*ec
;
171 pa_bool_t need_realign
;
173 /* to wakeup the source I/O thread */
175 pa_asyncmsgq
*asyncmsgq
;
176 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
179 pa_bool_t source_auto_desc
;
180 pa_source_output
*source_output
;
181 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
185 pa_bool_t sink_auto_desc
;
186 pa_sink_input
*sink_input
;
187 pa_memblockq
*sink_memblockq
;
188 int64_t send_counter
; /* updated in sink IO thread */
189 int64_t recv_counter
;
192 pa_atomic_t request_resync
;
195 pa_time_event
*time_event
;
196 pa_usec_t adjust_time
;
203 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
205 static const char* const valid_modargs
[] = {
222 "echo_suppress_attenuation",
223 "echo_suppress_attenuation_active",
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 pa_source_output_set_volume(u
->source_output
, &s
->real_volume
, s
->save_volume
, TRUE
);
527 /* Called from main context */
528 static void sink_set_volume_cb(pa_sink
*s
) {
531 pa_sink_assert_ref(s
);
532 pa_assert_se(u
= s
->userdata
);
534 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
535 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
538 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
541 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 pa_source_output_get_volume(u
->source_output
, &v
, TRUE
);
554 if (pa_cvolume_equal(&s
->real_volume
, &v
))
559 pa_source_set_soft_volume(s
, NULL
);
562 /* Called from main context */
563 static void source_set_mute_cb(pa_source
*s
) {
566 pa_source_assert_ref(s
);
567 pa_assert_se(u
= s
->userdata
);
569 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
570 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
573 pa_source_output_set_mute(u
->source_output
, s
->muted
, s
->save_muted
);
576 /* Called from main context */
577 static void sink_set_mute_cb(pa_sink
*s
) {
580 pa_sink_assert_ref(s
);
581 pa_assert_se(u
= s
->userdata
);
583 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
584 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
587 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
590 /* Called from main context */
591 static void source_get_mute_cb(pa_source
*s
) {
594 pa_source_assert_ref(s
);
595 pa_assert_se(u
= s
->userdata
);
597 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
598 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
601 pa_source_output_get_mute(u
->source_output
);
604 /* must be called from the input thread context */
605 static void apply_diff_time(struct userdata
*u
, int64_t diff_time
) {
609 diff
= pa_usec_to_bytes(-diff_time
, &u
->source_output
->sample_spec
);
612 /* add some extra safety samples to compensate for jitter in the
614 diff
+= 10 * pa_frame_size (&u
->source_output
->sample_spec
);
616 pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time
, (long long) diff
);
621 } else if (diff_time
> 0) {
622 diff
= pa_usec_to_bytes(diff_time
, &u
->source_output
->sample_spec
);
625 pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time
, (long long) diff
);
627 u
->source_skip
= diff
;
633 /* must be called from the input thread */
634 static void do_resync(struct userdata
*u
) {
636 struct snapshot latency_snapshot
;
638 pa_log("Doing resync");
640 /* update our snapshot */
641 source_output_snapshot_within_thread(u
, &latency_snapshot
);
642 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
644 /* calculate drift between capture and playback */
645 diff_time
= calc_diff(u
, &latency_snapshot
);
647 /* and adjust for the drift */
648 apply_diff_time(u
, diff_time
);
651 /* Called from input thread context */
652 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
656 pa_source_output_assert_ref(o
);
657 pa_source_output_assert_io_context(o
);
658 pa_assert_se(u
= o
->userdata
);
660 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
661 pa_log("push when no link?");
665 /* handle queued messages */
667 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
671 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
675 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
677 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
678 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
680 while (rlen
>= u
->blocksize
) {
681 pa_memchunk rchunk
, pchunk
;
683 /* take fixed block from recorded samples */
684 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
686 if (plen
> u
->blocksize
&& u
->source_skip
== 0) {
687 uint8_t *rdata
, *pdata
, *cdata
;
693 if (u
->sink_skip
> plen
)
696 to_skip
= u
->sink_skip
;
698 pa_memblockq_drop(u
->sink_memblockq
, to_skip
);
701 u
->sink_skip
-= to_skip
;
704 if (plen
> u
->blocksize
&& u
->sink_skip
== 0) {
705 /* take fixed block from played samples */
706 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, u
->blocksize
, &pchunk
);
708 rdata
= pa_memblock_acquire(rchunk
.memblock
);
709 rdata
+= rchunk
.index
;
710 pdata
= pa_memblock_acquire(pchunk
.memblock
);
711 pdata
+= pchunk
.index
;
714 cchunk
.length
= u
->blocksize
;
715 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
716 cdata
= pa_memblock_acquire(cchunk
.memblock
);
719 if (u
->captured_file
)
720 fwrite(rdata
, 1, u
->blocksize
, u
->captured_file
);
722 fwrite(pdata
, 1, u
->blocksize
, u
->played_file
);
725 /* perform echo cancelation */
726 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
728 /* preprecessor is run after AEC. This is not a mistake! */
730 speex_preprocess_run(u
->ec
->pp_state
, (spx_int16_t
*) cdata
);
733 if (u
->canceled_file
)
734 fwrite(cdata
, 1, u
->blocksize
, u
->canceled_file
);
737 pa_memblock_release(cchunk
.memblock
);
738 pa_memblock_release(pchunk
.memblock
);
739 pa_memblock_release(rchunk
.memblock
);
741 /* drop consumed sink samples */
742 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
743 pa_memblock_unref(pchunk
.memblock
);
745 pa_memblock_unref(rchunk
.memblock
);
746 /* the filtered samples now become the samples from our
750 plen
-= u
->blocksize
;
754 /* forward the (echo-canceled) data to the virtual source */
755 pa_source_post(u
->source
, &rchunk
);
756 pa_memblock_unref(rchunk
.memblock
);
758 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
759 rlen
-= u
->blocksize
;
761 if (u
->source_skip
) {
762 if (u
->source_skip
> u
->blocksize
) {
763 u
->source_skip
-= u
->blocksize
;
766 u
->sink_skip
+= (u
->blocksize
- u
->source_skip
);
773 /* Called from I/O thread context */
774 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
777 pa_sink_input_assert_ref(i
);
779 pa_assert_se(u
= i
->userdata
);
781 if (u
->sink
->thread_info
.rewind_requested
)
782 pa_sink_process_rewind(u
->sink
, 0);
784 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
786 if (i
->thread_info
.underrun_for
> 0) {
787 pa_log_debug("Handling end of underrun.");
788 pa_atomic_store(&u
->request_resync
, 1);
791 /* let source thread handle the chunk. pass the sample count as well so that
792 * the source IO thread can update the right variables. */
793 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
794 NULL
, 0, chunk
, NULL
);
795 u
->send_counter
+= chunk
->length
;
800 /* Called from input thread context */
801 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
804 pa_source_output_assert_ref(o
);
805 pa_source_output_assert_io_context(o
);
806 pa_assert_se(u
= o
->userdata
);
808 pa_source_process_rewind(u
->source
, nbytes
);
810 /* go back on read side, we need to use older sink data for this */
811 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
813 /* manipulate write index */
814 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
816 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
817 (long long) pa_memblockq_get_length (u
->source_memblockq
));
820 /* Called from I/O thread context */
821 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
824 pa_sink_input_assert_ref(i
);
825 pa_assert_se(u
= i
->userdata
);
827 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
829 pa_sink_process_rewind(u
->sink
, nbytes
);
831 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
832 u
->send_counter
-= nbytes
;
835 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
836 size_t delay
, rlen
, plen
;
837 pa_usec_t now
, latency
;
839 now
= pa_rtclock_now();
840 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
841 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
843 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
844 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
845 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
847 snapshot
->source_now
= now
;
848 snapshot
->source_latency
= latency
;
849 snapshot
->source_delay
= delay
;
850 snapshot
->recv_counter
= u
->recv_counter
;
851 snapshot
->rlen
= rlen
+ u
->sink_skip
;
852 snapshot
->plen
= plen
+ u
->source_skip
;
856 /* Called from output thread context */
857 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
858 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
862 case SOURCE_OUTPUT_MESSAGE_POST
:
864 pa_source_output_assert_io_context(u
->source_output
);
866 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
867 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
869 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
871 u
->recv_counter
+= (int64_t) chunk
->length
;
875 case SOURCE_OUTPUT_MESSAGE_REWIND
:
876 pa_source_output_assert_io_context(u
->source_output
);
878 /* manipulate write index, never go past what we have */
879 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
880 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
882 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
884 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
886 u
->recv_counter
-= offset
;
890 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
891 struct snapshot
*snapshot
= (struct snapshot
*) data
;
893 source_output_snapshot_within_thread(u
, snapshot
);
897 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
898 apply_diff_time(u
, offset
);
903 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
906 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
907 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
911 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
913 pa_usec_t now
, latency
;
914 struct snapshot
*snapshot
= (struct snapshot
*) data
;
916 pa_sink_input_assert_io_context(u
->sink_input
);
918 now
= pa_rtclock_now();
919 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
920 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
922 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
924 snapshot
->sink_now
= now
;
925 snapshot
->sink_latency
= latency
;
926 snapshot
->sink_delay
= delay
;
927 snapshot
->send_counter
= u
->send_counter
;
932 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
935 /* Called from I/O thread context */
936 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
939 pa_sink_input_assert_ref(i
);
940 pa_assert_se(u
= i
->userdata
);
942 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
944 pa_memblockq_set_maxrewind(u
->sink_memblockq
, nbytes
);
945 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
948 /* Called from I/O thread context */
949 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
952 pa_source_output_assert_ref(o
);
953 pa_assert_se(u
= o
->userdata
);
955 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
957 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
960 /* Called from I/O thread context */
961 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
964 pa_sink_input_assert_ref(i
);
965 pa_assert_se(u
= i
->userdata
);
967 pa_log_debug("Sink input update max request %lld", (long long) nbytes
);
969 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
);
972 /* Called from I/O thread context */
973 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
977 pa_sink_input_assert_ref(i
);
978 pa_assert_se(u
= i
->userdata
);
980 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
982 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
985 /* Called from I/O thread context */
986 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
990 pa_source_output_assert_ref(o
);
991 pa_assert_se(u
= o
->userdata
);
993 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
995 pa_log_debug("source output update requested latency %lld", (long long) latency
);
998 /* Called from I/O thread context */
999 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
1002 pa_sink_input_assert_ref(i
);
1003 pa_assert_se(u
= i
->userdata
);
1005 pa_log_debug("Sink input update latency range %lld %lld",
1006 (long long) i
->sink
->thread_info
.min_latency
,
1007 (long long) i
->sink
->thread_info
.max_latency
);
1009 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1012 /* Called from I/O thread context */
1013 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
1016 pa_source_output_assert_ref(o
);
1017 pa_assert_se(u
= o
->userdata
);
1019 pa_log_debug("Source output update latency range %lld %lld",
1020 (long long) o
->source
->thread_info
.min_latency
,
1021 (long long) o
->source
->thread_info
.max_latency
);
1023 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1026 /* Called from I/O thread context */
1027 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
1030 pa_sink_input_assert_ref(i
);
1031 pa_assert_se(u
= i
->userdata
);
1033 pa_log_debug("Sink input update fixed latency %lld",
1034 (long long) i
->sink
->thread_info
.fixed_latency
);
1036 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1039 /* Called from I/O thread context */
1040 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1043 pa_source_output_assert_ref(o
);
1044 pa_assert_se(u
= o
->userdata
);
1046 pa_log_debug("Source output update fixed latency %lld",
1047 (long long) o
->source
->thread_info
.fixed_latency
);
1049 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1052 /* Called from output thread context */
1053 static void source_output_attach_cb(pa_source_output
*o
) {
1056 pa_source_output_assert_ref(o
);
1057 pa_source_output_assert_io_context(o
);
1058 pa_assert_se(u
= o
->userdata
);
1060 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1061 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1062 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1063 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1065 pa_log_debug("Source output %p attach", o
);
1067 pa_source_attach_within_thread(u
->source
);
1069 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1070 o
->source
->thread_info
.rtpoll
,
1075 /* Called from I/O thread context */
1076 static void sink_input_attach_cb(pa_sink_input
*i
) {
1079 pa_sink_input_assert_ref(i
);
1080 pa_assert_se(u
= i
->userdata
);
1082 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1083 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1085 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1086 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1087 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1089 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1090 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1092 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1093 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1095 pa_log_debug("Sink input %p attach", i
);
1097 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1098 i
->sink
->thread_info
.rtpoll
,
1102 pa_sink_attach_within_thread(u
->sink
);
1106 /* Called from output thread context */
1107 static void source_output_detach_cb(pa_source_output
*o
) {
1110 pa_source_output_assert_ref(o
);
1111 pa_source_output_assert_io_context(o
);
1112 pa_assert_se(u
= o
->userdata
);
1114 pa_source_detach_within_thread(u
->source
);
1115 pa_source_set_rtpoll(u
->source
, NULL
);
1117 pa_log_debug("Source output %p detach", o
);
1119 if (u
->rtpoll_item_read
) {
1120 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1121 u
->rtpoll_item_read
= NULL
;
1125 /* Called from I/O thread context */
1126 static void sink_input_detach_cb(pa_sink_input
*i
) {
1129 pa_sink_input_assert_ref(i
);
1130 pa_assert_se(u
= i
->userdata
);
1132 pa_sink_detach_within_thread(u
->sink
);
1134 pa_sink_set_rtpoll(u
->sink
, NULL
);
1136 pa_log_debug("Sink input %p detach", i
);
1138 if (u
->rtpoll_item_write
) {
1139 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1140 u
->rtpoll_item_write
= NULL
;
1144 /* Called from output thread context */
1145 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1148 pa_source_output_assert_ref(o
);
1149 pa_source_output_assert_io_context(o
);
1150 pa_assert_se(u
= o
->userdata
);
1152 pa_log_debug("Source output %p state %d", o
, state
);
1155 /* Called from IO thread context */
1156 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1159 pa_sink_input_assert_ref(i
);
1160 pa_assert_se(u
= i
->userdata
);
1162 pa_log_debug("Sink input %p state %d", i
, state
);
1164 /* If we are added for the first time, ask for a rewinding so that
1165 * we are heard right-away. */
1166 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1167 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1168 pa_log_debug("Requesting rewind due to state change.");
1169 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1173 /* Called from main thread */
1174 static void source_output_kill_cb(pa_source_output
*o
) {
1177 pa_source_output_assert_ref(o
);
1178 pa_assert_ctl_context();
1179 pa_assert_se(u
= o
->userdata
);
1181 /* The order here matters! We first kill the source output, followed
1182 * by the source. That means the source callbacks must be protected
1183 * against an unconnected source output! */
1184 pa_source_output_unlink(u
->source_output
);
1185 pa_source_unlink(u
->source
);
1187 pa_source_output_unref(u
->source_output
);
1188 u
->source_output
= NULL
;
1190 pa_source_unref(u
->source
);
1193 pa_log_debug("Source output kill %p", o
);
1195 pa_module_unload_request(u
->module
, TRUE
);
1198 /* Called from main context */
1199 static void sink_input_kill_cb(pa_sink_input
*i
) {
1202 pa_sink_input_assert_ref(i
);
1203 pa_assert_se(u
= i
->userdata
);
1205 /* The order here matters! We first kill the sink input, followed
1206 * by the sink. That means the sink callbacks must be protected
1207 * against an unconnected sink input! */
1208 pa_sink_input_unlink(u
->sink_input
);
1209 pa_sink_unlink(u
->sink
);
1211 pa_sink_input_unref(u
->sink_input
);
1212 u
->sink_input
= NULL
;
1214 pa_sink_unref(u
->sink
);
1217 pa_log_debug("Sink input kill %p", i
);
1219 pa_module_unload_request(u
->module
, TRUE
);
1222 /* Called from main thread */
1223 static pa_bool_t
source_output_may_move_to_cb(pa_source_output
*o
, pa_source
*dest
) {
1226 pa_source_output_assert_ref(o
);
1227 pa_assert_ctl_context();
1228 pa_assert_se(u
= o
->userdata
);
1230 return (u
->source
!= dest
) && (u
->sink
!= dest
->monitor_of
);
1233 /* Called from main context */
1234 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1237 pa_sink_input_assert_ref(i
);
1238 pa_assert_se(u
= i
->userdata
);
1240 return u
->sink
!= dest
;
1243 /* Called from main thread */
1244 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1247 pa_source_output_assert_ref(o
);
1248 pa_assert_ctl_context();
1249 pa_assert_se(u
= o
->userdata
);
1252 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1253 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1255 pa_source_set_asyncmsgq(u
->source
, NULL
);
1257 if (u
->source_auto_desc
&& dest
) {
1261 pl
= pa_proplist_new();
1262 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1263 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1264 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1266 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1267 pa_proplist_free(pl
);
1271 /* Called from main context */
1272 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1275 pa_sink_input_assert_ref(i
);
1276 pa_assert_se(u
= i
->userdata
);
1279 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1280 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1282 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1284 if (u
->sink_auto_desc
&& dest
) {
1288 pl
= pa_proplist_new();
1289 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1290 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1291 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1293 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1294 pa_proplist_free(pl
);
1298 /* Called from main context */
1299 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1302 pa_sink_input_assert_ref(i
);
1303 pa_assert_se(u
= i
->userdata
);
1305 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1308 /* Called from main context */
1309 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1312 pa_sink_input_assert_ref(i
);
1313 pa_assert_se(u
= i
->userdata
);
1315 pa_sink_mute_changed(u
->sink
, i
->muted
);
1318 static pa_echo_canceller_method_t
get_ec_method_from_string(const char *method
) {
1319 if (strcmp(method
, "speex") == 0)
1320 return PA_ECHO_CANCELLER_SPEEX
;
1321 else if (strcmp(method
, "adrian") == 0)
1322 return PA_ECHO_CANCELLER_ADRIAN
;
1324 return PA_ECHO_CANCELLER_INVALID
;
1327 int pa__init(pa_module
*m
) {
1329 pa_sample_spec source_ss
, sink_ss
;
1330 pa_channel_map source_map
, sink_map
;
1332 pa_source
*source_master
=NULL
;
1333 pa_sink
*sink_master
=NULL
;
1334 pa_source_output_new_data source_output_data
;
1335 pa_sink_input_new_data sink_input_data
;
1336 pa_source_new_data source_data
;
1337 pa_sink_new_data sink_data
;
1338 pa_memchunk silence
;
1339 pa_echo_canceller_method_t ec_method
;
1340 uint32_t adjust_time_sec
;
1344 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1345 pa_log("Failed to parse module arguments.");
1349 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1350 pa_log("Master source not found");
1353 pa_assert(source_master
);
1355 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1356 pa_log("Master sink not found");
1359 pa_assert(sink_master
);
1361 source_ss
= source_master
->sample_spec
;
1362 source_map
= source_master
->channel_map
;
1363 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &source_ss
, &source_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1364 pa_log("Invalid sample format specification or channel map");
1368 sink_ss
= sink_master
->sample_spec
;
1369 sink_map
= sink_master
->channel_map
;
1371 u
= pa_xnew0(struct userdata
, 1);
1373 pa_log("Failed to alloc userdata");
1380 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1382 pa_log("Failed to alloc echo canceller");
1386 if ((ec_method
= get_ec_method_from_string(pa_modargs_get_value(ma
, "aec_method", DEFAULT_ECHO_CANCELLER
))) < 0) {
1387 pa_log("Invalid echo canceller implementation");
1391 u
->ec
->init
= ec_table
[ec_method
].init
;
1392 u
->ec
->run
= ec_table
[ec_method
].run
;
1393 u
->ec
->done
= ec_table
[ec_method
].done
;
1395 adjust_time_sec
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1396 if (pa_modargs_get_value_u32(ma
, "adjust_time", &adjust_time_sec
) < 0) {
1397 pa_log("Failed to parse adjust_time value");
1401 if (adjust_time_sec
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1402 u
->adjust_time
= adjust_time_sec
* PA_USEC_PER_SEC
;
1404 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1406 u
->ec
->agc
= DEFAULT_AGC_ENABLED
;
1407 if (pa_modargs_get_value_boolean(ma
, "agc", &u
->ec
->agc
) < 0) {
1408 pa_log("Failed to parse agc value");
1412 u
->ec
->denoise
= DEFAULT_DENOISE_ENABLED
;
1413 if (pa_modargs_get_value_boolean(ma
, "denoise", &u
->ec
->denoise
) < 0) {
1414 pa_log("Failed to parse denoise value");
1418 u
->ec
->echo_suppress
= DEFAULT_ECHO_SUPPRESS_ENABLED
;
1419 if (pa_modargs_get_value_boolean(ma
, "echo_suppress", &u
->ec
->echo_suppress
) < 0) {
1420 pa_log("Failed to parse echo_suppress value");
1423 if (u
->ec
->echo_suppress
&& ec_method
!= PA_ECHO_CANCELLER_SPEEX
) {
1424 pa_log("Echo suppression is only useful with the speex canceller");
1428 u
->ec
->echo_suppress_attenuation
= DEFAULT_ECHO_SUPPRESS_ATTENUATION
;
1429 if (pa_modargs_get_value_s32(ma
, "echo_suppress_attenuation", &u
->ec
->echo_suppress_attenuation
) < 0) {
1430 pa_log("Failed to parse echo_suppress_attenuation value");
1433 if (u
->ec
->echo_suppress_attenuation
> 0) {
1434 pa_log("echo_suppress_attenuation should be a negative dB value");
1438 u
->ec
->echo_suppress_attenuation_active
= DEFAULT_ECHO_SUPPRESS_ATTENUATION
;
1439 if (pa_modargs_get_value_s32(ma
, "echo_suppress_attenuation_active", &u
->ec
->echo_suppress_attenuation_active
) < 0) {
1440 pa_log("Failed to parse echo_supress_attenuation_active value");
1443 if (u
->ec
->echo_suppress_attenuation_active
> 0) {
1444 pa_log("echo_suppress_attenuation_active should be a negative dB value");
1448 u
->save_aec
= DEFAULT_SAVE_AEC
;
1449 if (pa_modargs_get_value_u32(ma
, "save_aec", &u
->save_aec
) < 0) {
1450 pa_log("Failed to parse save_aec value");
1454 u
->autoloaded
= DEFAULT_AUTOLOADED
;
1455 if (pa_modargs_get_value_boolean(ma
, "autoloaded", &u
->autoloaded
) < 0) {
1456 pa_log("Failed to parse autoloaded value");
1460 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1461 u
->need_realign
= TRUE
;
1463 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
))) {
1464 pa_log("Failed to init AEC engine");
1469 if (u
->ec
->agc
|| u
->ec
->denoise
|| u
->ec
->echo_suppress
) {
1472 if (source_ss
.channels
!= 1) {
1473 pa_log("AGC, denoising and echo suppression only work with channels=1");
1477 u
->ec
->pp_state
= speex_preprocess_state_init(u
->blocksize
/ pa_frame_size(&source_ss
), source_ss
.rate
);
1480 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_AGC
, &tmp
);
1481 tmp
= u
->ec
->denoise
;
1482 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_DENOISE
, &tmp
);
1483 if (u
->ec
->echo_suppress
) {
1484 if (u
->ec
->echo_suppress_attenuation
)
1485 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS
, &u
->ec
->echo_suppress_attenuation
);
1486 if (u
->ec
->echo_suppress_attenuation_active
) {
1487 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS_ACTIVE
,
1488 &u
->ec
->echo_suppress_attenuation_active
);
1490 speex_preprocess_ctl(u
->ec
->pp_state
, SPEEX_PREPROCESS_SET_ECHO_STATE
, u
->ec
->params
.priv
.speex
.state
);
1495 pa_source_new_data_init(&source_data
);
1496 source_data
.driver
= __FILE__
;
1497 source_data
.module
= m
;
1498 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1499 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1500 pa_source_new_data_set_sample_spec(&source_data
, &source_ss
);
1501 pa_source_new_data_set_channel_map(&source_data
, &source_map
);
1502 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1503 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1505 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1506 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1508 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1509 pa_log("Invalid properties");
1510 pa_source_new_data_done(&source_data
);
1514 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1517 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1518 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1521 u
->source
= pa_source_new(m
->core
, &source_data
,
1522 PA_SOURCE_HW_MUTE_CTRL
|PA_SOURCE_HW_VOLUME_CTRL
|PA_SOURCE_DECIBEL_VOLUME
|
1523 (source_master
->flags
& (PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
)));
1524 pa_source_new_data_done(&source_data
);
1527 pa_log("Failed to create source.");
1531 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1532 u
->source
->set_state
= source_set_state_cb
;
1533 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1534 u
->source
->set_volume
= source_set_volume_cb
;
1535 u
->source
->set_mute
= source_set_mute_cb
;
1536 u
->source
->get_volume
= source_get_volume_cb
;
1537 u
->source
->get_mute
= source_get_mute_cb
;
1538 u
->source
->userdata
= u
;
1540 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1543 pa_sink_new_data_init(&sink_data
);
1544 sink_data
.driver
= __FILE__
;
1545 sink_data
.module
= m
;
1546 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1547 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1548 pa_sink_new_data_set_sample_spec(&sink_data
, &sink_ss
);
1549 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
1550 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1551 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1553 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1554 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1556 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1557 pa_log("Invalid properties");
1558 pa_sink_new_data_done(&sink_data
);
1562 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1565 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1566 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1569 u
->sink
= pa_sink_new(m
->core
, &sink_data
,
1570 PA_SINK_HW_MUTE_CTRL
|PA_SINK_HW_VOLUME_CTRL
|PA_SINK_DECIBEL_VOLUME
|
1571 (sink_master
->flags
& (PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
)));
1572 pa_sink_new_data_done(&sink_data
);
1575 pa_log("Failed to create sink.");
1579 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1580 u
->sink
->set_state
= sink_set_state_cb
;
1581 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1582 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1583 u
->sink
->set_volume
= sink_set_volume_cb
;
1584 u
->sink
->set_mute
= sink_set_mute_cb
;
1585 u
->sink
->userdata
= u
;
1587 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1589 /* Create source output */
1590 pa_source_output_new_data_init(&source_output_data
);
1591 source_output_data
.driver
= __FILE__
;
1592 source_output_data
.module
= m
;
1593 pa_source_output_new_data_set_source(&source_output_data
, source_master
, FALSE
);
1594 source_output_data
.destination_source
= u
->source
;
1596 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1598 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1599 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1600 pa_source_output_new_data_set_sample_spec(&source_output_data
, &source_ss
);
1601 pa_source_output_new_data_set_channel_map(&source_output_data
, &source_map
);
1603 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1604 pa_source_output_new_data_done(&source_output_data
);
1606 if (!u
->source_output
)
1609 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1610 u
->source_output
->push
= source_output_push_cb
;
1611 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1612 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1613 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1614 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1615 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1616 u
->source_output
->kill
= source_output_kill_cb
;
1617 u
->source_output
->attach
= source_output_attach_cb
;
1618 u
->source_output
->detach
= source_output_detach_cb
;
1619 u
->source_output
->state_change
= source_output_state_change_cb
;
1620 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1621 u
->source_output
->moving
= source_output_moving_cb
;
1622 u
->source_output
->userdata
= u
;
1624 u
->source
->output_from_master
= u
->source_output
;
1626 /* Create sink input */
1627 pa_sink_input_new_data_init(&sink_input_data
);
1628 sink_input_data
.driver
= __FILE__
;
1629 sink_input_data
.module
= m
;
1630 pa_sink_input_new_data_set_sink(&sink_input_data
, sink_master
, FALSE
);
1631 sink_input_data
.origin_sink
= u
->sink
;
1632 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1633 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1634 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_ss
);
1635 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_map
);
1636 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1638 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1639 pa_sink_input_new_data_done(&sink_input_data
);
1644 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1645 u
->sink_input
->pop
= sink_input_pop_cb
;
1646 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1647 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1648 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1649 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1650 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1651 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1652 u
->sink_input
->kill
= sink_input_kill_cb
;
1653 u
->sink_input
->attach
= sink_input_attach_cb
;
1654 u
->sink_input
->detach
= sink_input_detach_cb
;
1655 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1656 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1657 u
->sink_input
->moving
= sink_input_moving_cb
;
1658 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1659 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1660 u
->sink_input
->userdata
= u
;
1662 u
->sink
->input_to_master
= u
->sink_input
;
1664 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1666 u
->source_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1667 pa_frame_size(&source_ss
), 1, 1, 0, &silence
);
1668 u
->sink_memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0,
1669 pa_frame_size(&sink_ss
), 1, 1, 0, &silence
);
1671 pa_memblock_unref(silence
.memblock
);
1673 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1674 pa_log("Failed to create memblockq.");
1678 /* our source and sink are not suspended when we create them */
1681 if (u
->adjust_time
> 0)
1682 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1685 pa_log("Creating AEC files in /tmp");
1686 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1687 if (u
->captured_file
== NULL
)
1688 perror ("fopen failed");
1689 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1690 if (u
->played_file
== NULL
)
1691 perror ("fopen failed");
1692 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1693 if (u
->canceled_file
== NULL
)
1694 perror ("fopen failed");
1697 pa_sink_put(u
->sink
);
1698 pa_source_put(u
->source
);
1700 pa_sink_input_put(u
->sink_input
);
1701 pa_source_output_put(u
->source_output
);
1703 pa_modargs_free(ma
);
1709 pa_modargs_free(ma
);
1716 int pa__get_n_used(pa_module
*m
) {
1720 pa_assert_se(u
= m
->userdata
);
1722 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1725 void pa__done(pa_module
*m
) {
1730 if (!(u
= m
->userdata
))
1733 /* See comments in source_output_kill_cb() above regarding
1734 * destruction order! */
1737 u
->core
->mainloop
->time_free(u
->time_event
);
1739 if (u
->source_output
)
1740 pa_source_output_unlink(u
->source_output
);
1742 pa_sink_input_unlink(u
->sink_input
);
1745 pa_source_unlink(u
->source
);
1747 pa_sink_unlink(u
->sink
);
1749 if (u
->source_output
)
1750 pa_source_output_unref(u
->source_output
);
1752 pa_sink_input_unref(u
->sink_input
);
1755 pa_source_unref(u
->source
);
1757 pa_sink_unref(u
->sink
);
1759 if (u
->source_memblockq
)
1760 pa_memblockq_free(u
->source_memblockq
);
1761 if (u
->sink_memblockq
)
1762 pa_memblockq_free(u
->sink_memblockq
);
1764 if (u
->ec
->pp_state
)
1765 speex_preprocess_state_destroy(u
->ec
->pp_state
);
1775 pa_asyncmsgq_unref(u
->asyncmsgq
);