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