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"
26 #include "gst/glib-compat-private.h"
28 static GstStaticPadTemplate src_template =
29 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
32 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%d",
37 GST_DEBUG_CATEGORY_STATIC (gst_stream_combiner_debug);
38 #define GST_CAT_DEFAULT gst_stream_combiner_debug
40 G_DEFINE_TYPE (GstStreamCombiner, gst_stream_combiner, GST_TYPE_ELEMENT);
42 #define STREAMS_LOCK(obj) (g_mutex_lock(obj->lock))
43 #define STREAMS_UNLOCK(obj) (g_mutex_unlock(obj->lock))
45 static void gst_stream_combiner_dispose (GObject * object);
47 static GstPad *gst_stream_combiner_request_new_pad (GstElement * element,
48 GstPadTemplate * templ, const gchar * name);
49 static void gst_stream_combiner_release_pad (GstElement * element,
53 gst_stream_combiner_class_init (GstStreamCombinerClass * klass)
55 GObjectClass *gobject_klass;
56 GstElementClass *gstelement_klass;
58 gobject_klass = (GObjectClass *) klass;
59 gstelement_klass = (GstElementClass *) klass;
61 gobject_klass->dispose = gst_stream_combiner_dispose;
63 GST_DEBUG_CATEGORY_INIT (gst_stream_combiner_debug, "streamcombiner", 0,
66 gst_element_class_add_static_pad_template (gstelement_klass, &src_template);
67 gst_element_class_add_static_pad_template (gstelement_klass, &sink_template);
69 gstelement_klass->request_new_pad =
70 GST_DEBUG_FUNCPTR (gst_stream_combiner_request_new_pad);
71 gstelement_klass->release_pad =
72 GST_DEBUG_FUNCPTR (gst_stream_combiner_release_pad);
74 gst_element_class_set_details_simple (gstelement_klass,
75 "streamcombiner", "Generic",
76 "Recombines streams splitted by the streamsplitter element",
77 "Edward Hervey <edward.hervey@collabora.co.uk>");
81 gst_stream_combiner_dispose (GObject * object)
83 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) object;
85 if (stream_combiner->lock) {
86 g_mutex_free (stream_combiner->lock);
87 stream_combiner->lock = NULL;
90 G_OBJECT_CLASS (gst_stream_combiner_parent_class)->dispose (object);
94 gst_stream_combiner_chain (GstPad * pad, GstBuffer * buf)
96 GstStreamCombiner *stream_combiner =
97 (GstStreamCombiner *) GST_PAD_PARENT (pad);
98 /* FIXME : IMPLEMENT */
100 /* with lock taken, check if we're the active stream, if not drop */
101 return gst_pad_push (stream_combiner->srcpad, buf);
105 gst_stream_combiner_sink_event (GstPad * pad, GstEvent * event)
107 GstStreamCombiner *stream_combiner =
108 (GstStreamCombiner *) GST_PAD_PARENT (pad);
109 /* FIXME : IMPLEMENT */
111 GST_DEBUG_OBJECT (pad, "Got event %s", GST_EVENT_TYPE_NAME (event));
113 switch (GST_EVENT_TYPE (event)) {
114 case GST_EVENT_CUSTOM_DOWNSTREAM:
115 if (gst_event_has_name (event, "stream-switching-eos")) {
116 gst_event_unref (event);
117 event = gst_event_new_eos ();
124 /* NEW_SEGMENT : lock, wait for other stream to EOS, select stream, unlock, push */
125 /* EOS : lock, mark pad as unused, unlock , drop event */
126 /* CUSTOM_REAL_EOS : push EOS downstream */
127 /* FLUSH_START : lock, mark as flushing, unlock. if wasn't flushing forward */
128 /* FLUSH_STOP : lock, unmark as flushing, unlock, if was flushing forward */
129 /* OTHER : if selected pad forward */
130 return gst_pad_push_event (stream_combiner->srcpad, event);
134 gst_stream_combiner_sink_getcaps (GstPad * pad)
136 GstStreamCombiner *stream_combiner =
137 (GstStreamCombiner *) GST_PAD_PARENT (pad);
139 return gst_pad_peer_get_caps_reffed (stream_combiner->srcpad);
143 gst_stream_combiner_sink_setcaps (GstPad * pad, GstCaps * caps)
145 GstStreamCombiner *stream_combiner =
146 (GstStreamCombiner *) GST_PAD_PARENT (pad);
148 gboolean res = FALSE;
150 GST_DEBUG_OBJECT (pad, "caps:%" GST_PTR_FORMAT, caps);
152 peer = gst_pad_get_peer (stream_combiner->srcpad);
154 GST_DEBUG_OBJECT (peer, "Setting caps");
155 res = gst_pad_set_caps (peer, caps);
156 gst_object_unref (peer);
158 GST_WARNING_OBJECT (stream_combiner, "sourcepad has no peer !");
163 gst_stream_combiner_src_event (GstPad * pad, GstEvent * event)
165 GstStreamCombiner *stream_combiner =
166 (GstStreamCombiner *) GST_PAD_PARENT (pad);
167 GstPad *sinkpad = NULL;
169 STREAMS_LOCK (stream_combiner);
170 if (stream_combiner->current)
171 sinkpad = stream_combiner->current;
172 else if (stream_combiner->sinkpads)
173 sinkpad = (GstPad *) stream_combiner->sinkpads->data;
174 STREAMS_UNLOCK (stream_combiner);
177 /* Forward upstream as is */
178 return gst_pad_push_event (sinkpad, event);
183 gst_stream_combiner_src_query (GstPad * pad, GstQuery * query)
185 GstStreamCombiner *stream_combiner =
186 (GstStreamCombiner *) GST_PAD_PARENT (pad);
188 GstPad *sinkpad = NULL;
190 STREAMS_LOCK (stream_combiner);
191 if (stream_combiner->current)
192 sinkpad = stream_combiner->current;
193 else if (stream_combiner->sinkpads)
194 sinkpad = (GstPad *) stream_combiner->sinkpads->data;
195 STREAMS_UNLOCK (stream_combiner);
198 /* Forward upstream as is */
199 return gst_pad_peer_query (sinkpad, query);
204 gst_stream_combiner_init (GstStreamCombiner * stream_combiner)
206 stream_combiner->srcpad =
207 gst_pad_new_from_static_template (&src_template, "src");
208 gst_pad_set_event_function (stream_combiner->srcpad,
209 gst_stream_combiner_src_event);
210 gst_pad_set_query_function (stream_combiner->srcpad,
211 gst_stream_combiner_src_query);
212 gst_element_add_pad (GST_ELEMENT (stream_combiner), stream_combiner->srcpad);
214 stream_combiner->lock = g_mutex_new ();
218 gst_stream_combiner_request_new_pad (GstElement * element,
219 GstPadTemplate * templ, const gchar * name)
221 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
224 GST_DEBUG_OBJECT (element, "templ:%p, name:%s", templ, name);
226 sinkpad = gst_pad_new_from_static_template (&sink_template, name);
227 /* FIXME : No buffer alloc for the time being, it will resort to the fallback */
228 /* gst_pad_set_bufferalloc_function (sinkpad, gst_stream_combiner_buffer_alloc); */
229 gst_pad_set_chain_function (sinkpad, gst_stream_combiner_chain);
230 gst_pad_set_event_function (sinkpad, gst_stream_combiner_sink_event);
231 gst_pad_set_getcaps_function (sinkpad, gst_stream_combiner_sink_getcaps);
232 gst_pad_set_setcaps_function (sinkpad, gst_stream_combiner_sink_setcaps);
234 STREAMS_LOCK (stream_combiner);
235 stream_combiner->sinkpads =
236 g_list_append (stream_combiner->sinkpads, sinkpad);
237 gst_pad_set_active (sinkpad, TRUE);
238 gst_element_add_pad (element, sinkpad);
239 stream_combiner->cookie++;
240 STREAMS_UNLOCK (stream_combiner);
242 GST_DEBUG_OBJECT (element, "Returning pad %p", sinkpad);
248 gst_stream_combiner_release_pad (GstElement * element, GstPad * pad)
250 GstStreamCombiner *stream_combiner = (GstStreamCombiner *) element;
253 GST_DEBUG_OBJECT (element, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
255 STREAMS_LOCK (stream_combiner);
256 tmp = g_list_find (stream_combiner->sinkpads, pad);
258 GstPad *pad = (GstPad *) tmp->data;
260 stream_combiner->sinkpads =
261 g_list_delete_link (stream_combiner->sinkpads, tmp);
262 stream_combiner->cookie++;
264 if (pad == stream_combiner->current) {
265 /* Deactivate current flow */
266 GST_DEBUG_OBJECT (element, "Removed pad was the current one");
267 stream_combiner->current = NULL;
269 GST_DEBUG_OBJECT (element, "Removing pad from ourself");
270 gst_element_remove_pad (element, pad);
272 STREAMS_UNLOCK (stream_combiner);