wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2013, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "talk/app/webrtc/fakeportallocatorfactory.h" |
buildbot@webrtc.org | 61c1b8e | 2014-04-09 06:06:38 +0000 | [diff] [blame] | 29 | #include "talk/app/webrtc/test/fakedtlsidentityservice.h" |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 30 | #include "talk/app/webrtc/test/fakeperiodicvideocapturer.h" |
| 31 | #include "talk/app/webrtc/test/mockpeerconnectionobservers.h" |
| 32 | #include "talk/app/webrtc/test/peerconnectiontestwrapper.h" |
| 33 | #include "talk/app/webrtc/videosourceinterface.h" |
| 34 | #include "talk/base/gunit.h" |
| 35 | |
| 36 | static const char kStreamLabelBase[] = "stream_label"; |
| 37 | static const char kVideoTrackLabelBase[] = "video_track"; |
| 38 | static const char kAudioTrackLabelBase[] = "audio_track"; |
buildbot@webrtc.org | 3e01e0b | 2014-05-13 17:54:10 +0000 | [diff] [blame] | 39 | static const int kMaxWait = 10000; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 40 | static const int kTestAudioFrameCount = 3; |
| 41 | static const int kTestVideoFrameCount = 3; |
| 42 | |
| 43 | using webrtc::FakeConstraints; |
| 44 | using webrtc::FakeVideoTrackRenderer; |
| 45 | using webrtc::IceCandidateInterface; |
| 46 | using webrtc::MediaConstraintsInterface; |
| 47 | using webrtc::MediaStreamInterface; |
| 48 | using webrtc::MockSetSessionDescriptionObserver; |
| 49 | using webrtc::PeerConnectionInterface; |
| 50 | using webrtc::SessionDescriptionInterface; |
| 51 | using webrtc::VideoTrackInterface; |
| 52 | |
| 53 | void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller, |
| 54 | PeerConnectionTestWrapper* callee) { |
| 55 | caller->SignalOnIceCandidateReady.connect( |
| 56 | callee, &PeerConnectionTestWrapper::AddIceCandidate); |
| 57 | callee->SignalOnIceCandidateReady.connect( |
| 58 | caller, &PeerConnectionTestWrapper::AddIceCandidate); |
| 59 | |
| 60 | caller->SignalOnSdpReady.connect( |
| 61 | callee, &PeerConnectionTestWrapper::ReceiveOfferSdp); |
| 62 | callee->SignalOnSdpReady.connect( |
| 63 | caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp); |
| 64 | } |
| 65 | |
| 66 | PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name) |
| 67 | : name_(name) {} |
| 68 | |
| 69 | PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {} |
| 70 | |
| 71 | bool PeerConnectionTestWrapper::CreatePc( |
| 72 | const MediaConstraintsInterface* constraints) { |
| 73 | allocator_factory_ = webrtc::FakePortAllocatorFactory::Create(); |
| 74 | if (!allocator_factory_) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | audio_thread_.Start(); |
| 79 | fake_audio_capture_module_ = FakeAudioCaptureModule::Create( |
| 80 | &audio_thread_); |
| 81 | if (fake_audio_capture_module_ == NULL) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | peer_connection_factory_ = webrtc::CreatePeerConnectionFactory( |
| 86 | talk_base::Thread::Current(), talk_base::Thread::Current(), |
| 87 | fake_audio_capture_module_, NULL, NULL); |
| 88 | if (!peer_connection_factory_) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // CreatePeerConnection with IceServers. |
| 93 | webrtc::PeerConnectionInterface::IceServers ice_servers; |
| 94 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 95 | ice_server.uri = "stun:stun.l.google.com:19302"; |
| 96 | ice_servers.push_back(ice_server); |
buildbot@webrtc.org | 61c1b8e | 2014-04-09 06:06:38 +0000 | [diff] [blame] | 97 | FakeIdentityService* dtls_service = |
| 98 | talk_base::SSLStreamAdapter::HaveDtlsSrtp() ? |
| 99 | new FakeIdentityService() : NULL; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 100 | peer_connection_ = peer_connection_factory_->CreatePeerConnection( |
buildbot@webrtc.org | 61c1b8e | 2014-04-09 06:06:38 +0000 | [diff] [blame] | 101 | ice_servers, constraints, allocator_factory_.get(), dtls_service, this); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 102 | |
| 103 | return peer_connection_.get() != NULL; |
| 104 | } |
| 105 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame^] | 106 | talk_base::scoped_refptr<webrtc::DataChannelInterface> |
| 107 | PeerConnectionTestWrapper::CreateDataChannel( |
| 108 | const std::string& label, |
| 109 | const webrtc::DataChannelInit& init) { |
| 110 | return peer_connection_->CreateDataChannel(label, &init); |
| 111 | } |
| 112 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 113 | void PeerConnectionTestWrapper::OnAddStream(MediaStreamInterface* stream) { |
| 114 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 115 | << ": OnAddStream"; |
| 116 | // TODO(ronghuawu): support multiple streams. |
| 117 | if (stream->GetVideoTracks().size() > 0) { |
| 118 | renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0])); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void 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.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame^] | 132 | void PeerConnectionTestWrapper::OnDataChannel( |
| 133 | webrtc::DataChannelInterface* data_channel) { |
| 134 | SignalOnDataChannel(data_channel); |
| 135 | } |
| 136 | |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 137 | void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) { |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 138 | // This callback should take the ownership of |desc|. |
| 139 | talk_base::scoped_ptr<SessionDescriptionInterface> owned_desc(desc); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 140 | std::string sdp; |
| 141 | EXPECT_TRUE(desc->ToString(&sdp)); |
| 142 | |
| 143 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 144 | << ": " << desc->type() << " sdp created: " << sdp; |
| 145 | |
| 146 | // Give the user a chance to modify sdp for testing. |
| 147 | SignalOnSdpCreated(&sdp); |
| 148 | |
| 149 | SetLocalDescription(desc->type(), sdp); |
| 150 | |
| 151 | SignalOnSdpReady(sdp); |
| 152 | } |
| 153 | |
| 154 | void PeerConnectionTestWrapper::CreateOffer( |
| 155 | const MediaConstraintsInterface* constraints) { |
| 156 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 157 | << ": CreateOffer."; |
| 158 | peer_connection_->CreateOffer(this, constraints); |
| 159 | } |
| 160 | |
| 161 | void PeerConnectionTestWrapper::CreateAnswer( |
| 162 | const MediaConstraintsInterface* constraints) { |
| 163 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 164 | << ": CreateAnswer."; |
| 165 | peer_connection_->CreateAnswer(this, constraints); |
| 166 | } |
| 167 | |
| 168 | void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) { |
| 169 | SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp); |
| 170 | CreateAnswer(NULL); |
| 171 | } |
| 172 | |
| 173 | void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) { |
| 174 | SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp); |
| 175 | } |
| 176 | |
| 177 | void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type, |
| 178 | const std::string& sdp) { |
| 179 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 180 | << ": SetLocalDescription " << type << " " << sdp; |
| 181 | |
| 182 | talk_base::scoped_refptr<MockSetSessionDescriptionObserver> |
| 183 | observer(new talk_base::RefCountedObject< |
| 184 | MockSetSessionDescriptionObserver>()); |
| 185 | peer_connection_->SetLocalDescription( |
| 186 | observer, webrtc::CreateSessionDescription(type, sdp, NULL)); |
| 187 | } |
| 188 | |
| 189 | void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type, |
| 190 | const std::string& sdp) { |
| 191 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 192 | << ": SetRemoteDescription " << type << " " << sdp; |
| 193 | |
| 194 | talk_base::scoped_refptr<MockSetSessionDescriptionObserver> |
| 195 | observer(new talk_base::RefCountedObject< |
| 196 | MockSetSessionDescriptionObserver>()); |
| 197 | peer_connection_->SetRemoteDescription( |
| 198 | observer, webrtc::CreateSessionDescription(type, sdp, NULL)); |
| 199 | } |
| 200 | |
| 201 | void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid, |
| 202 | int sdp_mline_index, |
| 203 | const std::string& candidate) { |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 204 | talk_base::scoped_ptr<webrtc::IceCandidateInterface> owned_candidate( |
| 205 | webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL)); |
| 206 | EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get())); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void PeerConnectionTestWrapper::WaitForCallEstablished() { |
| 210 | WaitForConnection(); |
| 211 | WaitForAudio(); |
| 212 | WaitForVideo(); |
| 213 | } |
| 214 | |
| 215 | void PeerConnectionTestWrapper::WaitForConnection() { |
| 216 | EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait); |
| 217 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 218 | << ": Connected."; |
| 219 | } |
| 220 | |
| 221 | bool PeerConnectionTestWrapper::CheckForConnection() { |
| 222 | return (peer_connection_->ice_connection_state() == |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 223 | PeerConnectionInterface::kIceConnectionConnected) || |
| 224 | (peer_connection_->ice_connection_state() == |
| 225 | PeerConnectionInterface::kIceConnectionCompleted); |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void PeerConnectionTestWrapper::WaitForAudio() { |
| 229 | EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait); |
| 230 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 231 | << ": Got enough audio frames."; |
| 232 | } |
| 233 | |
| 234 | bool PeerConnectionTestWrapper::CheckForAudio() { |
| 235 | return (fake_audio_capture_module_->frames_received() >= |
| 236 | kTestAudioFrameCount); |
| 237 | } |
| 238 | |
| 239 | void PeerConnectionTestWrapper::WaitForVideo() { |
| 240 | EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait); |
| 241 | LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_ |
| 242 | << ": Got enough video frames."; |
| 243 | } |
| 244 | |
| 245 | bool PeerConnectionTestWrapper::CheckForVideo() { |
| 246 | if (!renderer_) { |
| 247 | return false; |
| 248 | } |
| 249 | return (renderer_->num_rendered_frames() >= kTestVideoFrameCount); |
| 250 | } |
| 251 | |
| 252 | void PeerConnectionTestWrapper::GetAndAddUserMedia( |
| 253 | bool audio, const webrtc::FakeConstraints& audio_constraints, |
| 254 | bool video, const webrtc::FakeConstraints& video_constraints) { |
| 255 | talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream = |
| 256 | GetUserMedia(audio, audio_constraints, video, video_constraints); |
| 257 | EXPECT_TRUE(peer_connection_->AddStream(stream, NULL)); |
| 258 | } |
| 259 | |
| 260 | talk_base::scoped_refptr<webrtc::MediaStreamInterface> |
| 261 | PeerConnectionTestWrapper::GetUserMedia( |
| 262 | bool audio, const webrtc::FakeConstraints& audio_constraints, |
| 263 | bool video, const webrtc::FakeConstraints& video_constraints) { |
| 264 | std::string label = kStreamLabelBase + |
| 265 | talk_base::ToString<int>( |
| 266 | static_cast<int>(peer_connection_->local_streams()->count())); |
| 267 | talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream = |
| 268 | peer_connection_factory_->CreateLocalMediaStream(label); |
| 269 | |
| 270 | if (audio) { |
| 271 | FakeConstraints constraints = audio_constraints; |
| 272 | // Disable highpass filter so that we can get all the test audio frames. |
| 273 | constraints.AddMandatory( |
| 274 | MediaConstraintsInterface::kHighpassFilter, false); |
| 275 | talk_base::scoped_refptr<webrtc::AudioSourceInterface> source = |
| 276 | peer_connection_factory_->CreateAudioSource(&constraints); |
| 277 | talk_base::scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
| 278 | peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase, |
| 279 | source)); |
| 280 | stream->AddTrack(audio_track); |
| 281 | } |
| 282 | |
| 283 | if (video) { |
| 284 | // Set max frame rate to 10fps to reduce the risk of the tests to be flaky. |
| 285 | FakeConstraints constraints = video_constraints; |
| 286 | constraints.SetMandatoryMaxFrameRate(10); |
| 287 | |
| 288 | talk_base::scoped_refptr<webrtc::VideoSourceInterface> source = |
| 289 | peer_connection_factory_->CreateVideoSource( |
| 290 | new webrtc::FakePeriodicVideoCapturer(), &constraints); |
| 291 | std::string videotrack_label = label + kVideoTrackLabelBase; |
| 292 | talk_base::scoped_refptr<webrtc::VideoTrackInterface> video_track( |
| 293 | peer_connection_factory_->CreateVideoTrack(videotrack_label, source)); |
| 294 | |
| 295 | stream->AddTrack(video_track); |
| 296 | } |
| 297 | return stream; |
| 298 | } |