pad templates: Allow specifying GType
[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  * @title: GstPadTemplate
26  * @short_description: Describe the media type of a pad.
27  * @see_also: #GstPad, #GstElementFactory
28  *
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).
33  *
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.
38  *
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().
43  *
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.
47  *
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.
52  *
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 ()).
56  *
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
65  *       "audio/x-raw, "
66  *         "channels = (int) [ 1, 6 ]"
67  *     )
68  *   );
69  *   void
70  *   my_method (void)
71  *   {
72  *     GstPad *pad;
73  *     pad = gst_pad_new_from_static_template (&amp;my_template, "sink");
74  *     ...
75  *   }
76  * ]|
77  *
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" -->
81  *   static void
82  *   my_element_class_init (GstMyElementClass *klass)
83  *   {
84  *     GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
85  *
86  *     gst_element_class_add_static_pad_template (gstelement_class, &amp;my_template);
87  *   }
88  * ]|
89  */
90
91 #include "gst_private.h"
92
93 #include "gstpad.h"
94 #include "gstpadtemplate.h"
95 #include "gstenumtypes.h"
96 #include "gstutils.h"
97 #include "gstinfo.h"
98 #include "gsterror.h"
99 #include "gstvalue.h"
100
101 #define GST_CAT_DEFAULT GST_CAT_PADS
102
103 enum
104 {
105   PROP_NAME_TEMPLATE = 1,
106   PROP_DIRECTION,
107   PROP_PRESENCE,
108   PROP_CAPS,
109   PROP_GTYPE,
110 };
111
112 enum
113 {
114   TEMPL_PAD_CREATED,
115   /* FILL ME */
116   LAST_SIGNAL
117 };
118
119 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
120
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);
126
127 #define gst_pad_template_parent_class parent_class
128 G_DEFINE_TYPE (GstPadTemplate, gst_pad_template, GST_TYPE_OBJECT);
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   /**
140    * GstPadTemplate::pad-created:
141    * @pad_template: the object which received the signal.
142    * @pad: the pad that was created.
143    *
144    * This signal is fired when an element creates a pad from this template.
145    */
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);
150
151   gobject_class->dispose = gst_pad_template_dispose;
152
153   gobject_class->get_property = gst_pad_template_get_property;
154   gobject_class->set_property = gst_pad_template_set_property;
155
156   /**
157    * GstPadTemplate:name-template:
158    *
159    * The name template of the pad template.
160    */
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));
165
166   /**
167    * GstPadTemplate:direction:
168    *
169    * The direction of the pad described by the pad template.
170    */
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));
176
177   /**
178    * GstPadTemplate:presence:
179    *
180    * When the pad described by the pad template will become available.
181    */
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));
187
188   /**
189    * GstPadTemplate:caps:
190    *
191    * The capabilities of the pad described by the pad template.
192    */
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",
196           GST_TYPE_CAPS,
197           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
198
199   /**
200    * GstPadTemplate:gtype:
201    *
202    * The type of the pad described by the pad template.
203    *
204    * Since: 1.14
205    */
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",
209           G_TYPE_NONE,
210           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
211
212   gstobject_class->path_string_separator = "*";
213 }
214
215 static void
216 gst_pad_template_init (GstPadTemplate * templ)
217 {
218   /* GstPadTemplate objects are usually leaked */
219   GST_OBJECT_FLAG_SET (templ, GST_OBJECT_FLAG_MAY_BE_LEAKED);
220 }
221
222 static void
223 gst_pad_template_dispose (GObject * object)
224 {
225   GstPadTemplate *templ = GST_PAD_TEMPLATE (object);
226
227   g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
228   if (GST_PAD_TEMPLATE_CAPS (templ)) {
229     gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
230   }
231
232   G_OBJECT_CLASS (parent_class)->dispose (object);
233 }
234
235 /* ALWAYS padtemplates cannot have conversion specifications (like src_%d),
236  * since it doesn't make sense.
237  * SOMETIMES padtemplates can do whatever they want, they are provided by the
238  * element.
239  * REQUEST padtemplates can have multiple specifiers in case of %d and %u, like
240  * src_%u_%u, but %s only can be used once in the template.
241  */
242 static gboolean
243 name_is_valid (const gchar * name, GstPadPresence presence)
244 {
245   const gchar *str, *underscore = NULL;
246   gboolean has_s = FALSE;
247
248   if (presence == GST_PAD_ALWAYS) {
249     if (strchr (name, '%')) {
250       g_warning ("invalid name template %s: conversion specifications are not"
251           " allowed for GST_PAD_ALWAYS padtemplates", name);
252       return FALSE;
253     }
254   } else if (presence == GST_PAD_REQUEST) {
255     str = strchr (name, '%');
256
257     while (str) {
258       if (*(str + 1) != 's' && *(str + 1) != 'd' && *(str + 1) != 'u') {
259         g_warning
260             ("invalid name template %s: conversion specification must be of"
261             " type '%%d', '%%u' or '%%s' for GST_PAD_REQUEST padtemplate",
262             name);
263         return FALSE;
264       }
265
266       if (*(str + 1) == 's' && (*(str + 2) != '\0' || has_s)) {
267         g_warning
268             ("invalid name template %s: conversion specification of type '%%s'"
269             "only can be used once in the GST_PAD_REQUEST padtemplate at the "
270             "very end and not allowed any other characters with '%%s'", name);
271         return FALSE;
272       }
273
274       if (*(str + 1) == 's') {
275         has_s = TRUE;
276       }
277
278       underscore = strchr (str, '_');
279       str = strchr (str + 1, '%');
280
281       if (str && (!underscore || (underscore && str < underscore))) {
282         g_warning
283             ("invalid name template %s: each of conversion specifications "
284             "must be separated by an underscore", name);
285         return FALSE;
286       }
287     }
288   }
289
290   return TRUE;
291 }
292
293 G_DEFINE_POINTER_TYPE (GstStaticPadTemplate, gst_static_pad_template);
294
295 /**
296  * gst_static_pad_template_get:
297  * @pad_template: the static pad template
298  *
299  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
300  *
301  * Returns: (transfer floating): a new #GstPadTemplate.
302  */
303 /* FIXME0.11: rename to gst_pad_template_new_from_static_pad_template() */
304 GstPadTemplate *
305 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
306 {
307   GstPadTemplate *new;
308   GstCaps *caps;
309
310   if (!name_is_valid (pad_template->name_template, pad_template->presence))
311     return NULL;
312
313   caps = gst_static_caps_get (&pad_template->static_caps);
314
315   new = g_object_new (gst_pad_template_get_type (),
316       "name", pad_template->name_template,
317       "name-template", pad_template->name_template,
318       "direction", pad_template->direction,
319       "presence", pad_template->presence, "caps", caps, NULL);
320
321   gst_caps_unref (caps);
322
323   return new;
324 }
325
326 /**
327  * gst_pad_template_new_from_static_pad_template_with_gtype:
328  * @pad_template: the static pad template
329  * @pad_type: The #GType of the pad to create
330  *
331  * Converts a #GstStaticPadTemplate into a #GstPadTemplate with a type.
332  *
333  * Returns: (transfer floating): a new #GstPadTemplate.
334  */
335 GstPadTemplate *
336 gst_pad_template_new_from_static_pad_template_with_gtype (GstStaticPadTemplate *
337     pad_template, GType pad_type)
338 {
339   GstPadTemplate *new;
340   GstCaps *caps;
341
342   if (!name_is_valid (pad_template->name_template, pad_template->presence))
343     return NULL;
344
345   caps = gst_static_caps_get (&pad_template->static_caps);
346
347   new = g_object_new (gst_pad_template_get_type (),
348       "name", pad_template->name_template,
349       "name-template", pad_template->name_template,
350       "direction", pad_template->direction,
351       "presence", pad_template->presence, "caps", caps, "gtype", pad_type,
352       NULL);
353
354   gst_caps_unref (caps);
355
356   return new;
357 }
358
359 /**
360  * gst_pad_template_new:
361  * @name_template: the name template.
362  * @direction: the #GstPadDirection of the template.
363  * @presence: the #GstPadPresence of the pad.
364  * @caps: (transfer none): a #GstCaps set for the template.
365  *
366  * Creates a new pad template with a name according to the given template
367  * and with the given arguments.
368  *
369  * Returns: (transfer floating): a new #GstPadTemplate.
370  */
371 GstPadTemplate *
372 gst_pad_template_new (const gchar * name_template,
373     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
374 {
375   GstPadTemplate *new;
376
377   g_return_val_if_fail (name_template != NULL, NULL);
378   g_return_val_if_fail (caps != NULL, NULL);
379   g_return_val_if_fail (direction == GST_PAD_SRC
380       || direction == GST_PAD_SINK, NULL);
381   g_return_val_if_fail (presence == GST_PAD_ALWAYS
382       || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
383
384   if (!name_is_valid (name_template, presence)) {
385     return NULL;
386   }
387
388   new = g_object_new (gst_pad_template_get_type (),
389       "name", name_template, "name-template", name_template,
390       "direction", direction, "presence", presence, "caps", caps, NULL);
391
392   return new;
393 }
394
395 /**
396  * gst_static_pad_template_get_caps:
397  * @templ: a #GstStaticPadTemplate to get capabilities of.
398  *
399  * Gets the capabilities of the static pad template.
400  *
401  * Returns: (transfer full): the #GstCaps of the static pad template.
402  * Unref after usage. Since the core holds an additional
403  * ref to the returned caps, use gst_caps_make_writable()
404  * on the returned caps to modify it.
405  */
406 GstCaps *
407 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
408 {
409   g_return_val_if_fail (templ, NULL);
410
411   return gst_static_caps_get (&templ->static_caps);
412 }
413
414 /**
415  * gst_pad_template_get_caps:
416  * @templ: a #GstPadTemplate to get capabilities of.
417  *
418  * Gets the capabilities of the pad template.
419  *
420  * Returns: (transfer full): the #GstCaps of the pad template.
421  * Unref after usage.
422  */
423 GstCaps *
424 gst_pad_template_get_caps (GstPadTemplate * templ)
425 {
426   GstCaps *caps;
427   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
428
429   caps = GST_PAD_TEMPLATE_CAPS (templ);
430
431   return (caps ? gst_caps_ref (caps) : NULL);
432 }
433
434 /**
435  * gst_pad_template_pad_created:
436  * @templ: a #GstPadTemplate that has been created
437  * @pad:   the #GstPad that created it
438  *
439  * Emit the pad-created signal for this template when created by this pad.
440  */
441 void
442 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
443 {
444   g_signal_emit (templ, gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
445 }
446
447 static void
448 gst_pad_template_set_property (GObject * object, guint prop_id,
449     const GValue * value, GParamSpec * pspec)
450 {
451   /* these properties are all construct-only */
452   switch (prop_id) {
453     case PROP_NAME_TEMPLATE:
454       GST_PAD_TEMPLATE_NAME_TEMPLATE (object) = g_value_dup_string (value);
455       break;
456     case PROP_DIRECTION:
457       GST_PAD_TEMPLATE_DIRECTION (object) =
458           (GstPadDirection) g_value_get_enum (value);
459       break;
460     case PROP_PRESENCE:
461       GST_PAD_TEMPLATE_PRESENCE (object) =
462           (GstPadPresence) g_value_get_enum (value);
463       break;
464     case PROP_CAPS:
465       GST_PAD_TEMPLATE_CAPS (object) = g_value_dup_boxed (value);
466       if (GST_PAD_TEMPLATE_CAPS (object) != NULL) {
467         /* GstPadTemplate are usually leaked so are their caps */
468         GST_MINI_OBJECT_FLAG_SET (GST_PAD_TEMPLATE_CAPS (object),
469             GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
470       }
471       break;
472     case PROP_GTYPE:
473       GST_PAD_TEMPLATE_GTYPE (object) = g_value_get_gtype (value);
474       break;
475     default:
476       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
477       break;
478   }
479 }
480
481 static void
482 gst_pad_template_get_property (GObject * object, guint prop_id, GValue * value,
483     GParamSpec * pspec)
484 {
485   /* these properties are all construct-only */
486   switch (prop_id) {
487     case PROP_NAME_TEMPLATE:
488       g_value_set_string (value, GST_PAD_TEMPLATE_NAME_TEMPLATE (object));
489       break;
490     case PROP_DIRECTION:
491       g_value_set_enum (value, GST_PAD_TEMPLATE_DIRECTION (object));
492       break;
493     case PROP_PRESENCE:
494       g_value_set_enum (value, GST_PAD_TEMPLATE_PRESENCE (object));
495       break;
496     case PROP_CAPS:
497       g_value_set_boxed (value, GST_PAD_TEMPLATE_CAPS (object));
498       break;
499     case PROP_GTYPE:
500       g_value_set_gtype (value, GST_PAD_TEMPLATE_GTYPE (object));
501       break;
502     default:
503       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504       break;
505   }
506 }