blob: 88629050cef32ead905de46f2cb23f6ba8a0b0f0 [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;
solenberg2779bab2016-11-17 04:45:19 -0800195 const int kSampleRateHz = 16000;
196 config_.sample_rate_hz = kSampleRateHz;
197 UseNoMocks();
198 CreateInstance();
199 // Event: 2, E bit, Volume: 17, Length: 4336.
Jonas Olssona4d87372019-07-05 19:08:33 +0200200 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700201 RTPHeader rtp_header;
202 rtp_header.payloadType = kPayloadType;
203 rtp_header.sequenceNumber = 0x1234;
204 rtp_header.timestamp = 0x12345678;
205 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800206
Niels Möller05543682019-01-10 16:55:06 +0100207 EXPECT_TRUE(neteq_->RegisterPayloadType(
208 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800209
210 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200211 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
solenberg2779bab2016-11-17 04:45:19 -0800212
213 // Pull audio once.
214 const size_t kMaxOutputSize =
215 static_cast<size_t>(10 * kSampleRateHz / 1000);
216 AudioFrame output;
217 bool muted;
218 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
219 ASSERT_FALSE(muted);
220 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
221 EXPECT_EQ(1u, output.num_channels_);
222 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
223
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000224 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
225 // returned by |GetAudio()|.
226 EXPECT_THAT(output.packet_infos_, IsEmpty());
227
solenberg2779bab2016-11-17 04:45:19 -0800228 // Verify first 64 samples of actual output.
Jonas Olssona4d87372019-07-05 19:08:33 +0200229 const std::vector<int16_t> kOutput(
230 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
231 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
232 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
233 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
234 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
235 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
236 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800237 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700238 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800239 }
240
henrik.lundin1d9061e2016-04-26 12:19:34 -0700241 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000242 NetEq::Config config_;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000243 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700244 TickTimer* tick_timer_ = nullptr;
245 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
246 BufferLevelFilter* buffer_level_filter_ = nullptr;
247 bool use_mock_buffer_level_filter_ = true;
248 MockDecoderDatabase* mock_decoder_database_ = nullptr;
249 DecoderDatabase* decoder_database_ = nullptr;
250 bool use_mock_decoder_database_ = true;
251 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
252 DelayPeakDetector* delay_peak_detector_ = nullptr;
253 bool use_mock_delay_peak_detector_ = true;
254 MockDelayManager* mock_delay_manager_ = nullptr;
255 DelayManager* delay_manager_ = nullptr;
256 bool use_mock_delay_manager_ = true;
257 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
258 DtmfBuffer* dtmf_buffer_ = nullptr;
259 bool use_mock_dtmf_buffer_ = true;
260 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
261 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
262 bool use_mock_dtmf_tone_generator_ = true;
263 MockPacketBuffer* mock_packet_buffer_ = nullptr;
264 PacketBuffer* packet_buffer_ = nullptr;
265 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700266 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
267 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700268 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000269};
270
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000271// This tests the interface class NetEq.
272// TODO(hlundin): Move to separate file?
273TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000274 NetEq::Config config;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000275 SimulatedClock clock(0);
276 NetEq* neteq =
277 NetEq::Create(config, &clock, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000278 delete neteq;
279}
280
kwiberg5adaf732016-10-04 09:33:27 -0700281TEST_F(NetEqImplTest, RegisterPayloadType) {
282 CreateInstance();
283 constexpr int rtp_payload_type = 0;
284 const SdpAudioFormat format("pcmu", 8000, 1);
285 EXPECT_CALL(*mock_decoder_database_,
286 RegisterPayload(rtp_payload_type, format));
287 neteq_->RegisterPayloadType(rtp_payload_type, format);
288}
289
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000291 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000292 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000293 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200295 // Check that kOK is returned when database returns kDecoderNotFound, because
296 // removing a payload type that was never registered is not an error.
297 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298}
299
kwiberg6b19b562016-09-20 04:02:25 -0700300TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
301 CreateInstance();
302 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
303 neteq_->RemoveAllPayloadTypes();
304}
305
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000307 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000308 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309 const uint8_t kPayloadType = 0;
310 const uint16_t kFirstSequenceNumber = 0x1234;
311 const uint32_t kFirstTimestamp = 0x12345678;
312 const uint32_t kSsrc = 0x87654321;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700314 RTPHeader rtp_header;
315 rtp_header.payloadType = kPayloadType;
316 rtp_header.sequenceNumber = kFirstSequenceNumber;
317 rtp_header.timestamp = kFirstTimestamp;
318 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700319 Packet fake_packet;
320 fake_packet.payload_type = kPayloadType;
321 fake_packet.sequence_number = kFirstSequenceNumber;
322 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323
kwibergc0f2dcf2016-05-31 06:28:03 -0700324 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
325 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100326 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800327 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200328 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800329 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700330 EXPECT_EQ("pcmu", format.name);
331
332 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
333 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
334 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
kwibergc0f2dcf2016-05-31 06:28:03 -0700335 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
336
337 *dec = std::move(mock_decoder);
338 }));
Niels Möller72899062019-01-11 09:36:13 +0100339 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
340 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341
342 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000343 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000344 .WillRepeatedly(Return(&info));
345
346 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000347 EXPECT_CALL(*mock_packet_buffer_, Empty())
348 .WillOnce(Return(false)); // Called once after first packet is inserted.
Jonas Olssona4d87372019-07-05 19:08:33 +0200349 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200350 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100352 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
353 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
355 // index) is a pointer, and the variable pointed to is set to kPayloadType.
356 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
357 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700358 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000359 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700360 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000361
362 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200363 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364
365 // Expectations for delay manager.
366 {
367 // All expectations within this block must be called in this specific order.
368 InSequence sequence; // Dummy variable.
369 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000370 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371 .Times(2)
372 .WillRepeatedly(Return(-1));
Jonas Olssona4d87372019-07-05 19:08:33 +0200373 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0)).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000374 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000376 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
377 .WillOnce(Return(0));
378 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000379 .WillOnce(Return(0));
380 }
381
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200383 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384
385 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700386 rtp_header.timestamp += 160;
387 rtp_header.sequenceNumber += 1;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200388 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000389}
390
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000391TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
392 UseNoMocks();
393 CreateInstance();
394
395 const int kPayloadLengthSamples = 80;
396 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200397 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000398 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700399 RTPHeader rtp_header;
400 rtp_header.payloadType = kPayloadType;
401 rtp_header.sequenceNumber = 0x1234;
402 rtp_header.timestamp = 0x12345678;
403 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000404
Niels Möller05543682019-01-10 16:55:06 +0100405 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
406 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000407
408 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700409 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
Karl Wiberg45eb1352019-10-10 14:23:00 +0200410 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700411 rtp_header.timestamp += kPayloadLengthSamples;
412 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000413 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
414 }
415
416 // Insert one more packet and make sure the buffer got flushed. That is, it
417 // should only hold one single packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200418 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700419 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700420 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700421 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
422 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000423}
424
solenberg2779bab2016-11-17 04:45:19 -0800425TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100426 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800427}
solenberg99df6c02016-10-11 04:35:34 -0700428
solenberg2779bab2016-11-17 04:45:19 -0800429TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100430 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800431}
solenberg99df6c02016-10-11 04:35:34 -0700432
solenberg2779bab2016-11-17 04:45:19 -0800433TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100434 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800435}
solenberg99df6c02016-10-11 04:35:34 -0700436
solenberg2779bab2016-11-17 04:45:19 -0800437TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100438 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700439}
440
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000441// This test verifies that timestamps propagate from the incoming packets
442// through to the sync buffer and to the playout timestamp.
443TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000444 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000445 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700446 const size_t kPayloadLengthSamples =
447 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000448 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
449 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700450 RTPHeader rtp_header;
451 rtp_header.payloadType = kPayloadType;
452 rtp_header.sequenceNumber = 0x1234;
453 rtp_header.timestamp = 0x12345678;
454 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000455 rtp_header.numCSRCs = 3;
456 rtp_header.arrOfCSRCs[0] = 43;
457 rtp_header.arrOfCSRCs[1] = 65;
458 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000459
460 // This is a dummy decoder that produces as many output samples as the input
461 // has bytes. The output is an increasing series, starting at 1 for the first
462 // sample, and then increasing by 1 for each sample.
463 class CountingSamplesDecoder : public AudioDecoder {
464 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000465 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000466
467 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100468 int DecodeInternal(const uint8_t* encoded,
469 size_t encoded_len,
470 int /* sample_rate_hz */,
471 int16_t* decoded,
472 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000473 for (size_t i = 0; i < encoded_len; ++i) {
474 decoded[i] = next_value_++;
475 }
476 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200477 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000478 }
479
Karl Wiberg43766482015-08-27 15:22:11 +0200480 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000481
kwiberg347d3512016-06-16 01:59:09 -0700482 int SampleRateHz() const override { return kSampleRateHz; }
483
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000484 size_t Channels() const override { return 1; }
485
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000486 uint16_t next_value() const { return next_value_; }
487
488 private:
489 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000490 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000491
Niels Möllerb7180c02018-12-06 13:07:11 +0100492 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
493 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
494
495 UseNoMocks();
496 CreateInstance(decoder_factory);
497
498 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100499 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000500
501 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000502 clock_.AdvanceTimeMilliseconds(123456);
503 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200504 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000505
506 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700507 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800508 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700509 bool muted;
510 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
511 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800512 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
513 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800514 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000515
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000516 // Verify |output.packet_infos_|.
517 ASSERT_THAT(output.packet_infos_, SizeIs(1));
518 {
519 const auto& packet_info = output.packet_infos_[0];
520 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
521 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
522 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
523 EXPECT_FALSE(packet_info.audio_level().has_value());
524 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
525 }
526
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000527 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700528 EXPECT_EQ(kPayloadLengthSamples,
529 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000530
531 // The value of the last of the output samples is the same as the number of
532 // samples played from the decoded packet. Thus, this number + the RTP
533 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200534 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700535 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200536 absl::optional<uint32_t>(rtp_header.timestamp +
537 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700538 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000539
540 // Check the timestamp for the last value in the sync buffer. This should
541 // be one full frame length ahead of the RTP timestamp.
542 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
543 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700544 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000545 sync_buffer->end_timestamp());
546
547 // Check that the number of samples still to play from the sync buffer add
548 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800549 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700550 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800551 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000552}
553
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000554TEST_F(NetEqImplTest, ReorderedPacket) {
555 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100556 // Create a mock decoder object.
557 MockAudioDecoder mock_decoder;
558
559 CreateInstance(
560 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000561
562 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000563 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700564 const size_t kPayloadLengthSamples =
565 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000566 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
567 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700568 RTPHeader rtp_header;
569 rtp_header.payloadType = kPayloadType;
570 rtp_header.sequenceNumber = 0x1234;
571 rtp_header.timestamp = 0x12345678;
572 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000573 rtp_header.extension.hasAudioLevel = true;
574 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000575
Karl Wiberg43766482015-08-27 15:22:11 +0200576 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700577 EXPECT_CALL(mock_decoder, SampleRateHz())
578 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000579 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin034154b2016-04-27 06:11:50 -0700580 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200581 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000582 int16_t dummy_output[kPayloadLengthSamples] = {0};
583 // The below expectation will make the mock decoder write
584 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100585 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
586 kSampleRateHz, _, _))
587 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000588 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100589 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200590 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100591 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
592 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000593
594 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000595 clock_.AdvanceTimeMilliseconds(123456);
596 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200597 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000598
599 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700600 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800601 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700602 bool muted;
603 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800604 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
605 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800606 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000607
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000608 // Verify |output.packet_infos_|.
609 ASSERT_THAT(output.packet_infos_, SizeIs(1));
610 {
611 const auto& packet_info = output.packet_infos_[0];
612 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
613 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
614 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
615 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
616 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
617 }
618
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000619 // Insert two more packets. The first one is out of order, and is already too
620 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700621 rtp_header.sequenceNumber -= 1;
622 rtp_header.timestamp -= kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000623 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000624 payload[0] = 1;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000625 clock_.AdvanceTimeMilliseconds(1000);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200626 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700627 rtp_header.sequenceNumber += 2;
628 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000629 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000630 payload[0] = 2;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000631 clock_.AdvanceTimeMilliseconds(2000);
632 expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200633 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000634
635 // Expect only the second packet to be decoded (the one with "2" as the first
636 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100637 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
638 kSampleRateHz, _, _))
639 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000640 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100641 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200642 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000643
644 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700645 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800646 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
647 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800648 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000649
650 // Now check the packet buffer, and make sure it is empty, since the
651 // out-of-order packet should have been discarded.
652 EXPECT_TRUE(packet_buffer_->Empty());
653
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000654 // Verify |output.packet_infos_|. Expect to only see the second packet.
655 ASSERT_THAT(output.packet_infos_, SizeIs(1));
656 {
657 const auto& packet_info = output.packet_infos_[0];
658 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
659 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
660 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
661 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
662 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
663 }
664
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000665 EXPECT_CALL(mock_decoder, Die());
666}
667
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000668// This test verifies that NetEq can handle the situation where the first
669// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000670TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000671 UseNoMocks();
672 CreateInstance();
673
674 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000675 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700676 const size_t kPayloadLengthSamples =
677 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700678 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000679 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700680 RTPHeader rtp_header;
681 rtp_header.payloadType = kPayloadType;
682 rtp_header.sequenceNumber = 0x1234;
683 rtp_header.timestamp = 0x12345678;
684 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000685
686 // Insert one packet. Note that we have not registered any payload type, so
687 // this packet will be rejected.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200688 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000689
690 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700691 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800692 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700693 bool muted;
694 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800695 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
696 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
697 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800698 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000699 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000700
701 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100702 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
703 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000704
705 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700706 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700707 rtp_header.sequenceNumber++;
708 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200709 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000710 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
711 }
712
713 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700714 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700715 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800716 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
717 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
718 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800719 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000720 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000721 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000722 }
723}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000724
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200725// This test verifies that audio interruption is not logged for the initial
726// PLC period before the first packet is deocoded.
727// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
728TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
729 UseNoMocks();
730 CreateInstance();
731
732 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200733 const int kSampleRateHz = 8000;
734 const size_t kPayloadLengthSamples =
735 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
736 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
737 uint8_t payload[kPayloadLengthBytes] = {0};
738 RTPHeader rtp_header;
739 rtp_header.payloadType = kPayloadType;
740 rtp_header.sequenceNumber = 0x1234;
741 rtp_header.timestamp = 0x12345678;
742 rtp_header.ssrc = 0x87654321;
743
744 // Register the payload type.
745 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
746 SdpAudioFormat("l16", 8000, 1)));
747
748 // Pull audio several times. No packets have been inserted yet.
749 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
750 AudioFrame output;
751 bool muted;
752 for (int i = 0; i < 100; ++i) {
753 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
754 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
755 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
756 EXPECT_EQ(1u, output.num_channels_);
757 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000758 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200759 }
760
761 // Insert 10 packets.
762 for (size_t i = 0; i < 10; ++i) {
763 rtp_header.sequenceNumber++;
764 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200765 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200766 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
767 }
768
769 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
770 for (size_t i = 0; i < 3; ++i) {
771 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
772 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
773 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
774 EXPECT_EQ(1u, output.num_channels_);
775 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
776 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000777 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200778 }
779
780 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200781 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200782}
783
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000784// This test verifies that NetEq can handle comfort noise and enters/quits codec
785// internal CNG mode properly.
786TEST_F(NetEqImplTest, CodecInternalCng) {
787 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100788 // Create a mock decoder object.
789 MockAudioDecoder mock_decoder;
790 CreateInstance(
791 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000792
793 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000794 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700795 const size_t kPayloadLengthSamples =
796 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
797 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000798 uint8_t payload[kPayloadLengthBytes] = {0};
799 int16_t dummy_output[kPayloadLengthSamples] = {0};
800
henrik.lundin246ef3e2017-04-24 09:14:32 -0700801 RTPHeader rtp_header;
802 rtp_header.payloadType = kPayloadType;
803 rtp_header.sequenceNumber = 0x1234;
804 rtp_header.timestamp = 0x12345678;
805 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000806
Karl Wiberg43766482015-08-27 15:22:11 +0200807 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700808 EXPECT_CALL(mock_decoder, SampleRateHz())
809 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000810 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700811 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200812 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700813 // Packed duration when asking the decoder for more CNG data (without a new
814 // packet).
815 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200816 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000817
818 // Pointee(x) verifies that first byte of the payload equals x, this makes it
819 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100820 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
821 kSampleRateKhz * 1000, _, _))
822 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000823 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100824 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200825 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000826
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100827 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
828 kSampleRateKhz * 1000, _, _))
829 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000830 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100831 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200832 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000833
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100834 EXPECT_CALL(mock_decoder,
835 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
836 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000837 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100838 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200839 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000840
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100841 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
842 kSampleRateKhz * 1000, _, _))
843 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000844 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100845 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200846 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000847
Niels Möller50b66d52018-12-11 14:43:21 +0100848 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
849 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000850
851 // Insert one packet (decoder will return speech).
Karl Wiberg45eb1352019-10-10 14:23:00 +0200852 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000853
854 // Insert second packet (decoder will return CNG).
855 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700856 rtp_header.sequenceNumber++;
857 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200858 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000859
Peter Kastingdce40cf2015-08-24 14:52:23 -0700860 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800861 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800862 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200863 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
864 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
865 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000866 int expected_timestamp_increment[8] = {
867 -1, // will not be used.
868 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200869 -1,
870 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
871 -1,
872 -1,
873 50 * kSampleRateKhz,
874 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000875
henrik.lundin7a926812016-05-12 13:51:28 -0700876 bool muted;
877 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200878 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700879 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000880
henrik.lundin0d96ab72016-04-06 12:28:26 -0700881 // Lambda for verifying the timestamps.
882 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200883 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700884 if (expected_timestamp_increment[i] == -1) {
885 // Expect to get an empty timestamp value during CNG and PLC.
886 EXPECT_FALSE(ts) << "i = " << i;
887 } else {
888 ASSERT_TRUE(ts) << "i = " << i;
889 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
890 << "i = " << i;
891 last_timestamp = ts;
892 }
893 };
894
Peter Kastingdce40cf2015-08-24 14:52:23 -0700895 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800896 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
897 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800898 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700899 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700900 SCOPED_TRACE("");
901 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000902 }
903
904 // Insert third packet, which leaves a gap from last packet.
905 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700906 rtp_header.sequenceNumber += 2;
907 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200908 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000909
Peter Kastingdce40cf2015-08-24 14:52:23 -0700910 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800911 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
912 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800913 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700914 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700915 SCOPED_TRACE("");
916 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000917 }
918
919 // Now check the packet buffer, and make sure it is empty.
920 EXPECT_TRUE(packet_buffer_->Empty());
921
922 EXPECT_CALL(mock_decoder, Die());
923}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000924
925TEST_F(NetEqImplTest, UnsupportedDecoder) {
926 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100927 ::testing::NiceMock<MockAudioDecoder> decoder;
928
929 CreateInstance(
930 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700931 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800932 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000933
934 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000935 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000936
Peter Kastingdce40cf2015-08-24 14:52:23 -0700937 const size_t kPayloadLengthSamples =
938 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000939 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700940 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200941 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700942 RTPHeader rtp_header;
943 rtp_header.payloadType = kPayloadType;
944 rtp_header.sequenceNumber = 0x1234;
945 rtp_header.timestamp = 0x12345678;
946 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000947
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000948 const uint8_t kFirstPayloadValue = 1;
949 const uint8_t kSecondPayloadValue = 2;
950
ossu61a208b2016-09-20 01:38:00 -0700951 EXPECT_CALL(decoder,
952 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
953 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200954 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000955
ossu61a208b2016-09-20 01:38:00 -0700956 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000957 .Times(0);
958
ossu61a208b2016-09-20 01:38:00 -0700959 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
960 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000961 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700962 .WillOnce(DoAll(
963 SetArrayArgument<3>(dummy_output,
964 dummy_output + kPayloadLengthSamples * kChannels),
965 SetArgPointee<4>(AudioDecoder::kSpeech),
966 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000967
ossu61a208b2016-09-20 01:38:00 -0700968 EXPECT_CALL(decoder,
969 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
970 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200971 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700972
Jonas Olssona4d87372019-07-05 19:08:33 +0200973 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -0700974
Jonas Olssona4d87372019-07-05 19:08:33 +0200975 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000976
Niels Möllera1eb9c72018-12-07 15:24:42 +0100977 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
978 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000979
980 // Insert one packet.
981 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200982 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000983
984 // Insert another packet.
985 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700986 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000987 // The second timestamp needs to be at least 30 ms after the first to make
988 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700989 rtp_header.timestamp += 3 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200990 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000991
henrik.lundin6d8e0112016-03-04 10:34:21 -0800992 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700993 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800994 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200995 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700996 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800997 // Output size and number of channels should be correct.
998 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
999 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1000 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001001 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001002
henrik.lundin6d8e0112016-03-04 10:34:21 -08001003 // Second call to GetAudio will decode the packet that is ok. No errors are
1004 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -07001005 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001006 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1007 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001008 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -07001009
1010 // Die isn't called through NiceMock (since it's called by the
1011 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1012 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +00001013}
1014
henrik.lundin116c84e2015-08-27 13:14:48 -07001015// This test inserts packets until the buffer is flushed. After that, it asks
1016// NetEq for the network statistics. The purpose of the test is to make sure
1017// that even though the buffer size increment is negative (which it becomes when
1018// the packet causing a flush is inserted), the packet length stored in the
1019// decision logic remains valid.
1020TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1021 UseNoMocks();
1022 CreateInstance();
1023
1024 const size_t kPayloadLengthSamples = 80;
1025 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1026 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin116c84e2015-08-27 13:14:48 -07001027 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001028 RTPHeader rtp_header;
1029 rtp_header.payloadType = kPayloadType;
1030 rtp_header.sequenceNumber = 0x1234;
1031 rtp_header.timestamp = 0x12345678;
1032 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001033
Niels Möller05543682019-01-10 16:55:06 +01001034 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1035 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001036
1037 // Insert packets until the buffer flushes.
1038 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1039 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
Karl Wiberg45eb1352019-10-10 14:23:00 +02001040 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -07001041 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1042 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001043 }
1044 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1045
1046 // Ask for network statistics. This should not crash.
1047 NetEqNetworkStatistics stats;
1048 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1049}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001050
1051TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1052 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001053 // Create a mock decoder object.
1054 MockAudioDecoder mock_decoder;
1055
1056 CreateInstance(
1057 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001058
1059 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001060 const int kSampleRateHz = 8000;
1061 const size_t kPayloadLengthSamples =
1062 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1063 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1064 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001065 RTPHeader rtp_header;
1066 rtp_header.payloadType = kPayloadType;
1067 rtp_header.sequenceNumber = 0x1234;
1068 rtp_header.timestamp = 0x12345678;
1069 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001070
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001071 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001072 EXPECT_CALL(mock_decoder, SampleRateHz())
1073 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001074 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001075 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001076 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001077 int16_t dummy_output[kPayloadLengthSamples] = {0};
1078 // The below expectation will make the mock decoder write
1079 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1080 // speech. That is, the decoded length is 5 samples shorter than the expected.
1081 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001082 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001083 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001084 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001085 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001086 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001087 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001088 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1089 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001090
1091 // Insert one packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +02001092 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001093
1094 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1095
1096 // Pull audio once.
1097 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001098 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001099 bool muted;
1100 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001101 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1102 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001103 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001104 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001105
1106 EXPECT_CALL(mock_decoder, Die());
1107}
minyuel6d92bf52015-09-23 15:20:39 +02001108
1109// This test checks the behavior of NetEq when audio decoder fails.
1110TEST_F(NetEqImplTest, DecodingError) {
1111 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001112 // Create a mock decoder object.
1113 MockAudioDecoder mock_decoder;
1114
1115 CreateInstance(
1116 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001117
1118 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001119 const int kSampleRateHz = 8000;
1120 const int kDecoderErrorCode = -97; // Any negative number.
1121
1122 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1123 const size_t kFrameLengthSamples =
1124 static_cast<size_t>(5 * kSampleRateHz / 1000);
1125
1126 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1127
1128 uint8_t payload[kPayloadLengthBytes] = {0};
1129
henrik.lundin246ef3e2017-04-24 09:14:32 -07001130 RTPHeader rtp_header;
1131 rtp_header.payloadType = kPayloadType;
1132 rtp_header.sequenceNumber = 0x1234;
1133 rtp_header.timestamp = 0x12345678;
1134 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001135
minyuel6d92bf52015-09-23 15:20:39 +02001136 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001137 EXPECT_CALL(mock_decoder, SampleRateHz())
1138 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001139 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001140 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001141 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001142 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1143 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001144 int16_t dummy_output[kFrameLengthSamples] = {0};
1145
1146 {
1147 InSequence sequence; // Dummy variable.
1148 // Mock decoder works normally the first time.
1149 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001150 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001151 .Times(3)
1152 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001153 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001154 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001155 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001156 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001157 .RetiresOnSaturation();
1158
1159 // Then mock decoder fails. A common reason for failure can be buffer being
1160 // too short
1161 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001162 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001163 .WillOnce(Return(-1))
1164 .RetiresOnSaturation();
1165
1166 // Mock decoder finally returns to normal.
1167 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001168 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001169 .Times(2)
1170 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001171 DoAll(SetArrayArgument<3>(dummy_output,
1172 dummy_output + kFrameLengthSamples),
1173 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001174 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001175 }
1176
Niels Möllera1eb9c72018-12-07 15:24:42 +01001177 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1178 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001179
1180 // Insert packets.
1181 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001182 rtp_header.sequenceNumber += 1;
1183 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001184 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001185 }
1186
1187 // Pull audio.
1188 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001189 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001190 bool muted;
1191 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001192 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1193 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001194 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001195 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001196
1197 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001198 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001199 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1200 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001201 // We are not expecting anything for output.speech_type_, since an error was
1202 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001203
1204 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001205 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001206 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1207 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001208 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001209 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001210
1211 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001212 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001213 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1214 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001215 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001216 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001217
1218 EXPECT_CALL(mock_decoder, Die());
1219}
1220
1221// This test checks the behavior of NetEq when audio decoder fails during CNG.
1222TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1223 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001224
1225 // Create a mock decoder object.
1226 MockAudioDecoder mock_decoder;
1227 CreateInstance(
1228 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001229
1230 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001231 const int kSampleRateHz = 8000;
1232 const int kDecoderErrorCode = -97; // Any negative number.
1233
1234 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1235 const size_t kFrameLengthSamples =
1236 static_cast<size_t>(5 * kSampleRateHz / 1000);
1237
1238 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1239
1240 uint8_t payload[kPayloadLengthBytes] = {0};
1241
henrik.lundin246ef3e2017-04-24 09:14:32 -07001242 RTPHeader rtp_header;
1243 rtp_header.payloadType = kPayloadType;
1244 rtp_header.sequenceNumber = 0x1234;
1245 rtp_header.timestamp = 0x12345678;
1246 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001247
minyuel6d92bf52015-09-23 15:20:39 +02001248 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001249 EXPECT_CALL(mock_decoder, SampleRateHz())
1250 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001251 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001252 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001253 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001254 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001255 int16_t dummy_output[kFrameLengthSamples] = {0};
1256
1257 {
1258 InSequence sequence; // Dummy variable.
1259 // Mock decoder works normally the first 2 times.
1260 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001261 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001262 .Times(2)
1263 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001264 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001265 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001266 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001267 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001268 .RetiresOnSaturation();
1269
1270 // Then mock decoder fails. A common reason for failure can be buffer being
1271 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001272 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001273 .WillOnce(Return(-1))
1274 .RetiresOnSaturation();
1275
1276 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001277 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001278 .Times(2)
1279 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001280 DoAll(SetArrayArgument<3>(dummy_output,
1281 dummy_output + kFrameLengthSamples),
1282 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001283 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001284 }
1285
Niels Möller50b66d52018-12-11 14:43:21 +01001286 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1287 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001288
1289 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1290 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001291 rtp_header.sequenceNumber += 1;
1292 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001293 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001294 }
1295
1296 // Pull audio.
1297 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001298 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001299 bool muted;
1300 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001301 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1302 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001303 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001304
1305 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001306 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001307 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1308 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001309 // We are not expecting anything for output.speech_type_, since an error was
1310 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001311
1312 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001313 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001314 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1315 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001316 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001317
1318 EXPECT_CALL(mock_decoder, Die());
1319}
1320
henrik.lundind89814b2015-11-23 06:49:25 -08001321// Tests that the return value from last_output_sample_rate_hz() is equal to the
1322// configured inital sample rate.
1323TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1324 UseNoMocks();
1325 config_.sample_rate_hz = 48000;
1326 CreateInstance();
1327 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1328}
1329
henrik.lundined497212016-04-25 10:11:38 -07001330TEST_F(NetEqImplTest, TickTimerIncrement) {
1331 UseNoMocks();
1332 CreateInstance();
1333 ASSERT_TRUE(tick_timer_);
1334 EXPECT_EQ(0u, tick_timer_->ticks());
1335 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001336 bool muted;
1337 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001338 EXPECT_EQ(1u, tick_timer_->ticks());
1339}
1340
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001341TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1342 UseNoMocks();
1343 use_mock_delay_manager_ = true;
1344 CreateInstance();
1345
1346 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1347 .WillOnce(Return(true))
1348 .WillOnce(Return(false));
1349
1350 const int delay_ms = 200;
1351
1352 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1353 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1354}
1355
1356TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1357 UseNoMocks();
1358 use_mock_delay_manager_ = true;
1359 CreateInstance();
1360
1361 const int delay_ms = 200;
1362
1363 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1364 .WillOnce(Return(delay_ms));
1365
1366 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1367}
1368
henrik.lundin114c1b32017-04-26 07:47:32 -07001369TEST_F(NetEqImplTest, TargetDelayMs) {
1370 UseNoMocks();
1371 use_mock_delay_manager_ = true;
1372 CreateInstance();
1373 // Let the dummy target delay be 17 packets.
1374 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1375 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1376 .WillOnce(Return(kTargetLevelPacketsQ8));
1377 // Default packet size before any packet has been decoded is 30 ms, so we are
1378 // expecting 17 * 30 = 510 ms target delay.
1379 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1380}
1381
henrik.lundinb8c55b12017-05-10 07:38:01 -07001382TEST_F(NetEqImplTest, InsertEmptyPacket) {
1383 UseNoMocks();
1384 use_mock_delay_manager_ = true;
1385 CreateInstance();
1386
1387 RTPHeader rtp_header;
1388 rtp_header.payloadType = 17;
1389 rtp_header.sequenceNumber = 0x1234;
1390 rtp_header.timestamp = 0x12345678;
1391 rtp_header.ssrc = 0x87654321;
1392
1393 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1394 neteq_->InsertEmptyPacket(rtp_header);
1395}
1396
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001397TEST_F(NetEqImplTest, EnableRtxHandling) {
1398 UseNoMocks();
1399 use_mock_delay_manager_ = true;
1400 config_.enable_rtx_handling = true;
1401 CreateInstance();
1402 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1403 .Times(1)
1404 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1405
1406 const int kPayloadLengthSamples = 80;
1407 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1408 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001409 uint8_t payload[kPayloadLengthBytes] = {0};
1410 RTPHeader rtp_header;
1411 rtp_header.payloadType = kPayloadType;
1412 rtp_header.sequenceNumber = 0x1234;
1413 rtp_header.timestamp = 0x12345678;
1414 rtp_header.ssrc = 0x87654321;
1415
Niels Möller05543682019-01-10 16:55:06 +01001416 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1417 SdpAudioFormat("l16", 8000, 1)));
Karl Wiberg45eb1352019-10-10 14:23:00 +02001418 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001419 AudioFrame output;
1420 bool muted;
1421 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1422
1423 // Insert second packet that was sent before the first packet.
1424 rtp_header.sequenceNumber -= 1;
1425 rtp_header.timestamp -= kPayloadLengthSamples;
1426 EXPECT_CALL(*mock_delay_manager_,
1427 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
Karl Wiberg45eb1352019-10-10 14:23:00 +02001428 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001429}
1430
minyue5bd33972016-05-02 04:46:11 -07001431class Decoder120ms : public AudioDecoder {
1432 public:
kwiberg347d3512016-06-16 01:59:09 -07001433 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1434 : sample_rate_hz_(sample_rate_hz),
1435 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001436 speech_type_(speech_type) {}
1437
1438 int DecodeInternal(const uint8_t* encoded,
1439 size_t encoded_len,
1440 int sample_rate_hz,
1441 int16_t* decoded,
1442 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001443 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001444 size_t decoded_len =
1445 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1446 for (size_t i = 0; i < decoded_len; ++i) {
1447 decoded[i] = next_value_++;
1448 }
1449 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001450 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001451 }
1452
1453 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001454 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001455 size_t Channels() const override { return 2; }
1456
1457 private:
kwiberg347d3512016-06-16 01:59:09 -07001458 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001459 int16_t next_value_;
1460 SpeechType speech_type_;
1461};
1462
1463class NetEqImplTest120ms : public NetEqImplTest {
1464 protected:
1465 NetEqImplTest120ms() : NetEqImplTest() {}
1466 virtual ~NetEqImplTest120ms() {}
1467
1468 void CreateInstanceNoMocks() {
1469 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001470 CreateInstance(decoder_factory_);
1471 EXPECT_TRUE(neteq_->RegisterPayloadType(
1472 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001473 }
1474
1475 void CreateInstanceWithDelayManagerMock() {
1476 UseNoMocks();
1477 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001478 CreateInstance(decoder_factory_);
1479 EXPECT_TRUE(neteq_->RegisterPayloadType(
1480 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001481 }
1482
1483 uint32_t timestamp_diff_between_packets() const {
1484 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1485 }
1486
1487 uint32_t first_timestamp() const { return 10u; }
1488
1489 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001490 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001491 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001492 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1493 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001494 }
1495 }
1496
1497 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001498 RTPHeader rtp_header;
1499 rtp_header.payloadType = kPayloadType;
1500 rtp_header.sequenceNumber = sequence_number_;
1501 rtp_header.timestamp = timestamp;
1502 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001503 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1504 uint8_t payload[kPayloadLengthBytes] = {0};
Karl Wiberg45eb1352019-10-10 14:23:00 +02001505 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue5bd33972016-05-02 04:46:11 -07001506 sequence_number_++;
1507 }
1508
1509 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001510 const uint32_t sampling_freq = kSamplingFreq_;
1511 decoder_factory_ =
1512 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1513 [sampling_freq, speech_type]() {
1514 std::unique_ptr<AudioDecoder> decoder =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001515 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
Niels Möllera0f44302018-11-30 10:45:12 +01001516 RTC_CHECK_EQ(2, decoder->Channels());
1517 return decoder;
1518 });
minyue5bd33972016-05-02 04:46:11 -07001519 }
1520
Niels Möllera0f44302018-11-30 10:45:12 +01001521 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001522 AudioFrame output_;
1523 const uint32_t kPayloadType = 17;
1524 const uint32_t kSamplingFreq_ = 48000;
1525 uint16_t sequence_number_ = 1;
1526};
1527
minyue5bd33972016-05-02 04:46:11 -07001528TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001529 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001530 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001531
1532 InsertPacket(first_timestamp());
1533 GetFirstPacket();
1534
henrik.lundin7a926812016-05-12 13:51:28 -07001535 bool muted;
1536 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001537 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1538}
1539
1540TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001541 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001542 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001543
1544 InsertPacket(first_timestamp());
1545 GetFirstPacket();
1546
1547 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1548}
1549
1550TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001551 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001552 CreateInstanceWithDelayManagerMock();
1553
minyue5bd33972016-05-02 04:46:11 -07001554 InsertPacket(first_timestamp());
1555
1556 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001557 bool muted;
1558 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001559
1560 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1561
1562 // Delay manager reports a target level which should cause a Merge.
1563 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1564
henrik.lundin7a926812016-05-12 13:51:28 -07001565 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001566 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1567}
1568
1569TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001570 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001571 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001572
1573 InsertPacket(first_timestamp());
1574 GetFirstPacket();
1575
henrik.lundin7a926812016-05-12 13:51:28 -07001576 bool muted;
1577 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001578 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1579}
1580
1581TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001582 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001583 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001584
1585 InsertPacket(first_timestamp());
1586 GetFirstPacket();
1587 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1588
1589 // Delay manager report buffer limit which should cause a FastAccelerate.
1590 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1591 .Times(1)
1592 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1593
henrik.lundin7a926812016-05-12 13:51:28 -07001594 bool muted;
1595 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001596 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1597}
1598
1599TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001600 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001601 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001602
1603 InsertPacket(first_timestamp());
1604 GetFirstPacket();
1605
1606 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1607
1608 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1609 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1610 .Times(1)
1611 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1612
henrik.lundin7a926812016-05-12 13:51:28 -07001613 bool muted;
1614 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001615 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1616}
1617
1618TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001619 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001620 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001621
1622 InsertPacket(first_timestamp());
1623 GetFirstPacket();
1624
1625 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1626
1627 // Delay manager report buffer limit which should cause a Accelerate.
1628 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1629 .Times(1)
1630 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1631
henrik.lundin7a926812016-05-12 13:51:28 -07001632 bool muted;
1633 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001634 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1635}
1636
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001637} // namespace webrtc