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 <memory>
12
13 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
14 #include "common_types.h" // NOLINT(build/include)
15 #include "modules/audio_coding/neteq/accelerate.h"
16 #include "modules/audio_coding/neteq/expand.h"
17 #include "modules/audio_coding/neteq/include/neteq.h"
18 #include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
19 #include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
20 #include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
21 #include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
22 #include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
23 #include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
24 #include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
25 #include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
26 #include "modules/audio_coding/neteq/neteq_impl.h"
27 #include "modules/audio_coding/neteq/preemptive_expand.h"
28 #include "modules/audio_coding/neteq/sync_buffer.h"
29 #include "modules/audio_coding/neteq/timestamp_scaler.h"
30 #include "modules/include/module_common_types.h"
31 #include "rtc_base/numerics/safe_conversions.h"
32 #include "test/gmock.h"
33 #include "test/gtest.h"
34 #include "test/mock_audio_decoder.h"
35 #include "test/mock_audio_decoder_factory.h"
36
37 using ::testing::AtLeast;
38 using ::testing::Return;
39 using ::testing::ReturnNull;
40 using ::testing::_;
41 using ::testing::SetArgPointee;
42 using ::testing::SetArrayArgument;
43 using ::testing::InSequence;
44 using ::testing::Invoke;
45 using ::testing::WithArg;
46 using ::testing::Pointee;
47 using ::testing::IsNull;
48
49 namespace webrtc {
50
51 // This function is called when inserting a packet list into the mock packet
52 // buffer. The purpose is to delete all inserted packets properly, to avoid
53 // memory leaks in the test.
DeletePacketsAndReturnOk(PacketList * packet_list)54 int DeletePacketsAndReturnOk(PacketList* packet_list) {
55 packet_list->clear();
56 return PacketBuffer::kOK;
57 }
58
59 class NetEqImplTest : public ::testing::Test {
60 protected:
NetEqImplTest()61 NetEqImplTest() { config_.sample_rate_hz = 8000; }
62
CreateInstance()63 void CreateInstance() {
64 NetEqImpl::Dependencies deps(config_, CreateBuiltinAudioDecoderFactory());
65
66 // Get a local pointer to NetEq's TickTimer object.
67 tick_timer_ = deps.tick_timer.get();
68
69 if (use_mock_buffer_level_filter_) {
70 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
71 mock_buffer_level_filter_ = mock.get();
72 deps.buffer_level_filter = std::move(mock);
73 }
74 buffer_level_filter_ = deps.buffer_level_filter.get();
75
76 if (use_mock_decoder_database_) {
77 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
78 mock_decoder_database_ = mock.get();
79 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
80 .WillOnce(ReturnNull());
81 deps.decoder_database = std::move(mock);
82 }
83 decoder_database_ = deps.decoder_database.get();
84
85 if (use_mock_delay_peak_detector_) {
86 std::unique_ptr<MockDelayPeakDetector> mock(
87 new MockDelayPeakDetector(tick_timer_));
88 mock_delay_peak_detector_ = mock.get();
89 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
90 deps.delay_peak_detector = std::move(mock);
91 }
92 delay_peak_detector_ = deps.delay_peak_detector.get();
93
94 if (use_mock_delay_manager_) {
95 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
96 config_.max_packets_in_buffer, delay_peak_detector_, tick_timer_));
97 mock_delay_manager_ = mock.get();
98 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
99 deps.delay_manager = std::move(mock);
100 }
101 delay_manager_ = deps.delay_manager.get();
102
103 if (use_mock_dtmf_buffer_) {
104 std::unique_ptr<MockDtmfBuffer> mock(
105 new MockDtmfBuffer(config_.sample_rate_hz));
106 mock_dtmf_buffer_ = mock.get();
107 deps.dtmf_buffer = std::move(mock);
108 }
109 dtmf_buffer_ = deps.dtmf_buffer.get();
110
111 if (use_mock_dtmf_tone_generator_) {
112 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
113 mock_dtmf_tone_generator_ = mock.get();
114 deps.dtmf_tone_generator = std::move(mock);
115 }
116 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
117
118 if (use_mock_packet_buffer_) {
119 std::unique_ptr<MockPacketBuffer> mock(
120 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
121 mock_packet_buffer_ = mock.get();
122 deps.packet_buffer = std::move(mock);
123 }
124 packet_buffer_ = deps.packet_buffer.get();
125
126 if (use_mock_payload_splitter_) {
127 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
128 mock_payload_splitter_ = mock.get();
129 deps.red_payload_splitter = std::move(mock);
130 }
131 red_payload_splitter_ = deps.red_payload_splitter.get();
132
133 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
134 new TimestampScaler(*deps.decoder_database.get()));
135
136 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
137 ASSERT_TRUE(neteq_ != NULL);
138 }
139
UseNoMocks()140 void UseNoMocks() {
141 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
142 use_mock_buffer_level_filter_ = false;
143 use_mock_decoder_database_ = false;
144 use_mock_delay_peak_detector_ = false;
145 use_mock_delay_manager_ = false;
146 use_mock_dtmf_buffer_ = false;
147 use_mock_dtmf_tone_generator_ = false;
148 use_mock_packet_buffer_ = false;
149 use_mock_payload_splitter_ = false;
150 }
151
~NetEqImplTest()152 virtual ~NetEqImplTest() {
153 if (use_mock_buffer_level_filter_) {
154 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
155 }
156 if (use_mock_decoder_database_) {
157 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
158 }
159 if (use_mock_delay_manager_) {
160 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
161 }
162 if (use_mock_delay_peak_detector_) {
163 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
164 }
165 if (use_mock_dtmf_buffer_) {
166 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
167 }
168 if (use_mock_dtmf_tone_generator_) {
169 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
170 }
171 if (use_mock_packet_buffer_) {
172 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
173 }
174 }
175
TestDtmfPacket(NetEqDecoder decoder_type)176 void TestDtmfPacket(NetEqDecoder decoder_type) {
177 const size_t kPayloadLength = 4;
178 const uint8_t kPayloadType = 110;
179 const uint32_t kReceiveTime = 17;
180 const int kSampleRateHz = 16000;
181 config_.sample_rate_hz = kSampleRateHz;
182 UseNoMocks();
183 CreateInstance();
184 // Event: 2, E bit, Volume: 17, Length: 4336.
185 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
186 RTPHeader rtp_header;
187 rtp_header.payloadType = kPayloadType;
188 rtp_header.sequenceNumber = 0x1234;
189 rtp_header.timestamp = 0x12345678;
190 rtp_header.ssrc = 0x87654321;
191
192 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
193 decoder_type, "telephone-event", kPayloadType));
194
195 // Insert first packet.
196 EXPECT_EQ(NetEq::kOK,
197 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
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 // Verify first 64 samples of actual output.
211 const std::vector<int16_t> kOutput({
212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
213 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
214 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
215 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
216 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
217 -2534, -1163 });
218 ASSERT_GE(kMaxOutputSize, kOutput.size());
219 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
220 }
221
222 std::unique_ptr<NetEqImpl> neteq_;
223 NetEq::Config config_;
224 TickTimer* tick_timer_ = nullptr;
225 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
226 BufferLevelFilter* buffer_level_filter_ = nullptr;
227 bool use_mock_buffer_level_filter_ = true;
228 MockDecoderDatabase* mock_decoder_database_ = nullptr;
229 DecoderDatabase* decoder_database_ = nullptr;
230 bool use_mock_decoder_database_ = true;
231 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
232 DelayPeakDetector* delay_peak_detector_ = nullptr;
233 bool use_mock_delay_peak_detector_ = true;
234 MockDelayManager* mock_delay_manager_ = nullptr;
235 DelayManager* delay_manager_ = nullptr;
236 bool use_mock_delay_manager_ = 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
252 // This tests the interface class NetEq.
253 // TODO(hlundin): Move to separate file?
TEST(NetEq,CreateAndDestroy)254 TEST(NetEq, CreateAndDestroy) {
255 NetEq::Config config;
256 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
257 delete neteq;
258 }
259
TEST_F(NetEqImplTest,RegisterPayloadTypeNetEqDecoder)260 TEST_F(NetEqImplTest, RegisterPayloadTypeNetEqDecoder) {
261 CreateInstance();
262 uint8_t rtp_payload_type = 0;
263 NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
264 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
265 EXPECT_CALL(*mock_decoder_database_,
266 RegisterPayload(rtp_payload_type, codec_type, kCodecName));
267 neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
268 }
269
TEST_F(NetEqImplTest,RegisterPayloadType)270 TEST_F(NetEqImplTest, RegisterPayloadType) {
271 CreateInstance();
272 constexpr int rtp_payload_type = 0;
273 const SdpAudioFormat format("pcmu", 8000, 1);
274 EXPECT_CALL(*mock_decoder_database_,
275 RegisterPayload(rtp_payload_type, format));
276 neteq_->RegisterPayloadType(rtp_payload_type, format);
277 }
278
TEST_F(NetEqImplTest,RemovePayloadType)279 TEST_F(NetEqImplTest, RemovePayloadType) {
280 CreateInstance();
281 uint8_t rtp_payload_type = 0;
282 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
283 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
284 // Check that kOK is returned when database returns kDecoderNotFound, because
285 // removing a payload type that was never registered is not an error.
286 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
287 }
288
TEST_F(NetEqImplTest,RemoveAllPayloadTypes)289 TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
290 CreateInstance();
291 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
292 neteq_->RemoveAllPayloadTypes();
293 }
294
TEST_F(NetEqImplTest,InsertPacket)295 TEST_F(NetEqImplTest, InsertPacket) {
296 CreateInstance();
297 const size_t kPayloadLength = 100;
298 const uint8_t kPayloadType = 0;
299 const uint16_t kFirstSequenceNumber = 0x1234;
300 const uint32_t kFirstTimestamp = 0x12345678;
301 const uint32_t kSsrc = 0x87654321;
302 const uint32_t kFirstReceiveTime = 17;
303 uint8_t payload[kPayloadLength] = {0};
304 RTPHeader rtp_header;
305 rtp_header.payloadType = kPayloadType;
306 rtp_header.sequenceNumber = kFirstSequenceNumber;
307 rtp_header.timestamp = kFirstTimestamp;
308 rtp_header.ssrc = kSsrc;
309 Packet fake_packet;
310 fake_packet.payload_type = kPayloadType;
311 fake_packet.sequence_number = kFirstSequenceNumber;
312 fake_packet.timestamp = kFirstTimestamp;
313
314 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
315 new rtc::RefCountedObject<MockAudioDecoderFactory>);
316 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _))
317 .WillOnce(Invoke([&](const SdpAudioFormat& format,
318 std::unique_ptr<AudioDecoder>* dec) {
319 EXPECT_EQ("pcmu", format.name);
320
321 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
322 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
323 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
324 // BWE update function called with first packet.
325 EXPECT_CALL(*mock_decoder,
326 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
327 kFirstTimestamp, kFirstReceiveTime));
328 // BWE update function called with second packet.
329 EXPECT_CALL(
330 *mock_decoder,
331 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
332 kFirstTimestamp + 160, kFirstReceiveTime + 155));
333 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
334
335 *dec = std::move(mock_decoder);
336 }));
337 DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu,
338 mock_decoder_factory);
339
340 // Expectations for decoder database.
341 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
342 .WillRepeatedly(Return(&info));
343
344 // Expectations for packet buffer.
345 EXPECT_CALL(*mock_packet_buffer_, Empty())
346 .WillOnce(Return(false)); // Called once after first packet is inserted.
347 EXPECT_CALL(*mock_packet_buffer_, Flush())
348 .Times(1);
349 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
350 .Times(2)
351 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
352 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
353 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
354 // index) is a pointer, and the variable pointed to is set to kPayloadType.
355 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
356 // packets in the list (to avoid memory leaks in the test).
357 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
358 .Times(1)
359 .WillOnce(Return(&fake_packet));
360
361 // Expectations for DTMF buffer.
362 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
363 .Times(1);
364
365 // Expectations for delay manager.
366 {
367 // All expectations within this block must be called in this specific order.
368 InSequence sequence; // Dummy variable.
369 // Expectations when the first packet is inserted.
370 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
371 .Times(2)
372 .WillRepeatedly(Return(-1));
373 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
374 .Times(1);
375 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
376 // Expectations when the second packet is inserted. Slightly different.
377 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
378 .WillOnce(Return(0));
379 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
380 .WillOnce(Return(0));
381 }
382
383 // Insert first packet.
384 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
385
386 // Insert second packet.
387 rtp_header.timestamp += 160;
388 rtp_header.sequenceNumber += 1;
389 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
390 }
391
TEST_F(NetEqImplTest,InsertPacketsUntilBufferIsFull)392 TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
393 UseNoMocks();
394 CreateInstance();
395
396 const int kPayloadLengthSamples = 80;
397 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
398 const uint8_t kPayloadType = 17; // Just an arbitrary number.
399 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
400 uint8_t payload[kPayloadLengthBytes] = {0};
401 RTPHeader rtp_header;
402 rtp_header.payloadType = kPayloadType;
403 rtp_header.sequenceNumber = 0x1234;
404 rtp_header.timestamp = 0x12345678;
405 rtp_header.ssrc = 0x87654321;
406
407 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
408 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
409
410 // Insert packets. The buffer should not flush.
411 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
412 EXPECT_EQ(NetEq::kOK,
413 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
414 rtp_header.timestamp += kPayloadLengthSamples;
415 rtp_header.sequenceNumber += 1;
416 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
417 }
418
419 // Insert one more packet and make sure the buffer got flushed. That is, it
420 // should only hold one single packet.
421 EXPECT_EQ(NetEq::kOK,
422 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
423 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
424 const Packet* test_packet = packet_buffer_->PeekNextPacket();
425 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
426 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
427 }
428
TEST_F(NetEqImplTest,TestDtmfPacketAVT)429 TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
430 TestDtmfPacket(NetEqDecoder::kDecoderAVT);
431 }
432
TEST_F(NetEqImplTest,TestDtmfPacketAVT16kHz)433 TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
434 TestDtmfPacket(NetEqDecoder::kDecoderAVT16kHz);
435 }
436
TEST_F(NetEqImplTest,TestDtmfPacketAVT32kHz)437 TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
438 TestDtmfPacket(NetEqDecoder::kDecoderAVT32kHz);
439 }
440
TEST_F(NetEqImplTest,TestDtmfPacketAVT48kHz)441 TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
442 TestDtmfPacket(NetEqDecoder::kDecoderAVT48kHz);
443 }
444
445 // This test verifies that timestamps propagate from the incoming packets
446 // through to the sync buffer and to the playout timestamp.
TEST_F(NetEqImplTest,VerifyTimestampPropagation)447 TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
448 UseNoMocks();
449 CreateInstance();
450
451 const uint8_t kPayloadType = 17; // Just an arbitrary number.
452 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
453 const int kSampleRateHz = 8000;
454 const size_t kPayloadLengthSamples =
455 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
456 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
457 uint8_t payload[kPayloadLengthBytes] = {0};
458 RTPHeader rtp_header;
459 rtp_header.payloadType = kPayloadType;
460 rtp_header.sequenceNumber = 0x1234;
461 rtp_header.timestamp = 0x12345678;
462 rtp_header.ssrc = 0x87654321;
463
464 // This is a dummy decoder that produces as many output samples as the input
465 // has bytes. The output is an increasing series, starting at 1 for the first
466 // sample, and then increasing by 1 for each sample.
467 class CountingSamplesDecoder : public AudioDecoder {
468 public:
469 CountingSamplesDecoder() : next_value_(1) {}
470
471 // Produce as many samples as input bytes (|encoded_len|).
472 int DecodeInternal(const uint8_t* encoded,
473 size_t encoded_len,
474 int /* sample_rate_hz */,
475 int16_t* decoded,
476 SpeechType* speech_type) override {
477 for (size_t i = 0; i < encoded_len; ++i) {
478 decoded[i] = next_value_++;
479 }
480 *speech_type = kSpeech;
481 return rtc::checked_cast<int>(encoded_len);
482 }
483
484 void Reset() override { next_value_ = 1; }
485
486 int SampleRateHz() const override { return kSampleRateHz; }
487
488 size_t Channels() const override { return 1; }
489
490 uint16_t next_value() const { return next_value_; }
491
492 private:
493 int16_t next_value_;
494 } decoder_;
495
496 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
497 &decoder_, NetEqDecoder::kDecoderPCM16B,
498 "dummy name", kPayloadType));
499
500 // Insert one packet.
501 EXPECT_EQ(NetEq::kOK,
502 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
503
504 // Pull audio once.
505 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
506 AudioFrame output;
507 bool muted;
508 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
509 ASSERT_FALSE(muted);
510 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
511 EXPECT_EQ(1u, output.num_channels_);
512 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
513
514 // Start with a simple check that the fake decoder is behaving as expected.
515 EXPECT_EQ(kPayloadLengthSamples,
516 static_cast<size_t>(decoder_.next_value() - 1));
517
518 // The value of the last of the output samples is the same as the number of
519 // samples played from the decoded packet. Thus, this number + the RTP
520 // timestamp should match the playout timestamp.
521 // Wrap the expected value in an rtc::Optional to compare them as such.
522 EXPECT_EQ(
523 rtc::Optional<uint32_t>(rtp_header.timestamp +
524 output.data()[output.samples_per_channel_ - 1]),
525 neteq_->GetPlayoutTimestamp());
526
527 // Check the timestamp for the last value in the sync buffer. This should
528 // be one full frame length ahead of the RTP timestamp.
529 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
530 ASSERT_TRUE(sync_buffer != NULL);
531 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
532 sync_buffer->end_timestamp());
533
534 // Check that the number of samples still to play from the sync buffer add
535 // up with what was already played out.
536 EXPECT_EQ(
537 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
538 sync_buffer->FutureLength());
539 }
540
TEST_F(NetEqImplTest,ReorderedPacket)541 TEST_F(NetEqImplTest, ReorderedPacket) {
542 UseNoMocks();
543 CreateInstance();
544
545 const uint8_t kPayloadType = 17; // Just an arbitrary number.
546 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
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
558 // Create a mock decoder object.
559 MockAudioDecoder mock_decoder;
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, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
565 .WillRepeatedly(Return(0));
566 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
567 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
568 int16_t dummy_output[kPayloadLengthSamples] = {0};
569 // The below expectation will make the mock decoder write
570 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
571 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
572 kSampleRateHz, _, _))
573 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
574 dummy_output + kPayloadLengthSamples),
575 SetArgPointee<4>(AudioDecoder::kSpeech),
576 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
577 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
578 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
579 "dummy name", kPayloadType));
580
581 // Insert one packet.
582 EXPECT_EQ(NetEq::kOK,
583 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
584
585 // Pull audio once.
586 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
587 AudioFrame output;
588 bool muted;
589 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
590 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
591 EXPECT_EQ(1u, output.num_channels_);
592 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
593
594 // Insert two more packets. The first one is out of order, and is already too
595 // old, the second one is the expected next packet.
596 rtp_header.sequenceNumber -= 1;
597 rtp_header.timestamp -= kPayloadLengthSamples;
598 payload[0] = 1;
599 EXPECT_EQ(NetEq::kOK,
600 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
601 rtp_header.sequenceNumber += 2;
602 rtp_header.timestamp += 2 * kPayloadLengthSamples;
603 payload[0] = 2;
604 EXPECT_EQ(NetEq::kOK,
605 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
606
607 // Expect only the second packet to be decoded (the one with "2" as the first
608 // payload byte).
609 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
610 kSampleRateHz, _, _))
611 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
612 dummy_output + kPayloadLengthSamples),
613 SetArgPointee<4>(AudioDecoder::kSpeech),
614 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
615
616 // Pull audio once.
617 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
618 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
619 EXPECT_EQ(1u, output.num_channels_);
620 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
621
622 // Now check the packet buffer, and make sure it is empty, since the
623 // out-of-order packet should have been discarded.
624 EXPECT_TRUE(packet_buffer_->Empty());
625
626 EXPECT_CALL(mock_decoder, Die());
627 }
628
629 // This test verifies that NetEq can handle the situation where the first
630 // incoming packet is rejected.
TEST_F(NetEqImplTest,FirstPacketUnknown)631 TEST_F(NetEqImplTest, FirstPacketUnknown) {
632 UseNoMocks();
633 CreateInstance();
634
635 const uint8_t kPayloadType = 17; // Just an arbitrary number.
636 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
637 const int kSampleRateHz = 8000;
638 const size_t kPayloadLengthSamples =
639 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
640 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
641 uint8_t payload[kPayloadLengthBytes] = {0};
642 RTPHeader rtp_header;
643 rtp_header.payloadType = kPayloadType;
644 rtp_header.sequenceNumber = 0x1234;
645 rtp_header.timestamp = 0x12345678;
646 rtp_header.ssrc = 0x87654321;
647
648 // Insert one packet. Note that we have not registered any payload type, so
649 // this packet will be rejected.
650 EXPECT_EQ(NetEq::kFail,
651 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
652
653 // Pull audio once.
654 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
655 AudioFrame output;
656 bool muted;
657 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
658 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
659 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
660 EXPECT_EQ(1u, output.num_channels_);
661 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
662
663 // Register the payload type.
664 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
665 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
666
667 // Insert 10 packets.
668 for (size_t i = 0; i < 10; ++i) {
669 rtp_header.sequenceNumber++;
670 rtp_header.timestamp += kPayloadLengthSamples;
671 EXPECT_EQ(NetEq::kOK,
672 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
673 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
674 }
675
676 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
677 for (size_t i = 0; i < 3; ++i) {
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::kNormalSpeech, output.speech_type_)
683 << "NetEq did not decode the packets as expected.";
684 }
685 }
686
687 // This test verifies that NetEq can handle comfort noise and enters/quits codec
688 // internal CNG mode properly.
TEST_F(NetEqImplTest,CodecInternalCng)689 TEST_F(NetEqImplTest, CodecInternalCng) {
690 UseNoMocks();
691 CreateInstance();
692
693 const uint8_t kPayloadType = 17; // Just an arbitrary number.
694 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
695 const int kSampleRateKhz = 48;
696 const size_t kPayloadLengthSamples =
697 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
698 const size_t kPayloadLengthBytes = 10;
699 uint8_t payload[kPayloadLengthBytes] = {0};
700 int16_t dummy_output[kPayloadLengthSamples] = {0};
701
702 RTPHeader rtp_header;
703 rtp_header.payloadType = kPayloadType;
704 rtp_header.sequenceNumber = 0x1234;
705 rtp_header.timestamp = 0x12345678;
706 rtp_header.ssrc = 0x87654321;
707
708 // Create a mock decoder object.
709 MockAudioDecoder mock_decoder;
710 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
711 EXPECT_CALL(mock_decoder, SampleRateHz())
712 .WillRepeatedly(Return(kSampleRateKhz * 1000));
713 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
714 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
715 .WillRepeatedly(Return(0));
716 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
717 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
718 // Packed duration when asking the decoder for more CNG data (without a new
719 // packet).
720 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
721 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
722
723 // Pointee(x) verifies that first byte of the payload equals x, this makes it
724 // possible to verify that the correct payload is fed to Decode().
725 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
726 kSampleRateKhz * 1000, _, _))
727 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
728 dummy_output + kPayloadLengthSamples),
729 SetArgPointee<4>(AudioDecoder::kSpeech),
730 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
731
732 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
733 kSampleRateKhz * 1000, _, _))
734 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
735 dummy_output + kPayloadLengthSamples),
736 SetArgPointee<4>(AudioDecoder::kComfortNoise),
737 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
738
739 EXPECT_CALL(mock_decoder,
740 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
741 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
742 dummy_output + kPayloadLengthSamples),
743 SetArgPointee<4>(AudioDecoder::kComfortNoise),
744 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
745
746 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
747 kSampleRateKhz * 1000, _, _))
748 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
749 dummy_output + kPayloadLengthSamples),
750 SetArgPointee<4>(AudioDecoder::kSpeech),
751 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
752
753 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
754 &mock_decoder, NetEqDecoder::kDecoderOpus,
755 "dummy name", kPayloadType));
756
757 // Insert one packet (decoder will return speech).
758 EXPECT_EQ(NetEq::kOK,
759 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
760
761 // Insert second packet (decoder will return CNG).
762 payload[0] = 1;
763 rtp_header.sequenceNumber++;
764 rtp_header.timestamp += kPayloadLengthSamples;
765 EXPECT_EQ(NetEq::kOK,
766 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
767
768 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
769 AudioFrame output;
770 AudioFrame::SpeechType expected_type[8] = {
771 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
772 AudioFrame::kCNG, AudioFrame::kCNG,
773 AudioFrame::kCNG, AudioFrame::kCNG,
774 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
775 };
776 int expected_timestamp_increment[8] = {
777 -1, // will not be used.
778 10 * kSampleRateKhz,
779 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
780 -1, -1,
781 50 * kSampleRateKhz, 10 * kSampleRateKhz
782 };
783
784 bool muted;
785 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
786 rtc::Optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
787 ASSERT_TRUE(last_timestamp);
788
789 // Lambda for verifying the timestamps.
790 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
791 rtc::Optional<uint32_t> ts, size_t i) {
792 if (expected_timestamp_increment[i] == -1) {
793 // Expect to get an empty timestamp value during CNG and PLC.
794 EXPECT_FALSE(ts) << "i = " << i;
795 } else {
796 ASSERT_TRUE(ts) << "i = " << i;
797 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
798 << "i = " << i;
799 last_timestamp = ts;
800 }
801 };
802
803 for (size_t i = 1; i < 6; ++i) {
804 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
805 EXPECT_EQ(1u, output.num_channels_);
806 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
807 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
808 SCOPED_TRACE("");
809 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
810 }
811
812 // Insert third packet, which leaves a gap from last packet.
813 payload[0] = 2;
814 rtp_header.sequenceNumber += 2;
815 rtp_header.timestamp += 2 * kPayloadLengthSamples;
816 EXPECT_EQ(NetEq::kOK,
817 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
818
819 for (size_t i = 6; i < 8; ++i) {
820 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
821 EXPECT_EQ(1u, output.num_channels_);
822 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
823 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
824 SCOPED_TRACE("");
825 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
826 }
827
828 // Now check the packet buffer, and make sure it is empty.
829 EXPECT_TRUE(packet_buffer_->Empty());
830
831 EXPECT_CALL(mock_decoder, Die());
832 }
833
TEST_F(NetEqImplTest,UnsupportedDecoder)834 TEST_F(NetEqImplTest, UnsupportedDecoder) {
835 UseNoMocks();
836 CreateInstance();
837 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
838 static const size_t kChannels = 2;
839
840 const uint8_t kPayloadType = 17; // Just an arbitrary number.
841 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
842 const int kSampleRateHz = 8000;
843
844 const size_t kPayloadLengthSamples =
845 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
846 const size_t kPayloadLengthBytes = 1;
847 uint8_t payload[kPayloadLengthBytes] = {0};
848 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
849 RTPHeader rtp_header;
850 rtp_header.payloadType = kPayloadType;
851 rtp_header.sequenceNumber = 0x1234;
852 rtp_header.timestamp = 0x12345678;
853 rtp_header.ssrc = 0x87654321;
854
855 ::testing::NiceMock<MockAudioDecoder> decoder;
856
857 const uint8_t kFirstPayloadValue = 1;
858 const uint8_t kSecondPayloadValue = 2;
859
860 EXPECT_CALL(decoder,
861 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
862 .Times(AtLeast(1))
863 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
864
865 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
866 .Times(0);
867
868 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
869 kPayloadLengthBytes, kSampleRateHz, _, _))
870 .Times(1)
871 .WillOnce(DoAll(
872 SetArrayArgument<3>(dummy_output,
873 dummy_output + kPayloadLengthSamples * kChannels),
874 SetArgPointee<4>(AudioDecoder::kSpeech),
875 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
876
877 EXPECT_CALL(decoder,
878 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
879 .Times(AtLeast(1))
880 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
881
882 EXPECT_CALL(decoder, SampleRateHz())
883 .WillRepeatedly(Return(kSampleRateHz));
884
885 EXPECT_CALL(decoder, Channels())
886 .WillRepeatedly(Return(kChannels));
887
888 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
889 &decoder, NetEqDecoder::kDecoderPCM16B,
890 "dummy name", kPayloadType));
891
892 // Insert one packet.
893 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
894 EXPECT_EQ(NetEq::kOK,
895 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
896
897 // Insert another packet.
898 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
899 rtp_header.sequenceNumber++;
900 // The second timestamp needs to be at least 30 ms after the first to make
901 // the second packet get decoded.
902 rtp_header.timestamp += 3 * kPayloadLengthSamples;
903 EXPECT_EQ(NetEq::kOK,
904 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
905
906 AudioFrame output;
907 bool muted;
908 // First call to GetAudio will try to decode the "faulty" packet.
909 // Expect kFail return value.
910 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
911 // Output size and number of channels should be correct.
912 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
913 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
914 EXPECT_EQ(kChannels, output.num_channels_);
915
916 // Second call to GetAudio will decode the packet that is ok. No errors are
917 // expected.
918 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
919 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
920 EXPECT_EQ(kChannels, output.num_channels_);
921
922 // Die isn't called through NiceMock (since it's called by the
923 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
924 EXPECT_CALL(decoder, Die());
925 }
926
927 // This test inserts packets until the buffer is flushed. After that, it asks
928 // NetEq for the network statistics. The purpose of the test is to make sure
929 // that even though the buffer size increment is negative (which it becomes when
930 // the packet causing a flush is inserted), the packet length stored in the
931 // decision logic remains valid.
TEST_F(NetEqImplTest,FloodBufferAndGetNetworkStats)932 TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
933 UseNoMocks();
934 CreateInstance();
935
936 const size_t kPayloadLengthSamples = 80;
937 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
938 const uint8_t kPayloadType = 17; // Just an arbitrary number.
939 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
940 uint8_t payload[kPayloadLengthBytes] = {0};
941 RTPHeader rtp_header;
942 rtp_header.payloadType = kPayloadType;
943 rtp_header.sequenceNumber = 0x1234;
944 rtp_header.timestamp = 0x12345678;
945 rtp_header.ssrc = 0x87654321;
946
947 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
948 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
949
950 // Insert packets until the buffer flushes.
951 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
952 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
953 EXPECT_EQ(NetEq::kOK,
954 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
955 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
956 ++rtp_header.sequenceNumber;
957 }
958 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
959
960 // Ask for network statistics. This should not crash.
961 NetEqNetworkStatistics stats;
962 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
963 }
964
TEST_F(NetEqImplTest,DecodedPayloadTooShort)965 TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
966 UseNoMocks();
967 CreateInstance();
968
969 const uint8_t kPayloadType = 17; // Just an arbitrary number.
970 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
971 const int kSampleRateHz = 8000;
972 const size_t kPayloadLengthSamples =
973 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
974 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
975 uint8_t payload[kPayloadLengthBytes] = {0};
976 RTPHeader rtp_header;
977 rtp_header.payloadType = kPayloadType;
978 rtp_header.sequenceNumber = 0x1234;
979 rtp_header.timestamp = 0x12345678;
980 rtp_header.ssrc = 0x87654321;
981
982 // Create a mock decoder object.
983 MockAudioDecoder mock_decoder;
984 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
985 EXPECT_CALL(mock_decoder, SampleRateHz())
986 .WillRepeatedly(Return(kSampleRateHz));
987 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
988 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
989 .WillRepeatedly(Return(0));
990 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
991 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
992 int16_t dummy_output[kPayloadLengthSamples] = {0};
993 // The below expectation will make the mock decoder write
994 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
995 // speech. That is, the decoded length is 5 samples shorter than the expected.
996 EXPECT_CALL(mock_decoder,
997 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
998 .WillOnce(
999 DoAll(SetArrayArgument<3>(dummy_output,
1000 dummy_output + kPayloadLengthSamples - 5),
1001 SetArgPointee<4>(AudioDecoder::kSpeech),
1002 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
1003 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1004 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
1005 "dummy name", kPayloadType));
1006
1007 // Insert one packet.
1008 EXPECT_EQ(NetEq::kOK,
1009 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1010
1011 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1012
1013 // Pull audio once.
1014 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
1015 AudioFrame output;
1016 bool muted;
1017 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1018 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1019 EXPECT_EQ(1u, output.num_channels_);
1020 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
1021
1022 EXPECT_CALL(mock_decoder, Die());
1023 }
1024
1025 // This test checks the behavior of NetEq when audio decoder fails.
TEST_F(NetEqImplTest,DecodingError)1026 TEST_F(NetEqImplTest, DecodingError) {
1027 UseNoMocks();
1028 CreateInstance();
1029
1030 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1031 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1032 const int kSampleRateHz = 8000;
1033 const int kDecoderErrorCode = -97; // Any negative number.
1034
1035 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1036 const size_t kFrameLengthSamples =
1037 static_cast<size_t>(5 * kSampleRateHz / 1000);
1038
1039 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1040
1041 uint8_t payload[kPayloadLengthBytes] = {0};
1042
1043 RTPHeader rtp_header;
1044 rtp_header.payloadType = kPayloadType;
1045 rtp_header.sequenceNumber = 0x1234;
1046 rtp_header.timestamp = 0x12345678;
1047 rtp_header.ssrc = 0x87654321;
1048
1049 // Create a mock decoder object.
1050 MockAudioDecoder mock_decoder;
1051 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
1052 EXPECT_CALL(mock_decoder, SampleRateHz())
1053 .WillRepeatedly(Return(kSampleRateHz));
1054 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1055 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1056 .WillRepeatedly(Return(0));
1057 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
1058 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
1059 EXPECT_CALL(mock_decoder, ErrorCode())
1060 .WillOnce(Return(kDecoderErrorCode));
1061 EXPECT_CALL(mock_decoder, HasDecodePlc())
1062 .WillOnce(Return(false));
1063 int16_t dummy_output[kFrameLengthSamples] = {0};
1064
1065 {
1066 InSequence sequence; // Dummy variable.
1067 // Mock decoder works normally the first time.
1068 EXPECT_CALL(mock_decoder,
1069 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1070 .Times(3)
1071 .WillRepeatedly(
1072 DoAll(SetArrayArgument<3>(dummy_output,
1073 dummy_output + kFrameLengthSamples),
1074 SetArgPointee<4>(AudioDecoder::kSpeech),
1075 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
1076 .RetiresOnSaturation();
1077
1078 // Then mock decoder fails. A common reason for failure can be buffer being
1079 // too short
1080 EXPECT_CALL(mock_decoder,
1081 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1082 .WillOnce(Return(-1))
1083 .RetiresOnSaturation();
1084
1085 // Mock decoder finally returns to normal.
1086 EXPECT_CALL(mock_decoder,
1087 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1088 .Times(2)
1089 .WillRepeatedly(
1090 DoAll(SetArrayArgument<3>(dummy_output,
1091 dummy_output + kFrameLengthSamples),
1092 SetArgPointee<4>(AudioDecoder::kSpeech),
1093 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
1094 }
1095
1096 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1097 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
1098 "dummy name", kPayloadType));
1099
1100 // Insert packets.
1101 for (int i = 0; i < 6; ++i) {
1102 rtp_header.sequenceNumber += 1;
1103 rtp_header.timestamp += kFrameLengthSamples;
1104 EXPECT_EQ(NetEq::kOK,
1105 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1106 }
1107
1108 // Pull audio.
1109 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
1110 AudioFrame output;
1111 bool muted;
1112 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1113 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1114 EXPECT_EQ(1u, output.num_channels_);
1115 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
1116
1117 // Pull audio again. Decoder fails.
1118 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
1119 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1120 EXPECT_EQ(1u, output.num_channels_);
1121 // We are not expecting anything for output.speech_type_, since an error was
1122 // returned.
1123
1124 // Pull audio again, should continue an expansion.
1125 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1126 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1127 EXPECT_EQ(1u, output.num_channels_);
1128 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
1129
1130 // Pull audio again, should behave normal.
1131 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1132 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1133 EXPECT_EQ(1u, output.num_channels_);
1134 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
1135
1136 EXPECT_CALL(mock_decoder, Die());
1137 }
1138
1139 // This test checks the behavior of NetEq when audio decoder fails during CNG.
TEST_F(NetEqImplTest,DecodingErrorDuringInternalCng)1140 TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1141 UseNoMocks();
1142 CreateInstance();
1143
1144 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1145 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1146 const int kSampleRateHz = 8000;
1147 const int kDecoderErrorCode = -97; // Any negative number.
1148
1149 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1150 const size_t kFrameLengthSamples =
1151 static_cast<size_t>(5 * kSampleRateHz / 1000);
1152
1153 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1154
1155 uint8_t payload[kPayloadLengthBytes] = {0};
1156
1157 RTPHeader rtp_header;
1158 rtp_header.payloadType = kPayloadType;
1159 rtp_header.sequenceNumber = 0x1234;
1160 rtp_header.timestamp = 0x12345678;
1161 rtp_header.ssrc = 0x87654321;
1162
1163 // Create a mock decoder object.
1164 MockAudioDecoder mock_decoder;
1165 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
1166 EXPECT_CALL(mock_decoder, SampleRateHz())
1167 .WillRepeatedly(Return(kSampleRateHz));
1168 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1169 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1170 .WillRepeatedly(Return(0));
1171 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
1172 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
1173 EXPECT_CALL(mock_decoder, ErrorCode())
1174 .WillOnce(Return(kDecoderErrorCode));
1175 int16_t dummy_output[kFrameLengthSamples] = {0};
1176
1177 {
1178 InSequence sequence; // Dummy variable.
1179 // Mock decoder works normally the first 2 times.
1180 EXPECT_CALL(mock_decoder,
1181 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
1182 .Times(2)
1183 .WillRepeatedly(
1184 DoAll(SetArrayArgument<3>(dummy_output,
1185 dummy_output + kFrameLengthSamples),
1186 SetArgPointee<4>(AudioDecoder::kComfortNoise),
1187 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
1188 .RetiresOnSaturation();
1189
1190 // Then mock decoder fails. A common reason for failure can be buffer being
1191 // too short
1192 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
1193 .WillOnce(Return(-1))
1194 .RetiresOnSaturation();
1195
1196 // Mock decoder finally returns to normal.
1197 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
1198 .Times(2)
1199 .WillRepeatedly(
1200 DoAll(SetArrayArgument<3>(dummy_output,
1201 dummy_output + kFrameLengthSamples),
1202 SetArgPointee<4>(AudioDecoder::kComfortNoise),
1203 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
1204 }
1205
1206 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1207 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
1208 "dummy name", kPayloadType));
1209
1210 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1211 for (int i = 0; i < 2; ++i) {
1212 rtp_header.sequenceNumber += 1;
1213 rtp_header.timestamp += kFrameLengthSamples;
1214 EXPECT_EQ(NetEq::kOK,
1215 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
1216 }
1217
1218 // Pull audio.
1219 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
1220 AudioFrame output;
1221 bool muted;
1222 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1223 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1224 EXPECT_EQ(1u, output.num_channels_);
1225 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
1226
1227 // Pull audio again. Decoder fails.
1228 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
1229 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1230 EXPECT_EQ(1u, output.num_channels_);
1231 // We are not expecting anything for output.speech_type_, since an error was
1232 // returned.
1233
1234 // Pull audio again, should resume codec CNG.
1235 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1236 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1237 EXPECT_EQ(1u, output.num_channels_);
1238 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
1239
1240 EXPECT_CALL(mock_decoder, Die());
1241 }
1242
1243 // Tests that the return value from last_output_sample_rate_hz() is equal to the
1244 // configured inital sample rate.
TEST_F(NetEqImplTest,InitialLastOutputSampleRate)1245 TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1246 UseNoMocks();
1247 config_.sample_rate_hz = 48000;
1248 CreateInstance();
1249 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1250 }
1251
TEST_F(NetEqImplTest,TickTimerIncrement)1252 TEST_F(NetEqImplTest, TickTimerIncrement) {
1253 UseNoMocks();
1254 CreateInstance();
1255 ASSERT_TRUE(tick_timer_);
1256 EXPECT_EQ(0u, tick_timer_->ticks());
1257 AudioFrame output;
1258 bool muted;
1259 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1260 EXPECT_EQ(1u, tick_timer_->ticks());
1261 }
1262
TEST_F(NetEqImplTest,TargetDelayMs)1263 TEST_F(NetEqImplTest, TargetDelayMs) {
1264 UseNoMocks();
1265 use_mock_delay_manager_ = true;
1266 CreateInstance();
1267 // Let the dummy target delay be 17 packets.
1268 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1269 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1270 .WillOnce(Return(kTargetLevelPacketsQ8));
1271 // Default packet size before any packet has been decoded is 30 ms, so we are
1272 // expecting 17 * 30 = 510 ms target delay.
1273 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1274 }
1275
TEST_F(NetEqImplTest,InsertEmptyPacket)1276 TEST_F(NetEqImplTest, InsertEmptyPacket) {
1277 UseNoMocks();
1278 use_mock_delay_manager_ = true;
1279 CreateInstance();
1280
1281 RTPHeader rtp_header;
1282 rtp_header.payloadType = 17;
1283 rtp_header.sequenceNumber = 0x1234;
1284 rtp_header.timestamp = 0x12345678;
1285 rtp_header.ssrc = 0x87654321;
1286
1287 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1288 neteq_->InsertEmptyPacket(rtp_header);
1289 }
1290
1291 class Decoder120ms : public AudioDecoder {
1292 public:
Decoder120ms(int sample_rate_hz,SpeechType speech_type)1293 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1294 : sample_rate_hz_(sample_rate_hz),
1295 next_value_(1),
1296 speech_type_(speech_type) {}
1297
DecodeInternal(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,SpeechType * speech_type)1298 int DecodeInternal(const uint8_t* encoded,
1299 size_t encoded_len,
1300 int sample_rate_hz,
1301 int16_t* decoded,
1302 SpeechType* speech_type) override {
1303 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
1304 size_t decoded_len =
1305 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1306 for (size_t i = 0; i < decoded_len; ++i) {
1307 decoded[i] = next_value_++;
1308 }
1309 *speech_type = speech_type_;
1310 return rtc::checked_cast<int>(decoded_len);
1311 }
1312
Reset()1313 void Reset() override { next_value_ = 1; }
SampleRateHz() const1314 int SampleRateHz() const override { return sample_rate_hz_; }
Channels() const1315 size_t Channels() const override { return 2; }
1316
1317 private:
1318 int sample_rate_hz_;
1319 int16_t next_value_;
1320 SpeechType speech_type_;
1321 };
1322
1323 class NetEqImplTest120ms : public NetEqImplTest {
1324 protected:
NetEqImplTest120ms()1325 NetEqImplTest120ms() : NetEqImplTest() {}
~NetEqImplTest120ms()1326 virtual ~NetEqImplTest120ms() {}
1327
CreateInstanceNoMocks()1328 void CreateInstanceNoMocks() {
1329 UseNoMocks();
1330 CreateInstance();
1331 }
1332
CreateInstanceWithDelayManagerMock()1333 void CreateInstanceWithDelayManagerMock() {
1334 UseNoMocks();
1335 use_mock_delay_manager_ = true;
1336 CreateInstance();
1337 }
1338
timestamp_diff_between_packets() const1339 uint32_t timestamp_diff_between_packets() const {
1340 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1341 }
1342
first_timestamp() const1343 uint32_t first_timestamp() const { return 10u; }
1344
GetFirstPacket()1345 void GetFirstPacket() {
1346 bool muted;
1347 for (int i = 0; i < 12; i++) {
1348 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1349 EXPECT_FALSE(muted);
1350 }
1351 }
1352
InsertPacket(uint32_t timestamp)1353 void InsertPacket(uint32_t timestamp) {
1354 RTPHeader rtp_header;
1355 rtp_header.payloadType = kPayloadType;
1356 rtp_header.sequenceNumber = sequence_number_;
1357 rtp_header.timestamp = timestamp;
1358 rtp_header.ssrc = 15;
1359 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1360 uint8_t payload[kPayloadLengthBytes] = {0};
1361 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
1362 sequence_number_++;
1363 }
1364
Register120msCodec(AudioDecoder::SpeechType speech_type)1365 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
1366 decoder_.reset(new Decoder120ms(kSamplingFreq_, speech_type));
1367 ASSERT_EQ(2u, decoder_->Channels());
1368 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1369 decoder_.get(), NetEqDecoder::kDecoderOpus_2ch,
1370 "120ms codec", kPayloadType));
1371 }
1372
1373 std::unique_ptr<Decoder120ms> decoder_;
1374 AudioFrame output_;
1375 const uint32_t kPayloadType = 17;
1376 const uint32_t kSamplingFreq_ = 48000;
1377 uint16_t sequence_number_ = 1;
1378 };
1379
TEST_F(NetEqImplTest120ms,AudioRepetition)1380 TEST_F(NetEqImplTest120ms, AudioRepetition) {
1381 config_.playout_mode = kPlayoutFax;
1382 CreateInstanceNoMocks();
1383 Register120msCodec(AudioDecoder::kSpeech);
1384
1385 InsertPacket(first_timestamp());
1386 GetFirstPacket();
1387
1388 bool muted;
1389 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1390 EXPECT_EQ(kAudioRepetition, neteq_->last_operation_for_test());
1391 }
1392
TEST_F(NetEqImplTest120ms,AlternativePlc)1393 TEST_F(NetEqImplTest120ms, AlternativePlc) {
1394 config_.playout_mode = kPlayoutOff;
1395 CreateInstanceNoMocks();
1396 Register120msCodec(AudioDecoder::kSpeech);
1397
1398 InsertPacket(first_timestamp());
1399 GetFirstPacket();
1400
1401 bool muted;
1402 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1403 EXPECT_EQ(kAlternativePlc, neteq_->last_operation_for_test());
1404 }
1405
TEST_F(NetEqImplTest120ms,CodecInternalCng)1406 TEST_F(NetEqImplTest120ms, CodecInternalCng) {
1407 CreateInstanceNoMocks();
1408 Register120msCodec(AudioDecoder::kComfortNoise);
1409
1410 InsertPacket(first_timestamp());
1411 GetFirstPacket();
1412
1413 bool muted;
1414 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1415 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1416 }
1417
TEST_F(NetEqImplTest120ms,Normal)1418 TEST_F(NetEqImplTest120ms, Normal) {
1419 CreateInstanceNoMocks();
1420 Register120msCodec(AudioDecoder::kSpeech);
1421
1422 InsertPacket(first_timestamp());
1423 GetFirstPacket();
1424
1425 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1426 }
1427
TEST_F(NetEqImplTest120ms,Merge)1428 TEST_F(NetEqImplTest120ms, Merge) {
1429 CreateInstanceWithDelayManagerMock();
1430
1431 Register120msCodec(AudioDecoder::kSpeech);
1432 InsertPacket(first_timestamp());
1433
1434 GetFirstPacket();
1435 bool muted;
1436 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1437
1438 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1439
1440 // Delay manager reports a target level which should cause a Merge.
1441 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1442
1443 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1444 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1445 }
1446
TEST_F(NetEqImplTest120ms,Expand)1447 TEST_F(NetEqImplTest120ms, Expand) {
1448 CreateInstanceNoMocks();
1449 Register120msCodec(AudioDecoder::kSpeech);
1450
1451 InsertPacket(first_timestamp());
1452 GetFirstPacket();
1453
1454 bool muted;
1455 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1456 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1457 }
1458
TEST_F(NetEqImplTest120ms,FastAccelerate)1459 TEST_F(NetEqImplTest120ms, FastAccelerate) {
1460 CreateInstanceWithDelayManagerMock();
1461 Register120msCodec(AudioDecoder::kSpeech);
1462
1463 InsertPacket(first_timestamp());
1464 GetFirstPacket();
1465 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1466
1467 // Delay manager report buffer limit which should cause a FastAccelerate.
1468 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1469 .Times(1)
1470 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1471
1472 bool muted;
1473 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1474 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1475 }
1476
TEST_F(NetEqImplTest120ms,PreemptiveExpand)1477 TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
1478 CreateInstanceWithDelayManagerMock();
1479 Register120msCodec(AudioDecoder::kSpeech);
1480
1481 InsertPacket(first_timestamp());
1482 GetFirstPacket();
1483
1484 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1485
1486 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1487 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1488 .Times(1)
1489 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1490
1491 bool muted;
1492 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1493 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1494 }
1495
TEST_F(NetEqImplTest120ms,Accelerate)1496 TEST_F(NetEqImplTest120ms, Accelerate) {
1497 CreateInstanceWithDelayManagerMock();
1498 Register120msCodec(AudioDecoder::kSpeech);
1499
1500 InsertPacket(first_timestamp());
1501 GetFirstPacket();
1502
1503 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1504
1505 // Delay manager report buffer limit which should cause a Accelerate.
1506 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1507 .Times(1)
1508 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1509
1510 bool muted;
1511 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1512 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1513 }
1514
1515 }// namespace webrtc
1516