LibWebRTCProvider should check existence of libwebrtc.dylib
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 1 Mar 2017 00:07:23 +0000 (00:07 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 1 Mar 2017 00:07:23 +0000 (00:07 +0000)
https://bugs.webkit.org/show_bug.cgi?id=168986
Source/WebCore:

<rdar://problem/30735413>

Reviewed by Youenn Fablet.

Some configurations of the webrtc tests overwrite the availability of WebRTC in order to test it
with MockLibWebRTCPeerConnection.  If the dylib can not be found, we can't use it, so we want to
fail these tests rather than crashing calling rtc::LogMessage::LogToDebug.

* Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp:
(WebCore::LibWebRTCMediaEndpoint::doCreateOffer):
(WebCore::LibWebRTCMediaEndpoint::doCreateAnswer):
* platform/mediastream/libwebrtc/LibWebRTCProvider.cpp:
(WebCore::LibWebRTCProvider::factory):
(WebCore::LibWebRTCProvider::webRTCAvailable):
* platform/mediastream/libwebrtc/LibWebRTCProvider.h:
* testing/MockLibWebRTCPeerConnection.cpp:
(WebCore::useMockRTCPeerConnectionFactory):
(WebCore::MockLibWebRTCPeerConnectionFactory::CreatePeerConnection):

Source/WebKit2:

Reviewed by Youenn Fablet.

* Shared/WebPreferencesDefinitions.h:
* Shared/WebPreferencesStore.cpp:
* UIProcess/WebPreferences.cpp:
(WebKit::checkWebRTCAvailability): Deleted.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@213190 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp
Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.cpp
Source/WebCore/platform/mediastream/libwebrtc/LibWebRTCProvider.h
Source/WebCore/testing/MockLibWebRTCPeerConnection.cpp
Source/WebKit2/ChangeLog
Source/WebKit2/Shared/WebPreferencesDefinitions.h
Source/WebKit2/Shared/WebPreferencesStore.cpp
Source/WebKit2/UIProcess/WebPreferences.cpp

index 5145a3c..c375b49 100644 (file)
@@ -2300,6 +2300,8 @@ set(WebCore_SOURCES
     platform/mediastream/RealtimeMediaSourceSettings.cpp
     platform/mediastream/SDPProcessorScriptResource.cpp
 
+    platform/mediastream/libwebrtc/LibWebRTCProvider.cpp
+
     platform/mock/DeviceOrientationClientMock.cpp
     platform/mock/GeolocationClientMock.cpp
     platform/mock/MockMediaEndpoint.cpp
index 4645453..31e2fbc 100644 (file)
@@ -1,3 +1,26 @@
+2017-02-28  Alex Christensen  <achristensen@webkit.org>
+
+        LibWebRTCProvider should check existence of libwebrtc.dylib
+        https://bugs.webkit.org/show_bug.cgi?id=168986
+        <rdar://problem/30735413>
+
+        Reviewed by Youenn Fablet.
+
+        Some configurations of the webrtc tests overwrite the availability of WebRTC in order to test it
+        with MockLibWebRTCPeerConnection.  If the dylib can not be found, we can't use it, so we want to
+        fail these tests rather than crashing calling rtc::LogMessage::LogToDebug.
+
+        * Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp:
+        (WebCore::LibWebRTCMediaEndpoint::doCreateOffer):
+        (WebCore::LibWebRTCMediaEndpoint::doCreateAnswer):
+        * platform/mediastream/libwebrtc/LibWebRTCProvider.cpp:
+        (WebCore::LibWebRTCProvider::factory):
+        (WebCore::LibWebRTCProvider::webRTCAvailable):
+        * platform/mediastream/libwebrtc/LibWebRTCProvider.h:
+        * testing/MockLibWebRTCPeerConnection.cpp:
+        (WebCore::useMockRTCPeerConnectionFactory):
+        (WebCore::MockLibWebRTCPeerConnectionFactory::CreatePeerConnection):
+
 2017-02-28  Tim Horton  <timothy_horton@apple.com>
 
         Apply a review comment that was missing in r213182
index ab1a539..8681ab5 100644 (file)
@@ -155,11 +155,16 @@ static inline std::string streamId(RTCPeerConnection& connection)
 
 void LibWebRTCMediaEndpoint::doCreateOffer()
 {
+    if (!LibWebRTCProvider::factory()) {
+        m_peerConnectionBackend.createOfferFailed(Exception { NOT_SUPPORTED_ERR, ASCIILiteral("libwebrtc backend is missing.") });
+        return;
+    }
+        
     m_isInitiator = true;
     auto& senders = m_peerConnectionBackend.connection().getSenders();
     if (senders.size()) {
         // FIXME: We only support one stream for the moment.
-        auto stream = LibWebRTCProvider::factory().CreateLocalMediaStream(streamId(m_peerConnectionBackend.connection()));
+        auto stream = LibWebRTCProvider::factory()->CreateLocalMediaStream(streamId(m_peerConnectionBackend.connection()));
         for (RTCRtpSender& sender : senders) {
             auto* track = sender.track();
             if (track) {
@@ -167,13 +172,13 @@ void LibWebRTCMediaEndpoint::doCreateOffer()
                 auto& source = track->source();
                 if (source.type() == RealtimeMediaSource::Audio) {
                     auto trackSource = RealtimeOutgoingAudioSource::create(source);
-                    auto rtcTrack = LibWebRTCProvider::factory().CreateAudioTrack(track->id().utf8().data(), trackSource.ptr());
+                    auto rtcTrack = LibWebRTCProvider::factory()->CreateAudioTrack(track->id().utf8().data(), trackSource.ptr());
                     trackSource->setTrack(rtc::scoped_refptr<webrtc::AudioTrackInterface>(rtcTrack));
                     m_peerConnectionBackend.addAudioSource(WTFMove(trackSource));
                     stream->AddTrack(WTFMove(rtcTrack));
                 } else {
                     auto videoSource = RealtimeOutgoingVideoSource::create(source);
-                    auto videoTrack = LibWebRTCProvider::factory().CreateVideoTrack(track->id().utf8().data(), videoSource.ptr());
+                    auto videoTrack = LibWebRTCProvider::factory()->CreateVideoTrack(track->id().utf8().data(), videoSource.ptr());
                     m_peerConnectionBackend.addVideoSource(WTFMove(videoSource));
                     stream->AddTrack(WTFMove(videoTrack));
                 }
@@ -186,12 +191,17 @@ void LibWebRTCMediaEndpoint::doCreateOffer()
 
 void LibWebRTCMediaEndpoint::doCreateAnswer()
 {
+    if (!LibWebRTCProvider::factory()) {
+        m_peerConnectionBackend.createAnswerFailed(Exception { NOT_SUPPORTED_ERR, ASCIILiteral("libwebrtc backend is missing.") });
+        return;
+    }
+
     m_isInitiator = false;
 
     auto& senders = m_peerConnectionBackend.connection().getSenders();
     if (senders.size()) {
         // FIXME: We only support one stream for the moment.
-        auto stream = LibWebRTCProvider::factory().CreateLocalMediaStream(streamId(m_peerConnectionBackend.connection()));
+        auto stream = LibWebRTCProvider::factory()->CreateLocalMediaStream(streamId(m_peerConnectionBackend.connection()));
         for (RTCRtpSender& sender : senders) {
             auto* track = sender.track();
             if (track) {
@@ -199,13 +209,13 @@ void LibWebRTCMediaEndpoint::doCreateAnswer()
                 auto& source = track->source();
                 if (source.type() == RealtimeMediaSource::Audio) {
                     auto trackSource = RealtimeOutgoingAudioSource::create(source);
-                    auto rtcTrack = LibWebRTCProvider::factory().CreateAudioTrack(track->id().utf8().data(), trackSource.ptr());
+                    auto rtcTrack = LibWebRTCProvider::factory()->CreateAudioTrack(track->id().utf8().data(), trackSource.ptr());
                     trackSource->setTrack(rtc::scoped_refptr<webrtc::AudioTrackInterface>(rtcTrack));
                     m_peerConnectionBackend.addAudioSource(WTFMove(trackSource));
                     stream->AddTrack(WTFMove(rtcTrack));
                 } else {
                     auto videoSource = RealtimeOutgoingVideoSource::create(source);
-                    auto videoTrack = LibWebRTCProvider::factory().CreateVideoTrack(track->id().utf8().data(), videoSource.ptr());
+                    auto videoTrack = LibWebRTCProvider::factory()->CreateVideoTrack(track->id().utf8().data(), videoSource.ptr());
                     m_peerConnectionBackend.addVideoSource(WTFMove(videoSource));
                     stream->AddTrack(WTFMove(videoTrack));
                 }
index 8ed5a5a..d7cbecc 100644 (file)
@@ -27,9 +27,9 @@
 #include "LibWebRTCProvider.h"
 
 #if USE(LIBWEBRTC)
-
 #include "LibWebRTCAudioModule.h"
 #include "Logging.h"
+#include <dlfcn.h>
 #include <webrtc/api/peerconnectionfactory.h>
 #include <webrtc/api/peerconnectionfactoryproxy.h>
 #include <webrtc/base/physicalsocketserver.h>
 #include <webrtc/sdk/objc/Framework/Classes/videotoolboxvideocodecfactory.h>
 #include <wtf/Function.h>
 #include <wtf/NeverDestroyed.h>
+#endif
 
 namespace WebCore {
 
+#if USE(LIBWEBRTC)
 struct PeerConnectionFactoryAndThreads : public rtc::MessageHandler {
     std::unique_ptr<LibWebRTCAudioModule> audioDeviceModule;
     std::unique_ptr<rtc::Thread> networkThread;
@@ -111,11 +113,13 @@ static void initializePeerConnectionFactoryAndThreads()
     ASSERT(factoryAndThreads.factory);
 }
 
-webrtc::PeerConnectionFactoryInterface& LibWebRTCProvider::factory()
+webrtc::PeerConnectionFactoryInterface* LibWebRTCProvider::factory()
 {
+    if (!webRTCAvailable())
+        return nullptr;
     if (!staticFactoryAndThreads().factory)
         initializePeerConnectionFactoryAndThreads();
-    return *staticFactoryAndThreads().factory;
+    return staticFactoryAndThreads().factory;
 }
 
 void LibWebRTCProvider::setPeerConnectionFactory(rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>&& factory)
@@ -166,7 +170,22 @@ rtc::scoped_refptr<webrtc::PeerConnectionInterface> LibWebRTCProvider::createPee
 
     return createActualPeerConnection(observer, WTFMove(portAllocator));
 }
+#endif // USE(LIBWEBRTC)
 
-} // namespace WebCore
+bool LibWebRTCProvider::webRTCAvailable()
+{
+#if USE(LIBWEBRTC)
+    static bool available = [] {
+        void* libwebrtcLibrary = dlopen("libwebrtc.dylib", RTLD_LAZY);
+        if (!libwebrtcLibrary)
+            return false;
+        dlclose(libwebrtcLibrary);
+        return true;
+    }();
+    return available;
+#else
+    return true;
+#endif
+}
 
-#endif // USE(LIBWEBRTC)
+} // namespace WebCore
index 1f54809..790bbb5 100644 (file)
@@ -50,13 +50,14 @@ public:
     LibWebRTCProvider() = default;
     virtual ~LibWebRTCProvider() = default;
 
+    static bool webRTCAvailable();
 #if USE(LIBWEBRTC)
     WEBCORE_EXPORT virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface> createPeerConnection(webrtc::PeerConnectionObserver&);
 
     // FIXME: Make these methods not static.
     static WEBCORE_EXPORT void callOnWebRTCNetworkThread(Function<void()>&&);
     static WEBCORE_EXPORT void callOnWebRTCSignalingThread(Function<void()>&&);
-    static WEBCORE_EXPORT webrtc::PeerConnectionFactoryInterface& factory();
+    static WEBCORE_EXPORT webrtc::PeerConnectionFactoryInterface* factory();
     // Used for mock testing
     static void setPeerConnectionFactory(rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>&&);
 
index 9634931..2f80afa 100644 (file)
@@ -51,7 +51,7 @@ void useMockRTCPeerConnectionFactory(LibWebRTCProvider* provider, const String&
 {
     if (provider && !realPeerConnectionFactory()) {
         auto& factory = getRealPeerConnectionFactory();
-        factory = &provider->factory();
+        factory = provider->factory();
     }
 
     LibWebRTCProvider::setPeerConnectionFactory(MockLibWebRTCPeerConnectionFactory::create(String(testCase)));
@@ -162,6 +162,9 @@ MockLibWebRTCPeerConnectionFactory::MockLibWebRTCPeerConnectionFactory(String&&
 
 rtc::scoped_refptr<webrtc::PeerConnectionInterface> MockLibWebRTCPeerConnectionFactory::CreatePeerConnection(const webrtc::PeerConnectionInterface::RTCConfiguration& configuration, std::unique_ptr<cricket::PortAllocator> portAllocator, std::unique_ptr<rtc::RTCCertificateGeneratorInterface> generator, webrtc::PeerConnectionObserver* observer)
 {
+    if (!realPeerConnectionFactory())
+        return nullptr;
+
     if (m_numberOfRealPeerConnections) {
         auto connection = realPeerConnectionFactory()->CreatePeerConnection(configuration, WTFMove(portAllocator), WTFMove(generator), observer);
         --m_numberOfRealPeerConnections;
index 5aa4293..6f14146 100644 (file)
@@ -1,3 +1,15 @@
+2017-02-28  Alex Christensen  <achristensen@webkit.org>
+
+        LibWebRTCProvider should check existence of libwebrtc.dylib
+        https://bugs.webkit.org/show_bug.cgi?id=168986
+
+        Reviewed by Youenn Fablet.
+
+        * Shared/WebPreferencesDefinitions.h:
+        * Shared/WebPreferencesStore.cpp:
+        * UIProcess/WebPreferences.cpp:
+        (WebKit::checkWebRTCAvailability): Deleted.
+
 2017-02-28  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         Unreviewed, build fix after r213176.
index bef2754..d16134d 100644 (file)
 //   DEFAULT_EXPERIMENTAL_FEATURE_ENABLED (for features that are ready for
 //   wider testing).
 
-// Disable webrtc feature if libwebrtc library is not present
-namespace WebKit {
-bool checkWebRTCAvailability();
-}
-
 #define FOR_EACH_WEBKIT_EXPERIMENTAL_FEATURE_PREFERENCE(macro) \
     macro(SpringTimingFunctionEnabled, springTimingFunctionEnabled, Bool, bool, DEFAULT_EXPERIMENTAL_FEATURES_ENABLED, "CSS Spring Animations", "CSS Spring Animation prototype") \
     macro(GamepadsEnabled, gamepadsEnabled, Bool, bool, DEFAULT_EXPERIMENTAL_FEATURES_ENABLED, "Gamepads", "Web Gamepad API support") \
@@ -328,7 +323,7 @@ bool checkWebRTCAvailability();
     macro(UserTimingEnabled, userTimingEnabled, Bool, bool, DEFAULT_EXPERIMENTAL_FEATURES_ENABLED, "User Timing", "Enable UserTiming API") \
     macro(WebAnimationsEnabled, webAnimationsEnabled, Bool, bool, false, "Web Animations", "Web Animations prototype") \
     macro(WebGL2Enabled, webGL2Enabled, Bool, bool, DEFAULT_EXPERIMENTAL_FEATURES_ENABLED, "WebGL 2.0", "WebGL 2 prototype") \
-    macro(PeerConnectionEnabled, peerConnectionEnabled, Bool, bool, checkWebRTCAvailability(), "WebRTC", "Enable WebRTC API") \
+    macro(PeerConnectionEnabled, peerConnectionEnabled, Bool, bool, WebCore::LibWebRTCProvider::webRTCAvailable(), "WebRTC", "Enable WebRTC API") \
     \
 
 #if PLATFORM(COCOA)
index 4585783..f0eb387 100644 (file)
@@ -29,6 +29,7 @@
 #include "FontSmoothingLevel.h"
 #include "WebCoreArgumentCoders.h"
 #include "WebPreferencesKeys.h"
+#include <WebCore/LibWebRTCProvider.h>
 #include <WebCore/Settings.h>
 #include <WebCore/TextEncodingRegistry.h>
 #include <wtf/NeverDestroyed.h>
index 4cec876..3a3df81 100644 (file)
@@ -29,7 +29,7 @@
 #include "WebPageGroup.h"
 #include "WebPreferencesKeys.h"
 #include "WebProcessPool.h"
-#include <dlfcn.h>
+#include <WebCore/LibWebRTCProvider.h>
 #include <wtf/NeverDestroyed.h>
 #include <wtf/ThreadingPrimitives.h>
 
@@ -196,22 +196,6 @@ FOR_EACH_WEBKIT_DEBUG_PREFERENCE(DEFINE_PREFERENCE_GETTER_AND_SETTERS)
 
 #undef DEFINE_PREFERENCE_GETTER_AND_SETTERS
 
-bool checkWebRTCAvailability()
-{
-#if USE(LIBWEBRTC)
-    static bool available = [&] {
-        void* libwebrtcLibrary = dlopen("libwebrtc.dylib", RTLD_LAZY);
-        if (!libwebrtcLibrary)
-            return false;
-        dlclose(libwebrtcLibrary);
-        return true;
-    }();
-    return available;
-#else
-    return true;
-#endif
-}
-
 #define DEFINE_EXPERIMENTAL_PREFERENCE_GETTER_AND_SETTERS(KeyUpper, KeyLower, TypeName, Type, DefaultValue, HumanReadableName, HumanReadableDescription) \
     void WebPreferences::set##KeyUpper(const Type& value) \
     { \