blob: 6d28ef8111480076d1cc6cfadf5fe899cf7bdb22 [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
Steve Anton36b29d12017-10-30 09:57:42 -070011#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080012#include <utility>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "p2p/base/fakeportallocator.h"
15#include "pc/test/fakeperiodicvideocapturer.h"
16#include "pc/test/fakertccertificategenerator.h"
17#include "pc/test/mockpeerconnectionobservers.h"
18#include "pc/test/peerconnectiontestwrapper.h"
19#include "rtc_base/gunit.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000020
21static const char kStreamLabelBase[] = "stream_label";
22static const char kVideoTrackLabelBase[] = "video_track";
23static const char kAudioTrackLabelBase[] = "audio_track";
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000024static const int kMaxWait = 10000;
wu@webrtc.org364f2042013-11-20 21:49:41 +000025static const int kTestAudioFrameCount = 3;
26static const int kTestVideoFrameCount = 3;
27
28using webrtc::FakeConstraints;
29using webrtc::FakeVideoTrackRenderer;
30using webrtc::IceCandidateInterface;
31using webrtc::MediaConstraintsInterface;
32using webrtc::MediaStreamInterface;
33using webrtc::MockSetSessionDescriptionObserver;
34using webrtc::PeerConnectionInterface;
35using webrtc::SessionDescriptionInterface;
36using webrtc::VideoTrackInterface;
37
38void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
39 PeerConnectionTestWrapper* callee) {
40 caller->SignalOnIceCandidateReady.connect(
41 callee, &PeerConnectionTestWrapper::AddIceCandidate);
42 callee->SignalOnIceCandidateReady.connect(
43 caller, &PeerConnectionTestWrapper::AddIceCandidate);
44
45 caller->SignalOnSdpReady.connect(
46 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
47 callee->SignalOnSdpReady.connect(
48 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
49}
50
danilchape9021a32016-05-17 01:52:02 -070051PeerConnectionTestWrapper::PeerConnectionTestWrapper(
52 const std::string& name,
53 rtc::Thread* network_thread,
54 rtc::Thread* worker_thread)
55 : name_(name),
56 network_thread_(network_thread),
57 worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000058
59PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
60
61bool PeerConnectionTestWrapper::CreatePc(
zhihuang9763d562016-08-05 11:14:50 -070062 const MediaConstraintsInterface* constraints,
kwiberg9e5b11e2017-04-19 03:47:57 -070063 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
64 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
65 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
kwibergd1fe2812016-04-27 06:47:29 -070066 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -070067 new cricket::FakePortAllocator(network_thread_, nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000068
deadbeefee8c6d32015-08-13 14:27:18 -070069 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
wu@webrtc.org364f2042013-11-20 21:49:41 +000070 if (fake_audio_capture_module_ == NULL) {
71 return false;
72 }
73
74 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -070075 network_thread_, worker_thread_, rtc::Thread::Current(),
kwiberg9e5b11e2017-04-19 03:47:57 -070076 fake_audio_capture_module_, audio_encoder_factory, audio_decoder_factory,
77 nullptr, nullptr);
wu@webrtc.org364f2042013-11-20 21:49:41 +000078 if (!peer_connection_factory_) {
79 return false;
80 }
81
Henrik Boströmd79599d2016-06-01 13:58:50 +020082 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator(
deadbeef1b54a5f2017-01-23 19:39:57 -080083 new FakeRTCCertificateGenerator());
Henrik Boströmd79599d2016-06-01 13:58:50 +020084 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
85 config, constraints, std::move(port_allocator), std::move(cert_generator),
86 this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000087
88 return peer_connection_.get() != NULL;
89}
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +000092PeerConnectionTestWrapper::CreateDataChannel(
93 const std::string& label,
94 const webrtc::DataChannelInit& init) {
95 return peer_connection_->CreateDataChannel(label, &init);
96}
97
Taylor Brandstetter98cde262016-05-31 13:02:21 -070098void PeerConnectionTestWrapper::OnAddStream(
99 rtc::scoped_refptr<MediaStreamInterface> stream) {
wu@webrtc.org364f2042013-11-20 21:49:41 +0000100 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
101 << ": OnAddStream";
102 // TODO(ronghuawu): support multiple streams.
103 if (stream->GetVideoTracks().size() > 0) {
104 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
105 }
106}
107
108void PeerConnectionTestWrapper::OnIceCandidate(
109 const IceCandidateInterface* candidate) {
110 std::string sdp;
111 EXPECT_TRUE(candidate->ToString(&sdp));
112 // Give the user a chance to modify sdp for testing.
113 SignalOnIceCandidateCreated(&sdp);
114 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
115 sdp);
116}
117
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000118void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700119 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000120 SignalOnDataChannel(data_channel);
121}
122
wu@webrtc.org364f2042013-11-20 21:49:41 +0000123void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000124 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700125 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000126 std::string sdp;
127 EXPECT_TRUE(desc->ToString(&sdp));
128
129 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
130 << ": " << desc->type() << " sdp created: " << sdp;
131
132 // Give the user a chance to modify sdp for testing.
133 SignalOnSdpCreated(&sdp);
134
135 SetLocalDescription(desc->type(), sdp);
136
137 SignalOnSdpReady(sdp);
138}
139
140void PeerConnectionTestWrapper::CreateOffer(
141 const MediaConstraintsInterface* constraints) {
142 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
143 << ": CreateOffer.";
144 peer_connection_->CreateOffer(this, constraints);
145}
146
147void PeerConnectionTestWrapper::CreateAnswer(
148 const MediaConstraintsInterface* constraints) {
149 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
150 << ": CreateAnswer.";
151 peer_connection_->CreateAnswer(this, constraints);
152}
153
154void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
155 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
156 CreateAnswer(NULL);
157}
158
159void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
160 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
161}
162
163void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
164 const std::string& sdp) {
165 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
166 << ": SetLocalDescription " << type << " " << sdp;
167
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000168 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
169 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000170 MockSetSessionDescriptionObserver>());
171 peer_connection_->SetLocalDescription(
172 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
173}
174
175void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
176 const std::string& sdp) {
177 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
178 << ": SetRemoteDescription " << type << " " << sdp;
179
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000180 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
181 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000182 MockSetSessionDescriptionObserver>());
183 peer_connection_->SetRemoteDescription(
184 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
185}
186
187void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
188 int sdp_mline_index,
189 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700190 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000191 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
192 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000193}
194
195void PeerConnectionTestWrapper::WaitForCallEstablished() {
196 WaitForConnection();
197 WaitForAudio();
198 WaitForVideo();
199}
200
201void PeerConnectionTestWrapper::WaitForConnection() {
202 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
203 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
204 << ": Connected.";
205}
206
207bool PeerConnectionTestWrapper::CheckForConnection() {
208 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000209 PeerConnectionInterface::kIceConnectionConnected) ||
210 (peer_connection_->ice_connection_state() ==
211 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000212}
213
214void PeerConnectionTestWrapper::WaitForAudio() {
215 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
216 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
217 << ": Got enough audio frames.";
218}
219
220bool PeerConnectionTestWrapper::CheckForAudio() {
221 return (fake_audio_capture_module_->frames_received() >=
222 kTestAudioFrameCount);
223}
224
225void PeerConnectionTestWrapper::WaitForVideo() {
226 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
227 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
228 << ": Got enough video frames.";
229}
230
231bool PeerConnectionTestWrapper::CheckForVideo() {
232 if (!renderer_) {
233 return false;
234 }
235 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
236}
237
238void PeerConnectionTestWrapper::GetAndAddUserMedia(
239 bool audio, const webrtc::FakeConstraints& audio_constraints,
240 bool video, const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000241 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000242 GetUserMedia(audio, audio_constraints, video, video_constraints);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000243 EXPECT_TRUE(peer_connection_->AddStream(stream));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000244}
245
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000246rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000247 PeerConnectionTestWrapper::GetUserMedia(
248 bool audio, const webrtc::FakeConstraints& audio_constraints,
249 bool video, const webrtc::FakeConstraints& video_constraints) {
250 std::string label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000251 rtc::ToString<int>(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000252 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000254 peer_connection_factory_->CreateLocalMediaStream(label);
255
256 if (audio) {
257 FakeConstraints constraints = audio_constraints;
258 // Disable highpass filter so that we can get all the test audio frames.
259 constraints.AddMandatory(
260 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000261 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000262 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000263 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000264 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
265 source));
266 stream->AddTrack(audio_track);
267 }
268
269 if (video) {
270 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
271 FakeConstraints constraints = video_constraints;
272 constraints.SetMandatoryMaxFrameRate(10);
273
perkja3ede6c2016-03-08 01:27:48 +0100274 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000275 peer_connection_factory_->CreateVideoSource(
deadbeef112b2e92017-02-10 20:13:37 -0800276 std::unique_ptr<cricket::VideoCapturer>(
277 new webrtc::FakePeriodicVideoCapturer()),
278 &constraints);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000279 std::string videotrack_label = label + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000280 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000281 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
282
283 stream->AddTrack(video_track);
284 }
285 return stream;
286}