blob: f1bf21d53f7c1c9b7968e2c6ca46b6fda95a212b [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000013#include "webrtc/base/gunit.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070014#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080015#include "webrtc/pc/test/fakeperiodicvideocapturer.h"
16#include "webrtc/pc/test/fakertccertificategenerator.h"
17#include "webrtc/pc/test/mockpeerconnectionobservers.h"
18#include "webrtc/pc/test/peerconnectiontestwrapper.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
danilchape9021a32016-05-17 01:52:02 -070050PeerConnectionTestWrapper::PeerConnectionTestWrapper(
51 const std::string& name,
52 rtc::Thread* network_thread,
53 rtc::Thread* worker_thread)
54 : name_(name),
55 network_thread_(network_thread),
56 worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000057
58PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
59
60bool PeerConnectionTestWrapper::CreatePc(
zhihuang9763d562016-08-05 11:14:50 -070061 const MediaConstraintsInterface* constraints,
kwiberg9e5b11e2017-04-19 03:47:57 -070062 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
63 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
64 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
kwibergd1fe2812016-04-27 06:47:29 -070065 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -070066 new cricket::FakePortAllocator(network_thread_, nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000067
deadbeefee8c6d32015-08-13 14:27:18 -070068 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000069 if (fake_audio_capture_module_ == NULL) {
70 return false;
71 }
72
73 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -070074 network_thread_, worker_thread_, rtc::Thread::Current(),
kwiberg9e5b11e2017-04-19 03:47:57 -070075 fake_audio_capture_module_, audio_encoder_factory, audio_decoder_factory,
76 nullptr, nullptr);
wu@webrtc.org364f2042013-11-20 21:49:41 +000077 if (!peer_connection_factory_) {
78 return false;
79 }
80
Henrik Boströmd79599d2016-06-01 13:58:50 +020081 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator(
deadbeef1b54a5f2017-01-23 19:39:57 -080082 new FakeRTCCertificateGenerator());
Henrik Boströmd79599d2016-06-01 13:58:50 +020083 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
84 config, constraints, std::move(port_allocator), std::move(cert_generator),
85 this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000086
87 return peer_connection_.get() != NULL;
88}
89
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000091PeerConnectionTestWrapper::CreateDataChannel(
92 const std::string& label,
93 const webrtc::DataChannelInit& init) {
94 return peer_connection_->CreateDataChannel(label, &init);
95}
96
Taylor Brandstetter98cde262016-05-31 13:02:21 -070097void PeerConnectionTestWrapper::OnAddStream(
98 rtc::scoped_refptr<MediaStreamInterface> stream) {
wu@webrtc.org364f2042013-11-20 21:49:41 +000099 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
100 << ": OnAddStream";
101 // TODO(ronghuawu): support multiple streams.
102 if (stream->GetVideoTracks().size() > 0) {
103 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
104 }
105}
106
107void PeerConnectionTestWrapper::OnIceCandidate(
108 const IceCandidateInterface* candidate) {
109 std::string sdp;
110 EXPECT_TRUE(candidate->ToString(&sdp));
111 // Give the user a chance to modify sdp for testing.
112 SignalOnIceCandidateCreated(&sdp);
113 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
114 sdp);
115}
116
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000117void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700118 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000119 SignalOnDataChannel(data_channel);
120}
121
wu@webrtc.org364f2042013-11-20 21:49:41 +0000122void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000123 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700124 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000125 std::string sdp;
126 EXPECT_TRUE(desc->ToString(&sdp));
127
128 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
129 << ": " << desc->type() << " sdp created: " << sdp;
130
131 // Give the user a chance to modify sdp for testing.
132 SignalOnSdpCreated(&sdp);
133
134 SetLocalDescription(desc->type(), sdp);
135
136 SignalOnSdpReady(sdp);
137}
138
139void PeerConnectionTestWrapper::CreateOffer(
140 const MediaConstraintsInterface* constraints) {
141 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
142 << ": CreateOffer.";
143 peer_connection_->CreateOffer(this, constraints);
144}
145
146void PeerConnectionTestWrapper::CreateAnswer(
147 const MediaConstraintsInterface* constraints) {
148 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
149 << ": CreateAnswer.";
150 peer_connection_->CreateAnswer(this, constraints);
151}
152
153void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
154 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
155 CreateAnswer(NULL);
156}
157
158void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
159 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
160}
161
162void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
163 const std::string& sdp) {
164 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
165 << ": SetLocalDescription " << type << " " << sdp;
166
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000167 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
168 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000169 MockSetSessionDescriptionObserver>());
170 peer_connection_->SetLocalDescription(
171 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
172}
173
174void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
175 const std::string& sdp) {
176 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
177 << ": SetRemoteDescription " << type << " " << sdp;
178
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000179 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
180 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000181 MockSetSessionDescriptionObserver>());
182 peer_connection_->SetRemoteDescription(
183 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
184}
185
186void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
187 int sdp_mline_index,
188 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700189 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000190 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
191 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000192}
193
194void PeerConnectionTestWrapper::WaitForCallEstablished() {
195 WaitForConnection();
196 WaitForAudio();
197 WaitForVideo();
198}
199
200void PeerConnectionTestWrapper::WaitForConnection() {
201 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
202 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
203 << ": Connected.";
204}
205
206bool PeerConnectionTestWrapper::CheckForConnection() {
207 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000208 PeerConnectionInterface::kIceConnectionConnected) ||
209 (peer_connection_->ice_connection_state() ==
210 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000211}
212
213void PeerConnectionTestWrapper::WaitForAudio() {
214 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
215 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
216 << ": Got enough audio frames.";
217}
218
219bool PeerConnectionTestWrapper::CheckForAudio() {
220 return (fake_audio_capture_module_->frames_received() >=
221 kTestAudioFrameCount);
222}
223
224void PeerConnectionTestWrapper::WaitForVideo() {
225 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
226 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
227 << ": Got enough video frames.";
228}
229
230bool PeerConnectionTestWrapper::CheckForVideo() {
231 if (!renderer_) {
232 return false;
233 }
234 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
235}
236
237void PeerConnectionTestWrapper::GetAndAddUserMedia(
238 bool audio, const webrtc::FakeConstraints& audio_constraints,
239 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000240 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000241 GetUserMedia(audio, audio_constraints, video, video_constraints);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000242 EXPECT_TRUE(peer_connection_->AddStream(stream));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000243}
244
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000245rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000246 PeerConnectionTestWrapper::GetUserMedia(
247 bool audio, const webrtc::FakeConstraints& audio_constraints,
248 bool video, const webrtc::FakeConstraints& video_constraints) {
249 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000250 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000251 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000252 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000253 peer_connection_factory_->CreateLocalMediaStream(label);
254
255 if (audio) {
256 FakeConstraints constraints = audio_constraints;
257 // Disable highpass filter so that we can get all the test audio frames.
258 constraints.AddMandatory(
259 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000260 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000261 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000262 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000263 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
264 source));
265 stream->AddTrack(audio_track);
266 }
267
268 if (video) {
269 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
270 FakeConstraints constraints = video_constraints;
271 constraints.SetMandatoryMaxFrameRate(10);
272
perkja3ede6c2016-03-08 01:27:48 +0100273 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000274 peer_connection_factory_->CreateVideoSource(
deadbeef112b2e92017-02-10 20:13:37 -0800275 std::unique_ptr<cricket::VideoCapturer>(
276 new webrtc::FakePeriodicVideoCapturer()),
277 &constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000278 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000279 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000280 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
281
282 stream->AddTrack(video_track);
283 }
284 return stream;
285}