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