don't mix tabs and spaces
[platform/upstream/gstreamer.git] / plugins / elements / gsttee.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gsttee.c: Tee element, one in N 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 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include "gsttee.h"
28
29 #include <string.h>
30
31 GST_DEBUG_CATEGORY_STATIC (gst_tee_debug);
32 #define GST_CAT_DEFAULT gst_tee_debug
33
34 GstElementDetails gst_tee_details = GST_ELEMENT_DETAILS ("Tee pipe fitting",
35     "Generic",
36     "1-to-N pipe fitting",
37     "Erik Walthinsen <omega@cse.ogi.edu>, "
38     "Wim Taymans <wim.taymans@chello.be>");
39
40 /* Tee signals and args */
41 enum
42 {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 enum
48 {
49   ARG_0,
50   ARG_SILENT,
51   ARG_NUM_PADS,
52   ARG_LAST_MESSAGE,
53   /* FILL ME */
54 };
55
56 GstStaticPadTemplate tee_src_template = GST_STATIC_PAD_TEMPLATE ("src%d",
57     GST_PAD_SRC,
58     GST_PAD_REQUEST,
59     GST_STATIC_CAPS_ANY);
60
61 #define _do_init(bla) \
62     GST_DEBUG_CATEGORY_INIT (gst_tee_debug, "tee", 0, "tee element");
63
64 GST_BOILERPLATE_FULL (GstTee, gst_tee, GstElement, GST_TYPE_ELEMENT, _do_init);
65
66 static GstPad *gst_tee_request_new_pad (GstElement * element,
67     GstPadTemplate * temp, const gchar * unused);
68
69 static void gst_tee_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_tee_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73
74 static void gst_tee_chain (GstPad * pad, GstData * _data);
75
76
77 static void
78 gst_tee_base_init (gpointer g_class)
79 {
80   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
81
82   gst_element_class_set_details (gstelement_class, &gst_tee_details);
83   gst_element_class_add_pad_template (gstelement_class,
84       gst_static_pad_template_get (&tee_src_template));
85 }
86 static void
87 gst_tee_class_init (GstTeeClass * klass)
88 {
89   GObjectClass *gobject_class;
90   GstElementClass *gstelement_class;
91
92   gobject_class = (GObjectClass *) klass;
93   gstelement_class = (GstElementClass *) klass;
94
95
96   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_PADS,
97       g_param_spec_int ("num_pads", "num_pads", "num_pads",
98           0, G_MAXINT, 0, G_PARAM_READABLE));
99   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
100       g_param_spec_boolean ("silent", "silent", "silent",
101           TRUE, G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
102   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
103       g_param_spec_string ("last_message", "last_message", "last_message",
104           NULL, G_PARAM_READABLE));
105
106
107   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_tee_set_property);
108   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_tee_get_property);
109
110   gstelement_class->request_new_pad =
111       GST_DEBUG_FUNCPTR (gst_tee_request_new_pad);
112 }
113
114 static void
115 gst_tee_init (GstTee * tee)
116 {
117   tee->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
118   gst_element_add_pad (GST_ELEMENT (tee), tee->sinkpad);
119   gst_pad_set_chain_function (tee->sinkpad, GST_DEBUG_FUNCPTR (gst_tee_chain));
120   gst_pad_set_link_function (tee->sinkpad,
121       GST_DEBUG_FUNCPTR (gst_pad_proxy_pad_link));
122   gst_pad_set_getcaps_function (tee->sinkpad,
123       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
124
125   tee->last_message = NULL;
126 }
127
128 /* helper compare function */
129 gint
130 name_pad_compare (gconstpointer a, gconstpointer b)
131 {
132   GstPad *pad = (GstPad *) a;
133   gchar *name = (gchar *) b;
134
135   g_assert (GST_IS_PAD (pad));
136
137   return strcmp (name, gst_pad_get_name (pad)); /* returns 0 if match */
138 }
139
140 static GstPad *
141 gst_tee_request_new_pad (GstElement * element, GstPadTemplate * templ,
142     const gchar * unused)
143 {
144   gchar *name;
145   GstPad *srcpad;
146   GstTee *tee;
147   gint i = 0;
148   const GList *pads;
149
150   g_return_val_if_fail (GST_IS_TEE (element), NULL);
151
152   if (templ->direction != GST_PAD_SRC) {
153     g_warning ("gsttee: request new pad that is not a SRC pad\n");
154     return NULL;
155   }
156
157   tee = GST_TEE (element);
158
159   /* try names in order and find one that's not in use atm */
160   pads = gst_element_get_pad_list (element);
161
162   name = NULL;
163   while (!name) {
164     name = g_strdup_printf ("src%d", i);
165     if (g_list_find_custom ((GList *) pads, (gconstpointer) name,
166             name_pad_compare) != NULL) {
167       /* this name is taken, use the next one */
168       ++i;
169       g_free (name);
170       name = NULL;
171     }
172   }
173   if (!tee->silent) {
174     g_free (tee->last_message);
175     tee->last_message = g_strdup_printf ("new pad %s", name);
176     g_object_notify (G_OBJECT (tee), "last_message");
177   }
178
179   srcpad = gst_pad_new_from_template (templ, name);
180   g_free (name);
181   gst_pad_set_link_function (srcpad,
182       GST_DEBUG_FUNCPTR (gst_pad_proxy_pad_link));
183   gst_pad_set_getcaps_function (srcpad,
184       GST_DEBUG_FUNCPTR (gst_pad_proxy_getcaps));
185   gst_element_add_pad (GST_ELEMENT (tee), srcpad);
186   GST_PAD_ELEMENT_PRIVATE (srcpad) = NULL;
187
188   if (GST_PAD_CAPS (tee->sinkpad)) {
189     gst_pad_try_set_caps (srcpad, GST_PAD_CAPS (tee->sinkpad));
190   }
191
192   return srcpad;
193 }
194
195 static void
196 gst_tee_set_property (GObject * object, guint prop_id, const GValue * value,
197     GParamSpec * pspec)
198 {
199   GstTee *tee;
200
201   /* it's not null if we got it, but it might not be ours */
202   g_return_if_fail (GST_IS_TEE (object));
203
204   tee = GST_TEE (object);
205
206   switch (prop_id) {
207     case ARG_SILENT:
208       tee->silent = g_value_get_boolean (value);
209       g_object_notify (G_OBJECT (tee), "silent");
210       break;
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214   }
215 }
216
217 static void
218 gst_tee_get_property (GObject * object, guint prop_id, GValue * value,
219     GParamSpec * pspec)
220 {
221   GstTee *tee;
222
223   /* it's not null if we got it, but it might not be ours */
224   g_return_if_fail (GST_IS_TEE (object));
225
226   tee = GST_TEE (object);
227
228   switch (prop_id) {
229     case ARG_NUM_PADS:
230       g_value_set_int (value, GST_ELEMENT (tee)->numsrcpads);
231       break;
232     case ARG_SILENT:
233       g_value_set_boolean (value, tee->silent);
234       break;
235     case ARG_LAST_MESSAGE:
236       g_value_set_string ((GValue *) value, tee->last_message);
237       break;
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240       break;
241   }
242 }
243
244 /**
245  * gst_tee_chain:
246  * @pad: the pad to follow
247  * @buf: the buffer to pass
248  *
249  * Chain a buffer on a pad.
250  */
251 static void
252 gst_tee_chain (GstPad * pad, GstData * _data)
253 {
254   GstBuffer *buf = GST_BUFFER (_data);
255   GstTee *tee;
256   const GList *pads;
257
258   g_return_if_fail (pad != NULL);
259   g_return_if_fail (GST_IS_PAD (pad));
260   g_return_if_fail (buf != NULL);
261
262   tee = GST_TEE (gst_pad_get_parent (pad));
263
264   gst_buffer_ref_by_count (buf, GST_ELEMENT (tee)->numsrcpads - 1);
265
266   pads = gst_element_get_pad_list (GST_ELEMENT (tee));
267
268   while (pads) {
269     GstPad *outpad = GST_PAD (pads->data);
270
271     pads = g_list_next (pads);
272
273     if (GST_PAD_DIRECTION (outpad) != GST_PAD_SRC)
274       continue;
275
276     if (!tee->silent) {
277       g_free (tee->last_message);
278       tee->last_message =
279           g_strdup_printf ("chain        ******* (%s:%s)t (%d bytes, %"
280           G_GUINT64_FORMAT ") %p", GST_DEBUG_PAD_NAME (outpad),
281           GST_BUFFER_SIZE (buf), GST_BUFFER_TIMESTAMP (buf), buf);
282       g_object_notify (G_OBJECT (tee), "last_message");
283     }
284
285     if (GST_PAD_IS_USABLE (outpad))
286       gst_pad_push (outpad, GST_DATA (buf));
287     else
288       gst_buffer_unref (buf);
289   }
290 }