2010-08-13 15:36:38 +00:00
|
|
|
/* GStreamer Stream Combiner
|
|
|
|
* Copyright (C) 2010 Edward Hervey <edward.hervey@collabora.co.uk>
|
|
|
|
* (C) 2009 Nokia Corporation
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-08-13 15:36:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gststreamcombiner.h"
|
2013-07-21 22:48:54 +00:00
|
|
|
#include "gststreamcombinerpad.h"
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
2011-11-04 09:48:50 +00:00
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
|
2010-08-13 15:36:38 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_stream_combiner_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_stream_combiner_debug
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstStreamCombiner, gst_stream_combiner, GST_TYPE_ELEMENT);
|
|
|
|
|
2013-07-21 22:48:54 +00:00
|
|
|
G_DEFINE_TYPE (GstStreamCombinerPad, gst_stream_combiner_pad, GST_TYPE_PAD);
|
|
|
|
|
2012-01-18 17:21:36 +00:00
|
|
|
#define STREAMS_LOCK(obj) (g_mutex_lock(&obj->lock))
|
|
|
|
#define STREAMS_UNLOCK(obj) (g_mutex_unlock(&obj->lock))
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2012-01-18 17:21:36 +00:00
|
|
|
static void gst_stream_combiner_finalize (GObject * object);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
static GstPad *gst_stream_combiner_request_new_pad (GstElement * element,
|
2011-05-10 14:44:37 +00:00
|
|
|
GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
|
2010-08-13 15:36:38 +00:00
|
|
|
static void gst_stream_combiner_release_pad (GstElement * element,
|
|
|
|
GstPad * pad);
|
|
|
|
|
2013-07-21 22:48:54 +00:00
|
|
|
static void
|
|
|
|
gst_stream_combiner_pad_class_init (GstStreamCombinerPadClass * klass)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_stream_combiner_pad_init (GstStreamCombinerPad * mixerpad)
|
|
|
|
{
|
|
|
|
mixerpad->is_eos = FALSE;
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
static void
|
|
|
|
gst_stream_combiner_class_init (GstStreamCombinerClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_klass;
|
|
|
|
GstElementClass *gstelement_klass;
|
|
|
|
|
|
|
|
gobject_klass = (GObjectClass *) klass;
|
|
|
|
gstelement_klass = (GstElementClass *) klass;
|
|
|
|
|
2012-01-18 17:21:36 +00:00
|
|
|
gobject_klass->finalize = gst_stream_combiner_finalize;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_stream_combiner_debug, "streamcombiner", 0,
|
|
|
|
"Stream Combiner");
|
|
|
|
|
2016-03-03 07:46:24 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_klass, &src_template);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_klass, &sink_template);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
gstelement_klass->request_new_pad =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_combiner_request_new_pad);
|
|
|
|
gstelement_klass->release_pad =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_combiner_release_pad);
|
|
|
|
|
2012-04-09 23:45:16 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_klass,
|
2010-08-13 15:36:38 +00:00
|
|
|
"streamcombiner", "Generic",
|
|
|
|
"Recombines streams splitted by the streamsplitter element",
|
|
|
|
"Edward Hervey <edward.hervey@collabora.co.uk>");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-01-18 17:21:36 +00:00
|
|
|
gst_stream_combiner_finalize (GObject * object)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) object;
|
|
|
|
|
2012-01-18 17:21:36 +00:00
|
|
|
g_mutex_clear (&stream_combiner->lock);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2012-01-18 17:21:36 +00:00
|
|
|
G_OBJECT_CLASS (gst_stream_combiner_parent_class)->finalize (object);
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-17 11:48:25 +00:00
|
|
|
gst_stream_combiner_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2011-11-17 11:48:25 +00:00
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
|
2010-08-13 15:36:38 +00:00
|
|
|
/* FIXME : IMPLEMENT */
|
|
|
|
|
|
|
|
/* with lock taken, check if we're the active stream, if not drop */
|
|
|
|
return gst_pad_push (stream_combiner->srcpad, buf);
|
|
|
|
}
|
|
|
|
|
2013-07-21 22:48:54 +00:00
|
|
|
static gboolean
|
|
|
|
_all_sink_pads_eos (GstStreamCombiner * combiner)
|
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
for (tmp = combiner->sinkpads; tmp; tmp = tmp->next) {
|
|
|
|
if (!(GST_STREAM_COMBINER_PAD (tmp->data))->is_eos)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
static gboolean
|
2011-11-17 11:48:25 +00:00
|
|
|
gst_stream_combiner_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2011-11-17 11:48:25 +00:00
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
|
2013-07-21 22:48:54 +00:00
|
|
|
GstStreamCombinerPad *combiner_pad = GST_STREAM_COMBINER_PAD (pad);
|
2010-08-13 15:36:38 +00:00
|
|
|
/* FIXME : IMPLEMENT */
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "Got event %s", GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2013-07-21 22:48:54 +00:00
|
|
|
case GST_EVENT_EOS:
|
2013-07-22 06:08:27 +00:00
|
|
|
STREAMS_LOCK (stream_combiner);
|
2013-07-21 22:48:54 +00:00
|
|
|
combiner_pad->is_eos = TRUE;
|
|
|
|
if (!_all_sink_pads_eos (stream_combiner)) {
|
2010-08-13 15:36:38 +00:00
|
|
|
gst_event_unref (event);
|
2013-07-21 22:48:54 +00:00
|
|
|
event = NULL;
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (stream_combiner, "All sink pads eos, pushing eos");
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
2013-07-22 06:08:27 +00:00
|
|
|
STREAMS_UNLOCK (stream_combiner);
|
2010-08-13 15:36:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-07 08:58:27 +00:00
|
|
|
/* SEGMENT : lock, wait for other stream to EOS, select stream, unlock, push */
|
2010-08-13 15:36:38 +00:00
|
|
|
/* FLUSH_START : lock, mark as flushing, unlock. if wasn't flushing forward */
|
|
|
|
/* FLUSH_STOP : lock, unmark as flushing, unlock, if was flushing forward */
|
|
|
|
/* OTHER : if selected pad forward */
|
2013-07-21 22:48:54 +00:00
|
|
|
if (event)
|
2013-07-23 07:14:23 +00:00
|
|
|
return gst_pad_push_event (stream_combiner->srcpad, event);
|
2013-07-21 22:48:54 +00:00
|
|
|
return FALSE;
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
2011-11-15 15:30:38 +00:00
|
|
|
static gboolean
|
2011-11-16 16:25:17 +00:00
|
|
|
gst_stream_combiner_sink_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2011-11-16 16:25:17 +00:00
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2011-11-15 15:30:38 +00:00
|
|
|
return gst_pad_peer_query (stream_combiner->srcpad, query);
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-17 11:48:25 +00:00
|
|
|
gst_stream_combiner_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2011-11-17 11:48:25 +00:00
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
|
2010-08-13 15:36:38 +00:00
|
|
|
GstPad *sinkpad = NULL;
|
|
|
|
|
|
|
|
STREAMS_LOCK (stream_combiner);
|
|
|
|
if (stream_combiner->current)
|
|
|
|
sinkpad = stream_combiner->current;
|
|
|
|
else if (stream_combiner->sinkpads)
|
|
|
|
sinkpad = (GstPad *) stream_combiner->sinkpads->data;
|
|
|
|
STREAMS_UNLOCK (stream_combiner);
|
|
|
|
|
|
|
|
if (sinkpad)
|
|
|
|
/* Forward upstream as is */
|
|
|
|
return gst_pad_push_event (sinkpad, event);
|
2011-11-15 15:30:38 +00:00
|
|
|
|
2010-08-13 15:36:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-16 16:25:17 +00:00
|
|
|
gst_stream_combiner_src_query (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
2011-11-16 16:25:17 +00:00
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
|
2010-08-13 15:36:38 +00:00
|
|
|
GstPad *sinkpad = NULL;
|
2012-01-02 17:28:46 +00:00
|
|
|
gboolean ret = FALSE;
|
2010-08-13 15:36:38 +00:00
|
|
|
|
2012-01-02 17:28:46 +00:00
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CAPS:
|
|
|
|
ret = gst_pad_query_default (pad, parent, query);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
STREAMS_LOCK (stream_combiner);
|
|
|
|
if (stream_combiner->current)
|
|
|
|
sinkpad = stream_combiner->current;
|
|
|
|
else if (stream_combiner->sinkpads)
|
|
|
|
sinkpad = (GstPad *) stream_combiner->sinkpads->data;
|
|
|
|
STREAMS_UNLOCK (stream_combiner);
|
|
|
|
|
|
|
|
if (sinkpad)
|
|
|
|
/* Forward upstream as is */
|
|
|
|
ret = gst_pad_peer_query (sinkpad, query);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_stream_combiner_init (GstStreamCombiner * stream_combiner)
|
|
|
|
{
|
|
|
|
stream_combiner->srcpad =
|
|
|
|
gst_pad_new_from_static_template (&src_template, "src");
|
|
|
|
gst_pad_set_event_function (stream_combiner->srcpad,
|
|
|
|
gst_stream_combiner_src_event);
|
|
|
|
gst_pad_set_query_function (stream_combiner->srcpad,
|
|
|
|
gst_stream_combiner_src_query);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (stream_combiner), stream_combiner->srcpad);
|
|
|
|
|
2012-01-18 17:21:36 +00:00
|
|
|
g_mutex_init (&stream_combiner->lock);
|
2010-08-13 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstPad *
|
|
|
|
gst_stream_combiner_request_new_pad (GstElement * element,
|
2011-05-10 14:44:37 +00:00
|
|
|
GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
|
2010-08-13 15:36:38 +00:00
|
|
|
{
|
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
|
2013-07-21 22:48:54 +00:00
|
|
|
GstStreamCombinerPad *combiner_pad;
|
2010-08-13 15:36:38 +00:00
|
|
|
GstPad *sinkpad;
|
2013-07-21 22:48:54 +00:00
|
|
|
GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
|
|
|
|
GstPadTemplate *template =
|
|
|
|
gst_element_class_get_pad_template (klass, "sink_%u");
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (element, "templ:%p, name:%s", templ, name);
|
|
|
|
|
2013-07-21 22:48:54 +00:00
|
|
|
combiner_pad = g_object_new (GST_TYPE_STREAM_COMBINER_PAD, "name", name,
|
|
|
|
"template", template, "direction", template->direction, NULL);
|
|
|
|
|
|
|
|
sinkpad = GST_PAD_CAST (combiner_pad);
|
2010-08-13 15:36:38 +00:00
|
|
|
gst_pad_set_chain_function (sinkpad, gst_stream_combiner_chain);
|
|
|
|
gst_pad_set_event_function (sinkpad, gst_stream_combiner_sink_event);
|
2011-11-15 15:30:38 +00:00
|
|
|
gst_pad_set_query_function (sinkpad, gst_stream_combiner_sink_query);
|
2010-08-13 15:36:38 +00:00
|
|
|
|
|
|
|
STREAMS_LOCK (stream_combiner);
|
|
|
|
stream_combiner->sinkpads =
|
|
|
|
g_list_append (stream_combiner->sinkpads, sinkpad);
|
|
|
|
gst_pad_set_active (sinkpad, TRUE);
|
|
|
|
gst_element_add_pad (element, sinkpad);
|
|
|
|
stream_combiner->cookie++;
|
|
|
|
STREAMS_UNLOCK (stream_combiner);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (element, "Returning pad %p", sinkpad);
|
|
|
|
|
|
|
|
return sinkpad;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_stream_combiner_release_pad (GstElement * element, GstPad * pad)
|
|
|
|
{
|
|
|
|
GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
|
|
|
|
GList *tmp;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
|
|
|
|
|
|
|
|
STREAMS_LOCK (stream_combiner);
|
|
|
|
tmp = g_list_find (stream_combiner->sinkpads, pad);
|
|
|
|
if (tmp) {
|
|
|
|
GstPad *pad = (GstPad *) tmp->data;
|
|
|
|
|
|
|
|
stream_combiner->sinkpads =
|
|
|
|
g_list_delete_link (stream_combiner->sinkpads, tmp);
|
|
|
|
stream_combiner->cookie++;
|
|
|
|
|
|
|
|
if (pad == stream_combiner->current) {
|
|
|
|
/* Deactivate current flow */
|
|
|
|
GST_DEBUG_OBJECT (element, "Removed pad was the current one");
|
|
|
|
stream_combiner->current = NULL;
|
|
|
|
}
|
|
|
|
GST_DEBUG_OBJECT (element, "Removing pad from ourself");
|
|
|
|
gst_element_remove_pad (element, pad);
|
|
|
|
}
|
|
|
|
STREAMS_UNLOCK (stream_combiner);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|