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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 15 | #include "webrtc/modules/audio_coding/neteq/interface/neteq.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
pbos@webrtc.org | 3ecc162 | 2014-03-07 15:23:34 +0000 | [diff] [blame] | 17 | #include <math.h> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> // memset |
| 20 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 21 | #include <algorithm> |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 22 | #include <set> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 26 | #include "gflags/gflags.h" |
kjellander@webrtc.org | 3c0aae1 | 2014-09-04 09:55:40 +0000 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame^] | 28 | #include "webrtc/base/scoped_ptr.h" |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 29 | #include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h" |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 30 | #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h" |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 31 | #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | #include "webrtc/test/testsupport/fileutils.h" |
henrike@webrtc.org | a950300b | 2013-07-08 18:53:54 +0000 | [diff] [blame] | 33 | #include "webrtc/test/testsupport/gtest_disable.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 34 | #include "webrtc/typedefs.h" |
| 35 | |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 36 | DEFINE_bool(gen_ref, false, "Generate reference files."); |
| 37 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 38 | namespace webrtc { |
| 39 | |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 40 | static bool IsAllZero(const int16_t* buf, int buf_length) { |
| 41 | bool all_zero = true; |
| 42 | for (int n = 0; n < buf_length && all_zero; ++n) |
| 43 | all_zero = buf[n] == 0; |
| 44 | return all_zero; |
| 45 | } |
| 46 | |
| 47 | static bool IsAllNonZero(const int16_t* buf, int buf_length) { |
| 48 | bool all_non_zero = true; |
| 49 | for (int n = 0; n < buf_length && all_non_zero; ++n) |
| 50 | all_non_zero = buf[n] != 0; |
| 51 | return all_non_zero; |
| 52 | } |
| 53 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | class RefFiles { |
| 55 | public: |
| 56 | RefFiles(const std::string& input_file, const std::string& output_file); |
| 57 | ~RefFiles(); |
| 58 | template<class T> void ProcessReference(const T& test_results); |
| 59 | template<typename T, size_t n> void ProcessReference( |
| 60 | const T (&test_results)[n], |
| 61 | size_t length); |
| 62 | template<typename T, size_t n> void WriteToFile( |
| 63 | const T (&test_results)[n], |
| 64 | size_t length); |
| 65 | template<typename T, size_t n> void ReadFromFileAndCompare( |
| 66 | const T (&test_results)[n], |
| 67 | size_t length); |
| 68 | void WriteToFile(const NetEqNetworkStatistics& stats); |
| 69 | void ReadFromFileAndCompare(const NetEqNetworkStatistics& stats); |
| 70 | void WriteToFile(const RtcpStatistics& stats); |
| 71 | void ReadFromFileAndCompare(const RtcpStatistics& stats); |
| 72 | |
| 73 | FILE* input_fp_; |
| 74 | FILE* output_fp_; |
| 75 | }; |
| 76 | |
| 77 | RefFiles::RefFiles(const std::string &input_file, |
| 78 | const std::string &output_file) |
| 79 | : input_fp_(NULL), |
| 80 | output_fp_(NULL) { |
| 81 | if (!input_file.empty()) { |
| 82 | input_fp_ = fopen(input_file.c_str(), "rb"); |
| 83 | EXPECT_TRUE(input_fp_ != NULL); |
| 84 | } |
| 85 | if (!output_file.empty()) { |
| 86 | output_fp_ = fopen(output_file.c_str(), "wb"); |
| 87 | EXPECT_TRUE(output_fp_ != NULL); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | RefFiles::~RefFiles() { |
| 92 | if (input_fp_) { |
| 93 | EXPECT_EQ(EOF, fgetc(input_fp_)); // Make sure that we reached the end. |
| 94 | fclose(input_fp_); |
| 95 | } |
| 96 | if (output_fp_) fclose(output_fp_); |
| 97 | } |
| 98 | |
| 99 | template<class T> |
| 100 | void RefFiles::ProcessReference(const T& test_results) { |
| 101 | WriteToFile(test_results); |
| 102 | ReadFromFileAndCompare(test_results); |
| 103 | } |
| 104 | |
| 105 | template<typename T, size_t n> |
| 106 | void RefFiles::ProcessReference(const T (&test_results)[n], size_t length) { |
| 107 | WriteToFile(test_results, length); |
| 108 | ReadFromFileAndCompare(test_results, length); |
| 109 | } |
| 110 | |
| 111 | template<typename T, size_t n> |
| 112 | void RefFiles::WriteToFile(const T (&test_results)[n], size_t length) { |
| 113 | if (output_fp_) { |
| 114 | ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | template<typename T, size_t n> |
| 119 | void RefFiles::ReadFromFileAndCompare(const T (&test_results)[n], |
| 120 | size_t length) { |
| 121 | if (input_fp_) { |
| 122 | // Read from ref file. |
| 123 | T* ref = new T[length]; |
| 124 | ASSERT_EQ(length, fread(ref, sizeof(T), length, input_fp_)); |
| 125 | // Compare |
| 126 | ASSERT_EQ(0, memcmp(&test_results, ref, sizeof(T) * length)); |
| 127 | delete [] ref; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void RefFiles::WriteToFile(const NetEqNetworkStatistics& stats) { |
| 132 | if (output_fp_) { |
| 133 | ASSERT_EQ(1u, fwrite(&stats, sizeof(NetEqNetworkStatistics), 1, |
| 134 | output_fp_)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void RefFiles::ReadFromFileAndCompare( |
| 139 | const NetEqNetworkStatistics& stats) { |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 140 | // TODO(minyue): Update resource/audio_coding/neteq_network_stats.dat and |
| 141 | // resource/audio_coding/neteq_network_stats_win32.dat. |
| 142 | struct NetEqNetworkStatisticsOld { |
| 143 | uint16_t current_buffer_size_ms; // Current jitter buffer size in ms. |
| 144 | uint16_t preferred_buffer_size_ms; // Target buffer size in ms. |
| 145 | uint16_t jitter_peaks_found; // 1 if adding extra delay due to peaky |
| 146 | // jitter; 0 otherwise. |
| 147 | uint16_t packet_loss_rate; // Loss rate (network + late) in Q14. |
| 148 | uint16_t packet_discard_rate; // Late loss rate in Q14. |
| 149 | uint16_t expand_rate; // Fraction (of original stream) of synthesized |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 150 | // audio inserted through expansion (in Q14). |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 151 | uint16_t preemptive_rate; // Fraction of data inserted through pre-emptive |
| 152 | // expansion (in Q14). |
| 153 | uint16_t accelerate_rate; // Fraction of data removed through acceleration |
| 154 | // (in Q14). |
| 155 | int32_t clockdrift_ppm; // Average clock-drift in parts-per-million |
| 156 | // (positive or negative). |
| 157 | int added_zero_samples; // Number of zero samples added in "off" mode. |
| 158 | }; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 159 | if (input_fp_) { |
| 160 | // Read from ref file. |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 161 | size_t stat_size = sizeof(NetEqNetworkStatisticsOld); |
| 162 | NetEqNetworkStatisticsOld ref_stats; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 163 | ASSERT_EQ(1u, fread(&ref_stats, stat_size, 1, input_fp_)); |
| 164 | // Compare |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 165 | ASSERT_EQ(stats.current_buffer_size_ms, ref_stats.current_buffer_size_ms); |
| 166 | ASSERT_EQ(stats.preferred_buffer_size_ms, |
| 167 | ref_stats.preferred_buffer_size_ms); |
| 168 | ASSERT_EQ(stats.jitter_peaks_found, ref_stats.jitter_peaks_found); |
| 169 | ASSERT_EQ(stats.packet_loss_rate, ref_stats.packet_loss_rate); |
| 170 | ASSERT_EQ(stats.packet_discard_rate, ref_stats.packet_discard_rate); |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 171 | ASSERT_EQ(stats.expand_rate, ref_stats.expand_rate); |
minyue@webrtc.org | 2c1bcf2 | 2015-02-17 10:17:09 +0000 | [diff] [blame] | 172 | ASSERT_EQ(stats.preemptive_rate, ref_stats.preemptive_rate); |
| 173 | ASSERT_EQ(stats.accelerate_rate, ref_stats.accelerate_rate); |
| 174 | ASSERT_EQ(stats.clockdrift_ppm, ref_stats.clockdrift_ppm); |
| 175 | ASSERT_EQ(stats.added_zero_samples, ref_stats.added_zero_samples); |
| 176 | ASSERT_EQ(stats.secondary_decoded_rate, 0); |
minyue@webrtc.org | 7d721ee | 2015-02-18 10:01:53 +0000 | [diff] [blame] | 177 | ASSERT_LE(stats.speech_expand_rate, ref_stats.expand_rate); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | void RefFiles::WriteToFile(const RtcpStatistics& stats) { |
| 182 | if (output_fp_) { |
| 183 | ASSERT_EQ(1u, fwrite(&(stats.fraction_lost), sizeof(stats.fraction_lost), 1, |
| 184 | output_fp_)); |
| 185 | ASSERT_EQ(1u, fwrite(&(stats.cumulative_lost), |
| 186 | sizeof(stats.cumulative_lost), 1, output_fp_)); |
sprang@webrtc.org | fe5d36b | 2013-10-28 09:21:07 +0000 | [diff] [blame] | 187 | ASSERT_EQ(1u, fwrite(&(stats.extended_max_sequence_number), |
| 188 | sizeof(stats.extended_max_sequence_number), 1, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 189 | output_fp_)); |
| 190 | ASSERT_EQ(1u, fwrite(&(stats.jitter), sizeof(stats.jitter), 1, |
| 191 | output_fp_)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void RefFiles::ReadFromFileAndCompare( |
| 196 | const RtcpStatistics& stats) { |
| 197 | if (input_fp_) { |
| 198 | // Read from ref file. |
| 199 | RtcpStatistics ref_stats; |
| 200 | ASSERT_EQ(1u, fread(&(ref_stats.fraction_lost), |
| 201 | sizeof(ref_stats.fraction_lost), 1, input_fp_)); |
| 202 | ASSERT_EQ(1u, fread(&(ref_stats.cumulative_lost), |
| 203 | sizeof(ref_stats.cumulative_lost), 1, input_fp_)); |
sprang@webrtc.org | fe5d36b | 2013-10-28 09:21:07 +0000 | [diff] [blame] | 204 | ASSERT_EQ(1u, fread(&(ref_stats.extended_max_sequence_number), |
| 205 | sizeof(ref_stats.extended_max_sequence_number), 1, |
| 206 | input_fp_)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 207 | ASSERT_EQ(1u, fread(&(ref_stats.jitter), sizeof(ref_stats.jitter), 1, |
| 208 | input_fp_)); |
| 209 | // Compare |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 210 | ASSERT_EQ(ref_stats.fraction_lost, stats.fraction_lost); |
| 211 | ASSERT_EQ(ref_stats.cumulative_lost, stats.cumulative_lost); |
| 212 | ASSERT_EQ(ref_stats.extended_max_sequence_number, |
sprang@webrtc.org | fe5d36b | 2013-10-28 09:21:07 +0000 | [diff] [blame] | 213 | stats.extended_max_sequence_number); |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 214 | ASSERT_EQ(ref_stats.jitter, stats.jitter); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | class NetEqDecodingTest : public ::testing::Test { |
| 219 | protected: |
| 220 | // NetEQ must be polled for data once every 10 ms. Thus, neither of the |
| 221 | // constants below can be changed. |
| 222 | static const int kTimeStepMs = 10; |
| 223 | static const int kBlockSize8kHz = kTimeStepMs * 8; |
| 224 | static const int kBlockSize16kHz = kTimeStepMs * 16; |
| 225 | static const int kBlockSize32kHz = kTimeStepMs * 32; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 226 | static const size_t kMaxBlockSize = kBlockSize32kHz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 227 | static const int kInitSampleRateHz = 8000; |
| 228 | |
| 229 | NetEqDecodingTest(); |
| 230 | virtual void SetUp(); |
| 231 | virtual void TearDown(); |
| 232 | void SelectDecoders(NetEqDecoder* used_codec); |
| 233 | void LoadDecoders(); |
| 234 | void OpenInputFile(const std::string &rtp_file); |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 235 | void Process(int* out_len); |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 236 | void DecodeAndCompare(const std::string& rtp_file, |
| 237 | const std::string& ref_file, |
| 238 | const std::string& stat_ref_file, |
| 239 | const std::string& rtcp_ref_file); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 240 | static void PopulateRtpInfo(int frame_index, |
| 241 | int timestamp, |
| 242 | WebRtcRTPHeader* rtp_info); |
| 243 | static void PopulateCng(int frame_index, |
| 244 | int timestamp, |
| 245 | WebRtcRTPHeader* rtp_info, |
| 246 | uint8_t* payload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 247 | size_t* payload_len); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 248 | |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 249 | void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp, |
| 250 | const std::set<uint16_t>& drop_seq_numbers, |
| 251 | bool expect_seq_no_wrap, bool expect_timestamp_wrap); |
| 252 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 253 | void LongCngWithClockDrift(double drift_factor, |
| 254 | double network_freeze_ms, |
| 255 | bool pull_audio_during_freeze, |
| 256 | int delay_tolerance_ms, |
| 257 | int max_time_to_speech_ms); |
| 258 | |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 259 | void DuplicateCng(); |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 260 | |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 261 | uint32_t PlayoutTimestamp(); |
| 262 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 263 | NetEq* neteq_; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 264 | NetEq::Config config_; |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame^] | 265 | rtc::scoped_ptr<test::RtpFileSource> rtp_source_; |
| 266 | rtc::scoped_ptr<test::Packet> packet_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 267 | unsigned int sim_clock_; |
| 268 | int16_t out_data_[kMaxBlockSize]; |
| 269 | int output_sample_rate_; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 270 | int algorithmic_delay_ms_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | // Allocating the static const so that it can be passed by reference. |
| 274 | const int NetEqDecodingTest::kTimeStepMs; |
| 275 | const int NetEqDecodingTest::kBlockSize8kHz; |
| 276 | const int NetEqDecodingTest::kBlockSize16kHz; |
| 277 | const int NetEqDecodingTest::kBlockSize32kHz; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 278 | const size_t NetEqDecodingTest::kMaxBlockSize; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 279 | const int NetEqDecodingTest::kInitSampleRateHz; |
| 280 | |
| 281 | NetEqDecodingTest::NetEqDecodingTest() |
| 282 | : neteq_(NULL), |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 283 | config_(), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 284 | sim_clock_(0), |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 285 | output_sample_rate_(kInitSampleRateHz), |
| 286 | algorithmic_delay_ms_(0) { |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 287 | config_.sample_rate_hz = kInitSampleRateHz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 288 | memset(out_data_, 0, sizeof(out_data_)); |
| 289 | } |
| 290 | |
| 291 | void NetEqDecodingTest::SetUp() { |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 292 | neteq_ = NetEq::Create(config_); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 293 | NetEqNetworkStatistics stat; |
| 294 | ASSERT_EQ(0, neteq_->NetworkStatistics(&stat)); |
| 295 | algorithmic_delay_ms_ = stat.current_buffer_size_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 296 | ASSERT_TRUE(neteq_); |
| 297 | LoadDecoders(); |
| 298 | } |
| 299 | |
| 300 | void NetEqDecodingTest::TearDown() { |
| 301 | delete neteq_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void NetEqDecodingTest::LoadDecoders() { |
| 305 | // Load PCMu. |
| 306 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMu, 0)); |
| 307 | // Load PCMa. |
| 308 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCMa, 8)); |
henrike@webrtc.org | a950300b | 2013-07-08 18:53:54 +0000 | [diff] [blame] | 309 | #ifndef WEBRTC_ANDROID |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 310 | // Load iLBC. |
| 311 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderILBC, 102)); |
henrike@webrtc.org | a950300b | 2013-07-08 18:53:54 +0000 | [diff] [blame] | 312 | #endif // WEBRTC_ANDROID |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 313 | // Load iSAC. |
| 314 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, 103)); |
turaj@webrtc.org | 5272eb8 | 2013-11-23 00:11:32 +0000 | [diff] [blame] | 315 | #ifndef WEBRTC_ANDROID |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 316 | // Load iSAC SWB. |
| 317 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACswb, 104)); |
henrik.lundin@webrtc.org | ac59dba | 2013-01-31 09:55:24 +0000 | [diff] [blame] | 318 | // Load iSAC FB. |
| 319 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISACfb, 105)); |
turaj@webrtc.org | 5272eb8 | 2013-11-23 00:11:32 +0000 | [diff] [blame] | 320 | #endif // WEBRTC_ANDROID |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 321 | // Load PCM16B nb. |
| 322 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16B, 93)); |
| 323 | // Load PCM16B wb. |
| 324 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, 94)); |
| 325 | // Load PCM16B swb32. |
| 326 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bswb32kHz, 95)); |
| 327 | // Load CNG 8 kHz. |
| 328 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, 13)); |
| 329 | // Load CNG 16 kHz. |
| 330 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, 98)); |
| 331 | } |
| 332 | |
| 333 | void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) { |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 334 | rtp_source_.reset(test::RtpFileSource::Create(rtp_file)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 335 | } |
| 336 | |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 337 | void NetEqDecodingTest::Process(int* out_len) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 338 | // Check if time to receive. |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 339 | while (packet_ && sim_clock_ >= packet_->time_ms()) { |
| 340 | if (packet_->payload_length_bytes() > 0) { |
| 341 | WebRtcRTPHeader rtp_header; |
| 342 | packet_->ConvertHeader(&rtp_header); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 343 | ASSERT_EQ(0, neteq_->InsertPacket( |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 344 | rtp_header, packet_->payload(), |
| 345 | packet_->payload_length_bytes(), |
| 346 | packet_->time_ms() * (output_sample_rate_ / 1000))); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 347 | } |
| 348 | // Get next packet. |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 349 | packet_.reset(rtp_source_->NextPacket()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 350 | } |
| 351 | |
henrik.lundin@webrtc.org | e1d468c | 2013-01-30 07:37:20 +0000 | [diff] [blame] | 352 | // Get audio from NetEq. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 353 | NetEqOutputType type; |
| 354 | int num_channels; |
| 355 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, out_len, |
| 356 | &num_channels, &type)); |
| 357 | ASSERT_TRUE((*out_len == kBlockSize8kHz) || |
| 358 | (*out_len == kBlockSize16kHz) || |
| 359 | (*out_len == kBlockSize32kHz)); |
| 360 | output_sample_rate_ = *out_len / 10 * 1000; |
| 361 | |
| 362 | // Increase time. |
| 363 | sim_clock_ += kTimeStepMs; |
| 364 | } |
| 365 | |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 366 | void NetEqDecodingTest::DecodeAndCompare(const std::string& rtp_file, |
| 367 | const std::string& ref_file, |
| 368 | const std::string& stat_ref_file, |
| 369 | const std::string& rtcp_ref_file) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 370 | OpenInputFile(rtp_file); |
| 371 | |
| 372 | std::string ref_out_file = ""; |
| 373 | if (ref_file.empty()) { |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 374 | ref_out_file = webrtc::test::OutputPath() + "neteq_universal_ref.pcm"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 375 | } |
| 376 | RefFiles ref_files(ref_file, ref_out_file); |
| 377 | |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 378 | std::string stat_out_file = ""; |
| 379 | if (stat_ref_file.empty()) { |
| 380 | stat_out_file = webrtc::test::OutputPath() + "neteq_network_stats.dat"; |
| 381 | } |
| 382 | RefFiles network_stat_files(stat_ref_file, stat_out_file); |
| 383 | |
| 384 | std::string rtcp_out_file = ""; |
| 385 | if (rtcp_ref_file.empty()) { |
| 386 | rtcp_out_file = webrtc::test::OutputPath() + "neteq_rtcp_stats.dat"; |
| 387 | } |
| 388 | RefFiles rtcp_stat_files(rtcp_ref_file, rtcp_out_file); |
| 389 | |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 390 | packet_.reset(rtp_source_->NextPacket()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 391 | int i = 0; |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 392 | while (packet_) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 393 | std::ostringstream ss; |
| 394 | ss << "Lap number " << i++ << " in DecodeAndCompare while loop"; |
| 395 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
turaj@webrtc.org | 58cd316 | 2013-10-31 15:15:55 +0000 | [diff] [blame] | 396 | int out_len = 0; |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 397 | ASSERT_NO_FATAL_FAILURE(Process(&out_len)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 398 | ASSERT_NO_FATAL_FAILURE(ref_files.ProcessReference(out_data_, out_len)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 399 | |
| 400 | // Query the network statistics API once per second |
| 401 | if (sim_clock_ % 1000 == 0) { |
| 402 | // Process NetworkStatistics. |
| 403 | NetEqNetworkStatistics network_stats; |
| 404 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 405 | ASSERT_NO_FATAL_FAILURE( |
| 406 | network_stat_files.ProcessReference(network_stats)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 407 | |
| 408 | // Process RTCPstat. |
| 409 | RtcpStatistics rtcp_stats; |
| 410 | neteq_->GetRtcpStatistics(&rtcp_stats); |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 411 | ASSERT_NO_FATAL_FAILURE(rtcp_stat_files.ProcessReference(rtcp_stats)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void NetEqDecodingTest::PopulateRtpInfo(int frame_index, |
| 417 | int timestamp, |
| 418 | WebRtcRTPHeader* rtp_info) { |
| 419 | rtp_info->header.sequenceNumber = frame_index; |
| 420 | rtp_info->header.timestamp = timestamp; |
| 421 | rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC. |
| 422 | rtp_info->header.payloadType = 94; // PCM16b WB codec. |
| 423 | rtp_info->header.markerBit = 0; |
| 424 | } |
| 425 | |
| 426 | void NetEqDecodingTest::PopulateCng(int frame_index, |
| 427 | int timestamp, |
| 428 | WebRtcRTPHeader* rtp_info, |
| 429 | uint8_t* payload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 430 | size_t* payload_len) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 431 | rtp_info->header.sequenceNumber = frame_index; |
| 432 | rtp_info->header.timestamp = timestamp; |
| 433 | rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC. |
| 434 | rtp_info->header.payloadType = 98; // WB CNG. |
| 435 | rtp_info->header.markerBit = 0; |
| 436 | payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen. |
| 437 | *payload_len = 1; // Only noise level, no spectral parameters. |
| 438 | } |
| 439 | |
henrik.lundin@webrtc.org | 48438c2 | 2014-05-20 16:07:43 +0000 | [diff] [blame] | 440 | TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(TestBitExactness)) { |
andrew@webrtc.org | f6a638e | 2014-02-04 01:31:28 +0000 | [diff] [blame] | 441 | const std::string input_rtp_file = webrtc::test::ProjectRootPath() + |
henrik.lundin@webrtc.org | 73deaad | 2013-01-31 13:32:51 +0000 | [diff] [blame] | 442 | "resources/audio_coding/neteq_universal_new.rtp"; |
henrik.lundin@webrtc.org | 48438c2 | 2014-05-20 16:07:43 +0000 | [diff] [blame] | 443 | // Note that neteq4_universal_ref.pcm and neteq4_universal_ref_win_32.pcm |
| 444 | // are identical. The latter could have been removed, but if clients still |
| 445 | // have a copy of the file, the test will fail. |
andrew@webrtc.org | f6a638e | 2014-02-04 01:31:28 +0000 | [diff] [blame] | 446 | const std::string input_ref_file = |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 447 | webrtc::test::ResourcePath("audio_coding/neteq4_universal_ref", "pcm"); |
henrik.lundin@webrtc.org | 6e3968f | 2013-01-31 15:07:30 +0000 | [diff] [blame] | 448 | #if defined(_MSC_VER) && (_MSC_VER >= 1700) |
| 449 | // For Visual Studio 2012 and later, we will have to use the generic reference |
| 450 | // file, rather than the windows-specific one. |
andrew@webrtc.org | f6a638e | 2014-02-04 01:31:28 +0000 | [diff] [blame] | 451 | const std::string network_stat_ref_file = webrtc::test::ProjectRootPath() + |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 452 | "resources/audio_coding/neteq4_network_stats.dat"; |
henrik.lundin@webrtc.org | 6e3968f | 2013-01-31 15:07:30 +0000 | [diff] [blame] | 453 | #else |
andrew@webrtc.org | f6a638e | 2014-02-04 01:31:28 +0000 | [diff] [blame] | 454 | const std::string network_stat_ref_file = |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 455 | webrtc::test::ResourcePath("audio_coding/neteq4_network_stats", "dat"); |
henrik.lundin@webrtc.org | 6e3968f | 2013-01-31 15:07:30 +0000 | [diff] [blame] | 456 | #endif |
andrew@webrtc.org | f6a638e | 2014-02-04 01:31:28 +0000 | [diff] [blame] | 457 | const std::string rtcp_stat_ref_file = |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 458 | webrtc::test::ResourcePath("audio_coding/neteq4_rtcp_stats", "dat"); |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 459 | |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 460 | if (FLAGS_gen_ref) { |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 461 | DecodeAndCompare(input_rtp_file, "", "", ""); |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 462 | } else { |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 463 | DecodeAndCompare(input_rtp_file, |
| 464 | input_ref_file, |
| 465 | network_stat_ref_file, |
| 466 | rtcp_stat_ref_file); |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 467 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 468 | } |
| 469 | |
henrik.lundin@webrtc.org | 7cbc4f9 | 2014-10-07 06:37:39 +0000 | [diff] [blame] | 470 | // Use fax mode to avoid time-scaling. This is to simplify the testing of |
| 471 | // packet waiting times in the packet buffer. |
| 472 | class NetEqDecodingTestFaxMode : public NetEqDecodingTest { |
| 473 | protected: |
| 474 | NetEqDecodingTestFaxMode() : NetEqDecodingTest() { |
| 475 | config_.playout_mode = kPlayoutFax; |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 480 | // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio. |
| 481 | size_t num_frames = 30; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 482 | const size_t kSamples = 10 * 16; |
| 483 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 484 | for (size_t i = 0; i < num_frames; ++i) { |
| 485 | uint16_t payload[kSamples] = {0}; |
| 486 | WebRtcRTPHeader rtp_info; |
| 487 | rtp_info.header.sequenceNumber = i; |
| 488 | rtp_info.header.timestamp = i * kSamples; |
| 489 | rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC. |
| 490 | rtp_info.header.payloadType = 94; // PCM16b WB codec. |
| 491 | rtp_info.header.markerBit = 0; |
| 492 | ASSERT_EQ(0, neteq_->InsertPacket( |
| 493 | rtp_info, |
| 494 | reinterpret_cast<uint8_t*>(payload), |
| 495 | kPayloadBytes, 0)); |
| 496 | } |
| 497 | // Pull out all data. |
| 498 | for (size_t i = 0; i < num_frames; ++i) { |
| 499 | int out_len; |
| 500 | int num_channels; |
| 501 | NetEqOutputType type; |
| 502 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 503 | &num_channels, &type)); |
| 504 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 505 | } |
| 506 | |
| 507 | std::vector<int> waiting_times; |
| 508 | neteq_->WaitingTimes(&waiting_times); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 509 | EXPECT_EQ(num_frames, waiting_times.size()); |
| 510 | // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms |
| 511 | // spacing (per definition), we expect the delay to increase with 10 ms for |
| 512 | // each packet. |
| 513 | for (size_t i = 0; i < waiting_times.size(); ++i) { |
| 514 | EXPECT_EQ(static_cast<int>(i + 1) * 10, waiting_times[i]); |
| 515 | } |
| 516 | |
| 517 | // Check statistics again and make sure it's been reset. |
| 518 | neteq_->WaitingTimes(&waiting_times); |
turaj@webrtc.org | 58cd316 | 2013-10-31 15:15:55 +0000 | [diff] [blame] | 519 | int len = waiting_times.size(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 520 | EXPECT_EQ(0, len); |
| 521 | |
| 522 | // Process > 100 frames, and make sure that that we get statistics |
| 523 | // only for 100 frames. Note the new SSRC, causing NetEQ to reset. |
| 524 | num_frames = 110; |
| 525 | for (size_t i = 0; i < num_frames; ++i) { |
| 526 | uint16_t payload[kSamples] = {0}; |
| 527 | WebRtcRTPHeader rtp_info; |
| 528 | rtp_info.header.sequenceNumber = i; |
| 529 | rtp_info.header.timestamp = i * kSamples; |
| 530 | rtp_info.header.ssrc = 0x1235; // Just an arbitrary SSRC. |
| 531 | rtp_info.header.payloadType = 94; // PCM16b WB codec. |
| 532 | rtp_info.header.markerBit = 0; |
| 533 | ASSERT_EQ(0, neteq_->InsertPacket( |
| 534 | rtp_info, |
| 535 | reinterpret_cast<uint8_t*>(payload), |
| 536 | kPayloadBytes, 0)); |
| 537 | int out_len; |
| 538 | int num_channels; |
| 539 | NetEqOutputType type; |
| 540 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 541 | &num_channels, &type)); |
| 542 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 543 | } |
| 544 | |
| 545 | neteq_->WaitingTimes(&waiting_times); |
| 546 | EXPECT_EQ(100u, waiting_times.size()); |
| 547 | } |
| 548 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 549 | TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 550 | const int kNumFrames = 3000; // Needed for convergence. |
| 551 | int frame_index = 0; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 552 | const size_t kSamples = 10 * 16; |
| 553 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 554 | while (frame_index < kNumFrames) { |
| 555 | // Insert one packet each time, except every 10th time where we insert two |
| 556 | // packets at once. This will create a negative clock-drift of approx. 10%. |
| 557 | int num_packets = (frame_index % 10 == 0 ? 2 : 1); |
| 558 | for (int n = 0; n < num_packets; ++n) { |
| 559 | uint8_t payload[kPayloadBytes] = {0}; |
| 560 | WebRtcRTPHeader rtp_info; |
| 561 | PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info); |
| 562 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 563 | ++frame_index; |
| 564 | } |
| 565 | |
| 566 | // Pull out data once. |
| 567 | int out_len; |
| 568 | int num_channels; |
| 569 | NetEqOutputType type; |
| 570 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 571 | &num_channels, &type)); |
| 572 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 573 | } |
| 574 | |
| 575 | NetEqNetworkStatistics network_stats; |
| 576 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 577 | EXPECT_EQ(-103196, network_stats.clockdrift_ppm); |
| 578 | } |
| 579 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 580 | TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 581 | const int kNumFrames = 5000; // Needed for convergence. |
| 582 | int frame_index = 0; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 583 | const size_t kSamples = 10 * 16; |
| 584 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 585 | for (int i = 0; i < kNumFrames; ++i) { |
| 586 | // Insert one packet each time, except every 10th time where we don't insert |
| 587 | // any packet. This will create a positive clock-drift of approx. 11%. |
| 588 | int num_packets = (i % 10 == 9 ? 0 : 1); |
| 589 | for (int n = 0; n < num_packets; ++n) { |
| 590 | uint8_t payload[kPayloadBytes] = {0}; |
| 591 | WebRtcRTPHeader rtp_info; |
| 592 | PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info); |
| 593 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 594 | ++frame_index; |
| 595 | } |
| 596 | |
| 597 | // Pull out data once. |
| 598 | int out_len; |
| 599 | int num_channels; |
| 600 | NetEqOutputType type; |
| 601 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 602 | &num_channels, &type)); |
| 603 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 604 | } |
| 605 | |
| 606 | NetEqNetworkStatistics network_stats; |
| 607 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 608 | EXPECT_EQ(110946, network_stats.clockdrift_ppm); |
| 609 | } |
| 610 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 611 | void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor, |
| 612 | double network_freeze_ms, |
| 613 | bool pull_audio_during_freeze, |
| 614 | int delay_tolerance_ms, |
| 615 | int max_time_to_speech_ms) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 616 | uint16_t seq_no = 0; |
| 617 | uint32_t timestamp = 0; |
| 618 | const int kFrameSizeMs = 30; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 619 | const size_t kSamples = kFrameSizeMs * 16; |
| 620 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 621 | double next_input_time_ms = 0.0; |
| 622 | double t_ms; |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 623 | int out_len; |
| 624 | int num_channels; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 625 | NetEqOutputType type; |
| 626 | |
| 627 | // Insert speech for 5 seconds. |
| 628 | const int kSpeechDurationMs = 5000; |
| 629 | for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) { |
| 630 | // Each turn in this for loop is 10 ms. |
| 631 | while (next_input_time_ms <= t_ms) { |
| 632 | // Insert one 30 ms speech frame. |
| 633 | uint8_t payload[kPayloadBytes] = {0}; |
| 634 | WebRtcRTPHeader rtp_info; |
| 635 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
| 636 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 637 | ++seq_no; |
| 638 | timestamp += kSamples; |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 639 | next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 640 | } |
| 641 | // Pull out data once. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 642 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 643 | &num_channels, &type)); |
| 644 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 645 | } |
| 646 | |
| 647 | EXPECT_EQ(kOutputNormal, type); |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 648 | int32_t delay_before = timestamp - PlayoutTimestamp(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 649 | |
| 650 | // Insert CNG for 1 minute (= 60000 ms). |
| 651 | const int kCngPeriodMs = 100; |
| 652 | const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples. |
| 653 | const int kCngDurationMs = 60000; |
| 654 | for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) { |
| 655 | // Each turn in this for loop is 10 ms. |
| 656 | while (next_input_time_ms <= t_ms) { |
| 657 | // Insert one CNG frame each 100 ms. |
| 658 | uint8_t payload[kPayloadBytes]; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 659 | size_t payload_len; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 660 | WebRtcRTPHeader rtp_info; |
| 661 | PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); |
| 662 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0)); |
| 663 | ++seq_no; |
| 664 | timestamp += kCngPeriodSamples; |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 665 | next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 666 | } |
| 667 | // Pull out data once. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 668 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 669 | &num_channels, &type)); |
| 670 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 671 | } |
| 672 | |
| 673 | EXPECT_EQ(kOutputCNG, type); |
| 674 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 675 | if (network_freeze_ms > 0) { |
| 676 | // First keep pulling audio for |network_freeze_ms| without inserting |
| 677 | // any data, then insert CNG data corresponding to |network_freeze_ms| |
| 678 | // without pulling any output audio. |
| 679 | const double loop_end_time = t_ms + network_freeze_ms; |
| 680 | for (; t_ms < loop_end_time; t_ms += 10) { |
| 681 | // Pull out data once. |
| 682 | ASSERT_EQ(0, |
| 683 | neteq_->GetAudio( |
| 684 | kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); |
| 685 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 686 | EXPECT_EQ(kOutputCNG, type); |
| 687 | } |
| 688 | bool pull_once = pull_audio_during_freeze; |
| 689 | // If |pull_once| is true, GetAudio will be called once half-way through |
| 690 | // the network recovery period. |
| 691 | double pull_time_ms = (t_ms + next_input_time_ms) / 2; |
| 692 | while (next_input_time_ms <= t_ms) { |
| 693 | if (pull_once && next_input_time_ms >= pull_time_ms) { |
| 694 | pull_once = false; |
| 695 | // Pull out data once. |
| 696 | ASSERT_EQ( |
| 697 | 0, |
| 698 | neteq_->GetAudio( |
| 699 | kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); |
| 700 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 701 | EXPECT_EQ(kOutputCNG, type); |
| 702 | t_ms += 10; |
| 703 | } |
| 704 | // Insert one CNG frame each 100 ms. |
| 705 | uint8_t payload[kPayloadBytes]; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 706 | size_t payload_len; |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 707 | WebRtcRTPHeader rtp_info; |
| 708 | PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); |
| 709 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0)); |
| 710 | ++seq_no; |
| 711 | timestamp += kCngPeriodSamples; |
| 712 | next_input_time_ms += kCngPeriodMs * drift_factor; |
| 713 | } |
| 714 | } |
| 715 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 716 | // Insert speech again until output type is speech. |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 717 | double speech_restart_time_ms = t_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 718 | while (type != kOutputNormal) { |
| 719 | // Each turn in this for loop is 10 ms. |
| 720 | while (next_input_time_ms <= t_ms) { |
| 721 | // Insert one 30 ms speech frame. |
| 722 | uint8_t payload[kPayloadBytes] = {0}; |
| 723 | WebRtcRTPHeader rtp_info; |
| 724 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
| 725 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 726 | ++seq_no; |
| 727 | timestamp += kSamples; |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 728 | next_input_time_ms += kFrameSizeMs * drift_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 729 | } |
| 730 | // Pull out data once. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 731 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 732 | &num_channels, &type)); |
| 733 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 734 | // Increase clock. |
| 735 | t_ms += 10; |
| 736 | } |
| 737 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 738 | // Check that the speech starts again within reasonable time. |
| 739 | double time_until_speech_returns_ms = t_ms - speech_restart_time_ms; |
| 740 | EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms); |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 741 | int32_t delay_after = timestamp - PlayoutTimestamp(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 742 | // Compare delay before and after, and make sure it differs less than 20 ms. |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 743 | EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16); |
| 744 | EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 745 | } |
| 746 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 747 | TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) { |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 748 | // Apply a clock drift of -25 ms / s (sender faster than receiver). |
| 749 | const double kDriftFactor = 1000.0 / (1000.0 + 25.0); |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 750 | const double kNetworkFreezeTimeMs = 0.0; |
| 751 | const bool kGetAudioDuringFreezeRecovery = false; |
| 752 | const int kDelayToleranceMs = 20; |
| 753 | const int kMaxTimeToSpeechMs = 100; |
| 754 | LongCngWithClockDrift(kDriftFactor, |
| 755 | kNetworkFreezeTimeMs, |
| 756 | kGetAudioDuringFreezeRecovery, |
| 757 | kDelayToleranceMs, |
| 758 | kMaxTimeToSpeechMs); |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 759 | } |
| 760 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 761 | TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) { |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 762 | // Apply a clock drift of +25 ms / s (sender slower than receiver). |
| 763 | const double kDriftFactor = 1000.0 / (1000.0 - 25.0); |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 764 | const double kNetworkFreezeTimeMs = 0.0; |
| 765 | const bool kGetAudioDuringFreezeRecovery = false; |
| 766 | const int kDelayToleranceMs = 20; |
| 767 | const int kMaxTimeToSpeechMs = 100; |
| 768 | LongCngWithClockDrift(kDriftFactor, |
| 769 | kNetworkFreezeTimeMs, |
| 770 | kGetAudioDuringFreezeRecovery, |
| 771 | kDelayToleranceMs, |
| 772 | kMaxTimeToSpeechMs); |
| 773 | } |
| 774 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 775 | TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 776 | // Apply a clock drift of -25 ms / s (sender faster than receiver). |
| 777 | const double kDriftFactor = 1000.0 / (1000.0 + 25.0); |
| 778 | const double kNetworkFreezeTimeMs = 5000.0; |
| 779 | const bool kGetAudioDuringFreezeRecovery = false; |
| 780 | const int kDelayToleranceMs = 50; |
| 781 | const int kMaxTimeToSpeechMs = 200; |
| 782 | LongCngWithClockDrift(kDriftFactor, |
| 783 | kNetworkFreezeTimeMs, |
| 784 | kGetAudioDuringFreezeRecovery, |
| 785 | kDelayToleranceMs, |
| 786 | kMaxTimeToSpeechMs); |
| 787 | } |
| 788 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 789 | TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 790 | // Apply a clock drift of +25 ms / s (sender slower than receiver). |
| 791 | const double kDriftFactor = 1000.0 / (1000.0 - 25.0); |
| 792 | const double kNetworkFreezeTimeMs = 5000.0; |
| 793 | const bool kGetAudioDuringFreezeRecovery = false; |
| 794 | const int kDelayToleranceMs = 20; |
| 795 | const int kMaxTimeToSpeechMs = 100; |
| 796 | LongCngWithClockDrift(kDriftFactor, |
| 797 | kNetworkFreezeTimeMs, |
| 798 | kGetAudioDuringFreezeRecovery, |
| 799 | kDelayToleranceMs, |
| 800 | kMaxTimeToSpeechMs); |
| 801 | } |
| 802 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 803 | TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 804 | // Apply a clock drift of +25 ms / s (sender slower than receiver). |
| 805 | const double kDriftFactor = 1000.0 / (1000.0 - 25.0); |
| 806 | const double kNetworkFreezeTimeMs = 5000.0; |
| 807 | const bool kGetAudioDuringFreezeRecovery = true; |
| 808 | const int kDelayToleranceMs = 20; |
| 809 | const int kMaxTimeToSpeechMs = 100; |
| 810 | LongCngWithClockDrift(kDriftFactor, |
| 811 | kNetworkFreezeTimeMs, |
| 812 | kGetAudioDuringFreezeRecovery, |
| 813 | kDelayToleranceMs, |
| 814 | kMaxTimeToSpeechMs); |
| 815 | } |
| 816 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 817 | TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 818 | const double kDriftFactor = 1.0; // No drift. |
| 819 | const double kNetworkFreezeTimeMs = 0.0; |
| 820 | const bool kGetAudioDuringFreezeRecovery = false; |
| 821 | const int kDelayToleranceMs = 10; |
| 822 | const int kMaxTimeToSpeechMs = 50; |
| 823 | LongCngWithClockDrift(kDriftFactor, |
| 824 | kNetworkFreezeTimeMs, |
| 825 | kGetAudioDuringFreezeRecovery, |
| 826 | kDelayToleranceMs, |
| 827 | kMaxTimeToSpeechMs); |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 828 | } |
| 829 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 830 | TEST_F(NetEqDecodingTest, UnknownPayloadType) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 831 | const size_t kPayloadBytes = 100; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 832 | uint8_t payload[kPayloadBytes] = {0}; |
| 833 | WebRtcRTPHeader rtp_info; |
| 834 | PopulateRtpInfo(0, 0, &rtp_info); |
| 835 | rtp_info.header.payloadType = 1; // Not registered as a decoder. |
| 836 | EXPECT_EQ(NetEq::kFail, |
| 837 | neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 838 | EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError()); |
| 839 | } |
| 840 | |
henrike@webrtc.org | a950300b | 2013-07-08 18:53:54 +0000 | [diff] [blame] | 841 | TEST_F(NetEqDecodingTest, DISABLED_ON_ANDROID(DecoderError)) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 842 | const size_t kPayloadBytes = 100; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 843 | uint8_t payload[kPayloadBytes] = {0}; |
| 844 | WebRtcRTPHeader rtp_info; |
| 845 | PopulateRtpInfo(0, 0, &rtp_info); |
| 846 | rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid. |
| 847 | EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 848 | NetEqOutputType type; |
| 849 | // Set all of |out_data_| to 1, and verify that it was set to 0 by the call |
| 850 | // to GetAudio. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 851 | for (size_t i = 0; i < kMaxBlockSize; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 852 | out_data_[i] = 1; |
| 853 | } |
| 854 | int num_channels; |
| 855 | int samples_per_channel; |
| 856 | EXPECT_EQ(NetEq::kFail, |
| 857 | neteq_->GetAudio(kMaxBlockSize, out_data_, |
| 858 | &samples_per_channel, &num_channels, &type)); |
| 859 | // Verify that there is a decoder error to check. |
| 860 | EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError()); |
| 861 | // Code 6730 is an iSAC error code. |
| 862 | EXPECT_EQ(6730, neteq_->LastDecoderError()); |
| 863 | // Verify that the first 160 samples are set to 0, and that the remaining |
| 864 | // samples are left unmodified. |
| 865 | static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate. |
| 866 | for (int i = 0; i < kExpectedOutputLength; ++i) { |
| 867 | std::ostringstream ss; |
| 868 | ss << "i = " << i; |
| 869 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
| 870 | EXPECT_EQ(0, out_data_[i]); |
| 871 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 872 | for (size_t i = kExpectedOutputLength; i < kMaxBlockSize; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 873 | std::ostringstream ss; |
| 874 | ss << "i = " << i; |
| 875 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
| 876 | EXPECT_EQ(1, out_data_[i]); |
| 877 | } |
| 878 | } |
| 879 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 880 | TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 881 | NetEqOutputType type; |
| 882 | // Set all of |out_data_| to 1, and verify that it was set to 0 by the call |
| 883 | // to GetAudio. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 884 | for (size_t i = 0; i < kMaxBlockSize; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 885 | out_data_[i] = 1; |
| 886 | } |
| 887 | int num_channels; |
| 888 | int samples_per_channel; |
| 889 | EXPECT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, |
| 890 | &samples_per_channel, |
| 891 | &num_channels, &type)); |
| 892 | // Verify that the first block of samples is set to 0. |
| 893 | static const int kExpectedOutputLength = |
| 894 | kInitSampleRateHz / 100; // 10 ms at initial sample rate. |
| 895 | for (int i = 0; i < kExpectedOutputLength; ++i) { |
| 896 | std::ostringstream ss; |
| 897 | ss << "i = " << i; |
| 898 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
| 899 | EXPECT_EQ(0, out_data_[i]); |
| 900 | } |
| 901 | } |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 902 | |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 903 | class NetEqBgnTest : public NetEqDecodingTest { |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 904 | protected: |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 905 | virtual void TestCondition(double sum_squared_noise, |
| 906 | bool should_be_faded) = 0; |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 907 | |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 908 | void CheckBgn(int sampling_rate_hz) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 909 | int16_t expected_samples_per_channel = 0; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 910 | uint8_t payload_type = 0xFF; // Invalid. |
| 911 | if (sampling_rate_hz == 8000) { |
| 912 | expected_samples_per_channel = kBlockSize8kHz; |
| 913 | payload_type = 93; // PCM 16, 8 kHz. |
| 914 | } else if (sampling_rate_hz == 16000) { |
| 915 | expected_samples_per_channel = kBlockSize16kHz; |
| 916 | payload_type = 94; // PCM 16, 16 kHZ. |
| 917 | } else if (sampling_rate_hz == 32000) { |
| 918 | expected_samples_per_channel = kBlockSize32kHz; |
| 919 | payload_type = 95; // PCM 16, 32 kHz. |
| 920 | } else { |
| 921 | ASSERT_TRUE(false); // Unsupported test case. |
| 922 | } |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 923 | |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 924 | NetEqOutputType type; |
| 925 | int16_t output[kBlockSize32kHz]; // Maximum size is chosen. |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 926 | test::AudioLoop input; |
| 927 | // We are using the same 32 kHz input file for all tests, regardless of |
| 928 | // |sampling_rate_hz|. The output may sound weird, but the test is still |
| 929 | // valid. |
| 930 | ASSERT_TRUE(input.Init( |
| 931 | webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), |
| 932 | 10 * sampling_rate_hz, // Max 10 seconds loop length. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 933 | static_cast<size_t>(expected_samples_per_channel))); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 934 | |
| 935 | // Payload of 10 ms of PCM16 32 kHz. |
| 936 | uint8_t payload[kBlockSize32kHz * sizeof(int16_t)]; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 937 | WebRtcRTPHeader rtp_info; |
| 938 | PopulateRtpInfo(0, 0, &rtp_info); |
| 939 | rtp_info.header.payloadType = payload_type; |
| 940 | |
| 941 | int number_channels = 0; |
| 942 | int samples_per_channel = 0; |
| 943 | |
| 944 | uint32_t receive_timestamp = 0; |
| 945 | for (int n = 0; n < 10; ++n) { // Insert few packets and get audio. |
kwiberg@webrtc.org | 648f5d6 | 2015-02-10 09:18:28 +0000 | [diff] [blame] | 946 | int16_t enc_len_bytes = WebRtcPcm16b_Encode( |
| 947 | input.GetNextBlock(), expected_samples_per_channel, payload); |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 948 | ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2); |
| 949 | |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 950 | number_channels = 0; |
| 951 | samples_per_channel = 0; |
| 952 | ASSERT_EQ(0, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 953 | neteq_->InsertPacket(rtp_info, payload, |
| 954 | static_cast<size_t>(enc_len_bytes), |
| 955 | receive_timestamp)); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 956 | ASSERT_EQ(0, |
| 957 | neteq_->GetAudio(kBlockSize32kHz, |
| 958 | output, |
| 959 | &samples_per_channel, |
| 960 | &number_channels, |
| 961 | &type)); |
| 962 | ASSERT_EQ(1, number_channels); |
| 963 | ASSERT_EQ(expected_samples_per_channel, samples_per_channel); |
| 964 | ASSERT_EQ(kOutputNormal, type); |
| 965 | |
| 966 | // Next packet. |
| 967 | rtp_info.header.timestamp += expected_samples_per_channel; |
| 968 | rtp_info.header.sequenceNumber++; |
| 969 | receive_timestamp += expected_samples_per_channel; |
| 970 | } |
| 971 | |
| 972 | number_channels = 0; |
| 973 | samples_per_channel = 0; |
| 974 | |
| 975 | // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull |
| 976 | // one frame without checking speech-type. This is the first frame pulled |
| 977 | // without inserting any packet, and might not be labeled as PLC. |
| 978 | ASSERT_EQ(0, |
| 979 | neteq_->GetAudio(kBlockSize32kHz, |
| 980 | output, |
| 981 | &samples_per_channel, |
| 982 | &number_channels, |
| 983 | &type)); |
| 984 | ASSERT_EQ(1, number_channels); |
| 985 | ASSERT_EQ(expected_samples_per_channel, samples_per_channel); |
| 986 | |
| 987 | // To be able to test the fading of background noise we need at lease to |
| 988 | // pull 611 frames. |
| 989 | const int kFadingThreshold = 611; |
| 990 | |
| 991 | // Test several CNG-to-PLC packet for the expected behavior. The number 20 |
| 992 | // is arbitrary, but sufficiently large to test enough number of frames. |
| 993 | const int kNumPlcToCngTestFrames = 20; |
| 994 | bool plc_to_cng = false; |
| 995 | for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) { |
| 996 | number_channels = 0; |
| 997 | samples_per_channel = 0; |
| 998 | memset(output, 1, sizeof(output)); // Set to non-zero. |
| 999 | ASSERT_EQ(0, |
| 1000 | neteq_->GetAudio(kBlockSize32kHz, |
| 1001 | output, |
| 1002 | &samples_per_channel, |
| 1003 | &number_channels, |
| 1004 | &type)); |
| 1005 | ASSERT_EQ(1, number_channels); |
| 1006 | ASSERT_EQ(expected_samples_per_channel, samples_per_channel); |
| 1007 | if (type == kOutputPLCtoCNG) { |
| 1008 | plc_to_cng = true; |
| 1009 | double sum_squared = 0; |
| 1010 | for (int k = 0; k < number_channels * samples_per_channel; ++k) |
| 1011 | sum_squared += output[k] * output[k]; |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1012 | TestCondition(sum_squared, n > kFadingThreshold); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 1013 | } else { |
| 1014 | EXPECT_EQ(kOutputPLC, type); |
| 1015 | } |
| 1016 | } |
| 1017 | EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred. |
| 1018 | } |
| 1019 | }; |
| 1020 | |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1021 | class NetEqBgnTestOn : public NetEqBgnTest { |
| 1022 | protected: |
| 1023 | NetEqBgnTestOn() : NetEqBgnTest() { |
| 1024 | config_.background_noise_mode = NetEq::kBgnOn; |
| 1025 | } |
| 1026 | |
| 1027 | void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) { |
| 1028 | EXPECT_NE(0, sum_squared_noise); |
| 1029 | } |
| 1030 | }; |
| 1031 | |
| 1032 | class NetEqBgnTestOff : public NetEqBgnTest { |
| 1033 | protected: |
| 1034 | NetEqBgnTestOff() : NetEqBgnTest() { |
| 1035 | config_.background_noise_mode = NetEq::kBgnOff; |
| 1036 | } |
| 1037 | |
| 1038 | void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) { |
| 1039 | EXPECT_EQ(0, sum_squared_noise); |
| 1040 | } |
| 1041 | }; |
| 1042 | |
| 1043 | class NetEqBgnTestFade : public NetEqBgnTest { |
| 1044 | protected: |
| 1045 | NetEqBgnTestFade() : NetEqBgnTest() { |
| 1046 | config_.background_noise_mode = NetEq::kBgnFade; |
| 1047 | } |
| 1048 | |
| 1049 | void TestCondition(double sum_squared_noise, bool should_be_faded) { |
| 1050 | if (should_be_faded) |
| 1051 | EXPECT_EQ(0, sum_squared_noise); |
| 1052 | } |
| 1053 | }; |
| 1054 | |
| 1055 | TEST_F(NetEqBgnTestOn, RunTest) { |
| 1056 | CheckBgn(8000); |
| 1057 | CheckBgn(16000); |
| 1058 | CheckBgn(32000); |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 1059 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1060 | |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1061 | TEST_F(NetEqBgnTestOff, RunTest) { |
| 1062 | CheckBgn(8000); |
| 1063 | CheckBgn(16000); |
| 1064 | CheckBgn(32000); |
| 1065 | } |
| 1066 | |
| 1067 | TEST_F(NetEqBgnTestFade, RunTest) { |
| 1068 | CheckBgn(8000); |
| 1069 | CheckBgn(16000); |
| 1070 | CheckBgn(32000); |
| 1071 | } |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 1072 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 1073 | TEST_F(NetEqDecodingTest, SyncPacketInsert) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1074 | WebRtcRTPHeader rtp_info; |
| 1075 | uint32_t receive_timestamp = 0; |
| 1076 | // For the readability use the following payloads instead of the defaults of |
| 1077 | // this test. |
| 1078 | uint8_t kPcm16WbPayloadType = 1; |
| 1079 | uint8_t kCngNbPayloadType = 2; |
| 1080 | uint8_t kCngWbPayloadType = 3; |
| 1081 | uint8_t kCngSwb32PayloadType = 4; |
| 1082 | uint8_t kCngSwb48PayloadType = 5; |
| 1083 | uint8_t kAvtPayloadType = 6; |
| 1084 | uint8_t kRedPayloadType = 7; |
| 1085 | uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered. |
| 1086 | |
| 1087 | // Register decoders. |
| 1088 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderPCM16Bwb, |
| 1089 | kPcm16WbPayloadType)); |
| 1090 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGnb, kCngNbPayloadType)); |
| 1091 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGwb, kCngWbPayloadType)); |
| 1092 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb32kHz, |
| 1093 | kCngSwb32PayloadType)); |
| 1094 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderCNGswb48kHz, |
| 1095 | kCngSwb48PayloadType)); |
| 1096 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderAVT, kAvtPayloadType)); |
| 1097 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderRED, kRedPayloadType)); |
| 1098 | ASSERT_EQ(0, neteq_->RegisterPayloadType(kDecoderISAC, kIsacPayloadType)); |
| 1099 | |
| 1100 | PopulateRtpInfo(0, 0, &rtp_info); |
| 1101 | rtp_info.header.payloadType = kPcm16WbPayloadType; |
| 1102 | |
| 1103 | // The first packet injected cannot be sync-packet. |
| 1104 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1105 | |
| 1106 | // Payload length of 10 ms PCM16 16 kHz. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1107 | const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1108 | uint8_t payload[kPayloadBytes] = {0}; |
| 1109 | ASSERT_EQ(0, neteq_->InsertPacket( |
| 1110 | rtp_info, payload, kPayloadBytes, receive_timestamp)); |
| 1111 | |
| 1112 | // Next packet. Last packet contained 10 ms audio. |
| 1113 | rtp_info.header.sequenceNumber++; |
| 1114 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1115 | receive_timestamp += kBlockSize16kHz; |
| 1116 | |
| 1117 | // Unacceptable payload types CNG, AVT (DTMF), RED. |
| 1118 | rtp_info.header.payloadType = kCngNbPayloadType; |
| 1119 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1120 | |
| 1121 | rtp_info.header.payloadType = kCngWbPayloadType; |
| 1122 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1123 | |
| 1124 | rtp_info.header.payloadType = kCngSwb32PayloadType; |
| 1125 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1126 | |
| 1127 | rtp_info.header.payloadType = kCngSwb48PayloadType; |
| 1128 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1129 | |
| 1130 | rtp_info.header.payloadType = kAvtPayloadType; |
| 1131 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1132 | |
| 1133 | rtp_info.header.payloadType = kRedPayloadType; |
| 1134 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1135 | |
| 1136 | // Change of codec cannot be initiated with a sync packet. |
| 1137 | rtp_info.header.payloadType = kIsacPayloadType; |
| 1138 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1139 | |
| 1140 | // Change of SSRC is not allowed with a sync packet. |
| 1141 | rtp_info.header.payloadType = kPcm16WbPayloadType; |
| 1142 | ++rtp_info.header.ssrc; |
| 1143 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1144 | |
| 1145 | --rtp_info.header.ssrc; |
| 1146 | EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1147 | } |
| 1148 | |
| 1149 | // First insert several noise like packets, then sync-packets. Decoding all |
| 1150 | // packets should not produce error, statistics should not show any packet loss |
| 1151 | // and sync-packets should decode to zero. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1152 | // TODO(turajs) we will have a better test if we have a referece NetEq, and |
| 1153 | // when Sync packets are inserted in "test" NetEq we insert all-zero payload |
| 1154 | // in reference NetEq and compare the output of those two. |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 1155 | TEST_F(NetEqDecodingTest, SyncPacketDecode) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1156 | WebRtcRTPHeader rtp_info; |
| 1157 | PopulateRtpInfo(0, 0, &rtp_info); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1158 | const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1159 | uint8_t payload[kPayloadBytes]; |
| 1160 | int16_t decoded[kBlockSize16kHz]; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1161 | int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1162 | for (size_t n = 0; n < kPayloadBytes; ++n) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1163 | payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence. |
| 1164 | } |
| 1165 | // Insert some packets which decode to noise. We are not interested in |
| 1166 | // actual decoded values. |
| 1167 | NetEqOutputType output_type; |
| 1168 | int num_channels; |
| 1169 | int samples_per_channel; |
| 1170 | uint32_t receive_timestamp = 0; |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1171 | for (int n = 0; n < 100; ++n) { |
| 1172 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, |
| 1173 | receive_timestamp)); |
| 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 | |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1180 | rtp_info.header.sequenceNumber++; |
| 1181 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1182 | receive_timestamp += kBlockSize16kHz; |
| 1183 | } |
| 1184 | const int kNumSyncPackets = 10; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Make sure sufficient number of sync packets are inserted that we can |
| 1187 | // conduct a test. |
| 1188 | ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1189 | // Insert sync-packets, the decoded sequence should be all-zero. |
| 1190 | for (int n = 0; n < kNumSyncPackets; ++n) { |
| 1191 | ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1192 | ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, |
| 1193 | &samples_per_channel, &num_channels, |
| 1194 | &output_type)); |
| 1195 | ASSERT_EQ(kBlockSize16kHz, samples_per_channel); |
| 1196 | ASSERT_EQ(1, num_channels); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1197 | if (n > algorithmic_frame_delay) { |
| 1198 | EXPECT_TRUE(IsAllZero(decoded, samples_per_channel * num_channels)); |
| 1199 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1200 | rtp_info.header.sequenceNumber++; |
| 1201 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1202 | receive_timestamp += kBlockSize16kHz; |
| 1203 | } |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1204 | |
| 1205 | // We insert regular packets, if sync packet are not correctly buffered then |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1206 | // network statistics would show some packet loss. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1207 | for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) { |
| 1208 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, |
| 1209 | receive_timestamp)); |
| 1210 | ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, |
| 1211 | &samples_per_channel, &num_channels, |
| 1212 | &output_type)); |
| 1213 | if (n >= algorithmic_frame_delay + 1) { |
| 1214 | // Expect that this frame contain samples from regular RTP. |
| 1215 | EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels)); |
| 1216 | } |
| 1217 | rtp_info.header.sequenceNumber++; |
| 1218 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1219 | receive_timestamp += kBlockSize16kHz; |
| 1220 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1221 | NetEqNetworkStatistics network_stats; |
| 1222 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 1223 | // Expecting a "clean" network. |
| 1224 | EXPECT_EQ(0, network_stats.packet_loss_rate); |
| 1225 | EXPECT_EQ(0, network_stats.expand_rate); |
| 1226 | EXPECT_EQ(0, network_stats.accelerate_rate); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1227 | EXPECT_LE(network_stats.preemptive_rate, 150); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // Test if the size of the packet buffer reported correctly when containing |
| 1231 | // sync packets. Also, test if network packets override sync packets. That is to |
| 1232 | // prefer decoding a network packet to a sync packet, if both have same sequence |
| 1233 | // number and timestamp. |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 1234 | TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1235 | WebRtcRTPHeader rtp_info; |
| 1236 | PopulateRtpInfo(0, 0, &rtp_info); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1237 | const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1238 | uint8_t payload[kPayloadBytes]; |
| 1239 | int16_t decoded[kBlockSize16kHz]; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1240 | for (size_t n = 0; n < kPayloadBytes; ++n) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1241 | payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence. |
| 1242 | } |
| 1243 | // Insert some packets which decode to noise. We are not interested in |
| 1244 | // actual decoded values. |
| 1245 | NetEqOutputType output_type; |
| 1246 | int num_channels; |
| 1247 | int samples_per_channel; |
| 1248 | uint32_t receive_timestamp = 0; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1249 | int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1; |
| 1250 | for (int n = 0; n < algorithmic_frame_delay; ++n) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1251 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, |
| 1252 | receive_timestamp)); |
| 1253 | ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, |
| 1254 | &samples_per_channel, &num_channels, |
| 1255 | &output_type)); |
| 1256 | ASSERT_EQ(kBlockSize16kHz, samples_per_channel); |
| 1257 | ASSERT_EQ(1, num_channels); |
| 1258 | rtp_info.header.sequenceNumber++; |
| 1259 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1260 | receive_timestamp += kBlockSize16kHz; |
| 1261 | } |
| 1262 | const int kNumSyncPackets = 10; |
| 1263 | |
| 1264 | WebRtcRTPHeader first_sync_packet_rtp_info; |
| 1265 | memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info)); |
| 1266 | |
| 1267 | // Insert sync-packets, but no decoding. |
| 1268 | for (int n = 0; n < kNumSyncPackets; ++n) { |
| 1269 | ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1270 | rtp_info.header.sequenceNumber++; |
| 1271 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1272 | receive_timestamp += kBlockSize16kHz; |
| 1273 | } |
| 1274 | NetEqNetworkStatistics network_stats; |
| 1275 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1276 | EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_, |
| 1277 | network_stats.current_buffer_size_ms); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1278 | |
| 1279 | // Rewind |rtp_info| to that of the first sync packet. |
| 1280 | memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info)); |
| 1281 | |
| 1282 | // Insert. |
| 1283 | for (int n = 0; n < kNumSyncPackets; ++n) { |
| 1284 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, |
| 1285 | receive_timestamp)); |
| 1286 | rtp_info.header.sequenceNumber++; |
| 1287 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1288 | receive_timestamp += kBlockSize16kHz; |
| 1289 | } |
| 1290 | |
| 1291 | // Decode. |
| 1292 | for (int n = 0; n < kNumSyncPackets; ++n) { |
| 1293 | ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, |
| 1294 | &samples_per_channel, &num_channels, |
| 1295 | &output_type)); |
| 1296 | ASSERT_EQ(kBlockSize16kHz, samples_per_channel); |
| 1297 | ASSERT_EQ(1, num_channels); |
| 1298 | EXPECT_TRUE(IsAllNonZero(decoded, samples_per_channel * num_channels)); |
| 1299 | } |
| 1300 | } |
| 1301 | |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1302 | void NetEqDecodingTest::WrapTest(uint16_t start_seq_no, |
| 1303 | uint32_t start_timestamp, |
| 1304 | const std::set<uint16_t>& drop_seq_numbers, |
| 1305 | bool expect_seq_no_wrap, |
| 1306 | bool expect_timestamp_wrap) { |
| 1307 | uint16_t seq_no = start_seq_no; |
| 1308 | uint32_t timestamp = start_timestamp; |
| 1309 | const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame. |
| 1310 | const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs; |
| 1311 | const int kSamples = kBlockSize16kHz * kBlocksPerFrame; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1312 | const size_t kPayloadBytes = kSamples * sizeof(int16_t); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1313 | double next_input_time_ms = 0.0; |
| 1314 | int16_t decoded[kBlockSize16kHz]; |
| 1315 | int num_channels; |
| 1316 | int samples_per_channel; |
| 1317 | NetEqOutputType output_type; |
| 1318 | uint32_t receive_timestamp = 0; |
| 1319 | |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1320 | // Insert speech for 2 seconds. |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1321 | const int kSpeechDurationMs = 2000; |
| 1322 | int packets_inserted = 0; |
| 1323 | uint16_t last_seq_no; |
| 1324 | uint32_t last_timestamp; |
| 1325 | bool timestamp_wrapped = false; |
| 1326 | bool seq_no_wrapped = false; |
| 1327 | for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) { |
| 1328 | // Each turn in this for loop is 10 ms. |
| 1329 | while (next_input_time_ms <= t_ms) { |
| 1330 | // Insert one 30 ms speech frame. |
| 1331 | uint8_t payload[kPayloadBytes] = {0}; |
| 1332 | WebRtcRTPHeader rtp_info; |
| 1333 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
| 1334 | if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) { |
| 1335 | // This sequence number was not in the set to drop. Insert it. |
| 1336 | ASSERT_EQ(0, |
| 1337 | neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, |
| 1338 | receive_timestamp)); |
| 1339 | ++packets_inserted; |
| 1340 | } |
| 1341 | NetEqNetworkStatistics network_stats; |
| 1342 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 1343 | |
| 1344 | // Due to internal NetEq logic, preferred buffer-size is about 4 times the |
| 1345 | // packet size for first few packets. Therefore we refrain from checking |
| 1346 | // the criteria. |
| 1347 | if (packets_inserted > 4) { |
| 1348 | // Expect preferred and actual buffer size to be no more than 2 frames. |
| 1349 | EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1350 | EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 + |
| 1351 | algorithmic_delay_ms_); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1352 | } |
| 1353 | last_seq_no = seq_no; |
| 1354 | last_timestamp = timestamp; |
| 1355 | |
| 1356 | ++seq_no; |
| 1357 | timestamp += kSamples; |
| 1358 | receive_timestamp += kSamples; |
| 1359 | next_input_time_ms += static_cast<double>(kFrameSizeMs); |
| 1360 | |
| 1361 | seq_no_wrapped |= seq_no < last_seq_no; |
| 1362 | timestamp_wrapped |= timestamp < last_timestamp; |
| 1363 | } |
| 1364 | // Pull out data once. |
| 1365 | ASSERT_EQ(0, neteq_->GetAudio(kBlockSize16kHz, decoded, |
| 1366 | &samples_per_channel, &num_channels, |
| 1367 | &output_type)); |
| 1368 | ASSERT_EQ(kBlockSize16kHz, samples_per_channel); |
| 1369 | ASSERT_EQ(1, num_channels); |
| 1370 | |
| 1371 | // Expect delay (in samples) to be less than 2 packets. |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 1372 | EXPECT_LE(timestamp - PlayoutTimestamp(), |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1373 | static_cast<uint32_t>(kSamples * 2)); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1374 | } |
| 1375 | // Make sure we have actually tested wrap-around. |
| 1376 | ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped); |
| 1377 | ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped); |
| 1378 | } |
| 1379 | |
| 1380 | TEST_F(NetEqDecodingTest, SequenceNumberWrap) { |
| 1381 | // Start with a sequence number that will soon wrap. |
| 1382 | std::set<uint16_t> drop_seq_numbers; // Don't drop any packets. |
| 1383 | WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); |
| 1384 | } |
| 1385 | |
| 1386 | TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) { |
| 1387 | // Start with a sequence number that will soon wrap. |
| 1388 | std::set<uint16_t> drop_seq_numbers; |
| 1389 | drop_seq_numbers.insert(0xFFFF); |
| 1390 | drop_seq_numbers.insert(0x0); |
| 1391 | WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); |
| 1392 | } |
| 1393 | |
| 1394 | TEST_F(NetEqDecodingTest, TimestampWrap) { |
| 1395 | // Start with a timestamp that will soon wrap. |
| 1396 | std::set<uint16_t> drop_seq_numbers; |
| 1397 | WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true); |
| 1398 | } |
| 1399 | |
| 1400 | TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) { |
| 1401 | // Start with a timestamp and a sequence number that will wrap at the same |
| 1402 | // time. |
| 1403 | std::set<uint16_t> drop_seq_numbers; |
| 1404 | WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true); |
| 1405 | } |
| 1406 | |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1407 | void NetEqDecodingTest::DuplicateCng() { |
| 1408 | uint16_t seq_no = 0; |
| 1409 | uint32_t timestamp = 0; |
| 1410 | const int kFrameSizeMs = 10; |
| 1411 | const int kSampleRateKhz = 16; |
| 1412 | const int kSamples = kFrameSizeMs * kSampleRateKhz; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1413 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1414 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1415 | const int algorithmic_delay_samples = std::max( |
| 1416 | algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8); |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1417 | // Insert three speech packets. Three are needed to get the frame length |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1418 | // correct. |
| 1419 | int out_len; |
| 1420 | int num_channels; |
| 1421 | NetEqOutputType type; |
| 1422 | uint8_t payload[kPayloadBytes] = {0}; |
| 1423 | WebRtcRTPHeader rtp_info; |
| 1424 | for (int i = 0; i < 3; ++i) { |
| 1425 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
| 1426 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 1427 | ++seq_no; |
| 1428 | timestamp += kSamples; |
| 1429 | |
| 1430 | // Pull audio once. |
| 1431 | ASSERT_EQ(0, |
| 1432 | neteq_->GetAudio( |
| 1433 | kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); |
| 1434 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 1435 | } |
| 1436 | // Verify speech output. |
| 1437 | EXPECT_EQ(kOutputNormal, type); |
| 1438 | |
| 1439 | // Insert same CNG packet twice. |
| 1440 | const int kCngPeriodMs = 100; |
| 1441 | const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1442 | size_t payload_len; |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1443 | PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); |
| 1444 | // This is the first time this CNG packet is inserted. |
| 1445 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0)); |
| 1446 | |
| 1447 | // Pull audio once and make sure CNG is played. |
| 1448 | ASSERT_EQ(0, |
| 1449 | neteq_->GetAudio( |
| 1450 | kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); |
| 1451 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 1452 | EXPECT_EQ(kOutputCNG, type); |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 1453 | EXPECT_EQ(timestamp - algorithmic_delay_samples, PlayoutTimestamp()); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1454 | |
| 1455 | // Insert the same CNG packet again. Note that at this point it is old, since |
| 1456 | // we have already decoded the first copy of it. |
| 1457 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, payload_len, 0)); |
| 1458 | |
| 1459 | // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since |
| 1460 | // we have already pulled out CNG once. |
| 1461 | for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) { |
| 1462 | ASSERT_EQ(0, |
| 1463 | neteq_->GetAudio( |
| 1464 | kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); |
| 1465 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 1466 | EXPECT_EQ(kOutputCNG, type); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1467 | EXPECT_EQ(timestamp - algorithmic_delay_samples, |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 1468 | PlayoutTimestamp()); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | // Insert speech again. |
| 1472 | ++seq_no; |
| 1473 | timestamp += kCngPeriodSamples; |
| 1474 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
| 1475 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 1476 | |
| 1477 | // Pull audio once and verify that the output is speech again. |
| 1478 | ASSERT_EQ(0, |
| 1479 | neteq_->GetAudio( |
| 1480 | kMaxBlockSize, out_data_, &out_len, &num_channels, &type)); |
| 1481 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 1482 | EXPECT_EQ(kOutputNormal, type); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1483 | EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples, |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 1484 | PlayoutTimestamp()); |
| 1485 | } |
| 1486 | |
| 1487 | uint32_t NetEqDecodingTest::PlayoutTimestamp() { |
| 1488 | uint32_t playout_timestamp = 0; |
| 1489 | EXPECT_TRUE(neteq_->GetPlayoutTimestamp(&playout_timestamp)); |
| 1490 | return playout_timestamp; |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); } |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1494 | |
| 1495 | TEST_F(NetEqDecodingTest, CngFirst) { |
| 1496 | uint16_t seq_no = 0; |
| 1497 | uint32_t timestamp = 0; |
| 1498 | const int kFrameSizeMs = 10; |
| 1499 | const int kSampleRateKhz = 16; |
| 1500 | const int kSamples = kFrameSizeMs * kSampleRateKhz; |
| 1501 | const int kPayloadBytes = kSamples * 2; |
| 1502 | const int kCngPeriodMs = 100; |
| 1503 | const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz; |
| 1504 | size_t payload_len; |
| 1505 | |
| 1506 | uint8_t payload[kPayloadBytes] = {0}; |
| 1507 | WebRtcRTPHeader rtp_info; |
| 1508 | |
| 1509 | PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); |
| 1510 | ASSERT_EQ(NetEq::kOK, |
| 1511 | neteq_->InsertPacket(rtp_info, payload, payload_len, 0)); |
| 1512 | ++seq_no; |
| 1513 | timestamp += kCngPeriodSamples; |
| 1514 | |
| 1515 | // Pull audio once and make sure CNG is played. |
| 1516 | int out_len; |
| 1517 | int num_channels; |
| 1518 | NetEqOutputType type; |
| 1519 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 1520 | &num_channels, &type)); |
| 1521 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 1522 | EXPECT_EQ(kOutputCNG, type); |
| 1523 | |
| 1524 | // Insert some speech packets. |
| 1525 | for (int i = 0; i < 3; ++i) { |
| 1526 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
| 1527 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, kPayloadBytes, 0)); |
| 1528 | ++seq_no; |
| 1529 | timestamp += kSamples; |
| 1530 | |
| 1531 | // Pull audio once. |
| 1532 | ASSERT_EQ(0, neteq_->GetAudio(kMaxBlockSize, out_data_, &out_len, |
| 1533 | &num_channels, &type)); |
| 1534 | ASSERT_EQ(kBlockSize16kHz, out_len); |
| 1535 | } |
| 1536 | // Verify speech output. |
| 1537 | EXPECT_EQ(kOutputNormal, type); |
| 1538 | } |
| 1539 | |
henrik.lundin@webrtc.org | e7ce437 | 2014-01-09 14:01:55 +0000 | [diff] [blame] | 1540 | } // namespace webrtc |