docs: fix problems introduced by c068b225fef5a9bf0
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, 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. This allows for both inspection of handled types before loading the
30  * element plugin as well as identifying pads on elements that are not yet
31  * created (request or sometimes pads).
32  *
33  * Pad and PadTemplates have #GstCaps attached to it to describe the media type
34  * they are capable of dealing with. gst_pad_template_get_caps() or
35  * GST_PAD_TEMPLATE_CAPS() are used to get the caps of a padtemplate. It's not
36  * possible to modify the caps of a padtemplate after creation.
37  *
38  * PadTemplates have a #GstPadPresence property which identifies the lifetime
39  * of the pad and that can be retrieved with GST_PAD_TEMPLATE_PRESENCE(). Also
40  * the direction of the pad can be retrieved from the #GstPadTemplate with
41  * GST_PAD_TEMPLATE_DIRECTION().
42  *
43  * The GST_PAD_TEMPLATE_NAME_TEMPLATE () is important for GST_PAD_REQUEST pads
44  * because it has to be used as the name in the gst_element_get_request_pad()
45  * call to instantiate a pad from this template.
46  *
47  * Padtemplates can be created with gst_pad_template_new() or with
48  * gst_static_pad_template_get (), which creates a #GstPadTemplate from a
49  * #GstStaticPadTemplate that can be filled with the
50  * convenient GST_STATIC_PAD_TEMPLATE() macro.
51  *
52  * A padtemplate can be used to create a pad (see gst_pad_new_from_template()
53  * or gst_pad_new_from_static_template ()) or to add to an element class
54  * (see gst_element_class_add_pad_template ()).
55  *
56  * The following code example shows the code to create a pad from a padtemplate.
57  * |[
58  *   GstStaticPadTemplate my_template =
59  *   GST_STATIC_PAD_TEMPLATE (
60  *     "sink",          // the name of the pad
61  *     GST_PAD_SINK,    // the direction of the pad
62  *     GST_PAD_ALWAYS,  // when this pad will be present
63  *     GST_STATIC_CAPS (        // the capabilities of the padtemplate
64  *       "audio/x-raw, "
65  *         "channels = (int) [ 1, 6 ]"
66  *     )
67  *   );
68  *   void
69  *   my_method (void)
70  *   {
71  *     GstPad *pad;
72  *     pad = gst_pad_new_from_static_template (&amp;my_template, "sink");
73  *     ...
74  *   }
75  * ]|
76  *
77  * The following example shows you how to add the padtemplate to an
78  * element class, this is usually done in the class_init of the class:
79  * |[
80  *   static void
81  *   my_element_class_init (GstMyElementClass *klass)
82  *   {
83  *     GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
84  *
85  *     gst_element_class_add_pad_template (gstelement_class,
86  *         gst_static_pad_template_get (&amp;my_template));
87  *   }
88  * ]|
89  *
90  * Last reviewed on 2006-02-14 (0.10.3)
91  */
92
93 #include "gst_private.h"
94
95 #include "gstpad.h"
96 #include "gstpadtemplate.h"
97 #include "gstenumtypes.h"
98 #include "gstutils.h"
99 #include "gstinfo.h"
100 #include "gsterror.h"
101 #include "gstvalue.h"
102
103 #define GST_CAT_DEFAULT GST_CAT_PADS
104
105 enum
106 {
107   PROP_NAME_TEMPLATE = 1,
108   PROP_DIRECTION,
109   PROP_PRESENCE,
110   PROP_CAPS
111 };
112
113 enum
114 {
115   TEMPL_PAD_CREATED,
116   /* FILL ME */
117   LAST_SIGNAL
118 };
119
120 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
121
122 static void gst_pad_template_dispose (GObject * object);
123 static void gst_pad_template_set_property (GObject * object, guint prop_id,
124     const GValue * value, GParamSpec * pspec);
125 static void gst_pad_template_get_property (GObject * object, guint prop_id,
126     GValue * value, GParamSpec * pspec);
127
128 #define gst_pad_template_parent_class parent_class
129 G_DEFINE_TYPE (GstPadTemplate, gst_pad_template, GST_TYPE_OBJECT);
130
131 static void
132 gst_pad_template_class_init (GstPadTemplateClass * klass)
133 {
134   GObjectClass *gobject_class;
135   GstObjectClass *gstobject_class;
136
137   gobject_class = (GObjectClass *) klass;
138   gstobject_class = (GstObjectClass *) klass;
139
140   /**
141    * GstPadTemplate::pad-created:
142    * @pad_template: the object which received the signal.
143    * @pad: the pad that was created.
144    *
145    * This signal is fired when an element creates a pad from this template.
146    */
147   gst_pad_template_signals[TEMPL_PAD_CREATED] =
148       g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
149       G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
150       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
151
152   gobject_class->dispose = gst_pad_template_dispose;
153
154   gobject_class->get_property = gst_pad_template_get_property;
155   gobject_class->set_property = gst_pad_template_set_property;
156
157   /**
158    * GstPadTemplate:name-template:
159    *
160    * The name template of the pad template.
161    */
162   g_object_class_install_property (gobject_class, PROP_NAME_TEMPLATE,
163       g_param_spec_string ("name-template", "Name template",
164           "The name template of the pad template", NULL,
165           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
166
167   /**
168    * GstPadTemplate:direction:
169    *
170    * The direction of the pad described by the pad template.
171    */
172   g_object_class_install_property (gobject_class, PROP_DIRECTION,
173       g_param_spec_enum ("direction", "Direction",
174           "The direction of the pad described by the pad template",
175           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
176           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
177
178   /**
179    * GstPadTemplate:presence:
180    *
181    * When the pad described by the pad template will become available.
182    */
183   g_object_class_install_property (gobject_class, PROP_PRESENCE,
184       g_param_spec_enum ("presence", "Presence",
185           "When the pad described by the pad template will become available",
186           GST_TYPE_PAD_PRESENCE, GST_PAD_ALWAYS,
187           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
188
189   /**
190    * GstPadTemplate:caps:
191    *
192    * The capabilities of the pad described by the pad template.
193    */
194   g_object_class_install_property (gobject_class, PROP_CAPS,
195       g_param_spec_boxed ("caps", "Caps",
196           "The capabilities of the pad described by the pad template",
197           GST_TYPE_CAPS,
198           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
199
200   gstobject_class->path_string_separator = "*";
201 }
202
203 static void
204 gst_pad_template_init (GstPadTemplate * templ)
205 {
206 }
207
208 static void
209 gst_pad_template_dispose (GObject * object)
210 {
211   GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
212
213   g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
214   if (GST_PAD_TEMPLATE_CAPS (templ)) {
215     gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
216   }
217
218   G_OBJECT_CLASS (parent_class)->dispose (object);
219 }
220
221 /* ALWAYS padtemplates cannot have conversion specifications (like src_%d),
222  * since it doesn't make sense.
223  * SOMETIMES padtemplates can do whatever they want, they are provided by the
224  * element.
225  * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
226  * 'sink%d' template is automatically selected), so we need to restrict their
227  * naming.
228  */
229 static gboolean
230 name_is_valid (const gchar * name, GstPadPresence presence)
231 {
232   const gchar *str;
233
234   if (presence == GST_PAD_ALWAYS) {
235     if (strchr (name, '%')) {
236       g_warning ("invalid name template %s: conversion specifications are not"
237           " allowed for GST_PAD_ALWAYS padtemplates", name);
238       return FALSE;
239     }
240   } else if (presence == GST_PAD_REQUEST) {
241     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
242       g_warning ("invalid name template %s: only one conversion specification"
243           " allowed in GST_PAD_REQUEST padtemplate", name);
244       return FALSE;
245     }
246     if (str && (*(str + 1) != 's' && *(str + 1) != 'd' && *(str + 1) != 'u')) {
247       g_warning ("invalid name template %s: conversion specification must be of"
248           " type '%%d', '%%u' or '%%s' for GST_PAD_REQUEST padtemplate", name);
249       return FALSE;
250     }
251     if (str && (*(str + 2) != '\0')) {
252       g_warning ("invalid name template %s: conversion specification must"
253           " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
254       return FALSE;
255     }
256   }
257
258   return TRUE;
259 }
260
261 G_DEFINE_POINTER_TYPE (GstStaticPadTemplate, gst_static_pad_template);
262
263 /**
264  * gst_static_pad_template_get:
265  * @pad_template: the static pad template
266  *
267  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
268  *
269  * Returns: (transfer full): a new #GstPadTemplate.
270  */
271 /* FIXME0.11: rename to gst_pad_template_new_from_static_pad_template() */
272 GstPadTemplate *
273 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
274 {
275   GstPadTemplate *new;
276   GstCaps *caps;
277
278   if (!name_is_valid (pad_template->name_template, pad_template->presence))
279     return NULL;
280
281   caps = gst_static_caps_get (&pad_template->static_caps);
282
283   new = g_object_new (gst_pad_template_get_type (),
284       "name", pad_template->name_template,
285       "name-template", pad_template->name_template,
286       "direction", pad_template->direction,
287       "presence", pad_template->presence, "caps", caps, NULL);
288
289   gst_caps_unref (caps);
290
291   return new;
292 }
293
294 /**
295  * gst_pad_template_new:
296  * @name_template: the name template.
297  * @direction: the #GstPadDirection of the template.
298  * @presence: the #GstPadPresence of the pad.
299  * @caps: (transfer none): a #GstCaps set for the template.
300  *
301  * Creates a new pad template with a name according to the given template
302  * and with the given arguments.
303  *
304  * Returns: (transfer floating): a new #GstPadTemplate.
305  */
306 GstPadTemplate *
307 gst_pad_template_new (const gchar * name_template,
308     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
309 {
310   GstPadTemplate *new;
311
312   g_return_val_if_fail (name_template != NULL, NULL);
313   g_return_val_if_fail (caps != NULL, NULL);
314   g_return_val_if_fail (direction == GST_PAD_SRC
315       || direction == GST_PAD_SINK, NULL);
316   g_return_val_if_fail (presence == GST_PAD_ALWAYS
317       || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
318
319   if (!name_is_valid (name_template, presence)) {
320     return NULL;
321   }
322
323   new = g_object_new (gst_pad_template_get_type (),
324       "name", name_template, "name-template", name_template,
325       "direction", direction, "presence", presence, "caps", caps, NULL);
326
327   return new;
328 }
329
330 /**
331  * gst_static_pad_template_get_caps:
332  * @templ: a #GstStaticPadTemplate to get capabilities of.
333  *
334  * Gets the capabilities of the static pad template.
335  *
336  * Returns: (transfer full): the #GstCaps of the static pad template.
337  * Unref after usage. Since the core holds an additional
338  * ref to the returned caps, use gst_caps_make_writable()
339  * on the returned caps to modify it.
340  */
341 GstCaps *
342 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
343 {
344   g_return_val_if_fail (templ, NULL);
345
346   return gst_static_caps_get (&templ->static_caps);
347 }
348
349 /**
350  * gst_pad_template_get_caps:
351  * @templ: a #GstPadTemplate to get capabilities of.
352  *
353  * Gets the capabilities of the pad template.
354  *
355  * Returns: (transfer full): the #GstCaps of the pad template.
356  * Unref after usage.
357  */
358 GstCaps *
359 gst_pad_template_get_caps (GstPadTemplate * templ)
360 {
361   GstCaps *caps;
362   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
363
364   caps = GST_PAD_TEMPLATE_CAPS (templ);
365
366   return (caps ? gst_caps_ref (caps) : NULL);
367 }
368
369 /**
370  * gst_pad_template_pad_created:
371  * @templ: a #GstPadTemplate that has been created
372  * @pad:   the #GstPad that created it
373  *
374  * Emit the pad-created signal for this template when created by this pad.
375  */
376 void
377 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
378 {
379   g_signal_emit (templ, gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
380 }
381
382 static void
383 gst_pad_template_set_property (GObject * object, guint prop_id,
384     const GValue * value, GParamSpec * pspec)
385 {
386   /* these properties are all construct-only */
387   switch (prop_id) {
388     case PROP_NAME_TEMPLATE:
389       GST_PAD_TEMPLATE_NAME_TEMPLATE (object) = g_value_dup_string (value);
390       break;
391     case PROP_DIRECTION:
392       GST_PAD_TEMPLATE_DIRECTION (object) =
393           (GstPadDirection) g_value_get_enum (value);
394       break;
395     case PROP_PRESENCE:
396       GST_PAD_TEMPLATE_PRESENCE (object) =
397           (GstPadPresence) g_value_get_enum (value);
398       break;
399     case PROP_CAPS:
400       GST_PAD_TEMPLATE_CAPS (object) = g_value_dup_boxed (value);
401       break;
402     default:
403       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404       break;
405   }
406 }
407
408 static void
409 gst_pad_template_get_property (GObject * object, guint prop_id, GValue * value,
410     GParamSpec * pspec)
411 {
412   /* these properties are all construct-only */
413   switch (prop_id) {
414     case PROP_NAME_TEMPLATE:
415       g_value_set_string (value, GST_PAD_TEMPLATE_NAME_TEMPLATE (object));
416       break;
417     case PROP_DIRECTION:
418       g_value_set_enum (value, GST_PAD_TEMPLATE_DIRECTION (object));
419       break;
420     case PROP_PRESENCE:
421       g_value_set_enum (value, GST_PAD_TEMPLATE_PRESENCE (object));
422       break;
423     case PROP_CAPS:
424       g_value_set_boxed (value, GST_PAD_TEMPLATE_CAPS (object));
425       break;
426     default:
427       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428       break;
429   }
430 }