mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-12-20 15:27:07 +00:00
dashdemux: refactor to use the uridownloader lib instead of internal copy
This commit is contained in:
parent
5efedbff0a
commit
770df6277a
9 changed files with 8 additions and 747 deletions
|
@ -4,21 +4,18 @@ plugin_LTLIBRARIES = libgstdashdemux.la
|
|||
libgstdashdemux_la_SOURCES = \
|
||||
gstmpdparser.c \
|
||||
gstdashdemux.c \
|
||||
gstfragment.c \
|
||||
gsturidownloader.c \
|
||||
gstplugin.c
|
||||
|
||||
# headers we need but don't want installed
|
||||
noinst_HEADERS = \
|
||||
gstmpdparser.h \
|
||||
gstfragmented.h \
|
||||
gstfragment.h \
|
||||
gstdashdemux.h \
|
||||
gsturidownloader.h
|
||||
noinst_HEADERS = \
|
||||
gstmpdparser.h \
|
||||
gstdashdemux.h
|
||||
|
||||
# compiler and linker flags used to compile this plugin, set in configure.ac
|
||||
libgstdashdemux_la_CFLAGS = $(GST_CFLAGS)
|
||||
libgstdashdemux_la_LIBADD = $(GST_LIBS) $(GST_BASE_LIBS)
|
||||
libgstdashdemux_la_CFLAGS = $(GST_PLUGINS_BAD_CFLAGS) $(GST_CFLAGS)
|
||||
libgstdashdemux_la_LIBADD = \
|
||||
$(top_builddir)/gst-libs/gst/uridownloader/libgsturidownloader-$(GST_MAJORMINOR).la \
|
||||
$(GST_LIBS) $(GST_BASE_LIBS)
|
||||
libgstdashdemux_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
|
||||
libgstdashdemux_la_LIBTOOLFLAGS = --tag=disable-static
|
||||
|
||||
|
|
|
@ -34,8 +34,7 @@
|
|||
#include <gst/base/gstadapter.h>
|
||||
#include <gst/base/gstdataqueue.h>
|
||||
#include "gstmpdparser.h"
|
||||
#include "gstfragmented.h"
|
||||
#include "gsturidownloader.h"
|
||||
#include <gst/uridownloader/gsturidownloader.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
#define GST_TYPE_DASH_DEMUX \
|
||||
|
|
|
@ -1,204 +0,0 @@
|
|||
/* 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <gst/base/gstadapter.h>
|
||||
#include "gstfragmented.h"
|
||||
#include "gstfragment.h"
|
||||
|
||||
#define GST_CAT_DEFAULT fragmented_debug
|
||||
|
||||
#define GST_FRAGMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_FRAGMENT, GstFragmentPrivate))
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_INDEX,
|
||||
PROP_NAME,
|
||||
PROP_DURATION,
|
||||
PROP_DISCONTINOUS,
|
||||
PROP_LAST
|
||||
};
|
||||
|
||||
struct _GstFragmentPrivate
|
||||
{
|
||||
GstAdapter *adapter;
|
||||
GstBuffer *buffer;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (GstFragment, gst_fragment, G_TYPE_OBJECT);
|
||||
|
||||
static void gst_fragment_dispose (GObject * object);
|
||||
static void gst_fragment_finalize (GObject * object);
|
||||
|
||||
static void
|
||||
gst_fragment_get_property (GObject * object,
|
||||
guint property_id, GValue * value, GParamSpec * pspec)
|
||||
{
|
||||
GstFragment *fragment = GST_FRAGMENT (object);
|
||||
|
||||
switch (property_id) {
|
||||
case PROP_INDEX:
|
||||
g_value_set_uint (value, fragment->index);
|
||||
break;
|
||||
|
||||
case PROP_NAME:
|
||||
g_value_set_string (value, fragment->name);
|
||||
break;
|
||||
|
||||
case PROP_DURATION:
|
||||
g_value_set_uint64 (value, fragment->stop_time - fragment->start_time);
|
||||
break;
|
||||
|
||||
case PROP_DISCONTINOUS:
|
||||
g_value_set_boolean (value, fragment->discontinuous);
|
||||
break;
|
||||
|
||||
default:
|
||||
/* We don't have any other property... */
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
gst_fragment_class_init (GstFragmentClass * klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
g_type_class_add_private (klass, sizeof (GstFragmentPrivate));
|
||||
|
||||
gobject_class->get_property = gst_fragment_get_property;
|
||||
gobject_class->dispose = gst_fragment_dispose;
|
||||
gobject_class->finalize = gst_fragment_finalize;
|
||||
|
||||
g_object_class_install_property (gobject_class, PROP_INDEX,
|
||||
g_param_spec_uint ("index", "Index", "Index of the fragment", 0,
|
||||
G_MAXUINT, 0, G_PARAM_READABLE));
|
||||
|
||||
g_object_class_install_property (gobject_class, PROP_NAME,
|
||||
g_param_spec_string ("name", "Name",
|
||||
"Name of the fragment (eg:fragment-12.ts)", NULL, G_PARAM_READABLE));
|
||||
|
||||
g_object_class_install_property (gobject_class, PROP_DISCONTINOUS,
|
||||
g_param_spec_boolean ("discontinuous", "Discontinous",
|
||||
"Whether this fragment has a discontinuity or not",
|
||||
FALSE, G_PARAM_READABLE));
|
||||
|
||||
g_object_class_install_property (gobject_class, PROP_DURATION,
|
||||
g_param_spec_uint64 ("duration", "Fragment duration",
|
||||
"Duration of the fragment", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
|
||||
}
|
||||
|
||||
static void
|
||||
gst_fragment_init (GstFragment * fragment)
|
||||
{
|
||||
GstFragmentPrivate *priv;
|
||||
|
||||
fragment->priv = priv = GST_FRAGMENT_GET_PRIVATE (fragment);
|
||||
|
||||
priv->adapter = gst_adapter_new ();
|
||||
fragment->download_start_time = gst_util_get_timestamp ();
|
||||
fragment->start_time = 0;
|
||||
fragment->stop_time = 0;
|
||||
fragment->index = 0;
|
||||
fragment->name = g_strdup ("");
|
||||
fragment->completed = FALSE;
|
||||
fragment->discontinuous = FALSE;
|
||||
}
|
||||
|
||||
GstFragment *
|
||||
gst_fragment_new (void)
|
||||
{
|
||||
return GST_FRAGMENT (g_object_new (GST_TYPE_FRAGMENT, NULL));
|
||||
}
|
||||
|
||||
static void
|
||||
gst_fragment_finalize (GObject * gobject)
|
||||
{
|
||||
GstFragment *fragment = GST_FRAGMENT (gobject);
|
||||
|
||||
g_free (fragment->name);
|
||||
|
||||
G_OBJECT_CLASS (gst_fragment_parent_class)->finalize (gobject);
|
||||
}
|
||||
|
||||
void
|
||||
gst_fragment_dispose (GObject * object)
|
||||
{
|
||||
GstFragmentPrivate *priv = GST_FRAGMENT (object)->priv;
|
||||
|
||||
if (priv->adapter) {
|
||||
gst_object_unref (priv->adapter);
|
||||
priv->adapter = NULL;
|
||||
}
|
||||
if (priv->buffer) {
|
||||
gst_buffer_unref (priv->buffer);
|
||||
priv->buffer = NULL;
|
||||
}
|
||||
|
||||
G_OBJECT_CLASS (gst_fragment_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
GstBuffer *
|
||||
gst_fragment_get_buffer (GstFragment * fragment)
|
||||
{
|
||||
g_return_val_if_fail (fragment != NULL, NULL);
|
||||
|
||||
if (!fragment->completed)
|
||||
return NULL;
|
||||
|
||||
if (!fragment->priv->buffer) {
|
||||
fragment->priv->buffer = gst_adapter_take_buffer (fragment->priv->adapter,
|
||||
gst_adapter_available (fragment->priv->adapter));
|
||||
}
|
||||
|
||||
return gst_buffer_ref (fragment->priv->buffer);
|
||||
}
|
||||
|
||||
gboolean
|
||||
gst_fragment_add_buffer (GstFragment * fragment, GstBuffer * buffer)
|
||||
{
|
||||
g_return_val_if_fail (fragment != NULL, FALSE);
|
||||
g_return_val_if_fail (buffer != NULL, FALSE);
|
||||
|
||||
if (fragment->completed) {
|
||||
GST_WARNING ("Fragment is completed, could not add more buffers");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
GST_DEBUG ("Adding new buffer to the fragment");
|
||||
/* We steal the buffers you pass in */
|
||||
gst_adapter_push (fragment->priv->adapter, buffer);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gsize
|
||||
gst_fragment_get_total_size (GstFragment * fragment)
|
||||
{
|
||||
g_return_val_if_fail (GST_IS_FRAGMENT (fragment), 0);
|
||||
|
||||
if (fragment->priv->buffer)
|
||||
return GST_BUFFER_SIZE (fragment->priv->buffer);
|
||||
|
||||
return gst_adapter_available (fragment->priv->adapter);
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
/* GStreamer
|
||||
* Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
|
||||
*
|
||||
* gstfragment.h:
|
||||
*
|
||||
* 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __GSTFRAGMENT_H__
|
||||
#define __GSTFRAGMENT_H__
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <gst/gst.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define GST_TYPE_FRAGMENT (gst_fragment_get_type())
|
||||
#define GST_FRAGMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FRAGMENT,GstFragment))
|
||||
#define GST_FRAGMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FRAGMENT,GstFragmentClass))
|
||||
#define GST_IS_FRAGMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FRAGMENT))
|
||||
#define GST_IS_FRAGMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FRAGMENT))
|
||||
|
||||
typedef struct _GstFragment GstFragment;
|
||||
typedef struct _GstFragmentPrivate GstFragmentPrivate;
|
||||
typedef struct _GstFragmentClass GstFragmentClass;
|
||||
|
||||
struct _GstFragment
|
||||
{
|
||||
GObject parent;
|
||||
|
||||
gchar * name; /* Name of the fragment */
|
||||
gboolean completed; /* Whether the fragment is complete or not */
|
||||
guint64 download_start_time; /* Epoch time when the download started */
|
||||
guint64 download_stop_time; /* Epoch time when the download finished */
|
||||
guint64 start_time; /* Start time of the fragment */
|
||||
guint64 stop_time; /* Stop time of the fragment */
|
||||
gboolean index; /* Index of the fragment */
|
||||
gboolean discontinuous; /* Whether this fragment is discontinuous or not */
|
||||
|
||||
GstFragmentPrivate *priv;
|
||||
};
|
||||
|
||||
struct _GstFragmentClass
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
};
|
||||
|
||||
GType gst_fragment_get_type (void);
|
||||
|
||||
GstBuffer * gst_fragment_get_buffer (GstFragment *fragment);
|
||||
gboolean gst_fragment_set_headers (GstFragment *fragment, GstBuffer **buffer, guint count);
|
||||
gboolean gst_fragment_add_buffer (GstFragment *fragment, GstBuffer *buffer);
|
||||
gsize gst_fragment_get_total_size (GstFragment * fragment);
|
||||
GstFragment * gst_fragment_new (void);
|
||||
|
||||
G_END_DECLS
|
||||
#endif /* __GSTFRAGMENT_H__ */
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
#ifndef __GST_FRAGMENTED_H__
|
||||
#define __GST_FRAGMENTED_H__
|
||||
|
||||
#include <gst/gst.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
GST_DEBUG_CATEGORY_EXTERN (fragmented_debug);
|
||||
|
||||
#define LOG_CAPS(obj, caps) GST_DEBUG_OBJECT (obj, "%s: %" GST_PTR_FORMAT, #caps, caps)
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GST_FRAGMENTED_H__ */
|
||||
|
||||
|
|
@ -29,11 +29,8 @@
|
|||
#include <string.h>
|
||||
#include <libxml/parser.h>
|
||||
#include <libxml/tree.h>
|
||||
#include "gstfragmented.h"
|
||||
#include "gstmpdparser.h"
|
||||
|
||||
#define GST_CAT_DEFAULT fragmented_debug
|
||||
|
||||
/* Property parsing */
|
||||
static gchar *gst_mpdparser_get_xml_prop_string (xmlNode * a_node,
|
||||
const gchar * property);
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
#include <gst/gst.h>
|
||||
|
||||
#include "gstfragmented.h"
|
||||
#include "gstdashdemux.h"
|
||||
|
||||
GST_DEBUG_CATEGORY (fragmented_debug);
|
||||
|
|
|
@ -1,374 +0,0 @@
|
|||
/* 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include "gstfragmented.h"
|
||||
#include "gstfragment.h"
|
||||
#include "gsturidownloader.h"
|
||||
|
||||
GST_DEBUG_CATEGORY_STATIC (uridownloader_debug);
|
||||
#define GST_CAT_DEFAULT (uridownloader_debug)
|
||||
|
||||
#define GST_URI_DOWNLOADER_GET_PRIVATE(obj) \
|
||||
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
|
||||
GST_TYPE_URI_DOWNLOADER, GstUriDownloaderPrivate))
|
||||
|
||||
struct _GstUriDownloaderPrivate
|
||||
{
|
||||
/* Fragments fetcher */
|
||||
GstElement *urisrc;
|
||||
GstBus *bus;
|
||||
GstPad *pad;
|
||||
GTimeVal *timeout;
|
||||
GstFragment *download;
|
||||
GMutex *lock;
|
||||
GCond *cond;
|
||||
gboolean cancelled;
|
||||
};
|
||||
|
||||
static void gst_uri_downloader_finalize (GObject * object);
|
||||
static void gst_uri_downloader_dispose (GObject * object);
|
||||
|
||||
static GstFlowReturn gst_uri_downloader_chain (GstPad * pad, GstBuffer * buf);
|
||||
static gboolean gst_uri_downloader_sink_event (GstPad * pad, GstEvent * event);
|
||||
static GstBusSyncReply gst_uri_downloader_bus_handler (GstBus * bus,
|
||||
GstMessage * message, gpointer data);
|
||||
|
||||
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,
|
||||
_do_init);
|
||||
|
||||
static void
|
||||
gst_uri_downloader_class_init (GstUriDownloaderClass * klass)
|
||||
{
|
||||
GObjectClass *gobject_class;
|
||||
|
||||
gobject_class = (GObjectClass *) klass;
|
||||
|
||||
g_type_class_add_private (klass, sizeof (GstUriDownloaderPrivate));
|
||||
|
||||
gobject_class->dispose = gst_uri_downloader_dispose;
|
||||
gobject_class->finalize = gst_uri_downloader_finalize;
|
||||
}
|
||||
|
||||
static void
|
||||
gst_uri_downloader_init (GstUriDownloader * downloader)
|
||||
{
|
||||
downloader->priv = GST_URI_DOWNLOADER_GET_PRIVATE (downloader);
|
||||
|
||||
/* 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 ();
|
||||
|
||||
downloader->priv->lock = g_mutex_new ();
|
||||
downloader->priv->cond = g_cond_new ();
|
||||
}
|
||||
|
||||
static void
|
||||
gst_uri_downloader_dispose (GObject * object)
|
||||
{
|
||||
GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
|
||||
|
||||
if (downloader->priv->urisrc != NULL) {
|
||||
gst_object_unref (downloader->priv->urisrc);
|
||||
downloader->priv->urisrc = NULL;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
G_OBJECT_CLASS (gst_uri_downloader_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
static void
|
||||
gst_uri_downloader_finalize (GObject * object)
|
||||
{
|
||||
GstUriDownloader *downloader = GST_URI_DOWNLOADER (object);
|
||||
|
||||
g_mutex_free (downloader->priv->lock);
|
||||
g_cond_free (downloader->priv->cond);
|
||||
|
||||
G_OBJECT_CLASS (gst_uri_downloader_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
GstUriDownloader *
|
||||
gst_uri_downloader_new (void)
|
||||
{
|
||||
return g_object_new (GST_TYPE_URI_DOWNLOADER, NULL);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_uri_downloader_sink_event (GstPad * pad, GstEvent * event)
|
||||
{
|
||||
gboolean ret = FALSE;
|
||||
GstUriDownloader *downloader;
|
||||
|
||||
downloader = GST_URI_DOWNLOADER (gst_pad_get_element_private (pad));
|
||||
|
||||
switch (event->type) {
|
||||
case GST_EVENT_EOS:{
|
||||
g_mutex_lock (downloader->priv->lock);
|
||||
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);
|
||||
}
|
||||
g_mutex_unlock (downloader->priv->lock);
|
||||
gst_event_unref (event);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ret = gst_pad_event_default (pad, event);
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static GstBusSyncReply
|
||||
gst_uri_downloader_bus_handler (GstBus * bus,
|
||||
GstMessage * message, gpointer data)
|
||||
{
|
||||
GstUriDownloader *downloader = (GstUriDownloader *) (data);
|
||||
|
||||
if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR ||
|
||||
GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING) {
|
||||
GError *err = NULL;
|
||||
gchar *dbg_info = NULL;
|
||||
|
||||
gst_message_parse_error (message, &err, &dbg_info);
|
||||
GST_WARNING_OBJECT (downloader,
|
||||
"Received error: %s from %s, the download will be cancelled",
|
||||
GST_OBJECT_NAME (message->src), err->message);
|
||||
GST_DEBUG ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
|
||||
g_error_free (err);
|
||||
g_free (dbg_info);
|
||||
|
||||
/* remove the sync handler to avoid duplicated messages */
|
||||
gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL);
|
||||
gst_uri_downloader_cancel (downloader);
|
||||
}
|
||||
|
||||
gst_message_unref (message);
|
||||
return GST_BUS_DROP;
|
||||
}
|
||||
|
||||
static GstFlowReturn
|
||||
gst_uri_downloader_chain (GstPad * pad, 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.
|
||||
*/
|
||||
g_mutex_lock (downloader->priv->lock);
|
||||
if (downloader->priv->download == NULL) {
|
||||
/* Download cancelled, quit */
|
||||
goto done;
|
||||
}
|
||||
|
||||
GST_LOG_OBJECT (downloader, "The uri fetcher received a new buffer "
|
||||
"of size %u", GST_BUFFER_SIZE (buf));
|
||||
if (!gst_fragment_add_buffer (downloader->priv->download, buf))
|
||||
GST_WARNING_OBJECT (downloader, "Could not add buffer to fragment");
|
||||
|
||||
done:
|
||||
{
|
||||
g_mutex_unlock (downloader->priv->lock);
|
||||
return GST_FLOW_OK;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
gst_uri_downloader_stop (GstUriDownloader * downloader)
|
||||
{
|
||||
GstPad *pad;
|
||||
|
||||
GST_DEBUG_OBJECT (downloader, "Stopping source element");
|
||||
|
||||
/* remove the bus' sync handler */
|
||||
gst_bus_set_sync_handler (downloader->priv->bus, NULL, NULL);
|
||||
/* 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);
|
||||
}
|
||||
/* set the element state to NULL */
|
||||
gst_element_set_state (downloader->priv->urisrc, GST_STATE_NULL);
|
||||
gst_element_get_state (downloader->priv->urisrc, NULL, NULL,
|
||||
GST_CLOCK_TIME_NONE);
|
||||
}
|
||||
|
||||
void
|
||||
gst_uri_downloader_reset (GstUriDownloader * downloader)
|
||||
{
|
||||
g_return_if_fail (downloader != NULL);
|
||||
|
||||
g_mutex_lock (downloader->priv->lock);
|
||||
downloader->priv->cancelled = FALSE;
|
||||
g_mutex_unlock (downloader->priv->lock);
|
||||
}
|
||||
|
||||
void
|
||||
gst_uri_downloader_cancel (GstUriDownloader * downloader)
|
||||
{
|
||||
g_return_if_fail (downloader != NULL);
|
||||
|
||||
g_mutex_lock (downloader->priv->lock);
|
||||
if (downloader->priv->download != NULL) {
|
||||
GST_DEBUG_OBJECT (downloader, "Cancelling download");
|
||||
g_object_unref (downloader->priv->download);
|
||||
downloader->priv->download = NULL;
|
||||
downloader->priv->cancelled = TRUE;
|
||||
GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
|
||||
g_cond_signal (downloader->priv->cond);
|
||||
g_mutex_unlock (downloader->priv->lock);
|
||||
} else {
|
||||
gboolean cancelled;
|
||||
|
||||
cancelled = downloader->priv->cancelled;
|
||||
downloader->priv->cancelled = TRUE;
|
||||
g_mutex_unlock (downloader->priv->lock);
|
||||
|
||||
if (cancelled)
|
||||
GST_DEBUG_OBJECT (downloader,
|
||||
"Trying to cancel a download that was already cancelled");
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gst_uri_downloader_set_uri (GstUriDownloader * downloader, const gchar * uri)
|
||||
{
|
||||
GstPad *pad;
|
||||
|
||||
g_return_val_if_fail (downloader != NULL, FALSE);
|
||||
|
||||
if (!gst_uri_is_valid (uri))
|
||||
return FALSE;
|
||||
|
||||
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);
|
||||
if (!downloader->priv->urisrc)
|
||||
return FALSE;
|
||||
|
||||
/* 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
GstFragment *
|
||||
gst_uri_downloader_fetch_uri (GstUriDownloader * downloader, const gchar * uri)
|
||||
{
|
||||
GstStateChangeReturn ret;
|
||||
GstFragment *download = NULL;
|
||||
|
||||
g_mutex_lock (downloader->priv->lock);
|
||||
|
||||
if (!gst_uri_downloader_set_uri (downloader, uri)) {
|
||||
goto quit;
|
||||
}
|
||||
|
||||
downloader->priv->download = gst_fragment_new ();
|
||||
|
||||
ret = gst_element_set_state (downloader->priv->urisrc, GST_STATE_PLAYING);
|
||||
if (ret == GST_STATE_CHANGE_FAILURE) {
|
||||
g_object_unref (downloader->priv->download);
|
||||
downloader->priv->download = NULL;
|
||||
goto quit;
|
||||
}
|
||||
|
||||
/* wait until:
|
||||
* - the download succeed (EOS in the src pad)
|
||||
* - the download failed (Error message on the fetcher bus)
|
||||
* - the download was canceled
|
||||
*/
|
||||
GST_DEBUG_OBJECT (downloader, "Waiting to fetch the URI %s", uri);
|
||||
if (downloader->priv->cancelled) {
|
||||
g_object_unref (downloader->priv->download);
|
||||
downloader->priv->download = NULL;
|
||||
goto quit;
|
||||
}
|
||||
g_cond_wait (downloader->priv->cond, downloader->priv->lock);
|
||||
|
||||
download = downloader->priv->download;
|
||||
downloader->priv->download = NULL;
|
||||
|
||||
if (download != NULL)
|
||||
GST_INFO_OBJECT (downloader, "URI %s fetched successfully", uri);
|
||||
else
|
||||
GST_INFO_OBJECT (downloader, "Error fetching URI %s", uri);
|
||||
|
||||
quit:
|
||||
{
|
||||
gst_uri_downloader_stop (downloader);
|
||||
g_mutex_unlock (downloader->priv->lock);
|
||||
return download;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
/* GStreamer
|
||||
* Copyright (C) 2011 Andoni Morales Alastruey <ylatuya@gmail.com>
|
||||
*
|
||||
* gsturidownloader.h:
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Youshould have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __GSTURI_DOWNLOADER_H__
|
||||
#define __GSTURI_DOWNLOADER_H__
|
||||
|
||||
#include <glib-object.h>
|
||||
#include <gst/gst.h>
|
||||
#include "gstfragment.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define GST_TYPE_URI_DOWNLOADER (gst_uri_downloader_get_type())
|
||||
#define GST_URI_DOWNLOADER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_URI_DOWNLOADER,GstUriDownloader))
|
||||
#define GST_URI_DOWNLOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_URI_DOWNLOADER,GstUriDownloaderClass))
|
||||
#define GST_IS_URI_DOWNLOADER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_URI_DOWNLOADER))
|
||||
#define GST_IS_URI_DOWNLOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_URI_DOWNLOADER))
|
||||
|
||||
typedef struct _GstUriDownloader GstUriDownloader;
|
||||
typedef struct _GstUriDownloaderPrivate GstUriDownloaderPrivate;
|
||||
typedef struct _GstUriDownloaderClass GstUriDownloaderClass;
|
||||
|
||||
struct _GstUriDownloader
|
||||
{
|
||||
GstObject parent;
|
||||
|
||||
GstUriDownloaderPrivate *priv;
|
||||
};
|
||||
|
||||
struct _GstUriDownloaderClass
|
||||
{
|
||||
GstObjectClass parent_class;
|
||||
|
||||
/*< private >*/
|
||||
gpointer _gst_reserved[GST_PADDING];
|
||||
};
|
||||
|
||||
GType gst_uri_downloader_get_type (void);
|
||||
|
||||
GstUriDownloader * gst_uri_downloader_new (void);
|
||||
GstFragment * gst_uri_downloader_fetch_uri (GstUriDownloader * downloader, const gchar * uri);
|
||||
void gst_uri_downloader_reset (GstUriDownloader *downloader);
|
||||
void gst_uri_downloader_cancel (GstUriDownloader *downloader);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GSTURIDOWNLOADER_H__ */
|
||||
|
Loading…
Reference in a new issue