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