blob: ded54bf7ee789038544e4cb6906ef03d97637aa5 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/neteq_impl.h"
12
kwiberg84be5112016-04-27 01:19:58 -070013#include <memory>
Chen Xing3e8ef942019-07-01 17:16:32 +020014#include <utility>
15#include <vector>
kwiberg84be5112016-04-27 01:19:58 -070016
Niels Möllera0f44302018-11-30 10:45:12 +010017#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/neteq/accelerate.h"
20#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010021#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_coding/neteq/include/neteq.h"
23#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
24#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
25#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
26#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
27#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
28#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
29#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
30#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010032#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "modules/audio_coding/neteq/sync_buffer.h"
34#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_conversions.h"
Chen Xing3e8ef942019-07-01 17:16:32 +020036#include "system_wrappers/include/clock.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010037#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010038#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "test/gmock.h"
40#include "test/gtest.h"
41#include "test/mock_audio_decoder.h"
42#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010045using ::testing::AtLeast;
46using ::testing::DoAll;
Chen Xing3e8ef942019-07-01 17:16:32 +020047using ::testing::ElementsAre;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048using ::testing::InSequence;
49using ::testing::Invoke;
Chen Xing3e8ef942019-07-01 17:16:32 +020050using ::testing::IsEmpty;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000051using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010052using ::testing::Pointee;
53using ::testing::Return;
54using ::testing::ReturnNull;
55using ::testing::SetArgPointee;
56using ::testing::SetArrayArgument;
Chen Xing3e8ef942019-07-01 17:16:32 +020057using ::testing::SizeIs;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010058using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059
60namespace webrtc {
61
62// This function is called when inserting a packet list into the mock packet
63// buffer. The purpose is to delete all inserted packets properly, to avoid
64// memory leaks in the test.
65int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070066 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067 return PacketBuffer::kOK;
68}
69
70class NetEqImplTest : public ::testing::Test {
71 protected:
Chen Xing3e8ef942019-07-01 17:16:32 +020072 NetEqImplTest() : clock_(0) { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000073
Niels Möllera0f44302018-11-30 10:45:12 +010074 void CreateInstance(
75 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
76 ASSERT_TRUE(decoder_factory);
Chen Xing3e8ef942019-07-01 17:16:32 +020077 NetEqImpl::Dependencies deps(config_, &clock_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070078
79 // Get a local pointer to NetEq's TickTimer object.
80 tick_timer_ = deps.tick_timer.get();
81
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000082 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070083 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
84 mock_buffer_level_filter_ = mock.get();
85 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000086 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070087 buffer_level_filter_ = deps.buffer_level_filter.get();
88
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000089 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070090 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
91 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000092 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
93 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070094 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000095 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070096 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000097
henrik.lundin1d9061e2016-04-26 12:19:34 -070098 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070099 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100100 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700101 mock_delay_peak_detector_ = mock.get();
102 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
103 deps.delay_peak_detector = std::move(mock);
104 }
105 delay_peak_detector_ = deps.delay_peak_detector.get();
106
107 if (use_mock_delay_manager_) {
108 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100109 config_.max_packets_in_buffer, config_.min_delay_ms, 1020054733,
110 DelayManager::HistogramMode::INTER_ARRIVAL_TIME,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100111 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
Jakob Ivarsson44507082019-03-05 16:59:03 +0100112 deps.stats.get(), absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700113 mock_delay_manager_ = mock.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700114 deps.delay_manager = std::move(mock);
115 }
116 delay_manager_ = deps.delay_manager.get();
117
118 if (use_mock_dtmf_buffer_) {
119 std::unique_ptr<MockDtmfBuffer> mock(
120 new MockDtmfBuffer(config_.sample_rate_hz));
121 mock_dtmf_buffer_ = mock.get();
122 deps.dtmf_buffer = std::move(mock);
123 }
124 dtmf_buffer_ = deps.dtmf_buffer.get();
125
126 if (use_mock_dtmf_tone_generator_) {
127 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
128 mock_dtmf_tone_generator_ = mock.get();
129 deps.dtmf_tone_generator = std::move(mock);
130 }
131 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
132
133 if (use_mock_packet_buffer_) {
134 std::unique_ptr<MockPacketBuffer> mock(
135 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
136 mock_packet_buffer_ = mock.get();
137 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700138 }
139 packet_buffer_ = deps.packet_buffer.get();
140
141 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700142 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700143 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700144 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700145 }
ossua70695a2016-09-22 02:06:28 -0700146 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700147
148 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
149 new TimestampScaler(*deps.decoder_database.get()));
150
151 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000152 ASSERT_TRUE(neteq_ != NULL);
153 }
154
Niels Möllera0f44302018-11-30 10:45:12 +0100155 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
156
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000157 void UseNoMocks() {
158 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
159 use_mock_buffer_level_filter_ = false;
160 use_mock_decoder_database_ = false;
161 use_mock_delay_peak_detector_ = false;
162 use_mock_delay_manager_ = false;
163 use_mock_dtmf_buffer_ = false;
164 use_mock_dtmf_tone_generator_ = false;
165 use_mock_packet_buffer_ = false;
166 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 }
168
169 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000170 if (use_mock_buffer_level_filter_) {
171 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
172 }
173 if (use_mock_decoder_database_) {
174 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
175 }
176 if (use_mock_delay_manager_) {
177 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
178 }
179 if (use_mock_delay_peak_detector_) {
180 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
181 }
182 if (use_mock_dtmf_buffer_) {
183 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
184 }
185 if (use_mock_dtmf_tone_generator_) {
186 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
187 }
188 if (use_mock_packet_buffer_) {
189 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
190 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191 }
192
Niels Möller05543682019-01-10 16:55:06 +0100193 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800194 const size_t kPayloadLength = 4;
195 const uint8_t kPayloadType = 110;
196 const uint32_t kReceiveTime = 17;
197 const int kSampleRateHz = 16000;
198 config_.sample_rate_hz = kSampleRateHz;
199 UseNoMocks();
200 CreateInstance();
201 // Event: 2, E bit, Volume: 17, Length: 4336.
Jonas Olssona4d87372019-07-05 19:08:33 +0200202 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700203 RTPHeader rtp_header;
204 rtp_header.payloadType = kPayloadType;
205 rtp_header.sequenceNumber = 0x1234;
206 rtp_header.timestamp = 0x12345678;
207 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800208
Niels Möller05543682019-01-10 16:55:06 +0100209 EXPECT_TRUE(neteq_->RegisterPayloadType(
210 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800211
212 // Insert first packet.
213 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700214 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800215
216 // Pull audio once.
217 const size_t kMaxOutputSize =
218 static_cast<size_t>(10 * kSampleRateHz / 1000);
219 AudioFrame output;
220 bool muted;
221 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
222 ASSERT_FALSE(muted);
223 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
224 EXPECT_EQ(1u, output.num_channels_);
225 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
226
Chen Xing3e8ef942019-07-01 17:16:32 +0200227 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
228 // returned by |GetAudio()|.
229 EXPECT_THAT(output.packet_infos_, IsEmpty());
230
solenberg2779bab2016-11-17 04:45:19 -0800231 // Verify first 64 samples of actual output.
Jonas Olssona4d87372019-07-05 19:08:33 +0200232 const std::vector<int16_t> kOutput(
233 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
234 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
235 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
236 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
237 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
238 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
239 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800240 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700241 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800242 }
243
henrik.lundin1d9061e2016-04-26 12:19:34 -0700244 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000245 NetEq::Config config_;
Chen Xing3e8ef942019-07-01 17:16:32 +0200246 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700247 TickTimer* tick_timer_ = nullptr;
248 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
249 BufferLevelFilter* buffer_level_filter_ = nullptr;
250 bool use_mock_buffer_level_filter_ = true;
251 MockDecoderDatabase* mock_decoder_database_ = nullptr;
252 DecoderDatabase* decoder_database_ = nullptr;
253 bool use_mock_decoder_database_ = true;
254 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
255 DelayPeakDetector* delay_peak_detector_ = nullptr;
256 bool use_mock_delay_peak_detector_ = true;
257 MockDelayManager* mock_delay_manager_ = nullptr;
258 DelayManager* delay_manager_ = nullptr;
259 bool use_mock_delay_manager_ = true;
260 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
261 DtmfBuffer* dtmf_buffer_ = nullptr;
262 bool use_mock_dtmf_buffer_ = true;
263 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
264 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
265 bool use_mock_dtmf_tone_generator_ = true;
266 MockPacketBuffer* mock_packet_buffer_ = nullptr;
267 PacketBuffer* packet_buffer_ = nullptr;
268 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700269 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
270 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700271 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272};
273
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274// This tests the interface class NetEq.
275// TODO(hlundin): Move to separate file?
276TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000277 NetEq::Config config;
Chen Xing3e8ef942019-07-01 17:16:32 +0200278 SimulatedClock clock(0);
279 NetEq* neteq =
280 NetEq::Create(config, &clock, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 delete neteq;
282}
283
kwiberg5adaf732016-10-04 09:33:27 -0700284TEST_F(NetEqImplTest, RegisterPayloadType) {
285 CreateInstance();
286 constexpr int rtp_payload_type = 0;
287 const SdpAudioFormat format("pcmu", 8000, 1);
288 EXPECT_CALL(*mock_decoder_database_,
289 RegisterPayload(rtp_payload_type, format));
290 neteq_->RegisterPayloadType(rtp_payload_type, format);
291}
292
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000294 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000296 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000297 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200298 // Check that kOK is returned when database returns kDecoderNotFound, because
299 // removing a payload type that was never registered is not an error.
300 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301}
302
kwiberg6b19b562016-09-20 04:02:25 -0700303TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
304 CreateInstance();
305 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
306 neteq_->RemoveAllPayloadTypes();
307}
308
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000310 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000311 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312 const uint8_t kPayloadType = 0;
313 const uint16_t kFirstSequenceNumber = 0x1234;
314 const uint32_t kFirstTimestamp = 0x12345678;
315 const uint32_t kSsrc = 0x87654321;
316 const uint32_t kFirstReceiveTime = 17;
317 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700318 RTPHeader rtp_header;
319 rtp_header.payloadType = kPayloadType;
320 rtp_header.sequenceNumber = kFirstSequenceNumber;
321 rtp_header.timestamp = kFirstTimestamp;
322 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700323 Packet fake_packet;
324 fake_packet.payload_type = kPayloadType;
325 fake_packet.sequence_number = kFirstSequenceNumber;
326 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327
kwibergc0f2dcf2016-05-31 06:28:03 -0700328 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
329 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100330 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800331 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200332 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800333 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700334 EXPECT_EQ("pcmu", format.name);
335
336 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
337 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
338 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
339 // BWE update function called with first packet.
340 EXPECT_CALL(*mock_decoder,
341 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
342 kFirstTimestamp, kFirstReceiveTime));
343 // BWE update function called with second packet.
344 EXPECT_CALL(
345 *mock_decoder,
346 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
347 kFirstTimestamp + 160, kFirstReceiveTime + 155));
348 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
349
350 *dec = std::move(mock_decoder);
351 }));
Niels Möller72899062019-01-11 09:36:13 +0100352 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
353 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354
355 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000356 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 .WillRepeatedly(Return(&info));
358
359 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000360 EXPECT_CALL(*mock_packet_buffer_, Empty())
361 .WillOnce(Return(false)); // Called once after first packet is inserted.
Jonas Olssona4d87372019-07-05 19:08:33 +0200362 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200363 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100365 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
366 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
368 // index) is a pointer, and the variable pointed to is set to kPayloadType.
369 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
370 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700371 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000372 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700373 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000374
375 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200376 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377
378 // Expectations for delay manager.
379 {
380 // All expectations within this block must be called in this specific order.
381 InSequence sequence; // Dummy variable.
382 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000383 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384 .Times(2)
385 .WillRepeatedly(Return(-1));
Jonas Olssona4d87372019-07-05 19:08:33 +0200386 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0)).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000387 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000389 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
390 .WillOnce(Return(0));
391 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392 .WillOnce(Return(0));
393 }
394
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000395 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700396 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397
398 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700399 rtp_header.timestamp += 160;
400 rtp_header.sequenceNumber += 1;
401 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000402}
403
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000404TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
405 UseNoMocks();
406 CreateInstance();
407
408 const int kPayloadLengthSamples = 80;
409 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200410 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000411 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
412 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700413 RTPHeader rtp_header;
414 rtp_header.payloadType = kPayloadType;
415 rtp_header.sequenceNumber = 0x1234;
416 rtp_header.timestamp = 0x12345678;
417 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000418
Niels Möller05543682019-01-10 16:55:06 +0100419 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
420 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000421
422 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700423 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000424 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700425 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
426 rtp_header.timestamp += kPayloadLengthSamples;
427 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000428 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
429 }
430
431 // Insert one more packet and make sure the buffer got flushed. That is, it
432 // should only hold one single packet.
433 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700434 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700435 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700436 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700437 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
438 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000439}
440
solenberg2779bab2016-11-17 04:45:19 -0800441TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100442 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800443}
solenberg99df6c02016-10-11 04:35:34 -0700444
solenberg2779bab2016-11-17 04:45:19 -0800445TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100446 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800447}
solenberg99df6c02016-10-11 04:35:34 -0700448
solenberg2779bab2016-11-17 04:45:19 -0800449TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100450 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800451}
solenberg99df6c02016-10-11 04:35:34 -0700452
solenberg2779bab2016-11-17 04:45:19 -0800453TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100454 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700455}
456
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000457// This test verifies that timestamps propagate from the incoming packets
458// through to the sync buffer and to the playout timestamp.
459TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000460 const uint8_t kPayloadType = 17; // Just an arbitrary number.
461 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
462 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700463 const size_t kPayloadLengthSamples =
464 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000465 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
466 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700467 RTPHeader rtp_header;
468 rtp_header.payloadType = kPayloadType;
469 rtp_header.sequenceNumber = 0x1234;
470 rtp_header.timestamp = 0x12345678;
471 rtp_header.ssrc = 0x87654321;
Chen Xing3e8ef942019-07-01 17:16:32 +0200472 rtp_header.numCSRCs = 3;
473 rtp_header.arrOfCSRCs[0] = 43;
474 rtp_header.arrOfCSRCs[1] = 65;
475 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000476
477 // This is a dummy decoder that produces as many output samples as the input
478 // has bytes. The output is an increasing series, starting at 1 for the first
479 // sample, and then increasing by 1 for each sample.
480 class CountingSamplesDecoder : public AudioDecoder {
481 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000482 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000483
484 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100485 int DecodeInternal(const uint8_t* encoded,
486 size_t encoded_len,
487 int /* sample_rate_hz */,
488 int16_t* decoded,
489 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000490 for (size_t i = 0; i < encoded_len; ++i) {
491 decoded[i] = next_value_++;
492 }
493 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200494 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000495 }
496
Karl Wiberg43766482015-08-27 15:22:11 +0200497 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000498
kwiberg347d3512016-06-16 01:59:09 -0700499 int SampleRateHz() const override { return kSampleRateHz; }
500
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000501 size_t Channels() const override { return 1; }
502
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000503 uint16_t next_value() const { return next_value_; }
504
505 private:
506 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000507 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000508
Niels Möllerb7180c02018-12-06 13:07:11 +0100509 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
510 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
511
512 UseNoMocks();
513 CreateInstance(decoder_factory);
514
515 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100516 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000517
518 // Insert one packet.
Chen Xing3e8ef942019-07-01 17:16:32 +0200519 clock_.AdvanceTimeMilliseconds(123456);
520 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000521 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700522 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000523
524 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700525 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800526 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700527 bool muted;
528 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
529 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800530 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
531 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800532 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000533
Chen Xing3e8ef942019-07-01 17:16:32 +0200534 // Verify |output.packet_infos_|.
535 ASSERT_THAT(output.packet_infos_, SizeIs(1));
536 {
537 const auto& packet_info = output.packet_infos_[0];
538 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
539 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
540 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
541 EXPECT_FALSE(packet_info.audio_level().has_value());
542 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
543 }
544
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000545 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700546 EXPECT_EQ(kPayloadLengthSamples,
547 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000548
549 // The value of the last of the output samples is the same as the number of
550 // samples played from the decoded packet. Thus, this number + the RTP
551 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200552 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700553 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200554 absl::optional<uint32_t>(rtp_header.timestamp +
555 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700556 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000557
558 // Check the timestamp for the last value in the sync buffer. This should
559 // be one full frame length ahead of the RTP timestamp.
560 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
561 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700562 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000563 sync_buffer->end_timestamp());
564
565 // Check that the number of samples still to play from the sync buffer add
566 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800567 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700568 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800569 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000570}
571
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000572TEST_F(NetEqImplTest, ReorderedPacket) {
573 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100574 // Create a mock decoder object.
575 MockAudioDecoder mock_decoder;
576
577 CreateInstance(
578 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000579
580 const uint8_t kPayloadType = 17; // Just an arbitrary number.
581 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
582 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700583 const size_t kPayloadLengthSamples =
584 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000585 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
586 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700587 RTPHeader rtp_header;
588 rtp_header.payloadType = kPayloadType;
589 rtp_header.sequenceNumber = 0x1234;
590 rtp_header.timestamp = 0x12345678;
591 rtp_header.ssrc = 0x87654321;
Chen Xing3e8ef942019-07-01 17:16:32 +0200592 rtp_header.extension.hasAudioLevel = true;
593 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000594
Karl Wiberg43766482015-08-27 15:22:11 +0200595 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700596 EXPECT_CALL(mock_decoder, SampleRateHz())
597 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000598 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000599 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
600 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700601 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200602 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000603 int16_t dummy_output[kPayloadLengthSamples] = {0};
604 // The below expectation will make the mock decoder write
605 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100606 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
607 kSampleRateHz, _, _))
608 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000609 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100610 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200611 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100612 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
613 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000614
615 // Insert one packet.
Chen Xing3e8ef942019-07-01 17:16:32 +0200616 clock_.AdvanceTimeMilliseconds(123456);
617 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000618 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700619 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000620
621 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700622 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800623 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700624 bool muted;
625 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800626 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
627 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800628 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000629
Chen Xing3e8ef942019-07-01 17:16:32 +0200630 // Verify |output.packet_infos_|.
631 ASSERT_THAT(output.packet_infos_, SizeIs(1));
632 {
633 const auto& packet_info = output.packet_infos_[0];
634 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
635 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
636 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
637 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
638 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
639 }
640
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000641 // Insert two more packets. The first one is out of order, and is already too
642 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700643 rtp_header.sequenceNumber -= 1;
644 rtp_header.timestamp -= kPayloadLengthSamples;
Chen Xing3e8ef942019-07-01 17:16:32 +0200645 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000646 payload[0] = 1;
Chen Xing3e8ef942019-07-01 17:16:32 +0200647 clock_.AdvanceTimeMilliseconds(1000);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000648 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700649 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
650 rtp_header.sequenceNumber += 2;
651 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Chen Xing3e8ef942019-07-01 17:16:32 +0200652 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000653 payload[0] = 2;
Chen Xing3e8ef942019-07-01 17:16:32 +0200654 clock_.AdvanceTimeMilliseconds(2000);
655 expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000656 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700657 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000658
659 // Expect only the second packet to be decoded (the one with "2" as the first
660 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100661 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
662 kSampleRateHz, _, _))
663 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000664 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100665 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200666 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000667
668 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700669 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800670 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
671 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800672 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000673
674 // Now check the packet buffer, and make sure it is empty, since the
675 // out-of-order packet should have been discarded.
676 EXPECT_TRUE(packet_buffer_->Empty());
677
Chen Xing3e8ef942019-07-01 17:16:32 +0200678 // Verify |output.packet_infos_|. Expect to only see the second packet.
679 ASSERT_THAT(output.packet_infos_, SizeIs(1));
680 {
681 const auto& packet_info = output.packet_infos_[0];
682 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
683 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
684 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
685 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
686 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
687 }
688
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000689 EXPECT_CALL(mock_decoder, Die());
690}
691
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000692// This test verifies that NetEq can handle the situation where the first
693// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000694TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000695 UseNoMocks();
696 CreateInstance();
697
698 const uint8_t kPayloadType = 17; // Just an arbitrary number.
699 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
700 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700701 const size_t kPayloadLengthSamples =
702 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700703 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000704 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700705 RTPHeader rtp_header;
706 rtp_header.payloadType = kPayloadType;
707 rtp_header.sequenceNumber = 0x1234;
708 rtp_header.timestamp = 0x12345678;
709 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000710
711 // Insert one packet. Note that we have not registered any payload type, so
712 // this packet will be rejected.
713 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700714 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000715
716 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700717 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800718 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700719 bool muted;
720 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800721 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
722 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
723 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800724 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +0200725 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000726
727 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100728 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
729 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000730
731 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700732 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700733 rtp_header.sequenceNumber++;
734 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000735 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700736 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000737 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
738 }
739
740 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700741 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700742 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800743 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
744 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
745 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800746 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000747 << "NetEq did not decode the packets as expected.";
Chen Xing3e8ef942019-07-01 17:16:32 +0200748 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000749 }
750}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000751
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200752// This test verifies that audio interruption is not logged for the initial
753// PLC period before the first packet is deocoded.
754// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
755TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
756 UseNoMocks();
757 CreateInstance();
758
759 const uint8_t kPayloadType = 17; // Just an arbitrary number.
760 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
761 const int kSampleRateHz = 8000;
762 const size_t kPayloadLengthSamples =
763 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
764 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
765 uint8_t payload[kPayloadLengthBytes] = {0};
766 RTPHeader rtp_header;
767 rtp_header.payloadType = kPayloadType;
768 rtp_header.sequenceNumber = 0x1234;
769 rtp_header.timestamp = 0x12345678;
770 rtp_header.ssrc = 0x87654321;
771
772 // Register the payload type.
773 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
774 SdpAudioFormat("l16", 8000, 1)));
775
776 // Pull audio several times. No packets have been inserted yet.
777 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
778 AudioFrame output;
779 bool muted;
780 for (int i = 0; i < 100; ++i) {
781 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
782 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
783 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
784 EXPECT_EQ(1u, output.num_channels_);
785 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +0200786 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200787 }
788
789 // Insert 10 packets.
790 for (size_t i = 0; i < 10; ++i) {
791 rtp_header.sequenceNumber++;
792 rtp_header.timestamp += kPayloadLengthSamples;
793 EXPECT_EQ(NetEq::kOK,
794 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
795 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
796 }
797
798 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
799 for (size_t i = 0; i < 3; ++i) {
800 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
801 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
802 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
803 EXPECT_EQ(1u, output.num_channels_);
804 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
805 << "NetEq did not decode the packets as expected.";
Chen Xing3e8ef942019-07-01 17:16:32 +0200806 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200807 }
808
809 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200810 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200811}
812
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000813// This test verifies that NetEq can handle comfort noise and enters/quits codec
814// internal CNG mode properly.
815TEST_F(NetEqImplTest, CodecInternalCng) {
816 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100817 // Create a mock decoder object.
818 MockAudioDecoder mock_decoder;
819 CreateInstance(
820 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000821
822 const uint8_t kPayloadType = 17; // Just an arbitrary number.
823 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
824 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700825 const size_t kPayloadLengthSamples =
826 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
827 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000828 uint8_t payload[kPayloadLengthBytes] = {0};
829 int16_t dummy_output[kPayloadLengthSamples] = {0};
830
henrik.lundin246ef3e2017-04-24 09:14:32 -0700831 RTPHeader rtp_header;
832 rtp_header.payloadType = kPayloadType;
833 rtp_header.sequenceNumber = 0x1234;
834 rtp_header.timestamp = 0x12345678;
835 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000836
Karl Wiberg43766482015-08-27 15:22:11 +0200837 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700838 EXPECT_CALL(mock_decoder, SampleRateHz())
839 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000840 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000841 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
842 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700843 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200844 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700845 // Packed duration when asking the decoder for more CNG data (without a new
846 // packet).
847 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200848 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000849
850 // Pointee(x) verifies that first byte of the payload equals x, this makes it
851 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100852 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
853 kSampleRateKhz * 1000, _, _))
854 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000855 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100856 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200857 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000858
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100859 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
860 kSampleRateKhz * 1000, _, _))
861 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000862 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100863 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200864 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000865
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100866 EXPECT_CALL(mock_decoder,
867 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
868 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000869 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100870 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200871 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000872
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100873 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
874 kSampleRateKhz * 1000, _, _))
875 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000876 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100877 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200878 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000879
Niels Möller50b66d52018-12-11 14:43:21 +0100880 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
881 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000882
883 // Insert one packet (decoder will return speech).
884 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700885 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000886
887 // Insert second packet (decoder will return CNG).
888 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700889 rtp_header.sequenceNumber++;
890 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000891 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700892 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000893
Peter Kastingdce40cf2015-08-24 14:52:23 -0700894 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800895 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800896 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200897 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
898 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
899 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000900 int expected_timestamp_increment[8] = {
901 -1, // will not be used.
902 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200903 -1,
904 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
905 -1,
906 -1,
907 50 * kSampleRateKhz,
908 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000909
henrik.lundin7a926812016-05-12 13:51:28 -0700910 bool muted;
911 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200912 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700913 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000914
henrik.lundin0d96ab72016-04-06 12:28:26 -0700915 // Lambda for verifying the timestamps.
916 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200917 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700918 if (expected_timestamp_increment[i] == -1) {
919 // Expect to get an empty timestamp value during CNG and PLC.
920 EXPECT_FALSE(ts) << "i = " << i;
921 } else {
922 ASSERT_TRUE(ts) << "i = " << i;
923 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
924 << "i = " << i;
925 last_timestamp = ts;
926 }
927 };
928
Peter Kastingdce40cf2015-08-24 14:52:23 -0700929 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800930 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
931 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800932 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700933 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700934 SCOPED_TRACE("");
935 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000936 }
937
938 // Insert third packet, which leaves a gap from last packet.
939 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700940 rtp_header.sequenceNumber += 2;
941 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000942 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700943 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000944
Peter Kastingdce40cf2015-08-24 14:52:23 -0700945 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800946 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
947 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800948 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700949 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700950 SCOPED_TRACE("");
951 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000952 }
953
954 // Now check the packet buffer, and make sure it is empty.
955 EXPECT_TRUE(packet_buffer_->Empty());
956
957 EXPECT_CALL(mock_decoder, Die());
958}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000959
960TEST_F(NetEqImplTest, UnsupportedDecoder) {
961 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100962 ::testing::NiceMock<MockAudioDecoder> decoder;
963
964 CreateInstance(
965 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700966 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800967 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000968
969 const uint8_t kPayloadType = 17; // Just an arbitrary number.
970 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
971 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000972
Peter Kastingdce40cf2015-08-24 14:52:23 -0700973 const size_t kPayloadLengthSamples =
974 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000975 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700976 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200977 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700978 RTPHeader rtp_header;
979 rtp_header.payloadType = kPayloadType;
980 rtp_header.sequenceNumber = 0x1234;
981 rtp_header.timestamp = 0x12345678;
982 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000983
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000984 const uint8_t kFirstPayloadValue = 1;
985 const uint8_t kSecondPayloadValue = 2;
986
ossu61a208b2016-09-20 01:38:00 -0700987 EXPECT_CALL(decoder,
988 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
989 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200990 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000991
ossu61a208b2016-09-20 01:38:00 -0700992 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000993 .Times(0);
994
ossu61a208b2016-09-20 01:38:00 -0700995 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
996 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000997 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700998 .WillOnce(DoAll(
999 SetArrayArgument<3>(dummy_output,
1000 dummy_output + kPayloadLengthSamples * kChannels),
1001 SetArgPointee<4>(AudioDecoder::kSpeech),
1002 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001003
ossu61a208b2016-09-20 01:38:00 -07001004 EXPECT_CALL(decoder,
1005 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
1006 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001007 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -07001008
Jonas Olssona4d87372019-07-05 19:08:33 +02001009 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -07001010
Jonas Olssona4d87372019-07-05 19:08:33 +02001011 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001012
Niels Möllera1eb9c72018-12-07 15:24:42 +01001013 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1014 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001015
1016 // Insert one packet.
1017 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
1018 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001019 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001020
1021 // Insert another packet.
1022 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001023 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001024 // The second timestamp needs to be at least 30 ms after the first to make
1025 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001026 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001027 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001028 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001029
henrik.lundin6d8e0112016-03-04 10:34:21 -08001030 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001031 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001032 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +02001033 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -07001034 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001035 // Output size and number of channels should be correct.
1036 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
1037 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1038 EXPECT_EQ(kChannels, output.num_channels_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001039 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001040
henrik.lundin6d8e0112016-03-04 10:34:21 -08001041 // Second call to GetAudio will decode the packet that is ok. No errors are
1042 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -07001043 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001044 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1045 EXPECT_EQ(kChannels, output.num_channels_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001046 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -07001047
1048 // Die isn't called through NiceMock (since it's called by the
1049 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1050 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001051}
1052
henrik.lundin116c84e2015-08-27 13:14:48 -07001053// This test inserts packets until the buffer is flushed. After that, it asks
1054// NetEq for the network statistics. The purpose of the test is to make sure
1055// that even though the buffer size increment is negative (which it becomes when
1056// the packet causing a flush is inserted), the packet length stored in the
1057// decision logic remains valid.
1058TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1059 UseNoMocks();
1060 CreateInstance();
1061
1062 const size_t kPayloadLengthSamples = 80;
1063 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1064 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1065 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1066 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001067 RTPHeader rtp_header;
1068 rtp_header.payloadType = kPayloadType;
1069 rtp_header.sequenceNumber = 0x1234;
1070 rtp_header.timestamp = 0x12345678;
1071 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001072
Niels Möller05543682019-01-10 16:55:06 +01001073 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1074 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001075
1076 // Insert packets until the buffer flushes.
1077 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1078 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1079 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001080 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1081 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1082 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001083 }
1084 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1085
1086 // Ask for network statistics. This should not crash.
1087 NetEqNetworkStatistics stats;
1088 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1089}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001090
1091TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1092 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001093 // Create a mock decoder object.
1094 MockAudioDecoder mock_decoder;
1095
1096 CreateInstance(
1097 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001098
1099 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1100 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1101 const int kSampleRateHz = 8000;
1102 const size_t kPayloadLengthSamples =
1103 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1104 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1105 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001106 RTPHeader rtp_header;
1107 rtp_header.payloadType = kPayloadType;
1108 rtp_header.sequenceNumber = 0x1234;
1109 rtp_header.timestamp = 0x12345678;
1110 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001111
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001112 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001113 EXPECT_CALL(mock_decoder, SampleRateHz())
1114 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001115 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1116 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1117 .WillRepeatedly(Return(0));
1118 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001119 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001120 int16_t dummy_output[kPayloadLengthSamples] = {0};
1121 // The below expectation will make the mock decoder write
1122 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1123 // speech. That is, the decoded length is 5 samples shorter than the expected.
1124 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001125 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001126 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001127 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001128 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001129 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001130 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001131 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1132 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001133
1134 // Insert one packet.
1135 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001136 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001137
1138 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1139
1140 // Pull audio once.
1141 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001142 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001143 bool muted;
1144 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001145 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1146 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001147 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001148 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001149
1150 EXPECT_CALL(mock_decoder, Die());
1151}
minyuel6d92bf52015-09-23 15:20:39 +02001152
1153// This test checks the behavior of NetEq when audio decoder fails.
1154TEST_F(NetEqImplTest, DecodingError) {
1155 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001156 // Create a mock decoder object.
1157 MockAudioDecoder mock_decoder;
1158
1159 CreateInstance(
1160 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001161
1162 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1163 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1164 const int kSampleRateHz = 8000;
1165 const int kDecoderErrorCode = -97; // Any negative number.
1166
1167 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1168 const size_t kFrameLengthSamples =
1169 static_cast<size_t>(5 * kSampleRateHz / 1000);
1170
1171 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1172
1173 uint8_t payload[kPayloadLengthBytes] = {0};
1174
henrik.lundin246ef3e2017-04-24 09:14:32 -07001175 RTPHeader rtp_header;
1176 rtp_header.payloadType = kPayloadType;
1177 rtp_header.sequenceNumber = 0x1234;
1178 rtp_header.timestamp = 0x12345678;
1179 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001180
minyuel6d92bf52015-09-23 15:20:39 +02001181 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001182 EXPECT_CALL(mock_decoder, SampleRateHz())
1183 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001184 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1185 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1186 .WillRepeatedly(Return(0));
1187 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001188 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001189 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1190 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001191 int16_t dummy_output[kFrameLengthSamples] = {0};
1192
1193 {
1194 InSequence sequence; // Dummy variable.
1195 // Mock decoder works normally the first time.
1196 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001197 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001198 .Times(3)
1199 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001200 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001201 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001202 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001203 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001204 .RetiresOnSaturation();
1205
1206 // Then mock decoder fails. A common reason for failure can be buffer being
1207 // too short
1208 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001209 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001210 .WillOnce(Return(-1))
1211 .RetiresOnSaturation();
1212
1213 // Mock decoder finally returns to normal.
1214 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001215 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001216 .Times(2)
1217 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001218 DoAll(SetArrayArgument<3>(dummy_output,
1219 dummy_output + kFrameLengthSamples),
1220 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001221 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001222 }
1223
Niels Möllera1eb9c72018-12-07 15:24:42 +01001224 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1225 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001226
1227 // Insert packets.
1228 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001229 rtp_header.sequenceNumber += 1;
1230 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001231 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001232 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001233 }
1234
1235 // Pull audio.
1236 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001237 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001238 bool muted;
1239 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001240 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1241 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001242 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001243 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001244
1245 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001246 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001247 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1248 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001249 // We are not expecting anything for output.speech_type_, since an error was
1250 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001251
1252 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001253 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001254 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1255 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001256 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001257 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001258
1259 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001260 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001261 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1262 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001263 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001264 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001265
1266 EXPECT_CALL(mock_decoder, Die());
1267}
1268
1269// This test checks the behavior of NetEq when audio decoder fails during CNG.
1270TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1271 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001272
1273 // Create a mock decoder object.
1274 MockAudioDecoder mock_decoder;
1275 CreateInstance(
1276 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001277
1278 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1279 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1280 const int kSampleRateHz = 8000;
1281 const int kDecoderErrorCode = -97; // Any negative number.
1282
1283 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1284 const size_t kFrameLengthSamples =
1285 static_cast<size_t>(5 * kSampleRateHz / 1000);
1286
1287 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1288
1289 uint8_t payload[kPayloadLengthBytes] = {0};
1290
henrik.lundin246ef3e2017-04-24 09:14:32 -07001291 RTPHeader rtp_header;
1292 rtp_header.payloadType = kPayloadType;
1293 rtp_header.sequenceNumber = 0x1234;
1294 rtp_header.timestamp = 0x12345678;
1295 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001296
minyuel6d92bf52015-09-23 15:20:39 +02001297 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001298 EXPECT_CALL(mock_decoder, SampleRateHz())
1299 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001300 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1301 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1302 .WillRepeatedly(Return(0));
1303 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001304 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001305 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001306 int16_t dummy_output[kFrameLengthSamples] = {0};
1307
1308 {
1309 InSequence sequence; // Dummy variable.
1310 // Mock decoder works normally the first 2 times.
1311 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001312 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001313 .Times(2)
1314 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001315 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001316 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001317 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001318 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001319 .RetiresOnSaturation();
1320
1321 // Then mock decoder fails. A common reason for failure can be buffer being
1322 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001323 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001324 .WillOnce(Return(-1))
1325 .RetiresOnSaturation();
1326
1327 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001328 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001329 .Times(2)
1330 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001331 DoAll(SetArrayArgument<3>(dummy_output,
1332 dummy_output + kFrameLengthSamples),
1333 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001334 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001335 }
1336
Niels Möller50b66d52018-12-11 14:43:21 +01001337 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1338 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001339
1340 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1341 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001342 rtp_header.sequenceNumber += 1;
1343 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001344 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001345 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001346 }
1347
1348 // Pull audio.
1349 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001350 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001351 bool muted;
1352 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001353 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1354 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001355 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001356
1357 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001358 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001359 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1360 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001361 // We are not expecting anything for output.speech_type_, since an error was
1362 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001363
1364 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001365 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001366 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1367 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001368 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001369
1370 EXPECT_CALL(mock_decoder, Die());
1371}
1372
henrik.lundind89814b2015-11-23 06:49:25 -08001373// Tests that the return value from last_output_sample_rate_hz() is equal to the
1374// configured inital sample rate.
1375TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1376 UseNoMocks();
1377 config_.sample_rate_hz = 48000;
1378 CreateInstance();
1379 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1380}
1381
henrik.lundined497212016-04-25 10:11:38 -07001382TEST_F(NetEqImplTest, TickTimerIncrement) {
1383 UseNoMocks();
1384 CreateInstance();
1385 ASSERT_TRUE(tick_timer_);
1386 EXPECT_EQ(0u, tick_timer_->ticks());
1387 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001388 bool muted;
1389 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001390 EXPECT_EQ(1u, tick_timer_->ticks());
1391}
1392
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001393TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1394 UseNoMocks();
1395 use_mock_delay_manager_ = true;
1396 CreateInstance();
1397
1398 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1399 .WillOnce(Return(true))
1400 .WillOnce(Return(false));
1401
1402 const int delay_ms = 200;
1403
1404 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1405 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1406}
1407
1408TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1409 UseNoMocks();
1410 use_mock_delay_manager_ = true;
1411 CreateInstance();
1412
1413 const int delay_ms = 200;
1414
1415 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1416 .WillOnce(Return(delay_ms));
1417
1418 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1419}
1420
henrik.lundin114c1b32017-04-26 07:47:32 -07001421TEST_F(NetEqImplTest, TargetDelayMs) {
1422 UseNoMocks();
1423 use_mock_delay_manager_ = true;
1424 CreateInstance();
1425 // Let the dummy target delay be 17 packets.
1426 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1427 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1428 .WillOnce(Return(kTargetLevelPacketsQ8));
1429 // Default packet size before any packet has been decoded is 30 ms, so we are
1430 // expecting 17 * 30 = 510 ms target delay.
1431 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1432}
1433
henrik.lundinb8c55b12017-05-10 07:38:01 -07001434TEST_F(NetEqImplTest, InsertEmptyPacket) {
1435 UseNoMocks();
1436 use_mock_delay_manager_ = true;
1437 CreateInstance();
1438
1439 RTPHeader rtp_header;
1440 rtp_header.payloadType = 17;
1441 rtp_header.sequenceNumber = 0x1234;
1442 rtp_header.timestamp = 0x12345678;
1443 rtp_header.ssrc = 0x87654321;
1444
1445 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1446 neteq_->InsertEmptyPacket(rtp_header);
1447}
1448
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001449TEST_F(NetEqImplTest, EnableRtxHandling) {
1450 UseNoMocks();
1451 use_mock_delay_manager_ = true;
1452 config_.enable_rtx_handling = true;
1453 CreateInstance();
1454 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1455 .Times(1)
1456 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1457
1458 const int kPayloadLengthSamples = 80;
1459 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1460 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1461 const uint32_t kReceiveTime = 17;
1462 uint8_t payload[kPayloadLengthBytes] = {0};
1463 RTPHeader rtp_header;
1464 rtp_header.payloadType = kPayloadType;
1465 rtp_header.sequenceNumber = 0x1234;
1466 rtp_header.timestamp = 0x12345678;
1467 rtp_header.ssrc = 0x87654321;
1468
Niels Möller05543682019-01-10 16:55:06 +01001469 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1470 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001471 EXPECT_EQ(NetEq::kOK,
1472 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1473 AudioFrame output;
1474 bool muted;
1475 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1476
1477 // Insert second packet that was sent before the first packet.
1478 rtp_header.sequenceNumber -= 1;
1479 rtp_header.timestamp -= kPayloadLengthSamples;
1480 EXPECT_CALL(*mock_delay_manager_,
1481 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1482 EXPECT_EQ(NetEq::kOK,
1483 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1484}
1485
minyue5bd33972016-05-02 04:46:11 -07001486class Decoder120ms : public AudioDecoder {
1487 public:
kwiberg347d3512016-06-16 01:59:09 -07001488 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1489 : sample_rate_hz_(sample_rate_hz),
1490 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001491 speech_type_(speech_type) {}
1492
1493 int DecodeInternal(const uint8_t* encoded,
1494 size_t encoded_len,
1495 int sample_rate_hz,
1496 int16_t* decoded,
1497 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001498 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001499 size_t decoded_len =
1500 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1501 for (size_t i = 0; i < decoded_len; ++i) {
1502 decoded[i] = next_value_++;
1503 }
1504 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001505 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001506 }
1507
1508 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001509 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001510 size_t Channels() const override { return 2; }
1511
1512 private:
kwiberg347d3512016-06-16 01:59:09 -07001513 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001514 int16_t next_value_;
1515 SpeechType speech_type_;
1516};
1517
1518class NetEqImplTest120ms : public NetEqImplTest {
1519 protected:
1520 NetEqImplTest120ms() : NetEqImplTest() {}
1521 virtual ~NetEqImplTest120ms() {}
1522
1523 void CreateInstanceNoMocks() {
1524 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001525 CreateInstance(decoder_factory_);
1526 EXPECT_TRUE(neteq_->RegisterPayloadType(
1527 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001528 }
1529
1530 void CreateInstanceWithDelayManagerMock() {
1531 UseNoMocks();
1532 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001533 CreateInstance(decoder_factory_);
1534 EXPECT_TRUE(neteq_->RegisterPayloadType(
1535 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001536 }
1537
1538 uint32_t timestamp_diff_between_packets() const {
1539 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1540 }
1541
1542 uint32_t first_timestamp() const { return 10u; }
1543
1544 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001545 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001546 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001547 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1548 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001549 }
1550 }
1551
1552 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001553 RTPHeader rtp_header;
1554 rtp_header.payloadType = kPayloadType;
1555 rtp_header.sequenceNumber = sequence_number_;
1556 rtp_header.timestamp = timestamp;
1557 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001558 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1559 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001560 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001561 sequence_number_++;
1562 }
1563
1564 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001565 const uint32_t sampling_freq = kSamplingFreq_;
1566 decoder_factory_ =
1567 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1568 [sampling_freq, speech_type]() {
1569 std::unique_ptr<AudioDecoder> decoder =
1570 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1571 RTC_CHECK_EQ(2, decoder->Channels());
1572 return decoder;
1573 });
minyue5bd33972016-05-02 04:46:11 -07001574 }
1575
Niels Möllera0f44302018-11-30 10:45:12 +01001576 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001577 AudioFrame output_;
1578 const uint32_t kPayloadType = 17;
1579 const uint32_t kSamplingFreq_ = 48000;
1580 uint16_t sequence_number_ = 1;
1581};
1582
minyue5bd33972016-05-02 04:46:11 -07001583TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001584 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001585 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001586
1587 InsertPacket(first_timestamp());
1588 GetFirstPacket();
1589
henrik.lundin7a926812016-05-12 13:51:28 -07001590 bool muted;
1591 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001592 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1593}
1594
1595TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001596 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001597 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001598
1599 InsertPacket(first_timestamp());
1600 GetFirstPacket();
1601
1602 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1603}
1604
1605TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001606 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001607 CreateInstanceWithDelayManagerMock();
1608
minyue5bd33972016-05-02 04:46:11 -07001609 InsertPacket(first_timestamp());
1610
1611 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001612 bool muted;
1613 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001614
1615 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1616
1617 // Delay manager reports a target level which should cause a Merge.
1618 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1619
henrik.lundin7a926812016-05-12 13:51:28 -07001620 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001621 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1622}
1623
1624TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001625 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001626 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001627
1628 InsertPacket(first_timestamp());
1629 GetFirstPacket();
1630
henrik.lundin7a926812016-05-12 13:51:28 -07001631 bool muted;
1632 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001633 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1634}
1635
1636TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001637 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001638 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001639
1640 InsertPacket(first_timestamp());
1641 GetFirstPacket();
1642 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1643
1644 // Delay manager report buffer limit which should cause a FastAccelerate.
1645 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1646 .Times(1)
1647 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1648
henrik.lundin7a926812016-05-12 13:51:28 -07001649 bool muted;
1650 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001651 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1652}
1653
1654TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001655 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001656 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001657
1658 InsertPacket(first_timestamp());
1659 GetFirstPacket();
1660
1661 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1662
1663 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1664 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1665 .Times(1)
1666 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1667
henrik.lundin7a926812016-05-12 13:51:28 -07001668 bool muted;
1669 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001670 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1671}
1672
1673TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001674 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001675 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001676
1677 InsertPacket(first_timestamp());
1678 GetFirstPacket();
1679
1680 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1681
1682 // Delay manager report buffer limit which should cause a Accelerate.
1683 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1684 .Times(1)
1685 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1686
henrik.lundin7a926812016-05-12 13:51:28 -07001687 bool muted;
1688 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001689 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1690}
1691
Chen Xing3e8ef942019-07-01 17:16:32 +02001692} // namespace webrtc