-
Notifications
You must be signed in to change notification settings - Fork 161
/
AudioThread.cpp
785 lines (681 loc) · 23.7 KB
/
AudioThread.cpp
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
/*
* Copyright (c) 2005-2018, BearWare.dk
*
* Contact Information:
*
* Bjoern D. Rasmussen
* Kirketoften 5
* DK-8260 Viby J
* Denmark
* Email: contact@bearware.dk
* Phone: +45 20 20 54 59
* Web: http://www.bearware.dk
*
* This source code is part of the TeamTalk SDK owned by
* BearWare.dk. Use of this file, or its compiled unit, requires a
* TeamTalk SDK License Key issued by BearWare.dk.
*
* The TeamTalk SDK License Agreement along with its Terms and
* Conditions are outlined in the file License.txt included with the
* TeamTalk SDK distribution.
*
*/
#include "AudioThread.h"
#include <teamtalk/ttassert.h>
#include <teamtalk/CodecCommon.h>
#include <teamtalk/PacketLayout.h>
#include <codec/MediaUtil.h>
#include <myace/MyACE.h>
using namespace std;
using namespace teamtalk;
AudioThread::AudioThread()
: m_voicelevel(VU_METER_MIN)
, m_voiceactlevel(VU_METER_MIN)
, m_gainlevel(GAIN_NORMAL)
, m_enc_cleared(true)
, m_voiceact_delay(1, 500000)
, m_tone_sample_index(0)
, m_tone_frequency(0)
{
memset(&m_codec, 0, sizeof(m_codec));
m_codec.codec = teamtalk::CODEC_NO_CODEC;
m_encbuf.resize(MAX_ENC_FRAMESIZE);
}
AudioThread::~AudioThread()
{
}
bool AudioThread::StartEncoder(audioencodercallback_t callback,
const teamtalk::AudioCodec& codec,
bool spawn_thread)
{
if(this->thr_count() != 0)
return false;
TTASSERT(this->msg_queue()->is_empty());
int callback_samples = GetAudioCodecCbSamples(codec);
int sample_rate = GetAudioCodecSampleRate(codec);
int channels = GetAudioCodecChannels(codec);
switch(codec.codec)
{
case CODEC_NO_CODEC :
{
m_codec = codec;
m_callback = callback;
return true;
}
break;
case CODEC_SPEEX :
#if defined(ENABLE_SPEEX)
{
TTASSERT(callback_samples);
TTASSERT(sample_rate);
TTASSERT(channels);
m_speex.reset(new SpeexEncoder());
if(!m_speex->Initialize(codec.speex.bandmode,
DEFAULT_SPEEX_COMPLEXITY,
codec.speex.quality))
{
StopEncoder();
return false;
}
}
break;
#else
return false;
#endif
case CODEC_SPEEX_VBR :
#if defined(ENABLE_SPEEX)
{
TTASSERT(callback_samples);
TTASSERT(sample_rate);
TTASSERT(channels);
m_speex.reset(new SpeexEncoder());
if(!m_speex->Initialize(codec.speex_vbr.bandmode,
DEFAULT_SPEEX_COMPLEXITY,
(float)codec.speex_vbr.vbr_quality,
codec.speex_vbr.bitrate,
codec.speex_vbr.max_bitrate,
codec.speex_vbr.dtx))
{
StopEncoder();
return false;
}
}
break;
#else
return false;
#endif
#if defined(ENABLE_OPUS)
case CODEC_OPUS :
{
TTASSERT(callback_samples);
TTASSERT(sample_rate);
TTASSERT(channels);
m_opus.reset(new OpusEncode());
if(!m_opus->Open(codec.opus.samplerate, codec.opus.channels,
codec.opus.application) ||
!m_opus->SetComplexity(codec.opus.complexity) ||
!m_opus->SetFEC(codec.opus.fec) ||
!m_opus->SetDTX(codec.opus.dtx) ||
!m_opus->SetBitrate(codec.opus.bitrate) ||
!m_opus->SetVBR(codec.opus.vbr) ||
!m_opus->SetVBRConstraint(codec.opus.vbr_constraint))
{
StopEncoder();
return false;
}
}
break;
#else
return false;
#endif
default:
TTASSERT(codec.codec == CODEC_SPEEX);
}
TTASSERT(sample_rate);
if(!sample_rate || !callback_samples)
return false;
m_codec = codec;
m_callback = callback;
//allow one second of audio to build up in the queue
int max_queue = PCM16_BYTES(sample_rate, GetAudioCodecChannels(codec));
max_queue += (1 + (sample_rate / callback_samples)) * sizeof(media::AudioFrame);
this->msg_queue()->activate();
this->msg_queue()->high_water_mark(max_queue);
this->msg_queue()->low_water_mark(max_queue);
if(spawn_thread && this->activate() < 0)
{
StopEncoder();
return false;
}
MYTRACE_COND(codec.codec == CODEC_SPEEX,
ACE_TEXT("Launched Speex encoder, samplerate %d, bitrate %d, cb %d, fpp %d\n"),
GetAudioCodecSampleRate(codec), GetAudioCodecBitRate(codec),
GetAudioCodecFrameSize(codec), GetAudioCodecFramesPerPacket(codec));
MYTRACE_COND(codec.codec == CODEC_SPEEX_VBR,
ACE_TEXT("Launched Speex VBR encoder, samplerate %d, bitrate %d, cb %d, fpp %d\n"),
GetAudioCodecSampleRate(codec), GetAudioCodecBitRate(codec),
GetAudioCodecFrameSize(codec), GetAudioCodecFramesPerPacket(codec));
MYTRACE_COND(codec.codec == CODEC_OPUS,
ACE_TEXT("Launched OPUS VBR encoder, samplerate %d, bitrate %d, cb %d, channels %d\n"),
GetAudioCodecSampleRate(codec), GetAudioCodecBitRate(codec),
GetAudioCodecFrameSize(codec), GetAudioCodecChannels(codec));
return true;
}
void AudioThread::StopEncoder()
{
int ret = this->msg_queue()->close();
TTASSERT(ret >= 0);
wait();
#if defined(ENABLE_SPEEXDSP)
m_preprocess_left.reset();
m_preprocess_right.reset();
#endif
#if defined(ENABLE_WEBRTC)
m_apm.reset();
m_aps.reset();
#endif
#if defined(ENABLE_SPEEX)
m_speex.reset();
#endif
#if defined(ENABLE_OPUS)
m_opus.reset();
#endif
m_enc_cleared = true;
m_echobuf.clear();
m_callback = {};
memset(&m_codec, 0, sizeof(m_codec));
m_codec.codec = teamtalk::CODEC_NO_CODEC;
}
int AudioThread::close(u_long)
{
MYTRACE( ACE_TEXT("Audio Encoder thread closed\n") );
return 0;
}
bool AudioThread::UpdatePreprocessor(const teamtalk::AudioPreprocessor& preprocess)
{
//set AGC
std::unique_lock<std::recursive_mutex> g(m_preprocess_lock);
if (preprocess.preprocessor != AUDIOPREPROCESSOR_TEAMTALK)
MuteSound(false, false);
#if defined(ENABLE_SPEEXDSP)
if (preprocess.preprocessor != AUDIOPREPROCESSOR_SPEEXDSP)
{
m_preprocess_left.reset();
m_preprocess_right.reset();
}
#endif
#if defined(ENABLE_WEBRTC)
if (preprocess.preprocessor != AUDIOPREPROCESSOR_WEBRTC)
{
m_apm.reset();
m_aps.reset();
}
#endif
// just ignore preprocessor if not audio codec is set
if (codec().codec == CODEC_NO_CODEC)
return true;
MYTRACE(ACE_TEXT("Setting up audio preprocessor: %d\n"), preprocess.preprocessor);
switch (preprocess.preprocessor)
{
case AUDIOPREPROCESSOR_NONE :
// 'm_gainlevel' should not be reset
return true;
case AUDIOPREPROCESSOR_SPEEXDSP :
// 'm_gainlevel' should not be reset
return UpdatePreprocess(preprocess.speexdsp);
case AUDIOPREPROCESSOR_TEAMTALK :
MuteSound(preprocess.ttpreprocessor.muteleft, preprocess.ttpreprocessor.muteright);
m_gainlevel = preprocess.ttpreprocessor.gainlevel;
return true;
case AUDIOPREPROCESSOR_WEBRTC :
#if defined(ENABLE_WEBRTC)
// WebRTC requires 10 msec audio frames
if (GetAudioCodecCbMillis(m_codec) % 10 != 0)
{
MYTRACE(ACE_TEXT("Failed to initialize WebRTC audio preprocessor. Not 10 msec frames.\n"));
return false;
}
if (!m_apm)
m_apm.reset(webrtc::AudioProcessingBuilder().Create());
m_apm->ApplyConfig(preprocess.webrtc);
if (m_apm->Initialize() != webrtc::AudioProcessing::kNoError)
{
m_apm.reset();
MYTRACE(ACE_TEXT("Failed to initialize WebRTC audio preprocessor\n"));
return false;
}
else
{
MYTRACE(ACE_TEXT("Initialized WebRTC: gain2=%d level=%g, denoise=%d suppress=%d, echo%d\n"),
int(m_apm->GetConfig().gain_controller2.enabled),
double(m_apm->GetConfig().gain_controller2.fixed_digital.gain_db),
int(m_apm->GetConfig().noise_suppression.enabled),
int(m_apm->GetConfig().noise_suppression.level),
int(m_apm->GetConfig().echo_canceller.enabled));
}
m_aps.reset(new webrtc::AudioProcessingStats());
return true;
#else
return false;
#endif
}
return false;
}
bool AudioThread::UpdatePreprocess(const teamtalk::SpeexDSP& speexdsp)
{
#if defined(ENABLE_SPEEXDSP)
assert(codec().codec != CODEC_NO_CODEC);
int callback_samples = GetAudioCodecCbSamples(codec());
int sample_rate = GetAudioCodecSampleRate(codec());
int channels = GetAudioCodecChannels(codec());
if (!m_preprocess_left)
{
if (channels == 2)
{
m_preprocess_left.reset(new SpeexPreprocess());
m_preprocess_right.reset(new SpeexPreprocess());
if (!m_preprocess_left->Initialize(sample_rate, callback_samples) ||
!m_preprocess_right->Initialize(sample_rate, callback_samples))
{
m_preprocess_left.reset();
m_preprocess_right.reset();
return false;
}
//Speex has denoise on by default, so disable
m_preprocess_left->EnableDenoise(false);
m_preprocess_right->EnableDenoise(false);
}
else
{
m_preprocess_left.reset(new SpeexPreprocess());
if (!m_preprocess_left->Initialize(sample_rate, callback_samples))
{
m_preprocess_left.reset();
return false;
}
m_preprocess_left->EnableDenoise(false);
}
}
SpeexAGC agc;
agc.gain_level = (float)speexdsp.agc_gainlevel;
agc.max_increment = speexdsp.agc_maxincdbsec;
agc.max_decrement = speexdsp.agc_maxdecdbsec;
agc.max_gain = speexdsp.agc_maxgaindb;
//AGC
bool agc_success = true;
agc_success &= m_preprocess_left->EnableAGC(speexdsp.enable_agc);
agc_success &= (channels == 1 || m_preprocess_right->EnableAGC(speexdsp.enable_agc));
agc_success &= m_preprocess_left->SetAGCSettings(agc);
agc_success &= (channels == 1 || m_preprocess_right->SetAGCSettings(agc));
//denoise
bool denoise_success = true;
denoise_success &= m_preprocess_left->EnableDenoise(speexdsp.enable_denoise);
denoise_success &= (channels == 1 || m_preprocess_right->EnableDenoise(speexdsp.enable_denoise));
denoise_success &= m_preprocess_left->SetDenoiseLevel(speexdsp.maxnoisesuppressdb);
denoise_success &= (channels == 1 || m_preprocess_right->SetDenoiseLevel(speexdsp.maxnoisesuppressdb));
//set AEC
bool aec_success = true;
aec_success &= m_preprocess_left->EnableEchoCancel(speexdsp.enable_aec);
aec_success &= (channels == 1 || m_preprocess_right->EnableEchoCancel(speexdsp.enable_aec));
aec_success &= m_preprocess_left->SetEchoSuppressLevel(speexdsp.aec_suppress_level);
aec_success &= (channels == 1 || m_preprocess_right->SetEchoSuppressLevel(speexdsp.aec_suppress_level));
aec_success &= m_preprocess_left->SetEchoSuppressActive(speexdsp.aec_suppress_active);
aec_success &= (channels == 1 || m_preprocess_right->SetEchoSuppressActive(speexdsp.aec_suppress_active));
//set dereverb
bool dereverb = true;
m_preprocess_left->EnableDereverb(dereverb);
if(channels == 2)
m_preprocess_right->EnableDereverb(dereverb);
MYTRACE_COND(!agc_success && speexdsp.enable_agc,
ACE_TEXT("Failed to set SpeexDSP AGC settings\n"));
MYTRACE_COND(!denoise_success && speexdsp.enable_denoise,
ACE_TEXT("Failed to set SpeexDSP denoise settings\n"));
MYTRACE_COND(!aec_success && speexdsp.enable_aec,
ACE_TEXT("Failed to set SpeexDSP AEC settings\n"));
if ((speexdsp.enable_agc && !agc_success) ||
(speexdsp.enable_denoise && !denoise_success) ||
(speexdsp.enable_aec && !aec_success))
return false;
MYTRACE(ACE_TEXT("Set audio cfg. AGC: %d, %d, %d, %d, %d. Denoise: %d, %d. AEC: %d, %d, %d.\n"),
speexdsp.enable_agc, (int)speexdsp.agc_gainlevel,
speexdsp.agc_maxincdbsec, speexdsp.agc_maxdecdbsec,
speexdsp.agc_maxgaindb, speexdsp.enable_denoise,
speexdsp.maxnoisesuppressdb, speexdsp.enable_aec,
speexdsp.aec_suppress_level, speexdsp.aec_suppress_active);
return true;
#else
return false;
#endif
}
void AudioThread::MuteSound(bool leftchannel, bool rightchannel)
{
m_stereo = ToStereoMask(leftchannel, rightchannel);
}
void AudioThread::QueueAudio(const media::AudioFrame& audframe)
{
TTASSERT(m_codec.codec != CODEC_NO_CODEC);
assert(audframe.inputfmt.channels == audframe.outputfmt.channels || audframe.outputfmt.channels == 0);
assert(audframe.input_samples == audframe.output_samples || audframe.output_samples == 0);
ACE_Message_Block* mb = AudioFrameToMsgBlock(audframe);
if (mb)
QueueAudio(mb);
}
void AudioThread::QueueAudio(ACE_Message_Block* mb_audio)
{
//add audio
ACE_Time_Value tv;
if(putq(mb_audio, &tv)<0)
{
MYTRACE(ACE_TEXT("AudioThread msg_q full, dropped frame\n"));
mb_audio->release();
}
}
bool AudioThread::IsVoiceActive()
{
#if defined(ENABLE_WEBRTC)
std::unique_lock<std::recursive_mutex> g(m_preprocess_lock);
if (m_apm && m_apm->GetConfig().voice_detection.enabled)
{
assert(m_aps);
return m_aps->voice_detected.value_or(false) ||
m_lastActive + m_voiceact_delay > ACE_OS::gettimeofday();
}
#endif
return m_voicelevel >= m_voiceactlevel ||
m_lastActive + m_voiceact_delay > ACE_OS::gettimeofday();
}
int AudioThread::GetCurrentVoiceLevel()
{
#if defined(ENABLE_WEBRTC)
std::unique_lock<std::recursive_mutex> g(m_preprocess_lock);
if (m_apm)
{
assert(m_aps);
auto cfg = m_apm->GetConfig();
if (cfg.level_estimation.enabled)
{
// WebRTC's maximum value for dB from digital full scale
float value = 127.f - m_aps->output_rms_dbfs.value_or(0);
value /= 127.f;
return int(VU_METER_MAX * value);
}
}
#endif
return m_voicelevel;
}
void AudioThread::ProcessQueue(ACE_Time_Value* tm)
{
TTASSERT(m_codec.codec != CODEC_NO_CODEC);
TTASSERT(m_callback);
ACE_Message_Block* mb;
while (getq(mb, tm) >= 0)
{
MBGuard g(mb);
media::AudioFrame af(mb);
ProcessAudioFrame(af);
}
}
int AudioThread::svc(void)
{
ProcessQueue(NULL);
return 0;
}
void AudioThread::ProcessAudioFrame(media::AudioFrame& audblock)
{
if(m_tone_frequency)
m_tone_sample_index = GenerateTone(audblock, m_tone_sample_index, m_tone_frequency);
SOFTGAIN(audblock.input_buffer, audblock.input_samples,
audblock.inputfmt.channels, m_gainlevel, GAIN_NORMAL);
#if defined(ENABLE_SPEEXDSP)
PreprocessSpeex(audblock);
#endif
#if defined(ENABLE_WEBRTC)
bool vad = false;
if (m_gainlevel > 0)
{
// WebRTC preprocessing (especially AEC) is very CPU-intensive
// Only do it if the input is not muted.
// This allows client apps that use PTT to close the MIC input when there's
// no PTT and thus prevent the processing hit.
// AEC still functions fine if it's activated like this, although there's
// minute echo fragment at the (re)start of the preprocessing
PreprocessWebRTC(audblock, vad);
}
if (!vad)
#endif
{
MeasureVoiceLevel(audblock);
}
// mute left or right speaker (if enabled)
if(audblock.inputfmt.channels == 2)
SelectStereo(m_stereo, audblock.input_buffer, audblock.input_samples);
if ((IsVoiceActive() && audblock.voiceact_enc) || audblock.force_enc)
{
//encode
const char* enc_data = NULL;
std::vector<int> enc_frame_sizes;
switch(m_codec.codec)
{
case CODEC_SPEEX :
case CODEC_SPEEX_VBR :
#if defined(ENABLE_SPEEX)
enc_data = ProcessSpeex(audblock, enc_frame_sizes);
#endif
break;
case CODEC_OPUS :
#if defined(ENABLE_OPUS)
enc_data = ProcessOPUS(audblock, enc_frame_sizes);
#endif
break;
case CODEC_NO_CODEC :
case CODEC_WEBM_VP8 :
break;
}
if(enc_data)
{
int nbBytes = 0;
for(size_t i=0;i<enc_frame_sizes.size();i++)
nbBytes += enc_frame_sizes[i];
m_callback(m_codec, enc_data, nbBytes,
enc_frame_sizes, audblock);
}
m_enc_cleared = false;
}
else
{
//clear encoder state
if(!m_enc_cleared)
{
#if defined(ENABLE_SPEEX)
if(m_speex)
m_speex->Reset();
#endif
#if defined(ENABLE_OPUS)
if (m_opus)
m_opus->Reset();
#endif
m_enc_cleared = true;
}
m_callback(m_codec, NULL, 0, std::vector<int>(), audblock);
}
}
void AudioThread::MeasureVoiceLevel(const media::AudioFrame& audblock)
{
const int VU_MAX_VOLUME = 8000; //real maximum is if all samples are 32768
const int VOICEACT_STOPDELAY = 1500;//msecs to wait before stopping after voiceact has been disabled
int lsum = 0, rsum = 0, sum = 0;
int samples_total = audblock.input_samples * audblock.inputfmt.channels;
if (audblock.inputfmt.channels == 2)
{
for (int i = 0; i < samples_total; i += 2)
{
lsum += abs(audblock.input_buffer[i]);
rsum += abs(audblock.input_buffer[i + 1]);
}
switch (m_stereo)
{
case STEREO_BOTH:
sum = (lsum + rsum) / 2;
break;
case STEREO_LEFT:
sum = lsum;
break;
case STEREO_RIGHT:
sum = rsum;
break;
case STEREO_NONE:
sum = 0;
break;
}
}
else
{
for (int i = 0; i < samples_total; ++i)
sum += abs(audblock.input_buffer[i]);
}
int avg = sum / audblock.input_samples;
avg = 100 * avg / VU_MAX_VOLUME;
this->m_voicelevel = avg > VU_METER_MAX ? VU_METER_MAX : avg;
if (this->m_voicelevel >= this->m_voiceactlevel)
m_lastActive = ACE_OS::gettimeofday();
}
#if defined(ENABLE_SPEEXDSP)
void AudioThread::PreprocessSpeex(media::AudioFrame& audblock)
{
std::unique_lock<std::recursive_mutex> g(m_preprocess_lock);
bool preprocess = false;
if (!m_preprocess_left)
return;
preprocess |= m_preprocess_left->IsEchoCancel();
preprocess |= m_preprocess_left->IsDenoising();
//don't include dereverb since it's not user configurable
// preprocess |= m_preprocess_left->IsDereverbing();
preprocess |= m_preprocess_left->IsAGC();
if(!preprocess)
return;
if(audblock.inputfmt.channels == 1)
{
if (m_preprocess_left->IsEchoCancel() &&
audblock.outputfmt.channels == 1 && audblock.output_buffer)
{
if(m_echobuf.size() != (size_t)audblock.input_samples)
m_echobuf.resize(audblock.input_samples);
m_preprocess_left->EchoCancel(audblock.input_buffer,
audblock.output_buffer,
&m_echobuf[0]);
audblock.input_buffer = &m_echobuf[0];
}
m_preprocess_left->Preprocess(audblock.input_buffer); //denoise, AGC, etc
}
else if(audblock.inputfmt.channels == 2)
{
assert(m_preprocess_right);
vector<short> in_leftchan(audblock.input_samples),
in_rightchan(audblock.input_samples);
SplitStereo(audblock.input_buffer, audblock.input_samples, in_leftchan, in_rightchan);
if(m_preprocess_left->IsEchoCancel() && m_preprocess_right->IsEchoCancel() &&
audblock.outputfmt.channels == 2 && audblock.output_buffer)
{
assert(audblock.input_samples == audblock.output_samples);
vector<short> out_leftchan(audblock.output_samples),
out_rightchan(audblock.output_samples),
echobuf_left(audblock.output_samples),
echobuf_right(audblock.output_samples);
SplitStereo(audblock.output_buffer, audblock.output_samples,
out_leftchan, out_rightchan);
m_preprocess_left->EchoCancel(&in_leftchan[0], &out_leftchan[0],
&echobuf_left[0]);
in_leftchan.swap(echobuf_left);
m_preprocess_right->EchoCancel(&in_rightchan[0], &out_rightchan[0],
&echobuf_right[0]);
in_rightchan.swap(echobuf_right);
}
m_preprocess_left->Preprocess(&in_leftchan[0]); //denoise, AGC, etc
m_preprocess_right->Preprocess(&in_rightchan[0]); //denoise, AGC, etc
MergeStereo(in_leftchan, in_rightchan, audblock.input_buffer,
audblock.input_samples);
}
}
#endif
#if defined(ENABLE_WEBRTC)
void AudioThread::PreprocessWebRTC(media::AudioFrame& audblock, bool& vad)
{
std::unique_lock<std::recursive_mutex> g(m_preprocess_lock);
if (!m_apm)
return;
if (WebRTCPreprocess(*m_apm, audblock, audblock, m_aps.get()) != audblock.input_samples)
{
MYTRACE(ACE_TEXT("WebRTC failed to process audio\n"));
}
vad = m_apm->GetConfig().voice_detection.enabled;
if (vad)
{
assert(m_aps);
if (m_aps->voice_detected.value_or(false))
m_lastActive = ACE_OS::gettimeofday();
}
}
#endif
#if defined(ENABLE_SPEEX)
const char* AudioThread::ProcessSpeex(const media::AudioFrame& audblock,
std::vector<int>& enc_frame_sizes)
{
TTASSERT(m_speex);
int framesize = GetAudioCodecFrameSize(m_codec);
int nbBytes = 0, n_processed = 0, ret;
int fpp = GetAudioCodecFramesPerPacket(m_codec);
int enc_frm_size;
assert(fpp);
assert(framesize>0);
if (framesize <= 0 || fpp <= 0)
return nullptr;
enc_frm_size = int(m_encbuf.size()) / fpp;
while(n_processed < audblock.input_samples)
{
assert(nbBytes + enc_frm_size <= int(m_encbuf.size()));
ret = m_speex->Encode(&audblock.input_buffer[n_processed],
&m_encbuf[nbBytes], enc_frm_size);
assert(ret>0);
if(ret <= 0)
return nullptr;
enc_frame_sizes.push_back(ret);
n_processed += framesize;
nbBytes += ret;
}
TTASSERT(nbBytes <= (int)m_encbuf.size());
return &m_encbuf[0];
}
#endif
#if defined(ENABLE_OPUS)
const char* AudioThread::ProcessOPUS(const media::AudioFrame& audblock,
std::vector<int>& enc_frame_sizes)
{
TTASSERT(m_opus);
TTASSERT(audblock.input_samples == GetAudioCodecCbSamples(m_codec));
int framesize = GetAudioCodecFrameSize(m_codec);
int channels = GetAudioCodecChannels(m_codec);
int fpp = GetAudioCodecFramesPerPacket(m_codec);
int nbBytes = 0, n_processed = 0, ret;
int enc_frm_size;
assert(fpp);
assert(framesize>0);
if (framesize <= 0 || fpp <= 0)
return nullptr;
enc_frm_size = int(m_encbuf.size()) / fpp;
while(n_processed < audblock.input_samples)
{
assert(nbBytes + enc_frm_size <= int(m_encbuf.size()));
ret = m_opus->Encode(&audblock.input_buffer[n_processed*channels],
framesize, &m_encbuf[nbBytes], enc_frm_size);
assert(ret>0);
if(ret <= 0)
return nullptr;
// enc_frm_size -= ret; /* stay within MAX_ENC_FRAMESIZE */
enc_frame_sizes.push_back(ret);
n_processed += framesize;
nbBytes += ret;
}
TTASSERT(nbBytes <= (int)m_encbuf.size());
return &m_encbuf[0];
}
#endif