blob: 1ec2b4703ec4032fafe6d9e59b65797b9d5cc565 [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 Brandstettera1c30352016-05-13 08:15:11 -070018#include "webrtc/p2p/base/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
perkj57db6522016-04-08 08:16:33 -070050PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name,
51 rtc::Thread* worker_thread)
52 : name_(name), worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000053
54PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
55
56bool PeerConnectionTestWrapper::CreatePc(
57 const MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -070058 std::unique_ptr<cricket::PortAllocator> port_allocator(
perkj57db6522016-04-08 08:16:33 -070059 new cricket::FakePortAllocator(worker_thread_, nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000060
deadbeefee8c6d32015-08-13 14:27:18 -070061 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000062 if (fake_audio_capture_module_ == NULL) {
63 return false;
64 }
65
66 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
perkj57db6522016-04-08 08:16:33 -070067 worker_thread_, rtc::Thread::Current(), fake_audio_capture_module_, NULL,
68 NULL);
wu@webrtc.org364f2042013-11-20 21:49:41 +000069 if (!peer_connection_factory_) {
70 return false;
71 }
72
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080073 // CreatePeerConnection with RTCConfiguration.
74 webrtc::PeerConnectionInterface::RTCConfiguration config;
wu@webrtc.org364f2042013-11-20 21:49:41 +000075 webrtc::PeerConnectionInterface::IceServer ice_server;
76 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080077 config.servers.push_back(ice_server);
kwibergd1fe2812016-04-27 06:47:29 -070078 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
79 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
80 : nullptr);
wu@webrtc.org364f2042013-11-20 21:49:41 +000081 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080082 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -080083 std::move(dtls_identity_store), this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000084
85 return peer_connection_.get() != NULL;
86}
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000089PeerConnectionTestWrapper::CreateDataChannel(
90 const std::string& label,
91 const webrtc::DataChannelInit& init) {
92 return peer_connection_->CreateDataChannel(label, &init);
93}
94
wu@webrtc.org364f2042013-11-20 21:49:41 +000095void PeerConnectionTestWrapper::OnAddStream(MediaStreamInterface* stream) {
96 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
97 << ": OnAddStream";
98 // TODO(ronghuawu): support multiple streams.
99 if (stream->GetVideoTracks().size() > 0) {
100 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
101 }
102}
103
104void PeerConnectionTestWrapper::OnIceCandidate(
105 const IceCandidateInterface* candidate) {
106 std::string sdp;
107 EXPECT_TRUE(candidate->ToString(&sdp));
108 // Give the user a chance to modify sdp for testing.
109 SignalOnIceCandidateCreated(&sdp);
110 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
111 sdp);
112}
113
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000114void PeerConnectionTestWrapper::OnDataChannel(
115 webrtc::DataChannelInterface* data_channel) {
116 SignalOnDataChannel(data_channel);
117}
118
wu@webrtc.org364f2042013-11-20 21:49:41 +0000119void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000120 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700121 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000122 std::string sdp;
123 EXPECT_TRUE(desc->ToString(&sdp));
124
125 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
126 << ": " << desc->type() << " sdp created: " << sdp;
127
128 // Give the user a chance to modify sdp for testing.
129 SignalOnSdpCreated(&sdp);
130
131 SetLocalDescription(desc->type(), sdp);
132
133 SignalOnSdpReady(sdp);
134}
135
136void PeerConnectionTestWrapper::CreateOffer(
137 const MediaConstraintsInterface* constraints) {
138 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
139 << ": CreateOffer.";
140 peer_connection_->CreateOffer(this, constraints);
141}
142
143void PeerConnectionTestWrapper::CreateAnswer(
144 const MediaConstraintsInterface* constraints) {
145 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
146 << ": CreateAnswer.";
147 peer_connection_->CreateAnswer(this, constraints);
148}
149
150void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
151 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
152 CreateAnswer(NULL);
153}
154
155void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
156 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
157}
158
159void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
160 const std::string& sdp) {
161 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
162 << ": SetLocalDescription " << type << " " << sdp;
163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
165 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000166 MockSetSessionDescriptionObserver>());
167 peer_connection_->SetLocalDescription(
168 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
169}
170
171void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
172 const std::string& sdp) {
173 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
174 << ": SetRemoteDescription " << type << " " << sdp;
175
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
177 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000178 MockSetSessionDescriptionObserver>());
179 peer_connection_->SetRemoteDescription(
180 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
181}
182
183void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
184 int sdp_mline_index,
185 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700186 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000187 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
188 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000189}
190
191void PeerConnectionTestWrapper::WaitForCallEstablished() {
192 WaitForConnection();
193 WaitForAudio();
194 WaitForVideo();
195}
196
197void PeerConnectionTestWrapper::WaitForConnection() {
198 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
199 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
200 << ": Connected.";
201}
202
203bool PeerConnectionTestWrapper::CheckForConnection() {
204 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000205 PeerConnectionInterface::kIceConnectionConnected) ||
206 (peer_connection_->ice_connection_state() ==
207 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000208}
209
210void PeerConnectionTestWrapper::WaitForAudio() {
211 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
212 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
213 << ": Got enough audio frames.";
214}
215
216bool PeerConnectionTestWrapper::CheckForAudio() {
217 return (fake_audio_capture_module_->frames_received() >=
218 kTestAudioFrameCount);
219}
220
221void PeerConnectionTestWrapper::WaitForVideo() {
222 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
223 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
224 << ": Got enough video frames.";
225}
226
227bool PeerConnectionTestWrapper::CheckForVideo() {
228 if (!renderer_) {
229 return false;
230 }
231 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
232}
233
234void PeerConnectionTestWrapper::GetAndAddUserMedia(
235 bool audio, const webrtc::FakeConstraints& audio_constraints,
236 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000237 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000238 GetUserMedia(audio, audio_constraints, video, video_constraints);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000239 EXPECT_TRUE(peer_connection_->AddStream(stream));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000240}
241
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000242rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000243 PeerConnectionTestWrapper::GetUserMedia(
244 bool audio, const webrtc::FakeConstraints& audio_constraints,
245 bool video, const webrtc::FakeConstraints& video_constraints) {
246 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000247 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000248 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000249 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000250 peer_connection_factory_->CreateLocalMediaStream(label);
251
252 if (audio) {
253 FakeConstraints constraints = audio_constraints;
254 // Disable highpass filter so that we can get all the test audio frames.
255 constraints.AddMandatory(
256 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000258 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000259 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000260 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
261 source));
262 stream->AddTrack(audio_track);
263 }
264
265 if (video) {
266 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
267 FakeConstraints constraints = video_constraints;
268 constraints.SetMandatoryMaxFrameRate(10);
269
perkja3ede6c2016-03-08 01:27:48 +0100270 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000271 peer_connection_factory_->CreateVideoSource(
272 new webrtc::FakePeriodicVideoCapturer(), &constraints);
273 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000274 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000275 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
276
277 stream->AddTrack(video_track);
278 }
279 return stream;
280}