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_STATIC (gst_aggregator_debug);
30 #define GST_CAT_DEFAULT gst_aggregator_debug
32 GstElementDetails gst_aggregator_details = GST_ELEMENT_DETAILS (
33 "Aggregator pipe fitting",
35 "N-to-1 pipe fitting",
36 "Wim Taymans <wim.taymans@chello.be>"
39 /* Aggregator signals and args */
54 GstStaticPadTemplate aggregator_src_template = GST_STATIC_PAD_TEMPLATE (
61 #define GST_TYPE_AGGREGATOR_SCHED (gst_aggregator_sched_get_type())
63 gst_aggregator_sched_get_type (void)
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"},
72 if (!aggregator_sched_type) {
73 aggregator_sched_type = g_enum_register_static ("GstAggregatorSched", aggregator_sched);
75 return aggregator_sched_type;
78 #define AGGREGATOR_IS_LOOP_BASED(ag) ((ag)->sched != AGGREGATOR_CHAIN)
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);
84 static GstPad* gst_aggregator_request_new_pad (GstElement *element, GstPadTemplate *temp, const
86 static void gst_aggregator_update_functions (GstAggregator *aggregator);
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);
93 static void gst_aggregator_chain (GstPad *pad, GstData *_data);
94 static void gst_aggregator_loop (GstElement *element);
96 static GstElementClass *parent_class = NULL;
97 /*static guint gst_aggregator_signals[LAST_SIGNAL] = { 0 };*/
100 gst_aggregator_get_type (void)
102 static GType aggregator_type = 0;
104 if (!aggregator_type) {
105 static const GTypeInfo aggregator_info = {
106 sizeof(GstAggregatorClass),
107 gst_aggregator_base_init,
109 (GClassInitFunc)gst_aggregator_class_init,
112 sizeof(GstAggregator),
114 (GInstanceInitFunc)gst_aggregator_init,
116 aggregator_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAggregator", &aggregator_info, 0);
118 GST_DEBUG_CATEGORY_INIT (gst_aggregator_debug, "aggregator", 0, "aggregator element");
120 return aggregator_type;
124 gst_aggregator_base_init (gpointer g_class)
126 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
127 gst_element_class_add_pad_template (gstelement_class,
128 gst_static_pad_template_get (&aggregator_src_template));
129 gst_element_class_set_details (gstelement_class, &gst_aggregator_details);
132 gst_aggregator_class_init (GstAggregatorClass *klass)
134 GObjectClass *gobject_class;
135 GstElementClass *gstelement_class;
137 gobject_class = (GObjectClass*) klass;
138 gstelement_class = (GstElementClass*) klass;
140 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
142 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
143 g_param_spec_int ("num_pads", "Num pads", "The number of source pads",
144 0, G_MAXINT, 0, G_PARAM_READABLE));
145 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
146 g_param_spec_boolean ("silent", "Silent", "Don't produce messages",
147 FALSE, G_PARAM_READWRITE));
148 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SCHED,
149 g_param_spec_enum ("sched", "Scheduling", "The type of scheduling this element should use",
150 GST_TYPE_AGGREGATOR_SCHED, AGGREGATOR_CHAIN, G_PARAM_READWRITE));
151 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
152 g_param_spec_string ("last_message", "Last message", "The current state of the element",
153 NULL, G_PARAM_READABLE));
155 gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_aggregator_set_property);
156 gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_aggregator_get_property);
158 gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_aggregator_request_new_pad);
162 gst_aggregator_init (GstAggregator *aggregator)
164 aggregator->srcpad = gst_pad_new ("src", GST_PAD_SRC);
165 gst_pad_set_getcaps_function (aggregator->srcpad, gst_pad_proxy_getcaps);
166 gst_element_add_pad (GST_ELEMENT (aggregator), aggregator->srcpad);
168 aggregator->numsinkpads = 0;
169 aggregator->sinkpads = NULL;
170 aggregator->silent = FALSE;
171 aggregator->sched = AGGREGATOR_LOOP;
173 gst_aggregator_update_functions (aggregator);
177 gst_aggregator_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *unused)
181 GstAggregator *aggregator;
183 g_return_val_if_fail (GST_IS_AGGREGATOR (element), NULL);
185 if (templ->direction != GST_PAD_SINK) {
186 g_warning ("gstaggregator: request new pad that is not a sink pad\n");
190 aggregator = GST_AGGREGATOR (element);
192 name = g_strdup_printf ("sink%d",aggregator->numsinkpads);
194 sinkpad = gst_pad_new_from_template (templ, name);
197 if (!AGGREGATOR_IS_LOOP_BASED (aggregator)) {
198 gst_pad_set_chain_function (sinkpad, gst_aggregator_chain);
200 gst_pad_set_getcaps_function (sinkpad, gst_pad_proxy_getcaps);
201 gst_element_add_pad (GST_ELEMENT (aggregator), sinkpad);
203 aggregator->sinkpads = g_list_prepend (aggregator->sinkpads, sinkpad);
204 aggregator->numsinkpads++;
210 gst_aggregator_update_functions (GstAggregator *aggregator)
214 if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
215 gst_element_set_loop_function (GST_ELEMENT (aggregator), GST_DEBUG_FUNCPTR (gst_aggregator_loop));
218 gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
221 pads = aggregator->sinkpads;
223 GstPad *pad = GST_PAD (pads->data);
225 if (AGGREGATOR_IS_LOOP_BASED (aggregator)) {
226 gst_pad_set_get_function (pad, NULL);
229 gst_element_set_loop_function (GST_ELEMENT (aggregator), NULL);
231 pads = g_list_next (pads);
236 gst_aggregator_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
238 GstAggregator *aggregator;
240 /* it's not null if we got it, but it might not be ours */
241 g_return_if_fail (GST_IS_AGGREGATOR (object));
243 aggregator = GST_AGGREGATOR (object);
247 aggregator->silent = g_value_get_boolean (value);
250 aggregator->sched = g_value_get_enum (value);
251 gst_aggregator_update_functions (aggregator);
259 gst_aggregator_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
261 GstAggregator *aggregator;
263 /* it's not null if we got it, but it might not be ours */
264 g_return_if_fail (GST_IS_AGGREGATOR (object));
266 aggregator = GST_AGGREGATOR (object);
270 g_value_set_int (value, aggregator->numsinkpads);
273 g_value_set_boolean (value, aggregator->silent);
276 g_value_set_enum (value, aggregator->sched);
278 case ARG_LAST_MESSAGE:
279 g_value_set_string (value, aggregator->last_message);
287 gst_aggregator_push (GstAggregator *aggregator, GstPad *pad, GstBuffer *buf, guchar *debug)
289 if (!aggregator->silent) {
290 g_free (aggregator->last_message);
292 aggregator->last_message = g_strdup_printf ("%10.10s ******* (%s:%s)a (%d bytes, %"
293 G_GUINT64_FORMAT ")",
294 debug, GST_DEBUG_PAD_NAME (pad), GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf));
296 g_object_notify (G_OBJECT (aggregator), "last_message");
299 gst_pad_push (aggregator->srcpad, GST_DATA (buf));
303 gst_aggregator_loop (GstElement *element)
305 GstAggregator *aggregator;
309 aggregator = GST_AGGREGATOR (element);
311 if (aggregator->sched == AGGREGATOR_LOOP) {
312 GList *pads = aggregator->sinkpads;
314 /* we'll loop over all pads and try to pull from all
317 GstPad *pad = GST_PAD (pads->data);
318 pads = g_list_next (pads);
320 /* we need to check is the pad is usable. IS_USABLE will check
321 * if the pad is linked, if it is enabled (the element is
322 * playing and the app didn't gst_pad_set_enabled (pad, FALSE))
323 * and that the peer pad is also enabled.
325 if (GST_PAD_IS_USABLE (pad)) {
326 buf = GST_BUFFER (gst_pad_pull (pad));
329 /* then push it forward */
330 gst_aggregator_push (aggregator, pad, buf, debug);
335 if (aggregator->sched == AGGREGATOR_LOOP_SELECT) {
338 debug = "loop_select";
340 pad = gst_pad_selectv (aggregator->sinkpads);
341 buf = GST_BUFFER (gst_pad_pull (pad));
343 gst_aggregator_push (aggregator, pad, buf, debug);
346 g_assert_not_reached ();
352 * gst_aggregator_chain:
353 * @pad: the pad to follow
354 * @buf: the buffer to pass
356 * Chain a buffer on a pad.
359 gst_aggregator_chain (GstPad *pad, GstData *_data)
361 GstBuffer *buf = GST_BUFFER (_data);
362 GstAggregator *aggregator;
364 g_return_if_fail (pad != NULL);
365 g_return_if_fail (GST_IS_PAD (pad));
366 g_return_if_fail (buf != NULL);
368 aggregator = GST_AGGREGATOR (gst_pad_get_parent (pad));
369 /* gst_trace_add_entry (NULL, 0, buf, "aggregator buffer");*/
371 gst_aggregator_push (aggregator, pad, buf, "chain");