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 * @title: GstPadTemplate
26 * @short_description: Describe the media type of a pad.
27 * @see_also: #GstPad, #GstElementFactory
29 * Padtemplates describe the possible media types a pad or an elementfactory can
30 * handle. This allows for both inspection of handled types before loading the
31 * element plugin as well as identifying pads on elements that are not yet
32 * created (request or sometimes pads).
34 * Pad and PadTemplates have #GstCaps attached to it to describe the media type
35 * they are capable of dealing with. gst_pad_template_get_caps() or
36 * GST_PAD_TEMPLATE_CAPS() are used to get the caps of a padtemplate. It's not
37 * possible to modify the caps of a padtemplate after creation.
39 * PadTemplates have a #GstPadPresence property which identifies the lifetime
40 * of the pad and that can be retrieved with GST_PAD_TEMPLATE_PRESENCE(). Also
41 * the direction of the pad can be retrieved from the #GstPadTemplate with
42 * GST_PAD_TEMPLATE_DIRECTION().
44 * The GST_PAD_TEMPLATE_NAME_TEMPLATE () is important for GST_PAD_REQUEST pads
45 * because it has to be used as the name in the gst_element_get_request_pad()
46 * call to instantiate a pad from this template.
48 * Padtemplates can be created with gst_pad_template_new() or with
49 * gst_static_pad_template_get (), which creates a #GstPadTemplate from a
50 * #GstStaticPadTemplate that can be filled with the
51 * convenient GST_STATIC_PAD_TEMPLATE() macro.
53 * A padtemplate can be used to create a pad (see gst_pad_new_from_template()
54 * or gst_pad_new_from_static_template ()) or to add to an element class
55 * (see gst_element_class_add_static_pad_template ()).
57 * The following code example shows the code to create a pad from a padtemplate.
58 * |[<!-- language="C" -->
59 * GstStaticPadTemplate my_template =
60 * GST_STATIC_PAD_TEMPLATE (
61 * "sink", // the name of the pad
62 * GST_PAD_SINK, // the direction of the pad
63 * GST_PAD_ALWAYS, // when this pad will be present
64 * GST_STATIC_CAPS ( // the capabilities of the padtemplate
66 * "channels = (int) [ 1, 6 ]"
73 * pad = gst_pad_new_from_static_template (&my_template, "sink");
78 * The following example shows you how to add the padtemplate to an
79 * element class, this is usually done in the class_init of the class:
80 * |[<!-- language="C" -->
82 * my_element_class_init (GstMyElementClass *klass)
84 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
86 * gst_element_class_add_static_pad_template (gstelement_class, &my_template);
91 #include "gst_private.h"
94 #include "gstpadtemplate.h"
95 #include "gstenumtypes.h"
101 #define GST_CAT_DEFAULT GST_CAT_PADS
105 PROP_NAME_TEMPLATE = 1,
119 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
121 static void gst_pad_template_dispose (GObject * object);
122 static void gst_pad_template_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_pad_template_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 #define gst_pad_template_parent_class parent_class
128 G_DEFINE_TYPE (GstPadTemplate, gst_pad_template, GST_TYPE_OBJECT);
131 gst_pad_template_class_init (GstPadTemplateClass * klass)
133 GObjectClass *gobject_class;
134 GstObjectClass *gstobject_class;
136 gobject_class = (GObjectClass *) klass;
137 gstobject_class = (GstObjectClass *) klass;
140 * GstPadTemplate::pad-created:
141 * @pad_template: the object which received the signal.
142 * @pad: the pad that was created.
144 * This signal is fired when an element creates a pad from this template.
146 gst_pad_template_signals[TEMPL_PAD_CREATED] =
147 g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
148 G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
149 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
151 gobject_class->dispose = gst_pad_template_dispose;
153 gobject_class->get_property = gst_pad_template_get_property;
154 gobject_class->set_property = gst_pad_template_set_property;
157 * GstPadTemplate:name-template:
159 * The name template of the pad template.
161 g_object_class_install_property (gobject_class, PROP_NAME_TEMPLATE,
162 g_param_spec_string ("name-template", "Name template",
163 "The name template of the pad template", NULL,
164 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
167 * GstPadTemplate:direction:
169 * The direction of the pad described by the pad template.
171 g_object_class_install_property (gobject_class, PROP_DIRECTION,
172 g_param_spec_enum ("direction", "Direction",
173 "The direction of the pad described by the pad template",
174 GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
175 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
178 * GstPadTemplate:presence:
180 * When the pad described by the pad template will become available.
182 g_object_class_install_property (gobject_class, PROP_PRESENCE,
183 g_param_spec_enum ("presence", "Presence",
184 "When the pad described by the pad template will become available",
185 GST_TYPE_PAD_PRESENCE, GST_PAD_ALWAYS,
186 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
189 * GstPadTemplate:caps:
191 * The capabilities of the pad described by the pad template.
193 g_object_class_install_property (gobject_class, PROP_CAPS,
194 g_param_spec_boxed ("caps", "Caps",
195 "The capabilities of the pad described by the pad template",
197 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
200 * GstPadTemplate:gtype:
202 * The type of the pad described by the pad template.
206 g_object_class_install_property (gobject_class, PROP_GTYPE,
207 g_param_spec_gtype ("gtype", "GType",
208 "The GType of the pad described by the pad template",
210 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
212 gstobject_class->path_string_separator = "*";
216 gst_pad_template_init (GstPadTemplate * templ)
218 /* GstPadTemplate objects are usually leaked */
219 GST_OBJECT_FLAG_SET (templ, GST_OBJECT_FLAG_MAY_BE_LEAKED);
220 GST_PAD_TEMPLATE_GTYPE (templ) = G_TYPE_NONE;
224 gst_pad_template_dispose (GObject * object)
226 GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
228 g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
229 if (GST_PAD_TEMPLATE_CAPS (templ)) {
230 gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
233 G_OBJECT_CLASS (parent_class)->dispose (object);
236 /* ALWAYS padtemplates cannot have conversion specifications (like src_%d),
237 * since it doesn't make sense.
238 * SOMETIMES padtemplates can do whatever they want, they are provided by the
240 * REQUEST padtemplates can have multiple specifiers in case of %d and %u, like
241 * src_%u_%u, but %s only can be used once in the template.
244 name_is_valid (const gchar * name, GstPadPresence presence)
246 const gchar *str, *underscore = NULL;
247 gboolean has_s = FALSE;
249 if (presence == GST_PAD_ALWAYS) {
250 if (strchr (name, '%')) {
251 g_warning ("invalid name template %s: conversion specifications are not"
252 " allowed for GST_PAD_ALWAYS padtemplates", name);
255 } else if (presence == GST_PAD_REQUEST) {
256 str = strchr (name, '%');
259 if (*(str + 1) != 's' && *(str + 1) != 'd' && *(str + 1) != 'u') {
261 ("invalid name template %s: conversion specification must be of"
262 " type '%%d', '%%u' or '%%s' for GST_PAD_REQUEST padtemplate",
267 if (*(str + 1) == 's' && (*(str + 2) != '\0' || has_s)) {
269 ("invalid name template %s: conversion specification of type '%%s'"
270 "only can be used once in the GST_PAD_REQUEST padtemplate at the "
271 "very end and not allowed any other characters with '%%s'", name);
275 if (*(str + 1) == 's') {
279 underscore = strchr (str, '_');
280 str = strchr (str + 1, '%');
282 if (str && (!underscore || (underscore && str < underscore))) {
284 ("invalid name template %s: each of conversion specifications "
285 "must be separated by an underscore", name);
294 G_DEFINE_POINTER_TYPE (GstStaticPadTemplate, gst_static_pad_template);
297 * gst_static_pad_template_get:
298 * @pad_template: the static pad template
300 * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
302 * Returns: (transfer floating) (nullable): a new #GstPadTemplate.
304 /* FIXME0.11: rename to gst_pad_template_new_from_static_pad_template() */
306 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
311 if (!name_is_valid (pad_template->name_template, pad_template->presence))
314 caps = gst_static_caps_get (&pad_template->static_caps);
316 new = g_object_new (gst_pad_template_get_type (),
317 "name", pad_template->name_template,
318 "name-template", pad_template->name_template,
319 "direction", pad_template->direction,
320 "presence", pad_template->presence, "caps", caps, NULL);
322 gst_caps_unref (caps);
328 * gst_pad_template_new_from_static_pad_template_with_gtype:
329 * @pad_template: the static pad template
330 * @pad_type: The #GType of the pad to create
332 * Converts a #GstStaticPadTemplate into a #GstPadTemplate with a type.
334 * Returns: (transfer floating): a new #GstPadTemplate.
339 gst_pad_template_new_from_static_pad_template_with_gtype (GstStaticPadTemplate *
340 pad_template, GType pad_type)
345 g_return_val_if_fail (g_type_is_a (pad_type, GST_TYPE_PAD), NULL);
347 if (!name_is_valid (pad_template->name_template, pad_template->presence))
350 caps = gst_static_caps_get (&pad_template->static_caps);
352 new = g_object_new (gst_pad_template_get_type (),
353 "name", pad_template->name_template,
354 "name-template", pad_template->name_template,
355 "direction", pad_template->direction,
356 "presence", pad_template->presence, "caps", caps, "gtype", pad_type,
359 gst_caps_unref (caps);
365 * gst_pad_template_new:
366 * @name_template: the name template.
367 * @direction: the #GstPadDirection of the template.
368 * @presence: the #GstPadPresence of the pad.
369 * @caps: (transfer none): a #GstCaps set for the template.
371 * Creates a new pad template with a name according to the given template
372 * and with the given arguments.
374 * Returns: (transfer floating) (nullable): a new #GstPadTemplate.
377 gst_pad_template_new (const gchar * name_template,
378 GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
382 g_return_val_if_fail (name_template != NULL, NULL);
383 g_return_val_if_fail (caps != NULL, NULL);
384 g_return_val_if_fail (direction == GST_PAD_SRC
385 || direction == GST_PAD_SINK, NULL);
386 g_return_val_if_fail (presence == GST_PAD_ALWAYS
387 || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
389 if (!name_is_valid (name_template, presence)) {
393 new = g_object_new (gst_pad_template_get_type (),
394 "name", name_template, "name-template", name_template,
395 "direction", direction, "presence", presence, "caps", caps, NULL);
401 * gst_pad_template_new_with_gtype:
402 * @name_template: the name template.
403 * @direction: the #GstPadDirection of the template.
404 * @presence: the #GstPadPresence of the pad.
405 * @caps: (transfer none): a #GstCaps set for the template.
406 * @pad_type: The #GType of the pad to create
408 * Creates a new pad template with a name according to the given template
409 * and with the given arguments.
411 * Returns: (transfer floating): a new #GstPadTemplate.
416 gst_pad_template_new_with_gtype (const gchar * name_template,
417 GstPadDirection direction, GstPadPresence presence, GstCaps * caps,
422 g_return_val_if_fail (name_template != NULL, NULL);
423 g_return_val_if_fail (caps != NULL, NULL);
424 g_return_val_if_fail (direction == GST_PAD_SRC
425 || direction == GST_PAD_SINK, NULL);
426 g_return_val_if_fail (presence == GST_PAD_ALWAYS
427 || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
428 g_return_val_if_fail (g_type_is_a (pad_type, GST_TYPE_PAD), NULL);
430 if (!name_is_valid (name_template, presence)) {
434 new = g_object_new (gst_pad_template_get_type (),
435 "name", name_template, "name-template", name_template,
436 "direction", direction, "presence", presence, "caps", caps,
437 "gtype", pad_type, NULL);
443 * gst_static_pad_template_get_caps:
444 * @templ: a #GstStaticPadTemplate to get capabilities of.
446 * Gets the capabilities of the static pad template.
448 * Returns: (transfer full): the #GstCaps of the static pad template.
449 * Unref after usage. Since the core holds an additional
450 * ref to the returned caps, use gst_caps_make_writable()
451 * on the returned caps to modify it.
454 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
456 g_return_val_if_fail (templ, NULL);
458 return gst_static_caps_get (&templ->static_caps);
462 * gst_pad_template_get_caps:
463 * @templ: a #GstPadTemplate to get capabilities of.
465 * Gets the capabilities of the pad template.
467 * Returns: (transfer full): the #GstCaps of the pad template.
471 gst_pad_template_get_caps (GstPadTemplate * templ)
474 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
476 caps = GST_PAD_TEMPLATE_CAPS (templ);
478 return (caps ? gst_caps_ref (caps) : NULL);
482 * gst_pad_template_pad_created:
483 * @templ: a #GstPadTemplate that has been created
484 * @pad: the #GstPad that created it
486 * Emit the pad-created signal for this template when created by this pad.
489 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
491 g_signal_emit (templ, gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
495 gst_pad_template_set_property (GObject * object, guint prop_id,
496 const GValue * value, GParamSpec * pspec)
498 /* these properties are all construct-only */
500 case PROP_NAME_TEMPLATE:
501 GST_PAD_TEMPLATE_NAME_TEMPLATE (object) = g_value_dup_string (value);
504 GST_PAD_TEMPLATE_DIRECTION (object) =
505 (GstPadDirection) g_value_get_enum (value);
508 GST_PAD_TEMPLATE_PRESENCE (object) =
509 (GstPadPresence) g_value_get_enum (value);
512 GST_PAD_TEMPLATE_CAPS (object) = g_value_dup_boxed (value);
513 if (GST_PAD_TEMPLATE_CAPS (object) != NULL) {
514 /* GstPadTemplate are usually leaked so are their caps */
515 GST_MINI_OBJECT_FLAG_SET (GST_PAD_TEMPLATE_CAPS (object),
516 GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
520 GST_PAD_TEMPLATE_GTYPE (object) = g_value_get_gtype (value);
523 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529 gst_pad_template_get_property (GObject * object, guint prop_id, GValue * value,
532 /* these properties are all construct-only */
534 case PROP_NAME_TEMPLATE:
535 g_value_set_string (value, GST_PAD_TEMPLATE_NAME_TEMPLATE (object));
538 g_value_set_enum (value, GST_PAD_TEMPLATE_DIRECTION (object));
541 g_value_set_enum (value, GST_PAD_TEMPLATE_PRESENCE (object));
544 g_value_set_boxed (value, GST_PAD_TEMPLATE_CAPS (object));
547 g_value_set_gtype (value, GST_PAD_TEMPLATE_GTYPE (object));
550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);