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);
79 #define DEFAULT_FORWARD_STICKY_EVENTS TRUE
84 PROP_FORWARD_STICKY_EVENTS
88 gst_funnel_pad_class_init (GstFunnelPadClass * klass)
93 gst_funnel_pad_init (GstFunnelPad * pad)
98 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
101 GST_STATIC_CAPS_ANY);
103 static GstStaticPadTemplate src_template = 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, GstObject * parent,
121 static GstFlowReturn gst_funnel_sink_chain_list (GstPad * pad,
122 GstObject * parent, GstBufferList * list);
123 static gboolean gst_funnel_sink_event (GstPad * pad, GstObject * parent,
127 gst_funnel_set_property (GObject * object, guint prop_id,
128 const GValue * value, GParamSpec * pspec)
130 GstFunnel *funnel = GST_FUNNEL (object);
133 case PROP_FORWARD_STICKY_EVENTS:
134 funnel->forward_sticky_events = g_value_get_boolean (value);
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 gst_funnel_get_property (GObject * object, guint prop_id, GValue * value,
146 GstFunnel *funnel = GST_FUNNEL (object);
149 case PROP_FORWARD_STICKY_EVENTS:
150 g_value_set_boolean (value, funnel->forward_sticky_events);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gst_funnel_dispose (GObject * object)
161 GstFunnel *funnel = GST_FUNNEL (object);
164 gst_object_replace ((GstObject **) & funnel->last_sinkpad, NULL);
167 for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
168 GstPad *pad = GST_PAD (item->data);
170 if (GST_PAD_IS_SINK (pad)) {
171 gst_element_release_request_pad (GST_ELEMENT (object), pad);
176 G_OBJECT_CLASS (parent_class)->dispose (object);
180 gst_funnel_class_init (GstFunnelClass * klass)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
185 gobject_class->set_property = gst_funnel_set_property;
186 gobject_class->get_property = gst_funnel_get_property;
187 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_funnel_dispose);
189 g_object_class_install_property (gobject_class, PROP_FORWARD_STICKY_EVENTS,
190 g_param_spec_boolean ("forward-sticky-events", "Forward sticky events",
191 "Forward sticky events on stream changes",
192 DEFAULT_FORWARD_STICKY_EVENTS,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
194 GST_PARAM_MUTABLE_READY));
196 gst_element_class_set_static_metadata (gstelement_class,
197 "Funnel pipe fitting", "Generic", "N-to-1 pipe fitting",
198 "Olivier Crete <olivier.crete@collabora.co.uk>");
200 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
201 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
203 gstelement_class->request_new_pad =
204 GST_DEBUG_FUNCPTR (gst_funnel_request_new_pad);
205 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_funnel_release_pad);
206 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_funnel_change_state);
210 gst_funnel_init (GstFunnel * funnel)
212 funnel->srcpad = gst_pad_new_from_static_template (&src_template, "src");
213 gst_pad_use_fixed_caps (funnel->srcpad);
215 gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
217 funnel->forward_sticky_events = DEFAULT_FORWARD_STICKY_EVENTS;
221 gst_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
222 const gchar * name, const GstCaps * caps)
226 GST_DEBUG_OBJECT (element, "requesting pad");
228 sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_FUNNEL_PAD,
229 "name", name, "direction", templ->direction, "template", templ,
232 gst_pad_set_chain_function (sinkpad,
233 GST_DEBUG_FUNCPTR (gst_funnel_sink_chain));
234 gst_pad_set_chain_list_function (sinkpad,
235 GST_DEBUG_FUNCPTR (gst_funnel_sink_chain_list));
236 gst_pad_set_event_function (sinkpad,
237 GST_DEBUG_FUNCPTR (gst_funnel_sink_event));
239 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
240 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
242 gst_pad_set_active (sinkpad, TRUE);
244 gst_element_add_pad (element, sinkpad);
246 GST_DEBUG_OBJECT (element, "requested pad %s:%s",
247 GST_DEBUG_PAD_NAME (sinkpad));
253 gst_funnel_all_sinkpads_eos_unlocked (GstFunnel * funnel, GstPad * pad)
255 GstElement *element = GST_ELEMENT_CAST (funnel);
257 gboolean all_eos = FALSE;
260 if (element->numsinkpads == 0)
263 for (item = element->sinkpads; item != NULL; item = g_list_next (item)) {
264 GstFunnelPad *sinkpad = GST_FUNNEL_PAD_CAST (item->data);
266 if (!sinkpad->got_eos) {
278 gst_funnel_release_pad (GstElement * element, GstPad * pad)
280 GstFunnel *funnel = GST_FUNNEL (element);
281 GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
283 gboolean send_eos = FALSE;
285 GST_DEBUG_OBJECT (funnel, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
287 gst_pad_set_active (pad, FALSE);
289 got_eos = fpad->got_eos;
291 gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
293 GST_OBJECT_LOCK (funnel);
294 if (!got_eos && gst_funnel_all_sinkpads_eos_unlocked (funnel, NULL)) {
295 GST_DEBUG_OBJECT (funnel, "Pad removed. All others are EOS. Sending EOS");
298 GST_OBJECT_UNLOCK (funnel);
301 if (!gst_pad_push_event (funnel->srcpad, gst_event_new_eos ()))
302 GST_WARNING_OBJECT (funnel, "Failure pushing EOS");
306 forward_events (GstPad * pad, GstEvent ** event, gpointer user_data)
308 GstPad *srcpad = user_data;
310 if (GST_EVENT_TYPE (*event) != GST_EVENT_EOS)
311 gst_pad_push_event (srcpad, gst_event_ref (*event));
317 gst_funnel_sink_chain_object (GstPad * pad, GstFunnel * funnel,
318 gboolean is_list, GstMiniObject * obj)
322 GST_DEBUG_OBJECT (pad, "received %" GST_PTR_FORMAT, obj);
324 GST_PAD_STREAM_LOCK (funnel->srcpad);
326 if ((funnel->last_sinkpad == NULL) || (funnel->forward_sticky_events
327 && (funnel->last_sinkpad != pad))) {
328 gst_object_replace ((GstObject **) & funnel->last_sinkpad,
331 GST_DEBUG_OBJECT (pad, "Forwarding sticky events");
332 gst_pad_sticky_events_foreach (pad, forward_events, funnel->srcpad);
336 res = gst_pad_push_list (funnel->srcpad, GST_BUFFER_LIST_CAST (obj));
338 res = gst_pad_push (funnel->srcpad, GST_BUFFER_CAST (obj));
340 GST_PAD_STREAM_UNLOCK (funnel->srcpad);
342 GST_LOG_OBJECT (pad, "handled buffer%s %s", (is_list ? "list" : ""),
343 gst_flow_get_name (res));
349 gst_funnel_sink_chain_list (GstPad * pad, GstObject * parent,
350 GstBufferList * list)
352 GstFunnel *funnel = GST_FUNNEL (parent);
354 return gst_funnel_sink_chain_object (pad, funnel, TRUE,
355 GST_MINI_OBJECT_CAST (list));
359 gst_funnel_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
361 GstFunnel *funnel = GST_FUNNEL (parent);
363 return gst_funnel_sink_chain_object (pad, funnel, FALSE,
364 GST_MINI_OBJECT_CAST (buffer));
368 gst_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
370 GstFunnel *funnel = GST_FUNNEL (parent);
371 GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
372 gboolean forward = TRUE;
374 gboolean unlock = FALSE;
376 GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
378 if (GST_EVENT_IS_STICKY (event)) {
380 GST_PAD_STREAM_LOCK (funnel->srcpad);
382 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
383 GST_OBJECT_LOCK (funnel);
384 fpad->got_eos = TRUE;
385 if (!gst_funnel_all_sinkpads_eos_unlocked (funnel, pad)) {
390 GST_OBJECT_UNLOCK (funnel);
391 } else if (pad != funnel->last_sinkpad) {
394 } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
396 GST_PAD_STREAM_LOCK (funnel->srcpad);
397 GST_OBJECT_LOCK (funnel);
398 fpad->got_eos = FALSE;
399 GST_OBJECT_UNLOCK (funnel);
400 } else if (GST_EVENT_TYPE (event) == GST_EVENT_GAP) {
401 /* If no data is coming and we receive GAP event, need to forward sticky events. */
403 GST_PAD_STREAM_LOCK (funnel->srcpad);
405 if ((funnel->last_sinkpad == NULL) || (funnel->forward_sticky_events
406 && (funnel->last_sinkpad != pad))) {
407 gst_object_replace ((GstObject **) & funnel->last_sinkpad,
409 gst_pad_sticky_events_foreach (pad, forward_events, funnel->srcpad);
414 res = gst_pad_push_event (funnel->srcpad, event);
416 gst_event_unref (event);
419 GST_PAD_STREAM_UNLOCK (funnel->srcpad);
425 reset_pad (const GValue * data, gpointer user_data)
427 GstPad *pad = g_value_get_object (data);
428 GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
429 GstFunnel *funnel = user_data;
431 GST_OBJECT_LOCK (funnel);
432 fpad->got_eos = FALSE;
433 GST_OBJECT_UNLOCK (funnel);
436 static GstStateChangeReturn
437 gst_funnel_change_state (GstElement * element, GstStateChange transition)
439 GstStateChangeReturn ret;
441 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
442 if (ret == GST_STATE_CHANGE_FAILURE)
445 switch (transition) {
446 case GST_STATE_CHANGE_PAUSED_TO_READY:
448 GstIterator *iter = gst_element_iterate_sink_pads (element);
449 GstIteratorResult res;
452 res = gst_iterator_foreach (iter, reset_pad, element);
453 if (res == GST_ITERATOR_RESYNC)
454 gst_iterator_resync (iter);
455 } while (res == GST_ITERATOR_RESYNC);
456 gst_iterator_free (iter);
458 if (res == GST_ITERATOR_ERROR)
459 return GST_STATE_CHANGE_FAILURE;