blob: b34d7f80c5ffbf4fce373df03625fd6e904a58a8 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "p2p/base/fakeportallocator.h"
Steve Antona3a92c22017-12-07 10:27:41 -080016#include "pc/sdputils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "pc/test/fakeperiodicvideocapturer.h"
18#include "pc/test/fakertccertificategenerator.h"
19#include "pc/test/mockpeerconnectionobservers.h"
20#include "pc/test/peerconnectiontestwrapper.h"
21#include "rtc_base/gunit.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000022
wu@webrtc.org364f2042013-11-20 21:49:41 +000023using webrtc::FakeConstraints;
24using webrtc::FakeVideoTrackRenderer;
25using webrtc::IceCandidateInterface;
26using webrtc::MediaConstraintsInterface;
27using webrtc::MediaStreamInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080028using webrtc::MediaStreamTrackInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000029using webrtc::MockSetSessionDescriptionObserver;
30using webrtc::PeerConnectionInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080031using webrtc::RtpReceiverInterface;
Steve Antona3a92c22017-12-07 10:27:41 -080032using webrtc::SdpType;
wu@webrtc.org364f2042013-11-20 21:49:41 +000033using webrtc::SessionDescriptionInterface;
34using webrtc::VideoTrackInterface;
35
Steve Antona3a92c22017-12-07 10:27:41 -080036namespace {
37const char kStreamLabelBase[] = "stream_label";
38const char kVideoTrackLabelBase[] = "video_track";
39const char kAudioTrackLabelBase[] = "audio_track";
40constexpr int kMaxWait = 10000;
41constexpr int kTestAudioFrameCount = 3;
42constexpr int kTestVideoFrameCount = 3;
43} // namespace
44
wu@webrtc.org364f2042013-11-20 21:49:41 +000045void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
46 PeerConnectionTestWrapper* callee) {
47 caller->SignalOnIceCandidateReady.connect(
48 callee, &PeerConnectionTestWrapper::AddIceCandidate);
49 callee->SignalOnIceCandidateReady.connect(
50 caller, &PeerConnectionTestWrapper::AddIceCandidate);
51
52 caller->SignalOnSdpReady.connect(
53 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
54 callee->SignalOnSdpReady.connect(
55 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
56}
57
danilchape9021a32016-05-17 01:52:02 -070058PeerConnectionTestWrapper::PeerConnectionTestWrapper(
59 const std::string& name,
60 rtc::Thread* network_thread,
61 rtc::Thread* worker_thread)
62 : name_(name),
63 network_thread_(network_thread),
64 worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000065
66PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
67
68bool PeerConnectionTestWrapper::CreatePc(
zhihuang9763d562016-08-05 11:14:50 -070069 const MediaConstraintsInterface* constraints,
kwiberg9e5b11e2017-04-19 03:47:57 -070070 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
71 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
72 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
kwibergd1fe2812016-04-27 06:47:29 -070073 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -070074 new cricket::FakePortAllocator(network_thread_, nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000075
deadbeefee8c6d32015-08-13 14:27:18 -070076 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000077 if (fake_audio_capture_module_ == NULL) {
78 return false;
79 }
80
81 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -070082 network_thread_, worker_thread_, rtc::Thread::Current(),
kwiberg9e5b11e2017-04-19 03:47:57 -070083 fake_audio_capture_module_, audio_encoder_factory, audio_decoder_factory,
84 nullptr, nullptr);
wu@webrtc.org364f2042013-11-20 21:49:41 +000085 if (!peer_connection_factory_) {
86 return false;
87 }
88
Henrik Boströmd79599d2016-06-01 13:58:50 +020089 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator(
deadbeef1b54a5f2017-01-23 19:39:57 -080090 new FakeRTCCertificateGenerator());
Henrik Boströmd79599d2016-06-01 13:58:50 +020091 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
92 config, constraints, std::move(port_allocator), std::move(cert_generator),
93 this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000094
95 return peer_connection_.get() != NULL;
96}
97
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000099PeerConnectionTestWrapper::CreateDataChannel(
100 const std::string& label,
101 const webrtc::DataChannelInit& init) {
102 return peer_connection_->CreateDataChannel(label, &init);
103}
104
Steve Anton191c39f2018-01-24 19:35:55 -0800105void PeerConnectionTestWrapper::OnAddTrack(
106 rtc::scoped_refptr<RtpReceiverInterface> receiver,
107 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
108 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": OnAddTrack";
109 if (receiver->track()->kind() == MediaStreamTrackInterface::kVideoKind) {
110 auto* video_track =
111 static_cast<VideoTrackInterface*>(receiver->track().get());
112 renderer_ = rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000113 }
114}
115
116void PeerConnectionTestWrapper::OnIceCandidate(
117 const IceCandidateInterface* candidate) {
118 std::string sdp;
119 EXPECT_TRUE(candidate->ToString(&sdp));
120 // Give the user a chance to modify sdp for testing.
121 SignalOnIceCandidateCreated(&sdp);
122 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
123 sdp);
124}
125
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000126void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700127 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000128 SignalOnDataChannel(data_channel);
129}
130
wu@webrtc.org364f2042013-11-20 21:49:41 +0000131void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000132 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700133 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000134 std::string sdp;
135 EXPECT_TRUE(desc->ToString(&sdp));
136
Mirko Bonadei675513b2017-11-09 11:09:25 +0100137 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": "
Steve Antona3a92c22017-12-07 10:27:41 -0800138 << webrtc::SdpTypeToString(desc->GetType())
139 << " sdp created: " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000140
141 // Give the user a chance to modify sdp for testing.
142 SignalOnSdpCreated(&sdp);
143
Steve Antona3a92c22017-12-07 10:27:41 -0800144 SetLocalDescription(desc->GetType(), sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000145
146 SignalOnSdpReady(sdp);
147}
148
149void PeerConnectionTestWrapper::CreateOffer(
150 const MediaConstraintsInterface* constraints) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100151 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000152 peer_connection_->CreateOffer(this, constraints);
153}
154
155void PeerConnectionTestWrapper::CreateAnswer(
156 const MediaConstraintsInterface* constraints) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100157 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
158 << ": CreateAnswer.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000159 peer_connection_->CreateAnswer(this, constraints);
160}
161
162void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800163 SetRemoteDescription(SdpType::kOffer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000164 CreateAnswer(NULL);
165}
166
167void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800168 SetRemoteDescription(SdpType::kAnswer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000169}
170
Steve Antona3a92c22017-12-07 10:27:41 -0800171void PeerConnectionTestWrapper::SetLocalDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000172 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100173 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800174 << ": SetLocalDescription " << webrtc::SdpTypeToString(type)
175 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000176
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000177 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
178 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000179 MockSetSessionDescriptionObserver>());
180 peer_connection_->SetLocalDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800181 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000182}
183
Steve Antona3a92c22017-12-07 10:27:41 -0800184void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000185 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100186 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800187 << ": SetRemoteDescription " << webrtc::SdpTypeToString(type)
188 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000189
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000190 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
191 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000192 MockSetSessionDescriptionObserver>());
193 peer_connection_->SetRemoteDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800194 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000195}
196
197void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
198 int sdp_mline_index,
199 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700200 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000201 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
202 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000203}
204
205void PeerConnectionTestWrapper::WaitForCallEstablished() {
206 WaitForConnection();
207 WaitForAudio();
208 WaitForVideo();
209}
210
211void PeerConnectionTestWrapper::WaitForConnection() {
212 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100213 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000214}
215
216bool PeerConnectionTestWrapper::CheckForConnection() {
217 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000218 PeerConnectionInterface::kIceConnectionConnected) ||
219 (peer_connection_->ice_connection_state() ==
220 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000221}
222
223void PeerConnectionTestWrapper::WaitForAudio() {
224 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100225 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
226 << ": Got enough audio frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000227}
228
229bool PeerConnectionTestWrapper::CheckForAudio() {
230 return (fake_audio_capture_module_->frames_received() >=
231 kTestAudioFrameCount);
232}
233
234void PeerConnectionTestWrapper::WaitForVideo() {
235 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100236 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
237 << ": Got enough video frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000238}
239
240bool PeerConnectionTestWrapper::CheckForVideo() {
241 if (!renderer_) {
242 return false;
243 }
244 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
245}
246
247void PeerConnectionTestWrapper::GetAndAddUserMedia(
248 bool audio, const webrtc::FakeConstraints& audio_constraints,
249 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000250 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000251 GetUserMedia(audio, audio_constraints, video, video_constraints);
Steve Anton191c39f2018-01-24 19:35:55 -0800252 for (auto audio_track : stream->GetAudioTracks()) {
253 EXPECT_TRUE(
254 peer_connection_->AddTrack(audio_track, {stream->label()}).ok());
255 }
256 for (auto video_track : stream->GetVideoTracks()) {
257 EXPECT_TRUE(
258 peer_connection_->AddTrack(video_track, {stream->label()}).ok());
259 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000260}
261
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000262rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000263 PeerConnectionTestWrapper::GetUserMedia(
264 bool audio, const webrtc::FakeConstraints& audio_constraints,
265 bool video, const webrtc::FakeConstraints& video_constraints) {
266 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000268 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000269 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000270 peer_connection_factory_->CreateLocalMediaStream(label);
271
272 if (audio) {
273 FakeConstraints constraints = audio_constraints;
274 // Disable highpass filter so that we can get all the test audio frames.
275 constraints.AddMandatory(
276 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000278 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000279 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000280 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
281 source));
282 stream->AddTrack(audio_track);
283 }
284
285 if (video) {
286 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
287 FakeConstraints constraints = video_constraints;
288 constraints.SetMandatoryMaxFrameRate(10);
289
perkja3ede6c2016-03-08 01:27:48 +0100290 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000291 peer_connection_factory_->CreateVideoSource(
deadbeef112b2e92017-02-10 20:13:37 -0800292 std::unique_ptr<cricket::VideoCapturer>(
293 new webrtc::FakePeriodicVideoCapturer()),
294 &constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000295 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000296 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000297 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
298
299 stream->AddTrack(video_track);
300 }
301 return stream;
302}