a hack to work around intltool's brokenness a current check for mpeg2dec details...
[platform/upstream/gstreamer.git] / gst / elements / gstaggregator.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gstaggregator.c: Aggregator element, N in 1 out
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "gstaggregator.h"
24
25
26 GstElementDetails gst_aggregator_details = {
27   "Aggregator pipe fitting",
28   "Generic",
29   "N-to-1 pipe fitting",
30   VERSION,
31   "Wim Taymans <wim.taymans@chello.be>",
32   "(C) 2001",
33 };
34
35 /* Aggregator signals and args */
36 enum {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum {
42   ARG_0,
43   ARG_NUM_PADS,
44   ARG_SILENT,
45   ARG_SCHED,
46   ARG_LAST_MESSAGE,
47   /* FILL ME */
48 };
49
50 GST_PAD_TEMPLATE_FACTORY (aggregator_src_factory,
51   "sink%d",
52   GST_PAD_SINK,
53   GST_PAD_REQUEST,
54   NULL                  /* no caps */
55 );
56
57 #define GST_TYPE_AGGREGATOR_SCHED (gst_aggregator_sched_get_type())
58 static GType
59 gst_aggregator_sched_get_type (void)
60 {
61   static GType aggregator_sched_type = 0;
62   static GEnumValue aggregator_sched[] = {
63     { AGGREGATOR_LOOP,          "1", "Loop Based"},
64     { AGGREGATOR_LOOP_PEEK,     "2", "Loop Based Peek"},
65     { AGGREGATOR_LOOP_SELECT,   "3", "Loop Based Select"},
66     { AGGREGATOR_CHAIN,         "4", "Chain Based"},
67     {0, NULL, NULL},
68   };
69   if (!aggregator_sched_type) {
70     aggregator_sched_type = g_enum_register_static ("GstAggregatorSched", aggregator_sched);
71   }
72   return aggregator_sched_type;
73 }
74
75 #define AGGREGATOR_IS_LOOP_BASED(ag)    ((ag)->sched != AGGREGATOR_CHAIN)
76
77 static void     gst_aggregator_class_init       (GstAggregatorClass *klass);
78 static void     gst_aggregator_init             (GstAggregator *aggregator);
79
80 static GstPad*  gst_aggregator_request_new_pad  (GstElement *element, GstPadTemplate *temp, const
81                                                  gchar *unused);
82
83 static void     gst_aggregator_set_property     (GObject *object, guint prop_id, 
84                                                  const GValue *value, GParamSpec *pspec);
85 static void     gst_aggregator_get_property     (GObject *object, guint prop_id, 
86                                                  GValue *value, GParamSpec *pspec);
87
88 static void     gst_aggregator_chain            (GstPad *pad, GstBuffer *buf);
89 static void     gst_aggregator_loop             (GstElement *element);
90
91 static GstElementClass *parent_class = NULL;
92 /*static guint gst_aggregator_signals[LAST_SIGNAL] = { 0 };*/
93
94 GType
95 gst_aggregator_get_type (void) 
96 {
97   static GType aggregator_type = 0;
98
99   if (!aggregator_type) {
100     static const GTypeInfo aggregator_info = {
101       sizeof(GstAggregatorClass),      
102       NULL,
103       NULL,
104       (GClassInitFunc)gst_aggregator_class_init,
105       NULL,
106       NULL,
107       sizeof(GstAggregator),
108       0,
109       (GInstanceInitFunc)gst_aggregator_init,
110     };
111     aggregator_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAggregator", &aggregator_info, 0);
112   }
113   return aggregator_type;
114 }
115
116 static void
117 gst_aggregator_class_init (GstAggregatorClass *klass) 
118 {
119   GObjectClass *gobject_class;
120   GstElementClass *gstelement_class;
121
122   gobject_class = (GObjectClass*) klass;
123   gstelement_class = (GstElementClass*) klass;
124
125   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
126
127   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
128     g_param_spec_int ("num_pads", "num_pads", "num_pads",
129                       0, G_MAXINT, 0, G_PARAM_READABLE)); 
130   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
131     g_param_spec_boolean ("silent", "silent", "silent",
132                       FALSE, G_PARAM_READWRITE)); 
133   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SCHED,
134     g_param_spec_enum ("sched", "sched", "sched",
135                       GST_TYPE_AGGREGATOR_SCHED, AGGREGATOR_CHAIN, G_PARAM_READWRITE)); 
136   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
137     g_param_spec_string ("last_message", "last_message", "last_message",
138                          NULL, G_PARAM_READABLE));
139
140   gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_aggregator_set_property);
141   gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_aggregator_get_property);
142
143   gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_aggregator_request_new_pad);
144 }
145
146 static void 
147 gst_aggregator_init (GstAggregator *aggregator) 
148 {
149   aggregator->srcpad = gst_pad_new ("src", GST_PAD_SRC);
150   gst_element_add_pad (GST_ELEMENT (aggregator), aggregator->srcpad);
151
152   aggregator->numsinkpads = 0;
153   aggregator->sinkpads = NULL;
154   aggregator->silent = FALSE;
155   aggregator->sched = AGGREGATOR_LOOP;
156 }
157
158 static GstPad*
159 gst_aggregator_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *unused) 
160 {
161   gchar *name;
162   GstPad *sinkpad;
163   GstAggregator *aggregator;
164
165   g_return_val_if_fail (GST_IS_AGGREGATOR (element), NULL);
166
167   if (templ->direction != GST_PAD_SINK) {
168     g_warning ("gstaggregator: request new pad that is not a SRC pad\n");
169     return NULL;
170   }
171
172   aggregator = GST_AGGREGATOR (element);
173
174   name = g_strdup_printf ("sink%d",aggregator->numsinkpads);
175   
176   sinkpad = gst_pad_new_from_template (templ, name);
177   gst_pad_set_chain_function (sinkpad, gst_aggregator_chain);
178   gst_element_add_pad (GST_ELEMENT (aggregator), sinkpad);
179   
180   aggregator->sinkpads = g_list_prepend (aggregator->sinkpads, sinkpad);
181   aggregator->numsinkpads++;
182   
183   return sinkpad;
184 }
185
186 static void
187 gst_aggregator_update_functions (GstAggregator *aggregator)
188 {
189   GList *pads;
190
191   if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
192     gst_element_set_loop_function (GST_ELEMENT (aggregator), GST_DEBUG_FUNCPTR (gst_aggregator_loop));
193   }
194   else {
195     gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
196   }
197
198   pads = aggregator->sinkpads;
199   while (pads) {
200     GstPad *pad = GST_PAD (pads->data);
201                           
202     if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
203       gst_pad_set_get_function (pad, NULL);
204     }
205     else {
206       gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
207     }
208     pads = g_list_next (pads);
209   }
210 }
211
212 static void
213 gst_aggregator_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
214 {
215   GstAggregator *aggregator;
216
217   /* it's not null if we got it, but it might not be ours */
218   g_return_if_fail (GST_IS_AGGREGATOR (object));
219
220   aggregator = GST_AGGREGATOR (object);
221
222   switch (prop_id) {
223     case ARG_SILENT:
224       aggregator->silent = g_value_get_boolean (value);
225       break;
226     case ARG_SCHED:
227       aggregator->sched = g_value_get_enum (value);
228       gst_aggregator_update_functions (aggregator);
229       break;
230     default:
231       break;
232   }
233 }
234
235 static void
236 gst_aggregator_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
237 {
238   GstAggregator *aggregator;
239
240   /* it's not null if we got it, but it might not be ours */
241   g_return_if_fail (GST_IS_AGGREGATOR (object));
242
243   aggregator = GST_AGGREGATOR (object);
244
245   switch (prop_id) {
246     case ARG_NUM_PADS:
247       g_value_set_int (value, aggregator->numsinkpads);
248       break;
249     case ARG_SILENT:
250       g_value_set_boolean (value, aggregator->silent);
251       break;
252     case ARG_SCHED:
253       g_value_set_enum (value, aggregator->sched);
254       break;
255     case ARG_LAST_MESSAGE:
256       g_value_set_string (value, aggregator->last_message);
257       break;
258     default:
259       break;
260   }
261 }
262
263 static void 
264 gst_aggregator_push (GstAggregator *aggregator, GstPad *pad, GstBuffer *buf, guchar *debug) 
265 {
266   if (!aggregator->silent) {
267     g_free (aggregator->last_message);
268
269     aggregator->last_message = g_strdup_printf ("%10.10s ******* (%s:%s)a (%d bytes, %llu)",
270             debug, GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
271
272     g_object_notify (G_OBJECT (aggregator), "last_message");
273   }
274
275   gst_pad_push (aggregator->srcpad, buf);
276 }
277
278 static void 
279 gst_aggregator_loop (GstElement *element) 
280 {
281   GstAggregator *aggregator;
282   GstBuffer *buf;
283   guchar *debug;
284
285   aggregator = GST_AGGREGATOR (element);
286
287   if (aggregator->sched == AGGREGATOR_LOOP ||
288       aggregator->sched == AGGREGATOR_LOOP_PEEK) {
289     GList *pads = aggregator->sinkpads;
290
291     while (pads) {
292       GstPad *pad = GST_PAD (pads->data);
293       pads = g_list_next (pads);
294
295       if (aggregator->sched == AGGREGATOR_LOOP_PEEK) {
296         buf = gst_pad_peek (pad);
297         if (buf == NULL)
298           continue;
299
300         g_assert (buf == gst_pad_pull (pad));
301         debug = "loop_peek";
302       }
303       else {
304         buf = gst_pad_pull (pad);
305         debug = "loop";
306       }
307       gst_aggregator_push (aggregator, pad, buf, debug);
308     }
309   }
310   else {
311     if (aggregator->sched == AGGREGATOR_LOOP_SELECT) {
312       GstPad *pad;
313
314       debug = "loop_select";
315
316       pad = gst_pad_select (aggregator->sinkpads);
317       buf = gst_pad_pull (pad);
318
319       gst_aggregator_push (aggregator, pad, buf, debug);
320     }
321     else {
322       g_assert_not_reached ();
323     }
324   }
325 }
326
327 /**
328  * gst_aggregator_chain:
329  * @pad: the pad to follow
330  * @buf: the buffer to pass
331  *
332  * Chain a buffer on a pad.
333  */
334 static void 
335 gst_aggregator_chain (GstPad *pad, GstBuffer *buf) 
336 {
337   GstAggregator *aggregator;
338
339   g_return_if_fail (pad != NULL);
340   g_return_if_fail (GST_IS_PAD (pad));
341   g_return_if_fail (buf != NULL);
342
343   aggregator = GST_AGGREGATOR (gst_pad_get_parent (pad));
344 /*  gst_trace_add_entry (NULL, 0, buf, "aggregator buffer");*/
345
346   gst_aggregator_push (aggregator, pad, buf, "chain");
347 }
348
349 gboolean
350 gst_aggregator_factory_init (GstElementFactory *factory)
351 {
352   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (aggregator_src_factory));
353
354   return TRUE;
355 }
356