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