base: assorted introspection fixes and additions
[platform/upstream/gstreamer.git] / libs / gst / base / gstcollectpads.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sourceforge.net>
4  * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  *
6  * gstcollectpads.c:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 /**
24  * SECTION:gstcollectpads
25  * @short_description: manages a set of pads that operate in collect mode
26  * @see_also:
27  *
28  * Manages a set of pads that operate in collect mode. This means that control
29  * is given to the manager of this object when all pads have data.
30  * <itemizedlist>
31  *   <listitem><para>
32  *     Collectpads are created with gst_collect_pads_new(). A callback should then
33  *     be installed with gst_collect_pads_set_function ().
34  *   </para></listitem>
35  *   <listitem><para>
36  *     Pads are added to the collection with gst_collect_pads_add_pad()/
37  *     gst_collect_pads_remove_pad(). The pad
38  *     has to be a sinkpad. The chain and event functions of the pad are
39  *     overridden. The element_private of the pad is used to store
40  *     private information for the collectpads.
41  *   </para></listitem>
42  *   <listitem><para>
43  *     For each pad, data is queued in the _chain function or by
44  *     performing a pull_range.
45  *   </para></listitem>
46  *   <listitem><para>
47  *     When data is queued on all pads in waiting mode, the callback function is called.
48  *   </para></listitem>
49  *   <listitem><para>
50  *     Data can be dequeued from the pad with the gst_collect_pads_pop() method.
51  *     One can peek at the data with the gst_collect_pads_peek() function.
52  *     These functions will return %NULL if the pad received an EOS event. When all
53  *     pads return %NULL from a gst_collect_pads_peek(), the element can emit an EOS
54  *     event itself.
55  *   </para></listitem>
56  *   <listitem><para>
57  *     Data can also be dequeued in byte units using the gst_collect_pads_available(),
58  *     gst_collect_pads_read_buffer() and gst_collect_pads_flush() calls.
59  *   </para></listitem>
60  *   <listitem><para>
61  *     Elements should call gst_collect_pads_start() and gst_collect_pads_stop() in
62  *     their state change functions to start and stop the processing of the collectpads.
63  *     The gst_collect_pads_stop() call should be called before calling the parent
64  *     element state change function in the PAUSED_TO_READY state change to ensure
65  *     no pad is blocked and the element can finish streaming.
66  *   </para></listitem>
67  *   <listitem><para>
68  *     gst_collect_pads_set_waiting() sets a pad to waiting or non-waiting mode.
69  *     CollectPads element is not waiting for data to be collected on non-waiting pads.
70  *     Thus these pads may but need not have data when the callback is called.
71  *     All pads are in waiting mode by default.
72  *   </para></listitem>
73  * </itemizedlist>
74  */
75
76 #ifdef HAVE_CONFIG_H
77 #  include "config.h"
78 #endif
79
80 #include <gst/gst_private.h>
81
82 #include "gstcollectpads.h"
83
84 #include "../../../gst/glib-compat-private.h"
85
86 GST_DEBUG_CATEGORY_STATIC (collect_pads_debug);
87 #define GST_CAT_DEFAULT collect_pads_debug
88
89 #define parent_class gst_collect_pads_parent_class
90 G_DEFINE_TYPE (GstCollectPads, gst_collect_pads, GST_TYPE_OBJECT);
91
92 struct _GstCollectDataPrivate
93 {
94   /* refcounting for struct, and destroy callback */
95   GstCollectDataDestroyNotify destroy_notify;
96   gint refcount;
97 };
98
99 struct _GstCollectPadsPrivate
100 {
101   /* with LOCK and/or STREAM_LOCK */
102   gboolean started;
103
104   /* with STREAM_LOCK */
105   guint32 cookie;               /* @data list cookie */
106   guint numpads;                /* number of pads in @data */
107   guint queuedpads;             /* number of pads with a buffer */
108   guint eospads;                /* number of pads that are EOS */
109   GstClockTime earliest_time;   /* Current earliest time */
110   GstCollectData *earliest_data;        /* Pad data for current earliest time */
111
112   /* with LOCK */
113   GSList *pad_list;             /* updated pad list */
114   guint32 pad_cookie;           /* updated cookie */
115
116   GstCollectPadsFunction func;  /* function and user_data for callback */
117   gpointer user_data;
118   GstCollectPadsBufferFunction buffer_func;     /* function and user_data for buffer callback */
119   gpointer buffer_user_data;
120   GstCollectPadsCompareFunction compare_func;
121   gpointer compare_user_data;
122   GstCollectPadsEventFunction event_func;       /* function and data for event callback */
123   gpointer event_user_data;
124   GstCollectPadsQueryFunction query_func;
125   gpointer query_user_data;
126   GstCollectPadsClipFunction clip_func;
127   gpointer clip_user_data;
128   GstCollectPadsFlushFunction flush_func;
129   gpointer flush_user_data;
130
131   /* no other lock needed */
132   GMutex evt_lock;              /* these make up sort of poor man's event signaling */
133   GCond evt_cond;
134   guint32 evt_cookie;
135
136   gboolean seeking;
137   gboolean pending_flush_start;
138   gboolean pending_flush_stop;
139 };
140
141 static void gst_collect_pads_clear (GstCollectPads * pads,
142     GstCollectData * data);
143 static GstFlowReturn gst_collect_pads_chain (GstPad * pad, GstObject * parent,
144     GstBuffer * buffer);
145 static gboolean gst_collect_pads_event (GstPad * pad, GstObject * parent,
146     GstEvent * event);
147 static gboolean gst_collect_pads_query (GstPad * pad, GstObject * parent,
148     GstQuery * query);
149 static void gst_collect_pads_finalize (GObject * object);
150 static GstFlowReturn gst_collect_pads_default_collected (GstCollectPads *
151     pads, gpointer user_data);
152 static gint gst_collect_pads_default_compare_func (GstCollectPads * pads,
153     GstCollectData * data1, GstClockTime timestamp1, GstCollectData * data2,
154     GstClockTime timestamp2, gpointer user_data);
155 static gboolean gst_collect_pads_recalculate_full (GstCollectPads * pads);
156 static void ref_data (GstCollectData * data);
157 static void unref_data (GstCollectData * data);
158
159 static gboolean gst_collect_pads_event_default_internal (GstCollectPads *
160     pads, GstCollectData * data, GstEvent * event, gpointer user_data);
161 static gboolean gst_collect_pads_query_default_internal (GstCollectPads *
162     pads, GstCollectData * data, GstQuery * query, gpointer user_data);
163
164
165 /* Some properties are protected by LOCK, others by STREAM_LOCK
166  * However, manipulating either of these partitions may require
167  * to signal/wake a _WAIT, so use a separate (sort of) event to prevent races
168  * Alternative implementations are possible, e.g. some low-level re-implementing
169  * of the 2 above locks to drop both of them atomically when going into _WAIT.
170  */
171 #define GST_COLLECT_PADS_GET_EVT_COND(pads) (&((GstCollectPads *)pads)->priv->evt_cond)
172 #define GST_COLLECT_PADS_GET_EVT_LOCK(pads) (&((GstCollectPads *)pads)->priv->evt_lock)
173 #define GST_COLLECT_PADS_EVT_WAIT(pads, cookie) G_STMT_START {    \
174   g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));            \
175   /* should work unless a lot of event'ing and thread starvation */\
176   while (cookie == ((GstCollectPads *) pads)->priv->evt_cookie)         \
177     g_cond_wait (GST_COLLECT_PADS_GET_EVT_COND (pads),            \
178         GST_COLLECT_PADS_GET_EVT_LOCK (pads));                    \
179   cookie = ((GstCollectPads *) pads)->priv->evt_cookie;                 \
180   g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));          \
181 } G_STMT_END
182 #define GST_COLLECT_PADS_EVT_WAIT_TIMED(pads, cookie, timeout) G_STMT_START { \
183   GTimeVal __tv; \
184   \
185   g_get_current_time (&tv); \
186   g_time_val_add (&tv, timeout); \
187   \
188   g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));            \
189   /* should work unless a lot of event'ing and thread starvation */\
190   while (cookie == ((GstCollectPads *) pads)->priv->evt_cookie)         \
191     g_cond_timed_wait (GST_COLLECT_PADS_GET_EVT_COND (pads),            \
192         GST_COLLECT_PADS_GET_EVT_LOCK (pads), &tv);                    \
193   cookie = ((GstCollectPads *) pads)->priv->evt_cookie;                 \
194   g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));          \
195 } G_STMT_END
196 #define GST_COLLECT_PADS_EVT_BROADCAST(pads) G_STMT_START {       \
197   g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));            \
198   /* never mind wrap-around */                                     \
199   ++(((GstCollectPads *) pads)->priv->evt_cookie);                      \
200   g_cond_broadcast (GST_COLLECT_PADS_GET_EVT_COND (pads));        \
201   g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));          \
202 } G_STMT_END
203 #define GST_COLLECT_PADS_EVT_INIT(cookie) G_STMT_START {          \
204   g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));            \
205   cookie = ((GstCollectPads *) pads)->priv->evt_cookie;                 \
206   g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads));          \
207 } G_STMT_END
208
209 static void
210 gst_collect_pads_class_init (GstCollectPadsClass * klass)
211 {
212   GObjectClass *gobject_class = (GObjectClass *) klass;
213
214   g_type_class_add_private (klass, sizeof (GstCollectPadsPrivate));
215
216   GST_DEBUG_CATEGORY_INIT (collect_pads_debug, "collectpads", 0,
217       "GstCollectPads");
218
219   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_collect_pads_finalize);
220 }
221
222 static void
223 gst_collect_pads_init (GstCollectPads * pads)
224 {
225   pads->priv =
226       G_TYPE_INSTANCE_GET_PRIVATE (pads, GST_TYPE_COLLECT_PADS,
227       GstCollectPadsPrivate);
228
229   pads->data = NULL;
230   pads->priv->cookie = 0;
231   pads->priv->numpads = 0;
232   pads->priv->queuedpads = 0;
233   pads->priv->eospads = 0;
234   pads->priv->started = FALSE;
235
236   g_rec_mutex_init (&pads->stream_lock);
237
238   pads->priv->func = gst_collect_pads_default_collected;
239   pads->priv->user_data = NULL;
240   pads->priv->event_func = NULL;
241   pads->priv->event_user_data = NULL;
242
243   /* members for default muxing */
244   pads->priv->buffer_func = NULL;
245   pads->priv->buffer_user_data = NULL;
246   pads->priv->compare_func = gst_collect_pads_default_compare_func;
247   pads->priv->compare_user_data = NULL;
248   pads->priv->earliest_data = NULL;
249   pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
250
251   pads->priv->event_func = gst_collect_pads_event_default_internal;
252   pads->priv->query_func = gst_collect_pads_query_default_internal;
253
254   /* members to manage the pad list */
255   pads->priv->pad_cookie = 0;
256   pads->priv->pad_list = NULL;
257
258   /* members for event */
259   g_mutex_init (&pads->priv->evt_lock);
260   g_cond_init (&pads->priv->evt_cond);
261   pads->priv->evt_cookie = 0;
262
263   pads->priv->seeking = FALSE;
264   pads->priv->pending_flush_start = FALSE;
265   pads->priv->pending_flush_stop = FALSE;
266
267   /* clear floating flag */
268   gst_object_ref_sink (pads);
269 }
270
271 static void
272 gst_collect_pads_finalize (GObject * object)
273 {
274   GstCollectPads *pads = GST_COLLECT_PADS (object);
275
276   GST_DEBUG_OBJECT (object, "finalize");
277
278   g_rec_mutex_clear (&pads->stream_lock);
279
280   g_cond_clear (&pads->priv->evt_cond);
281   g_mutex_clear (&pads->priv->evt_lock);
282
283   /* Remove pads and free pads list */
284   g_slist_foreach (pads->priv->pad_list, (GFunc) unref_data, NULL);
285   g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
286   g_slist_free (pads->data);
287   g_slist_free (pads->priv->pad_list);
288
289   G_OBJECT_CLASS (parent_class)->finalize (object);
290 }
291
292 /**
293  * gst_collect_pads_new:
294  *
295  * Create a new instance of #GstCollectPads.
296  *
297  * MT safe.
298  *
299  * Returns: (transfer full): a new #GstCollectPads, or %NULL in case of an error.
300  */
301 GstCollectPads *
302 gst_collect_pads_new (void)
303 {
304   GstCollectPads *newcoll;
305
306   newcoll = g_object_new (GST_TYPE_COLLECT_PADS, NULL);
307
308   return newcoll;
309 }
310
311 /* Must be called with GstObject lock! */
312 static void
313 gst_collect_pads_set_buffer_function_locked (GstCollectPads * pads,
314     GstCollectPadsBufferFunction func, gpointer user_data)
315 {
316   pads->priv->buffer_func = func;
317   pads->priv->buffer_user_data = user_data;
318 }
319
320 /**
321  * gst_collect_pads_set_buffer_function:
322  * @pads: the collectpads to use
323  * @func: the function to set
324  * @user_data: (closure): user data passed to the function
325  *
326  * Set the callback function and user data that will be called with
327  * the oldest buffer when all pads have been collected, or %NULL on EOS.
328  * If a buffer is passed, the callback owns a reference and must unref
329  * it.
330  *
331  * MT safe.
332  */
333 void
334 gst_collect_pads_set_buffer_function (GstCollectPads * pads,
335     GstCollectPadsBufferFunction func, gpointer user_data)
336 {
337   g_return_if_fail (pads != NULL);
338   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
339
340   GST_OBJECT_LOCK (pads);
341   gst_collect_pads_set_buffer_function_locked (pads, func, user_data);
342   GST_OBJECT_UNLOCK (pads);
343 }
344
345 /**
346  * gst_collect_pads_set_compare_function:
347  * @pads: the pads to use
348  * @func: the function to set
349  * @user_data: (closure): user data passed to the function
350  *
351  * Set the timestamp comparison function.
352  *
353  * MT safe.
354  */
355 /* NOTE allowing to change comparison seems not advisable;
356 no known use-case, and collaboration with default algorithm is unpredictable.
357 If custom compairing/operation is needed, just use a collect function of
358 your own */
359 void
360 gst_collect_pads_set_compare_function (GstCollectPads * pads,
361     GstCollectPadsCompareFunction func, gpointer user_data)
362 {
363   g_return_if_fail (pads != NULL);
364   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
365
366   GST_OBJECT_LOCK (pads);
367   pads->priv->compare_func = func;
368   pads->priv->compare_user_data = user_data;
369   GST_OBJECT_UNLOCK (pads);
370 }
371
372 /**
373  * gst_collect_pads_set_function:
374  * @pads: the collectpads to use
375  * @func: the function to set
376  * @user_data: user data passed to the function
377  *
378  * CollectPads provides a default collection algorithm that will determine
379  * the oldest buffer available on all of its pads, and then delegate
380  * to a configured callback.
381  * However, if circumstances are more complicated and/or more control
382  * is desired, this sets a callback that will be invoked instead when
383  * all the pads added to the collection have buffers queued.
384  * Evidently, this callback is not compatible with
385  * gst_collect_pads_set_buffer_function() callback.
386  * If this callback is set, the former will be unset.
387  *
388  * MT safe.
389  */
390 void
391 gst_collect_pads_set_function (GstCollectPads * pads,
392     GstCollectPadsFunction func, gpointer user_data)
393 {
394   g_return_if_fail (pads != NULL);
395   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
396
397   GST_OBJECT_LOCK (pads);
398   pads->priv->func = func;
399   pads->priv->user_data = user_data;
400   gst_collect_pads_set_buffer_function_locked (pads, NULL, NULL);
401   GST_OBJECT_UNLOCK (pads);
402 }
403
404 static void
405 ref_data (GstCollectData * data)
406 {
407   g_assert (data != NULL);
408
409   g_atomic_int_inc (&(data->priv->refcount));
410 }
411
412 static void
413 unref_data (GstCollectData * data)
414 {
415   g_assert (data != NULL);
416   g_assert (data->priv->refcount > 0);
417
418   if (!g_atomic_int_dec_and_test (&(data->priv->refcount)))
419     return;
420
421   if (data->priv->destroy_notify)
422     data->priv->destroy_notify (data);
423
424   g_object_unref (data->pad);
425   if (data->buffer) {
426     gst_buffer_unref (data->buffer);
427   }
428   g_free (data->priv);
429   g_free (data);
430 }
431
432 /**
433  * gst_collect_pads_set_event_function:
434  * @pads: the collectpads to use
435  * @func: the function to set
436  * @user_data: user data passed to the function
437  *
438  * Set the event callback function and user data that will be called when
439  * collectpads has received an event originating from one of the collected
440  * pads.  If the event being processed is a serialized one, this callback is
441  * called with @pads STREAM_LOCK held, otherwise not.  As this lock should be
442  * held when calling a number of CollectPads functions, it should be acquired
443  * if so (unusually) needed.
444  *
445  * MT safe.
446  */
447 void
448 gst_collect_pads_set_event_function (GstCollectPads * pads,
449     GstCollectPadsEventFunction func, gpointer user_data)
450 {
451   g_return_if_fail (pads != NULL);
452   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
453
454   GST_OBJECT_LOCK (pads);
455   pads->priv->event_func = func;
456   pads->priv->event_user_data = user_data;
457   GST_OBJECT_UNLOCK (pads);
458 }
459
460 /**
461  * gst_collect_pads_set_query_function:
462  * @pads: the collectpads to use
463  * @func: the function to set
464  * @user_data: user data passed to the function
465  *
466  * Set the query callback function and user data that will be called after
467  * collectpads has received a query originating from one of the collected
468  * pads.  If the query being processed is a serialized one, this callback is
469  * called with @pads STREAM_LOCK held, otherwise not.  As this lock should be
470  * held when calling a number of CollectPads functions, it should be acquired
471  * if so (unusually) needed.
472  *
473  * MT safe.
474  */
475 void
476 gst_collect_pads_set_query_function (GstCollectPads * pads,
477     GstCollectPadsQueryFunction func, gpointer user_data)
478 {
479   g_return_if_fail (pads != NULL);
480   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
481
482   GST_OBJECT_LOCK (pads);
483   pads->priv->query_func = func;
484   pads->priv->query_user_data = user_data;
485   GST_OBJECT_UNLOCK (pads);
486 }
487
488 /**
489 * gst_collect_pads_clip_running_time:
490 * @pads: the collectpads to use
491 * @cdata: collect data of corresponding pad
492 * @buf: buffer being clipped
493 * @outbuf: (allow-none): output buffer with running time, or NULL if clipped
494 * @user_data: user data (unused)
495 *
496 * Convenience clipping function that converts incoming buffer's timestamp
497 * to running time, or clips the buffer if outside configured segment.
498 */
499 GstFlowReturn
500 gst_collect_pads_clip_running_time (GstCollectPads * pads,
501     GstCollectData * cdata, GstBuffer * buf, GstBuffer ** outbuf,
502     gpointer user_data)
503 {
504   GstClockTime time;
505
506   *outbuf = buf;
507   time = GST_BUFFER_PTS (buf);
508
509   /* invalid left alone and passed */
510   if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
511     time = gst_segment_to_running_time (&cdata->segment, GST_FORMAT_TIME, time);
512     if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
513       GST_DEBUG_OBJECT (cdata->pad, "clipping buffer on pad outside segment %"
514           GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
515       gst_buffer_unref (buf);
516       *outbuf = NULL;
517     } else {
518       GST_LOG_OBJECT (cdata->pad, "buffer ts %" GST_TIME_FORMAT " -> %"
519           GST_TIME_FORMAT " running time",
520           GST_TIME_ARGS (GST_BUFFER_PTS (buf)), GST_TIME_ARGS (time));
521       *outbuf = gst_buffer_make_writable (buf);
522       GST_BUFFER_PTS (*outbuf) = time;
523       GST_BUFFER_DTS (*outbuf) = gst_segment_to_running_time (&cdata->segment,
524           GST_FORMAT_TIME, GST_BUFFER_DTS (*outbuf));
525     }
526   }
527
528   return GST_FLOW_OK;
529 }
530
531 /**
532  * gst_collect_pads_set_clip_function:
533  * @pads: the collectpads to use
534  * @clipfunc: clip function to install
535  * @user_data: user data to pass to @clip_func
536  *
537  * Install a clipping function that is called right after a buffer is received
538  * on a pad managed by @pads. See #GstCollectPadsClipFunction for more info.
539  */
540 void
541 gst_collect_pads_set_clip_function (GstCollectPads * pads,
542     GstCollectPadsClipFunction clipfunc, gpointer user_data)
543 {
544   g_return_if_fail (pads != NULL);
545   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
546
547   pads->priv->clip_func = clipfunc;
548   pads->priv->clip_user_data = user_data;
549 }
550
551 /**
552  * gst_collect_pads_set_flush_function:
553  * @pads: the collectpads to use
554  * @func: flush function to install
555  * @user_data: user data to pass to @func
556  *
557  * Install a flush function that is called when the internal
558  * state of all pads should be flushed as part of flushing seek
559  * handling. See #GstCollectPadsFlushFunction for more info.
560  *
561  * Since: 1.4
562  */
563 void
564 gst_collect_pads_set_flush_function (GstCollectPads * pads,
565     GstCollectPadsFlushFunction func, gpointer user_data)
566 {
567   g_return_if_fail (pads != NULL);
568   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
569
570   pads->priv->flush_func = func;
571   pads->priv->flush_user_data = user_data;
572 }
573
574 /**
575  * gst_collect_pads_add_pad:
576  * @pads: the collectpads to use
577  * @pad: (transfer none): the pad to add
578  * @size: the size of the returned #GstCollectData structure
579  * @destroy_notify: (scope async): function to be called before the returned
580  *   #GstCollectData structure is freed
581  * @lock: whether to lock this pad in usual waiting state
582  *
583  * Add a pad to the collection of collect pads. The pad has to be
584  * a sinkpad. The refcount of the pad is incremented. Use
585  * gst_collect_pads_remove_pad() to remove the pad from the collection
586  * again.
587  *
588  * You specify a size for the returned #GstCollectData structure
589  * so that you can use it to store additional information.
590  *
591  * You can also specify a #GstCollectDataDestroyNotify that will be called
592  * just before the #GstCollectData structure is freed. It is passed the
593  * pointer to the structure and should free any custom memory and resources
594  * allocated for it.
595  *
596  * Keeping a pad locked in waiting state is only relevant when using
597  * the default collection algorithm (providing the oldest buffer).
598  * It ensures a buffer must be available on this pad for a collection
599  * to take place.  This is of typical use to a muxer element where
600  * non-subtitle streams should always be in waiting state,
601  * e.g. to assure that caps information is available on all these streams
602  * when initial headers have to be written.
603  *
604  * The pad will be automatically activated in push mode when @pads is
605  * started.
606  *
607  * MT safe.
608  *
609  * Returns: (nullable) (transfer none): a new #GstCollectData to identify the
610  *   new pad. Or %NULL if wrong parameters are supplied.
611  */
612 GstCollectData *
613 gst_collect_pads_add_pad (GstCollectPads * pads, GstPad * pad, guint size,
614     GstCollectDataDestroyNotify destroy_notify, gboolean lock)
615 {
616   GstCollectData *data;
617
618   g_return_val_if_fail (pads != NULL, NULL);
619   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
620   g_return_val_if_fail (pad != NULL, NULL);
621   g_return_val_if_fail (GST_PAD_IS_SINK (pad), NULL);
622   g_return_val_if_fail (size >= sizeof (GstCollectData), NULL);
623
624   GST_DEBUG_OBJECT (pads, "adding pad %s:%s", GST_DEBUG_PAD_NAME (pad));
625
626   data = g_malloc0 (size);
627   data->priv = g_new0 (GstCollectDataPrivate, 1);
628   data->collect = pads;
629   data->pad = gst_object_ref (pad);
630   data->buffer = NULL;
631   data->pos = 0;
632   gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
633   data->state = GST_COLLECT_PADS_STATE_WAITING;
634   data->state |= lock ? GST_COLLECT_PADS_STATE_LOCKED : 0;
635   data->priv->refcount = 1;
636   data->priv->destroy_notify = destroy_notify;
637
638   GST_OBJECT_LOCK (pads);
639   GST_OBJECT_LOCK (pad);
640   gst_pad_set_element_private (pad, data);
641   GST_OBJECT_UNLOCK (pad);
642   pads->priv->pad_list = g_slist_append (pads->priv->pad_list, data);
643   gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_chain));
644   gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_event));
645   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_query));
646   /* backward compat, also add to data if stopped, so that the element already
647    * has this in the public data list before going PAUSED (typically)
648    * this can only be done when we are stopped because we don't take the
649    * STREAM_LOCK to protect the pads->data list. */
650   if (!pads->priv->started) {
651     pads->data = g_slist_append (pads->data, data);
652     ref_data (data);
653   }
654   /* activate the pad when needed */
655   if (pads->priv->started)
656     gst_pad_set_active (pad, TRUE);
657   pads->priv->pad_cookie++;
658   GST_OBJECT_UNLOCK (pads);
659
660   return data;
661 }
662
663 static gint
664 find_pad (GstCollectData * data, GstPad * pad)
665 {
666   if (data->pad == pad)
667     return 0;
668   return 1;
669 }
670
671 /**
672  * gst_collect_pads_remove_pad:
673  * @pads: the collectpads to use
674  * @pad: (transfer none): the pad to remove
675  *
676  * Remove a pad from the collection of collect pads. This function will also
677  * free the #GstCollectData and all the resources that were allocated with
678  * gst_collect_pads_add_pad().
679  *
680  * The pad will be deactivated automatically when @pads is stopped.
681  *
682  * MT safe.
683  *
684  * Returns: %TRUE if the pad could be removed.
685  */
686 gboolean
687 gst_collect_pads_remove_pad (GstCollectPads * pads, GstPad * pad)
688 {
689   GstCollectData *data;
690   GSList *list;
691
692   g_return_val_if_fail (pads != NULL, FALSE);
693   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), FALSE);
694   g_return_val_if_fail (pad != NULL, FALSE);
695   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
696
697   GST_DEBUG_OBJECT (pads, "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
698
699   GST_OBJECT_LOCK (pads);
700   list =
701       g_slist_find_custom (pads->priv->pad_list, pad, (GCompareFunc) find_pad);
702   if (!list)
703     goto unknown_pad;
704
705   data = (GstCollectData *) list->data;
706
707   GST_DEBUG_OBJECT (pads, "found pad %s:%s at %p", GST_DEBUG_PAD_NAME (pad),
708       data);
709
710   /* clear the stuff we configured */
711   gst_pad_set_chain_function (pad, NULL);
712   gst_pad_set_event_function (pad, NULL);
713   GST_OBJECT_LOCK (pad);
714   gst_pad_set_element_private (pad, NULL);
715   GST_OBJECT_UNLOCK (pad);
716
717   /* backward compat, also remove from data if stopped, note that this function
718    * can only be called when we are stopped because we don't take the
719    * STREAM_LOCK to protect the pads->data list. */
720   if (!pads->priv->started) {
721     GSList *dlist;
722
723     dlist = g_slist_find_custom (pads->data, pad, (GCompareFunc) find_pad);
724     if (dlist) {
725       GstCollectData *pdata = dlist->data;
726
727       pads->data = g_slist_delete_link (pads->data, dlist);
728       unref_data (pdata);
729     }
730   }
731   /* remove from the pad list */
732   pads->priv->pad_list = g_slist_delete_link (pads->priv->pad_list, list);
733   pads->priv->pad_cookie++;
734
735   /* signal waiters because something changed */
736   GST_COLLECT_PADS_EVT_BROADCAST (pads);
737
738   /* deactivate the pad when needed */
739   if (!pads->priv->started)
740     gst_pad_set_active (pad, FALSE);
741
742   /* clean and free the collect data */
743   unref_data (data);
744
745   GST_OBJECT_UNLOCK (pads);
746
747   return TRUE;
748
749 unknown_pad:
750   {
751     GST_WARNING_OBJECT (pads, "cannot remove unknown pad %s:%s",
752         GST_DEBUG_PAD_NAME (pad));
753     GST_OBJECT_UNLOCK (pads);
754     return FALSE;
755   }
756 }
757
758 /*
759  * Must be called with STREAM_LOCK and OBJECT_LOCK.
760  */
761 static void
762 gst_collect_pads_set_flushing_unlocked (GstCollectPads * pads,
763     gboolean flushing)
764 {
765   GSList *walk = NULL;
766
767   /* Update the pads flushing flag */
768   for (walk = pads->priv->pad_list; walk; walk = g_slist_next (walk)) {
769     GstCollectData *cdata = walk->data;
770
771     if (GST_IS_PAD (cdata->pad)) {
772       GST_OBJECT_LOCK (cdata->pad);
773       if (flushing)
774         GST_PAD_SET_FLUSHING (cdata->pad);
775       else
776         GST_PAD_UNSET_FLUSHING (cdata->pad);
777       if (flushing)
778         GST_COLLECT_PADS_STATE_SET (cdata, GST_COLLECT_PADS_STATE_FLUSHING);
779       else
780         GST_COLLECT_PADS_STATE_UNSET (cdata, GST_COLLECT_PADS_STATE_FLUSHING);
781       gst_collect_pads_clear (pads, cdata);
782       GST_OBJECT_UNLOCK (cdata->pad);
783     }
784   }
785
786   /* inform _chain of changes */
787   GST_COLLECT_PADS_EVT_BROADCAST (pads);
788 }
789
790 /**
791  * gst_collect_pads_set_flushing:
792  * @pads: the collectpads to use
793  * @flushing: desired state of the pads
794  *
795  * Change the flushing state of all the pads in the collection. No pad
796  * is able to accept anymore data when @flushing is %TRUE. Calling this
797  * function with @flushing %FALSE makes @pads accept data again.
798  * Caller must ensure that downstream streaming (thread) is not blocked,
799  * e.g. by sending a FLUSH_START downstream.
800  *
801  * MT safe.
802  */
803 void
804 gst_collect_pads_set_flushing (GstCollectPads * pads, gboolean flushing)
805 {
806   g_return_if_fail (pads != NULL);
807   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
808
809   /* NOTE since this eventually calls _pop, some (STREAM_)LOCK is needed here */
810   GST_COLLECT_PADS_STREAM_LOCK (pads);
811   GST_OBJECT_LOCK (pads);
812   gst_collect_pads_set_flushing_unlocked (pads, flushing);
813   GST_OBJECT_UNLOCK (pads);
814   GST_COLLECT_PADS_STREAM_UNLOCK (pads);
815 }
816
817 /**
818  * gst_collect_pads_start:
819  * @pads: the collectpads to use
820  *
821  * Starts the processing of data in the collect_pads.
822  *
823  * MT safe.
824  */
825 void
826 gst_collect_pads_start (GstCollectPads * pads)
827 {
828   GSList *collected;
829
830   g_return_if_fail (pads != NULL);
831   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
832
833   GST_DEBUG_OBJECT (pads, "starting collect pads");
834
835   /* make sure stop and collect cannot be called anymore */
836   GST_COLLECT_PADS_STREAM_LOCK (pads);
837
838   /* make pads streamable */
839   GST_OBJECT_LOCK (pads);
840
841   /* loop over the master pad list and reset the segment */
842   collected = pads->priv->pad_list;
843   for (; collected; collected = g_slist_next (collected)) {
844     GstCollectData *data;
845
846     data = collected->data;
847     gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
848   }
849
850   gst_collect_pads_set_flushing_unlocked (pads, FALSE);
851
852   /* Start collect pads */
853   pads->priv->started = TRUE;
854   GST_OBJECT_UNLOCK (pads);
855   GST_COLLECT_PADS_STREAM_UNLOCK (pads);
856 }
857
858 /**
859  * gst_collect_pads_stop:
860  * @pads: the collectpads to use
861  *
862  * Stops the processing of data in the collect_pads. this function
863  * will also unblock any blocking operations.
864  *
865  * MT safe.
866  */
867 void
868 gst_collect_pads_stop (GstCollectPads * pads)
869 {
870   GSList *collected;
871
872   g_return_if_fail (pads != NULL);
873   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
874
875   GST_DEBUG_OBJECT (pads, "stopping collect pads");
876
877   /* make sure collect and start cannot be called anymore */
878   GST_COLLECT_PADS_STREAM_LOCK (pads);
879
880   /* make pads not accept data anymore */
881   GST_OBJECT_LOCK (pads);
882   gst_collect_pads_set_flushing_unlocked (pads, TRUE);
883
884   /* Stop collect pads */
885   pads->priv->started = FALSE;
886   pads->priv->eospads = 0;
887   pads->priv->queuedpads = 0;
888
889   /* loop over the master pad list and flush buffers */
890   collected = pads->priv->pad_list;
891   for (; collected; collected = g_slist_next (collected)) {
892     GstCollectData *data;
893     GstBuffer **buffer_p;
894
895     data = collected->data;
896     if (data->buffer) {
897       buffer_p = &data->buffer;
898       gst_buffer_replace (buffer_p, NULL);
899       data->pos = 0;
900     }
901     GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_EOS);
902   }
903
904   if (pads->priv->earliest_data)
905     unref_data (pads->priv->earliest_data);
906   pads->priv->earliest_data = NULL;
907   pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
908
909   GST_OBJECT_UNLOCK (pads);
910   /* Wake them up so they can end the chain functions. */
911   GST_COLLECT_PADS_EVT_BROADCAST (pads);
912
913   GST_COLLECT_PADS_STREAM_UNLOCK (pads);
914 }
915
916 /**
917  * gst_collect_pads_peek:
918  * @pads: the collectpads to peek
919  * @data: the data to use
920  *
921  * Peek at the buffer currently queued in @data. This function
922  * should be called with the @pads STREAM_LOCK held, such as in the callback
923  * handler.
924  *
925  * MT safe.
926  *
927  * Returns: The buffer in @data or %NULL if no buffer is queued.
928  *  should unref the buffer after usage.
929  */
930 GstBuffer *
931 gst_collect_pads_peek (GstCollectPads * pads, GstCollectData * data)
932 {
933   GstBuffer *result;
934
935   g_return_val_if_fail (pads != NULL, NULL);
936   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
937   g_return_val_if_fail (data != NULL, NULL);
938
939   if ((result = data->buffer))
940     gst_buffer_ref (result);
941
942   GST_DEBUG_OBJECT (pads, "Peeking at pad %s:%s: buffer=%p",
943       GST_DEBUG_PAD_NAME (data->pad), result);
944
945   return result;
946 }
947
948 /**
949  * gst_collect_pads_pop:
950  * @pads: the collectpads to pop
951  * @data: the data to use
952  *
953  * Pop the buffer currently queued in @data. This function
954  * should be called with the @pads STREAM_LOCK held, such as in the callback
955  * handler.
956  *
957  * MT safe.
958  *
959  * Returns: (transfer full): The buffer in @data or %NULL if no buffer was
960  *   queued. You should unref the buffer after usage.
961  */
962 GstBuffer *
963 gst_collect_pads_pop (GstCollectPads * pads, GstCollectData * data)
964 {
965   GstBuffer *result;
966
967   g_return_val_if_fail (pads != NULL, NULL);
968   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
969   g_return_val_if_fail (data != NULL, NULL);
970
971   if ((result = data->buffer)) {
972     data->buffer = NULL;
973     data->pos = 0;
974     /* one less pad with queued data now */
975     if (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING))
976       pads->priv->queuedpads--;
977   }
978
979   GST_COLLECT_PADS_EVT_BROADCAST (pads);
980
981   GST_DEBUG_OBJECT (pads, "Pop buffer on pad %s:%s: buffer=%p",
982       GST_DEBUG_PAD_NAME (data->pad), result);
983
984   return result;
985 }
986
987 /* pop and unref the currently queued buffer, should be called with STREAM_LOCK
988  * held */
989 static void
990 gst_collect_pads_clear (GstCollectPads * pads, GstCollectData * data)
991 {
992   GstBuffer *buf;
993
994   if ((buf = gst_collect_pads_pop (pads, data)))
995     gst_buffer_unref (buf);
996 }
997
998 /**
999  * gst_collect_pads_available:
1000  * @pads: the collectpads to query
1001  *
1002  * Query how much bytes can be read from each queued buffer. This means
1003  * that the result of this call is the maximum number of bytes that can
1004  * be read from each of the pads.
1005  *
1006  * This function should be called with @pads STREAM_LOCK held, such as
1007  * in the callback.
1008  *
1009  * MT safe.
1010  *
1011  * Returns: The maximum number of bytes queued on all pads. This function
1012  * returns 0 if a pad has no queued buffer.
1013  */
1014 /* we might pre-calculate this in some struct field,
1015  * but would then have to maintain this in _chain and particularly _pop, etc,
1016  * even if element is never interested in this information */
1017 guint
1018 gst_collect_pads_available (GstCollectPads * pads)
1019 {
1020   GSList *collected;
1021   guint result = G_MAXUINT;
1022
1023   g_return_val_if_fail (pads != NULL, 0);
1024   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), 0);
1025
1026   collected = pads->data;
1027   for (; collected; collected = g_slist_next (collected)) {
1028     GstCollectData *pdata;
1029     GstBuffer *buffer;
1030     gint size;
1031
1032     pdata = (GstCollectData *) collected->data;
1033
1034     /* ignore pad with EOS */
1035     if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (pdata,
1036                 GST_COLLECT_PADS_STATE_EOS))) {
1037       GST_DEBUG_OBJECT (pads, "pad %p is EOS", pdata);
1038       continue;
1039     }
1040
1041     /* an empty buffer without EOS is weird when we get here.. */
1042     if (G_UNLIKELY ((buffer = pdata->buffer) == NULL)) {
1043       GST_WARNING_OBJECT (pads, "pad %p has no buffer", pdata);
1044       goto not_filled;
1045     }
1046
1047     /* this is the size left of the buffer */
1048     size = gst_buffer_get_size (buffer) - pdata->pos;
1049     GST_DEBUG_OBJECT (pads, "pad %p has %d bytes left", pdata, size);
1050
1051     /* need to return the min of all available data */
1052     if (size < result)
1053       result = size;
1054   }
1055   /* nothing changed, all must be EOS then, return 0 */
1056   if (G_UNLIKELY (result == G_MAXUINT))
1057     result = 0;
1058
1059   return result;
1060
1061 not_filled:
1062   {
1063     return 0;
1064   }
1065 }
1066
1067 /**
1068  * gst_collect_pads_flush:
1069  * @pads: the collectpads to query
1070  * @data: the data to use
1071  * @size: the number of bytes to flush
1072  *
1073  * Flush @size bytes from the pad @data.
1074  *
1075  * This function should be called with @pads STREAM_LOCK held, such as
1076  * in the callback.
1077  *
1078  * MT safe.
1079  *
1080  * Returns: The number of bytes flushed This can be less than @size and
1081  * is 0 if the pad was end-of-stream.
1082  */
1083 guint
1084 gst_collect_pads_flush (GstCollectPads * pads, GstCollectData * data,
1085     guint size)
1086 {
1087   guint flushsize;
1088   gsize bsize;
1089   GstBuffer *buffer;
1090
1091   g_return_val_if_fail (pads != NULL, 0);
1092   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), 0);
1093   g_return_val_if_fail (data != NULL, 0);
1094
1095   /* no buffer, must be EOS */
1096   if ((buffer = data->buffer) == NULL)
1097     return 0;
1098
1099   bsize = gst_buffer_get_size (buffer);
1100
1101   /* this is what we can flush at max */
1102   flushsize = MIN (size, bsize - data->pos);
1103
1104   data->pos += size;
1105
1106   if (data->pos >= bsize)
1107     /* _clear will also reset data->pos to 0 */
1108     gst_collect_pads_clear (pads, data);
1109
1110   return flushsize;
1111 }
1112
1113 /**
1114  * gst_collect_pads_read_buffer:
1115  * @pads: the collectpads to query
1116  * @data: the data to use
1117  * @size: the number of bytes to read
1118  *
1119  * Get a subbuffer of @size bytes from the given pad @data.
1120  *
1121  * This function should be called with @pads STREAM_LOCK held, such as in the
1122  * callback.
1123  *
1124  * MT safe.
1125  *
1126  * Returns: (transfer full): A sub buffer. The size of the buffer can be less that requested.
1127  * A return of %NULL signals that the pad is end-of-stream.
1128  * Unref the buffer after use.
1129  */
1130 GstBuffer *
1131 gst_collect_pads_read_buffer (GstCollectPads * pads, GstCollectData * data,
1132     guint size)
1133 {
1134   guint readsize;
1135   GstBuffer *buffer;
1136
1137   g_return_val_if_fail (pads != NULL, NULL);
1138   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
1139   g_return_val_if_fail (data != NULL, NULL);
1140
1141   /* no buffer, must be EOS */
1142   if ((buffer = data->buffer) == NULL)
1143     return NULL;
1144
1145   readsize = MIN (size, gst_buffer_get_size (buffer) - data->pos);
1146
1147   return gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, data->pos,
1148       readsize);
1149 }
1150
1151 /**
1152  * gst_collect_pads_take_buffer:
1153  * @pads: the collectpads to query
1154  * @data: the data to use
1155  * @size: the number of bytes to read
1156  *
1157  * Get a subbuffer of @size bytes from the given pad @data. Flushes the amount
1158  * of read bytes.
1159  *
1160  * This function should be called with @pads STREAM_LOCK held, such as in the
1161  * callback.
1162  *
1163  * MT safe.
1164  *
1165  * Returns: A sub buffer. The size of the buffer can be less that requested.
1166  * A return of %NULL signals that the pad is end-of-stream.
1167  * Unref the buffer after use.
1168  */
1169 GstBuffer *
1170 gst_collect_pads_take_buffer (GstCollectPads * pads, GstCollectData * data,
1171     guint size)
1172 {
1173   GstBuffer *buffer = gst_collect_pads_read_buffer (pads, data, size);
1174
1175   if (buffer) {
1176     gst_collect_pads_flush (pads, data, gst_buffer_get_size (buffer));
1177   }
1178   return buffer;
1179 }
1180
1181 /**
1182  * gst_collect_pads_set_waiting:
1183  * @pads: the collectpads
1184  * @data: the data to use
1185  * @waiting: boolean indicating whether this pad should operate
1186  *           in waiting or non-waiting mode
1187  *
1188  * Sets a pad to waiting or non-waiting mode, if at least this pad
1189  * has not been created with locked waiting state,
1190  * in which case nothing happens.
1191  *
1192  * This function should be called with @pads STREAM_LOCK held, such as
1193  * in the callback.
1194  *
1195  * MT safe.
1196  */
1197 void
1198 gst_collect_pads_set_waiting (GstCollectPads * pads, GstCollectData * data,
1199     gboolean waiting)
1200 {
1201   g_return_if_fail (pads != NULL);
1202   g_return_if_fail (GST_IS_COLLECT_PADS (pads));
1203   g_return_if_fail (data != NULL);
1204
1205   GST_DEBUG_OBJECT (pads, "Setting pad %s to waiting %d, locked %d",
1206       GST_PAD_NAME (data->pad), waiting,
1207       GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_LOCKED));
1208
1209   /* Do something only on a change and if not locked */
1210   if (!GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_LOCKED) &&
1211       (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING) !=
1212           ! !waiting)) {
1213     /* Set waiting state for this pad */
1214     if (waiting)
1215       GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_WAITING);
1216     else
1217       GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_WAITING);
1218     /* Update number of queued pads if needed */
1219     if (!data->buffer &&
1220         !GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_EOS)) {
1221       if (waiting)
1222         pads->priv->queuedpads--;
1223       else
1224         pads->priv->queuedpads++;
1225     }
1226
1227     /* signal waiters because something changed */
1228     GST_COLLECT_PADS_EVT_BROADCAST (pads);
1229   }
1230 }
1231
1232 /* see if pads were added or removed and update our stats. Any pad
1233  * added after releasing the LOCK will get collected in the next
1234  * round.
1235  *
1236  * We can do a quick check by checking the cookies, that get changed
1237  * whenever the pad list is updated.
1238  *
1239  * Must be called with STREAM_LOCK.
1240  */
1241 static void
1242 gst_collect_pads_check_pads (GstCollectPads * pads)
1243 {
1244   /* the master list and cookie are protected with LOCK */
1245   GST_OBJECT_LOCK (pads);
1246   if (G_UNLIKELY (pads->priv->pad_cookie != pads->priv->cookie)) {
1247     GSList *collected;
1248
1249     /* clear list and stats */
1250     g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
1251     g_slist_free (pads->data);
1252     pads->data = NULL;
1253     pads->priv->numpads = 0;
1254     pads->priv->queuedpads = 0;
1255     pads->priv->eospads = 0;
1256     if (pads->priv->earliest_data)
1257       unref_data (pads->priv->earliest_data);
1258     pads->priv->earliest_data = NULL;
1259     pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
1260
1261     /* loop over the master pad list */
1262     collected = pads->priv->pad_list;
1263     for (; collected; collected = g_slist_next (collected)) {
1264       GstCollectData *data;
1265
1266       /* update the stats */
1267       pads->priv->numpads++;
1268       data = collected->data;
1269       if (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_EOS))
1270         pads->priv->eospads++;
1271       else if (data->buffer || !GST_COLLECT_PADS_STATE_IS_SET (data,
1272               GST_COLLECT_PADS_STATE_WAITING))
1273         pads->priv->queuedpads++;
1274
1275       /* add to the list of pads to collect */
1276       ref_data (data);
1277       /* preserve order of adding/requesting pads */
1278       pads->data = g_slist_append (pads->data, data);
1279     }
1280     /* and update the cookie */
1281     pads->priv->cookie = pads->priv->pad_cookie;
1282   }
1283   GST_OBJECT_UNLOCK (pads);
1284 }
1285
1286 /* checks if all the pads are collected and call the collectfunction
1287  *
1288  * Should be called with STREAM_LOCK.
1289  *
1290  * Returns: The #GstFlowReturn of collection.
1291  */
1292 static GstFlowReturn
1293 gst_collect_pads_check_collected (GstCollectPads * pads)
1294 {
1295   GstFlowReturn flow_ret = GST_FLOW_OK;
1296   GstCollectPadsFunction func;
1297   gpointer user_data;
1298
1299   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), GST_FLOW_ERROR);
1300
1301   GST_OBJECT_LOCK (pads);
1302   func = pads->priv->func;
1303   user_data = pads->priv->user_data;
1304   GST_OBJECT_UNLOCK (pads);
1305
1306   g_return_val_if_fail (pads->priv->func != NULL, GST_FLOW_NOT_SUPPORTED);
1307
1308   /* check for new pads, update stats etc.. */
1309   gst_collect_pads_check_pads (pads);
1310
1311   if (G_UNLIKELY (pads->priv->eospads == pads->priv->numpads)) {
1312     /* If all our pads are EOS just collect once to let the element
1313      * do its final EOS handling. */
1314     GST_DEBUG_OBJECT (pads, "All active pads (%d) are EOS, calling %s",
1315         pads->priv->numpads, GST_DEBUG_FUNCPTR_NAME (func));
1316
1317     if (G_UNLIKELY (g_atomic_int_compare_and_exchange (&pads->priv->seeking,
1318                 TRUE, FALSE) == TRUE)) {
1319       GST_INFO_OBJECT (pads, "finished seeking");
1320     }
1321     do {
1322       flow_ret = func (pads, user_data);
1323     } while (flow_ret == GST_FLOW_OK);
1324   } else {
1325     gboolean collected = FALSE;
1326
1327     /* We call the collected function as long as our condition matches. */
1328     while (((pads->priv->queuedpads + pads->priv->eospads) >=
1329             pads->priv->numpads)) {
1330       GST_DEBUG_OBJECT (pads,
1331           "All active pads (%d + %d >= %d) have data, " "calling %s",
1332           pads->priv->queuedpads, pads->priv->eospads, pads->priv->numpads,
1333           GST_DEBUG_FUNCPTR_NAME (func));
1334
1335       if (G_UNLIKELY (g_atomic_int_compare_and_exchange (&pads->priv->seeking,
1336                   TRUE, FALSE) == TRUE)) {
1337         GST_INFO_OBJECT (pads, "finished seeking");
1338       }
1339       flow_ret = func (pads, user_data);
1340       collected = TRUE;
1341
1342       /* break on error */
1343       if (flow_ret != GST_FLOW_OK)
1344         break;
1345       /* Don't keep looping after telling the element EOS or flushing */
1346       if (pads->priv->queuedpads == 0)
1347         break;
1348     }
1349     if (!collected)
1350       GST_DEBUG_OBJECT (pads, "Not all active pads (%d) have data, continuing",
1351           pads->priv->numpads);
1352   }
1353   return flow_ret;
1354 }
1355
1356
1357 /* General overview:
1358  * - only pad with a buffer can determine earliest_data (and earliest_time)
1359  * - only segment info determines (non-)waiting state
1360  * - ? perhaps use _stream_time for comparison
1361  *   (which muxers might have use as well ?)
1362  */
1363
1364 /*
1365  * Function to recalculate the waiting state of all pads.
1366  *
1367  * Must be called with STREAM_LOCK.
1368  *
1369  * Returns %TRUE if a pad was set to waiting
1370  * (from non-waiting state).
1371  */
1372 static gboolean
1373 gst_collect_pads_recalculate_waiting (GstCollectPads * pads)
1374 {
1375   GSList *collected;
1376   gboolean result = FALSE;
1377
1378   /* If earliest time is not known, there is nothing to do. */
1379   if (pads->priv->earliest_data == NULL)
1380     return FALSE;
1381
1382   for (collected = pads->data; collected; collected = g_slist_next (collected)) {
1383     GstCollectData *data = (GstCollectData *) collected->data;
1384     int cmp_res;
1385     GstClockTime comp_time;
1386
1387     /* check if pad has a segment */
1388     if (data->segment.format == GST_FORMAT_UNDEFINED) {
1389       GST_WARNING_OBJECT (pads,
1390           "GstCollectPads has no time segment, assuming 0 based.");
1391       gst_segment_init (&data->segment, GST_FORMAT_TIME);
1392       GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_NEW_SEGMENT);
1393     }
1394
1395     /* check segment format */
1396     if (data->segment.format != GST_FORMAT_TIME) {
1397       GST_ERROR_OBJECT (pads, "GstCollectPads can handle only time segments.");
1398       continue;
1399     }
1400
1401     /* check if the waiting state should be changed */
1402     comp_time = data->segment.position;
1403     cmp_res = pads->priv->compare_func (pads, data, comp_time,
1404         pads->priv->earliest_data, pads->priv->earliest_time,
1405         pads->priv->compare_user_data);
1406     if (cmp_res > 0)
1407       /* stop waiting */
1408       gst_collect_pads_set_waiting (pads, data, FALSE);
1409     else {
1410       if (!GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING)) {
1411         /* start waiting */
1412         gst_collect_pads_set_waiting (pads, data, TRUE);
1413         result = TRUE;
1414       }
1415     }
1416   }
1417
1418   return result;
1419 }
1420
1421 /**
1422  * gst_collect_pads_find_best_pad:
1423  * @pads: the collectpads to use
1424  * @data: returns the collectdata for earliest data
1425  * @time: returns the earliest available buffertime
1426  *
1427  * Find the oldest/best pad, i.e. pad holding the oldest buffer and
1428  * and return the corresponding #GstCollectData and buffertime.
1429  *
1430  * This function should be called with STREAM_LOCK held,
1431  * such as in the callback.
1432  */
1433 static void
1434 gst_collect_pads_find_best_pad (GstCollectPads * pads,
1435     GstCollectData ** data, GstClockTime * time)
1436 {
1437   GSList *collected;
1438   GstCollectData *best = NULL;
1439   GstClockTime best_time = GST_CLOCK_TIME_NONE;
1440
1441   g_return_if_fail (data != NULL);
1442   g_return_if_fail (time != NULL);
1443
1444   for (collected = pads->data; collected; collected = g_slist_next (collected)) {
1445     GstBuffer *buffer;
1446     GstCollectData *data = (GstCollectData *) collected->data;
1447     GstClockTime timestamp;
1448
1449     buffer = gst_collect_pads_peek (pads, data);
1450     /* if we have a buffer check if it is better then the current best one */
1451     if (buffer != NULL) {
1452       timestamp = GST_BUFFER_DTS (buffer);
1453       if (!GST_CLOCK_TIME_IS_VALID (timestamp)) {
1454         timestamp = GST_BUFFER_PTS (buffer);
1455       }
1456       gst_buffer_unref (buffer);
1457       if (best == NULL || pads->priv->compare_func (pads, data, timestamp,
1458               best, best_time, pads->priv->compare_user_data) < 0) {
1459         best = data;
1460         best_time = timestamp;
1461       }
1462     }
1463   }
1464
1465   /* set earliest time */
1466   *data = best;
1467   *time = best_time;
1468
1469   GST_DEBUG_OBJECT (pads, "best pad %s, best time %" GST_TIME_FORMAT,
1470       best ? GST_PAD_NAME (((GstCollectData *) best)->pad) : "(nil)",
1471       GST_TIME_ARGS (best_time));
1472 }
1473
1474 /*
1475  * Function to recalculate earliest_data and earliest_timestamp. This also calls
1476  * gst_collect_pads_recalculate_waiting
1477  *
1478  * Must be called with STREAM_LOCK.
1479  */
1480 static gboolean
1481 gst_collect_pads_recalculate_full (GstCollectPads * pads)
1482 {
1483   if (pads->priv->earliest_data)
1484     unref_data (pads->priv->earliest_data);
1485   gst_collect_pads_find_best_pad (pads, &pads->priv->earliest_data,
1486       &pads->priv->earliest_time);
1487   if (pads->priv->earliest_data)
1488     ref_data (pads->priv->earliest_data);
1489   return gst_collect_pads_recalculate_waiting (pads);
1490 }
1491
1492 /*
1493  * Default collect callback triggered when #GstCollectPads gathered all data.
1494  *
1495  * Called with STREAM_LOCK.
1496  */
1497 static GstFlowReturn
1498 gst_collect_pads_default_collected (GstCollectPads * pads, gpointer user_data)
1499 {
1500   GstCollectData *best = NULL;
1501   GstBuffer *buffer;
1502   GstFlowReturn ret = GST_FLOW_OK;
1503   GstCollectPadsBufferFunction func;
1504   gpointer buffer_user_data;
1505
1506   g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), GST_FLOW_ERROR);
1507
1508   GST_OBJECT_LOCK (pads);
1509   func = pads->priv->buffer_func;
1510   buffer_user_data = pads->priv->buffer_user_data;
1511   GST_OBJECT_UNLOCK (pads);
1512
1513   g_return_val_if_fail (func != NULL, GST_FLOW_NOT_SUPPORTED);
1514
1515   /* Find the oldest pad at all cost */
1516   if (gst_collect_pads_recalculate_full (pads)) {
1517     /* waiting was switched on,
1518      * so give another thread a chance to deliver a possibly
1519      * older buffer; don't charge on yet with the current oldest */
1520     ret = GST_FLOW_OK;
1521     goto done;
1522   }
1523
1524   best = pads->priv->earliest_data;
1525
1526   /* No data collected means EOS. */
1527   if (G_UNLIKELY (best == NULL)) {
1528     ret = func (pads, best, NULL, buffer_user_data);
1529     if (ret == GST_FLOW_OK)
1530       ret = GST_FLOW_EOS;
1531     goto done;
1532   }
1533
1534   /* make sure that the pad we take a buffer from is waiting;
1535    * otherwise popping a buffer will seem not to have happened
1536    * and collectpads can get into a busy loop */
1537   gst_collect_pads_set_waiting (pads, best, TRUE);
1538
1539   /* Send buffer */
1540   buffer = gst_collect_pads_pop (pads, best);
1541   ret = func (pads, best, buffer, buffer_user_data);
1542
1543   /* maybe non-waiting was forced to waiting above due to
1544    * newsegment events coming too sparsely,
1545    * so re-check to restore state to avoid hanging/waiting */
1546   gst_collect_pads_recalculate_full (pads);
1547
1548 done:
1549   return ret;
1550 }
1551
1552 /*
1553  * Default timestamp compare function.
1554  */
1555 static gint
1556 gst_collect_pads_default_compare_func (GstCollectPads * pads,
1557     GstCollectData * data1, GstClockTime timestamp1,
1558     GstCollectData * data2, GstClockTime timestamp2, gpointer user_data)
1559 {
1560
1561   GST_LOG_OBJECT (pads, "comparing %" GST_TIME_FORMAT
1562       " and %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp1),
1563       GST_TIME_ARGS (timestamp2));
1564   /* non-valid timestamps go first as they are probably headers or so */
1565   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp1)))
1566     return GST_CLOCK_TIME_IS_VALID (timestamp2) ? -1 : 0;
1567
1568   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp2)))
1569     return 1;
1570
1571   /* compare timestamp */
1572   if (timestamp1 < timestamp2)
1573     return -1;
1574
1575   if (timestamp1 > timestamp2)
1576     return 1;
1577
1578   return 0;
1579 }
1580
1581 /* called with STREAM_LOCK */
1582 static void
1583 gst_collect_pads_handle_position_update (GstCollectPads * pads,
1584     GstCollectData * data, GstClockTime new_pos)
1585 {
1586   gint cmp_res;
1587
1588   /* If oldest time is not known, or current pad got newsegment;
1589    * recalculate the state */
1590   if (!pads->priv->earliest_data || pads->priv->earliest_data == data) {
1591     gst_collect_pads_recalculate_full (pads);
1592     goto exit;
1593   }
1594
1595   /* Check if the waiting state of the pad should change. */
1596   cmp_res =
1597       pads->priv->compare_func (pads, data, new_pos,
1598       pads->priv->earliest_data, pads->priv->earliest_time,
1599       pads->priv->compare_user_data);
1600
1601   if (cmp_res > 0)
1602     /* Stop waiting */
1603     gst_collect_pads_set_waiting (pads, data, FALSE);
1604
1605 exit:
1606   return;
1607
1608 }
1609
1610 static GstClockTime
1611 gst_collect_pads_clip_time (GstCollectPads * pads, GstCollectData * data,
1612     GstClockTime time)
1613 {
1614   GstClockTime otime = time;
1615   GstBuffer *in, *out = NULL;
1616
1617   if (pads->priv->clip_func) {
1618     in = gst_buffer_new ();
1619     GST_BUFFER_PTS (in) = time;
1620     GST_BUFFER_DTS (in) = time;
1621     pads->priv->clip_func (pads, data, in, &out, pads->priv->clip_user_data);
1622     if (out) {
1623       otime = GST_BUFFER_PTS (out);
1624       gst_buffer_unref (out);
1625     } else {
1626       /* FIXME should distinguish between ahead or after segment,
1627        * let's assume after segment and use some large time ... */
1628       otime = G_MAXINT64 / 2;
1629     }
1630   }
1631
1632   return otime;
1633 }
1634
1635 /**
1636  * gst_collect_pads_event_default:
1637  * @pads: the collectpads to use
1638  * @data: collect data of corresponding pad
1639  * @event: event being processed
1640  * @discard: process but do not send event downstream
1641  *
1642  * Default #GstCollectPads event handling that elements should always
1643  * chain up to to ensure proper operation.  Element might however indicate
1644  * event should not be forwarded downstream.
1645  */
1646 gboolean
1647 gst_collect_pads_event_default (GstCollectPads * pads, GstCollectData * data,
1648     GstEvent * event, gboolean discard)
1649 {
1650   gboolean res = TRUE;
1651   GstCollectPadsBufferFunction buffer_func;
1652   GstObject *parent;
1653   GstPad *pad;
1654
1655   GST_OBJECT_LOCK (pads);
1656   buffer_func = pads->priv->buffer_func;
1657   GST_OBJECT_UNLOCK (pads);
1658
1659   pad = data->pad;
1660   parent = GST_OBJECT_PARENT (pad);
1661
1662   switch (GST_EVENT_TYPE (event)) {
1663     case GST_EVENT_FLUSH_START:
1664     {
1665       if (g_atomic_int_get (&pads->priv->seeking)) {
1666         /* drop all but the first FLUSH_STARTs when seeking */
1667         if (g_atomic_int_compare_and_exchange (&pads->priv->pending_flush_start,
1668                 TRUE, FALSE) == FALSE)
1669           goto eat;
1670
1671         /* unblock collect pads */
1672         gst_pad_event_default (pad, parent, event);
1673         event = NULL;
1674
1675         GST_COLLECT_PADS_STREAM_LOCK (pads);
1676         /* Start flushing. We never call gst_collect_pads_set_flushing (FALSE), we
1677          * instead wait until each pad gets its FLUSH_STOP and let that reset the pad to
1678          * non-flushing (which happens in gst_collect_pads_event_default).
1679          */
1680         gst_collect_pads_set_flushing (pads, TRUE);
1681
1682         if (pads->priv->flush_func)
1683           pads->priv->flush_func (pads, pads->priv->flush_user_data);
1684
1685         g_atomic_int_set (&pads->priv->pending_flush_stop, TRUE);
1686         GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1687
1688         goto eat;
1689       } else {
1690         /* forward event to unblock check_collected */
1691         GST_DEBUG_OBJECT (pad, "forwarding flush start");
1692         res = gst_pad_event_default (pad, parent, event);
1693         event = NULL;
1694
1695         /* now unblock the chain function.
1696          * no cond per pad, so they all unblock,
1697          * non-flushing block again */
1698         GST_COLLECT_PADS_STREAM_LOCK (pads);
1699         GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_FLUSHING);
1700         gst_collect_pads_clear (pads, data);
1701
1702         /* cater for possible default muxing functionality */
1703         if (buffer_func) {
1704           /* restore to initial state */
1705           gst_collect_pads_set_waiting (pads, data, TRUE);
1706           /* if the current pad is affected, reset state, recalculate later */
1707           if (pads->priv->earliest_data == data) {
1708             unref_data (data);
1709             pads->priv->earliest_data = NULL;
1710             pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
1711           }
1712         }
1713
1714         GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1715
1716         goto eat;
1717       }
1718     }
1719     case GST_EVENT_FLUSH_STOP:
1720     {
1721       /* flush the 1 buffer queue */
1722       GST_COLLECT_PADS_STREAM_LOCK (pads);
1723       GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_FLUSHING);
1724       gst_collect_pads_clear (pads, data);
1725       /* we need new segment info after the flush */
1726       gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
1727       GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_NEW_SEGMENT);
1728       /* if the pad was EOS, remove the EOS flag and
1729        * decrement the number of eospads */
1730       if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
1731                   GST_COLLECT_PADS_STATE_EOS))) {
1732         if (!GST_COLLECT_PADS_STATE_IS_SET (data,
1733                 GST_COLLECT_PADS_STATE_WAITING))
1734           pads->priv->queuedpads++;
1735         if (!g_atomic_int_get (&pads->priv->seeking)) {
1736           pads->priv->eospads--;
1737         }
1738         GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_EOS);
1739       }
1740       GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1741
1742       if (g_atomic_int_get (&pads->priv->seeking)) {
1743         if (g_atomic_int_compare_and_exchange (&pads->priv->pending_flush_stop,
1744                 TRUE, FALSE))
1745           goto forward;
1746         else
1747           goto eat;
1748       } else {
1749         goto forward;
1750       }
1751     }
1752     case GST_EVENT_EOS:
1753     {
1754       GST_COLLECT_PADS_STREAM_LOCK (pads);
1755       /* if the pad was not EOS, make it EOS and so we
1756        * have one more eospad */
1757       if (G_LIKELY (!GST_COLLECT_PADS_STATE_IS_SET (data,
1758                   GST_COLLECT_PADS_STATE_EOS))) {
1759         GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_EOS);
1760         if (!GST_COLLECT_PADS_STATE_IS_SET (data,
1761                 GST_COLLECT_PADS_STATE_WAITING))
1762           pads->priv->queuedpads--;
1763         pads->priv->eospads++;
1764       }
1765       /* check if we need collecting anything, we ignore the result. */
1766       gst_collect_pads_check_collected (pads);
1767       GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1768
1769       goto eat;
1770     }
1771     case GST_EVENT_SEGMENT:
1772     {
1773       GstSegment seg;
1774
1775       GST_COLLECT_PADS_STREAM_LOCK (pads);
1776
1777       gst_event_copy_segment (event, &seg);
1778
1779       GST_DEBUG_OBJECT (data->pad, "got segment %" GST_SEGMENT_FORMAT, &seg);
1780
1781       /* default collection can not handle other segment formats than time */
1782       if (buffer_func && seg.format != GST_FORMAT_TIME) {
1783         GST_WARNING_OBJECT (pads, "GstCollectPads default collecting "
1784             "can only handle time segments. Non time segment ignored.");
1785         goto newsegment_done;
1786       }
1787
1788       /* need to update segment first */
1789       data->segment = seg;
1790       GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_NEW_SEGMENT);
1791
1792       /* now we can use for e.g. running time */
1793       seg.position =
1794           gst_collect_pads_clip_time (pads, data, seg.start + seg.offset);
1795       /* update again */
1796       data->segment = seg;
1797
1798       /* default muxing functionality */
1799       if (!buffer_func)
1800         goto newsegment_done;
1801
1802       gst_collect_pads_handle_position_update (pads, data, seg.position);
1803
1804     newsegment_done:
1805       GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1806       /* we must not forward this event since multiple segments will be
1807        * accumulated and this is certainly not what we want. */
1808       goto eat;
1809     }
1810     case GST_EVENT_GAP:
1811     {
1812       GstClockTime start, duration;
1813
1814       GST_COLLECT_PADS_STREAM_LOCK (pads);
1815
1816       gst_event_parse_gap (event, &start, &duration);
1817       /* FIXME, handle reverse playback case */
1818       if (GST_CLOCK_TIME_IS_VALID (duration))
1819         start += duration;
1820       /* we do not expect another buffer until after gap,
1821        * so that is our position now */
1822       data->segment.position = gst_collect_pads_clip_time (pads, data, start);
1823
1824       gst_collect_pads_handle_position_update (pads, data,
1825           data->segment.position);
1826
1827       GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1828       goto eat;
1829     }
1830     case GST_EVENT_STREAM_START:
1831       /* drop stream start events, element must create its own start event,
1832        * we can't just forward the first random stream start event we get */
1833       goto eat;
1834     case GST_EVENT_CAPS:
1835       goto eat;
1836     default:
1837       /* forward other events */
1838       goto forward;
1839   }
1840
1841 eat:
1842   if (event)
1843     gst_event_unref (event);
1844   return res;
1845
1846 forward:
1847   if (discard)
1848     goto eat;
1849   else
1850     return gst_pad_event_default (pad, parent, event);
1851 }
1852
1853 typedef struct
1854 {
1855   GstEvent *event;
1856   gboolean result;
1857 } EventData;
1858
1859 static gboolean
1860 event_forward_func (GstPad * pad, EventData * data)
1861 {
1862   gboolean ret = TRUE;
1863   GstPad *peer = gst_pad_get_peer (pad);
1864
1865   if (peer) {
1866     ret = gst_pad_send_event (peer, gst_event_ref (data->event));
1867     gst_object_unref (peer);
1868   }
1869
1870   data->result &= ret;
1871   /* Always send to all pads */
1872   return FALSE;
1873 }
1874
1875 static gboolean
1876 forward_event_to_all_sinkpads (GstPad * srcpad, GstEvent * event)
1877 {
1878   EventData data;
1879
1880   data.event = event;
1881   data.result = TRUE;
1882
1883   gst_pad_forward (srcpad, (GstPadForwardFunction) event_forward_func, &data);
1884
1885   gst_event_unref (event);
1886
1887   return data.result;
1888 }
1889
1890 /**
1891  * gst_collect_pads_src_event_default:
1892  * @pads: the collectpads to use
1893  * @pad: src #GstPad that received the event
1894  * @event: event being processed
1895  *
1896  * Default #GstCollectPads event handling for the src pad of elements.
1897  * Elements can chain up to this to let flushing seek event handling
1898  * be done by GstCollectPads.
1899  *
1900  * Since: 1.4
1901  */
1902 gboolean
1903 gst_collect_pads_src_event_default (GstCollectPads * pads, GstPad * pad,
1904     GstEvent * event)
1905 {
1906   GstObject *parent;
1907   gboolean res = TRUE;
1908
1909   parent = GST_OBJECT_PARENT (pad);
1910
1911   switch (GST_EVENT_TYPE (event)) {
1912     case GST_EVENT_SEEK:{
1913       GstSeekFlags flags;
1914
1915       pads->priv->eospads = 0;
1916
1917       GST_INFO_OBJECT (pads, "starting seek");
1918
1919       gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL, NULL);
1920       if (flags & GST_SEEK_FLAG_FLUSH) {
1921         g_atomic_int_set (&pads->priv->seeking, TRUE);
1922         g_atomic_int_set (&pads->priv->pending_flush_start, TRUE);
1923         /* forward the seek upstream */
1924         res = forward_event_to_all_sinkpads (pad, event);
1925         event = NULL;
1926         if (!res) {
1927           g_atomic_int_set (&pads->priv->seeking, FALSE);
1928           g_atomic_int_set (&pads->priv->pending_flush_start, FALSE);
1929         }
1930       }
1931
1932       GST_INFO_OBJECT (pads, "seek done, result: %d", res);
1933
1934       break;
1935     }
1936     default:
1937       break;
1938   }
1939
1940   if (event)
1941     res = gst_pad_event_default (pad, parent, event);
1942
1943   return res;
1944 }
1945
1946 static gboolean
1947 gst_collect_pads_event_default_internal (GstCollectPads * pads,
1948     GstCollectData * data, GstEvent * event, gpointer user_data)
1949 {
1950   return gst_collect_pads_event_default (pads, data, event, FALSE);
1951 }
1952
1953 static gboolean
1954 gst_collect_pads_event (GstPad * pad, GstObject * parent, GstEvent * event)
1955 {
1956   gboolean res = FALSE, need_unlock = FALSE;
1957   GstCollectData *data;
1958   GstCollectPads *pads;
1959   GstCollectPadsEventFunction event_func;
1960   gpointer event_user_data;
1961
1962   /* some magic to get the managing collect_pads */
1963   GST_OBJECT_LOCK (pad);
1964   data = (GstCollectData *) gst_pad_get_element_private (pad);
1965   if (G_UNLIKELY (data == NULL))
1966     goto pad_removed;
1967   ref_data (data);
1968   GST_OBJECT_UNLOCK (pad);
1969
1970   res = FALSE;
1971
1972   pads = data->collect;
1973
1974   GST_DEBUG_OBJECT (data->pad, "Got %s event on sink pad",
1975       GST_EVENT_TYPE_NAME (event));
1976
1977   GST_OBJECT_LOCK (pads);
1978   event_func = pads->priv->event_func;
1979   event_user_data = pads->priv->event_user_data;
1980   GST_OBJECT_UNLOCK (pads);
1981
1982   if (GST_EVENT_IS_SERIALIZED (event)) {
1983     GST_COLLECT_PADS_STREAM_LOCK (pads);
1984     need_unlock = TRUE;
1985   }
1986
1987   if (G_LIKELY (event_func)) {
1988     res = event_func (pads, data, event, event_user_data);
1989   }
1990
1991   if (need_unlock)
1992     GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1993
1994   unref_data (data);
1995   return res;
1996
1997   /* ERRORS */
1998 pad_removed:
1999   {
2000     GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2001     GST_OBJECT_UNLOCK (pad);
2002     return FALSE;
2003   }
2004 }
2005
2006 /**
2007  * gst_collect_pads_query_default:
2008  * @pads: the collectpads to use
2009  * @data: collect data of corresponding pad
2010  * @query: query being processed
2011  * @discard: process but do not send event downstream
2012  *
2013  * Default #GstCollectPads query handling that elements should always
2014  * chain up to to ensure proper operation.  Element might however indicate
2015  * query should not be forwarded downstream.
2016  */
2017 gboolean
2018 gst_collect_pads_query_default (GstCollectPads * pads, GstCollectData * data,
2019     GstQuery * query, gboolean discard)
2020 {
2021   gboolean res = TRUE;
2022   GstObject *parent;
2023   GstPad *pad;
2024
2025   pad = data->pad;
2026   parent = GST_OBJECT_PARENT (pad);
2027
2028   switch (GST_QUERY_TYPE (query)) {
2029     case GST_QUERY_SEEKING:
2030     {
2031       GstFormat format;
2032
2033       /* don't pass it along as some (file)sink might claim it does
2034        * whereas with a collectpads in between that will not likely work */
2035       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2036       gst_query_set_seeking (query, format, FALSE, 0, -1);
2037       res = TRUE;
2038       discard = TRUE;
2039       break;
2040     }
2041     default:
2042       break;
2043   }
2044
2045   if (!discard)
2046     return gst_pad_query_default (pad, parent, query);
2047   else
2048     return res;
2049 }
2050
2051 static gboolean
2052 gst_collect_pads_query_default_internal (GstCollectPads * pads,
2053     GstCollectData * data, GstQuery * query, gpointer user_data)
2054 {
2055   return gst_collect_pads_query_default (pads, data, query, FALSE);
2056 }
2057
2058 static gboolean
2059 gst_collect_pads_query (GstPad * pad, GstObject * parent, GstQuery * query)
2060 {
2061   gboolean res = FALSE, need_unlock = FALSE;
2062   GstCollectData *data;
2063   GstCollectPads *pads;
2064   GstCollectPadsQueryFunction query_func;
2065   gpointer query_user_data;
2066
2067   GST_DEBUG_OBJECT (pad, "Got %s query on sink pad",
2068       GST_QUERY_TYPE_NAME (query));
2069
2070   /* some magic to get the managing collect_pads */
2071   GST_OBJECT_LOCK (pad);
2072   data = (GstCollectData *) gst_pad_get_element_private (pad);
2073   if (G_UNLIKELY (data == NULL))
2074     goto pad_removed;
2075   ref_data (data);
2076   GST_OBJECT_UNLOCK (pad);
2077
2078   pads = data->collect;
2079
2080   GST_OBJECT_LOCK (pads);
2081   query_func = pads->priv->query_func;
2082   query_user_data = pads->priv->query_user_data;
2083   GST_OBJECT_UNLOCK (pads);
2084
2085   if (GST_QUERY_IS_SERIALIZED (query)) {
2086     GST_COLLECT_PADS_STREAM_LOCK (pads);
2087     need_unlock = TRUE;
2088   }
2089
2090   if (G_LIKELY (query_func)) {
2091     res = query_func (pads, data, query, query_user_data);
2092   }
2093
2094   if (need_unlock)
2095     GST_COLLECT_PADS_STREAM_UNLOCK (pads);
2096
2097   unref_data (data);
2098   return res;
2099
2100   /* ERRORS */
2101 pad_removed:
2102   {
2103     GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2104     GST_OBJECT_UNLOCK (pad);
2105     return FALSE;
2106   }
2107 }
2108
2109
2110 /* For each buffer we receive we check if our collected condition is reached
2111  * and if so we call the collected function. When this is done we check if
2112  * data has been unqueued. If data is still queued we wait holding the stream
2113  * lock to make sure no EOS event can happen while we are ready to be
2114  * collected 
2115  */
2116 static GstFlowReturn
2117 gst_collect_pads_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2118 {
2119   GstCollectData *data;
2120   GstCollectPads *pads;
2121   GstFlowReturn ret;
2122   GstBuffer **buffer_p;
2123   guint32 cookie;
2124
2125   GST_DEBUG ("Got buffer for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2126
2127   /* some magic to get the managing collect_pads */
2128   GST_OBJECT_LOCK (pad);
2129   data = (GstCollectData *) gst_pad_get_element_private (pad);
2130   if (G_UNLIKELY (data == NULL))
2131     goto no_data;
2132   ref_data (data);
2133   GST_OBJECT_UNLOCK (pad);
2134
2135   pads = data->collect;
2136
2137   GST_COLLECT_PADS_STREAM_LOCK (pads);
2138   /* if not started, bail out */
2139   if (G_UNLIKELY (!pads->priv->started))
2140     goto not_started;
2141   /* check if this pad is flushing */
2142   if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
2143               GST_COLLECT_PADS_STATE_FLUSHING)))
2144     goto flushing;
2145   /* pad was EOS, we can refuse this data */
2146   if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
2147               GST_COLLECT_PADS_STATE_EOS)))
2148     goto eos;
2149
2150   /* see if we need to clip */
2151   if (pads->priv->clip_func) {
2152     GstBuffer *outbuf = NULL;
2153     ret =
2154         pads->priv->clip_func (pads, data, buffer, &outbuf,
2155         pads->priv->clip_user_data);
2156     buffer = outbuf;
2157
2158     if (G_UNLIKELY (outbuf == NULL))
2159       goto clipped;
2160
2161     if (G_UNLIKELY (ret == GST_FLOW_EOS))
2162       goto eos;
2163     else if (G_UNLIKELY (ret != GST_FLOW_OK))
2164       goto error;
2165   }
2166
2167   GST_DEBUG_OBJECT (pads, "Queuing buffer %p for pad %s:%s", buffer,
2168       GST_DEBUG_PAD_NAME (pad));
2169
2170   /* One more pad has data queued */
2171   if (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING))
2172     pads->priv->queuedpads++;
2173   buffer_p = &data->buffer;
2174   gst_buffer_replace (buffer_p, buffer);
2175
2176   /* update segment last position if in TIME */
2177   if (G_LIKELY (data->segment.format == GST_FORMAT_TIME)) {
2178     GstClockTime timestamp;
2179
2180     timestamp = GST_BUFFER_DTS (buffer);
2181     if (!GST_CLOCK_TIME_IS_VALID (timestamp))
2182       timestamp = GST_BUFFER_PTS (buffer);
2183
2184     if (GST_CLOCK_TIME_IS_VALID (timestamp))
2185       data->segment.position = timestamp;
2186   }
2187
2188   /* While we have data queued on this pad try to collect stuff */
2189   do {
2190     /* Check if our collected condition is matched and call the collected
2191      * function if it is */
2192     ret = gst_collect_pads_check_collected (pads);
2193     /* when an error occurs, we want to report this back to the caller ASAP
2194      * without having to block if the buffer was not popped */
2195     if (G_UNLIKELY (ret != GST_FLOW_OK))
2196       goto error;
2197
2198     /* data was consumed, we can exit and accept new data */
2199     if (data->buffer == NULL)
2200       break;
2201
2202     /* Having the _INIT here means we don't care about any broadcast up to here
2203      * (most of which occur with STREAM_LOCK held, so could not have happened
2204      * anyway).  We do care about e.g. a remove initiated broadcast as of this
2205      * point.  Putting it here also makes this thread ignores any evt it raised
2206      * itself (as is a usual WAIT semantic).
2207      */
2208     GST_COLLECT_PADS_EVT_INIT (cookie);
2209
2210     /* pad could be removed and re-added */
2211     unref_data (data);
2212     GST_OBJECT_LOCK (pad);
2213     if (G_UNLIKELY ((data = gst_pad_get_element_private (pad)) == NULL))
2214       goto pad_removed;
2215     ref_data (data);
2216     GST_OBJECT_UNLOCK (pad);
2217
2218     GST_DEBUG_OBJECT (pads, "Pad %s:%s has a buffer queued, waiting",
2219         GST_DEBUG_PAD_NAME (pad));
2220
2221     /* wait to be collected, this must happen from another thread triggered
2222      * by the _chain function of another pad. We release the lock so we
2223      * can get stopped or flushed as well. We can however not get EOS
2224      * because we still hold the STREAM_LOCK.
2225      */
2226     GST_COLLECT_PADS_STREAM_UNLOCK (pads);
2227     GST_COLLECT_PADS_EVT_WAIT (pads, cookie);
2228     GST_COLLECT_PADS_STREAM_LOCK (pads);
2229
2230     GST_DEBUG_OBJECT (pads, "Pad %s:%s resuming", GST_DEBUG_PAD_NAME (pad));
2231
2232     /* after a signal, we could be stopped */
2233     if (G_UNLIKELY (!pads->priv->started))
2234       goto not_started;
2235     /* check if this pad is flushing */
2236     if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
2237                 GST_COLLECT_PADS_STATE_FLUSHING)))
2238       goto flushing;
2239   }
2240   while (data->buffer != NULL);
2241
2242 unlock_done:
2243   GST_COLLECT_PADS_STREAM_UNLOCK (pads);
2244   /* data is definitely NULL if pad_removed goto was run. */
2245   if (data)
2246     unref_data (data);
2247   if (buffer)
2248     gst_buffer_unref (buffer);
2249   return ret;
2250
2251 pad_removed:
2252   {
2253     GST_WARNING ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2254     GST_OBJECT_UNLOCK (pad);
2255     ret = GST_FLOW_NOT_LINKED;
2256     goto unlock_done;
2257   }
2258   /* ERRORS */
2259 no_data:
2260   {
2261     GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2262     GST_OBJECT_UNLOCK (pad);
2263     gst_buffer_unref (buffer);
2264     return GST_FLOW_NOT_LINKED;
2265   }
2266 not_started:
2267   {
2268     GST_DEBUG ("not started");
2269     gst_collect_pads_clear (pads, data);
2270     ret = GST_FLOW_FLUSHING;
2271     goto unlock_done;
2272   }
2273 flushing:
2274   {
2275     GST_DEBUG ("pad %s:%s is flushing", GST_DEBUG_PAD_NAME (pad));
2276     gst_collect_pads_clear (pads, data);
2277     ret = GST_FLOW_FLUSHING;
2278     goto unlock_done;
2279   }
2280 eos:
2281   {
2282     /* we should not post an error for this, just inform upstream that
2283      * we don't expect anything anymore */
2284     GST_DEBUG ("pad %s:%s is eos", GST_DEBUG_PAD_NAME (pad));
2285     ret = GST_FLOW_EOS;
2286     goto unlock_done;
2287   }
2288 clipped:
2289   {
2290     GST_DEBUG ("clipped buffer on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2291     ret = GST_FLOW_OK;
2292     goto unlock_done;
2293   }
2294 error:
2295   {
2296     /* we print the error, the element should post a reasonable error
2297      * message for fatal errors */
2298     GST_DEBUG ("collect failed, reason %d (%s)", ret, gst_flow_get_name (ret));
2299     gst_collect_pads_clear (pads, data);
2300     goto unlock_done;
2301   }
2302 }