uridownloader: refactor gsturidownloader to its own lib
authorThiago Santos <thiago.sousa.santos@collabora.com>
Tue, 16 Apr 2013 22:47:02 +0000 (19:47 -0300)
committerThiago Santos <thiago.sousa.santos@collabora.com>
Tue, 7 May 2013 23:02:41 +0000 (20:02 -0300)
gsturidownloader can be reused by other plugins, better have it
into its own lib

configure.ac
gst-libs/gst/Makefile.am
gst-libs/gst/uridownloader/Makefile.am [new file with mode: 0644]
gst-libs/gst/uridownloader/gstfragment.c [new file with mode: 0644]
gst-libs/gst/uridownloader/gstfragment.h [new file with mode: 0644]
gst-libs/gst/uridownloader/gsturidownloader.c [new file with mode: 0644]
gst-libs/gst/uridownloader/gsturidownloader.h [new file with mode: 0644]
gst-libs/gst/uridownloader/gsturidownloader_debug.h [new file with mode: 0644]

index 6e0a5e8..1d31c1e 100644 (file)
@@ -2404,6 +2404,7 @@ gst-libs/gst/insertbin/Makefile
 gst-libs/gst/interfaces/Makefile
 gst-libs/gst/signalprocessor/Makefile
 gst-libs/gst/codecparsers/Makefile
+gst-libs/gst/uridownloader/Makefile
 sys/Makefile
 sys/dshowdecwrapper/Makefile
 sys/acmenc/Makefile
index 9af1a16..ed96f52 100644 (file)
@@ -3,9 +3,8 @@ EGL_DIR = egl
 endif
 
 SUBDIRS = interfaces signalprocessor basecamerabinsrc codecparsers \
-        insertbin $(EGL_DIR)
+        insertbin uridownloader $(EGL_DIR)
 
 noinst_HEADERS = gst-i18n-plugin.h gettext.h glib-compat-private.h
 DIST_SUBDIRS = interfaces egl signalprocessor basecamerabinsrc codecparsers \
-       insertbin
-
+       insertbin uridownloader
diff --git a/gst-libs/gst/uridownloader/Makefile.am b/gst-libs/gst/uridownloader/Makefile.am
new file mode 100644 (file)
index 0000000..a63f2ac
--- /dev/null
@@ -0,0 +1,40 @@
+lib_LTLIBRARIES = libgsturidownloader-@GST_API_VERSION@.la
+
+libgsturidownloader_@GST_API_VERSION@_la_SOURCES = \
+       gstfragment.c gsturidownloader.c
+
+libgsturidownloader_@GST_API_VERSION@includedir = \
+       $(includedir)/gstreamer-@GST_API_VERSION@/gst/uridownloader
+
+libgsturidownloader_@GST_API_VERSION@include_HEADERS = \
+       gstfragment.h gsturidownloader.h gsturidownloader_debug.h
+
+libgsturidownloader_@GST_API_VERSION@_la_CFLAGS = \
+       $(GST_PLUGINS_BAD_CFLAGS) \
+       -DGST_USE_UNSTABLE_API \
+       $(GST_CFLAGS)
+
+libgsturidownloader_@GST_API_VERSION@_la_LIBADD = \
+       $(GST_BASE_LIBS) \
+       $(GST_LIBS)
+
+libgsturidownloader_@GST_API_VERSION@_la_LDFLAGS = \
+       $(GST_LIB_LDFLAGS) \
+       $(GST_ALL_LDFLAGS) \
+       $(GST_LT_LDFLAGS)
+
+Android.mk:  $(BUILT_SOURCES) Makefile.am
+       androgenizer -:PROJECT libgsturidownloader -:STATIC libgsturidownloader-@GST_API_VERSION@ \
+        -:TAGS eng debug \
+         -:REL_TOP $(top_srcdir) -:ABS_TOP $(abs_top_srcdir) \
+        -:SOURCES $(libgsturidownloader_@GST_API_VERSION@_la_SOURCES) \
+         $(built_sources) \
+        -:CFLAGS $(DEFS) $(libgsturidownloader_@GST_API_VERSION@_la_CFLAGS) \
+        -:LDFLAGS $(libgsturidownloader_@GST_API_VERSION@_la_LDFLAGS) \
+                  $(libgsturidownloader@GST_API_VERSION@_la_LIBADD) \
+                  -ldl \
+        -:HEADER_TARGET gstreamer-@GST_API_VERSION@/gst/uridownloader \
+        -:HEADERS $(libgsturidownloaderinclude_HEADERS) \
+         $(built_headers) \
+        -:PASSTHROUGH LOCAL_ARM_MODE:=arm \
+       > $@
diff --git a/gst-libs/gst/uridownloader/gstfragment.c b/gst-libs/gst/uridownloader/gstfragment.c
new file mode 100644 (file)
index 0000000..452456a
--- /dev/null
@@ -0,0 +1,265 @@
+/* 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.
+ */
+
+#include <glib.h>
+#include <gst/base/gsttypefindhelper.h>
+#include <gst/base/gstadapter.h>
+#include "gstfragment.h"
+#include "gsturidownloader_debug.h"
+
+#define GST_CAT_DEFAULT uridownloader_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_BUFFER,
+  PROP_CAPS,
+  PROP_LAST
+};
+
+struct _GstFragmentPrivate
+{
+  GstBuffer *buffer;
+  GstCaps *caps;
+  GMutex lock;
+};
+
+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_set_property (GObject * object,
+    guint property_id, const GValue * value, GParamSpec * pspec)
+{
+  GstFragment *fragment = GST_FRAGMENT (object);
+
+  switch (property_id) {
+    case PROP_CAPS:
+      gst_fragment_set_caps (fragment, g_value_get_boxed (value));
+      break;
+
+    default:
+      /* We don't have any other property... */
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+  }
+}
+
+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;
+
+    case PROP_BUFFER:
+      g_value_set_boxed (value, gst_fragment_get_buffer (fragment));
+      break;
+
+    case PROP_CAPS:
+      g_value_set_boxed (value, gst_fragment_get_caps (fragment));
+      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->set_property = gst_fragment_set_property;
+  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));
+
+  g_object_class_install_property (gobject_class, PROP_BUFFER,
+      g_param_spec_boxed ("buffer", "Buffer",
+          "The fragment's buffer", GST_TYPE_BUFFER,
+          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_CAPS,
+      g_param_spec_boxed ("caps", "Fragment caps",
+          "The caps of the fragment's buffer. (NULL = detect)", GST_TYPE_CAPS,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
+
+static void
+gst_fragment_init (GstFragment * fragment)
+{
+  GstFragmentPrivate *priv;
+
+  fragment->priv = priv = GST_FRAGMENT_GET_PRIVATE (fragment);
+
+  g_mutex_init (&fragment->priv->lock);
+  priv->buffer = NULL;
+  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_mutex_clear (&fragment->priv->lock);
+
+  G_OBJECT_CLASS (gst_fragment_parent_class)->finalize (gobject);
+}
+
+void
+gst_fragment_dispose (GObject * object)
+{
+  GstFragmentPrivate *priv = GST_FRAGMENT (object)->priv;
+
+  if (priv->buffer != NULL) {
+    gst_buffer_unref (priv->buffer);
+    priv->buffer = NULL;
+  }
+
+  if (priv->caps != NULL) {
+    gst_caps_unref (priv->caps);
+    priv->caps = 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;
+
+  gst_buffer_ref (fragment->priv->buffer);
+  return fragment->priv->buffer;
+}
+
+void
+gst_fragment_set_caps (GstFragment * fragment, GstCaps * caps)
+{
+  g_return_if_fail (fragment != NULL);
+
+  g_mutex_lock (&fragment->priv->lock);
+  gst_caps_replace (&fragment->priv->caps, caps);
+  g_mutex_unlock (&fragment->priv->lock);
+}
+
+GstCaps *
+gst_fragment_get_caps (GstFragment * fragment)
+{
+  g_return_val_if_fail (fragment != NULL, NULL);
+
+  if (!fragment->completed)
+    return NULL;
+
+  g_mutex_lock (&fragment->priv->lock);
+  if (fragment->priv->caps == NULL)
+    fragment->priv->caps =
+        gst_type_find_helper_for_buffer (NULL, fragment->priv->buffer, NULL);
+  gst_caps_ref (fragment->priv->caps);
+  g_mutex_unlock (&fragment->priv->lock);
+
+  return fragment->priv->caps;
+}
+
+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 */
+  if (fragment->priv->buffer == NULL)
+    fragment->priv->buffer = buffer;
+  else
+    fragment->priv->buffer = gst_buffer_append (fragment->priv->buffer, buffer);
+  return TRUE;
+}
diff --git a/gst-libs/gst/uridownloader/gstfragment.h b/gst-libs/gst/uridownloader/gstfragment.h
new file mode 100644 (file)
index 0000000..d7292b4
--- /dev/null
@@ -0,0 +1,70 @@
+/* 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., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, 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);
+void gst_fragment_set_caps (GstFragment * fragment, GstCaps * caps);
+GstCaps * gst_fragment_get_caps (GstFragment * fragment);
+gboolean gst_fragment_add_buffer (GstFragment *fragment, GstBuffer *buffer);
+GstFragment * gst_fragment_new (void);
+
+G_END_DECLS
+#endif /* __GSTFRAGMENT_H__ */
diff --git a/gst-libs/gst/uridownloader/gsturidownloader.c b/gst-libs/gst/uridownloader/gsturidownloader.c
new file mode 100644 (file)
index 0000000..5384141
--- /dev/null
@@ -0,0 +1,359 @@
+/* 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.
+ */
+
+#include <glib.h>
+#include "gstfragment.h"
+#include "gsturidownloader.h"
+#include "gsturidownloader_debug.h"
+
+#define GST_CAT_DEFAULT uridownloader_debug
+GST_DEBUG_CATEGORY (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;
+};
+
+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);
+
+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 ();
+
+  g_mutex_init (&downloader->priv->lock);
+  g_cond_init (&downloader->priv->cond);
+}
+
+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_clear (&downloader->priv->lock);
+  g_cond_clear (&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, 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_OBJECT_UNLOCK (downloader);
+        GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
+        g_mutex_lock (&downloader->priv->lock);
+        g_cond_signal (&downloader->priv->cond);
+        g_mutex_unlock (&downloader->priv->lock);
+      } else {
+        GST_OBJECT_UNLOCK (downloader);
+      }
+      gst_event_unref (event);
+      break;
+    }
+    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);
+
+  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, NULL);
+    gst_uri_downloader_cancel (downloader);
+  }
+
+  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 */
+    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));
+  if (!gst_fragment_add_buffer (downloader->priv->download, buf))
+    GST_WARNING_OBJECT (downloader, "Could not add buffer to fragment");
+  GST_OBJECT_UNLOCK (downloader);
+
+done:
+  {
+    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, 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_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;
+    GST_OBJECT_UNLOCK (downloader);
+    GST_DEBUG_OBJECT (downloader, "Signaling chain funtion");
+    g_mutex_lock (&downloader->priv->lock);
+    g_cond_signal (&downloader->priv->cond);
+    g_mutex_unlock (&downloader->priv->lock);
+  } else {
+    GST_OBJECT_UNLOCK (downloader);
+    GST_DEBUG_OBJECT (downloader,
+        "Trying to cancell a download that was alredy cancelled");
+  }
+}
+
+static gboolean
+gst_uri_downloader_set_uri (GstUriDownloader * downloader, const gchar * uri)
+{
+  GstPad *pad;
+
+  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, 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, 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;
+}
+
+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");
+  g_cond_wait (&downloader->priv->cond, &downloader->priv->lock);
+
+  GST_OBJECT_LOCK (downloader);
+  download = downloader->priv->download;
+  downloader->priv->download = NULL;
+  GST_OBJECT_UNLOCK (downloader);
+
+  if (download != NULL)
+    GST_INFO_OBJECT (downloader, "URI fetched successfully");
+  else
+    GST_INFO_OBJECT (downloader, "Error fetching URI");
+
+quit:
+  {
+    gst_uri_downloader_stop (downloader);
+    g_mutex_unlock (&downloader->priv->lock);
+    return download;
+  }
+}
diff --git a/gst-libs/gst/uridownloader/gsturidownloader.h b/gst-libs/gst/uridownloader/gsturidownloader.h
new file mode 100644 (file)
index 0000000..8c1a6ec
--- /dev/null
@@ -0,0 +1,64 @@
+/* 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., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, 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_cancel (GstUriDownloader *downloader);
+void gst_uri_downloader_free (GstUriDownloader *downloader);
+
+G_END_DECLS
+#endif /* __GSTURIDOWNLOADER_H__ */
diff --git a/gst-libs/gst/uridownloader/gsturidownloader_debug.h b/gst-libs/gst/uridownloader/gsturidownloader_debug.h
new file mode 100644 (file)
index 0000000..f7d36c0
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef __GST_URIDOWNLOADER_DEBUG_H__
+#define __GST_URIDOWNLOADER_DEBUG_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+
+GST_DEBUG_CATEGORY_EXTERN (uridownloader_debug);
+
+G_END_DECLS
+
+#endif /* __GST_URIDOWNLOADER_DEBUG_H__ */