blob: 23a070de1362666fe7b0d128cf003e564c135de3 [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));
268 // Load iSAC SWB.
269 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104));
henrik.lundin@webrtc.orgac59dba2013-01-31 09:55:24 +0000270 // Load iSAC FB.
271 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 // Load PCM16B nb.
273 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93));
274 // Load PCM16B wb.
275 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94));
276 // Load PCM16B swb32.
277 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95));
278 // Load CNG 8 kHz.
279 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13));
280 // Load CNG 16 kHz.
281 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98));
282}
283
284void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
285 rtp_fp_ = fopen(rtp_file.c_str(), "rb");
286 ASSERT_TRUE(rtp_fp_ != NULL);
287 ASSERT_EQ(0, NETEQTEST_RTPpacket::skipFileHeader(rtp_fp_));
288}
289
290void NetEqDecodingTest::Process(NETEQTEST_RTPpacket* rtp, int* out_len) {
291 // Check if time to receive.
292 while ((sim_clock_ >= rtp->time()) &&
293 (rtp->dataLen() >= 0)) {
294 if (rtp->dataLen() > 0) {
295 WebRtcRTPHeader rtpInfo;
296 rtp->parseHeader(&rtpInfo);
297 ASSERT_EQ(0, neteq_->InsertPacket(
298 rtpInfo,
299 rtp->payload(),
300 rtp->payloadLen(),
301 rtp->time() * (output_sample_rate_ / 1000)));
302 }
303 // Get next packet.
304 ASSERT_NE(-1, rtp->readFromFile(rtp_fp_));
305 }
306
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000307 // Get audio from NetEq.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 NetEqOutputType type;
309 int num_channels;
310 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len,
311 &num_channels, &type));
312 ASSERT_TRUE((*out_len == kBlockSize8kHz) ||
313 (*out_len == kBlockSize16kHz) ||
314 (*out_len == kBlockSize32kHz));
315 output_sample_rate_ = *out_len / 10 * 1000;
316
317 // Increase time.
318 sim_clock_ += kTimeStepMs;
319}
320
321void NetEqDecodingTest::DecodeAndCompare(const std::string &rtp_file,
322 const std::string &ref_file) {
323 OpenInputFile(rtp_file);
324
325 std::string ref_out_file = "";
326 if (ref_file.empty()) {
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000327 ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000328 }
329 RefFiles ref_files(ref_file, ref_out_file);
330
331 NETEQTEST_RTPpacket rtp;
332 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
333 int i = 0;
334 while (rtp.dataLen() >= 0) {
335 std::ostringstream ss;
336 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
337 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000338 int out_len = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 ASSERT_NO_FATAL_FAILURE(Process(&rtp, &out_len));
340 ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len));
341 }
342}
343
344void NetEqDecodingTest::DecodeAndCheckStats(const std::string &rtp_file,
345 const std::string &stat_ref_file,
346 const std::string &rtcp_ref_file) {
347 OpenInputFile(rtp_file);
348 std::string stat_out_file = "";
349 if (stat_ref_file.empty()) {
350 stat_out_file = webrtc::test::OutputPath() +
351 "neteq_network_stats.dat";
352 }
353 RefFiles network_stat_files(stat_ref_file, stat_out_file);
354
355 std::string rtcp_out_file = "";
356 if (rtcp_ref_file.empty()) {
357 rtcp_out_file = webrtc::test::OutputPath() +
358 "neteq_rtcp_stats.dat";
359 }
360 RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file);
361
362 NETEQTEST_RTPpacket rtp;
363 ASSERT_GT(rtp.readFromFile(rtp_fp_), 0);
364 while (rtp.dataLen() >= 0) {
365 int out_len;
366 Process(&rtp, &out_len);
367
368 // Query the network statistics API once per second
369 if (sim_clock_ % 1000 == 0) {
370 // Process NetworkStatistics.
371 NetEqNetworkStatistics network_stats;
372 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
373 network_stat_files.ProcessReference(network_stats);
374
375 // Process RTCPstat.
376 RtcpStatistics rtcp_stats;
377 neteq_->GetRtcpStatistics(&rtcp_stats);
378 rtcp_stat_files.ProcessReference(rtcp_stats);
379 }
380 }
381}
382
383void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
384 int timestamp,
385 WebRtcRTPHeader* rtp_info) {
386 rtp_info->header.sequenceNumber = frame_index;
387 rtp_info->header.timestamp = timestamp;
388 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
389 rtp_info->header.payloadType = 94; // PCM16b WB codec.
390 rtp_info->header.markerBit = 0;
391}
392
393void NetEqDecodingTest::PopulateCng(int frame_index,
394 int timestamp,
395 WebRtcRTPHeader* rtp_info,
396 uint8_t* payload,
397 int* payload_len) {
398 rtp_info->header.sequenceNumber = frame_index;
399 rtp_info->header.timestamp = timestamp;
400 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
401 rtp_info->header.payloadType = 98; // WB CNG.
402 rtp_info->header.markerBit = 0;
403 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
404 *payload_len = 1; // Only noise level, no spectral parameters.
405}
406
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000407void NetEqDecodingTest::CheckBgnOff(int sampling_rate_hz,
408 NetEqBackgroundNoiseMode bgn_mode) {
409 int expected_samples_per_channel = 0;
410 uint8_t payload_type = 0xFF; // Invalid.
411 if (sampling_rate_hz == 8000) {
412 expected_samples_per_channel = kBlockSize8kHz;
413 payload_type = 93; // PCM 16, 8 kHz.
414 } else if (sampling_rate_hz == 16000) {
415 expected_samples_per_channel = kBlockSize16kHz;
416 payload_type = 94; // PCM 16, 16 kHZ.
417 } else if (sampling_rate_hz == 32000) {
418 expected_samples_per_channel = kBlockSize32kHz;
419 payload_type = 95; // PCM 16, 32 kHz.
420 } else {
421 ASSERT_TRUE(false); // Unsupported test case.
422 }
423
424 NetEqOutputType type;
425 int16_t output[kBlockSize32kHz]; // Maximum size is chosen.
426 int16_t input[kBlockSize32kHz]; // Maximum size is chosen.
427
428 // Payload of 10 ms of PCM16 32 kHz.
429 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
430
431 // Random payload.
432 for (int n = 0; n < expected_samples_per_channel; ++n) {
433 input[n] = (rand() & ((1 << 10) - 1)) - ((1 << 5) - 1);
434 }
435 int enc_len_bytes = WebRtcPcm16b_EncodeW16(
436 input, expected_samples_per_channel, reinterpret_cast<int16_t*>(payload));
437 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
438
439 WebRtcRTPHeader rtp_info;
440 PopulateRtpInfo(0, 0, &rtp_info);
441 rtp_info.header.payloadType = payload_type;
442
443 int number_channels = 0;
444 int samples_per_channel = 0;
445
446 uint32_t receive_timestamp = 0;
447 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
448 number_channels = 0;
449 samples_per_channel = 0;
450 ASSERT_EQ(0, neteq_->InsertPacket(
451 rtp_info, payload, enc_len_bytes, receive_timestamp));
452 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
453 &number_channels, &type));
454 ASSERT_EQ(1, number_channels);
455 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
456 ASSERT_EQ(kOutputNormal, type);
457
458 // Next packet.
459 rtp_info.header.timestamp += expected_samples_per_channel;
460 rtp_info.header.sequenceNumber++;
461 receive_timestamp += expected_samples_per_channel;
462 }
463
464 number_channels = 0;
465 samples_per_channel = 0;
466
467 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull one
468 // frame without checking speech-type. This is the first frame pulled without
469 // inserting any packet, and might not be labeled as PCL.
470 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
471 &number_channels, &type));
472 ASSERT_EQ(1, number_channels);
473 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
474
475 // To be able to test the fading of background noise we need at lease to pull
476 // 610 frames.
477 const int kFadingThreshold = 610;
478
479 // Test several CNG-to-PLC packet for the expected behavior. The number 20 is
480 // arbitrary, but sufficiently large to test enough number of frames.
481 const int kNumPlcToCngTestFrames = 20;
482 bool plc_to_cng = false;
483 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
484 number_channels = 0;
485 samples_per_channel = 0;
486 memset(output, 1, sizeof(output)); // Set to non-zero.
487 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize32kHz, output, &samples_per_channel,
488 &number_channels, &type));
489 ASSERT_EQ(1, number_channels);
490 ASSERT_EQ(expected_samples_per_channel, samples_per_channel);
491 if (type == kOutputPLCtoCNG) {
492 plc_to_cng = true;
493 double sum_squared = 0;
494 for (int k = 0; k < number_channels * samples_per_channel; ++k)
495 sum_squared += output[k] * output[k];
496 if (bgn_mode == kBgnOn) {
497 EXPECT_NE(0, sum_squared);
498 } else if (bgn_mode == kBgnOff || n > kFadingThreshold) {
499 EXPECT_EQ(0, sum_squared);
500 }
501 } else {
502 EXPECT_EQ(kOutputPLC, type);
503 }
504 }
505 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
506}
507
kjellander@webrtc.org6eba2772013-06-04 05:46:37 +0000508#if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
509// Disabled for Windows 64-bit until webrtc:1458 is fixed.
510#define MAYBE_TestBitExactness DISABLED_TestBitExactness
511#else
512#define MAYBE_TestBitExactness TestBitExactness
513#endif
514
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000515TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(MAYBE_TestBitExactness)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000516 const std::string kInputRtpFile = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000517 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000518#if defined(_MSC_VER) && (_MSC_VER >= 1700)
519 // For Visual Studio 2012 and later, we will have to use the generic reference
520 // file, rather than the windows-specific one.
521 const std::string kInputRefFile = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000522 "resources/audio_coding/neteq4_universal_ref.pcm";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000523#else
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000524 const std::string kInputRefFile =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000525 webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000526#endif
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000527
528 if (FLAGS_gen_ref) {
529 DecodeAndCompare(kInputRtpFile, "");
530 } else {
531 DecodeAndCompare(kInputRtpFile, kInputRefFile);
532 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000533}
534
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000535TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestNetworkStatistics)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000536 const std::string kInputRtpFile = webrtc::test::ProjectRootPath() +
henrik.lundin@webrtc.org73deaad2013-01-31 13:32:51 +0000537 "resources/audio_coding/neteq_universal_new.rtp";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000538#if defined(_MSC_VER) && (_MSC_VER >= 1700)
539 // For Visual Studio 2012 and later, we will have to use the generic reference
540 // file, rather than the windows-specific one.
541 const std::string kNetworkStatRefFile = webrtc::test::ProjectRootPath() +
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000542 "resources/audio_coding/neteq4_network_stats.dat";
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000543#else
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000544 const std::string kNetworkStatRefFile =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000545 webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat");
henrik.lundin@webrtc.org6e3968f2013-01-31 15:07:30 +0000546#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000547 const std::string kRtcpStatRefFile =
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000548 webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat");
549 if (FLAGS_gen_ref) {
550 DecodeAndCheckStats(kInputRtpFile, "", "");
551 } else {
552 DecodeAndCheckStats(kInputRtpFile, kNetworkStatRefFile, kRtcpStatRefFile);
553 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554}
555
556// TODO(hlundin): Re-enable test once the statistics interface is up and again.
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000557TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestFrameWaitingTimeStatistics)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558 // Use fax mode to avoid time-scaling. This is to simplify the testing of
559 // packet waiting times in the packet buffer.
560 neteq_->SetPlayoutMode(kPlayoutFax);
561 ASSERT_EQ(kPlayoutFax, neteq_->PlayoutMode());
562 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
563 size_t num_frames = 30;
564 const int kSamples = 10 * 16;
565 const int kPayloadBytes = kSamples * 2;
566 for (size_t i = 0; i < num_frames; ++i) {
567 uint16_t payload[kSamples] = {0};
568 WebRtcRTPHeader rtp_info;
569 rtp_info.header.sequenceNumber = i;
570 rtp_info.header.timestamp = i * kSamples;
571 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
572 rtp_info.header.payloadType = 94; // PCM16b WB codec.
573 rtp_info.header.markerBit = 0;
574 ASSERT_EQ(0, neteq_->InsertPacket(
575 rtp_info,
576 reinterpret_cast<uint8_t*>(payload),
577 kPayloadBytes, 0));
578 }
579 // Pull out all data.
580 for (size_t i = 0; i < num_frames; ++i) {
581 int out_len;
582 int num_channels;
583 NetEqOutputType type;
584 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
585 &num_channels, &type));
586 ASSERT_EQ(kBlockSize16kHz, out_len);
587 }
588
589 std::vector<int> waiting_times;
590 neteq_->WaitingTimes(&waiting_times);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000591 EXPECT_EQ(num_frames, waiting_times.size());
592 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
593 // spacing (per definition), we expect the delay to increase with 10 ms for
594 // each packet.
595 for (size_t i = 0; i < waiting_times.size(); ++i) {
596 EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]);
597 }
598
599 // Check statistics again and make sure it's been reset.
600 neteq_->WaitingTimes(&waiting_times);
turaj@webrtc.org58cd3162013-10-31 15:15:55 +0000601 int len = waiting_times.size();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602 EXPECT_EQ(0, len);
603
604 // Process > 100 frames, and make sure that that we get statistics
605 // only for 100 frames. Note the new SSRC, causing NetEQ to reset.
606 num_frames = 110;
607 for (size_t i = 0; i < num_frames; ++i) {
608 uint16_t payload[kSamples] = {0};
609 WebRtcRTPHeader rtp_info;
610 rtp_info.header.sequenceNumber = i;
611 rtp_info.header.timestamp = i * kSamples;
612 rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC.
613 rtp_info.header.payloadType = 94; // PCM16b WB codec.
614 rtp_info.header.markerBit = 0;
615 ASSERT_EQ(0, neteq_->InsertPacket(
616 rtp_info,
617 reinterpret_cast<uint8_t*>(payload),
618 kPayloadBytes, 0));
619 int out_len;
620 int num_channels;
621 NetEqOutputType type;
622 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
623 &num_channels, &type));
624 ASSERT_EQ(kBlockSize16kHz, out_len);
625 }
626
627 neteq_->WaitingTimes(&waiting_times);
628 EXPECT_EQ(100u, waiting_times.size());
629}
630
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000631TEST_F(NetEqDecodingTest,
632 DISABLED_ON_ANDROID(TestAverageInterArrivalTimeNegative)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000633 const int kNumFrames = 3000; // Needed for convergence.
634 int frame_index = 0;
635 const int kSamples = 10 * 16;
636 const int kPayloadBytes = kSamples * 2;
637 while (frame_index < kNumFrames) {
638 // Insert one packet each time, except every 10th time where we insert two
639 // packets at once. This will create a negative clock-drift of approx. 10%.
640 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
641 for (int n = 0; n < num_packets; ++n) {
642 uint8_t payload[kPayloadBytes] = {0};
643 WebRtcRTPHeader rtp_info;
644 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
645 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
646 ++frame_index;
647 }
648
649 // Pull out data once.
650 int out_len;
651 int num_channels;
652 NetEqOutputType type;
653 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
654 &num_channels, &type));
655 ASSERT_EQ(kBlockSize16kHz, out_len);
656 }
657
658 NetEqNetworkStatistics network_stats;
659 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
660 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
661}
662
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000663TEST_F(NetEqDecodingTest,
664 DISABLED_ON_ANDROID(TestAverageInterArrivalTimePositive)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000665 const int kNumFrames = 5000; // Needed for convergence.
666 int frame_index = 0;
667 const int kSamples = 10 * 16;
668 const int kPayloadBytes = kSamples * 2;
669 for (int i = 0; i < kNumFrames; ++i) {
670 // Insert one packet each time, except every 10th time where we don't insert
671 // any packet. This will create a positive clock-drift of approx. 11%.
672 int num_packets = (i % 10 == 9 ? 0 : 1);
673 for (int n = 0; n < num_packets; ++n) {
674 uint8_t payload[kPayloadBytes] = {0};
675 WebRtcRTPHeader rtp_info;
676 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
677 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
678 ++frame_index;
679 }
680
681 // Pull out data once.
682 int out_len;
683 int num_channels;
684 NetEqOutputType type;
685 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
686 &num_channels, &type));
687 ASSERT_EQ(kBlockSize16kHz, out_len);
688 }
689
690 NetEqNetworkStatistics network_stats;
691 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
692 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
693}
694
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000695TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(LongCngWithClockDrift)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000696 uint16_t seq_no = 0;
697 uint32_t timestamp = 0;
698 const int kFrameSizeMs = 30;
699 const int kSamples = kFrameSizeMs * 16;
700 const int kPayloadBytes = kSamples * 2;
701 // Apply a clock drift of -25 ms / s (sender faster than receiver).
702 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
703 double next_input_time_ms = 0.0;
704 double t_ms;
705 NetEqOutputType type;
706
707 // Insert speech for 5 seconds.
708 const int kSpeechDurationMs = 5000;
709 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
710 // Each turn in this for loop is 10 ms.
711 while (next_input_time_ms <= t_ms) {
712 // Insert one 30 ms speech frame.
713 uint8_t payload[kPayloadBytes] = {0};
714 WebRtcRTPHeader rtp_info;
715 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
716 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
717 ++seq_no;
718 timestamp += kSamples;
719 next_input_time_ms += static_cast<double>(kFrameSizeMs) * kDriftFactor;
720 }
721 // Pull out data once.
722 int out_len;
723 int num_channels;
724 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
725 &num_channels, &type));
726 ASSERT_EQ(kBlockSize16kHz, out_len);
727 }
728
729 EXPECT_EQ(kOutputNormal, type);
730 int32_t delay_before = timestamp - neteq_->PlayoutTimestamp();
731
732 // Insert CNG for 1 minute (= 60000 ms).
733 const int kCngPeriodMs = 100;
734 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
735 const int kCngDurationMs = 60000;
736 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
737 // Each turn in this for loop is 10 ms.
738 while (next_input_time_ms <= t_ms) {
739 // Insert one CNG frame each 100 ms.
740 uint8_t payload[kPayloadBytes];
741 int payload_len;
742 WebRtcRTPHeader rtp_info;
743 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
744 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0));
745 ++seq_no;
746 timestamp += kCngPeriodSamples;
747 next_input_time_ms += static_cast<double>(kCngPeriodMs) * kDriftFactor;
748 }
749 // Pull out data once.
750 int out_len;
751 int num_channels;
752 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
753 &num_channels, &type));
754 ASSERT_EQ(kBlockSize16kHz, out_len);
755 }
756
757 EXPECT_EQ(kOutputCNG, type);
758
759 // Insert speech again until output type is speech.
760 while (type != kOutputNormal) {
761 // Each turn in this for loop is 10 ms.
762 while (next_input_time_ms <= t_ms) {
763 // Insert one 30 ms speech frame.
764 uint8_t payload[kPayloadBytes] = {0};
765 WebRtcRTPHeader rtp_info;
766 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
767 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
768 ++seq_no;
769 timestamp += kSamples;
770 next_input_time_ms += static_cast<double>(kFrameSizeMs) * kDriftFactor;
771 }
772 // Pull out data once.
773 int out_len;
774 int num_channels;
775 ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len,
776 &num_channels, &type));
777 ASSERT_EQ(kBlockSize16kHz, out_len);
778 // Increase clock.
779 t_ms += 10;
780 }
781
782 int32_t delay_after = timestamp - neteq_->PlayoutTimestamp();
783 // Compare delay before and after, and make sure it differs less than 20 ms.
784 EXPECT_LE(delay_after, delay_before + 20 * 16);
785 EXPECT_GE(delay_after, delay_before - 20 * 16);
786}
787
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000788TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(UnknownPayloadType)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000789 const int kPayloadBytes = 100;
790 uint8_t payload[kPayloadBytes] = {0};
791 WebRtcRTPHeader rtp_info;
792 PopulateRtpInfo(0, 0, &rtp_info);
793 rtp_info.header.payloadType = 1; // Not registered as a decoder.
794 EXPECT_EQ(NetEq::kFail,
795 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
796 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
797}
798
minyue@webrtc.org7bb54362013-08-06 05:40:57 +0000799TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(OversizePacket)) {
800 // Payload size is greater than packet buffer size
801 const int kPayloadBytes = NetEq::kMaxBytesInBuffer + 1;
802 uint8_t payload[kPayloadBytes] = {0};
803 WebRtcRTPHeader rtp_info;
804 PopulateRtpInfo(0, 0, &rtp_info);
805 rtp_info.header.payloadType = 103; // iSAC, no packet splitting.
806 EXPECT_EQ(NetEq::kFail,
807 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
808 EXPECT_EQ(NetEq::kOversizePacket, neteq_->LastError());
809}
810
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000811TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000812 const int kPayloadBytes = 100;
813 uint8_t payload[kPayloadBytes] = {0};
814 WebRtcRTPHeader rtp_info;
815 PopulateRtpInfo(0, 0, &rtp_info);
816 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
817 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0));
818 NetEqOutputType type;
819 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
820 // to GetAudio.
821 for (int i = 0; i < kMaxBlockSize; ++i) {
822 out_data_[i] = 1;
823 }
824 int num_channels;
825 int samples_per_channel;
826 EXPECT_EQ(NetEq::kFail,
827 neteq_->GetAudio(kMaxBlockSize, out_data_,
828 &samples_per_channel, &num_channels, &type));
829 // Verify that there is a decoder error to check.
830 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
831 // Code 6730 is an iSAC error code.
832 EXPECT_EQ(6730, neteq_->LastDecoderError());
833 // Verify that the first 160 samples are set to 0, and that the remaining
834 // samples are left unmodified.
835 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
836 for (int i = 0; i < kExpectedOutputLength; ++i) {
837 std::ostringstream ss;
838 ss << "i = " << i;
839 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
840 EXPECT_EQ(0, out_data_[i]);
841 }
842 for (int i = kExpectedOutputLength; i < kMaxBlockSize; ++i) {
843 std::ostringstream ss;
844 ss << "i = " << i;
845 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
846 EXPECT_EQ(1, out_data_[i]);
847 }
848}
849
henrike@webrtc.orga950300b2013-07-08 18:53:54 +0000850TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(GetAudioBeforeInsertPacket)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000851 NetEqOutputType type;
852 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
853 // to GetAudio.
854 for (int i = 0; i < kMaxBlockSize; ++i) {
855 out_data_[i] = 1;
856 }
857 int num_channels;
858 int samples_per_channel;
859 EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_,
860 &samples_per_channel,
861 &num_channels, &type));
862 // Verify that the first block of samples is set to 0.
863 static const int kExpectedOutputLength =
864 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
865 for (int i = 0; i < kExpectedOutputLength; ++i) {
866 std::ostringstream ss;
867 ss << "i = " << i;
868 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
869 EXPECT_EQ(0, out_data_[i]);
870 }
871}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000872
turaj@webrtc.org3fdeddb2013-09-25 22:19:22 +0000873TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(BackgroundNoise)) {
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000874 neteq_->SetBackgroundNoiseMode(kBgnOn);
875 CheckBgnOff(8000, kBgnOn);
876 CheckBgnOff(16000, kBgnOn);
877 CheckBgnOff(32000, kBgnOn);
878 EXPECT_EQ(kBgnOn, neteq_->BackgroundNoiseMode());
879
880 neteq_->SetBackgroundNoiseMode(kBgnOff);
881 CheckBgnOff(8000, kBgnOff);
882 CheckBgnOff(16000, kBgnOff);
883 CheckBgnOff(32000, kBgnOff);
884 EXPECT_EQ(kBgnOff, neteq_->BackgroundNoiseMode());
885
886 neteq_->SetBackgroundNoiseMode(kBgnFade);
887 CheckBgnOff(8000, kBgnFade);
888 CheckBgnOff(16000, kBgnFade);
889 CheckBgnOff(32000, kBgnFade);
890 EXPECT_EQ(kBgnFade, neteq_->BackgroundNoiseMode());
891}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000892
893TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketInsert)) {
894 WebRtcRTPHeader rtp_info;
895 uint32_t receive_timestamp = 0;
896 // For the readability use the following payloads instead of the defaults of
897 // this test.
898 uint8_t kPcm16WbPayloadType = 1;
899 uint8_t kCngNbPayloadType = 2;
900 uint8_t kCngWbPayloadType = 3;
901 uint8_t kCngSwb32PayloadType = 4;
902 uint8_t kCngSwb48PayloadType = 5;
903 uint8_t kAvtPayloadType = 6;
904 uint8_t kRedPayloadType = 7;
905 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
906
907 // Register decoders.
908 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb,
909 kPcm16WbPayloadType));
910 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType));
911 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType));
912 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz,
913 kCngSwb32PayloadType));
914 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz,
915 kCngSwb48PayloadType));
916 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType));
917 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType));
918 ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType));
919
920 PopulateRtpInfo(0, 0, &rtp_info);
921 rtp_info.header.payloadType = kPcm16WbPayloadType;
922
923 // The first packet injected cannot be sync-packet.
924 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
925
926 // Payload length of 10 ms PCM16 16 kHz.
927 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
928 uint8_t payload[kPayloadBytes] = {0};
929 ASSERT_EQ(0, neteq_->InsertPacket(
930 rtp_info, payload, kPayloadBytes, receive_timestamp));
931
932 // Next packet. Last packet contained 10 ms audio.
933 rtp_info.header.sequenceNumber++;
934 rtp_info.header.timestamp += kBlockSize16kHz;
935 receive_timestamp += kBlockSize16kHz;
936
937 // Unacceptable payload types CNG, AVT (DTMF), RED.
938 rtp_info.header.payloadType = kCngNbPayloadType;
939 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
940
941 rtp_info.header.payloadType = kCngWbPayloadType;
942 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
943
944 rtp_info.header.payloadType = kCngSwb32PayloadType;
945 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
946
947 rtp_info.header.payloadType = kCngSwb48PayloadType;
948 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
949
950 rtp_info.header.payloadType = kAvtPayloadType;
951 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
952
953 rtp_info.header.payloadType = kRedPayloadType;
954 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
955
956 // Change of codec cannot be initiated with a sync packet.
957 rtp_info.header.payloadType = kIsacPayloadType;
958 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
959
960 // Change of SSRC is not allowed with a sync packet.
961 rtp_info.header.payloadType = kPcm16WbPayloadType;
962 ++rtp_info.header.ssrc;
963 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
964
965 --rtp_info.header.ssrc;
966 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
967}
968
969// First insert several noise like packets, then sync-packets. Decoding all
970// packets should not produce error, statistics should not show any packet loss
971// and sync-packets should decode to zero.
972TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(SyncPacketDecode)) {
973 WebRtcRTPHeader rtp_info;
974 PopulateRtpInfo(0, 0, &rtp_info);
975 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
976 uint8_t payload[kPayloadBytes];
977 int16_t decoded[kBlockSize16kHz];
978 for (int n = 0; n < kPayloadBytes; ++n) {
979 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
980 }
981 // Insert some packets which decode to noise. We are not interested in
982 // actual decoded values.
983 NetEqOutputType output_type;
984 int num_channels;
985 int samples_per_channel;
986 uint32_t receive_timestamp = 0;
987 int delay_samples = 0;
988 for (int n = 0; n < 100; ++n) {
989 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
990 receive_timestamp));
991 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
992 &samples_per_channel, &num_channels,
993 &output_type));
994 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
995 ASSERT_EQ(1, num_channels);
996
997 // Even if there is RTP packet in NetEq's buffer, the first frame pulled
998 // from NetEq starts with few zero samples. Here we measure this delay.
999 if (n == 0) {
1000 while(decoded[delay_samples] == 0) delay_samples++;
1001 }
1002 rtp_info.header.sequenceNumber++;
1003 rtp_info.header.timestamp += kBlockSize16kHz;
1004 receive_timestamp += kBlockSize16kHz;
1005 }
1006 const int kNumSyncPackets = 10;
1007 // Insert sync-packets, the decoded sequence should be all-zero.
1008 for (int n = 0; n < kNumSyncPackets; ++n) {
1009 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1010 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1011 &samples_per_channel, &num_channels,
1012 &output_type));
1013 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1014 ASSERT_EQ(1, num_channels);
1015 EXPECT_TRUE(IsAllZero(&decoded[delay_samples],
1016 samples_per_channel * num_channels - delay_samples));
1017 delay_samples = 0; // Delay only matters in the first frame.
1018 rtp_info.header.sequenceNumber++;
1019 rtp_info.header.timestamp += kBlockSize16kHz;
1020 receive_timestamp += kBlockSize16kHz;
1021 }
1022 // We insert a regular packet, if sync packet are not correctly buffered then
1023 // network statistics would show some packet loss.
1024 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1025 receive_timestamp));
1026 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1027 &samples_per_channel, &num_channels,
1028 &output_type));
1029 // Make sure the last inserted packet is decoded and there are non-zero
1030 // samples.
1031 EXPECT_FALSE(IsAllZero(decoded, samples_per_channel * num_channels));
1032 NetEqNetworkStatistics network_stats;
1033 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1034 // Expecting a "clean" network.
1035 EXPECT_EQ(0, network_stats.packet_loss_rate);
1036 EXPECT_EQ(0, network_stats.expand_rate);
1037 EXPECT_EQ(0, network_stats.accelerate_rate);
1038 EXPECT_EQ(0, network_stats.preemptive_rate);
1039}
1040
1041// Test if the size of the packet buffer reported correctly when containing
1042// sync packets. Also, test if network packets override sync packets. That is to
1043// prefer decoding a network packet to a sync packet, if both have same sequence
1044// number and timestamp.
1045TEST_F(NetEqDecodingTest,
1046 DISABLED_ON_ANDROID(SyncPacketBufferSizeAndOverridenByNetworkPackets)) {
1047 WebRtcRTPHeader rtp_info;
1048 PopulateRtpInfo(0, 0, &rtp_info);
1049 const int kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
1050 uint8_t payload[kPayloadBytes];
1051 int16_t decoded[kBlockSize16kHz];
1052 for (int n = 0; n < kPayloadBytes; ++n) {
1053 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1054 }
1055 // Insert some packets which decode to noise. We are not interested in
1056 // actual decoded values.
1057 NetEqOutputType output_type;
1058 int num_channels;
1059 int samples_per_channel;
1060 uint32_t receive_timestamp = 0;
1061 for (int n = 0; n < 1; ++n) {
1062 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1063 receive_timestamp));
1064 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1065 &samples_per_channel, &num_channels,
1066 &output_type));
1067 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1068 ASSERT_EQ(1, num_channels);
1069 rtp_info.header.sequenceNumber++;
1070 rtp_info.header.timestamp += kBlockSize16kHz;
1071 receive_timestamp += kBlockSize16kHz;
1072 }
1073 const int kNumSyncPackets = 10;
1074
1075 WebRtcRTPHeader first_sync_packet_rtp_info;
1076 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1077
1078 // Insert sync-packets, but no decoding.
1079 for (int n = 0; n < kNumSyncPackets; ++n) {
1080 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1081 rtp_info.header.sequenceNumber++;
1082 rtp_info.header.timestamp += kBlockSize16kHz;
1083 receive_timestamp += kBlockSize16kHz;
1084 }
1085 NetEqNetworkStatistics network_stats;
1086 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1087 EXPECT_EQ(kNumSyncPackets * 10, network_stats.current_buffer_size_ms);
1088
1089 // Rewind |rtp_info| to that of the first sync packet.
1090 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1091
1092 // Insert.
1093 for (int n = 0; n < kNumSyncPackets; ++n) {
1094 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1095 receive_timestamp));
1096 rtp_info.header.sequenceNumber++;
1097 rtp_info.header.timestamp += kBlockSize16kHz;
1098 receive_timestamp += kBlockSize16kHz;
1099 }
1100
1101 // Decode.
1102 for (int n = 0; n < kNumSyncPackets; ++n) {
1103 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1104 &samples_per_channel, &num_channels,
1105 &output_type));
1106 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1107 ASSERT_EQ(1, num_channels);
1108 EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels));
1109 }
1110}
1111
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001112void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1113 uint32_t start_timestamp,
1114 const std::set<uint16_t>& drop_seq_numbers,
1115 bool expect_seq_no_wrap,
1116 bool expect_timestamp_wrap) {
1117 uint16_t seq_no = start_seq_no;
1118 uint32_t timestamp = start_timestamp;
1119 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1120 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1121 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
1122 const int kPayloadBytes = kSamples * sizeof(int16_t);
1123 double next_input_time_ms = 0.0;
1124 int16_t decoded[kBlockSize16kHz];
1125 int num_channels;
1126 int samples_per_channel;
1127 NetEqOutputType output_type;
1128 uint32_t receive_timestamp = 0;
1129
1130 // Insert speech for 1 second.
1131 const int kSpeechDurationMs = 2000;
1132 int packets_inserted = 0;
1133 uint16_t last_seq_no;
1134 uint32_t last_timestamp;
1135 bool timestamp_wrapped = false;
1136 bool seq_no_wrapped = false;
1137 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1138 // Each turn in this for loop is 10 ms.
1139 while (next_input_time_ms <= t_ms) {
1140 // Insert one 30 ms speech frame.
1141 uint8_t payload[kPayloadBytes] = {0};
1142 WebRtcRTPHeader rtp_info;
1143 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1144 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1145 // This sequence number was not in the set to drop. Insert it.
1146 ASSERT_EQ(0,
1147 neteq_->InsertPacket(rtp_info, payload, kPayloadBytes,
1148 receive_timestamp));
1149 ++packets_inserted;
1150 }
1151 NetEqNetworkStatistics network_stats;
1152 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1153
1154 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1155 // packet size for first few packets. Therefore we refrain from checking
1156 // the criteria.
1157 if (packets_inserted > 4) {
1158 // Expect preferred and actual buffer size to be no more than 2 frames.
1159 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
1160 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2);
1161 }
1162 last_seq_no = seq_no;
1163 last_timestamp = timestamp;
1164
1165 ++seq_no;
1166 timestamp += kSamples;
1167 receive_timestamp += kSamples;
1168 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1169
1170 seq_no_wrapped |= seq_no < last_seq_no;
1171 timestamp_wrapped |= timestamp < last_timestamp;
1172 }
1173 // Pull out data once.
1174 ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded,
1175 &samples_per_channel, &num_channels,
1176 &output_type));
1177 ASSERT_EQ(kBlockSize16kHz, samples_per_channel);
1178 ASSERT_EQ(1, num_channels);
1179
1180 // Expect delay (in samples) to be less than 2 packets.
1181 EXPECT_LE(timestamp - neteq_->PlayoutTimestamp(),
1182 static_cast<uint32_t>(kSamples * 2));
1183
1184 }
1185 // Make sure we have actually tested wrap-around.
1186 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1187 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1188}
1189
1190TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1191 // Start with a sequence number that will soon wrap.
1192 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1193 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1194}
1195
1196TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1197 // Start with a sequence number that will soon wrap.
1198 std::set<uint16_t> drop_seq_numbers;
1199 drop_seq_numbers.insert(0xFFFF);
1200 drop_seq_numbers.insert(0x0);
1201 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1202}
1203
1204TEST_F(NetEqDecodingTest, TimestampWrap) {
1205 // Start with a timestamp that will soon wrap.
1206 std::set<uint16_t> drop_seq_numbers;
1207 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1208}
1209
1210TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1211 // Start with a timestamp and a sequence number that will wrap at the same
1212 // time.
1213 std::set<uint16_t> drop_seq_numbers;
1214 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1215}
1216
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001217} // namespace