blob: 784c63c28fad3bfa01dd7e09166e53afec919a66 [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 Bonadei71207422017-09-15 13:58:09 +020015#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/neteq/accelerate.h"
17#include "modules/audio_coding/neteq/expand.h"
18#include "modules/audio_coding/neteq/include/neteq.h"
19#include "modules/audio_coding/neteq/mock/mock_buffer_level_filter.h"
20#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
21#include "modules/audio_coding/neteq/mock/mock_delay_manager.h"
22#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
23#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
24#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
25#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
26#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
27#include "modules/audio_coding/neteq/neteq_impl.h"
28#include "modules/audio_coding/neteq/preemptive_expand.h"
29#include "modules/audio_coding/neteq/sync_buffer.h"
30#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010031#include "rtc_base/numerics/safe_conversions.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) {
455 UseNoMocks();
456 CreateInstance();
457
458 const uint8_t kPayloadType = 17; // Just an arbitrary number.
459 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
460 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700461 const size_t kPayloadLengthSamples =
462 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000463 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
464 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700465 RTPHeader rtp_header;
466 rtp_header.payloadType = kPayloadType;
467 rtp_header.sequenceNumber = 0x1234;
468 rtp_header.timestamp = 0x12345678;
469 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000470
471 // This is a dummy decoder that produces as many output samples as the input
472 // has bytes. The output is an increasing series, starting at 1 for the first
473 // sample, and then increasing by 1 for each sample.
474 class CountingSamplesDecoder : public AudioDecoder {
475 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000476 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000477
478 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100479 int DecodeInternal(const uint8_t* encoded,
480 size_t encoded_len,
481 int /* sample_rate_hz */,
482 int16_t* decoded,
483 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000484 for (size_t i = 0; i < encoded_len; ++i) {
485 decoded[i] = next_value_++;
486 }
487 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200488 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000489 }
490
Karl Wiberg43766482015-08-27 15:22:11 +0200491 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000492
kwiberg347d3512016-06-16 01:59:09 -0700493 int SampleRateHz() const override { return kSampleRateHz; }
494
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000495 size_t Channels() const override { return 1; }
496
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000497 uint16_t next_value() const { return next_value_; }
498
499 private:
500 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000501 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000502
kwibergee1879c2015-10-29 06:20:28 -0700503 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
504 &decoder_, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700505 "dummy name", kPayloadType));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000506
507 // Insert one packet.
508 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700509 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000510
511 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700512 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800513 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700514 bool muted;
515 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
516 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800517 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
518 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800519 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000520
521 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700522 EXPECT_EQ(kPayloadLengthSamples,
523 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000524
525 // The value of the last of the output samples is the same as the number of
526 // samples played from the decoded packet. Thus, this number + the RTP
527 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200528 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700529 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200530 absl::optional<uint32_t>(rtp_header.timestamp +
531 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700532 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000533
534 // Check the timestamp for the last value in the sync buffer. This should
535 // be one full frame length ahead of the RTP timestamp.
536 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
537 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700538 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000539 sync_buffer->end_timestamp());
540
541 // Check that the number of samples still to play from the sync buffer add
542 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800543 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700544 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800545 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000546}
547
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000548TEST_F(NetEqImplTest, ReorderedPacket) {
549 UseNoMocks();
550 CreateInstance();
551
552 const uint8_t kPayloadType = 17; // Just an arbitrary number.
553 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
554 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700555 const size_t kPayloadLengthSamples =
556 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000557 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
558 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700559 RTPHeader rtp_header;
560 rtp_header.payloadType = kPayloadType;
561 rtp_header.sequenceNumber = 0x1234;
562 rtp_header.timestamp = 0x12345678;
563 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000564
565 // Create a mock decoder object.
566 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200567 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700568 EXPECT_CALL(mock_decoder, SampleRateHz())
569 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000570 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000571 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
572 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700573 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200574 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000575 int16_t dummy_output[kPayloadLengthSamples] = {0};
576 // The below expectation will make the mock decoder write
577 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100578 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
579 kSampleRateHz, _, _))
580 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000581 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100582 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200583 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
kwibergee1879c2015-10-29 06:20:28 -0700584 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
585 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700586 "dummy name", kPayloadType));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000587
588 // Insert one packet.
589 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700590 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000591
592 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700593 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800594 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700595 bool muted;
596 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800597 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
598 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800599 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000600
601 // Insert two more packets. The first one is out of order, and is already too
602 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700603 rtp_header.sequenceNumber -= 1;
604 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000605 payload[0] = 1;
606 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700607 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
608 rtp_header.sequenceNumber += 2;
609 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000610 payload[0] = 2;
611 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700612 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000613
614 // Expect only the second packet to be decoded (the one with "2" as the first
615 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100616 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
617 kSampleRateHz, _, _))
618 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000619 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100620 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200621 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000622
623 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700624 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800625 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
626 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800627 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000628
629 // Now check the packet buffer, and make sure it is empty, since the
630 // out-of-order packet should have been discarded.
631 EXPECT_TRUE(packet_buffer_->Empty());
632
633 EXPECT_CALL(mock_decoder, Die());
634}
635
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000636// This test verifies that NetEq can handle the situation where the first
637// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000638TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000639 UseNoMocks();
640 CreateInstance();
641
642 const uint8_t kPayloadType = 17; // Just an arbitrary number.
643 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
644 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700645 const size_t kPayloadLengthSamples =
646 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700647 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000648 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700649 RTPHeader rtp_header;
650 rtp_header.payloadType = kPayloadType;
651 rtp_header.sequenceNumber = 0x1234;
652 rtp_header.timestamp = 0x12345678;
653 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000654
655 // Insert one packet. Note that we have not registered any payload type, so
656 // this packet will be rejected.
657 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700658 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000659
660 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700661 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800662 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700663 bool muted;
664 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800665 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
666 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
667 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800668 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000669
670 // Register the payload type.
kwibergee1879c2015-10-29 06:20:28 -0700671 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800672 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000673
674 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700675 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700676 rtp_header.sequenceNumber++;
677 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000678 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700679 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000680 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
681 }
682
683 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700684 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700685 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800686 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
687 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
688 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800689 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000690 << "NetEq did not decode the packets as expected.";
691 }
692}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000693
694// This test verifies that NetEq can handle comfort noise and enters/quits codec
695// internal CNG mode properly.
696TEST_F(NetEqImplTest, CodecInternalCng) {
697 UseNoMocks();
698 CreateInstance();
699
700 const uint8_t kPayloadType = 17; // Just an arbitrary number.
701 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
702 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700703 const size_t kPayloadLengthSamples =
704 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
705 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000706 uint8_t payload[kPayloadLengthBytes] = {0};
707 int16_t dummy_output[kPayloadLengthSamples] = {0};
708
henrik.lundin246ef3e2017-04-24 09:14:32 -0700709 RTPHeader rtp_header;
710 rtp_header.payloadType = kPayloadType;
711 rtp_header.sequenceNumber = 0x1234;
712 rtp_header.timestamp = 0x12345678;
713 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000714
715 // Create a mock decoder object.
716 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200717 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700718 EXPECT_CALL(mock_decoder, SampleRateHz())
719 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000720 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000721 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
722 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700723 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200724 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700725 // Packed duration when asking the decoder for more CNG data (without a new
726 // packet).
727 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200728 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000729
730 // Pointee(x) verifies that first byte of the payload equals x, this makes it
731 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100732 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
733 kSampleRateKhz * 1000, _, _))
734 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000735 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100736 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200737 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000738
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100739 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
740 kSampleRateKhz * 1000, _, _))
741 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000742 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100743 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200744 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000745
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100746 EXPECT_CALL(mock_decoder,
747 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
748 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000749 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100750 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200751 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000752
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100753 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
754 kSampleRateKhz * 1000, _, _))
755 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000756 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100757 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200758 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000759
Karl Wibergd8399e62015-05-25 14:39:56 +0200760 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
kwibergee1879c2015-10-29 06:20:28 -0700761 &mock_decoder, NetEqDecoder::kDecoderOpus,
kwiberg342f7402016-06-16 03:18:00 -0700762 "dummy name", kPayloadType));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000763
764 // Insert one packet (decoder will return speech).
765 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700766 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000767
768 // Insert second packet (decoder will return CNG).
769 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700770 rtp_header.sequenceNumber++;
771 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000772 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700773 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000774
Peter Kastingdce40cf2015-08-24 14:52:23 -0700775 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800776 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800777 AudioFrame::SpeechType expected_type[8] = {
778 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
779 AudioFrame::kCNG, AudioFrame::kCNG,
780 AudioFrame::kCNG, AudioFrame::kCNG,
781 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000782 };
783 int expected_timestamp_increment[8] = {
784 -1, // will not be used.
785 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700786 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
787 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000788 50 * kSampleRateKhz, 10 * kSampleRateKhz
789 };
790
henrik.lundin7a926812016-05-12 13:51:28 -0700791 bool muted;
792 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200793 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700794 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000795
henrik.lundin0d96ab72016-04-06 12:28:26 -0700796 // Lambda for verifying the timestamps.
797 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200798 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700799 if (expected_timestamp_increment[i] == -1) {
800 // Expect to get an empty timestamp value during CNG and PLC.
801 EXPECT_FALSE(ts) << "i = " << i;
802 } else {
803 ASSERT_TRUE(ts) << "i = " << i;
804 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
805 << "i = " << i;
806 last_timestamp = ts;
807 }
808 };
809
Peter Kastingdce40cf2015-08-24 14:52:23 -0700810 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800811 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
812 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800813 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700814 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700815 SCOPED_TRACE("");
816 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000817 }
818
819 // Insert third packet, which leaves a gap from last packet.
820 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700821 rtp_header.sequenceNumber += 2;
822 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000823 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700824 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000825
Peter Kastingdce40cf2015-08-24 14:52:23 -0700826 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800827 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
828 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800829 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700830 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700831 SCOPED_TRACE("");
832 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000833 }
834
835 // Now check the packet buffer, and make sure it is empty.
836 EXPECT_TRUE(packet_buffer_->Empty());
837
838 EXPECT_CALL(mock_decoder, Die());
839}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000840
841TEST_F(NetEqImplTest, UnsupportedDecoder) {
842 UseNoMocks();
843 CreateInstance();
minyue5bd33972016-05-02 04:46:11 -0700844 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800845 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000846
847 const uint8_t kPayloadType = 17; // Just an arbitrary number.
848 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
849 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000850
Peter Kastingdce40cf2015-08-24 14:52:23 -0700851 const size_t kPayloadLengthSamples =
852 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000853 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700854 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200855 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700856 RTPHeader rtp_header;
857 rtp_header.payloadType = kPayloadType;
858 rtp_header.sequenceNumber = 0x1234;
859 rtp_header.timestamp = 0x12345678;
860 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000861
ossu61a208b2016-09-20 01:38:00 -0700862 ::testing::NiceMock<MockAudioDecoder> decoder;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000863
864 const uint8_t kFirstPayloadValue = 1;
865 const uint8_t kSecondPayloadValue = 2;
866
ossu61a208b2016-09-20 01:38:00 -0700867 EXPECT_CALL(decoder,
868 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
869 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200870 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000871
ossu61a208b2016-09-20 01:38:00 -0700872 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000873 .Times(0);
874
ossu61a208b2016-09-20 01:38:00 -0700875 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
876 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000877 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700878 .WillOnce(DoAll(
879 SetArrayArgument<3>(dummy_output,
880 dummy_output + kPayloadLengthSamples * kChannels),
881 SetArgPointee<4>(AudioDecoder::kSpeech),
882 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000883
ossu61a208b2016-09-20 01:38:00 -0700884 EXPECT_CALL(decoder,
885 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
886 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200887 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700888
889 EXPECT_CALL(decoder, SampleRateHz())
890 .WillRepeatedly(Return(kSampleRateHz));
891
892 EXPECT_CALL(decoder, Channels())
893 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000894
kwibergee1879c2015-10-29 06:20:28 -0700895 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
ossu61a208b2016-09-20 01:38:00 -0700896 &decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700897 "dummy name", kPayloadType));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000898
899 // Insert one packet.
900 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
901 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700902 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000903
904 // Insert another packet.
905 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700906 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000907 // The second timestamp needs to be at least 30 ms after the first to make
908 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700909 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000910 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700911 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000912
henrik.lundin6d8e0112016-03-04 10:34:21 -0800913 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700914 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800915 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200916 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700917 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800918 // Output size and number of channels should be correct.
919 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
920 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
921 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000922
henrik.lundin6d8e0112016-03-04 10:34:21 -0800923 // Second call to GetAudio will decode the packet that is ok. No errors are
924 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700925 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800926 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
927 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700928
929 // Die isn't called through NiceMock (since it's called by the
930 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
931 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000932}
933
henrik.lundin116c84e2015-08-27 13:14:48 -0700934// This test inserts packets until the buffer is flushed. After that, it asks
935// NetEq for the network statistics. The purpose of the test is to make sure
936// that even though the buffer size increment is negative (which it becomes when
937// the packet causing a flush is inserted), the packet length stored in the
938// decision logic remains valid.
939TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
940 UseNoMocks();
941 CreateInstance();
942
943 const size_t kPayloadLengthSamples = 80;
944 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
945 const uint8_t kPayloadType = 17; // Just an arbitrary number.
946 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
947 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700948 RTPHeader rtp_header;
949 rtp_header.payloadType = kPayloadType;
950 rtp_header.sequenceNumber = 0x1234;
951 rtp_header.timestamp = 0x12345678;
952 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700953
kwibergee1879c2015-10-29 06:20:28 -0700954 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800955 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin116c84e2015-08-27 13:14:48 -0700956
957 // Insert packets until the buffer flushes.
958 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
959 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
960 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700961 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
962 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
963 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700964 }
965 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
966
967 // Ask for network statistics. This should not crash.
968 NetEqNetworkStatistics stats;
969 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
970}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200971
972TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
973 UseNoMocks();
974 CreateInstance();
975
976 const uint8_t kPayloadType = 17; // Just an arbitrary number.
977 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
978 const int kSampleRateHz = 8000;
979 const size_t kPayloadLengthSamples =
980 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
981 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
982 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700983 RTPHeader rtp_header;
984 rtp_header.payloadType = kPayloadType;
985 rtp_header.sequenceNumber = 0x1234;
986 rtp_header.timestamp = 0x12345678;
987 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200988
989 // Create a mock decoder object.
990 MockAudioDecoder mock_decoder;
991 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700992 EXPECT_CALL(mock_decoder, SampleRateHz())
993 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200994 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
995 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
996 .WillRepeatedly(Return(0));
997 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200998 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200999 int16_t dummy_output[kPayloadLengthSamples] = {0};
1000 // The below expectation will make the mock decoder write
1001 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1002 // speech. That is, the decoded length is 5 samples shorter than the expected.
1003 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001004 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001005 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001006 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001007 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001008 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001009 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
kwibergee1879c2015-10-29 06:20:28 -07001010 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1011 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001012 "dummy name", kPayloadType));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001013
1014 // Insert one packet.
1015 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001016 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001017
1018 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1019
1020 // Pull audio once.
1021 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001022 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001023 bool muted;
1024 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001025 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1026 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001027 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001028
1029 EXPECT_CALL(mock_decoder, Die());
1030}
minyuel6d92bf52015-09-23 15:20:39 +02001031
1032// This test checks the behavior of NetEq when audio decoder fails.
1033TEST_F(NetEqImplTest, DecodingError) {
1034 UseNoMocks();
1035 CreateInstance();
1036
1037 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1038 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1039 const int kSampleRateHz = 8000;
1040 const int kDecoderErrorCode = -97; // Any negative number.
1041
1042 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1043 const size_t kFrameLengthSamples =
1044 static_cast<size_t>(5 * kSampleRateHz / 1000);
1045
1046 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1047
1048 uint8_t payload[kPayloadLengthBytes] = {0};
1049
henrik.lundin246ef3e2017-04-24 09:14:32 -07001050 RTPHeader rtp_header;
1051 rtp_header.payloadType = kPayloadType;
1052 rtp_header.sequenceNumber = 0x1234;
1053 rtp_header.timestamp = 0x12345678;
1054 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001055
1056 // Create a mock decoder object.
1057 MockAudioDecoder mock_decoder;
1058 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001059 EXPECT_CALL(mock_decoder, SampleRateHz())
1060 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001061 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1062 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1063 .WillRepeatedly(Return(0));
1064 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001065 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001066 EXPECT_CALL(mock_decoder, ErrorCode())
1067 .WillOnce(Return(kDecoderErrorCode));
1068 EXPECT_CALL(mock_decoder, HasDecodePlc())
1069 .WillOnce(Return(false));
1070 int16_t dummy_output[kFrameLengthSamples] = {0};
1071
1072 {
1073 InSequence sequence; // Dummy variable.
1074 // Mock decoder works normally the first time.
1075 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001076 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001077 .Times(3)
1078 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001079 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001080 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001081 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001082 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001083 .RetiresOnSaturation();
1084
1085 // Then mock decoder fails. A common reason for failure can be buffer being
1086 // too short
1087 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001088 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001089 .WillOnce(Return(-1))
1090 .RetiresOnSaturation();
1091
1092 // Mock decoder finally returns to normal.
1093 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001094 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001095 .Times(2)
1096 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001097 DoAll(SetArrayArgument<3>(dummy_output,
1098 dummy_output + kFrameLengthSamples),
1099 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001100 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001101 }
1102
kwibergee1879c2015-10-29 06:20:28 -07001103 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1104 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001105 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001106
1107 // Insert packets.
1108 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001109 rtp_header.sequenceNumber += 1;
1110 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001111 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001112 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001113 }
1114
1115 // Pull audio.
1116 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001117 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001118 bool muted;
1119 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001120 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1121 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001122 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001123
1124 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001125 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001126 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1127 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001128 // We are not expecting anything for output.speech_type_, since an error was
1129 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001130
1131 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001132 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001133 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1134 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001135 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001136
1137 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001138 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001139 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1140 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001141 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001142
1143 EXPECT_CALL(mock_decoder, Die());
1144}
1145
1146// This test checks the behavior of NetEq when audio decoder fails during CNG.
1147TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1148 UseNoMocks();
1149 CreateInstance();
1150
1151 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1152 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1153 const int kSampleRateHz = 8000;
1154 const int kDecoderErrorCode = -97; // Any negative number.
1155
1156 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1157 const size_t kFrameLengthSamples =
1158 static_cast<size_t>(5 * kSampleRateHz / 1000);
1159
1160 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1161
1162 uint8_t payload[kPayloadLengthBytes] = {0};
1163
henrik.lundin246ef3e2017-04-24 09:14:32 -07001164 RTPHeader rtp_header;
1165 rtp_header.payloadType = kPayloadType;
1166 rtp_header.sequenceNumber = 0x1234;
1167 rtp_header.timestamp = 0x12345678;
1168 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001169
1170 // Create a mock decoder object.
1171 MockAudioDecoder mock_decoder;
1172 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001173 EXPECT_CALL(mock_decoder, SampleRateHz())
1174 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001175 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1176 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1177 .WillRepeatedly(Return(0));
1178 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001179 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001180 EXPECT_CALL(mock_decoder, ErrorCode())
1181 .WillOnce(Return(kDecoderErrorCode));
1182 int16_t dummy_output[kFrameLengthSamples] = {0};
1183
1184 {
1185 InSequence sequence; // Dummy variable.
1186 // Mock decoder works normally the first 2 times.
1187 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001188 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001189 .Times(2)
1190 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001191 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001192 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001193 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001194 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001195 .RetiresOnSaturation();
1196
1197 // Then mock decoder fails. A common reason for failure can be buffer being
1198 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001199 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001200 .WillOnce(Return(-1))
1201 .RetiresOnSaturation();
1202
1203 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001204 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001205 .Times(2)
1206 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001207 DoAll(SetArrayArgument<3>(dummy_output,
1208 dummy_output + kFrameLengthSamples),
1209 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001210 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001211 }
1212
kwibergee1879c2015-10-29 06:20:28 -07001213 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1214 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001215 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001216
1217 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1218 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001219 rtp_header.sequenceNumber += 1;
1220 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001221 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001222 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001223 }
1224
1225 // Pull audio.
1226 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001227 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001228 bool muted;
1229 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001230 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1231 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001232 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001233
1234 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001235 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001236 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1237 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001238 // We are not expecting anything for output.speech_type_, since an error was
1239 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001240
1241 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001242 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001243 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1244 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001245 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001246
1247 EXPECT_CALL(mock_decoder, Die());
1248}
1249
henrik.lundind89814b2015-11-23 06:49:25 -08001250// Tests that the return value from last_output_sample_rate_hz() is equal to the
1251// configured inital sample rate.
1252TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1253 UseNoMocks();
1254 config_.sample_rate_hz = 48000;
1255 CreateInstance();
1256 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1257}
1258
henrik.lundined497212016-04-25 10:11:38 -07001259TEST_F(NetEqImplTest, TickTimerIncrement) {
1260 UseNoMocks();
1261 CreateInstance();
1262 ASSERT_TRUE(tick_timer_);
1263 EXPECT_EQ(0u, tick_timer_->ticks());
1264 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001265 bool muted;
1266 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001267 EXPECT_EQ(1u, tick_timer_->ticks());
1268}
1269
henrik.lundin114c1b32017-04-26 07:47:32 -07001270TEST_F(NetEqImplTest, TargetDelayMs) {
1271 UseNoMocks();
1272 use_mock_delay_manager_ = true;
1273 CreateInstance();
1274 // Let the dummy target delay be 17 packets.
1275 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1276 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1277 .WillOnce(Return(kTargetLevelPacketsQ8));
1278 // Default packet size before any packet has been decoded is 30 ms, so we are
1279 // expecting 17 * 30 = 510 ms target delay.
1280 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1281}
1282
henrik.lundinb8c55b12017-05-10 07:38:01 -07001283TEST_F(NetEqImplTest, InsertEmptyPacket) {
1284 UseNoMocks();
1285 use_mock_delay_manager_ = true;
1286 CreateInstance();
1287
1288 RTPHeader rtp_header;
1289 rtp_header.payloadType = 17;
1290 rtp_header.sequenceNumber = 0x1234;
1291 rtp_header.timestamp = 0x12345678;
1292 rtp_header.ssrc = 0x87654321;
1293
1294 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1295 neteq_->InsertEmptyPacket(rtp_header);
1296}
1297
minyue5bd33972016-05-02 04:46:11 -07001298class Decoder120ms : public AudioDecoder {
1299 public:
kwiberg347d3512016-06-16 01:59:09 -07001300 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1301 : sample_rate_hz_(sample_rate_hz),
1302 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001303 speech_type_(speech_type) {}
1304
1305 int DecodeInternal(const uint8_t* encoded,
1306 size_t encoded_len,
1307 int sample_rate_hz,
1308 int16_t* decoded,
1309 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001310 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001311 size_t decoded_len =
1312 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1313 for (size_t i = 0; i < decoded_len; ++i) {
1314 decoded[i] = next_value_++;
1315 }
1316 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001317 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001318 }
1319
1320 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001321 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001322 size_t Channels() const override { return 2; }
1323
1324 private:
kwiberg347d3512016-06-16 01:59:09 -07001325 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001326 int16_t next_value_;
1327 SpeechType speech_type_;
1328};
1329
1330class NetEqImplTest120ms : public NetEqImplTest {
1331 protected:
1332 NetEqImplTest120ms() : NetEqImplTest() {}
1333 virtual ~NetEqImplTest120ms() {}
1334
1335 void CreateInstanceNoMocks() {
1336 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001337 CreateInstance(decoder_factory_);
1338 EXPECT_TRUE(neteq_->RegisterPayloadType(
1339 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001340 }
1341
1342 void CreateInstanceWithDelayManagerMock() {
1343 UseNoMocks();
1344 use_mock_delay_manager_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001345 CreateInstance(decoder_factory_);
1346 EXPECT_TRUE(neteq_->RegisterPayloadType(
1347 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001348 }
1349
1350 uint32_t timestamp_diff_between_packets() const {
1351 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1352 }
1353
1354 uint32_t first_timestamp() const { return 10u; }
1355
1356 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001357 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001358 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001359 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1360 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001361 }
1362 }
1363
1364 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001365 RTPHeader rtp_header;
1366 rtp_header.payloadType = kPayloadType;
1367 rtp_header.sequenceNumber = sequence_number_;
1368 rtp_header.timestamp = timestamp;
1369 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001370 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1371 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001372 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001373 sequence_number_++;
1374 }
1375
1376 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001377 const uint32_t sampling_freq = kSamplingFreq_;
1378 decoder_factory_ =
1379 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1380 [sampling_freq, speech_type]() {
1381 std::unique_ptr<AudioDecoder> decoder =
1382 absl::make_unique<Decoder120ms>(sampling_freq, speech_type);
1383 RTC_CHECK_EQ(2, decoder->Channels());
1384 return decoder;
1385 });
minyue5bd33972016-05-02 04:46:11 -07001386 }
1387
Niels Möllera0f44302018-11-30 10:45:12 +01001388 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001389 AudioFrame output_;
1390 const uint32_t kPayloadType = 17;
1391 const uint32_t kSamplingFreq_ = 48000;
1392 uint16_t sequence_number_ = 1;
1393};
1394
minyue5bd33972016-05-02 04:46:11 -07001395TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001396 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001397 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001398
1399 InsertPacket(first_timestamp());
1400 GetFirstPacket();
1401
henrik.lundin7a926812016-05-12 13:51:28 -07001402 bool muted;
1403 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001404 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1405}
1406
1407TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001408 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001409 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001410
1411 InsertPacket(first_timestamp());
1412 GetFirstPacket();
1413
1414 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1415}
1416
1417TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001418 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001419 CreateInstanceWithDelayManagerMock();
1420
minyue5bd33972016-05-02 04:46:11 -07001421 InsertPacket(first_timestamp());
1422
1423 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001424 bool muted;
1425 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001426
1427 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1428
1429 // Delay manager reports a target level which should cause a Merge.
1430 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1431
henrik.lundin7a926812016-05-12 13:51:28 -07001432 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001433 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1434}
1435
1436TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001437 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001438 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001439
1440 InsertPacket(first_timestamp());
1441 GetFirstPacket();
1442
henrik.lundin7a926812016-05-12 13:51:28 -07001443 bool muted;
1444 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001445 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1446}
1447
1448TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001449 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001450 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001451
1452 InsertPacket(first_timestamp());
1453 GetFirstPacket();
1454 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1455
1456 // Delay manager report buffer limit which should cause a FastAccelerate.
1457 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1458 .Times(1)
1459 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1460
henrik.lundin7a926812016-05-12 13:51:28 -07001461 bool muted;
1462 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001463 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1464}
1465
1466TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001467 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001468 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001469
1470 InsertPacket(first_timestamp());
1471 GetFirstPacket();
1472
1473 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1474
1475 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1476 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1477 .Times(1)
1478 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1479
henrik.lundin7a926812016-05-12 13:51:28 -07001480 bool muted;
1481 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001482 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1483}
1484
1485TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001486 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001487 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001488
1489 InsertPacket(first_timestamp());
1490 GetFirstPacket();
1491
1492 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1493
1494 // Delay manager report buffer limit which should cause a Accelerate.
1495 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1496 .Times(1)
1497 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1498
henrik.lundin7a926812016-05-12 13:51:28 -07001499 bool muted;
1500 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001501 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1502}
1503
minyuel6d92bf52015-09-23 15:20:39 +02001504}// namespace webrtc