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