blob: 564f6bd0323e014771266f9ec432b099758ef18b [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
zstein7cb69d52017-05-08 11:52:38 -070016#include "webrtc/base/ptr_util.h"
ossuf515ab82016-12-07 04:52:58 -080017#include "webrtc/call/audio_state.h"
18#include "webrtc/call/call.h"
zstein7cb69d52017-05-08 11:52:38 -070019#include "webrtc/call/fake_rtp_transport_controller_send.h"
skvlad11a9cbf2016-10-07 11:53:05 -070020#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
ossuf515ab82016-12-07 04:52:58 -080021#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
zstein7cb69d52017-05-08 11:52:38 -070022#include "webrtc/modules/congestion_controller/include/mock/mock_send_side_congestion_controller.h"
kwibergac9f8762016-09-30 22:29:43 -070023#include "webrtc/test/gtest.h"
kwiberg37e99fd2017-04-10 05:15:48 -070024#include "webrtc/test/mock_audio_decoder_factory.h"
brandtr8313a6f2017-01-13 07:41:19 -080025#include "webrtc/test/mock_transport.h"
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010026#include "webrtc/test/mock_voice_engine.h"
solenbergc7a8b082015-10-16 14:35:07 -070027
28namespace {
29
30struct CallHelper {
ossu29b1a8d2016-06-13 07:34:51 -070031 explicit CallHelper(
32 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory = nullptr)
33 : voice_engine_(decoder_factory) {
solenberg566ef242015-11-06 15:34:49 -080034 webrtc::AudioState::Config audio_state_config;
35 audio_state_config.voice_engine = &voice_engine_;
aleloi10111bc2016-11-17 06:48:48 -080036 audio_state_config.audio_mixer = webrtc::AudioMixerImpl::Create();
aleloidd310712016-11-17 06:28:59 -080037 EXPECT_CALL(voice_engine_, audio_device_module());
38 EXPECT_CALL(voice_engine_, audio_processing());
39 EXPECT_CALL(voice_engine_, audio_transport());
skvlad11a9cbf2016-10-07 11:53:05 -070040 webrtc::Call::Config config(&event_log_);
solenberg566ef242015-11-06 15:34:49 -080041 config.audio_state = webrtc::AudioState::Create(audio_state_config);
solenbergc7a8b082015-10-16 14:35:07 -070042 call_.reset(webrtc::Call::Create(config));
43 }
44
45 webrtc::Call* operator->() { return call_.get(); }
solenberg7602aab2016-11-14 11:30:07 -080046 webrtc::test::MockVoiceEngine* voice_engine() { return &voice_engine_; }
solenbergc7a8b082015-10-16 14:35:07 -070047
48 private:
solenberg3a941542015-11-16 07:34:50 -080049 testing::NiceMock<webrtc::test::MockVoiceEngine> voice_engine_;
skvlad11a9cbf2016-10-07 11:53:05 -070050 webrtc::RtcEventLogNullImpl event_log_;
kwibergb25345e2016-03-12 06:10:44 -080051 std::unique_ptr<webrtc::Call> call_;
solenbergc7a8b082015-10-16 14:35:07 -070052};
53} // namespace
54
55namespace webrtc {
56
57TEST(CallTest, ConstructDestruct) {
58 CallHelper call;
59}
60
61TEST(CallTest, CreateDestroy_AudioSendStream) {
62 CallHelper call;
63 AudioSendStream::Config config(nullptr);
64 config.rtp.ssrc = 42;
65 config.voe_channel_id = 123;
66 AudioSendStream* stream = call->CreateAudioSendStream(config);
67 EXPECT_NE(stream, nullptr);
68 call->DestroyAudioSendStream(stream);
69}
70
71TEST(CallTest, CreateDestroy_AudioReceiveStream) {
ossu29b1a8d2016-06-13 07:34:51 -070072 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
73 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
74 CallHelper call(decoder_factory);
solenbergc7a8b082015-10-16 14:35:07 -070075 AudioReceiveStream::Config config;
76 config.rtp.remote_ssrc = 42;
77 config.voe_channel_id = 123;
ossu29b1a8d2016-06-13 07:34:51 -070078 config.decoder_factory = decoder_factory;
solenbergc7a8b082015-10-16 14:35:07 -070079 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
80 EXPECT_NE(stream, nullptr);
81 call->DestroyAudioReceiveStream(stream);
82}
83
84TEST(CallTest, CreateDestroy_AudioSendStreams) {
85 CallHelper call;
86 AudioSendStream::Config config(nullptr);
87 config.voe_channel_id = 123;
88 std::list<AudioSendStream*> streams;
89 for (int i = 0; i < 2; ++i) {
90 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
91 config.rtp.ssrc = ssrc;
92 AudioSendStream* stream = call->CreateAudioSendStream(config);
93 EXPECT_NE(stream, nullptr);
94 if (ssrc & 1) {
95 streams.push_back(stream);
96 } else {
97 streams.push_front(stream);
98 }
99 }
100 for (auto s : streams) {
101 call->DestroyAudioSendStream(s);
102 }
103 streams.clear();
104 }
105}
106
107TEST(CallTest, CreateDestroy_AudioReceiveStreams) {
ossu29b1a8d2016-06-13 07:34:51 -0700108 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
109 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
110 CallHelper call(decoder_factory);
solenbergc7a8b082015-10-16 14:35:07 -0700111 AudioReceiveStream::Config config;
112 config.voe_channel_id = 123;
ossu29b1a8d2016-06-13 07:34:51 -0700113 config.decoder_factory = decoder_factory;
solenbergc7a8b082015-10-16 14:35:07 -0700114 std::list<AudioReceiveStream*> streams;
115 for (int i = 0; i < 2; ++i) {
116 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
117 config.rtp.remote_ssrc = ssrc;
118 AudioReceiveStream* stream = call->CreateAudioReceiveStream(config);
119 EXPECT_NE(stream, nullptr);
120 if (ssrc & 1) {
121 streams.push_back(stream);
122 } else {
123 streams.push_front(stream);
124 }
125 }
126 for (auto s : streams) {
127 call->DestroyAudioReceiveStream(s);
128 }
129 streams.clear();
130 }
131}
brandtr25445d32016-10-23 23:37:14 -0700132
solenberg7602aab2016-11-14 11:30:07 -0800133TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_RecvFirst) {
134 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
135 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
136 CallHelper call(decoder_factory);
137
138 constexpr int kRecvChannelId = 101;
139
140 // Set up the mock to create a channel proxy which we know of, so that we can
141 // add our expectations to it.
142 test::MockVoEChannelProxy* recv_channel_proxy = nullptr;
143 EXPECT_CALL(*call.voice_engine(), ChannelProxyFactory(testing::_))
144 .WillRepeatedly(testing::Invoke([&](int channel_id) {
145 test::MockVoEChannelProxy* channel_proxy =
146 new testing::NiceMock<test::MockVoEChannelProxy>();
147 EXPECT_CALL(*channel_proxy, GetAudioDecoderFactory())
148 .WillRepeatedly(testing::ReturnRef(decoder_factory));
kwiberg1c07c702017-03-27 07:15:49 -0700149 EXPECT_CALL(*channel_proxy, SetReceiveCodecs(testing::_))
150 .WillRepeatedly(testing::Invoke(
151 [](const std::map<int, SdpAudioFormat>& codecs) {
152 EXPECT_THAT(codecs, testing::IsEmpty());
153 }));
solenberg7602aab2016-11-14 11:30:07 -0800154 // If being called for the send channel, save a pointer to the channel
155 // proxy for later.
156 if (channel_id == kRecvChannelId) {
157 EXPECT_FALSE(recv_channel_proxy);
158 recv_channel_proxy = channel_proxy;
159 }
160 return channel_proxy;
161 }));
162
163 AudioReceiveStream::Config recv_config;
164 recv_config.rtp.remote_ssrc = 42;
165 recv_config.rtp.local_ssrc = 777;
166 recv_config.voe_channel_id = kRecvChannelId;
167 recv_config.decoder_factory = decoder_factory;
168 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
169 EXPECT_NE(recv_stream, nullptr);
170
171 EXPECT_CALL(*recv_channel_proxy, AssociateSendChannel(testing::_)).Times(1);
172 AudioSendStream::Config send_config(nullptr);
173 send_config.rtp.ssrc = 777;
174 send_config.voe_channel_id = 123;
175 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
176 EXPECT_NE(send_stream, nullptr);
177
178 EXPECT_CALL(*recv_channel_proxy, DisassociateSendChannel()).Times(1);
179 call->DestroyAudioSendStream(send_stream);
180
181 EXPECT_CALL(*recv_channel_proxy, DisassociateSendChannel()).Times(1);
182 call->DestroyAudioReceiveStream(recv_stream);
183}
184
185TEST(CallTest, CreateDestroy_AssociateAudioSendReceiveStreams_SendFirst) {
186 rtc::scoped_refptr<webrtc::AudioDecoderFactory> decoder_factory(
187 new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>);
188 CallHelper call(decoder_factory);
189
190 constexpr int kRecvChannelId = 101;
191
192 // Set up the mock to create a channel proxy which we know of, so that we can
193 // add our expectations to it.
194 test::MockVoEChannelProxy* recv_channel_proxy = nullptr;
195 EXPECT_CALL(*call.voice_engine(), ChannelProxyFactory(testing::_))
196 .WillRepeatedly(testing::Invoke([&](int channel_id) {
197 test::MockVoEChannelProxy* channel_proxy =
198 new testing::NiceMock<test::MockVoEChannelProxy>();
199 EXPECT_CALL(*channel_proxy, GetAudioDecoderFactory())
200 .WillRepeatedly(testing::ReturnRef(decoder_factory));
kwiberg1c07c702017-03-27 07:15:49 -0700201 EXPECT_CALL(*channel_proxy, SetReceiveCodecs(testing::_))
202 .WillRepeatedly(testing::Invoke(
203 [](const std::map<int, SdpAudioFormat>& codecs) {
204 EXPECT_THAT(codecs, testing::IsEmpty());
205 }));
solenberg7602aab2016-11-14 11:30:07 -0800206 // If being called for the send channel, save a pointer to the channel
207 // proxy for later.
208 if (channel_id == kRecvChannelId) {
209 EXPECT_FALSE(recv_channel_proxy);
210 recv_channel_proxy = channel_proxy;
211 // We need to set this expectation here since the channel proxy is
212 // created as a side effect of CreateAudioReceiveStream().
213 EXPECT_CALL(*recv_channel_proxy,
214 AssociateSendChannel(testing::_)).Times(1);
215 }
216 return channel_proxy;
217 }));
218
219 AudioSendStream::Config send_config(nullptr);
220 send_config.rtp.ssrc = 777;
221 send_config.voe_channel_id = 123;
222 AudioSendStream* send_stream = call->CreateAudioSendStream(send_config);
223 EXPECT_NE(send_stream, nullptr);
224
225 AudioReceiveStream::Config recv_config;
226 recv_config.rtp.remote_ssrc = 42;
227 recv_config.rtp.local_ssrc = 777;
228 recv_config.voe_channel_id = kRecvChannelId;
229 recv_config.decoder_factory = decoder_factory;
230 AudioReceiveStream* recv_stream = call->CreateAudioReceiveStream(recv_config);
231 EXPECT_NE(recv_stream, nullptr);
232
233 EXPECT_CALL(*recv_channel_proxy, DisassociateSendChannel()).Times(1);
234 call->DestroyAudioReceiveStream(recv_stream);
235
236 call->DestroyAudioSendStream(send_stream);
237}
238
brandtr25445d32016-10-23 23:37:14 -0700239TEST(CallTest, CreateDestroy_FlexfecReceiveStream) {
240 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800241 MockTransport rtcp_send_transport;
242 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800243 config.payload_type = 118;
244 config.remote_ssrc = 38837212;
brandtr25445d32016-10-23 23:37:14 -0700245 config.protected_media_ssrcs = {27273};
246
247 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
248 EXPECT_NE(stream, nullptr);
249 call->DestroyFlexfecReceiveStream(stream);
250}
251
252TEST(CallTest, CreateDestroy_FlexfecReceiveStreams) {
253 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800254 MockTransport rtcp_send_transport;
255 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800256 config.payload_type = 118;
brandtr25445d32016-10-23 23:37:14 -0700257 std::list<FlexfecReceiveStream*> streams;
258
259 for (int i = 0; i < 2; ++i) {
260 for (uint32_t ssrc = 0; ssrc < 1234567; ssrc += 34567) {
brandtr1cfbd602016-12-08 04:17:53 -0800261 config.remote_ssrc = ssrc;
brandtr25445d32016-10-23 23:37:14 -0700262 config.protected_media_ssrcs = {ssrc + 1};
263 FlexfecReceiveStream* stream = call->CreateFlexfecReceiveStream(config);
264 EXPECT_NE(stream, nullptr);
265 if (ssrc & 1) {
266 streams.push_back(stream);
267 } else {
268 streams.push_front(stream);
269 }
270 }
271 for (auto s : streams) {
272 call->DestroyFlexfecReceiveStream(s);
273 }
274 streams.clear();
275 }
276}
277
278TEST(CallTest, MultipleFlexfecReceiveStreamsProtectingSingleVideoStream) {
279 CallHelper call;
brandtr8313a6f2017-01-13 07:41:19 -0800280 MockTransport rtcp_send_transport;
281 FlexfecReceiveStream::Config config(&rtcp_send_transport);
brandtr1cfbd602016-12-08 04:17:53 -0800282 config.payload_type = 118;
brandtr25445d32016-10-23 23:37:14 -0700283 config.protected_media_ssrcs = {1324234};
284 FlexfecReceiveStream* stream;
285 std::list<FlexfecReceiveStream*> streams;
286
brandtr1cfbd602016-12-08 04:17:53 -0800287 config.remote_ssrc = 838383;
brandtr25445d32016-10-23 23:37:14 -0700288 stream = call->CreateFlexfecReceiveStream(config);
289 EXPECT_NE(stream, nullptr);
290 streams.push_back(stream);
291
brandtr1cfbd602016-12-08 04:17:53 -0800292 config.remote_ssrc = 424993;
brandtr25445d32016-10-23 23:37:14 -0700293 stream = call->CreateFlexfecReceiveStream(config);
294 EXPECT_NE(stream, nullptr);
295 streams.push_back(stream);
296
brandtr1cfbd602016-12-08 04:17:53 -0800297 config.remote_ssrc = 99383;
brandtr25445d32016-10-23 23:37:14 -0700298 stream = call->CreateFlexfecReceiveStream(config);
299 EXPECT_NE(stream, nullptr);
300 streams.push_back(stream);
301
brandtr1cfbd602016-12-08 04:17:53 -0800302 config.remote_ssrc = 5548;
brandtr25445d32016-10-23 23:37:14 -0700303 stream = call->CreateFlexfecReceiveStream(config);
304 EXPECT_NE(stream, nullptr);
305 streams.push_back(stream);
306
307 for (auto s : streams) {
308 call->DestroyFlexfecReceiveStream(s);
309 }
310}
311
zstein7cb69d52017-05-08 11:52:38 -0700312// TODO(zstein): This is just a motivating example for
313// MockSendSideCongestionController. It should be deleted once we have more
314// meaningful tests.
315TEST(CallTest, MockSendSideCongestionControllerExample) {
316 RtcEventLogNullImpl event_log;
317 Call::Config config(&event_log);
318
319 SimulatedClock clock(123456);
320 PacketRouter packet_router;
321 testing::NiceMock<test::MockSendSideCongestionController> mock_cc(
322 &clock, &event_log, &packet_router);
323 auto transport_send =
324 rtc::MakeUnique<FakeRtpTransportControllerSend>(&mock_cc);
325 std::unique_ptr<Call> call(Call::Create(config, std::move(transport_send)));
326
327 Call::Config::BitrateConfig bitrate_config;
328 bitrate_config.min_bitrate_bps = 1;
329 bitrate_config.start_bitrate_bps = 2;
330 bitrate_config.max_bitrate_bps = 3;
331
332 EXPECT_CALL(mock_cc, SetBweBitrates(1, 2, 3));
333 call->SetBitrateConfig(bitrate_config);
334}
335
solenbergc7a8b082015-10-16 14:35:07 -0700336} // namespace webrtc