blob: 6bfb59f935ea7b68e13e1f0ace68bb5c3943f00a [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/peer_connection_wrapper.h"
Steve Anton94286cb2017-09-26 16:20:19 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdint.h>
Steve Anton94286cb2017-09-26 16:20:19 -070014#include <memory>
15#include <string>
16#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070017#include <vector>
Steve Anton94286cb2017-09-26 16:20:19 -070018
Steve Anton10542f22019-01-11 09:11:00 -080019#include "api/set_remote_description_observer_interface.h"
20#include "pc/sdp_utils.h"
21#include "pc/test/fake_video_track_source.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/checks.h"
Mirko Bonadei2a823102017-11-13 11:50:33 +010023#include "rtc_base/function_view.h"
Steve Anton94286cb2017-09-26 16:20:19 -070024#include "rtc_base/gunit.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/ref_counted_object.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "test/gtest.h"
Steve Anton94286cb2017-09-26 16:20:19 -070028
29namespace webrtc {
30
Steve Anton22da89f2018-01-25 13:58:07 -080031using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
32
Steve Anton94286cb2017-09-26 16:20:19 -070033namespace {
Steve Anton6f25b092017-10-23 09:39:20 -070034const uint32_t kDefaultTimeout = 10000U;
Steve Anton94286cb2017-09-26 16:20:19 -070035}
36
37PeerConnectionWrapper::PeerConnectionWrapper(
38 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
39 rtc::scoped_refptr<PeerConnectionInterface> pc,
40 std::unique_ptr<MockPeerConnectionObserver> observer)
Mirko Bonadei2a823102017-11-13 11:50:33 +010041 : pc_factory_(std::move(pc_factory)),
42 observer_(std::move(observer)),
43 pc_(std::move(pc)) {
Steve Anton94286cb2017-09-26 16:20:19 -070044 RTC_DCHECK(pc_factory_);
45 RTC_DCHECK(pc_);
46 RTC_DCHECK(observer_);
47 observer_->SetPeerConnectionInterface(pc_.get());
48}
49
50PeerConnectionWrapper::~PeerConnectionWrapper() = default;
51
52PeerConnectionFactoryInterface* PeerConnectionWrapper::pc_factory() {
53 return pc_factory_.get();
54}
55
56PeerConnectionInterface* PeerConnectionWrapper::pc() {
57 return pc_.get();
58}
59
60MockPeerConnectionObserver* PeerConnectionWrapper::observer() {
61 return observer_.get();
62}
63
64std::unique_ptr<SessionDescriptionInterface>
65PeerConnectionWrapper::CreateOffer() {
Steve Anton22da89f2018-01-25 13:58:07 -080066 return CreateOffer(RTCOfferAnswerOptions());
Steve Anton94286cb2017-09-26 16:20:19 -070067}
68
69std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateOffer(
Steve Anton8d3444d2017-10-20 15:30:51 -070070 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
71 std::string* error_out) {
72 return CreateSdp(
73 [this, options](CreateSessionDescriptionObserver* observer) {
74 pc()->CreateOffer(observer, options);
75 },
76 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -070077}
78
79std::unique_ptr<SessionDescriptionInterface>
80PeerConnectionWrapper::CreateOfferAndSetAsLocal() {
Steve Anton22da89f2018-01-25 13:58:07 -080081 return CreateOfferAndSetAsLocal(RTCOfferAnswerOptions());
Steve Anton8d3444d2017-10-20 15:30:51 -070082}
83
84std::unique_ptr<SessionDescriptionInterface>
85PeerConnectionWrapper::CreateOfferAndSetAsLocal(
86 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
87 auto offer = CreateOffer(options);
Steve Anton94286cb2017-09-26 16:20:19 -070088 if (!offer) {
89 return nullptr;
90 }
91 EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer.get())));
92 return offer;
93}
94
95std::unique_ptr<SessionDescriptionInterface>
96PeerConnectionWrapper::CreateAnswer() {
Steve Anton22da89f2018-01-25 13:58:07 -080097 return CreateAnswer(RTCOfferAnswerOptions());
Steve Anton94286cb2017-09-26 16:20:19 -070098}
99
100std::unique_ptr<SessionDescriptionInterface>
101PeerConnectionWrapper::CreateAnswer(
Steve Anton8d3444d2017-10-20 15:30:51 -0700102 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
103 std::string* error_out) {
104 return CreateSdp(
105 [this, options](CreateSessionDescriptionObserver* observer) {
106 pc()->CreateAnswer(observer, options);
107 },
108 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700109}
110
111std::unique_ptr<SessionDescriptionInterface>
112PeerConnectionWrapper::CreateAnswerAndSetAsLocal() {
Steve Anton22da89f2018-01-25 13:58:07 -0800113 return CreateAnswerAndSetAsLocal(RTCOfferAnswerOptions());
Steve Anton8d3444d2017-10-20 15:30:51 -0700114}
115
116std::unique_ptr<SessionDescriptionInterface>
117PeerConnectionWrapper::CreateAnswerAndSetAsLocal(
118 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
119 auto answer = CreateAnswer(options);
Steve Anton94286cb2017-09-26 16:20:19 -0700120 if (!answer) {
121 return nullptr;
122 }
123 EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(answer.get())));
124 return answer;
125}
126
127std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100128 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700129 std::string* error_out) {
Steve Anton94286cb2017-09-26 16:20:19 -0700130 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
131 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
132 fn(observer);
Steve Anton6f25b092017-10-23 09:39:20 -0700133 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
Steve Anton8d3444d2017-10-20 15:30:51 -0700134 if (error_out && !observer->result()) {
135 *error_out = observer->error();
136 }
Steve Anton94286cb2017-09-26 16:20:19 -0700137 return observer->MoveDescription();
138}
139
140bool PeerConnectionWrapper::SetLocalDescription(
Steve Anton8d3444d2017-10-20 15:30:51 -0700141 std::unique_ptr<SessionDescriptionInterface> desc,
142 std::string* error_out) {
143 return SetSdp(
144 [this, &desc](SetSessionDescriptionObserver* observer) {
145 pc()->SetLocalDescription(observer, desc.release());
146 },
147 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700148}
149
150bool PeerConnectionWrapper::SetRemoteDescription(
Steve Anton8d3444d2017-10-20 15:30:51 -0700151 std::unique_ptr<SessionDescriptionInterface> desc,
152 std::string* error_out) {
153 return SetSdp(
154 [this, &desc](SetSessionDescriptionObserver* observer) {
155 pc()->SetRemoteDescription(observer, desc.release());
156 },
157 error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700158}
159
Henrik Boström31638672017-11-23 17:48:32 +0100160bool PeerConnectionWrapper::SetRemoteDescription(
161 std::unique_ptr<SessionDescriptionInterface> desc,
162 RTCError* error_out) {
163 rtc::scoped_refptr<MockSetRemoteDescriptionObserver> observer =
164 new MockSetRemoteDescriptionObserver();
165 pc()->SetRemoteDescription(std::move(desc), observer);
166 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
167 bool ok = observer->error().ok();
168 if (error_out)
169 *error_out = std::move(observer->error());
170 return ok;
171}
172
Steve Anton94286cb2017-09-26 16:20:19 -0700173bool PeerConnectionWrapper::SetSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100174 rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700175 std::string* error_out) {
Steve Anton94286cb2017-09-26 16:20:19 -0700176 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
177 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
178 fn(observer);
Steve Anton6f25b092017-10-23 09:39:20 -0700179 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
Steve Anton8d3444d2017-10-20 15:30:51 -0700180 if (error_out && !observer->result()) {
181 *error_out = observer->error();
Steve Anton94286cb2017-09-26 16:20:19 -0700182 }
183 return observer->result();
184}
185
Steve Antondcc3c022017-12-22 16:02:54 -0800186bool PeerConnectionWrapper::ExchangeOfferAnswerWith(
187 PeerConnectionWrapper* answerer) {
Steve Anton22da89f2018-01-25 13:58:07 -0800188 return ExchangeOfferAnswerWith(answerer, RTCOfferAnswerOptions(),
189 RTCOfferAnswerOptions());
190}
191
192bool PeerConnectionWrapper::ExchangeOfferAnswerWith(
193 PeerConnectionWrapper* answerer,
194 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
195 const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options) {
Steve Antondcc3c022017-12-22 16:02:54 -0800196 RTC_DCHECK(answerer);
197 if (answerer == this) {
198 RTC_LOG(LS_ERROR) << "Cannot exchange offer/answer with ourself!";
199 return false;
200 }
Steve Anton22da89f2018-01-25 13:58:07 -0800201 auto offer = CreateOffer(offer_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800202 EXPECT_TRUE(offer);
203 if (!offer) {
204 return false;
205 }
206 bool set_local_offer =
207 SetLocalDescription(CloneSessionDescription(offer.get()));
208 EXPECT_TRUE(set_local_offer);
209 if (!set_local_offer) {
210 return false;
211 }
212 bool set_remote_offer = answerer->SetRemoteDescription(std::move(offer));
213 EXPECT_TRUE(set_remote_offer);
214 if (!set_remote_offer) {
215 return false;
216 }
Steve Anton22da89f2018-01-25 13:58:07 -0800217 auto answer = answerer->CreateAnswer(answer_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800218 EXPECT_TRUE(answer);
219 if (!answer) {
220 return false;
221 }
222 bool set_local_answer =
223 answerer->SetLocalDescription(CloneSessionDescription(answer.get()));
224 EXPECT_TRUE(set_local_answer);
225 if (!set_local_answer) {
226 return false;
227 }
228 bool set_remote_answer = SetRemoteDescription(std::move(answer));
229 EXPECT_TRUE(set_remote_answer);
230 return set_remote_answer;
231}
232
Steve Anton9158ef62017-11-27 13:01:52 -0800233rtc::scoped_refptr<RtpTransceiverInterface>
234PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type) {
235 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
236 pc()->AddTransceiver(media_type);
237 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
238 return result.MoveValue();
239}
240
241rtc::scoped_refptr<RtpTransceiverInterface>
242PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type,
243 const RtpTransceiverInit& init) {
244 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
245 pc()->AddTransceiver(media_type, init);
246 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
247 return result.MoveValue();
248}
249
250rtc::scoped_refptr<RtpTransceiverInterface>
251PeerConnectionWrapper::AddTransceiver(
252 rtc::scoped_refptr<MediaStreamTrackInterface> track) {
253 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
254 pc()->AddTransceiver(track);
255 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
256 return result.MoveValue();
257}
258
259rtc::scoped_refptr<RtpTransceiverInterface>
260PeerConnectionWrapper::AddTransceiver(
261 rtc::scoped_refptr<MediaStreamTrackInterface> track,
262 const RtpTransceiverInit& init) {
263 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
264 pc()->AddTransceiver(track, init);
265 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
266 return result.MoveValue();
267}
268
269rtc::scoped_refptr<AudioTrackInterface> PeerConnectionWrapper::CreateAudioTrack(
270 const std::string& label) {
271 return pc_factory()->CreateAudioTrack(label, nullptr);
272}
273
274rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack(
275 const std::string& label) {
Niels Möllere8ae5df2018-05-29 09:13:57 +0200276 return pc_factory()->CreateVideoTrack(label, FakeVideoTrackSource::Create());
Steve Anton9158ef62017-11-27 13:01:52 -0800277}
278
Steve Anton2d6c76a2018-01-05 17:10:52 -0800279rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddTrack(
280 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800281 const std::vector<std::string>& stream_ids) {
Steve Anton2d6c76a2018-01-05 17:10:52 -0800282 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> result =
Seth Hampson845e8782018-03-02 11:34:10 -0800283 pc()->AddTrack(track, stream_ids);
Steve Anton2d6c76a2018-01-05 17:10:52 -0800284 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
285 return result.MoveValue();
286}
287
Steve Anton8d3444d2017-10-20 15:30:51 -0700288rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack(
289 const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800290 const std::vector<std::string>& stream_ids) {
291 return AddTrack(CreateAudioTrack(track_label), stream_ids);
Steve Anton94286cb2017-09-26 16:20:19 -0700292}
293
Steve Anton8d3444d2017-10-20 15:30:51 -0700294rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack(
295 const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800296 const std::vector<std::string>& stream_ids) {
297 return AddTrack(CreateVideoTrack(track_label), stream_ids);
Steve Anton94286cb2017-09-26 16:20:19 -0700298}
299
Steve Antonfa2260d2017-12-28 16:38:23 -0800300rtc::scoped_refptr<DataChannelInterface>
301PeerConnectionWrapper::CreateDataChannel(const std::string& label) {
302 return pc()->CreateDataChannel(label, nullptr);
303}
304
Steve Anton8d3444d2017-10-20 15:30:51 -0700305PeerConnectionInterface::SignalingState
306PeerConnectionWrapper::signaling_state() {
307 return pc()->signaling_state();
Steve Anton94286cb2017-09-26 16:20:19 -0700308}
309
Steve Antonf1c6db12017-10-13 11:13:35 -0700310bool PeerConnectionWrapper::IsIceGatheringDone() {
Steve Anton6f25b092017-10-23 09:39:20 -0700311 return observer()->ice_gathering_complete_;
312}
313
314bool PeerConnectionWrapper::IsIceConnected() {
315 return observer()->ice_connected_;
316}
317
318rtc::scoped_refptr<const webrtc::RTCStatsReport>
319PeerConnectionWrapper::GetStats() {
320 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
321 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
322 pc()->GetStats(callback);
323 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
324 return callback->report();
Steve Antonf1c6db12017-10-13 11:13:35 -0700325}
326
Steve Anton94286cb2017-09-26 16:20:19 -0700327} // namespace webrtc