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