Convert %lld and %llu in printf formats to G_G[U]INT64_FORMAT. Fix pointer<->int...
[platform/upstream/gstreamer.git] / plugins / 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   "LGPL",
30   "N-to-1 pipe fitting",
31   VERSION,
32   "Wim Taymans <wim.taymans@chello.be>",
33   "(C) 2001",
34 };
35
36 /* Aggregator signals and args */
37 enum {
38   /* FILL ME */
39   LAST_SIGNAL
40 };
41
42 enum {
43   ARG_0,
44   ARG_NUM_PADS,
45   ARG_SILENT,
46   ARG_SCHED,
47   ARG_LAST_MESSAGE,
48   /* FILL ME */
49 };
50
51 GST_PAD_TEMPLATE_FACTORY (aggregator_src_factory,
52   "sink%d",
53   GST_PAD_SINK,
54   GST_PAD_REQUEST,
55   NULL                  /* no caps */
56 );
57
58 #define GST_TYPE_AGGREGATOR_SCHED (gst_aggregator_sched_get_type())
59 static GType
60 gst_aggregator_sched_get_type (void)
61 {
62   static GType aggregator_sched_type = 0;
63   static GEnumValue aggregator_sched[] = {
64     { AGGREGATOR_LOOP,          "1", "Loop Based"},
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", "The number of source 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", "Don't produce messages",
132                       FALSE, G_PARAM_READWRITE)); 
133   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SCHED,
134     g_param_spec_enum ("sched", "Scheduling", "The type of scheduling this element should use",
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", "The current state of the element",
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   g_free (name);
178   
179   gst_pad_set_chain_function (sinkpad, gst_aggregator_chain);
180   gst_element_add_pad (GST_ELEMENT (aggregator), sinkpad);
181   
182   aggregator->sinkpads = g_list_prepend (aggregator->sinkpads, sinkpad);
183   aggregator->numsinkpads++;
184   
185   return sinkpad;
186 }
187
188 static void
189 gst_aggregator_update_functions (GstAggregator *aggregator)
190 {
191   GList *pads;
192
193   if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
194     gst_element_set_loop_function (GST_ELEMENT (aggregator), GST_DEBUG_FUNCPTR (gst_aggregator_loop));
195   }
196   else {
197     gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
198   }
199
200   pads = aggregator->sinkpads;
201   while (pads) {
202     GstPad *pad = GST_PAD (pads->data);
203                           
204     if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
205       gst_pad_set_get_function (pad, NULL);
206     }
207     else {
208       gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
209     }
210     pads = g_list_next (pads);
211   }
212 }
213
214 static void
215 gst_aggregator_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
216 {
217   GstAggregator *aggregator;
218
219   /* it's not null if we got it, but it might not be ours */
220   g_return_if_fail (GST_IS_AGGREGATOR (object));
221
222   aggregator = GST_AGGREGATOR (object);
223
224   switch (prop_id) {
225     case ARG_SILENT:
226       aggregator->silent = g_value_get_boolean (value);
227       break;
228     case ARG_SCHED:
229       aggregator->sched = g_value_get_enum (value);
230       gst_aggregator_update_functions (aggregator);
231       break;
232     default:
233       break;
234   }
235 }
236
237 static void
238 gst_aggregator_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
239 {
240   GstAggregator *aggregator;
241
242   /* it's not null if we got it, but it might not be ours */
243   g_return_if_fail (GST_IS_AGGREGATOR (object));
244
245   aggregator = GST_AGGREGATOR (object);
246
247   switch (prop_id) {
248     case ARG_NUM_PADS:
249       g_value_set_int (value, aggregator->numsinkpads);
250       break;
251     case ARG_SILENT:
252       g_value_set_boolean (value, aggregator->silent);
253       break;
254     case ARG_SCHED:
255       g_value_set_enum (value, aggregator->sched);
256       break;
257     case ARG_LAST_MESSAGE:
258       g_value_set_string (value, aggregator->last_message);
259       break;
260     default:
261       break;
262   }
263 }
264
265 static void 
266 gst_aggregator_push (GstAggregator *aggregator, GstPad *pad, GstBuffer *buf, guchar *debug) 
267 {
268   if (!aggregator->silent) {
269     g_free (aggregator->last_message);
270
271     aggregator->last_message = g_strdup_printf ("%10.10s ******* (%s:%s)a (%d bytes, %"
272                                                 G_GUINT64_FORMAT ")",
273             debug, GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
274
275     g_object_notify (G_OBJECT (aggregator), "last_message");
276   }
277
278   gst_pad_push (aggregator->srcpad, buf);
279 }
280
281 static void 
282 gst_aggregator_loop (GstElement *element) 
283 {
284   GstAggregator *aggregator;
285   GstBuffer *buf;
286   guchar *debug;
287
288   aggregator = GST_AGGREGATOR (element);
289
290   if (aggregator->sched == AGGREGATOR_LOOP) {
291     GList *pads = aggregator->sinkpads;
292
293     while (pads) {
294       GstPad *pad = GST_PAD (pads->data);
295       pads = g_list_next (pads);
296
297       buf = gst_pad_pull (pad);
298       debug = "loop";
299
300       gst_aggregator_push (aggregator, pad, buf, debug);
301     }
302   }
303   else {
304     if (aggregator->sched == AGGREGATOR_LOOP_SELECT) {
305       GstPad *pad;
306
307       debug = "loop_select";
308
309       pad = gst_pad_select (aggregator->sinkpads);
310       buf = gst_pad_pull (pad);
311
312       gst_aggregator_push (aggregator, pad, buf, debug);
313     }
314     else {
315       g_assert_not_reached ();
316     }
317   }
318 }
319
320 /**
321  * gst_aggregator_chain:
322  * @pad: the pad to follow
323  * @buf: the buffer to pass
324  *
325  * Chain a buffer on a pad.
326  */
327 static void 
328 gst_aggregator_chain (GstPad *pad, GstBuffer *buf) 
329 {
330   GstAggregator *aggregator;
331
332   g_return_if_fail (pad != NULL);
333   g_return_if_fail (GST_IS_PAD (pad));
334   g_return_if_fail (buf != NULL);
335
336   aggregator = GST_AGGREGATOR (gst_pad_get_parent (pad));
337 /*  gst_trace_add_entry (NULL, 0, buf, "aggregator buffer");*/
338
339   gst_aggregator_push (aggregator, pad, buf, "chain");
340 }
341
342 gboolean
343 gst_aggregator_factory_init (GstElementFactory *factory)
344 {
345   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (aggregator_src_factory));
346
347   return TRUE;
348 }
349