typefind: Only push a CAPS event downstream if the sinkpad is not in PULL mode
[platform/upstream/gstreamer.git] / plugins / elements / gstfunnel.c
1 /*
2  * GStreamer Funnel element
3  *
4  * Copyright 2007 Collabora Ltd.
5  *  @author: Olivier Crete <olivier.crete@collabora.co.uk>
6  * Copyright 2007 Nokia Corp.
7  *
8  * gstfunnel.c: Simple Funnel element
9  *
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.
14  *
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.
19  *
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
23  */
24
25 /**
26  * SECTION:element-funnel
27  *
28  * Takes packets from various input sinks into one output source.
29  *
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.
36  *
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "gstfunnel.h"
44
45 GST_DEBUG_CATEGORY_STATIC (gst_funnel_debug);
46 #define GST_CAT_DEFAULT gst_funnel_debug
47
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))
61
62 typedef struct _GstFunnelPad GstFunnelPad;
63 typedef struct _GstFunnelPadClass GstFunnelPadClass;
64
65 struct _GstFunnelPad
66 {
67   GstPad parent;
68
69   gboolean got_eos;
70 };
71
72 struct _GstFunnelPadClass
73 {
74   GstPadClass parent;
75 };
76
77 G_DEFINE_TYPE (GstFunnelPad, gst_funnel_pad, GST_TYPE_PAD);
78
79 #define DEFAULT_FORWARD_STICKY_EVENTS   TRUE
80
81 enum
82 {
83   PROP_0,
84   PROP_FORWARD_STICKY_EVENTS
85 };
86
87 static void
88 gst_funnel_pad_class_init (GstFunnelPadClass * klass)
89 {
90 }
91
92 static void
93 gst_funnel_pad_init (GstFunnelPad * pad)
94 {
95   pad->got_eos = FALSE;
96 }
97
98 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
99     GST_PAD_SINK,
100     GST_PAD_REQUEST,
101     GST_STATIC_CAPS_ANY);
102
103 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
104     GST_PAD_SRC,
105     GST_PAD_ALWAYS,
106     GST_STATIC_CAPS_ANY);
107
108 #define _do_init \
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);
112
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);
118
119 static GstFlowReturn gst_funnel_sink_chain (GstPad * pad, GstObject * parent,
120     GstBuffer * buffer);
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,
124     GstEvent * event);
125
126 static void
127 gst_funnel_set_property (GObject * object, guint prop_id,
128     const GValue * value, GParamSpec * pspec)
129 {
130   GstFunnel *funnel = GST_FUNNEL (object);
131
132   switch (prop_id) {
133     case PROP_FORWARD_STICKY_EVENTS:
134       funnel->forward_sticky_events = g_value_get_boolean (value);
135       break;
136     default:
137       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138       break;
139   }
140 }
141
142 static void
143 gst_funnel_get_property (GObject * object, guint prop_id, GValue * value,
144     GParamSpec * pspec)
145 {
146   GstFunnel *funnel = GST_FUNNEL (object);
147
148   switch (prop_id) {
149     case PROP_FORWARD_STICKY_EVENTS:
150       g_value_set_boolean (value, funnel->forward_sticky_events);
151       break;
152     default:
153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154       break;
155   }
156 }
157
158 static void
159 gst_funnel_dispose (GObject * object)
160 {
161   GstFunnel *funnel = GST_FUNNEL (object);
162   GList *item;
163
164   gst_object_replace ((GstObject **) & funnel->last_sinkpad, NULL);
165
166 restart:
167   for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
168     GstPad *pad = GST_PAD (item->data);
169
170     if (GST_PAD_IS_SINK (pad)) {
171       gst_element_release_request_pad (GST_ELEMENT (object), pad);
172       goto restart;
173     }
174   }
175
176   G_OBJECT_CLASS (parent_class)->dispose (object);
177 }
178
179 static void
180 gst_funnel_class_init (GstFunnelClass * klass)
181 {
182   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
183   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
184
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);
188
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));
195
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>");
199
200   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
201   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
202
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);
207 }
208
209 static void
210 gst_funnel_init (GstFunnel * funnel)
211 {
212   funnel->srcpad = gst_pad_new_from_static_template (&src_template, "src");
213   gst_pad_use_fixed_caps (funnel->srcpad);
214
215   gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
216
217   funnel->forward_sticky_events = DEFAULT_FORWARD_STICKY_EVENTS;
218 }
219
220 static GstPad *
221 gst_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
222     const gchar * name, const GstCaps * caps)
223 {
224   GstPad *sinkpad;
225
226   GST_DEBUG_OBJECT (element, "requesting pad");
227
228   sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_FUNNEL_PAD,
229           "name", name, "direction", templ->direction, "template", templ,
230           NULL));
231
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));
238
239   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
240   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
241
242   gst_pad_set_active (sinkpad, TRUE);
243
244   gst_element_add_pad (element, sinkpad);
245
246   GST_DEBUG_OBJECT (element, "requested pad %s:%s",
247       GST_DEBUG_PAD_NAME (sinkpad));
248
249   return sinkpad;
250 }
251
252 static gboolean
253 gst_funnel_all_sinkpads_eos_unlocked (GstFunnel * funnel, GstPad * pad)
254 {
255   GstElement *element = GST_ELEMENT_CAST (funnel);
256   GList *item;
257   gboolean all_eos = FALSE;
258
259
260   if (element->numsinkpads == 0)
261     goto done;
262
263   for (item = element->sinkpads; item != NULL; item = g_list_next (item)) {
264     GstFunnelPad *sinkpad = GST_FUNNEL_PAD_CAST (item->data);
265
266     if (!sinkpad->got_eos) {
267       return FALSE;
268     }
269   }
270
271   all_eos = TRUE;
272
273 done:
274   return all_eos;
275 }
276
277 static void
278 gst_funnel_release_pad (GstElement * element, GstPad * pad)
279 {
280   GstFunnel *funnel = GST_FUNNEL (element);
281   GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
282   gboolean got_eos;
283   gboolean send_eos = FALSE;
284
285   GST_DEBUG_OBJECT (funnel, "releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
286
287   gst_pad_set_active (pad, FALSE);
288
289   got_eos = fpad->got_eos;
290
291   gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
292
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");
296     send_eos = TRUE;
297   }
298   GST_OBJECT_UNLOCK (funnel);
299
300   if (send_eos)
301     if (!gst_pad_push_event (funnel->srcpad, gst_event_new_eos ()))
302       GST_WARNING_OBJECT (funnel, "Failure pushing EOS");
303 }
304
305 static gboolean
306 forward_events (GstPad * pad, GstEvent ** event, gpointer user_data)
307 {
308   GstPad *srcpad = user_data;
309
310   if (GST_EVENT_TYPE (*event) != GST_EVENT_EOS)
311     gst_pad_push_event (srcpad, gst_event_ref (*event));
312
313   return TRUE;
314 }
315
316 static GstFlowReturn
317 gst_funnel_sink_chain_object (GstPad * pad, GstFunnel * funnel,
318     gboolean is_list, GstMiniObject * obj)
319 {
320   GstFlowReturn res;
321
322   GST_DEBUG_OBJECT (pad, "received %" GST_PTR_FORMAT, obj);
323
324   GST_PAD_STREAM_LOCK (funnel->srcpad);
325
326   if ((funnel->last_sinkpad == NULL) || (funnel->forward_sticky_events
327           && (funnel->last_sinkpad != pad))) {
328     gst_object_replace ((GstObject **) & funnel->last_sinkpad,
329         GST_OBJECT (pad));
330
331     GST_DEBUG_OBJECT (pad, "Forwarding sticky events");
332     gst_pad_sticky_events_foreach (pad, forward_events, funnel->srcpad);
333   }
334
335   if (is_list)
336     res = gst_pad_push_list (funnel->srcpad, GST_BUFFER_LIST_CAST (obj));
337   else
338     res = gst_pad_push (funnel->srcpad, GST_BUFFER_CAST (obj));
339
340   GST_PAD_STREAM_UNLOCK (funnel->srcpad);
341
342   GST_LOG_OBJECT (pad, "handled buffer%s %s", (is_list ? "list" : ""),
343       gst_flow_get_name (res));
344
345   return res;
346 }
347
348 static GstFlowReturn
349 gst_funnel_sink_chain_list (GstPad * pad, GstObject * parent,
350     GstBufferList * list)
351 {
352   GstFunnel *funnel = GST_FUNNEL (parent);
353
354   return gst_funnel_sink_chain_object (pad, funnel, TRUE,
355       GST_MINI_OBJECT_CAST (list));
356 }
357
358 static GstFlowReturn
359 gst_funnel_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
360 {
361   GstFunnel *funnel = GST_FUNNEL (parent);
362
363   return gst_funnel_sink_chain_object (pad, funnel, FALSE,
364       GST_MINI_OBJECT_CAST (buffer));
365 }
366
367 static gboolean
368 gst_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
369 {
370   GstFunnel *funnel = GST_FUNNEL (parent);
371   GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
372   gboolean forward = TRUE;
373   gboolean res = TRUE;
374   gboolean unlock = FALSE;
375
376   GST_DEBUG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
377
378   if (GST_EVENT_IS_STICKY (event)) {
379     unlock = TRUE;
380     GST_PAD_STREAM_LOCK (funnel->srcpad);
381
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)) {
386         forward = FALSE;
387       } else {
388         forward = TRUE;
389       }
390       GST_OBJECT_UNLOCK (funnel);
391     } else if (pad != funnel->last_sinkpad) {
392       forward = FALSE;
393     }
394   } else if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
395     unlock = TRUE;
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. */
402     unlock = TRUE;
403     GST_PAD_STREAM_LOCK (funnel->srcpad);
404     GST_OBJECT_LOCK (funnel);
405     gst_object_replace ((GstObject **) & funnel->last_sinkpad,
406         GST_OBJECT (pad));
407     GST_OBJECT_UNLOCK (funnel);
408     gst_pad_sticky_events_foreach (pad, forward_events, funnel->srcpad);
409   }
410
411   if (forward)
412     res = gst_pad_push_event (funnel->srcpad, event);
413   else
414     gst_event_unref (event);
415
416   if (unlock)
417     GST_PAD_STREAM_UNLOCK (funnel->srcpad);
418
419   return res;
420 }
421
422 static void
423 reset_pad (const GValue * data, gpointer user_data)
424 {
425   GstPad *pad = g_value_get_object (data);
426   GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
427   GstFunnel *funnel = user_data;
428
429   GST_OBJECT_LOCK (funnel);
430   fpad->got_eos = FALSE;
431   GST_OBJECT_UNLOCK (funnel);
432 }
433
434 static GstStateChangeReturn
435 gst_funnel_change_state (GstElement * element, GstStateChange transition)
436 {
437   GstStateChangeReturn ret;
438
439   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
440   if (ret == GST_STATE_CHANGE_FAILURE)
441     return ret;
442
443   switch (transition) {
444     case GST_STATE_CHANGE_PAUSED_TO_READY:
445     {
446       GstIterator *iter = gst_element_iterate_sink_pads (element);
447       GstIteratorResult res;
448
449       do {
450         res = gst_iterator_foreach (iter, reset_pad, element);
451         if (res == GST_ITERATOR_RESYNC)
452           gst_iterator_resync (iter);
453       } while (res == GST_ITERATOR_RESYNC);
454       gst_iterator_free (iter);
455
456       if (res == GST_ITERATOR_ERROR)
457         return GST_STATE_CHANGE_FAILURE;
458
459     }
460       break;
461     default:
462       break;
463   }
464
465   return ret;
466 }