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