gstinfo.[ch], cothreads.c: added initial support for -finstrument_functions gstbin...
[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   "Aggregator",
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   /* FILL ME */
47 };
48
49 GST_PADTEMPLATE_FACTORY (aggregator_src_factory,
50   "sink%d",
51   GST_PAD_SINK,
52   GST_PAD_REQUEST,
53   NULL                  /* no caps */
54 );
55
56 #define GST_TYPE_AGGREGATOR_SCHED (gst_aggregator_sched_get_type())
57 static GType
58 gst_aggregator_sched_get_type (void)
59 {
60   static GType aggregator_sched_type = 0;
61   static GEnumValue aggregator_sched[] = {
62     { AGGREGATOR_LOOP,          "1", "Loop Based"},
63     { AGGREGATOR_LOOP_PEEK,     "2", "Loop Based Peek"},
64     { AGGREGATOR_LOOP_SELECT,   "3", "Loop Based Select"},
65     { AGGREGATOR_CHAIN,         "4", "Chain Based"},
66     {0, NULL, NULL},
67   };
68   if (!aggregator_sched_type) {
69     aggregator_sched_type = g_enum_register_static ("GstAggregatorSched", aggregator_sched);
70   }
71   return aggregator_sched_type;
72 }
73
74 #define AGGREGATOR_IS_LOOP_BASED(ag)    ((ag)->sched != AGGREGATOR_CHAIN)
75
76 static void     gst_aggregator_class_init       (GstAggregatorClass *klass);
77 static void     gst_aggregator_init             (GstAggregator *aggregator);
78
79 static GstPad*  gst_aggregator_request_new_pad  (GstElement *element, GstPadTemplate *temp);
80
81 static void     gst_aggregator_set_property     (GObject *object, guint prop_id, 
82                                                  const GValue *value, GParamSpec *pspec);
83 static void     gst_aggregator_get_property     (GObject *object, guint prop_id, 
84                                                  GValue *value, GParamSpec *pspec);
85
86 static void     gst_aggregator_chain            (GstPad *pad, GstBuffer *buf);
87 static void     gst_aggregator_loop             (GstElement *element);
88
89 static GstElementClass *parent_class = NULL;
90 //static guint gst_aggregator_signals[LAST_SIGNAL] = { 0 };
91
92 GType
93 gst_aggregator_get_type (void) 
94 {
95   static GType aggregator_type = 0;
96
97   if (!aggregator_type) {
98     static const GTypeInfo aggregator_info = {
99       sizeof(GstAggregatorClass),      
100       NULL,
101       NULL,
102       (GClassInitFunc)gst_aggregator_class_init,
103       NULL,
104       NULL,
105       sizeof(GstAggregator),
106       0,
107       (GInstanceInitFunc)gst_aggregator_init,
108     };
109     aggregator_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAggregator", &aggregator_info, 0);
110   }
111   return aggregator_type;
112 }
113
114 static void
115 gst_aggregator_class_init (GstAggregatorClass *klass) 
116 {
117   GObjectClass *gobject_class;
118   GstElementClass *gstelement_class;
119
120   gobject_class = (GObjectClass*) klass;
121   gstelement_class = (GstElementClass*) klass;
122
123   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
124
125   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
126     g_param_spec_int ("num_pads", "num_pads", "num_pads",
127                       0, G_MAXINT, 0, G_PARAM_READABLE)); 
128   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
129     g_param_spec_boolean ("silent", "silent", "silent",
130                       FALSE, G_PARAM_READWRITE)); 
131   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SCHED,
132     g_param_spec_enum ("sched", "sched", "sched",
133                       GST_TYPE_AGGREGATOR_SCHED, AGGREGATOR_CHAIN, G_PARAM_READWRITE)); 
134
135   gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_aggregator_set_property);
136   gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_aggregator_get_property);
137
138   gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_aggregator_request_new_pad);
139 }
140
141 static void 
142 gst_aggregator_init (GstAggregator *aggregator) 
143 {
144   aggregator->srcpad = gst_pad_new ("src", GST_PAD_SRC);
145   gst_element_add_pad (GST_ELEMENT (aggregator), aggregator->srcpad);
146
147   aggregator->numsinkpads = 0;
148   aggregator->sinkpads = NULL;
149   aggregator->silent = FALSE;
150 }
151
152 static GstPad*
153 gst_aggregator_request_new_pad (GstElement *element, GstPadTemplate *templ) 
154 {
155   gchar *name;
156   GstPad *sinkpad;
157   GstAggregator *aggregator;
158
159   g_return_val_if_fail (GST_IS_AGGREGATOR (element), NULL);
160
161   if (templ->direction != GST_PAD_SINK) {
162     g_warning ("gstaggregator: request new pad that is not a SRC pad\n");
163     return NULL;
164   }
165
166   aggregator = GST_AGGREGATOR (element);
167
168   name = g_strdup_printf ("sink%d",aggregator->numsinkpads);
169   
170   sinkpad = gst_pad_new_from_template (templ, name);
171   gst_pad_set_chain_function (sinkpad, gst_aggregator_chain);
172   gst_element_add_pad (GST_ELEMENT (aggregator), sinkpad);
173   
174   aggregator->sinkpads = g_list_prepend (aggregator->sinkpads, sinkpad);
175   aggregator->numsinkpads++;
176   
177   return sinkpad;
178 }
179
180 static void
181 gst_aggregator_update_functions (GstAggregator *aggregator)
182 {
183   GList *pads;
184
185   if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
186     gst_element_set_loop_function (GST_ELEMENT (aggregator), GST_DEBUG_FUNCPTR (gst_aggregator_loop));
187   }
188   else {
189     gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
190   }
191
192   pads = aggregator->sinkpads;
193   while (pads) {
194     GstPad *pad = GST_PAD (pads->data);
195                           
196     if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
197       gst_pad_set_get_function (pad, NULL);
198     }
199     else {
200       gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
201     }
202     pads = g_list_next (pads);
203   }
204 }
205
206 static void
207 gst_aggregator_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
208 {
209   GstAggregator *aggregator;
210
211   /* it's not null if we got it, but it might not be ours */
212   g_return_if_fail (GST_IS_AGGREGATOR (object));
213
214   aggregator = GST_AGGREGATOR (object);
215
216   switch (prop_id) {
217     case ARG_SILENT:
218       aggregator->silent = g_value_get_boolean (value);
219       break;
220     case ARG_SCHED:
221       aggregator->sched = g_value_get_enum (value);
222       gst_aggregator_update_functions (aggregator);
223       break;
224     default:
225       break;
226   }
227 }
228
229 static void
230 gst_aggregator_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
231 {
232   GstAggregator *aggregator;
233
234   /* it's not null if we got it, but it might not be ours */
235   g_return_if_fail (GST_IS_AGGREGATOR (object));
236
237   aggregator = GST_AGGREGATOR (object);
238
239   switch (prop_id) {
240     case ARG_NUM_PADS:
241       g_value_set_int (value, aggregator->numsinkpads);
242       break;
243     case ARG_SILENT:
244       g_value_set_boolean (value, aggregator->silent);
245       break;
246     case ARG_SCHED:
247       g_value_set_enum (value, aggregator->sched);
248       break;
249     default:
250       break;
251   }
252 }
253
254 static void 
255 gst_aggregator_push (GstAggregator *aggregator, GstPad *pad, GstBuffer *buf, guchar *debug) 
256 {
257   if (!aggregator->silent)
258     g_print("aggregator: %10.10s ******* (%s:%s)a (%d bytes, %llu) \n",
259             debug, GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
260
261   gst_pad_push (aggregator->srcpad, buf);
262 }
263
264 static void 
265 gst_aggregator_loop (GstElement *element) 
266 {
267   GstAggregator *aggregator;
268   GstBuffer *buf;
269   guchar *debug;
270
271   aggregator = GST_AGGREGATOR (element);
272
273   do {
274     if (aggregator->sched == AGGREGATOR_LOOP ||
275         aggregator->sched == AGGREGATOR_LOOP_PEEK) {
276       GList *pads = aggregator->sinkpads;
277
278       while (pads) {
279         GstPad *pad = GST_PAD (pads->data);
280         pads = g_list_next (pads);
281
282         if (aggregator->sched == AGGREGATOR_LOOP_PEEK) {
283           buf = gst_pad_peek (pad);
284           if (buf == NULL)
285             continue;
286
287           g_assert (buf == gst_pad_pull (pad));
288           debug = "loop_peek";
289         }
290         else {
291           buf = gst_pad_pull (pad);
292           debug = "loop";
293         }
294         gst_aggregator_push (aggregator, pad, buf, debug);
295       }
296     }
297     else {
298       if (aggregator->sched == AGGREGATOR_LOOP_SELECT) {
299         GstPad *pad;
300
301         debug = "loop_select";
302
303         pad = gst_pad_select (aggregator->sinkpads);
304         buf = gst_pad_pull (pad);
305
306         gst_aggregator_push (aggregator, pad, buf, debug);
307       }
308       else {
309         g_assert_not_reached ();
310       }
311     }
312   } while (!GST_ELEMENT_IS_COTHREAD_STOPPING (element));
313 }
314
315 /**
316  * gst_aggregator_chain:
317  * @pad: the pad to follow
318  * @buf: the buffer to pass
319  *
320  * Chain a buffer on a pad.
321  */
322 static void 
323 gst_aggregator_chain (GstPad *pad, GstBuffer *buf) 
324 {
325   GstAggregator *aggregator;
326
327   g_return_if_fail (pad != NULL);
328   g_return_if_fail (GST_IS_PAD (pad));
329   g_return_if_fail (buf != NULL);
330
331   aggregator = GST_AGGREGATOR (gst_pad_get_parent (pad));
332 //  gst_trace_add_entry (NULL, 0, buf, "aggregator buffer");
333
334   gst_aggregator_push (aggregator, pad, buf, "chain");
335 }
336
337 gboolean
338 gst_aggregator_factory_init (GstElementFactory *factory)
339 {
340   gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (aggregator_src_factory));
341
342   return TRUE;
343 }
344