2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * SECTION:gstcollectpads
23 * @short_description: manages a set of pads that operate in collect mode
26 * Manages a set of pads that operate in collect mode. This means that control
27 * is given to the manager of this object when all pads have data.
30 * Collectpads are created with gst_collect_pads_new(). A callback should then
31 * be installed with gst_collect_pads_set_function ().
34 * Pads are added to the collection with gst_collect_pads_add_pad()/
35 * gst_collect_pads_remove_pad(). The pad
36 * has to be a sinkpad. The chain and event functions of the pad are
37 * overridden. The element_private of the pad is used to store
38 * private information for the collectpads.
41 * For each pad, data is queued in the _chain function or by
42 * performing a pull_range.
45 * When data is queued on all pads, the callback function is called.
48 * Data can be dequeued from the pad with the gst_collect_pads_pop() method.
49 * One can peek at the data with the gst_collect_pads_peek() function.
50 * These functions will return NULL if the pad received an EOS event. When all
51 * pads return NULL from a gst_collect_pads_peek(), the element can emit an EOS
55 * Data can also be dequeued in byte units using the gst_collect_pads_available(),
56 * gst_collect_pads_read() and gst_collect_pads_flush() calls.
59 * Elements should call gst_collect_pads_start() and gst_collect_pads_stop() in
60 * their state change functions to start and stop the processing of the collecpads.
61 * The gst_collect_pads_stop() call should be called before calling the parent
62 * element state change function in the PAUSED_TO_READY state change to ensure
63 * no pad is blocked and the element can finish streaming.
66 * gst_collect_pads_collect() and gst_collect_pads_collect_range() can be used by
67 * elements that start a #GstTask to drive the collect_pads. This feature is however
68 * not yet implemented.
72 * Last reviewed on 2006-05-10 (0.10.6)
75 #include "gstcollectpads.h"
77 GST_DEBUG_CATEGORY_STATIC (collect_pads_debug);
78 #define GST_CAT_DEFAULT collect_pads_debug
80 #define GST_COLLECT_PADS_GET_PRIVATE(obj) \
81 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_COLLECT_PADS, GstCollectPadsPrivate))
83 struct _GstCollectPadsPrivate
85 GstCollectPadsClipFunction clipfunc;
86 gpointer clipfunc_user_data;
89 #define gst_collect_pads_parent_class parent_class
90 G_DEFINE_TYPE (GstCollectPads, gst_collect_pads, GST_TYPE_OBJECT);
92 static void gst_collect_pads_clear (GstCollectPads * pads,
93 GstCollectData * data);
94 static GstFlowReturn gst_collect_pads_chain (GstPad * pad, GstBuffer * buffer);
95 static gboolean gst_collect_pads_event (GstPad * pad, GstEvent * event);
96 static void gst_collect_pads_finalize (GObject * object);
97 static void ref_data (GstCollectData * data);
98 static void unref_data (GstCollectData * data);
99 static void gst_collect_pads_check_pads_unlocked (GstCollectPads * pads);
102 gst_collect_pads_class_init (GstCollectPadsClass * klass)
104 GObjectClass *gobject_class = (GObjectClass *) klass;
106 g_type_class_add_private (klass, sizeof (GstCollectPadsPrivate));
108 GST_DEBUG_CATEGORY_INIT (collect_pads_debug, "collectpads", 0,
111 gobject_class->finalize = gst_collect_pads_finalize;
115 gst_collect_pads_init (GstCollectPads * pads)
117 pads->abidata.ABI.priv = GST_COLLECT_PADS_GET_PRIVATE (pads);
119 pads->cond = g_cond_new ();
123 pads->queuedpads = 0;
125 pads->started = FALSE;
127 /* members to manage the pad list */
128 pads->abidata.ABI.pad_lock = g_mutex_new ();
129 pads->abidata.ABI.pad_cookie = 0;
130 pads->abidata.ABI.pad_list = NULL;
134 gst_collect_pads_finalize (GObject * object)
137 GstCollectPads *pads = GST_COLLECT_PADS (object);
139 GST_DEBUG ("finalize");
141 g_cond_free (pads->cond);
142 g_mutex_free (pads->abidata.ABI.pad_lock);
145 collected = pads->abidata.ABI.pad_list;
146 for (; collected; collected = g_slist_next (collected)) {
147 GstCollectData *pdata = (GstCollectData *) collected->data;
152 g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
153 g_slist_free (pads->data);
154 g_slist_free (pads->abidata.ABI.pad_list);
156 G_OBJECT_CLASS (parent_class)->finalize (object);
160 * gst_collect_pads_new:
162 * Create a new instance of #GstCollectPads.
166 * Returns: (transfer full): a new #GstCollectPads, or NULL in case of an error.
169 gst_collect_pads_new (void)
171 GstCollectPads *newcoll;
173 newcoll = g_object_newv (GST_TYPE_COLLECT_PADS, 0, NULL);
179 * gst_collect_pads_set_function:
180 * @pads: the collectspads to use
181 * @func: the function to set
182 * @user_data: (closure): user data passed to the function
184 * Set the callback function and user data that will be called when
185 * all the pads added to the collection have buffers queued.
190 gst_collect_pads_set_function (GstCollectPads * pads,
191 GstCollectPadsFunction func, gpointer user_data)
193 g_return_if_fail (pads != NULL);
194 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
196 GST_OBJECT_LOCK (pads);
198 pads->user_data = user_data;
199 GST_OBJECT_UNLOCK (pads);
203 ref_data (GstCollectData * data)
205 g_assert (data != NULL);
207 g_atomic_int_inc (&(data->abidata.ABI.refcount));
211 unref_data (GstCollectData * data)
213 GstCollectDataDestroyNotify destroy_notify;
215 g_assert (data != NULL);
216 g_assert (data->abidata.ABI.refcount > 0);
218 if (!g_atomic_int_dec_and_test (&(data->abidata.ABI.refcount)))
221 /* FIXME: Ugly hack as we can't add more fields to GstCollectData */
222 destroy_notify = (GstCollectDataDestroyNotify)
223 g_object_get_data (G_OBJECT (data->pad),
224 "gst-collect-data-destroy-notify");
227 destroy_notify (data);
229 g_object_unref (data->pad);
231 gst_buffer_unref (data->buffer);
237 * gst_collect_pads_add_pad:
238 * @pads: the collectspads to use
239 * @pad: (transfer none): the pad to add
240 * @size: the size of the returned #GstCollectData structure
242 * Add a pad to the collection of collect pads. The pad has to be
243 * a sinkpad. The refcount of the pad is incremented. Use
244 * gst_collect_pads_remove_pad() to remove the pad from the collection
247 * This function will override the chain and event functions of the pad
248 * along with the element_private data, which is used to store private
249 * information for the collectpads.
251 * You specify a size for the returned #GstCollectData structure
252 * so that you can use it to store additional information.
254 * The pad will be automatically activated in push mode when @pads is
257 * This function calls gst_collect_pads_add_pad_full() passing a value of NULL
258 * for destroy_notify.
262 * Returns: a new #GstCollectData to identify the new pad. Or NULL
263 * if wrong parameters are supplied.
266 gst_collect_pads_add_pad (GstCollectPads * pads, GstPad * pad, guint size)
268 return gst_collect_pads_add_pad_full (pads, pad, size, NULL);
272 * gst_collect_pads_add_pad_full:
273 * @pads: the collectspads to use
274 * @pad: (transfer none): the pad to add
275 * @size: the size of the returned #GstCollectData structure
276 * @destroy_notify: function to be called before the returned #GstCollectData
279 * Add a pad to the collection of collect pads. The pad has to be
280 * a sinkpad. The refcount of the pad is incremented. Use
281 * gst_collect_pads_remove_pad() to remove the pad from the collection
284 * You specify a size for the returned #GstCollectData structure
285 * so that you can use it to store additional information.
287 * You can also specify a #GstCollectDataDestroyNotify that will be called
288 * just before the #GstCollectData structure is freed. It is passed the
289 * pointer to the structure and should free any custom memory and resources
292 * The pad will be automatically activated in push mode when @pads is
299 * Returns: a new #GstCollectData to identify the new pad. Or NULL
300 * if wrong parameters are supplied.
303 gst_collect_pads_add_pad_full (GstCollectPads * pads, GstPad * pad, guint size,
304 GstCollectDataDestroyNotify destroy_notify)
306 GstCollectData *data;
308 g_return_val_if_fail (pads != NULL, NULL);
309 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
310 g_return_val_if_fail (pad != NULL, NULL);
311 g_return_val_if_fail (GST_PAD_IS_SINK (pad), NULL);
312 g_return_val_if_fail (size >= sizeof (GstCollectData), NULL);
314 GST_DEBUG ("adding pad %s:%s", GST_DEBUG_PAD_NAME (pad));
316 data = g_malloc0 (size);
317 data->collect = pads;
318 data->pad = gst_object_ref (pad);
321 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
322 data->abidata.ABI.flushing = FALSE;
323 data->abidata.ABI.new_segment = FALSE;
324 data->abidata.ABI.eos = FALSE;
325 data->abidata.ABI.refcount = 1;
327 /* FIXME: Ugly hack as we can't add more fields to GstCollectData */
328 g_object_set_data (G_OBJECT (pad), "gst-collect-data-destroy-notify",
329 (void *) destroy_notify);
331 GST_COLLECT_PADS_PAD_LOCK (pads);
332 GST_OBJECT_LOCK (pad);
333 gst_pad_set_element_private (pad, data);
334 GST_OBJECT_UNLOCK (pad);
335 pads->abidata.ABI.pad_list =
336 g_slist_append (pads->abidata.ABI.pad_list, data);
337 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_chain));
338 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_collect_pads_event));
339 /* activate the pad when needed */
341 gst_pad_set_active (pad, TRUE);
342 pads->abidata.ABI.pad_cookie++;
343 GST_COLLECT_PADS_PAD_UNLOCK (pads);
349 find_pad (GstCollectData * data, GstPad * pad)
351 if (data->pad == pad)
357 * gst_collect_pads_set_clip_function:
358 * @pads: the collectspads to use
359 * @clipfunc: clip function to install
360 * @user_data: (closure): user data to pass to @clip_func
362 * Install a clipping function that is called right after a buffer is received
363 * on a pad managed by @pads. See #GstCollectPadsClipFunction for more info.
368 gst_collect_pads_set_clip_function (GstCollectPads * pads,
369 GstCollectPadsClipFunction clipfunc, gpointer user_data)
371 GstCollectPadsPrivate *priv;
373 g_return_if_fail (pads != NULL);
374 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
376 priv = pads->abidata.ABI.priv;
378 priv->clipfunc = clipfunc;
379 priv->clipfunc_user_data = user_data;
383 * gst_collect_pads_remove_pad:
384 * @pads: the collectspads to use
385 * @pad: (transfer none): the pad to remove
387 * Remove a pad from the collection of collect pads. This function will also
388 * free the #GstCollectData and all the resources that were allocated with
389 * gst_collect_pads_add_pad().
391 * The pad will be deactivated automatically when @pads is stopped.
395 * Returns: %TRUE if the pad could be removed.
398 gst_collect_pads_remove_pad (GstCollectPads * pads, GstPad * pad)
400 GstCollectData *data;
403 g_return_val_if_fail (pads != NULL, FALSE);
404 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), FALSE);
405 g_return_val_if_fail (pad != NULL, FALSE);
406 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
408 GST_DEBUG ("removing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
410 GST_COLLECT_PADS_PAD_LOCK (pads);
412 g_slist_find_custom (pads->abidata.ABI.pad_list, pad,
413 (GCompareFunc) find_pad);
417 data = (GstCollectData *) list->data;
419 GST_DEBUG ("found pad %s:%s at %p", GST_DEBUG_PAD_NAME (pad), data);
421 /* clear the stuff we configured */
422 gst_pad_set_chain_function (pad, NULL);
423 gst_pad_set_event_function (pad, NULL);
424 GST_OBJECT_LOCK (pad);
425 gst_pad_set_element_private (pad, NULL);
426 GST_OBJECT_UNLOCK (pad);
428 /* backward compat, also remove from data if stopped, note that this function
429 * can only be called when we are stopped because we don't take the LOCK to
430 * protect the pads->data list. */
431 if (!pads->started) {
434 dlist = g_slist_find_custom (pads->data, pad, (GCompareFunc) find_pad);
436 GstCollectData *pdata = dlist->data;
438 pads->data = g_slist_delete_link (pads->data, dlist);
442 /* remove from the pad list */
443 pads->abidata.ABI.pad_list =
444 g_slist_delete_link (pads->abidata.ABI.pad_list, list);
445 pads->abidata.ABI.pad_cookie++;
447 /* signal waiters because something changed */
448 GST_COLLECT_PADS_BROADCAST (pads);
450 /* deactivate the pad when needed */
452 gst_pad_set_active (pad, FALSE);
454 /* clean and free the collect data */
457 GST_COLLECT_PADS_PAD_UNLOCK (pads);
463 GST_WARNING ("cannot remove unknown pad %s:%s", GST_DEBUG_PAD_NAME (pad));
464 GST_COLLECT_PADS_PAD_UNLOCK (pads);
470 * gst_collect_pads_is_active:
471 * @pads: (transfer none): the collectspads to use
472 * @pad: the pad to check
474 * Check if a pad is active.
476 * This function is currently not implemented.
480 * Returns: %TRUE if the pad is active.
483 gst_collect_pads_is_active (GstCollectPads * pads, GstPad * pad)
485 g_return_val_if_fail (pads != NULL, FALSE);
486 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), FALSE);
487 g_return_val_if_fail (pad != NULL, FALSE);
488 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
490 g_warning ("gst_collect_pads_is_active() is not implemented");
496 * gst_collect_pads_collect:
497 * @pads: the collectspads to use
499 * Collect data on all pads. This function is usually called
500 * from a #GstTask function in an element.
502 * This function is currently not implemented.
506 * Returns: #GstFlowReturn of the operation.
509 gst_collect_pads_collect (GstCollectPads * pads)
511 g_return_val_if_fail (pads != NULL, GST_FLOW_ERROR);
512 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), GST_FLOW_ERROR);
514 g_warning ("gst_collect_pads_collect() is not implemented");
516 return GST_FLOW_NOT_SUPPORTED;
520 * gst_collect_pads_collect_range:
521 * @pads: the collectspads to use
522 * @offset: the offset to collect
523 * @length: the length to collect
525 * Collect data with @offset and @length on all pads. This function
526 * is typically called in the getrange function of an element.
528 * This function is currently not implemented.
532 * Returns: #GstFlowReturn of the operation.
535 gst_collect_pads_collect_range (GstCollectPads * pads, guint64 offset,
538 g_return_val_if_fail (pads != NULL, GST_FLOW_ERROR);
539 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), GST_FLOW_ERROR);
541 g_warning ("gst_collect_pads_collect_range() is not implemented");
543 return GST_FLOW_NOT_SUPPORTED;
547 gst_collect_pads_is_flushing (GstCollectPads * pads)
552 GST_COLLECT_PADS_PAD_LOCK (pads);
554 /* Ensure pads->data state */
555 gst_collect_pads_check_pads_unlocked (pads);
557 GST_DEBUG ("Getting flushing state (pads:%p, pads->data:%p)",
560 for (walk = pads->data; walk; walk = g_slist_next (walk)) {
561 GstCollectData *cdata = walk->data;
563 GST_DEBUG_OBJECT (cdata->pad, "flushing:%d", cdata->abidata.ABI.flushing);
565 if (cdata->abidata.ABI.flushing) {
572 GST_COLLECT_PADS_PAD_UNLOCK (pads);
576 /* FIXME, I think this function is used to work around bad behaviour
577 * of elements that add pads to themselves without activating them.
579 * Must be called with PAD_LOCK.
582 gst_collect_pads_set_flushing_unlocked (GstCollectPads * pads,
587 GST_DEBUG ("Setting flushing (%d)", flushing);
589 /* Update the pads flushing flag */
590 for (walk = pads->data; walk; walk = g_slist_next (walk)) {
591 GstCollectData *cdata = walk->data;
593 if (GST_IS_PAD (cdata->pad)) {
594 GST_OBJECT_LOCK (cdata->pad);
596 GST_PAD_SET_FLUSHING (cdata->pad);
598 GST_PAD_UNSET_FLUSHING (cdata->pad);
599 cdata->abidata.ABI.flushing = flushing;
600 gst_collect_pads_clear (pads, cdata);
601 GST_OBJECT_UNLOCK (cdata->pad);
604 /* Setting the pads to flushing means that we changed the values which
605 * are 'protected' by the cookie. We therefore update it to force a
606 * recalculation of the current pad status. */
607 pads->abidata.ABI.pad_cookie++;
611 * gst_collect_pads_set_flushing:
612 * @pads: the collectspads to use
613 * @flushing: desired state of the pads
615 * Change the flushing state of all the pads in the collection. No pad
616 * is able to accept anymore data when @flushing is %TRUE. Calling this
617 * function with @flushing %FALSE makes @pads accept data again.
624 gst_collect_pads_set_flushing (GstCollectPads * pads, gboolean flushing)
626 g_return_if_fail (pads != NULL);
627 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
629 GST_COLLECT_PADS_PAD_LOCK (pads);
630 /* Ensure pads->data state */
631 gst_collect_pads_check_pads_unlocked (pads);
632 gst_collect_pads_set_flushing_unlocked (pads, flushing);
633 GST_COLLECT_PADS_PAD_UNLOCK (pads);
637 * gst_collect_pads_start:
638 * @pads: the collectspads to use
640 * Starts the processing of data in the collect_pads.
645 gst_collect_pads_start (GstCollectPads * pads)
649 g_return_if_fail (pads != NULL);
650 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
652 GST_DEBUG_OBJECT (pads, "starting collect pads");
654 /* make sure stop and collect cannot be called anymore */
655 GST_OBJECT_LOCK (pads);
657 /* make pads streamable */
658 GST_COLLECT_PADS_PAD_LOCK (pads);
660 /* loop over the master pad list and reset the segment */
661 collected = pads->abidata.ABI.pad_list;
662 for (; collected; collected = g_slist_next (collected)) {
663 GstCollectData *data;
665 data = collected->data;
666 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
669 gst_collect_pads_set_flushing_unlocked (pads, FALSE);
671 /* Start collect pads */
672 pads->started = TRUE;
673 GST_COLLECT_PADS_PAD_UNLOCK (pads);
674 GST_OBJECT_UNLOCK (pads);
678 * gst_collect_pads_stop:
679 * @pads: the collectspads to use
681 * Stops the processing of data in the collect_pads. this function
682 * will also unblock any blocking operations.
687 gst_collect_pads_stop (GstCollectPads * pads)
691 g_return_if_fail (pads != NULL);
692 g_return_if_fail (GST_IS_COLLECT_PADS (pads));
694 GST_DEBUG_OBJECT (pads, "stopping collect pads");
696 /* make sure collect and start cannot be called anymore */
697 GST_OBJECT_LOCK (pads);
699 /* make pads not accept data anymore */
700 GST_COLLECT_PADS_PAD_LOCK (pads);
701 gst_collect_pads_set_flushing_unlocked (pads, TRUE);
703 /* Stop collect pads */
704 pads->started = FALSE;
706 pads->queuedpads = 0;
708 /* loop over the master pad list and flush buffers */
709 collected = pads->abidata.ABI.pad_list;
710 for (; collected; collected = g_slist_next (collected)) {
711 GstCollectData *data;
712 GstBuffer **buffer_p;
714 data = collected->data;
716 buffer_p = &data->buffer;
717 gst_buffer_replace (buffer_p, NULL);
720 data->abidata.ABI.eos = FALSE;
723 GST_COLLECT_PADS_PAD_UNLOCK (pads);
724 /* Wake them up so they can end the chain functions. */
725 GST_COLLECT_PADS_BROADCAST (pads);
727 GST_OBJECT_UNLOCK (pads);
731 * gst_collect_pads_peek:
732 * @pads: the collectspads to peek
733 * @data: the data to use
735 * Peek at the buffer currently queued in @data. This function
736 * should be called with the @pads LOCK held, such as in the callback
741 * Returns: (transfer full): The buffer in @data or NULL if no buffer is queued.
742 * should unref the buffer after usage.
745 gst_collect_pads_peek (GstCollectPads * pads, GstCollectData * data)
749 g_return_val_if_fail (pads != NULL, NULL);
750 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
751 g_return_val_if_fail (data != NULL, NULL);
753 if ((result = data->buffer))
754 gst_buffer_ref (result);
756 GST_DEBUG ("Peeking at pad %s:%s: buffer=%p",
757 GST_DEBUG_PAD_NAME (data->pad), result);
763 * gst_collect_pads_pop:
764 * @pads: the collectspads to pop
765 * @data: the data to use
767 * Pop the buffer currently queued in @data. This function
768 * should be called with the @pads LOCK held, such as in the callback
771 * Free-function: gst_buffer_unref
775 * Returns: (transfer full): The buffer in @data or NULL if no buffer was
776 * queued. You should unref the buffer after usage.
779 gst_collect_pads_pop (GstCollectPads * pads, GstCollectData * data)
783 g_return_val_if_fail (pads != NULL, NULL);
784 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
785 g_return_val_if_fail (data != NULL, NULL);
787 if ((result = data->buffer)) {
790 /* one less pad with queued data now */
794 GST_COLLECT_PADS_BROADCAST (pads);
796 GST_DEBUG ("Pop buffer on pad %s:%s: buffer=%p",
797 GST_DEBUG_PAD_NAME (data->pad), result);
802 /* pop and unref the currently queued buffer, should e called with the LOCK
805 gst_collect_pads_clear (GstCollectPads * pads, GstCollectData * data)
809 if ((buf = gst_collect_pads_pop (pads, data)))
810 gst_buffer_unref (buf);
814 * gst_collect_pads_available:
815 * @pads: the collectspads to query
817 * Query how much bytes can be read from each queued buffer. This means
818 * that the result of this call is the maximum number of bytes that can
819 * be read from each of the pads.
821 * This function should be called with @pads LOCK held, such as
826 * Returns: The maximum number of bytes queued on all pads. This function
827 * returns 0 if a pad has no queued buffer.
829 /* FIXME, we can do this in the _chain functions */
831 gst_collect_pads_available (GstCollectPads * pads)
834 guint result = G_MAXUINT;
836 g_return_val_if_fail (pads != NULL, 0);
837 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), 0);
839 for (collected = pads->data; collected; collected = g_slist_next (collected)) {
840 GstCollectData *pdata;
844 pdata = (GstCollectData *) collected->data;
846 /* ignore pad with EOS */
847 if (G_UNLIKELY (pdata->abidata.ABI.eos)) {
848 GST_DEBUG ("pad %s:%s is EOS", GST_DEBUG_PAD_NAME (pdata->pad));
852 /* an empty buffer without EOS is weird when we get here.. */
853 if (G_UNLIKELY ((buffer = pdata->buffer) == NULL)) {
854 GST_WARNING ("pad %s:%s has no buffer", GST_DEBUG_PAD_NAME (pdata->pad));
858 /* this is the size left of the buffer */
859 size = gst_buffer_get_size (buffer) - pdata->pos;
860 GST_DEBUG ("pad %s:%s has %d bytes left",
861 GST_DEBUG_PAD_NAME (pdata->pad), size);
863 /* need to return the min of all available data */
867 /* nothing changed, all must be EOS then, return 0 */
868 if (G_UNLIKELY (result == G_MAXUINT))
880 * gst_collect_pads_read_buffer:
881 * @pads: the collectspads to query
882 * @data: the data to use
883 * @size: the number of bytes to read
885 * Get a buffer of @size bytes from the given pad @data.
887 * This function should be called with @pads LOCK held, such as in the callback.
889 * Free-function: gst_buffer_unref
891 * Returns: (transfer full): a #GstBuffer. The size of the buffer can be less
892 * that requested. A return of NULL signals that the pad is end-of-stream.
893 * Unref the buffer with gst_buffer_unref() after use.
900 gst_collect_pads_read_buffer (GstCollectPads * pads, GstCollectData * data,
903 guint readsize, bufsize;
906 g_return_val_if_fail (pads != NULL, NULL);
907 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), NULL);
908 g_return_val_if_fail (data != NULL, NULL);
910 /* no buffer, must be EOS */
911 if ((buffer = data->buffer) == NULL)
914 bufsize = gst_buffer_get_size (buffer);
916 readsize = MIN (size, bufsize - data->pos);
918 if (data->pos == 0 && readsize == bufsize)
919 return gst_buffer_ref (buffer);
921 return gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, data->pos,
926 * gst_collect_pads_take_buffer:
927 * @pads: the collectspads to query
928 * @data: the data to use
929 * @size: the number of bytes to read
931 * Get a buffer of @size bytes from the given pad @data. Flushes the amount
934 * This function should be called with @pads LOCK held, such as in the callback.
936 * Free-function: gst_buffer_unref
940 * Returns: (transfer full): a #GstBuffer. The size of the buffer can be less
941 * that requested. A return of NULL signals that the pad is end-of-stream.
942 * Unref the buffer after use.
947 gst_collect_pads_take_buffer (GstCollectPads * pads, GstCollectData * data,
950 GstBuffer *buffer = gst_collect_pads_read_buffer (pads, data, size);
953 gst_collect_pads_flush (pads, data, gst_buffer_get_size (buffer));
959 * gst_collect_pads_flush:
960 * @pads: the collectspads to query
961 * @data: the data to use
962 * @size: the number of bytes to flush
964 * Flush @size bytes from the pad @data.
966 * This function should be called with @pads LOCK held, such as
971 * Returns: The number of bytes flushed. This can be less than @size and
972 * is 0 if the pad was end-of-stream.
975 gst_collect_pads_flush (GstCollectPads * pads, GstCollectData * data,
982 g_return_val_if_fail (pads != NULL, 0);
983 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), 0);
984 g_return_val_if_fail (data != NULL, 0);
986 /* no buffer, must be EOS */
987 if ((buffer = data->buffer) == NULL)
990 bsize = gst_buffer_get_size (buffer);
992 /* this is what we can flush at max */
993 flushsize = MIN (size, bsize - data->pos);
997 GST_LOG_OBJECT (pads, "Flushing %d bytes, requested %u", flushsize, size);
999 if (data->pos >= bsize)
1000 /* _clear will also reset data->pos to 0 */
1001 gst_collect_pads_clear (pads, data);
1006 /* see if pads were added or removed and update our stats. Any pad
1007 * added after releasing the PAD_LOCK will get collected in the next
1010 * We can do a quick check by checking the cookies, that get changed
1011 * whenever the pad list is updated.
1013 * Must be called with LOCK.
1016 gst_collect_pads_check_pads_unlocked (GstCollectPads * pads)
1018 GST_DEBUG ("stored cookie : %d, used_cookie:%d",
1019 pads->abidata.ABI.pad_cookie, pads->cookie);
1020 if (G_UNLIKELY (pads->abidata.ABI.pad_cookie != pads->cookie)) {
1023 /* clear list and stats */
1024 g_slist_foreach (pads->data, (GFunc) unref_data, NULL);
1025 g_slist_free (pads->data);
1028 pads->queuedpads = 0;
1031 /* loop over the master pad list */
1032 collected = pads->abidata.ABI.pad_list;
1033 for (; collected; collected = g_slist_next (collected)) {
1034 GstCollectData *data;
1036 /* update the stats */
1038 data = collected->data;
1040 if (G_LIKELY (!data->abidata.ABI.flushing)) {
1043 if (data->abidata.ABI.eos)
1047 /* add to the list of pads to collect */
1049 pads->data = g_slist_prepend (pads->data, data);
1051 /* and update the cookie */
1052 pads->cookie = pads->abidata.ABI.pad_cookie;
1057 gst_collect_pads_check_pads (GstCollectPads * pads)
1059 /* the master list and cookie are protected with the PAD_LOCK */
1060 GST_COLLECT_PADS_PAD_LOCK (pads);
1061 gst_collect_pads_check_pads_unlocked (pads);
1062 GST_COLLECT_PADS_PAD_UNLOCK (pads);
1065 /* checks if all the pads are collected and call the collectfunction
1067 * Should be called with LOCK.
1069 * Returns: The #GstFlowReturn of collection.
1071 static GstFlowReturn
1072 gst_collect_pads_check_collected (GstCollectPads * pads)
1074 GstFlowReturn flow_ret = GST_FLOW_OK;
1076 g_return_val_if_fail (GST_IS_COLLECT_PADS (pads), GST_FLOW_ERROR);
1077 g_return_val_if_fail (pads->func != NULL, GST_FLOW_NOT_SUPPORTED);
1079 /* check for new pads, update stats etc.. */
1080 gst_collect_pads_check_pads (pads);
1082 if (G_UNLIKELY (pads->eospads == pads->numpads)) {
1083 /* If all our pads are EOS just collect once to let the element
1084 * do its final EOS handling. */
1085 GST_DEBUG ("All active pads (%d) are EOS, calling %s",
1086 pads->numpads, GST_DEBUG_FUNCPTR_NAME (pads->func));
1087 flow_ret = pads->func (pads, pads->user_data);
1089 gboolean collected = FALSE;
1091 /* We call the collected function as long as our condition matches.
1092 * FIXME: should we error out if the collect function did not pop anything ?
1093 * we can get a busy loop here if the element does not pop from the collect
1096 while (((pads->queuedpads + pads->eospads) >= pads->numpads)) {
1097 GST_DEBUG ("All active pads (%d + %d >= %d) have data, calling %s",
1098 pads->queuedpads, pads->eospads, pads->numpads,
1099 GST_DEBUG_FUNCPTR_NAME (pads->func));
1100 flow_ret = pads->func (pads, pads->user_data);
1103 /* break on error */
1104 if (flow_ret != GST_FLOW_OK)
1106 /* Don't keep looping after telling the element EOS or flushing */
1107 if (pads->queuedpads == 0)
1111 GST_DEBUG ("Not all active pads (%d) have data, continuing",
1118 gst_collect_pads_event (GstPad * pad, GstEvent * event)
1121 GstCollectData *data;
1122 GstCollectPads *pads;
1124 /* some magic to get the managing collect_pads */
1125 GST_OBJECT_LOCK (pad);
1126 data = (GstCollectData *) gst_pad_get_element_private (pad);
1127 if (G_UNLIKELY (data == NULL))
1130 GST_OBJECT_UNLOCK (pad);
1134 pads = data->collect;
1136 GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
1137 GST_DEBUG_PAD_NAME (data->pad));
1139 switch (GST_EVENT_TYPE (event)) {
1140 case GST_EVENT_FLUSH_START:
1142 /* forward event to unblock check_collected */
1143 gst_pad_event_default (pad, event);
1145 /* now unblock the chain function.
1146 * no cond per pad, so they all unblock,
1147 * non-flushing block again */
1148 GST_OBJECT_LOCK (pads);
1149 data->abidata.ABI.flushing = TRUE;
1150 gst_collect_pads_clear (pads, data);
1151 GST_OBJECT_UNLOCK (pads);
1153 /* event already cleaned up by forwarding */
1156 case GST_EVENT_FLUSH_STOP:
1158 /* flush the 1 buffer queue */
1159 GST_OBJECT_LOCK (pads);
1160 data->abidata.ABI.flushing = FALSE;
1161 gst_collect_pads_clear (pads, data);
1162 /* we need new segment info after the flush */
1163 gst_segment_init (&data->segment, GST_FORMAT_UNDEFINED);
1164 data->abidata.ABI.new_segment = FALSE;
1165 /* if the pad was EOS, remove the EOS flag and
1166 * decrement the number of eospads */
1167 if (G_UNLIKELY (data->abidata.ABI.eos == TRUE)) {
1169 data->abidata.ABI.eos = FALSE;
1172 if (!gst_collect_pads_is_flushing (pads)) {
1173 /* forward event if all pads are no longer flushing */
1174 GST_DEBUG ("No more pads are flushing, forwarding FLUSH_STOP");
1175 GST_OBJECT_UNLOCK (pads);
1178 gst_event_unref (event);
1179 GST_OBJECT_UNLOCK (pads);
1184 GST_OBJECT_LOCK (pads);
1185 /* if the pad was not EOS, make it EOS and so we
1186 * have one more eospad */
1187 if (G_LIKELY (data->abidata.ABI.eos == FALSE)) {
1188 data->abidata.ABI.eos = TRUE;
1191 /* check if we need collecting anything, we ignore the
1193 gst_collect_pads_check_collected (pads);
1194 GST_OBJECT_UNLOCK (pads);
1196 /* We eat this event, element should do something
1197 * in the collected callback. */
1198 gst_event_unref (event);
1201 case GST_EVENT_NEWSEGMENT:
1203 gint64 start, stop, time;
1204 gdouble rate, arate;
1208 gst_event_parse_new_segment (event, &update, &rate, &arate, &format,
1209 &start, &stop, &time);
1211 GST_DEBUG_OBJECT (data->pad, "got newsegment, start %" GST_TIME_FORMAT
1212 ", stop %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
1213 GST_TIME_ARGS (stop));
1215 gst_segment_set_newsegment (&data->segment, update, rate, arate,
1216 format, start, stop, time);
1218 data->abidata.ABI.new_segment = TRUE;
1220 /* we must not forward this event since multiple segments will be
1221 * accumulated and this is certainly not what we want. */
1222 gst_event_unref (event);
1223 /* FIXME: collect-pads based elements need to create their own newsegment
1224 * event (and only one really)
1225 * (a) make the segment part of the GstCollectData structure of each pad,
1226 * so you can just check that once you have a buffer queued on that pad,
1227 * (b) you can override a pad's event function with your own,
1228 * catch the newsegment event and then pass it on to the original
1229 * gstcollectpads event function
1230 * (that's what avimux does for something IIRC)
1236 /* forward other events */
1241 GST_DEBUG_OBJECT (pads, "forward unhandled event: %s",
1242 GST_EVENT_TYPE_NAME (event));
1243 res = gst_pad_event_default (pad, event);
1252 GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
1253 GST_OBJECT_UNLOCK (pad);
1258 /* For each buffer we receive we check if our collected condition is reached
1259 * and if so we call the collected function. When this is done we check if
1260 * data has been unqueued. If data is still queued we wait holding the stream
1261 * lock to make sure no EOS event can happen while we are ready to be
1264 static GstFlowReturn
1265 gst_collect_pads_chain (GstPad * pad, GstBuffer * buffer)
1267 GstCollectData *data;
1268 GstCollectPads *pads;
1269 GstCollectPadsPrivate *priv;
1272 GST_DEBUG ("Got buffer for pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1274 /* some magic to get the managing collect_pads */
1275 GST_OBJECT_LOCK (pad);
1276 data = (GstCollectData *) gst_pad_get_element_private (pad);
1277 if (G_UNLIKELY (data == NULL))
1280 GST_OBJECT_UNLOCK (pad);
1282 pads = data->collect;
1283 priv = pads->abidata.ABI.priv;
1285 GST_OBJECT_LOCK (pads);
1286 /* if not started, bail out */
1287 if (G_UNLIKELY (!pads->started))
1289 /* check if this pad is flushing */
1290 if (G_UNLIKELY (data->abidata.ABI.flushing))
1292 /* pad was EOS, we can refuse this data */
1293 if (G_UNLIKELY (data->abidata.ABI.eos))
1296 /* see if we need to clip */
1297 if (priv->clipfunc) {
1298 buffer = priv->clipfunc (pads, data, buffer, priv->clipfunc_user_data);
1300 if (G_UNLIKELY (buffer == NULL))
1304 GST_DEBUG ("Queuing buffer %p for pad %s:%s", buffer,
1305 GST_DEBUG_PAD_NAME (pad));
1307 /* One more pad has data queued */
1309 /* take ownership of the buffer */
1311 gst_buffer_unref (data->buffer);
1312 data->buffer = buffer;
1315 /* update segment last position if in TIME */
1316 if (G_LIKELY (data->segment.format == GST_FORMAT_TIME)) {
1317 GstClockTime timestamp = GST_BUFFER_TIMESTAMP (data->buffer);
1319 if (GST_CLOCK_TIME_IS_VALID (timestamp))
1320 gst_segment_set_last_stop (&data->segment, GST_FORMAT_TIME, timestamp);
1323 /* While we have data queued on this pad try to collect stuff */
1325 GST_DEBUG ("Pad %s:%s checking", GST_DEBUG_PAD_NAME (pad));
1326 /* Check if our collected condition is matched and call the collected function
1328 ret = gst_collect_pads_check_collected (pads);
1329 /* when an error occurs, we want to report this back to the caller ASAP
1330 * without having to block if the buffer was not popped */
1331 if (G_UNLIKELY (ret != GST_FLOW_OK))
1334 /* data was consumed, we can exit and accept new data */
1335 if (data->buffer == NULL)
1338 /* Check if we got removed in the mean time, FIXME, this is racy.
1339 * Between this check and the _WAIT, the pad could be removed which will
1340 * makes us hang in the _WAIT. */
1341 GST_OBJECT_LOCK (pad);
1342 if (G_UNLIKELY (gst_pad_get_element_private (pad) == NULL))
1344 GST_OBJECT_UNLOCK (pad);
1346 GST_DEBUG ("Pad %s:%s has a buffer queued, waiting",
1347 GST_DEBUG_PAD_NAME (pad));
1349 /* wait to be collected, this must happen from another thread triggered
1350 * by the _chain function of another pad. We release the lock so we
1351 * can get stopped or flushed as well. We can however not get EOS
1352 * because we still hold the STREAM_LOCK.
1354 GST_COLLECT_PADS_WAIT (pads);
1356 GST_DEBUG ("Pad %s:%s resuming", GST_DEBUG_PAD_NAME (pad));
1358 /* after a signal, we could be stopped */
1359 if (G_UNLIKELY (!pads->started))
1361 /* check if this pad is flushing */
1362 if (G_UNLIKELY (data->abidata.ABI.flushing))
1365 while (data->buffer != NULL);
1368 GST_DEBUG ("Pad %s:%s done", GST_DEBUG_PAD_NAME (pad));
1369 GST_OBJECT_UNLOCK (pads);
1372 gst_buffer_unref (buffer);
1377 GST_WARNING ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
1378 GST_OBJECT_UNLOCK (pad);
1379 ret = GST_FLOW_NOT_LINKED;
1385 GST_DEBUG ("%s got removed from collectpads", GST_OBJECT_NAME (pad));
1386 GST_OBJECT_UNLOCK (pad);
1387 gst_buffer_unref (buffer);
1388 return GST_FLOW_NOT_LINKED;
1392 GST_DEBUG ("not started");
1393 gst_collect_pads_clear (pads, data);
1394 ret = GST_FLOW_WRONG_STATE;
1399 GST_DEBUG ("pad %s:%s is flushing", GST_DEBUG_PAD_NAME (pad));
1400 gst_collect_pads_clear (pads, data);
1401 ret = GST_FLOW_WRONG_STATE;
1406 /* we should not post an error for this, just inform upstream that
1407 * we don't expect anything anymore */
1408 GST_DEBUG ("pad %s:%s is eos", GST_DEBUG_PAD_NAME (pad));
1409 ret = GST_FLOW_UNEXPECTED;
1414 GST_DEBUG ("clipped buffer on pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1420 /* we print the error, the element should post a reasonable error
1421 * message for fatal errors */
1422 GST_DEBUG ("collect failed, reason %d (%s)", ret, gst_flow_get_name (ret));
1423 gst_collect_pads_clear (pads, data);