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