2013-04-16 22:47:02 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
|
|
|
|
*
|
|
|
|
* gstfragment.c:
|
|
|
|
*
|
|
|
|
* 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; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2018-09-24 10:52:22 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2013-04-16 22:47:02 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include "gstfragment.h"
|
|
|
|
#include "gsturidownloader.h"
|
|
|
|
#include "gsturidownloader_debug.h"
|
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT uridownloader_debug
|
|
|
|
GST_DEBUG_CATEGORY (uridownloader_debug);
|
|
|
|
|
|
|
|
struct _GstUriDownloaderPrivate
|
|
|
|
{
|
|
|
|
/* Fragments fetcher */
|
|
|
|
GstElement *urisrc;
|
|
|
|
GstBus *bus;
|
|
|
|
GstPad *pad;
|
|
|
|
GstFragment *download;
|
2014-03-07 15:22:10 +00:00
|
|
|
gboolean got_buffer;
|
2013-07-03 02:16:59 +00:00
|
|
|
GMutex download_lock; /* used to restrict to one download only */
|
|
|
|
|
2017-03-17 10:58:06 +00:00
|
|
|
GWeakRef parent;
|
2015-12-09 16:12:42 +00:00
|
|
|
|
2014-02-11 13:13:30 +00:00
|
|
|
GError *err;
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
GCond cond;
|
2013-04-16 20:23:02 +00:00
|
|
|
gboolean cancelled;
|
2013-04-16 22:47:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void gst_uri_downloader_finalize (GObject * object);
|
|
|
|
static void gst_uri_downloader_dispose (GObject * object);
|
|
|
|
|
|
|
|
static GstFlowReturn gst_uri_downloader_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buf);
|
|
|
|
static gboolean gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static GstBusSyncReply gst_uri_downloader_bus_handler (GstBus * bus,
|
|
|
|
GstMessage * message, gpointer data);
|
|
|
|
|
2016-05-06 08:51:39 +00:00
|
|
|
static gboolean gst_uri_downloader_ensure_src (GstUriDownloader * downloader,
|
|
|
|
const gchar * uri);
|
|
|
|
static void gst_uri_downloader_destroy_src (GstUriDownloader * downloader);
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
static GstStaticPadTemplate sinkpadtemplate = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
#define _do_init \
|
|
|
|
{ \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (uridownloader_debug, "uridownloader", 0, "URI downloader"); \
|
|
|
|
}
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstUriDownloader, gst_uri_downloader, GST_TYPE_OBJECT,
|
2018-06-23 22:17:26 +00:00
|
|
|
G_ADD_PRIVATE (GstUriDownloader)
|
2013-04-16 22:47:02 +00:00
|
|
|
_do_init);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_uri_downloader_class_init (GstUriDownloaderClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
|
|
|
|
gobject_class->dispose = gst_uri_downloader_dispose;
|
|
|
|
gobject_class->finalize = gst_uri_downloader_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_uri_downloader_init (GstUriDownloader * downloader)
|
|
|
|
{
|
2018-06-23 22:17:26 +00:00
|
|
|
downloader->priv = gst_uri_downloader_get_instance_private (downloader);
|
2013-04-16 22:47:02 +00:00
|
|
|
|
|
|
|
/* Initialize the sink pad. This pad will be connected to the src pad of the
|
|
|
|
* element created with gst_element_make_from_uri and will handle the download */
|
|
|
|
downloader->priv->pad =
|
|
|
|
gst_pad_new_from_static_template (&sinkpadtemplate, "sink");
|
|
|
|
gst_pad_set_chain_function (downloader->priv->pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_uri_downloader_chain));
|
|
|
|
gst_pad_set_event_function (downloader->priv->pad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_uri_downloader_sink_event));
|
|
|
|
gst_pad_set_element_private (downloader->priv->pad, downloader);
|
|
|
|
gst_pad_set_active (downloader->priv->pad, TRUE);
|
|
|
|
|
|
|
|
/* Create a bus to handle error and warning message from the source element */
|
|
|
|
downloader->priv->bus = gst_bus_new ();
|
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
g_mutex_init (&downloader->priv->download_lock);
|
2013-04-16 22:47:02 +00:00
|
|
|
g_cond_init (&downloader->priv->cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_uri_downloader_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
|
|
|
|
|
2016-05-06 08:51:39 +00:00
|
|
|
gst_uri_downloader_destroy_src (downloader);
|
2013-04-16 22:47:02 +00:00
|
|
|
|
|
|
|
if (downloader->priv->bus != NULL) {
|
|
|
|
gst_object_unref (downloader->priv->bus);
|
|
|
|
downloader->priv->bus = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloader->priv->pad) {
|
|
|
|
gst_object_unref (downloader->priv->pad);
|
|
|
|
downloader->priv->pad = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (downloader->priv->download) {
|
|
|
|
g_object_unref (downloader->priv->download);
|
|
|
|
downloader->priv->download = NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-17 10:58:06 +00:00
|
|
|
g_weak_ref_clear (&downloader->priv->parent);
|
2015-12-09 16:12:42 +00:00
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
G_OBJECT_CLASS (gst_uri_downloader_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_uri_downloader_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
|
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
g_mutex_clear (&downloader->priv->download_lock);
|
2013-04-16 22:47:02 +00:00
|
|
|
g_cond_clear (&downloader->priv->cond);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_uri_downloader_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
GstUriDownloader *
|
|
|
|
gst_uri_downloader_new (void)
|
|
|
|
{
|
2017-05-15 17:31:31 +00:00
|
|
|
GstUriDownloader *downloader;
|
|
|
|
|
|
|
|
downloader = g_object_new (GST_TYPE_URI_DOWNLOADER, NULL);
|
|
|
|
gst_object_ref_sink (downloader);
|
|
|
|
|
|
|
|
return downloader;
|
2013-04-16 22:47:02 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 16:12:42 +00:00
|
|
|
/**
|
|
|
|
* gst_uri_downloader_set_parent:
|
|
|
|
* @param downloader: the #GstUriDownloader
|
|
|
|
* @param parent: the parent #GstElement
|
|
|
|
*
|
|
|
|
* Sets an element as parent of this #GstUriDownloader so that context
|
|
|
|
* requests from the underlying source are proxied to the main pipeline
|
|
|
|
* and set back if a context was provided.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_uri_downloader_set_parent (GstUriDownloader * downloader,
|
|
|
|
GstElement * parent)
|
|
|
|
{
|
2017-03-17 10:58:06 +00:00
|
|
|
g_weak_ref_set (&downloader->priv->parent, parent);
|
2015-12-09 16:12:42 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
static gboolean
|
|
|
|
gst_uri_downloader_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
GstUriDownloader *downloader;
|
|
|
|
|
|
|
|
downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case GST_EVENT_EOS:{
|
|
|
|
GST_OBJECT_LOCK (downloader);
|
|
|
|
GST_DEBUG_OBJECT (downloader, "Got EOS on the fetcher pad");
|
|
|
|
if (downloader->priv->download != NULL) {
|
|
|
|
/* signal we have fetched the URI */
|
|
|
|
downloader->priv->download->completed = TRUE;
|
|
|
|
downloader->priv->download->download_stop_time =
|
|
|
|
gst_util_get_timestamp ();
|
|
|
|
GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
|
|
|
|
g_cond_signal (&downloader->priv->cond);
|
|
|
|
}
|
2013-07-03 02:16:59 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
2013-04-16 22:47:02 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
|
|
|
}
|
2015-08-14 08:44:24 +00:00
|
|
|
case GST_EVENT_CUSTOM_DOWNSTREAM_STICKY:{
|
|
|
|
const GstStructure *str;
|
|
|
|
str = gst_event_get_structure (event);
|
|
|
|
if (gst_structure_has_name (str, "http-headers")) {
|
|
|
|
GST_OBJECT_LOCK (downloader);
|
|
|
|
if (downloader->priv->download != NULL) {
|
|
|
|
if (downloader->priv->download->headers)
|
|
|
|
gst_structure_free (downloader->priv->download->headers);
|
|
|
|
downloader->priv->download->headers = gst_structure_copy (str);
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* falls through */
|
2013-04-16 22:47:02 +00:00
|
|
|
default:
|
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBusSyncReply
|
|
|
|
gst_uri_downloader_bus_handler (GstBus * bus,
|
|
|
|
GstMessage * message, gpointer data)
|
|
|
|
{
|
|
|
|
GstUriDownloader *downloader = (GstUriDownloader *) (data);
|
|
|
|
|
2014-02-27 10:53:12 +00:00
|
|
|
if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
|
2013-04-16 22:47:02 +00:00
|
|
|
GError *err = NULL;
|
|
|
|
gchar *dbg_info = NULL;
|
2014-06-19 07:57:25 +00:00
|
|
|
gchar *new_error = NULL;
|
2013-04-16 22:47:02 +00:00
|
|
|
|
|
|
|
gst_message_parse_error (message, &err, &dbg_info);
|
|
|
|
GST_WARNING_OBJECT (downloader,
|
|
|
|
"Received error: %s from %s, the download will be cancelled",
|
2014-03-13 13:41:06 +00:00
|
|
|
err->message, GST_OBJECT_NAME (message->src));
|
2019-12-11 09:47:20 +00:00
|
|
|
GST_DEBUG ("Debugging info: %s", (dbg_info) ? dbg_info : "none");
|
2014-02-11 13:13:30 +00:00
|
|
|
|
2014-06-19 07:57:25 +00:00
|
|
|
if (dbg_info)
|
|
|
|
new_error = g_strdup_printf ("%s: %s\n", err->message, dbg_info);
|
|
|
|
if (new_error) {
|
|
|
|
g_free (err->message);
|
|
|
|
err->message = new_error;
|
|
|
|
}
|
|
|
|
|
2014-02-11 13:13:30 +00:00
|
|
|
if (!downloader->priv->err)
|
|
|
|
downloader->priv->err = err;
|
|
|
|
else
|
|
|
|
g_error_free (err);
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
g_free (dbg_info);
|
|
|
|
|
|
|
|
/* remove the sync handler to avoid duplicated messages */
|
|
|
|
gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL, NULL);
|
2013-07-09 01:00:48 +00:00
|
|
|
|
|
|
|
/* stop the download */
|
|
|
|
GST_OBJECT_LOCK (downloader);
|
|
|
|
if (downloader->priv->download != NULL) {
|
|
|
|
GST_DEBUG_OBJECT (downloader, "Stopping download");
|
|
|
|
g_object_unref (downloader->priv->download);
|
|
|
|
downloader->priv->download = NULL;
|
2014-02-04 11:52:25 +00:00
|
|
|
downloader->priv->cancelled = TRUE;
|
2013-07-09 01:00:48 +00:00
|
|
|
g_cond_signal (&downloader->priv->cond);
|
|
|
|
}
|
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
2014-02-27 10:53:12 +00:00
|
|
|
} else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) {
|
|
|
|
GError *err = NULL;
|
|
|
|
gchar *dbg_info = NULL;
|
|
|
|
|
|
|
|
gst_message_parse_warning (message, &err, &dbg_info);
|
|
|
|
GST_WARNING_OBJECT (downloader,
|
2014-02-27 11:02:19 +00:00
|
|
|
"Received warning: %s from %s",
|
2014-02-27 10:53:12 +00:00
|
|
|
GST_OBJECT_NAME (message->src), err->message);
|
2019-12-11 09:47:20 +00:00
|
|
|
GST_DEBUG ("Debugging info: %s", (dbg_info) ? dbg_info : "none");
|
2014-02-27 10:53:12 +00:00
|
|
|
g_error_free (err);
|
|
|
|
g_free (dbg_info);
|
2015-12-09 16:12:42 +00:00
|
|
|
} else if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_NEED_CONTEXT) {
|
2017-03-17 10:58:06 +00:00
|
|
|
GstElement *parent = g_weak_ref_get (&downloader->priv->parent);
|
|
|
|
|
2015-12-09 16:12:42 +00:00
|
|
|
/* post the same need-context as if it was from the parent and then
|
|
|
|
* get it to our internal element that requested it */
|
2017-03-17 10:58:06 +00:00
|
|
|
if (parent && GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
|
2015-12-09 16:12:42 +00:00
|
|
|
const gchar *context_type;
|
|
|
|
GstContext *context;
|
|
|
|
GstElement *msg_src = GST_ELEMENT_CAST (GST_MESSAGE_SRC (message));
|
|
|
|
|
|
|
|
gst_message_parse_context_type (message, &context_type);
|
2017-03-17 10:58:06 +00:00
|
|
|
context = gst_element_get_context (parent, context_type);
|
2015-12-09 16:12:42 +00:00
|
|
|
|
|
|
|
/* No context, request one */
|
|
|
|
if (!context) {
|
|
|
|
GstMessage *need_context_msg =
|
2017-03-17 10:58:06 +00:00
|
|
|
gst_message_new_need_context (GST_OBJECT_CAST (parent),
|
|
|
|
context_type);
|
|
|
|
gst_element_post_message (parent, need_context_msg);
|
|
|
|
context = gst_element_get_context (parent, context_type);
|
2015-12-09 16:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (context) {
|
|
|
|
gst_element_set_context (msg_src, context);
|
|
|
|
gst_context_unref (context);
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 10:58:06 +00:00
|
|
|
if (parent)
|
|
|
|
gst_object_unref (parent);
|
2013-04-16 22:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_message_unref (message);
|
|
|
|
return GST_BUS_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_uri_downloader_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstUriDownloader *downloader;
|
|
|
|
|
|
|
|
downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
|
|
|
|
|
|
|
|
/* HTML errors (404, 500, etc...) are also pushed through this pad as
|
|
|
|
* response but the source element will also post a warning or error message
|
|
|
|
* in the bus, which is handled synchronously cancelling the download.
|
|
|
|
*/
|
|
|
|
GST_OBJECT_LOCK (downloader);
|
|
|
|
if (downloader->priv->download == NULL) {
|
|
|
|
/* Download cancelled, quit */
|
2015-01-15 16:11:24 +00:00
|
|
|
gst_buffer_unref (buf);
|
2013-04-16 22:47:02 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (downloader, "The uri fetcher received a new buffer "
|
|
|
|
"of size %" G_GSIZE_FORMAT, gst_buffer_get_size (buf));
|
2014-03-07 15:22:10 +00:00
|
|
|
downloader->priv->got_buffer = TRUE;
|
2015-01-15 16:11:24 +00:00
|
|
|
if (!gst_fragment_add_buffer (downloader->priv->download, buf)) {
|
2013-04-16 22:47:02 +00:00
|
|
|
GST_WARNING_OBJECT (downloader, "Could not add buffer to fragment");
|
2015-01-15 16:11:24 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
}
|
2013-04-16 22:47:02 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
|
|
|
|
|
|
|
done:
|
|
|
|
{
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 20:23:02 +00:00
|
|
|
void
|
|
|
|
gst_uri_downloader_reset (GstUriDownloader * downloader)
|
|
|
|
{
|
|
|
|
g_return_if_fail (downloader != NULL);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (downloader);
|
|
|
|
downloader->priv->cancelled = FALSE;
|
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
|
|
|
}
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
void
|
|
|
|
gst_uri_downloader_cancel (GstUriDownloader * downloader)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (downloader);
|
|
|
|
if (downloader->priv->download != NULL) {
|
|
|
|
GST_DEBUG_OBJECT (downloader, "Cancelling download");
|
|
|
|
g_object_unref (downloader->priv->download);
|
|
|
|
downloader->priv->download = NULL;
|
2013-04-16 20:23:02 +00:00
|
|
|
downloader->priv->cancelled = TRUE;
|
2013-04-16 22:47:02 +00:00
|
|
|
GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
|
|
|
|
g_cond_signal (&downloader->priv->cond);
|
|
|
|
} else {
|
2013-04-16 20:23:02 +00:00
|
|
|
gboolean cancelled;
|
|
|
|
|
|
|
|
cancelled = downloader->priv->cancelled;
|
|
|
|
downloader->priv->cancelled = TRUE;
|
|
|
|
if (cancelled)
|
|
|
|
GST_DEBUG_OBJECT (downloader,
|
2013-07-09 01:00:48 +00:00
|
|
|
"Trying to cancel a download that was alredy cancelled");
|
2013-04-16 22:47:02 +00:00
|
|
|
}
|
2013-07-03 02:16:59 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
2013-04-16 22:47:02 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 21:28:15 +00:00
|
|
|
static gboolean
|
|
|
|
gst_uri_downloader_set_range (GstUriDownloader * downloader,
|
|
|
|
gint64 range_start, gint64 range_end)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (range_start >= 0, FALSE);
|
|
|
|
g_return_val_if_fail (range_end >= -1, FALSE);
|
|
|
|
|
|
|
|
if (range_start || (range_end >= 0)) {
|
|
|
|
GstEvent *seek;
|
|
|
|
|
|
|
|
seek = gst_event_new_seek (1.0, GST_FORMAT_BYTES, GST_SEEK_FLAG_FLUSH,
|
|
|
|
GST_SEEK_TYPE_SET, range_start, GST_SEEK_TYPE_SET, range_end);
|
|
|
|
|
|
|
|
return gst_element_send_event (downloader->priv->urisrc, seek);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
static gboolean
|
2016-05-06 08:51:39 +00:00
|
|
|
gst_uri_downloader_ensure_src (GstUriDownloader * downloader, const gchar * uri)
|
2013-04-16 22:47:02 +00:00
|
|
|
{
|
2014-02-13 09:06:48 +00:00
|
|
|
if (downloader->priv->urisrc) {
|
|
|
|
gchar *old_protocol, *new_protocol;
|
|
|
|
gchar *old_uri;
|
2013-02-07 03:28:15 +00:00
|
|
|
|
2014-02-13 09:06:48 +00:00
|
|
|
old_uri =
|
|
|
|
gst_uri_handler_get_uri (GST_URI_HANDLER (downloader->priv->urisrc));
|
|
|
|
old_protocol = gst_uri_get_protocol (old_uri);
|
|
|
|
new_protocol = gst_uri_get_protocol (uri);
|
|
|
|
|
|
|
|
if (!g_str_equal (old_protocol, new_protocol)) {
|
2016-05-06 08:51:39 +00:00
|
|
|
gst_uri_downloader_destroy_src (downloader);
|
2014-02-13 09:06:48 +00:00
|
|
|
GST_DEBUG_OBJECT (downloader, "Can't re-use old source element");
|
|
|
|
} else {
|
2014-02-13 09:21:15 +00:00
|
|
|
GError *err = NULL;
|
|
|
|
|
2014-02-13 09:06:48 +00:00
|
|
|
GST_DEBUG_OBJECT (downloader, "Re-using old source element");
|
2016-05-06 08:51:39 +00:00
|
|
|
if (!gst_uri_handler_set_uri
|
|
|
|
(GST_URI_HANDLER (downloader->priv->urisrc), uri, &err)) {
|
|
|
|
GST_DEBUG_OBJECT (downloader,
|
|
|
|
"Failed to re-use old source element: %s", err->message);
|
2014-02-13 09:21:15 +00:00
|
|
|
g_clear_error (&err);
|
2016-05-06 08:51:39 +00:00
|
|
|
gst_uri_downloader_destroy_src (downloader);
|
2014-02-13 09:21:15 +00:00
|
|
|
}
|
2014-02-13 09:06:48 +00:00
|
|
|
}
|
|
|
|
g_free (old_uri);
|
|
|
|
g_free (old_protocol);
|
|
|
|
g_free (new_protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!downloader->priv->urisrc) {
|
|
|
|
GST_DEBUG_OBJECT (downloader, "Creating source element for the URI:%s",
|
|
|
|
uri);
|
|
|
|
downloader->priv->urisrc =
|
|
|
|
gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
|
2016-05-06 08:51:39 +00:00
|
|
|
if (downloader->priv->urisrc) {
|
|
|
|
/* gst_element_make_from_uri returns a floating reference
|
|
|
|
* and we are not going to transfer the ownership, so we
|
|
|
|
* should take it.
|
|
|
|
*/
|
|
|
|
gst_object_ref_sink (downloader->priv->urisrc);
|
|
|
|
}
|
2014-02-13 09:06:48 +00:00
|
|
|
}
|
2013-04-16 22:47:02 +00:00
|
|
|
|
2016-05-06 08:51:39 +00:00
|
|
|
return downloader->priv->urisrc != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_uri_downloader_destroy_src (GstUriDownloader * downloader)
|
|
|
|
{
|
|
|
|
if (!downloader->priv->urisrc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gst_element_set_state (downloader->priv->urisrc, GST_STATE_NULL);
|
|
|
|
gst_object_unref (downloader->priv->urisrc);
|
|
|
|
downloader->priv->urisrc = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_uri_downloader_set_uri (GstUriDownloader * downloader, const gchar * uri,
|
|
|
|
const gchar * referer, gboolean compress,
|
|
|
|
gboolean refresh, gboolean allow_cache)
|
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
|
|
|
if (!gst_uri_is_valid (uri))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!gst_uri_downloader_ensure_src (downloader, uri))
|
|
|
|
return FALSE;
|
|
|
|
|
2014-02-12 12:08:29 +00:00
|
|
|
gobject_class = G_OBJECT_GET_CLASS (downloader->priv->urisrc);
|
|
|
|
if (g_object_class_find_property (gobject_class, "compress"))
|
|
|
|
g_object_set (downloader->priv->urisrc, "compress", compress, NULL);
|
2014-02-13 09:21:15 +00:00
|
|
|
if (g_object_class_find_property (gobject_class, "keep-alive"))
|
|
|
|
g_object_set (downloader->priv->urisrc, "keep-alive", TRUE, NULL);
|
2014-04-28 08:12:52 +00:00
|
|
|
if (g_object_class_find_property (gobject_class, "extra-headers")) {
|
2014-05-05 07:36:56 +00:00
|
|
|
if (referer || refresh || !allow_cache) {
|
2014-05-02 08:36:21 +00:00
|
|
|
GstStructure *extra_headers = gst_structure_new_empty ("headers");
|
|
|
|
|
|
|
|
if (referer)
|
|
|
|
gst_structure_set (extra_headers, "Referer", G_TYPE_STRING, referer,
|
|
|
|
NULL);
|
2014-05-05 07:36:56 +00:00
|
|
|
|
|
|
|
if (!allow_cache)
|
|
|
|
gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
|
|
|
|
"no-cache", NULL);
|
|
|
|
else if (refresh)
|
2014-05-02 08:36:21 +00:00
|
|
|
gst_structure_set (extra_headers, "Cache-Control", G_TYPE_STRING,
|
|
|
|
"max-age=0", NULL);
|
2014-04-28 07:23:34 +00:00
|
|
|
|
2014-04-28 08:12:52 +00:00
|
|
|
g_object_set (downloader->priv->urisrc, "extra-headers", extra_headers,
|
|
|
|
NULL);
|
2014-04-28 07:23:34 +00:00
|
|
|
|
2014-04-28 08:12:52 +00:00
|
|
|
gst_structure_free (extra_headers);
|
|
|
|
} else {
|
|
|
|
g_object_set (downloader->priv->urisrc, "extra-headers", NULL, NULL);
|
|
|
|
}
|
2014-04-28 07:23:34 +00:00
|
|
|
}
|
2014-02-12 12:08:29 +00:00
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
/* add a sync handler for the bus messages to detect errors in the download */
|
|
|
|
gst_element_set_bus (GST_ELEMENT (downloader->priv->urisrc),
|
|
|
|
downloader->priv->bus);
|
|
|
|
gst_bus_set_sync_handler (downloader->priv->bus,
|
|
|
|
gst_uri_downloader_bus_handler, downloader, NULL);
|
|
|
|
|
|
|
|
pad = gst_element_get_static_pad (downloader->priv->urisrc, "src");
|
|
|
|
if (!pad)
|
|
|
|
return FALSE;
|
|
|
|
gst_pad_link (pad, downloader->priv->pad);
|
|
|
|
gst_object_unref (pad);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-08-14 08:44:24 +00:00
|
|
|
static gboolean
|
|
|
|
gst_uri_downloader_set_method (GstUriDownloader * downloader,
|
|
|
|
const gchar * method)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
|
|
|
if (!downloader->priv->urisrc)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_GET_CLASS (downloader->priv->urisrc);
|
|
|
|
if (g_object_class_find_property (gobject_class, "method")) {
|
|
|
|
g_object_set (downloader->priv->urisrc, "method", method, NULL);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
GstFragment *
|
2014-05-02 08:36:21 +00:00
|
|
|
gst_uri_downloader_fetch_uri (GstUriDownloader * downloader,
|
|
|
|
const gchar * uri, const gchar * referer, gboolean compress,
|
2014-05-05 07:36:56 +00:00
|
|
|
gboolean refresh, gboolean allow_cache, GError ** err)
|
2014-04-28 07:23:34 +00:00
|
|
|
{
|
2014-05-02 08:36:21 +00:00
|
|
|
return gst_uri_downloader_fetch_uri_with_range (downloader, uri,
|
2014-05-05 07:36:56 +00:00
|
|
|
referer, compress, refresh, allow_cache, 0, -1, err);
|
2013-06-19 21:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-02 08:36:21 +00:00
|
|
|
* gst_uri_downloader_fetch_uri_with_range:
|
2013-06-19 21:28:15 +00:00
|
|
|
* @downloader: the #GstUriDownloader
|
|
|
|
* @uri: the uri
|
|
|
|
* @range_start: the starting byte index
|
|
|
|
* @range_end: the final byte index, use -1 for unspecified
|
|
|
|
*
|
|
|
|
* Returns the downloaded #GstFragment
|
|
|
|
*/
|
|
|
|
GstFragment *
|
2014-05-02 08:36:21 +00:00
|
|
|
gst_uri_downloader_fetch_uri_with_range (GstUriDownloader *
|
2014-04-28 07:23:34 +00:00
|
|
|
downloader, const gchar * uri, const gchar * referer, gboolean compress,
|
2014-05-05 07:36:56 +00:00
|
|
|
gboolean refresh, gboolean allow_cache,
|
|
|
|
gint64 range_start, gint64 range_end, GError ** err)
|
2013-04-16 22:47:02 +00:00
|
|
|
{
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
GstFragment *download = NULL;
|
|
|
|
|
2013-07-09 01:00:48 +00:00
|
|
|
GST_DEBUG_OBJECT (downloader, "Fetching URI %s", uri);
|
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
g_mutex_lock (&downloader->priv->download_lock);
|
2014-02-11 13:13:30 +00:00
|
|
|
downloader->priv->err = NULL;
|
2014-03-07 15:22:10 +00:00
|
|
|
downloader->priv->got_buffer = FALSE;
|
2013-04-16 22:47:02 +00:00
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
GST_OBJECT_LOCK (downloader);
|
2013-04-16 20:23:02 +00:00
|
|
|
if (downloader->priv->cancelled) {
|
2013-07-09 01:00:48 +00:00
|
|
|
GST_DEBUG_OBJECT (downloader, "Cancelled, aborting fetch");
|
2013-04-16 20:23:02 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
2014-05-05 07:36:56 +00:00
|
|
|
if (!gst_uri_downloader_set_uri (downloader, uri, referer, compress, refresh,
|
|
|
|
allow_cache)) {
|
2013-06-19 21:28:15 +00:00
|
|
|
GST_WARNING_OBJECT (downloader, "Failed to set URI");
|
2013-04-16 22:47:02 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
2013-02-07 03:28:15 +00:00
|
|
|
gst_bus_set_flushing (downloader->priv->bus, FALSE);
|
2015-01-15 16:11:24 +00:00
|
|
|
if (downloader->priv->download)
|
|
|
|
g_object_unref (downloader->priv->download);
|
2013-07-09 01:00:48 +00:00
|
|
|
downloader->priv->download = gst_fragment_new ();
|
2015-08-14 08:44:24 +00:00
|
|
|
downloader->priv->download->range_start = range_start;
|
|
|
|
downloader->priv->download->range_end = range_end;
|
2013-07-03 02:16:59 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
2013-06-19 21:28:15 +00:00
|
|
|
ret = gst_element_set_state (downloader->priv->urisrc, GST_STATE_READY);
|
2013-07-03 02:16:59 +00:00
|
|
|
GST_OBJECT_LOCK (downloader);
|
2013-07-09 01:00:48 +00:00
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE || downloader->priv->download == NULL) {
|
|
|
|
GST_WARNING_OBJECT (downloader, "Failed to set src to READY");
|
2013-06-19 21:28:15 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
/* might have been cancelled because of failures in state change */
|
|
|
|
if (downloader->priv->cancelled) {
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
2015-08-14 08:44:24 +00:00
|
|
|
if (range_start < 0 && range_end < 0) {
|
|
|
|
if (!gst_uri_downloader_set_method (downloader, "HEAD")) {
|
|
|
|
GST_WARNING_OBJECT (downloader, "Failed to set HTTP method");
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!gst_uri_downloader_set_range (downloader, range_start, range_end)) {
|
|
|
|
GST_WARNING_OBJECT (downloader, "Failed to set range");
|
|
|
|
goto quit;
|
|
|
|
}
|
2013-06-19 21:28:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
2013-04-16 22:47:02 +00:00
|
|
|
ret = gst_element_set_state (downloader->priv->urisrc, GST_STATE_PLAYING);
|
2013-07-03 02:16:59 +00:00
|
|
|
GST_OBJECT_LOCK (downloader);
|
2013-04-16 22:47:02 +00:00
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE) {
|
2013-07-09 01:01:58 +00:00
|
|
|
if (downloader->priv->download) {
|
|
|
|
g_object_unref (downloader->priv->download);
|
|
|
|
downloader->priv->download = NULL;
|
|
|
|
}
|
2013-04-16 22:47:02 +00:00
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
/* might have been cancelled because of failures in state change */
|
|
|
|
if (downloader->priv->cancelled) {
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
/* wait until:
|
|
|
|
* - the download succeed (EOS in the src pad)
|
|
|
|
* - the download failed (Error message on the fetcher bus)
|
|
|
|
* - the download was canceled
|
|
|
|
*/
|
2013-04-16 20:23:02 +00:00
|
|
|
GST_DEBUG_OBJECT (downloader, "Waiting to fetch the URI %s", uri);
|
2014-02-04 11:52:25 +00:00
|
|
|
while (!downloader->priv->cancelled && !downloader->priv->download->completed)
|
|
|
|
g_cond_wait (&downloader->priv->cond, GST_OBJECT_GET_LOCK (downloader));
|
2013-04-16 22:47:02 +00:00
|
|
|
|
2013-04-16 20:23:02 +00:00
|
|
|
if (downloader->priv->cancelled) {
|
|
|
|
if (downloader->priv->download) {
|
|
|
|
g_object_unref (downloader->priv->download);
|
|
|
|
downloader->priv->download = NULL;
|
|
|
|
}
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
|
2013-04-16 22:47:02 +00:00
|
|
|
download = downloader->priv->download;
|
|
|
|
downloader->priv->download = NULL;
|
2014-03-07 15:22:10 +00:00
|
|
|
if (!downloader->priv->got_buffer) {
|
2015-08-14 08:44:24 +00:00
|
|
|
if (download->range_start < 0 && download->range_end < 0) {
|
|
|
|
/* HEAD request, so we don't expect a response */
|
|
|
|
} else {
|
|
|
|
g_object_unref (download);
|
|
|
|
download = NULL;
|
|
|
|
GST_ERROR_OBJECT (downloader, "Didn't retrieve a buffer before EOS");
|
|
|
|
}
|
2014-03-07 15:22:10 +00:00
|
|
|
}
|
2013-04-16 22:47:02 +00:00
|
|
|
|
|
|
|
if (download != NULL)
|
|
|
|
GST_INFO_OBJECT (downloader, "URI fetched successfully");
|
|
|
|
else
|
|
|
|
GST_INFO_OBJECT (downloader, "Error fetching URI");
|
|
|
|
|
|
|
|
quit:
|
|
|
|
{
|
2013-12-20 22:42:52 +00:00
|
|
|
if (downloader->priv->urisrc) {
|
|
|
|
GstPad *pad;
|
|
|
|
GstElement *urisrc;
|
|
|
|
|
2014-02-12 16:59:45 +00:00
|
|
|
urisrc = downloader->priv->urisrc;
|
|
|
|
|
2013-12-20 22:42:52 +00:00
|
|
|
GST_DEBUG_OBJECT (downloader, "Stopping source element %s",
|
2014-02-12 16:59:45 +00:00
|
|
|
GST_ELEMENT_NAME (urisrc));
|
2013-12-20 22:42:52 +00:00
|
|
|
|
|
|
|
/* remove the bus' sync handler */
|
|
|
|
gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL, NULL);
|
2014-02-13 09:06:48 +00:00
|
|
|
gst_bus_set_flushing (downloader->priv->bus, TRUE);
|
2014-02-12 16:59:45 +00:00
|
|
|
|
|
|
|
/* set the element state to NULL */
|
2014-02-12 17:06:43 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
2014-05-28 07:42:44 +00:00
|
|
|
if (download == NULL) {
|
2014-04-25 12:41:27 +00:00
|
|
|
gst_element_set_state (urisrc, GST_STATE_NULL);
|
2014-05-28 07:42:44 +00:00
|
|
|
} else {
|
|
|
|
GstQuery *query;
|
|
|
|
|
2019-09-02 19:08:44 +00:00
|
|
|
/* Download successful, let's query the URI */
|
2014-05-28 07:42:44 +00:00
|
|
|
query = gst_query_new_uri ();
|
|
|
|
if (gst_element_query (urisrc, query)) {
|
|
|
|
gst_query_parse_uri (query, &download->uri);
|
|
|
|
gst_query_parse_uri_redirection (query, &download->redirect_uri);
|
|
|
|
gst_query_parse_uri_redirection_permanent (query,
|
|
|
|
&download->redirect_permanent);
|
|
|
|
}
|
|
|
|
gst_query_unref (query);
|
2014-04-25 12:41:27 +00:00
|
|
|
gst_element_set_state (urisrc, GST_STATE_READY);
|
2014-05-28 07:42:44 +00:00
|
|
|
}
|
2014-02-12 17:06:43 +00:00
|
|
|
GST_OBJECT_LOCK (downloader);
|
2014-02-12 16:59:45 +00:00
|
|
|
gst_element_set_bus (urisrc, NULL);
|
|
|
|
|
2013-12-20 22:42:52 +00:00
|
|
|
/* unlink the source element from the internal pad */
|
|
|
|
pad = gst_pad_get_peer (downloader->priv->pad);
|
|
|
|
if (pad) {
|
|
|
|
gst_pad_unlink (pad, downloader->priv->pad);
|
|
|
|
gst_object_unref (pad);
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 09:06:48 +00:00
|
|
|
GST_OBJECT_UNLOCK (downloader);
|
2014-01-29 19:38:57 +00:00
|
|
|
|
2014-02-11 13:13:30 +00:00
|
|
|
if (download == NULL) {
|
|
|
|
if (!downloader->priv->err) {
|
|
|
|
g_set_error (err, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_OPEN_READ,
|
|
|
|
"Failed to download '%s'", uri);
|
|
|
|
} else {
|
|
|
|
g_propagate_error (err, downloader->priv->err);
|
2014-02-11 14:26:01 +00:00
|
|
|
downloader->priv->err = NULL;
|
2014-02-11 13:13:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:26:01 +00:00
|
|
|
downloader->priv->cancelled = FALSE;
|
|
|
|
|
2013-07-03 02:16:59 +00:00
|
|
|
g_mutex_unlock (&downloader->priv->download_lock);
|
2013-04-16 22:47:02 +00:00
|
|
|
return download;
|
|
|
|
}
|
|
|
|
}
|