[GStreamer] Use GstBus sync message handler and schedule tasks to the main thread...
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Nov 2015 10:43:52 +0000 (10:43 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 3 Nov 2015 10:43:52 +0000 (10:43 +0000)
https://bugs.webkit.org/show_bug.cgi?id=150800

Reviewed by Philippe Normand.

This way we would avoid all the GScource + polling mechanism that
GST uses internally to handle messages asynchronously in the main thread.

* platform/graphics/gstreamer/GRefPtrGStreamer.cpp:
(WTF::adoptGRef):
(WTF::refGPtr<GstMessage>):
(WTF::derefGPtr<GstMessage>):
* platform/graphics/gstreamer/GRefPtrGStreamer.h:
* platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp:
(WebCore::MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer):
Initialize the WeakPtr factory.
(WebCore::MediaPlayerPrivateGStreamer::~MediaPlayerPrivateGStreamer):
reset the GstBus sync handler.
(WebCore::MediaPlayerPrivateGStreamer::handleMessage): Make it void.
(WebCore::MediaPlayerPrivateGStreamer::createGSTPlayBin): Add a
GstBus sync message handler and schedule the messages to the main
thread with RunLoop::main().dispatch().
(WebCore::mediaPlayerPrivateMessageCallback): Deleted.
* platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h:
(WebCore::MediaPlayerPrivateGStreamer::createWeakPtr): Create a WeakPtr.
* platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp:
(WebCore::MediaPlayerPrivateGStreamerBase::handleSyncMessage):
Handle the need context message that needs to be handled in the
caller thread.
(WebCore::mediaPlayerPrivateNeedContextMessageCallback): Deleted.
(WebCore::MediaPlayerPrivateGStreamerBase::~MediaPlayerPrivateGStreamerBase):
(WebCore::MediaPlayerPrivateGStreamerBase::setPipeline): Do not
connect to sync-message signal, handleSyncMessage() will be called
to handled messages synchronously.
(WebCore::MediaPlayerPrivateGStreamerBase::handleNeedContextMessage): Deleted.
* platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h:

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

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.cpp
Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.h
Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp
Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h
Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp
Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h

index 2924487685384e9f25a7580c8ced6c43241dd739..69e09ca845dedfde4b8ebe013029072036e29ded 100644 (file)
@@ -1,3 +1,42 @@
+2015-11-03  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [GStreamer] Use GstBus sync message handler and schedule tasks to the main thread with RunLoop::dispatch
+        https://bugs.webkit.org/show_bug.cgi?id=150800
+
+        Reviewed by Philippe Normand.
+
+        This way we would avoid all the GScource + polling mechanism that
+        GST uses internally to handle messages asynchronously in the main thread.
+
+        * platform/graphics/gstreamer/GRefPtrGStreamer.cpp:
+        (WTF::adoptGRef):
+        (WTF::refGPtr<GstMessage>):
+        (WTF::derefGPtr<GstMessage>):
+        * platform/graphics/gstreamer/GRefPtrGStreamer.h:
+        * platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp:
+        (WebCore::MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer):
+        Initialize the WeakPtr factory.
+        (WebCore::MediaPlayerPrivateGStreamer::~MediaPlayerPrivateGStreamer):
+        reset the GstBus sync handler.
+        (WebCore::MediaPlayerPrivateGStreamer::handleMessage): Make it void.
+        (WebCore::MediaPlayerPrivateGStreamer::createGSTPlayBin): Add a
+        GstBus sync message handler and schedule the messages to the main
+        thread with RunLoop::main().dispatch().
+        (WebCore::mediaPlayerPrivateMessageCallback): Deleted.
+        * platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h:
+        (WebCore::MediaPlayerPrivateGStreamer::createWeakPtr): Create a WeakPtr.
+        * platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp:
+        (WebCore::MediaPlayerPrivateGStreamerBase::handleSyncMessage):
+        Handle the need context message that needs to be handled in the
+        caller thread.
+        (WebCore::mediaPlayerPrivateNeedContextMessageCallback): Deleted.
+        (WebCore::MediaPlayerPrivateGStreamerBase::~MediaPlayerPrivateGStreamerBase):
+        (WebCore::MediaPlayerPrivateGStreamerBase::setPipeline): Do not
+        connect to sync-message signal, handleSyncMessage() will be called
+        to handled messages synchronously.
+        (WebCore::MediaPlayerPrivateGStreamerBase::handleNeedContextMessage): Deleted.
+        * platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.h:
+
 2015-11-03  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         [GStreamer] Cleanup the iradio properties
index 667bd76cec11448ee2677ad8dffde5ae9bfa8d97..842d937ee37cfad52a828472a11e374a7672743e 100644 (file)
@@ -259,6 +259,25 @@ template<> void derefGPtr<GstToc>(GstToc* ptr)
         gst_toc_unref(ptr);
 }
 
+template<> GRefPtr<GstMessage> adoptGRef(GstMessage* ptr)
+{
+    return GRefPtr<GstMessage>(ptr, GRefPtrAdopt);
+}
+
+template<> GstMessage* refGPtr<GstMessage>(GstMessage* ptr)
+{
+    if (ptr)
+        return gst_message_ref(ptr);
+
+    return ptr;
+}
+
+template<> void derefGPtr<GstMessage>(GstMessage* ptr)
+{
+    if (ptr)
+        gst_message_unref(ptr);
+}
+
 template <> GRefPtr<WebKitVideoSink> adoptGRef(WebKitVideoSink* ptr)
 {
     ASSERT(!ptr || !g_object_is_floating(G_OBJECT(ptr)));
index 9739c7cb2aba50cada85ff4b97bd88cd87d10e36..6802458957c458e3d7673f3987330e3c920d3302 100644 (file)
@@ -35,6 +35,7 @@ typedef struct _GstSample GstSample;
 typedef struct _GstTagList GstTagList;
 typedef struct _GstEvent GstEvent;
 typedef struct _GstToc GstToc;
+typedef struct _GstMessage GstMessage;
 typedef struct _WebKitVideoSink WebKitVideoSink;
 typedef struct _WebKitWebSrc WebKitWebSrc;
 
@@ -88,6 +89,10 @@ template<> GRefPtr<GstToc> adoptGRef(GstToc* ptr);
 template<> GstToc* refGPtr<GstToc>(GstToc* ptr);
 template<> void derefGPtr<GstToc>(GstToc* ptr);
 
+template<> GRefPtr<GstMessage> adoptGRef(GstMessage*);
+template<> GstMessage* refGPtr<GstMessage>(GstMessage*);
+template<> void derefGPtr<GstMessage>(GstMessage*);
+
 template<> GRefPtr<WebKitVideoSink> adoptGRef(WebKitVideoSink* ptr);
 template<> WebKitVideoSink* refGPtr<WebKitVideoSink>(WebKitVideoSink* ptr);
 template<> void derefGPtr<WebKitVideoSink>(WebKitVideoSink* ptr);
index 986d0fadef67c2283a85b93a3a364ca8e7778bd0..0a5670ce78c96380fd500b6fc39b84b5ab87d845 100644 (file)
@@ -41,6 +41,7 @@
 #include <limits>
 #include <wtf/HexNumber.h>
 #include <wtf/MediaTime.h>
+#include <wtf/RunLoop.h>
 #include <wtf/glib/GUniquePtr.h>
 #include <wtf/text/CString.h>
 
@@ -80,11 +81,6 @@ using namespace std;
 
 namespace WebCore {
 
-static gboolean mediaPlayerPrivateMessageCallback(GstBus*, GstMessage* message, MediaPlayerPrivateGStreamer* player)
-{
-    return player->handleMessage(message);
-}
-
 static void mediaPlayerPrivateSourceChangedCallback(GObject*, GParamSpec*, MediaPlayerPrivateGStreamer* player)
 {
     player->sourceChanged();
@@ -174,6 +170,7 @@ bool MediaPlayerPrivateGStreamer::isAvailable()
 
 MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer(MediaPlayer* player)
     : MediaPlayerPrivateGStreamerBase(player)
+    , m_weakPtrFactory(this)
     , m_source(0)
     , m_seekTime(0)
     , m_changingRate(false)
@@ -247,8 +244,7 @@ MediaPlayerPrivateGStreamer::~MediaPlayerPrivateGStreamer()
     if (m_pipeline) {
         GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline.get())));
         ASSERT(bus);
-        g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateMessageCallback), this);
-        gst_bus_remove_signal_watch(bus.get());
+        gst_bus_set_sync_handler(bus.get(), nullptr, nullptr, nullptr);
 
         g_signal_handlers_disconnect_by_func(m_pipeline.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateSourceChangedCallback), this);
         g_signal_handlers_disconnect_by_func(m_pipeline.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateVideoChangedCallback), this);
@@ -905,7 +901,7 @@ std::unique_ptr<PlatformTimeRanges> MediaPlayerPrivateGStreamer::buffered() cons
     return timeRanges;
 }
 
-gboolean MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
+void MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
 {
     GUniqueOutPtr<GError> err;
     GUniqueOutPtr<gchar> debug;
@@ -924,7 +920,7 @@ gboolean MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
         // notify of the new location(s) of the media.
         if (!g_strcmp0(messageTypeName, "redirect")) {
             mediaLocationChanged(message);
-            return TRUE;
+            return;
         }
     }
 
@@ -1065,7 +1061,7 @@ gboolean MediaPlayerPrivateGStreamer::handleMessage(GstMessage* message)
                     GST_MESSAGE_TYPE_NAME(message));
         break;
     }
-    return TRUE;
+    return;
 }
 
 void MediaPlayerPrivateGStreamer::processBufferingStats(GstMessage* message)
@@ -1938,8 +1934,24 @@ void MediaPlayerPrivateGStreamer::createGSTPlayBin()
     setStreamVolumeElement(GST_STREAM_VOLUME(m_pipeline.get()));
 
     GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline.get())));
-    gst_bus_add_signal_watch(bus.get());
-    g_signal_connect(bus.get(), "message", G_CALLBACK(mediaPlayerPrivateMessageCallback), this);
+    gst_bus_set_sync_handler(bus.get(), [](GstBus*, GstMessage* message, gpointer userData) {
+        auto& player = *static_cast<MediaPlayerPrivateGStreamer*>(userData);
+
+        if (!player.handleSyncMessage(message)) {
+            if (isMainThread())
+                player.handleMessage(message);
+            else {
+                GRefPtr<GstMessage> protectMessage(message);
+                auto weakThis = player.createWeakPtr();
+                RunLoop::main().dispatch([weakThis, protectMessage] {
+                    if (weakThis)
+                        weakThis->handleMessage(protectMessage.get());
+                });
+            }
+        }
+        gst_message_unref(message);
+        return GST_BUS_DROP;
+    }, this, nullptr);
 
     g_object_set(m_pipeline.get(), "mute", m_player->muted(), nullptr);
 
index 26418470885a725838bd1cc51c8419f154329f52..17913df275989c9334b683f2ce2ce4cca3ffd766 100644 (file)
@@ -33,6 +33,7 @@
 #include <gst/gst.h>
 #include <gst/pbutils/install-plugins.h>
 #include <wtf/Forward.h>
+#include <wtf/WeakPtr.h>
 #include <wtf/glib/GThreadSafeMainLoopSource.h>
 
 #if ENABLE(VIDEO_TRACK) && USE(GSTREAMER_MPEGTS)
@@ -67,7 +68,7 @@ public:
     ~MediaPlayerPrivateGStreamer();
 
     static void registerMediaEngine(MediaEngineRegistrar);
-    gboolean handleMessage(GstMessage*);
+    void handleMessage(GstMessage*);
     void handlePluginInstallerResult(GstInstallPluginsReturn);
 
     bool hasVideo() const override { return m_hasVideo; }
@@ -147,6 +148,8 @@ private:
 
     static bool isAvailable();
 
+    WeakPtr<MediaPlayerPrivateGStreamer> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
+
     GstElement* createAudioSink() override;
 
     float playbackPosition() const;
@@ -172,7 +175,6 @@ private:
     bool doSeek(gint64 position, float rate, GstSeekFlags seekType);
     void updatePlaybackRate();
 
-
     String engineDescription() const override { return "GStreamer"; }
     bool isLiveStream() const override { return m_isStreaming; }
     bool didPassCORSAccessCheck() const override;
@@ -186,7 +188,8 @@ private:
     MediaTime totalFrameDelay() override { return MediaTime::zeroTime(); }
 #endif
 
-private:
+    WeakPtrFactory<MediaPlayerPrivateGStreamer> m_weakPtrFactory;
+
     GRefPtr<GstElement> m_source;
 #if ENABLE(VIDEO_TRACK)
     GRefPtr<GstElement> m_textAppSink;
index d6fdc0b7d7148620ab0299b612fddd7d22ae252a..0e055e4bff2b432e9a0bfdf01c02dcca423885cd 100644 (file)
@@ -128,11 +128,6 @@ static gboolean mediaPlayerPrivateDrawCallback(GstElement*, GstContext*, GstSamp
 }
 #endif
 
-static void mediaPlayerPrivateNeedContextMessageCallback(GstBus*, GstMessage* message, MediaPlayerPrivateGStreamerBase* player)
-{
-    player->handleNeedContextMessage(message);
-}
-
 MediaPlayerPrivateGStreamerBase::MediaPlayerPrivateGStreamerBase(MediaPlayer* player)
     : m_player(player)
     , m_fpsSink(0)
@@ -179,8 +174,6 @@ MediaPlayerPrivateGStreamerBase::~MediaPlayerPrivateGStreamerBase()
     if (m_pipeline) {
         GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline.get())));
         ASSERT(bus);
-        g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast<gpointer>(mediaPlayerPrivateNeedContextMessageCallback), this);
-        gst_bus_disable_sync_message_emission(bus.get());
         m_pipeline.clear();
     }
 
@@ -193,26 +186,25 @@ MediaPlayerPrivateGStreamerBase::~MediaPlayerPrivateGStreamerBase()
 void MediaPlayerPrivateGStreamerBase::setPipeline(GstElement* pipeline)
 {
     m_pipeline = pipeline;
-
-    GRefPtr<GstBus> bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline.get())));
-    gst_bus_enable_sync_message_emission(bus.get());
-    g_signal_connect(bus.get(), "sync-message::need-context", G_CALLBACK(mediaPlayerPrivateNeedContextMessageCallback), this);
 }
 
-void MediaPlayerPrivateGStreamerBase::handleNeedContextMessage(GstMessage* message)
+bool MediaPlayerPrivateGStreamerBase::handleSyncMessage(GstMessage* message)
 {
 #if USE(GSTREAMER_GL)
+    if (GST_MESSAGE_TYPE(message) != GST_MESSAGE_NEED_CONTEXT)
+        return false;
+
     const gchar* contextType;
     gst_message_parse_context_type(message, &contextType);
 
     if (!ensureGstGLContext())
-        return;
+        return false;
 
     if (!g_strcmp0(contextType, GST_GL_DISPLAY_CONTEXT_TYPE)) {
         GstContext* displayContext = gst_context_new(GST_GL_DISPLAY_CONTEXT_TYPE, TRUE);
         gst_context_set_gl_display(displayContext, m_glDisplay.get());
         gst_element_set_context(GST_ELEMENT(message->src), displayContext);
-        return;
+        return true;
     }
 
     if (!g_strcmp0(contextType, "gst.gl.app_context")) {
@@ -220,11 +212,13 @@ void MediaPlayerPrivateGStreamerBase::handleNeedContextMessage(GstMessage* messa
         GstStructure* structure = gst_context_writable_structure(appContext);
         gst_structure_set(structure, "context", GST_GL_TYPE_CONTEXT, m_glContext.get(), nullptr);
         gst_element_set_context(GST_ELEMENT(message->src), appContext);
-        return;
+        return true;
     }
 #else
     UNUSED_PARAM(message);
 #endif // USE(GSTREAMER_GL)
+
+    return false;
 }
 
 #if USE(GSTREAMER_GL)
index d2f88e8754c56d9036a472de3f65bc49fa2d7d71..6d6d78bd5f2c0b85fd60e8f4ae32fe2bc437af91 100644 (file)
@@ -66,7 +66,6 @@ public:
 #if USE(GSTREAMER_GL)
     bool ensureGstGLContext();
 #endif
-    void handleNeedContextMessage(GstMessage*);
 
     bool supportsMuting() const { return true; }
     void setMuted(bool);
@@ -121,6 +120,8 @@ protected:
 
     void setPipeline(GstElement*);
 
+    virtual bool handleSyncMessage(GstMessage*);
+
     MediaPlayer* m_player;
     GRefPtr<GstElement> m_pipeline;
     GRefPtr<GstStreamVolume> m_volumeElement;