blob: a9bb36fff94bb8323c53ad816f8089c3a50dbc9c [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
2 * Copyright (c) 2015 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
11#include <list>
kwiberg1c07c702017-03-27 07:15:49 -070012#include <map>
kwibergb25345e2016-03-12 06:10:44 -080013#include <memory>
zstein7cb69d52017-05-08 11:52:38 -070014#include <utility>
solenbergc7a8b082015-10-16 14:35:07 -070015
Karl Wibergf3850f62017-11-02 13:04:41 +010016#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/test/mock_audio_mixer.h"
18#include "call/audio_state.h"
19#include "call/call.h"
20#include "call/fake_rtp_transport_controller_send.h"
21#include "logging/rtc_event_log/rtc_event_log.h"
22#include "modules/audio_device/include/mock_audio_device.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010023#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/congestion_controller/include/mock/mock_send_side_congestion_controller.h"
25#include "modules/pacing/mock/mock_paced_sender.h"
26#include "modules/rtp_rtcp/include/rtp_rtcp.h"
27#include "rtc_base/ptr_util.h"
28#include "test/fake_encoder.h"
29#include "test/gtest.h"
30#include "test/mock_audio_decoder_factory.h"
31#include "test/mock_transport.h"
32#include "test/mock_voice_engine.h"
solenbergc7a8b082015-10-16 14:35:07 -070033
34namespace {
35
36struct CallHelper {
ossu29b1a8d2016-06-13 07:34:51 -070037 explicit CallHelper(
38 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory = nullptr)
39 : voice_engine_(decoder_factory) {
solenberg566ef242015-11-06 15:34:49 -080040 webrtc::AudioState::Config audio_state_config;
41 audio_state_config.voice_engine = &voice_engine_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010042 audio_state_config.audio_mixer =
43 new rtc::RefCountedObject<webrtc::test::MockAudioMixer>();
44 audio_state_config.audio_processing =
45 new rtc::RefCountedObject<webrtc::test::MockAudioProcessing>();
46 audio_state_config.audio_device_module =
47 new rtc::RefCountedObject<webrtc::test::MockAudioDeviceModule>();
skvlad11a9cbf2016-10-07 11:53:05 -070048 webrtc::Call::Config config(&event_log_);
solenberg566ef242015-11-06 15:34:49 -080049 config.audio_state = webrtc::AudioState::Create(audio_state_config);
solenbergc7a8b082015-10-16 14:35:07 -070050 call_.reset(webrtc::Call::Create(config));
51 }
52
53 webrtc::Call* operator->() { return call_.get(); }
solenberg7602aab2016-11-14 11:30:07 -080054 webrtc::test::MockVoiceEngine* voice_engine() { return &voice_engine_; }
solenbergc7a8b082015-10-16 14:35:07 -070055
56 private:
solenberg3a941542015-11-16 07:34:50 -080057 testing::NiceMock<webrtc::test::MockVoiceEngine> voice_engine_;
skvlad11a9cbf2016-10-07 11:53:05 -070058 webrtc::RtcEventLogNullImpl event_log_;
kwibergb25345e2016-03-12 06:10:44 -080059 std::unique_ptr<webrtc::Call> call_;
solenbergc7a8b082015-10-16 14:35:07 -070060};
61} // namespace
62
63namespace webrtc {
64
65TEST(CallTest, ConstructDestruct) {
66 CallHelper call;
67}
68
69TEST(CallTest, CreateDestroy_AudioSendStream) {
70 CallHelper call;
71 AudioSendStream::Config config(nullptr);
72 config.rtp.ssrc = 42;
73 config.voe_channel_id = 123;
74 AudioSendStream* stream = call->CreateAudioSendStream(config);
75 EXPECT_NE(stream, nullptr);
76 call->DestroyAudioSendStream(stream);
77}
78
79TEST(CallTest, CreateDestroy_AudioReceiveStream) {
ossu29b1a8d2016-06-13 07:34:51 -070080 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
81 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
82 CallHelper call(decoder_factory);
solenbergc7a8b082015-10-16 14:35:07 -070083 AudioReceiveStream::Config config;
84 config.rtp.remote_ssrc = 42;
85 config.voe_channel_id = 123;
ossu29b1a8d2016-06-13 07:34:51 -070086 config.decoder_factory = decoder_factory;
solenbergc7a8b082015-10-16 14:35:07 -070087 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
88 EXPECT_NE(stream, nullptr);
89 call->DestroyAudioReceiveStream(stream);
90}
91
92TEST(CallTest, CreateDestroy_AudioSendStreams) {
93 CallHelper call;
94 AudioSendStream::Config config(nullptr);
95 config.voe_channel_id = 123;
96 std::list<AudioSendStream*> streams;
97 for (int i = 0; i < 2; ++i) {
98 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
99 config.rtp.ssrc = ssrc;
100 AudioSendStream* stream = call->CreateAudioSendStream(config);
101 EXPECT_NE(stream, nullptr);
102 if (ssrc & 1) {
103 streams.push_back(stream);
104 } else {
105 streams.push_front(stream);
106 }
107 }
108 for (auto s : streams) {
109 call->DestroyAudioSendStream(s);
110 }
111 streams.clear();
112 }
113}
114
115TEST(CallTest, CreateDestroy_AudioReceiveStreams) {
ossu29b1a8d2016-06-13 07:34:51 -0700116 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
117 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
118 CallHelper call(decoder_factory);
solenbergc7a8b082015-10-16 14:35:07 -0700119 AudioReceiveStream::Config config;
120 config.voe_channel_id = 123;
ossu29b1a8d2016-06-13 07:34:51 -0700121 config.decoder_factory = decoder_factory;
solenbergc7a8b082015-10-16 14:35:07 -0700122 std::list<AudioReceiveStream*> streams;
123 for (int i = 0; i < 2; ++i) {
124 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
125 config.rtp.remote_ssrc = ssrc;
126 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
127 EXPECT_NE(stream, nullptr);
128 if (ssrc & 1) {
129 streams.push_back(stream);
130 } else {
131 streams.push_front(stream);
132 }
133 }
134 for (auto s : streams) {
135 call->DestroyAudioReceiveStream(s);
136 }
137 streams.clear();
138 }
139}
brandtr25445d32016-10-23 23:37:14 -0700140
solenberg7602aab2016-11-14 11:30:07 -0800141TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_RecvFirst) {
142 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
143 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
144 CallHelper call(decoder_factory);
ossuc3d4b482017-05-23 06:07:11 -0700145 ::testing::NiceMock<MockRtpRtcp> mock_rtp_rtcp;
solenberg7602aab2016-11-14 11:30:07 -0800146
147 constexpr int kRecvChannelId = 101;
148
149 // Set up the mock to create a channel proxy which we know of, so that we can
150 // add our expectations to it.
151 test::MockVoEChannelProxy* recv_channel_proxy = nullptr;
152 EXPECT_CALL(*call.voice_engine(), ChannelProxyFactory(testing::_))
153 .WillRepeatedly(testing::Invoke([&](int channel_id) {
154 test::MockVoEChannelProxy* channel_proxy =
155 new testing::NiceMock<test::MockVoEChannelProxy>();
156 EXPECT_CALL(*channel_proxy, GetAudioDecoderFactory())
157 .WillRepeatedly(testing::ReturnRef(decoder_factory));
kwiberg1c07c702017-03-27 07:15:49 -0700158 EXPECT_CALL(*channel_proxy, SetReceiveCodecs(testing::_))
159 .WillRepeatedly(testing::Invoke(
160 [](const std::map<int, SdpAudioFormat>& codecs) {
161 EXPECT_THAT(codecs, testing::IsEmpty());
162 }));
ossuc3d4b482017-05-23 06:07:11 -0700163 EXPECT_CALL(*channel_proxy, GetRtpRtcp(testing::_, testing::_))
164 .WillRepeatedly(testing::SetArgPointee<0>(&mock_rtp_rtcp));
solenberg7602aab2016-11-14 11:30:07 -0800165 // If being called for the send channel, save a pointer to the channel
166 // proxy for later.
167 if (channel_id == kRecvChannelId) {
168 EXPECT_FALSE(recv_channel_proxy);
169 recv_channel_proxy = channel_proxy;
170 }
171 return channel_proxy;
172 }));
173
174 AudioReceiveStream::Config recv_config;
175 recv_config.rtp.remote_ssrc = 42;
176 recv_config.rtp.local_ssrc = 777;
177 recv_config.voe_channel_id = kRecvChannelId;
178 recv_config.decoder_factory = decoder_factory;
179 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
180 EXPECT_NE(recv_stream, nullptr);
181
182 EXPECT_CALL(*recv_channel_proxy, AssociateSendChannel(testing::_)).Times(1);
183 AudioSendStream::Config send_config(nullptr);
184 send_config.rtp.ssrc = 777;
185 send_config.voe_channel_id = 123;
186 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
187 EXPECT_NE(send_stream, nullptr);
188
189 EXPECT_CALL(*recv_channel_proxy, DisassociateSendChannel()).Times(1);
190 call->DestroyAudioSendStream(send_stream);
191
192 EXPECT_CALL(*recv_channel_proxy, DisassociateSendChannel()).Times(1);
193 call->DestroyAudioReceiveStream(recv_stream);
194}
195
196TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_SendFirst) {
197 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
198 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
199 CallHelper call(decoder_factory);
ossuc3d4b482017-05-23 06:07:11 -0700200 ::testing::NiceMock<MockRtpRtcp> mock_rtp_rtcp;
solenberg7602aab2016-11-14 11:30:07 -0800201
202 constexpr int kRecvChannelId = 101;
203
204 // Set up the mock to create a channel proxy which we know of, so that we can
205 // add our expectations to it.
206 test::MockVoEChannelProxy* recv_channel_proxy = nullptr;
207 EXPECT_CALL(*call.voice_engine(), ChannelProxyFactory(testing::_))
208 .WillRepeatedly(testing::Invoke([&](int channel_id) {
209 test::MockVoEChannelProxy* channel_proxy =
210 new testing::NiceMock<test::MockVoEChannelProxy>();
211 EXPECT_CALL(*channel_proxy, GetAudioDecoderFactory())
212 .WillRepeatedly(testing::ReturnRef(decoder_factory));
kwiberg1c07c702017-03-27 07:15:49 -0700213 EXPECT_CALL(*channel_proxy, SetReceiveCodecs(testing::_))
214 .WillRepeatedly(testing::Invoke(
215 [](const std::map<int, SdpAudioFormat>& codecs) {
216 EXPECT_THAT(codecs, testing::IsEmpty());
217 }));
ossuc3d4b482017-05-23 06:07:11 -0700218 EXPECT_CALL(*channel_proxy, GetRtpRtcp(testing::_, testing::_))
219 .WillRepeatedly(testing::SetArgPointee<0>(&mock_rtp_rtcp));
solenberg7602aab2016-11-14 11:30:07 -0800220 // If being called for the send channel, save a pointer to the channel
221 // proxy for later.
222 if (channel_id == kRecvChannelId) {
223 EXPECT_FALSE(recv_channel_proxy);
224 recv_channel_proxy = channel_proxy;
225 // We need to set this expectation here since the channel proxy is
226 // created as a side effect of CreateAudioReceiveStream().
227 EXPECT_CALL(*recv_channel_proxy,
228 AssociateSendChannel(testing::_)).Times(1);
229 }
230 return channel_proxy;
231 }));
232
233 AudioSendStream::Config send_config(nullptr);
234 send_config.rtp.ssrc = 777;
235 send_config.voe_channel_id = 123;
236 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
237 EXPECT_NE(send_stream, nullptr);
238
239 AudioReceiveStream::Config recv_config;
240 recv_config.rtp.remote_ssrc = 42;
241 recv_config.rtp.local_ssrc = 777;
242 recv_config.voe_channel_id = kRecvChannelId;
243 recv_config.decoder_factory = decoder_factory;
244 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
245 EXPECT_NE(recv_stream, nullptr);
246
247 EXPECT_CALL(*recv_channel_proxy, DisassociateSendChannel()).Times(1);
248 call->DestroyAudioReceiveStream(recv_stream);
249
250 call->DestroyAudioSendStream(send_stream);
251}
252
brandtr25445d32016-10-23 23:37:14 -0700253TEST(CallTest, CreateDestroy_FlexfecReceiveStream) {
254 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800255 MockTransport rtcp_send_transport;
256 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800257 config.payload_type = 118;
258 config.remote_ssrc = 38837212;
brandtr25445d32016-10-23 23:37:14 -0700259 config.protected_media_ssrcs = {27273};
260
261 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
262 EXPECT_NE(stream, nullptr);
263 call->DestroyFlexfecReceiveStream(stream);
264}
265
266TEST(CallTest, CreateDestroy_FlexfecReceiveStreams) {
267 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800268 MockTransport rtcp_send_transport;
269 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800270 config.payload_type = 118;
brandtr25445d32016-10-23 23:37:14 -0700271 std::list<FlexfecReceiveStream*> streams;
272
273 for (int i = 0; i < 2; ++i) {
274 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
brandtr1cfbd602016-12-08 04:17:53 -0800275 config.remote_ssrc = ssrc;
brandtr25445d32016-10-23 23:37:14 -0700276 config.protected_media_ssrcs = {ssrc + 1};
277 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
278 EXPECT_NE(stream, nullptr);
279 if (ssrc & 1) {
280 streams.push_back(stream);
281 } else {
282 streams.push_front(stream);
283 }
284 }
285 for (auto s : streams) {
286 call->DestroyFlexfecReceiveStream(s);
287 }
288 streams.clear();
289 }
290}
291
292TEST(CallTest, MultipleFlexfecReceiveStreamsProtectingSingleVideoStream) {
293 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800294 MockTransport rtcp_send_transport;
295 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800296 config.payload_type = 118;
brandtr25445d32016-10-23 23:37:14 -0700297 config.protected_media_ssrcs = {1324234};
298 FlexfecReceiveStream* stream;
299 std::list<FlexfecReceiveStream*> streams;
300
brandtr1cfbd602016-12-08 04:17:53 -0800301 config.remote_ssrc = 838383;
brandtr25445d32016-10-23 23:37:14 -0700302 stream = call->CreateFlexfecReceiveStream(config);
303 EXPECT_NE(stream, nullptr);
304 streams.push_back(stream);
305
brandtr1cfbd602016-12-08 04:17:53 -0800306 config.remote_ssrc = 424993;
brandtr25445d32016-10-23 23:37:14 -0700307 stream = call->CreateFlexfecReceiveStream(config);
308 EXPECT_NE(stream, nullptr);
309 streams.push_back(stream);
310
brandtr1cfbd602016-12-08 04:17:53 -0800311 config.remote_ssrc = 99383;
brandtr25445d32016-10-23 23:37:14 -0700312 stream = call->CreateFlexfecReceiveStream(config);
313 EXPECT_NE(stream, nullptr);
314 streams.push_back(stream);
315
brandtr1cfbd602016-12-08 04:17:53 -0800316 config.remote_ssrc = 5548;
brandtr25445d32016-10-23 23:37:14 -0700317 stream = call->CreateFlexfecReceiveStream(config);
318 EXPECT_NE(stream, nullptr);
319 streams.push_back(stream);
320
321 for (auto s : streams) {
322 call->DestroyFlexfecReceiveStream(s);
323 }
324}
325
zstein8c96a142017-05-17 11:49:12 -0700326namespace {
327struct CallBitrateHelper {
zstein4b979802017-06-02 14:37:37 -0700328 CallBitrateHelper() : CallBitrateHelper(Call::Config::BitrateConfig()) {}
zstein7cb69d52017-05-08 11:52:38 -0700329
zstein4b979802017-06-02 14:37:37 -0700330 explicit CallBitrateHelper(const Call::Config::BitrateConfig& bitrate_config)
Stefan Holmer5c8942a2017-08-22 16:16:44 +0200331 : mock_cc_(Clock::GetRealTimeClock(), &event_log_, &pacer_) {
zstein4b979802017-06-02 14:37:37 -0700332 Call::Config config(&event_log_);
333 config.bitrate_config = bitrate_config;
334 call_.reset(
335 Call::Create(config, rtc::MakeUnique<FakeRtpTransportControllerSend>(
Stefan Holmer5c8942a2017-08-22 16:16:44 +0200336 &packet_router_, &pacer_, &mock_cc_)));
zstein4b979802017-06-02 14:37:37 -0700337 }
zstein8c96a142017-05-17 11:49:12 -0700338
339 webrtc::Call* operator->() { return call_.get(); }
340 testing::NiceMock<test::MockSendSideCongestionController>& mock_cc() {
341 return mock_cc_;
342 }
343
344 private:
345 webrtc::RtcEventLogNullImpl event_log_;
346 PacketRouter packet_router_;
Stefan Holmer5c8942a2017-08-22 16:16:44 +0200347 testing::NiceMock<MockPacedSender> pacer_;
zstein8c96a142017-05-17 11:49:12 -0700348 testing::NiceMock<test::MockSendSideCongestionController> mock_cc_;
349 std::unique_ptr<Call> call_;
350};
351} // namespace
352
353TEST(CallBitrateTest, SetBitrateConfigWithValidConfigCallsSetBweBitrates) {
354 CallBitrateHelper call;
zstein7cb69d52017-05-08 11:52:38 -0700355
356 Call::Config::BitrateConfig bitrate_config;
357 bitrate_config.min_bitrate_bps = 1;
358 bitrate_config.start_bitrate_bps = 2;
359 bitrate_config.max_bitrate_bps = 3;
360
zstein8c96a142017-05-17 11:49:12 -0700361 EXPECT_CALL(call.mock_cc(), SetBweBitrates(1, 2, 3));
362 call->SetBitrateConfig(bitrate_config);
363}
364
365TEST(CallBitrateTest, SetBitrateConfigWithDifferentMinCallsSetBweBitrates) {
366 CallBitrateHelper call;
367
368 Call::Config::BitrateConfig bitrate_config;
369 bitrate_config.min_bitrate_bps = 10;
370 bitrate_config.start_bitrate_bps = 20;
371 bitrate_config.max_bitrate_bps = 30;
372 call->SetBitrateConfig(bitrate_config);
373
374 bitrate_config.min_bitrate_bps = 11;
zstein4b979802017-06-02 14:37:37 -0700375 EXPECT_CALL(call.mock_cc(), SetBweBitrates(11, -1, 30));
zstein8c96a142017-05-17 11:49:12 -0700376 call->SetBitrateConfig(bitrate_config);
377}
378
379TEST(CallBitrateTest, SetBitrateConfigWithDifferentStartCallsSetBweBitrates) {
380 CallBitrateHelper call;
381
382 Call::Config::BitrateConfig bitrate_config;
383 bitrate_config.min_bitrate_bps = 10;
384 bitrate_config.start_bitrate_bps = 20;
385 bitrate_config.max_bitrate_bps = 30;
386 call->SetBitrateConfig(bitrate_config);
387
388 bitrate_config.start_bitrate_bps = 21;
389 EXPECT_CALL(call.mock_cc(), SetBweBitrates(10, 21, 30));
390 call->SetBitrateConfig(bitrate_config);
391}
392
393TEST(CallBitrateTest, SetBitrateConfigWithDifferentMaxCallsSetBweBitrates) {
394 CallBitrateHelper call;
395
396 Call::Config::BitrateConfig bitrate_config;
397 bitrate_config.min_bitrate_bps = 10;
398 bitrate_config.start_bitrate_bps = 20;
399 bitrate_config.max_bitrate_bps = 30;
400 call->SetBitrateConfig(bitrate_config);
401
402 bitrate_config.max_bitrate_bps = 31;
zstein4b979802017-06-02 14:37:37 -0700403 EXPECT_CALL(call.mock_cc(), SetBweBitrates(10, -1, 31));
zstein8c96a142017-05-17 11:49:12 -0700404 call->SetBitrateConfig(bitrate_config);
405}
406
407TEST(CallBitrateTest, SetBitrateConfigWithSameConfigElidesSecondCall) {
408 CallBitrateHelper call;
zstein8c96a142017-05-17 11:49:12 -0700409 Call::Config::BitrateConfig bitrate_config;
410 bitrate_config.min_bitrate_bps = 1;
411 bitrate_config.start_bitrate_bps = 2;
412 bitrate_config.max_bitrate_bps = 3;
413
414 EXPECT_CALL(call.mock_cc(), SetBweBitrates(1, 2, 3)).Times(1);
415 call->SetBitrateConfig(bitrate_config);
416 call->SetBitrateConfig(bitrate_config);
417}
418
419TEST(CallBitrateTest,
420 SetBitrateConfigWithSameMinMaxAndNegativeStartElidesSecondCall) {
421 CallBitrateHelper call;
422
423 Call::Config::BitrateConfig bitrate_config;
424 bitrate_config.min_bitrate_bps = 1;
425 bitrate_config.start_bitrate_bps = 2;
426 bitrate_config.max_bitrate_bps = 3;
427
428 EXPECT_CALL(call.mock_cc(), SetBweBitrates(1, 2, 3)).Times(1);
429 call->SetBitrateConfig(bitrate_config);
430
431 bitrate_config.start_bitrate_bps = -1;
zstein7cb69d52017-05-08 11:52:38 -0700432 call->SetBitrateConfig(bitrate_config);
433}
434
ossuc3d4b482017-05-23 06:07:11 -0700435TEST(CallTest, RecreatingAudioStreamWithSameSsrcReusesRtpState) {
436 constexpr uint32_t kSSRC = 12345;
ossuc3d4b482017-05-23 06:07:11 -0700437
438 // There's similar functionality in cricket::VoEWrapper but it's not reachable
439 // from here. Since we're working on removing VoE interfaces, I doubt it's
440 // worth making VoEWrapper more easily available.
441 struct ScopedVoiceEngine {
442 ScopedVoiceEngine()
443 : voe(VoiceEngine::Create()),
444 base(VoEBase::GetInterface(voe)) {}
445 ~ScopedVoiceEngine() {
446 base->Release();
447 EXPECT_TRUE(VoiceEngine::Delete(voe));
448 }
449
450 VoiceEngine* voe;
451 VoEBase* base;
452 };
453 ScopedVoiceEngine voice_engine;
454
ossuc3d4b482017-05-23 06:07:11 -0700455 AudioState::Config audio_state_config;
456 audio_state_config.voice_engine = voice_engine.voe;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100457 audio_state_config.audio_mixer =
458 new rtc::RefCountedObject<test::MockAudioMixer>();
459 audio_state_config.audio_processing =
460 new rtc::RefCountedObject<test::MockAudioProcessing>();
461 audio_state_config.audio_device_module =
462 new rtc::RefCountedObject<test::MockAudioDeviceModule>();
463 voice_engine.base->Init(audio_state_config.audio_device_module, nullptr,
Karl Wibergf3850f62017-11-02 13:04:41 +0100464 CreateBuiltinAudioDecoderFactory());
ossuc3d4b482017-05-23 06:07:11 -0700465 auto audio_state = AudioState::Create(audio_state_config);
peaha9cc40b2017-06-29 08:32:09 -0700466
ossuc3d4b482017-05-23 06:07:11 -0700467 RtcEventLogNullImpl event_log;
468 Call::Config call_config(&event_log);
469 call_config.audio_state = audio_state;
470 std::unique_ptr<Call> call(Call::Create(call_config));
471
472 auto create_stream_and_get_rtp_state = [&](uint32_t ssrc) {
473 AudioSendStream::Config config(nullptr);
474 config.rtp.ssrc = ssrc;
475 config.voe_channel_id = voice_engine.base->CreateChannel();
476 AudioSendStream* stream = call->CreateAudioSendStream(config);
477 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine.voe);
478 auto channel_proxy = voe_impl->GetChannelProxy(config.voe_channel_id);
479 RtpRtcp* rtp_rtcp = nullptr;
480 RtpReceiver* rtp_receiver = nullptr; // Unused but required for call.
481 channel_proxy->GetRtpRtcp(&rtp_rtcp, &rtp_receiver);
482 const RtpState rtp_state = rtp_rtcp->GetRtpState();
483 call->DestroyAudioSendStream(stream);
484 voice_engine.base->DeleteChannel(config.voe_channel_id);
485 return rtp_state;
486 };
487
488 const RtpState rtp_state1 = create_stream_and_get_rtp_state(kSSRC);
489 const RtpState rtp_state2 = create_stream_and_get_rtp_state(kSSRC);
490
491 EXPECT_EQ(rtp_state1.sequence_number, rtp_state2.sequence_number);
492 EXPECT_EQ(rtp_state1.start_timestamp, rtp_state2.start_timestamp);
493 EXPECT_EQ(rtp_state1.timestamp, rtp_state2.timestamp);
494 EXPECT_EQ(rtp_state1.capture_time_ms, rtp_state2.capture_time_ms);
495 EXPECT_EQ(rtp_state1.last_timestamp_time_ms,
496 rtp_state2.last_timestamp_time_ms);
497 EXPECT_EQ(rtp_state1.media_has_been_sent, rtp_state2.media_has_been_sent);
498}
zstein4b979802017-06-02 14:37:37 -0700499TEST(CallBitrateTest, BiggerMaskMinUsed) {
500 CallBitrateHelper call;
501 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100502 mask.min_bitrate_bps = 1234;
zstein4b979802017-06-02 14:37:37 -0700503
504 EXPECT_CALL(call.mock_cc(),
505 SetBweBitrates(*mask.min_bitrate_bps, testing::_, testing::_));
506 call->SetBitrateConfigMask(mask);
507}
508
509TEST(CallBitrateTest, BiggerConfigMinUsed) {
510 CallBitrateHelper call;
511 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100512 mask.min_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700513 EXPECT_CALL(call.mock_cc(), SetBweBitrates(1000, testing::_, testing::_));
514 call->SetBitrateConfigMask(mask);
515
516 Call::Config::BitrateConfig config;
517 config.min_bitrate_bps = 1234;
518
519 EXPECT_CALL(call.mock_cc(), SetBweBitrates(1234, testing::_, testing::_));
520 call->SetBitrateConfig(config);
521}
522
523// The last call to set start should be used.
524TEST(CallBitrateTest, LatestStartMaskPreferred) {
525 CallBitrateHelper call;
526 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100527 mask.start_bitrate_bps = 1300;
zstein4b979802017-06-02 14:37:37 -0700528
529 EXPECT_CALL(call.mock_cc(),
530 SetBweBitrates(testing::_, *mask.start_bitrate_bps, testing::_));
531 call->SetBitrateConfigMask(mask);
532
533 Call::Config::BitrateConfig bitrate_config;
534 bitrate_config.start_bitrate_bps = 1200;
535
536 EXPECT_CALL(
537 call.mock_cc(),
538 SetBweBitrates(testing::_, bitrate_config.start_bitrate_bps, testing::_));
539 call->SetBitrateConfig(bitrate_config);
540}
541
542TEST(CallBitrateTest, SmallerMaskMaxUsed) {
543 Call::Config::BitrateConfig bitrate_config;
544 bitrate_config.max_bitrate_bps = bitrate_config.start_bitrate_bps + 2000;
545 CallBitrateHelper call(bitrate_config);
546
547 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100548 mask.max_bitrate_bps = bitrate_config.start_bitrate_bps + 1000;
zstein4b979802017-06-02 14:37:37 -0700549
550 EXPECT_CALL(call.mock_cc(),
551 SetBweBitrates(testing::_, testing::_, *mask.max_bitrate_bps));
552 call->SetBitrateConfigMask(mask);
553}
554
555TEST(CallBitrateTest, SmallerConfigMaxUsed) {
556 Call::Config::BitrateConfig bitrate_config;
557 bitrate_config.max_bitrate_bps = bitrate_config.start_bitrate_bps + 1000;
558 CallBitrateHelper call(bitrate_config);
559
560 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100561 mask.max_bitrate_bps = bitrate_config.start_bitrate_bps + 2000;
zstein4b979802017-06-02 14:37:37 -0700562
563 // Expect no calls because nothing changes
564 EXPECT_CALL(call.mock_cc(),
565 SetBweBitrates(testing::_, testing::_, testing::_))
566 .Times(0);
567 call->SetBitrateConfigMask(mask);
568}
569
570TEST(CallBitrateTest, MaskStartLessThanConfigMinClamped) {
571 Call::Config::BitrateConfig bitrate_config;
572 bitrate_config.min_bitrate_bps = 2000;
573 CallBitrateHelper call(bitrate_config);
574
575 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100576 mask.start_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700577
578 EXPECT_CALL(call.mock_cc(), SetBweBitrates(2000, 2000, testing::_));
579 call->SetBitrateConfigMask(mask);
580}
581
582TEST(CallBitrateTest, MaskStartGreaterThanConfigMaxClamped) {
583 Call::Config::BitrateConfig bitrate_config;
584 bitrate_config.start_bitrate_bps = 2000;
585 CallBitrateHelper call(bitrate_config);
586
587 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100588 mask.max_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700589
590 EXPECT_CALL(call.mock_cc(), SetBweBitrates(testing::_, -1, 1000));
591 call->SetBitrateConfigMask(mask);
592}
593
594TEST(CallBitrateTest, MaskMinGreaterThanConfigMaxClamped) {
595 Call::Config::BitrateConfig bitrate_config;
596 bitrate_config.min_bitrate_bps = 2000;
597 CallBitrateHelper call(bitrate_config);
598
599 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100600 mask.max_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700601
602 EXPECT_CALL(call.mock_cc(), SetBweBitrates(1000, testing::_, 1000));
603 call->SetBitrateConfigMask(mask);
604}
605
606TEST(CallBitrateTest, SettingMaskStartForcesUpdate) {
607 CallBitrateHelper call;
608
609 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100610 mask.start_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700611
612 // SetBweBitrates should be called twice with the same params since
613 // start_bitrate_bps is set.
614 EXPECT_CALL(call.mock_cc(), SetBweBitrates(testing::_, 1000, testing::_))
615 .Times(2);
616 call->SetBitrateConfigMask(mask);
617 call->SetBitrateConfigMask(mask);
618}
619
620TEST(CallBitrateTest, SetBitrateConfigWithNoChangesDoesNotCallSetBweBitrates) {
621 CallBitrateHelper call;
622
623 Call::Config::BitrateConfig config1;
624 config1.min_bitrate_bps = 0;
625 config1.start_bitrate_bps = 1000;
626 config1.max_bitrate_bps = -1;
627
628 Call::Config::BitrateConfig config2;
629 config2.min_bitrate_bps = 0;
630 config2.start_bitrate_bps = -1;
631 config2.max_bitrate_bps = -1;
632
633 // The second call should not call SetBweBitrates because it doesn't
634 // change any values.
635 EXPECT_CALL(call.mock_cc(), SetBweBitrates(0, 1000, -1));
636 call->SetBitrateConfig(config1);
637 call->SetBitrateConfig(config2);
638}
639
640// If SetBitrateConfig changes the max, but not the effective max,
641// SetBweBitrates shouldn't be called, to avoid unnecessary encoder
642// reconfigurations.
643TEST(CallBitrateTest, SetBweBitratesNotCalledWhenEffectiveMaxUnchanged) {
644 CallBitrateHelper call;
645
646 Call::Config::BitrateConfig config;
647 config.min_bitrate_bps = 0;
648 config.start_bitrate_bps = -1;
649 config.max_bitrate_bps = 2000;
650 EXPECT_CALL(call.mock_cc(), SetBweBitrates(testing::_, testing::_, 2000));
651 call->SetBitrateConfig(config);
652
653 // Reduce effective max to 1000 with the mask.
654 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100655 mask.max_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700656 EXPECT_CALL(call.mock_cc(), SetBweBitrates(testing::_, testing::_, 1000));
657 call->SetBitrateConfigMask(mask);
658
659 // This leaves the effective max unchanged, so SetBweBitrates shouldn't be
660 // called again.
661 config.max_bitrate_bps = 1000;
662 call->SetBitrateConfig(config);
663}
664
665// When the "start bitrate" mask is removed, SetBweBitrates shouldn't be called
666// again, since nothing's changing.
667TEST(CallBitrateTest, SetBweBitratesNotCalledWhenStartMaskRemoved) {
668 CallBitrateHelper call;
669
670 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100671 mask.start_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700672 EXPECT_CALL(call.mock_cc(), SetBweBitrates(0, 1000, -1));
673 call->SetBitrateConfigMask(mask);
674
675 mask.start_bitrate_bps.reset();
676 call->SetBitrateConfigMask(mask);
677}
678
679// Test that if SetBitrateConfig is called after SetBitrateConfigMask applies a
680// "start" value, the SetBitrateConfig call won't apply that start value a
681// second time.
682TEST(CallBitrateTest, SetBitrateConfigAfterSetBitrateConfigMaskWithStart) {
683 CallBitrateHelper call;
684
685 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100686 mask.start_bitrate_bps = 1000;
zstein4b979802017-06-02 14:37:37 -0700687 EXPECT_CALL(call.mock_cc(), SetBweBitrates(0, 1000, -1));
688 call->SetBitrateConfigMask(mask);
689
690 Call::Config::BitrateConfig config;
691 config.min_bitrate_bps = 0;
692 config.start_bitrate_bps = -1;
693 config.max_bitrate_bps = 5000;
694 // The start value isn't changing, so SetBweBitrates should be called with
695 // -1.
696 EXPECT_CALL(call.mock_cc(), SetBweBitrates(0, -1, 5000));
697 call->SetBitrateConfig(config);
698}
699
700TEST(CallBitrateTest, SetBweBitratesNotCalledWhenClampedMinUnchanged) {
701 Call::Config::BitrateConfig bitrate_config;
702 bitrate_config.start_bitrate_bps = 500;
703 bitrate_config.max_bitrate_bps = 1000;
704 CallBitrateHelper call(bitrate_config);
705
706 // Set min to 2000; it is clamped to the max (1000).
707 Call::Config::BitrateConfigMask mask;
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100708 mask.min_bitrate_bps = 2000;
zstein4b979802017-06-02 14:37:37 -0700709 EXPECT_CALL(call.mock_cc(), SetBweBitrates(1000, -1, 1000));
710 call->SetBitrateConfigMask(mask);
711
712 // Set min to 3000; the clamped value stays the same so nothing happens.
Oskar Sundbomfedc00c2017-11-16 10:55:08 +0100713 mask.min_bitrate_bps = 3000;
zstein4b979802017-06-02 14:37:37 -0700714 call->SetBitrateConfigMask(mask);
715}
ossuc3d4b482017-05-23 06:07:11 -0700716
solenbergc7a8b082015-10-16 14:35:07 -0700717} // namespace webrtc