blob: 025ed693d93ff40e07dd9d22de15866fcb23f5f2 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
kwiberg84be5112016-04-27 01:19:58 -070011#include <memory>
12
Niels Möllera0f44302018-11-30 10:45:12 +010013#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_coding/neteq/accelerate.h"
16#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010017#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/neteq/include/neteq.h"
19#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
20#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
21#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
22#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
23#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
24#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
25#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
26#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
27#include "modules/audio_coding/neteq/neteq_impl.h"
28#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010029#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_coding/neteq/sync_buffer.h"
31#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010032#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010033#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010034#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "test/gmock.h"
36#include "test/gtest.h"
37#include "test/mock_audio_decoder.h"
38#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010041using ::testing::AtLeast;
42using ::testing::DoAll;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043using ::testing::InSequence;
44using ::testing::Invoke;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000045using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010046using ::testing::Pointee;
47using ::testing::Return;
48using ::testing::ReturnNull;
49using ::testing::SetArgPointee;
50using ::testing::SetArrayArgument;
51using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052
53namespace webrtc {
54
55// This function is called when inserting a packet list into the mock packet
56// buffer. The purpose is to delete all inserted packets properly, to avoid
57// memory leaks in the test.
58int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070059 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060 return PacketBuffer::kOK;
61}
62
63class NetEqImplTest : public ::testing::Test {
64 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070065 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000066
Niels Möllera0f44302018-11-30 10:45:12 +010067 void CreateInstance(
68 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
69 ASSERT_TRUE(decoder_factory);
70 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070071
72 // Get a local pointer to NetEq's TickTimer object.
73 tick_timer_ = deps.tick_timer.get();
74
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000075 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070076 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
77 mock_buffer_level_filter_ = mock.get();
78 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000079 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070080 buffer_level_filter_ = deps.buffer_level_filter.get();
81
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000082 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070083 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
84 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000085 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
86 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070087 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000088 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070089 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000090
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070092 std::unique_ptr<MockDelayPeakDetector> mock(
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010093 new MockDelayPeakDetector(tick_timer_, config_.enable_rtx_handling));
henrik.lundin1d9061e2016-04-26 12:19:34 -070094 mock_delay_peak_detector_ = mock.get();
95 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
96 deps.delay_peak_detector = std::move(mock);
97 }
98 delay_peak_detector_ = deps.delay_peak_detector.get();
99
100 if (use_mock_delay_manager_) {
101 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100102 config_.max_packets_in_buffer, config_.min_delay_ms, 1020054733,
103 DelayManager::HistogramMode::INTER_ARRIVAL_TIME,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100104 config_.enable_rtx_handling, delay_peak_detector_, tick_timer_,
Jakob Ivarsson44507082019-03-05 16:59:03 +0100105 deps.stats.get(), absl::make_unique<Histogram>(50, 32745)));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700106 mock_delay_manager_ = mock.get();
107 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
108 deps.delay_manager = std::move(mock);
109 }
110 delay_manager_ = deps.delay_manager.get();
111
112 if (use_mock_dtmf_buffer_) {
113 std::unique_ptr<MockDtmfBuffer> mock(
114 new MockDtmfBuffer(config_.sample_rate_hz));
115 mock_dtmf_buffer_ = mock.get();
116 deps.dtmf_buffer = std::move(mock);
117 }
118 dtmf_buffer_ = deps.dtmf_buffer.get();
119
120 if (use_mock_dtmf_tone_generator_) {
121 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
122 mock_dtmf_tone_generator_ = mock.get();
123 deps.dtmf_tone_generator = std::move(mock);
124 }
125 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
126
127 if (use_mock_packet_buffer_) {
128 std::unique_ptr<MockPacketBuffer> mock(
129 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
130 mock_packet_buffer_ = mock.get();
131 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700132 }
133 packet_buffer_ = deps.packet_buffer.get();
134
135 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700136 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700137 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700138 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700139 }
ossua70695a2016-09-22 02:06:28 -0700140 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700141
142 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
143 new TimestampScaler(*deps.decoder_database.get()));
144
145 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000146 ASSERT_TRUE(neteq_ != NULL);
147 }
148
Niels Möllera0f44302018-11-30 10:45:12 +0100149 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
150
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000151 void UseNoMocks() {
152 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
153 use_mock_buffer_level_filter_ = false;
154 use_mock_decoder_database_ = false;
155 use_mock_delay_peak_detector_ = false;
156 use_mock_delay_manager_ = false;
157 use_mock_dtmf_buffer_ = false;
158 use_mock_dtmf_tone_generator_ = false;
159 use_mock_packet_buffer_ = false;
160 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000161 }
162
163 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000164 if (use_mock_buffer_level_filter_) {
165 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
166 }
167 if (use_mock_decoder_database_) {
168 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
169 }
170 if (use_mock_delay_manager_) {
171 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
172 }
173 if (use_mock_delay_peak_detector_) {
174 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
175 }
176 if (use_mock_dtmf_buffer_) {
177 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
178 }
179 if (use_mock_dtmf_tone_generator_) {
180 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
181 }
182 if (use_mock_packet_buffer_) {
183 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
184 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000185 }
186
Niels Möller05543682019-01-10 16:55:06 +0100187 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800188 const size_t kPayloadLength = 4;
189 const uint8_t kPayloadType = 110;
190 const uint32_t kReceiveTime = 17;
191 const int kSampleRateHz = 16000;
192 config_.sample_rate_hz = kSampleRateHz;
193 UseNoMocks();
194 CreateInstance();
195 // Event: 2, E bit, Volume: 17, Length: 4336.
196 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700197 RTPHeader rtp_header;
198 rtp_header.payloadType = kPayloadType;
199 rtp_header.sequenceNumber = 0x1234;
200 rtp_header.timestamp = 0x12345678;
201 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800202
Niels Möller05543682019-01-10 16:55:06 +0100203 EXPECT_TRUE(neteq_->RegisterPayloadType(
204 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800205
206 // Insert first packet.
207 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700208 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800209
210 // Pull audio once.
211 const size_t kMaxOutputSize =
212 static_cast<size_t>(10 * kSampleRateHz / 1000);
213 AudioFrame output;
214 bool muted;
215 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
216 ASSERT_FALSE(muted);
217 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
218 EXPECT_EQ(1u, output.num_channels_);
219 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
220
221 // Verify first 64 samples of actual output.
222 const std::vector<int16_t> kOutput({
223 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
224 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
225 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
226 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
227 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
228 -2534, -1163 });
229 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700230 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800231 }
232
henrik.lundin1d9061e2016-04-26 12:19:34 -0700233 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000234 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700235 TickTimer* tick_timer_ = nullptr;
236 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
237 BufferLevelFilter* buffer_level_filter_ = nullptr;
238 bool use_mock_buffer_level_filter_ = true;
239 MockDecoderDatabase* mock_decoder_database_ = nullptr;
240 DecoderDatabase* decoder_database_ = nullptr;
241 bool use_mock_decoder_database_ = true;
242 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
243 DelayPeakDetector* delay_peak_detector_ = nullptr;
244 bool use_mock_delay_peak_detector_ = true;
245 MockDelayManager* mock_delay_manager_ = nullptr;
246 DelayManager* delay_manager_ = nullptr;
247 bool use_mock_delay_manager_ = true;
248 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
249 DtmfBuffer* dtmf_buffer_ = nullptr;
250 bool use_mock_dtmf_buffer_ = true;
251 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
252 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
253 bool use_mock_dtmf_tone_generator_ = true;
254 MockPacketBuffer* mock_packet_buffer_ = nullptr;
255 PacketBuffer* packet_buffer_ = nullptr;
256 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700257 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
258 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700259 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000260};
261
262
263// This tests the interface class NetEq.
264// TODO(hlundin): Move to separate file?
265TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000266 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700267 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 delete neteq;
269}
270
kwiberg5adaf732016-10-04 09:33:27 -0700271TEST_F(NetEqImplTest, RegisterPayloadType) {
272 CreateInstance();
273 constexpr int rtp_payload_type = 0;
274 const SdpAudioFormat format("pcmu", 8000, 1);
275 EXPECT_CALL(*mock_decoder_database_,
276 RegisterPayload(rtp_payload_type, format));
277 neteq_->RegisterPayloadType(rtp_payload_type, format);
278}
279
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000281 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000283 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200285 // Check that kOK is returned when database returns kDecoderNotFound, because
286 // removing a payload type that was never registered is not an error.
287 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288}
289
kwiberg6b19b562016-09-20 04:02:25 -0700290TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
291 CreateInstance();
292 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
293 neteq_->RemoveAllPayloadTypes();
294}
295
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000296TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000297 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000298 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299 const uint8_t kPayloadType = 0;
300 const uint16_t kFirstSequenceNumber = 0x1234;
301 const uint32_t kFirstTimestamp = 0x12345678;
302 const uint32_t kSsrc = 0x87654321;
303 const uint32_t kFirstReceiveTime = 17;
304 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700305 RTPHeader rtp_header;
306 rtp_header.payloadType = kPayloadType;
307 rtp_header.sequenceNumber = kFirstSequenceNumber;
308 rtp_header.timestamp = kFirstTimestamp;
309 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700310 Packet fake_packet;
311 fake_packet.payload_type = kPayloadType;
312 fake_packet.sequence_number = kFirstSequenceNumber;
313 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314
kwibergc0f2dcf2016-05-31 06:28:03 -0700315 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
316 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100317 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800318 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200319 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800320 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700321 EXPECT_EQ("pcmu", format.name);
322
323 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
324 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
325 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
326 // BWE update function called with first packet.
327 EXPECT_CALL(*mock_decoder,
328 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
329 kFirstTimestamp, kFirstReceiveTime));
330 // BWE update function called with second packet.
331 EXPECT_CALL(
332 *mock_decoder,
333 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
334 kFirstTimestamp + 160, kFirstReceiveTime + 155));
335 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.
349 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200351 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000352 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100353 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
354 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
356 // index) is a pointer, and the variable pointed to is set to kPayloadType.
357 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
358 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700359 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000360 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700361 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362
363 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000364 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000365 .Times(1);
366
367 // Expectations for delay manager.
368 {
369 // All expectations within this block must be called in this specific order.
370 InSequence sequence; // Dummy variable.
371 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000372 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000373 .Times(2)
374 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000375 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000376 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000377 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000379 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
380 .WillOnce(Return(0));
381 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 .WillOnce(Return(0));
383 }
384
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700386 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387
388 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700389 rtp_header.timestamp += 160;
390 rtp_header.sequenceNumber += 1;
391 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392}
393
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000394TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
395 UseNoMocks();
396 CreateInstance();
397
398 const int kPayloadLengthSamples = 80;
399 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
400 const uint8_t kPayloadType = 17; // Just an arbitrary number.
401 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
402 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700403 RTPHeader rtp_header;
404 rtp_header.payloadType = kPayloadType;
405 rtp_header.sequenceNumber = 0x1234;
406 rtp_header.timestamp = 0x12345678;
407 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000408
Niels Möller05543682019-01-10 16:55:06 +0100409 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
410 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000411
412 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700413 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000414 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700415 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
416 rtp_header.timestamp += kPayloadLengthSamples;
417 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000418 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
419 }
420
421 // Insert one more packet and make sure the buffer got flushed. That is, it
422 // should only hold one single packet.
423 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700424 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700425 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700426 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700427 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
428 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000429}
430
solenberg2779bab2016-11-17 04:45:19 -0800431TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100432 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800433}
solenberg99df6c02016-10-11 04:35:34 -0700434
solenberg2779bab2016-11-17 04:45:19 -0800435TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100436 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800437}
solenberg99df6c02016-10-11 04:35:34 -0700438
solenberg2779bab2016-11-17 04:45:19 -0800439TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100440 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800441}
solenberg99df6c02016-10-11 04:35:34 -0700442
solenberg2779bab2016-11-17 04:45:19 -0800443TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100444 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700445}
446
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000447// This test verifies that timestamps propagate from the incoming packets
448// through to the sync buffer and to the playout timestamp.
449TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000450 const uint8_t kPayloadType = 17; // Just an arbitrary number.
451 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
452 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700453 const size_t kPayloadLengthSamples =
454 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000455 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
456 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700457 RTPHeader rtp_header;
458 rtp_header.payloadType = kPayloadType;
459 rtp_header.sequenceNumber = 0x1234;
460 rtp_header.timestamp = 0x12345678;
461 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000462
463 // This is a dummy decoder that produces as many output samples as the input
464 // has bytes. The output is an increasing series, starting at 1 for the first
465 // sample, and then increasing by 1 for each sample.
466 class CountingSamplesDecoder : public AudioDecoder {
467 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000468 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000469
470 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100471 int DecodeInternal(const uint8_t* encoded,
472 size_t encoded_len,
473 int /* sample_rate_hz */,
474 int16_t* decoded,
475 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000476 for (size_t i = 0; i < encoded_len; ++i) {
477 decoded[i] = next_value_++;
478 }
479 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200480 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000481 }
482
Karl Wiberg43766482015-08-27 15:22:11 +0200483 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000484
kwiberg347d3512016-06-16 01:59:09 -0700485 int SampleRateHz() const override { return kSampleRateHz; }
486
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000487 size_t Channels() const override { return 1; }
488
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000489 uint16_t next_value() const { return next_value_; }
490
491 private:
492 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000493 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000494
Niels Möllerb7180c02018-12-06 13:07:11 +0100495 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
496 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
497
498 UseNoMocks();
499 CreateInstance(decoder_factory);
500
501 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100502 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000503
504 // Insert one packet.
505 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700506 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000507
508 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700509 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800510 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700511 bool muted;
512 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
513 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800514 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
515 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800516 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000517
518 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700519 EXPECT_EQ(kPayloadLengthSamples,
520 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000521
522 // The value of the last of the output samples is the same as the number of
523 // samples played from the decoded packet. Thus, this number + the RTP
524 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200525 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700526 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200527 absl::optional<uint32_t>(rtp_header.timestamp +
528 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700529 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000530
531 // Check the timestamp for the last value in the sync buffer. This should
532 // be one full frame length ahead of the RTP timestamp.
533 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
534 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700535 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000536 sync_buffer->end_timestamp());
537
538 // Check that the number of samples still to play from the sync buffer add
539 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800540 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700541 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800542 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000543}
544
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000545TEST_F(NetEqImplTest, ReorderedPacket) {
546 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100547 // Create a mock decoder object.
548 MockAudioDecoder mock_decoder;
549
550 CreateInstance(
551 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000552
553 const uint8_t kPayloadType = 17; // Just an arbitrary number.
554 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
555 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700556 const size_t kPayloadLengthSamples =
557 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000558 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
559 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700560 RTPHeader rtp_header;
561 rtp_header.payloadType = kPayloadType;
562 rtp_header.sequenceNumber = 0x1234;
563 rtp_header.timestamp = 0x12345678;
564 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000565
Karl Wiberg43766482015-08-27 15:22:11 +0200566 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700567 EXPECT_CALL(mock_decoder, SampleRateHz())
568 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000569 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000570 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
571 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700572 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200573 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000574 int16_t dummy_output[kPayloadLengthSamples] = {0};
575 // The below expectation will make the mock decoder write
576 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100577 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
578 kSampleRateHz, _, _))
579 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000580 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100581 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200582 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100583 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
584 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000585
586 // Insert one packet.
587 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700588 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000589
590 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700591 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800592 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700593 bool muted;
594 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800595 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
596 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800597 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000598
599 // Insert two more packets. The first one is out of order, and is already too
600 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700601 rtp_header.sequenceNumber -= 1;
602 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000603 payload[0] = 1;
604 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700605 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
606 rtp_header.sequenceNumber += 2;
607 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000608 payload[0] = 2;
609 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700610 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000611
612 // Expect only the second packet to be decoded (the one with "2" as the first
613 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100614 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
615 kSampleRateHz, _, _))
616 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000617 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100618 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200619 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000620
621 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700622 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800623 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
624 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800625 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000626
627 // Now check the packet buffer, and make sure it is empty, since the
628 // out-of-order packet should have been discarded.
629 EXPECT_TRUE(packet_buffer_->Empty());
630
631 EXPECT_CALL(mock_decoder, Die());
632}
633
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000634// This test verifies that NetEq can handle the situation where the first
635// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000636TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000637 UseNoMocks();
638 CreateInstance();
639
640 const uint8_t kPayloadType = 17; // Just an arbitrary number.
641 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
642 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700643 const size_t kPayloadLengthSamples =
644 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700645 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000646 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700647 RTPHeader rtp_header;
648 rtp_header.payloadType = kPayloadType;
649 rtp_header.sequenceNumber = 0x1234;
650 rtp_header.timestamp = 0x12345678;
651 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000652
653 // Insert one packet. Note that we have not registered any payload type, so
654 // this packet will be rejected.
655 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700656 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000657
658 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700659 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800660 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700661 bool muted;
662 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800663 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
664 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
665 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800666 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000667
668 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100669 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
670 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000671
672 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700673 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700674 rtp_header.sequenceNumber++;
675 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000676 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700677 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000678 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
679 }
680
681 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700682 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700683 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800684 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
685 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
686 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800687 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000688 << "NetEq did not decode the packets as expected.";
689 }
690}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000691
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200692// This test verifies that audio interruption is not logged for the initial
693// PLC period before the first packet is deocoded.
694// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
695TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
696 UseNoMocks();
697 CreateInstance();
698
699 const uint8_t kPayloadType = 17; // Just an arbitrary number.
700 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
701 const int kSampleRateHz = 8000;
702 const size_t kPayloadLengthSamples =
703 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
704 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
705 uint8_t payload[kPayloadLengthBytes] = {0};
706 RTPHeader rtp_header;
707 rtp_header.payloadType = kPayloadType;
708 rtp_header.sequenceNumber = 0x1234;
709 rtp_header.timestamp = 0x12345678;
710 rtp_header.ssrc = 0x87654321;
711
712 // Register the payload type.
713 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
714 SdpAudioFormat("l16", 8000, 1)));
715
716 // Pull audio several times. No packets have been inserted yet.
717 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
718 AudioFrame output;
719 bool muted;
720 for (int i = 0; i < 100; ++i) {
721 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
722 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
723 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
724 EXPECT_EQ(1u, output.num_channels_);
725 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
726 }
727
728 // Insert 10 packets.
729 for (size_t i = 0; i < 10; ++i) {
730 rtp_header.sequenceNumber++;
731 rtp_header.timestamp += kPayloadLengthSamples;
732 EXPECT_EQ(NetEq::kOK,
733 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
734 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
735 }
736
737 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
738 for (size_t i = 0; i < 3; ++i) {
739 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
740 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
741 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
742 EXPECT_EQ(1u, output.num_channels_);
743 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
744 << "NetEq did not decode the packets as expected.";
745 }
746
747 auto lifetime_stats = neteq_->GetLifetimeStatistics();
748 EXPECT_EQ(0u, lifetime_stats.interruption_count);
749}
750
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000751// This test verifies that NetEq can handle comfort noise and enters/quits codec
752// internal CNG mode properly.
753TEST_F(NetEqImplTest, CodecInternalCng) {
754 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100755 // Create a mock decoder object.
756 MockAudioDecoder mock_decoder;
757 CreateInstance(
758 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000759
760 const uint8_t kPayloadType = 17; // Just an arbitrary number.
761 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
762 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700763 const size_t kPayloadLengthSamples =
764 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
765 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000766 uint8_t payload[kPayloadLengthBytes] = {0};
767 int16_t dummy_output[kPayloadLengthSamples] = {0};
768
henrik.lundin246ef3e2017-04-24 09:14:32 -0700769 RTPHeader rtp_header;
770 rtp_header.payloadType = kPayloadType;
771 rtp_header.sequenceNumber = 0x1234;
772 rtp_header.timestamp = 0x12345678;
773 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000774
Karl Wiberg43766482015-08-27 15:22:11 +0200775 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700776 EXPECT_CALL(mock_decoder, SampleRateHz())
777 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000778 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000779 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
780 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700781 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200782 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700783 // Packed duration when asking the decoder for more CNG data (without a new
784 // packet).
785 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200786 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000787
788 // Pointee(x) verifies that first byte of the payload equals x, this makes it
789 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100790 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
791 kSampleRateKhz * 1000, _, _))
792 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000793 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100794 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200795 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000796
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100797 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
798 kSampleRateKhz * 1000, _, _))
799 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000800 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100801 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200802 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000803
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100804 EXPECT_CALL(mock_decoder,
805 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
806 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000807 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100808 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200809 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000810
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100811 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
812 kSampleRateKhz * 1000, _, _))
813 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000814 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100815 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200816 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000817
Niels Möller50b66d52018-12-11 14:43:21 +0100818 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
819 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000820
821 // Insert one packet (decoder will return speech).
822 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700823 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000824
825 // Insert second packet (decoder will return CNG).
826 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700827 rtp_header.sequenceNumber++;
828 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000829 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700830 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000831
Peter Kastingdce40cf2015-08-24 14:52:23 -0700832 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800833 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800834 AudioFrame::SpeechType expected_type[8] = {
835 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
836 AudioFrame::kCNG, AudioFrame::kCNG,
837 AudioFrame::kCNG, AudioFrame::kCNG,
838 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000839 };
840 int expected_timestamp_increment[8] = {
841 -1, // will not be used.
842 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700843 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
844 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000845 50 * kSampleRateKhz, 10 * kSampleRateKhz
846 };
847
henrik.lundin7a926812016-05-12 13:51:28 -0700848 bool muted;
849 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200850 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700851 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000852
henrik.lundin0d96ab72016-04-06 12:28:26 -0700853 // Lambda for verifying the timestamps.
854 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200855 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700856 if (expected_timestamp_increment[i] == -1) {
857 // Expect to get an empty timestamp value during CNG and PLC.
858 EXPECT_FALSE(ts) << "i = " << i;
859 } else {
860 ASSERT_TRUE(ts) << "i = " << i;
861 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
862 << "i = " << i;
863 last_timestamp = ts;
864 }
865 };
866
Peter Kastingdce40cf2015-08-24 14:52:23 -0700867 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800868 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
869 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800870 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700871 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700872 SCOPED_TRACE("");
873 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000874 }
875
876 // Insert third packet, which leaves a gap from last packet.
877 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700878 rtp_header.sequenceNumber += 2;
879 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000880 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700881 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000882
Peter Kastingdce40cf2015-08-24 14:52:23 -0700883 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800884 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
885 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800886 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700887 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700888 SCOPED_TRACE("");
889 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000890 }
891
892 // Now check the packet buffer, and make sure it is empty.
893 EXPECT_TRUE(packet_buffer_->Empty());
894
895 EXPECT_CALL(mock_decoder, Die());
896}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000897
898TEST_F(NetEqImplTest, UnsupportedDecoder) {
899 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100900 ::testing::NiceMock<MockAudioDecoder> decoder;
901
902 CreateInstance(
903 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700904 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800905 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000906
907 const uint8_t kPayloadType = 17; // Just an arbitrary number.
908 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
909 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000910
Peter Kastingdce40cf2015-08-24 14:52:23 -0700911 const size_t kPayloadLengthSamples =
912 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000913 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700914 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200915 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700916 RTPHeader rtp_header;
917 rtp_header.payloadType = kPayloadType;
918 rtp_header.sequenceNumber = 0x1234;
919 rtp_header.timestamp = 0x12345678;
920 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000921
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000922 const uint8_t kFirstPayloadValue = 1;
923 const uint8_t kSecondPayloadValue = 2;
924
ossu61a208b2016-09-20 01:38:00 -0700925 EXPECT_CALL(decoder,
926 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
927 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200928 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000929
ossu61a208b2016-09-20 01:38:00 -0700930 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000931 .Times(0);
932
ossu61a208b2016-09-20 01:38:00 -0700933 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
934 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000935 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700936 .WillOnce(DoAll(
937 SetArrayArgument<3>(dummy_output,
938 dummy_output + kPayloadLengthSamples * kChannels),
939 SetArgPointee<4>(AudioDecoder::kSpeech),
940 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000941
ossu61a208b2016-09-20 01:38:00 -0700942 EXPECT_CALL(decoder,
943 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
944 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200945 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700946
947 EXPECT_CALL(decoder, SampleRateHz())
948 .WillRepeatedly(Return(kSampleRateHz));
949
950 EXPECT_CALL(decoder, Channels())
951 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000952
Niels Möllera1eb9c72018-12-07 15:24:42 +0100953 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
954 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000955
956 // Insert one packet.
957 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
958 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700959 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000960
961 // Insert another packet.
962 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700963 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000964 // The second timestamp needs to be at least 30 ms after the first to make
965 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700966 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000967 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700968 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000969
henrik.lundin6d8e0112016-03-04 10:34:21 -0800970 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700971 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800972 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200973 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700974 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800975 // Output size and number of channels should be correct.
976 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
977 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
978 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000979
henrik.lundin6d8e0112016-03-04 10:34:21 -0800980 // Second call to GetAudio will decode the packet that is ok. No errors are
981 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700982 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800983 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
984 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700985
986 // Die isn't called through NiceMock (since it's called by the
987 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
988 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000989}
990
henrik.lundin116c84e2015-08-27 13:14:48 -0700991// This test inserts packets until the buffer is flushed. After that, it asks
992// NetEq for the network statistics. The purpose of the test is to make sure
993// that even though the buffer size increment is negative (which it becomes when
994// the packet causing a flush is inserted), the packet length stored in the
995// decision logic remains valid.
996TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
997 UseNoMocks();
998 CreateInstance();
999
1000 const size_t kPayloadLengthSamples = 80;
1001 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1002 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1003 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1004 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001005 RTPHeader rtp_header;
1006 rtp_header.payloadType = kPayloadType;
1007 rtp_header.sequenceNumber = 0x1234;
1008 rtp_header.timestamp = 0x12345678;
1009 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001010
Niels Möller05543682019-01-10 16:55:06 +01001011 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1012 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001013
1014 // Insert packets until the buffer flushes.
1015 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1016 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1017 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001018 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1019 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1020 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001021 }
1022 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1023
1024 // Ask for network statistics. This should not crash.
1025 NetEqNetworkStatistics stats;
1026 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1027}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001028
1029TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1030 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001031 // Create a mock decoder object.
1032 MockAudioDecoder mock_decoder;
1033
1034 CreateInstance(
1035 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001036
1037 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1038 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1039 const int kSampleRateHz = 8000;
1040 const size_t kPayloadLengthSamples =
1041 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1042 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1043 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001044 RTPHeader rtp_header;
1045 rtp_header.payloadType = kPayloadType;
1046 rtp_header.sequenceNumber = 0x1234;
1047 rtp_header.timestamp = 0x12345678;
1048 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001049
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001050 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001051 EXPECT_CALL(mock_decoder, SampleRateHz())
1052 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001053 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1054 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1055 .WillRepeatedly(Return(0));
1056 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001057 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001058 int16_t dummy_output[kPayloadLengthSamples] = {0};
1059 // The below expectation will make the mock decoder write
1060 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1061 // speech. That is, the decoded length is 5 samples shorter than the expected.
1062 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001063 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001064 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001065 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001066 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001067 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001068 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001069 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1070 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001071
1072 // Insert one packet.
1073 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001074 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001075
1076 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1077
1078 // Pull audio once.
1079 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001080 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001081 bool muted;
1082 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001083 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1084 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001085 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001086
1087 EXPECT_CALL(mock_decoder, Die());
1088}
minyuel6d92bf52015-09-23 15:20:39 +02001089
1090// This test checks the behavior of NetEq when audio decoder fails.
1091TEST_F(NetEqImplTest, DecodingError) {
1092 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001093 // Create a mock decoder object.
1094 MockAudioDecoder mock_decoder;
1095
1096 CreateInstance(
1097 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001098
1099 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1100 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1101 const int kSampleRateHz = 8000;
1102 const int kDecoderErrorCode = -97; // Any negative number.
1103
1104 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1105 const size_t kFrameLengthSamples =
1106 static_cast<size_t>(5 * kSampleRateHz / 1000);
1107
1108 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1109
1110 uint8_t payload[kPayloadLengthBytes] = {0};
1111
henrik.lundin246ef3e2017-04-24 09:14:32 -07001112 RTPHeader rtp_header;
1113 rtp_header.payloadType = kPayloadType;
1114 rtp_header.sequenceNumber = 0x1234;
1115 rtp_header.timestamp = 0x12345678;
1116 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001117
minyuel6d92bf52015-09-23 15:20:39 +02001118 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001119 EXPECT_CALL(mock_decoder, SampleRateHz())
1120 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001121 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1122 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1123 .WillRepeatedly(Return(0));
1124 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001125 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001126 EXPECT_CALL(mock_decoder, ErrorCode())
1127 .WillOnce(Return(kDecoderErrorCode));
1128 EXPECT_CALL(mock_decoder, HasDecodePlc())
1129 .WillOnce(Return(false));
1130 int16_t dummy_output[kFrameLengthSamples] = {0};
1131
1132 {
1133 InSequence sequence; // Dummy variable.
1134 // Mock decoder works normally the first time.
1135 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001136 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001137 .Times(3)
1138 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001139 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001140 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001141 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001142 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001143 .RetiresOnSaturation();
1144
1145 // Then mock decoder fails. A common reason for failure can be buffer being
1146 // too short
1147 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001148 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001149 .WillOnce(Return(-1))
1150 .RetiresOnSaturation();
1151
1152 // Mock decoder finally returns to normal.
1153 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001154 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001155 .Times(2)
1156 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001157 DoAll(SetArrayArgument<3>(dummy_output,
1158 dummy_output + kFrameLengthSamples),
1159 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001160 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001161 }
1162
Niels Möllera1eb9c72018-12-07 15:24:42 +01001163 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1164 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001165
1166 // Insert packets.
1167 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001168 rtp_header.sequenceNumber += 1;
1169 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001170 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001171 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001172 }
1173
1174 // Pull audio.
1175 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001176 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001177 bool muted;
1178 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001179 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1180 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001181 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001182
1183 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001184 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001185 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1186 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001187 // We are not expecting anything for output.speech_type_, since an error was
1188 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001189
1190 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001191 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::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001195
1196 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001197 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001198 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1199 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001200 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001201
1202 EXPECT_CALL(mock_decoder, Die());
1203}
1204
1205// This test checks the behavior of NetEq when audio decoder fails during CNG.
1206TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1207 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001208
1209 // Create a mock decoder object.
1210 MockAudioDecoder mock_decoder;
1211 CreateInstance(
1212 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001213
1214 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1215 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1216 const int kSampleRateHz = 8000;
1217 const int kDecoderErrorCode = -97; // Any negative number.
1218
1219 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1220 const size_t kFrameLengthSamples =
1221 static_cast<size_t>(5 * kSampleRateHz / 1000);
1222
1223 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1224
1225 uint8_t payload[kPayloadLengthBytes] = {0};
1226
henrik.lundin246ef3e2017-04-24 09:14:32 -07001227 RTPHeader rtp_header;
1228 rtp_header.payloadType = kPayloadType;
1229 rtp_header.sequenceNumber = 0x1234;
1230 rtp_header.timestamp = 0x12345678;
1231 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001232
minyuel6d92bf52015-09-23 15:20:39 +02001233 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001234 EXPECT_CALL(mock_decoder, SampleRateHz())
1235 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001236 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1237 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1238 .WillRepeatedly(Return(0));
1239 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001240 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001241 EXPECT_CALL(mock_decoder, ErrorCode())
1242 .WillOnce(Return(kDecoderErrorCode));
1243 int16_t dummy_output[kFrameLengthSamples] = {0};
1244
1245 {
1246 InSequence sequence; // Dummy variable.
1247 // Mock decoder works normally the first 2 times.
1248 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001249 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001250 .Times(2)
1251 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001252 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001253 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001254 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001255 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001256 .RetiresOnSaturation();
1257
1258 // Then mock decoder fails. A common reason for failure can be buffer being
1259 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001260 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001261 .WillOnce(Return(-1))
1262 .RetiresOnSaturation();
1263
1264 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001265 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001266 .Times(2)
1267 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001268 DoAll(SetArrayArgument<3>(dummy_output,
1269 dummy_output + kFrameLengthSamples),
1270 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001271 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001272 }
1273
Niels Möller50b66d52018-12-11 14:43:21 +01001274 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1275 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001276
1277 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1278 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001279 rtp_header.sequenceNumber += 1;
1280 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001281 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001282 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001283 }
1284
1285 // Pull audio.
1286 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001287 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001288 bool muted;
1289 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001290 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1291 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001292 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001293
1294 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001295 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001296 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1297 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001298 // We are not expecting anything for output.speech_type_, since an error was
1299 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001300
1301 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001302 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001303 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1304 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001305 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001306
1307 EXPECT_CALL(mock_decoder, Die());
1308}
1309
henrik.lundind89814b2015-11-23 06:49:25 -08001310// Tests that the return value from last_output_sample_rate_hz() is equal to the
1311// configured inital sample rate.
1312TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1313 UseNoMocks();
1314 config_.sample_rate_hz = 48000;
1315 CreateInstance();
1316 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1317}
1318
henrik.lundined497212016-04-25 10:11:38 -07001319TEST_F(NetEqImplTest, TickTimerIncrement) {
1320 UseNoMocks();
1321 CreateInstance();
1322 ASSERT_TRUE(tick_timer_);
1323 EXPECT_EQ(0u, tick_timer_->ticks());
1324 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001325 bool muted;
1326 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001327 EXPECT_EQ(1u, tick_timer_->ticks());
1328}
1329
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001330TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1331 UseNoMocks();
1332 use_mock_delay_manager_ = true;
1333 CreateInstance();
1334
1335 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1336 .WillOnce(Return(true))
1337 .WillOnce(Return(false));
1338
1339 const int delay_ms = 200;
1340
1341 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1342 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1343}
1344
1345TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1346 UseNoMocks();
1347 use_mock_delay_manager_ = true;
1348 CreateInstance();
1349
1350 const int delay_ms = 200;
1351
1352 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1353 .WillOnce(Return(delay_ms));
1354
1355 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1356}
1357
henrik.lundin114c1b32017-04-26 07:47:32 -07001358TEST_F(NetEqImplTest, TargetDelayMs) {
1359 UseNoMocks();
1360 use_mock_delay_manager_ = true;
1361 CreateInstance();
1362 // Let the dummy target delay be 17 packets.
1363 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1364 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1365 .WillOnce(Return(kTargetLevelPacketsQ8));
1366 // Default packet size before any packet has been decoded is 30 ms, so we are
1367 // expecting 17 * 30 = 510 ms target delay.
1368 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1369}
1370
henrik.lundinb8c55b12017-05-10 07:38:01 -07001371TEST_F(NetEqImplTest, InsertEmptyPacket) {
1372 UseNoMocks();
1373 use_mock_delay_manager_ = true;
1374 CreateInstance();
1375
1376 RTPHeader rtp_header;
1377 rtp_header.payloadType = 17;
1378 rtp_header.sequenceNumber = 0x1234;
1379 rtp_header.timestamp = 0x12345678;
1380 rtp_header.ssrc = 0x87654321;
1381
1382 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1383 neteq_->InsertEmptyPacket(rtp_header);
1384}
1385
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001386TEST_F(NetEqImplTest, EnableRtxHandling) {
1387 UseNoMocks();
1388 use_mock_delay_manager_ = true;
1389 config_.enable_rtx_handling = true;
1390 CreateInstance();
1391 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1392 .Times(1)
1393 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1394
1395 const int kPayloadLengthSamples = 80;
1396 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1397 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1398 const uint32_t kReceiveTime = 17;
1399 uint8_t payload[kPayloadLengthBytes] = {0};
1400 RTPHeader rtp_header;
1401 rtp_header.payloadType = kPayloadType;
1402 rtp_header.sequenceNumber = 0x1234;
1403 rtp_header.timestamp = 0x12345678;
1404 rtp_header.ssrc = 0x87654321;
1405
Niels Möller05543682019-01-10 16:55:06 +01001406 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1407 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001408 EXPECT_EQ(NetEq::kOK,
1409 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1410 AudioFrame output;
1411 bool muted;
1412 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1413
1414 // Insert second packet that was sent before the first packet.
1415 rtp_header.sequenceNumber -= 1;
1416 rtp_header.timestamp -= kPayloadLengthSamples;
1417 EXPECT_CALL(*mock_delay_manager_,
1418 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1419 EXPECT_EQ(NetEq::kOK,
1420 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1421}
1422
minyue5bd33972016-05-02 04:46:11 -07001423class Decoder120ms : public AudioDecoder {
1424 public:
kwiberg347d3512016-06-16 01:59:09 -07001425 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1426 : sample_rate_hz_(sample_rate_hz),
1427 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001428 speech_type_(speech_type) {}
1429
1430 int DecodeInternal(const uint8_t* encoded,
1431 size_t encoded_len,
1432 int sample_rate_hz,
1433 int16_t* decoded,
1434 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001435 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001436 size_t decoded_len =
1437 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1438 for (size_t i = 0; i < decoded_len; ++i) {
1439 decoded[i] = next_value_++;
1440 }
1441 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001442 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001443 }
1444
1445 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001446 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001447 size_t Channels() const override { return 2; }
1448
1449 private:
kwiberg347d3512016-06-16 01:59:09 -07001450 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001451 int16_t next_value_;
1452 SpeechType speech_type_;
1453};
1454
1455class NetEqImplTest120ms : public NetEqImplTest {
1456 protected:
1457 NetEqImplTest120ms() : NetEqImplTest() {}
1458 virtual ~NetEqImplTest120ms() {}
1459
1460 void CreateInstanceNoMocks() {
1461 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001462 CreateInstance(decoder_factory_);
1463 EXPECT_TRUE(neteq_->RegisterPayloadType(
1464 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001465 }
1466
1467 void CreateInstanceWithDelayManagerMock() {
1468 UseNoMocks();
1469 use_mock_delay_manager_ = true;
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 uint32_t timestamp_diff_between_packets() const {
1476 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1477 }
1478
1479 uint32_t first_timestamp() const { return 10u; }
1480
1481 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001482 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001483 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001484 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1485 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001486 }
1487 }
1488
1489 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001490 RTPHeader rtp_header;
1491 rtp_header.payloadType = kPayloadType;
1492 rtp_header.sequenceNumber = sequence_number_;
1493 rtp_header.timestamp = timestamp;
1494 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001495 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1496 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001497 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001498 sequence_number_++;
1499 }
1500
1501 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001502 const uint32_t sampling_freq = kSamplingFreq_;
1503 decoder_factory_ =
1504 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1505 [sampling_freq, speech_type]() {
1506 std::unique_ptr<AudioDecoder> decoder =
1507 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1508 RTC_CHECK_EQ(2, decoder->Channels());
1509 return decoder;
1510 });
minyue5bd33972016-05-02 04:46:11 -07001511 }
1512
Niels Möllera0f44302018-11-30 10:45:12 +01001513 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001514 AudioFrame output_;
1515 const uint32_t kPayloadType = 17;
1516 const uint32_t kSamplingFreq_ = 48000;
1517 uint16_t sequence_number_ = 1;
1518};
1519
minyue5bd33972016-05-02 04:46:11 -07001520TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001521 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001522 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001523
1524 InsertPacket(first_timestamp());
1525 GetFirstPacket();
1526
henrik.lundin7a926812016-05-12 13:51:28 -07001527 bool muted;
1528 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001529 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1530}
1531
1532TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001533 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001534 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001535
1536 InsertPacket(first_timestamp());
1537 GetFirstPacket();
1538
1539 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1540}
1541
1542TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001543 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001544 CreateInstanceWithDelayManagerMock();
1545
minyue5bd33972016-05-02 04:46:11 -07001546 InsertPacket(first_timestamp());
1547
1548 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001549 bool muted;
1550 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001551
1552 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1553
1554 // Delay manager reports a target level which should cause a Merge.
1555 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1556
henrik.lundin7a926812016-05-12 13:51:28 -07001557 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001558 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1559}
1560
1561TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001562 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001563 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001564
1565 InsertPacket(first_timestamp());
1566 GetFirstPacket();
1567
henrik.lundin7a926812016-05-12 13:51:28 -07001568 bool muted;
1569 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001570 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1571}
1572
1573TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001574 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001575 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001576
1577 InsertPacket(first_timestamp());
1578 GetFirstPacket();
1579 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1580
1581 // Delay manager report buffer limit which should cause a FastAccelerate.
1582 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1583 .Times(1)
1584 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1585
henrik.lundin7a926812016-05-12 13:51:28 -07001586 bool muted;
1587 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001588 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1589}
1590
1591TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001592 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001593 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001594
1595 InsertPacket(first_timestamp());
1596 GetFirstPacket();
1597
1598 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1599
1600 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1601 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1602 .Times(1)
1603 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1604
henrik.lundin7a926812016-05-12 13:51:28 -07001605 bool muted;
1606 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001607 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1608}
1609
1610TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001611 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001612 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001613
1614 InsertPacket(first_timestamp());
1615 GetFirstPacket();
1616
1617 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1618
1619 // Delay manager report buffer limit which should cause a Accelerate.
1620 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1621 .Times(1)
1622 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1623
henrik.lundin7a926812016-05-12 13:51:28 -07001624 bool muted;
1625 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001626 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1627}
1628
minyuel6d92bf52015-09-23 15:20:39 +02001629}// namespace webrtc