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>
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.
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.
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.
24 * SECTION:gstcollectpads
25 * @title: GstCollectPads
26 * @short_description: manages a set of pads that operate in collect mode
29 * Manages a set of pads that operate in collect mode. This means that control
30 * is given to the manager of this object when all pads have data.
32 * * Collectpads are created with gst_collect_pads_new(). A callback should then
33 * be installed with gst_collect_pads_set_function ().
35 * * Pads are added to the collection with gst_collect_pads_add_pad()/
36 * gst_collect_pads_remove_pad(). The pad has to be a sinkpad. When added,
37 * the chain, event and query functions of the pad are overridden. The
38 * element_private of the pad is used to store private information for the
41 * * For each pad, data is queued in the _chain function or by
42 * performing a pull_range.
44 * * When data is queued on all pads in waiting mode, the callback function is called.
46 * * Data can be dequeued from the pad with the gst_collect_pads_pop() method.
47 * One can peek at the data with the gst_collect_pads_peek() function.
48 * These functions will return %NULL if the pad received an EOS event. When all
49 * pads return %NULL from a gst_collect_pads_peek(), the element can emit an EOS
52 * * Data can also be dequeued in byte units using the gst_collect_pads_available(),
53 * gst_collect_pads_read_buffer() and gst_collect_pads_flush() calls.
55 * * Elements should call gst_collect_pads_start() and gst_collect_pads_stop() in
56 * their state change functions to start and stop the processing of the collectpads.
57 * The gst_collect_pads_stop() call should be called before calling the parent
58 * element state change function in the PAUSED_TO_READY state change to ensure
59 * no pad is blocked and the element can finish streaming.
61 * * gst_collect_pads_set_waiting() sets a pad to waiting or non-waiting mode.
62 * CollectPads element is not waiting for data to be collected on non-waiting pads.
63 * Thus these pads may but need not have data when the callback is called.
64 * All pads are in waiting mode by default.
72 #include <gst/gst_private.h>
74 #include "gstcollectpads.h"
76 #include "../../../gst/glib-compat-private.h"
78 GST_DEBUG_CATEGORY_STATIC (collect_pads_debug);
79 #define GST_CAT_DEFAULT collect_pads_debug
81 struct _GstCollectDataPrivate
83 /* refcounting for struct, and destroy callback */
84 GstCollectDataDestroyNotify destroy_notify;
88 struct _GstCollectPadsPrivate
90 /* with LOCK and/or STREAM_LOCK */
93 /* with STREAM_LOCK */
94 guint32 cookie; /* pad_list cookie */
95 guint numpads; /* number of pads in @data */
96 guint queuedpads; /* number of pads with a buffer */
97 guint eospads; /* number of pads that are EOS */
98 GstClockTime earliest_time; /* Current earliest time */
99 GstCollectData *earliest_data; /* Pad data for current earliest time */
102 GSList *pad_list; /* list of GstCollectData* */
103 guint32 pad_cookie; /* updated cookie */
105 GstCollectPadsFunction func; /* function and user_data for callback */
107 GstCollectPadsBufferFunction buffer_func; /* function and user_data for buffer callback */
108 gpointer buffer_user_data;
109 GstCollectPadsCompareFunction compare_func;
110 gpointer compare_user_data;
111 GstCollectPadsEventFunction event_func; /* function and data for event callback */
112 gpointer event_user_data;
113 GstCollectPadsQueryFunction query_func;
114 gpointer query_user_data;
115 GstCollectPadsClipFunction clip_func;
116 gpointer clip_user_data;
117 GstCollectPadsFlushFunction flush_func;
118 gpointer flush_user_data;
120 /* no other lock needed */
121 GMutex evt_lock; /* these make up sort of poor man's event signaling */
126 gboolean pending_flush_start;
127 gboolean pending_flush_stop;
130 #define parent_class gst_collect_pads_parent_class
131 G_DEFINE_TYPE_WITH_PRIVATE (GstCollectPads, gst_collect_pads, GST_TYPE_OBJECT);
133 static void gst_collect_pads_clear (GstCollectPads * pads,
134 GstCollectData * data);
135 static GstFlowReturn gst_collect_pads_chain (GstPad * pad, GstObject * parent,
137 static gboolean gst_collect_pads_event (GstPad * pad, GstObject * parent,
139 static gboolean gst_collect_pads_query (GstPad * pad, GstObject * parent,
141 static void gst_collect_pads_finalize (GObject * object);
142 static GstFlowReturn gst_collect_pads_default_collected (GstCollectPads *
143 pads, gpointer user_data);
144 static gint gst_collect_pads_default_compare_func (GstCollectPads * pads,
145 GstCollectData * data1, GstClockTime timestamp1, GstCollectData * data2,
146 GstClockTime timestamp2, gpointer user_data);
147 static gboolean gst_collect_pads_recalculate_full (GstCollectPads * pads);
148 static void ref_data (GstCollectData * data);
149 static void unref_data (GstCollectData * data);
151 static gboolean gst_collect_pads_event_default_internal (GstCollectPads *
152 pads, GstCollectData * data, GstEvent * event, gpointer user_data);
153 static gboolean gst_collect_pads_query_default_internal (GstCollectPads *
154 pads, GstCollectData * data, GstQuery * query, gpointer user_data);
157 /* Some properties are protected by LOCK, others by STREAM_LOCK
158 * However, manipulating either of these partitions may require
159 * to signal/wake a _WAIT, so use a separate (sort of) event to prevent races
160 * Alternative implementations are possible, e.g. some low-level re-implementing
161 * of the 2 above locks to drop both of them atomically when going into _WAIT.
163 #define GST_COLLECT_PADS_GET_EVT_COND(pads) (&((GstCollectPads *)pads)->priv->evt_cond)
164 #define GST_COLLECT_PADS_GET_EVT_LOCK(pads) (&((GstCollectPads *)pads)->priv->evt_lock)
165 #define GST_COLLECT_PADS_EVT_WAIT(pads, cookie) G_STMT_START { \
166 g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
167 /* should work unless a lot of event'ing and thread starvation */\
168 while (cookie == ((GstCollectPads *) pads)->priv->evt_cookie) \
169 g_cond_wait (GST_COLLECT_PADS_GET_EVT_COND (pads), \
170 GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
171 cookie = ((GstCollectPads *) pads)->priv->evt_cookie; \
172 g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
174 #define GST_COLLECT_PADS_EVT_WAIT_TIMED(pads, cookie, timeout) G_STMT_START { \
177 g_get_current_time (&tv); \
178 g_time_val_add (&tv, timeout); \
180 g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
181 /* should work unless a lot of event'ing and thread starvation */\
182 while (cookie == ((GstCollectPads *) pads)->priv->evt_cookie) \
183 g_cond_timed_wait (GST_COLLECT_PADS_GET_EVT_COND (pads), \
184 GST_COLLECT_PADS_GET_EVT_LOCK (pads), &tv); \
185 cookie = ((GstCollectPads *) pads)->priv->evt_cookie; \
186 g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
188 #define GST_COLLECT_PADS_EVT_BROADCAST(pads) G_STMT_START { \
189 g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
190 /* never mind wrap-around */ \
191 ++(((GstCollectPads *) pads)->priv->evt_cookie); \
192 g_cond_broadcast (GST_COLLECT_PADS_GET_EVT_COND (pads)); \
193 g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
195 #define GST_COLLECT_PADS_EVT_INIT(cookie) G_STMT_START { \
196 g_mutex_lock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
197 cookie = ((GstCollectPads *) pads)->priv->evt_cookie; \
198 g_mutex_unlock (GST_COLLECT_PADS_GET_EVT_LOCK (pads)); \
202 gst_collect_pads_class_init (GstCollectPadsClass * klass)
204 GObjectClass *gobject_class = (GObjectClass *) klass;
206 GST_DEBUG_CATEGORY_INIT (collect_pads_debug, "collectpads", 0,
209 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_collect_pads_finalize);
213 gst_collect_pads_init (GstCollectPads * pads)
215 pads->priv = gst_collect_pads_get_instance_private (pads);
218 pads->priv->cookie = 0;
219 pads->priv->numpads = 0;
220 pads->priv->queuedpads = 0;
221 pads->priv->eospads = 0;
222 pads->priv->started = FALSE;
224 g_rec_mutex_init (&pads->stream_lock);
226 pads->priv->func = gst_collect_pads_default_collected;
227 pads->priv->user_data = NULL;
228 pads->priv->event_func = NULL;
229 pads->priv->event_user_data = NULL;
231 /* members for default muxing */
232 pads->priv->buffer_func = NULL;
233 pads->priv->buffer_user_data = NULL;
234 pads->priv->compare_func = gst_collect_pads_default_compare_func;
235 pads->priv->compare_user_data = NULL;
236 pads->priv->earliest_data = NULL;
237 pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
239 pads->priv->event_func = gst_collect_pads_event_default_internal;
240 pads->priv->query_func = gst_collect_pads_query_default_internal;
242 /* members to manage the pad list */
243 pads->priv->pad_cookie = 0;
244 pads->priv->pad_list = NULL;
246 /* members for event */
247 g_mutex_init (&pads->priv->evt_lock);
248 g_cond_init (&pads->priv->evt_cond);
249 pads->priv->evt_cookie = 0;
251 pads->priv->seeking = FALSE;
252 pads->priv->pending_flush_start = FALSE;
253 pads->priv->pending_flush_stop = FALSE;
257 gst_collect_pads_finalize (GObject * object)
259 GstCollectPads *pads = GST_COLLECT_PADS (object);
261 GST_DEBUG_OBJECT (object, "finalize");
263 g_rec_mutex_clear (&pads->stream_lock);
265 g_cond_clear (&pads->priv->evt_cond);
266 g_mutex_clear (&pads->priv->evt_lock);
268 /* Remove pads and free pads list */
269 g_slist_foreach (pads->priv->pad_list, (GFunc) unref_data, NULL);
270 g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
271 g_slist_free (pads->data);
272 g_slist_free (pads->priv->pad_list);
274 G_OBJECT_CLASS (parent_class)->finalize (object);
278 * gst_collect_pads_new:
280 * Create a new instance of #GstCollectPads.
284 * Returns: (transfer full): a new #GstCollectPads, or %NULL in case of an error.
287 gst_collect_pads_new (void)
289 GstCollectPads *newcoll;
291 newcoll = g_object_new (GST_TYPE_COLLECT_PADS, NULL);
293 /* clear floating flag */
294 gst_object_ref_sink (newcoll);
299 /* Must be called with GstObject lock! */
301 gst_collect_pads_set_buffer_function_locked (GstCollectPads * pads,
302 GstCollectPadsBufferFunction func, gpointer user_data)
304 pads->priv->buffer_func = func;
305 pads->priv->buffer_user_data = user_data;
309 * gst_collect_pads_set_buffer_function:
310 * @pads: the collectpads to use
311 * @func: (scope call): the function to set
312 * @user_data: (closure): user data passed to the function
314 * Set the callback function and user data that will be called with
315 * the oldest buffer when all pads have been collected, or %NULL on EOS.
316 * If a buffer is passed, the callback owns a reference and must unref
322 gst_collect_pads_set_buffer_function (GstCollectPads * pads,
323 GstCollectPadsBufferFunction func, gpointer user_data)
325 g_return_if_fail (pads != NULL);
326 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
328 GST_OBJECT_LOCK (pads);
329 gst_collect_pads_set_buffer_function_locked (pads, func, user_data);
330 GST_OBJECT_UNLOCK (pads);
334 * gst_collect_pads_set_compare_function:
335 * @pads: the pads to use
336 * @func: (scope call): the function to set
337 * @user_data: (closure): user data passed to the function
339 * Set the timestamp comparison function.
343 /* NOTE allowing to change comparison seems not advisable;
344 no known use-case, and collaboration with default algorithm is unpredictable.
345 If custom comparing/operation is needed, just use a collect function of
348 gst_collect_pads_set_compare_function (GstCollectPads * pads,
349 GstCollectPadsCompareFunction func, gpointer user_data)
351 g_return_if_fail (pads != NULL);
352 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
354 GST_OBJECT_LOCK (pads);
355 pads->priv->compare_func = func;
356 pads->priv->compare_user_data = user_data;
357 GST_OBJECT_UNLOCK (pads);
361 * gst_collect_pads_set_function:
362 * @pads: the collectpads to use
363 * @func: (scope call): the function to set
364 * @user_data: user data passed to the function
366 * CollectPads provides a default collection algorithm that will determine
367 * the oldest buffer available on all of its pads, and then delegate
368 * to a configured callback.
369 * However, if circumstances are more complicated and/or more control
370 * is desired, this sets a callback that will be invoked instead when
371 * all the pads added to the collection have buffers queued.
372 * Evidently, this callback is not compatible with
373 * gst_collect_pads_set_buffer_function() callback.
374 * If this callback is set, the former will be unset.
379 gst_collect_pads_set_function (GstCollectPads * pads,
380 GstCollectPadsFunction func, gpointer user_data)
382 g_return_if_fail (pads != NULL);
383 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
385 GST_OBJECT_LOCK (pads);
386 pads->priv->func = func;
387 pads->priv->user_data = user_data;
388 gst_collect_pads_set_buffer_function_locked (pads, NULL, NULL);
389 GST_OBJECT_UNLOCK (pads);
393 ref_data (GstCollectData * data)
395 g_assert (data != NULL);
397 g_atomic_int_inc (&(data->priv->refcount));
401 unref_data (GstCollectData * data)
403 g_assert (data != NULL);
404 g_assert (data->priv->refcount > 0);
406 if (!g_atomic_int_dec_and_test (&(data->priv->refcount)))
409 if (data->priv->destroy_notify)
410 data->priv->destroy_notify (data);
412 g_object_unref (data->pad);
414 gst_buffer_unref (data->buffer);
421 * gst_collect_pads_set_event_function:
422 * @pads: the collectpads to use
423 * @func: (scope call): the function to set
424 * @user_data: user data passed to the function
426 * Set the event callback function and user data that will be called when
427 * collectpads has received an event originating from one of the collected
428 * pads. If the event being processed is a serialized one, this callback is
429 * called with @pads STREAM_LOCK held, otherwise not. As this lock should be
430 * held when calling a number of CollectPads functions, it should be acquired
431 * if so (unusually) needed.
436 gst_collect_pads_set_event_function (GstCollectPads * pads,
437 GstCollectPadsEventFunction func, gpointer user_data)
439 g_return_if_fail (pads != NULL);
440 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
442 GST_OBJECT_LOCK (pads);
443 pads->priv->event_func = func;
444 pads->priv->event_user_data = user_data;
445 GST_OBJECT_UNLOCK (pads);
449 * gst_collect_pads_set_query_function:
450 * @pads: the collectpads to use
451 * @func: (scope call): the function to set
452 * @user_data: user data passed to the function
454 * Set the query callback function and user data that will be called after
455 * collectpads has received a query originating from one of the collected
456 * pads. If the query being processed is a serialized one, this callback is
457 * called with @pads STREAM_LOCK held, otherwise not. As this lock should be
458 * held when calling a number of CollectPads functions, it should be acquired
459 * if so (unusually) needed.
464 gst_collect_pads_set_query_function (GstCollectPads * pads,
465 GstCollectPadsQueryFunction func, gpointer user_data)
467 g_return_if_fail (pads != NULL);
468 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
470 GST_OBJECT_LOCK (pads);
471 pads->priv->query_func = func;
472 pads->priv->query_user_data = user_data;
473 GST_OBJECT_UNLOCK (pads);
477 * gst_collect_pads_clip_running_time:
478 * @pads: the collectpads to use
479 * @cdata: collect data of corresponding pad
480 * @buf: buffer being clipped
481 * @outbuf: (allow-none) (out): output buffer with running time, or NULL if clipped
482 * @user_data: user data (unused)
484 * Convenience clipping function that converts incoming buffer's timestamp
485 * to running time, or clips the buffer if outside configured segment.
487 * Since 1.6, this clipping function also sets the DTS parameter of the
488 * GstCollectData structure. This version of the running time DTS can be
489 * negative. G_MININT64 is used to indicate invalid value.
492 gst_collect_pads_clip_running_time (GstCollectPads * pads,
493 GstCollectData * cdata, GstBuffer * buf, GstBuffer ** outbuf,
498 /* invalid left alone and passed */
499 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS_OR_PTS (buf)))) {
501 GstClockTime buf_dts, abs_dts;
504 time = GST_BUFFER_PTS (buf);
506 if (GST_CLOCK_TIME_IS_VALID (time)) {
508 gst_segment_to_running_time (&cdata->segment, GST_FORMAT_TIME, time);
509 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
510 GST_DEBUG_OBJECT (cdata->pad, "clipping buffer on pad outside segment %"
511 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
512 gst_buffer_unref (buf);
518 GST_LOG_OBJECT (cdata->pad, "buffer pts %" 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;
524 dts_sign = gst_segment_to_running_time_full (&cdata->segment,
525 GST_FORMAT_TIME, GST_BUFFER_DTS (*outbuf), &abs_dts);
526 buf_dts = GST_BUFFER_DTS (*outbuf);
528 GST_BUFFER_DTS (*outbuf) = abs_dts;
529 GST_COLLECT_PADS_DTS (cdata) = abs_dts;
530 } else if (dts_sign < 0) {
531 GST_BUFFER_DTS (*outbuf) = GST_CLOCK_TIME_NONE;
532 GST_COLLECT_PADS_DTS (cdata) = -((gint64) abs_dts);
534 GST_BUFFER_DTS (*outbuf) = GST_CLOCK_TIME_NONE;
535 GST_COLLECT_PADS_DTS (cdata) = GST_CLOCK_STIME_NONE;
538 GST_LOG_OBJECT (cdata->pad, "buffer dts %" GST_TIME_FORMAT " -> %"
539 GST_STIME_FORMAT " running time", GST_TIME_ARGS (buf_dts),
540 GST_STIME_ARGS (GST_COLLECT_PADS_DTS (cdata)));
547 * gst_collect_pads_set_clip_function:
548 * @pads: the collectpads to use
549 * @clipfunc: (scope call): clip function to install
550 * @user_data: user data to pass to @clip_func
552 * Install a clipping function that is called right after a buffer is received
553 * on a pad managed by @pads. See #GstCollectPadsClipFunction for more info.
556 gst_collect_pads_set_clip_function (GstCollectPads * pads,
557 GstCollectPadsClipFunction clipfunc, gpointer user_data)
559 g_return_if_fail (pads != NULL);
560 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
562 pads->priv->clip_func = clipfunc;
563 pads->priv->clip_user_data = user_data;
567 * gst_collect_pads_set_flush_function:
568 * @pads: the collectpads to use
569 * @func: (scope call): flush function to install
570 * @user_data: user data to pass to @func
572 * Install a flush function that is called when the internal
573 * state of all pads should be flushed as part of flushing seek
574 * handling. See #GstCollectPadsFlushFunction for more info.
579 gst_collect_pads_set_flush_function (GstCollectPads * pads,
580 GstCollectPadsFlushFunction func, gpointer user_data)
582 g_return_if_fail (pads != NULL);
583 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
585 pads->priv->flush_func = func;
586 pads->priv->flush_user_data = user_data;
590 * gst_collect_pads_add_pad:
591 * @pads: the collectpads to use
592 * @pad: (transfer none): the pad to add
593 * @size: the size of the returned #GstCollectData structure
594 * @destroy_notify: (scope async): function to be called before the returned
595 * #GstCollectData structure is freed
596 * @lock: whether to lock this pad in usual waiting state
598 * Add a pad to the collection of collect pads. The pad has to be
599 * a sinkpad. The refcount of the pad is incremented. Use
600 * gst_collect_pads_remove_pad() to remove the pad from the collection
603 * You specify a size for the returned #GstCollectData structure
604 * so that you can use it to store additional information.
606 * You can also specify a #GstCollectDataDestroyNotify that will be called
607 * just before the #GstCollectData structure is freed. It is passed the
608 * pointer to the structure and should free any custom memory and resources
611 * Keeping a pad locked in waiting state is only relevant when using
612 * the default collection algorithm (providing the oldest buffer).
613 * It ensures a buffer must be available on this pad for a collection
614 * to take place. This is of typical use to a muxer element where
615 * non-subtitle streams should always be in waiting state,
616 * e.g. to assure that caps information is available on all these streams
617 * when initial headers have to be written.
619 * The pad will be automatically activated in push mode when @pads is
624 * Returns: (nullable) (transfer none): a new #GstCollectData to identify the
625 * new pad. Or %NULL if wrong parameters are supplied.
628 gst_collect_pads_add_pad (GstCollectPads * pads, GstPad * pad, guint size,
629 GstCollectDataDestroyNotify destroy_notify, gboolean lock)
631 GstCollectData *data;
633 g_return_val_if_fail (pads != NULL, NULL);
634 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
635 g_return_val_if_fail (pad != NULL, NULL);
636 g_return_val_if_fail (GST_PAD_IS_SINK (pad), NULL);
637 g_return_val_if_fail (size >= sizeof (GstCollectData), NULL);
639 GST_DEBUG_OBJECT (pads, "adding pad %s:%s", GST_DEBUG_PAD_NAME (pad));
641 data = g_malloc0 (size);
642 data->priv = g_new0 (GstCollectDataPrivate, 1);
643 data->collect = pads;
644 data->pad = gst_object_ref (pad);
647 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
648 data->state = GST_COLLECT_PADS_STATE_WAITING;
649 data->state |= lock ? GST_COLLECT_PADS_STATE_LOCKED : 0;
650 data->priv->refcount = 1;
651 data->priv->destroy_notify = destroy_notify;
652 data->ABI.abi.dts = G_MININT64;
654 GST_OBJECT_LOCK (pads);
655 GST_OBJECT_LOCK (pad);
656 gst_pad_set_element_private (pad, data);
657 GST_OBJECT_UNLOCK (pad);
658 pads->priv->pad_list = g_slist_append (pads->priv->pad_list, data);
659 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_chain));
660 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_event));
661 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_query));
662 /* backward compat, also add to data if stopped, so that the element already
663 * has this in the public data list before going PAUSED (typically)
664 * this can only be done when we are stopped because we don't take the
665 * STREAM_LOCK to protect the pads->data list. */
666 if (!pads->priv->started) {
667 pads->data = g_slist_append (pads->data, data);
670 /* activate the pad when needed */
671 if (pads->priv->started)
672 gst_pad_set_active (pad, TRUE);
673 pads->priv->pad_cookie++;
674 GST_OBJECT_UNLOCK (pads);
680 find_pad (GstCollectData * data, GstPad * pad)
682 if (data->pad == pad)
688 * gst_collect_pads_remove_pad:
689 * @pads: the collectpads to use
690 * @pad: (transfer none): the pad to remove
692 * Remove a pad from the collection of collect pads. This function will also
693 * free the #GstCollectData and all the resources that were allocated with
694 * gst_collect_pads_add_pad().
696 * The pad will be deactivated automatically when @pads is stopped.
700 * Returns: %TRUE if the pad could be removed.
703 gst_collect_pads_remove_pad (GstCollectPads * pads, GstPad * pad)
705 GstCollectData *data;
708 g_return_val_if_fail (pads != NULL, FALSE);
709 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), FALSE);
710 g_return_val_if_fail (pad != NULL, FALSE);
711 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
713 GST_DEBUG_OBJECT (pads, "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
715 GST_OBJECT_LOCK (pads);
717 g_slist_find_custom (pads->priv->pad_list, pad, (GCompareFunc) find_pad);
721 data = (GstCollectData *) list->data;
723 GST_DEBUG_OBJECT (pads, "found pad %s:%s at %p", GST_DEBUG_PAD_NAME (pad),
726 /* clear the stuff we configured */
727 gst_pad_set_chain_function (pad, NULL);
728 gst_pad_set_event_function (pad, NULL);
729 GST_OBJECT_LOCK (pad);
730 gst_pad_set_element_private (pad, NULL);
731 GST_OBJECT_UNLOCK (pad);
733 /* backward compat, also remove from data if stopped, note that this function
734 * can only be called when we are stopped because we don't take the
735 * STREAM_LOCK to protect the pads->data list. */
736 if (!pads->priv->started) {
739 dlist = g_slist_find_custom (pads->data, pad, (GCompareFunc) find_pad);
741 GstCollectData *pdata = dlist->data;
743 pads->data = g_slist_delete_link (pads->data, dlist);
747 /* remove from the pad list */
748 pads->priv->pad_list = g_slist_delete_link (pads->priv->pad_list, list);
749 pads->priv->pad_cookie++;
751 /* signal waiters because something changed */
752 GST_COLLECT_PADS_EVT_BROADCAST (pads);
754 /* deactivate the pad when needed */
755 if (!pads->priv->started)
756 gst_pad_set_active (pad, FALSE);
758 /* clean and free the collect data */
761 GST_OBJECT_UNLOCK (pads);
767 GST_WARNING_OBJECT (pads, "cannot remove unknown pad %s:%s",
768 GST_DEBUG_PAD_NAME (pad));
769 GST_OBJECT_UNLOCK (pads);
775 * Must be called with STREAM_LOCK and OBJECT_LOCK.
778 gst_collect_pads_set_flushing_unlocked (GstCollectPads * pads,
783 GST_DEBUG ("sink-pads flushing=%d", flushing);
785 /* Update the pads flushing flag */
786 for (walk = pads->priv->pad_list; walk; walk = g_slist_next (walk)) {
787 GstCollectData *cdata = walk->data;
789 if (GST_IS_PAD (cdata->pad)) {
790 GST_OBJECT_LOCK (cdata->pad);
792 GST_PAD_SET_FLUSHING (cdata->pad);
794 GST_PAD_UNSET_FLUSHING (cdata->pad);
796 GST_COLLECT_PADS_STATE_SET (cdata, GST_COLLECT_PADS_STATE_FLUSHING);
798 GST_COLLECT_PADS_STATE_UNSET (cdata, GST_COLLECT_PADS_STATE_FLUSHING);
799 gst_collect_pads_clear (pads, cdata);
800 GST_OBJECT_UNLOCK (cdata->pad);
804 /* inform _chain of changes */
805 GST_COLLECT_PADS_EVT_BROADCAST (pads);
809 * gst_collect_pads_set_flushing:
810 * @pads: the collectpads to use
811 * @flushing: desired state of the pads
813 * Change the flushing state of all the pads in the collection. No pad
814 * is able to accept anymore data when @flushing is %TRUE. Calling this
815 * function with @flushing %FALSE makes @pads accept data again.
816 * Caller must ensure that downstream streaming (thread) is not blocked,
817 * e.g. by sending a FLUSH_START downstream.
822 gst_collect_pads_set_flushing (GstCollectPads * pads, gboolean flushing)
824 g_return_if_fail (pads != NULL);
825 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
827 /* NOTE since this eventually calls _pop, some (STREAM_)LOCK is needed here */
828 GST_COLLECT_PADS_STREAM_LOCK (pads);
829 GST_OBJECT_LOCK (pads);
830 gst_collect_pads_set_flushing_unlocked (pads, flushing);
831 GST_OBJECT_UNLOCK (pads);
832 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
836 * gst_collect_pads_start:
837 * @pads: the collectpads to use
839 * Starts the processing of data in the collect_pads.
844 gst_collect_pads_start (GstCollectPads * pads)
848 g_return_if_fail (pads != NULL);
849 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
851 GST_DEBUG_OBJECT (pads, "starting collect pads");
853 /* make sure stop and collect cannot be called anymore */
854 GST_COLLECT_PADS_STREAM_LOCK (pads);
856 /* make pads streamable */
857 GST_OBJECT_LOCK (pads);
859 /* loop over the master pad list and reset the segment */
860 collected = pads->priv->pad_list;
861 for (; collected; collected = g_slist_next (collected)) {
862 GstCollectData *data;
864 data = collected->data;
865 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
868 gst_collect_pads_set_flushing_unlocked (pads, FALSE);
870 /* Start collect pads */
871 pads->priv->started = TRUE;
872 GST_OBJECT_UNLOCK (pads);
873 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
877 * gst_collect_pads_stop:
878 * @pads: the collectpads to use
880 * Stops the processing of data in the collect_pads. this function
881 * will also unblock any blocking operations.
886 gst_collect_pads_stop (GstCollectPads * pads)
890 g_return_if_fail (pads != NULL);
891 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
893 GST_DEBUG_OBJECT (pads, "stopping collect pads");
895 /* make sure collect and start cannot be called anymore */
896 GST_COLLECT_PADS_STREAM_LOCK (pads);
898 /* make pads not accept data anymore */
899 GST_OBJECT_LOCK (pads);
900 gst_collect_pads_set_flushing_unlocked (pads, TRUE);
902 /* Stop collect pads */
903 pads->priv->started = FALSE;
904 pads->priv->eospads = 0;
905 pads->priv->queuedpads = 0;
907 /* loop over the master pad list and flush buffers */
908 collected = pads->priv->pad_list;
909 for (; collected; collected = g_slist_next (collected)) {
910 GstCollectData *data;
911 GstBuffer **buffer_p;
913 data = collected->data;
915 buffer_p = &data->buffer;
916 gst_buffer_replace (buffer_p, NULL);
919 GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_EOS);
922 if (pads->priv->earliest_data)
923 unref_data (pads->priv->earliest_data);
924 pads->priv->earliest_data = NULL;
925 pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
927 GST_OBJECT_UNLOCK (pads);
928 /* Wake them up so they can end the chain functions. */
929 GST_COLLECT_PADS_EVT_BROADCAST (pads);
931 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
935 * gst_collect_pads_peek:
936 * @pads: the collectpads to peek
937 * @data: the data to use
939 * Peek at the buffer currently queued in @data. This function
940 * should be called with the @pads STREAM_LOCK held, such as in the callback
945 * Returns: (transfer full) (nullable): The buffer in @data or %NULL if no
946 * buffer is queued. should unref the buffer after usage.
949 gst_collect_pads_peek (GstCollectPads * pads, GstCollectData * data)
953 g_return_val_if_fail (pads != NULL, NULL);
954 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
955 g_return_val_if_fail (data != NULL, NULL);
957 if ((result = data->buffer))
958 gst_buffer_ref (result);
960 GST_DEBUG_OBJECT (pads, "Peeking at pad %s:%s: buffer=%" GST_PTR_FORMAT,
961 GST_DEBUG_PAD_NAME (data->pad), result);
967 * gst_collect_pads_pop:
968 * @pads: the collectpads to pop
969 * @data: the data to use
971 * Pop the buffer currently queued in @data. This function
972 * should be called with the @pads STREAM_LOCK held, such as in the callback
977 * Returns: (transfer full) (nullable): The buffer in @data or %NULL if no
978 * buffer was queued. You should unref the buffer after usage.
981 gst_collect_pads_pop (GstCollectPads * pads, GstCollectData * data)
985 g_return_val_if_fail (pads != NULL, NULL);
986 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
987 g_return_val_if_fail (data != NULL, NULL);
989 if ((result = data->buffer)) {
992 /* one less pad with queued data now */
993 if (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING))
994 pads->priv->queuedpads--;
997 GST_COLLECT_PADS_EVT_BROADCAST (pads);
999 GST_DEBUG_OBJECT (pads, "Pop buffer on pad %s:%s: buffer=%" GST_PTR_FORMAT,
1000 GST_DEBUG_PAD_NAME (data->pad), result);
1005 /* pop and unref the currently queued buffer, should be called with STREAM_LOCK
1008 gst_collect_pads_clear (GstCollectPads * pads, GstCollectData * data)
1012 if ((buf = gst_collect_pads_pop (pads, data)))
1013 gst_buffer_unref (buf);
1017 * gst_collect_pads_available:
1018 * @pads: the collectpads to query
1020 * Query how much bytes can be read from each queued buffer. This means
1021 * that the result of this call is the maximum number of bytes that can
1022 * be read from each of the pads.
1024 * This function should be called with @pads STREAM_LOCK held, such as
1029 * Returns: The maximum number of bytes queued on all pads. This function
1030 * returns 0 if a pad has no queued buffer.
1032 /* we might pre-calculate this in some struct field,
1033 * but would then have to maintain this in _chain and particularly _pop, etc,
1034 * even if element is never interested in this information */
1036 gst_collect_pads_available (GstCollectPads * pads)
1039 guint result = G_MAXUINT;
1041 g_return_val_if_fail (pads != NULL, 0);
1042 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), 0);
1044 collected = pads->data;
1045 for (; collected; collected = g_slist_next (collected)) {
1046 GstCollectData *pdata;
1050 pdata = (GstCollectData *) collected->data;
1052 /* ignore pad with EOS */
1053 if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (pdata,
1054 GST_COLLECT_PADS_STATE_EOS))) {
1055 GST_DEBUG_OBJECT (pads, "pad %p is EOS", pdata);
1059 /* an empty buffer without EOS is weird when we get here.. */
1060 if (G_UNLIKELY ((buffer = pdata->buffer) == NULL)) {
1061 GST_WARNING_OBJECT (pads, "pad %p has no buffer", pdata);
1065 /* this is the size left of the buffer */
1066 size = gst_buffer_get_size (buffer) - pdata->pos;
1067 GST_DEBUG_OBJECT (pads, "pad %p has %d bytes left", pdata, size);
1069 /* need to return the min of all available data */
1073 /* nothing changed, all must be EOS then, return 0 */
1074 if (G_UNLIKELY (result == G_MAXUINT))
1086 * gst_collect_pads_flush:
1087 * @pads: the collectpads to query
1088 * @data: the data to use
1089 * @size: the number of bytes to flush
1091 * Flush @size bytes from the pad @data.
1093 * This function should be called with @pads STREAM_LOCK held, such as
1098 * Returns: The number of bytes flushed This can be less than @size and
1099 * is 0 if the pad was end-of-stream.
1102 gst_collect_pads_flush (GstCollectPads * pads, GstCollectData * data,
1109 g_return_val_if_fail (pads != NULL, 0);
1110 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), 0);
1111 g_return_val_if_fail (data != NULL, 0);
1113 /* no buffer, must be EOS */
1114 if ((buffer = data->buffer) == NULL)
1117 bsize = gst_buffer_get_size (buffer);
1119 /* this is what we can flush at max */
1120 flushsize = MIN (size, bsize - data->pos);
1124 if (data->pos >= bsize)
1125 /* _clear will also reset data->pos to 0 */
1126 gst_collect_pads_clear (pads, data);
1132 * gst_collect_pads_read_buffer:
1133 * @pads: the collectpads to query
1134 * @data: the data to use
1135 * @size: the number of bytes to read
1137 * Get a subbuffer of @size bytes from the given pad @data.
1139 * This function should be called with @pads STREAM_LOCK held, such as in the
1144 * Returns: (transfer full) (nullable): A sub buffer. The size of the buffer can
1145 * be less that requested. A return of %NULL signals that the pad is
1146 * end-of-stream. Unref the buffer after use.
1149 gst_collect_pads_read_buffer (GstCollectPads * pads, GstCollectData * data,
1152 guint readsize, buf_size;
1155 g_return_val_if_fail (pads != NULL, NULL);
1156 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
1157 g_return_val_if_fail (data != NULL, NULL);
1159 /* no buffer, must be EOS */
1160 if ((buffer = data->buffer) == NULL)
1163 buf_size = gst_buffer_get_size (buffer);
1164 readsize = MIN (size, buf_size - data->pos);
1166 return gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, data->pos,
1171 * gst_collect_pads_take_buffer:
1172 * @pads: the collectpads to query
1173 * @data: the data to use
1174 * @size: the number of bytes to read
1176 * Get a subbuffer of @size bytes from the given pad @data. Flushes the amount
1179 * This function should be called with @pads STREAM_LOCK held, such as in the
1184 * Returns: (transfer full) (nullable): A sub buffer. The size of the buffer can
1185 * be less that requested. A return of %NULL signals that the pad is
1186 * end-of-stream. Unref the buffer after use.
1189 gst_collect_pads_take_buffer (GstCollectPads * pads, GstCollectData * data,
1192 GstBuffer *buffer = gst_collect_pads_read_buffer (pads, data, size);
1195 gst_collect_pads_flush (pads, data, gst_buffer_get_size (buffer));
1201 * gst_collect_pads_set_waiting:
1202 * @pads: the collectpads
1203 * @data: the data to use
1204 * @waiting: boolean indicating whether this pad should operate
1205 * in waiting or non-waiting mode
1207 * Sets a pad to waiting or non-waiting mode, if at least this pad
1208 * has not been created with locked waiting state,
1209 * in which case nothing happens.
1211 * This function should be called with @pads STREAM_LOCK held, such as
1217 gst_collect_pads_set_waiting (GstCollectPads * pads, GstCollectData * data,
1220 g_return_if_fail (pads != NULL);
1221 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
1222 g_return_if_fail (data != NULL);
1224 GST_DEBUG_OBJECT (pads, "Setting pad %s to waiting %d, locked %d",
1225 GST_PAD_NAME (data->pad), waiting,
1226 GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_LOCKED));
1228 /* Do something only on a change and if not locked */
1229 if (!GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_LOCKED) &&
1230 (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING) !=
1232 /* Set waiting state for this pad */
1234 GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_WAITING);
1236 GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_WAITING);
1237 /* Update number of queued pads if needed */
1238 if (!data->buffer &&
1239 !GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_EOS)) {
1241 pads->priv->queuedpads--;
1243 pads->priv->queuedpads++;
1246 /* signal waiters because something changed */
1247 GST_COLLECT_PADS_EVT_BROADCAST (pads);
1251 /* see if pads were added or removed and update our stats. Any pad
1252 * added after releasing the LOCK will get collected in the next
1255 * We can do a quick check by checking the cookies, that get changed
1256 * whenever the pad list is updated.
1258 * Must be called with STREAM_LOCK.
1261 gst_collect_pads_check_pads (GstCollectPads * pads)
1263 /* the master list and cookie are protected with LOCK */
1264 GST_OBJECT_LOCK (pads);
1265 if (G_UNLIKELY (pads->priv->pad_cookie != pads->priv->cookie)) {
1268 /* clear list and stats */
1269 g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
1270 g_slist_free (pads->data);
1272 pads->priv->numpads = 0;
1273 pads->priv->queuedpads = 0;
1274 pads->priv->eospads = 0;
1275 if (pads->priv->earliest_data)
1276 unref_data (pads->priv->earliest_data);
1277 pads->priv->earliest_data = NULL;
1278 pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
1280 /* loop over the master pad list */
1281 collected = pads->priv->pad_list;
1282 for (; collected; collected = g_slist_next (collected)) {
1283 GstCollectData *data;
1285 /* update the stats */
1286 pads->priv->numpads++;
1287 data = collected->data;
1288 if (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_EOS))
1289 pads->priv->eospads++;
1290 else if (data->buffer || !GST_COLLECT_PADS_STATE_IS_SET (data,
1291 GST_COLLECT_PADS_STATE_WAITING))
1292 pads->priv->queuedpads++;
1294 /* add to the list of pads to collect */
1296 /* preserve order of adding/requesting pads */
1297 pads->data = g_slist_append (pads->data, data);
1299 /* and update the cookie */
1300 pads->priv->cookie = pads->priv->pad_cookie;
1302 GST_OBJECT_UNLOCK (pads);
1305 /* checks if all the pads are collected and call the collectfunction
1307 * Should be called with STREAM_LOCK.
1309 * Returns: The #GstFlowReturn of collection.
1311 static GstFlowReturn
1312 gst_collect_pads_check_collected (GstCollectPads * pads)
1314 GstFlowReturn flow_ret = GST_FLOW_OK;
1315 GstCollectPadsFunction func;
1318 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), GST_FLOW_ERROR);
1320 GST_OBJECT_LOCK (pads);
1321 func = pads->priv->func;
1322 user_data = pads->priv->user_data;
1323 GST_OBJECT_UNLOCK (pads);
1325 g_return_val_if_fail (pads->priv->func != NULL, GST_FLOW_NOT_SUPPORTED);
1327 /* check for new pads, update stats etc.. */
1328 gst_collect_pads_check_pads (pads);
1330 if (G_UNLIKELY (pads->priv->eospads == pads->priv->numpads)) {
1331 /* If all our pads are EOS just collect once to let the element
1332 * do its final EOS handling. */
1333 GST_DEBUG_OBJECT (pads, "All active pads (%d) are EOS, calling %s",
1334 pads->priv->numpads, GST_DEBUG_FUNCPTR_NAME (func));
1336 if (G_UNLIKELY (g_atomic_int_compare_and_exchange (&pads->priv->seeking,
1338 GST_INFO_OBJECT (pads, "finished seeking");
1341 flow_ret = func (pads, user_data);
1342 } while (flow_ret == GST_FLOW_OK);
1344 gboolean collected = FALSE;
1346 /* We call the collected function as long as our condition matches. */
1347 while (((pads->priv->queuedpads + pads->priv->eospads) >=
1348 pads->priv->numpads)) {
1349 GST_DEBUG_OBJECT (pads,
1350 "All active pads (%d + %d >= %d) have data, " "calling %s",
1351 pads->priv->queuedpads, pads->priv->eospads, pads->priv->numpads,
1352 GST_DEBUG_FUNCPTR_NAME (func));
1354 if (G_UNLIKELY (g_atomic_int_compare_and_exchange (&pads->priv->seeking,
1356 GST_INFO_OBJECT (pads, "finished seeking");
1358 flow_ret = func (pads, user_data);
1361 /* break on error */
1362 if (flow_ret != GST_FLOW_OK)
1364 /* Don't keep looping after telling the element EOS or flushing */
1365 if (pads->priv->queuedpads == 0)
1369 GST_DEBUG_OBJECT (pads, "Not all active pads (%d) have data, continuing",
1370 pads->priv->numpads);
1376 /* General overview:
1377 * - only pad with a buffer can determine earliest_data (and earliest_time)
1378 * - only segment info determines (non-)waiting state
1379 * - ? perhaps use _stream_time for comparison
1380 * (which muxers might have use as well ?)
1384 * Function to recalculate the waiting state of all pads.
1386 * Must be called with STREAM_LOCK.
1388 * Returns %TRUE if a pad was set to waiting
1389 * (from non-waiting state).
1392 gst_collect_pads_recalculate_waiting (GstCollectPads * pads)
1395 gboolean result = FALSE;
1397 /* If earliest time is not known, there is nothing to do. */
1398 if (pads->priv->earliest_data == NULL)
1401 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
1402 GstCollectData *data = (GstCollectData *) collected->data;
1404 GstClockTime comp_time;
1406 /* check if pad has a segment */
1407 if (data->segment.format == GST_FORMAT_UNDEFINED) {
1408 GST_WARNING_OBJECT (pads,
1409 "GstCollectPads has no time segment, assuming 0 based.");
1410 gst_segment_init (&data->segment, GST_FORMAT_TIME);
1411 GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_NEW_SEGMENT);
1414 /* check segment format */
1415 if (data->segment.format != GST_FORMAT_TIME) {
1416 GST_ERROR_OBJECT (pads, "GstCollectPads can handle only time segments.");
1420 /* check if the waiting state should be changed */
1421 comp_time = data->segment.position;
1422 cmp_res = pads->priv->compare_func (pads, data, comp_time,
1423 pads->priv->earliest_data, pads->priv->earliest_time,
1424 pads->priv->compare_user_data);
1427 gst_collect_pads_set_waiting (pads, data, FALSE);
1429 if (!GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING)) {
1431 gst_collect_pads_set_waiting (pads, data, TRUE);
1441 * gst_collect_pads_find_best_pad:
1442 * @pads: the collectpads to use
1443 * @data: returns the collectdata for earliest data
1444 * @time: returns the earliest available buffertime
1446 * Find the oldest/best pad, i.e. pad holding the oldest buffer and
1447 * and return the corresponding #GstCollectData and buffertime.
1449 * This function should be called with STREAM_LOCK held,
1450 * such as in the callback.
1453 gst_collect_pads_find_best_pad (GstCollectPads * pads,
1454 GstCollectData ** data, GstClockTime * time)
1457 GstCollectData *best = NULL;
1458 GstClockTime best_time = GST_CLOCK_TIME_NONE;
1460 g_return_if_fail (data != NULL);
1461 g_return_if_fail (time != NULL);
1463 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
1465 GstCollectData *data = (GstCollectData *) collected->data;
1466 GstClockTime timestamp;
1468 buffer = gst_collect_pads_peek (pads, data);
1469 /* if we have a buffer check if it is better then the current best one */
1470 if (buffer != NULL) {
1471 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
1472 gst_buffer_unref (buffer);
1473 if (best == NULL || pads->priv->compare_func (pads, data, timestamp,
1474 best, best_time, pads->priv->compare_user_data) < 0) {
1476 best_time = timestamp;
1481 /* set earliest time */
1485 GST_DEBUG_OBJECT (pads, "best pad %s, best time %" GST_TIME_FORMAT,
1486 best ? GST_PAD_NAME (((GstCollectData *) best)->pad) : "(nil)",
1487 GST_TIME_ARGS (best_time));
1491 * Function to recalculate earliest_data and earliest_timestamp. This also calls
1492 * gst_collect_pads_recalculate_waiting
1494 * Must be called with STREAM_LOCK.
1497 gst_collect_pads_recalculate_full (GstCollectPads * pads)
1499 if (pads->priv->earliest_data)
1500 unref_data (pads->priv->earliest_data);
1501 gst_collect_pads_find_best_pad (pads, &pads->priv->earliest_data,
1502 &pads->priv->earliest_time);
1503 if (pads->priv->earliest_data)
1504 ref_data (pads->priv->earliest_data);
1505 return gst_collect_pads_recalculate_waiting (pads);
1509 * Default collect callback triggered when #GstCollectPads gathered all data.
1511 * Called with STREAM_LOCK.
1513 static GstFlowReturn
1514 gst_collect_pads_default_collected (GstCollectPads * pads, gpointer user_data)
1516 GstCollectData *best = NULL;
1518 GstFlowReturn ret = GST_FLOW_OK;
1519 GstCollectPadsBufferFunction func;
1520 gpointer buffer_user_data;
1522 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), GST_FLOW_ERROR);
1524 GST_OBJECT_LOCK (pads);
1525 func = pads->priv->buffer_func;
1526 buffer_user_data = pads->priv->buffer_user_data;
1527 GST_OBJECT_UNLOCK (pads);
1529 g_return_val_if_fail (func != NULL, GST_FLOW_NOT_SUPPORTED);
1531 /* Find the oldest pad at all cost */
1532 if (gst_collect_pads_recalculate_full (pads)) {
1533 /* waiting was switched on,
1534 * so give another thread a chance to deliver a possibly
1535 * older buffer; don't charge on yet with the current oldest */
1540 best = pads->priv->earliest_data;
1542 /* No data collected means EOS. */
1543 if (G_UNLIKELY (best == NULL)) {
1544 ret = func (pads, best, NULL, buffer_user_data);
1545 if (ret == GST_FLOW_OK)
1550 /* make sure that the pad we take a buffer from is waiting;
1551 * otherwise popping a buffer will seem not to have happened
1552 * and collectpads can get into a busy loop */
1553 gst_collect_pads_set_waiting (pads, best, TRUE);
1556 buffer = gst_collect_pads_pop (pads, best);
1557 ret = func (pads, best, buffer, buffer_user_data);
1559 /* maybe non-waiting was forced to waiting above due to
1560 * newsegment events coming too sparsely,
1561 * so re-check to restore state to avoid hanging/waiting */
1562 gst_collect_pads_recalculate_full (pads);
1569 * Default timestamp compare function.
1572 gst_collect_pads_default_compare_func (GstCollectPads * pads,
1573 GstCollectData * data1, GstClockTime timestamp1,
1574 GstCollectData * data2, GstClockTime timestamp2, gpointer user_data)
1577 GST_LOG_OBJECT (pads, "comparing %" GST_TIME_FORMAT
1578 " and %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp1),
1579 GST_TIME_ARGS (timestamp2));
1580 /* non-valid timestamps go first as they are probably headers or so */
1581 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp1)))
1582 return GST_CLOCK_TIME_IS_VALID (timestamp2) ? -1 : 0;
1584 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp2)))
1587 /* compare timestamp */
1588 if (timestamp1 < timestamp2)
1591 if (timestamp1 > timestamp2)
1597 /* called with STREAM_LOCK */
1599 gst_collect_pads_handle_position_update (GstCollectPads * pads,
1600 GstCollectData * data, GstClockTime new_pos)
1604 /* If oldest time is not known, or current pad got newsegment;
1605 * recalculate the state */
1606 if (!pads->priv->earliest_data || pads->priv->earliest_data == data) {
1607 gst_collect_pads_recalculate_full (pads);
1611 /* Check if the waiting state of the pad should change. */
1613 pads->priv->compare_func (pads, data, new_pos,
1614 pads->priv->earliest_data, pads->priv->earliest_time,
1615 pads->priv->compare_user_data);
1619 gst_collect_pads_set_waiting (pads, data, FALSE);
1627 gst_collect_pads_clip_time (GstCollectPads * pads, GstCollectData * data,
1630 GstClockTime otime = time;
1631 GstBuffer *in, *out = NULL;
1633 if (pads->priv->clip_func) {
1634 in = gst_buffer_new ();
1635 GST_BUFFER_PTS (in) = time;
1636 GST_BUFFER_DTS (in) = GST_CLOCK_TIME_NONE;
1637 pads->priv->clip_func (pads, data, in, &out, pads->priv->clip_user_data);
1639 otime = GST_BUFFER_PTS (out);
1640 gst_buffer_unref (out);
1642 /* FIXME should distinguish between ahead or after segment,
1643 * let's assume after segment and use some large time ... */
1644 otime = G_MAXINT64 / 2;
1652 * gst_collect_pads_event_default:
1653 * @pads: the collectpads to use
1654 * @data: collect data of corresponding pad
1655 * @event: event being processed
1656 * @discard: process but do not send event downstream
1658 * Default #GstCollectPads event handling that elements should always
1659 * chain up to to ensure proper operation. Element might however indicate
1660 * event should not be forwarded downstream.
1663 gst_collect_pads_event_default (GstCollectPads * pads, GstCollectData * data,
1664 GstEvent * event, gboolean discard)
1666 gboolean res = TRUE;
1667 GstCollectPadsBufferFunction buffer_func;
1671 GST_OBJECT_LOCK (pads);
1672 buffer_func = pads->priv->buffer_func;
1673 GST_OBJECT_UNLOCK (pads);
1676 parent = GST_OBJECT_PARENT (pad);
1678 GST_DEBUG_OBJECT (pad, "Got '%s' event", GST_EVENT_TYPE_NAME (event));
1680 switch (GST_EVENT_TYPE (event)) {
1681 case GST_EVENT_FLUSH_START:
1683 if (g_atomic_int_get (&pads->priv->seeking)) {
1684 /* drop all but the first FLUSH_STARTs when seeking */
1685 if (!g_atomic_int_compare_and_exchange (&pads->
1686 priv->pending_flush_start, TRUE, FALSE))
1689 /* unblock collect pads */
1690 gst_pad_event_default (pad, parent, event);
1693 GST_COLLECT_PADS_STREAM_LOCK (pads);
1694 /* Start flushing. We never call gst_collect_pads_set_flushing (FALSE), we
1695 * instead wait until each pad gets its FLUSH_STOP and let that reset the pad to
1696 * non-flushing (which happens in gst_collect_pads_event_default).
1698 gst_collect_pads_set_flushing (pads, TRUE);
1700 if (pads->priv->flush_func)
1701 pads->priv->flush_func (pads, pads->priv->flush_user_data);
1703 g_atomic_int_set (&pads->priv->pending_flush_stop, TRUE);
1704 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1708 /* forward event to unblock check_collected */
1709 GST_DEBUG_OBJECT (pad, "forwarding flush start");
1710 if (!(res = gst_pad_event_default (pad, parent, event))) {
1711 GST_WARNING_OBJECT (pad, "forwarding flush start failed");
1715 /* now unblock the chain function.
1716 * no cond per pad, so they all unblock,
1717 * non-flushing block again */
1718 GST_COLLECT_PADS_STREAM_LOCK (pads);
1719 GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_FLUSHING);
1720 gst_collect_pads_clear (pads, data);
1722 /* cater for possible default muxing functionality */
1724 /* restore to initial state */
1725 gst_collect_pads_set_waiting (pads, data, TRUE);
1726 /* if the current pad is affected, reset state, recalculate later */
1727 if (pads->priv->earliest_data == data) {
1729 pads->priv->earliest_data = NULL;
1730 pads->priv->earliest_time = GST_CLOCK_TIME_NONE;
1734 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1739 case GST_EVENT_FLUSH_STOP:
1741 /* flush the 1 buffer queue */
1742 GST_COLLECT_PADS_STREAM_LOCK (pads);
1743 GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_FLUSHING);
1744 gst_collect_pads_clear (pads, data);
1745 /* we need new segment info after the flush */
1746 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
1747 GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_NEW_SEGMENT);
1748 /* if the pad was EOS, remove the EOS flag and
1749 * decrement the number of eospads */
1750 if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
1751 GST_COLLECT_PADS_STATE_EOS))) {
1752 if (!GST_COLLECT_PADS_STATE_IS_SET (data,
1753 GST_COLLECT_PADS_STATE_WAITING))
1754 pads->priv->queuedpads++;
1755 if (!g_atomic_int_get (&pads->priv->seeking)) {
1756 pads->priv->eospads--;
1758 GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_EOS);
1760 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1762 if (g_atomic_int_get (&pads->priv->seeking)) {
1763 if (g_atomic_int_compare_and_exchange (&pads->priv->pending_flush_stop,
1774 GST_COLLECT_PADS_STREAM_LOCK (pads);
1775 /* if the pad was not EOS, make it EOS and so we
1776 * have one more eospad */
1777 if (G_LIKELY (!GST_COLLECT_PADS_STATE_IS_SET (data,
1778 GST_COLLECT_PADS_STATE_EOS))) {
1779 GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_EOS);
1780 if (!GST_COLLECT_PADS_STATE_IS_SET (data,
1781 GST_COLLECT_PADS_STATE_WAITING))
1782 pads->priv->queuedpads--;
1783 pads->priv->eospads++;
1785 /* check if we need collecting anything, we ignore the result. */
1786 gst_collect_pads_check_collected (pads);
1787 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1791 case GST_EVENT_SEGMENT:
1795 GST_COLLECT_PADS_STREAM_LOCK (pads);
1797 gst_event_copy_segment (event, &seg);
1799 GST_DEBUG_OBJECT (data->pad, "got segment %" GST_SEGMENT_FORMAT, &seg);
1801 /* default collection can not handle other segment formats than time */
1802 if (buffer_func && seg.format != GST_FORMAT_TIME) {
1803 GST_WARNING_OBJECT (pads, "GstCollectPads default collecting "
1804 "can only handle time segments. Non time segment ignored.");
1805 goto newsegment_done;
1808 /* need to update segment first */
1809 data->segment = seg;
1810 GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_NEW_SEGMENT);
1812 /* now we can use for e.g. running time */
1814 gst_collect_pads_clip_time (pads, data, seg.start + seg.offset);
1816 data->segment = seg;
1818 /* default muxing functionality */
1820 goto newsegment_done;
1822 gst_collect_pads_handle_position_update (pads, data, seg.position);
1825 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1826 /* we must not forward this event since multiple segments will be
1827 * accumulated and this is certainly not what we want. */
1832 GstClockTime start, duration;
1834 GST_COLLECT_PADS_STREAM_LOCK (pads);
1836 gst_event_parse_gap (event, &start, &duration);
1837 /* FIXME, handle reverse playback case */
1838 if (GST_CLOCK_TIME_IS_VALID (duration))
1840 /* we do not expect another buffer until after gap,
1841 * so that is our position now */
1842 data->segment.position = gst_collect_pads_clip_time (pads, data, start);
1844 gst_collect_pads_handle_position_update (pads, data,
1845 data->segment.position);
1847 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
1850 case GST_EVENT_STREAM_START:
1851 /* drop stream start events, element must create its own start event,
1852 * we can't just forward the first random stream start event we get */
1854 case GST_EVENT_CAPS:
1857 /* forward other events */
1862 GST_DEBUG_OBJECT (pads, "dropping event: %" GST_PTR_FORMAT, event);
1864 gst_event_unref (event);
1871 GST_DEBUG_OBJECT (pads, "forward event: %" GST_PTR_FORMAT, event);
1872 return gst_pad_event_default (pad, parent, event);
1883 event_forward_func (GstPad * pad, EventData * data)
1885 gboolean ret = TRUE;
1886 GstPad *peer = gst_pad_get_peer (pad);
1889 ret = gst_pad_send_event (peer, gst_event_ref (data->event));
1890 gst_object_unref (peer);
1893 data->result &= ret;
1894 /* Always send to all pads */
1899 forward_event_to_all_sinkpads (GstPad * srcpad, GstEvent * event)
1906 gst_pad_forward (srcpad, (GstPadForwardFunction) event_forward_func, &data);
1908 gst_event_unref (event);
1914 * gst_collect_pads_src_event_default:
1915 * @pads: the #GstCollectPads to use
1916 * @pad: src #GstPad that received the event
1917 * @event: event being processed
1919 * Default #GstCollectPads event handling for the src pad of elements.
1920 * Elements can chain up to this to let flushing seek event handling
1921 * be done by #GstCollectPads.
1926 gst_collect_pads_src_event_default (GstCollectPads * pads, GstPad * pad,
1930 gboolean res = TRUE;
1932 parent = GST_OBJECT_PARENT (pad);
1934 switch (GST_EVENT_TYPE (event)) {
1935 case GST_EVENT_SEEK:{
1938 pads->priv->eospads = 0;
1940 GST_INFO_OBJECT (pads, "starting seek");
1942 gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL, NULL);
1943 if (flags & GST_SEEK_FLAG_FLUSH) {
1944 g_atomic_int_set (&pads->priv->seeking, TRUE);
1945 g_atomic_int_set (&pads->priv->pending_flush_start, TRUE);
1946 /* forward the seek upstream */
1947 res = forward_event_to_all_sinkpads (pad, event);
1950 g_atomic_int_set (&pads->priv->seeking, FALSE);
1951 g_atomic_int_set (&pads->priv->pending_flush_start, FALSE);
1955 GST_INFO_OBJECT (pads, "seek done, result: %d", res);
1964 res = gst_pad_event_default (pad, parent, event);
1970 gst_collect_pads_event_default_internal (GstCollectPads * pads,
1971 GstCollectData * data, GstEvent * event, gpointer user_data)
1973 return gst_collect_pads_event_default (pads, data, event, FALSE);
1977 gst_collect_pads_event (GstPad * pad, GstObject * parent, GstEvent * event)
1979 gboolean res = FALSE, need_unlock = FALSE;
1980 GstCollectData *data;
1981 GstCollectPads *pads;
1982 GstCollectPadsEventFunction event_func;
1983 gpointer event_user_data;
1985 /* some magic to get the managing collect_pads */
1986 GST_OBJECT_LOCK (pad);
1987 data = (GstCollectData *) gst_pad_get_element_private (pad);
1988 if (G_UNLIKELY (data == NULL))
1991 GST_OBJECT_UNLOCK (pad);
1995 pads = data->collect;
1997 GST_DEBUG_OBJECT (data->pad, "Got %s event on sink pad",
1998 GST_EVENT_TYPE_NAME (event));
2000 GST_OBJECT_LOCK (pads);
2001 event_func = pads->priv->event_func;
2002 event_user_data = pads->priv->event_user_data;
2003 GST_OBJECT_UNLOCK (pads);
2005 if (GST_EVENT_IS_SERIALIZED (event)) {
2006 GST_COLLECT_PADS_STREAM_LOCK (pads);
2010 if (G_LIKELY (event_func)) {
2011 res = event_func (pads, data, event, event_user_data);
2015 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
2023 GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2024 GST_OBJECT_UNLOCK (pad);
2030 * gst_collect_pads_query_default:
2031 * @pads: the collectpads to use
2032 * @data: collect data of corresponding pad
2033 * @query: query being processed
2034 * @discard: process but do not send event downstream
2036 * Default #GstCollectPads query handling that elements should always
2037 * chain up to to ensure proper operation. Element might however indicate
2038 * query should not be forwarded downstream.
2041 gst_collect_pads_query_default (GstCollectPads * pads, GstCollectData * data,
2042 GstQuery * query, gboolean discard)
2044 gboolean res = TRUE;
2049 parent = GST_OBJECT_PARENT (pad);
2051 switch (GST_QUERY_TYPE (query)) {
2052 case GST_QUERY_SEEKING:
2056 /* don't pass it along as some (file)sink might claim it does
2057 * whereas with a collectpads in between that will not likely work */
2058 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2059 gst_query_set_seeking (query, format, FALSE, 0, -1);
2069 return gst_pad_query_default (pad, parent, query);
2075 gst_collect_pads_query_default_internal (GstCollectPads * pads,
2076 GstCollectData * data, GstQuery * query, gpointer user_data)
2078 return gst_collect_pads_query_default (pads, data, query, FALSE);
2082 gst_collect_pads_query (GstPad * pad, GstObject * parent, GstQuery * query)
2084 gboolean res = FALSE, need_unlock = FALSE;
2085 GstCollectData *data;
2086 GstCollectPads *pads;
2087 GstCollectPadsQueryFunction query_func;
2088 gpointer query_user_data;
2090 GST_DEBUG_OBJECT (pad, "Got %s query on sink pad",
2091 GST_QUERY_TYPE_NAME (query));
2093 /* some magic to get the managing collect_pads */
2094 GST_OBJECT_LOCK (pad);
2095 data = (GstCollectData *) gst_pad_get_element_private (pad);
2096 if (G_UNLIKELY (data == NULL))
2099 GST_OBJECT_UNLOCK (pad);
2101 pads = data->collect;
2103 GST_OBJECT_LOCK (pads);
2104 query_func = pads->priv->query_func;
2105 query_user_data = pads->priv->query_user_data;
2106 GST_OBJECT_UNLOCK (pads);
2108 if (GST_QUERY_IS_SERIALIZED (query)) {
2109 GST_COLLECT_PADS_STREAM_LOCK (pads);
2113 if (G_LIKELY (query_func)) {
2114 res = query_func (pads, data, query, query_user_data);
2118 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
2126 GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2127 GST_OBJECT_UNLOCK (pad);
2133 /* For each buffer we receive we check if our collected condition is reached
2134 * and if so we call the collected function. When this is done we check if
2135 * data has been unqueued. If data is still queued we wait holding the stream
2136 * lock to make sure no EOS event can happen while we are ready to be
2139 static GstFlowReturn
2140 gst_collect_pads_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2142 GstCollectData *data;
2143 GstCollectPads *pads;
2145 GstBuffer **buffer_p;
2148 GST_DEBUG ("Got buffer for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2150 /* some magic to get the managing collect_pads */
2151 GST_OBJECT_LOCK (pad);
2152 data = (GstCollectData *) gst_pad_get_element_private (pad);
2153 if (G_UNLIKELY (data == NULL))
2156 GST_OBJECT_UNLOCK (pad);
2158 pads = data->collect;
2160 GST_COLLECT_PADS_STREAM_LOCK (pads);
2161 /* if not started, bail out */
2162 if (G_UNLIKELY (!pads->priv->started))
2164 /* check if this pad is flushing */
2165 if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
2166 GST_COLLECT_PADS_STATE_FLUSHING)))
2168 /* pad was EOS, we can refuse this data */
2169 if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
2170 GST_COLLECT_PADS_STATE_EOS)))
2173 /* see if we need to clip */
2174 if (pads->priv->clip_func) {
2175 GstBuffer *outbuf = NULL;
2177 pads->priv->clip_func (pads, data, buffer, &outbuf,
2178 pads->priv->clip_user_data);
2181 if (G_UNLIKELY (outbuf == NULL))
2184 if (G_UNLIKELY (ret == GST_FLOW_EOS))
2186 else if (G_UNLIKELY (ret != GST_FLOW_OK))
2190 GST_DEBUG_OBJECT (pads, "Queuing buffer %p for pad %s:%s", buffer,
2191 GST_DEBUG_PAD_NAME (pad));
2193 /* One more pad has data queued */
2194 if (GST_COLLECT_PADS_STATE_IS_SET (data, GST_COLLECT_PADS_STATE_WAITING))
2195 pads->priv->queuedpads++;
2196 buffer_p = &data->buffer;
2197 gst_buffer_replace (buffer_p, buffer);
2199 /* update segment last position if in TIME */
2200 if (G_LIKELY (data->segment.format == GST_FORMAT_TIME)) {
2201 GstClockTime timestamp;
2203 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
2205 if (GST_CLOCK_TIME_IS_VALID (timestamp))
2206 data->segment.position = timestamp;
2209 /* While we have data queued on this pad try to collect stuff */
2211 /* Check if our collected condition is matched and call the collected
2212 * function if it is */
2213 ret = gst_collect_pads_check_collected (pads);
2214 /* when an error occurs, we want to report this back to the caller ASAP
2215 * without having to block if the buffer was not popped */
2216 if (G_UNLIKELY (ret != GST_FLOW_OK))
2219 /* data was consumed, we can exit and accept new data */
2220 if (data->buffer == NULL)
2223 /* Having the _INIT here means we don't care about any broadcast up to here
2224 * (most of which occur with STREAM_LOCK held, so could not have happened
2225 * anyway). We do care about e.g. a remove initiated broadcast as of this
2226 * point. Putting it here also makes this thread ignores any evt it raised
2227 * itself (as is a usual WAIT semantic).
2229 GST_COLLECT_PADS_EVT_INIT (cookie);
2231 /* pad could be removed and re-added */
2233 GST_OBJECT_LOCK (pad);
2234 if (G_UNLIKELY ((data = gst_pad_get_element_private (pad)) == NULL))
2237 GST_OBJECT_UNLOCK (pad);
2239 GST_DEBUG_OBJECT (pads, "Pad %s:%s has a buffer queued, waiting",
2240 GST_DEBUG_PAD_NAME (pad));
2242 /* wait to be collected, this must happen from another thread triggered
2243 * by the _chain function of another pad. We release the lock so we
2244 * can get stopped or flushed as well. We can however not get EOS
2245 * because we still hold the STREAM_LOCK.
2247 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
2248 GST_COLLECT_PADS_EVT_WAIT (pads, cookie);
2249 GST_COLLECT_PADS_STREAM_LOCK (pads);
2251 GST_DEBUG_OBJECT (pads, "Pad %s:%s resuming", GST_DEBUG_PAD_NAME (pad));
2253 /* after a signal, we could be stopped */
2254 if (G_UNLIKELY (!pads->priv->started))
2256 /* check if this pad is flushing */
2257 if (G_UNLIKELY (GST_COLLECT_PADS_STATE_IS_SET (data,
2258 GST_COLLECT_PADS_STATE_FLUSHING)))
2261 while (data->buffer != NULL);
2264 GST_COLLECT_PADS_STREAM_UNLOCK (pads);
2265 /* data is definitely NULL if pad_removed goto was run. */
2269 gst_buffer_unref (buffer);
2274 GST_WARNING ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2275 GST_OBJECT_UNLOCK (pad);
2276 ret = GST_FLOW_NOT_LINKED;
2282 GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
2283 GST_OBJECT_UNLOCK (pad);
2284 gst_buffer_unref (buffer);
2285 return GST_FLOW_NOT_LINKED;
2289 GST_DEBUG ("not started");
2290 gst_collect_pads_clear (pads, data);
2291 ret = GST_FLOW_FLUSHING;
2296 GST_DEBUG ("pad %s:%s is flushing", GST_DEBUG_PAD_NAME (pad));
2297 gst_collect_pads_clear (pads, data);
2298 ret = GST_FLOW_FLUSHING;
2303 /* we should not post an error for this, just inform upstream that
2304 * we don't expect anything anymore */
2305 GST_DEBUG ("pad %s:%s is eos", GST_DEBUG_PAD_NAME (pad));
2311 GST_DEBUG ("clipped buffer on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
2317 /* we print the error, the element should post a reasonable error
2318 * message for fatal errors */
2319 GST_DEBUG ("collect failed, reason %d (%s)", ret, gst_flow_get_name (ret));
2320 gst_collect_pads_clear (pads, data);