1/*
2 * Copyright (C) 2017 Apple Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#pragma once
26
27#if USE(LIBWEBRTC)
28
29#include "LibWebRTCMacros.h"
30
31ALLOW_UNUSED_PARAMETERS_BEGIN
32
33#include <webrtc/api/mediastreaminterface.h>
34#include <webrtc/api/peerconnectioninterface.h>
35
36ALLOW_UNUSED_PARAMETERS_END
37
38#include <wtf/text/WTFString.h>
39
40namespace WebCore {
41
42class LibWebRTCProvider;
43class MockRtpSender;
44
45void useMockRTCPeerConnectionFactory(LibWebRTCProvider*, const String&);
46void useRealRTCPeerConnectionFactory(LibWebRTCProvider&);
47
48class MockLibWebRTCPeerConnection : public webrtc::PeerConnectionInterface {
49public:
50 ~MockLibWebRTCPeerConnection();
51
52protected:
53 explicit MockLibWebRTCPeerConnection(webrtc::PeerConnectionObserver& observer) : m_observer(observer) { }
54
55private:
56 rtc::scoped_refptr<webrtc::StreamCollectionInterface> local_streams() override { return nullptr; }
57 rtc::scoped_refptr<webrtc::StreamCollectionInterface> remote_streams() override { return nullptr; }
58 const webrtc::SessionDescriptionInterface* local_description() const override { return nullptr; }
59 const webrtc::SessionDescriptionInterface* remote_description() const override { return nullptr; }
60 bool AddIceCandidate(const webrtc::IceCandidateInterface*) override { return true; }
61 SignalingState signaling_state() override { return kStable; }
62 IceConnectionState ice_connection_state() override { return kIceConnectionNew; }
63 IceGatheringState ice_gathering_state() override { return kIceGatheringNew; }
64 void StopRtcEventLog() override { }
65 void Close() override { }
66
67 bool AddStream(webrtc::MediaStreamInterface*) final { return false; }
68 void RemoveStream(webrtc::MediaStreamInterface*) final { }
69
70protected:
71 void SetRemoteDescription(webrtc::SetSessionDescriptionObserver*, webrtc::SessionDescriptionInterface*) final;
72 void CreateAnswer(webrtc::CreateSessionDescriptionObserver*, const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions&) final;
73 rtc::scoped_refptr<webrtc::DataChannelInterface> CreateDataChannel(const std::string&, const webrtc::DataChannelInit*) final;
74 webrtc::RTCErrorOr<rtc::scoped_refptr<webrtc::RtpSenderInterface>> AddTrack(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>, const std::vector<std::string>& streams) final;
75 bool RemoveTrack(webrtc::RtpSenderInterface*) final;
76 webrtc::RTCError SetBitrate(const BitrateParameters&) final { return { }; }
77
78
79 void SetLocalDescription(webrtc::SetSessionDescriptionObserver*, webrtc::SessionDescriptionInterface*) override;
80 bool GetStats(webrtc::StatsObserver*, webrtc::MediaStreamTrackInterface*, StatsOutputLevel) override { return false; }
81 void CreateOffer(webrtc::CreateSessionDescriptionObserver*, const webrtc::PeerConnectionInterface::RTCOfferAnswerOptions&) override;
82
83 virtual void gotLocalDescription() { }
84
85 webrtc::PeerConnectionObserver& m_observer;
86 unsigned m_counter { 0 };
87 Vector<rtc::scoped_refptr<MockRtpSender>> m_senders;
88 bool m_isInitiator { true };
89 bool m_isReceivingAudio { false };
90 bool m_isReceivingVideo { false };
91 std::string m_streamLabel;
92};
93
94class MockLibWebRTCSessionDescription: public webrtc::SessionDescriptionInterface {
95public:
96 explicit MockLibWebRTCSessionDescription(std::string&& sdp) : m_sdp(WTFMove(sdp)) { }
97
98private:
99 bool ToString(std::string* out) const final { *out = m_sdp; return true; }
100
101 cricket::SessionDescription* description() final { return nullptr; }
102 const cricket::SessionDescription* description() const final { return nullptr; }
103 std::string session_id() const final { return ""; }
104 std::string session_version() const final { return ""; }
105 std::string type() const final { return ""; }
106 bool AddCandidate(const webrtc::IceCandidateInterface*) final { return true; }
107 size_t number_of_mediasections() const final { return 0; }
108 const webrtc::IceCandidateCollection* candidates(size_t) const final { return nullptr; }
109
110 std::string m_sdp;
111};
112
113class MockLibWebRTCIceCandidate : public webrtc::IceCandidateInterface {
114public:
115 MockLibWebRTCIceCandidate(const char* sdp, const char* sdpMid)
116 : m_sdp(sdp)
117 , m_sdpMid(sdpMid) { }
118
119private:
120 std::string sdp_mid() const final { return m_sdpMid; }
121 int sdp_mline_index() const final { return 0; }
122 const cricket::Candidate& candidate() const final { return m_candidate; }
123 bool ToString(std::string* out) const final { *out = m_sdp; return true; }
124
125protected:
126 const char* m_sdp;
127 const char* m_sdpMid;
128 cricket::Candidate m_candidate;
129};
130
131class MockLibWebRTCAudioTrack : public webrtc::AudioTrackInterface {
132public:
133 explicit MockLibWebRTCAudioTrack(const std::string& id, webrtc::AudioSourceInterface* source)
134 : m_id(id)
135 , m_source(source) { }
136
137private:
138 webrtc::AudioSourceInterface* GetSource() const final { return m_source; }
139 void AddSink(webrtc::AudioTrackSinkInterface* sink) final {
140 if (m_source)
141 m_source->AddSink(sink);
142 }
143 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) final {
144 if (m_source)
145 m_source->RemoveSink(sink);
146 }
147 void RegisterObserver(webrtc::ObserverInterface*) final { }
148 void UnregisterObserver(webrtc::ObserverInterface*) final { }
149
150 std::string kind() const final { return "audio"; }
151 std::string id() const final { return m_id; }
152 bool enabled() const final { return m_enabled; }
153 TrackState state() const final { return kLive; }
154 bool set_enabled(bool enabled) final { m_enabled = enabled; return true; }
155
156 bool m_enabled { true };
157 std::string m_id;
158 rtc::scoped_refptr<webrtc::AudioSourceInterface> m_source;
159};
160
161class MockLibWebRTCVideoTrack : public webrtc::VideoTrackInterface {
162public:
163 explicit MockLibWebRTCVideoTrack(const std::string& id, webrtc::VideoTrackSourceInterface* source)
164 : m_id(id)
165 , m_source(source) { }
166
167private:
168 webrtc::VideoTrackSourceInterface* GetSource() const final { return m_source; }
169 void RegisterObserver(webrtc::ObserverInterface*) final { }
170 void UnregisterObserver(webrtc::ObserverInterface*) final { }
171
172 std::string kind() const final { return "video"; }
173 std::string id() const final { return m_id; }
174 bool enabled() const final { return m_enabled; }
175 TrackState state() const final { return kLive; }
176 bool set_enabled(bool enabled) final { m_enabled = enabled; return true; }
177
178 bool m_enabled;
179 std::string m_id;
180 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> m_source;
181};
182
183class MockLibWebRTCDataChannel : public webrtc::DataChannelInterface {
184public:
185 MockLibWebRTCDataChannel(std::string&& label, bool ordered, bool reliable, int id)
186 : m_label(WTFMove(label))
187 , m_ordered(ordered)
188 , m_reliable(reliable)
189 , m_id(id) { }
190
191private:
192 void RegisterObserver(webrtc::DataChannelObserver*) final { }
193 void UnregisterObserver() final { }
194 std::string label() const final { return m_label; }
195 bool reliable() const final { return m_reliable; }
196 bool ordered() const final { return m_ordered; }
197
198 int id() const final { return m_id; }
199 DataState state() const final { return kConnecting; }
200 uint64_t buffered_amount() const final { return 0; }
201 void Close() final { }
202 bool Send(const webrtc::DataBuffer&) final { return true; }
203 uint32_t messages_sent() const final { return 0; }
204 uint64_t bytes_sent() const final { return 0; }
205 uint32_t messages_received() const final { return 0; }
206 uint64_t bytes_received() const final { return 0; }
207
208 std::string m_label;
209 bool m_ordered { true };
210 bool m_reliable { false };
211 int m_id { -1 };
212};
213
214class MockRtpSender : public webrtc::RtpSenderInterface {
215public:
216 MockRtpSender(rtc::scoped_refptr<webrtc::MediaStreamTrackInterface>&& track) : m_track(WTFMove(track)) { }
217 bool SetTrack(webrtc::MediaStreamTrackInterface* track) final
218 {
219 m_track = track;
220 return true;
221 }
222 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track() const final { return m_track; }
223
224 uint32_t ssrc() const { return 0; }
225 cricket::MediaType media_type() const { return cricket::MEDIA_TYPE_VIDEO; }
226 std::string id() const { return ""; }
227 std::vector<std::string> stream_ids() const { return { }; }
228 webrtc::RtpParameters GetParameters() final { return { }; }
229 webrtc::RTCError SetParameters(const webrtc::RtpParameters&) final { return { }; }
230 rtc::scoped_refptr<webrtc::DtmfSenderInterface> GetDtmfSender() const final { return nullptr; }
231
232private:
233 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> m_track;
234};
235
236class MockLibWebRTCPeerConnectionFactory : public webrtc::PeerConnectionFactoryInterface {
237public:
238 static rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> create(const String& testCase) { return new rtc::RefCountedObject<MockLibWebRTCPeerConnectionFactory>(testCase); }
239
240protected:
241 explicit MockLibWebRTCPeerConnectionFactory(const String&);
242
243private:
244 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(const webrtc::PeerConnectionInterface::RTCConfiguration&, webrtc::PeerConnectionDependencies) final;
245
246 rtc::scoped_refptr<webrtc::MediaStreamInterface> CreateLocalMediaStream(const std::string&) final;
247
248 void SetOptions(const Options&) final { }
249 rtc::scoped_refptr<webrtc::AudioSourceInterface> CreateAudioSource(const cricket::AudioOptions&) final { return nullptr; }
250
251 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> CreateVideoSource(cricket::VideoCapturer*) final { return nullptr; }
252 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>, const webrtc::MediaConstraintsInterface*) final { return nullptr; }
253
254 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateVideoTrack(const std::string&, webrtc::VideoTrackSourceInterface*) final;
255 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateAudioTrack(const std::string&, webrtc::AudioSourceInterface*) final;
256
257 bool StartAecDump(rtc::PlatformFile, int64_t) final { return false; }
258 void StopAecDump() final { }
259
260private:
261 String m_testCase;
262};
263
264} // namespace WebCore
265
266#endif // USE(LIBWEBRTC)
267