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