2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@chello.be>
5 * gstaggregator.c: Aggregator element, N in 1 out
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.
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.
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.
27 #include "gstaggregator.h"
29 GST_DEBUG_CATEGORY (gst_aggregator_debug);
30 #define GST_CAT_DEFAULT gst_aggregator_debug
32 GstElementDetails gst_aggregator_details = {
33 "Aggregator pipe fitting",
36 "N-to-1 pipe fitting",
38 "Wim Taymans <wim.taymans@chello.be>",
42 /* Aggregator signals and args */
57 GST_PAD_TEMPLATE_FACTORY (aggregator_src_factory,
64 #define GST_TYPE_AGGREGATOR_SCHED (gst_aggregator_sched_get_type())
66 gst_aggregator_sched_get_type (void)
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"},
75 if (!aggregator_sched_type) {
76 aggregator_sched_type = g_enum_register_static ("GstAggregatorSched", aggregator_sched);
78 return aggregator_sched_type;
81 #define AGGREGATOR_IS_LOOP_BASED(ag) ((ag)->sched != AGGREGATOR_CHAIN)
83 static void gst_aggregator_class_init (GstAggregatorClass *klass);
84 static void gst_aggregator_init (GstAggregator *aggregator);
86 static GstPad* gst_aggregator_request_new_pad (GstElement *element, GstPadTemplate *temp, const
88 static void gst_aggregator_update_functions (GstAggregator *aggregator);
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);
95 static void gst_aggregator_chain (GstPad *pad, GstBuffer *buf);
96 static void gst_aggregator_loop (GstElement *element);
98 static GstElementClass *parent_class = NULL;
99 /*static guint gst_aggregator_signals[LAST_SIGNAL] = { 0 };*/
102 gst_aggregator_get_type (void)
104 static GType aggregator_type = 0;
106 if (!aggregator_type) {
107 static const GTypeInfo aggregator_info = {
108 sizeof(GstAggregatorClass),
111 (GClassInitFunc)gst_aggregator_class_init,
114 sizeof(GstAggregator),
116 (GInstanceInitFunc)gst_aggregator_init,
118 aggregator_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAggregator", &aggregator_info, 0);
120 return aggregator_type;
124 gst_aggregator_class_init (GstAggregatorClass *klass)
126 GObjectClass *gobject_class;
127 GstElementClass *gstelement_class;
129 gobject_class = (GObjectClass*) klass;
130 gstelement_class = (GstElementClass*) klass;
132 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
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));
147 gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_aggregator_set_property);
148 gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_aggregator_get_property);
150 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_aggregator_request_new_pad);
154 gst_aggregator_init (GstAggregator *aggregator)
156 aggregator->srcpad = gst_pad_new ("src", GST_PAD_SRC);
157 gst_element_add_pad (GST_ELEMENT (aggregator), aggregator->srcpad);
159 aggregator->numsinkpads = 0;
160 aggregator->sinkpads = NULL;
161 aggregator->silent = FALSE;
162 aggregator->sched = AGGREGATOR_LOOP;
164 gst_aggregator_update_functions (aggregator);
168 gst_aggregator_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *unused)
172 GstAggregator *aggregator;
174 g_return_val_if_fail (GST_IS_AGGREGATOR (element), NULL);
176 if (templ->direction != GST_PAD_SINK) {
177 g_warning ("gstaggregator: request new pad that is not a SRC pad\n");
181 aggregator = GST_AGGREGATOR (element);
183 name = g_strdup_printf ("sink%d",aggregator->numsinkpads);
185 sinkpad = gst_pad_new_from_template (templ, name);
188 if (!AGGREGATOR_IS_LOOP_BASED (aggregator)) {
189 gst_pad_set_chain_function (sinkpad, gst_aggregator_chain);
191 gst_element_add_pad (GST_ELEMENT (aggregator), sinkpad);
193 aggregator->sinkpads = g_list_prepend (aggregator->sinkpads, sinkpad);
194 aggregator->numsinkpads++;
200 gst_aggregator_update_functions (GstAggregator *aggregator)
204 if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
205 gst_element_set_loop_function (GST_ELEMENT (aggregator), GST_DEBUG_FUNCPTR (gst_aggregator_loop));
208 gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
211 pads = aggregator->sinkpads;
213 GstPad *pad = GST_PAD (pads->data);
215 if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
216 gst_pad_set_get_function (pad, NULL);
219 gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
221 pads = g_list_next (pads);
226 gst_aggregator_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
228 GstAggregator *aggregator;
230 /* it's not null if we got it, but it might not be ours */
231 g_return_if_fail (GST_IS_AGGREGATOR (object));
233 aggregator = GST_AGGREGATOR (object);
237 aggregator->silent = g_value_get_boolean (value);
240 aggregator->sched = g_value_get_enum (value);
241 gst_aggregator_update_functions (aggregator);
249 gst_aggregator_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
251 GstAggregator *aggregator;
253 /* it's not null if we got it, but it might not be ours */
254 g_return_if_fail (GST_IS_AGGREGATOR (object));
256 aggregator = GST_AGGREGATOR (object);
260 g_value_set_int (value, aggregator->numsinkpads);
263 g_value_set_boolean (value, aggregator->silent);
266 g_value_set_enum (value, aggregator->sched);
268 case ARG_LAST_MESSAGE:
269 g_value_set_string (value, aggregator->last_message);
277 gst_aggregator_push (GstAggregator *aggregator, GstPad *pad, GstBuffer *buf, guchar *debug)
279 if (!aggregator->silent) {
280 g_free (aggregator->last_message);
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));
286 g_object_notify (G_OBJECT (aggregator), "last_message");
289 gst_pad_push (aggregator->srcpad, buf);
293 gst_aggregator_loop (GstElement *element)
295 GstAggregator *aggregator;
299 aggregator = GST_AGGREGATOR (element);
301 if (aggregator->sched == AGGREGATOR_LOOP) {
302 GList *pads = aggregator->sinkpads;
304 /* we'll loop over all pads and try to pull from all
307 GstPad *pad = GST_PAD (pads->data);
308 pads = g_list_next (pads);
310 /* we need to check is the pad is usable. IS_USABLE will check
311 * if the pad is linked, if it is enabled (the element is
312 * playing and the app didn't gst_pad_set_enabled (pad, FALSE))
313 * and that the peer pad is also enabled.
315 if (GST_PAD_IS_USABLE (pad)) {
316 buf = gst_pad_pull (pad);
319 /* then push it forward */
320 gst_aggregator_push (aggregator, pad, buf, debug);
325 if (aggregator->sched == AGGREGATOR_LOOP_SELECT) {
328 debug = "loop_select";
330 pad = gst_pad_select (aggregator->sinkpads);
331 buf = gst_pad_pull (pad);
333 gst_aggregator_push (aggregator, pad, buf, debug);
336 g_assert_not_reached ();
342 * gst_aggregator_chain:
343 * @pad: the pad to follow
344 * @buf: the buffer to pass
346 * Chain a buffer on a pad.
349 gst_aggregator_chain (GstPad *pad, GstBuffer *buf)
351 GstAggregator *aggregator;
353 g_return_if_fail (pad != NULL);
354 g_return_if_fail (GST_IS_PAD (pad));
355 g_return_if_fail (buf != NULL);
357 aggregator = GST_AGGREGATOR (gst_pad_get_parent (pad));
358 /* gst_trace_add_entry (NULL, 0, buf, "aggregator buffer");*/
360 gst_aggregator_push (aggregator, pad, buf, "chain");
364 gst_aggregator_factory_init (GstElementFactory *factory)
366 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (aggregator_src_factory));