blob: a5b546831cd2a90a9d5881b67a6c7afcaa298f9f [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"
kwiberg77eab702016-09-28 17:42:01 -070024#include "webrtc/test/gtest.h"
25#include "webrtc/base/ignore_wundef.h"
minyue4f906772016-04-29 11:05:14 -070026#include "webrtc/base/sha1digest.h"
27#include "webrtc/base/stringencode.h"
ossue3525782016-05-25 07:37:43 -070028#include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h"
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +000029#include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +000030#include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
kjellander@webrtc.org3c652b62015-11-18 23:07:57 +010031#include "webrtc/modules/audio_coding/codecs/pcm16b/pcm16b.h"
henrik.lundin6d8e0112016-03-04 10:34:21 -080032#include "webrtc/modules/include/module_common_types.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033#include "webrtc/test/testsupport/fileutils.h"
34#include "webrtc/typedefs.h"
35
minyue5f026d02015-12-16 07:36:04 -080036#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
kwiberg77eab702016-09-28 17:42:01 -070037RTC_PUSH_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080038#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
39#include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
40#else
41#include "webrtc/audio_coding/neteq/neteq_unittest.pb.h"
42#endif
kwiberg77eab702016-09-28 17:42:01 -070043RTC_POP_IGNORING_WUNDEF()
minyue5f026d02015-12-16 07:36:04 -080044#endif
45
turaj@webrtc.orga6101d72013-10-01 22:01:09 +000046DEFINE_bool(gen_ref, false, "Generate reference files.");
47
minyue5f026d02015-12-16 07:36:04 -080048namespace {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
minyue4f906772016-04-29 11:05:14 -070050const std::string& PlatformChecksum(const std::string& checksum_general,
51 const std::string& checksum_android,
52 const std::string& checksum_win_32,
53 const std::string& checksum_win_64) {
kwiberg77eab702016-09-28 17:42:01 -070054#if defined(WEBRTC_ANDROID)
minyue4f906772016-04-29 11:05:14 -070055 return checksum_android;
kwiberg77eab702016-09-28 17:42:01 -070056#elif defined(WEBRTC_WIN)
minyue4f906772016-04-29 11:05:14 -070057 #ifdef WEBRTC_ARCH_64_BITS
58 return checksum_win_64;
59 #else
60 return checksum_win_32;
61 #endif // WEBRTC_ARCH_64_BITS
62#else
63 return checksum_general;
64#endif // WEBRTC_WIN
65}
66
minyue5f026d02015-12-16 07:36:04 -080067#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
68void Convert(const webrtc::NetEqNetworkStatistics& stats_raw,
69 webrtc::neteq_unittest::NetEqNetworkStatistics* stats) {
70 stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
71 stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
72 stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
73 stats->set_packet_loss_rate(stats_raw.packet_loss_rate);
74 stats->set_packet_discard_rate(stats_raw.packet_discard_rate);
75 stats->set_expand_rate(stats_raw.expand_rate);
76 stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
77 stats->set_preemptive_rate(stats_raw.preemptive_rate);
78 stats->set_accelerate_rate(stats_raw.accelerate_rate);
79 stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
80 stats->set_clockdrift_ppm(stats_raw.clockdrift_ppm);
81 stats->set_added_zero_samples(stats_raw.added_zero_samples);
82 stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
83 stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
84 stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
85 stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
86}
87
88void Convert(const webrtc::RtcpStatistics& stats_raw,
89 webrtc::neteq_unittest::RtcpStatistics* stats) {
90 stats->set_fraction_lost(stats_raw.fraction_lost);
91 stats->set_cumulative_lost(stats_raw.cumulative_lost);
92 stats->set_extended_max_sequence_number(
93 stats_raw.extended_max_sequence_number);
94 stats->set_jitter(stats_raw.jitter);
95}
96
minyue4f906772016-04-29 11:05:14 -070097void AddMessage(FILE* file, rtc::MessageDigest* digest,
98 const std::string& message) {
minyue5f026d02015-12-16 07:36:04 -080099 int32_t size = message.length();
minyue4f906772016-04-29 11:05:14 -0700100 if (file)
101 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
102 digest->Update(&size, sizeof(size));
103
104 if (file)
105 ASSERT_EQ(static_cast<size_t>(size),
106 fwrite(message.data(), sizeof(char), size, file));
107 digest->Update(message.data(), sizeof(char) * size);
minyue5f026d02015-12-16 07:36:04 -0800108}
109
minyue5f026d02015-12-16 07:36:04 -0800110#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
111
henrik.lundin7a926812016-05-12 13:51:28 -0700112void LoadDecoders(webrtc::NetEq* neteq) {
113 // Load PCMu.
114 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMu,
115 "pcmu", 0));
116 // Load PCMa.
117 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCMa,
118 "pcma", 8));
119#ifdef WEBRTC_CODEC_ILBC
120 // Load iLBC.
121 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderILBC,
122 "ilbc", 102));
123#endif
124#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
125 // Load iSAC.
126 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISAC,
127 "isac", 103));
128#endif
129#ifdef WEBRTC_CODEC_ISAC
130 // Load iSAC SWB.
131 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderISACswb,
132 "isac-swb", 104));
133#endif
134#ifdef WEBRTC_CODEC_OPUS
135 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderOpus,
136 "opus", 111));
137#endif
138 // Load PCM16B nb.
139 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderPCM16B,
140 "pcm16-nb", 93));
141 // Load PCM16B wb.
142 ASSERT_EQ(0, neteq->RegisterPayloadType(
143 webrtc::NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb", 94));
144 // Load PCM16B swb32.
145 ASSERT_EQ(
146 0, neteq->RegisterPayloadType(
147 webrtc::NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32", 95));
148 // Load CNG 8 kHz.
149 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGnb,
150 "cng-nb", 13));
151 // Load CNG 16 kHz.
152 ASSERT_EQ(0, neteq->RegisterPayloadType(webrtc::NetEqDecoder::kDecoderCNGwb,
153 "cng-wb", 98));
154}
minyue5f026d02015-12-16 07:36:04 -0800155} // namespace
156
157namespace webrtc {
158
minyue4f906772016-04-29 11:05:14 -0700159class ResultSink {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160 public:
minyue4f906772016-04-29 11:05:14 -0700161 explicit ResultSink(const std::string& output_file);
162 ~ResultSink();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163
minyue4f906772016-04-29 11:05:14 -0700164 template<typename T, size_t n> void AddResult(
165 const T (&test_results)[n],
166 size_t length);
167
168 void AddResult(const NetEqNetworkStatistics& stats);
169 void AddResult(const RtcpStatistics& stats);
170
171 void VerifyChecksum(const std::string& ref_check_sum);
172
173 private:
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 FILE* output_fp_;
minyue4f906772016-04-29 11:05:14 -0700175 std::unique_ptr<rtc::MessageDigest> digest_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000176};
177
minyue4f906772016-04-29 11:05:14 -0700178ResultSink::ResultSink(const std::string &output_file)
179 : output_fp_(nullptr),
180 digest_(new rtc::Sha1Digest()) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 if (!output_file.empty()) {
182 output_fp_ = fopen(output_file.c_str(), "wb");
183 EXPECT_TRUE(output_fp_ != NULL);
184 }
185}
186
minyue4f906772016-04-29 11:05:14 -0700187ResultSink::~ResultSink() {
188 if (output_fp_)
189 fclose(output_fp_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190}
191
192template<typename T, size_t n>
minyue4f906772016-04-29 11:05:14 -0700193void ResultSink::AddResult(const T (&test_results)[n], size_t length) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194 if (output_fp_) {
195 ASSERT_EQ(length, fwrite(&test_results, sizeof(T), length, output_fp_));
196 }
minyue4f906772016-04-29 11:05:14 -0700197 digest_->Update(&test_results, sizeof(T) * length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000198}
199
minyue4f906772016-04-29 11:05:14 -0700200void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800201#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800202 neteq_unittest::NetEqNetworkStatistics stats;
203 Convert(stats_raw, &stats);
204
205 std::string stats_string;
206 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700207 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800208#else
209 FAIL() << "Writing to reference file requires Proto Buffer.";
210#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000211}
212
minyue4f906772016-04-29 11:05:14 -0700213void ResultSink::AddResult(const RtcpStatistics& stats_raw) {
minyue5f026d02015-12-16 07:36:04 -0800214#ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
minyue5f026d02015-12-16 07:36:04 -0800215 neteq_unittest::RtcpStatistics stats;
216 Convert(stats_raw, &stats);
217
218 std::string stats_string;
219 ASSERT_TRUE(stats.SerializeToString(&stats_string));
minyue4f906772016-04-29 11:05:14 -0700220 AddMessage(output_fp_, digest_.get(), stats_string);
minyue5f026d02015-12-16 07:36:04 -0800221#else
222 FAIL() << "Writing to reference file requires Proto Buffer.";
223#endif // WEBRTC_NETEQ_UNITTEST_BITEXACT
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000224}
225
minyue4f906772016-04-29 11:05:14 -0700226void ResultSink::VerifyChecksum(const std::string& checksum) {
227 std::vector<char> buffer;
228 buffer.resize(digest_->Size());
229 digest_->Finish(&buffer[0], buffer.size());
230 const std::string result = rtc::hex_encode(&buffer[0], digest_->Size());
231 EXPECT_EQ(checksum, result);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000232}
233
234class NetEqDecodingTest : public ::testing::Test {
235 protected:
236 // NetEQ must be polled for data once every 10 ms. Thus, neither of the
237 // constants below can be changed.
238 static const int kTimeStepMs = 10;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700239 static const size_t kBlockSize8kHz = kTimeStepMs * 8;
240 static const size_t kBlockSize16kHz = kTimeStepMs * 16;
241 static const size_t kBlockSize32kHz = kTimeStepMs * 32;
minyue93c08b72015-12-22 09:57:41 -0800242 static const size_t kBlockSize48kHz = kTimeStepMs * 48;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000243 static const int kInitSampleRateHz = 8000;
244
245 NetEqDecodingTest();
246 virtual void SetUp();
247 virtual void TearDown();
248 void SelectDecoders(NetEqDecoder* used_codec);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249 void OpenInputFile(const std::string &rtp_file);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800250 void Process();
minyue5f026d02015-12-16 07:36:04 -0800251
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000252 void DecodeAndCompare(const std::string& rtp_file,
minyue4f906772016-04-29 11:05:14 -0700253 const std::string& output_checksum,
254 const std::string& network_stats_checksum,
255 const std::string& rtcp_stats_checksum,
256 bool gen_ref);
minyue5f026d02015-12-16 07:36:04 -0800257
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000258 static void PopulateRtpInfo(int frame_index,
259 int timestamp,
260 WebRtcRTPHeader* rtp_info);
261 static void PopulateCng(int frame_index,
262 int timestamp,
263 WebRtcRTPHeader* rtp_info,
264 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000265 size_t* payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266
turaj@webrtc.org78b41a02013-11-22 20:27:07 +0000267 void WrapTest(uint16_t start_seq_no, uint32_t start_timestamp,
268 const std::set<uint16_t>& drop_seq_numbers,
269 bool expect_seq_no_wrap, bool expect_timestamp_wrap);
270
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000271 void LongCngWithClockDrift(double drift_factor,
272 double network_freeze_ms,
273 bool pull_audio_during_freeze,
274 int delay_tolerance_ms,
275 int max_time_to_speech_ms);
276
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +0000277 void DuplicateCng();
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000278
henrik.lundin0d96ab72016-04-06 12:28:26 -0700279 rtc::Optional<uint32_t> PlayoutTimestamp();
wu@webrtc.org94454b72014-06-05 20:34:08 +0000280
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 NetEq* neteq_;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000282 NetEq::Config config_;
kwiberg2d0c3322016-02-14 09:28:33 -0800283 std::unique_ptr<test::RtpFileSource> rtp_source_;
284 std::unique_ptr<test::Packet> packet_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285 unsigned int sim_clock_;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800286 AudioFrame out_frame_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287 int output_sample_rate_;
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000288 int algorithmic_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289};
290
291// Allocating the static const so that it can be passed by reference.
292const int NetEqDecodingTest::kTimeStepMs;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700293const size_t NetEqDecodingTest::kBlockSize8kHz;
294const size_t NetEqDecodingTest::kBlockSize16kHz;
295const size_t NetEqDecodingTest::kBlockSize32kHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296const int NetEqDecodingTest::kInitSampleRateHz;
297
298NetEqDecodingTest::NetEqDecodingTest()
299 : neteq_(NULL),
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000300 config_(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301 sim_clock_(0),
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000302 output_sample_rate_(kInitSampleRateHz),
303 algorithmic_delay_ms_(0) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000304 config_.sample_rate_hz = kInitSampleRateHz;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000305}
306
307void NetEqDecodingTest::SetUp() {
ossue3525782016-05-25 07:37:43 -0700308 neteq_ = NetEq::Create(config_, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +0000309 NetEqNetworkStatistics stat;
310 ASSERT_EQ(0, neteq_->NetworkStatistics(&stat));
311 algorithmic_delay_ms_ = stat.current_buffer_size_ms;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312 ASSERT_TRUE(neteq_);
henrik.lundin7a926812016-05-12 13:51:28 -0700313 LoadDecoders(neteq_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314}
315
316void NetEqDecodingTest::TearDown() {
317 delete neteq_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000318}
319
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000320void NetEqDecodingTest::OpenInputFile(const std::string &rtp_file) {
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000321 rtp_source_.reset(test::RtpFileSource::Create(rtp_file));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322}
323
henrik.lundin6d8e0112016-03-04 10:34:21 -0800324void NetEqDecodingTest::Process() {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000325 // Check if time to receive.
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000326 while (packet_ && sim_clock_ >= packet_->time_ms()) {
327 if (packet_->payload_length_bytes() > 0) {
328 WebRtcRTPHeader rtp_header;
329 packet_->ConvertHeader(&rtp_header);
ivoc72c08ed2016-01-20 07:26:24 -0800330#ifndef WEBRTC_CODEC_ISAC
331 // Ignore payload type 104 (iSAC-swb) if ISAC is not supported.
332 if (rtp_header.header.payloadType != 104)
333#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334 ASSERT_EQ(0, neteq_->InsertPacket(
kwibergee2bac22015-11-11 10:34:00 -0800335 rtp_header,
336 rtc::ArrayView<const uint8_t>(
337 packet_->payload(), packet_->payload_length_bytes()),
338 static_cast<uint32_t>(packet_->time_ms() *
339 (output_sample_rate_ / 1000))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000340 }
341 // Get next packet.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700342 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 }
344
henrik.lundin@webrtc.orge1d468c2013-01-30 07:37:20 +0000345 // Get audio from NetEq.
henrik.lundin7a926812016-05-12 13:51:28 -0700346 bool muted;
347 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
348 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800349 ASSERT_TRUE((out_frame_.samples_per_channel_ == kBlockSize8kHz) ||
350 (out_frame_.samples_per_channel_ == kBlockSize16kHz) ||
351 (out_frame_.samples_per_channel_ == kBlockSize32kHz) ||
352 (out_frame_.samples_per_channel_ == kBlockSize48kHz));
353 output_sample_rate_ = out_frame_.sample_rate_hz_;
henrik.lundind89814b2015-11-23 06:49:25 -0800354 EXPECT_EQ(output_sample_rate_, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355
356 // Increase time.
357 sim_clock_ += kTimeStepMs;
358}
359
minyue4f906772016-04-29 11:05:14 -0700360void NetEqDecodingTest::DecodeAndCompare(
361 const std::string& rtp_file,
362 const std::string& output_checksum,
363 const std::string& network_stats_checksum,
364 const std::string& rtcp_stats_checksum,
365 bool gen_ref) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366 OpenInputFile(rtp_file);
367
minyue4f906772016-04-29 11:05:14 -0700368 std::string ref_out_file =
369 gen_ref ? webrtc::test::OutputPath() + "neteq_universal_ref.pcm" : "";
370 ResultSink output(ref_out_file);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371
minyue4f906772016-04-29 11:05:14 -0700372 std::string stat_out_file =
373 gen_ref ? webrtc::test::OutputPath() + "neteq_network_stats.dat" : "";
374 ResultSink network_stats(stat_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000375
minyue4f906772016-04-29 11:05:14 -0700376 std::string rtcp_out_file =
377 gen_ref ? webrtc::test::OutputPath() + "neteq_rtcp_stats.dat" : "";
378 ResultSink rtcp_stats(rtcp_out_file);
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000379
henrik.lundin46ba49c2016-05-24 22:50:47 -0700380 packet_ = rtp_source_->NextPacket();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 int i = 0;
henrik.lundin@webrtc.org966a7082014-11-17 09:08:38 +0000382 while (packet_) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 std::ostringstream ss;
384 ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
385 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800386 ASSERT_NO_FATAL_FAILURE(Process());
minyue4f906772016-04-29 11:05:14 -0700387 ASSERT_NO_FATAL_FAILURE(output.AddResult(
henrik.lundin6d8e0112016-03-04 10:34:21 -0800388 out_frame_.data_, out_frame_.samples_per_channel_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389
390 // Query the network statistics API once per second
391 if (sim_clock_ % 1000 == 0) {
392 // Process NetworkStatistics.
minyue4f906772016-04-29 11:05:14 -0700393 NetEqNetworkStatistics current_network_stats;
394 ASSERT_EQ(0, neteq_->NetworkStatistics(&current_network_stats));
395 ASSERT_NO_FATAL_FAILURE(network_stats.AddResult(current_network_stats));
396
henrik.lundin9c3efd02015-08-27 13:12:22 -0700397 // Compare with CurrentDelay, which should be identical.
minyue4f906772016-04-29 11:05:14 -0700398 EXPECT_EQ(current_network_stats.current_buffer_size_ms,
399 neteq_->CurrentDelayMs());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000400
401 // Process RTCPstat.
minyue4f906772016-04-29 11:05:14 -0700402 RtcpStatistics current_rtcp_stats;
403 neteq_->GetRtcpStatistics(&current_rtcp_stats);
404 ASSERT_NO_FATAL_FAILURE(rtcp_stats.AddResult(current_rtcp_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000405 }
406 }
minyue4f906772016-04-29 11:05:14 -0700407
408 SCOPED_TRACE("Check output audio.");
409 output.VerifyChecksum(output_checksum);
410 SCOPED_TRACE("Check network stats.");
411 network_stats.VerifyChecksum(network_stats_checksum);
412 SCOPED_TRACE("Check rtcp stats.");
413 rtcp_stats.VerifyChecksum(rtcp_stats_checksum);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000414}
415
416void NetEqDecodingTest::PopulateRtpInfo(int frame_index,
417 int timestamp,
418 WebRtcRTPHeader* rtp_info) {
419 rtp_info->header.sequenceNumber = frame_index;
420 rtp_info->header.timestamp = timestamp;
421 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
422 rtp_info->header.payloadType = 94; // PCM16b WB codec.
423 rtp_info->header.markerBit = 0;
424}
425
426void NetEqDecodingTest::PopulateCng(int frame_index,
427 int timestamp,
428 WebRtcRTPHeader* rtp_info,
429 uint8_t* payload,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000430 size_t* payload_len) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000431 rtp_info->header.sequenceNumber = frame_index;
432 rtp_info->header.timestamp = timestamp;
433 rtp_info->header.ssrc = 0x1234; // Just an arbitrary SSRC.
434 rtp_info->header.payloadType = 98; // WB CNG.
435 rtp_info->header.markerBit = 0;
436 payload[0] = 64; // Noise level -64 dBov, quite arbitrarily chosen.
437 *payload_len = 1; // Only noise level, no spectral parameters.
438}
439
ivoc72c08ed2016-01-20 07:26:24 -0800440#if !defined(WEBRTC_IOS) && defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
441 (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
442 defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_CODEC_G722) && \
pbosc7a65692016-05-06 12:50:04 -0700443 !defined(WEBRTC_ARCH_ARM64)
minyue5f026d02015-12-16 07:36:04 -0800444#define MAYBE_TestBitExactness TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700445#else
minyue5f026d02015-12-16 07:36:04 -0800446#define MAYBE_TestBitExactness DISABLED_TestBitExactness
kwiberg98ab3a42015-09-30 21:54:21 -0700447#endif
minyue5f026d02015-12-16 07:36:04 -0800448TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) {
minyue49c454e2016-01-08 11:30:14 -0800449 const std::string input_rtp_file =
450 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
henrik.lundin@webrtc.org4e4b0982014-08-11 14:48:49 +0000451
minyue4f906772016-04-29 11:05:14 -0700452 const std::string output_checksum = PlatformChecksum(
henrik.lundinc7668042016-08-25 23:53:38 -0700453 "acd33f5c73625c1529c412ad59b5565132826f1b",
454 "1a2e82a0410421c1d1d3eb0615334db5e2c63784",
455 "acd33f5c73625c1529c412ad59b5565132826f1b",
456 "52797b781758a1d2303140b80b9c5030c9093d6b");
minyue4f906772016-04-29 11:05:14 -0700457
458 const std::string network_stats_checksum = PlatformChecksum(
henrik.lundinc7668042016-08-25 23:53:38 -0700459 "9c5bb9e74a583be89313b158a19ea10d41bf9de6",
460 "e948ec65cf18852ba2a197189a3186635db34c3b",
461 "9c5bb9e74a583be89313b158a19ea10d41bf9de6",
462 "9c5bb9e74a583be89313b158a19ea10d41bf9de6");
minyue4f906772016-04-29 11:05:14 -0700463
464 const std::string rtcp_stats_checksum = PlatformChecksum(
465 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
466 "f3f7b3d3e71d7e635240b5373b57df6a7e4ce9d4",
467 "b8880bf9fed2487efbddcb8d94b9937a29ae521d",
468 "b8880bf9fed2487efbddcb8d94b9937a29ae521d");
469
470 DecodeAndCompare(input_rtp_file,
471 output_checksum,
472 network_stats_checksum,
473 rtcp_stats_checksum,
474 FLAGS_gen_ref);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000475}
476
minyue93c08b72015-12-22 09:57:41 -0800477#if !defined(WEBRTC_IOS) && !defined(WEBRTC_ANDROID) && \
478 defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \
pbosc7a65692016-05-06 12:50:04 -0700479 defined(WEBRTC_CODEC_OPUS)
minyue93c08b72015-12-22 09:57:41 -0800480#define MAYBE_TestOpusBitExactness TestOpusBitExactness
481#else
482#define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness
483#endif
flim64a7eab2016-08-12 04:36:05 -0700484TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) {
minyue93c08b72015-12-22 09:57:41 -0800485 const std::string input_rtp_file =
486 webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp");
minyue93c08b72015-12-22 09:57:41 -0800487
minyue4f906772016-04-29 11:05:14 -0700488 const std::string output_checksum = PlatformChecksum(
flim64a7eab2016-08-12 04:36:05 -0700489 "9d7d52bc94e941d106aa518f324f16a58d231586",
490 "9d7d52bc94e941d106aa518f324f16a58d231586",
491 "9d7d52bc94e941d106aa518f324f16a58d231586",
492 "9d7d52bc94e941d106aa518f324f16a58d231586");
minyue4f906772016-04-29 11:05:14 -0700493
494 const std::string network_stats_checksum = PlatformChecksum(
flim64a7eab2016-08-12 04:36:05 -0700495 "191af29ed3b8b6dd4c4cc94dc3f33bdf48f055ef",
496 "191af29ed3b8b6dd4c4cc94dc3f33bdf48f055ef",
497 "191af29ed3b8b6dd4c4cc94dc3f33bdf48f055ef",
498 "191af29ed3b8b6dd4c4cc94dc3f33bdf48f055ef");
minyue4f906772016-04-29 11:05:14 -0700499
500 const std::string rtcp_stats_checksum = PlatformChecksum(
501 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
502 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
503 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0",
504 "e37c797e3de6a64dda88c9ade7a013d022a2e1e0");
505
506 DecodeAndCompare(input_rtp_file,
507 output_checksum,
508 network_stats_checksum,
509 rtcp_stats_checksum,
510 FLAGS_gen_ref);
minyue93c08b72015-12-22 09:57:41 -0800511}
512
henrik.lundin@webrtc.org7cbc4f92014-10-07 06:37:39 +0000513// Use fax mode to avoid time-scaling. This is to simplify the testing of
514// packet waiting times in the packet buffer.
515class NetEqDecodingTestFaxMode : public NetEqDecodingTest {
516 protected:
517 NetEqDecodingTestFaxMode() : NetEqDecodingTest() {
518 config_.playout_mode = kPlayoutFax;
519 }
520};
521
522TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000523 // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio.
524 size_t num_frames = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000525 const size_t kSamples = 10 * 16;
526 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000527 for (size_t i = 0; i < num_frames; ++i) {
kwibergee2bac22015-11-11 10:34:00 -0800528 const uint8_t payload[kPayloadBytes] = {0};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000529 WebRtcRTPHeader rtp_info;
530 rtp_info.header.sequenceNumber = i;
531 rtp_info.header.timestamp = i * kSamples;
532 rtp_info.header.ssrc = 0x1234; // Just an arbitrary SSRC.
533 rtp_info.header.payloadType = 94; // PCM16b WB codec.
534 rtp_info.header.markerBit = 0;
kwibergee2bac22015-11-11 10:34:00 -0800535 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000536 }
537 // Pull out all data.
538 for (size_t i = 0; i < num_frames; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700539 bool muted;
540 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800541 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000542 }
543
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200544 NetEqNetworkStatistics stats;
545 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000546 // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms
547 // spacing (per definition), we expect the delay to increase with 10 ms for
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200548 // each packet. Thus, we are calculating the statistics for a series from 10
549 // to 300, in steps of 10 ms.
550 EXPECT_EQ(155, stats.mean_waiting_time_ms);
551 EXPECT_EQ(155, stats.median_waiting_time_ms);
552 EXPECT_EQ(10, stats.min_waiting_time_ms);
553 EXPECT_EQ(300, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000554
555 // Check statistics again and make sure it's been reset.
Henrik Lundin1bb8cf82015-08-25 13:08:04 +0200556 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
557 EXPECT_EQ(-1, stats.mean_waiting_time_ms);
558 EXPECT_EQ(-1, stats.median_waiting_time_ms);
559 EXPECT_EQ(-1, stats.min_waiting_time_ms);
560 EXPECT_EQ(-1, stats.max_waiting_time_ms);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000561}
562
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000563TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimeNegative) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000564 const int kNumFrames = 3000; // Needed for convergence.
565 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000566 const size_t kSamples = 10 * 16;
567 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000568 while (frame_index < kNumFrames) {
569 // Insert one packet each time, except every 10th time where we insert two
570 // packets at once. This will create a negative clock-drift of approx. 10%.
571 int num_packets = (frame_index % 10 == 0 ? 2 : 1);
572 for (int n = 0; n < num_packets; ++n) {
573 uint8_t payload[kPayloadBytes] = {0};
574 WebRtcRTPHeader rtp_info;
575 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800576 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000577 ++frame_index;
578 }
579
580 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700581 bool muted;
582 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800583 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000584 }
585
586 NetEqNetworkStatistics network_stats;
587 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
588 EXPECT_EQ(-103196, network_stats.clockdrift_ppm);
589}
590
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000591TEST_F(NetEqDecodingTest, TestAverageInterArrivalTimePositive) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000592 const int kNumFrames = 5000; // Needed for convergence.
593 int frame_index = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000594 const size_t kSamples = 10 * 16;
595 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000596 for (int i = 0; i < kNumFrames; ++i) {
597 // Insert one packet each time, except every 10th time where we don't insert
598 // any packet. This will create a positive clock-drift of approx. 11%.
599 int num_packets = (i % 10 == 9 ? 0 : 1);
600 for (int n = 0; n < num_packets; ++n) {
601 uint8_t payload[kPayloadBytes] = {0};
602 WebRtcRTPHeader rtp_info;
603 PopulateRtpInfo(frame_index, frame_index * kSamples, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800604 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000605 ++frame_index;
606 }
607
608 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700609 bool muted;
610 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800611 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000612 }
613
614 NetEqNetworkStatistics network_stats;
615 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
616 EXPECT_EQ(110946, network_stats.clockdrift_ppm);
617}
618
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000619void NetEqDecodingTest::LongCngWithClockDrift(double drift_factor,
620 double network_freeze_ms,
621 bool pull_audio_during_freeze,
622 int delay_tolerance_ms,
623 int max_time_to_speech_ms) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000624 uint16_t seq_no = 0;
625 uint32_t timestamp = 0;
626 const int kFrameSizeMs = 30;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000627 const size_t kSamples = kFrameSizeMs * 16;
628 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000629 double next_input_time_ms = 0.0;
630 double t_ms;
henrik.lundin7a926812016-05-12 13:51:28 -0700631 bool muted;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000632
633 // Insert speech for 5 seconds.
634 const int kSpeechDurationMs = 5000;
635 for (t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
636 // Each turn in this for loop is 10 ms.
637 while (next_input_time_ms <= t_ms) {
638 // Insert one 30 ms speech frame.
639 uint8_t payload[kPayloadBytes] = {0};
640 WebRtcRTPHeader rtp_info;
641 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800642 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000643 ++seq_no;
644 timestamp += kSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000645 next_input_time_ms += static_cast<double>(kFrameSizeMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000646 }
647 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700648 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800649 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000650 }
651
henrik.lundin55480f52016-03-08 02:37:57 -0800652 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700653 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
654 ASSERT_TRUE(playout_timestamp);
655 int32_t delay_before = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000656
657 // Insert CNG for 1 minute (= 60000 ms).
658 const int kCngPeriodMs = 100;
659 const int kCngPeriodSamples = kCngPeriodMs * 16; // Period in 16 kHz samples.
660 const int kCngDurationMs = 60000;
661 for (; t_ms < kSpeechDurationMs + kCngDurationMs; t_ms += 10) {
662 // Each turn in this for loop is 10 ms.
663 while (next_input_time_ms <= t_ms) {
664 // Insert one CNG frame each 100 ms.
665 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000666 size_t payload_len;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000667 WebRtcRTPHeader rtp_info;
668 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800669 ASSERT_EQ(0, neteq_->InsertPacket(
670 rtp_info,
671 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000672 ++seq_no;
673 timestamp += kCngPeriodSamples;
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000674 next_input_time_ms += static_cast<double>(kCngPeriodMs) * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000675 }
676 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700677 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800678 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000679 }
680
henrik.lundin55480f52016-03-08 02:37:57 -0800681 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000682
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000683 if (network_freeze_ms > 0) {
684 // First keep pulling audio for |network_freeze_ms| without inserting
685 // any data, then insert CNG data corresponding to |network_freeze_ms|
686 // without pulling any output audio.
687 const double loop_end_time = t_ms + network_freeze_ms;
688 for (; t_ms < loop_end_time; t_ms += 10) {
689 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700690 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800691 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800692 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000693 }
694 bool pull_once = pull_audio_during_freeze;
695 // If |pull_once| is true, GetAudio will be called once half-way through
696 // the network recovery period.
697 double pull_time_ms = (t_ms + next_input_time_ms) / 2;
698 while (next_input_time_ms <= t_ms) {
699 if (pull_once && next_input_time_ms >= pull_time_ms) {
700 pull_once = false;
701 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700702 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800703 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800704 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000705 t_ms += 10;
706 }
707 // Insert one CNG frame each 100 ms.
708 uint8_t payload[kPayloadBytes];
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000709 size_t payload_len;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000710 WebRtcRTPHeader rtp_info;
711 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -0800712 ASSERT_EQ(0, neteq_->InsertPacket(
713 rtp_info,
714 rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000715 ++seq_no;
716 timestamp += kCngPeriodSamples;
717 next_input_time_ms += kCngPeriodMs * drift_factor;
718 }
719 }
720
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000721 // Insert speech again until output type is speech.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000722 double speech_restart_time_ms = t_ms;
henrik.lundin55480f52016-03-08 02:37:57 -0800723 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000724 // Each turn in this for loop is 10 ms.
725 while (next_input_time_ms <= t_ms) {
726 // Insert one 30 ms speech frame.
727 uint8_t payload[kPayloadBytes] = {0};
728 WebRtcRTPHeader rtp_info;
729 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -0800730 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000731 ++seq_no;
732 timestamp += kSamples;
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000733 next_input_time_ms += kFrameSizeMs * drift_factor;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000734 }
735 // Pull out data once.
henrik.lundin7a926812016-05-12 13:51:28 -0700736 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800737 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000738 // Increase clock.
739 t_ms += 10;
740 }
741
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000742 // Check that the speech starts again within reasonable time.
743 double time_until_speech_returns_ms = t_ms - speech_restart_time_ms;
744 EXPECT_LT(time_until_speech_returns_ms, max_time_to_speech_ms);
henrik.lundin0d96ab72016-04-06 12:28:26 -0700745 playout_timestamp = PlayoutTimestamp();
746 ASSERT_TRUE(playout_timestamp);
747 int32_t delay_after = timestamp - *playout_timestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000748 // Compare delay before and after, and make sure it differs less than 20 ms.
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000749 EXPECT_LE(delay_after, delay_before + delay_tolerance_ms * 16);
750 EXPECT_GE(delay_after, delay_before - delay_tolerance_ms * 16);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000751}
752
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000753TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000754 // Apply a clock drift of -25 ms / s (sender faster than receiver).
755 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000756 const double kNetworkFreezeTimeMs = 0.0;
757 const bool kGetAudioDuringFreezeRecovery = false;
758 const int kDelayToleranceMs = 20;
759 const int kMaxTimeToSpeechMs = 100;
760 LongCngWithClockDrift(kDriftFactor,
761 kNetworkFreezeTimeMs,
762 kGetAudioDuringFreezeRecovery,
763 kDelayToleranceMs,
764 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000765}
766
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000767TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) {
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000768 // Apply a clock drift of +25 ms / s (sender slower than receiver).
769 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000770 const double kNetworkFreezeTimeMs = 0.0;
771 const bool kGetAudioDuringFreezeRecovery = false;
772 const int kDelayToleranceMs = 20;
773 const int kMaxTimeToSpeechMs = 100;
774 LongCngWithClockDrift(kDriftFactor,
775 kNetworkFreezeTimeMs,
776 kGetAudioDuringFreezeRecovery,
777 kDelayToleranceMs,
778 kMaxTimeToSpeechMs);
779}
780
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000781TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000782 // Apply a clock drift of -25 ms / s (sender faster than receiver).
783 const double kDriftFactor = 1000.0 / (1000.0 + 25.0);
784 const double kNetworkFreezeTimeMs = 5000.0;
785 const bool kGetAudioDuringFreezeRecovery = false;
786 const int kDelayToleranceMs = 50;
787 const int kMaxTimeToSpeechMs = 200;
788 LongCngWithClockDrift(kDriftFactor,
789 kNetworkFreezeTimeMs,
790 kGetAudioDuringFreezeRecovery,
791 kDelayToleranceMs,
792 kMaxTimeToSpeechMs);
793}
794
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000795TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000796 // Apply a clock drift of +25 ms / s (sender slower than receiver).
797 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
798 const double kNetworkFreezeTimeMs = 5000.0;
799 const bool kGetAudioDuringFreezeRecovery = false;
800 const int kDelayToleranceMs = 20;
801 const int kMaxTimeToSpeechMs = 100;
802 LongCngWithClockDrift(kDriftFactor,
803 kNetworkFreezeTimeMs,
804 kGetAudioDuringFreezeRecovery,
805 kDelayToleranceMs,
806 kMaxTimeToSpeechMs);
807}
808
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000809TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000810 // Apply a clock drift of +25 ms / s (sender slower than receiver).
811 const double kDriftFactor = 1000.0 / (1000.0 - 25.0);
812 const double kNetworkFreezeTimeMs = 5000.0;
813 const bool kGetAudioDuringFreezeRecovery = true;
814 const int kDelayToleranceMs = 20;
815 const int kMaxTimeToSpeechMs = 100;
816 LongCngWithClockDrift(kDriftFactor,
817 kNetworkFreezeTimeMs,
818 kGetAudioDuringFreezeRecovery,
819 kDelayToleranceMs,
820 kMaxTimeToSpeechMs);
821}
822
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000823TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) {
henrik.lundin@webrtc.org24779fe2014-03-14 12:40:05 +0000824 const double kDriftFactor = 1.0; // No drift.
825 const double kNetworkFreezeTimeMs = 0.0;
826 const bool kGetAudioDuringFreezeRecovery = false;
827 const int kDelayToleranceMs = 10;
828 const int kMaxTimeToSpeechMs = 50;
829 LongCngWithClockDrift(kDriftFactor,
830 kNetworkFreezeTimeMs,
831 kGetAudioDuringFreezeRecovery,
832 kDelayToleranceMs,
833 kMaxTimeToSpeechMs);
henrik.lundin@webrtc.orgfcfc6a92014-02-13 11:42:28 +0000834}
835
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000836TEST_F(NetEqDecodingTest, UnknownPayloadType) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000837 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000838 uint8_t payload[kPayloadBytes] = {0};
839 WebRtcRTPHeader rtp_info;
840 PopulateRtpInfo(0, 0, &rtp_info);
841 rtp_info.header.payloadType = 1; // Not registered as a decoder.
kwibergee2bac22015-11-11 10:34:00 -0800842 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000843 EXPECT_EQ(NetEq::kUnknownRtpPayloadType, neteq_->LastError());
844}
845
Peter Boströme2976c82016-01-04 22:44:05 +0100846#if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
ivoc72c08ed2016-01-20 07:26:24 -0800847#define MAYBE_DecoderError DecoderError
848#else
849#define MAYBE_DecoderError DISABLED_DecoderError
850#endif
851
Peter Boströme2976c82016-01-04 22:44:05 +0100852TEST_F(NetEqDecodingTest, MAYBE_DecoderError) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000853 const size_t kPayloadBytes = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000854 uint8_t payload[kPayloadBytes] = {0};
855 WebRtcRTPHeader rtp_info;
856 PopulateRtpInfo(0, 0, &rtp_info);
857 rtp_info.header.payloadType = 103; // iSAC, but the payload is invalid.
kwibergee2bac22015-11-11 10:34:00 -0800858 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000859 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
860 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800861 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
862 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000863 }
henrik.lundin7a926812016-05-12 13:51:28 -0700864 bool muted;
865 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted));
866 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000867 // Verify that there is a decoder error to check.
868 EXPECT_EQ(NetEq::kDecoderErrorCode, neteq_->LastError());
ivoc72c08ed2016-01-20 07:26:24 -0800869
870 enum NetEqDecoderError {
871 ISAC_LENGTH_MISMATCH = 6730,
872 ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH = 6640
873 };
874#if defined(WEBRTC_CODEC_ISAC)
875 EXPECT_EQ(ISAC_LENGTH_MISMATCH, neteq_->LastDecoderError());
876#elif defined(WEBRTC_CODEC_ISACFX)
877 EXPECT_EQ(ISAC_RANGE_ERROR_DECODE_FRAME_LENGTH, neteq_->LastDecoderError());
878#endif
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000879 // Verify that the first 160 samples are set to 0, and that the remaining
880 // samples are left unmodified.
881 static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate.
882 for (int i = 0; i < kExpectedOutputLength; ++i) {
883 std::ostringstream ss;
884 ss << "i = " << i;
885 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800886 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000887 }
henrik.lundin6d8e0112016-03-04 10:34:21 -0800888 for (size_t i = kExpectedOutputLength; i < AudioFrame::kMaxDataSizeSamples;
889 ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000890 std::ostringstream ss;
891 ss << "i = " << i;
892 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800893 EXPECT_EQ(1, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000894 }
895}
896
henrik.lundin@webrtc.orgb4e80e02014-05-15 07:14:00 +0000897TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000898 // Set all of |out_data_| to 1, and verify that it was set to 0 by the call
899 // to GetAudio.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800900 for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) {
901 out_frame_.data_[i] = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000902 }
henrik.lundin7a926812016-05-12 13:51:28 -0700903 bool muted;
904 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
905 ASSERT_FALSE(muted);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000906 // Verify that the first block of samples is set to 0.
907 static const int kExpectedOutputLength =
908 kInitSampleRateHz / 100; // 10 ms at initial sample rate.
909 for (int i = 0; i < kExpectedOutputLength; ++i) {
910 std::ostringstream ss;
911 ss << "i = " << i;
912 SCOPED_TRACE(ss.str()); // Print out the parameter values on failure.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800913 EXPECT_EQ(0, out_frame_.data_[i]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000914 }
henrik.lundind89814b2015-11-23 06:49:25 -0800915 // Verify that the sample rate did not change from the initial configuration.
916 EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000917}
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000918
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000919class NetEqBgnTest : public NetEqDecodingTest {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000920 protected:
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000921 virtual void TestCondition(double sum_squared_noise,
922 bool should_be_faded) = 0;
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000923
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000924 void CheckBgn(int sampling_rate_hz) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700925 size_t expected_samples_per_channel = 0;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000926 uint8_t payload_type = 0xFF; // Invalid.
927 if (sampling_rate_hz == 8000) {
928 expected_samples_per_channel = kBlockSize8kHz;
929 payload_type = 93; // PCM 16, 8 kHz.
930 } else if (sampling_rate_hz == 16000) {
931 expected_samples_per_channel = kBlockSize16kHz;
932 payload_type = 94; // PCM 16, 16 kHZ.
933 } else if (sampling_rate_hz == 32000) {
934 expected_samples_per_channel = kBlockSize32kHz;
935 payload_type = 95; // PCM 16, 32 kHz.
936 } else {
937 ASSERT_TRUE(false); // Unsupported test case.
938 }
turaj@webrtc.orgff43c852013-09-25 00:07:27 +0000939
henrik.lundin6d8e0112016-03-04 10:34:21 -0800940 AudioFrame output;
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000941 test::AudioLoop input;
942 // We are using the same 32 kHz input file for all tests, regardless of
943 // |sampling_rate_hz|. The output may sound weird, but the test is still
944 // valid.
945 ASSERT_TRUE(input.Init(
946 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"),
947 10 * sampling_rate_hz, // Max 10 seconds loop length.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700948 expected_samples_per_channel));
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000949
950 // Payload of 10 ms of PCM16 32 kHz.
951 uint8_t payload[kBlockSize32kHz * sizeof(int16_t)];
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000952 WebRtcRTPHeader rtp_info;
953 PopulateRtpInfo(0, 0, &rtp_info);
954 rtp_info.header.payloadType = payload_type;
955
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000956 uint32_t receive_timestamp = 0;
henrik.lundin7a926812016-05-12 13:51:28 -0700957 bool muted;
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000958 for (int n = 0; n < 10; ++n) { // Insert few packets and get audio.
kwiberg288886b2015-11-06 01:21:35 -0800959 auto block = input.GetNextBlock();
960 ASSERT_EQ(expected_samples_per_channel, block.size());
961 size_t enc_len_bytes =
962 WebRtcPcm16b_Encode(block.data(), block.size(), payload);
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +0000963 ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2);
964
kwibergee2bac22015-11-11 10:34:00 -0800965 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>(
966 payload, enc_len_bytes),
967 receive_timestamp));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800968 output.Reset();
henrik.lundin7a926812016-05-12 13:51:28 -0700969 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800970 ASSERT_EQ(1u, output.num_channels_);
971 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -0800972 ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000973
974 // Next packet.
975 rtp_info.header.timestamp += expected_samples_per_channel;
976 rtp_info.header.sequenceNumber++;
977 receive_timestamp += expected_samples_per_channel;
978 }
979
henrik.lundin6d8e0112016-03-04 10:34:21 -0800980 output.Reset();
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000981
982 // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull
983 // one frame without checking speech-type. This is the first frame pulled
984 // without inserting any packet, and might not be labeled as PLC.
henrik.lundin7a926812016-05-12 13:51:28 -0700985 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800986 ASSERT_EQ(1u, output.num_channels_);
987 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +0000988
989 // To be able to test the fading of background noise we need at lease to
990 // pull 611 frames.
991 const int kFadingThreshold = 611;
992
993 // Test several CNG-to-PLC packet for the expected behavior. The number 20
994 // is arbitrary, but sufficiently large to test enough number of frames.
995 const int kNumPlcToCngTestFrames = 20;
996 bool plc_to_cng = false;
997 for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800998 output.Reset();
999 memset(output.data_, 1, sizeof(output.data_)); // Set to non-zero.
henrik.lundin7a926812016-05-12 13:51:28 -07001000 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
1001 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001002 ASSERT_EQ(1u, output.num_channels_);
1003 ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001004 if (output.speech_type_ == AudioFrame::kPLCCNG) {
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001005 plc_to_cng = true;
1006 double sum_squared = 0;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001007 for (size_t k = 0;
1008 k < output.num_channels_ * output.samples_per_channel_; ++k)
1009 sum_squared += output.data_[k] * output.data_[k];
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001010 TestCondition(sum_squared, n > kFadingThreshold);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001011 } else {
henrik.lundin55480f52016-03-08 02:37:57 -08001012 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001013 }
1014 }
1015 EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred.
1016 }
1017};
1018
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001019class NetEqBgnTestOn : public NetEqBgnTest {
1020 protected:
1021 NetEqBgnTestOn() : NetEqBgnTest() {
1022 config_.background_noise_mode = NetEq::kBgnOn;
1023 }
1024
1025 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1026 EXPECT_NE(0, sum_squared_noise);
1027 }
1028};
1029
1030class NetEqBgnTestOff : public NetEqBgnTest {
1031 protected:
1032 NetEqBgnTestOff() : NetEqBgnTest() {
1033 config_.background_noise_mode = NetEq::kBgnOff;
1034 }
1035
1036 void TestCondition(double sum_squared_noise, bool /*should_be_faded*/) {
1037 EXPECT_EQ(0, sum_squared_noise);
1038 }
1039};
1040
1041class NetEqBgnTestFade : public NetEqBgnTest {
1042 protected:
1043 NetEqBgnTestFade() : NetEqBgnTest() {
1044 config_.background_noise_mode = NetEq::kBgnFade;
1045 }
1046
1047 void TestCondition(double sum_squared_noise, bool should_be_faded) {
1048 if (should_be_faded)
1049 EXPECT_EQ(0, sum_squared_noise);
1050 }
1051};
1052
henrika1d34fe92015-06-16 10:04:20 +02001053TEST_F(NetEqBgnTestOn, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001054 CheckBgn(8000);
1055 CheckBgn(16000);
1056 CheckBgn(32000);
turaj@webrtc.orgff43c852013-09-25 00:07:27 +00001057}
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +00001058
henrika1d34fe92015-06-16 10:04:20 +02001059TEST_F(NetEqBgnTestOff, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001060 CheckBgn(8000);
1061 CheckBgn(16000);
1062 CheckBgn(32000);
1063}
1064
henrika1d34fe92015-06-16 10:04:20 +02001065TEST_F(NetEqBgnTestFade, RunTest) {
henrik.lundin@webrtc.org9b8102c2014-08-21 08:27:44 +00001066 CheckBgn(8000);
1067 CheckBgn(16000);
1068 CheckBgn(32000);
1069}
henrik.lundin@webrtc.orgea257842014-08-07 12:27:37 +00001070
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001071void NetEqDecodingTest::WrapTest(uint16_t start_seq_no,
1072 uint32_t start_timestamp,
1073 const std::set<uint16_t>& drop_seq_numbers,
1074 bool expect_seq_no_wrap,
1075 bool expect_timestamp_wrap) {
1076 uint16_t seq_no = start_seq_no;
1077 uint32_t timestamp = start_timestamp;
1078 const int kBlocksPerFrame = 3; // Number of 10 ms blocks per frame.
1079 const int kFrameSizeMs = kBlocksPerFrame * kTimeStepMs;
1080 const int kSamples = kBlockSize16kHz * kBlocksPerFrame;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001081 const size_t kPayloadBytes = kSamples * sizeof(int16_t);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001082 double next_input_time_ms = 0.0;
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001083 uint32_t receive_timestamp = 0;
1084
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001085 // Insert speech for 2 seconds.
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001086 const int kSpeechDurationMs = 2000;
1087 int packets_inserted = 0;
1088 uint16_t last_seq_no;
1089 uint32_t last_timestamp;
1090 bool timestamp_wrapped = false;
1091 bool seq_no_wrapped = false;
1092 for (double t_ms = 0; t_ms < kSpeechDurationMs; t_ms += 10) {
1093 // Each turn in this for loop is 10 ms.
1094 while (next_input_time_ms <= t_ms) {
1095 // Insert one 30 ms speech frame.
1096 uint8_t payload[kPayloadBytes] = {0};
1097 WebRtcRTPHeader rtp_info;
1098 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
1099 if (drop_seq_numbers.find(seq_no) == drop_seq_numbers.end()) {
1100 // This sequence number was not in the set to drop. Insert it.
1101 ASSERT_EQ(0,
kwibergee2bac22015-11-11 10:34:00 -08001102 neteq_->InsertPacket(rtp_info, payload, receive_timestamp));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001103 ++packets_inserted;
1104 }
1105 NetEqNetworkStatistics network_stats;
1106 ASSERT_EQ(0, neteq_->NetworkStatistics(&network_stats));
1107
1108 // Due to internal NetEq logic, preferred buffer-size is about 4 times the
1109 // packet size for first few packets. Therefore we refrain from checking
1110 // the criteria.
1111 if (packets_inserted > 4) {
1112 // Expect preferred and actual buffer size to be no more than 2 frames.
1113 EXPECT_LE(network_stats.preferred_buffer_size_ms, kFrameSizeMs * 2);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001114 EXPECT_LE(network_stats.current_buffer_size_ms, kFrameSizeMs * 2 +
1115 algorithmic_delay_ms_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001116 }
1117 last_seq_no = seq_no;
1118 last_timestamp = timestamp;
1119
1120 ++seq_no;
1121 timestamp += kSamples;
1122 receive_timestamp += kSamples;
1123 next_input_time_ms += static_cast<double>(kFrameSizeMs);
1124
1125 seq_no_wrapped |= seq_no < last_seq_no;
1126 timestamp_wrapped |= timestamp < last_timestamp;
1127 }
1128 // Pull out data once.
henrik.lundin6d8e0112016-03-04 10:34:21 -08001129 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001130 bool muted;
1131 ASSERT_EQ(0, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001132 ASSERT_EQ(kBlockSize16kHz, output.samples_per_channel_);
1133 ASSERT_EQ(1u, output.num_channels_);
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001134
1135 // Expect delay (in samples) to be less than 2 packets.
henrik.lundin0d96ab72016-04-06 12:28:26 -07001136 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1137 ASSERT_TRUE(playout_timestamp);
1138 EXPECT_LE(timestamp - *playout_timestamp,
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001139 static_cast<uint32_t>(kSamples * 2));
turaj@webrtc.org78b41a02013-11-22 20:27:07 +00001140 }
1141 // Make sure we have actually tested wrap-around.
1142 ASSERT_EQ(expect_seq_no_wrap, seq_no_wrapped);
1143 ASSERT_EQ(expect_timestamp_wrap, timestamp_wrapped);
1144}
1145
1146TEST_F(NetEqDecodingTest, SequenceNumberWrap) {
1147 // Start with a sequence number that will soon wrap.
1148 std::set<uint16_t> drop_seq_numbers; // Don't drop any packets.
1149 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1150}
1151
1152TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) {
1153 // Start with a sequence number that will soon wrap.
1154 std::set<uint16_t> drop_seq_numbers;
1155 drop_seq_numbers.insert(0xFFFF);
1156 drop_seq_numbers.insert(0x0);
1157 WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false);
1158}
1159
1160TEST_F(NetEqDecodingTest, TimestampWrap) {
1161 // Start with a timestamp that will soon wrap.
1162 std::set<uint16_t> drop_seq_numbers;
1163 WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true);
1164}
1165
1166TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) {
1167 // Start with a timestamp and a sequence number that will wrap at the same
1168 // time.
1169 std::set<uint16_t> drop_seq_numbers;
1170 WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true);
1171}
1172
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001173void NetEqDecodingTest::DuplicateCng() {
1174 uint16_t seq_no = 0;
1175 uint32_t timestamp = 0;
1176 const int kFrameSizeMs = 10;
1177 const int kSampleRateKhz = 16;
1178 const int kSamples = kFrameSizeMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001179 const size_t kPayloadBytes = kSamples * 2;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001180
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001181 const int algorithmic_delay_samples = std::max(
1182 algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001183 // Insert three speech packets. Three are needed to get the frame length
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001184 // correct.
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001185 uint8_t payload[kPayloadBytes] = {0};
1186 WebRtcRTPHeader rtp_info;
henrik.lundin7a926812016-05-12 13:51:28 -07001187 bool muted;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001188 for (int i = 0; i < 3; ++i) {
1189 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001190 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001191 ++seq_no;
1192 timestamp += kSamples;
1193
1194 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001195 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001196 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001197 }
1198 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001199 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001200
1201 // Insert same CNG packet twice.
1202 const int kCngPeriodMs = 100;
1203 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001204 size_t payload_len;
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001205 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
1206 // This is the first time this CNG packet is inserted.
kwibergee2bac22015-11-11 10:34:00 -08001207 ASSERT_EQ(
1208 0, neteq_->InsertPacket(
1209 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001210
1211 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001212 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001213 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001214 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001215 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
1216 EXPECT_EQ(timestamp - algorithmic_delay_samples,
1217 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001218
1219 // Insert the same CNG packet again. Note that at this point it is old, since
1220 // we have already decoded the first copy of it.
kwibergee2bac22015-11-11 10:34:00 -08001221 ASSERT_EQ(
1222 0, neteq_->InsertPacket(
1223 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001224
1225 // Pull audio until we have played |kCngPeriodMs| of CNG. Start at 10 ms since
1226 // we have already pulled out CNG once.
1227 for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) {
henrik.lundin7a926812016-05-12 13:51:28 -07001228 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001229 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001230 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001231 EXPECT_FALSE(PlayoutTimestamp()); // Returns empty value during CNG.
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001232 EXPECT_EQ(timestamp - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001233 out_frame_.timestamp_ + out_frame_.samples_per_channel_);
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001234 }
1235
1236 // Insert speech again.
1237 ++seq_no;
1238 timestamp += kCngPeriodSamples;
1239 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001240 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001241
1242 // Pull audio once and verify that the output is speech again.
henrik.lundin7a926812016-05-12 13:51:28 -07001243 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001244 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001245 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin0d96ab72016-04-06 12:28:26 -07001246 rtc::Optional<uint32_t> playout_timestamp = PlayoutTimestamp();
1247 ASSERT_TRUE(playout_timestamp);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +00001248 EXPECT_EQ(timestamp + kSamples - algorithmic_delay_samples,
henrik.lundin0d96ab72016-04-06 12:28:26 -07001249 *playout_timestamp);
wu@webrtc.org94454b72014-06-05 20:34:08 +00001250}
1251
henrik.lundin0d96ab72016-04-06 12:28:26 -07001252rtc::Optional<uint32_t> NetEqDecodingTest::PlayoutTimestamp() {
1253 return neteq_->GetPlayoutTimestamp();
henrik.lundin@webrtc.orgca8cb952014-03-12 10:26:52 +00001254}
1255
1256TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { DuplicateCng(); }
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001257
1258TEST_F(NetEqDecodingTest, CngFirst) {
1259 uint16_t seq_no = 0;
1260 uint32_t timestamp = 0;
1261 const int kFrameSizeMs = 10;
1262 const int kSampleRateKhz = 16;
1263 const int kSamples = kFrameSizeMs * kSampleRateKhz;
1264 const int kPayloadBytes = kSamples * 2;
1265 const int kCngPeriodMs = 100;
1266 const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz;
1267 size_t payload_len;
1268
1269 uint8_t payload[kPayloadBytes] = {0};
1270 WebRtcRTPHeader rtp_info;
1271
1272 PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len);
kwibergee2bac22015-11-11 10:34:00 -08001273 ASSERT_EQ(
1274 NetEq::kOK,
1275 neteq_->InsertPacket(
1276 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001277 ++seq_no;
1278 timestamp += kCngPeriodSamples;
1279
1280 // Pull audio once and make sure CNG is played.
henrik.lundin7a926812016-05-12 13:51:28 -07001281 bool muted;
1282 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001283 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin55480f52016-03-08 02:37:57 -08001284 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001285
1286 // Insert some speech packets.
henrik.lundin549d80b2016-08-25 00:44:24 -07001287 const uint32_t first_speech_timestamp = timestamp;
1288 int timeout_counter = 0;
1289 do {
1290 ASSERT_LT(timeout_counter++, 20) << "Test timed out";
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001291 PopulateRtpInfo(seq_no, timestamp, &rtp_info);
kwibergee2bac22015-11-11 10:34:00 -08001292 ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001293 ++seq_no;
1294 timestamp += kSamples;
1295
1296 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -07001297 ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001298 ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_);
henrik.lundin549d80b2016-08-25 00:44:24 -07001299 } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp));
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001300 // Verify speech output.
henrik.lundin55480f52016-03-08 02:37:57 -08001301 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
henrik.lundin@webrtc.orgc93437e2014-12-01 11:42:42 +00001302}
henrik.lundin7a926812016-05-12 13:51:28 -07001303
1304class NetEqDecodingTestWithMutedState : public NetEqDecodingTest {
1305 public:
1306 NetEqDecodingTestWithMutedState() : NetEqDecodingTest() {
1307 config_.enable_muted_state = true;
1308 }
1309
1310 protected:
1311 static constexpr size_t kSamples = 10 * 16;
1312 static constexpr size_t kPayloadBytes = kSamples * 2;
1313
1314 void InsertPacket(uint32_t rtp_timestamp) {
1315 uint8_t payload[kPayloadBytes] = {0};
1316 WebRtcRTPHeader rtp_info;
1317 PopulateRtpInfo(0, rtp_timestamp, &rtp_info);
1318 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1319 }
1320
henrik.lundin42feb512016-09-20 06:51:40 -07001321 void InsertCngPacket(uint32_t rtp_timestamp) {
1322 uint8_t payload[kPayloadBytes] = {0};
1323 WebRtcRTPHeader rtp_info;
1324 size_t payload_len;
1325 PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len);
1326 EXPECT_EQ(
1327 NetEq::kOK,
1328 neteq_->InsertPacket(
1329 rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len), 0));
1330 }
1331
henrik.lundin7a926812016-05-12 13:51:28 -07001332 bool GetAudioReturnMuted() {
1333 bool muted;
1334 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1335 return muted;
1336 }
1337
1338 void GetAudioUntilMuted() {
1339 while (!GetAudioReturnMuted()) {
1340 ASSERT_LT(counter_++, 1000) << "Test timed out";
1341 }
1342 }
1343
1344 void GetAudioUntilNormal() {
1345 bool muted = false;
1346 while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) {
1347 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1348 ASSERT_LT(counter_++, 1000) << "Test timed out";
1349 }
1350 EXPECT_FALSE(muted);
1351 }
1352
1353 int counter_ = 0;
1354};
1355
1356// Verifies that NetEq goes in and out of muted state as expected.
1357TEST_F(NetEqDecodingTestWithMutedState, MutedState) {
1358 // Insert one speech packet.
1359 InsertPacket(0);
1360 // Pull out audio once and expect it not to be muted.
1361 EXPECT_FALSE(GetAudioReturnMuted());
1362 // Pull data until faded out.
1363 GetAudioUntilMuted();
1364
1365 // Verify that output audio is not written during muted mode. Other parameters
1366 // should be correct, though.
1367 AudioFrame new_frame;
1368 for (auto& d : new_frame.data_) {
1369 d = 17;
1370 }
1371 bool muted;
1372 EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted));
1373 EXPECT_TRUE(muted);
1374 for (auto d : new_frame.data_) {
1375 EXPECT_EQ(17, d);
1376 }
1377 EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_,
1378 new_frame.timestamp_);
1379 EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_);
1380 EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_);
1381 EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_);
1382 EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_);
1383 EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_);
1384
1385 // Insert new data. Timestamp is corrected for the time elapsed since the last
1386 // packet. Verify that normal operation resumes.
1387 InsertPacket(kSamples * counter_);
1388 GetAudioUntilNormal();
henrik.lundin612c25e2016-05-25 08:21:04 -07001389
1390 NetEqNetworkStatistics stats;
1391 EXPECT_EQ(0, neteq_->NetworkStatistics(&stats));
1392 // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were
1393 // concealment samples, in Q14 (16384 = 100%) .The vast majority should be
1394 // concealment samples in this test.
1395 EXPECT_GT(stats.expand_rate, 14000);
1396 // And, it should be greater than the speech_expand_rate.
1397 EXPECT_GT(stats.expand_rate, stats.speech_expand_rate);
henrik.lundin7a926812016-05-12 13:51:28 -07001398}
1399
1400// Verifies that NetEq goes out of muted state when given a delayed packet.
1401TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) {
1402 // Insert one speech packet.
1403 InsertPacket(0);
1404 // Pull out audio once and expect it not to be muted.
1405 EXPECT_FALSE(GetAudioReturnMuted());
1406 // Pull data until faded out.
1407 GetAudioUntilMuted();
1408 // Insert new data. Timestamp is only corrected for the half of the time
1409 // elapsed since the last packet. That is, the new packet is delayed. Verify
1410 // that normal operation resumes.
1411 InsertPacket(kSamples * counter_ / 2);
1412 GetAudioUntilNormal();
1413}
1414
1415// Verifies that NetEq goes out of muted state when given a future packet.
1416TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) {
1417 // Insert one speech packet.
1418 InsertPacket(0);
1419 // Pull out audio once and expect it not to be muted.
1420 EXPECT_FALSE(GetAudioReturnMuted());
1421 // Pull data until faded out.
1422 GetAudioUntilMuted();
1423 // Insert new data. Timestamp is over-corrected for the time elapsed since the
1424 // last packet. That is, the new packet is too early. Verify that normal
1425 // operation resumes.
1426 InsertPacket(kSamples * counter_ * 2);
1427 GetAudioUntilNormal();
1428}
1429
1430// Verifies that NetEq goes out of muted state when given an old packet.
1431TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) {
1432 // Insert one speech packet.
1433 InsertPacket(0);
1434 // Pull out audio once and expect it not to be muted.
1435 EXPECT_FALSE(GetAudioReturnMuted());
1436 // Pull data until faded out.
1437 GetAudioUntilMuted();
1438
1439 EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1440 // Insert packet which is older than the first packet.
1441 InsertPacket(kSamples * (counter_ - 1000));
1442 EXPECT_FALSE(GetAudioReturnMuted());
1443 EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_);
1444}
1445
henrik.lundin42feb512016-09-20 06:51:40 -07001446// Verifies that NetEq doesn't enter muted state when CNG mode is active and the
1447// packet stream is suspended for a long time.
1448TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) {
1449 // Insert one CNG packet.
1450 InsertCngPacket(0);
1451
1452 // Pull 10 seconds of audio (10 ms audio generated per lap).
1453 for (int i = 0; i < 1000; ++i) {
1454 bool muted;
1455 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1456 ASSERT_FALSE(muted);
1457 }
1458 EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_);
1459}
1460
1461// Verifies that NetEq goes back to normal after a long CNG period with the
1462// packet stream suspended.
1463TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) {
1464 // Insert one CNG packet.
1465 InsertCngPacket(0);
1466
1467 // Pull 10 seconds of audio (10 ms audio generated per lap).
1468 for (int i = 0; i < 1000; ++i) {
1469 bool muted;
1470 EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted));
1471 }
1472
1473 // Insert new data. Timestamp is corrected for the time elapsed since the last
1474 // packet. Verify that normal operation resumes.
1475 InsertPacket(kSamples * counter_);
1476 GetAudioUntilNormal();
1477}
1478
henrik.lundin7a926812016-05-12 13:51:28 -07001479class NetEqDecodingTestTwoInstances : public NetEqDecodingTest {
1480 public:
1481 NetEqDecodingTestTwoInstances() : NetEqDecodingTest() {}
1482
1483 void SetUp() override {
1484 NetEqDecodingTest::SetUp();
1485 config2_ = config_;
1486 }
1487
1488 void CreateSecondInstance() {
ossue3525782016-05-25 07:37:43 -07001489 neteq2_.reset(NetEq::Create(config2_, CreateBuiltinAudioDecoderFactory()));
henrik.lundin7a926812016-05-12 13:51:28 -07001490 ASSERT_TRUE(neteq2_);
1491 LoadDecoders(neteq2_.get());
1492 }
1493
1494 protected:
1495 std::unique_ptr<NetEq> neteq2_;
1496 NetEq::Config config2_;
1497};
1498
1499namespace {
1500::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a,
1501 const AudioFrame& b) {
1502 if (a.timestamp_ != b.timestamp_)
1503 return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_
1504 << " != " << b.timestamp_ << ")";
1505 if (a.sample_rate_hz_ != b.sample_rate_hz_)
1506 return ::testing::AssertionFailure() << "sample_rate_hz_ diff ("
1507 << a.sample_rate_hz_
1508 << " != " << b.sample_rate_hz_ << ")";
1509 if (a.samples_per_channel_ != b.samples_per_channel_)
1510 return ::testing::AssertionFailure()
1511 << "samples_per_channel_ diff (" << a.samples_per_channel_
1512 << " != " << b.samples_per_channel_ << ")";
1513 if (a.num_channels_ != b.num_channels_)
1514 return ::testing::AssertionFailure() << "num_channels_ diff ("
1515 << a.num_channels_
1516 << " != " << b.num_channels_ << ")";
1517 if (a.speech_type_ != b.speech_type_)
1518 return ::testing::AssertionFailure() << "speech_type_ diff ("
1519 << a.speech_type_
1520 << " != " << b.speech_type_ << ")";
1521 if (a.vad_activity_ != b.vad_activity_)
1522 return ::testing::AssertionFailure() << "vad_activity_ diff ("
1523 << a.vad_activity_
1524 << " != " << b.vad_activity_ << ")";
1525 return ::testing::AssertionSuccess();
1526}
1527
1528::testing::AssertionResult AudioFramesEqual(const AudioFrame& a,
1529 const AudioFrame& b) {
1530 ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b);
1531 if (!res)
1532 return res;
1533 if (memcmp(
1534 a.data_, b.data_,
1535 a.samples_per_channel_ * a.num_channels_ * sizeof(a.data_[0])) != 0) {
1536 return ::testing::AssertionFailure() << "data_ diff";
1537 }
1538 return ::testing::AssertionSuccess();
1539}
1540
1541} // namespace
1542
1543TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) {
1544 ASSERT_FALSE(config_.enable_muted_state);
1545 config2_.enable_muted_state = true;
1546 CreateSecondInstance();
1547
1548 // Insert one speech packet into both NetEqs.
1549 const size_t kSamples = 10 * 16;
1550 const size_t kPayloadBytes = kSamples * 2;
1551 uint8_t payload[kPayloadBytes] = {0};
1552 WebRtcRTPHeader rtp_info;
1553 PopulateRtpInfo(0, 0, &rtp_info);
1554 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1555 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1556
1557 AudioFrame out_frame1, out_frame2;
1558 bool muted;
1559 for (int i = 0; i < 1000; ++i) {
1560 std::ostringstream ss;
1561 ss << "i = " << i;
1562 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1563 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1564 EXPECT_FALSE(muted);
1565 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1566 if (muted) {
1567 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1568 } else {
1569 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1570 }
1571 }
1572 EXPECT_TRUE(muted);
1573
1574 // Insert new data. Timestamp is corrected for the time elapsed since the last
1575 // packet.
1576 PopulateRtpInfo(0, kSamples * 1000, &rtp_info);
1577 EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload, 0));
1578 EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload, 0));
1579
1580 int counter = 0;
1581 while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
1582 ASSERT_LT(counter++, 1000) << "Test timed out";
1583 std::ostringstream ss;
1584 ss << "counter = " << counter;
1585 SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure.
1586 EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
1587 EXPECT_FALSE(muted);
1588 EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted));
1589 if (muted) {
1590 EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2));
1591 } else {
1592 EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2));
1593 }
1594 }
1595 EXPECT_FALSE(muted);
1596}
1597
henrik.lundin@webrtc.orge7ce4372014-01-09 14:01:55 +00001598} // namespace webrtc