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