[WebRTC] Limit libwebrtc logging in Debug build
[WebKit-https.git] / Source / WebCore / platform / mediastream / libwebrtc / LibWebRTCProvider.cpp
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 #include "config.h"
27 #include "LibWebRTCProvider.h"
28
29 #if USE(LIBWEBRTC)
30
31 #include "LibWebRTCAudioModule.h"
32 #include "Logging.h"
33 #include <webrtc/api/peerconnectionfactory.h>
34 #include <webrtc/api/peerconnectionfactoryproxy.h>
35 #include <webrtc/base/physicalsocketserver.h>
36 #include <webrtc/p2p/client/basicportallocator.h>
37 #include <webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.h>
38 #include <wtf/Function.h>
39 #include <wtf/NeverDestroyed.h>
40
41 namespace WebCore {
42
43 struct PeerConnectionFactoryAndThreads : public rtc::MessageHandler {
44     std::unique_ptr<LibWebRTCAudioModule> audioDeviceModule;
45     std::unique_ptr<rtc::Thread> networkThread;
46     std::unique_ptr<rtc::Thread> signalingThread;
47     rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> factory;
48     bool networkThreadWithSocketServer { false };
49 private:
50     void OnMessage(rtc::Message*);
51 };
52
53 static inline PeerConnectionFactoryAndThreads& staticFactoryAndThreads()
54 {
55     static NeverDestroyed<PeerConnectionFactoryAndThreads> factoryAndThreads;
56     return factoryAndThreads.get();
57 }
58
59 struct ThreadMessageData : public rtc::MessageData {
60     ThreadMessageData(Function<void()>&& callback)
61         : callback(WTFMove(callback))
62     { }
63     Function<void()> callback;
64 };
65
66 void PeerConnectionFactoryAndThreads::OnMessage(rtc::Message* message)
67 {
68     ASSERT(message->message_id == 1);
69     auto* data = static_cast<ThreadMessageData*>(message->pdata);
70     data->callback();
71     delete data;
72 }
73
74 void LibWebRTCProvider::callOnWebRTCNetworkThread(Function<void()>&& callback)
75 {
76     PeerConnectionFactoryAndThreads& threads = staticFactoryAndThreads();
77     threads.networkThread->Post(RTC_FROM_HERE, &threads, 1, new ThreadMessageData(WTFMove(callback)));
78 }
79
80 void LibWebRTCProvider::callOnWebRTCSignalingThread(Function<void()>&& callback)
81 {
82     PeerConnectionFactoryAndThreads& threads = staticFactoryAndThreads();
83     threads.signalingThread->Post(RTC_FROM_HERE, &threads, 1, new ThreadMessageData(WTFMove(callback)));
84 }
85
86 static void initializePeerConnectionFactoryAndThreads()
87 {
88 #if defined(NDEBUG)
89     rtc::LogMessage::LogToDebug(rtc::LS_NONE);
90 #else
91     if (LogWebRTC.state != WTFLogChannelOn)
92         rtc::LogMessage::LogToDebug(rtc::LS_WARNING);
93 #endif
94     auto& factoryAndThreads = staticFactoryAndThreads();
95
96     ASSERT(!factoryAndThreads.factory);
97
98     auto thread = rtc::Thread::Create();
99     factoryAndThreads.networkThread = factoryAndThreads.networkThreadWithSocketServer ? rtc::Thread::CreateWithSocketServer() : rtc::Thread::Create();
100     bool result = factoryAndThreads.networkThread->Start();
101     ASSERT_UNUSED(result, result);
102
103     factoryAndThreads.signalingThread = rtc::Thread::Create();
104     result = factoryAndThreads.signalingThread->Start();
105     ASSERT(result);
106
107     factoryAndThreads.audioDeviceModule = std::make_unique<LibWebRTCAudioModule>();
108
109     factoryAndThreads.factory = webrtc::CreatePeerConnectionFactory(factoryAndThreads.networkThread.get(), factoryAndThreads.networkThread.get(), factoryAndThreads.signalingThread.get(), factoryAndThreads.audioDeviceModule.get(), new webrtc::VideoToolboxVideoEncoderFactory(), new webrtc::VideoToolboxVideoDecoderFactory());
110
111     ASSERT(factoryAndThreads.factory);
112 }
113
114 webrtc::PeerConnectionFactoryInterface& LibWebRTCProvider::factory()
115 {
116     if (!staticFactoryAndThreads().factory)
117         initializePeerConnectionFactoryAndThreads();
118     return *staticFactoryAndThreads().factory;
119 }
120
121 void LibWebRTCProvider::setPeerConnectionFactory(rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>&& factory)
122 {
123     if (!staticFactoryAndThreads().factory)
124         initializePeerConnectionFactoryAndThreads();
125
126     staticFactoryAndThreads().factory = webrtc::PeerConnectionFactoryProxy::Create(staticFactoryAndThreads().signalingThread.get(), WTFMove(factory));
127 }
128
129 static rtc::scoped_refptr<webrtc::PeerConnectionInterface> createActualPeerConnection(webrtc::PeerConnectionObserver& observer, std::unique_ptr<cricket::BasicPortAllocator>&& portAllocator)
130 {
131     ASSERT(staticFactoryAndThreads().factory);
132
133     webrtc::PeerConnectionInterface::RTCConfiguration config;
134     // FIXME: Add a default configuration.
135     return staticFactoryAndThreads().factory->CreatePeerConnection(config, WTFMove(portAllocator), nullptr, &observer);
136 }
137
138 rtc::scoped_refptr<webrtc::PeerConnectionInterface> LibWebRTCProvider::createPeerConnection(webrtc::PeerConnectionObserver& observer)
139 {
140     // Default WK1 implementation.
141     auto& factoryAndThreads = staticFactoryAndThreads();
142     if (!factoryAndThreads.factory) {
143         staticFactoryAndThreads().networkThreadWithSocketServer = true;
144         initializePeerConnectionFactoryAndThreads();
145     }
146     ASSERT(staticFactoryAndThreads().networkThreadWithSocketServer);
147
148     return createActualPeerConnection(observer, nullptr);
149 }
150
151 rtc::scoped_refptr<webrtc::PeerConnectionInterface> LibWebRTCProvider::createPeerConnection(webrtc::PeerConnectionObserver& observer, rtc::NetworkManager& networkManager, rtc::PacketSocketFactory& packetSocketFactory)
152 {
153     ASSERT(!staticFactoryAndThreads().networkThreadWithSocketServer);
154
155     auto& factoryAndThreads = staticFactoryAndThreads();
156     if (!factoryAndThreads.factory)
157         initializePeerConnectionFactoryAndThreads();
158
159     std::unique_ptr<cricket::BasicPortAllocator> portAllocator;
160     staticFactoryAndThreads().signalingThread->Invoke<void>(RTC_FROM_HERE, [&]() {
161         auto basicPortAllocator = std::make_unique<cricket::BasicPortAllocator>(&networkManager, &packetSocketFactory);
162         if (!m_enableEnumeratingAllNetworkInterfaces)
163             basicPortAllocator->set_flags(basicPortAllocator->flags() | cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION);
164         portAllocator = WTFMove(basicPortAllocator);
165     });
166
167     return createActualPeerConnection(observer, WTFMove(portAllocator));
168 }
169
170 } // namespace WebCore
171
172 #endif // USE(LIBWEBRTC)