blob: 0e087c847f18fdfc35fd28f322f5fab25952a030 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020014#include "common_types.h" // NOLINT(build/include)
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "test/gmock.h"
32#include "test/gtest.h"
33#include "test/mock_audio_decoder.h"
34#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +000036using ::testing::AtLeast;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037using ::testing::Return;
38using ::testing::ReturnNull;
39using ::testing::_;
40using ::testing::SetArgPointee;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000041using ::testing::SetArrayArgument;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042using ::testing::InSequence;
43using ::testing::Invoke;
44using ::testing::WithArg;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +000045using ::testing::Pointee;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000046using ::testing::IsNull;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047
48namespace webrtc {
49
50// This function is called when inserting a packet list into the mock packet
51// buffer. The purpose is to delete all inserted packets properly, to avoid
52// memory leaks in the test.
53int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070054 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055 return PacketBuffer::kOK;
56}
57
58class NetEqImplTest : public ::testing::Test {
59 protected:
henrik.lundin1d9061e2016-04-26 12:19:34 -070060 NetEqImplTest() { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000061
62 void CreateInstance() {
ossue3525782016-05-25 07:37:43 -070063 NetEqImpl::Dependencies deps(config_, CreateBuiltinAudioDecoderFactory());
henrik.lundin1d9061e2016-04-26 12:19:34 -070064
65 // Get a local pointer to NetEq's TickTimer object.
66 tick_timer_ = deps.tick_timer.get();
67
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000068 if (use_mock_buffer_level_filter_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070069 std::unique_ptr<MockBufferLevelFilter> mock(new MockBufferLevelFilter);
70 mock_buffer_level_filter_ = mock.get();
71 deps.buffer_level_filter = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000072 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070073 buffer_level_filter_ = deps.buffer_level_filter.get();
74
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000075 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070076 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
77 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000078 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
79 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070080 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000081 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070082 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000083
henrik.lundin1d9061e2016-04-26 12:19:34 -070084 if (use_mock_delay_peak_detector_) {
henrik.lundinf3933702016-04-28 01:53:52 -070085 std::unique_ptr<MockDelayPeakDetector> mock(
86 new MockDelayPeakDetector(tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070087 mock_delay_peak_detector_ = mock.get();
88 EXPECT_CALL(*mock_delay_peak_detector_, Reset()).Times(1);
89 deps.delay_peak_detector = std::move(mock);
90 }
91 delay_peak_detector_ = deps.delay_peak_detector.get();
92
93 if (use_mock_delay_manager_) {
94 std::unique_ptr<MockDelayManager> mock(new MockDelayManager(
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010095 config_.max_packets_in_buffer, config_.min_delay_ms,
96 delay_peak_detector_, tick_timer_));
henrik.lundin1d9061e2016-04-26 12:19:34 -070097 mock_delay_manager_ = mock.get();
98 EXPECT_CALL(*mock_delay_manager_, set_streaming_mode(false)).Times(1);
99 deps.delay_manager = std::move(mock);
100 }
101 delay_manager_ = deps.delay_manager.get();
102
103 if (use_mock_dtmf_buffer_) {
104 std::unique_ptr<MockDtmfBuffer> mock(
105 new MockDtmfBuffer(config_.sample_rate_hz));
106 mock_dtmf_buffer_ = mock.get();
107 deps.dtmf_buffer = std::move(mock);
108 }
109 dtmf_buffer_ = deps.dtmf_buffer.get();
110
111 if (use_mock_dtmf_tone_generator_) {
112 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
113 mock_dtmf_tone_generator_ = mock.get();
114 deps.dtmf_tone_generator = std::move(mock);
115 }
116 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
117
118 if (use_mock_packet_buffer_) {
119 std::unique_ptr<MockPacketBuffer> mock(
120 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
121 mock_packet_buffer_ = mock.get();
122 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700123 }
124 packet_buffer_ = deps.packet_buffer.get();
125
126 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700127 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700128 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700129 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700130 }
ossua70695a2016-09-22 02:06:28 -0700131 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700132
133 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
134 new TimestampScaler(*deps.decoder_database.get()));
135
136 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000137 ASSERT_TRUE(neteq_ != NULL);
138 }
139
140 void UseNoMocks() {
141 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
142 use_mock_buffer_level_filter_ = false;
143 use_mock_decoder_database_ = false;
144 use_mock_delay_peak_detector_ = false;
145 use_mock_delay_manager_ = false;
146 use_mock_dtmf_buffer_ = false;
147 use_mock_dtmf_tone_generator_ = false;
148 use_mock_packet_buffer_ = false;
149 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000150 }
151
152 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000153 if (use_mock_buffer_level_filter_) {
154 EXPECT_CALL(*mock_buffer_level_filter_, Die()).Times(1);
155 }
156 if (use_mock_decoder_database_) {
157 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
158 }
159 if (use_mock_delay_manager_) {
160 EXPECT_CALL(*mock_delay_manager_, Die()).Times(1);
161 }
162 if (use_mock_delay_peak_detector_) {
163 EXPECT_CALL(*mock_delay_peak_detector_, Die()).Times(1);
164 }
165 if (use_mock_dtmf_buffer_) {
166 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
167 }
168 if (use_mock_dtmf_tone_generator_) {
169 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
170 }
171 if (use_mock_packet_buffer_) {
172 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
173 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 }
175
solenberg2779bab2016-11-17 04:45:19 -0800176 void TestDtmfPacket(NetEqDecoder decoder_type) {
177 const size_t kPayloadLength = 4;
178 const uint8_t kPayloadType = 110;
179 const uint32_t kReceiveTime = 17;
180 const int kSampleRateHz = 16000;
181 config_.sample_rate_hz = kSampleRateHz;
182 UseNoMocks();
183 CreateInstance();
184 // Event: 2, E bit, Volume: 17, Length: 4336.
185 uint8_t payload[kPayloadLength] = { 0x02, 0x80 + 0x11, 0x10, 0xF0 };
henrik.lundin246ef3e2017-04-24 09:14:32 -0700186 RTPHeader rtp_header;
187 rtp_header.payloadType = kPayloadType;
188 rtp_header.sequenceNumber = 0x1234;
189 rtp_header.timestamp = 0x12345678;
190 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800191
192 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
193 decoder_type, "telephone-event", kPayloadType));
194
195 // Insert first packet.
196 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700197 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
solenberg2779bab2016-11-17 04:45:19 -0800198
199 // Pull audio once.
200 const size_t kMaxOutputSize =
201 static_cast<size_t>(10 * kSampleRateHz / 1000);
202 AudioFrame output;
203 bool muted;
204 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
205 ASSERT_FALSE(muted);
206 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
207 EXPECT_EQ(1u, output.num_channels_);
208 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
209
210 // Verify first 64 samples of actual output.
211 const std::vector<int16_t> kOutput({
212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1578, -2816, -3460, -3403, -2709, -1594,
213 -363, 671, 1269, 1328, 908, 202, -513, -964, -955, -431, 504, 1617,
214 2602, 3164, 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688,
215 -1440, -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315, 515,
216 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482, -3864, -3516,
217 -2534, -1163 });
218 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700219 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800220 }
221
henrik.lundin1d9061e2016-04-26 12:19:34 -0700222 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000223 NetEq::Config config_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700224 TickTimer* tick_timer_ = nullptr;
225 MockBufferLevelFilter* mock_buffer_level_filter_ = nullptr;
226 BufferLevelFilter* buffer_level_filter_ = nullptr;
227 bool use_mock_buffer_level_filter_ = true;
228 MockDecoderDatabase* mock_decoder_database_ = nullptr;
229 DecoderDatabase* decoder_database_ = nullptr;
230 bool use_mock_decoder_database_ = true;
231 MockDelayPeakDetector* mock_delay_peak_detector_ = nullptr;
232 DelayPeakDetector* delay_peak_detector_ = nullptr;
233 bool use_mock_delay_peak_detector_ = true;
234 MockDelayManager* mock_delay_manager_ = nullptr;
235 DelayManager* delay_manager_ = nullptr;
236 bool use_mock_delay_manager_ = true;
237 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
238 DtmfBuffer* dtmf_buffer_ = nullptr;
239 bool use_mock_dtmf_buffer_ = true;
240 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
241 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
242 bool use_mock_dtmf_tone_generator_ = true;
243 MockPacketBuffer* mock_packet_buffer_ = nullptr;
244 PacketBuffer* packet_buffer_ = nullptr;
245 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700246 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
247 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700248 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000249};
250
251
252// This tests the interface class NetEq.
253// TODO(hlundin): Move to separate file?
254TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000255 NetEq::Config config;
ossue3525782016-05-25 07:37:43 -0700256 NetEq* neteq = NetEq::Create(config, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000257 delete neteq;
258}
259
kwiberg5adaf732016-10-04 09:33:27 -0700260TEST_F(NetEqImplTest, RegisterPayloadTypeNetEqDecoder) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000261 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000262 uint8_t rtp_payload_type = 0;
kwibergee1879c2015-10-29 06:20:28 -0700263 NetEqDecoder codec_type = NetEqDecoder::kDecoderPCMu;
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800264 const std::string kCodecName = "Robert\'); DROP TABLE Students;";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000265 EXPECT_CALL(*mock_decoder_database_,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800266 RegisterPayload(rtp_payload_type, codec_type, kCodecName));
267 neteq_->RegisterPayloadType(codec_type, kCodecName, rtp_payload_type);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268}
269
kwiberg5adaf732016-10-04 09:33:27 -0700270TEST_F(NetEqImplTest, RegisterPayloadType) {
271 CreateInstance();
272 constexpr int rtp_payload_type = 0;
273 const SdpAudioFormat format("pcmu", 8000, 1);
274 EXPECT_CALL(*mock_decoder_database_,
275 RegisterPayload(rtp_payload_type, format));
276 neteq_->RegisterPayloadType(rtp_payload_type, format);
277}
278
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000279TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000280 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000282 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200284 // Check that kOK is returned when database returns kDecoderNotFound, because
285 // removing a payload type that was never registered is not an error.
286 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287}
288
kwiberg6b19b562016-09-20 04:02:25 -0700289TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
290 CreateInstance();
291 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
292 neteq_->RemoveAllPayloadTypes();
293}
294
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000296 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000297 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000298 const uint8_t kPayloadType = 0;
299 const uint16_t kFirstSequenceNumber = 0x1234;
300 const uint32_t kFirstTimestamp = 0x12345678;
301 const uint32_t kSsrc = 0x87654321;
302 const uint32_t kFirstReceiveTime = 17;
303 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700304 RTPHeader rtp_header;
305 rtp_header.payloadType = kPayloadType;
306 rtp_header.sequenceNumber = kFirstSequenceNumber;
307 rtp_header.timestamp = kFirstTimestamp;
308 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700309 Packet fake_packet;
310 fake_packet.payload_type = kPayloadType;
311 fake_packet.sequence_number = kFirstSequenceNumber;
312 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313
kwibergc0f2dcf2016-05-31 06:28:03 -0700314 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
315 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100316 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800317 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200318 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800319 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700320 EXPECT_EQ("pcmu", format.name);
321
322 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
323 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
324 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
325 // BWE update function called with first packet.
326 EXPECT_CALL(*mock_decoder,
327 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber,
328 kFirstTimestamp, kFirstReceiveTime));
329 // BWE update function called with second packet.
330 EXPECT_CALL(
331 *mock_decoder,
332 IncomingPacket(_, kPayloadLength, kFirstSequenceNumber + 1,
333 kFirstTimestamp + 160, kFirstReceiveTime + 155));
334 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
335
336 *dec = std::move(mock_decoder);
337 }));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200338 DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, absl::nullopt,
ossu84bc9852016-08-26 05:41:23 -0700339 mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000340
341 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000342 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343 .WillRepeatedly(Return(&info));
344
345 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000346 EXPECT_CALL(*mock_packet_buffer_, Empty())
347 .WillOnce(Return(false)); // Called once after first packet is inserted.
348 EXPECT_CALL(*mock_packet_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 .Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200350 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100352 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
353 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000354 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
355 // index) is a pointer, and the variable pointed to is set to kPayloadType.
356 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
357 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700358 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000359 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700360 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000361
362 // Expectations for DTMF buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000363 EXPECT_CALL(*mock_dtmf_buffer_, Flush())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364 .Times(1);
365
366 // Expectations for delay manager.
367 {
368 // All expectations within this block must be called in this specific order.
369 InSequence sequence; // Dummy variable.
370 // Expectations when the first packet is inserted.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000371 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000372 .Times(2)
373 .WillRepeatedly(Return(-1));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000374 EXPECT_CALL(*mock_delay_manager_, set_last_pack_cng_or_dtmf(0))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375 .Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000376 EXPECT_CALL(*mock_delay_manager_, ResetPacketIatCount()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000377 // Expectations when the second packet is inserted. Slightly different.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000378 EXPECT_CALL(*mock_delay_manager_, last_pack_cng_or_dtmf())
379 .WillOnce(Return(0));
380 EXPECT_CALL(*mock_delay_manager_, SetPacketAudioLength(30))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000381 .WillOnce(Return(0));
382 }
383
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000384 // Insert first packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700385 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386
387 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700388 rtp_header.timestamp += 160;
389 rtp_header.sequenceNumber += 1;
390 neteq_->InsertPacket(rtp_header, payload, kFirstReceiveTime + 155);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000391}
392
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000393TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
394 UseNoMocks();
395 CreateInstance();
396
397 const int kPayloadLengthSamples = 80;
398 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
399 const uint8_t kPayloadType = 17; // Just an arbitrary number.
400 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
401 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700402 RTPHeader rtp_header;
403 rtp_header.payloadType = kPayloadType;
404 rtp_header.sequenceNumber = 0x1234;
405 rtp_header.timestamp = 0x12345678;
406 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000407
kwibergee1879c2015-10-29 06:20:28 -0700408 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800409 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000410
411 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700412 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000413 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700414 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
415 rtp_header.timestamp += kPayloadLengthSamples;
416 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000417 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
418 }
419
420 // Insert one more packet and make sure the buffer got flushed. That is, it
421 // should only hold one single packet.
422 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700423 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700424 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700425 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700426 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
427 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000428}
429
solenberg2779bab2016-11-17 04:45:19 -0800430TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
431 TestDtmfPacket(NetEqDecoder::kDecoderAVT);
432}
solenberg99df6c02016-10-11 04:35:34 -0700433
solenberg2779bab2016-11-17 04:45:19 -0800434TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
435 TestDtmfPacket(NetEqDecoder::kDecoderAVT16kHz);
436}
solenberg99df6c02016-10-11 04:35:34 -0700437
solenberg2779bab2016-11-17 04:45:19 -0800438TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
439 TestDtmfPacket(NetEqDecoder::kDecoderAVT32kHz);
440}
solenberg99df6c02016-10-11 04:35:34 -0700441
solenberg2779bab2016-11-17 04:45:19 -0800442TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
443 TestDtmfPacket(NetEqDecoder::kDecoderAVT48kHz);
solenberg99df6c02016-10-11 04:35:34 -0700444}
445
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000446// This test verifies that timestamps propagate from the incoming packets
447// through to the sync buffer and to the playout timestamp.
448TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
449 UseNoMocks();
450 CreateInstance();
451
452 const uint8_t kPayloadType = 17; // Just an arbitrary number.
453 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
454 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700455 const size_t kPayloadLengthSamples =
456 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000457 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
458 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700459 RTPHeader rtp_header;
460 rtp_header.payloadType = kPayloadType;
461 rtp_header.sequenceNumber = 0x1234;
462 rtp_header.timestamp = 0x12345678;
463 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000464
465 // This is a dummy decoder that produces as many output samples as the input
466 // has bytes. The output is an increasing series, starting at 1 for the first
467 // sample, and then increasing by 1 for each sample.
468 class CountingSamplesDecoder : public AudioDecoder {
469 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000470 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000471
472 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100473 int DecodeInternal(const uint8_t* encoded,
474 size_t encoded_len,
475 int /* sample_rate_hz */,
476 int16_t* decoded,
477 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000478 for (size_t i = 0; i < encoded_len; ++i) {
479 decoded[i] = next_value_++;
480 }
481 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200482 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000483 }
484
Karl Wiberg43766482015-08-27 15:22:11 +0200485 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000486
kwiberg347d3512016-06-16 01:59:09 -0700487 int SampleRateHz() const override { return kSampleRateHz; }
488
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000489 size_t Channels() const override { return 1; }
490
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000491 uint16_t next_value() const { return next_value_; }
492
493 private:
494 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000495 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000496
kwibergee1879c2015-10-29 06:20:28 -0700497 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
498 &decoder_, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700499 "dummy name", kPayloadType));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000500
501 // Insert one packet.
502 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700503 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000504
505 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700506 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800507 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700508 bool muted;
509 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
510 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800511 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
512 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800513 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000514
515 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700516 EXPECT_EQ(kPayloadLengthSamples,
517 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000518
519 // The value of the last of the output samples is the same as the number of
520 // samples played from the decoded packet. Thus, this number + the RTP
521 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200522 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700523 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200524 absl::optional<uint32_t>(rtp_header.timestamp +
525 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700526 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000527
528 // Check the timestamp for the last value in the sync buffer. This should
529 // be one full frame length ahead of the RTP timestamp.
530 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
531 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700532 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000533 sync_buffer->end_timestamp());
534
535 // Check that the number of samples still to play from the sync buffer add
536 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800537 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700538 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800539 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000540}
541
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000542TEST_F(NetEqImplTest, ReorderedPacket) {
543 UseNoMocks();
544 CreateInstance();
545
546 const uint8_t kPayloadType = 17; // Just an arbitrary number.
547 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
548 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700549 const size_t kPayloadLengthSamples =
550 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000551 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
552 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700553 RTPHeader rtp_header;
554 rtp_header.payloadType = kPayloadType;
555 rtp_header.sequenceNumber = 0x1234;
556 rtp_header.timestamp = 0x12345678;
557 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000558
559 // Create a mock decoder object.
560 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200561 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700562 EXPECT_CALL(mock_decoder, SampleRateHz())
563 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000564 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000565 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
566 .WillRepeatedly(Return(0));
henrik.lundin034154b2016-04-27 06:11:50 -0700567 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200568 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000569 int16_t dummy_output[kPayloadLengthSamples] = {0};
570 // The below expectation will make the mock decoder write
571 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100572 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
573 kSampleRateHz, _, _))
574 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000575 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100576 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200577 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
kwibergee1879c2015-10-29 06:20:28 -0700578 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
579 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700580 "dummy name", kPayloadType));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000581
582 // Insert one packet.
583 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700584 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000585
586 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700587 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800588 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700589 bool muted;
590 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800591 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
592 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800593 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000594
595 // Insert two more packets. The first one is out of order, and is already too
596 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700597 rtp_header.sequenceNumber -= 1;
598 rtp_header.timestamp -= kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000599 payload[0] = 1;
600 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700601 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
602 rtp_header.sequenceNumber += 2;
603 rtp_header.timestamp += 2 * kPayloadLengthSamples;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000604 payload[0] = 2;
605 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700606 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000607
608 // Expect only the second packet to be decoded (the one with "2" as the first
609 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100610 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
611 kSampleRateHz, _, _))
612 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000613 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100614 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200615 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000616
617 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700618 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800619 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
620 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800621 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000622
623 // Now check the packet buffer, and make sure it is empty, since the
624 // out-of-order packet should have been discarded.
625 EXPECT_TRUE(packet_buffer_->Empty());
626
627 EXPECT_CALL(mock_decoder, Die());
628}
629
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000630// This test verifies that NetEq can handle the situation where the first
631// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000632TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000633 UseNoMocks();
634 CreateInstance();
635
636 const uint8_t kPayloadType = 17; // Just an arbitrary number.
637 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
638 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700639 const size_t kPayloadLengthSamples =
640 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700641 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000642 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700643 RTPHeader rtp_header;
644 rtp_header.payloadType = kPayloadType;
645 rtp_header.sequenceNumber = 0x1234;
646 rtp_header.timestamp = 0x12345678;
647 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000648
649 // Insert one packet. Note that we have not registered any payload type, so
650 // this packet will be rejected.
651 EXPECT_EQ(NetEq::kFail,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700652 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000653
654 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700655 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800656 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700657 bool muted;
658 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800659 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
660 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
661 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800662 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000663
664 // Register the payload type.
kwibergee1879c2015-10-29 06:20:28 -0700665 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800666 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000667
668 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700669 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700670 rtp_header.sequenceNumber++;
671 rtp_header.timestamp += kPayloadLengthSamples;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000672 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700673 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000674 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
675 }
676
677 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700678 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700679 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800680 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
681 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
682 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800683 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000684 << "NetEq did not decode the packets as expected.";
685 }
686}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000687
688// This test verifies that NetEq can handle comfort noise and enters/quits codec
689// internal CNG mode properly.
690TEST_F(NetEqImplTest, CodecInternalCng) {
691 UseNoMocks();
692 CreateInstance();
693
694 const uint8_t kPayloadType = 17; // Just an arbitrary number.
695 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
696 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700697 const size_t kPayloadLengthSamples =
698 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
699 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000700 uint8_t payload[kPayloadLengthBytes] = {0};
701 int16_t dummy_output[kPayloadLengthSamples] = {0};
702
henrik.lundin246ef3e2017-04-24 09:14:32 -0700703 RTPHeader rtp_header;
704 rtp_header.payloadType = kPayloadType;
705 rtp_header.sequenceNumber = 0x1234;
706 rtp_header.timestamp = 0x12345678;
707 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000708
709 // Create a mock decoder object.
710 MockAudioDecoder mock_decoder;
Karl Wiberg43766482015-08-27 15:22:11 +0200711 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700712 EXPECT_CALL(mock_decoder, SampleRateHz())
713 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000714 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000715 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
716 .WillRepeatedly(Return(0));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700717 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200718 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700719 // Packed duration when asking the decoder for more CNG data (without a new
720 // packet).
721 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200722 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000723
724 // Pointee(x) verifies that first byte of the payload equals x, this makes it
725 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100726 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
727 kSampleRateKhz * 1000, _, _))
728 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000729 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100730 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200731 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000732
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100733 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
734 kSampleRateKhz * 1000, _, _))
735 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000736 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100737 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200738 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000739
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100740 EXPECT_CALL(mock_decoder,
741 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
742 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000743 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100744 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200745 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000746
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100747 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
748 kSampleRateKhz * 1000, _, _))
749 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000750 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100751 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200752 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000753
Karl Wibergd8399e62015-05-25 14:39:56 +0200754 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
kwibergee1879c2015-10-29 06:20:28 -0700755 &mock_decoder, NetEqDecoder::kDecoderOpus,
kwiberg342f7402016-06-16 03:18:00 -0700756 "dummy name", kPayloadType));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000757
758 // Insert one packet (decoder will return speech).
759 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700760 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000761
762 // Insert second packet (decoder will return CNG).
763 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700764 rtp_header.sequenceNumber++;
765 rtp_header.timestamp += kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000766 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700767 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000768
Peter Kastingdce40cf2015-08-24 14:52:23 -0700769 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800770 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800771 AudioFrame::SpeechType expected_type[8] = {
772 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech,
773 AudioFrame::kCNG, AudioFrame::kCNG,
774 AudioFrame::kCNG, AudioFrame::kCNG,
775 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000776 };
777 int expected_timestamp_increment[8] = {
778 -1, // will not be used.
779 10 * kSampleRateKhz,
henrik.lundin0d96ab72016-04-06 12:28:26 -0700780 -1, -1, // timestamp will be empty during CNG mode; indicated by -1 here.
781 -1, -1,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000782 50 * kSampleRateKhz, 10 * kSampleRateKhz
783 };
784
henrik.lundin7a926812016-05-12 13:51:28 -0700785 bool muted;
786 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200787 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700788 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000789
henrik.lundin0d96ab72016-04-06 12:28:26 -0700790 // Lambda for verifying the timestamps.
791 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200792 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700793 if (expected_timestamp_increment[i] == -1) {
794 // Expect to get an empty timestamp value during CNG and PLC.
795 EXPECT_FALSE(ts) << "i = " << i;
796 } else {
797 ASSERT_TRUE(ts) << "i = " << i;
798 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
799 << "i = " << i;
800 last_timestamp = ts;
801 }
802 };
803
Peter Kastingdce40cf2015-08-24 14:52:23 -0700804 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800805 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
806 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800807 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700808 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700809 SCOPED_TRACE("");
810 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000811 }
812
813 // Insert third packet, which leaves a gap from last packet.
814 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700815 rtp_header.sequenceNumber += 2;
816 rtp_header.timestamp += 2 * kPayloadLengthSamples;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000817 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700818 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000819
Peter Kastingdce40cf2015-08-24 14:52:23 -0700820 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800821 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
822 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800823 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700824 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700825 SCOPED_TRACE("");
826 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000827 }
828
829 // Now check the packet buffer, and make sure it is empty.
830 EXPECT_TRUE(packet_buffer_->Empty());
831
832 EXPECT_CALL(mock_decoder, Die());
833}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000834
835TEST_F(NetEqImplTest, UnsupportedDecoder) {
836 UseNoMocks();
837 CreateInstance();
minyue5bd33972016-05-02 04:46:11 -0700838 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800839 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000840
841 const uint8_t kPayloadType = 17; // Just an arbitrary number.
842 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
843 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000844
Peter Kastingdce40cf2015-08-24 14:52:23 -0700845 const size_t kPayloadLengthSamples =
846 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000847 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700848 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200849 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700850 RTPHeader rtp_header;
851 rtp_header.payloadType = kPayloadType;
852 rtp_header.sequenceNumber = 0x1234;
853 rtp_header.timestamp = 0x12345678;
854 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000855
ossu61a208b2016-09-20 01:38:00 -0700856 ::testing::NiceMock<MockAudioDecoder> decoder;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000857
858 const uint8_t kFirstPayloadValue = 1;
859 const uint8_t kSecondPayloadValue = 2;
860
ossu61a208b2016-09-20 01:38:00 -0700861 EXPECT_CALL(decoder,
862 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
863 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200864 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000865
ossu61a208b2016-09-20 01:38:00 -0700866 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000867 .Times(0);
868
ossu61a208b2016-09-20 01:38:00 -0700869 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
870 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000871 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700872 .WillOnce(DoAll(
873 SetArrayArgument<3>(dummy_output,
874 dummy_output + kPayloadLengthSamples * kChannels),
875 SetArgPointee<4>(AudioDecoder::kSpeech),
876 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000877
ossu61a208b2016-09-20 01:38:00 -0700878 EXPECT_CALL(decoder,
879 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
880 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200881 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700882
883 EXPECT_CALL(decoder, SampleRateHz())
884 .WillRepeatedly(Return(kSampleRateHz));
885
886 EXPECT_CALL(decoder, Channels())
887 .WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000888
kwibergee1879c2015-10-29 06:20:28 -0700889 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
ossu61a208b2016-09-20 01:38:00 -0700890 &decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -0700891 "dummy name", kPayloadType));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000892
893 // Insert one packet.
894 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
895 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700896 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000897
898 // Insert another packet.
899 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700900 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000901 // The second timestamp needs to be at least 30 ms after the first to make
902 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700903 rtp_header.timestamp += 3 * kPayloadLengthSamples;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000904 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700905 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000906
henrik.lundin6d8e0112016-03-04 10:34:21 -0800907 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700908 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800909 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200910 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700911 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800912 // Output size and number of channels should be correct.
913 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
914 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
915 EXPECT_EQ(kChannels, output.num_channels_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000916
henrik.lundin6d8e0112016-03-04 10:34:21 -0800917 // Second call to GetAudio will decode the packet that is ok. No errors are
918 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700919 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800920 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
921 EXPECT_EQ(kChannels, output.num_channels_);
ossu61a208b2016-09-20 01:38:00 -0700922
923 // Die isn't called through NiceMock (since it's called by the
924 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
925 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000926}
927
henrik.lundin116c84e2015-08-27 13:14:48 -0700928// This test inserts packets until the buffer is flushed. After that, it asks
929// NetEq for the network statistics. The purpose of the test is to make sure
930// that even though the buffer size increment is negative (which it becomes when
931// the packet causing a flush is inserted), the packet length stored in the
932// decision logic remains valid.
933TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
934 UseNoMocks();
935 CreateInstance();
936
937 const size_t kPayloadLengthSamples = 80;
938 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
939 const uint8_t kPayloadType = 17; // Just an arbitrary number.
940 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
941 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700942 RTPHeader rtp_header;
943 rtp_header.payloadType = kPayloadType;
944 rtp_header.sequenceNumber = 0x1234;
945 rtp_header.timestamp = 0x12345678;
946 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -0700947
kwibergee1879c2015-10-29 06:20:28 -0700948 EXPECT_EQ(NetEq::kOK, neteq_->RegisterPayloadType(
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800949 NetEqDecoder::kDecoderPCM16B, "", kPayloadType));
henrik.lundin116c84e2015-08-27 13:14:48 -0700950
951 // Insert packets until the buffer flushes.
952 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
953 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
954 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -0700955 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
956 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
957 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -0700958 }
959 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
960
961 // Ask for network statistics. This should not crash.
962 NetEqNetworkStatistics stats;
963 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
964}
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200965
966TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
967 UseNoMocks();
968 CreateInstance();
969
970 const uint8_t kPayloadType = 17; // Just an arbitrary number.
971 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
972 const int kSampleRateHz = 8000;
973 const size_t kPayloadLengthSamples =
974 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
975 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
976 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700977 RTPHeader rtp_header;
978 rtp_header.payloadType = kPayloadType;
979 rtp_header.sequenceNumber = 0x1234;
980 rtp_header.timestamp = 0x12345678;
981 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200982
983 // Create a mock decoder object.
984 MockAudioDecoder mock_decoder;
985 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700986 EXPECT_CALL(mock_decoder, SampleRateHz())
987 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200988 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
989 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
990 .WillRepeatedly(Return(0));
991 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200992 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200993 int16_t dummy_output[kPayloadLengthSamples] = {0};
994 // The below expectation will make the mock decoder write
995 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
996 // speech. That is, the decoded length is 5 samples shorter than the expected.
997 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100998 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +0200999 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001000 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001001 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001002 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001003 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
kwibergee1879c2015-10-29 06:20:28 -07001004 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1005 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001006 "dummy name", kPayloadType));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001007
1008 // Insert one packet.
1009 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001010 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001011
1012 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1013
1014 // Pull audio once.
1015 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001016 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001017 bool muted;
1018 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001019 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1020 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001021 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001022
1023 EXPECT_CALL(mock_decoder, Die());
1024}
minyuel6d92bf52015-09-23 15:20:39 +02001025
1026// This test checks the behavior of NetEq when audio decoder fails.
1027TEST_F(NetEqImplTest, DecodingError) {
1028 UseNoMocks();
1029 CreateInstance();
1030
1031 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1032 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1033 const int kSampleRateHz = 8000;
1034 const int kDecoderErrorCode = -97; // Any negative number.
1035
1036 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1037 const size_t kFrameLengthSamples =
1038 static_cast<size_t>(5 * kSampleRateHz / 1000);
1039
1040 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1041
1042 uint8_t payload[kPayloadLengthBytes] = {0};
1043
henrik.lundin246ef3e2017-04-24 09:14:32 -07001044 RTPHeader rtp_header;
1045 rtp_header.payloadType = kPayloadType;
1046 rtp_header.sequenceNumber = 0x1234;
1047 rtp_header.timestamp = 0x12345678;
1048 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001049
1050 // Create a mock decoder object.
1051 MockAudioDecoder mock_decoder;
1052 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001053 EXPECT_CALL(mock_decoder, SampleRateHz())
1054 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001055 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1056 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1057 .WillRepeatedly(Return(0));
1058 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001059 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001060 EXPECT_CALL(mock_decoder, ErrorCode())
1061 .WillOnce(Return(kDecoderErrorCode));
1062 EXPECT_CALL(mock_decoder, HasDecodePlc())
1063 .WillOnce(Return(false));
1064 int16_t dummy_output[kFrameLengthSamples] = {0};
1065
1066 {
1067 InSequence sequence; // Dummy variable.
1068 // Mock decoder works normally the first time.
1069 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001070 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001071 .Times(3)
1072 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001073 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001074 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001075 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001076 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001077 .RetiresOnSaturation();
1078
1079 // Then mock decoder fails. A common reason for failure can be buffer being
1080 // too short
1081 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001082 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001083 .WillOnce(Return(-1))
1084 .RetiresOnSaturation();
1085
1086 // Mock decoder finally returns to normal.
1087 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001088 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001089 .Times(2)
1090 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001091 DoAll(SetArrayArgument<3>(dummy_output,
1092 dummy_output + kFrameLengthSamples),
1093 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001094 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001095 }
1096
kwibergee1879c2015-10-29 06:20:28 -07001097 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1098 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001099 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001100
1101 // Insert packets.
1102 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001103 rtp_header.sequenceNumber += 1;
1104 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001105 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001106 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001107 }
1108
1109 // Pull audio.
1110 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001111 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001112 bool muted;
1113 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001114 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1115 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001116 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001117
1118 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001119 EXPECT_EQ(NetEq::kFail, 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 // We are not expecting anything for output.speech_type_, since an error was
1123 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001124
1125 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001126 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001127 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1128 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001129 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001130
1131 // Pull audio again, should behave normal.
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::kNormalSpeech, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001136
1137 EXPECT_CALL(mock_decoder, Die());
1138}
1139
1140// This test checks the behavior of NetEq when audio decoder fails during CNG.
1141TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1142 UseNoMocks();
1143 CreateInstance();
1144
1145 const uint8_t kPayloadType = 17; // Just an arbitrary number.
1146 const uint32_t kReceiveTime = 17; // Value doesn't matter for this test.
1147 const int kSampleRateHz = 8000;
1148 const int kDecoderErrorCode = -97; // Any negative number.
1149
1150 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1151 const size_t kFrameLengthSamples =
1152 static_cast<size_t>(5 * kSampleRateHz / 1000);
1153
1154 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1155
1156 uint8_t payload[kPayloadLengthBytes] = {0};
1157
henrik.lundin246ef3e2017-04-24 09:14:32 -07001158 RTPHeader rtp_header;
1159 rtp_header.payloadType = kPayloadType;
1160 rtp_header.sequenceNumber = 0x1234;
1161 rtp_header.timestamp = 0x12345678;
1162 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001163
1164 // Create a mock decoder object.
1165 MockAudioDecoder mock_decoder;
1166 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001167 EXPECT_CALL(mock_decoder, SampleRateHz())
1168 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001169 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
1170 EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
1171 .WillRepeatedly(Return(0));
1172 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001173 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
minyuel6d92bf52015-09-23 15:20:39 +02001174 EXPECT_CALL(mock_decoder, ErrorCode())
1175 .WillOnce(Return(kDecoderErrorCode));
1176 int16_t dummy_output[kFrameLengthSamples] = {0};
1177
1178 {
1179 InSequence sequence; // Dummy variable.
1180 // Mock decoder works normally the first 2 times.
1181 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001182 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001183 .Times(2)
1184 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001185 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001186 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001187 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001188 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001189 .RetiresOnSaturation();
1190
1191 // Then mock decoder fails. A common reason for failure can be buffer being
1192 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001193 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001194 .WillOnce(Return(-1))
1195 .RetiresOnSaturation();
1196
1197 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001198 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001199 .Times(2)
1200 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001201 DoAll(SetArrayArgument<3>(dummy_output,
1202 dummy_output + kFrameLengthSamples),
1203 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001204 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001205 }
1206
kwibergee1879c2015-10-29 06:20:28 -07001207 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1208 &mock_decoder, NetEqDecoder::kDecoderPCM16B,
kwiberg342f7402016-06-16 03:18:00 -07001209 "dummy name", kPayloadType));
minyuel6d92bf52015-09-23 15:20:39 +02001210
1211 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1212 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001213 rtp_header.sequenceNumber += 1;
1214 rtp_header.timestamp += kFrameLengthSamples;
minyuel6d92bf52015-09-23 15:20:39 +02001215 EXPECT_EQ(NetEq::kOK,
henrik.lundin246ef3e2017-04-24 09:14:32 -07001216 neteq_->InsertPacket(rtp_header, payload, kReceiveTime));
minyuel6d92bf52015-09-23 15:20:39 +02001217 }
1218
1219 // Pull audio.
1220 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001221 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001222 bool muted;
1223 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001224 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1225 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001226 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001227
1228 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001229 EXPECT_EQ(NetEq::kFail, 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 // We are not expecting anything for output.speech_type_, since an error was
1233 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001234
1235 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001236 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001237 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1238 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001239 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001240
1241 EXPECT_CALL(mock_decoder, Die());
1242}
1243
henrik.lundind89814b2015-11-23 06:49:25 -08001244// Tests that the return value from last_output_sample_rate_hz() is equal to the
1245// configured inital sample rate.
1246TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1247 UseNoMocks();
1248 config_.sample_rate_hz = 48000;
1249 CreateInstance();
1250 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1251}
1252
henrik.lundined497212016-04-25 10:11:38 -07001253TEST_F(NetEqImplTest, TickTimerIncrement) {
1254 UseNoMocks();
1255 CreateInstance();
1256 ASSERT_TRUE(tick_timer_);
1257 EXPECT_EQ(0u, tick_timer_->ticks());
1258 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001259 bool muted;
1260 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001261 EXPECT_EQ(1u, tick_timer_->ticks());
1262}
1263
henrik.lundin114c1b32017-04-26 07:47:32 -07001264TEST_F(NetEqImplTest, TargetDelayMs) {
1265 UseNoMocks();
1266 use_mock_delay_manager_ = true;
1267 CreateInstance();
1268 // Let the dummy target delay be 17 packets.
1269 constexpr int kTargetLevelPacketsQ8 = 17 << 8;
1270 EXPECT_CALL(*mock_delay_manager_, TargetLevel())
1271 .WillOnce(Return(kTargetLevelPacketsQ8));
1272 // Default packet size before any packet has been decoded is 30 ms, so we are
1273 // expecting 17 * 30 = 510 ms target delay.
1274 EXPECT_EQ(17 * 30, neteq_->TargetDelayMs());
1275}
1276
henrik.lundinb8c55b12017-05-10 07:38:01 -07001277TEST_F(NetEqImplTest, InsertEmptyPacket) {
1278 UseNoMocks();
1279 use_mock_delay_manager_ = true;
1280 CreateInstance();
1281
1282 RTPHeader rtp_header;
1283 rtp_header.payloadType = 17;
1284 rtp_header.sequenceNumber = 0x1234;
1285 rtp_header.timestamp = 0x12345678;
1286 rtp_header.ssrc = 0x87654321;
1287
1288 EXPECT_CALL(*mock_delay_manager_, RegisterEmptyPacket());
1289 neteq_->InsertEmptyPacket(rtp_header);
1290}
1291
minyue5bd33972016-05-02 04:46:11 -07001292class Decoder120ms : public AudioDecoder {
1293 public:
kwiberg347d3512016-06-16 01:59:09 -07001294 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1295 : sample_rate_hz_(sample_rate_hz),
1296 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001297 speech_type_(speech_type) {}
1298
1299 int DecodeInternal(const uint8_t* encoded,
1300 size_t encoded_len,
1301 int sample_rate_hz,
1302 int16_t* decoded,
1303 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001304 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001305 size_t decoded_len =
1306 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1307 for (size_t i = 0; i < decoded_len; ++i) {
1308 decoded[i] = next_value_++;
1309 }
1310 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001311 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001312 }
1313
1314 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001315 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001316 size_t Channels() const override { return 2; }
1317
1318 private:
kwiberg347d3512016-06-16 01:59:09 -07001319 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001320 int16_t next_value_;
1321 SpeechType speech_type_;
1322};
1323
1324class NetEqImplTest120ms : public NetEqImplTest {
1325 protected:
1326 NetEqImplTest120ms() : NetEqImplTest() {}
1327 virtual ~NetEqImplTest120ms() {}
1328
1329 void CreateInstanceNoMocks() {
1330 UseNoMocks();
1331 CreateInstance();
1332 }
1333
1334 void CreateInstanceWithDelayManagerMock() {
1335 UseNoMocks();
1336 use_mock_delay_manager_ = true;
1337 CreateInstance();
1338 }
1339
1340 uint32_t timestamp_diff_between_packets() const {
1341 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1342 }
1343
1344 uint32_t first_timestamp() const { return 10u; }
1345
1346 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001347 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001348 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001349 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1350 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001351 }
1352 }
1353
1354 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001355 RTPHeader rtp_header;
1356 rtp_header.payloadType = kPayloadType;
1357 rtp_header.sequenceNumber = sequence_number_;
1358 rtp_header.timestamp = timestamp;
1359 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001360 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1361 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001362 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload, 10));
minyue5bd33972016-05-02 04:46:11 -07001363 sequence_number_++;
1364 }
1365
1366 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
kwiberg347d3512016-06-16 01:59:09 -07001367 decoder_.reset(new Decoder120ms(kSamplingFreq_, speech_type));
minyue5bd33972016-05-02 04:46:11 -07001368 ASSERT_EQ(2u, decoder_->Channels());
1369 EXPECT_EQ(NetEq::kOK, neteq_->RegisterExternalDecoder(
1370 decoder_.get(), NetEqDecoder::kDecoderOpus_2ch,
kwiberg342f7402016-06-16 03:18:00 -07001371 "120ms codec", kPayloadType));
minyue5bd33972016-05-02 04:46:11 -07001372 }
1373
1374 std::unique_ptr<Decoder120ms> decoder_;
1375 AudioFrame output_;
1376 const uint32_t kPayloadType = 17;
1377 const uint32_t kSamplingFreq_ = 48000;
1378 uint16_t sequence_number_ = 1;
1379};
1380
minyue5bd33972016-05-02 04:46:11 -07001381TEST_F(NetEqImplTest120ms, CodecInternalCng) {
1382 CreateInstanceNoMocks();
1383 Register120msCodec(AudioDecoder::kComfortNoise);
1384
1385 InsertPacket(first_timestamp());
1386 GetFirstPacket();
1387
henrik.lundin7a926812016-05-12 13:51:28 -07001388 bool muted;
1389 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001390 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1391}
1392
1393TEST_F(NetEqImplTest120ms, Normal) {
1394 CreateInstanceNoMocks();
1395 Register120msCodec(AudioDecoder::kSpeech);
1396
1397 InsertPacket(first_timestamp());
1398 GetFirstPacket();
1399
1400 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1401}
1402
1403TEST_F(NetEqImplTest120ms, Merge) {
1404 CreateInstanceWithDelayManagerMock();
1405
1406 Register120msCodec(AudioDecoder::kSpeech);
1407 InsertPacket(first_timestamp());
1408
1409 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001410 bool muted;
1411 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001412
1413 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1414
1415 // Delay manager reports a target level which should cause a Merge.
1416 EXPECT_CALL(*mock_delay_manager_, TargetLevel()).WillOnce(Return(-10));
1417
henrik.lundin7a926812016-05-12 13:51:28 -07001418 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001419 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1420}
1421
1422TEST_F(NetEqImplTest120ms, Expand) {
1423 CreateInstanceNoMocks();
1424 Register120msCodec(AudioDecoder::kSpeech);
1425
1426 InsertPacket(first_timestamp());
1427 GetFirstPacket();
1428
henrik.lundin7a926812016-05-12 13:51:28 -07001429 bool muted;
1430 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001431 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1432}
1433
1434TEST_F(NetEqImplTest120ms, FastAccelerate) {
1435 CreateInstanceWithDelayManagerMock();
1436 Register120msCodec(AudioDecoder::kSpeech);
1437
1438 InsertPacket(first_timestamp());
1439 GetFirstPacket();
1440 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1441
1442 // Delay manager report buffer limit which should cause a FastAccelerate.
1443 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1444 .Times(1)
1445 .WillOnce(DoAll(SetArgPointee<0>(0), SetArgPointee<1>(0)));
1446
henrik.lundin7a926812016-05-12 13:51:28 -07001447 bool muted;
1448 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001449 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1450}
1451
1452TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
1453 CreateInstanceWithDelayManagerMock();
1454 Register120msCodec(AudioDecoder::kSpeech);
1455
1456 InsertPacket(first_timestamp());
1457 GetFirstPacket();
1458
1459 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1460
1461 // Delay manager report buffer limit which should cause a PreemptiveExpand.
1462 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1463 .Times(1)
1464 .WillOnce(DoAll(SetArgPointee<0>(100), SetArgPointee<1>(100)));
1465
henrik.lundin7a926812016-05-12 13:51:28 -07001466 bool muted;
1467 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001468 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1469}
1470
1471TEST_F(NetEqImplTest120ms, Accelerate) {
1472 CreateInstanceWithDelayManagerMock();
1473 Register120msCodec(AudioDecoder::kSpeech);
1474
1475 InsertPacket(first_timestamp());
1476 GetFirstPacket();
1477
1478 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1479
1480 // Delay manager report buffer limit which should cause a Accelerate.
1481 EXPECT_CALL(*mock_delay_manager_, BufferLimits(_, _))
1482 .Times(1)
1483 .WillOnce(DoAll(SetArgPointee<0>(1), SetArgPointee<1>(2)));
1484
henrik.lundin7a926812016-05-12 13:51:28 -07001485 bool muted;
1486 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001487 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1488}
1489
minyuel6d92bf52015-09-23 15:20:39 +02001490}// namespace webrtc