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