blob: 00b3174ff09586533112c6da0f7edfd4162bca0b [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
692// This test verifies that NetEq can handle comfort noise and enters/quits codec
693// internal CNG mode properly.
694TEST_F(NetEqImplTest, CodecInternalCng) {
695 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100696 // Create a mock decoder object.
697 MockAudioDecoder mock_decoder;
698 CreateInstance(
699 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000700
701 const uint8_t kPayloadType = 17; // Just an arbitrary number.
702 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
703 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700704 const size_t kPayloadLengthSamples =
705 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
706 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000707 uint8_t payload[kPayloadLengthBytes] = {0};
708 int16_t dummy_output[kPayloadLengthSamples] = {0};
709
henrik.lundin246ef3e2017-04-24 09:14:32 -0700710 RTPHeader rtp_header;
711 rtp_header.payloadType = kPayloadType;
712 rtp_header.sequenceNumber = 0x1234;
713 rtp_header.timestamp = 0x12345678;
714 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000715
Karl Wiberg43766482015-08-27 15:22:11 +0200716 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700717 EXPECT_CALL(mock_decoder, SampleRateHz())
718 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000719 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000720 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
721 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700722 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200723 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700724 // Packed duration when asking the decoder for more CNG data (without a new
725 // packet).
726 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200727 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000728
729 // Pointee(x) verifies that first byte of the payload equals x, this makes it
730 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100731 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
732 kSampleRateKhz * 1000, _, _))
733 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000734 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100735 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200736 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000737
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100738 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
739 kSampleRateKhz * 1000, _, _))
740 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000741 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100742 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200743 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000744
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100745 EXPECT_CALL(mock_decoder,
746 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
747 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000748 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100749 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200750 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000751
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100752 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
753 kSampleRateKhz * 1000, _, _))
754 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000755 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100756 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200757 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000758
Niels Möller50b66d52018-12-11 14:43:21 +0100759 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
760 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000761
762 // Insert one packet (decoder will return speech).
763 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700764 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000765
766 // Insert second packet (decoder will return CNG).
767 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700768 rtp_header.sequenceNumber++;
769 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000770 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700771 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000772
Peter Kastingdce40cf2015-08-24 14:52:23 -0700773 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800774 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800775 AudioFrame::SpeechType expected_type[8] = {
776 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
777 AudioFrame::kCNG, AudioFrame::kCNG,
778 AudioFrame::kCNG, AudioFrame::kCNG,
779 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000780 };
781 int expected_timestamp_increment[8] = {
782 -1, // will not be used.
783 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700784 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
785 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000786 50 * kSampleRateKhz, 10 * kSampleRateKhz
787 };
788
henrik.lundin7a926812016-05-12 13:51:28 -0700789 bool muted;
790 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200791 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700792 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000793
henrik.lundin0d96ab72016-04-06 12:28:26 -0700794 // Lambda for verifying the timestamps.
795 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200796 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700797 if (expected_timestamp_increment[i] == -1) {
798 // Expect to get an empty timestamp value during CNG and PLC.
799 EXPECT_FALSE(ts) << "i = " << i;
800 } else {
801 ASSERT_TRUE(ts) << "i = " << i;
802 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
803 << "i = " << i;
804 last_timestamp = ts;
805 }
806 };
807
Peter Kastingdce40cf2015-08-24 14:52:23 -0700808 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800809 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
810 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800811 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700812 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700813 SCOPED_TRACE("");
814 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000815 }
816
817 // Insert third packet, which leaves a gap from last packet.
818 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700819 rtp_header.sequenceNumber += 2;
820 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000821 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700822 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000823
Peter Kastingdce40cf2015-08-24 14:52:23 -0700824 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800825 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
826 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800827 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700828 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700829 SCOPED_TRACE("");
830 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000831 }
832
833 // Now check the packet buffer, and make sure it is empty.
834 EXPECT_TRUE(packet_buffer_->Empty());
835
836 EXPECT_CALL(mock_decoder, Die());
837}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000838
839TEST_F(NetEqImplTest, UnsupportedDecoder) {
840 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100841 ::testing::NiceMock<MockAudioDecoder> decoder;
842
843 CreateInstance(
844 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700845 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800846 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000847
848 const uint8_t kPayloadType = 17; // Just an arbitrary number.
849 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
850 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000851
Peter Kastingdce40cf2015-08-24 14:52:23 -0700852 const size_t kPayloadLengthSamples =
853 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000854 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700855 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200856 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700857 RTPHeader rtp_header;
858 rtp_header.payloadType = kPayloadType;
859 rtp_header.sequenceNumber = 0x1234;
860 rtp_header.timestamp = 0x12345678;
861 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000862
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000863 const uint8_t kFirstPayloadValue = 1;
864 const uint8_t kSecondPayloadValue = 2;
865
ossu61a208b2016-09-20 01:38:00 -0700866 EXPECT_CALL(decoder,
867 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
868 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200869 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000870
ossu61a208b2016-09-20 01:38:00 -0700871 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000872 .Times(0);
873
ossu61a208b2016-09-20 01:38:00 -0700874 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
875 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000876 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700877 .WillOnce(DoAll(
878 SetArrayArgument<3>(dummy_output,
879 dummy_output + kPayloadLengthSamples * kChannels),
880 SetArgPointee<4>(AudioDecoder::kSpeech),
881 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000882
ossu61a208b2016-09-20 01:38:00 -0700883 EXPECT_CALL(decoder,
884 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
885 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200886 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700887
888 EXPECT_CALL(decoder, SampleRateHz())
889 .WillRepeatedly(Return(kSampleRateHz));
890
891 EXPECT_CALL(decoder, Channels())
892 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000893
Niels Möllera1eb9c72018-12-07 15:24:42 +0100894 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
895 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000896
897 // Insert one packet.
898 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
899 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700900 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000901
902 // Insert another packet.
903 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700904 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000905 // The second timestamp needs to be at least 30 ms after the first to make
906 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700907 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000908 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700909 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000910
henrik.lundin6d8e0112016-03-04 10:34:21 -0800911 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700912 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800913 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200914 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700915 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800916 // Output size and number of channels should be correct.
917 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
918 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
919 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000920
henrik.lundin6d8e0112016-03-04 10:34:21 -0800921 // Second call to GetAudio will decode the packet that is ok. No errors are
922 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700923 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800924 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
925 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700926
927 // Die isn't called through NiceMock (since it's called by the
928 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
929 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000930}
931
henrik.lundin116c84e2015-08-27 13:14:48 -0700932// This test inserts packets until the buffer is flushed. After that, it asks
933// NetEq for the network statistics. The purpose of the test is to make sure
934// that even though the buffer size increment is negative (which it becomes when
935// the packet causing a flush is inserted), the packet length stored in the
936// decision logic remains valid.
937TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
938 UseNoMocks();
939 CreateInstance();
940
941 const size_t kPayloadLengthSamples = 80;
942 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
943 const uint8_t kPayloadType = 17; // Just an arbitrary number.
944 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
945 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700946 RTPHeader rtp_header;
947 rtp_header.payloadType = kPayloadType;
948 rtp_header.sequenceNumber = 0x1234;
949 rtp_header.timestamp = 0x12345678;
950 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700951
Niels Möller05543682019-01-10 16:55:06 +0100952 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
953 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -0700954
955 // Insert packets until the buffer flushes.
956 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
957 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
958 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700959 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
960 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
961 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700962 }
963 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
964
965 // Ask for network statistics. This should not crash.
966 NetEqNetworkStatistics stats;
967 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
968}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200969
970TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
971 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100972 // Create a mock decoder object.
973 MockAudioDecoder mock_decoder;
974
975 CreateInstance(
976 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200977
978 const uint8_t kPayloadType = 17; // Just an arbitrary number.
979 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
980 const int kSampleRateHz = 8000;
981 const size_t kPayloadLengthSamples =
982 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
983 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
984 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700985 RTPHeader rtp_header;
986 rtp_header.payloadType = kPayloadType;
987 rtp_header.sequenceNumber = 0x1234;
988 rtp_header.timestamp = 0x12345678;
989 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200990
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200991 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700992 EXPECT_CALL(mock_decoder, SampleRateHz())
993 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200994 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
995 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
996 .WillRepeatedly(Return(0));
997 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200998 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200999 int16_t dummy_output[kPayloadLengthSamples] = {0};
1000 // The below expectation will make the mock decoder write
1001 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1002 // speech. That is, the decoded length is 5 samples shorter than the expected.
1003 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001004 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001005 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001006 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001007 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001008 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001009 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001010 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1011 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001012
1013 // Insert one packet.
1014 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001015 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001016
1017 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1018
1019 // Pull audio once.
1020 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001021 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001022 bool muted;
1023 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001024 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1025 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001026 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001027
1028 EXPECT_CALL(mock_decoder, Die());
1029}
minyuel6d92bf52015-09-23 15:20:39 +02001030
1031// This test checks the behavior of NetEq when audio decoder fails.
1032TEST_F(NetEqImplTest, DecodingError) {
1033 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001034 // Create a mock decoder object.
1035 MockAudioDecoder mock_decoder;
1036
1037 CreateInstance(
1038 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001039
1040 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1041 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1042 const int kSampleRateHz = 8000;
1043 const int kDecoderErrorCode = -97; // Any negative number.
1044
1045 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1046 const size_t kFrameLengthSamples =
1047 static_cast<size_t>(5 * kSampleRateHz / 1000);
1048
1049 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1050
1051 uint8_t payload[kPayloadLengthBytes] = {0};
1052
henrik.lundin246ef3e2017-04-24 09:14:32 -07001053 RTPHeader rtp_header;
1054 rtp_header.payloadType = kPayloadType;
1055 rtp_header.sequenceNumber = 0x1234;
1056 rtp_header.timestamp = 0x12345678;
1057 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001058
minyuel6d92bf52015-09-23 15:20:39 +02001059 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001060 EXPECT_CALL(mock_decoder, SampleRateHz())
1061 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001062 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1063 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1064 .WillRepeatedly(Return(0));
1065 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001066 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001067 EXPECT_CALL(mock_decoder, ErrorCode())
1068 .WillOnce(Return(kDecoderErrorCode));
1069 EXPECT_CALL(mock_decoder, HasDecodePlc())
1070 .WillOnce(Return(false));
1071 int16_t dummy_output[kFrameLengthSamples] = {0};
1072
1073 {
1074 InSequence sequence; // Dummy variable.
1075 // Mock decoder works normally the first time.
1076 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001077 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001078 .Times(3)
1079 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001080 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001081 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001082 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001083 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001084 .RetiresOnSaturation();
1085
1086 // Then mock decoder fails. A common reason for failure can be buffer being
1087 // too short
1088 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001089 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001090 .WillOnce(Return(-1))
1091 .RetiresOnSaturation();
1092
1093 // Mock decoder finally returns to normal.
1094 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001095 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001096 .Times(2)
1097 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001098 DoAll(SetArrayArgument<3>(dummy_output,
1099 dummy_output + kFrameLengthSamples),
1100 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001101 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001102 }
1103
Niels Möllera1eb9c72018-12-07 15:24:42 +01001104 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1105 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001106
1107 // Insert packets.
1108 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001109 rtp_header.sequenceNumber += 1;
1110 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001111 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001112 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001113 }
1114
1115 // Pull audio.
1116 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001117 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001118 bool muted;
1119 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001120 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1121 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001122 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001123
1124 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001125 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001126 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1127 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001128 // We are not expecting anything for output.speech_type_, since an error was
1129 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001130
1131 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001132 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001133 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1134 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001135 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001136
1137 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001138 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001139 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1140 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001141 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001142
1143 EXPECT_CALL(mock_decoder, Die());
1144}
1145
1146// This test checks the behavior of NetEq when audio decoder fails during CNG.
1147TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1148 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001149
1150 // Create a mock decoder object.
1151 MockAudioDecoder mock_decoder;
1152 CreateInstance(
1153 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001154
1155 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1156 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1157 const int kSampleRateHz = 8000;
1158 const int kDecoderErrorCode = -97; // Any negative number.
1159
1160 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1161 const size_t kFrameLengthSamples =
1162 static_cast<size_t>(5 * kSampleRateHz / 1000);
1163
1164 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1165
1166 uint8_t payload[kPayloadLengthBytes] = {0};
1167
henrik.lundin246ef3e2017-04-24 09:14:32 -07001168 RTPHeader rtp_header;
1169 rtp_header.payloadType = kPayloadType;
1170 rtp_header.sequenceNumber = 0x1234;
1171 rtp_header.timestamp = 0x12345678;
1172 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001173
minyuel6d92bf52015-09-23 15:20:39 +02001174 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001175 EXPECT_CALL(mock_decoder, SampleRateHz())
1176 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001177 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1178 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1179 .WillRepeatedly(Return(0));
1180 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001181 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001182 EXPECT_CALL(mock_decoder, ErrorCode())
1183 .WillOnce(Return(kDecoderErrorCode));
1184 int16_t dummy_output[kFrameLengthSamples] = {0};
1185
1186 {
1187 InSequence sequence; // Dummy variable.
1188 // Mock decoder works normally the first 2 times.
1189 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001190 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001191 .Times(2)
1192 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001193 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001194 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001195 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001196 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001197 .RetiresOnSaturation();
1198
1199 // Then mock decoder fails. A common reason for failure can be buffer being
1200 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001201 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001202 .WillOnce(Return(-1))
1203 .RetiresOnSaturation();
1204
1205 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001206 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001207 .Times(2)
1208 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001209 DoAll(SetArrayArgument<3>(dummy_output,
1210 dummy_output + kFrameLengthSamples),
1211 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001212 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001213 }
1214
Niels Möller50b66d52018-12-11 14:43:21 +01001215 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1216 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001217
1218 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1219 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001220 rtp_header.sequenceNumber += 1;
1221 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001222 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001223 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001224 }
1225
1226 // Pull audio.
1227 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001228 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001229 bool muted;
1230 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001231 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1232 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001233 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001234
1235 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001236 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001237 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1238 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001239 // We are not expecting anything for output.speech_type_, since an error was
1240 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001241
1242 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001243 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001244 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1245 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001246 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001247
1248 EXPECT_CALL(mock_decoder, Die());
1249}
1250
henrik.lundind89814b2015-11-23 06:49:25 -08001251// Tests that the return value from last_output_sample_rate_hz() is equal to the
1252// configured inital sample rate.
1253TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1254 UseNoMocks();
1255 config_.sample_rate_hz = 48000;
1256 CreateInstance();
1257 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1258}
1259
henrik.lundined497212016-04-25 10:11:38 -07001260TEST_F(NetEqImplTest, TickTimerIncrement) {
1261 UseNoMocks();
1262 CreateInstance();
1263 ASSERT_TRUE(tick_timer_);
1264 EXPECT_EQ(0u, tick_timer_->ticks());
1265 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001266 bool muted;
1267 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001268 EXPECT_EQ(1u, tick_timer_->ticks());
1269}
1270
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001271TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1272 UseNoMocks();
1273 use_mock_delay_manager_ = true;
1274 CreateInstance();
1275
1276 EXPECT_CALL(*mock_delay_manager_, SetBaseMinimumDelay(_))
1277 .WillOnce(Return(true))
1278 .WillOnce(Return(false));
1279
1280 const int delay_ms = 200;
1281
1282 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1283 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1284}
1285
1286TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1287 UseNoMocks();
1288 use_mock_delay_manager_ = true;
1289 CreateInstance();
1290
1291 const int delay_ms = 200;
1292
1293 EXPECT_CALL(*mock_delay_manager_, GetBaseMinimumDelay())
1294 .WillOnce(Return(delay_ms));
1295
1296 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1297}
1298
henrik.lundin114c1b32017-04-26 07:47:32 -07001299TEST_F(NetEqImplTest, TargetDelayMs) {
1300 UseNoMocks();
1301 use_mock_delay_manager_ = true;
1302 CreateInstance();
1303 // Let the dummy target delay be 17 packets.
1304 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1305 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1306 .WillOnce(Return(kTargetLevelPacketsQ8));
1307 // Default packet size before any packet has been decoded is 30 ms, so we are
1308 // expecting 17 * 30 = 510 ms target delay.
1309 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1310}
1311
henrik.lundinb8c55b12017-05-10 07:38:01 -07001312TEST_F(NetEqImplTest, InsertEmptyPacket) {
1313 UseNoMocks();
1314 use_mock_delay_manager_ = true;
1315 CreateInstance();
1316
1317 RTPHeader rtp_header;
1318 rtp_header.payloadType = 17;
1319 rtp_header.sequenceNumber = 0x1234;
1320 rtp_header.timestamp = 0x12345678;
1321 rtp_header.ssrc = 0x87654321;
1322
1323 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1324 neteq_->InsertEmptyPacket(rtp_header);
1325}
1326
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001327TEST_F(NetEqImplTest, EnableRtxHandling) {
1328 UseNoMocks();
1329 use_mock_delay_manager_ = true;
1330 config_.enable_rtx_handling = true;
1331 CreateInstance();
1332 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1333 .Times(1)
1334 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1335
1336 const int kPayloadLengthSamples = 80;
1337 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1338 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1339 const uint32_t kReceiveTime = 17;
1340 uint8_t payload[kPayloadLengthBytes] = {0};
1341 RTPHeader rtp_header;
1342 rtp_header.payloadType = kPayloadType;
1343 rtp_header.sequenceNumber = 0x1234;
1344 rtp_header.timestamp = 0x12345678;
1345 rtp_header.ssrc = 0x87654321;
1346
Niels Möller05543682019-01-10 16:55:06 +01001347 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1348 SdpAudioFormat("l16", 8000, 1)));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001349 EXPECT_EQ(NetEq::kOK,
1350 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1351 AudioFrame output;
1352 bool muted;
1353 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1354
1355 // Insert second packet that was sent before the first packet.
1356 rtp_header.sequenceNumber -= 1;
1357 rtp_header.timestamp -= kPayloadLengthSamples;
1358 EXPECT_CALL(*mock_delay_manager_,
1359 Update(rtp_header.sequenceNumber, rtp_header.timestamp, _));
1360 EXPECT_EQ(NetEq::kOK,
1361 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1362}
1363
minyue5bd33972016-05-02 04:46:11 -07001364class Decoder120ms : public AudioDecoder {
1365 public:
kwiberg347d3512016-06-16 01:59:09 -07001366 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1367 : sample_rate_hz_(sample_rate_hz),
1368 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001369 speech_type_(speech_type) {}
1370
1371 int DecodeInternal(const uint8_t* encoded,
1372 size_t encoded_len,
1373 int sample_rate_hz,
1374 int16_t* decoded,
1375 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001376 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001377 size_t decoded_len =
1378 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1379 for (size_t i = 0; i < decoded_len; ++i) {
1380 decoded[i] = next_value_++;
1381 }
1382 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001383 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001384 }
1385
1386 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001387 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001388 size_t Channels() const override { return 2; }
1389
1390 private:
kwiberg347d3512016-06-16 01:59:09 -07001391 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001392 int16_t next_value_;
1393 SpeechType speech_type_;
1394};
1395
1396class NetEqImplTest120ms : public NetEqImplTest {
1397 protected:
1398 NetEqImplTest120ms() : NetEqImplTest() {}
1399 virtual ~NetEqImplTest120ms() {}
1400
1401 void CreateInstanceNoMocks() {
1402 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001403 CreateInstance(decoder_factory_);
1404 EXPECT_TRUE(neteq_->RegisterPayloadType(
1405 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001406 }
1407
1408 void CreateInstanceWithDelayManagerMock() {
1409 UseNoMocks();
1410 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001411 CreateInstance(decoder_factory_);
1412 EXPECT_TRUE(neteq_->RegisterPayloadType(
1413 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001414 }
1415
1416 uint32_t timestamp_diff_between_packets() const {
1417 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1418 }
1419
1420 uint32_t first_timestamp() const { return 10u; }
1421
1422 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001423 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001424 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001425 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1426 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001427 }
1428 }
1429
1430 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001431 RTPHeader rtp_header;
1432 rtp_header.payloadType = kPayloadType;
1433 rtp_header.sequenceNumber = sequence_number_;
1434 rtp_header.timestamp = timestamp;
1435 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001436 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1437 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001438 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001439 sequence_number_++;
1440 }
1441
1442 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001443 const uint32_t sampling_freq = kSamplingFreq_;
1444 decoder_factory_ =
1445 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1446 [sampling_freq, speech_type]() {
1447 std::unique_ptr<AudioDecoder> decoder =
1448 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1449 RTC_CHECK_EQ(2, decoder->Channels());
1450 return decoder;
1451 });
minyue5bd33972016-05-02 04:46:11 -07001452 }
1453
Niels Möllera0f44302018-11-30 10:45:12 +01001454 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001455 AudioFrame output_;
1456 const uint32_t kPayloadType = 17;
1457 const uint32_t kSamplingFreq_ = 48000;
1458 uint16_t sequence_number_ = 1;
1459};
1460
minyue5bd33972016-05-02 04:46:11 -07001461TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001462 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001463 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001464
1465 InsertPacket(first_timestamp());
1466 GetFirstPacket();
1467
henrik.lundin7a926812016-05-12 13:51:28 -07001468 bool muted;
1469 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001470 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1471}
1472
1473TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001474 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001475 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001476
1477 InsertPacket(first_timestamp());
1478 GetFirstPacket();
1479
1480 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1481}
1482
1483TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001484 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001485 CreateInstanceWithDelayManagerMock();
1486
minyue5bd33972016-05-02 04:46:11 -07001487 InsertPacket(first_timestamp());
1488
1489 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001490 bool muted;
1491 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001492
1493 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1494
1495 // Delay manager reports a target level which should cause a Merge.
1496 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1497
henrik.lundin7a926812016-05-12 13:51:28 -07001498 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001499 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1500}
1501
1502TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001503 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001504 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001505
1506 InsertPacket(first_timestamp());
1507 GetFirstPacket();
1508
henrik.lundin7a926812016-05-12 13:51:28 -07001509 bool muted;
1510 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001511 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1512}
1513
1514TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001515 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001516 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001517
1518 InsertPacket(first_timestamp());
1519 GetFirstPacket();
1520 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1521
1522 // Delay manager report buffer limit which should cause a FastAccelerate.
1523 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1524 .Times(1)
1525 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
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(kFastAccelerate, neteq_->last_operation_for_test());
1530}
1531
1532TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001533 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001534 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001535
1536 InsertPacket(first_timestamp());
1537 GetFirstPacket();
1538
1539 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1540
1541 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1542 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1543 .Times(1)
1544 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1545
henrik.lundin7a926812016-05-12 13:51:28 -07001546 bool muted;
1547 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001548 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1549}
1550
1551TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001552 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001553 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001554
1555 InsertPacket(first_timestamp());
1556 GetFirstPacket();
1557
1558 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1559
1560 // Delay manager report buffer limit which should cause a Accelerate.
1561 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1562 .Times(1)
1563 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1564
henrik.lundin7a926812016-05-12 13:51:28 -07001565 bool muted;
1566 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001567 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1568}
1569
minyuel6d92bf52015-09-23 15:20:39 +02001570}// namespace webrtc