<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[192511] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/192511">192511</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2015-11-17 01:57:42 -0800 (Tue, 17 Nov 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>[GStreamer] Use RunLoop instead of GMainLoop in AudioFileReaderGStreamer
https://bugs.webkit.org/show_bug.cgi?id=151256

Reviewed by Žan Doberšek.

Use RunLoop instead of the platform specific code. The AudioBus
can be created from any thread, so we create a helper thread to
ensure we don't use the main RunLoop.

This patch also includes some code cleanups:
  - Uses smart pointers when possible.
  - Fixes uninitialized members in constructors.
  - Makes private members private.
  - Uses lambdas instead of static non-members functions.
  - nullptr instead of 0 in some places.

* platform/audio/gstreamer/AudioFileReaderGStreamer.cpp:
(WebCore::AudioFileReader::createWeakPtr):
(WebCore::AudioFileReader::deinterleavePadAddedCallback):
(WebCore::AudioFileReader::deinterleaveReadyCallback):
(WebCore::AudioFileReader::decodebinPadAddedCallback):
(WebCore::AudioFileReader::AudioFileReader):
(WebCore::AudioFileReader::~AudioFileReader):
(WebCore::AudioFileReader::handleSample):
(WebCore::AudioFileReader::handleMessage):
(WebCore::AudioFileReader::handleNewDeinterleavePad):
(WebCore::AudioFileReader::deinterleavePadsConfigured):
(WebCore::AudioFileReader::plugDeinterleave):
(WebCore::AudioFileReader::decodeAudioForBusCreation):
(WebCore::AudioFileReader::createBus):
(WebCore::createBusFromAudioFile):
(WebCore::createBusFromInMemoryAudioFile):
(WebCore::onAppsinkPullRequiredCallback): Deleted.
(WebCore::messageCallback): Deleted.
(WebCore::onGStreamerDeinterleavePadAddedCallback): Deleted.
(WebCore::onGStreamerDeinterleaveReadyCallback): Deleted.
(WebCore::onGStreamerDecodebinPadAddedCallback): Deleted.
* platform/graphics/gstreamer/GRefPtrGStreamer.cpp:
(WTF::adoptGRef):
(WTF::refGPtr&lt;GstBufferList&gt;):
(WTF::derefGPtr&lt;GstBufferList&gt;):
* platform/graphics/gstreamer/GRefPtrGStreamer.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformaudiogstreamerAudioFileReaderGStreamercpp">trunk/Source/WebCore/platform/audio/gstreamer/AudioFileReaderGStreamer.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsgstreamerGRefPtrGStreamercpp">trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsgstreamerGRefPtrGStreamerh">trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (192510 => 192511)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-11-17 08:37:21 UTC (rev 192510)
+++ trunk/Source/WebCore/ChangeLog        2015-11-17 09:57:42 UTC (rev 192511)
</span><span class="lines">@@ -1,3 +1,48 @@
</span><ins>+2015-11-17  Carlos Garcia Campos  &lt;cgarcia@igalia.com&gt;
+
+        [GStreamer] Use RunLoop instead of GMainLoop in AudioFileReaderGStreamer
+        https://bugs.webkit.org/show_bug.cgi?id=151256
+
+        Reviewed by Žan Doberšek.
+
+        Use RunLoop instead of the platform specific code. The AudioBus
+        can be created from any thread, so we create a helper thread to
+        ensure we don't use the main RunLoop.
+
+        This patch also includes some code cleanups:
+          - Uses smart pointers when possible.
+          - Fixes uninitialized members in constructors.
+          - Makes private members private.
+          - Uses lambdas instead of static non-members functions.
+          - nullptr instead of 0 in some places.
+
+        * platform/audio/gstreamer/AudioFileReaderGStreamer.cpp:
+        (WebCore::AudioFileReader::createWeakPtr):
+        (WebCore::AudioFileReader::deinterleavePadAddedCallback):
+        (WebCore::AudioFileReader::deinterleaveReadyCallback):
+        (WebCore::AudioFileReader::decodebinPadAddedCallback):
+        (WebCore::AudioFileReader::AudioFileReader):
+        (WebCore::AudioFileReader::~AudioFileReader):
+        (WebCore::AudioFileReader::handleSample):
+        (WebCore::AudioFileReader::handleMessage):
+        (WebCore::AudioFileReader::handleNewDeinterleavePad):
+        (WebCore::AudioFileReader::deinterleavePadsConfigured):
+        (WebCore::AudioFileReader::plugDeinterleave):
+        (WebCore::AudioFileReader::decodeAudioForBusCreation):
+        (WebCore::AudioFileReader::createBus):
+        (WebCore::createBusFromAudioFile):
+        (WebCore::createBusFromInMemoryAudioFile):
+        (WebCore::onAppsinkPullRequiredCallback): Deleted.
+        (WebCore::messageCallback): Deleted.
+        (WebCore::onGStreamerDeinterleavePadAddedCallback): Deleted.
+        (WebCore::onGStreamerDeinterleaveReadyCallback): Deleted.
+        (WebCore::onGStreamerDecodebinPadAddedCallback): Deleted.
+        * platform/graphics/gstreamer/GRefPtrGStreamer.cpp:
+        (WTF::adoptGRef):
+        (WTF::refGPtr&lt;GstBufferList&gt;):
+        (WTF::derefGPtr&lt;GstBufferList&gt;):
+        * platform/graphics/gstreamer/GRefPtrGStreamer.h:
+
</ins><span class="cx"> 2015-11-16  Eric Carlson  &lt;eric.carlson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [MediaStream] VideoTrack should respond to MediaStreamTrack state changes
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudiogstreamerAudioFileReaderGStreamercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/gstreamer/AudioFileReaderGStreamer.cpp (192510 => 192511)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/gstreamer/AudioFileReaderGStreamer.cpp        2015-11-17 08:37:21 UTC (rev 192510)
+++ trunk/Source/WebCore/platform/audio/gstreamer/AudioFileReaderGStreamer.cpp        2015-11-17 09:57:42 UTC (rev 192511)
</span><span class="lines">@@ -22,16 +22,18 @@
</span><span class="cx"> #if ENABLE(WEB_AUDIO)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;AudioFileReader.h&quot;
</span><del>-
</del><span class="cx"> #include &quot;AudioBus.h&quot;
</span><del>-
</del><ins>+#include &quot;GRefPtrGStreamer.h&quot;
</ins><span class="cx"> #include &lt;gio/gio.h&gt;
</span><span class="cx"> #include &lt;gst/app/gstappsink.h&gt;
</span><span class="cx"> #include &lt;gst/audio/audio-info.h&gt;
</span><span class="cx"> #include &lt;gst/gst.h&gt;
</span><ins>+#include &lt;wtf/MainThread.h&gt;
</ins><span class="cx"> #include &lt;wtf/Noncopyable.h&gt;
</span><ins>+#include &lt;wtf/RunLoop.h&gt;
+#include &lt;wtf/Threading.h&gt;
+#include &lt;wtf/WeakPtr.h&gt;
</ins><span class="cx"> #include &lt;wtf/glib/GRefPtr.h&gt;
</span><del>-#include &lt;wtf/glib/GThreadSafeMainLoopSource.h&gt;
</del><span class="cx"> #include &lt;wtf/glib/GUniquePtr.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="lines">@@ -45,29 +47,36 @@
</span><span class="cx"> 
</span><span class="cx">     PassRefPtr&lt;AudioBus&gt; createBus(float sampleRate, bool mixToMono);
</span><span class="cx"> 
</span><del>-    GstFlowReturn handleSample(GstAppSink*);
-    gboolean handleMessage(GstMessage*);
</del><ins>+private:
+    WeakPtr&lt;AudioFileReader&gt; createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
+
+    static void deinterleavePadAddedCallback(AudioFileReader*, GstPad*);
+    static void deinterleaveReadyCallback(AudioFileReader*);
+    static void decodebinPadAddedCallback(AudioFileReader*, GstPad*);
+
+    void handleMessage(GstMessage*);
</ins><span class="cx">     void handleNewDeinterleavePad(GstPad*);
</span><span class="cx">     void deinterleavePadsConfigured();
</span><span class="cx">     void plugDeinterleave(GstPad*);
</span><span class="cx">     void decodeAudioForBusCreation();
</span><ins>+    GstFlowReturn handleSample(GstAppSink*);
</ins><span class="cx"> 
</span><del>-private:
-    const void* m_data;
-    size_t m_dataSize;
-    const char* m_filePath;
</del><ins>+    WeakPtrFactory&lt;AudioFileReader&gt; m_weakPtrFactory;
+    RunLoop&amp; m_runLoop;
+    const void* m_data { nullptr };
+    size_t m_dataSize { 0 };
+    const char* m_filePath { nullptr };
</ins><span class="cx"> 
</span><del>-    float m_sampleRate;
-    int m_channels;
-    GstBufferList* m_frontLeftBuffers;
-    GstBufferList* m_frontRightBuffers;
</del><ins>+    float m_sampleRate { 0 };
+    int m_channels { 0 };
+    GRefPtr&lt;GstBufferList&gt; m_frontLeftBuffers;
+    GRefPtr&lt;GstBufferList&gt; m_frontRightBuffers;
</ins><span class="cx"> 
</span><del>-    GstElement* m_pipeline;
-    unsigned m_channelSize;
</del><ins>+    GRefPtr&lt;GstElement&gt; m_pipeline;
+    unsigned m_channelSize { 0 };
</ins><span class="cx">     GRefPtr&lt;GstElement&gt; m_decodebin;
</span><span class="cx">     GRefPtr&lt;GstElement&gt; m_deInterleave;
</span><del>-    GRefPtr&lt;GMainLoop&gt; m_loop;
-    bool m_errorOccurred;
</del><ins>+    bool m_errorOccurred { false };
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> static void copyGstreamerBuffersToAudioChannel(GstBufferList* buffers, AudioChannel* audioChannel)
</span><span class="lines">@@ -83,92 +92,71 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static GstFlowReturn onAppsinkPullRequiredCallback(GstAppSink* sink, gpointer userData)
</del><ins>+void AudioFileReader::deinterleavePadAddedCallback(AudioFileReader* reader, GstPad* pad)
</ins><span class="cx"> {
</span><del>-    return static_cast&lt;AudioFileReader*&gt;(userData)-&gt;handleSample(sink);
-}
-
-gboolean messageCallback(GstBus*, GstMessage* message, AudioFileReader* reader)
-{
-    return reader-&gt;handleMessage(message);
-}
-
-static void onGStreamerDeinterleavePadAddedCallback(GstElement*, GstPad* pad, AudioFileReader* reader)
-{
</del><span class="cx">     reader-&gt;handleNewDeinterleavePad(pad);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void onGStreamerDeinterleaveReadyCallback(GstElement*, AudioFileReader* reader)
</del><ins>+void AudioFileReader::deinterleaveReadyCallback(AudioFileReader* reader)
</ins><span class="cx"> {
</span><span class="cx">     reader-&gt;deinterleavePadsConfigured();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static void onGStreamerDecodebinPadAddedCallback(GstElement*, GstPad* pad, AudioFileReader* reader)
</del><ins>+void AudioFileReader::decodebinPadAddedCallback(AudioFileReader* reader, GstPad* pad)
</ins><span class="cx"> {
</span><span class="cx">     reader-&gt;plugDeinterleave(pad);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> AudioFileReader::AudioFileReader(const char* filePath)
</span><del>-    : m_data(0)
-    , m_dataSize(0)
</del><ins>+    : m_weakPtrFactory(this)
+    , m_runLoop(RunLoop::current())
</ins><span class="cx">     , m_filePath(filePath)
</span><del>-    , m_channelSize(0)
-    , m_errorOccurred(false)
</del><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> AudioFileReader::AudioFileReader(const void* data, size_t dataSize)
</span><del>-    : m_data(data)
</del><ins>+    : m_weakPtrFactory(this)
+    , m_runLoop(RunLoop::current())
+    , m_data(data)
</ins><span class="cx">     , m_dataSize(dataSize)
</span><del>-    , m_filePath(0)
-    , m_channelSize(0)
-    , m_errorOccurred(false)
</del><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> AudioFileReader::~AudioFileReader()
</span><span class="cx"> {
</span><span class="cx">     if (m_pipeline) {
</span><del>-        GRefPtr&lt;GstBus&gt; bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline)));
</del><ins>+        GRefPtr&lt;GstBus&gt; bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline.get())));
</ins><span class="cx">         ASSERT(bus);
</span><del>-        g_signal_handlers_disconnect_by_func(bus.get(), reinterpret_cast&lt;gpointer&gt;(messageCallback), this);
</del><ins>+        gst_bus_set_sync_handler(bus.get(), nullptr, nullptr, nullptr);
</ins><span class="cx"> 
</span><del>-        gst_element_set_state(m_pipeline, GST_STATE_NULL);
-        gst_object_unref(GST_OBJECT(m_pipeline));
</del><ins>+        gst_element_set_state(m_pipeline.get(), GST_STATE_NULL);
+        m_pipeline = nullptr;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (m_decodebin) {
</span><del>-        g_signal_handlers_disconnect_by_func(m_decodebin.get(), reinterpret_cast&lt;gpointer&gt;(onGStreamerDecodebinPadAddedCallback), this);
-        m_decodebin.clear();
</del><ins>+        g_signal_handlers_disconnect_matched(m_decodebin.get(), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, this);
+        m_decodebin = nullptr;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (m_deInterleave) {
</span><del>-        g_signal_handlers_disconnect_by_func(m_deInterleave.get(), reinterpret_cast&lt;gpointer&gt;(onGStreamerDeinterleavePadAddedCallback), this);
-        g_signal_handlers_disconnect_by_func(m_deInterleave.get(), reinterpret_cast&lt;gpointer&gt;(onGStreamerDeinterleaveReadyCallback), this);
-        m_deInterleave.clear();
</del><ins>+        g_signal_handlers_disconnect_matched(m_deInterleave.get(), G_SIGNAL_MATCH_DATA, 0, 0, nullptr, nullptr, this);
+        m_deInterleave = nullptr;
</ins><span class="cx">     }
</span><del>-
-    gst_buffer_list_unref(m_frontLeftBuffers);
-    gst_buffer_list_unref(m_frontRightBuffers);
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> GstFlowReturn AudioFileReader::handleSample(GstAppSink* sink)
</span><span class="cx"> {
</span><del>-    GstSample* sample = gst_app_sink_pull_sample(sink);
</del><ins>+    GRefPtr&lt;GstSample&gt; sample = adoptGRef(gst_app_sink_pull_sample(sink));
</ins><span class="cx">     if (!sample)
</span><span class="cx">         return GST_FLOW_ERROR;
</span><span class="cx"> 
</span><del>-    GstBuffer* buffer = gst_sample_get_buffer(sample);
-    if (!buffer) {
-        gst_sample_unref(sample);
</del><ins>+    GstBuffer* buffer = gst_sample_get_buffer(sample.get());
+    if (!buffer)
</ins><span class="cx">         return GST_FLOW_ERROR;
</span><del>-    }
</del><span class="cx"> 
</span><del>-    GstCaps* caps = gst_sample_get_caps(sample);
-    if (!caps) {
-        gst_sample_unref(sample);
</del><ins>+    GstCaps* caps = gst_sample_get_caps(sample.get());
+    if (!caps)
</ins><span class="cx">         return GST_FLOW_ERROR;
</span><del>-    }
</del><span class="cx"> 
</span><span class="cx">     GstAudioInfo info;
</span><span class="cx">     gst_audio_info_from_caps(&amp;info, caps);
</span><span class="lines">@@ -179,28 +167,29 @@
</span><span class="cx">     switch (GST_AUDIO_INFO_POSITION(&amp;info, 0)) {
</span><span class="cx">     case GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT:
</span><span class="cx">     case GST_AUDIO_CHANNEL_POSITION_MONO:
</span><del>-        gst_buffer_list_add(m_frontLeftBuffers, gst_buffer_ref(buffer));
</del><ins>+        gst_buffer_list_add(m_frontLeftBuffers.get(), gst_buffer_ref(buffer));
</ins><span class="cx">         m_channelSize += frames;
</span><span class="cx">         break;
</span><span class="cx">     case GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT:
</span><del>-        gst_buffer_list_add(m_frontRightBuffers, gst_buffer_ref(buffer));
</del><ins>+        gst_buffer_list_add(m_frontRightBuffers.get(), gst_buffer_ref(buffer));
</ins><span class="cx">         break;
</span><span class="cx">     default:
</span><span class="cx">         break;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    gst_sample_unref(sample);
</del><span class="cx">     return GST_FLOW_OK;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-gboolean AudioFileReader::handleMessage(GstMessage* message)
</del><ins>+void AudioFileReader::handleMessage(GstMessage* message)
</ins><span class="cx"> {
</span><ins>+    ASSERT(&amp;m_runLoop == &amp;RunLoop::current());
+
</ins><span class="cx">     GUniqueOutPtr&lt;GError&gt; error;
</span><span class="cx">     GUniqueOutPtr&lt;gchar&gt; debug;
</span><span class="cx"> 
</span><span class="cx">     switch (GST_MESSAGE_TYPE(message)) {
</span><span class="cx">     case GST_MESSAGE_EOS:
</span><del>-        g_main_loop_quit(m_loop.get());
</del><ins>+        m_runLoop.stop();
</ins><span class="cx">         break;
</span><span class="cx">     case GST_MESSAGE_WARNING:
</span><span class="cx">         gst_message_parse_warning(message, &amp;error.outPtr(), &amp;debug.outPtr());
</span><span class="lines">@@ -210,13 +199,12 @@
</span><span class="cx">         gst_message_parse_error(message, &amp;error.outPtr(), &amp;debug.outPtr());
</span><span class="cx">         g_warning(&quot;Error: %d, %s. Debug output: %s&quot;, error-&gt;code,  error-&gt;message, debug.get());
</span><span class="cx">         m_errorOccurred = true;
</span><del>-        gst_element_set_state(m_pipeline, GST_STATE_NULL);
-        g_main_loop_quit(m_loop.get());
</del><ins>+        gst_element_set_state(m_pipeline.get(), GST_STATE_NULL);
+        m_runLoop.stop();
</ins><span class="cx">         break;
</span><span class="cx">     default:
</span><span class="cx">         break;
</span><span class="cx">     }
</span><del>-    return TRUE;
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void AudioFileReader::handleNewDeinterleavePad(GstPad* pad)
</span><span class="lines">@@ -228,19 +216,23 @@
</span><span class="cx">     GstElement* queue = gst_element_factory_make(&quot;queue&quot;, 0);
</span><span class="cx">     GstElement* sink = gst_element_factory_make(&quot;appsink&quot;, 0);
</span><span class="cx"> 
</span><del>-    GstAppSinkCallbacks callbacks;
-    callbacks.eos = 0;
-    callbacks.new_preroll = 0;
-    callbacks.new_sample = onAppsinkPullRequiredCallback;
</del><ins>+    static GstAppSinkCallbacks callbacks = {
+        nullptr, // eos
+        nullptr, // new_preroll
+        // new_sample
+        [](GstAppSink* sink, gpointer userData) -&gt; GstFlowReturn {
+            return static_cast&lt;AudioFileReader*&gt;(userData)-&gt;handleSample(sink);
+        },
+        { nullptr }
+    };
</ins><span class="cx">     gst_app_sink_set_callbacks(GST_APP_SINK(sink), &amp;callbacks, this, 0);
</span><span class="cx"> 
</span><span class="cx">     g_object_set(sink, &quot;sync&quot;, FALSE, NULL);
</span><span class="cx"> 
</span><del>-    gst_bin_add_many(GST_BIN(m_pipeline), queue, sink, NULL);
</del><ins>+    gst_bin_add_many(GST_BIN(m_pipeline.get()), queue, sink, nullptr);
</ins><span class="cx"> 
</span><del>-    GstPad* sinkPad = gst_element_get_static_pad(queue, &quot;sink&quot;);
-    gst_pad_link_full(pad, sinkPad, GST_PAD_LINK_CHECK_NOTHING);
-    gst_object_unref(GST_OBJECT(sinkPad));
</del><ins>+    GRefPtr&lt;GstPad&gt; sinkPad = adoptGRef(gst_element_get_static_pad(queue, &quot;sink&quot;));
+    gst_pad_link_full(pad, sinkPad.get(), GST_PAD_LINK_CHECK_NOTHING);
</ins><span class="cx"> 
</span><span class="cx">     gst_element_link_pads_full(queue, &quot;src&quot;, sink, &quot;sink&quot;, GST_PAD_LINK_CHECK_NOTHING);
</span><span class="cx"> 
</span><span class="lines">@@ -252,7 +244,7 @@
</span><span class="cx"> {
</span><span class="cx">     // All deinterleave src pads are now available, let's roll to
</span><span class="cx">     // PLAYING so data flows towards the sinks and it can be retrieved.
</span><del>-    gst_element_set_state(m_pipeline, GST_STATE_PLAYING);
</del><ins>+    gst_element_set_state(m_pipeline.get(), GST_STATE_PLAYING);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void AudioFileReader::plugDeinterleave(GstPad* pad)
</span><span class="lines">@@ -270,22 +262,20 @@
</span><span class="cx">     m_deInterleave = gst_element_factory_make(&quot;deinterleave&quot;, &quot;deinterleave&quot;);
</span><span class="cx"> 
</span><span class="cx">     g_object_set(m_deInterleave.get(), &quot;keep-positions&quot;, TRUE, NULL);
</span><del>-    g_signal_connect(m_deInterleave.get(), &quot;pad-added&quot;, G_CALLBACK(onGStreamerDeinterleavePadAddedCallback), this);
-    g_signal_connect(m_deInterleave.get(), &quot;no-more-pads&quot;, G_CALLBACK(onGStreamerDeinterleaveReadyCallback), this);
</del><ins>+    g_signal_connect_swapped(m_deInterleave.get(), &quot;pad-added&quot;, G_CALLBACK(deinterleavePadAddedCallback), this);
+    g_signal_connect_swapped(m_deInterleave.get(), &quot;no-more-pads&quot;, G_CALLBACK(deinterleaveReadyCallback), this);
</ins><span class="cx"> 
</span><del>-    GstCaps* caps = gst_caps_new_simple(&quot;audio/x-raw&quot;,
</del><ins>+    GRefPtr&lt;GstCaps&gt; caps = adoptGRef(gst_caps_new_simple(&quot;audio/x-raw&quot;,
</ins><span class="cx">         &quot;rate&quot;, G_TYPE_INT, static_cast&lt;int&gt;(m_sampleRate),
</span><span class="cx">         &quot;channels&quot;, G_TYPE_INT, m_channels,
</span><span class="cx">         &quot;format&quot;, G_TYPE_STRING, GST_AUDIO_NE(F32),
</span><del>-        &quot;layout&quot;, G_TYPE_STRING, &quot;interleaved&quot;, nullptr);
-    g_object_set(capsFilter, &quot;caps&quot;, caps, NULL);
-    gst_caps_unref(caps);
</del><ins>+        &quot;layout&quot;, G_TYPE_STRING, &quot;interleaved&quot;, nullptr));
+    g_object_set(capsFilter, &quot;caps&quot;, caps.get(), nullptr);
</ins><span class="cx"> 
</span><del>-    gst_bin_add_many(GST_BIN(m_pipeline), audioConvert, audioResample, capsFilter, m_deInterleave.get(), NULL);
</del><ins>+    gst_bin_add_many(GST_BIN(m_pipeline.get()), audioConvert, audioResample, capsFilter, m_deInterleave.get(), nullptr);
</ins><span class="cx"> 
</span><del>-    GstPad* sinkPad = gst_element_get_static_pad(audioConvert, &quot;sink&quot;);
-    gst_pad_link_full(pad, sinkPad, GST_PAD_LINK_CHECK_NOTHING);
-    gst_object_unref(GST_OBJECT(sinkPad));
</del><ins>+    GRefPtr&lt;GstPad&gt; sinkPad = adoptGRef(gst_element_get_static_pad(audioConvert, &quot;sink&quot;));
+    gst_pad_link_full(pad, sinkPad.get(), GST_PAD_LINK_CHECK_NOTHING);
</ins><span class="cx"> 
</span><span class="cx">     gst_element_link_pads_full(audioConvert, &quot;src&quot;, audioResample, &quot;sink&quot;, GST_PAD_LINK_CHECK_NOTHING);
</span><span class="cx">     gst_element_link_pads_full(audioResample, &quot;src&quot;, capsFilter, &quot;sink&quot;, GST_PAD_LINK_CHECK_NOTHING);
</span><span class="lines">@@ -299,14 +289,29 @@
</span><span class="cx"> 
</span><span class="cx"> void AudioFileReader::decodeAudioForBusCreation()
</span><span class="cx"> {
</span><ins>+    ASSERT(&amp;m_runLoop == &amp;RunLoop::current());
+
</ins><span class="cx">     // Build the pipeline (giostreamsrc | filesrc) ! decodebin2
</span><span class="cx">     // A deinterleave element is added once a src pad becomes available in decodebin.
</span><del>-    m_pipeline = gst_pipeline_new(0);
</del><ins>+    m_pipeline = gst_pipeline_new(nullptr);
</ins><span class="cx"> 
</span><del>-    GRefPtr&lt;GstBus&gt; bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline)));
</del><ins>+    GRefPtr&lt;GstBus&gt; bus = adoptGRef(gst_pipeline_get_bus(GST_PIPELINE(m_pipeline.get())));
</ins><span class="cx">     ASSERT(bus);
</span><del>-    gst_bus_add_signal_watch(bus.get());
-    g_signal_connect(bus.get(), &quot;message&quot;, G_CALLBACK(messageCallback), this);
</del><ins>+    gst_bus_set_sync_handler(bus.get(), [](GstBus*, GstMessage* message, gpointer userData) {
+        auto&amp; reader = *static_cast&lt;AudioFileReader*&gt;(userData);
+        if (&amp;reader.m_runLoop == &amp;RunLoop::current())
+            reader.handleMessage(message);
+        else {
+            GRefPtr&lt;GstMessage&gt; protectMessage(message);
+            auto weakThis = reader.createWeakPtr();
+            reader.m_runLoop.dispatch([weakThis, protectMessage] {
+                if (weakThis)
+                    weakThis-&gt;handleMessage(protectMessage.get());
+            });
+        }
+        gst_message_unref(message);
+        return GST_BUS_DROP;
+    }, this, nullptr);
</ins><span class="cx"> 
</span><span class="cx">     GstElement* source;
</span><span class="cx">     if (m_data) {
</span><span class="lines">@@ -320,17 +325,16 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     m_decodebin = gst_element_factory_make(&quot;decodebin&quot;, &quot;decodebin&quot;);
</span><del>-    g_signal_connect(m_decodebin.get(), &quot;pad-added&quot;, G_CALLBACK(onGStreamerDecodebinPadAddedCallback), this);
</del><ins>+    g_signal_connect_swapped(m_decodebin.get(), &quot;pad-added&quot;, G_CALLBACK(decodebinPadAddedCallback), this);
</ins><span class="cx"> 
</span><del>-    gst_bin_add_many(GST_BIN(m_pipeline), source, m_decodebin.get(), NULL);
</del><ins>+    gst_bin_add_many(GST_BIN(m_pipeline.get()), source, m_decodebin.get(), NULL);
</ins><span class="cx">     gst_element_link_pads_full(source, &quot;src&quot;, m_decodebin.get(), &quot;sink&quot;, GST_PAD_LINK_CHECK_NOTHING);
</span><span class="cx"> 
</span><del>-    // Catch errors here immediately, there might not be an error message if
-    // we're unlucky.
-    if (gst_element_set_state(m_pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
</del><ins>+    // Catch errors here immediately, there might not be an error message if we're unlucky.
+    if (gst_element_set_state(m_pipeline.get(), GST_STATE_PAUSED) == GST_STATE_CHANGE_FAILURE) {
</ins><span class="cx">         g_warning(&quot;Error: Failed to set pipeline to PAUSED&quot;);
</span><span class="cx">         m_errorOccurred = true;
</span><del>-        g_main_loop_quit(m_loop.get());
</del><ins>+        m_runLoop.stop();
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -339,45 +343,48 @@
</span><span class="cx">     m_sampleRate = sampleRate;
</span><span class="cx">     m_channels = mixToMono ? 1 : 2;
</span><span class="cx"> 
</span><del>-    m_frontLeftBuffers = gst_buffer_list_new();
-    m_frontRightBuffers = gst_buffer_list_new();
</del><ins>+    m_frontLeftBuffers = adoptGRef(gst_buffer_list_new());
+    m_frontRightBuffers = adoptGRef(gst_buffer_list_new());
</ins><span class="cx"> 
</span><del>-    GRefPtr&lt;GMainContext&gt; context = adoptGRef(g_main_context_new());
-    g_main_context_push_thread_default(context.get());
-    m_loop = adoptGRef(g_main_loop_new(context.get(), FALSE));
-
</del><span class="cx">     // Start the pipeline processing just after the loop is started.
</span><del>-    GThreadSafeMainLoopSource source;
-    source.schedule(&quot;[WebKit] AudioFileReader::decodeAudioForBusCreation&quot;, [this] { decodeAudioForBusCreation(); }, G_PRIORITY_DEFAULT, nullptr, context.get());
</del><ins>+    m_runLoop.dispatch([this] { decodeAudioForBusCreation(); });
+    m_runLoop.run();
</ins><span class="cx"> 
</span><del>-    g_main_loop_run(m_loop.get());
-    g_main_context_pop_thread_default(context.get());
-
</del><span class="cx">     // Set pipeline to GST_STATE_NULL state here already ASAP to
</span><span class="cx">     // release any resources that might still be used.
</span><del>-    gst_element_set_state(m_pipeline, GST_STATE_NULL);
</del><ins>+    gst_element_set_state(m_pipeline.get(), GST_STATE_NULL);
</ins><span class="cx"> 
</span><span class="cx">     if (m_errorOccurred)
</span><del>-        return 0;
</del><ins>+        return nullptr;
</ins><span class="cx"> 
</span><span class="cx">     RefPtr&lt;AudioBus&gt; audioBus = AudioBus::create(m_channels, m_channelSize, true);
</span><span class="cx">     audioBus-&gt;setSampleRate(m_sampleRate);
</span><span class="cx"> 
</span><del>-    copyGstreamerBuffersToAudioChannel(m_frontLeftBuffers, audioBus-&gt;channel(0));
</del><ins>+    copyGstreamerBuffersToAudioChannel(m_frontLeftBuffers.get(), audioBus-&gt;channel(0));
</ins><span class="cx">     if (!mixToMono)
</span><del>-        copyGstreamerBuffersToAudioChannel(m_frontRightBuffers, audioBus-&gt;channel(1));
</del><ins>+        copyGstreamerBuffersToAudioChannel(m_frontRightBuffers.get(), audioBus-&gt;channel(1));
</ins><span class="cx"> 
</span><span class="cx">     return audioBus;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> PassRefPtr&lt;AudioBus&gt; createBusFromAudioFile(const char* filePath, bool mixToMono, float sampleRate)
</span><span class="cx"> {
</span><del>-    return AudioFileReader(filePath).createBus(sampleRate, mixToMono);
</del><ins>+    RefPtr&lt;AudioBus&gt; returnValue;
+    auto threadID = createThread(&quot;AudioFileReader&quot;, [&amp;returnValue, filePath, mixToMono, sampleRate] {
+        returnValue = AudioFileReader(filePath).createBus(sampleRate, mixToMono);
+    });
+    waitForThreadCompletion(threadID);
+    return returnValue;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> PassRefPtr&lt;AudioBus&gt; createBusFromInMemoryAudioFile(const void* data, size_t dataSize, bool mixToMono, float sampleRate)
</span><span class="cx"> {
</span><del>-    return AudioFileReader(data, dataSize).createBus(sampleRate, mixToMono);
</del><ins>+    RefPtr&lt;AudioBus&gt; returnValue;
+    auto threadID = createThread(&quot;AudioFileReader&quot;, [&amp;returnValue, data, dataSize, mixToMono, sampleRate] {
+        returnValue = AudioFileReader(data, dataSize).createBus(sampleRate, mixToMono);
+    });
+    waitForThreadCompletion(threadID);
+    return returnValue;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // WebCore
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsgstreamerGRefPtrGStreamercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.cpp (192510 => 192511)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.cpp        2015-11-17 08:37:21 UTC (rev 192510)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.cpp        2015-11-17 09:57:42 UTC (rev 192511)
</span><span class="lines">@@ -183,6 +183,25 @@
</span><span class="cx">         gst_buffer_unref(ptr);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+template&lt;&gt; GRefPtr&lt;GstBufferList&gt; adoptGRef(GstBufferList* ptr)
+{
+    return GRefPtr&lt;GstBufferList&gt;(ptr, GRefPtrAdopt);
+}
+
+template&lt;&gt; GstBufferList* refGPtr&lt;GstBufferList&gt;(GstBufferList* ptr)
+{
+    if (ptr)
+        gst_buffer_list_ref(ptr);
+
+    return ptr;
+}
+
+template&lt;&gt; void derefGPtr&lt;GstBufferList&gt;(GstBufferList* ptr)
+{
+    if (ptr)
+        gst_buffer_list_unref(ptr);
+}
+
</ins><span class="cx"> template&lt;&gt; GRefPtr&lt;GstSample&gt; adoptGRef(GstSample* ptr)
</span><span class="cx"> {
</span><span class="cx">     return GRefPtr&lt;GstSample&gt;(ptr, GRefPtrAdopt);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsgstreamerGRefPtrGStreamerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.h (192510 => 192511)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.h        2015-11-17 08:37:21 UTC (rev 192510)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GRefPtrGStreamer.h        2015-11-17 09:57:42 UTC (rev 192511)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> typedef struct _GstBus GstBus;
</span><span class="cx"> typedef struct _GstElementFactory GstElementFactory;
</span><span class="cx"> typedef struct _GstBuffer GstBuffer;
</span><ins>+typedef struct _GstBufferList GstBufferList;
</ins><span class="cx"> typedef struct _GstSample GstSample;
</span><span class="cx"> typedef struct _GstTagList GstTagList;
</span><span class="cx"> typedef struct _GstEvent GstEvent;
</span><span class="lines">@@ -73,6 +74,10 @@
</span><span class="cx"> template&lt;&gt; GstBuffer* refGPtr&lt;GstBuffer&gt;(GstBuffer* ptr);
</span><span class="cx"> template&lt;&gt; void derefGPtr&lt;GstBuffer&gt;(GstBuffer* ptr);
</span><span class="cx"> 
</span><ins>+template&lt;&gt; GRefPtr&lt;GstBufferList&gt; adoptGRef(GstBufferList*);
+template&lt;&gt; GstBufferList* refGPtr&lt;GstBufferList&gt;(GstBufferList*);
+template&lt;&gt; void derefGPtr&lt;GstBufferList&gt;(GstBufferList*);
+
</ins><span class="cx"> template&lt;&gt; GRefPtr&lt;GstSample&gt; adoptGRef(GstSample* ptr);
</span><span class="cx"> template&lt;&gt; GstSample* refGPtr&lt;GstSample&gt;(GstSample* ptr);
</span><span class="cx"> template&lt;&gt; void derefGPtr&lt;GstSample&gt;(GstSample* ptr);
</span></span></pre>
</div>
</div>

</body>
</html>