blob: d17936757ac3a7840b8ec56c3d43a2a1f73eadb9 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Kjellander74640892015-10-29 11:31:02 +010011#include "webrtc/modules/audio_coding/neteq/include/neteq.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org3ecc1622014-03-07 15:23:34 +000013#include <math.h>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014#include <stdlib.h>
15#include <string.h> // memset
16
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000017#include <algorithm>
kwiberg2d0c3322016-02-14 09:28:33 -080018#include <memory>
turaj@webrtc.org78b41a02013-11-22 20:27:07 +000019#include <set>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020#include <string>
21#include <vector>
22
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000023#include "gflags/gflags.h"
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000024#include "testing/gtest/include/gtest/gtest.h"
minyue4f906772016-04-29 11:05:14 -070025#include "webrtc/base/sha1digest.h"
26#include "webrtc/base/stringencode.h"
ossue3525782016-05-25 07:37:43 -070027#include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000028#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000029#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
kjellander@webrtc.org3c652b62015-11-18 23:07:57 +010030#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080031#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032#include "webrtc/test/testsupport/fileutils.h"
33#include "webrtc/typedefs.h"
34
minyue5f026d02015-12-16 07:36:04 -080035#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
36#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
37#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
38#else
39#include "webrtc/audio_coding/neteq/neteq_unittest.pb.h"
40#endif
41#endif
42
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000043DEFINE_bool(gen_ref, false, "Generate reference files.");
44
minyue5f026d02015-12-16 07:36:04 -080045namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046
minyue4f906772016-04-29 11:05:14 -070047const std::string& PlatformChecksum(const std::string& checksum_general,
48 const std::string& checksum_android,
49 const std::string& checksum_win_32,
50 const std::string& checksum_win_64) {
51#ifdef WEBRTC_ANDROID
52 return checksum_android;
53#elif WEBRTC_WIN
54 #ifdef WEBRTC_ARCH_64_BITS
55 return checksum_win_64;
56 #else
57 return checksum_win_32;
58 #endif // WEBRTC_ARCH_64_BITS
59#else
60 return checksum_general;
61#endif // WEBRTC_WIN
62}
63
minyue5f026d02015-12-16 07:36:04 -080064bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000065 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070066 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000067 all_zero = buf[n] == 0;
68 return all_zero;
69}
70
minyue5f026d02015-12-16 07:36:04 -080071bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000072 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070073 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000074 all_non_zero = buf[n] != 0;
75 return all_non_zero;
76}
77
minyue5f026d02015-12-16 07:36:04 -080078#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
79void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
80 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
81 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
82 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
83 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
84 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
85 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
86 stats->set_expand_rate(stats_raw.expand_rate);
87 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
88 stats->set_preemptive_rate(stats_raw.preemptive_rate);
89 stats->set_accelerate_rate(stats_raw.accelerate_rate);
90 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
91 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
92 stats->set_added_zero_samples(stats_raw.added_zero_samples);
93 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
94 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
95 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
96 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
97}
98
99void Convert(const webrtc::RtcpStatistics& stats_raw,
100 webrtc::neteq_unittest::RtcpStatistics* stats) {
101 stats->set_fraction_lost(stats_raw.fraction_lost);
102 stats->set_cumulative_lost(stats_raw.cumulative_lost);
103 stats->set_extended_max_sequence_number(
104 stats_raw.extended_max_sequence_number);
105 stats->set_jitter(stats_raw.jitter);
106}
107
minyue4f906772016-04-29 11:05:14 -0700108void AddMessage(FILE* file, rtc::MessageDigest* digest,
109 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800110 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700111 if (file)
112 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
113 digest->Update(&size, sizeof(size));
114
115 if (file)
116 ASSERT_EQ(static_cast<size_t>(size),
117 fwrite(message.data(), sizeof(char), size, file));
118 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800119}
120
minyue5f026d02015-12-16 07:36:04 -0800121#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
122
henrik.lundin7a926812016-05-12 13:51:28 -0700123void LoadDecoders(webrtc::NetEq* neteq) {
124 // Load PCMu.
125 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMu,
126 "pcmu", 0));
127 // Load PCMa.
128 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
129 "pcma", 8));
130#ifdef WEBRTC_CODEC_ILBC
131 // Load iLBC.
132 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderILBC,
133 "ilbc", 102));
134#endif
135#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
136 // Load iSAC.
137 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISAC,
138 "isac", 103));
139#endif
140#ifdef WEBRTC_CODEC_ISAC
141 // Load iSAC SWB.
142 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISACswb,
143 "isac-swb", 104));
144#endif
145#ifdef WEBRTC_CODEC_OPUS
146 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderOpus,
147 "opus", 111));
148#endif
149 // Load PCM16B nb.
150 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCM16B,
151 "pcm16-nb", 93));
152 // Load PCM16B wb.
153 ASSERT_EQ(0, neteq->RegisterPayloadType(
154 webrtc::NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb", 94));
155 // Load PCM16B swb32.
156 ASSERT_EQ(
157 0, neteq->RegisterPayloadType(
158 webrtc::NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32", 95));
159 // Load CNG 8 kHz.
160 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGnb,
161 "cng-nb", 13));
162 // Load CNG 16 kHz.
163 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGwb,
164 "cng-wb", 98));
165}
minyue5f026d02015-12-16 07:36:04 -0800166} // namespace
167
168namespace webrtc {
169
minyue4f906772016-04-29 11:05:14 -0700170class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171 public:
minyue4f906772016-04-29 11:05:14 -0700172 explicit ResultSink(const std::string& output_file);
173 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174
minyue4f906772016-04-29 11:05:14 -0700175 template<typename T, size_t n> void AddResult(
176 const T (&test_results)[n],
177 size_t length);
178
179 void AddResult(const NetEqNetworkStatistics& stats);
180 void AddResult(const RtcpStatistics& stats);
181
182 void VerifyChecksum(const std::string& ref_check_sum);
183
184 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000185 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700186 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187};
188
minyue4f906772016-04-29 11:05:14 -0700189ResultSink::ResultSink(const std::string &output_file)
190 : output_fp_(nullptr),
191 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000192 if (!output_file.empty()) {
193 output_fp_ = fopen(output_file.c_str(), "wb");
194 EXPECT_TRUE(output_fp_ != NULL);
195 }
196}
197
minyue4f906772016-04-29 11:05:14 -0700198ResultSink::~ResultSink() {
199 if (output_fp_)
200 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201}
202
203template<typename T, size_t n>
minyue4f906772016-04-29 11:05:14 -0700204void ResultSink::AddResult(const T (&test_results)[n], size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205 if (output_fp_) {
206 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
207 }
minyue4f906772016-04-29 11:05:14 -0700208 digest_->Update(&test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000209}
210
minyue4f906772016-04-29 11:05:14 -0700211void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800212#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800213 neteq_unittest::NetEqNetworkStatistics stats;
214 Convert(stats_raw, &stats);
215
216 std::string stats_string;
217 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700218 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800219#else
220 FAIL() << "Writing to reference file requires Proto Buffer.";
221#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000222}
223
minyue4f906772016-04-29 11:05:14 -0700224void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800225#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800226 neteq_unittest::RtcpStatistics stats;
227 Convert(stats_raw, &stats);
228
229 std::string stats_string;
230 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700231 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800232#else
233 FAIL() << "Writing to reference file requires Proto Buffer.";
234#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000235}
236
minyue4f906772016-04-29 11:05:14 -0700237void ResultSink::VerifyChecksum(const std::string& checksum) {
238 std::vector<char> buffer;
239 buffer.resize(digest_->Size());
240 digest_->Finish(&buffer[0], buffer.size());
241 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
242 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000243}
244
245class NetEqDecodingTest : public ::testing::Test {
246 protected:
247 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
248 // constants below can be changed.
249 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700250 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
251 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
252 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800253 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000254 static const int kInitSampleRateHz = 8000;
255
256 NetEqDecodingTest();
257 virtual void SetUp();
258 virtual void TearDown();
259 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000260 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800261 void Process();
minyue5f026d02015-12-16 07:36:04 -0800262
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000263 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700264 const std::string& output_checksum,
265 const std::string& network_stats_checksum,
266 const std::string& rtcp_stats_checksum,
267 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800268
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269 static void PopulateRtpInfo(int frame_index,
270 int timestamp,
271 WebRtcRTPHeader* rtp_info);
272 static void PopulateCng(int frame_index,
273 int timestamp,
274 WebRtcRTPHeader* rtp_info,
275 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000276 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000277
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000278 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
279 const std::set<uint16_t>& drop_seq_numbers,
280 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
281
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000282 void LongCngWithClockDrift(double drift_factor,
283 double network_freeze_ms,
284 bool pull_audio_during_freeze,
285 int delay_tolerance_ms,
286 int max_time_to_speech_ms);
287
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000288 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000289
henrik.lundin0d96ab72016-04-06 12:28:26 -0700290 rtc::Optional<uint32_t> PlayoutTimestamp();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000291
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000292 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000293 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800294 std::unique_ptr<test::RtpFileSource> rtp_source_;
295 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800297 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000299 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300};
301
302// Allocating the static const so that it can be passed by reference.
303const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700304const size_t NetEqDecodingTest::kBlockSize8kHz;
305const size_t NetEqDecodingTest::kBlockSize16kHz;
306const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000307const int NetEqDecodingTest::kInitSampleRateHz;
308
309NetEqDecodingTest::NetEqDecodingTest()
310 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000311 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000313 output_sample_rate_(kInitSampleRateHz),
314 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000315 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000316}
317
318void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700319 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000320 NetEqNetworkStatistics stat;
321 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
322 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700324 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325}
326
327void NetEqDecodingTest::TearDown() {
328 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329}
330
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000332 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333}
334
henrik.lundin6d8e0112016-03-04 10:34:21 -0800335void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000337 while (packet_ && sim_clock_ >= packet_->time_ms()) {
338 if (packet_->payload_length_bytes() > 0) {
339 WebRtcRTPHeader rtp_header;
340 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800341#ifndef WEBRTC_CODEC_ISAC
342 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
343 if (rtp_header.header.payloadType != 104)
344#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000345 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800346 rtp_header,
347 rtc::ArrayView<const uint8_t>(
348 packet_->payload(), packet_->payload_length_bytes()),
349 static_cast<uint32_t>(packet_->time_ms() *
350 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 }
352 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700353 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 }
355
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000356 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700357 bool muted;
358 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
359 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800360 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
361 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
362 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
363 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
364 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800365 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366
367 // Increase time.
368 sim_clock_ += kTimeStepMs;
369}
370
minyue4f906772016-04-29 11:05:14 -0700371void NetEqDecodingTest::DecodeAndCompare(
372 const std::string& rtp_file,
373 const std::string& output_checksum,
374 const std::string& network_stats_checksum,
375 const std::string& rtcp_stats_checksum,
376 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 OpenInputFile(rtp_file);
378
minyue4f906772016-04-29 11:05:14 -0700379 std::string ref_out_file =
380 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
381 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382
minyue4f906772016-04-29 11:05:14 -0700383 std::string stat_out_file =
384 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
385 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000386
minyue4f906772016-04-29 11:05:14 -0700387 std::string rtcp_out_file =
388 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
389 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000390
henrik.lundin46ba49c2016-05-24 22:50:47 -0700391 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000393 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 std::ostringstream ss;
395 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
396 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800397 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700398 ASSERT_NO_FATAL_FAILURE(output.AddResult(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800399 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000400
401 // Query the network statistics API once per second
402 if (sim_clock_ % 1000 == 0) {
403 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700404 NetEqNetworkStatistics current_network_stats;
405 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
406 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
407
henrik.lundin9c3efd02015-08-27 13:12:22 -0700408 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700409 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
410 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000411
412 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700413 RtcpStatistics current_rtcp_stats;
414 neteq_->GetRtcpStatistics(&current_rtcp_stats);
415 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000416 }
417 }
minyue4f906772016-04-29 11:05:14 -0700418
419 SCOPED_TRACE("Check output audio.");
420 output.VerifyChecksum(output_checksum);
421 SCOPED_TRACE("Check network stats.");
422 network_stats.VerifyChecksum(network_stats_checksum);
423 SCOPED_TRACE("Check rtcp stats.");
424 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000425}
426
427void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
428 int timestamp,
429 WebRtcRTPHeader* rtp_info) {
430 rtp_info->header.sequenceNumber = frame_index;
431 rtp_info->header.timestamp = timestamp;
432 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
433 rtp_info->header.payloadType = 94; // PCM16b WB codec.
434 rtp_info->header.markerBit = 0;
435}
436
437void NetEqDecodingTest::PopulateCng(int frame_index,
438 int timestamp,
439 WebRtcRTPHeader* rtp_info,
440 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000441 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000442 rtp_info->header.sequenceNumber = frame_index;
443 rtp_info->header.timestamp = timestamp;
444 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
445 rtp_info->header.payloadType = 98; // WB CNG.
446 rtp_info->header.markerBit = 0;
447 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
448 *payload_len = 1; // Only noise level, no spectral parameters.
449}
450
ivoc72c08ed2016-01-20 07:26:24 -0800451#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
452 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
453 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700454 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800455#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700456#else
minyue5f026d02015-12-16 07:36:04 -0800457#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700458#endif
minyue5f026d02015-12-16 07:36:04 -0800459TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800460 const std::string input_rtp_file =
461 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000462
minyue4f906772016-04-29 11:05:14 -0700463 const std::string output_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700464 "472ebe1126f41fdb6b5c63c87f625a52e7604e49",
465 "d2a6b6ff54b340cf9f961c7f07768d86b3761073",
466 "472ebe1126f41fdb6b5c63c87f625a52e7604e49",
467 "f9749813dbc3fb59dae761de518fec65b8407c5b");
minyue4f906772016-04-29 11:05:14 -0700468
469 const std::string network_stats_checksum = PlatformChecksum(
470 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
minyue53ff70f2016-05-02 01:50:30 -0700471 "01be67dc4c3b8e74743a45cbd8684c0535dec9ad",
minyue4f906772016-04-29 11:05:14 -0700472 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
473 "2cf380a05ee07080bd72471e8ec7777a39644ec9");
474
475 const std::string rtcp_stats_checksum = PlatformChecksum(
476 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
477 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
478 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
479 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
480
481 DecodeAndCompare(input_rtp_file,
482 output_checksum,
483 network_stats_checksum,
484 rtcp_stats_checksum,
485 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000486}
487
minyue93c08b72015-12-22 09:57:41 -0800488#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
489 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
pbosc7a65692016-05-06 12:50:04 -0700490 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800491#define MAYBE_TestOpusBitExactness TestOpusBitExactness
492#else
493#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
494#endif
495TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
496 const std::string input_rtp_file =
497 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800498
minyue4f906772016-04-29 11:05:14 -0700499 const std::string output_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700500 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
501 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
502 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
503 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4");
minyue4f906772016-04-29 11:05:14 -0700504
505 const std::string network_stats_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700506 "6eab76efbde753d4dde38983445ca16b4ce59b39",
507 "6eab76efbde753d4dde38983445ca16b4ce59b39",
508 "6eab76efbde753d4dde38983445ca16b4ce59b39",
509 "6eab76efbde753d4dde38983445ca16b4ce59b39");
minyue4f906772016-04-29 11:05:14 -0700510
511 const std::string rtcp_stats_checksum = PlatformChecksum(
512 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
513 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
514 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
515 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
516
517 DecodeAndCompare(input_rtp_file,
518 output_checksum,
519 network_stats_checksum,
520 rtcp_stats_checksum,
521 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800522}
523
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000524// Use fax mode to avoid time-scaling. This is to simplify the testing of
525// packet waiting times in the packet buffer.
526class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
527 protected:
528 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
529 config_.playout_mode = kPlayoutFax;
530 }
531};
532
533TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000534 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
535 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000536 const size_t kSamples = 10 * 16;
537 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000538 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800539 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000540 WebRtcRTPHeader rtp_info;
541 rtp_info.header.sequenceNumber = i;
542 rtp_info.header.timestamp = i * kSamples;
543 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
544 rtp_info.header.payloadType = 94; // PCM16b WB codec.
545 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800546 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000547 }
548 // Pull out all data.
549 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700550 bool muted;
551 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800552 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000553 }
554
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200555 NetEqNetworkStatistics stats;
556 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000557 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
558 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200559 // each packet. Thus, we are calculating the statistics for a series from 10
560 // to 300, in steps of 10 ms.
561 EXPECT_EQ(155, stats.mean_waiting_time_ms);
562 EXPECT_EQ(155, stats.median_waiting_time_ms);
563 EXPECT_EQ(10, stats.min_waiting_time_ms);
564 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000565
566 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200567 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
568 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
569 EXPECT_EQ(-1, stats.median_waiting_time_ms);
570 EXPECT_EQ(-1, stats.min_waiting_time_ms);
571 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000572}
573
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000574TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000575 const int kNumFrames = 3000; // Needed for convergence.
576 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000577 const size_t kSamples = 10 * 16;
578 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000579 while (frame_index < kNumFrames) {
580 // Insert one packet each time, except every 10th time where we insert two
581 // packets at once. This will create a negative clock-drift of approx. 10%.
582 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
583 for (int n = 0; n < num_packets; ++n) {
584 uint8_t payload[kPayloadBytes] = {0};
585 WebRtcRTPHeader rtp_info;
586 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800587 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000588 ++frame_index;
589 }
590
591 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700592 bool muted;
593 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800594 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595 }
596
597 NetEqNetworkStatistics network_stats;
598 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
599 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
600}
601
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000602TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000603 const int kNumFrames = 5000; // Needed for convergence.
604 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000605 const size_t kSamples = 10 * 16;
606 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000607 for (int i = 0; i < kNumFrames; ++i) {
608 // Insert one packet each time, except every 10th time where we don't insert
609 // any packet. This will create a positive clock-drift of approx. 11%.
610 int num_packets = (i % 10 == 9 ? 0 : 1);
611 for (int n = 0; n < num_packets; ++n) {
612 uint8_t payload[kPayloadBytes] = {0};
613 WebRtcRTPHeader rtp_info;
614 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800615 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000616 ++frame_index;
617 }
618
619 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700620 bool muted;
621 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800622 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000623 }
624
625 NetEqNetworkStatistics network_stats;
626 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
627 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
628}
629
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000630void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
631 double network_freeze_ms,
632 bool pull_audio_during_freeze,
633 int delay_tolerance_ms,
634 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000635 uint16_t seq_no = 0;
636 uint32_t timestamp = 0;
637 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000638 const size_t kSamples = kFrameSizeMs * 16;
639 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000640 double next_input_time_ms = 0.0;
641 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700642 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000643
644 // Insert speech for 5 seconds.
645 const int kSpeechDurationMs = 5000;
646 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
647 // Each turn in this for loop is 10 ms.
648 while (next_input_time_ms <= t_ms) {
649 // Insert one 30 ms speech frame.
650 uint8_t payload[kPayloadBytes] = {0};
651 WebRtcRTPHeader rtp_info;
652 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800653 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000654 ++seq_no;
655 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000656 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000657 }
658 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700659 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800660 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000661 }
662
henrik.lundin55480f52016-03-08 02:37:57 -0800663 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700664 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
665 ASSERT_TRUE(playout_timestamp);
666 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000667
668 // Insert CNG for 1 minute (= 60000 ms).
669 const int kCngPeriodMs = 100;
670 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
671 const int kCngDurationMs = 60000;
672 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
673 // Each turn in this for loop is 10 ms.
674 while (next_input_time_ms <= t_ms) {
675 // Insert one CNG frame each 100 ms.
676 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000677 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000678 WebRtcRTPHeader rtp_info;
679 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800680 ASSERT_EQ(0, neteq_->InsertPacket(
681 rtp_info,
682 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000683 ++seq_no;
684 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000685 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000686 }
687 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700688 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800689 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000690 }
691
henrik.lundin55480f52016-03-08 02:37:57 -0800692 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000693
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000694 if (network_freeze_ms > 0) {
695 // First keep pulling audio for |network_freeze_ms| without inserting
696 // any data, then insert CNG data corresponding to |network_freeze_ms|
697 // without pulling any output audio.
698 const double loop_end_time = t_ms + network_freeze_ms;
699 for (; t_ms < loop_end_time; t_ms += 10) {
700 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700701 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800702 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800703 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000704 }
705 bool pull_once = pull_audio_during_freeze;
706 // If |pull_once| is true, GetAudio will be called once half-way through
707 // the network recovery period.
708 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
709 while (next_input_time_ms <= t_ms) {
710 if (pull_once && next_input_time_ms >= pull_time_ms) {
711 pull_once = false;
712 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700713 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800714 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800715 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000716 t_ms += 10;
717 }
718 // Insert one CNG frame each 100 ms.
719 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000720 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000721 WebRtcRTPHeader rtp_info;
722 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800723 ASSERT_EQ(0, neteq_->InsertPacket(
724 rtp_info,
725 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000726 ++seq_no;
727 timestamp += kCngPeriodSamples;
728 next_input_time_ms += kCngPeriodMs * drift_factor;
729 }
730 }
731
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000732 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000733 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800734 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000735 // Each turn in this for loop is 10 ms.
736 while (next_input_time_ms <= t_ms) {
737 // Insert one 30 ms speech frame.
738 uint8_t payload[kPayloadBytes] = {0};
739 WebRtcRTPHeader rtp_info;
740 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800741 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000742 ++seq_no;
743 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000744 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000745 }
746 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700747 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800748 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000749 // Increase clock.
750 t_ms += 10;
751 }
752
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000753 // Check that the speech starts again within reasonable time.
754 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
755 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700756 playout_timestamp = PlayoutTimestamp();
757 ASSERT_TRUE(playout_timestamp);
758 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000759 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000760 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
761 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000762}
763
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000764TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000765 // Apply a clock drift of -25 ms / s (sender faster than receiver).
766 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000767 const double kNetworkFreezeTimeMs = 0.0;
768 const bool kGetAudioDuringFreezeRecovery = false;
769 const int kDelayToleranceMs = 20;
770 const int kMaxTimeToSpeechMs = 100;
771 LongCngWithClockDrift(kDriftFactor,
772 kNetworkFreezeTimeMs,
773 kGetAudioDuringFreezeRecovery,
774 kDelayToleranceMs,
775 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000776}
777
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000778TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000779 // Apply a clock drift of +25 ms / s (sender slower than receiver).
780 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000781 const double kNetworkFreezeTimeMs = 0.0;
782 const bool kGetAudioDuringFreezeRecovery = false;
783 const int kDelayToleranceMs = 20;
784 const int kMaxTimeToSpeechMs = 100;
785 LongCngWithClockDrift(kDriftFactor,
786 kNetworkFreezeTimeMs,
787 kGetAudioDuringFreezeRecovery,
788 kDelayToleranceMs,
789 kMaxTimeToSpeechMs);
790}
791
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000792TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000793 // Apply a clock drift of -25 ms / s (sender faster than receiver).
794 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
795 const double kNetworkFreezeTimeMs = 5000.0;
796 const bool kGetAudioDuringFreezeRecovery = false;
797 const int kDelayToleranceMs = 50;
798 const int kMaxTimeToSpeechMs = 200;
799 LongCngWithClockDrift(kDriftFactor,
800 kNetworkFreezeTimeMs,
801 kGetAudioDuringFreezeRecovery,
802 kDelayToleranceMs,
803 kMaxTimeToSpeechMs);
804}
805
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000806TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000807 // Apply a clock drift of +25 ms / s (sender slower than receiver).
808 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
809 const double kNetworkFreezeTimeMs = 5000.0;
810 const bool kGetAudioDuringFreezeRecovery = false;
811 const int kDelayToleranceMs = 20;
812 const int kMaxTimeToSpeechMs = 100;
813 LongCngWithClockDrift(kDriftFactor,
814 kNetworkFreezeTimeMs,
815 kGetAudioDuringFreezeRecovery,
816 kDelayToleranceMs,
817 kMaxTimeToSpeechMs);
818}
819
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000820TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000821 // Apply a clock drift of +25 ms / s (sender slower than receiver).
822 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
823 const double kNetworkFreezeTimeMs = 5000.0;
824 const bool kGetAudioDuringFreezeRecovery = true;
825 const int kDelayToleranceMs = 20;
826 const int kMaxTimeToSpeechMs = 100;
827 LongCngWithClockDrift(kDriftFactor,
828 kNetworkFreezeTimeMs,
829 kGetAudioDuringFreezeRecovery,
830 kDelayToleranceMs,
831 kMaxTimeToSpeechMs);
832}
833
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000834TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000835 const double kDriftFactor = 1.0; // No drift.
836 const double kNetworkFreezeTimeMs = 0.0;
837 const bool kGetAudioDuringFreezeRecovery = false;
838 const int kDelayToleranceMs = 10;
839 const int kMaxTimeToSpeechMs = 50;
840 LongCngWithClockDrift(kDriftFactor,
841 kNetworkFreezeTimeMs,
842 kGetAudioDuringFreezeRecovery,
843 kDelayToleranceMs,
844 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000845}
846
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000847TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000848 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000849 uint8_t payload[kPayloadBytes] = {0};
850 WebRtcRTPHeader rtp_info;
851 PopulateRtpInfo(0, 0, &rtp_info);
852 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800853 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000854 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
855}
856
Peter Boströme2976c82016-01-04 22:44:05 +0100857#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800858#define MAYBE_DecoderError DecoderError
859#else
860#define MAYBE_DecoderError DISABLED_DecoderError
861#endif
862
Peter Boströme2976c82016-01-04 22:44:05 +0100863TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000864 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000865 uint8_t payload[kPayloadBytes] = {0};
866 WebRtcRTPHeader rtp_info;
867 PopulateRtpInfo(0, 0, &rtp_info);
868 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800869 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000870 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
871 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800872 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
873 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000874 }
henrik.lundin7a926812016-05-12 13:51:28 -0700875 bool muted;
876 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
877 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000878 // Verify that there is a decoder error to check.
879 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800880
881 enum NetEqDecoderError {
882 ISAC_LENGTH_MISMATCH = 6730,
883 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
884 };
885#if defined(WEBRTC_CODEC_ISAC)
886 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
887#elif defined(WEBRTC_CODEC_ISACFX)
888 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
889#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000890 // Verify that the first 160 samples are set to 0, and that the remaining
891 // samples are left unmodified.
892 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
893 for (int i = 0; i < kExpectedOutputLength; ++i) {
894 std::ostringstream ss;
895 ss << "i = " << i;
896 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800897 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000898 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800899 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
900 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000901 std::ostringstream ss;
902 ss << "i = " << i;
903 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800904 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000905 }
906}
907
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000908TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000909 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
910 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800911 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
912 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000913 }
henrik.lundin7a926812016-05-12 13:51:28 -0700914 bool muted;
915 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
916 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000917 // Verify that the first block of samples is set to 0.
918 static const int kExpectedOutputLength =
919 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
920 for (int i = 0; i < kExpectedOutputLength; ++i) {
921 std::ostringstream ss;
922 ss << "i = " << i;
923 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800924 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000925 }
henrik.lundind89814b2015-11-23 06:49:25 -0800926 // Verify that the sample rate did not change from the initial configuration.
927 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000928}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000929
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000930class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000931 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000932 virtual void TestCondition(double sum_squared_noise,
933 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000934
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000935 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700936 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000937 uint8_t payload_type = 0xFF; // Invalid.
938 if (sampling_rate_hz == 8000) {
939 expected_samples_per_channel = kBlockSize8kHz;
940 payload_type = 93; // PCM 16, 8 kHz.
941 } else if (sampling_rate_hz == 16000) {
942 expected_samples_per_channel = kBlockSize16kHz;
943 payload_type = 94; // PCM 16, 16 kHZ.
944 } else if (sampling_rate_hz == 32000) {
945 expected_samples_per_channel = kBlockSize32kHz;
946 payload_type = 95; // PCM 16, 32 kHz.
947 } else {
948 ASSERT_TRUE(false); // Unsupported test case.
949 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000950
henrik.lundin6d8e0112016-03-04 10:34:21 -0800951 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000952 test::AudioLoop input;
953 // We are using the same 32 kHz input file for all tests, regardless of
954 // |sampling_rate_hz|. The output may sound weird, but the test is still
955 // valid.
956 ASSERT_TRUE(input.Init(
957 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
958 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700959 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000960
961 // Payload of 10 ms of PCM16 32 kHz.
962 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000963 WebRtcRTPHeader rtp_info;
964 PopulateRtpInfo(0, 0, &rtp_info);
965 rtp_info.header.payloadType = payload_type;
966
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000967 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700968 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000969 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800970 auto block = input.GetNextBlock();
971 ASSERT_EQ(expected_samples_per_channel, block.size());
972 size_t enc_len_bytes =
973 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000974 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
975
kwibergee2bac22015-11-11 10:34:00 -0800976 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
977 payload, enc_len_bytes),
978 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800979 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700980 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800981 ASSERT_EQ(1u, output.num_channels_);
982 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800983 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000984
985 // Next packet.
986 rtp_info.header.timestamp += expected_samples_per_channel;
987 rtp_info.header.sequenceNumber++;
988 receive_timestamp += expected_samples_per_channel;
989 }
990
henrik.lundin6d8e0112016-03-04 10:34:21 -0800991 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000992
993 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
994 // one frame without checking speech-type. This is the first frame pulled
995 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700996 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800997 ASSERT_EQ(1u, output.num_channels_);
998 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000999
1000 // To be able to test the fading of background noise we need at lease to
1001 // pull 611 frames.
1002 const int kFadingThreshold = 611;
1003
1004 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1005 // is arbitrary, but sufficiently large to test enough number of frames.
1006 const int kNumPlcToCngTestFrames = 20;
1007 bool plc_to_cng = false;
1008 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001009 output.Reset();
1010 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin7a926812016-05-12 13:51:28 -07001011 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1012 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001013 ASSERT_EQ(1u, output.num_channels_);
1014 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001015 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001016 plc_to_cng = true;
1017 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001018 for (size_t k = 0;
1019 k < output.num_channels_ * output.samples_per_channel_; ++k)
1020 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001021 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001022 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001023 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001024 }
1025 }
1026 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1027 }
1028};
1029
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001030class NetEqBgnTestOn : public NetEqBgnTest {
1031 protected:
1032 NetEqBgnTestOn() : NetEqBgnTest() {
1033 config_.background_noise_mode = NetEq::kBgnOn;
1034 }
1035
1036 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1037 EXPECT_NE(0, sum_squared_noise);
1038 }
1039};
1040
1041class NetEqBgnTestOff : public NetEqBgnTest {
1042 protected:
1043 NetEqBgnTestOff() : NetEqBgnTest() {
1044 config_.background_noise_mode = NetEq::kBgnOff;
1045 }
1046
1047 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1048 EXPECT_EQ(0, sum_squared_noise);
1049 }
1050};
1051
1052class NetEqBgnTestFade : public NetEqBgnTest {
1053 protected:
1054 NetEqBgnTestFade() : NetEqBgnTest() {
1055 config_.background_noise_mode = NetEq::kBgnFade;
1056 }
1057
1058 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1059 if (should_be_faded)
1060 EXPECT_EQ(0, sum_squared_noise);
1061 }
1062};
1063
henrika1d34fe92015-06-16 10:04:20 +02001064TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001065 CheckBgn(8000);
1066 CheckBgn(16000);
1067 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001068}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001069
henrika1d34fe92015-06-16 10:04:20 +02001070TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001071 CheckBgn(8000);
1072 CheckBgn(16000);
1073 CheckBgn(32000);
1074}
1075
henrika1d34fe92015-06-16 10:04:20 +02001076TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001077 CheckBgn(8000);
1078 CheckBgn(16000);
1079 CheckBgn(32000);
1080}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001081
Peter Boströme2976c82016-01-04 22:44:05 +01001082#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -08001083#define MAYBE_SyncPacketInsert SyncPacketInsert
1084#else
1085#define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert
1086#endif
1087TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001088 WebRtcRTPHeader rtp_info;
1089 uint32_t receive_timestamp = 0;
1090 // For the readability use the following payloads instead of the defaults of
1091 // this test.
1092 uint8_t kPcm16WbPayloadType = 1;
1093 uint8_t kCngNbPayloadType = 2;
1094 uint8_t kCngWbPayloadType = 3;
1095 uint8_t kCngSwb32PayloadType = 4;
1096 uint8_t kCngSwb48PayloadType = 5;
1097 uint8_t kAvtPayloadType = 6;
1098 uint8_t kRedPayloadType = 7;
1099 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1100
1101 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001102 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001103 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001104 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001105 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001106 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001107 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001108 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001109 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001110 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001111 "cng-swb48", kCngSwb48PayloadType));
1112 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001113 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001114 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001115 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001116 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001117 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001118
1119 PopulateRtpInfo(0, 0, &rtp_info);
1120 rtp_info.header.payloadType = kPcm16WbPayloadType;
1121
1122 // The first packet injected cannot be sync-packet.
1123 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1124
1125 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001126 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001127 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001128 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001129
1130 // Next packet. Last packet contained 10 ms audio.
1131 rtp_info.header.sequenceNumber++;
1132 rtp_info.header.timestamp += kBlockSize16kHz;
1133 receive_timestamp += kBlockSize16kHz;
1134
1135 // Unacceptable payload types CNG, AVT (DTMF), RED.
1136 rtp_info.header.payloadType = kCngNbPayloadType;
1137 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1138
1139 rtp_info.header.payloadType = kCngWbPayloadType;
1140 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1141
1142 rtp_info.header.payloadType = kCngSwb32PayloadType;
1143 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1144
1145 rtp_info.header.payloadType = kCngSwb48PayloadType;
1146 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1147
1148 rtp_info.header.payloadType = kAvtPayloadType;
1149 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1150
1151 rtp_info.header.payloadType = kRedPayloadType;
1152 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1153
1154 // Change of codec cannot be initiated with a sync packet.
1155 rtp_info.header.payloadType = kIsacPayloadType;
1156 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1157
1158 // Change of SSRC is not allowed with a sync packet.
1159 rtp_info.header.payloadType = kPcm16WbPayloadType;
1160 ++rtp_info.header.ssrc;
1161 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1162
1163 --rtp_info.header.ssrc;
1164 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1165}
1166
1167// First insert several noise like packets, then sync-packets. Decoding all
1168// packets should not produce error, statistics should not show any packet loss
1169// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001170// TODO(turajs) we will have a better test if we have a referece NetEq, and
1171// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1172// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001173TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001174 WebRtcRTPHeader rtp_info;
1175 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001176 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001177 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001178 AudioFrame output;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001179 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001180 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001181 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1182 }
1183 // Insert some packets which decode to noise. We are not interested in
1184 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001185 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -07001186 bool muted;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001187 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001188 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001189 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001190 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1191 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001192
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001193 rtp_info.header.sequenceNumber++;
1194 rtp_info.header.timestamp += kBlockSize16kHz;
1195 receive_timestamp += kBlockSize16kHz;
1196 }
1197 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001198
1199 // Make sure sufficient number of sync packets are inserted that we can
1200 // conduct a test.
1201 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001202 // Insert sync-packets, the decoded sequence should be all-zero.
1203 for (int n = 0; n < kNumSyncPackets; ++n) {
1204 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001205 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1206 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001207 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1208 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001209 if (n > algorithmic_frame_delay) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001210 EXPECT_TRUE(IsAllZero(
1211 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001212 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001213 rtp_info.header.sequenceNumber++;
1214 rtp_info.header.timestamp += kBlockSize16kHz;
1215 receive_timestamp += kBlockSize16kHz;
1216 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001217
1218 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001219 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001220 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001221 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001222 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1223 ASSERT_FALSE(muted);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001224 if (n >= algorithmic_frame_delay + 1) {
1225 // Expect that this frame contain samples from regular RTP.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001226 EXPECT_TRUE(IsAllNonZero(
1227 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001228 }
1229 rtp_info.header.sequenceNumber++;
1230 rtp_info.header.timestamp += kBlockSize16kHz;
1231 receive_timestamp += kBlockSize16kHz;
1232 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001233 NetEqNetworkStatistics network_stats;
1234 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1235 // Expecting a "clean" network.
1236 EXPECT_EQ(0, network_stats.packet_loss_rate);
1237 EXPECT_EQ(0, network_stats.expand_rate);
1238 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001239 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001240}
1241
1242// Test if the size of the packet buffer reported correctly when containing
1243// sync packets. Also, test if network packets override sync packets. That is to
1244// prefer decoding a network packet to a sync packet, if both have same sequence
1245// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001246TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001247 WebRtcRTPHeader rtp_info;
1248 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001249 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001250 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001251 AudioFrame output;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001252 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001253 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1254 }
1255 // Insert some packets which decode to noise. We are not interested in
1256 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001257 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001258 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
henrik.lundin7a926812016-05-12 13:51:28 -07001259 bool muted;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001260 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001261 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001262 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001263 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1264 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001265 rtp_info.header.sequenceNumber++;
1266 rtp_info.header.timestamp += kBlockSize16kHz;
1267 receive_timestamp += kBlockSize16kHz;
1268 }
1269 const int kNumSyncPackets = 10;
1270
1271 WebRtcRTPHeader first_sync_packet_rtp_info;
1272 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1273
1274 // Insert sync-packets, but no decoding.
1275 for (int n = 0; n < kNumSyncPackets; ++n) {
1276 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1277 rtp_info.header.sequenceNumber++;
1278 rtp_info.header.timestamp += kBlockSize16kHz;
1279 receive_timestamp += kBlockSize16kHz;
1280 }
1281 NetEqNetworkStatistics network_stats;
1282 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001283 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1284 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001285
1286 // Rewind |rtp_info| to that of the first sync packet.
1287 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1288
1289 // Insert.
1290 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001291 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001292 rtp_info.header.sequenceNumber++;
1293 rtp_info.header.timestamp += kBlockSize16kHz;
1294 receive_timestamp += kBlockSize16kHz;
1295 }
1296
1297 // Decode.
1298 for (int n = 0; n < kNumSyncPackets; ++n) {
henrik.lundin7a926812016-05-12 13:51:28 -07001299 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1300 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001301 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1302 ASSERT_EQ(1u, output.num_channels_);
1303 EXPECT_TRUE(IsAllNonZero(
1304 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001305 }
1306}
1307
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001308void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1309 uint32_t start_timestamp,
1310 const std::set<uint16_t>& drop_seq_numbers,
1311 bool expect_seq_no_wrap,
1312 bool expect_timestamp_wrap) {
1313 uint16_t seq_no = start_seq_no;
1314 uint32_t timestamp = start_timestamp;
1315 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1316 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1317 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001318 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001319 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001320 uint32_t receive_timestamp = 0;
1321
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001322 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001323 const int kSpeechDurationMs = 2000;
1324 int packets_inserted = 0;
1325 uint16_t last_seq_no;
1326 uint32_t last_timestamp;
1327 bool timestamp_wrapped = false;
1328 bool seq_no_wrapped = false;
1329 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1330 // Each turn in this for loop is 10 ms.
1331 while (next_input_time_ms <= t_ms) {
1332 // Insert one 30 ms speech frame.
1333 uint8_t payload[kPayloadBytes] = {0};
1334 WebRtcRTPHeader rtp_info;
1335 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1336 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1337 // This sequence number was not in the set to drop. Insert it.
1338 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001339 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001340 ++packets_inserted;
1341 }
1342 NetEqNetworkStatistics network_stats;
1343 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1344
1345 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1346 // packet size for first few packets. Therefore we refrain from checking
1347 // the criteria.
1348 if (packets_inserted > 4) {
1349 // Expect preferred and actual buffer size to be no more than 2 frames.
1350 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001351 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1352 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001353 }
1354 last_seq_no = seq_no;
1355 last_timestamp = timestamp;
1356
1357 ++seq_no;
1358 timestamp += kSamples;
1359 receive_timestamp += kSamples;
1360 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1361
1362 seq_no_wrapped |= seq_no < last_seq_no;
1363 timestamp_wrapped |= timestamp < last_timestamp;
1364 }
1365 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001366 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001367 bool muted;
1368 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001369 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1370 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001371
1372 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001373 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1374 ASSERT_TRUE(playout_timestamp);
1375 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001376 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001377 }
1378 // Make sure we have actually tested wrap-around.
1379 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1380 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1381}
1382
1383TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1384 // Start with a sequence number that will soon wrap.
1385 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1386 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1387}
1388
1389TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1390 // Start with a sequence number that will soon wrap.
1391 std::set<uint16_t> drop_seq_numbers;
1392 drop_seq_numbers.insert(0xFFFF);
1393 drop_seq_numbers.insert(0x0);
1394 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1395}
1396
1397TEST_F(NetEqDecodingTest, TimestampWrap) {
1398 // Start with a timestamp that will soon wrap.
1399 std::set<uint16_t> drop_seq_numbers;
1400 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1401}
1402
1403TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1404 // Start with a timestamp and a sequence number that will wrap at the same
1405 // time.
1406 std::set<uint16_t> drop_seq_numbers;
1407 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1408}
1409
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001410void NetEqDecodingTest::DuplicateCng() {
1411 uint16_t seq_no = 0;
1412 uint32_t timestamp = 0;
1413 const int kFrameSizeMs = 10;
1414 const int kSampleRateKhz = 16;
1415 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001416 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001417
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001418 const int algorithmic_delay_samples = std::max(
1419 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001420 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001421 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001422 uint8_t payload[kPayloadBytes] = {0};
1423 WebRtcRTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001424 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001425 for (int i = 0; i < 3; ++i) {
1426 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001427 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001428 ++seq_no;
1429 timestamp += kSamples;
1430
1431 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001432 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001433 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001434 }
1435 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001436 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001437
1438 // Insert same CNG packet twice.
1439 const int kCngPeriodMs = 100;
1440 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001441 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001442 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1443 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001444 ASSERT_EQ(
1445 0, neteq_->InsertPacket(
1446 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001447
1448 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001449 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001450 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001451 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001452 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1453 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1454 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001455
1456 // Insert the same CNG packet again. Note that at this point it is old, since
1457 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001458 ASSERT_EQ(
1459 0, neteq_->InsertPacket(
1460 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001461
1462 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1463 // we have already pulled out CNG once.
1464 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001465 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001466 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001467 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001468 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001469 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001470 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001471 }
1472
1473 // Insert speech again.
1474 ++seq_no;
1475 timestamp += kCngPeriodSamples;
1476 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001477 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001478
1479 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001480 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001481 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001482 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001483 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1484 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001485 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001486 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001487}
1488
henrik.lundin0d96ab72016-04-06 12:28:26 -07001489rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1490 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001491}
1492
1493TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001494
1495TEST_F(NetEqDecodingTest, CngFirst) {
1496 uint16_t seq_no = 0;
1497 uint32_t timestamp = 0;
1498 const int kFrameSizeMs = 10;
1499 const int kSampleRateKhz = 16;
1500 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1501 const int kPayloadBytes = kSamples * 2;
1502 const int kCngPeriodMs = 100;
1503 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1504 size_t payload_len;
1505
1506 uint8_t payload[kPayloadBytes] = {0};
1507 WebRtcRTPHeader rtp_info;
1508
1509 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001510 ASSERT_EQ(
1511 NetEq::kOK,
1512 neteq_->InsertPacket(
1513 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001514 ++seq_no;
1515 timestamp += kCngPeriodSamples;
1516
1517 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001518 bool muted;
1519 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001520 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001521 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001522
1523 // Insert some speech packets.
1524 for (int i = 0; i < 3; ++i) {
1525 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001526 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001527 ++seq_no;
1528 timestamp += kSamples;
1529
1530 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001531 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001532 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001533 }
1534 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001535 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001536}
henrik.lundin7a926812016-05-12 13:51:28 -07001537
1538class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1539 public:
1540 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1541 config_.enable_muted_state = true;
1542 }
1543
1544 protected:
1545 static constexpr size_t kSamples = 10 * 16;
1546 static constexpr size_t kPayloadBytes = kSamples * 2;
1547
1548 void InsertPacket(uint32_t rtp_timestamp) {
1549 uint8_t payload[kPayloadBytes] = {0};
1550 WebRtcRTPHeader rtp_info;
1551 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
1552 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1553 }
1554
1555 bool GetAudioReturnMuted() {
1556 bool muted;
1557 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1558 return muted;
1559 }
1560
1561 void GetAudioUntilMuted() {
1562 while (!GetAudioReturnMuted()) {
1563 ASSERT_LT(counter_++, 1000) << "Test timed out";
1564 }
1565 }
1566
1567 void GetAudioUntilNormal() {
1568 bool muted = false;
1569 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1570 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1571 ASSERT_LT(counter_++, 1000) << "Test timed out";
1572 }
1573 EXPECT_FALSE(muted);
1574 }
1575
1576 int counter_ = 0;
1577};
1578
1579// Verifies that NetEq goes in and out of muted state as expected.
1580TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1581 // Insert one speech packet.
1582 InsertPacket(0);
1583 // Pull out audio once and expect it not to be muted.
1584 EXPECT_FALSE(GetAudioReturnMuted());
1585 // Pull data until faded out.
1586 GetAudioUntilMuted();
1587
1588 // Verify that output audio is not written during muted mode. Other parameters
1589 // should be correct, though.
1590 AudioFrame new_frame;
1591 for (auto& d : new_frame.data_) {
1592 d = 17;
1593 }
1594 bool muted;
1595 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1596 EXPECT_TRUE(muted);
1597 for (auto d : new_frame.data_) {
1598 EXPECT_EQ(17, d);
1599 }
1600 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1601 new_frame.timestamp_);
1602 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1603 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1604 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1605 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1606 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1607
1608 // Insert new data. Timestamp is corrected for the time elapsed since the last
1609 // packet. Verify that normal operation resumes.
1610 InsertPacket(kSamples * counter_);
1611 GetAudioUntilNormal();
1612}
1613
1614// Verifies that NetEq goes out of muted state when given a delayed packet.
1615TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1616 // Insert one speech packet.
1617 InsertPacket(0);
1618 // Pull out audio once and expect it not to be muted.
1619 EXPECT_FALSE(GetAudioReturnMuted());
1620 // Pull data until faded out.
1621 GetAudioUntilMuted();
1622 // Insert new data. Timestamp is only corrected for the half of the time
1623 // elapsed since the last packet. That is, the new packet is delayed. Verify
1624 // that normal operation resumes.
1625 InsertPacket(kSamples * counter_ / 2);
1626 GetAudioUntilNormal();
1627}
1628
1629// Verifies that NetEq goes out of muted state when given a future packet.
1630TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1631 // Insert one speech packet.
1632 InsertPacket(0);
1633 // Pull out audio once and expect it not to be muted.
1634 EXPECT_FALSE(GetAudioReturnMuted());
1635 // Pull data until faded out.
1636 GetAudioUntilMuted();
1637 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1638 // last packet. That is, the new packet is too early. Verify that normal
1639 // operation resumes.
1640 InsertPacket(kSamples * counter_ * 2);
1641 GetAudioUntilNormal();
1642}
1643
1644// Verifies that NetEq goes out of muted state when given an old packet.
1645TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1646 // Insert one speech packet.
1647 InsertPacket(0);
1648 // Pull out audio once and expect it not to be muted.
1649 EXPECT_FALSE(GetAudioReturnMuted());
1650 // Pull data until faded out.
1651 GetAudioUntilMuted();
1652
1653 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1654 // Insert packet which is older than the first packet.
1655 InsertPacket(kSamples * (counter_ - 1000));
1656 EXPECT_FALSE(GetAudioReturnMuted());
1657 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1658}
1659
1660class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1661 public:
1662 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1663
1664 void SetUp() override {
1665 NetEqDecodingTest::SetUp();
1666 config2_ = config_;
1667 }
1668
1669 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001670 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001671 ASSERT_TRUE(neteq2_);
1672 LoadDecoders(neteq2_.get());
1673 }
1674
1675 protected:
1676 std::unique_ptr<NetEq> neteq2_;
1677 NetEq::Config config2_;
1678};
1679
1680namespace {
1681::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1682 const AudioFrame& b) {
1683 if (a.timestamp_ != b.timestamp_)
1684 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1685 << " != " << b.timestamp_ << ")";
1686 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1687 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1688 << a.sample_rate_hz_
1689 << " != " << b.sample_rate_hz_ << ")";
1690 if (a.samples_per_channel_ != b.samples_per_channel_)
1691 return ::testing::AssertionFailure()
1692 << "samples_per_channel_ diff (" << a.samples_per_channel_
1693 << " != " << b.samples_per_channel_ << ")";
1694 if (a.num_channels_ != b.num_channels_)
1695 return ::testing::AssertionFailure() << "num_channels_ diff ("
1696 << a.num_channels_
1697 << " != " << b.num_channels_ << ")";
1698 if (a.speech_type_ != b.speech_type_)
1699 return ::testing::AssertionFailure() << "speech_type_ diff ("
1700 << a.speech_type_
1701 << " != " << b.speech_type_ << ")";
1702 if (a.vad_activity_ != b.vad_activity_)
1703 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1704 << a.vad_activity_
1705 << " != " << b.vad_activity_ << ")";
1706 return ::testing::AssertionSuccess();
1707}
1708
1709::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1710 const AudioFrame& b) {
1711 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1712 if (!res)
1713 return res;
1714 if (memcmp(
1715 a.data_, b.data_,
1716 a.samples_per_channel_ * a.num_channels_ * sizeof(a.data_[0])) != 0) {
1717 return ::testing::AssertionFailure() << "data_ diff";
1718 }
1719 return ::testing::AssertionSuccess();
1720}
1721
1722} // namespace
1723
1724TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1725 ASSERT_FALSE(config_.enable_muted_state);
1726 config2_.enable_muted_state = true;
1727 CreateSecondInstance();
1728
1729 // Insert one speech packet into both NetEqs.
1730 const size_t kSamples = 10 * 16;
1731 const size_t kPayloadBytes = kSamples * 2;
1732 uint8_t payload[kPayloadBytes] = {0};
1733 WebRtcRTPHeader rtp_info;
1734 PopulateRtpInfo(0, 0, &rtp_info);
1735 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1736 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1737
1738 AudioFrame out_frame1, out_frame2;
1739 bool muted;
1740 for (int i = 0; i < 1000; ++i) {
1741 std::ostringstream ss;
1742 ss << "i = " << i;
1743 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1744 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1745 EXPECT_FALSE(muted);
1746 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1747 if (muted) {
1748 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1749 } else {
1750 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1751 }
1752 }
1753 EXPECT_TRUE(muted);
1754
1755 // Insert new data. Timestamp is corrected for the time elapsed since the last
1756 // packet.
1757 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
1758 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1759 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1760
1761 int counter = 0;
1762 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1763 ASSERT_LT(counter++, 1000) << "Test timed out";
1764 std::ostringstream ss;
1765 ss << "counter = " << counter;
1766 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1767 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1768 EXPECT_FALSE(muted);
1769 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1770 if (muted) {
1771 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1772 } else {
1773 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1774 }
1775 }
1776 EXPECT_FALSE(muted);
1777}
1778
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001779} // namespace webrtc