GST_DEBUG reorganization containing loads of stuff:
[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 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include "gstaggregator.h"
28
29 GST_DEBUG_CATEGORY (gst_aggregator_debug);
30 #define GST_CAT_DEFAULT gst_aggregator_debug
31
32 GstElementDetails gst_aggregator_details = {
33   "Aggregator pipe fitting",
34   "Generic",
35   "LGPL",
36   "N-to-1 pipe fitting",
37   VERSION,
38   "Wim Taymans <wim.taymans@chello.be>",
39   "(C) 2001",
40 };
41
42 /* Aggregator signals and args */
43 enum {
44   /* FILL ME */
45   LAST_SIGNAL
46 };
47
48 enum {
49   ARG_0,
50   ARG_NUM_PADS,
51   ARG_SILENT,
52   ARG_SCHED,
53   ARG_LAST_MESSAGE,
54   /* FILL ME */
55 };
56
57 GST_PAD_TEMPLATE_FACTORY (aggregator_src_factory,
58   "sink%d",
59   GST_PAD_SINK,
60   GST_PAD_REQUEST,
61   NULL                  /* no caps */
62 );
63
64 #define GST_TYPE_AGGREGATOR_SCHED (gst_aggregator_sched_get_type())
65 static GType
66 gst_aggregator_sched_get_type (void)
67 {
68   static GType aggregator_sched_type = 0;
69   static GEnumValue aggregator_sched[] = {
70     { AGGREGATOR_LOOP,          "1", "Loop Based"},
71     { AGGREGATOR_LOOP_SELECT,   "3", "Loop Based Select"},
72     { AGGREGATOR_CHAIN,         "4", "Chain Based"},
73     {0, NULL, NULL},
74   };
75   if (!aggregator_sched_type) {
76     aggregator_sched_type = g_enum_register_static ("GstAggregatorSched", aggregator_sched);
77   }
78   return aggregator_sched_type;
79 }
80
81 #define AGGREGATOR_IS_LOOP_BASED(ag)    ((ag)->sched != AGGREGATOR_CHAIN)
82
83 static void     gst_aggregator_class_init       (GstAggregatorClass *klass);
84 static void     gst_aggregator_init             (GstAggregator *aggregator);
85
86 static GstPad*  gst_aggregator_request_new_pad  (GstElement *element, GstPadTemplate *temp, const
87                                                  gchar *unused);
88 static void     gst_aggregator_update_functions (GstAggregator *aggregator);
89
90 static void     gst_aggregator_set_property     (GObject *object, guint prop_id, 
91                                                  const GValue *value, GParamSpec *pspec);
92 static void     gst_aggregator_get_property     (GObject *object, guint prop_id, 
93                                                  GValue *value, GParamSpec *pspec);
94
95 static void     gst_aggregator_chain            (GstPad *pad, GstBuffer *buf);
96 static void     gst_aggregator_loop             (GstElement *element);
97
98 static GstElementClass *parent_class = NULL;
99 /*static guint gst_aggregator_signals[LAST_SIGNAL] = { 0 };*/
100
101 GType
102 gst_aggregator_get_type (void) 
103 {
104   static GType aggregator_type = 0;
105
106   if (!aggregator_type) {
107     static const GTypeInfo aggregator_info = {
108       sizeof(GstAggregatorClass),      
109       NULL,
110       NULL,
111       (GClassInitFunc)gst_aggregator_class_init,
112       NULL,
113       NULL,
114       sizeof(GstAggregator),
115       0,
116       (GInstanceInitFunc)gst_aggregator_init,
117     };
118     aggregator_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAggregator", &aggregator_info, 0);
119   }
120   return aggregator_type;
121 }
122
123 static void
124 gst_aggregator_class_init (GstAggregatorClass *klass) 
125 {
126   GObjectClass *gobject_class;
127   GstElementClass *gstelement_class;
128
129   gobject_class = (GObjectClass*) klass;
130   gstelement_class = (GstElementClass*) klass;
131
132   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
133
134   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
135     g_param_spec_int ("num_pads", "Num pads", "The number of source pads",
136                       0, G_MAXINT, 0, G_PARAM_READABLE)); 
137   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
138     g_param_spec_boolean ("silent", "Silent", "Don't produce messages",
139                       FALSE, G_PARAM_READWRITE)); 
140   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SCHED,
141     g_param_spec_enum ("sched", "Scheduling", "The type of scheduling this element should use",
142                       GST_TYPE_AGGREGATOR_SCHED, AGGREGATOR_CHAIN, G_PARAM_READWRITE)); 
143   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
144     g_param_spec_string ("last_message", "Last message", "The current state of the element",
145                          NULL, G_PARAM_READABLE));
146
147   gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_aggregator_set_property);
148   gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_aggregator_get_property);
149
150   gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_aggregator_request_new_pad);
151 }
152
153 static void 
154 gst_aggregator_init (GstAggregator *aggregator) 
155 {
156   aggregator->srcpad = gst_pad_new ("src", GST_PAD_SRC);
157   gst_element_add_pad (GST_ELEMENT (aggregator), aggregator->srcpad);
158
159   aggregator->numsinkpads = 0;
160   aggregator->sinkpads = NULL;
161   aggregator->silent = FALSE;
162   aggregator->sched = AGGREGATOR_LOOP;
163
164   gst_aggregator_update_functions (aggregator);
165 }
166
167 static GstPad*
168 gst_aggregator_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *unused) 
169 {
170   gchar *name;
171   GstPad *sinkpad;
172   GstAggregator *aggregator;
173
174   g_return_val_if_fail (GST_IS_AGGREGATOR (element), NULL);
175
176   if (templ->direction != GST_PAD_SINK) {
177     g_warning ("gstaggregator: request new pad that is not a SRC pad\n");
178     return NULL;
179   }
180
181   aggregator = GST_AGGREGATOR (element);
182
183   name = g_strdup_printf ("sink%d",aggregator->numsinkpads);
184   
185   sinkpad = gst_pad_new_from_template (templ, name);
186   g_free (name);
187   
188   if (!AGGREGATOR_IS_LOOP_BASED (aggregator)) {
189     gst_pad_set_chain_function (sinkpad, gst_aggregator_chain);
190   }
191   gst_element_add_pad (GST_ELEMENT (aggregator), sinkpad);
192   
193   aggregator->sinkpads = g_list_prepend (aggregator->sinkpads, sinkpad);
194   aggregator->numsinkpads++;
195   
196   return sinkpad;
197 }
198
199 static void
200 gst_aggregator_update_functions (GstAggregator *aggregator)
201 {
202   GList *pads;
203
204   if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
205     gst_element_set_loop_function (GST_ELEMENT (aggregator), GST_DEBUG_FUNCPTR (gst_aggregator_loop));
206   }
207   else {
208     gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
209   }
210
211   pads = aggregator->sinkpads;
212   while (pads) {
213     GstPad *pad = GST_PAD (pads->data);
214                           
215     if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
216       gst_pad_set_get_function (pad, NULL);
217     }
218     else {
219       gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
220     }
221     pads = g_list_next (pads);
222   }
223 }
224
225 static void
226 gst_aggregator_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
227 {
228   GstAggregator *aggregator;
229
230   /* it's not null if we got it, but it might not be ours */
231   g_return_if_fail (GST_IS_AGGREGATOR (object));
232
233   aggregator = GST_AGGREGATOR (object);
234
235   switch (prop_id) {
236     case ARG_SILENT:
237       aggregator->silent = g_value_get_boolean (value);
238       break;
239     case ARG_SCHED:
240       aggregator->sched = g_value_get_enum (value);
241       gst_aggregator_update_functions (aggregator);
242       break;
243     default:
244       break;
245   }
246 }
247
248 static void
249 gst_aggregator_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
250 {
251   GstAggregator *aggregator;
252
253   /* it's not null if we got it, but it might not be ours */
254   g_return_if_fail (GST_IS_AGGREGATOR (object));
255
256   aggregator = GST_AGGREGATOR (object);
257
258   switch (prop_id) {
259     case ARG_NUM_PADS:
260       g_value_set_int (value, aggregator->numsinkpads);
261       break;
262     case ARG_SILENT:
263       g_value_set_boolean (value, aggregator->silent);
264       break;
265     case ARG_SCHED:
266       g_value_set_enum (value, aggregator->sched);
267       break;
268     case ARG_LAST_MESSAGE:
269       g_value_set_string (value, aggregator->last_message);
270       break;
271     default:
272       break;
273   }
274 }
275
276 static void 
277 gst_aggregator_push (GstAggregator *aggregator, GstPad *pad, GstBuffer *buf, guchar *debug) 
278 {
279   if (!aggregator->silent) {
280     g_free (aggregator->last_message);
281
282     aggregator->last_message = g_strdup_printf ("%10.10s ******* (%s:%s)a (%d bytes, %"
283                                                 G_GUINT64_FORMAT ")",
284             debug, GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
285
286     g_object_notify (G_OBJECT (aggregator), "last_message");
287   }
288
289   gst_pad_push (aggregator->srcpad, buf);
290 }
291
292 static void 
293 gst_aggregator_loop (GstElement *element) 
294 {
295   GstAggregator *aggregator;
296   GstBuffer *buf;
297   guchar *debug;
298
299   aggregator = GST_AGGREGATOR (element);
300
301   if (aggregator->sched == AGGREGATOR_LOOP) {
302     GList *pads = aggregator->sinkpads;
303
304     while (pads) {
305       GstPad *pad = GST_PAD (pads->data);
306       pads = g_list_next (pads);
307
308       g_print ("inspecting pad %s:%s\n", GST_DEBUG_PAD_NAME (pad));
309       if (GST_PAD_IS_ACTIVE (pad)) {
310         buf = gst_pad_pull (pad);
311         debug = "loop";
312
313         gst_aggregator_push (aggregator, pad, buf, debug);
314       }
315     }
316   }
317   else {
318     if (aggregator->sched == AGGREGATOR_LOOP_SELECT) {
319       GstPad *pad;
320
321       debug = "loop_select";
322
323       pad = gst_pad_select (aggregator->sinkpads);
324       buf = gst_pad_pull (pad);
325
326       gst_aggregator_push (aggregator, pad, buf, debug);
327     }
328     else {
329       g_assert_not_reached ();
330     }
331   }
332 }
333
334 /**
335  * gst_aggregator_chain:
336  * @pad: the pad to follow
337  * @buf: the buffer to pass
338  *
339  * Chain a buffer on a pad.
340  */
341 static void 
342 gst_aggregator_chain (GstPad *pad, GstBuffer *buf) 
343 {
344   GstAggregator *aggregator;
345
346   g_return_if_fail (pad != NULL);
347   g_return_if_fail (GST_IS_PAD (pad));
348   g_return_if_fail (buf != NULL);
349
350   aggregator = GST_AGGREGATOR (gst_pad_get_parent (pad));
351 /*  gst_trace_add_entry (NULL, 0, buf, "aggregator buffer");*/
352
353   gst_aggregator_push (aggregator, pad, buf, "chain");
354 }
355
356 gboolean
357 gst_aggregator_factory_init (GstElementFactory *factory)
358 {
359   gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (aggregator_src_factory));
360
361   return TRUE;
362 }