Index: libs/gst/base/Makefile.am
--- libs/gst/base/Makefile.am
+++ libs/gst/base/Makefile.am
@@ -9,7 +9,8 @@
 	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,5 +26,6 @@
 	gstbasetransform.h	\
 	gstcollectpads.h	\
 	gstpushsrc.h		\
-	gsttypefindhelper.h	
+	gsttypefindhelper.h	\
+	gstsafequeue.h
 
Index: libs/gst/base/gstsafequeue.c
--- /dev/null
+++ libs/gst/base/gstsafequeue.c
@@ -0,0 +1,684 @@
+/* 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);
+
+/* 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_BUFFERS,
+  ARG_CUR_LEVEL_BYTES,
+  ARG_CUR_LEVEL_TIME,
+  ARG_MAX_SIZE_BUFFERS,
+  ARG_MAX_SIZE_BYTES,
+  ARG_MAX_SIZE_TIME,
+  ARG_MIN_THRESHOLD_BUFFERS,
+  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 buffers, %u of %u-%u"	\
+	       "bytes, %"G_GUINT64_FORMAT" of %"G_GUINT64_FORMAT"-%"	\
+	       G_GUINT64_FORMAT " ns, %u elements",			\
+	       queue, \
+	       q->cur_level.buffers, \
+	       q->min_threshold.buffers, \
+	       q->max_size.buffers, \
+	       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 buffer became empty (empty).
+   * A buffer is empty if the total amount of data inside it (num-buffers, 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 buffer became full (full).
+   * A buffer is full if the total amount of data inside it (num-buffers, 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_BUFFERS,
+      g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
+          "Current number of buffers 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_BUFFERS,
+      g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
+          "Max. number of buffers 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_BUFFERS,
+      g_param_spec_uint ("min-threshold-buffers", "Min. threshold (buffers)",
+          "Min. number of buffers 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.buffers = 0; /* no content */
+  queue->cur_level.bytes = 0;   /* no content */
+  queue->cur_level.time = 0;    /* no content */
+  queue->max_size.buffers = 200;        /* 200 buffers */
+  queue->max_size.bytes = 10 * 1024 * 1024;     /* 10 MB */
+  queue->max_size.time = GST_SECOND;    /* 1 s. */
+  queue->min_threshold.buffers = 0;     /* no threshold */
+  queue->min_threshold.bytes = 0;       /* no threshold */
+  queue->min_threshold.time = 0;        /* no threshold */
+  queue->orig_min_threshold.buffers = 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)) {
+    GstMiniObject *data = g_queue_pop_head (queue->queue);
+
+    gst_mini_object_unref (data);
+  }
+
+  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)) {
+    GstMiniObject *data = g_queue_pop_head (queue->queue);
+
+    /* Then lose another reference because we are supposed to destroy that
+       data when flushing */
+    gst_mini_object_unref (data);
+  }
+  queue->cur_level.buffers = 0;
+  queue->cur_level.bytes = 0;
+  queue->cur_level.time = 0;
+  queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
+  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.buffers > 0 &&
+          queue->cur_level.buffers < queue->min_threshold.buffers) ||
+      (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.buffers > 0 &&
+              queue->cur_level.buffers >= queue->max_size.buffers) ||
+          (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 (GstSafeQueue * queue, GstMiniObject * object)
+{
+  gboolean res = FALSE;
+
+  
+  GST_DEBUG ("queue:%p , object:%p", queue, object);
+
+  GST_SAFE_QUEUE_MUTEX_LOCK_CHECK (queue, done);
+  /* Only need to check for size if it's a buffer */
+  STATUS (queue, "before pushing");
+  if (GST_IS_BUFFER (object)) {
+    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 (GST_IS_EVENT (object) && (GST_EVENT_TYPE (object) == GST_EVENT_EOS)) {
+    /* Zero the thresholds, this makes sure the queue is completely
+     * filled and we can read all data from the queue. */
+    queue->min_threshold.buffers = 0;
+    queue->min_threshold.bytes = 0;
+    queue->min_threshold.time = 0;
+  }
+
+  g_queue_push_tail (queue->queue, object);
+  res = TRUE;
+
+  if (GST_IS_BUFFER (object)) {
+    queue->cur_level.buffers++;
+    queue->cur_level.bytes += GST_BUFFER_SIZE (object);
+    if (GST_BUFFER_DURATION (object) != GST_CLOCK_TIME_NONE)
+      queue->cur_level.time += GST_BUFFER_DURATION (object);
+  }
+
+  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;
+}
+
+/**
+ * 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 (GstSafeQueue * queue, GstMiniObject ** object)
+{
+  gboolean res = FALSE;
+  
+  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 */
+  *object = g_queue_pop_head (queue->queue);
+
+  if (GST_IS_BUFFER (*object)) {
+    queue->cur_level.buffers--;
+    queue->cur_level.bytes -= GST_BUFFER_SIZE (*object);
+    if (GST_BUFFER_DURATION (*object) != GST_CLOCK_TIME_NONE)
+      queue->cur_level.time -= GST_BUFFER_DURATION (*object);
+  }
+  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;
+}
+
+/**
+ * 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;
+  GstMiniObject *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) {
+    if (G_TYPE_CHECK_INSTANCE_TYPE (item->data, type)) {
+      leak = item->data;
+      break;
+    }
+  }
+
+  if (!leak)
+    goto done;
+
+  g_queue_delete_link (queue->queue, item);
+
+  if (GST_IS_BUFFER (leak)) {
+    queue->cur_level.bytes -= GST_BUFFER_SIZE (leak);
+    queue->cur_level.buffers--;
+    if (GST_BUFFER_DURATION (leak) != GST_CLOCK_TIME_NONE)
+      queue->cur_level.time -= GST_BUFFER_DURATION (leak);
+  }
+  gst_mini_object_unref (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_BUFFERS:
+      queue->max_size.buffers = 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_BUFFERS:
+      queue->min_threshold.buffers = g_value_get_uint (value);
+      queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
+      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_BUFFERS:
+      g_value_set_uint (value, queue->cur_level.buffers);
+      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_BUFFERS:
+      g_value_set_uint (value, queue->max_size.buffers);
+      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_BUFFERS:
+      g_value_set_uint (value, queue->min_threshold.buffers);
+      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
--- /dev/null
+++ libs/gst/base/gstsafequeue.h
@@ -0,0 +1,114 @@
+/* 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   buffers;
+    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 (GstSafeQueue * queue, GstMiniObject * object);
+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: plugins/elements/gstqueue.c
--- plugins/elements/gstqueue.c
+++ plugins/elements/gstqueue.c
@@ -60,21 +60,8 @@
 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
 
 #define STATUS(queue, pad, msg) \
-  GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
-                      "(%s:%s) " msg ": %u of %u-%u buffers, %u of %u-%u " \
-                      "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
-                      "-%" G_GUINT64_FORMAT " ns, %u elements", \
-                      GST_DEBUG_PAD_NAME (pad), \
-                      queue->cur_level.buffers, \
-                      queue->min_threshold.buffers, \
-                      queue->max_size.buffers, \
-                      queue->cur_level.bytes, \
-                      queue->min_threshold.bytes, \
-                      queue->max_size.bytes, \
-                      queue->cur_level.time, \
-                      queue->min_threshold.time, \
-                      queue->max_size.time, \
-                      queue->queue->length)
+  GST_CAT_LOG_OBJECT (queue_dataflow, queue, "(%s:%s) " msg, \
+		      GST_DEBUG_PAD_NAME (pad));
 
 static const GstElementDetails gst_queue_details = GST_ELEMENT_DETAILS ("Queue",
     "Generic",
@@ -109,30 +96,6 @@
   /* FILL ME */
 };
 
-#define GST_QUEUE_MUTEX_LOCK(q) G_STMT_START {                          \
-  GST_CAT_LOG_OBJECT (queue_dataflow, q,                                \
-      "locking qlock from thread %p",                                   \
-      g_thread_self ());                                                \
-  g_mutex_lock (q->qlock);                                              \
-  GST_CAT_LOG_OBJECT (queue_dataflow, q,                                \
-      "locked qlock from thread %p",                                    \
-      g_thread_self ());                                                \
-} G_STMT_END
-
-#define GST_QUEUE_MUTEX_LOCK_CHECK(q,label) G_STMT_START {              \
-  GST_QUEUE_MUTEX_LOCK (q);                                             \
-  if (q->srcresult != GST_FLOW_OK)                                      \
-    goto label;                                                         \
-} G_STMT_END
-
-#define GST_QUEUE_MUTEX_UNLOCK(q) G_STMT_START {                        \
-  GST_CAT_LOG_OBJECT (queue_dataflow, q,                                \
-      "unlocking qlock from thread %p",                                 \
-      g_thread_self ());                                                \
-  g_mutex_unlock (q->qlock);                                            \
-} G_STMT_END
-
-
 static void gst_queue_base_init (GstQueueClass * klass);
 static void gst_queue_class_init (GstQueueClass * klass);
 static void gst_queue_init (GstQueue * queue);
@@ -147,7 +110,7 @@
 static GstFlowReturn gst_queue_bufferalloc (GstPad * pad, guint64 offset,
     guint size, GstCaps * caps, GstBuffer ** buf);
 static gboolean gst_queue_acceptcaps (GstPad * pad, GstCaps * caps);
-static gboolean gst_queue_push_one (GstQueue * queue);
+static gboolean gst_queue_push_one (GstQueue * queue, GstMiniObject * object);
 static void gst_queue_loop (GstPad * pad);
 
 static gboolean gst_queue_handle_sink_event (GstPad * pad, GstEvent * event);
@@ -158,16 +121,12 @@
 static GstCaps *gst_queue_getcaps (GstPad * pad);
 static GstPadLinkReturn gst_queue_link_sink (GstPad * pad, GstPad * peer);
 static GstPadLinkReturn gst_queue_link_src (GstPad * pad, GstPad * peer);
-static void gst_queue_locked_flush (GstQueue * queue);
 
 static gboolean gst_queue_src_activate_push (GstPad * pad, gboolean active);
 static gboolean gst_queue_sink_activate_push (GstPad * pad, gboolean active);
 static GstStateChangeReturn gst_queue_change_state (GstElement * element,
     GstStateChange transition);
 
-static gboolean gst_queue_is_empty (GstQueue * queue);
-static gboolean gst_queue_is_filled (GstQueue * queue);
-
 #define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
 
 static GType
@@ -368,26 +327,10 @@
       GST_DEBUG_FUNCPTR (gst_queue_handle_src_query));
   gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
 
-  queue->cur_level.buffers = 0; /* no content */
-  queue->cur_level.bytes = 0;   /* no content */
-  queue->cur_level.time = 0;    /* no content */
-  queue->max_size.buffers = 200;        /* 200 buffers */
-  queue->max_size.bytes = 10 * 1024 * 1024;     /* 10 MB */
-  queue->max_size.time = GST_SECOND;    /* 1 s. */
-  queue->min_threshold.buffers = 0;     /* no threshold */
-  queue->min_threshold.bytes = 0;       /* no threshold */
-  queue->min_threshold.time = 0;        /* no threshold */
-  queue->orig_min_threshold.buffers = 0;
-  queue->orig_min_threshold.bytes = 0;
-  queue->orig_min_threshold.time = 0;
-
   queue->leaky = GST_QUEUE_NO_LEAK;
   queue->srcresult = GST_FLOW_WRONG_STATE;
 
-  queue->qlock = g_mutex_new ();
-  queue->item_add = g_cond_new ();
-  queue->item_del = g_cond_new ();
-  queue->queue = g_queue_new ();
+  queue->queue = gst_safe_queue_new();
 
   GST_DEBUG_OBJECT (queue,
       "initialized queue's not_empty & not_full conditions");
@@ -401,17 +344,8 @@
 
   GST_DEBUG_OBJECT (queue, "finalizing queue");
 
-  while (!g_queue_is_empty (queue->queue)) {
-    GstMiniObject *data = g_queue_pop_head (queue->queue);
-
-    gst_mini_object_unref (data);
-  }
-  g_queue_free (queue->queue);
-  GST_DEBUG_OBJECT (queue, "free mutex");
-  g_mutex_free (queue->qlock);
-  GST_DEBUG_OBJECT (queue, "done free mutex");
-  g_cond_free (queue->item_add);
-  g_cond_free (queue->item_del);
+  gst_safe_queue_flush (queue->queue);
+  g_object_unref (G_OBJECT (queue->queue));
 
   if (G_OBJECT_CLASS (parent_class)->finalize)
     G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -455,15 +389,14 @@
   }
 
   if (GST_PAD_LINK_SUCCESSFUL (result)) {
-    GST_QUEUE_MUTEX_LOCK (queue);
     if (queue->srcresult == GST_FLOW_OK) {
+      gst_safe_queue_set_flushing (queue->queue, FALSE);
       gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
       GST_DEBUG_OBJECT (queue, "starting task as pad is linked");
     } else {
       GST_DEBUG_OBJECT (queue, "not starting task reason %s",
           gst_flow_get_name (queue->srcresult));
     }
-    GST_QUEUE_MUTEX_UNLOCK (queue);
   }
   gst_object_unref (queue);
 
@@ -493,27 +426,6 @@
   return TRUE;
 }
 
-static void
-gst_queue_locked_flush (GstQueue * queue)
-{
-  while (!g_queue_is_empty (queue->queue)) {
-    GstMiniObject *data = g_queue_pop_head (queue->queue);
-
-    /* Then lose another reference because we are supposed to destroy that
-       data when flushing */
-    gst_mini_object_unref (data);
-  }
-  queue->cur_level.buffers = 0;
-  queue->cur_level.bytes = 0;
-  queue->cur_level.time = 0;
-  queue->min_threshold.buffers = queue->orig_min_threshold.buffers;
-  queue->min_threshold.bytes = queue->orig_min_threshold.bytes;
-  queue->min_threshold.time = queue->orig_min_threshold.time;
-
-  /* we deleted something... */
-  g_cond_signal (queue->item_del);
-}
-
 static gboolean
 gst_queue_handle_sink_event (GstPad * pad, GstEvent * event)
 {
@@ -528,13 +440,8 @@
       /* forward event */
       gst_pad_push_event (queue->srcpad, event);
 
-      /* now unblock the chain function */
-      GST_QUEUE_MUTEX_LOCK (queue);
       queue->srcresult = GST_FLOW_WRONG_STATE;
-      /* unblock the loop and chain functions */
-      g_cond_signal (queue->item_add);
-      g_cond_signal (queue->item_del);
-      GST_QUEUE_MUTEX_UNLOCK (queue);
+      gst_safe_queue_set_flushing (queue->queue, TRUE);
 
       /* make sure it pauses, this should happen since we sent
        * flush_start downstream. */
@@ -546,8 +453,8 @@
       /* forward event */
       gst_pad_push_event (queue->srcpad, event);
 
-      GST_QUEUE_MUTEX_LOCK (queue);
-      gst_queue_locked_flush (queue);
+      gst_safe_queue_flush (queue->queue);
+      gst_safe_queue_set_flushing (queue->queue, FALSE);
       queue->srcresult = GST_FLOW_OK;
       if (gst_pad_is_linked (queue->srcpad)) {
         gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue_loop,
@@ -555,7 +462,6 @@
       } else {
         GST_DEBUG_OBJECT (queue, "not re-starting task as pad is not linked");
       }
-      GST_QUEUE_MUTEX_UNLOCK (queue);
 
       STATUS (queue, pad, "after flush");
       goto done;
@@ -575,47 +481,13 @@
       break;
   }
 
-  GST_QUEUE_MUTEX_LOCK (queue);
-  if (have_eos) {
-    /* Zero the thresholds, this makes sure the queue is completely
-     * filled and we can read all data from the queue. */
-    queue->min_threshold.buffers = 0;
-    queue->min_threshold.bytes = 0;
-    queue->min_threshold.time = 0;
-  }
-  g_queue_push_tail (queue->queue, event);
-  g_cond_signal (queue->item_add);
-  GST_QUEUE_MUTEX_UNLOCK (queue);
+  gst_safe_queue_push (queue->queue, GST_MINI_OBJECT (event));
 
 done:
 
   return TRUE;
 }
 
-static gboolean
-gst_queue_is_empty (GstQueue * queue)
-{
-  return (queue->queue->length == 0 ||
-      (queue->min_threshold.buffers > 0 &&
-          queue->cur_level.buffers < queue->min_threshold.buffers) ||
-      (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_queue_is_filled (GstQueue * queue)
-{
-  return (((queue->max_size.buffers > 0 &&
-              queue->cur_level.buffers >= queue->max_size.buffers) ||
-          (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)));
-}
-
-
 static GstFlowReturn
 gst_queue_chain (GstPad * pad, GstBuffer * buffer)
 {
@@ -623,120 +495,39 @@
 
   queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
 
-  /* we have to lock the queue since we span threads */
-  GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
-
-  GST_CAT_LOG_OBJECT (queue_dataflow, queue,
-      "adding buffer %p of size %d", buffer, GST_BUFFER_SIZE (buffer));
-
-  /* We make space available if we're "full" according to whatever
-   * the user defined as "full". Note that this only applies to buffers.
-   * We always handle events and they don't count in our statistics. */
-  while (gst_queue_is_filled (queue)) {
-    GST_QUEUE_MUTEX_UNLOCK (queue);
-    g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_OVERRUN], 0);
-    GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
-
-    /* how are we going to make space for this buffer? */
+  if (gst_safe_queue_is_full (queue->queue)) {
     switch (queue->leaky) {
-        /* leak current buffer */
-      case GST_QUEUE_LEAK_UPSTREAM:
-        GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
-            "queue is full, leaking buffer on upstream end");
-        /* now we can clean up and exit right away */
-        goto out_unref;
-
-        /* leak first buffer in the queue */
-      case GST_QUEUE_LEAK_DOWNSTREAM:{
-        /* this is a bit hacky. We'll manually iterate the list
-         * and find the first buffer from the head on. We'll
-         * unref that and "fix up" the GQueue object... */
-        GList *item;
-        GstMiniObject *leak = NULL;
-
-        GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
-            "queue is full, leaking buffer on downstream end");
-
-        for (item = g_queue_peek_head_link (queue->queue); item;
-            item = item->next) {
-          if (GST_IS_BUFFER (item->data)) {
-            leak = item->data;
-            break;
-          }
-        }
-
-        /* if we didn't find anything, it means we have no buffers
-         * in here. That cannot happen, since we had >= 1 bufs */
-        g_assert (leak);
-
-        /* Now remove the link from the queue */
-        g_queue_delete_link (queue->queue, item);
-
-        /* and unref the buffer at the end. Twice, because we keep a ref
-         * to make things read-only. Also keep our list uptodate. */
-        queue->cur_level.bytes -= GST_BUFFER_SIZE (leak);
-        queue->cur_level.buffers--;
-        if (GST_BUFFER_DURATION (leak) != GST_CLOCK_TIME_NONE)
-          queue->cur_level.time -= GST_BUFFER_DURATION (leak);
-
-        gst_buffer_unref (leak);
-        break;
-      }
-
-      default:
-        g_warning ("Unknown leaky type, using default");
-        /* fall-through */
-
-        /* don't leak. Instead, wait for space to be available */
-      case GST_QUEUE_NO_LEAK:
-        STATUS (queue, pad, "pre-full wait");
-
-        /* we recheck, the signal could have changed the thresholds */
-        while (gst_queue_is_filled (queue)) {
-          STATUS (queue, pad,
-              "waiting for item_del signal from thread using qlock");
-          g_cond_wait (queue->item_del, queue->qlock);
-
-          if (queue->srcresult != GST_FLOW_OK)
-            goto out_flushing;
-
-          /* if there's a pending state change for this queue
-           * or its manager, switch back to iterator so bottom
-           * half of state change executes */
-          STATUS (queue, pad,
-              "received item_del signal from thread using qlock");
-        }
-
-        STATUS (queue, pad, "post-full wait");
-        GST_QUEUE_MUTEX_UNLOCK (queue);
-        g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
-        GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
-
-        break;
+    case GST_QUEUE_LEAK_UPSTREAM:
+      GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
+			    "queue is full, leaking buffer on upstream end");
+      /* now we can clean up and exit right away */
+      goto out_unref;
+      
+    case GST_QUEUE_LEAK_DOWNSTREAM: {
+      gboolean res;
+      /* drop the most downstream buffer */
+      res = gst_safe_queue_drop_head (queue->queue, GST_TYPE_BUFFER);
+      
+      /* if we didn't find anything, it means we have no buffers
+       * in here. That cannot happen, since we had >= 1 bufs */
+      g_assert (res);
+      break;
+    }
+    default:
+      /* Just push the buffer on the safe queue */
+      break;
     }
   }
 
-  g_queue_push_tail (queue->queue, buffer);
-
-  /* add buffer to the statistics */
-  queue->cur_level.buffers++;
-  queue->cur_level.bytes += GST_BUFFER_SIZE (buffer);
-  if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE)
-    queue->cur_level.time += GST_BUFFER_DURATION (buffer);
-
-  STATUS (queue, pad, "+ level");
-
-  GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_add");
-  g_cond_signal (queue->item_add);
-  GST_QUEUE_MUTEX_UNLOCK (queue);
+  /* This will block if the queue is full .... which is fine */
+  if (!(gst_safe_queue_push (queue->queue, (GstMiniObject *) buffer)))
+    goto out_flushing;
 
   return GST_FLOW_OK;
 
   /* special conditions */
 out_unref:
   {
-    GST_QUEUE_MUTEX_UNLOCK (queue);
-
     gst_buffer_unref (buffer);
 
     return GST_FLOW_OK;
@@ -747,7 +538,6 @@
 
     GST_CAT_LOG_OBJECT (queue_dataflow, queue,
         "exit because task paused, reason: %s", gst_flow_get_name (ret));
-    GST_QUEUE_MUTEX_UNLOCK (queue);
 
     gst_buffer_unref (buffer);
 
@@ -756,30 +546,19 @@
 }
 
 static gboolean
-gst_queue_push_one (GstQueue * queue)
+gst_queue_push_one (GstQueue * queue, GstMiniObject * data)
 {
   gboolean restart = TRUE;
-  GstMiniObject *data;
 
-  /* There's something in the list now, whatever it is */
-  data = g_queue_pop_head (queue->queue);
   GST_CAT_LOG_OBJECT (queue_dataflow, queue,
       "retrieved data %p from queue", data);
 
   if (GST_IS_BUFFER (data)) {
     GstFlowReturn result;
 
-    /* Update statistics */
-    queue->cur_level.buffers--;
-    queue->cur_level.bytes -= GST_BUFFER_SIZE (data);
-    if (GST_BUFFER_DURATION (data) != GST_CLOCK_TIME_NONE)
-      queue->cur_level.time -= GST_BUFFER_DURATION (data);
-
-    GST_QUEUE_MUTEX_UNLOCK (queue);
     result = gst_pad_push (queue->srcpad, GST_BUFFER (data));
     /* need to check for srcresult here as well */
-    GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
-    /* else result of push indicates what happens */
+
     if (result != GST_FLOW_OK) {
       const gchar *flowname;
 
@@ -788,23 +567,19 @@
       queue->srcresult = result;
 
       GST_DEBUG_OBJECT (queue, "pausing queue, reason %s", flowname);
+      gst_safe_queue_set_flushing (queue->queue, TRUE);
       gst_pad_pause_task (queue->srcpad);
     }
   } else if (GST_IS_EVENT (data)) {
     if (GST_EVENT_TYPE (data) == GST_EVENT_EOS) {
-      queue->cur_level.buffers = 0;
-      queue->cur_level.bytes = 0;
-      queue->cur_level.time = 0;
-      /* all incomming data is now unexpected */
       queue->srcresult = GST_FLOW_UNEXPECTED;
       /* and we don't need to process anymore */
       GST_DEBUG_OBJECT (queue, "pausing queue, we're EOS now");
+      gst_safe_queue_set_flushing (queue->queue, TRUE);
       gst_pad_pause_task (queue->srcpad);
       restart = FALSE;
     }
-    GST_QUEUE_MUTEX_UNLOCK (queue);
     gst_pad_push_event (queue->srcpad, GST_EVENT (data));
-    GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
     if (restart == TRUE)
       return TRUE;
   } else {
@@ -814,66 +589,27 @@
 
   STATUS (queue, queue->srcpad, "after _get()");
 
-  GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_del");
-  g_cond_signal (queue->item_del);
-
   return FALSE;
-
-out_flushing:
-  gst_pad_pause_task (queue->srcpad);
-  GST_CAT_LOG_OBJECT (queue_dataflow, queue,
-      "exit because task paused, reason:  %s",
-      gst_flow_get_name (queue->srcresult));
-
-  return FALSE;                 /* FALSE == no restart */
 }
 
 static void
 gst_queue_loop (GstPad * pad)
 {
   GstQueue *queue;
+  GstMiniObject * object;
 
   queue = GST_QUEUE (GST_PAD_PARENT (pad));
 
-  /* have to lock for thread-safety */
-  GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
-
-restart:
-  while (gst_queue_is_empty (queue)) {
-    GST_QUEUE_MUTEX_UNLOCK (queue);
-    g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_UNDERRUN], 0);
-    GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
-
-    STATUS (queue, pad, "pre-empty wait");
-    /* we recheck, the signal could have changed the thresholds */
-    while (gst_queue_is_empty (queue)) {
-      STATUS (queue, pad, "waiting for item_add");
-
-      GST_LOG_OBJECT (queue, "doing g_cond_wait using qlock from thread %p",
-          g_thread_self ());
-      g_cond_wait (queue->item_add, queue->qlock);
-
-      /* we released the lock in the g_cond above so we might be
-       * flushing now */
-      if (queue->srcresult != GST_FLOW_OK)
-        goto out_flushing;
-
-      GST_LOG_OBJECT (queue, "done g_cond_wait using qlock from thread %p",
-          g_thread_self ());
-      STATUS (queue, pad, "got item_add signal");
-    }
-
-    STATUS (queue, pad, "post-empty wait");
-    GST_QUEUE_MUTEX_UNLOCK (queue);
-    g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
-    GST_QUEUE_MUTEX_LOCK_CHECK (queue, out_flushing);
+ restart:
+  if (!(gst_safe_queue_pop (queue->queue, &object))) {
+    /* pause task */
+    if (queue->srcresult != GST_FLOW_OK)
+      goto out_flushing;
   }
 
-  if (gst_queue_push_one (queue))
+  if (gst_queue_push_one (queue, object))
     goto restart;
 
-  GST_QUEUE_MUTEX_UNLOCK (queue);
-
   return;
 
 out_flushing:
@@ -883,8 +619,6 @@
         "exit because task paused, reason:  %s",
         gst_flow_get_name (queue->srcresult));
 
-    GST_QUEUE_MUTEX_UNLOCK (queue);
-
     return;
   }
 }
@@ -933,10 +667,10 @@
       /* FIXME: this code assumes that there's no discont in the queue */
       switch (format) {
         case GST_FORMAT_BYTES:
-          peer_pos -= queue->cur_level.bytes;
+          peer_pos -= queue->queue->cur_level.bytes;
           break;
         case GST_FORMAT_TIME:
-          peer_pos -= queue->cur_level.time;
+          peer_pos -= queue->queue->cur_level.time;
           break;
         default:
           GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
@@ -964,15 +698,11 @@
   queue = GST_QUEUE (gst_pad_get_parent (pad));
 
   if (active) {
-    GST_QUEUE_MUTEX_LOCK (queue);
     queue->srcresult = GST_FLOW_OK;
-    GST_QUEUE_MUTEX_UNLOCK (queue);
   } else {
     /* step 1, unblock chain function */
-    GST_QUEUE_MUTEX_LOCK (queue);
     queue->srcresult = GST_FLOW_WRONG_STATE;
-    gst_queue_locked_flush (queue);
-    GST_QUEUE_MUTEX_UNLOCK (queue);
+    gst_safe_queue_flush (queue->queue);
   }
 
   gst_object_unref (queue);
@@ -989,26 +719,22 @@
   queue = GST_QUEUE (gst_pad_get_parent (pad));
 
   if (active) {
-    GST_QUEUE_MUTEX_LOCK (queue);
     queue->srcresult = GST_FLOW_OK;
     /* we do not start the task yet if the pad is not connected */
-    if (gst_pad_is_linked (pad))
+    if (gst_pad_is_linked (pad)) {
+      gst_safe_queue_set_flushing (queue->queue, FALSE);
       result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue_loop, pad);
-    else {
+    } else {
       GST_DEBUG_OBJECT (queue, "not starting task as pad is not linked");
       result = TRUE;
     }
-    GST_QUEUE_MUTEX_UNLOCK (queue);
   } else {
     /* step 1, unblock loop function */
-    GST_QUEUE_MUTEX_LOCK (queue);
     queue->srcresult = GST_FLOW_WRONG_STATE;
-    /* the item add signal will unblock */
-    g_cond_signal (queue->item_add);
-    GST_QUEUE_MUTEX_UNLOCK (queue);
-
+    gst_safe_queue_set_flushing (queue->queue, TRUE);
     /* step 2, make sure streaming finishes */
     result = gst_pad_stop_task (pad);
+    gst_safe_queue_set_flushing (queue->queue, FALSE);
   }
 
   gst_object_unref (queue);
@@ -1051,18 +777,7 @@
   return ret;
 }
 
-/* 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);
+#define SET_PROPERTY(name) g_object_set_property (G_OBJECT (queue->queue), name, value)
 
 static void
 gst_queue_set_property (GObject * object,
@@ -1070,37 +785,24 @@
 {
   GstQueue *queue = GST_QUEUE (object);
 
-  /* someone could change levels here, and since this
-   * affects the get/put funcs, we need to lock for safety. */
-  GST_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);
+      SET_PROPERTY ("max-size-bytes");
       break;
     case ARG_MAX_SIZE_BUFFERS:
-      queue->max_size.buffers = g_value_get_uint (value);
-      QUEUE_CAPACITY_CHANGE (queue);
+      SET_PROPERTY ("max-size-buffers");
       break;
     case ARG_MAX_SIZE_TIME:
-      queue->max_size.time = g_value_get_uint64 (value);
-      QUEUE_CAPACITY_CHANGE (queue);
+      SET_PROPERTY ("max-size-time");
       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);
+      SET_PROPERTY ("min-threshold-bytes");
       break;
     case ARG_MIN_THRESHOLD_BUFFERS:
-      queue->min_threshold.buffers = g_value_get_uint (value);
-      queue->orig_min_threshold.buffers = queue->min_threshold.buffers;
-      QUEUE_THRESHOLD_CHANGE (queue);
+      SET_PROPERTY ("min-threshold-buffers");
       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);
+  case ARG_MIN_THRESHOLD_TIME:
+    SET_PROPERTY ("min-threshold-time");
       break;
     case ARG_LEAKY:
       queue->leaky = g_value_get_enum (value);
@@ -1109,45 +811,43 @@
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
   }
-
-  GST_QUEUE_MUTEX_UNLOCK (queue);
 }
 
+#define GET_PROPERTY(name) g_object_get_property (G_OBJECT (queue->queue), name, value)
+
 static void
 gst_queue_get_property (GObject * object,
     guint prop_id, GValue * value, GParamSpec * pspec)
 {
   GstQueue *queue = GST_QUEUE (object);
 
-  GST_QUEUE_MUTEX_LOCK (queue);
-
   switch (prop_id) {
     case ARG_CUR_LEVEL_BYTES:
-      g_value_set_uint (value, queue->cur_level.bytes);
+      GET_PROPERTY ("current-level-bytes");
       break;
     case ARG_CUR_LEVEL_BUFFERS:
-      g_value_set_uint (value, queue->cur_level.buffers);
+      GET_PROPERTY ("current-level-buffers");
       break;
     case ARG_CUR_LEVEL_TIME:
-      g_value_set_uint64 (value, queue->cur_level.time);
+      GET_PROPERTY ("current-level-time");
       break;
     case ARG_MAX_SIZE_BYTES:
-      g_value_set_uint (value, queue->max_size.bytes);
+      GET_PROPERTY ("max-size-bytes");
       break;
     case ARG_MAX_SIZE_BUFFERS:
-      g_value_set_uint (value, queue->max_size.buffers);
+      GET_PROPERTY ("max-size-buffers");
       break;
     case ARG_MAX_SIZE_TIME:
-      g_value_set_uint64 (value, queue->max_size.time);
+      GET_PROPERTY ("max-size-time");
       break;
     case ARG_MIN_THRESHOLD_BYTES:
-      g_value_set_uint (value, queue->min_threshold.bytes);
+      GET_PROPERTY ("min-threshold-bytes");
       break;
     case ARG_MIN_THRESHOLD_BUFFERS:
-      g_value_set_uint (value, queue->min_threshold.buffers);
+      GET_PROPERTY ("min-threshold-buffers");
       break;
     case ARG_MIN_THRESHOLD_TIME:
-      g_value_set_uint64 (value, queue->min_threshold.time);
+      GET_PROPERTY ("min-threshold-time");
       break;
     case ARG_LEAKY:
       g_value_set_enum (value, queue->leaky);
@@ -1157,5 +857,4 @@
       break;
   }
 
-  GST_QUEUE_MUTEX_UNLOCK (queue);
 }
