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