3 * Copyright 2013, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
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"
35 static const char kStreamLabelBase[] = "stream_label";
36 static const char kVideoTrackLabelBase[] = "video_track";
37 static const char kAudioTrackLabelBase[] = "audio_track";
38 static const int kMaxWait = 5000;
39 static const int kTestAudioFrameCount = 3;
40 static const int kTestVideoFrameCount = 3;
42 using webrtc::FakeConstraints;
43 using webrtc::FakeVideoTrackRenderer;
44 using webrtc::IceCandidateInterface;
45 using webrtc::MediaConstraintsInterface;
46 using webrtc::MediaStreamInterface;
47 using webrtc::MockSetSessionDescriptionObserver;
48 using webrtc::PeerConnectionInterface;
49 using webrtc::SessionDescriptionInterface;
50 using webrtc::VideoTrackInterface;
52 void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller,
53 PeerConnectionTestWrapper* callee) {
54 caller->SignalOnIceCandidateReady.connect(
55 callee, &PeerConnectionTestWrapper::AddIceCandidate);
56 callee->SignalOnIceCandidateReady.connect(
57 caller, &PeerConnectionTestWrapper::AddIceCandidate);
59 caller->SignalOnSdpReady.connect(
60 callee, &PeerConnectionTestWrapper::ReceiveOfferSdp);
61 callee->SignalOnSdpReady.connect(
62 caller, &PeerConnectionTestWrapper::ReceiveAnswerSdp);
65 PeerConnectionTestWrapper::PeerConnectionTestWrapper(const std::string& name)
68 PeerConnectionTestWrapper::~PeerConnectionTestWrapper() {}
70 bool PeerConnectionTestWrapper::CreatePc(
71 const MediaConstraintsInterface* constraints) {
72 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
73 if (!allocator_factory_) {
77 audio_thread_.Start();
78 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
80 if (fake_audio_capture_module_ == NULL) {
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_) {
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);
99 return peer_connection_.get() != NULL;
102 void PeerConnectionTestWrapper::OnAddStream(MediaStreamInterface* stream) {
103 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
105 // TODO(ronghuawu): support multiple streams.
106 if (stream->GetVideoTracks().size() > 0) {
107 renderer_.reset(new FakeVideoTrackRenderer(stream->GetVideoTracks()[0]));
111 void PeerConnectionTestWrapper::OnIceCandidate(
112 const IceCandidateInterface* candidate) {
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(),
121 void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
122 // This callback should take the ownership of |desc|.
123 talk_base::scoped_ptr<SessionDescriptionInterface> owned_desc(desc);
125 EXPECT_TRUE(desc->ToString(&sdp));
127 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
128 << ": " << desc->type() << " sdp created: " << sdp;
130 // Give the user a chance to modify sdp for testing.
131 SignalOnSdpCreated(&sdp);
133 SetLocalDescription(desc->type(), sdp);
135 SignalOnSdpReady(sdp);
138 void PeerConnectionTestWrapper::CreateOffer(
139 const MediaConstraintsInterface* constraints) {
140 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
142 peer_connection_->CreateOffer(this, constraints);
145 void PeerConnectionTestWrapper::CreateAnswer(
146 const MediaConstraintsInterface* constraints) {
147 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
148 << ": CreateAnswer.";
149 peer_connection_->CreateAnswer(this, constraints);
152 void PeerConnectionTestWrapper::ReceiveOfferSdp(const std::string& sdp) {
153 SetRemoteDescription(SessionDescriptionInterface::kOffer, sdp);
157 void PeerConnectionTestWrapper::ReceiveAnswerSdp(const std::string& sdp) {
158 SetRemoteDescription(SessionDescriptionInterface::kAnswer, sdp);
161 void PeerConnectionTestWrapper::SetLocalDescription(const std::string& type,
162 const std::string& sdp) {
163 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
164 << ": SetLocalDescription " << type << " " << sdp;
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));
173 void PeerConnectionTestWrapper::SetRemoteDescription(const std::string& type,
174 const std::string& sdp) {
175 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
176 << ": SetRemoteDescription " << type << " " << sdp;
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));
185 void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
187 const std::string& candidate) {
188 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()));
193 void PeerConnectionTestWrapper::WaitForCallEstablished() {
199 void PeerConnectionTestWrapper::WaitForConnection() {
200 EXPECT_TRUE_WAIT(CheckForConnection(), kMaxWait);
201 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
205 bool PeerConnectionTestWrapper::CheckForConnection() {
206 return (peer_connection_->ice_connection_state() ==
207 PeerConnectionInterface::kIceConnectionConnected) ||
208 (peer_connection_->ice_connection_state() ==
209 PeerConnectionInterface::kIceConnectionCompleted);
212 void PeerConnectionTestWrapper::WaitForAudio() {
213 EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
214 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
215 << ": Got enough audio frames.";
218 bool PeerConnectionTestWrapper::CheckForAudio() {
219 return (fake_audio_capture_module_->frames_received() >=
220 kTestAudioFrameCount);
223 void PeerConnectionTestWrapper::WaitForVideo() {
224 EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
225 LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
226 << ": Got enough video frames.";
229 bool PeerConnectionTestWrapper::CheckForVideo() {
233 return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
236 void 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));
244 talk_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);
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,
264 stream->AddTrack(audio_track);
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);
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));
279 stream->AddTrack(video_track);