blob: 1b3af036e7807266835aa8e854cda7bbe25ff878 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2011 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/*
12 * This file includes unit tests for NetEQ.
13 */
14
15#include "webrtc/modules/audio_coding/neteq4/interface/neteq.h"
16
17#include <stdlib.h>
18#include <string.h> // memset
19
20#include <string>
21#include <vector>
22
23#include "gtest/gtest.h"
24#include "webrtc/modules/audio_coding/neteq4/test/NETEQTEST_RTPpacket.h"
25#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000026#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027#include "webrtc/typedefs.h"
28
29namespace webrtc {
30
31class RefFiles {
32 public:
33 RefFiles(const std::string& input_file, const std::string& output_file);
34 ~RefFiles();
35 template<class T> void ProcessReference(const T& test_results);
36 template<typename T, size_t n> void ProcessReference(
37 const T (&test_results)[n],
38 size_t length);
39 template<typename T, size_t n> void WriteToFile(
40 const T (&test_results)[n],
41 size_t length);
42 template<typename T, size_t n> void ReadFromFileAndCompare(
43 const T (&test_results)[n],
44 size_t length);
45 void WriteToFile(const NetEqNetworkStatistics& stats);
46 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
47 void WriteToFile(const RtcpStatistics& stats);
48 void ReadFromFileAndCompare(const RtcpStatistics& stats);
49
50 FILE* input_fp_;
51 FILE* output_fp_;
52};
53
54RefFiles::RefFiles(const std::string &input_file,
55 const std::string &output_file)
56 : input_fp_(NULL),
57 output_fp_(NULL) {
58 if (!input_file.empty()) {
59 input_fp_ = fopen(input_file.c_str(), "rb");
60 EXPECT_TRUE(input_fp_ != NULL);
61 }
62 if (!output_file.empty()) {
63 output_fp_ = fopen(output_file.c_str(), "wb");
64 EXPECT_TRUE(output_fp_ != NULL);
65 }
66}
67
68RefFiles::~RefFiles() {
69 if (input_fp_) {
70 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
71 fclose(input_fp_);
72 }
73 if (output_fp_) fclose(output_fp_);
74}
75
76template<class T>
77void RefFiles::ProcessReference(const T& test_results) {
78 WriteToFile(test_results);
79 ReadFromFileAndCompare(test_results);
80}
81
82template<typename T, size_t n>
83void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
84 WriteToFile(test_results, length);
85 ReadFromFileAndCompare(test_results, length);
86}
87
88template<typename T, size_t n>
89void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
90 if (output_fp_) {
91 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
92 }
93}
94
95template<typename T, size_t n>
96void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
97 size_t length) {
98 if (input_fp_) {
99 // Read from ref file.
100 T* ref = new T[length];
101 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
102 // Compare
103 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
104 delete [] ref;
105 }
106}
107
108void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
109 if (output_fp_) {
110 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
111 output_fp_));
112 }
113}
114
115void RefFiles::ReadFromFileAndCompare(
116 const NetEqNetworkStatistics& stats) {
117 if (input_fp_) {
118 // Read from ref file.
119 size_t stat_size = sizeof(NetEqNetworkStatistics);
120 NetEqNetworkStatistics ref_stats;
121 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
122 // Compare
123 EXPECT_EQ(0, memcmp(&stats, &ref_stats, stat_size));
124 }
125}
126
127void RefFiles::WriteToFile(const RtcpStatistics& stats) {
128 if (output_fp_) {
129 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
130 output_fp_));
131 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
132 sizeof(stats.cumulative_lost), 1, output_fp_));
133 ASSERT_EQ(1u, fwrite(&(stats.extended_max), sizeof(stats.extended_max), 1,
134 output_fp_));
135 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
136 output_fp_));
137 }
138}
139
140void RefFiles::ReadFromFileAndCompare(
141 const RtcpStatistics& stats) {
142 if (input_fp_) {
143 // Read from ref file.
144 RtcpStatistics ref_stats;
145 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
146 sizeof(ref_stats.fraction_lost), 1, input_fp_));
147 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
148 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
149 ASSERT_EQ(1u, fread(&(ref_stats.extended_max),
150 sizeof(ref_stats.extended_max), 1, input_fp_));
151 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
152 input_fp_));
153 // Compare
154 EXPECT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
155 EXPECT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
156 EXPECT_EQ(ref_stats.extended_max, stats.extended_max);
157 EXPECT_EQ(ref_stats.jitter, stats.jitter);
158 }
159}
160
161class NetEqDecodingTest : public ::testing::Test {
162 protected:
163 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
164 // constants below can be changed.
165 static const int kTimeStepMs = 10;
166 static const int kBlockSize8kHz = kTimeStepMs * 8;
167 static const int kBlockSize16kHz = kTimeStepMs * 16;
168 static const int kBlockSize32kHz = kTimeStepMs * 32;
169 static const int kMaxBlockSize = kBlockSize32kHz;
170 static const int kInitSampleRateHz = 8000;
171
172 NetEqDecodingTest();
173 virtual void SetUp();
174 virtual void TearDown();
175 void SelectDecoders(NetEqDecoder* used_codec);
176 void LoadDecoders();
177 void OpenInputFile(const std::string &rtp_file);
178 void Process(NETEQTEST_RTPpacket* rtp_ptr, int* out_len);
179 void DecodeAndCompare(const std::string &rtp_file,
180 const std::string &ref_file);
181 void DecodeAndCheckStats(const std::string &rtp_file,
182 const std::string &stat_ref_file,
183 const std::string &rtcp_ref_file);
184 static void PopulateRtpInfo(int frame_index,
185 int timestamp,
186 WebRtcRTPHeader* rtp_info);
187 static void PopulateCng(int frame_index,
188 int timestamp,
189 WebRtcRTPHeader* rtp_info,
190 uint8_t* payload,
191 int* payload_len);
192
193 NetEq* neteq_;
194 FILE* rtp_fp_;
195 unsigned int sim_clock_;
196 int16_t out_data_[kMaxBlockSize];
197 int output_sample_rate_;
198};
199
200// Allocating the static const so that it can be passed by reference.
201const int NetEqDecodingTest::kTimeStepMs;
202const int NetEqDecodingTest::kBlockSize8kHz;
203const int NetEqDecodingTest::kBlockSize16kHz;
204const int NetEqDecodingTest::kBlockSize32kHz;
205const int NetEqDecodingTest::kMaxBlockSize;
206const int NetEqDecodingTest::kInitSampleRateHz;
207
208NetEqDecodingTest::NetEqDecodingTest()
209 : neteq_(NULL),
210 rtp_fp_(NULL),
211 sim_clock_(0),
212 output_sample_rate_(kInitSampleRateHz) {
213 memset(out_data_, 0, sizeof(out_data_));
214}
215
216void NetEqDecodingTest::SetUp() {
217 neteq_ = NetEq::Create(kInitSampleRateHz);
218 ASSERT_TRUE(neteq_);
219 LoadDecoders();
220}
221
222void NetEqDecodingTest::TearDown() {
223 delete neteq_;
224 if (rtp_fp_)
225 fclose(rtp_fp_);
226}
227
228void NetEqDecodingTest::LoadDecoders() {
229 // Load PCMu.
230 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
231 // Load PCMa.
232 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000233#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000234 // Load iLBC.
235 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000236#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000237 // Load iSAC.
238 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
239 // Load iSAC SWB.
240 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000241 // Load iSAC FB.
242 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000243 // Load PCM16B nb.
244 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
245 // Load PCM16B wb.
246 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
247 // Load PCM16B swb32.
248 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
249 // Load CNG 8 kHz.
250 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
251 // Load CNG 16 kHz.
252 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
253}
254
255void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
256 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
257 ASSERT_TRUE(rtp_fp_ != NULL);
258 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
259}
260
261void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
262 // Check if time to receive.
263 while ((sim_clock_ >= rtp->time()) &&
264 (rtp->dataLen() >= 0)) {
265 if (rtp->dataLen() > 0) {
266 WebRtcRTPHeader rtpInfo;
267 rtp->parseHeader(&rtpInfo);
268 ASSERT_EQ(0, neteq_->InsertPacket(
269 rtpInfo,
270 rtp->payload(),
271 rtp->payloadLen(),
272 rtp->time() * (output_sample_rate_ / 1000)));
273 }
274 // Get next packet.
275 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
276 }
277
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000278 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279 NetEqOutputType type;
280 int num_channels;
281 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
282 &num_channels, &type));
283 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
284 (*out_len == kBlockSize16kHz) ||
285 (*out_len == kBlockSize32kHz));
286 output_sample_rate_ = *out_len / 10 * 1000;
287
288 // Increase time.
289 sim_clock_ += kTimeStepMs;
290}
291
292void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
293 const std::string &ref_file) {
294 OpenInputFile(rtp_file);
295
296 std::string ref_out_file = "";
297 if (ref_file.empty()) {
298 ref_out_file = webrtc::test::OutputPath() + "neteq_out.pcm";
299 }
300 RefFiles ref_files(ref_file, ref_out_file);
301
302 NETEQTEST_RTPpacket rtp;
303 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
304 int i = 0;
305 while (rtp.dataLen() >= 0) {
306 std::ostringstream ss;
307 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
308 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
309 int out_len;
310 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
311 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
312 }
313}
314
315void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
316 const std::string &stat_ref_file,
317 const std::string &rtcp_ref_file) {
318 OpenInputFile(rtp_file);
319 std::string stat_out_file = "";
320 if (stat_ref_file.empty()) {
321 stat_out_file = webrtc::test::OutputPath() +
322 "neteq_network_stats.dat";
323 }
324 RefFiles network_stat_files(stat_ref_file, stat_out_file);
325
326 std::string rtcp_out_file = "";
327 if (rtcp_ref_file.empty()) {
328 rtcp_out_file = webrtc::test::OutputPath() +
329 "neteq_rtcp_stats.dat";
330 }
331 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
332
333 NETEQTEST_RTPpacket rtp;
334 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
335 while (rtp.dataLen() >= 0) {
336 int out_len;
337 Process(&rtp, &out_len);
338
339 // Query the network statistics API once per second
340 if (sim_clock_ % 1000 == 0) {
341 // Process NetworkStatistics.
342 NetEqNetworkStatistics network_stats;
343 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
344 network_stat_files.ProcessReference(network_stats);
345
346 // Process RTCPstat.
347 RtcpStatistics rtcp_stats;
348 neteq_->GetRtcpStatistics(&rtcp_stats);
349 rtcp_stat_files.ProcessReference(rtcp_stats);
350 }
351 }
352}
353
354void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
355 int timestamp,
356 WebRtcRTPHeader* rtp_info) {
357 rtp_info->header.sequenceNumber = frame_index;
358 rtp_info->header.timestamp = timestamp;
359 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
360 rtp_info->header.payloadType = 94; // PCM16b WB codec.
361 rtp_info->header.markerBit = 0;
362}
363
364void NetEqDecodingTest::PopulateCng(int frame_index,
365 int timestamp,
366 WebRtcRTPHeader* rtp_info,
367 uint8_t* payload,
368 int* payload_len) {
369 rtp_info->header.sequenceNumber = frame_index;
370 rtp_info->header.timestamp = timestamp;
371 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
372 rtp_info->header.payloadType = 98; // WB CNG.
373 rtp_info->header.markerBit = 0;
374 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
375 *payload_len = 1; // Only noise level, no spectral parameters.
376}
377
kjellander@webrtc.org6eba2772013-06-04 05:46:37 +0000378#if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
379// Disabled for Windows 64-bit until webrtc:1458 is fixed.
380#define MAYBE_TestBitExactness DISABLED_TestBitExactness
381#else
382#define MAYBE_TestBitExactness TestBitExactness
383#endif
384
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000385TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(MAYBE_TestBitExactness)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386 const std::string kInputRtpFile = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000387 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000388#if defined(_MSC_VER) && (_MSC_VER >= 1700)
389 // For Visual Studio 2012 and later, we will have to use the generic reference
390 // file, rather than the windows-specific one.
391 const std::string kInputRefFile = webrtc::test::ProjectRootPath() +
392 "resources/audio_coding/neteq_universal_ref.pcm";
393#else
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 const std::string kInputRefFile =
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000395 webrtc::test::ResourcePath("audio_coding/neteq_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000396#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397 DecodeAndCompare(kInputRtpFile, kInputRefFile);
398}
399
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000400TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401 const std::string kInputRtpFile = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000402 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000403#if defined(_MSC_VER) && (_MSC_VER >= 1700)
404 // For Visual Studio 2012 and later, we will have to use the generic reference
405 // file, rather than the windows-specific one.
406 const std::string kNetworkStatRefFile = webrtc::test::ProjectRootPath() +
407 "resources/audio_coding/neteq_network_stats.dat";
408#else
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000409 const std::string kNetworkStatRefFile =
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000410 webrtc::test::ResourcePath("audio_coding/neteq_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000411#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000412 const std::string kRtcpStatRefFile =
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000413 webrtc::test::ResourcePath("audio_coding/neteq_rtcp_stats", "dat");
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000414 DecodeAndCheckStats(kInputRtpFile, kNetworkStatRefFile, kRtcpStatRefFile);
415}
416
417// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000418TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestFrameWaitingTimeStatistics)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000419 // Use fax mode to avoid time-scaling. This is to simplify the testing of
420 // packet waiting times in the packet buffer.
421 neteq_->SetPlayoutMode(kPlayoutFax);
422 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
423 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
424 size_t num_frames = 30;
425 const int kSamples = 10 * 16;
426 const int kPayloadBytes = kSamples * 2;
427 for (size_t i = 0; i < num_frames; ++i) {
428 uint16_t payload[kSamples] = {0};
429 WebRtcRTPHeader rtp_info;
430 rtp_info.header.sequenceNumber = i;
431 rtp_info.header.timestamp = i * kSamples;
432 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
433 rtp_info.header.payloadType = 94; // PCM16b WB codec.
434 rtp_info.header.markerBit = 0;
435 ASSERT_EQ(0, neteq_->InsertPacket(
436 rtp_info,
437 reinterpret_cast<uint8_t*>(payload),
438 kPayloadBytes, 0));
439 }
440 // Pull out all data.
441 for (size_t i = 0; i < num_frames; ++i) {
442 int out_len;
443 int num_channels;
444 NetEqOutputType type;
445 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
446 &num_channels, &type));
447 ASSERT_EQ(kBlockSize16kHz, out_len);
448 }
449
450 std::vector<int> waiting_times;
451 neteq_->WaitingTimes(&waiting_times);
452 int len = waiting_times.size();
453 EXPECT_EQ(num_frames, waiting_times.size());
454 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
455 // spacing (per definition), we expect the delay to increase with 10 ms for
456 // each packet.
457 for (size_t i = 0; i < waiting_times.size(); ++i) {
458 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
459 }
460
461 // Check statistics again and make sure it's been reset.
462 neteq_->WaitingTimes(&waiting_times);
463 len = waiting_times.size();
464 EXPECT_EQ(0, len);
465
466 // Process > 100 frames, and make sure that that we get statistics
467 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
468 num_frames = 110;
469 for (size_t i = 0; i < num_frames; ++i) {
470 uint16_t payload[kSamples] = {0};
471 WebRtcRTPHeader rtp_info;
472 rtp_info.header.sequenceNumber = i;
473 rtp_info.header.timestamp = i * kSamples;
474 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
475 rtp_info.header.payloadType = 94; // PCM16b WB codec.
476 rtp_info.header.markerBit = 0;
477 ASSERT_EQ(0, neteq_->InsertPacket(
478 rtp_info,
479 reinterpret_cast<uint8_t*>(payload),
480 kPayloadBytes, 0));
481 int out_len;
482 int num_channels;
483 NetEqOutputType type;
484 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
485 &num_channels, &type));
486 ASSERT_EQ(kBlockSize16kHz, out_len);
487 }
488
489 neteq_->WaitingTimes(&waiting_times);
490 EXPECT_EQ(100u, waiting_times.size());
491}
492
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000493TEST_F(NetEqDecodingTest,
494 DISABLED_ON_ANDROID(TestAverageInterArrivalTimeNegative)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000495 const int kNumFrames = 3000; // Needed for convergence.
496 int frame_index = 0;
497 const int kSamples = 10 * 16;
498 const int kPayloadBytes = kSamples * 2;
499 while (frame_index < kNumFrames) {
500 // Insert one packet each time, except every 10th time where we insert two
501 // packets at once. This will create a negative clock-drift of approx. 10%.
502 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
503 for (int n = 0; n < num_packets; ++n) {
504 uint8_t payload[kPayloadBytes] = {0};
505 WebRtcRTPHeader rtp_info;
506 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
507 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
508 ++frame_index;
509 }
510
511 // Pull out data once.
512 int out_len;
513 int num_channels;
514 NetEqOutputType type;
515 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
516 &num_channels, &type));
517 ASSERT_EQ(kBlockSize16kHz, out_len);
518 }
519
520 NetEqNetworkStatistics network_stats;
521 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
522 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
523}
524
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000525TEST_F(NetEqDecodingTest,
526 DISABLED_ON_ANDROID(TestAverageInterArrivalTimePositive)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000527 const int kNumFrames = 5000; // Needed for convergence.
528 int frame_index = 0;
529 const int kSamples = 10 * 16;
530 const int kPayloadBytes = kSamples * 2;
531 for (int i = 0; i < kNumFrames; ++i) {
532 // Insert one packet each time, except every 10th time where we don't insert
533 // any packet. This will create a positive clock-drift of approx. 11%.
534 int num_packets = (i % 10 == 9 ? 0 : 1);
535 for (int n = 0; n < num_packets; ++n) {
536 uint8_t payload[kPayloadBytes] = {0};
537 WebRtcRTPHeader rtp_info;
538 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
539 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
540 ++frame_index;
541 }
542
543 // Pull out data once.
544 int out_len;
545 int num_channels;
546 NetEqOutputType type;
547 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
548 &num_channels, &type));
549 ASSERT_EQ(kBlockSize16kHz, out_len);
550 }
551
552 NetEqNetworkStatistics network_stats;
553 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
554 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
555}
556
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000557TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithClockDrift)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558 uint16_t seq_no = 0;
559 uint32_t timestamp = 0;
560 const int kFrameSizeMs = 30;
561 const int kSamples = kFrameSizeMs * 16;
562 const int kPayloadBytes = kSamples * 2;
563 // Apply a clock drift of -25 ms / s (sender faster than receiver).
564 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
565 double next_input_time_ms = 0.0;
566 double t_ms;
567 NetEqOutputType type;
568
569 // Insert speech for 5 seconds.
570 const int kSpeechDurationMs = 5000;
571 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
572 // Each turn in this for loop is 10 ms.
573 while (next_input_time_ms <= t_ms) {
574 // Insert one 30 ms speech frame.
575 uint8_t payload[kPayloadBytes] = {0};
576 WebRtcRTPHeader rtp_info;
577 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
578 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
579 ++seq_no;
580 timestamp += kSamples;
581 next_input_time_ms += static_cast<double>(kFrameSizeMs) * kDriftFactor;
582 }
583 // Pull out data once.
584 int out_len;
585 int num_channels;
586 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
587 &num_channels, &type));
588 ASSERT_EQ(kBlockSize16kHz, out_len);
589 }
590
591 EXPECT_EQ(kOutputNormal, type);
592 int32_t delay_before = timestamp - neteq_->PlayoutTimestamp();
593
594 // Insert CNG for 1 minute (= 60000 ms).
595 const int kCngPeriodMs = 100;
596 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
597 const int kCngDurationMs = 60000;
598 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
599 // Each turn in this for loop is 10 ms.
600 while (next_input_time_ms <= t_ms) {
601 // Insert one CNG frame each 100 ms.
602 uint8_t payload[kPayloadBytes];
603 int payload_len;
604 WebRtcRTPHeader rtp_info;
605 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
606 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
607 ++seq_no;
608 timestamp += kCngPeriodSamples;
609 next_input_time_ms += static_cast<double>(kCngPeriodMs) * kDriftFactor;
610 }
611 // Pull out data once.
612 int out_len;
613 int num_channels;
614 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
615 &num_channels, &type));
616 ASSERT_EQ(kBlockSize16kHz, out_len);
617 }
618
619 EXPECT_EQ(kOutputCNG, type);
620
621 // Insert speech again until output type is speech.
622 while (type != kOutputNormal) {
623 // Each turn in this for loop is 10 ms.
624 while (next_input_time_ms <= t_ms) {
625 // Insert one 30 ms speech frame.
626 uint8_t payload[kPayloadBytes] = {0};
627 WebRtcRTPHeader rtp_info;
628 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
629 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
630 ++seq_no;
631 timestamp += kSamples;
632 next_input_time_ms += static_cast<double>(kFrameSizeMs) * kDriftFactor;
633 }
634 // Pull out data once.
635 int out_len;
636 int num_channels;
637 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
638 &num_channels, &type));
639 ASSERT_EQ(kBlockSize16kHz, out_len);
640 // Increase clock.
641 t_ms += 10;
642 }
643
644 int32_t delay_after = timestamp - neteq_->PlayoutTimestamp();
645 // Compare delay before and after, and make sure it differs less than 20 ms.
646 EXPECT_LE(delay_after, delay_before + 20 * 16);
647 EXPECT_GE(delay_after, delay_before - 20 * 16);
648}
649
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000650TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(UnknownPayloadType)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000651 const int kPayloadBytes = 100;
652 uint8_t payload[kPayloadBytes] = {0};
653 WebRtcRTPHeader rtp_info;
654 PopulateRtpInfo(0, 0, &rtp_info);
655 rtp_info.header.payloadType = 1; // Not registered as a decoder.
656 EXPECT_EQ(NetEq::kFail,
657 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
658 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
659}
660
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000661TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000662 const int kPayloadBytes = 100;
663 uint8_t payload[kPayloadBytes] = {0};
664 WebRtcRTPHeader rtp_info;
665 PopulateRtpInfo(0, 0, &rtp_info);
666 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
667 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
668 NetEqOutputType type;
669 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
670 // to GetAudio.
671 for (int i = 0; i < kMaxBlockSize; ++i) {
672 out_data_[i] = 1;
673 }
674 int num_channels;
675 int samples_per_channel;
676 EXPECT_EQ(NetEq::kFail,
677 neteq_->GetAudio(kMaxBlockSize, out_data_,
678 &samples_per_channel, &num_channels, &type));
679 // Verify that there is a decoder error to check.
680 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
681 // Code 6730 is an iSAC error code.
682 EXPECT_EQ(6730, neteq_->LastDecoderError());
683 // Verify that the first 160 samples are set to 0, and that the remaining
684 // samples are left unmodified.
685 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
686 for (int i = 0; i < kExpectedOutputLength; ++i) {
687 std::ostringstream ss;
688 ss << "i = " << i;
689 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
690 EXPECT_EQ(0, out_data_[i]);
691 }
692 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
693 std::ostringstream ss;
694 ss << "i = " << i;
695 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
696 EXPECT_EQ(1, out_data_[i]);
697 }
698}
699
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000700TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(GetAudioBeforeInsertPacket)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000701 NetEqOutputType type;
702 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
703 // to GetAudio.
704 for (int i = 0; i < kMaxBlockSize; ++i) {
705 out_data_[i] = 1;
706 }
707 int num_channels;
708 int samples_per_channel;
709 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
710 &samples_per_channel,
711 &num_channels, &type));
712 // Verify that the first block of samples is set to 0.
713 static const int kExpectedOutputLength =
714 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
715 for (int i = 0; i < kExpectedOutputLength; ++i) {
716 std::ostringstream ss;
717 ss << "i = " << i;
718 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
719 EXPECT_EQ(0, out_data_[i]);
720 }
721}
722} // namespace