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