2 * Copyright (C) 2009, 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 * Copyright (C) 2013 Collabora Ltd.
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.
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.
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
21 #include "WebKitWebSourceGStreamer.h"
23 #if ENABLE(VIDEO) && USE(GSTREAMER)
25 #include "GRefPtrGStreamer.h"
26 #include "GStreamerUtilities.h"
27 #include "GUniquePtrGStreamer.h"
28 #include "HTTPHeaderNames.h"
29 #include "MainThreadNotifier.h"
30 #include "MediaPlayer.h"
31 #include "NotImplemented.h"
32 #include "PlatformMediaResourceLoader.h"
33 #include "ResourceError.h"
34 #include "ResourceHandle.h"
35 #include "ResourceHandleClient.h"
36 #include "ResourceRequest.h"
37 #include "ResourceResponse.h"
38 #include "SharedBuffer.h"
39 #include <gst/app/gstappsrc.h>
41 #include <gst/pbutils/missing-plugins.h>
42 #include <wtf/MainThread.h>
43 #include <wtf/Noncopyable.h>
44 #include <wtf/glib/GMutexLocker.h>
45 #include <wtf/glib/GRefPtr.h>
46 #include <wtf/glib/GUniquePtr.h>
47 #include <wtf/text/CString.h>
49 using namespace WebCore;
51 class StreamingClient {
53 StreamingClient(WebKitWebSrc*);
54 virtual ~StreamingClient();
57 char* createReadBuffer(size_t requestedSize, size_t& actualSize);
58 void handleResponseReceived(const ResourceResponse&);
59 void handleDataReceived(const char*, int);
60 void handleNotifyFinished();
65 class CachedResourceStreamingClient final : public PlatformMediaResourceClient, public StreamingClient {
66 WTF_MAKE_NONCOPYABLE(CachedResourceStreamingClient);
68 CachedResourceStreamingClient(WebKitWebSrc*);
69 virtual ~CachedResourceStreamingClient();
72 // PlatformMediaResourceClient virtual methods.
74 char* getOrCreateReadBuffer(PlatformMediaResource&, size_t requestedSize, size_t& actualSize) override;
76 void responseReceived(PlatformMediaResource&, const ResourceResponse&) override;
77 void dataReceived(PlatformMediaResource&, const char*, int) override;
78 void accessControlCheckFailed(PlatformMediaResource&, const ResourceError&) override;
79 void loadFailed(PlatformMediaResource&, const ResourceError&) override;
80 void loadFinished(PlatformMediaResource&) override;
83 class ResourceHandleStreamingClient : public ResourceHandleClient, public StreamingClient {
84 WTF_MAKE_NONCOPYABLE(ResourceHandleStreamingClient); WTF_MAKE_FAST_ALLOCATED;
86 ResourceHandleStreamingClient(WebKitWebSrc*, ResourceRequest&&);
87 virtual ~ResourceHandleStreamingClient();
89 // StreamingClient virtual methods.
90 bool loadFailed() const;
91 void setDefersLoading(bool);
94 // ResourceHandleClient virtual methods.
96 char* getOrCreateReadBuffer(size_t requestedSize, size_t& actualSize) override;
98 ResourceRequest willSendRequest(ResourceHandle*, ResourceRequest&&, ResourceResponse&&) override;
99 void didReceiveResponse(ResourceHandle*, ResourceResponse&&) override;
100 void didReceiveData(ResourceHandle*, const char*, unsigned, int) override;
101 void didReceiveBuffer(ResourceHandle*, Ref<SharedBuffer>&&, int encodedLength) override;
102 void didFinishLoading(ResourceHandle*, double /*finishTime*/) override;
103 void didFail(ResourceHandle*, const ResourceError&) override;
104 void wasBlocked(ResourceHandle*) override;
105 void cannotShowURL(ResourceHandle*) override;
107 ThreadIdentifier m_thread { 0 };
108 Lock m_initializeRunLoopConditionMutex;
109 Condition m_initializeRunLoopCondition;
110 RunLoop* m_runLoop { nullptr };
111 Lock m_terminateRunLoopConditionMutex;
112 Condition m_terminateRunLoopCondition;
113 RefPtr<ResourceHandle> m_resource;
116 enum MainThreadSourceNotification {
124 #define WEBKIT_WEB_SRC_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_SRC, WebKitWebSrcPrivate))
125 struct _WebKitWebSrcPrivate {
130 GUniquePtr<GstStructure> extraHeaders;
132 GUniquePtr<gchar> httpMethod;
134 WebCore::MediaPlayer* player;
136 RefPtr<PlatformMediaResourceLoader> loader;
137 RefPtr<PlatformMediaResource> resource;
138 std::unique_ptr<ResourceHandleStreamingClient> client;
140 bool didPassAccessControlCheck;
148 guint64 requestedOffset;
150 bool createdInMainThread;
151 MainThreadNotifier<MainThreadSourceNotification> notifier;
152 GRefPtr<GstBuffer> buffer;
164 static GstStaticPadTemplate srcTemplate = GST_STATIC_PAD_TEMPLATE("src",
167 GST_STATIC_CAPS_ANY);
169 GST_DEBUG_CATEGORY_STATIC(webkit_web_src_debug);
170 #define GST_CAT_DEFAULT webkit_web_src_debug
172 static void webKitWebSrcUriHandlerInit(gpointer gIface, gpointer ifaceData);
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);
180 static gboolean webKitWebSrcQueryWithParent(GstPad*, GstObject*, GstQuery*);
182 static void webKitWebSrcNeedData(WebKitWebSrc*);
183 static void webKitWebSrcEnoughData(WebKitWebSrc*);
184 static gboolean webKitWebSrcSeek(WebKitWebSrc*, guint64);
186 static GstAppSrcCallbacks appsrcCallbacks = {
188 [](GstAppSrc*, guint, gpointer userData) {
189 webKitWebSrcNeedData(WEBKIT_WEB_SRC(userData));
192 [](GstAppSrc*, gpointer userData) {
193 webKitWebSrcEnoughData(WEBKIT_WEB_SRC(userData));
196 [](GstAppSrc*, guint64 offset, gpointer userData) -> gboolean {
197 return webKitWebSrcSeek(WEBKIT_WEB_SRC(userData), offset);
202 #define webkit_web_src_parent_class parent_class
203 // We split this out into another macro to avoid a check-webkit-style error.
204 #define WEBKIT_WEB_SRC_CATEGORY_INIT GST_DEBUG_CATEGORY_INIT(webkit_web_src_debug, "webkitwebsrc", 0, "websrc element");
205 G_DEFINE_TYPE_WITH_CODE(WebKitWebSrc, webkit_web_src, GST_TYPE_BIN,
206 G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, webKitWebSrcUriHandlerInit);
207 WEBKIT_WEB_SRC_CATEGORY_INIT);
209 static void webkit_web_src_class_init(WebKitWebSrcClass* klass)
211 GObjectClass* oklass = G_OBJECT_CLASS(klass);
212 GstElementClass* eklass = GST_ELEMENT_CLASS(klass);
214 oklass->dispose = webKitWebSrcDispose;
215 oklass->finalize = webKitWebSrcFinalize;
216 oklass->set_property = webKitWebSrcSetProperty;
217 oklass->get_property = webKitWebSrcGetProperty;
219 gst_element_class_add_pad_template(eklass,
220 gst_static_pad_template_get(&srcTemplate));
221 gst_element_class_set_metadata(eklass, "WebKit Web source element", "Source", "Handles HTTP/HTTPS uris",
222 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
224 /* Allows setting the uri using the 'location' property, which is used
225 * for example by gst_element_make_from_uri() */
226 g_object_class_install_property(oklass,
228 g_param_spec_string("location",
230 "Location to read from",
232 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
234 g_object_class_install_property(oklass, PROP_KEEP_ALIVE,
235 g_param_spec_boolean("keep-alive", "keep-alive", "Use HTTP persistent connections",
236 FALSE, static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
238 g_object_class_install_property(oklass, PROP_EXTRA_HEADERS,
239 g_param_spec_boxed("extra-headers", "Extra Headers", "Extra headers to append to the HTTP request",
240 GST_TYPE_STRUCTURE, static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
242 g_object_class_install_property(oklass, PROP_COMPRESS,
243 g_param_spec_boolean("compress", "Compress", "Allow compressed content encodings",
244 FALSE, static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
246 g_object_class_install_property(oklass, PROP_METHOD,
247 g_param_spec_string("method", "method", "The HTTP method to use (default: GET)",
248 nullptr, static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
250 eklass->change_state = webKitWebSrcChangeState;
252 g_type_class_add_private(klass, sizeof(WebKitWebSrcPrivate));
255 static void webkit_web_src_init(WebKitWebSrc* src)
257 WebKitWebSrcPrivate* priv = WEBKIT_WEB_SRC_GET_PRIVATE(src);
260 new (priv) WebKitWebSrcPrivate();
262 priv->createdInMainThread = isMainThread();
264 priv->appsrc = GST_APP_SRC(gst_element_factory_make("appsrc", 0));
266 GST_ERROR_OBJECT(src, "Failed to create appsrc");
270 gst_bin_add(GST_BIN(src), GST_ELEMENT(priv->appsrc));
273 GRefPtr<GstPad> targetPad = adoptGRef(gst_element_get_static_pad(GST_ELEMENT(priv->appsrc), "src"));
274 priv->srcpad = webkitGstGhostPadFromStaticTemplate(&srcTemplate, "src", targetPad.get());
276 gst_element_add_pad(GST_ELEMENT(src), priv->srcpad);
278 GST_OBJECT_FLAG_SET(priv->srcpad, GST_PAD_FLAG_NEED_PARENT);
279 gst_pad_set_query_function(priv->srcpad, webKitWebSrcQueryWithParent);
281 gst_app_src_set_callbacks(priv->appsrc, &appsrcCallbacks, src, 0);
282 gst_app_src_set_emit_signals(priv->appsrc, FALSE);
283 gst_app_src_set_stream_type(priv->appsrc, GST_APP_STREAM_TYPE_SEEKABLE);
285 // 512k is a abitrary number but we should choose a value
286 // here to not pause/unpause the SoupMessage too often and
287 // to make sure there's always some data available for
288 // GStreamer to handle.
289 gst_app_src_set_max_bytes(priv->appsrc, 512 * 1024);
291 // Emit the need-data signal if the queue contains less
292 // than 20% of data. Without this the need-data signal
293 // is emitted when the queue is empty, we then dispatch
294 // the soup message unpausing to the main loop and from
295 // there unpause the soup message. This already takes
296 // quite some time and libsoup even needs some more time
297 // to actually provide data again. If we do all this
298 // already if the queue is 20% empty, it's much more
299 // likely that libsoup already provides new data before
300 // the queue is really empty.
301 // This might need tweaking for ports not using libsoup.
302 g_object_set(priv->appsrc, "min-percent", 20, NULL);
304 gst_app_src_set_caps(priv->appsrc, 0);
305 gst_app_src_set_size(priv->appsrc, -1);
308 static void webKitWebSrcDispose(GObject* object)
310 WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
311 WebKitWebSrcPrivate* priv = src->priv;
315 GST_CALL_PARENT(G_OBJECT_CLASS, dispose, (object));
318 static void webKitWebSrcFinalize(GObject* object)
320 WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
321 WebKitWebSrcPrivate* priv = src->priv;
324 priv->~WebKitWebSrcPrivate();
326 GST_CALL_PARENT(G_OBJECT_CLASS, finalize, (object));
329 static void webKitWebSrcSetProperty(GObject* object, guint propID, const GValue* value, GParamSpec* pspec)
331 WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
335 gst_uri_handler_set_uri(reinterpret_cast<GstURIHandler*>(src), g_value_get_string(value), 0);
337 case PROP_KEEP_ALIVE:
338 src->priv->keepAlive = g_value_get_boolean(value);
340 case PROP_EXTRA_HEADERS: {
341 const GstStructure* s = gst_value_get_structure(value);
342 src->priv->extraHeaders.reset(s ? gst_structure_copy(s) : nullptr);
346 src->priv->compress = g_value_get_boolean(value);
349 src->priv->httpMethod.reset(g_value_dup_string(value));
352 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, pspec);
357 static void webKitWebSrcGetProperty(GObject* object, guint propID, GValue* value, GParamSpec* pspec)
359 WebKitWebSrc* src = WEBKIT_WEB_SRC(object);
360 WebKitWebSrcPrivate* priv = src->priv;
362 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
365 g_value_set_string(value, priv->uri);
367 case PROP_KEEP_ALIVE:
368 g_value_set_boolean(value, priv->keepAlive);
370 case PROP_EXTRA_HEADERS:
371 gst_value_set_structure(value, priv->extraHeaders.get());
374 g_value_set_boolean(value, priv->compress);
377 g_value_set_string(value, priv->httpMethod.get());
380 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, pspec);
385 static void webKitWebSrcStop(WebKitWebSrc* src)
387 WebKitWebSrcPrivate* priv = src->priv;
389 if (priv->resource || (priv->loader && !priv->keepAlive)) {
390 GRefPtr<WebKitWebSrc> protector = WTF::ensureGRef(src);
391 priv->notifier.cancelPendingNotifications(MainThreadSourceNotification::NeedData | MainThreadSourceNotification::EnoughData | MainThreadSourceNotification::Seek);
392 priv->notifier.notify(MainThreadSourceNotification::Stop, [protector, keepAlive = priv->keepAlive] {
393 WebKitWebSrcPrivate* priv = protector->priv;
395 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(protector.get()));
396 if (priv->resource) {
397 priv->resource->stop();
398 priv->resource->setClient(nullptr);
399 priv->resource = nullptr;
403 priv->loader = nullptr;
407 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
409 bool wasSeeking = std::exchange(priv->isSeeking, false);
411 priv->client = nullptr;
414 unmapGstBuffer(priv->buffer.get());
415 priv->buffer.clear();
418 priv->paused = false;
421 priv->seekable = FALSE;
425 priv->requestedOffset = 0;
432 gst_app_src_set_caps(priv->appsrc, 0);
434 gst_app_src_set_size(priv->appsrc, -1);
437 GST_DEBUG_OBJECT(src, "Stopped request");
440 static bool webKitWebSrcSetExtraHeader(GQuark fieldId, const GValue* value, gpointer userData)
442 GUniquePtr<gchar> fieldContent;
444 if (G_VALUE_HOLDS_STRING(value))
445 fieldContent.reset(g_value_dup_string(value));
447 GValue dest = G_VALUE_INIT;
449 g_value_init(&dest, G_TYPE_STRING);
450 if (g_value_transform(value, &dest))
451 fieldContent.reset(g_value_dup_string(&dest));
454 const gchar* fieldName = g_quark_to_string(fieldId);
455 if (!fieldContent.get()) {
456 GST_ERROR("extra-headers field '%s' contains no value or can't be converted to a string", fieldName);
460 GST_DEBUG("Appending extra header: \"%s: %s\"", fieldName, fieldContent.get());
461 ResourceRequest* request = static_cast<ResourceRequest*>(userData);
462 request->setHTTPHeaderField(fieldName, fieldContent.get());
466 static gboolean webKitWebSrcProcessExtraHeaders(GQuark fieldId, const GValue* value, gpointer userData)
468 if (G_VALUE_TYPE(value) == GST_TYPE_ARRAY) {
469 unsigned size = gst_value_array_get_size(value);
471 for (unsigned i = 0; i < size; i++) {
472 if (!webKitWebSrcSetExtraHeader(fieldId, gst_value_array_get_value(value, i), userData))
478 if (G_VALUE_TYPE(value) == GST_TYPE_LIST) {
479 unsigned size = gst_value_list_get_size(value);
481 for (unsigned i = 0; i < size; i++) {
482 if (!webKitWebSrcSetExtraHeader(fieldId, gst_value_list_get_value(value, i), userData))
488 return webKitWebSrcSetExtraHeader(fieldId, value, userData);
491 static void webKitWebSrcStart(WebKitWebSrc* src)
493 WebKitWebSrcPrivate* priv = src->priv;
495 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
497 priv->didPassAccessControlCheck = false;
500 GST_ERROR_OBJECT(src, "No URI provided");
502 webKitWebSrcStop(src);
506 ASSERT(!priv->client);
508 GST_DEBUG_OBJECT(src, "Fetching %s", priv->uri);
509 URL url = URL(URL(), priv->uri);
511 ResourceRequest request(url);
512 request.setAllowCookies(true);
513 request.setFirstPartyForCookies(url);
518 request.setHTTPReferrer(priv->player->referrer());
520 if (priv->httpMethod.get())
521 request.setHTTPMethod(priv->httpMethod.get());
524 // By default, HTTP Accept-Encoding is disabled here as we don't
525 // want the received response to be encoded in any way as we need
526 // to rely on the proper size of the returned data on
527 // didReceiveResponse.
528 // If Accept-Encoding is used, the server may send the data in encoded format and
529 // request.expectedContentLength() will have the "wrong" size (the size of the
530 // compressed data), even though the data received in didReceiveData is uncompressed.
531 // This is however useful to enable for adaptive streaming
532 // scenarios, when the demuxer needs to download playlists.
534 request.setAcceptEncoding(false);
537 // Let Apple web servers know we want to access their nice movie trailers.
538 if (!g_ascii_strcasecmp("movies.apple.com", url.host().utf8().data())
539 || !g_ascii_strcasecmp("trailers.apple.com", url.host().utf8().data()))
540 request.setHTTPUserAgent("Quicktime/7.6.6");
542 if (priv->requestedOffset) {
543 GUniquePtr<gchar> val(g_strdup_printf("bytes=%" G_GUINT64_FORMAT "-", priv->requestedOffset));
544 request.setHTTPHeaderField(HTTPHeaderName::Range, val.get());
546 priv->offset = priv->requestedOffset;
548 if (!priv->keepAlive) {
549 GST_DEBUG_OBJECT(src, "Persistent connection support disabled");
550 request.setHTTPHeaderField(HTTPHeaderName::Connection, "close");
553 if (priv->extraHeaders)
554 gst_structure_foreach(priv->extraHeaders.get(), webKitWebSrcProcessExtraHeaders, &request);
556 // We always request Icecast/Shoutcast metadata, just in case ...
557 request.setHTTPHeaderField(HTTPHeaderName::IcyMetadata, "1");
559 if (!priv->player || !priv->createdInMainThread) {
560 priv->client = std::make_unique<ResourceHandleStreamingClient>(src, WTFMove(request));
561 if (priv->client->loadFailed()) {
562 GST_ERROR_OBJECT(src, "Failed to setup streaming client");
563 priv->client = nullptr;
565 webKitWebSrcStop(src);
567 GST_DEBUG_OBJECT(src, "Started request");
572 GRefPtr<WebKitWebSrc> protector = WTF::ensureGRef(src);
573 priv->notifier.notify(MainThreadSourceNotification::Start, [protector, request = WTFMove(request)] {
574 WebKitWebSrcPrivate* priv = protector->priv;
576 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(protector.get()));
578 priv->loader = priv->player->createResourceLoader();
580 PlatformMediaResourceLoader::LoadOptions loadOptions = 0;
581 if (request.url().protocolIsBlob())
582 loadOptions |= PlatformMediaResourceLoader::LoadOption::BufferData;
583 // FIXME: request should be moved for efficiency
584 priv->resource = priv->loader->requestResource(ResourceRequest(request), loadOptions);
585 if (priv->resource) {
586 priv->resource->setClient(std::make_unique<CachedResourceStreamingClient>(protector.get()));
587 GST_DEBUG_OBJECT(protector.get(), "Started request");
589 GST_ERROR_OBJECT(protector.get(), "Failed to setup streaming client");
590 priv->loader = nullptr;
592 webKitWebSrcStop(protector.get());
597 static GstStateChangeReturn webKitWebSrcChangeState(GstElement* element, GstStateChange transition)
599 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
600 WebKitWebSrc* src = WEBKIT_WEB_SRC(element);
601 WebKitWebSrcPrivate* priv = src->priv;
603 switch (transition) {
604 case GST_STATE_CHANGE_NULL_TO_READY:
606 gst_element_post_message(element,
607 gst_missing_element_message_new(element, "appsrc"));
608 GST_ELEMENT_ERROR(src, CORE, MISSING_PLUGIN, (0), ("no appsrc"));
609 return GST_STATE_CHANGE_FAILURE;
616 ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
617 if (G_UNLIKELY(ret == GST_STATE_CHANGE_FAILURE)) {
618 GST_DEBUG_OBJECT(src, "State change failed");
622 switch (transition) {
623 case GST_STATE_CHANGE_READY_TO_PAUSED:
625 GST_DEBUG_OBJECT(src, "READY->PAUSED");
626 webKitWebSrcStart(src);
629 case GST_STATE_CHANGE_PAUSED_TO_READY:
631 GST_DEBUG_OBJECT(src, "PAUSED->READY");
632 webKitWebSrcStop(src);
642 static gboolean webKitWebSrcQueryWithParent(GstPad* pad, GstObject* parent, GstQuery* query)
644 WebKitWebSrc* src = WEBKIT_WEB_SRC(GST_ELEMENT(parent));
645 gboolean result = FALSE;
647 switch (GST_QUERY_TYPE(query)) {
648 case GST_QUERY_DURATION: {
651 gst_query_parse_duration(query, &format, NULL);
653 GST_DEBUG_OBJECT(src, "duration query in format %s", gst_format_get_name(format));
654 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
655 if (format == GST_FORMAT_BYTES && src->priv->size > 0) {
656 gst_query_set_duration(query, format, src->priv->size);
661 case GST_QUERY_URI: {
662 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
663 gst_query_set_uri(query, src->priv->uri);
667 case GST_QUERY_SCHEDULING: {
668 GstSchedulingFlags flags;
669 int minSize, maxSize, align;
671 gst_query_parse_scheduling(query, &flags, &minSize, &maxSize, &align);
672 gst_query_set_scheduling(query, static_cast<GstSchedulingFlags>(flags | GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED), minSize, maxSize, align);
677 GRefPtr<GstPad> target = adoptGRef(gst_ghost_pad_get_target(GST_GHOST_PAD_CAST(pad)));
679 // Forward the query to the proxy target pad.
681 result = gst_pad_query(target.get(), query);
689 static bool urlHasSupportedProtocol(const URL& url)
691 return url.isValid() && (url.protocolIsInHTTPFamily() || url.protocolIsBlob());
694 // uri handler interface
696 static GstURIType webKitWebSrcUriGetType(GType)
701 const gchar* const* webKitWebSrcGetProtocols(GType)
703 static const char* protocols[] = {"http", "https", "blob", 0 };
707 static gchar* webKitWebSrcGetUri(GstURIHandler* handler)
709 WebKitWebSrc* src = WEBKIT_WEB_SRC(handler);
712 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
713 ret = g_strdup(src->priv->uri);
717 static gboolean webKitWebSrcSetUri(GstURIHandler* handler, const gchar* uri, GError** error)
719 WebKitWebSrc* src = WEBKIT_WEB_SRC(handler);
720 WebKitWebSrcPrivate* priv = src->priv;
722 if (GST_STATE(src) >= GST_STATE_PAUSED) {
723 GST_ERROR_OBJECT(src, "URI can only be set in states < PAUSED");
727 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
736 if (!urlHasSupportedProtocol(url)) {
737 g_set_error(error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI, "Invalid URI '%s'", uri);
741 priv->uri = g_strdup(url.string().utf8().data());
745 static void webKitWebSrcUriHandlerInit(gpointer gIface, gpointer)
747 GstURIHandlerInterface* iface = (GstURIHandlerInterface *) gIface;
749 iface->get_type = webKitWebSrcUriGetType;
750 iface->get_protocols = webKitWebSrcGetProtocols;
751 iface->get_uri = webKitWebSrcGetUri;
752 iface->set_uri = webKitWebSrcSetUri;
755 static void webKitWebSrcNeedData(WebKitWebSrc* src)
757 WebKitWebSrcPrivate* priv = src->priv;
759 GST_DEBUG_OBJECT(src, "Need more data");
762 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
765 priv->paused = false;
767 priv->client->setDefersLoading(false);
772 GRefPtr<WebKitWebSrc> protector = WTF::ensureGRef(src);
773 priv->notifier.notify(MainThreadSourceNotification::NeedData, [protector] {
774 WebKitWebSrcPrivate* priv = protector->priv;
776 priv->resource->setDefersLoading(false);
780 static void webKitWebSrcEnoughData(WebKitWebSrc* src)
782 WebKitWebSrcPrivate* priv = src->priv;
784 GST_DEBUG_OBJECT(src, "Have enough data");
787 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
792 priv->client->setDefersLoading(true);
797 GRefPtr<WebKitWebSrc> protector = WTF::ensureGRef(src);
798 priv->notifier.notify(MainThreadSourceNotification::EnoughData, [protector] {
799 WebKitWebSrcPrivate* priv = protector->priv;
801 priv->resource->setDefersLoading(true);
805 static gboolean webKitWebSrcSeek(WebKitWebSrc* src, guint64 offset)
807 WebKitWebSrcPrivate* priv = src->priv;
810 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
811 if (offset == priv->offset && priv->requestedOffset == priv->offset)
817 priv->isSeeking = true;
818 priv->requestedOffset = offset;
821 GST_DEBUG_OBJECT(src, "Seeking to offset: %" G_GUINT64_FORMAT, src->priv->requestedOffset);
823 webKitWebSrcStop(src);
824 webKitWebSrcStart(src);
828 GRefPtr<WebKitWebSrc> protector = WTF::ensureGRef(src);
829 priv->notifier.notify(MainThreadSourceNotification::Seek, [protector] {
830 webKitWebSrcStop(protector.get());
831 webKitWebSrcStart(protector.get());
836 void webKitWebSrcSetMediaPlayer(WebKitWebSrc* src, WebCore::MediaPlayer* player)
839 ASSERT(src->priv->createdInMainThread);
840 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
841 src->priv->player = player;
844 bool webKitSrcPassedCORSAccessCheck(WebKitWebSrc* src)
846 return src->priv->didPassAccessControlCheck;
849 StreamingClient::StreamingClient(WebKitWebSrc* src)
850 : m_src(static_cast<GstElement*>(gst_object_ref(src)))
854 StreamingClient::~StreamingClient()
856 gst_object_unref(m_src);
859 char* StreamingClient::createReadBuffer(size_t requestedSize, size_t& actualSize)
861 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
862 WebKitWebSrcPrivate* priv = src->priv;
864 ASSERT(!priv->buffer);
866 GstBuffer* buffer = gst_buffer_new_and_alloc(requestedSize);
868 mapGstBuffer(buffer, GST_MAP_WRITE);
870 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
871 priv->buffer = adoptGRef(buffer);
874 actualSize = gst_buffer_get_size(buffer);
875 return getGstBufferDataPointer(buffer);
878 void StreamingClient::handleResponseReceived(const ResourceResponse& response)
880 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
881 WebKitWebSrcPrivate* priv = src->priv;
883 GST_DEBUG_OBJECT(src, "Received response: %d", response.httpStatusCode());
885 if (response.httpStatusCode() >= 400) {
886 GST_ELEMENT_ERROR(src, RESOURCE, READ, ("Received %d HTTP error code", response.httpStatusCode()), (nullptr));
887 gst_app_src_end_of_stream(priv->appsrc);
888 webKitWebSrcStop(src);
892 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
894 if (priv->isSeeking) {
895 GST_DEBUG_OBJECT(src, "Seek in progress, ignoring response");
899 if (priv->requestedOffset) {
900 // Seeking ... we expect a 206 == PARTIAL_CONTENT
901 if (response.httpStatusCode() == 200) {
902 // Range request didn't have a ranged response; resetting offset.
904 } else if (response.httpStatusCode() != 206) {
905 // Range request completely failed.
907 GST_ELEMENT_ERROR(src, RESOURCE, READ, ("Received unexpected %d HTTP status code", response.httpStatusCode()), (nullptr));
908 gst_app_src_end_of_stream(priv->appsrc);
909 webKitWebSrcStop(src);
914 long long length = response.expectedContentLength();
915 if (length > 0 && priv->requestedOffset && response.httpStatusCode() == 206)
916 length += priv->requestedOffset;
918 priv->size = length >= 0 ? length : 0;
919 priv->seekable = length > 0 && g_ascii_strcasecmp("none", response.httpHeaderField(HTTPHeaderName::AcceptRanges).utf8().data());
923 // notify size/duration
925 gst_app_src_set_size(priv->appsrc, length);
927 gst_app_src_set_size(priv->appsrc, -1);
929 gst_app_src_set_caps(priv->appsrc, 0);
932 void StreamingClient::handleDataReceived(const char* data, int length)
934 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
935 WebKitWebSrcPrivate* priv = src->priv;
937 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
939 GST_LOG_OBJECT(src, "Have %lld bytes of data", priv->buffer ? static_cast<long long>(gst_buffer_get_size(priv->buffer.get())) : length);
941 ASSERT(!priv->buffer || data == getGstBufferDataPointer(priv->buffer.get()));
944 unmapGstBuffer(priv->buffer.get());
946 if (priv->isSeeking) {
947 GST_DEBUG_OBJECT(src, "Seek in progress, ignoring data");
948 priv->buffer.clear();
952 if (priv->offset < priv->requestedOffset) {
953 // Range request failed; seeking manually.
954 if (priv->offset + length <= priv->requestedOffset) {
955 // Discard all the buffers coming before the requested seek position.
956 priv->offset += length;
957 priv->buffer.clear();
961 if (priv->offset + length > priv->requestedOffset) {
962 guint64 offset = priv->requestedOffset - priv->offset;
966 gst_buffer_resize(priv->buffer.get(), offset, -1);
967 priv->offset = priv->requestedOffset;
970 priv->requestedOffset = 0;
973 // Ports using the GStreamer backend but not the soup implementation of ResourceHandle
974 // won't be using buffers provided by this client, the buffer is created here in that case.
976 priv->buffer = adoptGRef(createGstBufferForData(data, length));
978 gst_buffer_set_size(priv->buffer.get(), static_cast<gssize>(length));
980 GST_BUFFER_OFFSET(priv->buffer.get()) = priv->offset;
981 if (priv->requestedOffset == priv->offset)
982 priv->requestedOffset += length;
983 priv->offset += length;
984 // priv->size == 0 if received length on didReceiveResponse < 0.
985 if (priv->size > 0 && priv->offset > priv->size) {
986 GST_DEBUG_OBJECT(src, "Updating internal size from %" G_GUINT64_FORMAT " to %" G_GUINT64_FORMAT, priv->size, priv->offset);
987 gst_app_src_set_size(priv->appsrc, priv->offset);
988 priv->size = priv->offset;
990 GST_BUFFER_OFFSET_END(priv->buffer.get()) = priv->offset;
994 GstFlowReturn ret = gst_app_src_push_buffer(priv->appsrc, priv->buffer.leakRef());
995 if (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)
996 GST_ELEMENT_ERROR(src, CORE, FAILED, (0), (0));
999 void StreamingClient::handleNotifyFinished()
1001 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
1002 WebKitWebSrcPrivate* priv = src->priv;
1004 GST_DEBUG_OBJECT(src, "Have EOS");
1006 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
1007 if (!priv->isSeeking) {
1009 gst_app_src_end_of_stream(priv->appsrc);
1013 CachedResourceStreamingClient::CachedResourceStreamingClient(WebKitWebSrc* src)
1014 : StreamingClient(src)
1018 CachedResourceStreamingClient::~CachedResourceStreamingClient()
1023 char* CachedResourceStreamingClient::getOrCreateReadBuffer(PlatformMediaResource&, size_t requestedSize, size_t& actualSize)
1025 return createReadBuffer(requestedSize, actualSize);
1029 void CachedResourceStreamingClient::responseReceived(PlatformMediaResource&, const ResourceResponse& response)
1031 WebKitWebSrcPrivate* priv = WEBKIT_WEB_SRC(m_src)->priv;
1032 priv->didPassAccessControlCheck = priv->resource->didPassAccessControlCheck();
1033 handleResponseReceived(response);
1036 void CachedResourceStreamingClient::dataReceived(PlatformMediaResource&, const char* data, int length)
1038 handleDataReceived(data, length);
1041 void CachedResourceStreamingClient::accessControlCheckFailed(PlatformMediaResource&, const ResourceError& error)
1043 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
1044 GST_ELEMENT_ERROR(src, RESOURCE, READ, ("%s", error.localizedDescription().utf8().data()), (nullptr));
1045 gst_app_src_end_of_stream(src->priv->appsrc);
1046 webKitWebSrcStop(src);
1049 void CachedResourceStreamingClient::loadFailed(PlatformMediaResource&, const ResourceError& error)
1051 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
1053 if (!error.isCancellation()) {
1054 GST_ERROR_OBJECT(src, "Have failure: %s", error.localizedDescription().utf8().data());
1055 GST_ELEMENT_ERROR(src, RESOURCE, FAILED, ("%s", error.localizedDescription().utf8().data()), (nullptr));
1058 gst_app_src_end_of_stream(src->priv->appsrc);
1061 void CachedResourceStreamingClient::loadFinished(PlatformMediaResource&)
1063 handleNotifyFinished();
1066 ResourceHandleStreamingClient::ResourceHandleStreamingClient(WebKitWebSrc* src, ResourceRequest&& request)
1067 : StreamingClient(src)
1069 LockHolder locker(m_initializeRunLoopConditionMutex);
1070 m_thread = createThread("ResourceHandleStreamingClient", [this, request = WTFMove(request)] {
1072 LockHolder locker(m_initializeRunLoopConditionMutex);
1073 m_runLoop = &RunLoop::current();
1074 m_resource = ResourceHandle::create(nullptr /*context*/, request, this, true, false);
1075 m_initializeRunLoopCondition.notifyOne();
1080 m_runLoop->dispatch([this] { m_resource->setDefersLoading(false); });
1083 LockHolder locker(m_terminateRunLoopConditionMutex);
1084 m_runLoop = nullptr;
1085 m_resource->clearClient();
1086 m_resource->cancel();
1087 m_resource = nullptr;
1088 m_terminateRunLoopCondition.notifyOne();
1091 m_initializeRunLoopCondition.wait(m_initializeRunLoopConditionMutex);
1094 ResourceHandleStreamingClient::~ResourceHandleStreamingClient()
1097 detachThread(m_thread);
1101 if (m_runLoop == &RunLoop::current())
1104 LockHolder locker(m_terminateRunLoopConditionMutex);
1106 m_terminateRunLoopCondition.wait(m_terminateRunLoopConditionMutex);
1110 bool ResourceHandleStreamingClient::loadFailed() const
1115 void ResourceHandleStreamingClient::setDefersLoading(bool defers)
1117 m_runLoop->dispatch([this, defers] {
1119 m_resource->setDefersLoading(defers);
1124 char* ResourceHandleStreamingClient::getOrCreateReadBuffer(size_t requestedSize, size_t& actualSize)
1126 return createReadBuffer(requestedSize, actualSize);
1130 ResourceRequest ResourceHandleStreamingClient::willSendRequest(ResourceHandle*, ResourceRequest&& request, ResourceResponse&&)
1132 return WTFMove(request);
1135 void ResourceHandleStreamingClient::didReceiveResponse(ResourceHandle*, ResourceResponse&& response)
1137 handleResponseReceived(response);
1140 void ResourceHandleStreamingClient::didReceiveData(ResourceHandle*, const char* /* data */, unsigned /* length */, int)
1142 ASSERT_NOT_REACHED();
1145 void ResourceHandleStreamingClient::didReceiveBuffer(ResourceHandle*, Ref<SharedBuffer>&& buffer, int /* encodedLength */)
1147 // This pattern is suggested by SharedBuffer.h.
1148 const char* segment;
1149 unsigned position = 0;
1150 while (unsigned length = buffer->getSomeData(segment, position)) {
1151 handleDataReceived(segment, length);
1156 void ResourceHandleStreamingClient::didFinishLoading(ResourceHandle*, double)
1158 handleNotifyFinished();
1161 void ResourceHandleStreamingClient::didFail(ResourceHandle*, const ResourceError& error)
1163 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
1165 GST_ERROR_OBJECT(src, "Have failure: %s", error.localizedDescription().utf8().data());
1166 GST_ELEMENT_ERROR(src, RESOURCE, FAILED, ("%s", error.localizedDescription().utf8().data()), (0));
1167 gst_app_src_end_of_stream(src->priv->appsrc);
1170 void ResourceHandleStreamingClient::wasBlocked(ResourceHandle*)
1172 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
1173 GUniquePtr<gchar> uri;
1175 GST_ERROR_OBJECT(src, "Request was blocked");
1177 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
1178 uri.reset(g_strdup(src->priv->uri));
1181 GST_ELEMENT_ERROR(src, RESOURCE, OPEN_READ, ("Access to \"%s\" was blocked", uri.get()), (0));
1184 void ResourceHandleStreamingClient::cannotShowURL(ResourceHandle*)
1186 WebKitWebSrc* src = WEBKIT_WEB_SRC(m_src);
1187 GUniquePtr<gchar> uri;
1189 GST_ERROR_OBJECT(src, "Cannot show URL");
1191 WTF::GMutexLocker<GMutex> locker(*GST_OBJECT_GET_LOCK(src));
1192 uri.reset(g_strdup(src->priv->uri));
1195 GST_ELEMENT_ERROR(src, RESOURCE, OPEN_READ, ("Can't show \"%s\"", uri.get()), (0));
1198 #endif // USE(GSTREAMER)