1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/modules/audio_coding/test/TestVADDTX.h"
12
13#include <string>
14
15#include "webrtc/engine_configurations.h"
16#include "webrtc/modules/audio_coding/test/PCMFile.h"
17#include "webrtc/modules/audio_coding/test/utility.h"
18#include "webrtc/test/testsupport/fileutils.h"
19
20namespace webrtc {
21
22#ifdef WEBRTC_CODEC_ISAC
23const CodecInst kIsacWb = {103, "ISAC", 16000, 480, 1, 32000};
24const CodecInst kIsacSwb = {104, "ISAC", 32000, 960, 1, 56000};
25#endif
26
27#ifdef WEBRTC_CODEC_ILBC
28const CodecInst kIlbc = {102, "ILBC", 8000, 240, 1, 13300};
29#endif
30
31#ifdef WEBRTC_CODEC_OPUS
32const CodecInst kOpus = {120, "opus", 48000, 960, 1, 64000};
33const CodecInst kOpusStereo = {120, "opus", 48000, 960, 2, 64000};
34#endif
35
36ActivityMonitor::ActivityMonitor() {
37  ResetStatistics();
38}
39
40int32_t ActivityMonitor::InFrameType(FrameType frame_type) {
41  counter_[frame_type]++;
42  return 0;
43}
44
45void ActivityMonitor::PrintStatistics() {
46  printf("\n");
47  printf("kEmptyFrame       %u\n", counter_[kEmptyFrame]);
48  printf("kAudioFrameSpeech %u\n", counter_[kAudioFrameSpeech]);
49  printf("kAudioFrameCN     %u\n", counter_[kAudioFrameCN]);
50  printf("kVideoFrameKey    %u\n", counter_[kVideoFrameKey]);
51  printf("kVideoFrameDelta  %u\n", counter_[kVideoFrameDelta]);
52  printf("\n\n");
53}
54
55void ActivityMonitor::ResetStatistics() {
56  memset(counter_, 0, sizeof(counter_));
57}
58
59void ActivityMonitor::GetStatistics(uint32_t* counter) {
60  memcpy(counter, counter_, sizeof(counter_));
61}
62
63TestVadDtx::TestVadDtx()
64    : acm_send_(AudioCodingModule::Create(0)),
65      acm_receive_(AudioCodingModule::Create(1)),
66      channel_(new Channel),
67      monitor_(new ActivityMonitor) {
68  EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get()));
69  channel_->RegisterReceiverACM(acm_receive_.get());
70  EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get()));
71}
72
73void TestVadDtx::RegisterCodec(CodecInst codec_param) {
74  // Set the codec for sending and receiving.
75  EXPECT_EQ(0, acm_send_->RegisterSendCodec(codec_param));
76  EXPECT_EQ(0, acm_receive_->RegisterReceiveCodec(codec_param));
77  channel_->SetIsStereo(codec_param.channels > 1);
78}
79
80// Encoding a file and see if the numbers that various packets occur follow
81// the expectation.
82void TestVadDtx::Run(std::string in_filename, int frequency, int channels,
83                     std::string out_filename, bool append,
84                     const int* expects) {
85  monitor_->ResetStatistics();
86
87  PCMFile in_file;
88  in_file.Open(in_filename, frequency, "rb");
89  in_file.ReadStereo(channels > 1);
90  // Set test length to 1000 ms (100 blocks of 10 ms each).
91  in_file.SetNum10MsBlocksToRead(100);
92  // Fast-forward both files 500 ms (50 blocks). The first second of the file is
93  // silence, but we want to keep half of that to test silence periods.
94  in_file.FastForward(50);
95
96  PCMFile out_file;
97  if (append) {
98    out_file.Open(out_filename, kOutputFreqHz, "ab");
99  } else {
100    out_file.Open(out_filename, kOutputFreqHz, "wb");
101  }
102
103  uint16_t frame_size_samples = in_file.PayloadLength10Ms();
104  uint32_t time_stamp = 0x12345678;
105  AudioFrame audio_frame;
106  while (!in_file.EndOfFile()) {
107    in_file.Read10MsData(audio_frame);
108    audio_frame.timestamp_ = time_stamp;
109    time_stamp += frame_size_samples;
110    EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
111    acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame);
112    out_file.Write10MsData(audio_frame);
113  }
114
115  in_file.Close();
116  out_file.Close();
117
118#ifdef PRINT_STAT
119  monitor_->PrintStatistics();
120#endif
121
122  uint32_t stats[5];
123  monitor_->GetStatistics(stats);
124  monitor_->ResetStatistics();
125
126  for (const auto& st : stats) {
127    int i = &st - stats;  // Calculate the current position in stats.
128    switch (expects[i]) {
129      case 0: {
130        EXPECT_EQ(0u, st) << "stats[" << i << "] error.";
131        break;
132      }
133      case 1: {
134        EXPECT_GT(st, 0u) << "stats[" << i << "] error.";
135        break;
136      }
137    }
138  }
139}
140
141// Following is the implementation of TestWebRtcVadDtx.
142TestWebRtcVadDtx::TestWebRtcVadDtx()
143    : vad_enabled_(false),
144      dtx_enabled_(false),
145      output_file_num_(0) {
146}
147
148void TestWebRtcVadDtx::Perform() {
149  // Go through various test cases.
150#ifdef WEBRTC_CODEC_ISAC
151  // Register iSAC WB as send codec
152  RegisterCodec(kIsacWb);
153  RunTestCases();
154
155  // Register iSAC SWB as send codec
156  RegisterCodec(kIsacSwb);
157  RunTestCases();
158#endif
159
160#ifdef WEBRTC_CODEC_ILBC
161  // Register iLBC as send codec
162  RegisterCodec(kIlbc);
163  RunTestCases();
164#endif
165
166#ifdef WEBRTC_CODEC_OPUS
167  // Register Opus as send codec
168  RegisterCodec(kOpus);
169  RunTestCases();
170#endif
171}
172
173// Test various configurations on VAD/DTX.
174void TestWebRtcVadDtx::RunTestCases() {
175  // #1 DTX = OFF, VAD = OFF, VADNormal
176  SetVAD(false, false, VADNormal);
177  Test(true);
178
179  // #2 DTX = ON, VAD = ON, VADAggr
180  SetVAD(true, true, VADAggr);
181  Test(false);
182
183  // #3 DTX = ON, VAD = ON, VADLowBitrate
184  SetVAD(true, true, VADLowBitrate);
185  Test(false);
186
187  // #4 DTX = ON, VAD = ON, VADVeryAggr
188  SetVAD(true, true, VADVeryAggr);
189  Test(false);
190
191  // #5 DTX = ON, VAD = ON, VADNormal
192  SetVAD(true, true, VADNormal);
193  Test(false);
194}
195
196// Set the expectation and run the test.
197void TestWebRtcVadDtx::Test(bool new_outfile) {
198  int expects[] = {-1, 1, dtx_enabled_, 0, 0};
199  if (new_outfile) {
200    output_file_num_++;
201  }
202  std::stringstream out_filename;
203  out_filename << webrtc::test::OutputPath()
204               << "testWebRtcVadDtx_outFile_"
205               << output_file_num_
206               << ".pcm";
207  Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
208      32000, 1, out_filename.str(), !new_outfile, expects);
209}
210
211void TestWebRtcVadDtx::SetVAD(bool enable_dtx, bool enable_vad,
212                              ACMVADMode vad_mode) {
213  ACMVADMode mode;
214  EXPECT_EQ(0, acm_send_->SetVAD(enable_dtx, enable_vad, vad_mode));
215  EXPECT_EQ(0, acm_send_->VAD(&dtx_enabled_, &vad_enabled_, &mode));
216
217  auto codec_param = acm_send_->SendCodec();
218  ASSERT_TRUE(codec_param);
219  if (STR_CASE_CMP(codec_param->plname, "opus") == 0) {
220    // If send codec is Opus, WebRTC VAD/DTX cannot be used.
221    enable_dtx = enable_vad = false;
222  }
223
224  EXPECT_EQ(dtx_enabled_ , enable_dtx); // DTX should be set as expected.
225
226  if (dtx_enabled_) {
227    EXPECT_TRUE(vad_enabled_); // WebRTC DTX cannot run without WebRTC VAD.
228  } else {
229    // Using no DTX should not affect setting of VAD.
230    EXPECT_EQ(enable_vad, vad_enabled_);
231  }
232}
233
234// Following is the implementation of TestOpusDtx.
235void TestOpusDtx::Perform() {
236#ifdef WEBRTC_CODEC_ISAC
237  // If we set other codec than Opus, DTX cannot be switched on.
238  RegisterCodec(kIsacWb);
239  EXPECT_EQ(-1, acm_send_->EnableOpusDtx());
240  EXPECT_EQ(0, acm_send_->DisableOpusDtx());
241#endif
242
243#ifdef WEBRTC_CODEC_OPUS
244  int expects[] = {0, 1, 0, 0, 0};
245
246  // Register Opus as send codec
247  std::string out_filename = webrtc::test::OutputPath() +
248      "testOpusDtx_outFile_mono.pcm";
249  RegisterCodec(kOpus);
250  EXPECT_EQ(0, acm_send_->DisableOpusDtx());
251
252  Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
253      32000, 1, out_filename, false, expects);
254
255  EXPECT_EQ(0, acm_send_->EnableOpusDtx());
256  expects[kEmptyFrame] = 1;
257  Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
258      32000, 1, out_filename, true, expects);
259
260  // Register stereo Opus as send codec
261  out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
262  RegisterCodec(kOpusStereo);
263  EXPECT_EQ(0, acm_send_->DisableOpusDtx());
264  expects[kEmptyFrame] = 0;
265  Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
266      32000, 2, out_filename, false, expects);
267
268  EXPECT_EQ(0, acm_send_->EnableOpusDtx());
269
270  expects[kEmptyFrame] = 1;
271  Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"),
272      32000, 2, out_filename, true, expects);
273#endif
274}
275
276}  // namespace webrtc
277