[GStreamer] No CORS support for media elements
[WebKit-https.git] / Source / WebCore / platform / graphics / gstreamer / WebKitWebSourceGStreamer.cpp
1 /*
2  *  Copyright (C) 2009, 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *  Copyright (C) 2013 Collabora Ltd.
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "config.h"
21 #include "WebKitWebSourceGStreamer.h"
22
23 #if ENABLE(VIDEO) && USE(GSTREAMER)
24
25 #include "CachedRawResource.h"
26 #include "CachedRawResourceClient.h"
27 #include "CachedResourceHandle.h"
28 #include "CachedResourceLoader.h"
29 #include "CachedResourceRequest.h"
30 #include "CrossOriginAccessControl.h"
31 #include "GRefPtrGStreamer.h"
32 #include "GStreamerUtilities.h"
33 #include "MediaPlayer.h"
34 #include "NotImplemented.h"
35 #include "ResourceHandle.h"
36 #include "ResourceHandleClient.h"
37 #include "ResourceRequest.h"
38 #include "ResourceResponse.h"
39 #include "SecurityOrigin.h"
40 #include "SharedBuffer.h"
41 #include <gst/app/gstappsrc.h>
42 #include <gst/gst.h>
43 #include <gst/pbutils/missing-plugins.h>
44 #include <wtf/Noncopyable.h>
45 #include <wtf/gobject/GMainLoopSource.h>
46 #include <wtf/gobject/GMutexLocker.h>
47 #include <wtf/gobject/GRefPtr.h>
48 #include <wtf/gobject/GUniquePtr.h>
49 #include <wtf/text/CString.h>
50
51 using namespace WebCore;
52
53 enum CORSAccessCheckResult {
54     CORSNoCheck,
55     CORSSuccess,
56     CORSFailure
57 };
58
59 class StreamingClient {
60     public:
61         StreamingClient(WebKitWebSrc*);
62         virtual ~StreamingClient();
63
64         virtual bool loadFailed() const = 0;
65         virtual void setDefersLoading(bool) = 0;
66
67     protected:
68         char* createReadBuffer(size_t requestedSize, size_t& actualSize);
69         void handleResponseReceived(const ResourceResponse&, CORSAccessCheckResult);
70         void handleDataReceived(const char*, int);
71         void handleNotifyFinished();
72
73         GRefPtr<GstElement> m_src;
74 };
75
76 class CachedResourceStreamingClient : public CachedRawResourceClient, public StreamingClient {
77     WTF_MAKE_NONCOPYABLE(CachedResourceStreamingClient); WTF_MAKE_FAST_ALLOCATED;
78     public:
79         CachedResourceStreamingClient(WebKitWebSrc*, CachedResourceLoader*, const ResourceRequest&, MediaPlayerClient::CORSMode);
80         virtual ~CachedResourceStreamingClient();
81
82         // StreamingClient virtual methods.
83         virtual bool loadFailed() const;
84         virtual void setDefersLoading(bool);
85
86     private:
87         // CachedResourceClient virtual methods.
88         virtual char* getOrCreateReadBuffer(CachedResource*, size_t requestedSize, size_t& actualSize);
89         virtual void responseReceived(CachedResource*, const ResourceResponse&);
90         virtual void dataReceived(CachedResource*, const char*, int);
91         virtual void notifyFinished(CachedResource*);
92
93         CachedResourceHandle<CachedRawResource> m_resource;
94         RefPtr<SecurityOrigin> m_origin;
95 };
96
97 class ResourceHandleStreamingClient : public ResourceHandleClient, public StreamingClient {
98     WTF_MAKE_NONCOPYABLE(ResourceHandleStreamingClient); WTF_MAKE_FAST_ALLOCATED;
99     public:
100         ResourceHandleStreamingClient(WebKitWebSrc*, const ResourceRequest&);
101         virtual ~ResourceHandleStreamingClient();
102
103         // StreamingClient virtual methods.
104         virtual bool loadFailed() const;
105         virtual void setDefersLoading(bool);
106
107     private:
108         // ResourceHandleClient virtual methods.
109         virtual char* getOrCreateReadBuffer(size_t requestedSize, size_t& actualSize);
110         virtual void willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse&);
111         virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
112         virtual void didReceiveData(ResourceHandle*, const char*, unsigned, int);
113         virtual void didReceiveBuffer(ResourceHandle*, PassRefPtr<SharedBuffer>, int encodedLength);
114         virtual void didFinishLoading(ResourceHandle*, double /*finishTime*/);
115         virtual void didFail(ResourceHandle*, const ResourceError&);
116         virtual void wasBlocked(ResourceHandle*);
117         virtual void cannotShowURL(ResourceHandle*);
118
119         RefPtr<ResourceHandle> m_resource;
120 };
121
122 #define WEBKIT_WEB_SRC_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_SRC, WebKitWebSrcPrivate))
123 struct _WebKitWebSrcPrivate {
124     GstAppSrc* appsrc;
125     GstPad* srcpad;
126     gchar* uri;
127
128     WebCore::MediaPlayer* player;
129
130     StreamingClient* client;
131
132     CORSAccessCheckResult corsAccessCheck;
133
134     guint64 offset;
135     guint64 size;
136     gboolean seekable;
137     gboolean paused;
138
139     guint64 requestedOffset;
140
141     GMainLoopSource startSource;
142     GMainLoopSource stopSource;
143     GMainLoopSource needDataSource;
144     GMainLoopSource enoughDataSource;
145     GMainLoopSource seekSource;
146
147     GRefPtr<GstBuffer> buffer;
148
149     // icecast stuff
150     gchar* iradioName;
151     gchar* iradioGenre;
152     gchar* iradioUrl;
153     gchar* iradioTitle;
154 };
155
156 enum {
157     PROP_IRADIO_NAME = 1,
158     PROP_IRADIO_GENRE,
159     PROP_IRADIO_URL,
160     PROP_IRADIO_TITLE,
161     PROP_LOCATION
162 };
163
164 static GstStaticPadTemplate srcTemplate = GST_STATIC_PAD_TEMPLATE("src",
165                                                                   GST_PAD_SRC,
166                                                                   GST_PAD_ALWAYS,
167                                                                   GST_STATIC_CAPS_ANY);
168
169 GST_DEBUG_CATEGORY_STATIC(webkit_web_src_debug);
170 #define GST_CAT_DEFAULT webkit_web_src_debug
171
172 static void webKitWebSrcUriHandlerInit(gpointer gIface, gpointer ifaceData);
173
174 static void webKitWebSrcDispose(GObject*);
175 static void webKitWebSrcFinalize(GObject*);
176 static void webKitWebSrcSetProperty(GObject*, guint propertyID, const GValue*, GParamSpec*);
177 static void webKitWebSrcGetProperty(GObject*, guint propertyID, GValue*, GParamSpec*);
178 static GstStateChangeReturn webKitWebSrcChangeState(GstElement*, GstStateChange);
179
180 static gboolean webKitWebSrcQueryWithParent(GstPad*, GstObject*, GstQuery*);
181
182 static void webKitWebSrcNeedDataCb(GstAppSrc*, guint length, gpointer userData);
183 static void webKitWebSrcEnoughDataCb(GstAppSrc*, gpointer userData);
184 static gboolean webKitWebSrcSeekDataCb(GstAppSrc*, guint64 offset, gpointer userData);
185
186 static GstAppSrcCallbacks appsrcCallbacks = {
187     webKitWebSrcNeedDataCb,
188     webKitWebSrcEnoughDataCb,
189     webKitWebSrcSeekDataCb,
190     { 0 }
191 };
192
193 #define webkit_web_src_parent_class parent_class
194 // We split this out into another macro to avoid a check-webkit-style error.
195 #define WEBKIT_WEB_SRC_CATEGORY_INIT GST_DEBUG_CATEGORY_INIT(webkit_web_src_debug, "webkitwebsrc", 0, "websrc element");
196 G_DEFINE_TYPE_WITH_CODE(WebKitWebSrc, webkit_web_src, GST_TYPE_BIN,
197                          G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, webKitWebSrcUriHandlerInit);
198                          WEBKIT_WEB_SRC_CATEGORY_INIT);
199
200 static void webkit_web_src_class_init(WebKitWebSrcClass* klass)
201 {
202     GObjectClass* oklass = G_OBJECT_CLASS(klass);
203     GstElementClass* eklass = GST_ELEMENT_CLASS(klass);
204
205     oklass->dispose = webKitWebSrcDispose;
206     oklass->finalize = webKitWebSrcFinalize;
207     oklass->set_property = webKitWebSrcSetProperty;
208     oklass->get_property = webKitWebSrcGetProperty;
209
210     gst_element_class_add_pad_template(eklass,
211                                        gst_static_pad_template_get(&srcTemplate));
212     gst_element_class_set_metadata(eklass, "WebKit Web source element", "Source", "Handles HTTP/HTTPS uris",
213                                "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
214
215     // icecast stuff
216     g_object_class_install_property(oklass,
217                                     PROP_IRADIO_NAME,
218                                     g_param_spec_string("iradio-name",
219                                                         "iradio-name",
220                                                         "Name of the stream",
221                                                         0,
222                                                         (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
223
224     g_object_class_install_property(oklass,
225                                     PROP_IRADIO_GENRE,
226                                     g_param_spec_string("iradio-genre",
227                                                         "iradio-genre",
228                                                         "Genre of the stream",
229                                                         0,
230                                                         (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
231
232     g_object_class_install_property(oklass,
233                                     PROP_IRADIO_URL,
234                                     g_param_spec_string("iradio-url",
235                                                         "iradio-url",
236                                                         "Homepage URL for radio stream",
237                                                         0,
238                                                         (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
239
240     g_object_class_install_property(oklass,
241                                     PROP_IRADIO_TITLE,
242                                     g_param_spec_string("iradio-title",
243                                                         "iradio-title",
244                                                         "Name of currently playing song",
245                                                         0,
246                                                         (GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
247
248
249     /* Allows setting the uri using the 'location' property, which is used
250      * for example by gst_element_make_from_uri() */
251     g_object_class_install_property(oklass,
252                                     PROP_LOCATION,
253                                     g_param_spec_string("location",
254                                                         "location",
255                                                         "Location to read from",
256                                                         0,
257                                                         (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
258     eklass->change_state = webKitWebSrcChangeState;
259
260     g_type_class_add_private(klass, sizeof(WebKitWebSrcPrivate));
261 }
262
263 static void webkit_web_src_init(WebKitWebSrc* src)
264 {
265     WebKitWebSrcPrivate* priv = WEBKIT_WEB_SRC_GET_PRIVATE(src);
266
267     src->priv = priv;
268     new (priv) WebKitWebSrcPrivate();
269
270     priv->appsrc = GST_APP_SRC(gst_element_factory_make("appsrc", 0));
271     if (!priv->appsrc) {
272         GST_ERROR_OBJECT(src, "Failed to create appsrc");
273         return;
274     }
275
276     gst_bin_add(GST_BIN(src), GST_ELEMENT(priv->appsrc));
277
278
279     GRefPtr<GstPad> targetPad = adoptGRef(gst_element_get_static_pad(GST_ELEMENT(priv->appsrc), "src"));
280     priv->srcpad = webkitGstGhostPadFromStaticTemplate(&srcTemplate, "src", targetPad.get());
281
282     gst_element_add_pad(GST_ELEMENT(src), priv->srcpad);
283
284     GST_OBJECT_FLAG_SET(priv->srcpad, GST_PAD_FLAG_NEED_PARENT);
285     gst_pad_set_query_function(priv->srcpad, webKitWebSrcQueryWithParent);
286
287     gst_app_src_set_callbacks(priv->appsrc, &appsrcCallbacks, src, 0);
288     gst_app_src_set_emit_signals(priv->appsrc, FALSE);
289     gst_app_src_set_stream_type(priv->appsrc, GST_APP_STREAM_TYPE_SEEKABLE);
290
291     // 512k is a abitrary number but we should choose a value
292     // here to not pause/unpause the SoupMessage too often and
293     // to make sure there's always some data available for
294     // GStreamer to handle.
295     gst_app_src_set_max_bytes(priv->appsrc, 512 * 1024);
296
297     // Emit the need-data signal if the queue contains less
298     // than 20% of data. Without this the need-data signal
299     // is emitted when the queue is empty, we then dispatch
300     // the soup message unpausing to the main loop and from
301     // there unpause the soup message. This already takes
302     // quite some time and libsoup even needs some more time
303     // to actually provide data again. If we do all this
304     // already if the queue is 20% empty, it's much more
305     // likely that libsoup already provides new data before
306     // the queue is really empty.
307     // This might need tweaking for ports not using libsoup.
308     g_object_set(priv->appsrc, "min-percent", 20, NULL);
309
310     gst_app_src_set_caps(priv->appsrc, 0);
311     gst_app_src_set_size(priv->appsrc, -1);
312 }
313
314 static void webKitWebSrcDispose(GObject* object)
315 {
316     WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
317     WebKitWebSrcPrivate* priv = src->priv;
318
319     priv->player = 0;
320
321     GST_CALL_PARENT(G_OBJECT_CLASS, dispose, (object));
322 }
323
324 static void webKitWebSrcFinalize(GObject* object)
325 {
326     WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
327     WebKitWebSrcPrivate* priv = src->priv;
328
329     g_free(priv->uri);
330     priv->~WebKitWebSrcPrivate();
331
332     GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
333 }
334
335 static void webKitWebSrcSetProperty(GObject* object, guint propID, const GValue* value, GParamSpec* pspec)
336 {
337     WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
338
339     switch (propID) {
340     case PROP_LOCATION:
341         gst_uri_handler_set_uri(reinterpret_cast<GstURIHandler*>(src), g_value_get_string(value), 0);
342         break;
343     default:
344         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, pspec);
345         break;
346     }
347 }
348
349 static void webKitWebSrcGetProperty(GObject* object, guint propID, GValue* value, GParamSpec* pspec)
350 {
351     WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
352     WebKitWebSrcPrivate* priv = src->priv;
353
354     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
355     switch (propID) {
356     case PROP_IRADIO_NAME:
357         g_value_set_string(value, priv->iradioName);
358         break;
359     case PROP_IRADIO_GENRE:
360         g_value_set_string(value, priv->iradioGenre);
361         break;
362     case PROP_IRADIO_URL:
363         g_value_set_string(value, priv->iradioUrl);
364         break;
365     case PROP_IRADIO_TITLE:
366         g_value_set_string(value, priv->iradioTitle);
367         break;
368     case PROP_LOCATION:
369         g_value_set_string(value, priv->uri);
370         break;
371     default:
372         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, pspec);
373         break;
374     }
375 }
376
377 static void removeTimeoutSources(WebKitWebSrc* src)
378 {
379     WebKitWebSrcPrivate* priv = src->priv;
380
381     priv->startSource.cancel();
382     priv->needDataSource.cancel();
383     priv->enoughDataSource.cancel();
384     priv->seekSource.cancel();
385 }
386
387 static void webKitWebSrcStop(WebKitWebSrc* src)
388 {
389     WebKitWebSrcPrivate* priv = src->priv;
390
391     ASSERT(isMainThread());
392
393     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
394
395     bool seeking = priv->seekSource.isActive();
396
397     removeTimeoutSources(src);
398
399     if (priv->client) {
400         delete priv->client;
401         priv->client = 0;
402     }
403
404     if (priv->buffer) {
405         unmapGstBuffer(priv->buffer.get());
406         priv->buffer.clear();
407     }
408
409     priv->paused = FALSE;
410
411     g_free(priv->iradioName);
412     priv->iradioName = 0;
413
414     g_free(priv->iradioGenre);
415     priv->iradioGenre = 0;
416
417     g_free(priv->iradioUrl);
418     priv->iradioUrl = 0;
419
420     g_free(priv->iradioTitle);
421     priv->iradioTitle = 0;
422
423     priv->offset = 0;
424     priv->seekable = FALSE;
425
426     if (!seeking) {
427         priv->size = 0;
428         priv->requestedOffset = 0;
429         priv->player = 0;
430     }
431
432     locker.unlock();
433
434     if (priv->appsrc) {
435         gst_app_src_set_caps(priv->appsrc, 0);
436         if (!seeking)
437             gst_app_src_set_size(priv->appsrc, -1);
438     }
439
440     GST_DEBUG_OBJECT(src, "Stopped request");
441 }
442
443 static void webKitWebSrcStart(WebKitWebSrc* src)
444 {
445     WebKitWebSrcPrivate* priv = src->priv;
446
447     ASSERT(isMainThread());
448
449     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
450
451     priv->corsAccessCheck = CORSNoCheck;
452
453     if (!priv->uri) {
454         GST_ERROR_OBJECT(src, "No URI provided");
455         locker.unlock();
456         webKitWebSrcStop(src);
457         return;
458     }
459
460     ASSERT(!priv->client);
461
462     URL url = URL(URL(), priv->uri);
463
464     ResourceRequest request(url);
465     request.setAllowCookies(true);
466
467     if (priv->player)
468         request.setHTTPReferrer(priv->player->referrer());
469
470 #if USE(SOUP)
471     // Let's disable HTTP Accept-Encoding here as we don't want the received response to be
472     // encoded in any way as we need to rely on the proper size of the returned data on
473     // didReceiveResponse.
474     // If Accept-Encoding is used, the server may send the data in encoded format and
475     // request.expectedContentLength() will have the "wrong" size (the size of the
476     // compressed data), even though the data received in didReceiveData is uncompressed.
477     request.setAcceptEncoding(false);
478 #endif
479
480     // Let Apple web servers know we want to access their nice movie trailers.
481     if (!g_ascii_strcasecmp("movies.apple.com", url.host().utf8().data())
482         || !g_ascii_strcasecmp("trailers.apple.com", url.host().utf8().data()))
483         request.setHTTPUserAgent("Quicktime/7.6.6");
484
485     if (priv->requestedOffset) {
486         GUniquePtr<gchar> val(g_strdup_printf("bytes=%" G_GUINT64_FORMAT "-", priv->requestedOffset));
487         request.setHTTPHeaderField("Range", val.get());
488     }
489     priv->offset = priv->requestedOffset;
490
491     // We always request Icecast/Shoutcast metadata, just in case ...
492     request.setHTTPHeaderField("icy-metadata", "1");
493
494     // Needed to use DLNA streaming servers
495     request.setHTTPHeaderField("transferMode.dlna", "Streaming");
496
497     if (priv->player) {
498         if (CachedResourceLoader* loader = priv->player->cachedResourceLoader())
499             priv->client = new CachedResourceStreamingClient(src, loader, request, priv->player->mediaPlayerClient()->mediaPlayerCORSMode());
500     }
501
502     if (!priv->client)
503         priv->client = new ResourceHandleStreamingClient(src, request);
504
505     if (!priv->client || priv->client->loadFailed()) {
506         GST_ERROR_OBJECT(src, "Failed to setup streaming client");
507         if (priv->client) {
508             delete priv->client;
509             priv->client = 0;
510         }
511         locker.unlock();
512         webKitWebSrcStop(src);
513         return;
514     }
515     GST_DEBUG_OBJECT(src, "Started request");
516 }
517
518 static GstStateChangeReturn webKitWebSrcChangeState(GstElement* element, GstStateChange transition)
519 {
520     GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
521     WebKitWebSrc* src = WEBKIT_WEB_SRC(element);
522     WebKitWebSrcPrivate* priv = src->priv;
523
524     switch (transition) {
525     case GST_STATE_CHANGE_NULL_TO_READY:
526         if (!priv->appsrc) {
527             gst_element_post_message(element,
528                                      gst_missing_element_message_new(element, "appsrc"));
529             GST_ELEMENT_ERROR(src, CORE, MISSING_PLUGIN, (0), ("no appsrc"));
530             return GST_STATE_CHANGE_FAILURE;
531         }
532         break;
533     default:
534         break;
535     }
536
537     ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
538     if (G_UNLIKELY(ret == GST_STATE_CHANGE_FAILURE)) {
539         GST_DEBUG_OBJECT(src, "State change failed");
540         return ret;
541     }
542
543     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
544     switch (transition) {
545     case GST_STATE_CHANGE_READY_TO_PAUSED:
546         GST_DEBUG_OBJECT(src, "READY->PAUSED");
547         gst_object_ref(src);
548         priv->startSource.schedule("[WebKit] webKitWebSrcStart", std::function<void()>(std::bind(webKitWebSrcStart, src)), G_PRIORITY_DEFAULT,
549             [src] { gst_object_unref(src); });
550         break;
551     case GST_STATE_CHANGE_PAUSED_TO_READY:
552         GST_DEBUG_OBJECT(src, "PAUSED->READY");
553         // cancel pending sources
554         removeTimeoutSources(src);
555         gst_object_ref(src);
556         priv->stopSource.schedule("[WebKit] webKitWebSrcStop", std::function<void()>(std::bind(webKitWebSrcStop, src)), G_PRIORITY_DEFAULT,
557             [src] { gst_object_unref(src); });
558         break;
559     default:
560         break;
561     }
562
563     return ret;
564 }
565
566 static gboolean webKitWebSrcQueryWithParent(GstPad* pad, GstObject* parent, GstQuery* query)
567 {
568     WebKitWebSrc* src = WEBKIT_WEB_SRC(GST_ELEMENT(parent));
569     gboolean result = FALSE;
570
571     switch (GST_QUERY_TYPE(query)) {
572     case GST_QUERY_DURATION: {
573         GstFormat format;
574
575         gst_query_parse_duration(query, &format, NULL);
576
577         GST_DEBUG_OBJECT(src, "duration query in format %s", gst_format_get_name(format));
578         GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
579         if (format == GST_FORMAT_BYTES && src->priv->size > 0) {
580             gst_query_set_duration(query, format, src->priv->size);
581             result = TRUE;
582         }
583         break;
584     }
585     case GST_QUERY_URI: {
586         GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
587         gst_query_set_uri(query, src->priv->uri);
588         result = TRUE;
589         break;
590     }
591     default: {
592         GRefPtr<GstPad> target = adoptGRef(gst_ghost_pad_get_target(GST_GHOST_PAD_CAST(pad)));
593
594         // Forward the query to the proxy target pad.
595         if (target)
596             result = gst_pad_query(target.get(), query);
597         break;
598     }
599     }
600
601     return result;
602 }
603
604 static bool urlHasSupportedProtocol(const URL& url)
605 {
606     return url.isValid() && (url.protocolIsInHTTPFamily() || url.protocolIs("blob"));
607 }
608
609 // uri handler interface
610
611 static GstURIType webKitWebSrcUriGetType(GType)
612 {
613     return GST_URI_SRC;
614 }
615
616 const gchar* const* webKitWebSrcGetProtocols(GType)
617 {
618     static const char* protocols[] = {"http", "https", "blob", 0 };
619     return protocols;
620 }
621
622 static gchar* webKitWebSrcGetUri(GstURIHandler* handler)
623 {
624     WebKitWebSrc* src = WEBKIT_WEB_SRC(handler);
625     gchar* ret;
626
627     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
628     ret = g_strdup(src->priv->uri);
629     return ret;
630 }
631
632 static gboolean webKitWebSrcSetUri(GstURIHandler* handler, const gchar* uri, GError** error)
633 {
634     WebKitWebSrc* src = WEBKIT_WEB_SRC(handler);
635     WebKitWebSrcPrivate* priv = src->priv;
636
637     if (GST_STATE(src) >= GST_STATE_PAUSED) {
638         GST_ERROR_OBJECT(src, "URI can only be set in states < PAUSED");
639         return FALSE;
640     }
641
642     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
643
644     g_free(priv->uri);
645     priv->uri = 0;
646
647     if (!uri)
648         return TRUE;
649
650     URL url(URL(), uri);
651     if (!urlHasSupportedProtocol(url)) {
652         g_set_error(error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI, "Invalid URI '%s'", uri);
653         return FALSE;
654     }
655
656     priv->uri = g_strdup(url.string().utf8().data());
657     return TRUE;
658 }
659
660 static void webKitWebSrcUriHandlerInit(gpointer gIface, gpointer)
661 {
662     GstURIHandlerInterface* iface = (GstURIHandlerInterface *) gIface;
663
664     iface->get_type = webKitWebSrcUriGetType;
665     iface->get_protocols = webKitWebSrcGetProtocols;
666     iface->get_uri = webKitWebSrcGetUri;
667     iface->set_uri = webKitWebSrcSetUri;
668 }
669
670 // appsrc callbacks
671
672 static void webKitWebSrcNeedDataMainCb(WebKitWebSrc* src)
673 {
674     WebKitWebSrcPrivate* priv = src->priv;
675
676     ASSERT(isMainThread());
677
678     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
679     priv->paused = FALSE;
680     locker.unlock();
681
682     if (priv->client)
683         priv->client->setDefersLoading(false);
684 }
685
686 static void webKitWebSrcNeedDataCb(GstAppSrc*, guint length, gpointer userData)
687 {
688     WebKitWebSrc* src = WEBKIT_WEB_SRC(userData);
689     WebKitWebSrcPrivate* priv = src->priv;
690
691     GST_DEBUG_OBJECT(src, "Need more data: %u", length);
692
693     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
694     if (priv->needDataSource.isScheduled() || !priv->paused)
695         return;
696
697     gst_object_ref(src);
698     priv->needDataSource.schedule("[WebKit] webKitWebSrcNeedDataMainCb", std::function<void()>(std::bind(webKitWebSrcNeedDataMainCb, src)), G_PRIORITY_DEFAULT,
699         [src] { gst_object_unref(src); });
700 }
701
702 static void webKitWebSrcEnoughDataMainCb(WebKitWebSrc* src)
703 {
704     WebKitWebSrcPrivate* priv = src->priv;
705
706     ASSERT(isMainThread());
707
708     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
709     priv->paused = TRUE;
710     locker.unlock();
711
712     if (priv->client)
713         priv->client->setDefersLoading(true);
714 }
715
716 static void webKitWebSrcEnoughDataCb(GstAppSrc*, gpointer userData)
717 {
718     WebKitWebSrc* src = WEBKIT_WEB_SRC(userData);
719     WebKitWebSrcPrivate* priv = src->priv;
720
721     GST_DEBUG_OBJECT(src, "Have enough data");
722
723     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
724     if (priv->enoughDataSource.isScheduled() || priv->paused)
725         return;
726
727     gst_object_ref(src);
728     priv->enoughDataSource.schedule("[WebKit] webKitWebSrcEnoughDataMainCb", std::function<void()>(std::bind(webKitWebSrcEnoughDataMainCb, src)), G_PRIORITY_DEFAULT,
729         [src] { gst_object_unref(src); });
730 }
731
732 static void webKitWebSrcSeekMainCb(WebKitWebSrc* src)
733 {
734     ASSERT(isMainThread());
735
736     webKitWebSrcStop(src);
737     webKitWebSrcStart(src);
738 }
739
740 static gboolean webKitWebSrcSeekDataCb(GstAppSrc*, guint64 offset, gpointer userData)
741 {
742     WebKitWebSrc* src = WEBKIT_WEB_SRC(userData);
743     WebKitWebSrcPrivate* priv = src->priv;
744
745     GST_DEBUG_OBJECT(src, "Seeking to offset: %" G_GUINT64_FORMAT, offset);
746     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
747     if (offset == priv->offset && priv->requestedOffset == priv->offset)
748         return TRUE;
749
750     if (!priv->seekable)
751         return FALSE;
752
753     GST_DEBUG_OBJECT(src, "Doing range-request seek");
754     priv->requestedOffset = offset;
755
756     gst_object_ref(src);
757     priv->seekSource.schedule("[WebKit] webKitWebSrcSeekMainCb", std::function<void()>(std::bind(webKitWebSrcSeekMainCb, src)), G_PRIORITY_DEFAULT,
758         [src] { gst_object_unref(src); });
759     return TRUE;
760 }
761
762 void webKitWebSrcSetMediaPlayer(WebKitWebSrc* src, WebCore::MediaPlayer* player)
763 {
764     ASSERT(player);
765     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
766     src->priv->player = player;
767 }
768
769 bool webKitSrcPassedCORSAccessCheck(WebKitWebSrc* src)
770 {
771     return src->priv->corsAccessCheck == CORSSuccess;
772 }
773
774 StreamingClient::StreamingClient(WebKitWebSrc* src)
775     : m_src(adoptGRef(static_cast<GstElement*>(gst_object_ref(src))))
776 {
777 }
778
779 StreamingClient::~StreamingClient()
780 {
781 }
782
783 char* StreamingClient::createReadBuffer(size_t requestedSize, size_t& actualSize)
784 {
785     WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
786     WebKitWebSrcPrivate* priv = src->priv;
787
788     ASSERT(!priv->buffer);
789
790     GstBuffer* buffer = gst_buffer_new_and_alloc(requestedSize);
791
792     mapGstBuffer(buffer);
793
794     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
795     priv->buffer = adoptGRef(buffer);
796     locker.unlock();
797
798     actualSize = gst_buffer_get_size(buffer);
799     return getGstBufferDataPointer(buffer);
800 }
801
802 void StreamingClient::handleResponseReceived(const ResourceResponse& response, CORSAccessCheckResult corsAccessCheck)
803 {
804     WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
805     WebKitWebSrcPrivate* priv = src->priv;
806
807     GST_DEBUG_OBJECT(src, "Received response: %d", response.httpStatusCode());
808
809     if (response.httpStatusCode() >= 400 || corsAccessCheck == CORSFailure) {
810         // Received error code or CORS check failed
811         if (corsAccessCheck == CORSFailure)
812             GST_ELEMENT_ERROR(src, RESOURCE, READ, ("Cross-origin stream load denied by Cross-Origin Resource Sharing policy."), (nullptr));
813         else
814             GST_ELEMENT_ERROR(src, RESOURCE, READ, ("Received %d HTTP error code", response.httpStatusCode()), (nullptr));
815         gst_app_src_end_of_stream(priv->appsrc);
816         webKitWebSrcStop(src);
817         return;
818     }
819
820     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
821
822     priv->corsAccessCheck = corsAccessCheck;
823
824     if (priv->seekSource.isActive()) {
825         GST_DEBUG_OBJECT(src, "Seek in progress, ignoring response");
826         return;
827     }
828
829     if (priv->requestedOffset) {
830         // Seeking ... we expect a 206 == PARTIAL_CONTENT
831         if (response.httpStatusCode() == 200) {
832             // Range request didn't have a ranged response; resetting offset.
833             priv->offset = 0;
834         } else if (response.httpStatusCode() != 206) {
835             // Range request completely failed.
836             locker.unlock();
837             GST_ELEMENT_ERROR(src, RESOURCE, READ, ("Received unexpected %d HTTP status code", response.httpStatusCode()), (nullptr));
838             gst_app_src_end_of_stream(priv->appsrc);
839             webKitWebSrcStop(src);
840             return;
841         }
842     }
843
844     long long length = response.expectedContentLength();
845     if (length > 0 && priv->requestedOffset && response.httpStatusCode() == 206)
846         length += priv->requestedOffset;
847
848     priv->size = length >= 0 ? length : 0;
849     priv->seekable = length > 0 && g_ascii_strcasecmp("none", response.httpHeaderField("Accept-Ranges").utf8().data());
850
851     // Wait until we unlock to send notifications
852     g_object_freeze_notify(G_OBJECT(src));
853
854     GstTagList* tags = gst_tag_list_new_empty();
855     String value = response.httpHeaderField("icy-name");
856     if (!value.isEmpty()) {
857         g_free(priv->iradioName);
858         priv->iradioName = g_strdup(value.utf8().data());
859         g_object_notify(G_OBJECT(src), "iradio-name");
860         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION, priv->iradioName, NULL);
861     }
862     value = response.httpHeaderField("icy-genre");
863     if (!value.isEmpty()) {
864         g_free(priv->iradioGenre);
865         priv->iradioGenre = g_strdup(value.utf8().data());
866         g_object_notify(G_OBJECT(src), "iradio-genre");
867         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_GENRE, priv->iradioGenre, NULL);
868     }
869     value = response.httpHeaderField("icy-url");
870     if (!value.isEmpty()) {
871         g_free(priv->iradioUrl);
872         priv->iradioUrl = g_strdup(value.utf8().data());
873         g_object_notify(G_OBJECT(src), "iradio-url");
874         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_LOCATION, priv->iradioUrl, NULL);
875     }
876     value = response.httpHeaderField("icy-title");
877     if (!value.isEmpty()) {
878         g_free(priv->iradioTitle);
879         priv->iradioTitle = g_strdup(value.utf8().data());
880         g_object_notify(G_OBJECT(src), "iradio-title");
881         gst_tag_list_add(tags, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, priv->iradioTitle, NULL);
882     }
883
884     locker.unlock();
885     g_object_thaw_notify(G_OBJECT(src));
886
887     // notify size/duration
888     if (length > 0) {
889         gst_app_src_set_size(priv->appsrc, length);
890     } else
891         gst_app_src_set_size(priv->appsrc, -1);
892
893     // icecast stuff
894     value = response.httpHeaderField("icy-metaint");
895     if (!value.isEmpty()) {
896         gchar* endptr = 0;
897         gint64 icyMetaInt = g_ascii_strtoll(value.utf8().data(), &endptr, 10);
898
899         if (endptr && *endptr == '\0' && icyMetaInt > 0) {
900             GRefPtr<GstCaps> caps = adoptGRef(gst_caps_new_simple("application/x-icy", "metadata-interval", G_TYPE_INT, (gint) icyMetaInt, NULL));
901
902             gst_app_src_set_caps(priv->appsrc, caps.get());
903         }
904     } else
905         gst_app_src_set_caps(priv->appsrc, 0);
906
907     // notify tags
908     if (gst_tag_list_is_empty(tags))
909         gst_tag_list_unref(tags);
910     else
911         gst_pad_push_event(priv->srcpad, gst_event_new_tag(tags));
912 }
913
914 void StreamingClient::handleDataReceived(const char* data, int length)
915 {
916     WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
917     WebKitWebSrcPrivate* priv = src->priv;
918
919     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
920
921     GST_LOG_OBJECT(src, "Have %lld bytes of data", priv->buffer ? static_cast<long long>(gst_buffer_get_size(priv->buffer.get())) : length);
922
923     ASSERT(!priv->buffer || data == getGstBufferDataPointer(priv->buffer.get()));
924
925     if (priv->buffer)
926         unmapGstBuffer(priv->buffer.get());
927
928     if (priv->seekSource.isActive()) {
929         GST_DEBUG_OBJECT(src, "Seek in progress, ignoring data");
930         priv->buffer.clear();
931         return;
932     }
933
934     if (priv->offset < priv->requestedOffset) {
935         // Range request failed; seeking manually.
936         if (priv->offset + length <= priv->requestedOffset) {
937             // Discard all the buffers coming before the requested seek position.
938             priv->offset += length;
939             priv->buffer.clear();
940             return;
941         }
942
943         if (priv->offset + length > priv->requestedOffset) {
944             guint64 offset = priv->requestedOffset - priv->offset;
945             data += offset;
946             length -= offset;
947             if (priv->buffer)
948                 gst_buffer_resize(priv->buffer.get(), offset, -1);
949             priv->offset = priv->requestedOffset;
950         }
951
952         priv->requestedOffset = 0;
953     }
954
955     // Ports using the GStreamer backend but not the soup implementation of ResourceHandle
956     // won't be using buffers provided by this client, the buffer is created here in that case.
957     if (!priv->buffer)
958         priv->buffer = adoptGRef(createGstBufferForData(data, length));
959     else
960         gst_buffer_set_size(priv->buffer.get(), static_cast<gssize>(length));
961
962     GST_BUFFER_OFFSET(priv->buffer.get()) = priv->offset;
963     if (priv->requestedOffset == priv->offset)
964         priv->requestedOffset += length;
965     priv->offset += length;
966     // priv->size == 0 if received length on didReceiveResponse < 0.
967     if (priv->size > 0 && priv->offset > priv->size) {
968         GST_DEBUG_OBJECT(src, "Updating internal size from %" G_GUINT64_FORMAT " to %" G_GUINT64_FORMAT, priv->size, priv->offset);
969         gst_app_src_set_size(priv->appsrc, priv->offset);
970         priv->size = priv->offset;
971     }
972     GST_BUFFER_OFFSET_END(priv->buffer.get()) = priv->offset;
973
974     locker.unlock();
975
976     GstFlowReturn ret = gst_app_src_push_buffer(priv->appsrc, priv->buffer.leakRef());
977     if (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)
978         GST_ELEMENT_ERROR(src, CORE, FAILED, (0), (0));
979 }
980
981 void StreamingClient::handleNotifyFinished()
982 {
983     WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
984     WebKitWebSrcPrivate* priv = src->priv;
985
986     GST_DEBUG_OBJECT(src, "Have EOS");
987
988     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
989     if (!priv->seekSource.isActive()) {
990         locker.unlock();
991         gst_app_src_end_of_stream(priv->appsrc);
992     }
993 }
994
995 CachedResourceStreamingClient::CachedResourceStreamingClient(WebKitWebSrc* src, CachedResourceLoader* resourceLoader, const ResourceRequest& request, MediaPlayerClient::CORSMode corsMode)
996     : StreamingClient(src)
997 {
998     DataBufferingPolicy bufferingPolicy = request.url().protocolIs("blob") ? BufferData : DoNotBufferData;
999     RequestOriginPolicy corsPolicy = corsMode != MediaPlayerClient::Unspecified ? PotentiallyCrossOriginEnabled : UseDefaultOriginRestrictionsForType;
1000     StoredCredentials allowCredentials = corsMode == MediaPlayerClient::UseCredentials ? AllowStoredCredentials : DoNotAllowStoredCredentials;
1001     ResourceLoaderOptions options(SendCallbacks, DoNotSniffContent, bufferingPolicy, allowCredentials, DoNotAskClientForCrossOriginCredentials, DoSecurityCheck, corsPolicy);
1002
1003     CachedResourceRequest cacheRequest(request, options);
1004
1005     if (corsMode != MediaPlayerClient::Unspecified) {
1006         m_origin = resourceLoader->document() ? resourceLoader->document()->securityOrigin() : nullptr;
1007         updateRequestForAccessControl(cacheRequest.mutableResourceRequest(), m_origin.get(), allowCredentials);
1008     }
1009
1010     // TODO: Decide whether to use preflight mode for cross-origin requests (see http://wkbug.com/131484).
1011     m_resource = resourceLoader->requestRawResource(cacheRequest);
1012     if (m_resource)
1013         m_resource->addClient(this);
1014 }
1015
1016 CachedResourceStreamingClient::~CachedResourceStreamingClient()
1017 {
1018     if (m_resource) {
1019         m_resource->removeClient(this);
1020         m_resource = 0;
1021     }
1022 }
1023
1024 bool CachedResourceStreamingClient::loadFailed() const
1025 {
1026     return !m_resource;
1027 }
1028
1029 void CachedResourceStreamingClient::setDefersLoading(bool defers)
1030 {
1031     if (m_resource)
1032         m_resource->setDefersLoading(defers);
1033 }
1034
1035 char* CachedResourceStreamingClient::getOrCreateReadBuffer(CachedResource*, size_t requestedSize, size_t& actualSize)
1036 {
1037     return createReadBuffer(requestedSize, actualSize);
1038 }
1039
1040 void CachedResourceStreamingClient::responseReceived(CachedResource* resource, const ResourceResponse& response)
1041 {
1042     CORSAccessCheckResult corsAccessCheck = CORSNoCheck;
1043     if (m_origin)
1044         corsAccessCheck = (m_origin->canRequest(response.url()) || resource->passesAccessControlCheck(m_origin.get())) ? CORSSuccess : CORSFailure;
1045     handleResponseReceived(response, corsAccessCheck);
1046 }
1047
1048 void CachedResourceStreamingClient::dataReceived(CachedResource*, const char* data, int length)
1049 {
1050     handleDataReceived(data, length);
1051 }
1052
1053 void CachedResourceStreamingClient::notifyFinished(CachedResource* resource)
1054 {
1055     if (resource->loadFailedOrCanceled()) {
1056         WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
1057
1058         if (!resource->wasCanceled()) {
1059             const ResourceError& error = resource->resourceError();
1060             GST_ERROR_OBJECT(src, "Have failure: %s", error.localizedDescription().utf8().data());
1061             GST_ELEMENT_ERROR(src, RESOURCE, FAILED, ("%s", error.localizedDescription().utf8().data()), (0));
1062         }
1063         gst_app_src_end_of_stream(src->priv->appsrc);
1064         return;
1065     }
1066
1067     handleNotifyFinished();
1068 }
1069
1070 ResourceHandleStreamingClient::ResourceHandleStreamingClient(WebKitWebSrc* src, const ResourceRequest& request)
1071     : StreamingClient(src)
1072 {
1073     m_resource = ResourceHandle::create(0 /*context*/, request, this, false, false);
1074 }
1075
1076 ResourceHandleStreamingClient::~ResourceHandleStreamingClient()
1077 {
1078     if (m_resource) {
1079         m_resource->cancel();
1080         m_resource.release();
1081         m_resource = 0;
1082     }
1083 }
1084
1085 bool ResourceHandleStreamingClient::loadFailed() const
1086 {
1087     return !m_resource;
1088 }
1089
1090 void ResourceHandleStreamingClient::setDefersLoading(bool defers)
1091 {
1092     if (m_resource)
1093         m_resource->setDefersLoading(defers);
1094 }
1095
1096 char* ResourceHandleStreamingClient::getOrCreateReadBuffer(size_t requestedSize, size_t& actualSize)
1097 {
1098     return createReadBuffer(requestedSize, actualSize);
1099 }
1100
1101 void ResourceHandleStreamingClient::willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse&)
1102 {
1103 }
1104
1105 void ResourceHandleStreamingClient::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
1106 {
1107     handleResponseReceived(response, CORSNoCheck);
1108 }
1109
1110 void ResourceHandleStreamingClient::didReceiveData(ResourceHandle*, const char* /* data */, unsigned /* length */, int)
1111 {
1112     ASSERT_NOT_REACHED();
1113 }
1114
1115 void ResourceHandleStreamingClient::didReceiveBuffer(ResourceHandle*, PassRefPtr<SharedBuffer> buffer, int /* encodedLength */)
1116 {
1117     // This pattern is suggested by SharedBuffer.h.
1118     const char* segment;
1119     unsigned position = 0;
1120     while (unsigned length = buffer->getSomeData(segment, position)) {
1121         handleDataReceived(segment, length);
1122         position += length;
1123     }
1124 }
1125
1126 void ResourceHandleStreamingClient::didFinishLoading(ResourceHandle*, double)
1127 {
1128     handleNotifyFinished();
1129 }
1130
1131 void ResourceHandleStreamingClient::didFail(ResourceHandle*, const ResourceError& error)
1132 {
1133     WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
1134
1135     GST_ERROR_OBJECT(src, "Have failure: %s", error.localizedDescription().utf8().data());
1136     GST_ELEMENT_ERROR(src, RESOURCE, FAILED, ("%s", error.localizedDescription().utf8().data()), (0));
1137     gst_app_src_end_of_stream(src->priv->appsrc);
1138 }
1139
1140 void ResourceHandleStreamingClient::wasBlocked(ResourceHandle*)
1141 {
1142     WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
1143     GUniquePtr<gchar> uri;
1144
1145     GST_ERROR_OBJECT(src, "Request was blocked");
1146
1147     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
1148     uri.reset(g_strdup(src->priv->uri));
1149     locker.unlock();
1150
1151     GST_ELEMENT_ERROR(src, RESOURCE, OPEN_READ, ("Access to \"%s\" was blocked", uri.get()), (0));
1152 }
1153
1154 void ResourceHandleStreamingClient::cannotShowURL(ResourceHandle*)
1155 {
1156     WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src.get());
1157     GUniquePtr<gchar> uri;
1158
1159     GST_ERROR_OBJECT(src, "Cannot show URL");
1160
1161     GMutexLocker locker(GST_OBJECT_GET_LOCK(src));
1162     uri.reset(g_strdup(src->priv->uri));
1163     locker.unlock();
1164
1165     GST_ELEMENT_ERROR(src, RESOURCE, OPEN_READ, ("Can't show \"%s\"", uri.get()), (0));
1166 }
1167
1168 #endif // USE(GSTREAMER)
1169