blob: 4d2bc284a750379911ca9695ecb39907b03d384e [file] [log] [blame]
Steve Anton94286cb2017-09-26 16:20:19 -07001/*
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 09:11:00 -080011#ifndef PC_PEER_CONNECTION_WRAPPER_H_
12#define PC_PEER_CONNECTION_WRAPPER_H_
Steve Anton94286cb2017-09-26 16:20:19 -070013
Steve Anton94286cb2017-09-26 16:20:19 -070014#include <memory>
15#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070016#include <vector>
Steve Anton94286cb2017-09-26 16:20:19 -070017
Steve Anton10542f22019-01-11 09:11:00 -080018#include "api/data_channel_interface.h"
Artem Titov741daaf2019-03-21 14:37:36 +010019#include "api/function_view.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "api/jsep.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#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 20:26:48 +010027#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "api/stats/rtc_stats_report.h"
29#include "pc/test/mock_peer_connection_observers.h"
Steve Anton94286cb2017-09-26 16:20:19 -070030
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 15:30:51 -070067 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
68 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070069 // 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 15:30:51 -070072 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
73 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
74 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070075 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 15:30:51 -070081 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
82 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070083 // 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 15:30:51 -070086 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
87 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
88 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070089 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal();
Eldar Rello5ab79e62019-10-09 18:29:44 +030090 std::unique_ptr<SessionDescriptionInterface> CreateRollback();
Steve Anton94286cb2017-09-26 16:20:19 -070091
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 15:30:51 -070095 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
96 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070097 // 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 15:30:51 -0700100 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
101 std::string* error_out = nullptr);
Henrik Boström31638672017-11-23 17:48:32 +0100102 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
103 RTCError* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700104
Steve Antondcc3c022017-12-22 16:02:54 -0800105 // 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 13:58:07 -0800109 // 1. this->CreateOffer(offer_options)
Steve Antondcc3c022017-12-22 16:02:54 -0800110 // 2. this->SetLocalDescription(offer)
111 // 3. answerer->SetRemoteDescription(offer)
Steve Anton22da89f2018-01-25 13:58:07 -0800112 // 4. answerer->CreateAnswer(answer_options)
Steve Antondcc3c022017-12-22 16:02:54 -0800113 // 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 13:58:07 -0800119 bool ExchangeOfferAnswerWith(
120 PeerConnectionWrapper* answerer,
121 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_options,
122 const PeerConnectionInterface::RTCOfferAnswerOptions& answer_options);
Steve Antondcc3c022017-12-22 16:02:54 -0800123
Steve Anton9158ef62017-11-27 13:01:52 -0800124 // 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-05 17:10:52 -0800146 // 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 11:34:10 -0800150 const std::vector<std::string>& stream_ids = {});
Steve Anton2d6c76a2018-01-05 17:10:52 -0800151
Steve Anton8d3444d2017-10-20 15:30:51 -0700152 // 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 11:34:10 -0800156 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700157
158 // Calls the underlying PeerConnection's AddTrack method with a video media
Niels Möllere8ae5df2018-05-29 09:13:57 +0200159 // stream track fed by a FakeVideoTrackSource.
Steve Anton8d3444d2017-10-20 15:30:51 -0700160 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
161 const std::string& track_label,
Seth Hampson845e8782018-03-02 11:34:10 -0800162 const std::vector<std::string>& stream_ids = {});
Steve Anton8d3444d2017-10-20 15:30:51 -0700163
Steve Antonfa2260d2017-12-28 16:38:23 -0800164 // 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 15:30:51 -0700169 // Returns the signaling state of the underlying PeerConnection.
170 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 16:20:19 -0700171
Steve Antonf1c6db12017-10-13 11:13:35 -0700172 // Returns true if ICE has finished gathering candidates.
173 bool IsIceGatheringDone();
174
Steve Anton6f25b092017-10-23 09:39:20 -0700175 // 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 16:20:19 -0700182 private:
183 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100184 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700185 std::string* error_out);
Mirko Bonadei2a823102017-11-13 11:50:33 +0100186 bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700187 std::string* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700188
189 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000190 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700191 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 16:20:19 -0700192};
193
194} // namespace webrtc
195
Steve Anton10542f22019-01-11 09:11:00 -0800196#endif // PC_PEER_CONNECTION_WRAPPER_H_