Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef PC_PEER_CONNECTION_WRAPPER_H_ |
| 12 | #define PC_PEER_CONNECTION_WRAPPER_H_ |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 13 | |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 16 | #include <vector> |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 17 | |
Jeremy Leconte | eccd93e | 2023-02-10 08:26:50 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 19 | #include "api/data_channel_interface.h" |
Artem Titov | 741daaf | 2019-03-21 13:37:36 | [diff] [blame] | 20 | #include "api/function_view.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 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" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 28 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 29 | #include "api/stats/rtc_stats_report.h" |
| 30 | #include "pc/test/mock_peer_connection_observers.h" |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 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( |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 68 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 69 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 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. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 73 | std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal( |
| 74 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 75 | // Calls CreateOfferAndSetAsLocal with default options. |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 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( |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 82 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 83 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 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. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 87 | std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal( |
| 88 | const PeerConnectionInterface::RTCOfferAnswerOptions& options); |
| 89 | // Calls CreateAnswerAndSetAsLocal with default options. |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 90 | std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 91 | std::unique_ptr<SessionDescriptionInterface> CreateRollback(); |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 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. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 96 | bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 97 | std::string* error_out = nullptr); |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 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. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 101 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 102 | std::string* error_out = nullptr); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 103 | bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc, |
| 104 | RTCError* error_out); |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 105 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 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: |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 110 | // 1. this->CreateOffer(offer_options) |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 111 | // 2. this->SetLocalDescription(offer) |
| 112 | // 3. answerer->SetRemoteDescription(offer) |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 113 | // 4. answerer->CreateAnswer(answer_options) |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 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); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 120 | bool ExchangeOfferAnswerWith( |
| 121 | PeerConnectionWrapper* answerer, |
| 122 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options, |
| 123 | const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 124 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 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 | |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 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, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 151 | const std::vector<std::string>& stream_ids = {}); |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 152 | |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 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 | |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 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, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 162 | const std::vector<std::string>& stream_ids = {}); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 163 | |
| 164 | // Calls the underlying PeerConnection's AddTrack method with a video media |
Niels Möller | e8ae5df | 2018-05-29 07:13:57 | [diff] [blame] | 165 | // stream track fed by a FakeVideoTrackSource. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 166 | rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack( |
| 167 | const std::string& track_label, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 168 | const std::vector<std::string>& stream_ids = {}); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 169 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 170 | // Calls the underlying PeerConnection's CreateDataChannel method with default |
| 171 | // initialization parameters. |
| 172 | rtc::scoped_refptr<DataChannelInterface> CreateDataChannel( |
Jeremy Leconte | eccd93e | 2023-02-10 08:26:50 | [diff] [blame] | 173 | const std::string& label, |
| 174 | const absl::optional<DataChannelInit>& config = absl::nullopt); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 175 | |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 176 | // Returns the signaling state of the underlying PeerConnection. |
| 177 | PeerConnectionInterface::SignalingState signaling_state(); |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 178 | |
Steve Anton | f1c6db1 | 2017-10-13 18:13:35 | [diff] [blame] | 179 | // Returns true if ICE has finished gathering candidates. |
| 180 | bool IsIceGatheringDone(); |
| 181 | |
Steve Anton | 6f25b09 | 2017-10-23 16:39:20 | [diff] [blame] | 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 | |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 189 | private: |
| 190 | std::unique_ptr<SessionDescriptionInterface> CreateSdp( |
Mirko Bonadei | 2a82310 | 2017-11-13 10:50:33 | [diff] [blame] | 191 | rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 192 | std::string* error_out); |
Mirko Bonadei | 2a82310 | 2017-11-13 10:50:33 | [diff] [blame] | 193 | bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn, |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 194 | std::string* error_out); |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 195 | |
| 196 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
Olga Sharonova | f2662f0 | 2017-10-20 09:42:08 | [diff] [blame] | 197 | std::unique_ptr<MockPeerConnectionObserver> observer_; |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 198 | rtc::scoped_refptr<PeerConnectionInterface> pc_; |
Steve Anton | 94286cb | 2017-09-26 23:20:19 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } // namespace webrtc |
| 202 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 203 | #endif // PC_PEER_CONNECTION_WRAPPER_H_ |