Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: e9996cbec8015ff328b9dd32d982e6e13e261ec0 [file] [log] [blame]
Steve Anton94286cb2017-09-26 23:20:191/*
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 Anton10542f22019-01-11 17:11:0011#ifndef PC_PEER_CONNECTION_WRAPPER_H_
12#define PC_PEER_CONNECTION_WRAPPER_H_
Steve Anton94286cb2017-09-26 23:20:1913
Steve Anton94286cb2017-09-26 23:20:1914#include <memory>
Florent Castelli8037fc62024-08-29 13:00:4015#include <optional>
Steve Anton94286cb2017-09-26 23:20:1916#include <string>
Steve Anton36b29d12017-10-30 16:57:4217#include <vector>
Steve Anton94286cb2017-09-26 23:20:1918
Steve Anton10542f22019-01-11 17:11:0019#include "api/data_channel_interface.h"
Artem Titov741daaf2019-03-21 13:37:3620#include "api/function_view.h"
Yves Gerey3e707812018-11-28 15:47:4921#include "api/jsep.h"
Steve Anton10542f22019-01-11 17:11:0022#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 Hanckecfaba8f2025-01-15 01:16:3926#include "api/rtp_parameters.h"
Steve Anton10542f22019-01-11 17:11:0027#include "api/rtp_sender_interface.h"
28#include "api/rtp_transceiver_interface.h"
Mirko Bonadeid9708072019-01-25 19:26:4829#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0030#include "api/stats/rtc_stats_report.h"
Danil Chapovalovb1ec8132025-02-04 16:50:4431#include "pc/peer_connection.h"
Steve Anton10542f22019-01-11 17:11:0032#include "pc/test/mock_peer_connection_observers.h"
Steve Anton94286cb2017-09-26 23:20:1933
34namespace 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.
50class 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 Shrubsolee6a1f702025-04-15 14:55:4257 scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
58 scoped_refptr<PeerConnectionInterface> pc,
Steve Anton94286cb2017-09-26 23:20:1959 std::unique_ptr<MockPeerConnectionObserver> observer);
60 virtual ~PeerConnectionWrapper();
61
62 PeerConnectionFactoryInterface* pc_factory();
63 PeerConnectionInterface* pc();
64 MockPeerConnectionObserver* observer();
65
Danil Chapovalovb1ec8132025-02-04 16:50:4466 PeerConnection* GetInternalPeerConnection();
67
Steve Anton94286cb2017-09-26 23:20:1968 // 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 Anton8d3444d2017-10-20 22:30:5172 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
73 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1974 // 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 Anton8d3444d2017-10-20 22:30:5177 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
78 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
79 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 23:20:1980 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 Anton8d3444d2017-10-20 22:30:5186 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
87 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1988 // 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 Anton8d3444d2017-10-20 22:30:5191 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
92 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
93 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 23:20:1994 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal();
Eldar Rello5ab79e62019-10-09 15:29:4495 std::unique_ptr<SessionDescriptionInterface> CreateRollback();
Steve Anton94286cb2017-09-26 23:20:1996
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 Anton8d3444d2017-10-20 22:30:51100 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
101 std::string* error_out = nullptr);
Philipp Hanckecfaba8f2025-01-15 01:16:39102 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
103 RTCError* error_out);
Steve Anton94286cb2017-09-26 23:20:19104 // 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 Anton8d3444d2017-10-20 22:30:51107 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
108 std::string* error_out = nullptr);
Henrik Boström31638672017-11-23 16:48:32109 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
110 RTCError* error_out);
Steve Anton94286cb2017-09-26 23:20:19111
Steve Antondcc3c022017-12-23 00:02:54112 // 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 Anton22da89f2018-01-25 21:58:07116 // 1. this->CreateOffer(offer_options)
Steve Antondcc3c022017-12-23 00:02:54117 // 2. this->SetLocalDescription(offer)
118 // 3. answerer->SetRemoteDescription(offer)
Steve Anton22da89f2018-01-25 21:58:07119 // 4. answerer->CreateAnswer(answer_options)
Steve Antondcc3c022017-12-23 00:02:54120 // 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 Anton22da89f2018-01-25 21:58:07126 bool ExchangeOfferAnswerWith(
127 PeerConnectionWrapper* answerer,
128 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
129 const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options);
Steve Antondcc3c022017-12-23 00:02:54130
Steve Anton9158ef62017-11-27 21:01:52131 // 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 Shrubsolee6a1f702025-04-15 14:55:42134 scoped_refptr<RtpTransceiverInterface> AddTransceiver(
Harald Alvestrand9f033692025-03-25 22:31:05135 webrtc::MediaType media_type);
Evan Shrubsolee6a1f702025-04-15 14:55:42136 scoped_refptr<RtpTransceiverInterface> AddTransceiver(
Harald Alvestrand9f033692025-03-25 22:31:05137 webrtc::MediaType media_type,
Steve Anton9158ef62017-11-27 21:01:52138 const RtpTransceiverInit& init);
Evan Shrubsolee6a1f702025-04-15 14:55:42139 scoped_refptr<RtpTransceiverInterface> AddTransceiver(
140 scoped_refptr<MediaStreamTrackInterface> track);
141 scoped_refptr<RtpTransceiverInterface> AddTransceiver(
142 scoped_refptr<MediaStreamTrackInterface> track,
Steve Anton9158ef62017-11-27 21:01:52143 const RtpTransceiverInit& init);
144
145 // Returns a new dummy audio track with the given label.
Evan Shrubsolee6a1f702025-04-15 14:55:42146 scoped_refptr<AudioTrackInterface> CreateAudioTrack(const std::string& label);
Steve Anton9158ef62017-11-27 21:01:52147
148 // Returns a new dummy video track with the given label.
Evan Shrubsolee6a1f702025-04-15 14:55:42149 scoped_refptr<VideoTrackInterface> CreateVideoTrack(const std::string& label);
Steve Anton9158ef62017-11-27 21:01:52150
Steve Anton2d6c76a2018-01-06 01:10:52151 // Wrapper for the underlying PeerConnection's AddTrack method. DCHECKs if
152 // AddTrack fails.
Evan Shrubsolee6a1f702025-04-15 14:55:42153 scoped_refptr<RtpSenderInterface> AddTrack(
154 scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:10155 const std::vector<std::string>& stream_ids = {});
Steve Anton2d6c76a2018-01-06 01:10:52156
Evan Shrubsolee6a1f702025-04-15 14:55:42157 scoped_refptr<RtpSenderInterface> AddTrack(
158 scoped_refptr<MediaStreamTrackInterface> track,
Jonas Oreland4b2a1062022-10-19 07:24:42159 const std::vector<std::string>& stream_ids,
160 const std::vector<RtpEncodingParameters>& init_send_encodings);
161
Steve Anton8d3444d2017-10-20 22:30:51162 // Calls the underlying PeerConnection's AddTrack method with an audio media
163 // stream track not bound to any source.
Evan Shrubsolee6a1f702025-04-15 14:55:42164 scoped_refptr<RtpSenderInterface> AddAudioTrack(
Steve Anton8d3444d2017-10-20 22:30:51165 const std::string& track_label,
Seth Hampson845e8782018-03-02 19:34:10166 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 22:30:51167
168 // Calls the underlying PeerConnection's AddTrack method with a video media
Niels Möllere8ae5df2018-05-29 07:13:57169 // stream track fed by a FakeVideoTrackSource.
Evan Shrubsolee6a1f702025-04-15 14:55:42170 scoped_refptr<RtpSenderInterface> AddVideoTrack(
Steve Anton8d3444d2017-10-20 22:30:51171 const std::string& track_label,
Seth Hampson845e8782018-03-02 19:34:10172 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 22:30:51173
Steve Antonfa2260d2017-12-29 00:38:23174 // Calls the underlying PeerConnection's CreateDataChannel method with default
175 // initialization parameters.
Evan Shrubsolee6a1f702025-04-15 14:55:42176 scoped_refptr<DataChannelInterface> CreateDataChannel(
Jeremy Leconteeccd93e2023-02-10 08:26:50177 const std::string& label,
Florent Castelli8037fc62024-08-29 13:00:40178 const std::optional<DataChannelInit>& config = std::nullopt);
Steve Antonfa2260d2017-12-29 00:38:23179
Steve Anton8d3444d2017-10-20 22:30:51180 // Returns the signaling state of the underlying PeerConnection.
181 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 23:20:19182
Steve Antonf1c6db12017-10-13 18:13:35183 // Returns true if ICE has finished gathering candidates.
184 bool IsIceGatheringDone();
185
Steve Anton6f25b092017-10-23 16:39:20186 // 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 Shrubsolee6a1f702025-04-15 14:55:42191 scoped_refptr<const RTCStatsReport> GetStats();
Steve Anton6f25b092017-10-23 16:39:20192
Steve Anton94286cb2017-09-26 23:20:19193 private:
194 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Evan Shrubsole97feff22025-03-12 09:13:45195 FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 22:30:51196 std::string* error_out);
Evan Shrubsole97feff22025-03-12 09:13:45197 bool SetSdp(FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 22:30:51198 std::string* error_out);
Steve Anton94286cb2017-09-26 23:20:19199
Evan Shrubsolee6a1f702025-04-15 14:55:42200 scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08201 std::unique_ptr<MockPeerConnectionObserver> observer_;
Evan Shrubsolee6a1f702025-04-15 14:55:42202 scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 23:20:19203};
204
205} // namespace webrtc
206
Steve Anton10542f22019-01-11 17:11:00207#endif // PC_PEER_CONNECTION_WRAPPER_H_