File: TestVADDTX.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (377 lines) | stat: -rw-r--r-- 10,124 bytes parent folder | download
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
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/modules/audio_coding/main/test/TestVADDTX.h"

#include <iostream>

#include "webrtc/common_types.h"
#include "webrtc/engine_configurations.h"
#include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
#include "webrtc/modules/audio_coding/main/interface/audio_coding_module_typedefs.h"
#include "webrtc/modules/audio_coding/main/test/utility.h"
#include "webrtc/system_wrappers/interface/trace.h"
#include "webrtc/test/testsupport/fileutils.h"

namespace webrtc {

TestVADDTX::TestVADDTX()
    : _acmA(AudioCodingModule::Create(0)),
      _acmB(AudioCodingModule::Create(1)),
      _channelA2B(NULL) {}

TestVADDTX::~TestVADDTX() {
  if (_channelA2B != NULL) {
    delete _channelA2B;
    _channelA2B = NULL;
  }
}

void TestVADDTX::Perform() {
  const std::string file_name = webrtc::test::ResourcePath(
      "audio_coding/testfile32kHz", "pcm");
  _inFileA.Open(file_name, 32000, "rb");

  EXPECT_EQ(0, _acmA->InitializeReceiver());
  EXPECT_EQ(0, _acmB->InitializeReceiver());

  uint8_t numEncoders = _acmA->NumberOfCodecs();
  CodecInst myCodecParam;
  for (uint8_t n = 0; n < numEncoders; n++) {
    EXPECT_EQ(0, _acmB->Codec(n, &myCodecParam));
    if (!strcmp(myCodecParam.plname, "opus")) {
      // Register Opus as mono.
      myCodecParam.channels = 1;
    }
    EXPECT_EQ(0, _acmB->RegisterReceiveCodec(myCodecParam));
  }

  // Create and connect the channel
  _channelA2B = new Channel;
  _acmA->RegisterTransportCallback(_channelA2B);
  _channelA2B->RegisterReceiverACM(_acmB.get());

  _acmA->RegisterVADCallback(&_monitor);

  int16_t testCntr = 1;

#ifdef WEBRTC_CODEC_ISAC
  // Open outputfile
  OpenOutFile(testCntr++);

  // Register iSAC WB as send codec
  char nameISAC[] = "ISAC";
  RegisterSendCodec('A', nameISAC, 16000);

  // Run the five test cased
  runTestCases();

  // Close file
  _outFileB.Close();

  // Open outputfile
  OpenOutFile(testCntr++);

  // Register iSAC SWB as send codec
  RegisterSendCodec('A', nameISAC, 32000);

  // Run the five test cased
  runTestCases();

  // Close file
  _outFileB.Close();
#endif
#ifdef WEBRTC_CODEC_ILBC
  // Open outputfile
  OpenOutFile(testCntr++);

  // Register iLBC as send codec
  char nameILBC[] = "ilbc";
  RegisterSendCodec('A', nameILBC);

  // Run the five test cased
  runTestCases();

  // Close file
  _outFileB.Close();

#endif
#ifdef WEBRTC_CODEC_OPUS
  // Open outputfile
  OpenOutFile(testCntr++);

  // Register Opus as send codec
  char nameOPUS[] = "opus";
  RegisterSendCodec('A', nameOPUS);

  // Run the five test cased
  runTestCases();

  // Close file
  _outFileB.Close();

#endif
}

void TestVADDTX::runTestCases() {
  // #1 DTX = OFF, VAD = ON, VADNormal
  SetVAD(false, true, VADNormal);
  Run();
  VerifyTest();

  // #2 DTX = OFF, VAD = ON, VADAggr
  SetVAD(false, true, VADAggr);
  Run();
  VerifyTest();

  // #3 DTX = ON, VAD = ON, VADLowBitrate
  SetVAD(true, true, VADLowBitrate);
  Run();
  VerifyTest();

  // #4 DTX = ON, VAD = ON, VADVeryAggr
  SetVAD(true, true, VADVeryAggr);
  Run();
  VerifyTest();

  // #5 DTX = ON, VAD = OFF, VADNormal
  SetVAD(true, false, VADNormal);
  Run();
  VerifyTest();
}

void TestVADDTX::runTestInternalDTX(int expected_result) {
  // #6 DTX = ON, VAD = ON, VADNormal
  SetVAD(true, true, VADNormal);
  EXPECT_EQ(expected_result, _acmA->ReplaceInternalDTXWithWebRtc(true));
  if (expected_result == 0) {
    Run();
    VerifyTest();
  }
}

void TestVADDTX::SetVAD(bool statusDTX, bool statusVAD, int16_t vadMode) {
  bool dtxEnabled, vadEnabled;
  ACMVADMode vadModeSet;

  EXPECT_EQ(0, _acmA->SetVAD(statusDTX, statusVAD, (ACMVADMode) vadMode));
  EXPECT_EQ(0, _acmA->VAD(&dtxEnabled, &vadEnabled, &vadModeSet));

  // Requested VAD/DTX settings
  _setStruct.statusDTX = statusDTX;
  _setStruct.statusVAD = statusVAD;
  _setStruct.vadMode = (ACMVADMode) vadMode;

  // VAD settings after setting VAD in ACM
  _getStruct.statusDTX = dtxEnabled;
  _getStruct.statusVAD = vadEnabled;
  _getStruct.vadMode = vadModeSet;
}

VADDTXstruct TestVADDTX::GetVAD() {
  VADDTXstruct retStruct;
  bool dtxEnabled, vadEnabled;
  ACMVADMode vadModeSet;

  EXPECT_EQ(0, _acmA->VAD(&dtxEnabled, &vadEnabled, &vadModeSet));

  retStruct.statusDTX = dtxEnabled;
  retStruct.statusVAD = vadEnabled;
  retStruct.vadMode = vadModeSet;
  return retStruct;
}

int16_t TestVADDTX::RegisterSendCodec(char side, char* codecName,
                                      int32_t samplingFreqHz,
                                      int32_t rateKbps) {
  std::cout << std::flush;
  AudioCodingModule* myACM;
  switch (side) {
    case 'A': {
      myACM = _acmA.get();
      break;
    }
    case 'B': {
      myACM = _acmB.get();
      break;
    }
    default:
      return -1;
  }

  if (myACM == NULL) {
    return -1;
  }

  CodecInst myCodecParam;
  for (int16_t codecCntr = 0; codecCntr < myACM->NumberOfCodecs();
      codecCntr++) {
    EXPECT_EQ(0, myACM->Codec((uint8_t) codecCntr, &myCodecParam));
    if (!STR_CASE_CMP(myCodecParam.plname, codecName)) {
      if ((samplingFreqHz == -1) || (myCodecParam.plfreq == samplingFreqHz)) {
        if ((rateKbps == -1) || (myCodecParam.rate == rateKbps)) {
          break;
        }
      }
    }
  }

  // We only allow VAD/DTX when sending mono.
  myCodecParam.channels = 1;
  EXPECT_EQ(0, myACM->RegisterSendCodec(myCodecParam));

  // initialization was succesful
  return 0;
}

void TestVADDTX::Run() {
  AudioFrame audioFrame;

  uint16_t SamplesIn10MsecA = _inFileA.PayloadLength10Ms();
  uint32_t timestampA = 1;
  int32_t outFreqHzB = _outFileB.SamplingFrequency();

  while (!_inFileA.EndOfFile()) {
    _inFileA.Read10MsData(audioFrame);
    audioFrame.timestamp_ = timestampA;
    timestampA += SamplesIn10MsecA;
    EXPECT_EQ(0, _acmA->Add10MsData(audioFrame));
    EXPECT_GT(_acmA->Process(), -1);
    EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame));
    _outFileB.Write10MsData(audioFrame.data_, audioFrame.samples_per_channel_);
  }
#ifdef PRINT_STAT
  _monitor.PrintStatistics();
#endif
  _inFileA.Rewind();
  _monitor.GetStatistics(_statCounter);
  _monitor.ResetStatistics();
}

void TestVADDTX::OpenOutFile(int16_t test_number) {
  std::string file_name;
  std::stringstream file_stream;
  file_stream << webrtc::test::OutputPath();
  file_stream << "testVADDTX_outFile_";
  file_stream << test_number << ".pcm";
  file_name = file_stream.str();
  _outFileB.Open(file_name, 16000, "wb");
}

int16_t TestVADDTX::VerifyTest() {
  // Verify empty frame result
  uint8_t statusEF = 0;
  uint8_t vadPattern = 0;
  uint8_t emptyFramePattern[6];
  CodecInst myCodecParam;
  _acmA->SendCodec(&myCodecParam);

  // TODO(minyue): Remove these treatment on Opus when DTX is properly handled
  // by ACMOpus.
  if (STR_CASE_CMP(myCodecParam.plname, "opus") == 0) {
      _setStruct.statusDTX = false;
      _setStruct.statusVAD = false;
  }

  bool isReplaced = false;
  _acmA->IsInternalDTXReplacedWithWebRtc(&isReplaced);
  bool webRtcDtxInUse = _getStruct.statusDTX && isReplaced;
  bool codecDtxInUse = _getStruct.statusDTX && !isReplaced;

  // Check for error in VAD/DTX settings
  if (_getStruct.statusDTX != _setStruct.statusDTX) {
    // DTX status doesn't match expected.
    vadPattern |= 1;
  }
  if (!_getStruct.statusVAD && webRtcDtxInUse) {
    // WebRTC DTX cannot run without WebRTC VAD.
    vadPattern |= 2;
  }
  if ((!_getStruct.statusDTX || codecDtxInUse) &&
      (_getStruct.statusVAD != _setStruct.statusVAD)) {
    // Using no DTX or codec Internal DTX should not affect setting of VAD.
    vadPattern |= 4;
  }
  if (_getStruct.vadMode != _setStruct.vadMode) {
    // VAD Mode doesn't match expected.
    vadPattern |= 8;
  }

  // Set expected empty frame pattern
  int ii;
  for (ii = 0; ii < 6; ii++) {
    emptyFramePattern[ii] = 0;
  }
  // 0 - "kNoEncoding", not important to check.
  //      Codecs with packetsize != 80 samples will get this output.
  // 1 - "kActiveNormalEncoded", expect to receive some frames with this label .
  // 2 - "kPassiveNormalEncoded".
  // 3 - "kPassiveDTXNB".
  // 4 - "kPassiveDTXWB".
  // 5 - "kPassiveDTXSWB".
  emptyFramePattern[0] = 1;
  emptyFramePattern[1] = 1;
  emptyFramePattern[2] = _getStruct.statusVAD && !webRtcDtxInUse;
  emptyFramePattern[3] = webRtcDtxInUse && (_acmA->SendFrequency() == 8000);
  emptyFramePattern[4] = webRtcDtxInUse && (_acmA->SendFrequency() == 16000);
  emptyFramePattern[5] = webRtcDtxInUse && (_acmA->SendFrequency() == 32000);

  // Check pattern 1-5 (skip 0)
  for (int ii = 1; ii < 6; ii++) {
    if (emptyFramePattern[ii]) {
      statusEF |= (_statCounter[ii] == 0);
    } else {
      statusEF |= (_statCounter[ii] > 0);
    }
  }
  EXPECT_EQ(0, statusEF);
  EXPECT_EQ(0, vadPattern);

  return 0;
}

ActivityMonitor::ActivityMonitor() {
  _counter[0] = _counter[1] = _counter[2] = _counter[3] = _counter[4] =
      _counter[5] = 0;
}

ActivityMonitor::~ActivityMonitor() {
}

int32_t ActivityMonitor::InFrameType(int16_t frameType) {
  _counter[frameType]++;
  return 0;
}

void ActivityMonitor::PrintStatistics() {
  printf("\n");
  printf("kActiveNormalEncoded  kPassiveNormalEncoded  kPassiveDTXWB  ");
  printf("kPassiveDTXNB kPassiveDTXSWB kFrameEmpty\n");
  printf("%19u", _counter[1]);
  printf("%22u", _counter[2]);
  printf("%14u", _counter[3]);
  printf("%14u", _counter[4]);
  printf("%14u", _counter[5]);
  printf("%11u", _counter[0]);
  printf("\n\n");
}

void ActivityMonitor::ResetStatistics() {
  _counter[0] = _counter[1] = _counter[2] = _counter[3] = _counter[4] =
      _counter[5] = 0;
}

void ActivityMonitor::GetStatistics(uint32_t* getCounter) {
  for (int ii = 0; ii < 6; ii++) {
    getCounter[ii] = _counter[ii];
  }
}

}  // namespace webrtc