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