blob: c22ecaf0ee84626288ddf160c2b99e4d87fc6df2 [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) {
122 std::string sdp;
123 EXPECT_TRUE(desc->ToString(&sdp));
124
125 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
126 << ": " << desc->type() << " sdp created: " << sdp;
127
128 // Give the user a chance to modify sdp for testing.
129 SignalOnSdpCreated(&sdp);
130
131 SetLocalDescription(desc->type(), sdp);
132
133 SignalOnSdpReady(sdp);
134}
135
136void PeerConnectionTestWrapper::CreateOffer(
137 const MediaConstraintsInterface* constraints) {
138 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
139 << ": CreateOffer.";
140 peer_connection_->CreateOffer(this, constraints);
141}
142
143void PeerConnectionTestWrapper::CreateAnswer(
144 const MediaConstraintsInterface* constraints) {
145 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
146 << ": CreateAnswer.";
147 peer_connection_->CreateAnswer(this, constraints);
148}
149
150void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
151 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
152 CreateAnswer(NULL);
153}
154
155void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
156 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
157}
158
159void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
160 const std::string& sdp) {
161 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
162 << ": SetLocalDescription " << type << " " << sdp;
163
164 talk_base::scoped_refptr<MockSetSessionDescriptionObserver>
165 observer(new talk_base::RefCountedObject<
166 MockSetSessionDescriptionObserver>());
167 peer_connection_->SetLocalDescription(
168 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
169}
170
171void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
172 const std::string& sdp) {
173 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
174 << ": SetRemoteDescription " << type << " " << sdp;
175
176 talk_base::scoped_refptr<MockSetSessionDescriptionObserver>
177 observer(new talk_base::RefCountedObject<
178 MockSetSessionDescriptionObserver>());
179 peer_connection_->SetRemoteDescription(
180 observer, webrtc::CreateSessionDescription(type, sdp, NULL));
181}
182
183void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
184 int sdp_mline_index,
185 const std::string& candidate) {
186 EXPECT_TRUE(peer_connection_->AddIceCandidate(
187 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index,
188 candidate, NULL)));
189}
190
191void PeerConnectionTestWrapper::WaitForCallEstablished() {
192 WaitForConnection();
193 WaitForAudio();
194 WaitForVideo();
195}
196
197void PeerConnectionTestWrapper::WaitForConnection() {
198 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
199 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
200 << ": Connected.";
201}
202
203bool PeerConnectionTestWrapper::CheckForConnection() {
204 return (peer_connection_->ice_connection_state() ==
205 PeerConnectionInterface::kIceConnectionConnected);
206}
207
208void PeerConnectionTestWrapper::WaitForAudio() {
209 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
210 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
211 << ": Got enough audio frames.";
212}
213
214bool PeerConnectionTestWrapper::CheckForAudio() {
215 return (fake_audio_capture_module_->frames_received() >=
216 kTestAudioFrameCount);
217}
218
219void PeerConnectionTestWrapper::WaitForVideo() {
220 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
221 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
222 << ": Got enough video frames.";
223}
224
225bool PeerConnectionTestWrapper::CheckForVideo() {
226 if (!renderer_) {
227 return false;
228 }
229 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
230}
231
232void PeerConnectionTestWrapper::GetAndAddUserMedia(
233 bool audio, const webrtc::FakeConstraints& audio_constraints,
234 bool video, const webrtc::FakeConstraints& video_constraints) {
235 talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream =
236 GetUserMedia(audio, audio_constraints, video, video_constraints);
237 EXPECT_TRUE(peer_connection_->AddStream(stream, NULL));
238}
239
240talk_base::scoped_refptr<webrtc::MediaStreamInterface>
241 PeerConnectionTestWrapper::GetUserMedia(
242 bool audio, const webrtc::FakeConstraints& audio_constraints,
243 bool video, const webrtc::FakeConstraints& video_constraints) {
244 std::string label = kStreamLabelBase +
245 talk_base::ToString<int>(
246 static_cast<int>(peer_connection_->local_streams()->count()));
247 talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream =
248 peer_connection_factory_->CreateLocalMediaStream(label);
249
250 if (audio) {
251 FakeConstraints constraints = audio_constraints;
252 // Disable highpass filter so that we can get all the test audio frames.
253 constraints.AddMandatory(
254 MediaConstraintsInterface::kHighpassFilter, false);
255 talk_base::scoped_refptr<webrtc::AudioSourceInterface> source =
256 peer_connection_factory_->CreateAudioSource(&constraints);
257 talk_base::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
258 peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
259 source));
260 stream->AddTrack(audio_track);
261 }
262
263 if (video) {
264 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
265 FakeConstraints constraints = video_constraints;
266 constraints.SetMandatoryMaxFrameRate(10);
267
268 talk_base::scoped_refptr<webrtc::VideoSourceInterface> source =
269 peer_connection_factory_->CreateVideoSource(
270 new webrtc::FakePeriodicVideoCapturer(), &constraints);
271 std::string videotrack_label = label + kVideoTrackLabelBase;
272 talk_base::scoped_refptr<webrtc::VideoTrackInterface> video_track(
273 peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
274
275 stream->AddTrack(video_track);
276 }
277 return stream;
278}