]> code.delx.au - pulseaudio/blob - src/modules/echo-cancel/module-echo-cancel.c
echo-cancel: Split out speex code from the core module
[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 "frame_size_ms=<amount of data to process at one time> "
74 "filter_size_ms=<amount of echo to cancel> "
75 "adjust_time=<how often to readjust rates in s> "
76 "format=<sample format> "
77 "rate=<sample rate> "
78 "channels=<number of channels> "
79 "channel_map=<channel map> "
80 "save_aec=<save AEC data in /tmp> "
81 ));
82
83 /* NOTE: Make sure the enum and ec_table are maintained in the correct order */
84 enum {
85 PA_ECHO_CANCELLER_SPEEX,
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
100 /* should be between 10-20 ms */
101 #define DEFAULT_FRAME_SIZE_MS 20
102 /* should be between 100-500 ms */
103 #define DEFAULT_FILTER_SIZE_MS 200
104
105 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
106 #define DEFAULT_SAVE_AEC 0
107
108 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
109
110 /* This module creates a new (virtual) source and sink.
111 *
112 * The data sent to the new sink is kept in a memblockq before being
113 * forwarded to the real sink_master.
114 *
115 * Data read from source_master is matched against the saved sink data and
116 * echo canceled data is then pushed onto the new source.
117 *
118 * Both source and sink masters have their own threads to push/pull data
119 * respectively. We however perform all our actions in the source IO thread.
120 * To do this we send all played samples to the source IO thread where they
121 * are then pushed into the memblockq.
122 *
123 * Alignment is performed in two steps:
124 *
125 * 1) when something happens that requires quick adjustement of the alignment of
126 * capture and playback samples, we perform a resync. This adjusts the
127 * position in the playback memblock to the requested sample. Quick
128 * adjustements include moving the playback samples before the capture
129 * samples (because else the echo canceler does not work) or when the
130 * playback pointer drifts too far away.
131 *
132 * 2) periodically check the difference between capture and playback. we use a
133 * low and high watermark for adjusting the alignment. playback should always
134 * be before capture and the difference should not be bigger than one frame
135 * size. We would ideally like to resample the sink_input but most driver
136 * don't give enough accuracy to be able to do that right now.
137 */
138
139 struct snapshot {
140 pa_usec_t sink_now;
141 pa_usec_t sink_latency;
142 size_t sink_delay;
143 int64_t send_counter;
144
145 pa_usec_t source_now;
146 pa_usec_t source_latency;
147 size_t source_delay;
148 int64_t recv_counter;
149 size_t rlen;
150 size_t plen;
151 };
152
153 struct userdata {
154 pa_core *core;
155 pa_module *module;
156
157 uint32_t frame_size_ms;
158 uint32_t save_aec;
159
160 pa_echo_canceller *ec;
161
162 pa_bool_t need_realign;
163
164 /* to wakeup the source I/O thread */
165 pa_bool_t in_push;
166 pa_asyncmsgq *asyncmsgq;
167 pa_rtpoll_item *rtpoll_item_read, *rtpoll_item_write;
168
169 pa_source *source;
170 pa_bool_t source_auto_desc;
171 pa_source_output *source_output;
172 pa_memblockq *source_memblockq; /* echo canceler needs fixed sized chunks */
173 pa_atomic_t source_active;
174
175 pa_sink *sink;
176 pa_bool_t sink_auto_desc;
177 pa_sink_input *sink_input;
178 pa_memblockq *sink_memblockq;
179 int64_t send_counter; /* updated in sink IO thread */
180 int64_t recv_counter;
181 pa_atomic_t sink_active;
182
183 pa_atomic_t request_resync;
184
185 pa_time_event *time_event;
186 pa_usec_t adjust_time;
187
188 FILE *captured_file;
189 FILE *played_file;
190 FILE *canceled_file;
191 };
192
193 static void source_output_snapshot_within_thread(struct userdata *u, struct snapshot *snapshot);
194
195 static const char* const valid_modargs[] = {
196 "source_name",
197 "source_properties",
198 "source_master",
199 "sink_name",
200 "sink_properties",
201 "sink_master",
202 "frame_size_ms",
203 "filter_size_ms",
204 "adjust_time",
205 "format",
206 "rate",
207 "channels",
208 "channel_map",
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 ss;
1281 pa_channel_map 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 frame_size_ms, filter_size_ms;
1291 uint32_t adjust_time_sec;
1292
1293 pa_assert(m);
1294
1295 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1296 pa_log("Failed to parse module arguments.");
1297 goto fail;
1298 }
1299
1300 if (!(source_master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source_master", NULL), PA_NAMEREG_SOURCE))) {
1301 pa_log("Master source not found");
1302 goto fail;
1303 }
1304 pa_assert(source_master);
1305
1306 if (!(sink_master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink_master", NULL), PA_NAMEREG_SINK))) {
1307 pa_log("Master sink not found");
1308 goto fail;
1309 }
1310 pa_assert(sink_master);
1311
1312 frame_size_ms = DEFAULT_FRAME_SIZE_MS;
1313 if (pa_modargs_get_value_u32(ma, "frame_size_ms", &frame_size_ms) < 0 || frame_size_ms < 1 || frame_size_ms > 200) {
1314 pa_log("Invalid frame_size_ms specification");
1315 goto fail;
1316 }
1317
1318 filter_size_ms = DEFAULT_FILTER_SIZE_MS;
1319 if (pa_modargs_get_value_u32(ma, "filter_size_ms", &filter_size_ms) < 0 || filter_size_ms < 1 || filter_size_ms > 2000) {
1320 pa_log("Invalid filter_size_ms specification");
1321 goto fail;
1322 }
1323
1324 ss = source_master->sample_spec;
1325 ss.format = PA_SAMPLE_S16LE;
1326 map = source_master->channel_map;
1327 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
1328 pa_log("Invalid sample format specification or channel map");
1329 goto fail;
1330 }
1331
1332 u = pa_xnew0(struct userdata, 1);
1333 if (!u) {
1334 pa_log("Failed to alloc userdata");
1335 goto fail;
1336 }
1337 u->core = m->core;
1338 u->module = m;
1339 m->userdata = u;
1340 u->frame_size_ms = frame_size_ms;
1341
1342 u->ec = pa_xnew0(pa_echo_canceller, 1);
1343 if (!u->ec) {
1344 pa_log("Failed to alloc echo canceller");
1345 goto fail;
1346 }
1347 u->ec->init = ec_table[DEFAULT_ECHO_CANCELLER].init;
1348 u->ec->run = ec_table[DEFAULT_ECHO_CANCELLER].run;
1349 u->ec->done = ec_table[DEFAULT_ECHO_CANCELLER].done;
1350 u->ec->get_block_size = ec_table[DEFAULT_ECHO_CANCELLER].get_block_size;
1351
1352 adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC;
1353 if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) {
1354 pa_log("Failed to parse adjust_time value");
1355 goto fail;
1356 }
1357
1358 if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC)
1359 u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC;
1360 else
1361 u->adjust_time = DEFAULT_ADJUST_TIME_USEC;
1362
1363 u->save_aec = DEFAULT_SAVE_AEC;
1364 if (pa_modargs_get_value_u32(ma, "save_aec", &u->save_aec) < 0) {
1365 pa_log("Failed to parse save_aec value");
1366 goto fail;
1367 }
1368
1369 u->asyncmsgq = pa_asyncmsgq_new(0);
1370 u->need_realign = TRUE;
1371 if (u->ec->init) {
1372 if (!u->ec->init(u->ec, ss, map, filter_size_ms, frame_size_ms)) {
1373 pa_log("Failed to init AEC engine");
1374 goto fail;
1375 }
1376 }
1377
1378 /* Create source */
1379 pa_source_new_data_init(&source_data);
1380 source_data.driver = __FILE__;
1381 source_data.module = m;
1382 if (!(source_data.name = pa_xstrdup(pa_modargs_get_value(ma, "source_name", NULL))))
1383 source_data.name = pa_sprintf_malloc("%s.echo-cancel", source_master->name);
1384 pa_source_new_data_set_sample_spec(&source_data, &ss);
1385 pa_source_new_data_set_channel_map(&source_data, &map);
1386 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, source_master->name);
1387 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1388 pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
1389 pa_proplist_sets(source_data.proplist, "device.echo-cancel.name", source_data.name);
1390
1391 if (pa_modargs_get_proplist(ma, "source_properties", source_data.proplist, PA_UPDATE_REPLACE) < 0) {
1392 pa_log("Invalid properties");
1393 pa_source_new_data_done(&source_data);
1394 goto fail;
1395 }
1396
1397 if ((u->source_auto_desc = !pa_proplist_contains(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
1398 const char *z;
1399
1400 z = pa_proplist_gets(source_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
1401 pa_proplist_setf(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Source %s on %s", source_data.name, z ? z : source_master->name);
1402 }
1403
1404 u->source = pa_source_new(m->core, &source_data,
1405 PA_SOURCE_HW_MUTE_CTRL|PA_SOURCE_HW_VOLUME_CTRL|PA_SOURCE_DECIBEL_VOLUME|
1406 (source_master->flags & (PA_SOURCE_LATENCY|PA_SOURCE_DYNAMIC_LATENCY)));
1407 pa_source_new_data_done(&source_data);
1408
1409 if (!u->source) {
1410 pa_log("Failed to create source.");
1411 goto fail;
1412 }
1413
1414 u->source->parent.process_msg = source_process_msg_cb;
1415 u->source->set_state = source_set_state_cb;
1416 u->source->update_requested_latency = source_update_requested_latency_cb;
1417 u->source->set_volume = source_set_volume_cb;
1418 u->source->set_mute = source_set_mute_cb;
1419 u->source->get_volume = source_get_volume_cb;
1420 u->source->get_mute = source_get_mute_cb;
1421 u->source->userdata = u;
1422
1423 pa_source_set_asyncmsgq(u->source, source_master->asyncmsgq);
1424
1425 /* Create sink */
1426 pa_sink_new_data_init(&sink_data);
1427 sink_data.driver = __FILE__;
1428 sink_data.module = m;
1429 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
1430 sink_data.name = pa_sprintf_malloc("%s.echo-cancel", sink_master->name);
1431 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
1432 pa_sink_new_data_set_channel_map(&sink_data, &map);
1433 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, sink_master->name);
1434 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
1435 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone");
1436 pa_proplist_sets(sink_data.proplist, "device.echo-cancel.name", sink_data.name);
1437
1438 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
1439 pa_log("Invalid properties");
1440 pa_sink_new_data_done(&sink_data);
1441 goto fail;
1442 }
1443
1444 if ((u->sink_auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
1445 const char *z;
1446
1447 z = pa_proplist_gets(sink_master->proplist, PA_PROP_DEVICE_DESCRIPTION);
1448 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Echo-Cancel Sink %s on %s", sink_data.name, z ? z : sink_master->name);
1449 }
1450
1451 u->sink = pa_sink_new(m->core, &sink_data,
1452 PA_SINK_HW_MUTE_CTRL|PA_SINK_HW_VOLUME_CTRL|PA_SINK_DECIBEL_VOLUME|
1453 (sink_master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)));
1454 pa_sink_new_data_done(&sink_data);
1455
1456 if (!u->sink) {
1457 pa_log("Failed to create sink.");
1458 goto fail;
1459 }
1460
1461 u->sink->parent.process_msg = sink_process_msg_cb;
1462 u->sink->set_state = sink_set_state_cb;
1463 u->sink->update_requested_latency = sink_update_requested_latency_cb;
1464 u->sink->request_rewind = sink_request_rewind_cb;
1465 u->sink->set_volume = sink_set_volume_cb;
1466 u->sink->set_mute = sink_set_mute_cb;
1467 u->sink->userdata = u;
1468
1469 pa_sink_set_asyncmsgq(u->sink, sink_master->asyncmsgq);
1470
1471 /* Create source output */
1472 pa_source_output_new_data_init(&source_output_data);
1473 source_output_data.driver = __FILE__;
1474 source_output_data.module = m;
1475 source_output_data.source = source_master;
1476 /* FIXME
1477 source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */
1478
1479 pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_NAME, "Echo-Cancel Source Stream");
1480 pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
1481 pa_source_output_new_data_set_sample_spec(&source_output_data, &ss);
1482 pa_source_output_new_data_set_channel_map(&source_output_data, &map);
1483
1484 pa_source_output_new(&u->source_output, m->core, &source_output_data);
1485 pa_source_output_new_data_done(&source_output_data);
1486
1487 if (!u->source_output)
1488 goto fail;
1489
1490 u->source_output->parent.process_msg = source_output_process_msg_cb;
1491 u->source_output->push = source_output_push_cb;
1492 u->source_output->process_rewind = source_output_process_rewind_cb;
1493 u->source_output->update_max_rewind = source_output_update_max_rewind_cb;
1494 u->source_output->update_source_requested_latency = source_output_update_source_requested_latency_cb;
1495 u->source_output->update_source_latency_range = source_output_update_source_latency_range_cb;
1496 u->source_output->update_source_fixed_latency = source_output_update_source_fixed_latency_cb;
1497 u->source_output->kill = source_output_kill_cb;
1498 u->source_output->attach = source_output_attach_cb;
1499 u->source_output->detach = source_output_detach_cb;
1500 u->source_output->state_change = source_output_state_change_cb;
1501 u->source_output->may_move_to = source_output_may_move_to_cb;
1502 u->source_output->moving = source_output_moving_cb;
1503 u->source_output->userdata = u;
1504
1505 /* Create sink input */
1506 pa_sink_input_new_data_init(&sink_input_data);
1507 sink_input_data.driver = __FILE__;
1508 sink_input_data.module = m;
1509 sink_input_data.sink = sink_master;
1510 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Echo-Cancel Sink Stream");
1511 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
1512 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
1513 pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
1514 sink_input_data.flags = PA_SINK_INPUT_VARIABLE_RATE;
1515
1516 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
1517 pa_sink_input_new_data_done(&sink_input_data);
1518
1519 if (!u->sink_input)
1520 goto fail;
1521
1522 u->sink_input->parent.process_msg = sink_input_process_msg_cb;
1523 u->sink_input->pop = sink_input_pop_cb;
1524 u->sink_input->process_rewind = sink_input_process_rewind_cb;
1525 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
1526 u->sink_input->update_max_request = sink_input_update_max_request_cb;
1527 u->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
1528 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
1529 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
1530 u->sink_input->kill = sink_input_kill_cb;
1531 u->sink_input->attach = sink_input_attach_cb;
1532 u->sink_input->detach = sink_input_detach_cb;
1533 u->sink_input->state_change = sink_input_state_change_cb;
1534 u->sink_input->may_move_to = sink_input_may_move_to_cb;
1535 u->sink_input->moving = sink_input_moving_cb;
1536 u->sink_input->volume_changed = sink_input_volume_changed_cb;
1537 u->sink_input->mute_changed = sink_input_mute_changed_cb;
1538 u->sink_input->userdata = u;
1539
1540 pa_sink_input_get_silence(u->sink_input, &silence);
1541
1542 u->source_memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0,
1543 pa_frame_size(&ss), 1, 1, 0, &silence);
1544 u->sink_memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0,
1545 pa_frame_size(&ss), 1, 1, 0, &silence);
1546
1547 pa_memblock_unref(silence.memblock);
1548
1549 if (!u->source_memblockq || !u->sink_memblockq) {
1550 pa_log("Failed to create memblockq.");
1551 goto fail;
1552 }
1553
1554 if (u->adjust_time > 0)
1555 u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u);
1556
1557 if (u->save_aec) {
1558 pa_log("Creating AEC files in /tmp");
1559 u->captured_file = fopen("/tmp/aec_rec.sw", "wb");
1560 if (u->captured_file == NULL)
1561 perror ("fopen failed");
1562 u->played_file = fopen("/tmp/aec_play.sw", "wb");
1563 if (u->played_file == NULL)
1564 perror ("fopen failed");
1565 u->canceled_file = fopen("/tmp/aec_out.sw", "wb");
1566 if (u->canceled_file == NULL)
1567 perror ("fopen failed");
1568 }
1569
1570 pa_sink_put(u->sink);
1571 pa_source_put(u->source);
1572
1573 pa_sink_input_put(u->sink_input);
1574 pa_source_output_put(u->source_output);
1575
1576 pa_modargs_free(ma);
1577
1578 return 0;
1579
1580 fail:
1581 if (ma)
1582 pa_modargs_free(ma);
1583
1584 pa__done(m);
1585
1586 return -1;
1587 }
1588
1589 int pa__get_n_used(pa_module *m) {
1590 struct userdata *u;
1591
1592 pa_assert(m);
1593 pa_assert_se(u = m->userdata);
1594
1595 return pa_sink_linked_by(u->sink) + pa_source_linked_by(u->source);
1596 }
1597
1598 void pa__done(pa_module*m) {
1599 struct userdata *u;
1600
1601 pa_assert(m);
1602
1603 if (!(u = m->userdata))
1604 return;
1605
1606 /* See comments in source_output_kill_cb() above regarding
1607 * destruction order! */
1608
1609 if (u->source_output)
1610 pa_source_output_unlink(u->source_output);
1611 if (u->sink_input)
1612 pa_sink_input_unlink(u->sink_input);
1613
1614 if (u->source)
1615 pa_source_unlink(u->source);
1616 if (u->sink)
1617 pa_sink_unlink(u->sink);
1618
1619 if (u->source_output)
1620 pa_source_output_unref(u->source_output);
1621 if (u->sink_input)
1622 pa_sink_input_unref(u->sink_input);
1623
1624 if (u->source)
1625 pa_source_unref(u->source);
1626 if (u->sink)
1627 pa_sink_unref(u->sink);
1628
1629 if (u->time_event)
1630 u->core->mainloop->time_free(u->time_event);
1631
1632 if (u->source_memblockq)
1633 pa_memblockq_free(u->source_memblockq);
1634 if (u->sink_memblockq)
1635 pa_memblockq_free(u->sink_memblockq);
1636
1637 if (u->ec) {
1638 if (u->ec->done)
1639 u->ec->done(u->ec);
1640
1641 pa_xfree(u->ec);
1642 }
1643
1644 if (u->asyncmsgq)
1645 pa_asyncmsgq_unref(u->asyncmsgq);
1646
1647 pa_xfree(u);
1648 }