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