gstpad.c 178 KB
Newer Older
1 2 3 4
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
5
 * gstpad.c: Pads for linking elements together
Erik Walthinsen's avatar
Erik Walthinsen committed
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.
Erik Walthinsen's avatar
Erik Walthinsen committed
21
 */
22 23
/**
 * SECTION:gstpad
24
 * @title: GstPad
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
25 26
 * @short_description: Object contained by elements that allows links to
 *                     other elements
Wim Taymans's avatar
Wim Taymans committed
27
 * @see_also: #GstPadTemplate, #GstElement, #GstEvent, #GstQuery, #GstBuffer
28
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
29 30
 * A #GstElement is linked to other elements via "pads", which are extremely
 * light-weight generic link points.
31
 *
Wim Taymans's avatar
Wim Taymans committed
32 33
 * Pads have a #GstPadDirection, source pads produce data, sink pads consume
 * data.
34
 *
Wim Taymans's avatar
Wim Taymans committed
35 36 37 38 39
 * Pads are typically created from a #GstPadTemplate with
 * gst_pad_new_from_template() and are then added to a #GstElement. This usually
 * happens when the element is created but it can also happen dynamically based
 * on the data that the element is processing or based on the pads that the
 * application requests.
40 41
 *
 * Pads without pad templates can be created with gst_pad_new(),
42
 * which takes a direction and a name as an argument.  If the name is %NULL,
43 44
 * then a guaranteed unique name will be assigned to it.
 *
Wim Taymans's avatar
Wim Taymans committed
45 46 47 48
 * A #GstElement creating a pad will typically use the various
 * gst_pad_set_*_function() calls to register callbacks for events, queries or
 * dataflow on the pads.
 *
49 50
 * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
 *
51
 * After two pads are retrieved from an element by gst_element_get_static_pad(),
Wim Taymans's avatar
Wim Taymans committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 * the pads can be linked with gst_pad_link(). (For quick links,
 * you can also use gst_element_link(), which will make the obvious
 * link for you if it's straightforward.). Pads can be unlinked again with
 * gst_pad_unlink(). gst_pad_get_peer() can be used to check what the pad is
 * linked to.
 *
 * Before dataflow is possible on the pads, they need to be activated with
 * gst_pad_set_active().
 *
 * gst_pad_query() and gst_pad_peer_query() can be used to query various
 * properties of the pad and the stream.
 *
 * To send a #GstEvent on a pad, use gst_pad_send_event() and
 * gst_pad_push_event(). Some events will be sticky on the pad, meaning that
 * after they pass on the pad they can be queried later with
 * gst_pad_get_sticky_event() and gst_pad_sticky_events_foreach().
 * gst_pad_get_current_caps() and gst_pad_has_current_caps() are convenience
 * functions to query the current sticky CAPS event on a pad.
70
 *
Wim Taymans's avatar
Wim Taymans committed
71
 * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
72 73
 * or pull in a buffer.
 *
Wim Taymans's avatar
Wim Taymans committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
 * The dataflow, events and queries that happen on a pad can be monitored with
 * probes that can be installed with gst_pad_add_probe(). gst_pad_is_blocked()
 * can be used to check if a block probe is installed on the pad.
 * gst_pad_is_blocking() checks if the blocking probe is currently blocking the
 * pad. gst_pad_remove_probe() is used to remove a previously installed probe
 * and unblock blocking probes if any.
 *
 * Pad have an offset that can be retrieved with gst_pad_get_offset(). This
 * offset will be applied to the running_time of all data passing over the pad.
 * gst_pad_set_offset() can be used to change the offset.
 *
 * Convenience functions exist to start, pause and stop the task on a pad with
 * gst_pad_start_task(), gst_pad_pause_task() and gst_pad_stop_task()
 * respectively.
88
 */
Erik Walthinsen's avatar
Erik Walthinsen committed
89

90 91
#include "gst_private.h"

92
#include "gstpad.h"
93
#include "gstpadtemplate.h"
94
#include "gstenumtypes.h"
95
#include "gstutils.h"
96
#include "gstinfo.h"
97
#include "gsterror.h"
98
#include "gsttracerutils.h"
David Schleef's avatar
David Schleef committed
99
#include "gstvalue.h"
100
#include "glib-compat-private.h"
David Schleef's avatar
David Schleef committed
101

102
GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
103 104
#define GST_CAT_DEFAULT GST_CAT_PADS

105 106 107 108 109 110
/* Pad signals and args */
enum
{
  PAD_LINKED,
  PAD_UNLINKED,
  /* FILL ME */
111
  LAST_SIGNAL
112 113 114 115 116 117 118 119
};

enum
{
  PAD_PROP_0,
  PAD_PROP_CAPS,
  PAD_PROP_DIRECTION,
  PAD_PROP_TEMPLATE,
120 121
  PAD_PROP_OFFSET
      /* FILL ME */
122 123
};

124 125
#define _PAD_PROBE_TYPE_ALL_BOTH_AND_FLUSH (GST_PAD_PROBE_TYPE_ALL_BOTH | GST_PAD_PROBE_TYPE_EVENT_FLUSH)

126 127
/* we have a pending and an active event on the pad. On source pads only the
 * active event is used. On sinkpads, events are copied to the pending entry and
128
 * moved to the active event when the eventfunc returned %TRUE. */
129 130
typedef struct
{
Wim Taymans's avatar
Wim Taymans committed
131
  gboolean received;
132 133 134
  GstEvent *event;
} PadEvent;

Wim Taymans's avatar
Wim Taymans committed
135 136
struct _GstPadPrivate
{
Wim Taymans's avatar
Wim Taymans committed
137 138
  guint events_cookie;
  GArray *events;
139
  guint last_cookie;
140 141

  gint using;
Wim Taymans's avatar
Wim Taymans committed
142
  guint probe_list_cookie;
143 144 145 146 147

  /* counter of how many idle probes are running directly from the add_probe
   * call. Used to block any data flowing in the pad while the idle callback
   * Doesn't finish its work */
  gint idle_running;
148 149 150 151 152

  /* conditional and variable used to ensure pads only get (de)activated
   * by a single thread at a time. Protected by the object lock */
  GCond activation_cond;
  gboolean in_activation;
Wim Taymans's avatar
Wim Taymans committed
153 154
};

155 156 157 158 159
typedef struct
{
  GHook hook;
} GstProbe;

160 161
#define GST_PAD_IS_RUNNING_IDLE_PROBE(p) \
    (((GstPad *)(p))->priv->idle_running > 0)
162 163 164 165

typedef struct
{
  GstPad *pad;
Wim Taymans's avatar
Wim Taymans committed
166
  GstPadProbeInfo *info;
Wim Taymans's avatar
Wim Taymans committed
167
  gboolean dropped;
168
  gboolean pass;
169
  gboolean handled;
Wim Taymans's avatar
Wim Taymans committed
170
  gboolean marshalled;
171

172
  gulong *called_probes;
173 174 175
  guint n_called_probes;
  guint called_probes_size;
  gboolean retry;
176 177
} ProbeMarshall;

178
static void gst_pad_dispose (GObject * object);
179 180 181 182 183
static void gst_pad_finalize (GObject * object);
static void gst_pad_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_pad_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
184

185
static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
186
static gboolean gst_pad_activate_default (GstPad * pad, GstObject * parent);
187
static GstFlowReturn gst_pad_chain_list_default (GstPad * pad,
188
    GstObject * parent, GstBufferList * list);
189

Wim Taymans's avatar
Wim Taymans committed
190 191 192
static GstFlowReturn gst_pad_send_event_unchecked (GstPad * pad,
    GstEvent * event, GstPadProbeType type);
static GstFlowReturn gst_pad_push_event_unchecked (GstPad * pad,
193
    GstEvent * event, GstPadProbeType type);
Wim Taymans's avatar
Wim Taymans committed
194

195 196 197
static gboolean activate_mode_internal (GstPad * pad, GstObject * parent,
    GstPadMode mode, gboolean active);

198
static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
Erik Walthinsen's avatar
Erik Walthinsen committed
199

200 201
static GParamSpec *pspec_caps = NULL;

202
/* quarks for probe signals */
203
static GQuark buffer_quark;
204
static GQuark buffer_list_quark;
205 206
static GQuark event_quark;

207 208
typedef struct
{
209 210
  const gint ret;
  const gchar *name;
211 212 213 214
  GQuark quark;
} GstFlowQuarks;

static GstFlowQuarks flow_quarks[] = {
215
  {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
216 217
  {GST_FLOW_OK, "ok", 0},
  {GST_FLOW_NOT_LINKED, "not-linked", 0},
218
  {GST_FLOW_FLUSHING, "flushing", 0},
219
  {GST_FLOW_EOS, "eos", 0},
220 221 222
  {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
  {GST_FLOW_ERROR, "error", 0},
  {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
223
  {GST_FLOW_CUSTOM_ERROR, "custom-error", 0}
224 225 226 227 228 229 230 231
};

/**
 * gst_flow_get_name:
 * @ret: a #GstFlowReturn to get the name of.
 *
 * Gets a string representing the given flow return.
 *
232
 * Returns: a static string with the name of the flow return.
233
 */
234
const gchar *
235 236 237 238
gst_flow_get_name (GstFlowReturn ret)
{
  gint i;

239 240
  ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);

241
  for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    if (ret == flow_quarks[i].ret)
      return flow_quarks[i].name;
  }
  return "unknown";
}

/**
 * gst_flow_to_quark:
 * @ret: a #GstFlowReturn to get the quark of.
 *
 * Get the unique quark for the given GstFlowReturn.
 *
 * Returns: the quark associated with the flow return or 0 if an
 * invalid return was specified.
 */
GQuark
gst_flow_to_quark (GstFlowReturn ret)
{
  gint i;

262 263
  ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);

264
  for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {
265 266 267 268 269 270
    if (ret == flow_quarks[i].ret)
      return flow_quarks[i].quark;
  }
  return 0;
}

271 272 273 274 275 276 277 278
/**
 * gst_pad_link_get_name:
 * @ret: a #GstPadLinkReturn to get the name of.
 *
 * Gets a string representing the given pad-link return.
 *
 * Returns: a static string with the name of the pad-link return.
 *
279
 * Since: 1.4
280 281 282 283
 */
const gchar *
gst_pad_link_get_name (GstPadLinkReturn ret)
{
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  switch (ret) {
    case GST_PAD_LINK_OK:
      return "ok";
    case GST_PAD_LINK_WRONG_HIERARCHY:
      return "wrong hierarchy";
    case GST_PAD_LINK_WAS_LINKED:
      return "was linked";
    case GST_PAD_LINK_WRONG_DIRECTION:
      return "wrong direction";
    case GST_PAD_LINK_NOFORMAT:
      return "no common format";
    case GST_PAD_LINK_NOSCHED:
      return "incompatible scheduling";
    case GST_PAD_LINK_REFUSED:
      return "refused";
  }
  g_return_val_if_reached ("unknown");
301 302
}

303 304 305 306 307
#define _do_init \
{ \
  gint i; \
  \
  buffer_quark = g_quark_from_static_string ("buffer"); \
308
  buffer_list_quark = g_quark_from_static_string ("bufferlist"); \
309 310
  event_quark = g_quark_from_static_string ("event"); \
  \
311
  for (i = 0; i < G_N_ELEMENTS (flow_quarks); i++) {			\
312 313 314 315 316
    flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
  } \
  \
  GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
      GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
Erik Walthinsen's avatar
Erik Walthinsen committed
317 318
}

319
#define gst_pad_parent_class parent_class
320 321
G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT,
    G_ADD_PRIVATE (GstPad) _do_init);
322

Erik Walthinsen's avatar
Erik Walthinsen committed
323
static void
324
gst_pad_class_init (GstPadClass * klass)
325
{
326
  GObjectClass *gobject_class;
327
  GstObjectClass *gstobject_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
328

329 330
  gobject_class = G_OBJECT_CLASS (klass);
  gstobject_class = GST_OBJECT_CLASS (klass);
Erik Walthinsen's avatar
Erik Walthinsen committed
331

332 333 334 335
  gobject_class->dispose = gst_pad_dispose;
  gobject_class->finalize = gst_pad_finalize;
  gobject_class->set_property = gst_pad_set_property;
  gobject_class->get_property = gst_pad_get_property;
Erik Walthinsen's avatar
Erik Walthinsen committed
336

337 338 339 340 341 342 343
  /**
   * GstPad::linked:
   * @pad: the pad that emitted the signal
   * @peer: the peer pad that has been connected
   *
   * Signals that a pad has been linked to the peer pad.
   */
344
  gst_pad_signals[PAD_LINKED] =
345
      g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
346
      G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
347
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
348 349 350 351 352 353 354
  /**
   * GstPad::unlinked:
   * @pad: the pad that emitted the signal
   * @peer: the peer pad that has been disconnected
   *
   * Signals that a pad has been unlinked from the peer pad.
   */
355
  gst_pad_signals[PAD_UNLINKED] =
356
      g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
357
      G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
358
      g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_PAD);
359

360 361 362 363 364
  pspec_caps = g_param_spec_boxed ("caps", "Caps",
      "The capabilities of the pad", GST_TYPE_CAPS,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (gobject_class, PAD_PROP_CAPS, pspec_caps);

365
  g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
366 367
      g_param_spec_enum ("direction", "Direction", "The direction of the pad",
          GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
368
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
369

370
  /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
371
  g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
372 373
      g_param_spec_object ("template", "Template",
          "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
374
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375

376 377 378 379 380 381 382 383 384 385 386 387
  /**
   * GstPad:offset:
   *
   * The offset that will be applied to the running time of the pad.
   *
   * Since: 1.6
   */
  g_object_class_install_property (gobject_class, PAD_PROP_OFFSET,
      g_param_spec_int64 ("offset", "Offset",
          "The running time offset of the pad", 0, G_MAXINT64, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

388
  gstobject_class->path_string_separator = ".";
389

390 391 392 393 394
  /* Register common function pointer descriptions */
  GST_DEBUG_REGISTER_FUNCPTR (gst_pad_activate_default);
  GST_DEBUG_REGISTER_FUNCPTR (gst_pad_event_default);
  GST_DEBUG_REGISTER_FUNCPTR (gst_pad_query_default);
  GST_DEBUG_REGISTER_FUNCPTR (gst_pad_iterate_internal_links_default);
395
  GST_DEBUG_REGISTER_FUNCPTR (gst_pad_chain_list_default);
Erik Walthinsen's avatar
Erik Walthinsen committed
396 397
}

Wim Taymans's avatar
Wim Taymans committed
398
static void
399
gst_pad_init (GstPad * pad)
400
{
401
  pad->priv = gst_pad_get_instance_private (pad);
Wim Taymans's avatar
Wim Taymans committed
402

403
  GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
404

405 406 407 408
  GST_PAD_ACTIVATEFUNC (pad) = gst_pad_activate_default;
  GST_PAD_EVENTFUNC (pad) = gst_pad_event_default;
  GST_PAD_QUERYFUNC (pad) = gst_pad_query_default;
  GST_PAD_ITERINTLINKFUNC (pad) = gst_pad_iterate_internal_links_default;
409
  GST_PAD_CHAINLISTFUNC (pad) = gst_pad_chain_list_default;
410

411
  GST_PAD_SET_FLUSHING (pad);
Wim Taymans's avatar
Wim Taymans committed
412

Wim Taymans's avatar
Wim Taymans committed
413
  g_rec_mutex_init (&pad->stream_rec_lock);
Wim Taymans's avatar
Wim Taymans committed
414

Wim Taymans's avatar
Wim Taymans committed
415
  g_cond_init (&pad->block_cond);
416 417

  g_hook_list_init (&pad->probes, sizeof (GstProbe));
418

419
  pad->priv->events = g_array_sized_new (FALSE, TRUE, sizeof (PadEvent), 16);
420 421
  pad->priv->events_cookie = 0;
  pad->priv->last_cookie = -1;
422 423
  g_cond_init (&pad->priv->activation_cond);

424
  pad->ABI.abi.last_flowret = GST_FLOW_FLUSHING;
Wim Taymans's avatar
Wim Taymans committed
425 426
}

427
/* called when setting the pad inactive. It removes all sticky events from
Wim Taymans's avatar
Wim Taymans committed
428
 * the pad. must be called with object lock */
429
static void
Wim Taymans's avatar
Wim Taymans committed
430
remove_events (GstPad * pad)
431
{
Wim Taymans's avatar
Wim Taymans committed
432 433
  guint i, len;
  GArray *events;
434
  gboolean notify = FALSE;
435

Wim Taymans's avatar
Wim Taymans committed
436
  events = pad->priv->events;
Wim Taymans's avatar
Wim Taymans committed
437

Wim Taymans's avatar
Wim Taymans committed
438 439 440
  len = events->len;
  for (i = 0; i < len; i++) {
    PadEvent *ev = &g_array_index (events, PadEvent, i);
441 442 443 444
    GstEvent *event = ev->event;

    ev->event = NULL;

445 446
    if (event && GST_EVENT_TYPE (event) == GST_EVENT_CAPS)
      notify = TRUE;
447 448

    gst_event_unref (event);
Wim Taymans's avatar
Wim Taymans committed
449
  }
450

Wim Taymans's avatar
Wim Taymans committed
451
  GST_OBJECT_FLAG_UNSET (pad, GST_PAD_FLAG_PENDING_EVENTS);
Wim Taymans's avatar
Wim Taymans committed
452 453
  g_array_set_size (events, 0);
  pad->priv->events_cookie++;
454 455 456 457 458 459 460 461 462

  if (notify) {
    GST_OBJECT_UNLOCK (pad);

    GST_DEBUG_OBJECT (pad, "notify caps");
    g_object_notify_by_pspec ((GObject *) pad, pspec_caps);

    GST_OBJECT_LOCK (pad);
  }
Wim Taymans's avatar
Wim Taymans committed
463 464
}

Wim Taymans's avatar
Wim Taymans committed
465
/* should be called with object lock */
Wim Taymans's avatar
Wim Taymans committed
466 467
static PadEvent *
find_event_by_type (GstPad * pad, GstEventType type, guint idx)
468
{
Wim Taymans's avatar
Wim Taymans committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
  guint i, len;
  GArray *events;
  PadEvent *ev;

  events = pad->priv->events;
  len = events->len;

  for (i = 0; i < len; i++) {
    ev = &g_array_index (events, PadEvent, i);
    if (ev->event == NULL)
      continue;

    if (GST_EVENT_TYPE (ev->event) == type) {
      if (idx == 0)
        goto found;
      idx--;
485 486
    } else if (GST_EVENT_TYPE (ev->event) > type) {
      break;
Wim Taymans's avatar
Wim Taymans committed
487
    }
488
  }
Wim Taymans's avatar
Wim Taymans committed
489 490 491 492 493
  ev = NULL;
found:
  return ev;
}

Wim Taymans's avatar
Wim Taymans committed
494
/* should be called with OBJECT lock */
Wim Taymans's avatar
Wim Taymans committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508
static PadEvent *
find_event (GstPad * pad, GstEvent * event)
{
  guint i, len;
  GArray *events;
  PadEvent *ev;

  events = pad->priv->events;
  len = events->len;

  for (i = 0; i < len; i++) {
    ev = &g_array_index (events, PadEvent, i);
    if (event == ev->event)
      goto found;
509 510
    else if (GST_EVENT_TYPE (ev->event) > GST_EVENT_TYPE (event))
      break;
Wim Taymans's avatar
Wim Taymans committed
511 512 513 514
  }
  ev = NULL;
found:
  return ev;
515 516
}

Wim Taymans's avatar
Wim Taymans committed
517
/* should be called with OBJECT lock */
Wim Taymans's avatar
Wim Taymans committed
518
static void
Wim Taymans's avatar
Wim Taymans committed
519
remove_event_by_type (GstPad * pad, GstEventType type)
Wim Taymans's avatar
Wim Taymans committed
520
{
Wim Taymans's avatar
Wim Taymans committed
521 522 523 524 525 526 527 528 529 530 531 532 533
  guint i, len;
  GArray *events;
  PadEvent *ev;

  events = pad->priv->events;
  len = events->len;

  i = 0;
  while (i < len) {
    ev = &g_array_index (events, PadEvent, i);
    if (ev->event == NULL)
      goto next;

534 535 536
    if (GST_EVENT_TYPE (ev->event) > type)
      break;
    else if (GST_EVENT_TYPE (ev->event) != type)
Wim Taymans's avatar
Wim Taymans committed
537 538 539 540 541 542 543 544 545 546 547 548 549
      goto next;

    gst_event_unref (ev->event);
    g_array_remove_index (events, i);
    len--;
    pad->priv->events_cookie++;
    continue;

  next:
    i++;
  }
}

Wim Taymans's avatar
Wim Taymans committed
550
/* check all events on srcpad against those on sinkpad. All events that are not
551
 * on sinkpad are marked as received=%FALSE and the PENDING_EVENTS is set on the
Wim Taymans's avatar
Wim Taymans committed
552
 * srcpad so that the events will be sent next time */
Wim Taymans's avatar
Wim Taymans committed
553
/* should be called with srcpad and sinkpad LOCKS */
Wim Taymans's avatar
Wim Taymans committed
554 555 556 557 558 559
static void
schedule_events (GstPad * srcpad, GstPad * sinkpad)
{
  gint i, len;
  GArray *events;
  PadEvent *ev;
Wim Taymans's avatar
Wim Taymans committed
560
  gboolean pending = FALSE;
Wim Taymans's avatar
Wim Taymans committed
561

Wim Taymans's avatar
Wim Taymans committed
562 563 564 565 566 567 568
  events = srcpad->priv->events;
  len = events->len;

  for (i = 0; i < len; i++) {
    ev = &g_array_index (events, PadEvent, i);
    if (ev->event == NULL)
      continue;
Wim Taymans's avatar
Wim Taymans committed
569

Wim Taymans's avatar
Wim Taymans committed
570 571 572 573 574
    if (sinkpad == NULL || !find_event (sinkpad, ev->event)) {
      ev->received = FALSE;
      pending = TRUE;
    }
  }
Wim Taymans's avatar
Wim Taymans committed
575
  if (pending)
Wim Taymans's avatar
Wim Taymans committed
576
    GST_OBJECT_FLAG_SET (srcpad, GST_PAD_FLAG_PENDING_EVENTS);
Wim Taymans's avatar
Wim Taymans committed
577 578
}

Wim Taymans's avatar
Wim Taymans committed
579 580 581
typedef gboolean (*PadEventFunction) (GstPad * pad, PadEvent * ev,
    gpointer user_data);

Wim Taymans's avatar
Wim Taymans committed
582
/* should be called with pad LOCK */
Wim Taymans's avatar
Wim Taymans committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
static void
events_foreach (GstPad * pad, PadEventFunction func, gpointer user_data)
{
  guint i, len;
  GArray *events;
  gboolean ret;
  guint cookie;

  events = pad->priv->events;

restart:
  cookie = pad->priv->events_cookie;
  i = 0;
  len = events->len;
  while (i < len) {
    PadEvent *ev, ev_ret;

    ev = &g_array_index (events, PadEvent, i);
    if (G_UNLIKELY (ev->event == NULL))
      goto next;

luz.paz's avatar
luz.paz committed
604
    /* take additional ref, func might release the lock */
Wim Taymans's avatar
Wim Taymans committed
605 606 607 608 609 610 611 612 613 614 615 616 617
    ev_ret.event = gst_event_ref (ev->event);
    ev_ret.received = ev->received;

    ret = func (pad, &ev_ret, user_data);

    /* recheck the cookie, lock might have been released and the list could have
     * changed */
    if (G_UNLIKELY (cookie != pad->priv->events_cookie)) {
      if (G_LIKELY (ev_ret.event))
        gst_event_unref (ev_ret.event);
      goto restart;
    }

618 619 620
    /* store the received state */
    ev->received = ev_ret.received;

Wim Taymans's avatar
Wim Taymans committed
621 622 623 624
    /* if the event changed, we need to do something */
    if (G_UNLIKELY (ev->event != ev_ret.event)) {
      if (G_UNLIKELY (ev_ret.event == NULL)) {
        /* function unreffed and set the event to NULL, remove it */
625
        gst_event_unref (ev->event);
Wim Taymans's avatar
Wim Taymans committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
        g_array_remove_index (events, i);
        len--;
        cookie = ++pad->priv->events_cookie;
        continue;
      } else {
        /* function gave a new event for us */
        gst_event_take (&ev->event, ev_ret.event);
      }
    } else {
      /* just unref, nothing changed */
      gst_event_unref (ev_ret.event);
    }
    if (!ret)
      break;
  next:
    i++;
  }
}
Wim Taymans's avatar
Wim Taymans committed
644 645 646

/* should be called with LOCK */
static GstEvent *
647 648
_apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream,
    gint64 pad_offset)
Wim Taymans's avatar
Wim Taymans committed
649
{
650
  gint64 offset;
651

652
  GST_DEBUG_OBJECT (pad, "apply pad offset %" GST_STIME_FORMAT,
653
      GST_STIME_ARGS (pad_offset));
654

655 656
  if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
    GstSegment segment;
657

658
    g_assert (!upstream);
659

660 661 662
    /* copy segment values */
    gst_event_copy_segment (event, &segment);
    gst_event_unref (event);
663

664
    gst_segment_offset_running_time (&segment, segment.format, pad_offset);
665
    event = gst_event_new_segment (&segment);
Wim Taymans's avatar
Wim Taymans committed
666
  }
667 668 669 670

  event = gst_event_make_writable (event);
  offset = gst_event_get_running_time_offset (event);
  if (upstream)
671
    offset -= pad_offset;
672
  else
673
    offset += pad_offset;
674 675
  gst_event_set_running_time_offset (event, offset);

Wim Taymans's avatar
Wim Taymans committed
676
  return event;
Wim Taymans's avatar
Wim Taymans committed
677 678
}

679 680 681 682
static inline GstEvent *
apply_pad_offset (GstPad * pad, GstEvent * event, gboolean upstream)
{
  if (G_UNLIKELY (pad->offset != 0))
683
    return _apply_pad_offset (pad, event, upstream, pad->offset);
684 685 686
  return event;
}

Wim Taymans's avatar
Wim Taymans committed
687
/* should be called with the OBJECT_LOCK */
688 689 690 691
static GstCaps *
get_pad_caps (GstPad * pad)
{
  GstCaps *caps = NULL;
Wim Taymans's avatar
Wim Taymans committed
692
  PadEvent *ev;
693

Wim Taymans's avatar
Wim Taymans committed
694 695 696
  ev = find_event_by_type (pad, GST_EVENT_CAPS, 0);
  if (ev && ev->event)
    gst_event_parse_caps (ev->event, &caps);
697 698 699

  return caps;
}
700

701
static void
702 703
gst_pad_dispose (GObject * object)
{
Wim Taymans's avatar
Wim Taymans committed
704
  GstPad *pad = GST_PAD_CAST (object);
705
  GstPad *peer;
706

707
  GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "%p dispose", pad);
708

709 710 711 712 713 714 715 716 717 718 719 720
  /* unlink the peer pad */
  if ((peer = gst_pad_get_peer (pad))) {
    /* window for MT unsafeness, someone else could unlink here
     * and then we call unlink with wrong pads. The unlink
     * function would catch this and safely return failed. */
    if (GST_PAD_IS_SRC (pad))
      gst_pad_unlink (pad, peer);
    else
      gst_pad_unlink (peer, pad);

    gst_object_unref (peer);
  }
721

722
  gst_pad_set_pad_template (pad, NULL);
723

Wim Taymans's avatar
Wim Taymans committed
724
  GST_OBJECT_LOCK (pad);
Wim Taymans's avatar
Wim Taymans committed
725
  remove_events (pad);
Wim Taymans's avatar
Wim Taymans committed
726
  GST_OBJECT_UNLOCK (pad);
727

728 729
  g_hook_list_clear (&pad->probes);

730
  G_OBJECT_CLASS (parent_class)->dispose (object);
731 732 733 734 735
}

static void
gst_pad_finalize (GObject * object)
{
Wim Taymans's avatar
Wim Taymans committed
736
  GstPad *pad = GST_PAD_CAST (object);
737 738 739 740 741 742 743 744
  GstTask *task;

  /* in case the task is still around, clean it up */
  if ((task = GST_PAD_TASK (pad))) {
    gst_task_join (task);
    GST_PAD_TASK (pad) = NULL;
    gst_object_unref (task);
  }
745

746
  if (pad->activatenotify)
747
    pad->activatenotify (pad->activatedata);
748
  if (pad->activatemodenotify)
749
    pad->activatemodenotify (pad->activatemodedata);
750
  if (pad->linknotify)
751
    pad->linknotify (pad->linkdata);
752
  if (pad->unlinknotify)
753
    pad->unlinknotify (pad->unlinkdata);
754
  if (pad->chainnotify)
755
    pad->chainnotify (pad->chaindata);
756
  if (pad->chainlistnotify)
757
    pad->chainlistnotify (pad->chainlistdata);
758
  if (pad->getrangenotify)
759
    pad->getrangenotify (pad->getrangedata);
760
  if (pad->eventnotify)
761
    pad->eventnotify (pad->eventdata);
762
  if (pad->querynotify)
763
    pad->querynotify (pad->querydata);
764
  if (pad->iterintlinknotify)
765
    pad->iterintlinknotify (pad->iterintlinkdata);
766

Wim Taymans's avatar
Wim Taymans committed
767 768
  g_rec_mutex_clear (&pad->stream_rec_lock);
  g_cond_clear (&pad->block_cond);
769
  g_cond_clear (&pad->priv->activation_cond);
Wim Taymans's avatar
Wim Taymans committed
770
  g_array_free (pad->priv->events, TRUE);
771

772
  G_OBJECT_CLASS (parent_class)->finalize (object);
773 774 775 776
}

static void
gst_pad_set_property (GObject * object, guint prop_id,
777
    const GValue * value, GParamSpec * pspec)
778
{
779
  g_return_if_fail (GST_IS_PAD (object));
780

781
  switch (prop_id) {
782
    case PAD_PROP_DIRECTION:
783
      GST_PAD_DIRECTION (object) = (GstPadDirection) g_value_get_enum (value);
784 785 786
      break;
    case PAD_PROP_TEMPLATE:
      gst_pad_set_pad_template (GST_PAD_CAST (object),
787
          (GstPadTemplate *) g_value_get_object (value));
788
      break;
789 790 791
    case PAD_PROP_OFFSET:
      gst_pad_set_offset (GST_PAD_CAST (object), g_value_get_int64 (value));
      break;
792
    default:
793
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
794 795 796 797 798
      break;
  }
}

static void
799
gst_pad_get_property (GObject * object, guint prop_id,
800
    GValue * value, GParamSpec * pspec)
801 802 803
{
  g_return_if_fail (GST_IS_PAD (object));

804
  switch (prop_id) {
805
    case PAD_PROP_CAPS:
806
      GST_OBJECT_LOCK (object);
807
      g_value_set_boxed (value, get_pad_caps (GST_PAD_CAST (object)));
808
      GST_OBJECT_UNLOCK (object);
809 810 811 812 813
      break;
    case PAD_PROP_DIRECTION:
      g_value_set_enum (value, GST_PAD_DIRECTION (object));
      break;
    case PAD_PROP_TEMPLATE:
814
      g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
Wim Taymans's avatar
Wim Taymans committed
815
      break;
816 817 818
    case PAD_PROP_OFFSET:
      g_value_set_int64 (value, gst_pad_get_offset (GST_PAD_CAST (object)));
      break;
819
    default:
820
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
821 822 823 824
      break;
  }
}

825
/**
826
 * gst_pad_new:
827
 * @name: (allow-none): the name of the new pad.
828
 * @direction: the #GstPadDirection of the pad.
829
 *
830
 * Creates a new pad with the given name in the given direction.
831
 * If name is %NULL, a guaranteed unique name (across all pads)
832
 * will be assigned.
833
 * This function makes a copy of the name so you can safely free the name.
834
 *
835
 * Returns: (transfer floating): a new #GstPad.
836 837
 *
 * MT safe.
838
 */
839 840
GstPad *
gst_pad_new (const gchar * name, GstPadDirection direction)
841
{
842 843
  return g_object_new (GST_TYPE_PAD,
      "name", name, "direction", direction, NULL);
844
}
845

846
/**
847 848
 * gst_pad_new_from_template:
 * @templ: the pad template to use
849
 * @name: (allow-none): the name of the pad
850
 *
851
 * Creates a new pad with the given name from the given template.
852
 * If name is %NULL, a guaranteed unique name (across all pads)
853
 * will be assigned.
854
 * This function makes a copy of the name so you can safely free the name.
855
 *
856
 * Returns: (transfer floating): a new #GstPad.
857
 */
858
GstPad *
859
gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
860
{
861 862 863
  GType pad_type =
      GST_PAD_TEMPLATE_GTYPE (templ) ==
      G_TYPE_NONE ? GST_TYPE_PAD : GST_PAD_TEMPLATE_GTYPE (templ);
864

865 866
  g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);

867
  return g_object_new (pad_type,
868
      "name", name, "direction", templ->direction, "template", templ, NULL);
Erik Walthinsen's avatar
Erik Walthinsen committed
869 870
}

871 872 873
/**
 * gst_pad_new_from_static_template:
 * @templ: the #GstStaticPadTemplate to use
874
 * @name: the name of the pad
875 876
 *
 * Creates a new pad with the given name from the given static template.
877
 * If name is %NULL, a guaranteed unique name (across all pads)
878 879 880
 * will be assigned.
 * This function makes a copy of the name so you can safely free the name.
 *
881
 * Returns: (transfer floating): a new #GstPad.
882 883 884 885 886 887 888 889 890 891 892 893 894 895
 */
GstPad *
gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
    const gchar * name)
{
  GstPad *pad;
  GstPadTemplate *template;

  template = gst_static_pad_template_get (templ);
  pad = gst_pad_new_from_template (template, name);
  gst_object_unref (template);
  return pad;
}

896 897 898 899 900 901 902 903 904 905 906 907 908 909
#define ACQUIRE_PARENT(pad, parent, label)                      \
  G_STMT_START {                                                \
    if (G_LIKELY ((parent = GST_OBJECT_PARENT (pad))))          \
      gst_object_ref (parent);                                  \
    else if (G_LIKELY (GST_PAD_NEEDS_PARENT (pad)))             \
      goto label;                                               \
  } G_STMT_END

#define RELEASE_PARENT(parent)                                  \
  G_STMT_START {                                                \
    if (G_LIKELY (parent))                                      \
      gst_object_unref (parent);                                \
  } G_STMT_END

910 911
/**
 * gst_pad_get_direction:
912
 * @pad: a #GstPad to get the direction of.
913
 *
914
 * Gets the direction of the pad. The direction of the pad is
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
915
 * decided at construction time so this function does not take
916
 * the LOCK.
917
 *
918
 * Returns: the #GstPadDirection of the pad.
919 920
 *
 * MT safe.
921
 */
Wim Taymans's avatar
Wim Taymans committed
922
GstPadDirection
923
gst_pad_get_direction (GstPad * pad)
924
{
925 926
  GstPadDirection result;

927
  /* PAD_UNKNOWN is a little silly but we need some sort of
928
   * error return value */
929
  g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
Erik Walthinsen's avatar
Erik Walthinsen committed
930

931
  result = GST_PAD_DIRECTION (pad);
932 933

  return result;
Erik Walthinsen's avatar
Erik Walthinsen committed
934 935
}

936
static gboolean
937
gst_pad_activate_default (GstPad * pad, GstObject * parent)
938
{
939 940 941
  g_return_val_if_fail (GST_IS_PAD (pad), FALSE);

  return activate_mode_internal (pad, parent, GST_PAD_MODE_PUSH, TRUE);
942 943
}

944 945 946 947 948 949 950 951 952
/**
 * gst_pad_mode_get_name:
 * @mode: the pad mode
 *
 * Return the name of a pad mode, for use in debug messages mostly.
 *
 * Returns: short mnemonic for pad mode @mode
 */
const gchar *
953 954 955 956 957 958