bufferlist: fix a comment
[platform/upstream/gstreamer.git] / gst / gstpad.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpad.c: Pads for linking elements together
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  * SECTION:gstpad
24  * @short_description: Object contained by elements that allows links to
25  *                     other elements
26  * @see_also: #GstPadTemplate, #GstElement, #GstEvent
27  *
28  * A #GstElement is linked to other elements via "pads", which are extremely
29  * light-weight generic link points.
30  * After two pads are retrieved from an element with gst_element_get_pad(),
31  * the pads can be link with gst_pad_link(). (For quick links,
32  * you can also use gst_element_link(), which will make the obvious
33  * link for you if it's straightforward.)
34  *
35  * Pads are typically created from a #GstPadTemplate with
36  * gst_pad_new_from_template().
37  *
38  * Pads have #GstCaps attached to it to describe the media type they are
39  * capable of dealing with.  gst_pad_get_caps() and gst_pad_set_caps() are
40  * used to manipulate the caps of the pads.
41  * Pads created from a pad template cannot set capabilities that are
42  * incompatible with the pad template capabilities.
43  *
44  * Pads without pad templates can be created with gst_pad_new(),
45  * which takes a direction and a name as an argument.  If the name is NULL,
46  * then a guaranteed unique name will be assigned to it.
47  *
48  * gst_pad_get_parent() will retrieve the #GstElement that owns the pad.
49  *
50  * A #GstElement creating a pad will typically use the various
51  * gst_pad_set_*_function() calls to register callbacks for various events
52  * on the pads.
53  *
54  * GstElements will use gst_pad_push() and gst_pad_pull_range() to push out
55  * or pull in a buffer.
56  *
57  * To send a #GstEvent on a pad, use gst_pad_send_event() and
58  * gst_pad_push_event().
59  *
60  * Last reviewed on 2006-07-06 (0.10.9)
61  */
62
63 #include "gst_private.h"
64
65 #include "gstpad.h"
66 #include "gstpadtemplate.h"
67 #include "gstenumtypes.h"
68 #include "gstmarshal.h"
69 #include "gstutils.h"
70 #include "gstinfo.h"
71 #include "gsterror.h"
72 #include "gstvalue.h"
73 #include "glib-compat-private.h"
74
75 GST_DEBUG_CATEGORY_STATIC (debug_dataflow);
76 #define GST_CAT_DEFAULT GST_CAT_PADS
77
78 /* Pad signals and args */
79 enum
80 {
81   PAD_LINKED,
82   PAD_UNLINKED,
83   PAD_REQUEST_LINK,
84   PAD_HAVE_DATA,
85   /* FILL ME */
86   LAST_SIGNAL
87 };
88
89 enum
90 {
91   PAD_PROP_0,
92   PAD_PROP_CAPS,
93   PAD_PROP_DIRECTION,
94   PAD_PROP_TEMPLATE,
95   /* FILL ME */
96 };
97
98 static void gst_pad_dispose (GObject * object);
99 static void gst_pad_finalize (GObject * object);
100 static void gst_pad_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec);
102 static void gst_pad_get_property (GObject * object, guint prop_id,
103     GValue * value, GParamSpec * pspec);
104
105 static GstFlowReturn handle_pad_block (GstPad * pad);
106 static GstCaps *gst_pad_get_caps_unlocked (GstPad * pad);
107 static void gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ);
108 static gboolean gst_pad_activate_default (GstPad * pad);
109 static gboolean gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps);
110
111 #ifndef GST_DISABLE_LOADSAVE
112 static xmlNodePtr gst_pad_save_thyself (GstObject * object, xmlNodePtr parent);
113 #endif
114
115 static GstObjectClass *parent_class = NULL;
116 static guint gst_pad_signals[LAST_SIGNAL] = { 0 };
117
118 /* quarks for probe signals */
119 static GQuark buffer_quark;
120 static GQuark event_quark;
121
122 typedef struct
123 {
124   const gint ret;
125   const gchar *name;
126   GQuark quark;
127 } GstFlowQuarks;
128
129 static GstFlowQuarks flow_quarks[] = {
130   {GST_FLOW_CUSTOM_SUCCESS, "custom-success", 0},
131   {GST_FLOW_RESEND, "resend", 0},
132   {GST_FLOW_OK, "ok", 0},
133   {GST_FLOW_NOT_LINKED, "not-linked", 0},
134   {GST_FLOW_WRONG_STATE, "wrong-state", 0},
135   {GST_FLOW_UNEXPECTED, "unexpected", 0},
136   {GST_FLOW_NOT_NEGOTIATED, "not-negotiated", 0},
137   {GST_FLOW_ERROR, "error", 0},
138   {GST_FLOW_NOT_SUPPORTED, "not-supported", 0},
139   {GST_FLOW_CUSTOM_ERROR, "custom-error", 0},
140
141   {0, NULL, 0}
142 };
143
144 /**
145  * gst_flow_get_name:
146  * @ret: a #GstFlowReturn to get the name of.
147  *
148  * Gets a string representing the given flow return.
149  *
150  * Returns: a static string with the name of the flow return.
151  */
152 G_CONST_RETURN gchar *
153 gst_flow_get_name (GstFlowReturn ret)
154 {
155   gint i;
156
157   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
158
159   for (i = 0; flow_quarks[i].name; i++) {
160     if (ret == flow_quarks[i].ret)
161       return flow_quarks[i].name;
162   }
163   return "unknown";
164 }
165
166 /**
167  * gst_flow_to_quark:
168  * @ret: a #GstFlowReturn to get the quark of.
169  *
170  * Get the unique quark for the given GstFlowReturn.
171  *
172  * Returns: the quark associated with the flow return or 0 if an
173  * invalid return was specified.
174  */
175 GQuark
176 gst_flow_to_quark (GstFlowReturn ret)
177 {
178   gint i;
179
180   ret = CLAMP (ret, GST_FLOW_CUSTOM_ERROR, GST_FLOW_CUSTOM_SUCCESS);
181
182   for (i = 0; flow_quarks[i].name; i++) {
183     if (ret == flow_quarks[i].ret)
184       return flow_quarks[i].quark;
185   }
186   return 0;
187 }
188
189 #define _do_init \
190 { \
191   gint i; \
192   \
193   buffer_quark = g_quark_from_static_string ("buffer"); \
194   event_quark = g_quark_from_static_string ("event"); \
195   \
196   for (i = 0; flow_quarks[i].name; i++) { \
197     flow_quarks[i].quark = g_quark_from_static_string (flow_quarks[i].name); \
198   } \
199   \
200   GST_DEBUG_CATEGORY_INIT (debug_dataflow, "GST_DATAFLOW", \
201       GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "dataflow inside pads"); \
202 }
203
204 G_DEFINE_TYPE_WITH_CODE (GstPad, gst_pad, GST_TYPE_OBJECT, _do_init);
205
206 static gboolean
207 _gst_do_pass_data_accumulator (GSignalInvocationHint * ihint,
208     GValue * return_accu, const GValue * handler_return, gpointer dummy)
209 {
210   gboolean ret = g_value_get_boolean (handler_return);
211
212   GST_DEBUG ("accumulated %d", ret);
213   g_value_set_boolean (return_accu, ret);
214
215   return ret;
216 }
217
218 static gboolean
219 default_have_data (GstPad * pad, GstMiniObject * o)
220 {
221   return TRUE;
222 }
223
224 static void
225 gst_pad_class_init (GstPadClass * klass)
226 {
227   GObjectClass *gobject_class;
228   GstObjectClass *gstobject_class;
229
230   gobject_class = G_OBJECT_CLASS (klass);
231   gstobject_class = GST_OBJECT_CLASS (klass);
232
233   parent_class = g_type_class_peek_parent (klass);
234
235   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pad_dispose);
236   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pad_finalize);
237   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pad_set_property);
238   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pad_get_property);
239
240   /**
241    * GstPad::linked:
242    * @pad: the pad that emitted the signal
243    * @peer: the peer pad that has been connected
244    *
245    * Signals that a pad has been linked to the peer pad.
246    */
247   gst_pad_signals[PAD_LINKED] =
248       g_signal_new ("linked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
249       G_STRUCT_OFFSET (GstPadClass, linked), NULL, NULL,
250       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
251   /**
252    * GstPad::unlinked:
253    * @pad: the pad that emitted the signal
254    * @peer: the peer pad that has been disconnected
255    *
256    * Signals that a pad has been unlinked from the peer pad.
257    */
258   gst_pad_signals[PAD_UNLINKED] =
259       g_signal_new ("unlinked", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
260       G_STRUCT_OFFSET (GstPadClass, unlinked), NULL, NULL,
261       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_PAD);
262   /**
263    * GstPad::request-link:
264    * @pad: the pad that emitted the signal
265    * @peer: the peer pad for which a connection is requested
266    *
267    * Signals that a pad connection has been requested.
268    */
269   gst_pad_signals[PAD_REQUEST_LINK] =
270       g_signal_new ("request-link", G_TYPE_FROM_CLASS (klass),
271       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstPadClass, request_link), NULL,
272       NULL, gst_marshal_VOID__OBJECT, G_TYPE_NONE, 0);
273
274   /**
275    * GstPad::have-data:
276    * @pad: the pad that emitted the signal
277    * @mini_obj: new data
278    *
279    * Signals that new data is available on the pad. This signal is used
280    * internally for implementing pad probes.
281    * See gst_pad_add_*_probe functions.
282    *
283    * Returns: %TRUE to keep the data, %FALSE to drop it
284    */
285   gst_pad_signals[PAD_HAVE_DATA] =
286       g_signal_new ("have-data", G_TYPE_FROM_CLASS (klass),
287       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
288       G_STRUCT_OFFSET (GstPadClass, have_data),
289       _gst_do_pass_data_accumulator,
290       NULL, gst_marshal_BOOLEAN__POINTER, G_TYPE_BOOLEAN, 1,
291       GST_TYPE_MINI_OBJECT);
292
293   g_object_class_install_property (gobject_class, PAD_PROP_CAPS,
294       g_param_spec_boxed ("caps", "Caps", "The capabilities of the pad",
295           GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
296   g_object_class_install_property (gobject_class, PAD_PROP_DIRECTION,
297       g_param_spec_enum ("direction", "Direction", "The direction of the pad",
298           GST_TYPE_PAD_DIRECTION, GST_PAD_UNKNOWN,
299           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
300   /* FIXME, Make G_PARAM_CONSTRUCT_ONLY when we fix ghostpads. */
301   g_object_class_install_property (gobject_class, PAD_PROP_TEMPLATE,
302       g_param_spec_object ("template", "Template",
303           "The GstPadTemplate of this pad", GST_TYPE_PAD_TEMPLATE,
304           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305
306 #ifndef GST_DISABLE_LOADSAVE
307   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_pad_save_thyself);
308 #endif
309   gstobject_class->path_string_separator = ".";
310
311   klass->have_data = default_have_data;
312 }
313
314 static void
315 gst_pad_init (GstPad * pad)
316 {
317   GST_PAD_DIRECTION (pad) = GST_PAD_UNKNOWN;
318   GST_PAD_PEER (pad) = NULL;
319
320   GST_PAD_CHAINFUNC (pad) = NULL;
321
322   GST_PAD_LINKFUNC (pad) = NULL;
323
324   GST_PAD_CAPS (pad) = NULL;
325   GST_PAD_GETCAPSFUNC (pad) = NULL;
326
327   GST_PAD_ACTIVATEFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_activate_default);
328   GST_PAD_EVENTFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_event_default);
329   GST_PAD_QUERYTYPEFUNC (pad) =
330       GST_DEBUG_FUNCPTR (gst_pad_get_query_types_default);
331   GST_PAD_QUERYFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_query_default);
332   GST_PAD_INTLINKFUNC (pad) =
333       GST_DEBUG_FUNCPTR (gst_pad_get_internal_links_default);
334   GST_PAD_ITERINTLINKFUNC (pad) =
335       GST_DEBUG_FUNCPTR (gst_pad_iterate_internal_links_default);
336
337   GST_PAD_ACCEPTCAPSFUNC (pad) = GST_DEBUG_FUNCPTR (gst_pad_acceptcaps_default);
338
339   pad->do_buffer_signals = 0;
340   pad->do_event_signals = 0;
341
342   GST_PAD_SET_FLUSHING (pad);
343
344   pad->preroll_lock = g_mutex_new ();
345   pad->preroll_cond = g_cond_new ();
346
347   pad->stream_rec_lock = g_new (GStaticRecMutex, 1);
348   g_static_rec_mutex_init (pad->stream_rec_lock);
349
350   pad->block_cond = g_cond_new ();
351 }
352
353 static void
354 gst_pad_dispose (GObject * object)
355 {
356   GstPad *pad = GST_PAD (object);
357   GstPad *peer;
358
359   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, pad, "dispose");
360
361   /* unlink the peer pad */
362   if ((peer = gst_pad_get_peer (pad))) {
363     /* window for MT unsafeness, someone else could unlink here
364      * and then we call unlink with wrong pads. The unlink
365      * function would catch this and safely return failed. */
366     if (GST_PAD_IS_SRC (pad))
367       gst_pad_unlink (pad, peer);
368     else
369       gst_pad_unlink (peer, pad);
370
371     gst_object_unref (peer);
372   }
373
374   /* clear the caps */
375   gst_caps_replace (&GST_PAD_CAPS (pad), NULL);
376
377   gst_pad_set_pad_template (pad, NULL);
378
379   if (pad->block_destroy_data && pad->block_data) {
380     pad->block_destroy_data (pad->block_data);
381     pad->block_data = NULL;
382   }
383
384   G_OBJECT_CLASS (parent_class)->dispose (object);
385 }
386
387 static void
388 gst_pad_finalize (GObject * object)
389 {
390   GstPad *pad = GST_PAD (object);
391   GstTask *task;
392
393   /* in case the task is still around, clean it up */
394   if ((task = GST_PAD_TASK (pad))) {
395     gst_task_join (task);
396     GST_PAD_TASK (pad) = NULL;
397     gst_object_unref (task);
398   }
399
400   if (pad->stream_rec_lock) {
401     g_static_rec_mutex_free (pad->stream_rec_lock);
402     g_free (pad->stream_rec_lock);
403     pad->stream_rec_lock = NULL;
404   }
405   if (pad->preroll_lock) {
406     g_mutex_free (pad->preroll_lock);
407     g_cond_free (pad->preroll_cond);
408     pad->preroll_lock = NULL;
409     pad->preroll_cond = NULL;
410   }
411   if (pad->block_cond) {
412     g_cond_free (pad->block_cond);
413     pad->block_cond = NULL;
414   }
415
416   G_OBJECT_CLASS (parent_class)->finalize (object);
417 }
418
419 static void
420 gst_pad_set_property (GObject * object, guint prop_id,
421     const GValue * value, GParamSpec * pspec)
422 {
423   g_return_if_fail (GST_IS_PAD (object));
424
425   switch (prop_id) {
426     case PAD_PROP_DIRECTION:
427       GST_PAD_DIRECTION (object) = g_value_get_enum (value);
428       break;
429     case PAD_PROP_TEMPLATE:
430       gst_pad_set_pad_template (GST_PAD_CAST (object),
431           (GstPadTemplate *) g_value_get_object (value));
432       break;
433     default:
434       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
435       break;
436   }
437 }
438
439 static void
440 gst_pad_get_property (GObject * object, guint prop_id,
441     GValue * value, GParamSpec * pspec)
442 {
443   g_return_if_fail (GST_IS_PAD (object));
444
445   switch (prop_id) {
446     case PAD_PROP_CAPS:
447       GST_OBJECT_LOCK (object);
448       g_value_set_boxed (value, GST_PAD_CAPS (object));
449       GST_OBJECT_UNLOCK (object);
450       break;
451     case PAD_PROP_DIRECTION:
452       g_value_set_enum (value, GST_PAD_DIRECTION (object));
453       break;
454     case PAD_PROP_TEMPLATE:
455       g_value_set_object (value, GST_PAD_PAD_TEMPLATE (object));
456       break;
457     default:
458       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459       break;
460   }
461 }
462
463 /**
464  * gst_pad_new:
465  * @name: the name of the new pad.
466  * @direction: the #GstPadDirection of the pad.
467  *
468  * Creates a new pad with the given name in the given direction.
469  * If name is NULL, a guaranteed unique name (across all pads)
470  * will be assigned.
471  * This function makes a copy of the name so you can safely free the name.
472  *
473  * Returns: a new #GstPad, or NULL in case of an error.
474  *
475  * MT safe.
476  */
477 GstPad *
478 gst_pad_new (const gchar * name, GstPadDirection direction)
479 {
480   return g_object_new (GST_TYPE_PAD,
481       "name", name, "direction", direction, NULL);
482 }
483
484 /**
485  * gst_pad_new_from_template:
486  * @templ: the pad template to use
487  * @name: the name of the element
488  *
489  * Creates a new pad with the given name from the given template.
490  * If name is NULL, a guaranteed unique name (across all pads)
491  * will be assigned.
492  * This function makes a copy of the name so you can safely free the name.
493  *
494  * Returns: a new #GstPad, or NULL in case of an error.
495  */
496 GstPad *
497 gst_pad_new_from_template (GstPadTemplate * templ, const gchar * name)
498 {
499   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
500
501   return g_object_new (GST_TYPE_PAD,
502       "name", name, "direction", templ->direction, "template", templ, NULL);
503 }
504
505 /**
506  * gst_pad_new_from_static_template:
507  * @templ: the #GstStaticPadTemplate to use
508  * @name: the name of the element
509  *
510  * Creates a new pad with the given name from the given static template.
511  * If name is NULL, a guaranteed unique name (across all pads)
512  * will be assigned.
513  * This function makes a copy of the name so you can safely free the name.
514  *
515  * Returns: a new #GstPad, or NULL in case of an error.
516  */
517 GstPad *
518 gst_pad_new_from_static_template (GstStaticPadTemplate * templ,
519     const gchar * name)
520 {
521   GstPad *pad;
522   GstPadTemplate *template;
523
524   template = gst_static_pad_template_get (templ);
525   pad = gst_pad_new_from_template (template, name);
526   gst_object_unref (template);
527   return pad;
528 }
529
530 /**
531  * gst_pad_get_direction:
532  * @pad: a #GstPad to get the direction of.
533  *
534  * Gets the direction of the pad. The direction of the pad is
535  * decided at construction time so this function does not take
536  * the LOCK.
537  *
538  * Returns: the #GstPadDirection of the pad.
539  *
540  * MT safe.
541  */
542 GstPadDirection
543 gst_pad_get_direction (GstPad * pad)
544 {
545   GstPadDirection result;
546
547   /* PAD_UNKNOWN is a little silly but we need some sort of
548    * error return value */
549   g_return_val_if_fail (GST_IS_PAD (pad), GST_PAD_UNKNOWN);
550
551   result = GST_PAD_DIRECTION (pad);
552
553   return result;
554 }
555
556 static gboolean
557 gst_pad_activate_default (GstPad * pad)
558 {
559   return gst_pad_activate_push (pad, TRUE);
560 }
561
562 static void
563 pre_activate (GstPad * pad, GstActivateMode new_mode)
564 {
565   switch (new_mode) {
566     case GST_ACTIVATE_PUSH:
567     case GST_ACTIVATE_PULL:
568       GST_OBJECT_LOCK (pad);
569       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE %d, unset flushing",
570           new_mode);
571       GST_PAD_UNSET_FLUSHING (pad);
572       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
573       GST_OBJECT_UNLOCK (pad);
574       break;
575     case GST_ACTIVATE_NONE:
576       GST_OBJECT_LOCK (pad);
577       GST_DEBUG_OBJECT (pad, "setting ACTIVATE_MODE NONE, set flushing");
578       GST_PAD_SET_FLUSHING (pad);
579       GST_PAD_ACTIVATE_MODE (pad) = new_mode;
580       /* unlock blocked pads so element can resume and stop */
581       GST_PAD_BLOCK_BROADCAST (pad);
582       GST_OBJECT_UNLOCK (pad);
583       break;
584   }
585 }
586
587 static void
588 post_activate (GstPad * pad, GstActivateMode new_mode)
589 {
590   switch (new_mode) {
591     case GST_ACTIVATE_PUSH:
592     case GST_ACTIVATE_PULL:
593       /* nop */
594       break;
595     case GST_ACTIVATE_NONE:
596       /* ensures that streaming stops */
597       GST_PAD_STREAM_LOCK (pad);
598       GST_DEBUG_OBJECT (pad, "stopped streaming");
599       GST_PAD_STREAM_UNLOCK (pad);
600       break;
601   }
602 }
603
604 /**
605  * gst_pad_set_active:
606  * @pad: the #GstPad to activate or deactivate.
607  * @active: whether or not the pad should be active.
608  *
609  * Activates or deactivates the given pad.
610  * Normally called from within core state change functions.
611  *
612  * If @active, makes sure the pad is active. If it is already active, either in
613  * push or pull mode, just return. Otherwise dispatches to the pad's activate
614  * function to perform the actual activation.
615  *
616  * If not @active, checks the pad's current mode and calls
617  * gst_pad_activate_push() or gst_pad_activate_pull(), as appropriate, with a
618  * FALSE argument.
619  *
620  * Returns: #TRUE if the operation was successful.
621  *
622  * MT safe.
623  */
624 gboolean
625 gst_pad_set_active (GstPad * pad, gboolean active)
626 {
627   GstActivateMode old;
628   gboolean ret = FALSE;
629
630   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
631
632   GST_OBJECT_LOCK (pad);
633   old = GST_PAD_ACTIVATE_MODE (pad);
634   GST_OBJECT_UNLOCK (pad);
635
636   if (active) {
637     switch (old) {
638       case GST_ACTIVATE_PUSH:
639         GST_DEBUG_OBJECT (pad, "activating pad from push");
640         ret = TRUE;
641         break;
642       case GST_ACTIVATE_PULL:
643         GST_DEBUG_OBJECT (pad, "activating pad from pull");
644         ret = TRUE;
645         break;
646       case GST_ACTIVATE_NONE:
647         GST_DEBUG_OBJECT (pad, "activating pad from none");
648         ret = (GST_PAD_ACTIVATEFUNC (pad)) (pad);
649         break;
650     }
651   } else {
652     switch (old) {
653       case GST_ACTIVATE_PUSH:
654         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
655         ret = gst_pad_activate_push (pad, FALSE);
656         break;
657       case GST_ACTIVATE_PULL:
658         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
659         ret = gst_pad_activate_pull (pad, FALSE);
660         break;
661       case GST_ACTIVATE_NONE:
662         GST_DEBUG_OBJECT (pad, "deactivating pad from none");
663         ret = TRUE;
664         break;
665     }
666   }
667
668   if (!ret) {
669     GST_OBJECT_LOCK (pad);
670     if (!active) {
671       g_critical ("Failed to deactivate pad %s:%s, very bad",
672           GST_DEBUG_PAD_NAME (pad));
673     } else {
674       GST_WARNING_OBJECT (pad, "Failed to activate pad");
675     }
676     GST_OBJECT_UNLOCK (pad);
677   }
678
679   return ret;
680 }
681
682 /**
683  * gst_pad_activate_pull:
684  * @pad: the #GstPad to activate or deactivate.
685  * @active: whether or not the pad should be active.
686  *
687  * Activates or deactivates the given pad in pull mode via dispatching to the
688  * pad's activatepullfunc. For use from within pad activation functions only.
689  * When called on sink pads, will first proxy the call to the peer pad, which
690  * is expected to activate its internally linked pads from within its
691  * activate_pull function.
692  *
693  * If you don't know what this is, you probably don't want to call it.
694  *
695  * Returns: TRUE if the operation was successful.
696  *
697  * MT safe.
698  */
699 gboolean
700 gst_pad_activate_pull (GstPad * pad, gboolean active)
701 {
702   GstActivateMode old, new;
703   GstPad *peer;
704
705   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
706
707   GST_OBJECT_LOCK (pad);
708   old = GST_PAD_ACTIVATE_MODE (pad);
709   GST_OBJECT_UNLOCK (pad);
710
711   if (active) {
712     switch (old) {
713       case GST_ACTIVATE_PULL:
714         GST_DEBUG_OBJECT (pad, "activating pad from pull, was ok");
715         goto was_ok;
716       case GST_ACTIVATE_PUSH:
717         GST_DEBUG_OBJECT (pad,
718             "activating pad from push, deactivate push first");
719         /* pad was activate in the wrong direction, deactivate it
720          * and reactivate it in pull mode */
721         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
722           goto deactivate_failed;
723         /* fallthrough, pad is deactivated now. */
724       case GST_ACTIVATE_NONE:
725         GST_DEBUG_OBJECT (pad, "activating pad from none");
726         break;
727     }
728   } else {
729     switch (old) {
730       case GST_ACTIVATE_NONE:
731         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
732         goto was_ok;
733       case GST_ACTIVATE_PUSH:
734         GST_DEBUG_OBJECT (pad, "deactivating pad from push, weird");
735         /* pad was activated in the other direction, deactivate it
736          * in push mode, this should not happen... */
737         if (G_UNLIKELY (!gst_pad_activate_push (pad, FALSE)))
738           goto deactivate_failed;
739         /* everything is fine now */
740         goto was_ok;
741       case GST_ACTIVATE_PULL:
742         GST_DEBUG_OBJECT (pad, "deactivating pad from pull");
743         break;
744     }
745   }
746
747   if (gst_pad_get_direction (pad) == GST_PAD_SINK) {
748     if ((peer = gst_pad_get_peer (pad))) {
749       GST_DEBUG_OBJECT (pad, "calling peer");
750       if (G_UNLIKELY (!gst_pad_activate_pull (peer, active)))
751         goto peer_failed;
752       gst_object_unref (peer);
753     } else {
754       /* there is no peer, this is only fatal when we activate. When we
755        * deactivate, we must assume the application has unlinked the peer and
756        * will deactivate it eventually. */
757       if (active)
758         goto not_linked;
759       else
760         GST_DEBUG_OBJECT (pad, "deactivating unlinked pad");
761     }
762   } else {
763     if (G_UNLIKELY (GST_PAD_GETRANGEFUNC (pad) == NULL))
764       goto failure;             /* Can't activate pull on a src without a
765                                    getrange function */
766   }
767
768   new = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
769   pre_activate (pad, new);
770
771   if (GST_PAD_ACTIVATEPULLFUNC (pad)) {
772     if (G_UNLIKELY (!GST_PAD_ACTIVATEPULLFUNC (pad) (pad, active)))
773       goto failure;
774   } else {
775     /* can happen for sinks of passthrough elements */
776   }
777
778   post_activate (pad, new);
779
780   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in pull mode",
781       active ? "activated" : "deactivated");
782
783   return TRUE;
784
785 was_ok:
786   {
787     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in pull mode",
788         active ? "activated" : "deactivated");
789     return TRUE;
790   }
791 deactivate_failed:
792   {
793     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
794         "failed to %s in switch to pull from mode %d",
795         (active ? "activate" : "deactivate"), old);
796     return FALSE;
797   }
798 peer_failed:
799   {
800     GST_OBJECT_LOCK (peer);
801     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
802         "activate_pull on peer (%s:%s) failed", GST_DEBUG_PAD_NAME (peer));
803     GST_OBJECT_UNLOCK (peer);
804     gst_object_unref (peer);
805     return FALSE;
806   }
807 not_linked:
808   {
809     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "can't activate unlinked sink "
810         "pad in pull mode");
811     return FALSE;
812   }
813 failure:
814   {
815     GST_OBJECT_LOCK (pad);
816     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in pull mode",
817         active ? "activate" : "deactivate");
818     GST_PAD_SET_FLUSHING (pad);
819     GST_PAD_ACTIVATE_MODE (pad) = old;
820     GST_OBJECT_UNLOCK (pad);
821     return FALSE;
822   }
823 }
824
825 /**
826  * gst_pad_activate_push:
827  * @pad: the #GstPad to activate or deactivate.
828  * @active: whether the pad should be active or not.
829  *
830  * Activates or deactivates the given pad in push mode via dispatching to the
831  * pad's activatepushfunc. For use from within pad activation functions only.
832  *
833  * If you don't know what this is, you probably don't want to call it.
834  *
835  * Returns: %TRUE if the operation was successful.
836  *
837  * MT safe.
838  */
839 gboolean
840 gst_pad_activate_push (GstPad * pad, gboolean active)
841 {
842   GstActivateMode old, new;
843
844   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
845   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "trying to set %s in push mode",
846       active ? "activated" : "deactivated");
847
848   GST_OBJECT_LOCK (pad);
849   old = GST_PAD_ACTIVATE_MODE (pad);
850   GST_OBJECT_UNLOCK (pad);
851
852   if (active) {
853     switch (old) {
854       case GST_ACTIVATE_PUSH:
855         GST_DEBUG_OBJECT (pad, "activating pad from push, was ok");
856         goto was_ok;
857       case GST_ACTIVATE_PULL:
858         GST_DEBUG_OBJECT (pad,
859             "activating pad from push, deactivating pull first");
860         /* pad was activate in the wrong direction, deactivate it
861          * an reactivate it in push mode */
862         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
863           goto deactivate_failed;
864         /* fallthrough, pad is deactivated now. */
865       case GST_ACTIVATE_NONE:
866         GST_DEBUG_OBJECT (pad, "activating pad from none");
867         break;
868     }
869   } else {
870     switch (old) {
871       case GST_ACTIVATE_NONE:
872         GST_DEBUG_OBJECT (pad, "deactivating pad from none, was ok");
873         goto was_ok;
874       case GST_ACTIVATE_PULL:
875         GST_DEBUG_OBJECT (pad, "deactivating pad from pull, weird");
876         /* pad was activated in the other direction, deactivate it
877          * in pull mode, this should not happen... */
878         if (G_UNLIKELY (!gst_pad_activate_pull (pad, FALSE)))
879           goto deactivate_failed;
880         /* everything is fine now */
881         goto was_ok;
882       case GST_ACTIVATE_PUSH:
883         GST_DEBUG_OBJECT (pad, "deactivating pad from push");
884         break;
885     }
886   }
887
888   new = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
889   pre_activate (pad, new);
890
891   if (GST_PAD_ACTIVATEPUSHFUNC (pad)) {
892     if (G_UNLIKELY (!GST_PAD_ACTIVATEPUSHFUNC (pad) (pad, active))) {
893       goto failure;
894     }
895   } else {
896     /* quite ok, element relies on state change func to prepare itself */
897   }
898
899   post_activate (pad, new);
900
901   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "%s in push mode",
902       active ? "activated" : "deactivated");
903   return TRUE;
904
905 was_ok:
906   {
907     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "already %s in push mode",
908         active ? "activated" : "deactivated");
909     return TRUE;
910   }
911 deactivate_failed:
912   {
913     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
914         "failed to %s in switch to push from mode %d",
915         (active ? "activate" : "deactivate"), old);
916     return FALSE;
917   }
918 failure:
919   {
920     GST_OBJECT_LOCK (pad);
921     GST_CAT_INFO_OBJECT (GST_CAT_PADS, pad, "failed to %s in push mode",
922         active ? "activate" : "deactivate");
923     GST_PAD_SET_FLUSHING (pad);
924     GST_PAD_ACTIVATE_MODE (pad) = old;
925     GST_OBJECT_UNLOCK (pad);
926     return FALSE;
927   }
928 }
929
930 /**
931  * gst_pad_is_active:
932  * @pad: the #GstPad to query
933  *
934  * Query if a pad is active
935  *
936  * Returns: TRUE if the pad is active.
937  *
938  * MT safe.
939  */
940 gboolean
941 gst_pad_is_active (GstPad * pad)
942 {
943   gboolean result = FALSE;
944
945   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
946
947   GST_OBJECT_LOCK (pad);
948   result = GST_PAD_MODE_ACTIVATE (GST_PAD_ACTIVATE_MODE (pad));
949   GST_OBJECT_UNLOCK (pad);
950
951   return result;
952 }
953
954 /**
955  * gst_pad_set_blocked_async_full:
956  * @pad: the #GstPad to block or unblock
957  * @blocked: boolean indicating whether the pad should be blocked or unblocked
958  * @callback: #GstPadBlockCallback that will be called when the
959  *            operation succeeds
960  * @user_data: user data passed to the callback
961  * @destroy_data: #GDestroyNotify for user_data
962  *
963  * Blocks or unblocks the dataflow on a pad. The provided callback
964  * is called when the operation succeeds; this happens right before the next
965  * attempt at pushing a buffer on the pad.
966  *
967  * This can take a while as the pad can only become blocked when real dataflow
968  * is happening.
969  * When the pipeline is stalled, for example in PAUSED, this can
970  * take an indeterminate amount of time.
971  * You can pass NULL as the callback to make this call block. Be careful with
972  * this blocking call as it might not return for reasons stated above.
973  *
974  * Returns: TRUE if the pad could be blocked. This function can fail if the
975  * wrong parameters were passed or the pad was already in the requested state.
976  *
977  * MT safe.
978  *
979  * Since: 0.10.23
980  */
981 gboolean
982 gst_pad_set_blocked_async_full (GstPad * pad, gboolean blocked,
983     GstPadBlockCallback callback, gpointer user_data,
984     GDestroyNotify destroy_data)
985 {
986   gboolean was_blocked = FALSE;
987
988   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
989
990   GST_OBJECT_LOCK (pad);
991
992   was_blocked = GST_PAD_IS_BLOCKED (pad);
993
994   if (G_UNLIKELY (was_blocked == blocked))
995     goto had_right_state;
996
997   if (blocked) {
998     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocking pad");
999
1000     GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKED);
1001
1002     if (pad->block_destroy_data && pad->block_data &&
1003         pad->block_data != user_data)
1004       pad->block_destroy_data (pad->block_data);
1005
1006     pad->block_callback = callback;
1007     pad->block_data = user_data;
1008     pad->block_destroy_data = destroy_data;
1009     pad->abidata.ABI.block_callback_called = FALSE;
1010     if (!callback) {
1011       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for block");
1012       GST_PAD_BLOCK_WAIT (pad);
1013       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "blocked");
1014     }
1015   } else {
1016     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocking pad");
1017
1018     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKED);
1019
1020     if (pad->block_destroy_data && pad->block_data &&
1021         pad->block_data != user_data)
1022       pad->block_destroy_data (pad->block_data);
1023
1024     pad->block_callback = callback;
1025     pad->block_data = user_data;
1026     pad->block_destroy_data = destroy_data;
1027     pad->abidata.ABI.block_callback_called = FALSE;
1028
1029     GST_PAD_BLOCK_BROADCAST (pad);
1030     if (!callback) {
1031       /* no callback, wait for the unblock to happen */
1032       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "waiting for unblock");
1033       GST_PAD_BLOCK_WAIT (pad);
1034       GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "unblocked");
1035     }
1036   }
1037   GST_OBJECT_UNLOCK (pad);
1038
1039   return TRUE;
1040
1041 had_right_state:
1042   {
1043     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
1044         "pad was in right state (%d)", was_blocked);
1045     GST_OBJECT_UNLOCK (pad);
1046
1047     return FALSE;
1048   }
1049 }
1050
1051 /**
1052  * gst_pad_set_blocked_async:
1053  * @pad: the #GstPad to block or unblock
1054  * @blocked: boolean indicating whether the pad should be blocked or unblocked
1055  * @callback: #GstPadBlockCallback that will be called when the
1056  *            operation succeeds
1057  * @user_data: user data passed to the callback
1058  *
1059  * Blocks or unblocks the dataflow on a pad. The provided callback
1060  * is called when the operation succeeds; this happens right before the next
1061  * attempt at pushing a buffer on the pad.
1062  *
1063  * This can take a while as the pad can only become blocked when real dataflow
1064  * is happening.
1065  * When the pipeline is stalled, for example in PAUSED, this can
1066  * take an indeterminate amount of time.
1067  * You can pass NULL as the callback to make this call block. Be careful with
1068  * this blocking call as it might not return for reasons stated above.
1069  *
1070  * Returns: TRUE if the pad could be blocked. This function can fail if the
1071  * wrong parameters were passed or the pad was already in the requested state.
1072  *
1073  * MT safe.
1074  */
1075 gboolean
1076 gst_pad_set_blocked_async (GstPad * pad, gboolean blocked,
1077     GstPadBlockCallback callback, gpointer user_data)
1078 {
1079   return gst_pad_set_blocked_async_full (pad, blocked,
1080       callback, user_data, NULL);
1081 }
1082
1083 /**
1084  * gst_pad_set_blocked:
1085  * @pad: the #GstPad to block or unblock
1086  * @blocked: boolean indicating we should block or unblock
1087  *
1088  * Blocks or unblocks the dataflow on a pad. This function is
1089  * a shortcut for gst_pad_set_blocked_async() with a NULL
1090  * callback.
1091  *
1092  * Returns: TRUE if the pad could be blocked. This function can fail if the
1093  * wrong parameters were passed or the pad was already in the requested state.
1094  *
1095  * MT safe.
1096  */
1097 gboolean
1098 gst_pad_set_blocked (GstPad * pad, gboolean blocked)
1099 {
1100   return gst_pad_set_blocked_async (pad, blocked, NULL, NULL);
1101 }
1102
1103 /**
1104  * gst_pad_is_blocked:
1105  * @pad: the #GstPad to query
1106  *
1107  * Checks if the pad is blocked or not. This function returns the
1108  * last requested state of the pad. It is not certain that the pad
1109  * is actually blocking at this point (see gst_pad_is_blocking()).
1110  *
1111  * Returns: TRUE if the pad is blocked.
1112  *
1113  * MT safe.
1114  */
1115 gboolean
1116 gst_pad_is_blocked (GstPad * pad)
1117 {
1118   gboolean result = FALSE;
1119
1120   g_return_val_if_fail (GST_IS_PAD (pad), result);
1121
1122   GST_OBJECT_LOCK (pad);
1123   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKED);
1124   GST_OBJECT_UNLOCK (pad);
1125
1126   return result;
1127 }
1128
1129 /**
1130  * gst_pad_is_blocking:
1131  * @pad: the #GstPad to query
1132  *
1133  * Checks if the pad is blocking or not. This is a guaranteed state
1134  * of whether the pad is actually blocking on a #GstBuffer or a #GstEvent.
1135  *
1136  * Returns: TRUE if the pad is blocking.
1137  *
1138  * MT safe.
1139  *
1140  * Since: 0.10.11
1141  */
1142 gboolean
1143 gst_pad_is_blocking (GstPad * pad)
1144 {
1145   gboolean result = FALSE;
1146
1147   g_return_val_if_fail (GST_IS_PAD (pad), result);
1148
1149   GST_OBJECT_LOCK (pad);
1150   /* the blocking flag is only valid if the pad is not flushing */
1151   result = GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_BLOCKING) &&
1152       !GST_OBJECT_FLAG_IS_SET (pad, GST_PAD_FLUSHING);
1153   GST_OBJECT_UNLOCK (pad);
1154
1155   return result;
1156 }
1157
1158 /**
1159  * gst_pad_set_activate_function:
1160  * @pad: a #GstPad.
1161  * @activate: the #GstPadActivateFunction to set.
1162  *
1163  * Sets the given activate function for @pad. The activate function will
1164  * dispatch to gst_pad_activate_push() or gst_pad_activate_pull() to perform
1165  * the actual activation. Only makes sense to set on sink pads.
1166  *
1167  * Call this function if your sink pad can start a pull-based task.
1168  */
1169 void
1170 gst_pad_set_activate_function (GstPad * pad, GstPadActivateFunction activate)
1171 {
1172   g_return_if_fail (GST_IS_PAD (pad));
1173
1174   GST_PAD_ACTIVATEFUNC (pad) = activate;
1175   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatefunc set to %s",
1176       GST_DEBUG_FUNCPTR_NAME (activate));
1177 }
1178
1179 /**
1180  * gst_pad_set_activatepull_function:
1181  * @pad: a #GstPad.
1182  * @activatepull: the #GstPadActivateModeFunction to set.
1183  *
1184  * Sets the given activate_pull function for the pad. An activate_pull function
1185  * prepares the element and any upstream connections for pulling. See XXX
1186  * part-activation.txt for details.
1187  */
1188 void
1189 gst_pad_set_activatepull_function (GstPad * pad,
1190     GstPadActivateModeFunction activatepull)
1191 {
1192   g_return_if_fail (GST_IS_PAD (pad));
1193
1194   GST_PAD_ACTIVATEPULLFUNC (pad) = activatepull;
1195   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepullfunc set to %s",
1196       GST_DEBUG_FUNCPTR_NAME (activatepull));
1197 }
1198
1199 /**
1200  * gst_pad_set_activatepush_function:
1201  * @pad: a #GstPad.
1202  * @activatepush: the #GstPadActivateModeFunction to set.
1203  *
1204  * Sets the given activate_push function for the pad. An activate_push function
1205  * prepares the element for pushing. See XXX part-activation.txt for details.
1206  */
1207 void
1208 gst_pad_set_activatepush_function (GstPad * pad,
1209     GstPadActivateModeFunction activatepush)
1210 {
1211   g_return_if_fail (GST_IS_PAD (pad));
1212
1213   GST_PAD_ACTIVATEPUSHFUNC (pad) = activatepush;
1214   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "activatepushfunc set to %s",
1215       GST_DEBUG_FUNCPTR_NAME (activatepush));
1216 }
1217
1218 /**
1219  * gst_pad_set_chain_function:
1220  * @pad: a sink #GstPad.
1221  * @chain: the #GstPadChainFunction to set.
1222  *
1223  * Sets the given chain function for the pad. The chain function is called to
1224  * process a #GstBuffer input buffer. see #GstPadChainFunction for more details.
1225  */
1226 void
1227 gst_pad_set_chain_function (GstPad * pad, GstPadChainFunction chain)
1228 {
1229   g_return_if_fail (GST_IS_PAD (pad));
1230   g_return_if_fail (GST_PAD_IS_SINK (pad));
1231
1232   GST_PAD_CHAINFUNC (pad) = chain;
1233   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainfunc set to %s",
1234       GST_DEBUG_FUNCPTR_NAME (chain));
1235 }
1236
1237 /**
1238  * gst_pad_set_chain_list_function:
1239  * @pad: a sink #GstPad.
1240  * @chainlist: the #GstPadChainListFunction to set.
1241  *
1242  * Sets the given chain list function for the pad. The chainlist function is
1243  * called to process a #GstBufferList input buffer list. See
1244  * #GstPadChainListFunction for more details.
1245  *
1246  * Since: 0.10.24
1247  */
1248 void
1249 gst_pad_set_chain_list_function (GstPad * pad,
1250     GstPadChainListFunction chainlist)
1251 {
1252   g_return_if_fail (GST_IS_PAD (pad));
1253   g_return_if_fail (GST_PAD_IS_SINK (pad));
1254
1255   GST_PAD_CHAINLISTFUNC (pad) = chainlist;
1256   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "chainlistfunc set to %s",
1257       GST_DEBUG_FUNCPTR_NAME (chainlist));
1258 }
1259
1260 /**
1261  * gst_pad_set_getrange_function:
1262  * @pad: a source #GstPad.
1263  * @get: the #GstPadGetRangeFunction to set.
1264  *
1265  * Sets the given getrange function for the pad. The getrange function is
1266  * called to produce a new #GstBuffer to start the processing pipeline. see
1267  * #GstPadGetRangeFunction for a description of the getrange function.
1268  */
1269 void
1270 gst_pad_set_getrange_function (GstPad * pad, GstPadGetRangeFunction get)
1271 {
1272   g_return_if_fail (GST_IS_PAD (pad));
1273   g_return_if_fail (GST_PAD_IS_SRC (pad));
1274
1275   GST_PAD_GETRANGEFUNC (pad) = get;
1276
1277   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getrangefunc set to %s",
1278       GST_DEBUG_FUNCPTR_NAME (get));
1279 }
1280
1281 /**
1282  * gst_pad_set_checkgetrange_function:
1283  * @pad: a source #GstPad.
1284  * @check: the #GstPadCheckGetRangeFunction to set.
1285  *
1286  * Sets the given checkgetrange function for the pad. Implement this function
1287  * on a pad if you dynamically support getrange based scheduling on the pad.
1288  */
1289 void
1290 gst_pad_set_checkgetrange_function (GstPad * pad,
1291     GstPadCheckGetRangeFunction check)
1292 {
1293   g_return_if_fail (GST_IS_PAD (pad));
1294   g_return_if_fail (GST_PAD_IS_SRC (pad));
1295
1296   GST_PAD_CHECKGETRANGEFUNC (pad) = check;
1297
1298   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "checkgetrangefunc set to %s",
1299       GST_DEBUG_FUNCPTR_NAME (check));
1300 }
1301
1302 /**
1303  * gst_pad_set_event_function:
1304  * @pad: a #GstPad of either direction.
1305  * @event: the #GstPadEventFunction to set.
1306  *
1307  * Sets the given event handler for the pad.
1308  */
1309 void
1310 gst_pad_set_event_function (GstPad * pad, GstPadEventFunction event)
1311 {
1312   g_return_if_fail (GST_IS_PAD (pad));
1313
1314   GST_PAD_EVENTFUNC (pad) = event;
1315
1316   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "eventfunc for set to %s",
1317       GST_DEBUG_FUNCPTR_NAME (event));
1318 }
1319
1320 /**
1321  * gst_pad_set_query_function:
1322  * @pad: a #GstPad of either direction.
1323  * @query: the #GstPadQueryFunction to set.
1324  *
1325  * Set the given query function for the pad.
1326  */
1327 void
1328 gst_pad_set_query_function (GstPad * pad, GstPadQueryFunction query)
1329 {
1330   g_return_if_fail (GST_IS_PAD (pad));
1331
1332   GST_PAD_QUERYFUNC (pad) = query;
1333
1334   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "queryfunc set to %s",
1335       GST_DEBUG_FUNCPTR_NAME (query));
1336 }
1337
1338 /**
1339  * gst_pad_set_query_type_function:
1340  * @pad: a #GstPad of either direction.
1341  * @type_func: the #GstPadQueryTypeFunction to set.
1342  *
1343  * Set the given query type function for the pad.
1344  */
1345 void
1346 gst_pad_set_query_type_function (GstPad * pad,
1347     GstPadQueryTypeFunction type_func)
1348 {
1349   g_return_if_fail (GST_IS_PAD (pad));
1350
1351   GST_PAD_QUERYTYPEFUNC (pad) = type_func;
1352
1353   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "querytypefunc set to %s",
1354       GST_DEBUG_FUNCPTR_NAME (type_func));
1355 }
1356
1357 /**
1358  * gst_pad_get_query_types:
1359  * @pad: a #GstPad.
1360  *
1361  * Get an array of supported queries that can be performed
1362  * on this pad.
1363  *
1364  * Returns: a zero-terminated array of #GstQueryType.
1365  */
1366 const GstQueryType *
1367 gst_pad_get_query_types (GstPad * pad)
1368 {
1369   GstPadQueryTypeFunction func;
1370
1371   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1372
1373   if (G_UNLIKELY ((func = GST_PAD_QUERYTYPEFUNC (pad)) == NULL))
1374     goto no_func;
1375
1376   return func (pad);
1377
1378 no_func:
1379   {
1380     return NULL;
1381   }
1382 }
1383
1384 static gboolean
1385 gst_pad_get_query_types_dispatcher (GstPad * pad, const GstQueryType ** data)
1386 {
1387   *data = gst_pad_get_query_types (pad);
1388
1389   return TRUE;
1390 }
1391
1392 /**
1393  * gst_pad_get_query_types_default:
1394  * @pad: a #GstPad.
1395  *
1396  * Invoke the default dispatcher for the query types on
1397  * the pad.
1398  *
1399  * Returns: an zero-terminated array of #GstQueryType, or NULL if none of the
1400  * internally-linked pads has a query types function.
1401  */
1402 const GstQueryType *
1403 gst_pad_get_query_types_default (GstPad * pad)
1404 {
1405   GstQueryType *result = NULL;
1406
1407   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1408
1409   gst_pad_dispatcher (pad, (GstPadDispatcherFunction)
1410       gst_pad_get_query_types_dispatcher, &result);
1411
1412   return result;
1413 }
1414
1415 /**
1416  * gst_pad_set_iterate_internal_links_function:
1417  * @pad: a #GstPad of either direction.
1418  * @iterintlink: the #GstPadIterIntLinkFunction to set.
1419  *
1420  * Sets the given internal link iterator function for the pad.
1421  *
1422  * Since: 0.10.21
1423  */
1424 void
1425 gst_pad_set_iterate_internal_links_function (GstPad * pad,
1426     GstPadIterIntLinkFunction iterintlink)
1427 {
1428   g_return_if_fail (GST_IS_PAD (pad));
1429
1430   GST_PAD_ITERINTLINKFUNC (pad) = iterintlink;
1431   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link iterator set to %s",
1432       GST_DEBUG_FUNCPTR_NAME (iterintlink));
1433 }
1434
1435 /**
1436  * gst_pad_set_internal_link_function:
1437  * @pad: a #GstPad of either direction.
1438  * @intlink: the #GstPadIntLinkFunction to set.
1439  *
1440  * Sets the given internal link function for the pad.
1441  *
1442  * Deprecated: Use the thread-safe gst_pad_set_iterate_internal_links_function()
1443  */
1444 #ifndef GST_REMOVE_DEPRECATED
1445 void
1446 gst_pad_set_internal_link_function (GstPad * pad, GstPadIntLinkFunction intlink)
1447 {
1448   g_return_if_fail (GST_IS_PAD (pad));
1449
1450   GST_PAD_INTLINKFUNC (pad) = intlink;
1451   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "internal link set to %s",
1452       GST_DEBUG_FUNCPTR_NAME (intlink));
1453 }
1454 #endif /* GST_REMOVE_DEPRECATED */
1455
1456 /**
1457  * gst_pad_set_link_function:
1458  * @pad: a #GstPad.
1459  * @link: the #GstPadLinkFunction to set.
1460  *
1461  * Sets the given link function for the pad. It will be called when
1462  * the pad is linked with another pad.
1463  *
1464  * The return value #GST_PAD_LINK_OK should be used when the connection can be
1465  * made.
1466  *
1467  * The return value #GST_PAD_LINK_REFUSED should be used when the connection
1468  * cannot be made for some reason.
1469  *
1470  * If @link is installed on a source pad, it should call the #GstPadLinkFunction
1471  * of the peer sink pad, if present.
1472  */
1473 void
1474 gst_pad_set_link_function (GstPad * pad, GstPadLinkFunction link)
1475 {
1476   g_return_if_fail (GST_IS_PAD (pad));
1477
1478   GST_PAD_LINKFUNC (pad) = link;
1479   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "linkfunc set to %s",
1480       GST_DEBUG_FUNCPTR_NAME (link));
1481 }
1482
1483 /**
1484  * gst_pad_set_unlink_function:
1485  * @pad: a #GstPad.
1486  * @unlink: the #GstPadUnlinkFunction to set.
1487  *
1488  * Sets the given unlink function for the pad. It will be called
1489  * when the pad is unlinked.
1490  */
1491 void
1492 gst_pad_set_unlink_function (GstPad * pad, GstPadUnlinkFunction unlink)
1493 {
1494   g_return_if_fail (GST_IS_PAD (pad));
1495
1496   GST_PAD_UNLINKFUNC (pad) = unlink;
1497   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "unlinkfunc set to %s",
1498       GST_DEBUG_FUNCPTR_NAME (unlink));
1499 }
1500
1501 /**
1502  * gst_pad_set_getcaps_function:
1503  * @pad: a #GstPad.
1504  * @getcaps: the #GstPadGetCapsFunction to set.
1505  *
1506  * Sets the given getcaps function for the pad. @getcaps should return the
1507  * allowable caps for a pad in the context of the element's state, its link to
1508  * other elements, and the devices or files it has opened. These caps must be a
1509  * subset of the pad template caps. In the NULL state with no links, @getcaps
1510  * should ideally return the same caps as the pad template. In rare
1511  * circumstances, an object property can affect the caps returned by @getcaps,
1512  * but this is discouraged.
1513  *
1514  * You do not need to call this function if @pad's allowed caps are always the
1515  * same as the pad template caps. This can only be true if the padtemplate
1516  * has fixed simple caps.
1517  *
1518  * For most filters, the caps returned by @getcaps is directly affected by the
1519  * allowed caps on other pads. For demuxers and decoders, the caps returned by
1520  * the srcpad's getcaps function is directly related to the stream data. Again,
1521  * @getcaps should return the most specific caps it reasonably can, since this
1522  * helps with autoplugging.
1523  *
1524  * Note that the return value from @getcaps is owned by the caller, so the
1525  * caller should unref the caps after usage.
1526  */
1527 void
1528 gst_pad_set_getcaps_function (GstPad * pad, GstPadGetCapsFunction getcaps)
1529 {
1530   g_return_if_fail (GST_IS_PAD (pad));
1531
1532   GST_PAD_GETCAPSFUNC (pad) = getcaps;
1533   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "getcapsfunc set to %s",
1534       GST_DEBUG_FUNCPTR_NAME (getcaps));
1535 }
1536
1537 /**
1538  * gst_pad_set_acceptcaps_function:
1539  * @pad: a #GstPad.
1540  * @acceptcaps: the #GstPadAcceptCapsFunction to set.
1541  *
1542  * Sets the given acceptcaps function for the pad.  The acceptcaps function
1543  * will be called to check if the pad can accept the given caps. Setting the
1544  * acceptcaps function to NULL restores the default behaviour of allowing
1545  * any caps that matches the caps from gst_pad_get_caps.
1546  */
1547 void
1548 gst_pad_set_acceptcaps_function (GstPad * pad,
1549     GstPadAcceptCapsFunction acceptcaps)
1550 {
1551   g_return_if_fail (GST_IS_PAD (pad));
1552
1553   GST_PAD_ACCEPTCAPSFUNC (pad) = acceptcaps;
1554   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "acceptcapsfunc set to %s",
1555       GST_DEBUG_FUNCPTR_NAME (acceptcaps));
1556 }
1557
1558 /**
1559  * gst_pad_set_fixatecaps_function:
1560  * @pad: a #GstPad.
1561  * @fixatecaps: the #GstPadFixateCapsFunction to set.
1562  *
1563  * Sets the given fixatecaps function for the pad.  The fixatecaps function
1564  * will be called whenever the default values for a GstCaps needs to be
1565  * filled in.
1566  */
1567 void
1568 gst_pad_set_fixatecaps_function (GstPad * pad,
1569     GstPadFixateCapsFunction fixatecaps)
1570 {
1571   g_return_if_fail (GST_IS_PAD (pad));
1572
1573   GST_PAD_FIXATECAPSFUNC (pad) = fixatecaps;
1574   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fixatecapsfunc set to %s",
1575       GST_DEBUG_FUNCPTR_NAME (fixatecaps));
1576 }
1577
1578 /**
1579  * gst_pad_set_setcaps_function:
1580  * @pad: a #GstPad.
1581  * @setcaps: the #GstPadSetCapsFunction to set.
1582  *
1583  * Sets the given setcaps function for the pad.  The setcaps function
1584  * will be called whenever a buffer with a new media type is pushed or
1585  * pulled from the pad. The pad/element needs to update its internal
1586  * structures to process the new media type. If this new type is not
1587  * acceptable, the setcaps function should return FALSE.
1588  */
1589 void
1590 gst_pad_set_setcaps_function (GstPad * pad, GstPadSetCapsFunction setcaps)
1591 {
1592   g_return_if_fail (GST_IS_PAD (pad));
1593
1594   GST_PAD_SETCAPSFUNC (pad) = setcaps;
1595   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "setcapsfunc set to %s",
1596       GST_DEBUG_FUNCPTR_NAME (setcaps));
1597 }
1598
1599 /**
1600  * gst_pad_set_bufferalloc_function:
1601  * @pad: a sink #GstPad.
1602  * @bufalloc: the #GstPadBufferAllocFunction to set.
1603  *
1604  * Sets the given bufferalloc function for the pad. Note that the
1605  * bufferalloc function can only be set on sinkpads.
1606  */
1607 void
1608 gst_pad_set_bufferalloc_function (GstPad * pad,
1609     GstPadBufferAllocFunction bufalloc)
1610 {
1611   g_return_if_fail (GST_IS_PAD (pad));
1612   g_return_if_fail (GST_PAD_IS_SINK (pad));
1613
1614   GST_PAD_BUFFERALLOCFUNC (pad) = bufalloc;
1615   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "bufferallocfunc set to %s",
1616       GST_DEBUG_FUNCPTR_NAME (bufalloc));
1617 }
1618
1619 /**
1620  * gst_pad_unlink:
1621  * @srcpad: the source #GstPad to unlink.
1622  * @sinkpad: the sink #GstPad to unlink.
1623  *
1624  * Unlinks the source pad from the sink pad. Will emit the #GstPad::unlinked
1625  * signal on both pads.
1626  *
1627  * Returns: TRUE if the pads were unlinked. This function returns FALSE if
1628  * the pads were not linked together.
1629  *
1630  * MT safe.
1631  */
1632 gboolean
1633 gst_pad_unlink (GstPad * srcpad, GstPad * sinkpad)
1634 {
1635   gboolean result = FALSE;
1636   GstElement *parent = NULL;
1637
1638   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1639   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), FALSE);
1640   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1641   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad), FALSE);
1642
1643   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinking %s:%s(%p) and %s:%s(%p)",
1644       GST_DEBUG_PAD_NAME (srcpad), srcpad,
1645       GST_DEBUG_PAD_NAME (sinkpad), sinkpad);
1646
1647   /* We need to notify the parent before taking any pad locks as the bin in
1648    * question might be waiting for a lock on the pad while holding its lock
1649    * that our message will try to take. */
1650   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1651     if (GST_IS_ELEMENT (parent)) {
1652       gst_element_post_message (parent,
1653           gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
1654               GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, TRUE));
1655     } else {
1656       gst_object_unref (parent);
1657       parent = NULL;
1658     }
1659   }
1660
1661   GST_OBJECT_LOCK (srcpad);
1662
1663   GST_OBJECT_LOCK (sinkpad);
1664
1665   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != sinkpad))
1666     goto not_linked_together;
1667
1668   if (GST_PAD_UNLINKFUNC (srcpad)) {
1669     GST_PAD_UNLINKFUNC (srcpad) (srcpad);
1670   }
1671   if (GST_PAD_UNLINKFUNC (sinkpad)) {
1672     GST_PAD_UNLINKFUNC (sinkpad) (sinkpad);
1673   }
1674
1675   /* first clear peers */
1676   GST_PAD_PEER (srcpad) = NULL;
1677   GST_PAD_PEER (sinkpad) = NULL;
1678
1679   GST_OBJECT_UNLOCK (sinkpad);
1680   GST_OBJECT_UNLOCK (srcpad);
1681
1682   /* fire off a signal to each of the pads telling them
1683    * that they've been unlinked */
1684   g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_UNLINKED], 0, sinkpad);
1685   g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_UNLINKED], 0, srcpad);
1686
1687   GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "unlinked %s:%s and %s:%s",
1688       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1689
1690   result = TRUE;
1691
1692 done:
1693   if (parent != NULL) {
1694     gst_element_post_message (parent,
1695         gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
1696             GST_STRUCTURE_CHANGE_TYPE_PAD_UNLINK, parent, FALSE));
1697     gst_object_unref (parent);
1698   }
1699   return result;
1700
1701   /* ERRORS */
1702 not_linked_together:
1703   {
1704     /* we do not emit a warning in this case because unlinking cannot
1705      * be made MT safe.*/
1706     GST_OBJECT_UNLOCK (sinkpad);
1707     GST_OBJECT_UNLOCK (srcpad);
1708     goto done;
1709   }
1710 }
1711
1712 /**
1713  * gst_pad_is_linked:
1714  * @pad: pad to check
1715  *
1716  * Checks if a @pad is linked to another pad or not.
1717  *
1718  * Returns: TRUE if the pad is linked, FALSE otherwise.
1719  *
1720  * MT safe.
1721  */
1722 gboolean
1723 gst_pad_is_linked (GstPad * pad)
1724 {
1725   gboolean result;
1726
1727   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1728
1729   GST_OBJECT_LOCK (pad);
1730   result = (GST_PAD_PEER (pad) != NULL);
1731   GST_OBJECT_UNLOCK (pad);
1732
1733   return result;
1734 }
1735
1736 /* get the caps from both pads and see if the intersection
1737  * is not empty.
1738  *
1739  * This function should be called with the pad LOCK on both
1740  * pads
1741  */
1742 static gboolean
1743 gst_pad_link_check_compatible_unlocked (GstPad * src, GstPad * sink)
1744 {
1745   GstCaps *srccaps;
1746   GstCaps *sinkcaps;
1747   GstCaps *icaps;
1748
1749   srccaps = gst_pad_get_caps_unlocked (src);
1750   sinkcaps = gst_pad_get_caps_unlocked (sink);
1751
1752   GST_CAT_DEBUG (GST_CAT_CAPS, "src caps %" GST_PTR_FORMAT, srccaps);
1753   GST_CAT_DEBUG (GST_CAT_CAPS, "sink caps %" GST_PTR_FORMAT, sinkcaps);
1754
1755   /* if we have caps on both pads we can check the intersection. If one
1756    * of the caps is NULL, we return TRUE. */
1757   if (srccaps == NULL || sinkcaps == NULL) {
1758     if (srccaps)
1759       gst_caps_unref (srccaps);
1760     if (sinkcaps)
1761       gst_caps_unref (sinkcaps);
1762     goto done;
1763   }
1764
1765   icaps = gst_caps_intersect (srccaps, sinkcaps);
1766   gst_caps_unref (srccaps);
1767   gst_caps_unref (sinkcaps);
1768
1769   if (icaps == NULL)
1770     goto was_null;
1771
1772   GST_CAT_DEBUG (GST_CAT_CAPS,
1773       "intersection caps %p %" GST_PTR_FORMAT, icaps, icaps);
1774
1775   if (gst_caps_is_empty (icaps))
1776     goto was_empty;
1777
1778   gst_caps_unref (icaps);
1779
1780 done:
1781   return TRUE;
1782
1783   /* incompatible cases */
1784 was_null:
1785   {
1786     GST_CAT_DEBUG (GST_CAT_CAPS, "intersection gave NULL");
1787     return FALSE;
1788   }
1789 was_empty:
1790   {
1791     GST_CAT_DEBUG (GST_CAT_CAPS, "intersection is EMPTY");
1792     gst_caps_unref (icaps);
1793     return FALSE;
1794   }
1795 }
1796
1797 /* check if the grandparents of both pads are the same.
1798  * This check is required so that we don't try to link
1799  * pads from elements in different bins without ghostpads.
1800  *
1801  * The LOCK should be held on both pads
1802  */
1803 static gboolean
1804 gst_pad_link_check_hierarchy (GstPad * src, GstPad * sink)
1805 {
1806   GstObject *psrc, *psink;
1807
1808   psrc = GST_OBJECT_PARENT (src);
1809   psink = GST_OBJECT_PARENT (sink);
1810
1811   /* if one of the pads has no parent, we allow the link */
1812   if (G_UNLIKELY (psrc == NULL || psink == NULL))
1813     goto no_parent;
1814
1815   /* only care about parents that are elements */
1816   if (G_UNLIKELY (!GST_IS_ELEMENT (psrc) || !GST_IS_ELEMENT (psink)))
1817     goto no_element_parent;
1818
1819   /* if the parents are the same, we have a loop */
1820   if (G_UNLIKELY (psrc == psink))
1821     goto same_parents;
1822
1823   /* if they both have a parent, we check the grandparents. We can not lock
1824    * the parent because we hold on the child (pad) and the locking order is
1825    * parent >> child. */
1826   psrc = GST_OBJECT_PARENT (psrc);
1827   psink = GST_OBJECT_PARENT (psink);
1828
1829   /* if they have grandparents but they are not the same */
1830   if (G_UNLIKELY (psrc != psink))
1831     goto wrong_grandparents;
1832
1833   return TRUE;
1834
1835   /* ERRORS */
1836 no_parent:
1837   {
1838     GST_CAT_DEBUG (GST_CAT_CAPS,
1839         "one of the pads has no parent %" GST_PTR_FORMAT " and %"
1840         GST_PTR_FORMAT, psrc, psink);
1841     return TRUE;
1842   }
1843 no_element_parent:
1844   {
1845     GST_CAT_DEBUG (GST_CAT_CAPS,
1846         "one of the pads has no element parent %" GST_PTR_FORMAT " and %"
1847         GST_PTR_FORMAT, psrc, psink);
1848     return TRUE;
1849   }
1850 same_parents:
1851   {
1852     GST_CAT_DEBUG (GST_CAT_CAPS, "pads have same parent %" GST_PTR_FORMAT,
1853         psrc);
1854     return FALSE;
1855   }
1856 wrong_grandparents:
1857   {
1858     GST_CAT_DEBUG (GST_CAT_CAPS,
1859         "pads have different grandparents %" GST_PTR_FORMAT " and %"
1860         GST_PTR_FORMAT, psrc, psink);
1861     return FALSE;
1862   }
1863 }
1864
1865 /* FIXME leftover from an attempt at refactoring... */
1866 /* call with the two pads unlocked, when this function returns GST_PAD_LINK_OK,
1867  * the two pads will be locked in the srcpad, sinkpad order. */
1868 static GstPadLinkReturn
1869 gst_pad_link_prepare (GstPad * srcpad, GstPad * sinkpad)
1870 {
1871   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1872       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1873
1874   GST_OBJECT_LOCK (srcpad);
1875
1876   if (G_UNLIKELY (GST_PAD_PEER (srcpad) != NULL))
1877     goto src_was_linked;
1878
1879   GST_OBJECT_LOCK (sinkpad);
1880
1881   if (G_UNLIKELY (GST_PAD_PEER (sinkpad) != NULL))
1882     goto sink_was_linked;
1883
1884   /* check hierarchy, pads can only be linked if the grandparents
1885    * are the same. */
1886   if (!gst_pad_link_check_hierarchy (srcpad, sinkpad))
1887     goto wrong_hierarchy;
1888
1889   /* check pad caps for non-empty intersection */
1890   if (!gst_pad_link_check_compatible_unlocked (srcpad, sinkpad))
1891     goto no_format;
1892
1893   /* FIXME check pad scheduling for non-empty intersection */
1894
1895   return GST_PAD_LINK_OK;
1896
1897 src_was_linked:
1898   {
1899     GST_CAT_INFO (GST_CAT_PADS, "src %s:%s was already linked to %s:%s",
1900         GST_DEBUG_PAD_NAME (srcpad),
1901         GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1902     /* we do not emit a warning in this case because unlinking cannot
1903      * be made MT safe.*/
1904     GST_OBJECT_UNLOCK (srcpad);
1905     return GST_PAD_LINK_WAS_LINKED;
1906   }
1907 sink_was_linked:
1908   {
1909     GST_CAT_INFO (GST_CAT_PADS, "sink %s:%s was already linked to %s:%s",
1910         GST_DEBUG_PAD_NAME (sinkpad),
1911         GST_DEBUG_PAD_NAME (GST_PAD_PEER (sinkpad)));
1912     /* we do not emit a warning in this case because unlinking cannot
1913      * be made MT safe.*/
1914     GST_OBJECT_UNLOCK (sinkpad);
1915     GST_OBJECT_UNLOCK (srcpad);
1916     return GST_PAD_LINK_WAS_LINKED;
1917   }
1918 wrong_hierarchy:
1919   {
1920     GST_CAT_INFO (GST_CAT_PADS, "pads have wrong hierarchy");
1921     GST_OBJECT_UNLOCK (sinkpad);
1922     GST_OBJECT_UNLOCK (srcpad);
1923     return GST_PAD_LINK_WRONG_HIERARCHY;
1924   }
1925 no_format:
1926   {
1927     GST_CAT_INFO (GST_CAT_PADS, "caps are incompatible");
1928     GST_OBJECT_UNLOCK (sinkpad);
1929     GST_OBJECT_UNLOCK (srcpad);
1930     return GST_PAD_LINK_NOFORMAT;
1931   }
1932 }
1933
1934 /**
1935  * gst_pad_can_link:
1936  * @srcpad: the source #GstPad.
1937  * @sinkpad: the sink #GstPad.
1938  *
1939  * Checks if the source pad and the sink pad are compatible so they can be
1940  * linked. 
1941  *
1942  * Returns: TRUE if the pads can be linked.
1943  */
1944 gboolean
1945 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1946 {
1947   GstPadLinkReturn result;
1948
1949   /* generic checks */
1950   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1951   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1952
1953   GST_CAT_INFO (GST_CAT_PADS, "check if %s:%s can link with %s:%s",
1954       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1955
1956   /* gst_pad_link_prepare does everything for us, we only release the locks
1957    * on the pads that it gets us. If this function returns !OK the locks are not
1958    * taken anymore. */
1959   result = gst_pad_link_prepare (srcpad, sinkpad);
1960   if (result != GST_PAD_LINK_OK)
1961     goto done;
1962
1963   GST_OBJECT_UNLOCK (srcpad);
1964   GST_OBJECT_UNLOCK (sinkpad);
1965
1966 done:
1967   return result == GST_PAD_LINK_OK;
1968 }
1969
1970 /**
1971  * gst_pad_link:
1972  * @srcpad: the source #GstPad to link.
1973  * @sinkpad: the sink #GstPad to link.
1974  *
1975  * Links the source pad and the sink pad.
1976  *
1977  * Returns: A result code indicating if the connection worked or
1978  *          what went wrong.
1979  *
1980  * MT Safe.
1981  */
1982 GstPadLinkReturn
1983 gst_pad_link (GstPad * srcpad, GstPad * sinkpad)
1984 {
1985   GstPadLinkReturn result;
1986   GstElement *parent;
1987
1988   g_return_val_if_fail (GST_IS_PAD (srcpad), GST_PAD_LINK_REFUSED);
1989   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), GST_PAD_LINK_WRONG_DIRECTION);
1990   g_return_val_if_fail (GST_IS_PAD (sinkpad), GST_PAD_LINK_REFUSED);
1991   g_return_val_if_fail (GST_PAD_IS_SINK (sinkpad),
1992       GST_PAD_LINK_WRONG_DIRECTION);
1993
1994   /* Notify the parent early. See gst_pad_unlink for details. */
1995   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (srcpad)))) {
1996     if (GST_IS_ELEMENT (parent)) {
1997       gst_element_post_message (parent,
1998           gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
1999               GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, TRUE));
2000     } else {
2001       gst_object_unref (parent);
2002       parent = NULL;
2003     }
2004   }
2005
2006   /* prepare will also lock the two pads */
2007   result = gst_pad_link_prepare (srcpad, sinkpad);
2008
2009   if (result != GST_PAD_LINK_OK)
2010     goto done;
2011
2012   /* must set peers before calling the link function */
2013   GST_PAD_PEER (srcpad) = sinkpad;
2014   GST_PAD_PEER (sinkpad) = srcpad;
2015
2016   GST_OBJECT_UNLOCK (sinkpad);
2017   GST_OBJECT_UNLOCK (srcpad);
2018
2019   /* FIXME released the locks here, concurrent thread might link
2020    * something else. */
2021   if (GST_PAD_LINKFUNC (srcpad)) {
2022     /* this one will call the peer link function */
2023     result = GST_PAD_LINKFUNC (srcpad) (srcpad, sinkpad);
2024   } else if (GST_PAD_LINKFUNC (sinkpad)) {
2025     /* if no source link function, we need to call the sink link
2026      * function ourselves. */
2027     result = GST_PAD_LINKFUNC (sinkpad) (sinkpad, srcpad);
2028   } else {
2029     result = GST_PAD_LINK_OK;
2030   }
2031
2032   GST_OBJECT_LOCK (srcpad);
2033   GST_OBJECT_LOCK (sinkpad);
2034
2035   if (result == GST_PAD_LINK_OK) {
2036     GST_OBJECT_UNLOCK (sinkpad);
2037     GST_OBJECT_UNLOCK (srcpad);
2038
2039     /* fire off a signal to each of the pads telling them
2040      * that they've been linked */
2041     g_signal_emit (G_OBJECT (srcpad), gst_pad_signals[PAD_LINKED], 0, sinkpad);
2042     g_signal_emit (G_OBJECT (sinkpad), gst_pad_signals[PAD_LINKED], 0, srcpad);
2043
2044     GST_CAT_INFO (GST_CAT_PADS, "linked %s:%s and %s:%s, successful",
2045         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2046   } else {
2047     GST_CAT_INFO (GST_CAT_PADS, "link between %s:%s and %s:%s failed",
2048         GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2049
2050     GST_PAD_PEER (srcpad) = NULL;
2051     GST_PAD_PEER (sinkpad) = NULL;
2052
2053     GST_OBJECT_UNLOCK (sinkpad);
2054     GST_OBJECT_UNLOCK (srcpad);
2055   }
2056
2057 done:
2058   if (parent) {
2059     gst_element_post_message (parent,
2060         gst_message_new_structure_change (GST_OBJECT_CAST (srcpad),
2061             GST_STRUCTURE_CHANGE_TYPE_PAD_LINK, parent, FALSE));
2062     gst_object_unref (parent);
2063   }
2064
2065   return result;
2066 }
2067
2068 static void
2069 gst_pad_set_pad_template (GstPad * pad, GstPadTemplate * templ)
2070 {
2071   GstPadTemplate **template_p;
2072
2073   /* this function would need checks if it weren't static */
2074
2075   GST_OBJECT_LOCK (pad);
2076   template_p = &pad->padtemplate;
2077   gst_object_replace ((GstObject **) template_p, (GstObject *) templ);
2078   GST_OBJECT_UNLOCK (pad);
2079
2080   if (templ)
2081     gst_pad_template_pad_created (templ, pad);
2082 }
2083
2084 /**
2085  * gst_pad_get_pad_template:
2086  * @pad: a #GstPad.
2087  *
2088  * Gets the template for @pad.
2089  *
2090  * Returns: the #GstPadTemplate from which this pad was instantiated, or %NULL
2091  * if this pad has no template.
2092  *
2093  * FIXME: currently returns an unrefcounted padtemplate.
2094  */
2095 GstPadTemplate *
2096 gst_pad_get_pad_template (GstPad * pad)
2097 {
2098   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2099
2100   return GST_PAD_PAD_TEMPLATE (pad);
2101 }
2102
2103
2104 /* should be called with the pad LOCK held */
2105 /* refs the caps, so caller is responsible for getting it unreffed */
2106 static GstCaps *
2107 gst_pad_get_caps_unlocked (GstPad * pad)
2108 {
2109   GstCaps *result = NULL;
2110   GstPadTemplate *templ;
2111
2112   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2113
2114   if (GST_PAD_GETCAPSFUNC (pad)) {
2115     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2116         "dispatching to pad getcaps function");
2117
2118     GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_GETCAPS);
2119     GST_OBJECT_UNLOCK (pad);
2120     result = GST_PAD_GETCAPSFUNC (pad) (pad);
2121     GST_OBJECT_LOCK (pad);
2122     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_GETCAPS);
2123
2124     if (result == NULL) {
2125       g_critical ("pad %s:%s returned NULL caps from getcaps function",
2126           GST_DEBUG_PAD_NAME (pad));
2127     } else {
2128       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2129           "pad getcaps returned %" GST_PTR_FORMAT, result);
2130 #ifndef G_DISABLE_ASSERT
2131       /* check that the returned caps are a real subset of the template caps */
2132       if (GST_PAD_PAD_TEMPLATE (pad)) {
2133         const GstCaps *templ_caps =
2134             GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2135         if (!gst_caps_is_subset (result, templ_caps)) {
2136           GstCaps *temp;
2137
2138           GST_CAT_ERROR_OBJECT (GST_CAT_CAPS, pad,
2139               "pad returned caps %" GST_PTR_FORMAT
2140               " which are not a real subset of its template caps %"
2141               GST_PTR_FORMAT, result, templ_caps);
2142           g_warning
2143               ("pad %s:%s returned caps which are not a real "
2144               "subset of its template caps", GST_DEBUG_PAD_NAME (pad));
2145           temp = gst_caps_intersect (templ_caps, result);
2146           gst_caps_unref (result);
2147           result = temp;
2148         }
2149       }
2150 #endif
2151       goto done;
2152     }
2153   }
2154   if ((templ = GST_PAD_PAD_TEMPLATE (pad))) {
2155     result = GST_PAD_TEMPLATE_CAPS (templ);
2156     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2157         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2158         result);
2159
2160     result = gst_caps_ref (result);
2161     goto done;
2162   }
2163   if ((result = GST_PAD_CAPS (pad))) {
2164     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2165         "using pad caps %p %" GST_PTR_FORMAT, result, result);
2166
2167     result = gst_caps_ref (result);
2168     goto done;
2169   }
2170
2171   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad has no caps");
2172   result = gst_caps_new_empty ();
2173
2174 done:
2175   return result;
2176 }
2177
2178 /**
2179  * gst_pad_get_caps:
2180  * @pad: a  #GstPad to get the capabilities of.
2181  *
2182  * Gets the capabilities this pad can produce or consume.
2183  * Note that this method doesn't necessarily return the caps set by
2184  * gst_pad_set_caps() - use GST_PAD_CAPS() for that instead.
2185  * gst_pad_get_caps returns all possible caps a pad can operate with, using
2186  * the pad's get_caps function;
2187  * this returns the pad template caps if not explicitly set.
2188  *
2189  * Returns: a newly allocated copy of the #GstCaps of this pad.
2190  *
2191  * MT safe.
2192  */
2193 GstCaps *
2194 gst_pad_get_caps (GstPad * pad)
2195 {
2196   GstCaps *result = NULL;
2197
2198   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2199
2200   GST_OBJECT_LOCK (pad);
2201
2202   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
2203
2204   result = gst_pad_get_caps_unlocked (pad);
2205
2206   /* be sure that we have a copy */
2207   if (result)
2208     result = gst_caps_make_writable (result);
2209
2210   GST_OBJECT_UNLOCK (pad);
2211
2212   return result;
2213 }
2214
2215 /**
2216  * gst_pad_peer_get_caps:
2217  * @pad: a  #GstPad to get the peer capabilities of.
2218  *
2219  * Gets the capabilities of the peer connected to this pad.
2220  *
2221  * Returns: the #GstCaps of the peer pad. This function returns a new caps, so
2222  * use gst_caps_unref to get rid of it. this function returns NULL if there is
2223  * no peer pad.
2224  */
2225 GstCaps *
2226 gst_pad_peer_get_caps (GstPad * pad)
2227 {
2228   GstPad *peerpad;
2229   GstCaps *result = NULL;
2230
2231   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2232
2233   GST_OBJECT_LOCK (pad);
2234
2235   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get peer caps");
2236
2237   peerpad = GST_PAD_PEER (pad);
2238   if (G_UNLIKELY (peerpad == NULL))
2239     goto no_peer;
2240
2241   gst_object_ref (peerpad);
2242   GST_OBJECT_UNLOCK (pad);
2243
2244   result = gst_pad_get_caps (peerpad);
2245
2246   gst_object_unref (peerpad);
2247
2248   return result;
2249
2250 no_peer:
2251   {
2252     GST_OBJECT_UNLOCK (pad);
2253     return NULL;
2254   }
2255 }
2256
2257 static gboolean
2258 fixate_value (GValue * dest, const GValue * src)
2259 {
2260   if (G_VALUE_TYPE (src) == GST_TYPE_INT_RANGE) {
2261     g_value_init (dest, G_TYPE_INT);
2262     g_value_set_int (dest, gst_value_get_int_range_min (src));
2263   } else if (G_VALUE_TYPE (src) == GST_TYPE_DOUBLE_RANGE) {
2264     g_value_init (dest, G_TYPE_DOUBLE);
2265     g_value_set_double (dest, gst_value_get_double_range_min (src));
2266   } else if (G_VALUE_TYPE (src) == GST_TYPE_FRACTION_RANGE) {
2267     gst_value_init_and_copy (dest, gst_value_get_fraction_range_min (src));
2268   } else if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
2269     GValue temp = { 0 };
2270
2271     /* list could be empty */
2272     if (gst_value_list_get_size (src) <= 0)
2273       return FALSE;
2274
2275     gst_value_init_and_copy (&temp, gst_value_list_get_value (src, 0));
2276
2277     if (!fixate_value (dest, &temp))
2278       gst_value_init_and_copy (dest, &temp);
2279     g_value_unset (&temp);
2280   } else if (G_VALUE_TYPE (src) == GST_TYPE_ARRAY) {
2281     gboolean res = FALSE;
2282     guint n;
2283
2284     g_value_init (dest, GST_TYPE_ARRAY);
2285     for (n = 0; n < gst_value_array_get_size (src); n++) {
2286       GValue kid = { 0 };
2287       const GValue *orig_kid = gst_value_array_get_value (src, n);
2288
2289       if (!fixate_value (&kid, orig_kid))
2290         gst_value_init_and_copy (&kid, orig_kid);
2291       else
2292         res = TRUE;
2293       gst_value_array_append_value (dest, &kid);
2294       g_value_unset (&kid);
2295     }
2296
2297     if (!res)
2298       g_value_unset (dest);
2299
2300     return res;
2301   } else {
2302     return FALSE;
2303   }
2304
2305   return TRUE;
2306 }
2307
2308 static gboolean
2309 gst_pad_default_fixate (GQuark field_id, const GValue * value, gpointer data)
2310 {
2311   GstStructure *s = data;
2312   GValue v = { 0 };
2313
2314   if (fixate_value (&v, value)) {
2315     gst_structure_id_set_value (s, field_id, &v);
2316     g_value_unset (&v);
2317   }
2318
2319   return TRUE;
2320 }
2321
2322 /**
2323  * gst_pad_fixate_caps:
2324  * @pad: a  #GstPad to fixate
2325  * @caps: the  #GstCaps to fixate
2326  *
2327  * Fixate a caps on the given pad. Modifies the caps in place, so you should
2328  * make sure that the caps are actually writable (see gst_caps_make_writable()).
2329  */
2330 void
2331 gst_pad_fixate_caps (GstPad * pad, GstCaps * caps)
2332 {
2333   GstPadFixateCapsFunction fixatefunc;
2334   guint n;
2335
2336   g_return_if_fail (GST_IS_PAD (pad));
2337   g_return_if_fail (caps != NULL);
2338
2339   if (gst_caps_is_fixed (caps))
2340     return;
2341
2342   fixatefunc = GST_PAD_FIXATECAPSFUNC (pad);
2343   if (fixatefunc) {
2344     fixatefunc (pad, caps);
2345   }
2346
2347   /* default fixation */
2348   for (n = 0; n < gst_caps_get_size (caps); n++) {
2349     GstStructure *s = gst_caps_get_structure (caps, n);
2350
2351     gst_structure_foreach (s, gst_pad_default_fixate, s);
2352   }
2353 }
2354
2355 /* Default accept caps implementation just checks against
2356  * against the allowed caps for the pad */
2357 static gboolean
2358 gst_pad_acceptcaps_default (GstPad * pad, GstCaps * caps)
2359 {
2360   /* get the caps and see if it intersects to something
2361    * not empty */
2362   GstCaps *intersect;
2363   GstCaps *allowed;
2364   gboolean result = FALSE;
2365
2366   GST_DEBUG_OBJECT (pad, "caps %" GST_PTR_FORMAT, caps);
2367
2368   allowed = gst_pad_get_caps (pad);
2369   if (!allowed)
2370     goto nothing_allowed;
2371
2372   GST_DEBUG_OBJECT (pad, "allowed caps %" GST_PTR_FORMAT, allowed);
2373
2374   intersect = gst_caps_intersect (allowed, caps);
2375
2376   GST_DEBUG_OBJECT (pad, "intersection %" GST_PTR_FORMAT, intersect);
2377
2378   result = !gst_caps_is_empty (intersect);
2379
2380   gst_caps_unref (allowed);
2381   gst_caps_unref (intersect);
2382
2383   return result;
2384
2385   /* ERRORS */
2386 nothing_allowed:
2387   {
2388     GST_DEBUG_OBJECT (pad, "no caps allowed on the pad");
2389     return FALSE;
2390   }
2391 }
2392
2393 /**
2394  * gst_pad_accept_caps:
2395  * @pad: a #GstPad to check
2396  * @caps: a #GstCaps to check on the pad
2397  *
2398  * Check if the given pad accepts the caps.
2399  *
2400  * Returns: TRUE if the pad can accept the caps.
2401  */
2402 gboolean
2403 gst_pad_accept_caps (GstPad * pad, GstCaps * caps)
2404 {
2405   gboolean result;
2406   GstPadAcceptCapsFunction acceptfunc;
2407   GstCaps *existing = NULL;
2408
2409   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2410
2411   /* any pad can be unnegotiated */
2412   if (caps == NULL)
2413     return TRUE;
2414
2415   /* lock for checking the existing caps */
2416   GST_OBJECT_LOCK (pad);
2417   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
2418   /* The current caps on a pad are trivially acceptable */
2419   if (G_LIKELY ((existing = GST_PAD_CAPS (pad)))) {
2420     if (caps == existing || gst_caps_is_equal (caps, existing))
2421       goto is_same_caps;
2422   }
2423   acceptfunc = GST_PAD_ACCEPTCAPSFUNC (pad);
2424   GST_OBJECT_UNLOCK (pad);
2425
2426   if (G_LIKELY (acceptfunc)) {
2427     /* we can call the function */
2428     result = acceptfunc (pad, caps);
2429     GST_DEBUG_OBJECT (pad, "acceptfunc returned %d", result);
2430   } else {
2431     /* Only null if the element explicitly unset it */
2432     result = gst_pad_acceptcaps_default (pad, caps);
2433     GST_DEBUG_OBJECT (pad, "default acceptcaps returned %d", result);
2434   }
2435   return result;
2436
2437 is_same_caps:
2438   {
2439     GST_DEBUG_OBJECT (pad, "pad had same caps");
2440     GST_OBJECT_UNLOCK (pad);
2441     return TRUE;
2442   }
2443 }
2444
2445 /**
2446  * gst_pad_peer_accept_caps:
2447  * @pad: a  #GstPad to check the peer of
2448  * @caps: a #GstCaps to check on the pad
2449  *
2450  * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2451  * returns TRUE.
2452  *
2453  * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
2454  */
2455 gboolean
2456 gst_pad_peer_accept_caps (GstPad * pad, GstCaps * caps)
2457 {
2458   GstPad *peerpad;
2459   gboolean result;
2460
2461   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2462
2463   GST_OBJECT_LOCK (pad);
2464
2465   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "peer accept caps of (%p)", pad);
2466
2467   peerpad = GST_PAD_PEER (pad);
2468   if (G_UNLIKELY (peerpad == NULL))
2469     goto no_peer;
2470
2471   gst_object_ref (peerpad);
2472   /* release lock before calling external methods but keep ref to pad */
2473   GST_OBJECT_UNLOCK (pad);
2474
2475   result = gst_pad_accept_caps (peerpad, caps);
2476
2477   gst_object_unref (peerpad);
2478
2479   return result;
2480
2481 no_peer:
2482   {
2483     GST_OBJECT_UNLOCK (pad);
2484     return TRUE;
2485   }
2486 }
2487
2488 /**
2489  * gst_pad_set_caps:
2490  * @pad: a  #GstPad to set the capabilities of.
2491  * @caps: a #GstCaps to set.
2492  *
2493  * Sets the capabilities of this pad. The caps must be fixed. Any previous
2494  * caps on the pad will be unreffed. This function refs the caps so you should
2495  * unref if as soon as you don't need it anymore.
2496  * It is possible to set NULL caps, which will make the pad unnegotiated
2497  * again.
2498  *
2499  * Returns: TRUE if the caps could be set. FALSE if the caps were not fixed
2500  * or bad parameters were provided to this function.
2501  *
2502  * MT safe.
2503  */
2504 gboolean
2505 gst_pad_set_caps (GstPad * pad, GstCaps * caps)
2506 {
2507   GstPadSetCapsFunction setcaps;
2508   GstCaps *existing;
2509
2510   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2511   g_return_val_if_fail (caps == NULL || gst_caps_is_fixed (caps), FALSE);
2512
2513   GST_OBJECT_LOCK (pad);
2514   existing = GST_PAD_CAPS (pad);
2515   if (existing == caps)
2516     goto was_ok;
2517
2518   if (gst_caps_is_equal (caps, existing))
2519     goto setting_same_caps;
2520
2521   setcaps = GST_PAD_SETCAPSFUNC (pad);
2522
2523   /* call setcaps function to configure the pad only if the
2524    * caps is not NULL */
2525   if (setcaps != NULL && caps) {
2526     if (!GST_PAD_IS_IN_SETCAPS (pad)) {
2527       GST_OBJECT_FLAG_SET (pad, GST_PAD_IN_SETCAPS);
2528       GST_OBJECT_UNLOCK (pad);
2529       if (!setcaps (pad, caps))
2530         goto could_not_set;
2531       GST_OBJECT_LOCK (pad);
2532       GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2533     } else {
2534       GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "pad was dispatching");
2535     }
2536   }
2537
2538   gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2539   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "caps %" GST_PTR_FORMAT, caps);
2540   GST_OBJECT_UNLOCK (pad);
2541
2542   g_object_notify (G_OBJECT (pad), "caps");
2543
2544   return TRUE;
2545
2546 was_ok:
2547   {
2548     GST_OBJECT_UNLOCK (pad);
2549     return TRUE;
2550   }
2551 setting_same_caps:
2552   {
2553     gst_caps_replace (&GST_PAD_CAPS (pad), caps);
2554     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2555         "caps %" GST_PTR_FORMAT " same as existing, updating ptr only", caps);
2556     GST_OBJECT_UNLOCK (pad);
2557     return TRUE;
2558   }
2559
2560   /* ERRORS */
2561 could_not_set:
2562   {
2563     GST_OBJECT_LOCK (pad);
2564     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_IN_SETCAPS);
2565     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2566         "caps %" GST_PTR_FORMAT " could not be set", caps);
2567     GST_OBJECT_UNLOCK (pad);
2568
2569     return FALSE;
2570   }
2571 }
2572
2573 static gboolean
2574 gst_pad_configure_sink (GstPad * pad, GstCaps * caps)
2575 {
2576   gboolean res;
2577
2578   /* See if pad accepts the caps */
2579   if (!gst_pad_accept_caps (pad, caps))
2580     goto not_accepted;
2581
2582   /* set caps on pad if call succeeds */
2583   res = gst_pad_set_caps (pad, caps);
2584   /* no need to unref the caps here, set_caps takes a ref and
2585    * our ref goes away when we leave this function. */
2586
2587   return res;
2588
2589 not_accepted:
2590   {
2591     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2592         "caps %" GST_PTR_FORMAT " not accepted", caps);
2593     return FALSE;
2594   }
2595 }
2596
2597 /* returns TRUE if the src pad could be configured to accept the given caps */
2598 static gboolean
2599 gst_pad_configure_src (GstPad * pad, GstCaps * caps, gboolean dosetcaps)
2600 {
2601   gboolean res;
2602
2603   if (dosetcaps) {
2604     /* See if pad accepts the caps */
2605     if (!gst_pad_accept_caps (pad, caps))
2606       goto not_accepted;
2607
2608     res = gst_pad_set_caps (pad, caps);
2609   } else {
2610     res = TRUE;
2611   }
2612   return res;
2613
2614 not_accepted:
2615   {
2616     GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2617         "caps %" GST_PTR_FORMAT " not accepted", caps);
2618     return FALSE;
2619   }
2620 }
2621
2622 /**
2623  * gst_pad_get_pad_template_caps:
2624  * @pad: a #GstPad to get the template capabilities from.
2625  *
2626  * Gets the capabilities for @pad's template.
2627  *
2628  * Returns: the #GstCaps of this pad template. If you intend to keep a
2629  * reference on the caps, make a copy (see gst_caps_copy ()).
2630  */
2631 const GstCaps *
2632 gst_pad_get_pad_template_caps (GstPad * pad)
2633 {
2634   static GstStaticCaps anycaps = GST_STATIC_CAPS ("ANY");
2635
2636   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2637
2638   if (GST_PAD_PAD_TEMPLATE (pad))
2639     return GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad));
2640
2641   return gst_static_caps_get (&anycaps);
2642 }
2643
2644 /**
2645  * gst_pad_get_peer:
2646  * @pad: a #GstPad to get the peer of.
2647  *
2648  * Gets the peer of @pad. This function refs the peer pad so
2649  * you need to unref it after use.
2650  *
2651  * Returns: the peer #GstPad. Unref after usage.
2652  *
2653  * MT safe.
2654  */
2655 GstPad *
2656 gst_pad_get_peer (GstPad * pad)
2657 {
2658   GstPad *result;
2659
2660   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2661
2662   GST_OBJECT_LOCK (pad);
2663   result = GST_PAD_PEER (pad);
2664   if (result)
2665     gst_object_ref (result);
2666   GST_OBJECT_UNLOCK (pad);
2667
2668   return result;
2669 }
2670
2671 /**
2672  * gst_pad_get_allowed_caps:
2673  * @pad: a #GstPad.
2674  *
2675  * Gets the capabilities of the allowed media types that can flow through
2676  * @pad and its peer.
2677  *
2678  * The allowed capabilities is calculated as the intersection of the results of
2679  * calling gst_pad_get_caps() on @pad and its peer. The caller owns a reference
2680  * on the resulting caps.
2681  *
2682  * Returns: the allowed #GstCaps of the pad link. Unref the caps when you no
2683  * longer need it. This function returns NULL when @pad has no peer.
2684  *
2685  * MT safe.
2686  */
2687 GstCaps *
2688 gst_pad_get_allowed_caps (GstPad * pad)
2689 {
2690   GstCaps *mycaps;
2691   GstCaps *caps;
2692   GstCaps *peercaps;
2693   GstPad *peer;
2694
2695   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2696
2697   GST_OBJECT_LOCK (pad);
2698
2699   peer = GST_PAD_PEER (pad);
2700   if (G_UNLIKELY (peer == NULL))
2701     goto no_peer;
2702
2703   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting allowed caps");
2704
2705   gst_object_ref (peer);
2706   GST_OBJECT_UNLOCK (pad);
2707   mycaps = gst_pad_get_caps (pad);
2708
2709   peercaps = gst_pad_get_caps (peer);
2710   gst_object_unref (peer);
2711
2712   caps = gst_caps_intersect (mycaps, peercaps);
2713   gst_caps_unref (peercaps);
2714   gst_caps_unref (mycaps);
2715
2716   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "allowed caps %" GST_PTR_FORMAT,
2717       caps);
2718
2719   return caps;
2720
2721 no_peer:
2722   {
2723     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2724     GST_OBJECT_UNLOCK (pad);
2725
2726     return NULL;
2727   }
2728 }
2729
2730 /**
2731  * gst_pad_get_negotiated_caps:
2732  * @pad: a #GstPad.
2733  *
2734  * Gets the capabilities of the media type that currently flows through @pad
2735  * and its peer.
2736  *
2737  * This function can be used on both src and sinkpads. Note that srcpads are
2738  * always negotiated before sinkpads so it is possible that the negotiated caps
2739  * on the srcpad do not match the negotiated caps of the peer.
2740  *
2741  * Returns: the negotiated #GstCaps of the pad link.  Unref the caps when
2742  * you no longer need it. This function returns NULL when the @pad has no
2743  * peer or is not negotiated yet.
2744  *
2745  * MT safe.
2746  */
2747 GstCaps *
2748 gst_pad_get_negotiated_caps (GstPad * pad)
2749 {
2750   GstCaps *caps;
2751   GstPad *peer;
2752
2753   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2754
2755   GST_OBJECT_LOCK (pad);
2756
2757   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2758     goto no_peer;
2759
2760   GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "getting negotiated caps");
2761
2762   caps = GST_PAD_CAPS (pad);
2763   if (caps)
2764     gst_caps_ref (caps);
2765   GST_OBJECT_UNLOCK (pad);
2766
2767   GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "negotiated caps %" GST_PTR_FORMAT,
2768       caps);
2769
2770   return caps;
2771
2772 no_peer:
2773   {
2774     GST_CAT_DEBUG_OBJECT (GST_CAT_PROPERTIES, pad, "no peer");
2775     GST_OBJECT_UNLOCK (pad);
2776
2777     return NULL;
2778   }
2779 }
2780
2781 /* calls the buffer_alloc function on the given pad */
2782 static GstFlowReturn
2783 gst_pad_buffer_alloc_unchecked (GstPad * pad, guint64 offset, gint size,
2784     GstCaps * caps, GstBuffer ** buf)
2785 {
2786   GstFlowReturn ret;
2787   GstPadBufferAllocFunction bufferallocfunc;
2788
2789   GST_OBJECT_LOCK (pad);
2790   /* when the pad is flushing we cannot give a buffer */
2791   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
2792     goto flushing;
2793
2794   bufferallocfunc = pad->bufferallocfunc;
2795
2796   if (offset == GST_BUFFER_OFFSET_NONE) {
2797     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2798         "calling bufferallocfunc &%s (@%p) for size %d offset NONE",
2799         GST_DEBUG_FUNCPTR_NAME (bufferallocfunc), bufferallocfunc, size);
2800   } else {
2801     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2802         "calling bufferallocfunc &%s (@%p) of for size %d offset %"
2803         G_GUINT64_FORMAT, GST_DEBUG_FUNCPTR_NAME (bufferallocfunc),
2804         bufferallocfunc, size, offset);
2805   }
2806   GST_OBJECT_UNLOCK (pad);
2807
2808   /* G_LIKELY for now since most elements don't implement a buffer alloc
2809    * function and there is no default alloc proxy function as this is usually
2810    * not possible. */
2811   if (G_LIKELY (bufferallocfunc == NULL))
2812     goto fallback;
2813
2814   ret = bufferallocfunc (pad, offset, size, caps, buf);
2815
2816   if (G_UNLIKELY (ret != GST_FLOW_OK))
2817     goto error;
2818
2819   /* no error, but NULL buffer means fallback to the default */
2820   if (G_UNLIKELY (*buf == NULL))
2821     goto fallback;
2822
2823   /* If the buffer alloc function didn't set up the caps like it should,
2824    * do it for it */
2825   if (G_UNLIKELY (caps && (GST_BUFFER_CAPS (*buf) == NULL))) {
2826     GST_WARNING_OBJECT (pad,
2827         "Buffer allocation function did not set caps. Setting");
2828     gst_buffer_set_caps (*buf, caps);
2829   }
2830   return ret;
2831
2832 flushing:
2833   {
2834     /* pad was flushing */
2835     GST_OBJECT_UNLOCK (pad);
2836     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad was flushing");
2837     return GST_FLOW_WRONG_STATE;
2838   }
2839 error:
2840   {
2841     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2842         "alloc function returned error (%d) %s", ret, gst_flow_get_name (ret));
2843     return ret;
2844   }
2845 fallback:
2846   {
2847     /* fallback case, allocate a buffer of our own, add pad caps. */
2848     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "fallback buffer alloc");
2849
2850     if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2851       GST_BUFFER_OFFSET (*buf) = offset;
2852       gst_buffer_set_caps (*buf, caps);
2853       return GST_FLOW_OK;
2854     } else {
2855       GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2856           "out of memory allocating %d bytes", size);
2857       return GST_FLOW_ERROR;
2858     }
2859   }
2860 }
2861
2862 /* FIXME 0.11: size should be unsigned */
2863 static GstFlowReturn
2864 gst_pad_alloc_buffer_full (GstPad * pad, guint64 offset, gint size,
2865     GstCaps * caps, GstBuffer ** buf, gboolean setcaps)
2866 {
2867   GstPad *peer;
2868   GstFlowReturn ret;
2869   GstCaps *newcaps;
2870   gboolean caps_changed;
2871
2872   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
2873   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
2874   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2875   g_return_val_if_fail (size >= 0, GST_FLOW_ERROR);
2876
2877   GST_DEBUG_OBJECT (pad, "offset %" G_GUINT64_FORMAT ", size %d, caps %"
2878       GST_PTR_FORMAT, offset, size, caps);
2879
2880   GST_OBJECT_LOCK (pad);
2881   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
2882     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
2883       goto flushed;
2884
2885   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
2886     goto no_peer;
2887
2888   gst_object_ref (peer);
2889   GST_OBJECT_UNLOCK (pad);
2890
2891   ret = gst_pad_buffer_alloc_unchecked (peer, offset, size, caps, buf);
2892   gst_object_unref (peer);
2893
2894   if (G_UNLIKELY (ret != GST_FLOW_OK))
2895     goto peer_error;
2896
2897   /* FIXME, move capnego this into a base class? */
2898   newcaps = GST_BUFFER_CAPS (*buf);
2899
2900   /* Lock for checking caps, pretty pointless as the _pad_push() function might
2901    * change it concurrently, one of the problems with automatic caps setting in
2902    * pad_alloc_and_set_caps. Worst case, if does a check too much, but only
2903    * when there is heavy renegotiation going on in both directions. */
2904   GST_OBJECT_LOCK (pad);
2905   caps_changed = newcaps && newcaps != GST_PAD_CAPS (pad);
2906   GST_OBJECT_UNLOCK (pad);
2907
2908   /* we got a new datatype on the pad, see if it can handle it */
2909   if (G_UNLIKELY (caps_changed)) {
2910     GST_DEBUG_OBJECT (pad,
2911         "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
2912         GST_PAD_CAPS (pad), newcaps, newcaps);
2913     if (G_UNLIKELY (!gst_pad_configure_src (pad, newcaps, setcaps)))
2914       goto not_negotiated;
2915   }
2916
2917   /* sanity check (only if caps are the same) */
2918   if (G_LIKELY (newcaps == caps) && G_UNLIKELY (GST_BUFFER_SIZE (*buf) < size))
2919     goto wrong_size_fallback;
2920
2921   return ret;
2922
2923 flushed:
2924   {
2925     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "pad block stopped by flush");
2926     GST_OBJECT_UNLOCK (pad);
2927     return ret;
2928   }
2929 no_peer:
2930   {
2931     /* pad has no peer */
2932     GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2933         "called bufferallocfunc but had no peer");
2934     GST_OBJECT_UNLOCK (pad);
2935     return GST_FLOW_NOT_LINKED;
2936   }
2937 peer_error:
2938   {
2939     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2940         "alloc function returned error %s", gst_flow_get_name (ret));
2941     return ret;
2942   }
2943 not_negotiated:
2944   {
2945     gst_buffer_unref (*buf);
2946     *buf = NULL;
2947     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
2948         "alloc function returned unacceptable buffer");
2949     return GST_FLOW_NOT_NEGOTIATED;
2950   }
2951 wrong_size_fallback:
2952   {
2953     GST_CAT_ERROR_OBJECT (GST_CAT_PADS, pad, "buffer returned by alloc "
2954         "function is too small (%u < %d), doing fallback buffer alloc",
2955         GST_BUFFER_SIZE (*buf), size);
2956
2957     gst_buffer_unref (*buf);
2958
2959     if ((*buf = gst_buffer_try_new_and_alloc (size))) {
2960       GST_BUFFER_OFFSET (*buf) = offset;
2961       gst_buffer_set_caps (*buf, caps);
2962       return GST_FLOW_OK;
2963     } else {
2964       GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2965           "out of memory allocating %d bytes", size);
2966       return GST_FLOW_ERROR;
2967     }
2968   }
2969 }
2970
2971 /**
2972  * gst_pad_alloc_buffer:
2973  * @pad: a source #GstPad
2974  * @offset: the offset of the new buffer in the stream
2975  * @size: the size of the new buffer
2976  * @caps: the caps of the new buffer
2977  * @buf: a newly allocated buffer
2978  *
2979  * Allocates a new, empty buffer optimized to push to pad @pad.  This
2980  * function only works if @pad is a source pad and has a peer.
2981  *
2982  * A new, empty #GstBuffer will be put in the @buf argument.
2983  * You need to check the caps of the buffer after performing this
2984  * function and renegotiate to the format if needed. If the caps changed, it is
2985  * possible that the buffer returned in @buf is not of the right size for the
2986  * new format, @buf needs to be unreffed and reallocated if this is the case.
2987  *
2988  * Returns: a result code indicating success of the operation. Any
2989  * result code other than #GST_FLOW_OK is an error and @buf should
2990  * not be used.
2991  * An error can occur if the pad is not connected or when the downstream
2992  * peer elements cannot provide an acceptable buffer.
2993  *
2994  * MT safe.
2995  */
2996
2997 /* FIXME 0.11: size should be unsigned */
2998 GstFlowReturn
2999 gst_pad_alloc_buffer (GstPad * pad, guint64 offset, gint size, GstCaps * caps,
3000     GstBuffer ** buf)
3001 {
3002   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, FALSE);
3003 }
3004
3005 /**
3006  * gst_pad_alloc_buffer_and_set_caps:
3007  * @pad: a source #GstPad
3008  * @offset: the offset of the new buffer in the stream
3009  * @size: the size of the new buffer
3010  * @caps: the caps of the new buffer
3011  * @buf: a newly allocated buffer
3012  *
3013  * In addition to the function gst_pad_alloc_buffer(), this function
3014  * automatically calls gst_pad_set_caps() when the caps of the
3015  * newly allocated buffer are different from the @pad caps.
3016  *
3017  * After a renegotiation, the size of the new buffer returned in @buf could
3018  * be of the wrong size for the new format and must be unreffed an reallocated
3019  * in that case.
3020  *
3021  * Returns: a result code indicating success of the operation. Any
3022  * result code other than #GST_FLOW_OK is an error and @buf should
3023  * not be used.
3024  * An error can occur if the pad is not connected or when the downstream
3025  * peer elements cannot provide an acceptable buffer.
3026  *
3027  * MT safe.
3028  */
3029
3030 /* FIXME 0.11: size should be unsigned */
3031 GstFlowReturn
3032 gst_pad_alloc_buffer_and_set_caps (GstPad * pad, guint64 offset, gint size,
3033     GstCaps * caps, GstBuffer ** buf)
3034 {
3035   return gst_pad_alloc_buffer_full (pad, offset, size, caps, buf, TRUE);
3036 }
3037
3038
3039 #ifndef GST_REMOVE_DEPRECATED
3040 typedef struct
3041 {
3042   GList *list;
3043   guint32 cookie;
3044 } IntLinkIterData;
3045
3046 static void
3047 int_link_iter_data_free (IntLinkIterData * data)
3048 {
3049   g_list_free (data->list);
3050   g_free (data);
3051 }
3052 #endif
3053
3054 static GstIteratorItem
3055 iterate_pad (GstIterator * it, GstPad * pad)
3056 {
3057   gst_object_ref (pad);
3058   return GST_ITERATOR_ITEM_PASS;
3059 }
3060
3061 /**
3062  * gst_pad_iterate_internal_links_default:
3063  * @pad: the #GstPad to get the internal links of.
3064  *
3065  * Iterate the list of pads to which the given pad is linked to inside of
3066  * the parent element.
3067  * This is the default handler, and thus returns an iterator of all of the
3068  * pads inside the parent element with opposite direction.
3069  *
3070  * The caller must free this iterator after use with gst_iterator_free().
3071  *
3072  * Returns: a #GstIterator of #GstPad, or NULL if @pad has no parent. Unref each
3073  * returned pad with gst_object_unref().
3074  *
3075  * Since: 0.10.21
3076  */
3077 GstIterator *
3078 gst_pad_iterate_internal_links_default (GstPad * pad)
3079 {
3080   GstIterator *res;
3081   GList **padlist;
3082   guint32 *cookie;
3083   GMutex *lock;
3084   gpointer owner;
3085   GstIteratorDisposeFunction dispose;
3086
3087   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3088
3089 #ifndef GST_REMOVE_DEPRECATED
3090   /* when we get here, the default handler for the iterate links is called,
3091    * which means that the user has not installed a custom one. We first check if
3092    * there is maybe a custom legacy function we can call. */
3093   if (GST_PAD_INTLINKFUNC (pad) &&
3094       GST_PAD_INTLINKFUNC (pad) != gst_pad_get_internal_links_default) {
3095     IntLinkIterData *data;
3096
3097     /* make an iterator for the list. We can't protect the list with a
3098      * cookie. If we would take the cookie of the parent element, we need to
3099      * have a parent, which is not required for GST_PAD_INTLINKFUNC(). We could
3100      * cache the per-pad list and invalidate the list when a new call to
3101      * INTLINKFUNC() returned a different list but then this would only work if
3102      * two concurrent iterators were used and the last iterator would still be
3103      * thread-unsafe. Just don't use this method anymore. */
3104     data = g_new0 (IntLinkIterData, 1);
3105     data->list = GST_PAD_INTLINKFUNC (pad) (pad);
3106     data->cookie = 0;
3107
3108     GST_WARNING_OBJECT (pad, "Making unsafe iterator");
3109
3110     cookie = &data->cookie;
3111     padlist = &data->list;
3112     owner = data;
3113     dispose = (GstIteratorDisposeFunction) int_link_iter_data_free;
3114     /* reuse the pad lock, it's all we have here */
3115     lock = GST_OBJECT_GET_LOCK (pad);
3116   } else
3117 #endif
3118   {
3119     GstElement *parent;
3120
3121     GST_OBJECT_LOCK (pad);
3122     parent = GST_PAD_PARENT (pad);
3123     if (!parent || !GST_IS_ELEMENT (parent))
3124       goto no_parent;
3125
3126     gst_object_ref (parent);
3127     GST_OBJECT_UNLOCK (pad);
3128
3129     if (pad->direction == GST_PAD_SRC)
3130       padlist = &parent->sinkpads;
3131     else
3132       padlist = &parent->srcpads;
3133
3134     GST_DEBUG_OBJECT (pad, "Making iterator");
3135
3136     cookie = &parent->pads_cookie;
3137     owner = parent;
3138     dispose = (GstIteratorDisposeFunction) gst_object_unref;
3139     lock = GST_OBJECT_GET_LOCK (parent);
3140   }
3141
3142   res = gst_iterator_new_list (GST_TYPE_PAD,
3143       lock, cookie, padlist, owner, (GstIteratorItemFunction) iterate_pad,
3144       dispose);
3145
3146   return res;
3147
3148   /* ERRORS */
3149 no_parent:
3150   {
3151     GST_OBJECT_UNLOCK (pad);
3152     GST_DEBUG_OBJECT (pad, "no parent element");
3153     return NULL;
3154   }
3155 }
3156
3157 /**
3158  * gst_pad_iterate_internal_links:
3159  * @pad: the GstPad to get the internal links of.
3160  *
3161  * Gets an iterator for the pads to which the given pad is linked to inside
3162  * of the parent element.
3163  *
3164  * Each #GstPad element yielded by the iterator will have its refcount increased,
3165  * so unref after use.
3166  *
3167  * Returns: a new #GstIterator of #GstPad or %NULL when the pad does not have an
3168  * iterator function configured. Use gst_iterator_free() after usage.
3169  *
3170  * Since: 0.10.21
3171  */
3172 GstIterator *
3173 gst_pad_iterate_internal_links (GstPad * pad)
3174 {
3175   GstIterator *res = NULL;
3176
3177   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3178
3179   if (GST_PAD_ITERINTLINKFUNC (pad))
3180     res = GST_PAD_ITERINTLINKFUNC (pad) (pad);
3181
3182   return res;
3183 }
3184
3185 #ifndef GST_REMOVE_DEPRECATED
3186 static void
3187 add_unref_pad_to_list (GstPad * pad, GList * list)
3188 {
3189   list = g_list_prepend (list, pad);
3190   gst_object_unref (pad);
3191 }
3192 #endif
3193
3194 /**
3195  * gst_pad_get_internal_links_default:
3196  * @pad: the #GstPad to get the internal links of.
3197  *
3198  * Gets a list of pads to which the given pad is linked to
3199  * inside of the parent element.
3200  * This is the default handler, and thus returns a list of all of the
3201  * pads inside the parent element with opposite direction.
3202  *
3203  * The caller must free this list after use with g_list_free().
3204  *
3205  * Returns: a newly allocated #GList of pads, or NULL if the pad has no parent.
3206  *
3207  * Not MT safe.
3208  *
3209  * Deprecated: This function does not ref the pads in the list so that they
3210  * could become invalid by the time the application accesses them. It's also
3211  * possible that the list changes while handling the pads, which the caller of
3212  * this function is unable to know. Use the thread-safe 
3213  * gst_pad_iterate_internal_links_default() instead.
3214  */
3215 #ifndef GST_REMOVE_DEPRECATED
3216 GList *
3217 gst_pad_get_internal_links_default (GstPad * pad)
3218 {
3219   GList *res = NULL;
3220   GstElement *parent;
3221
3222   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3223
3224   GST_WARNING_OBJECT (pad, "Unsafe internal links used");
3225
3226   /* when we get here, the default handler for get_internal_links is called,
3227    * which means that the user has not installed a custom one. We first check if
3228    * there is maybe a custom iterate function we can call. */
3229   if (GST_PAD_ITERINTLINKFUNC (pad) &&
3230       GST_PAD_ITERINTLINKFUNC (pad) != gst_pad_iterate_internal_links_default) {
3231     GstIterator *it;
3232     GstIteratorResult ires;
3233     gboolean done = FALSE;
3234
3235     it = gst_pad_iterate_internal_links (pad);
3236     /* loop over the iterator and put all elements into a list, we also
3237      * immediatly unref them, which is bad. */
3238     do {
3239       ires = gst_iterator_foreach (it, (GFunc) add_unref_pad_to_list, res);
3240       switch (ires) {
3241         case GST_ITERATOR_OK:
3242         case GST_ITERATOR_DONE:
3243         case GST_ITERATOR_ERROR:
3244           done = TRUE;
3245           break;
3246         case GST_ITERATOR_RESYNC:
3247           /* restart, discard previous list */
3248           gst_iterator_resync (it);
3249           g_list_free (res);
3250           res = NULL;
3251           break;
3252       }
3253     } while (!done);
3254
3255     gst_iterator_free (it);
3256   } else {
3257     /* lock pad, check and ref parent */
3258     GST_OBJECT_LOCK (pad);
3259     parent = GST_PAD_PARENT (pad);
3260     if (!parent || !GST_IS_ELEMENT (parent))
3261       goto no_parent;
3262
3263     parent = gst_object_ref (parent);
3264     GST_OBJECT_UNLOCK (pad);
3265
3266     /* now lock the parent while we copy the pads */
3267     GST_OBJECT_LOCK (parent);
3268     if (pad->direction == GST_PAD_SRC)
3269       res = g_list_copy (parent->sinkpads);
3270     else
3271       res = g_list_copy (parent->srcpads);
3272     GST_OBJECT_UNLOCK (parent);
3273
3274     gst_object_unref (parent);
3275   }
3276
3277   /* At this point pads can be changed and unreffed. Nothing we can do about it
3278    * because for compatibility reasons this function cannot ref the pads or
3279    * notify the app that the list changed. */
3280
3281   return res;
3282
3283 no_parent:
3284   {
3285     GST_DEBUG_OBJECT (pad, "no parent");
3286     GST_OBJECT_UNLOCK (pad);
3287     return NULL;
3288   }
3289 }
3290 #endif /* GST_REMOVE_DEPRECATED */
3291
3292 /**
3293  * gst_pad_get_internal_links:
3294  * @pad: the #GstPad to get the internal links of.
3295  *
3296  * Gets a list of pads to which the given pad is linked to
3297  * inside of the parent element.
3298  * The caller must free this list after use.
3299  *
3300  * Returns: a newly allocated #GList of pads, free with g_list_free().
3301  *
3302  * Not MT safe.
3303  * 
3304  * Deprecated: This function does not ref the pads in the list so that they
3305  * could become invalid by the time the application accesses them. It's also
3306  * possible that the list changes while handling the pads, which the caller of
3307  * this function is unable to know. Use the thread-safe 
3308  * gst_pad_iterate_internal_links() instead.
3309  */
3310 #ifndef GST_REMOVE_DEPRECATED
3311 GList *
3312 gst_pad_get_internal_links (GstPad * pad)
3313 {
3314   GList *res = NULL;
3315
3316   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3317
3318   GST_WARNING_OBJECT (pad, "Calling unsafe internal links");
3319
3320   if (GST_PAD_INTLINKFUNC (pad))
3321     res = GST_PAD_INTLINKFUNC (pad) (pad);
3322
3323   return res;
3324 }
3325 #endif /* GST_REMOVE_DEPRECATED */
3326
3327 static gboolean
3328 gst_pad_event_default_dispatch (GstPad * pad, GstEvent * event)
3329 {
3330   gboolean result = FALSE;
3331   GstIterator *iter;
3332   gboolean done = FALSE;
3333   gpointer item;
3334   GstPad *eventpad;
3335   GList *pushed_pads = NULL;
3336
3337   GST_INFO_OBJECT (pad, "Sending event %p (%s) to all internally linked pads",
3338       event, GST_EVENT_TYPE_NAME (event));
3339
3340   iter = gst_pad_iterate_internal_links (pad);
3341
3342   if (!iter)
3343     goto no_iter;
3344
3345   while (!done) {
3346     switch (gst_iterator_next (iter, &item)) {
3347       case GST_ITERATOR_OK:
3348         eventpad = GST_PAD (item);
3349
3350         /* if already pushed,  skip */
3351         if (g_list_find (pushed_pads, eventpad)) {
3352           gst_object_unref (item);
3353           break;
3354         }
3355
3356         if (GST_PAD_IS_SRC (eventpad)) {
3357           /* for each pad we send to, we should ref the event; it's up
3358            * to downstream to unref again when handled. */
3359           GST_LOG_OBJECT (pad, "Reffing and sending event %p (%s) to %s:%s",
3360               event, GST_EVENT_TYPE_NAME (event),
3361               GST_DEBUG_PAD_NAME (eventpad));
3362           gst_event_ref (event);
3363           result |= gst_pad_push_event (eventpad, event);
3364         } else {
3365           /* we only send the event on one pad, multi-sinkpad elements
3366            * should implement a handler */
3367           GST_LOG_OBJECT (pad, "sending event %p (%s) to one sink pad %s:%s",
3368               event, GST_EVENT_TYPE_NAME (event),
3369               GST_DEBUG_PAD_NAME (eventpad));
3370           result = gst_pad_push_event (eventpad, event);
3371           done = TRUE;
3372           event = NULL;
3373         }
3374
3375         pushed_pads = g_list_prepend (pushed_pads, eventpad);
3376
3377         gst_object_unref (item);
3378         break;
3379       case GST_ITERATOR_RESYNC:
3380         /* FIXME, if we want to reset the result value we need to remember which
3381          * pads pushed with which result */
3382         gst_iterator_resync (iter);
3383         break;
3384       case GST_ITERATOR_ERROR:
3385         GST_ERROR_OBJECT (pad, "Could not iterate over internally linked pads");
3386         done = TRUE;
3387         break;
3388       case GST_ITERATOR_DONE:
3389         done = TRUE;
3390         break;
3391     }
3392   }
3393   gst_iterator_free (iter);
3394
3395 no_iter:
3396
3397   /* If this is a sinkpad and we don't have pads to send the event to, we
3398    * return TRUE. This is so that when using the default handler on a sink
3399    * element, we don't fail to push it. */
3400   if (!pushed_pads)
3401     result = GST_PAD_IS_SINK (pad);
3402
3403   g_list_free (pushed_pads);
3404
3405   /* we handled the incoming event so we unref once */
3406   if (event) {
3407     GST_LOG_OBJECT (pad, "handled event %p, unreffing", event);
3408     gst_event_unref (event);
3409   }
3410
3411   return result;
3412 }
3413
3414 /**
3415  * gst_pad_event_default:
3416  * @pad: a #GstPad to call the default event handler on.
3417  * @event: the #GstEvent to handle.
3418  *
3419  * Invokes the default event handler for the given pad. End-of-stream and
3420  * discontinuity events are handled specially, and then the event is sent to all
3421  * pads internally linked to @pad. Note that if there are many possible sink
3422  * pads that are internally linked to @pad, only one will be sent an event.
3423  * Multi-sinkpad elements should implement custom event handlers.
3424  *
3425  * Returns: TRUE if the event was sent succesfully.
3426  */
3427 gboolean
3428 gst_pad_event_default (GstPad * pad, GstEvent * event)
3429 {
3430   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3431   g_return_val_if_fail (event != NULL, FALSE);
3432
3433   switch (GST_EVENT_TYPE (event)) {
3434     case GST_EVENT_EOS:
3435     {
3436       GST_DEBUG_OBJECT (pad, "pausing task because of eos");
3437       gst_pad_pause_task (pad);
3438     }
3439       /* fall thru */
3440     default:
3441       break;
3442   }
3443
3444   return gst_pad_event_default_dispatch (pad, event);
3445 }
3446
3447 /**
3448  * gst_pad_dispatcher:
3449  * @pad: a #GstPad to dispatch.
3450  * @dispatch: the #GstDispatcherFunction to call.
3451  * @data: gpointer user data passed to the dispatcher function.
3452  *
3453  * Invokes the given dispatcher function on each respective peer of
3454  * all pads that are internally linked to the given pad.
3455  * The GstPadDispatcherFunction should return TRUE when no further pads
3456  * need to be processed.
3457  *
3458  * Returns: TRUE if one of the dispatcher functions returned TRUE.
3459  */
3460 gboolean
3461 gst_pad_dispatcher (GstPad * pad, GstPadDispatcherFunction dispatch,
3462     gpointer data)
3463 {
3464   gboolean res = FALSE;
3465   GstIterator *iter = NULL;
3466   gboolean done = FALSE;
3467   gpointer item;
3468
3469   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3470   g_return_val_if_fail (dispatch != NULL, FALSE);
3471
3472   iter = gst_pad_iterate_internal_links (pad);
3473
3474   if (!iter)
3475     goto no_iter;
3476
3477   while (!done) {
3478     switch (gst_iterator_next (iter, &item)) {
3479       case GST_ITERATOR_OK:
3480       {
3481         GstPad *int_pad = GST_PAD_CAST (item);
3482         GstPad *int_peer = gst_pad_get_peer (int_pad);
3483
3484         if (int_peer) {
3485           GST_DEBUG_OBJECT (int_pad, "dispatching to peer %s:%s",
3486               GST_DEBUG_PAD_NAME (int_peer));
3487           done = res = dispatch (int_peer, data);
3488           gst_object_unref (int_peer);
3489         } else {
3490           GST_DEBUG_OBJECT (int_pad, "no peer");
3491         }
3492       }
3493         gst_object_unref (item);
3494         break;
3495       case GST_ITERATOR_RESYNC:
3496         gst_iterator_resync (iter);
3497         break;
3498       case GST_ITERATOR_ERROR:
3499         done = TRUE;
3500         GST_ERROR_OBJECT (pad, "Could not iterate internally linked pads");
3501         break;
3502       case GST_ITERATOR_DONE:
3503         done = TRUE;
3504         break;
3505     }
3506   }
3507   gst_iterator_free (iter);
3508
3509   GST_DEBUG_OBJECT (pad, "done, result %d", res);
3510
3511 no_iter:
3512
3513   return res;
3514 }
3515
3516 /**
3517  * gst_pad_query:
3518  * @pad: a #GstPad to invoke the default query on.
3519  * @query: the #GstQuery to perform.
3520  *
3521  * Dispatches a query to a pad. The query should have been allocated by the
3522  * caller via one of the type-specific allocation functions in gstquery.h. The
3523  * element is responsible for filling the query with an appropriate response,
3524  * which should then be parsed with a type-specific query parsing function.
3525  *
3526  * Again, the caller is responsible for both the allocation and deallocation of
3527  * the query structure.
3528  *
3529  * Returns: TRUE if the query could be performed.
3530  */
3531 gboolean
3532 gst_pad_query (GstPad * pad, GstQuery * query)
3533 {
3534   GstPadQueryFunction func;
3535
3536   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3537   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3538
3539   GST_DEBUG_OBJECT (pad, "sending query %p", query);
3540
3541   if ((func = GST_PAD_QUERYFUNC (pad)) == NULL)
3542     goto no_func;
3543
3544   return func (pad, query);
3545
3546 no_func:
3547   {
3548     GST_DEBUG_OBJECT (pad, "had no query function");
3549     return FALSE;
3550   }
3551 }
3552
3553 /**
3554  * gst_pad_peer_query:
3555  * @pad: a #GstPad to invoke the peer query on.
3556  * @query: the #GstQuery to perform.
3557  *
3558  * Performs gst_pad_query() on the peer of @pad.
3559  *
3560  * The caller is responsible for both the allocation and deallocation of
3561  * the query structure.
3562  *
3563  * Returns: TRUE if the query could be performed. This function returns %FALSE
3564  * if @pad has no peer.
3565  *
3566  * Since: 0.10.15
3567  */
3568 gboolean
3569 gst_pad_peer_query (GstPad * pad, GstQuery * query)
3570 {
3571   GstPad *peerpad;
3572   gboolean result;
3573
3574   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3575   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
3576
3577   GST_OBJECT_LOCK (pad);
3578
3579   GST_DEBUG_OBJECT (pad, "peer query");
3580
3581   peerpad = GST_PAD_PEER (pad);
3582   if (G_UNLIKELY (peerpad == NULL))
3583     goto no_peer;
3584
3585   gst_object_ref (peerpad);
3586   GST_OBJECT_UNLOCK (pad);
3587
3588   result = gst_pad_query (peerpad, query);
3589
3590   gst_object_unref (peerpad);
3591
3592   return result;
3593
3594   /* ERRORS */
3595 no_peer:
3596   {
3597     GST_WARNING_OBJECT (pad, "pad has no peer");
3598     GST_OBJECT_UNLOCK (pad);
3599     return FALSE;
3600   }
3601 }
3602
3603 /**
3604  * gst_pad_query_default:
3605  * @pad: a #GstPad to call the default query handler on.
3606  * @query: the #GstQuery to handle.
3607  *
3608  * Invokes the default query handler for the given pad.
3609  * The query is sent to all pads internally linked to @pad. Note that
3610  * if there are many possible sink pads that are internally linked to
3611  * @pad, only one will be sent the query.
3612  * Multi-sinkpad elements should implement custom query handlers.
3613  *
3614  * Returns: TRUE if the query was performed succesfully.
3615  */
3616 gboolean
3617 gst_pad_query_default (GstPad * pad, GstQuery * query)
3618 {
3619   switch (GST_QUERY_TYPE (query)) {
3620     case GST_QUERY_POSITION:
3621     case GST_QUERY_SEEKING:
3622     case GST_QUERY_FORMATS:
3623     case GST_QUERY_LATENCY:
3624     case GST_QUERY_JITTER:
3625     case GST_QUERY_RATE:
3626     case GST_QUERY_CONVERT:
3627     default:
3628       return gst_pad_dispatcher
3629           (pad, (GstPadDispatcherFunction) gst_pad_query, query);
3630   }
3631 }
3632
3633 #ifndef GST_DISABLE_LOADSAVE
3634 /* FIXME: why isn't this on a GstElement ? */
3635 /**
3636  * gst_pad_load_and_link:
3637  * @self: an #xmlNodePtr to read the description from.
3638  * @parent: the #GstObject element that owns the pad.
3639  *
3640  * Reads the pad definition from the XML node and links the given pad
3641  * in the element to a pad of an element up in the hierarchy.
3642  */
3643 void
3644 gst_pad_load_and_link (xmlNodePtr self, GstObject * parent)
3645 {
3646   xmlNodePtr field = self->xmlChildrenNode;
3647   GstPad *pad = NULL, *targetpad;
3648   gchar *peer = NULL;
3649   gchar **split;
3650   GstElement *target;
3651   GstObject *grandparent;
3652   gchar *name = NULL;
3653
3654   while (field) {
3655     if (!strcmp ((char *) field->name, "name")) {
3656       name = (gchar *) xmlNodeGetContent (field);
3657       pad = gst_element_get_static_pad (GST_ELEMENT (parent), name);
3658       if (!pad)
3659         pad = gst_element_get_request_pad (GST_ELEMENT (parent), name);
3660       g_free (name);
3661     } else if (!strcmp ((char *) field->name, "peer")) {
3662       peer = (gchar *) xmlNodeGetContent (field);
3663     }
3664     field = field->next;
3665   }
3666   g_return_if_fail (pad != NULL);
3667
3668   if (peer == NULL)
3669     return;
3670
3671   split = g_strsplit (peer, ".", 2);
3672
3673   if (split[0] == NULL || split[1] == NULL) {
3674     GST_CAT_DEBUG_OBJECT (GST_CAT_XML, pad,
3675         "Could not parse peer '%s', leaving unlinked", peer);
3676
3677     g_free (peer);
3678     return;
3679   }
3680   g_free (peer);
3681
3682   g_return_if_fail (split[0] != NULL);
3683   g_return_if_fail (split[1] != NULL);
3684
3685   grandparent = gst_object_get_parent (parent);
3686
3687   if (grandparent && GST_IS_BIN (grandparent)) {
3688     target = gst_bin_get_by_name_recurse_up (GST_BIN (grandparent), split[0]);
3689   } else
3690     goto cleanup;
3691
3692   if (target == NULL)
3693     goto cleanup;
3694
3695   targetpad = gst_element_get_static_pad (target, split[1]);
3696   if (!pad)
3697     targetpad = gst_element_get_request_pad (target, split[1]);
3698
3699   if (targetpad == NULL)
3700     goto cleanup;
3701
3702   gst_pad_link (pad, targetpad);
3703
3704 cleanup:
3705   g_strfreev (split);
3706 }
3707
3708 /**
3709  * gst_pad_save_thyself:
3710  * @pad: a #GstPad to save.
3711  * @parent: the parent #xmlNodePtr to save the description in.
3712  *
3713  * Saves the pad into an xml representation.
3714  *
3715  * Returns: the #xmlNodePtr representation of the pad.
3716  */
3717 static xmlNodePtr
3718 gst_pad_save_thyself (GstObject * object, xmlNodePtr parent)
3719 {
3720   GstPad *pad;
3721   GstPad *peer;
3722
3723   g_return_val_if_fail (GST_IS_PAD (object), NULL);
3724
3725   pad = GST_PAD (object);
3726
3727   xmlNewChild (parent, NULL, (xmlChar *) "name",
3728       (xmlChar *) GST_PAD_NAME (pad));
3729
3730   if (GST_PAD_IS_SRC (pad)) {
3731     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "source");
3732   } else if (GST_PAD_IS_SINK (pad)) {
3733     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "sink");
3734   } else {
3735     xmlNewChild (parent, NULL, (xmlChar *) "direction", (xmlChar *) "unknown");
3736   }
3737
3738   if (GST_PAD_PEER (pad) != NULL) {
3739     gchar *content;
3740
3741     peer = GST_PAD_PEER (pad);
3742     /* first check to see if the peer's parent's parent is the same */
3743     /* we just save it off */
3744     content = g_strdup_printf ("%s.%s",
3745         GST_OBJECT_NAME (GST_PAD_PARENT (peer)), GST_PAD_NAME (peer));
3746     xmlNewChild (parent, NULL, (xmlChar *) "peer", (xmlChar *) content);
3747     g_free (content);
3748   } else
3749     xmlNewChild (parent, NULL, (xmlChar *) "peer", NULL);
3750
3751   return parent;
3752 }
3753
3754 #if 0
3755 /**
3756  * gst_ghost_pad_save_thyself:
3757  * @pad: a ghost #GstPad to save.
3758  * @parent: the parent #xmlNodePtr to save the description in.
3759  *
3760  * Saves the ghost pad into an xml representation.
3761  *
3762  * Returns: the #xmlNodePtr representation of the pad.
3763  */
3764 xmlNodePtr
3765 gst_ghost_pad_save_thyself (GstPad * pad, xmlNodePtr parent)
3766 {
3767   xmlNodePtr self;
3768
3769   g_return_val_if_fail (GST_IS_GHOST_PAD (pad), NULL);
3770
3771   self = xmlNewChild (parent, NULL, (xmlChar *) "ghostpad", NULL);
3772   xmlNewChild (self, NULL, (xmlChar *) "name", (xmlChar *) GST_PAD_NAME (pad));
3773   xmlNewChild (self, NULL, (xmlChar *) "parent",
3774       (xmlChar *) GST_OBJECT_NAME (GST_PAD_PARENT (pad)));
3775
3776   /* FIXME FIXME FIXME! */
3777
3778   return self;
3779 }
3780 #endif /* 0 */
3781 #endif /* GST_DISABLE_LOADSAVE */
3782
3783 /*
3784  * should be called with pad OBJECT_LOCK and STREAM_LOCK held.
3785  * GST_PAD_IS_BLOCKED (pad) == TRUE when this function is
3786  * called.
3787  *
3788  * This function performs the pad blocking when an event, buffer push
3789  * or buffer_alloc is performed on a _SRC_ pad. It blocks the
3790  * streaming thread after informing the pad has been blocked.
3791  *
3792  * An application can with this method wait and block any streaming
3793  * thread and perform operations such as seeking or linking.
3794  *
3795  * Two methods are available for notifying the application of the
3796  * block:
3797  * - the callback method, which happens in the STREAMING thread with
3798  *   the STREAM_LOCK held. With this method, the most useful way of
3799  *   dealing with the callback is to post a message to the main thread
3800  *   where the pad block can then be handled outside of the streaming
3801  *   thread. With the last method one can perform all operations such
3802  *   as doing a state change, linking, unblocking, seeking etc on the
3803  *   pad.
3804  * - the GCond signal method, which makes any thread unblock when
3805  *   the pad block happens.
3806  *
3807  * During the actual blocking state, the GST_PAD_BLOCKING flag is set.
3808  * The GST_PAD_BLOCKING flag is unset when the pad was unblocked.
3809  *
3810  * MT safe.
3811  */
3812 static GstFlowReturn
3813 handle_pad_block (GstPad * pad)
3814 {
3815   GstPadBlockCallback callback;
3816   gpointer user_data;
3817   GstFlowReturn ret = GST_FLOW_OK;
3818
3819   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "signal block taken");
3820
3821   /* flushing, don't bother trying to block and return WRONG_STATE
3822    * right away */
3823   if (GST_PAD_IS_FLUSHING (pad))
3824     goto flushingnonref;
3825
3826   /* we grab an extra ref for the callbacks, this is probably not
3827    * needed (callback code does not have a ref and cannot unref). I
3828    * think this was done to make it possible to unref the element in
3829    * the callback, which is in the end totally impossible as it
3830    * requires grabbing the STREAM_LOCK and OBJECT_LOCK which are
3831    * all taken when calling this function. */
3832   gst_object_ref (pad);
3833
3834   while (GST_PAD_IS_BLOCKED (pad)) {
3835     do {
3836       /* we either have a callback installed to notify the block or
3837        * some other thread is doing a GCond wait. */
3838       callback = pad->block_callback;
3839       pad->abidata.ABI.block_callback_called = TRUE;
3840       if (callback) {
3841         /* there is a callback installed, call it. We release the
3842          * lock so that the callback can do something usefull with the
3843          * pad */
3844         user_data = pad->block_data;
3845         GST_OBJECT_UNLOCK (pad);
3846         callback (pad, TRUE, user_data);
3847         GST_OBJECT_LOCK (pad);
3848
3849         /* we released the lock, recheck flushing */
3850         if (GST_PAD_IS_FLUSHING (pad))
3851           goto flushing;
3852       } else {
3853         /* no callback, signal the thread that is doing a GCond wait
3854          * if any. */
3855         GST_PAD_BLOCK_BROADCAST (pad);
3856       }
3857     } while (pad->abidata.ABI.block_callback_called == FALSE
3858         && GST_PAD_IS_BLOCKED (pad));
3859
3860     /* OBJECT_LOCK could have been released when we did the callback, which
3861      * then could have made the pad unblock so we need to check the blocking
3862      * condition again.   */
3863     if (!GST_PAD_IS_BLOCKED (pad))
3864       break;
3865
3866     /* now we block the streaming thread. It can be unlocked when we
3867      * deactivate the pad (which will also set the FLUSHING flag) or
3868      * when the pad is unblocked. A flushing event will also unblock
3869      * the pad after setting the FLUSHING flag. */
3870     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
3871         "Waiting to be unblocked or set flushing");
3872     GST_OBJECT_FLAG_SET (pad, GST_PAD_BLOCKING);
3873     GST_PAD_BLOCK_WAIT (pad);
3874     GST_OBJECT_FLAG_UNSET (pad, GST_PAD_BLOCKING);
3875
3876     /* see if we got unblocked by a flush or not */
3877     if (GST_PAD_IS_FLUSHING (pad))
3878       goto flushing;
3879   }
3880
3881   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "got unblocked");
3882
3883   /* when we get here, the pad is unblocked again and we perform
3884    * the needed unblock code. */
3885   callback = pad->block_callback;
3886   if (callback) {
3887     /* we need to call the callback */
3888     user_data = pad->block_data;
3889     GST_OBJECT_UNLOCK (pad);
3890     callback (pad, FALSE, user_data);
3891     GST_OBJECT_LOCK (pad);
3892   } else {
3893     /* we need to signal the thread waiting on the GCond */
3894     GST_PAD_BLOCK_BROADCAST (pad);
3895   }
3896
3897   gst_object_unref (pad);
3898
3899   return ret;
3900
3901 flushingnonref:
3902   {
3903     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad was flushing");
3904     return GST_FLOW_WRONG_STATE;
3905   }
3906 flushing:
3907   {
3908     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad, "pad became flushing");
3909     gst_object_unref (pad);
3910     return GST_FLOW_WRONG_STATE;
3911   }
3912 }
3913
3914 /**********************************************************************
3915  * Data passing functions
3916  */
3917
3918 static gboolean
3919 gst_pad_emit_have_data_signal (GstPad * pad, GstMiniObject * obj)
3920 {
3921   GValue ret = { 0 };
3922   GValue args[2] = { {0}, {0} };
3923   gboolean res;
3924   GQuark detail;
3925
3926   /* init */
3927   g_value_init (&ret, G_TYPE_BOOLEAN);
3928   g_value_set_boolean (&ret, TRUE);
3929   g_value_init (&args[0], GST_TYPE_PAD);
3930   g_value_set_object (&args[0], pad);
3931   g_value_init (&args[1], GST_TYPE_MINI_OBJECT);
3932   gst_value_set_mini_object (&args[1], obj);
3933
3934   if (GST_IS_EVENT (obj))
3935     detail = event_quark;
3936   else
3937     detail = buffer_quark;
3938
3939   /* actually emit */
3940   g_signal_emitv (args, gst_pad_signals[PAD_HAVE_DATA], detail, &ret);
3941   res = g_value_get_boolean (&ret);
3942
3943   /* clean up */
3944   g_value_unset (&ret);
3945   g_value_unset (&args[0]);
3946   g_value_unset (&args[1]);
3947
3948   return res;
3949 }
3950
3951 static void
3952 gst_pad_data_unref (gboolean is_buffer, void *data)
3953 {
3954   if (G_LIKELY (is_buffer)) {
3955     gst_buffer_unref (data);
3956   } else {
3957     gst_buffer_list_unref (data);
3958   }
3959 }
3960
3961 static GstCaps *
3962 gst_pad_data_get_caps (gboolean is_buffer, void *data)
3963 {
3964   GstCaps *caps;
3965
3966   if (G_LIKELY (is_buffer)) {
3967     caps = GST_BUFFER_CAPS (data);
3968   } else {
3969     GstBufferListIterator *it;
3970     GstBuffer *buf;
3971
3972     caps = NULL;
3973     it = gst_buffer_list_iterate (GST_BUFFER_LIST_CAST (data));
3974     if (gst_buffer_list_iterator_next_group (it)) {
3975       buf = gst_buffer_list_iterator_next (it);
3976       if (buf != NULL) {
3977         caps = GST_BUFFER_CAPS (buf);
3978       }
3979     }
3980     gst_buffer_list_iterator_free (it);
3981   }
3982   return caps;
3983 }
3984
3985 /* this is the chain function that does not perform the additional argument
3986  * checking for that little extra speed.
3987  */
3988 static inline GstFlowReturn
3989 gst_pad_chain_data_unchecked (GstPad * pad, gboolean is_buffer, void *data)
3990 {
3991   GstCaps *caps;
3992   gboolean caps_changed;
3993   GstFlowReturn ret;
3994   gboolean emit_signal;
3995
3996   GST_PAD_STREAM_LOCK (pad);
3997
3998   GST_OBJECT_LOCK (pad);
3999   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4000     goto flushing;
4001
4002   caps = gst_pad_data_get_caps (is_buffer, data);
4003   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4004
4005   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4006   GST_OBJECT_UNLOCK (pad);
4007
4008   /* see if the signal should be emited, we emit before caps nego as
4009    * we might drop the buffer and do capsnego for nothing. */
4010   if (G_UNLIKELY (emit_signal)) {
4011     if (G_LIKELY (is_buffer)) {
4012       if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4013         goto dropping;
4014     } else {
4015       /* chain all groups in the buffer list one by one to avoid problems with
4016        * buffer probes that push buffers or events */
4017       goto chain_groups;
4018     }
4019   }
4020
4021   /* we got a new datatype on the pad, see if it can handle it */
4022   if (G_UNLIKELY (caps_changed)) {
4023     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4024     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4025       goto not_negotiated;
4026   }
4027
4028   /* NOTE: we read the chainfunc unlocked.
4029    * we cannot hold the lock for the pad so we might send
4030    * the data to the wrong function. This is not really a
4031    * problem since functions are assigned at creation time
4032    * and don't change that often... */
4033   if (G_LIKELY (is_buffer)) {
4034     GstPadChainFunction chainfunc;
4035
4036     if (G_UNLIKELY ((chainfunc = GST_PAD_CHAINFUNC (pad)) == NULL))
4037       goto no_function;
4038
4039     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4040         "calling chainfunction &%s", GST_DEBUG_FUNCPTR_NAME (chainfunc));
4041
4042     ret = chainfunc (pad, GST_BUFFER_CAST (data));
4043
4044     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4045         "called chainfunction &%s, returned %s",
4046         GST_DEBUG_FUNCPTR_NAME (chainfunc), gst_flow_get_name (ret));
4047   } else {
4048     GstPadChainListFunction chainlistfunc;
4049
4050     if (G_UNLIKELY ((chainlistfunc = GST_PAD_CHAINLISTFUNC (pad)) == NULL))
4051       goto chain_groups;
4052
4053     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4054         "calling chainlistfunction &%s",
4055         GST_DEBUG_FUNCPTR_NAME (chainlistfunc));
4056
4057     ret = chainlistfunc (pad, GST_BUFFER_LIST_CAST (data));
4058
4059     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4060         "called chainlistfunction &%s, returned %s",
4061         GST_DEBUG_FUNCPTR_NAME (chainlistfunc), gst_flow_get_name (ret));
4062   }
4063
4064   GST_PAD_STREAM_UNLOCK (pad);
4065
4066   return ret;
4067
4068 chain_groups:
4069   {
4070     GstBufferList *list;
4071     GstBufferListIterator *it;
4072     GstBuffer *group;
4073
4074     GST_PAD_STREAM_UNLOCK (pad);
4075
4076     GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
4077
4078     list = GST_BUFFER_LIST_CAST (data);
4079     it = gst_buffer_list_iterate (list);
4080
4081     ret = GST_FLOW_OK;
4082     if (gst_buffer_list_iterator_next_group (it)) {
4083       do {
4084         group = gst_buffer_list_iterator_merge_group (it);
4085         if (group == NULL) {
4086           group = gst_buffer_new ();
4087           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4088         } else {
4089           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
4090         }
4091         ret = gst_pad_chain_data_unchecked (pad, TRUE, group);
4092       } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4093     } else {
4094       GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
4095       ret = gst_pad_chain_data_unchecked (pad, TRUE, gst_buffer_new ());
4096     }
4097
4098     gst_buffer_list_iterator_free (it);
4099     gst_buffer_list_unref (list);
4100
4101     return ret;
4102   }
4103
4104   /* ERRORS */
4105 flushing:
4106   {
4107     gst_pad_data_unref (is_buffer, data);
4108     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4109         "pushing, but pad was flushing");
4110     GST_OBJECT_UNLOCK (pad);
4111     GST_PAD_STREAM_UNLOCK (pad);
4112     return GST_FLOW_WRONG_STATE;
4113   }
4114 dropping:
4115   {
4116     gst_pad_data_unref (is_buffer, data);
4117     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4118     GST_PAD_STREAM_UNLOCK (pad);
4119     return GST_FLOW_OK;
4120   }
4121 not_negotiated:
4122   {
4123     gst_pad_data_unref (is_buffer, data);
4124     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4125         "pushing data but pad did not accept");
4126     GST_PAD_STREAM_UNLOCK (pad);
4127     return GST_FLOW_NOT_NEGOTIATED;
4128   }
4129 no_function:
4130   {
4131     gst_pad_data_unref (is_buffer, data);
4132     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4133         "pushing, but not chainhandler");
4134     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4135         ("push on pad %s:%s but it has no chainfunction",
4136             GST_DEBUG_PAD_NAME (pad)));
4137     GST_PAD_STREAM_UNLOCK (pad);
4138     return GST_FLOW_NOT_SUPPORTED;
4139   }
4140 }
4141
4142 /**
4143  * gst_pad_chain:
4144  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4145  * @buffer: the #GstBuffer to send, return GST_FLOW_ERROR if not.
4146  *
4147  * Chain a buffer to @pad.
4148  *
4149  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4150  *
4151  * If the caps on @buffer are different from the current caps on @pad, this
4152  * function will call any setcaps function (see gst_pad_set_setcaps_function())
4153  * installed on @pad. If the new caps are not acceptable for @pad, this
4154  * function returns #GST_FLOW_NOT_NEGOTIATED.
4155  *
4156  * The function proceeds calling the chain function installed on @pad (see
4157  * gst_pad_set_chain_function()) and the return value of that function is
4158  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4159  * chain function.
4160  *
4161  * In all cases, success or failure, the caller loses its reference to @buffer
4162  * after calling this function.
4163  *
4164  * Returns: a #GstFlowReturn from the pad.
4165  *
4166  * MT safe.
4167  */
4168 GstFlowReturn
4169 gst_pad_chain (GstPad * pad, GstBuffer * buffer)
4170 {
4171   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4172   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4173   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4174
4175   return gst_pad_chain_data_unchecked (pad, TRUE, buffer);
4176 }
4177
4178 /**
4179  * gst_pad_chain_list:
4180  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4181  * @list: the #GstBufferList to send, return GST_FLOW_ERROR if not.
4182  *
4183  * Chain a bufferlist to @pad.
4184  *
4185  * The function returns #GST_FLOW_WRONG_STATE if the pad was flushing.
4186  *
4187  * If the caps on the first buffer of @list are different from the current
4188  * caps on @pad, this function will call any setcaps function
4189  * (see gst_pad_set_setcaps_function()) installed on @pad. If the new caps
4190  * are not acceptable for @pad, this function returns #GST_FLOW_NOT_NEGOTIATED.
4191  *
4192  * The function proceeds calling the chainlist function installed on @pad (see
4193  * gst_pad_set_chain_list_function()) and the return value of that function is
4194  * returned to the caller. #GST_FLOW_NOT_SUPPORTED is returned if @pad has no
4195  * chainlist function.
4196  *
4197  * In all cases, success or failure, the caller loses its reference to @list
4198  * after calling this function.
4199  *
4200  * Returns: a #GstFlowReturn from the pad.
4201  *
4202  * Since: 0.10.24
4203  *
4204  * MT safe.
4205  */
4206 GstFlowReturn
4207 gst_pad_chain_list (GstPad * pad, GstBufferList * list)
4208 {
4209   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4210   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4211   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4212
4213   return gst_pad_chain_data_unchecked (pad, FALSE, list);
4214 }
4215
4216 static GstFlowReturn
4217 gst_pad_push_data (GstPad * pad, gboolean is_buffer, void *data)
4218 {
4219   GstPad *peer;
4220   GstFlowReturn ret;
4221   GstCaps *caps;
4222   gboolean caps_changed;
4223
4224   GST_OBJECT_LOCK (pad);
4225
4226   /* FIXME: this check can go away; pad_set_blocked could be implemented with
4227    * probes completely or probes with an extended pad block. */
4228   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4229     if ((ret = handle_pad_block (pad)) != GST_FLOW_OK)
4230       goto flushed;
4231
4232   /* we emit signals on the pad arg, the peer will have a chance to
4233    * emit in the _chain() function */
4234   if (G_UNLIKELY (GST_PAD_DO_BUFFER_SIGNALS (pad) > 0)) {
4235     /* unlock before emitting */
4236     GST_OBJECT_UNLOCK (pad);
4237
4238     if (G_LIKELY (is_buffer)) {
4239       /* if the signal handler returned FALSE, it means we should just drop the
4240        * buffer */
4241       if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (data)))
4242         goto dropped;
4243     } else {
4244       /* push all buffers in the list */
4245       goto push_groups;
4246     }
4247     GST_OBJECT_LOCK (pad);
4248   }
4249
4250   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4251     goto not_linked;
4252
4253   /* Before pushing the buffer to the peer pad, ensure that caps
4254    * are set on this pad */
4255   caps = gst_pad_data_get_caps (is_buffer, data);
4256   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4257
4258   /* take ref to peer pad before releasing the lock */
4259   gst_object_ref (peer);
4260
4261   GST_OBJECT_UNLOCK (pad);
4262
4263   /* we got a new datatype from the pad, it had better handle it */
4264   if (G_UNLIKELY (caps_changed)) {
4265     GST_DEBUG_OBJECT (pad,
4266         "caps changed from %" GST_PTR_FORMAT " to %p %" GST_PTR_FORMAT,
4267         GST_PAD_CAPS (pad), caps, caps);
4268     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4269       goto not_negotiated;
4270   }
4271
4272   ret = gst_pad_chain_data_unchecked (peer, is_buffer, data);
4273
4274   gst_object_unref (peer);
4275
4276   return ret;
4277
4278 push_groups:
4279   {
4280     GstBufferList *list;
4281     GstBufferListIterator *it;
4282     GstBuffer *group;
4283
4284     GST_INFO_OBJECT (pad, "pushing each group in list as a merged buffer");
4285
4286     list = GST_BUFFER_LIST_CAST (data);
4287     it = gst_buffer_list_iterate (list);
4288
4289     ret = GST_FLOW_OK;
4290     if (gst_buffer_list_iterator_next_group (it)) {
4291       do {
4292         group = gst_buffer_list_iterator_merge_group (it);
4293         if (group == NULL) {
4294           group = gst_buffer_new ();
4295           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4296         } else {
4297           GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing group");
4298         }
4299         ret = gst_pad_push_data (pad, TRUE, group);
4300       } while (ret == GST_FLOW_OK && gst_buffer_list_iterator_next_group (it));
4301     } else {
4302       GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "pushing empty group");
4303       ret = gst_pad_push_data (pad, TRUE, gst_buffer_new ());
4304     }
4305
4306     gst_buffer_list_iterator_free (it);
4307     gst_buffer_list_unref (list);
4308
4309     return ret;
4310   }
4311
4312   /* ERROR recovery here */
4313 flushed:
4314   {
4315     gst_pad_data_unref (is_buffer, data);
4316     GST_DEBUG_OBJECT (pad, "pad block stopped by flush");
4317     GST_OBJECT_UNLOCK (pad);
4318     return ret;
4319   }
4320 dropped:
4321   {
4322     gst_pad_data_unref (is_buffer, data);
4323     GST_DEBUG_OBJECT (pad, "Dropping buffer due to FALSE probe return");
4324     return GST_FLOW_OK;
4325   }
4326 not_linked:
4327   {
4328     gst_pad_data_unref (is_buffer, data);
4329     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4330         "pushing, but it was not linked");
4331     GST_OBJECT_UNLOCK (pad);
4332     return GST_FLOW_NOT_LINKED;
4333   }
4334 not_negotiated:
4335   {
4336     gst_pad_data_unref (is_buffer, data);
4337     gst_object_unref (peer);
4338     GST_CAT_DEBUG_OBJECT (GST_CAT_SCHEDULING, pad,
4339         "element pushed data then refused to accept the caps");
4340     return GST_FLOW_NOT_NEGOTIATED;
4341   }
4342 }
4343
4344 /**
4345  * gst_pad_push:
4346  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4347  * @buffer: the #GstBuffer to push returns GST_FLOW_ERROR if not.
4348  *
4349  * Pushes a buffer to the peer of @pad.
4350  *
4351  * This function will call an installed pad block before triggering any
4352  * installed pad probes.
4353  *
4354  * If the caps on @buffer are different from the currently configured caps on
4355  * @pad, this function will call any installed setcaps function on @pad (see
4356  * gst_pad_set_setcaps_function()). In case of failure to renegotiate the new
4357  * format, this function returns #GST_FLOW_NOT_NEGOTIATED.
4358  *
4359  * The function proceeds calling gst_pad_chain() on the peer pad and returns
4360  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4361  * be returned.
4362  *
4363  * In all cases, success or failure, the caller loses its reference to @buffer
4364  * after calling this function.
4365  *
4366  * Returns: a #GstFlowReturn from the peer pad.
4367  *
4368  * MT safe.
4369  */
4370 GstFlowReturn
4371 gst_pad_push (GstPad * pad, GstBuffer * buffer)
4372 {
4373   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4374   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4375   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4376
4377   return gst_pad_push_data (pad, TRUE, buffer);
4378 }
4379
4380 /**
4381  * gst_pad_push_list:
4382  * @pad: a source #GstPad, returns #GST_FLOW_ERROR if not.
4383  * @list: the #GstBufferList to push returns GST_FLOW_ERROR if not.
4384  *
4385  * Pushes a buffer list to the peer of @pad.
4386  *
4387  * This function will call an installed pad block before triggering any
4388  * installed pad probes.
4389  *
4390  * If the caps on the first buffer in the first group of @list are different
4391  * from the currently configured caps on @pad, this function will call any
4392  * installed setcaps function on @pad (see gst_pad_set_setcaps_function()). In
4393  * case of failure to renegotiate the new format, this function returns
4394  * #GST_FLOW_NOT_NEGOTIATED.
4395  *
4396  * If there are any probes installed on @pad every group of the buffer list
4397  * will be merged into a normal #GstBuffer and pushed via gst_pad_push and the
4398  * buffer list will be unreffed.
4399  *
4400  * The function proceeds calling the chain function on the peer pad and returns
4401  * the value from that function. If @pad has no peer, #GST_FLOW_NOT_LINKED will
4402  * be returned. If the peer pad does not have any installed chainlist function
4403  * every group buffer of the list will be merged into a normal #GstBuffer and
4404  * chained via gst_pad_chain().
4405  *
4406  * In all cases, success or failure, the caller loses its reference to @list
4407  * after calling this function.
4408  *
4409  * Returns: a #GstFlowReturn from the peer pad.
4410  *
4411  * MT safe.
4412  *
4413  * Since: 0.10.24
4414  */
4415 GstFlowReturn
4416 gst_pad_push_list (GstPad * pad, GstBufferList * list)
4417 {
4418   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4419   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4420   g_return_val_if_fail (GST_IS_BUFFER_LIST (list), GST_FLOW_ERROR);
4421
4422   return gst_pad_push_data (pad, FALSE, list);
4423 }
4424
4425 /**
4426  * gst_pad_check_pull_range:
4427  * @pad: a sink #GstPad.
4428  *
4429  * Checks if a gst_pad_pull_range() can be performed on the peer
4430  * source pad. This function is used by plugins that want to check
4431  * if they can use random access on the peer source pad.
4432  *
4433  * The peer sourcepad can implement a custom #GstPadCheckGetRangeFunction
4434  * if it needs to perform some logic to determine if pull_range is
4435  * possible.
4436  *
4437  * Returns: a gboolean with the result.
4438  *
4439  * MT safe.
4440  */
4441 gboolean
4442 gst_pad_check_pull_range (GstPad * pad)
4443 {
4444   GstPad *peer;
4445   gboolean ret;
4446   GstPadCheckGetRangeFunction checkgetrangefunc;
4447
4448   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4449
4450   GST_OBJECT_LOCK (pad);
4451   if (!GST_PAD_IS_SINK (pad))
4452     goto wrong_direction;
4453
4454   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4455     goto not_connected;
4456
4457   gst_object_ref (peer);
4458   GST_OBJECT_UNLOCK (pad);
4459
4460   /* see note in above function */
4461   if (G_LIKELY ((checkgetrangefunc = peer->checkgetrangefunc) == NULL)) {
4462     /* FIXME, kindoff ghetto */
4463     ret = GST_PAD_GETRANGEFUNC (peer) != NULL;
4464     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4465         "no checkgetrangefunc, assuming %d", ret);
4466   } else {
4467     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4468         "calling checkgetrangefunc %s of peer pad %s:%s",
4469         GST_DEBUG_FUNCPTR_NAME (checkgetrangefunc), GST_DEBUG_PAD_NAME (peer));
4470
4471     ret = checkgetrangefunc (peer);
4472   }
4473
4474   gst_object_unref (peer);
4475
4476   return ret;
4477
4478   /* ERROR recovery here */
4479 wrong_direction:
4480   {
4481     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4482         "checking pull range, but pad must be a sinkpad");
4483     GST_OBJECT_UNLOCK (pad);
4484     return FALSE;
4485   }
4486 not_connected:
4487   {
4488     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4489         "checking pull range, but it was not linked");
4490     GST_OBJECT_UNLOCK (pad);
4491     return FALSE;
4492   }
4493 }
4494
4495 /**
4496  * gst_pad_get_range:
4497  * @pad: a src #GstPad, returns #GST_FLOW_ERROR if not.
4498  * @offset: The start offset of the buffer
4499  * @size: The length of the buffer
4500  * @buffer: a pointer to hold the #GstBuffer, returns #GST_FLOW_ERROR if %NULL.
4501  *
4502  * When @pad is flushing this function returns #GST_FLOW_WRONG_STATE
4503  * immediatly.
4504  *
4505  * Calls the getrange function of @pad, see #GstPadGetRangeFunction for a
4506  * description of a getrange function. If @pad has no getrange function
4507  * installed (see gst_pad_set_getrange_function()) this function returns
4508  * #GST_FLOW_NOT_SUPPORTED.
4509  *
4510  * This is a lowlevel function. Usualy gst_pad_pull_range() is used.
4511  *
4512  * Returns: a #GstFlowReturn from the pad.
4513  *
4514  * MT safe.
4515  */
4516 GstFlowReturn
4517 gst_pad_get_range (GstPad * pad, guint64 offset, guint size,
4518     GstBuffer ** buffer)
4519 {
4520   GstFlowReturn ret;
4521   GstPadGetRangeFunction getrangefunc;
4522   gboolean emit_signal;
4523   GstCaps *caps;
4524   gboolean caps_changed;
4525
4526   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4527   g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
4528   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4529
4530   GST_PAD_STREAM_LOCK (pad);
4531
4532   GST_OBJECT_LOCK (pad);
4533   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4534     goto flushing;
4535
4536   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4537   GST_OBJECT_UNLOCK (pad);
4538
4539   if (G_UNLIKELY ((getrangefunc = GST_PAD_GETRANGEFUNC (pad)) == NULL))
4540     goto no_function;
4541
4542   GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4543       "calling getrangefunc %s, offset %"
4544       G_GUINT64_FORMAT ", size %u",
4545       GST_DEBUG_FUNCPTR_NAME (getrangefunc), offset, size);
4546
4547   ret = getrangefunc (pad, offset, size, buffer);
4548
4549   /* can only fire the signal if we have a valid buffer */
4550   if (G_UNLIKELY (emit_signal) && (ret == GST_FLOW_OK)) {
4551     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4552       goto dropping;
4553   }
4554
4555   GST_PAD_STREAM_UNLOCK (pad);
4556
4557   if (G_UNLIKELY (ret != GST_FLOW_OK))
4558     goto get_range_failed;
4559
4560   GST_OBJECT_LOCK (pad);
4561   /* Before pushing the buffer to the peer pad, ensure that caps
4562    * are set on this pad */
4563   caps = GST_BUFFER_CAPS (*buffer);
4564   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4565   GST_OBJECT_UNLOCK (pad);
4566
4567   if (G_UNLIKELY (caps_changed)) {
4568     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4569     /* this should usually work because the element produced the buffer */
4570     if (G_UNLIKELY (!gst_pad_configure_src (pad, caps, TRUE)))
4571       goto not_negotiated;
4572   }
4573   return ret;
4574
4575   /* ERRORS */
4576 flushing:
4577   {
4578     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4579         "pulling range, but pad was flushing");
4580     GST_OBJECT_UNLOCK (pad);
4581     GST_PAD_STREAM_UNLOCK (pad);
4582     return GST_FLOW_WRONG_STATE;
4583   }
4584 no_function:
4585   {
4586     GST_ELEMENT_ERROR (GST_PAD_PARENT (pad), CORE, PAD, (NULL),
4587         ("pullrange on pad %s:%s but it has no getrangefunction",
4588             GST_DEBUG_PAD_NAME (pad)));
4589     GST_PAD_STREAM_UNLOCK (pad);
4590     return GST_FLOW_NOT_SUPPORTED;
4591   }
4592 dropping:
4593   {
4594     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4595         "Dropping data after FALSE probe return");
4596     GST_PAD_STREAM_UNLOCK (pad);
4597     gst_buffer_unref (*buffer);
4598     *buffer = NULL;
4599     return GST_FLOW_UNEXPECTED;
4600   }
4601 get_range_failed:
4602   {
4603     *buffer = NULL;
4604     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4605         "getrange failed %s", gst_flow_get_name (ret));
4606     return ret;
4607   }
4608 not_negotiated:
4609   {
4610     gst_buffer_unref (*buffer);
4611     *buffer = NULL;
4612     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4613         "getrange returned buffer of unaccaptable caps");
4614     return GST_FLOW_NOT_NEGOTIATED;
4615   }
4616 }
4617
4618
4619 /**
4620  * gst_pad_pull_range:
4621  * @pad: a sink #GstPad, returns GST_FLOW_ERROR if not.
4622  * @offset: The start offset of the buffer
4623  * @size: The length of the buffer
4624  * @buffer: a pointer to hold the #GstBuffer, returns GST_FLOW_ERROR if %NULL.
4625  *
4626  * Pulls a @buffer from the peer pad.
4627  *
4628  * This function will first trigger the pad block signal if it was
4629  * installed.
4630  *
4631  * When @pad is not linked #GST_FLOW_NOT_LINKED is returned else this
4632  * function returns the result of gst_pad_get_range() on the peer pad.
4633  * See gst_pad_get_range() for a list of return values and for the
4634  * semantics of the arguments of this function.
4635  *
4636  * @buffer's caps must either be unset or the same as what is already
4637  * configured on @pad. Renegotiation within a running pull-mode pipeline is not
4638  * supported.
4639  *
4640  * Returns: a #GstFlowReturn from the peer pad.
4641  * When this function returns #GST_FLOW_OK, @buffer will contain a valid
4642  * #GstBuffer that should be freed with gst_buffer_unref() after usage.
4643  * @buffer may not be used or freed when any other return value than
4644  * #GST_FLOW_OK is returned.
4645  *
4646  * MT safe.
4647  */
4648 GstFlowReturn
4649 gst_pad_pull_range (GstPad * pad, guint64 offset, guint size,
4650     GstBuffer ** buffer)
4651 {
4652   GstPad *peer;
4653   GstFlowReturn ret;
4654   gboolean emit_signal;
4655   GstCaps *caps;
4656   gboolean caps_changed;
4657
4658   g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
4659   g_return_val_if_fail (GST_PAD_IS_SINK (pad), GST_FLOW_ERROR);
4660   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
4661
4662   GST_OBJECT_LOCK (pad);
4663
4664   while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad)))
4665     handle_pad_block (pad);
4666
4667   if (G_UNLIKELY ((peer = GST_PAD_PEER (pad)) == NULL))
4668     goto not_connected;
4669
4670   /* signal emision for the pad, peer has chance to emit when
4671    * we call _get_range() */
4672   emit_signal = GST_PAD_DO_BUFFER_SIGNALS (pad) > 0;
4673
4674   gst_object_ref (peer);
4675   GST_OBJECT_UNLOCK (pad);
4676
4677   ret = gst_pad_get_range (peer, offset, size, buffer);
4678
4679   gst_object_unref (peer);
4680
4681   if (G_UNLIKELY (ret != GST_FLOW_OK))
4682     goto pull_range_failed;
4683
4684   /* can only fire the signal if we have a valid buffer */
4685   if (G_UNLIKELY (emit_signal)) {
4686     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (*buffer)))
4687       goto dropping;
4688   }
4689
4690   GST_OBJECT_LOCK (pad);
4691   /* Before pushing the buffer to the peer pad, ensure that caps
4692    * are set on this pad */
4693   caps = GST_BUFFER_CAPS (*buffer);
4694   caps_changed = caps && caps != GST_PAD_CAPS (pad);
4695   GST_OBJECT_UNLOCK (pad);
4696
4697   /* we got a new datatype on the pad, see if it can handle it */
4698   if (G_UNLIKELY (caps_changed)) {
4699     GST_DEBUG_OBJECT (pad, "caps changed to %p %" GST_PTR_FORMAT, caps, caps);
4700     if (G_UNLIKELY (!gst_pad_configure_sink (pad, caps)))
4701       goto not_negotiated;
4702   }
4703   return ret;
4704
4705   /* ERROR recovery here */
4706 not_connected:
4707   {
4708     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4709         "pulling range, but it was not linked");
4710     GST_OBJECT_UNLOCK (pad);
4711     return GST_FLOW_NOT_LINKED;
4712   }
4713 pull_range_failed:
4714   {
4715     *buffer = NULL;
4716     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4717         "pullrange failed %s", gst_flow_get_name (ret));
4718     return ret;
4719   }
4720 dropping:
4721   {
4722     GST_CAT_LOG_OBJECT (GST_CAT_SCHEDULING, pad,
4723         "Dropping data after FALSE probe return");
4724     gst_buffer_unref (*buffer);
4725     *buffer = NULL;
4726     return GST_FLOW_UNEXPECTED;
4727   }
4728 not_negotiated:
4729   {
4730     gst_buffer_unref (*buffer);
4731     *buffer = NULL;
4732     GST_CAT_WARNING_OBJECT (GST_CAT_SCHEDULING, pad,
4733         "pullrange returned buffer of different caps");
4734     return GST_FLOW_NOT_NEGOTIATED;
4735   }
4736 }
4737
4738 /**
4739  * gst_pad_push_event:
4740  * @pad: a #GstPad to push the event to.
4741  * @event: the #GstEvent to send to the pad.
4742  *
4743  * Sends the event to the peer of the given pad. This function is
4744  * mainly used by elements to send events to their peer
4745  * elements.
4746  *
4747  * This function takes owership of the provided event so you should
4748  * gst_event_ref() it if you want to reuse the event after this call.
4749  *
4750  * Returns: TRUE if the event was handled.
4751  *
4752  * MT safe.
4753  */
4754 gboolean
4755 gst_pad_push_event (GstPad * pad, GstEvent * event)
4756 {
4757   GstPad *peerpad;
4758   gboolean result;
4759
4760   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4761   g_return_val_if_fail (event != NULL, FALSE);
4762   g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
4763
4764   GST_LOG_OBJECT (pad, "event: %s", GST_EVENT_TYPE_NAME (event));
4765
4766   GST_OBJECT_LOCK (pad);
4767
4768   /* Two checks to be made:
4769    * . (un)set the FLUSHING flag for flushing events,
4770    * . handle pad blocking */
4771   switch (GST_EVENT_TYPE (event)) {
4772     case GST_EVENT_FLUSH_START:
4773       GST_PAD_SET_FLUSHING (pad);
4774
4775       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4776         /* flush start will have set the FLUSHING flag and will then
4777          * unlock all threads doing a GCond wait on the blocking pad. This
4778          * will typically unblock the STREAMING thread blocked on a pad. */
4779         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-start, "
4780             "doing block signal.");
4781         GST_PAD_BLOCK_BROADCAST (pad);
4782         goto flushed;
4783       }
4784       break;
4785     case GST_EVENT_FLUSH_STOP:
4786       GST_PAD_UNSET_FLUSHING (pad);
4787
4788       /* if we are blocked, flush away the FLUSH_STOP event */
4789       if (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4790         GST_LOG_OBJECT (pad, "Pad is blocked, not forwarding flush-stop");
4791         goto flushed;
4792       }
4793       break;
4794     default:
4795       while (G_UNLIKELY (GST_PAD_IS_BLOCKED (pad))) {
4796         /* block the event as long as the pad is blocked */
4797         if (handle_pad_block (pad) != GST_FLOW_OK)
4798           goto flushed;
4799       }
4800       break;
4801   }
4802
4803   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4804     GST_OBJECT_UNLOCK (pad);
4805
4806     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT (event)))
4807       goto dropping;
4808
4809     GST_OBJECT_LOCK (pad);
4810   }
4811   peerpad = GST_PAD_PEER (pad);
4812   if (peerpad == NULL)
4813     goto not_linked;
4814
4815   GST_LOG_OBJECT (pad, "sending event %s to peerpad %" GST_PTR_FORMAT,
4816       GST_EVENT_TYPE_NAME (event), peerpad);
4817   gst_object_ref (peerpad);
4818   GST_OBJECT_UNLOCK (pad);
4819
4820   result = gst_pad_send_event (peerpad, event);
4821
4822   /* Note: we gave away ownership of the event at this point */
4823   GST_LOG_OBJECT (pad, "sent event to peerpad %" GST_PTR_FORMAT ", result %d",
4824       peerpad, result);
4825   gst_object_unref (peerpad);
4826
4827   return result;
4828
4829   /* ERROR handling */
4830 dropping:
4831   {
4832     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
4833     gst_event_unref (event);
4834     return FALSE;
4835   }
4836 not_linked:
4837   {
4838     GST_DEBUG_OBJECT (pad, "Dropping event because pad is not linked");
4839     gst_event_unref (event);
4840     GST_OBJECT_UNLOCK (pad);
4841     return FALSE;
4842   }
4843 flushed:
4844   {
4845     GST_DEBUG_OBJECT (pad,
4846         "Not forwarding event since we're flushing and blocking");
4847     gst_event_unref (event);
4848     GST_OBJECT_UNLOCK (pad);
4849     return TRUE;
4850   }
4851 }
4852
4853 /**
4854  * gst_pad_send_event:
4855  * @pad: a #GstPad to send the event to.
4856  * @event: the #GstEvent to send to the pad.
4857  *
4858  * Sends the event to the pad. This function can be used
4859  * by applications to send events in the pipeline.
4860  *
4861  * If @pad is a source pad, @event should be an upstream event. If @pad is a
4862  * sink pad, @event should be a downstream event. For example, you would not
4863  * send a #GST_EVENT_EOS on a src pad; EOS events only propagate downstream.
4864  * Furthermore, some downstream events have to be serialized with data flow,
4865  * like EOS, while some can travel out-of-band, like #GST_EVENT_FLUSH_START. If
4866  * the event needs to be serialized with data flow, this function will take the
4867  * pad's stream lock while calling its event function.
4868  *
4869  * To find out whether an event type is upstream, downstream, or downstream and
4870  * serialized, see #GstEventTypeFlags, gst_event_type_get_flags(),
4871  * #GST_EVENT_IS_UPSTREAM, #GST_EVENT_IS_DOWNSTREAM, and
4872  * #GST_EVENT_IS_SERIALIZED. Note that in practice that an application or
4873  * plugin doesn't need to bother itself with this information; the core handles
4874  * all necessary locks and checks.
4875  *
4876  * This function takes owership of the provided event so you should
4877  * gst_event_ref() it if you want to reuse the event after this call.
4878  *
4879  * Returns: TRUE if the event was handled.
4880  */
4881 gboolean
4882 gst_pad_send_event (GstPad * pad, GstEvent * event)
4883 {
4884   gboolean result = FALSE;
4885   GstPadEventFunction eventfunc;
4886   gboolean serialized, need_unlock = FALSE;
4887
4888   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
4889   g_return_val_if_fail (event != NULL, FALSE);
4890
4891   GST_OBJECT_LOCK (pad);
4892   if (GST_PAD_IS_SINK (pad)) {
4893     if (G_UNLIKELY (!GST_EVENT_IS_DOWNSTREAM (event)))
4894       goto wrong_direction;
4895     serialized = GST_EVENT_IS_SERIALIZED (event);
4896   } else if (GST_PAD_IS_SRC (pad)) {
4897     if (G_UNLIKELY (!GST_EVENT_IS_UPSTREAM (event)))
4898       goto wrong_direction;
4899     /* events on srcpad never are serialized */
4900     serialized = FALSE;
4901   } else
4902     goto unknown_direction;
4903
4904   if (G_UNLIKELY (GST_EVENT_SRC (event) == NULL)) {
4905     GST_LOG_OBJECT (pad, "event had no source, setting pad as event source");
4906     GST_EVENT_SRC (event) = gst_object_ref (pad);
4907   }
4908
4909   /* pad signals */
4910   if (G_UNLIKELY (GST_PAD_DO_EVENT_SIGNALS (pad) > 0)) {
4911     GST_OBJECT_UNLOCK (pad);
4912
4913     if (!gst_pad_emit_have_data_signal (pad, GST_MINI_OBJECT_CAST (event)))
4914       goto dropping;
4915
4916     GST_OBJECT_LOCK (pad);
4917   }
4918
4919   switch (GST_EVENT_TYPE (event)) {
4920     case GST_EVENT_FLUSH_START:
4921       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad,
4922           "have event type %d (FLUSH_START)", GST_EVENT_TYPE (event));
4923
4924       /* can't even accept a flush begin event when flushing */
4925       if (GST_PAD_IS_FLUSHING (pad))
4926         goto flushing;
4927       GST_PAD_SET_FLUSHING (pad);
4928       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "set flush flag");
4929       break;
4930     case GST_EVENT_FLUSH_STOP:
4931       GST_PAD_UNSET_FLUSHING (pad);
4932       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "cleared flush flag");
4933       GST_OBJECT_UNLOCK (pad);
4934       /* grab stream lock */
4935       GST_PAD_STREAM_LOCK (pad);
4936       need_unlock = TRUE;
4937       GST_OBJECT_LOCK (pad);
4938       break;
4939     default:
4940       GST_CAT_DEBUG_OBJECT (GST_CAT_EVENT, pad, "have event type %s",
4941           GST_EVENT_TYPE_NAME (event));
4942
4943       /* make this a little faster, no point in grabbing the lock
4944        * if the pad is allready flushing. */
4945       if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4946         goto flushing;
4947
4948       if (serialized) {
4949         /* lock order: STREAM_LOCK, LOCK, recheck flushing. */
4950         GST_OBJECT_UNLOCK (pad);
4951         GST_PAD_STREAM_LOCK (pad);
4952         need_unlock = TRUE;
4953         GST_OBJECT_LOCK (pad);
4954         if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
4955           goto flushing;
4956       }
4957       break;
4958   }
4959   if (G_UNLIKELY ((eventfunc = GST_PAD_EVENTFUNC (pad)) == NULL))
4960     goto no_function;
4961
4962   GST_OBJECT_UNLOCK (pad);
4963
4964   result = eventfunc (pad, event);
4965
4966   if (need_unlock)
4967     GST_PAD_STREAM_UNLOCK (pad);
4968
4969   GST_DEBUG_OBJECT (pad, "sent event, result %d", result);
4970
4971   return result;
4972
4973   /* ERROR handling */
4974 wrong_direction:
4975   {
4976     g_warning ("pad %s:%s sending %s event in wrong direction",
4977         GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
4978     GST_OBJECT_UNLOCK (pad);
4979     gst_event_unref (event);
4980     return FALSE;
4981   }
4982 unknown_direction:
4983   {
4984     g_warning ("pad %s:%s has invalid direction", GST_DEBUG_PAD_NAME (pad));
4985     GST_OBJECT_UNLOCK (pad);
4986     gst_event_unref (event);
4987     return FALSE;
4988   }
4989 no_function:
4990   {
4991     g_warning ("pad %s:%s has no event handler, file a bug.",
4992         GST_DEBUG_PAD_NAME (pad));
4993     GST_OBJECT_UNLOCK (pad);
4994     if (need_unlock)
4995       GST_PAD_STREAM_UNLOCK (pad);
4996     gst_event_unref (event);
4997     return FALSE;
4998   }
4999 flushing:
5000   {
5001     GST_OBJECT_UNLOCK (pad);
5002     if (need_unlock)
5003       GST_PAD_STREAM_UNLOCK (pad);
5004     GST_CAT_INFO_OBJECT (GST_CAT_EVENT, pad,
5005         "Received event on flushing pad. Discarding");
5006     gst_event_unref (event);
5007     return FALSE;
5008   }
5009 dropping:
5010   {
5011     GST_DEBUG_OBJECT (pad, "Dropping event after FALSE probe return");
5012     gst_event_unref (event);
5013     return FALSE;
5014   }
5015 }
5016
5017 /**
5018  * gst_pad_set_element_private:
5019  * @pad: the #GstPad to set the private data of.
5020  * @priv: The private data to attach to the pad.
5021  *
5022  * Set the given private data gpointer on the pad.
5023  * This function can only be used by the element that owns the pad.
5024  * No locking is performed in this function.
5025  */
5026 void
5027 gst_pad_set_element_private (GstPad * pad, gpointer priv)
5028 {
5029   pad->element_private = priv;
5030 }
5031
5032 /**
5033  * gst_pad_get_element_private:
5034  * @pad: the #GstPad to get the private data of.
5035  *
5036  * Gets the private data of a pad.
5037  * No locking is performed in this function.
5038  *
5039  * Returns: a #gpointer to the private data.
5040  */
5041 gpointer
5042 gst_pad_get_element_private (GstPad * pad)
5043 {
5044   return pad->element_private;
5045 }
5046
5047 static void
5048 do_stream_status (GstPad * pad, GstStreamStatusType type,
5049     GThread * thread, GstTask * task)
5050 {
5051   GstElement *parent;
5052
5053   GST_DEBUG_OBJECT (pad, "doing stream-status %d", type);
5054
5055   if ((parent = GST_ELEMENT_CAST (gst_pad_get_parent (pad)))) {
5056     if (GST_IS_ELEMENT (parent)) {
5057       GstMessage *message;
5058       GValue value = { 0 };
5059
5060       message = gst_message_new_stream_status (GST_OBJECT_CAST (pad),
5061           type, parent);
5062
5063       g_value_init (&value, GST_TYPE_TASK);
5064       g_value_set_object (&value, task);
5065       gst_message_set_stream_status_object (message, &value);
5066       g_value_unset (&value);
5067
5068       GST_DEBUG_OBJECT (pad, "posting stream-status %d", type);
5069       gst_element_post_message (parent, message);
5070     }
5071     gst_object_unref (parent);
5072   }
5073 }
5074
5075 static void
5076 pad_enter_thread (GstTask * task, GThread * thread, gpointer user_data)
5077 {
5078   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_ENTER,
5079       thread, task);
5080 }
5081
5082 static void
5083 pad_leave_thread (GstTask * task, GThread * thread, gpointer user_data)
5084 {
5085   do_stream_status (GST_PAD_CAST (user_data), GST_STREAM_STATUS_TYPE_LEAVE,
5086       thread, task);
5087 }
5088
5089 static GstTaskThreadCallbacks thr_callbacks = {
5090   pad_enter_thread,
5091   pad_leave_thread,
5092 };
5093
5094 /**
5095  * gst_pad_start_task:
5096  * @pad: the #GstPad to start the task of
5097  * @func: the task function to call
5098  * @data: data passed to the task function
5099  *
5100  * Starts a task that repeatedly calls @func with @data. This function
5101  * is mostly used in pad activation functions to start the dataflow.
5102  * The #GST_PAD_STREAM_LOCK of @pad will automatically be acquired
5103  * before @func is called.
5104  *
5105  * Returns: a %TRUE if the task could be started.
5106  */
5107 gboolean
5108 gst_pad_start_task (GstPad * pad, GstTaskFunction func, gpointer data)
5109 {
5110   GstTask *task;
5111   gboolean res;
5112
5113   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5114   g_return_val_if_fail (func != NULL, FALSE);
5115
5116   GST_DEBUG_OBJECT (pad, "start task");
5117
5118   GST_OBJECT_LOCK (pad);
5119   task = GST_PAD_TASK (pad);
5120   if (task == NULL) {
5121     task = gst_task_create (func, data);
5122     gst_task_set_lock (task, GST_PAD_GET_STREAM_LOCK (pad));
5123     gst_task_set_thread_callbacks (task, &thr_callbacks, pad, NULL);
5124     GST_DEBUG_OBJECT (pad, "created task");
5125     /* release lock to post the message */
5126     GST_OBJECT_UNLOCK (pad);
5127
5128     do_stream_status (pad, GST_STREAM_STATUS_TYPE_CREATE, NULL, task);
5129
5130     GST_OBJECT_LOCK (pad);
5131     /* nobody else is supposed to have changed the pad now */
5132     if (GST_PAD_TASK (pad) != NULL)
5133       goto concurrent_start;
5134     GST_PAD_TASK (pad) = task;
5135   }
5136   res = gst_task_set_state (task, GST_TASK_STARTED);
5137   GST_OBJECT_UNLOCK (pad);
5138
5139   return res;
5140
5141   /* ERRORS */
5142 concurrent_start:
5143   {
5144     g_warning ("two threads started pad %s:%s at the same time",
5145         GST_DEBUG_PAD_NAME (pad));
5146     GST_OBJECT_UNLOCK (pad);
5147     gst_object_unref (task);
5148     return FALSE;
5149   }
5150 }
5151
5152 /**
5153  * gst_pad_pause_task:
5154  * @pad: the #GstPad to pause the task of
5155  *
5156  * Pause the task of @pad. This function will also wait until the
5157  * function executed by the task is finished if this function is not
5158  * called from the task function.
5159  *
5160  * Returns: a TRUE if the task could be paused or FALSE when the pad
5161  * has no task.
5162  */
5163 gboolean
5164 gst_pad_pause_task (GstPad * pad)
5165 {
5166   GstTask *task;
5167   gboolean res;
5168
5169   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5170
5171   GST_DEBUG_OBJECT (pad, "pause task");
5172
5173   GST_OBJECT_LOCK (pad);
5174   task = GST_PAD_TASK (pad);
5175   if (task == NULL)
5176     goto no_task;
5177   res = gst_task_set_state (task, GST_TASK_PAUSED);
5178   GST_OBJECT_UNLOCK (pad);
5179
5180   /* wait for task function to finish, this lock is recursive so it does nothing
5181    * when the pause is called from the task itself */
5182   GST_PAD_STREAM_LOCK (pad);
5183   GST_PAD_STREAM_UNLOCK (pad);
5184
5185   return res;
5186
5187 no_task:
5188   {
5189     GST_DEBUG_OBJECT (pad, "pad has no task");
5190     GST_OBJECT_UNLOCK (pad);
5191     return FALSE;
5192   }
5193 }
5194
5195 /**
5196  * gst_pad_stop_task:
5197  * @pad: the #GstPad to stop the task of
5198  *
5199  * Stop the task of @pad. This function will also make sure that the
5200  * function executed by the task will effectively stop if not called
5201  * from the GstTaskFunction.
5202  *
5203  * This function will deadlock if called from the GstTaskFunction of
5204  * the task. Use gst_task_pause() instead.
5205  *
5206  * Regardless of whether the pad has a task, the stream lock is acquired and
5207  * released so as to ensure that streaming through this pad has finished.
5208  *
5209  * Returns: a TRUE if the task could be stopped or FALSE on error.
5210  */
5211 gboolean
5212 gst_pad_stop_task (GstPad * pad)
5213 {
5214   GstTask *task;
5215   gboolean res;
5216
5217   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
5218
5219   GST_DEBUG_OBJECT (pad, "stop task");
5220
5221   GST_OBJECT_LOCK (pad);
5222   task = GST_PAD_TASK (pad);
5223   if (task == NULL)
5224     goto no_task;
5225   GST_PAD_TASK (pad) = NULL;
5226   res = gst_task_set_state (task, GST_TASK_STOPPED);
5227   GST_OBJECT_UNLOCK (pad);
5228
5229   GST_PAD_STREAM_LOCK (pad);
5230   GST_PAD_STREAM_UNLOCK (pad);
5231
5232   if (!gst_task_join (task))
5233     goto join_failed;
5234
5235   gst_object_unref (task);
5236
5237   return res;
5238
5239 no_task:
5240   {
5241     GST_DEBUG_OBJECT (pad, "no task");
5242     GST_OBJECT_UNLOCK (pad);
5243
5244     GST_PAD_STREAM_LOCK (pad);
5245     GST_PAD_STREAM_UNLOCK (pad);
5246
5247     /* this is not an error */
5248     return TRUE;
5249   }
5250 join_failed:
5251   {
5252     /* this is bad, possibly the application tried to join the task from
5253      * the task's thread. We install the task again so that it will be stopped
5254      * again from the right thread next time hopefully. */
5255     GST_OBJECT_LOCK (pad);
5256     GST_DEBUG_OBJECT (pad, "join failed");
5257     /* we can only install this task if there was no other task */
5258     if (GST_PAD_TASK (pad) == NULL)
5259       GST_PAD_TASK (pad) = task;
5260     GST_OBJECT_UNLOCK (pad);
5261
5262     return FALSE;
5263   }
5264 }