gstpadtemplate.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstpadtemplate.c: Templates for pad creation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
21
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
22

23 24
/**
 * SECTION:gstpadtemplate
25
 * @title: GstPadTemplate
26 27 28 29
 * @short_description: Describe the media type of a pad.
 * @see_also: #GstPad, #GstElementFactory
 *
 * Padtemplates describe the possible media types a pad or an elementfactory can
30 31 32
 * handle. This allows for both inspection of handled types before loading the
 * element plugin as well as identifying pads on elements that are not yet
 * created (request or sometimes pads).
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
33
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
34
 * Pad and PadTemplates have #GstCaps attached to it to describe the media type
35 36
 * they are capable of dealing with. gst_pad_template_get_caps() or
 * 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.
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
38
 *
39
 * PadTemplates have a #GstPadPresence property which identifies the lifetime
40 41
 * of the pad and that can be retrieved with GST_PAD_TEMPLATE_PRESENCE(). Also
 * the direction of the pad can be retrieved from the #GstPadTemplate with
42 43 44
 * GST_PAD_TEMPLATE_DIRECTION().
 *
 * The GST_PAD_TEMPLATE_NAME_TEMPLATE () is important for GST_PAD_REQUEST pads
Stefan Kost's avatar
Stefan Kost committed
45
 * because it has to be used as the name in the gst_element_get_request_pad()
46 47
 * call to instantiate a pad from this template.
 *
48
 * Padtemplates can be created with gst_pad_template_new() or with
49 50
 * gst_static_pad_template_get (), which creates a #GstPadTemplate from a
 * #GstStaticPadTemplate that can be filled with the
51
 * convenient GST_STATIC_PAD_TEMPLATE() macro.
52
 *
53 54
 * A padtemplate can be used to create a pad (see gst_pad_new_from_template()
 * or gst_pad_new_from_static_template ()) or to add to an element class
55
 * (see gst_element_class_add_static_pad_template ()).
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
56
 *
57
 * The following code example shows the code to create a pad from a padtemplate.
58
 * |[<!-- language="C" -->
59 60
 *   GstStaticPadTemplate my_template =
 *   GST_STATIC_PAD_TEMPLATE (
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61 62 63 64
 *     "sink",          // the name of the pad
 *     GST_PAD_SINK,    // the direction of the pad
 *     GST_PAD_ALWAYS,  // when this pad will be present
 *     GST_STATIC_CAPS (        // the capabilities of the padtemplate
65
 *       "audio/x-raw, "
66 67
 *         "channels = (int) [ 1, 6 ]"
 *     )
68
 *   );
69
 *   void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
70
 *   my_method (void)
71 72
 *   {
 *     GstPad *pad;
73
 *     pad = gst_pad_new_from_static_template (&amp;my_template, "sink");
74 75
 *     ...
 *   }
76
 * ]|
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
77
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
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 92 93 94 95 96 97 98 99 100 101 102
 */

#include "gst_private.h"

#include "gstpad.h"
#include "gstpadtemplate.h"
#include "gstenumtypes.h"
#include "gstutils.h"
#include "gstinfo.h"
#include "gsterror.h"
#include "gstvalue.h"

#define GST_CAT_DEFAULT GST_CAT_PADS

103 104 105 106 107 108 109 110
enum
{
  PROP_NAME_TEMPLATE = 1,
  PROP_DIRECTION,
  PROP_PRESENCE,
  PROP_CAPS
};

111 112 113 114 115 116 117 118 119 120
enum
{
  TEMPL_PAD_CREATED,
  /* FILL ME */
  LAST_SIGNAL
};

static guint gst_pad_template_signals[LAST_SIGNAL] = { 0 };

static void gst_pad_template_dispose (GObject * object);
121 122 123 124
static void gst_pad_template_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_pad_template_get_property (GObject * object, guint prop_id,
    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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

static void
gst_pad_template_class_init (GstPadTemplateClass * klass)
{
  GObjectClass *gobject_class;
  GstObjectClass *gstobject_class;

  gobject_class = (GObjectClass *) klass;
  gstobject_class = (GstObjectClass *) klass;

  /**
   * GstPadTemplate::pad-created:
   * @pad_template: the object which received the signal.
   * @pad: the pad that was created.
   *
   * This signal is fired when an element creates a pad from this template.
   */
  gst_pad_template_signals[TEMPL_PAD_CREATED] =
      g_signal_new ("pad-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstPadTemplateClass, pad_created),
148
      NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
149 150 151

  gobject_class->dispose = gst_pad_template_dispose;

152 153 154 155
  gobject_class->get_property = gst_pad_template_get_property;
  gobject_class->set_property = gst_pad_template_set_property;

  /**
156
   * GstPadTemplate:name-template:
157 158 159 160 161 162 163 164 165
   *
   * The name template of the pad template.
   */
  g_object_class_install_property (gobject_class, PROP_NAME_TEMPLATE,
      g_param_spec_string ("name-template", "Name template",
          "The name template of the pad template", NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  /**
166
   * GstPadTemplate:direction:
167 168 169 170 171 172 173 174 175 176
   *
   * The direction of the pad described by the pad template.
   */
  g_object_class_install_property (gobject_class, PROP_DIRECTION,
      g_param_spec_enum ("direction", "Direction",
          "The direction of the pad described by the pad template",
          GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  /**
177
   * GstPadTemplate:presence:
178 179 180 181 182 183 184 185 186 187
   *
   * When the pad described by the pad template will become available.
   */
  g_object_class_install_property (gobject_class, PROP_PRESENCE,
      g_param_spec_enum ("presence", "Presence",
          "When the pad described by the pad template will become available",
          GST_TYPE_PAD_PRESENCE, GST_PAD_ALWAYS,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  /**
188
   * GstPadTemplate:caps:
189 190 191 192 193 194 195 196 197
   *
   * The capabilities of the pad described by the pad template.
   */
  g_object_class_install_property (gobject_class, PROP_CAPS,
      g_param_spec_boxed ("caps", "Caps",
          "The capabilities of the pad described by the pad template",
          GST_TYPE_CAPS,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

198 199 200 201
  gstobject_class->path_string_separator = "*";
}

static void
202
gst_pad_template_init (GstPadTemplate * templ)
203
{
Guillaume Desmottes's avatar
Guillaume Desmottes committed
204 205
  /* GstPadTemplate objects are usually leaked */
  GST_OBJECT_FLAG_SET (templ, GST_OBJECT_FLAG_MAY_BE_LEAKED);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
}

static void
gst_pad_template_dispose (GObject * object)
{
  GstPadTemplate *templ = GST_PAD_TEMPLATE (object);

  g_free (GST_PAD_TEMPLATE_NAME_TEMPLATE (templ));
  if (GST_PAD_TEMPLATE_CAPS (templ)) {
    gst_caps_unref (GST_PAD_TEMPLATE_CAPS (templ));
  }

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

/* ALWAYS padtemplates cannot have conversion specifications (like src_%d),
 * since it doesn't make sense.
 * SOMETIMES padtemplates can do whatever they want, they are provided by the
 * element.
225 226
 * REQUEST padtemplates can have multiple specifiers in case of %d and %u, like
 * src_%u_%u, but %s only can be used once in the template.
227
 */
228
static gboolean
229 230
name_is_valid (const gchar * name, GstPadPresence presence)
{
231 232
  const gchar *str, *underscore = NULL;
  gboolean has_s = FALSE;
233 234 235 236 237 238 239 240

  if (presence == GST_PAD_ALWAYS) {
    if (strchr (name, '%')) {
      g_warning ("invalid name template %s: conversion specifications are not"
          " allowed for GST_PAD_ALWAYS padtemplates", name);
      return FALSE;
    }
  } else if (presence == GST_PAD_REQUEST) {
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    str = strchr (name, '%');

    while (str) {
      if (*(str + 1) != 's' && *(str + 1) != 'd' && *(str + 1) != 'u') {
        g_warning
            ("invalid name template %s: conversion specification must be of"
            " type '%%d', '%%u' or '%%s' for GST_PAD_REQUEST padtemplate",
            name);
        return FALSE;
      }

      if (*(str + 1) == 's' && (*(str + 2) != '\0' || has_s)) {
        g_warning
            ("invalid name template %s: conversion specification of type '%%s'"
            "only can be used once in the GST_PAD_REQUEST padtemplate at the "
            "very end and not allowed any other characters with '%%s'", name);
        return FALSE;
      }

      if (*(str + 1) == 's') {
        has_s = TRUE;
      }

      underscore = strchr (str, '_');
      str = strchr (str + 1, '%');

      if (str && (!underscore || (underscore && str < underscore))) {
        g_warning
            ("invalid name template %s: each of conversion specifications "
            "must be separated by an underscore", name);
        return FALSE;
      }
273 274
    }
  }
275

276 277 278
  return TRUE;
}

279
G_DEFINE_POINTER_TYPE (GstStaticPadTemplate, gst_static_pad_template);
280

281 282 283 284 285 286
/**
 * gst_static_pad_template_get:
 * @pad_template: the static pad template
 *
 * Converts a #GstStaticPadTemplate into a #GstPadTemplate.
 *
287
 * Returns: (transfer full): a new #GstPadTemplate.
288
 */
289
/* FIXME0.11: rename to gst_pad_template_new_from_static_pad_template() */
290 291 292 293
GstPadTemplate *
gst_static_pad_template_get (GstStaticPadTemplate * pad_template)
{
  GstPadTemplate *new;
294
  GstCaps *caps;
295 296 297 298

  if (!name_is_valid (pad_template->name_template, pad_template->presence))
    return NULL;

299
  caps = gst_static_caps_get (&pad_template->static_caps);
300

301 302 303 304 305
  new = g_object_new (gst_pad_template_get_type (),
      "name", pad_template->name_template,
      "name-template", pad_template->name_template,
      "direction", pad_template->direction,
      "presence", pad_template->presence, "caps", caps, NULL);
306

307
  gst_caps_unref (caps);
308 309 310 311 312 313 314 315 316

  return new;
}

/**
 * gst_pad_template_new:
 * @name_template: the name template.
 * @direction: the #GstPadDirection of the template.
 * @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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
 */
GstPadTemplate *
gst_pad_template_new (const gchar * name_template,
    GstPadDirection direction, GstPadPresence presence, GstCaps * caps)
{
  GstPadTemplate *new;

  g_return_val_if_fail (name_template != NULL, NULL);
  g_return_val_if_fail (caps != NULL, NULL);
  g_return_val_if_fail (direction == GST_PAD_SRC
      || direction == GST_PAD_SINK, NULL);
  g_return_val_if_fail (presence == GST_PAD_ALWAYS
      || presence == GST_PAD_SOMETIMES || presence == GST_PAD_REQUEST, NULL);

  if (!name_is_valid (name_template, presence)) {
    return NULL;
  }

  new = g_object_new (gst_pad_template_get_type (),
342 343
      "name", name_template, "name-template", name_template,
      "direction", direction, "presence", presence, "caps", caps, NULL);
344 345 346 347 348 349 350 351 352 353

  return new;
}

/**
 * gst_static_pad_template_get_caps:
 * @templ: a #GstStaticPadTemplate to get capabilities of.
 *
 * Gets the capabilities of the static pad template.
 *
354
 * Returns: (transfer full): the #GstCaps of the static pad template.
355 356 357
 * Unref after usage. Since the core holds an additional
 * ref to the returned caps, use gst_caps_make_writable()
 * on the returned caps to modify it.
358 359 360 361 362 363
 */
GstCaps *
gst_static_pad_template_get_caps (GstStaticPadTemplate * templ)
{
  g_return_val_if_fail (templ, NULL);

364
  return gst_static_caps_get (&templ->static_caps);
365 366 367 368 369 370 371 372
}

/**
 * gst_pad_template_get_caps:
 * @templ: a #GstPadTemplate to get capabilities of.
 *
 * Gets the capabilities of the pad template.
 *
373 374
 * Returns: (transfer full): the #GstCaps of the pad template.
 * Unref after usage.
375 376 377 378
 */
GstCaps *
gst_pad_template_get_caps (GstPadTemplate * templ)
{
379
  GstCaps *caps;
380 381
  g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);

382 383 384
  caps = GST_PAD_TEMPLATE_CAPS (templ);

  return (caps ? gst_caps_ref (caps) : NULL);
385 386
}

387 388 389 390 391 392 393
/**
 * gst_pad_template_pad_created:
 * @templ: a #GstPadTemplate that has been created
 * @pad:   the #GstPad that created it
 *
 * Emit the pad-created signal for this template when created by this pad.
 */
394 395 396
void
gst_pad_template_pad_created (GstPadTemplate * templ, GstPad * pad)
{
397
  g_signal_emit (templ, gst_pad_template_signals[TEMPL_PAD_CREATED], 0, pad);
398
}
399 400 401 402 403 404 405 406 407 408 409

static void
gst_pad_template_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  /* these properties are all construct-only */
  switch (prop_id) {
    case PROP_NAME_TEMPLATE:
      GST_PAD_TEMPLATE_NAME_TEMPLATE (object) = g_value_dup_string (value);
      break;
    case PROP_DIRECTION:
410 411
      GST_PAD_TEMPLATE_DIRECTION (object) =
          (GstPadDirection) g_value_get_enum (value);
412 413
      break;
    case PROP_PRESENCE:
414 415
      GST_PAD_TEMPLATE_PRESENCE (object) =
          (GstPadPresence) g_value_get_enum (value);
416 417
      break;
    case PROP_CAPS:
418
      GST_PAD_TEMPLATE_CAPS (object) = g_value_dup_boxed (value);
419 420 421 422 423
      if (GST_PAD_TEMPLATE_CAPS (object) != NULL) {
        /* GstPadTemplate are usually leaked so are their caps */
        GST_MINI_OBJECT_FLAG_SET (GST_PAD_TEMPLATE_CAPS (object),
            GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
      }
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_pad_template_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  /* these properties are all construct-only */
  switch (prop_id) {
    case PROP_NAME_TEMPLATE:
      g_value_set_string (value, GST_PAD_TEMPLATE_NAME_TEMPLATE (object));
      break;
    case PROP_DIRECTION:
      g_value_set_enum (value, GST_PAD_TEMPLATE_DIRECTION (object));
      break;
    case PROP_PRESENCE:
      g_value_set_enum (value, GST_PAD_TEMPLATE_PRESENCE (object));
      break;
    case PROP_CAPS:
      g_value_set_boxed (value, GST_PAD_TEMPLATE_CAPS (object));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}