2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
29 * Data is queued until one of the limits specified by the
30 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
31 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
38 * You can query how many buffers are queued by reading the
39 * #GstQueue2:current-level-buffers property.
41 * The default queue size limits are 100 buffers, 2MB of data, or
42 * two seconds worth of data, whichever is reached first.
44 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
45 * will allocate a random free filename and buffer data in the file.
46 * By using this, it will buffer the entire stream data on the file independently
47 * of the queue size limits, they will only be used for buffering statistics.
49 * The temp-location property will be used to notify the application of the
57 #include "gstqueue2.h"
59 #include <glib/gstdio.h>
61 #include "gst/gst-i18n-lib.h"
62 #include "gst/glib-compat-private.h"
67 #include <io.h> /* lseek, open, close, read */
69 #define lseek _lseeki64
76 #ifdef __BIONIC__ /* Android */
84 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
94 GST_DEBUG_CATEGORY_STATIC (queue_debug);
95 #define GST_CAT_DEFAULT (queue_debug)
96 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
105 #define DEFAULT_BUFFER_SIZE 4096
106 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
107 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
108 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
110 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
112 /* default property values */
113 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
114 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
115 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
116 #define DEFAULT_USE_BUFFERING FALSE
117 #define DEFAULT_USE_RATE_ESTIMATE TRUE
118 #define DEFAULT_LOW_PERCENT 10
119 #define DEFAULT_HIGH_PERCENT 99
120 #define DEFAULT_TEMP_REMOVE TRUE
121 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
126 PROP_CUR_LEVEL_BUFFERS,
127 PROP_CUR_LEVEL_BYTES,
129 PROP_MAX_SIZE_BUFFERS,
133 PROP_USE_RATE_ESTIMATE,
139 PROP_RING_BUFFER_MAX_SIZE,
144 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
151 #define STATUS(queue, pad, msg) \
152 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
153 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
154 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
155 " ns, %"G_GUINT64_FORMAT" items", \
156 GST_DEBUG_PAD_NAME (pad), \
157 queue->cur_level.buffers, \
158 queue->max_level.buffers, \
159 queue->cur_level.bytes, \
160 queue->max_level.bytes, \
161 queue->cur_level.time, \
162 queue->max_level.time, \
163 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
164 queue->current->writing_pos - queue->current->max_reading_pos : \
165 queue->queue.length))
167 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
168 g_mutex_lock (&q->qlock); \
171 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
172 GST_QUEUE2_MUTEX_LOCK (q); \
173 if (res != GST_FLOW_OK) \
177 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
178 g_mutex_unlock (&q->qlock); \
181 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
182 STATUS (queue, q->sinkpad, "wait for DEL"); \
183 q->waiting_del = TRUE; \
184 g_cond_wait (&q->item_del, &queue->qlock); \
185 q->waiting_del = FALSE; \
186 if (res != GST_FLOW_OK) { \
187 STATUS (queue, q->srcpad, "received DEL wakeup"); \
190 STATUS (queue, q->sinkpad, "received DEL"); \
193 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
194 STATUS (queue, q->srcpad, "wait for ADD"); \
195 q->waiting_add = TRUE; \
196 g_cond_wait (&q->item_add, &q->qlock); \
197 q->waiting_add = FALSE; \
198 if (res != GST_FLOW_OK) { \
199 STATUS (queue, q->srcpad, "received ADD wakeup"); \
202 STATUS (queue, q->srcpad, "received ADD"); \
205 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
206 if (q->waiting_del) { \
207 STATUS (q, q->srcpad, "signal DEL"); \
208 g_cond_signal (&q->item_del); \
212 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
213 if (q->waiting_add) { \
214 STATUS (q, q->sinkpad, "signal ADD"); \
215 g_cond_signal (&q->item_add); \
219 #define SET_PERCENT(q, perc) G_STMT_START { \
220 if (perc != q->buffering_percent) { \
221 q->buffering_percent = perc; \
222 q->percent_changed = TRUE; \
223 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
224 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
225 &q->buffering_left); \
230 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
231 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
232 "dataflow inside the queue element");
233 #define gst_queue2_parent_class parent_class
234 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
236 static void gst_queue2_finalize (GObject * object);
238 static void gst_queue2_set_property (GObject * object,
239 guint prop_id, const GValue * value, GParamSpec * pspec);
240 static void gst_queue2_get_property (GObject * object,
241 guint prop_id, GValue * value, GParamSpec * pspec);
243 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
245 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
246 GstBufferList * buffer_list);
247 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
248 static void gst_queue2_loop (GstPad * pad);
250 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
252 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
255 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
257 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
259 static gboolean gst_queue2_handle_query (GstElement * element,
262 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
263 guint64 offset, guint length, GstBuffer ** buffer);
265 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
266 GstPadMode mode, gboolean active);
267 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
268 GstPadMode mode, gboolean active);
269 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
270 GstStateChange transition);
272 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
273 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
275 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
276 static void update_in_rates (GstQueue2 * queue);
277 static void gst_queue2_post_buffering (GstQueue2 * queue);
281 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
282 GST_QUEUE2_ITEM_TYPE_BUFFER,
283 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
284 GST_QUEUE2_ITEM_TYPE_EVENT,
285 GST_QUEUE2_ITEM_TYPE_QUERY
290 GstQueue2ItemType type;
294 static guint gst_queue2_signals[LAST_SIGNAL] = { 0 };
297 gst_queue2_class_init (GstQueue2Class * klass)
299 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
300 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
302 gobject_class->set_property = gst_queue2_set_property;
303 gobject_class->get_property = gst_queue2_get_property;
307 * GstQueue2::overrun:
308 * @queue: the queue2 instance
310 * Reports that the buffer became full (overrun).
311 * A buffer is full if the total amount of data inside it (num-buffers, time,
312 * size) is higher than the boundary values which can be set through the
313 * GObject properties.
317 gst_queue2_signals[SIGNAL_OVERRUN] =
318 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
319 G_STRUCT_OFFSET (GstQueue2Class, overrun), NULL, NULL,
320 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
323 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
324 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
325 "Current amount of data in the queue (bytes)",
326 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
328 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
329 "Current number of buffers in the queue",
330 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
332 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
333 "Current amount of data in the queue (in ns)",
334 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
336 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
337 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
338 "Max. amount of data in the queue (bytes, 0=disable)",
339 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
340 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
341 G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
343 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
344 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
345 DEFAULT_MAX_SIZE_BUFFERS,
346 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
347 G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
349 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
350 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
351 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
352 G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
355 g_param_spec_boolean ("use-buffering", "Use buffering",
356 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
357 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
358 G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
360 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
361 "Estimate the bitrate of the stream to calculate time level",
362 DEFAULT_USE_RATE_ESTIMATE,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
365 g_param_spec_int ("low-percent", "Low percent",
366 "Low threshold for buffering to start. Only used if use-buffering is True",
367 0, 100, DEFAULT_LOW_PERCENT,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
370 g_param_spec_int ("high-percent", "High percent",
371 "High threshold for buffering to finish. Only used if use-buffering is True",
372 0, 100, DEFAULT_HIGH_PERCENT,
373 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
376 g_param_spec_string ("temp-template", "Temporary File Template",
377 "File template to store temporary files in, should contain directory "
378 "and XXXXXX. (NULL == disabled)",
379 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
382 g_param_spec_string ("temp-location", "Temporary File Location",
383 "Location to store temporary files in (Only read this property, "
384 "use temp-template to configure the name template)",
385 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
388 * GstQueue2:temp-remove
390 * When temp-template is set, remove the temporary file when going to READY.
392 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
393 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
394 "Remove the temp-location after use",
395 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 * GstQueue2:ring-buffer-max-size
400 * The maximum size of the ring buffer in bytes. If set to 0, the ring
401 * buffer is disabled. Default 0.
403 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
404 g_param_spec_uint64 ("ring-buffer-max-size",
405 "Max. ring buffer size (bytes)",
406 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
407 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 * GstQueue2:avg-in-rate
413 * The average input data rate.
415 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
416 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
417 "Average input data rate (bytes/s)",
418 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
420 /* set several parent class virtual functions */
421 gobject_class->finalize = gst_queue2_finalize;
423 gst_element_class_add_pad_template (gstelement_class,
424 gst_static_pad_template_get (&srctemplate));
425 gst_element_class_add_pad_template (gstelement_class,
426 gst_static_pad_template_get (&sinktemplate));
428 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
431 "Erik Walthinsen <omega@cse.ogi.edu>, "
432 "Wim Taymans <wim.taymans@gmail.com>");
434 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
435 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
439 gst_queue2_init (GstQueue2 * queue)
441 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
443 gst_pad_set_chain_function (queue->sinkpad,
444 GST_DEBUG_FUNCPTR (gst_queue2_chain));
445 gst_pad_set_chain_list_function (queue->sinkpad,
446 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
447 gst_pad_set_activatemode_function (queue->sinkpad,
448 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
449 gst_pad_set_event_function (queue->sinkpad,
450 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
451 gst_pad_set_query_function (queue->sinkpad,
452 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
453 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
454 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
456 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
458 gst_pad_set_activatemode_function (queue->srcpad,
459 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
460 gst_pad_set_getrange_function (queue->srcpad,
461 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
462 gst_pad_set_event_function (queue->srcpad,
463 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
464 gst_pad_set_query_function (queue->srcpad,
465 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
466 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
467 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
470 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
471 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
472 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
473 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
474 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
475 queue->use_buffering = DEFAULT_USE_BUFFERING;
476 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
477 queue->low_percent = DEFAULT_LOW_PERCENT;
478 queue->high_percent = DEFAULT_HIGH_PERCENT;
480 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
481 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
483 queue->sinktime = GST_CLOCK_TIME_NONE;
484 queue->srctime = GST_CLOCK_TIME_NONE;
485 queue->sink_tainted = TRUE;
486 queue->src_tainted = TRUE;
488 queue->srcresult = GST_FLOW_FLUSHING;
489 queue->sinkresult = GST_FLOW_FLUSHING;
490 queue->is_eos = FALSE;
491 queue->in_timer = g_timer_new ();
492 queue->out_timer = g_timer_new ();
494 g_mutex_init (&queue->qlock);
495 queue->waiting_add = FALSE;
496 g_cond_init (&queue->item_add);
497 queue->waiting_del = FALSE;
498 g_cond_init (&queue->item_del);
499 g_queue_init (&queue->queue);
501 g_cond_init (&queue->query_handled);
502 queue->last_query = FALSE;
504 g_mutex_init (&queue->buffering_post_lock);
505 queue->buffering_percent = 100;
507 /* tempfile related */
508 queue->temp_template = NULL;
509 queue->temp_location = NULL;
510 queue->temp_remove = DEFAULT_TEMP_REMOVE;
512 queue->ring_buffer = NULL;
513 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
515 GST_DEBUG_OBJECT (queue,
516 "initialized queue's not_empty & not_full conditions");
519 /* called only once, as opposed to dispose */
521 gst_queue2_finalize (GObject * object)
523 GstQueue2 *queue = GST_QUEUE2 (object);
525 GST_DEBUG_OBJECT (queue, "finalizing queue");
527 while (!g_queue_is_empty (&queue->queue)) {
528 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
530 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
531 gst_mini_object_unref (qitem->item);
532 g_slice_free (GstQueue2Item, qitem);
535 queue->last_query = FALSE;
536 g_queue_clear (&queue->queue);
537 g_mutex_clear (&queue->qlock);
538 g_mutex_clear (&queue->buffering_post_lock);
539 g_cond_clear (&queue->item_add);
540 g_cond_clear (&queue->item_del);
541 g_cond_clear (&queue->query_handled);
542 g_timer_destroy (queue->in_timer);
543 g_timer_destroy (queue->out_timer);
545 /* temp_file path cleanup */
546 g_free (queue->temp_template);
547 g_free (queue->temp_location);
549 G_OBJECT_CLASS (parent_class)->finalize (object);
553 debug_ranges (GstQueue2 * queue)
555 GstQueue2Range *walk;
557 for (walk = queue->ranges; walk; walk = walk->next) {
558 GST_DEBUG_OBJECT (queue,
559 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
560 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
561 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
562 walk->rb_writing_pos, walk->reading_pos,
563 walk == queue->current ? "**y**" : " n ");
567 /* clear all the downloaded ranges */
569 clean_ranges (GstQueue2 * queue)
571 GST_DEBUG_OBJECT (queue, "clean queue ranges");
573 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
574 queue->ranges = NULL;
575 queue->current = NULL;
578 /* find a range that contains @offset or NULL when nothing does */
579 static GstQueue2Range *
580 find_range (GstQueue2 * queue, guint64 offset)
582 GstQueue2Range *range = NULL;
583 GstQueue2Range *walk;
585 /* first do a quick check for the current range */
586 for (walk = queue->ranges; walk; walk = walk->next) {
587 if (offset >= walk->offset && offset <= walk->writing_pos) {
588 /* we can reuse an existing range */
594 GST_DEBUG_OBJECT (queue,
595 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
596 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
598 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
604 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
606 guint64 max_reading_pos, writing_pos;
608 writing_pos = range->writing_pos;
609 max_reading_pos = range->max_reading_pos;
611 if (writing_pos > max_reading_pos)
612 queue->cur_level.bytes = writing_pos - max_reading_pos;
614 queue->cur_level.bytes = 0;
617 /* make a new range for @offset or reuse an existing range */
618 static GstQueue2Range *
619 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
621 GstQueue2Range *range, *prev, *next;
623 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
625 if ((range = find_range (queue, offset))) {
626 GST_DEBUG_OBJECT (queue,
627 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
629 if (update_existing && range->writing_pos != offset) {
630 GST_DEBUG_OBJECT (queue, "updating range writing position to "
631 "%" G_GUINT64_FORMAT, offset);
632 range->writing_pos = offset;
635 GST_DEBUG_OBJECT (queue,
636 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
638 range = g_slice_new0 (GstQueue2Range);
639 range->offset = offset;
640 /* we want to write to the next location in the ring buffer */
641 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
642 range->writing_pos = offset;
643 range->rb_writing_pos = range->rb_offset;
644 range->reading_pos = offset;
645 range->max_reading_pos = offset;
649 next = queue->ranges;
651 if (next->offset > offset) {
652 /* insert before next */
653 GST_DEBUG_OBJECT (queue,
654 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
666 queue->ranges = range;
668 debug_ranges (queue);
670 /* update the stats for this range */
671 update_cur_level (queue, range);
677 /* clear and init the download ranges for offset 0 */
679 init_ranges (GstQueue2 * queue)
681 GST_DEBUG_OBJECT (queue, "init queue ranges");
683 /* get rid of all the current ranges */
684 clean_ranges (queue);
685 /* make a range for offset 0 */
686 queue->current = add_range (queue, 0, TRUE);
689 /* calculate the diff between running time on the sink and src of the queue.
690 * This is the total amount of time in the queue. */
692 update_time_level (GstQueue2 * queue)
694 if (queue->sink_tainted) {
696 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
697 queue->sink_segment.position);
698 queue->sink_tainted = FALSE;
701 if (queue->src_tainted) {
703 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
704 queue->src_segment.position);
705 queue->src_tainted = FALSE;
708 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
709 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
711 if (queue->sinktime != GST_CLOCK_TIME_NONE
712 && queue->srctime != GST_CLOCK_TIME_NONE
713 && queue->sinktime >= queue->srctime)
714 queue->cur_level.time = queue->sinktime - queue->srctime;
716 queue->cur_level.time = 0;
719 /* take a SEGMENT event and apply the values to segment, updating the time
722 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
725 gst_event_copy_segment (event, segment);
727 if (segment->format == GST_FORMAT_BYTES) {
728 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
729 /* start is where we'll be getting from and as such writing next */
730 queue->current = add_range (queue, segment->start, TRUE);
734 /* now configure the values, we use these to track timestamps on the
736 if (segment->format != GST_FORMAT_TIME) {
737 /* non-time format, pretend the current time segment is closed with a
738 * 0 start and unknown stop time. */
739 segment->format = GST_FORMAT_TIME;
745 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
748 queue->sink_tainted = TRUE;
750 queue->src_tainted = TRUE;
752 /* segment can update the time level of the queue */
753 update_time_level (queue);
757 apply_gap (GstQueue2 * queue, GstEvent * event,
758 GstSegment * segment, gboolean is_sink)
760 GstClockTime timestamp;
761 GstClockTime duration;
763 gst_event_parse_gap (event, ×tamp, &duration);
765 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
767 if (GST_CLOCK_TIME_IS_VALID (duration)) {
768 timestamp += duration;
771 segment->position = timestamp;
774 queue->sink_tainted = TRUE;
776 queue->src_tainted = TRUE;
778 /* calc diff with other end */
779 update_time_level (queue);
783 /* take a buffer and update segment, updating the time level of the queue. */
785 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
788 GstClockTime duration, timestamp;
790 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
791 duration = GST_BUFFER_DURATION (buffer);
793 /* if no timestamp is set, assume it's continuous with the previous
795 if (timestamp == GST_CLOCK_TIME_NONE)
796 timestamp = segment->position;
799 if (duration != GST_CLOCK_TIME_NONE)
800 timestamp += duration;
802 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
803 GST_TIME_ARGS (timestamp));
805 segment->position = timestamp;
808 queue->sink_tainted = TRUE;
810 queue->src_tainted = TRUE;
812 /* calc diff with other end */
813 update_time_level (queue);
817 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
819 GstClockTime *timestamp = data;
822 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
823 " duration %" GST_TIME_FORMAT, idx,
824 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
825 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
826 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
828 btime = GST_BUFFER_DTS_OR_PTS (*buf);
829 if (GST_CLOCK_TIME_IS_VALID (btime))
832 if (GST_BUFFER_DURATION_IS_VALID (*buf))
833 *timestamp += GST_BUFFER_DURATION (*buf);
835 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
839 /* take a buffer list and update segment, updating the time level of the queue */
841 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
842 GstSegment * segment, gboolean is_sink)
844 GstClockTime timestamp;
846 /* if no timestamp is set, assume it's continuous with the previous time */
847 timestamp = segment->position;
849 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
851 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
852 GST_TIME_ARGS (timestamp));
854 segment->position = timestamp;
857 queue->sink_tainted = TRUE;
859 queue->src_tainted = TRUE;
861 /* calc diff with other end */
862 update_time_level (queue);
866 get_percent (guint64 cur_level, guint64 max_level, guint64 alt_max)
874 p = gst_util_uint64_scale (cur_level, 100, MIN (max_level, alt_max));
876 p = gst_util_uint64_scale (cur_level, 100, max_level);
882 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
887 if (queue->high_percent <= 0) {
891 *is_buffering = FALSE;
894 #define GET_PERCENT(format,alt_max) \
895 get_percent(queue->cur_level.format,queue->max_level.format,(alt_max))
898 /* on EOS we are always 100% full, we set the var here so that it we can
899 * reuse the logic below to stop buffering */
901 GST_LOG_OBJECT (queue, "we are EOS");
903 GST_LOG_OBJECT (queue,
904 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
905 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
906 queue->cur_level.buffers);
908 /* figure out the percent we are filled, we take the max of all formats. */
909 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
910 perc = GET_PERCENT (bytes, 0);
912 guint64 rb_size = queue->ring_buffer_max_size;
913 perc = GET_PERCENT (bytes, rb_size);
915 perc = MAX (perc, GET_PERCENT (time, 0));
916 perc = MAX (perc, GET_PERCENT (buffers, 0));
918 /* also apply the rate estimate when we need to */
919 if (queue->use_rate_estimate)
920 perc = MAX (perc, GET_PERCENT (rate_time, 0));
922 /* Don't get to 0% unless we're really empty */
923 if (queue->cur_level.bytes > 0)
924 perc = MAX (1, perc);
929 *is_buffering = queue->is_buffering;
931 /* scale to high percent so that it becomes the 100% mark */
932 perc = perc * 100 / queue->high_percent;
940 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
947 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
948 gint * avg_in, gint * avg_out, gint64 * buffering_left)
951 if (!QUEUE_IS_USING_QUEUE (queue)) {
952 if (QUEUE_IS_USING_RING_BUFFER (queue))
953 *mode = GST_BUFFERING_TIMESHIFT;
955 *mode = GST_BUFFERING_DOWNLOAD;
957 *mode = GST_BUFFERING_STREAM;
962 *avg_in = queue->byte_in_rate;
964 *avg_out = queue->byte_out_rate;
966 if (buffering_left) {
967 *buffering_left = (percent == 100 ? 0 : -1);
969 if (queue->use_rate_estimate) {
972 max = queue->max_level.rate_time;
973 cur = queue->cur_level.rate_time;
975 if (percent != 100 && max > cur)
976 *buffering_left = (max - cur) / 1000000;
982 gst_queue2_post_buffering (GstQueue2 * queue)
984 GstMessage *msg = NULL;
986 g_mutex_lock (&queue->buffering_post_lock);
987 GST_QUEUE2_MUTEX_LOCK (queue);
988 if (queue->percent_changed) {
989 gint percent = queue->buffering_percent;
991 queue->percent_changed = FALSE;
993 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
994 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
996 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
997 queue->avg_out, queue->buffering_left);
999 GST_QUEUE2_MUTEX_UNLOCK (queue);
1002 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1004 g_mutex_unlock (&queue->buffering_post_lock);
1008 update_buffering (GstQueue2 * queue)
1012 /* Ensure the variables used to calculate buffering state are up-to-date. */
1014 update_cur_level (queue, queue->current);
1015 update_in_rates (queue);
1017 if (!get_buffering_percent (queue, NULL, &percent))
1020 if (queue->is_buffering) {
1021 /* if we were buffering see if we reached the high watermark */
1023 queue->is_buffering = FALSE;
1025 SET_PERCENT (queue, percent);
1027 /* we were not buffering, check if we need to start buffering if we drop
1028 * below the low threshold */
1029 if (percent < queue->low_percent) {
1030 queue->is_buffering = TRUE;
1031 SET_PERCENT (queue, percent);
1037 reset_rate_timer (GstQueue2 * queue)
1039 queue->bytes_in = 0;
1040 queue->bytes_out = 0;
1041 queue->byte_in_rate = 0.0;
1042 queue->byte_in_period = 0;
1043 queue->byte_out_rate = 0.0;
1044 queue->last_update_in_rates_elapsed = 0.0;
1045 queue->last_in_elapsed = 0.0;
1046 queue->last_out_elapsed = 0.0;
1047 queue->in_timer_started = FALSE;
1048 queue->out_timer_started = FALSE;
1051 /* the interval in seconds to recalculate the rate */
1052 #define RATE_INTERVAL 0.2
1053 /* Tuning for rate estimation. We use a large window for the input rate because
1054 * it should be stable when connected to a network. The output rate is less
1055 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1056 * therefore adapt more quickly.
1057 * However, initial input rate may be subject to a burst, and should therefore
1058 * initially also adapt more quickly to changes, and only later on give higher
1059 * weight to previous values. */
1060 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1061 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1064 update_in_rates (GstQueue2 * queue)
1066 gdouble elapsed, period;
1067 gdouble byte_in_rate;
1069 if (!queue->in_timer_started) {
1070 queue->in_timer_started = TRUE;
1071 g_timer_start (queue->in_timer);
1075 queue->last_update_in_rates_elapsed = elapsed =
1076 g_timer_elapsed (queue->in_timer, NULL);
1078 /* recalc after each interval. */
1079 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1080 period = elapsed - queue->last_in_elapsed;
1082 GST_DEBUG_OBJECT (queue,
1083 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1084 period, queue->bytes_in, queue->byte_in_period);
1086 byte_in_rate = queue->bytes_in / period;
1088 if (queue->byte_in_rate == 0.0)
1089 queue->byte_in_rate = byte_in_rate;
1091 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1092 (double) queue->byte_in_period, period);
1094 /* another data point, cap at 16 for long time running average */
1095 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1096 queue->byte_in_period += period;
1098 /* reset the values to calculate rate over the next interval */
1099 queue->last_in_elapsed = elapsed;
1100 queue->bytes_in = 0;
1103 if (queue->byte_in_rate > 0.0) {
1104 queue->cur_level.rate_time =
1105 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1107 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1108 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1112 update_out_rates (GstQueue2 * queue)
1114 gdouble elapsed, period;
1115 gdouble byte_out_rate;
1117 if (!queue->out_timer_started) {
1118 queue->out_timer_started = TRUE;
1119 g_timer_start (queue->out_timer);
1123 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1125 /* recalc after each interval. */
1126 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1127 period = elapsed - queue->last_out_elapsed;
1129 GST_DEBUG_OBJECT (queue,
1130 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1132 byte_out_rate = queue->bytes_out / period;
1134 if (queue->byte_out_rate == 0.0)
1135 queue->byte_out_rate = byte_out_rate;
1137 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1139 /* reset the values to calculate rate over the next interval */
1140 queue->last_out_elapsed = elapsed;
1141 queue->bytes_out = 0;
1143 if (queue->byte_in_rate > 0.0) {
1144 queue->cur_level.rate_time =
1145 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1147 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1148 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1152 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1154 guint64 reading_pos, max_reading_pos;
1157 max_reading_pos = range->max_reading_pos;
1159 max_reading_pos = MAX (max_reading_pos, reading_pos);
1161 GST_DEBUG_OBJECT (queue,
1162 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1163 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1164 range->max_reading_pos = max_reading_pos;
1166 update_cur_level (queue, range);
1170 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1175 /* until we receive the FLUSH_STOP from this seek, we skip data */
1176 queue->seeking = TRUE;
1177 GST_QUEUE2_MUTEX_UNLOCK (queue);
1179 debug_ranges (queue);
1181 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1184 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1185 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1186 GST_SEEK_TYPE_NONE, -1);
1188 res = gst_pad_push_event (queue->sinkpad, event);
1189 GST_QUEUE2_MUTEX_LOCK (queue);
1192 /* Between us sending the seek event and re-acquiring the lock, the source
1193 * thread might already have pushed data and moved along the range's
1194 * writing_pos beyond the seek offset. In that case we don't want to set
1195 * the writing position back to the requested seek position, as it would
1196 * cause data to be written to the wrong offset in the file or ring buffer.
1197 * We still do the add_range call to switch the current range to the
1198 * requested range, or create one if one doesn't exist yet. */
1199 queue->current = add_range (queue, offset, FALSE);
1205 /* get the threshold for when we decide to seek rather than wait */
1207 get_seek_threshold (GstQueue2 * queue)
1211 /* FIXME, find a good threshold based on the incoming rate. */
1212 threshold = 1024 * 512;
1214 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1215 threshold = MIN (threshold,
1216 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1221 /* see if there is enough data in the file to read a full buffer */
1223 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1225 GstQueue2Range *range;
1227 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1230 if ((range = find_range (queue, offset))) {
1231 if (queue->current != range) {
1232 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1233 perform_seek_to_offset (queue, range->writing_pos);
1236 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1237 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1239 /* we have a range for offset */
1240 GST_DEBUG_OBJECT (queue,
1241 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1242 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1244 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1247 if (offset + length <= range->writing_pos)
1250 GST_DEBUG_OBJECT (queue,
1251 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1252 (offset + length) - range->writing_pos);
1255 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1256 " len %u", offset, length);
1257 /* we don't have the range, see how far away we are */
1258 if (!queue->is_eos && queue->current) {
1259 guint64 threshold = get_seek_threshold (queue);
1261 if (offset >= queue->current->offset && offset <=
1262 queue->current->writing_pos + threshold) {
1263 GST_INFO_OBJECT (queue,
1264 "requested data is within range, wait for data");
1269 /* too far away, do a seek */
1270 perform_seek_to_offset (queue, offset);
1277 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1278 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1279 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1281 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1284 static GstFlowReturn
1285 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1286 guint8 * dst, gint64 * read_return)
1288 guint8 *ring_buffer;
1291 ring_buffer = queue->ring_buffer;
1293 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1296 /* this should not block */
1297 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1299 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1300 res = fread (dst, 1, length, queue->temp_file);
1302 memcpy (dst, ring_buffer + offset, length);
1306 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1308 if (G_UNLIKELY (res < length)) {
1309 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1310 goto could_not_read;
1311 /* check for errors or EOF */
1312 if (ferror (queue->temp_file))
1313 goto could_not_read;
1314 if (feof (queue->temp_file) && length > 0)
1324 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1325 return GST_FLOW_ERROR;
1329 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1330 return GST_FLOW_ERROR;
1334 GST_DEBUG ("non-regular file hits EOS");
1335 return GST_FLOW_EOS;
1339 static GstFlowReturn
1340 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1341 GstBuffer ** buffer)
1346 guint64 file_offset;
1347 guint block_length, remaining, read_length;
1351 GstFlowReturn ret = GST_FLOW_OK;
1353 /* allocate the output buffer of the requested size */
1354 if (*buffer == NULL)
1355 buf = gst_buffer_new_allocate (NULL, length, NULL);
1359 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1362 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1366 rb_size = queue->ring_buffer_max_size;
1367 max_size = QUEUE_MAX_BYTES (queue);
1370 while (remaining > 0) {
1371 /* configure how much/whether to read */
1372 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1375 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1378 /* calculate how far away the offset is */
1379 if (queue->current->writing_pos > rpos)
1380 level = queue->current->writing_pos - rpos;
1384 GST_DEBUG_OBJECT (queue,
1385 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1386 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1387 rpos, queue->current->writing_pos, level, max_size);
1389 if (level >= max_size) {
1390 /* we don't have the data but if we have a ring buffer that is full, we
1392 GST_DEBUG_OBJECT (queue,
1393 "ring buffer full, reading QUEUE_MAX_BYTES %"
1394 G_GUINT64_FORMAT " bytes", max_size);
1395 read_length = max_size;
1396 } else if (queue->is_eos) {
1397 /* won't get any more data so read any data we have */
1399 GST_DEBUG_OBJECT (queue,
1400 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1402 read_length = level;
1410 if (read_length == 0) {
1411 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1412 GST_DEBUG_OBJECT (queue,
1413 "update current position [%" G_GUINT64_FORMAT "-%"
1414 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1415 update_cur_pos (queue, queue->current, rpos);
1416 GST_QUEUE2_SIGNAL_DEL (queue);
1419 if (queue->use_buffering)
1420 update_buffering (queue);
1422 GST_DEBUG_OBJECT (queue, "waiting for add");
1423 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1427 /* we have the requested data so read it */
1428 read_length = remaining;
1431 /* set range reading_pos to actual reading position for this read */
1432 queue->current->reading_pos = rpos;
1434 /* configure how much and from where to read */
1435 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1437 (queue->current->rb_offset + (rpos -
1438 queue->current->offset)) % rb_size;
1439 if (file_offset + read_length > rb_size) {
1440 block_length = rb_size - file_offset;
1442 block_length = read_length;
1446 block_length = read_length;
1449 /* while we still have data to read, we loop */
1450 while (read_length > 0) {
1454 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1455 data, &read_return);
1456 if (ret != GST_FLOW_OK)
1459 file_offset += read_return;
1460 if (QUEUE_IS_USING_RING_BUFFER (queue))
1461 file_offset %= rb_size;
1463 data += read_return;
1464 read_length -= read_return;
1465 block_length = read_length;
1466 remaining -= read_return;
1468 rpos = (queue->current->reading_pos += read_return);
1469 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1471 GST_QUEUE2_SIGNAL_DEL (queue);
1472 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1475 gst_buffer_unmap (buf, &info);
1476 gst_buffer_resize (buf, 0, length);
1478 GST_BUFFER_OFFSET (buf) = offset;
1479 GST_BUFFER_OFFSET_END (buf) = offset + length;
1488 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1489 gst_buffer_unmap (buf, &info);
1490 if (*buffer == NULL)
1491 gst_buffer_unref (buf);
1492 return GST_FLOW_EOS;
1496 GST_DEBUG_OBJECT (queue, "we are flushing");
1497 gst_buffer_unmap (buf, &info);
1498 if (*buffer == NULL)
1499 gst_buffer_unref (buf);
1500 return GST_FLOW_FLUSHING;
1504 GST_DEBUG_OBJECT (queue, "we have a read error");
1505 gst_buffer_unmap (buf, &info);
1506 if (*buffer == NULL)
1507 gst_buffer_unref (buf);
1512 /* should be called with QUEUE_LOCK */
1513 static GstMiniObject *
1514 gst_queue2_read_item_from_file (GstQueue2 * queue)
1516 GstMiniObject *item;
1518 if (queue->stream_start_event != NULL) {
1519 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1520 queue->stream_start_event = NULL;
1521 } else if (queue->starting_segment != NULL) {
1522 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1523 queue->starting_segment = NULL;
1526 GstBuffer *buffer = NULL;
1527 guint64 reading_pos;
1529 reading_pos = queue->current->reading_pos;
1532 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1537 item = GST_MINI_OBJECT_CAST (buffer);
1540 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1550 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1551 * the temp filename. */
1553 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1558 if (queue->temp_file)
1559 goto already_opened;
1561 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1563 /* If temp_template was set, allocate a filename and open that file */
1566 if (queue->temp_template == NULL)
1569 /* make copy of the template, we don't want to change this */
1570 name = g_strdup (queue->temp_template);
1573 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1575 fd = g_mkstemp (name);
1579 goto mkstemp_failed;
1581 /* open the file for update/writing */
1582 queue->temp_file = fdopen (fd, "wb+");
1583 /* error creating file */
1584 if (queue->temp_file == NULL)
1587 g_free (queue->temp_location);
1588 queue->temp_location = name;
1590 GST_QUEUE2_MUTEX_UNLOCK (queue);
1592 /* we can't emit the notify with the lock */
1593 g_object_notify (G_OBJECT (queue), "temp-location");
1595 GST_QUEUE2_MUTEX_LOCK (queue);
1597 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1604 GST_DEBUG_OBJECT (queue, "temp file was already open");
1609 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1610 (_("No Temp directory specified.")), (NULL));
1615 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1616 (_("Could not create temp file \"%s\"."), queue->temp_template),
1623 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1624 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1633 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1636 if (queue->temp_file == NULL)
1639 GST_DEBUG_OBJECT (queue, "closing temp file");
1641 fflush (queue->temp_file);
1642 fclose (queue->temp_file);
1644 if (queue->temp_remove) {
1645 if (remove (queue->temp_location) < 0) {
1646 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1647 queue->temp_location, g_strerror (errno));
1651 queue->temp_file = NULL;
1652 clean_ranges (queue);
1656 gst_queue2_flush_temp_file (GstQueue2 * queue)
1658 if (queue->temp_file == NULL)
1661 GST_DEBUG_OBJECT (queue, "flushing temp file");
1663 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1667 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1669 if (!QUEUE_IS_USING_QUEUE (queue)) {
1670 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1671 gst_queue2_flush_temp_file (queue);
1672 init_ranges (queue);
1674 while (!g_queue_is_empty (&queue->queue)) {
1675 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1677 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1678 && GST_EVENT_IS_STICKY (qitem->item)
1679 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1680 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1681 gst_pad_store_sticky_event (queue->srcpad,
1682 GST_EVENT_CAST (qitem->item));
1685 /* Then lose another reference because we are supposed to destroy that
1686 data when flushing */
1687 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1688 gst_mini_object_unref (qitem->item);
1689 g_slice_free (GstQueue2Item, qitem);
1692 queue->last_query = FALSE;
1693 g_cond_signal (&queue->query_handled);
1694 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1695 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1696 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1697 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1698 queue->sink_tainted = queue->src_tainted = TRUE;
1699 if (queue->starting_segment != NULL)
1700 gst_event_unref (queue->starting_segment);
1701 queue->starting_segment = NULL;
1702 queue->segment_event_received = FALSE;
1703 gst_event_replace (&queue->stream_start_event, NULL);
1705 /* we deleted a lot of something */
1706 GST_QUEUE2_SIGNAL_DEL (queue);
1710 gst_queue2_wait_free_space (GstQueue2 * queue)
1712 /* We make space available if we're "full" according to whatever
1713 * the user defined as "full". */
1714 if (gst_queue2_is_filled (queue)) {
1717 /* pause the timer while we wait. The fact that we are waiting does not mean
1718 * the byterate on the input pad is lower */
1719 if ((started = queue->in_timer_started))
1720 g_timer_stop (queue->in_timer);
1722 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1723 "queue is full, waiting for free space");
1725 GST_QUEUE2_MUTEX_UNLOCK (queue);
1726 g_signal_emit (queue, gst_queue2_signals[SIGNAL_OVERRUN], 0);
1727 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
1728 /* we recheck, the signal could have changed the thresholds */
1729 if (!gst_queue2_is_filled (queue))
1732 /* Wait for space to be available, we could be unlocked because of a flush. */
1733 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1735 while (gst_queue2_is_filled (queue));
1737 /* and continue if we were running before */
1739 g_timer_continue (queue->in_timer);
1746 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1752 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1755 guint8 *data, *ring_buffer;
1756 guint size, rb_size;
1757 guint64 writing_pos, new_writing_pos;
1758 GstQueue2Range *range, *prev, *next;
1759 gboolean do_seek = FALSE;
1761 if (QUEUE_IS_USING_RING_BUFFER (queue))
1762 writing_pos = queue->current->rb_writing_pos;
1764 writing_pos = queue->current->writing_pos;
1765 ring_buffer = queue->ring_buffer;
1766 rb_size = queue->ring_buffer_max_size;
1768 gst_buffer_map (buffer, &info, GST_MAP_READ);
1773 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1777 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1778 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1779 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1780 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1781 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1787 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1790 /* calculate the space in the ring buffer not used by data from
1791 * the current range */
1792 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1793 /* wait until there is some free space */
1794 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1796 /* get the amount of space we have */
1797 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1799 /* calculate if we need to split or if we can write the entire
1801 to_write = MIN (size, space);
1803 /* the writing position in the ring buffer after writing (part
1804 * or all of) the buffer */
1805 new_writing_pos = (writing_pos + to_write) % rb_size;
1808 range = queue->ranges;
1810 /* if we need to overwrite data in the ring buffer, we need to
1813 * warning: this code is complicated and includes some
1814 * simplifications - pen, paper and diagrams for the cases
1817 guint64 range_data_start, range_data_end;
1818 GstQueue2Range *range_to_destroy = NULL;
1820 range_data_start = range->rb_offset;
1821 range_data_end = range->rb_writing_pos;
1823 /* handle the special case where the range has no data in it */
1824 if (range->writing_pos == range->offset) {
1825 if (range != queue->current) {
1826 GST_DEBUG_OBJECT (queue,
1827 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1828 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1830 range_to_destroy = range;
1832 prev->next = range->next;
1837 if (range_data_end > range_data_start) {
1838 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1841 if (new_writing_pos > range_data_start) {
1842 if (new_writing_pos >= range_data_end) {
1843 GST_DEBUG_OBJECT (queue,
1844 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1845 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1847 range_to_destroy = range;
1849 prev->next = range->next;
1851 GST_DEBUG_OBJECT (queue,
1852 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1853 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1854 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1855 range->offset + new_writing_pos - range_data_start,
1857 range->offset += (new_writing_pos - range_data_start);
1858 range->rb_offset = new_writing_pos;
1862 guint64 new_wpos_virt = writing_pos + to_write;
1864 if (new_wpos_virt <= range_data_start)
1867 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1868 GST_DEBUG_OBJECT (queue,
1869 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1870 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1872 range_to_destroy = range;
1874 prev->next = range->next;
1876 GST_DEBUG_OBJECT (queue,
1877 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1878 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1879 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1880 range->offset + new_writing_pos - range_data_start,
1882 range->offset += (new_wpos_virt - range_data_start);
1883 range->rb_offset = new_writing_pos;
1888 if (!range_to_destroy)
1891 range = range->next;
1892 if (range_to_destroy) {
1893 if (range_to_destroy == queue->ranges)
1894 queue->ranges = range;
1895 g_slice_free (GstQueue2Range, range_to_destroy);
1896 range_to_destroy = NULL;
1901 new_writing_pos = writing_pos + to_write;
1904 if (QUEUE_IS_USING_TEMP_FILE (queue)
1905 && FSEEK_FILE (queue->temp_file, writing_pos))
1908 if (new_writing_pos > writing_pos) {
1909 GST_INFO_OBJECT (queue,
1910 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1911 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1912 queue->current->writing_pos, queue->current->rb_writing_pos);
1913 /* either not using ring buffer or no wrapping, just write */
1914 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1915 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1918 memcpy (ring_buffer + writing_pos, data, to_write);
1921 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1922 /* try to merge with next range */
1923 while ((next = queue->current->next)) {
1924 GST_INFO_OBJECT (queue,
1925 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1926 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1927 if (new_writing_pos < next->offset)
1930 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1933 /* remove the group */
1934 queue->current->next = next->next;
1936 /* We use the threshold to decide if we want to do a seek or simply
1937 * read the data again. If there is not so much data in the range we
1938 * prefer to avoid to seek and read it again. */
1939 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1940 /* the new range had more data than the threshold, it's worth keeping
1941 * it and doing a seek. */
1942 new_writing_pos = next->writing_pos;
1945 g_slice_free (GstQueue2Range, next);
1947 goto update_and_signal;
1951 guint block_one, block_two;
1953 block_one = rb_size - writing_pos;
1954 block_two = to_write - block_one;
1956 if (block_one > 0) {
1957 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1958 /* write data to end of ring buffer */
1959 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1960 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1963 memcpy (ring_buffer + writing_pos, data, block_one);
1967 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1970 if (block_two > 0) {
1971 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1972 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1973 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1976 memcpy (ring_buffer, data + block_one, block_two);
1982 /* update the writing positions */
1984 GST_INFO_OBJECT (queue,
1985 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1986 to_write, writing_pos, size);
1988 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1990 queue->current->writing_pos += to_write;
1991 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1993 queue->current->writing_pos = writing_pos = new_writing_pos;
1996 perform_seek_to_offset (queue, new_writing_pos);
1998 update_cur_level (queue, queue->current);
2000 /* update the buffering status */
2001 if (queue->use_buffering)
2002 update_buffering (queue);
2004 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2005 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2007 GST_QUEUE2_SIGNAL_ADD (queue);
2010 gst_buffer_unmap (buffer, &info);
2017 GST_DEBUG_OBJECT (queue, "we are flushing");
2018 gst_buffer_unmap (buffer, &info);
2019 /* FIXME - GST_FLOW_EOS ? */
2024 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2025 gst_buffer_unmap (buffer, &info);
2032 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2036 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2037 (_("Error while writing to download file.")),
2038 ("%s", g_strerror (errno)));
2041 gst_buffer_unmap (buffer, &info);
2047 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2049 GstQueue2 *queue = q;
2051 GST_TRACE_OBJECT (queue,
2052 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2053 gst_buffer_get_size (*buf));
2055 if (!gst_queue2_create_write (queue, *buf)) {
2056 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2063 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2065 guint *p_size = data;
2068 buf_size = gst_buffer_get_size (*buf);
2069 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2070 *p_size += buf_size;
2074 /* enqueue an item an update the level stats */
2076 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2077 GstQueue2ItemType item_type)
2079 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2083 buffer = GST_BUFFER_CAST (item);
2084 size = gst_buffer_get_size (buffer);
2086 /* add buffer to the statistics */
2087 if (QUEUE_IS_USING_QUEUE (queue)) {
2088 queue->cur_level.buffers++;
2089 queue->cur_level.bytes += size;
2091 queue->bytes_in += size;
2093 /* apply new buffer to segment stats */
2094 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
2095 /* update the byterate stats */
2096 update_in_rates (queue);
2098 if (!QUEUE_IS_USING_QUEUE (queue)) {
2099 /* FIXME - check return value? */
2100 gst_queue2_create_write (queue, buffer);
2102 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2103 GstBufferList *buffer_list;
2106 buffer_list = GST_BUFFER_LIST_CAST (item);
2108 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2109 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2111 /* add buffer to the statistics */
2112 if (QUEUE_IS_USING_QUEUE (queue)) {
2113 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2114 queue->cur_level.bytes += size;
2116 queue->bytes_in += size;
2118 /* apply new buffer to segment stats */
2119 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2121 /* update the byterate stats */
2122 update_in_rates (queue);
2124 if (!QUEUE_IS_USING_QUEUE (queue)) {
2125 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2127 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2130 event = GST_EVENT_CAST (item);
2132 switch (GST_EVENT_TYPE (event)) {
2134 /* Zero the thresholds, this makes sure the queue is completely
2135 * filled and we can read all data from the queue. */
2136 GST_DEBUG_OBJECT (queue, "we have EOS");
2137 queue->is_eos = TRUE;
2139 case GST_EVENT_SEGMENT:
2140 apply_segment (queue, event, &queue->sink_segment, TRUE);
2141 /* This is our first new segment, we hold it
2142 * as we can't save it on the temp file */
2143 if (!QUEUE_IS_USING_QUEUE (queue)) {
2144 if (queue->segment_event_received)
2145 goto unexpected_event;
2147 queue->segment_event_received = TRUE;
2148 if (queue->starting_segment != NULL)
2149 gst_event_unref (queue->starting_segment);
2150 queue->starting_segment = event;
2153 /* a new segment allows us to accept more buffers if we got EOS
2154 * from downstream */
2155 queue->unexpected = FALSE;
2158 apply_gap (queue, event, &queue->sink_segment, TRUE);
2160 case GST_EVENT_STREAM_START:
2161 if (!QUEUE_IS_USING_QUEUE (queue)) {
2162 gst_event_replace (&queue->stream_start_event, event);
2163 gst_event_unref (event);
2167 case GST_EVENT_CAPS:{
2170 gst_event_parse_caps (event, &caps);
2171 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2173 if (!QUEUE_IS_USING_QUEUE (queue)) {
2174 GST_LOG ("Dropping caps event, not using queue");
2175 gst_event_unref (event);
2181 if (!QUEUE_IS_USING_QUEUE (queue))
2182 goto unexpected_event;
2185 } else if (GST_IS_QUERY (item)) {
2186 /* Can't happen as we check that in the caller */
2187 if (!QUEUE_IS_USING_QUEUE (queue))
2188 g_assert_not_reached ();
2190 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2191 item, GST_OBJECT_NAME (queue));
2192 /* we can't really unref since we don't know what it is */
2197 /* update the buffering status */
2198 if (queue->use_buffering)
2199 update_buffering (queue);
2201 if (QUEUE_IS_USING_QUEUE (queue)) {
2202 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2203 qitem->type = item_type;
2205 g_queue_push_tail (&queue->queue, qitem);
2207 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2210 GST_QUEUE2_SIGNAL_ADD (queue);
2218 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2220 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2221 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2222 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2223 gst_event_unref (GST_EVENT_CAST (item));
2228 /* dequeue an item from the queue and update level stats */
2229 static GstMiniObject *
2230 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2232 GstMiniObject *item;
2234 if (!QUEUE_IS_USING_QUEUE (queue)) {
2235 item = gst_queue2_read_item_from_file (queue);
2237 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2243 g_slice_free (GstQueue2Item, qitem);
2249 if (GST_IS_BUFFER (item)) {
2253 buffer = GST_BUFFER_CAST (item);
2254 size = gst_buffer_get_size (buffer);
2255 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2257 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2258 "retrieved buffer %p from queue", buffer);
2260 if (QUEUE_IS_USING_QUEUE (queue)) {
2261 queue->cur_level.buffers--;
2262 queue->cur_level.bytes -= size;
2264 queue->bytes_out += size;
2266 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2267 /* update the byterate stats */
2268 update_out_rates (queue);
2269 /* update the buffering */
2270 if (queue->use_buffering)
2271 update_buffering (queue);
2273 } else if (GST_IS_EVENT (item)) {
2274 GstEvent *event = GST_EVENT_CAST (item);
2276 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2278 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2279 "retrieved event %p from queue", event);
2281 switch (GST_EVENT_TYPE (event)) {
2283 /* queue is empty now that we dequeued the EOS */
2284 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2286 case GST_EVENT_SEGMENT:
2287 apply_segment (queue, event, &queue->src_segment, FALSE);
2290 apply_gap (queue, event, &queue->src_segment, FALSE);
2295 } else if (GST_IS_BUFFER_LIST (item)) {
2296 GstBufferList *buffer_list;
2299 buffer_list = GST_BUFFER_LIST_CAST (item);
2300 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2301 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2303 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2304 "retrieved buffer list %p from queue", buffer_list);
2306 if (QUEUE_IS_USING_QUEUE (queue)) {
2307 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2308 queue->cur_level.bytes -= size;
2310 queue->bytes_out += size;
2312 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2313 /* update the byterate stats */
2314 update_out_rates (queue);
2315 /* update the buffering */
2316 if (queue->use_buffering)
2317 update_buffering (queue);
2318 } else if (GST_IS_QUERY (item)) {
2319 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2320 "retrieved query %p from queue", item);
2321 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2324 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2325 item, GST_OBJECT_NAME (queue));
2327 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2329 GST_QUEUE2_SIGNAL_DEL (queue);
2336 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2342 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2345 gboolean ret = TRUE;
2348 queue = GST_QUEUE2 (parent);
2350 switch (GST_EVENT_TYPE (event)) {
2351 case GST_EVENT_FLUSH_START:
2353 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2354 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2356 ret = gst_pad_push_event (queue->srcpad, event);
2358 /* now unblock the chain function */
2359 GST_QUEUE2_MUTEX_LOCK (queue);
2360 queue->srcresult = GST_FLOW_FLUSHING;
2361 queue->sinkresult = GST_FLOW_FLUSHING;
2362 /* unblock the loop and chain functions */
2363 GST_QUEUE2_SIGNAL_ADD (queue);
2364 GST_QUEUE2_SIGNAL_DEL (queue);
2365 queue->last_query = FALSE;
2366 g_cond_signal (&queue->query_handled);
2367 GST_QUEUE2_MUTEX_UNLOCK (queue);
2369 /* make sure it pauses, this should happen since we sent
2370 * flush_start downstream. */
2371 gst_pad_pause_task (queue->srcpad);
2372 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2374 GST_QUEUE2_MUTEX_LOCK (queue);
2375 /* flush the sink pad */
2376 queue->sinkresult = GST_FLOW_FLUSHING;
2377 GST_QUEUE2_SIGNAL_DEL (queue);
2378 queue->last_query = FALSE;
2379 g_cond_signal (&queue->query_handled);
2380 GST_QUEUE2_MUTEX_UNLOCK (queue);
2382 gst_event_unref (event);
2386 case GST_EVENT_FLUSH_STOP:
2388 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2390 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2392 ret = gst_pad_push_event (queue->srcpad, event);
2394 GST_QUEUE2_MUTEX_LOCK (queue);
2395 gst_queue2_locked_flush (queue, FALSE, TRUE);
2396 queue->srcresult = GST_FLOW_OK;
2397 queue->sinkresult = GST_FLOW_OK;
2398 queue->is_eos = FALSE;
2399 queue->unexpected = FALSE;
2400 queue->seeking = FALSE;
2401 /* reset rate counters */
2402 reset_rate_timer (queue);
2403 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2404 queue->srcpad, NULL);
2405 GST_QUEUE2_MUTEX_UNLOCK (queue);
2407 GST_QUEUE2_MUTEX_LOCK (queue);
2408 queue->segment_event_received = FALSE;
2409 queue->is_eos = FALSE;
2410 queue->unexpected = FALSE;
2411 queue->sinkresult = GST_FLOW_OK;
2412 queue->seeking = FALSE;
2413 GST_QUEUE2_MUTEX_UNLOCK (queue);
2415 gst_event_unref (event);
2420 if (GST_EVENT_IS_SERIALIZED (event)) {
2421 /* serialized events go in the queue */
2422 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2423 if (queue->srcresult != GST_FLOW_OK) {
2424 /* Errors in sticky event pushing are no problem and ignored here
2425 * as they will cause more meaningful errors during data flow.
2426 * For EOS events, that are not followed by data flow, we still
2427 * return FALSE here though and report an error.
2429 if (!GST_EVENT_IS_STICKY (event)) {
2430 goto out_flow_error;
2431 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2432 if (queue->srcresult == GST_FLOW_NOT_LINKED
2433 || queue->srcresult < GST_FLOW_EOS) {
2434 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2435 (_("Internal data flow error.")),
2436 ("streaming task paused, reason %s (%d)",
2437 gst_flow_get_name (queue->srcresult), queue->srcresult));
2439 goto out_flow_error;
2442 /* refuse more events on EOS */
2445 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2446 GST_QUEUE2_MUTEX_UNLOCK (queue);
2447 gst_queue2_post_buffering (queue);
2449 /* non-serialized events are passed upstream. */
2450 ret = gst_pad_push_event (queue->srcpad, event);
2459 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2460 GST_QUEUE2_MUTEX_UNLOCK (queue);
2461 gst_event_unref (event);
2466 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2467 GST_QUEUE2_MUTEX_UNLOCK (queue);
2468 gst_event_unref (event);
2473 GST_LOG_OBJECT (queue,
2474 "refusing event, we have a downstream flow error: %s",
2475 gst_flow_get_name (queue->srcresult));
2476 GST_QUEUE2_MUTEX_UNLOCK (queue);
2477 gst_event_unref (event);
2483 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2489 queue = GST_QUEUE2 (parent);
2491 switch (GST_QUERY_TYPE (query)) {
2493 if (GST_QUERY_IS_SERIALIZED (query)) {
2494 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2495 /* serialized events go in the queue. We need to be certain that we
2496 * don't cause deadlocks waiting for the query return value. We check if
2497 * the queue is empty (nothing is blocking downstream and the query can
2498 * be pushed for sure) or we are not buffering. If we are buffering,
2499 * the pipeline waits to unblock downstream until our queue fills up
2500 * completely, which can not happen if we block on the query..
2501 * Therefore we only potentially block when we are not buffering. */
2502 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2503 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2504 || !queue->use_buffering)) {
2505 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2506 gst_queue2_locked_enqueue (queue, query,
2507 GST_QUEUE2_ITEM_TYPE_QUERY);
2509 STATUS (queue, queue->sinkpad, "wait for QUERY");
2510 g_cond_wait (&queue->query_handled, &queue->qlock);
2511 if (queue->sinkresult != GST_FLOW_OK)
2513 res = queue->last_query;
2515 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2519 GST_DEBUG_OBJECT (queue,
2520 "refusing query, we are not using the queue");
2523 GST_QUEUE2_MUTEX_UNLOCK (queue);
2524 gst_queue2_post_buffering (queue);
2526 res = gst_pad_query_default (pad, parent, query);
2535 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2536 GST_QUEUE2_MUTEX_UNLOCK (queue);
2542 gst_queue2_is_empty (GstQueue2 * queue)
2544 /* never empty on EOS */
2548 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2549 return queue->current->writing_pos <= queue->current->max_reading_pos;
2551 if (queue->queue.length == 0)
2559 gst_queue2_is_filled (GstQueue2 * queue)
2563 /* always filled on EOS */
2567 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2568 (queue->cur_level.format) >= ((alt_max) ? \
2569 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2571 /* if using a ring buffer we're filled if all ring buffer space is used
2572 * _by the current range_ */
2573 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2574 guint64 rb_size = queue->ring_buffer_max_size;
2575 GST_DEBUG_OBJECT (queue,
2576 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2577 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2578 return CHECK_FILLED (bytes, rb_size);
2581 /* if using file, we're never filled if we don't have EOS */
2582 if (QUEUE_IS_USING_TEMP_FILE (queue))
2585 /* we are never filled when we have no buffers at all */
2586 if (queue->cur_level.buffers == 0)
2589 /* we are filled if one of the current levels exceeds the max */
2590 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2591 || CHECK_FILLED (time, 0);
2593 /* if we need to, use the rate estimate to check against the max time we are
2594 * allowed to queue */
2595 if (queue->use_rate_estimate)
2596 res |= CHECK_FILLED (rate_time, 0);
2602 static GstFlowReturn
2603 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2604 GstMiniObject * item, GstQueue2ItemType item_type)
2606 /* we have to lock the queue since we span threads */
2607 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2608 /* when we received EOS, we refuse more data */
2611 /* when we received unexpected from downstream, refuse more buffers */
2612 if (queue->unexpected)
2613 goto out_unexpected;
2615 /* while we didn't receive the newsegment, we're seeking and we skip data */
2619 if (!gst_queue2_wait_free_space (queue))
2622 /* put buffer in queue now */
2623 gst_queue2_locked_enqueue (queue, item, item_type);
2624 GST_QUEUE2_MUTEX_UNLOCK (queue);
2625 gst_queue2_post_buffering (queue);
2629 /* special conditions */
2632 GstFlowReturn ret = queue->sinkresult;
2634 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2635 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2636 GST_QUEUE2_MUTEX_UNLOCK (queue);
2637 gst_mini_object_unref (item);
2643 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2644 GST_QUEUE2_MUTEX_UNLOCK (queue);
2645 gst_mini_object_unref (item);
2647 return GST_FLOW_EOS;
2651 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2652 GST_QUEUE2_MUTEX_UNLOCK (queue);
2653 gst_mini_object_unref (item);
2659 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2660 GST_QUEUE2_MUTEX_UNLOCK (queue);
2661 gst_mini_object_unref (item);
2663 return GST_FLOW_EOS;
2667 static GstFlowReturn
2668 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2672 queue = GST_QUEUE2 (parent);
2674 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2675 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2676 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2677 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2678 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2680 return gst_queue2_chain_buffer_or_buffer_list (queue,
2681 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2684 static GstFlowReturn
2685 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2686 GstBufferList * buffer_list)
2690 queue = GST_QUEUE2 (parent);
2692 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2693 "received buffer list %p", buffer_list);
2695 return gst_queue2_chain_buffer_or_buffer_list (queue,
2696 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2699 static GstMiniObject *
2700 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2702 GstMiniObject *data;
2704 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2706 /* stop pushing buffers, we dequeue all items until we see an item that we
2707 * can push again, which is EOS or SEGMENT. If there is nothing in the
2708 * queue we can push, we set a flag to make the sinkpad refuse more
2709 * buffers with an EOS return value until we receive something
2710 * pushable again or we get flushed. */
2711 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2712 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2713 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2714 "dropping EOS buffer %p", data);
2715 gst_buffer_unref (GST_BUFFER_CAST (data));
2716 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2717 GstEvent *event = GST_EVENT_CAST (data);
2718 GstEventType type = GST_EVENT_TYPE (event);
2720 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2721 /* we found a pushable item in the queue, push it out */
2722 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2723 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2726 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2727 "dropping EOS event %p", event);
2728 gst_event_unref (event);
2729 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2730 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2731 "dropping EOS buffer list %p", data);
2732 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2733 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2734 queue->last_query = FALSE;
2735 g_cond_signal (&queue->query_handled);
2736 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2739 /* no more items in the queue. Set the unexpected flag so that upstream
2740 * make us refuse any more buffers on the sinkpad. Since we will still
2741 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2742 * task function does not shut down. */
2743 queue->unexpected = TRUE;
2747 /* dequeue an item from the queue an push it downstream. This functions returns
2748 * the result of the push. */
2749 static GstFlowReturn
2750 gst_queue2_push_one (GstQueue2 * queue)
2752 GstFlowReturn result = queue->srcresult;
2753 GstMiniObject *data;
2754 GstQueue2ItemType item_type;
2756 data = gst_queue2_locked_dequeue (queue, &item_type);
2761 STATUS (queue, queue->srcpad, "We have something dequeud");
2762 g_atomic_int_set (&queue->downstream_may_block,
2763 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2764 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2765 GST_QUEUE2_MUTEX_UNLOCK (queue);
2766 gst_queue2_post_buffering (queue);
2768 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2771 buffer = GST_BUFFER_CAST (data);
2773 result = gst_pad_push (queue->srcpad, buffer);
2774 g_atomic_int_set (&queue->downstream_may_block, 0);
2776 /* need to check for srcresult here as well */
2777 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2778 if (result == GST_FLOW_EOS) {
2779 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2782 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2783 * to the caller so that the task function does not shut down */
2784 result = GST_FLOW_OK;
2786 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2787 GstEvent *event = GST_EVENT_CAST (data);
2788 GstEventType type = GST_EVENT_TYPE (event);
2790 gst_pad_push_event (queue->srcpad, event);
2792 /* if we're EOS, return EOS so that the task pauses. */
2793 if (type == GST_EVENT_EOS) {
2794 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2795 "pushed EOS event %p, return EOS", event);
2796 result = GST_FLOW_EOS;
2799 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2800 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2801 GstBufferList *buffer_list;
2803 buffer_list = GST_BUFFER_LIST_CAST (data);
2805 result = gst_pad_push_list (queue->srcpad, buffer_list);
2806 g_atomic_int_set (&queue->downstream_may_block, 0);
2808 /* need to check for srcresult here as well */
2809 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2810 if (result == GST_FLOW_EOS) {
2811 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2814 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2815 * to the caller so that the task function does not shut down */
2816 result = GST_FLOW_OK;
2818 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2819 GstQuery *query = GST_QUERY_CAST (data);
2821 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2822 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2823 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2824 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2825 "did query %p, return %d", query, queue->last_query);
2826 g_cond_signal (&queue->query_handled);
2827 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2828 result = GST_FLOW_OK;
2835 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2836 "exit because we have no item in the queue");
2837 return GST_FLOW_ERROR;
2841 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2842 return GST_FLOW_FLUSHING;
2846 /* called repeatedly with @pad as the source pad. This function should push out
2847 * data to the peer element. */
2849 gst_queue2_loop (GstPad * pad)
2854 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2856 /* have to lock for thread-safety */
2857 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2859 if (gst_queue2_is_empty (queue)) {
2862 /* pause the timer while we wait. The fact that we are waiting does not mean
2863 * the byterate on the output pad is lower */
2864 if ((started = queue->out_timer_started))
2865 g_timer_stop (queue->out_timer);
2867 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2868 "queue is empty, waiting for new data");
2870 /* Wait for data to be available, we could be unlocked because of a flush. */
2871 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2873 while (gst_queue2_is_empty (queue));
2875 /* and continue if we were running before */
2877 g_timer_continue (queue->out_timer);
2879 ret = gst_queue2_push_one (queue);
2880 queue->srcresult = ret;
2881 queue->sinkresult = ret;
2882 if (ret != GST_FLOW_OK)
2885 GST_QUEUE2_MUTEX_UNLOCK (queue);
2886 gst_queue2_post_buffering (queue);
2893 gboolean eos = queue->is_eos;
2894 GstFlowReturn ret = queue->srcresult;
2896 gst_pad_pause_task (queue->srcpad);
2897 if (ret == GST_FLOW_FLUSHING) {
2898 gst_queue2_locked_flush (queue, FALSE, FALSE);
2900 GST_QUEUE2_SIGNAL_DEL (queue);
2901 queue->last_query = FALSE;
2902 g_cond_signal (&queue->query_handled);
2904 GST_QUEUE2_MUTEX_UNLOCK (queue);
2905 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2906 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2907 /* let app know about us giving up if upstream is not expected to do so */
2908 /* EOS is already taken care of elsewhere */
2909 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2910 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2911 (_("Internal data flow error.")),
2912 ("streaming task paused, reason %s (%d)",
2913 gst_flow_get_name (ret), ret));
2914 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2921 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2923 gboolean res = TRUE;
2924 GstQueue2 *queue = GST_QUEUE2 (parent);
2926 #ifndef GST_DISABLE_GST_DEBUG
2927 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2928 event, GST_EVENT_TYPE_NAME (event));
2931 switch (GST_EVENT_TYPE (event)) {
2932 case GST_EVENT_FLUSH_START:
2933 if (QUEUE_IS_USING_QUEUE (queue)) {
2934 /* just forward upstream */
2935 res = gst_pad_push_event (queue->sinkpad, event);
2937 /* now unblock the getrange function */
2938 GST_QUEUE2_MUTEX_LOCK (queue);
2939 GST_DEBUG_OBJECT (queue, "flushing");
2940 queue->srcresult = GST_FLOW_FLUSHING;
2941 GST_QUEUE2_SIGNAL_ADD (queue);
2942 GST_QUEUE2_MUTEX_UNLOCK (queue);
2944 /* when using a temp file, we eat the event */
2946 gst_event_unref (event);
2949 case GST_EVENT_FLUSH_STOP:
2950 if (QUEUE_IS_USING_QUEUE (queue)) {
2951 /* just forward upstream */
2952 res = gst_pad_push_event (queue->sinkpad, event);
2954 /* now unblock the getrange function */
2955 GST_QUEUE2_MUTEX_LOCK (queue);
2956 queue->srcresult = GST_FLOW_OK;
2957 GST_QUEUE2_MUTEX_UNLOCK (queue);
2959 /* when using a temp file, we eat the event */
2961 gst_event_unref (event);
2964 case GST_EVENT_RECONFIGURE:
2965 GST_QUEUE2_MUTEX_LOCK (queue);
2966 /* assume downstream is linked now and try to push again */
2967 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2968 queue->srcresult = GST_FLOW_OK;
2969 queue->sinkresult = GST_FLOW_OK;
2970 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2971 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2975 GST_QUEUE2_MUTEX_UNLOCK (queue);
2977 res = gst_pad_push_event (queue->sinkpad, event);
2980 res = gst_pad_push_event (queue->sinkpad, event);
2988 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2992 queue = GST_QUEUE2 (parent);
2994 switch (GST_QUERY_TYPE (query)) {
2995 case GST_QUERY_POSITION:
3000 if (!gst_pad_peer_query (queue->sinkpad, query))
3003 /* get peer position */
3004 gst_query_parse_position (query, &format, &peer_pos);
3006 /* FIXME: this code assumes that there's no discont in the queue */
3008 case GST_FORMAT_BYTES:
3009 peer_pos -= queue->cur_level.bytes;
3011 case GST_FORMAT_TIME:
3012 peer_pos -= queue->cur_level.time;
3015 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3016 "know how to adjust value", gst_format_get_name (format));
3019 /* set updated position */
3020 gst_query_set_position (query, format, peer_pos);
3023 case GST_QUERY_DURATION:
3025 GST_DEBUG_OBJECT (queue, "doing peer query");
3027 if (!gst_pad_peer_query (queue->sinkpad, query))
3030 GST_DEBUG_OBJECT (queue, "peer query success");
3033 case GST_QUERY_BUFFERING:
3036 gboolean is_buffering;
3037 GstBufferingMode mode;
3038 gint avg_in, avg_out;
3039 gint64 buffering_left;
3041 GST_DEBUG_OBJECT (queue, "query buffering");
3043 get_buffering_percent (queue, &is_buffering, &percent);
3044 gst_query_set_buffering_percent (query, is_buffering, percent);
3046 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3048 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3051 if (!QUEUE_IS_USING_QUEUE (queue)) {
3052 /* add ranges for download and ringbuffer buffering */
3054 gint64 start, stop, range_start, range_stop;
3055 guint64 writing_pos;
3056 gint64 estimated_total;
3058 gboolean peer_res, is_eos;
3059 GstQueue2Range *queued_ranges;
3061 /* we need a current download region */
3062 if (queue->current == NULL)
3065 writing_pos = queue->current->writing_pos;
3066 is_eos = queue->is_eos;
3069 /* we're EOS, we know the duration in bytes now */
3071 duration = writing_pos;
3073 /* get duration of upstream in bytes */
3074 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3075 GST_FORMAT_BYTES, &duration);
3078 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3079 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3081 /* calculate remaining and total download time */
3082 if (peer_res && avg_in > 0.0)
3083 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3085 estimated_total = -1;
3087 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3090 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3093 case GST_FORMAT_PERCENT:
3094 /* we need duration */
3099 /* get our available data relative to the duration */
3102 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3107 case GST_FORMAT_BYTES:
3117 /* fill out the buffered ranges */
3118 for (queued_ranges = queue->ranges; queued_ranges;
3119 queued_ranges = queued_ranges->next) {
3121 case GST_FORMAT_PERCENT:
3122 if (duration == -1) {
3128 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3129 queued_ranges->offset, duration);
3131 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3132 queued_ranges->writing_pos, duration);
3134 case GST_FORMAT_BYTES:
3135 range_start = queued_ranges->offset;
3136 range_stop = queued_ranges->writing_pos;
3143 if (range_start == range_stop)
3145 GST_DEBUG_OBJECT (queue,
3146 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3147 G_GINT64_FORMAT, range_start, range_stop);
3148 gst_query_add_buffering_range (query, range_start, range_stop);
3151 gst_query_set_buffering_range (query, format, start, stop,
3156 case GST_QUERY_SCHEDULING:
3159 GstSchedulingFlags flags = 0;
3161 if (!gst_pad_peer_query (queue->sinkpad, query))
3164 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3166 /* we can operate in pull mode when we are using a tempfile */
3167 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3170 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3171 gst_query_set_scheduling (query, flags, 0, -1, 0);
3173 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3174 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3178 /* peer handled other queries */
3179 if (!gst_pad_query_default (pad, parent, query))
3189 GST_DEBUG_OBJECT (queue, "failed peer query");
3195 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3197 GstQueue2 *queue = GST_QUEUE2 (element);
3199 /* simply forward to the srcpad query function */
3200 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3205 gst_queue2_update_upstream_size (GstQueue2 * queue)
3207 gint64 upstream_size = -1;
3209 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3211 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3213 /* upstream_size can be negative but queue->upstream_size is unsigned.
3214 * Prevent setting negative values to it (the query can return -1) */
3215 if (upstream_size >= 0)
3216 queue->upstream_size = upstream_size;
3218 queue->upstream_size = 0;
3222 static GstFlowReturn
3223 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3224 guint length, GstBuffer ** buffer)
3229 queue = GST_QUEUE2_CAST (parent);
3231 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3232 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3233 offset = (offset == -1) ? queue->current->reading_pos : offset;
3235 GST_DEBUG_OBJECT (queue,
3236 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3238 /* catch any reads beyond the size of the file here to make sure queue2
3239 * doesn't send seek events beyond the size of the file upstream, since
3240 * that would confuse elements such as souphttpsrc and/or http servers.
3241 * Demuxers often just loop until EOS at the end of the file to figure out
3242 * when they've read all the end-headers or index chunks. */
3243 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3244 gst_queue2_update_upstream_size (queue);
3245 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3246 goto out_unexpected;
3249 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3250 gst_queue2_update_upstream_size (queue);
3251 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3252 length = queue->upstream_size - offset;
3253 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3257 /* FIXME - function will block when the range is not yet available */
3258 ret = gst_queue2_create_read (queue, offset, length, buffer);
3259 GST_QUEUE2_MUTEX_UNLOCK (queue);
3260 gst_queue2_post_buffering (queue);
3267 ret = queue->srcresult;
3269 GST_DEBUG_OBJECT (queue, "we are flushing");
3270 GST_QUEUE2_MUTEX_UNLOCK (queue);
3275 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3276 GST_QUEUE2_MUTEX_UNLOCK (queue);
3277 return GST_FLOW_EOS;
3281 /* sink currently only operates in push mode */
3283 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3284 GstPadMode mode, gboolean active)
3289 queue = GST_QUEUE2 (parent);
3292 case GST_PAD_MODE_PUSH:
3294 GST_QUEUE2_MUTEX_LOCK (queue);
3295 GST_DEBUG_OBJECT (queue, "activating push mode");
3296 queue->srcresult = GST_FLOW_OK;
3297 queue->sinkresult = GST_FLOW_OK;
3298 queue->is_eos = FALSE;
3299 queue->unexpected = FALSE;
3300 reset_rate_timer (queue);
3301 GST_QUEUE2_MUTEX_UNLOCK (queue);
3303 /* unblock chain function */
3304 GST_QUEUE2_MUTEX_LOCK (queue);
3305 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3306 queue->srcresult = GST_FLOW_FLUSHING;
3307 queue->sinkresult = GST_FLOW_FLUSHING;
3308 GST_QUEUE2_SIGNAL_DEL (queue);
3309 /* Unblock query handler */
3310 queue->last_query = FALSE;
3311 g_cond_signal (&queue->query_handled);
3312 GST_QUEUE2_MUTEX_UNLOCK (queue);
3314 /* wait until it is unblocked and clean up */
3315 GST_PAD_STREAM_LOCK (pad);
3316 GST_QUEUE2_MUTEX_LOCK (queue);
3317 gst_queue2_locked_flush (queue, TRUE, FALSE);
3318 GST_QUEUE2_MUTEX_UNLOCK (queue);
3319 GST_PAD_STREAM_UNLOCK (pad);
3330 /* src operating in push mode, we start a task on the source pad that pushes out
3331 * buffers from the queue */
3333 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3335 gboolean result = FALSE;
3338 queue = GST_QUEUE2 (parent);
3341 GST_QUEUE2_MUTEX_LOCK (queue);
3342 GST_DEBUG_OBJECT (queue, "activating push mode");
3343 queue->srcresult = GST_FLOW_OK;
3344 queue->sinkresult = GST_FLOW_OK;
3345 queue->is_eos = FALSE;
3346 queue->unexpected = FALSE;
3348 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3349 GST_QUEUE2_MUTEX_UNLOCK (queue);
3351 /* unblock loop function */
3352 GST_QUEUE2_MUTEX_LOCK (queue);
3353 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3354 queue->srcresult = GST_FLOW_FLUSHING;
3355 queue->sinkresult = GST_FLOW_FLUSHING;
3356 /* the item add signal will unblock */
3357 GST_QUEUE2_SIGNAL_ADD (queue);
3358 GST_QUEUE2_MUTEX_UNLOCK (queue);
3360 /* step 2, make sure streaming finishes */
3361 result = gst_pad_stop_task (pad);
3367 /* pull mode, downstream will call our getrange function */
3369 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3374 queue = GST_QUEUE2 (parent);
3377 GST_QUEUE2_MUTEX_LOCK (queue);
3378 if (!QUEUE_IS_USING_QUEUE (queue)) {
3379 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3380 /* open the temp file now */
3381 result = gst_queue2_open_temp_location_file (queue);
3382 } else if (!queue->ring_buffer) {
3383 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3384 result = ! !queue->ring_buffer;
3389 GST_DEBUG_OBJECT (queue, "activating pull mode");
3390 init_ranges (queue);
3391 queue->srcresult = GST_FLOW_OK;
3392 queue->sinkresult = GST_FLOW_OK;
3393 queue->is_eos = FALSE;
3394 queue->unexpected = FALSE;
3395 queue->upstream_size = 0;
3397 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3398 /* this is not allowed, we cannot operate in pull mode without a temp
3400 queue->srcresult = GST_FLOW_FLUSHING;
3401 queue->sinkresult = GST_FLOW_FLUSHING;
3404 GST_QUEUE2_MUTEX_UNLOCK (queue);
3406 GST_QUEUE2_MUTEX_LOCK (queue);
3407 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3408 queue->srcresult = GST_FLOW_FLUSHING;
3409 queue->sinkresult = GST_FLOW_FLUSHING;
3410 /* this will unlock getrange */
3411 GST_QUEUE2_SIGNAL_ADD (queue);
3413 GST_QUEUE2_MUTEX_UNLOCK (queue);
3420 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3426 case GST_PAD_MODE_PULL:
3427 res = gst_queue2_src_activate_pull (pad, parent, active);
3429 case GST_PAD_MODE_PUSH:
3430 res = gst_queue2_src_activate_push (pad, parent, active);
3433 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3440 static GstStateChangeReturn
3441 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3444 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3446 queue = GST_QUEUE2 (element);
3448 switch (transition) {
3449 case GST_STATE_CHANGE_NULL_TO_READY:
3451 case GST_STATE_CHANGE_READY_TO_PAUSED:
3452 GST_QUEUE2_MUTEX_LOCK (queue);
3453 if (!QUEUE_IS_USING_QUEUE (queue)) {
3454 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3455 if (!gst_queue2_open_temp_location_file (queue))
3456 ret = GST_STATE_CHANGE_FAILURE;
3458 if (queue->ring_buffer) {
3459 g_free (queue->ring_buffer);
3460 queue->ring_buffer = NULL;
3462 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3463 ret = GST_STATE_CHANGE_FAILURE;
3465 init_ranges (queue);
3467 queue->segment_event_received = FALSE;
3468 queue->starting_segment = NULL;
3469 gst_event_replace (&queue->stream_start_event, NULL);
3470 GST_QUEUE2_MUTEX_UNLOCK (queue);
3472 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3478 if (ret == GST_STATE_CHANGE_FAILURE)
3481 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3483 if (ret == GST_STATE_CHANGE_FAILURE)
3486 switch (transition) {
3487 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3489 case GST_STATE_CHANGE_PAUSED_TO_READY:
3490 GST_QUEUE2_MUTEX_LOCK (queue);
3491 if (!QUEUE_IS_USING_QUEUE (queue)) {
3492 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3493 gst_queue2_close_temp_location_file (queue);
3494 } else if (queue->ring_buffer) {
3495 g_free (queue->ring_buffer);
3496 queue->ring_buffer = NULL;
3498 clean_ranges (queue);
3500 if (queue->starting_segment != NULL) {
3501 gst_event_unref (queue->starting_segment);
3502 queue->starting_segment = NULL;
3504 gst_event_replace (&queue->stream_start_event, NULL);
3505 GST_QUEUE2_MUTEX_UNLOCK (queue);
3507 case GST_STATE_CHANGE_READY_TO_NULL:
3516 /* changing the capacity of the queue must wake up
3517 * the _chain function, it might have more room now
3518 * to store the buffer/event in the queue */
3519 #define QUEUE_CAPACITY_CHANGE(q) \
3520 GST_QUEUE2_SIGNAL_DEL (queue); \
3521 if (queue->use_buffering) \
3522 update_buffering (queue);
3524 /* Changing the minimum required fill level must
3525 * wake up the _loop function as it might now
3526 * be able to preceed.
3528 #define QUEUE_THRESHOLD_CHANGE(q)\
3529 GST_QUEUE2_SIGNAL_ADD (queue);
3532 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3536 /* the element must be stopped in order to do this */
3537 GST_OBJECT_LOCK (queue);
3538 state = GST_STATE (queue);
3539 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3541 GST_OBJECT_UNLOCK (queue);
3543 /* set new location */
3544 g_free (queue->temp_template);
3545 queue->temp_template = g_strdup (template);
3552 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3553 GST_OBJECT_UNLOCK (queue);
3558 gst_queue2_set_property (GObject * object,
3559 guint prop_id, const GValue * value, GParamSpec * pspec)
3561 GstQueue2 *queue = GST_QUEUE2 (object);
3563 /* someone could change levels here, and since this
3564 * affects the get/put funcs, we need to lock for safety. */
3565 GST_QUEUE2_MUTEX_LOCK (queue);
3568 case PROP_MAX_SIZE_BYTES:
3569 queue->max_level.bytes = g_value_get_uint (value);
3570 QUEUE_CAPACITY_CHANGE (queue);
3572 case PROP_MAX_SIZE_BUFFERS:
3573 queue->max_level.buffers = g_value_get_uint (value);
3574 QUEUE_CAPACITY_CHANGE (queue);
3576 case PROP_MAX_SIZE_TIME:
3577 queue->max_level.time = g_value_get_uint64 (value);
3578 /* set rate_time to the same value. We use an extra field in the level
3579 * structure so that we can easily access and compare it */
3580 queue->max_level.rate_time = queue->max_level.time;
3581 QUEUE_CAPACITY_CHANGE (queue);
3583 case PROP_USE_BUFFERING:
3584 queue->use_buffering = g_value_get_boolean (value);
3585 if (!queue->use_buffering && queue->is_buffering) {
3586 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3587 "posting 100%% message");
3588 SET_PERCENT (queue, 100);
3589 queue->is_buffering = FALSE;
3592 if (queue->use_buffering) {
3593 queue->is_buffering = TRUE;
3594 update_buffering (queue);
3597 case PROP_USE_RATE_ESTIMATE:
3598 queue->use_rate_estimate = g_value_get_boolean (value);
3600 case PROP_LOW_PERCENT:
3601 queue->low_percent = g_value_get_int (value);
3603 case PROP_HIGH_PERCENT:
3604 queue->high_percent = g_value_get_int (value);
3606 case PROP_TEMP_TEMPLATE:
3607 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3609 case PROP_TEMP_REMOVE:
3610 queue->temp_remove = g_value_get_boolean (value);
3612 case PROP_RING_BUFFER_MAX_SIZE:
3613 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3616 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3620 GST_QUEUE2_MUTEX_UNLOCK (queue);
3621 gst_queue2_post_buffering (queue);
3625 gst_queue2_get_property (GObject * object,
3626 guint prop_id, GValue * value, GParamSpec * pspec)
3628 GstQueue2 *queue = GST_QUEUE2 (object);
3630 GST_QUEUE2_MUTEX_LOCK (queue);
3633 case PROP_CUR_LEVEL_BYTES:
3634 g_value_set_uint (value, queue->cur_level.bytes);
3636 case PROP_CUR_LEVEL_BUFFERS:
3637 g_value_set_uint (value, queue->cur_level.buffers);
3639 case PROP_CUR_LEVEL_TIME:
3640 g_value_set_uint64 (value, queue->cur_level.time);
3642 case PROP_MAX_SIZE_BYTES:
3643 g_value_set_uint (value, queue->max_level.bytes);
3645 case PROP_MAX_SIZE_BUFFERS:
3646 g_value_set_uint (value, queue->max_level.buffers);
3648 case PROP_MAX_SIZE_TIME:
3649 g_value_set_uint64 (value, queue->max_level.time);
3651 case PROP_USE_BUFFERING:
3652 g_value_set_boolean (value, queue->use_buffering);
3654 case PROP_USE_RATE_ESTIMATE:
3655 g_value_set_boolean (value, queue->use_rate_estimate);
3657 case PROP_LOW_PERCENT:
3658 g_value_set_int (value, queue->low_percent);
3660 case PROP_HIGH_PERCENT:
3661 g_value_set_int (value, queue->high_percent);
3663 case PROP_TEMP_TEMPLATE:
3664 g_value_set_string (value, queue->temp_template);
3666 case PROP_TEMP_LOCATION:
3667 g_value_set_string (value, queue->temp_location);
3669 case PROP_TEMP_REMOVE:
3670 g_value_set_boolean (value, queue->temp_remove);
3672 case PROP_RING_BUFFER_MAX_SIZE:
3673 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3675 case PROP_AVG_IN_RATE:
3677 gdouble in_rate = queue->byte_in_rate;
3679 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3680 * calculated, so calculate it here. */
3681 if (in_rate == 0.0 && queue->bytes_in
3682 && queue->last_update_in_rates_elapsed > 0.0)
3683 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3685 g_value_set_int64 (value, (gint64) in_rate);
3689 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3693 GST_QUEUE2_MUTEX_UNLOCK (queue);