blob: 91d4f508f77b9bbe191079b6cb257069e3bc332d [file] [log] [blame]
wu@webrtc.org364f2042013-11-20 21:49:41 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2013 Google Inc.
wu@webrtc.org364f2042013-11-20 21:49:41 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/fakeportallocatorfactory.h"
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +000029#include "talk/app/webrtc/test/fakedtlsidentityservice.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000030#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
31#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
32#include "talk/app/webrtc/test/peerconnectiontestwrapper.h"
33#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/gunit.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000035
36static const char kStreamLabelBase[] = "stream_label";
37static const char kVideoTrackLabelBase[] = "video_track";
38static const char kAudioTrackLabelBase[] = "audio_track";
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000039static const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000040static const int kTestAudioFrameCount = 3;
41static const int kTestVideoFrameCount = 3;
42
43using webrtc::FakeConstraints;
44using webrtc::FakeVideoTrackRenderer;
45using webrtc::IceCandidateInterface;
46using webrtc::MediaConstraintsInterface;
47using webrtc::MediaStreamInterface;
48using webrtc::MockSetSessionDescriptionObserver;
49using webrtc::PeerConnectionInterface;
50using webrtc::SessionDescriptionInterface;
51using webrtc::VideoTrackInterface;
52
53void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
54 PeerConnectionTestWrapper* callee) {
55 caller->SignalOnIceCandidateReady.connect(
56 callee, &PeerConnectionTestWrapper::AddIceCandidate);
57 callee->SignalOnIceCandidateReady.connect(
58 caller, &PeerConnectionTestWrapper::AddIceCandidate);
59
60 caller->SignalOnSdpReady.connect(
61 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
62 callee->SignalOnSdpReady.connect(
63 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
64}
65
66PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name)
67 : name_(name) {}
68
69PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
70
71bool PeerConnectionTestWrapper::CreatePc(
72 const MediaConstraintsInterface* constraints) {
73 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
74 if (!allocator_factory_) {
75 return false;
76 }
77
wu@webrtc.org364f2042013-11-20 21:49:41 +000078 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +000079 rtc::Thread::Current());
wu@webrtc.org364f2042013-11-20 21:49:41 +000080 if (fake_audio_capture_module_ == NULL) {
81 return false;
82 }
83
84 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 rtc::Thread::Current(), rtc::Thread::Current(),
wu@webrtc.org364f2042013-11-20 21:49:41 +000086 fake_audio_capture_module_, NULL, NULL);
87 if (!peer_connection_factory_) {
88 return false;
89 }
90
91 // CreatePeerConnection with IceServers.
92 webrtc::PeerConnectionInterface::IceServers ice_servers;
93 webrtc::PeerConnectionInterface::IceServer ice_server;
94 ice_server.uri = "stun:stun.l.google.com:19302";
95 ice_servers.push_back(ice_server);
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +000096 FakeIdentityService* dtls_service =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +000098 new FakeIdentityService() : NULL;
wu@webrtc.org364f2042013-11-20 21:49:41 +000099 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000100 ice_servers, constraints, allocator_factory_.get(), dtls_service, this);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000101
102 return peer_connection_.get() != NULL;
103}
104
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000105rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000106PeerConnectionTestWrapper::CreateDataChannel(
107 const std::string& label,
108 const webrtc::DataChannelInit& init) {
109 return peer_connection_->CreateDataChannel(label, &init);
110}
111
wu@webrtc.org364f2042013-11-20 21:49:41 +0000112void PeerConnectionTestWrapper::OnAddStream(MediaStreamInterface* stream) {
113 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
114 << ": OnAddStream";
115 // TODO(ronghuawu): support multiple streams.
116 if (stream->GetVideoTracks().size() > 0) {
117 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
118 }
119}
120
121void PeerConnectionTestWrapper::OnIceCandidate(
122 const IceCandidateInterface* candidate) {
123 std::string sdp;
124 EXPECT_TRUE(candidate->ToString(&sdp));
125 // Give the user a chance to modify sdp for testing.
126 SignalOnIceCandidateCreated(&sdp);
127 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
128 sdp);
129}
130
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000131void PeerConnectionTestWrapper::OnDataChannel(
132 webrtc::DataChannelInterface* data_channel) {
133 SignalOnDataChannel(data_channel);
134}
135
wu@webrtc.org364f2042013-11-20 21:49:41 +0000136void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000137 // This callback should take the ownership of |desc|.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000138 rtc::scoped_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000139 std::string sdp;
140 EXPECT_TRUE(desc->ToString(&sdp));
141
142 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
143 << ": " << desc->type() << " sdp created: " << sdp;
144
145 // Give the user a chance to modify sdp for testing.
146 SignalOnSdpCreated(&sdp);
147
148 SetLocalDescription(desc->type(), sdp);
149
150 SignalOnSdpReady(sdp);
151}
152
153void PeerConnectionTestWrapper::CreateOffer(
154 const MediaConstraintsInterface* constraints) {
155 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
156 << ": CreateOffer.";
157 peer_connection_->CreateOffer(this, constraints);
158}
159
160void PeerConnectionTestWrapper::CreateAnswer(
161 const MediaConstraintsInterface* constraints) {
162 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
163 << ": CreateAnswer.";
164 peer_connection_->CreateAnswer(this, constraints);
165}
166
167void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
168 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
169 CreateAnswer(NULL);
170}
171
172void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
173 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
174}
175
176void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
177 const std::string& sdp) {
178 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
179 << ": SetLocalDescription " << type << " " << sdp;
180
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000181 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
182 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000183 MockSetSessionDescriptionObserver>());
184 peer_connection_->SetLocalDescription(
185 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
186}
187
188void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
189 const std::string& sdp) {
190 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
191 << ": SetRemoteDescription " << type << " " << sdp;
192
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000193 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
194 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000195 MockSetSessionDescriptionObserver>());
196 peer_connection_->SetRemoteDescription(
197 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
198}
199
200void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
201 int sdp_mline_index,
202 const std::string& candidate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000203 rtc::scoped_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000204 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
205 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000206}
207
208void PeerConnectionTestWrapper::WaitForCallEstablished() {
209 WaitForConnection();
210 WaitForAudio();
211 WaitForVideo();
212}
213
214void PeerConnectionTestWrapper::WaitForConnection() {
215 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
216 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
217 << ": Connected.";
218}
219
220bool PeerConnectionTestWrapper::CheckForConnection() {
221 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000222 PeerConnectionInterface::kIceConnectionConnected) ||
223 (peer_connection_->ice_connection_state() ==
224 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000225}
226
227void PeerConnectionTestWrapper::WaitForAudio() {
228 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
229 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
230 << ": Got enough audio frames.";
231}
232
233bool PeerConnectionTestWrapper::CheckForAudio() {
234 return (fake_audio_capture_module_->frames_received() >=
235 kTestAudioFrameCount);
236}
237
238void PeerConnectionTestWrapper::WaitForVideo() {
239 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
240 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
241 << ": Got enough video frames.";
242}
243
244bool PeerConnectionTestWrapper::CheckForVideo() {
245 if (!renderer_) {
246 return false;
247 }
248 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
249}
250
251void PeerConnectionTestWrapper::GetAndAddUserMedia(
252 bool audio, const webrtc::FakeConstraints& audio_constraints,
253 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000254 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000255 GetUserMedia(audio, audio_constraints, video, video_constraints);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000256 EXPECT_TRUE(peer_connection_->AddStream(stream));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000257}
258
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000259rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000260 PeerConnectionTestWrapper::GetUserMedia(
261 bool audio, const webrtc::FakeConstraints& audio_constraints,
262 bool video, const webrtc::FakeConstraints& video_constraints) {
263 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000264 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000265 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000267 peer_connection_factory_->CreateLocalMediaStream(label);
268
269 if (audio) {
270 FakeConstraints constraints = audio_constraints;
271 // Disable highpass filter so that we can get all the test audio frames.
272 constraints.AddMandatory(
273 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000274 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000275 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000276 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000277 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
278 source));
279 stream->AddTrack(audio_track);
280 }
281
282 if (video) {
283 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
284 FakeConstraints constraints = video_constraints;
285 constraints.SetMandatoryMaxFrameRate(10);
286
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000288 peer_connection_factory_->CreateVideoSource(
289 new webrtc::FakePeriodicVideoCapturer(), &constraints);
290 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000291 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000292 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
293
294 stream->AddTrack(video_track);
295 }
296 return stream;
297}