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