| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 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. | |
| 9 */ | |
| 10 | |
| 11 #include <utility> | |
| 12 | |
| 13 #include "webrtc/api/test/fakeperiodicvideocapturer.h" | |
| 14 #include "webrtc/api/test/fakertccertificategenerator.h" | |
| 15 #include "webrtc/api/test/mockpeerconnectionobservers.h" | |
| 16 #include "webrtc/api/test/peerconnectiontestwrapper.h" | |
| 17 #include "webrtc/base/gunit.h" | |
| 18 #include "webrtc/p2p/base/fakeportallocator.h" | |
| 19 | |
| 20 static const char kStreamLabelBase[] = "stream_label"; | |
| 21 static const char kVideoTrackLabelBase[] = "video_track"; | |
| 22 static const char kAudioTrackLabelBase[] = "audio_track"; | |
| 23 static const int kMaxWait = 10000; | |
| 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 | |
| 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) {} | |
| 57 | |
| 58 PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {} | |
| 59 | |
| 60 bool PeerConnectionTestWrapper::CreatePc( | |
| 61 const MediaConstraintsInterface* constraints, | |
| 62 const webrtc::PeerConnectionInterface::RTCConfiguration& config) { | |
| 63 std::unique_ptr<cricket::PortAllocator> port_allocator( | |
| 64 new cricket::FakePortAllocator(network_thread_, nullptr)); | |
| 65 | |
| 66 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(); | |
| 67 if (fake_audio_capture_module_ == NULL) { | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( | |
| 72 network_thread_, worker_thread_, rtc::Thread::Current(), | |
| 73 fake_audio_capture_module_, NULL, NULL); | |
| 74 if (!peer_connection_factory_) { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator( | |
| 79 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeRTCCertificateGenerator() | |
| 80 : nullptr); | |
| 81 peer_connection_ = peer_connection_factory_->CreatePeerConnection( | |
| 82 config, constraints, std::move(port_allocator), std::move(cert_generator), | |
| 83 this); | |
| 84 | |
| 85 return peer_connection_.get() != NULL; | |
| 86 } | |
| 87 | |
| 88 rtc::scoped_refptr<webrtc::DataChannelInterface> | |
| 89 PeerConnectionTestWrapper::CreateDataChannel( | |
| 90 const std::string& label, | |
| 91 const webrtc::DataChannelInit& init) { | |
| 92 return peer_connection_->CreateDataChannel(label, &init); | |
| 93 } | |
| 94 | |
| 95 void PeerConnectionTestWrapper::OnAddStream( | |
| 96 rtc::scoped_refptr<MediaStreamInterface> stream) { | |
| 97 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 98 << ": OnAddStream"; | |
| 99 // TODO(ronghuawu): support multiple streams. | |
| 100 if (stream->GetVideoTracks().size() > 0) { | |
| 101 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0])); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void PeerConnectionTestWrapper::OnIceCandidate( | |
| 106 const IceCandidateInterface* candidate) { | |
| 107 std::string sdp; | |
| 108 EXPECT_TRUE(candidate->ToString(&sdp)); | |
| 109 // Give the user a chance to modify sdp for testing. | |
| 110 SignalOnIceCandidateCreated(&sdp); | |
| 111 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(), | |
| 112 sdp); | |
| 113 } | |
| 114 | |
| 115 void PeerConnectionTestWrapper::OnDataChannel( | |
| 116 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) { | |
| 117 SignalOnDataChannel(data_channel); | |
| 118 } | |
| 119 | |
| 120 void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) { | |
| 121 // This callback should take the ownership of |desc|. | |
| 122 std::unique_ptr<SessionDescriptionInterface> owned_desc(desc); | |
| 123 std::string sdp; | |
| 124 EXPECT_TRUE(desc->ToString(&sdp)); | |
| 125 | |
| 126 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 127 << ": " << desc->type() << " sdp created: " << sdp; | |
| 128 | |
| 129 // Give the user a chance to modify sdp for testing. | |
| 130 SignalOnSdpCreated(&sdp); | |
| 131 | |
| 132 SetLocalDescription(desc->type(), sdp); | |
| 133 | |
| 134 SignalOnSdpReady(sdp); | |
| 135 } | |
| 136 | |
| 137 void PeerConnectionTestWrapper::CreateOffer( | |
| 138 const MediaConstraintsInterface* constraints) { | |
| 139 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 140 << ": CreateOffer."; | |
| 141 peer_connection_->CreateOffer(this, constraints); | |
| 142 } | |
| 143 | |
| 144 void PeerConnectionTestWrapper::CreateAnswer( | |
| 145 const MediaConstraintsInterface* constraints) { | |
| 146 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 147 << ": CreateAnswer."; | |
| 148 peer_connection_->CreateAnswer(this, constraints); | |
| 149 } | |
| 150 | |
| 151 void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) { | |
| 152 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp); | |
| 153 CreateAnswer(NULL); | |
| 154 } | |
| 155 | |
| 156 void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) { | |
| 157 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp); | |
| 158 } | |
| 159 | |
| 160 void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type, | |
| 161 const std::string& sdp) { | |
| 162 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 163 << ": SetLocalDescription " << type << " " << sdp; | |
| 164 | |
| 165 rtc::scoped_refptr<MockSetSessionDescriptionObserver> | |
| 166 observer(new rtc::RefCountedObject< | |
| 167 MockSetSessionDescriptionObserver>()); | |
| 168 peer_connection_->SetLocalDescription( | |
| 169 observer, webrtc::CreateSessionDescription(type, sdp, NULL)); | |
| 170 } | |
| 171 | |
| 172 void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type, | |
| 173 const std::string& sdp) { | |
| 174 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 175 << ": SetRemoteDescription " << type << " " << sdp; | |
| 176 | |
| 177 rtc::scoped_refptr<MockSetSessionDescriptionObserver> | |
| 178 observer(new rtc::RefCountedObject< | |
| 179 MockSetSessionDescriptionObserver>()); | |
| 180 peer_connection_->SetRemoteDescription( | |
| 181 observer, webrtc::CreateSessionDescription(type, sdp, NULL)); | |
| 182 } | |
| 183 | |
| 184 void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid, | |
| 185 int sdp_mline_index, | |
| 186 const std::string& candidate) { | |
| 187 std::unique_ptr<webrtc::IceCandidateInterface> owned_candidate( | |
| 188 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL)); | |
| 189 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get())); | |
| 190 } | |
| 191 | |
| 192 void PeerConnectionTestWrapper::WaitForCallEstablished() { | |
| 193 WaitForConnection(); | |
| 194 WaitForAudio(); | |
| 195 WaitForVideo(); | |
| 196 } | |
| 197 | |
| 198 void PeerConnectionTestWrapper::WaitForConnection() { | |
| 199 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait); | |
| 200 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 201 << ": Connected."; | |
| 202 } | |
| 203 | |
| 204 bool PeerConnectionTestWrapper::CheckForConnection() { | |
| 205 return (peer_connection_->ice_connection_state() == | |
| 206 PeerConnectionInterface::kIceConnectionConnected) || | |
| 207 (peer_connection_->ice_connection_state() == | |
| 208 PeerConnectionInterface::kIceConnectionCompleted); | |
| 209 } | |
| 210 | |
| 211 void PeerConnectionTestWrapper::WaitForAudio() { | |
| 212 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait); | |
| 213 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 214 << ": Got enough audio frames."; | |
| 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); | |
| 224 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ | |
| 225 << ": Got enough video frames."; | |
| 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) { | |
| 238 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = | |
| 239 GetUserMedia(audio, audio_constraints, video, video_constraints); | |
| 240 EXPECT_TRUE(peer_connection_->AddStream(stream)); | |
| 241 } | |
| 242 | |
| 243 rtc::scoped_refptr<webrtc::MediaStreamInterface> | |
| 244 PeerConnectionTestWrapper::GetUserMedia( | |
| 245 bool audio, const webrtc::FakeConstraints& audio_constraints, | |
| 246 bool video, const webrtc::FakeConstraints& video_constraints) { | |
| 247 std::string label = kStreamLabelBase + | |
| 248 rtc::ToString<int>( | |
| 249 static_cast<int>(peer_connection_->local_streams()->count())); | |
| 250 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = | |
| 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); | |
| 258 rtc::scoped_refptr<webrtc::AudioSourceInterface> source = | |
| 259 peer_connection_factory_->CreateAudioSource(&constraints); | |
| 260 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track( | |
| 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 | |
| 271 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source = | |
| 272 peer_connection_factory_->CreateVideoSource( | |
| 273 new webrtc::FakePeriodicVideoCapturer(), &constraints); | |
| 274 std::string videotrack_label = label + kVideoTrackLabelBase; | |
| 275 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track( | |
| 276 peer_connection_factory_->CreateVideoTrack(videotrack_label, source)); | |
| 277 | |
| 278 stream->AddTrack(video_track); | |
| 279 } | |
| 280 return stream; | |
| 281 } | |
| OLD | NEW |