0505da60283ae679dde388594a657d120b64ceeb
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 * gstcollectpads2.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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23 /**
24 * SECTION:gstcollectpads2
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_pads2_new(). A callback should then
33 * be installed with gst_collect_pads2_set_function ().
34 * </para></listitem>
35 * <listitem><para>
36 * Pads are added to the collection with gst_collect_pads2_add_pad()/
37 * gst_collect_pads2_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_pads2_pop() method.
51 * One can peek at the data with the gst_collect_pads2_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_pads2_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_pads2_available(),
58 * gst_collect_pads2_read() and gst_collect_pads2_flush() calls.
59 * </para></listitem>
60 * <listitem><para>
61 * Elements should call gst_collect_pads2_start() and gst_collect_pads2_stop() in
62 * their state change functions to start and stop the processing of the collectpads.
63 * The gst_collect_pads2_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_pads2_collect() and gst_collect_pads2_collect_range() can be used by
69 * elements that start a #GstTask to drive the collect_pads2. This feature is however
70 * not yet implemented.
71 * </para></listitem>
72 * <listitem><para>
73 * gst_collect_pads2_set_waiting() sets a pad to waiting or non-waiting mode.
74 * CollectPads element is not waiting for data to be collected on non-waiting pads.
75 * Thus these pads may but need not have data when the callback is called.
76 * All pads are in waiting mode by default.
77 * </para></listitem>
78 * </itemizedlist>
79 *
80 * Last reviewed on 2011-10-28 (0.10.36)
81 *
82 * Since: 0.10.36
83 */
85 #ifdef HAVE_CONFIG_H
86 # include "config.h"
87 #endif
89 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
90 * with newer GLib versions (>= 2.31.0) */
91 #define GLIB_DISABLE_DEPRECATION_WARNINGS
92 #include <gst/gst_private.h>
94 #include "gstcollectpads2.h"
96 #include "../../../gst/glib-compat-private.h"
98 GST_DEBUG_CATEGORY_STATIC (collect_pads2_debug);
99 #define GST_CAT_DEFAULT collect_pads2_debug
101 #define parent_class gst_collect_pads2_parent_class
102 G_DEFINE_TYPE (GstCollectPads2, gst_collect_pads2, GST_TYPE_OBJECT);
104 static void gst_collect_pads2_clear (GstCollectPads2 * pads,
105 GstCollectData2 * data);
106 static GstFlowReturn gst_collect_pads2_chain (GstPad * pad, GstBuffer * buffer);
107 static gboolean gst_collect_pads2_event (GstPad * pad, GstEvent * event);
108 static void gst_collect_pads2_finalize (GObject * object);
109 static GstFlowReturn gst_collect_pads2_default_collected (GstCollectPads2 *
110 pads, gpointer user_data);
111 static gint gst_collect_pads2_default_compare_func (GstCollectPads2 * pads,
112 GstCollectData2 * data1, GstClockTime timestamp1, GstCollectData2 * data2,
113 GstClockTime timestamp2, gpointer user_data);
114 static gboolean gst_collect_pads2_recalculate_full (GstCollectPads2 * pads);
115 static void ref_data (GstCollectData2 * data);
116 static void unref_data (GstCollectData2 * data);
118 /* Some properties are protected by LOCK, others by STREAM_LOCK
119 * However, manipulating either of these partitions may require
120 * to signal/wake a _WAIT, so use a separate (sort of) event to prevent races
121 * Alternative implementations are possible, e.g. some low-level re-implementing
122 * of the 2 above locks to drop both of them atomically when going into _WAIT.
123 */
124 #define GST_COLLECT_PADS2_GET_EVT_COND(pads) (((GstCollectPads2 *)pads)->evt_cond)
125 #define GST_COLLECT_PADS2_GET_EVT_LOCK(pads) (((GstCollectPads2 *)pads)->evt_lock)
126 #define GST_COLLECT_PADS2_EVT_WAIT(pads, cookie) G_STMT_START { \
127 g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
128 /* should work unless a lot of event'ing and thread starvation */\
129 while (cookie == ((GstCollectPads2 *) pads)->evt_cookie) \
130 g_cond_wait (GST_COLLECT_PADS2_GET_EVT_COND (pads), \
131 GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
132 cookie = ((GstCollectPads2 *) pads)->evt_cookie; \
133 g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
134 } G_STMT_END
135 #define GST_COLLECT_PADS2_EVT_WAIT_TIMED(pads, cookie, timeout) G_STMT_START { \
136 GTimeVal __tv; \
137 \
138 g_get_current_time (&tv); \
139 g_time_val_add (&tv, timeout); \
140 \
141 g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
142 /* should work unless a lot of event'ing and thread starvation */\
143 while (cookie == ((GstCollectPads2 *) pads)->evt_cookie) \
144 g_cond_timed_wait (GST_COLLECT_PADS2_GET_EVT_COND (pads), \
145 GST_COLLECT_PADS2_GET_EVT_LOCK (pads), &tv); \
146 cookie = ((GstCollectPads2 *) pads)->evt_cookie; \
147 g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
148 } G_STMT_END
149 #define GST_COLLECT_PADS2_EVT_BROADCAST(pads) G_STMT_START { \
150 g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
151 /* never mind wrap-around */ \
152 ++(((GstCollectPads2 *) pads)->evt_cookie); \
153 g_cond_broadcast (GST_COLLECT_PADS2_GET_EVT_COND (pads)); \
154 g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
155 } G_STMT_END
156 #define GST_COLLECT_PADS2_EVT_INIT(cookie) G_STMT_START { \
157 g_mutex_lock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
158 cookie = ((GstCollectPads2 *) pads)->evt_cookie; \
159 g_mutex_unlock (GST_COLLECT_PADS2_GET_EVT_LOCK (pads)); \
160 } G_STMT_END
162 static void
163 gst_collect_pads2_class_init (GstCollectPads2Class * klass)
164 {
165 GObjectClass *gobject_class = (GObjectClass *) klass;
167 GST_DEBUG_CATEGORY_INIT (collect_pads2_debug, "collectpads2", 0,
168 "GstCollectPads2");
170 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_collect_pads2_finalize);
171 }
173 static void
174 gst_collect_pads2_init (GstCollectPads2 * pads)
175 {
176 pads->data = NULL;
177 pads->cookie = 0;
178 pads->numpads = 0;
179 pads->queuedpads = 0;
180 pads->eospads = 0;
181 pads->started = FALSE;
183 g_static_rec_mutex_init (&pads->stream_lock);
185 pads->func = gst_collect_pads2_default_collected;
186 pads->user_data = NULL;
187 pads->event_func = NULL;
188 pads->event_user_data = NULL;
190 /* members for default muxing */
191 pads->buffer_func = NULL;
192 pads->buffer_user_data = NULL;
193 pads->compare_func = gst_collect_pads2_default_compare_func;
194 pads->compare_user_data = NULL;
195 pads->earliest_data = NULL;
196 pads->earliest_time = GST_CLOCK_TIME_NONE;
198 /* members to manage the pad list */
199 pads->pad_cookie = 0;
200 pads->pad_list = NULL;
202 /* members for event */
203 pads->evt_lock = g_mutex_new ();
204 pads->evt_cond = g_cond_new ();
205 pads->evt_cookie = 0;
206 }
208 static void
209 gst_collect_pads2_finalize (GObject * object)
210 {
211 GstCollectPads2 *pads = GST_COLLECT_PADS2 (object);
213 GST_DEBUG_OBJECT (object, "finalize");
215 g_static_rec_mutex_free (&pads->stream_lock);
217 g_cond_free (pads->evt_cond);
218 g_mutex_free (pads->evt_lock);
220 /* Remove pads and free pads list */
221 g_slist_foreach (pads->pad_list, (GFunc) unref_data, NULL);
222 g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
223 g_slist_free (pads->data);
224 g_slist_free (pads->pad_list);
226 G_OBJECT_CLASS (parent_class)->finalize (object);
227 }
229 /**
230 * gst_collect_pads2_new:
231 *
232 * Create a new instance of #GstCollectsPads.
233 *
234 * Returns: a new #GstCollectPads2, or NULL in case of an error.
235 *
236 * MT safe.
237 *
238 * Since: 0.10.36
239 */
240 GstCollectPads2 *
241 gst_collect_pads2_new (void)
242 {
243 GstCollectPads2 *newcoll;
245 newcoll = g_object_new (GST_TYPE_COLLECT_PADS2, NULL);
247 return newcoll;
248 }
250 /* Must be called with GstObject lock! */
251 static void
252 gst_collect_pads2_set_buffer_function_locked (GstCollectPads2 * pads,
253 GstCollectPads2BufferFunction func, gpointer user_data)
254 {
255 pads->buffer_func = func;
256 pads->buffer_user_data = user_data;
257 }
259 /**
260 * gst_collect_pads2_set_buffer_function:
261 * @pads: the collectpads to use
262 * @func: the function to set
263 * @user_data: user data passed to the function
264 *
265 * Set the callback function and user data that will be called with
266 * the oldest buffer when all pads have been collected.
267 *
268 * MT safe.
269 *
270 * Since: 0.10.36
271 */
272 void
273 gst_collect_pads2_set_buffer_function (GstCollectPads2 * pads,
274 GstCollectPads2BufferFunction func, gpointer user_data)
275 {
276 g_return_if_fail (pads != NULL);
277 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
279 GST_OBJECT_LOCK (pads);
280 gst_collect_pads2_set_buffer_function_locked (pads, func, user_data);
281 GST_OBJECT_UNLOCK (pads);
282 }
284 /**
285 * gst_collect_pads2_set_compare_function:
286 * @pads: the pads to use
287 * @func: the function to set
288 * @user_data: user data passed to the function
289 *
290 * Set the timestamp comparison function.
291 *
292 * MT safe.
293 *
294 * Since: 0.10.36
295 */
296 /* NOTE allowing to change comparison seems not advisable;
297 no known use-case, and collaboration with default algorithm is unpredictable.
298 If custom compairing/operation is needed, just use a collect function of
299 your own */
300 void
301 gst_collect_pads2_set_compare_function (GstCollectPads2 * pads,
302 GstCollectPads2CompareFunction func, gpointer user_data)
303 {
304 g_return_if_fail (pads != NULL);
305 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
307 GST_OBJECT_LOCK (pads);
308 pads->compare_func = func;
309 pads->compare_user_data = user_data;
310 GST_OBJECT_UNLOCK (pads);
311 }
313 /**
314 * gst_collect_pads2_set_function:
315 * @pads: the collectspads to use
316 * @func: the function to set
317 * @user_data: user data passed to the function
318 *
319 * CollectPads provides a default collection algorithm that will determine
320 * the oldest buffer available on all of its pads, and then delegate
321 * to a configured callback.
322 * However, if circumstances are more complicated and/or more control
323 * is desired, this sets a callback that will be invoked instead when
324 * all the pads added to the collection have buffers queued.
325 * Evidently, this callback is not compatible with
326 * gst_collect_pads2_set_buffer_function() callback.
327 * If this callback is set, the former will be unset.
328 *
329 * MT safe.
330 *
331 * Since: 0.10.36
332 */
333 void
334 gst_collect_pads2_set_function (GstCollectPads2 * pads,
335 GstCollectPads2Function func, gpointer user_data)
336 {
337 g_return_if_fail (pads != NULL);
338 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
340 GST_OBJECT_LOCK (pads);
341 pads->func = func;
342 pads->user_data = user_data;
343 gst_collect_pads2_set_buffer_function_locked (pads, NULL, NULL);
344 GST_OBJECT_UNLOCK (pads);
345 }
347 static void
348 ref_data (GstCollectData2 * data)
349 {
350 g_assert (data != NULL);
352 g_atomic_int_inc (&(data->refcount));
353 }
355 static void
356 unref_data (GstCollectData2 * data)
357 {
358 g_assert (data != NULL);
359 g_assert (data->refcount > 0);
361 if (!g_atomic_int_dec_and_test (&(data->refcount)))
362 return;
364 if (data->destroy_notify)
365 data->destroy_notify (data);
367 g_object_unref (data->pad);
368 if (data->buffer) {
369 gst_buffer_unref (data->buffer);
370 }
371 g_free (data);
372 }
374 /**
375 * gst_collect_pads2_set_event_function:
376 * @pads: the collectspads to use
377 * @func: the function to set
378 * @user_data: user data passed to the function
379 *
380 * Set the event callback function and user data that will be called after
381 * collectpads has processed and event originating from one of the collected
382 * pads. If the event being processed is a serialized one, this callback is
383 * called with @pads STREAM_LOCK held, otherwise not. As this lock should be
384 * held when calling a number of CollectPads functions, it should be acquired
385 * if so (unusually) needed.
386 *
387 * MT safe.
388 *
389 * Since: 0.10.36
390 */
391 void
392 gst_collect_pads2_set_event_function (GstCollectPads2 * pads,
393 GstCollectPads2EventFunction func, gpointer user_data)
394 {
395 g_return_if_fail (pads != NULL);
396 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
398 GST_OBJECT_LOCK (pads);
399 pads->event_func = func;
400 pads->event_user_data = user_data;
401 GST_OBJECT_UNLOCK (pads);
402 }
404 /**
405 * gst_collect_pads2_set_clip_function:
406 * @pads: the collectspads to use
407 * @clipfunc: clip function to install
408 * @user_data: user data to pass to @clip_func
409 *
410 * Install a clipping function that is called right after a buffer is received
411 * on a pad managed by @pads. See #GstCollectPad2ClipFunction for more info.
412 *
413 * Since: 0.10.36
414 */
415 void
416 gst_collect_pads2_set_clip_function (GstCollectPads2 * pads,
417 GstCollectPads2ClipFunction clipfunc, gpointer user_data)
418 {
419 g_return_if_fail (pads != NULL);
420 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
422 pads->clip_func = clipfunc;
423 pads->clip_user_data = user_data;
424 }
426 /**
427 * gst_collect_pads2_add_pad:
428 * @pads: the collectspads to use
429 * @pad: the pad to add
430 * @size: the size of the returned #GstCollectData2 structure
431 *
432 * Add a pad to the collection of collect pads. The pad has to be
433 * a sinkpad. The refcount of the pad is incremented. Use
434 * gst_collect_pads2_remove_pad() to remove the pad from the collection
435 * again.
436 *
437 * You specify a size for the returned #GstCollectData2 structure
438 * so that you can use it to store additional information.
439 *
440 * The pad will be automatically activated in push mode when @pads is
441 * started.
442 *
443 * This function calls gst_collect_pads2_add_pad() passing a value of NULL
444 * for destroy_notify and TRUE for locked.
445 *
446 * Returns: a new #GstCollectData2 to identify the new pad. Or NULL
447 * if wrong parameters are supplied.
448 *
449 * MT safe.
450 *
451 * Since: 0.10.36
452 */
453 GstCollectData2 *
454 gst_collect_pads2_add_pad (GstCollectPads2 * pads, GstPad * pad, guint size)
455 {
456 return gst_collect_pads2_add_pad_full (pads, pad, size, NULL, TRUE);
457 }
459 /**
460 * gst_collect_pads2_add_pad_full:
461 * @pads: the collectspads to use
462 * @pad: the pad to add
463 * @size: the size of the returned #GstCollectData2 structure
464 * @destroy_notify: function to be called before the returned #GstCollectData2
465 * structure is freed
466 * @lock: whether to lock this pad in usual waiting state
467 *
468 * Add a pad to the collection of collect pads. The pad has to be
469 * a sinkpad. The refcount of the pad is incremented. Use
470 * gst_collect_pads2_remove_pad() to remove the pad from the collection
471 * again.
472 *
473 * You specify a size for the returned #GstCollectData2 structure
474 * so that you can use it to store additional information.
475 *
476 * You can also specify a #GstCollectData2DestroyNotify that will be called
477 * just before the #GstCollectData2 structure is freed. It is passed the
478 * pointer to the structure and should free any custom memory and resources
479 * allocated for it.
480 *
481 * Keeping a pad locked in waiting state is only relevant when using
482 * the default collection algorithm (providing the oldest buffer).
483 * It ensures a buffer must be available on this pad for a collection
484 * to take place. This is of typical use to a muxer element where
485 * non-subtitle streams should always be in waiting state,
486 * e.g. to assure that caps information is available on all these streams
487 * when initial headers have to be written.
488 *
489 * The pad will be automatically activated in push mode when @pads is
490 * started.
491 *
492 * Since: 0.10.36
493 *
494 * Returns: a new #GstCollectData2 to identify the new pad. Or NULL
495 * if wrong parameters are supplied.
496 *
497 * MT safe.
498 */
499 GstCollectData2 *
500 gst_collect_pads2_add_pad_full (GstCollectPads2 * pads, GstPad * pad,
501 guint size, GstCollectData2DestroyNotify destroy_notify, gboolean lock)
502 {
503 GstCollectData2 *data;
505 g_return_val_if_fail (pads != NULL, NULL);
506 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
507 g_return_val_if_fail (pad != NULL, NULL);
508 g_return_val_if_fail (GST_PAD_IS_SINK (pad), NULL);
509 g_return_val_if_fail (size >= sizeof (GstCollectData2), NULL);
511 GST_DEBUG_OBJECT (pads, "adding pad %s:%s", GST_DEBUG_PAD_NAME (pad));
513 data = g_malloc0 (size);
514 data->collect = pads;
515 data->pad = gst_object_ref (pad);
516 data->buffer = NULL;
517 data->pos = 0;
518 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
519 data->state = GST_COLLECT_PADS2_STATE_WAITING;
520 data->state |= lock ? GST_COLLECT_PADS2_STATE_LOCKED : 0;
521 data->refcount = 1;
522 data->destroy_notify = destroy_notify;
524 GST_OBJECT_LOCK (pads);
525 GST_OBJECT_LOCK (pad);
526 gst_pad_set_element_private (pad, data);
527 GST_OBJECT_UNLOCK (pad);
528 pads->pad_list = g_slist_append (pads->pad_list, data);
529 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads2_chain));
530 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads2_event));
531 /* backward compat, also add to data if stopped, so that the element already
532 * has this in the public data list before going PAUSED (typically)
533 * this can only be done when we are stopped because we don't take the
534 * STREAM_LOCK to protect the pads->data list. */
535 if (!pads->started) {
536 pads->data = g_slist_append (pads->data, data);
537 ref_data (data);
538 }
539 /* activate the pad when needed */
540 if (pads->started)
541 gst_pad_set_active (pad, TRUE);
542 pads->pad_cookie++;
543 GST_OBJECT_UNLOCK (pads);
545 return data;
546 }
548 static gint
549 find_pad (GstCollectData2 * data, GstPad * pad)
550 {
551 if (data->pad == pad)
552 return 0;
553 return 1;
554 }
556 /**
557 * gst_collect_pads2_remove_pad:
558 * @pads: the collectspads to use
559 * @pad: the pad to remove
560 *
561 * Remove a pad from the collection of collect pads. This function will also
562 * free the #GstCollectData2 and all the resources that were allocated with
563 * gst_collect_pads2_add_pad().
564 *
565 * The pad will be deactivated automatically when @pads is stopped.
566 *
567 * Returns: %TRUE if the pad could be removed.
568 *
569 * MT safe.
570 *
571 * Since: 0.10.36
572 */
573 gboolean
574 gst_collect_pads2_remove_pad (GstCollectPads2 * pads, GstPad * pad)
575 {
576 GstCollectData2 *data;
577 GSList *list;
579 g_return_val_if_fail (pads != NULL, FALSE);
580 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), FALSE);
581 g_return_val_if_fail (pad != NULL, FALSE);
582 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
584 GST_DEBUG_OBJECT (pads, "removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
586 GST_OBJECT_LOCK (pads);
587 list = g_slist_find_custom (pads->pad_list, pad, (GCompareFunc) find_pad);
588 if (!list)
589 goto unknown_pad;
591 data = (GstCollectData2 *) list->data;
593 GST_DEBUG_OBJECT (pads, "found pad %s:%s at %p", GST_DEBUG_PAD_NAME (pad),
594 data);
596 /* clear the stuff we configured */
597 gst_pad_set_chain_function (pad, NULL);
598 gst_pad_set_event_function (pad, NULL);
599 GST_OBJECT_LOCK (pad);
600 gst_pad_set_element_private (pad, NULL);
601 GST_OBJECT_UNLOCK (pad);
603 /* backward compat, also remove from data if stopped, note that this function
604 * can only be called when we are stopped because we don't take the
605 * STREAM_LOCK to protect the pads->data list. */
606 if (!pads->started) {
607 GSList *dlist;
609 dlist = g_slist_find_custom (pads->data, pad, (GCompareFunc) find_pad);
610 if (dlist) {
611 GstCollectData2 *pdata = dlist->data;
613 pads->data = g_slist_delete_link (pads->data, dlist);
614 unref_data (pdata);
615 }
616 }
617 /* remove from the pad list */
618 pads->pad_list = g_slist_delete_link (pads->pad_list, list);
619 pads->pad_cookie++;
621 /* signal waiters because something changed */
622 GST_COLLECT_PADS2_EVT_BROADCAST (pads);
624 /* deactivate the pad when needed */
625 if (!pads->started)
626 gst_pad_set_active (pad, FALSE);
628 /* clean and free the collect data */
629 unref_data (data);
631 GST_OBJECT_UNLOCK (pads);
633 return TRUE;
635 unknown_pad:
636 {
637 GST_WARNING_OBJECT (pads, "cannot remove unknown pad %s:%s",
638 GST_DEBUG_PAD_NAME (pad));
639 GST_OBJECT_UNLOCK (pads);
640 return FALSE;
641 }
642 }
644 /**
645 * gst_collect_pads2_is_active:
646 * @pads: the collectspads to use
647 * @pad: the pad to check
648 *
649 * Check if a pad is active.
650 *
651 * This function is currently not implemented.
652 *
653 * Returns: %TRUE if the pad is active.
654 *
655 * MT safe.
656 *
657 * Since: 0.10.36
658 */
659 gboolean
660 gst_collect_pads2_is_active (GstCollectPads2 * pads, GstPad * pad)
661 {
662 g_return_val_if_fail (pads != NULL, FALSE);
663 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), FALSE);
664 g_return_val_if_fail (pad != NULL, FALSE);
665 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
667 g_warning ("gst_collect_pads2_is_active() is not implemented");
669 return FALSE;
670 }
672 /**
673 * gst_collect_pads2_collect:
674 * @pads: the collectspads to use
675 *
676 * Collect data on all pads. This function is usually called
677 * from a #GstTask function in an element.
678 *
679 * This function is currently not implemented.
680 *
681 * Returns: #GstFlowReturn of the operation.
682 *
683 * MT safe.
684 *
685 * Since: 0.10.36
686 */
687 GstFlowReturn
688 gst_collect_pads2_collect (GstCollectPads2 * pads)
689 {
690 g_return_val_if_fail (pads != NULL, GST_FLOW_ERROR);
691 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);
693 g_warning ("gst_collect_pads2_collect() is not implemented");
695 return GST_FLOW_NOT_SUPPORTED;
696 }
698 /**
699 * gst_collect_pads2_collect_range:
700 * @pads: the collectspads to use
701 * @offset: the offset to collect
702 * @length: the length to collect
703 *
704 * Collect data with @offset and @length on all pads. This function
705 * is typically called in the getrange function of an element.
706 *
707 * This function is currently not implemented.
708 *
709 * Returns: #GstFlowReturn of the operation.
710 *
711 * MT safe.
712 *
713 * Since: 0.10.36
714 */
715 GstFlowReturn
716 gst_collect_pads2_collect_range (GstCollectPads2 * pads, guint64 offset,
717 guint length)
718 {
719 g_return_val_if_fail (pads != NULL, GST_FLOW_ERROR);
720 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);
722 g_warning ("gst_collect_pads2_collect_range() is not implemented");
724 return GST_FLOW_NOT_SUPPORTED;
725 }
727 /*
728 * Must be called with STREAM_LOCK.
729 */
730 static void
731 gst_collect_pads2_set_flushing_unlocked (GstCollectPads2 * pads,
732 gboolean flushing)
733 {
734 GSList *walk = NULL;
736 /* Update the pads flushing flag */
737 for (walk = pads->data; walk; walk = g_slist_next (walk)) {
738 GstCollectData2 *cdata = walk->data;
740 if (GST_IS_PAD (cdata->pad)) {
741 GST_OBJECT_LOCK (cdata->pad);
742 if (flushing)
743 GST_PAD_SET_FLUSHING (cdata->pad);
744 else
745 GST_PAD_UNSET_FLUSHING (cdata->pad);
746 if (flushing)
747 GST_COLLECT_PADS2_STATE_SET (cdata, GST_COLLECT_PADS2_STATE_FLUSHING);
748 else
749 GST_COLLECT_PADS2_STATE_UNSET (cdata, GST_COLLECT_PADS2_STATE_FLUSHING);
750 gst_collect_pads2_clear (pads, cdata);
751 GST_OBJECT_UNLOCK (cdata->pad);
752 }
753 }
755 /* inform _chain of changes */
756 GST_COLLECT_PADS2_EVT_BROADCAST (pads);
757 }
759 /**
760 * gst_collect_pads2_set_flushing:
761 * @pads: the collectspads to use
762 * @flushing: desired state of the pads
763 *
764 * Change the flushing state of all the pads in the collection. No pad
765 * is able to accept anymore data when @flushing is %TRUE. Calling this
766 * function with @flushing %FALSE makes @pads accept data again.
767 * Caller must ensure that downstream streaming (thread) is not blocked,
768 * e.g. by sending a FLUSH_START downstream.
769 *
770 * MT safe.
771 *
772 *
773 * Since: 0.10.36
774 */
775 void
776 gst_collect_pads2_set_flushing (GstCollectPads2 * pads, gboolean flushing)
777 {
778 g_return_if_fail (pads != NULL);
779 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
781 /* NOTE since this eventually calls _pop, some (STREAM_)LOCK is needed here */
782 GST_COLLECT_PADS2_STREAM_LOCK (pads);
783 gst_collect_pads2_set_flushing_unlocked (pads, flushing);
784 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
785 }
787 /**
788 * gst_collect_pads2_start:
789 * @pads: the collectspads to use
790 *
791 * Starts the processing of data in the collect_pads2.
792 *
793 * MT safe.
794 *
795 * Since: 0.10.36
796 */
797 void
798 gst_collect_pads2_start (GstCollectPads2 * pads)
799 {
800 GSList *collected;
802 g_return_if_fail (pads != NULL);
803 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
805 GST_DEBUG_OBJECT (pads, "starting collect pads");
807 /* make sure stop and collect cannot be called anymore */
808 GST_COLLECT_PADS2_STREAM_LOCK (pads);
810 /* make pads streamable */
811 GST_OBJECT_LOCK (pads);
813 /* loop over the master pad list and reset the segment */
814 collected = pads->pad_list;
815 for (; collected; collected = g_slist_next (collected)) {
816 GstCollectData2 *data;
818 data = collected->data;
819 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
820 }
822 gst_collect_pads2_set_flushing_unlocked (pads, FALSE);
824 /* Start collect pads */
825 pads->started = TRUE;
826 GST_OBJECT_UNLOCK (pads);
827 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
828 }
830 /**
831 * gst_collect_pads2_stop:
832 * @pads: the collectspads to use
833 *
834 * Stops the processing of data in the collect_pads2. this function
835 * will also unblock any blocking operations.
836 *
837 * MT safe.
838 *
839 * Since: 0.10.36
840 */
841 void
842 gst_collect_pads2_stop (GstCollectPads2 * pads)
843 {
844 GSList *collected;
846 g_return_if_fail (pads != NULL);
847 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
849 GST_DEBUG_OBJECT (pads, "stopping collect pads");
851 /* make sure collect and start cannot be called anymore */
852 GST_COLLECT_PADS2_STREAM_LOCK (pads);
854 /* make pads not accept data anymore */
855 GST_OBJECT_LOCK (pads);
856 gst_collect_pads2_set_flushing_unlocked (pads, TRUE);
858 /* Stop collect pads */
859 pads->started = FALSE;
860 pads->eospads = 0;
861 pads->queuedpads = 0;
863 /* loop over the master pad list and flush buffers */
864 collected = pads->pad_list;
865 for (; collected; collected = g_slist_next (collected)) {
866 GstCollectData2 *data;
867 GstBuffer **buffer_p;
869 data = collected->data;
870 if (data->buffer) {
871 buffer_p = &data->buffer;
872 gst_buffer_replace (buffer_p, NULL);
873 data->pos = 0;
874 }
875 GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_EOS);
876 }
878 if (pads->earliest_data)
879 unref_data (pads->earliest_data);
880 pads->earliest_data = NULL;
881 pads->earliest_time = GST_CLOCK_TIME_NONE;
883 GST_OBJECT_UNLOCK (pads);
884 /* Wake them up so they can end the chain functions. */
885 GST_COLLECT_PADS2_EVT_BROADCAST (pads);
887 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
888 }
890 /**
891 * gst_collect_pads2_peek:
892 * @pads: the collectspads to peek
893 * @data: the data to use
894 *
895 * Peek at the buffer currently queued in @data. This function
896 * should be called with the @pads STREAM_LOCK held, such as in the callback
897 * handler.
898 *
899 * Returns: The buffer in @data or NULL if no buffer is queued.
900 * should unref the buffer after usage.
901 *
902 * MT safe.
903 *
904 * Since: 0.10.36
905 */
906 GstBuffer *
907 gst_collect_pads2_peek (GstCollectPads2 * pads, GstCollectData2 * data)
908 {
909 GstBuffer *result;
911 g_return_val_if_fail (pads != NULL, NULL);
912 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
913 g_return_val_if_fail (data != NULL, NULL);
915 if ((result = data->buffer))
916 gst_buffer_ref (result);
918 GST_DEBUG_OBJECT (pads, "Peeking at pad %s:%s: buffer=%p",
919 GST_DEBUG_PAD_NAME (data->pad), result);
921 return result;
922 }
924 /**
925 * gst_collect_pads2_pop:
926 * @pads: the collectspads to pop
927 * @data: the data to use
928 *
929 * Pop the buffer currently queued in @data. This function
930 * should be called with the @pads STREAM_LOCK held, such as in the callback
931 * handler.
932 *
933 * Returns: The buffer in @data or NULL if no buffer was queued.
934 * You should unref the buffer after usage.
935 *
936 * MT safe.
937 *
938 * Since: 0.10.36
939 */
940 GstBuffer *
941 gst_collect_pads2_pop (GstCollectPads2 * pads, GstCollectData2 * data)
942 {
943 GstBuffer *result;
945 g_return_val_if_fail (pads != NULL, NULL);
946 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
947 g_return_val_if_fail (data != NULL, NULL);
949 if ((result = data->buffer)) {
950 data->buffer = NULL;
951 data->pos = 0;
952 /* one less pad with queued data now */
953 if (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_WAITING))
954 pads->queuedpads--;
955 }
957 GST_COLLECT_PADS2_EVT_BROADCAST (pads);
959 GST_DEBUG_OBJECT (pads, "Pop buffer on pad %s:%s: buffer=%p",
960 GST_DEBUG_PAD_NAME (data->pad), result);
962 return result;
963 }
965 /* pop and unref the currently queued buffer, should be called with STREAM_LOCK
966 * held */
967 static void
968 gst_collect_pads2_clear (GstCollectPads2 * pads, GstCollectData2 * data)
969 {
970 GstBuffer *buf;
972 if ((buf = gst_collect_pads2_pop (pads, data)))
973 gst_buffer_unref (buf);
974 }
976 /**
977 * gst_collect_pads2_available:
978 * @pads: the collectspads to query
979 *
980 * Query how much bytes can be read from each queued buffer. This means
981 * that the result of this call is the maximum number of bytes that can
982 * be read from each of the pads.
983 *
984 * This function should be called with @pads STREAM_LOCK held, such as
985 * in the callback.
986 *
987 * Returns: The maximum number of bytes queued on all pads. This function
988 * returns 0 if a pad has no queued buffer.
989 *
990 * MT safe.
991 *
992 * Since: 0.10.36
993 */
994 /* we might pre-calculate this in some struct field,
995 * but would then have to maintain this in _chain and particularly _pop, etc,
996 * even if element is never interested in this information */
997 guint
998 gst_collect_pads2_available (GstCollectPads2 * pads)
999 {
1000 GSList *collected;
1001 guint result = G_MAXUINT;
1003 g_return_val_if_fail (pads != NULL, 0);
1004 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), 0);
1006 collected = pads->data;
1007 for (; collected; collected = g_slist_next (collected)) {
1008 GstCollectData2 *pdata;
1009 GstBuffer *buffer;
1010 gint size;
1012 pdata = (GstCollectData2 *) collected->data;
1014 /* ignore pad with EOS */
1015 if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (pdata,
1016 GST_COLLECT_PADS2_STATE_EOS))) {
1017 GST_DEBUG_OBJECT (pads, "pad %p is EOS", pdata);
1018 continue;
1019 }
1021 /* an empty buffer without EOS is weird when we get here.. */
1022 if (G_UNLIKELY ((buffer = pdata->buffer) == NULL)) {
1023 GST_WARNING_OBJECT (pads, "pad %p has no buffer", pdata);
1024 goto not_filled;
1025 }
1027 /* this is the size left of the buffer */
1028 size = GST_BUFFER_SIZE (buffer) - pdata->pos;
1029 GST_DEBUG_OBJECT (pads, "pad %p has %d bytes left", pdata, size);
1031 /* need to return the min of all available data */
1032 if (size < result)
1033 result = size;
1034 }
1035 /* nothing changed, all must be EOS then, return 0 */
1036 if (G_UNLIKELY (result == G_MAXUINT))
1037 result = 0;
1039 return result;
1041 not_filled:
1042 {
1043 return 0;
1044 }
1045 }
1047 /**
1048 * gst_collect_pads2_read:
1049 * @pads: the collectspads to query
1050 * @data: the data to use
1051 * @bytes: a pointer to a byte array
1052 * @size: the number of bytes to read
1053 *
1054 * Get a pointer in @bytes where @size bytes can be read from the
1055 * given pad data.
1056 *
1057 * This function should be called with @pads STREAM_LOCK held, such as
1058 * in the callback.
1059 *
1060 * Returns: The number of bytes available for consumption in the
1061 * memory pointed to by @bytes. This can be less than @size and
1062 * is 0 if the pad is end-of-stream.
1063 *
1064 * MT safe.
1065 *
1066 * Since: 0.10.36
1067 */
1068 guint
1069 gst_collect_pads2_read (GstCollectPads2 * pads, GstCollectData2 * data,
1070 guint8 ** bytes, guint size)
1071 {
1072 guint readsize;
1073 GstBuffer *buffer;
1075 g_return_val_if_fail (pads != NULL, 0);
1076 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), 0);
1077 g_return_val_if_fail (data != NULL, 0);
1078 g_return_val_if_fail (bytes != NULL, 0);
1080 /* no buffer, must be EOS */
1081 if ((buffer = data->buffer) == NULL)
1082 return 0;
1084 readsize = MIN (size, GST_BUFFER_SIZE (buffer) - data->pos);
1086 *bytes = GST_BUFFER_DATA (buffer) + data->pos;
1088 return readsize;
1089 }
1091 /**
1092 * gst_collect_pads2_flush:
1093 * @pads: the collectspads to query
1094 * @data: the data to use
1095 * @size: the number of bytes to flush
1096 *
1097 * Flush @size bytes from the pad @data.
1098 *
1099 * This function should be called with @pads STREAM_LOCK held, such as
1100 * in the callback.
1101 *
1102 * Returns: The number of bytes flushed This can be less than @size and
1103 * is 0 if the pad was end-of-stream.
1104 *
1105 * MT safe.
1106 *
1107 * Since: 0.10.36
1108 */
1109 guint
1110 gst_collect_pads2_flush (GstCollectPads2 * pads, GstCollectData2 * data,
1111 guint size)
1112 {
1113 guint flushsize;
1114 GstBuffer *buffer;
1116 g_return_val_if_fail (pads != NULL, 0);
1117 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), 0);
1118 g_return_val_if_fail (data != NULL, 0);
1120 /* no buffer, must be EOS */
1121 if ((buffer = data->buffer) == NULL)
1122 return 0;
1124 /* this is what we can flush at max */
1125 flushsize = MIN (size, GST_BUFFER_SIZE (buffer) - data->pos);
1127 data->pos += size;
1129 if (data->pos >= GST_BUFFER_SIZE (buffer))
1130 /* _clear will also reset data->pos to 0 */
1131 gst_collect_pads2_clear (pads, data);
1133 return flushsize;
1134 }
1136 /**
1137 * gst_collect_pads2_read_buffer:
1138 * @pads: the collectspads to query
1139 * @data: the data to use
1140 * @size: the number of bytes to read
1141 *
1142 * Get a subbuffer of @size bytes from the given pad @data.
1143 *
1144 * This function should be called with @pads STREAM_LOCK held, such as in the
1145 * callback.
1146 *
1147 * Since: 0.10.36
1148 *
1149 * Returns: A sub buffer. The size of the buffer can be less that requested.
1150 * A return of NULL signals that the pad is end-of-stream.
1151 * Unref the buffer after use.
1152 *
1153 * MT safe.
1154 */
1155 GstBuffer *
1156 gst_collect_pads2_read_buffer (GstCollectPads2 * pads, GstCollectData2 * data,
1157 guint size)
1158 {
1159 guint readsize;
1160 GstBuffer *buffer;
1162 g_return_val_if_fail (pads != NULL, NULL);
1163 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), NULL);
1164 g_return_val_if_fail (data != NULL, NULL);
1166 /* no buffer, must be EOS */
1167 if ((buffer = data->buffer) == NULL)
1168 return NULL;
1170 readsize = MIN (size, GST_BUFFER_SIZE (buffer) - data->pos);
1172 return gst_buffer_create_sub (buffer, data->pos, readsize);
1173 }
1175 /**
1176 * gst_collect_pads2_take_buffer:
1177 * @pads: the collectspads to query
1178 * @data: the data to use
1179 * @size: the number of bytes to read
1180 *
1181 * Get a subbuffer of @size bytes from the given pad @data. Flushes the amount
1182 * of read bytes.
1183 *
1184 * This function should be called with @pads STREAM_LOCK held, such as in the
1185 * callback.
1186 *
1187 * Since: 0.10.36
1188 *
1189 * Returns: A sub buffer. The size of the buffer can be less that requested.
1190 * A return of NULL signals that the pad is end-of-stream.
1191 * Unref the buffer after use.
1192 *
1193 * MT safe.
1194 */
1195 GstBuffer *
1196 gst_collect_pads2_take_buffer (GstCollectPads2 * pads, GstCollectData2 * data,
1197 guint size)
1198 {
1199 GstBuffer *buffer = gst_collect_pads2_read_buffer (pads, data, size);
1201 if (buffer) {
1202 gst_collect_pads2_flush (pads, data, GST_BUFFER_SIZE (buffer));
1203 }
1204 return buffer;
1205 }
1207 /**
1208 * gst_collect_pads2_set_waiting:
1209 * @pads: the collectspads
1210 * @data: the data to use
1211 * @waiting: boolean indicating whether this pad should operate
1212 * in waiting or non-waiting mode
1213 *
1214 * Sets a pad to waiting or non-waiting mode, if at least this pad
1215 * has not been created with locked waiting state,
1216 * in which case nothing happens.
1217 *
1218 * This function should be called with @pads STREAM_LOCK held, such as
1219 * in the callback.
1220 *
1221 * MT safe.
1222 *
1223 * Since: 0.10.36
1224 */
1225 void
1226 gst_collect_pads2_set_waiting (GstCollectPads2 * pads, GstCollectData2 * data,
1227 gboolean waiting)
1228 {
1229 g_return_if_fail (pads != NULL);
1230 g_return_if_fail (GST_IS_COLLECT_PADS2 (pads));
1231 g_return_if_fail (data != NULL);
1233 GST_DEBUG_OBJECT (pads, "Setting pad %s to waiting %d, locked %d",
1234 GST_PAD_NAME (data->pad), waiting,
1235 GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_LOCKED));
1237 /* Do something only on a change and if not locked */
1238 if (!GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_LOCKED) &&
1239 (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_WAITING) !=
1240 ! !waiting)) {
1241 /* Set waiting state for this pad */
1242 if (waiting)
1243 GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_WAITING);
1244 else
1245 GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_WAITING);
1246 /* Update number of queued pads if needed */
1247 if (!data->buffer &&
1248 !GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_EOS)) {
1249 if (waiting)
1250 pads->queuedpads--;
1251 else
1252 pads->queuedpads++;
1253 }
1255 /* signal waiters because something changed */
1256 GST_COLLECT_PADS2_EVT_BROADCAST (pads);
1257 }
1258 }
1260 /* see if pads were added or removed and update our stats. Any pad
1261 * added after releasing the LOCK will get collected in the next
1262 * round.
1263 *
1264 * We can do a quick check by checking the cookies, that get changed
1265 * whenever the pad list is updated.
1266 *
1267 * Must be called with STREAM_LOCK.
1268 */
1269 static void
1270 gst_collect_pads2_check_pads (GstCollectPads2 * pads)
1271 {
1272 /* the master list and cookie are protected with LOCK */
1273 GST_OBJECT_LOCK (pads);
1274 if (G_UNLIKELY (pads->pad_cookie != pads->cookie)) {
1275 GSList *collected;
1277 /* clear list and stats */
1278 g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
1279 g_slist_free (pads->data);
1280 pads->data = NULL;
1281 pads->numpads = 0;
1282 pads->queuedpads = 0;
1283 pads->eospads = 0;
1284 if (pads->earliest_data)
1285 unref_data (pads->earliest_data);
1286 pads->earliest_data = NULL;
1287 pads->earliest_time = GST_CLOCK_TIME_NONE;
1289 /* loop over the master pad list */
1290 collected = pads->pad_list;
1291 for (; collected; collected = g_slist_next (collected)) {
1292 GstCollectData2 *data;
1294 /* update the stats */
1295 pads->numpads++;
1296 data = collected->data;
1297 if (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_EOS))
1298 pads->eospads++;
1299 else if (data->buffer || !GST_COLLECT_PADS2_STATE_IS_SET (data,
1300 GST_COLLECT_PADS2_STATE_WAITING))
1301 pads->queuedpads++;
1303 /* add to the list of pads to collect */
1304 ref_data (data);
1305 /* preserve order of adding/requesting pads */
1306 pads->data = g_slist_append (pads->data, data);
1307 }
1308 /* and update the cookie */
1309 pads->cookie = pads->pad_cookie;
1310 }
1311 GST_OBJECT_UNLOCK (pads);
1312 }
1314 /* checks if all the pads are collected and call the collectfunction
1315 *
1316 * Should be called with STREAM_LOCK.
1317 *
1318 * Returns: The #GstFlowReturn of collection.
1319 */
1320 static GstFlowReturn
1321 gst_collect_pads2_check_collected (GstCollectPads2 * pads)
1322 {
1323 GstFlowReturn flow_ret = GST_FLOW_OK;
1324 GstCollectPads2Function func;
1325 gpointer user_data;
1327 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);
1329 GST_OBJECT_LOCK (pads);
1330 func = pads->func;
1331 user_data = pads->user_data;
1332 GST_OBJECT_UNLOCK (pads);
1334 g_return_val_if_fail (pads->func != NULL, GST_FLOW_NOT_SUPPORTED);
1336 /* check for new pads, update stats etc.. */
1337 gst_collect_pads2_check_pads (pads);
1339 if (G_UNLIKELY (pads->eospads == pads->numpads)) {
1340 /* If all our pads are EOS just collect once to let the element
1341 * do its final EOS handling. */
1342 GST_DEBUG_OBJECT (pads, "All active pads (%d) are EOS, calling %s",
1343 pads->numpads, GST_DEBUG_FUNCPTR_NAME (func));
1345 flow_ret = func (pads, user_data);
1346 } else {
1347 gboolean collected = FALSE;
1349 /* We call the collected function as long as our condition matches. */
1350 while (((pads->queuedpads + pads->eospads) >= pads->numpads)) {
1351 GST_DEBUG_OBJECT (pads, "All active pads (%d + %d >= %d) have data, "
1352 "calling %s", pads->queuedpads, pads->eospads, pads->numpads,
1353 GST_DEBUG_FUNCPTR_NAME (func));
1355 flow_ret = func (pads, user_data);
1356 collected = TRUE;
1358 /* break on error */
1359 if (flow_ret != GST_FLOW_OK)
1360 break;
1361 /* Don't keep looping after telling the element EOS or flushing */
1362 if (pads->queuedpads == 0)
1363 break;
1364 }
1365 if (!collected)
1366 GST_DEBUG_OBJECT (pads, "Not all active pads (%d) have data, continuing",
1367 pads->numpads);
1368 }
1369 return flow_ret;
1370 }
1373 /* General overview:
1374 * - only pad with a buffer can determine earliest_data (and earliest_time)
1375 * - only segment info determines (non-)waiting state
1376 * - ? perhaps use _stream_time for comparison
1377 * (which muxers might have use as well ?)
1378 */
1380 /*
1381 * Function to recalculate the waiting state of all pads.
1382 *
1383 * Must be called with STREAM_LOCK.
1384 *
1385 * Returns TRUE if a pad was set to waiting
1386 * (from non-waiting state).
1387 */
1388 static gboolean
1389 gst_collect_pads2_recalculate_waiting (GstCollectPads2 * pads)
1390 {
1391 GSList *collected;
1392 gboolean result = FALSE;
1394 /* If earliest time is not known, there is nothing to do. */
1395 if (pads->earliest_data == NULL)
1396 return FALSE;
1398 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
1399 GstCollectData2 *data = (GstCollectData2 *) collected->data;
1400 int cmp_res;
1402 /* check if pad has a segment */
1403 if (data->segment.format == GST_FORMAT_UNDEFINED)
1404 continue;
1406 /* check segment format */
1407 if (data->segment.format != GST_FORMAT_TIME) {
1408 GST_ERROR_OBJECT (pads, "GstCollectPads2 can handle only time segments.");
1409 continue;
1410 }
1412 /* check if the waiting state should be changed */
1413 cmp_res = pads->compare_func (pads, data, data->segment.start,
1414 pads->earliest_data, pads->earliest_time, pads->compare_user_data);
1415 if (cmp_res > 0)
1416 /* stop waiting */
1417 gst_collect_pads2_set_waiting (pads, data, FALSE);
1418 else {
1419 if (!GST_COLLECT_PADS2_STATE_IS_SET (data,
1420 GST_COLLECT_PADS2_STATE_WAITING)) {
1421 /* start waiting */
1422 gst_collect_pads2_set_waiting (pads, data, TRUE);
1423 result = TRUE;
1424 }
1425 }
1426 }
1428 return result;
1429 }
1431 /**
1432 * gst_collect_pads2_find_best_pad:
1433 * @pads: the collectpads to use
1434 * @data: returns the collectdata for earliest data
1435 * @time: returns the earliest available buffertime
1436 *
1437 * Find the oldest/best pad, i.e. pad holding the oldest buffer and
1438 * and return the corresponding #GstCollectData2 and buffertime.
1439 *
1440 * This function should be called with STREAM_LOCK held,
1441 * such as in the callback.
1442 *
1443 * Since: 0.10.36
1444 */
1445 static void
1446 gst_collect_pads2_find_best_pad (GstCollectPads2 * pads,
1447 GstCollectData2 ** data, GstClockTime * time)
1448 {
1449 GSList *collected;
1450 GstCollectData2 *best = NULL;
1451 GstClockTime best_time = GST_CLOCK_TIME_NONE;
1453 g_return_if_fail (data != NULL);
1454 g_return_if_fail (time != NULL);
1456 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
1457 GstBuffer *buffer;
1458 GstCollectData2 *data = (GstCollectData2 *) collected->data;
1459 GstClockTime timestamp;
1461 buffer = gst_collect_pads2_peek (pads, data);
1462 /* if we have a buffer check if it is better then the current best one */
1463 if (buffer != NULL) {
1464 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1465 gst_buffer_unref (buffer);
1466 if (best == NULL || pads->compare_func (pads, data, timestamp,
1467 best, best_time, pads->compare_user_data) < 0) {
1468 best = data;
1469 best_time = timestamp;
1470 }
1471 }
1472 }
1474 /* set earliest time */
1475 *data = best;
1476 *time = best_time;
1478 GST_DEBUG_OBJECT (pads, "best pad %s, best time %" GST_TIME_FORMAT,
1479 best ? GST_PAD_NAME (((GstCollectData2 *) best)->pad) : "(nil)",
1480 GST_TIME_ARGS (best_time));
1481 }
1483 /*
1484 * Function to recalculate earliest_data and earliest_timestamp. This also calls
1485 * gst_collect_pads2_recalculate_waiting
1486 *
1487 * Must be called with STREAM_LOCK.
1488 */
1489 static gboolean
1490 gst_collect_pads2_recalculate_full (GstCollectPads2 * pads)
1491 {
1492 if (pads->earliest_data)
1493 unref_data (pads->earliest_data);
1494 gst_collect_pads2_find_best_pad (pads, &pads->earliest_data,
1495 &pads->earliest_time);
1496 if (pads->earliest_data)
1497 ref_data (pads->earliest_data);
1498 return gst_collect_pads2_recalculate_waiting (pads);
1499 }
1501 /*
1502 * Default collect callback triggered when #GstCollectPads2 gathered all data.
1503 *
1504 * Called with STREAM_LOCK.
1505 */
1506 static GstFlowReturn
1507 gst_collect_pads2_default_collected (GstCollectPads2 * pads, gpointer user_data)
1508 {
1509 GstCollectData2 *best = NULL;
1510 GstBuffer *buffer;
1511 GstFlowReturn ret = GST_FLOW_OK;
1512 GstCollectPads2BufferFunction func;
1513 gpointer buffer_user_data;
1515 g_return_val_if_fail (GST_IS_COLLECT_PADS2 (pads), GST_FLOW_ERROR);
1517 GST_OBJECT_LOCK (pads);
1518 func = pads->buffer_func;
1519 buffer_user_data = pads->buffer_user_data;
1520 GST_OBJECT_UNLOCK (pads);
1522 g_return_val_if_fail (func != NULL, GST_FLOW_NOT_SUPPORTED);
1524 /* Find the oldest pad at all cost */
1525 if (gst_collect_pads2_recalculate_full (pads)) {
1526 /* waiting was switched on,
1527 * so give another thread a chance to deliver a possibly
1528 * older buffer; don't charge on yet with the current oldest */
1529 ret = GST_FLOW_OK;
1530 }
1532 best = pads->earliest_data;
1534 /* No data collected means EOS. */
1535 if (G_UNLIKELY (best == NULL)) {
1536 ret = func (pads, best, NULL, buffer_user_data);
1537 if (ret == GST_FLOW_OK)
1538 ret = GST_FLOW_UNEXPECTED;
1539 goto done;
1540 }
1542 /* make sure that the pad we take a buffer from is waiting;
1543 * otherwise popping a buffer will seem not to have happened
1544 * and collectpads can get into a busy loop */
1545 gst_collect_pads2_set_waiting (pads, best, TRUE);
1547 /* Send buffer */
1548 buffer = gst_collect_pads2_pop (pads, best);
1549 ret = func (pads, best, buffer, buffer_user_data);
1551 /* maybe non-waiting was forced to waiting above due to
1552 * newsegment events coming too sparsely,
1553 * so re-check to restore state to avoid hanging/waiting */
1554 gst_collect_pads2_recalculate_full (pads);
1556 done:
1557 return ret;
1558 }
1560 /*
1561 * Default timestamp compare function.
1562 */
1563 static gint
1564 gst_collect_pads2_default_compare_func (GstCollectPads2 * pads,
1565 GstCollectData2 * data1, GstClockTime timestamp1,
1566 GstCollectData2 * data2, GstClockTime timestamp2, gpointer user_data)
1567 {
1569 GST_LOG_OBJECT (pads, "comparing %" GST_TIME_FORMAT
1570 " and %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp1),
1571 GST_TIME_ARGS (timestamp2));
1572 /* non-valid timestamps go first as they are probably headers or so */
1573 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp1)))
1574 return GST_CLOCK_TIME_IS_VALID (timestamp2) ? -1 : 0;
1576 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp2)))
1577 return 1;
1579 /* compare timestamp */
1580 if (timestamp1 < timestamp2)
1581 return -1;
1583 if (timestamp1 > timestamp2)
1584 return 1;
1586 return 0;
1587 }
1589 static gboolean
1590 gst_collect_pads2_event (GstPad * pad, GstEvent * event)
1591 {
1592 gboolean res = FALSE, need_unlock = FALSE;
1593 GstCollectData2 *data;
1594 GstCollectPads2 *pads;
1595 GstCollectPads2EventFunction event_func;
1596 GstCollectPads2BufferFunction buffer_func;
1597 gpointer event_user_data;
1599 /* some magic to get the managing collect_pads2 */
1600 GST_OBJECT_LOCK (pad);
1601 data = (GstCollectData2 *) gst_pad_get_element_private (pad);
1602 if (G_UNLIKELY (data == NULL))
1603 goto pad_removed;
1604 ref_data (data);
1605 GST_OBJECT_UNLOCK (pad);
1607 res = FALSE;
1609 pads = data->collect;
1611 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
1612 GST_DEBUG_PAD_NAME (data->pad));
1614 GST_OBJECT_LOCK (pads);
1615 event_func = pads->event_func;
1616 event_user_data = pads->event_user_data;
1617 buffer_func = pads->buffer_func;
1618 GST_OBJECT_UNLOCK (pads);
1620 switch (GST_EVENT_TYPE (event)) {
1621 case GST_EVENT_FLUSH_START:
1622 {
1623 /* forward event to unblock check_collected */
1624 if (event_func)
1625 res = event_func (pads, data, event, event_user_data);
1626 if (!res)
1627 res = gst_pad_event_default (pad, event);
1629 /* now unblock the chain function.
1630 * no cond per pad, so they all unblock,
1631 * non-flushing block again */
1632 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1633 GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_FLUSHING);
1634 gst_collect_pads2_clear (pads, data);
1636 /* cater for possible default muxing functionality */
1637 if (buffer_func) {
1638 /* restore to initial state */
1639 gst_collect_pads2_set_waiting (pads, data, TRUE);
1640 /* if the current pad is affected, reset state, recalculate later */
1641 if (pads->earliest_data == data) {
1642 unref_data (data);
1643 pads->earliest_data = NULL;
1644 pads->earliest_time = GST_CLOCK_TIME_NONE;
1645 }
1646 }
1648 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1650 /* event already cleaned up by forwarding */
1651 res = TRUE;
1652 goto done;
1653 }
1654 case GST_EVENT_FLUSH_STOP:
1655 {
1656 /* flush the 1 buffer queue */
1657 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1658 GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_FLUSHING);
1659 gst_collect_pads2_clear (pads, data);
1660 /* we need new segment info after the flush */
1661 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
1662 GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_NEW_SEGMENT);
1663 /* if the pad was EOS, remove the EOS flag and
1664 * decrement the number of eospads */
1665 if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data,
1666 GST_COLLECT_PADS2_STATE_EOS))) {
1667 if (!GST_COLLECT_PADS2_STATE_IS_SET (data,
1668 GST_COLLECT_PADS2_STATE_WAITING))
1669 pads->queuedpads++;
1670 pads->eospads--;
1671 GST_COLLECT_PADS2_STATE_UNSET (data, GST_COLLECT_PADS2_STATE_EOS);
1672 }
1673 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1675 /* forward event */
1676 goto forward_or_default;
1677 }
1678 case GST_EVENT_EOS:
1679 {
1680 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1681 /* if the pad was not EOS, make it EOS and so we
1682 * have one more eospad */
1683 if (G_LIKELY (!GST_COLLECT_PADS2_STATE_IS_SET (data,
1684 GST_COLLECT_PADS2_STATE_EOS))) {
1685 GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_EOS);
1686 if (!GST_COLLECT_PADS2_STATE_IS_SET (data,
1687 GST_COLLECT_PADS2_STATE_WAITING))
1688 pads->queuedpads--;
1689 pads->eospads++;
1690 }
1691 /* check if we need collecting anything, we ignore the result. */
1692 gst_collect_pads2_check_collected (pads);
1693 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1695 goto forward_or_eat;
1696 }
1697 case GST_EVENT_NEWSEGMENT:
1698 {
1699 gint64 start, stop, time;
1700 gdouble rate, arate;
1701 GstFormat format;
1702 gboolean update;
1703 gint cmp_res;
1705 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1707 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1708 &start, &stop, &time);
1710 GST_DEBUG_OBJECT (data->pad, "got newsegment, start %" GST_TIME_FORMAT
1711 ", stop %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
1712 GST_TIME_ARGS (stop));
1714 gst_segment_set_newsegment_full (&data->segment, update, rate, arate,
1715 format, start, stop, time);
1717 GST_COLLECT_PADS2_STATE_SET (data, GST_COLLECT_PADS2_STATE_NEW_SEGMENT);
1719 /* default muxing functionality */
1720 if (!buffer_func)
1721 goto newsegment_done;
1723 /* default collection can not handle other segment formats than time */
1724 if (format != GST_FORMAT_TIME) {
1725 GST_ERROR_OBJECT (pads, "GstCollectPads2 default collecting "
1726 "can only handle time segments.");
1727 goto newsegment_done;
1728 }
1730 /* If oldest time is not known, or current pad got newsegment;
1731 * recalculate the state */
1732 if (!pads->earliest_data || pads->earliest_data == data) {
1733 gst_collect_pads2_recalculate_full (pads);
1734 goto newsegment_done;
1735 }
1737 /* Check if the waiting state of the pad should change. */
1738 cmp_res = pads->compare_func (pads, data, start, pads->earliest_data,
1739 pads->earliest_time, pads->compare_user_data);
1741 if (cmp_res > 0)
1742 /* Stop waiting */
1743 gst_collect_pads2_set_waiting (pads, data, FALSE);
1745 newsegment_done:
1746 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1747 /* we must not forward this event since multiple segments will be
1748 * accumulated and this is certainly not what we want. */
1749 goto forward_or_eat;
1750 }
1751 default:
1752 /* forward other events */
1753 goto forward_or_default;
1754 }
1756 forward_or_default:
1757 if (GST_EVENT_IS_SERIALIZED (event)) {
1758 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1759 need_unlock = TRUE;
1760 }
1761 if (event_func)
1762 res = event_func (pads, data, event, event_user_data);
1763 if (!res)
1764 res = gst_pad_event_default (pad, event);
1765 if (need_unlock)
1766 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1767 goto done;
1769 forward_or_eat:
1770 if (GST_EVENT_IS_SERIALIZED (event)) {
1771 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1772 need_unlock = TRUE;
1773 }
1774 if (event_func)
1775 res = event_func (pads, data, event, event_user_data);
1776 if (!res) {
1777 gst_event_unref (event);
1778 res = TRUE;
1779 }
1780 if (need_unlock)
1781 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1782 goto done;
1784 done:
1785 unref_data (data);
1786 return res;
1788 /* ERRORS */
1789 pad_removed:
1790 {
1791 GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
1792 GST_OBJECT_UNLOCK (pad);
1793 return FALSE;
1794 }
1795 }
1797 /* For each buffer we receive we check if our collected condition is reached
1798 * and if so we call the collected function. When this is done we check if
1799 * data has been unqueued. If data is still queued we wait holding the stream
1800 * lock to make sure no EOS event can happen while we are ready to be
1801 * collected
1802 */
1803 static GstFlowReturn
1804 gst_collect_pads2_chain (GstPad * pad, GstBuffer * buffer)
1805 {
1806 GstCollectData2 *data;
1807 GstCollectPads2 *pads;
1808 GstFlowReturn ret;
1809 GstBuffer **buffer_p;
1810 guint32 cookie;
1812 GST_DEBUG ("Got buffer for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1814 /* some magic to get the managing collect_pads2 */
1815 GST_OBJECT_LOCK (pad);
1816 data = (GstCollectData2 *) gst_pad_get_element_private (pad);
1817 if (G_UNLIKELY (data == NULL))
1818 goto no_data;
1819 ref_data (data);
1820 GST_OBJECT_UNLOCK (pad);
1822 pads = data->collect;
1824 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1825 /* if not started, bail out */
1826 if (G_UNLIKELY (!pads->started))
1827 goto not_started;
1828 /* check if this pad is flushing */
1829 if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data,
1830 GST_COLLECT_PADS2_STATE_FLUSHING)))
1831 goto flushing;
1832 /* pad was EOS, we can refuse this data */
1833 if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data,
1834 GST_COLLECT_PADS2_STATE_EOS)))
1835 goto unexpected;
1837 /* see if we need to clip */
1838 if (pads->clip_func) {
1839 GstBuffer *outbuf = NULL;
1840 ret = pads->clip_func (pads, data, buffer, &outbuf, pads->clip_user_data);
1841 buffer = outbuf;
1843 if (G_UNLIKELY (outbuf == NULL))
1844 goto clipped;
1846 if (G_UNLIKELY (ret == GST_FLOW_UNEXPECTED))
1847 goto unexpected;
1848 else if (G_UNLIKELY (ret != GST_FLOW_OK))
1849 goto error;
1850 }
1852 GST_DEBUG_OBJECT (pads, "Queuing buffer %p for pad %s:%s", buffer,
1853 GST_DEBUG_PAD_NAME (pad));
1855 /* One more pad has data queued */
1856 if (GST_COLLECT_PADS2_STATE_IS_SET (data, GST_COLLECT_PADS2_STATE_WAITING))
1857 pads->queuedpads++;
1858 buffer_p = &data->buffer;
1859 gst_buffer_replace (buffer_p, buffer);
1861 /* update segment last position if in TIME */
1862 if (G_LIKELY (data->segment.format == GST_FORMAT_TIME)) {
1863 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
1865 if (GST_CLOCK_TIME_IS_VALID (timestamp))
1866 gst_segment_set_last_stop (&data->segment, GST_FORMAT_TIME, timestamp);
1867 }
1869 /* While we have data queued on this pad try to collect stuff */
1870 do {
1871 /* Check if our collected condition is matched and call the collected
1872 * function if it is */
1873 ret = gst_collect_pads2_check_collected (pads);
1874 /* when an error occurs, we want to report this back to the caller ASAP
1875 * without having to block if the buffer was not popped */
1876 if (G_UNLIKELY (ret != GST_FLOW_OK))
1877 goto error;
1879 /* data was consumed, we can exit and accept new data */
1880 if (data->buffer == NULL)
1881 break;
1883 /* Having the _INIT here means we don't care about any broadcast up to here
1884 * (most of which occur with STREAM_LOCK held, so could not have happened
1885 * anyway). We do care about e.g. a remove initiated broadcast as of this
1886 * point. Putting it here also makes this thread ignores any evt it raised
1887 * itself (as is a usual WAIT semantic).
1888 */
1889 GST_COLLECT_PADS2_EVT_INIT (cookie);
1891 /* pad could be removed and re-added */
1892 unref_data (data);
1893 GST_OBJECT_LOCK (pad);
1894 if (G_UNLIKELY ((data = gst_pad_get_element_private (pad)) == NULL))
1895 goto pad_removed;
1896 ref_data (data);
1897 GST_OBJECT_UNLOCK (pad);
1899 GST_DEBUG_OBJECT (pads, "Pad %s:%s has a buffer queued, waiting",
1900 GST_DEBUG_PAD_NAME (pad));
1902 /* wait to be collected, this must happen from another thread triggered
1903 * by the _chain function of another pad. We release the lock so we
1904 * can get stopped or flushed as well. We can however not get EOS
1905 * because we still hold the STREAM_LOCK.
1906 */
1907 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1908 GST_COLLECT_PADS2_EVT_WAIT (pads, cookie);
1909 GST_COLLECT_PADS2_STREAM_LOCK (pads);
1911 GST_DEBUG_OBJECT (pads, "Pad %s:%s resuming", GST_DEBUG_PAD_NAME (pad));
1913 /* after a signal, we could be stopped */
1914 if (G_UNLIKELY (!pads->started))
1915 goto not_started;
1916 /* check if this pad is flushing */
1917 if (G_UNLIKELY (GST_COLLECT_PADS2_STATE_IS_SET (data,
1918 GST_COLLECT_PADS2_STATE_FLUSHING)))
1919 goto flushing;
1920 }
1921 while (data->buffer != NULL);
1923 unlock_done:
1924 GST_COLLECT_PADS2_STREAM_UNLOCK (pads);
1925 unref_data (data);
1926 if (buffer)
1927 gst_buffer_unref (buffer);
1928 return ret;
1930 pad_removed:
1931 {
1932 GST_WARNING ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
1933 GST_OBJECT_UNLOCK (pad);
1934 ret = GST_FLOW_NOT_LINKED;
1935 goto unlock_done;
1936 }
1937 /* ERRORS */
1938 no_data:
1939 {
1940 GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
1941 GST_OBJECT_UNLOCK (pad);
1942 gst_buffer_unref (buffer);
1943 return GST_FLOW_NOT_LINKED;
1944 }
1945 not_started:
1946 {
1947 GST_DEBUG ("not started");
1948 gst_collect_pads2_clear (pads, data);
1949 ret = GST_FLOW_WRONG_STATE;
1950 goto unlock_done;
1951 }
1952 flushing:
1953 {
1954 GST_DEBUG ("pad %s:%s is flushing", GST_DEBUG_PAD_NAME (pad));
1955 gst_collect_pads2_clear (pads, data);
1956 ret = GST_FLOW_WRONG_STATE;
1957 goto unlock_done;
1958 }
1959 unexpected:
1960 {
1961 /* we should not post an error for this, just inform upstream that
1962 * we don't expect anything anymore */
1963 GST_DEBUG ("pad %s:%s is eos", GST_DEBUG_PAD_NAME (pad));
1964 ret = GST_FLOW_UNEXPECTED;
1965 goto unlock_done;
1966 }
1967 clipped:
1968 {
1969 GST_DEBUG ("clipped buffer on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1970 ret = GST_FLOW_OK;
1971 goto unlock_done;
1972 }
1973 error:
1974 {
1975 /* we print the error, the element should post a reasonable error
1976 * message for fatal errors */
1977 GST_DEBUG ("collect failed, reason %d (%s)", ret, gst_flow_get_name (ret));
1978 gst_collect_pads2_clear (pads, data);
1979 goto unlock_done;
1980 }
1981 }