blob: 2ff9b5d0092c24ac84629438111a0c996da70f01 [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
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
Henrik Kjellander15583c12016-02-10 10:53:12 +010013#include "webrtc/api/test/fakedtlsidentitystore.h"
14#include "webrtc/api/test/fakeperiodicvideocapturer.h"
15#include "webrtc/api/test/mockpeerconnectionobservers.h"
16#include "webrtc/api/test/peerconnectiontestwrapper.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/gunit.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080018#include "webrtc/p2p/client/fakeportallocator.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000019
20static const char kStreamLabelBase[] = "stream_label";
21static const char kVideoTrackLabelBase[] = "video_track";
22static const char kAudioTrackLabelBase[] = "audio_track";
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000023static const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000024static const int kTestAudioFrameCount = 3;
25static const int kTestVideoFrameCount = 3;
26
27using webrtc::FakeConstraints;
28using webrtc::FakeVideoTrackRenderer;
29using webrtc::IceCandidateInterface;
30using webrtc::MediaConstraintsInterface;
31using webrtc::MediaStreamInterface;
32using webrtc::MockSetSessionDescriptionObserver;
33using webrtc::PeerConnectionInterface;
34using webrtc::SessionDescriptionInterface;
35using webrtc::VideoTrackInterface;
36
37void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
38 PeerConnectionTestWrapper* callee) {
39 caller->SignalOnIceCandidateReady.connect(
40 callee, &PeerConnectionTestWrapper::AddIceCandidate);
41 callee->SignalOnIceCandidateReady.connect(
42 caller, &PeerConnectionTestWrapper::AddIceCandidate);
43
44 caller->SignalOnSdpReady.connect(
45 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
46 callee->SignalOnSdpReady.connect(
47 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
48}
49
50PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name)
51 : name_(name) {}
52
53PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
54
55bool PeerConnectionTestWrapper::CreatePc(
56 const MediaConstraintsInterface* constraints) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080057 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
58 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000059
deadbeefee8c6d32015-08-13 14:27:18 -070060 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000061 if (fake_audio_capture_module_ == NULL) {
62 return false;
63 }
64
65 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 rtc::Thread::Current(), rtc::Thread::Current(),
wu@webrtc.org364f2042013-11-20 21:49:41 +000067 fake_audio_capture_module_, NULL, NULL);
68 if (!peer_connection_factory_) {
69 return false;
70 }
71
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080072 // CreatePeerConnection with RTCConfiguration.
73 webrtc::PeerConnectionInterface::RTCConfiguration config;
wu@webrtc.org364f2042013-11-20 21:49:41 +000074 webrtc::PeerConnectionInterface::IceServer ice_server;
75 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080076 config.servers.push_back(ice_server);
Henrik Boström5e56c592015-08-11 10:33:13 +020077 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
Henrik Boström5e56c592015-08-11 10:33:13 +020079 new FakeDtlsIdentityStore() : nullptr);
wu@webrtc.org364f2042013-11-20 21:49:41 +000080 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080081 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -080082 std::move(dtls_identity_store), this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000083
84 return peer_connection_.get() != NULL;
85}
86
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000087rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000088PeerConnectionTestWrapper::CreateDataChannel(
89 const std::string& label,
90 const webrtc::DataChannelInit& init) {
91 return peer_connection_->CreateDataChannel(label, &init);
92}
93
wu@webrtc.org364f2042013-11-20 21:49:41 +000094void PeerConnectionTestWrapper::OnAddStream(MediaStreamInterface* stream) {
95 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
96 << ": OnAddStream";
97 // TODO(ronghuawu): support multiple streams.
98 if (stream->GetVideoTracks().size() > 0) {
99 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
100 }
101}
102
103void PeerConnectionTestWrapper::OnIceCandidate(
104 const IceCandidateInterface* candidate) {
105 std::string sdp;
106 EXPECT_TRUE(candidate->ToString(&sdp));
107 // Give the user a chance to modify sdp for testing.
108 SignalOnIceCandidateCreated(&sdp);
109 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
110 sdp);
111}
112
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000113void PeerConnectionTestWrapper::OnDataChannel(
114 webrtc::DataChannelInterface* data_channel) {
115 SignalOnDataChannel(data_channel);
116}
117
wu@webrtc.org364f2042013-11-20 21:49:41 +0000118void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000119 // This callback should take the ownership of |desc|.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000120 rtc::scoped_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000121 std::string sdp;
122 EXPECT_TRUE(desc->ToString(&sdp));
123
124 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
125 << ": " << desc->type() << " sdp created: " << sdp;
126
127 // Give the user a chance to modify sdp for testing.
128 SignalOnSdpCreated(&sdp);
129
130 SetLocalDescription(desc->type(), sdp);
131
132 SignalOnSdpReady(sdp);
133}
134
135void PeerConnectionTestWrapper::CreateOffer(
136 const MediaConstraintsInterface* constraints) {
137 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
138 << ": CreateOffer.";
139 peer_connection_->CreateOffer(this, constraints);
140}
141
142void PeerConnectionTestWrapper::CreateAnswer(
143 const MediaConstraintsInterface* constraints) {
144 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
145 << ": CreateAnswer.";
146 peer_connection_->CreateAnswer(this, constraints);
147}
148
149void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
150 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
151 CreateAnswer(NULL);
152}
153
154void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
155 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
156}
157
158void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
159 const std::string& sdp) {
160 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
161 << ": SetLocalDescription " << type << " " << sdp;
162
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000163 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
164 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000165 MockSetSessionDescriptionObserver>());
166 peer_connection_->SetLocalDescription(
167 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
168}
169
170void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
171 const std::string& sdp) {
172 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
173 << ": SetRemoteDescription " << type << " " << sdp;
174
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000175 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
176 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000177 MockSetSessionDescriptionObserver>());
178 peer_connection_->SetRemoteDescription(
179 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
180}
181
182void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
183 int sdp_mline_index,
184 const std::string& candidate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000185 rtc::scoped_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000186 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
187 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000188}
189
190void PeerConnectionTestWrapper::WaitForCallEstablished() {
191 WaitForConnection();
192 WaitForAudio();
193 WaitForVideo();
194}
195
196void PeerConnectionTestWrapper::WaitForConnection() {
197 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
198 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
199 << ": Connected.";
200}
201
202bool PeerConnectionTestWrapper::CheckForConnection() {
203 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000204 PeerConnectionInterface::kIceConnectionConnected) ||
205 (peer_connection_->ice_connection_state() ==
206 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000207}
208
209void PeerConnectionTestWrapper::WaitForAudio() {
210 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
211 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
212 << ": Got enough audio frames.";
213}
214
215bool PeerConnectionTestWrapper::CheckForAudio() {
216 return (fake_audio_capture_module_->frames_received() >=
217 kTestAudioFrameCount);
218}
219
220void PeerConnectionTestWrapper::WaitForVideo() {
221 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
222 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
223 << ": Got enough video frames.";
224}
225
226bool PeerConnectionTestWrapper::CheckForVideo() {
227 if (!renderer_) {
228 return false;
229 }
230 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
231}
232
233void PeerConnectionTestWrapper::GetAndAddUserMedia(
234 bool audio, const webrtc::FakeConstraints& audio_constraints,
235 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000236 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000237 GetUserMedia(audio, audio_constraints, video, video_constraints);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000238 EXPECT_TRUE(peer_connection_->AddStream(stream));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000239}
240
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000241rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000242 PeerConnectionTestWrapper::GetUserMedia(
243 bool audio, const webrtc::FakeConstraints& audio_constraints,
244 bool video, const webrtc::FakeConstraints& video_constraints) {
245 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000246 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000247 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000248 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000249 peer_connection_factory_->CreateLocalMediaStream(label);
250
251 if (audio) {
252 FakeConstraints constraints = audio_constraints;
253 // Disable highpass filter so that we can get all the test audio frames.
254 constraints.AddMandatory(
255 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000257 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000258 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000259 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
260 source));
261 stream->AddTrack(audio_track);
262 }
263
264 if (video) {
265 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
266 FakeConstraints constraints = video_constraints;
267 constraints.SetMandatoryMaxFrameRate(10);
268
perkja3ede6c2016-03-08 01:27:48 +0100269 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000270 peer_connection_factory_->CreateVideoSource(
271 new webrtc::FakePeriodicVideoCapturer(), &constraints);
272 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000273 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000274 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
275
276 stream->AddTrack(video_track);
277 }
278 return stream;
279}