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