blob: e7d19eaa9b473d8fa841c08a4b5b1e251f17f9fc [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
11#ifndef PC_PEERCONNECTIONWRAPPER_H_
12#define PC_PEERCONNECTIONWRAPPER_H_
13
14#include <functional>
15#include <memory>
16#include <string>
Steve Anton36b29d12017-10-30 09:57:42 -070017#include <vector>
Steve Anton94286cb2017-09-26 16:20:19 -070018
19#include "api/peerconnectioninterface.h"
20#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei2a823102017-11-13 11:50:33 +010021#include "rtc_base/function_view.h"
Steve Anton94286cb2017-09-26 16:20:19 -070022
23namespace webrtc {
24
25// Class that wraps a PeerConnection so that it is easier to use in unit tests.
26// Namely, gives a synchronous API for the event-callback-based API of
27// PeerConnection and provides an observer object that stores information from
28// PeerConnectionObserver callbacks.
29//
30// This is intended to be subclassed if additional information needs to be
31// stored with the PeerConnection (e.g., fake PeerConnection parameters so that
32// tests can be written against those interactions). The base
33// PeerConnectionWrapper should only have helper methods that are broadly
34// useful. More specific helper methods should be created in the test-specific
35// subclass.
36//
37// The wrapper is intended to be constructed by specialized factory methods on
38// a test fixture class then used as a local variable in each test case.
39class PeerConnectionWrapper {
40 public:
41 // Constructs a PeerConnectionWrapper from the given PeerConnection.
42 // The given PeerConnectionFactory should be the factory that created the
43 // PeerConnection and the MockPeerConnectionObserver should be the observer
44 // that is watching the PeerConnection.
45 PeerConnectionWrapper(
46 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory,
47 rtc::scoped_refptr<PeerConnectionInterface> pc,
48 std::unique_ptr<MockPeerConnectionObserver> observer);
49 virtual ~PeerConnectionWrapper();
50
51 PeerConnectionFactoryInterface* pc_factory();
52 PeerConnectionInterface* pc();
53 MockPeerConnectionObserver* observer();
54
55 // Calls the underlying PeerConnection's CreateOffer method and returns the
56 // resulting SessionDescription once it is available. If the method call
57 // failed, null is returned.
58 std::unique_ptr<SessionDescriptionInterface> CreateOffer(
Steve Anton8d3444d2017-10-20 15:30:51 -070059 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
60 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070061 // Calls CreateOffer with default options.
62 std::unique_ptr<SessionDescriptionInterface> CreateOffer();
63 // Calls CreateOffer and sets a copy of the offer as the local description.
Steve Anton8d3444d2017-10-20 15:30:51 -070064 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal(
65 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
66 // Calls CreateOfferAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070067 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndSetAsLocal();
68
69 // Calls the underlying PeerConnection's CreateAnswer method and returns the
70 // resulting SessionDescription once it is available. If the method call
71 // failed, null is returned.
72 std::unique_ptr<SessionDescriptionInterface> CreateAnswer(
Steve Anton8d3444d2017-10-20 15:30:51 -070073 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
74 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070075 // Calls CreateAnswer with the default options.
76 std::unique_ptr<SessionDescriptionInterface> CreateAnswer();
77 // Calls CreateAnswer and sets a copy of the offer as the local description.
Steve Anton8d3444d2017-10-20 15:30:51 -070078 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal(
79 const PeerConnectionInterface::RTCOfferAnswerOptions& options);
80 // Calls CreateAnswerAndSetAsLocal with default options.
Steve Anton94286cb2017-09-26 16:20:19 -070081 std::unique_ptr<SessionDescriptionInterface> CreateAnswerAndSetAsLocal();
82
83 // Calls the underlying PeerConnection's SetLocalDescription method with the
84 // given session description and waits for the success/failure response.
85 // Returns true if the description was successfully set.
Steve Anton8d3444d2017-10-20 15:30:51 -070086 bool SetLocalDescription(std::unique_ptr<SessionDescriptionInterface> desc,
87 std::string* error_out = nullptr);
Steve Anton94286cb2017-09-26 16:20:19 -070088 // Calls the underlying PeerConnection's SetRemoteDescription method with the
89 // given session description and waits for the success/failure response.
90 // Returns true if the description was successfully set.
Steve Anton8d3444d2017-10-20 15:30:51 -070091 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
92 std::string* error_out = nullptr);
Henrik Boström31638672017-11-23 17:48:32 +010093 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc,
94 RTCError* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -070095
Steve Anton9158ef62017-11-27 13:01:52 -080096 // The following are wrappers for the underlying PeerConnection's
97 // AddTransceiver method. They return the result of calling AddTransceiver
98 // with the given arguments, DCHECKing if there is an error.
99 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
100 cricket::MediaType media_type);
101 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
102 cricket::MediaType media_type,
103 const RtpTransceiverInit& init);
104 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
105 rtc::scoped_refptr<MediaStreamTrackInterface> track);
106 rtc::scoped_refptr<RtpTransceiverInterface> AddTransceiver(
107 rtc::scoped_refptr<MediaStreamTrackInterface> track,
108 const RtpTransceiverInit& init);
109
110 // Returns a new dummy audio track with the given label.
111 rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
112 const std::string& label);
113
114 // Returns a new dummy video track with the given label.
115 rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
116 const std::string& label);
117
Steve Anton8d3444d2017-10-20 15:30:51 -0700118 // Calls the underlying PeerConnection's AddTrack method with an audio media
119 // stream track not bound to any source.
120 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack(
121 const std::string& track_label,
122 std::vector<MediaStreamInterface*> streams = {});
123
124 // Calls the underlying PeerConnection's AddTrack method with a video media
125 // stream track fed by a fake video capturer.
126 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack(
127 const std::string& track_label,
128 std::vector<MediaStreamInterface*> streams = {});
129
130 // Returns the signaling state of the underlying PeerConnection.
131 PeerConnectionInterface::SignalingState signaling_state();
Steve Anton94286cb2017-09-26 16:20:19 -0700132
Steve Antonf1c6db12017-10-13 11:13:35 -0700133 // Returns true if ICE has finished gathering candidates.
134 bool IsIceGatheringDone();
135
Steve Anton6f25b092017-10-23 09:39:20 -0700136 // Returns true if ICE has established a connection.
137 bool IsIceConnected();
138
139 // Calls GetStats() on the underlying PeerConnection and returns the resulting
140 // report. If GetStats() fails, this method returns null and fails the test.
141 rtc::scoped_refptr<const RTCStatsReport> GetStats();
142
Steve Anton94286cb2017-09-26 16:20:19 -0700143 private:
144 std::unique_ptr<SessionDescriptionInterface> CreateSdp(
Mirko Bonadei2a823102017-11-13 11:50:33 +0100145 rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700146 std::string* error_out);
Mirko Bonadei2a823102017-11-13 11:50:33 +0100147 bool SetSdp(rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
Steve Anton8d3444d2017-10-20 15:30:51 -0700148 std::string* error_out);
Steve Anton94286cb2017-09-26 16:20:19 -0700149
150 rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_;
Olga Sharonovaf2662f02017-10-20 09:42:08 +0000151 std::unique_ptr<MockPeerConnectionObserver> observer_;
Steve Anton8d3444d2017-10-20 15:30:51 -0700152 rtc::scoped_refptr<PeerConnectionInterface> pc_;
Steve Anton94286cb2017-09-26 16:20:19 -0700153};
154
155} // namespace webrtc
156
157#endif // PC_PEERCONNECTIONWRAPPER_H_