[gstreamer] Make sure gstreamer source element is thread-safe
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Aug 2013 15:00:07 +0000 (15:00 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Aug 2013 15:00:07 +0000 (15:00 +0000)
https://bugs.webkit.org/show_bug.cgi?id=115352

Patch by Andre Moreira Magalhaes <andre.magalhaes@collabora.co.uk> on 2013-08-27
Reviewed by Philippe Normand.

Source/WebCore:

GStreamer source element may be created by any gstreamer element on any thread by calling
gst_element_make_from_uri with the URIs handled by the source element.
This patch makes sure the gstreamer source element is thread-safe to avoid issues with it
being created outside the main thread.

* platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp:
(webkit_web_src_init):
(webKitWebSrcDispose):
(webKitWebSrcFinalize):
(webKitWebSrcSetProperty):
(webKitWebSrcGetProperty):
(removeTimeoutSources):
(webKitWebSrcStop):
(webKitWebSrcStart):
(webKitWebSrcChangeState):
(webKitWebSrcQueryWithParent):
(webKitWebSrcGetUri):
(webKitWebSrcSetUri):
(webKitWebSrcNeedDataMainCb):
(webKitWebSrcNeedDataCb):
(webKitWebSrcEnoughDataMainCb):
(webKitWebSrcEnoughDataCb):
(webKitWebSrcSeekMainCb):
(webKitWebSrcSeekDataCb):
(webKitWebSrcSetMediaPlayer):
(StreamingClient::StreamingClient):
(StreamingClient::~StreamingClient):
(StreamingClient::createReadBuffer):
(StreamingClient::handleResponseReceived):
(StreamingClient::handleDataReceived):
(StreamingClient::handleNotifyFinished):
(CachedResourceStreamingClient::CachedResourceStreamingClient):
(CachedResourceStreamingClient::~CachedResourceStreamingClient):
(CachedResourceStreamingClient::loadFailed):
(CachedResourceStreamingClient::setDefersLoading):
(CachedResourceStreamingClient::getOrCreateReadBuffer):
(CachedResourceStreamingClient::responseReceived):
(CachedResourceStreamingClient::dataReceived):
(CachedResourceStreamingClient::notifyFinished):
(ResourceHandleStreamingClient::ResourceHandleStreamingClient):
(ResourceHandleStreamingClient::~ResourceHandleStreamingClient):
(ResourceHandleStreamingClient::loadFailed):
(ResourceHandleStreamingClient::setDefersLoading):
(ResourceHandleStreamingClient::getOrCreateReadBuffer):
(ResourceHandleStreamingClient::willSendRequest):
(ResourceHandleStreamingClient::didReceiveResponse):
(ResourceHandleStreamingClient::didReceiveData):
(ResourceHandleStreamingClient::didFinishLoading):
(ResourceHandleStreamingClient::didFail):
(ResourceHandleStreamingClient::wasBlocked):
(ResourceHandleStreamingClient::cannotShowURL):
Make element thread-safe, add support to use the element without a player associated (e.g.
the DASH plugin using the webkitsrc to download fragments), use GMutexLocker to simplify
locks and other general improvements.

Source/WTF:

Add convenience class that simplifies locking and unlocking a GMutex.

* GNUmakefile.list.am:
* wtf/gobject/GMutexLocker.h: Added.
(WebCore::GMutexLocker::GMutexLocker):
(WebCore::GMutexLocker::~GMutexLocker):
(WebCore::GMutexLocker::lock):
(WebCore::GMutexLocker::unlock):
(WebCore::GMutexLocker::mutex):

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

Source/WTF/ChangeLog
Source/WTF/GNUmakefile.list.am
Source/WTF/wtf/gobject/GMutexLocker.h [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp

index 84217ecea6cbac00f926e05fbe5cf8f5fa35162e..e2b74bd9326f544ed54a734b2d2fea3607384a5c 100644 (file)
@@ -1,3 +1,20 @@
+2013-08-27  Andre Moreira Magalhaes   <andre.magalhaes@collabora.co.uk>
+
+        [gstreamer] Make sure gstreamer source element is thread-safe
+        https://bugs.webkit.org/show_bug.cgi?id=115352
+
+        Reviewed by Philippe Normand.
+
+        Add convenience class that simplifies locking and unlocking a GMutex.
+
+        * GNUmakefile.list.am:
+        * wtf/gobject/GMutexLocker.h: Added.
+        (WebCore::GMutexLocker::GMutexLocker):
+        (WebCore::GMutexLocker::~GMutexLocker):
+        (WebCore::GMutexLocker::lock):
+        (WebCore::GMutexLocker::unlock):
+        (WebCore::GMutexLocker::mutex):
+
 2013-08-26  Andy Estes  <aestes@apple.com>
 
         Don't leak objects in HardAutorelease when OBJC_NO_GC is undefined but
index da147659ff8f21fccc3502beff474acfbf7a2831..b3c9988bf4d26955cf0c7d8e424cd54b3d1bcaa3 100644 (file)
@@ -213,6 +213,7 @@ wtf_sources += \
     Source/WTF/wtf/dtoa/strtod.cc \
     Source/WTF/wtf/dtoa/strtod.h \
     Source/WTF/wtf/dtoa/utils.h \
+    Source/WTF/wtf/gobject/GMutexLocker.h \
     Source/WTF/wtf/gobject/GOwnPtr.cpp \
     Source/WTF/wtf/gobject/GOwnPtr.h \
     Source/WTF/wtf/gobject/GRefPtr.cpp \
diff --git a/Source/WTF/wtf/gobject/GMutexLocker.h b/Source/WTF/wtf/gobject/GMutexLocker.h
new file mode 100644 (file)
index 0000000..9800dd9
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ *  Copyright (C) 2013 Collabora Ltd.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Library General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Library General Public License
+ *  along with this library; see the file COPYING.LIB.  If not, write to
+ *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ *  Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GMutexLocker_h
+#define GMutexLocker_h
+
+#if USE(GLIB)
+
+#include <glib.h>
+
+#include <wtf/FastAllocBase.h>
+#include <wtf/Noncopyable.h>
+
+namespace WebCore {
+
+class GMutexLocker {
+    WTF_MAKE_NONCOPYABLE(GMutexLocker); WTF_MAKE_FAST_ALLOCATED;
+
+public:
+    inline explicit GMutexLocker(GMutex* mutex)
+        : m_mutex(mutex)
+        , m_val(0)
+    {
+        lock();
+    }
+
+    inline ~GMutexLocker() { unlock(); }
+
+    inline void lock()
+    {
+        if (m_mutex && !m_val) {
+            g_mutex_lock(m_mutex);
+            m_val = 1;
+        }
+    }
+
+    inline void unlock()
+    {
+        if (m_mutex && m_val) {
+            m_val = 0;
+            g_mutex_unlock(m_mutex);
+        }
+    }
+
+    inline GMutex* mutex() const { return m_mutex; }
+
+private:
+    GMutex* m_mutex;
+    uint8_t m_val;
+};
+
+}
+
+#endif // USE(GLIB)
+
+#endif // GMutexLocker_h
index c87426351aacdceee2d70653cd57a65905f5b700..f08d9e374283866b072bc069224b6bc5d2a68c71 100644 (file)
@@ -1,3 +1,65 @@
+2013-08-27  Andre Moreira Magalhaes   <andre.magalhaes@collabora.co.uk>
+
+        [gstreamer] Make sure gstreamer source element is thread-safe
+        https://bugs.webkit.org/show_bug.cgi?id=115352
+
+        Reviewed by Philippe Normand.
+
+        GStreamer source element may be created by any gstreamer element on any thread by calling
+        gst_element_make_from_uri with the URIs handled by the source element.
+        This patch makes sure the gstreamer source element is thread-safe to avoid issues with it
+        being created outside the main thread.
+
+        * platform/graphics/gstreamer/WebKitWebSourceGStreamer.cpp:
+        (webkit_web_src_init):
+        (webKitWebSrcDispose):
+        (webKitWebSrcFinalize):
+        (webKitWebSrcSetProperty):
+        (webKitWebSrcGetProperty):
+        (removeTimeoutSources):
+        (webKitWebSrcStop):
+        (webKitWebSrcStart):
+        (webKitWebSrcChangeState):
+        (webKitWebSrcQueryWithParent):
+        (webKitWebSrcGetUri):
+        (webKitWebSrcSetUri):
+        (webKitWebSrcNeedDataMainCb):
+        (webKitWebSrcNeedDataCb):
+        (webKitWebSrcEnoughDataMainCb):
+        (webKitWebSrcEnoughDataCb):
+        (webKitWebSrcSeekMainCb):
+        (webKitWebSrcSeekDataCb):
+        (webKitWebSrcSetMediaPlayer):
+        (StreamingClient::StreamingClient):
+        (StreamingClient::~StreamingClient):
+        (StreamingClient::createReadBuffer):
+        (StreamingClient::handleResponseReceived):
+        (StreamingClient::handleDataReceived):
+        (StreamingClient::handleNotifyFinished):
+        (CachedResourceStreamingClient::CachedResourceStreamingClient):
+        (CachedResourceStreamingClient::~CachedResourceStreamingClient):
+        (CachedResourceStreamingClient::loadFailed):
+        (CachedResourceStreamingClient::setDefersLoading):
+        (CachedResourceStreamingClient::getOrCreateReadBuffer):
+        (CachedResourceStreamingClient::responseReceived):
+        (CachedResourceStreamingClient::dataReceived):
+        (CachedResourceStreamingClient::notifyFinished):
+        (ResourceHandleStreamingClient::ResourceHandleStreamingClient):
+        (ResourceHandleStreamingClient::~ResourceHandleStreamingClient):
+        (ResourceHandleStreamingClient::loadFailed):
+        (ResourceHandleStreamingClient::setDefersLoading):
+        (ResourceHandleStreamingClient::getOrCreateReadBuffer):
+        (ResourceHandleStreamingClient::willSendRequest):
+        (ResourceHandleStreamingClient::didReceiveResponse):
+        (ResourceHandleStreamingClient::didReceiveData):
+        (ResourceHandleStreamingClient::didFinishLoading):
+        (ResourceHandleStreamingClient::didFail):
+        (ResourceHandleStreamingClient::wasBlocked):
+        (ResourceHandleStreamingClient::cannotShowURL):
+        Make element thread-safe, add support to use the element without a player associated (e.g.
+        the DASH plugin using the webkitsrc to download fragments), use GMutexLocker to simplify
+        locks and other general improvements.
+
 2013-08-27  Antti Koivisto  <antti@apple.com>
 
         Remove branch from DescendantIteratorAdapter::begin() when invoking for ContainerNode
index 0cc575eb6c989b17a2ffd57a1188c1c010f04024..f8c79045f31e852ac2d87e2ab9c0823f1a68fc82 100644 (file)
 #include "GStreamerVersioning.h"
 #include "MediaPlayer.h"
 #include "NotImplemented.h"
+#include "ResourceHandle.h"
+#include "ResourceHandleClient.h"
 #include "ResourceRequest.h"
 #include "ResourceResponse.h"
 #include <gst/app/gstappsrc.h>
 #include <gst/gst.h>
 #include <gst/pbutils/missing-plugins.h>
 #include <wtf/Noncopyable.h>
+#include <wtf/gobject/GMutexLocker.h>
 #include <wtf/gobject/GOwnPtr.h>
 #include <wtf/gobject/GRefPtr.h>
 #include <wtf/text/CString.h>
 
 using namespace WebCore;
 
-class StreamingClient : public CachedRawResourceClient {
-    WTF_MAKE_NONCOPYABLE(StreamingClient); WTF_MAKE_FAST_ALLOCATED;
+class StreamingClient {
     public:
         StreamingClient(WebKitWebSrc*);
         virtual ~StreamingClient();
 
+        virtual bool loadFailed() const = 0;
+        virtual void setDefersLoading(bool) = 0;
+
+    protected:
+        char* createReadBuffer(size_t requestedSize, size_t& actualSize);
+        void handleResponseReceived(const ResourceResponse&);
+        void handleDataReceived(const char*, int);
+        void handleNotifyFinished();
+
+        GRefPtr<GstElement> m_src;
+};
+
+class CachedResourceStreamingClient : public CachedRawResourceClient, public StreamingClient {
+    WTF_MAKE_NONCOPYABLE(CachedResourceStreamingClient); WTF_MAKE_FAST_ALLOCATED;
+    public:
+        CachedResourceStreamingClient(WebKitWebSrc*, CachedResourceLoader*, const ResourceRequest&);
+        virtual ~CachedResourceStreamingClient();
+
+        // StreamingClient virtual methods.
+        virtual bool loadFailed() const;
+        virtual void setDefersLoading(bool);
+
     private:
-        // CachedResourceClient
+        // CachedResourceClient virtual methods.
+        virtual char* getOrCreateReadBuffer(CachedResource*, size_t requestedSize, size_t& actualSize);
         virtual void responseReceived(CachedResource*, const ResourceResponse&);
         virtual void dataReceived(CachedResource*, const char*, int);
         virtual void notifyFinished(CachedResource*);
-        virtual char* getOrCreateReadBuffer(CachedResource*, size_t requestedSize, size_t& actualSize);
 
-        WebKitWebSrc* m_src;
+        CachedResourceHandle<CachedRawResource> m_resource;
+};
+
+class ResourceHandleStreamingClient : public ResourceHandleClient, public StreamingClient {
+    WTF_MAKE_NONCOPYABLE(ResourceHandleStreamingClient); WTF_MAKE_FAST_ALLOCATED;
+    public:
+        ResourceHandleStreamingClient(WebKitWebSrc*, const ResourceRequest&);
+        virtual ~ResourceHandleStreamingClient();
+
+        // StreamingClient virtual methods.
+        virtual bool loadFailed() const;
+        virtual void setDefersLoading(bool);
+
+    private:
+        // ResourceHandleClient virtual methods.
+        virtual char* getOrCreateReadBuffer(size_t requestedSize, size_t& actualSize);
+        virtual void willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse&);
+        virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
+        virtual void didReceiveData(ResourceHandle*, const char*, int, int);
+        virtual void didFinishLoading(ResourceHandle*, double /*finishTime*/);
+        virtual void didFail(ResourceHandle*, const ResourceError&);
+        virtual void wasBlocked(ResourceHandle*);
+        virtual void cannotShowURL(ResourceHandle*);
+
+        RefPtr<ResourceHandle> m_resource;
 };
 
 #define WEBKIT_WEB_SRC_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_SRC, WebKitWebSrcPrivate))
@@ -68,7 +116,6 @@ struct _WebKitWebSrcPrivate {
     WebCore::MediaPlayer* player;
 
     StreamingClient* client;
-    CachedResourceHandle<CachedRawResource> resource;
 
     guint64 offset;
     guint64 size;
@@ -77,6 +124,8 @@ struct _WebKitWebSrcPrivate {
 
     guint64 requestedOffset;
 
+    guint startID;
+    guint stopID;
     guint needDataID;
     guint enoughDataID;
     guint seekID;
@@ -129,8 +178,6 @@ static void webKitWebSrcNeedDataCb(GstAppSrc*, guint length, gpointer userData);
 static void webKitWebSrcEnoughDataCb(GstAppSrc*, gpointer userData);
 static gboolean webKitWebSrcSeekDataCb(GstAppSrc*, guint64 offset, gpointer userData);
 
-static void webKitWebSrcStop(WebKitWebSrc*, bool);
-
 static GstAppSrcCallbacks appsrcCallbacks = {
     webKitWebSrcNeedDataCb,
     webKitWebSrcEnoughDataCb,
@@ -184,7 +231,7 @@ static void webkit_web_src_class_init(WebKitWebSrcClass* klass)
                                                         "Genre of the stream",
                                                         0,
                                                         (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
-                                                        
+
     g_object_class_install_property(oklass,
                                     PROP_IRADIO_URL,
                                     g_param_spec_string("iradio-url",
@@ -222,8 +269,6 @@ static void webkit_web_src_init(WebKitWebSrc* src)
 
     src->priv = priv;
 
-    priv->client = new StreamingClient(src);
-
     priv->appsrc = GST_APP_SRC(gst_element_factory_make("appsrc", 0));
     if (!priv->appsrc) {
         GST_ERROR_OBJECT(src, "Failed to create appsrc");
@@ -272,7 +317,8 @@ static void webkit_web_src_init(WebKitWebSrc* src)
     if (priv->haveAppSrc27)
         g_object_set(priv->appsrc, "min-percent", 20, NULL);
 
-    webKitWebSrcStop(src, false);
+    gst_app_src_set_caps(priv->appsrc, 0);
+    gst_app_src_set_size(priv->appsrc, -1);
 }
 
 static void webKitWebSrcDispose(GObject* object)
@@ -280,13 +326,6 @@ static void webKitWebSrcDispose(GObject* object)
     WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
     WebKitWebSrcPrivate* priv = src->priv;
 
-    if (priv->buffer) {
-#ifdef GST_API_VERSION_1
-        unmapGstBuffer(priv->buffer.get());
-#endif
-        priv->buffer.clear();
-    }
-
     priv->player = 0;
 
     GST_CALL_PARENT(G_OBJECT_CLASS, dispose, (object));
@@ -297,8 +336,6 @@ static void webKitWebSrcFinalize(GObject* object)
     WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
     WebKitWebSrcPrivate* priv = src->priv;
 
-    delete priv->client;
-
     g_free(priv->uri);
 
     GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
@@ -310,9 +347,11 @@ static void webKitWebSrcSetProperty(GObject* object, guint propID, const GValue*
     WebKitWebSrcPrivate* priv = src->priv;
 
     switch (propID) {
-    case PROP_IRADIO_MODE:
+    case PROP_IRADIO_MODE: {
+        GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
         priv->iradioMode = g_value_get_boolean(value);
         break;
+    }
     case PROP_LOCATION:
 #ifdef GST_API_VERSION_1
         gst_uri_handler_set_uri(reinterpret_cast<GstURIHandler*>(src), g_value_get_string(value), 0);
@@ -331,6 +370,7 @@ static void webKitWebSrcGetProperty(GObject* object, guint propID, GValue* value
     WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
     WebKitWebSrcPrivate* priv = src->priv;
 
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
     switch (propID) {
     case PROP_IRADIO_MODE:
         g_value_set_boolean(value, priv->iradioMode);
@@ -356,15 +396,39 @@ static void webKitWebSrcGetProperty(GObject* object, guint propID, GValue* value
     }
 }
 
+static void removeTimeoutSources(WebKitWebSrc* src)
+{
+    WebKitWebSrcPrivate* priv = src->priv;
+
+    if (priv->startID)
+        g_source_remove(priv->startID);
+    priv->startID = 0;
 
-static void webKitWebSrcStop(WebKitWebSrc* src, bool seeking)
+    if (priv->needDataID)
+        g_source_remove(priv->needDataID);
+    priv->needDataID = 0;
+
+    if (priv->enoughDataID)
+        g_source_remove(priv->enoughDataID);
+    priv->enoughDataID = 0;
+
+    if (priv->seekID)
+        g_source_remove(priv->seekID);
+    priv->seekID = 0;
+}
+
+static gboolean webKitWebSrcStop(WebKitWebSrc* src)
 {
     WebKitWebSrcPrivate* priv = src->priv;
 
-    if (priv->resource) {
-        priv->resource->removeClient(priv->client);
-        priv->resource = 0;
-    }
+    ASSERT(isMainThread());
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+
+    bool seeking = priv->seekID;
+
+    removeTimeoutSources(src);
+    priv->stopID = 0;
 
     if (priv->client) {
         delete priv->client;
@@ -378,21 +442,7 @@ static void webKitWebSrcStop(WebKitWebSrc* src, bool seeking)
         priv->buffer.clear();
     }
 
-    GST_OBJECT_LOCK(src);
-    if (priv->needDataID)
-        g_source_remove(priv->needDataID);
-    priv->needDataID = 0;
-
-    if (priv->enoughDataID)
-        g_source_remove(priv->enoughDataID);
-    priv->enoughDataID = 0;
-
-    if (priv->seekID)
-        g_source_remove(priv->seekID);
-    priv->seekID = 0;
-
     priv->paused = FALSE;
-    GST_OBJECT_UNLOCK(src);
 
     g_free(priv->iradioName);
     priv->iradioName = 0;
@@ -406,39 +456,54 @@ static void webKitWebSrcStop(WebKitWebSrc* src, bool seeking)
     g_free(priv->iradioTitle);
     priv->iradioTitle = 0;
 
-    if (priv->appsrc) {
-        gst_app_src_set_caps(priv->appsrc, 0);
-        if (!seeking)
-            gst_app_src_set_size(priv->appsrc, -1);
-    }
-
     priv->offset = 0;
     priv->seekable = FALSE;
 
     if (!seeking) {
         priv->size = 0;
         priv->requestedOffset = 0;
+        priv->player = 0;
+    }
+
+    locker.unlock();
+
+    if (priv->appsrc) {
+        gst_app_src_set_caps(priv->appsrc, 0);
+        if (!seeking)
+            gst_app_src_set_size(priv->appsrc, -1);
     }
 
     GST_DEBUG_OBJECT(src, "Stopped request");
+
+    return FALSE;
 }
 
-static bool webKitWebSrcStart(WebKitWebSrc* src)
+static gboolean webKitWebSrcStart(WebKitWebSrc* src)
 {
     WebKitWebSrcPrivate* priv = src->priv;
 
+    ASSERT(isMainThread());
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+
+    priv->startID = 0;
+
     if (!priv->uri) {
         GST_ERROR_OBJECT(src, "No URI provided");
-        return false;
+        locker.unlock();
+        webKitWebSrcStop(src);
+        return FALSE;
     }
 
-    ASSERT(priv->player);
+    ASSERT(!priv->client);
 
     KURL url = KURL(KURL(), priv->uri);
 
     ResourceRequest request(url);
     request.setAllowCookies(true);
-    request.setHTTPReferrer(priv->player->referrer());
+
+    if (priv->player)
+        request.setHTTPReferrer(priv->player->referrer());
 
 #if USE(SOUP)
     // Let's disable HTTP Accept-Encoding here as we don't want the received response to be
@@ -469,23 +534,26 @@ static bool webKitWebSrcStart(WebKitWebSrc* src)
     // Needed to use DLNA streaming servers
     request.setHTTPHeaderField("transferMode.dlna", "Streaming");
 
-    if (CachedResourceLoader* loader = priv->player->cachedResourceLoader()) {
-        CachedResourceRequest cacheRequest(request, ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, DoNotBufferData, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType));
-        priv->resource = loader->requestRawResource(cacheRequest);
-        if (priv->resource) {
-            priv->client = new StreamingClient(src);
-            priv->resource->addClient(priv->client);
-        }
+    if (priv->player) {
+        if (CachedResourceLoader* loader = priv->player->cachedResourceLoader())
+            priv->client = new CachedResourceStreamingClient(src, loader, request);
     }
 
-    if (!priv->resource) {
-        GST_ERROR_OBJECT(src, "Failed to schedule resource load");
-        return false;
-    }
+    if (!priv->client)
+        priv->client = new ResourceHandleStreamingClient(src, request);
 
+    if (!priv->client || priv->client->loadFailed()) {
+        GST_ERROR_OBJECT(src, "Failed to setup streaming client");
+        if (priv->client) {
+            delete priv->client;
+            priv->client = 0;
+        }
+        locker.unlock();
+        webKitWebSrcStop(src);
+        return FALSE;
+    }
     GST_DEBUG_OBJECT(src, "Started request");
-
-    return true;
+    return FALSE;
 }
 
 static GstStateChangeReturn webKitWebSrcChangeState(GstElement* element, GstStateChange transition)
@@ -513,15 +581,17 @@ static GstStateChangeReturn webKitWebSrcChangeState(GstElement* element, GstStat
         return ret;
     }
 
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
     switch (transition) {
     case GST_STATE_CHANGE_READY_TO_PAUSED:
         GST_DEBUG_OBJECT(src, "READY->PAUSED");
-        if (!webKitWebSrcStart(src))
-            ret = GST_STATE_CHANGE_FAILURE;
+        priv->startID = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) webKitWebSrcStart, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
         break;
     case GST_STATE_CHANGE_PAUSED_TO_READY:
         GST_DEBUG_OBJECT(src, "PAUSED->READY");
-        webKitWebSrcStop(src, false);
+        // cancel pending sources
+        removeTimeoutSources(src);
+        priv->stopID = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) webKitWebSrcStop, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
         break;
     default:
         break;
@@ -532,7 +602,7 @@ static GstStateChangeReturn webKitWebSrcChangeState(GstElement* element, GstStat
 
 static gboolean webKitWebSrcQueryWithParent(GstPad* pad, GstObject* parent, GstQuery* query)
 {
-    WebKitWebSrc* webkitSrc = WEBKIT_WEB_SRC(GST_ELEMENT(parent));
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(GST_ELEMENT(parent));
     gboolean result = FALSE;
 
     switch (GST_QUERY_TYPE(query)) {
@@ -541,15 +611,17 @@ static gboolean webKitWebSrcQueryWithParent(GstPad* pad, GstObject* parent, GstQ
 
         gst_query_parse_duration(query, &format, NULL);
 
-        GST_DEBUG_OBJECT(webkitSrc, "duration query in format %s", gst_format_get_name(format));
-        if ((format == GST_FORMAT_BYTES) && (webkitSrc->priv->size > 0)) {
-            gst_query_set_duration(query, format, webkitSrc->priv->size);
+        GST_DEBUG_OBJECT(src, "duration query in format %s", gst_format_get_name(format));
+        GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+        if (format == GST_FORMAT_BYTES && src->priv->size > 0) {
+            gst_query_set_duration(query, format, src->priv->size);
             result = TRUE;
         }
         break;
     }
     case GST_QUERY_URI: {
-        gst_query_set_uri(query, webkitSrc->priv->uri);
+        GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+        gst_query_set_uri(query, src->priv->uri);
         result = TRUE;
         break;
     }
@@ -590,7 +662,12 @@ const gchar* const* webKitWebSrcGetProtocols(GType)
 
 static gchar* webKitWebSrcGetUri(GstURIHandler* handler)
 {
-    return g_strdup(WEBKIT_WEB_SRC(handler)->priv->uri);
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(handler);
+    gchar* ret;
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    ret = g_strdup(src->priv->uri);
+    return ret;
 }
 
 static gboolean webKitWebSrcSetUri(GstURIHandler* handler, const gchar* uri, GError** error)
@@ -603,6 +680,8 @@ static gboolean webKitWebSrcSetUri(GstURIHandler* handler, const gchar* uri, GEr
         return FALSE;
     }
 
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+
     g_free(priv->uri);
     priv->uri = 0;
 
@@ -634,7 +713,12 @@ static gchar** webKitWebSrcGetProtocols(void)
 
 static const gchar* webKitWebSrcGetUri(GstURIHandler* handler)
 {
-    return g_strdup(WEBKIT_WEB_SRC(handler)->priv->uri);
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(handler);
+    gchar* ret;
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    ret = g_strdup(src->priv->uri);
+    return ret;
 }
 
 static gboolean webKitWebSrcSetUri(GstURIHandler* handler, const gchar* uri)
@@ -647,6 +731,8 @@ static gboolean webKitWebSrcSetUri(GstURIHandler* handler, const gchar* uri)
         return FALSE;
     }
 
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+
     g_free(priv->uri);
     priv->uri = 0;
 
@@ -681,12 +767,19 @@ static gboolean webKitWebSrcNeedDataMainCb(WebKitWebSrc* src)
 {
     WebKitWebSrcPrivate* priv = src->priv;
 
-    priv->resource->setDefersLoading(false);
+    ASSERT(isMainThread());
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    // already stopped
+    if (!priv->needDataID)
+        return FALSE;
 
-    GST_OBJECT_LOCK(src);
     priv->paused = FALSE;
     priv->needDataID = 0;
-    GST_OBJECT_UNLOCK(src);
+    locker.unlock();
+
+    if (priv->client)
+        priv->client->setDefersLoading(false);
     return FALSE;
 }
 
@@ -697,27 +790,31 @@ static void webKitWebSrcNeedDataCb(GstAppSrc*, guint length, gpointer userData)
 
     GST_DEBUG_OBJECT(src, "Need more data: %u", length);
 
-    GST_OBJECT_LOCK(src);
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
     if (priv->needDataID || !priv->paused) {
-        GST_OBJECT_UNLOCK(src);
         return;
     }
 
-    priv->needDataID = g_timeout_add_full(G_PRIORITY_DEFAULT, 0, (GSourceFunc) webKitWebSrcNeedDataMainCb, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
-    GST_OBJECT_UNLOCK(src);
+    priv->needDataID = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) webKitWebSrcNeedDataMainCb, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
 }
 
 static gboolean webKitWebSrcEnoughDataMainCb(WebKitWebSrc* src)
 {
     WebKitWebSrcPrivate* priv = src->priv;
 
-    priv->resource->setDefersLoading(true);
+    ASSERT(isMainThread());
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    // already stopped
+    if (!priv->enoughDataID)
+        return FALSE;
 
-    GST_OBJECT_LOCK(src);
     priv->paused = TRUE;
     priv->enoughDataID = 0;
-    GST_OBJECT_UNLOCK(src);
+    locker.unlock();
 
+    if (priv->client)
+        priv->client->setDefersLoading(true);
     return FALSE;
 }
 
@@ -728,19 +825,27 @@ static void webKitWebSrcEnoughDataCb(GstAppSrc*, gpointer userData)
 
     GST_DEBUG_OBJECT(src, "Have enough data");
 
-    GST_OBJECT_LOCK(src);
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
     if (priv->enoughDataID || priv->paused) {
-        GST_OBJECT_UNLOCK(src);
         return;
     }
 
-    priv->enoughDataID = g_timeout_add_full(G_PRIORITY_DEFAULT, 0, (GSourceFunc) webKitWebSrcEnoughDataMainCb, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
-    GST_OBJECT_UNLOCK(src);
+    priv->enoughDataID = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) webKitWebSrcEnoughDataMainCb, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
 }
 
 static gboolean webKitWebSrcSeekMainCb(WebKitWebSrc* src)
 {
-    webKitWebSrcStop(src, true);
+    WebKitWebSrcPrivate* priv = src->priv;
+
+    ASSERT(isMainThread());
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    // already stopped
+    if (!priv->seekID)
+        return FALSE;
+    locker.unlock();
+
+    webKitWebSrcStop(src);
     webKitWebSrcStart(src);
 
     return FALSE;
@@ -752,6 +857,7 @@ static gboolean webKitWebSrcSeekDataCb(GstAppSrc*, guint64 offset, gpointer user
     WebKitWebSrcPrivate* priv = src->priv;
 
     GST_DEBUG_OBJECT(src, "Seeking to offset: %" G_GUINT64_FORMAT, offset);
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
     if (offset == priv->offset && priv->requestedOffset == priv->offset)
         return TRUE;
 
@@ -761,110 +867,140 @@ static gboolean webKitWebSrcSeekDataCb(GstAppSrc*, guint64 offset, gpointer user
     GST_DEBUG_OBJECT(src, "Doing range-request seek");
     priv->requestedOffset = offset;
 
-    GST_OBJECT_LOCK(src);
     if (priv->seekID)
         g_source_remove(priv->seekID);
-    priv->seekID = g_timeout_add_full(G_PRIORITY_DEFAULT, 0, (GSourceFunc) webKitWebSrcSeekMainCb, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
-    GST_OBJECT_UNLOCK(src);
-    
+    priv->seekID = g_idle_add_full(G_PRIORITY_DEFAULT, (GSourceFunc) webKitWebSrcSeekMainCb, gst_object_ref(src), (GDestroyNotify) gst_object_unref);
     return TRUE;
 }
 
 void webKitWebSrcSetMediaPlayer(WebKitWebSrc* src, WebCore::MediaPlayer* player)
 {
     ASSERT(player);
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
     src->priv->player = player;
 }
 
-StreamingClient::StreamingClient(WebKitWebSrc* src) : m_src(src)
+StreamingClient::StreamingClient(WebKitWebSrc* src)
+    : m_src(adoptGRef(static_cast<GstElement*>(gst_object_ref(src))))
 {
-
 }
 
 StreamingClient::~StreamingClient()
 {
+}
+
+char* StreamingClient::createReadBuffer(size_t requestedSize, size_t& actualSize)
+{
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+    WebKitWebSrcPrivate* priv = src->priv;
+
+    ASSERT(!priv->buffer);
+
+    GstBuffer* buffer = gst_buffer_new_and_alloc(requestedSize);
 
+#ifdef GST_API_VERSION_1
+    mapGstBuffer(buffer);
+#endif
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    priv->buffer = adoptGRef(buffer);
+    locker.unlock();
+
+    actualSize = getGstBufferSize(buffer);
+    return getGstBufferDataPointer(buffer);
 }
 
-void StreamingClient::responseReceived(CachedResource* resource, const ResourceResponse& response)
+void StreamingClient::handleResponseReceived(const ResourceResponse& response)
 {
-    WebKitWebSrcPrivate* priv = m_src->priv;
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+    WebKitWebSrcPrivate* priv = src->priv;
+
+    GST_DEBUG_OBJECT(src, "Received response: %d", response.httpStatusCode());
 
-    GST_DEBUG_OBJECT(m_src, "Received response: %d", response.httpStatusCode());
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
 
     // If we seeked we need 206 == PARTIAL_CONTENT
     if (priv->requestedOffset && response.httpStatusCode() != 206) {
-        GST_ELEMENT_ERROR(m_src, RESOURCE, READ, (0), (0));
+        locker.unlock();
+        GST_ELEMENT_ERROR(src, RESOURCE, READ, (0), (0));
         gst_app_src_end_of_stream(priv->appsrc);
-        webKitWebSrcStop(m_src, false);
+        webKitWebSrcStop(src);
         return;
     }
 
     long long length = response.expectedContentLength();
-    if (length > 0) {
+    if (length > 0)
         length += priv->requestedOffset;
-        gst_app_src_set_size(priv->appsrc, length);
-
-#ifndef GST_API_VERSION_1
-        if (!priv->haveAppSrc27) {
-            gst_segment_set_duration(&GST_BASE_SRC(priv->appsrc)->segment, GST_FORMAT_BYTES, length);
-            gst_element_post_message(GST_ELEMENT(priv->appsrc),
-                                     gst_message_new_duration(GST_OBJECT(priv->appsrc),
-                                                              GST_FORMAT_BYTES, length));
-        }
-#endif
-    }
 
     priv->size = length >= 0 ? length : 0;
     priv->seekable = length > 0 && g_ascii_strcasecmp("none", response.httpHeaderField("Accept-Ranges").utf8().data());
 
-    // icecast stuff
-    String value = response.httpHeaderField("icy-metaint");
-    if (!value.isEmpty()) {
-        gchar* endptr = 0;
-        gint64 icyMetaInt = g_ascii_strtoll(value.utf8().data(), &endptr, 10);
-            
-        if (endptr && *endptr == '\0' && icyMetaInt > 0) {
-            GRefPtr<GstCaps> caps = adoptGRef(gst_caps_new_simple("application/x-icy", "metadata-interval", G_TYPE_INT, (gint) icyMetaInt, NULL));
-
-            gst_app_src_set_caps(priv->appsrc, caps.get());
-        }
-    }
-
 #ifdef GST_API_VERSION_1
     GstTagList* tags = gst_tag_list_new_empty();
 #else
     GstTagList* tags = gst_tag_list_new();
 #endif
-    value = response.httpHeaderField("icy-name");
+    String value = response.httpHeaderField("icy-name");
     if (!value.isEmpty()) {
         g_free(priv->iradioName);
         priv->iradioName = g_strdup(value.utf8().data());
-        g_object_notify(G_OBJECT(m_src), "iradio-name");
+        g_object_notify(G_OBJECT(src), "iradio-name");
         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION, priv->iradioName, NULL);
     }
     value = response.httpHeaderField("icy-genre");
     if (!value.isEmpty()) {
         g_free(priv->iradioGenre);
         priv->iradioGenre = g_strdup(value.utf8().data());
-        g_object_notify(G_OBJECT(m_src), "iradio-genre");
+        g_object_notify(G_OBJECT(src), "iradio-genre");
         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE, priv->iradioGenre, NULL);
     }
     value = response.httpHeaderField("icy-url");
     if (!value.isEmpty()) {
         g_free(priv->iradioUrl);
         priv->iradioUrl = g_strdup(value.utf8().data());
-        g_object_notify(G_OBJECT(m_src), "iradio-url");
+        g_object_notify(G_OBJECT(src), "iradio-url");
         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION, priv->iradioUrl, NULL);
     }
     value = response.httpHeaderField("icy-title");
     if (!value.isEmpty()) {
         g_free(priv->iradioTitle);
         priv->iradioTitle = g_strdup(value.utf8().data());
-        g_object_notify(G_OBJECT(m_src), "iradio-title");
+        g_object_notify(G_OBJECT(src), "iradio-title");
         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, priv->iradioTitle, NULL);
     }
 
+    locker.unlock();
+
+    // notify size/duration
+    if (length > 0) {
+        gst_app_src_set_size(priv->appsrc, length);
+
+#ifndef GST_API_VERSION_1
+        if (!priv->haveAppSrc27) {
+            gst_segment_set_duration(&GST_BASE_SRC(priv->appsrc)->segment, GST_FORMAT_BYTES, length);
+            gst_element_post_message(GST_ELEMENT(priv->appsrc),
+                gst_message_new_duration(GST_OBJECT(priv->appsrc),
+                    GST_FORMAT_BYTES, length));
+        }
+#endif
+    } else
+        gst_app_src_set_size(priv->appsrc, -1);
+
+    // icecast stuff
+    value = response.httpHeaderField("icy-metaint");
+    if (!value.isEmpty()) {
+        gchar* endptr = 0;
+        gint64 icyMetaInt = g_ascii_strtoll(value.utf8().data(), &endptr, 10);
+
+        if (endptr && *endptr == '\0' && icyMetaInt > 0) {
+            GRefPtr<GstCaps> caps = adoptGRef(gst_caps_new_simple("application/x-icy", "metadata-interval", G_TYPE_INT, (gint) icyMetaInt, NULL));
+
+            gst_app_src_set_caps(priv->appsrc, caps.get());
+        }
+    } else
+        gst_app_src_set_caps(priv->appsrc, 0);
+
+    // notify tags
     if (gst_tag_list_is_empty(tags))
 #ifdef GST_API_VERSION_1
         gst_tag_list_unref(tags);
@@ -872,14 +1008,17 @@ void StreamingClient::responseReceived(CachedResource* resource, const ResourceR
         gst_tag_list_free(tags);
 #endif
     else
-        notifyGstTagsOnPad(GST_ELEMENT(m_src), m_src->priv->srcpad, tags);
+        notifyGstTagsOnPad(GST_ELEMENT(src), priv->srcpad, tags);
 }
 
-void StreamingClient::dataReceived(CachedResource* resource, const char* data, int length)
+void StreamingClient::handleDataReceived(const char* data, int length)
 {
-    WebKitWebSrcPrivate* priv = m_src->priv;
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+    WebKitWebSrcPrivate* priv = src->priv;
 
-    GST_LOG_OBJECT(m_src, "Have %d bytes of data", priv->buffer ? getGstBufferSize(priv->buffer.get()) : length);
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+
+    GST_LOG_OBJECT(src, "Have %d bytes of data", priv->buffer ? getGstBufferSize(priv->buffer.get()) : length);
 
     ASSERT(!priv->buffer || data == getGstBufferDataPointer(priv->buffer.get()));
 
@@ -888,8 +1027,8 @@ void StreamingClient::dataReceived(CachedResource* resource, const char* data, i
         unmapGstBuffer(priv->buffer.get());
 #endif
 
-    if (priv->seekID || resource != priv->resource) {
-        GST_DEBUG_OBJECT(m_src, "Seek in progress, ignoring data");
+    if (priv->seekID) {
+        GST_DEBUG_OBJECT(src, "Seek in progress, ignoring data");
         priv->buffer.clear();
         return;
     }
@@ -907,57 +1046,182 @@ void StreamingClient::dataReceived(CachedResource* resource, const char* data, i
     priv->offset += length;
     // priv->size == 0 if received length on didReceiveResponse < 0.
     if (priv->size > 0 && priv->offset > priv->size) {
-        GST_DEBUG_OBJECT(m_src, "Updating internal size from %" G_GUINT64_FORMAT " to %" G_GUINT64_FORMAT, priv->size, priv->offset);
+        GST_DEBUG_OBJECT(src, "Updating internal size from %" G_GUINT64_FORMAT " to %" G_GUINT64_FORMAT, priv->size, priv->offset);
         gst_app_src_set_size(priv->appsrc, priv->offset);
         priv->size = priv->offset;
     }
     GST_BUFFER_OFFSET_END(priv->buffer.get()) = priv->offset;
 
+    locker.unlock();
+
     GstFlowReturn ret = gst_app_src_push_buffer(priv->appsrc, priv->buffer.leakRef());
 #ifdef GST_API_VERSION_1
     if (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)
 #else
     if (ret != GST_FLOW_OK && ret != GST_FLOW_UNEXPECTED)
 #endif
-        GST_ELEMENT_ERROR(m_src, CORE, FAILED, (0), (0));
+        GST_ELEMENT_ERROR(src, CORE, FAILED, (0), (0));
 }
 
-char* StreamingClient::getOrCreateReadBuffer(CachedResource*, size_t requestedSize, size_t& actualSize)
+void StreamingClient::handleNotifyFinished()
 {
-    WebKitWebSrcPrivate* priv = m_src->priv;
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+    WebKitWebSrcPrivate* priv = src->priv;
 
-    ASSERT(!priv->buffer);
+    GST_DEBUG_OBJECT(src, "Have EOS");
 
-    GstBuffer* buffer = gst_buffer_new_and_alloc(requestedSize);
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    if (!priv->seekID) {
+        locker.unlock();
+        gst_app_src_end_of_stream(priv->appsrc);
+    }
+}
 
-#ifdef GST_API_VERSION_1
-    mapGstBuffer(buffer);
-#endif
+CachedResourceStreamingClient::CachedResourceStreamingClient(WebKitWebSrc* src, CachedResourceLoader* resourceLoader, const ResourceRequest& request)
+    : StreamingClient(src)
+{
+    CachedResourceRequest cacheRequest(request, ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, DoNotBufferData, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType));
+    m_resource = resourceLoader->requestRawResource(cacheRequest);
+    if (m_resource)
+        m_resource->addClient(this);
+}
 
-    priv->buffer = adoptGRef(buffer);
+CachedResourceStreamingClient::~CachedResourceStreamingClient()
+{
+    if (m_resource) {
+        m_resource->removeClient(this);
+        m_resource = 0;
+    }
+}
 
-    actualSize = getGstBufferSize(buffer);
-    return getGstBufferDataPointer(buffer);
+bool CachedResourceStreamingClient::loadFailed() const
+{
+    return !m_resource;
+}
+
+void CachedResourceStreamingClient::setDefersLoading(bool defers)
+{
+    if (m_resource)
+        m_resource->setDefersLoading(defers);
 }
 
-void StreamingClient::notifyFinished(CachedResource* resource)
+char* CachedResourceStreamingClient::getOrCreateReadBuffer(CachedResource*, size_t requestedSize, size_t& actualSize)
 {
-    WebKitWebSrcPrivate* priv = m_src->priv;
+    return createReadBuffer(requestedSize, actualSize);
+}
+
+void CachedResourceStreamingClient::responseReceived(CachedResource*, const ResourceResponse& response)
+{
+    handleResponseReceived(response);
+}
 
+void CachedResourceStreamingClient::dataReceived(CachedResource*, const char* data, int length)
+{
+    handleDataReceived(data, length);
+}
+
+void CachedResourceStreamingClient::notifyFinished(CachedResource* resource)
+{
     if (resource->loadFailedOrCanceled()) {
+        WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+
         if (!resource->wasCanceled()) {
             const ResourceError& error = resource->resourceError();
-            GST_ERROR_OBJECT(m_src, "Have failure: %s", error.localizedDescription().utf8().data());
-            GST_ELEMENT_ERROR(m_src, RESOURCE, FAILED, ("%s", error.localizedDescription().utf8().data()), (0));
+            GST_ERROR_OBJECT(src, "Have failure: %s", error.localizedDescription().utf8().data());
+            GST_ELEMENT_ERROR(src, RESOURCE, FAILED, ("%s", error.localizedDescription().utf8().data()), (0));
         }
-        gst_app_src_end_of_stream(m_src->priv->appsrc);
+        gst_app_src_end_of_stream(src->priv->appsrc);
         return;
     }
 
-    GST_DEBUG_OBJECT(m_src, "Have EOS");
+    handleNotifyFinished();
+}
 
-    if (!priv->seekID)
-        gst_app_src_end_of_stream(m_src->priv->appsrc);
+ResourceHandleStreamingClient::ResourceHandleStreamingClient(WebKitWebSrc* src, const ResourceRequest& request)
+    : StreamingClient(src)
+{
+    m_resource = ResourceHandle::create(0 /*context*/, request, this, false, false);
+}
+
+ResourceHandleStreamingClient::~ResourceHandleStreamingClient()
+{
+    if (m_resource) {
+        m_resource->cancel();
+        m_resource.release();
+        m_resource = 0;
+    }
+}
+
+bool ResourceHandleStreamingClient::loadFailed() const
+{
+    return !m_resource;
+}
+
+void ResourceHandleStreamingClient::setDefersLoading(bool defers)
+{
+    if (m_resource)
+        m_resource->setDefersLoading(defers);
+}
+
+char* ResourceHandleStreamingClient::getOrCreateReadBuffer(size_t requestedSize, size_t& actualSize)
+{
+    return createReadBuffer(requestedSize, actualSize);
+}
+
+void ResourceHandleStreamingClient::willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse&)
+{
+}
+
+void ResourceHandleStreamingClient::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
+{
+    handleResponseReceived(response);
+}
+
+void ResourceHandleStreamingClient::didReceiveData(ResourceHandle*, const char* data, int length, int)
+{
+    handleDataReceived(data, length);
+}
+
+void ResourceHandleStreamingClient::didFinishLoading(ResourceHandle*, double)
+{
+    handleNotifyFinished();
+}
+
+void ResourceHandleStreamingClient::didFail(ResourceHandle*, const ResourceError& error)
+{
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+
+    GST_ERROR_OBJECT(src, "Have failure: %s", error.localizedDescription().utf8().data());
+    GST_ELEMENT_ERROR(src, RESOURCE, FAILED, ("%s", error.localizedDescription().utf8().data()), (0));
+    gst_app_src_end_of_stream(src->priv->appsrc);
+}
+
+void ResourceHandleStreamingClient::wasBlocked(ResourceHandle*)
+{
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+    GOwnPtr<gchar> uri;
+
+    GST_ERROR_OBJECT(src, "Request was blocked");
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    uri.set(g_strdup(src->priv->uri));
+    locker.unlock();
+
+    GST_ELEMENT_ERROR(src, RESOURCE, OPEN_READ, ("Access to \"%s\" was blocked", uri.get()), (0));
+}
+
+void ResourceHandleStreamingClient::cannotShowURL(ResourceHandle*)
+{
+    WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
+    GOwnPtr<gchar> uri;
+
+    GST_ERROR_OBJECT(src, "Cannot show URL");
+
+    GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
+    uri.set(g_strdup(src->priv->uri));
+    locker.unlock();
+
+    GST_ELEMENT_ERROR(src, RESOURCE, OPEN_READ, ("Can't show \"%s\"", uri.get()), (0));
 }
 
 #endif // USE(GSTREAMER)