Merge the tizen patch based on 1.12.2
[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 };
110
111 enum
112 {
113   TEMPL_PAD_CREATED,
114   /* FILL ME */
115   LAST_SIGNAL
116 };
117
118 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
119
120 static void gst_pad_template_dispose (GObject * object);
121 static void gst_pad_template_set_property (GObject * object, guint prop_id,
122     const GValue * value, GParamSpec * pspec);
123 static void gst_pad_template_get_property (GObject * object, guint prop_id,
124     GValue * value, GParamSpec * pspec);
125
126 #define gst_pad_template_parent_class parent_class
127 G_DEFINE_TYPE (GstPadTemplate, gst_pad_template, GST_TYPE_OBJECT);
128
129 static void
130 gst_pad_template_class_init (GstPadTemplateClass * klass)
131 {
132   GObjectClass *gobject_class;
133   GstObjectClass *gstobject_class;
134
135   gobject_class = (GObjectClass *) klass;
136   gstobject_class = (GstObjectClass *) klass;
137
138   /**
139    * GstPadTemplate::pad-created:
140    * @pad_template: the object which received the signal.
141    * @pad: the pad that was created.
142    *
143    * This signal is fired when an element creates a pad from this template.
144    */
145   gst_pad_template_signals[TEMPL_PAD_CREATED] =
146       g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
147       G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
148       NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
149
150   gobject_class->dispose = gst_pad_template_dispose;
151
152   gobject_class->get_property = gst_pad_template_get_property;
153   gobject_class->set_property = gst_pad_template_set_property;
154
155   /**
156    * GstPadTemplate:name-template:
157    *
158    * The name template of the pad template.
159    */
160   g_object_class_install_property (gobject_class, PROP_NAME_TEMPLATE,
161       g_param_spec_string ("name-template", "Name template",
162           "The name template of the pad template", NULL,
163           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
164
165   /**
166    * GstPadTemplate:direction:
167    *
168    * The direction of the pad described by the pad template.
169    */
170   g_object_class_install_property (gobject_class, PROP_DIRECTION,
171       g_param_spec_enum ("direction", "Direction",
172           "The direction of the pad described by the pad template",
173           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
174           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
175
176   /**
177    * GstPadTemplate:presence:
178    *
179    * When the pad described by the pad template will become available.
180    */
181   g_object_class_install_property (gobject_class, PROP_PRESENCE,
182       g_param_spec_enum ("presence", "Presence",
183           "When the pad described by the pad template will become available",
184           GST_TYPE_PAD_PRESENCE, GST_PAD_ALWAYS,
185           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
186
187   /**
188    * GstPadTemplate:caps:
189    *
190    * The capabilities of the pad described by the pad template.
191    */
192   g_object_class_install_property (gobject_class, PROP_CAPS,
193       g_param_spec_boxed ("caps", "Caps",
194           "The capabilities of the pad described by the pad template",
195           GST_TYPE_CAPS,
196           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
197
198   gstobject_class->path_string_separator = "*";
199 }
200
201 static void
202 gst_pad_template_init (GstPadTemplate * templ)
203 {
204   /* GstPadTemplate objects are usually leaked */
205   GST_OBJECT_FLAG_SET (templ, GST_OBJECT_FLAG_MAY_BE_LEAKED);
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 have multiple specifiers in case of %d and %u, like
226  * src_%u_%u, but %s only can be used once in the template.
227  */
228 static gboolean
229 name_is_valid (const gchar * name, GstPadPresence presence)
230 {
231   const gchar *str, *underscore = NULL;
232   gboolean has_s = FALSE;
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     str = strchr (name, '%');
242
243     while (str) {
244       if (*(str + 1) != 's' && *(str + 1) != 'd' && *(str + 1) != 'u') {
245         g_warning
246             ("invalid name template %s: conversion specification must be of"
247             " type '%%d', '%%u' or '%%s' for GST_PAD_REQUEST padtemplate",
248             name);
249         return FALSE;
250       }
251
252       if (*(str + 1) == 's' && (*(str + 2) != '\0' || has_s)) {
253         g_warning
254             ("invalid name template %s: conversion specification of type '%%s'"
255             "only can be used once in the GST_PAD_REQUEST padtemplate at the "
256             "very end and not allowed any other characters with '%%s'", name);
257         return FALSE;
258       }
259
260       if (*(str + 1) == 's') {
261         has_s = TRUE;
262       }
263
264       underscore = strchr (str, '_');
265       str = strchr (str + 1, '%');
266
267       if (str && (!underscore || (underscore && str < underscore))) {
268         g_warning
269             ("invalid name template %s: each of conversion specifications "
270             "must be separated by an underscore", name);
271         return FALSE;
272       }
273     }
274   }
275
276   return TRUE;
277 }
278
279 G_DEFINE_POINTER_TYPE (GstStaticPadTemplate, gst_static_pad_template);
280
281 /**
282  * gst_static_pad_template_get:
283  * @pad_template: the static pad template
284  *
285  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
286  *
287  * Returns: (transfer full): a new #GstPadTemplate.
288  */
289 /* FIXME0.11: rename to gst_pad_template_new_from_static_pad_template() */
290 GstPadTemplate *
291 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
292 {
293   GstPadTemplate *new;
294   GstCaps *caps;
295
296   if (!name_is_valid (pad_template->name_template, pad_template->presence))
297     return NULL;
298
299   caps = gst_static_caps_get (&pad_template->static_caps);
300
301   new = g_object_new (gst_pad_template_get_type (),
302       "name", pad_template->name_template,
303       "name-template", pad_template->name_template,
304       "direction", pad_template->direction,
305       "presence", pad_template->presence, "caps", caps, NULL);
306
307   gst_caps_unref (caps);
308
309   return new;
310 }
311
312 /**
313  * gst_pad_template_new:
314  * @name_template: the name template.
315  * @direction: the #GstPadDirection of the template.
316  * @presence: the #GstPadPresence of the pad.
317  * @caps: (transfer none): a #GstCaps set for the template.
318  *
319  * Creates a new pad template with a name according to the given template
320  * and with the given arguments.
321  *
322  * Returns: (transfer floating): a new #GstPadTemplate.
323  */
324 GstPadTemplate *
325 gst_pad_template_new (const gchar * name_template,
326     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
327 {
328   GstPadTemplate *new;
329
330   g_return_val_if_fail (name_template != NULL, NULL);
331   g_return_val_if_fail (caps != NULL, NULL);
332   g_return_val_if_fail (direction == GST_PAD_SRC
333       || direction == GST_PAD_SINK, NULL);
334   g_return_val_if_fail (presence == GST_PAD_ALWAYS
335       || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
336
337   if (!name_is_valid (name_template, presence)) {
338     return NULL;
339   }
340
341   new = g_object_new (gst_pad_template_get_type (),
342       "name", name_template, "name-template", name_template,
343       "direction", direction, "presence", presence, "caps", caps, NULL);
344
345   return new;
346 }
347
348 /**
349  * gst_static_pad_template_get_caps:
350  * @templ: a #GstStaticPadTemplate to get capabilities of.
351  *
352  * Gets the capabilities of the static pad template.
353  *
354  * Returns: (transfer full): the #GstCaps of the static pad template.
355  * Unref after usage. Since the core holds an additional
356  * ref to the returned caps, use gst_caps_make_writable()
357  * on the returned caps to modify it.
358  */
359 GstCaps *
360 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
361 {
362   g_return_val_if_fail (templ, NULL);
363
364   return gst_static_caps_get (&templ->static_caps);
365 }
366
367 /**
368  * gst_pad_template_get_caps:
369  * @templ: a #GstPadTemplate to get capabilities of.
370  *
371  * Gets the capabilities of the pad template.
372  *
373  * Returns: (transfer full): the #GstCaps of the pad template.
374  * Unref after usage.
375  */
376 GstCaps *
377 gst_pad_template_get_caps (GstPadTemplate * templ)
378 {
379   GstCaps *caps;
380   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
381
382   caps = GST_PAD_TEMPLATE_CAPS (templ);
383
384   return (caps ? gst_caps_ref (caps) : NULL);
385 }
386
387 /**
388  * gst_pad_template_pad_created:
389  * @templ: a #GstPadTemplate that has been created
390  * @pad:   the #GstPad that created it
391  *
392  * Emit the pad-created signal for this template when created by this pad.
393  */
394 void
395 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
396 {
397   g_signal_emit (templ, gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
398 }
399
400 static void
401 gst_pad_template_set_property (GObject * object, guint prop_id,
402     const GValue * value, GParamSpec * pspec)
403 {
404   /* these properties are all construct-only */
405   switch (prop_id) {
406     case PROP_NAME_TEMPLATE:
407       GST_PAD_TEMPLATE_NAME_TEMPLATE (object) = g_value_dup_string (value);
408       break;
409     case PROP_DIRECTION:
410       GST_PAD_TEMPLATE_DIRECTION (object) =
411           (GstPadDirection) g_value_get_enum (value);
412       break;
413     case PROP_PRESENCE:
414       GST_PAD_TEMPLATE_PRESENCE (object) =
415           (GstPadPresence) g_value_get_enum (value);
416       break;
417     case PROP_CAPS:
418       GST_PAD_TEMPLATE_CAPS (object) = g_value_dup_boxed (value);
419       if (GST_PAD_TEMPLATE_CAPS (object) != NULL) {
420         /* GstPadTemplate are usually leaked so are their caps */
421         GST_MINI_OBJECT_FLAG_SET (GST_PAD_TEMPLATE_CAPS (object),
422             GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
423       }
424       break;
425     default:
426       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
427       break;
428   }
429 }
430
431 static void
432 gst_pad_template_get_property (GObject * object, guint prop_id, GValue * value,
433     GParamSpec * pspec)
434 {
435   /* these properties are all construct-only */
436   switch (prop_id) {
437     case PROP_NAME_TEMPLATE:
438       g_value_set_string (value, GST_PAD_TEMPLATE_NAME_TEMPLATE (object));
439       break;
440     case PROP_DIRECTION:
441       g_value_set_enum (value, GST_PAD_TEMPLATE_DIRECTION (object));
442       break;
443     case PROP_PRESENCE:
444       g_value_set_enum (value, GST_PAD_TEMPLATE_PRESENCE (object));
445       break;
446     case PROP_CAPS:
447       g_value_set_boxed (value, GST_PAD_TEMPLATE_CAPS (object));
448       break;
449     default:
450       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451       break;
452   }
453 }