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