Merge branch 'master' into 0.11
[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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gstpadtemplate
25  * @short_description: Describe the media type of a pad.
26  * @see_also: #GstPad, #GstElementFactory
27  *
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).
32  *
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.
37  *
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().
42  *
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.
46  *
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.
51  *
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 ()).
55  *
56  * The following code example shows the code to create a pad from a padtemplate.
57  * <example>
58  * <title>Create a pad from a padtemplate</title>
59  *   <programlisting>
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
66  *       "audio/x-raw-int, "
67  *         "channels = (int) [ 1, 6 ]"
68  *     )
69  *   );
70  *   void
71  *   my_method (void)
72  *   {
73  *     GstPad *pad;
74  *     pad = gst_pad_new_from_static_template (&amp;my_template, "sink");
75  *     ...
76  *   }
77  *   </programlisting>
78  * </example>
79  *
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:
82  * <informalexample>
83  *   <programlisting>
84  *   static void
85  *   my_element_class_init (GstMyElementClass *klass)
86  *   {
87  *     GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
88  *
89  *     gst_element_class_add_pad_template (gstelement_class,
90  *         gst_static_pad_template_get (&amp;my_template));
91  *   }
92  *   </programlisting>
93  * </informalexample>
94  *
95  * Last reviewed on 2006-02-14 (0.10.3)
96  */
97
98 #include "gst_private.h"
99
100 #include "gstpad.h"
101 #include "gstpadtemplate.h"
102 #include "gstenumtypes.h"
103 #include "gstmarshal.h"
104 #include "gstutils.h"
105 #include "gstinfo.h"
106 #include "gsterror.h"
107 #include "gstvalue.h"
108
109 #define GST_CAT_DEFAULT GST_CAT_PADS
110
111 enum
112 {
113   PROP_NAME_TEMPLATE = 1,
114   PROP_DIRECTION,
115   PROP_PRESENCE,
116   PROP_CAPS
117 };
118
119 enum
120 {
121   TEMPL_PAD_CREATED,
122   /* FILL ME */
123   LAST_SIGNAL
124 };
125
126 static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };
127
128 static void gst_pad_template_dispose (GObject * object);
129 static void gst_pad_template_set_property (GObject * object, guint prop_id,
130     const GValue * value, GParamSpec * pspec);
131 static void gst_pad_template_get_property (GObject * object, guint prop_id,
132     GValue * value, GParamSpec * pspec);
133
134 #define gst_pad_template_parent_class parent_class
135 G_DEFINE_TYPE (GstPadTemplate, gst_pad_template, GST_TYPE_OBJECT);
136
137 static void
138 gst_pad_template_class_init (GstPadTemplateClass * klass)
139 {
140   GObjectClass *gobject_class;
141   GstObjectClass *gstobject_class;
142
143   gobject_class = (GObjectClass *) klass;
144   gstobject_class = (GstObjectClass *) klass;
145
146   /**
147    * GstPadTemplate::pad-created:
148    * @pad_template: the object which received the signal.
149    * @pad: the pad that was created.
150    *
151    * This signal is fired when an element creates a pad from this template.
152    */
153   gst_pad_template_signals[TEMPL_PAD_CREATED] =
154       g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
155       G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
156       NULL, NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
157
158   gobject_class->dispose = gst_pad_template_dispose;
159
160   gobject_class->get_property = gst_pad_template_get_property;
161   gobject_class->set_property = gst_pad_template_set_property;
162
163   /**
164    * GstPadTemplate:name-template
165    *
166    * The name template of the pad template.
167    *
168    * Since: 0.10.21
169    */
170   g_object_class_install_property (gobject_class, PROP_NAME_TEMPLATE,
171       g_param_spec_string ("name-template", "Name template",
172           "The name template of the pad template", NULL,
173           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
174
175   /**
176    * GstPadTemplate:direction
177    *
178    * The direction of the pad described by the pad template.
179    *
180    * Since: 0.10.21
181    */
182   g_object_class_install_property (gobject_class, PROP_DIRECTION,
183       g_param_spec_enum ("direction", "Direction",
184           "The direction of the pad described by the pad template",
185           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
186           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
187
188   /**
189    * GstPadTemplate:presence
190    *
191    * When the pad described by the pad template will become available.
192    *
193    * Since: 0.10.21
194    */
195   g_object_class_install_property (gobject_class, PROP_PRESENCE,
196       g_param_spec_enum ("presence", "Presence",
197           "When the pad described by the pad template will become available",
198           GST_TYPE_PAD_PRESENCE, GST_PAD_ALWAYS,
199           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
200
201   /**
202    * GstPadTemplate:caps
203    *
204    * The capabilities of the pad described by the pad template.
205    *
206    * Since: 0.10.21
207    */
208   g_object_class_install_property (gobject_class, PROP_CAPS,
209       g_param_spec_boxed ("caps", "Caps",
210           "The capabilities of the pad described by the pad template",
211           GST_TYPE_CAPS,
212           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
213
214   gstobject_class->path_string_separator = "*";
215 }
216
217 static void
218 gst_pad_template_init (GstPadTemplate * templ)
219 {
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 be reverse-parsed (the user asks for 'sink1', the
240  * 'sink%d' template is automatically selected), so we need to restrict their
241  * naming.
242  */
243 static gboolean
244 name_is_valid (const gchar * name, GstPadPresence presence)
245 {
246   const gchar *str;
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     if ((str = strchr (name, '%')) && strchr (str + 1, '%')) {
256       g_warning ("invalid name template %s: only one conversion specification"
257           " allowed in GST_PAD_REQUEST padtemplate", name);
258       return FALSE;
259     }
260     if (str && (*(str + 1) != 's' && *(str + 1) != 'd' && *(str + 1) != 'u')) {
261       g_warning ("invalid name template %s: conversion specification must be of"
262           " type '%%d', '%%u' or '%%s' for GST_PAD_REQUEST padtemplate", name);
263       return FALSE;
264     }
265     if (str && (*(str + 2) != '\0')) {
266       g_warning ("invalid name template %s: conversion specification must"
267           " appear at the end of the GST_PAD_REQUEST padtemplate name", name);
268       return FALSE;
269     }
270   }
271
272   return TRUE;
273 }
274
275 G_DEFINE_POINTER_TYPE (GstStaticPadTemplate, gst_static_pad_template);
276
277 /**
278  * gst_static_pad_template_get:
279  * @pad_template: the static pad template
280  *
281  * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
282  *
283  * Returns: (transfer full): a new #GstPadTemplate.
284  */
285 /* FIXME0.11: rename to gst_pad_template_new_from_static_pad_template() */
286 GstPadTemplate *
287 gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
288 {
289   GstPadTemplate *new;
290   GstCaps *caps;
291
292   if (!name_is_valid (pad_template->name_template, pad_template->presence))
293     return NULL;
294
295   caps = gst_static_caps_get (&pad_template->static_caps);
296
297   new = g_object_new (gst_pad_template_get_type (),
298       "name", pad_template->name_template,
299       "name-template", pad_template->name_template,
300       "direction", pad_template->direction,
301       "presence", pad_template->presence, "caps", caps, NULL);
302
303   gst_caps_unref (caps);
304
305   return new;
306 }
307
308 /**
309  * gst_pad_template_new:
310  * @name_template: the name template.
311  * @direction: the #GstPadDirection of the template.
312  * @presence: the #GstPadPresence of the pad.
313  * @caps: (transfer none): a #GstCaps set for the template.
314  *
315  * Creates a new pad template with a name according to the given template
316  * and with the given arguments.
317  *
318  * Returns: (transfer floating): a new #GstPadTemplate.
319  */
320 GstPadTemplate *
321 gst_pad_template_new (const gchar * name_template,
322     GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
323 {
324   GstPadTemplate *new;
325
326   g_return_val_if_fail (name_template != NULL, NULL);
327   g_return_val_if_fail (caps != NULL, NULL);
328   g_return_val_if_fail (direction == GST_PAD_SRC
329       || direction == GST_PAD_SINK, NULL);
330   g_return_val_if_fail (presence == GST_PAD_ALWAYS
331       || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);
332
333   if (!name_is_valid (name_template, presence)) {
334     return NULL;
335   }
336
337   new = g_object_new (gst_pad_template_get_type (),
338       "name", name_template, "name-template", name_template,
339       "direction", direction, "presence", presence, "caps", caps, NULL);
340
341   return new;
342 }
343
344 /**
345  * gst_static_pad_template_get_caps:
346  * @templ: a #GstStaticPadTemplate to get capabilities of.
347  *
348  * Gets the capabilities of the static pad template.
349  *
350  * Returns: (transfer full): the #GstCaps of the static pad template.
351  * Unref after usage. Since the core holds an additional
352  * ref to the returned caps, use gst_caps_make_writable()
353  * on the returned caps to modify it.
354  */
355 GstCaps *
356 gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
357 {
358   g_return_val_if_fail (templ, NULL);
359
360   return (GstCaps *) gst_static_caps_get (&templ->static_caps);
361 }
362
363 /**
364  * gst_pad_template_get_caps:
365  * @templ: a #GstPadTemplate to get capabilities of.
366  *
367  * Gets the capabilities of the pad template.
368  *
369  * Returns: (transfer full): the #GstCaps of the pad template.
370  * Unref after usage.
371  */
372 GstCaps *
373 gst_pad_template_get_caps (GstPadTemplate * templ)
374 {
375   GstCaps *caps;
376   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
377
378   caps = GST_PAD_TEMPLATE_CAPS (templ);
379
380   return (caps ? gst_caps_ref (caps) : NULL);
381 }
382
383 /**
384  * gst_pad_template_pad_created:
385  * @templ: a #GstPadTemplate that has been created
386  * @pad:   the #GstPad that created it
387  *
388  * Emit the pad-created signal for this template when created by this pad.
389  */
390 void
391 gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
392 {
393   g_signal_emit (templ, gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
394 }
395
396 static void
397 gst_pad_template_set_property (GObject * object, guint prop_id,
398     const GValue * value, GParamSpec * pspec)
399 {
400   /* these properties are all construct-only */
401   switch (prop_id) {
402     case PROP_NAME_TEMPLATE:
403       GST_PAD_TEMPLATE_NAME_TEMPLATE (object) = g_value_dup_string (value);
404       break;
405     case PROP_DIRECTION:
406       GST_PAD_TEMPLATE_DIRECTION (object) =
407           (GstPadDirection) g_value_get_enum (value);
408       break;
409     case PROP_PRESENCE:
410       GST_PAD_TEMPLATE_PRESENCE (object) =
411           (GstPadPresence) g_value_get_enum (value);
412       break;
413     case PROP_CAPS:
414       GST_PAD_TEMPLATE_CAPS (object) = g_value_dup_boxed (value);
415       break;
416     default:
417       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
418       break;
419   }
420 }
421
422 static void
423 gst_pad_template_get_property (GObject * object, guint prop_id, GValue * value,
424     GParamSpec * pspec)
425 {
426   /* these properties are all construct-only */
427   switch (prop_id) {
428     case PROP_NAME_TEMPLATE:
429       g_value_set_string (value, GST_PAD_TEMPLATE_NAME_TEMPLATE (object));
430       break;
431     case PROP_DIRECTION:
432       g_value_set_enum (value, GST_PAD_TEMPLATE_DIRECTION (object));
433       break;
434     case PROP_PRESENCE:
435       g_value_set_enum (value, GST_PAD_TEMPLATE_PRESENCE (object));
436       break;
437     case PROP_CAPS:
438       g_value_set_boxed (value, GST_PAD_TEMPLATE_CAPS (object));
439       break;
440     default:
441       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
442       break;
443   }
444 }