Merge branch 'master' into 0.11
[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   GstSegment segment;
70 };
71
72 struct _GstFunnelPadClass
73 {
74   GstPadClass parent;
75 };
76
77 G_DEFINE_TYPE (GstFunnelPad, gst_funnel_pad, GST_TYPE_PAD);
78
79 static void
80 gst_funnel_pad_class_init (GstFunnelPadClass * klass)
81 {
82 }
83
84 static void
85 gst_funnel_pad_reset (GstFunnelPad * pad)
86 {
87   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
88 }
89
90 static void
91 gst_funnel_pad_init (GstFunnelPad * pad)
92 {
93   gst_funnel_pad_reset (pad);
94 }
95
96 static GstStaticPadTemplate funnel_sink_template =
97 GST_STATIC_PAD_TEMPLATE ("sink_%u",
98     GST_PAD_SINK,
99     GST_PAD_REQUEST,
100     GST_STATIC_CAPS_ANY);
101
102 static GstStaticPadTemplate funnel_src_template =
103 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 gboolean gst_funnel_sink_event (GstPad * pad, GstObject * parent,
122     GstEvent * event);
123 static gboolean gst_funnel_sink_query (GstPad * pad, GstObject * parent,
124     GstQuery * query);
125
126 static gboolean gst_funnel_src_event (GstPad * pad, GstObject * parent,
127     GstEvent * event);
128
129 static void
130 gst_funnel_dispose (GObject * object)
131 {
132   GList *item;
133
134 restart:
135   for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
136     GstPad *pad = GST_PAD (item->data);
137
138     if (GST_PAD_IS_SINK (pad)) {
139       gst_element_release_request_pad (GST_ELEMENT (object), pad);
140       goto restart;
141     }
142   }
143
144   G_OBJECT_CLASS (parent_class)->dispose (object);
145 }
146
147 static void
148 gst_funnel_class_init (GstFunnelClass * klass)
149 {
150   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
152
153   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_funnel_dispose);
154
155   gst_element_class_set_details_simple (gstelement_class,
156       "Funnel pipe fitting", "Generic", "N-to-1 pipe fitting",
157       "Olivier Crete <olivier.crete@collabora.co.uk>");
158
159   gst_element_class_add_pad_template (gstelement_class,
160       gst_static_pad_template_get (&funnel_sink_template));
161   gst_element_class_add_pad_template (gstelement_class,
162       gst_static_pad_template_get (&funnel_src_template));
163
164   gstelement_class->request_new_pad =
165       GST_DEBUG_FUNCPTR (gst_funnel_request_new_pad);
166   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_funnel_release_pad);
167   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_funnel_change_state);
168 }
169
170 static void
171 gst_funnel_init (GstFunnel * funnel)
172 {
173   funnel->srcpad = gst_pad_new_from_static_template (&funnel_src_template,
174       "src");
175   gst_pad_set_event_function (funnel->srcpad, gst_funnel_src_event);
176   gst_pad_use_fixed_caps (funnel->srcpad);
177   gst_element_add_pad (GST_ELEMENT (funnel), funnel->srcpad);
178 }
179
180 static GstPad *
181 gst_funnel_request_new_pad (GstElement * element, GstPadTemplate * templ,
182     const gchar * name, const GstCaps * caps)
183 {
184   GstPad *sinkpad;
185
186   GST_DEBUG_OBJECT (element, "requesting pad");
187
188   sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_FUNNEL_PAD,
189           "name", name, "direction", templ->direction, "template", templ,
190           NULL));
191
192   gst_pad_set_chain_function (sinkpad,
193       GST_DEBUG_FUNCPTR (gst_funnel_sink_chain));
194   gst_pad_set_event_function (sinkpad,
195       GST_DEBUG_FUNCPTR (gst_funnel_sink_event));
196   gst_pad_set_query_function (sinkpad,
197       GST_DEBUG_FUNCPTR (gst_funnel_sink_query));
198
199   gst_pad_set_active (sinkpad, TRUE);
200
201   gst_element_add_pad (element, sinkpad);
202
203   return sinkpad;
204 }
205
206 static void
207 gst_funnel_release_pad (GstElement * element, GstPad * pad)
208 {
209   GstFunnel *funnel = GST_FUNNEL (element);
210
211   GST_DEBUG_OBJECT (funnel, "releasing pad");
212
213   gst_pad_set_active (pad, FALSE);
214
215   gst_element_remove_pad (GST_ELEMENT_CAST (funnel), pad);
216 }
217
218 static GstFlowReturn
219 gst_funnel_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
220 {
221   GstFlowReturn res;
222   GstFunnel *funnel = GST_FUNNEL (parent);
223   GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
224   GstEvent *event = NULL;
225   GstClockTime newts;
226 #if 0
227   GstCaps *padcaps;
228 #endif
229
230   GST_DEBUG_OBJECT (funnel, "received buffer %p", buffer);
231
232   GST_OBJECT_LOCK (funnel);
233   if (fpad->segment.format == GST_FORMAT_UNDEFINED) {
234     GST_WARNING_OBJECT (funnel, "Got buffer without segment,"
235         " setting segment [0,inf[");
236     gst_segment_init (&fpad->segment, GST_FORMAT_TIME);
237   }
238
239   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buffer)))
240     fpad->segment.position = GST_BUFFER_TIMESTAMP (buffer);
241
242   newts = gst_segment_to_running_time (&fpad->segment,
243       fpad->segment.format, GST_BUFFER_TIMESTAMP (buffer));
244   if (newts != GST_BUFFER_TIMESTAMP (buffer)) {
245     buffer = gst_buffer_make_writable (buffer);
246     GST_BUFFER_TIMESTAMP (buffer) = newts;
247   }
248
249   if (!funnel->has_segment) {
250     GstSegment segment;
251
252     gst_segment_init (&segment, GST_FORMAT_TIME);
253     event = gst_event_new_segment (&segment);
254     funnel->has_segment = TRUE;
255   }
256   GST_OBJECT_UNLOCK (funnel);
257
258   if (event) {
259     if (!gst_pad_push_event (funnel->srcpad, event))
260       GST_WARNING_OBJECT (funnel, "Could not push out newsegment event");
261   }
262 #if 0
263   GST_OBJECT_LOCK (pad);
264   padcaps = GST_PAD_CAPS (funnel->srcpad);
265   GST_OBJECT_UNLOCK (pad);
266
267   if (GST_BUFFER_CAPS (buffer) && GST_BUFFER_CAPS (buffer) != padcaps) {
268     if (!gst_pad_set_caps (funnel->srcpad, GST_BUFFER_CAPS (buffer))) {
269       res = GST_FLOW_NOT_NEGOTIATED;
270       goto out;
271     }
272   }
273 #endif
274
275   res = gst_pad_push (funnel->srcpad, buffer);
276
277   GST_LOG_OBJECT (funnel, "handled buffer %s", gst_flow_get_name (res));
278
279 #if 0
280 out:
281 #endif
282
283   return res;
284 }
285
286 static gboolean
287 gst_funnel_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
288 {
289   GstFunnel *funnel = GST_FUNNEL (parent);
290   GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
291   gboolean forward = TRUE;
292   gboolean res = TRUE;
293
294   switch (GST_EVENT_TYPE (event)) {
295     case GST_EVENT_SEGMENT:
296     {
297       GST_OBJECT_LOCK (funnel);
298       gst_event_copy_segment (event, &fpad->segment);
299       GST_OBJECT_UNLOCK (funnel);
300
301       forward = FALSE;
302       break;
303     }
304     case GST_EVENT_FLUSH_STOP:
305     {
306       GST_OBJECT_LOCK (funnel);
307       gst_segment_init (&fpad->segment, GST_FORMAT_UNDEFINED);
308       funnel->has_segment = FALSE;
309       GST_OBJECT_UNLOCK (funnel);
310       break;
311     }
312     default:
313       break;
314   }
315
316   if (forward)
317     res = gst_pad_push_event (funnel->srcpad, event);
318   else
319     gst_event_unref (event);
320
321   return res;
322 }
323
324 static gboolean
325 gst_funnel_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
326 {
327   GstFunnel *funnel = GST_FUNNEL (parent);
328   gboolean forward = TRUE;
329   gboolean res = TRUE;
330
331   if (forward)
332     res = gst_pad_peer_query (funnel->srcpad, query);
333
334   return res;
335 }
336
337 static gboolean
338 gst_funnel_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
339 {
340   GstElement *funnel;
341   GstIterator *iter;
342   GstPad *sinkpad;
343   gboolean result = FALSE;
344   gboolean done = FALSE;
345   GValue value = { 0, };
346
347   funnel = GST_ELEMENT_CAST (parent);
348
349   iter = gst_element_iterate_sink_pads (funnel);
350
351   while (!done) {
352     switch (gst_iterator_next (iter, &value)) {
353       case GST_ITERATOR_OK:
354         sinkpad = g_value_get_object (&value);
355         gst_event_ref (event);
356         result |= gst_pad_push_event (sinkpad, event);
357         g_value_reset (&value);
358         break;
359       case GST_ITERATOR_RESYNC:
360         gst_iterator_resync (iter);
361         result = FALSE;
362         break;
363       case GST_ITERATOR_ERROR:
364         GST_WARNING_OBJECT (funnel, "Error iterating sinkpads");
365       case GST_ITERATOR_DONE:
366         done = TRUE;
367         break;
368     }
369   }
370   g_value_unset (&value);
371   gst_iterator_free (iter);
372   gst_event_unref (event);
373
374   return result;
375 }
376
377 static void
378 reset_pad (const GValue * data, gpointer user_data)
379 {
380   GstPad *pad = g_value_get_object (data);
381   GstFunnelPad *fpad = GST_FUNNEL_PAD_CAST (pad);
382
383   GST_OBJECT_LOCK (pad);
384   gst_funnel_pad_reset (fpad);
385   GST_OBJECT_UNLOCK (pad);
386 }
387
388 static GstStateChangeReturn
389 gst_funnel_change_state (GstElement * element, GstStateChange transition)
390 {
391   GstFunnel *funnel = GST_FUNNEL (element);
392   GstStateChangeReturn ret;
393
394   switch (transition) {
395     case GST_STATE_CHANGE_READY_TO_PAUSED:
396     {
397       GstIterator *iter = gst_element_iterate_sink_pads (element);
398       GstIteratorResult res;
399
400       do {
401         res = gst_iterator_foreach (iter, reset_pad, NULL);
402       } while (res == GST_ITERATOR_RESYNC);
403
404       gst_iterator_free (iter);
405
406       if (res == GST_ITERATOR_ERROR)
407         return GST_STATE_CHANGE_FAILURE;
408
409       GST_OBJECT_LOCK (funnel);
410       funnel->has_segment = FALSE;
411       GST_OBJECT_UNLOCK (funnel);
412     }
413       break;
414     default:
415       break;
416   }
417
418   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
419
420   return ret;
421 }