b96688a2149dd400bf9e8c0b0517c1cd09d8dc84
[platform/upstream/gstreamer.git] / gst / gstpadtemplate.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpadtemplate.c: Templates for pad creation
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 /**
24  * SECTION:gstpadtemplate
25  * @short_description: Describe the media type of a pad.
26  * @see_also: #GstPad, #GstElementFactory
27  *
28  * Padtemplates describe the possible media types a pad or an elementfactory can
29  * handle.
30  *
31  * Pad and PadTemplates have #GstCaps attached to it to describe the media type
32  * they are capable of dealing with. gst_pad_template_get_caps() is used to get
33  * the caps of a padtemplate. It's not possible to modify the caps of a
34  * padtemplate after creation.
35  *
36  * Padtemplates can be created with gst_pad_template_new() or with the
37  * convenient GST_PAD_TEMPLATE_FACTORY() macro. A padtemplate can be used to
38  * create a pad or to add to an elementfactory.
39  *
40  * The following code example shows the code to create a pad from a padtemplate.
41  * <example>
42  * <title>Create a pad from a padtemplate</title>
43  *   <programlisting>
44  *   GstStaticPadTemplate my_template =
45  *   GST_STATIC_PAD_TEMPLATE (
46  *     "sink",          // the name of the pad
47  *     GST_PAD_SINK,    // the direction of the pad
48  *     GST_PAD_ALWAYS,  // when this pad will be present
49  *     GST_STATIC_CAPS (        // the capabilities of the padtemplate
50  *       "audio/x-raw-int, "
51  *         "channels = (int) [ 1, 6 ]"
52  *     )
53  *   )
54  *   void
55  *   my_method (void)
56  *   {
57  *     GstPad *pad;
58  *     pad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (my_template_factory), "sink");
59  *     ...
60  *   }
61  *   </programlisting>
62  * </example>
63  *
64  * The following example shows you how to add the padtemplate to an
65  * elementfactory:
66  * <informalexample>
67  *   <programlisting>
68  *   gboolean
69  *   my_factory_init (GstPlugin *plugin)
70  *   {
71  *     GstElementFactory *factory;
72  *     factory = gst_element_factory_new ("my_factory", GST_TYPE_MYFACTORY, &amp;gst_myfactory_details);
73  *     g_return_val_if_fail (factory != NULL, FALSE);
74  *     gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (my_template_factory));
75  *     gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
76  *     return TRUE;
77  *   }
78  *   </programlisting>
79  * </informalexample>
80  */
81
82 #include "gst_private.h"
83
84 #include "gstpad.h"
85 #include "gstpadtemplate.h"
86 #include "gstenumtypes.h"
87 #include "gstmarshal.h"
88 #include "gstutils.h"
89 #include "gstinfo.h"
90 #include "gsterror.h"
91 #include "gstvalue.h"
92
93 #define GST_CAT_DEFAULT GST_CAT_PADS
94
95 enum
96 {
97   TEMPL_PAD_CREATED,
98   /* FILL ME */
99   LAST_SIGNAL
100 };
101
102 static GstObject *parent_class = NULL;
103 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
104
105 static void gst_pad_template_class_init (GstPadTemplateClass * klass);
106 static void gst_pad_template_init (GstPadTemplate * templ);
107 static void gst_pad_template_dispose (GObject * object);
108
109 GType
110 gst_pad_template_get_type (void)
111 {
112   static GType padtemplate_type = 0;
113
114   if (!padtemplate_type) {
115     static const GTypeInfo padtemplate_info = {
116       sizeof (GstPadTemplateClass), NULL, NULL,
117       (GClassInitFunc) gst_pad_template_class_init, NULL, NULL,
118       sizeof (GstPadTemplate),
119       0,
120       (GInstanceInitFunc) gst_pad_template_init, NULL
121     };
122
123     padtemplate_type =
124         g_type_register_static (GST_TYPE_OBJECT, "GstPadTemplate",
125         &padtemplate_info, 0);
126   }
127   return padtemplate_type;
128 }
129
130 static void
131 gst_pad_template_class_init (GstPadTemplateClass * klass)
132 {
133   GObjectClass *gobject_class;
134   GstObjectClass *gstobject_class;
135
136   gobject_class = (GObjectClass *) klass;
137   gstobject_class = (GstObjectClass *) klass;
138
139   parent_class = g_type_class_ref (GST_TYPE_OBJECT);
140
141   /**
142    * GstPadTemplate::pad-created:
143    * @pad_template: the object which received the signal.
144    * @pad: the pad that was created.
145    *
146    * This signal is fired when an element creates a pad from this template.
147    */
148   gst_pad_template_signals[TEMPL_PAD_CREATED] =
149       g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
150       G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
151       NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
152
153   gobject_class->dispose = gst_pad_template_dispose;
154
155   gstobject_class->path_string_separator = "*";
156 }
157
158 static void
159 gst_pad_template_init (GstPadTemplate * templ)
160 {
161 }
162
163 static void
164 gst_pad_template_dispose (GObject * object)
165 {
166   GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
167
168   g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
169   if (GST_PAD_TEMPLATE_CAPS (templ)) {
170     gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
171   }
172
173   G_OBJECT_CLASS (parent_class)->dispose (object);
174 }
175
176 /* ALWAYS padtemplates cannot have conversion specifications (like src_%d),
177  * since it doesn't make sense.
178  * SOMETIMES padtemplates can do whatever they want, they are provided by the
179  * element.
180  * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
181  * 'sink%d' template is automatically selected), so we need to restrict their
182  * naming.
183  */
184 static gboolean
185 name_is_valid (const gchar * name, GstPadPresence presence)
186 {
187   const gchar *str;
188
189   if (presence == GST_PAD_ALWAYS) {
190     if (strchr (name, '%')) {
191       g_warning ("invalid name template %s: conversion specifications are not"
192           " allowed for GST_PAD_ALWAYS padtemplates", name);
193       return FALSE;
194     }
195   } else if (presence == GST_PAD_REQUEST) {
196     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
197       g_warning ("invalid name template %s: only one conversion specification"
198           " allowed in GST_PAD_REQUEST padtemplate", name);
199       return FALSE;
200     }
201     if (str && (*(str + 1) != 's' && *(str + 1) != 'd')) {
202       g_warning ("invalid name template %s: conversion specification must be of"
203           " type '%%d' or '%%s' for GST_PAD_REQUEST padtemplate", name);
204       return FALSE;
205     }
206     if (str && (*(str + 2) != '\0')) {
207       g_warning ("invalid name template %s: conversion specification must"
208           " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
209       return FALSE;
210     }
211   }
212
213   return TRUE;
214 }
215
216 /**
217  * gst_static_pad_template_get:
218  * @pad_template: the static pad template
219  *
220  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
221  *
222  * Returns: a new #GstPadTemplate.
223  */
224 GstPadTemplate *
225 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
226 {
227   GstPadTemplate *new;
228
229   if (!name_is_valid (pad_template->name_template, pad_template->presence))
230     return NULL;
231
232   new = g_object_new (gst_pad_template_get_type (),
233       "name", pad_template->name_template, NULL);
234
235   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (pad_template->name_template);
236   GST_PAD_TEMPLATE_DIRECTION (new) = pad_template->direction;
237   GST_PAD_TEMPLATE_PRESENCE (new) = pad_template->presence;
238
239   GST_PAD_TEMPLATE_CAPS (new) =
240       gst_caps_copy (gst_static_caps_get (&pad_template->static_caps));
241
242   return new;
243 }
244
245 /**
246  * gst_pad_template_new:
247  * @name_template: the name template.
248  * @direction: the #GstPadDirection of the template.
249  * @presence: the #GstPadPresence of the pad.
250  * @caps: a #GstCaps set for the template. The caps are taken ownership of.
251  *
252  * Creates a new pad template with a name according to the given template
253  * and with the given arguments. This functions takes ownership of the provided
254  * caps, so be sure to not use them afterwards.
255  *
256  * Returns: a new #GstPadTemplate.
257  */
258 GstPadTemplate *
259 gst_pad_template_new (const gchar * name_template,
260     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
261 {
262   GstPadTemplate *new;
263
264   g_return_val_if_fail (name_template != NULL, NULL);
265   g_return_val_if_fail (caps != NULL, NULL);
266   g_return_val_if_fail (direction == GST_PAD_SRC
267       || direction == GST_PAD_SINK, NULL);
268   g_return_val_if_fail (presence == GST_PAD_ALWAYS
269       || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
270
271   if (!name_is_valid (name_template, presence)) {
272     gst_caps_unref (caps);
273     return NULL;
274   }
275
276   new = g_object_new (gst_pad_template_get_type (),
277       "name", name_template, NULL);
278
279   GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (name_template);
280   GST_PAD_TEMPLATE_DIRECTION (new) = direction;
281   GST_PAD_TEMPLATE_PRESENCE (new) = presence;
282   GST_PAD_TEMPLATE_CAPS (new) = caps;
283
284   return new;
285 }
286
287 /**
288  * gst_static_pad_template_get_caps:
289  * @templ: a #GstStaticPadTemplate to get capabilities of.
290  *
291  * Gets the capabilities of the static pad template.
292  *
293  * Returns: the #GstCaps of the static pad template. If you need to keep a
294  * reference to the caps, take a ref (see gst_caps_ref ()).
295  */
296 GstCaps *
297 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
298 {
299   g_return_val_if_fail (templ, NULL);
300
301   return (GstCaps *) gst_static_caps_get (&templ->static_caps);
302 }
303
304 /**
305  * gst_pad_template_get_caps:
306  * @templ: a #GstPadTemplate to get capabilities of.
307  *
308  * Gets the capabilities of the pad template.
309  *
310  * Returns: the #GstCaps of the pad template. If you need to keep a reference to
311  * the caps, take a ref (see gst_caps_ref ()).
312  */
313 GstCaps *
314 gst_pad_template_get_caps (GstPadTemplate * templ)
315 {
316   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
317
318   return GST_PAD_TEMPLATE_CAPS (templ);
319 }
320
321 void
322 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
323 {
324   gst_object_sink (GST_OBJECT (templ));
325   g_signal_emit (G_OBJECT (templ),
326       gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
327 }