blob: a3ee5ab30061b51bcbc7be4791a954deb684bd7f [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>
Steve Anton191c39f2018-01-24 19:35:55 -080013#include <vector>
kwiberg0eb15ed2015-12-17 03:04:15 -080014
Anders Carlsson67537952018-05-03 11:28:29 +020015#include "api/video_codecs/builtin_video_decoder_factory.h"
16#include "api/video_codecs/builtin_video_encoder_factory.h"
17#include "modules/audio_processing/include/audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "p2p/base/fakeportallocator.h"
Steve Antona3a92c22017-12-07 10:27:41 -080019#include "pc/sdputils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "pc/test/fakeperiodicvideocapturer.h"
21#include "pc/test/fakertccertificategenerator.h"
22#include "pc/test/mockpeerconnectionobservers.h"
23#include "pc/test/peerconnectiontestwrapper.h"
24#include "rtc_base/gunit.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000025
wu@webrtc.org364f2042013-11-20 21:49:41 +000026using webrtc::FakeConstraints;
27using webrtc::FakeVideoTrackRenderer;
28using webrtc::IceCandidateInterface;
29using webrtc::MediaConstraintsInterface;
30using webrtc::MediaStreamInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080031using webrtc::MediaStreamTrackInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000032using webrtc::MockSetSessionDescriptionObserver;
33using webrtc::PeerConnectionInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080034using webrtc::RtpReceiverInterface;
Steve Antona3a92c22017-12-07 10:27:41 -080035using webrtc::SdpType;
wu@webrtc.org364f2042013-11-20 21:49:41 +000036using webrtc::SessionDescriptionInterface;
37using webrtc::VideoTrackInterface;
38
Steve Antona3a92c22017-12-07 10:27:41 -080039namespace {
Seth Hampson845e8782018-03-02 11:34:10 -080040const char kStreamIdBase[] = "stream_id";
Steve Antona3a92c22017-12-07 10:27:41 -080041const char kVideoTrackLabelBase[] = "video_track";
42const char kAudioTrackLabelBase[] = "audio_track";
43constexpr int kMaxWait = 10000;
44constexpr int kTestAudioFrameCount = 3;
45constexpr int kTestVideoFrameCount = 3;
46} // namespace
47
wu@webrtc.org364f2042013-11-20 21:49:41 +000048void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
49 PeerConnectionTestWrapper* callee) {
50 caller->SignalOnIceCandidateReady.connect(
51 callee, &PeerConnectionTestWrapper::AddIceCandidate);
52 callee->SignalOnIceCandidateReady.connect(
53 caller, &PeerConnectionTestWrapper::AddIceCandidate);
54
55 caller->SignalOnSdpReady.connect(
56 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
57 callee->SignalOnSdpReady.connect(
58 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
59}
60
danilchape9021a32016-05-17 01:52:02 -070061PeerConnectionTestWrapper::PeerConnectionTestWrapper(
62 const std::string& name,
63 rtc::Thread* network_thread,
64 rtc::Thread* worker_thread)
65 : name_(name),
66 network_thread_(network_thread),
67 worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000068
69PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
70
71bool PeerConnectionTestWrapper::CreatePc(
zhihuang9763d562016-08-05 11:14:50 -070072 const MediaConstraintsInterface* constraints,
kwiberg9e5b11e2017-04-19 03:47:57 -070073 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
74 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
75 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) {
kwibergd1fe2812016-04-27 06:47:29 -070076 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -070077 new cricket::FakePortAllocator(network_thread_, nullptr));
wu@webrtc.org364f2042013-11-20 21:49:41 +000078
deadbeefee8c6d32015-08-13 14:27:18 -070079 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
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(
danilchape9021a32016-05-17 01:52:02 -070085 network_thread_, worker_thread_, rtc::Thread::Current(),
Anders Carlsson67537952018-05-03 11:28:29 +020086 rtc::scoped_refptr<webrtc::AudioDeviceModule>(fake_audio_capture_module_),
87 audio_encoder_factory, audio_decoder_factory,
88 webrtc::CreateBuiltinVideoEncoderFactory(),
89 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
90 nullptr /* audio_processing */);
wu@webrtc.org364f2042013-11-20 21:49:41 +000091 if (!peer_connection_factory_) {
92 return false;
93 }
94
Henrik Boströmd79599d2016-06-01 13:58:50 +020095 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator(
deadbeef1b54a5f2017-01-23 19:39:57 -080096 new FakeRTCCertificateGenerator());
Henrik Boströmd79599d2016-06-01 13:58:50 +020097 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
98 config, constraints, std::move(port_allocator), std::move(cert_generator),
99 this);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000100
101 return peer_connection_.get() != NULL;
102}
103
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000105PeerConnectionTestWrapper::CreateDataChannel(
106 const std::string& label,
107 const webrtc::DataChannelInit& init) {
108 return peer_connection_->CreateDataChannel(label, &init);
109}
110
Steve Anton191c39f2018-01-24 19:35:55 -0800111void PeerConnectionTestWrapper::OnAddTrack(
112 rtc::scoped_refptr<RtpReceiverInterface> receiver,
113 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
114 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": OnAddTrack";
115 if (receiver->track()->kind() == MediaStreamTrackInterface::kVideoKind) {
116 auto* video_track =
117 static_cast<VideoTrackInterface*>(receiver->track().get());
118 renderer_ = rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000119 }
120}
121
122void PeerConnectionTestWrapper::OnIceCandidate(
123 const IceCandidateInterface* candidate) {
124 std::string sdp;
125 EXPECT_TRUE(candidate->ToString(&sdp));
126 // Give the user a chance to modify sdp for testing.
127 SignalOnIceCandidateCreated(&sdp);
128 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
129 sdp);
130}
131
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000132void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700133 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000134 SignalOnDataChannel(data_channel);
135}
136
wu@webrtc.org364f2042013-11-20 21:49:41 +0000137void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000138 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700139 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000140 std::string sdp;
141 EXPECT_TRUE(desc->ToString(&sdp));
142
Mirko Bonadei675513b2017-11-09 11:09:25 +0100143 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": "
Steve Antona3a92c22017-12-07 10:27:41 -0800144 << webrtc::SdpTypeToString(desc->GetType())
145 << " sdp created: " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000146
147 // Give the user a chance to modify sdp for testing.
148 SignalOnSdpCreated(&sdp);
149
Steve Antona3a92c22017-12-07 10:27:41 -0800150 SetLocalDescription(desc->GetType(), sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000151
152 SignalOnSdpReady(sdp);
153}
154
155void PeerConnectionTestWrapper::CreateOffer(
156 const MediaConstraintsInterface* constraints) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100157 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000158 peer_connection_->CreateOffer(this, constraints);
159}
160
161void PeerConnectionTestWrapper::CreateAnswer(
162 const MediaConstraintsInterface* constraints) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100163 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
164 << ": CreateAnswer.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000165 peer_connection_->CreateAnswer(this, constraints);
166}
167
168void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800169 SetRemoteDescription(SdpType::kOffer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000170 CreateAnswer(NULL);
171}
172
173void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800174 SetRemoteDescription(SdpType::kAnswer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000175}
176
Steve Antona3a92c22017-12-07 10:27:41 -0800177void PeerConnectionTestWrapper::SetLocalDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000178 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100179 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800180 << ": SetLocalDescription " << webrtc::SdpTypeToString(type)
181 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000182
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(
Steve Antona3a92c22017-12-07 10:27:41 -0800187 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000188}
189
Steve Antona3a92c22017-12-07 10:27:41 -0800190void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000191 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100192 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800193 << ": SetRemoteDescription " << webrtc::SdpTypeToString(type)
194 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000195
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000196 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
197 observer(new rtc::RefCountedObject<
wu@webrtc.org364f2042013-11-20 21:49:41 +0000198 MockSetSessionDescriptionObserver>());
199 peer_connection_->SetRemoteDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800200 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000201}
202
203void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
204 int sdp_mline_index,
205 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700206 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000207 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
208 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000209}
210
211void PeerConnectionTestWrapper::WaitForCallEstablished() {
212 WaitForConnection();
213 WaitForAudio();
214 WaitForVideo();
215}
216
217void PeerConnectionTestWrapper::WaitForConnection() {
218 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100219 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000220}
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);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100231 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
232 << ": Got enough audio frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000233}
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);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100242 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
243 << ": Got enough video frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000244}
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);
Steve Anton191c39f2018-01-24 19:35:55 -0800258 for (auto audio_track : stream->GetAudioTracks()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700259 EXPECT_TRUE(peer_connection_->AddTrack(audio_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-24 19:35:55 -0800260 }
261 for (auto video_track : stream->GetVideoTracks()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700262 EXPECT_TRUE(peer_connection_->AddTrack(video_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-24 19:35:55 -0800263 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000264}
265
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266rtc::scoped_refptr<webrtc::MediaStreamInterface>
wu@webrtc.org364f2042013-11-20 21:49:41 +0000267 PeerConnectionTestWrapper::GetUserMedia(
268 bool audio, const webrtc::FakeConstraints& audio_constraints,
269 bool video, const webrtc::FakeConstraints& video_constraints) {
Seth Hampson845e8782018-03-02 11:34:10 -0800270 std::string stream_id =
271 kStreamIdBase + rtc::ToString(num_get_user_media_calls_++);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
Seth Hampson845e8782018-03-02 11:34:10 -0800273 peer_connection_factory_->CreateLocalMediaStream(stream_id);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000274
275 if (audio) {
276 FakeConstraints constraints = audio_constraints;
277 // Disable highpass filter so that we can get all the test audio frames.
278 constraints.AddMandatory(
279 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000280 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000281 peer_connection_factory_->CreateAudioSource(&constraints);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000282 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000283 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
284 source));
285 stream->AddTrack(audio_track);
286 }
287
288 if (video) {
289 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
290 FakeConstraints constraints = video_constraints;
291 constraints.SetMandatoryMaxFrameRate(10);
292
perkja3ede6c2016-03-08 01:27:48 +0100293 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
wu@webrtc.org364f2042013-11-20 21:49:41 +0000294 peer_connection_factory_->CreateVideoSource(
deadbeef112b2e92017-02-10 20:13:37 -0800295 std::unique_ptr<cricket::VideoCapturer>(
296 new webrtc::FakePeriodicVideoCapturer()),
297 &constraints);
Seth Hampson845e8782018-03-02 11:34:10 -0800298 std::string videotrack_label = stream_id + kVideoTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000299 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
wu@webrtc.org364f2042013-11-20 21:49:41 +0000300 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
301
302 stream->AddTrack(video_track);
303 }
304 return stream;
305}