Blame gst/encoding/gststreamcombiner.c

Packit 971217
/* GStreamer Stream Combiner
Packit 971217
 * Copyright (C) 2010 Edward Hervey <edward.hervey@collabora.co.uk>
Packit 971217
 *           (C) 2009 Nokia Corporation
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include "gststreamcombiner.h"
Packit 971217
#include "gststreamcombinerpad.h"
Packit 971217
Packit 971217
static GstStaticPadTemplate src_template =
Packit 971217
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
Packit 971217
    GST_STATIC_CAPS_ANY);
Packit 971217
Packit 971217
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
Packit 971217
    GST_PAD_SINK,
Packit 971217
    GST_PAD_REQUEST,
Packit 971217
    GST_STATIC_CAPS_ANY);
Packit 971217
Packit 971217
GST_DEBUG_CATEGORY_STATIC (gst_stream_combiner_debug);
Packit 971217
#define GST_CAT_DEFAULT gst_stream_combiner_debug
Packit 971217
Packit 971217
G_DEFINE_TYPE (GstStreamCombiner, gst_stream_combiner, GST_TYPE_ELEMENT);
Packit 971217
Packit 971217
G_DEFINE_TYPE (GstStreamCombinerPad, gst_stream_combiner_pad, GST_TYPE_PAD);
Packit 971217
Packit 971217
#define STREAMS_LOCK(obj) (g_mutex_lock(&obj->lock))
Packit 971217
#define STREAMS_UNLOCK(obj) (g_mutex_unlock(&obj->lock))
Packit 971217
Packit 971217
static void gst_stream_combiner_finalize (GObject * object);
Packit 971217
Packit 971217
static GstPad *gst_stream_combiner_request_new_pad (GstElement * element,
Packit 971217
    GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
Packit 971217
static void gst_stream_combiner_release_pad (GstElement * element,
Packit 971217
    GstPad * pad);
Packit 971217
Packit 971217
static void
Packit 971217
gst_stream_combiner_pad_class_init (GstStreamCombinerPadClass * klass)
Packit 971217
{
Packit 971217
  return;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_stream_combiner_pad_init (GstStreamCombinerPad * mixerpad)
Packit 971217
{
Packit 971217
  mixerpad->is_eos = FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_stream_combiner_class_init (GstStreamCombinerClass * klass)
Packit 971217
{
Packit 971217
  GObjectClass *gobject_klass;
Packit 971217
  GstElementClass *gstelement_klass;
Packit 971217
Packit 971217
  gobject_klass = (GObjectClass *) klass;
Packit 971217
  gstelement_klass = (GstElementClass *) klass;
Packit 971217
Packit 971217
  gobject_klass->finalize = gst_stream_combiner_finalize;
Packit 971217
Packit 971217
  GST_DEBUG_CATEGORY_INIT (gst_stream_combiner_debug, "streamcombiner", 0,
Packit 971217
      "Stream Combiner");
Packit 971217
Packit 971217
  gst_element_class_add_static_pad_template (gstelement_klass, &src_template);
Packit 971217
  gst_element_class_add_static_pad_template (gstelement_klass, &sink_template);
Packit 971217
Packit 971217
  gstelement_klass->request_new_pad =
Packit 971217
      GST_DEBUG_FUNCPTR (gst_stream_combiner_request_new_pad);
Packit 971217
  gstelement_klass->release_pad =
Packit 971217
      GST_DEBUG_FUNCPTR (gst_stream_combiner_release_pad);
Packit 971217
Packit 971217
  gst_element_class_set_static_metadata (gstelement_klass,
Packit 971217
      "streamcombiner", "Generic",
Packit 971217
      "Recombines streams splitted by the streamsplitter element",
Packit 971217
      "Edward Hervey <edward.hervey@collabora.co.uk>");
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_stream_combiner_finalize (GObject * object)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) object;
Packit 971217
Packit 971217
  g_mutex_clear (&stream_combiner->lock);
Packit 971217
Packit 971217
  G_OBJECT_CLASS (gst_stream_combiner_parent_class)->finalize (object);
Packit 971217
}
Packit 971217
Packit 971217
static GstFlowReturn
Packit 971217
gst_stream_combiner_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
Packit 971217
  /* FIXME : IMPLEMENT */
Packit 971217
Packit 971217
  /* with lock taken, check if we're the active stream, if not drop */
Packit 971217
  return gst_pad_push (stream_combiner->srcpad, buf);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
_all_sink_pads_eos (GstStreamCombiner * combiner)
Packit 971217
{
Packit 971217
  GList *tmp;
Packit 971217
Packit 971217
  for (tmp = combiner->sinkpads; tmp; tmp = tmp->next) {
Packit 971217
    if (!(GST_STREAM_COMBINER_PAD (tmp->data))->is_eos)
Packit 971217
      return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_stream_combiner_sink_event (GstPad * pad, GstObject * parent,
Packit 971217
    GstEvent * event)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
Packit 971217
  GstStreamCombinerPad *combiner_pad = GST_STREAM_COMBINER_PAD (pad);
Packit 971217
  /* FIXME : IMPLEMENT */
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (pad, "Got event %s", GST_EVENT_TYPE_NAME (event));
Packit 971217
Packit 971217
  switch (GST_EVENT_TYPE (event)) {
Packit 971217
    case GST_EVENT_EOS:
Packit 971217
      STREAMS_LOCK (stream_combiner);
Packit 971217
      combiner_pad->is_eos = TRUE;
Packit 971217
      if (!_all_sink_pads_eos (stream_combiner)) {
Packit 971217
        gst_event_unref (event);
Packit 971217
        event = NULL;
Packit 971217
      } else {
Packit 971217
        GST_DEBUG_OBJECT (stream_combiner, "All sink pads eos, pushing eos");
Packit 971217
      }
Packit 971217
      STREAMS_UNLOCK (stream_combiner);
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  /* SEGMENT : lock, wait for other stream to EOS, select stream, unlock, push */
Packit 971217
  /* FLUSH_START : lock, mark as flushing, unlock. if wasn't flushing forward */
Packit 971217
  /* FLUSH_STOP : lock, unmark as flushing, unlock, if was flushing forward */
Packit 971217
  /* OTHER : if selected pad forward */
Packit 971217
  if (event)
Packit 971217
    return gst_pad_push_event (stream_combiner->srcpad, event);
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_stream_combiner_sink_query (GstPad * pad, GstObject * parent,
Packit 971217
    GstQuery * query)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
Packit 971217
Packit 971217
  return gst_pad_peer_query (stream_combiner->srcpad, query);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_stream_combiner_src_event (GstPad * pad, GstObject * parent,
Packit 971217
    GstEvent * event)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
Packit 971217
  GstPad *sinkpad = NULL;
Packit 971217
Packit 971217
  STREAMS_LOCK (stream_combiner);
Packit 971217
  if (stream_combiner->current)
Packit 971217
    sinkpad = stream_combiner->current;
Packit 971217
  else if (stream_combiner->sinkpads)
Packit 971217
    sinkpad = (GstPad *) stream_combiner->sinkpads->data;
Packit 971217
  STREAMS_UNLOCK (stream_combiner);
Packit 971217
Packit 971217
  if (sinkpad)
Packit 971217
    /* Forward upstream as is */
Packit 971217
    return gst_pad_push_event (sinkpad, event);
Packit 971217
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_stream_combiner_src_query (GstPad * pad, GstObject * parent,
Packit 971217
    GstQuery * query)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
Packit 971217
  GstPad *sinkpad = NULL;
Packit 971217
  gboolean ret = FALSE;
Packit 971217
Packit 971217
  switch (GST_QUERY_TYPE (query)) {
Packit 971217
    case GST_QUERY_CAPS:
Packit 971217
      ret = gst_pad_query_default (pad, parent, query);
Packit 971217
      break;
Packit 971217
    default:
Packit 971217
      STREAMS_LOCK (stream_combiner);
Packit 971217
      if (stream_combiner->current)
Packit 971217
        sinkpad = stream_combiner->current;
Packit 971217
      else if (stream_combiner->sinkpads)
Packit 971217
        sinkpad = (GstPad *) stream_combiner->sinkpads->data;
Packit 971217
      STREAMS_UNLOCK (stream_combiner);
Packit 971217
Packit 971217
      if (sinkpad)
Packit 971217
        /* Forward upstream as is */
Packit 971217
        ret = gst_pad_peer_query (sinkpad, query);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
  return ret;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_stream_combiner_init (GstStreamCombiner * stream_combiner)
Packit 971217
{
Packit 971217
  stream_combiner->srcpad =
Packit 971217
      gst_pad_new_from_static_template (&src_template, "src");
Packit 971217
  gst_pad_set_event_function (stream_combiner->srcpad,
Packit 971217
      gst_stream_combiner_src_event);
Packit 971217
  gst_pad_set_query_function (stream_combiner->srcpad,
Packit 971217
      gst_stream_combiner_src_query);
Packit 971217
  gst_element_add_pad (GST_ELEMENT (stream_combiner), stream_combiner->srcpad);
Packit 971217
Packit 971217
  g_mutex_init (&stream_combiner->lock);
Packit 971217
}
Packit 971217
Packit 971217
static GstPad *
Packit 971217
gst_stream_combiner_request_new_pad (GstElement * element,
Packit 971217
    GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
Packit 971217
  GstStreamCombinerPad *combiner_pad;
Packit 971217
  GstPad *sinkpad;
Packit 971217
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
Packit 971217
  GstPadTemplate *template =
Packit 971217
      gst_element_class_get_pad_template (klass, "sink_%u");
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (element, "templ:%p, name:%s", templ, name);
Packit 971217
Packit 971217
  combiner_pad = g_object_new (GST_TYPE_STREAM_COMBINER_PAD, "name", name,
Packit 971217
      "template", template, "direction", template->direction, NULL);
Packit 971217
Packit 971217
  sinkpad = GST_PAD_CAST (combiner_pad);
Packit 971217
  gst_pad_set_chain_function (sinkpad, gst_stream_combiner_chain);
Packit 971217
  gst_pad_set_event_function (sinkpad, gst_stream_combiner_sink_event);
Packit 971217
  gst_pad_set_query_function (sinkpad, gst_stream_combiner_sink_query);
Packit 971217
Packit 971217
  STREAMS_LOCK (stream_combiner);
Packit 971217
  stream_combiner->sinkpads =
Packit 971217
      g_list_append (stream_combiner->sinkpads, sinkpad);
Packit 971217
  gst_pad_set_active (sinkpad, TRUE);
Packit 971217
  gst_element_add_pad (element, sinkpad);
Packit 971217
  stream_combiner->cookie++;
Packit 971217
  STREAMS_UNLOCK (stream_combiner);
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (element, "Returning pad %p", sinkpad);
Packit 971217
Packit 971217
  return sinkpad;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_stream_combiner_release_pad (GstElement * element, GstPad * pad)
Packit 971217
{
Packit 971217
  GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
Packit 971217
  GList *tmp;
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
Packit 971217
Packit 971217
  STREAMS_LOCK (stream_combiner);
Packit 971217
  tmp = g_list_find (stream_combiner->sinkpads, pad);
Packit 971217
  if (tmp) {
Packit 971217
    GstPad *pad = (GstPad *) tmp->data;
Packit 971217
Packit 971217
    stream_combiner->sinkpads =
Packit 971217
        g_list_delete_link (stream_combiner->sinkpads, tmp);
Packit 971217
    stream_combiner->cookie++;
Packit 971217
Packit 971217
    if (pad == stream_combiner->current) {
Packit 971217
      /* Deactivate current flow */
Packit 971217
      GST_DEBUG_OBJECT (element, "Removed pad was the current one");
Packit 971217
      stream_combiner->current = NULL;
Packit 971217
    }
Packit 971217
    GST_DEBUG_OBJECT (element, "Removing pad from ourself");
Packit 971217
    gst_element_remove_pad (element, pad);
Packit 971217
  }
Packit 971217
  STREAMS_UNLOCK (stream_combiner);
Packit 971217
Packit 971217
  return;
Packit 971217
}