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