blob: 859980750f244a96d2814d7229792e0ad2fdc47a [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"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010018#include "api/neteq/default_neteq_controller_factory.h"
19#include "api/neteq/neteq.h"
20#include "api/neteq/neteq_controller.h"
21#include "api/test/neteq_factory_with_codecs.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_coding/neteq/accelerate.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020023#include "modules/audio_coding/neteq/decision_logic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_coding/neteq/expand.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010025#include "modules/audio_coding/neteq/histogram.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/neteq/mock/mock_dtmf_buffer.h"
28#include "modules/audio_coding/neteq/mock/mock_dtmf_tone_generator.h"
Ivo Creusen53a31f72019-10-24 15:20:39 +020029#include "modules/audio_coding/neteq/mock/mock_neteq_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_coding/neteq/mock/mock_packet_buffer.h"
31#include "modules/audio_coding/neteq/mock/mock_red_payload_splitter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_coding/neteq/preemptive_expand.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010033#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "modules/audio_coding/neteq/sync_buffer.h"
35#include "modules/audio_coding/neteq/timestamp_scaler.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010036#include "rtc_base/numerics/safe_conversions.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000037#include "system_wrappers/include/clock.h"
Niels Möllerb7180c02018-12-06 13:07:11 +010038#include "test/audio_decoder_proxy_factory.h"
Niels Möllera0f44302018-11-30 10:45:12 +010039#include "test/function_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "test/gmock.h"
41#include "test/gtest.h"
42#include "test/mock_audio_decoder.h"
43#include "test/mock_audio_decoder_factory.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045using ::testing::_;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010046using ::testing::AtLeast;
47using ::testing::DoAll;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000048using ::testing::ElementsAre;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049using ::testing::InSequence;
50using ::testing::Invoke;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000051using ::testing::IsEmpty;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +000052using ::testing::IsNull;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010053using ::testing::Pointee;
54using ::testing::Return;
55using ::testing::ReturnNull;
56using ::testing::SetArgPointee;
57using ::testing::SetArrayArgument;
Alessio Bazzica8f319a32019-07-24 16:47:02 +000058using ::testing::SizeIs;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010059using ::testing::WithArg;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
61namespace webrtc {
62
63// This function is called when inserting a packet list into the mock packet
64// buffer. The purpose is to delete all inserted packets properly, to avoid
65// memory leaks in the test.
66int DeletePacketsAndReturnOk(PacketList* packet_list) {
ossua73f6c92016-10-24 08:25:28 -070067 packet_list->clear();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 return PacketBuffer::kOK;
69}
70
71class NetEqImplTest : public ::testing::Test {
72 protected:
Alessio Bazzica8f319a32019-07-24 16:47:02 +000073 NetEqImplTest() : clock_(0) { config_.sample_rate_hz = 8000; }
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000074
Niels Möllera0f44302018-11-30 10:45:12 +010075 void CreateInstance(
76 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
77 ASSERT_TRUE(decoder_factory);
Ivo Creusen3ce44a32019-10-31 14:38:11 +010078 NetEqImpl::Dependencies deps(config_, &clock_, decoder_factory,
79 DefaultNetEqControllerFactory());
henrik.lundin1d9061e2016-04-26 12:19:34 -070080
81 // Get a local pointer to NetEq's TickTimer object.
82 tick_timer_ = deps.tick_timer.get();
83
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000084 if (use_mock_decoder_database_) {
henrik.lundin1d9061e2016-04-26 12:19:34 -070085 std::unique_ptr<MockDecoderDatabase> mock(new MockDecoderDatabase);
86 mock_decoder_database_ = mock.get();
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000087 EXPECT_CALL(*mock_decoder_database_, GetActiveCngDecoder())
88 .WillOnce(ReturnNull());
henrik.lundin1d9061e2016-04-26 12:19:34 -070089 deps.decoder_database = std::move(mock);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +000090 }
henrik.lundin1d9061e2016-04-26 12:19:34 -070091 decoder_database_ = deps.decoder_database.get();
henrik.lundin@webrtc.orgd9faa462014-01-14 10:18:45 +000092
henrik.lundin1d9061e2016-04-26 12:19:34 -070093 if (use_mock_dtmf_buffer_) {
94 std::unique_ptr<MockDtmfBuffer> mock(
95 new MockDtmfBuffer(config_.sample_rate_hz));
96 mock_dtmf_buffer_ = mock.get();
97 deps.dtmf_buffer = std::move(mock);
98 }
99 dtmf_buffer_ = deps.dtmf_buffer.get();
100
101 if (use_mock_dtmf_tone_generator_) {
102 std::unique_ptr<MockDtmfToneGenerator> mock(new MockDtmfToneGenerator);
103 mock_dtmf_tone_generator_ = mock.get();
104 deps.dtmf_tone_generator = std::move(mock);
105 }
106 dtmf_tone_generator_ = deps.dtmf_tone_generator.get();
107
108 if (use_mock_packet_buffer_) {
109 std::unique_ptr<MockPacketBuffer> mock(
110 new MockPacketBuffer(config_.max_packets_in_buffer, tick_timer_));
111 mock_packet_buffer_ = mock.get();
112 deps.packet_buffer = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700113 }
114 packet_buffer_ = deps.packet_buffer.get();
115
Ivo Creusen53a31f72019-10-24 15:20:39 +0200116 if (use_mock_neteq_controller_) {
117 std::unique_ptr<MockNetEqController> mock(new MockNetEqController());
118 mock_neteq_controller_ = mock.get();
119 deps.neteq_controller = std::move(mock);
120 } else {
121 deps.stats = std::make_unique<StatisticsCalculator>();
122 NetEqController::Config controller_config;
123 controller_config.tick_timer = tick_timer_;
124 controller_config.base_min_delay_ms = config_.min_delay_ms;
125 controller_config.enable_rtx_handling = config_.enable_rtx_handling;
126 controller_config.allow_time_stretching = true;
127 controller_config.max_packets_in_buffer = config_.max_packets_in_buffer;
128 deps.neteq_controller =
129 std::make_unique<DecisionLogic>(std::move(controller_config));
130 }
131 neteq_controller_ = deps.neteq_controller.get();
132
henrik.lundin1d9061e2016-04-26 12:19:34 -0700133 if (use_mock_payload_splitter_) {
ossua70695a2016-09-22 02:06:28 -0700134 std::unique_ptr<MockRedPayloadSplitter> mock(new MockRedPayloadSplitter);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700135 mock_payload_splitter_ = mock.get();
ossua70695a2016-09-22 02:06:28 -0700136 deps.red_payload_splitter = std::move(mock);
henrik.lundin1d9061e2016-04-26 12:19:34 -0700137 }
ossua70695a2016-09-22 02:06:28 -0700138 red_payload_splitter_ = deps.red_payload_splitter.get();
henrik.lundin1d9061e2016-04-26 12:19:34 -0700139
140 deps.timestamp_scaler = std::unique_ptr<TimestampScaler>(
141 new TimestampScaler(*deps.decoder_database.get()));
142
143 neteq_.reset(new NetEqImpl(config_, std::move(deps)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000144 ASSERT_TRUE(neteq_ != NULL);
145 }
146
Niels Möllera0f44302018-11-30 10:45:12 +0100147 void CreateInstance() { CreateInstance(CreateBuiltinAudioDecoderFactory()); }
148
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000149 void UseNoMocks() {
150 ASSERT_TRUE(neteq_ == NULL) << "Must call UseNoMocks before CreateInstance";
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000151 use_mock_decoder_database_ = false;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200152 use_mock_neteq_controller_ = false;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000153 use_mock_dtmf_buffer_ = false;
154 use_mock_dtmf_tone_generator_ = false;
155 use_mock_packet_buffer_ = false;
156 use_mock_payload_splitter_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000157 }
158
159 virtual ~NetEqImplTest() {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000160 if (use_mock_decoder_database_) {
161 EXPECT_CALL(*mock_decoder_database_, Die()).Times(1);
162 }
Ivo Creusen53a31f72019-10-24 15:20:39 +0200163 if (use_mock_neteq_controller_) {
164 EXPECT_CALL(*mock_neteq_controller_, Die()).Times(1);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000165 }
166 if (use_mock_dtmf_buffer_) {
167 EXPECT_CALL(*mock_dtmf_buffer_, Die()).Times(1);
168 }
169 if (use_mock_dtmf_tone_generator_) {
170 EXPECT_CALL(*mock_dtmf_tone_generator_, Die()).Times(1);
171 }
172 if (use_mock_packet_buffer_) {
173 EXPECT_CALL(*mock_packet_buffer_, Die()).Times(1);
174 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000175 }
176
Niels Möller05543682019-01-10 16:55:06 +0100177 void TestDtmfPacket(int sample_rate_hz) {
solenberg2779bab2016-11-17 04:45:19 -0800178 const size_t kPayloadLength = 4;
179 const uint8_t kPayloadType = 110;
solenberg2779bab2016-11-17 04:45:19 -0800180 const int kSampleRateHz = 16000;
181 config_.sample_rate_hz = kSampleRateHz;
182 UseNoMocks();
183 CreateInstance();
184 // Event: 2, E bit, Volume: 17, Length: 4336.
Jonas Olssona4d87372019-07-05 19:08:33 +0200185 uint8_t payload[kPayloadLength] = {0x02, 0x80 + 0x11, 0x10, 0xF0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700186 RTPHeader rtp_header;
187 rtp_header.payloadType = kPayloadType;
188 rtp_header.sequenceNumber = 0x1234;
189 rtp_header.timestamp = 0x12345678;
190 rtp_header.ssrc = 0x87654321;
solenberg2779bab2016-11-17 04:45:19 -0800191
Niels Möller05543682019-01-10 16:55:06 +0100192 EXPECT_TRUE(neteq_->RegisterPayloadType(
193 kPayloadType, SdpAudioFormat("telephone-event", sample_rate_hz, 1)));
solenberg2779bab2016-11-17 04:45:19 -0800194
195 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200196 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
solenberg2779bab2016-11-17 04:45:19 -0800197
198 // Pull audio once.
199 const size_t kMaxOutputSize =
200 static_cast<size_t>(10 * kSampleRateHz / 1000);
201 AudioFrame output;
202 bool muted;
203 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
204 ASSERT_FALSE(muted);
205 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
206 EXPECT_EQ(1u, output.num_channels_);
207 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
208
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000209 // DTMF packets are immediately consumed by |InsertPacket()| and won't be
210 // returned by |GetAudio()|.
211 EXPECT_THAT(output.packet_infos_, IsEmpty());
212
solenberg2779bab2016-11-17 04:45:19 -0800213 // Verify first 64 samples of actual output.
Jonas Olssona4d87372019-07-05 19:08:33 +0200214 const std::vector<int16_t> kOutput(
215 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216 -1578, -2816, -3460, -3403, -2709, -1594, -363, 671, 1269, 1328,
217 908, 202, -513, -964, -955, -431, 504, 1617, 2602, 3164,
218 3101, 2364, 1073, -511, -2047, -3198, -3721, -3525, -2688, -1440,
219 -99, 1015, 1663, 1744, 1319, 588, -171, -680, -747, -315,
220 515, 1512, 2378, 2828, 2674, 1877, 568, -986, -2446, -3482,
221 -3864, -3516, -2534, -1163});
solenberg2779bab2016-11-17 04:45:19 -0800222 ASSERT_GE(kMaxOutputSize, kOutput.size());
yujo36b1a5f2017-06-12 12:45:32 -0700223 EXPECT_TRUE(std::equal(kOutput.begin(), kOutput.end(), output.data()));
solenberg2779bab2016-11-17 04:45:19 -0800224 }
225
henrik.lundin1d9061e2016-04-26 12:19:34 -0700226 std::unique_ptr<NetEqImpl> neteq_;
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000227 NetEq::Config config_;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000228 SimulatedClock clock_;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700229 TickTimer* tick_timer_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700230 MockDecoderDatabase* mock_decoder_database_ = nullptr;
231 DecoderDatabase* decoder_database_ = nullptr;
232 bool use_mock_decoder_database_ = true;
Ivo Creusen53a31f72019-10-24 15:20:39 +0200233 MockNetEqController* mock_neteq_controller_ = nullptr;
234 NetEqController* neteq_controller_ = nullptr;
235 bool use_mock_neteq_controller_ = true;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700236 MockDtmfBuffer* mock_dtmf_buffer_ = nullptr;
237 DtmfBuffer* dtmf_buffer_ = nullptr;
238 bool use_mock_dtmf_buffer_ = true;
239 MockDtmfToneGenerator* mock_dtmf_tone_generator_ = nullptr;
240 DtmfToneGenerator* dtmf_tone_generator_ = nullptr;
241 bool use_mock_dtmf_tone_generator_ = true;
242 MockPacketBuffer* mock_packet_buffer_ = nullptr;
243 PacketBuffer* packet_buffer_ = nullptr;
244 bool use_mock_packet_buffer_ = true;
ossua70695a2016-09-22 02:06:28 -0700245 MockRedPayloadSplitter* mock_payload_splitter_ = nullptr;
246 RedPayloadSplitter* red_payload_splitter_ = nullptr;
henrik.lundin1d9061e2016-04-26 12:19:34 -0700247 bool use_mock_payload_splitter_ = true;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000248};
249
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000250// This tests the interface class NetEq.
251// TODO(hlundin): Move to separate file?
252TEST(NetEq, CreateAndDestroy) {
henrik.lundin@webrtc.org35ead382014-04-14 18:49:17 +0000253 NetEq::Config config;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000254 SimulatedClock clock(0);
Ivo Creusen3ce44a32019-10-31 14:38:11 +0100255 std::unique_ptr<NetEqFactory> neteq_factory = CreateNetEqFactoryWithCodecs();
256 std::unique_ptr<NetEq> neteq = neteq_factory->CreateNetEq(config, &clock);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000257}
258
kwiberg5adaf732016-10-04 09:33:27 -0700259TEST_F(NetEqImplTest, RegisterPayloadType) {
260 CreateInstance();
261 constexpr int rtp_payload_type = 0;
262 const SdpAudioFormat format("pcmu", 8000, 1);
263 EXPECT_CALL(*mock_decoder_database_,
264 RegisterPayload(rtp_payload_type, format));
265 neteq_->RegisterPayloadType(rtp_payload_type, format);
266}
267
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000268TEST_F(NetEqImplTest, RemovePayloadType) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000269 CreateInstance();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000270 uint8_t rtp_payload_type = 0;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000271 EXPECT_CALL(*mock_decoder_database_, Remove(rtp_payload_type))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 .WillOnce(Return(DecoderDatabase::kDecoderNotFound));
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200273 // Check that kOK is returned when database returns kDecoderNotFound, because
274 // removing a payload type that was never registered is not an error.
275 EXPECT_EQ(NetEq::kOK, neteq_->RemovePayloadType(rtp_payload_type));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000276}
277
kwiberg6b19b562016-09-20 04:02:25 -0700278TEST_F(NetEqImplTest, RemoveAllPayloadTypes) {
279 CreateInstance();
280 EXPECT_CALL(*mock_decoder_database_, RemoveAll()).WillOnce(Return());
281 neteq_->RemoveAllPayloadTypes();
282}
283
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284TEST_F(NetEqImplTest, InsertPacket) {
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000285 CreateInstance();
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000286 const size_t kPayloadLength = 100;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000287 const uint8_t kPayloadType = 0;
288 const uint16_t kFirstSequenceNumber = 0x1234;
289 const uint32_t kFirstTimestamp = 0x12345678;
290 const uint32_t kSsrc = 0x87654321;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000291 uint8_t payload[kPayloadLength] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700292 RTPHeader rtp_header;
293 rtp_header.payloadType = kPayloadType;
294 rtp_header.sequenceNumber = kFirstSequenceNumber;
295 rtp_header.timestamp = kFirstTimestamp;
296 rtp_header.ssrc = kSsrc;
ossu7a377612016-10-18 04:06:13 -0700297 Packet fake_packet;
298 fake_packet.payload_type = kPayloadType;
299 fake_packet.sequence_number = kFirstSequenceNumber;
300 fake_packet.timestamp = kFirstTimestamp;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000301
kwibergc0f2dcf2016-05-31 06:28:03 -0700302 rtc::scoped_refptr<MockAudioDecoderFactory> mock_decoder_factory(
303 new rtc::RefCountedObject<MockAudioDecoderFactory>);
Karl Wibergd6fbf2a2018-02-27 13:37:31 +0100304 EXPECT_CALL(*mock_decoder_factory, MakeAudioDecoderMock(_, _, _))
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800305 .WillOnce(Invoke([&](const SdpAudioFormat& format,
Danil Chapovalovb6021232018-06-19 13:26:36 +0200306 absl::optional<AudioCodecPairId> codec_pair_id,
ehmaldonadob55bd5f2017-02-02 11:51:21 -0800307 std::unique_ptr<AudioDecoder>* dec) {
kwibergc0f2dcf2016-05-31 06:28:03 -0700308 EXPECT_EQ("pcmu", format.name);
309
310 std::unique_ptr<MockAudioDecoder> mock_decoder(new MockAudioDecoder);
311 EXPECT_CALL(*mock_decoder, Channels()).WillRepeatedly(Return(1));
312 EXPECT_CALL(*mock_decoder, SampleRateHz()).WillRepeatedly(Return(8000));
kwibergc0f2dcf2016-05-31 06:28:03 -0700313 EXPECT_CALL(*mock_decoder, Die()).Times(1); // Called when deleted.
314
315 *dec = std::move(mock_decoder);
316 }));
Niels Möller72899062019-01-11 09:36:13 +0100317 DecoderDatabase::DecoderInfo info(SdpAudioFormat("pcmu", 8000, 1),
318 absl::nullopt, mock_decoder_factory);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319
320 // Expectations for decoder database.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000321 EXPECT_CALL(*mock_decoder_database_, GetDecoderInfo(kPayloadType))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 .WillRepeatedly(Return(&info));
323
324 // Expectations for packet buffer.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000325 EXPECT_CALL(*mock_packet_buffer_, Empty())
326 .WillOnce(Return(false)); // Called once after first packet is inserted.
Jonas Olssona4d87372019-07-05 19:08:33 +0200327 EXPECT_CALL(*mock_packet_buffer_, Flush()).Times(1);
minyue-webrtc12d30842017-07-19 11:44:06 +0200328 EXPECT_CALL(*mock_packet_buffer_, InsertPacketList(_, _, _, _, _))
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329 .Times(2)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100330 .WillRepeatedly(DoAll(SetArgPointee<2>(kPayloadType),
331 WithArg<0>(Invoke(DeletePacketsAndReturnOk))));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332 // SetArgPointee<2>(kPayloadType) means that the third argument (zero-based
333 // index) is a pointer, and the variable pointed to is set to kPayloadType.
334 // Also invoke the function DeletePacketsAndReturnOk to properly delete all
335 // packets in the list (to avoid memory leaks in the test).
ossu7a377612016-10-18 04:06:13 -0700336 EXPECT_CALL(*mock_packet_buffer_, PeekNextPacket())
turaj@webrtc.orga6101d72013-10-01 22:01:09 +0000337 .Times(1)
ossu7a377612016-10-18 04:06:13 -0700338 .WillOnce(Return(&fake_packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339
340 // Expectations for DTMF buffer.
Jonas Olssona4d87372019-07-05 19:08:33 +0200341 EXPECT_CALL(*mock_dtmf_buffer_, Flush()).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342
343 // Expectations for delay manager.
344 {
345 // All expectations within this block must be called in this specific order.
346 InSequence sequence; // Dummy variable.
347 // Expectations when the first packet is inserted.
Ivo Creusen53a31f72019-10-24 15:20:39 +0200348 EXPECT_CALL(*mock_neteq_controller_,
349 PacketArrived(/*last_cng_or_dtmf*/ false,
350 /*packet_length_samples*/ _,
351 /*should_update_stats*/ _,
352 /*main_sequence_number*/ kFirstSequenceNumber,
353 /*main_timestamp*/ kFirstTimestamp,
354 /*fs_hz*/ 8000));
355 EXPECT_CALL(*mock_neteq_controller_,
356 PacketArrived(/*last_cng_or_dtmf*/ false,
357 /*packet_length_samples*/ _,
358 /*should_update_stats*/ _,
359 /*main_sequence_number*/ kFirstSequenceNumber + 1,
360 /*main_timestamp*/ kFirstTimestamp + 160,
361 /*fs_hz*/ 8000));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000362 }
363
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000364 // Insert first packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200365 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000366
367 // Insert second packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700368 rtp_header.timestamp += 160;
369 rtp_header.sequenceNumber += 1;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200370 neteq_->InsertPacket(rtp_header, payload);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000371}
372
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000373TEST_F(NetEqImplTest, InsertPacketsUntilBufferIsFull) {
374 UseNoMocks();
375 CreateInstance();
376
377 const int kPayloadLengthSamples = 80;
378 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
Jonas Olssona4d87372019-07-05 19:08:33 +0200379 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000380 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700381 RTPHeader rtp_header;
382 rtp_header.payloadType = kPayloadType;
383 rtp_header.sequenceNumber = 0x1234;
384 rtp_header.timestamp = 0x12345678;
385 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000386
Niels Möller05543682019-01-10 16:55:06 +0100387 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
388 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000389
390 // Insert packets. The buffer should not flush.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700391 for (size_t i = 1; i <= config_.max_packets_in_buffer; ++i) {
Karl Wiberg45eb1352019-10-10 14:23:00 +0200392 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700393 rtp_header.timestamp += kPayloadLengthSamples;
394 rtp_header.sequenceNumber += 1;
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000395 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
396 }
397
398 // Insert one more packet and make sure the buffer got flushed. That is, it
399 // should only hold one single packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200400 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700401 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700402 const Packet* test_packet = packet_buffer_->PeekNextPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700403 EXPECT_EQ(rtp_header.timestamp, test_packet->timestamp);
404 EXPECT_EQ(rtp_header.sequenceNumber, test_packet->sequence_number);
henrik.lundin@webrtc.org04ea2322014-03-12 05:55:10 +0000405}
406
solenberg2779bab2016-11-17 04:45:19 -0800407TEST_F(NetEqImplTest, TestDtmfPacketAVT) {
Niels Möller05543682019-01-10 16:55:06 +0100408 TestDtmfPacket(8000);
solenberg2779bab2016-11-17 04:45:19 -0800409}
solenberg99df6c02016-10-11 04:35:34 -0700410
solenberg2779bab2016-11-17 04:45:19 -0800411TEST_F(NetEqImplTest, TestDtmfPacketAVT16kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100412 TestDtmfPacket(16000);
solenberg2779bab2016-11-17 04:45:19 -0800413}
solenberg99df6c02016-10-11 04:35:34 -0700414
solenberg2779bab2016-11-17 04:45:19 -0800415TEST_F(NetEqImplTest, TestDtmfPacketAVT32kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100416 TestDtmfPacket(32000);
solenberg2779bab2016-11-17 04:45:19 -0800417}
solenberg99df6c02016-10-11 04:35:34 -0700418
solenberg2779bab2016-11-17 04:45:19 -0800419TEST_F(NetEqImplTest, TestDtmfPacketAVT48kHz) {
Niels Möller05543682019-01-10 16:55:06 +0100420 TestDtmfPacket(48000);
solenberg99df6c02016-10-11 04:35:34 -0700421}
422
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000423// This test verifies that timestamps propagate from the incoming packets
424// through to the sync buffer and to the playout timestamp.
425TEST_F(NetEqImplTest, VerifyTimestampPropagation) {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000426 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000427 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700428 const size_t kPayloadLengthSamples =
429 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000430 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
431 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700432 RTPHeader rtp_header;
433 rtp_header.payloadType = kPayloadType;
434 rtp_header.sequenceNumber = 0x1234;
435 rtp_header.timestamp = 0x12345678;
436 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000437 rtp_header.numCSRCs = 3;
438 rtp_header.arrOfCSRCs[0] = 43;
439 rtp_header.arrOfCSRCs[1] = 65;
440 rtp_header.arrOfCSRCs[2] = 17;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000441
442 // This is a dummy decoder that produces as many output samples as the input
443 // has bytes. The output is an increasing series, starting at 1 for the first
444 // sample, and then increasing by 1 for each sample.
445 class CountingSamplesDecoder : public AudioDecoder {
446 public:
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000447 CountingSamplesDecoder() : next_value_(1) {}
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000448
449 // Produce as many samples as input bytes (|encoded_len|).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100450 int DecodeInternal(const uint8_t* encoded,
451 size_t encoded_len,
452 int /* sample_rate_hz */,
453 int16_t* decoded,
454 SpeechType* speech_type) override {
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000455 for (size_t i = 0; i < encoded_len; ++i) {
456 decoded[i] = next_value_++;
457 }
458 *speech_type = kSpeech;
Mirko Bonadei737e0732017-10-19 09:00:17 +0200459 return rtc::checked_cast<int>(encoded_len);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000460 }
461
Karl Wiberg43766482015-08-27 15:22:11 +0200462 void Reset() override { next_value_ = 1; }
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000463
kwiberg347d3512016-06-16 01:59:09 -0700464 int SampleRateHz() const override { return kSampleRateHz; }
465
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000466 size_t Channels() const override { return 1; }
467
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000468 uint16_t next_value() const { return next_value_; }
469
470 private:
471 int16_t next_value_;
kwiberg@webrtc.org721ef632014-11-04 11:51:46 +0000472 } decoder_;
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000473
Niels Möllerb7180c02018-12-06 13:07:11 +0100474 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
475 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder_);
476
477 UseNoMocks();
478 CreateInstance(decoder_factory);
479
480 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
Niels Möllera1eb9c72018-12-07 15:24:42 +0100481 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000482
483 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000484 clock_.AdvanceTimeMilliseconds(123456);
485 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200486 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000487
488 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700489 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800490 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700491 bool muted;
492 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
493 ASSERT_FALSE(muted);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800494 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
495 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800496 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000497
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000498 // Verify |output.packet_infos_|.
499 ASSERT_THAT(output.packet_infos_, SizeIs(1));
500 {
501 const auto& packet_info = output.packet_infos_[0];
502 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
503 EXPECT_THAT(packet_info.csrcs(), ElementsAre(43, 65, 17));
504 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
505 EXPECT_FALSE(packet_info.audio_level().has_value());
506 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
507 }
508
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000509 // Start with a simple check that the fake decoder is behaving as expected.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700510 EXPECT_EQ(kPayloadLengthSamples,
511 static_cast<size_t>(decoder_.next_value() - 1));
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000512
513 // The value of the last of the output samples is the same as the number of
514 // samples played from the decoded packet. Thus, this number + the RTP
515 // timestamp should match the playout timestamp.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200516 // Wrap the expected value in an absl::optional to compare them as such.
henrik.lundin9a410dd2016-04-06 01:39:22 -0700517 EXPECT_EQ(
Danil Chapovalovb6021232018-06-19 13:26:36 +0200518 absl::optional<uint32_t>(rtp_header.timestamp +
519 output.data()[output.samples_per_channel_ - 1]),
henrik.lundin9a410dd2016-04-06 01:39:22 -0700520 neteq_->GetPlayoutTimestamp());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000521
522 // Check the timestamp for the last value in the sync buffer. This should
523 // be one full frame length ahead of the RTP timestamp.
524 const SyncBuffer* sync_buffer = neteq_->sync_buffer_for_test();
525 ASSERT_TRUE(sync_buffer != NULL);
henrik.lundin246ef3e2017-04-24 09:14:32 -0700526 EXPECT_EQ(rtp_header.timestamp + kPayloadLengthSamples,
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000527 sync_buffer->end_timestamp());
528
529 // Check that the number of samples still to play from the sync buffer add
530 // up with what was already played out.
henrik.lundin6d8e0112016-03-04 10:34:21 -0800531 EXPECT_EQ(
yujo36b1a5f2017-06-12 12:45:32 -0700532 kPayloadLengthSamples - output.data()[output.samples_per_channel_ - 1],
henrik.lundin6d8e0112016-03-04 10:34:21 -0800533 sync_buffer->FutureLength());
henrik.lundin@webrtc.orgb287d962014-04-07 21:21:45 +0000534}
535
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000536TEST_F(NetEqImplTest, ReorderedPacket) {
537 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100538 // Create a mock decoder object.
539 MockAudioDecoder mock_decoder;
540
541 CreateInstance(
542 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000543
544 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000545 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700546 const size_t kPayloadLengthSamples =
547 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000548 const size_t kPayloadLengthBytes = kPayloadLengthSamples;
549 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700550 RTPHeader rtp_header;
551 rtp_header.payloadType = kPayloadType;
552 rtp_header.sequenceNumber = 0x1234;
553 rtp_header.timestamp = 0x12345678;
554 rtp_header.ssrc = 0x87654321;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000555 rtp_header.extension.hasAudioLevel = true;
556 rtp_header.extension.audioLevel = 42;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000557
Karl Wiberg43766482015-08-27 15:22:11 +0200558 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700559 EXPECT_CALL(mock_decoder, SampleRateHz())
560 .WillRepeatedly(Return(kSampleRateHz));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000561 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin034154b2016-04-27 06:11:50 -0700562 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200563 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000564 int16_t dummy_output[kPayloadLengthSamples] = {0};
565 // The below expectation will make the mock decoder write
566 // |kPayloadLengthSamples| zeros to the output array, and mark it as speech.
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100567 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
568 kSampleRateHz, _, _))
569 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000570 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100571 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200572 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
Niels Möllera1eb9c72018-12-07 15:24:42 +0100573 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
574 SdpAudioFormat("L16", 8000, 1)));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000575
576 // Insert one packet.
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000577 clock_.AdvanceTimeMilliseconds(123456);
578 int64_t expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200579 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000580
581 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700582 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800583 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700584 bool muted;
585 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800586 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
587 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800588 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000589
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000590 // Verify |output.packet_infos_|.
591 ASSERT_THAT(output.packet_infos_, SizeIs(1));
592 {
593 const auto& packet_info = output.packet_infos_[0];
594 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
595 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
596 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
597 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
598 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
599 }
600
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000601 // Insert two more packets. The first one is out of order, and is already too
602 // old, the second one is the expected next packet.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700603 rtp_header.sequenceNumber -= 1;
604 rtp_header.timestamp -= kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000605 rtp_header.extension.audioLevel = 1;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000606 payload[0] = 1;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000607 clock_.AdvanceTimeMilliseconds(1000);
Karl Wiberg45eb1352019-10-10 14:23:00 +0200608 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -0700609 rtp_header.sequenceNumber += 2;
610 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000611 rtp_header.extension.audioLevel = 2;
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000612 payload[0] = 2;
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000613 clock_.AdvanceTimeMilliseconds(2000);
614 expected_receive_time_ms = clock_.TimeInMilliseconds();
Karl Wiberg45eb1352019-10-10 14:23:00 +0200615 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000616
617 // Expect only the second packet to be decoded (the one with "2" as the first
618 // payload byte).
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100619 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
620 kSampleRateHz, _, _))
621 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000622 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100623 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200624 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000625
626 // Pull audio once.
henrik.lundin7a926812016-05-12 13:51:28 -0700627 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800628 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
629 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800630 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000631
632 // Now check the packet buffer, and make sure it is empty, since the
633 // out-of-order packet should have been discarded.
634 EXPECT_TRUE(packet_buffer_->Empty());
635
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000636 // Verify |output.packet_infos_|. Expect to only see the second packet.
637 ASSERT_THAT(output.packet_infos_, SizeIs(1));
638 {
639 const auto& packet_info = output.packet_infos_[0];
640 EXPECT_EQ(packet_info.ssrc(), rtp_header.ssrc);
641 EXPECT_THAT(packet_info.csrcs(), IsEmpty());
642 EXPECT_EQ(packet_info.rtp_timestamp(), rtp_header.timestamp);
643 EXPECT_EQ(packet_info.audio_level(), rtp_header.extension.audioLevel);
644 EXPECT_EQ(packet_info.receive_time_ms(), expected_receive_time_ms);
645 }
646
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000647 EXPECT_CALL(mock_decoder, Die());
648}
649
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000650// This test verifies that NetEq can handle the situation where the first
651// incoming packet is rejected.
henrik.lundin@webrtc.org6ff3ac12014-11-20 14:14:49 +0000652TEST_F(NetEqImplTest, FirstPacketUnknown) {
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000653 UseNoMocks();
654 CreateInstance();
655
656 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000657 const int kSampleRateHz = 8000;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700658 const size_t kPayloadLengthSamples =
659 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
henrik.lundinc9ec8752016-10-13 02:43:34 -0700660 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000661 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700662 RTPHeader rtp_header;
663 rtp_header.payloadType = kPayloadType;
664 rtp_header.sequenceNumber = 0x1234;
665 rtp_header.timestamp = 0x12345678;
666 rtp_header.ssrc = 0x87654321;
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000667
668 // Insert one packet. Note that we have not registered any payload type, so
669 // this packet will be rejected.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200670 EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000671
672 // Pull audio once.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700673 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800674 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700675 bool muted;
676 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800677 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
678 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
679 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800680 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000681 EXPECT_THAT(output.packet_infos_, IsEmpty());
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000682
683 // Register the payload type.
Niels Möller05543682019-01-10 16:55:06 +0100684 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
685 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000686
687 // Insert 10 packets.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700688 for (size_t i = 0; i < 10; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -0700689 rtp_header.sequenceNumber++;
690 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200691 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000692 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
693 }
694
695 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700696 for (size_t i = 0; i < 3; ++i) {
henrik.lundin7a926812016-05-12 13:51:28 -0700697 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800698 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
699 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
700 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800701 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000702 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000703 EXPECT_THAT(output.packet_infos_, SizeIs(1));
henrik.lundin@webrtc.orged910682014-11-20 11:01:02 +0000704 }
705}
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000706
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200707// This test verifies that audio interruption is not logged for the initial
708// PLC period before the first packet is deocoded.
709// TODO(henrik.lundin) Maybe move this test to neteq_network_stats_unittest.cc.
710TEST_F(NetEqImplTest, NoAudioInterruptionLoggedBeforeFirstDecode) {
711 UseNoMocks();
712 CreateInstance();
713
714 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200715 const int kSampleRateHz = 8000;
716 const size_t kPayloadLengthSamples =
717 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
718 const size_t kPayloadLengthBytes = kPayloadLengthSamples * 2;
719 uint8_t payload[kPayloadLengthBytes] = {0};
720 RTPHeader rtp_header;
721 rtp_header.payloadType = kPayloadType;
722 rtp_header.sequenceNumber = 0x1234;
723 rtp_header.timestamp = 0x12345678;
724 rtp_header.ssrc = 0x87654321;
725
726 // Register the payload type.
727 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
728 SdpAudioFormat("l16", 8000, 1)));
729
730 // Pull audio several times. No packets have been inserted yet.
731 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
732 AudioFrame output;
733 bool muted;
734 for (int i = 0; i < 100; ++i) {
735 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
736 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
737 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
738 EXPECT_EQ(1u, output.num_channels_);
739 EXPECT_NE(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000740 EXPECT_THAT(output.packet_infos_, IsEmpty());
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200741 }
742
743 // Insert 10 packets.
744 for (size_t i = 0; i < 10; ++i) {
745 rtp_header.sequenceNumber++;
746 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200747 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200748 EXPECT_EQ(i + 1, packet_buffer_->NumPacketsInBuffer());
749 }
750
751 // Pull audio repeatedly and make sure we get normal output, that is not PLC.
752 for (size_t i = 0; i < 3; ++i) {
753 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
754 ASSERT_LE(output.samples_per_channel_, kMaxOutputSize);
755 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
756 EXPECT_EQ(1u, output.num_channels_);
757 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_)
758 << "NetEq did not decode the packets as expected.";
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000759 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200760 }
761
762 auto lifetime_stats = neteq_->GetLifetimeStatistics();
Henrik Lundin44125fa2019-04-29 17:00:46 +0200763 EXPECT_EQ(0, lifetime_stats.interruption_count);
Henrik Lundin2a8bd092019-04-26 09:47:07 +0200764}
765
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000766// This test verifies that NetEq can handle comfort noise and enters/quits codec
767// internal CNG mode properly.
768TEST_F(NetEqImplTest, CodecInternalCng) {
769 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +0100770 // Create a mock decoder object.
771 MockAudioDecoder mock_decoder;
772 CreateInstance(
773 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000774
775 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000776 const int kSampleRateKhz = 48;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700777 const size_t kPayloadLengthSamples =
778 static_cast<size_t>(20 * kSampleRateKhz); // 20 ms.
779 const size_t kPayloadLengthBytes = 10;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000780 uint8_t payload[kPayloadLengthBytes] = {0};
781 int16_t dummy_output[kPayloadLengthSamples] = {0};
782
henrik.lundin246ef3e2017-04-24 09:14:32 -0700783 RTPHeader rtp_header;
784 rtp_header.payloadType = kPayloadType;
785 rtp_header.sequenceNumber = 0x1234;
786 rtp_header.timestamp = 0x12345678;
787 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000788
Karl Wiberg43766482015-08-27 15:22:11 +0200789 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -0700790 EXPECT_CALL(mock_decoder, SampleRateHz())
791 .WillRepeatedly(Return(kSampleRateKhz * 1000));
henrik.lundin@webrtc.org6dba1eb2015-03-18 09:47:08 +0000792 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700793 EXPECT_CALL(mock_decoder, PacketDuration(_, kPayloadLengthBytes))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200794 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700795 // Packed duration when asking the decoder for more CNG data (without a new
796 // packet).
797 EXPECT_CALL(mock_decoder, PacketDuration(nullptr, 0))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200798 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000799
800 // Pointee(x) verifies that first byte of the payload equals x, this makes it
801 // possible to verify that the correct payload is fed to Decode().
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100802 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(0), kPayloadLengthBytes,
803 kSampleRateKhz * 1000, _, _))
804 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000805 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100806 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200807 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000808
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100809 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(1), kPayloadLengthBytes,
810 kSampleRateKhz * 1000, _, _))
811 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000812 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100813 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200814 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000815
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100816 EXPECT_CALL(mock_decoder,
817 DecodeInternal(IsNull(), 0, kSampleRateKhz * 1000, _, _))
818 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000819 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100820 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200821 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000822
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100823 EXPECT_CALL(mock_decoder, DecodeInternal(Pointee(2), kPayloadLengthBytes,
824 kSampleRateKhz * 1000, _, _))
825 .WillOnce(DoAll(SetArrayArgument<3>(dummy_output,
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000826 dummy_output + kPayloadLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +0100827 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200828 Return(rtc::checked_cast<int>(kPayloadLengthSamples))));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000829
Niels Möller50b66d52018-12-11 14:43:21 +0100830 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
831 SdpAudioFormat("opus", 48000, 2)));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000832
833 // Insert one packet (decoder will return speech).
Karl Wiberg45eb1352019-10-10 14:23:00 +0200834 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000835
836 // Insert second packet (decoder will return CNG).
837 payload[0] = 1;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700838 rtp_header.sequenceNumber++;
839 rtp_header.timestamp += kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200840 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000841
Peter Kastingdce40cf2015-08-24 14:52:23 -0700842 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateKhz);
henrik.lundin6d8e0112016-03-04 10:34:21 -0800843 AudioFrame output;
henrik.lundin55480f52016-03-08 02:37:57 -0800844 AudioFrame::SpeechType expected_type[8] = {
Jonas Olssona4d87372019-07-05 19:08:33 +0200845 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech, AudioFrame::kCNG,
846 AudioFrame::kCNG, AudioFrame::kCNG, AudioFrame::kCNG,
847 AudioFrame::kNormalSpeech, AudioFrame::kNormalSpeech};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000848 int expected_timestamp_increment[8] = {
849 -1, // will not be used.
850 10 * kSampleRateKhz,
Jonas Olssona4d87372019-07-05 19:08:33 +0200851 -1,
852 -1, // timestamp will be empty during CNG mode; indicated by -1 here.
853 -1,
854 -1,
855 50 * kSampleRateKhz,
856 10 * kSampleRateKhz};
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000857
henrik.lundin7a926812016-05-12 13:51:28 -0700858 bool muted;
859 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
Danil Chapovalovb6021232018-06-19 13:26:36 +0200860 absl::optional<uint32_t> last_timestamp = neteq_->GetPlayoutTimestamp();
henrik.lundin9a410dd2016-04-06 01:39:22 -0700861 ASSERT_TRUE(last_timestamp);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000862
henrik.lundin0d96ab72016-04-06 12:28:26 -0700863 // Lambda for verifying the timestamps.
864 auto verify_timestamp = [&last_timestamp, &expected_timestamp_increment](
Danil Chapovalovb6021232018-06-19 13:26:36 +0200865 absl::optional<uint32_t> ts, size_t i) {
henrik.lundin0d96ab72016-04-06 12:28:26 -0700866 if (expected_timestamp_increment[i] == -1) {
867 // Expect to get an empty timestamp value during CNG and PLC.
868 EXPECT_FALSE(ts) << "i = " << i;
869 } else {
870 ASSERT_TRUE(ts) << "i = " << i;
871 EXPECT_EQ(*ts, *last_timestamp + expected_timestamp_increment[i])
872 << "i = " << i;
873 last_timestamp = ts;
874 }
875 };
876
Peter Kastingdce40cf2015-08-24 14:52:23 -0700877 for (size_t i = 1; i < 6; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800878 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
879 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800880 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700881 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700882 SCOPED_TRACE("");
883 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000884 }
885
886 // Insert third packet, which leaves a gap from last packet.
887 payload[0] = 2;
henrik.lundin246ef3e2017-04-24 09:14:32 -0700888 rtp_header.sequenceNumber += 2;
889 rtp_header.timestamp += 2 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200890 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000891
Peter Kastingdce40cf2015-08-24 14:52:23 -0700892 for (size_t i = 6; i < 8; ++i) {
henrik.lundin6d8e0112016-03-04 10:34:21 -0800893 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
894 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -0800895 EXPECT_EQ(expected_type[i - 1], output.speech_type_);
henrik.lundin7a926812016-05-12 13:51:28 -0700896 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin0d96ab72016-04-06 12:28:26 -0700897 SCOPED_TRACE("");
898 verify_timestamp(neteq_->GetPlayoutTimestamp(), i);
minyue@webrtc.org1784d7c2014-12-09 10:46:39 +0000899 }
900
901 // Now check the packet buffer, and make sure it is empty.
902 EXPECT_TRUE(packet_buffer_->Empty());
903
904 EXPECT_CALL(mock_decoder, Die());
905}
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000906
907TEST_F(NetEqImplTest, UnsupportedDecoder) {
908 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +0100909 ::testing::NiceMock<MockAudioDecoder> decoder;
910
911 CreateInstance(
912 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&decoder));
minyue5bd33972016-05-02 04:46:11 -0700913 static const size_t kNetEqMaxFrameSize = 5760; // 120 ms @ 48 kHz.
Peter Kasting69558702016-01-12 16:26:35 -0800914 static const size_t kChannels = 2;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000915
916 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000917 const int kSampleRateHz = 8000;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000918
Peter Kastingdce40cf2015-08-24 14:52:23 -0700919 const size_t kPayloadLengthSamples =
920 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000921 const size_t kPayloadLengthBytes = 1;
minyue5bd33972016-05-02 04:46:11 -0700922 uint8_t payload[kPayloadLengthBytes] = {0};
Minyue323b1322015-05-25 13:49:37 +0200923 int16_t dummy_output[kPayloadLengthSamples * kChannels] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -0700924 RTPHeader rtp_header;
925 rtp_header.payloadType = kPayloadType;
926 rtp_header.sequenceNumber = 0x1234;
927 rtp_header.timestamp = 0x12345678;
928 rtp_header.ssrc = 0x87654321;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000929
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000930 const uint8_t kFirstPayloadValue = 1;
931 const uint8_t kSecondPayloadValue = 2;
932
ossu61a208b2016-09-20 01:38:00 -0700933 EXPECT_CALL(decoder,
934 PacketDuration(Pointee(kFirstPayloadValue), kPayloadLengthBytes))
935 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200936 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize + 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000937
ossu61a208b2016-09-20 01:38:00 -0700938 EXPECT_CALL(decoder, DecodeInternal(Pointee(kFirstPayloadValue), _, _, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000939 .Times(0);
940
ossu61a208b2016-09-20 01:38:00 -0700941 EXPECT_CALL(decoder, DecodeInternal(Pointee(kSecondPayloadValue),
942 kPayloadLengthBytes, kSampleRateHz, _, _))
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000943 .Times(1)
ossu61a208b2016-09-20 01:38:00 -0700944 .WillOnce(DoAll(
945 SetArrayArgument<3>(dummy_output,
946 dummy_output + kPayloadLengthSamples * kChannels),
947 SetArgPointee<4>(AudioDecoder::kSpeech),
948 Return(static_cast<int>(kPayloadLengthSamples * kChannels))));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000949
ossu61a208b2016-09-20 01:38:00 -0700950 EXPECT_CALL(decoder,
951 PacketDuration(Pointee(kSecondPayloadValue), kPayloadLengthBytes))
952 .Times(AtLeast(1))
Mirko Bonadeib7e17882017-10-20 11:18:47 +0200953 .WillRepeatedly(Return(rtc::checked_cast<int>(kNetEqMaxFrameSize)));
ossu61a208b2016-09-20 01:38:00 -0700954
Jonas Olssona4d87372019-07-05 19:08:33 +0200955 EXPECT_CALL(decoder, SampleRateHz()).WillRepeatedly(Return(kSampleRateHz));
ossu61a208b2016-09-20 01:38:00 -0700956
Jonas Olssona4d87372019-07-05 19:08:33 +0200957 EXPECT_CALL(decoder, Channels()).WillRepeatedly(Return(kChannels));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000958
Niels Möllera1eb9c72018-12-07 15:24:42 +0100959 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
960 SdpAudioFormat("L16", 8000, 1)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000961
962 // Insert one packet.
963 payload[0] = kFirstPayloadValue; // This will make Decode() fail.
Karl Wiberg45eb1352019-10-10 14:23:00 +0200964 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000965
966 // Insert another packet.
967 payload[0] = kSecondPayloadValue; // This will make Decode() successful.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700968 rtp_header.sequenceNumber++;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000969 // The second timestamp needs to be at least 30 ms after the first to make
970 // the second packet get decoded.
henrik.lundin246ef3e2017-04-24 09:14:32 -0700971 rtp_header.timestamp += 3 * kPayloadLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +0200972 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000973
henrik.lundin6d8e0112016-03-04 10:34:21 -0800974 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -0700975 bool muted;
henrik.lundin6d8e0112016-03-04 10:34:21 -0800976 // First call to GetAudio will try to decode the "faulty" packet.
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200977 // Expect kFail return value.
henrik.lundin7a926812016-05-12 13:51:28 -0700978 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800979 // Output size and number of channels should be correct.
980 const size_t kExpectedOutputSize = 10 * (kSampleRateHz / 1000) * kChannels;
981 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
982 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000983 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000984
henrik.lundin6d8e0112016-03-04 10:34:21 -0800985 // Second call to GetAudio will decode the packet that is ok. No errors are
986 // expected.
henrik.lundin7a926812016-05-12 13:51:28 -0700987 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800988 EXPECT_EQ(kExpectedOutputSize, output.samples_per_channel_ * kChannels);
989 EXPECT_EQ(kChannels, output.num_channels_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000990 EXPECT_THAT(output.packet_infos_, SizeIs(1));
ossu61a208b2016-09-20 01:38:00 -0700991
992 // Die isn't called through NiceMock (since it's called by the
993 // MockAudioDecoder constructor), so it needs to be mocked explicitly.
994 EXPECT_CALL(decoder, Die());
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000995}
996
henrik.lundin116c84e2015-08-27 13:14:48 -0700997// This test inserts packets until the buffer is flushed. After that, it asks
998// NetEq for the network statistics. The purpose of the test is to make sure
999// that even though the buffer size increment is negative (which it becomes when
1000// the packet causing a flush is inserted), the packet length stored in the
1001// decision logic remains valid.
1002TEST_F(NetEqImplTest, FloodBufferAndGetNetworkStats) {
1003 UseNoMocks();
1004 CreateInstance();
1005
1006 const size_t kPayloadLengthSamples = 80;
1007 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1008 const uint8_t kPayloadType = 17; // Just an arbitrary number.
henrik.lundin116c84e2015-08-27 13:14:48 -07001009 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001010 RTPHeader rtp_header;
1011 rtp_header.payloadType = kPayloadType;
1012 rtp_header.sequenceNumber = 0x1234;
1013 rtp_header.timestamp = 0x12345678;
1014 rtp_header.ssrc = 0x87654321;
henrik.lundin116c84e2015-08-27 13:14:48 -07001015
Niels Möller05543682019-01-10 16:55:06 +01001016 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1017 SdpAudioFormat("l16", 8000, 1)));
henrik.lundin116c84e2015-08-27 13:14:48 -07001018
1019 // Insert packets until the buffer flushes.
1020 for (size_t i = 0; i <= config_.max_packets_in_buffer; ++i) {
1021 EXPECT_EQ(i, packet_buffer_->NumPacketsInBuffer());
Karl Wiberg45eb1352019-10-10 14:23:00 +02001022 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
henrik.lundin246ef3e2017-04-24 09:14:32 -07001023 rtp_header.timestamp += rtc::checked_cast<uint32_t>(kPayloadLengthSamples);
1024 ++rtp_header.sequenceNumber;
henrik.lundin116c84e2015-08-27 13:14:48 -07001025 }
1026 EXPECT_EQ(1u, packet_buffer_->NumPacketsInBuffer());
1027
1028 // Ask for network statistics. This should not crash.
1029 NetEqNetworkStatistics stats;
1030 EXPECT_EQ(NetEq::kOK, neteq_->NetworkStatistics(&stats));
1031}
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001032
1033TEST_F(NetEqImplTest, DecodedPayloadTooShort) {
1034 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001035 // Create a mock decoder object.
1036 MockAudioDecoder mock_decoder;
1037
1038 CreateInstance(
1039 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001040
1041 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001042 const int kSampleRateHz = 8000;
1043 const size_t kPayloadLengthSamples =
1044 static_cast<size_t>(10 * kSampleRateHz / 1000); // 10 ms.
1045 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples;
1046 uint8_t payload[kPayloadLengthBytes] = {0};
henrik.lundin246ef3e2017-04-24 09:14:32 -07001047 RTPHeader rtp_header;
1048 rtp_header.payloadType = kPayloadType;
1049 rtp_header.sequenceNumber = 0x1234;
1050 rtp_header.timestamp = 0x12345678;
1051 rtp_header.ssrc = 0x87654321;
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001052
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001053 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001054 EXPECT_CALL(mock_decoder, SampleRateHz())
1055 .WillRepeatedly(Return(kSampleRateHz));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001056 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001057 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001058 .WillRepeatedly(Return(rtc::checked_cast<int>(kPayloadLengthSamples)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001059 int16_t dummy_output[kPayloadLengthSamples] = {0};
1060 // The below expectation will make the mock decoder write
1061 // |kPayloadLengthSamples| - 5 zeros to the output array, and mark it as
1062 // speech. That is, the decoded length is 5 samples shorter than the expected.
1063 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001064 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001065 .WillOnce(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001066 DoAll(SetArrayArgument<3>(dummy_output,
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001067 dummy_output + kPayloadLengthSamples - 5),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001068 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001069 Return(rtc::checked_cast<int>(kPayloadLengthSamples - 5))));
Niels Möllera1eb9c72018-12-07 15:24:42 +01001070 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1071 SdpAudioFormat("L16", 8000, 1)));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001072
1073 // Insert one packet.
Karl Wiberg45eb1352019-10-10 14:23:00 +02001074 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001075
1076 EXPECT_EQ(5u, neteq_->sync_buffer_for_test()->FutureLength());
1077
1078 // Pull audio once.
1079 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001080 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001081 bool muted;
1082 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001083 ASSERT_EQ(kMaxOutputSize, output.samples_per_channel_);
1084 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001085 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001086 EXPECT_THAT(output.packet_infos_, SizeIs(1));
Henrik Lundin05f71fc2015-09-01 11:51:58 +02001087
1088 EXPECT_CALL(mock_decoder, Die());
1089}
minyuel6d92bf52015-09-23 15:20:39 +02001090
1091// This test checks the behavior of NetEq when audio decoder fails.
1092TEST_F(NetEqImplTest, DecodingError) {
1093 UseNoMocks();
Niels Möllera1eb9c72018-12-07 15:24:42 +01001094 // Create a mock decoder object.
1095 MockAudioDecoder mock_decoder;
1096
1097 CreateInstance(
1098 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001099
1100 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001101 const int kSampleRateHz = 8000;
1102 const int kDecoderErrorCode = -97; // Any negative number.
1103
1104 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1105 const size_t kFrameLengthSamples =
1106 static_cast<size_t>(5 * kSampleRateHz / 1000);
1107
1108 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1109
1110 uint8_t payload[kPayloadLengthBytes] = {0};
1111
henrik.lundin246ef3e2017-04-24 09:14:32 -07001112 RTPHeader rtp_header;
1113 rtp_header.payloadType = kPayloadType;
1114 rtp_header.sequenceNumber = 0x1234;
1115 rtp_header.timestamp = 0x12345678;
1116 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001117
minyuel6d92bf52015-09-23 15:20:39 +02001118 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001119 EXPECT_CALL(mock_decoder, SampleRateHz())
1120 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001121 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001122 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001123 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001124 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
1125 EXPECT_CALL(mock_decoder, HasDecodePlc()).WillOnce(Return(false));
minyuel6d92bf52015-09-23 15:20:39 +02001126 int16_t dummy_output[kFrameLengthSamples] = {0};
1127
1128 {
1129 InSequence sequence; // Dummy variable.
1130 // Mock decoder works normally the first time.
1131 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001132 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001133 .Times(3)
1134 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001135 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001136 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001137 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001138 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001139 .RetiresOnSaturation();
1140
1141 // Then mock decoder fails. A common reason for failure can be buffer being
1142 // too short
1143 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001144 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001145 .WillOnce(Return(-1))
1146 .RetiresOnSaturation();
1147
1148 // Mock decoder finally returns to normal.
1149 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001150 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001151 .Times(2)
1152 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001153 DoAll(SetArrayArgument<3>(dummy_output,
1154 dummy_output + kFrameLengthSamples),
1155 SetArgPointee<4>(AudioDecoder::kSpeech),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001156 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001157 }
1158
Niels Möllera1eb9c72018-12-07 15:24:42 +01001159 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1160 SdpAudioFormat("L16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001161
1162 // Insert packets.
1163 for (int i = 0; i < 6; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001164 rtp_header.sequenceNumber += 1;
1165 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001166 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001167 }
1168
1169 // Pull audio.
1170 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001171 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001172 bool muted;
1173 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001174 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1175 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001176 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001177 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001178
1179 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001180 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001181 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1182 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001183 // We are not expecting anything for output.speech_type_, since an error was
1184 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001185
1186 // Pull audio again, should continue an expansion.
henrik.lundin7a926812016-05-12 13:51:28 -07001187 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001188 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1189 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001190 EXPECT_EQ(AudioFrame::kPLC, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001191 EXPECT_THAT(output.packet_infos_, IsEmpty());
minyuel6d92bf52015-09-23 15:20:39 +02001192
1193 // Pull audio again, should behave normal.
henrik.lundin7a926812016-05-12 13:51:28 -07001194 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001195 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1196 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001197 EXPECT_EQ(AudioFrame::kNormalSpeech, output.speech_type_);
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001198 EXPECT_THAT(output.packet_infos_, SizeIs(2)); // 5 ms packets vs 10 ms output
minyuel6d92bf52015-09-23 15:20:39 +02001199
1200 EXPECT_CALL(mock_decoder, Die());
1201}
1202
1203// This test checks the behavior of NetEq when audio decoder fails during CNG.
1204TEST_F(NetEqImplTest, DecodingErrorDuringInternalCng) {
1205 UseNoMocks();
Niels Möller50b66d52018-12-11 14:43:21 +01001206
1207 // Create a mock decoder object.
1208 MockAudioDecoder mock_decoder;
1209 CreateInstance(
1210 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(&mock_decoder));
minyuel6d92bf52015-09-23 15:20:39 +02001211
1212 const uint8_t kPayloadType = 17; // Just an arbitrary number.
minyuel6d92bf52015-09-23 15:20:39 +02001213 const int kSampleRateHz = 8000;
1214 const int kDecoderErrorCode = -97; // Any negative number.
1215
1216 // We let decoder return 5 ms each time, and therefore, 2 packets make 10 ms.
1217 const size_t kFrameLengthSamples =
1218 static_cast<size_t>(5 * kSampleRateHz / 1000);
1219
1220 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1221
1222 uint8_t payload[kPayloadLengthBytes] = {0};
1223
henrik.lundin246ef3e2017-04-24 09:14:32 -07001224 RTPHeader rtp_header;
1225 rtp_header.payloadType = kPayloadType;
1226 rtp_header.sequenceNumber = 0x1234;
1227 rtp_header.timestamp = 0x12345678;
1228 rtp_header.ssrc = 0x87654321;
minyuel6d92bf52015-09-23 15:20:39 +02001229
minyuel6d92bf52015-09-23 15:20:39 +02001230 EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
kwiberg342f7402016-06-16 03:18:00 -07001231 EXPECT_CALL(mock_decoder, SampleRateHz())
1232 .WillRepeatedly(Return(kSampleRateHz));
minyuel6d92bf52015-09-23 15:20:39 +02001233 EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
minyuel6d92bf52015-09-23 15:20:39 +02001234 EXPECT_CALL(mock_decoder, PacketDuration(_, _))
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001235 .WillRepeatedly(Return(rtc::checked_cast<int>(kFrameLengthSamples)));
Jonas Olssona4d87372019-07-05 19:08:33 +02001236 EXPECT_CALL(mock_decoder, ErrorCode()).WillOnce(Return(kDecoderErrorCode));
minyuel6d92bf52015-09-23 15:20:39 +02001237 int16_t dummy_output[kFrameLengthSamples] = {0};
1238
1239 {
1240 InSequence sequence; // Dummy variable.
1241 // Mock decoder works normally the first 2 times.
1242 EXPECT_CALL(mock_decoder,
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001243 DecodeInternal(_, kPayloadLengthBytes, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001244 .Times(2)
1245 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001246 DoAll(SetArrayArgument<3>(dummy_output,
minyuel6d92bf52015-09-23 15:20:39 +02001247 dummy_output + kFrameLengthSamples),
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001248 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001249 Return(rtc::checked_cast<int>(kFrameLengthSamples))))
minyuel6d92bf52015-09-23 15:20:39 +02001250 .RetiresOnSaturation();
1251
1252 // Then mock decoder fails. A common reason for failure can be buffer being
1253 // too short
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001254 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001255 .WillOnce(Return(-1))
1256 .RetiresOnSaturation();
1257
1258 // Mock decoder finally returns to normal.
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001259 EXPECT_CALL(mock_decoder, DecodeInternal(nullptr, 0, kSampleRateHz, _, _))
minyuel6d92bf52015-09-23 15:20:39 +02001260 .Times(2)
1261 .WillRepeatedly(
Peter Boströmd7b7ae82015-12-08 13:41:35 +01001262 DoAll(SetArrayArgument<3>(dummy_output,
1263 dummy_output + kFrameLengthSamples),
1264 SetArgPointee<4>(AudioDecoder::kComfortNoise),
Mirko Bonadeib7e17882017-10-20 11:18:47 +02001265 Return(rtc::checked_cast<int>(kFrameLengthSamples))));
minyuel6d92bf52015-09-23 15:20:39 +02001266 }
1267
Niels Möller50b66d52018-12-11 14:43:21 +01001268 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1269 SdpAudioFormat("l16", 8000, 1)));
minyuel6d92bf52015-09-23 15:20:39 +02001270
1271 // Insert 2 packets. This will make netEq into codec internal CNG mode.
1272 for (int i = 0; i < 2; ++i) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001273 rtp_header.sequenceNumber += 1;
1274 rtp_header.timestamp += kFrameLengthSamples;
Karl Wiberg45eb1352019-10-10 14:23:00 +02001275 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyuel6d92bf52015-09-23 15:20:39 +02001276 }
1277
1278 // Pull audio.
1279 const size_t kMaxOutputSize = static_cast<size_t>(10 * kSampleRateHz / 1000);
henrik.lundin6d8e0112016-03-04 10:34:21 -08001280 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001281 bool muted;
1282 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001283 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1284 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001285 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001286
1287 // Pull audio again. Decoder fails.
henrik.lundin7a926812016-05-12 13:51:28 -07001288 EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001289 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1290 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001291 // We are not expecting anything for output.speech_type_, since an error was
1292 // returned.
minyuel6d92bf52015-09-23 15:20:39 +02001293
1294 // Pull audio again, should resume codec CNG.
henrik.lundin7a926812016-05-12 13:51:28 -07001295 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -08001296 EXPECT_EQ(kMaxOutputSize, output.samples_per_channel_);
1297 EXPECT_EQ(1u, output.num_channels_);
henrik.lundin55480f52016-03-08 02:37:57 -08001298 EXPECT_EQ(AudioFrame::kCNG, output.speech_type_);
minyuel6d92bf52015-09-23 15:20:39 +02001299
1300 EXPECT_CALL(mock_decoder, Die());
1301}
1302
henrik.lundind89814b2015-11-23 06:49:25 -08001303// Tests that the return value from last_output_sample_rate_hz() is equal to the
1304// configured inital sample rate.
1305TEST_F(NetEqImplTest, InitialLastOutputSampleRate) {
1306 UseNoMocks();
1307 config_.sample_rate_hz = 48000;
1308 CreateInstance();
1309 EXPECT_EQ(48000, neteq_->last_output_sample_rate_hz());
1310}
1311
henrik.lundined497212016-04-25 10:11:38 -07001312TEST_F(NetEqImplTest, TickTimerIncrement) {
1313 UseNoMocks();
1314 CreateInstance();
1315 ASSERT_TRUE(tick_timer_);
1316 EXPECT_EQ(0u, tick_timer_->ticks());
1317 AudioFrame output;
henrik.lundin7a926812016-05-12 13:51:28 -07001318 bool muted;
1319 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
henrik.lundined497212016-04-25 10:11:38 -07001320 EXPECT_EQ(1u, tick_timer_->ticks());
1321}
1322
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001323TEST_F(NetEqImplTest, SetBaseMinimumDelay) {
1324 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001325 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001326 CreateInstance();
1327
Ivo Creusen53a31f72019-10-24 15:20:39 +02001328 EXPECT_CALL(*mock_neteq_controller_, SetBaseMinimumDelay(_))
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001329 .WillOnce(Return(true))
1330 .WillOnce(Return(false));
1331
1332 const int delay_ms = 200;
1333
1334 EXPECT_EQ(true, neteq_->SetBaseMinimumDelayMs(delay_ms));
1335 EXPECT_EQ(false, neteq_->SetBaseMinimumDelayMs(delay_ms));
1336}
1337
1338TEST_F(NetEqImplTest, GetBaseMinimumDelayMs) {
1339 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001340 use_mock_neteq_controller_ = true;
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001341 CreateInstance();
1342
1343 const int delay_ms = 200;
1344
Ivo Creusen53a31f72019-10-24 15:20:39 +02001345 EXPECT_CALL(*mock_neteq_controller_, GetBaseMinimumDelay())
Ruslan Burakov9bee67c2019-02-05 13:49:26 +01001346 .WillOnce(Return(delay_ms));
1347
1348 EXPECT_EQ(delay_ms, neteq_->GetBaseMinimumDelayMs());
1349}
1350
henrik.lundin114c1b32017-04-26 07:47:32 -07001351TEST_F(NetEqImplTest, TargetDelayMs) {
1352 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001353 use_mock_neteq_controller_ = true;
henrik.lundin114c1b32017-04-26 07:47:32 -07001354 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001355 constexpr int kTargetLevelMs = 510;
1356 EXPECT_CALL(*mock_neteq_controller_, TargetLevelMs())
1357 .WillOnce(Return(kTargetLevelMs));
1358 EXPECT_EQ(510, neteq_->TargetDelayMs());
henrik.lundin114c1b32017-04-26 07:47:32 -07001359}
1360
henrik.lundinb8c55b12017-05-10 07:38:01 -07001361TEST_F(NetEqImplTest, InsertEmptyPacket) {
1362 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001363 use_mock_neteq_controller_ = true;
henrik.lundinb8c55b12017-05-10 07:38:01 -07001364 CreateInstance();
1365
1366 RTPHeader rtp_header;
1367 rtp_header.payloadType = 17;
1368 rtp_header.sequenceNumber = 0x1234;
1369 rtp_header.timestamp = 0x12345678;
1370 rtp_header.ssrc = 0x87654321;
1371
Ivo Creusen53a31f72019-10-24 15:20:39 +02001372 EXPECT_CALL(*mock_neteq_controller_, RegisterEmptyPacket());
henrik.lundinb8c55b12017-05-10 07:38:01 -07001373 neteq_->InsertEmptyPacket(rtp_header);
1374}
1375
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001376TEST_F(NetEqImplTest, EnableRtxHandling) {
1377 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001378 use_mock_neteq_controller_ = true;
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001379 config_.enable_rtx_handling = true;
1380 CreateInstance();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001381 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001382 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001383 .WillOnce(Return(NetEq::Operation::kNormal));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001384
1385 const int kPayloadLengthSamples = 80;
1386 const size_t kPayloadLengthBytes = 2 * kPayloadLengthSamples; // PCM 16-bit.
1387 const uint8_t kPayloadType = 17; // Just an arbitrary number.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001388 uint8_t payload[kPayloadLengthBytes] = {0};
1389 RTPHeader rtp_header;
1390 rtp_header.payloadType = kPayloadType;
1391 rtp_header.sequenceNumber = 0x1234;
1392 rtp_header.timestamp = 0x12345678;
1393 rtp_header.ssrc = 0x87654321;
1394
Niels Möller05543682019-01-10 16:55:06 +01001395 EXPECT_TRUE(neteq_->RegisterPayloadType(kPayloadType,
1396 SdpAudioFormat("l16", 8000, 1)));
Karl Wiberg45eb1352019-10-10 14:23:00 +02001397 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001398 AudioFrame output;
1399 bool muted;
1400 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output, &muted));
1401
1402 // Insert second packet that was sent before the first packet.
1403 rtp_header.sequenceNumber -= 1;
1404 rtp_header.timestamp -= kPayloadLengthSamples;
Ivo Creusen53a31f72019-10-24 15:20:39 +02001405 EXPECT_CALL(*mock_neteq_controller_,
1406 PacketArrived(
1407 /*last_cng_or_dtmf*/ _,
1408 /*packet_length_samples*/ kPayloadLengthSamples,
1409 /*should_update_stats*/ _,
1410 /*main_sequence_number*/ rtp_header.sequenceNumber,
1411 /*main_timestamp*/ rtp_header.timestamp,
1412 /*fs_hz*/ 8000));
1413
Karl Wiberg45eb1352019-10-10 14:23:00 +02001414 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
Jakob Ivarsson39b934b2019-01-10 10:28:23 +01001415}
1416
minyue5bd33972016-05-02 04:46:11 -07001417class Decoder120ms : public AudioDecoder {
1418 public:
kwiberg347d3512016-06-16 01:59:09 -07001419 Decoder120ms(int sample_rate_hz, SpeechType speech_type)
1420 : sample_rate_hz_(sample_rate_hz),
1421 next_value_(1),
minyue5bd33972016-05-02 04:46:11 -07001422 speech_type_(speech_type) {}
1423
1424 int DecodeInternal(const uint8_t* encoded,
1425 size_t encoded_len,
1426 int sample_rate_hz,
1427 int16_t* decoded,
1428 SpeechType* speech_type) override {
kwiberg347d3512016-06-16 01:59:09 -07001429 EXPECT_EQ(sample_rate_hz_, sample_rate_hz);
minyue5bd33972016-05-02 04:46:11 -07001430 size_t decoded_len =
1431 rtc::CheckedDivExact(sample_rate_hz, 1000) * 120 * Channels();
1432 for (size_t i = 0; i < decoded_len; ++i) {
1433 decoded[i] = next_value_++;
1434 }
1435 *speech_type = speech_type_;
Mirko Bonadei737e0732017-10-19 09:00:17 +02001436 return rtc::checked_cast<int>(decoded_len);
minyue5bd33972016-05-02 04:46:11 -07001437 }
1438
1439 void Reset() override { next_value_ = 1; }
kwiberg347d3512016-06-16 01:59:09 -07001440 int SampleRateHz() const override { return sample_rate_hz_; }
minyue5bd33972016-05-02 04:46:11 -07001441 size_t Channels() const override { return 2; }
1442
1443 private:
kwiberg347d3512016-06-16 01:59:09 -07001444 int sample_rate_hz_;
minyue5bd33972016-05-02 04:46:11 -07001445 int16_t next_value_;
1446 SpeechType speech_type_;
1447};
1448
1449class NetEqImplTest120ms : public NetEqImplTest {
1450 protected:
1451 NetEqImplTest120ms() : NetEqImplTest() {}
1452 virtual ~NetEqImplTest120ms() {}
1453
1454 void CreateInstanceNoMocks() {
1455 UseNoMocks();
Niels Möllera0f44302018-11-30 10:45:12 +01001456 CreateInstance(decoder_factory_);
1457 EXPECT_TRUE(neteq_->RegisterPayloadType(
1458 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001459 }
1460
1461 void CreateInstanceWithDelayManagerMock() {
1462 UseNoMocks();
Ivo Creusen53a31f72019-10-24 15:20:39 +02001463 use_mock_neteq_controller_ = true;
Niels Möllera0f44302018-11-30 10:45:12 +01001464 CreateInstance(decoder_factory_);
1465 EXPECT_TRUE(neteq_->RegisterPayloadType(
1466 kPayloadType, SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}})));
minyue5bd33972016-05-02 04:46:11 -07001467 }
1468
1469 uint32_t timestamp_diff_between_packets() const {
1470 return rtc::CheckedDivExact(kSamplingFreq_, 1000u) * 120;
1471 }
1472
1473 uint32_t first_timestamp() const { return 10u; }
1474
1475 void GetFirstPacket() {
henrik.lundin7a926812016-05-12 13:51:28 -07001476 bool muted;
minyue5bd33972016-05-02 04:46:11 -07001477 for (int i = 0; i < 12; i++) {
henrik.lundin7a926812016-05-12 13:51:28 -07001478 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
1479 EXPECT_FALSE(muted);
minyue5bd33972016-05-02 04:46:11 -07001480 }
1481 }
1482
1483 void InsertPacket(uint32_t timestamp) {
henrik.lundin246ef3e2017-04-24 09:14:32 -07001484 RTPHeader rtp_header;
1485 rtp_header.payloadType = kPayloadType;
1486 rtp_header.sequenceNumber = sequence_number_;
1487 rtp_header.timestamp = timestamp;
1488 rtp_header.ssrc = 15;
minyue5bd33972016-05-02 04:46:11 -07001489 const size_t kPayloadLengthBytes = 1; // This can be arbitrary.
1490 uint8_t payload[kPayloadLengthBytes] = {0};
Karl Wiberg45eb1352019-10-10 14:23:00 +02001491 EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_header, payload));
minyue5bd33972016-05-02 04:46:11 -07001492 sequence_number_++;
1493 }
1494
1495 void Register120msCodec(AudioDecoder::SpeechType speech_type) {
Niels Möllera0f44302018-11-30 10:45:12 +01001496 const uint32_t sampling_freq = kSamplingFreq_;
1497 decoder_factory_ =
1498 new rtc::RefCountedObject<test::FunctionAudioDecoderFactory>(
1499 [sampling_freq, speech_type]() {
1500 std::unique_ptr<AudioDecoder> decoder =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001501 std::make_unique<Decoder120ms>(sampling_freq, speech_type);
Niels Möllera0f44302018-11-30 10:45:12 +01001502 RTC_CHECK_EQ(2, decoder->Channels());
1503 return decoder;
1504 });
minyue5bd33972016-05-02 04:46:11 -07001505 }
1506
Niels Möllera0f44302018-11-30 10:45:12 +01001507 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
minyue5bd33972016-05-02 04:46:11 -07001508 AudioFrame output_;
1509 const uint32_t kPayloadType = 17;
1510 const uint32_t kSamplingFreq_ = 48000;
1511 uint16_t sequence_number_ = 1;
1512};
1513
minyue5bd33972016-05-02 04:46:11 -07001514TEST_F(NetEqImplTest120ms, CodecInternalCng) {
minyue5bd33972016-05-02 04:46:11 -07001515 Register120msCodec(AudioDecoder::kComfortNoise);
Niels Möllera0f44302018-11-30 10:45:12 +01001516 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001517
1518 InsertPacket(first_timestamp());
1519 GetFirstPacket();
1520
henrik.lundin7a926812016-05-12 13:51:28 -07001521 bool muted;
1522 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001523 EXPECT_EQ(NetEq::Operation::kCodecInternalCng,
1524 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001525}
1526
1527TEST_F(NetEqImplTest120ms, Normal) {
minyue5bd33972016-05-02 04:46:11 -07001528 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001529 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001530
1531 InsertPacket(first_timestamp());
1532 GetFirstPacket();
1533
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001534 EXPECT_EQ(NetEq::Operation::kNormal, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001535}
1536
1537TEST_F(NetEqImplTest120ms, Merge) {
Niels Möllera0f44302018-11-30 10:45:12 +01001538 Register120msCodec(AudioDecoder::kSpeech);
minyue5bd33972016-05-02 04:46:11 -07001539 CreateInstanceWithDelayManagerMock();
1540
Ivo Creusen53a31f72019-10-24 15:20:39 +02001541 EXPECT_CALL(*mock_neteq_controller_, CngOff()).WillRepeatedly(Return(true));
minyue5bd33972016-05-02 04:46:11 -07001542 InsertPacket(first_timestamp());
1543
1544 GetFirstPacket();
henrik.lundin7a926812016-05-12 13:51:28 -07001545 bool muted;
Ivo Creusen53a31f72019-10-24 15:20:39 +02001546 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001547 .WillOnce(Return(NetEq::Operation::kExpand));
henrik.lundin7a926812016-05-12 13:51:28 -07001548 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
minyue5bd33972016-05-02 04:46:11 -07001549
1550 InsertPacket(first_timestamp() + 2 * timestamp_diff_between_packets());
1551
Ivo Creusen53a31f72019-10-24 15:20:39 +02001552 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001553 .WillOnce(Return(NetEq::Operation::kMerge));
minyue5bd33972016-05-02 04:46:11 -07001554
henrik.lundin7a926812016-05-12 13:51:28 -07001555 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001556 EXPECT_EQ(NetEq::Operation::kMerge, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001557}
1558
1559TEST_F(NetEqImplTest120ms, Expand) {
minyue5bd33972016-05-02 04:46:11 -07001560 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001561 CreateInstanceNoMocks();
minyue5bd33972016-05-02 04:46:11 -07001562
1563 InsertPacket(first_timestamp());
1564 GetFirstPacket();
1565
henrik.lundin7a926812016-05-12 13:51:28 -07001566 bool muted;
1567 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001568 EXPECT_EQ(NetEq::Operation::kExpand, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001569}
1570
1571TEST_F(NetEqImplTest120ms, FastAccelerate) {
minyue5bd33972016-05-02 04:46:11 -07001572 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001573 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001574
1575 InsertPacket(first_timestamp());
1576 GetFirstPacket();
1577 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1578
Ivo Creusen53a31f72019-10-24 15:20:39 +02001579 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001580 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001581 .WillOnce(Return(NetEq::Operation::kFastAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001582
henrik.lundin7a926812016-05-12 13:51:28 -07001583 bool muted;
1584 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001585 EXPECT_EQ(NetEq::Operation::kFastAccelerate,
1586 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001587}
1588
1589TEST_F(NetEqImplTest120ms, PreemptiveExpand) {
minyue5bd33972016-05-02 04:46:11 -07001590 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001591 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001592
1593 InsertPacket(first_timestamp());
1594 GetFirstPacket();
1595
1596 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1597
Ivo Creusen53a31f72019-10-24 15:20:39 +02001598 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001599 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001600 .WillOnce(Return(NetEq::Operation::kPreemptiveExpand));
minyue5bd33972016-05-02 04:46:11 -07001601
henrik.lundin7a926812016-05-12 13:51:28 -07001602 bool muted;
1603 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001604 EXPECT_EQ(NetEq::Operation::kPreemptiveExpand,
1605 neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001606}
1607
1608TEST_F(NetEqImplTest120ms, Accelerate) {
minyue5bd33972016-05-02 04:46:11 -07001609 Register120msCodec(AudioDecoder::kSpeech);
Niels Möllera0f44302018-11-30 10:45:12 +01001610 CreateInstanceWithDelayManagerMock();
minyue5bd33972016-05-02 04:46:11 -07001611
1612 InsertPacket(first_timestamp());
1613 GetFirstPacket();
1614
1615 InsertPacket(first_timestamp() + timestamp_diff_between_packets());
1616
Ivo Creusen53a31f72019-10-24 15:20:39 +02001617 EXPECT_CALL(*mock_neteq_controller_, GetDecision(_, _))
minyue5bd33972016-05-02 04:46:11 -07001618 .Times(1)
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001619 .WillOnce(Return(NetEq::Operation::kAccelerate));
minyue5bd33972016-05-02 04:46:11 -07001620
henrik.lundin7a926812016-05-12 13:51:28 -07001621 bool muted;
1622 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_, &muted));
Ivo Creusen3ce44a32019-10-31 14:38:11 +01001623 EXPECT_EQ(NetEq::Operation::kAccelerate, neteq_->last_operation_for_test());
minyue5bd33972016-05-02 04:46:11 -07001624}
1625
Alessio Bazzica8f319a32019-07-24 16:47:02 +00001626} // namespace webrtc