blob: 0c7c09059cdc8a7c686b757bf92d80066f322c37 [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
kwiberg84be5112016-04-27 01:19:58 -070011#include <memory>
Chen Xing3e8ef942019-07-01 17:16:32 +020012#include <utility>
13#include <vector>
kwiberg84be5112016-04-27 01:19:58 -070014
Niels Möllera0f44302018-11-30 10:45:12 +010015#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/accelerate.h"
18#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010019#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/neteq/include/neteq.h"
21#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
22#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
23#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
24#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
25#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
26#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
27#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
28#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
29#include "modules/audio_coding/neteq/neteq_impl.h"
30#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010031#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_coding/neteq/sync_buffer.h"
33#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010034#include "rtc_base/numerics/safe_conversions.h"
Chen Xing3e8ef942019-07-01 17:16:32 +020035#include "system_wrappers/include/clock.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010036#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010037#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "test/gmock.h"
39#include "test/gtest.h"
40#include "test/mock_audio_decoder.h"
41#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010044using ::testing::AtLeast;
45using ::testing::DoAll;
Chen Xing3e8ef942019-07-01 17:16:32 +020046using ::testing::ElementsAre;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047using ::testing::InSequence;
48using ::testing::Invoke;
Chen Xing3e8ef942019-07-01 17:16:32 +020049using ::testing::IsEmpty;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000050using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010051using ::testing::Pointee;
52using ::testing::Return;
53using ::testing::ReturnNull;
54using ::testing::SetArgPointee;
55using ::testing::SetArrayArgument;
Chen Xing3e8ef942019-07-01 17:16:32 +020056using ::testing::SizeIs;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010057using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058
59namespace webrtc {
60
61// This function is called when inserting a packet list into the mock packet
62// buffer. The purpose is to delete all inserted packets properly, to avoid
63// memory leaks in the test.
64int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070065 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066 return PacketBuffer::kOK;
67}
68
69class NetEqImplTest : public ::testing::Test {
70 protected:
Chen Xing3e8ef942019-07-01 17:16:32 +020071 NetEqImplTest() : clock_(0) { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000072
Niels Möllera0f44302018-11-30 10:45:12 +010073 void CreateInstance(
74 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
75 ASSERT_TRUE(decoder_factory);
Chen Xing3e8ef942019-07-01 17:16:32 +020076 NetEqImpl::Dependencies deps(config_, &clock_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070077
78 // Get a local pointer to NetEq's TickTimer object.
79 tick_timer_ = deps.tick_timer.get();
80
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000081 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070082 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
83 mock_buffer_level_filter_ = mock.get();
84 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000085 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070086 buffer_level_filter_ = deps.buffer_level_filter.get();
87
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000088 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070089 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
90 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000091 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
92 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070093 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000094 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070095 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000096
henrik.lundin1d9061e2016-04-26 12:19:34 -070097 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070098 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010099 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700100 mock_delay_peak_detector_ = mock.get();
101 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
102 deps.delay_peak_detector = std::move(mock);
103 }
104 delay_peak_detector_ = deps.delay_peak_detector.get();
105
106 if (use_mock_delay_manager_) {
107 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100108 config_.max_packets_in_buffer, config_.min_delay_ms, 1020054733,
109 DelayManager::HistogramMode::INTER_ARRIVAL_TIME,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100110 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
Jakob Ivarsson44507082019-03-05 16:59:03 +0100111 deps.stats.get(), absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700112 mock_delay_manager_ = mock.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700113 deps.delay_manager = std::move(mock);
114 }
115 delay_manager_ = deps.delay_manager.get();
116
117 if (use_mock_dtmf_buffer_) {
118 std::unique_ptr<MockDtmfBuffer> mock(
119 new MockDtmfBuffer(config_.sample_rate_hz));
120 mock_dtmf_buffer_ = mock.get();
121 deps.dtmf_buffer = std::move(mock);
122 }
123 dtmf_buffer_ = deps.dtmf_buffer.get();
124
125 if (use_mock_dtmf_tone_generator_) {
126 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
127 mock_dtmf_tone_generator_ = mock.get();
128 deps.dtmf_tone_generator = std::move(mock);
129 }
130 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
131
132 if (use_mock_packet_buffer_) {
133 std::unique_ptr<MockPacketBuffer> mock(
134 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
135 mock_packet_buffer_ = mock.get();
136 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700137 }
138 packet_buffer_ = deps.packet_buffer.get();
139
140 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700141 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700142 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700143 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700144 }
ossua70695a2016-09-22 02:06:28 -0700145 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700146
147 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
148 new TimestampScaler(*deps.decoder_database.get()));
149
150 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000151 ASSERT_TRUE(neteq_ != NULL);
152 }
153
Niels Möllera0f44302018-11-30 10:45:12 +0100154 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
155
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000156 void UseNoMocks() {
157 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
158 use_mock_buffer_level_filter_ = false;
159 use_mock_decoder_database_ = false;
160 use_mock_delay_peak_detector_ = false;
161 use_mock_delay_manager_ = false;
162 use_mock_dtmf_buffer_ = false;
163 use_mock_dtmf_tone_generator_ = false;
164 use_mock_packet_buffer_ = false;
165 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000166 }
167
168 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000169 if (use_mock_buffer_level_filter_) {
170 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
171 }
172 if (use_mock_decoder_database_) {
173 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
174 }
175 if (use_mock_delay_manager_) {
176 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
177 }
178 if (use_mock_delay_peak_detector_) {
179 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
180 }
181 if (use_mock_dtmf_buffer_) {
182 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
183 }
184 if (use_mock_dtmf_tone_generator_) {
185 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
186 }
187 if (use_mock_packet_buffer_) {
188 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
189 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190 }
191
Niels Möller05543682019-01-10 16:55:06 +0100192 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800193 const size_t kPayloadLength = 4;
194 const uint8_t kPayloadType = 110;
195 const uint32_t kReceiveTime = 17;
196 const int kSampleRateHz = 16000;
197 config_.sample_rate_hz = kSampleRateHz;
198 UseNoMocks();
199 CreateInstance();
200 // Event: 2, E bit, Volume: 17, Length: 4336.
201 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700202 RTPHeader rtp_header;
203 rtp_header.payloadType = kPayloadType;
204 rtp_header.sequenceNumber = 0x1234;
205 rtp_header.timestamp = 0x12345678;
206 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800207
Niels Möller05543682019-01-10 16:55:06 +0100208 EXPECT_TRUE(neteq_->RegisterPayloadType(
209 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800210
211 // Insert first packet.
212 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700213 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800214
215 // Pull audio once.
216 const size_t kMaxOutputSize =
217 static_cast<size_t>(10 * kSampleRateHz / 1000);
218 AudioFrame output;
219 bool muted;
220 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
221 ASSERT_FALSE(muted);
222 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
223 EXPECT_EQ(1u, output.num_channels_);
224 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
225
Chen Xing3e8ef942019-07-01 17:16:32 +0200226 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
227 // returned by |GetAudio()|.
228 EXPECT_THAT(output.packet_infos_, IsEmpty());
229
solenberg2779bab2016-11-17 04:45:19 -0800230 // Verify first 64 samples of actual output.
231 const std::vector<int16_t> kOutput({
232 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
233 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
234 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
235 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
236 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
237 -2534, -1163 });
238 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700239 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800240 }
241
henrik.lundin1d9061e2016-04-26 12:19:34 -0700242 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000243 NetEq::Config config_;
Chen Xing3e8ef942019-07-01 17:16:32 +0200244 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700245 TickTimer* tick_timer_ = nullptr;
246 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
247 BufferLevelFilter* buffer_level_filter_ = nullptr;
248 bool use_mock_buffer_level_filter_ = true;
249 MockDecoderDatabase* mock_decoder_database_ = nullptr;
250 DecoderDatabase* decoder_database_ = nullptr;
251 bool use_mock_decoder_database_ = true;
252 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
253 DelayPeakDetector* delay_peak_detector_ = nullptr;
254 bool use_mock_delay_peak_detector_ = true;
255 MockDelayManager* mock_delay_manager_ = nullptr;
256 DelayManager* delay_manager_ = nullptr;
257 bool use_mock_delay_manager_ = true;
258 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
259 DtmfBuffer* dtmf_buffer_ = nullptr;
260 bool use_mock_dtmf_buffer_ = true;
261 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
262 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
263 bool use_mock_dtmf_tone_generator_ = true;
264 MockPacketBuffer* mock_packet_buffer_ = nullptr;
265 PacketBuffer* packet_buffer_ = nullptr;
266 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700267 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
268 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700269 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270};
271
272
273// This tests the interface class NetEq.
274// TODO(hlundin): Move to separate file?
275TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000276 NetEq::Config config;
Chen Xing3e8ef942019-07-01 17:16:32 +0200277 SimulatedClock clock(0);
278 NetEq* neteq =
279 NetEq::Create(config, &clock, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 delete neteq;
281}
282
kwiberg5adaf732016-10-04 09:33:27 -0700283TEST_F(NetEqImplTest, RegisterPayloadType) {
284 CreateInstance();
285 constexpr int rtp_payload_type = 0;
286 const SdpAudioFormat format("pcmu", 8000, 1);
287 EXPECT_CALL(*mock_decoder_database_,
288 RegisterPayload(rtp_payload_type, format));
289 neteq_->RegisterPayloadType(rtp_payload_type, format);
290}
291
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000292TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000293 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000295 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200297 // Check that kOK is returned when database returns kDecoderNotFound, because
298 // removing a payload type that was never registered is not an error.
299 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300}
301
kwiberg6b19b562016-09-20 04:02:25 -0700302TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
303 CreateInstance();
304 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
305 neteq_->RemoveAllPayloadTypes();
306}
307
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000309 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000310 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311 const uint8_t kPayloadType = 0;
312 const uint16_t kFirstSequenceNumber = 0x1234;
313 const uint32_t kFirstTimestamp = 0x12345678;
314 const uint32_t kSsrc = 0x87654321;
315 const uint32_t kFirstReceiveTime = 17;
316 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700317 RTPHeader rtp_header;
318 rtp_header.payloadType = kPayloadType;
319 rtp_header.sequenceNumber = kFirstSequenceNumber;
320 rtp_header.timestamp = kFirstTimestamp;
321 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700322 Packet fake_packet;
323 fake_packet.payload_type = kPayloadType;
324 fake_packet.sequence_number = kFirstSequenceNumber;
325 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000326
kwibergc0f2dcf2016-05-31 06:28:03 -0700327 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
328 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100329 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800330 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200331 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800332 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700333 EXPECT_EQ("pcmu", format.name);
334
335 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
336 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
337 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
338 // BWE update function called with first packet.
339 EXPECT_CALL(*mock_decoder,
340 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
341 kFirstTimestamp, kFirstReceiveTime));
342 // BWE update function called with second packet.
343 EXPECT_CALL(
344 *mock_decoder,
345 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
346 kFirstTimestamp + 160, kFirstReceiveTime + 155));
347 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
348
349 *dec = std::move(mock_decoder);
350 }));
Niels Möller72899062019-01-11 09:36:13 +0100351 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
352 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353
354 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000355 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000356 .WillRepeatedly(Return(&info));
357
358 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000359 EXPECT_CALL(*mock_packet_buffer_, Empty())
360 .WillOnce(Return(false)); // Called once after first packet is inserted.
361 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362 .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.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000376 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 .Times(1);
378
379 // Expectations for delay manager.
380 {
381 // All expectations within this block must be called in this specific order.
382 InSequence sequence; // Dummy variable.
383 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000384 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385 .Times(2)
386 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000387 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000389 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000391 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
392 .WillOnce(Return(0));
393 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 .WillOnce(Return(0));
395 }
396
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700398 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000399
400 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700401 rtp_header.timestamp += 160;
402 rtp_header.sequenceNumber += 1;
403 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000404}
405
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000406TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
407 UseNoMocks();
408 CreateInstance();
409
410 const int kPayloadLengthSamples = 80;
411 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
412 const uint8_t kPayloadType = 17; // Just an arbitrary number.
413 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
414 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700415 RTPHeader rtp_header;
416 rtp_header.payloadType = kPayloadType;
417 rtp_header.sequenceNumber = 0x1234;
418 rtp_header.timestamp = 0x12345678;
419 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000420
Niels Möller05543682019-01-10 16:55:06 +0100421 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
422 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000423
424 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700425 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000426 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700427 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
428 rtp_header.timestamp += kPayloadLengthSamples;
429 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000430 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
431 }
432
433 // Insert one more packet and make sure the buffer got flushed. That is, it
434 // should only hold one single packet.
435 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700436 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700437 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700438 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700439 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
440 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000441}
442
solenberg2779bab2016-11-17 04:45:19 -0800443TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100444 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800445}
solenberg99df6c02016-10-11 04:35:34 -0700446
solenberg2779bab2016-11-17 04:45:19 -0800447TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100448 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800449}
solenberg99df6c02016-10-11 04:35:34 -0700450
solenberg2779bab2016-11-17 04:45:19 -0800451TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100452 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800453}
solenberg99df6c02016-10-11 04:35:34 -0700454
solenberg2779bab2016-11-17 04:45:19 -0800455TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100456 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700457}
458
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000459// This test verifies that timestamps propagate from the incoming packets
460// through to the sync buffer and to the playout timestamp.
461TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000462 const uint8_t kPayloadType = 17; // Just an arbitrary number.
463 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
464 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700465 const size_t kPayloadLengthSamples =
466 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000467 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
468 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700469 RTPHeader rtp_header;
470 rtp_header.payloadType = kPayloadType;
471 rtp_header.sequenceNumber = 0x1234;
472 rtp_header.timestamp = 0x12345678;
473 rtp_header.ssrc = 0x87654321;
Chen Xing3e8ef942019-07-01 17:16:32 +0200474 rtp_header.numCSRCs = 3;
475 rtp_header.arrOfCSRCs[0] = 43;
476 rtp_header.arrOfCSRCs[1] = 65;
477 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000478
479 // This is a dummy decoder that produces as many output samples as the input
480 // has bytes. The output is an increasing series, starting at 1 for the first
481 // sample, and then increasing by 1 for each sample.
482 class CountingSamplesDecoder : public AudioDecoder {
483 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000484 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000485
486 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100487 int DecodeInternal(const uint8_t* encoded,
488 size_t encoded_len,
489 int /* sample_rate_hz */,
490 int16_t* decoded,
491 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000492 for (size_t i = 0; i < encoded_len; ++i) {
493 decoded[i] = next_value_++;
494 }
495 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200496 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000497 }
498
Karl Wiberg43766482015-08-27 15:22:11 +0200499 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000500
kwiberg347d3512016-06-16 01:59:09 -0700501 int SampleRateHz() const override { return kSampleRateHz; }
502
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000503 size_t Channels() const override { return 1; }
504
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000505 uint16_t next_value() const { return next_value_; }
506
507 private:
508 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000509 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000510
Niels Möllerb7180c02018-12-06 13:07:11 +0100511 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
512 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
513
514 UseNoMocks();
515 CreateInstance(decoder_factory);
516
517 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100518 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000519
520 // Insert one packet.
Chen Xing3e8ef942019-07-01 17:16:32 +0200521 clock_.AdvanceTimeMilliseconds(123456);
522 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000523 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700524 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000525
526 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700527 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800528 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700529 bool muted;
530 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
531 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800532 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
533 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800534 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000535
Chen Xing3e8ef942019-07-01 17:16:32 +0200536 // Verify |output.packet_infos_|.
537 ASSERT_THAT(output.packet_infos_, SizeIs(1));
538 {
539 const auto& packet_info = output.packet_infos_[0];
540 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
541 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
542 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
543 EXPECT_FALSE(packet_info.audio_level().has_value());
544 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
545 }
546
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000547 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700548 EXPECT_EQ(kPayloadLengthSamples,
549 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000550
551 // The value of the last of the output samples is the same as the number of
552 // samples played from the decoded packet. Thus, this number + the RTP
553 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200554 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700555 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200556 absl::optional<uint32_t>(rtp_header.timestamp +
557 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700558 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000559
560 // Check the timestamp for the last value in the sync buffer. This should
561 // be one full frame length ahead of the RTP timestamp.
562 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
563 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700564 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000565 sync_buffer->end_timestamp());
566
567 // Check that the number of samples still to play from the sync buffer add
568 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800569 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700570 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800571 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000572}
573
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000574TEST_F(NetEqImplTest, ReorderedPacket) {
575 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100576 // Create a mock decoder object.
577 MockAudioDecoder mock_decoder;
578
579 CreateInstance(
580 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000581
582 const uint8_t kPayloadType = 17; // Just an arbitrary number.
583 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
584 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700585 const size_t kPayloadLengthSamples =
586 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000587 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
588 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700589 RTPHeader rtp_header;
590 rtp_header.payloadType = kPayloadType;
591 rtp_header.sequenceNumber = 0x1234;
592 rtp_header.timestamp = 0x12345678;
593 rtp_header.ssrc = 0x87654321;
Chen Xing3e8ef942019-07-01 17:16:32 +0200594 rtp_header.extension.hasAudioLevel = true;
595 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000596
Karl Wiberg43766482015-08-27 15:22:11 +0200597 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700598 EXPECT_CALL(mock_decoder, SampleRateHz())
599 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000600 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000601 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
602 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700603 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200604 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000605 int16_t dummy_output[kPayloadLengthSamples] = {0};
606 // The below expectation will make the mock decoder write
607 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100608 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
609 kSampleRateHz, _, _))
610 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000611 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100612 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200613 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100614 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
615 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000616
617 // Insert one packet.
Chen Xing3e8ef942019-07-01 17:16:32 +0200618 clock_.AdvanceTimeMilliseconds(123456);
619 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000620 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700621 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000622
623 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700624 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800625 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700626 bool muted;
627 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800628 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
629 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800630 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000631
Chen Xing3e8ef942019-07-01 17:16:32 +0200632 // Verify |output.packet_infos_|.
633 ASSERT_THAT(output.packet_infos_, SizeIs(1));
634 {
635 const auto& packet_info = output.packet_infos_[0];
636 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
637 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
638 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
639 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
640 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
641 }
642
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000643 // Insert two more packets. The first one is out of order, and is already too
644 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700645 rtp_header.sequenceNumber -= 1;
646 rtp_header.timestamp -= kPayloadLengthSamples;
Chen Xing3e8ef942019-07-01 17:16:32 +0200647 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000648 payload[0] = 1;
Chen Xing3e8ef942019-07-01 17:16:32 +0200649 clock_.AdvanceTimeMilliseconds(1000);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000650 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700651 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
652 rtp_header.sequenceNumber += 2;
653 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Chen Xing3e8ef942019-07-01 17:16:32 +0200654 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000655 payload[0] = 2;
Chen Xing3e8ef942019-07-01 17:16:32 +0200656 clock_.AdvanceTimeMilliseconds(2000);
657 expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000658 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700659 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000660
661 // Expect only the second packet to be decoded (the one with "2" as the first
662 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100663 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
664 kSampleRateHz, _, _))
665 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000666 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100667 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200668 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000669
670 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700671 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800672 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
673 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800674 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000675
676 // Now check the packet buffer, and make sure it is empty, since the
677 // out-of-order packet should have been discarded.
678 EXPECT_TRUE(packet_buffer_->Empty());
679
Chen Xing3e8ef942019-07-01 17:16:32 +0200680 // Verify |output.packet_infos_|. Expect to only see the second packet.
681 ASSERT_THAT(output.packet_infos_, SizeIs(1));
682 {
683 const auto& packet_info = output.packet_infos_[0];
684 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
685 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
686 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
687 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
688 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
689 }
690
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000691 EXPECT_CALL(mock_decoder, Die());
692}
693
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000694// This test verifies that NetEq can handle the situation where the first
695// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000696TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000697 UseNoMocks();
698 CreateInstance();
699
700 const uint8_t kPayloadType = 17; // Just an arbitrary number.
701 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
702 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700703 const size_t kPayloadLengthSamples =
704 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700705 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000706 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700707 RTPHeader rtp_header;
708 rtp_header.payloadType = kPayloadType;
709 rtp_header.sequenceNumber = 0x1234;
710 rtp_header.timestamp = 0x12345678;
711 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000712
713 // Insert one packet. Note that we have not registered any payload type, so
714 // this packet will be rejected.
715 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700716 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000717
718 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700719 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800720 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700721 bool muted;
722 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800723 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
724 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
725 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800726 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +0200727 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000728
729 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100730 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
731 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000732
733 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700734 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700735 rtp_header.sequenceNumber++;
736 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000737 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700738 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000739 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
740 }
741
742 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700743 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700744 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800745 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
746 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
747 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800748 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000749 << "NetEq did not decode the packets as expected.";
Chen Xing3e8ef942019-07-01 17:16:32 +0200750 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000751 }
752}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000753
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200754// This test verifies that audio interruption is not logged for the initial
755// PLC period before the first packet is deocoded.
756// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
757TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
758 UseNoMocks();
759 CreateInstance();
760
761 const uint8_t kPayloadType = 17; // Just an arbitrary number.
762 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
763 const int kSampleRateHz = 8000;
764 const size_t kPayloadLengthSamples =
765 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
766 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
767 uint8_t payload[kPayloadLengthBytes] = {0};
768 RTPHeader rtp_header;
769 rtp_header.payloadType = kPayloadType;
770 rtp_header.sequenceNumber = 0x1234;
771 rtp_header.timestamp = 0x12345678;
772 rtp_header.ssrc = 0x87654321;
773
774 // Register the payload type.
775 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
776 SdpAudioFormat("l16", 8000, 1)));
777
778 // Pull audio several times. No packets have been inserted yet.
779 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
780 AudioFrame output;
781 bool muted;
782 for (int i = 0; i < 100; ++i) {
783 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
784 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
785 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
786 EXPECT_EQ(1u, output.num_channels_);
787 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +0200788 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200789 }
790
791 // Insert 10 packets.
792 for (size_t i = 0; i < 10; ++i) {
793 rtp_header.sequenceNumber++;
794 rtp_header.timestamp += kPayloadLengthSamples;
795 EXPECT_EQ(NetEq::kOK,
796 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
797 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
798 }
799
800 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
801 for (size_t i = 0; i < 3; ++i) {
802 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
803 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
804 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
805 EXPECT_EQ(1u, output.num_channels_);
806 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
807 << "NetEq did not decode the packets as expected.";
Chen Xing3e8ef942019-07-01 17:16:32 +0200808 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200809 }
810
811 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200812 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200813}
814
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000815// This test verifies that NetEq can handle comfort noise and enters/quits codec
816// internal CNG mode properly.
817TEST_F(NetEqImplTest, CodecInternalCng) {
818 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100819 // Create a mock decoder object.
820 MockAudioDecoder mock_decoder;
821 CreateInstance(
822 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000823
824 const uint8_t kPayloadType = 17; // Just an arbitrary number.
825 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
826 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700827 const size_t kPayloadLengthSamples =
828 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
829 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000830 uint8_t payload[kPayloadLengthBytes] = {0};
831 int16_t dummy_output[kPayloadLengthSamples] = {0};
832
henrik.lundin246ef3e2017-04-24 09:14:32 -0700833 RTPHeader rtp_header;
834 rtp_header.payloadType = kPayloadType;
835 rtp_header.sequenceNumber = 0x1234;
836 rtp_header.timestamp = 0x12345678;
837 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000838
Karl Wiberg43766482015-08-27 15:22:11 +0200839 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700840 EXPECT_CALL(mock_decoder, SampleRateHz())
841 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000842 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000843 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
844 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700845 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200846 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700847 // Packed duration when asking the decoder for more CNG data (without a new
848 // packet).
849 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200850 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000851
852 // Pointee(x) verifies that first byte of the payload equals x, this makes it
853 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100854 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
855 kSampleRateKhz * 1000, _, _))
856 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000857 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100858 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200859 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000860
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100861 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
862 kSampleRateKhz * 1000, _, _))
863 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000864 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100865 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200866 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000867
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100868 EXPECT_CALL(mock_decoder,
869 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
870 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000871 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100872 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200873 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000874
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100875 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
876 kSampleRateKhz * 1000, _, _))
877 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000878 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100879 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200880 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000881
Niels Möller50b66d52018-12-11 14:43:21 +0100882 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
883 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000884
885 // Insert one packet (decoder will return speech).
886 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700887 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000888
889 // Insert second packet (decoder will return CNG).
890 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700891 rtp_header.sequenceNumber++;
892 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000893 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700894 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000895
Peter Kastingdce40cf2015-08-24 14:52:23 -0700896 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800897 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800898 AudioFrame::SpeechType expected_type[8] = {
899 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
900 AudioFrame::kCNG, AudioFrame::kCNG,
901 AudioFrame::kCNG, AudioFrame::kCNG,
902 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000903 };
904 int expected_timestamp_increment[8] = {
905 -1, // will not be used.
906 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700907 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
908 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000909 50 * kSampleRateKhz, 10 * kSampleRateKhz
910 };
911
henrik.lundin7a926812016-05-12 13:51:28 -0700912 bool muted;
913 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200914 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700915 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000916
henrik.lundin0d96ab72016-04-06 12:28:26 -0700917 // Lambda for verifying the timestamps.
918 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200919 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700920 if (expected_timestamp_increment[i] == -1) {
921 // Expect to get an empty timestamp value during CNG and PLC.
922 EXPECT_FALSE(ts) << "i = " << i;
923 } else {
924 ASSERT_TRUE(ts) << "i = " << i;
925 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
926 << "i = " << i;
927 last_timestamp = ts;
928 }
929 };
930
Peter Kastingdce40cf2015-08-24 14:52:23 -0700931 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800932 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
933 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800934 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700935 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700936 SCOPED_TRACE("");
937 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000938 }
939
940 // Insert third packet, which leaves a gap from last packet.
941 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700942 rtp_header.sequenceNumber += 2;
943 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000944 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700945 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000946
Peter Kastingdce40cf2015-08-24 14:52:23 -0700947 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800948 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
949 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800950 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700951 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700952 SCOPED_TRACE("");
953 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000954 }
955
956 // Now check the packet buffer, and make sure it is empty.
957 EXPECT_TRUE(packet_buffer_->Empty());
958
959 EXPECT_CALL(mock_decoder, Die());
960}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000961
962TEST_F(NetEqImplTest, UnsupportedDecoder) {
963 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100964 ::testing::NiceMock<MockAudioDecoder> decoder;
965
966 CreateInstance(
967 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700968 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800969 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000970
971 const uint8_t kPayloadType = 17; // Just an arbitrary number.
972 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
973 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000974
Peter Kastingdce40cf2015-08-24 14:52:23 -0700975 const size_t kPayloadLengthSamples =
976 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000977 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700978 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200979 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700980 RTPHeader rtp_header;
981 rtp_header.payloadType = kPayloadType;
982 rtp_header.sequenceNumber = 0x1234;
983 rtp_header.timestamp = 0x12345678;
984 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000985
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000986 const uint8_t kFirstPayloadValue = 1;
987 const uint8_t kSecondPayloadValue = 2;
988
ossu61a208b2016-09-20 01:38:00 -0700989 EXPECT_CALL(decoder,
990 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
991 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200992 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000993
ossu61a208b2016-09-20 01:38:00 -0700994 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000995 .Times(0);
996
ossu61a208b2016-09-20 01:38:00 -0700997 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
998 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000999 .Times(1)
ossu61a208b2016-09-20 01:38:00 -07001000 .WillOnce(DoAll(
1001 SetArrayArgument<3>(dummy_output,
1002 dummy_output + kPayloadLengthSamples * kChannels),
1003 SetArgPointee<4>(AudioDecoder::kSpeech),
1004 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001005
ossu61a208b2016-09-20 01:38:00 -07001006 EXPECT_CALL(decoder,
1007 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
1008 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001009 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -07001010
1011 EXPECT_CALL(decoder, SampleRateHz())
1012 .WillRepeatedly(Return(kSampleRateHz));
1013
1014 EXPECT_CALL(decoder, Channels())
1015 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001016
Niels Möllera1eb9c72018-12-07 15:24:42 +01001017 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1018 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001019
1020 // Insert one packet.
1021 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
1022 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001023 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001024
1025 // Insert another packet.
1026 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001027 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001028 // The second timestamp needs to be at least 30 ms after the first to make
1029 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001030 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001031 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001032 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001033
henrik.lundin6d8e0112016-03-04 10:34:21 -08001034 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001035 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001036 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +02001037 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -07001038 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001039 // Output size and number of channels should be correct.
1040 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
1041 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1042 EXPECT_EQ(kChannels, output.num_channels_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001043 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001044
henrik.lundin6d8e0112016-03-04 10:34:21 -08001045 // Second call to GetAudio will decode the packet that is ok. No errors are
1046 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -07001047 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001048 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1049 EXPECT_EQ(kChannels, output.num_channels_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001050 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -07001051
1052 // Die isn't called through NiceMock (since it's called by the
1053 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1054 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001055}
1056
henrik.lundin116c84e2015-08-27 13:14:48 -07001057// This test inserts packets until the buffer is flushed. After that, it asks
1058// NetEq for the network statistics. The purpose of the test is to make sure
1059// that even though the buffer size increment is negative (which it becomes when
1060// the packet causing a flush is inserted), the packet length stored in the
1061// decision logic remains valid.
1062TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1063 UseNoMocks();
1064 CreateInstance();
1065
1066 const size_t kPayloadLengthSamples = 80;
1067 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1068 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1069 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1070 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001071 RTPHeader rtp_header;
1072 rtp_header.payloadType = kPayloadType;
1073 rtp_header.sequenceNumber = 0x1234;
1074 rtp_header.timestamp = 0x12345678;
1075 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001076
Niels Möller05543682019-01-10 16:55:06 +01001077 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1078 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001079
1080 // Insert packets until the buffer flushes.
1081 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1082 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1083 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001084 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1085 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1086 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001087 }
1088 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1089
1090 // Ask for network statistics. This should not crash.
1091 NetEqNetworkStatistics stats;
1092 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1093}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001094
1095TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1096 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001097 // Create a mock decoder object.
1098 MockAudioDecoder mock_decoder;
1099
1100 CreateInstance(
1101 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001102
1103 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1104 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1105 const int kSampleRateHz = 8000;
1106 const size_t kPayloadLengthSamples =
1107 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1108 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1109 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001110 RTPHeader rtp_header;
1111 rtp_header.payloadType = kPayloadType;
1112 rtp_header.sequenceNumber = 0x1234;
1113 rtp_header.timestamp = 0x12345678;
1114 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001115
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001116 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001117 EXPECT_CALL(mock_decoder, SampleRateHz())
1118 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001119 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1120 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1121 .WillRepeatedly(Return(0));
1122 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001123 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001124 int16_t dummy_output[kPayloadLengthSamples] = {0};
1125 // The below expectation will make the mock decoder write
1126 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1127 // speech. That is, the decoded length is 5 samples shorter than the expected.
1128 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001129 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001130 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001131 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001132 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001133 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001134 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001135 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1136 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001137
1138 // Insert one packet.
1139 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001140 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001141
1142 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1143
1144 // Pull audio once.
1145 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001146 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001147 bool muted;
1148 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001149 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1150 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001151 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001152 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001153
1154 EXPECT_CALL(mock_decoder, Die());
1155}
minyuel6d92bf52015-09-23 15:20:39 +02001156
1157// This test checks the behavior of NetEq when audio decoder fails.
1158TEST_F(NetEqImplTest, DecodingError) {
1159 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001160 // Create a mock decoder object.
1161 MockAudioDecoder mock_decoder;
1162
1163 CreateInstance(
1164 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001165
1166 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1167 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1168 const int kSampleRateHz = 8000;
1169 const int kDecoderErrorCode = -97; // Any negative number.
1170
1171 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1172 const size_t kFrameLengthSamples =
1173 static_cast<size_t>(5 * kSampleRateHz / 1000);
1174
1175 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1176
1177 uint8_t payload[kPayloadLengthBytes] = {0};
1178
henrik.lundin246ef3e2017-04-24 09:14:32 -07001179 RTPHeader rtp_header;
1180 rtp_header.payloadType = kPayloadType;
1181 rtp_header.sequenceNumber = 0x1234;
1182 rtp_header.timestamp = 0x12345678;
1183 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001184
minyuel6d92bf52015-09-23 15:20:39 +02001185 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001186 EXPECT_CALL(mock_decoder, SampleRateHz())
1187 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001188 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1189 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1190 .WillRepeatedly(Return(0));
1191 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001192 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001193 EXPECT_CALL(mock_decoder, ErrorCode())
1194 .WillOnce(Return(kDecoderErrorCode));
1195 EXPECT_CALL(mock_decoder, HasDecodePlc())
1196 .WillOnce(Return(false));
1197 int16_t dummy_output[kFrameLengthSamples] = {0};
1198
1199 {
1200 InSequence sequence; // Dummy variable.
1201 // Mock decoder works normally the first time.
1202 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001203 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001204 .Times(3)
1205 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001206 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001207 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001208 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001209 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001210 .RetiresOnSaturation();
1211
1212 // Then mock decoder fails. A common reason for failure can be buffer being
1213 // too short
1214 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001215 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001216 .WillOnce(Return(-1))
1217 .RetiresOnSaturation();
1218
1219 // Mock decoder finally returns to normal.
1220 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001221 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001222 .Times(2)
1223 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001224 DoAll(SetArrayArgument<3>(dummy_output,
1225 dummy_output + kFrameLengthSamples),
1226 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001227 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001228 }
1229
Niels Möllera1eb9c72018-12-07 15:24:42 +01001230 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1231 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001232
1233 // Insert packets.
1234 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001235 rtp_header.sequenceNumber += 1;
1236 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001237 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001238 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001239 }
1240
1241 // Pull audio.
1242 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001243 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001244 bool muted;
1245 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001246 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1247 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001248 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001249 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001250
1251 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001252 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001253 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1254 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001255 // We are not expecting anything for output.speech_type_, since an error was
1256 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001257
1258 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001259 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001260 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1261 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001262 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001263 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001264
1265 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001266 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001267 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1268 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001269 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Chen Xing3e8ef942019-07-01 17:16:32 +02001270 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001271
1272 EXPECT_CALL(mock_decoder, Die());
1273}
1274
1275// This test checks the behavior of NetEq when audio decoder fails during CNG.
1276TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1277 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001278
1279 // Create a mock decoder object.
1280 MockAudioDecoder mock_decoder;
1281 CreateInstance(
1282 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001283
1284 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1285 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1286 const int kSampleRateHz = 8000;
1287 const int kDecoderErrorCode = -97; // Any negative number.
1288
1289 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1290 const size_t kFrameLengthSamples =
1291 static_cast<size_t>(5 * kSampleRateHz / 1000);
1292
1293 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1294
1295 uint8_t payload[kPayloadLengthBytes] = {0};
1296
henrik.lundin246ef3e2017-04-24 09:14:32 -07001297 RTPHeader rtp_header;
1298 rtp_header.payloadType = kPayloadType;
1299 rtp_header.sequenceNumber = 0x1234;
1300 rtp_header.timestamp = 0x12345678;
1301 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001302
minyuel6d92bf52015-09-23 15:20:39 +02001303 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001304 EXPECT_CALL(mock_decoder, SampleRateHz())
1305 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001306 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1307 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1308 .WillRepeatedly(Return(0));
1309 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001310 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001311 EXPECT_CALL(mock_decoder, ErrorCode())
1312 .WillOnce(Return(kDecoderErrorCode));
1313 int16_t dummy_output[kFrameLengthSamples] = {0};
1314
1315 {
1316 InSequence sequence; // Dummy variable.
1317 // Mock decoder works normally the first 2 times.
1318 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001319 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001320 .Times(2)
1321 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001322 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001323 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001324 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001325 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001326 .RetiresOnSaturation();
1327
1328 // Then mock decoder fails. A common reason for failure can be buffer being
1329 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001330 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001331 .WillOnce(Return(-1))
1332 .RetiresOnSaturation();
1333
1334 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001335 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001336 .Times(2)
1337 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001338 DoAll(SetArrayArgument<3>(dummy_output,
1339 dummy_output + kFrameLengthSamples),
1340 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001341 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001342 }
1343
Niels Möller50b66d52018-12-11 14:43:21 +01001344 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1345 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001346
1347 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1348 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001349 rtp_header.sequenceNumber += 1;
1350 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001351 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001352 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001353 }
1354
1355 // Pull audio.
1356 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001357 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001358 bool muted;
1359 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001360 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1361 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001362 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001363
1364 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001365 EXPECT_EQ(NetEq::kFail, 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 // We are not expecting anything for output.speech_type_, since an error was
1369 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001370
1371 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001372 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001373 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1374 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001375 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001376
1377 EXPECT_CALL(mock_decoder, Die());
1378}
1379
henrik.lundind89814b2015-11-23 06:49:25 -08001380// Tests that the return value from last_output_sample_rate_hz() is equal to the
1381// configured inital sample rate.
1382TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1383 UseNoMocks();
1384 config_.sample_rate_hz = 48000;
1385 CreateInstance();
1386 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1387}
1388
henrik.lundined497212016-04-25 10:11:38 -07001389TEST_F(NetEqImplTest, TickTimerIncrement) {
1390 UseNoMocks();
1391 CreateInstance();
1392 ASSERT_TRUE(tick_timer_);
1393 EXPECT_EQ(0u, tick_timer_->ticks());
1394 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001395 bool muted;
1396 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001397 EXPECT_EQ(1u, tick_timer_->ticks());
1398}
1399
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001400TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1401 UseNoMocks();
1402 use_mock_delay_manager_ = true;
1403 CreateInstance();
1404
1405 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1406 .WillOnce(Return(true))
1407 .WillOnce(Return(false));
1408
1409 const int delay_ms = 200;
1410
1411 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1412 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1413}
1414
1415TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1416 UseNoMocks();
1417 use_mock_delay_manager_ = true;
1418 CreateInstance();
1419
1420 const int delay_ms = 200;
1421
1422 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1423 .WillOnce(Return(delay_ms));
1424
1425 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1426}
1427
henrik.lundin114c1b32017-04-26 07:47:32 -07001428TEST_F(NetEqImplTest, TargetDelayMs) {
1429 UseNoMocks();
1430 use_mock_delay_manager_ = true;
1431 CreateInstance();
1432 // Let the dummy target delay be 17 packets.
1433 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1434 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1435 .WillOnce(Return(kTargetLevelPacketsQ8));
1436 // Default packet size before any packet has been decoded is 30 ms, so we are
1437 // expecting 17 * 30 = 510 ms target delay.
1438 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1439}
1440
henrik.lundinb8c55b12017-05-10 07:38:01 -07001441TEST_F(NetEqImplTest, InsertEmptyPacket) {
1442 UseNoMocks();
1443 use_mock_delay_manager_ = true;
1444 CreateInstance();
1445
1446 RTPHeader rtp_header;
1447 rtp_header.payloadType = 17;
1448 rtp_header.sequenceNumber = 0x1234;
1449 rtp_header.timestamp = 0x12345678;
1450 rtp_header.ssrc = 0x87654321;
1451
1452 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1453 neteq_->InsertEmptyPacket(rtp_header);
1454}
1455
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001456TEST_F(NetEqImplTest, EnableRtxHandling) {
1457 UseNoMocks();
1458 use_mock_delay_manager_ = true;
1459 config_.enable_rtx_handling = true;
1460 CreateInstance();
1461 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1462 .Times(1)
1463 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1464
1465 const int kPayloadLengthSamples = 80;
1466 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1467 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1468 const uint32_t kReceiveTime = 17;
1469 uint8_t payload[kPayloadLengthBytes] = {0};
1470 RTPHeader rtp_header;
1471 rtp_header.payloadType = kPayloadType;
1472 rtp_header.sequenceNumber = 0x1234;
1473 rtp_header.timestamp = 0x12345678;
1474 rtp_header.ssrc = 0x87654321;
1475
Niels Möller05543682019-01-10 16:55:06 +01001476 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1477 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001478 EXPECT_EQ(NetEq::kOK,
1479 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1480 AudioFrame output;
1481 bool muted;
1482 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1483
1484 // Insert second packet that was sent before the first packet.
1485 rtp_header.sequenceNumber -= 1;
1486 rtp_header.timestamp -= kPayloadLengthSamples;
1487 EXPECT_CALL(*mock_delay_manager_,
1488 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1489 EXPECT_EQ(NetEq::kOK,
1490 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1491}
1492
minyue5bd33972016-05-02 04:46:11 -07001493class Decoder120ms : public AudioDecoder {
1494 public:
kwiberg347d3512016-06-16 01:59:09 -07001495 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1496 : sample_rate_hz_(sample_rate_hz),
1497 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001498 speech_type_(speech_type) {}
1499
1500 int DecodeInternal(const uint8_t* encoded,
1501 size_t encoded_len,
1502 int sample_rate_hz,
1503 int16_t* decoded,
1504 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001505 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001506 size_t decoded_len =
1507 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1508 for (size_t i = 0; i < decoded_len; ++i) {
1509 decoded[i] = next_value_++;
1510 }
1511 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001512 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001513 }
1514
1515 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001516 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001517 size_t Channels() const override { return 2; }
1518
1519 private:
kwiberg347d3512016-06-16 01:59:09 -07001520 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001521 int16_t next_value_;
1522 SpeechType speech_type_;
1523};
1524
1525class NetEqImplTest120ms : public NetEqImplTest {
1526 protected:
1527 NetEqImplTest120ms() : NetEqImplTest() {}
1528 virtual ~NetEqImplTest120ms() {}
1529
1530 void CreateInstanceNoMocks() {
1531 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001532 CreateInstance(decoder_factory_);
1533 EXPECT_TRUE(neteq_->RegisterPayloadType(
1534 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001535 }
1536
1537 void CreateInstanceWithDelayManagerMock() {
1538 UseNoMocks();
1539 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001540 CreateInstance(decoder_factory_);
1541 EXPECT_TRUE(neteq_->RegisterPayloadType(
1542 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001543 }
1544
1545 uint32_t timestamp_diff_between_packets() const {
1546 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1547 }
1548
1549 uint32_t first_timestamp() const { return 10u; }
1550
1551 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001552 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001553 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001554 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1555 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001556 }
1557 }
1558
1559 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001560 RTPHeader rtp_header;
1561 rtp_header.payloadType = kPayloadType;
1562 rtp_header.sequenceNumber = sequence_number_;
1563 rtp_header.timestamp = timestamp;
1564 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001565 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1566 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001567 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001568 sequence_number_++;
1569 }
1570
1571 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001572 const uint32_t sampling_freq = kSamplingFreq_;
1573 decoder_factory_ =
1574 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1575 [sampling_freq, speech_type]() {
1576 std::unique_ptr<AudioDecoder> decoder =
1577 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1578 RTC_CHECK_EQ(2, decoder->Channels());
1579 return decoder;
1580 });
minyue5bd33972016-05-02 04:46:11 -07001581 }
1582
Niels Möllera0f44302018-11-30 10:45:12 +01001583 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001584 AudioFrame output_;
1585 const uint32_t kPayloadType = 17;
1586 const uint32_t kSamplingFreq_ = 48000;
1587 uint16_t sequence_number_ = 1;
1588};
1589
minyue5bd33972016-05-02 04:46:11 -07001590TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001591 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001592 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001593
1594 InsertPacket(first_timestamp());
1595 GetFirstPacket();
1596
henrik.lundin7a926812016-05-12 13:51:28 -07001597 bool muted;
1598 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001599 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1600}
1601
1602TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001603 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001604 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001605
1606 InsertPacket(first_timestamp());
1607 GetFirstPacket();
1608
1609 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1610}
1611
1612TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001613 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001614 CreateInstanceWithDelayManagerMock();
1615
minyue5bd33972016-05-02 04:46:11 -07001616 InsertPacket(first_timestamp());
1617
1618 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001619 bool muted;
1620 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001621
1622 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1623
1624 // Delay manager reports a target level which should cause a Merge.
1625 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1626
henrik.lundin7a926812016-05-12 13:51:28 -07001627 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001628 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1629}
1630
1631TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001632 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001633 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001634
1635 InsertPacket(first_timestamp());
1636 GetFirstPacket();
1637
henrik.lundin7a926812016-05-12 13:51:28 -07001638 bool muted;
1639 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001640 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1641}
1642
1643TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001644 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001645 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001646
1647 InsertPacket(first_timestamp());
1648 GetFirstPacket();
1649 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1650
1651 // Delay manager report buffer limit which should cause a FastAccelerate.
1652 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1653 .Times(1)
1654 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1655
henrik.lundin7a926812016-05-12 13:51:28 -07001656 bool muted;
1657 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001658 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1659}
1660
1661TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001662 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001663 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001664
1665 InsertPacket(first_timestamp());
1666 GetFirstPacket();
1667
1668 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1669
1670 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1671 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1672 .Times(1)
1673 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1674
henrik.lundin7a926812016-05-12 13:51:28 -07001675 bool muted;
1676 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001677 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1678}
1679
1680TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001681 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001682 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001683
1684 InsertPacket(first_timestamp());
1685 GetFirstPacket();
1686
1687 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1688
1689 // Delay manager report buffer limit which should cause a Accelerate.
1690 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1691 .Times(1)
1692 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1693
henrik.lundin7a926812016-05-12 13:51:28 -07001694 bool muted;
1695 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001696 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1697}
1698
Chen Xing3e8ef942019-07-01 17:16:32 +02001699} // namespace webrtc