]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/module-echo-cancel.c
echo-cancel: Remove redundant variable
[pulseaudio] / src / modules / echo-cancel / module-echo-cancel.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2010 Wim Taymans <wim.taymans@gmail.com>
5
6 Based on module-virtual-sink.c
7 module-virtual-source.c
8 module-loopback.c
9
10 Copyright 2010 Intel Corporation
11 Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
12
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.
17
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.
22
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
26 USA.
27 ***/
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <stdio.h>
34
35 #include "echo-cancel.h"
36
37 #include <pulse/xmalloc.h>
38 #include <pulse/timeval.h>
39 #include <pulse/rtclock.h>
40
41 #include <pulsecore/i18n.h>
42 #include <pulsecore/atomic.h>
43 #include <pulsecore/macro.h>
44 #include <pulsecore/namereg.h>
45 #include <pulsecore/sink.h>
46 #include <pulsecore/module.h>
47 #include <pulsecore/core-rtclock.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/log.h>
51 #include <pulsecore/rtpoll.h>
52 #include <pulsecore/sample-util.h>
53 #include <pulsecore/ltdl-helper.h>
54
55 #include "module-echo-cancel-symdef.h"
56
57 PA_MODULE_AUTHOR("Wim Taymans");
58 PA_MODULE_DESCRIPTION("Echo Cancellation");
59 PA_MODULE_VERSION(PACKAGE_VERSION);
60 PA_MODULE_LOAD_ONCE(FALSE);
61 PA_MODULE_USAGE(
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> "
70 "rate=<sample rate> "
71 "channels=<number of channels> "
72 "channel_map=<channel map> "
73 "aec_method=<implementation to use> "
74 "aec_args=<parameters for the AEC engine> "
75 "save_aec=<save AEC data in /tmp> "
76 "autoloaded=<set if this module is being loaded automatically> "
77 "use_volume_sharing=<yes or no> "
78 ));
79
80 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
81 typedef enum {
82 PA_ECHO_CANCELLER_INVALID = -1,
83 PA_ECHO_CANCELLER_SPEEX = 0,
84 PA_ECHO_CANCELLER_ADRIAN,
85 } pa_echo_canceller_method_t;
86
87 #define DEFAULT_ECHO_CANCELLER "speex"
88
89 static const pa_echo_canceller ec_table[] = {
90 {
91 /* Speex */
92 .init = pa_speex_ec_init,
93 .run = pa_speex_ec_run,
94 .done = pa_speex_ec_done,
95 },
96 {
97 /* Adrian Andre's NLMS implementation */
98 .init = pa_adrian_ec_init,
99 .run = pa_adrian_ec_run,
100 .done = pa_adrian_ec_done,
101 },
102 };
103
104 #define DEFAULT_RATE 32000
105 #define DEFAULT_CHANNELS 1
106 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
107 #define DEFAULT_SAVE_AEC FALSE
108 #define DEFAULT_AUTOLOADED FALSE
109
110 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
111
112 /* This module creates a new (virtual) source and sink.
113 *
114 * The data sent to the new sink is kept in a memblockq before being
115 * forwarded to the real sink_master.
116 *
117 * Data read from source_master is matched against the saved sink data and
118 * echo canceled data is then pushed onto the new source.
119 *
120 * Both source and sink masters have their own threads to push/pull data
121 * respectively. We however perform all our actions in the source IO thread.
122 * To do this we send all played samples to the source IO thread where they
123 * are then pushed into the memblockq.
124 *
125 * Alignment is performed in two steps:
126 *
127 * 1) when something happens that requires quick adjustment of the alignment of
128 * capture and playback samples, we perform a resync. This adjusts the
129 * position in the playback memblock to the requested sample. Quick
130 * adjustments include moving the playback samples before the capture
131 * samples (because else the echo canceler does not work) or when the
132 * playback pointer drifts too far away.
133 *
134 * 2) periodically check the difference between capture and playback. we use a
135 * low and high watermark for adjusting the alignment. playback should always
136 * be before capture and the difference should not be bigger than one frame
137 * size. We would ideally like to resample the sink_input but most driver
138 * don't give enough accuracy to be able to do that right now.
139 */
140
141 struct snapshot {
142 pa_usec_t sink_now;
143 pa_usec_t sink_latency;
144 size_t sink_delay;
145 int64_t send_counter;
146
147 pa_usec_t source_now;
148 pa_usec_t source_latency;
149 size_t source_delay;
150 int64_t recv_counter;
151 size_t rlen;
152 size_t plen;
153 };
154
155 struct userdata {
156 pa_core *core;
157 pa_module *module;
158
159 pa_bool_t autoloaded;
160 pa_bool_t dead;
161 pa_bool_t save_aec;
162
163 pa_echo_canceller *ec;
164 uint32_t blocksize;
165
166 pa_bool_t need_realign;
167
168 /* to wakeup the source I/O thread */
169 pa_asyncmsgq *asyncmsgq;
170 pa_rtpoll_item *rtpoll_item_read, *rtpoll_item_write;
171
172 pa_source *source;
173 pa_bool_t source_auto_desc;
174 pa_source_output *source_output;
175 pa_memblockq *source_memblockq; /* echo canceler needs fixed sized chunks */
176 size_t source_skip;
177
178 pa_sink *sink;
179 pa_bool_t sink_auto_desc;
180 pa_sink_input *sink_input;
181 pa_memblockq *sink_memblockq;
182 int64_t send_counter; /* updated in sink IO thread */
183 int64_t recv_counter;
184 size_t sink_skip;
185
186 pa_atomic_t request_resync;
187
188 int active_mask;
189 pa_time_event *time_event;
190 pa_usec_t adjust_time;
191
192 FILE *captured_file;
193 FILE *played_file;
194 FILE *canceled_file;
195 };
196
197 static void source_output_snapshot_within_thread(struct userdata *u, struct snapshot *snapshot);
198
199 static const char* const valid_modargs[] = {
200 "source_name",
201 "source_properties",
202 "source_master",
203 "sink_name",
204 "sink_properties",
205 "sink_master",
206 "adjust_time",
207 "format",
208 "rate",
209 "channels",
210 "channel_map",
211 "aec_method",
212 "aec_args",
213 "save_aec",
214 "autoloaded",
215 "use_volume_sharing",
216 NULL
217 };
218
219 enum {
220 SOURCE_OUTPUT_MESSAGE_POST = PA_SOURCE_OUTPUT_MESSAGE_MAX,
221 SOURCE_OUTPUT_MESSAGE_REWIND,
222 SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT,
223 SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME
224 };
225
226 enum {
227 SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT
228 };
229
230 static int64_t calc_diff(struct userdata *u, struct snapshot *snapshot) {
231 int64_t buffer, diff_time, buffer_latency;
232
233 /* get the number of samples between capture and playback */
234 if (snapshot->plen > snapshot->rlen)
235 buffer = snapshot->plen - snapshot->rlen;
236 else
237 buffer = 0;
238
239 buffer += snapshot->source_delay + snapshot->sink_delay;
240
241 /* add the amount of samples not yet transferred to the source context */
242 if (snapshot->recv_counter <= snapshot->send_counter)
243 buffer += (int64_t) (snapshot->send_counter - snapshot->recv_counter);
244 else
245 buffer += PA_CLIP_SUB(buffer, (int64_t) (snapshot->recv_counter - snapshot->send_counter));
246
247 /* convert to time */
248 buffer_latency = pa_bytes_to_usec(buffer, &u->source_output->sample_spec);
249
250 /* capture and playback samples are perfectly aligned when diff_time is 0 */
251 diff_time = (snapshot->sink_now + snapshot->sink_latency - buffer_latency) -
252 (snapshot->source_now - snapshot->source_latency);
253
254 pa_log_debug("diff %lld (%lld - %lld + %lld) %lld %lld %lld %lld", (long long) diff_time,
255 (long long) snapshot->sink_latency,
256 (long long) buffer_latency, (long long) snapshot->source_latency,
257 (long long) snapshot->source_delay, (long long) snapshot->sink_delay,
258 (long long) (snapshot->send_counter - snapshot->recv_counter),
259 (long long) (snapshot->sink_now - snapshot->source_now));
260
261 return diff_time;
262 }
263
264 /* Called from main context */
265 static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
266 struct userdata *u = userdata;
267 uint32_t old_rate, base_rate, new_rate;
268 int64_t diff_time;
269 /*size_t fs*/
270 struct snapshot latency_snapshot;
271
272 pa_assert(u);
273 pa_assert(a);
274 pa_assert(u->time_event == e);
275 pa_assert_ctl_context();
276
277 if (u->active_mask != 3)
278 return;
279
280 /* update our snapshots */
281 pa_asyncmsgq_send(u->source_output->source->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT, &latency_snapshot, 0, NULL);
282 pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, &latency_snapshot, 0, NULL);
283
284 /* calculate drift between capture and playback */
285 diff_time = calc_diff(u, &latency_snapshot);
286
287 /*fs = pa_frame_size(&u->source_output->sample_spec);*/
288 old_rate = u->sink_input->sample_spec.rate;
289 base_rate = u->source_output->sample_spec.rate;
290
291 if (diff_time < 0) {
292 /* recording before playback, we need to adjust quickly. The echo
293 * canceler does not work in this case. */
294 pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME,
295 NULL, diff_time, NULL, NULL);
296 /*new_rate = base_rate - ((pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
297 new_rate = base_rate;
298 }
299 else {
300 if (diff_time > 1000) {
301 /* diff too big, quickly adjust */
302 pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME,
303 NULL, diff_time, NULL, NULL);
304 }
305
306 /* recording behind playback, we need to slowly adjust the rate to match */
307 /*new_rate = base_rate + ((pa_usec_to_bytes(diff_time, &u->source_output->sample_spec) / fs) * PA_USEC_PER_SEC) / u->adjust_time;*/
308
309 /* assume equal samplerates for now */
310 new_rate = base_rate;
311 }
312
313 /* make sure we don't make too big adjustments because that sounds horrible */
314 if (new_rate > base_rate * 1.1 || new_rate < base_rate * 0.9)
315 new_rate = base_rate;
316
317 if (new_rate != old_rate) {
318 pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate, (unsigned long) new_rate);
319
320 pa_sink_input_set_rate(u->sink_input, new_rate);
321 }
322
323 pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
324 }
325
326 /* Called from source I/O thread context */
327 static int source_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
328 struct userdata *u = PA_SOURCE(o)->userdata;
329
330 switch (code) {
331
332 case PA_SOURCE_MESSAGE_GET_LATENCY:
333
334 /* The source is _put() before the source output is, so let's
335 * make sure we don't access it in that time. Also, the
336 * source output is first shut down, the source second. */
337 if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
338 !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state)) {
339 *((pa_usec_t*) data) = 0;
340 return 0;
341 }
342
343 *((pa_usec_t*) data) =
344
345 /* Get the latency of the master source */
346 pa_source_get_latency_within_thread(u->source_output->source) +
347 /* Add the latency internal to our source output on top */
348 pa_bytes_to_usec(pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq), &u->source_output->source->sample_spec) +
349 /* and the buffering we do on the source */
350 pa_bytes_to_usec(u->blocksize, &u->source_output->source->sample_spec);
351
352 return 0;
353
354 }
355
356 return pa_source_process_msg(o, code, data, offset, chunk);
357 }
358
359 /* Called from sink I/O thread context */
360 static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
361 struct userdata *u = PA_SINK(o)->userdata;
362
363 switch (code) {
364
365 case PA_SINK_MESSAGE_GET_LATENCY:
366
367 /* The sink is _put() before the sink input is, so let's
368 * make sure we don't access it in that time. Also, the
369 * sink input is first shut down, the sink second. */
370 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
371 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
372 *((pa_usec_t*) data) = 0;
373 return 0;
374 }
375
376 *((pa_usec_t*) data) =
377
378 /* Get the latency of the master sink */
379 pa_sink_get_latency_within_thread(u->sink_input->sink) +
380
381 /* Add the latency internal to our sink input on top */
382 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
383
384 return 0;
385 }
386
387 return pa_sink_process_msg(o, code, data, offset, chunk);
388 }
389
390
391 /* Called from main context */
392 static int source_set_state_cb(pa_source *s, pa_source_state_t state) {
393 struct userdata *u;
394
395 pa_source_assert_ref(s);
396 pa_assert_se(u = s->userdata);
397
398 if (!PA_SOURCE_IS_LINKED(state) ||
399 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
400 return 0;
401
402 pa_log_debug("Source state %d %d", state, u->active_mask);
403
404 if (state == PA_SOURCE_RUNNING) {
405 /* restart timer when both sink and source are active */
406 u->active_mask |= 1;
407 if (u->active_mask == 3)
408 pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
409
410 pa_atomic_store(&u->request_resync, 1);
411 pa_source_output_cork(u->source_output, FALSE);
412 } else if (state == PA_SOURCE_SUSPENDED) {
413 u->active_mask &= ~1;
414 pa_source_output_cork(u->source_output, TRUE);
415 }
416 return 0;
417 }
418
419 /* Called from main context */
420 static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
421 struct userdata *u;
422
423 pa_sink_assert_ref(s);
424 pa_assert_se(u = s->userdata);
425
426 if (!PA_SINK_IS_LINKED(state) ||
427 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
428 return 0;
429
430 pa_log_debug("Sink state %d %d", state, u->active_mask);
431
432 if (state == PA_SINK_RUNNING) {
433 /* restart timer when both sink and source are active */
434 u->active_mask |= 2;
435 if (u->active_mask == 3)
436 pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time);
437
438 pa_atomic_store(&u->request_resync, 1);
439 pa_sink_input_cork(u->sink_input, FALSE);
440 } else if (state == PA_SINK_SUSPENDED) {
441 u->active_mask &= ~2;
442 pa_sink_input_cork(u->sink_input, TRUE);
443 }
444 return 0;
445 }
446
447 /* Called from I/O thread context */
448 static void source_update_requested_latency_cb(pa_source *s) {
449 struct userdata *u;
450
451 pa_source_assert_ref(s);
452 pa_assert_se(u = s->userdata);
453
454 if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
455 !PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state))
456 return;
457
458 pa_log_debug("Source update requested latency");
459
460 /* Just hand this one over to the master source */
461 pa_source_output_set_requested_latency_within_thread(
462 u->source_output,
463 pa_source_get_requested_latency_within_thread(s));
464 }
465
466 /* Called from I/O thread context */
467 static void sink_update_requested_latency_cb(pa_sink *s) {
468 struct userdata *u;
469
470 pa_sink_assert_ref(s);
471 pa_assert_se(u = s->userdata);
472
473 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
474 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
475 return;
476
477 pa_log_debug("Sink update requested latency");
478
479 /* Just hand this one over to the master sink */
480 pa_sink_input_set_requested_latency_within_thread(
481 u->sink_input,
482 pa_sink_get_requested_latency_within_thread(s));
483 }
484
485 /* Called from I/O thread context */
486 static void sink_request_rewind_cb(pa_sink *s) {
487 struct userdata *u;
488
489 pa_sink_assert_ref(s);
490 pa_assert_se(u = s->userdata);
491
492 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
493 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
494 return;
495
496 pa_log_debug("Sink request rewind %lld", (long long) s->thread_info.rewind_nbytes);
497
498 /* Just hand this one over to the master sink */
499 pa_sink_input_request_rewind(u->sink_input,
500 s->thread_info.rewind_nbytes, TRUE, FALSE, FALSE);
501 }
502
503 /* Called from main context */
504 static void source_set_volume_cb(pa_source *s) {
505 struct userdata *u;
506
507 pa_source_assert_ref(s);
508 pa_assert_se(u = s->userdata);
509
510 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
511 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
512 return;
513
514 pa_source_output_set_volume(u->source_output, &s->real_volume, s->save_volume, TRUE);
515 }
516
517 /* Called from main context */
518 static void sink_set_volume_cb(pa_sink *s) {
519 struct userdata *u;
520
521 pa_sink_assert_ref(s);
522 pa_assert_se(u = s->userdata);
523
524 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
525 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
526 return;
527
528 pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
529 }
530
531 static void source_get_volume_cb(pa_source *s) {
532 struct userdata *u;
533 pa_cvolume v;
534
535 pa_source_assert_ref(s);
536 pa_assert_se(u = s->userdata);
537
538 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
539 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
540 return;
541
542 pa_source_output_get_volume(u->source_output, &v, TRUE);
543
544 if (pa_cvolume_equal(&s->real_volume, &v))
545 /* no change */
546 return;
547
548 s->real_volume = v;
549 pa_source_set_soft_volume(s, NULL);
550 }
551
552 /* Called from main context */
553 static void source_set_mute_cb(pa_source *s) {
554 struct userdata *u;
555
556 pa_source_assert_ref(s);
557 pa_assert_se(u = s->userdata);
558
559 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
560 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
561 return;
562
563 pa_source_output_set_mute(u->source_output, s->muted, s->save_muted);
564 }
565
566 /* Called from main context */
567 static void sink_set_mute_cb(pa_sink *s) {
568 struct userdata *u;
569
570 pa_sink_assert_ref(s);
571 pa_assert_se(u = s->userdata);
572
573 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
574 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
575 return;
576
577 pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
578 }
579
580 /* Called from main context */
581 static void source_get_mute_cb(pa_source *s) {
582 struct userdata *u;
583
584 pa_source_assert_ref(s);
585 pa_assert_se(u = s->userdata);
586
587 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)) ||
588 !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
589 return;
590
591 pa_source_output_get_mute(u->source_output);
592 }
593
594 /* must be called from the input thread context */
595 static void apply_diff_time(struct userdata *u, int64_t diff_time) {
596 int64_t diff;
597
598 if (diff_time < 0) {
599 diff = pa_usec_to_bytes(-diff_time, &u->source_output->sample_spec);
600
601 if (diff > 0) {
602 /* add some extra safety samples to compensate for jitter in the
603 * timings */
604 diff += 10 * pa_frame_size (&u->source_output->sample_spec);
605
606 pa_log("Playback after capture (%lld), drop sink %lld", (long long) diff_time, (long long) diff);
607
608 u->sink_skip = diff;
609 u->source_skip = 0;
610 }
611 } else if (diff_time > 0) {
612 diff = pa_usec_to_bytes(diff_time, &u->source_output->sample_spec);
613
614 if (diff > 0) {
615 pa_log("playback too far ahead (%lld), drop source %lld", (long long) diff_time, (long long) diff);
616
617 u->source_skip = diff;
618 u->sink_skip = 0;
619 }
620 }
621 }
622
623 /* must be called from the input thread */
624 static void do_resync(struct userdata *u) {
625 int64_t diff_time;
626 struct snapshot latency_snapshot;
627
628 pa_log("Doing resync");
629
630 /* update our snapshot */
631 source_output_snapshot_within_thread(u, &latency_snapshot);
632 pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, &latency_snapshot, 0, NULL);
633
634 /* calculate drift between capture and playback */
635 diff_time = calc_diff(u, &latency_snapshot);
636
637 /* and adjust for the drift */
638 apply_diff_time(u, diff_time);
639 }
640
641 /* Called from input thread context */
642 static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) {
643 struct userdata *u;
644 size_t rlen, plen;
645
646 pa_source_output_assert_ref(o);
647 pa_source_output_assert_io_context(o);
648 pa_assert_se(u = o->userdata);
649
650 if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output))) {
651 pa_log("push when no link?");
652 return;
653 }
654
655 /* handle queued messages, do any message sending of our own */
656 while (pa_asyncmsgq_process_one(u->asyncmsgq) > 0)
657 ;
658
659 if (pa_atomic_cmpxchg (&u->request_resync, 1, 0)) {
660 do_resync(u);
661 }
662
663 pa_memblockq_push_align(u->source_memblockq, chunk);
664
665 rlen = pa_memblockq_get_length(u->source_memblockq);
666 plen = pa_memblockq_get_length(u->sink_memblockq);
667
668 while (rlen >= u->blocksize) {
669 pa_memchunk rchunk, pchunk;
670
671 /* take fixed block from recorded samples */
672 pa_memblockq_peek_fixed_size(u->source_memblockq, u->blocksize, &rchunk);
673
674 if (plen > u->blocksize && u->source_skip == 0) {
675 uint8_t *rdata, *pdata, *cdata;
676 pa_memchunk cchunk;
677 int unused;
678
679 if (u->sink_skip) {
680 size_t to_skip;
681
682 if (u->sink_skip > plen)
683 to_skip = plen;
684 else
685 to_skip = u->sink_skip;
686
687 pa_memblockq_drop(u->sink_memblockq, to_skip);
688 plen -= to_skip;
689
690 u->sink_skip -= to_skip;
691 }
692
693 if (plen > u->blocksize && u->sink_skip == 0) {
694 /* take fixed block from played samples */
695 pa_memblockq_peek_fixed_size(u->sink_memblockq, u->blocksize, &pchunk);
696
697 rdata = pa_memblock_acquire(rchunk.memblock);
698 rdata += rchunk.index;
699 pdata = pa_memblock_acquire(pchunk.memblock);
700 pdata += pchunk.index;
701
702 cchunk.index = 0;
703 cchunk.length = u->blocksize;
704 cchunk.memblock = pa_memblock_new(u->source->core->mempool, cchunk.length);
705 cdata = pa_memblock_acquire(cchunk.memblock);
706
707 if (u->save_aec) {
708 if (u->captured_file)
709 unused = fwrite(rdata, 1, u->blocksize, u->captured_file);
710 if (u->played_file)
711 unused = fwrite(pdata, 1, u->blocksize, u->played_file);
712 }
713
714 /* perform echo cancellation */
715 u->ec->run(u->ec, rdata, pdata, cdata);
716
717 if (u->save_aec) {
718 if (u->canceled_file)
719 unused = fwrite(cdata, 1, u->blocksize, u->canceled_file);
720 }
721
722 pa_memblock_release(cchunk.memblock);
723 pa_memblock_release(pchunk.memblock);
724 pa_memblock_release(rchunk.memblock);
725
726 /* drop consumed sink samples */
727 pa_memblockq_drop(u->sink_memblockq, u->blocksize);
728 pa_memblock_unref(pchunk.memblock);
729
730 pa_memblock_unref(rchunk.memblock);
731 /* the filtered samples now become the samples from our
732 * source */
733 rchunk = cchunk;
734
735 plen -= u->blocksize;
736 }
737 }
738
739 /* forward the (echo-canceled) data to the virtual source */
740 pa_source_post(u->source, &rchunk);
741 pa_memblock_unref(rchunk.memblock);
742
743 pa_memblockq_drop(u->source_memblockq, u->blocksize);
744 rlen -= u->blocksize;
745
746 if (u->source_skip) {
747 if (u->source_skip > u->blocksize) {
748 u->source_skip -= u->blocksize;
749 }
750 else {
751 u->sink_skip += (u->blocksize - u->source_skip);
752 u->source_skip = 0;
753 }
754 }
755 }
756 }
757
758 /* Called from I/O thread context */
759 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
760 struct userdata *u;
761
762 pa_sink_input_assert_ref(i);
763 pa_assert(chunk);
764 pa_assert_se(u = i->userdata);
765
766 if (u->sink->thread_info.rewind_requested)
767 pa_sink_process_rewind(u->sink, 0);
768
769 pa_sink_render_full(u->sink, nbytes, chunk);
770
771 if (i->thread_info.underrun_for > 0) {
772 pa_log_debug("Handling end of underrun.");
773 pa_atomic_store(&u->request_resync, 1);
774 }
775
776 /* let source thread handle the chunk. pass the sample count as well so that
777 * the source IO thread can update the right variables. */
778 pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_POST,
779 NULL, 0, chunk, NULL);
780 u->send_counter += chunk->length;
781
782 return 0;
783 }
784
785 /* Called from input thread context */
786 static void source_output_process_rewind_cb(pa_source_output *o, size_t nbytes) {
787 struct userdata *u;
788
789 pa_source_output_assert_ref(o);
790 pa_source_output_assert_io_context(o);
791 pa_assert_se(u = o->userdata);
792
793 pa_source_process_rewind(u->source, nbytes);
794
795 /* go back on read side, we need to use older sink data for this */
796 pa_memblockq_rewind(u->sink_memblockq, nbytes);
797
798 /* manipulate write index */
799 pa_memblockq_seek(u->source_memblockq, -nbytes, PA_SEEK_RELATIVE, TRUE);
800
801 pa_log_debug("Source rewind (%lld) %lld", (long long) nbytes,
802 (long long) pa_memblockq_get_length (u->source_memblockq));
803 }
804
805 /* Called from I/O thread context */
806 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
807 struct userdata *u;
808
809 pa_sink_input_assert_ref(i);
810 pa_assert_se(u = i->userdata);
811
812 pa_log_debug("Sink process rewind %lld", (long long) nbytes);
813
814 pa_sink_process_rewind(u->sink, nbytes);
815
816 pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_REWIND, NULL, (int64_t) nbytes, NULL, NULL);
817 u->send_counter -= nbytes;
818 }
819
820 static void source_output_snapshot_within_thread(struct userdata *u, struct snapshot *snapshot) {
821 size_t delay, rlen, plen;
822 pa_usec_t now, latency;
823
824 now = pa_rtclock_now();
825 latency = pa_source_get_latency_within_thread(u->source_output->source);
826 delay = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq);
827
828 delay = (u->source_output->thread_info.resampler ? pa_resampler_request(u->source_output->thread_info.resampler, delay) : delay);
829 rlen = pa_memblockq_get_length(u->source_memblockq);
830 plen = pa_memblockq_get_length(u->sink_memblockq);
831
832 snapshot->source_now = now;
833 snapshot->source_latency = latency;
834 snapshot->source_delay = delay;
835 snapshot->recv_counter = u->recv_counter;
836 snapshot->rlen = rlen + u->sink_skip;
837 snapshot->plen = plen + u->source_skip;
838 }
839
840
841 /* Called from output thread context */
842 static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
843 struct userdata *u = PA_SOURCE_OUTPUT(obj)->userdata;
844
845 switch (code) {
846
847 case SOURCE_OUTPUT_MESSAGE_POST:
848
849 pa_source_output_assert_io_context(u->source_output);
850
851 if (PA_SOURCE_IS_OPENED(u->source_output->source->thread_info.state))
852 pa_memblockq_push_align(u->sink_memblockq, chunk);
853 else
854 pa_memblockq_flush_write(u->sink_memblockq, TRUE);
855
856 u->recv_counter += (int64_t) chunk->length;
857
858 return 0;
859
860 case SOURCE_OUTPUT_MESSAGE_REWIND:
861 pa_source_output_assert_io_context(u->source_output);
862
863 /* manipulate write index, never go past what we have */
864 if (PA_SOURCE_IS_OPENED(u->source_output->source->thread_info.state))
865 pa_memblockq_seek(u->sink_memblockq, -offset, PA_SEEK_RELATIVE, TRUE);
866 else
867 pa_memblockq_flush_write(u->sink_memblockq, TRUE);
868
869 pa_log_debug("Sink rewind (%lld)", (long long) offset);
870
871 u->recv_counter -= offset;
872
873 return 0;
874
875 case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT: {
876 struct snapshot *snapshot = (struct snapshot *) data;
877
878 source_output_snapshot_within_thread(u, snapshot);
879 return 0;
880 }
881
882 case SOURCE_OUTPUT_MESSAGE_APPLY_DIFF_TIME:
883 apply_diff_time(u, offset);
884 return 0;
885
886 }
887
888 return pa_source_output_process_msg(obj, code, data, offset, chunk);
889 }
890
891 static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
892 struct userdata *u = PA_SINK_INPUT(obj)->userdata;
893
894 switch (code) {
895
896 case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT: {
897 size_t delay;
898 pa_usec_t now, latency;
899 struct snapshot *snapshot = (struct snapshot *) data;
900
901 pa_sink_input_assert_io_context(u->sink_input);
902
903 now = pa_rtclock_now();
904 latency = pa_sink_get_latency_within_thread(u->sink_input->sink);
905 delay = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq);
906
907 delay = (u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, delay) : delay);
908
909 snapshot->sink_now = now;
910 snapshot->sink_latency = latency;
911 snapshot->sink_delay = delay;
912 snapshot->send_counter = u->send_counter;
913 return 0;
914 }
915 }
916
917 return pa_sink_input_process_msg(obj, code, data, offset, chunk);
918 }
919
920 /* Called from I/O thread context */
921 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
922 struct userdata *u;
923
924 pa_sink_input_assert_ref(i);
925 pa_assert_se(u = i->userdata);
926
927 pa_log_debug("Sink input update max rewind %lld", (long long) nbytes);
928
929 pa_memblockq_set_maxrewind(u->sink_memblockq, nbytes);
930 pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
931 }
932
933 /* Called from I/O thread context */
934 static void source_output_update_max_rewind_cb(pa_source_output *o, size_t nbytes) {
935 struct userdata *u;
936
937 pa_source_output_assert_ref(o);
938 pa_assert_se(u = o->userdata);
939
940 pa_log_debug("Source output update max rewind %lld", (long long) nbytes);
941
942 pa_source_set_max_rewind_within_thread(u->source, nbytes);
943 }
944
945 /* Called from I/O thread context */
946 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
947 struct userdata *u;
948
949 pa_sink_input_assert_ref(i);
950 pa_assert_se(u = i->userdata);
951
952 pa_log_debug("Sink input update max request %lld", (long long) nbytes);
953
954 pa_sink_set_max_request_within_thread(u->sink, nbytes);
955 }
956
957 /* Called from I/O thread context */
958 static void sink_input_update_sink_requested_latency_cb(pa_sink_input *i) {
959 struct userdata *u;
960 pa_usec_t latency;
961
962 pa_sink_input_assert_ref(i);
963 pa_assert_se(u = i->userdata);
964
965 latency = pa_sink_get_requested_latency_within_thread(i->sink);
966
967 pa_log_debug("Sink input update requested latency %lld", (long long) latency);
968 }
969
970 /* Called from I/O thread context */
971 static void source_output_update_source_requested_latency_cb(pa_source_output *o) {
972 struct userdata *u;
973 pa_usec_t latency;
974
975 pa_source_output_assert_ref(o);
976 pa_assert_se(u = o->userdata);
977
978 latency = pa_source_get_requested_latency_within_thread(o->source);
979
980 pa_log_debug("source output update requested latency %lld", (long long) latency);
981 }
982
983 /* Called from I/O thread context */
984 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
985 struct userdata *u;
986
987 pa_sink_input_assert_ref(i);
988 pa_assert_se(u = i->userdata);
989
990 pa_log_debug("Sink input update latency range %lld %lld",
991 (long long) i->sink->thread_info.min_latency,
992 (long long) i->sink->thread_info.max_latency);
993
994 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
995 }
996
997 /* Called from I/O thread context */
998 static void source_output_update_source_latency_range_cb(pa_source_output *o) {
999 struct userdata *u;
1000
1001 pa_source_output_assert_ref(o);
1002 pa_assert_se(u = o->userdata);
1003
1004 pa_log_debug("Source output update latency range %lld %lld",
1005 (long long) o->source->thread_info.min_latency,
1006 (long long) o->source->thread_info.max_latency);
1007
1008 pa_source_set_latency_range_within_thread(u->source, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
1009 }
1010
1011 /* Called from I/O thread context */
1012 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
1013 struct userdata *u;
1014
1015 pa_sink_input_assert_ref(i);
1016 pa_assert_se(u = i->userdata);
1017
1018 pa_log_debug("Sink input update fixed latency %lld",
1019 (long long) i->sink->thread_info.fixed_latency);
1020
1021 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
1022 }
1023
1024 /* Called from I/O thread context */
1025 static void source_output_update_source_fixed_latency_cb(pa_source_output *o) {
1026 struct userdata *u;
1027
1028 pa_source_output_assert_ref(o);
1029 pa_assert_se(u = o->userdata);
1030
1031 pa_log_debug("Source output update fixed latency %lld",
1032 (long long) o->source->thread_info.fixed_latency);
1033
1034 pa_source_set_fixed_latency_within_thread(u->source, o->source->thread_info.fixed_latency);
1035 }
1036
1037 /* Called from output thread context */
1038 static void source_output_attach_cb(pa_source_output *o) {
1039 struct userdata *u;
1040
1041 pa_source_output_assert_ref(o);
1042 pa_source_output_assert_io_context(o);
1043 pa_assert_se(u = o->userdata);
1044
1045 pa_source_set_rtpoll(u->source, o->source->thread_info.rtpoll);
1046 pa_source_set_latency_range_within_thread(u->source, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
1047 pa_source_set_fixed_latency_within_thread(u->source, o->source->thread_info.fixed_latency);
1048 pa_source_set_max_rewind_within_thread(u->source, pa_source_output_get_max_rewind(o));
1049
1050 pa_log_debug("Source output %d attach", o->index);
1051
1052 pa_source_attach_within_thread(u->source);
1053
1054 u->rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
1055 o->source->thread_info.rtpoll,
1056 PA_RTPOLL_LATE,
1057 u->asyncmsgq);
1058 }
1059
1060 /* Called from I/O thread context */
1061 static void sink_input_attach_cb(pa_sink_input *i) {
1062 struct userdata *u;
1063
1064 pa_sink_input_assert_ref(i);
1065 pa_assert_se(u = i->userdata);
1066
1067 pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
1068 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
1069
1070 /* (8.1) IF YOU NEED A FIXED BLOCK SIZE ADD THE LATENCY FOR ONE
1071 * BLOCK MINUS ONE SAMPLE HERE. SEE (7) */
1072 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
1073
1074 /* (8.2) IF YOU NEED A FIXED BLOCK SIZE ROUND
1075 * pa_sink_input_get_max_request(i) UP TO MULTIPLES OF IT
1076 * HERE. SEE (6) */
1077 pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
1078 pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
1079
1080 pa_log_debug("Sink input %d attach", i->index);
1081
1082 u->rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
1083 i->sink->thread_info.rtpoll,
1084 PA_RTPOLL_LATE,
1085 u->asyncmsgq);
1086
1087 pa_sink_attach_within_thread(u->sink);
1088 }
1089
1090
1091 /* Called from output thread context */
1092 static void source_output_detach_cb(pa_source_output *o) {
1093 struct userdata *u;
1094
1095 pa_source_output_assert_ref(o);
1096 pa_source_output_assert_io_context(o);
1097 pa_assert_se(u = o->userdata);
1098
1099 pa_source_detach_within_thread(u->source);
1100 pa_source_set_rtpoll(u->source, NULL);
1101
1102 pa_log_debug("Source output %d detach", o->index);
1103
1104 if (u->rtpoll_item_read) {
1105 pa_rtpoll_item_free(u->rtpoll_item_read);
1106 u->rtpoll_item_read = NULL;
1107 }
1108 }
1109
1110 /* Called from I/O thread context */
1111 static void sink_input_detach_cb(pa_sink_input *i) {
1112 struct userdata *u;
1113
1114 pa_sink_input_assert_ref(i);
1115 pa_assert_se(u = i->userdata);
1116
1117 pa_sink_detach_within_thread(u->sink);
1118
1119 pa_sink_set_rtpoll(u->sink, NULL);
1120
1121 pa_log_debug("Sink input %d detach", i->index);
1122
1123 if (u->rtpoll_item_write) {
1124 pa_rtpoll_item_free(u->rtpoll_item_write);
1125 u->rtpoll_item_write = NULL;
1126 }
1127 }
1128
1129 /* Called from output thread context */
1130 static void source_output_state_change_cb(pa_source_output *o, pa_source_output_state_t state) {
1131 struct userdata *u;
1132
1133 pa_source_output_assert_ref(o);
1134 pa_source_output_assert_io_context(o);
1135 pa_assert_se(u = o->userdata);
1136
1137 pa_log_debug("Source output %d state %d", o->index, state);
1138 }
1139
1140 /* Called from IO thread context */
1141 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
1142 struct userdata *u;
1143
1144 pa_sink_input_assert_ref(i);
1145 pa_assert_se(u = i->userdata);
1146
1147 pa_log_debug("Sink input %d state %d", i->index, state);
1148
1149 /* If we are added for the first time, ask for a rewinding so that
1150 * we are heard right-away. */
1151 if (PA_SINK_INPUT_IS_LINKED(state) &&
1152 i->thread_info.state == PA_SINK_INPUT_INIT) {
1153 pa_log_debug("Requesting rewind due to state change.");
1154 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
1155 }
1156 }
1157
1158 /* Called from main thread */
1159 static void source_output_kill_cb(pa_source_output *o) {
1160 struct userdata *u;
1161
1162 pa_source_output_assert_ref(o);
1163 pa_assert_ctl_context();
1164 pa_assert_se(u = o->userdata);
1165
1166 u->dead = TRUE;
1167
1168 /* The order here matters! We first kill the source output, followed
1169 * by the source. That means the source callbacks must be protected
1170 * against an unconnected source output! */
1171 pa_source_output_unlink(u->source_output);
1172 pa_source_unlink(u->source);
1173
1174 pa_source_output_unref(u->source_output);
1175 u->source_output = NULL;
1176
1177 pa_source_unref(u->source);
1178 u->source = NULL;
1179
1180 pa_log_debug("Source output kill %d", o->index);
1181
1182 pa_module_unload_request(u->module, TRUE);
1183 }
1184
1185 /* Called from main context */
1186 static void sink_input_kill_cb(pa_sink_input *i) {
1187 struct userdata *u;
1188
1189 pa_sink_input_assert_ref(i);
1190 pa_assert_se(u = i->userdata);
1191
1192 u->dead = TRUE;
1193
1194 /* The order here matters! We first kill the sink input, followed
1195 * by the sink. That means the sink callbacks must be protected
1196 * against an unconnected sink input! */
1197 pa_sink_input_unlink(u->sink_input);
1198 pa_sink_unlink(u->sink);
1199
1200 pa_sink_input_unref(u->sink_input);
1201 u->sink_input = NULL;
1202
1203 pa_sink_unref(u->sink);
1204 u->sink = NULL;
1205
1206 pa_log_debug("Sink input kill %d", i->index);
1207
1208 pa_module_unload_request(u->module, TRUE);
1209 }
1210
1211 /* Called from main thread */
1212 static pa_bool_t source_output_may_move_to_cb(pa_source_output *o, pa_source *dest) {
1213 struct userdata *u;
1214
1215 pa_source_output_assert_ref(o);
1216 pa_assert_ctl_context();
1217 pa_assert_se(u = o->userdata);
1218
1219 if (u->dead)
1220 return FALSE;
1221
1222 return (u->source != dest) && (u->sink != dest->monitor_of);
1223 }
1224
1225 /* Called from main context */
1226 static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
1227 struct userdata *u;
1228
1229 pa_sink_input_assert_ref(i);
1230 pa_assert_se(u = i->userdata);
1231
1232 if (u->dead)
1233 return FALSE;
1234
1235 return u->sink != dest;
1236 }
1237
1238 /* Called from main thread */
1239 static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
1240 struct userdata *u;
1241
1242 pa_source_output_assert_ref(o);
1243 pa_assert_ctl_context();
1244 pa_assert_se(u = o->userdata);
1245
1246 if (dest) {
1247 pa_source_set_asyncmsgq(u->source, dest->asyncmsgq);
1248 pa_source_update_flags(u->source, PA_SOURCE_LATENCY|PA_SOURCE_DYNAMIC_LATENCY, dest->flags);
1249 } else
1250 pa_source_set_asyncmsgq(u->source, NULL);
1251
1252 if (u->source_auto_desc && dest) {
1253 const char *z;
1254 pa_proplist *pl;
1255
1256 pl = pa_proplist_new();
1257 z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
1258 pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Source %s on %s",
1259 pa_proplist_gets(u->source->proplist, "device.echo-cancel.name"), z ? z : dest->name);
1260
1261 pa_source_update_proplist(u->source, PA_UPDATE_REPLACE, pl);
1262 pa_proplist_free(pl);
1263 }
1264 }
1265
1266 /* Called from main context */
1267 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
1268 struct userdata *u;
1269
1270 pa_sink_input_assert_ref(i);
1271 pa_assert_se(u = i->userdata);
1272
1273 if (dest) {
1274 pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
1275 pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
1276 } else
1277 pa_sink_set_asyncmsgq(u->sink, NULL);
1278
1279 if (u->sink_auto_desc && dest) {
1280 const char *z;
1281 pa_proplist *pl;
1282
1283 pl = pa_proplist_new();
1284 z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
1285 pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Sink %s on %s",
1286 pa_proplist_gets(u->sink->proplist, "device.echo-cancel.name"), z ? z : dest->name);
1287
1288 pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
1289 pa_proplist_free(pl);
1290 }
1291 }
1292
1293 /* Called from main context */
1294 static void sink_input_volume_changed_cb(pa_sink_input *i) {
1295 struct userdata *u;
1296
1297 pa_sink_input_assert_ref(i);
1298 pa_assert_se(u = i->userdata);
1299
1300 pa_sink_volume_changed(u->sink, &i->volume);
1301 }
1302
1303 /* Called from main context */
1304 static void sink_input_mute_changed_cb(pa_sink_input *i) {
1305 struct userdata *u;
1306
1307 pa_sink_input_assert_ref(i);
1308 pa_assert_se(u = i->userdata);
1309
1310 pa_sink_mute_changed(u->sink, i->muted);
1311 }
1312
1313 static pa_echo_canceller_method_t get_ec_method_from_string(const char *method) {
1314 if (pa_streq(method, "speex"))
1315 return PA_ECHO_CANCELLER_SPEEX;
1316 else if (pa_streq(method, "adrian"))
1317 return PA_ECHO_CANCELLER_ADRIAN;
1318 else
1319 return PA_ECHO_CANCELLER_INVALID;
1320 }
1321
1322 /* Common initialisation bits between module-echo-cancel and the standalone test program */
1323 static int init_common(pa_modargs *ma, struct userdata *u, pa_sample_spec *source_ss, pa_channel_map *source_map) {
1324 pa_echo_canceller_method_t ec_method;
1325
1326 if (pa_modargs_get_sample_spec_and_channel_map(ma, source_ss, source_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
1327 pa_log("Invalid sample format specification or channel map");
1328 goto fail;
1329 }
1330
1331 u->ec = pa_xnew0(pa_echo_canceller, 1);
1332 if (!u->ec) {
1333 pa_log("Failed to alloc echo canceller");
1334 goto fail;
1335 }
1336
1337 if ((ec_method = get_ec_method_from_string(pa_modargs_get_value(ma, "aec_method", DEFAULT_ECHO_CANCELLER))) < 0) {
1338 pa_log("Invalid echo canceller implementation");
1339 goto fail;
1340 }
1341
1342 u->ec->init = ec_table[ec_method].init;
1343 u->ec->run = ec_table[ec_method].run;
1344 u->ec->done = ec_table[ec_method].done;
1345
1346 return 0;
1347
1348 fail:
1349 return -1;
1350 }
1351
1352
1353 int pa__init(pa_module*m) {
1354 struct userdata *u;
1355 pa_sample_spec source_ss, sink_ss;
1356 pa_channel_map source_map, sink_map;
1357 pa_modargs *ma;
1358 pa_source *source_master=NULL;
1359 pa_sink *sink_master=NULL;
1360 pa_source_output_new_data source_output_data;
1361 pa_sink_input_new_data sink_input_data;
1362 pa_source_new_data source_data;
1363 pa_sink_new_data sink_data;
1364 pa_memchunk silence;
1365 uint32_t adjust_time_sec;
1366 pa_bool_t use_volume_sharing = TRUE;
1367
1368 pa_assert(m);
1369
1370 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1371 pa_log("Failed to parse module arguments.");
1372 goto fail;
1373 }
1374
1375 if (!(source_master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source_master", NULL), PA_NAMEREG_SOURCE))) {
1376 pa_log("Master source not found");
1377 goto fail;
1378 }
1379 pa_assert(source_master);
1380
1381 if (!(sink_master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink_master", NULL), PA_NAMEREG_SINK))) {
1382 pa_log("Master sink not found");
1383 goto fail;
1384 }
1385 pa_assert(sink_master);
1386
1387 if (source_master->monitor_of == sink_master) {
1388 pa_log("Can't cancel echo between a sink and its monitor");
1389 goto fail;
1390 }
1391
1392 source_ss = source_master->sample_spec;
1393 source_ss.rate = DEFAULT_RATE;
1394 source_ss.channels = DEFAULT_CHANNELS;
1395 pa_channel_map_init_auto(&source_map, source_ss.channels, PA_CHANNEL_MAP_DEFAULT);
1396
1397 sink_ss = sink_master->sample_spec;
1398 sink_map = sink_master->channel_map;
1399
1400 if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
1401 pa_log("use_volume_sharing= expects a boolean argument");
1402 goto fail;
1403 }
1404
1405 u = pa_xnew0(struct userdata, 1);
1406 if (!u) {
1407 pa_log("Failed to alloc userdata");
1408 goto fail;
1409 }
1410 u->core = m->core;
1411 u->module = m;
1412 m->userdata = u;
1413 u->dead = FALSE;
1414
1415 adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
1416 if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
1417 pa_log("Failed to parse adjust_time value");
1418 goto fail;
1419 }
1420
1421 if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
1422 u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
1423 else
1424 u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
1425
1426 u->save_aec = DEFAULT_SAVE_AEC;
1427 if (pa_modargs_get_value_boolean(ma, "save_aec", &u->save_aec) < 0) {
1428 pa_log("Failed to parse save_aec value");
1429 goto fail;
1430 }
1431
1432 u->autoloaded = DEFAULT_AUTOLOADED;
1433 if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
1434 pa_log("Failed to parse autoloaded value");
1435 goto fail;
1436 }
1437
1438 if (init_common(ma, u, &source_ss, &source_map))
1439 goto fail;
1440
1441 u->asyncmsgq = pa_asyncmsgq_new(0);
1442 u->need_realign = TRUE;
1443
1444 if (u->ec->init) {
1445 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))) {
1446 pa_log("Failed to init AEC engine");
1447 goto fail;
1448 }
1449 }
1450
1451 /* Create source */
1452 pa_source_new_data_init(&source_data);
1453 source_data.driver = __FILE__;
1454 source_data.module = m;
1455 if (!(source_data.name = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
1456 source_data.name = pa_sprintf_malloc("%s.echo-cancel", source_master->name);
1457 pa_source_new_data_set_sample_spec(&source_data, &source_ss);
1458 pa_source_new_data_set_channel_map(&source_data, &source_map);
1459 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, source_master->name);
1460 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1461 if (!u->autoloaded)
1462 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
1463 pa_proplist_sets(source_data.proplist, "device.echo-cancel.name", source_data.name);
1464
1465 if (pa_modargs_get_proplist(ma, "source_properties", source_data.proplist, PA_UPDATE_REPLACE) < 0) {
1466 pa_log("Invalid properties");
1467 pa_source_new_data_done(&source_data);
1468 goto fail;
1469 }
1470
1471 if ((u->source_auto_desc = !pa_proplist_contains(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
1472 const char *z;
1473
1474 z = pa_proplist_gets(source_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
1475 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Source %s on %s", source_data.name, z ? z : source_master->name);
1476 }
1477
1478 u->source = pa_source_new(m->core, &source_data, (source_master->flags & (PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY))
1479 | (use_volume_sharing ? PA_SOURCE_SHARE_VOLUME_WITH_MASTER : 0));
1480 pa_source_new_data_done(&source_data);
1481
1482 if (!u->source) {
1483 pa_log("Failed to create source.");
1484 goto fail;
1485 }
1486
1487 u->source->parent.process_msg = source_process_msg_cb;
1488 u->source->set_state = source_set_state_cb;
1489 u->source->update_requested_latency = source_update_requested_latency_cb;
1490 pa_source_set_get_mute_callback(u->source, source_get_mute_cb);
1491 pa_source_set_set_mute_callback(u->source, source_set_mute_cb);
1492 if (!use_volume_sharing) {
1493 pa_source_set_get_volume_callback(u->source, source_get_volume_cb);
1494 pa_source_set_set_volume_callback(u->source, source_set_volume_cb);
1495 pa_source_enable_decibel_volume(u->source, TRUE);
1496 }
1497 u->source->userdata = u;
1498
1499 pa_source_set_asyncmsgq(u->source, source_master->asyncmsgq);
1500
1501 /* Create sink */
1502 pa_sink_new_data_init(&sink_data);
1503 sink_data.driver = __FILE__;
1504 sink_data.module = m;
1505 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
1506 sink_data.name = pa_sprintf_malloc("%s.echo-cancel", sink_master->name);
1507 pa_sink_new_data_set_sample_spec(&sink_data, &sink_ss);
1508 pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
1509 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, sink_master->name);
1510 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1511 if (!u->autoloaded)
1512 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
1513 pa_proplist_sets(sink_data.proplist, "device.echo-cancel.name", sink_data.name);
1514
1515 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
1516 pa_log("Invalid properties");
1517 pa_sink_new_data_done(&sink_data);
1518 goto fail;
1519 }
1520
1521 if ((u->sink_auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
1522 const char *z;
1523
1524 z = pa_proplist_gets(sink_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
1525 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Sink %s on %s", sink_data.name, z ? z : sink_master->name);
1526 }
1527
1528 u->sink = pa_sink_new(m->core, &sink_data, (sink_master->flags & (PA_SINK_LATENCY | PA_SINK_DYNAMIC_LATENCY))
1529 | (use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0));
1530 pa_sink_new_data_done(&sink_data);
1531
1532 if (!u->sink) {
1533 pa_log("Failed to create sink.");
1534 goto fail;
1535 }
1536
1537 u->sink->parent.process_msg = sink_process_msg_cb;
1538 u->sink->set_state = sink_set_state_cb;
1539 u->sink->update_requested_latency = sink_update_requested_latency_cb;
1540 u->sink->request_rewind = sink_request_rewind_cb;
1541 pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
1542 if (!use_volume_sharing) {
1543 pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
1544 pa_sink_enable_decibel_volume(u->sink, TRUE);
1545 }
1546 u->sink->userdata = u;
1547
1548 pa_sink_set_asyncmsgq(u->sink, sink_master->asyncmsgq);
1549
1550 /* Create source output */
1551 pa_source_output_new_data_init(&source_output_data);
1552 source_output_data.driver = __FILE__;
1553 source_output_data.module = m;
1554 pa_source_output_new_data_set_source(&source_output_data, source_master, FALSE);
1555 source_output_data.destination_source = u->source;
1556 /* FIXME
1557 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1558
1559 pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_NAME, "Echo-Cancel Source Stream");
1560 pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
1561 pa_source_output_new_data_set_sample_spec(&source_output_data, &source_ss);
1562 pa_source_output_new_data_set_channel_map(&source_output_data, &source_map);
1563
1564 pa_source_output_new(&u->source_output, m->core, &source_output_data);
1565 pa_source_output_new_data_done(&source_output_data);
1566
1567 if (!u->source_output)
1568 goto fail;
1569
1570 u->source_output->parent.process_msg = source_output_process_msg_cb;
1571 u->source_output->push = source_output_push_cb;
1572 u->source_output->process_rewind = source_output_process_rewind_cb;
1573 u->source_output->update_max_rewind = source_output_update_max_rewind_cb;
1574 u->source_output->update_source_requested_latency = source_output_update_source_requested_latency_cb;
1575 u->source_output->update_source_latency_range = source_output_update_source_latency_range_cb;
1576 u->source_output->update_source_fixed_latency = source_output_update_source_fixed_latency_cb;
1577 u->source_output->kill = source_output_kill_cb;
1578 u->source_output->attach = source_output_attach_cb;
1579 u->source_output->detach = source_output_detach_cb;
1580 u->source_output->state_change = source_output_state_change_cb;
1581 u->source_output->may_move_to = source_output_may_move_to_cb;
1582 u->source_output->moving = source_output_moving_cb;
1583 u->source_output->userdata = u;
1584
1585 u->source->output_from_master = u->source_output;
1586
1587 /* Create sink input */
1588 pa_sink_input_new_data_init(&sink_input_data);
1589 sink_input_data.driver = __FILE__;
1590 sink_input_data.module = m;
1591 pa_sink_input_new_data_set_sink(&sink_input_data, sink_master, FALSE);
1592 sink_input_data.origin_sink = u->sink;
1593 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Echo-Cancel Sink Stream");
1594 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
1595 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &sink_ss);
1596 pa_sink_input_new_data_set_channel_map(&sink_input_data, &sink_map);
1597 sink_input_data.flags = PA_SINK_INPUT_VARIABLE_RATE;
1598
1599 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
1600 pa_sink_input_new_data_done(&sink_input_data);
1601
1602 if (!u->sink_input)
1603 goto fail;
1604
1605 u->sink_input->parent.process_msg = sink_input_process_msg_cb;
1606 u->sink_input->pop = sink_input_pop_cb;
1607 u->sink_input->process_rewind = sink_input_process_rewind_cb;
1608 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
1609 u->sink_input->update_max_request = sink_input_update_max_request_cb;
1610 u->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
1611 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
1612 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
1613 u->sink_input->kill = sink_input_kill_cb;
1614 u->sink_input->attach = sink_input_attach_cb;
1615 u->sink_input->detach = sink_input_detach_cb;
1616 u->sink_input->state_change = sink_input_state_change_cb;
1617 u->sink_input->may_move_to = sink_input_may_move_to_cb;
1618 u->sink_input->moving = sink_input_moving_cb;
1619 if (!use_volume_sharing)
1620 u->sink_input->volume_changed = sink_input_volume_changed_cb;
1621 u->sink_input->mute_changed = sink_input_mute_changed_cb;
1622 u->sink_input->userdata = u;
1623
1624 u->sink->input_to_master = u->sink_input;
1625
1626 pa_sink_input_get_silence(u->sink_input, &silence);
1627
1628 u->source_memblockq = pa_memblockq_new("module-echo-cancel source_memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0,
1629 &source_ss, 1, 1, 0, &silence);
1630 u->sink_memblockq = pa_memblockq_new("module-echo-cancel sink_memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0,
1631 &sink_ss, 1, 1, 0, &silence);
1632
1633 pa_memblock_unref(silence.memblock);
1634
1635 if (!u->source_memblockq || !u->sink_memblockq) {
1636 pa_log("Failed to create memblockq.");
1637 goto fail;
1638 }
1639
1640 /* our source and sink are not suspended when we create them */
1641 u->active_mask = 3;
1642
1643 if (u->adjust_time > 0)
1644 u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
1645
1646 if (u->save_aec) {
1647 pa_log("Creating AEC files in /tmp");
1648 u->captured_file = fopen("/tmp/aec_rec.sw", "wb");
1649 if (u->captured_file == NULL)
1650 perror ("fopen failed");
1651 u->played_file = fopen("/tmp/aec_play.sw", "wb");
1652 if (u->played_file == NULL)
1653 perror ("fopen failed");
1654 u->canceled_file = fopen("/tmp/aec_out.sw", "wb");
1655 if (u->canceled_file == NULL)
1656 perror ("fopen failed");
1657 }
1658
1659 pa_sink_put(u->sink);
1660 pa_source_put(u->source);
1661
1662 pa_sink_input_put(u->sink_input);
1663 pa_source_output_put(u->source_output);
1664
1665 pa_modargs_free(ma);
1666
1667 return 0;
1668
1669 fail:
1670 if (ma)
1671 pa_modargs_free(ma);
1672
1673 pa__done(m);
1674
1675 return -1;
1676 }
1677
1678 int pa__get_n_used(pa_module *m) {
1679 struct userdata *u;
1680
1681 pa_assert(m);
1682 pa_assert_se(u = m->userdata);
1683
1684 return pa_sink_linked_by(u->sink) + pa_source_linked_by(u->source);
1685 }
1686
1687 void pa__done(pa_module*m) {
1688 struct userdata *u;
1689
1690 pa_assert(m);
1691
1692 if (!(u = m->userdata))
1693 return;
1694
1695 u->dead = TRUE;
1696
1697 /* See comments in source_output_kill_cb() above regarding
1698 * destruction order! */
1699
1700 if (u->time_event)
1701 u->core->mainloop->time_free(u->time_event);
1702
1703 if (u->source_output)
1704 pa_source_output_unlink(u->source_output);
1705 if (u->sink_input)
1706 pa_sink_input_unlink(u->sink_input);
1707
1708 if (u->source)
1709 pa_source_unlink(u->source);
1710 if (u->sink)
1711 pa_sink_unlink(u->sink);
1712
1713 if (u->source_output)
1714 pa_source_output_unref(u->source_output);
1715 if (u->sink_input)
1716 pa_sink_input_unref(u->sink_input);
1717
1718 if (u->source)
1719 pa_source_unref(u->source);
1720 if (u->sink)
1721 pa_sink_unref(u->sink);
1722
1723 if (u->source_memblockq)
1724 pa_memblockq_free(u->source_memblockq);
1725 if (u->sink_memblockq)
1726 pa_memblockq_free(u->sink_memblockq);
1727
1728 if (u->ec) {
1729 if (u->ec->done)
1730 u->ec->done(u->ec);
1731
1732 pa_xfree(u->ec);
1733 }
1734
1735 if (u->asyncmsgq)
1736 pa_asyncmsgq_unref(u->asyncmsgq);
1737
1738 pa_xfree(u);
1739 }
1740
1741 #ifdef ECHO_CANCEL_TEST
1742 /*
1743 * Stand-alone test program for running in the canceller on pre-recorded files.
1744 */
1745 int main(int argc, char* argv[]) {
1746 struct userdata u;
1747 pa_sample_spec source_ss, sink_ss;
1748 pa_channel_map source_map, sink_map;
1749 pa_modargs *ma = NULL;
1750 uint8_t *rdata = NULL, *pdata = NULL, *cdata = NULL;
1751 int ret = 0, unused;
1752
1753 pa_memzero(&u, sizeof(u));
1754
1755 if (argc < 4 || argc > 6) {
1756 goto usage;
1757 }
1758
1759 u.ec = pa_xnew0(pa_echo_canceller, 1);
1760 if (!u.ec) {
1761 pa_log("Failed to alloc echo canceller");
1762 goto fail;
1763 }
1764
1765 u.captured_file = fopen(argv[2], "r");
1766 if (u.captured_file == NULL) {
1767 perror ("fopen failed");
1768 goto fail;
1769 }
1770 u.played_file = fopen(argv[1], "r");
1771 if (u.played_file == NULL) {
1772 perror ("fopen failed");
1773 goto fail;
1774 }
1775 u.canceled_file = fopen(argv[3], "wb");
1776 if (u.canceled_file == NULL) {
1777 perror ("fopen failed");
1778 goto fail;
1779 }
1780
1781 u.core = pa_xnew0(pa_core, 1);
1782 u.core->cpu_info.cpu_type = PA_CPU_X86;
1783 u.core->cpu_info.flags.x86 |= PA_CPU_X86_SSE;
1784
1785 if (!(ma = pa_modargs_new(argc > 4 ? argv[4] : NULL, valid_modargs))) {
1786 pa_log("Failed to parse module arguments.");
1787 goto fail;
1788 }
1789
1790 source_ss.format = PA_SAMPLE_S16LE;
1791 source_ss.rate = DEFAULT_RATE;
1792 source_ss.channels = DEFAULT_CHANNELS;
1793 pa_channel_map_init_auto(&source_map, source_ss.channels, PA_CHANNEL_MAP_DEFAULT);
1794
1795 init_common(ma, &u, &source_ss, &source_map);
1796
1797 if (!u.ec->init(u.core, u.ec, &source_ss, &source_map, &sink_ss, &sink_map, &u.blocksize,
1798 (argc > 4) ? argv[5] : NULL )) {
1799 pa_log("Failed to init AEC engine");
1800 goto fail;
1801 }
1802
1803 rdata = pa_xmalloc(u.blocksize);
1804 pdata = pa_xmalloc(u.blocksize);
1805 cdata = pa_xmalloc(u.blocksize);
1806
1807 while (fread(rdata, u.blocksize, 1, u.captured_file) > 0) {
1808 if (fread(pdata, u.blocksize, 1, u.played_file) == 0) {
1809 perror("played file ended before captured file");
1810 break;
1811 }
1812
1813 u.ec->run(u.ec, rdata, pdata, cdata);
1814
1815 unused = fwrite(cdata, u.blocksize, 1, u.canceled_file);
1816 }
1817
1818 u.ec->done(u.ec);
1819
1820 fclose(u.captured_file);
1821 fclose(u.played_file);
1822 fclose(u.canceled_file);
1823
1824 out:
1825 pa_xfree(rdata);
1826 pa_xfree(pdata);
1827 pa_xfree(cdata);
1828
1829 pa_xfree(u.ec);
1830 pa_xfree(u.core);
1831
1832 if (ma)
1833 pa_modargs_free(ma);
1834
1835 return ret;
1836
1837 usage:
1838 pa_log("Usage: %s play_file rec_file out_file [module args] [aec_args]",argv[0]);
1839
1840 fail:
1841 ret = -1;
1842 goto out;
1843 }
1844 #endif /* ECHO_CANCEL_TEST */