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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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_%d",
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_dispose (GObject * object);
46 static GstPad *gst_stream_combiner_request_new_pad (GstElement * element,
47 GstPadTemplate * templ, const gchar * name);
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->dispose = gst_stream_combiner_dispose;
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_details_simple (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_dispose (GObject * object)
84 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) object;
86 if (stream_combiner->lock) {
87 g_mutex_free (stream_combiner->lock);
88 stream_combiner->lock = NULL;
91 G_OBJECT_CLASS (gst_stream_combiner_parent_class)->dispose (object);
95 gst_stream_combiner_chain (GstPad * pad, GstBuffer * buf)
97 GstStreamCombiner *stream_combiner =
98 (GstStreamCombiner *) GST_PAD_PARENT (pad);
99 /* FIXME : IMPLEMENT */
101 /* with lock taken, check if we're the active stream, if not drop */
102 return gst_pad_push (stream_combiner->srcpad, buf);
106 gst_stream_combiner_sink_event (GstPad * pad, GstEvent * event)
108 GstStreamCombiner *stream_combiner =
109 (GstStreamCombiner *) GST_PAD_PARENT (pad);
110 /* FIXME : IMPLEMENT */
112 GST_DEBUG_OBJECT (pad, "Got event %s", GST_EVENT_TYPE_NAME (event));
114 switch (GST_EVENT_TYPE (event)) {
115 case GST_EVENT_CUSTOM_DOWNSTREAM:
116 if (gst_event_has_name (event, "stream-switching-eos")) {
117 gst_event_unref (event);
118 event = gst_event_new_eos ();
125 /* NEW_SEGMENT : lock, wait for other stream to EOS, select stream, unlock, push */
126 /* EOS : lock, mark pad as unused, unlock , drop event */
127 /* CUSTOM_REAL_EOS : push EOS downstream */
128 /* FLUSH_START : lock, mark as flushing, unlock. if wasn't flushing forward */
129 /* FLUSH_STOP : lock, unmark as flushing, unlock, if was flushing forward */
130 /* OTHER : if selected pad forward */
131 return gst_pad_push_event (stream_combiner->srcpad, event);
135 gst_stream_combiner_sink_getcaps (GstPad * pad)
137 GstStreamCombiner *stream_combiner =
138 (GstStreamCombiner *) GST_PAD_PARENT (pad);
140 return gst_pad_peer_get_caps_reffed (stream_combiner->srcpad);
144 gst_stream_combiner_sink_setcaps (GstPad * pad, GstCaps * caps)
146 GstStreamCombiner *stream_combiner =
147 (GstStreamCombiner *) GST_PAD_PARENT (pad);
149 gboolean res = FALSE;
151 GST_DEBUG_OBJECT (pad, "caps:%" GST_PTR_FORMAT, caps);
153 peer = gst_pad_get_peer (stream_combiner->srcpad);
155 GST_DEBUG_OBJECT (peer, "Setting caps");
156 res = gst_pad_set_caps (peer, caps);
157 gst_object_unref (peer);
159 GST_WARNING_OBJECT (stream_combiner, "sourcepad has no peer !");
164 gst_stream_combiner_src_event (GstPad * pad, GstEvent * event)
166 GstStreamCombiner *stream_combiner =
167 (GstStreamCombiner *) GST_PAD_PARENT (pad);
168 GstPad *sinkpad = NULL;
170 STREAMS_LOCK (stream_combiner);
171 if (stream_combiner->current)
172 sinkpad = stream_combiner->current;
173 else if (stream_combiner->sinkpads)
174 sinkpad = (GstPad *) stream_combiner->sinkpads->data;
175 STREAMS_UNLOCK (stream_combiner);
178 /* Forward upstream as is */
179 return gst_pad_push_event (sinkpad, event);
184 gst_stream_combiner_src_query (GstPad * pad, GstQuery * query)
186 GstStreamCombiner *stream_combiner =
187 (GstStreamCombiner *) GST_PAD_PARENT (pad);
189 GstPad *sinkpad = NULL;
191 STREAMS_LOCK (stream_combiner);
192 if (stream_combiner->current)
193 sinkpad = stream_combiner->current;
194 else if (stream_combiner->sinkpads)
195 sinkpad = (GstPad *) stream_combiner->sinkpads->data;
196 STREAMS_UNLOCK (stream_combiner);
199 /* Forward upstream as is */
200 return gst_pad_peer_query (sinkpad, query);
205 gst_stream_combiner_init (GstStreamCombiner * stream_combiner)
207 stream_combiner->srcpad =
208 gst_pad_new_from_static_template (&src_template, "src");
209 gst_pad_set_event_function (stream_combiner->srcpad,
210 gst_stream_combiner_src_event);
211 gst_pad_set_query_function (stream_combiner->srcpad,
212 gst_stream_combiner_src_query);
213 gst_element_add_pad (GST_ELEMENT (stream_combiner), stream_combiner->srcpad);
215 stream_combiner->lock = g_mutex_new ();
219 gst_stream_combiner_request_new_pad (GstElement * element,
220 GstPadTemplate * templ, const gchar * name)
222 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
225 GST_DEBUG_OBJECT (element, "templ:%p, name:%s", templ, name);
227 sinkpad = gst_pad_new_from_static_template (&sink_template, name);
228 /* FIXME : No buffer alloc for the time being, it will resort to the fallback */
229 /* gst_pad_set_bufferalloc_function (sinkpad, gst_stream_combiner_buffer_alloc); */
230 gst_pad_set_chain_function (sinkpad, gst_stream_combiner_chain);
231 gst_pad_set_event_function (sinkpad, gst_stream_combiner_sink_event);
232 gst_pad_set_getcaps_function (sinkpad, gst_stream_combiner_sink_getcaps);
233 gst_pad_set_setcaps_function (sinkpad, gst_stream_combiner_sink_setcaps);
235 STREAMS_LOCK (stream_combiner);
236 stream_combiner->sinkpads =
237 g_list_append (stream_combiner->sinkpads, sinkpad);
238 gst_pad_set_active (sinkpad, TRUE);
239 gst_element_add_pad (element, sinkpad);
240 stream_combiner->cookie++;
241 STREAMS_UNLOCK (stream_combiner);
243 GST_DEBUG_OBJECT (element, "Returning pad %p", sinkpad);
249 gst_stream_combiner_release_pad (GstElement * element, GstPad * pad)
251 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
254 GST_DEBUG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
256 STREAMS_LOCK (stream_combiner);
257 tmp = g_list_find (stream_combiner->sinkpads, pad);
259 GstPad *pad = (GstPad *) tmp->data;
261 stream_combiner->sinkpads =
262 g_list_delete_link (stream_combiner->sinkpads, tmp);
263 stream_combiner->cookie++;
265 if (pad == stream_combiner->current) {
266 /* Deactivate current flow */
267 GST_DEBUG_OBJECT (element, "Removed pad was the current one");
268 stream_combiner->current = NULL;
270 GST_DEBUG_OBJECT (element, "Removing pad from ourself");
271 gst_element_remove_pad (element, pad);
273 STREAMS_UNLOCK (stream_combiner);