blob: 40d52f5f2c9e60aa98dcda28301545749340106f [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
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000020#include <cmath>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000021#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022#include <string>
23#include <vector>
24
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000025#include "gflags/gflags.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026#include "gtest/gtest.h"
27#include "webrtc/modules/audio_coding/neteq4/test/NETEQTEST_RTPpacket.h"
turaj@webrtc.orgff43c852013-09-25 00:07:27 +000028#include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029#include "webrtc/test/testsupport/fileutils.h"
henrike@webrtc.orga950300b2013-07-08 18:53:54 +000030#include "webrtc/test/testsupport/gtest_disable.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031#include "webrtc/typedefs.h"
32
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000033DEFINE_bool(gen_ref, false, "Generate reference files.");
34
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035namespace webrtc {
36
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000037static bool IsAllZero(const int16_t* buf, int buf_length) {
38 bool all_zero = true;
39 for (int n = 0; n < buf_length && all_zero; ++n)
40 all_zero = buf[n] == 0;
41 return all_zero;
42}
43
44static bool IsAllNonZero(const int16_t* buf, int buf_length) {
45 bool all_non_zero = true;
46 for (int n = 0; n < buf_length && all_non_zero; ++n)
47 all_non_zero = buf[n] != 0;
48 return all_non_zero;
49}
50
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051class RefFiles {
52 public:
53 RefFiles(const std::string& input_file, const std::string& output_file);
54 ~RefFiles();
55 template<class T> void ProcessReference(const T& test_results);
56 template<typename T, size_t n> void ProcessReference(
57 const T (&test_results)[n],
58 size_t length);
59 template<typename T, size_t n> void WriteToFile(
60 const T (&test_results)[n],
61 size_t length);
62 template<typename T, size_t n> void ReadFromFileAndCompare(
63 const T (&test_results)[n],
64 size_t length);
65 void WriteToFile(const NetEqNetworkStatistics& stats);
66 void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats);
67 void WriteToFile(const RtcpStatistics& stats);
68 void ReadFromFileAndCompare(const RtcpStatistics& stats);
69
70 FILE* input_fp_;
71 FILE* output_fp_;
72};
73
74RefFiles::RefFiles(const std::string &input_file,
75 const std::string &output_file)
76 : input_fp_(NULL),
77 output_fp_(NULL) {
78 if (!input_file.empty()) {
79 input_fp_ = fopen(input_file.c_str(), "rb");
80 EXPECT_TRUE(input_fp_ != NULL);
81 }
82 if (!output_file.empty()) {
83 output_fp_ = fopen(output_file.c_str(), "wb");
84 EXPECT_TRUE(output_fp_ != NULL);
85 }
86}
87
88RefFiles::~RefFiles() {
89 if (input_fp_) {
90 EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end.
91 fclose(input_fp_);
92 }
93 if (output_fp_) fclose(output_fp_);
94}
95
96template<class T>
97void RefFiles::ProcessReference(const T& test_results) {
98 WriteToFile(test_results);
99 ReadFromFileAndCompare(test_results);
100}
101
102template<typename T, size_t n>
103void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) {
104 WriteToFile(test_results, length);
105 ReadFromFileAndCompare(test_results, length);
106}
107
108template<typename T, size_t n>
109void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) {
110 if (output_fp_) {
111 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
112 }
113}
114
115template<typename T, size_t n>
116void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n],
117 size_t length) {
118 if (input_fp_) {
119 // Read from ref file.
120 T* ref = new T[length];
121 ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_));
122 // Compare
123 ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length));
124 delete [] ref;
125 }
126}
127
128void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) {
129 if (output_fp_) {
130 ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1,
131 output_fp_));
132 }
133}
134
135void RefFiles::ReadFromFileAndCompare(
136 const NetEqNetworkStatistics& stats) {
137 if (input_fp_) {
138 // Read from ref file.
139 size_t stat_size = sizeof(NetEqNetworkStatistics);
140 NetEqNetworkStatistics ref_stats;
141 ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_));
142 // Compare
143 EXPECT_EQ(0, memcmp(&stats, &ref_stats, stat_size));
144 }
145}
146
147void RefFiles::WriteToFile(const RtcpStatistics& stats) {
148 if (output_fp_) {
149 ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1,
150 output_fp_));
151 ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost),
152 sizeof(stats.cumulative_lost), 1, output_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000153 ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number),
154 sizeof(stats.extended_max_sequence_number), 1,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 output_fp_));
156 ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1,
157 output_fp_));
158 }
159}
160
161void RefFiles::ReadFromFileAndCompare(
162 const RtcpStatistics& stats) {
163 if (input_fp_) {
164 // Read from ref file.
165 RtcpStatistics ref_stats;
166 ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost),
167 sizeof(ref_stats.fraction_lost), 1, input_fp_));
168 ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost),
169 sizeof(ref_stats.cumulative_lost), 1, input_fp_));
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000170 ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number),
171 sizeof(ref_stats.extended_max_sequence_number), 1,
172 input_fp_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173 ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1,
174 input_fp_));
175 // Compare
176 EXPECT_EQ(ref_stats.fraction_lost, stats.fraction_lost);
177 EXPECT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost);
sprang@webrtc.orgfe5d36b2013-10-28 09:21:07 +0000178 EXPECT_EQ(ref_stats.extended_max_sequence_number,
179 stats.extended_max_sequence_number);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 EXPECT_EQ(ref_stats.jitter, stats.jitter);
181 }
182}
183
184class NetEqDecodingTest : public ::testing::Test {
185 protected:
186 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
187 // constants below can be changed.
188 static const int kTimeStepMs = 10;
189 static const int kBlockSize8kHz = kTimeStepMs * 8;
190 static const int kBlockSize16kHz = kTimeStepMs * 16;
191 static const int kBlockSize32kHz = kTimeStepMs * 32;
192 static const int kMaxBlockSize = kBlockSize32kHz;
193 static const int kInitSampleRateHz = 8000;
194
195 NetEqDecodingTest();
196 virtual void SetUp();
197 virtual void TearDown();
198 void SelectDecoders(NetEqDecoder* used_codec);
199 void LoadDecoders();
200 void OpenInputFile(const std::string &rtp_file);
201 void Process(NETEQTEST_RTPpacket* rtp_ptr, int* out_len);
202 void DecodeAndCompare(const std::string &rtp_file,
203 const std::string &ref_file);
204 void DecodeAndCheckStats(const std::string &rtp_file,
205 const std::string &stat_ref_file,
206 const std::string &rtcp_ref_file);
207 static void PopulateRtpInfo(int frame_index,
208 int timestamp,
209 WebRtcRTPHeader* rtp_info);
210 static void PopulateCng(int frame_index,
211 int timestamp,
212 WebRtcRTPHeader* rtp_info,
213 uint8_t* payload,
214 int* payload_len);
215
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000216 void CheckBgnOff(int sampling_rate, NetEqBackgroundNoiseMode bgn_mode);
217
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000218 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
219 const std::set<uint16_t>& drop_seq_numbers,
220 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
221
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000222 NetEq* neteq_;
223 FILE* rtp_fp_;
224 unsigned int sim_clock_;
225 int16_t out_data_[kMaxBlockSize];
226 int output_sample_rate_;
227};
228
229// Allocating the static const so that it can be passed by reference.
230const int NetEqDecodingTest::kTimeStepMs;
231const int NetEqDecodingTest::kBlockSize8kHz;
232const int NetEqDecodingTest::kBlockSize16kHz;
233const int NetEqDecodingTest::kBlockSize32kHz;
234const int NetEqDecodingTest::kMaxBlockSize;
235const int NetEqDecodingTest::kInitSampleRateHz;
236
237NetEqDecodingTest::NetEqDecodingTest()
238 : neteq_(NULL),
239 rtp_fp_(NULL),
240 sim_clock_(0),
241 output_sample_rate_(kInitSampleRateHz) {
242 memset(out_data_, 0, sizeof(out_data_));
243}
244
245void NetEqDecodingTest::SetUp() {
246 neteq_ = NetEq::Create(kInitSampleRateHz);
247 ASSERT_TRUE(neteq_);
248 LoadDecoders();
249}
250
251void NetEqDecodingTest::TearDown() {
252 delete neteq_;
253 if (rtp_fp_)
254 fclose(rtp_fp_);
255}
256
257void NetEqDecodingTest::LoadDecoders() {
258 // Load PCMu.
259 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0));
260 // Load PCMa.
261 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000262#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 // Load iLBC.
264 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102));
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000265#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 // Load iSAC.
267 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000268#ifndef WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269 // Load iSAC SWB.
270 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000271 // Load iSAC FB.
272 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
turaj@webrtc.org5272eb82013-11-23 00:11:32 +0000273#endif // WEBRTC_ANDROID
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274 // Load PCM16B nb.
275 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
276 // Load PCM16B wb.
277 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
278 // Load PCM16B swb32.
279 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
280 // Load CNG 8 kHz.
281 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
282 // Load CNG 16 kHz.
283 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
284}
285
286void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
287 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
288 ASSERT_TRUE(rtp_fp_ != NULL);
289 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
290}
291
292void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
293 // Check if time to receive.
294 while ((sim_clock_ >= rtp->time()) &&
295 (rtp->dataLen() >= 0)) {
296 if (rtp->dataLen() > 0) {
297 WebRtcRTPHeader rtpInfo;
298 rtp->parseHeader(&rtpInfo);
299 ASSERT_EQ(0, neteq_->InsertPacket(
300 rtpInfo,
301 rtp->payload(),
302 rtp->payloadLen(),
303 rtp->time() * (output_sample_rate_ / 1000)));
304 }
305 // Get next packet.
306 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
307 }
308
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000309 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310 NetEqOutputType type;
311 int num_channels;
312 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
313 &num_channels, &type));
314 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
315 (*out_len == kBlockSize16kHz) ||
316 (*out_len == kBlockSize32kHz));
317 output_sample_rate_ = *out_len / 10 * 1000;
318
319 // Increase time.
320 sim_clock_ += kTimeStepMs;
321}
322
323void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
324 const std::string &ref_file) {
325 OpenInputFile(rtp_file);
326
327 std::string ref_out_file = "";
328 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000329 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330 }
331 RefFiles ref_files(ref_file, ref_out_file);
332
333 NETEQTEST_RTPpacket rtp;
334 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
335 int i = 0;
336 while (rtp.dataLen() >= 0) {
337 std::ostringstream ss;
338 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
339 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000340 int out_len = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
342 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
343 }
344}
345
346void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
347 const std::string &stat_ref_file,
348 const std::string &rtcp_ref_file) {
349 OpenInputFile(rtp_file);
350 std::string stat_out_file = "";
351 if (stat_ref_file.empty()) {
352 stat_out_file = webrtc::test::OutputPath() +
353 "neteq_network_stats.dat";
354 }
355 RefFiles network_stat_files(stat_ref_file, stat_out_file);
356
357 std::string rtcp_out_file = "";
358 if (rtcp_ref_file.empty()) {
359 rtcp_out_file = webrtc::test::OutputPath() +
360 "neteq_rtcp_stats.dat";
361 }
362 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
363
364 NETEQTEST_RTPpacket rtp;
365 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
366 while (rtp.dataLen() >= 0) {
367 int out_len;
368 Process(&rtp, &out_len);
369
370 // Query the network statistics API once per second
371 if (sim_clock_ % 1000 == 0) {
372 // Process NetworkStatistics.
373 NetEqNetworkStatistics network_stats;
374 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
375 network_stat_files.ProcessReference(network_stats);
376
377 // Process RTCPstat.
378 RtcpStatistics rtcp_stats;
379 neteq_->GetRtcpStatistics(&rtcp_stats);
380 rtcp_stat_files.ProcessReference(rtcp_stats);
381 }
382 }
383}
384
385void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
386 int timestamp,
387 WebRtcRTPHeader* rtp_info) {
388 rtp_info->header.sequenceNumber = frame_index;
389 rtp_info->header.timestamp = timestamp;
390 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
391 rtp_info->header.payloadType = 94; // PCM16b WB codec.
392 rtp_info->header.markerBit = 0;
393}
394
395void NetEqDecodingTest::PopulateCng(int frame_index,
396 int timestamp,
397 WebRtcRTPHeader* rtp_info,
398 uint8_t* payload,
399 int* payload_len) {
400 rtp_info->header.sequenceNumber = frame_index;
401 rtp_info->header.timestamp = timestamp;
402 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
403 rtp_info->header.payloadType = 98; // WB CNG.
404 rtp_info->header.markerBit = 0;
405 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
406 *payload_len = 1; // Only noise level, no spectral parameters.
407}
408
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000409void NetEqDecodingTest::CheckBgnOff(int sampling_rate_hz,
410 NetEqBackgroundNoiseMode bgn_mode) {
411 int expected_samples_per_channel = 0;
412 uint8_t payload_type = 0xFF; // Invalid.
413 if (sampling_rate_hz == 8000) {
414 expected_samples_per_channel = kBlockSize8kHz;
415 payload_type = 93; // PCM 16, 8 kHz.
416 } else if (sampling_rate_hz == 16000) {
417 expected_samples_per_channel = kBlockSize16kHz;
418 payload_type = 94; // PCM 16, 16 kHZ.
419 } else if (sampling_rate_hz == 32000) {
420 expected_samples_per_channel = kBlockSize32kHz;
421 payload_type = 95; // PCM 16, 32 kHz.
422 } else {
423 ASSERT_TRUE(false); // Unsupported test case.
424 }
425
426 NetEqOutputType type;
427 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
428 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
429
430 // Payload of 10 ms of PCM16 32 kHz.
431 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
432
433 // Random payload.
434 for (int n = 0; n < expected_samples_per_channel; ++n) {
435 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
436 }
437 int enc_len_bytes = WebRtcPcm16b_EncodeW16(
438 input, expected_samples_per_channel, reinterpret_cast<int16_t*>(payload));
439 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
440
441 WebRtcRTPHeader rtp_info;
442 PopulateRtpInfo(0, 0, &rtp_info);
443 rtp_info.header.payloadType = payload_type;
444
445 int number_channels = 0;
446 int samples_per_channel = 0;
447
448 uint32_t receive_timestamp = 0;
449 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
450 number_channels = 0;
451 samples_per_channel = 0;
452 ASSERT_EQ(0, neteq_->InsertPacket(
453 rtp_info, payload, enc_len_bytes, receive_timestamp));
454 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
455 &number_channels, &type));
456 ASSERT_EQ(1, number_channels);
457 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
458 ASSERT_EQ(kOutputNormal, type);
459
460 // Next packet.
461 rtp_info.header.timestamp += expected_samples_per_channel;
462 rtp_info.header.sequenceNumber++;
463 receive_timestamp += expected_samples_per_channel;
464 }
465
466 number_channels = 0;
467 samples_per_channel = 0;
468
469 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull one
470 // frame without checking speech-type. This is the first frame pulled without
471 // inserting any packet, and might not be labeled as PCL.
472 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
473 &number_channels, &type));
474 ASSERT_EQ(1, number_channels);
475 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
476
477 // To be able to test the fading of background noise we need at lease to pull
478 // 610 frames.
479 const int kFadingThreshold = 610;
480
481 // Test several CNG-to-PLC packet for the expected behavior. The number 20 is
482 // arbitrary, but sufficiently large to test enough number of frames.
483 const int kNumPlcToCngTestFrames = 20;
484 bool plc_to_cng = false;
485 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
486 number_channels = 0;
487 samples_per_channel = 0;
488 memset(output, 1, sizeof(output)); // Set to non-zero.
489 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
490 &number_channels, &type));
491 ASSERT_EQ(1, number_channels);
492 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
493 if (type == kOutputPLCtoCNG) {
494 plc_to_cng = true;
495 double sum_squared = 0;
496 for (int k = 0; k < number_channels * samples_per_channel; ++k)
497 sum_squared += output[k] * output[k];
498 if (bgn_mode == kBgnOn) {
499 EXPECT_NE(0, sum_squared);
500 } else if (bgn_mode == kBgnOff || n > kFadingThreshold) {
501 EXPECT_EQ(0, sum_squared);
502 }
503 } else {
504 EXPECT_EQ(kOutputPLC, type);
505 }
506 }
507 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
508}
509
kjellander@webrtc.org6eba2772013-06-04 05:46:37 +0000510#if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
511// Disabled for Windows 64-bit until webrtc:1458 is fixed.
512#define MAYBE_TestBitExactness DISABLED_TestBitExactness
513#else
514#define MAYBE_TestBitExactness TestBitExactness
515#endif
516
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000517TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(MAYBE_TestBitExactness)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000518 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000519 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000520#if defined(_MSC_VER) && (_MSC_VER >= 1700)
521 // For Visual Studio 2012 and later, we will have to use the generic reference
522 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000523 const std::string input_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000524 "resources/audio_coding/neteq4_universal_ref.pcm";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000525#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000526 const std::string input_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000527 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000528#endif
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000529
530 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000531 DecodeAndCompare(input_rtp_file, "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000532 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000533 DecodeAndCompare(input_rtp_file, input_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000534 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000535}
536
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000537TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000538 const std::string input_rtp_file = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000539 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000540#if defined(_MSC_VER) && (_MSC_VER >= 1700)
541 // For Visual Studio 2012 and later, we will have to use the generic reference
542 // file, rather than the windows-specific one.
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000543 const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000544 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000545#else
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000546 const std::string network_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000547 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000548#endif
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000549 const std::string rtcp_stat_ref_file =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000550 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
551 if (FLAGS_gen_ref) {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000552 DecodeAndCheckStats(input_rtp_file, "", "");
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000553 } else {
andrew@webrtc.orgf6a638e2014-02-04 01:31:28 +0000554 DecodeAndCheckStats(input_rtp_file, network_stat_ref_file,
555 rtcp_stat_ref_file);
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000556 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000557}
558
559// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000560TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestFrameWaitingTimeStatistics)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000561 // Use fax mode to avoid time-scaling. This is to simplify the testing of
562 // packet waiting times in the packet buffer.
563 neteq_->SetPlayoutMode(kPlayoutFax);
564 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
565 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
566 size_t num_frames = 30;
567 const int kSamples = 10 * 16;
568 const int kPayloadBytes = kSamples * 2;
569 for (size_t i = 0; i < num_frames; ++i) {
570 uint16_t payload[kSamples] = {0};
571 WebRtcRTPHeader rtp_info;
572 rtp_info.header.sequenceNumber = i;
573 rtp_info.header.timestamp = i * kSamples;
574 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
575 rtp_info.header.payloadType = 94; // PCM16b WB codec.
576 rtp_info.header.markerBit = 0;
577 ASSERT_EQ(0, neteq_->InsertPacket(
578 rtp_info,
579 reinterpret_cast<uint8_t*>(payload),
580 kPayloadBytes, 0));
581 }
582 // Pull out all data.
583 for (size_t i = 0; i < num_frames; ++i) {
584 int out_len;
585 int num_channels;
586 NetEqOutputType type;
587 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
588 &num_channels, &type));
589 ASSERT_EQ(kBlockSize16kHz, out_len);
590 }
591
592 std::vector<int> waiting_times;
593 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000594 EXPECT_EQ(num_frames, waiting_times.size());
595 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
596 // spacing (per definition), we expect the delay to increase with 10 ms for
597 // each packet.
598 for (size_t i = 0; i < waiting_times.size(); ++i) {
599 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
600 }
601
602 // Check statistics again and make sure it's been reset.
603 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000604 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605 EXPECT_EQ(0, len);
606
607 // Process > 100 frames, and make sure that that we get statistics
608 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
609 num_frames = 110;
610 for (size_t i = 0; i < num_frames; ++i) {
611 uint16_t payload[kSamples] = {0};
612 WebRtcRTPHeader rtp_info;
613 rtp_info.header.sequenceNumber = i;
614 rtp_info.header.timestamp = i * kSamples;
615 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
616 rtp_info.header.payloadType = 94; // PCM16b WB codec.
617 rtp_info.header.markerBit = 0;
618 ASSERT_EQ(0, neteq_->InsertPacket(
619 rtp_info,
620 reinterpret_cast<uint8_t*>(payload),
621 kPayloadBytes, 0));
622 int out_len;
623 int num_channels;
624 NetEqOutputType type;
625 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
626 &num_channels, &type));
627 ASSERT_EQ(kBlockSize16kHz, out_len);
628 }
629
630 neteq_->WaitingTimes(&waiting_times);
631 EXPECT_EQ(100u, waiting_times.size());
632}
633
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000634TEST_F(NetEqDecodingTest,
635 DISABLED_ON_ANDROID(TestAverageInterArrivalTimeNegative)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000636 const int kNumFrames = 3000; // Needed for convergence.
637 int frame_index = 0;
638 const int kSamples = 10 * 16;
639 const int kPayloadBytes = kSamples * 2;
640 while (frame_index < kNumFrames) {
641 // Insert one packet each time, except every 10th time where we insert two
642 // packets at once. This will create a negative clock-drift of approx. 10%.
643 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
644 for (int n = 0; n < num_packets; ++n) {
645 uint8_t payload[kPayloadBytes] = {0};
646 WebRtcRTPHeader rtp_info;
647 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
648 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
649 ++frame_index;
650 }
651
652 // Pull out data once.
653 int out_len;
654 int num_channels;
655 NetEqOutputType type;
656 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
657 &num_channels, &type));
658 ASSERT_EQ(kBlockSize16kHz, out_len);
659 }
660
661 NetEqNetworkStatistics network_stats;
662 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
663 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
664}
665
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000666TEST_F(NetEqDecodingTest,
667 DISABLED_ON_ANDROID(TestAverageInterArrivalTimePositive)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000668 const int kNumFrames = 5000; // Needed for convergence.
669 int frame_index = 0;
670 const int kSamples = 10 * 16;
671 const int kPayloadBytes = kSamples * 2;
672 for (int i = 0; i < kNumFrames; ++i) {
673 // Insert one packet each time, except every 10th time where we don't insert
674 // any packet. This will create a positive clock-drift of approx. 11%.
675 int num_packets = (i % 10 == 9 ? 0 : 1);
676 for (int n = 0; n < num_packets; ++n) {
677 uint8_t payload[kPayloadBytes] = {0};
678 WebRtcRTPHeader rtp_info;
679 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
680 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
681 ++frame_index;
682 }
683
684 // Pull out data once.
685 int out_len;
686 int num_channels;
687 NetEqOutputType type;
688 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
689 &num_channels, &type));
690 ASSERT_EQ(kBlockSize16kHz, out_len);
691 }
692
693 NetEqNetworkStatistics network_stats;
694 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
695 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
696}
697
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000698TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithClockDrift)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000699 uint16_t seq_no = 0;
700 uint32_t timestamp = 0;
701 const int kFrameSizeMs = 30;
702 const int kSamples = kFrameSizeMs * 16;
703 const int kPayloadBytes = kSamples * 2;
704 // Apply a clock drift of -25 ms / s (sender faster than receiver).
705 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
706 double next_input_time_ms = 0.0;
707 double t_ms;
708 NetEqOutputType type;
709
710 // Insert speech for 5 seconds.
711 const int kSpeechDurationMs = 5000;
712 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
713 // Each turn in this for loop is 10 ms.
714 while (next_input_time_ms <= t_ms) {
715 // Insert one 30 ms speech frame.
716 uint8_t payload[kPayloadBytes] = {0};
717 WebRtcRTPHeader rtp_info;
718 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
719 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
720 ++seq_no;
721 timestamp += kSamples;
722 next_input_time_ms += static_cast<double>(kFrameSizeMs) * kDriftFactor;
723 }
724 // Pull out data once.
725 int out_len;
726 int num_channels;
727 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
728 &num_channels, &type));
729 ASSERT_EQ(kBlockSize16kHz, out_len);
730 }
731
732 EXPECT_EQ(kOutputNormal, type);
733 int32_t delay_before = timestamp - neteq_->PlayoutTimestamp();
734
735 // Insert CNG for 1 minute (= 60000 ms).
736 const int kCngPeriodMs = 100;
737 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
738 const int kCngDurationMs = 60000;
739 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
740 // Each turn in this for loop is 10 ms.
741 while (next_input_time_ms <= t_ms) {
742 // Insert one CNG frame each 100 ms.
743 uint8_t payload[kPayloadBytes];
744 int payload_len;
745 WebRtcRTPHeader rtp_info;
746 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
747 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
748 ++seq_no;
749 timestamp += kCngPeriodSamples;
750 next_input_time_ms += static_cast<double>(kCngPeriodMs) * kDriftFactor;
751 }
752 // Pull out data once.
753 int out_len;
754 int num_channels;
755 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
756 &num_channels, &type));
757 ASSERT_EQ(kBlockSize16kHz, out_len);
758 }
759
760 EXPECT_EQ(kOutputCNG, type);
761
762 // Insert speech again until output type is speech.
763 while (type != kOutputNormal) {
764 // Each turn in this for loop is 10 ms.
765 while (next_input_time_ms <= t_ms) {
766 // Insert one 30 ms speech frame.
767 uint8_t payload[kPayloadBytes] = {0};
768 WebRtcRTPHeader rtp_info;
769 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
770 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
771 ++seq_no;
772 timestamp += kSamples;
773 next_input_time_ms += static_cast<double>(kFrameSizeMs) * kDriftFactor;
774 }
775 // Pull out data once.
776 int out_len;
777 int num_channels;
778 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
779 &num_channels, &type));
780 ASSERT_EQ(kBlockSize16kHz, out_len);
781 // Increase clock.
782 t_ms += 10;
783 }
784
785 int32_t delay_after = timestamp - neteq_->PlayoutTimestamp();
786 // Compare delay before and after, and make sure it differs less than 20 ms.
787 EXPECT_LE(delay_after, delay_before + 20 * 16);
788 EXPECT_GE(delay_after, delay_before - 20 * 16);
789}
790
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000791TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(UnknownPayloadType)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000792 const int kPayloadBytes = 100;
793 uint8_t payload[kPayloadBytes] = {0};
794 WebRtcRTPHeader rtp_info;
795 PopulateRtpInfo(0, 0, &rtp_info);
796 rtp_info.header.payloadType = 1; // Not registered as a decoder.
797 EXPECT_EQ(NetEq::kFail,
798 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
799 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
800}
801
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000802TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(OversizePacket)) {
803 // Payload size is greater than packet buffer size
804 const int kPayloadBytes = NetEq::kMaxBytesInBuffer + 1;
805 uint8_t payload[kPayloadBytes] = {0};
806 WebRtcRTPHeader rtp_info;
807 PopulateRtpInfo(0, 0, &rtp_info);
808 rtp_info.header.payloadType = 103; // iSAC, no packet splitting.
809 EXPECT_EQ(NetEq::kFail,
810 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
811 EXPECT_EQ(NetEq::kOversizePacket, neteq_->LastError());
812}
813
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000814TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000815 const int kPayloadBytes = 100;
816 uint8_t payload[kPayloadBytes] = {0};
817 WebRtcRTPHeader rtp_info;
818 PopulateRtpInfo(0, 0, &rtp_info);
819 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
820 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
821 NetEqOutputType type;
822 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
823 // to GetAudio.
824 for (int i = 0; i < kMaxBlockSize; ++i) {
825 out_data_[i] = 1;
826 }
827 int num_channels;
828 int samples_per_channel;
829 EXPECT_EQ(NetEq::kFail,
830 neteq_->GetAudio(kMaxBlockSize, out_data_,
831 &samples_per_channel, &num_channels, &type));
832 // Verify that there is a decoder error to check.
833 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
834 // Code 6730 is an iSAC error code.
835 EXPECT_EQ(6730, neteq_->LastDecoderError());
836 // Verify that the first 160 samples are set to 0, and that the remaining
837 // samples are left unmodified.
838 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
839 for (int i = 0; i < kExpectedOutputLength; ++i) {
840 std::ostringstream ss;
841 ss << "i = " << i;
842 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
843 EXPECT_EQ(0, out_data_[i]);
844 }
845 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
846 std::ostringstream ss;
847 ss << "i = " << i;
848 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
849 EXPECT_EQ(1, out_data_[i]);
850 }
851}
852
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000853TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(GetAudioBeforeInsertPacket)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000854 NetEqOutputType type;
855 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
856 // to GetAudio.
857 for (int i = 0; i < kMaxBlockSize; ++i) {
858 out_data_[i] = 1;
859 }
860 int num_channels;
861 int samples_per_channel;
862 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
863 &samples_per_channel,
864 &num_channels, &type));
865 // Verify that the first block of samples is set to 0.
866 static const int kExpectedOutputLength =
867 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
868 for (int i = 0; i < kExpectedOutputLength; ++i) {
869 std::ostringstream ss;
870 ss << "i = " << i;
871 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
872 EXPECT_EQ(0, out_data_[i]);
873 }
874}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000875
turaj@webrtc.org3fdeddb2013-09-25 22:19:22 +0000876TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(BackgroundNoise)) {
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000877 neteq_->SetBackgroundNoiseMode(kBgnOn);
878 CheckBgnOff(8000, kBgnOn);
879 CheckBgnOff(16000, kBgnOn);
880 CheckBgnOff(32000, kBgnOn);
881 EXPECT_EQ(kBgnOn, neteq_->BackgroundNoiseMode());
882
883 neteq_->SetBackgroundNoiseMode(kBgnOff);
884 CheckBgnOff(8000, kBgnOff);
885 CheckBgnOff(16000, kBgnOff);
886 CheckBgnOff(32000, kBgnOff);
887 EXPECT_EQ(kBgnOff, neteq_->BackgroundNoiseMode());
888
889 neteq_->SetBackgroundNoiseMode(kBgnFade);
890 CheckBgnOff(8000, kBgnFade);
891 CheckBgnOff(16000, kBgnFade);
892 CheckBgnOff(32000, kBgnFade);
893 EXPECT_EQ(kBgnFade, neteq_->BackgroundNoiseMode());
894}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000895
896TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketInsert)) {
897 WebRtcRTPHeader rtp_info;
898 uint32_t receive_timestamp = 0;
899 // For the readability use the following payloads instead of the defaults of
900 // this test.
901 uint8_t kPcm16WbPayloadType = 1;
902 uint8_t kCngNbPayloadType = 2;
903 uint8_t kCngWbPayloadType = 3;
904 uint8_t kCngSwb32PayloadType = 4;
905 uint8_t kCngSwb48PayloadType = 5;
906 uint8_t kAvtPayloadType = 6;
907 uint8_t kRedPayloadType = 7;
908 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
909
910 // Register decoders.
911 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
912 kPcm16WbPayloadType));
913 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
914 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
915 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
916 kCngSwb32PayloadType));
917 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
918 kCngSwb48PayloadType));
919 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
920 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
921 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
922
923 PopulateRtpInfo(0, 0, &rtp_info);
924 rtp_info.header.payloadType = kPcm16WbPayloadType;
925
926 // The first packet injected cannot be sync-packet.
927 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
928
929 // Payload length of 10 ms PCM16 16 kHz.
930 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
931 uint8_t payload[kPayloadBytes] = {0};
932 ASSERT_EQ(0, neteq_->InsertPacket(
933 rtp_info, payload, kPayloadBytes, receive_timestamp));
934
935 // Next packet. Last packet contained 10 ms audio.
936 rtp_info.header.sequenceNumber++;
937 rtp_info.header.timestamp += kBlockSize16kHz;
938 receive_timestamp += kBlockSize16kHz;
939
940 // Unacceptable payload types CNG, AVT (DTMF), RED.
941 rtp_info.header.payloadType = kCngNbPayloadType;
942 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
943
944 rtp_info.header.payloadType = kCngWbPayloadType;
945 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
946
947 rtp_info.header.payloadType = kCngSwb32PayloadType;
948 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
949
950 rtp_info.header.payloadType = kCngSwb48PayloadType;
951 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
952
953 rtp_info.header.payloadType = kAvtPayloadType;
954 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
955
956 rtp_info.header.payloadType = kRedPayloadType;
957 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
958
959 // Change of codec cannot be initiated with a sync packet.
960 rtp_info.header.payloadType = kIsacPayloadType;
961 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
962
963 // Change of SSRC is not allowed with a sync packet.
964 rtp_info.header.payloadType = kPcm16WbPayloadType;
965 ++rtp_info.header.ssrc;
966 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
967
968 --rtp_info.header.ssrc;
969 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
970}
971
972// First insert several noise like packets, then sync-packets. Decoding all
973// packets should not produce error, statistics should not show any packet loss
974// and sync-packets should decode to zero.
975TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketDecode)) {
976 WebRtcRTPHeader rtp_info;
977 PopulateRtpInfo(0, 0, &rtp_info);
978 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
979 uint8_t payload[kPayloadBytes];
980 int16_t decoded[kBlockSize16kHz];
981 for (int n = 0; n < kPayloadBytes; ++n) {
982 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
983 }
984 // Insert some packets which decode to noise. We are not interested in
985 // actual decoded values.
986 NetEqOutputType output_type;
987 int num_channels;
988 int samples_per_channel;
989 uint32_t receive_timestamp = 0;
990 int delay_samples = 0;
991 for (int n = 0; n < 100; ++n) {
992 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
993 receive_timestamp));
994 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
995 &samples_per_channel, &num_channels,
996 &output_type));
997 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
998 ASSERT_EQ(1, num_channels);
999
1000 // Even if there is RTP packet in NetEq's buffer, the first frame pulled
1001 // from NetEq starts with few zero samples. Here we measure this delay.
1002 if (n == 0) {
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001003 while (decoded[delay_samples] == 0) delay_samples++;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001004 }
1005 rtp_info.header.sequenceNumber++;
1006 rtp_info.header.timestamp += kBlockSize16kHz;
1007 receive_timestamp += kBlockSize16kHz;
1008 }
1009 const int kNumSyncPackets = 10;
1010 // Insert sync-packets, the decoded sequence should be all-zero.
1011 for (int n = 0; n < kNumSyncPackets; ++n) {
1012 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1013 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1014 &samples_per_channel, &num_channels,
1015 &output_type));
1016 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1017 ASSERT_EQ(1, num_channels);
1018 EXPECT_TRUE(IsAllZero(&decoded[delay_samples],
1019 samples_per_channel * num_channels - delay_samples));
1020 delay_samples = 0; // Delay only matters in the first frame.
1021 rtp_info.header.sequenceNumber++;
1022 rtp_info.header.timestamp += kBlockSize16kHz;
1023 receive_timestamp += kBlockSize16kHz;
1024 }
1025 // We insert a regular packet, if sync packet are not correctly buffered then
1026 // network statistics would show some packet loss.
1027 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1028 receive_timestamp));
1029 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1030 &samples_per_channel, &num_channels,
1031 &output_type));
1032 // Make sure the last inserted packet is decoded and there are non-zero
1033 // samples.
1034 EXPECT_FALSE(IsAllZero(decoded, samples_per_channel * num_channels));
1035 NetEqNetworkStatistics network_stats;
1036 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1037 // Expecting a "clean" network.
1038 EXPECT_EQ(0, network_stats.packet_loss_rate);
1039 EXPECT_EQ(0, network_stats.expand_rate);
1040 EXPECT_EQ(0, network_stats.accelerate_rate);
1041 EXPECT_EQ(0, network_stats.preemptive_rate);
1042}
1043
1044// Test if the size of the packet buffer reported correctly when containing
1045// sync packets. Also, test if network packets override sync packets. That is to
1046// prefer decoding a network packet to a sync packet, if both have same sequence
1047// number and timestamp.
1048TEST_F(NetEqDecodingTest,
1049 DISABLED_ON_ANDROID(SyncPacketBufferSizeAndOverridenByNetworkPackets)) {
1050 WebRtcRTPHeader rtp_info;
1051 PopulateRtpInfo(0, 0, &rtp_info);
1052 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1053 uint8_t payload[kPayloadBytes];
1054 int16_t decoded[kBlockSize16kHz];
1055 for (int n = 0; n < kPayloadBytes; ++n) {
1056 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1057 }
1058 // Insert some packets which decode to noise. We are not interested in
1059 // actual decoded values.
1060 NetEqOutputType output_type;
1061 int num_channels;
1062 int samples_per_channel;
1063 uint32_t receive_timestamp = 0;
1064 for (int n = 0; n < 1; ++n) {
1065 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1066 receive_timestamp));
1067 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1068 &samples_per_channel, &num_channels,
1069 &output_type));
1070 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1071 ASSERT_EQ(1, num_channels);
1072 rtp_info.header.sequenceNumber++;
1073 rtp_info.header.timestamp += kBlockSize16kHz;
1074 receive_timestamp += kBlockSize16kHz;
1075 }
1076 const int kNumSyncPackets = 10;
1077
1078 WebRtcRTPHeader first_sync_packet_rtp_info;
1079 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1080
1081 // Insert sync-packets, but no decoding.
1082 for (int n = 0; n < kNumSyncPackets; ++n) {
1083 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1084 rtp_info.header.sequenceNumber++;
1085 rtp_info.header.timestamp += kBlockSize16kHz;
1086 receive_timestamp += kBlockSize16kHz;
1087 }
1088 NetEqNetworkStatistics network_stats;
1089 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1090 EXPECT_EQ(kNumSyncPackets * 10, network_stats.current_buffer_size_ms);
1091
1092 // Rewind |rtp_info| to that of the first sync packet.
1093 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1094
1095 // Insert.
1096 for (int n = 0; n < kNumSyncPackets; ++n) {
1097 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1098 receive_timestamp));
1099 rtp_info.header.sequenceNumber++;
1100 rtp_info.header.timestamp += kBlockSize16kHz;
1101 receive_timestamp += kBlockSize16kHz;
1102 }
1103
1104 // Decode.
1105 for (int n = 0; n < kNumSyncPackets; ++n) {
1106 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1107 &samples_per_channel, &num_channels,
1108 &output_type));
1109 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1110 ASSERT_EQ(1, num_channels);
1111 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1112 }
1113}
1114
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001115void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1116 uint32_t start_timestamp,
1117 const std::set<uint16_t>& drop_seq_numbers,
1118 bool expect_seq_no_wrap,
1119 bool expect_timestamp_wrap) {
1120 uint16_t seq_no = start_seq_no;
1121 uint32_t timestamp = start_timestamp;
1122 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1123 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1124 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1125 const int kPayloadBytes = kSamples * sizeof(int16_t);
1126 double next_input_time_ms = 0.0;
1127 int16_t decoded[kBlockSize16kHz];
1128 int num_channels;
1129 int samples_per_channel;
1130 NetEqOutputType output_type;
1131 uint32_t receive_timestamp = 0;
1132
1133 // Insert speech for 1 second.
1134 const int kSpeechDurationMs = 2000;
1135 int packets_inserted = 0;
1136 uint16_t last_seq_no;
1137 uint32_t last_timestamp;
1138 bool timestamp_wrapped = false;
1139 bool seq_no_wrapped = false;
1140 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1141 // Each turn in this for loop is 10 ms.
1142 while (next_input_time_ms <= t_ms) {
1143 // Insert one 30 ms speech frame.
1144 uint8_t payload[kPayloadBytes] = {0};
1145 WebRtcRTPHeader rtp_info;
1146 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1147 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1148 // This sequence number was not in the set to drop. Insert it.
1149 ASSERT_EQ(0,
1150 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1151 receive_timestamp));
1152 ++packets_inserted;
1153 }
1154 NetEqNetworkStatistics network_stats;
1155 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1156
1157 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1158 // packet size for first few packets. Therefore we refrain from checking
1159 // the criteria.
1160 if (packets_inserted > 4) {
1161 // Expect preferred and actual buffer size to be no more than 2 frames.
1162 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
1163 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2);
1164 }
1165 last_seq_no = seq_no;
1166 last_timestamp = timestamp;
1167
1168 ++seq_no;
1169 timestamp += kSamples;
1170 receive_timestamp += kSamples;
1171 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1172
1173 seq_no_wrapped |= seq_no < last_seq_no;
1174 timestamp_wrapped |= timestamp < last_timestamp;
1175 }
1176 // Pull out data once.
1177 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1178 &samples_per_channel, &num_channels,
1179 &output_type));
1180 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1181 ASSERT_EQ(1, num_channels);
1182
1183 // Expect delay (in samples) to be less than 2 packets.
1184 EXPECT_LE(timestamp - neteq_->PlayoutTimestamp(),
1185 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001186 }
1187 // Make sure we have actually tested wrap-around.
1188 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1189 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1190}
1191
1192TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1193 // Start with a sequence number that will soon wrap.
1194 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1195 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1196}
1197
1198TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1199 // Start with a sequence number that will soon wrap.
1200 std::set<uint16_t> drop_seq_numbers;
1201 drop_seq_numbers.insert(0xFFFF);
1202 drop_seq_numbers.insert(0x0);
1203 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1204}
1205
1206TEST_F(NetEqDecodingTest, TimestampWrap) {
1207 // Start with a timestamp that will soon wrap.
1208 std::set<uint16_t> drop_seq_numbers;
1209 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1210}
1211
1212TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1213 // Start with a timestamp and a sequence number that will wrap at the same
1214 // time.
1215 std::set<uint16_t> drop_seq_numbers;
1216 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1217}
1218
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001219} // namespace webrtc