2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@chello.be>
5 * gsttee.c: Tee element, one in N 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.
31 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
36 GST_DEBUG_CATEGORY_STATIC (gst_tee_debug);
37 #define GST_CAT_DEFAULT gst_tee_debug
39 GstElementDetails gst_tee_details = GST_ELEMENT_DETAILS ("Tee pipe fitting",
41 "1-to-N pipe fitting",
42 "Erik Walthinsen <omega@cse.ogi.edu>, "
43 "Wim Taymans <wim.taymans@chello.be>");
45 /* Tee signals and args */
61 GstStaticPadTemplate tee_src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
66 #define _do_init(bla) \
67 GST_DEBUG_CATEGORY_INIT (gst_tee_debug, "tee", 0, "tee element");
69 GST_BOILERPLATE_FULL (GstTee, gst_tee, GstElement, GST_TYPE_ELEMENT, _do_init);
71 static GstPad *gst_tee_request_new_pad (GstElement * element,
72 GstPadTemplate * temp, const gchar * unused);
74 static void gst_tee_finalize (GObject * object);
75 static void gst_tee_set_property (GObject * object, guint prop_id,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_tee_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
80 static void gst_tee_chain (GstPad * pad, GstData * _data);
84 gst_tee_base_init (gpointer g_class)
86 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
88 gst_element_class_add_pad_template (gstelement_class,
89 gst_static_pad_template_get (&sinktemplate));
90 gst_element_class_set_details (gstelement_class, &gst_tee_details);
91 gst_element_class_add_pad_template (gstelement_class,
92 gst_static_pad_template_get (&tee_src_template));
96 gst_tee_finalize (GObject * object)
100 tee = GST_TEE (object);
102 g_free (tee->last_message);
104 G_OBJECT_CLASS (parent_class)->finalize (object);
108 gst_tee_class_init (GstTeeClass * klass)
110 GObjectClass *gobject_class;
111 GstElementClass *gstelement_class;
113 gobject_class = (GObjectClass *) klass;
114 gstelement_class = (GstElementClass *) klass;
117 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
118 g_param_spec_int ("num_pads", "num_pads", "num_pads",
119 0, G_MAXINT, 0, G_PARAM_READABLE));
120 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
121 g_param_spec_boolean ("silent", "silent", "silent",
122 TRUE, G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
123 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
124 g_param_spec_string ("last_message", "last_message", "last_message",
125 NULL, G_PARAM_READABLE));
128 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tee_finalize);
129 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_tee_set_property);
130 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_tee_get_property);
132 gstelement_class->request_new_pad =
133 GST_DEBUG_FUNCPTR (gst_tee_request_new_pad);
137 gst_tee_init (GstTee * tee)
140 gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate),
142 gst_element_add_pad (GST_ELEMENT (tee), tee->sinkpad);
143 gst_pad_set_chain_function (tee->sinkpad, GST_DEBUG_FUNCPTR (gst_tee_chain));
144 //gst_pad_set_link_function (tee->sinkpad, GST_DEBUG_FUNCPTR (gst_pad_proxy_pad_link));
145 gst_pad_set_getcaps_function (tee->sinkpad,
146 GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
148 tee->last_message = NULL;
151 /* helper compare function */
153 name_pad_compare (gconstpointer a, gconstpointer b)
155 GstPad *pad = (GstPad *) a;
156 gchar *name = (gchar *) b;
158 g_assert (GST_IS_PAD (pad));
160 return strcmp (name, gst_pad_get_name (pad)); /* returns 0 if match */
164 gst_tee_getcaps (GstPad * _pad)
166 GstTee *tee = GST_TEE (gst_pad_get_parent (_pad));
167 GstCaps *caps = gst_caps_new_any (), *tmp, *res;
171 for (pads = GST_ELEMENT (tee)->pads; pads != NULL; pads = pads->next) {
172 pad = GST_PAD (pads->data);
176 tmp = gst_pad_get_allowed_caps (pad);
177 res = gst_caps_intersect (caps, tmp);
178 gst_caps_unref (tmp);
179 gst_caps_unref (caps);
186 static GstPadLinkReturn
187 gst_tee_link (GstPad * _pad, const GstCaps * caps)
189 GstTee *tee = GST_TEE (gst_pad_get_parent (_pad));
190 GstPadLinkReturn res;
194 GST_DEBUG_OBJECT (tee, "Forwarding link to all other pads");
196 for (pads = GST_ELEMENT (tee)->pads; pads != NULL; pads = pads->next) {
197 pad = GST_PAD (pads->data);
201 res = gst_pad_try_set_caps (pad, caps);
202 GST_DEBUG_OBJECT (tee, "Pad %s:%s gave response %d",
203 GST_DEBUG_PAD_NAME (pad), res);
204 if (GST_PAD_LINK_FAILED (res))
208 return GST_PAD_LINK_OK;
212 gst_tee_request_new_pad (GstElement * element, GstPadTemplate * templ,
213 const gchar * unused)
221 g_return_val_if_fail (GST_IS_TEE (element), NULL);
223 if (templ->direction != GST_PAD_SRC) {
224 g_warning ("gsttee: request new pad that is not a SRC pad\n");
228 tee = GST_TEE (element);
230 /* try names in order and find one that's not in use atm */
231 pads = element->pads;
235 name = g_strdup_printf ("src%d", i);
236 if (g_list_find_custom ((GList *) pads, (gconstpointer) name,
237 name_pad_compare) != NULL) {
238 /* this name is taken, use the next one */
245 g_free (tee->last_message);
246 tee->last_message = g_strdup_printf ("new pad %s", name);
247 g_object_notify (G_OBJECT (tee), "last_message");
250 srcpad = gst_pad_new_from_template (templ, name);
252 gst_pad_set_link_function (srcpad, GST_DEBUG_FUNCPTR (gst_tee_link));
253 gst_pad_set_getcaps_function (srcpad, GST_DEBUG_FUNCPTR (gst_tee_getcaps));
254 gst_element_add_pad (GST_ELEMENT (tee), srcpad);
255 GST_PAD_ELEMENT_PRIVATE (srcpad) = NULL;
257 if (GST_PAD_CAPS (tee->sinkpad)) {
258 gst_pad_try_set_caps (srcpad, GST_PAD_CAPS (tee->sinkpad));
265 gst_tee_set_property (GObject * object, guint prop_id, const GValue * value,
270 /* it's not null if we got it, but it might not be ours */
271 g_return_if_fail (GST_IS_TEE (object));
273 tee = GST_TEE (object);
277 tee->silent = g_value_get_boolean (value);
278 g_object_notify (G_OBJECT (tee), "silent");
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 gst_tee_get_property (GObject * object, guint prop_id, GValue * value,
292 /* it's not null if we got it, but it might not be ours */
293 g_return_if_fail (GST_IS_TEE (object));
295 tee = GST_TEE (object);
299 g_value_set_int (value, GST_ELEMENT (tee)->numsrcpads);
302 g_value_set_boolean (value, tee->silent);
304 case ARG_LAST_MESSAGE:
305 g_value_set_string (value, tee->last_message);
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 * @pad: the pad to follow
316 * @buf: the buffer to pass
318 * Chain a buffer on a pad.
321 gst_tee_chain (GstPad * pad, GstData * _data)
323 GstBuffer *buf = GST_BUFFER (_data);
327 g_return_if_fail (pad != NULL);
328 g_return_if_fail (GST_IS_PAD (pad));
329 g_return_if_fail (buf != NULL);
331 tee = GST_TEE (gst_pad_get_parent (pad));
333 gst_buffer_ref_by_count (buf, GST_ELEMENT (tee)->numsrcpads - 1);
335 pads = GST_ELEMENT (tee)->pads;
338 GstPad *outpad = GST_PAD (pads->data);
340 pads = g_list_next (pads);
342 if (GST_PAD_DIRECTION (outpad) != GST_PAD_SRC)
346 g_free (tee->last_message);
348 g_strdup_printf ("chain ******* (%s:%s)t (%d bytes, %"
349 G_GUINT64_FORMAT ") %p", GST_DEBUG_PAD_NAME (outpad),
350 GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf), buf);
351 g_object_notify (G_OBJECT (tee), "last_message");
354 if (GST_PAD_IS_USABLE (outpad))
355 gst_pad_push (outpad, GST_DATA (buf));
357 gst_buffer_unref (buf);