blob: 4a6f505fc639c4edb0b515f712a471ec0c9237cc [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"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000027#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000028#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
kjellander@webrtc.org3c652b62015-11-18 23:07:57 +010029#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080030#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031#include "webrtc/test/testsupport/fileutils.h"
32#include "webrtc/typedefs.h"
33
minyue5f026d02015-12-16 07:36:04 -080034#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
35#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
36#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
37#else
38#include "webrtc/audio_coding/neteq/neteq_unittest.pb.h"
39#endif
40#endif
41
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000042DEFINE_bool(gen_ref, false, "Generate reference files.");
43
minyue5f026d02015-12-16 07:36:04 -080044namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045
minyue4f906772016-04-29 11:05:14 -070046const std::string& PlatformChecksum(const std::string& checksum_general,
47 const std::string& checksum_android,
48 const std::string& checksum_win_32,
49 const std::string& checksum_win_64) {
50#ifdef WEBRTC_ANDROID
51 return checksum_android;
52#elif WEBRTC_WIN
53 #ifdef WEBRTC_ARCH_64_BITS
54 return checksum_win_64;
55 #else
56 return checksum_win_32;
57 #endif // WEBRTC_ARCH_64_BITS
58#else
59 return checksum_general;
60#endif // WEBRTC_WIN
61}
62
minyue5f026d02015-12-16 07:36:04 -080063bool IsAllZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000064 bool all_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 for (size_t n = 0; n < buf_length && all_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000066 all_zero = buf[n] == 0;
67 return all_zero;
68}
69
minyue5f026d02015-12-16 07:36:04 -080070bool IsAllNonZero(const int16_t* buf, size_t buf_length) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000071 bool all_non_zero = true;
Peter Kastingdce40cf2015-08-24 14:52:23 -070072 for (size_t n = 0; n < buf_length && all_non_zero; ++n)
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +000073 all_non_zero = buf[n] != 0;
74 return all_non_zero;
75}
76
minyue5f026d02015-12-16 07:36:04 -080077#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
78void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
79 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
80 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
81 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
82 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
83 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
84 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
85 stats->set_expand_rate(stats_raw.expand_rate);
86 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
87 stats->set_preemptive_rate(stats_raw.preemptive_rate);
88 stats->set_accelerate_rate(stats_raw.accelerate_rate);
89 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
90 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
91 stats->set_added_zero_samples(stats_raw.added_zero_samples);
92 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
93 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
94 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
95 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
96}
97
98void Convert(const webrtc::RtcpStatistics& stats_raw,
99 webrtc::neteq_unittest::RtcpStatistics* stats) {
100 stats->set_fraction_lost(stats_raw.fraction_lost);
101 stats->set_cumulative_lost(stats_raw.cumulative_lost);
102 stats->set_extended_max_sequence_number(
103 stats_raw.extended_max_sequence_number);
104 stats->set_jitter(stats_raw.jitter);
105}
106
minyue4f906772016-04-29 11:05:14 -0700107void AddMessage(FILE* file, rtc::MessageDigest* digest,
108 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -0800109 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700110 if (file)
111 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
112 digest->Update(&size, sizeof(size));
113
114 if (file)
115 ASSERT_EQ(static_cast<size_t>(size),
116 fwrite(message.data(), sizeof(char), size, file));
117 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800118}
119
minyue5f026d02015-12-16 07:36:04 -0800120#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
121
henrik.lundin7a926812016-05-12 13:51:28 -0700122void LoadDecoders(webrtc::NetEq* neteq) {
123 // Load PCMu.
124 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMu,
125 "pcmu", 0));
126 // Load PCMa.
127 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
128 "pcma", 8));
129#ifdef WEBRTC_CODEC_ILBC
130 // Load iLBC.
131 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderILBC,
132 "ilbc", 102));
133#endif
134#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
135 // Load iSAC.
136 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISAC,
137 "isac", 103));
138#endif
139#ifdef WEBRTC_CODEC_ISAC
140 // Load iSAC SWB.
141 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISACswb,
142 "isac-swb", 104));
143#endif
144#ifdef WEBRTC_CODEC_OPUS
145 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderOpus,
146 "opus", 111));
147#endif
148 // Load PCM16B nb.
149 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCM16B,
150 "pcm16-nb", 93));
151 // Load PCM16B wb.
152 ASSERT_EQ(0, neteq->RegisterPayloadType(
153 webrtc::NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb", 94));
154 // Load PCM16B swb32.
155 ASSERT_EQ(
156 0, neteq->RegisterPayloadType(
157 webrtc::NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32", 95));
158 // Load CNG 8 kHz.
159 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGnb,
160 "cng-nb", 13));
161 // Load CNG 16 kHz.
162 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGwb,
163 "cng-wb", 98));
164}
minyue5f026d02015-12-16 07:36:04 -0800165} // namespace
166
167namespace webrtc {
168
minyue4f906772016-04-29 11:05:14 -0700169class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 public:
minyue4f906772016-04-29 11:05:14 -0700171 explicit ResultSink(const std::string& output_file);
172 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000173
minyue4f906772016-04-29 11:05:14 -0700174 template<typename T, size_t n> void AddResult(
175 const T (&test_results)[n],
176 size_t length);
177
178 void AddResult(const NetEqNetworkStatistics& stats);
179 void AddResult(const RtcpStatistics& stats);
180
181 void VerifyChecksum(const std::string& ref_check_sum);
182
183 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000184 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700185 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186};
187
minyue4f906772016-04-29 11:05:14 -0700188ResultSink::ResultSink(const std::string &output_file)
189 : output_fp_(nullptr),
190 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191 if (!output_file.empty()) {
192 output_fp_ = fopen(output_file.c_str(), "wb");
193 EXPECT_TRUE(output_fp_ != NULL);
194 }
195}
196
minyue4f906772016-04-29 11:05:14 -0700197ResultSink::~ResultSink() {
198 if (output_fp_)
199 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000200}
201
202template<typename T, size_t n>
minyue4f906772016-04-29 11:05:14 -0700203void ResultSink::AddResult(const T (&test_results)[n], size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204 if (output_fp_) {
205 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
206 }
minyue4f906772016-04-29 11:05:14 -0700207 digest_->Update(&test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000208}
209
minyue4f906772016-04-29 11:05:14 -0700210void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800211#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800212 neteq_unittest::NetEqNetworkStatistics stats;
213 Convert(stats_raw, &stats);
214
215 std::string stats_string;
216 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700217 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800218#else
219 FAIL() << "Writing to reference file requires Proto Buffer.";
220#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000221}
222
minyue4f906772016-04-29 11:05:14 -0700223void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800224#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800225 neteq_unittest::RtcpStatistics stats;
226 Convert(stats_raw, &stats);
227
228 std::string stats_string;
229 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700230 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800231#else
232 FAIL() << "Writing to reference file requires Proto Buffer.";
233#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000234}
235
minyue4f906772016-04-29 11:05:14 -0700236void ResultSink::VerifyChecksum(const std::string& checksum) {
237 std::vector<char> buffer;
238 buffer.resize(digest_->Size());
239 digest_->Finish(&buffer[0], buffer.size());
240 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
241 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000242}
243
244class NetEqDecodingTest : public ::testing::Test {
245 protected:
246 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
247 // constants below can be changed.
248 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700249 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
250 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
251 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800252 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000253 static const int kInitSampleRateHz = 8000;
254
255 NetEqDecodingTest();
256 virtual void SetUp();
257 virtual void TearDown();
258 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000259 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800260 void Process();
minyue5f026d02015-12-16 07:36:04 -0800261
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000262 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700263 const std::string& output_checksum,
264 const std::string& network_stats_checksum,
265 const std::string& rtcp_stats_checksum,
266 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800267
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 static void PopulateRtpInfo(int frame_index,
269 int timestamp,
270 WebRtcRTPHeader* rtp_info);
271 static void PopulateCng(int frame_index,
272 int timestamp,
273 WebRtcRTPHeader* rtp_info,
274 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000275 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000276
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000277 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
278 const std::set<uint16_t>& drop_seq_numbers,
279 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
280
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000281 void LongCngWithClockDrift(double drift_factor,
282 double network_freeze_ms,
283 bool pull_audio_during_freeze,
284 int delay_tolerance_ms,
285 int max_time_to_speech_ms);
286
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000287 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000288
henrik.lundin0d96ab72016-04-06 12:28:26 -0700289 rtc::Optional<uint32_t> PlayoutTimestamp();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000290
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000292 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800293 std::unique_ptr<test::RtpFileSource> rtp_source_;
294 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800296 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000298 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299};
300
301// Allocating the static const so that it can be passed by reference.
302const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700303const size_t NetEqDecodingTest::kBlockSize8kHz;
304const size_t NetEqDecodingTest::kBlockSize16kHz;
305const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306const int NetEqDecodingTest::kInitSampleRateHz;
307
308NetEqDecodingTest::NetEqDecodingTest()
309 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000310 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000312 output_sample_rate_(kInitSampleRateHz),
313 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000314 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315}
316
317void NetEqDecodingTest::SetUp() {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000318 neteq_ = NetEq::Create(config_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000319 NetEqNetworkStatistics stat;
320 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
321 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700323 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000324}
325
326void NetEqDecodingTest::TearDown() {
327 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000328}
329
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000331 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332}
333
henrik.lundin6d8e0112016-03-04 10:34:21 -0800334void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000335 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000336 while (packet_ && sim_clock_ >= packet_->time_ms()) {
337 if (packet_->payload_length_bytes() > 0) {
338 WebRtcRTPHeader rtp_header;
339 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800340#ifndef WEBRTC_CODEC_ISAC
341 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
342 if (rtp_header.header.payloadType != 104)
343#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800345 rtp_header,
346 rtc::ArrayView<const uint8_t>(
347 packet_->payload(), packet_->payload_length_bytes()),
348 static_cast<uint32_t>(packet_->time_ms() *
349 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 }
351 // Get next packet.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000352 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353 }
354
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000355 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700356 bool muted;
357 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
358 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800359 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
360 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
361 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
362 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
363 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800364 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000365
366 // Increase time.
367 sim_clock_ += kTimeStepMs;
368}
369
minyue4f906772016-04-29 11:05:14 -0700370void NetEqDecodingTest::DecodeAndCompare(
371 const std::string& rtp_file,
372 const std::string& output_checksum,
373 const std::string& network_stats_checksum,
374 const std::string& rtcp_stats_checksum,
375 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 OpenInputFile(rtp_file);
377
minyue4f906772016-04-29 11:05:14 -0700378 std::string ref_out_file =
379 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
380 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381
minyue4f906772016-04-29 11:05:14 -0700382 std::string stat_out_file =
383 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
384 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000385
minyue4f906772016-04-29 11:05:14 -0700386 std::string rtcp_out_file =
387 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
388 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000389
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000390 packet_.reset(rtp_source_->NextPacket());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000392 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000393 std::ostringstream ss;
394 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
395 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800396 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700397 ASSERT_NO_FATAL_FAILURE(output.AddResult(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800398 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399
400 // Query the network statistics API once per second
401 if (sim_clock_ % 1000 == 0) {
402 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700403 NetEqNetworkStatistics current_network_stats;
404 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
405 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
406
henrik.lundin9c3efd02015-08-27 13:12:22 -0700407 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700408 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
409 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000410
411 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700412 RtcpStatistics current_rtcp_stats;
413 neteq_->GetRtcpStatistics(&current_rtcp_stats);
414 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000415 }
416 }
minyue4f906772016-04-29 11:05:14 -0700417
418 SCOPED_TRACE("Check output audio.");
419 output.VerifyChecksum(output_checksum);
420 SCOPED_TRACE("Check network stats.");
421 network_stats.VerifyChecksum(network_stats_checksum);
422 SCOPED_TRACE("Check rtcp stats.");
423 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000424}
425
426void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
427 int timestamp,
428 WebRtcRTPHeader* rtp_info) {
429 rtp_info->header.sequenceNumber = frame_index;
430 rtp_info->header.timestamp = timestamp;
431 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
432 rtp_info->header.payloadType = 94; // PCM16b WB codec.
433 rtp_info->header.markerBit = 0;
434}
435
436void NetEqDecodingTest::PopulateCng(int frame_index,
437 int timestamp,
438 WebRtcRTPHeader* rtp_info,
439 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000440 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000441 rtp_info->header.sequenceNumber = frame_index;
442 rtp_info->header.timestamp = timestamp;
443 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
444 rtp_info->header.payloadType = 98; // WB CNG.
445 rtp_info->header.markerBit = 0;
446 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
447 *payload_len = 1; // Only noise level, no spectral parameters.
448}
449
ivoc72c08ed2016-01-20 07:26:24 -0800450#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
451 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
452 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700453 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800454#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700455#else
minyue5f026d02015-12-16 07:36:04 -0800456#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700457#endif
minyue5f026d02015-12-16 07:36:04 -0800458TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800459 const std::string input_rtp_file =
460 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000461
minyue4f906772016-04-29 11:05:14 -0700462 const std::string output_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700463 "472ebe1126f41fdb6b5c63c87f625a52e7604e49",
464 "d2a6b6ff54b340cf9f961c7f07768d86b3761073",
465 "472ebe1126f41fdb6b5c63c87f625a52e7604e49",
466 "f9749813dbc3fb59dae761de518fec65b8407c5b");
minyue4f906772016-04-29 11:05:14 -0700467
468 const std::string network_stats_checksum = PlatformChecksum(
469 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
minyue53ff70f2016-05-02 01:50:30 -0700470 "01be67dc4c3b8e74743a45cbd8684c0535dec9ad",
minyue4f906772016-04-29 11:05:14 -0700471 "2cf380a05ee07080bd72471e8ec7777a39644ec9",
472 "2cf380a05ee07080bd72471e8ec7777a39644ec9");
473
474 const std::string rtcp_stats_checksum = PlatformChecksum(
475 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
476 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
477 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
478 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
479
480 DecodeAndCompare(input_rtp_file,
481 output_checksum,
482 network_stats_checksum,
483 rtcp_stats_checksum,
484 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000485}
486
minyue93c08b72015-12-22 09:57:41 -0800487#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
488 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
pbosc7a65692016-05-06 12:50:04 -0700489 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800490#define MAYBE_TestOpusBitExactness TestOpusBitExactness
491#else
492#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
493#endif
494TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
495 const std::string input_rtp_file =
496 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800497
minyue4f906772016-04-29 11:05:14 -0700498 const std::string output_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700499 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
500 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
501 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4",
502 "19ad24b4a1eb7a9620e6da09f98c49aa5792ade4");
minyue4f906772016-04-29 11:05:14 -0700503
504 const std::string network_stats_checksum = PlatformChecksum(
minyue53ff70f2016-05-02 01:50:30 -0700505 "6eab76efbde753d4dde38983445ca16b4ce59b39",
506 "6eab76efbde753d4dde38983445ca16b4ce59b39",
507 "6eab76efbde753d4dde38983445ca16b4ce59b39",
508 "6eab76efbde753d4dde38983445ca16b4ce59b39");
minyue4f906772016-04-29 11:05:14 -0700509
510 const std::string rtcp_stats_checksum = PlatformChecksum(
511 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
512 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
513 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
514 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
515
516 DecodeAndCompare(input_rtp_file,
517 output_checksum,
518 network_stats_checksum,
519 rtcp_stats_checksum,
520 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800521}
522
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000523// Use fax mode to avoid time-scaling. This is to simplify the testing of
524// packet waiting times in the packet buffer.
525class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
526 protected:
527 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
528 config_.playout_mode = kPlayoutFax;
529 }
530};
531
532TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000533 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
534 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000535 const size_t kSamples = 10 * 16;
536 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000537 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800538 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000539 WebRtcRTPHeader rtp_info;
540 rtp_info.header.sequenceNumber = i;
541 rtp_info.header.timestamp = i * kSamples;
542 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
543 rtp_info.header.payloadType = 94; // PCM16b WB codec.
544 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800545 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000546 }
547 // Pull out all data.
548 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700549 bool muted;
550 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800551 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000552 }
553
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200554 NetEqNetworkStatistics stats;
555 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000556 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
557 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200558 // each packet. Thus, we are calculating the statistics for a series from 10
559 // to 300, in steps of 10 ms.
560 EXPECT_EQ(155, stats.mean_waiting_time_ms);
561 EXPECT_EQ(155, stats.median_waiting_time_ms);
562 EXPECT_EQ(10, stats.min_waiting_time_ms);
563 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000564
565 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200566 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
567 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
568 EXPECT_EQ(-1, stats.median_waiting_time_ms);
569 EXPECT_EQ(-1, stats.min_waiting_time_ms);
570 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000571}
572
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000573TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000574 const int kNumFrames = 3000; // Needed for convergence.
575 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000576 const size_t kSamples = 10 * 16;
577 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000578 while (frame_index < kNumFrames) {
579 // Insert one packet each time, except every 10th time where we insert two
580 // packets at once. This will create a negative clock-drift of approx. 10%.
581 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
582 for (int n = 0; n < num_packets; ++n) {
583 uint8_t payload[kPayloadBytes] = {0};
584 WebRtcRTPHeader rtp_info;
585 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800586 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000587 ++frame_index;
588 }
589
590 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700591 bool muted;
592 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800593 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000594 }
595
596 NetEqNetworkStatistics network_stats;
597 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
598 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
599}
600
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000601TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000602 const int kNumFrames = 5000; // Needed for convergence.
603 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000604 const size_t kSamples = 10 * 16;
605 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000606 for (int i = 0; i < kNumFrames; ++i) {
607 // Insert one packet each time, except every 10th time where we don't insert
608 // any packet. This will create a positive clock-drift of approx. 11%.
609 int num_packets = (i % 10 == 9 ? 0 : 1);
610 for (int n = 0; n < num_packets; ++n) {
611 uint8_t payload[kPayloadBytes] = {0};
612 WebRtcRTPHeader rtp_info;
613 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800614 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000615 ++frame_index;
616 }
617
618 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700619 bool muted;
620 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800621 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000622 }
623
624 NetEqNetworkStatistics network_stats;
625 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
626 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
627}
628
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000629void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
630 double network_freeze_ms,
631 bool pull_audio_during_freeze,
632 int delay_tolerance_ms,
633 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000634 uint16_t seq_no = 0;
635 uint32_t timestamp = 0;
636 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000637 const size_t kSamples = kFrameSizeMs * 16;
638 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000639 double next_input_time_ms = 0.0;
640 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700641 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000642
643 // Insert speech for 5 seconds.
644 const int kSpeechDurationMs = 5000;
645 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
646 // Each turn in this for loop is 10 ms.
647 while (next_input_time_ms <= t_ms) {
648 // Insert one 30 ms speech frame.
649 uint8_t payload[kPayloadBytes] = {0};
650 WebRtcRTPHeader rtp_info;
651 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800652 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000653 ++seq_no;
654 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000655 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000656 }
657 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700658 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800659 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000660 }
661
henrik.lundin55480f52016-03-08 02:37:57 -0800662 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700663 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
664 ASSERT_TRUE(playout_timestamp);
665 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000666
667 // Insert CNG for 1 minute (= 60000 ms).
668 const int kCngPeriodMs = 100;
669 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
670 const int kCngDurationMs = 60000;
671 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
672 // Each turn in this for loop is 10 ms.
673 while (next_input_time_ms <= t_ms) {
674 // Insert one CNG frame each 100 ms.
675 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000676 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000677 WebRtcRTPHeader rtp_info;
678 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800679 ASSERT_EQ(0, neteq_->InsertPacket(
680 rtp_info,
681 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000682 ++seq_no;
683 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000684 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000685 }
686 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700687 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800688 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000689 }
690
henrik.lundin55480f52016-03-08 02:37:57 -0800691 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000692
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000693 if (network_freeze_ms > 0) {
694 // First keep pulling audio for |network_freeze_ms| without inserting
695 // any data, then insert CNG data corresponding to |network_freeze_ms|
696 // without pulling any output audio.
697 const double loop_end_time = t_ms + network_freeze_ms;
698 for (; t_ms < loop_end_time; t_ms += 10) {
699 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700700 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800701 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800702 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000703 }
704 bool pull_once = pull_audio_during_freeze;
705 // If |pull_once| is true, GetAudio will be called once half-way through
706 // the network recovery period.
707 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
708 while (next_input_time_ms <= t_ms) {
709 if (pull_once && next_input_time_ms >= pull_time_ms) {
710 pull_once = false;
711 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700712 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800713 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800714 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000715 t_ms += 10;
716 }
717 // Insert one CNG frame each 100 ms.
718 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000719 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000720 WebRtcRTPHeader rtp_info;
721 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800722 ASSERT_EQ(0, neteq_->InsertPacket(
723 rtp_info,
724 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000725 ++seq_no;
726 timestamp += kCngPeriodSamples;
727 next_input_time_ms += kCngPeriodMs * drift_factor;
728 }
729 }
730
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000732 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800733 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000734 // Each turn in this for loop is 10 ms.
735 while (next_input_time_ms <= t_ms) {
736 // Insert one 30 ms speech frame.
737 uint8_t payload[kPayloadBytes] = {0};
738 WebRtcRTPHeader rtp_info;
739 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800740 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000741 ++seq_no;
742 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000743 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000744 }
745 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700746 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800747 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000748 // Increase clock.
749 t_ms += 10;
750 }
751
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000752 // Check that the speech starts again within reasonable time.
753 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
754 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700755 playout_timestamp = PlayoutTimestamp();
756 ASSERT_TRUE(playout_timestamp);
757 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000758 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000759 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
760 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000761}
762
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000763TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000764 // Apply a clock drift of -25 ms / s (sender faster than receiver).
765 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000766 const double kNetworkFreezeTimeMs = 0.0;
767 const bool kGetAudioDuringFreezeRecovery = false;
768 const int kDelayToleranceMs = 20;
769 const int kMaxTimeToSpeechMs = 100;
770 LongCngWithClockDrift(kDriftFactor,
771 kNetworkFreezeTimeMs,
772 kGetAudioDuringFreezeRecovery,
773 kDelayToleranceMs,
774 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000775}
776
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000777TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000778 // Apply a clock drift of +25 ms / s (sender slower than receiver).
779 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000780 const double kNetworkFreezeTimeMs = 0.0;
781 const bool kGetAudioDuringFreezeRecovery = false;
782 const int kDelayToleranceMs = 20;
783 const int kMaxTimeToSpeechMs = 100;
784 LongCngWithClockDrift(kDriftFactor,
785 kNetworkFreezeTimeMs,
786 kGetAudioDuringFreezeRecovery,
787 kDelayToleranceMs,
788 kMaxTimeToSpeechMs);
789}
790
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000791TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000792 // Apply a clock drift of -25 ms / s (sender faster than receiver).
793 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
794 const double kNetworkFreezeTimeMs = 5000.0;
795 const bool kGetAudioDuringFreezeRecovery = false;
796 const int kDelayToleranceMs = 50;
797 const int kMaxTimeToSpeechMs = 200;
798 LongCngWithClockDrift(kDriftFactor,
799 kNetworkFreezeTimeMs,
800 kGetAudioDuringFreezeRecovery,
801 kDelayToleranceMs,
802 kMaxTimeToSpeechMs);
803}
804
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000805TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000806 // Apply a clock drift of +25 ms / s (sender slower than receiver).
807 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
808 const double kNetworkFreezeTimeMs = 5000.0;
809 const bool kGetAudioDuringFreezeRecovery = false;
810 const int kDelayToleranceMs = 20;
811 const int kMaxTimeToSpeechMs = 100;
812 LongCngWithClockDrift(kDriftFactor,
813 kNetworkFreezeTimeMs,
814 kGetAudioDuringFreezeRecovery,
815 kDelayToleranceMs,
816 kMaxTimeToSpeechMs);
817}
818
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000819TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000820 // Apply a clock drift of +25 ms / s (sender slower than receiver).
821 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
822 const double kNetworkFreezeTimeMs = 5000.0;
823 const bool kGetAudioDuringFreezeRecovery = true;
824 const int kDelayToleranceMs = 20;
825 const int kMaxTimeToSpeechMs = 100;
826 LongCngWithClockDrift(kDriftFactor,
827 kNetworkFreezeTimeMs,
828 kGetAudioDuringFreezeRecovery,
829 kDelayToleranceMs,
830 kMaxTimeToSpeechMs);
831}
832
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000833TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000834 const double kDriftFactor = 1.0; // No drift.
835 const double kNetworkFreezeTimeMs = 0.0;
836 const bool kGetAudioDuringFreezeRecovery = false;
837 const int kDelayToleranceMs = 10;
838 const int kMaxTimeToSpeechMs = 50;
839 LongCngWithClockDrift(kDriftFactor,
840 kNetworkFreezeTimeMs,
841 kGetAudioDuringFreezeRecovery,
842 kDelayToleranceMs,
843 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000844}
845
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000846TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000847 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000848 uint8_t payload[kPayloadBytes] = {0};
849 WebRtcRTPHeader rtp_info;
850 PopulateRtpInfo(0, 0, &rtp_info);
851 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800852 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000853 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
854}
855
Peter Boströme2976c82016-01-04 22:44:05 +0100856#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800857#define MAYBE_DecoderError DecoderError
858#else
859#define MAYBE_DecoderError DISABLED_DecoderError
860#endif
861
Peter Boströme2976c82016-01-04 22:44:05 +0100862TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000863 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000864 uint8_t payload[kPayloadBytes] = {0};
865 WebRtcRTPHeader rtp_info;
866 PopulateRtpInfo(0, 0, &rtp_info);
867 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800868 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000869 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
870 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800871 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
872 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000873 }
henrik.lundin7a926812016-05-12 13:51:28 -0700874 bool muted;
875 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
876 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000877 // Verify that there is a decoder error to check.
878 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800879
880 enum NetEqDecoderError {
881 ISAC_LENGTH_MISMATCH = 6730,
882 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
883 };
884#if defined(WEBRTC_CODEC_ISAC)
885 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
886#elif defined(WEBRTC_CODEC_ISACFX)
887 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
888#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000889 // Verify that the first 160 samples are set to 0, and that the remaining
890 // samples are left unmodified.
891 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
892 for (int i = 0; i < kExpectedOutputLength; ++i) {
893 std::ostringstream ss;
894 ss << "i = " << i;
895 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800896 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000897 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800898 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
899 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000900 std::ostringstream ss;
901 ss << "i = " << i;
902 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800903 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000904 }
905}
906
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000907TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000908 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
909 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800910 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
911 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000912 }
henrik.lundin7a926812016-05-12 13:51:28 -0700913 bool muted;
914 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
915 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000916 // Verify that the first block of samples is set to 0.
917 static const int kExpectedOutputLength =
918 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
919 for (int i = 0; i < kExpectedOutputLength; ++i) {
920 std::ostringstream ss;
921 ss << "i = " << i;
922 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800923 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000924 }
henrik.lundind89814b2015-11-23 06:49:25 -0800925 // Verify that the sample rate did not change from the initial configuration.
926 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000927}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000928
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000929class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000930 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000931 virtual void TestCondition(double sum_squared_noise,
932 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000933
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000934 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700935 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000936 uint8_t payload_type = 0xFF; // Invalid.
937 if (sampling_rate_hz == 8000) {
938 expected_samples_per_channel = kBlockSize8kHz;
939 payload_type = 93; // PCM 16, 8 kHz.
940 } else if (sampling_rate_hz == 16000) {
941 expected_samples_per_channel = kBlockSize16kHz;
942 payload_type = 94; // PCM 16, 16 kHZ.
943 } else if (sampling_rate_hz == 32000) {
944 expected_samples_per_channel = kBlockSize32kHz;
945 payload_type = 95; // PCM 16, 32 kHz.
946 } else {
947 ASSERT_TRUE(false); // Unsupported test case.
948 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000949
henrik.lundin6d8e0112016-03-04 10:34:21 -0800950 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000951 test::AudioLoop input;
952 // We are using the same 32 kHz input file for all tests, regardless of
953 // |sampling_rate_hz|. The output may sound weird, but the test is still
954 // valid.
955 ASSERT_TRUE(input.Init(
956 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
957 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700958 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000959
960 // Payload of 10 ms of PCM16 32 kHz.
961 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000962 WebRtcRTPHeader rtp_info;
963 PopulateRtpInfo(0, 0, &rtp_info);
964 rtp_info.header.payloadType = payload_type;
965
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000966 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700967 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000968 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800969 auto block = input.GetNextBlock();
970 ASSERT_EQ(expected_samples_per_channel, block.size());
971 size_t enc_len_bytes =
972 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000973 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
974
kwibergee2bac22015-11-11 10:34:00 -0800975 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
976 payload, enc_len_bytes),
977 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800978 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700979 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800980 ASSERT_EQ(1u, output.num_channels_);
981 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800982 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000983
984 // Next packet.
985 rtp_info.header.timestamp += expected_samples_per_channel;
986 rtp_info.header.sequenceNumber++;
987 receive_timestamp += expected_samples_per_channel;
988 }
989
henrik.lundin6d8e0112016-03-04 10:34:21 -0800990 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000991
992 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
993 // one frame without checking speech-type. This is the first frame pulled
994 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700995 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800996 ASSERT_EQ(1u, output.num_channels_);
997 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000998
999 // To be able to test the fading of background noise we need at lease to
1000 // pull 611 frames.
1001 const int kFadingThreshold = 611;
1002
1003 // Test several CNG-to-PLC packet for the expected behavior. The number 20
1004 // is arbitrary, but sufficiently large to test enough number of frames.
1005 const int kNumPlcToCngTestFrames = 20;
1006 bool plc_to_cng = false;
1007 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001008 output.Reset();
1009 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin7a926812016-05-12 13:51:28 -07001010 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1011 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001012 ASSERT_EQ(1u, output.num_channels_);
1013 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001014 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001015 plc_to_cng = true;
1016 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001017 for (size_t k = 0;
1018 k < output.num_channels_ * output.samples_per_channel_; ++k)
1019 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001020 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001021 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001022 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001023 }
1024 }
1025 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1026 }
1027};
1028
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001029class NetEqBgnTestOn : public NetEqBgnTest {
1030 protected:
1031 NetEqBgnTestOn() : NetEqBgnTest() {
1032 config_.background_noise_mode = NetEq::kBgnOn;
1033 }
1034
1035 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1036 EXPECT_NE(0, sum_squared_noise);
1037 }
1038};
1039
1040class NetEqBgnTestOff : public NetEqBgnTest {
1041 protected:
1042 NetEqBgnTestOff() : NetEqBgnTest() {
1043 config_.background_noise_mode = NetEq::kBgnOff;
1044 }
1045
1046 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1047 EXPECT_EQ(0, sum_squared_noise);
1048 }
1049};
1050
1051class NetEqBgnTestFade : public NetEqBgnTest {
1052 protected:
1053 NetEqBgnTestFade() : NetEqBgnTest() {
1054 config_.background_noise_mode = NetEq::kBgnFade;
1055 }
1056
1057 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1058 if (should_be_faded)
1059 EXPECT_EQ(0, sum_squared_noise);
1060 }
1061};
1062
henrika1d34fe92015-06-16 10:04:20 +02001063TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001064 CheckBgn(8000);
1065 CheckBgn(16000);
1066 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001067}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001068
henrika1d34fe92015-06-16 10:04:20 +02001069TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001070 CheckBgn(8000);
1071 CheckBgn(16000);
1072 CheckBgn(32000);
1073}
1074
henrika1d34fe92015-06-16 10:04:20 +02001075TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001076 CheckBgn(8000);
1077 CheckBgn(16000);
1078 CheckBgn(32000);
1079}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001080
Peter Boströme2976c82016-01-04 22:44:05 +01001081#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -08001082#define MAYBE_SyncPacketInsert SyncPacketInsert
1083#else
1084#define MAYBE_SyncPacketInsert DISABLED_SyncPacketInsert
1085#endif
1086TEST_F(NetEqDecodingTest, MAYBE_SyncPacketInsert) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001087 WebRtcRTPHeader rtp_info;
1088 uint32_t receive_timestamp = 0;
1089 // For the readability use the following payloads instead of the defaults of
1090 // this test.
1091 uint8_t kPcm16WbPayloadType = 1;
1092 uint8_t kCngNbPayloadType = 2;
1093 uint8_t kCngWbPayloadType = 3;
1094 uint8_t kCngSwb32PayloadType = 4;
1095 uint8_t kCngSwb48PayloadType = 5;
1096 uint8_t kAvtPayloadType = 6;
1097 uint8_t kRedPayloadType = 7;
1098 uint8_t kIsacPayloadType = 9; // Payload type 8 is already registered.
1099
1100 // Register decoders.
kwibergee1879c2015-10-29 06:20:28 -07001101 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderPCM16Bwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001102 "pcm16-wb", kPcm16WbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001103 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGnb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001104 "cng-nb", kCngNbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001105 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGwb,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001106 "cng-wb", kCngWbPayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001107 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb32kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001108 "cng-swb32", kCngSwb32PayloadType));
kwibergee1879c2015-10-29 06:20:28 -07001109 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderCNGswb48kHz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001110 "cng-swb48", kCngSwb48PayloadType));
1111 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderAVT, "avt",
kwibergee1879c2015-10-29 06:20:28 -07001112 kAvtPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001113 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderRED, "red",
kwibergee1879c2015-10-29 06:20:28 -07001114 kRedPayloadType));
henrik.lundin4cf61dd2015-12-09 06:20:58 -08001115 ASSERT_EQ(0, neteq_->RegisterPayloadType(NetEqDecoder::kDecoderISAC, "isac",
kwibergee1879c2015-10-29 06:20:28 -07001116 kIsacPayloadType));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001117
1118 PopulateRtpInfo(0, 0, &rtp_info);
1119 rtp_info.header.payloadType = kPcm16WbPayloadType;
1120
1121 // The first packet injected cannot be sync-packet.
1122 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1123
1124 // Payload length of 10 ms PCM16 16 kHz.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001125 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001126 uint8_t payload[kPayloadBytes] = {0};
kwibergee2bac22015-11-11 10:34:00 -08001127 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001128
1129 // Next packet. Last packet contained 10 ms audio.
1130 rtp_info.header.sequenceNumber++;
1131 rtp_info.header.timestamp += kBlockSize16kHz;
1132 receive_timestamp += kBlockSize16kHz;
1133
1134 // Unacceptable payload types CNG, AVT (DTMF), RED.
1135 rtp_info.header.payloadType = kCngNbPayloadType;
1136 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1137
1138 rtp_info.header.payloadType = kCngWbPayloadType;
1139 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1140
1141 rtp_info.header.payloadType = kCngSwb32PayloadType;
1142 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1143
1144 rtp_info.header.payloadType = kCngSwb48PayloadType;
1145 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1146
1147 rtp_info.header.payloadType = kAvtPayloadType;
1148 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1149
1150 rtp_info.header.payloadType = kRedPayloadType;
1151 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1152
1153 // Change of codec cannot be initiated with a sync packet.
1154 rtp_info.header.payloadType = kIsacPayloadType;
1155 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1156
1157 // Change of SSRC is not allowed with a sync packet.
1158 rtp_info.header.payloadType = kPcm16WbPayloadType;
1159 ++rtp_info.header.ssrc;
1160 EXPECT_EQ(-1, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1161
1162 --rtp_info.header.ssrc;
1163 EXPECT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1164}
1165
1166// First insert several noise like packets, then sync-packets. Decoding all
1167// packets should not produce error, statistics should not show any packet loss
1168// and sync-packets should decode to zero.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001169// TODO(turajs) we will have a better test if we have a referece NetEq, and
1170// when Sync packets are inserted in "test" NetEq we insert all-zero payload
1171// in reference NetEq and compare the output of those two.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001172TEST_F(NetEqDecodingTest, SyncPacketDecode) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001173 WebRtcRTPHeader rtp_info;
1174 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001175 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001176 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001177 AudioFrame output;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001178 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001179 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001180 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1181 }
1182 // Insert some packets which decode to noise. We are not interested in
1183 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001184 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -07001185 bool muted;
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001186 for (int n = 0; n < 100; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001187 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001188 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001189 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1190 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001191
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001192 rtp_info.header.sequenceNumber++;
1193 rtp_info.header.timestamp += kBlockSize16kHz;
1194 receive_timestamp += kBlockSize16kHz;
1195 }
1196 const int kNumSyncPackets = 10;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001197
1198 // Make sure sufficient number of sync packets are inserted that we can
1199 // conduct a test.
1200 ASSERT_GT(kNumSyncPackets, algorithmic_frame_delay);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001201 // Insert sync-packets, the decoded sequence should be all-zero.
1202 for (int n = 0; n < kNumSyncPackets; ++n) {
1203 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001204 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1205 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001206 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1207 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001208 if (n > algorithmic_frame_delay) {
henrik.lundin6d8e0112016-03-04 10:34:21 -08001209 EXPECT_TRUE(IsAllZero(
1210 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001211 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001212 rtp_info.header.sequenceNumber++;
1213 rtp_info.header.timestamp += kBlockSize16kHz;
1214 receive_timestamp += kBlockSize16kHz;
1215 }
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001216
1217 // We insert regular packets, if sync packet are not correctly buffered then
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001218 // network statistics would show some packet loss.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001219 for (int n = 0; n <= algorithmic_frame_delay + 10; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001220 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001221 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1222 ASSERT_FALSE(muted);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001223 if (n >= algorithmic_frame_delay + 1) {
1224 // Expect that this frame contain samples from regular RTP.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001225 EXPECT_TRUE(IsAllNonZero(
1226 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001227 }
1228 rtp_info.header.sequenceNumber++;
1229 rtp_info.header.timestamp += kBlockSize16kHz;
1230 receive_timestamp += kBlockSize16kHz;
1231 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001232 NetEqNetworkStatistics network_stats;
1233 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1234 // Expecting a "clean" network.
1235 EXPECT_EQ(0, network_stats.packet_loss_rate);
1236 EXPECT_EQ(0, network_stats.expand_rate);
1237 EXPECT_EQ(0, network_stats.accelerate_rate);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001238 EXPECT_LE(network_stats.preemptive_rate, 150);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001239}
1240
1241// Test if the size of the packet buffer reported correctly when containing
1242// sync packets. Also, test if network packets override sync packets. That is to
1243// prefer decoding a network packet to a sync packet, if both have same sequence
1244// number and timestamp.
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +00001245TEST_F(NetEqDecodingTest, SyncPacketBufferSizeAndOverridenByNetworkPackets) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001246 WebRtcRTPHeader rtp_info;
1247 PopulateRtpInfo(0, 0, &rtp_info);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001248 const size_t kPayloadBytes = kBlockSize16kHz * sizeof(int16_t);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001249 uint8_t payload[kPayloadBytes];
henrik.lundin6d8e0112016-03-04 10:34:21 -08001250 AudioFrame output;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001251 for (size_t n = 0; n < kPayloadBytes; ++n) {
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001252 payload[n] = (rand() & 0xF0) + 1; // Non-zero random sequence.
1253 }
1254 // Insert some packets which decode to noise. We are not interested in
1255 // actual decoded values.
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001256 uint32_t receive_timestamp = 0;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001257 int algorithmic_frame_delay = algorithmic_delay_ms_ / 10 + 1;
henrik.lundin7a926812016-05-12 13:51:28 -07001258 bool muted;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001259 for (int n = 0; n < algorithmic_frame_delay; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001260 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
henrik.lundin7a926812016-05-12 13:51:28 -07001261 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001262 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1263 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001264 rtp_info.header.sequenceNumber++;
1265 rtp_info.header.timestamp += kBlockSize16kHz;
1266 receive_timestamp += kBlockSize16kHz;
1267 }
1268 const int kNumSyncPackets = 10;
1269
1270 WebRtcRTPHeader first_sync_packet_rtp_info;
1271 memcpy(&first_sync_packet_rtp_info, &rtp_info, sizeof(rtp_info));
1272
1273 // Insert sync-packets, but no decoding.
1274 for (int n = 0; n < kNumSyncPackets; ++n) {
1275 ASSERT_EQ(0, neteq_->InsertSyncPacket(rtp_info, receive_timestamp));
1276 rtp_info.header.sequenceNumber++;
1277 rtp_info.header.timestamp += kBlockSize16kHz;
1278 receive_timestamp += kBlockSize16kHz;
1279 }
1280 NetEqNetworkStatistics network_stats;
1281 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001282 EXPECT_EQ(kNumSyncPackets * 10 + algorithmic_delay_ms_,
1283 network_stats.current_buffer_size_ms);
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001284
1285 // Rewind |rtp_info| to that of the first sync packet.
1286 memcpy(&rtp_info, &first_sync_packet_rtp_info, sizeof(rtp_info));
1287
1288 // Insert.
1289 for (int n = 0; n < kNumSyncPackets; ++n) {
kwibergee2bac22015-11-11 10:34:00 -08001290 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001291 rtp_info.header.sequenceNumber++;
1292 rtp_info.header.timestamp += kBlockSize16kHz;
1293 receive_timestamp += kBlockSize16kHz;
1294 }
1295
1296 // Decode.
1297 for (int n = 0; n < kNumSyncPackets; ++n) {
henrik.lundin7a926812016-05-12 13:51:28 -07001298 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1299 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001300 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1301 ASSERT_EQ(1u, output.num_channels_);
1302 EXPECT_TRUE(IsAllNonZero(
1303 output.data_, output.samples_per_channel_ * output.num_channels_));
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001304 }
1305}
1306
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001307void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1308 uint32_t start_timestamp,
1309 const std::set<uint16_t>& drop_seq_numbers,
1310 bool expect_seq_no_wrap,
1311 bool expect_timestamp_wrap) {
1312 uint16_t seq_no = start_seq_no;
1313 uint32_t timestamp = start_timestamp;
1314 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1315 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1316 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001317 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001318 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001319 uint32_t receive_timestamp = 0;
1320
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001321 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001322 const int kSpeechDurationMs = 2000;
1323 int packets_inserted = 0;
1324 uint16_t last_seq_no;
1325 uint32_t last_timestamp;
1326 bool timestamp_wrapped = false;
1327 bool seq_no_wrapped = false;
1328 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1329 // Each turn in this for loop is 10 ms.
1330 while (next_input_time_ms <= t_ms) {
1331 // Insert one 30 ms speech frame.
1332 uint8_t payload[kPayloadBytes] = {0};
1333 WebRtcRTPHeader rtp_info;
1334 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1335 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1336 // This sequence number was not in the set to drop. Insert it.
1337 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001338 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001339 ++packets_inserted;
1340 }
1341 NetEqNetworkStatistics network_stats;
1342 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1343
1344 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1345 // packet size for first few packets. Therefore we refrain from checking
1346 // the criteria.
1347 if (packets_inserted > 4) {
1348 // Expect preferred and actual buffer size to be no more than 2 frames.
1349 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001350 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1351 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001352 }
1353 last_seq_no = seq_no;
1354 last_timestamp = timestamp;
1355
1356 ++seq_no;
1357 timestamp += kSamples;
1358 receive_timestamp += kSamples;
1359 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1360
1361 seq_no_wrapped |= seq_no < last_seq_no;
1362 timestamp_wrapped |= timestamp < last_timestamp;
1363 }
1364 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001365 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001366 bool muted;
1367 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001368 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1369 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001370
1371 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001372 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1373 ASSERT_TRUE(playout_timestamp);
1374 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001375 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001376 }
1377 // Make sure we have actually tested wrap-around.
1378 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1379 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1380}
1381
1382TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1383 // Start with a sequence number that will soon wrap.
1384 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1385 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1386}
1387
1388TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1389 // Start with a sequence number that will soon wrap.
1390 std::set<uint16_t> drop_seq_numbers;
1391 drop_seq_numbers.insert(0xFFFF);
1392 drop_seq_numbers.insert(0x0);
1393 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1394}
1395
1396TEST_F(NetEqDecodingTest, TimestampWrap) {
1397 // Start with a timestamp that will soon wrap.
1398 std::set<uint16_t> drop_seq_numbers;
1399 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1400}
1401
1402TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1403 // Start with a timestamp and a sequence number that will wrap at the same
1404 // time.
1405 std::set<uint16_t> drop_seq_numbers;
1406 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1407}
1408
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001409void NetEqDecodingTest::DuplicateCng() {
1410 uint16_t seq_no = 0;
1411 uint32_t timestamp = 0;
1412 const int kFrameSizeMs = 10;
1413 const int kSampleRateKhz = 16;
1414 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001415 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001416
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001417 const int algorithmic_delay_samples = std::max(
1418 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001419 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001420 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001421 uint8_t payload[kPayloadBytes] = {0};
1422 WebRtcRTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001423 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001424 for (int i = 0; i < 3; ++i) {
1425 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001426 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001427 ++seq_no;
1428 timestamp += kSamples;
1429
1430 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001431 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001432 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001433 }
1434 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001435 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001436
1437 // Insert same CNG packet twice.
1438 const int kCngPeriodMs = 100;
1439 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001440 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001441 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1442 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001443 ASSERT_EQ(
1444 0, neteq_->InsertPacket(
1445 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001446
1447 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001448 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001449 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001450 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001451 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1452 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1453 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001454
1455 // Insert the same CNG packet again. Note that at this point it is old, since
1456 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001457 ASSERT_EQ(
1458 0, neteq_->InsertPacket(
1459 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001460
1461 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1462 // we have already pulled out CNG once.
1463 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001464 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001465 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001466 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001467 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001468 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001469 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001470 }
1471
1472 // Insert speech again.
1473 ++seq_no;
1474 timestamp += kCngPeriodSamples;
1475 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001476 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001477
1478 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001479 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001480 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001481 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001482 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1483 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001484 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001485 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001486}
1487
henrik.lundin0d96ab72016-04-06 12:28:26 -07001488rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1489 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001490}
1491
1492TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001493
1494TEST_F(NetEqDecodingTest, CngFirst) {
1495 uint16_t seq_no = 0;
1496 uint32_t timestamp = 0;
1497 const int kFrameSizeMs = 10;
1498 const int kSampleRateKhz = 16;
1499 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1500 const int kPayloadBytes = kSamples * 2;
1501 const int kCngPeriodMs = 100;
1502 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1503 size_t payload_len;
1504
1505 uint8_t payload[kPayloadBytes] = {0};
1506 WebRtcRTPHeader rtp_info;
1507
1508 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001509 ASSERT_EQ(
1510 NetEq::kOK,
1511 neteq_->InsertPacket(
1512 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001513 ++seq_no;
1514 timestamp += kCngPeriodSamples;
1515
1516 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001517 bool muted;
1518 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001519 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001520 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001521
1522 // Insert some speech packets.
1523 for (int i = 0; i < 3; ++i) {
1524 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001525 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001526 ++seq_no;
1527 timestamp += kSamples;
1528
1529 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001530 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001531 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001532 }
1533 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001534 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001535}
henrik.lundin7a926812016-05-12 13:51:28 -07001536
1537class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1538 public:
1539 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1540 config_.enable_muted_state = true;
1541 }
1542
1543 protected:
1544 static constexpr size_t kSamples = 10 * 16;
1545 static constexpr size_t kPayloadBytes = kSamples * 2;
1546
1547 void InsertPacket(uint32_t rtp_timestamp) {
1548 uint8_t payload[kPayloadBytes] = {0};
1549 WebRtcRTPHeader rtp_info;
1550 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
1551 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1552 }
1553
1554 bool GetAudioReturnMuted() {
1555 bool muted;
1556 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1557 return muted;
1558 }
1559
1560 void GetAudioUntilMuted() {
1561 while (!GetAudioReturnMuted()) {
1562 ASSERT_LT(counter_++, 1000) << "Test timed out";
1563 }
1564 }
1565
1566 void GetAudioUntilNormal() {
1567 bool muted = false;
1568 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1569 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1570 ASSERT_LT(counter_++, 1000) << "Test timed out";
1571 }
1572 EXPECT_FALSE(muted);
1573 }
1574
1575 int counter_ = 0;
1576};
1577
1578// Verifies that NetEq goes in and out of muted state as expected.
1579TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1580 // Insert one speech packet.
1581 InsertPacket(0);
1582 // Pull out audio once and expect it not to be muted.
1583 EXPECT_FALSE(GetAudioReturnMuted());
1584 // Pull data until faded out.
1585 GetAudioUntilMuted();
1586
1587 // Verify that output audio is not written during muted mode. Other parameters
1588 // should be correct, though.
1589 AudioFrame new_frame;
1590 for (auto& d : new_frame.data_) {
1591 d = 17;
1592 }
1593 bool muted;
1594 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1595 EXPECT_TRUE(muted);
1596 for (auto d : new_frame.data_) {
1597 EXPECT_EQ(17, d);
1598 }
1599 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1600 new_frame.timestamp_);
1601 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1602 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1603 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1604 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1605 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1606
1607 // Insert new data. Timestamp is corrected for the time elapsed since the last
1608 // packet. Verify that normal operation resumes.
1609 InsertPacket(kSamples * counter_);
1610 GetAudioUntilNormal();
1611}
1612
1613// Verifies that NetEq goes out of muted state when given a delayed packet.
1614TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1615 // Insert one speech packet.
1616 InsertPacket(0);
1617 // Pull out audio once and expect it not to be muted.
1618 EXPECT_FALSE(GetAudioReturnMuted());
1619 // Pull data until faded out.
1620 GetAudioUntilMuted();
1621 // Insert new data. Timestamp is only corrected for the half of the time
1622 // elapsed since the last packet. That is, the new packet is delayed. Verify
1623 // that normal operation resumes.
1624 InsertPacket(kSamples * counter_ / 2);
1625 GetAudioUntilNormal();
1626}
1627
1628// Verifies that NetEq goes out of muted state when given a future packet.
1629TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1630 // Insert one speech packet.
1631 InsertPacket(0);
1632 // Pull out audio once and expect it not to be muted.
1633 EXPECT_FALSE(GetAudioReturnMuted());
1634 // Pull data until faded out.
1635 GetAudioUntilMuted();
1636 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1637 // last packet. That is, the new packet is too early. Verify that normal
1638 // operation resumes.
1639 InsertPacket(kSamples * counter_ * 2);
1640 GetAudioUntilNormal();
1641}
1642
1643// Verifies that NetEq goes out of muted state when given an old packet.
1644TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1645 // Insert one speech packet.
1646 InsertPacket(0);
1647 // Pull out audio once and expect it not to be muted.
1648 EXPECT_FALSE(GetAudioReturnMuted());
1649 // Pull data until faded out.
1650 GetAudioUntilMuted();
1651
1652 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1653 // Insert packet which is older than the first packet.
1654 InsertPacket(kSamples * (counter_ - 1000));
1655 EXPECT_FALSE(GetAudioReturnMuted());
1656 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1657}
1658
1659class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1660 public:
1661 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1662
1663 void SetUp() override {
1664 NetEqDecodingTest::SetUp();
1665 config2_ = config_;
1666 }
1667
1668 void CreateSecondInstance() {
1669 neteq2_.reset(NetEq::Create(config2_));
1670 ASSERT_TRUE(neteq2_);
1671 LoadDecoders(neteq2_.get());
1672 }
1673
1674 protected:
1675 std::unique_ptr<NetEq> neteq2_;
1676 NetEq::Config config2_;
1677};
1678
1679namespace {
1680::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1681 const AudioFrame& b) {
1682 if (a.timestamp_ != b.timestamp_)
1683 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1684 << " != " << b.timestamp_ << ")";
1685 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1686 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1687 << a.sample_rate_hz_
1688 << " != " << b.sample_rate_hz_ << ")";
1689 if (a.samples_per_channel_ != b.samples_per_channel_)
1690 return ::testing::AssertionFailure()
1691 << "samples_per_channel_ diff (" << a.samples_per_channel_
1692 << " != " << b.samples_per_channel_ << ")";
1693 if (a.num_channels_ != b.num_channels_)
1694 return ::testing::AssertionFailure() << "num_channels_ diff ("
1695 << a.num_channels_
1696 << " != " << b.num_channels_ << ")";
1697 if (a.speech_type_ != b.speech_type_)
1698 return ::testing::AssertionFailure() << "speech_type_ diff ("
1699 << a.speech_type_
1700 << " != " << b.speech_type_ << ")";
1701 if (a.vad_activity_ != b.vad_activity_)
1702 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1703 << a.vad_activity_
1704 << " != " << b.vad_activity_ << ")";
1705 return ::testing::AssertionSuccess();
1706}
1707
1708::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1709 const AudioFrame& b) {
1710 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1711 if (!res)
1712 return res;
1713 if (memcmp(
1714 a.data_, b.data_,
1715 a.samples_per_channel_ * a.num_channels_ * sizeof(a.data_[0])) != 0) {
1716 return ::testing::AssertionFailure() << "data_ diff";
1717 }
1718 return ::testing::AssertionSuccess();
1719}
1720
1721} // namespace
1722
1723TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1724 ASSERT_FALSE(config_.enable_muted_state);
1725 config2_.enable_muted_state = true;
1726 CreateSecondInstance();
1727
1728 // Insert one speech packet into both NetEqs.
1729 const size_t kSamples = 10 * 16;
1730 const size_t kPayloadBytes = kSamples * 2;
1731 uint8_t payload[kPayloadBytes] = {0};
1732 WebRtcRTPHeader rtp_info;
1733 PopulateRtpInfo(0, 0, &rtp_info);
1734 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1735 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1736
1737 AudioFrame out_frame1, out_frame2;
1738 bool muted;
1739 for (int i = 0; i < 1000; ++i) {
1740 std::ostringstream ss;
1741 ss << "i = " << i;
1742 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1743 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1744 EXPECT_FALSE(muted);
1745 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1746 if (muted) {
1747 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1748 } else {
1749 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1750 }
1751 }
1752 EXPECT_TRUE(muted);
1753
1754 // Insert new data. Timestamp is corrected for the time elapsed since the last
1755 // packet.
1756 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
1757 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1758 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1759
1760 int counter = 0;
1761 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1762 ASSERT_LT(counter++, 1000) << "Test timed out";
1763 std::ostringstream ss;
1764 ss << "counter = " << counter;
1765 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1766 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1767 EXPECT_FALSE(muted);
1768 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1769 if (muted) {
1770 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1771 } else {
1772 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1773 }
1774 }
1775 EXPECT_FALSE(muted);
1776}
1777
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001778} // namespace webrtc