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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 * SECTION:gstpadtemplate
24 * @short_description: Describe the media type of a pad.
25 * @see_also: #GstPad, #GstElementFactory
27 * Padtemplates describe the possible media types a pad or an elementfactory can
30 * Pad and PadTemplates have #GstCaps attached to it to describe the media type they
31 * are capable of dealing with. gst_pad_template_get_caps() is used to get the
32 * caps of a padtemplate. It's not possible to modify the caps of a padtemplate after
35 * Padtemplates can be created with gst_pad_template_new() or with the convenient
36 * GST_PAD_TEMPLATE_FACTORY() macro. A padtemplate can be used to create a pad or
37 * to add to an elementfactory.
39 * The following code example shows the code to create a pad from a padtemplate.
41 * <title>Create a pad from a padtemplate</title>
43 * GstStaticPadTemplate my_template =
44 * GST_STATIC_PAD_TEMPLATE (
45 * "sink", // the name of the pad
46 * GST_PAD_SINK, // the direction of the pad
47 * GST_PAD_ALWAYS, // when this pad will be present
48 * GST_STATIC_CAPS ( // the capabilities of the padtemplate
50 * "channels = (int) [ 1, 6 ]"
59 * pad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (my_template_factory), "sink");
65 * The following example shows you how to add the padtemplate to an elementfactory:
69 * my_factory_init (GstPlugin *plugin)
71 * GstElementFactory *factory;
73 * factory = gst_element_factory_new ("my_factory", GST_TYPE_MYFACTORY, &gst_myfactory_details);
74 * g_return_val_if_fail (factory != NULL, FALSE);
76 * gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (my_template_factory));
78 * gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
86 #include "gst_private.h"
89 #include "gstpadtemplate.h"
90 #include "gstenumtypes.h"
91 #include "gstmarshal.h"
97 #define GST_CAT_DEFAULT GST_CAT_PADS
106 static GstObject *parent_class = NULL;
107 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
109 static void gst_pad_template_class_init (GstPadTemplateClass * klass);
110 static void gst_pad_template_init (GstPadTemplate * templ);
111 static void gst_pad_template_dispose (GObject * object);
114 gst_pad_template_get_type (void)
116 static GType padtemplate_type = 0;
118 if (!padtemplate_type) {
119 static const GTypeInfo padtemplate_info = {
120 sizeof (GstPadTemplateClass), NULL, NULL,
121 (GClassInitFunc) gst_pad_template_class_init, NULL, NULL,
122 sizeof (GstPadTemplate),
124 (GInstanceInitFunc) gst_pad_template_init, NULL
128 g_type_register_static (GST_TYPE_OBJECT, "GstPadTemplate",
129 &padtemplate_info, 0);
131 return padtemplate_type;
135 gst_pad_template_class_init (GstPadTemplateClass * klass)
137 GObjectClass *gobject_class;
138 GstObjectClass *gstobject_class;
140 gobject_class = (GObjectClass *) klass;
141 gstobject_class = (GstObjectClass *) klass;
143 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
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, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
157 gobject_class->dispose = gst_pad_template_dispose;
159 gstobject_class->path_string_separator = "*";
163 gst_pad_template_init (GstPadTemplate * templ)
168 gst_pad_template_dispose (GObject * object)
170 GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
172 g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
173 if (GST_PAD_TEMPLATE_CAPS (templ)) {
174 gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
177 G_OBJECT_CLASS (parent_class)->dispose (object);
180 /* ALWAYS padtemplates cannot have conversion specifications (like src_%d),
181 * since it doesn't make sense.
182 * SOMETIMES padtemplates can do whatever they want, they are provided by the
184 * REQUEST padtemplates can be reverse-parsed (the user asks for 'sink1', the
185 * 'sink%d' template is automatically selected), so we need to restrict their
189 name_is_valid (const gchar * name, GstPadPresence presence)
193 if (presence == GST_PAD_ALWAYS) {
194 if (strchr (name, '%')) {
195 g_warning ("invalid name template %s: conversion specifications are not"
196 " allowed for GST_PAD_ALWAYS padtemplates", name);
199 } else if (presence == GST_PAD_REQUEST) {
200 if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
201 g_warning ("invalid name template %s: only one conversion specification"
202 " allowed in GST_PAD_REQUEST padtemplate", name);
205 if (str && (*(str + 1) != 's' && *(str + 1) != 'd')) {
206 g_warning ("invalid name template %s: conversion specification must be of"
207 " type '%%d' or '%%s' for GST_PAD_REQUEST padtemplate", name);
210 if (str && (*(str + 2) != '\0')) {
211 g_warning ("invalid name template %s: conversion specification must"
212 " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
221 * gst_static_pad_template_get:
222 * @pad_template: the static pad template
224 * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
226 * Returns: a new #GstPadTemplate.
229 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
233 if (!name_is_valid (pad_template->name_template, pad_template->presence))
236 new = g_object_new (gst_pad_template_get_type (),
237 "name", pad_template->name_template, NULL);
239 GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (pad_template->name_template);
240 GST_PAD_TEMPLATE_DIRECTION (new) = pad_template->direction;
241 GST_PAD_TEMPLATE_PRESENCE (new) = pad_template->presence;
243 GST_PAD_TEMPLATE_CAPS (new) =
244 gst_caps_copy (gst_static_caps_get (&pad_template->static_caps));
250 * gst_pad_template_new:
251 * @name_template: the name template.
252 * @direction: the #GstPadDirection of the template.
253 * @presence: the #GstPadPresence of the pad.
254 * @caps: a #GstCaps set for the template. The caps are taken ownership of.
256 * Creates a new pad template with a name according to the given template
257 * and with the given arguments. This functions takes ownership of the provided
258 * caps, so be sure to not use them afterwards.
260 * Returns: a new #GstPadTemplate.
263 gst_pad_template_new (const gchar * name_template,
264 GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
268 g_return_val_if_fail (name_template != NULL, NULL);
269 g_return_val_if_fail (caps != NULL, NULL);
270 g_return_val_if_fail (direction == GST_PAD_SRC
271 || direction == GST_PAD_SINK, NULL);
272 g_return_val_if_fail (presence == GST_PAD_ALWAYS
273 || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
275 if (!name_is_valid (name_template, presence)) {
276 gst_caps_unref (caps);
280 new = g_object_new (gst_pad_template_get_type (),
281 "name", name_template, NULL);
283 GST_PAD_TEMPLATE_NAME_TEMPLATE (new) = g_strdup (name_template);
284 GST_PAD_TEMPLATE_DIRECTION (new) = direction;
285 GST_PAD_TEMPLATE_PRESENCE (new) = presence;
286 GST_PAD_TEMPLATE_CAPS (new) = caps;
292 * gst_static_pad_template_get_caps:
293 * @templ: a #GstStaticPadTemplate to get capabilities of.
295 * Gets the capabilities of the static pad template.
297 * Returns: the #GstCaps of the static pad template. If you need to keep a
298 * reference to the caps, take a ref (see gst_caps_ref ()).
301 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
303 g_return_val_if_fail (templ, NULL);
305 return (GstCaps *) gst_static_caps_get (&templ->static_caps);
309 * gst_pad_template_get_caps:
310 * @templ: a #GstPadTemplate to get capabilities of.
312 * Gets the capabilities of the pad template.
314 * Returns: the #GstCaps of the pad template. If you need to keep a reference to
315 * the caps, take a ref (see gst_caps_ref ()).
318 gst_pad_template_get_caps (GstPadTemplate * templ)
320 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
322 return GST_PAD_TEMPLATE_CAPS (templ);
326 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
328 gst_object_sink (GST_OBJECT (templ));
329 g_signal_emit (G_OBJECT (templ),
330 gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);