blob: 4a47a4d71d3a0a1e4ad1c3726b0af9da808921c8 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/audio_coding/neteq/neteq_impl.h"
12
kwiberg84be5112016-04-27 01:19:58 -070013#include <memory>
Alessio Bazzica8f319a32019-07-24 16:47:02 +000014#include <utility>
15#include <vector>
kwiberg84be5112016-04-27 01:19:58 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/neteq/accelerate.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020019#include "modules/audio_coding/neteq/decision_logic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010021#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_coding/neteq/include/neteq.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
25#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020026#include "modules/audio_coding/neteq/mock/mock_neteq_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
28#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020029#include "modules/audio_coding/neteq/neteq_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010031#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_coding/neteq/sync_buffer.h"
33#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010034#include "rtc_base/numerics/safe_conversions.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000035#include "system_wrappers/include/clock.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010036#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010037#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "test/gmock.h"
39#include "test/gtest.h"
40#include "test/mock_audio_decoder.h"
41#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010044using ::testing::AtLeast;
45using ::testing::DoAll;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000046using ::testing::ElementsAre;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047using ::testing::InSequence;
48using ::testing::Invoke;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000049using ::testing::IsEmpty;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000050using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010051using ::testing::Pointee;
52using ::testing::Return;
53using ::testing::ReturnNull;
54using ::testing::SetArgPointee;
55using ::testing::SetArrayArgument;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000056using ::testing::SizeIs;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010057using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058
59namespace webrtc {
60
61// This function is called when inserting a packet list into the mock packet
62// buffer. The purpose is to delete all inserted packets properly, to avoid
63// memory leaks in the test.
64int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070065 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066 return PacketBuffer::kOK;
67}
68
69class NetEqImplTest : public ::testing::Test {
70 protected:
Alessio Bazzica8f319a32019-07-24 16:47:02 +000071 NetEqImplTest() : clock_(0) { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000072
Niels Möllera0f44302018-11-30 10:45:12 +010073 void CreateInstance(
74 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
75 ASSERT_TRUE(decoder_factory);
Alessio Bazzica8f319a32019-07-24 16:47:02 +000076 NetEqImpl::Dependencies deps(config_, &clock_, decoder_factory);
henrik.lundin1d9061e2016-04-26 12:19:34 -070077
78 // Get a local pointer to NetEq's TickTimer object.
79 tick_timer_ = deps.tick_timer.get();
80
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000081 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070082 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
83 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000084 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
85 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070086 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000087 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070088 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000089
henrik.lundin1d9061e2016-04-26 12:19:34 -070090 if (use_mock_dtmf_buffer_) {
91 std::unique_ptr<MockDtmfBuffer> mock(
92 new MockDtmfBuffer(config_.sample_rate_hz));
93 mock_dtmf_buffer_ = mock.get();
94 deps.dtmf_buffer = std::move(mock);
95 }
96 dtmf_buffer_ = deps.dtmf_buffer.get();
97
98 if (use_mock_dtmf_tone_generator_) {
99 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
100 mock_dtmf_tone_generator_ = mock.get();
101 deps.dtmf_tone_generator = std::move(mock);
102 }
103 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
104
105 if (use_mock_packet_buffer_) {
106 std::unique_ptr<MockPacketBuffer> mock(
107 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
108 mock_packet_buffer_ = mock.get();
109 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700110 }
111 packet_buffer_ = deps.packet_buffer.get();
112
Ivo Creusen53a31f72019-10-24 15:20:39 +0200113 if (use_mock_neteq_controller_) {
114 std::unique_ptr<MockNetEqController> mock(new MockNetEqController());
115 mock_neteq_controller_ = mock.get();
116 deps.neteq_controller = std::move(mock);
117 } else {
118 deps.stats = std::make_unique<StatisticsCalculator>();
119 NetEqController::Config controller_config;
120 controller_config.tick_timer = tick_timer_;
121 controller_config.base_min_delay_ms = config_.min_delay_ms;
122 controller_config.enable_rtx_handling = config_.enable_rtx_handling;
123 controller_config.allow_time_stretching = true;
124 controller_config.max_packets_in_buffer = config_.max_packets_in_buffer;
125 deps.neteq_controller =
126 std::make_unique<DecisionLogic>(std::move(controller_config));
127 }
128 neteq_controller_ = deps.neteq_controller.get();
129
henrik.lundin1d9061e2016-04-26 12:19:34 -0700130 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";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000148 use_mock_decoder_database_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200149 use_mock_neteq_controller_ = false;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000150 use_mock_dtmf_buffer_ = false;
151 use_mock_dtmf_tone_generator_ = false;
152 use_mock_packet_buffer_ = false;
153 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000154 }
155
156 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000157 if (use_mock_decoder_database_) {
158 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
159 }
Ivo Creusen53a31f72019-10-24 15:20:39 +0200160 if (use_mock_neteq_controller_) {
161 EXPECT_CALL(*mock_neteq_controller_, Die()).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000162 }
163 if (use_mock_dtmf_buffer_) {
164 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
165 }
166 if (use_mock_dtmf_tone_generator_) {
167 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
168 }
169 if (use_mock_packet_buffer_) {
170 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
171 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000172 }
173
Niels Möller05543682019-01-10 16:55:06 +0100174 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800175 const size_t kPayloadLength = 4;
176 const uint8_t kPayloadType = 110;
solenberg2779bab2016-11-17 04:45:19 -0800177 const int kSampleRateHz = 16000;
178 config_.sample_rate_hz = kSampleRateHz;
179 UseNoMocks();
180 CreateInstance();
181 // Event: 2, E bit, Volume: 17, Length: 4336.
Jonas Olssona4d87372019-07-05 19:08:33 +0200182 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700183 RTPHeader rtp_header;
184 rtp_header.payloadType = kPayloadType;
185 rtp_header.sequenceNumber = 0x1234;
186 rtp_header.timestamp = 0x12345678;
187 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800188
Niels Möller05543682019-01-10 16:55:06 +0100189 EXPECT_TRUE(neteq_->RegisterPayloadType(
190 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800191
192 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200193 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
solenberg2779bab2016-11-17 04:45:19 -0800194
195 // Pull audio once.
196 const size_t kMaxOutputSize =
197 static_cast<size_t>(10 * kSampleRateHz / 1000);
198 AudioFrame output;
199 bool muted;
200 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
201 ASSERT_FALSE(muted);
202 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
203 EXPECT_EQ(1u, output.num_channels_);
204 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
205
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000206 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
207 // returned by |GetAudio()|.
208 EXPECT_THAT(output.packet_infos_, IsEmpty());
209
solenberg2779bab2016-11-17 04:45:19 -0800210 // Verify first 64 samples of actual output.
Jonas Olssona4d87372019-07-05 19:08:33 +0200211 const std::vector<int16_t> kOutput(
212 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
213 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
214 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
215 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
216 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
217 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
218 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800219 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700220 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800221 }
222
henrik.lundin1d9061e2016-04-26 12:19:34 -0700223 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000224 NetEq::Config config_;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000225 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700226 TickTimer* tick_timer_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700227 MockDecoderDatabase* mock_decoder_database_ = nullptr;
228 DecoderDatabase* decoder_database_ = nullptr;
229 bool use_mock_decoder_database_ = true;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200230 MockNetEqController* mock_neteq_controller_ = nullptr;
231 NetEqController* neteq_controller_ = nullptr;
232 bool use_mock_neteq_controller_ = true;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700233 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
234 DtmfBuffer* dtmf_buffer_ = nullptr;
235 bool use_mock_dtmf_buffer_ = true;
236 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
237 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
238 bool use_mock_dtmf_tone_generator_ = true;
239 MockPacketBuffer* mock_packet_buffer_ = nullptr;
240 PacketBuffer* packet_buffer_ = nullptr;
241 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700242 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
243 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700244 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000245};
246
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000247// This tests the interface class NetEq.
248// TODO(hlundin): Move to separate file?
249TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000250 NetEq::Config config;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000251 SimulatedClock clock(0);
252 NetEq* neteq =
253 NetEq::Create(config, &clock, CreateBuiltinAudioDecoderFactory());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000254 delete neteq;
255}
256
kwiberg5adaf732016-10-04 09:33:27 -0700257TEST_F(NetEqImplTest, RegisterPayloadType) {
258 CreateInstance();
259 constexpr int rtp_payload_type = 0;
260 const SdpAudioFormat format("pcmu", 8000, 1);
261 EXPECT_CALL(*mock_decoder_database_,
262 RegisterPayload(rtp_payload_type, format));
263 neteq_->RegisterPayloadType(rtp_payload_type, format);
264}
265
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266TEST_F(NetEqImplTest, RemovePayloadType) {
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;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000269 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200271 // Check that kOK is returned when database returns kDecoderNotFound, because
272 // removing a payload type that was never registered is not an error.
273 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000274}
275
kwiberg6b19b562016-09-20 04:02:25 -0700276TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
277 CreateInstance();
278 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
279 neteq_->RemoveAllPayloadTypes();
280}
281
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000283 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000284 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285 const uint8_t kPayloadType = 0;
286 const uint16_t kFirstSequenceNumber = 0x1234;
287 const uint32_t kFirstTimestamp = 0x12345678;
288 const uint32_t kSsrc = 0x87654321;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700290 RTPHeader rtp_header;
291 rtp_header.payloadType = kPayloadType;
292 rtp_header.sequenceNumber = kFirstSequenceNumber;
293 rtp_header.timestamp = kFirstTimestamp;
294 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700295 Packet fake_packet;
296 fake_packet.payload_type = kPayloadType;
297 fake_packet.sequence_number = kFirstSequenceNumber;
298 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000299
kwibergc0f2dcf2016-05-31 06:28:03 -0700300 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
301 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100302 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800303 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200304 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800305 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700306 EXPECT_EQ("pcmu", format.name);
307
308 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
309 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
310 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
kwibergc0f2dcf2016-05-31 06:28:03 -0700311 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
312
313 *dec = std::move(mock_decoder);
314 }));
Niels Möller72899062019-01-11 09:36:13 +0100315 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
316 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317
318 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000319 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000320 .WillRepeatedly(Return(&info));
321
322 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000323 EXPECT_CALL(*mock_packet_buffer_, Empty())
324 .WillOnce(Return(false)); // Called once after first packet is inserted.
Jonas Olssona4d87372019-07-05 19:08:33 +0200325 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200326 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100328 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
329 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000330 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
331 // index) is a pointer, and the variable pointed to is set to kPayloadType.
332 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
333 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700334 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000335 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700336 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000337
338 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200339 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000340
341 // Expectations for delay manager.
342 {
343 // All expectations within this block must be called in this specific order.
344 InSequence sequence; // Dummy variable.
345 // Expectations when the first packet is inserted.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200346 EXPECT_CALL(*mock_neteq_controller_,
347 PacketArrived(/*last_cng_or_dtmf*/ false,
348 /*packet_length_samples*/ _,
349 /*should_update_stats*/ _,
350 /*main_sequence_number*/ kFirstSequenceNumber,
351 /*main_timestamp*/ kFirstTimestamp,
352 /*fs_hz*/ 8000));
353 EXPECT_CALL(*mock_neteq_controller_,
354 PacketArrived(/*last_cng_or_dtmf*/ false,
355 /*packet_length_samples*/ _,
356 /*should_update_stats*/ _,
357 /*main_sequence_number*/ kFirstSequenceNumber + 1,
358 /*main_timestamp*/ kFirstTimestamp + 160,
359 /*fs_hz*/ 8000));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360 }
361
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200363 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364
365 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700366 rtp_header.timestamp += 160;
367 rtp_header.sequenceNumber += 1;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200368 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000369}
370
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000371TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
372 UseNoMocks();
373 CreateInstance();
374
375 const int kPayloadLengthSamples = 80;
376 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200377 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000378 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700379 RTPHeader rtp_header;
380 rtp_header.payloadType = kPayloadType;
381 rtp_header.sequenceNumber = 0x1234;
382 rtp_header.timestamp = 0x12345678;
383 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000384
Niels Möller05543682019-01-10 16:55:06 +0100385 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
386 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000387
388 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700389 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
Karl Wiberg45eb1352019-10-10 14:23:00 +0200390 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700391 rtp_header.timestamp += kPayloadLengthSamples;
392 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000393 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
394 }
395
396 // Insert one more packet and make sure the buffer got flushed. That is, it
397 // should only hold one single packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200398 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700399 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700400 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700401 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
402 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000403}
404
solenberg2779bab2016-11-17 04:45:19 -0800405TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100406 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800407}
solenberg99df6c02016-10-11 04:35:34 -0700408
solenberg2779bab2016-11-17 04:45:19 -0800409TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100410 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800411}
solenberg99df6c02016-10-11 04:35:34 -0700412
solenberg2779bab2016-11-17 04:45:19 -0800413TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100414 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800415}
solenberg99df6c02016-10-11 04:35:34 -0700416
solenberg2779bab2016-11-17 04:45:19 -0800417TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100418 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700419}
420
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000421// This test verifies that timestamps propagate from the incoming packets
422// through to the sync buffer and to the playout timestamp.
423TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000424 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000425 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700426 const size_t kPayloadLengthSamples =
427 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000428 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
429 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700430 RTPHeader rtp_header;
431 rtp_header.payloadType = kPayloadType;
432 rtp_header.sequenceNumber = 0x1234;
433 rtp_header.timestamp = 0x12345678;
434 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000435 rtp_header.numCSRCs = 3;
436 rtp_header.arrOfCSRCs[0] = 43;
437 rtp_header.arrOfCSRCs[1] = 65;
438 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000439
440 // This is a dummy decoder that produces as many output samples as the input
441 // has bytes. The output is an increasing series, starting at 1 for the first
442 // sample, and then increasing by 1 for each sample.
443 class CountingSamplesDecoder : public AudioDecoder {
444 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000445 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000446
447 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100448 int DecodeInternal(const uint8_t* encoded,
449 size_t encoded_len,
450 int /* sample_rate_hz */,
451 int16_t* decoded,
452 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000453 for (size_t i = 0; i < encoded_len; ++i) {
454 decoded[i] = next_value_++;
455 }
456 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200457 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000458 }
459
Karl Wiberg43766482015-08-27 15:22:11 +0200460 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000461
kwiberg347d3512016-06-16 01:59:09 -0700462 int SampleRateHz() const override { return kSampleRateHz; }
463
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000464 size_t Channels() const override { return 1; }
465
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000466 uint16_t next_value() const { return next_value_; }
467
468 private:
469 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000470 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000471
Niels Möllerb7180c02018-12-06 13:07:11 +0100472 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
473 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
474
475 UseNoMocks();
476 CreateInstance(decoder_factory);
477
478 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100479 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000480
481 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000482 clock_.AdvanceTimeMilliseconds(123456);
483 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200484 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000485
486 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700487 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800488 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700489 bool muted;
490 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
491 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800492 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
493 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800494 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000495
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000496 // Verify |output.packet_infos_|.
497 ASSERT_THAT(output.packet_infos_, SizeIs(1));
498 {
499 const auto& packet_info = output.packet_infos_[0];
500 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
501 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
502 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
503 EXPECT_FALSE(packet_info.audio_level().has_value());
504 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
505 }
506
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000507 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700508 EXPECT_EQ(kPayloadLengthSamples,
509 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000510
511 // The value of the last of the output samples is the same as the number of
512 // samples played from the decoded packet. Thus, this number + the RTP
513 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200514 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700515 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200516 absl::optional<uint32_t>(rtp_header.timestamp +
517 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700518 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000519
520 // Check the timestamp for the last value in the sync buffer. This should
521 // be one full frame length ahead of the RTP timestamp.
522 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
523 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700524 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000525 sync_buffer->end_timestamp());
526
527 // Check that the number of samples still to play from the sync buffer add
528 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800529 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700530 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800531 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000532}
533
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000534TEST_F(NetEqImplTest, ReorderedPacket) {
535 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100536 // Create a mock decoder object.
537 MockAudioDecoder mock_decoder;
538
539 CreateInstance(
540 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000541
542 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000543 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700544 const size_t kPayloadLengthSamples =
545 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000546 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
547 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700548 RTPHeader rtp_header;
549 rtp_header.payloadType = kPayloadType;
550 rtp_header.sequenceNumber = 0x1234;
551 rtp_header.timestamp = 0x12345678;
552 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000553 rtp_header.extension.hasAudioLevel = true;
554 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000555
Karl Wiberg43766482015-08-27 15:22:11 +0200556 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700557 EXPECT_CALL(mock_decoder, SampleRateHz())
558 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000559 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin034154b2016-04-27 06:11:50 -0700560 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200561 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000562 int16_t dummy_output[kPayloadLengthSamples] = {0};
563 // The below expectation will make the mock decoder write
564 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100565 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
566 kSampleRateHz, _, _))
567 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000568 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100569 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200570 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100571 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
572 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000573
574 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000575 clock_.AdvanceTimeMilliseconds(123456);
576 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200577 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000578
579 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700580 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800581 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700582 bool muted;
583 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800584 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
585 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800586 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000587
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000588 // Verify |output.packet_infos_|.
589 ASSERT_THAT(output.packet_infos_, SizeIs(1));
590 {
591 const auto& packet_info = output.packet_infos_[0];
592 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
593 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
594 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
595 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
596 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
597 }
598
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000599 // Insert two more packets. The first one is out of order, and is already too
600 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700601 rtp_header.sequenceNumber -= 1;
602 rtp_header.timestamp -= kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000603 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000604 payload[0] = 1;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000605 clock_.AdvanceTimeMilliseconds(1000);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200606 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700607 rtp_header.sequenceNumber += 2;
608 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000609 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000610 payload[0] = 2;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000611 clock_.AdvanceTimeMilliseconds(2000);
612 expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200613 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000614
615 // Expect only the second packet to be decoded (the one with "2" as the first
616 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100617 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
618 kSampleRateHz, _, _))
619 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000620 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100621 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200622 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000623
624 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700625 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800626 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
627 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800628 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000629
630 // Now check the packet buffer, and make sure it is empty, since the
631 // out-of-order packet should have been discarded.
632 EXPECT_TRUE(packet_buffer_->Empty());
633
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000634 // Verify |output.packet_infos_|. Expect to only see the second packet.
635 ASSERT_THAT(output.packet_infos_, SizeIs(1));
636 {
637 const auto& packet_info = output.packet_infos_[0];
638 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
639 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
640 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
641 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
642 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
643 }
644
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000645 EXPECT_CALL(mock_decoder, Die());
646}
647
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000648// This test verifies that NetEq can handle the situation where the first
649// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000650TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000651 UseNoMocks();
652 CreateInstance();
653
654 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000655 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700656 const size_t kPayloadLengthSamples =
657 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700658 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000659 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700660 RTPHeader rtp_header;
661 rtp_header.payloadType = kPayloadType;
662 rtp_header.sequenceNumber = 0x1234;
663 rtp_header.timestamp = 0x12345678;
664 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000665
666 // Insert one packet. Note that we have not registered any payload type, so
667 // this packet will be rejected.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200668 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000669
670 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700671 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800672 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700673 bool muted;
674 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800675 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
676 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
677 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800678 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000679 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000680
681 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100682 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
683 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000684
685 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700686 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700687 rtp_header.sequenceNumber++;
688 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200689 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000690 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
691 }
692
693 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700694 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700695 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800696 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
697 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
698 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800699 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000700 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000701 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000702 }
703}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000704
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200705// This test verifies that audio interruption is not logged for the initial
706// PLC period before the first packet is deocoded.
707// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
708TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
709 UseNoMocks();
710 CreateInstance();
711
712 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200713 const int kSampleRateHz = 8000;
714 const size_t kPayloadLengthSamples =
715 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
716 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
717 uint8_t payload[kPayloadLengthBytes] = {0};
718 RTPHeader rtp_header;
719 rtp_header.payloadType = kPayloadType;
720 rtp_header.sequenceNumber = 0x1234;
721 rtp_header.timestamp = 0x12345678;
722 rtp_header.ssrc = 0x87654321;
723
724 // Register the payload type.
725 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
726 SdpAudioFormat("l16", 8000, 1)));
727
728 // Pull audio several times. No packets have been inserted yet.
729 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
730 AudioFrame output;
731 bool muted;
732 for (int i = 0; i < 100; ++i) {
733 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
734 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
735 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
736 EXPECT_EQ(1u, output.num_channels_);
737 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000738 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200739 }
740
741 // Insert 10 packets.
742 for (size_t i = 0; i < 10; ++i) {
743 rtp_header.sequenceNumber++;
744 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200745 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200746 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
747 }
748
749 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
750 for (size_t i = 0; i < 3; ++i) {
751 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
752 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
753 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
754 EXPECT_EQ(1u, output.num_channels_);
755 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
756 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000757 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200758 }
759
760 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200761 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200762}
763
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000764// This test verifies that NetEq can handle comfort noise and enters/quits codec
765// internal CNG mode properly.
766TEST_F(NetEqImplTest, CodecInternalCng) {
767 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100768 // Create a mock decoder object.
769 MockAudioDecoder mock_decoder;
770 CreateInstance(
771 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000772
773 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000774 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700775 const size_t kPayloadLengthSamples =
776 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
777 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000778 uint8_t payload[kPayloadLengthBytes] = {0};
779 int16_t dummy_output[kPayloadLengthSamples] = {0};
780
henrik.lundin246ef3e2017-04-24 09:14:32 -0700781 RTPHeader rtp_header;
782 rtp_header.payloadType = kPayloadType;
783 rtp_header.sequenceNumber = 0x1234;
784 rtp_header.timestamp = 0x12345678;
785 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000786
Karl Wiberg43766482015-08-27 15:22:11 +0200787 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700788 EXPECT_CALL(mock_decoder, SampleRateHz())
789 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000790 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700791 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200792 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700793 // Packed duration when asking the decoder for more CNG data (without a new
794 // packet).
795 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200796 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000797
798 // Pointee(x) verifies that first byte of the payload equals x, this makes it
799 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100800 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
801 kSampleRateKhz * 1000, _, _))
802 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000803 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100804 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200805 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000806
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100807 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
808 kSampleRateKhz * 1000, _, _))
809 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000810 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100811 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200812 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000813
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100814 EXPECT_CALL(mock_decoder,
815 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
816 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000817 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100818 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200819 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000820
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100821 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
822 kSampleRateKhz * 1000, _, _))
823 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000824 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100825 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200826 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000827
Niels Möller50b66d52018-12-11 14:43:21 +0100828 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
829 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000830
831 // Insert one packet (decoder will return speech).
Karl Wiberg45eb1352019-10-10 14:23:00 +0200832 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000833
834 // Insert second packet (decoder will return CNG).
835 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700836 rtp_header.sequenceNumber++;
837 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200838 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000839
Peter Kastingdce40cf2015-08-24 14:52:23 -0700840 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800841 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800842 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200843 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
844 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
845 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000846 int expected_timestamp_increment[8] = {
847 -1, // will not be used.
848 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200849 -1,
850 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
851 -1,
852 -1,
853 50 * kSampleRateKhz,
854 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000855
henrik.lundin7a926812016-05-12 13:51:28 -0700856 bool muted;
857 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200858 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700859 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000860
henrik.lundin0d96ab72016-04-06 12:28:26 -0700861 // Lambda for verifying the timestamps.
862 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200863 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700864 if (expected_timestamp_increment[i] == -1) {
865 // Expect to get an empty timestamp value during CNG and PLC.
866 EXPECT_FALSE(ts) << "i = " << i;
867 } else {
868 ASSERT_TRUE(ts) << "i = " << i;
869 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
870 << "i = " << i;
871 last_timestamp = ts;
872 }
873 };
874
Peter Kastingdce40cf2015-08-24 14:52:23 -0700875 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800876 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
877 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800878 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700879 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700880 SCOPED_TRACE("");
881 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000882 }
883
884 // Insert third packet, which leaves a gap from last packet.
885 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700886 rtp_header.sequenceNumber += 2;
887 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200888 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000889
Peter Kastingdce40cf2015-08-24 14:52:23 -0700890 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800891 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
892 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800893 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700894 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700895 SCOPED_TRACE("");
896 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000897 }
898
899 // Now check the packet buffer, and make sure it is empty.
900 EXPECT_TRUE(packet_buffer_->Empty());
901
902 EXPECT_CALL(mock_decoder, Die());
903}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000904
905TEST_F(NetEqImplTest, UnsupportedDecoder) {
906 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100907 ::testing::NiceMock<MockAudioDecoder> decoder;
908
909 CreateInstance(
910 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700911 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800912 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000913
914 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000915 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000916
Peter Kastingdce40cf2015-08-24 14:52:23 -0700917 const size_t kPayloadLengthSamples =
918 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000919 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700920 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200921 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700922 RTPHeader rtp_header;
923 rtp_header.payloadType = kPayloadType;
924 rtp_header.sequenceNumber = 0x1234;
925 rtp_header.timestamp = 0x12345678;
926 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000927
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000928 const uint8_t kFirstPayloadValue = 1;
929 const uint8_t kSecondPayloadValue = 2;
930
ossu61a208b2016-09-20 01:38:00 -0700931 EXPECT_CALL(decoder,
932 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
933 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200934 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000935
ossu61a208b2016-09-20 01:38:00 -0700936 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000937 .Times(0);
938
ossu61a208b2016-09-20 01:38:00 -0700939 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
940 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000941 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700942 .WillOnce(DoAll(
943 SetArrayArgument<3>(dummy_output,
944 dummy_output + kPayloadLengthSamples * kChannels),
945 SetArgPointee<4>(AudioDecoder::kSpeech),
946 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000947
ossu61a208b2016-09-20 01:38:00 -0700948 EXPECT_CALL(decoder,
949 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
950 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200951 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700952
Jonas Olssona4d87372019-07-05 19:08:33 +0200953 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -0700954
Jonas Olssona4d87372019-07-05 19:08:33 +0200955 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000956
Niels Möllera1eb9c72018-12-07 15:24:42 +0100957 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
958 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000959
960 // Insert one packet.
961 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200962 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000963
964 // Insert another packet.
965 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700966 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000967 // The second timestamp needs to be at least 30 ms after the first to make
968 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700969 rtp_header.timestamp += 3 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200970 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000971
henrik.lundin6d8e0112016-03-04 10:34:21 -0800972 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700973 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800974 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200975 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700976 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800977 // Output size and number of channels should be correct.
978 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
979 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
980 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000981 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000982
henrik.lundin6d8e0112016-03-04 10:34:21 -0800983 // Second call to GetAudio will decode the packet that is ok. No errors are
984 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700985 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800986 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
987 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000988 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -0700989
990 // Die isn't called through NiceMock (since it's called by the
991 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
992 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000993}
994
henrik.lundin116c84e2015-08-27 13:14:48 -0700995// This test inserts packets until the buffer is flushed. After that, it asks
996// NetEq for the network statistics. The purpose of the test is to make sure
997// that even though the buffer size increment is negative (which it becomes when
998// the packet causing a flush is inserted), the packet length stored in the
999// decision logic remains valid.
1000TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1001 UseNoMocks();
1002 CreateInstance();
1003
1004 const size_t kPayloadLengthSamples = 80;
1005 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1006 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin116c84e2015-08-27 13:14:48 -07001007 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001008 RTPHeader rtp_header;
1009 rtp_header.payloadType = kPayloadType;
1010 rtp_header.sequenceNumber = 0x1234;
1011 rtp_header.timestamp = 0x12345678;
1012 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001013
Niels Möller05543682019-01-10 16:55:06 +01001014 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1015 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001016
1017 // Insert packets until the buffer flushes.
1018 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1019 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
Karl Wiberg45eb1352019-10-10 14:23:00 +02001020 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -07001021 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1022 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001023 }
1024 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1025
1026 // Ask for network statistics. This should not crash.
1027 NetEqNetworkStatistics stats;
1028 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1029}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001030
1031TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1032 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001033 // Create a mock decoder object.
1034 MockAudioDecoder mock_decoder;
1035
1036 CreateInstance(
1037 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001038
1039 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001040 const int kSampleRateHz = 8000;
1041 const size_t kPayloadLengthSamples =
1042 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1043 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1044 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001045 RTPHeader rtp_header;
1046 rtp_header.payloadType = kPayloadType;
1047 rtp_header.sequenceNumber = 0x1234;
1048 rtp_header.timestamp = 0x12345678;
1049 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001050
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001051 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001052 EXPECT_CALL(mock_decoder, SampleRateHz())
1053 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001054 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001055 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001056 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001057 int16_t dummy_output[kPayloadLengthSamples] = {0};
1058 // The below expectation will make the mock decoder write
1059 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1060 // speech. That is, the decoded length is 5 samples shorter than the expected.
1061 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001062 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001063 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001064 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001065 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001066 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001067 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001068 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1069 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001070
1071 // Insert one packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +02001072 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001073
1074 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1075
1076 // Pull audio once.
1077 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001078 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001079 bool muted;
1080 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001081 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1082 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001083 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001084 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001085
1086 EXPECT_CALL(mock_decoder, Die());
1087}
minyuel6d92bf52015-09-23 15:20:39 +02001088
1089// This test checks the behavior of NetEq when audio decoder fails.
1090TEST_F(NetEqImplTest, DecodingError) {
1091 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001092 // Create a mock decoder object.
1093 MockAudioDecoder mock_decoder;
1094
1095 CreateInstance(
1096 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001097
1098 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001099 const int kSampleRateHz = 8000;
1100 const int kDecoderErrorCode = -97; // Any negative number.
1101
1102 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1103 const size_t kFrameLengthSamples =
1104 static_cast<size_t>(5 * kSampleRateHz / 1000);
1105
1106 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1107
1108 uint8_t payload[kPayloadLengthBytes] = {0};
1109
henrik.lundin246ef3e2017-04-24 09:14:32 -07001110 RTPHeader rtp_header;
1111 rtp_header.payloadType = kPayloadType;
1112 rtp_header.sequenceNumber = 0x1234;
1113 rtp_header.timestamp = 0x12345678;
1114 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001115
minyuel6d92bf52015-09-23 15:20:39 +02001116 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001117 EXPECT_CALL(mock_decoder, SampleRateHz())
1118 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001119 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001120 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001121 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001122 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1123 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001124 int16_t dummy_output[kFrameLengthSamples] = {0};
1125
1126 {
1127 InSequence sequence; // Dummy variable.
1128 // Mock decoder works normally the first time.
1129 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001130 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001131 .Times(3)
1132 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001133 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001134 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001135 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001136 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001137 .RetiresOnSaturation();
1138
1139 // Then mock decoder fails. A common reason for failure can be buffer being
1140 // too short
1141 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001142 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001143 .WillOnce(Return(-1))
1144 .RetiresOnSaturation();
1145
1146 // Mock decoder finally returns to normal.
1147 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001148 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001149 .Times(2)
1150 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001151 DoAll(SetArrayArgument<3>(dummy_output,
1152 dummy_output + kFrameLengthSamples),
1153 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001154 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001155 }
1156
Niels Möllera1eb9c72018-12-07 15:24:42 +01001157 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1158 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001159
1160 // Insert packets.
1161 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001162 rtp_header.sequenceNumber += 1;
1163 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001164 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001165 }
1166
1167 // Pull audio.
1168 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001169 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001170 bool muted;
1171 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001172 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1173 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001174 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001175 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001176
1177 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001178 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001179 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1180 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001181 // We are not expecting anything for output.speech_type_, since an error was
1182 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001183
1184 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001185 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001186 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1187 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001188 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001189 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001190
1191 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001192 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001193 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1194 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001195 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001196 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001197
1198 EXPECT_CALL(mock_decoder, Die());
1199}
1200
1201// This test checks the behavior of NetEq when audio decoder fails during CNG.
1202TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1203 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001204
1205 // Create a mock decoder object.
1206 MockAudioDecoder mock_decoder;
1207 CreateInstance(
1208 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001209
1210 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001211 const int kSampleRateHz = 8000;
1212 const int kDecoderErrorCode = -97; // Any negative number.
1213
1214 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1215 const size_t kFrameLengthSamples =
1216 static_cast<size_t>(5 * kSampleRateHz / 1000);
1217
1218 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1219
1220 uint8_t payload[kPayloadLengthBytes] = {0};
1221
henrik.lundin246ef3e2017-04-24 09:14:32 -07001222 RTPHeader rtp_header;
1223 rtp_header.payloadType = kPayloadType;
1224 rtp_header.sequenceNumber = 0x1234;
1225 rtp_header.timestamp = 0x12345678;
1226 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001227
minyuel6d92bf52015-09-23 15:20:39 +02001228 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001229 EXPECT_CALL(mock_decoder, SampleRateHz())
1230 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001231 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001232 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001233 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001234 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001235 int16_t dummy_output[kFrameLengthSamples] = {0};
1236
1237 {
1238 InSequence sequence; // Dummy variable.
1239 // Mock decoder works normally the first 2 times.
1240 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001241 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001242 .Times(2)
1243 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001244 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001245 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001246 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001247 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001248 .RetiresOnSaturation();
1249
1250 // Then mock decoder fails. A common reason for failure can be buffer being
1251 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001252 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001253 .WillOnce(Return(-1))
1254 .RetiresOnSaturation();
1255
1256 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001257 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001258 .Times(2)
1259 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001260 DoAll(SetArrayArgument<3>(dummy_output,
1261 dummy_output + kFrameLengthSamples),
1262 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001263 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001264 }
1265
Niels Möller50b66d52018-12-11 14:43:21 +01001266 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1267 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001268
1269 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1270 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001271 rtp_header.sequenceNumber += 1;
1272 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001273 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001274 }
1275
1276 // Pull audio.
1277 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001278 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001279 bool muted;
1280 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001281 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1282 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001283 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001284
1285 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001286 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001287 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1288 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001289 // We are not expecting anything for output.speech_type_, since an error was
1290 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001291
1292 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001293 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001294 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1295 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001296 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001297
1298 EXPECT_CALL(mock_decoder, Die());
1299}
1300
henrik.lundind89814b2015-11-23 06:49:25 -08001301// Tests that the return value from last_output_sample_rate_hz() is equal to the
1302// configured inital sample rate.
1303TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1304 UseNoMocks();
1305 config_.sample_rate_hz = 48000;
1306 CreateInstance();
1307 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1308}
1309
henrik.lundined497212016-04-25 10:11:38 -07001310TEST_F(NetEqImplTest, TickTimerIncrement) {
1311 UseNoMocks();
1312 CreateInstance();
1313 ASSERT_TRUE(tick_timer_);
1314 EXPECT_EQ(0u, tick_timer_->ticks());
1315 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001316 bool muted;
1317 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001318 EXPECT_EQ(1u, tick_timer_->ticks());
1319}
1320
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001321TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1322 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001323 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001324 CreateInstance();
1325
Ivo Creusen53a31f72019-10-24 15:20:39 +02001326 EXPECT_CALL(*mock_neteq_controller_, SetBaseMinimumDelay(_))
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001327 .WillOnce(Return(true))
1328 .WillOnce(Return(false));
1329
1330 const int delay_ms = 200;
1331
1332 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1333 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1334}
1335
1336TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1337 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001338 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001339 CreateInstance();
1340
1341 const int delay_ms = 200;
1342
Ivo Creusen53a31f72019-10-24 15:20:39 +02001343 EXPECT_CALL(*mock_neteq_controller_, GetBaseMinimumDelay())
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001344 .WillOnce(Return(delay_ms));
1345
1346 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1347}
1348
henrik.lundin114c1b32017-04-26 07:47:32 -07001349TEST_F(NetEqImplTest, TargetDelayMs) {
1350 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001351 use_mock_neteq_controller_ = true;
henrik.lundin114c1b32017-04-26 07:47:32 -07001352 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001353 constexpr int kTargetLevelMs = 510;
1354 EXPECT_CALL(*mock_neteq_controller_, TargetLevelMs())
1355 .WillOnce(Return(kTargetLevelMs));
1356 EXPECT_EQ(510, neteq_->TargetDelayMs());
henrik.lundin114c1b32017-04-26 07:47:32 -07001357}
1358
henrik.lundinb8c55b12017-05-10 07:38:01 -07001359TEST_F(NetEqImplTest, InsertEmptyPacket) {
1360 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001361 use_mock_neteq_controller_ = true;
henrik.lundinb8c55b12017-05-10 07:38:01 -07001362 CreateInstance();
1363
1364 RTPHeader rtp_header;
1365 rtp_header.payloadType = 17;
1366 rtp_header.sequenceNumber = 0x1234;
1367 rtp_header.timestamp = 0x12345678;
1368 rtp_header.ssrc = 0x87654321;
1369
Ivo Creusen53a31f72019-10-24 15:20:39 +02001370 EXPECT_CALL(*mock_neteq_controller_, RegisterEmptyPacket());
henrik.lundinb8c55b12017-05-10 07:38:01 -07001371 neteq_->InsertEmptyPacket(rtp_header);
1372}
1373
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001374TEST_F(NetEqImplTest, EnableRtxHandling) {
1375 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001376 use_mock_neteq_controller_ = true;
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001377 config_.enable_rtx_handling = true;
1378 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001379 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001380 .Times(1)
Ivo Creusen53a31f72019-10-24 15:20:39 +02001381 .WillOnce(Return(kNormal));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001382
1383 const int kPayloadLengthSamples = 80;
1384 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1385 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001386 uint8_t payload[kPayloadLengthBytes] = {0};
1387 RTPHeader rtp_header;
1388 rtp_header.payloadType = kPayloadType;
1389 rtp_header.sequenceNumber = 0x1234;
1390 rtp_header.timestamp = 0x12345678;
1391 rtp_header.ssrc = 0x87654321;
1392
Niels Möller05543682019-01-10 16:55:06 +01001393 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1394 SdpAudioFormat("l16", 8000, 1)));
Karl Wiberg45eb1352019-10-10 14:23:00 +02001395 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001396 AudioFrame output;
1397 bool muted;
1398 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1399
1400 // Insert second packet that was sent before the first packet.
1401 rtp_header.sequenceNumber -= 1;
1402 rtp_header.timestamp -= kPayloadLengthSamples;
Ivo Creusen53a31f72019-10-24 15:20:39 +02001403 EXPECT_CALL(*mock_neteq_controller_,
1404 PacketArrived(
1405 /*last_cng_or_dtmf*/ _,
1406 /*packet_length_samples*/ kPayloadLengthSamples,
1407 /*should_update_stats*/ _,
1408 /*main_sequence_number*/ rtp_header.sequenceNumber,
1409 /*main_timestamp*/ rtp_header.timestamp,
1410 /*fs_hz*/ 8000));
1411
Karl Wiberg45eb1352019-10-10 14:23:00 +02001412 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001413}
1414
minyue5bd33972016-05-02 04:46:11 -07001415class Decoder120ms : public AudioDecoder {
1416 public:
kwiberg347d3512016-06-16 01:59:09 -07001417 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1418 : sample_rate_hz_(sample_rate_hz),
1419 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001420 speech_type_(speech_type) {}
1421
1422 int DecodeInternal(const uint8_t* encoded,
1423 size_t encoded_len,
1424 int sample_rate_hz,
1425 int16_t* decoded,
1426 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001427 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001428 size_t decoded_len =
1429 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1430 for (size_t i = 0; i < decoded_len; ++i) {
1431 decoded[i] = next_value_++;
1432 }
1433 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001434 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001435 }
1436
1437 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001438 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001439 size_t Channels() const override { return 2; }
1440
1441 private:
kwiberg347d3512016-06-16 01:59:09 -07001442 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001443 int16_t next_value_;
1444 SpeechType speech_type_;
1445};
1446
1447class NetEqImplTest120ms : public NetEqImplTest {
1448 protected:
1449 NetEqImplTest120ms() : NetEqImplTest() {}
1450 virtual ~NetEqImplTest120ms() {}
1451
1452 void CreateInstanceNoMocks() {
1453 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001454 CreateInstance(decoder_factory_);
1455 EXPECT_TRUE(neteq_->RegisterPayloadType(
1456 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001457 }
1458
1459 void CreateInstanceWithDelayManagerMock() {
1460 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001461 use_mock_neteq_controller_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001462 CreateInstance(decoder_factory_);
1463 EXPECT_TRUE(neteq_->RegisterPayloadType(
1464 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001465 }
1466
1467 uint32_t timestamp_diff_between_packets() const {
1468 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1469 }
1470
1471 uint32_t first_timestamp() const { return 10u; }
1472
1473 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001474 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001475 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001476 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1477 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001478 }
1479 }
1480
1481 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001482 RTPHeader rtp_header;
1483 rtp_header.payloadType = kPayloadType;
1484 rtp_header.sequenceNumber = sequence_number_;
1485 rtp_header.timestamp = timestamp;
1486 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001487 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1488 uint8_t payload[kPayloadLengthBytes] = {0};
Karl Wiberg45eb1352019-10-10 14:23:00 +02001489 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue5bd33972016-05-02 04:46:11 -07001490 sequence_number_++;
1491 }
1492
1493 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001494 const uint32_t sampling_freq = kSamplingFreq_;
1495 decoder_factory_ =
1496 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1497 [sampling_freq, speech_type]() {
1498 std::unique_ptr<AudioDecoder> decoder =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001499 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
Niels Möllera0f44302018-11-30 10:45:12 +01001500 RTC_CHECK_EQ(2, decoder->Channels());
1501 return decoder;
1502 });
minyue5bd33972016-05-02 04:46:11 -07001503 }
1504
Niels Möllera0f44302018-11-30 10:45:12 +01001505 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001506 AudioFrame output_;
1507 const uint32_t kPayloadType = 17;
1508 const uint32_t kSamplingFreq_ = 48000;
1509 uint16_t sequence_number_ = 1;
1510};
1511
minyue5bd33972016-05-02 04:46:11 -07001512TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001513 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001514 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001515
1516 InsertPacket(first_timestamp());
1517 GetFirstPacket();
1518
henrik.lundin7a926812016-05-12 13:51:28 -07001519 bool muted;
1520 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001521 EXPECT_EQ(kCodecInternalCng, neteq_->last_operation_for_test());
1522}
1523
1524TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001525 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001526 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001527
1528 InsertPacket(first_timestamp());
1529 GetFirstPacket();
1530
1531 EXPECT_EQ(kNormal, neteq_->last_operation_for_test());
1532}
1533
1534TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001535 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001536 CreateInstanceWithDelayManagerMock();
1537
Ivo Creusen53a31f72019-10-24 15:20:39 +02001538 EXPECT_CALL(*mock_neteq_controller_, CngOff()).WillRepeatedly(Return(true));
minyue5bd33972016-05-02 04:46:11 -07001539 InsertPacket(first_timestamp());
1540
1541 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001542 bool muted;
Ivo Creusen53a31f72019-10-24 15:20:39 +02001543 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1544 .WillOnce(Return(kExpand));
henrik.lundin7a926812016-05-12 13:51:28 -07001545 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001546
1547 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1548
Ivo Creusen53a31f72019-10-24 15:20:39 +02001549 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
1550 .WillOnce(Return(kMerge));
minyue5bd33972016-05-02 04:46:11 -07001551
henrik.lundin7a926812016-05-12 13:51:28 -07001552 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001553 EXPECT_EQ(kMerge, neteq_->last_operation_for_test());
1554}
1555
1556TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001557 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001558 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001559
1560 InsertPacket(first_timestamp());
1561 GetFirstPacket();
1562
henrik.lundin7a926812016-05-12 13:51:28 -07001563 bool muted;
1564 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001565 EXPECT_EQ(kExpand, neteq_->last_operation_for_test());
1566}
1567
1568TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001569 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001570 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001571
1572 InsertPacket(first_timestamp());
1573 GetFirstPacket();
1574 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1575
Ivo Creusen53a31f72019-10-24 15:20:39 +02001576 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001577 .Times(1)
Ivo Creusen53a31f72019-10-24 15:20:39 +02001578 .WillOnce(Return(kFastAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001579
henrik.lundin7a926812016-05-12 13:51:28 -07001580 bool muted;
1581 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001582 EXPECT_EQ(kFastAccelerate, neteq_->last_operation_for_test());
1583}
1584
1585TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001586 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001587 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001588
1589 InsertPacket(first_timestamp());
1590 GetFirstPacket();
1591
1592 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1593
Ivo Creusen53a31f72019-10-24 15:20:39 +02001594 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001595 .Times(1)
Ivo Creusen53a31f72019-10-24 15:20:39 +02001596 .WillOnce(Return(kPreemptiveExpand));
minyue5bd33972016-05-02 04:46:11 -07001597
henrik.lundin7a926812016-05-12 13:51:28 -07001598 bool muted;
1599 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001600 EXPECT_EQ(kPreemptiveExpand, neteq_->last_operation_for_test());
1601}
1602
1603TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001604 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001605 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001606
1607 InsertPacket(first_timestamp());
1608 GetFirstPacket();
1609
1610 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1611
Ivo Creusen53a31f72019-10-24 15:20:39 +02001612 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001613 .Times(1)
Ivo Creusen53a31f72019-10-24 15:20:39 +02001614 .WillOnce(Return(kAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001615
henrik.lundin7a926812016-05-12 13:51:28 -07001616 bool muted;
1617 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001618 EXPECT_EQ(kAccelerate, neteq_->last_operation_for_test());
1619}
1620
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001621} // namespace webrtc