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