blob: 4d2bc284a750379911ca9695ecb39907b03d384e [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
Steve Anton10542f22019-01-11 17:11:0018#include "api/data_channel_interface.h"
Artem Titov741daaf2019-03-21 13:37:3619#include "api/function_view.h"
Yves Gerey3e707812018-11-28 15:47:4920#include "api/jsep.h"
Steve Anton10542f22019-01-11 17:11:0021#include "api/media_stream_interface.h"
22#include "api/media_types.h"
23#include "api/peer_connection_interface.h"
24#include "api/rtc_error.h"
25#include "api/rtp_sender_interface.h"
26#include "api/rtp_transceiver_interface.h"
Mirko Bonadeid9708072019-01-25 19:26:4827#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0028#include "api/stats/rtc_stats_report.h"
29#include "pc/test/mock_peer_connection_observers.h"
Steve Anton94286cb2017-09-26 23:20:1930
31namespace webrtc {
32
33// Class that wraps a PeerConnection so that it is easier to use in unit tests.
34// Namely, gives a synchronous API for the event-callback-based API of
35// PeerConnection and provides an observer object that stores information from
36// PeerConnectionObserver callbacks.
37//
38// This is intended to be subclassed if additional information needs to be
39// stored with the PeerConnection (e.g., fake PeerConnection parameters so that
40// tests can be written against those interactions). The base
41// PeerConnectionWrapper should only have helper methods that are broadly
42// useful. More specific helper methods should be created in the test-specific
43// subclass.
44//
45// The wrapper is intended to be constructed by specialized factory methods on
46// a test fixture class then used as a local variable in each test case.
47class PeerConnectionWrapper {
48 public:
49 // Constructs a PeerConnectionWrapper from the given PeerConnection.
50 // The given PeerConnectionFactory should be the factory that created the
51 // PeerConnection and the MockPeerConnectionObserver should be the observer
52 // that is watching the PeerConnection.
53 PeerConnectionWrapper(
54 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
55 rtc::scoped_refptr<PeerConnectionInterface> pc,
56 std::unique_ptr<MockPeerConnectionObserver> observer);
57 virtual ~PeerConnectionWrapper();
58
59 PeerConnectionFactoryInterface* pc_factory();
60 PeerConnectionInterface* pc();
61 MockPeerConnectionObserver* observer();
62
63 // Calls the underlying PeerConnection's CreateOffer method and returns the
64 // resulting SessionDescription once it is available. If the method call
65 // failed, null is returned.
66 std::unique_ptr<SessionDescriptionInterface> CreateOffer(
Steve Anton8d3444d2017-10-20 22:30:5167 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
68 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1969 // Calls CreateOffer with default options.
70 std::unique_ptr<SessionDescriptionInterface> CreateOffer();
71 // Calls CreateOffer and sets a copy of the offer as the local description.
Steve Anton8d3444d2017-10-20 22:30:5172 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
73 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
74 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 23:20:1975 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal();
76
77 // Calls the underlying PeerConnection's CreateAnswer method and returns the
78 // resulting SessionDescription once it is available. If the method call
79 // failed, null is returned.
80 std::unique_ptr<SessionDescriptionInterface> CreateAnswer(
Steve Anton8d3444d2017-10-20 22:30:5181 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
82 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1983 // Calls CreateAnswer with the default options.
84 std::unique_ptr<SessionDescriptionInterface> CreateAnswer();
85 // Calls CreateAnswer and sets a copy of the offer as the local description.
Steve Anton8d3444d2017-10-20 22:30:5186 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
87 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
88 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 23:20:1989 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal();
Eldar Rello5ab79e62019-10-09 15:29:4490 std::unique_ptr<SessionDescriptionInterface> CreateRollback();
Steve Anton94286cb2017-09-26 23:20:1991
92 // Calls the underlying PeerConnection's SetLocalDescription method with the
93 // given session description and waits for the success/failure response.
94 // Returns true if the description was successfully set.
Steve Anton8d3444d2017-10-20 22:30:5195 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
96 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 23:20:1997 // Calls the underlying PeerConnection's SetRemoteDescription 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 SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
101 std::string* error_out = nullptr);
Henrik Boström31638672017-11-23 16:48:32102 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
103 RTCError* error_out);
Steve Anton94286cb2017-09-26 23:20:19104
Steve Antondcc3c022017-12-23 00:02:54105 // Does a round of offer/answer with the local PeerConnectionWrapper
106 // generating the offer and the given PeerConnectionWrapper generating the
107 // answer.
108 // Equivalent to:
Steve Anton22da89f2018-01-25 21:58:07109 // 1. this->CreateOffer(offer_options)
Steve Antondcc3c022017-12-23 00:02:54110 // 2. this->SetLocalDescription(offer)
111 // 3. answerer->SetRemoteDescription(offer)
Steve Anton22da89f2018-01-25 21:58:07112 // 4. answerer->CreateAnswer(answer_options)
Steve Antondcc3c022017-12-23 00:02:54113 // 5. answerer->SetLocalDescription(answer)
114 // 6. this->SetRemoteDescription(answer)
115 // Returns true if all steps succeed, false otherwise.
116 // Suggested usage:
117 // ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
118 bool ExchangeOfferAnswerWith(PeerConnectionWrapper* answerer);
Steve Anton22da89f2018-01-25 21:58:07119 bool ExchangeOfferAnswerWith(
120 PeerConnectionWrapper* answerer,
121 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
122 const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options);
Steve Antondcc3c022017-12-23 00:02:54123
Steve Anton9158ef62017-11-27 21:01:52124 // The following are wrappers for the underlying PeerConnection's
125 // AddTransceiver method. They return the result of calling AddTransceiver
126 // with the given arguments, DCHECKing if there is an error.
127 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
128 cricket::MediaType media_type);
129 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
130 cricket::MediaType media_type,
131 const RtpTransceiverInit& init);
132 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
133 rtc::scoped_refptr<MediaStreamTrackInterface> track);
134 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
135 rtc::scoped_refptr<MediaStreamTrackInterface> track,
136 const RtpTransceiverInit& init);
137
138 // Returns a new dummy audio track with the given label.
139 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
140 const std::string& label);
141
142 // Returns a new dummy video track with the given label.
143 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
144 const std::string& label);
145
Steve Anton2d6c76a2018-01-06 01:10:52146 // Wrapper for the underlying PeerConnection's AddTrack method. DCHECKs if
147 // AddTrack fails.
148 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
149 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 19:34:10150 const std::vector<std::string>& stream_ids = {});
Steve Anton2d6c76a2018-01-06 01:10:52151
Steve Anton8d3444d2017-10-20 22:30:51152 // Calls the underlying PeerConnection's AddTrack method with an audio media
153 // stream track not bound to any source.
154 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack(
155 const std::string& track_label,
Seth Hampson845e8782018-03-02 19:34:10156 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 22:30:51157
158 // Calls the underlying PeerConnection's AddTrack method with a video media
Niels Möllere8ae5df2018-05-29 07:13:57159 // stream track fed by a FakeVideoTrackSource.
Steve Anton8d3444d2017-10-20 22:30:51160 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
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
Steve Antonfa2260d2017-12-29 00:38:23164 // Calls the underlying PeerConnection's CreateDataChannel method with default
165 // initialization parameters.
166 rtc::scoped_refptr<DataChannelInterface> CreateDataChannel(
167 const std::string& label);
168
Steve Anton8d3444d2017-10-20 22:30:51169 // Returns the signaling state of the underlying PeerConnection.
170 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 23:20:19171
Steve Antonf1c6db12017-10-13 18:13:35172 // Returns true if ICE has finished gathering candidates.
173 bool IsIceGatheringDone();
174
Steve Anton6f25b092017-10-23 16:39:20175 // Returns true if ICE has established a connection.
176 bool IsIceConnected();
177
178 // Calls GetStats() on the underlying PeerConnection and returns the resulting
179 // report. If GetStats() fails, this method returns null and fails the test.
180 rtc::scoped_refptr<const RTCStatsReport> GetStats();
181
Steve Anton94286cb2017-09-26 23:20:19182 private:
183 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Mirko Bonadei2a823102017-11-13 10:50:33184 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 22:30:51185 std::string* error_out);
Mirko Bonadei2a823102017-11-13 10:50:33186 bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 22:30:51187 std::string* error_out);
Steve Anton94286cb2017-09-26 23:20:19188
189 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08190 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 22:30:51191 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 23:20:19192};
193
194} // namespace webrtc
195
Steve Anton10542f22019-01-11 17:11:00196#endif // PC_PEER_CONNECTION_WRAPPER_H_