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