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 | |
Henrik Kjellander | 7464089 | 2015-10-29 11:31:02 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/include/neteq.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 3ecc162 | 2014-03-07 15:23:34 +0000 | [diff] [blame] | 13 | #include <math.h> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
| 15 | #include <string.h> // memset |
| 16 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 17 | #include <algorithm> |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 18 | #include <memory> |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 19 | #include <set> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 23 | #include "gflags/gflags.h" |
kjellander@webrtc.org | 3c0aae1 | 2014-09-04 09:55:40 +0000 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 25 | #include "webrtc/base/sha1digest.h" |
| 26 | #include "webrtc/base/stringencode.h" |
ossu | e352578 | 2016-05-25 07:37:43 -0700 | [diff] [blame^] | 27 | #include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h" |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 28 | #include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h" |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 29 | #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h" |
kjellander@webrtc.org | 3c652b6 | 2015-11-18 23:07:57 +0100 | [diff] [blame] | 30 | #include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h" |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 31 | #include "webrtc/modules/include/module_common_types.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | #include "webrtc/test/testsupport/fileutils.h" |
| 33 | #include "webrtc/typedefs.h" |
| 34 | |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 35 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 36 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| 37 | #include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h" |
| 38 | #else |
| 39 | #include "webrtc/audio_coding/neteq/neteq_unittest.pb.h" |
| 40 | #endif |
| 41 | #endif |
| 42 | |
turaj@webrtc.org | a6101d7 | 2013-10-01 22:01:09 +0000 | [diff] [blame] | 43 | DEFINE_bool(gen_ref, false, "Generate reference files."); |
| 44 | |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 45 | namespace { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 46 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 47 | const std::string& PlatformChecksum(const std::string& checksum_general, |
| 48 | const std::string& checksum_android, |
| 49 | const std::string& checksum_win_32, |
| 50 | const std::string& checksum_win_64) { |
| 51 | #ifdef WEBRTC_ANDROID |
| 52 | return checksum_android; |
| 53 | #elif WEBRTC_WIN |
| 54 | #ifdef WEBRTC_ARCH_64_BITS |
| 55 | return checksum_win_64; |
| 56 | #else |
| 57 | return checksum_win_32; |
| 58 | #endif // WEBRTC_ARCH_64_BITS |
| 59 | #else |
| 60 | return checksum_general; |
| 61 | #endif // WEBRTC_WIN |
| 62 | } |
| 63 | |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 64 | bool IsAllZero(const int16_t* buf, size_t buf_length) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 65 | bool all_zero = true; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 66 | for (size_t n = 0; n < buf_length && all_zero; ++n) |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 67 | all_zero = buf[n] == 0; |
| 68 | return all_zero; |
| 69 | } |
| 70 | |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 71 | bool IsAllNonZero(const int16_t* buf, size_t buf_length) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 72 | bool all_non_zero = true; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 73 | for (size_t n = 0; n < buf_length && all_non_zero; ++n) |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 74 | all_non_zero = buf[n] != 0; |
| 75 | return all_non_zero; |
| 76 | } |
| 77 | |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 78 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 79 | void Convert(const webrtc::NetEqNetworkStatistics& stats_raw, |
| 80 | webrtc::neteq_unittest::NetEqNetworkStatistics* stats) { |
| 81 | stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms); |
| 82 | stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms); |
| 83 | stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found); |
| 84 | stats->set_packet_loss_rate(stats_raw.packet_loss_rate); |
| 85 | stats->set_packet_discard_rate(stats_raw.packet_discard_rate); |
| 86 | stats->set_expand_rate(stats_raw.expand_rate); |
| 87 | stats->set_speech_expand_rate(stats_raw.speech_expand_rate); |
| 88 | stats->set_preemptive_rate(stats_raw.preemptive_rate); |
| 89 | stats->set_accelerate_rate(stats_raw.accelerate_rate); |
| 90 | stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate); |
| 91 | stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm); |
| 92 | stats->set_added_zero_samples(stats_raw.added_zero_samples); |
| 93 | stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms); |
| 94 | stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms); |
| 95 | stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms); |
| 96 | stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms); |
| 97 | } |
| 98 | |
| 99 | void Convert(const webrtc::RtcpStatistics& stats_raw, |
| 100 | webrtc::neteq_unittest::RtcpStatistics* stats) { |
| 101 | stats->set_fraction_lost(stats_raw.fraction_lost); |
| 102 | stats->set_cumulative_lost(stats_raw.cumulative_lost); |
| 103 | stats->set_extended_max_sequence_number( |
| 104 | stats_raw.extended_max_sequence_number); |
| 105 | stats->set_jitter(stats_raw.jitter); |
| 106 | } |
| 107 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 108 | void AddMessage(FILE* file, rtc::MessageDigest* digest, |
| 109 | const std::string& message) { |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 110 | int32_t size = message.length(); |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 111 | if (file) |
| 112 | ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file)); |
| 113 | digest->Update(&size, sizeof(size)); |
| 114 | |
| 115 | if (file) |
| 116 | ASSERT_EQ(static_cast<size_t>(size), |
| 117 | fwrite(message.data(), sizeof(char), size, file)); |
| 118 | digest->Update(message.data(), sizeof(char) * size); |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 119 | } |
| 120 | |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 121 | #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT |
| 122 | |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 123 | void LoadDecoders(webrtc::NetEq* neteq) { |
| 124 | // Load PCMu. |
| 125 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMu, |
| 126 | "pcmu", 0)); |
| 127 | // Load PCMa. |
| 128 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa, |
| 129 | "pcma", 8)); |
| 130 | #ifdef WEBRTC_CODEC_ILBC |
| 131 | // Load iLBC. |
| 132 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderILBC, |
| 133 | "ilbc", 102)); |
| 134 | #endif |
| 135 | #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) |
| 136 | // Load iSAC. |
| 137 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISAC, |
| 138 | "isac", 103)); |
| 139 | #endif |
| 140 | #ifdef WEBRTC_CODEC_ISAC |
| 141 | // Load iSAC SWB. |
| 142 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISACswb, |
| 143 | "isac-swb", 104)); |
| 144 | #endif |
| 145 | #ifdef WEBRTC_CODEC_OPUS |
| 146 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderOpus, |
| 147 | "opus", 111)); |
| 148 | #endif |
| 149 | // Load PCM16B nb. |
| 150 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCM16B, |
| 151 | "pcm16-nb", 93)); |
| 152 | // Load PCM16B wb. |
| 153 | ASSERT_EQ(0, neteq->RegisterPayloadType( |
| 154 | webrtc::NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb", 94)); |
| 155 | // Load PCM16B swb32. |
| 156 | ASSERT_EQ( |
| 157 | 0, neteq->RegisterPayloadType( |
| 158 | webrtc::NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32", 95)); |
| 159 | // Load CNG 8 kHz. |
| 160 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGnb, |
| 161 | "cng-nb", 13)); |
| 162 | // Load CNG 16 kHz. |
| 163 | ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGwb, |
| 164 | "cng-wb", 98)); |
| 165 | } |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 166 | } // namespace |
| 167 | |
| 168 | namespace webrtc { |
| 169 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 170 | class ResultSink { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 171 | public: |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 172 | explicit ResultSink(const std::string& output_file); |
| 173 | ~ResultSink(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 174 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 175 | template<typename T, size_t n> void AddResult( |
| 176 | const T (&test_results)[n], |
| 177 | size_t length); |
| 178 | |
| 179 | void AddResult(const NetEqNetworkStatistics& stats); |
| 180 | void AddResult(const RtcpStatistics& stats); |
| 181 | |
| 182 | void VerifyChecksum(const std::string& ref_check_sum); |
| 183 | |
| 184 | private: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 185 | FILE* output_fp_; |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 186 | std::unique_ptr<rtc::MessageDigest> digest_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 189 | ResultSink::ResultSink(const std::string &output_file) |
| 190 | : output_fp_(nullptr), |
| 191 | digest_(new rtc::Sha1Digest()) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 192 | if (!output_file.empty()) { |
| 193 | output_fp_ = fopen(output_file.c_str(), "wb"); |
| 194 | EXPECT_TRUE(output_fp_ != NULL); |
| 195 | } |
| 196 | } |
| 197 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 198 | ResultSink::~ResultSink() { |
| 199 | if (output_fp_) |
| 200 | fclose(output_fp_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template<typename T, size_t n> |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 204 | void ResultSink::AddResult(const T (&test_results)[n], size_t length) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 205 | if (output_fp_) { |
| 206 | ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_)); |
| 207 | } |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 208 | digest_->Update(&test_results, sizeof(T) * length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 209 | } |
| 210 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 211 | void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) { |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 212 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 213 | neteq_unittest::NetEqNetworkStatistics stats; |
| 214 | Convert(stats_raw, &stats); |
| 215 | |
| 216 | std::string stats_string; |
| 217 | ASSERT_TRUE(stats.SerializeToString(&stats_string)); |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 218 | AddMessage(output_fp_, digest_.get(), stats_string); |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 219 | #else |
| 220 | FAIL() << "Writing to reference file requires Proto Buffer."; |
| 221 | #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 222 | } |
| 223 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 224 | void ResultSink::AddResult(const RtcpStatistics& stats_raw) { |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 225 | #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 226 | neteq_unittest::RtcpStatistics stats; |
| 227 | Convert(stats_raw, &stats); |
| 228 | |
| 229 | std::string stats_string; |
| 230 | ASSERT_TRUE(stats.SerializeToString(&stats_string)); |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 231 | AddMessage(output_fp_, digest_.get(), stats_string); |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 232 | #else |
| 233 | FAIL() << "Writing to reference file requires Proto Buffer."; |
| 234 | #endif // WEBRTC_NETEQ_UNITTEST_BITEXACT |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 235 | } |
| 236 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 237 | void ResultSink::VerifyChecksum(const std::string& checksum) { |
| 238 | std::vector<char> buffer; |
| 239 | buffer.resize(digest_->Size()); |
| 240 | digest_->Finish(&buffer[0], buffer.size()); |
| 241 | const std::string result = rtc::hex_encode(&buffer[0], digest_->Size()); |
| 242 | EXPECT_EQ(checksum, result); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | class NetEqDecodingTest : public ::testing::Test { |
| 246 | protected: |
| 247 | // NetEQ must be polled for data once every 10 ms. Thus, neither of the |
| 248 | // constants below can be changed. |
| 249 | static const int kTimeStepMs = 10; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 250 | static const size_t kBlockSize8kHz = kTimeStepMs * 8; |
| 251 | static const size_t kBlockSize16kHz = kTimeStepMs * 16; |
| 252 | static const size_t kBlockSize32kHz = kTimeStepMs * 32; |
minyue | 93c08b7 | 2015-12-22 09:57:41 -0800 | [diff] [blame] | 253 | static const size_t kBlockSize48kHz = kTimeStepMs * 48; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 254 | static const int kInitSampleRateHz = 8000; |
| 255 | |
| 256 | NetEqDecodingTest(); |
| 257 | virtual void SetUp(); |
| 258 | virtual void TearDown(); |
| 259 | void SelectDecoders(NetEqDecoder* used_codec); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 260 | void OpenInputFile(const std::string &rtp_file); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 261 | void Process(); |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 262 | |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 263 | void DecodeAndCompare(const std::string& rtp_file, |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 264 | const std::string& output_checksum, |
| 265 | const std::string& network_stats_checksum, |
| 266 | const std::string& rtcp_stats_checksum, |
| 267 | bool gen_ref); |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 268 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 269 | static void PopulateRtpInfo(int frame_index, |
| 270 | int timestamp, |
| 271 | WebRtcRTPHeader* rtp_info); |
| 272 | static void PopulateCng(int frame_index, |
| 273 | int timestamp, |
| 274 | WebRtcRTPHeader* rtp_info, |
| 275 | uint8_t* payload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 276 | size_t* payload_len); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 277 | |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 278 | void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp, |
| 279 | const std::set<uint16_t>& drop_seq_numbers, |
| 280 | bool expect_seq_no_wrap, bool expect_timestamp_wrap); |
| 281 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 282 | void LongCngWithClockDrift(double drift_factor, |
| 283 | double network_freeze_ms, |
| 284 | bool pull_audio_during_freeze, |
| 285 | int delay_tolerance_ms, |
| 286 | int max_time_to_speech_ms); |
| 287 | |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 288 | void DuplicateCng(); |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 289 | |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 290 | rtc::Optional<uint32_t> PlayoutTimestamp(); |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 291 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 292 | NetEq* neteq_; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 293 | NetEq::Config config_; |
kwiberg | 2d0c332 | 2016-02-14 09:28:33 -0800 | [diff] [blame] | 294 | std::unique_ptr<test::RtpFileSource> rtp_source_; |
| 295 | std::unique_ptr<test::Packet> packet_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 296 | unsigned int sim_clock_; |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 297 | AudioFrame out_frame_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 298 | int output_sample_rate_; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 299 | int algorithmic_delay_ms_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | // Allocating the static const so that it can be passed by reference. |
| 303 | const int NetEqDecodingTest::kTimeStepMs; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 304 | const size_t NetEqDecodingTest::kBlockSize8kHz; |
| 305 | const size_t NetEqDecodingTest::kBlockSize16kHz; |
| 306 | const size_t NetEqDecodingTest::kBlockSize32kHz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 307 | const int NetEqDecodingTest::kInitSampleRateHz; |
| 308 | |
| 309 | NetEqDecodingTest::NetEqDecodingTest() |
| 310 | : neteq_(NULL), |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 311 | config_(), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 312 | sim_clock_(0), |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 313 | output_sample_rate_(kInitSampleRateHz), |
| 314 | algorithmic_delay_ms_(0) { |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 315 | config_.sample_rate_hz = kInitSampleRateHz; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void NetEqDecodingTest::SetUp() { |
ossu | e352578 | 2016-05-25 07:37:43 -0700 | [diff] [blame^] | 319 | neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory()); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 320 | NetEqNetworkStatistics stat; |
| 321 | ASSERT_EQ(0, neteq_->NetworkStatistics(&stat)); |
| 322 | algorithmic_delay_ms_ = stat.current_buffer_size_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 323 | ASSERT_TRUE(neteq_); |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 324 | LoadDecoders(neteq_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void NetEqDecodingTest::TearDown() { |
| 328 | delete neteq_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 329 | } |
| 330 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 331 | void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) { |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 332 | rtp_source_.reset(test::RtpFileSource::Create(rtp_file)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 333 | } |
| 334 | |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 335 | void NetEqDecodingTest::Process() { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 336 | // Check if time to receive. |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 337 | while (packet_ && sim_clock_ >= packet_->time_ms()) { |
| 338 | if (packet_->payload_length_bytes() > 0) { |
| 339 | WebRtcRTPHeader rtp_header; |
| 340 | packet_->ConvertHeader(&rtp_header); |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 341 | #ifndef WEBRTC_CODEC_ISAC |
| 342 | // Ignore payload type 104 (iSAC-swb) if ISAC is not supported. |
| 343 | if (rtp_header.header.payloadType != 104) |
| 344 | #endif |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 345 | ASSERT_EQ(0, neteq_->InsertPacket( |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 346 | rtp_header, |
| 347 | rtc::ArrayView<const uint8_t>( |
| 348 | packet_->payload(), packet_->payload_length_bytes()), |
| 349 | static_cast<uint32_t>(packet_->time_ms() * |
| 350 | (output_sample_rate_ / 1000)))); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 351 | } |
| 352 | // Get next packet. |
henrik.lundin | 46ba49c | 2016-05-24 22:50:47 -0700 | [diff] [blame] | 353 | packet_ = rtp_source_->NextPacket(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 354 | } |
| 355 | |
henrik.lundin@webrtc.org | e1d468c | 2013-01-30 07:37:20 +0000 | [diff] [blame] | 356 | // Get audio from NetEq. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 357 | bool muted; |
| 358 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
| 359 | ASSERT_FALSE(muted); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 360 | ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) || |
| 361 | (out_frame_.samples_per_channel_ == kBlockSize16kHz) || |
| 362 | (out_frame_.samples_per_channel_ == kBlockSize32kHz) || |
| 363 | (out_frame_.samples_per_channel_ == kBlockSize48kHz)); |
| 364 | output_sample_rate_ = out_frame_.sample_rate_hz_; |
henrik.lundin | d89814b | 2015-11-23 06:49:25 -0800 | [diff] [blame] | 365 | EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 366 | |
| 367 | // Increase time. |
| 368 | sim_clock_ += kTimeStepMs; |
| 369 | } |
| 370 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 371 | void NetEqDecodingTest::DecodeAndCompare( |
| 372 | const std::string& rtp_file, |
| 373 | const std::string& output_checksum, |
| 374 | const std::string& network_stats_checksum, |
| 375 | const std::string& rtcp_stats_checksum, |
| 376 | bool gen_ref) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 377 | OpenInputFile(rtp_file); |
| 378 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 379 | std::string ref_out_file = |
| 380 | gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : ""; |
| 381 | ResultSink output(ref_out_file); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 382 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 383 | std::string stat_out_file = |
| 384 | gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : ""; |
| 385 | ResultSink network_stats(stat_out_file); |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 386 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 387 | std::string rtcp_out_file = |
| 388 | gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : ""; |
| 389 | ResultSink rtcp_stats(rtcp_out_file); |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 390 | |
henrik.lundin | 46ba49c | 2016-05-24 22:50:47 -0700 | [diff] [blame] | 391 | packet_ = rtp_source_->NextPacket(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 392 | int i = 0; |
henrik.lundin@webrtc.org | 966a708 | 2014-11-17 09:08:38 +0000 | [diff] [blame] | 393 | while (packet_) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 394 | std::ostringstream ss; |
| 395 | ss << "Lap number " << i++ << " in DecodeAndCompare while loop"; |
| 396 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 397 | ASSERT_NO_FATAL_FAILURE(Process()); |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 398 | ASSERT_NO_FATAL_FAILURE(output.AddResult( |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 399 | out_frame_.data_, out_frame_.samples_per_channel_)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 400 | |
| 401 | // Query the network statistics API once per second |
| 402 | if (sim_clock_ % 1000 == 0) { |
| 403 | // Process NetworkStatistics. |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 404 | NetEqNetworkStatistics current_network_stats; |
| 405 | ASSERT_EQ(0, neteq_->NetworkStatistics(¤t_network_stats)); |
| 406 | ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats)); |
| 407 | |
henrik.lundin | 9c3efd0 | 2015-08-27 13:12:22 -0700 | [diff] [blame] | 408 | // Compare with CurrentDelay, which should be identical. |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 409 | EXPECT_EQ(current_network_stats.current_buffer_size_ms, |
| 410 | neteq_->CurrentDelayMs()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 411 | |
| 412 | // Process RTCPstat. |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 413 | RtcpStatistics current_rtcp_stats; |
| 414 | neteq_->GetRtcpStatistics(¤t_rtcp_stats); |
| 415 | ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 418 | |
| 419 | SCOPED_TRACE("Check output audio."); |
| 420 | output.VerifyChecksum(output_checksum); |
| 421 | SCOPED_TRACE("Check network stats."); |
| 422 | network_stats.VerifyChecksum(network_stats_checksum); |
| 423 | SCOPED_TRACE("Check rtcp stats."); |
| 424 | rtcp_stats.VerifyChecksum(rtcp_stats_checksum); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void NetEqDecodingTest::PopulateRtpInfo(int frame_index, |
| 428 | int timestamp, |
| 429 | WebRtcRTPHeader* rtp_info) { |
| 430 | rtp_info->header.sequenceNumber = frame_index; |
| 431 | rtp_info->header.timestamp = timestamp; |
| 432 | rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC. |
| 433 | rtp_info->header.payloadType = 94; // PCM16b WB codec. |
| 434 | rtp_info->header.markerBit = 0; |
| 435 | } |
| 436 | |
| 437 | void NetEqDecodingTest::PopulateCng(int frame_index, |
| 438 | int timestamp, |
| 439 | WebRtcRTPHeader* rtp_info, |
| 440 | uint8_t* payload, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 441 | size_t* payload_len) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 442 | rtp_info->header.sequenceNumber = frame_index; |
| 443 | rtp_info->header.timestamp = timestamp; |
| 444 | rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC. |
| 445 | rtp_info->header.payloadType = 98; // WB CNG. |
| 446 | rtp_info->header.markerBit = 0; |
| 447 | payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen. |
| 448 | *payload_len = 1; // Only noise level, no spectral parameters. |
| 449 | } |
| 450 | |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 451 | #if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \ |
| 452 | (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \ |
| 453 | defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \ |
pbos | c7a6569 | 2016-05-06 12:50:04 -0700 | [diff] [blame] | 454 | !defined(WEBRTC_ARCH_ARM64) |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 455 | #define MAYBE_TestBitExactness TestBitExactness |
kwiberg | 98ab3a4 | 2015-09-30 21:54:21 -0700 | [diff] [blame] | 456 | #else |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 457 | #define MAYBE_TestBitExactness DISABLED_TestBitExactness |
kwiberg | 98ab3a4 | 2015-09-30 21:54:21 -0700 | [diff] [blame] | 458 | #endif |
minyue | 5f026d0 | 2015-12-16 07:36:04 -0800 | [diff] [blame] | 459 | TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) { |
minyue | 49c454e | 2016-01-08 11:30:14 -0800 | [diff] [blame] | 460 | const std::string input_rtp_file = |
| 461 | webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"); |
henrik.lundin@webrtc.org | 4e4b098 | 2014-08-11 14:48:49 +0000 | [diff] [blame] | 462 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 463 | const std::string output_checksum = PlatformChecksum( |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 464 | "472ebe1126f41fdb6b5c63c87f625a52e7604e49", |
| 465 | "d2a6b6ff54b340cf9f961c7f07768d86b3761073", |
| 466 | "472ebe1126f41fdb6b5c63c87f625a52e7604e49", |
| 467 | "f9749813dbc3fb59dae761de518fec65b8407c5b"); |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 468 | |
| 469 | const std::string network_stats_checksum = PlatformChecksum( |
| 470 | "2cf380a05ee07080bd72471e8ec7777a39644ec9", |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 471 | "01be67dc4c3b8e74743a45cbd8684c0535dec9ad", |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 472 | "2cf380a05ee07080bd72471e8ec7777a39644ec9", |
| 473 | "2cf380a05ee07080bd72471e8ec7777a39644ec9"); |
| 474 | |
| 475 | const std::string rtcp_stats_checksum = PlatformChecksum( |
| 476 | "b8880bf9fed2487efbddcb8d94b9937a29ae521d", |
| 477 | "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4", |
| 478 | "b8880bf9fed2487efbddcb8d94b9937a29ae521d", |
| 479 | "b8880bf9fed2487efbddcb8d94b9937a29ae521d"); |
| 480 | |
| 481 | DecodeAndCompare(input_rtp_file, |
| 482 | output_checksum, |
| 483 | network_stats_checksum, |
| 484 | rtcp_stats_checksum, |
| 485 | FLAGS_gen_ref); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 486 | } |
| 487 | |
minyue | 93c08b7 | 2015-12-22 09:57:41 -0800 | [diff] [blame] | 488 | #if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \ |
| 489 | defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \ |
pbos | c7a6569 | 2016-05-06 12:50:04 -0700 | [diff] [blame] | 490 | defined(WEBRTC_CODEC_OPUS) |
minyue | 93c08b7 | 2015-12-22 09:57:41 -0800 | [diff] [blame] | 491 | #define MAYBE_TestOpusBitExactness TestOpusBitExactness |
| 492 | #else |
| 493 | #define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness |
| 494 | #endif |
| 495 | TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) { |
| 496 | const std::string input_rtp_file = |
| 497 | webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp"); |
minyue | 93c08b7 | 2015-12-22 09:57:41 -0800 | [diff] [blame] | 498 | |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 499 | const std::string output_checksum = PlatformChecksum( |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 500 | "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4", |
| 501 | "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4", |
| 502 | "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4", |
| 503 | "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4"); |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 504 | |
| 505 | const std::string network_stats_checksum = PlatformChecksum( |
minyue | 53ff70f | 2016-05-02 01:50:30 -0700 | [diff] [blame] | 506 | "6eab76efbde753d4dde38983445ca16b4ce59b39", |
| 507 | "6eab76efbde753d4dde38983445ca16b4ce59b39", |
| 508 | "6eab76efbde753d4dde38983445ca16b4ce59b39", |
| 509 | "6eab76efbde753d4dde38983445ca16b4ce59b39"); |
minyue | 4f90677 | 2016-04-29 11:05:14 -0700 | [diff] [blame] | 510 | |
| 511 | const std::string rtcp_stats_checksum = PlatformChecksum( |
| 512 | "e37c797e3de6a64dda88c9ade7a013d022a2e1e0", |
| 513 | "e37c797e3de6a64dda88c9ade7a013d022a2e1e0", |
| 514 | "e37c797e3de6a64dda88c9ade7a013d022a2e1e0", |
| 515 | "e37c797e3de6a64dda88c9ade7a013d022a2e1e0"); |
| 516 | |
| 517 | DecodeAndCompare(input_rtp_file, |
| 518 | output_checksum, |
| 519 | network_stats_checksum, |
| 520 | rtcp_stats_checksum, |
| 521 | FLAGS_gen_ref); |
minyue | 93c08b7 | 2015-12-22 09:57:41 -0800 | [diff] [blame] | 522 | } |
| 523 | |
henrik.lundin@webrtc.org | 7cbc4f9 | 2014-10-07 06:37:39 +0000 | [diff] [blame] | 524 | // Use fax mode to avoid time-scaling. This is to simplify the testing of |
| 525 | // packet waiting times in the packet buffer. |
| 526 | class NetEqDecodingTestFaxMode : public NetEqDecodingTest { |
| 527 | protected: |
| 528 | NetEqDecodingTestFaxMode() : NetEqDecodingTest() { |
| 529 | config_.playout_mode = kPlayoutFax; |
| 530 | } |
| 531 | }; |
| 532 | |
| 533 | TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 534 | // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio. |
| 535 | size_t num_frames = 30; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 536 | const size_t kSamples = 10 * 16; |
| 537 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 538 | for (size_t i = 0; i < num_frames; ++i) { |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 539 | const uint8_t payload[kPayloadBytes] = {0}; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 540 | WebRtcRTPHeader rtp_info; |
| 541 | rtp_info.header.sequenceNumber = i; |
| 542 | rtp_info.header.timestamp = i * kSamples; |
| 543 | rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC. |
| 544 | rtp_info.header.payloadType = 94; // PCM16b WB codec. |
| 545 | rtp_info.header.markerBit = 0; |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 546 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 547 | } |
| 548 | // Pull out all data. |
| 549 | for (size_t i = 0; i < num_frames; ++i) { |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 550 | bool muted; |
| 551 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 552 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 555 | NetEqNetworkStatistics stats; |
| 556 | EXPECT_EQ(0, neteq_->NetworkStatistics(&stats)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 557 | // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms |
| 558 | // spacing (per definition), we expect the delay to increase with 10 ms for |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 559 | // each packet. Thus, we are calculating the statistics for a series from 10 |
| 560 | // to 300, in steps of 10 ms. |
| 561 | EXPECT_EQ(155, stats.mean_waiting_time_ms); |
| 562 | EXPECT_EQ(155, stats.median_waiting_time_ms); |
| 563 | EXPECT_EQ(10, stats.min_waiting_time_ms); |
| 564 | EXPECT_EQ(300, stats.max_waiting_time_ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 565 | |
| 566 | // Check statistics again and make sure it's been reset. |
Henrik Lundin | 1bb8cf8 | 2015-08-25 13:08:04 +0200 | [diff] [blame] | 567 | EXPECT_EQ(0, neteq_->NetworkStatistics(&stats)); |
| 568 | EXPECT_EQ(-1, stats.mean_waiting_time_ms); |
| 569 | EXPECT_EQ(-1, stats.median_waiting_time_ms); |
| 570 | EXPECT_EQ(-1, stats.min_waiting_time_ms); |
| 571 | EXPECT_EQ(-1, stats.max_waiting_time_ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 572 | } |
| 573 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 574 | TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 575 | const int kNumFrames = 3000; // Needed for convergence. |
| 576 | int frame_index = 0; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 577 | const size_t kSamples = 10 * 16; |
| 578 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 579 | while (frame_index < kNumFrames) { |
| 580 | // Insert one packet each time, except every 10th time where we insert two |
| 581 | // packets at once. This will create a negative clock-drift of approx. 10%. |
| 582 | int num_packets = (frame_index % 10 == 0 ? 2 : 1); |
| 583 | for (int n = 0; n < num_packets; ++n) { |
| 584 | uint8_t payload[kPayloadBytes] = {0}; |
| 585 | WebRtcRTPHeader rtp_info; |
| 586 | PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 587 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 588 | ++frame_index; |
| 589 | } |
| 590 | |
| 591 | // Pull out data once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 592 | bool muted; |
| 593 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 594 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | NetEqNetworkStatistics network_stats; |
| 598 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 599 | EXPECT_EQ(-103196, network_stats.clockdrift_ppm); |
| 600 | } |
| 601 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 602 | TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 603 | const int kNumFrames = 5000; // Needed for convergence. |
| 604 | int frame_index = 0; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 605 | const size_t kSamples = 10 * 16; |
| 606 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 607 | for (int i = 0; i < kNumFrames; ++i) { |
| 608 | // Insert one packet each time, except every 10th time where we don't insert |
| 609 | // any packet. This will create a positive clock-drift of approx. 11%. |
| 610 | int num_packets = (i % 10 == 9 ? 0 : 1); |
| 611 | for (int n = 0; n < num_packets; ++n) { |
| 612 | uint8_t payload[kPayloadBytes] = {0}; |
| 613 | WebRtcRTPHeader rtp_info; |
| 614 | PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 615 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 616 | ++frame_index; |
| 617 | } |
| 618 | |
| 619 | // Pull out data once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 620 | bool muted; |
| 621 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 622 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | NetEqNetworkStatistics network_stats; |
| 626 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 627 | EXPECT_EQ(110946, network_stats.clockdrift_ppm); |
| 628 | } |
| 629 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 630 | void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor, |
| 631 | double network_freeze_ms, |
| 632 | bool pull_audio_during_freeze, |
| 633 | int delay_tolerance_ms, |
| 634 | int max_time_to_speech_ms) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 635 | uint16_t seq_no = 0; |
| 636 | uint32_t timestamp = 0; |
| 637 | const int kFrameSizeMs = 30; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 638 | const size_t kSamples = kFrameSizeMs * 16; |
| 639 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 640 | double next_input_time_ms = 0.0; |
| 641 | double t_ms; |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 642 | bool muted; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 643 | |
| 644 | // Insert speech for 5 seconds. |
| 645 | const int kSpeechDurationMs = 5000; |
| 646 | for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) { |
| 647 | // Each turn in this for loop is 10 ms. |
| 648 | while (next_input_time_ms <= t_ms) { |
| 649 | // Insert one 30 ms speech frame. |
| 650 | uint8_t payload[kPayloadBytes] = {0}; |
| 651 | WebRtcRTPHeader rtp_info; |
| 652 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 653 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 654 | ++seq_no; |
| 655 | timestamp += kSamples; |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 656 | next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 657 | } |
| 658 | // Pull out data once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 659 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 660 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 661 | } |
| 662 | |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 663 | EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 664 | rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp(); |
| 665 | ASSERT_TRUE(playout_timestamp); |
| 666 | int32_t delay_before = timestamp - *playout_timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 667 | |
| 668 | // Insert CNG for 1 minute (= 60000 ms). |
| 669 | const int kCngPeriodMs = 100; |
| 670 | const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples. |
| 671 | const int kCngDurationMs = 60000; |
| 672 | for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) { |
| 673 | // Each turn in this for loop is 10 ms. |
| 674 | while (next_input_time_ms <= t_ms) { |
| 675 | // Insert one CNG frame each 100 ms. |
| 676 | uint8_t payload[kPayloadBytes]; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 677 | size_t payload_len; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 678 | WebRtcRTPHeader rtp_info; |
| 679 | PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 680 | ASSERT_EQ(0, neteq_->InsertPacket( |
| 681 | rtp_info, |
| 682 | rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 683 | ++seq_no; |
| 684 | timestamp += kCngPeriodSamples; |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 685 | next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 686 | } |
| 687 | // Pull out data once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 688 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 689 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 690 | } |
| 691 | |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 692 | EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 693 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 694 | if (network_freeze_ms > 0) { |
| 695 | // First keep pulling audio for |network_freeze_ms| without inserting |
| 696 | // any data, then insert CNG data corresponding to |network_freeze_ms| |
| 697 | // without pulling any output audio. |
| 698 | const double loop_end_time = t_ms + network_freeze_ms; |
| 699 | for (; t_ms < loop_end_time; t_ms += 10) { |
| 700 | // Pull out data once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 701 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 702 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 703 | EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 704 | } |
| 705 | bool pull_once = pull_audio_during_freeze; |
| 706 | // If |pull_once| is true, GetAudio will be called once half-way through |
| 707 | // the network recovery period. |
| 708 | double pull_time_ms = (t_ms + next_input_time_ms) / 2; |
| 709 | while (next_input_time_ms <= t_ms) { |
| 710 | if (pull_once && next_input_time_ms >= pull_time_ms) { |
| 711 | pull_once = false; |
| 712 | // Pull out data once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 713 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 714 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 715 | EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 716 | t_ms += 10; |
| 717 | } |
| 718 | // Insert one CNG frame each 100 ms. |
| 719 | uint8_t payload[kPayloadBytes]; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 720 | size_t payload_len; |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 721 | WebRtcRTPHeader rtp_info; |
| 722 | PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 723 | ASSERT_EQ(0, neteq_->InsertPacket( |
| 724 | rtp_info, |
| 725 | rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 726 | ++seq_no; |
| 727 | timestamp += kCngPeriodSamples; |
| 728 | next_input_time_ms += kCngPeriodMs * drift_factor; |
| 729 | } |
| 730 | } |
| 731 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 732 | // Insert speech again until output type is speech. |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 733 | double speech_restart_time_ms = t_ms; |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 734 | while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 735 | // Each turn in this for loop is 10 ms. |
| 736 | while (next_input_time_ms <= t_ms) { |
| 737 | // Insert one 30 ms speech frame. |
| 738 | uint8_t payload[kPayloadBytes] = {0}; |
| 739 | WebRtcRTPHeader rtp_info; |
| 740 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 741 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 742 | ++seq_no; |
| 743 | timestamp += kSamples; |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 744 | next_input_time_ms += kFrameSizeMs * drift_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 745 | } |
| 746 | // Pull out data once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 747 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 748 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 749 | // Increase clock. |
| 750 | t_ms += 10; |
| 751 | } |
| 752 | |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 753 | // Check that the speech starts again within reasonable time. |
| 754 | double time_until_speech_returns_ms = t_ms - speech_restart_time_ms; |
| 755 | EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms); |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 756 | playout_timestamp = PlayoutTimestamp(); |
| 757 | ASSERT_TRUE(playout_timestamp); |
| 758 | int32_t delay_after = timestamp - *playout_timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 759 | // 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] | 760 | EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16); |
| 761 | EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 762 | } |
| 763 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 764 | TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) { |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 765 | // Apply a clock drift of -25 ms / s (sender faster than receiver). |
| 766 | const double kDriftFactor = 1000.0 / (1000.0 + 25.0); |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 767 | const double kNetworkFreezeTimeMs = 0.0; |
| 768 | const bool kGetAudioDuringFreezeRecovery = false; |
| 769 | const int kDelayToleranceMs = 20; |
| 770 | const int kMaxTimeToSpeechMs = 100; |
| 771 | LongCngWithClockDrift(kDriftFactor, |
| 772 | kNetworkFreezeTimeMs, |
| 773 | kGetAudioDuringFreezeRecovery, |
| 774 | kDelayToleranceMs, |
| 775 | kMaxTimeToSpeechMs); |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 776 | } |
| 777 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 778 | TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) { |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 779 | // Apply a clock drift of +25 ms / s (sender slower than receiver). |
| 780 | const double kDriftFactor = 1000.0 / (1000.0 - 25.0); |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 781 | const double kNetworkFreezeTimeMs = 0.0; |
| 782 | const bool kGetAudioDuringFreezeRecovery = false; |
| 783 | const int kDelayToleranceMs = 20; |
| 784 | const int kMaxTimeToSpeechMs = 100; |
| 785 | LongCngWithClockDrift(kDriftFactor, |
| 786 | kNetworkFreezeTimeMs, |
| 787 | kGetAudioDuringFreezeRecovery, |
| 788 | kDelayToleranceMs, |
| 789 | kMaxTimeToSpeechMs); |
| 790 | } |
| 791 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 792 | TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 793 | // Apply a clock drift of -25 ms / s (sender faster than receiver). |
| 794 | const double kDriftFactor = 1000.0 / (1000.0 + 25.0); |
| 795 | const double kNetworkFreezeTimeMs = 5000.0; |
| 796 | const bool kGetAudioDuringFreezeRecovery = false; |
| 797 | const int kDelayToleranceMs = 50; |
| 798 | const int kMaxTimeToSpeechMs = 200; |
| 799 | LongCngWithClockDrift(kDriftFactor, |
| 800 | kNetworkFreezeTimeMs, |
| 801 | kGetAudioDuringFreezeRecovery, |
| 802 | kDelayToleranceMs, |
| 803 | kMaxTimeToSpeechMs); |
| 804 | } |
| 805 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 806 | TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 807 | // Apply a clock drift of +25 ms / s (sender slower than receiver). |
| 808 | const double kDriftFactor = 1000.0 / (1000.0 - 25.0); |
| 809 | const double kNetworkFreezeTimeMs = 5000.0; |
| 810 | const bool kGetAudioDuringFreezeRecovery = false; |
| 811 | const int kDelayToleranceMs = 20; |
| 812 | const int kMaxTimeToSpeechMs = 100; |
| 813 | LongCngWithClockDrift(kDriftFactor, |
| 814 | kNetworkFreezeTimeMs, |
| 815 | kGetAudioDuringFreezeRecovery, |
| 816 | kDelayToleranceMs, |
| 817 | kMaxTimeToSpeechMs); |
| 818 | } |
| 819 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 820 | TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 821 | // Apply a clock drift of +25 ms / s (sender slower than receiver). |
| 822 | const double kDriftFactor = 1000.0 / (1000.0 - 25.0); |
| 823 | const double kNetworkFreezeTimeMs = 5000.0; |
| 824 | const bool kGetAudioDuringFreezeRecovery = true; |
| 825 | const int kDelayToleranceMs = 20; |
| 826 | const int kMaxTimeToSpeechMs = 100; |
| 827 | LongCngWithClockDrift(kDriftFactor, |
| 828 | kNetworkFreezeTimeMs, |
| 829 | kGetAudioDuringFreezeRecovery, |
| 830 | kDelayToleranceMs, |
| 831 | kMaxTimeToSpeechMs); |
| 832 | } |
| 833 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 834 | TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) { |
henrik.lundin@webrtc.org | 24779fe | 2014-03-14 12:40:05 +0000 | [diff] [blame] | 835 | const double kDriftFactor = 1.0; // No drift. |
| 836 | const double kNetworkFreezeTimeMs = 0.0; |
| 837 | const bool kGetAudioDuringFreezeRecovery = false; |
| 838 | const int kDelayToleranceMs = 10; |
| 839 | const int kMaxTimeToSpeechMs = 50; |
| 840 | LongCngWithClockDrift(kDriftFactor, |
| 841 | kNetworkFreezeTimeMs, |
| 842 | kGetAudioDuringFreezeRecovery, |
| 843 | kDelayToleranceMs, |
| 844 | kMaxTimeToSpeechMs); |
henrik.lundin@webrtc.org | fcfc6a9 | 2014-02-13 11:42:28 +0000 | [diff] [blame] | 845 | } |
| 846 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 847 | TEST_F(NetEqDecodingTest, UnknownPayloadType) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 848 | const size_t kPayloadBytes = 100; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 849 | uint8_t payload[kPayloadBytes] = {0}; |
| 850 | WebRtcRTPHeader rtp_info; |
| 851 | PopulateRtpInfo(0, 0, &rtp_info); |
| 852 | rtp_info.header.payloadType = 1; // Not registered as a decoder. |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 853 | EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 854 | EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError()); |
| 855 | } |
| 856 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 857 | #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 858 | #define MAYBE_DecoderError DecoderError |
| 859 | #else |
| 860 | #define MAYBE_DecoderError DISABLED_DecoderError |
| 861 | #endif |
| 862 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 863 | TEST_F(NetEqDecodingTest, MAYBE_DecoderError) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 864 | const size_t kPayloadBytes = 100; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 865 | uint8_t payload[kPayloadBytes] = {0}; |
| 866 | WebRtcRTPHeader rtp_info; |
| 867 | PopulateRtpInfo(0, 0, &rtp_info); |
| 868 | rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid. |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 869 | EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 870 | // Set all of |out_data_| to 1, and verify that it was set to 0 by the call |
| 871 | // to GetAudio. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 872 | for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) { |
| 873 | out_frame_.data_[i] = 1; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 874 | } |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 875 | bool muted; |
| 876 | EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted)); |
| 877 | ASSERT_FALSE(muted); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 878 | // Verify that there is a decoder error to check. |
| 879 | EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError()); |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 880 | |
| 881 | enum NetEqDecoderError { |
| 882 | ISAC_LENGTH_MISMATCH = 6730, |
| 883 | ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640 |
| 884 | }; |
| 885 | #if defined(WEBRTC_CODEC_ISAC) |
| 886 | EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError()); |
| 887 | #elif defined(WEBRTC_CODEC_ISACFX) |
| 888 | EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError()); |
| 889 | #endif |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 890 | // Verify that the first 160 samples are set to 0, and that the remaining |
| 891 | // samples are left unmodified. |
| 892 | static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate. |
| 893 | for (int i = 0; i < kExpectedOutputLength; ++i) { |
| 894 | std::ostringstream ss; |
| 895 | ss << "i = " << i; |
| 896 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 897 | EXPECT_EQ(0, out_frame_.data_[i]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 898 | } |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 899 | for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples; |
| 900 | ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 901 | std::ostringstream ss; |
| 902 | ss << "i = " << i; |
| 903 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 904 | EXPECT_EQ(1, out_frame_.data_[i]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 908 | TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 909 | // Set all of |out_data_| to 1, and verify that it was set to 0 by the call |
| 910 | // to GetAudio. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 911 | for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) { |
| 912 | out_frame_.data_[i] = 1; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 913 | } |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 914 | bool muted; |
| 915 | EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
| 916 | ASSERT_FALSE(muted); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 917 | // Verify that the first block of samples is set to 0. |
| 918 | static const int kExpectedOutputLength = |
| 919 | kInitSampleRateHz / 100; // 10 ms at initial sample rate. |
| 920 | for (int i = 0; i < kExpectedOutputLength; ++i) { |
| 921 | std::ostringstream ss; |
| 922 | ss << "i = " << i; |
| 923 | SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 924 | EXPECT_EQ(0, out_frame_.data_[i]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 925 | } |
henrik.lundin | d89814b | 2015-11-23 06:49:25 -0800 | [diff] [blame] | 926 | // Verify that the sample rate did not change from the initial configuration. |
| 927 | EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 928 | } |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 929 | |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 930 | class NetEqBgnTest : public NetEqDecodingTest { |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 931 | protected: |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 932 | virtual void TestCondition(double sum_squared_noise, |
| 933 | bool should_be_faded) = 0; |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 934 | |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 935 | void CheckBgn(int sampling_rate_hz) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 936 | size_t expected_samples_per_channel = 0; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 937 | uint8_t payload_type = 0xFF; // Invalid. |
| 938 | if (sampling_rate_hz == 8000) { |
| 939 | expected_samples_per_channel = kBlockSize8kHz; |
| 940 | payload_type = 93; // PCM 16, 8 kHz. |
| 941 | } else if (sampling_rate_hz == 16000) { |
| 942 | expected_samples_per_channel = kBlockSize16kHz; |
| 943 | payload_type = 94; // PCM 16, 16 kHZ. |
| 944 | } else if (sampling_rate_hz == 32000) { |
| 945 | expected_samples_per_channel = kBlockSize32kHz; |
| 946 | payload_type = 95; // PCM 16, 32 kHz. |
| 947 | } else { |
| 948 | ASSERT_TRUE(false); // Unsupported test case. |
| 949 | } |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 950 | |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 951 | AudioFrame output; |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 952 | test::AudioLoop input; |
| 953 | // We are using the same 32 kHz input file for all tests, regardless of |
| 954 | // |sampling_rate_hz|. The output may sound weird, but the test is still |
| 955 | // valid. |
| 956 | ASSERT_TRUE(input.Init( |
| 957 | webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), |
| 958 | 10 * sampling_rate_hz, // Max 10 seconds loop length. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 959 | expected_samples_per_channel)); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 960 | |
| 961 | // Payload of 10 ms of PCM16 32 kHz. |
| 962 | uint8_t payload[kBlockSize32kHz * sizeof(int16_t)]; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 963 | WebRtcRTPHeader rtp_info; |
| 964 | PopulateRtpInfo(0, 0, &rtp_info); |
| 965 | rtp_info.header.payloadType = payload_type; |
| 966 | |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 967 | uint32_t receive_timestamp = 0; |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 968 | bool muted; |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 969 | for (int n = 0; n < 10; ++n) { // Insert few packets and get audio. |
kwiberg | 288886b | 2015-11-06 01:21:35 -0800 | [diff] [blame] | 970 | auto block = input.GetNextBlock(); |
| 971 | ASSERT_EQ(expected_samples_per_channel, block.size()); |
| 972 | size_t enc_len_bytes = |
| 973 | WebRtcPcm16b_Encode(block.data(), block.size(), payload); |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 974 | ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2); |
| 975 | |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 976 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>( |
| 977 | payload, enc_len_bytes), |
| 978 | receive_timestamp)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 979 | output.Reset(); |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 980 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 981 | ASSERT_EQ(1u, output.num_channels_); |
| 982 | ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 983 | ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 984 | |
| 985 | // Next packet. |
| 986 | rtp_info.header.timestamp += expected_samples_per_channel; |
| 987 | rtp_info.header.sequenceNumber++; |
| 988 | receive_timestamp += expected_samples_per_channel; |
| 989 | } |
| 990 | |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 991 | output.Reset(); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 992 | |
| 993 | // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull |
| 994 | // one frame without checking speech-type. This is the first frame pulled |
| 995 | // without inserting any packet, and might not be labeled as PLC. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 996 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 997 | ASSERT_EQ(1u, output.num_channels_); |
| 998 | ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 999 | |
| 1000 | // To be able to test the fading of background noise we need at lease to |
| 1001 | // pull 611 frames. |
| 1002 | const int kFadingThreshold = 611; |
| 1003 | |
| 1004 | // Test several CNG-to-PLC packet for the expected behavior. The number 20 |
| 1005 | // is arbitrary, but sufficiently large to test enough number of frames. |
| 1006 | const int kNumPlcToCngTestFrames = 20; |
| 1007 | bool plc_to_cng = false; |
| 1008 | for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) { |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1009 | output.Reset(); |
| 1010 | memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1011 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
| 1012 | ASSERT_FALSE(muted); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1013 | ASSERT_EQ(1u, output.num_channels_); |
| 1014 | ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1015 | if (output.speech_type_ == AudioFrame::kPLCCNG) { |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 1016 | plc_to_cng = true; |
| 1017 | double sum_squared = 0; |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1018 | for (size_t k = 0; |
| 1019 | k < output.num_channels_ * output.samples_per_channel_; ++k) |
| 1020 | sum_squared += output.data_[k] * output.data_[k]; |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1021 | TestCondition(sum_squared, n > kFadingThreshold); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 1022 | } else { |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1023 | EXPECT_EQ(AudioFrame::kPLC, output.speech_type_); |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred. |
| 1027 | } |
| 1028 | }; |
| 1029 | |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1030 | class NetEqBgnTestOn : public NetEqBgnTest { |
| 1031 | protected: |
| 1032 | NetEqBgnTestOn() : NetEqBgnTest() { |
| 1033 | config_.background_noise_mode = NetEq::kBgnOn; |
| 1034 | } |
| 1035 | |
| 1036 | void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) { |
| 1037 | EXPECT_NE(0, sum_squared_noise); |
| 1038 | } |
| 1039 | }; |
| 1040 | |
| 1041 | class NetEqBgnTestOff : public NetEqBgnTest { |
| 1042 | protected: |
| 1043 | NetEqBgnTestOff() : NetEqBgnTest() { |
| 1044 | config_.background_noise_mode = NetEq::kBgnOff; |
| 1045 | } |
| 1046 | |
| 1047 | void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) { |
| 1048 | EXPECT_EQ(0, sum_squared_noise); |
| 1049 | } |
| 1050 | }; |
| 1051 | |
| 1052 | class NetEqBgnTestFade : public NetEqBgnTest { |
| 1053 | protected: |
| 1054 | NetEqBgnTestFade() : NetEqBgnTest() { |
| 1055 | config_.background_noise_mode = NetEq::kBgnFade; |
| 1056 | } |
| 1057 | |
| 1058 | void TestCondition(double sum_squared_noise, bool should_be_faded) { |
| 1059 | if (should_be_faded) |
| 1060 | EXPECT_EQ(0, sum_squared_noise); |
| 1061 | } |
| 1062 | }; |
| 1063 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 1064 | TEST_F(NetEqBgnTestOn, RunTest) { |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1065 | CheckBgn(8000); |
| 1066 | CheckBgn(16000); |
| 1067 | CheckBgn(32000); |
turaj@webrtc.org | ff43c85 | 2013-09-25 00:07:27 +0000 | [diff] [blame] | 1068 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1069 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 1070 | TEST_F(NetEqBgnTestOff, RunTest) { |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1071 | CheckBgn(8000); |
| 1072 | CheckBgn(16000); |
| 1073 | CheckBgn(32000); |
| 1074 | } |
| 1075 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 1076 | TEST_F(NetEqBgnTestFade, RunTest) { |
henrik.lundin@webrtc.org | 9b8102c | 2014-08-21 08:27:44 +0000 | [diff] [blame] | 1077 | CheckBgn(8000); |
| 1078 | CheckBgn(16000); |
| 1079 | CheckBgn(32000); |
| 1080 | } |
henrik.lundin@webrtc.org | ea25784 | 2014-08-07 12:27:37 +0000 | [diff] [blame] | 1081 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 1082 | #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 1083 | #define MAYBE_SyncPacketInsert SyncPacketInsert |
| 1084 | #else |
| 1085 | #define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert |
| 1086 | #endif |
| 1087 | TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1088 | WebRtcRTPHeader rtp_info; |
| 1089 | uint32_t receive_timestamp = 0; |
| 1090 | // For the readability use the following payloads instead of the defaults of |
| 1091 | // this test. |
| 1092 | uint8_t kPcm16WbPayloadType = 1; |
| 1093 | uint8_t kCngNbPayloadType = 2; |
| 1094 | uint8_t kCngWbPayloadType = 3; |
| 1095 | uint8_t kCngSwb32PayloadType = 4; |
| 1096 | uint8_t kCngSwb48PayloadType = 5; |
| 1097 | uint8_t kAvtPayloadType = 6; |
| 1098 | uint8_t kRedPayloadType = 7; |
| 1099 | uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered. |
| 1100 | |
| 1101 | // Register decoders. |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1102 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 1103 | "pcm16-wb", kPcm16WbPayloadType)); |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1104 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 1105 | "cng-nb", kCngNbPayloadType)); |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1106 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 1107 | "cng-wb", kCngWbPayloadType)); |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1108 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 1109 | "cng-swb32", kCngSwb32PayloadType)); |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1110 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 1111 | "cng-swb48", kCngSwb48PayloadType)); |
| 1112 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt", |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1113 | kAvtPayloadType)); |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 1114 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red", |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1115 | kRedPayloadType)); |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 1116 | ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac", |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 1117 | kIsacPayloadType)); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1118 | |
| 1119 | PopulateRtpInfo(0, 0, &rtp_info); |
| 1120 | rtp_info.header.payloadType = kPcm16WbPayloadType; |
| 1121 | |
| 1122 | // The first packet injected cannot be sync-packet. |
| 1123 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1124 | |
| 1125 | // Payload length of 10 ms PCM16 16 kHz. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1126 | const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1127 | uint8_t payload[kPayloadBytes] = {0}; |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1128 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Next packet. Last packet contained 10 ms audio. |
| 1131 | rtp_info.header.sequenceNumber++; |
| 1132 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1133 | receive_timestamp += kBlockSize16kHz; |
| 1134 | |
| 1135 | // Unacceptable payload types CNG, AVT (DTMF), RED. |
| 1136 | rtp_info.header.payloadType = kCngNbPayloadType; |
| 1137 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1138 | |
| 1139 | rtp_info.header.payloadType = kCngWbPayloadType; |
| 1140 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1141 | |
| 1142 | rtp_info.header.payloadType = kCngSwb32PayloadType; |
| 1143 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1144 | |
| 1145 | rtp_info.header.payloadType = kCngSwb48PayloadType; |
| 1146 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1147 | |
| 1148 | rtp_info.header.payloadType = kAvtPayloadType; |
| 1149 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1150 | |
| 1151 | rtp_info.header.payloadType = kRedPayloadType; |
| 1152 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1153 | |
| 1154 | // Change of codec cannot be initiated with a sync packet. |
| 1155 | rtp_info.header.payloadType = kIsacPayloadType; |
| 1156 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1157 | |
| 1158 | // Change of SSRC is not allowed with a sync packet. |
| 1159 | rtp_info.header.payloadType = kPcm16WbPayloadType; |
| 1160 | ++rtp_info.header.ssrc; |
| 1161 | EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1162 | |
| 1163 | --rtp_info.header.ssrc; |
| 1164 | EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1165 | } |
| 1166 | |
| 1167 | // First insert several noise like packets, then sync-packets. Decoding all |
| 1168 | // packets should not produce error, statistics should not show any packet loss |
| 1169 | // and sync-packets should decode to zero. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1170 | // TODO(turajs) we will have a better test if we have a referece NetEq, and |
| 1171 | // when Sync packets are inserted in "test" NetEq we insert all-zero payload |
| 1172 | // in reference NetEq and compare the output of those two. |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 1173 | TEST_F(NetEqDecodingTest, SyncPacketDecode) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1174 | WebRtcRTPHeader rtp_info; |
| 1175 | PopulateRtpInfo(0, 0, &rtp_info); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1176 | const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1177 | uint8_t payload[kPayloadBytes]; |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1178 | AudioFrame output; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1179 | int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1180 | for (size_t n = 0; n < kPayloadBytes; ++n) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1181 | payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence. |
| 1182 | } |
| 1183 | // Insert some packets which decode to noise. We are not interested in |
| 1184 | // actual decoded values. |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1185 | uint32_t receive_timestamp = 0; |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1186 | bool muted; |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1187 | for (int n = 0; n < 100; ++n) { |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1188 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1189 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1190 | ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_); |
| 1191 | ASSERT_EQ(1u, output.num_channels_); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1192 | |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1193 | rtp_info.header.sequenceNumber++; |
| 1194 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1195 | receive_timestamp += kBlockSize16kHz; |
| 1196 | } |
| 1197 | const int kNumSyncPackets = 10; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Make sure sufficient number of sync packets are inserted that we can |
| 1200 | // conduct a test. |
| 1201 | ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1202 | // Insert sync-packets, the decoded sequence should be all-zero. |
| 1203 | for (int n = 0; n < kNumSyncPackets; ++n) { |
| 1204 | ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1205 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
| 1206 | ASSERT_FALSE(muted); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1207 | ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_); |
| 1208 | ASSERT_EQ(1u, output.num_channels_); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1209 | if (n > algorithmic_frame_delay) { |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1210 | EXPECT_TRUE(IsAllZero( |
| 1211 | output.data_, output.samples_per_channel_ * output.num_channels_)); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1212 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1213 | rtp_info.header.sequenceNumber++; |
| 1214 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1215 | receive_timestamp += kBlockSize16kHz; |
| 1216 | } |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1217 | |
| 1218 | // 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] | 1219 | // network statistics would show some packet loss. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1220 | for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) { |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1221 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1222 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
| 1223 | ASSERT_FALSE(muted); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1224 | if (n >= algorithmic_frame_delay + 1) { |
| 1225 | // Expect that this frame contain samples from regular RTP. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1226 | EXPECT_TRUE(IsAllNonZero( |
| 1227 | output.data_, output.samples_per_channel_ * output.num_channels_)); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1228 | } |
| 1229 | rtp_info.header.sequenceNumber++; |
| 1230 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1231 | receive_timestamp += kBlockSize16kHz; |
| 1232 | } |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1233 | NetEqNetworkStatistics network_stats; |
| 1234 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 1235 | // Expecting a "clean" network. |
| 1236 | EXPECT_EQ(0, network_stats.packet_loss_rate); |
| 1237 | EXPECT_EQ(0, network_stats.expand_rate); |
| 1238 | EXPECT_EQ(0, network_stats.accelerate_rate); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1239 | EXPECT_LE(network_stats.preemptive_rate, 150); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | // Test if the size of the packet buffer reported correctly when containing |
| 1243 | // sync packets. Also, test if network packets override sync packets. That is to |
| 1244 | // prefer decoding a network packet to a sync packet, if both have same sequence |
| 1245 | // number and timestamp. |
henrik.lundin@webrtc.org | b4e80e0 | 2014-05-15 07:14:00 +0000 | [diff] [blame] | 1246 | TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1247 | WebRtcRTPHeader rtp_info; |
| 1248 | PopulateRtpInfo(0, 0, &rtp_info); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1249 | const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1250 | uint8_t payload[kPayloadBytes]; |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1251 | AudioFrame output; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1252 | for (size_t n = 0; n < kPayloadBytes; ++n) { |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1253 | payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence. |
| 1254 | } |
| 1255 | // Insert some packets which decode to noise. We are not interested in |
| 1256 | // actual decoded values. |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1257 | uint32_t receive_timestamp = 0; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1258 | int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1; |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1259 | bool muted; |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1260 | for (int n = 0; n < algorithmic_frame_delay; ++n) { |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1261 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1262 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1263 | ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_); |
| 1264 | ASSERT_EQ(1u, output.num_channels_); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1265 | rtp_info.header.sequenceNumber++; |
| 1266 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1267 | receive_timestamp += kBlockSize16kHz; |
| 1268 | } |
| 1269 | const int kNumSyncPackets = 10; |
| 1270 | |
| 1271 | WebRtcRTPHeader first_sync_packet_rtp_info; |
| 1272 | memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info)); |
| 1273 | |
| 1274 | // Insert sync-packets, but no decoding. |
| 1275 | for (int n = 0; n < kNumSyncPackets; ++n) { |
| 1276 | ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp)); |
| 1277 | rtp_info.header.sequenceNumber++; |
| 1278 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1279 | receive_timestamp += kBlockSize16kHz; |
| 1280 | } |
| 1281 | NetEqNetworkStatistics network_stats; |
| 1282 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1283 | EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_, |
| 1284 | network_stats.current_buffer_size_ms); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Rewind |rtp_info| to that of the first sync packet. |
| 1287 | memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info)); |
| 1288 | |
| 1289 | // Insert. |
| 1290 | for (int n = 0; n < kNumSyncPackets; ++n) { |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1291 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1292 | rtp_info.header.sequenceNumber++; |
| 1293 | rtp_info.header.timestamp += kBlockSize16kHz; |
| 1294 | receive_timestamp += kBlockSize16kHz; |
| 1295 | } |
| 1296 | |
| 1297 | // Decode. |
| 1298 | for (int n = 0; n < kNumSyncPackets; ++n) { |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1299 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
| 1300 | ASSERT_FALSE(muted); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1301 | ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_); |
| 1302 | ASSERT_EQ(1u, output.num_channels_); |
| 1303 | EXPECT_TRUE(IsAllNonZero( |
| 1304 | output.data_, output.samples_per_channel_ * output.num_channels_)); |
turaj@webrtc.org | 7b75ac6 | 2013-09-26 00:27:56 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1308 | void NetEqDecodingTest::WrapTest(uint16_t start_seq_no, |
| 1309 | uint32_t start_timestamp, |
| 1310 | const std::set<uint16_t>& drop_seq_numbers, |
| 1311 | bool expect_seq_no_wrap, |
| 1312 | bool expect_timestamp_wrap) { |
| 1313 | uint16_t seq_no = start_seq_no; |
| 1314 | uint32_t timestamp = start_timestamp; |
| 1315 | const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame. |
| 1316 | const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs; |
| 1317 | const int kSamples = kBlockSize16kHz * kBlocksPerFrame; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1318 | const size_t kPayloadBytes = kSamples * sizeof(int16_t); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1319 | double next_input_time_ms = 0.0; |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1320 | uint32_t receive_timestamp = 0; |
| 1321 | |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1322 | // Insert speech for 2 seconds. |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1323 | const int kSpeechDurationMs = 2000; |
| 1324 | int packets_inserted = 0; |
| 1325 | uint16_t last_seq_no; |
| 1326 | uint32_t last_timestamp; |
| 1327 | bool timestamp_wrapped = false; |
| 1328 | bool seq_no_wrapped = false; |
| 1329 | for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) { |
| 1330 | // Each turn in this for loop is 10 ms. |
| 1331 | while (next_input_time_ms <= t_ms) { |
| 1332 | // Insert one 30 ms speech frame. |
| 1333 | uint8_t payload[kPayloadBytes] = {0}; |
| 1334 | WebRtcRTPHeader rtp_info; |
| 1335 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
| 1336 | if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) { |
| 1337 | // This sequence number was not in the set to drop. Insert it. |
| 1338 | ASSERT_EQ(0, |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1339 | neteq_->InsertPacket(rtp_info, payload, receive_timestamp)); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1340 | ++packets_inserted; |
| 1341 | } |
| 1342 | NetEqNetworkStatistics network_stats; |
| 1343 | ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats)); |
| 1344 | |
| 1345 | // Due to internal NetEq logic, preferred buffer-size is about 4 times the |
| 1346 | // packet size for first few packets. Therefore we refrain from checking |
| 1347 | // the criteria. |
| 1348 | if (packets_inserted > 4) { |
| 1349 | // Expect preferred and actual buffer size to be no more than 2 frames. |
| 1350 | EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1351 | EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 + |
| 1352 | algorithmic_delay_ms_); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1353 | } |
| 1354 | last_seq_no = seq_no; |
| 1355 | last_timestamp = timestamp; |
| 1356 | |
| 1357 | ++seq_no; |
| 1358 | timestamp += kSamples; |
| 1359 | receive_timestamp += kSamples; |
| 1360 | next_input_time_ms += static_cast<double>(kFrameSizeMs); |
| 1361 | |
| 1362 | seq_no_wrapped |= seq_no < last_seq_no; |
| 1363 | timestamp_wrapped |= timestamp < last_timestamp; |
| 1364 | } |
| 1365 | // Pull out data once. |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1366 | AudioFrame output; |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1367 | bool muted; |
| 1368 | ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1369 | ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_); |
| 1370 | ASSERT_EQ(1u, output.num_channels_); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1371 | |
| 1372 | // Expect delay (in samples) to be less than 2 packets. |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 1373 | rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp(); |
| 1374 | ASSERT_TRUE(playout_timestamp); |
| 1375 | EXPECT_LE(timestamp - *playout_timestamp, |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1376 | static_cast<uint32_t>(kSamples * 2)); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 1377 | } |
| 1378 | // Make sure we have actually tested wrap-around. |
| 1379 | ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped); |
| 1380 | ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped); |
| 1381 | } |
| 1382 | |
| 1383 | TEST_F(NetEqDecodingTest, SequenceNumberWrap) { |
| 1384 | // Start with a sequence number that will soon wrap. |
| 1385 | std::set<uint16_t> drop_seq_numbers; // Don't drop any packets. |
| 1386 | WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); |
| 1387 | } |
| 1388 | |
| 1389 | TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) { |
| 1390 | // Start with a sequence number that will soon wrap. |
| 1391 | std::set<uint16_t> drop_seq_numbers; |
| 1392 | drop_seq_numbers.insert(0xFFFF); |
| 1393 | drop_seq_numbers.insert(0x0); |
| 1394 | WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); |
| 1395 | } |
| 1396 | |
| 1397 | TEST_F(NetEqDecodingTest, TimestampWrap) { |
| 1398 | // Start with a timestamp that will soon wrap. |
| 1399 | std::set<uint16_t> drop_seq_numbers; |
| 1400 | WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true); |
| 1401 | } |
| 1402 | |
| 1403 | TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) { |
| 1404 | // Start with a timestamp and a sequence number that will wrap at the same |
| 1405 | // time. |
| 1406 | std::set<uint16_t> drop_seq_numbers; |
| 1407 | WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true); |
| 1408 | } |
| 1409 | |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1410 | void NetEqDecodingTest::DuplicateCng() { |
| 1411 | uint16_t seq_no = 0; |
| 1412 | uint32_t timestamp = 0; |
| 1413 | const int kFrameSizeMs = 10; |
| 1414 | const int kSampleRateKhz = 16; |
| 1415 | const int kSamples = kFrameSizeMs * kSampleRateKhz; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1416 | const size_t kPayloadBytes = kSamples * 2; |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1417 | |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1418 | const int algorithmic_delay_samples = std::max( |
| 1419 | algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8); |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1420 | // 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] | 1421 | // correct. |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1422 | uint8_t payload[kPayloadBytes] = {0}; |
| 1423 | WebRtcRTPHeader rtp_info; |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1424 | bool muted; |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1425 | for (int i = 0; i < 3; ++i) { |
| 1426 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1427 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1428 | ++seq_no; |
| 1429 | timestamp += kSamples; |
| 1430 | |
| 1431 | // Pull audio once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1432 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1433 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1434 | } |
| 1435 | // Verify speech output. |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1436 | EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1437 | |
| 1438 | // Insert same CNG packet twice. |
| 1439 | const int kCngPeriodMs = 100; |
| 1440 | const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 1441 | size_t payload_len; |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1442 | PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); |
| 1443 | // This is the first time this CNG packet is inserted. |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1444 | ASSERT_EQ( |
| 1445 | 0, neteq_->InsertPacket( |
| 1446 | rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1447 | |
| 1448 | // Pull audio once and make sure CNG is played. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1449 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1450 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1451 | EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 1452 | EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG. |
| 1453 | EXPECT_EQ(timestamp - algorithmic_delay_samples, |
| 1454 | out_frame_.timestamp_ + out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Insert the same CNG packet again. Note that at this point it is old, since |
| 1457 | // we have already decoded the first copy of it. |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1458 | ASSERT_EQ( |
| 1459 | 0, neteq_->InsertPacket( |
| 1460 | rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since |
| 1463 | // we have already pulled out CNG once. |
| 1464 | for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) { |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1465 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1466 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1467 | EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 1468 | EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG. |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1469 | EXPECT_EQ(timestamp - algorithmic_delay_samples, |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 1470 | out_frame_.timestamp_ + out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | // Insert speech again. |
| 1474 | ++seq_no; |
| 1475 | timestamp += kCngPeriodSamples; |
| 1476 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1477 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | ca8cb95 | 2014-03-12 10:26:52 +0000 | [diff] [blame] | 1478 | |
| 1479 | // Pull audio once and verify that the output is speech again. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1480 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1481 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1482 | EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 1483 | rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp(); |
| 1484 | ASSERT_TRUE(playout_timestamp); |
turaj@webrtc.org | 8d1cdaa | 2014-04-11 18:47:55 +0000 | [diff] [blame] | 1485 | EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples, |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 1486 | *playout_timestamp); |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
henrik.lundin | 0d96ab7 | 2016-04-06 12:28:26 -0700 | [diff] [blame] | 1489 | rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() { |
| 1490 | return neteq_->GetPlayoutTimestamp(); |
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); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1510 | ASSERT_EQ( |
| 1511 | NetEq::kOK, |
| 1512 | neteq_->InsertPacket( |
| 1513 | rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0)); |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1514 | ++seq_no; |
| 1515 | timestamp += kCngPeriodSamples; |
| 1516 | |
| 1517 | // Pull audio once and make sure CNG is played. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1518 | bool muted; |
| 1519 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1520 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1521 | EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1522 | |
| 1523 | // Insert some speech packets. |
| 1524 | for (int i = 0; i < 3; ++i) { |
| 1525 | PopulateRtpInfo(seq_no, timestamp, &rtp_info); |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 1526 | ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1527 | ++seq_no; |
| 1528 | timestamp += kSamples; |
| 1529 | |
| 1530 | // Pull audio once. |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1531 | ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 1532 | ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1533 | } |
| 1534 | // Verify speech output. |
henrik.lundin | 55480f5 | 2016-03-08 02:37:57 -0800 | [diff] [blame] | 1535 | EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); |
henrik.lundin@webrtc.org | c93437e | 2014-12-01 11:42:42 +0000 | [diff] [blame] | 1536 | } |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1537 | |
| 1538 | class NetEqDecodingTestWithMutedState : public NetEqDecodingTest { |
| 1539 | public: |
| 1540 | NetEqDecodingTestWithMutedState() : NetEqDecodingTest() { |
| 1541 | config_.enable_muted_state = true; |
| 1542 | } |
| 1543 | |
| 1544 | protected: |
| 1545 | static constexpr size_t kSamples = 10 * 16; |
| 1546 | static constexpr size_t kPayloadBytes = kSamples * 2; |
| 1547 | |
| 1548 | void InsertPacket(uint32_t rtp_timestamp) { |
| 1549 | uint8_t payload[kPayloadBytes] = {0}; |
| 1550 | WebRtcRTPHeader rtp_info; |
| 1551 | PopulateRtpInfo(0, rtp_timestamp, &rtp_info); |
| 1552 | EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
| 1553 | } |
| 1554 | |
| 1555 | bool GetAudioReturnMuted() { |
| 1556 | bool muted; |
| 1557 | EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
| 1558 | return muted; |
| 1559 | } |
| 1560 | |
| 1561 | void GetAudioUntilMuted() { |
| 1562 | while (!GetAudioReturnMuted()) { |
| 1563 | ASSERT_LT(counter_++, 1000) << "Test timed out"; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | void GetAudioUntilNormal() { |
| 1568 | bool muted = false; |
| 1569 | while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) { |
| 1570 | EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); |
| 1571 | ASSERT_LT(counter_++, 1000) << "Test timed out"; |
| 1572 | } |
| 1573 | EXPECT_FALSE(muted); |
| 1574 | } |
| 1575 | |
| 1576 | int counter_ = 0; |
| 1577 | }; |
| 1578 | |
| 1579 | // Verifies that NetEq goes in and out of muted state as expected. |
| 1580 | TEST_F(NetEqDecodingTestWithMutedState, MutedState) { |
| 1581 | // Insert one speech packet. |
| 1582 | InsertPacket(0); |
| 1583 | // Pull out audio once and expect it not to be muted. |
| 1584 | EXPECT_FALSE(GetAudioReturnMuted()); |
| 1585 | // Pull data until faded out. |
| 1586 | GetAudioUntilMuted(); |
| 1587 | |
| 1588 | // Verify that output audio is not written during muted mode. Other parameters |
| 1589 | // should be correct, though. |
| 1590 | AudioFrame new_frame; |
| 1591 | for (auto& d : new_frame.data_) { |
| 1592 | d = 17; |
| 1593 | } |
| 1594 | bool muted; |
| 1595 | EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted)); |
| 1596 | EXPECT_TRUE(muted); |
| 1597 | for (auto d : new_frame.data_) { |
| 1598 | EXPECT_EQ(17, d); |
| 1599 | } |
| 1600 | EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_, |
| 1601 | new_frame.timestamp_); |
| 1602 | EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_); |
| 1603 | EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_); |
| 1604 | EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_); |
| 1605 | EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_); |
| 1606 | EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_); |
| 1607 | |
| 1608 | // Insert new data. Timestamp is corrected for the time elapsed since the last |
| 1609 | // packet. Verify that normal operation resumes. |
| 1610 | InsertPacket(kSamples * counter_); |
| 1611 | GetAudioUntilNormal(); |
| 1612 | } |
| 1613 | |
| 1614 | // Verifies that NetEq goes out of muted state when given a delayed packet. |
| 1615 | TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) { |
| 1616 | // Insert one speech packet. |
| 1617 | InsertPacket(0); |
| 1618 | // Pull out audio once and expect it not to be muted. |
| 1619 | EXPECT_FALSE(GetAudioReturnMuted()); |
| 1620 | // Pull data until faded out. |
| 1621 | GetAudioUntilMuted(); |
| 1622 | // Insert new data. Timestamp is only corrected for the half of the time |
| 1623 | // elapsed since the last packet. That is, the new packet is delayed. Verify |
| 1624 | // that normal operation resumes. |
| 1625 | InsertPacket(kSamples * counter_ / 2); |
| 1626 | GetAudioUntilNormal(); |
| 1627 | } |
| 1628 | |
| 1629 | // Verifies that NetEq goes out of muted state when given a future packet. |
| 1630 | TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) { |
| 1631 | // Insert one speech packet. |
| 1632 | InsertPacket(0); |
| 1633 | // Pull out audio once and expect it not to be muted. |
| 1634 | EXPECT_FALSE(GetAudioReturnMuted()); |
| 1635 | // Pull data until faded out. |
| 1636 | GetAudioUntilMuted(); |
| 1637 | // Insert new data. Timestamp is over-corrected for the time elapsed since the |
| 1638 | // last packet. That is, the new packet is too early. Verify that normal |
| 1639 | // operation resumes. |
| 1640 | InsertPacket(kSamples * counter_ * 2); |
| 1641 | GetAudioUntilNormal(); |
| 1642 | } |
| 1643 | |
| 1644 | // Verifies that NetEq goes out of muted state when given an old packet. |
| 1645 | TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) { |
| 1646 | // Insert one speech packet. |
| 1647 | InsertPacket(0); |
| 1648 | // Pull out audio once and expect it not to be muted. |
| 1649 | EXPECT_FALSE(GetAudioReturnMuted()); |
| 1650 | // Pull data until faded out. |
| 1651 | GetAudioUntilMuted(); |
| 1652 | |
| 1653 | EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_); |
| 1654 | // Insert packet which is older than the first packet. |
| 1655 | InsertPacket(kSamples * (counter_ - 1000)); |
| 1656 | EXPECT_FALSE(GetAudioReturnMuted()); |
| 1657 | EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); |
| 1658 | } |
| 1659 | |
| 1660 | class NetEqDecodingTestTwoInstances : public NetEqDecodingTest { |
| 1661 | public: |
| 1662 | NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {} |
| 1663 | |
| 1664 | void SetUp() override { |
| 1665 | NetEqDecodingTest::SetUp(); |
| 1666 | config2_ = config_; |
| 1667 | } |
| 1668 | |
| 1669 | void CreateSecondInstance() { |
ossu | e352578 | 2016-05-25 07:37:43 -0700 | [diff] [blame^] | 1670 | neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory())); |
henrik.lundin | 7a92681 | 2016-05-12 13:51:28 -0700 | [diff] [blame] | 1671 | ASSERT_TRUE(neteq2_); |
| 1672 | LoadDecoders(neteq2_.get()); |
| 1673 | } |
| 1674 | |
| 1675 | protected: |
| 1676 | std::unique_ptr<NetEq> neteq2_; |
| 1677 | NetEq::Config config2_; |
| 1678 | }; |
| 1679 | |
| 1680 | namespace { |
| 1681 | ::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a, |
| 1682 | const AudioFrame& b) { |
| 1683 | if (a.timestamp_ != b.timestamp_) |
| 1684 | return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_ |
| 1685 | << " != " << b.timestamp_ << ")"; |
| 1686 | if (a.sample_rate_hz_ != b.sample_rate_hz_) |
| 1687 | return ::testing::AssertionFailure() << "sample_rate_hz_ diff (" |
| 1688 | << a.sample_rate_hz_ |
| 1689 | << " != " << b.sample_rate_hz_ << ")"; |
| 1690 | if (a.samples_per_channel_ != b.samples_per_channel_) |
| 1691 | return ::testing::AssertionFailure() |
| 1692 | << "samples_per_channel_ diff (" << a.samples_per_channel_ |
| 1693 | << " != " << b.samples_per_channel_ << ")"; |
| 1694 | if (a.num_channels_ != b.num_channels_) |
| 1695 | return ::testing::AssertionFailure() << "num_channels_ diff (" |
| 1696 | << a.num_channels_ |
| 1697 | << " != " << b.num_channels_ << ")"; |
| 1698 | if (a.speech_type_ != b.speech_type_) |
| 1699 | return ::testing::AssertionFailure() << "speech_type_ diff (" |
| 1700 | << a.speech_type_ |
| 1701 | << " != " << b.speech_type_ << ")"; |
| 1702 | if (a.vad_activity_ != b.vad_activity_) |
| 1703 | return ::testing::AssertionFailure() << "vad_activity_ diff (" |
| 1704 | << a.vad_activity_ |
| 1705 | << " != " << b.vad_activity_ << ")"; |
| 1706 | return ::testing::AssertionSuccess(); |
| 1707 | } |
| 1708 | |
| 1709 | ::testing::AssertionResult AudioFramesEqual(const AudioFrame& a, |
| 1710 | const AudioFrame& b) { |
| 1711 | ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b); |
| 1712 | if (!res) |
| 1713 | return res; |
| 1714 | if (memcmp( |
| 1715 | a.data_, b.data_, |
| 1716 | a.samples_per_channel_ * a.num_channels_ * sizeof(a.data_[0])) != 0) { |
| 1717 | return ::testing::AssertionFailure() << "data_ diff"; |
| 1718 | } |
| 1719 | return ::testing::AssertionSuccess(); |
| 1720 | } |
| 1721 | |
| 1722 | } // namespace |
| 1723 | |
| 1724 | TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) { |
| 1725 | ASSERT_FALSE(config_.enable_muted_state); |
| 1726 | config2_.enable_muted_state = true; |
| 1727 | CreateSecondInstance(); |
| 1728 | |
| 1729 | // Insert one speech packet into both NetEqs. |
| 1730 | const size_t kSamples = 10 * 16; |
| 1731 | const size_t kPayloadBytes = kSamples * 2; |
| 1732 | uint8_t payload[kPayloadBytes] = {0}; |
| 1733 | WebRtcRTPHeader rtp_info; |
| 1734 | PopulateRtpInfo(0, 0, &rtp_info); |
| 1735 | EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
| 1736 | EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0)); |
| 1737 | |
| 1738 | AudioFrame out_frame1, out_frame2; |
| 1739 | bool muted; |
| 1740 | for (int i = 0; i < 1000; ++i) { |
| 1741 | std::ostringstream ss; |
| 1742 | ss << "i = " << i; |
| 1743 | SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure. |
| 1744 | EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted)); |
| 1745 | EXPECT_FALSE(muted); |
| 1746 | EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted)); |
| 1747 | if (muted) { |
| 1748 | EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2)); |
| 1749 | } else { |
| 1750 | EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2)); |
| 1751 | } |
| 1752 | } |
| 1753 | EXPECT_TRUE(muted); |
| 1754 | |
| 1755 | // Insert new data. Timestamp is corrected for the time elapsed since the last |
| 1756 | // packet. |
| 1757 | PopulateRtpInfo(0, kSamples * 1000, &rtp_info); |
| 1758 | EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0)); |
| 1759 | EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0)); |
| 1760 | |
| 1761 | int counter = 0; |
| 1762 | while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) { |
| 1763 | ASSERT_LT(counter++, 1000) << "Test timed out"; |
| 1764 | std::ostringstream ss; |
| 1765 | ss << "counter = " << counter; |
| 1766 | SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure. |
| 1767 | EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted)); |
| 1768 | EXPECT_FALSE(muted); |
| 1769 | EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted)); |
| 1770 | if (muted) { |
| 1771 | EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2)); |
| 1772 | } else { |
| 1773 | EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2)); |
| 1774 | } |
| 1775 | } |
| 1776 | EXPECT_FALSE(muted); |
| 1777 | } |
| 1778 | |
henrik.lundin@webrtc.org | e7ce437 | 2014-01-09 14:01:55 +0000 | [diff] [blame] | 1779 | } // namespace webrtc |