blob: ca4b6d2f9b390203ee9bca3e4f1f3d0be336765e [file] [log] [blame]
wu@webrtc.org364f2042013-11-20 21:49:41 +00001/*
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"
29#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
30#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
31#include "talk/app/webrtc/test/peerconnectiontestwrapper.h"
32#include "talk/app/webrtc/videosourceinterface.h"
33#include "talk/base/gunit.h"
34
35static const char kStreamLabelBase[] = "stream_label";
36static const char kVideoTrackLabelBase[] = "video_track";
37static const char kAudioTrackLabelBase[] = "audio_track";
38static const int kMaxWait = 5000;
39static const int kTestAudioFrameCount = 3;
40static const int kTestVideoFrameCount = 3;
41
42using webrtc::FakeConstraints;
43using webrtc::FakeVideoTrackRenderer;
44using webrtc::IceCandidateInterface;
45using webrtc::MediaConstraintsInterface;
46using webrtc::MediaStreamInterface;
47using webrtc::MockSetSessionDescriptionObserver;
48using webrtc::PeerConnectionInterface;
49using webrtc::SessionDescriptionInterface;
50using webrtc::VideoTrackInterface;
51
52void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
53 PeerConnectionTestWrapper* callee) {
54 caller->SignalOnIceCandidateReady.connect(
55 callee, &PeerConnectionTestWrapper::AddIceCandidate);
56 callee->SignalOnIceCandidateReady.connect(
57 caller, &PeerConnectionTestWrapper::AddIceCandidate);
58
59 caller->SignalOnSdpReady.connect(
60 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
61 callee->SignalOnSdpReady.connect(
62 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
63}
64
65PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name)
66 : name_(name) {}
67
68PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
69
70bool PeerConnectionTestWrapper::CreatePc(
71 const MediaConstraintsInterface* constraints) {
72 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
73 if (!allocator_factory_) {
74 return false;
75 }
76
77 audio_thread_.Start();
78 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
79 &audio_thread_);
80 if (fake_audio_capture_module_ == NULL) {
81 return false;
82 }
83
84 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
85 talk_base::Thread::Current(), talk_base::Thread::Current(),
86 fake_audio_capture_module_, NULL, NULL);
87 if (!peer_connection_factory_) {
88 return false;
89 }
90
91 // CreatePeerConnection with IceServers.
92 webrtc::PeerConnectionInterface::IceServers ice_servers;
93 webrtc::PeerConnectionInterface::IceServer ice_server;
94 ice_server.uri = "stun:stun.l.google.com:19302";
95 ice_servers.push_back(ice_server);
96 peer_connection_ = peer_connection_factory_->CreatePeerConnection(
97 ice_servers, constraints, allocator_factory_.get(), NULL, this);
98
99 return peer_connection_.get() != NULL;
100}
101
102void PeerConnectionTestWrapper::OnAddStream(MediaStreamInterface* stream) {
103 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
104 << ": OnAddStream";
105 // TODO(ronghuawu): support multiple streams.
106 if (stream->GetVideoTracks().size() > 0) {
107 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
108 }
109}
110
111void PeerConnectionTestWrapper::OnIceCandidate(
112 const IceCandidateInterface* candidate) {
113 std::string sdp;
114 EXPECT_TRUE(candidate->ToString(&sdp));
115 // Give the user a chance to modify sdp for testing.
116 SignalOnIceCandidateCreated(&sdp);
117 SignalOnIceCandidateReady(candidate->sdp_mid(), candidate->sdp_mline_index(),
118 sdp);
119}
120
121void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000122 // This callback should take the ownership of |desc|.
123 talk_base::scoped_ptr<SessionDescriptionInterface> owned_desc(desc);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000124 std::string sdp;
125 EXPECT_TRUE(desc->ToString(&sdp));
126
127 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
128 << ": " << desc->type() << " sdp created: " << sdp;
129
130 // Give the user a chance to modify sdp for testing.
131 SignalOnSdpCreated(&sdp);
132
133 SetLocalDescription(desc->type(), sdp);
134
135 SignalOnSdpReady(sdp);
136}
137
138void PeerConnectionTestWrapper::CreateOffer(
139 const MediaConstraintsInterface* constraints) {
140 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
141 << ": CreateOffer.";
142 peer_connection_->CreateOffer(this, constraints);
143}
144
145void PeerConnectionTestWrapper::CreateAnswer(
146 const MediaConstraintsInterface* constraints) {
147 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
148 << ": CreateAnswer.";
149 peer_connection_->CreateAnswer(this, constraints);
150}
151
152void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
153 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
154 CreateAnswer(NULL);
155}
156
157void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
158 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
159}
160
161void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
162 const std::string& sdp) {
163 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
164 << ": SetLocalDescription " << type << " " << sdp;
165
166 talk_base::scoped_refptr<MockSetSessionDescriptionObserver>
167 observer(new talk_base::RefCountedObject<
168 MockSetSessionDescriptionObserver>());
169 peer_connection_->SetLocalDescription(
170 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
171}
172
173void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
174 const std::string& sdp) {
175 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
176 << ": SetRemoteDescription " << type << " " << sdp;
177
178 talk_base::scoped_refptr<MockSetSessionDescriptionObserver>
179 observer(new talk_base::RefCountedObject<
180 MockSetSessionDescriptionObserver>());
181 peer_connection_->SetRemoteDescription(
182 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
183}
184
185void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
186 int sdp_mline_index,
187 const std::string& candidate) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000188 talk_base::scoped_ptr<webrtc::IceCandidateInterface> owned_candidate(
189 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, candidate, NULL));
190 EXPECT_TRUE(peer_connection_->AddIceCandidate(owned_candidate.get()));
wu@webrtc.org364f2042013-11-20 21:49:41 +0000191}
192
193void PeerConnectionTestWrapper::WaitForCallEstablished() {
194 WaitForConnection();
195 WaitForAudio();
196 WaitForVideo();
197}
198
199void PeerConnectionTestWrapper::WaitForConnection() {
200 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
201 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
202 << ": Connected.";
203}
204
205bool PeerConnectionTestWrapper::CheckForConnection() {
206 return (peer_connection_->ice_connection_state() ==
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000207 PeerConnectionInterface::kIceConnectionConnected) ||
208 (peer_connection_->ice_connection_state() ==
209 PeerConnectionInterface::kIceConnectionCompleted);
wu@webrtc.org364f2042013-11-20 21:49:41 +0000210}
211
212void PeerConnectionTestWrapper::WaitForAudio() {
213 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
214 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
215 << ": Got enough audio frames.";
216}
217
218bool PeerConnectionTestWrapper::CheckForAudio() {
219 return (fake_audio_capture_module_->frames_received() >=
220 kTestAudioFrameCount);
221}
222
223void PeerConnectionTestWrapper::WaitForVideo() {
224 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
225 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
226 << ": Got enough video frames.";
227}
228
229bool PeerConnectionTestWrapper::CheckForVideo() {
230 if (!renderer_) {
231 return false;
232 }
233 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
234}
235
236void PeerConnectionTestWrapper::GetAndAddUserMedia(
237 bool audio, const webrtc::FakeConstraints& audio_constraints,
238 bool video, const webrtc::FakeConstraints& video_constraints) {
239 talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream =
240 GetUserMedia(audio, audio_constraints, video, video_constraints);
241 EXPECT_TRUE(peer_connection_->AddStream(stream, NULL));
242}
243
244talk_base::scoped_refptr<webrtc::MediaStreamInterface>
245 PeerConnectionTestWrapper::GetUserMedia(
246 bool audio, const webrtc::FakeConstraints& audio_constraints,
247 bool video, const webrtc::FakeConstraints& video_constraints) {
248 std::string label = kStreamLabelBase +
249 talk_base::ToString<int>(
250 static_cast<int>(peer_connection_->local_streams()->count()));
251 talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream =
252 peer_connection_factory_->CreateLocalMediaStream(label);
253
254 if (audio) {
255 FakeConstraints constraints = audio_constraints;
256 // Disable highpass filter so that we can get all the test audio frames.
257 constraints.AddMandatory(
258 MediaConstraintsInterface::kHighpassFilter, false);
259 talk_base::scoped_refptr<webrtc::AudioSourceInterface> source =
260 peer_connection_factory_->CreateAudioSource(&constraints);
261 talk_base::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
262 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
263 source));
264 stream->AddTrack(audio_track);
265 }
266
267 if (video) {
268 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
269 FakeConstraints constraints = video_constraints;
270 constraints.SetMandatoryMaxFrameRate(10);
271
272 talk_base::scoped_refptr<webrtc::VideoSourceInterface> source =
273 peer_connection_factory_->CreateVideoSource(
274 new webrtc::FakePeriodicVideoCapturer(), &constraints);
275 std::string videotrack_label = label + kVideoTrackLabelBase;
276 talk_base::scoped_refptr<webrtc::VideoTrackInterface> video_track(
277 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
278
279 stream->AddTrack(video_track);
280 }
281 return stream;
282}