/* GStreamer
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2000 Wim Taymans <wtay@chello.be>
+ * 2003 Colin Walters <cwalters@gnome.org>
+ * 2005 Wim Taymans <wim@fluendo.com>
*
* gstqueue.c:
*
* Boston, MA 02111-1307, USA.
*/
-/* #define DEBUG_ENABLED */
-/* #define STATUS_ENABLED */
-#ifdef STATUS_ENABLED
-#define STATUS(A) GST_DEBUG(GST_CAT_DATAFLOW, A, GST_ELEMENT_NAME(queue))
-#else
-#define STATUS(A)
-#endif
-#include <pthread.h>
-
-#include "config.h"
#include "gst_private.h"
#include "gstqueue.h"
#include "gstscheduler.h"
+#include "gstpipeline.h"
#include "gstevent.h"
-
-GstElementDetails gst_queue_details = {
- "Queue",
- "Generic",
- "Simple data queue",
- VERSION,
- "Erik Walthinsen <omega@cse.ogi.edu>",
- "(C) 1999",
-};
+#include "gstinfo.h"
+#include "gsterror.h"
+
+static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
+ GST_PAD_SINK,
+ GST_PAD_ALWAYS,
+ GST_STATIC_CAPS_ANY);
+
+static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
+ GST_PAD_SRC,
+ GST_PAD_ALWAYS,
+ GST_STATIC_CAPS_ANY);
+
+GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
+#define GST_CAT_DEFAULT (queue_dataflow)
+
+#define STATUS(queue, 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)
+
+static GstElementDetails gst_queue_details = GST_ELEMENT_DETAILS ("Queue",
+ "Generic",
+ "Simple data queue",
+ "Erik Walthinsen <omega@cse.ogi.edu>");
/* Queue signals and args */
-enum {
- LOW_WATERMARK,
- HIGH_WATERMARK,
+enum
+{
+ SIGNAL_UNDERRUN,
+ SIGNAL_RUNNING,
+ SIGNAL_OVERRUN,
LAST_SIGNAL
};
-enum {
+enum
+{
ARG_0,
- ARG_LEVEL_BUFFERS,
- ARG_LEVEL_BYTES,
- ARG_LEVEL_TIME,
- ARG_SIZE_BUFFERS,
- ARG_SIZE_BYTES,
- ARG_SIZE_TIME,
+ /* 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,
- ARG_LEVEL,
- ARG_MAX_LEVEL,
ARG_MAY_DEADLOCK,
- ARG_BLOCK_TIMEOUT,
+ ARG_BLOCK_TIMEOUT
+ /* FILL ME */
};
+#define GST_QUEUE_MUTEX_LOCK G_STMT_START { \
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
+ "locking qlock from thread %p", \
+ g_thread_self ()); \
+ g_mutex_lock (queue->qlock); \
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
+ "locked qlock from thread %p", \
+ g_thread_self ()); \
+} G_STMT_END
+
+#define GST_QUEUE_MUTEX_UNLOCK G_STMT_START { \
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
+ "unlocking qlock from thread %p", \
+ g_thread_self ()); \
+ g_mutex_unlock (queue->qlock); \
+} G_STMT_END
+
-static void gst_queue_class_init (GstQueueClass *klass);
-static void gst_queue_init (GstQueue *queue);
-static void gst_queue_dispose (GObject *object);
+static void gst_queue_base_init (GstQueueClass * klass);
+static void gst_queue_class_init (GstQueueClass * klass);
+static void gst_queue_init (GstQueue * queue);
+static void gst_queue_finalize (GObject * object);
-static void gst_queue_set_property (GObject *object, guint prop_id,
- const GValue *value, GParamSpec *pspec);
-static void gst_queue_get_property (GObject *object, guint prop_id,
- GValue *value, GParamSpec *pspec);
+static void gst_queue_set_property (GObject * object,
+ guint prop_id, const GValue * value, GParamSpec * pspec);
+static void gst_queue_get_property (GObject * object,
+ guint prop_id, GValue * value, GParamSpec * pspec);
-static void gst_queue_chain (GstPad *pad, GstBuffer *buf);
-static GstBuffer * gst_queue_get (GstPad *pad);
-static GstBufferPool* gst_queue_get_bufferpool (GstPad *pad);
-
-static gboolean gst_queue_handle_src_event (GstPad *pad, GstEvent *event);
+static GstFlowReturn gst_queue_chain (GstPad * pad, GstBuffer * buffer);
+static GstBuffer *gst_queue_bufferalloc (GstPad * pad, guint64 offset,
+ guint size, GstCaps * caps);
+static void gst_queue_loop (GstPad * pad);
+static gboolean gst_queue_handle_sink_event (GstPad * pad, GstEvent * event);
-static void gst_queue_locked_flush (GstQueue *queue);
+static gboolean gst_queue_handle_src_event (GstPad * pad, GstEvent * event);
+static gboolean gst_queue_handle_src_query (GstPad * pad,
+ GstQueryType type, GstFormat * fmt, gint64 * value);
-static GstElementStateReturn gst_queue_change_state (GstElement *element);
-static gboolean gst_queue_release_locks (GstElement *element);
+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 (GstPad * pad, GstActivateMode mode);
+static GstElementStateReturn gst_queue_change_state (GstElement * element);
+
+
+#define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type ())
-
-#define GST_TYPE_QUEUE_LEAKY (queue_leaky_get_type())
static GType
-queue_leaky_get_type(void) {
+queue_leaky_get_type (void)
+{
static GType queue_leaky_type = 0;
static GEnumValue queue_leaky[] = {
- { GST_QUEUE_NO_LEAK, "0", "Not Leaky" },
- { GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream" },
- { GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream" },
- { 0, NULL, NULL },
+ {GST_QUEUE_NO_LEAK, "0", "Not Leaky"},
+ {GST_QUEUE_LEAK_UPSTREAM, "1", "Leaky on Upstream"},
+ {GST_QUEUE_LEAK_DOWNSTREAM, "2", "Leaky on Downstream"},
+ {0, NULL, NULL},
};
+
if (!queue_leaky_type) {
- queue_leaky_type = g_enum_register_static("GstQueueLeaky", queue_leaky);
+ queue_leaky_type = g_enum_register_static ("GstQueueLeaky", queue_leaky);
}
return queue_leaky_type;
}
static GstElementClass *parent_class = NULL;
-/* static guint gst_queue_signals[LAST_SIGNAL] = { 0 }; */
+static guint gst_queue_signals[LAST_SIGNAL] = { 0 };
GType
-gst_queue_get_type(void)
+gst_queue_get_type (void)
{
static GType queue_type = 0;
if (!queue_type) {
static const GTypeInfo queue_info = {
- sizeof(GstQueueClass),
- NULL,
+ sizeof (GstQueueClass),
+ (GBaseInitFunc) gst_queue_base_init,
NULL,
- (GClassInitFunc)gst_queue_class_init,
+ (GClassInitFunc) gst_queue_class_init,
NULL,
NULL,
- sizeof(GstQueue),
- 4,
- (GInstanceInitFunc)gst_queue_init,
+ sizeof (GstQueue),
+ 0,
+ (GInstanceInitFunc) gst_queue_init,
NULL
};
- queue_type = g_type_register_static (GST_TYPE_ELEMENT, "GstQueue", &queue_info, 0);
+
+ queue_type = g_type_register_static (GST_TYPE_ELEMENT,
+ "GstQueue", &queue_info, 0);
+ GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue_dataflow", 0,
+ "dataflow inside the queue element");
}
+
return queue_type;
}
static void
-gst_queue_class_init (GstQueueClass *klass)
-{
- GObjectClass *gobject_class;
- GstElementClass *gstelement_class;
-
- gobject_class = (GObjectClass*)klass;
- gstelement_class = (GstElementClass*)klass;
-
- parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
-
- g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEAKY,
- g_param_spec_enum ("leaky", "Leaky", "Where the queue leaks, if at all.",
- GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK, G_PARAM_READWRITE));
- g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LEVEL,
- g_param_spec_int ("level", "Level", "How many buffers are in the queue.",
- 0, G_MAXINT, 0, G_PARAM_READABLE));
- g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAX_LEVEL,
- g_param_spec_int ("max_level", "Maximum Level", "How many buffers the queue holds.",
- 0, G_MAXINT, 100, G_PARAM_READWRITE));
- g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MAY_DEADLOCK,
- g_param_spec_boolean ("may_deadlock", "May Deadlock", "The queue may deadlock if it's full and not PLAYING",
- TRUE, G_PARAM_READWRITE));
- g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCK_TIMEOUT,
- g_param_spec_int ("block_timeout", "Timeout for Block",
- "Microseconds until blocked queue times out and returns filler event. "
- "Value of -1 disables timeout",
- -1, G_MAXINT, -1, G_PARAM_READWRITE));
-
- gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_queue_dispose);
- gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_queue_set_property);
- gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_queue_get_property);
-
- gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_queue_change_state);
- gstelement_class->release_locks = GST_DEBUG_FUNCPTR(gst_queue_release_locks);
-}
-
-static GstPadConnectReturn
-gst_queue_connect (GstPad *pad, GstCaps *caps)
+gst_queue_base_init (GstQueueClass * klass)
{
- GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
- GstPad *otherpad;
+ GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
- if (pad == queue->srcpad)
- otherpad = queue->sinkpad;
- else
- otherpad = queue->srcpad;
-
- return gst_pad_proxy_connect (otherpad, caps);
+ gst_element_class_add_pad_template (gstelement_class,
+ gst_static_pad_template_get (&srctemplate));
+ gst_element_class_add_pad_template (gstelement_class,
+ gst_static_pad_template_get (&sinktemplate));
+ gst_element_class_set_details (gstelement_class, &gst_queue_details);
}
-static GstCaps*
-gst_queue_getcaps (GstPad *pad, GstCaps *caps)
+static void
+gst_queue_class_init (GstQueueClass * klass)
{
- GstQueue *queue = GST_QUEUE (gst_pad_get_parent (pad));
- GstPad *otherpad;
-
- if (pad == queue->srcpad)
- otherpad = queue->sinkpad;
- else
- otherpad = queue->srcpad;
-
- return gst_pad_get_allowed_caps (otherpad);
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+ GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
+
+ parent_class = g_type_class_peek_parent (klass);
+
+ gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_queue_set_property);
+ gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_queue_get_property);
+
+ /* signals */
+ gst_queue_signals[SIGNAL_UNDERRUN] =
+ g_signal_new ("underrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GstQueueClass, underrun), NULL, NULL,
+ g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
+ gst_queue_signals[SIGNAL_RUNNING] =
+ g_signal_new ("running", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GstQueueClass, running), NULL, NULL,
+ g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
+ gst_queue_signals[SIGNAL_OVERRUN] =
+ g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GstQueueClass, overrun), 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));
+
+ g_object_class_install_property (gobject_class, ARG_LEAKY,
+ g_param_spec_enum ("leaky", "Leaky",
+ "Where the queue leaks, if at all",
+ GST_TYPE_QUEUE_LEAKY, GST_QUEUE_NO_LEAK, G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, ARG_MAY_DEADLOCK,
+ g_param_spec_boolean ("may_deadlock", "May Deadlock",
+ "The queue may deadlock if it's full and not PLAYING",
+ TRUE, G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, ARG_BLOCK_TIMEOUT,
+ g_param_spec_uint64 ("block_timeout", "Timeout for Block",
+ "Nanoseconds until blocked queue times out and returns filler event. "
+ "Value of -1 disables timeout",
+ 0, G_MAXUINT64, -1, G_PARAM_READWRITE));
+
+ /* set several parent class virtual functions */
+ gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_queue_finalize);
+
+ gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue_change_state);
}
static void
-gst_queue_init (GstQueue *queue)
+gst_queue_init (GstQueue * queue)
{
- /* scheduling on this kind of element is, well, interesting */
- GST_FLAG_SET (queue, GST_ELEMENT_DECOUPLED);
- GST_FLAG_SET (queue, GST_ELEMENT_EVENT_AWARE);
-
- queue->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
- gst_pad_set_chain_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_chain));
+ queue->sinkpad =
+ gst_pad_new_from_template (gst_static_pad_template_get (&sinktemplate),
+ "sink");
+ gst_pad_set_chain_function (queue->sinkpad,
+ GST_DEBUG_FUNCPTR (gst_queue_chain));
+ gst_pad_set_event_function (queue->sinkpad,
+ GST_DEBUG_FUNCPTR (gst_queue_handle_sink_event));
+ gst_pad_set_link_function (queue->sinkpad,
+ GST_DEBUG_FUNCPTR (gst_queue_link_sink));
+ gst_pad_set_getcaps_function (queue->sinkpad,
+ GST_DEBUG_FUNCPTR (gst_queue_getcaps));
+ gst_pad_set_bufferalloc_function (queue->sinkpad,
+ GST_DEBUG_FUNCPTR (gst_queue_bufferalloc));
gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
- gst_pad_set_bufferpool_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_get_bufferpool));
- gst_pad_set_connect_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_connect));
- gst_pad_set_getcaps_function (queue->sinkpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
- queue->srcpad = gst_pad_new ("src", GST_PAD_SRC);
- gst_pad_set_get_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_get));
+ queue->srcpad =
+ gst_pad_new_from_template (gst_static_pad_template_get (&srctemplate),
+ "src");
+ gst_pad_set_loop_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_loop));
+ gst_pad_set_activate_function (queue->srcpad,
+ GST_DEBUG_FUNCPTR (gst_queue_src_activate));
+ gst_pad_set_link_function (queue->srcpad,
+ GST_DEBUG_FUNCPTR (gst_queue_link_src));
+ gst_pad_set_getcaps_function (queue->srcpad,
+ GST_DEBUG_FUNCPTR (gst_queue_getcaps));
+ gst_pad_set_event_function (queue->srcpad,
+ GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
+ gst_pad_set_query_function (queue->srcpad,
+ GST_DEBUG_FUNCPTR (gst_queue_handle_src_query));
gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
- gst_pad_set_connect_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_connect));
- gst_pad_set_getcaps_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_getcaps));
- gst_pad_set_event_function (queue->srcpad, GST_DEBUG_FUNCPTR (gst_queue_handle_src_event));
+
+ 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->leaky = GST_QUEUE_NO_LEAK;
- queue->queue = NULL;
- queue->level_buffers = 0;
- queue->level_bytes = 0;
- queue->level_time = 0LL;
- queue->size_buffers = 100; /* 100 buffers */
- queue->size_bytes = 100 * 1024; /* 100KB */
- queue->size_time = 1000000000LL; /* 1sec */
queue->may_deadlock = TRUE;
- queue->block_timeout = -1;
+ queue->block_timeout = GST_CLOCK_TIME_NONE;
+ queue->interrupt = FALSE;
+ queue->flush = FALSE;
queue->qlock = g_mutex_new ();
- queue->reader = FALSE;
- queue->writer = FALSE;
- queue->not_empty = g_cond_new ();
- queue->not_full = g_cond_new ();
- queue->events = g_async_queue_new();
- GST_DEBUG_ELEMENT (GST_CAT_THREAD, queue, "initialized queue's not_empty & not_full conditions");
+ queue->item_add = g_cond_new ();
+ queue->item_del = g_cond_new ();
+ queue->queue = g_queue_new ();
+
+ GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue,
+ "initialized queue's not_empty & not_full conditions");
}
+/* called only once, as opposed to dispose */
static void
-gst_queue_dispose (GObject *object)
+gst_queue_finalize (GObject * object)
{
GstQueue *queue = GST_QUEUE (object);
- g_mutex_free (queue->qlock);
- g_cond_free (queue->not_empty);
- g_cond_free (queue->not_full);
+ GST_DEBUG_OBJECT (queue, "finalizing queue");
- g_async_queue_unref(queue->events);
+ while (!g_queue_is_empty (queue->queue)) {
+ GstData *data = g_queue_pop_head (queue->queue);
+
+ gst_data_unref (data);
+ }
+ g_queue_free (queue->queue);
+ GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue, "free mutex");
+ g_mutex_free (queue->qlock);
+ GST_CAT_DEBUG_OBJECT (GST_CAT_THREAD, queue, "done free mutex");
+ g_cond_free (queue->item_add);
+ g_cond_free (queue->item_del);
- G_OBJECT_CLASS (parent_class)->dispose (object);
+ if (G_OBJECT_CLASS (parent_class)->finalize)
+ G_OBJECT_CLASS (parent_class)->finalize (object);
}
-static GstBufferPool*
-gst_queue_get_bufferpool (GstPad *pad)
+static GstCaps *
+gst_queue_getcaps (GstPad * pad)
{
GstQueue *queue;
+ GstPad *otherpad;
+ GstCaps *result;
- queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
+ queue = GST_QUEUE (GST_PAD_PARENT (pad));
+
+ otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
+ result = gst_pad_peer_get_caps (otherpad);
- return gst_pad_get_bufferpool (queue->srcpad);
+ return result;
}
-static void
-gst_queue_cleanup_buffers (gpointer data, const gpointer user_data)
+static GstPadLinkReturn
+gst_queue_link_sink (GstPad * pad, GstPad * peer)
{
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, user_data, "cleaning buffer %p", data);
+ return GST_PAD_LINK_OK;
+}
- if (GST_IS_BUFFER (data)) {
- gst_buffer_unref (GST_BUFFER (data));
- }
- else {
- gst_event_free (GST_EVENT (data));
+static GstPadLinkReturn
+gst_queue_link_src (GstPad * pad, GstPad * peer)
+{
+ GstPadLinkReturn result = GST_PAD_LINK_OK;
+
+ /* FIXME, see if we need to push or get pulled */
+ if (GST_RPAD_LINKFUNC (peer))
+ result = GST_RPAD_LINKFUNC (peer) (peer, pad);
+
+ return result;
+}
+
+static GstBuffer *
+gst_queue_bufferalloc (GstPad * pad, guint64 offset, guint size, GstCaps * caps)
+{
+ GstQueue *queue;
+ GstPad *otherpeer;
+ GstBuffer *result = NULL;
+
+ queue = GST_QUEUE (GST_PAD_PARENT (pad));
+
+ otherpeer = gst_pad_get_peer (queue->srcpad);
+ if (otherpeer == NULL || GST_RPAD_BUFFERALLOCFUNC (otherpeer) == NULL) {
+ /* let the default aloc function do the work */
+ result = NULL;
+ } else {
+ result =
+ GST_RPAD_BUFFERALLOCFUNC (otherpeer) (otherpeer, offset, size, caps);
}
+ if (otherpeer)
+ gst_object_unref (GST_OBJECT (otherpeer));
+
+ return result;
}
+
static void
-gst_queue_locked_flush (GstQueue *queue)
+gst_queue_locked_flush (GstQueue * queue)
{
- g_list_foreach (queue->queue, gst_queue_cleanup_buffers,
- (gpointer) queue);
- g_list_free (queue->queue);
+ while (!g_queue_is_empty (queue->queue)) {
+ GstData *data = g_queue_pop_head (queue->queue);
+
+ /* Then loose another reference because we are supposed to destroy that
+ data when flushing */
+ gst_data_unref (data);
+ }
+ queue->cur_level.buffers = 0;
+ queue->cur_level.bytes = 0;
+ queue->cur_level.time = 0;
+
+ /* make sure any pending buffers to be added are flushed too */
+ queue->flush = TRUE;
- queue->queue = NULL;
- queue->level_buffers = 0;
- queue->timeval = NULL;
+ /* we deleted something... */
+ g_cond_signal (queue->item_del);
}
-static void
-gst_queue_chain (GstPad *pad, GstBuffer *buf)
+#define STATUS(queue, 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)
+
+static gboolean
+gst_queue_handle_sink_event (GstPad * pad, GstEvent * event)
{
GstQueue *queue;
- gboolean reader;
-
- g_return_if_fail (pad != NULL);
- g_return_if_fail (GST_IS_PAD (pad));
- g_return_if_fail (buf != NULL);
queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
-
- /* check for events to send upstream */
- g_async_queue_lock(queue->events);
- while (g_async_queue_length_unlocked(queue->events) > 0){
- GstEvent *event = (GstEvent*)g_async_queue_pop_unlocked(queue->events);
- g_print("sending event upstream\n");
- gst_pad_event_default (pad, event);
- g_print("event sent\n");
- }
- g_async_queue_unlock(queue->events);
-restart:
- /* we have to lock the queue since we span threads */
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%ld", pthread_self ());
- g_mutex_lock (queue->qlock);
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%ld", pthread_self ());
-
- if (GST_IS_EVENT (buf)) {
- switch (GST_EVENT_TYPE (buf)) {
- case GST_EVENT_FLUSH:
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
- gst_queue_locked_flush (queue);
- break;
- case GST_EVENT_EOS:
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "eos in on %s %d\n",
- GST_ELEMENT_NAME (queue), queue->level_buffers);
- break;
- case GST_EVENT_DISCONTINUOUS:
+ switch (GST_EVENT_TYPE (event)) {
+ case GST_EVENT_FLUSH:
+ STATUS (queue, "received flush event");
+ /* forward event */
+ gst_pad_event_default (pad, event);
+ if (GST_EVENT_FLUSH_DONE (event)) {
+ GST_STREAM_LOCK (queue->srcpad);
+ gst_task_start (GST_RPAD_TASK (queue->srcpad));
+ GST_STREAM_UNLOCK (queue->srcpad);
+ } else {
+ /* now unblock the chain function */
+ GST_QUEUE_MUTEX_LOCK;
gst_queue_locked_flush (queue);
- break;
- default:
- /*gst_pad_event_default (pad, GST_EVENT (buf)); */
- break;
- }
- }
+ GST_QUEUE_MUTEX_UNLOCK;
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "adding buffer %p of size %d",buf,GST_BUFFER_SIZE(buf));
-
- if (queue->level_buffers == queue->size_buffers) {
- /* if this is a leaky queue... */
- if (queue->leaky) {
- /* FIXME don't want to leak events! */
- /* if we leak on the upstream side, drop the current buffer */
- if (queue->leaky == GST_QUEUE_LEAK_UPSTREAM) {
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end");
- if (GST_IS_EVENT (buf))
- fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
- GST_ELEMENT_NAME(GST_ELEMENT(queue)),
- GST_EVENT_TYPE(GST_EVENT(buf)));
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on upstream end");
- gst_buffer_unref(buf);
- /* now we have to clean up and exit right away */
- g_mutex_unlock (queue->qlock);
- return;
- }
- /* otherwise we have to push a buffer off the other end */
- else {
- GList *front;
- GstBuffer *leakbuf;
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue is full, leaking buffer on downstream end");
- front = queue->queue;
- leakbuf = (GstBuffer *)(front->data);
- if (GST_IS_EVENT (leakbuf))
- fprintf(stderr, "Error: queue [%s] leaked an event, type:%d\n",
- GST_ELEMENT_NAME(GST_ELEMENT(queue)),
- GST_EVENT_TYPE(GST_EVENT(leakbuf)));
- queue->level_buffers--;
- queue->level_bytes -= GST_BUFFER_SIZE(leakbuf);
- gst_buffer_unref(leakbuf);
- queue->queue = g_list_remove_link (queue->queue, front);
- g_list_free (front);
- }
- }
+ STATUS (queue, "after flush");
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre full wait, level:%d/%d",
- queue->level_buffers, queue->size_buffers);
- while (queue->level_buffers == queue->size_buffers) {
- /* if there's a pending state change for this queue or its manager, switch */
- /* back to iterator so bottom half of state change executes */
- //while (GST_STATE_PENDING (queue) != GST_STATE_VOID_PENDING) {
- if (queue->interrupt) {
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!");
- g_mutex_unlock (queue->qlock);
- if (gst_scheduler_interrupt (GST_RPAD_SCHED (queue->sinkpad), GST_ELEMENT (queue)))
- return;
- goto restart;
- }
- if (GST_STATE (queue) != GST_STATE_PLAYING) {
- /* this means the other end is shut down */
- /* try to signal to resolve the error */
- if (!queue->may_deadlock) {
- if (GST_IS_BUFFER (buf)) gst_buffer_unref (buf);
- else gst_event_free (GST_EVENT (buf));
- g_mutex_unlock (queue->qlock);
- gst_element_error (GST_ELEMENT (queue), "deadlock found, source pad elements are shut down");
- return;
- }
- else {
- g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
- }
- }
+ /* unblock the loop function */
+ g_cond_signal (queue->item_add);
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_full, level:%d/%d",
- queue->level_buffers, queue->size_buffers);
- if (queue->writer)
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple writers on queue!");
- queue->writer = TRUE;
- g_cond_wait (queue->not_full, queue->qlock);
- queue->writer = FALSE;
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_full signal");
- }
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post full wait, level:%d/%d",
- queue->level_buffers, queue->size_buffers);
+ /* make sure it stops */
+ GST_STREAM_LOCK (queue->srcpad);
+ gst_task_pause (GST_RPAD_TASK (queue->srcpad));
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
+ GST_STREAM_UNLOCK (queue->srcpad);
+ }
+ goto done;
+ case GST_EVENT_EOS:
+ STATUS (queue, "received EOS");
+ break;
+ default:
+ /* we put the event in the queue, we don't have to act ourselves */
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue,
+ "adding event %p of type %d", event, GST_EVENT_TYPE (event));
+ break;
}
- /* put the buffer on the tail of the list */
- queue->queue = g_list_append (queue->queue, buf);
- queue->level_buffers++;
- queue->level_bytes += GST_BUFFER_SIZE(buf);
+ GST_QUEUE_MUTEX_LOCK;
+ g_queue_push_tail (queue->queue, event);
+ g_cond_signal (queue->item_add);
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)+ level:%d/%d",
- GST_DEBUG_PAD_NAME(pad),
- queue->level_buffers, queue->size_buffers);
+ GST_QUEUE_MUTEX_UNLOCK;
+done:
- /* this assertion _has_ to hold */
- /* g_assert (g_list_length (queue->queue) == queue->level_buffers); */
-
- /* reader waiting on an empty queue */
- reader = queue->reader;
+ return TRUE;
+}
- g_mutex_unlock (queue->qlock);
+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));
+}
- if (reader)
- {
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_empty");
- g_cond_signal (queue->not_empty);
- }
+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 GstBuffer *
-gst_queue_get (GstPad *pad)
+
+static GstFlowReturn
+gst_queue_chain (GstPad * pad, GstBuffer * buffer)
{
GstQueue *queue;
- GstBuffer *buf = NULL;
- GList *front;
- gboolean writer;
-
- g_assert(pad != NULL);
- g_assert(GST_IS_PAD(pad));
- g_return_val_if_fail (pad != NULL, NULL);
- g_return_val_if_fail (GST_IS_PAD (pad), NULL);
queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
+ GST_STREAM_LOCK (pad);
-
-restart:
- /* have to lock for thread-safety */
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locking t:%ld", pthread_self ());
- g_mutex_lock (queue->qlock);
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "locked t:%ld %p", pthread_self (), queue->not_empty);
-
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "pre empty wait, level:%d/%d", queue->level_buffers, queue->size_buffers);
- while (queue->level_buffers == 0) {
- /* if there's a pending state change for this queue or its manager, switch
- * back to iterator so bottom half of state change executes
- */
- //while (GST_STATE_PENDING (queue) != GST_STATE_VOID_PENDING) {
- if (queue->interrupt) {
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "interrupted!!");
- g_mutex_unlock (queue->qlock);
- if (gst_scheduler_interrupt (GST_RPAD_SCHED (queue->srcpad), GST_ELEMENT (queue)))
- return NULL;
- goto restart;
- }
- if (GST_STATE (queue) != GST_STATE_PLAYING) {
- /* this means the other end is shut down */
- if (!queue->may_deadlock) {
- g_mutex_unlock (queue->qlock);
- gst_element_error (GST_ELEMENT (queue), "deadlock found, sink pad elements are shut down");
- goto restart;
- }
- else {
- g_print ("%s: waiting for the app to restart source pad elements\n", GST_ELEMENT_NAME (queue));
+ /* we have to lock the queue since we span threads */
+ GST_QUEUE_MUTEX_LOCK;
+
+ 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;
+ g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_OVERRUN], 0);
+ GST_QUEUE_MUTEX_LOCK;
+
+ /* how are we going to make space for this buffer? */
+ 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;
+ GstData *leak = NULL;
+
+ GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
+ "queue is full, leaking buffer on downstream end");
+
+ for (item = queue->queue->head; item != NULL; 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 it from the list, fixing up the GQueue
+ * CHECKME: is a queue->head the first or the last item? */
+ item = g_list_delete_link (queue->queue->head, item);
+ queue->queue->head = g_list_first (item);
+ queue->queue->tail = g_list_last (item);
+ queue->queue->length--;
+
+ /* 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 (buffer);
+ queue->cur_level.buffers--;
+ if (GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE)
+ queue->cur_level.time -= GST_BUFFER_DURATION (buffer);
+
+ gst_buffer_unref (buffer);
+ gst_buffer_unref (buffer);
+ break;
}
- }
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "waiting for not_empty, level:%d/%d", queue->level_buffers, queue->size_buffers);
- if (queue->reader)
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "WARNING: multiple readers on queue!");
- queue->reader = TRUE;
-
- if (queue->block_timeout > -1){
- GTimeVal timeout;
- g_get_current_time(&timeout);
- g_time_val_add(&timeout, queue->block_timeout);
- if (!g_cond_timed_wait (queue->not_empty, queue->qlock, &timeout)){
- g_mutex_unlock (queue->qlock);
- return GST_BUFFER(gst_event_new_filler());
- }
- }
- else {
- g_cond_wait (queue->not_empty, queue->qlock);
+ 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, "pre-full wait");
+
+ while (gst_queue_is_filled (queue)) {
+ STATUS (queue, "waiting for item_del signal from thread using qlock");
+ g_cond_wait (queue->item_del, queue->qlock);
+
+ /* 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, "received item_del signal from thread using qlock");
+ }
+
+ STATUS (queue, "post-full wait");
+ GST_QUEUE_MUTEX_UNLOCK;
+ g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
+ GST_QUEUE_MUTEX_LOCK;
+ break;
}
- queue->reader = FALSE;
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "got not_empty signal");
}
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "post empty wait, level:%d/%d", queue->level_buffers, queue->size_buffers);
+ /* we are flushing */
+ if (GST_RPAD_IS_FLUSHING (pad))
+ goto out_flushing;
+
+ 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, "+ level");
+
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_add");
+ g_cond_signal (queue->item_add);
+ GST_QUEUE_MUTEX_UNLOCK;
+ GST_STREAM_UNLOCK (pad);
+
+ return GST_FLOW_OK;
+
+out_unref:
+ GST_QUEUE_MUTEX_UNLOCK;
+ GST_STREAM_UNLOCK (pad);
- front = queue->queue;
- buf = (GstBuffer *)(front->data);
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "retrieved buffer %p from queue", buf);
- queue->queue = g_list_remove_link (queue->queue, front);
- g_list_free (front);
+ gst_buffer_unref (buffer);
- queue->level_buffers--;
- queue->level_bytes -= GST_BUFFER_SIZE(buf);
+ return GST_FLOW_OK;
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "(%s:%s)- level:%d/%d",
- GST_DEBUG_PAD_NAME(pad),
- queue->level_buffers, queue->size_buffers);
+out_flushing:
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because of flush");
+ GST_QUEUE_MUTEX_UNLOCK;
+ gst_task_pause (GST_RPAD_TASK (queue->srcpad));
+ GST_STREAM_UNLOCK (pad);
- /* this assertion _has_ to hold */
- /* g_assert (g_list_length (queue->queue) == queue->level_buffers); */
+ gst_buffer_unref (buffer);
+
+ return GST_FLOW_UNEXPECTED;
+}
+
+static void
+gst_queue_loop (GstPad * pad)
+{
+ GstQueue *queue;
+ GstData *data;
+ gboolean restart = TRUE;
- /* writer waiting on a full queue */
- writer = queue->writer;
+ queue = GST_QUEUE (GST_PAD_PARENT (pad));
- g_mutex_unlock (queue->qlock);
+ GST_STREAM_LOCK (pad);
- if (writer)
- {
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "signalling not_full");
- g_cond_signal (queue->not_full);
+ /* have to lock for thread-safety */
+ GST_QUEUE_MUTEX_LOCK;
+
+restart:
+ while (gst_queue_is_empty (queue)) {
+ GST_QUEUE_MUTEX_UNLOCK;
+ g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_UNDERRUN], 0);
+ GST_QUEUE_MUTEX_LOCK;
+
+ STATUS (queue, "pre-empty wait");
+ while (gst_queue_is_empty (queue)) {
+ STATUS (queue, "waiting for item_add");
+
+ /* we are flushing */
+ if (GST_RPAD_IS_FLUSHING (queue->sinkpad))
+ goto out_flushing;
+
+ 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 got unlocked because we are flushing */
+ if (GST_RPAD_IS_FLUSHING (queue->sinkpad))
+ goto out_flushing;
+
+ GST_LOG_OBJECT (queue, "done g_cond_wait using qlock from thread %p",
+ g_thread_self ());
+ STATUS (queue, "got item_add signal");
+ }
+
+ STATUS (queue, "post-empty wait");
+ GST_QUEUE_MUTEX_UNLOCK;
+ g_signal_emit (G_OBJECT (queue), gst_queue_signals[SIGNAL_RUNNING], 0);
+ GST_QUEUE_MUTEX_LOCK;
}
- /* FIXME where should this be? locked? */
- if (GST_IS_EVENT(buf)) {
- GstEvent *event = GST_EVENT(buf);
- switch (GST_EVENT_TYPE(event)) {
- case GST_EVENT_EOS:
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "queue \"%s\" eos", GST_ELEMENT_NAME (queue));
- gst_element_set_eos (GST_ELEMENT (queue));
- break;
- default:
- break;
+ /* 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;
+ result = gst_pad_push (pad, GST_BUFFER (data));
+ GST_QUEUE_MUTEX_LOCK;
+ if (result != GST_FLOW_OK) {
+ gst_task_pause (GST_RPAD_TASK (queue->srcpad));
+ }
+ } else {
+ if (GST_EVENT_TYPE (data) == GST_EVENT_EOS) {
+ gst_task_pause (GST_RPAD_TASK (queue->srcpad));
+ restart = FALSE;
}
+ GST_QUEUE_MUTEX_UNLOCK;
+ gst_pad_push_event (queue->srcpad, GST_EVENT (data));
+ GST_QUEUE_MUTEX_LOCK;
+ if (restart == TRUE)
+ goto restart;
}
- return buf;
+ STATUS (queue, "after _get()");
+
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, "signalling item_del");
+ g_cond_signal (queue->item_del);
+ GST_QUEUE_MUTEX_UNLOCK;
+ GST_STREAM_UNLOCK (pad);
+ return;
+
+out_flushing:
+ GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because of flush");
+ gst_task_pause (GST_RPAD_TASK (pad));
+ GST_QUEUE_MUTEX_UNLOCK;
+ GST_STREAM_UNLOCK (pad);
+ return;
}
static gboolean
-gst_queue_handle_src_event (GstPad *pad, GstEvent *event)
+gst_queue_handle_src_event (GstPad * pad, GstEvent * event)
{
- GstQueue *queue;
+ GstQueue *queue = GST_QUEUE (GST_PAD_PARENT (pad));
+ gboolean res = TRUE;
- queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
+ GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%d)",
+ event, GST_EVENT_TYPE (event));
- /* push the event to the queue for upstream consumption */
- g_async_queue_push(queue->events, event);
+ gst_event_ref (event);
+ res = gst_pad_event_default (pad, event);
+ GST_QUEUE_MUTEX_LOCK;
- g_mutex_lock (queue->qlock);
switch (GST_EVENT_TYPE (event)) {
- case GST_EVENT_FLUSH:
- GST_DEBUG_ELEMENT (GST_CAT_DATAFLOW, queue, "FLUSH event, flushing queue\n");
- gst_queue_locked_flush (queue);
- break;
case GST_EVENT_SEEK:
- if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
+ if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH) {
gst_queue_locked_flush (queue);
+ }
default:
break;
}
- g_mutex_unlock (queue->qlock);
+ GST_QUEUE_MUTEX_UNLOCK;
+ gst_event_unref (event);
+
+ return res;
+}
+
+static gboolean
+gst_queue_handle_src_query (GstPad * pad,
+ GstQueryType type, GstFormat * fmt, gint64 * value)
+{
+ GstQueue *queue = GST_QUEUE (GST_PAD_PARENT (pad));
+
+ if (!GST_PAD_PEER (queue->sinkpad))
+ return FALSE;
+ if (!gst_pad_query (GST_PAD_PEER (queue->sinkpad), type, fmt, value))
+ return FALSE;
+
+ if (type == GST_QUERY_POSITION) {
+ /* FIXME: this code assumes that there's no discont in the queue */
+ switch (*fmt) {
+ case GST_FORMAT_BYTES:
+ *value -= queue->cur_level.bytes;
+ break;
+ case GST_FORMAT_TIME:
+ *value -= queue->cur_level.time;
+ break;
+ default:
+ /* FIXME */
+ break;
+ }
+ }
- /* we have to claim success, but we don't really know */
return TRUE;
}
static gboolean
-gst_queue_release_locks (GstElement *element)
+gst_queue_src_activate (GstPad * pad, GstActivateMode mode)
{
+ gboolean result = FALSE;
GstQueue *queue;
- queue = GST_QUEUE (element);
-
- g_mutex_lock (queue->qlock);
- queue->interrupt = TRUE;
- g_cond_signal (queue->not_full);
- g_cond_signal (queue->not_empty);
- g_mutex_unlock (queue->qlock);
+ queue = GST_QUEUE (GST_OBJECT_PARENT (pad));
- return TRUE;
+ if (mode == GST_ACTIVATE_PUSH) {
+ /* if we have a scheduler we can start the task */
+ if (GST_ELEMENT_SCHEDULER (queue)) {
+ GST_STREAM_LOCK (pad);
+ GST_RPAD_TASK (pad) =
+ gst_scheduler_create_task (GST_ELEMENT_SCHEDULER (queue),
+ (GstTaskFunction) gst_queue_loop, pad);
+
+ gst_task_start (GST_RPAD_TASK (pad));
+ GST_STREAM_UNLOCK (pad);
+ result = TRUE;
+ }
+ } else {
+ /* step 1, unblock chain and loop functions */
+ queue->interrupt = TRUE;
+ g_cond_signal (queue->item_add);
+ g_cond_signal (queue->item_del);
+
+ /* step 2, make sure streaming finishes */
+ GST_STREAM_LOCK (pad);
+ /* step 3, stop the task */
+ gst_task_stop (GST_RPAD_TASK (pad));
+ gst_object_unref (GST_OBJECT (GST_RPAD_TASK (pad)));
+ GST_STREAM_UNLOCK (pad);
+
+ result = TRUE;
+ }
+ return result;
}
+
static GstElementStateReturn
-gst_queue_change_state (GstElement *element)
+gst_queue_change_state (GstElement * element)
{
GstQueue *queue;
- GstElementStateReturn ret;
- GstElementState new_state;
- g_return_val_if_fail (GST_IS_QUEUE (element), GST_STATE_FAILURE);
+ GstElementStateReturn ret = GST_STATE_SUCCESS;
queue = GST_QUEUE (element);
- GST_DEBUG_ENTER("('%s')", GST_ELEMENT_NAME (element));
+ GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "starting state change");
/* lock the queue so another thread (not in sync with this thread's state)
- * can't call this queue's _get (or whatever)
- */
- g_mutex_lock (queue->qlock);
-
- new_state = GST_STATE_PENDING (element);
+ * can't call this queue's _loop (or whatever) */
+ GST_QUEUE_MUTEX_LOCK;
- if (new_state == GST_STATE_PAUSED) {
- /*g_cond_signal (queue->not_full); */
- /*g_cond_signal (queue->not_empty); */
- }
- else if (new_state == GST_STATE_READY) {
- gst_queue_locked_flush (queue);
- }
- else if (new_state == GST_STATE_PLAYING) {
- if (!GST_PAD_IS_CONNECTED (queue->sinkpad)) {
- GST_DEBUG_ELEMENT (GST_CAT_STATES, queue, "queue %s is not connected", GST_ELEMENT_NAME (queue));
- /* FIXME can this be? */
- if (queue->reader)
- g_cond_signal (queue->not_empty);
- g_mutex_unlock (queue->qlock);
-
- return GST_STATE_FAILURE;
- }
- queue->interrupt = FALSE;
+ switch (GST_STATE_TRANSITION (element)) {
+ case GST_STATE_NULL_TO_READY:
+ gst_queue_locked_flush (queue);
+ break;
+ case GST_STATE_READY_TO_PAUSED:
+ break;
+ case GST_STATE_PAUSED_TO_PLAYING:
+ queue->interrupt = FALSE;
+ break;
+ default:
+ break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
- g_mutex_unlock (queue->qlock);
- GST_DEBUG_LEAVE("('%s')", GST_ELEMENT_NAME (element));
+ switch (GST_STATE_TRANSITION (element)) {
+ case GST_STATE_PLAYING_TO_PAUSED:
+ break;
+ case GST_STATE_PAUSED_TO_READY:
+ gst_queue_locked_flush (queue);
+ break;
+ case GST_STATE_READY_TO_NULL:
+ break;
+ default:
+ break;
+ }
+ GST_QUEUE_MUTEX_UNLOCK;
+
+ GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "done with state change");
+
return ret;
}
-
static void
-gst_queue_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
+gst_queue_set_property (GObject * object,
+ guint prop_id, const GValue * value, GParamSpec * pspec)
{
- GstQueue *queue;
-
- /* it's not null if we got it, but it might not be ours */
- g_return_if_fail (GST_IS_QUEUE (object));
+ GstQueue *queue = GST_QUEUE (object);
- 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;
switch (prop_id) {
+ case ARG_MAX_SIZE_BYTES:
+ queue->max_size.bytes = g_value_get_uint (value);
+ break;
+ case ARG_MAX_SIZE_BUFFERS:
+ queue->max_size.buffers = g_value_get_uint (value);
+ break;
+ case ARG_MAX_SIZE_TIME:
+ queue->max_size.time = g_value_get_uint64 (value);
+ break;
+ case ARG_MIN_THRESHOLD_BYTES:
+ queue->min_threshold.bytes = g_value_get_uint (value);
+ break;
+ case ARG_MIN_THRESHOLD_BUFFERS:
+ queue->min_threshold.buffers = g_value_get_uint (value);
+ break;
+ case ARG_MIN_THRESHOLD_TIME:
+ queue->min_threshold.time = g_value_get_uint64 (value);
+ break;
case ARG_LEAKY:
queue->leaky = g_value_get_enum (value);
break;
- case ARG_MAX_LEVEL:
- queue->size_buffers = g_value_get_int (value);
- break;
case ARG_MAY_DEADLOCK:
queue->may_deadlock = g_value_get_boolean (value);
break;
case ARG_BLOCK_TIMEOUT:
- queue->block_timeout = g_value_get_int (value);
+ queue->block_timeout = g_value_get_uint64 (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
+
+ GST_QUEUE_MUTEX_UNLOCK;
}
static void
-gst_queue_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+gst_queue_get_property (GObject * object,
+ guint prop_id, GValue * value, GParamSpec * pspec)
{
- GstQueue *queue;
-
- /* it's not null if we got it, but it might not be ours */
- g_return_if_fail (GST_IS_QUEUE (object));
-
- queue = GST_QUEUE (object);
+ GstQueue *queue = GST_QUEUE (object);
switch (prop_id) {
- case ARG_LEAKY:
- g_value_set_enum (value, queue->leaky);
+ 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_LEVEL:
- g_value_set_int (value, queue->level_buffers);
+ case ARG_MAX_SIZE_BUFFERS:
+ g_value_set_uint (value, queue->max_size.buffers);
break;
- case ARG_MAX_LEVEL:
- g_value_set_int (value, queue->size_buffers);
+ 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;
+ case ARG_LEAKY:
+ g_value_set_enum (value, queue->leaky);
break;
case ARG_MAY_DEADLOCK:
g_value_set_boolean (value, queue->may_deadlock);
break;
case ARG_BLOCK_TIMEOUT:
- g_value_set_int (value, queue->block_timeout);
+ g_value_set_uint64 (value, queue->block_timeout);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);