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