blob: aa2f79ea0af07eb25fa6ba08b3aeb82e15e94e07 [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>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/fakeportallocator.h"
Steve Antona3a92c22017-12-07 10:27:41 -080015#include "pc/sdputils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "pc/test/fakeperiodicvideocapturer.h"
17#include "pc/test/fakertccertificategenerator.h"
18#include "pc/test/mockpeerconnectionobservers.h"
19#include "pc/test/peerconnectiontestwrapper.h"
20#include "rtc_base/gunit.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000021
wu@webrtc.org364f2042013-11-20 21:49:41 +000022using webrtc::FakeConstraints;
23using webrtc::FakeVideoTrackRenderer;
24using webrtc::IceCandidateInterface;
25using webrtc::MediaConstraintsInterface;
26using webrtc::MediaStreamInterface;
27using webrtc::MockSetSessionDescriptionObserver;
28using webrtc::PeerConnectionInterface;
Steve Antona3a92c22017-12-07 10:27:41 -080029using webrtc::SdpType;
wu@webrtc.org364f2042013-11-20 21:49:41 +000030using webrtc::SessionDescriptionInterface;
31using webrtc::VideoTrackInterface;
32
Steve Antona3a92c22017-12-07 10:27:41 -080033namespace {
34const char kStreamLabelBase[] = "stream_label";
35const char kVideoTrackLabelBase[] = "video_track";
36const char kAudioTrackLabelBase[] = "audio_track";
37constexpr int kMaxWait = 10000;
38constexpr int kTestAudioFrameCount = 3;
39constexpr int kTestVideoFrameCount = 3;
40} // namespace
41
wu@webrtc.org364f2042013-11-20 21:49:41 +000042void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
43 PeerConnectionTestWrapper* callee) {
44 caller->SignalOnIceCandidateReady.connect(
45 callee, &PeerConnectionTestWrapper::AddIceCandidate);
46 callee->SignalOnIceCandidateReady.connect(
47 caller, &PeerConnectionTestWrapper::AddIceCandidate);
48
49 caller->SignalOnSdpReady.connect(
50 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
51 callee->SignalOnSdpReady.connect(
52 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
53}
54
danilchape9021a32016-05-17 01:52:02 -070055PeerConnectionTestWrapper::PeerConnectionTestWrapper(
56 const std::string& name,
57 rtc::Thread* network_thread,
58 rtc::Thread* worker_thread)
59 : name_(name),
60 network_thread_(network_thread),
61 worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000062
63PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
64
65bool PeerConnectionTestWrapper::CreatePc(
zhihuang9763d562016-08-05 11:14:50 -070066 const MediaConstraintsInterface* constraints,
kwiberg9e5b11e2017-04-19 03:47:57 -070067 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
68 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
69 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
kwibergd1fe2812016-04-27 06:47:29 -070070 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -070071 new cricket::FakePortAllocator(network_thread_, nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000072
deadbeefee8c6d32015-08-13 14:27:18 -070073 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000074 if (fake_audio_capture_module_ == NULL) {
75 return false;
76 }
77
78 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -070079 network_thread_, worker_thread_, rtc::Thread::Current(),
kwiberg9e5b11e2017-04-19 03:47:57 -070080 fake_audio_capture_module_, audio_encoder_factory, audio_decoder_factory,
81 nullptr, nullptr);
wu@webrtc.org364f2042013-11-20 21:49:41 +000082 if (!peer_connection_factory_) {
83 return false;
84 }
85
Henrik Boströmd79599d2016-06-01 13:58:50 +020086 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator(
deadbeef1b54a5f2017-01-23 19:39:57 -080087 new FakeRTCCertificateGenerator());
Henrik Boströmd79599d2016-06-01 13:58:50 +020088 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
89 config, constraints, std::move(port_allocator), std::move(cert_generator),
90 this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000091
92 return peer_connection_.get() != NULL;
93}
94
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000096PeerConnectionTestWrapper::CreateDataChannel(
97 const std::string& label,
98 const webrtc::DataChannelInit& init) {
99 return peer_connection_->CreateDataChannel(label, &init);
100}
101
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700102void PeerConnectionTestWrapper::OnAddStream(
103 rtc::scoped_refptr<MediaStreamInterface> stream) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": OnAddStream";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000105 // TODO(ronghuawu): support multiple streams.
106 if (stream->GetVideoTracks().size() > 0) {
107 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
108 }
109}
110
111void PeerConnectionTestWrapper::OnIceCandidate(
112 const IceCandidateInterface* candidate) {
113 std::string sdp;
114 EXPECT_TRUE(candidate->ToString(&sdp));
115 // Give the user a chance to modify sdp for testing.
116 SignalOnIceCandidateCreated(&sdp);
117 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
118 sdp);
119}
120
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000121void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700122 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000123 SignalOnDataChannel(data_channel);
124}
125
wu@webrtc.org364f2042013-11-20 21:49:41 +0000126void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000127 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700128 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000129 std::string sdp;
130 EXPECT_TRUE(desc->ToString(&sdp));
131
Mirko Bonadei675513b2017-11-09 11:09:25 +0100132 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": "
Steve Antona3a92c22017-12-07 10:27:41 -0800133 << webrtc::SdpTypeToString(desc->GetType())
134 << " sdp created: " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000135
136 // Give the user a chance to modify sdp for testing.
137 SignalOnSdpCreated(&sdp);
138
Steve Antona3a92c22017-12-07 10:27:41 -0800139 SetLocalDescription(desc->GetType(), sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000140
141 SignalOnSdpReady(sdp);
142}
143
144void PeerConnectionTestWrapper::CreateOffer(
145 const MediaConstraintsInterface* constraints) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100146 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000147 peer_connection_->CreateOffer(this, constraints);
148}
149
150void PeerConnectionTestWrapper::CreateAnswer(
151 const MediaConstraintsInterface* constraints) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100152 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
153 << ": CreateAnswer.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000154 peer_connection_->CreateAnswer(this, constraints);
155}
156
157void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800158 SetRemoteDescription(SdpType::kOffer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000159 CreateAnswer(NULL);
160}
161
162void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800163 SetRemoteDescription(SdpType::kAnswer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000164}
165
Steve Antona3a92c22017-12-07 10:27:41 -0800166void PeerConnectionTestWrapper::SetLocalDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000167 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100168 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800169 << ": SetLocalDescription " << webrtc::SdpTypeToString(type)
170 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000171
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000172 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
173 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000174 MockSetSessionDescriptionObserver>());
175 peer_connection_->SetLocalDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800176 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000177}
178
Steve Antona3a92c22017-12-07 10:27:41 -0800179void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000180 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800182 << ": SetRemoteDescription " << webrtc::SdpTypeToString(type)
183 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000184
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000185 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
186 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000187 MockSetSessionDescriptionObserver>());
188 peer_connection_->SetRemoteDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800189 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000190}
191
192void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
193 int sdp_mline_index,
194 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700195 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000196 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
197 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000198}
199
200void PeerConnectionTestWrapper::WaitForCallEstablished() {
201 WaitForConnection();
202 WaitForAudio();
203 WaitForVideo();
204}
205
206void PeerConnectionTestWrapper::WaitForConnection() {
207 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100208 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000209}
210
211bool PeerConnectionTestWrapper::CheckForConnection() {
212 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000213 PeerConnectionInterface::kIceConnectionConnected) ||
214 (peer_connection_->ice_connection_state() ==
215 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000216}
217
218void PeerConnectionTestWrapper::WaitForAudio() {
219 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100220 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
221 << ": Got enough audio frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000222}
223
224bool PeerConnectionTestWrapper::CheckForAudio() {
225 return (fake_audio_capture_module_->frames_received() >=
226 kTestAudioFrameCount);
227}
228
229void PeerConnectionTestWrapper::WaitForVideo() {
230 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100231 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
232 << ": Got enough video frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000233}
234
235bool PeerConnectionTestWrapper::CheckForVideo() {
236 if (!renderer_) {
237 return false;
238 }
239 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
240}
241
242void PeerConnectionTestWrapper::GetAndAddUserMedia(
243 bool audio, const webrtc::FakeConstraints& audio_constraints,
244 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000245 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000246 GetUserMedia(audio, audio_constraints, video, video_constraints);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000247 EXPECT_TRUE(peer_connection_->AddStream(stream));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000248}
249
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000250rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000251 PeerConnectionTestWrapper::GetUserMedia(
252 bool audio, const webrtc::FakeConstraints& audio_constraints,
253 bool video, const webrtc::FakeConstraints& video_constraints) {
254 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000255 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000256 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000258 peer_connection_factory_->CreateLocalMediaStream(label);
259
260 if (audio) {
261 FakeConstraints constraints = audio_constraints;
262 // Disable highpass filter so that we can get all the test audio frames.
263 constraints.AddMandatory(
264 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000266 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000268 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
269 source));
270 stream->AddTrack(audio_track);
271 }
272
273 if (video) {
274 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
275 FakeConstraints constraints = video_constraints;
276 constraints.SetMandatoryMaxFrameRate(10);
277
perkja3ede6c2016-03-08 01:27:48 +0100278 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000279 peer_connection_factory_->CreateVideoSource(
deadbeef112b2e92017-02-10 20:13:37 -0800280 std::unique_ptr<cricket::VideoCapturer>(
281 new webrtc::FakePeriodicVideoCapturer()),
282 &constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000283 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000285 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
286
287 stream->AddTrack(video_track);
288 }
289 return stream;
290}