blob: bf40bbcfb8b32c23a55c6a406c44acd5272266ad [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>
15#include <string>
Steve Anton36b29d12017-10-30 16:57:4216#include <vector>
Steve Anton94286cb2017-09-26 23:20:1917
Jeremy Leconteeccd93e2023-02-10 08:26:5018#include "absl/types/optional.h"
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"
26#include "api/rtp_sender_interface.h"
27#include "api/rtp_transceiver_interface.h"
Mirko Bonadeid9708072019-01-25 19:26:4828#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0029#include "api/stats/rtc_stats_report.h"
30#include "pc/test/mock_peer_connection_observers.h"
Steve Anton94286cb2017-09-26 23:20:1931
32namespace 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.
48class 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 Anton8d3444d2017-10-20 22:30:5168 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
69 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1970 // 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 Anton8d3444d2017-10-20 22:30:5173 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
74 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
75 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 23:20:1976 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 Anton8d3444d2017-10-20 22:30:5182 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
83 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1984 // 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 Anton8d3444d2017-10-20 22:30:5187 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
88 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
89 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 23:20:1990 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal();
Eldar Rello5ab79e62019-10-09 15:29:4491 std::unique_ptr<SessionDescriptionInterface> CreateRollback();
Steve Anton94286cb2017-09-26 23:20:1992
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 Anton8d3444d2017-10-20 22:30:5196 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
97 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1998 // 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 Anton8d3444d2017-10-20 22:30:51101 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
102 std::string* error_out = nullptr);
Henrik Boström31638672017-11-23 16:48:32103 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
104 RTCError* error_out);
Steve Anton94286cb2017-09-26 23:20:19105
Steve Antondcc3c022017-12-23 00:02:54106 // 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 Anton22da89f2018-01-25 21:58:07110 // 1. this->CreateOffer(offer_options)
Steve Antondcc3c022017-12-23 00:02:54111 // 2. this->SetLocalDescription(offer)
112 // 3. answerer->SetRemoteDescription(offer)
Steve Anton22da89f2018-01-25 21:58:07113 // 4. answerer->CreateAnswer(answer_options)
Steve Antondcc3c022017-12-23 00:02:54114 // 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 Anton22da89f2018-01-25 21:58:07120 bool ExchangeOfferAnswerWith(
121 PeerConnectionWrapper* answerer,
122 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
123 const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options);
Steve Antondcc3c022017-12-23 00:02:54124
Steve Anton9158ef62017-11-27 21:01:52125 // 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 Anton2d6c76a2018-01-06 01:10:52147 // 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 Hampson845e8782018-03-02 19:34:10151 const std::vector<std::string>& stream_ids = {});
Steve Anton2d6c76a2018-01-06 01:10:52152
Jonas Oreland4b2a1062022-10-19 07:24:42153 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 Anton8d3444d2017-10-20 22:30:51158 // 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 Hampson845e8782018-03-02 19:34:10162 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 22:30:51163
164 // Calls the underlying PeerConnection's AddTrack method with a video media
Niels Möllere8ae5df2018-05-29 07:13:57165 // stream track fed by a FakeVideoTrackSource.
Steve Anton8d3444d2017-10-20 22:30:51166 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
167 const std::string& track_label,
Seth Hampson845e8782018-03-02 19:34:10168 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 22:30:51169
Steve Antonfa2260d2017-12-29 00:38:23170 // Calls the underlying PeerConnection's CreateDataChannel method with default
171 // initialization parameters.
172 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
Jeremy Leconteeccd93e2023-02-10 08:26:50173 const std::string& label,
174 const absl::optional<DataChannelInit>& config = absl::nullopt);
Steve Antonfa2260d2017-12-29 00:38:23175
Steve Anton8d3444d2017-10-20 22:30:51176 // Returns the signaling state of the underlying PeerConnection.
177 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 23:20:19178
Steve Antonf1c6db12017-10-13 18:13:35179 // Returns true if ICE has finished gathering candidates.
180 bool IsIceGatheringDone();
181
Steve Anton6f25b092017-10-23 16:39:20182 // 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 Anton94286cb2017-09-26 23:20:19189 private:
190 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Mirko Bonadei2a823102017-11-13 10:50:33191 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 22:30:51192 std::string* error_out);
Mirko Bonadei2a823102017-11-13 10:50:33193 bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 22:30:51194 std::string* error_out);
Steve Anton94286cb2017-09-26 23:20:19195
196 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08197 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 22:30:51198 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 23:20:19199};
200
201} // namespace webrtc
202
Steve Anton10542f22019-01-11 17:11:00203#endif // PC_PEER_CONNECTION_WRAPPER_H_