peer_connection_wrapper.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef PC_PEER_CONNECTION_WRAPPER_H_
  11. #define PC_PEER_CONNECTION_WRAPPER_H_
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. #include "api/data_channel_interface.h"
  16. #include "api/function_view.h"
  17. #include "api/jsep.h"
  18. #include "api/media_stream_interface.h"
  19. #include "api/media_types.h"
  20. #include "api/peer_connection_interface.h"
  21. #include "api/rtc_error.h"
  22. #include "api/rtp_sender_interface.h"
  23. #include "api/rtp_transceiver_interface.h"
  24. #include "api/scoped_refptr.h"
  25. #include "api/stats/rtc_stats_report.h"
  26. #include "pc/test/mock_peer_connection_observers.h"
  27. namespace webrtc {
  28. // Class that wraps a PeerConnection so that it is easier to use in unit tests.
  29. // Namely, gives a synchronous API for the event-callback-based API of
  30. // PeerConnection and provides an observer object that stores information from
  31. // PeerConnectionObserver callbacks.
  32. //
  33. // This is intended to be subclassed if additional information needs to be
  34. // stored with the PeerConnection (e.g., fake PeerConnection parameters so that
  35. // tests can be written against those interactions). The base
  36. // PeerConnectionWrapper should only have helper methods that are broadly
  37. // useful. More specific helper methods should be created in the test-specific
  38. // subclass.
  39. //
  40. // The wrapper is intended to be constructed by specialized factory methods on
  41. // a test fixture class then used as a local variable in each test case.
  42. class PeerConnectionWrapper {
  43. public:
  44. // Constructs a PeerConnectionWrapper from the given PeerConnection.
  45. // The given PeerConnectionFactory should be the factory that created the
  46. // PeerConnection and the MockPeerConnectionObserver should be the observer
  47. // that is watching the PeerConnection.
  48. PeerConnectionWrapper(
  49. rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
  50. rtc::scoped_refptr<PeerConnectionInterface> pc,
  51. std::unique_ptr<MockPeerConnectionObserver> observer);
  52. virtual ~PeerConnectionWrapper();
  53. PeerConnectionFactoryInterface* pc_factory();
  54. PeerConnectionInterface* pc();
  55. MockPeerConnectionObserver* observer();
  56. // Calls the underlying PeerConnection's CreateOffer method and returns the
  57. // resulting SessionDescription once it is available. If the method call
  58. // failed, null is returned.
  59. std::unique_ptr<SessionDescriptionInterface> CreateOffer(
  60. const PeerConnectionInterface::RTCOfferAnswerOptions& options,
  61. std::string* error_out = nullptr);
  62. // Calls CreateOffer with default options.
  63. std::unique_ptr<SessionDescriptionInterface> CreateOffer();
  64. // Calls CreateOffer and sets a copy of the offer as the local description.
  65. std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
  66. const PeerConnectionInterface::RTCOfferAnswerOptions& options);
  67. // Calls CreateOfferAndSetAsLocal with default options.
  68. std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal();
  69. // Calls the underlying PeerConnection's CreateAnswer method and returns the
  70. // resulting SessionDescription once it is available. If the method call
  71. // failed, null is returned.
  72. std::unique_ptr<SessionDescriptionInterface> CreateAnswer(
  73. const PeerConnectionInterface::RTCOfferAnswerOptions& options,
  74. std::string* error_out = nullptr);
  75. // Calls CreateAnswer with the default options.
  76. std::unique_ptr<SessionDescriptionInterface> CreateAnswer();
  77. // Calls CreateAnswer and sets a copy of the offer as the local description.
  78. std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
  79. const PeerConnectionInterface::RTCOfferAnswerOptions& options);
  80. // Calls CreateAnswerAndSetAsLocal with default options.
  81. std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal();
  82. std::unique_ptr<SessionDescriptionInterface> CreateRollback();
  83. // Calls the underlying PeerConnection's SetLocalDescription method with the
  84. // given session description and waits for the success/failure response.
  85. // Returns true if the description was successfully set.
  86. bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
  87. std::string* error_out = nullptr);
  88. // Calls the underlying PeerConnection's SetRemoteDescription method with the
  89. // given session description and waits for the success/failure response.
  90. // Returns true if the description was successfully set.
  91. bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
  92. std::string* error_out = nullptr);
  93. bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
  94. RTCError* error_out);
  95. // Does a round of offer/answer with the local PeerConnectionWrapper
  96. // generating the offer and the given PeerConnectionWrapper generating the
  97. // answer.
  98. // Equivalent to:
  99. // 1. this->CreateOffer(offer_options)
  100. // 2. this->SetLocalDescription(offer)
  101. // 3. answerer->SetRemoteDescription(offer)
  102. // 4. answerer->CreateAnswer(answer_options)
  103. // 5. answerer->SetLocalDescription(answer)
  104. // 6. this->SetRemoteDescription(answer)
  105. // Returns true if all steps succeed, false otherwise.
  106. // Suggested usage:
  107. // ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
  108. bool ExchangeOfferAnswerWith(PeerConnectionWrapper* answerer);
  109. bool ExchangeOfferAnswerWith(
  110. PeerConnectionWrapper* answerer,
  111. const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
  112. const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options);
  113. // The following are wrappers for the underlying PeerConnection's
  114. // AddTransceiver method. They return the result of calling AddTransceiver
  115. // with the given arguments, DCHECKing if there is an error.
  116. rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
  117. cricket::MediaType media_type);
  118. rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
  119. cricket::MediaType media_type,
  120. const RtpTransceiverInit& init);
  121. rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
  122. rtc::scoped_refptr<MediaStreamTrackInterface> track);
  123. rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
  124. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  125. const RtpTransceiverInit& init);
  126. // Returns a new dummy audio track with the given label.
  127. rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
  128. const std::string& label);
  129. // Returns a new dummy video track with the given label.
  130. rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
  131. const std::string& label);
  132. // Wrapper for the underlying PeerConnection's AddTrack method. DCHECKs if
  133. // AddTrack fails.
  134. rtc::scoped_refptr<RtpSenderInterface> AddTrack(
  135. rtc::scoped_refptr<MediaStreamTrackInterface> track,
  136. const std::vector<std::string>& stream_ids = {});
  137. // Calls the underlying PeerConnection's AddTrack method with an audio media
  138. // stream track not bound to any source.
  139. rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack(
  140. const std::string& track_label,
  141. const std::vector<std::string>& stream_ids = {});
  142. // Calls the underlying PeerConnection's AddTrack method with a video media
  143. // stream track fed by a FakeVideoTrackSource.
  144. rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
  145. const std::string& track_label,
  146. const std::vector<std::string>& stream_ids = {});
  147. // Calls the underlying PeerConnection's CreateDataChannel method with default
  148. // initialization parameters.
  149. rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
  150. const std::string& label);
  151. // Returns the signaling state of the underlying PeerConnection.
  152. PeerConnectionInterface::SignalingState signaling_state();
  153. // Returns true if ICE has finished gathering candidates.
  154. bool IsIceGatheringDone();
  155. // Returns true if ICE has established a connection.
  156. bool IsIceConnected();
  157. // Calls GetStats() on the underlying PeerConnection and returns the resulting
  158. // report. If GetStats() fails, this method returns null and fails the test.
  159. rtc::scoped_refptr<const RTCStatsReport> GetStats();
  160. private:
  161. std::unique_ptr<SessionDescriptionInterface> CreateSdp(
  162. rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
  163. std::string* error_out);
  164. bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
  165. std::string* error_out);
  166. rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
  167. std::unique_ptr<MockPeerConnectionObserver> observer_;
  168. rtc::scoped_refptr<PeerConnectionInterface> pc_;
  169. };
  170. } // namespace webrtc
  171. #endif // PC_PEER_CONNECTION_WRAPPER_H_