2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpadtemplate.c: Templates for pad creation
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:gstpadtemplate
25 * @short_description: Describe the media type of a pad.
26 * @see_also: #GstPad, #GstElementFactory
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).
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.
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().
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.
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.
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 ()).
56 * The following code example shows the code to create a pad from a padtemplate.
58 * <title>Create a pad from a padtemplate</title>
60 * GstStaticPadTemplate my_template =
61 * GST_STATIC_PAD_TEMPLATE (
62 * "sink", // the name of the pad
63 * GST_PAD_SINK, // the direction of the pad
64 * GST_PAD_ALWAYS, // when this pad will be present
65 * GST_STATIC_CAPS ( // the capabilities of the padtemplate
67 * "channels = (int) [ 1, 6 ]"
74 * pad = gst_pad_new_from_static_template (&my_template, "sink");
80 * The following example shows you how to add the padtemplate to an
81 * element class, this is usually done in the class_init of the class:
85 * my_element_class_init (GstMyElementClass *klass)
87 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
89 * gst_element_class_add_pad_template (gstelement_class,
90 * gst_static_pad_template_get (&my_template));
95 * Last reviewed on 2006-02-14 (0.10.3)
98 #include "gst_private.h"
101 #include "gstpadtemplate.h"
102 #include "gstenumtypes.h"
103 #include "gstutils.h"
105 #include "gsterror.h"
106 #include "gstvalue.h"
108 #define GST_CAT_DEFAULT GST_CAT_PADS
112 PROP_NAME_TEMPLATE = 1,
125 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
127 static void gst_pad_template_dispose (GObject * object);
128 static void gst_pad_template_set_property (GObject * object, guint prop_id,
129 const GValue * value, GParamSpec * pspec);
130 static void gst_pad_template_get_property (GObject * object, guint prop_id,
131 GValue * value, GParamSpec * pspec);
133 #define gst_pad_template_parent_class parent_class
134 G_DEFINE_TYPE (GstPadTemplate, gst_pad_template, GST_TYPE_OBJECT);
137 gst_pad_template_class_init (GstPadTemplateClass * klass)
139 GObjectClass *gobject_class;
140 GstObjectClass *gstobject_class;
142 gobject_class = (GObjectClass *) klass;
143 gstobject_class = (GstObjectClass *) klass;
146 * GstPadTemplate::pad-created:
147 * @pad_template: the object which received the signal.
148 * @pad: the pad that was created.
150 * This signal is fired when an element creates a pad from this template.
152 gst_pad_template_signals[TEMPL_PAD_CREATED] =
153 g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
154 G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
155 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
157 gobject_class->dispose = gst_pad_template_dispose;
159 gobject_class->get_property = gst_pad_template_get_property;
160 gobject_class->set_property = gst_pad_template_set_property;
163 * GstPadTemplate:name-template:
165 * The name template of the pad template.
167 g_object_class_install_property (gobject_class, PROP_NAME_TEMPLATE,
168 g_param_spec_string ("name-template", "Name template",
169 "The name template of the pad template", NULL,
170 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
173 * GstPadTemplate:direction:
175 * The direction of the pad described by the pad template.
177 g_object_class_install_property (gobject_class, PROP_DIRECTION,
178 g_param_spec_enum ("direction", "Direction",
179 "The direction of the pad described by the pad template",
180 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
181 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
184 * GstPadTemplate:presence:
186 * When the pad described by the pad template will become available.
188 g_object_class_install_property (gobject_class, PROP_PRESENCE,
189 g_param_spec_enum ("presence", "Presence",
190 "When the pad described by the pad template will become available",
191 GST_TYPE_PAD_PRESENCE, GST_PAD_ALWAYS,
192 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
195 * GstPadTemplate:caps:
197 * The capabilities of the pad described by the pad template.
199 g_object_class_install_property (gobject_class, PROP_CAPS,
200 g_param_spec_boxed ("caps", "Caps",
201 "The capabilities of the pad described by the pad template",
203 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
205 gstobject_class->path_string_separator = "*";
209 gst_pad_template_init (GstPadTemplate * templ)
214 gst_pad_template_dispose (GObject * object)
216 GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
218 g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
219 if (GST_PAD_TEMPLATE_CAPS (templ)) {
220 gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
223 G_OBJECT_CLASS (parent_class)->dispose (object);
226 /* ALWAYS padtemplates cannot have conversion specifications (like src_%d),
227 * since it doesn't make sense.
228 * SOMETIMES padtemplates can do whatever they want, they are provided by the
230 * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
231 * 'sink%d' template is automatically selected), so we need to restrict their
235 name_is_valid (const gchar * name, GstPadPresence presence)
239 if (presence == GST_PAD_ALWAYS) {
240 if (strchr (name, '%')) {
241 g_warning ("invalid name template %s: conversion specifications are not"
242 " allowed for GST_PAD_ALWAYS padtemplates", name);
245 } else if (presence == GST_PAD_REQUEST) {
246 if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
247 g_warning ("invalid name template %s: only one conversion specification"
248 " allowed in GST_PAD_REQUEST padtemplate", name);
251 if (str && (*(str + 1) != 's' && *(str + 1) != 'd' && *(str + 1) != 'u')) {
252 g_warning ("invalid name template %s: conversion specification must be of"
253 " type '%%d', '%%u' or '%%s' for GST_PAD_REQUEST padtemplate", name);
256 if (str && (*(str + 2) != '\0')) {
257 g_warning ("invalid name template %s: conversion specification must"
258 " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
266 G_DEFINE_POINTER_TYPE (GstStaticPadTemplate, gst_static_pad_template);
269 * gst_static_pad_template_get:
270 * @pad_template: the static pad template
272 * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
274 * Returns: (transfer full): a new #GstPadTemplate.
276 /* FIXME0.11: rename to gst_pad_template_new_from_static_pad_template() */
278 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
283 if (!name_is_valid (pad_template->name_template, pad_template->presence))
286 caps = gst_static_caps_get (&pad_template->static_caps);
288 new = g_object_new (gst_pad_template_get_type (),
289 "name", pad_template->name_template,
290 "name-template", pad_template->name_template,
291 "direction", pad_template->direction,
292 "presence", pad_template->presence, "caps", caps, NULL);
294 gst_caps_unref (caps);
300 * gst_pad_template_new:
301 * @name_template: the name template.
302 * @direction: the #GstPadDirection of the template.
303 * @presence: the #GstPadPresence of the pad.
304 * @caps: (transfer none): a #GstCaps set for the template.
306 * Creates a new pad template with a name according to the given template
307 * and with the given arguments.
309 * Returns: (transfer floating): a new #GstPadTemplate.
312 gst_pad_template_new (const gchar * name_template,
313 GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
317 g_return_val_if_fail (name_template != NULL, NULL);
318 g_return_val_if_fail (caps != NULL, NULL);
319 g_return_val_if_fail (direction == GST_PAD_SRC
320 || direction == GST_PAD_SINK, NULL);
321 g_return_val_if_fail (presence == GST_PAD_ALWAYS
322 || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
324 if (!name_is_valid (name_template, presence)) {
328 new = g_object_new (gst_pad_template_get_type (),
329 "name", name_template, "name-template", name_template,
330 "direction", direction, "presence", presence, "caps", caps, NULL);
336 * gst_static_pad_template_get_caps:
337 * @templ: a #GstStaticPadTemplate to get capabilities of.
339 * Gets the capabilities of the static pad template.
341 * Returns: (transfer full): the #GstCaps of the static pad template.
342 * Unref after usage. Since the core holds an additional
343 * ref to the returned caps, use gst_caps_make_writable()
344 * on the returned caps to modify it.
347 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
349 g_return_val_if_fail (templ, NULL);
351 return gst_static_caps_get (&templ->static_caps);
355 * gst_pad_template_get_caps:
356 * @templ: a #GstPadTemplate to get capabilities of.
358 * Gets the capabilities of the pad template.
360 * Returns: (transfer full): the #GstCaps of the pad template.
364 gst_pad_template_get_caps (GstPadTemplate * templ)
367 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
369 caps = GST_PAD_TEMPLATE_CAPS (templ);
371 return (caps ? gst_caps_ref (caps) : NULL);
375 * gst_pad_template_pad_created:
376 * @templ: a #GstPadTemplate that has been created
377 * @pad: the #GstPad that created it
379 * Emit the pad-created signal for this template when created by this pad.
382 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
384 g_signal_emit (templ, gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
388 gst_pad_template_set_property (GObject * object, guint prop_id,
389 const GValue * value, GParamSpec * pspec)
391 /* these properties are all construct-only */
393 case PROP_NAME_TEMPLATE:
394 GST_PAD_TEMPLATE_NAME_TEMPLATE (object) = g_value_dup_string (value);
397 GST_PAD_TEMPLATE_DIRECTION (object) =
398 (GstPadDirection) g_value_get_enum (value);
401 GST_PAD_TEMPLATE_PRESENCE (object) =
402 (GstPadPresence) g_value_get_enum (value);
405 GST_PAD_TEMPLATE_CAPS (object) = g_value_dup_boxed (value);
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
414 gst_pad_template_get_property (GObject * object, guint prop_id, GValue * value,
417 /* these properties are all construct-only */
419 case PROP_NAME_TEMPLATE:
420 g_value_set_string (value, GST_PAD_TEMPLATE_NAME_TEMPLATE (object));
423 g_value_set_enum (value, GST_PAD_TEMPLATE_DIRECTION (object));
426 g_value_set_enum (value, GST_PAD_TEMPLATE_PRESENCE (object));
429 g_value_set_boxed (value, GST_PAD_TEMPLATE_CAPS (object));
432 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);