Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/talk/app/webrtc/test/peerconnectiontestwrapper.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 10.9 KB
Line 
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"
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  // This callback should take the ownership of |desc|.
123  talk_base::scoped_ptr<SessionDescriptionInterface> owned_desc(desc);
124  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) {
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()));
191}
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() ==
207          PeerConnectionInterface::kIceConnectionConnected);
208}
209
210void PeerConnectionTestWrapper::WaitForAudio() {
211  EXPECT_TRUE_WAIT(CheckForAudio(), kMaxWait);
212  LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
213               << ": Got enough audio frames.";
214}
215
216bool PeerConnectionTestWrapper::CheckForAudio() {
217  return (fake_audio_capture_module_->frames_received() >=
218          kTestAudioFrameCount);
219}
220
221void PeerConnectionTestWrapper::WaitForVideo() {
222  EXPECT_TRUE_WAIT(CheckForVideo(), kMaxWait);
223  LOG(LS_INFO) << "PeerConnectionTestWrapper " << name_
224               << ": Got enough video frames.";
225}
226
227bool PeerConnectionTestWrapper::CheckForVideo() {
228  if (!renderer_) {
229    return false;
230  }
231  return (renderer_->num_rendered_frames() >= kTestVideoFrameCount);
232}
233
234void PeerConnectionTestWrapper::GetAndAddUserMedia(
235    bool audio, const webrtc::FakeConstraints& audio_constraints,
236    bool video, const webrtc::FakeConstraints& video_constraints) {
237  talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream =
238      GetUserMedia(audio, audio_constraints, video, video_constraints);
239  EXPECT_TRUE(peer_connection_->AddStream(stream, NULL));
240}
241
242talk_base::scoped_refptr<webrtc::MediaStreamInterface>
243    PeerConnectionTestWrapper::GetUserMedia(
244        bool audio, const webrtc::FakeConstraints& audio_constraints,
245        bool video, const webrtc::FakeConstraints& video_constraints) {
246  std::string label = kStreamLabelBase +
247      talk_base::ToString<int>(
248          static_cast<int>(peer_connection_->local_streams()->count()));
249  talk_base::scoped_refptr<webrtc::MediaStreamInterface> stream =
250      peer_connection_factory_->CreateLocalMediaStream(label);
251
252  if (audio) {
253    FakeConstraints constraints = audio_constraints;
254    // Disable highpass filter so that we can get all the test audio frames.
255    constraints.AddMandatory(
256        MediaConstraintsInterface::kHighpassFilter, false);
257    talk_base::scoped_refptr<webrtc::AudioSourceInterface> source =
258        peer_connection_factory_->CreateAudioSource(&constraints);
259    talk_base::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
260        peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
261                                                   source));
262    stream->AddTrack(audio_track);
263  }
264
265  if (video) {
266    // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
267    FakeConstraints constraints = video_constraints;
268    constraints.SetMandatoryMaxFrameRate(10);
269
270    talk_base::scoped_refptr<webrtc::VideoSourceInterface> source =
271        peer_connection_factory_->CreateVideoSource(
272            new webrtc::FakePeriodicVideoCapturer(), &constraints);
273    std::string videotrack_label = label + kVideoTrackLabelBase;
274    talk_base::scoped_refptr<webrtc::VideoTrackInterface> video_track(
275        peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
276
277    stream->AddTrack(video_track);
278  }
279  return stream;
280}
Note: See TracBrowser for help on using the repository browser.