2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
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.
11 #include "pc/peer_connection_wrapper.h"
18 #include "absl/types/optional.h"
19 #include "api/function_view.h"
20 #include "api/set_remote_description_observer_interface.h"
21 #include "pc/sdp_utils.h"
22 #include "pc/test/fake_video_track_source.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/gunit.h"
25 #include "rtc_base/logging.h"
26 #include "test/gtest.h"
30 using RTCOfferAnswerOptions
= PeerConnectionInterface::RTCOfferAnswerOptions
;
33 const uint32_t kDefaultTimeout
= 10000U;
36 PeerConnectionWrapper::PeerConnectionWrapper(
37 rtc::scoped_refptr
<PeerConnectionFactoryInterface
> pc_factory
,
38 rtc::scoped_refptr
<PeerConnectionInterface
> pc
,
39 std::unique_ptr
<MockPeerConnectionObserver
> observer
)
40 : pc_factory_(std::move(pc_factory
)),
41 observer_(std::move(observer
)),
43 RTC_DCHECK(pc_factory_
);
45 RTC_DCHECK(observer_
);
46 observer_
->SetPeerConnectionInterface(pc_
.get());
49 PeerConnectionWrapper::~PeerConnectionWrapper() {
54 PeerConnectionFactoryInterface
* PeerConnectionWrapper::pc_factory() {
55 return pc_factory_
.get();
58 PeerConnectionInterface
* PeerConnectionWrapper::pc() {
62 MockPeerConnectionObserver
* PeerConnectionWrapper::observer() {
63 return observer_
.get();
66 std::unique_ptr
<SessionDescriptionInterface
>
67 PeerConnectionWrapper::CreateOffer() {
68 return CreateOffer(RTCOfferAnswerOptions());
71 std::unique_ptr
<SessionDescriptionInterface
> PeerConnectionWrapper::CreateOffer(
72 const PeerConnectionInterface::RTCOfferAnswerOptions
& options
,
73 std::string
* error_out
) {
75 [this, options
](CreateSessionDescriptionObserver
* observer
) {
76 pc()->CreateOffer(observer
, options
);
81 std::unique_ptr
<SessionDescriptionInterface
>
82 PeerConnectionWrapper::CreateOfferAndSetAsLocal() {
83 return CreateOfferAndSetAsLocal(RTCOfferAnswerOptions());
86 std::unique_ptr
<SessionDescriptionInterface
>
87 PeerConnectionWrapper::CreateOfferAndSetAsLocal(
88 const PeerConnectionInterface::RTCOfferAnswerOptions
& options
) {
89 auto offer
= CreateOffer(options
);
93 EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer
.get())));
97 std::unique_ptr
<SessionDescriptionInterface
>
98 PeerConnectionWrapper::CreateAnswer() {
99 return CreateAnswer(RTCOfferAnswerOptions());
102 std::unique_ptr
<SessionDescriptionInterface
>
103 PeerConnectionWrapper::CreateAnswer(
104 const PeerConnectionInterface::RTCOfferAnswerOptions
& options
,
105 std::string
* error_out
) {
107 [this, options
](CreateSessionDescriptionObserver
* observer
) {
108 pc()->CreateAnswer(observer
, options
);
113 std::unique_ptr
<SessionDescriptionInterface
>
114 PeerConnectionWrapper::CreateAnswerAndSetAsLocal() {
115 return CreateAnswerAndSetAsLocal(RTCOfferAnswerOptions());
118 std::unique_ptr
<SessionDescriptionInterface
>
119 PeerConnectionWrapper::CreateAnswerAndSetAsLocal(
120 const PeerConnectionInterface::RTCOfferAnswerOptions
& options
) {
121 auto answer
= CreateAnswer(options
);
125 EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(answer
.get())));
129 std::unique_ptr
<SessionDescriptionInterface
>
130 PeerConnectionWrapper::CreateRollback() {
131 return CreateSessionDescription(SdpType::kRollback
, "");
134 std::unique_ptr
<SessionDescriptionInterface
> PeerConnectionWrapper::CreateSdp(
135 rtc::FunctionView
<void(CreateSessionDescriptionObserver
*)> fn
,
136 std::string
* error_out
) {
137 auto observer
= rtc::make_ref_counted
<MockCreateSessionDescriptionObserver
>();
139 EXPECT_EQ_WAIT(true, observer
->called(), kDefaultTimeout
);
140 if (error_out
&& !observer
->result()) {
141 *error_out
= observer
->error();
143 return observer
->MoveDescription();
146 bool PeerConnectionWrapper::SetLocalDescription(
147 std::unique_ptr
<SessionDescriptionInterface
> desc
,
148 std::string
* error_out
) {
150 [this, &desc
](SetSessionDescriptionObserver
* observer
) {
151 pc()->SetLocalDescription(observer
, desc
.release());
156 bool PeerConnectionWrapper::SetRemoteDescription(
157 std::unique_ptr
<SessionDescriptionInterface
> desc
,
158 std::string
* error_out
) {
160 [this, &desc
](SetSessionDescriptionObserver
* observer
) {
161 pc()->SetRemoteDescription(observer
, desc
.release());
166 bool PeerConnectionWrapper::SetRemoteDescription(
167 std::unique_ptr
<SessionDescriptionInterface
> desc
,
168 RTCError
* error_out
) {
169 auto observer
= rtc::make_ref_counted
<FakeSetRemoteDescriptionObserver
>();
170 pc()->SetRemoteDescription(std::move(desc
), observer
);
171 EXPECT_EQ_WAIT(true, observer
->called(), kDefaultTimeout
);
172 bool ok
= observer
->error().ok();
174 *error_out
= std::move(observer
->error());
178 bool PeerConnectionWrapper::SetSdp(
179 rtc::FunctionView
<void(SetSessionDescriptionObserver
*)> fn
,
180 std::string
* error_out
) {
181 auto observer
= rtc::make_ref_counted
<MockSetSessionDescriptionObserver
>();
183 EXPECT_EQ_WAIT(true, observer
->called(), kDefaultTimeout
);
184 if (error_out
&& !observer
->result()) {
185 *error_out
= observer
->error();
187 return observer
->result();
190 bool PeerConnectionWrapper::ExchangeOfferAnswerWith(
191 PeerConnectionWrapper
* answerer
) {
192 return ExchangeOfferAnswerWith(answerer
, RTCOfferAnswerOptions(),
193 RTCOfferAnswerOptions());
196 bool PeerConnectionWrapper::ExchangeOfferAnswerWith(
197 PeerConnectionWrapper
* answerer
,
198 const PeerConnectionInterface::RTCOfferAnswerOptions
& offer_options
,
199 const PeerConnectionInterface::RTCOfferAnswerOptions
& answer_options
) {
200 RTC_DCHECK(answerer
);
201 if (answerer
== this) {
202 RTC_LOG(LS_ERROR
) << "Cannot exchange offer/answer with ourself!";
205 auto offer
= CreateOffer(offer_options
);
210 bool set_local_offer
=
211 SetLocalDescription(CloneSessionDescription(offer
.get()));
212 EXPECT_TRUE(set_local_offer
);
213 if (!set_local_offer
) {
216 bool set_remote_offer
= answerer
->SetRemoteDescription(std::move(offer
));
217 EXPECT_TRUE(set_remote_offer
);
218 if (!set_remote_offer
) {
221 auto answer
= answerer
->CreateAnswer(answer_options
);
226 bool set_local_answer
=
227 answerer
->SetLocalDescription(CloneSessionDescription(answer
.get()));
228 EXPECT_TRUE(set_local_answer
);
229 if (!set_local_answer
) {
232 bool set_remote_answer
= SetRemoteDescription(std::move(answer
));
233 EXPECT_TRUE(set_remote_answer
);
234 return set_remote_answer
;
237 rtc::scoped_refptr
<RtpTransceiverInterface
>
238 PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type
) {
239 RTCErrorOr
<rtc::scoped_refptr
<RtpTransceiverInterface
>> result
=
240 pc()->AddTransceiver(media_type
);
241 EXPECT_EQ(RTCErrorType::NONE
, result
.error().type());
242 return result
.MoveValue();
245 rtc::scoped_refptr
<RtpTransceiverInterface
>
246 PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type
,
247 const RtpTransceiverInit
& init
) {
248 RTCErrorOr
<rtc::scoped_refptr
<RtpTransceiverInterface
>> result
=
249 pc()->AddTransceiver(media_type
, init
);
250 EXPECT_EQ(RTCErrorType::NONE
, result
.error().type());
251 return result
.MoveValue();
254 rtc::scoped_refptr
<RtpTransceiverInterface
>
255 PeerConnectionWrapper::AddTransceiver(
256 rtc::scoped_refptr
<MediaStreamTrackInterface
> track
) {
257 RTCErrorOr
<rtc::scoped_refptr
<RtpTransceiverInterface
>> result
=
258 pc()->AddTransceiver(track
);
259 EXPECT_EQ(RTCErrorType::NONE
, result
.error().type());
260 return result
.MoveValue();
263 rtc::scoped_refptr
<RtpTransceiverInterface
>
264 PeerConnectionWrapper::AddTransceiver(
265 rtc::scoped_refptr
<MediaStreamTrackInterface
> track
,
266 const RtpTransceiverInit
& init
) {
267 RTCErrorOr
<rtc::scoped_refptr
<RtpTransceiverInterface
>> result
=
268 pc()->AddTransceiver(track
, init
);
269 EXPECT_EQ(RTCErrorType::NONE
, result
.error().type());
270 return result
.MoveValue();
273 rtc::scoped_refptr
<AudioTrackInterface
> PeerConnectionWrapper::CreateAudioTrack(
274 const std::string
& label
) {
275 return pc_factory()->CreateAudioTrack(label
, nullptr);
278 rtc::scoped_refptr
<VideoTrackInterface
> PeerConnectionWrapper::CreateVideoTrack(
279 const std::string
& label
) {
280 return pc_factory()->CreateVideoTrack(FakeVideoTrackSource::Create(), label
);
283 rtc::scoped_refptr
<RtpSenderInterface
> PeerConnectionWrapper::AddTrack(
284 rtc::scoped_refptr
<MediaStreamTrackInterface
> track
,
285 const std::vector
<std::string
>& stream_ids
) {
286 RTCErrorOr
<rtc::scoped_refptr
<RtpSenderInterface
>> result
=
287 pc()->AddTrack(track
, stream_ids
);
288 EXPECT_EQ(RTCErrorType::NONE
, result
.error().type());
289 return result
.MoveValue();
292 rtc::scoped_refptr
<RtpSenderInterface
> PeerConnectionWrapper::AddTrack(
293 rtc::scoped_refptr
<MediaStreamTrackInterface
> track
,
294 const std::vector
<std::string
>& stream_ids
,
295 const std::vector
<RtpEncodingParameters
>& init_send_encodings
) {
296 RTCErrorOr
<rtc::scoped_refptr
<RtpSenderInterface
>> result
=
297 pc()->AddTrack(track
, stream_ids
, init_send_encodings
);
298 EXPECT_EQ(RTCErrorType::NONE
, result
.error().type());
299 return result
.MoveValue();
302 rtc::scoped_refptr
<RtpSenderInterface
> PeerConnectionWrapper::AddAudioTrack(
303 const std::string
& track_label
,
304 const std::vector
<std::string
>& stream_ids
) {
305 return AddTrack(CreateAudioTrack(track_label
), stream_ids
);
308 rtc::scoped_refptr
<RtpSenderInterface
> PeerConnectionWrapper::AddVideoTrack(
309 const std::string
& track_label
,
310 const std::vector
<std::string
>& stream_ids
) {
311 return AddTrack(CreateVideoTrack(track_label
), stream_ids
);
314 rtc::scoped_refptr
<DataChannelInterface
>
315 PeerConnectionWrapper::CreateDataChannel(
316 const std::string
& label
,
317 const absl::optional
<DataChannelInit
>& config
) {
318 const DataChannelInit
* config_ptr
= config
.has_value() ? &(*config
) : nullptr;
319 auto result
= pc()->CreateDataChannelOrError(label
, config_ptr
);
321 RTC_LOG(LS_ERROR
) << "CreateDataChannel failed: "
322 << ToString(result
.error().type()) << " "
323 << result
.error().message();
326 return result
.MoveValue();
329 PeerConnectionInterface::SignalingState
330 PeerConnectionWrapper::signaling_state() {
331 return pc()->signaling_state();
334 bool PeerConnectionWrapper::IsIceGatheringDone() {
335 return observer()->ice_gathering_complete_
;
338 bool PeerConnectionWrapper::IsIceConnected() {
339 return observer()->ice_connected_
;
342 rtc::scoped_refptr
<const RTCStatsReport
> PeerConnectionWrapper::GetStats() {
343 auto callback
= rtc::make_ref_counted
<MockRTCStatsCollectorCallback
>();
344 pc()->GetStats(callback
.get());
345 EXPECT_TRUE_WAIT(callback
->called(), kDefaultTimeout
);
346 return callback
->report();
349 } // namespace webrtc