1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
|
From 17e72f00831a36da387ceafe7f3076ffa8f66aba Mon Sep 17 00:00:00 2001
From: Clemence Him <clemence.him@belledonne-communications.com>
Date: Fri, 22 Sep 2023 14:28:02 +0200
Subject: [PATCH] Base64 functions refactoring
---
aec.c | 781 +++++++++++++++++++++++++++++-----------------------------
1 file changed, 394 insertions(+), 387 deletions(-)
diff --git a/aec.c b/aec.c
index 271f370..995f655 100644
--- a/aec.c
+++ b/aec.c
@@ -24,19 +24,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msticker.h"
#ifdef BUILD_AEC
-#include "echo_cancellation.h"
#include "aec_splitting_filter.h"
+#include "echo_cancellation.h"
#endif
#ifdef BUILD_AECM
#include "echo_control_mobile.h"
#endif
-#include "ortp/b64.h"
#ifdef _WIN32
#include <malloc.h> /* for alloca */
#endif
-//#define EC_DUMP 1
+// #define EC_DUMP 1
#ifdef ANDROID
#define EC_DUMP_PREFIX "/sdcard"
#else
@@ -48,466 +47,485 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
static const float smooth_factor = 0.05f;
static const int framesize = 80;
-
typedef enum _WebRTCAECType {
- WebRTCAECTypeNormal,
- WebRTCAECTypeMobile
+ WebRTCAECTypeNormal,
+ WebRTCAECTypeMobile
} WebRTCAECType;
typedef struct WebRTCAECState {
- void *aecInst;
- MSBufferizer delayed_ref;
- MSFlowControlledBufferizer ref;
- MSBufferizer echo;
- int framesize;
- int samplerate;
- int delay_ms;
- int nominal_ref_samples;
- char *state_str;
+ void *aecInst;
+ MSBufferizer delayed_ref;
+ MSFlowControlledBufferizer ref;
+ MSBufferizer echo;
+ int framesize;
+ int samplerate;
+ int delay_ms;
+ int nominal_ref_samples;
+ char *state_str;
#ifdef EC_DUMP
- FILE *echofile;
- FILE *reffile;
- FILE *cleanfile;
+ FILE *echofile;
+ FILE *reffile;
+ FILE *cleanfile;
#endif
- bool_t echostarted;
- bool_t bypass_mode;
- bool_t using_zeroes;
- WebRTCAECType aec_type;
+ bool_t echostarted;
+ bool_t bypass_mode;
+ bool_t using_zeroes;
+ WebRTCAECType aec_type;
#ifdef BUILD_AEC
- MSWebRtcAecSplittingFilter *splitting_filter;
+ MSWebRtcAecSplittingFilter *splitting_filter;
#endif
} WebRTCAECState;
static void webrtc_aecgeneric_init(MSFilter *f, WebRTCAECType aec_type) {
- WebRTCAECState *s = (WebRTCAECState *) ms_new0(WebRTCAECState, 1);
-
- s->samplerate = 8000;
- ms_bufferizer_init(&s->delayed_ref);
- ms_bufferizer_init(&s->echo);
- ms_flow_controlled_bufferizer_init(&s->ref, f, s->samplerate, 1);
- s->delay_ms = 0;
- s->aecInst = NULL;
- s->framesize = framesize;
- s->state_str = NULL;
- s->using_zeroes = FALSE;
- s->echostarted = FALSE;
- s->bypass_mode = FALSE;
- s->aec_type = aec_type;
+ WebRTCAECState *s = (WebRTCAECState *)ms_new0(WebRTCAECState, 1);
+
+ s->samplerate = 8000;
+ ms_bufferizer_init(&s->delayed_ref);
+ ms_bufferizer_init(&s->echo);
+ ms_flow_controlled_bufferizer_init(&s->ref, f, s->samplerate, 1);
+ s->delay_ms = 0;
+ s->aecInst = NULL;
+ s->framesize = framesize;
+ s->state_str = NULL;
+ s->using_zeroes = FALSE;
+ s->echostarted = FALSE;
+ s->bypass_mode = FALSE;
+ s->aec_type = aec_type;
#ifdef EC_DUMP
- {
- char *fname = ms_strdup_printf("%s/mswebrtcaec-%p-echo.raw", EC_DUMP_PREFIX, f);
- s->echofile = fopen(fname, "w");
- ms_free(fname);
- fname = ms_strdup_printf("%s/mswebrtcaec-%p-ref.raw", EC_DUMP_PREFIX, f);
- s->reffile = fopen(fname, "w");
- ms_free(fname);
- fname = ms_strdup_printf("%s/mswebrtcaec-%p-clean.raw", EC_DUMP_PREFIX, f);
- s->cleanfile = fopen(fname, "w");
- ms_free(fname);
- }
+ {
+ char *fname =
+ ms_strdup_printf("%s/mswebrtcaec-%p-echo.raw", EC_DUMP_PREFIX, f);
+ s->echofile = fopen(fname, "w");
+ ms_free(fname);
+ fname = ms_strdup_printf("%s/mswebrtcaec-%p-ref.raw", EC_DUMP_PREFIX, f);
+ s->reffile = fopen(fname, "w");
+ ms_free(fname);
+ fname = ms_strdup_printf("%s/mswebrtcaec-%p-clean.raw", EC_DUMP_PREFIX, f);
+ s->cleanfile = fopen(fname, "w");
+ ms_free(fname);
+ }
#endif
- f->data = s;
+ f->data = s;
}
#ifdef BUILD_AEC
static void webrtc_aec_init(MSFilter *f) {
- webrtc_aecgeneric_init(f, WebRTCAECTypeNormal);
+ webrtc_aecgeneric_init(f, WebRTCAECTypeNormal);
}
#endif
#ifdef BUILD_AECM
static void webrtc_aecm_init(MSFilter *f) {
- webrtc_aecgeneric_init(f, WebRTCAECTypeMobile);
+ webrtc_aecgeneric_init(f, WebRTCAECTypeMobile);
}
#endif
static void webrtc_aec_uninit(MSFilter *f) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- if (s->state_str) ms_free(s->state_str);
- ms_bufferizer_uninit(&s->delayed_ref);
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ if (s->state_str)
+ ms_free(s->state_str);
+ ms_bufferizer_uninit(&s->delayed_ref);
#ifdef EC_DUMP
- if (s->echofile)
- fclose(s->echofile);
- if (s->reffile)
- fclose(s->reffile);
+ if (s->echofile)
+ fclose(s->echofile);
+ if (s->reffile)
+ fclose(s->reffile);
#endif
- ms_free(s);
+ ms_free(s);
}
static void configure_flow_controlled_bufferizer(WebRTCAECState *s) {
- ms_flow_controlled_bufferizer_set_samplerate(&s->ref, s->samplerate);
- ms_flow_controlled_bufferizer_set_max_size_ms(&s->ref, s->delay_ms);
- ms_flow_controlled_bufferizer_set_granularity_ms(&s->ref, (s->framesize * 1000) / s->samplerate);
+ ms_flow_controlled_bufferizer_set_samplerate(&s->ref, s->samplerate);
+ ms_flow_controlled_bufferizer_set_max_size_ms(&s->ref, s->delay_ms);
+ ms_flow_controlled_bufferizer_set_granularity_ms(
+ &s->ref, (s->framesize * 1000) / s->samplerate);
}
static void webrtc_aec_preprocess(MSFilter *f) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
#ifdef BUILD_AEC
- AecConfig aec_config;
+ AecConfig aec_config;
#endif
#ifdef BUILD_AECM
- AecmConfig aecm_config;
- int error_code;
+ AecmConfig aecm_config;
+ int error_code;
#endif
- int delay_samples = 0;
- mblk_t *m;
+ int delay_samples = 0;
+ mblk_t *m;
- s->echostarted = FALSE;
- delay_samples = s->delay_ms * s->samplerate / 1000;
- s->framesize=(framesize*s->samplerate)/8000;
- ms_message("Initializing WebRTC echo canceler with framesize=%i, delay_ms=%i, delay_samples=%i", s->framesize, s->delay_ms, delay_samples);
- configure_flow_controlled_bufferizer(s);
+ s->echostarted = FALSE;
+ delay_samples = s->delay_ms * s->samplerate / 1000;
+ s->framesize = (framesize * s->samplerate) / 8000;
+ ms_message("Initializing WebRTC echo canceler with framesize=%i, "
+ "delay_ms=%i, delay_samples=%i",
+ s->framesize, s->delay_ms, delay_samples);
+ configure_flow_controlled_bufferizer(s);
#ifdef BUILD_AEC
- if (s->aec_type == WebRTCAECTypeNormal) {
- if ((s->aecInst = WebRtcAec_Create()) == NULL) {
- s->bypass_mode = TRUE;
- ms_error("WebRtcAec_Create(): error, entering bypass mode");
- return;
- }
- if ((WebRtcAec_Init(s->aecInst, MIN(48000, s->samplerate), s->samplerate)) < 0) {
- ms_error("WebRtcAec_Init(): WebRTC echo canceller does not support %d samplerate", s->samplerate);
- s->bypass_mode = TRUE;
- ms_error("Entering bypass mode");
- return;
- }
- aec_config.nlpMode = kAecNlpAggressive;
- aec_config.skewMode = kAecFalse;
- aec_config.metricsMode = kAecFalse;
- aec_config.delay_logging = kAecFalse;
- if (WebRtcAec_set_config(s->aecInst, aec_config) != 0) {
- ms_error("WebRtcAec_set_config(): failed.");
- }
- }
+ if (s->aec_type == WebRTCAECTypeNormal) {
+ if ((s->aecInst = WebRtcAec_Create()) == NULL) {
+ s->bypass_mode = TRUE;
+ ms_error("WebRtcAec_Create(): error, entering bypass mode");
+ return;
+ }
+ if ((WebRtcAec_Init(s->aecInst, MIN(48000, s->samplerate), s->samplerate)) <
+ 0) {
+ ms_error("WebRtcAec_Init(): WebRTC echo canceller does not support %d "
+ "samplerate",
+ s->samplerate);
+ s->bypass_mode = TRUE;
+ ms_error("Entering bypass mode");
+ return;
+ }
+ aec_config.nlpMode = kAecNlpAggressive;
+ aec_config.skewMode = kAecFalse;
+ aec_config.metricsMode = kAecFalse;
+ aec_config.delay_logging = kAecFalse;
+ if (WebRtcAec_set_config(s->aecInst, aec_config) != 0) {
+ ms_error("WebRtcAec_set_config(): failed.");
+ }
+ }
#endif
#ifdef BUILD_AECM
- if (s->aec_type == WebRTCAECTypeMobile) {
- if ((s->aecInst = WebRtcAecm_Create()) == NULL) {
- s->bypass_mode = TRUE;
- ms_error("WebRtcAecm_Create(): error, entering bypass mode");
- return;
- }
- if ((error_code = WebRtcAecm_Init(s->aecInst, s->samplerate)) < 0) {
- if (error_code == AECM_BAD_PARAMETER_ERROR) {
- ms_error("WebRtcAecm_Init(): WebRTC echo canceller does not support %d samplerate", s->samplerate);
- }
- s->bypass_mode = TRUE;
- ms_error("Entering bypass mode");
- return;
- }
- aecm_config.cngMode = TRUE;
- aecm_config.echoMode = 3;
- if (WebRtcAecm_set_config(s->aecInst, aecm_config)!=0){
- ms_error("WebRtcAecm_set_config(): failed.");
- }
- }
+ if (s->aec_type == WebRTCAECTypeMobile) {
+ if ((s->aecInst = WebRtcAecm_Create()) == NULL) {
+ s->bypass_mode = TRUE;
+ ms_error("WebRtcAecm_Create(): error, entering bypass mode");
+ return;
+ }
+ if ((error_code = WebRtcAecm_Init(s->aecInst, s->samplerate)) < 0) {
+ if (error_code == AECM_BAD_PARAMETER_ERROR) {
+ ms_error("WebRtcAecm_Init(): WebRTC echo canceller does not support %d "
+ "samplerate",
+ s->samplerate);
+ }
+ s->bypass_mode = TRUE;
+ ms_error("Entering bypass mode");
+ return;
+ }
+ aecm_config.cngMode = TRUE;
+ aecm_config.echoMode = 3;
+ if (WebRtcAecm_set_config(s->aecInst, aecm_config) != 0) {
+ ms_error("WebRtcAecm_set_config(): failed.");
+ }
+ }
#endif
- /* fill with zeroes for the time of the delay*/
- m = allocb(delay_samples * 2, 0);
- m->b_wptr += delay_samples * 2;
- ms_bufferizer_put(&s->delayed_ref, m);
- s->nominal_ref_samples = delay_samples;
+ /* fill with zeroes for the time of the delay*/
+ m = allocb(delay_samples * 2, 0);
+ m->b_wptr += delay_samples * 2;
+ ms_bufferizer_put(&s->delayed_ref, m);
+ s->nominal_ref_samples = delay_samples;
}
/* inputs[0]= reference signal from far end (sent to soundcard)
* inputs[1]= near speech & echo signal (read from soundcard)
* outputs[0]= is a copy of inputs[0] to be sent to soundcard
* outputs[1]= near end speech, echo removed - towards far end
-*/
+ */
static void webrtc_aec_process(MSFilter *f) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- int nbytes = s->framesize * sizeof(int16_t);
- mblk_t *refm;
- int16_t *ref, *echo;
- int nbands = 1;
- int bandsize = s->framesize;
-
- if (s->bypass_mode) {
- while ((refm = ms_queue_get(f->inputs[0])) != NULL) {
- ms_queue_put(f->outputs[0], refm);
- }
- while ((refm = ms_queue_get(f->inputs[1])) != NULL) {
- ms_queue_put(f->outputs[1], refm);
- }
- return;
- }
-
- if (f->inputs[0] != NULL) {
- if (s->echostarted) {
- while ((refm = ms_queue_get(f->inputs[0])) != NULL) {
- mblk_t *cp=dupmsg(refm);
- ms_bufferizer_put(&s->delayed_ref,cp);
- ms_flow_controlled_bufferizer_put(&s->ref,refm);
- }
- } else {
- ms_warning("Getting reference signal but no echo to synchronize on.");
- ms_queue_flush(f->inputs[0]);
- }
- }
-
- ms_bufferizer_put_from_queue(&s->echo, f->inputs[1]);
-
- ref = (int16_t *) alloca(nbytes);
- echo = (int16_t *) alloca(nbytes);
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ int nbytes = s->framesize * sizeof(int16_t);
+ mblk_t *refm;
+ int16_t *ref, *echo;
+ int nbands = 1;
+ int bandsize = s->framesize;
+
+ if (s->bypass_mode) {
+ while ((refm = ms_queue_get(f->inputs[0])) != NULL) {
+ ms_queue_put(f->outputs[0], refm);
+ }
+ while ((refm = ms_queue_get(f->inputs[1])) != NULL) {
+ ms_queue_put(f->outputs[1], refm);
+ }
+ return;
+ }
+
+ if (f->inputs[0] != NULL) {
+ if (s->echostarted) {
+ while ((refm = ms_queue_get(f->inputs[0])) != NULL) {
+ mblk_t *cp = dupmsg(refm);
+ ms_bufferizer_put(&s->delayed_ref, cp);
+ ms_flow_controlled_bufferizer_put(&s->ref, refm);
+ }
+ } else {
+ ms_warning("Getting reference signal but no echo to synchronize on.");
+ ms_queue_flush(f->inputs[0]);
+ }
+ }
+
+ ms_bufferizer_put_from_queue(&s->echo, f->inputs[1]);
+
+ ref = (int16_t *)alloca(nbytes);
+ echo = (int16_t *)alloca(nbytes);
#ifdef BUILD_AEC
- if (s->aec_type == WebRTCAECTypeNormal) {
- if (s->samplerate > 16000) {
- nbands = s->samplerate / 16000;
- bandsize = 160;
- }
- if (!s->splitting_filter) {
- s->splitting_filter = mswebrtc_aec_splitting_filter_create(nbands, bandsize);
- }
- }
+ if (s->aec_type == WebRTCAECTypeNormal) {
+ if (s->samplerate > 16000) {
+ nbands = s->samplerate / 16000;
+ bandsize = 160;
+ }
+ if (!s->splitting_filter) {
+ s->splitting_filter =
+ mswebrtc_aec_splitting_filter_create(nbands, bandsize);
+ }
+ }
#endif
- while (ms_bufferizer_read(&s->echo, (uint8_t *)echo, (size_t)nbytes) >= (size_t)nbytes) {
- mblk_t *oecho = allocb(nbytes, 0);
- int avail;
- int avail_samples;
-
- if (!s->echostarted) s->echostarted = TRUE;
- if ((avail = ms_bufferizer_get_avail(&s->delayed_ref)) < ((s->nominal_ref_samples * 2) + nbytes)) {
- /*we don't have enough to read in a reference signal buffer, inject silence instead*/
- refm = allocb(nbytes, 0);
- memset(refm->b_wptr, 0, nbytes);
- refm->b_wptr += nbytes;
- ms_bufferizer_put(&s->delayed_ref, refm);
- /*
- * However, we don't inject this silence buffer to the sound card, in order to break the following bad loop:
- * - the sound playback filter detects it has too many pending samples, then triggers an event to request samples to be dropped upstream.
- * - the upstream MSFlowControl filter is requested to drop samples, which it starts to do.
- * - necessarily shortly after the AEC goes into a situation where it has not enough reference samples while processing an audio buffer from mic.
- * - if the AEC injects a silence buffer as output, then it will RECREATE a situation where the sound playback filter has too many pending samples.
- * That's why we should not do this.
- * By not doing this, we will create a discrepancy between what we really injected to the soundcard, and what we told to the
- * echo canceller about the samples we injected. This shifts the echo. The echo canceller will re-converge quickly to take into
- * account the situation.
- *
- */
- //ms_queue_put(f->outputs[0], dupmsg(refm));
- if (!s->using_zeroes) {
- ms_warning("Not enough ref samples, using zeroes");
- s->using_zeroes = TRUE;
- }
- } else {
- if (s->using_zeroes) {
- ms_message("Samples are back.");
- s->using_zeroes = FALSE;
- }
- /* read from our no-delay buffer and output */
- refm = allocb(nbytes, 0);
- if (ms_flow_controlled_bufferizer_read(&s->ref, refm->b_wptr, nbytes) == 0) {
- ms_fatal("Should never happen");
- }
- refm->b_wptr += nbytes;
- ms_queue_put(f->outputs[0], refm);
- }
-
- /*now read a valid buffer of delayed ref samples*/
- if (ms_bufferizer_read(&s->delayed_ref, (uint8_t *)ref, nbytes) == 0) {
- ms_fatal("Should never happen");
- }
- avail -= nbytes;
- avail_samples = avail / 2;
+ while (ms_bufferizer_read(&s->echo, (uint8_t *)echo, (size_t)nbytes) >=
+ (size_t)nbytes) {
+ mblk_t *oecho = allocb(nbytes, 0);
+ int avail;
+ int avail_samples;
+
+ if (!s->echostarted)
+ s->echostarted = TRUE;
+ if ((avail = ms_bufferizer_get_avail(&s->delayed_ref)) <
+ ((s->nominal_ref_samples * 2) + nbytes)) {
+ /*we don't have enough to read in a reference signal buffer, inject
+ * silence instead*/
+ refm = allocb(nbytes, 0);
+ memset(refm->b_wptr, 0, nbytes);
+ refm->b_wptr += nbytes;
+ ms_bufferizer_put(&s->delayed_ref, refm);
+ /*
+ * However, we don't inject this silence buffer to the sound card, in
+ * order to break the following bad loop:
+ * - the sound playback filter detects it has too many pending samples,
+ * then triggers an event to request samples to be dropped upstream.
+ * - the upstream MSFlowControl filter is requested to drop samples, which
+ * it starts to do.
+ * - necessarily shortly after the AEC goes into a situation where it has
+ * not enough reference samples while processing an audio buffer from mic.
+ * - if the AEC injects a silence buffer as output, then it will RECREATE
+ * a situation where the sound playback filter has too many pending
+ * samples. That's why we should not do this. By not doing this, we will
+ * create a discrepancy between what we really injected to the soundcard,
+ * and what we told to the echo canceller about the samples we injected.
+ * This shifts the echo. The echo canceller will re-converge quickly to
+ * take into account the situation.
+ *
+ */
+ // ms_queue_put(f->outputs[0], dupmsg(refm));
+ if (!s->using_zeroes) {
+ ms_warning("Not enough ref samples, using zeroes");
+ s->using_zeroes = TRUE;
+ }
+ } else {
+ if (s->using_zeroes) {
+ ms_message("Samples are back.");
+ s->using_zeroes = FALSE;
+ }
+ /* read from our no-delay buffer and output */
+ refm = allocb(nbytes, 0);
+ if (ms_flow_controlled_bufferizer_read(&s->ref, refm->b_wptr, nbytes) ==
+ 0) {
+ ms_fatal("Should never happen");
+ }
+ refm->b_wptr += nbytes;
+ ms_queue_put(f->outputs[0], refm);
+ }
+
+ /*now read a valid buffer of delayed ref samples*/
+ if (ms_bufferizer_read(&s->delayed_ref, (uint8_t *)ref, nbytes) == 0) {
+ ms_fatal("Should never happen");
+ }
+ avail -= nbytes;
+ avail_samples = avail / 2;
#ifdef EC_DUMP
- if (s->reffile)
- fwrite(ref, nbytes, 1, s->reffile);
- if (s->echofile)
- fwrite(echo, nbytes, 1, s->echofile);
+ if (s->reffile)
+ fwrite(ref, nbytes, 1, s->reffile);
+ if (s->echofile)
+ fwrite(echo, nbytes, 1, s->echofile);
#endif
#ifdef BUILD_AEC
- if (s->aec_type == WebRTCAECTypeNormal) {
- mswebrtc_aec_splitting_filter_analysis(s->splitting_filter, ref, echo);
- if (WebRtcAec_BufferFarend(s->aecInst,
- mswebrtc_aec_splitting_filter_get_ref(s->splitting_filter),
- (size_t)mswebrtc_aec_splitting_filter_get_bandsize(s->splitting_filter)) != 0)
- ms_error("WebRtcAec_BufferFarend() failed.");
- if (WebRtcAec_Process(s->aecInst,
- mswebrtc_aec_splitting_filter_get_echo_bands(s->splitting_filter),
- mswebrtc_aec_splitting_filter_get_number_of_bands(s->splitting_filter),
- mswebrtc_aec_splitting_filter_get_output_bands(s->splitting_filter),
- (size_t)mswebrtc_aec_splitting_filter_get_bandsize(s->splitting_filter), 0, 0) != 0)
- ms_error("WebRtcAec_Process() failed.");
- mswebrtc_aec_splitting_filter_synthesis(s->splitting_filter, (int16_t *)oecho->b_wptr);
- }
+ if (s->aec_type == WebRTCAECTypeNormal) {
+ mswebrtc_aec_splitting_filter_analysis(s->splitting_filter, ref, echo);
+ if (WebRtcAec_BufferFarend(
+ s->aecInst,
+ mswebrtc_aec_splitting_filter_get_ref(s->splitting_filter),
+ (size_t)mswebrtc_aec_splitting_filter_get_bandsize(
+ s->splitting_filter)) != 0)
+ ms_error("WebRtcAec_BufferFarend() failed.");
+ if (WebRtcAec_Process(
+ s->aecInst,
+ mswebrtc_aec_splitting_filter_get_echo_bands(s->splitting_filter),
+ mswebrtc_aec_splitting_filter_get_number_of_bands(
+ s->splitting_filter),
+ mswebrtc_aec_splitting_filter_get_output_bands(
+ s->splitting_filter),
+ (size_t)mswebrtc_aec_splitting_filter_get_bandsize(
+ s->splitting_filter),
+ 0, 0) != 0)
+ ms_error("WebRtcAec_Process() failed.");
+ mswebrtc_aec_splitting_filter_synthesis(s->splitting_filter,
+ (int16_t *)oecho->b_wptr);
+ }
#endif
#ifdef BUILD_AECM
- if (s->aec_type == WebRTCAECTypeMobile) {
- if (WebRtcAecm_BufferFarend(s->aecInst, ref, (size_t)s->framesize) != 0)
- ms_error("WebRtcAecm_BufferFarend() failed.");
- if (WebRtcAecm_Process(s->aecInst, echo, NULL, (int16_t *)oecho->b_wptr, (size_t)s->framesize, 0) != 0)
- ms_error("WebRtcAecm_Process() failed.");
- }
+ if (s->aec_type == WebRTCAECTypeMobile) {
+ if (WebRtcAecm_BufferFarend(s->aecInst, ref, (size_t)s->framesize) != 0)
+ ms_error("WebRtcAecm_BufferFarend() failed.");
+ if (WebRtcAecm_Process(s->aecInst, echo, NULL, (int16_t *)oecho->b_wptr,
+ (size_t)s->framesize, 0) != 0)
+ ms_error("WebRtcAecm_Process() failed.");
+ }
#endif
#ifdef EC_DUMP
- if (s->cleanfile)
- fwrite(oecho->b_wptr, nbytes, 1, s->cleanfile);
+ if (s->cleanfile)
+ fwrite(oecho->b_wptr, nbytes, 1, s->cleanfile);
#endif
- oecho->b_wptr += nbytes;
- ms_queue_put(f->outputs[1], oecho);
- }
+ oecho->b_wptr += nbytes;
+ ms_queue_put(f->outputs[1], oecho);
+ }
}
static void webrtc_aec_postprocess(MSFilter *f) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
- ms_bufferizer_flush(&s->delayed_ref);
- ms_bufferizer_flush(&s->echo);
- ms_flow_controlled_bufferizer_flush(&s->ref);
+ ms_bufferizer_flush(&s->delayed_ref);
+ ms_bufferizer_flush(&s->echo);
+ ms_flow_controlled_bufferizer_flush(&s->ref);
#ifdef BUILD_AEC
- if (s->splitting_filter) {
- mswebrtc_aec_splitting_filter_destroy(s->splitting_filter);
- s->splitting_filter = NULL;
- }
+ if (s->splitting_filter) {
+ mswebrtc_aec_splitting_filter_destroy(s->splitting_filter);
+ s->splitting_filter = NULL;
+ }
#endif
- if (s->aecInst != NULL) {
+ if (s->aecInst != NULL) {
#ifdef BUILD_AEC
- if (s->aec_type == WebRTCAECTypeNormal) {
- WebRtcAec_Free(s->aecInst);
- }
+ if (s->aec_type == WebRTCAECTypeNormal) {
+ WebRtcAec_Free(s->aecInst);
+ }
#endif
#ifdef BUILD_AECM
- if (s->aec_type == WebRTCAECTypeMobile) {
- WebRtcAecm_Free(s->aecInst);
- }
+ if (s->aec_type == WebRTCAECTypeMobile) {
+ WebRtcAecm_Free(s->aecInst);
+ }
#endif
- s->aecInst = NULL;
- }
+ s->aecInst = NULL;
+ }
}
static int webrtc_aec_set_sr(MSFilter *f, void *arg) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- int requested_sr = *(int *) arg;
- int sr = requested_sr;
-
- if ((s->aec_type == WebRTCAECTypeNormal) && (requested_sr >= 48000)) {
- sr = 48000;
- } else if ((s->aec_type == WebRTCAECTypeNormal) && (requested_sr >= 32000)) {
- sr = 32000;
- } else if (requested_sr >= 16000) {
- sr = 16000;
- } else {
- sr = 8000;
- }
- if (sr != requested_sr)
- ms_message("Webrtc %s does not support sampling rate %i, using %i instead", ((s->aec_type == WebRTCAECTypeNormal)?"aec":"aecm"),requested_sr, sr);
-
- s->samplerate = sr;
- configure_flow_controlled_bufferizer(s);
- return 0;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ int requested_sr = *(int *)arg;
+ int sr = requested_sr;
+
+ if ((s->aec_type == WebRTCAECTypeNormal) && (requested_sr >= 48000)) {
+ sr = 48000;
+ } else if ((s->aec_type == WebRTCAECTypeNormal) && (requested_sr >= 32000)) {
+ sr = 32000;
+ } else if (requested_sr >= 16000) {
+ sr = 16000;
+ } else {
+ sr = 8000;
+ }
+ if (sr != requested_sr)
+ ms_message("Webrtc %s does not support sampling rate %i, using %i instead",
+ ((s->aec_type == WebRTCAECTypeNormal) ? "aec" : "aecm"),
+ requested_sr, sr);
+
+ s->samplerate = sr;
+ configure_flow_controlled_bufferizer(s);
+ return 0;
}
static int webrtc_aec_get_sr(MSFilter *f, void *arg) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- *(int *) arg=s->samplerate;
- return 0;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ *(int *)arg = s->samplerate;
+ return 0;
}
static int webrtc_aec_set_framesize(MSFilter *f, void *arg) {
- /* Do nothing because the WebRTC echo canceller only accept specific values: 80 and 160. We use 80 at 8khz, and 160 at 16khz */
- return 0;
+ /* Do nothing because the WebRTC echo canceller only accept specific values:
+ * 80 and 160. We use 80 at 8khz, and 160 at 16khz */
+ return 0;
}
static int webrtc_aec_set_delay(MSFilter *f, void *arg) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- s->delay_ms = *(int *) arg;
- configure_flow_controlled_bufferizer(s);
- return 0;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ s->delay_ms = *(int *)arg;
+ configure_flow_controlled_bufferizer(s);
+ return 0;
}
static int webrtc_aec_set_tail_length(MSFilter *f, void *arg) {
- /* Do nothing because this is not needed by the WebRTC echo canceller. */
- return 0;
+ /* Do nothing because this is not needed by the WebRTC echo canceller. */
+ return 0;
}
static int webrtc_aec_set_bypass_mode(MSFilter *f, void *arg) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- s->bypass_mode = *(bool_t *) arg;
- ms_message("set EC bypass mode to [%i]", s->bypass_mode);
- return 0;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ s->bypass_mode = *(bool_t *)arg;
+ ms_message("set EC bypass mode to [%i]", s->bypass_mode);
+ return 0;
}
static int webrtc_aec_get_bypass_mode(MSFilter *f, void *arg) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- *(bool_t *) arg = s->bypass_mode;
- return 0;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ *(bool_t *)arg = s->bypass_mode;
+ return 0;
}
static int webrtc_aec_set_state(MSFilter *f, void *arg) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- s->state_str = ms_strdup((const char *) arg);
- return 0;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ s->state_str = ms_strdup((const char *)arg);
+ return 0;
}
static int webrtc_aec_get_state(MSFilter *f, void *arg) {
- WebRTCAECState *s = (WebRTCAECState *) f->data;
- *(char **) arg = s->state_str;
- return 0;
+ WebRTCAECState *s = (WebRTCAECState *)f->data;
+ *(char **)arg = s->state_str;
+ return 0;
}
static MSFilterMethod webrtc_aec_methods[] = {
- { MS_FILTER_SET_SAMPLE_RATE , webrtc_aec_set_sr },
- { MS_FILTER_GET_SAMPLE_RATE , webrtc_aec_get_sr },
- { MS_ECHO_CANCELLER_SET_TAIL_LENGTH , webrtc_aec_set_tail_length },
- { MS_ECHO_CANCELLER_SET_DELAY , webrtc_aec_set_delay },
- { MS_ECHO_CANCELLER_SET_FRAMESIZE , webrtc_aec_set_framesize },
- { MS_ECHO_CANCELLER_SET_BYPASS_MODE , webrtc_aec_set_bypass_mode },
- { MS_ECHO_CANCELLER_GET_BYPASS_MODE , webrtc_aec_get_bypass_mode },
- { MS_ECHO_CANCELLER_GET_STATE_STRING , webrtc_aec_get_state },
- { MS_ECHO_CANCELLER_SET_STATE_STRING , webrtc_aec_set_state },
- { 0, NULL }
-};
-
+ {MS_FILTER_SET_SAMPLE_RATE, webrtc_aec_set_sr},
+ {MS_FILTER_GET_SAMPLE_RATE, webrtc_aec_get_sr},
+ {MS_ECHO_CANCELLER_SET_TAIL_LENGTH, webrtc_aec_set_tail_length},
+ {MS_ECHO_CANCELLER_SET_DELAY, webrtc_aec_set_delay},
+ {MS_ECHO_CANCELLER_SET_FRAMESIZE, webrtc_aec_set_framesize},
+ {MS_ECHO_CANCELLER_SET_BYPASS_MODE, webrtc_aec_set_bypass_mode},
+ {MS_ECHO_CANCELLER_GET_BYPASS_MODE, webrtc_aec_get_bypass_mode},
+ {MS_ECHO_CANCELLER_GET_STATE_STRING, webrtc_aec_get_state},
+ {MS_ECHO_CANCELLER_SET_STATE_STRING, webrtc_aec_set_state},
+ {0, NULL}};
#ifdef BUILD_AEC
-#define MS_WEBRTC_AEC_NAME "MSWebRTCAEC"
+#define MS_WEBRTC_AEC_NAME "MSWebRTCAEC"
#define MS_WEBRTC_AEC_DESCRIPTION "Echo canceller using WebRTC library."
-#define MS_WEBRTC_AEC_CATEGORY MS_FILTER_OTHER
-#define MS_WEBRTC_AEC_ENC_FMT NULL
-#define MS_WEBRTC_AEC_NINPUTS 2
-#define MS_WEBRTC_AEC_NOUTPUTS 2
-#define MS_WEBRTC_AEC_FLAGS 0
+#define MS_WEBRTC_AEC_CATEGORY MS_FILTER_OTHER
+#define MS_WEBRTC_AEC_ENC_FMT NULL
+#define MS_WEBRTC_AEC_NINPUTS 2
+#define MS_WEBRTC_AEC_NOUTPUTS 2
+#define MS_WEBRTC_AEC_FLAGS 0
#ifdef _MSC_VER
MSFilterDesc ms_webrtc_aec_desc = {
- MS_FILTER_PLUGIN_ID,
- MS_WEBRTC_AEC_NAME,
- MS_WEBRTC_AEC_DESCRIPTION,
- MS_WEBRTC_AEC_CATEGORY,
- MS_WEBRTC_AEC_ENC_FMT,
- MS_WEBRTC_AEC_NINPUTS,
- MS_WEBRTC_AEC_NOUTPUTS,
- webrtc_aec_init,
- webrtc_aec_preprocess,
- webrtc_aec_process,
- webrtc_aec_postprocess,
- webrtc_aec_uninit,
- webrtc_aec_methods,
- MS_WEBRTC_AEC_FLAGS
-};
+ MS_FILTER_PLUGIN_ID, MS_WEBRTC_AEC_NAME, MS_WEBRTC_AEC_DESCRIPTION,
+ MS_WEBRTC_AEC_CATEGORY, MS_WEBRTC_AEC_ENC_FMT, MS_WEBRTC_AEC_NINPUTS,
+ MS_WEBRTC_AEC_NOUTPUTS, webrtc_aec_init, webrtc_aec_preprocess,
+ webrtc_aec_process, webrtc_aec_postprocess, webrtc_aec_uninit,
+ webrtc_aec_methods, MS_WEBRTC_AEC_FLAGS};
#else
-MSFilterDesc ms_webrtc_aec_desc = {
- .id = MS_FILTER_PLUGIN_ID,
- .name = MS_WEBRTC_AEC_NAME,
- .text = MS_WEBRTC_AEC_DESCRIPTION,
- .category = MS_WEBRTC_AEC_CATEGORY,
- .enc_fmt = MS_WEBRTC_AEC_ENC_FMT,
- .ninputs = MS_WEBRTC_AEC_NINPUTS,
- .noutputs = MS_WEBRTC_AEC_NOUTPUTS,
- .init = webrtc_aec_init,
- .preprocess = webrtc_aec_preprocess,
- .process = webrtc_aec_process,
- .postprocess = webrtc_aec_postprocess,
- .uninit = webrtc_aec_uninit,
- .methods = webrtc_aec_methods,
- .flags = MS_WEBRTC_AEC_FLAGS
-};
+MSFilterDesc ms_webrtc_aec_desc = {.id = MS_FILTER_PLUGIN_ID,
+ .name = MS_WEBRTC_AEC_NAME,
+ .text = MS_WEBRTC_AEC_DESCRIPTION,
+ .category = MS_WEBRTC_AEC_CATEGORY,
+ .enc_fmt = MS_WEBRTC_AEC_ENC_FMT,
+ .ninputs = MS_WEBRTC_AEC_NINPUTS,
+ .noutputs = MS_WEBRTC_AEC_NOUTPUTS,
+ .init = webrtc_aec_init,
+ .preprocess = webrtc_aec_preprocess,
+ .process = webrtc_aec_process,
+ .postprocess = webrtc_aec_postprocess,
+ .uninit = webrtc_aec_uninit,
+ .methods = webrtc_aec_methods,
+ .flags = MS_WEBRTC_AEC_FLAGS};
#endif
@@ -517,51 +535,40 @@ MS_FILTER_DESC_EXPORT(ms_webrtc_aec_desc)
#ifdef BUILD_AECM
-#define MS_WEBRTC_AECM_NAME "MSWebRTCAECM"
-#define MS_WEBRTC_AECM_DESCRIPTION "Echo canceller for mobile using WebRTC library."
-#define MS_WEBRTC_AECM_CATEGORY MS_FILTER_OTHER
-#define MS_WEBRTC_AECM_ENC_FMT NULL
-#define MS_WEBRTC_AECM_NINPUTS 2
-#define MS_WEBRTC_AECM_NOUTPUTS 2
-#define MS_WEBRTC_AECM_FLAGS 0
+#define MS_WEBRTC_AECM_NAME "MSWebRTCAECM"
+#define MS_WEBRTC_AECM_DESCRIPTION \
+ "Echo canceller for mobile using WebRTC library."
+#define MS_WEBRTC_AECM_CATEGORY MS_FILTER_OTHER
+#define MS_WEBRTC_AECM_ENC_FMT NULL
+#define MS_WEBRTC_AECM_NINPUTS 2
+#define MS_WEBRTC_AECM_NOUTPUTS 2
+#define MS_WEBRTC_AECM_FLAGS 0
#ifdef _MSC_VER
MSFilterDesc ms_webrtc_aecm_desc = {
- MS_FILTER_PLUGIN_ID,
- MS_WEBRTC_AECM_NAME,
- MS_WEBRTC_AECM_DESCRIPTION,
- MS_WEBRTC_AECM_CATEGORY,
- MS_WEBRTC_AECM_ENC_FMT,
- MS_WEBRTC_AECM_NINPUTS,
- MS_WEBRTC_AECM_NOUTPUTS,
- webrtc_aecm_init,
- webrtc_aec_preprocess,
- webrtc_aec_process,
- webrtc_aec_postprocess,
- webrtc_aec_uninit,
- webrtc_aec_methods,
- MS_WEBRTC_AECM_FLAGS
-};
+ MS_FILTER_PLUGIN_ID, MS_WEBRTC_AECM_NAME, MS_WEBRTC_AECM_DESCRIPTION,
+ MS_WEBRTC_AECM_CATEGORY, MS_WEBRTC_AECM_ENC_FMT, MS_WEBRTC_AECM_NINPUTS,
+ MS_WEBRTC_AECM_NOUTPUTS, webrtc_aecm_init, webrtc_aec_preprocess,
+ webrtc_aec_process, webrtc_aec_postprocess, webrtc_aec_uninit,
+ webrtc_aec_methods, MS_WEBRTC_AECM_FLAGS};
#else
-MSFilterDesc ms_webrtc_aecm_desc = {
- .id = MS_FILTER_PLUGIN_ID,
- .name = MS_WEBRTC_AECM_NAME,
- .text = MS_WEBRTC_AECM_DESCRIPTION,
- .category = MS_WEBRTC_AECM_CATEGORY,
- .enc_fmt = MS_WEBRTC_AECM_ENC_FMT,
- .ninputs = MS_WEBRTC_AECM_NINPUTS,
- .noutputs = MS_WEBRTC_AECM_NOUTPUTS,
- .init = webrtc_aecm_init,
- .preprocess = webrtc_aec_preprocess,
- .process = webrtc_aec_process,
- .postprocess = webrtc_aec_postprocess,
- .uninit = webrtc_aec_uninit,
- .methods = webrtc_aec_methods,
- .flags = MS_WEBRTC_AECM_FLAGS
-};
+MSFilterDesc ms_webrtc_aecm_desc = {.id = MS_FILTER_PLUGIN_ID,
+ .name = MS_WEBRTC_AECM_NAME,
+ .text = MS_WEBRTC_AECM_DESCRIPTION,
+ .category = MS_WEBRTC_AECM_CATEGORY,
+ .enc_fmt = MS_WEBRTC_AECM_ENC_FMT,
+ .ninputs = MS_WEBRTC_AECM_NINPUTS,
+ .noutputs = MS_WEBRTC_AECM_NOUTPUTS,
+ .init = webrtc_aecm_init,
+ .preprocess = webrtc_aec_preprocess,
+ .process = webrtc_aec_process,
+ .postprocess = webrtc_aec_postprocess,
+ .uninit = webrtc_aec_uninit,
+ .methods = webrtc_aec_methods,
+ .flags = MS_WEBRTC_AECM_FLAGS};
#endif
--
GitLab
|