2 * GStreamer Funnel element
4 * Copyright 2007 Collabora Ltd.
5 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
6 * Copyright 2007 Nokia Corp.
8 * gstfunnel.c: Simple Funnel element
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * SECTION:element-funnel
28 * Takes packets from various input sinks into one output source.
30 * funnel always outputs a single, open ended segment from
31 * 0 with in %GST_FORMAT_TIME and outputs the buffers of the
32 * different sinkpads with timestamps that are set to the
33 * running time for that stream. funnel does not synchronize
34 * the different input streams but simply forwards all buffers
35 * immediately when they arrive.
43 #include "gstfunnel.h"
45 GST_DEBUG_CATEGORY_STATIC (gst_funnel_debug);
46 #define GST_CAT_DEFAULT gst_funnel_debug
48 GType gst_funnel_pad_get_type (void);
49 #define GST_TYPE_FUNNEL_PAD \
50 (gst_funnel_pad_get_type())
51 #define GST_FUNNEL_PAD(obj) \
52 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_FUNNEL_PAD, GstFunnelPad))
53 #define GST_FUNNEL_PAD_CLASS(klass) \
54 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_FUNNEL_PAD, GstFunnelPadClass))
55 #define GST_IS_FUNNEL_PAD(obj) \
56 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FUNNEL_PAD))
57 #define GST_IS_FUNNEL_PAD_CLASS(klass) \
58 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_FUNNEL_PAD))
59 #define GST_FUNNEL_PAD_CAST(obj) \
60 ((GstFunnelPad *)(obj))
62 typedef struct _GstFunnelPad GstFunnelPad;
63 typedef struct _GstFunnelPadClass GstFunnelPadClass;
72 struct _GstFunnelPadClass
77 G_DEFINE_TYPE (GstFunnelPad, gst_funnel_pad, GST_TYPE_PAD);
80 gst_funnel_pad_class_init (GstFunnelPadClass * klass)
85 gst_funnel_pad_reset (GstFunnelPad * pad)
87 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
91 gst_funnel_pad_init (GstFunnelPad * pad)
93 gst_funnel_pad_reset (pad);
96 static GstStaticPadTemplate funnel_sink_template =
97 GST_STATIC_PAD_TEMPLATE ("sink%d",
100 GST_STATIC_CAPS_ANY);
102 static GstStaticPadTemplate funnel_src_template =
103 GST_STATIC_PAD_TEMPLATE ("src",
106 GST_STATIC_CAPS_ANY);
109 GST_DEBUG_CATEGORY_INIT (gst_funnel_debug, "funnel", 0, "funnel element");
110 #define gst_funnel_parent_class parent_class
111 G_DEFINE_TYPE_WITH_CODE (GstFunnel, gst_funnel, GST_TYPE_ELEMENT, _do_init);
113 static GstStateChangeReturn gst_funnel_change_state (GstElement * element,
114 GstStateChange transition);
115 static GstPad *gst_funnel_request_new_pad (GstElement * element,
116 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
117 static void gst_funnel_release_pad (GstElement * element, GstPad * pad);
119 static GstFlowReturn gst_funnel_sink_chain (GstPad * pad, GstBuffer * buffer);
120 static gboolean gst_funnel_sink_event (GstPad * pad, GstEvent * event);
121 static GstCaps *gst_funnel_sink_getcaps (GstPad * pad);
123 static gboolean gst_funnel_src_event (GstPad * pad, GstEvent * event);
126 gst_funnel_dispose (GObject * object)
131 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
132 GstPad *pad = GST_PAD (item->data);
134 if (GST_PAD_IS_SINK (pad)) {
135 gst_element_release_request_pad (GST_ELEMENT (object), pad);
140 G_OBJECT_CLASS (parent_class)->dispose (object);
144 gst_funnel_class_init (GstFunnelClass * klass)
146 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
147 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
149 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_funnel_dispose);
151 gst_element_class_set_details_simple (gstelement_class,
152 "Funnel pipe fitting", "Generic", "N-to-1 pipe fitting",
153 "Olivier Crete <olivier.crete@collabora.co.uk>");
155 gst_element_class_add_pad_template (gstelement_class,
156 gst_static_pad_template_get (&funnel_sink_template));
157 gst_element_class_add_pad_template (gstelement_class,
158 gst_static_pad_template_get (&funnel_src_template));
160 gstelement_class->request_new_pad =
161 GST_DEBUG_FUNCPTR (gst_funnel_request_new_pad);
162 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_funnel_release_pad);
163 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_funnel_change_state);
167 gst_funnel_init (GstFunnel * funnel)
169 funnel->srcpad = gst_pad_new_from_static_template (&funnel_src_template,
171 gst_pad_set_event_function (funnel->srcpad, gst_funnel_src_event);
172 gst_pad_use_fixed_caps (funnel->srcpad);
173 gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
177 gst_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
178 const gchar * name, const GstCaps * caps)
182 GST_DEBUG_OBJECT (element, "requesting pad");
184 sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_FUNNEL_PAD,
185 "name", name, "direction", templ->direction, "template", templ,
188 gst_pad_set_chain_function (sinkpad,
189 GST_DEBUG_FUNCPTR (gst_funnel_sink_chain));
190 gst_pad_set_event_function (sinkpad,
191 GST_DEBUG_FUNCPTR (gst_funnel_sink_event));
192 gst_pad_set_getcaps_function (sinkpad,
193 GST_DEBUG_FUNCPTR (gst_funnel_sink_getcaps));
195 gst_pad_set_active (sinkpad, TRUE);
197 gst_element_add_pad (element, sinkpad);
203 gst_funnel_release_pad (GstElement * element, GstPad * pad)
205 GstFunnel *funnel = GST_FUNNEL (element);
207 GST_DEBUG_OBJECT (funnel, "releasing pad");
209 gst_pad_set_active (pad, FALSE);
211 gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
215 gst_funnel_sink_getcaps (GstPad * pad)
217 GstFunnel *funnel = GST_FUNNEL (gst_pad_get_parent (pad));
220 if (G_UNLIKELY (funnel == NULL))
221 return gst_caps_new_any ();
223 caps = gst_pad_peer_get_caps_reffed (funnel->srcpad);
225 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
227 gst_object_unref (funnel);
233 gst_funnel_sink_chain (GstPad * pad, GstBuffer * buffer)
236 GstFunnel *funnel = GST_FUNNEL (gst_pad_get_parent (pad));
237 GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
238 GstEvent *event = NULL;
244 GST_DEBUG_OBJECT (funnel, "received buffer %p", buffer);
246 GST_OBJECT_LOCK (funnel);
247 if (fpad->segment.format == GST_FORMAT_UNDEFINED) {
248 GST_WARNING_OBJECT (funnel, "Got buffer without segment,"
249 " setting segment [0,inf[");
250 gst_segment_set_newsegment (&fpad->segment, FALSE, 1.0, 1.0,
251 GST_FORMAT_TIME, 0, -1, 0);
254 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)))
255 gst_segment_set_last_stop (&fpad->segment, fpad->segment.format,
256 GST_BUFFER_TIMESTAMP (buffer));
258 newts = gst_segment_to_running_time (&fpad->segment,
259 fpad->segment.format, GST_BUFFER_TIMESTAMP (buffer));
260 if (newts != GST_BUFFER_TIMESTAMP (buffer)) {
261 buffer = gst_buffer_make_writable (buffer);
262 GST_BUFFER_TIMESTAMP (buffer) = newts;
265 if (!funnel->has_segment) {
266 event = gst_event_new_new_segment (FALSE, 1.0, 1.0, GST_FORMAT_TIME,
268 funnel->has_segment = TRUE;
270 GST_OBJECT_UNLOCK (funnel);
273 if (!gst_pad_push_event (funnel->srcpad, event))
274 GST_WARNING_OBJECT (funnel, "Could not push out newsegment event");
277 GST_OBJECT_LOCK (pad);
278 padcaps = GST_PAD_CAPS (funnel->srcpad);
279 GST_OBJECT_UNLOCK (pad);
281 if (GST_BUFFER_CAPS (buffer) && GST_BUFFER_CAPS (buffer) != padcaps) {
282 if (!gst_pad_set_caps (funnel->srcpad, GST_BUFFER_CAPS (buffer))) {
283 res = GST_FLOW_NOT_NEGOTIATED;
289 res = gst_pad_push (funnel->srcpad, buffer);
291 GST_LOG_OBJECT (funnel, "handled buffer %s", gst_flow_get_name (res));
296 gst_object_unref (funnel);
302 gst_funnel_sink_event (GstPad * pad, GstEvent * event)
304 GstFunnel *funnel = GST_FUNNEL (gst_pad_get_parent (pad));
305 GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
306 gboolean forward = TRUE;
309 if (G_UNLIKELY (funnel == NULL)) {
310 gst_event_unref (event);
314 switch (GST_EVENT_TYPE (event)) {
315 case GST_EVENT_NEWSEGMENT:
324 gst_event_parse_new_segment (event, &update, &rate, &arate,
325 &format, &start, &stop, &time);
327 GST_OBJECT_LOCK (funnel);
328 gst_segment_set_newsegment (&fpad->segment, update, rate, arate,
329 format, start, stop, time);
330 GST_OBJECT_UNLOCK (funnel);
335 case GST_EVENT_FLUSH_STOP:
337 GST_OBJECT_LOCK (funnel);
338 gst_segment_init (&fpad->segment, GST_FORMAT_UNDEFINED);
339 funnel->has_segment = FALSE;
340 GST_OBJECT_UNLOCK (funnel);
348 res = gst_pad_push_event (funnel->srcpad, event);
350 gst_event_unref (event);
352 gst_object_unref (funnel);
358 gst_funnel_src_event (GstPad * pad, GstEvent * event)
363 gboolean result = FALSE;
364 gboolean done = FALSE;
365 GValue value = { 0, };
367 funnel = gst_pad_get_parent_element (pad);
368 if (G_UNLIKELY (funnel == NULL)) {
369 gst_event_unref (event);
373 iter = gst_element_iterate_sink_pads (funnel);
376 switch (gst_iterator_next (iter, &value)) {
377 case GST_ITERATOR_OK:
378 sinkpad = g_value_get_object (&value);
379 gst_event_ref (event);
380 result |= gst_pad_push_event (sinkpad, event);
381 g_value_reset (&value);
383 case GST_ITERATOR_RESYNC:
384 gst_iterator_resync (iter);
387 case GST_ITERATOR_ERROR:
388 GST_WARNING_OBJECT (funnel, "Error iterating sinkpads");
389 case GST_ITERATOR_DONE:
394 g_value_unset (&value);
395 gst_iterator_free (iter);
396 gst_object_unref (funnel);
397 gst_event_unref (event);
403 reset_pad (const GValue * data, gpointer user_data)
405 GstPad *pad = g_value_get_object (data);
406 GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
408 GST_OBJECT_LOCK (pad);
409 gst_funnel_pad_reset (fpad);
410 GST_OBJECT_UNLOCK (pad);
413 static GstStateChangeReturn
414 gst_funnel_change_state (GstElement * element, GstStateChange transition)
416 GstFunnel *funnel = GST_FUNNEL (element);
417 GstStateChangeReturn ret;
419 switch (transition) {
420 case GST_STATE_CHANGE_READY_TO_PAUSED:
422 GstIterator *iter = gst_element_iterate_sink_pads (element);
423 GstIteratorResult res;
426 res = gst_iterator_foreach (iter, reset_pad, NULL);
427 } while (res == GST_ITERATOR_RESYNC);
429 gst_iterator_free (iter);
431 if (res == GST_ITERATOR_ERROR)
432 return GST_STATE_CHANGE_FAILURE;
434 GST_OBJECT_LOCK (funnel);
435 funnel->has_segment = FALSE;
436 GST_OBJECT_UNLOCK (funnel);
443 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);