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