You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "pc/peer_connection_wrapper.h"
  11. #include <stdint.h>
  12. #include <memory>
  13. #include <string>
  14. #include <utility>
  15. #include <vector>
  16. #include "api/function_view.h"
  17. #include "api/set_remote_description_observer_interface.h"
  18. #include "pc/sdp_utils.h"
  19. #include "pc/test/fake_video_track_source.h"
  20. #include "rtc_base/checks.h"
  21. #include "rtc_base/gunit.h"
  22. #include "rtc_base/logging.h"
  23. #include "rtc_base/ref_counted_object.h"
  24. #include "test/gtest.h"
  25. namespace webrtc {
  26. using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
  27. namespace {
  28. const uint32_t kDefaultTimeout = 10000U;
  29. }
  30. PeerConnectionWrapper::PeerConnectionWrapper(
  31. rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
  32. rtc::scoped_refptr<PeerConnectionInterface> pc,
  33. std::unique_ptr<MockPeerConnectionObserver> observer)
  34. : pc_factory_(std::move(pc_factory)),
  35. observer_(std::move(observer)),
  36. pc_(std::move(pc)) {
  37. RTC_DCHECK(pc_factory_);
  38. RTC_DCHECK(pc_);
  39. RTC_DCHECK(observer_);
  40. observer_->SetPeerConnectionInterface(pc_.get());
  41. }
  42. PeerConnectionWrapper::~PeerConnectionWrapper() = default;
  43. PeerConnectionFactoryInterface* PeerConnectionWrapper::pc_factory() {
  44. return pc_factory_.get();
  45. }
  46. PeerConnectionInterface* PeerConnectionWrapper::pc() {
  47. return pc_.get();
  48. }
  49. MockPeerConnectionObserver* PeerConnectionWrapper::observer() {
  50. return observer_.get();
  51. }
  52. std::unique_ptr<SessionDescriptionInterface>
  53. PeerConnectionWrapper::CreateOffer() {
  54. return CreateOffer(RTCOfferAnswerOptions());
  55. }
  56. std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateOffer(
  57. const PeerConnectionInterface::RTCOfferAnswerOptions& options,
  58. std::string* error_out) {
  59. return CreateSdp(
  60. [this, options](CreateSessionDescriptionObserver* observer) {
  61. pc()->CreateOffer(observer, options);
  62. },
  63. error_out);
  64. }
  65. std::unique_ptr<SessionDescriptionInterface>
  66. PeerConnectionWrapper::CreateOfferAndSetAsLocal() {
  67. return CreateOfferAndSetAsLocal(RTCOfferAnswerOptions());
  68. }
  69. std::unique_ptr<SessionDescriptionInterface>
  70. PeerConnectionWrapper::CreateOfferAndSetAsLocal(
  71. const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
  72. auto offer = CreateOffer(options);
  73. if (!offer) {
  74. return nullptr;
  75. }
  76. EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(offer.get())));
  77. return offer;
  78. }
  79. std::unique_ptr<SessionDescriptionInterface>
  80. PeerConnectionWrapper::CreateAnswer() {
  81. return CreateAnswer(RTCOfferAnswerOptions());
  82. }
  83. std::unique_ptr<SessionDescriptionInterface>
  84. PeerConnectionWrapper::CreateAnswer(
  85. const PeerConnectionInterface::RTCOfferAnswerOptions& options,
  86. std::string* error_out) {
  87. return CreateSdp(
  88. [this, options](CreateSessionDescriptionObserver* observer) {
  89. pc()->CreateAnswer(observer, options);
  90. },
  91. error_out);
  92. }
  93. std::unique_ptr<SessionDescriptionInterface>
  94. PeerConnectionWrapper::CreateAnswerAndSetAsLocal() {
  95. return CreateAnswerAndSetAsLocal(RTCOfferAnswerOptions());
  96. }
  97. std::unique_ptr<SessionDescriptionInterface>
  98. PeerConnectionWrapper::CreateAnswerAndSetAsLocal(
  99. const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
  100. auto answer = CreateAnswer(options);
  101. if (!answer) {
  102. return nullptr;
  103. }
  104. EXPECT_TRUE(SetLocalDescription(CloneSessionDescription(answer.get())));
  105. return answer;
  106. }
  107. std::unique_ptr<SessionDescriptionInterface>
  108. PeerConnectionWrapper::CreateRollback() {
  109. return CreateSessionDescription(SdpType::kRollback, "");
  110. }
  111. std::unique_ptr<SessionDescriptionInterface> PeerConnectionWrapper::CreateSdp(
  112. rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
  113. std::string* error_out) {
  114. rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
  115. new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
  116. fn(observer);
  117. EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
  118. if (error_out && !observer->result()) {
  119. *error_out = observer->error();
  120. }
  121. return observer->MoveDescription();
  122. }
  123. bool PeerConnectionWrapper::SetLocalDescription(
  124. std::unique_ptr<SessionDescriptionInterface> desc,
  125. std::string* error_out) {
  126. return SetSdp(
  127. [this, &desc](SetSessionDescriptionObserver* observer) {
  128. pc()->SetLocalDescription(observer, desc.release());
  129. },
  130. error_out);
  131. }
  132. bool PeerConnectionWrapper::SetRemoteDescription(
  133. std::unique_ptr<SessionDescriptionInterface> desc,
  134. std::string* error_out) {
  135. return SetSdp(
  136. [this, &desc](SetSessionDescriptionObserver* observer) {
  137. pc()->SetRemoteDescription(observer, desc.release());
  138. },
  139. error_out);
  140. }
  141. bool PeerConnectionWrapper::SetRemoteDescription(
  142. std::unique_ptr<SessionDescriptionInterface> desc,
  143. RTCError* error_out) {
  144. rtc::scoped_refptr<MockSetRemoteDescriptionObserver> observer =
  145. new MockSetRemoteDescriptionObserver();
  146. pc()->SetRemoteDescription(std::move(desc), observer);
  147. EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
  148. bool ok = observer->error().ok();
  149. if (error_out)
  150. *error_out = std::move(observer->error());
  151. return ok;
  152. }
  153. bool PeerConnectionWrapper::SetSdp(
  154. rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
  155. std::string* error_out) {
  156. rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
  157. new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
  158. fn(observer);
  159. EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
  160. if (error_out && !observer->result()) {
  161. *error_out = observer->error();
  162. }
  163. return observer->result();
  164. }
  165. bool PeerConnectionWrapper::ExchangeOfferAnswerWith(
  166. PeerConnectionWrapper* answerer) {
  167. return ExchangeOfferAnswerWith(answerer, RTCOfferAnswerOptions(),
  168. RTCOfferAnswerOptions());
  169. }
  170. bool PeerConnectionWrapper::ExchangeOfferAnswerWith(
  171. PeerConnectionWrapper* answerer,
  172. const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
  173. const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options) {
  174. RTC_DCHECK(answerer);
  175. if (answerer == this) {
  176. RTC_LOG(LS_ERROR) << "Cannot exchange offer/answer with ourself!";
  177. return false;
  178. }
  179. auto offer = CreateOffer(offer_options);
  180. EXPECT_TRUE(offer);
  181. if (!offer) {
  182. return false;
  183. }
  184. bool set_local_offer =
  185. SetLocalDescription(CloneSessionDescription(offer.get()));
  186. EXPECT_TRUE(set_local_offer);
  187. if (!set_local_offer) {
  188. return false;
  189. }
  190. bool set_remote_offer = answerer->SetRemoteDescription(std::move(offer));
  191. EXPECT_TRUE(set_remote_offer);
  192. if (!set_remote_offer) {
  193. return false;
  194. }
  195. auto answer = answerer->CreateAnswer(answer_options);
  196. EXPECT_TRUE(answer);
  197. if (!answer) {
  198. return false;
  199. }
  200. bool set_local_answer =
  201. answerer->SetLocalDescription(CloneSessionDescription(answer.get()));
  202. EXPECT_TRUE(set_local_answer);
  203. if (!set_local_answer) {
  204. return false;
  205. }
  206. bool set_remote_answer = SetRemoteDescription(std::move(answer));
  207. EXPECT_TRUE(set_remote_answer);
  208. return set_remote_answer;
  209. }
  210. rtc::scoped_refptr<RtpTransceiverInterface>
  211. PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type) {
  212. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
  213. pc()->AddTransceiver(media_type);
  214. EXPECT_EQ(RTCErrorType::NONE, result.error().type());
  215. return result.MoveValue();
  216. }
  217. rtc::scoped_refptr<RtpTransceiverInterface>
  218. PeerConnectionWrapper::AddTransceiver(cricket::MediaType media_type,
  219. const RtpTransceiverInit& init) {
  220. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
  221. pc()->AddTransceiver(media_type, init);
  222. EXPECT_EQ(RTCErrorType::NONE, result.error().type());
  223. return result.MoveValue();
  224. }
  225. rtc::scoped_refptr<RtpTransceiverInterface>
  226. PeerConnectionWrapper::AddTransceiver(
  227. rtc::scoped_refptr<MediaStreamTrackInterface> track) {
  228. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
  229. pc()->AddTransceiver(track);
  230. EXPECT_EQ(RTCErrorType::NONE, result.error().type());
  231. return result.MoveValue();
  232. }
  233. rtc::scoped_refptr<RtpTransceiverInterface>
  234. PeerConnectionWrapper::AddTransceiver(
  235. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  236. const RtpTransceiverInit& init) {
  237. RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> result =
  238. pc()->AddTransceiver(track, init);
  239. EXPECT_EQ(RTCErrorType::NONE, result.error().type());
  240. return result.MoveValue();
  241. }
  242. rtc::scoped_refptr<AudioTrackInterface> PeerConnectionWrapper::CreateAudioTrack(
  243. const std::string& label) {
  244. return pc_factory()->CreateAudioTrack(label, nullptr);
  245. }
  246. rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack(
  247. const std::string& label) {
  248. return pc_factory()->CreateVideoTrack(label, FakeVideoTrackSource::Create());
  249. }
  250. rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddTrack(
  251. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  252. const std::vector<std::string>& stream_ids) {
  253. RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> result =
  254. pc()->AddTrack(track, stream_ids);
  255. EXPECT_EQ(RTCErrorType::NONE, result.error().type());
  256. return result.MoveValue();
  257. }
  258. rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddAudioTrack(
  259. const std::string& track_label,
  260. const std::vector<std::string>& stream_ids) {
  261. return AddTrack(CreateAudioTrack(track_label), stream_ids);
  262. }
  263. rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddVideoTrack(
  264. const std::string& track_label,
  265. const std::vector<std::string>& stream_ids) {
  266. return AddTrack(CreateVideoTrack(track_label), stream_ids);
  267. }
  268. rtc::scoped_refptr<DataChannelInterface>
  269. PeerConnectionWrapper::CreateDataChannel(const std::string& label) {
  270. return pc()->CreateDataChannel(label, nullptr);
  271. }
  272. PeerConnectionInterface::SignalingState
  273. PeerConnectionWrapper::signaling_state() {
  274. return pc()->signaling_state();
  275. }
  276. bool PeerConnectionWrapper::IsIceGatheringDone() {
  277. return observer()->ice_gathering_complete_;
  278. }
  279. bool PeerConnectionWrapper::IsIceConnected() {
  280. return observer()->ice_connected_;
  281. }
  282. rtc::scoped_refptr<const webrtc::RTCStatsReport>
  283. PeerConnectionWrapper::GetStats() {
  284. rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
  285. new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
  286. pc()->GetStats(callback);
  287. EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
  288. return callback->report();
  289. }
  290. } // namespace webrtc