wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 11 | #include <string> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #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.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 20 | |
| 21 | static const char kStreamLabelBase[] = "stream_label"; |
| 22 | static const char kVideoTrackLabelBase[] = "video_track"; |
| 23 | static const char kAudioTrackLabelBase[] = "audio_track"; |
buildbot@webrtc.org | 3e01e0b | 2014-05-13 17:54:10 +0000 | [diff] [blame] | 24 | static const int kMaxWait = 10000; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 25 | static const int kTestAudioFrameCount = 3; |
| 26 | static const int kTestVideoFrameCount = 3; |
| 27 | |
| 28 | using webrtc::FakeConstraints; |
| 29 | using webrtc::FakeVideoTrackRenderer; |
| 30 | using webrtc::IceCandidateInterface; |
| 31 | using webrtc::MediaConstraintsInterface; |
| 32 | using webrtc::MediaStreamInterface; |
| 33 | using webrtc::MockSetSessionDescriptionObserver; |
| 34 | using webrtc::PeerConnectionInterface; |
| 35 | using webrtc::SessionDescriptionInterface; |
| 36 | using webrtc::VideoTrackInterface; |
| 37 | |
| 38 | void 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 | |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 51 | PeerConnectionTestWrapper::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.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 58 | |
| 59 | PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {} |
| 60 | |
| 61 | bool PeerConnectionTestWrapper::CreatePc( |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 62 | const MediaConstraintsInterface* constraints, |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 63 | const webrtc::PeerConnectionInterface::RTCConfiguration& config, |
| 64 | rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, |
| 65 | rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 66 | std::unique_ptr<cricket::PortAllocator> port_allocator( |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 67 | new cricket::FakePortAllocator(network_thread_, nullptr)); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 68 | |
deadbeef | ee8c6d3 | 2015-08-13 14:27:18 -0700 | [diff] [blame] | 69 | fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 70 | if (fake_audio_capture_module_ == NULL) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 75 | network_thread_, worker_thread_, rtc::Thread::Current(), |
kwiberg | 9e5b11e | 2017-04-19 03:47:57 -0700 | [diff] [blame] | 76 | fake_audio_capture_module_, audio_encoder_factory, audio_decoder_factory, |
| 77 | nullptr, nullptr); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 78 | if (!peer_connection_factory_) { |
| 79 | return false; |
| 80 | } |
| 81 | |
Henrik Boström | d79599d | 2016-06-01 13:58:50 +0200 | [diff] [blame] | 82 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator( |
deadbeef | 1b54a5f | 2017-01-23 19:39:57 -0800 | [diff] [blame] | 83 | new FakeRTCCertificateGenerator()); |
Henrik Boström | d79599d | 2016-06-01 13:58:50 +0200 | [diff] [blame] | 84 | peer_connection_ = peer_connection_factory_->CreatePeerConnection( |
| 85 | config, constraints, std::move(port_allocator), std::move(cert_generator), |
| 86 | this); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 87 | |
| 88 | return peer_connection_.get() != NULL; |
| 89 | } |
| 90 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 91 | rtc::scoped_refptr<webrtc::DataChannelInterface> |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 92 | PeerConnectionTestWrapper::CreateDataChannel( |
| 93 | const std::string& label, |
| 94 | const webrtc::DataChannelInit& init) { |
| 95 | return peer_connection_->CreateDataChannel(label, &init); |
| 96 | } |
| 97 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 98 | void PeerConnectionTestWrapper::OnAddStream( |
| 99 | rtc::scoped_refptr<MediaStreamInterface> stream) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 100 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": OnAddStream"; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 101 | // TODO(ronghuawu): support multiple streams. |
| 102 | if (stream->GetVideoTracks().size() > 0) { |
| 103 | renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0])); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void 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.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 117 | void PeerConnectionTestWrapper::OnDataChannel( |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 118 | rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) { |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 119 | SignalOnDataChannel(data_channel); |
| 120 | } |
| 121 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 122 | void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) { |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 123 | // This callback should take the ownership of |desc|. |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 124 | std::unique_ptr<SessionDescriptionInterface> owned_desc(desc); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 125 | std::string sdp; |
| 126 | EXPECT_TRUE(desc->ToString(&sdp)); |
| 127 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 128 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": " |
| 129 | << desc->type() << " sdp created: " << sdp; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 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 | |
| 139 | void PeerConnectionTestWrapper::CreateOffer( |
| 140 | const MediaConstraintsInterface* constraints) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 141 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": CreateOffer."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 142 | peer_connection_->CreateOffer(this, constraints); |
| 143 | } |
| 144 | |
| 145 | void PeerConnectionTestWrapper::CreateAnswer( |
| 146 | const MediaConstraintsInterface* constraints) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 147 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 148 | << ": CreateAnswer."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 149 | peer_connection_->CreateAnswer(this, constraints); |
| 150 | } |
| 151 | |
| 152 | void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) { |
| 153 | SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp); |
| 154 | CreateAnswer(NULL); |
| 155 | } |
| 156 | |
| 157 | void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) { |
| 158 | SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp); |
| 159 | } |
| 160 | |
| 161 | void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type, |
| 162 | const std::string& sdp) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 163 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 164 | << ": SetLocalDescription " << type << " " << sdp; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 165 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 166 | rtc::scoped_refptr<MockSetSessionDescriptionObserver> |
| 167 | observer(new rtc::RefCountedObject< |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 168 | MockSetSessionDescriptionObserver>()); |
| 169 | peer_connection_->SetLocalDescription( |
| 170 | observer, webrtc::CreateSessionDescription(type, sdp, NULL)); |
| 171 | } |
| 172 | |
| 173 | void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type, |
| 174 | const std::string& sdp) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 175 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 176 | << ": SetRemoteDescription " << type << " " << sdp; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 177 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 178 | rtc::scoped_refptr<MockSetSessionDescriptionObserver> |
| 179 | observer(new rtc::RefCountedObject< |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 180 | MockSetSessionDescriptionObserver>()); |
| 181 | peer_connection_->SetRemoteDescription( |
| 182 | observer, webrtc::CreateSessionDescription(type, sdp, NULL)); |
| 183 | } |
| 184 | |
| 185 | void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid, |
| 186 | int sdp_mline_index, |
| 187 | const std::string& candidate) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 188 | std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate( |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 189 | webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL)); |
| 190 | EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get())); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void PeerConnectionTestWrapper::WaitForCallEstablished() { |
| 194 | WaitForConnection(); |
| 195 | WaitForAudio(); |
| 196 | WaitForVideo(); |
| 197 | } |
| 198 | |
| 199 | void PeerConnectionTestWrapper::WaitForConnection() { |
| 200 | EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 201 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ << ": Connected."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | bool PeerConnectionTestWrapper::CheckForConnection() { |
| 205 | return (peer_connection_->ice_connection_state() == |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 206 | PeerConnectionInterface::kIceConnectionConnected) || |
| 207 | (peer_connection_->ice_connection_state() == |
| 208 | PeerConnectionInterface::kIceConnectionCompleted); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void PeerConnectionTestWrapper::WaitForAudio() { |
| 212 | EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 213 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 214 | << ": Got enough audio frames."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | bool PeerConnectionTestWrapper::CheckForAudio() { |
| 218 | return (fake_audio_capture_module_->frames_received() >= |
| 219 | kTestAudioFrameCount); |
| 220 | } |
| 221 | |
| 222 | void PeerConnectionTestWrapper::WaitForVideo() { |
| 223 | EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 224 | RTC_LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 225 | << ": Got enough video frames."; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool PeerConnectionTestWrapper::CheckForVideo() { |
| 229 | if (!renderer_) { |
| 230 | return false; |
| 231 | } |
| 232 | return (renderer_->num_rendered_frames() >= kTestVideoFrameCount); |
| 233 | } |
| 234 | |
| 235 | void PeerConnectionTestWrapper::GetAndAddUserMedia( |
| 236 | bool audio, const webrtc::FakeConstraints& audio_constraints, |
| 237 | bool video, const webrtc::FakeConstraints& video_constraints) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 238 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 239 | GetUserMedia(audio, audio_constraints, video, video_constraints); |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 240 | EXPECT_TRUE(peer_connection_->AddStream(stream)); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 241 | } |
| 242 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 243 | rtc::scoped_refptr<webrtc::MediaStreamInterface> |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 244 | PeerConnectionTestWrapper::GetUserMedia( |
| 245 | bool audio, const webrtc::FakeConstraints& audio_constraints, |
| 246 | bool video, const webrtc::FakeConstraints& video_constraints) { |
| 247 | std::string label = kStreamLabelBase + |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 248 | rtc::ToString<int>( |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 249 | static_cast<int>(peer_connection_->local_streams()->count())); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 250 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 251 | peer_connection_factory_->CreateLocalMediaStream(label); |
| 252 | |
| 253 | if (audio) { |
| 254 | FakeConstraints constraints = audio_constraints; |
| 255 | // Disable highpass filter so that we can get all the test audio frames. |
| 256 | constraints.AddMandatory( |
| 257 | MediaConstraintsInterface::kHighpassFilter, false); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 258 | rtc::scoped_refptr<webrtc::AudioSourceInterface> source = |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 259 | peer_connection_factory_->CreateAudioSource(&constraints); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 260 | rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 261 | peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase, |
| 262 | source)); |
| 263 | stream->AddTrack(audio_track); |
| 264 | } |
| 265 | |
| 266 | if (video) { |
| 267 | // Set max frame rate to 10fps to reduce the risk of the tests to be flaky. |
| 268 | FakeConstraints constraints = video_constraints; |
| 269 | constraints.SetMandatoryMaxFrameRate(10); |
| 270 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 271 | rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source = |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 272 | peer_connection_factory_->CreateVideoSource( |
deadbeef | 112b2e9 | 2017-02-10 20:13:37 -0800 | [diff] [blame] | 273 | std::unique_ptr<cricket::VideoCapturer>( |
| 274 | new webrtc::FakePeriodicVideoCapturer()), |
| 275 | &constraints); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 276 | std::string videotrack_label = label + kVideoTrackLabelBase; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 277 | rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track( |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 278 | peer_connection_factory_->CreateVideoTrack(videotrack_label, source)); |
| 279 | |
| 280 | stream->AddTrack(video_track); |
| 281 | } |
| 282 | return stream; |
| 283 | } |