blob: 39c4e521ddfa3b37de8554a4fac2c32798687907 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/neteq_impl.h"
12
kwiberg84be5112016-04-27 01:19:58 -070013#include <memory>
Alessio Bazzica8f319a32019-07-24 16:47:02 +000014#include <utility>
15#include <vector>
kwiberg84be5112016-04-27 01:19:58 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/neteq/accelerate.h"
19#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010020#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/neteq/include/neteq.h"
22#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
23#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
24#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
25#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
26#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
27#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
28#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
29#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#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"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000035#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;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000046using ::testing::ElementsAre;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047using ::testing::InSequence;
48using ::testing::Invoke;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000049using ::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;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000056using ::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:
Alessio Bazzica8f319a32019-07-24 16:47:02 +000071 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);
Alessio Bazzica8f319a32019-07-24 16:47:02 +000076 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_,
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200111 deps.stats.get(), std::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.
Jonas Olssona4d87372019-07-05 19:08:33 +0200201 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
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000226 // 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.
Jonas Olssona4d87372019-07-05 19:08:33 +0200231 const std::vector<int16_t> kOutput(
232 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
233 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
234 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
235 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
236 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
237 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
238 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800239 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700240 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800241 }
242
henrik.lundin1d9061e2016-04-26 12:19:34 -0700243 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000244 NetEq::Config config_;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000245 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700246 TickTimer* tick_timer_ = nullptr;
247 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
248 BufferLevelFilter* buffer_level_filter_ = nullptr;
249 bool use_mock_buffer_level_filter_ = true;
250 MockDecoderDatabase* mock_decoder_database_ = nullptr;
251 DecoderDatabase* decoder_database_ = nullptr;
252 bool use_mock_decoder_database_ = true;
253 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
254 DelayPeakDetector* delay_peak_detector_ = nullptr;
255 bool use_mock_delay_peak_detector_ = true;
256 MockDelayManager* mock_delay_manager_ = nullptr;
257 DelayManager* delay_manager_ = nullptr;
258 bool use_mock_delay_manager_ = true;
259 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
260 DtmfBuffer* dtmf_buffer_ = nullptr;
261 bool use_mock_dtmf_buffer_ = true;
262 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
263 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
264 bool use_mock_dtmf_tone_generator_ = true;
265 MockPacketBuffer* mock_packet_buffer_ = nullptr;
266 PacketBuffer* packet_buffer_ = nullptr;
267 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700268 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
269 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700270 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000271};
272
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273// 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;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000277 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.
Jonas Olssona4d87372019-07-05 19:08:33 +0200361 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200362 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000363 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100364 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
365 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
367 // index) is a pointer, and the variable pointed to is set to kPayloadType.
368 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
369 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700370 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000371 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700372 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373
374 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200375 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376
377 // Expectations for delay manager.
378 {
379 // All expectations within this block must be called in this specific order.
380 InSequence sequence; // Dummy variable.
381 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000382 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 .Times(2)
384 .WillRepeatedly(Return(-1));
Jonas Olssona4d87372019-07-05 19:08:33 +0200385 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0)).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000386 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000388 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
389 .WillOnce(Return(0));
390 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391 .WillOnce(Return(0));
392 }
393
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000394 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700395 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000396
397 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700398 rtp_header.timestamp += 160;
399 rtp_header.sequenceNumber += 1;
400 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401}
402
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000403TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
404 UseNoMocks();
405 CreateInstance();
406
407 const int kPayloadLengthSamples = 80;
408 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200409 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000410 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
411 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700412 RTPHeader rtp_header;
413 rtp_header.payloadType = kPayloadType;
414 rtp_header.sequenceNumber = 0x1234;
415 rtp_header.timestamp = 0x12345678;
416 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000417
Niels Möller05543682019-01-10 16:55:06 +0100418 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
419 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000420
421 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700422 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000423 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700424 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
425 rtp_header.timestamp += kPayloadLengthSamples;
426 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000427 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
428 }
429
430 // Insert one more packet and make sure the buffer got flushed. That is, it
431 // should only hold one single packet.
432 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700433 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700434 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700435 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700436 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
437 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000438}
439
solenberg2779bab2016-11-17 04:45:19 -0800440TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100441 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800442}
solenberg99df6c02016-10-11 04:35:34 -0700443
solenberg2779bab2016-11-17 04:45:19 -0800444TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100445 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800446}
solenberg99df6c02016-10-11 04:35:34 -0700447
solenberg2779bab2016-11-17 04:45:19 -0800448TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100449 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800450}
solenberg99df6c02016-10-11 04:35:34 -0700451
solenberg2779bab2016-11-17 04:45:19 -0800452TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100453 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700454}
455
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000456// This test verifies that timestamps propagate from the incoming packets
457// through to the sync buffer and to the playout timestamp.
458TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000459 const uint8_t kPayloadType = 17; // Just an arbitrary number.
460 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
461 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700462 const size_t kPayloadLengthSamples =
463 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000464 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
465 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700466 RTPHeader rtp_header;
467 rtp_header.payloadType = kPayloadType;
468 rtp_header.sequenceNumber = 0x1234;
469 rtp_header.timestamp = 0x12345678;
470 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000471 rtp_header.numCSRCs = 3;
472 rtp_header.arrOfCSRCs[0] = 43;
473 rtp_header.arrOfCSRCs[1] = 65;
474 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000475
476 // This is a dummy decoder that produces as many output samples as the input
477 // has bytes. The output is an increasing series, starting at 1 for the first
478 // sample, and then increasing by 1 for each sample.
479 class CountingSamplesDecoder : public AudioDecoder {
480 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000481 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000482
483 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100484 int DecodeInternal(const uint8_t* encoded,
485 size_t encoded_len,
486 int /* sample_rate_hz */,
487 int16_t* decoded,
488 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000489 for (size_t i = 0; i < encoded_len; ++i) {
490 decoded[i] = next_value_++;
491 }
492 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200493 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000494 }
495
Karl Wiberg43766482015-08-27 15:22:11 +0200496 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000497
kwiberg347d3512016-06-16 01:59:09 -0700498 int SampleRateHz() const override { return kSampleRateHz; }
499
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000500 size_t Channels() const override { return 1; }
501
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000502 uint16_t next_value() const { return next_value_; }
503
504 private:
505 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000506 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000507
Niels Möllerb7180c02018-12-06 13:07:11 +0100508 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
509 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
510
511 UseNoMocks();
512 CreateInstance(decoder_factory);
513
514 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100515 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000516
517 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000518 clock_.AdvanceTimeMilliseconds(123456);
519 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000520 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700521 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000522
523 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700524 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800525 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700526 bool muted;
527 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
528 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800529 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
530 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800531 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000532
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000533 // Verify |output.packet_infos_|.
534 ASSERT_THAT(output.packet_infos_, SizeIs(1));
535 {
536 const auto& packet_info = output.packet_infos_[0];
537 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
538 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
539 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
540 EXPECT_FALSE(packet_info.audio_level().has_value());
541 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
542 }
543
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000544 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700545 EXPECT_EQ(kPayloadLengthSamples,
546 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000547
548 // The value of the last of the output samples is the same as the number of
549 // samples played from the decoded packet. Thus, this number + the RTP
550 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200551 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700552 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200553 absl::optional<uint32_t>(rtp_header.timestamp +
554 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700555 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000556
557 // Check the timestamp for the last value in the sync buffer. This should
558 // be one full frame length ahead of the RTP timestamp.
559 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
560 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700561 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000562 sync_buffer->end_timestamp());
563
564 // Check that the number of samples still to play from the sync buffer add
565 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800566 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700567 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800568 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000569}
570
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000571TEST_F(NetEqImplTest, ReorderedPacket) {
572 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100573 // Create a mock decoder object.
574 MockAudioDecoder mock_decoder;
575
576 CreateInstance(
577 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000578
579 const uint8_t kPayloadType = 17; // Just an arbitrary number.
580 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
581 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700582 const size_t kPayloadLengthSamples =
583 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000584 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
585 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700586 RTPHeader rtp_header;
587 rtp_header.payloadType = kPayloadType;
588 rtp_header.sequenceNumber = 0x1234;
589 rtp_header.timestamp = 0x12345678;
590 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000591 rtp_header.extension.hasAudioLevel = true;
592 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000593
Karl Wiberg43766482015-08-27 15:22:11 +0200594 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700595 EXPECT_CALL(mock_decoder, SampleRateHz())
596 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000597 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000598 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
599 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700600 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200601 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000602 int16_t dummy_output[kPayloadLengthSamples] = {0};
603 // The below expectation will make the mock decoder write
604 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100605 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
606 kSampleRateHz, _, _))
607 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000608 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100609 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200610 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100611 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
612 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000613
614 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000615 clock_.AdvanceTimeMilliseconds(123456);
616 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000617 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700618 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000619
620 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700621 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800622 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700623 bool muted;
624 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800625 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
626 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800627 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000628
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000629 // Verify |output.packet_infos_|.
630 ASSERT_THAT(output.packet_infos_, SizeIs(1));
631 {
632 const auto& packet_info = output.packet_infos_[0];
633 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
634 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
635 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
636 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
637 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
638 }
639
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000640 // Insert two more packets. The first one is out of order, and is already too
641 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700642 rtp_header.sequenceNumber -= 1;
643 rtp_header.timestamp -= kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000644 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000645 payload[0] = 1;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000646 clock_.AdvanceTimeMilliseconds(1000);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000647 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700648 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
649 rtp_header.sequenceNumber += 2;
650 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000651 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000652 payload[0] = 2;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000653 clock_.AdvanceTimeMilliseconds(2000);
654 expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000655 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700656 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000657
658 // Expect only the second packet to be decoded (the one with "2" as the first
659 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100660 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
661 kSampleRateHz, _, _))
662 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000663 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100664 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200665 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000666
667 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700668 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800669 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
670 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800671 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000672
673 // Now check the packet buffer, and make sure it is empty, since the
674 // out-of-order packet should have been discarded.
675 EXPECT_TRUE(packet_buffer_->Empty());
676
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000677 // Verify |output.packet_infos_|. Expect to only see the second packet.
678 ASSERT_THAT(output.packet_infos_, SizeIs(1));
679 {
680 const auto& packet_info = output.packet_infos_[0];
681 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
682 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
683 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
684 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
685 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
686 }
687
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000688 EXPECT_CALL(mock_decoder, Die());
689}
690
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000691// This test verifies that NetEq can handle the situation where the first
692// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000693TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000694 UseNoMocks();
695 CreateInstance();
696
697 const uint8_t kPayloadType = 17; // Just an arbitrary number.
698 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
699 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700700 const size_t kPayloadLengthSamples =
701 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700702 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000703 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700704 RTPHeader rtp_header;
705 rtp_header.payloadType = kPayloadType;
706 rtp_header.sequenceNumber = 0x1234;
707 rtp_header.timestamp = 0x12345678;
708 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000709
710 // Insert one packet. Note that we have not registered any payload type, so
711 // this packet will be rejected.
712 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700713 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000714
715 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700716 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800717 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700718 bool muted;
719 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800720 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
721 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
722 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800723 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000724 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000725
726 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100727 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
728 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000729
730 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700731 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700732 rtp_header.sequenceNumber++;
733 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000734 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700735 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000736 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
737 }
738
739 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700740 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700741 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800742 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
743 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
744 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800745 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000746 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000747 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000748 }
749}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000750
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200751// This test verifies that audio interruption is not logged for the initial
752// PLC period before the first packet is deocoded.
753// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
754TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
755 UseNoMocks();
756 CreateInstance();
757
758 const uint8_t kPayloadType = 17; // Just an arbitrary number.
759 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
760 const int kSampleRateHz = 8000;
761 const size_t kPayloadLengthSamples =
762 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
763 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
764 uint8_t payload[kPayloadLengthBytes] = {0};
765 RTPHeader rtp_header;
766 rtp_header.payloadType = kPayloadType;
767 rtp_header.sequenceNumber = 0x1234;
768 rtp_header.timestamp = 0x12345678;
769 rtp_header.ssrc = 0x87654321;
770
771 // Register the payload type.
772 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
773 SdpAudioFormat("l16", 8000, 1)));
774
775 // Pull audio several times. No packets have been inserted yet.
776 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
777 AudioFrame output;
778 bool muted;
779 for (int i = 0; i < 100; ++i) {
780 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
781 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
782 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
783 EXPECT_EQ(1u, output.num_channels_);
784 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000785 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200786 }
787
788 // Insert 10 packets.
789 for (size_t i = 0; i < 10; ++i) {
790 rtp_header.sequenceNumber++;
791 rtp_header.timestamp += kPayloadLengthSamples;
792 EXPECT_EQ(NetEq::kOK,
793 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
794 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
795 }
796
797 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
798 for (size_t i = 0; i < 3; ++i) {
799 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
800 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
801 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
802 EXPECT_EQ(1u, output.num_channels_);
803 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
804 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000805 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200806 }
807
808 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200809 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200810}
811
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000812// This test verifies that NetEq can handle comfort noise and enters/quits codec
813// internal CNG mode properly.
814TEST_F(NetEqImplTest, CodecInternalCng) {
815 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100816 // Create a mock decoder object.
817 MockAudioDecoder mock_decoder;
818 CreateInstance(
819 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000820
821 const uint8_t kPayloadType = 17; // Just an arbitrary number.
822 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
823 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700824 const size_t kPayloadLengthSamples =
825 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
826 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000827 uint8_t payload[kPayloadLengthBytes] = {0};
828 int16_t dummy_output[kPayloadLengthSamples] = {0};
829
henrik.lundin246ef3e2017-04-24 09:14:32 -0700830 RTPHeader rtp_header;
831 rtp_header.payloadType = kPayloadType;
832 rtp_header.sequenceNumber = 0x1234;
833 rtp_header.timestamp = 0x12345678;
834 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000835
Karl Wiberg43766482015-08-27 15:22:11 +0200836 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700837 EXPECT_CALL(mock_decoder, SampleRateHz())
838 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000839 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000840 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
841 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700842 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200843 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700844 // Packed duration when asking the decoder for more CNG data (without a new
845 // packet).
846 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200847 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000848
849 // Pointee(x) verifies that first byte of the payload equals x, this makes it
850 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100851 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
852 kSampleRateKhz * 1000, _, _))
853 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000854 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100855 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200856 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000857
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100858 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
859 kSampleRateKhz * 1000, _, _))
860 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000861 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100862 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200863 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000864
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100865 EXPECT_CALL(mock_decoder,
866 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
867 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000868 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100869 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200870 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000871
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100872 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
873 kSampleRateKhz * 1000, _, _))
874 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000875 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100876 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200877 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000878
Niels Möller50b66d52018-12-11 14:43:21 +0100879 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
880 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000881
882 // Insert one packet (decoder will return speech).
883 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700884 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000885
886 // Insert second packet (decoder will return CNG).
887 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700888 rtp_header.sequenceNumber++;
889 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000890 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700891 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000892
Peter Kastingdce40cf2015-08-24 14:52:23 -0700893 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800894 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800895 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200896 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
897 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
898 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000899 int expected_timestamp_increment[8] = {
900 -1, // will not be used.
901 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200902 -1,
903 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
904 -1,
905 -1,
906 50 * kSampleRateKhz,
907 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000908
henrik.lundin7a926812016-05-12 13:51:28 -0700909 bool muted;
910 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200911 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700912 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000913
henrik.lundin0d96ab72016-04-06 12:28:26 -0700914 // Lambda for verifying the timestamps.
915 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200916 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700917 if (expected_timestamp_increment[i] == -1) {
918 // Expect to get an empty timestamp value during CNG and PLC.
919 EXPECT_FALSE(ts) << "i = " << i;
920 } else {
921 ASSERT_TRUE(ts) << "i = " << i;
922 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
923 << "i = " << i;
924 last_timestamp = ts;
925 }
926 };
927
Peter Kastingdce40cf2015-08-24 14:52:23 -0700928 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800929 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
930 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800931 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700932 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700933 SCOPED_TRACE("");
934 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000935 }
936
937 // Insert third packet, which leaves a gap from last packet.
938 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700939 rtp_header.sequenceNumber += 2;
940 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000941 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700942 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000943
Peter Kastingdce40cf2015-08-24 14:52:23 -0700944 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800945 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
946 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800947 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700948 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700949 SCOPED_TRACE("");
950 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000951 }
952
953 // Now check the packet buffer, and make sure it is empty.
954 EXPECT_TRUE(packet_buffer_->Empty());
955
956 EXPECT_CALL(mock_decoder, Die());
957}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000958
959TEST_F(NetEqImplTest, UnsupportedDecoder) {
960 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100961 ::testing::NiceMock<MockAudioDecoder> decoder;
962
963 CreateInstance(
964 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700965 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800966 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000967
968 const uint8_t kPayloadType = 17; // Just an arbitrary number.
969 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
970 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000971
Peter Kastingdce40cf2015-08-24 14:52:23 -0700972 const size_t kPayloadLengthSamples =
973 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000974 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700975 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200976 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700977 RTPHeader rtp_header;
978 rtp_header.payloadType = kPayloadType;
979 rtp_header.sequenceNumber = 0x1234;
980 rtp_header.timestamp = 0x12345678;
981 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000982
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000983 const uint8_t kFirstPayloadValue = 1;
984 const uint8_t kSecondPayloadValue = 2;
985
ossu61a208b2016-09-20 01:38:00 -0700986 EXPECT_CALL(decoder,
987 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
988 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200989 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000990
ossu61a208b2016-09-20 01:38:00 -0700991 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000992 .Times(0);
993
ossu61a208b2016-09-20 01:38:00 -0700994 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
995 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000996 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700997 .WillOnce(DoAll(
998 SetArrayArgument<3>(dummy_output,
999 dummy_output + kPayloadLengthSamples * kChannels),
1000 SetArgPointee<4>(AudioDecoder::kSpeech),
1001 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001002
ossu61a208b2016-09-20 01:38:00 -07001003 EXPECT_CALL(decoder,
1004 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
1005 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001006 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -07001007
Jonas Olssona4d87372019-07-05 19:08:33 +02001008 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -07001009
Jonas Olssona4d87372019-07-05 19:08:33 +02001010 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001011
Niels Möllera1eb9c72018-12-07 15:24:42 +01001012 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1013 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001014
1015 // Insert one packet.
1016 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
1017 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001018 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001019
1020 // Insert another packet.
1021 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001022 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001023 // The second timestamp needs to be at least 30 ms after the first to make
1024 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001025 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001026 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001027 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001028
henrik.lundin6d8e0112016-03-04 10:34:21 -08001029 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001030 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001031 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +02001032 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -07001033 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001034 // Output size and number of channels should be correct.
1035 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
1036 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1037 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001038 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001039
henrik.lundin6d8e0112016-03-04 10:34:21 -08001040 // Second call to GetAudio will decode the packet that is ok. No errors are
1041 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -07001042 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001043 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1044 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001045 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -07001046
1047 // Die isn't called through NiceMock (since it's called by the
1048 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1049 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001050}
1051
henrik.lundin116c84e2015-08-27 13:14:48 -07001052// This test inserts packets until the buffer is flushed. After that, it asks
1053// NetEq for the network statistics. The purpose of the test is to make sure
1054// that even though the buffer size increment is negative (which it becomes when
1055// the packet causing a flush is inserted), the packet length stored in the
1056// decision logic remains valid.
1057TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1058 UseNoMocks();
1059 CreateInstance();
1060
1061 const size_t kPayloadLengthSamples = 80;
1062 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1063 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1064 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1065 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001066 RTPHeader rtp_header;
1067 rtp_header.payloadType = kPayloadType;
1068 rtp_header.sequenceNumber = 0x1234;
1069 rtp_header.timestamp = 0x12345678;
1070 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001071
Niels Möller05543682019-01-10 16:55:06 +01001072 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1073 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001074
1075 // Insert packets until the buffer flushes.
1076 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1077 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1078 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001079 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1080 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1081 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001082 }
1083 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1084
1085 // Ask for network statistics. This should not crash.
1086 NetEqNetworkStatistics stats;
1087 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1088}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001089
1090TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1091 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001092 // Create a mock decoder object.
1093 MockAudioDecoder mock_decoder;
1094
1095 CreateInstance(
1096 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001097
1098 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1099 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1100 const int kSampleRateHz = 8000;
1101 const size_t kPayloadLengthSamples =
1102 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1103 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1104 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001105 RTPHeader rtp_header;
1106 rtp_header.payloadType = kPayloadType;
1107 rtp_header.sequenceNumber = 0x1234;
1108 rtp_header.timestamp = 0x12345678;
1109 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001110
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001111 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001112 EXPECT_CALL(mock_decoder, SampleRateHz())
1113 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001114 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1115 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1116 .WillRepeatedly(Return(0));
1117 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001118 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001119 int16_t dummy_output[kPayloadLengthSamples] = {0};
1120 // The below expectation will make the mock decoder write
1121 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1122 // speech. That is, the decoded length is 5 samples shorter than the expected.
1123 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001124 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001125 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001126 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001127 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001128 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001129 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001130 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1131 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001132
1133 // Insert one packet.
1134 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001135 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001136
1137 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1138
1139 // Pull audio once.
1140 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001141 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001142 bool muted;
1143 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001144 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1145 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001146 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001147 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001148
1149 EXPECT_CALL(mock_decoder, Die());
1150}
minyuel6d92bf52015-09-23 15:20:39 +02001151
1152// This test checks the behavior of NetEq when audio decoder fails.
1153TEST_F(NetEqImplTest, DecodingError) {
1154 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001155 // Create a mock decoder object.
1156 MockAudioDecoder mock_decoder;
1157
1158 CreateInstance(
1159 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001160
1161 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1162 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1163 const int kSampleRateHz = 8000;
1164 const int kDecoderErrorCode = -97; // Any negative number.
1165
1166 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1167 const size_t kFrameLengthSamples =
1168 static_cast<size_t>(5 * kSampleRateHz / 1000);
1169
1170 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1171
1172 uint8_t payload[kPayloadLengthBytes] = {0};
1173
henrik.lundin246ef3e2017-04-24 09:14:32 -07001174 RTPHeader rtp_header;
1175 rtp_header.payloadType = kPayloadType;
1176 rtp_header.sequenceNumber = 0x1234;
1177 rtp_header.timestamp = 0x12345678;
1178 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001179
minyuel6d92bf52015-09-23 15:20:39 +02001180 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001181 EXPECT_CALL(mock_decoder, SampleRateHz())
1182 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001183 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1184 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1185 .WillRepeatedly(Return(0));
1186 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001187 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001188 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1189 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001190 int16_t dummy_output[kFrameLengthSamples] = {0};
1191
1192 {
1193 InSequence sequence; // Dummy variable.
1194 // Mock decoder works normally the first time.
1195 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001196 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001197 .Times(3)
1198 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001199 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001200 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001201 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001202 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001203 .RetiresOnSaturation();
1204
1205 // Then mock decoder fails. A common reason for failure can be buffer being
1206 // too short
1207 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001208 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001209 .WillOnce(Return(-1))
1210 .RetiresOnSaturation();
1211
1212 // Mock decoder finally returns to normal.
1213 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001214 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001215 .Times(2)
1216 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001217 DoAll(SetArrayArgument<3>(dummy_output,
1218 dummy_output + kFrameLengthSamples),
1219 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001220 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001221 }
1222
Niels Möllera1eb9c72018-12-07 15:24:42 +01001223 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1224 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001225
1226 // Insert packets.
1227 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001228 rtp_header.sequenceNumber += 1;
1229 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001230 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001231 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001232 }
1233
1234 // Pull audio.
1235 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001237 bool muted;
1238 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001239 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1240 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001241 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001242 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001243
1244 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001245 EXPECT_EQ(NetEq::kFail, 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 // We are not expecting anything for output.speech_type_, since an error was
1249 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001250
1251 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001252 EXPECT_EQ(NetEq::kOK, 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 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001256 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001257
1258 // Pull audio again, should behave normal.
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::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001263 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001264
1265 EXPECT_CALL(mock_decoder, Die());
1266}
1267
1268// This test checks the behavior of NetEq when audio decoder fails during CNG.
1269TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1270 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001271
1272 // Create a mock decoder object.
1273 MockAudioDecoder mock_decoder;
1274 CreateInstance(
1275 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001276
1277 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1278 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1279 const int kSampleRateHz = 8000;
1280 const int kDecoderErrorCode = -97; // Any negative number.
1281
1282 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1283 const size_t kFrameLengthSamples =
1284 static_cast<size_t>(5 * kSampleRateHz / 1000);
1285
1286 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1287
1288 uint8_t payload[kPayloadLengthBytes] = {0};
1289
henrik.lundin246ef3e2017-04-24 09:14:32 -07001290 RTPHeader rtp_header;
1291 rtp_header.payloadType = kPayloadType;
1292 rtp_header.sequenceNumber = 0x1234;
1293 rtp_header.timestamp = 0x12345678;
1294 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001295
minyuel6d92bf52015-09-23 15:20:39 +02001296 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001297 EXPECT_CALL(mock_decoder, SampleRateHz())
1298 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001299 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1300 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1301 .WillRepeatedly(Return(0));
1302 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001303 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001304 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001305 int16_t dummy_output[kFrameLengthSamples] = {0};
1306
1307 {
1308 InSequence sequence; // Dummy variable.
1309 // Mock decoder works normally the first 2 times.
1310 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001311 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001312 .Times(2)
1313 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001314 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001315 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001316 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001317 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001318 .RetiresOnSaturation();
1319
1320 // Then mock decoder fails. A common reason for failure can be buffer being
1321 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001322 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001323 .WillOnce(Return(-1))
1324 .RetiresOnSaturation();
1325
1326 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001327 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001328 .Times(2)
1329 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001330 DoAll(SetArrayArgument<3>(dummy_output,
1331 dummy_output + kFrameLengthSamples),
1332 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001333 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001334 }
1335
Niels Möller50b66d52018-12-11 14:43:21 +01001336 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1337 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001338
1339 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1340 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001341 rtp_header.sequenceNumber += 1;
1342 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001343 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001344 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001345 }
1346
1347 // Pull audio.
1348 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001349 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001350 bool muted;
1351 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001352 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1353 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001354 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001355
1356 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001357 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001358 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1359 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001360 // We are not expecting anything for output.speech_type_, since an error was
1361 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001362
1363 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001364 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001365 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1366 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001367 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001368
1369 EXPECT_CALL(mock_decoder, Die());
1370}
1371
henrik.lundind89814b2015-11-23 06:49:25 -08001372// Tests that the return value from last_output_sample_rate_hz() is equal to the
1373// configured inital sample rate.
1374TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1375 UseNoMocks();
1376 config_.sample_rate_hz = 48000;
1377 CreateInstance();
1378 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1379}
1380
henrik.lundined497212016-04-25 10:11:38 -07001381TEST_F(NetEqImplTest, TickTimerIncrement) {
1382 UseNoMocks();
1383 CreateInstance();
1384 ASSERT_TRUE(tick_timer_);
1385 EXPECT_EQ(0u, tick_timer_->ticks());
1386 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001387 bool muted;
1388 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001389 EXPECT_EQ(1u, tick_timer_->ticks());
1390}
1391
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001392TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1393 UseNoMocks();
1394 use_mock_delay_manager_ = true;
1395 CreateInstance();
1396
1397 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1398 .WillOnce(Return(true))
1399 .WillOnce(Return(false));
1400
1401 const int delay_ms = 200;
1402
1403 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1404 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1405}
1406
1407TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1408 UseNoMocks();
1409 use_mock_delay_manager_ = true;
1410 CreateInstance();
1411
1412 const int delay_ms = 200;
1413
1414 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1415 .WillOnce(Return(delay_ms));
1416
1417 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1418}
1419
henrik.lundin114c1b32017-04-26 07:47:32 -07001420TEST_F(NetEqImplTest, TargetDelayMs) {
1421 UseNoMocks();
1422 use_mock_delay_manager_ = true;
1423 CreateInstance();
1424 // Let the dummy target delay be 17 packets.
1425 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1426 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1427 .WillOnce(Return(kTargetLevelPacketsQ8));
1428 // Default packet size before any packet has been decoded is 30 ms, so we are
1429 // expecting 17 * 30 = 510 ms target delay.
1430 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1431}
1432
henrik.lundinb8c55b12017-05-10 07:38:01 -07001433TEST_F(NetEqImplTest, InsertEmptyPacket) {
1434 UseNoMocks();
1435 use_mock_delay_manager_ = true;
1436 CreateInstance();
1437
1438 RTPHeader rtp_header;
1439 rtp_header.payloadType = 17;
1440 rtp_header.sequenceNumber = 0x1234;
1441 rtp_header.timestamp = 0x12345678;
1442 rtp_header.ssrc = 0x87654321;
1443
1444 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1445 neteq_->InsertEmptyPacket(rtp_header);
1446}
1447
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001448TEST_F(NetEqImplTest, EnableRtxHandling) {
1449 UseNoMocks();
1450 use_mock_delay_manager_ = true;
1451 config_.enable_rtx_handling = true;
1452 CreateInstance();
1453 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1454 .Times(1)
1455 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1456
1457 const int kPayloadLengthSamples = 80;
1458 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1459 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1460 const uint32_t kReceiveTime = 17;
1461 uint8_t payload[kPayloadLengthBytes] = {0};
1462 RTPHeader rtp_header;
1463 rtp_header.payloadType = kPayloadType;
1464 rtp_header.sequenceNumber = 0x1234;
1465 rtp_header.timestamp = 0x12345678;
1466 rtp_header.ssrc = 0x87654321;
1467
Niels Möller05543682019-01-10 16:55:06 +01001468 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1469 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001470 EXPECT_EQ(NetEq::kOK,
1471 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1472 AudioFrame output;
1473 bool muted;
1474 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1475
1476 // Insert second packet that was sent before the first packet.
1477 rtp_header.sequenceNumber -= 1;
1478 rtp_header.timestamp -= kPayloadLengthSamples;
1479 EXPECT_CALL(*mock_delay_manager_,
1480 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1481 EXPECT_EQ(NetEq::kOK,
1482 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1483}
1484
minyue5bd33972016-05-02 04:46:11 -07001485class Decoder120ms : public AudioDecoder {
1486 public:
kwiberg347d3512016-06-16 01:59:09 -07001487 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1488 : sample_rate_hz_(sample_rate_hz),
1489 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001490 speech_type_(speech_type) {}
1491
1492 int DecodeInternal(const uint8_t* encoded,
1493 size_t encoded_len,
1494 int sample_rate_hz,
1495 int16_t* decoded,
1496 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001497 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001498 size_t decoded_len =
1499 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1500 for (size_t i = 0; i < decoded_len; ++i) {
1501 decoded[i] = next_value_++;
1502 }
1503 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001504 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001505 }
1506
1507 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001508 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001509 size_t Channels() const override { return 2; }
1510
1511 private:
kwiberg347d3512016-06-16 01:59:09 -07001512 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001513 int16_t next_value_;
1514 SpeechType speech_type_;
1515};
1516
1517class NetEqImplTest120ms : public NetEqImplTest {
1518 protected:
1519 NetEqImplTest120ms() : NetEqImplTest() {}
1520 virtual ~NetEqImplTest120ms() {}
1521
1522 void CreateInstanceNoMocks() {
1523 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001524 CreateInstance(decoder_factory_);
1525 EXPECT_TRUE(neteq_->RegisterPayloadType(
1526 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001527 }
1528
1529 void CreateInstanceWithDelayManagerMock() {
1530 UseNoMocks();
1531 use_mock_delay_manager_ = true;
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 uint32_t timestamp_diff_between_packets() const {
1538 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1539 }
1540
1541 uint32_t first_timestamp() const { return 10u; }
1542
1543 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001544 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001545 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001546 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1547 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001548 }
1549 }
1550
1551 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001552 RTPHeader rtp_header;
1553 rtp_header.payloadType = kPayloadType;
1554 rtp_header.sequenceNumber = sequence_number_;
1555 rtp_header.timestamp = timestamp;
1556 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001557 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1558 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001559 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001560 sequence_number_++;
1561 }
1562
1563 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001564 const uint32_t sampling_freq = kSamplingFreq_;
1565 decoder_factory_ =
1566 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1567 [sampling_freq, speech_type]() {
1568 std::unique_ptr<AudioDecoder> decoder =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001569 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
Niels Möllera0f44302018-11-30 10:45:12 +01001570 RTC_CHECK_EQ(2, decoder->Channels());
1571 return decoder;
1572 });
minyue5bd33972016-05-02 04:46:11 -07001573 }
1574
Niels Möllera0f44302018-11-30 10:45:12 +01001575 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001576 AudioFrame output_;
1577 const uint32_t kPayloadType = 17;
1578 const uint32_t kSamplingFreq_ = 48000;
1579 uint16_t sequence_number_ = 1;
1580};
1581
minyue5bd33972016-05-02 04:46:11 -07001582TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001583 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001584 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001585
1586 InsertPacket(first_timestamp());
1587 GetFirstPacket();
1588
henrik.lundin7a926812016-05-12 13:51:28 -07001589 bool muted;
1590 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001591 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1592}
1593
1594TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001595 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001596 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001597
1598 InsertPacket(first_timestamp());
1599 GetFirstPacket();
1600
1601 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1602}
1603
1604TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001605 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001606 CreateInstanceWithDelayManagerMock();
1607
minyue5bd33972016-05-02 04:46:11 -07001608 InsertPacket(first_timestamp());
1609
1610 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001611 bool muted;
1612 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001613
1614 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1615
1616 // Delay manager reports a target level which should cause a Merge.
1617 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1618
henrik.lundin7a926812016-05-12 13:51:28 -07001619 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001620 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1621}
1622
1623TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001624 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001625 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001626
1627 InsertPacket(first_timestamp());
1628 GetFirstPacket();
1629
henrik.lundin7a926812016-05-12 13:51:28 -07001630 bool muted;
1631 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001632 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1633}
1634
1635TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001636 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001637 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001638
1639 InsertPacket(first_timestamp());
1640 GetFirstPacket();
1641 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1642
1643 // Delay manager report buffer limit which should cause a FastAccelerate.
1644 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1645 .Times(1)
1646 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1647
henrik.lundin7a926812016-05-12 13:51:28 -07001648 bool muted;
1649 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001650 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1651}
1652
1653TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001654 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001655 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001656
1657 InsertPacket(first_timestamp());
1658 GetFirstPacket();
1659
1660 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1661
1662 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1663 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1664 .Times(1)
1665 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1666
henrik.lundin7a926812016-05-12 13:51:28 -07001667 bool muted;
1668 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001669 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1670}
1671
1672TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001673 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001674 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001675
1676 InsertPacket(first_timestamp());
1677 GetFirstPacket();
1678
1679 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1680
1681 // Delay manager report buffer limit which should cause a Accelerate.
1682 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1683 .Times(1)
1684 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1685
henrik.lundin7a926812016-05-12 13:51:28 -07001686 bool muted;
1687 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001688 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1689}
1690
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001691} // namespace webrtc