1 /* GStreamer Stream Combiner
2 * Copyright (C) 2010 Edward Hervey <edward.hervey@collabora.co.uk>
3 * (C) 2009 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "gststreamcombiner.h"
27 static GstStaticPadTemplate src_template =
28 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
31 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
36 GST_DEBUG_CATEGORY_STATIC (gst_stream_combiner_debug);
37 #define GST_CAT_DEFAULT gst_stream_combiner_debug
39 G_DEFINE_TYPE (GstStreamCombiner, gst_stream_combiner, GST_TYPE_ELEMENT);
41 #define STREAMS_LOCK(obj) (g_mutex_lock(&obj->lock))
42 #define STREAMS_UNLOCK(obj) (g_mutex_unlock(&obj->lock))
44 static void gst_stream_combiner_finalize (GObject * object);
46 static GstPad *gst_stream_combiner_request_new_pad (GstElement * element,
47 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
48 static void gst_stream_combiner_release_pad (GstElement * element,
52 gst_stream_combiner_class_init (GstStreamCombinerClass * klass)
54 GObjectClass *gobject_klass;
55 GstElementClass *gstelement_klass;
57 gobject_klass = (GObjectClass *) klass;
58 gstelement_klass = (GstElementClass *) klass;
60 gobject_klass->finalize = gst_stream_combiner_finalize;
62 GST_DEBUG_CATEGORY_INIT (gst_stream_combiner_debug, "streamcombiner", 0,
65 gst_element_class_add_pad_template (gstelement_klass,
66 gst_static_pad_template_get (&src_template));
67 gst_element_class_add_pad_template (gstelement_klass,
68 gst_static_pad_template_get (&sink_template));
70 gstelement_klass->request_new_pad =
71 GST_DEBUG_FUNCPTR (gst_stream_combiner_request_new_pad);
72 gstelement_klass->release_pad =
73 GST_DEBUG_FUNCPTR (gst_stream_combiner_release_pad);
75 gst_element_class_set_static_metadata (gstelement_klass,
76 "streamcombiner", "Generic",
77 "Recombines streams splitted by the streamsplitter element",
78 "Edward Hervey <edward.hervey@collabora.co.uk>");
82 gst_stream_combiner_finalize (GObject * object)
84 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) object;
86 g_mutex_clear (&stream_combiner->lock);
88 G_OBJECT_CLASS (gst_stream_combiner_parent_class)->finalize (object);
92 gst_stream_combiner_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
94 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
95 /* FIXME : IMPLEMENT */
97 /* with lock taken, check if we're the active stream, if not drop */
98 return gst_pad_push (stream_combiner->srcpad, buf);
102 gst_stream_combiner_sink_event (GstPad * pad, GstObject * parent,
105 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
106 /* FIXME : IMPLEMENT */
108 GST_DEBUG_OBJECT (pad, "Got event %s", GST_EVENT_TYPE_NAME (event));
110 switch (GST_EVENT_TYPE (event)) {
111 case GST_EVENT_CUSTOM_DOWNSTREAM:
112 if (gst_event_has_name (event, "stream-switching-eos")) {
113 gst_event_unref (event);
114 event = gst_event_new_eos ();
121 /* SEGMENT : lock, wait for other stream to EOS, select stream, unlock, push */
122 /* EOS : lock, mark pad as unused, unlock , drop event */
123 /* CUSTOM_REAL_EOS : push EOS downstream */
124 /* FLUSH_START : lock, mark as flushing, unlock. if wasn't flushing forward */
125 /* FLUSH_STOP : lock, unmark as flushing, unlock, if was flushing forward */
126 /* OTHER : if selected pad forward */
127 return gst_pad_push_event (stream_combiner->srcpad, event);
131 gst_stream_combiner_sink_query (GstPad * pad, GstObject * parent,
134 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
136 return gst_pad_peer_query (stream_combiner->srcpad, query);
140 gst_stream_combiner_src_event (GstPad * pad, GstObject * parent,
143 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
144 GstPad *sinkpad = NULL;
146 STREAMS_LOCK (stream_combiner);
147 if (stream_combiner->current)
148 sinkpad = stream_combiner->current;
149 else if (stream_combiner->sinkpads)
150 sinkpad = (GstPad *) stream_combiner->sinkpads->data;
151 STREAMS_UNLOCK (stream_combiner);
154 /* Forward upstream as is */
155 return gst_pad_push_event (sinkpad, event);
161 gst_stream_combiner_src_query (GstPad * pad, GstObject * parent,
164 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) parent;
165 GstPad *sinkpad = NULL;
166 gboolean ret = FALSE;
168 switch (GST_QUERY_TYPE (query)) {
170 ret = gst_pad_query_default (pad, parent, query);
173 STREAMS_LOCK (stream_combiner);
174 if (stream_combiner->current)
175 sinkpad = stream_combiner->current;
176 else if (stream_combiner->sinkpads)
177 sinkpad = (GstPad *) stream_combiner->sinkpads->data;
178 STREAMS_UNLOCK (stream_combiner);
181 /* Forward upstream as is */
182 ret = gst_pad_peer_query (sinkpad, query);
189 gst_stream_combiner_init (GstStreamCombiner * stream_combiner)
191 stream_combiner->srcpad =
192 gst_pad_new_from_static_template (&src_template, "src");
193 gst_pad_set_event_function (stream_combiner->srcpad,
194 gst_stream_combiner_src_event);
195 gst_pad_set_query_function (stream_combiner->srcpad,
196 gst_stream_combiner_src_query);
197 gst_element_add_pad (GST_ELEMENT (stream_combiner), stream_combiner->srcpad);
199 g_mutex_init (&stream_combiner->lock);
203 gst_stream_combiner_request_new_pad (GstElement * element,
204 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
206 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
209 GST_DEBUG_OBJECT (element, "templ:%p, name:%s", templ, name);
211 sinkpad = gst_pad_new_from_static_template (&sink_template, name);
212 gst_pad_set_chain_function (sinkpad, gst_stream_combiner_chain);
213 gst_pad_set_event_function (sinkpad, gst_stream_combiner_sink_event);
214 gst_pad_set_query_function (sinkpad, gst_stream_combiner_sink_query);
216 STREAMS_LOCK (stream_combiner);
217 stream_combiner->sinkpads =
218 g_list_append (stream_combiner->sinkpads, sinkpad);
219 gst_pad_set_active (sinkpad, TRUE);
220 gst_element_add_pad (element, sinkpad);
221 stream_combiner->cookie++;
222 STREAMS_UNLOCK (stream_combiner);
224 GST_DEBUG_OBJECT (element, "Returning pad %p", sinkpad);
230 gst_stream_combiner_release_pad (GstElement * element, GstPad * pad)
232 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
235 GST_DEBUG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
237 STREAMS_LOCK (stream_combiner);
238 tmp = g_list_find (stream_combiner->sinkpads, pad);
240 GstPad *pad = (GstPad *) tmp->data;
242 stream_combiner->sinkpads =
243 g_list_delete_link (stream_combiner->sinkpads, tmp);
244 stream_combiner->cookie++;
246 if (pad == stream_combiner->current) {
247 /* Deactivate current flow */
248 GST_DEBUG_OBJECT (element, "Removed pad was the current one");
249 stream_combiner->current = NULL;
251 GST_DEBUG_OBJECT (element, "Removing pad from ourself");
252 gst_element_remove_pad (element, pad);
254 STREAMS_UNLOCK (stream_combiner);