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/timeval.h>
40 #include <pulse/rtclock.h>
42 #include <pulsecore/i18n.h>
43 #include <pulsecore/atomic.h>
44 #include <pulsecore/macro.h>
45 #include <pulsecore/namereg.h>
46 #include <pulsecore/sink.h>
47 #include <pulsecore/module.h>
48 #include <pulsecore/core-rtclock.h>
49 #include <pulsecore/core-util.h>
50 #include <pulsecore/modargs.h>
51 #include <pulsecore/log.h>
52 #include <pulsecore/rtpoll.h>
53 #include <pulsecore/sample-util.h>
54 #include <pulsecore/ltdl-helper.h>
56 #include "module-echo-cancel-symdef.h"
58 PA_MODULE_AUTHOR("Wim Taymans");
59 PA_MODULE_DESCRIPTION("Echo Cancellation");
60 PA_MODULE_VERSION(PACKAGE_VERSION
);
61 PA_MODULE_LOAD_ONCE(FALSE
);
63 _("source_name=<name for the source> "
64 "source_properties=<properties for the source> "
65 "source_master=<name of source to filter> "
66 "sink_name=<name for the sink> "
67 "sink_properties=<properties for the sink> "
68 "sink_master=<name of sink to filter> "
69 "adjust_time=<how often to readjust rates in s> "
70 "adjust_threshold=<how much drift to readjust after in ms> "
71 "format=<sample format> "
73 "channels=<number of channels> "
74 "channel_map=<channel map> "
75 "aec_method=<implementation to use> "
76 "aec_args=<parameters for the AEC engine> "
77 "save_aec=<save AEC data in /tmp> "
78 "autoloaded=<set if this module is being loaded automatically> "
79 "use_volume_sharing=<yes or no> "
82 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
84 PA_ECHO_CANCELLER_INVALID
= -1,
85 PA_ECHO_CANCELLER_SPEEX
= 0,
86 PA_ECHO_CANCELLER_ADRIAN
,
88 PA_ECHO_CANCELLER_WEBRTC
,
90 } pa_echo_canceller_method_t
;
92 #define DEFAULT_ECHO_CANCELLER "speex"
94 static const pa_echo_canceller ec_table
[] = {
97 .init
= pa_speex_ec_init
,
98 .run
= pa_speex_ec_run
,
99 .done
= pa_speex_ec_done
,
102 /* Adrian Andre's NLMS implementation */
103 .init
= pa_adrian_ec_init
,
104 .run
= pa_adrian_ec_run
,
105 .done
= pa_adrian_ec_done
,
109 /* WebRTC's audio processing engine */
110 .init
= pa_webrtc_ec_init
,
111 .play
= pa_webrtc_ec_play
,
112 .record
= pa_webrtc_ec_record
,
113 .set_drift
= pa_webrtc_ec_set_drift
,
114 .run
= pa_webrtc_ec_run
,
115 .done
= pa_webrtc_ec_done
,
120 #define DEFAULT_RATE 32000
121 #define DEFAULT_CHANNELS 1
122 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
123 #define DEFAULT_ADJUST_TOLERANCE (5*PA_USEC_PER_MSEC)
124 #define DEFAULT_SAVE_AEC FALSE
125 #define DEFAULT_AUTOLOADED FALSE
127 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
129 /* Can only be used in main context */
130 #define IS_ACTIVE(u) ((pa_source_get_state((u)->source) == PA_SOURCE_RUNNING) && \
131 (pa_sink_get_state((u)->sink) == PA_SINK_RUNNING))
133 /* This module creates a new (virtual) source and sink.
135 * The data sent to the new sink is kept in a memblockq before being
136 * forwarded to the real sink_master.
138 * Data read from source_master is matched against the saved sink data and
139 * echo canceled data is then pushed onto the new source.
141 * Both source and sink masters have their own threads to push/pull data
142 * respectively. We however perform all our actions in the source IO thread.
143 * To do this we send all played samples to the source IO thread where they
144 * are then pushed into the memblockq.
146 * Alignment is performed in two steps:
148 * 1) when something happens that requires quick adjustment of the alignment of
149 * capture and playback samples, we perform a resync. This adjusts the
150 * position in the playback memblock to the requested sample. Quick
151 * adjustments include moving the playback samples before the capture
152 * samples (because else the echo canceler does not work) or when the
153 * playback pointer drifts too far away.
155 * 2) periodically check the difference between capture and playback. we use a
156 * low and high watermark for adjusting the alignment. playback should always
157 * be before capture and the difference should not be bigger than one frame
158 * size. We would ideally like to resample the sink_input but most driver
159 * don't give enough accuracy to be able to do that right now.
164 pa_usec_t sink_latency
;
166 int64_t send_counter
;
168 pa_usec_t source_now
;
169 pa_usec_t source_latency
;
171 int64_t recv_counter
;
180 pa_bool_t autoloaded
;
184 pa_echo_canceller
*ec
;
187 pa_bool_t need_realign
;
189 /* to wakeup the source I/O thread */
190 pa_asyncmsgq
*asyncmsgq
;
191 pa_rtpoll_item
*rtpoll_item_read
, *rtpoll_item_write
;
194 pa_bool_t source_auto_desc
;
195 pa_source_output
*source_output
;
196 pa_memblockq
*source_memblockq
; /* echo canceler needs fixed sized chunks */
200 pa_bool_t sink_auto_desc
;
201 pa_sink_input
*sink_input
;
202 pa_memblockq
*sink_memblockq
;
203 int64_t send_counter
; /* updated in sink IO thread */
204 int64_t recv_counter
;
207 /* Bytes left over from previous iteration */
211 pa_atomic_t request_resync
;
213 pa_time_event
*time_event
;
214 pa_usec_t adjust_time
;
215 int adjust_threshold
;
222 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
);
224 static const char* const valid_modargs
[] = {
241 "use_volume_sharing",
246 SOURCE_OUTPUT_MESSAGE_POST
= PA_SOURCE_OUTPUT_MESSAGE_MAX
,
247 SOURCE_OUTPUT_MESSAGE_REWIND
,
248 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
,
249 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
253 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
256 static int64_t calc_diff(struct userdata
*u
, struct snapshot
*snapshot
) {
257 int64_t buffer
, diff_time
, buffer_latency
;
259 /* get the number of samples between capture and playback */
260 if (snapshot
->plen
> snapshot
->rlen
)
261 buffer
= snapshot
->plen
- snapshot
->rlen
;
265 buffer
+= snapshot
->source_delay
+ snapshot
->sink_delay
;
267 /* add the amount of samples not yet transferred to the source context */
268 if (snapshot
->recv_counter
<= snapshot
->send_counter
)
269 buffer
+= (int64_t) (snapshot
->send_counter
- snapshot
->recv_counter
);
271 buffer
+= PA_CLIP_SUB(buffer
, (int64_t) (snapshot
->recv_counter
- snapshot
->send_counter
));
273 /* convert to time */
274 buffer_latency
= pa_bytes_to_usec(buffer
, &u
->source_output
->sample_spec
);
276 /* capture and playback samples are perfectly aligned when diff_time is 0 */
277 diff_time
= (snapshot
->sink_now
+ snapshot
->sink_latency
- buffer_latency
) -
278 (snapshot
->source_now
- snapshot
->source_latency
);
280 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time
,
281 (long long) snapshot
->sink_latency
,
282 (long long) buffer_latency
, (long long) snapshot
->source_latency
,
283 (long long) snapshot
->source_delay
, (long long) snapshot
->sink_delay
,
284 (long long) (snapshot
->send_counter
- snapshot
->recv_counter
),
285 (long long) (snapshot
->sink_now
- snapshot
->source_now
));
290 /* Called from main context */
291 static void time_callback(pa_mainloop_api
*a
, pa_time_event
*e
, const struct timeval
*t
, void *userdata
) {
292 struct userdata
*u
= userdata
;
293 uint32_t old_rate
, base_rate
, new_rate
;
296 struct snapshot latency_snapshot
;
300 pa_assert(u
->time_event
== e
);
301 pa_assert_ctl_context();
306 /* update our snapshots */
307 pa_asyncmsgq_send(u
->source_output
->source
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
308 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
310 /* calculate drift between capture and playback */
311 diff_time
= calc_diff(u
, &latency_snapshot
);
313 /*fs = pa_frame_size(&u->source_output->sample_spec);*/
314 old_rate
= u
->sink_input
->sample_spec
.rate
;
315 base_rate
= u
->source_output
->sample_spec
.rate
;
318 /* recording before playback, we need to adjust quickly. The echo
319 * canceler does not work in this case. */
320 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
321 NULL
, diff_time
, NULL
, NULL
);
322 /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
323 new_rate
= base_rate
;
326 if (diff_time
> u
->adjust_threshold
) {
327 /* diff too big, quickly adjust */
328 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
,
329 NULL
, diff_time
, NULL
, NULL
);
332 /* recording behind playback, we need to slowly adjust the rate to match */
333 /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
335 /* assume equal samplerates for now */
336 new_rate
= base_rate
;
339 /* make sure we don't make too big adjustments because that sounds horrible */
340 if (new_rate
> base_rate
* 1.1 || new_rate
< base_rate
* 0.9)
341 new_rate
= base_rate
;
343 if (new_rate
!= old_rate
) {
344 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate
, (unsigned long) new_rate
);
346 pa_sink_input_set_rate(u
->sink_input
, new_rate
);
349 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
352 /* Called from source I/O thread context */
353 static int source_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
354 struct userdata
*u
= PA_SOURCE(o
)->userdata
;
358 case PA_SOURCE_MESSAGE_GET_LATENCY
:
360 /* The source is _put() before the source output is, so let's
361 * make sure we don't access it in that time. Also, the
362 * source output is first shut down, the source second. */
363 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
364 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
)) {
365 *((pa_usec_t
*) data
) = 0;
369 *((pa_usec_t
*) data
) =
371 /* Get the latency of the master source */
372 pa_source_get_latency_within_thread(u
->source_output
->source
) +
373 /* Add the latency internal to our source output on top */
374 pa_bytes_to_usec(pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
), &u
->source_output
->source
->sample_spec
) +
375 /* and the buffering we do on the source */
376 pa_bytes_to_usec(u
->blocksize
, &u
->source_output
->source
->sample_spec
);
382 return pa_source_process_msg(o
, code
, data
, offset
, chunk
);
385 /* Called from sink I/O thread context */
386 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
387 struct userdata
*u
= PA_SINK(o
)->userdata
;
391 case PA_SINK_MESSAGE_GET_LATENCY
:
393 /* The sink is _put() before the sink input is, so let's
394 * make sure we don't access it in that time. Also, the
395 * sink input is first shut down, the sink second. */
396 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
397 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
398 *((pa_usec_t
*) data
) = 0;
402 *((pa_usec_t
*) data
) =
404 /* Get the latency of the master sink */
405 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
407 /* Add the latency internal to our sink input on top */
408 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
413 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
417 /* Called from main context */
418 static int source_set_state_cb(pa_source
*s
, pa_source_state_t state
) {
421 pa_source_assert_ref(s
);
422 pa_assert_se(u
= s
->userdata
);
424 if (!PA_SOURCE_IS_LINKED(state
) ||
425 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
428 if (state
== PA_SOURCE_RUNNING
) {
429 /* restart timer when both sink and source are active */
430 if (IS_ACTIVE(u
) && u
->adjust_time
)
431 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
433 pa_atomic_store(&u
->request_resync
, 1);
434 pa_source_output_cork(u
->source_output
, FALSE
);
435 } else if (state
== PA_SOURCE_SUSPENDED
) {
436 pa_source_output_cork(u
->source_output
, TRUE
);
442 /* Called from main context */
443 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
446 pa_sink_assert_ref(s
);
447 pa_assert_se(u
= s
->userdata
);
449 if (!PA_SINK_IS_LINKED(state
) ||
450 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
453 if (state
== PA_SINK_RUNNING
) {
454 /* restart timer when both sink and source are active */
455 if (IS_ACTIVE(u
) && u
->adjust_time
)
456 pa_core_rttime_restart(u
->core
, u
->time_event
, pa_rtclock_now() + u
->adjust_time
);
458 pa_atomic_store(&u
->request_resync
, 1);
459 pa_sink_input_cork(u
->sink_input
, FALSE
);
460 } else if (state
== PA_SINK_SUSPENDED
) {
461 pa_sink_input_cork(u
->sink_input
, TRUE
);
467 /* Called from I/O thread context */
468 static void source_update_requested_latency_cb(pa_source
*s
) {
471 pa_source_assert_ref(s
);
472 pa_assert_se(u
= s
->userdata
);
474 if (!PA_SOURCE_IS_LINKED(u
->source
->thread_info
.state
) ||
475 !PA_SOURCE_OUTPUT_IS_LINKED(u
->source_output
->thread_info
.state
))
478 pa_log_debug("Source update requested latency");
480 /* Just hand this one over to the master source */
481 pa_source_output_set_requested_latency_within_thread(
483 pa_source_get_requested_latency_within_thread(s
));
486 /* Called from I/O thread context */
487 static void sink_update_requested_latency_cb(pa_sink
*s
) {
490 pa_sink_assert_ref(s
);
491 pa_assert_se(u
= s
->userdata
);
493 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
494 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
497 pa_log_debug("Sink update requested latency");
499 /* Just hand this one over to the master sink */
500 pa_sink_input_set_requested_latency_within_thread(
502 pa_sink_get_requested_latency_within_thread(s
));
505 /* Called from I/O thread context */
506 static void sink_request_rewind_cb(pa_sink
*s
) {
509 pa_sink_assert_ref(s
);
510 pa_assert_se(u
= s
->userdata
);
512 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
513 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
516 pa_log_debug("Sink request rewind %lld", (long long) s
->thread_info
.rewind_nbytes
);
518 /* Just hand this one over to the master sink */
519 pa_sink_input_request_rewind(u
->sink_input
,
520 s
->thread_info
.rewind_nbytes
, TRUE
, FALSE
, FALSE
);
523 /* Called from main context */
524 static void source_set_volume_cb(pa_source
*s
) {
527 pa_source_assert_ref(s
);
528 pa_assert_se(u
= s
->userdata
);
530 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
531 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
534 pa_source_output_set_volume(u
->source_output
, &s
->real_volume
, s
->save_volume
, TRUE
);
537 /* Called from main context */
538 static void sink_set_volume_cb(pa_sink
*s
) {
541 pa_sink_assert_ref(s
);
542 pa_assert_se(u
= s
->userdata
);
544 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
545 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
548 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
551 static void source_get_volume_cb(pa_source
*s
) {
555 pa_source_assert_ref(s
);
556 pa_assert_se(u
= s
->userdata
);
558 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
559 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
562 pa_source_output_get_volume(u
->source_output
, &v
, TRUE
);
564 if (pa_cvolume_equal(&s
->real_volume
, &v
))
569 pa_source_set_soft_volume(s
, NULL
);
572 /* Called from main context */
573 static void source_set_mute_cb(pa_source
*s
) {
576 pa_source_assert_ref(s
);
577 pa_assert_se(u
= s
->userdata
);
579 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
580 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
583 pa_source_output_set_mute(u
->source_output
, s
->muted
, s
->save_muted
);
586 /* Called from main context */
587 static void sink_set_mute_cb(pa_sink
*s
) {
590 pa_sink_assert_ref(s
);
591 pa_assert_se(u
= s
->userdata
);
593 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
594 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
597 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
600 /* Called from main context */
601 static void source_get_mute_cb(pa_source
*s
) {
604 pa_source_assert_ref(s
);
605 pa_assert_se(u
= s
->userdata
);
607 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s
)) ||
608 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
)))
611 pa_source_output_get_mute(u
->source_output
);
614 /* must be called from the input thread context */
615 static void apply_diff_time(struct userdata
*u
, int64_t diff_time
) {
619 diff
= pa_usec_to_bytes(-diff_time
, &u
->source_output
->sample_spec
);
622 /* add some extra safety samples to compensate for jitter in the
624 diff
+= 10 * pa_frame_size (&u
->source_output
->sample_spec
);
626 pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time
, (long long) diff
);
631 } else if (diff_time
> 0) {
632 diff
= pa_usec_to_bytes(diff_time
, &u
->source_output
->sample_spec
);
635 pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time
, (long long) diff
);
637 u
->source_skip
= diff
;
643 /* must be called from the input thread */
644 static void do_resync(struct userdata
*u
) {
646 struct snapshot latency_snapshot
;
648 pa_log("Doing resync");
650 /* update our snapshot */
651 source_output_snapshot_within_thread(u
, &latency_snapshot
);
652 pa_asyncmsgq_send(u
->sink_input
->sink
->asyncmsgq
, PA_MSGOBJECT(u
->sink_input
), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
, &latency_snapshot
, 0, NULL
);
654 /* calculate drift between capture and playback */
655 diff_time
= calc_diff(u
, &latency_snapshot
);
657 /* and adjust for the drift */
658 apply_diff_time(u
, diff_time
);
661 /* 1. Calculate drift at this point, pass to canceller
662 * 2. Push out playback samples in blocksize chunks
663 * 3. Push out capture samples in blocksize chunks
667 static void do_push_drift_comp(struct userdata
*u
) {
669 pa_memchunk rchunk
, pchunk
, cchunk
;
670 uint8_t *rdata
, *pdata
, *cdata
;
673 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
674 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
676 /* Estimate snapshot drift as follows:
677 * pd: amount of data consumed since last time
678 * rd: amount of data consumed since last time
680 * drift = (pd - rd) / rd;
682 * We calculate pd and rd as the memblockq length less the number of
683 * samples left from the last iteration (to avoid double counting
684 * those remainder samples.
686 drift
= ((float)(plen
- u
->sink_rem
) - (rlen
- u
->source_rem
)) / ((float)(rlen
- u
->source_rem
));
687 u
->sink_rem
= plen
% u
->blocksize
;
688 u
->source_rem
= rlen
% u
->blocksize
;
690 /* Now let the canceller work its drift compensation magic */
691 u
->ec
->set_drift(u
->ec
, drift
);
693 /* Send in the playback samples first */
694 while (plen
>= u
->blocksize
) {
695 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, u
->blocksize
, &pchunk
);
696 pdata
= pa_memblock_acquire(pchunk
.memblock
);
697 pdata
+= pchunk
.index
;
699 u
->ec
->play(u
->ec
, pdata
);
701 pa_memblock_release(pchunk
.memblock
);
702 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
703 pa_memblock_unref(pchunk
.memblock
);
705 plen
-= u
->blocksize
;
708 /* And now the capture samples */
709 while (rlen
>= u
->blocksize
) {
710 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
712 rdata
= pa_memblock_acquire(rchunk
.memblock
);
713 rdata
+= rchunk
.index
;
716 cchunk
.length
= u
->blocksize
;
717 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
718 cdata
= pa_memblock_acquire(cchunk
.memblock
);
720 u
->ec
->record(u
->ec
, rdata
, cdata
);
722 pa_memblock_release(cchunk
.memblock
);
723 pa_memblock_release(rchunk
.memblock
);
725 pa_memblock_unref(rchunk
.memblock
);
727 pa_source_post(u
->source
, &cchunk
);
728 pa_memblock_unref(cchunk
.memblock
);
730 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
731 rlen
-= u
->blocksize
;
735 /* This one's simpler than the drift compensation case -- we just iterate over
736 * the capture buffer, and pass the canceller blocksize bytes of playback and
738 static void do_push(struct userdata
*u
) {
740 pa_memchunk rchunk
, pchunk
, cchunk
;
741 uint8_t *rdata
, *pdata
, *cdata
;
744 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
745 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
747 while (rlen
>= u
->blocksize
) {
748 /* take fixed block from recorded samples */
749 pa_memblockq_peek_fixed_size(u
->source_memblockq
, u
->blocksize
, &rchunk
);
751 if (plen
> u
->blocksize
) {
752 if (plen
> u
->blocksize
) {
753 /* take fixed block from played samples */
754 pa_memblockq_peek_fixed_size(u
->sink_memblockq
, u
->blocksize
, &pchunk
);
756 rdata
= pa_memblock_acquire(rchunk
.memblock
);
757 rdata
+= rchunk
.index
;
758 pdata
= pa_memblock_acquire(pchunk
.memblock
);
759 pdata
+= pchunk
.index
;
762 cchunk
.length
= u
->blocksize
;
763 cchunk
.memblock
= pa_memblock_new(u
->source
->core
->mempool
, cchunk
.length
);
764 cdata
= pa_memblock_acquire(cchunk
.memblock
);
767 if (u
->captured_file
)
768 unused
= fwrite(rdata
, 1, u
->blocksize
, u
->captured_file
);
770 unused
= fwrite(pdata
, 1, u
->blocksize
, u
->played_file
);
773 /* perform echo cancellation */
774 u
->ec
->run(u
->ec
, rdata
, pdata
, cdata
);
777 if (u
->canceled_file
)
778 unused
= fwrite(cdata
, 1, u
->blocksize
, u
->canceled_file
);
781 pa_memblock_release(cchunk
.memblock
);
782 pa_memblock_release(pchunk
.memblock
);
783 pa_memblock_release(rchunk
.memblock
);
785 /* drop consumed sink samples */
786 pa_memblockq_drop(u
->sink_memblockq
, u
->blocksize
);
787 pa_memblock_unref(pchunk
.memblock
);
789 pa_memblock_unref(rchunk
.memblock
);
790 /* the filtered samples now become the samples from our
794 plen
-= u
->blocksize
;
798 /* forward the (echo-canceled) data to the virtual source */
799 pa_source_post(u
->source
, &rchunk
);
800 pa_memblock_unref(rchunk
.memblock
);
802 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
803 rlen
-= u
->blocksize
;
807 /* Called from input thread context */
808 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
810 size_t rlen
, plen
, to_skip
;
813 pa_source_output_assert_ref(o
);
814 pa_source_output_assert_io_context(o
);
815 pa_assert_se(u
= o
->userdata
);
817 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u
->source_output
))) {
818 pa_log("push when no link?");
822 if (PA_UNLIKELY(u
->source
->thread_info
.state
!= PA_SOURCE_RUNNING
||
823 u
->sink
->thread_info
.state
!= PA_SINK_RUNNING
)) {
824 pa_source_post(u
->source
, chunk
);
828 /* handle queued messages, do any message sending of our own */
829 while (pa_asyncmsgq_process_one(u
->asyncmsgq
) > 0)
832 pa_memblockq_push_align(u
->source_memblockq
, chunk
);
834 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
835 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
837 /* Let's not do anything else till we have enough data to process */
838 if (rlen
< u
->blocksize
)
841 /* See if we need to drop samples in order to sync */
842 if (pa_atomic_cmpxchg (&u
->request_resync
, 1, 0)) {
846 /* Okay, skip cancellation for skipped source samples if needed. */
847 if (PA_UNLIKELY(u
->source_skip
)) {
848 /* The slightly tricky bit here is that we drop all but modulo
849 * blocksize bytes and then adjust for that last bit on the sink side.
850 * We do this because the source data is coming at a fixed rate, which
851 * means the only way to try to catch up is drop sink samples and let
852 * the canceller cope up with this. */
853 to_skip
= rlen
>= u
->source_skip
? u
->source_skip
: rlen
;
854 to_skip
-= to_skip
% u
->blocksize
;
857 pa_memblockq_peek_fixed_size(u
->source_memblockq
, to_skip
, &rchunk
);
858 pa_source_post(u
->source
, &rchunk
);
860 pa_memblock_unref(rchunk
.memblock
);
861 pa_memblockq_drop(u
->source_memblockq
, u
->blocksize
);
864 u
->source_skip
-= to_skip
;
867 if (rlen
&& u
->source_skip
% u
->blocksize
) {
868 u
->sink_skip
+= u
->blocksize
- (u
->source_skip
% u
->blocksize
);
869 u
->source_skip
-= (u
->source_skip
% u
->blocksize
);
873 /* And for the sink, these samples have been played back already, so we can
874 * just drop them and get on with it. */
875 if (PA_UNLIKELY(u
->sink_skip
)) {
876 to_skip
= plen
>= u
->sink_skip
? u
->sink_skip
: plen
;
878 pa_memblockq_drop(u
->sink_memblockq
, to_skip
);
881 u
->sink_skip
-= to_skip
;
884 /* process and push out samples */
885 if (u
->ec
->params
.drift_compensation
)
886 do_push_drift_comp(u
);
891 /* Called from I/O thread context */
892 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
895 pa_sink_input_assert_ref(i
);
897 pa_assert_se(u
= i
->userdata
);
899 if (u
->sink
->thread_info
.rewind_requested
)
900 pa_sink_process_rewind(u
->sink
, 0);
902 pa_sink_render_full(u
->sink
, nbytes
, chunk
);
904 if (i
->thread_info
.underrun_for
> 0) {
905 pa_log_debug("Handling end of underrun.");
906 pa_atomic_store(&u
->request_resync
, 1);
909 /* let source thread handle the chunk. pass the sample count as well so that
910 * the source IO thread can update the right variables. */
911 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_POST
,
912 NULL
, 0, chunk
, NULL
);
913 u
->send_counter
+= chunk
->length
;
918 /* Called from input thread context */
919 static void source_output_process_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
922 pa_source_output_assert_ref(o
);
923 pa_source_output_assert_io_context(o
);
924 pa_assert_se(u
= o
->userdata
);
926 pa_source_process_rewind(u
->source
, nbytes
);
928 /* go back on read side, we need to use older sink data for this */
929 pa_memblockq_rewind(u
->sink_memblockq
, nbytes
);
931 /* manipulate write index */
932 pa_memblockq_seek(u
->source_memblockq
, -nbytes
, PA_SEEK_RELATIVE
, TRUE
);
934 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes
,
935 (long long) pa_memblockq_get_length (u
->source_memblockq
));
938 /* Called from I/O thread context */
939 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
942 pa_sink_input_assert_ref(i
);
943 pa_assert_se(u
= i
->userdata
);
945 pa_log_debug("Sink process rewind %lld", (long long) nbytes
);
947 pa_sink_process_rewind(u
->sink
, nbytes
);
949 pa_asyncmsgq_post(u
->asyncmsgq
, PA_MSGOBJECT(u
->source_output
), SOURCE_OUTPUT_MESSAGE_REWIND
, NULL
, (int64_t) nbytes
, NULL
, NULL
);
950 u
->send_counter
-= nbytes
;
953 static void source_output_snapshot_within_thread(struct userdata
*u
, struct snapshot
*snapshot
) {
954 size_t delay
, rlen
, plen
;
955 pa_usec_t now
, latency
;
957 now
= pa_rtclock_now();
958 latency
= pa_source_get_latency_within_thread(u
->source_output
->source
);
959 delay
= pa_memblockq_get_length(u
->source_output
->thread_info
.delay_memblockq
);
961 delay
= (u
->source_output
->thread_info
.resampler
? pa_resampler_request(u
->source_output
->thread_info
.resampler
, delay
) : delay
);
962 rlen
= pa_memblockq_get_length(u
->source_memblockq
);
963 plen
= pa_memblockq_get_length(u
->sink_memblockq
);
965 snapshot
->source_now
= now
;
966 snapshot
->source_latency
= latency
;
967 snapshot
->source_delay
= delay
;
968 snapshot
->recv_counter
= u
->recv_counter
;
969 snapshot
->rlen
= rlen
+ u
->sink_skip
;
970 snapshot
->plen
= plen
+ u
->source_skip
;
974 /* Called from output thread context */
975 static int source_output_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
976 struct userdata
*u
= PA_SOURCE_OUTPUT(obj
)->userdata
;
980 case SOURCE_OUTPUT_MESSAGE_POST
:
982 pa_source_output_assert_io_context(u
->source_output
);
984 if (u
->source_output
->source
->thread_info
.state
== PA_SOURCE_RUNNING
)
985 pa_memblockq_push_align(u
->sink_memblockq
, chunk
);
987 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
989 u
->recv_counter
+= (int64_t) chunk
->length
;
993 case SOURCE_OUTPUT_MESSAGE_REWIND
:
994 pa_source_output_assert_io_context(u
->source_output
);
996 /* manipulate write index, never go past what we have */
997 if (PA_SOURCE_IS_OPENED(u
->source_output
->source
->thread_info
.state
))
998 pa_memblockq_seek(u
->sink_memblockq
, -offset
, PA_SEEK_RELATIVE
, TRUE
);
1000 pa_memblockq_flush_write(u
->sink_memblockq
, TRUE
);
1002 pa_log_debug("Sink rewind (%lld)", (long long) offset
);
1004 u
->recv_counter
-= offset
;
1008 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT
: {
1009 struct snapshot
*snapshot
= (struct snapshot
*) data
;
1011 source_output_snapshot_within_thread(u
, snapshot
);
1015 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
:
1016 apply_diff_time(u
, offset
);
1021 return pa_source_output_process_msg(obj
, code
, data
, offset
, chunk
);
1024 static int sink_input_process_msg_cb(pa_msgobject
*obj
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
1025 struct userdata
*u
= PA_SINK_INPUT(obj
)->userdata
;
1029 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
: {
1031 pa_usec_t now
, latency
;
1032 struct snapshot
*snapshot
= (struct snapshot
*) data
;
1034 pa_sink_input_assert_io_context(u
->sink_input
);
1036 now
= pa_rtclock_now();
1037 latency
= pa_sink_get_latency_within_thread(u
->sink_input
->sink
);
1038 delay
= pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
);
1040 delay
= (u
->sink_input
->thread_info
.resampler
? pa_resampler_request(u
->sink_input
->thread_info
.resampler
, delay
) : delay
);
1042 snapshot
->sink_now
= now
;
1043 snapshot
->sink_latency
= latency
;
1044 snapshot
->sink_delay
= delay
;
1045 snapshot
->send_counter
= u
->send_counter
;
1050 return pa_sink_input_process_msg(obj
, code
, data
, offset
, chunk
);
1053 /* Called from I/O thread context */
1054 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
1057 pa_sink_input_assert_ref(i
);
1058 pa_assert_se(u
= i
->userdata
);
1060 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes
);
1062 pa_memblockq_set_maxrewind(u
->sink_memblockq
, nbytes
);
1063 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
1066 /* Called from I/O thread context */
1067 static void source_output_update_max_rewind_cb(pa_source_output
*o
, size_t nbytes
) {
1070 pa_source_output_assert_ref(o
);
1071 pa_assert_se(u
= o
->userdata
);
1073 pa_log_debug("Source output update max rewind %lld", (long long) nbytes
);
1075 pa_source_set_max_rewind_within_thread(u
->source
, nbytes
);
1078 /* Called from I/O thread context */
1079 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
1082 pa_sink_input_assert_ref(i
);
1083 pa_assert_se(u
= i
->userdata
);
1085 pa_log_debug("Sink input update max request %lld", (long long) nbytes
);
1087 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
);
1090 /* Called from I/O thread context */
1091 static void sink_input_update_sink_requested_latency_cb(pa_sink_input
*i
) {
1095 pa_sink_input_assert_ref(i
);
1096 pa_assert_se(u
= i
->userdata
);
1098 latency
= pa_sink_get_requested_latency_within_thread(i
->sink
);
1100 pa_log_debug("Sink input update requested latency %lld", (long long) latency
);
1103 /* Called from I/O thread context */
1104 static void source_output_update_source_requested_latency_cb(pa_source_output
*o
) {
1108 pa_source_output_assert_ref(o
);
1109 pa_assert_se(u
= o
->userdata
);
1111 latency
= pa_source_get_requested_latency_within_thread(o
->source
);
1113 pa_log_debug("source output update requested latency %lld", (long long) latency
);
1116 /* Called from I/O thread context */
1117 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
1120 pa_sink_input_assert_ref(i
);
1121 pa_assert_se(u
= i
->userdata
);
1123 pa_log_debug("Sink input update latency range %lld %lld",
1124 (long long) i
->sink
->thread_info
.min_latency
,
1125 (long long) i
->sink
->thread_info
.max_latency
);
1127 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1130 /* Called from I/O thread context */
1131 static void source_output_update_source_latency_range_cb(pa_source_output
*o
) {
1134 pa_source_output_assert_ref(o
);
1135 pa_assert_se(u
= o
->userdata
);
1137 pa_log_debug("Source output update latency range %lld %lld",
1138 (long long) o
->source
->thread_info
.min_latency
,
1139 (long long) o
->source
->thread_info
.max_latency
);
1141 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1144 /* Called from I/O thread context */
1145 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
1148 pa_sink_input_assert_ref(i
);
1149 pa_assert_se(u
= i
->userdata
);
1151 pa_log_debug("Sink input update fixed latency %lld",
1152 (long long) i
->sink
->thread_info
.fixed_latency
);
1154 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1157 /* Called from I/O thread context */
1158 static void source_output_update_source_fixed_latency_cb(pa_source_output
*o
) {
1161 pa_source_output_assert_ref(o
);
1162 pa_assert_se(u
= o
->userdata
);
1164 pa_log_debug("Source output update fixed latency %lld",
1165 (long long) o
->source
->thread_info
.fixed_latency
);
1167 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1170 /* Called from output thread context */
1171 static void source_output_attach_cb(pa_source_output
*o
) {
1174 pa_source_output_assert_ref(o
);
1175 pa_source_output_assert_io_context(o
);
1176 pa_assert_se(u
= o
->userdata
);
1178 pa_source_set_rtpoll(u
->source
, o
->source
->thread_info
.rtpoll
);
1179 pa_source_set_latency_range_within_thread(u
->source
, o
->source
->thread_info
.min_latency
, o
->source
->thread_info
.max_latency
);
1180 pa_source_set_fixed_latency_within_thread(u
->source
, o
->source
->thread_info
.fixed_latency
);
1181 pa_source_set_max_rewind_within_thread(u
->source
, pa_source_output_get_max_rewind(o
));
1183 pa_log_debug("Source output %d attach", o
->index
);
1185 pa_source_attach_within_thread(u
->source
);
1187 u
->rtpoll_item_read
= pa_rtpoll_item_new_asyncmsgq_read(
1188 o
->source
->thread_info
.rtpoll
,
1193 /* Called from I/O thread context */
1194 static void sink_input_attach_cb(pa_sink_input
*i
) {
1197 pa_sink_input_assert_ref(i
);
1198 pa_assert_se(u
= i
->userdata
);
1200 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
1201 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
1203 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1204 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1205 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
1207 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1208 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1210 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
1211 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
1213 pa_log_debug("Sink input %d attach", i
->index
);
1215 u
->rtpoll_item_write
= pa_rtpoll_item_new_asyncmsgq_write(
1216 i
->sink
->thread_info
.rtpoll
,
1220 pa_sink_attach_within_thread(u
->sink
);
1224 /* Called from output thread context */
1225 static void source_output_detach_cb(pa_source_output
*o
) {
1228 pa_source_output_assert_ref(o
);
1229 pa_source_output_assert_io_context(o
);
1230 pa_assert_se(u
= o
->userdata
);
1232 pa_source_detach_within_thread(u
->source
);
1233 pa_source_set_rtpoll(u
->source
, NULL
);
1235 pa_log_debug("Source output %d detach", o
->index
);
1237 if (u
->rtpoll_item_read
) {
1238 pa_rtpoll_item_free(u
->rtpoll_item_read
);
1239 u
->rtpoll_item_read
= NULL
;
1243 /* Called from I/O thread context */
1244 static void sink_input_detach_cb(pa_sink_input
*i
) {
1247 pa_sink_input_assert_ref(i
);
1248 pa_assert_se(u
= i
->userdata
);
1250 pa_sink_detach_within_thread(u
->sink
);
1252 pa_sink_set_rtpoll(u
->sink
, NULL
);
1254 pa_log_debug("Sink input %d detach", i
->index
);
1256 if (u
->rtpoll_item_write
) {
1257 pa_rtpoll_item_free(u
->rtpoll_item_write
);
1258 u
->rtpoll_item_write
= NULL
;
1262 /* Called from output thread context */
1263 static void source_output_state_change_cb(pa_source_output
*o
, pa_source_output_state_t state
) {
1266 pa_source_output_assert_ref(o
);
1267 pa_source_output_assert_io_context(o
);
1268 pa_assert_se(u
= o
->userdata
);
1270 pa_log_debug("Source output %d state %d", o
->index
, state
);
1273 /* Called from IO thread context */
1274 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
1277 pa_sink_input_assert_ref(i
);
1278 pa_assert_se(u
= i
->userdata
);
1280 pa_log_debug("Sink input %d state %d", i
->index
, state
);
1282 /* If we are added for the first time, ask for a rewinding so that
1283 * we are heard right-away. */
1284 if (PA_SINK_INPUT_IS_LINKED(state
) &&
1285 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
1286 pa_log_debug("Requesting rewind due to state change.");
1287 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
1291 /* Called from main thread */
1292 static void source_output_kill_cb(pa_source_output
*o
) {
1295 pa_source_output_assert_ref(o
);
1296 pa_assert_ctl_context();
1297 pa_assert_se(u
= o
->userdata
);
1301 /* The order here matters! We first kill the source output, followed
1302 * by the source. That means the source callbacks must be protected
1303 * against an unconnected source output! */
1304 pa_source_output_unlink(u
->source_output
);
1305 pa_source_unlink(u
->source
);
1307 pa_source_output_unref(u
->source_output
);
1308 u
->source_output
= NULL
;
1310 pa_source_unref(u
->source
);
1313 pa_log_debug("Source output kill %d", o
->index
);
1315 pa_module_unload_request(u
->module
, TRUE
);
1318 /* Called from main context */
1319 static void sink_input_kill_cb(pa_sink_input
*i
) {
1322 pa_sink_input_assert_ref(i
);
1323 pa_assert_se(u
= i
->userdata
);
1327 /* The order here matters! We first kill the sink input, followed
1328 * by the sink. That means the sink callbacks must be protected
1329 * against an unconnected sink input! */
1330 pa_sink_input_unlink(u
->sink_input
);
1331 pa_sink_unlink(u
->sink
);
1333 pa_sink_input_unref(u
->sink_input
);
1334 u
->sink_input
= NULL
;
1336 pa_sink_unref(u
->sink
);
1339 pa_log_debug("Sink input kill %d", i
->index
);
1341 pa_module_unload_request(u
->module
, TRUE
);
1344 /* Called from main thread */
1345 static pa_bool_t
source_output_may_move_to_cb(pa_source_output
*o
, pa_source
*dest
) {
1348 pa_source_output_assert_ref(o
);
1349 pa_assert_ctl_context();
1350 pa_assert_se(u
= o
->userdata
);
1355 return (u
->source
!= dest
) && (u
->sink
!= dest
->monitor_of
);
1358 /* Called from main context */
1359 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1362 pa_sink_input_assert_ref(i
);
1363 pa_assert_se(u
= i
->userdata
);
1368 return u
->sink
!= dest
;
1371 /* Called from main thread */
1372 static void source_output_moving_cb(pa_source_output
*o
, pa_source
*dest
) {
1375 pa_source_output_assert_ref(o
);
1376 pa_assert_ctl_context();
1377 pa_assert_se(u
= o
->userdata
);
1380 pa_source_set_asyncmsgq(u
->source
, dest
->asyncmsgq
);
1381 pa_source_update_flags(u
->source
, PA_SOURCE_LATENCY
|PA_SOURCE_DYNAMIC_LATENCY
, dest
->flags
);
1383 pa_source_set_asyncmsgq(u
->source
, NULL
);
1385 if (u
->source_auto_desc
&& dest
) {
1389 pl
= pa_proplist_new();
1390 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1391 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s",
1392 pa_proplist_gets(u
->source
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1394 pa_source_update_proplist(u
->source
, PA_UPDATE_REPLACE
, pl
);
1395 pa_proplist_free(pl
);
1399 /* Called from main context */
1400 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1403 pa_sink_input_assert_ref(i
);
1404 pa_assert_se(u
= i
->userdata
);
1407 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1408 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1410 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1412 if (u
->sink_auto_desc
&& dest
) {
1416 pl
= pa_proplist_new();
1417 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1418 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s",
1419 pa_proplist_gets(u
->sink
->proplist
, "device.echo-cancel.name"), z
? z
: dest
->name
);
1421 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
1422 pa_proplist_free(pl
);
1426 /* Called from main context */
1427 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
1430 pa_sink_input_assert_ref(i
);
1431 pa_assert_se(u
= i
->userdata
);
1433 pa_sink_volume_changed(u
->sink
, &i
->volume
);
1436 /* Called from main context */
1437 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
1440 pa_sink_input_assert_ref(i
);
1441 pa_assert_se(u
= i
->userdata
);
1443 pa_sink_mute_changed(u
->sink
, i
->muted
);
1446 static pa_echo_canceller_method_t
get_ec_method_from_string(const char *method
) {
1447 if (pa_streq(method
, "speex"))
1448 return PA_ECHO_CANCELLER_SPEEX
;
1449 else if (pa_streq(method
, "adrian"))
1450 return PA_ECHO_CANCELLER_ADRIAN
;
1452 else if (pa_streq(method
, "webrtc"))
1453 return PA_ECHO_CANCELLER_WEBRTC
;
1456 return PA_ECHO_CANCELLER_INVALID
;
1459 /* Common initialisation bits between module-echo-cancel and the standalone test program */
1460 static int init_common(pa_modargs
*ma
, struct userdata
*u
, pa_sample_spec
*source_ss
, pa_channel_map
*source_map
) {
1461 pa_echo_canceller_method_t ec_method
;
1463 if (pa_modargs_get_sample_spec_and_channel_map(ma
, source_ss
, source_map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1464 pa_log("Invalid sample format specification or channel map");
1468 u
->ec
= pa_xnew0(pa_echo_canceller
, 1);
1470 pa_log("Failed to alloc echo canceller");
1474 if ((ec_method
= get_ec_method_from_string(pa_modargs_get_value(ma
, "aec_method", DEFAULT_ECHO_CANCELLER
))) < 0) {
1475 pa_log("Invalid echo canceller implementation");
1479 u
->ec
->init
= ec_table
[ec_method
].init
;
1480 u
->ec
->play
= ec_table
[ec_method
].play
;
1481 u
->ec
->record
= ec_table
[ec_method
].record
;
1482 u
->ec
->set_drift
= ec_table
[ec_method
].set_drift
;
1483 u
->ec
->run
= ec_table
[ec_method
].run
;
1484 u
->ec
->done
= ec_table
[ec_method
].done
;
1493 int pa__init(pa_module
*m
) {
1495 pa_sample_spec source_ss
, sink_ss
;
1496 pa_channel_map source_map
, sink_map
;
1498 pa_source
*source_master
=NULL
;
1499 pa_sink
*sink_master
=NULL
;
1500 pa_source_output_new_data source_output_data
;
1501 pa_sink_input_new_data sink_input_data
;
1502 pa_source_new_data source_data
;
1503 pa_sink_new_data sink_data
;
1504 pa_memchunk silence
;
1506 pa_bool_t use_volume_sharing
= TRUE
;
1510 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1511 pa_log("Failed to parse module arguments.");
1515 if (!(source_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "source_master", NULL
), PA_NAMEREG_SOURCE
))) {
1516 pa_log("Master source not found");
1519 pa_assert(source_master
);
1521 if (!(sink_master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "sink_master", NULL
), PA_NAMEREG_SINK
))) {
1522 pa_log("Master sink not found");
1525 pa_assert(sink_master
);
1527 if (source_master
->monitor_of
== sink_master
) {
1528 pa_log("Can't cancel echo between a sink and its monitor");
1532 source_ss
= source_master
->sample_spec
;
1533 source_ss
.rate
= DEFAULT_RATE
;
1534 source_ss
.channels
= DEFAULT_CHANNELS
;
1535 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1537 sink_ss
= sink_master
->sample_spec
;
1538 sink_map
= sink_master
->channel_map
;
1540 if (pa_modargs_get_value_boolean(ma
, "use_volume_sharing", &use_volume_sharing
) < 0) {
1541 pa_log("use_volume_sharing= expects a boolean argument");
1545 u
= pa_xnew0(struct userdata
, 1);
1547 pa_log("Failed to alloc userdata");
1555 temp
= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
;
1556 if (pa_modargs_get_value_u32(ma
, "adjust_time", &temp
) < 0) {
1557 pa_log("Failed to parse adjust_time value");
1561 if (temp
!= DEFAULT_ADJUST_TIME_USEC
/ PA_USEC_PER_SEC
)
1562 u
->adjust_time
= temp
* PA_USEC_PER_SEC
;
1564 u
->adjust_time
= DEFAULT_ADJUST_TIME_USEC
;
1566 temp
= DEFAULT_ADJUST_TOLERANCE
/ PA_USEC_PER_MSEC
;
1567 if (pa_modargs_get_value_u32(ma
, "adjust_threshold", &temp
) < 0) {
1568 pa_log("Failed to parse adjust_threshold value");
1572 if (temp
!= DEFAULT_ADJUST_TOLERANCE
/ PA_USEC_PER_MSEC
)
1573 u
->adjust_threshold
= temp
* PA_USEC_PER_MSEC
;
1575 u
->adjust_threshold
= DEFAULT_ADJUST_TOLERANCE
;
1577 u
->save_aec
= DEFAULT_SAVE_AEC
;
1578 if (pa_modargs_get_value_boolean(ma
, "save_aec", &u
->save_aec
) < 0) {
1579 pa_log("Failed to parse save_aec value");
1583 u
->autoloaded
= DEFAULT_AUTOLOADED
;
1584 if (pa_modargs_get_value_boolean(ma
, "autoloaded", &u
->autoloaded
) < 0) {
1585 pa_log("Failed to parse autoloaded value");
1589 if (init_common(ma
, u
, &source_ss
, &source_map
))
1592 u
->asyncmsgq
= pa_asyncmsgq_new(0);
1593 u
->need_realign
= TRUE
;
1596 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
))) {
1597 pa_log("Failed to init AEC engine");
1602 if (u
->ec
->params
.drift_compensation
)
1603 pa_assert(u
->ec
->set_drift
);
1606 pa_source_new_data_init(&source_data
);
1607 source_data
.driver
= __FILE__
;
1608 source_data
.module
= m
;
1609 if (!(source_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "source_name", NULL
))))
1610 source_data
.name
= pa_sprintf_malloc("%s.echo-cancel", source_master
->name
);
1611 pa_source_new_data_set_sample_spec(&source_data
, &source_ss
);
1612 pa_source_new_data_set_channel_map(&source_data
, &source_map
);
1613 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, source_master
->name
);
1614 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1616 pa_proplist_sets(source_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1617 pa_proplist_sets(source_data
.proplist
, "device.echo-cancel.name", source_data
.name
);
1619 if (pa_modargs_get_proplist(ma
, "source_properties", source_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1620 pa_log("Invalid properties");
1621 pa_source_new_data_done(&source_data
);
1625 if ((u
->source_auto_desc
= !pa_proplist_contains(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1628 z
= pa_proplist_gets(source_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1629 pa_proplist_setf(source_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Source %s on %s", source_data
.name
, z
? z
: source_master
->name
);
1632 u
->source
= pa_source_new(m
->core
, &source_data
, (source_master
->flags
& (PA_SOURCE_LATENCY
| PA_SOURCE_DYNAMIC_LATENCY
))
1633 | (use_volume_sharing
? PA_SOURCE_SHARE_VOLUME_WITH_MASTER
: 0));
1634 pa_source_new_data_done(&source_data
);
1637 pa_log("Failed to create source.");
1641 u
->source
->parent
.process_msg
= source_process_msg_cb
;
1642 u
->source
->set_state
= source_set_state_cb
;
1643 u
->source
->update_requested_latency
= source_update_requested_latency_cb
;
1644 pa_source_set_get_mute_callback(u
->source
, source_get_mute_cb
);
1645 pa_source_set_set_mute_callback(u
->source
, source_set_mute_cb
);
1646 if (!use_volume_sharing
) {
1647 pa_source_set_get_volume_callback(u
->source
, source_get_volume_cb
);
1648 pa_source_set_set_volume_callback(u
->source
, source_set_volume_cb
);
1649 pa_source_enable_decibel_volume(u
->source
, TRUE
);
1651 u
->source
->userdata
= u
;
1653 pa_source_set_asyncmsgq(u
->source
, source_master
->asyncmsgq
);
1656 pa_sink_new_data_init(&sink_data
);
1657 sink_data
.driver
= __FILE__
;
1658 sink_data
.module
= m
;
1659 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1660 sink_data
.name
= pa_sprintf_malloc("%s.echo-cancel", sink_master
->name
);
1661 pa_sink_new_data_set_sample_spec(&sink_data
, &sink_ss
);
1662 pa_sink_new_data_set_channel_map(&sink_data
, &sink_map
);
1663 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, sink_master
->name
);
1664 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1666 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_INTENDED_ROLES
, "phone");
1667 pa_proplist_sets(sink_data
.proplist
, "device.echo-cancel.name", sink_data
.name
);
1669 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1670 pa_log("Invalid properties");
1671 pa_sink_new_data_done(&sink_data
);
1675 if ((u
->sink_auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
1678 z
= pa_proplist_gets(sink_master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1679 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Echo-Cancel Sink %s on %s", sink_data
.name
, z
? z
: sink_master
->name
);
1682 u
->sink
= pa_sink_new(m
->core
, &sink_data
, (sink_master
->flags
& (PA_SINK_LATENCY
| PA_SINK_DYNAMIC_LATENCY
))
1683 | (use_volume_sharing
? PA_SINK_SHARE_VOLUME_WITH_MASTER
: 0));
1684 pa_sink_new_data_done(&sink_data
);
1687 pa_log("Failed to create sink.");
1691 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1692 u
->sink
->set_state
= sink_set_state_cb
;
1693 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1694 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1695 pa_sink_set_set_mute_callback(u
->sink
, sink_set_mute_cb
);
1696 if (!use_volume_sharing
) {
1697 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
1698 pa_sink_enable_decibel_volume(u
->sink
, TRUE
);
1700 u
->sink
->userdata
= u
;
1702 pa_sink_set_asyncmsgq(u
->sink
, sink_master
->asyncmsgq
);
1704 /* Create source output */
1705 pa_source_output_new_data_init(&source_output_data
);
1706 source_output_data
.driver
= __FILE__
;
1707 source_output_data
.module
= m
;
1708 pa_source_output_new_data_set_source(&source_output_data
, source_master
, FALSE
);
1709 source_output_data
.destination_source
= u
->source
;
1711 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1713 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Source Stream");
1714 pa_proplist_sets(source_output_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1715 pa_source_output_new_data_set_sample_spec(&source_output_data
, &source_ss
);
1716 pa_source_output_new_data_set_channel_map(&source_output_data
, &source_map
);
1718 pa_source_output_new(&u
->source_output
, m
->core
, &source_output_data
);
1719 pa_source_output_new_data_done(&source_output_data
);
1721 if (!u
->source_output
)
1724 u
->source_output
->parent
.process_msg
= source_output_process_msg_cb
;
1725 u
->source_output
->push
= source_output_push_cb
;
1726 u
->source_output
->process_rewind
= source_output_process_rewind_cb
;
1727 u
->source_output
->update_max_rewind
= source_output_update_max_rewind_cb
;
1728 u
->source_output
->update_source_requested_latency
= source_output_update_source_requested_latency_cb
;
1729 u
->source_output
->update_source_latency_range
= source_output_update_source_latency_range_cb
;
1730 u
->source_output
->update_source_fixed_latency
= source_output_update_source_fixed_latency_cb
;
1731 u
->source_output
->kill
= source_output_kill_cb
;
1732 u
->source_output
->attach
= source_output_attach_cb
;
1733 u
->source_output
->detach
= source_output_detach_cb
;
1734 u
->source_output
->state_change
= source_output_state_change_cb
;
1735 u
->source_output
->may_move_to
= source_output_may_move_to_cb
;
1736 u
->source_output
->moving
= source_output_moving_cb
;
1737 u
->source_output
->userdata
= u
;
1739 u
->source
->output_from_master
= u
->source_output
;
1741 /* Create sink input */
1742 pa_sink_input_new_data_init(&sink_input_data
);
1743 sink_input_data
.driver
= __FILE__
;
1744 sink_input_data
.module
= m
;
1745 pa_sink_input_new_data_set_sink(&sink_input_data
, sink_master
, FALSE
);
1746 sink_input_data
.origin_sink
= u
->sink
;
1747 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Echo-Cancel Sink Stream");
1748 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1749 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_ss
);
1750 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_map
);
1751 sink_input_data
.flags
= PA_SINK_INPUT_VARIABLE_RATE
;
1753 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1754 pa_sink_input_new_data_done(&sink_input_data
);
1759 u
->sink_input
->parent
.process_msg
= sink_input_process_msg_cb
;
1760 u
->sink_input
->pop
= sink_input_pop_cb
;
1761 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1762 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1763 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1764 u
->sink_input
->update_sink_requested_latency
= sink_input_update_sink_requested_latency_cb
;
1765 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1766 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1767 u
->sink_input
->kill
= sink_input_kill_cb
;
1768 u
->sink_input
->attach
= sink_input_attach_cb
;
1769 u
->sink_input
->detach
= sink_input_detach_cb
;
1770 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1771 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1772 u
->sink_input
->moving
= sink_input_moving_cb
;
1773 if (!use_volume_sharing
)
1774 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1775 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1776 u
->sink_input
->userdata
= u
;
1778 u
->sink
->input_to_master
= u
->sink_input
;
1780 pa_sink_input_get_silence(u
->sink_input
, &silence
);
1782 u
->source_memblockq
= pa_memblockq_new("module-echo-cancel source_memblockq", 0, MEMBLOCKQ_MAXLENGTH
, 0,
1783 &source_ss
, 1, 1, 0, &silence
);
1784 u
->sink_memblockq
= pa_memblockq_new("module-echo-cancel sink_memblockq", 0, MEMBLOCKQ_MAXLENGTH
, 0,
1785 &sink_ss
, 1, 1, 0, &silence
);
1787 pa_memblock_unref(silence
.memblock
);
1789 if (!u
->source_memblockq
|| !u
->sink_memblockq
) {
1790 pa_log("Failed to create memblockq.");
1794 if (u
->adjust_time
> 0 && !u
->ec
->params
.drift_compensation
)
1795 u
->time_event
= pa_core_rttime_new(m
->core
, pa_rtclock_now() + u
->adjust_time
, time_callback
, u
);
1796 else if (u
->ec
->params
.drift_compensation
) {
1797 pa_log_info("Canceller does drift compensation -- built-in compensation will be disabled");
1799 /* Perform resync just once to give the canceller a leg up */
1800 pa_atomic_store(&u
->request_resync
, 1);
1804 pa_log("Creating AEC files in /tmp");
1805 u
->captured_file
= fopen("/tmp/aec_rec.sw", "wb");
1806 if (u
->captured_file
== NULL
)
1807 perror ("fopen failed");
1808 u
->played_file
= fopen("/tmp/aec_play.sw", "wb");
1809 if (u
->played_file
== NULL
)
1810 perror ("fopen failed");
1811 u
->canceled_file
= fopen("/tmp/aec_out.sw", "wb");
1812 if (u
->canceled_file
== NULL
)
1813 perror ("fopen failed");
1816 pa_sink_put(u
->sink
);
1817 pa_source_put(u
->source
);
1819 pa_sink_input_put(u
->sink_input
);
1820 pa_source_output_put(u
->source_output
);
1822 pa_modargs_free(ma
);
1828 pa_modargs_free(ma
);
1835 int pa__get_n_used(pa_module
*m
) {
1839 pa_assert_se(u
= m
->userdata
);
1841 return pa_sink_linked_by(u
->sink
) + pa_source_linked_by(u
->source
);
1844 void pa__done(pa_module
*m
) {
1849 if (!(u
= m
->userdata
))
1854 /* See comments in source_output_kill_cb() above regarding
1855 * destruction order! */
1858 u
->core
->mainloop
->time_free(u
->time_event
);
1860 if (u
->source_output
)
1861 pa_source_output_unlink(u
->source_output
);
1863 pa_sink_input_unlink(u
->sink_input
);
1866 pa_source_unlink(u
->source
);
1868 pa_sink_unlink(u
->sink
);
1870 if (u
->source_output
)
1871 pa_source_output_unref(u
->source_output
);
1873 pa_sink_input_unref(u
->sink_input
);
1876 pa_source_unref(u
->source
);
1878 pa_sink_unref(u
->sink
);
1880 if (u
->source_memblockq
)
1881 pa_memblockq_free(u
->source_memblockq
);
1882 if (u
->sink_memblockq
)
1883 pa_memblockq_free(u
->sink_memblockq
);
1893 pa_asyncmsgq_unref(u
->asyncmsgq
);
1897 fclose(u
->played_file
);
1898 if (u
->captured_file
)
1899 fclose(u
->captured_file
);
1900 if (u
->canceled_file
)
1901 fclose(u
->canceled_file
);
1907 #ifdef ECHO_CANCEL_TEST
1909 * Stand-alone test program for running in the canceller on pre-recorded files.
1911 int main(int argc
, char* argv
[]) {
1913 pa_sample_spec source_ss
, sink_ss
;
1914 pa_channel_map source_map
, sink_map
;
1915 pa_modargs
*ma
= NULL
;
1916 uint8_t *rdata
= NULL
, *pdata
= NULL
, *cdata
= NULL
;
1917 int ret
= 0, unused
;
1919 pa_memzero(&u
, sizeof(u
));
1921 if (argc
< 4 || argc
> 6) {
1925 u
.ec
= pa_xnew0(pa_echo_canceller
, 1);
1927 pa_log("Failed to alloc echo canceller");
1931 u
.captured_file
= fopen(argv
[2], "r");
1932 if (u
.captured_file
== NULL
) {
1933 perror ("fopen failed");
1936 u
.played_file
= fopen(argv
[1], "r");
1937 if (u
.played_file
== NULL
) {
1938 perror ("fopen failed");
1941 u
.canceled_file
= fopen(argv
[3], "wb");
1942 if (u
.canceled_file
== NULL
) {
1943 perror ("fopen failed");
1947 u
.core
= pa_xnew0(pa_core
, 1);
1948 u
.core
->cpu_info
.cpu_type
= PA_CPU_X86
;
1949 u
.core
->cpu_info
.flags
.x86
|= PA_CPU_X86_SSE
;
1951 if (!(ma
= pa_modargs_new(argc
> 4 ? argv
[4] : NULL
, valid_modargs
))) {
1952 pa_log("Failed to parse module arguments.");
1956 source_ss
.format
= PA_SAMPLE_S16LE
;
1957 source_ss
.rate
= DEFAULT_RATE
;
1958 source_ss
.channels
= DEFAULT_CHANNELS
;
1959 pa_channel_map_init_auto(&source_map
, source_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
1961 init_common(ma
, &u
, &source_ss
, &source_map
);
1963 if (!u
.ec
->init(u
.core
, u
.ec
, &source_ss
, &source_map
, &sink_ss
, &sink_map
, &u
.blocksize
,
1964 (argc
> 4) ? argv
[5] : NULL
)) {
1965 pa_log("Failed to init AEC engine");
1969 rdata
= pa_xmalloc(u
.blocksize
);
1970 pdata
= pa_xmalloc(u
.blocksize
);
1971 cdata
= pa_xmalloc(u
.blocksize
);
1973 while (fread(rdata
, u
.blocksize
, 1, u
.captured_file
) > 0) {
1974 if (fread(pdata
, u
.blocksize
, 1, u
.played_file
) == 0) {
1975 perror("played file ended before captured file");
1979 u
.ec
->run(u
.ec
, rdata
, pdata
, cdata
);
1981 unused
= fwrite(cdata
, u
.blocksize
, 1, u
.canceled_file
);
1986 fclose(u
.captured_file
);
1987 fclose(u
.played_file
);
1988 fclose(u
.canceled_file
);
1999 pa_modargs_free(ma
);
2004 pa_log("Usage: %s play_file rec_file out_file [module args] [aec_args]",argv
[0]);
2010 #endif /* ECHO_CANCEL_TEST */