blob: 121cc64144ff39ab0ea9b0ad5dc2459965f347cb [file] [log] [blame]
Steve Anton94286cb2017-09-26 16:20:19 -07001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "pc/peerconnectionwrapper.h"
12
13#include <memory>
14#include <string>
15#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070016#include <vector>
Steve Anton94286cb2017-09-26 16:20:19 -070017
18#include "api/jsepsessiondescription.h"
19#include "media/base/fakevideocapturer.h"
Steve Anton97a9f762017-10-06 10:14:03 -070020#include "pc/sdputils.h"
Mirko Bonadei2a823102017-11-13 11:50:33 +010021#include "rtc_base/function_view.h"
Steve Anton94286cb2017-09-26 16:20:19 -070022#include "rtc_base/gunit.h"
23#include "rtc_base/ptr_util.h"
24
25namespace webrtc {
26
27namespace {
Steve Anton6f25b092017-10-23 09:39:20 -070028const uint32_t kDefaultTimeout = 10000U;
Steve Anton94286cb2017-09-26 16:20:19 -070029}
30
31PeerConnectionWrapper::PeerConnectionWrapper(
32 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
33 rtc::scoped_refptr<PeerConnectionInterface> pc,
34 std::unique_ptr<MockPeerConnectionObserver> observer)
Mirko Bonadei2a823102017-11-13 11:50:33 +010035 : pc_factory_(std::move(pc_factory)),
36 observer_(std::move(observer)),
37 pc_(std::move(pc)) {
Steve Anton94286cb2017-09-26 16:20:19 -070038 RTC_DCHECK(pc_factory_);
39 RTC_DCHECK(pc_);
40 RTC_DCHECK(observer_);
41 observer_->SetPeerConnectionInterface(pc_.get());
42}
43
44PeerConnectionWrapper::~PeerConnectionWrapper() = default;
45
46PeerConnectionFactoryInterface* PeerConnectionWrapper::pc_factory() {
47 return pc_factory_.get();
48}
49
50PeerConnectionInterface* PeerConnectionWrapper::pc() {
51 return pc_.get();
52}
53
54MockPeerConnectionObserver* PeerConnectionWrapper::observer() {
55 return observer_.get();
56}
57
58std::unique_ptr<SessionDescriptionInterface>
59PeerConnectionWrapper::CreateOffer() {
60 return CreateOffer(PeerConnectionInterface::RTCOfferAnswerOptions());
61}
62
63std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateOffer(
Steve Anton8d3444d2017-10-20 15:30:51 -070064 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
65 std::string* error_out) {
66 return CreateSdp(
67 [this, options](CreateSessionDescriptionObserver* observer) {
68 pc()->CreateOffer(observer, options);
69 },
70 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -070071}
72
73std::unique_ptr<SessionDescriptionInterface>
74PeerConnectionWrapper::CreateOfferAndSetAsLocal() {
Steve Anton8d3444d2017-10-20 15:30:51 -070075 return CreateOfferAndSetAsLocal(
76 PeerConnectionInterface::RTCOfferAnswerOptions());
77}
78
79std::unique_ptr<SessionDescriptionInterface>
80PeerConnectionWrapper::CreateOfferAndSetAsLocal(
81 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
82 auto offer = CreateOffer(options);
Steve Anton94286cb2017-09-26 16:20:19 -070083 if (!offer) {
84 return nullptr;
85 }
86 EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer.get())));
87 return offer;
88}
89
90std::unique_ptr<SessionDescriptionInterface>
91PeerConnectionWrapper::CreateAnswer() {
92 return CreateAnswer(PeerConnectionInterface::RTCOfferAnswerOptions());
93}
94
95std::unique_ptr<SessionDescriptionInterface>
96PeerConnectionWrapper::CreateAnswer(
Steve Anton8d3444d2017-10-20 15:30:51 -070097 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
98 std::string* error_out) {
99 return CreateSdp(
100 [this, options](CreateSessionDescriptionObserver* observer) {
101 pc()->CreateAnswer(observer, options);
102 },
103 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700104}
105
106std::unique_ptr<SessionDescriptionInterface>
107PeerConnectionWrapper::CreateAnswerAndSetAsLocal() {
Steve Anton8d3444d2017-10-20 15:30:51 -0700108 return CreateAnswerAndSetAsLocal(
109 PeerConnectionInterface::RTCOfferAnswerOptions());
110}
111
112std::unique_ptr<SessionDescriptionInterface>
113PeerConnectionWrapper::CreateAnswerAndSetAsLocal(
114 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
115 auto answer = CreateAnswer(options);
Steve Anton94286cb2017-09-26 16:20:19 -0700116 if (!answer) {
117 return nullptr;
118 }
119 EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(answer.get())));
120 return answer;
121}
122
123std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100124 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700125 std::string* error_out) {
Steve Anton94286cb2017-09-26 16:20:19 -0700126 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
127 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
128 fn(observer);
Steve Anton6f25b092017-10-23 09:39:20 -0700129 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
Steve Anton8d3444d2017-10-20 15:30:51 -0700130 if (error_out && !observer->result()) {
131 *error_out = observer->error();
132 }
Steve Anton94286cb2017-09-26 16:20:19 -0700133 return observer->MoveDescription();
134}
135
136bool PeerConnectionWrapper::SetLocalDescription(
Steve Anton8d3444d2017-10-20 15:30:51 -0700137 std::unique_ptr<SessionDescriptionInterface> desc,
138 std::string* error_out) {
139 return SetSdp(
140 [this, &desc](SetSessionDescriptionObserver* observer) {
141 pc()->SetLocalDescription(observer, desc.release());
142 },
143 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700144}
145
146bool PeerConnectionWrapper::SetRemoteDescription(
Steve Anton8d3444d2017-10-20 15:30:51 -0700147 std::unique_ptr<SessionDescriptionInterface> desc,
148 std::string* error_out) {
149 return SetSdp(
150 [this, &desc](SetSessionDescriptionObserver* observer) {
151 pc()->SetRemoteDescription(observer, desc.release());
152 },
153 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700154}
155
Henrik Boström31638672017-11-23 17:48:32 +0100156bool PeerConnectionWrapper::SetRemoteDescription(
157 std::unique_ptr<SessionDescriptionInterface> desc,
158 RTCError* error_out) {
159 rtc::scoped_refptr<MockSetRemoteDescriptionObserver> observer =
160 new MockSetRemoteDescriptionObserver();
161 pc()->SetRemoteDescription(std::move(desc), observer);
162 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
163 bool ok = observer->error().ok();
164 if (error_out)
165 *error_out = std::move(observer->error());
166 return ok;
167}
168
Steve Anton94286cb2017-09-26 16:20:19 -0700169bool PeerConnectionWrapper::SetSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100170 rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700171 std::string* error_out) {
Steve Anton94286cb2017-09-26 16:20:19 -0700172 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
173 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
174 fn(observer);
Steve Anton6f25b092017-10-23 09:39:20 -0700175 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
Steve Anton8d3444d2017-10-20 15:30:51 -0700176 if (error_out && !observer->result()) {
177 *error_out = observer->error();
Steve Anton94286cb2017-09-26 16:20:19 -0700178 }
179 return observer->result();
180}
181
Steve Anton9158ef62017-11-27 13:01:52 -0800182rtc::scoped_refptr<RtpTransceiverInterface>
183PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type) {
184 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
185 pc()->AddTransceiver(media_type);
186 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
187 return result.MoveValue();
188}
189
190rtc::scoped_refptr<RtpTransceiverInterface>
191PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type,
192 const RtpTransceiverInit& init) {
193 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
194 pc()->AddTransceiver(media_type, init);
195 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
196 return result.MoveValue();
197}
198
199rtc::scoped_refptr<RtpTransceiverInterface>
200PeerConnectionWrapper::AddTransceiver(
201 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
202 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
203 pc()->AddTransceiver(track);
204 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
205 return result.MoveValue();
206}
207
208rtc::scoped_refptr<RtpTransceiverInterface>
209PeerConnectionWrapper::AddTransceiver(
210 rtc::scoped_refptr<MediaStreamTrackInterface> track,
211 const RtpTransceiverInit& init) {
212 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
213 pc()->AddTransceiver(track, init);
214 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
215 return result.MoveValue();
216}
217
218rtc::scoped_refptr<AudioTrackInterface> PeerConnectionWrapper::CreateAudioTrack(
219 const std::string& label) {
220 return pc_factory()->CreateAudioTrack(label, nullptr);
221}
222
223rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack(
224 const std::string& label) {
225 auto video_source = pc_factory()->CreateVideoSource(
226 rtc::MakeUnique<cricket::FakeVideoCapturer>());
227 return pc_factory()->CreateVideoTrack(label, video_source);
228}
229
Steve Anton8d3444d2017-10-20 15:30:51 -0700230rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack(
231 const std::string& track_label,
232 std::vector<MediaStreamInterface*> streams) {
Steve Anton9158ef62017-11-27 13:01:52 -0800233 return pc()->AddTrack(CreateAudioTrack(track_label), std::move(streams));
Steve Anton94286cb2017-09-26 16:20:19 -0700234}
235
Steve Anton8d3444d2017-10-20 15:30:51 -0700236rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack(
237 const std::string& track_label,
238 std::vector<MediaStreamInterface*> streams) {
Steve Anton9158ef62017-11-27 13:01:52 -0800239 return pc()->AddTrack(CreateVideoTrack(track_label), std::move(streams));
Steve Anton94286cb2017-09-26 16:20:19 -0700240}
241
Steve Anton8d3444d2017-10-20 15:30:51 -0700242PeerConnectionInterface::SignalingState
243PeerConnectionWrapper::signaling_state() {
244 return pc()->signaling_state();
Steve Anton94286cb2017-09-26 16:20:19 -0700245}
246
Steve Antonf1c6db12017-10-13 11:13:35 -0700247bool PeerConnectionWrapper::IsIceGatheringDone() {
Steve Anton6f25b092017-10-23 09:39:20 -0700248 return observer()->ice_gathering_complete_;
249}
250
251bool PeerConnectionWrapper::IsIceConnected() {
252 return observer()->ice_connected_;
253}
254
255rtc::scoped_refptr<const webrtc::RTCStatsReport>
256PeerConnectionWrapper::GetStats() {
257 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
258 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
259 pc()->GetStats(callback);
260 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
261 return callback->report();
Steve Antonf1c6db12017-10-13 11:13:35 -0700262}
263
Steve Anton94286cb2017-09-26 16:20:19 -0700264} // namespace webrtc