Blame gst/gstiterator.c

Packit f546b1
/* GStreamer
Packit f546b1
 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
Packit f546b1
 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
Packit f546b1
 *
Packit f546b1
 * gstiterator.h: Base class for iterating datastructures.
Packit f546b1
 *
Packit f546b1
 * This library is free software; you can redistribute it and/or
Packit f546b1
 * modify it under the terms of the GNU Library General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2 of the License, or (at your option) any later version.
Packit f546b1
 *
Packit f546b1
 * This library is distributed in the hope that it will be useful,
Packit f546b1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f546b1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f546b1
 * Library General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Library General Public
Packit f546b1
 * License along with this library; if not, write to the
Packit f546b1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit f546b1
 * Boston, MA 02110-1301, USA.
Packit f546b1
 */
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * SECTION:gstiterator
Packit f546b1
 * @title: GstIterator
Packit f546b1
 * @short_description: Object to retrieve multiple elements in a threadsafe
Packit f546b1
 * way.
Packit f546b1
 * @see_also: #GstElement, #GstBin
Packit f546b1
 *
Packit f546b1
 * A GstIterator is used to retrieve multiple objects from another object in
Packit f546b1
 * a threadsafe way.
Packit f546b1
 *
Packit f546b1
 * Various GStreamer objects provide access to their internal structures using
Packit f546b1
 * an iterator.
Packit f546b1
 *
Packit f546b1
 * Note that if calling a GstIterator function results in your code receiving
Packit f546b1
 * a refcounted object (with, say, g_value_get_object()), the refcount for that
Packit f546b1
 * object will not be increased. Your code is responsible for taking a reference
Packit f546b1
 * if it wants to continue using it later.
Packit f546b1
 *
Packit f546b1
 * The basic use pattern of an iterator is as follows:
Packit f546b1
 * |[
Packit f546b1
 *   GstIterator *it = _get_iterator(object);
Packit f546b1
 *   GValue item = G_VALUE_INIT;
Packit f546b1
 *   done = FALSE;
Packit f546b1
 *   while (!done) {
Packit f546b1
 *     switch (gst_iterator_next (it, &item)) {
Packit f546b1
 *       case GST_ITERATOR_OK:
Packit f546b1
 *         ...get/use/change item here...
Packit f546b1
 *         g_value_reset (&item);
Packit f546b1
 *         break;
Packit f546b1
 *       case GST_ITERATOR_RESYNC:
Packit f546b1
 *         ...rollback changes to items...
Packit f546b1
 *         gst_iterator_resync (it);
Packit f546b1
 *         break;
Packit f546b1
 *       case GST_ITERATOR_ERROR:
Packit f546b1
 *         ...wrong parameters were given...
Packit f546b1
 *         done = TRUE;
Packit f546b1
 *         break;
Packit f546b1
 *       case GST_ITERATOR_DONE:
Packit f546b1
 *         done = TRUE;
Packit f546b1
 *         break;
Packit f546b1
 *     }
Packit f546b1
 *   }
Packit f546b1
 *   g_value_unset (&item);
Packit f546b1
 *   gst_iterator_free (it);
Packit f546b1
 * ]|
Packit f546b1
 */
Packit f546b1
Packit f546b1
#include "gst_private.h"
Packit f546b1
#include <gst/gstiterator.h>
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_copy:
Packit f546b1
 * @it: a #GstIterator
Packit f546b1
 *
Packit f546b1
 * Copy the iterator and its state.
Packit f546b1
 *
Packit f546b1
 * Returns: a new copy of @it.
Packit f546b1
 */
Packit f546b1
GstIterator *
Packit f546b1
gst_iterator_copy (const GstIterator * it)
Packit f546b1
{
Packit f546b1
  GstIterator *copy;
Packit f546b1
Packit f546b1
  copy = g_slice_copy (it->size, it);
Packit f546b1
  if (it->copy)
Packit f546b1
    it->copy (it, copy);
Packit f546b1
Packit f546b1
  return copy;
Packit f546b1
}
Packit f546b1
Packit f546b1
G_DEFINE_BOXED_TYPE (GstIterator, gst_iterator,
Packit f546b1
    (GBoxedCopyFunc) gst_iterator_copy, (GBoxedFreeFunc) gst_iterator_free);
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_iterator_init (GstIterator * it,
Packit f546b1
    guint size,
Packit f546b1
    GType type,
Packit f546b1
    GMutex * lock,
Packit f546b1
    guint32 * master_cookie,
Packit f546b1
    GstIteratorCopyFunction copy,
Packit f546b1
    GstIteratorNextFunction next,
Packit f546b1
    GstIteratorItemFunction item,
Packit f546b1
    GstIteratorResyncFunction resync, GstIteratorFreeFunction free)
Packit f546b1
{
Packit f546b1
  it->size = size;
Packit f546b1
  it->type = type;
Packit f546b1
  it->lock = lock;
Packit f546b1
  it->master_cookie = master_cookie;
Packit f546b1
  it->cookie = *master_cookie;
Packit f546b1
  it->copy = copy;
Packit f546b1
  it->next = next;
Packit f546b1
  it->item = item;
Packit f546b1
  it->resync = resync;
Packit f546b1
  it->free = free;
Packit f546b1
  it->pushed = NULL;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_new: (skip)
Packit f546b1
 * @size: the size of the iterator structure
Packit f546b1
 * @type: #GType of children
Packit f546b1
 * @lock: pointer to a #GMutex.
Packit f546b1
 * @master_cookie: pointer to a guint32 that is changed when the items in the
Packit f546b1
 *    iterator changed.
Packit f546b1
 * @copy: copy function
Packit f546b1
 * @next: function to get next item
Packit f546b1
 * @item: function to call on each item retrieved
Packit f546b1
 * @resync: function to resync the iterator
Packit f546b1
 * @free: function to free the iterator
Packit f546b1
 *
Packit f546b1
 * Create a new iterator. This function is mainly used for objects
Packit f546b1
 * implementing the next/resync/free function to iterate a data structure.
Packit f546b1
 *
Packit f546b1
 * For each item retrieved, the @item function is called with the lock
Packit f546b1
 * held. The @free function is called when the iterator is freed.
Packit f546b1
 *
Packit f546b1
 * Returns: the new #GstIterator.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
GstIterator *
Packit f546b1
gst_iterator_new (guint size,
Packit f546b1
    GType type,
Packit f546b1
    GMutex * lock,
Packit f546b1
    guint32 * master_cookie,
Packit f546b1
    GstIteratorCopyFunction copy,
Packit f546b1
    GstIteratorNextFunction next,
Packit f546b1
    GstIteratorItemFunction item,
Packit f546b1
    GstIteratorResyncFunction resync, GstIteratorFreeFunction free)
Packit f546b1
{
Packit f546b1
  GstIterator *result;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (size >= sizeof (GstIterator), NULL);
Packit f546b1
  g_return_val_if_fail (g_type_qname (type) != 0, NULL);
Packit f546b1
  g_return_val_if_fail (master_cookie != NULL, NULL);
Packit f546b1
  g_return_val_if_fail (next != NULL, NULL);
Packit f546b1
  g_return_val_if_fail (resync != NULL, NULL);
Packit f546b1
  g_return_val_if_fail (free != NULL, NULL);
Packit f546b1
Packit f546b1
  result = g_slice_alloc0 (size);
Packit f546b1
  gst_iterator_init (result, size, type, lock, master_cookie, copy, next, item,
Packit f546b1
      resync, free);
Packit f546b1
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
/*
Packit f546b1
 * list iterator
Packit f546b1
 */
Packit f546b1
typedef struct _GstListIterator
Packit f546b1
{
Packit f546b1
  GstIterator iterator;
Packit f546b1
  GObject *owner;
Packit f546b1
  GList **orig;
Packit f546b1
  GList *list;                  /* pointer in list */
Packit f546b1
Packit f546b1
  void (*set_value) (GValue * value, gpointer item);
Packit f546b1
} GstListIterator;
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_list_iterator_copy (const GstListIterator * it, GstListIterator * copy)
Packit f546b1
{
Packit f546b1
  if (copy->owner)
Packit f546b1
    g_object_ref (copy->owner);
Packit f546b1
}
Packit f546b1
Packit f546b1
static GstIteratorResult
Packit f546b1
gst_list_iterator_next (GstListIterator * it, GValue * elem)
Packit f546b1
{
Packit f546b1
  gpointer data;
Packit f546b1
Packit f546b1
  if (it->list == NULL)
Packit f546b1
    return GST_ITERATOR_DONE;
Packit f546b1
Packit f546b1
  data = it->list->data;
Packit f546b1
  it->list = g_list_next (it->list);
Packit f546b1
Packit f546b1
  it->set_value (elem, data);
Packit f546b1
Packit f546b1
  return GST_ITERATOR_OK;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_list_iterator_resync (GstListIterator * it)
Packit f546b1
{
Packit f546b1
  it->list = *it->orig;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_list_iterator_free (GstListIterator * it)
Packit f546b1
{
Packit f546b1
  if (it->owner)
Packit f546b1
    g_object_unref (it->owner);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_new_list: (skip)
Packit f546b1
 * @type: #GType of elements
Packit f546b1
 * @lock: pointer to a #GMutex protecting the list.
Packit f546b1
 * @master_cookie: pointer to a guint32 that is incremented when the list
Packit f546b1
 *     is changed.
Packit f546b1
 * @list: pointer to the list
Packit f546b1
 * @owner: object owning the list
Packit f546b1
 * @item: function to call on each item retrieved
Packit f546b1
 *
Packit f546b1
 * Create a new iterator designed for iterating @list.
Packit f546b1
 *
Packit f546b1
 * The list you iterate is usually part of a data structure @owner and is
Packit f546b1
 * protected with @lock.
Packit f546b1
 *
Packit f546b1
 * The iterator will use @lock to retrieve the next item of the list and it
Packit f546b1
 * will then call the @item function before releasing @lock again.
Packit f546b1
 *
Packit f546b1
 * When a concurrent update to the list is performed, usually by @owner while
Packit f546b1
 * holding @lock, @master_cookie will be updated. The iterator implementation
Packit f546b1
 * will notice the update of the cookie and will return %GST_ITERATOR_RESYNC to
Packit f546b1
 * the user of the iterator in the next call to gst_iterator_next().
Packit f546b1
 *
Packit f546b1
 * Returns: the new #GstIterator for @list.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
GstIterator *
Packit f546b1
gst_iterator_new_list (GType type,
Packit f546b1
    GMutex * lock, guint32 * master_cookie, GList ** list, GObject * owner,
Packit f546b1
    GstIteratorItemFunction item)
Packit f546b1
{
Packit f546b1
  GstListIterator *result;
Packit f546b1
  gpointer set_value;
Packit f546b1
Packit f546b1
  if (g_type_is_a (type, G_TYPE_OBJECT)) {
Packit f546b1
    set_value = g_value_set_object;
Packit f546b1
  } else if (g_type_is_a (type, G_TYPE_BOXED)) {
Packit f546b1
    set_value = g_value_set_boxed;
Packit f546b1
  } else if (g_type_is_a (type, G_TYPE_POINTER)) {
Packit f546b1
    set_value = g_value_set_pointer;
Packit f546b1
  } else if (g_type_is_a (type, G_TYPE_STRING)) {
Packit f546b1
    set_value = g_value_set_string;
Packit f546b1
  } else {
Packit f546b1
    g_critical ("List iterators can only be created for lists containing "
Packit f546b1
        "instances of GObject, boxed types, pointer types and strings");
Packit f546b1
    return NULL;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  /* no need to lock, nothing can change here */
Packit f546b1
  result = (GstListIterator *) gst_iterator_new (sizeof (GstListIterator),
Packit f546b1
      type,
Packit f546b1
      lock,
Packit f546b1
      master_cookie,
Packit f546b1
      (GstIteratorCopyFunction) gst_list_iterator_copy,
Packit f546b1
      (GstIteratorNextFunction) gst_list_iterator_next,
Packit f546b1
      (GstIteratorItemFunction) item,
Packit f546b1
      (GstIteratorResyncFunction) gst_list_iterator_resync,
Packit f546b1
      (GstIteratorFreeFunction) gst_list_iterator_free);
Packit f546b1
Packit f546b1
  result->owner = owner ? g_object_ref (owner) : NULL;
Packit f546b1
  result->orig = list;
Packit f546b1
  result->list = *list;
Packit f546b1
  result->set_value = set_value;
Packit f546b1
Packit f546b1
  return GST_ITERATOR (result);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_iterator_pop (GstIterator * it)
Packit f546b1
{
Packit f546b1
  if (it->pushed) {
Packit f546b1
    gst_iterator_free (it->pushed);
Packit f546b1
    it->pushed = NULL;
Packit f546b1
  }
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_next:
Packit f546b1
 * @it: The #GstIterator to iterate
Packit f546b1
 * @elem: (out caller-allocates): pointer to hold next element
Packit f546b1
 *
Packit f546b1
 * Get the next item from the iterator in @elem.
Packit f546b1
 *
Packit f546b1
 * Only when this function returns %GST_ITERATOR_OK, @elem will contain a valid
Packit f546b1
 * value. @elem must have been initialized to the type of the iterator or
Packit f546b1
 * initialized to zeroes with g_value_unset(). The caller is responsible for
Packit f546b1
 * unsetting or resetting @elem with g_value_unset() or g_value_reset()
Packit f546b1
 * after usage.
Packit f546b1
 *
Packit f546b1
 * When this function returns %GST_ITERATOR_DONE, no more elements can be
Packit f546b1
 * retrieved from @it.
Packit f546b1
 *
Packit f546b1
 * A return value of %GST_ITERATOR_RESYNC indicates that the element list was
Packit f546b1
 * concurrently updated. The user of @it should call gst_iterator_resync() to
Packit f546b1
 * get the newly updated list.
Packit f546b1
 *
Packit f546b1
 * A return value of %GST_ITERATOR_ERROR indicates an unrecoverable fatal error.
Packit f546b1
 *
Packit f546b1
 * Returns: The result of the iteration. Unset @elem after usage.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
GstIteratorResult
Packit f546b1
gst_iterator_next (GstIterator * it, GValue * elem)
Packit f546b1
{
Packit f546b1
  GstIteratorResult result;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (it != NULL, GST_ITERATOR_ERROR);
Packit f546b1
  g_return_val_if_fail (elem != NULL, GST_ITERATOR_ERROR);
Packit f546b1
  g_return_val_if_fail (G_VALUE_TYPE (elem) == G_TYPE_INVALID
Packit f546b1
      || G_VALUE_HOLDS (elem, it->type), GST_ITERATOR_ERROR);
Packit f546b1
Packit f546b1
  if (G_VALUE_TYPE (elem) == G_TYPE_INVALID)
Packit f546b1
    g_value_init (elem, it->type);
Packit f546b1
Packit f546b1
restart:
Packit f546b1
  if (it->pushed) {
Packit f546b1
    result = gst_iterator_next (it->pushed, elem);
Packit f546b1
    if (result == GST_ITERATOR_DONE) {
Packit f546b1
      /* we are done with this iterator, pop it and
Packit f546b1
       * fallthrough iterating the main iterator again. */
Packit f546b1
      gst_iterator_pop (it);
Packit f546b1
    } else {
Packit f546b1
      return result;
Packit f546b1
    }
Packit f546b1
  }
Packit f546b1
Packit f546b1
  if (G_LIKELY (it->lock))
Packit f546b1
    g_mutex_lock (it->lock);
Packit f546b1
Packit f546b1
  if (G_UNLIKELY (*it->master_cookie != it->cookie)) {
Packit f546b1
    result = GST_ITERATOR_RESYNC;
Packit f546b1
    goto done;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  result = it->next (it, elem);
Packit f546b1
  if (result == GST_ITERATOR_OK && it->item) {
Packit f546b1
    GstIteratorItem itemres;
Packit f546b1
Packit f546b1
    itemres = it->item (it, elem);
Packit f546b1
    switch (itemres) {
Packit f546b1
      case GST_ITERATOR_ITEM_SKIP:
Packit f546b1
        if (G_LIKELY (it->lock))
Packit f546b1
          g_mutex_unlock (it->lock);
Packit f546b1
        g_value_reset (elem);
Packit f546b1
        goto restart;
Packit f546b1
      case GST_ITERATOR_ITEM_END:
Packit f546b1
        result = GST_ITERATOR_DONE;
Packit f546b1
        g_value_reset (elem);
Packit f546b1
        break;
Packit f546b1
      case GST_ITERATOR_ITEM_PASS:
Packit f546b1
        break;
Packit f546b1
    }
Packit f546b1
  }
Packit f546b1
Packit f546b1
done:
Packit f546b1
  if (G_LIKELY (it->lock))
Packit f546b1
    g_mutex_unlock (it->lock);
Packit f546b1
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_resync:
Packit f546b1
 * @it: The #GstIterator to resync
Packit f546b1
 *
Packit f546b1
 * Resync the iterator. this function is mostly called
Packit f546b1
 * after gst_iterator_next() returned %GST_ITERATOR_RESYNC.
Packit f546b1
 *
Packit f546b1
 * When an iterator was pushed on @it, it will automatically be popped again
Packit f546b1
 * with this function.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_iterator_resync (GstIterator * it)
Packit f546b1
{
Packit f546b1
  g_return_if_fail (it != NULL);
Packit f546b1
Packit f546b1
  gst_iterator_pop (it);
Packit f546b1
Packit f546b1
  if (G_LIKELY (it->lock))
Packit f546b1
    g_mutex_lock (it->lock);
Packit f546b1
  it->resync (it);
Packit f546b1
  it->cookie = *it->master_cookie;
Packit f546b1
  if (G_LIKELY (it->lock))
Packit f546b1
    g_mutex_unlock (it->lock);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_free:
Packit f546b1
 * @it: The #GstIterator to free
Packit f546b1
 *
Packit f546b1
 * Free the iterator.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_iterator_free (GstIterator * it)
Packit f546b1
{
Packit f546b1
  g_return_if_fail (it != NULL);
Packit f546b1
Packit f546b1
  gst_iterator_pop (it);
Packit f546b1
Packit f546b1
  it->free (it);
Packit f546b1
Packit f546b1
  g_slice_free1 (it->size, it);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_push:
Packit f546b1
 * @it: The #GstIterator to use
Packit f546b1
 * @other: The #GstIterator to push
Packit f546b1
 *
Packit f546b1
 * Pushes @other iterator onto @it. All calls performed on @it are
Packit f546b1
 * forwarded to @other. If @other returns %GST_ITERATOR_DONE, it is
Packit f546b1
 * popped again and calls are handled by @it again.
Packit f546b1
 *
Packit f546b1
 * This function is mainly used by objects implementing the iterator
Packit f546b1
 * next function to recurse into substructures.
Packit f546b1
 *
Packit f546b1
 * When gst_iterator_resync() is called on @it, @other will automatically be
Packit f546b1
 * popped.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_iterator_push (GstIterator * it, GstIterator * other)
Packit f546b1
{
Packit f546b1
  g_return_if_fail (it != NULL);
Packit f546b1
  g_return_if_fail (other != NULL);
Packit f546b1
Packit f546b1
  it->pushed = other;
Packit f546b1
}
Packit f546b1
Packit f546b1
typedef struct _GstIteratorFilter
Packit f546b1
{
Packit f546b1
  GstIterator iterator;
Packit f546b1
  GstIterator *slave;
Packit f546b1
Packit f546b1
  GMutex *master_lock;
Packit f546b1
  GCompareFunc func;
Packit f546b1
  GValue user_data;
Packit f546b1
  gboolean have_user_data;
Packit f546b1
} GstIteratorFilter;
Packit f546b1
Packit f546b1
static GstIteratorResult
Packit f546b1
filter_next (GstIteratorFilter * it, GValue * elem)
Packit f546b1
{
Packit f546b1
  GstIteratorResult result = GST_ITERATOR_ERROR;
Packit f546b1
  gboolean done = FALSE;
Packit f546b1
  GValue item = { 0, };
Packit f546b1
Packit f546b1
  while (G_LIKELY (!done)) {
Packit f546b1
    result = gst_iterator_next (it->slave, &item);
Packit f546b1
    switch (result) {
Packit f546b1
      case GST_ITERATOR_OK:
Packit f546b1
        if (G_LIKELY (it->master_lock))
Packit f546b1
          g_mutex_unlock (it->master_lock);
Packit f546b1
        if (it->func (&item, &it->user_data) == 0) {
Packit f546b1
          g_value_copy (&item, elem);
Packit f546b1
          done = TRUE;
Packit f546b1
        }
Packit f546b1
        g_value_reset (&item);
Packit f546b1
        if (G_LIKELY (it->master_lock))
Packit f546b1
          g_mutex_lock (it->master_lock);
Packit f546b1
        break;
Packit f546b1
      case GST_ITERATOR_RESYNC:
Packit f546b1
      case GST_ITERATOR_DONE:
Packit f546b1
        done = TRUE;
Packit f546b1
        break;
Packit f546b1
      default:
Packit f546b1
        g_assert_not_reached ();
Packit f546b1
        break;
Packit f546b1
    }
Packit f546b1
  }
Packit f546b1
  g_value_unset (&item);
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
filter_copy (const GstIteratorFilter * it, GstIteratorFilter * copy)
Packit f546b1
{
Packit f546b1
  copy->slave = gst_iterator_copy (it->slave);
Packit f546b1
  copy->master_lock = copy->slave->lock ? copy->slave->lock : it->master_lock;
Packit f546b1
  copy->slave->lock = NULL;
Packit f546b1
Packit f546b1
  if (it->have_user_data) {
Packit f546b1
    memset (&copy->user_data, 0, sizeof (copy->user_data));
Packit f546b1
    g_value_init (&copy->user_data, G_VALUE_TYPE (&it->user_data));
Packit f546b1
    g_value_copy (&it->user_data, &copy->user_data);
Packit f546b1
  }
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
filter_resync (GstIteratorFilter * it)
Packit f546b1
{
Packit f546b1
  gst_iterator_resync (it->slave);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
filter_free (GstIteratorFilter * it)
Packit f546b1
{
Packit f546b1
  if (it->have_user_data)
Packit f546b1
    g_value_unset (&it->user_data);
Packit f546b1
  gst_iterator_free (it->slave);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_filter:
Packit f546b1
 * @it: The #GstIterator to filter
Packit f546b1
 * @func: (scope call): the compare function to select elements
Packit f546b1
 * @user_data: (closure): user data passed to the compare function
Packit f546b1
 *
Packit f546b1
 * Create a new iterator from an existing iterator. The new iterator
Packit f546b1
 * will only return those elements that match the given compare function @func.
Packit f546b1
 * The first parameter that is passed to @func is the #GValue of the current
Packit f546b1
 * iterator element and the second parameter is @user_data. @func should
Packit f546b1
 * return 0 for elements that should be included in the filtered iterator.
Packit f546b1
 *
Packit f546b1
 * When this iterator is freed, @it will also be freed.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full): a new #GstIterator.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
GstIterator *
Packit f546b1
gst_iterator_filter (GstIterator * it, GCompareFunc func,
Packit f546b1
    const GValue * user_data)
Packit f546b1
{
Packit f546b1
  GstIteratorFilter *result;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (it != NULL, NULL);
Packit f546b1
  g_return_val_if_fail (func != NULL, NULL);
Packit f546b1
Packit f546b1
  result = (GstIteratorFilter *) gst_iterator_new (sizeof (GstIteratorFilter),
Packit f546b1
      it->type, it->lock, it->master_cookie,
Packit f546b1
      (GstIteratorCopyFunction) filter_copy,
Packit f546b1
      (GstIteratorNextFunction) filter_next,
Packit f546b1
      (GstIteratorItemFunction) NULL,
Packit f546b1
      (GstIteratorResyncFunction) filter_resync,
Packit f546b1
      (GstIteratorFreeFunction) filter_free);
Packit f546b1
Packit f546b1
  result->master_lock = it->lock;
Packit f546b1
  it->lock = NULL;
Packit f546b1
  result->func = func;
Packit f546b1
  if (user_data) {
Packit f546b1
    g_value_init (&result->user_data, G_VALUE_TYPE (user_data));
Packit f546b1
    g_value_copy (user_data, &result->user_data);
Packit f546b1
    result->have_user_data = TRUE;
Packit f546b1
  } else {
Packit f546b1
    result->have_user_data = FALSE;
Packit f546b1
  }
Packit f546b1
  result->slave = it;
Packit f546b1
Packit f546b1
  return GST_ITERATOR (result);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_fold:
Packit f546b1
 * @it: The #GstIterator to fold over
Packit f546b1
 * @func: (scope call): the fold function
Packit f546b1
 * @ret: the seed value passed to the fold function
Packit f546b1
 * @user_data: (closure): user data passed to the fold function
Packit f546b1
 *
Packit f546b1
 * Folds @func over the elements of @iter. That is to say, @func will be called
Packit f546b1
 * as @func (object, @ret, @user_data) for each object in @it. The normal use
Packit f546b1
 * of this procedure is to accumulate the results of operating on the objects in
Packit f546b1
 * @ret.
Packit f546b1
 *
Packit f546b1
 * This procedure can be used (and is used internally) to implement the
Packit f546b1
 * gst_iterator_foreach() and gst_iterator_find_custom() operations.
Packit f546b1
 *
Packit f546b1
 * The fold will proceed as long as @func returns %TRUE. When the iterator has no
Packit f546b1
 * more arguments, %GST_ITERATOR_DONE will be returned. If @func returns %FALSE,
Packit f546b1
 * the fold will stop, and %GST_ITERATOR_OK will be returned. Errors or resyncs
Packit f546b1
 * will cause fold to return %GST_ITERATOR_ERROR or %GST_ITERATOR_RESYNC as
Packit f546b1
 * appropriate.
Packit f546b1
 *
Packit f546b1
 * The iterator will not be freed.
Packit f546b1
 *
Packit f546b1
 * Returns: A #GstIteratorResult, as described above.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
GstIteratorResult
Packit f546b1
gst_iterator_fold (GstIterator * it, GstIteratorFoldFunction func,
Packit f546b1
    GValue * ret, gpointer user_data)
Packit f546b1
{
Packit f546b1
  GValue item = { 0, };
Packit f546b1
  GstIteratorResult result;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (it != NULL, GST_ITERATOR_ERROR);
Packit f546b1
Packit f546b1
  while (1) {
Packit f546b1
    result = gst_iterator_next (it, &item);
Packit f546b1
    switch (result) {
Packit f546b1
      case GST_ITERATOR_OK:
Packit f546b1
        if (!func (&item, ret, user_data))
Packit f546b1
          goto fold_done;
Packit f546b1
Packit f546b1
        g_value_reset (&item);
Packit f546b1
        break;
Packit f546b1
      case GST_ITERATOR_RESYNC:
Packit f546b1
      case GST_ITERATOR_ERROR:
Packit f546b1
        goto fold_done;
Packit f546b1
      case GST_ITERATOR_DONE:
Packit f546b1
        goto fold_done;
Packit f546b1
    }
Packit f546b1
  }
Packit f546b1
Packit f546b1
fold_done:
Packit f546b1
Packit f546b1
#if GLIB_CHECK_VERSION (2, 48, 0)
Packit f546b1
  g_value_unset (&item);
Packit f546b1
#else
Packit f546b1
  if (item.g_type != 0)
Packit f546b1
    g_value_unset (&item);
Packit f546b1
#endif
Packit f546b1
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
typedef struct
Packit f546b1
{
Packit f546b1
  GstIteratorForeachFunction func;
Packit f546b1
  gpointer user_data;
Packit f546b1
} ForeachFoldData;
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
foreach_fold_func (const GValue * item, GValue * unused, ForeachFoldData * data)
Packit f546b1
{
Packit f546b1
  data->func (item, data->user_data);
Packit f546b1
  return TRUE;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_foreach:
Packit f546b1
 * @it: The #GstIterator to iterate
Packit f546b1
 * @func: (scope call): the function to call for each element.
Packit f546b1
 * @user_data: (closure): user data passed to the function
Packit f546b1
 *
Packit f546b1
 * Iterate over all element of @it and call the given function @func for
Packit f546b1
 * each element.
Packit f546b1
 *
Packit f546b1
 * Returns: the result call to gst_iterator_fold(). The iterator will not be
Packit f546b1
 * freed.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
GstIteratorResult
Packit f546b1
gst_iterator_foreach (GstIterator * it, GstIteratorForeachFunction func,
Packit f546b1
    gpointer user_data)
Packit f546b1
{
Packit f546b1
  ForeachFoldData data;
Packit f546b1
Packit f546b1
  data.func = func;
Packit f546b1
  data.user_data = user_data;
Packit f546b1
Packit f546b1
  return gst_iterator_fold (it, (GstIteratorFoldFunction) foreach_fold_func,
Packit f546b1
      NULL, &data);
Packit f546b1
}
Packit f546b1
Packit f546b1
typedef struct
Packit f546b1
{
Packit f546b1
  GCompareFunc func;
Packit f546b1
  gpointer user_data;
Packit f546b1
  gboolean found;
Packit f546b1
} FindCustomFoldData;
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
find_custom_fold_func (const GValue * item, GValue * ret,
Packit f546b1
    FindCustomFoldData * data)
Packit f546b1
{
Packit f546b1
  if (data->func (item, data->user_data) == 0) {
Packit f546b1
    data->found = TRUE;
Packit f546b1
    g_value_copy (item, ret);
Packit f546b1
    return FALSE;
Packit f546b1
  } else {
Packit f546b1
    return TRUE;
Packit f546b1
  }
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_find_custom:
Packit f546b1
 * @it: The #GstIterator to iterate
Packit f546b1
 * @func: (scope call): the compare function to use
Packit f546b1
 * @elem: (out): pointer to a #GValue where to store the result
Packit f546b1
 * @user_data: (closure): user data passed to the compare function
Packit f546b1
 *
Packit f546b1
 * Find the first element in @it that matches the compare function @func.
Packit f546b1
 * @func should return 0 when the element is found. The first parameter
Packit f546b1
 * to @func will be the current element of the iterator and the
Packit f546b1
 * second parameter will be @user_data.
Packit f546b1
 * The result will be stored in @elem if a result is found.
Packit f546b1
 *
Packit f546b1
 * The iterator will not be freed.
Packit f546b1
 *
Packit f546b1
 * This function will return %FALSE if an error happened to the iterator
Packit f546b1
 * or if the element wasn't found.
Packit f546b1
 *
Packit f546b1
 * Returns: Returns %TRUE if the element was found, else %FALSE.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
gboolean
Packit f546b1
gst_iterator_find_custom (GstIterator * it, GCompareFunc func,
Packit f546b1
    GValue * elem, gpointer user_data)
Packit f546b1
{
Packit f546b1
  GstIteratorResult res;
Packit f546b1
  FindCustomFoldData data;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (G_VALUE_TYPE (elem) == G_TYPE_INVALID
Packit f546b1
      || G_VALUE_HOLDS (elem, it->type), GST_ITERATOR_ERROR);
Packit f546b1
Packit f546b1
  if (G_VALUE_TYPE (elem) == G_TYPE_INVALID)
Packit f546b1
    g_value_init (elem, it->type);
Packit f546b1
Packit f546b1
  data.func = func;
Packit f546b1
  data.user_data = user_data;
Packit f546b1
  data.found = FALSE;
Packit f546b1
Packit f546b1
  do {
Packit f546b1
    res =
Packit f546b1
        gst_iterator_fold (it, (GstIteratorFoldFunction) find_custom_fold_func,
Packit f546b1
        elem, &data);
Packit f546b1
    if (res == GST_ITERATOR_RESYNC)
Packit f546b1
      gst_iterator_resync (it);
Packit f546b1
  } while (res == GST_ITERATOR_RESYNC);
Packit f546b1
Packit f546b1
  if (!data.found)
Packit f546b1
    g_value_unset (elem);
Packit f546b1
Packit f546b1
  return data.found;
Packit f546b1
}
Packit f546b1
Packit f546b1
typedef struct
Packit f546b1
{
Packit f546b1
  GstIterator parent;
Packit f546b1
  GValue object;
Packit f546b1
  gboolean visited;
Packit f546b1
  gboolean empty;
Packit f546b1
} GstSingleObjectIterator;
Packit f546b1
Packit f546b1
static guint32 _single_object_dummy_cookie = 0;
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_single_object_iterator_copy (const GstSingleObjectIterator * it,
Packit f546b1
    GstSingleObjectIterator * copy)
Packit f546b1
{
Packit f546b1
  if (!it->empty) {
Packit f546b1
    memset (&copy->object, 0, sizeof (copy->object));
Packit f546b1
    g_value_init (&copy->object, it->parent.type);
Packit f546b1
    g_value_copy (&it->object, &copy->object);
Packit f546b1
  }
Packit f546b1
}
Packit f546b1
Packit f546b1
static GstIteratorResult
Packit f546b1
gst_single_object_iterator_next (GstSingleObjectIterator * it, GValue * result)
Packit f546b1
{
Packit f546b1
  if (it->visited || it->empty)
Packit f546b1
    return GST_ITERATOR_DONE;
Packit f546b1
Packit f546b1
  g_value_copy (&it->object, result);
Packit f546b1
  it->visited = TRUE;
Packit f546b1
Packit f546b1
  return GST_ITERATOR_OK;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_single_object_iterator_resync (GstSingleObjectIterator * it)
Packit f546b1
{
Packit f546b1
  it->visited = FALSE;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_single_object_iterator_free (GstSingleObjectIterator * it)
Packit f546b1
{
Packit f546b1
  if (!it->empty)
Packit f546b1
    g_value_unset (&it->object);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_iterator_new_single:
Packit f546b1
 * @type: #GType of the passed object
Packit f546b1
 * @object: object that this iterator should return
Packit f546b1
 *
Packit f546b1
 * This #GstIterator is a convenient iterator for the common
Packit f546b1
 * case where a #GstIterator needs to be returned but only
Packit f546b1
 * a single object has to be considered. This happens often
Packit f546b1
 * for the #GstPadIterIntLinkFunction.
Packit f546b1
 *
Packit f546b1
 * Returns: the new #GstIterator for @object.
Packit f546b1
 */
Packit f546b1
GstIterator *
Packit f546b1
gst_iterator_new_single (GType type, const GValue * object)
Packit f546b1
{
Packit f546b1
  GstSingleObjectIterator *result;
Packit f546b1
Packit f546b1
  result = (GstSingleObjectIterator *)
Packit f546b1
      gst_iterator_new (sizeof (GstSingleObjectIterator),
Packit f546b1
      type, NULL, &_single_object_dummy_cookie,
Packit f546b1
      (GstIteratorCopyFunction) gst_single_object_iterator_copy,
Packit f546b1
      (GstIteratorNextFunction) gst_single_object_iterator_next,
Packit f546b1
      (GstIteratorItemFunction) NULL,
Packit f546b1
      (GstIteratorResyncFunction) gst_single_object_iterator_resync,
Packit f546b1
      (GstIteratorFreeFunction) gst_single_object_iterator_free);
Packit f546b1
Packit f546b1
  if (object) {
Packit f546b1
    g_value_init (&result->object, type);
Packit f546b1
    g_value_copy (object, &result->object);
Packit f546b1
    result->empty = FALSE;
Packit f546b1
  } else {
Packit f546b1
    result->empty = TRUE;
Packit f546b1
  }
Packit f546b1
  result->visited = FALSE;
Packit f546b1
Packit f546b1
  return GST_ITERATOR (result);
Packit f546b1
}