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