blob: 0010edb0f8cd3587f4724c245c9c81caa6ae0051 [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
Mirko Bonadei2ff3f492018-11-22 09:00:13 +010015#include "api/create_peerconnection_factory.h"
Anders Carlsson67537952018-05-03 11:28:29 +020016#include "api/video_codecs/builtin_video_decoder_factory.h"
17#include "api/video_codecs/builtin_video_encoder_factory.h"
18#include "modules/audio_processing/include/audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "p2p/base/fakeportallocator.h"
Steve Antona3a92c22017-12-07 10:27:41 -080020#include "pc/sdputils.h"
Niels Möllera1cc73f2018-05-28 16:20:42 +020021#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "pc/test/fakertccertificategenerator.h"
23#include "pc/test/mockpeerconnectionobservers.h"
24#include "pc/test/peerconnectiontestwrapper.h"
25#include "rtc_base/gunit.h"
Johannes Kron965e7942018-09-13 15:36:20 +020026#include "rtc_base/timeutils.h"
wu@webrtc.org364f2042013-11-20 21:49:41 +000027
wu@webrtc.org364f2042013-11-20 21:49:41 +000028using webrtc::FakeConstraints;
29using webrtc::FakeVideoTrackRenderer;
30using webrtc::IceCandidateInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000031using webrtc::MediaStreamInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080032using webrtc::MediaStreamTrackInterface;
wu@webrtc.org364f2042013-11-20 21:49:41 +000033using webrtc::MockSetSessionDescriptionObserver;
34using webrtc::PeerConnectionInterface;
Steve Anton191c39f2018-01-24 19:35:55 -080035using webrtc::RtpReceiverInterface;
Steve Antona3a92c22017-12-07 10:27:41 -080036using webrtc::SdpType;
wu@webrtc.org364f2042013-11-20 21:49:41 +000037using webrtc::SessionDescriptionInterface;
38using webrtc::VideoTrackInterface;
39
Steve Antona3a92c22017-12-07 10:27:41 -080040namespace {
Seth Hampson845e8782018-03-02 11:34:10 -080041const char kStreamIdBase[] = "stream_id";
Steve Antona3a92c22017-12-07 10:27:41 -080042const char kVideoTrackLabelBase[] = "video_track";
43const char kAudioTrackLabelBase[] = "audio_track";
44constexpr int kMaxWait = 10000;
45constexpr int kTestAudioFrameCount = 3;
46constexpr int kTestVideoFrameCount = 3;
47} // namespace
48
wu@webrtc.org364f2042013-11-20 21:49:41 +000049void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
50 PeerConnectionTestWrapper* callee) {
51 caller->SignalOnIceCandidateReady.connect(
52 callee, &PeerConnectionTestWrapper::AddIceCandidate);
53 callee->SignalOnIceCandidateReady.connect(
54 caller, &PeerConnectionTestWrapper::AddIceCandidate);
55
Yves Gerey665174f2018-06-19 15:03:05 +020056 caller->SignalOnSdpReady.connect(callee,
57 &PeerConnectionTestWrapper::ReceiveOfferSdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +000058 callee->SignalOnSdpReady.connect(
59 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
60}
61
danilchape9021a32016-05-17 01:52:02 -070062PeerConnectionTestWrapper::PeerConnectionTestWrapper(
63 const std::string& name,
64 rtc::Thread* network_thread,
65 rtc::Thread* worker_thread)
66 : name_(name),
67 network_thread_(network_thread),
68 worker_thread_(worker_thread) {}
wu@webrtc.org364f2042013-11-20 21:49:41 +000069
70PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
71
72bool PeerConnectionTestWrapper::CreatePc(
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(
Niels Möllerf06f9232018-08-07 12:32:18 +020098 config, std::move(port_allocator), std::move(cert_generator), this);
wu@webrtc.org364f2042013-11-20 21:49:41 +000099
100 return peer_connection_.get() != NULL;
101}
102
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103rtc::scoped_refptr<webrtc::DataChannelInterface>
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000104PeerConnectionTestWrapper::CreateDataChannel(
105 const std::string& label,
106 const webrtc::DataChannelInit& init) {
107 return peer_connection_->CreateDataChannel(label, &init);
108}
109
Steve Anton191c39f2018-01-24 19:35:55 -0800110void PeerConnectionTestWrapper::OnAddTrack(
111 rtc::scoped_refptr<RtpReceiverInterface> receiver,
112 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
113 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": OnAddTrack";
114 if (receiver->track()->kind() == MediaStreamTrackInterface::kVideoKind) {
115 auto* video_track =
116 static_cast<VideoTrackInterface*>(receiver->track().get());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200117 renderer_ = absl::make_unique<FakeVideoTrackRenderer>(video_track);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000118 }
119}
120
121void PeerConnectionTestWrapper::OnIceCandidate(
122 const IceCandidateInterface* candidate) {
123 std::string sdp;
124 EXPECT_TRUE(candidate->ToString(&sdp));
125 // Give the user a chance to modify sdp for testing.
126 SignalOnIceCandidateCreated(&sdp);
127 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
128 sdp);
129}
130
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000131void PeerConnectionTestWrapper::OnDataChannel(
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700132 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
jiayl@webrtc.org1a6c6282014-06-12 21:59:29 +0000133 SignalOnDataChannel(data_channel);
134}
135
wu@webrtc.org364f2042013-11-20 21:49:41 +0000136void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000137 // This callback should take the ownership of |desc|.
kwibergd1fe2812016-04-27 06:47:29 -0700138 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000139 std::string sdp;
140 EXPECT_TRUE(desc->ToString(&sdp));
141
Mirko Bonadei675513b2017-11-09 11:09:25 +0100142 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": "
Steve Antona3a92c22017-12-07 10:27:41 -0800143 << webrtc::SdpTypeToString(desc->GetType())
144 << " sdp created: " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000145
146 // Give the user a chance to modify sdp for testing.
147 SignalOnSdpCreated(&sdp);
148
Steve Antona3a92c22017-12-07 10:27:41 -0800149 SetLocalDescription(desc->GetType(), sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000150
151 SignalOnSdpReady(sdp);
152}
153
154void PeerConnectionTestWrapper::CreateOffer(
Niels Möllerf06f9232018-08-07 12:32:18 +0200155 const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100156 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer.";
Niels Möllerf06f9232018-08-07 12:32:18 +0200157 peer_connection_->CreateOffer(this, options);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000158}
159
160void PeerConnectionTestWrapper::CreateAnswer(
Niels Möllerf06f9232018-08-07 12:32:18 +0200161 const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions& options) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100162 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
163 << ": CreateAnswer.";
Niels Möllerf06f9232018-08-07 12:32:18 +0200164 peer_connection_->CreateAnswer(this, options);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000165}
166
167void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800168 SetRemoteDescription(SdpType::kOffer, sdp);
Niels Möllerf06f9232018-08-07 12:32:18 +0200169 CreateAnswer(webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000170}
171
172void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
Steve Antona3a92c22017-12-07 10:27:41 -0800173 SetRemoteDescription(SdpType::kAnswer, sdp);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000174}
175
Steve Antona3a92c22017-12-07 10:27:41 -0800176void PeerConnectionTestWrapper::SetLocalDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000177 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800179 << ": SetLocalDescription " << webrtc::SdpTypeToString(type)
180 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000181
Yves Gerey665174f2018-06-19 15:03:05 +0200182 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
183 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000184 peer_connection_->SetLocalDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800185 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000186}
187
Steve Antona3a92c22017-12-07 10:27:41 -0800188void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type,
wu@webrtc.org364f2042013-11-20 21:49:41 +0000189 const std::string& sdp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100190 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
Steve Antona3a92c22017-12-07 10:27:41 -0800191 << ": SetRemoteDescription " << webrtc::SdpTypeToString(type)
192 << " " << sdp;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000193
Yves Gerey665174f2018-06-19 15:03:05 +0200194 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
195 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000196 peer_connection_->SetRemoteDescription(
Steve Antona3a92c22017-12-07 10:27:41 -0800197 observer, webrtc::CreateSessionDescription(type, sdp).release());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000198}
199
200void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
201 int sdp_mline_index,
202 const std::string& candidate) {
kwibergd1fe2812016-04-27 06:47:29 -0700203 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000204 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
205 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000206}
207
208void PeerConnectionTestWrapper::WaitForCallEstablished() {
209 WaitForConnection();
210 WaitForAudio();
211 WaitForVideo();
212}
213
214void PeerConnectionTestWrapper::WaitForConnection() {
215 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100216 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000217}
218
219bool PeerConnectionTestWrapper::CheckForConnection() {
220 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000221 PeerConnectionInterface::kIceConnectionConnected) ||
222 (peer_connection_->ice_connection_state() ==
223 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000224}
225
226void PeerConnectionTestWrapper::WaitForAudio() {
227 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100228 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
229 << ": Got enough audio frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000230}
231
232bool PeerConnectionTestWrapper::CheckForAudio() {
233 return (fake_audio_capture_module_->frames_received() >=
234 kTestAudioFrameCount);
235}
236
237void PeerConnectionTestWrapper::WaitForVideo() {
238 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100239 RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
240 << ": Got enough video frames.";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000241}
242
243bool PeerConnectionTestWrapper::CheckForVideo() {
244 if (!renderer_) {
245 return false;
246 }
247 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
248}
249
250void PeerConnectionTestWrapper::GetAndAddUserMedia(
Yves Gerey665174f2018-06-19 15:03:05 +0200251 bool audio,
252 const cricket::AudioOptions& audio_options,
253 bool video,
254 const webrtc::FakeConstraints& video_constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000255 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
Niels Möller2d02e082018-05-21 11:23:35 +0200256 GetUserMedia(audio, audio_options, video, video_constraints);
Steve Anton191c39f2018-01-24 19:35:55 -0800257 for (auto audio_track : stream->GetAudioTracks()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700258 EXPECT_TRUE(peer_connection_->AddTrack(audio_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-24 19:35:55 -0800259 }
260 for (auto video_track : stream->GetVideoTracks()) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700261 EXPECT_TRUE(peer_connection_->AddTrack(video_track, {stream->id()}).ok());
Steve Anton191c39f2018-01-24 19:35:55 -0800262 }
wu@webrtc.org364f2042013-11-20 21:49:41 +0000263}
264
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265rtc::scoped_refptr<webrtc::MediaStreamInterface>
Niels Möller2d02e082018-05-21 11:23:35 +0200266PeerConnectionTestWrapper::GetUserMedia(
Yves Gerey665174f2018-06-19 15:03:05 +0200267 bool audio,
268 const cricket::AudioOptions& audio_options,
269 bool video,
270 const webrtc::FakeConstraints& video_constraints) {
Seth Hampson845e8782018-03-02 11:34:10 -0800271 std::string stream_id =
272 kStreamIdBase + rtc::ToString(num_get_user_media_calls_++);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000273 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
Seth Hampson845e8782018-03-02 11:34:10 -0800274 peer_connection_factory_->CreateLocalMediaStream(stream_id);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000275
276 if (audio) {
Niels Möller2d02e082018-05-21 11:23:35 +0200277 cricket::AudioOptions options = audio_options;
wu@webrtc.org364f2042013-11-20 21:49:41 +0000278 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200279 options.highpass_filter = false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000280 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200281 peer_connection_factory_->CreateAudioSource(options);
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.
Niels Möllera1cc73f2018-05-28 16:20:42 +0200290 webrtc::FakePeriodicVideoSource::Config config;
291 config.frame_interval_ms = 100;
Johannes Kron965e7942018-09-13 15:36:20 +0200292 config.timestamp_offset_ms = rtc::TimeMillis();
wu@webrtc.org364f2042013-11-20 21:49:41 +0000293
perkja3ede6c2016-03-08 01:27:48 +0100294 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
Niels Möllera1cc73f2018-05-28 16:20:42 +0200295 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
296 config, /* remote */ false);
297
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}