/* GStreamer
 *
 * Copyright (C) 2006 Lutz Mueller <lutz@topfrose.de>
 * 
 * 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 "gsturilistdec.h"

GST_DEBUG_CATEGORY_STATIC (urilist_dec_debug);
#define GST_CAT_DEFAULT urilist_dec_debug

static GstElementDetails urilist_dec_details = GST_ELEMENT_DETAILS (
    "URI list decoder", "Decoder/URI list",
    "Decode URI lists", "Lutz Mueller <lutz@topfrose.de>");

struct _GstUrilistDec
{
  GstBin parent;

  GstPad *snk;
};

struct _GstUrilistDecClass
{
  GstBinClass parent_class;
};

GST_BOILERPLATE (GstUrilistDec, gst_urilist_dec, GstBin, GST_TYPE_BIN);

static GstStaticPadTemplate tsink =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("text/uri-list"));

static GstStaticPadTemplate tsrc =
GST_STATIC_PAD_TEMPLATE ("src_%d", GST_PAD_SRC, GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

static gboolean
gst_urilist_dec_event (GstPad * pad, GstEvent * event)
{
  GstUrilistDec *dec = GST_URILIST_DEC (GST_PAD_PARENT (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_EOS:
      GST_DEBUG_OBJECT (dec, "Got EOS.");
      gst_element_no_more_pads (GST_ELEMENT (dec));
      gst_event_unref (event);
      return TRUE;
    default:
      return gst_pad_event_default (pad, event);
  }
}

static GstFlowReturn
gst_urilist_dec_chain (GstPad * pad, GstBuffer * buf)
{
  GstUrilistDec *dec = GST_URILIST_DEC (GST_PAD_PARENT (pad));
  gchar **list, **p, *str;

  str = g_strndup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
  list = g_strsplit (str, "\n", G_MAXINT);
  g_free (str);
  gst_buffer_unref (buf);
  for (p = list; p && *p; p++) {
    GstElement *e;
    g_message ("URI: %s", *p);

    e = gst_element_make_from_uri (GST_URI_SRC, *p, NULL);
    if (!e) {
      GST_DEBUG_OBJECT (dec, "Could not create element for URI '%s'.", *p);
      g_strfreev (list);
      return GST_FLOW_ERROR;
    }
    pad = gst_element_get_pad (e, "src");
    gst_bin_add (GST_BIN (dec), e);
    gst_element_add_pad (GST_ELEMENT (dec),
        gst_ghost_pad_new (*p, pad));
    gst_object_unref (GST_OBJECT (pad));
    if (!gst_element_sync_state_with_parent (e))
      GST_DEBUG_OBJECT (dec, "Could not synchronize state.");
  }
  g_strfreev (list);
  return GST_FLOW_OK;
}

static void
gst_urilist_dec_init (GstUrilistDec * dec, GstUrilistDecClass * klass)
{
  dec->snk = gst_pad_new_from_template (
      gst_static_pad_template_get (&tsink), "sink");
  gst_element_add_pad (GST_ELEMENT (dec), dec->snk);
  gst_pad_set_chain_function (dec->snk, gst_urilist_dec_chain);
  gst_pad_set_event_function (dec->snk, gst_urilist_dec_event);
}

static void
gst_urilist_dec_base_init (gpointer g_class)
{
  GstElementClass *ec = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&tsink));
  gst_element_class_add_pad_template (ec, gst_static_pad_template_get (&tsrc));
  gst_element_class_set_details (ec, &urilist_dec_details);
}

static void
gst_urilist_dec_class_init (GstUrilistDecClass * klass)
{
  GST_DEBUG_CATEGORY_INIT (urilist_dec_debug, "urilistdec", 0, "Decoder for URI lists");
}
