2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2001,2002,2003,2004,2005 Wim Taymans <wim@fluendo.com>
6 * gsttee.c: Tee element, one in N out
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 * @short_description: 1-to-N pipe fitting
27 * @see_also: #GstIdentity
29 * Split data to multiple pads.
40 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_DEBUG_CATEGORY_STATIC (gst_tee_debug);
46 #define GST_CAT_DEFAULT gst_tee_debug
48 static GstElementDetails gst_tee_details =
49 GST_ELEMENT_DETAILS ("Tee pipe fitting",
51 "1-to-N pipe fitting",
52 "Erik Walthinsen <omega@cse.ogi.edu>, "
53 "Wim \"Tim\" Taymans <wim@fluendo.com>");
66 GstStaticPadTemplate tee_src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
71 #define _do_init(bla) \
72 GST_DEBUG_CATEGORY_INIT (gst_tee_debug, "tee", 0, "tee element");
74 GST_BOILERPLATE_FULL (GstTee, gst_tee, GstElement, GST_TYPE_ELEMENT, _do_init);
76 static GstPad *gst_tee_request_new_pad (GstElement * element,
77 GstPadTemplate * temp, const gchar * unused);
79 static void gst_tee_finalize (GObject * object);
80 static void gst_tee_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_tee_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstFlowReturn gst_tee_chain (GstPad * pad, GstBuffer * buffer);
86 static void gst_tee_loop (GstPad * pad);
87 static gboolean gst_tee_sink_activate_push (GstPad * pad, gboolean active);
88 static gboolean gst_tee_sink_activate_pull (GstPad * pad, gboolean active);
92 gst_tee_base_init (gpointer g_class)
94 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
96 gst_element_class_add_pad_template (gstelement_class,
97 gst_static_pad_template_get (&sinktemplate));
98 gst_element_class_set_details (gstelement_class, &gst_tee_details);
99 gst_element_class_add_pad_template (gstelement_class,
100 gst_static_pad_template_get (&tee_src_template));
104 gst_tee_finalize (GObject * object)
108 tee = GST_TEE (object);
110 g_free (tee->last_message);
112 G_OBJECT_CLASS (parent_class)->finalize (object);
116 gst_tee_class_init (GstTeeClass * klass)
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
121 gobject_class = (GObjectClass *) klass;
122 gstelement_class = (GstElementClass *) klass;
124 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tee_finalize);
125 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_tee_set_property);
126 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_tee_get_property);
128 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_NUM_SRC_PADS,
129 g_param_spec_int ("num-src-pads", "num-src-pads", "num-src-pads",
130 0, G_MAXINT, 0, G_PARAM_READABLE));
131 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_SINK_LOOP,
132 g_param_spec_boolean ("has-sink-loop", "has-sink-loop", "has-sink-loop",
133 FALSE, G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
134 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_CHAIN,
135 g_param_spec_boolean ("has-chain", "has-chain", "has-chain",
136 TRUE, G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
137 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SILENT,
138 g_param_spec_boolean ("silent", "silent", "silent",
139 TRUE, G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
140 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LAST_MESSAGE,
141 g_param_spec_string ("last_message", "last_message", "last_message",
142 NULL, G_PARAM_READABLE));
144 gstelement_class->request_new_pad =
145 GST_DEBUG_FUNCPTR (gst_tee_request_new_pad);
149 gst_tee_init (GstTee * tee, GstTeeClass * g_class)
151 tee->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
152 gst_pad_set_setcaps_function (tee->sinkpad,
153 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
154 gst_pad_set_getcaps_function (tee->sinkpad,
155 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
156 gst_element_add_pad (GST_ELEMENT (tee), tee->sinkpad);
158 tee->last_message = NULL;
162 gst_tee_update_pad_functions (GstTee * tee)
164 gst_pad_set_activatepush_function (tee->sinkpad,
165 GST_DEBUG_FUNCPTR (gst_tee_sink_activate_push));
166 gst_pad_set_activatepull_function (tee->sinkpad,
167 GST_DEBUG_FUNCPTR (gst_tee_sink_activate_pull));
170 gst_pad_set_chain_function (tee->sinkpad,
171 GST_DEBUG_FUNCPTR (gst_tee_chain));
173 gst_pad_set_chain_function (tee->sinkpad, NULL);
177 gst_tee_request_new_pad (GstElement * element, GstPadTemplate * templ,
178 const gchar * unused)
184 tee = GST_TEE (element);
186 GST_OBJECT_LOCK (tee);
187 name = g_strdup_printf ("src%d", tee->pad_counter++);
188 GST_OBJECT_UNLOCK (tee);
190 srcpad = gst_pad_new_from_template (templ, name);
193 gst_pad_set_setcaps_function (srcpad,
194 GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
195 gst_pad_set_getcaps_function (srcpad,
196 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
197 gst_element_add_pad (GST_ELEMENT (tee), srcpad);
203 gst_tee_set_property (GObject * object, guint prop_id, const GValue * value,
206 GstTee *tee = GST_TEE (object);
208 GST_OBJECT_LOCK (tee);
210 case PROP_HAS_SINK_LOOP:
211 tee->has_sink_loop = g_value_get_boolean (value);
212 gst_tee_update_pad_functions (tee);
215 tee->has_chain = g_value_get_boolean (value);
216 gst_tee_update_pad_functions (tee);
219 tee->silent = g_value_get_boolean (value);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 GST_OBJECT_UNLOCK (tee);
229 gst_tee_get_property (GObject * object, guint prop_id, GValue * value,
232 GstTee *tee = GST_TEE (object);
234 GST_OBJECT_LOCK (tee);
236 case PROP_NUM_SRC_PADS:
237 g_value_set_int (value, GST_ELEMENT (tee)->numsrcpads);
239 case PROP_HAS_SINK_LOOP:
240 g_value_set_boolean (value, tee->has_sink_loop);
243 g_value_set_boolean (value, tee->has_chain);
246 g_value_set_boolean (value, tee->silent);
248 case PROP_LAST_MESSAGE:
249 g_value_set_string (value, tee->last_message);
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255 GST_OBJECT_UNLOCK (tee);
265 gst_tee_do_push (GstPad * pad, GValue * ret, PushData * data)
268 GstTee *tee = data->tee;
270 if (G_UNLIKELY (!data->tee->silent)) {
271 GstBuffer *buf = data->buffer;
273 GST_OBJECT_LOCK (tee);
274 g_free (tee->last_message);
276 g_strdup_printf ("chain ******* (%s:%s)t (%d bytes, %"
277 G_GUINT64_FORMAT ") %p", GST_DEBUG_PAD_NAME (pad),
278 GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf), buf);
279 GST_OBJECT_UNLOCK (tee);
280 g_object_notify (G_OBJECT (tee), "last_message");
284 res = gst_pad_push (pad, gst_buffer_ref (data->buffer));
285 GST_LOG_OBJECT (tee, "Pushing buffer %p to %" GST_PTR_FORMAT
286 " yielded result=%d", data->buffer, pad, res);
288 /* If it's fatal or OK, or if ret is currently
289 * not-linked, we overwrite the previous value */
290 if (GST_FLOW_IS_FATAL (res) || (res == GST_FLOW_OK) ||
291 (g_value_get_enum (ret) == GST_FLOW_NOT_LINKED)) {
292 GST_LOG_OBJECT (tee, "Replacing ret val %d with %d",
293 g_value_get_enum (ret), res);
294 g_value_set_enum (ret, res);
297 gst_object_unref (pad);
299 /* Stop iterating if flow return is fatal */
300 return (!GST_FLOW_IS_FATAL (res));
304 gst_tee_handle_buffer (GstTee * tee, GstBuffer * buffer)
309 GstIteratorResult res;
311 tee->offset += GST_BUFFER_SIZE (buffer);
313 g_value_init (&ret, GST_TYPE_FLOW_RETURN);
314 g_value_set_enum (&ret, GST_FLOW_NOT_LINKED);
315 iter = gst_element_iterate_src_pads (GST_ELEMENT (tee));
317 data.buffer = buffer;
319 GST_LOG_OBJECT (tee, "Starting to push buffer %p", buffer);
320 /* FIXME: Not sure how tee would handle RESEND buffer from some of the
321 * pads but not from others. */
322 res = gst_iterator_fold (iter, (GstIteratorFoldFunction) gst_tee_do_push,
324 gst_iterator_free (iter);
326 GST_LOG_OBJECT (tee, "Pushing buffer %p yielded result=%d", buffer,
327 g_value_get_enum (&ret));
329 gst_buffer_unref (buffer);
331 /* no need to unset gvalue */
332 return g_value_get_enum (&ret);
336 gst_tee_chain (GstPad * pad, GstBuffer * buffer)
341 tee = GST_TEE (GST_PAD_PARENT (pad));
343 res = gst_tee_handle_buffer (tee, buffer);
348 #define DEFAULT_SIZE 1024
351 gst_tee_loop (GstPad * pad)
357 tee = GST_TEE (GST_PAD_PARENT (pad));
359 res = gst_pad_pull_range (pad, tee->offset, DEFAULT_SIZE, &buffer);
360 if (res != GST_FLOW_OK)
363 res = gst_tee_handle_buffer (tee, buffer);
364 if (res != GST_FLOW_OK)
371 gst_pad_pause_task (pad);
377 gst_tee_sink_activate_push (GstPad * pad, gboolean active)
381 tee = GST_TEE (GST_OBJECT_PARENT (pad));
383 tee->sink_mode = active && GST_ACTIVATE_PUSH;
386 g_return_val_if_fail (tee->has_chain, FALSE);
392 /* won't be called until we implement an activate function */
394 gst_tee_sink_activate_pull (GstPad * pad, gboolean active)
398 tee = GST_TEE (GST_OBJECT_PARENT (pad));
400 tee->sink_mode = active && GST_ACTIVATE_PULL;
403 g_return_val_if_fail (tee->has_sink_loop, FALSE);
404 return gst_pad_start_task (pad, (GstTaskFunction) gst_tee_loop, pad);
406 return gst_pad_stop_task (pad);