1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
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''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "LibWebRTCMacros.h"
29#include <pal/SessionID.h>
30#include <wtf/CompletionHandler.h>
31#include <wtf/Expected.h>
32#include <wtf/UniqueRef.h>
33#include <wtf/text/WTFString.h>
34
35#if USE(LIBWEBRTC)
36
37ALLOW_UNUSED_PARAMETERS_BEGIN
38
39#include <webrtc/api/peerconnectioninterface.h>
40#include <webrtc/api/video_codecs/video_encoder_factory.h>
41#include <webrtc/api/video_codecs/video_decoder_factory.h>
42#include <webrtc/rtc_base/scoped_ref_ptr.h>
43
44ALLOW_UNUSED_PARAMETERS_END
45
46namespace rtc {
47class NetworkManager;
48class PacketSocketFactory;
49class Thread;
50class RTCCertificateGenerator;
51}
52
53namespace webrtc {
54class AsyncResolverFactory;
55class PeerConnectionFactoryInterface;
56}
57#endif
58
59namespace WebCore {
60
61class LibWebRTCAudioModule;
62struct RTCRtpCapabilities;
63
64enum class MDNSRegisterError { NotImplemented, BadParameter, DNSSD, Internal, Timeout };
65
66class WEBCORE_EXPORT LibWebRTCProvider {
67public:
68 static UniqueRef<LibWebRTCProvider> create();
69
70 virtual ~LibWebRTCProvider() = default;
71
72 static bool webRTCAvailable();
73
74 virtual void setActive(bool);
75
76 virtual void setH264HardwareEncoderAllowed(bool) { }
77
78 using IPAddressOrError = Expected<String, MDNSRegisterError>;
79 using MDNSNameOrError = Expected<String, MDNSRegisterError>;
80
81 virtual void unregisterMDNSNames(uint64_t documentIdentifier)
82 {
83 UNUSED_PARAM(documentIdentifier);
84 }
85
86 virtual void registerMDNSName(PAL::SessionID, uint64_t documentIdentifier, const String& ipAddress, CompletionHandler<void(MDNSNameOrError&&)>&& callback)
87 {
88 UNUSED_PARAM(documentIdentifier);
89 UNUSED_PARAM(ipAddress);
90 callback(makeUnexpected(MDNSRegisterError::NotImplemented));
91 }
92
93#if USE(LIBWEBRTC)
94 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface> createPeerConnection(webrtc::PeerConnectionObserver&, webrtc::PeerConnectionInterface::RTCConfiguration&&);
95
96 webrtc::PeerConnectionFactoryInterface* factory();
97
98 // FIXME: Make these methods not static.
99 static void callOnWebRTCNetworkThread(Function<void()>&&);
100 static void callOnWebRTCSignalingThread(Function<void()>&&);
101
102 // Used for mock testing
103 void setPeerConnectionFactory(rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>&&);
104
105 void disableEnumeratingAllNetworkInterfaces();
106 void enableEnumeratingAllNetworkInterfaces();
107
108 void supportsVP8(bool value) { m_supportsVP8 = value; }
109 virtual void disableNonLocalhostConnections() { m_disableNonLocalhostConnections = true; }
110
111 rtc::RTCCertificateGenerator& certificateGenerator();
112
113 Optional<RTCRtpCapabilities> receiverCapabilities(const String& kind);
114 Optional<RTCRtpCapabilities> senderCapabilities(const String& kind);
115
116 void clearFactory() { m_factory = nullptr; }
117
118 void setEnableLogging(bool);
119
120protected:
121 LibWebRTCProvider() = default;
122
123 rtc::scoped_refptr<webrtc::PeerConnectionInterface> createPeerConnection(webrtc::PeerConnectionObserver&, rtc::NetworkManager&, rtc::PacketSocketFactory&, webrtc::PeerConnectionInterface::RTCConfiguration&&, std::unique_ptr<webrtc::AsyncResolverFactory>&&);
124
125 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> createPeerConnectionFactory(rtc::Thread* networkThread, rtc::Thread* signalingThread, LibWebRTCAudioModule*);
126 virtual std::unique_ptr<webrtc::VideoDecoderFactory> createDecoderFactory();
127 virtual std::unique_ptr<webrtc::VideoEncoderFactory> createEncoderFactory();
128
129 bool m_enableEnumeratingAllNetworkInterfaces { false };
130 // FIXME: Remove m_useNetworkThreadWithSocketServer member variable and make it a global.
131 bool m_useNetworkThreadWithSocketServer { true };
132
133 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> m_factory;
134 bool m_disableNonLocalhostConnections { false };
135 bool m_supportsVP8 { false };
136 bool m_enableLogging { true };
137#endif
138};
139
140} // namespace WebCore
141
142namespace WTF {
143template<> struct EnumTraits<WebCore::MDNSRegisterError> {
144 using values = EnumValues<
145 WebCore::MDNSRegisterError,
146 WebCore::MDNSRegisterError::NotImplemented,
147 WebCore::MDNSRegisterError::BadParameter,
148 WebCore::MDNSRegisterError::DNSSD,
149 WebCore::MDNSRegisterError::Internal,
150 WebCore::MDNSRegisterError::Timeout
151 >;
152};
153}
154