blob: 032044c36ef9e264142c36786f36a0632baadbff [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
kwiberg0eb15ed2015-12-17 03:04:15 -080028#include <utility>
29
wu@webrtc.org364f2042013-11-20 21:49:41 +000030#include "talk/app/webrtc/fakeportallocatorfactory.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020031#include "talk/app/webrtc/test/fakedtlsidentitystore.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000032#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
33#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
34#include "talk/app/webrtc/test/peerconnectiontestwrapper.h"
35#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/gunit.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000037
38static const char kStreamLabelBase[] = "stream_label";
39static const char kVideoTrackLabelBase[] = "video_track";
40static const char kAudioTrackLabelBase[] = "audio_track";
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000041static const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000042static const int kTestAudioFrameCount = 3;
43static const int kTestVideoFrameCount = 3;
44
45using webrtc::FakeConstraints;
46using webrtc::FakeVideoTrackRenderer;
47using webrtc::IceCandidateInterface;
48using webrtc::MediaConstraintsInterface;
49using webrtc::MediaStreamInterface;
50using webrtc::MockSetSessionDescriptionObserver;
51using webrtc::PeerConnectionInterface;
52using webrtc::SessionDescriptionInterface;
53using webrtc::VideoTrackInterface;
54
55void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
56 PeerConnectionTestWrapper* callee) {
57 caller->SignalOnIceCandidateReady.connect(
58 callee, &PeerConnectionTestWrapper::AddIceCandidate);
59 callee->SignalOnIceCandidateReady.connect(
60 caller, &PeerConnectionTestWrapper::AddIceCandidate);
61
62 caller->SignalOnSdpReady.connect(
63 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
64 callee->SignalOnSdpReady.connect(
65 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
66}
67
68PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name)
69 : name_(name) {}
70
71PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
72
73bool PeerConnectionTestWrapper::CreatePc(
74 const MediaConstraintsInterface* constraints) {
75 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
76 if (!allocator_factory_) {
77 return false;
78 }
79
deadbeefee8c6d32015-08-13 14:27:18 -070080 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000081 if (fake_audio_capture_module_ == NULL) {
82 return false;
83 }
84
85 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000086 rtc::Thread::Current(), rtc::Thread::Current(),
wu@webrtc.org364f2042013-11-20 21:49:41 +000087 fake_audio_capture_module_, NULL, NULL);
88 if (!peer_connection_factory_) {
89 return false;
90 }
91
92 // CreatePeerConnection with IceServers.
93 webrtc::PeerConnectionInterface::IceServers ice_servers;
94 webrtc::PeerConnectionInterface::IceServer ice_server;
95 ice_server.uri = "stun:stun.l.google.com:19302";
96 ice_servers.push_back(ice_server);
Henrik Boström5e56c592015-08-11 10:33:13 +020097 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
Henrik Boström5e56c592015-08-11 10:33:13 +020099 new FakeDtlsIdentityStore() : nullptr);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000100 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200101 ice_servers, constraints, allocator_factory_.get(),
kwiberg0eb15ed2015-12-17 03:04:15 -0800102 std::move(dtls_identity_store), this);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000103
104 return peer_connection_.get() != NULL;
105}
106
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000108PeerConnectionTestWrapper::CreateDataChannel(
109 const std::string& label,
110 const webrtc::DataChannelInit& init) {
111 return peer_connection_->CreateDataChannel(label, &init);
112}
113
wu@webrtc.org364f2042013-11-20 21:49:41 +0000114void PeerConnectionTestWrapper::OnAddStream(MediaStreamInterface* stream) {
115 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
116 << ": OnAddStream";
117 // TODO(ronghuawu): support multiple streams.
118 if (stream->GetVideoTracks().size() > 0) {
119 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
120 }
121}
122
123void PeerConnectionTestWrapper::OnIceCandidate(
124 const IceCandidateInterface* candidate) {
125 std::string sdp;
126 EXPECT_TRUE(candidate->ToString(&sdp));
127 // Give the user a chance to modify sdp for testing.
128 SignalOnIceCandidateCreated(&sdp);
129 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
130 sdp);
131}
132
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000133void PeerConnectionTestWrapper::OnDataChannel(
134 webrtc::DataChannelInterface* data_channel) {
135 SignalOnDataChannel(data_channel);
136}
137
wu@webrtc.org364f2042013-11-20 21:49:41 +0000138void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000139 // This callback should take the ownership of |desc|.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000140 rtc::scoped_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000141 std::string sdp;
142 EXPECT_TRUE(desc->ToString(&sdp));
143
144 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
145 << ": " << desc->type() << " sdp created: " << sdp;
146
147 // Give the user a chance to modify sdp for testing.
148 SignalOnSdpCreated(&sdp);
149
150 SetLocalDescription(desc->type(), sdp);
151
152 SignalOnSdpReady(sdp);
153}
154
155void PeerConnectionTestWrapper::CreateOffer(
156 const MediaConstraintsInterface* constraints) {
157 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
158 << ": CreateOffer.";
159 peer_connection_->CreateOffer(this, constraints);
160}
161
162void PeerConnectionTestWrapper::CreateAnswer(
163 const MediaConstraintsInterface* constraints) {
164 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
165 << ": CreateAnswer.";
166 peer_connection_->CreateAnswer(this, constraints);
167}
168
169void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
170 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
171 CreateAnswer(NULL);
172}
173
174void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
175 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
176}
177
178void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
179 const std::string& sdp) {
180 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
181 << ": SetLocalDescription " << type << " " << sdp;
182
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
184 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000185 MockSetSessionDescriptionObserver>());
186 peer_connection_->SetLocalDescription(
187 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
188}
189
190void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
191 const std::string& sdp) {
192 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
193 << ": SetRemoteDescription " << type << " " << sdp;
194
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000195 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
196 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000197 MockSetSessionDescriptionObserver>());
198 peer_connection_->SetRemoteDescription(
199 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
200}
201
202void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
203 int sdp_mline_index,
204 const std::string& candidate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000205 rtc::scoped_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000206 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
207 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000208}
209
210void PeerConnectionTestWrapper::WaitForCallEstablished() {
211 WaitForConnection();
212 WaitForAudio();
213 WaitForVideo();
214}
215
216void PeerConnectionTestWrapper::WaitForConnection() {
217 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
218 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
219 << ": Connected.";
220}
221
222bool PeerConnectionTestWrapper::CheckForConnection() {
223 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000224 PeerConnectionInterface::kIceConnectionConnected) ||
225 (peer_connection_->ice_connection_state() ==
226 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000227}
228
229void PeerConnectionTestWrapper::WaitForAudio() {
230 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
231 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
232 << ": Got enough audio frames.";
233}
234
235bool PeerConnectionTestWrapper::CheckForAudio() {
236 return (fake_audio_capture_module_->frames_received() >=
237 kTestAudioFrameCount);
238}
239
240void PeerConnectionTestWrapper::WaitForVideo() {
241 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
242 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
243 << ": Got enough video frames.";
244}
245
246bool PeerConnectionTestWrapper::CheckForVideo() {
247 if (!renderer_) {
248 return false;
249 }
250 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
251}
252
253void PeerConnectionTestWrapper::GetAndAddUserMedia(
254 bool audio, const webrtc::FakeConstraints& audio_constraints,
255 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000257 GetUserMedia(audio, audio_constraints, video, video_constraints);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000258 EXPECT_TRUE(peer_connection_->AddStream(stream));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000259}
260
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000261rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000262 PeerConnectionTestWrapper::GetUserMedia(
263 bool audio, const webrtc::FakeConstraints& audio_constraints,
264 bool video, const webrtc::FakeConstraints& video_constraints) {
265 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000267 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000268 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000269 peer_connection_factory_->CreateLocalMediaStream(label);
270
271 if (audio) {
272 FakeConstraints constraints = audio_constraints;
273 // Disable highpass filter so that we can get all the test audio frames.
274 constraints.AddMandatory(
275 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000276 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000277 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000278 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000279 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
280 source));
281 stream->AddTrack(audio_track);
282 }
283
284 if (video) {
285 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
286 FakeConstraints constraints = video_constraints;
287 constraints.SetMandatoryMaxFrameRate(10);
288
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000290 peer_connection_factory_->CreateVideoSource(
291 new webrtc::FakePeriodicVideoCapturer(), &constraints);
292 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000294 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
295
296 stream->AddTrack(video_track);
297 }
298 return stream;
299}