1 /*
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
11 #include "modules/audio_coding/neteq/neteq_impl.h"
12
13 #include <memory>
14 #include <utility>
15 #include <vector>
16
17 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
18 #include "api/neteq/default_neteq_controller_factory.h"
19 #include "api/neteq/neteq.h"
20 #include "api/neteq/neteq_controller.h"
21 #include "modules/audio_coding/neteq/accelerate.h"
22 #include "modules/audio_coding/neteq/decision_logic.h"
23 #include "modules/audio_coding/neteq/default_neteq_factory.h"
24 #include "modules/audio_coding/neteq/expand.h"
25 #include "modules/audio_coding/neteq/histogram.h"
26 #include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
27 #include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
28 #include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
29 #include "modules/audio_coding/neteq/mock/mock_neteq_controller.h"
30 #include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
31 #include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
32 #include "modules/audio_coding/neteq/preemptive_expand.h"
33 #include "modules/audio_coding/neteq/statistics_calculator.h"
34 #include "modules/audio_coding/neteq/sync_buffer.h"
35 #include "modules/audio_coding/neteq/timestamp_scaler.h"
36 #include "rtc_base/numerics/safe_conversions.h"
37 #include "system_wrappers/include/clock.h"
38 #include "test/audio_decoder_proxy_factory.h"
39 #include "test/function_audio_decoder_factory.h"
40 #include "test/gmock.h"
41 #include "test/gtest.h"
42 #include "test/mock_audio_decoder.h"
43 #include "test/mock_audio_decoder_factory.h"
44
45 using ::testing::_;
46 using ::testing::AtLeast;
47 using ::testing::DoAll;
48 using ::testing::ElementsAre;
49 using ::testing::InSequence;
50 using ::testing::Invoke;
51 using ::testing::IsEmpty;
52 using ::testing::IsNull;
53 using ::testing::Pointee;
54 using ::testing::Return;
55 using ::testing::ReturnNull;
56 using ::testing::SetArgPointee;
57 using ::testing::SetArrayArgument;
58 using ::testing::SizeIs;
59 using ::testing::WithArg;
60
61 namespace webrtc {
62
63 // This function is called when inserting a packet list into the mock packet
64 // buffer. The purpose is to delete all inserted packets properly, to avoid
65 // memory leaks in the test.
DeletePacketsAndReturnOk(PacketList * packet_list)66 int DeletePacketsAndReturnOk(PacketList* packet_list) {
67 packet_list->clear();
68 return PacketBuffer::kOK;
69 }
70
71 class NetEqImplTest : public ::testing::Test {
72 protected:
NetEqImplTest()73 NetEqImplTest() : clock_(0) { config_.sample_rate_hz = 8000; }
74
CreateInstance(const rtc::scoped_refptr<AudioDecoderFactory> & decoder_factory)75 void CreateInstance(
76 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
77 ASSERT_TRUE(decoder_factory);
78 NetEqImpl::Dependencies deps(config_, &clock_, decoder_factory,
79 DefaultNetEqControllerFactory());
80
81 // Get a local pointer to NetEq's TickTimer object.
82 tick_timer_ = deps.tick_timer.get();
83
84 if (use_mock_decoder_database_) {
85 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
86 mock_decoder_database_ = mock.get();
87 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
88 .WillOnce(ReturnNull());
89 deps.decoder_database = std::move(mock);
90 }
91 decoder_database_ = deps.decoder_database.get();
92
93 if (use_mock_dtmf_buffer_) {
94 std::unique_ptr<MockDtmfBuffer> mock(
95 new MockDtmfBuffer(config_.sample_rate_hz));
96 mock_dtmf_buffer_ = mock.get();
97 deps.dtmf_buffer = std::move(mock);
98 }
99 dtmf_buffer_ = deps.dtmf_buffer.get();
100
101 if (use_mock_dtmf_tone_generator_) {
102 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
103 mock_dtmf_tone_generator_ = mock.get();
104 deps.dtmf_tone_generator = std::move(mock);
105 }
106 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
107
108 if (use_mock_packet_buffer_) {
109 std::unique_ptr<MockPacketBuffer> mock(
110 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
111 mock_packet_buffer_ = mock.get();
112 deps.packet_buffer = std::move(mock);
113 }
114 packet_buffer_ = deps.packet_buffer.get();
115
116 if (use_mock_neteq_controller_) {
117 std::unique_ptr<MockNetEqController> mock(new MockNetEqController());
118 mock_neteq_controller_ = mock.get();
119 deps.neteq_controller = std::move(mock);
120 } else {
121 deps.stats = std::make_unique<StatisticsCalculator>();
122 NetEqController::Config controller_config;
123 controller_config.tick_timer = tick_timer_;
124 controller_config.base_min_delay_ms = config_.min_delay_ms;
125 controller_config.enable_rtx_handling = config_.enable_rtx_handling;
126 controller_config.allow_time_stretching = true;
127 controller_config.max_packets_in_buffer = config_.max_packets_in_buffer;
128 controller_config.clock = &clock_;
129 deps.neteq_controller =
130 std::make_unique<DecisionLogic>(std::move(controller_config));
131 }
132 neteq_controller_ = deps.neteq_controller.get();
133
134 if (use_mock_payload_splitter_) {
135 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
136 mock_payload_splitter_ = mock.get();
137 deps.red_payload_splitter = std::move(mock);
138 }
139 red_payload_splitter_ = deps.red_payload_splitter.get();
140
141 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
142 new TimestampScaler(*deps.decoder_database.get()));
143
144 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
145 ASSERT_TRUE(neteq_ != NULL);
146 }
147
CreateInstance()148 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
149
UseNoMocks()150 void UseNoMocks() {
151 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
152 use_mock_decoder_database_ = false;
153 use_mock_neteq_controller_ = false;
154 use_mock_dtmf_buffer_ = false;
155 use_mock_dtmf_tone_generator_ = false;
156 use_mock_packet_buffer_ = false;
157 use_mock_payload_splitter_ = false;
158 }
159
~NetEqImplTest()160 virtual ~NetEqImplTest() {
161 if (use_mock_decoder_database_) {
162 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
163 }
164 if (use_mock_neteq_controller_) {
165 EXPECT_CALL(*mock_neteq_controller_, Die()).Times(1);
166 }
167 if (use_mock_dtmf_buffer_) {
168 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
169 }
170 if (use_mock_dtmf_tone_generator_) {
171 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
172 }
173 if (use_mock_packet_buffer_) {
174 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
175 }
176 }
177
TestDtmfPacket(int sample_rate_hz)178 void TestDtmfPacket(int sample_rate_hz) {
179 const size_t kPayloadLength = 4;
180 const uint8_t kPayloadType = 110;
181 const int kSampleRateHz = 16000;
182 config_.sample_rate_hz = kSampleRateHz;
183 UseNoMocks();
184 CreateInstance();
185 // Event: 2, E bit, Volume: 17, Length: 4336.
186 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
187 RTPHeader rtp_header;
188 rtp_header.payloadType = kPayloadType;
189 rtp_header.sequenceNumber = 0x1234;
190 rtp_header.timestamp = 0x12345678;
191 rtp_header.ssrc = 0x87654321;
192
193 EXPECT_TRUE(neteq_->RegisterPayloadType(
194 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
195
196 // Insert first packet.
197 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
198
199 // Pull audio once.
200 const size_t kMaxOutputSize =
201 static_cast<size_t>(10 * kSampleRateHz / 1000);
202 AudioFrame output;
203 bool muted;
204 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
205 ASSERT_FALSE(muted);
206 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
207 EXPECT_EQ(1u, output.num_channels_);
208 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
209
210 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
211 // returned by |GetAudio()|.
212 EXPECT_THAT(output.packet_infos_, IsEmpty());
213
214 // Verify first 64 samples of actual output.
215 const std::vector<int16_t> kOutput(
216 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
218 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
219 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
220 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
221 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
222 -3864, -3516, -2534, -1163});
223 ASSERT_GE(kMaxOutputSize, kOutput.size());
224 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
225 }
226
227 std::unique_ptr<NetEqImpl> neteq_;
228 NetEq::Config config_;
229 SimulatedClock clock_;
230 TickTimer* tick_timer_ = nullptr;
231 MockDecoderDatabase* mock_decoder_database_ = nullptr;
232 DecoderDatabase* decoder_database_ = nullptr;
233 bool use_mock_decoder_database_ = true;
234 MockNetEqController* mock_neteq_controller_ = nullptr;
235 NetEqController* neteq_controller_ = nullptr;
236 bool use_mock_neteq_controller_ = true;
237 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
238 DtmfBuffer* dtmf_buffer_ = nullptr;
239 bool use_mock_dtmf_buffer_ = true;
240 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
241 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
242 bool use_mock_dtmf_tone_generator_ = true;
243 MockPacketBuffer* mock_packet_buffer_ = nullptr;
244 PacketBuffer* packet_buffer_ = nullptr;
245 bool use_mock_packet_buffer_ = true;
246 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
247 RedPayloadSplitter* red_payload_splitter_ = nullptr;
248 bool use_mock_payload_splitter_ = true;
249 };
250
251 // This tests the interface class NetEq.
252 // TODO(hlundin): Move to separate file?
TEST(NetEq,CreateAndDestroy)253 TEST(NetEq, CreateAndDestroy) {
254 NetEq::Config config;
255 SimulatedClock clock(0);
256 auto decoder_factory = CreateBuiltinAudioDecoderFactory();
257 std::unique_ptr<NetEq> neteq =
258 DefaultNetEqFactory().CreateNetEq(config, decoder_factory, &clock);
259 }
260
TEST_F(NetEqImplTest,RegisterPayloadType)261 TEST_F(NetEqImplTest, RegisterPayloadType) {
262 CreateInstance();
263 constexpr int rtp_payload_type = 0;
264 const SdpAudioFormat format("pcmu", 8000, 1);
265 EXPECT_CALL(*mock_decoder_database_,
266 RegisterPayload(rtp_payload_type, format));
267 neteq_->RegisterPayloadType(rtp_payload_type, format);
268 }
269
TEST_F(NetEqImplTest,RemovePayloadType)270 TEST_F(NetEqImplTest, RemovePayloadType) {
271 CreateInstance();
272 uint8_t rtp_payload_type = 0;
273 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
274 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
275 // Check that kOK is returned when database returns kDecoderNotFound, because
276 // removing a payload type that was never registered is not an error.
277 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
278 }
279
TEST_F(NetEqImplTest,RemoveAllPayloadTypes)280 TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
281 CreateInstance();
282 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
283 neteq_->RemoveAllPayloadTypes();
284 }
285
TEST_F(NetEqImplTest,InsertPacket)286 TEST_F(NetEqImplTest, InsertPacket) {
287 CreateInstance();
288 const size_t kPayloadLength = 100;
289 const uint8_t kPayloadType = 0;
290 const uint16_t kFirstSequenceNumber = 0x1234;
291 const uint32_t kFirstTimestamp = 0x12345678;
292 const uint32_t kSsrc = 0x87654321;
293 uint8_t payload[kPayloadLength] = {0};
294 RTPHeader rtp_header;
295 rtp_header.payloadType = kPayloadType;
296 rtp_header.sequenceNumber = kFirstSequenceNumber;
297 rtp_header.timestamp = kFirstTimestamp;
298 rtp_header.ssrc = kSsrc;
299 Packet fake_packet;
300 fake_packet.payload_type = kPayloadType;
301 fake_packet.sequence_number = kFirstSequenceNumber;
302 fake_packet.timestamp = kFirstTimestamp;
303
304 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
305 new rtc::RefCountedObject<MockAudioDecoderFactory>);
306 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
307 .WillOnce(Invoke([&](const SdpAudioFormat& format,
308 absl::optional<AudioCodecPairId> codec_pair_id,
309 std::unique_ptr<AudioDecoder>* dec) {
310 EXPECT_EQ("pcmu", format.name);
311
312 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
313 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
314 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
315 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
316
317 *dec = std::move(mock_decoder);
318 }));
319 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
320 absl::nullopt, mock_decoder_factory);
321
322 // Expectations for decoder database.
323 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
324 .WillRepeatedly(Return(&info));
325
326 // Expectations for packet buffer.
327 EXPECT_CALL(*mock_packet_buffer_, Empty())
328 .WillOnce(Return(false)); // Called once after first packet is inserted.
329 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
330 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
331 .Times(2)
332 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
333 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
334 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
335 // index) is a pointer, and the variable pointed to is set to kPayloadType.
336 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
337 // packets in the list (to avoid memory leaks in the test).
338 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
339 .Times(1)
340 .WillOnce(Return(&fake_packet));
341
342 // Expectations for DTMF buffer.
343 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
344
345 // Expectations for delay manager.
346 {
347 // All expectations within this block must be called in this specific order.
348 InSequence sequence; // Dummy variable.
349 // Expectations when the first packet is inserted.
350 EXPECT_CALL(*mock_neteq_controller_,
351 PacketArrived(/*last_cng_or_dtmf*/ false,
352 /*packet_length_samples*/ _,
353 /*should_update_stats*/ _,
354 /*main_sequence_number*/ kFirstSequenceNumber,
355 /*main_timestamp*/ kFirstTimestamp,
356 /*fs_hz*/ 8000));
357 EXPECT_CALL(*mock_neteq_controller_,
358 PacketArrived(/*last_cng_or_dtmf*/ false,
359 /*packet_length_samples*/ _,
360 /*should_update_stats*/ _,
361 /*main_sequence_number*/ kFirstSequenceNumber + 1,
362 /*main_timestamp*/ kFirstTimestamp + 160,
363 /*fs_hz*/ 8000));
364 }
365
366 // Insert first packet.
367 neteq_->InsertPacket(rtp_header, payload);
368
369 // Insert second packet.
370 rtp_header.timestamp += 160;
371 rtp_header.sequenceNumber += 1;
372 neteq_->InsertPacket(rtp_header, payload);
373 }
374
TEST_F(NetEqImplTest,InsertPacketsUntilBufferIsFull)375 TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
376 UseNoMocks();
377 CreateInstance();
378
379 const int kPayloadLengthSamples = 80;
380 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
381 const uint8_t kPayloadType = 17; // Just an arbitrary number.
382 uint8_t payload[kPayloadLengthBytes] = {0};
383 RTPHeader rtp_header;
384 rtp_header.payloadType = kPayloadType;
385 rtp_header.sequenceNumber = 0x1234;
386 rtp_header.timestamp = 0x12345678;
387 rtp_header.ssrc = 0x87654321;
388
389 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
390 SdpAudioFormat("l16", 8000, 1)));
391
392 // Insert packets. The buffer should not flush.
393 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
394 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
395 rtp_header.timestamp += kPayloadLengthSamples;
396 rtp_header.sequenceNumber += 1;
397 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
398 }
399
400 // Insert one more packet and make sure the buffer got flushed. That is, it
401 // should only hold one single packet.
402 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
403 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
404 const Packet* test_packet = packet_buffer_->PeekNextPacket();
405 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
406 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
407 }
408
TEST_F(NetEqImplTest,TestDtmfPacketAVT)409 TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
410 TestDtmfPacket(8000);
411 }
412
TEST_F(NetEqImplTest,TestDtmfPacketAVT16kHz)413 TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
414 TestDtmfPacket(16000);
415 }
416
TEST_F(NetEqImplTest,TestDtmfPacketAVT32kHz)417 TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
418 TestDtmfPacket(32000);
419 }
420
TEST_F(NetEqImplTest,TestDtmfPacketAVT48kHz)421 TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
422 TestDtmfPacket(48000);
423 }
424
425 // This test verifies that timestamps propagate from the incoming packets
426 // through to the sync buffer and to the playout timestamp.
TEST_F(NetEqImplTest,VerifyTimestampPropagation)427 TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
428 const uint8_t kPayloadType = 17; // Just an arbitrary number.
429 const int kSampleRateHz = 8000;
430 const size_t kPayloadLengthSamples =
431 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
432 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
433 uint8_t payload[kPayloadLengthBytes] = {0};
434 RTPHeader rtp_header;
435 rtp_header.payloadType = kPayloadType;
436 rtp_header.sequenceNumber = 0x1234;
437 rtp_header.timestamp = 0x12345678;
438 rtp_header.ssrc = 0x87654321;
439 rtp_header.numCSRCs = 3;
440 rtp_header.arrOfCSRCs[0] = 43;
441 rtp_header.arrOfCSRCs[1] = 65;
442 rtp_header.arrOfCSRCs[2] = 17;
443
444 // This is a dummy decoder that produces as many output samples as the input
445 // has bytes. The output is an increasing series, starting at 1 for the first
446 // sample, and then increasing by 1 for each sample.
447 class CountingSamplesDecoder : public AudioDecoder {
448 public:
449 CountingSamplesDecoder() : next_value_(1) {}
450
451 // Produce as many samples as input bytes (|encoded_len|).
452 int DecodeInternal(const uint8_t* encoded,
453 size_t encoded_len,
454 int /* sample_rate_hz */,
455 int16_t* decoded,
456 SpeechType* speech_type) override {
457 for (size_t i = 0; i < encoded_len; ++i) {
458 decoded[i] = next_value_++;
459 }
460 *speech_type = kSpeech;
461 return rtc::checked_cast<int>(encoded_len);
462 }
463
464 void Reset() override { next_value_ = 1; }
465
466 int SampleRateHz() const override { return kSampleRateHz; }
467
468 size_t Channels() const override { return 1; }
469
470 uint16_t next_value() const { return next_value_; }
471
472 private:
473 int16_t next_value_;
474 } decoder_;
475
476 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
477 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
478
479 UseNoMocks();
480 CreateInstance(decoder_factory);
481
482 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
483 SdpAudioFormat("L16", 8000, 1)));
484
485 // Insert one packet.
486 clock_.AdvanceTimeMilliseconds(123456);
487 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
488 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
489
490 // Pull audio once.
491 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
492 AudioFrame output;
493 bool muted;
494 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
495 ASSERT_FALSE(muted);
496 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
497 EXPECT_EQ(1u, output.num_channels_);
498 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
499
500 // Verify |output.packet_infos_|.
501 ASSERT_THAT(output.packet_infos_, SizeIs(1));
502 {
503 const auto& packet_info = output.packet_infos_[0];
504 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
505 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
506 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
507 EXPECT_FALSE(packet_info.audio_level().has_value());
508 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
509 }
510
511 // Start with a simple check that the fake decoder is behaving as expected.
512 EXPECT_EQ(kPayloadLengthSamples,
513 static_cast<size_t>(decoder_.next_value() - 1));
514
515 // The value of the last of the output samples is the same as the number of
516 // samples played from the decoded packet. Thus, this number + the RTP
517 // timestamp should match the playout timestamp.
518 // Wrap the expected value in an absl::optional to compare them as such.
519 EXPECT_EQ(
520 absl::optional<uint32_t>(rtp_header.timestamp +
521 output.data()[output.samples_per_channel_ - 1]),
522 neteq_->GetPlayoutTimestamp());
523
524 // Check the timestamp for the last value in the sync buffer. This should
525 // be one full frame length ahead of the RTP timestamp.
526 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
527 ASSERT_TRUE(sync_buffer != NULL);
528 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
529 sync_buffer->end_timestamp());
530
531 // Check that the number of samples still to play from the sync buffer add
532 // up with what was already played out.
533 EXPECT_EQ(
534 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
535 sync_buffer->FutureLength());
536 }
537
TEST_F(NetEqImplTest,ReorderedPacket)538 TEST_F(NetEqImplTest, ReorderedPacket) {
539 UseNoMocks();
540 // Create a mock decoder object.
541 MockAudioDecoder mock_decoder;
542
543 CreateInstance(
544 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
545
546 const uint8_t kPayloadType = 17; // Just an arbitrary number.
547 const int kSampleRateHz = 8000;
548 const size_t kPayloadLengthSamples =
549 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
550 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
551 uint8_t payload[kPayloadLengthBytes] = {0};
552 RTPHeader rtp_header;
553 rtp_header.payloadType = kPayloadType;
554 rtp_header.sequenceNumber = 0x1234;
555 rtp_header.timestamp = 0x12345678;
556 rtp_header.ssrc = 0x87654321;
557 rtp_header.extension.hasAudioLevel = true;
558 rtp_header.extension.audioLevel = 42;
559
560 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
561 EXPECT_CALL(mock_decoder, SampleRateHz())
562 .WillRepeatedly(Return(kSampleRateHz));
563 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
564 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
565 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
566 int16_t dummy_output[kPayloadLengthSamples] = {0};
567 // The below expectation will make the mock decoder write
568 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
569 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
570 kSampleRateHz, _, _))
571 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
572 dummy_output + kPayloadLengthSamples),
573 SetArgPointee<4>(AudioDecoder::kSpeech),
574 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
575 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
576 SdpAudioFormat("L16", 8000, 1)));
577
578 // Insert one packet.
579 clock_.AdvanceTimeMilliseconds(123456);
580 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
581 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
582
583 // Pull audio once.
584 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
585 AudioFrame output;
586 bool muted;
587 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
588 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
589 EXPECT_EQ(1u, output.num_channels_);
590 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
591
592 // Verify |output.packet_infos_|.
593 ASSERT_THAT(output.packet_infos_, SizeIs(1));
594 {
595 const auto& packet_info = output.packet_infos_[0];
596 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
597 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
598 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
599 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
600 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
601 }
602
603 // Insert two more packets. The first one is out of order, and is already too
604 // old, the second one is the expected next packet.
605 rtp_header.sequenceNumber -= 1;
606 rtp_header.timestamp -= kPayloadLengthSamples;
607 rtp_header.extension.audioLevel = 1;
608 payload[0] = 1;
609 clock_.AdvanceTimeMilliseconds(1000);
610 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
611 rtp_header.sequenceNumber += 2;
612 rtp_header.timestamp += 2 * kPayloadLengthSamples;
613 rtp_header.extension.audioLevel = 2;
614 payload[0] = 2;
615 clock_.AdvanceTimeMilliseconds(2000);
616 expected_receive_time_ms = clock_.TimeInMilliseconds();
617 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
618
619 // Expect only the second packet to be decoded (the one with "2" as the first
620 // payload byte).
621 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
622 kSampleRateHz, _, _))
623 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
624 dummy_output + kPayloadLengthSamples),
625 SetArgPointee<4>(AudioDecoder::kSpeech),
626 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
627
628 // Pull audio once.
629 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
630 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
631 EXPECT_EQ(1u, output.num_channels_);
632 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
633
634 // Now check the packet buffer, and make sure it is empty, since the
635 // out-of-order packet should have been discarded.
636 EXPECT_TRUE(packet_buffer_->Empty());
637
638 // Verify |output.packet_infos_|. Expect to only see the second packet.
639 ASSERT_THAT(output.packet_infos_, SizeIs(1));
640 {
641 const auto& packet_info = output.packet_infos_[0];
642 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
643 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
644 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
645 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
646 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
647 }
648
649 EXPECT_CALL(mock_decoder, Die());
650 }
651
652 // This test verifies that NetEq can handle the situation where the first
653 // incoming packet is rejected.
TEST_F(NetEqImplTest,FirstPacketUnknown)654 TEST_F(NetEqImplTest, FirstPacketUnknown) {
655 UseNoMocks();
656 CreateInstance();
657
658 const uint8_t kPayloadType = 17; // Just an arbitrary number.
659 const int kSampleRateHz = 8000;
660 const size_t kPayloadLengthSamples =
661 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
662 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
663 uint8_t payload[kPayloadLengthBytes] = {0};
664 RTPHeader rtp_header;
665 rtp_header.payloadType = kPayloadType;
666 rtp_header.sequenceNumber = 0x1234;
667 rtp_header.timestamp = 0x12345678;
668 rtp_header.ssrc = 0x87654321;
669
670 // Insert one packet. Note that we have not registered any payload type, so
671 // this packet will be rejected.
672 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_header, payload));
673
674 // Pull audio once.
675 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
676 AudioFrame output;
677 bool muted;
678 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
679 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
680 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
681 EXPECT_EQ(1u, output.num_channels_);
682 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
683 EXPECT_THAT(output.packet_infos_, IsEmpty());
684
685 // Register the payload type.
686 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
687 SdpAudioFormat("l16", 8000, 1)));
688
689 // Insert 10 packets.
690 for (size_t i = 0; i < 10; ++i) {
691 rtp_header.sequenceNumber++;
692 rtp_header.timestamp += kPayloadLengthSamples;
693 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
694 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
695 }
696
697 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
698 for (size_t i = 0; i < 3; ++i) {
699 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
700 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
701 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
702 EXPECT_EQ(1u, output.num_channels_);
703 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
704 << "NetEq did not decode the packets as expected.";
705 EXPECT_THAT(output.packet_infos_, SizeIs(1));
706 }
707 }
708
709 // This test verifies that audio interruption is not logged for the initial
710 // PLC period before the first packet is deocoded.
711 // TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
712 // Make the test parametrized, so that we can test with different initial
713 // sample rates in NetEq.
714 class NetEqImplTestSampleRateParameter
715 : public NetEqImplTest,
716 public testing::WithParamInterface<int> {
717 protected:
NetEqImplTestSampleRateParameter()718 NetEqImplTestSampleRateParameter()
719 : NetEqImplTest(), initial_sample_rate_hz_(GetParam()) {
720 config_.sample_rate_hz = initial_sample_rate_hz_;
721 }
722
723 const int initial_sample_rate_hz_;
724 };
725
726 // This test does the following:
727 // 0. Set up NetEq with initial sample rate given by test parameter, and a codec
728 // sample rate of 16000.
729 // 1. Start calling GetAudio before inserting any encoded audio. The audio
730 // produced will be PLC.
731 // 2. Insert a number of encoded audio packets.
732 // 3. Keep calling GetAudio and verify that no audio interruption was logged.
733 // Call GetAudio until NetEq runs out of data again; PLC starts.
734 // 4. Insert one more packet.
735 // 5. Call GetAudio until that packet is decoded and the PLC ends.
736
TEST_P(NetEqImplTestSampleRateParameter,NoAudioInterruptionLoggedBeforeFirstDecode)737 TEST_P(NetEqImplTestSampleRateParameter,
738 NoAudioInterruptionLoggedBeforeFirstDecode) {
739 UseNoMocks();
740 CreateInstance();
741
742 const uint8_t kPayloadType = 17; // Just an arbitrary number.
743 const int kPayloadSampleRateHz = 16000;
744 const size_t kPayloadLengthSamples =
745 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms.
746 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
747 uint8_t payload[kPayloadLengthBytes] = {0};
748 RTPHeader rtp_header;
749 rtp_header.payloadType = kPayloadType;
750 rtp_header.sequenceNumber = 0x1234;
751 rtp_header.timestamp = 0x12345678;
752 rtp_header.ssrc = 0x87654321;
753
754 // Register the payload type.
755 EXPECT_TRUE(neteq_->RegisterPayloadType(
756 kPayloadType, SdpAudioFormat("l16", kPayloadSampleRateHz, 1)));
757
758 // Pull audio several times. No packets have been inserted yet.
759 const size_t initial_output_size =
760 static_cast<size_t>(10 * initial_sample_rate_hz_ / 1000); // 10 ms
761 AudioFrame output;
762 bool muted;
763 for (int i = 0; i < 100; ++i) {
764 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
765 EXPECT_EQ(initial_output_size, output.samples_per_channel_);
766 EXPECT_EQ(1u, output.num_channels_);
767 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
768 EXPECT_THAT(output.packet_infos_, IsEmpty());
769 }
770
771 // Lambda for inserting packets.
772 auto insert_packet = [&]() {
773 rtp_header.sequenceNumber++;
774 rtp_header.timestamp += kPayloadLengthSamples;
775 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
776 };
777 // Insert 10 packets.
778 for (size_t i = 0; i < 10; ++i) {
779 insert_packet();
780 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
781 }
782
783 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
784 constexpr size_t kOutputSize =
785 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms
786 for (size_t i = 0; i < 3; ++i) {
787 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
788 EXPECT_EQ(kOutputSize, output.samples_per_channel_);
789 EXPECT_EQ(1u, output.num_channels_);
790 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
791 << "NetEq did not decode the packets as expected.";
792 EXPECT_THAT(output.packet_infos_, SizeIs(1));
793 }
794
795 // Verify that no interruption was logged.
796 auto lifetime_stats = neteq_->GetLifetimeStatistics();
797 EXPECT_EQ(0, lifetime_stats.interruption_count);
798
799 // Keep pulling audio data until a new PLC period is started.
800 size_t count_loops = 0;
801 while (output.speech_type_ == AudioFrame::kNormalSpeech) {
802 // Make sure we don't hang the test if we never go to PLC.
803 ASSERT_LT(++count_loops, 100u);
804 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
805 }
806
807 // Insert one more packet.
808 insert_packet();
809
810 // Pull audio until the newly inserted packet is decoded and the PLC ends.
811 while (output.speech_type_ != AudioFrame::kNormalSpeech) {
812 // Make sure we don't hang the test if we never go to PLC.
813 ASSERT_LT(++count_loops, 100u);
814 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
815 }
816
817 // Verify that no interruption was logged.
818 lifetime_stats = neteq_->GetLifetimeStatistics();
819 EXPECT_EQ(0, lifetime_stats.interruption_count);
820 }
821
822 // This test does the following:
823 // 0. Set up NetEq with initial sample rate given by test parameter, and a codec
824 // sample rate of 16000.
825 // 1. Insert a number of encoded audio packets.
826 // 2. Call GetAudio and verify that decoded audio is produced.
827 // 3. Keep calling GetAudio until NetEq runs out of data; PLC starts.
828 // 4. Keep calling GetAudio until PLC has been produced for at least 150 ms.
829 // 5. Insert one more packet.
830 // 6. Call GetAudio until that packet is decoded and the PLC ends.
831 // 7. Verify that an interruption was logged.
832
TEST_P(NetEqImplTestSampleRateParameter,AudioInterruptionLogged)833 TEST_P(NetEqImplTestSampleRateParameter, AudioInterruptionLogged) {
834 UseNoMocks();
835 CreateInstance();
836
837 const uint8_t kPayloadType = 17; // Just an arbitrary number.
838 const int kPayloadSampleRateHz = 16000;
839 const size_t kPayloadLengthSamples =
840 static_cast<size_t>(10 * kPayloadSampleRateHz / 1000); // 10 ms.
841 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
842 uint8_t payload[kPayloadLengthBytes] = {0};
843 RTPHeader rtp_header;
844 rtp_header.payloadType = kPayloadType;
845 rtp_header.sequenceNumber = 0x1234;
846 rtp_header.timestamp = 0x12345678;
847 rtp_header.ssrc = 0x87654321;
848
849 // Register the payload type.
850 EXPECT_TRUE(neteq_->RegisterPayloadType(
851 kPayloadType, SdpAudioFormat("l16", kPayloadSampleRateHz, 1)));
852
853 // Lambda for inserting packets.
854 auto insert_packet = [&]() {
855 rtp_header.sequenceNumber++;
856 rtp_header.timestamp += kPayloadLengthSamples;
857 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
858 };
859 // Insert 10 packets.
860 for (size_t i = 0; i < 10; ++i) {
861 insert_packet();
862 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
863 }
864
865 AudioFrame output;
866 bool muted;
867 // Keep pulling audio data until a new PLC period is started.
868 size_t count_loops = 0;
869 do {
870 // Make sure we don't hang the test if we never go to PLC.
871 ASSERT_LT(++count_loops, 100u);
872 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
873 } while (output.speech_type_ == AudioFrame::kNormalSpeech);
874
875 // Pull audio 15 times, which produces 150 ms of output audio. This should
876 // all be produced as PLC. The total length of the gap will then be 150 ms
877 // plus an initial fraction of 10 ms at the start and the end of the PLC
878 // period. In total, less than 170 ms.
879 for (size_t i = 0; i < 15; ++i) {
880 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
881 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
882 }
883
884 // Insert one more packet.
885 insert_packet();
886
887 // Pull audio until the newly inserted packet is decoded and the PLC ends.
888 while (output.speech_type_ != AudioFrame::kNormalSpeech) {
889 // Make sure we don't hang the test if we never go to PLC.
890 ASSERT_LT(++count_loops, 100u);
891 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
892 }
893
894 // Verify that the interruption was logged.
895 auto lifetime_stats = neteq_->GetLifetimeStatistics();
896 EXPECT_EQ(1, lifetime_stats.interruption_count);
897 EXPECT_GT(lifetime_stats.total_interruption_duration_ms, 150);
898 EXPECT_LT(lifetime_stats.total_interruption_duration_ms, 170);
899 }
900
901 INSTANTIATE_TEST_SUITE_P(SampleRates,
902 NetEqImplTestSampleRateParameter,
903 testing::Values(8000, 16000, 32000, 48000));
904
905 // This test verifies that NetEq can handle comfort noise and enters/quits codec
906 // internal CNG mode properly.
TEST_F(NetEqImplTest,CodecInternalCng)907 TEST_F(NetEqImplTest, CodecInternalCng) {
908 UseNoMocks();
909 // Create a mock decoder object.
910 MockAudioDecoder mock_decoder;
911 CreateInstance(
912 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
913
914 const uint8_t kPayloadType = 17; // Just an arbitrary number.
915 const int kSampleRateKhz = 48;
916 const size_t kPayloadLengthSamples =
917 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
918 const size_t kPayloadLengthBytes = 10;
919 uint8_t payload[kPayloadLengthBytes] = {0};
920 int16_t dummy_output[kPayloadLengthSamples] = {0};
921
922 RTPHeader rtp_header;
923 rtp_header.payloadType = kPayloadType;
924 rtp_header.sequenceNumber = 0x1234;
925 rtp_header.timestamp = 0x12345678;
926 rtp_header.ssrc = 0x87654321;
927
928 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
929 EXPECT_CALL(mock_decoder, SampleRateHz())
930 .WillRepeatedly(Return(kSampleRateKhz * 1000));
931 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
932 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
933 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
934 // Packed duration when asking the decoder for more CNG data (without a new
935 // packet).
936 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
937 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
938
939 // Pointee(x) verifies that first byte of the payload equals x, this makes it
940 // possible to verify that the correct payload is fed to Decode().
941 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
942 kSampleRateKhz * 1000, _, _))
943 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
944 dummy_output + kPayloadLengthSamples),
945 SetArgPointee<4>(AudioDecoder::kSpeech),
946 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
947
948 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
949 kSampleRateKhz * 1000, _, _))
950 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
951 dummy_output + kPayloadLengthSamples),
952 SetArgPointee<4>(AudioDecoder::kComfortNoise),
953 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
954
955 EXPECT_CALL(mock_decoder,
956 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
957 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
958 dummy_output + kPayloadLengthSamples),
959 SetArgPointee<4>(AudioDecoder::kComfortNoise),
960 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
961
962 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
963 kSampleRateKhz * 1000, _, _))
964 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
965 dummy_output + kPayloadLengthSamples),
966 SetArgPointee<4>(AudioDecoder::kSpeech),
967 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
968
969 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
970 SdpAudioFormat("opus", 48000, 2)));
971
972 // Insert one packet (decoder will return speech).
973 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
974
975 // Insert second packet (decoder will return CNG).
976 payload[0] = 1;
977 rtp_header.sequenceNumber++;
978 rtp_header.timestamp += kPayloadLengthSamples;
979 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
980
981 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
982 AudioFrame output;
983 AudioFrame::SpeechType expected_type[8] = {
984 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
985 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
986 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
987 int expected_timestamp_increment[8] = {
988 -1, // will not be used.
989 10 * kSampleRateKhz,
990 -1,
991 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
992 -1,
993 -1,
994 50 * kSampleRateKhz,
995 10 * kSampleRateKhz};
996
997 bool muted;
998 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
999 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
1000 ASSERT_TRUE(last_timestamp);
1001
1002 // Lambda for verifying the timestamps.
1003 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
1004 absl::optional<uint32_t> ts, size_t i) {
1005 if (expected_timestamp_increment[i] == -1) {
1006 // Expect to get an empty timestamp value during CNG and PLC.
1007 EXPECT_FALSE(ts) << "i = " << i;
1008 } else {
1009 ASSERT_TRUE(ts) << "i = " << i;
1010 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
1011 << "i = " << i;
1012 last_timestamp = ts;
1013 }
1014 };
1015
1016 for (size_t i = 1; i < 6; ++i) {
1017 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1018 EXPECT_EQ(1u, output.num_channels_);
1019 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
1020 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1021 SCOPED_TRACE("");
1022 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
1023 }
1024
1025 // Insert third packet, which leaves a gap from last packet.
1026 payload[0] = 2;
1027 rtp_header.sequenceNumber += 2;
1028 rtp_header.timestamp += 2 * kPayloadLengthSamples;
1029 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1030
1031 for (size_t i = 6; i < 8; ++i) {
1032 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1033 EXPECT_EQ(1u, output.num_channels_);
1034 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
1035 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1036 SCOPED_TRACE("");
1037 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
1038 }
1039
1040 // Now check the packet buffer, and make sure it is empty.
1041 EXPECT_TRUE(packet_buffer_->Empty());
1042
1043 EXPECT_CALL(mock_decoder, Die());
1044 }
1045
TEST_F(NetEqImplTest,UnsupportedDecoder)1046 TEST_F(NetEqImplTest, UnsupportedDecoder) {
1047 UseNoMocks();
1048 ::testing::NiceMock<MockAudioDecoder> decoder;
1049
1050 CreateInstance(
1051 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
1052 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
1053 static const size_t kChannels = 2;
1054
1055 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1056 const int kSampleRateHz = 8000;
1057
1058 const size_t kPayloadLengthSamples =
1059 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1060 const size_t kPayloadLengthBytes = 1;
1061 uint8_t payload[kPayloadLengthBytes] = {0};
1062 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
1063 RTPHeader rtp_header;
1064 rtp_header.payloadType = kPayloadType;
1065 rtp_header.sequenceNumber = 0x1234;
1066 rtp_header.timestamp = 0x12345678;
1067 rtp_header.ssrc = 0x87654321;
1068
1069 const uint8_t kFirstPayloadValue = 1;
1070 const uint8_t kSecondPayloadValue = 2;
1071
1072 EXPECT_CALL(decoder,
1073 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
1074 .Times(AtLeast(1))
1075 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
1076
1077 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
1078 .Times(0);
1079
1080 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
1081 kPayloadLengthBytes, kSampleRateHz, _, _))
1082 .Times(1)
1083 .WillOnce(DoAll(
1084 SetArrayArgument<3>(dummy_output,
1085 dummy_output + kPayloadLengthSamples * kChannels),
1086 SetArgPointee<4>(AudioDecoder::kSpeech),
1087 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
1088
1089 EXPECT_CALL(decoder,
1090 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
1091 .Times(AtLeast(1))
1092 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
1093
1094 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
1095
1096 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
1097
1098 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1099 SdpAudioFormat("L16", 8000, 1)));
1100
1101 // Insert one packet.
1102 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
1103 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1104
1105 // Insert another packet.
1106 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
1107 rtp_header.sequenceNumber++;
1108 // The second timestamp needs to be at least 30 ms after the first to make
1109 // the second packet get decoded.
1110 rtp_header.timestamp += 3 * kPayloadLengthSamples;
1111 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1112
1113 AudioFrame output;
1114 bool muted;
1115 // First call to GetAudio will try to decode the "faulty" packet.
1116 // Expect kFail return value.
1117 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
1118 // Output size and number of channels should be correct.
1119 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
1120 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1121 EXPECT_EQ(kChannels, output.num_channels_);
1122 EXPECT_THAT(output.packet_infos_, IsEmpty());
1123
1124 // Second call to GetAudio will decode the packet that is ok. No errors are
1125 // expected.
1126 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1127 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
1128 EXPECT_EQ(kChannels, output.num_channels_);
1129 EXPECT_THAT(output.packet_infos_, SizeIs(1));
1130
1131 // Die isn't called through NiceMock (since it's called by the
1132 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
1133 EXPECT_CALL(decoder, Die());
1134 }
1135
1136 // This test inserts packets until the buffer is flushed. After that, it asks
1137 // NetEq for the network statistics. The purpose of the test is to make sure
1138 // that even though the buffer size increment is negative (which it becomes when
1139 // the packet causing a flush is inserted), the packet length stored in the
1140 // decision logic remains valid.
TEST_F(NetEqImplTest,FloodBufferAndGetNetworkStats)1141 TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1142 UseNoMocks();
1143 CreateInstance();
1144
1145 const size_t kPayloadLengthSamples = 80;
1146 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1147 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1148 uint8_t payload[kPayloadLengthBytes] = {0};
1149 RTPHeader rtp_header;
1150 rtp_header.payloadType = kPayloadType;
1151 rtp_header.sequenceNumber = 0x1234;
1152 rtp_header.timestamp = 0x12345678;
1153 rtp_header.ssrc = 0x87654321;
1154
1155 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1156 SdpAudioFormat("l16", 8000, 1)));
1157
1158 // Insert packets until the buffer flushes.
1159 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1160 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
1161 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1162 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1163 ++rtp_header.sequenceNumber;
1164 }
1165 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1166
1167 // Ask for network statistics. This should not crash.
1168 NetEqNetworkStatistics stats;
1169 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1170 }
1171
TEST_F(NetEqImplTest,DecodedPayloadTooShort)1172 TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1173 UseNoMocks();
1174 // Create a mock decoder object.
1175 MockAudioDecoder mock_decoder;
1176
1177 CreateInstance(
1178 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
1179
1180 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1181 const int kSampleRateHz = 8000;
1182 const size_t kPayloadLengthSamples =
1183 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1184 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1185 uint8_t payload[kPayloadLengthBytes] = {0};
1186 RTPHeader rtp_header;
1187 rtp_header.payloadType = kPayloadType;
1188 rtp_header.sequenceNumber = 0x1234;
1189 rtp_header.timestamp = 0x12345678;
1190 rtp_header.ssrc = 0x87654321;
1191
1192 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
1193 EXPECT_CALL(mock_decoder, SampleRateHz())
1194 .WillRepeatedly(Return(kSampleRateHz));
1195 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1196 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
1197 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
1198 int16_t dummy_output[kPayloadLengthSamples] = {0};
1199 // The below expectation will make the mock decoder write
1200 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1201 // speech. That is, the decoded length is 5 samples shorter than the expected.
1202 EXPECT_CALL(mock_decoder,
1203 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1204 .WillOnce(
1205 DoAll(SetArrayArgument<3>(dummy_output,
1206 dummy_output + kPayloadLengthSamples - 5),
1207 SetArgPointee<4>(AudioDecoder::kSpeech),
1208 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
1209 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1210 SdpAudioFormat("L16", 8000, 1)));
1211
1212 // Insert one packet.
1213 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1214
1215 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1216
1217 // Pull audio once.
1218 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
1219 AudioFrame output;
1220 bool muted;
1221 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1222 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1223 EXPECT_EQ(1u, output.num_channels_);
1224 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
1225 EXPECT_THAT(output.packet_infos_, SizeIs(1));
1226
1227 EXPECT_CALL(mock_decoder, Die());
1228 }
1229
1230 // This test checks the behavior of NetEq when audio decoder fails.
TEST_F(NetEqImplTest,DecodingError)1231 TEST_F(NetEqImplTest, DecodingError) {
1232 UseNoMocks();
1233 // Create a mock decoder object.
1234 MockAudioDecoder mock_decoder;
1235
1236 CreateInstance(
1237 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
1238
1239 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1240 const int kSampleRateHz = 8000;
1241 const int kDecoderErrorCode = -97; // Any negative number.
1242
1243 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1244 const size_t kFrameLengthSamples =
1245 static_cast<size_t>(5 * kSampleRateHz / 1000);
1246
1247 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1248
1249 uint8_t payload[kPayloadLengthBytes] = {0};
1250
1251 RTPHeader rtp_header;
1252 rtp_header.payloadType = kPayloadType;
1253 rtp_header.sequenceNumber = 0x1234;
1254 rtp_header.timestamp = 0x12345678;
1255 rtp_header.ssrc = 0x87654321;
1256
1257 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
1258 EXPECT_CALL(mock_decoder, SampleRateHz())
1259 .WillRepeatedly(Return(kSampleRateHz));
1260 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1261 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
1262 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
1263 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1264 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
1265 int16_t dummy_output[kFrameLengthSamples] = {0};
1266
1267 {
1268 InSequence sequence; // Dummy variable.
1269 // Mock decoder works normally the first time.
1270 EXPECT_CALL(mock_decoder,
1271 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1272 .Times(3)
1273 .WillRepeatedly(
1274 DoAll(SetArrayArgument<3>(dummy_output,
1275 dummy_output + kFrameLengthSamples),
1276 SetArgPointee<4>(AudioDecoder::kSpeech),
1277 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
1278 .RetiresOnSaturation();
1279
1280 // Then mock decoder fails. A common reason for failure can be buffer being
1281 // too short
1282 EXPECT_CALL(mock_decoder,
1283 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1284 .WillOnce(Return(-1))
1285 .RetiresOnSaturation();
1286
1287 // Mock decoder finally returns to normal.
1288 EXPECT_CALL(mock_decoder,
1289 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1290 .Times(2)
1291 .WillRepeatedly(
1292 DoAll(SetArrayArgument<3>(dummy_output,
1293 dummy_output + kFrameLengthSamples),
1294 SetArgPointee<4>(AudioDecoder::kSpeech),
1295 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
1296 }
1297
1298 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1299 SdpAudioFormat("L16", 8000, 1)));
1300
1301 // Insert packets.
1302 for (int i = 0; i < 6; ++i) {
1303 rtp_header.sequenceNumber += 1;
1304 rtp_header.timestamp += kFrameLengthSamples;
1305 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1306 }
1307
1308 // Pull audio.
1309 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
1310 AudioFrame output;
1311 bool muted;
1312 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1313 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1314 EXPECT_EQ(1u, output.num_channels_);
1315 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
1316 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
1317
1318 // Pull audio again. Decoder fails.
1319 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
1320 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1321 EXPECT_EQ(1u, output.num_channels_);
1322 // We are not expecting anything for output.speech_type_, since an error was
1323 // returned.
1324
1325 // Pull audio again, should continue an expansion.
1326 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1327 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1328 EXPECT_EQ(1u, output.num_channels_);
1329 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
1330 EXPECT_THAT(output.packet_infos_, IsEmpty());
1331
1332 // Pull audio again, should behave normal.
1333 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1334 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1335 EXPECT_EQ(1u, output.num_channels_);
1336 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
1337 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
1338
1339 EXPECT_CALL(mock_decoder, Die());
1340 }
1341
1342 // This test checks the behavior of NetEq when audio decoder fails during CNG.
TEST_F(NetEqImplTest,DecodingErrorDuringInternalCng)1343 TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1344 UseNoMocks();
1345
1346 // Create a mock decoder object.
1347 MockAudioDecoder mock_decoder;
1348 CreateInstance(
1349 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
1350
1351 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1352 const int kSampleRateHz = 8000;
1353 const int kDecoderErrorCode = -97; // Any negative number.
1354
1355 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1356 const size_t kFrameLengthSamples =
1357 static_cast<size_t>(5 * kSampleRateHz / 1000);
1358
1359 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1360
1361 uint8_t payload[kPayloadLengthBytes] = {0};
1362
1363 RTPHeader rtp_header;
1364 rtp_header.payloadType = kPayloadType;
1365 rtp_header.sequenceNumber = 0x1234;
1366 rtp_header.timestamp = 0x12345678;
1367 rtp_header.ssrc = 0x87654321;
1368
1369 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
1370 EXPECT_CALL(mock_decoder, SampleRateHz())
1371 .WillRepeatedly(Return(kSampleRateHz));
1372 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1373 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
1374 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
1375 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1376 int16_t dummy_output[kFrameLengthSamples] = {0};
1377
1378 {
1379 InSequence sequence; // Dummy variable.
1380 // Mock decoder works normally the first 2 times.
1381 EXPECT_CALL(mock_decoder,
1382 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1383 .Times(2)
1384 .WillRepeatedly(
1385 DoAll(SetArrayArgument<3>(dummy_output,
1386 dummy_output + kFrameLengthSamples),
1387 SetArgPointee<4>(AudioDecoder::kComfortNoise),
1388 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
1389 .RetiresOnSaturation();
1390
1391 // Then mock decoder fails. A common reason for failure can be buffer being
1392 // too short
1393 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
1394 .WillOnce(Return(-1))
1395 .RetiresOnSaturation();
1396
1397 // Mock decoder finally returns to normal.
1398 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
1399 .Times(2)
1400 .WillRepeatedly(
1401 DoAll(SetArrayArgument<3>(dummy_output,
1402 dummy_output + kFrameLengthSamples),
1403 SetArgPointee<4>(AudioDecoder::kComfortNoise),
1404 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
1405 }
1406
1407 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1408 SdpAudioFormat("l16", 8000, 1)));
1409
1410 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1411 for (int i = 0; i < 2; ++i) {
1412 rtp_header.sequenceNumber += 1;
1413 rtp_header.timestamp += kFrameLengthSamples;
1414 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1415 }
1416
1417 // Pull audio.
1418 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
1419 AudioFrame output;
1420 bool muted;
1421 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1422 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1423 EXPECT_EQ(1u, output.num_channels_);
1424 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
1425
1426 // Pull audio again. Decoder fails.
1427 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
1428 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1429 EXPECT_EQ(1u, output.num_channels_);
1430 // We are not expecting anything for output.speech_type_, since an error was
1431 // returned.
1432
1433 // Pull audio again, should resume codec CNG.
1434 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1435 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1436 EXPECT_EQ(1u, output.num_channels_);
1437 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
1438
1439 EXPECT_CALL(mock_decoder, Die());
1440 }
1441
1442 // Tests that the return value from last_output_sample_rate_hz() is equal to the
1443 // configured inital sample rate.
TEST_F(NetEqImplTest,InitialLastOutputSampleRate)1444 TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1445 UseNoMocks();
1446 config_.sample_rate_hz = 48000;
1447 CreateInstance();
1448 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1449 }
1450
TEST_F(NetEqImplTest,TickTimerIncrement)1451 TEST_F(NetEqImplTest, TickTimerIncrement) {
1452 UseNoMocks();
1453 CreateInstance();
1454 ASSERT_TRUE(tick_timer_);
1455 EXPECT_EQ(0u, tick_timer_->ticks());
1456 AudioFrame output;
1457 bool muted;
1458 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1459 EXPECT_EQ(1u, tick_timer_->ticks());
1460 }
1461
TEST_F(NetEqImplTest,SetBaseMinimumDelay)1462 TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1463 UseNoMocks();
1464 use_mock_neteq_controller_ = true;
1465 CreateInstance();
1466
1467 EXPECT_CALL(*mock_neteq_controller_, SetBaseMinimumDelay(_))
1468 .WillOnce(Return(true))
1469 .WillOnce(Return(false));
1470
1471 const int delay_ms = 200;
1472
1473 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1474 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1475 }
1476
TEST_F(NetEqImplTest,GetBaseMinimumDelayMs)1477 TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1478 UseNoMocks();
1479 use_mock_neteq_controller_ = true;
1480 CreateInstance();
1481
1482 const int delay_ms = 200;
1483
1484 EXPECT_CALL(*mock_neteq_controller_, GetBaseMinimumDelay())
1485 .WillOnce(Return(delay_ms));
1486
1487 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1488 }
1489
TEST_F(NetEqImplTest,TargetDelayMs)1490 TEST_F(NetEqImplTest, TargetDelayMs) {
1491 UseNoMocks();
1492 use_mock_neteq_controller_ = true;
1493 CreateInstance();
1494 constexpr int kTargetLevelMs = 510;
1495 EXPECT_CALL(*mock_neteq_controller_, TargetLevelMs())
1496 .WillOnce(Return(kTargetLevelMs));
1497 EXPECT_EQ(510, neteq_->TargetDelayMs());
1498 }
1499
TEST_F(NetEqImplTest,InsertEmptyPacket)1500 TEST_F(NetEqImplTest, InsertEmptyPacket) {
1501 UseNoMocks();
1502 use_mock_neteq_controller_ = true;
1503 CreateInstance();
1504
1505 RTPHeader rtp_header;
1506 rtp_header.payloadType = 17;
1507 rtp_header.sequenceNumber = 0x1234;
1508 rtp_header.timestamp = 0x12345678;
1509 rtp_header.ssrc = 0x87654321;
1510
1511 EXPECT_CALL(*mock_neteq_controller_, RegisterEmptyPacket());
1512 neteq_->InsertEmptyPacket(rtp_header);
1513 }
1514
TEST_F(NetEqImplTest,EnableRtxHandling)1515 TEST_F(NetEqImplTest, EnableRtxHandling) {
1516 UseNoMocks();
1517 use_mock_neteq_controller_ = true;
1518 config_.enable_rtx_handling = true;
1519 CreateInstance();
1520 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1521 .Times(1)
1522 .WillOnce(Return(NetEq::Operation::kNormal));
1523
1524 const int kPayloadLengthSamples = 80;
1525 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1526 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1527 uint8_t payload[kPayloadLengthBytes] = {0};
1528 RTPHeader rtp_header;
1529 rtp_header.payloadType = kPayloadType;
1530 rtp_header.sequenceNumber = 0x1234;
1531 rtp_header.timestamp = 0x12345678;
1532 rtp_header.ssrc = 0x87654321;
1533
1534 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1535 SdpAudioFormat("l16", 8000, 1)));
1536 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1537 AudioFrame output;
1538 bool muted;
1539 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1540
1541 // Insert second packet that was sent before the first packet.
1542 rtp_header.sequenceNumber -= 1;
1543 rtp_header.timestamp -= kPayloadLengthSamples;
1544 EXPECT_CALL(*mock_neteq_controller_,
1545 PacketArrived(
1546 /*last_cng_or_dtmf*/ _,
1547 /*packet_length_samples*/ kPayloadLengthSamples,
1548 /*should_update_stats*/ _,
1549 /*main_sequence_number*/ rtp_header.sequenceNumber,
1550 /*main_timestamp*/ rtp_header.timestamp,
1551 /*fs_hz*/ 8000));
1552
1553 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1554 }
1555
1556 class Decoder120ms : public AudioDecoder {
1557 public:
Decoder120ms(int sample_rate_hz,SpeechType speech_type)1558 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1559 : sample_rate_hz_(sample_rate_hz),
1560 next_value_(1),
1561 speech_type_(speech_type) {}
1562
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)1563 int DecodeInternal(const uint8_t* encoded,
1564 size_t encoded_len,
1565 int sample_rate_hz,
1566 int16_t* decoded,
1567 SpeechType* speech_type) override {
1568 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
1569 size_t decoded_len =
1570 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1571 for (size_t i = 0; i < decoded_len; ++i) {
1572 decoded[i] = next_value_++;
1573 }
1574 *speech_type = speech_type_;
1575 return rtc::checked_cast<int>(decoded_len);
1576 }
1577
Reset()1578 void Reset() override { next_value_ = 1; }
SampleRateHz() const1579 int SampleRateHz() const override { return sample_rate_hz_; }
Channels() const1580 size_t Channels() const override { return 2; }
1581
1582 private:
1583 int sample_rate_hz_;
1584 int16_t next_value_;
1585 SpeechType speech_type_;
1586 };
1587
1588 class NetEqImplTest120ms : public NetEqImplTest {
1589 protected:
NetEqImplTest120ms()1590 NetEqImplTest120ms() : NetEqImplTest() {}
~NetEqImplTest120ms()1591 virtual ~NetEqImplTest120ms() {}
1592
CreateInstanceNoMocks()1593 void CreateInstanceNoMocks() {
1594 UseNoMocks();
1595 CreateInstance(decoder_factory_);
1596 EXPECT_TRUE(neteq_->RegisterPayloadType(
1597 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
1598 }
1599
CreateInstanceWithDelayManagerMock()1600 void CreateInstanceWithDelayManagerMock() {
1601 UseNoMocks();
1602 use_mock_neteq_controller_ = true;
1603 CreateInstance(decoder_factory_);
1604 EXPECT_TRUE(neteq_->RegisterPayloadType(
1605 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
1606 }
1607
timestamp_diff_between_packets() const1608 uint32_t timestamp_diff_between_packets() const {
1609 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1610 }
1611
first_timestamp() const1612 uint32_t first_timestamp() const { return 10u; }
1613
GetFirstPacket()1614 void GetFirstPacket() {
1615 bool muted;
1616 for (int i = 0; i < 12; i++) {
1617 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1618 EXPECT_FALSE(muted);
1619 }
1620 }
1621
InsertPacket(uint32_t timestamp)1622 void InsertPacket(uint32_t timestamp) {
1623 RTPHeader rtp_header;
1624 rtp_header.payloadType = kPayloadType;
1625 rtp_header.sequenceNumber = sequence_number_;
1626 rtp_header.timestamp = timestamp;
1627 rtp_header.ssrc = 15;
1628 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1629 uint8_t payload[kPayloadLengthBytes] = {0};
1630 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
1631 sequence_number_++;
1632 }
1633
Register120msCodec(AudioDecoder::SpeechType speech_type)1634 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
1635 const uint32_t sampling_freq = kSamplingFreq_;
1636 decoder_factory_ =
1637 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1638 [sampling_freq, speech_type]() {
1639 std::unique_ptr<AudioDecoder> decoder =
1640 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
1641 RTC_CHECK_EQ(2, decoder->Channels());
1642 return decoder;
1643 });
1644 }
1645
1646 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
1647 AudioFrame output_;
1648 const uint32_t kPayloadType = 17;
1649 const uint32_t kSamplingFreq_ = 48000;
1650 uint16_t sequence_number_ = 1;
1651 };
1652
TEST_F(NetEqImplTest120ms,CodecInternalCng)1653 TEST_F(NetEqImplTest120ms, CodecInternalCng) {
1654 Register120msCodec(AudioDecoder::kComfortNoise);
1655 CreateInstanceNoMocks();
1656
1657 InsertPacket(first_timestamp());
1658 GetFirstPacket();
1659
1660 bool muted;
1661 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1662 EXPECT_EQ(NetEq::Operation::kCodecInternalCng,
1663 neteq_->last_operation_for_test());
1664 }
1665
TEST_F(NetEqImplTest120ms,Normal)1666 TEST_F(NetEqImplTest120ms, Normal) {
1667 Register120msCodec(AudioDecoder::kSpeech);
1668 CreateInstanceNoMocks();
1669
1670 InsertPacket(first_timestamp());
1671 GetFirstPacket();
1672
1673 EXPECT_EQ(NetEq::Operation::kNormal, neteq_->last_operation_for_test());
1674 }
1675
TEST_F(NetEqImplTest120ms,Merge)1676 TEST_F(NetEqImplTest120ms, Merge) {
1677 Register120msCodec(AudioDecoder::kSpeech);
1678 CreateInstanceWithDelayManagerMock();
1679
1680 EXPECT_CALL(*mock_neteq_controller_, CngOff()).WillRepeatedly(Return(true));
1681 InsertPacket(first_timestamp());
1682
1683 GetFirstPacket();
1684 bool muted;
1685 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1686 .WillOnce(Return(NetEq::Operation::kExpand));
1687 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1688
1689 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1690
1691 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1692 .WillOnce(Return(NetEq::Operation::kMerge));
1693
1694 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1695 EXPECT_EQ(NetEq::Operation::kMerge, neteq_->last_operation_for_test());
1696 }
1697
TEST_F(NetEqImplTest120ms,Expand)1698 TEST_F(NetEqImplTest120ms, Expand) {
1699 Register120msCodec(AudioDecoder::kSpeech);
1700 CreateInstanceNoMocks();
1701
1702 InsertPacket(first_timestamp());
1703 GetFirstPacket();
1704
1705 bool muted;
1706 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1707 EXPECT_EQ(NetEq::Operation::kExpand, neteq_->last_operation_for_test());
1708 }
1709
TEST_F(NetEqImplTest120ms,FastAccelerate)1710 TEST_F(NetEqImplTest120ms, FastAccelerate) {
1711 Register120msCodec(AudioDecoder::kSpeech);
1712 CreateInstanceWithDelayManagerMock();
1713
1714 InsertPacket(first_timestamp());
1715 GetFirstPacket();
1716 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1717
1718 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1719 .Times(1)
1720 .WillOnce(Return(NetEq::Operation::kFastAccelerate));
1721
1722 bool muted;
1723 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1724 EXPECT_EQ(NetEq::Operation::kFastAccelerate,
1725 neteq_->last_operation_for_test());
1726 }
1727
TEST_F(NetEqImplTest120ms,PreemptiveExpand)1728 TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
1729 Register120msCodec(AudioDecoder::kSpeech);
1730 CreateInstanceWithDelayManagerMock();
1731
1732 InsertPacket(first_timestamp());
1733 GetFirstPacket();
1734
1735 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1736
1737 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1738 .Times(1)
1739 .WillOnce(Return(NetEq::Operation::kPreemptiveExpand));
1740
1741 bool muted;
1742 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1743 EXPECT_EQ(NetEq::Operation::kPreemptiveExpand,
1744 neteq_->last_operation_for_test());
1745 }
1746
TEST_F(NetEqImplTest120ms,Accelerate)1747 TEST_F(NetEqImplTest120ms, Accelerate) {
1748 Register120msCodec(AudioDecoder::kSpeech);
1749 CreateInstanceWithDelayManagerMock();
1750
1751 InsertPacket(first_timestamp());
1752 GetFirstPacket();
1753
1754 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1755
1756 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1757 .Times(1)
1758 .WillOnce(Return(NetEq::Operation::kAccelerate));
1759
1760 bool muted;
1761 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1762 EXPECT_EQ(NetEq::Operation::kAccelerate, neteq_->last_operation_for_test());
1763 }
1764
1765 } // namespace webrtc
1766