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