Index: libs/gst/base/gstsafequeue.c
===================================================================
RCS file: libs/gst/base/gstsafequeue.c
diff -N libs/gst/base/gstsafequeue.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libs/gst/base/gstsafequeue.c	3 Nov 2006 16:01:34 -0000
@@ -0,0 +1,752 @@
+/* GStreamer
+ * Copyright (C) 2006 Edward Hervey <edward@fluendo.com>
+ *
+ * gstsafequeue.c:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <gst/gst.h>
+#include "gstsafequeue.h"
+
+GST_DEBUG_CATEGORY_STATIC (safe_queue_debug);
+#define GST_CAT_DEFAULT (safe_queue_debug)
+GST_DEBUG_CATEGORY_STATIC (safe_queue_dataflow);
+
+typedef struct _GstSafeQueueItem GstSafeQueueItem;
+
+struct _GstSafeQueueItem {
+  GstMiniObject		*object;
+  guint			size;
+  guint64		duration;
+  gboolean		last;
+  gboolean		visible;
+
+  /* user supplied data and destroy function */
+  gpointer		udata;
+  GDestroyNotify	udestroy;  
+};
+
+/* Queue signals and args */
+enum
+{
+  SIGNAL_EMPTY,
+  SIGNAL_RUNNING,
+  SIGNAL_FULL,
+  LAST_SIGNAL
+};
+
+enum
+{
+  ARG_0,
+  /* FIXME: don't we have another way of doing this
+   * "Gstreamer format" (frame/byte/time) queries? */
+  ARG_CUR_LEVEL_VISIBLE,
+  ARG_CUR_LEVEL_BYTES,
+  ARG_CUR_LEVEL_TIME,
+  ARG_MAX_SIZE_VISIBLE,
+  ARG_MAX_SIZE_BYTES,
+  ARG_MAX_SIZE_TIME,
+  ARG_MIN_THRESHOLD_VISIBLE,
+  ARG_MIN_THRESHOLD_BYTES,
+  ARG_MIN_THRESHOLD_TIME,
+  ARG_LEAKY,
+  /* FILL ME */
+};
+
+#define GST_SAFE_QUEUE_MUTEX_LOCK(q) G_STMT_START {                     \
+  GST_CAT_LOG (safe_queue_dataflow,		                                \
+      "locking qlock from thread %p",                                   \
+      g_thread_self ());                                                \
+  g_mutex_lock (q->qlock);                                              \
+  GST_CAT_LOG (safe_queue_dataflow,		                                \
+      "locked qlock from thread %p",                                    \
+      g_thread_self ());                                                \
+} G_STMT_END
+
+#define GST_SAFE_QUEUE_MUTEX_LOCK_CHECK(q, label) G_STMT_START {	\
+    GST_SAFE_QUEUE_MUTEX_LOCK (q);						\
+    if (q->flushing)							\
+      goto label;							\
+  } G_STMT_END
+
+#define GST_SAFE_QUEUE_MUTEX_UNLOCK(q) G_STMT_START {                   \
+  GST_CAT_LOG (safe_queue_dataflow,						\
+      "unlocking qlock from thread %p",                                 \
+      g_thread_self ());                                                \
+  g_mutex_unlock (q->qlock);                                            \
+} G_STMT_END
+
+#define STATUS(q, msg)							\
+  GST_CAT_LOG (safe_queue_dataflow, \
+	       "queue:%p " msg ": %u of %u-%u visible items, %u of %u-%u" \
+	       "bytes, %"G_GUINT64_FORMAT" of %"G_GUINT64_FORMAT"-%"	\
+	       G_GUINT64_FORMAT " ns, %u elements",			\
+	       queue, \
+	       q->cur_level.visible, \
+	       q->min_threshold.visible, \
+	       q->max_size.visible, \
+	       q->cur_level.bytes, \
+	       q->min_threshold.bytes, \
+	       q->max_size.bytes, \
+	       q->cur_level.time, \
+	       q->min_threshold.time, \
+	       q->max_size.time, \
+	       q->queue->length)
+
+static void gst_safe_queue_base_init (GstSafeQueueClass * klass);
+static void gst_safe_queue_class_init (GstSafeQueueClass * klass);
+static void gst_safe_queue_init (GstSafeQueue * queue);
+static void gst_safe_queue_finalize (GObject * object);
+
+static void gst_safe_queue_set_property (GObject * object,
+    guint prop_id, const GValue * value, GParamSpec * pspec);
+static void gst_safe_queue_get_property (GObject * object,
+    guint prop_id, GValue * value, GParamSpec * pspec);
+
+static GObjectClass *parent_class = NULL;
+static guint gst_safe_queue_signals[LAST_SIGNAL] = { 0 };
+
+GType
+gst_safe_queue_get_type (void)
+{
+  static GType queue_type = 0;
+
+  if (!queue_type) {
+    static const GTypeInfo queue_info = {
+      sizeof (GstSafeQueueClass),
+      (GBaseInitFunc) gst_safe_queue_base_init,
+      NULL,
+      (GClassInitFunc) gst_safe_queue_class_init,
+      NULL,
+      NULL,
+      sizeof (GstSafeQueue),
+      0,
+      (GInstanceInitFunc) gst_safe_queue_init,
+      NULL
+    };
+
+    queue_type = g_type_register_static (G_TYPE_OBJECT,
+        "GstSafeQueue", &queue_info, 0);
+    GST_DEBUG_CATEGORY_INIT (safe_queue_debug, "safequeue", 0, "safe queue object");
+    GST_DEBUG_CATEGORY_INIT (safe_queue_dataflow, "safe_queue_dataflow", 0,
+        "dataflow inside the safe queue object");
+  }
+
+  return queue_type;
+}
+
+static void
+gst_safe_queue_base_init (GstSafeQueueClass * klass)
+{
+  /* Do we need anything here ?? */
+  return;
+}
+
+static void
+gst_safe_queue_class_init (GstSafeQueueClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  parent_class = g_type_class_peek_parent (klass);
+
+  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_safe_queue_set_property);
+  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_safe_queue_get_property);
+
+  /* signals */
+  /**
+   * GstSafeQueue::empty:
+   * @queue: the queue instance
+   *
+   * Reports that the queue became empty (empty).
+   * A queue is empty if the total amount of visible items inside it (num-visible, time,
+   * size) is lower than the boundary values which can be set through the GObject
+   * properties.
+   */
+  gst_safe_queue_signals[SIGNAL_EMPTY] =
+      g_signal_new ("empty", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
+      G_STRUCT_OFFSET (GstSafeQueueClass, empty), NULL, NULL,
+      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
+
+  /**
+   * GstSafeQueue::running:
+   * @queue: the queue instance
+   *
+   * Reports that enough (min-threshold) data is in the queue. Use this signal
+   * together with the underrun signal to pause the pipeline on underrun and wait
+   * for the queue to fill-up before resume playback.
+   */
+  gst_safe_queue_signals[SIGNAL_RUNNING] =
+      g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
+      G_STRUCT_OFFSET (GstSafeQueueClass, running), NULL, NULL,
+      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
+
+  /**
+   * GstSafeQueue::full:
+   * @queue: the queue instance
+   *
+   * Reports that the queue became full (full).
+   * A queue is full if the total amount of data inside it (num-visible, time,
+   * size) is higher than the boundary values which can be set through the GObject
+   * properties.
+   */
+  gst_safe_queue_signals[SIGNAL_FULL] =
+      g_signal_new ("full", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
+      G_STRUCT_OFFSET (GstSafeQueueClass, full), NULL, NULL,
+      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
+
+  /* properties */
+  g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_BYTES,
+      g_param_spec_uint ("current-level-bytes", "Current level (kB)",
+          "Current amount of data in the queue (bytes)",
+          0, G_MAXUINT, 0, G_PARAM_READABLE));
+  g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_VISIBLE,
+      g_param_spec_uint ("current-level-visible", "Current level (visible items)",
+          "Current number of visible items in the queue",
+          0, G_MAXUINT, 0, G_PARAM_READABLE));
+  g_object_class_install_property (gobject_class, ARG_CUR_LEVEL_TIME,
+      g_param_spec_uint64 ("current-level-time", "Current level (ns)",
+          "Current amount of data in the queue (in ns)",
+          0, G_MAXUINT64, 0, G_PARAM_READABLE));
+
+  g_object_class_install_property (gobject_class, ARG_MAX_SIZE_BYTES,
+      g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
+          "Max. amount of data in the queue (bytes, 0=disable)",
+          0, G_MAXUINT, 0, G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, ARG_MAX_SIZE_VISIBLE,
+      g_param_spec_uint ("max-size-visible", "Max. size (visible items)",
+          "Max. number of visible items in the queue (0=disable)",
+          0, G_MAXUINT, 0, G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, ARG_MAX_SIZE_TIME,
+      g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
+          "Max. amount of data in the queue (in ns, 0=disable)",
+          0, G_MAXUINT64, 0, G_PARAM_READWRITE));
+
+  g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_BYTES,
+      g_param_spec_uint ("min-threshold-bytes", "Min. threshold (kB)",
+          "Min. amount of data in the queue to allow reading (bytes, 0=disable)",
+          0, G_MAXUINT, 0, G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_VISIBLE,
+      g_param_spec_uint ("min-threshold-visible", "Min. threshold (visible items)",
+          "Min. number of visible items in the queue to allow reading (0=disable)",
+          0, G_MAXUINT, 0, G_PARAM_READWRITE));
+  g_object_class_install_property (gobject_class, ARG_MIN_THRESHOLD_TIME,
+      g_param_spec_uint64 ("min-threshold-time", "Min. threshold (ns)",
+          "Min. amount of data in the queue to allow reading (in ns, 0=disable)",
+          0, G_MAXUINT64, 0, G_PARAM_READWRITE));
+
+  /* set several parent class virtual functions */
+  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_safe_queue_finalize);
+
+}
+
+static void
+gst_safe_queue_init (GstSafeQueue * queue)
+{
+  queue->cur_level.visible = 0; /* no content */
+  queue->cur_level.bytes = 0;   /* no content */
+  queue->cur_level.time = 0;    /* no content */
+  queue->max_size.visible = 200;        /* 200 visible */
+  queue->max_size.bytes = 10 * 1024 * 1024;     /* 10 MB */
+  queue->max_size.time = GST_SECOND;    /* 1 s. */
+  queue->min_threshold.visible = 0;     /* no threshold */
+  queue->min_threshold.bytes = 0;       /* no threshold */
+  queue->min_threshold.time = 0;        /* no threshold */
+  queue->orig_min_threshold.visible = 0;
+  queue->orig_min_threshold.bytes = 0;
+  queue->orig_min_threshold.time = 0;
+
+  queue->qlock = g_mutex_new ();
+  queue->item_add = g_cond_new ();
+  queue->item_del = g_cond_new ();
+  queue->queue = g_queue_new ();
+
+  GST_DEBUG ("initialized queue's not_empty & not_full conditions");
+}
+
+GstSafeQueue *
+gst_safe_queue_new (void)
+{
+  return g_object_new (GST_TYPE_SAFE_QUEUE, NULL);
+}
+
+/* called only once, as opposed to dispose */
+static void
+gst_safe_queue_finalize (GObject * object)
+{
+  GstSafeQueue *queue = GST_SAFE_QUEUE (object);
+
+  GST_DEBUG ("finalizing queue");
+
+  while (!g_queue_is_empty (queue->queue)) {
+    GstSafeQueueItem *item = g_queue_pop_head (queue->queue);
+
+    gst_mini_object_unref (item->object);
+    if (G_UNLIKELY (item->udata && item->udestroy))
+      item->udestroy(item->udata);
+    
+    g_slice_free (GstSafeQueueItem, item);
+  }
+
+  g_queue_free (queue->queue);
+  GST_DEBUG ("free mutex");
+  g_mutex_free (queue->qlock);
+  GST_DEBUG ("done free mutex");
+  g_cond_free (queue->item_add);
+  g_cond_free (queue->item_del);
+
+  if (G_OBJECT_CLASS (parent_class)->finalize)
+    G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gst_safe_queue_locked_flush (GstSafeQueue * queue)
+{
+
+  STATUS (queue, "before flushing");
+
+  while (!g_queue_is_empty (queue->queue)) {
+    GstSafeQueueItem *item = g_queue_pop_head (queue->queue);
+
+    gst_mini_object_unref (item->object);
+    if (G_UNLIKELY (item->udata && item->udestroy))
+      item->udestroy(item->udata);
+    
+    g_slice_free (GstSafeQueueItem, item);
+  }
+  queue->cur_level.visible = 0;
+  queue->cur_level.bytes = 0;
+  queue->cur_level.time = 0;
+  queue->min_threshold.visible = queue->orig_min_threshold.visible;
+  queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
+  queue->min_threshold.time = queue->orig_min_threshold.time;
+
+  STATUS (queue, "after flushing");
+  /* we deleted something... */
+  g_cond_signal (queue->item_del);
+}
+
+static gboolean
+gst_safe_queue_locked_is_empty (GstSafeQueue * queue)
+{
+  return (queue->queue->length == 0 ||
+      (queue->min_threshold.visible > 0 &&
+          queue->cur_level.visible < queue->min_threshold.visible) ||
+      (queue->min_threshold.bytes > 0 &&
+          queue->cur_level.bytes < queue->min_threshold.bytes) ||
+      (queue->min_threshold.time > 0 &&
+          queue->cur_level.time < queue->min_threshold.time));
+}
+
+static gboolean
+gst_safe_queue_locked_is_full (GstSafeQueue * queue)
+{
+  return (((queue->max_size.visible > 0 &&
+              queue->cur_level.visible >= queue->max_size.visible) ||
+          (queue->max_size.bytes > 0 &&
+              queue->cur_level.bytes >= queue->max_size.bytes) ||
+          (queue->max_size.time > 0 &&
+              queue->cur_level.time >= queue->max_size.time)));
+}
+
+/**
+ * gst_safe_queue_flush:
+ * @queue: The #GstSafeQueue to flush.
+ *
+ * Flushes all the contents of the @queue.
+ * MT safe.
+ */
+
+void gst_safe_queue_flush (GstSafeQueue * queue)
+{
+  GST_DEBUG ("queue:%p", queue);
+  GST_SAFE_QUEUE_MUTEX_LOCK (queue);
+  gst_safe_queue_locked_flush (queue);
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+}
+
+gboolean
+gst_safe_queue_is_empty (GstSafeQueue * queue)
+{
+  gboolean res;
+
+  GST_SAFE_QUEUE_MUTEX_LOCK (queue);
+  res = gst_safe_queue_locked_is_empty (queue);
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+
+  return res;
+}
+
+gboolean
+gst_safe_queue_is_full (GstSafeQueue * queue)
+{
+  gboolean res;
+
+  GST_SAFE_QUEUE_MUTEX_LOCK (queue);
+  res = gst_safe_queue_locked_is_full (queue);
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+
+  return res;
+}
+
+void
+gst_safe_queue_set_flushing (GstSafeQueue * queue, gboolean flushing)
+{
+
+  GST_DEBUG ("queue:%p , flushing:%d", queue, flushing);
+  GST_SAFE_QUEUE_MUTEX_LOCK (queue);
+  queue->flushing = flushing;
+  if (flushing) {
+    /* release push/pop functions */
+    g_cond_signal (queue->item_add);
+    g_cond_signal (queue->item_del);
+  }
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+}
+
+/**
+ * gst_safe_queue_push:
+ * @queue: The #GstSafeQueue to add an object to.
+ * @object: The #GstMiniObject to add.
+ *
+ * Adds @object to the tail of the queue, updating the current levels at the
+ * same time. If @object is a #GstBuffer, the size, time and items levels will
+ * be updated.
+ * This method is blocking and MT safe.
+ *
+ * Returns: TRUE if @object was properly added to the queue.
+ */
+
+gboolean
+gst_safe_queue_push_full (GstSafeQueue * queue, GstMiniObject * object,
+			  guint size, guint64 duration, gboolean last,
+			  gboolean visible, gpointer udata,
+			  GDestroyNotify udestroy)
+{
+  gboolean res = FALSE;
+  GstSafeQueueItem * item;
+  
+  GST_DEBUG ("queue:%p , object:%p, size:%d, duration:%"GST_TIME_FORMAT
+	     ", last:%d, visible:%d, udata:%p, udestroy:%p",
+	     queue, object, size, GST_TIME_ARGS(duration), last, visible,
+	     udata, udestroy);
+
+  GST_SAFE_QUEUE_MUTEX_LOCK_CHECK (queue, done);
+
+  /* Only need to check for size if it's a visible with size */
+  STATUS (queue, "before pushing");
+  if (visible && size) {
+    while (gst_safe_queue_locked_is_full (queue)) {
+      GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+      g_signal_emit (G_OBJECT (queue), gst_safe_queue_signals[SIGNAL_FULL], 0);
+      GST_SAFE_QUEUE_MUTEX_LOCK_CHECK (queue, done);
+
+      /* signal might have removed some items */
+      while (gst_safe_queue_locked_is_full (queue)) {
+	g_cond_wait (queue->item_del, queue->qlock);
+	if (queue->flushing)
+	  goto done;
+      }
+      GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+      g_signal_emit (G_OBJECT (queue), gst_safe_queue_signals[SIGNAL_RUNNING], 0);
+      GST_SAFE_QUEUE_MUTEX_LOCK_CHECK (queue, done);
+    }
+  } else if (last) {
+    /* Zero the thresholds, this makes sure the queue is completely
+     * filled and we can read all data from the queue. */
+    queue->min_threshold.visible = 0;
+    queue->min_threshold.bytes = 0;
+    queue->min_threshold.time = 0;
+  }
+
+  item = g_slice_new(GstSafeQueueItem);
+  item->object = object;
+  item->size = size;
+  item->duration = GST_CLOCK_TIME_IS_VALID (duration) ? duration : 0;
+  item->last = last;
+  item->visible = visible;
+  item->udata = udata;
+  item->udestroy = udestroy;
+
+  g_queue_push_tail (queue->queue, item);
+  res = TRUE;
+
+  if (visible)
+    queue->cur_level.visible++;
+  queue->cur_level.bytes += size;
+  queue->cur_level.time += item->duration;
+
+  STATUS (queue, "after pushing");
+
+  g_cond_signal (queue->item_add);
+
+ done:
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);  
+
+  GST_DEBUG ("queue:%p, result:%d", queue, res);
+
+  return res;
+}
+
+gboolean
+gst_safe_queue_push (GstSafeQueue * queue, GstMiniObject * object,
+		     guint size, guint64 duration, gboolean last,
+		     gboolean visible)
+{
+  return gst_safe_queue_push_full (queue, object, size, duration, last, visible, NULL, NULL);
+}
+
+/**
+ * gst_safe_queue_pop:
+ * @queue: The #GstSafeQueue to get an object from.
+ * @object: The storage for the returned #GstMiniObject.
+ *
+ * Retrives the #GstMiniObject located at the head of the queue.
+ * This method is blocking and MT safe.
+ *
+ * Returns: TRUE if a #GstMiniObject could be retrieved from the queue.
+ */
+
+gboolean
+gst_safe_queue_pop_full (GstSafeQueue * queue, GstMiniObject ** object, gpointer * udata)
+{
+  gboolean res = FALSE;
+  GstSafeQueueItem * item;
+  
+  GST_DEBUG ("queue:%p", queue);
+
+  g_return_val_if_fail (object != NULL, FALSE);
+
+  GST_SAFE_QUEUE_MUTEX_LOCK_CHECK (queue, done);
+
+  STATUS (queue, "before poping");
+
+  while (gst_safe_queue_locked_is_empty (queue)) {
+    GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+    g_signal_emit (G_OBJECT (queue), gst_safe_queue_signals[SIGNAL_EMPTY], 0);
+    GST_SAFE_QUEUE_MUTEX_LOCK_CHECK (queue, done);
+    
+    while (gst_safe_queue_locked_is_empty (queue)) {
+      g_cond_wait (queue->item_add, queue->qlock);
+      if (queue->flushing)
+	goto done;
+    }
+    GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+    g_signal_emit (G_OBJECT (queue), gst_safe_queue_signals[SIGNAL_RUNNING], 0);
+    GST_SAFE_QUEUE_MUTEX_LOCK_CHECK (queue, done);
+    
+  }
+
+  /* Grab next item */
+  item = g_queue_pop_head (queue->queue);
+
+  if (item->visible)
+    queue->cur_level.visible--;
+  queue->cur_level.bytes -= item->size;
+  queue->cur_level.time -= item->duration;
+
+  /* grab the GstMiniObject or unref it if the caller doesn't want it */
+  if (object)
+    *object = item->object;
+  else
+    gst_mini_object_unref (item->object);
+    
+  /* Grab the user data, or try to destroy it if the caller doesn't want it */
+  if (udata)
+    *udata = item->udata;
+  else if (item->udata && item->udestroy)
+    item->udestroy (item->udata);
+
+  g_slice_free (GstSafeQueueItem, item);
+
+  res = TRUE;
+
+  STATUS (queue, "after poping");
+
+  g_cond_signal (queue->item_del);
+
+ done:
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+
+  GST_DEBUG ("queue:%p , res:%d", queue, res);
+
+  return res;
+}
+
+gboolean
+gst_safe_queue_pop (GstSafeQueue * queue, GstMiniObject ** object)
+{
+  return gst_safe_queue_pop_full (queue, object, NULL);
+}
+
+/**
+ * gst_safe_queue_drop_head:
+ * @queue: The #GstSafeQueue to drop an item from.
+ * @type: The #GType of the item to drop.
+ *
+ * Pop and unref the head-most #GstMiniObject with the given #GType.
+ *
+ * Returns: TRUE if an element was removed.
+ */
+
+gboolean
+gst_safe_queue_drop_head (GstSafeQueue * queue, GType type)
+{
+  gboolean res = FALSE;
+  GList *item;
+  GstSafeQueueItem *leak = NULL;
+
+  GST_DEBUG ("queue:%p", queue);
+
+  GST_SAFE_QUEUE_MUTEX_LOCK (queue);
+  for (item = g_queue_peek_head_link (queue->queue); item;
+       item = item->next) {
+    GstSafeQueueItem *tmp = (GstSafeQueueItem*) item->data;
+    if (G_TYPE_CHECK_INSTANCE_TYPE (tmp->object, type)) {
+      leak = tmp;
+      break;
+    }
+  }
+
+  if (!leak)
+    goto done;
+
+  g_queue_delete_link (queue->queue, item);
+
+  if (leak->visible)
+    queue->cur_level.visible--;
+  if (leak->size)
+    queue->cur_level.bytes -= leak->size;
+  if (leak->duration != GST_CLOCK_TIME_NONE)
+    queue->cur_level.time -= leak->duration;
+
+  gst_mini_object_unref (leak->object);
+  if (leak->udata && leak->udestroy)
+    leak->udestroy(leak->udata);
+  g_slice_free (GstSafeQueueItem, leak);
+
+  res = TRUE;
+
+ done:
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+
+  GST_DEBUG ("queue:%p , res:%d", queue, res);
+
+  return res;
+}
+
+/* changing the capacity of the queue must wake up
+ * the _chain function, it might have more room now
+ * to store the buffer/event in the queue */
+#define QUEUE_CAPACITY_CHANGE(q)\
+  g_cond_signal (queue->item_del);
+
+/* Changing the minimum required fill level must
+ * wake up the _loop function as it might now
+ * be able to preceed.
+ */
+#define QUEUE_THRESHOLD_CHANGE(q)\
+  g_cond_signal (queue->item_add);
+
+static void
+gst_safe_queue_set_property (GObject * object,
+    guint prop_id, const GValue * value, GParamSpec * pspec)
+{
+  GstSafeQueue *queue = GST_SAFE_QUEUE (object);
+
+  /* someone could change levels here, and since this
+   * affects the get/put funcs, we need to lock for safety. */
+  GST_SAFE_QUEUE_MUTEX_LOCK (queue);
+
+  switch (prop_id) {
+    case ARG_MAX_SIZE_BYTES:
+      queue->max_size.bytes = g_value_get_uint (value);
+      QUEUE_CAPACITY_CHANGE (queue);
+      break;
+    case ARG_MAX_SIZE_VISIBLE:
+      queue->max_size.visible = g_value_get_uint (value);
+      QUEUE_CAPACITY_CHANGE (queue);
+      break;
+    case ARG_MAX_SIZE_TIME:
+      queue->max_size.time = g_value_get_uint64 (value);
+      QUEUE_CAPACITY_CHANGE (queue);
+      break;
+    case ARG_MIN_THRESHOLD_BYTES:
+      queue->min_threshold.bytes = g_value_get_uint (value);
+      queue->orig_min_threshold.bytes = queue->min_threshold.bytes;
+      QUEUE_THRESHOLD_CHANGE (queue);
+      break;
+    case ARG_MIN_THRESHOLD_VISIBLE:
+      queue->min_threshold.visible = g_value_get_uint (value);
+      queue->orig_min_threshold.visible = queue->min_threshold.visible;
+      QUEUE_THRESHOLD_CHANGE (queue);
+      break;
+    case ARG_MIN_THRESHOLD_TIME:
+      queue->min_threshold.time = g_value_get_uint64 (value);
+      queue->orig_min_threshold.time = queue->min_threshold.time;
+      QUEUE_THRESHOLD_CHANGE (queue);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+}
+
+static void
+gst_safe_queue_get_property (GObject * object,
+    guint prop_id, GValue * value, GParamSpec * pspec)
+{
+  GstSafeQueue *queue = GST_SAFE_QUEUE (object);
+
+  GST_SAFE_QUEUE_MUTEX_LOCK (queue);
+
+  switch (prop_id) {
+    case ARG_CUR_LEVEL_BYTES:
+      g_value_set_uint (value, queue->cur_level.bytes);
+      break;
+    case ARG_CUR_LEVEL_VISIBLE:
+      g_value_set_uint (value, queue->cur_level.visible);
+      break;
+    case ARG_CUR_LEVEL_TIME:
+      g_value_set_uint64 (value, queue->cur_level.time);
+      break;
+    case ARG_MAX_SIZE_BYTES:
+      g_value_set_uint (value, queue->max_size.bytes);
+      break;
+    case ARG_MAX_SIZE_VISIBLE:
+      g_value_set_uint (value, queue->max_size.visible);
+      break;
+    case ARG_MAX_SIZE_TIME:
+      g_value_set_uint64 (value, queue->max_size.time);
+      break;
+    case ARG_MIN_THRESHOLD_BYTES:
+      g_value_set_uint (value, queue->min_threshold.bytes);
+      break;
+    case ARG_MIN_THRESHOLD_VISIBLE:
+      g_value_set_uint (value, queue->min_threshold.visible);
+      break;
+    case ARG_MIN_THRESHOLD_TIME:
+      g_value_set_uint64 (value, queue->min_threshold.time);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+
+  GST_SAFE_QUEUE_MUTEX_UNLOCK (queue);
+}
Index: libs/gst/base/gstsafequeue.h
===================================================================
RCS file: libs/gst/base/gstsafequeue.h
diff -N libs/gst/base/gstsafequeue.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libs/gst/base/gstsafequeue.h	3 Nov 2006 16:01:34 -0000
@@ -0,0 +1,124 @@
+/* GStreamer
+ * Copyright (C) 2006 Edward Hervey <edward@fluendo.com>
+ *
+ * gstsafequeue.h:
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+
+#ifndef __GST_SAFE_QUEUE_H__
+#define __GST_SAFE_QUEUE_H__
+
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+
+#define GST_TYPE_SAFE_QUEUE \
+  (gst_safe_queue_get_type())
+#define GST_SAFE_QUEUE(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SAFE_QUEUE,GstSafeQueue))
+#define GST_SAFE_QUEUE_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SAFE_QUEUE,GstSafeQueueClass))
+#define GST_IS_SAFE_QUEUE(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SAFE_QUEUE))
+#define GST_IS_SAFE_QUEUE_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SAFE_QUEUE))
+
+typedef struct _GstSafeQueue GstSafeQueue;
+typedef struct _GstSafeQueueClass GstSafeQueueClass;
+typedef struct _GstSafeQueueSize GstSafeQueueSize;
+
+/**
+ * GstSafeQueueSize:
+ * @buffers: number of buffers
+ * @bytes: number of bytes
+ * @time: amount of time
+ *
+ * Structure describing the size of a queue.
+ */
+struct _GstSafeQueueSize {
+    guint   visible;
+    guint   bytes;
+    guint64 time;
+};
+
+/**
+ * GstSafeQueue:
+ *
+ * Opaque #GstSafeQueue structure.
+ */
+struct _GstSafeQueue {
+  GObject element;
+
+  /*< private >*/
+  /* the queue of data we're keeping our grubby hands on */
+  GQueue *queue;
+
+  GstSafeQueueSize
+    cur_level,		/* currently in the queue */
+    max_size,		/* max. amount of data allowed in the queue */
+    min_threshold,	/* min. amount of data required to wake reader */
+    orig_min_threshold; /* Original min.threshold, for reset in EOS */
+
+  GMutex *qlock;	/* lock for queue (vs object lock) */
+  GCond *item_add;	/* signals buffers now available for reading */
+  GCond *item_del;	/* signals space now available for writing */
+  gboolean flushing;	/* indicates whether conditions where signalled because
+			 * of external flushing */
+
+  gpointer _gst_reserved[GST_PADDING];
+};
+
+struct _GstSafeQueueClass {
+  GObjectClass parent_class;
+
+  /* signals */
+  void (*empty)		(GstSafeQueue *queue);
+  void (*running)	(GstSafeQueue *queue);
+  void (*full)		(GstSafeQueue *queue);
+
+  gpointer _gst_reserved[GST_PADDING];
+};
+
+GType gst_safe_queue_get_type (void);
+
+GstSafeQueue *	gst_safe_queue_new (void);
+
+gboolean	gst_safe_queue_push_full (GstSafeQueue * queue, GstMiniObject * object,
+					  guint size, guint64 duration, gboolean last,
+					  gboolean visible,
+					  gpointer udata, GDestroyNotify udestroy);
+gboolean	gst_safe_queue_push (GstSafeQueue * queue, GstMiniObject * object,
+				     guint size, guint64 duration, gboolean last,
+				     gboolean visible);
+
+gboolean	gst_safe_queue_pop_full (GstSafeQueue * queue, GstMiniObject ** object,
+					 gpointer * udata);
+gboolean	gst_safe_queue_pop (GstSafeQueue * queue, GstMiniObject ** object);
+
+void		gst_safe_queue_flush (GstSafeQueue * queue);
+void		gst_safe_queue_set_flushing (GstSafeQueue * queue, gboolean flushing);
+
+gboolean	gst_safe_queue_drop_head (GstSafeQueue * queue, GType type);
+
+gboolean	gst_safe_queue_is_full (GstSafeQueue * queue);
+gboolean	gst_safe_queue_is_empty (GstSafeQueue * queue);
+
+G_END_DECLS
+
+
+#endif /* __GST_SAFE_QUEUE_H__ */
Index: libs/gst/base/Makefile.am
===================================================================
RCS file: /cvs/gstreamer/gstreamer/libs/gst/base/Makefile.am,v
retrieving revision 1.17
diff -u -p -r1.17 Makefile.am
--- libs/gst/base/Makefile.am	1 Jul 2006 23:26:06 -0000	1.17
+++ libs/gst/base/Makefile.am	3 Nov 2006 16:01:34 -0000
@@ -9,7 +9,8 @@ libgstbase_@GST_MAJORMINOR@_la_SOURCES =
 	gstbasetransform.c	\
 	gstcollectpads.c	\
 	gstpushsrc.c		\
-	gsttypefindhelper.c
+	gsttypefindhelper.c	\
+	gstsafequeue.c
 
 libgstbase_@GST_MAJORMINOR@_la_CFLAGS = $(GST_OBJ_CFLAGS)
 libgstbase_@GST_MAJORMINOR@_la_LIBADD = $(GST_OBJ_LIBS)
@@ -25,7 +26,8 @@ libgstbase_@GST_MAJORMINOR@include_HEADE
 	gstbasetransform.h	\
 	gstcollectpads.h	\
 	gstpushsrc.h		\
-	gsttypefindhelper.h	
+	gsttypefindhelper.h	\
+	gstsafequeue.h
 
 CLEANFILES = *.gcno *.gcda *.gcov
 
