blob: 055efa0f27be4e1aedf7d04afb450eb55359d314 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
kwiberg84be5112016-04-27 01:19:58 -070011#include <memory>
12
Niels Möllera0f44302018-11-30 10:45:12 +010013#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_coding/neteq/accelerate.h"
16#include "modules/audio_coding/neteq/expand.h"
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"
Karl Wiberge40468b2017-11-22 10:42:26 +010030#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010031#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010032#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "test/gmock.h"
34#include "test/gtest.h"
35#include "test/mock_audio_decoder.h"
36#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000038using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039using ::testing::Return;
40using ::testing::ReturnNull;
41using ::testing::_;
42using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000043using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044using ::testing::InSequence;
45using ::testing::Invoke;
46using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000047using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000048using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
50namespace webrtc {
51
52// This function is called when inserting a packet list into the mock packet
53// buffer. The purpose is to delete all inserted packets properly, to avoid
54// memory leaks in the test.
55int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070056 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057 return PacketBuffer::kOK;
58}
59
60class NetEqImplTest : public ::testing::Test {
61 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070062 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000063
Niels Möllera0f44302018-11-30 10:45:12 +010064 void CreateInstance(
65 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
66 ASSERT_TRUE(decoder_factory);
67 NetEqImpl::Dependencies deps(config_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070068
69 // Get a local pointer to NetEq's TickTimer object.
70 tick_timer_ = deps.tick_timer.get();
71
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000072 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070073 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
74 mock_buffer_level_filter_ = mock.get();
75 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000076 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070077 buffer_level_filter_ = deps.buffer_level_filter.get();
78
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000079 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070080 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
81 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000082 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
83 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070084 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000085 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070086 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000087
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070089 std::unique_ptr<MockDelayPeakDetector> mock(
90 new MockDelayPeakDetector(tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 mock_delay_peak_detector_ = mock.get();
92 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
93 deps.delay_peak_detector = std::move(mock);
94 }
95 delay_peak_detector_ = deps.delay_peak_detector.get();
96
97 if (use_mock_delay_manager_) {
98 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010099 config_.max_packets_in_buffer, config_.min_delay_ms,
100 delay_peak_detector_, tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -0700101 mock_delay_manager_ = mock.get();
102 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
103 deps.delay_manager = std::move(mock);
104 }
105 delay_manager_ = deps.delay_manager.get();
106
107 if (use_mock_dtmf_buffer_) {
108 std::unique_ptr<MockDtmfBuffer> mock(
109 new MockDtmfBuffer(config_.sample_rate_hz));
110 mock_dtmf_buffer_ = mock.get();
111 deps.dtmf_buffer = std::move(mock);
112 }
113 dtmf_buffer_ = deps.dtmf_buffer.get();
114
115 if (use_mock_dtmf_tone_generator_) {
116 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
117 mock_dtmf_tone_generator_ = mock.get();
118 deps.dtmf_tone_generator = std::move(mock);
119 }
120 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
121
122 if (use_mock_packet_buffer_) {
123 std::unique_ptr<MockPacketBuffer> mock(
124 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
125 mock_packet_buffer_ = mock.get();
126 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700127 }
128 packet_buffer_ = deps.packet_buffer.get();
129
130 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700131 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700132 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700133 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700134 }
ossua70695a2016-09-22 02:06:28 -0700135 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700136
137 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
138 new TimestampScaler(*deps.decoder_database.get()));
139
140 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000141 ASSERT_TRUE(neteq_ != NULL);
142 }
143
Niels Möllera0f44302018-11-30 10:45:12 +0100144 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
145
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000146 void UseNoMocks() {
147 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
148 use_mock_buffer_level_filter_ = false;
149 use_mock_decoder_database_ = false;
150 use_mock_delay_peak_detector_ = false;
151 use_mock_delay_manager_ = false;
152 use_mock_dtmf_buffer_ = false;
153 use_mock_dtmf_tone_generator_ = false;
154 use_mock_packet_buffer_ = false;
155 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 }
157
158 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000159 if (use_mock_buffer_level_filter_) {
160 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
161 }
162 if (use_mock_decoder_database_) {
163 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
164 }
165 if (use_mock_delay_manager_) {
166 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
167 }
168 if (use_mock_delay_peak_detector_) {
169 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
170 }
171 if (use_mock_dtmf_buffer_) {
172 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
173 }
174 if (use_mock_dtmf_tone_generator_) {
175 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
176 }
177 if (use_mock_packet_buffer_) {
178 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
179 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 }
181
solenberg2779bab2016-11-17 04:45:19 -0800182 void TestDtmfPacket(NetEqDecoder decoder_type) {
183 const size_t kPayloadLength = 4;
184 const uint8_t kPayloadType = 110;
185 const uint32_t kReceiveTime = 17;
186 const int kSampleRateHz = 16000;
187 config_.sample_rate_hz = kSampleRateHz;
188 UseNoMocks();
189 CreateInstance();
190 // Event: 2, E bit, Volume: 17, Length: 4336.
191 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700192 RTPHeader rtp_header;
193 rtp_header.payloadType = kPayloadType;
194 rtp_header.sequenceNumber = 0x1234;
195 rtp_header.timestamp = 0x12345678;
196 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800197
198 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
199 decoder_type, "telephone-event", kPayloadType));
200
201 // Insert first packet.
202 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700203 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800204
205 // Pull audio once.
206 const size_t kMaxOutputSize =
207 static_cast<size_t>(10 * kSampleRateHz / 1000);
208 AudioFrame output;
209 bool muted;
210 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
211 ASSERT_FALSE(muted);
212 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
213 EXPECT_EQ(1u, output.num_channels_);
214 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
215
216 // Verify first 64 samples of actual output.
217 const std::vector<int16_t> kOutput({
218 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
219 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
220 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
221 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
222 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
223 -2534, -1163 });
224 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700225 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800226 }
227
henrik.lundin1d9061e2016-04-26 12:19:34 -0700228 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000229 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700230 TickTimer* tick_timer_ = nullptr;
231 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
232 BufferLevelFilter* buffer_level_filter_ = nullptr;
233 bool use_mock_buffer_level_filter_ = true;
234 MockDecoderDatabase* mock_decoder_database_ = nullptr;
235 DecoderDatabase* decoder_database_ = nullptr;
236 bool use_mock_decoder_database_ = true;
237 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
238 DelayPeakDetector* delay_peak_detector_ = nullptr;
239 bool use_mock_delay_peak_detector_ = true;
240 MockDelayManager* mock_delay_manager_ = nullptr;
241 DelayManager* delay_manager_ = nullptr;
242 bool use_mock_delay_manager_ = true;
243 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
244 DtmfBuffer* dtmf_buffer_ = nullptr;
245 bool use_mock_dtmf_buffer_ = true;
246 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
247 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
248 bool use_mock_dtmf_tone_generator_ = true;
249 MockPacketBuffer* mock_packet_buffer_ = nullptr;
250 PacketBuffer* packet_buffer_ = nullptr;
251 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700252 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
253 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700254 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000255};
256
257
258// This tests the interface class NetEq.
259// TODO(hlundin): Move to separate file?
260TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000261 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700262 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 delete neteq;
264}
265
kwiberg5adaf732016-10-04 09:33:27 -0700266TEST_F(NetEqImplTest, RegisterPayloadTypeNetEqDecoder) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000267 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268 uint8_t rtp_payload_type = 0;
kwibergee1879c2015-10-29 06:20:28 -0700269 NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800270 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000271 EXPECT_CALL(*mock_decoder_database_,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800272 RegisterPayload(rtp_payload_type, codec_type, kCodecName));
273 neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274}
275
kwiberg5adaf732016-10-04 09:33:27 -0700276TEST_F(NetEqImplTest, RegisterPayloadType) {
277 CreateInstance();
278 constexpr int rtp_payload_type = 0;
279 const SdpAudioFormat format("pcmu", 8000, 1);
280 EXPECT_CALL(*mock_decoder_database_,
281 RegisterPayload(rtp_payload_type, format));
282 neteq_->RegisterPayloadType(rtp_payload_type, format);
283}
284
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000286 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000288 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200290 // Check that kOK is returned when database returns kDecoderNotFound, because
291 // removing a payload type that was never registered is not an error.
292 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293}
294
kwiberg6b19b562016-09-20 04:02:25 -0700295TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
296 CreateInstance();
297 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
298 neteq_->RemoveAllPayloadTypes();
299}
300
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000302 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000303 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000304 const uint8_t kPayloadType = 0;
305 const uint16_t kFirstSequenceNumber = 0x1234;
306 const uint32_t kFirstTimestamp = 0x12345678;
307 const uint32_t kSsrc = 0x87654321;
308 const uint32_t kFirstReceiveTime = 17;
309 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700310 RTPHeader rtp_header;
311 rtp_header.payloadType = kPayloadType;
312 rtp_header.sequenceNumber = kFirstSequenceNumber;
313 rtp_header.timestamp = kFirstTimestamp;
314 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700315 Packet fake_packet;
316 fake_packet.payload_type = kPayloadType;
317 fake_packet.sequence_number = kFirstSequenceNumber;
318 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319
kwibergc0f2dcf2016-05-31 06:28:03 -0700320 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
321 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100322 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800323 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200324 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800325 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700326 EXPECT_EQ("pcmu", format.name);
327
328 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
329 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
330 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
331 // BWE update function called with first packet.
332 EXPECT_CALL(*mock_decoder,
333 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
334 kFirstTimestamp, kFirstReceiveTime));
335 // BWE update function called with second packet.
336 EXPECT_CALL(
337 *mock_decoder,
338 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
339 kFirstTimestamp + 160, kFirstReceiveTime + 155));
340 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
341
342 *dec = std::move(mock_decoder);
343 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200344 DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, absl::nullopt,
ossu84bc9852016-08-26 05:41:23 -0700345 mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346
347 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000348 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 .WillRepeatedly(Return(&info));
350
351 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000352 EXPECT_CALL(*mock_packet_buffer_, Empty())
353 .WillOnce(Return(false)); // Called once after first packet is inserted.
354 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200356 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100358 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
359 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
361 // index) is a pointer, and the variable pointed to is set to kPayloadType.
362 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
363 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700364 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000365 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700366 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000367
368 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000369 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000370 .Times(1);
371
372 // Expectations for delay manager.
373 {
374 // All expectations within this block must be called in this specific order.
375 InSequence sequence; // Dummy variable.
376 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000377 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378 .Times(2)
379 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000380 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000382 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000383 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000384 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
385 .WillOnce(Return(0));
386 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000387 .WillOnce(Return(0));
388 }
389
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700391 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000392
393 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700394 rtp_header.timestamp += 160;
395 rtp_header.sequenceNumber += 1;
396 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000397}
398
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000399TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
400 UseNoMocks();
401 CreateInstance();
402
403 const int kPayloadLengthSamples = 80;
404 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
405 const uint8_t kPayloadType = 17; // Just an arbitrary number.
406 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
407 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700408 RTPHeader rtp_header;
409 rtp_header.payloadType = kPayloadType;
410 rtp_header.sequenceNumber = 0x1234;
411 rtp_header.timestamp = 0x12345678;
412 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000413
kwibergee1879c2015-10-29 06:20:28 -0700414 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800415 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000416
417 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700418 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000419 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700420 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
421 rtp_header.timestamp += kPayloadLengthSamples;
422 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000423 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
424 }
425
426 // Insert one more packet and make sure the buffer got flushed. That is, it
427 // should only hold one single packet.
428 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700429 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700430 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700431 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700432 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
433 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000434}
435
solenberg2779bab2016-11-17 04:45:19 -0800436TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
437 TestDtmfPacket(NetEqDecoder::kDecoderAVT);
438}
solenberg99df6c02016-10-11 04:35:34 -0700439
solenberg2779bab2016-11-17 04:45:19 -0800440TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
441 TestDtmfPacket(NetEqDecoder::kDecoderAVT16kHz);
442}
solenberg99df6c02016-10-11 04:35:34 -0700443
solenberg2779bab2016-11-17 04:45:19 -0800444TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
445 TestDtmfPacket(NetEqDecoder::kDecoderAVT32kHz);
446}
solenberg99df6c02016-10-11 04:35:34 -0700447
solenberg2779bab2016-11-17 04:45:19 -0800448TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
449 TestDtmfPacket(NetEqDecoder::kDecoderAVT48kHz);
solenberg99df6c02016-10-11 04:35:34 -0700450}
451
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000452// This test verifies that timestamps propagate from the incoming packets
453// through to the sync buffer and to the playout timestamp.
454TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000455 const uint8_t kPayloadType = 17; // Just an arbitrary number.
456 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
457 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700458 const size_t kPayloadLengthSamples =
459 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000460 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
461 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700462 RTPHeader rtp_header;
463 rtp_header.payloadType = kPayloadType;
464 rtp_header.sequenceNumber = 0x1234;
465 rtp_header.timestamp = 0x12345678;
466 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000467
468 // This is a dummy decoder that produces as many output samples as the input
469 // has bytes. The output is an increasing series, starting at 1 for the first
470 // sample, and then increasing by 1 for each sample.
471 class CountingSamplesDecoder : public AudioDecoder {
472 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000473 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000474
475 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100476 int DecodeInternal(const uint8_t* encoded,
477 size_t encoded_len,
478 int /* sample_rate_hz */,
479 int16_t* decoded,
480 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000481 for (size_t i = 0; i < encoded_len; ++i) {
482 decoded[i] = next_value_++;
483 }
484 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200485 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000486 }
487
Karl Wiberg43766482015-08-27 15:22:11 +0200488 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000489
kwiberg347d3512016-06-16 01:59:09 -0700490 int SampleRateHz() const override { return kSampleRateHz; }
491
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000492 size_t Channels() const override { return 1; }
493
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000494 uint16_t next_value() const { return next_value_; }
495
496 private:
497 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000498 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000499
Niels Möllerb7180c02018-12-06 13:07:11 +0100500 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
501 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
502
503 UseNoMocks();
504 CreateInstance(decoder_factory);
505
506 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
507 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000508
509 // Insert one packet.
510 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700511 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000512
513 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700514 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800515 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700516 bool muted;
517 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
518 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800519 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
520 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800521 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000522
523 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700524 EXPECT_EQ(kPayloadLengthSamples,
525 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000526
527 // The value of the last of the output samples is the same as the number of
528 // samples played from the decoded packet. Thus, this number + the RTP
529 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200530 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700531 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200532 absl::optional<uint32_t>(rtp_header.timestamp +
533 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700534 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000535
536 // Check the timestamp for the last value in the sync buffer. This should
537 // be one full frame length ahead of the RTP timestamp.
538 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
539 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700540 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000541 sync_buffer->end_timestamp());
542
543 // Check that the number of samples still to play from the sync buffer add
544 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800545 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700546 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800547 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000548}
549
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000550TEST_F(NetEqImplTest, ReorderedPacket) {
551 UseNoMocks();
552 CreateInstance();
553
554 const uint8_t kPayloadType = 17; // Just an arbitrary number.
555 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
556 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700557 const size_t kPayloadLengthSamples =
558 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000559 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
560 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700561 RTPHeader rtp_header;
562 rtp_header.payloadType = kPayloadType;
563 rtp_header.sequenceNumber = 0x1234;
564 rtp_header.timestamp = 0x12345678;
565 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000566
567 // Create a mock decoder object.
568 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200569 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700570 EXPECT_CALL(mock_decoder, SampleRateHz())
571 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000572 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000573 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
574 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700575 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200576 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000577 int16_t dummy_output[kPayloadLengthSamples] = {0};
578 // The below expectation will make the mock decoder write
579 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100580 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
581 kSampleRateHz, _, _))
582 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000583 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100584 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200585 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
kwibergee1879c2015-10-29 06:20:28 -0700586 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
587 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700588 "dummy name", kPayloadType));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000589
590 // Insert one packet.
591 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700592 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000593
594 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700595 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800596 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700597 bool muted;
598 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800599 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
600 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800601 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000602
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.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700605 rtp_header.sequenceNumber -= 1;
606 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000607 payload[0] = 1;
608 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700609 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
610 rtp_header.sequenceNumber += 2;
611 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000612 payload[0] = 2;
613 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700614 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000615
616 // Expect only the second packet to be decoded (the one with "2" as the first
617 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100618 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
619 kSampleRateHz, _, _))
620 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000621 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100622 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200623 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000624
625 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700626 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800627 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
628 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800629 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000630
631 // Now check the packet buffer, and make sure it is empty, since the
632 // out-of-order packet should have been discarded.
633 EXPECT_TRUE(packet_buffer_->Empty());
634
635 EXPECT_CALL(mock_decoder, Die());
636}
637
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000638// This test verifies that NetEq can handle the situation where the first
639// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000640TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000641 UseNoMocks();
642 CreateInstance();
643
644 const uint8_t kPayloadType = 17; // Just an arbitrary number.
645 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
646 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700647 const size_t kPayloadLengthSamples =
648 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700649 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000650 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700651 RTPHeader rtp_header;
652 rtp_header.payloadType = kPayloadType;
653 rtp_header.sequenceNumber = 0x1234;
654 rtp_header.timestamp = 0x12345678;
655 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000656
657 // Insert one packet. Note that we have not registered any payload type, so
658 // this packet will be rejected.
659 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700660 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000661
662 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700663 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800664 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700665 bool muted;
666 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800667 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
668 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
669 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800670 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000671
672 // Register the payload type.
kwibergee1879c2015-10-29 06:20:28 -0700673 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800674 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000675
676 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700678 rtp_header.sequenceNumber++;
679 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000680 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700681 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000682 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
683 }
684
685 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700686 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700687 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800688 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
689 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
690 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800691 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000692 << "NetEq did not decode the packets as expected.";
693 }
694}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000695
696// This test verifies that NetEq can handle comfort noise and enters/quits codec
697// internal CNG mode properly.
698TEST_F(NetEqImplTest, CodecInternalCng) {
699 UseNoMocks();
700 CreateInstance();
701
702 const uint8_t kPayloadType = 17; // Just an arbitrary number.
703 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
704 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700705 const size_t kPayloadLengthSamples =
706 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
707 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000708 uint8_t payload[kPayloadLengthBytes] = {0};
709 int16_t dummy_output[kPayloadLengthSamples] = {0};
710
henrik.lundin246ef3e2017-04-24 09:14:32 -0700711 RTPHeader rtp_header;
712 rtp_header.payloadType = kPayloadType;
713 rtp_header.sequenceNumber = 0x1234;
714 rtp_header.timestamp = 0x12345678;
715 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000716
717 // Create a mock decoder object.
718 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200719 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700720 EXPECT_CALL(mock_decoder, SampleRateHz())
721 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000722 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000723 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
724 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700725 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200726 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700727 // Packed duration when asking the decoder for more CNG data (without a new
728 // packet).
729 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200730 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000731
732 // Pointee(x) verifies that first byte of the payload equals x, this makes it
733 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100734 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
735 kSampleRateKhz * 1000, _, _))
736 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000737 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100738 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200739 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000740
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100741 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
742 kSampleRateKhz * 1000, _, _))
743 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000744 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100745 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200746 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000747
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100748 EXPECT_CALL(mock_decoder,
749 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
750 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000751 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100752 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200753 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000754
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100755 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
756 kSampleRateKhz * 1000, _, _))
757 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000758 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100759 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200760 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000761
Karl Wibergd8399e62015-05-25 14:39:56 +0200762 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
kwibergee1879c2015-10-29 06:20:28 -0700763 &mock_decoder, NetEqDecoder::kDecoderOpus,
kwiberg342f7402016-06-16 03:18:00 -0700764 "dummy name", kPayloadType));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000765
766 // Insert one packet (decoder will return speech).
767 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700768 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000769
770 // Insert second packet (decoder will return CNG).
771 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700772 rtp_header.sequenceNumber++;
773 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000774 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700775 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000776
Peter Kastingdce40cf2015-08-24 14:52:23 -0700777 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800778 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800779 AudioFrame::SpeechType expected_type[8] = {
780 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
781 AudioFrame::kCNG, AudioFrame::kCNG,
782 AudioFrame::kCNG, AudioFrame::kCNG,
783 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000784 };
785 int expected_timestamp_increment[8] = {
786 -1, // will not be used.
787 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700788 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
789 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000790 50 * kSampleRateKhz, 10 * kSampleRateKhz
791 };
792
henrik.lundin7a926812016-05-12 13:51:28 -0700793 bool muted;
794 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200795 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700796 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000797
henrik.lundin0d96ab72016-04-06 12:28:26 -0700798 // Lambda for verifying the timestamps.
799 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200800 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700801 if (expected_timestamp_increment[i] == -1) {
802 // Expect to get an empty timestamp value during CNG and PLC.
803 EXPECT_FALSE(ts) << "i = " << i;
804 } else {
805 ASSERT_TRUE(ts) << "i = " << i;
806 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
807 << "i = " << i;
808 last_timestamp = ts;
809 }
810 };
811
Peter Kastingdce40cf2015-08-24 14:52:23 -0700812 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800813 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
814 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800815 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700816 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700817 SCOPED_TRACE("");
818 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000819 }
820
821 // Insert third packet, which leaves a gap from last packet.
822 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700823 rtp_header.sequenceNumber += 2;
824 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000825 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700826 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000827
Peter Kastingdce40cf2015-08-24 14:52:23 -0700828 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800829 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
830 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800831 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700832 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700833 SCOPED_TRACE("");
834 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000835 }
836
837 // Now check the packet buffer, and make sure it is empty.
838 EXPECT_TRUE(packet_buffer_->Empty());
839
840 EXPECT_CALL(mock_decoder, Die());
841}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000842
843TEST_F(NetEqImplTest, UnsupportedDecoder) {
844 UseNoMocks();
845 CreateInstance();
minyue5bd33972016-05-02 04:46:11 -0700846 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800847 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000848
849 const uint8_t kPayloadType = 17; // Just an arbitrary number.
850 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
851 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000852
Peter Kastingdce40cf2015-08-24 14:52:23 -0700853 const size_t kPayloadLengthSamples =
854 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000855 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700856 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200857 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700858 RTPHeader rtp_header;
859 rtp_header.payloadType = kPayloadType;
860 rtp_header.sequenceNumber = 0x1234;
861 rtp_header.timestamp = 0x12345678;
862 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000863
ossu61a208b2016-09-20 01:38:00 -0700864 ::testing::NiceMock<MockAudioDecoder> decoder;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000865
866 const uint8_t kFirstPayloadValue = 1;
867 const uint8_t kSecondPayloadValue = 2;
868
ossu61a208b2016-09-20 01:38:00 -0700869 EXPECT_CALL(decoder,
870 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
871 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200872 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000873
ossu61a208b2016-09-20 01:38:00 -0700874 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000875 .Times(0);
876
ossu61a208b2016-09-20 01:38:00 -0700877 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
878 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000879 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700880 .WillOnce(DoAll(
881 SetArrayArgument<3>(dummy_output,
882 dummy_output + kPayloadLengthSamples * kChannels),
883 SetArgPointee<4>(AudioDecoder::kSpeech),
884 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000885
ossu61a208b2016-09-20 01:38:00 -0700886 EXPECT_CALL(decoder,
887 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
888 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200889 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700890
891 EXPECT_CALL(decoder, SampleRateHz())
892 .WillRepeatedly(Return(kSampleRateHz));
893
894 EXPECT_CALL(decoder, Channels())
895 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000896
kwibergee1879c2015-10-29 06:20:28 -0700897 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
ossu61a208b2016-09-20 01:38:00 -0700898 &decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700899 "dummy name", kPayloadType));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000900
901 // Insert one packet.
902 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
903 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700904 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000905
906 // Insert another packet.
907 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700908 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000909 // The second timestamp needs to be at least 30 ms after the first to make
910 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700911 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000912 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700913 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000914
henrik.lundin6d8e0112016-03-04 10:34:21 -0800915 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700916 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800917 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200918 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700919 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800920 // Output size and number of channels should be correct.
921 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
922 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
923 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000924
henrik.lundin6d8e0112016-03-04 10:34:21 -0800925 // Second call to GetAudio will decode the packet that is ok. No errors are
926 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700927 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800928 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
929 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700930
931 // Die isn't called through NiceMock (since it's called by the
932 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
933 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000934}
935
henrik.lundin116c84e2015-08-27 13:14:48 -0700936// This test inserts packets until the buffer is flushed. After that, it asks
937// NetEq for the network statistics. The purpose of the test is to make sure
938// that even though the buffer size increment is negative (which it becomes when
939// the packet causing a flush is inserted), the packet length stored in the
940// decision logic remains valid.
941TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
942 UseNoMocks();
943 CreateInstance();
944
945 const size_t kPayloadLengthSamples = 80;
946 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
947 const uint8_t kPayloadType = 17; // Just an arbitrary number.
948 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
949 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700950 RTPHeader rtp_header;
951 rtp_header.payloadType = kPayloadType;
952 rtp_header.sequenceNumber = 0x1234;
953 rtp_header.timestamp = 0x12345678;
954 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700955
kwibergee1879c2015-10-29 06:20:28 -0700956 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800957 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin116c84e2015-08-27 13:14:48 -0700958
959 // Insert packets until the buffer flushes.
960 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
961 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
962 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700963 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
964 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
965 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700966 }
967 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
968
969 // Ask for network statistics. This should not crash.
970 NetEqNetworkStatistics stats;
971 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
972}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200973
974TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
975 UseNoMocks();
976 CreateInstance();
977
978 const uint8_t kPayloadType = 17; // Just an arbitrary number.
979 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
980 const int kSampleRateHz = 8000;
981 const size_t kPayloadLengthSamples =
982 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
983 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
984 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700985 RTPHeader rtp_header;
986 rtp_header.payloadType = kPayloadType;
987 rtp_header.sequenceNumber = 0x1234;
988 rtp_header.timestamp = 0x12345678;
989 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200990
991 // Create a mock decoder object.
992 MockAudioDecoder mock_decoder;
993 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700994 EXPECT_CALL(mock_decoder, SampleRateHz())
995 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200996 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
997 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
998 .WillRepeatedly(Return(0));
999 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001000 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001001 int16_t dummy_output[kPayloadLengthSamples] = {0};
1002 // The below expectation will make the mock decoder write
1003 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1004 // speech. That is, the decoded length is 5 samples shorter than the expected.
1005 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001006 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001007 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001008 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001009 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001010 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001011 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
kwibergee1879c2015-10-29 06:20:28 -07001012 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1013 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001014 "dummy name", kPayloadType));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001015
1016 // Insert one packet.
1017 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001018 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001019
1020 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1021
1022 // Pull audio once.
1023 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001024 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001025 bool muted;
1026 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001027 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1028 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001029 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001030
1031 EXPECT_CALL(mock_decoder, Die());
1032}
minyuel6d92bf52015-09-23 15:20:39 +02001033
1034// This test checks the behavior of NetEq when audio decoder fails.
1035TEST_F(NetEqImplTest, DecodingError) {
1036 UseNoMocks();
1037 CreateInstance();
1038
1039 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1040 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1041 const int kSampleRateHz = 8000;
1042 const int kDecoderErrorCode = -97; // Any negative number.
1043
1044 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1045 const size_t kFrameLengthSamples =
1046 static_cast<size_t>(5 * kSampleRateHz / 1000);
1047
1048 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1049
1050 uint8_t payload[kPayloadLengthBytes] = {0};
1051
henrik.lundin246ef3e2017-04-24 09:14:32 -07001052 RTPHeader rtp_header;
1053 rtp_header.payloadType = kPayloadType;
1054 rtp_header.sequenceNumber = 0x1234;
1055 rtp_header.timestamp = 0x12345678;
1056 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001057
1058 // Create a mock decoder object.
1059 MockAudioDecoder mock_decoder;
1060 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001061 EXPECT_CALL(mock_decoder, SampleRateHz())
1062 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001063 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1064 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1065 .WillRepeatedly(Return(0));
1066 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001067 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001068 EXPECT_CALL(mock_decoder, ErrorCode())
1069 .WillOnce(Return(kDecoderErrorCode));
1070 EXPECT_CALL(mock_decoder, HasDecodePlc())
1071 .WillOnce(Return(false));
1072 int16_t dummy_output[kFrameLengthSamples] = {0};
1073
1074 {
1075 InSequence sequence; // Dummy variable.
1076 // Mock decoder works normally the first time.
1077 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001078 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001079 .Times(3)
1080 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001081 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001082 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001083 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001084 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001085 .RetiresOnSaturation();
1086
1087 // Then mock decoder fails. A common reason for failure can be buffer being
1088 // too short
1089 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001090 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001091 .WillOnce(Return(-1))
1092 .RetiresOnSaturation();
1093
1094 // Mock decoder finally returns to normal.
1095 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001096 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001097 .Times(2)
1098 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001099 DoAll(SetArrayArgument<3>(dummy_output,
1100 dummy_output + kFrameLengthSamples),
1101 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001102 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001103 }
1104
kwibergee1879c2015-10-29 06:20:28 -07001105 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1106 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001107 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001108
1109 // Insert packets.
1110 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001111 rtp_header.sequenceNumber += 1;
1112 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001113 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001114 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001115 }
1116
1117 // Pull audio.
1118 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001119 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001120 bool muted;
1121 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001122 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1123 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001124 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001125
1126 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001127 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001128 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1129 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001130 // We are not expecting anything for output.speech_type_, since an error was
1131 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001132
1133 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001134 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001135 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1136 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001137 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001138
1139 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001140 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001141 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1142 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001143 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001144
1145 EXPECT_CALL(mock_decoder, Die());
1146}
1147
1148// This test checks the behavior of NetEq when audio decoder fails during CNG.
1149TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1150 UseNoMocks();
1151 CreateInstance();
1152
1153 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1154 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1155 const int kSampleRateHz = 8000;
1156 const int kDecoderErrorCode = -97; // Any negative number.
1157
1158 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1159 const size_t kFrameLengthSamples =
1160 static_cast<size_t>(5 * kSampleRateHz / 1000);
1161
1162 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1163
1164 uint8_t payload[kPayloadLengthBytes] = {0};
1165
henrik.lundin246ef3e2017-04-24 09:14:32 -07001166 RTPHeader rtp_header;
1167 rtp_header.payloadType = kPayloadType;
1168 rtp_header.sequenceNumber = 0x1234;
1169 rtp_header.timestamp = 0x12345678;
1170 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001171
1172 // Create a mock decoder object.
1173 MockAudioDecoder mock_decoder;
1174 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001175 EXPECT_CALL(mock_decoder, SampleRateHz())
1176 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001177 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1178 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1179 .WillRepeatedly(Return(0));
1180 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001181 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001182 EXPECT_CALL(mock_decoder, ErrorCode())
1183 .WillOnce(Return(kDecoderErrorCode));
1184 int16_t dummy_output[kFrameLengthSamples] = {0};
1185
1186 {
1187 InSequence sequence; // Dummy variable.
1188 // Mock decoder works normally the first 2 times.
1189 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001190 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001191 .Times(2)
1192 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001193 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001194 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001195 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001196 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001197 .RetiresOnSaturation();
1198
1199 // Then mock decoder fails. A common reason for failure can be buffer being
1200 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001201 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001202 .WillOnce(Return(-1))
1203 .RetiresOnSaturation();
1204
1205 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001206 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001207 .Times(2)
1208 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001209 DoAll(SetArrayArgument<3>(dummy_output,
1210 dummy_output + kFrameLengthSamples),
1211 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001212 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001213 }
1214
kwibergee1879c2015-10-29 06:20:28 -07001215 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1216 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001217 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001218
1219 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1220 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001221 rtp_header.sequenceNumber += 1;
1222 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001223 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001224 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001225 }
1226
1227 // Pull audio.
1228 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001229 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001230 bool muted;
1231 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001232 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1233 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001234 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001235
1236 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001237 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001238 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1239 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001240 // We are not expecting anything for output.speech_type_, since an error was
1241 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001242
1243 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001244 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001245 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1246 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001247 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001248
1249 EXPECT_CALL(mock_decoder, Die());
1250}
1251
henrik.lundind89814b2015-11-23 06:49:25 -08001252// Tests that the return value from last_output_sample_rate_hz() is equal to the
1253// configured inital sample rate.
1254TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1255 UseNoMocks();
1256 config_.sample_rate_hz = 48000;
1257 CreateInstance();
1258 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1259}
1260
henrik.lundined497212016-04-25 10:11:38 -07001261TEST_F(NetEqImplTest, TickTimerIncrement) {
1262 UseNoMocks();
1263 CreateInstance();
1264 ASSERT_TRUE(tick_timer_);
1265 EXPECT_EQ(0u, tick_timer_->ticks());
1266 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001267 bool muted;
1268 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001269 EXPECT_EQ(1u, tick_timer_->ticks());
1270}
1271
henrik.lundin114c1b32017-04-26 07:47:32 -07001272TEST_F(NetEqImplTest, TargetDelayMs) {
1273 UseNoMocks();
1274 use_mock_delay_manager_ = true;
1275 CreateInstance();
1276 // Let the dummy target delay be 17 packets.
1277 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1278 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1279 .WillOnce(Return(kTargetLevelPacketsQ8));
1280 // Default packet size before any packet has been decoded is 30 ms, so we are
1281 // expecting 17 * 30 = 510 ms target delay.
1282 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1283}
1284
henrik.lundinb8c55b12017-05-10 07:38:01 -07001285TEST_F(NetEqImplTest, InsertEmptyPacket) {
1286 UseNoMocks();
1287 use_mock_delay_manager_ = true;
1288 CreateInstance();
1289
1290 RTPHeader rtp_header;
1291 rtp_header.payloadType = 17;
1292 rtp_header.sequenceNumber = 0x1234;
1293 rtp_header.timestamp = 0x12345678;
1294 rtp_header.ssrc = 0x87654321;
1295
1296 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1297 neteq_->InsertEmptyPacket(rtp_header);
1298}
1299
minyue5bd33972016-05-02 04:46:11 -07001300class Decoder120ms : public AudioDecoder {
1301 public:
kwiberg347d3512016-06-16 01:59:09 -07001302 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1303 : sample_rate_hz_(sample_rate_hz),
1304 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001305 speech_type_(speech_type) {}
1306
1307 int DecodeInternal(const uint8_t* encoded,
1308 size_t encoded_len,
1309 int sample_rate_hz,
1310 int16_t* decoded,
1311 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001312 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001313 size_t decoded_len =
1314 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1315 for (size_t i = 0; i < decoded_len; ++i) {
1316 decoded[i] = next_value_++;
1317 }
1318 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001319 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001320 }
1321
1322 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001323 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001324 size_t Channels() const override { return 2; }
1325
1326 private:
kwiberg347d3512016-06-16 01:59:09 -07001327 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001328 int16_t next_value_;
1329 SpeechType speech_type_;
1330};
1331
1332class NetEqImplTest120ms : public NetEqImplTest {
1333 protected:
1334 NetEqImplTest120ms() : NetEqImplTest() {}
1335 virtual ~NetEqImplTest120ms() {}
1336
1337 void CreateInstanceNoMocks() {
1338 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001339 CreateInstance(decoder_factory_);
1340 EXPECT_TRUE(neteq_->RegisterPayloadType(
1341 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001342 }
1343
1344 void CreateInstanceWithDelayManagerMock() {
1345 UseNoMocks();
1346 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001347 CreateInstance(decoder_factory_);
1348 EXPECT_TRUE(neteq_->RegisterPayloadType(
1349 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001350 }
1351
1352 uint32_t timestamp_diff_between_packets() const {
1353 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1354 }
1355
1356 uint32_t first_timestamp() const { return 10u; }
1357
1358 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001359 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001360 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001361 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1362 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001363 }
1364 }
1365
1366 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001367 RTPHeader rtp_header;
1368 rtp_header.payloadType = kPayloadType;
1369 rtp_header.sequenceNumber = sequence_number_;
1370 rtp_header.timestamp = timestamp;
1371 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001372 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1373 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001374 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001375 sequence_number_++;
1376 }
1377
1378 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001379 const uint32_t sampling_freq = kSamplingFreq_;
1380 decoder_factory_ =
1381 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1382 [sampling_freq, speech_type]() {
1383 std::unique_ptr<AudioDecoder> decoder =
1384 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1385 RTC_CHECK_EQ(2, decoder->Channels());
1386 return decoder;
1387 });
minyue5bd33972016-05-02 04:46:11 -07001388 }
1389
Niels Möllera0f44302018-11-30 10:45:12 +01001390 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001391 AudioFrame output_;
1392 const uint32_t kPayloadType = 17;
1393 const uint32_t kSamplingFreq_ = 48000;
1394 uint16_t sequence_number_ = 1;
1395};
1396
minyue5bd33972016-05-02 04:46:11 -07001397TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001398 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001399 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001400
1401 InsertPacket(first_timestamp());
1402 GetFirstPacket();
1403
henrik.lundin7a926812016-05-12 13:51:28 -07001404 bool muted;
1405 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001406 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1407}
1408
1409TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001410 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001411 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001412
1413 InsertPacket(first_timestamp());
1414 GetFirstPacket();
1415
1416 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1417}
1418
1419TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001420 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001421 CreateInstanceWithDelayManagerMock();
1422
minyue5bd33972016-05-02 04:46:11 -07001423 InsertPacket(first_timestamp());
1424
1425 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001426 bool muted;
1427 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001428
1429 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1430
1431 // Delay manager reports a target level which should cause a Merge.
1432 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1433
henrik.lundin7a926812016-05-12 13:51:28 -07001434 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001435 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1436}
1437
1438TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001439 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001440 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001441
1442 InsertPacket(first_timestamp());
1443 GetFirstPacket();
1444
henrik.lundin7a926812016-05-12 13:51:28 -07001445 bool muted;
1446 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001447 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1448}
1449
1450TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001451 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001452 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001453
1454 InsertPacket(first_timestamp());
1455 GetFirstPacket();
1456 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1457
1458 // Delay manager report buffer limit which should cause a FastAccelerate.
1459 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1460 .Times(1)
1461 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1462
henrik.lundin7a926812016-05-12 13:51:28 -07001463 bool muted;
1464 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001465 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1466}
1467
1468TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001469 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001470 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001471
1472 InsertPacket(first_timestamp());
1473 GetFirstPacket();
1474
1475 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1476
1477 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1478 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1479 .Times(1)
1480 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1481
henrik.lundin7a926812016-05-12 13:51:28 -07001482 bool muted;
1483 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001484 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1485}
1486
1487TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001488 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001489 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001490
1491 InsertPacket(first_timestamp());
1492 GetFirstPacket();
1493
1494 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1495
1496 // Delay manager report buffer limit which should cause a Accelerate.
1497 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1498 .Times(1)
1499 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1500
henrik.lundin7a926812016-05-12 13:51:28 -07001501 bool muted;
1502 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001503 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1504}
1505
minyuel6d92bf52015-09-23 15:20:39 +02001506}// namespace webrtc