blob: fc699d314364894c5b747e4dae7897e89e076986 [file] [log] [blame]
wu@webrtc.org364f2042013-11-20 21:49:41 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
wu@webrtc.org364f2042013-11-20 21:49:41 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
wu@webrtc.org364f2042013-11-20 21:49:41 +00009 */
10
Steve Anton36b29d12017-10-30 09:57:42 -070011#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080012#include <utility>
Steve Anton191c39f2018-01-24 19:35:55 -080013#include <vector>
kwiberg0eb15ed2015-12-17 03:04:15 -080014
Anders Carlsson67537952018-05-03 11:28:29 +020015#include "api/video_codecs/builtin_video_decoder_factory.h"
16#include "api/video_codecs/builtin_video_encoder_factory.h"
17#include "modules/audio_processing/include/audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/fakeportallocator.h"
Steve Antona3a92c22017-12-07 10:27:41 -080019#include "pc/sdputils.h"
Niels Möllera1cc73f2018-05-28 16:20:42 +020020#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "pc/test/fakertccertificategenerator.h"
22#include "pc/test/mockpeerconnectionobservers.h"
23#include "pc/test/peerconnectiontestwrapper.h"
24#include "rtc_base/gunit.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000025
wu@webrtc.org364f2042013-11-20 21:49:41 +000026using webrtc::FakeConstraints;
27using webrtc::FakeVideoTrackRenderer;
28using webrtc::IceCandidateInterface;
29using webrtc::MediaConstraintsInterface;
30using webrtc::MediaStreamInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080031using webrtc::MediaStreamTrackInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000032using webrtc::MockSetSessionDescriptionObserver;
33using webrtc::PeerConnectionInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080034using webrtc::RtpReceiverInterface;
Steve Antona3a92c22017-12-07 10:27:41 -080035using webrtc::SdpType;
wu@webrtc.org364f2042013-11-20 21:49:41 +000036using webrtc::SessionDescriptionInterface;
37using webrtc::VideoTrackInterface;
38
Steve Antona3a92c22017-12-07 10:27:41 -080039namespace {
Seth Hampson845e8782018-03-02 11:34:10 -080040const char kStreamIdBase[] = "stream_id";
Steve Antona3a92c22017-12-07 10:27:41 -080041const char kVideoTrackLabelBase[] = "video_track";
42const char kAudioTrackLabelBase[] = "audio_track";
43constexpr int kMaxWait = 10000;
44constexpr int kTestAudioFrameCount = 3;
45constexpr int kTestVideoFrameCount = 3;
46} // namespace
47
wu@webrtc.org364f2042013-11-20 21:49:41 +000048void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
49 PeerConnectionTestWrapper* callee) {
50 caller->SignalOnIceCandidateReady.connect(
51 callee, &PeerConnectionTestWrapper::AddIceCandidate);
52 callee->SignalOnIceCandidateReady.connect(
53 caller, &PeerConnectionTestWrapper::AddIceCandidate);
54
Yves Gerey665174f2018-06-19 15:03:05 +020055 caller->SignalOnSdpReady.connect(callee,
56 &PeerConnectionTestWrapper::ReceiveOfferSdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +000057 callee->SignalOnSdpReady.connect(
58 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
59}
60
danilchape9021a32016-05-17 01:52:02 -070061PeerConnectionTestWrapper::PeerConnectionTestWrapper(
62 const std::string& name,
63 rtc::Thread* network_thread,
64 rtc::Thread* worker_thread)
65 : name_(name),
66 network_thread_(network_thread),
67 worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000068
69PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
70
71bool PeerConnectionTestWrapper::CreatePc(
kwiberg9e5b11e2017-04-19 03:47:57 -070072 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
73 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
74 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
kwibergd1fe2812016-04-27 06:47:29 -070075 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -070076 new cricket::FakePortAllocator(network_thread_, nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000077
deadbeefee8c6d32015-08-13 14:27:18 -070078 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000079 if (fake_audio_capture_module_ == NULL) {
80 return false;
81 }
82
83 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -070084 network_thread_, worker_thread_, rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +020085 rtc::scoped_refptr<webrtc::AudioDeviceModule>(fake_audio_capture_module_),
86 audio_encoder_factory, audio_decoder_factory,
87 webrtc::CreateBuiltinVideoEncoderFactory(),
88 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
89 nullptr /* audio_processing */);
wu@webrtc.org364f2042013-11-20 21:49:41 +000090 if (!peer_connection_factory_) {
91 return false;
92 }
93
Henrik Boströmd79599d2016-06-01 13:58:50 +020094 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator(
deadbeef1b54a5f2017-01-23 19:39:57 -080095 new FakeRTCCertificateGenerator());
Henrik Boströmd79599d2016-06-01 13:58:50 +020096 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
Niels Möllerf06f9232018-08-07 12:32:18 +020097 config, std::move(port_allocator), std::move(cert_generator), this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000098
99 return peer_connection_.get() != NULL;
100}
101
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000103PeerConnectionTestWrapper::CreateDataChannel(
104 const std::string& label,
105 const webrtc::DataChannelInit& init) {
106 return peer_connection_->CreateDataChannel(label, &init);
107}
108
Steve Anton191c39f2018-01-24 19:35:55 -0800109void PeerConnectionTestWrapper::OnAddTrack(
110 rtc::scoped_refptr<RtpReceiverInterface> receiver,
111 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
112 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": OnAddTrack";
113 if (receiver->track()->kind() == MediaStreamTrackInterface::kVideoKind) {
114 auto* video_track =
115 static_cast<VideoTrackInterface*>(receiver->track().get());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200116 renderer_ = absl::make_unique<FakeVideoTrackRenderer>(video_track);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000117 }
118}
119
120void PeerConnectionTestWrapper::OnIceCandidate(
121 const IceCandidateInterface* candidate) {
122 std::string sdp;
123 EXPECT_TRUE(candidate->ToString(&sdp));
124 // Give the user a chance to modify sdp for testing.
125 SignalOnIceCandidateCreated(&sdp);
126 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
127 sdp);
128}
129
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000130void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700131 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000132 SignalOnDataChannel(data_channel);
133}
134
wu@webrtc.org364f2042013-11-20 21:49:41 +0000135void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000136 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700137 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000138 std::string sdp;
139 EXPECT_TRUE(desc->ToString(&sdp));
140
Mirko Bonadei675513b2017-11-09 11:09:25 +0100141 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": "
Steve Antona3a92c22017-12-07 10:27:41 -0800142 << webrtc::SdpTypeToString(desc->GetType())
143 << " sdp created: " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000144
145 // Give the user a chance to modify sdp for testing.
146 SignalOnSdpCreated(&sdp);
147
Steve Antona3a92c22017-12-07 10:27:41 -0800148 SetLocalDescription(desc->GetType(), sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000149
150 SignalOnSdpReady(sdp);
151}
152
153void PeerConnectionTestWrapper::CreateOffer(
Niels Möllerf06f9232018-08-07 12:32:18 +0200154 const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100155 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer.";
Niels Möllerf06f9232018-08-07 12:32:18 +0200156 peer_connection_->CreateOffer(this, options);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000157}
158
159void PeerConnectionTestWrapper::CreateAnswer(
Niels Möllerf06f9232018-08-07 12:32:18 +0200160 const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100161 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
162 << ": CreateAnswer.";
Niels Möllerf06f9232018-08-07 12:32:18 +0200163 peer_connection_->CreateAnswer(this, options);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000164}
165
166void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800167 SetRemoteDescription(SdpType::kOffer, sdp);
Niels Möllerf06f9232018-08-07 12:32:18 +0200168 CreateAnswer(webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000169}
170
171void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800172 SetRemoteDescription(SdpType::kAnswer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000173}
174
Steve Antona3a92c22017-12-07 10:27:41 -0800175void PeerConnectionTestWrapper::SetLocalDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000176 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100177 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800178 << ": SetLocalDescription " << webrtc::SdpTypeToString(type)
179 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000180
Yves Gerey665174f2018-06-19 15:03:05 +0200181 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
182 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000183 peer_connection_->SetLocalDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800184 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000185}
186
Steve Antona3a92c22017-12-07 10:27:41 -0800187void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000188 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100189 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800190 << ": SetRemoteDescription " << webrtc::SdpTypeToString(type)
191 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000192
Yves Gerey665174f2018-06-19 15:03:05 +0200193 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
194 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000195 peer_connection_->SetRemoteDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800196 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000197}
198
199void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
200 int sdp_mline_index,
201 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700202 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000203 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
204 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000205}
206
207void PeerConnectionTestWrapper::WaitForCallEstablished() {
208 WaitForConnection();
209 WaitForAudio();
210 WaitForVideo();
211}
212
213void PeerConnectionTestWrapper::WaitForConnection() {
214 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100215 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000216}
217
218bool PeerConnectionTestWrapper::CheckForConnection() {
219 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000220 PeerConnectionInterface::kIceConnectionConnected) ||
221 (peer_connection_->ice_connection_state() ==
222 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000223}
224
225void PeerConnectionTestWrapper::WaitForAudio() {
226 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100227 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
228 << ": Got enough audio frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000229}
230
231bool PeerConnectionTestWrapper::CheckForAudio() {
232 return (fake_audio_capture_module_->frames_received() >=
233 kTestAudioFrameCount);
234}
235
236void PeerConnectionTestWrapper::WaitForVideo() {
237 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100238 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
239 << ": Got enough video frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000240}
241
242bool PeerConnectionTestWrapper::CheckForVideo() {
243 if (!renderer_) {
244 return false;
245 }
246 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
247}
248
249void PeerConnectionTestWrapper::GetAndAddUserMedia(
Yves Gerey665174f2018-06-19 15:03:05 +0200250 bool audio,
251 const cricket::AudioOptions& audio_options,
252 bool video,
253 const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000254 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
Niels Möller2d02e082018-05-21 11:23:35 +0200255 GetUserMedia(audio, audio_options, video, video_constraints);
Steve Anton191c39f2018-01-24 19:35:55 -0800256 for (auto audio_track : stream->GetAudioTracks()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700257 EXPECT_TRUE(peer_connection_->AddTrack(audio_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-24 19:35:55 -0800258 }
259 for (auto video_track : stream->GetVideoTracks()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700260 EXPECT_TRUE(peer_connection_->AddTrack(video_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-24 19:35:55 -0800261 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000262}
263
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000264rtc::scoped_refptr<webrtc::MediaStreamInterface>
Niels Möller2d02e082018-05-21 11:23:35 +0200265PeerConnectionTestWrapper::GetUserMedia(
Yves Gerey665174f2018-06-19 15:03:05 +0200266 bool audio,
267 const cricket::AudioOptions& audio_options,
268 bool video,
269 const webrtc::FakeConstraints& video_constraints) {
Seth Hampson845e8782018-03-02 11:34:10 -0800270 std::string stream_id =
271 kStreamIdBase + rtc::ToString(num_get_user_media_calls_++);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
Seth Hampson845e8782018-03-02 11:34:10 -0800273 peer_connection_factory_->CreateLocalMediaStream(stream_id);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000274
275 if (audio) {
Niels Möller2d02e082018-05-21 11:23:35 +0200276 cricket::AudioOptions options = audio_options;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000277 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200278 options.highpass_filter = false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000279 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200280 peer_connection_factory_->CreateAudioSource(options);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000281 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000282 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
283 source));
284 stream->AddTrack(audio_track);
285 }
286
287 if (video) {
288 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
Niels Möllera1cc73f2018-05-28 16:20:42 +0200289 webrtc::FakePeriodicVideoSource::Config config;
290 config.frame_interval_ms = 100;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000291
perkja3ede6c2016-03-08 01:27:48 +0100292 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
Niels Möllera1cc73f2018-05-28 16:20:42 +0200293 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
294 config, /* remote */ false);
295
Seth Hampson845e8782018-03-02 11:34:10 -0800296 std::string videotrack_label = stream_id + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000297 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000298 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
299
300 stream->AddTrack(video_track);
301 }
302 return stream;
303}