blob: 2f152c9ac06ba6d68e47f960de5bd536bd0791e2 [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));
kwibergc0f2dcf2016-05-31 06:28:03 -0700338 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
339
340 *dec = std::move(mock_decoder);
341 }));
Niels Möller72899062019-01-11 09:36:13 +0100342 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
343 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344
345 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000346 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000347 .WillRepeatedly(Return(&info));
348
349 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000350 EXPECT_CALL(*mock_packet_buffer_, Empty())
351 .WillOnce(Return(false)); // Called once after first packet is inserted.
Jonas Olssona4d87372019-07-05 19:08:33 +0200352 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200353 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100355 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
356 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
358 // index) is a pointer, and the variable pointed to is set to kPayloadType.
359 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
360 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700361 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000362 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700363 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364
365 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200366 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367
368 // Expectations for delay manager.
369 {
370 // All expectations within this block must be called in this specific order.
371 InSequence sequence; // Dummy variable.
372 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000373 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000374 .Times(2)
375 .WillRepeatedly(Return(-1));
Jonas Olssona4d87372019-07-05 19:08:33 +0200376 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0)).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000377 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000379 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
380 .WillOnce(Return(0));
381 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 .WillOnce(Return(0));
383 }
384
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700386 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387
388 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700389 rtp_header.timestamp += 160;
390 rtp_header.sequenceNumber += 1;
391 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392}
393
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000394TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
395 UseNoMocks();
396 CreateInstance();
397
398 const int kPayloadLengthSamples = 80;
399 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200400 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000401 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
402 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700403 RTPHeader rtp_header;
404 rtp_header.payloadType = kPayloadType;
405 rtp_header.sequenceNumber = 0x1234;
406 rtp_header.timestamp = 0x12345678;
407 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000408
Niels Möller05543682019-01-10 16:55:06 +0100409 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
410 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000411
412 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700413 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000414 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700415 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
416 rtp_header.timestamp += kPayloadLengthSamples;
417 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000418 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
419 }
420
421 // Insert one more packet and make sure the buffer got flushed. That is, it
422 // should only hold one single packet.
423 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700424 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700425 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700426 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700427 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
428 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000429}
430
solenberg2779bab2016-11-17 04:45:19 -0800431TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100432 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800433}
solenberg99df6c02016-10-11 04:35:34 -0700434
solenberg2779bab2016-11-17 04:45:19 -0800435TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100436 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800437}
solenberg99df6c02016-10-11 04:35:34 -0700438
solenberg2779bab2016-11-17 04:45:19 -0800439TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100440 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800441}
solenberg99df6c02016-10-11 04:35:34 -0700442
solenberg2779bab2016-11-17 04:45:19 -0800443TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100444 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700445}
446
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000447// This test verifies that timestamps propagate from the incoming packets
448// through to the sync buffer and to the playout timestamp.
449TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000450 const uint8_t kPayloadType = 17; // Just an arbitrary number.
451 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
452 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700453 const size_t kPayloadLengthSamples =
454 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000455 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
456 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700457 RTPHeader rtp_header;
458 rtp_header.payloadType = kPayloadType;
459 rtp_header.sequenceNumber = 0x1234;
460 rtp_header.timestamp = 0x12345678;
461 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000462 rtp_header.numCSRCs = 3;
463 rtp_header.arrOfCSRCs[0] = 43;
464 rtp_header.arrOfCSRCs[1] = 65;
465 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000466
467 // This is a dummy decoder that produces as many output samples as the input
468 // has bytes. The output is an increasing series, starting at 1 for the first
469 // sample, and then increasing by 1 for each sample.
470 class CountingSamplesDecoder : public AudioDecoder {
471 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000472 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000473
474 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100475 int DecodeInternal(const uint8_t* encoded,
476 size_t encoded_len,
477 int /* sample_rate_hz */,
478 int16_t* decoded,
479 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000480 for (size_t i = 0; i < encoded_len; ++i) {
481 decoded[i] = next_value_++;
482 }
483 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200484 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000485 }
486
Karl Wiberg43766482015-08-27 15:22:11 +0200487 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000488
kwiberg347d3512016-06-16 01:59:09 -0700489 int SampleRateHz() const override { return kSampleRateHz; }
490
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000491 size_t Channels() const override { return 1; }
492
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000493 uint16_t next_value() const { return next_value_; }
494
495 private:
496 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000497 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000498
Niels Möllerb7180c02018-12-06 13:07:11 +0100499 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
500 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
501
502 UseNoMocks();
503 CreateInstance(decoder_factory);
504
505 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100506 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000507
508 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000509 clock_.AdvanceTimeMilliseconds(123456);
510 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000511 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700512 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000513
514 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700515 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800516 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700517 bool muted;
518 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
519 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800520 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
521 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800522 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000523
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000524 // Verify |output.packet_infos_|.
525 ASSERT_THAT(output.packet_infos_, SizeIs(1));
526 {
527 const auto& packet_info = output.packet_infos_[0];
528 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
529 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
530 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
531 EXPECT_FALSE(packet_info.audio_level().has_value());
532 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
533 }
534
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000535 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700536 EXPECT_EQ(kPayloadLengthSamples,
537 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000538
539 // The value of the last of the output samples is the same as the number of
540 // samples played from the decoded packet. Thus, this number + the RTP
541 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200542 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700543 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200544 absl::optional<uint32_t>(rtp_header.timestamp +
545 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700546 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000547
548 // Check the timestamp for the last value in the sync buffer. This should
549 // be one full frame length ahead of the RTP timestamp.
550 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
551 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700552 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000553 sync_buffer->end_timestamp());
554
555 // Check that the number of samples still to play from the sync buffer add
556 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800557 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700558 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800559 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000560}
561
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000562TEST_F(NetEqImplTest, ReorderedPacket) {
563 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100564 // Create a mock decoder object.
565 MockAudioDecoder mock_decoder;
566
567 CreateInstance(
568 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000569
570 const uint8_t kPayloadType = 17; // Just an arbitrary number.
571 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
572 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700573 const size_t kPayloadLengthSamples =
574 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000575 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
576 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700577 RTPHeader rtp_header;
578 rtp_header.payloadType = kPayloadType;
579 rtp_header.sequenceNumber = 0x1234;
580 rtp_header.timestamp = 0x12345678;
581 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000582 rtp_header.extension.hasAudioLevel = true;
583 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000584
Karl Wiberg43766482015-08-27 15:22:11 +0200585 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700586 EXPECT_CALL(mock_decoder, SampleRateHz())
587 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000588 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin034154b2016-04-27 06:11:50 -0700589 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200590 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000591 int16_t dummy_output[kPayloadLengthSamples] = {0};
592 // The below expectation will make the mock decoder write
593 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100594 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
595 kSampleRateHz, _, _))
596 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000597 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100598 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200599 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100600 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
601 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000602
603 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000604 clock_.AdvanceTimeMilliseconds(123456);
605 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000606 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700607 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000608
609 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700610 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800611 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700612 bool muted;
613 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800614 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
615 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800616 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000617
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000618 // Verify |output.packet_infos_|.
619 ASSERT_THAT(output.packet_infos_, SizeIs(1));
620 {
621 const auto& packet_info = output.packet_infos_[0];
622 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
623 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
624 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
625 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
626 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
627 }
628
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000629 // Insert two more packets. The first one is out of order, and is already too
630 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700631 rtp_header.sequenceNumber -= 1;
632 rtp_header.timestamp -= kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000633 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000634 payload[0] = 1;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000635 clock_.AdvanceTimeMilliseconds(1000);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000636 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700637 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
638 rtp_header.sequenceNumber += 2;
639 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000640 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000641 payload[0] = 2;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000642 clock_.AdvanceTimeMilliseconds(2000);
643 expected_receive_time_ms = clock_.TimeInMilliseconds();
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000644 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700645 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000646
647 // Expect only the second packet to be decoded (the one with "2" as the first
648 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100649 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
650 kSampleRateHz, _, _))
651 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000652 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100653 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200654 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000655
656 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700657 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800658 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
659 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800660 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000661
662 // Now check the packet buffer, and make sure it is empty, since the
663 // out-of-order packet should have been discarded.
664 EXPECT_TRUE(packet_buffer_->Empty());
665
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000666 // Verify |output.packet_infos_|. Expect to only see the second packet.
667 ASSERT_THAT(output.packet_infos_, SizeIs(1));
668 {
669 const auto& packet_info = output.packet_infos_[0];
670 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
671 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
672 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
673 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
674 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
675 }
676
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000677 EXPECT_CALL(mock_decoder, Die());
678}
679
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000680// This test verifies that NetEq can handle the situation where the first
681// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000682TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000683 UseNoMocks();
684 CreateInstance();
685
686 const uint8_t kPayloadType = 17; // Just an arbitrary number.
687 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
688 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700689 const size_t kPayloadLengthSamples =
690 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700691 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000692 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700693 RTPHeader rtp_header;
694 rtp_header.payloadType = kPayloadType;
695 rtp_header.sequenceNumber = 0x1234;
696 rtp_header.timestamp = 0x12345678;
697 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000698
699 // Insert one packet. Note that we have not registered any payload type, so
700 // this packet will be rejected.
701 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700702 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000703
704 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700705 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800706 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700707 bool muted;
708 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800709 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
710 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
711 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800712 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000713 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000714
715 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100716 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
717 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000718
719 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700720 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700721 rtp_header.sequenceNumber++;
722 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000723 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700724 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000725 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
726 }
727
728 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700729 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700730 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800731 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
732 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
733 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800734 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000735 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000736 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000737 }
738}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000739
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200740// This test verifies that audio interruption is not logged for the initial
741// PLC period before the first packet is deocoded.
742// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
743TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
744 UseNoMocks();
745 CreateInstance();
746
747 const uint8_t kPayloadType = 17; // Just an arbitrary number.
748 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
749 const int kSampleRateHz = 8000;
750 const size_t kPayloadLengthSamples =
751 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
752 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
753 uint8_t payload[kPayloadLengthBytes] = {0};
754 RTPHeader rtp_header;
755 rtp_header.payloadType = kPayloadType;
756 rtp_header.sequenceNumber = 0x1234;
757 rtp_header.timestamp = 0x12345678;
758 rtp_header.ssrc = 0x87654321;
759
760 // Register the payload type.
761 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
762 SdpAudioFormat("l16", 8000, 1)));
763
764 // Pull audio several times. No packets have been inserted yet.
765 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
766 AudioFrame output;
767 bool muted;
768 for (int i = 0; i < 100; ++i) {
769 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
770 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
771 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
772 EXPECT_EQ(1u, output.num_channels_);
773 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000774 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200775 }
776
777 // Insert 10 packets.
778 for (size_t i = 0; i < 10; ++i) {
779 rtp_header.sequenceNumber++;
780 rtp_header.timestamp += kPayloadLengthSamples;
781 EXPECT_EQ(NetEq::kOK,
782 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
783 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
784 }
785
786 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
787 for (size_t i = 0; i < 3; ++i) {
788 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
789 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
790 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
791 EXPECT_EQ(1u, output.num_channels_);
792 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
793 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000794 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200795 }
796
797 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200798 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200799}
800
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000801// This test verifies that NetEq can handle comfort noise and enters/quits codec
802// internal CNG mode properly.
803TEST_F(NetEqImplTest, CodecInternalCng) {
804 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100805 // Create a mock decoder object.
806 MockAudioDecoder mock_decoder;
807 CreateInstance(
808 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000809
810 const uint8_t kPayloadType = 17; // Just an arbitrary number.
811 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
812 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700813 const size_t kPayloadLengthSamples =
814 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
815 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000816 uint8_t payload[kPayloadLengthBytes] = {0};
817 int16_t dummy_output[kPayloadLengthSamples] = {0};
818
henrik.lundin246ef3e2017-04-24 09:14:32 -0700819 RTPHeader rtp_header;
820 rtp_header.payloadType = kPayloadType;
821 rtp_header.sequenceNumber = 0x1234;
822 rtp_header.timestamp = 0x12345678;
823 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000824
Karl Wiberg43766482015-08-27 15:22:11 +0200825 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700826 EXPECT_CALL(mock_decoder, SampleRateHz())
827 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000828 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700829 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200830 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700831 // Packed duration when asking the decoder for more CNG data (without a new
832 // packet).
833 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200834 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000835
836 // Pointee(x) verifies that first byte of the payload equals x, this makes it
837 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100838 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
839 kSampleRateKhz * 1000, _, _))
840 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000841 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100842 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200843 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000844
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100845 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
846 kSampleRateKhz * 1000, _, _))
847 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000848 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100849 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200850 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000851
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100852 EXPECT_CALL(mock_decoder,
853 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
854 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000855 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100856 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200857 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000858
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100859 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
860 kSampleRateKhz * 1000, _, _))
861 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000862 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100863 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200864 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000865
Niels Möller50b66d52018-12-11 14:43:21 +0100866 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
867 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000868
869 // Insert one packet (decoder will return speech).
870 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700871 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000872
873 // Insert second packet (decoder will return CNG).
874 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700875 rtp_header.sequenceNumber++;
876 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000877 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700878 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000879
Peter Kastingdce40cf2015-08-24 14:52:23 -0700880 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800881 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800882 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200883 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
884 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
885 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000886 int expected_timestamp_increment[8] = {
887 -1, // will not be used.
888 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200889 -1,
890 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
891 -1,
892 -1,
893 50 * kSampleRateKhz,
894 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000895
henrik.lundin7a926812016-05-12 13:51:28 -0700896 bool muted;
897 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200898 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700899 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000900
henrik.lundin0d96ab72016-04-06 12:28:26 -0700901 // Lambda for verifying the timestamps.
902 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200903 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700904 if (expected_timestamp_increment[i] == -1) {
905 // Expect to get an empty timestamp value during CNG and PLC.
906 EXPECT_FALSE(ts) << "i = " << i;
907 } else {
908 ASSERT_TRUE(ts) << "i = " << i;
909 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
910 << "i = " << i;
911 last_timestamp = ts;
912 }
913 };
914
Peter Kastingdce40cf2015-08-24 14:52:23 -0700915 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800916 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
917 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800918 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700919 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700920 SCOPED_TRACE("");
921 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000922 }
923
924 // Insert third packet, which leaves a gap from last packet.
925 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700926 rtp_header.sequenceNumber += 2;
927 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000928 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700929 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000930
Peter Kastingdce40cf2015-08-24 14:52:23 -0700931 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800932 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
933 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800934 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700935 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700936 SCOPED_TRACE("");
937 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000938 }
939
940 // Now check the packet buffer, and make sure it is empty.
941 EXPECT_TRUE(packet_buffer_->Empty());
942
943 EXPECT_CALL(mock_decoder, Die());
944}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000945
946TEST_F(NetEqImplTest, UnsupportedDecoder) {
947 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100948 ::testing::NiceMock<MockAudioDecoder> decoder;
949
950 CreateInstance(
951 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700952 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800953 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000954
955 const uint8_t kPayloadType = 17; // Just an arbitrary number.
956 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
957 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000958
Peter Kastingdce40cf2015-08-24 14:52:23 -0700959 const size_t kPayloadLengthSamples =
960 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000961 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700962 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200963 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700964 RTPHeader rtp_header;
965 rtp_header.payloadType = kPayloadType;
966 rtp_header.sequenceNumber = 0x1234;
967 rtp_header.timestamp = 0x12345678;
968 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000969
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000970 const uint8_t kFirstPayloadValue = 1;
971 const uint8_t kSecondPayloadValue = 2;
972
ossu61a208b2016-09-20 01:38:00 -0700973 EXPECT_CALL(decoder,
974 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
975 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200976 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000977
ossu61a208b2016-09-20 01:38:00 -0700978 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000979 .Times(0);
980
ossu61a208b2016-09-20 01:38:00 -0700981 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
982 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000983 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700984 .WillOnce(DoAll(
985 SetArrayArgument<3>(dummy_output,
986 dummy_output + kPayloadLengthSamples * kChannels),
987 SetArgPointee<4>(AudioDecoder::kSpeech),
988 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000989
ossu61a208b2016-09-20 01:38:00 -0700990 EXPECT_CALL(decoder,
991 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
992 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200993 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700994
Jonas Olssona4d87372019-07-05 19:08:33 +0200995 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -0700996
Jonas Olssona4d87372019-07-05 19:08:33 +0200997 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000998
Niels Möllera1eb9c72018-12-07 15:24:42 +0100999 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1000 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001001
1002 // Insert one packet.
1003 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
1004 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001005 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001006
1007 // Insert another packet.
1008 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001009 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001010 // The second timestamp needs to be at least 30 ms after the first to make
1011 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -07001012 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001013 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001014 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001015
henrik.lundin6d8e0112016-03-04 10:34:21 -08001016 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001017 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -08001018 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +02001019 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -07001020 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001021 // Output size and number of channels should be correct.
1022 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
1023 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1024 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001025 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001026
henrik.lundin6d8e0112016-03-04 10:34:21 -08001027 // Second call to GetAudio will decode the packet that is ok. No errors are
1028 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -07001029 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001030 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1031 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001032 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -07001033
1034 // Die isn't called through NiceMock (since it's called by the
1035 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1036 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001037}
1038
henrik.lundin116c84e2015-08-27 13:14:48 -07001039// This test inserts packets until the buffer is flushed. After that, it asks
1040// NetEq for the network statistics. The purpose of the test is to make sure
1041// that even though the buffer size increment is negative (which it becomes when
1042// the packet causing a flush is inserted), the packet length stored in the
1043// decision logic remains valid.
1044TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1045 UseNoMocks();
1046 CreateInstance();
1047
1048 const size_t kPayloadLengthSamples = 80;
1049 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1050 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1051 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1052 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001053 RTPHeader rtp_header;
1054 rtp_header.payloadType = kPayloadType;
1055 rtp_header.sequenceNumber = 0x1234;
1056 rtp_header.timestamp = 0x12345678;
1057 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001058
Niels Möller05543682019-01-10 16:55:06 +01001059 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1060 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001061
1062 // Insert packets until the buffer flushes.
1063 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1064 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1065 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001066 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1067 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1068 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001069 }
1070 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1071
1072 // Ask for network statistics. This should not crash.
1073 NetEqNetworkStatistics stats;
1074 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1075}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001076
1077TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1078 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001079 // Create a mock decoder object.
1080 MockAudioDecoder mock_decoder;
1081
1082 CreateInstance(
1083 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001084
1085 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1086 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1087 const int kSampleRateHz = 8000;
1088 const size_t kPayloadLengthSamples =
1089 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1090 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1091 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001092 RTPHeader rtp_header;
1093 rtp_header.payloadType = kPayloadType;
1094 rtp_header.sequenceNumber = 0x1234;
1095 rtp_header.timestamp = 0x12345678;
1096 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001097
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001098 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001099 EXPECT_CALL(mock_decoder, SampleRateHz())
1100 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001101 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001102 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001103 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001104 int16_t dummy_output[kPayloadLengthSamples] = {0};
1105 // The below expectation will make the mock decoder write
1106 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1107 // speech. That is, the decoded length is 5 samples shorter than the expected.
1108 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001109 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001110 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001111 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001112 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001113 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001114 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001115 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1116 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001117
1118 // Insert one packet.
1119 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001120 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001121
1122 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1123
1124 // Pull audio once.
1125 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001126 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001127 bool muted;
1128 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001129 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1130 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001131 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001132 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001133
1134 EXPECT_CALL(mock_decoder, Die());
1135}
minyuel6d92bf52015-09-23 15:20:39 +02001136
1137// This test checks the behavior of NetEq when audio decoder fails.
1138TEST_F(NetEqImplTest, DecodingError) {
1139 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001140 // Create a mock decoder object.
1141 MockAudioDecoder mock_decoder;
1142
1143 CreateInstance(
1144 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001145
1146 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1147 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1148 const int kSampleRateHz = 8000;
1149 const int kDecoderErrorCode = -97; // Any negative number.
1150
1151 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1152 const size_t kFrameLengthSamples =
1153 static_cast<size_t>(5 * kSampleRateHz / 1000);
1154
1155 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1156
1157 uint8_t payload[kPayloadLengthBytes] = {0};
1158
henrik.lundin246ef3e2017-04-24 09:14:32 -07001159 RTPHeader rtp_header;
1160 rtp_header.payloadType = kPayloadType;
1161 rtp_header.sequenceNumber = 0x1234;
1162 rtp_header.timestamp = 0x12345678;
1163 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001164
minyuel6d92bf52015-09-23 15:20:39 +02001165 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001166 EXPECT_CALL(mock_decoder, SampleRateHz())
1167 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001168 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001169 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001170 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001171 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1172 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001173 int16_t dummy_output[kFrameLengthSamples] = {0};
1174
1175 {
1176 InSequence sequence; // Dummy variable.
1177 // Mock decoder works normally the first time.
1178 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001179 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001180 .Times(3)
1181 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001182 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001183 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001184 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001185 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001186 .RetiresOnSaturation();
1187
1188 // Then mock decoder fails. A common reason for failure can be buffer being
1189 // too short
1190 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001191 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001192 .WillOnce(Return(-1))
1193 .RetiresOnSaturation();
1194
1195 // Mock decoder finally returns to normal.
1196 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001197 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001198 .Times(2)
1199 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001200 DoAll(SetArrayArgument<3>(dummy_output,
1201 dummy_output + kFrameLengthSamples),
1202 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001203 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001204 }
1205
Niels Möllera1eb9c72018-12-07 15:24:42 +01001206 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1207 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001208
1209 // Insert packets.
1210 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001211 rtp_header.sequenceNumber += 1;
1212 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001213 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001214 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001215 }
1216
1217 // Pull audio.
1218 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001219 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001220 bool muted;
1221 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001222 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1223 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001224 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001225 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001226
1227 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001228 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001229 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1230 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001231 // We are not expecting anything for output.speech_type_, since an error was
1232 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001233
1234 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001235 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1237 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001238 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001239 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001240
1241 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001242 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001243 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1244 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001245 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001246 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001247
1248 EXPECT_CALL(mock_decoder, Die());
1249}
1250
1251// This test checks the behavior of NetEq when audio decoder fails during CNG.
1252TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1253 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001254
1255 // Create a mock decoder object.
1256 MockAudioDecoder mock_decoder;
1257 CreateInstance(
1258 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001259
1260 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1261 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1262 const int kSampleRateHz = 8000;
1263 const int kDecoderErrorCode = -97; // Any negative number.
1264
1265 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1266 const size_t kFrameLengthSamples =
1267 static_cast<size_t>(5 * kSampleRateHz / 1000);
1268
1269 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1270
1271 uint8_t payload[kPayloadLengthBytes] = {0};
1272
henrik.lundin246ef3e2017-04-24 09:14:32 -07001273 RTPHeader rtp_header;
1274 rtp_header.payloadType = kPayloadType;
1275 rtp_header.sequenceNumber = 0x1234;
1276 rtp_header.timestamp = 0x12345678;
1277 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001278
minyuel6d92bf52015-09-23 15:20:39 +02001279 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001280 EXPECT_CALL(mock_decoder, SampleRateHz())
1281 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001282 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001283 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001284 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001285 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001286 int16_t dummy_output[kFrameLengthSamples] = {0};
1287
1288 {
1289 InSequence sequence; // Dummy variable.
1290 // Mock decoder works normally the first 2 times.
1291 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001292 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001293 .Times(2)
1294 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001295 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001296 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001297 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001298 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001299 .RetiresOnSaturation();
1300
1301 // Then mock decoder fails. A common reason for failure can be buffer being
1302 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001303 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001304 .WillOnce(Return(-1))
1305 .RetiresOnSaturation();
1306
1307 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001308 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001309 .Times(2)
1310 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001311 DoAll(SetArrayArgument<3>(dummy_output,
1312 dummy_output + kFrameLengthSamples),
1313 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001314 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001315 }
1316
Niels Möller50b66d52018-12-11 14:43:21 +01001317 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1318 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001319
1320 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1321 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001322 rtp_header.sequenceNumber += 1;
1323 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001324 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001325 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001326 }
1327
1328 // Pull audio.
1329 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001330 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001331 bool muted;
1332 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001333 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1334 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001335 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001336
1337 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001338 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001339 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1340 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001341 // We are not expecting anything for output.speech_type_, since an error was
1342 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001343
1344 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001345 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001346 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1347 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001348 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001349
1350 EXPECT_CALL(mock_decoder, Die());
1351}
1352
henrik.lundind89814b2015-11-23 06:49:25 -08001353// Tests that the return value from last_output_sample_rate_hz() is equal to the
1354// configured inital sample rate.
1355TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1356 UseNoMocks();
1357 config_.sample_rate_hz = 48000;
1358 CreateInstance();
1359 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1360}
1361
henrik.lundined497212016-04-25 10:11:38 -07001362TEST_F(NetEqImplTest, TickTimerIncrement) {
1363 UseNoMocks();
1364 CreateInstance();
1365 ASSERT_TRUE(tick_timer_);
1366 EXPECT_EQ(0u, tick_timer_->ticks());
1367 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001368 bool muted;
1369 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001370 EXPECT_EQ(1u, tick_timer_->ticks());
1371}
1372
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001373TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1374 UseNoMocks();
1375 use_mock_delay_manager_ = true;
1376 CreateInstance();
1377
1378 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1379 .WillOnce(Return(true))
1380 .WillOnce(Return(false));
1381
1382 const int delay_ms = 200;
1383
1384 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1385 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1386}
1387
1388TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1389 UseNoMocks();
1390 use_mock_delay_manager_ = true;
1391 CreateInstance();
1392
1393 const int delay_ms = 200;
1394
1395 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1396 .WillOnce(Return(delay_ms));
1397
1398 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1399}
1400
henrik.lundin114c1b32017-04-26 07:47:32 -07001401TEST_F(NetEqImplTest, TargetDelayMs) {
1402 UseNoMocks();
1403 use_mock_delay_manager_ = true;
1404 CreateInstance();
1405 // Let the dummy target delay be 17 packets.
1406 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1407 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1408 .WillOnce(Return(kTargetLevelPacketsQ8));
1409 // Default packet size before any packet has been decoded is 30 ms, so we are
1410 // expecting 17 * 30 = 510 ms target delay.
1411 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1412}
1413
henrik.lundinb8c55b12017-05-10 07:38:01 -07001414TEST_F(NetEqImplTest, InsertEmptyPacket) {
1415 UseNoMocks();
1416 use_mock_delay_manager_ = true;
1417 CreateInstance();
1418
1419 RTPHeader rtp_header;
1420 rtp_header.payloadType = 17;
1421 rtp_header.sequenceNumber = 0x1234;
1422 rtp_header.timestamp = 0x12345678;
1423 rtp_header.ssrc = 0x87654321;
1424
1425 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1426 neteq_->InsertEmptyPacket(rtp_header);
1427}
1428
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001429TEST_F(NetEqImplTest, EnableRtxHandling) {
1430 UseNoMocks();
1431 use_mock_delay_manager_ = true;
1432 config_.enable_rtx_handling = true;
1433 CreateInstance();
1434 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1435 .Times(1)
1436 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1437
1438 const int kPayloadLengthSamples = 80;
1439 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1440 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1441 const uint32_t kReceiveTime = 17;
1442 uint8_t payload[kPayloadLengthBytes] = {0};
1443 RTPHeader rtp_header;
1444 rtp_header.payloadType = kPayloadType;
1445 rtp_header.sequenceNumber = 0x1234;
1446 rtp_header.timestamp = 0x12345678;
1447 rtp_header.ssrc = 0x87654321;
1448
Niels Möller05543682019-01-10 16:55:06 +01001449 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1450 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001451 EXPECT_EQ(NetEq::kOK,
1452 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1453 AudioFrame output;
1454 bool muted;
1455 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1456
1457 // Insert second packet that was sent before the first packet.
1458 rtp_header.sequenceNumber -= 1;
1459 rtp_header.timestamp -= kPayloadLengthSamples;
1460 EXPECT_CALL(*mock_delay_manager_,
1461 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1462 EXPECT_EQ(NetEq::kOK,
1463 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1464}
1465
minyue5bd33972016-05-02 04:46:11 -07001466class Decoder120ms : public AudioDecoder {
1467 public:
kwiberg347d3512016-06-16 01:59:09 -07001468 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1469 : sample_rate_hz_(sample_rate_hz),
1470 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001471 speech_type_(speech_type) {}
1472
1473 int DecodeInternal(const uint8_t* encoded,
1474 size_t encoded_len,
1475 int sample_rate_hz,
1476 int16_t* decoded,
1477 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001478 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001479 size_t decoded_len =
1480 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1481 for (size_t i = 0; i < decoded_len; ++i) {
1482 decoded[i] = next_value_++;
1483 }
1484 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001485 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001486 }
1487
1488 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001489 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001490 size_t Channels() const override { return 2; }
1491
1492 private:
kwiberg347d3512016-06-16 01:59:09 -07001493 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001494 int16_t next_value_;
1495 SpeechType speech_type_;
1496};
1497
1498class NetEqImplTest120ms : public NetEqImplTest {
1499 protected:
1500 NetEqImplTest120ms() : NetEqImplTest() {}
1501 virtual ~NetEqImplTest120ms() {}
1502
1503 void CreateInstanceNoMocks() {
1504 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001505 CreateInstance(decoder_factory_);
1506 EXPECT_TRUE(neteq_->RegisterPayloadType(
1507 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001508 }
1509
1510 void CreateInstanceWithDelayManagerMock() {
1511 UseNoMocks();
1512 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001513 CreateInstance(decoder_factory_);
1514 EXPECT_TRUE(neteq_->RegisterPayloadType(
1515 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001516 }
1517
1518 uint32_t timestamp_diff_between_packets() const {
1519 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1520 }
1521
1522 uint32_t first_timestamp() const { return 10u; }
1523
1524 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001525 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001526 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001527 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1528 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001529 }
1530 }
1531
1532 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001533 RTPHeader rtp_header;
1534 rtp_header.payloadType = kPayloadType;
1535 rtp_header.sequenceNumber = sequence_number_;
1536 rtp_header.timestamp = timestamp;
1537 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001538 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1539 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001540 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001541 sequence_number_++;
1542 }
1543
1544 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001545 const uint32_t sampling_freq = kSamplingFreq_;
1546 decoder_factory_ =
1547 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1548 [sampling_freq, speech_type]() {
1549 std::unique_ptr<AudioDecoder> decoder =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001550 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
Niels Möllera0f44302018-11-30 10:45:12 +01001551 RTC_CHECK_EQ(2, decoder->Channels());
1552 return decoder;
1553 });
minyue5bd33972016-05-02 04:46:11 -07001554 }
1555
Niels Möllera0f44302018-11-30 10:45:12 +01001556 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001557 AudioFrame output_;
1558 const uint32_t kPayloadType = 17;
1559 const uint32_t kSamplingFreq_ = 48000;
1560 uint16_t sequence_number_ = 1;
1561};
1562
minyue5bd33972016-05-02 04:46:11 -07001563TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001564 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001565 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001566
1567 InsertPacket(first_timestamp());
1568 GetFirstPacket();
1569
henrik.lundin7a926812016-05-12 13:51:28 -07001570 bool muted;
1571 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001572 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1573}
1574
1575TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001576 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001577 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001578
1579 InsertPacket(first_timestamp());
1580 GetFirstPacket();
1581
1582 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1583}
1584
1585TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001586 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001587 CreateInstanceWithDelayManagerMock();
1588
minyue5bd33972016-05-02 04:46:11 -07001589 InsertPacket(first_timestamp());
1590
1591 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001592 bool muted;
1593 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001594
1595 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1596
1597 // Delay manager reports a target level which should cause a Merge.
1598 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1599
henrik.lundin7a926812016-05-12 13:51:28 -07001600 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001601 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1602}
1603
1604TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001605 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001606 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001607
1608 InsertPacket(first_timestamp());
1609 GetFirstPacket();
1610
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 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1614}
1615
1616TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001617 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001618 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001619
1620 InsertPacket(first_timestamp());
1621 GetFirstPacket();
1622 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1623
1624 // Delay manager report buffer limit which should cause a FastAccelerate.
1625 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1626 .Times(1)
1627 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1628
henrik.lundin7a926812016-05-12 13:51:28 -07001629 bool muted;
1630 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001631 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1632}
1633
1634TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001635 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001636 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001637
1638 InsertPacket(first_timestamp());
1639 GetFirstPacket();
1640
1641 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1642
1643 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1644 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1645 .Times(1)
1646 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
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(kPreemptiveExpand, neteq_->last_operation_for_test());
1651}
1652
1653TEST_F(NetEqImplTest120ms, Accelerate) {
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 Accelerate.
1663 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1664 .Times(1)
1665 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
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(kAccelerate, neteq_->last_operation_for_test());
1670}
1671
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001672} // namespace webrtc