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 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
81 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_DEBUG_CATEGORY_STATIC (queue_debug);
87 #define GST_CAT_DEFAULT (queue_debug)
88 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
97 #define DEFAULT_BUFFER_SIZE 4096
98 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
99 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
100 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
102 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
104 /* default property values */
105 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
106 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
107 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
108 #define DEFAULT_USE_BUFFERING FALSE
109 #define DEFAULT_USE_RATE_ESTIMATE TRUE
110 #define DEFAULT_LOW_PERCENT 10
111 #define DEFAULT_HIGH_PERCENT 99
112 #define DEFAULT_TEMP_REMOVE TRUE
113 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
118 PROP_CUR_LEVEL_BUFFERS,
119 PROP_CUR_LEVEL_BYTES,
121 PROP_MAX_SIZE_BUFFERS,
125 PROP_USE_RATE_ESTIMATE,
131 PROP_RING_BUFFER_MAX_SIZE,
136 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
143 #define STATUS(queue, pad, msg) \
144 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
145 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
146 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
147 " ns, %"G_GUINT64_FORMAT" items", \
148 GST_DEBUG_PAD_NAME (pad), \
149 queue->cur_level.buffers, \
150 queue->max_level.buffers, \
151 queue->cur_level.bytes, \
152 queue->max_level.bytes, \
153 queue->cur_level.time, \
154 queue->max_level.time, \
155 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
156 queue->current->writing_pos - queue->current->max_reading_pos : \
157 queue->queue.length))
159 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
160 g_mutex_lock (&q->qlock); \
163 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
164 GST_QUEUE2_MUTEX_LOCK (q); \
165 if (res != GST_FLOW_OK) \
169 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
170 g_mutex_unlock (&q->qlock); \
173 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
174 STATUS (queue, q->sinkpad, "wait for DEL"); \
175 q->waiting_del = TRUE; \
176 g_cond_wait (&q->item_del, &queue->qlock); \
177 q->waiting_del = FALSE; \
178 if (res != GST_FLOW_OK) { \
179 STATUS (queue, q->srcpad, "received DEL wakeup"); \
182 STATUS (queue, q->sinkpad, "received DEL"); \
185 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
186 STATUS (queue, q->srcpad, "wait for ADD"); \
187 q->waiting_add = TRUE; \
188 g_cond_wait (&q->item_add, &q->qlock); \
189 q->waiting_add = FALSE; \
190 if (res != GST_FLOW_OK) { \
191 STATUS (queue, q->srcpad, "received ADD wakeup"); \
194 STATUS (queue, q->srcpad, "received ADD"); \
197 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
198 if (q->waiting_del) { \
199 STATUS (q, q->srcpad, "signal DEL"); \
200 g_cond_signal (&q->item_del); \
204 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
205 if (q->waiting_add) { \
206 STATUS (q, q->sinkpad, "signal ADD"); \
207 g_cond_signal (&q->item_add); \
211 #define SET_PERCENT(q, perc) G_STMT_START { \
212 if (perc != q->buffering_percent) { \
213 q->buffering_percent = perc; \
214 q->percent_changed = TRUE; \
215 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
216 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
217 &q->buffering_left); \
222 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
223 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
224 "dataflow inside the queue element");
225 #define gst_queue2_parent_class parent_class
226 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
228 static void gst_queue2_finalize (GObject * object);
230 static void gst_queue2_set_property (GObject * object,
231 guint prop_id, const GValue * value, GParamSpec * pspec);
232 static void gst_queue2_get_property (GObject * object,
233 guint prop_id, GValue * value, GParamSpec * pspec);
235 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
237 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
238 GstBufferList * buffer_list);
239 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
240 static void gst_queue2_loop (GstPad * pad);
242 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
244 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
247 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
249 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
251 static gboolean gst_queue2_handle_query (GstElement * element,
254 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
255 guint64 offset, guint length, GstBuffer ** buffer);
257 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
258 GstPadMode mode, gboolean active);
259 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
260 GstPadMode mode, gboolean active);
261 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
262 GstStateChange transition);
264 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
265 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
267 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
268 static void update_in_rates (GstQueue2 * queue);
269 static void gst_queue2_post_buffering (GstQueue2 * queue);
273 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
274 GST_QUEUE2_ITEM_TYPE_BUFFER,
275 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
276 GST_QUEUE2_ITEM_TYPE_EVENT,
277 GST_QUEUE2_ITEM_TYPE_QUERY
282 GstQueue2ItemType type;
286 static guint gst_queue2_signals[LAST_SIGNAL] = { 0 };
289 gst_queue2_class_init (GstQueue2Class * klass)
291 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
292 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
294 gobject_class->set_property = gst_queue2_set_property;
295 gobject_class->get_property = gst_queue2_get_property;
299 * GstQueue2::overrun:
300 * @queue: the queue2 instance
302 * Reports that the buffer became full (overrun).
303 * A buffer is full if the total amount of data inside it (num-buffers, time,
304 * size) is higher than the boundary values which can be set through the
305 * GObject properties.
309 gst_queue2_signals[SIGNAL_OVERRUN] =
310 g_signal_new ("overrun", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST,
311 G_STRUCT_OFFSET (GstQueue2Class, overrun), NULL, NULL,
312 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
315 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
316 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
317 "Current amount of data in the queue (bytes)",
318 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
320 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
321 "Current number of buffers in the queue",
322 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
324 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
325 "Current amount of data in the queue (in ns)",
326 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
329 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
330 "Max. amount of data in the queue (bytes, 0=disable)",
331 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
332 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
333 G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
335 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
336 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
337 DEFAULT_MAX_SIZE_BUFFERS,
338 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
339 G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
341 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
342 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
343 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
344 G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
347 g_param_spec_boolean ("use-buffering", "Use buffering",
348 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
349 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
350 G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
352 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
353 "Estimate the bitrate of the stream to calculate time level",
354 DEFAULT_USE_RATE_ESTIMATE,
355 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
357 g_param_spec_int ("low-percent", "Low percent",
358 "Low threshold for buffering to start. Only used if use-buffering is True",
359 0, 100, DEFAULT_LOW_PERCENT,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
362 g_param_spec_int ("high-percent", "High percent",
363 "High threshold for buffering to finish. Only used if use-buffering is True",
364 0, 100, DEFAULT_HIGH_PERCENT,
365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
368 g_param_spec_string ("temp-template", "Temporary File Template",
369 "File template to store temporary files in, should contain directory "
370 "and XXXXXX. (NULL == disabled)",
371 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
374 g_param_spec_string ("temp-location", "Temporary File Location",
375 "Location to store temporary files in (Only read this property, "
376 "use temp-template to configure the name template)",
377 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
380 * GstQueue2:temp-remove
382 * When temp-template is set, remove the temporary file when going to READY.
384 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
385 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
386 "Remove the temp-location after use",
387 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 * GstQueue2:ring-buffer-max-size
392 * The maximum size of the ring buffer in bytes. If set to 0, the ring
393 * buffer is disabled. Default 0.
395 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
396 g_param_spec_uint64 ("ring-buffer-max-size",
397 "Max. ring buffer size (bytes)",
398 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
399 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
400 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 * GstQueue2:avg-in-rate
405 * The average input data rate.
407 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
408 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
409 "Average input data rate (bytes/s)",
410 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
412 /* set several parent class virtual functions */
413 gobject_class->finalize = gst_queue2_finalize;
415 gst_element_class_add_pad_template (gstelement_class,
416 gst_static_pad_template_get (&srctemplate));
417 gst_element_class_add_pad_template (gstelement_class,
418 gst_static_pad_template_get (&sinktemplate));
420 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
423 "Erik Walthinsen <omega@cse.ogi.edu>, "
424 "Wim Taymans <wim.taymans@gmail.com>");
426 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
427 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
431 gst_queue2_init (GstQueue2 * queue)
433 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
435 gst_pad_set_chain_function (queue->sinkpad,
436 GST_DEBUG_FUNCPTR (gst_queue2_chain));
437 gst_pad_set_chain_list_function (queue->sinkpad,
438 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
439 gst_pad_set_activatemode_function (queue->sinkpad,
440 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
441 gst_pad_set_event_function (queue->sinkpad,
442 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
443 gst_pad_set_query_function (queue->sinkpad,
444 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
445 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
446 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
448 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
450 gst_pad_set_activatemode_function (queue->srcpad,
451 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
452 gst_pad_set_getrange_function (queue->srcpad,
453 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
454 gst_pad_set_event_function (queue->srcpad,
455 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
456 gst_pad_set_query_function (queue->srcpad,
457 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
458 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
459 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
462 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
463 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
464 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
465 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
466 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
467 queue->use_buffering = DEFAULT_USE_BUFFERING;
468 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
469 queue->low_percent = DEFAULT_LOW_PERCENT;
470 queue->high_percent = DEFAULT_HIGH_PERCENT;
472 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
473 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
475 queue->sinktime = GST_CLOCK_TIME_NONE;
476 queue->srctime = GST_CLOCK_TIME_NONE;
477 queue->sink_tainted = TRUE;
478 queue->src_tainted = TRUE;
480 queue->srcresult = GST_FLOW_FLUSHING;
481 queue->sinkresult = GST_FLOW_FLUSHING;
482 queue->is_eos = FALSE;
483 queue->in_timer = g_timer_new ();
484 queue->out_timer = g_timer_new ();
486 g_mutex_init (&queue->qlock);
487 queue->waiting_add = FALSE;
488 g_cond_init (&queue->item_add);
489 queue->waiting_del = FALSE;
490 g_cond_init (&queue->item_del);
491 g_queue_init (&queue->queue);
493 g_cond_init (&queue->query_handled);
494 queue->last_query = FALSE;
496 g_mutex_init (&queue->buffering_post_lock);
497 queue->buffering_percent = 100;
499 /* tempfile related */
500 queue->temp_template = NULL;
501 queue->temp_location = NULL;
502 queue->temp_remove = DEFAULT_TEMP_REMOVE;
504 queue->ring_buffer = NULL;
505 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
507 GST_DEBUG_OBJECT (queue,
508 "initialized queue's not_empty & not_full conditions");
511 /* called only once, as opposed to dispose */
513 gst_queue2_finalize (GObject * object)
515 GstQueue2 *queue = GST_QUEUE2 (object);
517 GST_DEBUG_OBJECT (queue, "finalizing queue");
519 while (!g_queue_is_empty (&queue->queue)) {
520 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
522 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
523 gst_mini_object_unref (qitem->item);
524 g_slice_free (GstQueue2Item, qitem);
527 queue->last_query = FALSE;
528 g_queue_clear (&queue->queue);
529 g_mutex_clear (&queue->qlock);
530 g_mutex_clear (&queue->buffering_post_lock);
531 g_cond_clear (&queue->item_add);
532 g_cond_clear (&queue->item_del);
533 g_cond_clear (&queue->query_handled);
534 g_timer_destroy (queue->in_timer);
535 g_timer_destroy (queue->out_timer);
537 /* temp_file path cleanup */
538 g_free (queue->temp_template);
539 g_free (queue->temp_location);
541 G_OBJECT_CLASS (parent_class)->finalize (object);
545 debug_ranges (GstQueue2 * queue)
547 GstQueue2Range *walk;
549 for (walk = queue->ranges; walk; walk = walk->next) {
550 GST_DEBUG_OBJECT (queue,
551 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
552 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
553 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
554 walk->rb_writing_pos, walk->reading_pos,
555 walk == queue->current ? "**y**" : " n ");
559 /* clear all the downloaded ranges */
561 clean_ranges (GstQueue2 * queue)
563 GST_DEBUG_OBJECT (queue, "clean queue ranges");
565 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
566 queue->ranges = NULL;
567 queue->current = NULL;
570 /* find a range that contains @offset or NULL when nothing does */
571 static GstQueue2Range *
572 find_range (GstQueue2 * queue, guint64 offset)
574 GstQueue2Range *range = NULL;
575 GstQueue2Range *walk;
577 /* first do a quick check for the current range */
578 for (walk = queue->ranges; walk; walk = walk->next) {
579 if (offset >= walk->offset && offset <= walk->writing_pos) {
580 /* we can reuse an existing range */
586 GST_DEBUG_OBJECT (queue,
587 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
588 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
590 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
596 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
598 guint64 max_reading_pos, writing_pos;
600 writing_pos = range->writing_pos;
601 max_reading_pos = range->max_reading_pos;
603 if (writing_pos > max_reading_pos)
604 queue->cur_level.bytes = writing_pos - max_reading_pos;
606 queue->cur_level.bytes = 0;
609 /* make a new range for @offset or reuse an existing range */
610 static GstQueue2Range *
611 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
613 GstQueue2Range *range, *prev, *next;
615 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
617 if ((range = find_range (queue, offset))) {
618 GST_DEBUG_OBJECT (queue,
619 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
621 if (update_existing && range->writing_pos != offset) {
622 GST_DEBUG_OBJECT (queue, "updating range writing position to "
623 "%" G_GUINT64_FORMAT, offset);
624 range->writing_pos = offset;
627 GST_DEBUG_OBJECT (queue,
628 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
630 range = g_slice_new0 (GstQueue2Range);
631 range->offset = offset;
632 /* we want to write to the next location in the ring buffer */
633 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
634 range->writing_pos = offset;
635 range->rb_writing_pos = range->rb_offset;
636 range->reading_pos = offset;
637 range->max_reading_pos = offset;
641 next = queue->ranges;
643 if (next->offset > offset) {
644 /* insert before next */
645 GST_DEBUG_OBJECT (queue,
646 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
658 queue->ranges = range;
660 debug_ranges (queue);
662 /* update the stats for this range */
663 update_cur_level (queue, range);
669 /* clear and init the download ranges for offset 0 */
671 init_ranges (GstQueue2 * queue)
673 GST_DEBUG_OBJECT (queue, "init queue ranges");
675 /* get rid of all the current ranges */
676 clean_ranges (queue);
677 /* make a range for offset 0 */
678 queue->current = add_range (queue, 0, TRUE);
681 /* calculate the diff between running time on the sink and src of the queue.
682 * This is the total amount of time in the queue. */
684 update_time_level (GstQueue2 * queue)
686 if (queue->sink_tainted) {
688 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
689 queue->sink_segment.position);
690 queue->sink_tainted = FALSE;
693 if (queue->src_tainted) {
695 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
696 queue->src_segment.position);
697 queue->src_tainted = FALSE;
700 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
701 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
703 if (queue->sinktime != GST_CLOCK_TIME_NONE
704 && queue->srctime != GST_CLOCK_TIME_NONE
705 && queue->sinktime >= queue->srctime)
706 queue->cur_level.time = queue->sinktime - queue->srctime;
708 queue->cur_level.time = 0;
711 /* take a SEGMENT event and apply the values to segment, updating the time
714 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
717 gst_event_copy_segment (event, segment);
719 if (segment->format == GST_FORMAT_BYTES) {
720 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
721 /* start is where we'll be getting from and as such writing next */
722 queue->current = add_range (queue, segment->start, TRUE);
726 /* now configure the values, we use these to track timestamps on the
728 if (segment->format != GST_FORMAT_TIME) {
729 /* non-time format, pretent the current time segment is closed with a
730 * 0 start and unknown stop time. */
731 segment->format = GST_FORMAT_TIME;
737 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
740 queue->sink_tainted = TRUE;
742 queue->src_tainted = TRUE;
744 /* segment can update the time level of the queue */
745 update_time_level (queue);
749 apply_gap (GstQueue2 * queue, GstEvent * event,
750 GstSegment * segment, gboolean is_sink)
752 GstClockTime timestamp;
753 GstClockTime duration;
755 gst_event_parse_gap (event, ×tamp, &duration);
757 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
759 if (GST_CLOCK_TIME_IS_VALID (duration)) {
760 timestamp += duration;
763 segment->position = timestamp;
766 queue->sink_tainted = TRUE;
768 queue->src_tainted = TRUE;
770 /* calc diff with other end */
771 update_time_level (queue);
775 /* take a buffer and update segment, updating the time level of the queue. */
777 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
780 GstClockTime duration, timestamp;
782 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
783 duration = GST_BUFFER_DURATION (buffer);
785 /* if no timestamp is set, assume it's continuous with the previous
787 if (timestamp == GST_CLOCK_TIME_NONE)
788 timestamp = segment->position;
791 if (duration != GST_CLOCK_TIME_NONE)
792 timestamp += duration;
794 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
795 GST_TIME_ARGS (timestamp));
797 segment->position = timestamp;
800 queue->sink_tainted = TRUE;
802 queue->src_tainted = TRUE;
804 /* calc diff with other end */
805 update_time_level (queue);
809 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
811 GstClockTime *timestamp = data;
814 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
815 " duration %" GST_TIME_FORMAT, idx,
816 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
817 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
818 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
820 btime = GST_BUFFER_DTS_OR_PTS (*buf);
821 if (GST_CLOCK_TIME_IS_VALID (btime))
824 if (GST_BUFFER_DURATION_IS_VALID (*buf))
825 *timestamp += GST_BUFFER_DURATION (*buf);
827 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
831 /* take a buffer list and update segment, updating the time level of the queue */
833 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
834 GstSegment * segment, gboolean is_sink)
836 GstClockTime timestamp;
838 /* if no timestamp is set, assume it's continuous with the previous time */
839 timestamp = segment->position;
841 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
843 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
844 GST_TIME_ARGS (timestamp));
846 segment->position = timestamp;
849 queue->sink_tainted = TRUE;
851 queue->src_tainted = TRUE;
853 /* calc diff with other end */
854 update_time_level (queue);
858 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
863 if (queue->high_percent <= 0) {
867 *is_buffering = FALSE;
870 #define GET_PERCENT(format,alt_max) ((queue->max_level.format) > 0 ? (queue->cur_level.format) * 100 / ((alt_max) > 0 ? MIN ((alt_max), (queue->max_level.format)) : (queue->max_level.format)) : 0)
873 /* on EOS we are always 100% full, we set the var here so that it we can
874 * reuse the logic below to stop buffering */
876 GST_LOG_OBJECT (queue, "we are EOS");
878 GST_LOG_OBJECT (queue,
879 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
880 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
881 queue->cur_level.buffers);
883 /* figure out the percent we are filled, we take the max of all formats. */
884 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
885 perc = GET_PERCENT (bytes, 0);
887 guint64 rb_size = queue->ring_buffer_max_size;
888 perc = GET_PERCENT (bytes, rb_size);
890 perc = MAX (perc, GET_PERCENT (time, 0));
891 perc = MAX (perc, GET_PERCENT (buffers, 0));
893 /* also apply the rate estimate when we need to */
894 if (queue->use_rate_estimate)
895 perc = MAX (perc, GET_PERCENT (rate_time, 0));
897 /* Don't get to 0% unless we're really empty */
898 if (queue->cur_level.bytes > 0)
899 perc = MAX (1, perc);
904 *is_buffering = queue->is_buffering;
906 /* scale to high percent so that it becomes the 100% mark */
907 perc = perc * 100 / queue->high_percent;
915 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
922 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
923 gint * avg_in, gint * avg_out, gint64 * buffering_left)
926 if (!QUEUE_IS_USING_QUEUE (queue)) {
927 if (QUEUE_IS_USING_RING_BUFFER (queue))
928 *mode = GST_BUFFERING_TIMESHIFT;
930 *mode = GST_BUFFERING_DOWNLOAD;
932 *mode = GST_BUFFERING_STREAM;
937 *avg_in = queue->byte_in_rate;
939 *avg_out = queue->byte_out_rate;
941 if (buffering_left) {
942 *buffering_left = (percent == 100 ? 0 : -1);
944 if (queue->use_rate_estimate) {
947 max = queue->max_level.rate_time;
948 cur = queue->cur_level.rate_time;
950 if (percent != 100 && max > cur)
951 *buffering_left = (max - cur) / 1000000;
957 gst_queue2_post_buffering (GstQueue2 * queue)
959 GstMessage *msg = NULL;
961 g_mutex_lock (&queue->buffering_post_lock);
962 GST_QUEUE2_MUTEX_LOCK (queue);
963 if (queue->percent_changed) {
964 gint percent = queue->buffering_percent;
966 queue->percent_changed = FALSE;
968 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
969 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
971 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
972 queue->avg_out, queue->buffering_left);
974 GST_QUEUE2_MUTEX_UNLOCK (queue);
977 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
979 g_mutex_unlock (&queue->buffering_post_lock);
983 update_buffering (GstQueue2 * queue)
987 /* Ensure the variables used to calculate buffering state are up-to-date. */
989 update_cur_level (queue, queue->current);
990 update_in_rates (queue);
992 if (!get_buffering_percent (queue, NULL, &percent))
995 if (queue->is_buffering) {
996 /* if we were buffering see if we reached the high watermark */
998 queue->is_buffering = FALSE;
1000 SET_PERCENT (queue, percent);
1002 /* we were not buffering, check if we need to start buffering if we drop
1003 * below the low threshold */
1004 if (percent < queue->low_percent) {
1005 queue->is_buffering = TRUE;
1006 SET_PERCENT (queue, percent);
1012 reset_rate_timer (GstQueue2 * queue)
1014 queue->bytes_in = 0;
1015 queue->bytes_out = 0;
1016 queue->byte_in_rate = 0.0;
1017 queue->byte_in_period = 0;
1018 queue->byte_out_rate = 0.0;
1019 queue->last_update_in_rates_elapsed = 0.0;
1020 queue->last_in_elapsed = 0.0;
1021 queue->last_out_elapsed = 0.0;
1022 queue->in_timer_started = FALSE;
1023 queue->out_timer_started = FALSE;
1026 /* the interval in seconds to recalculate the rate */
1027 #define RATE_INTERVAL 0.2
1028 /* Tuning for rate estimation. We use a large window for the input rate because
1029 * it should be stable when connected to a network. The output rate is less
1030 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1031 * therefore adapt more quickly.
1032 * However, initial input rate may be subject to a burst, and should therefore
1033 * initially also adapt more quickly to changes, and only later on give higher
1034 * weight to previous values. */
1035 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1036 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1039 update_in_rates (GstQueue2 * queue)
1041 gdouble elapsed, period;
1042 gdouble byte_in_rate;
1044 if (!queue->in_timer_started) {
1045 queue->in_timer_started = TRUE;
1046 g_timer_start (queue->in_timer);
1050 queue->last_update_in_rates_elapsed = elapsed =
1051 g_timer_elapsed (queue->in_timer, NULL);
1053 /* recalc after each interval. */
1054 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1055 period = elapsed - queue->last_in_elapsed;
1057 GST_DEBUG_OBJECT (queue,
1058 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1059 period, queue->bytes_in, queue->byte_in_period);
1061 byte_in_rate = queue->bytes_in / period;
1063 if (queue->byte_in_rate == 0.0)
1064 queue->byte_in_rate = byte_in_rate;
1066 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1067 (double) queue->byte_in_period, period);
1069 /* another data point, cap at 16 for long time running average */
1070 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1071 queue->byte_in_period += period;
1073 /* reset the values to calculate rate over the next interval */
1074 queue->last_in_elapsed = elapsed;
1075 queue->bytes_in = 0;
1078 if (queue->byte_in_rate > 0.0) {
1079 queue->cur_level.rate_time =
1080 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1082 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1083 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1087 update_out_rates (GstQueue2 * queue)
1089 gdouble elapsed, period;
1090 gdouble byte_out_rate;
1092 if (!queue->out_timer_started) {
1093 queue->out_timer_started = TRUE;
1094 g_timer_start (queue->out_timer);
1098 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1100 /* recalc after each interval. */
1101 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1102 period = elapsed - queue->last_out_elapsed;
1104 GST_DEBUG_OBJECT (queue,
1105 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1107 byte_out_rate = queue->bytes_out / period;
1109 if (queue->byte_out_rate == 0.0)
1110 queue->byte_out_rate = byte_out_rate;
1112 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1114 /* reset the values to calculate rate over the next interval */
1115 queue->last_out_elapsed = elapsed;
1116 queue->bytes_out = 0;
1118 if (queue->byte_in_rate > 0.0) {
1119 queue->cur_level.rate_time =
1120 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1122 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1123 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1127 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1129 guint64 reading_pos, max_reading_pos;
1132 max_reading_pos = range->max_reading_pos;
1134 max_reading_pos = MAX (max_reading_pos, reading_pos);
1136 GST_DEBUG_OBJECT (queue,
1137 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1138 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1139 range->max_reading_pos = max_reading_pos;
1141 update_cur_level (queue, range);
1145 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1150 /* until we receive the FLUSH_STOP from this seek, we skip data */
1151 queue->seeking = TRUE;
1152 GST_QUEUE2_MUTEX_UNLOCK (queue);
1154 debug_ranges (queue);
1156 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1159 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1160 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1161 GST_SEEK_TYPE_NONE, -1);
1163 res = gst_pad_push_event (queue->sinkpad, event);
1164 GST_QUEUE2_MUTEX_LOCK (queue);
1167 /* Between us sending the seek event and re-acquiring the lock, the source
1168 * thread might already have pushed data and moved along the range's
1169 * writing_pos beyond the seek offset. In that case we don't want to set
1170 * the writing position back to the requested seek position, as it would
1171 * cause data to be written to the wrong offset in the file or ring buffer.
1172 * We still do the add_range call to switch the current range to the
1173 * requested range, or create one if one doesn't exist yet. */
1174 queue->current = add_range (queue, offset, FALSE);
1180 /* get the threshold for when we decide to seek rather than wait */
1182 get_seek_threshold (GstQueue2 * queue)
1186 /* FIXME, find a good threshold based on the incoming rate. */
1187 threshold = 1024 * 512;
1189 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1190 threshold = MIN (threshold,
1191 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1196 /* see if there is enough data in the file to read a full buffer */
1198 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1200 GstQueue2Range *range;
1202 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1205 if ((range = find_range (queue, offset))) {
1206 if (queue->current != range) {
1207 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1208 perform_seek_to_offset (queue, range->writing_pos);
1211 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1212 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1214 /* we have a range for offset */
1215 GST_DEBUG_OBJECT (queue,
1216 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1217 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1219 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1222 if (offset + length <= range->writing_pos)
1225 GST_DEBUG_OBJECT (queue,
1226 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1227 (offset + length) - range->writing_pos);
1230 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1231 " len %u", offset, length);
1232 /* we don't have the range, see how far away we are */
1233 if (!queue->is_eos && queue->current) {
1234 guint64 threshold = get_seek_threshold (queue);
1236 if (offset >= queue->current->offset && offset <=
1237 queue->current->writing_pos + threshold) {
1238 GST_INFO_OBJECT (queue,
1239 "requested data is within range, wait for data");
1244 /* too far away, do a seek */
1245 perform_seek_to_offset (queue, offset);
1252 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1253 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1254 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1256 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1259 static GstFlowReturn
1260 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1261 guint8 * dst, gint64 * read_return)
1263 guint8 *ring_buffer;
1266 ring_buffer = queue->ring_buffer;
1268 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1271 /* this should not block */
1272 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1274 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1275 res = fread (dst, 1, length, queue->temp_file);
1277 memcpy (dst, ring_buffer + offset, length);
1281 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1283 if (G_UNLIKELY (res < length)) {
1284 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1285 goto could_not_read;
1286 /* check for errors or EOF */
1287 if (ferror (queue->temp_file))
1288 goto could_not_read;
1289 if (feof (queue->temp_file) && length > 0)
1299 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1300 return GST_FLOW_ERROR;
1304 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1305 return GST_FLOW_ERROR;
1309 GST_DEBUG ("non-regular file hits EOS");
1310 return GST_FLOW_EOS;
1314 static GstFlowReturn
1315 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1316 GstBuffer ** buffer)
1321 guint64 file_offset;
1322 guint block_length, remaining, read_length;
1326 GstFlowReturn ret = GST_FLOW_OK;
1328 /* allocate the output buffer of the requested size */
1329 if (*buffer == NULL)
1330 buf = gst_buffer_new_allocate (NULL, length, NULL);
1334 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1337 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1341 rb_size = queue->ring_buffer_max_size;
1342 max_size = QUEUE_MAX_BYTES (queue);
1345 while (remaining > 0) {
1346 /* configure how much/whether to read */
1347 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1350 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1353 /* calculate how far away the offset is */
1354 if (queue->current->writing_pos > rpos)
1355 level = queue->current->writing_pos - rpos;
1359 GST_DEBUG_OBJECT (queue,
1360 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1361 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1362 rpos, queue->current->writing_pos, level, max_size);
1364 if (level >= max_size) {
1365 /* we don't have the data but if we have a ring buffer that is full, we
1367 GST_DEBUG_OBJECT (queue,
1368 "ring buffer full, reading QUEUE_MAX_BYTES %"
1369 G_GUINT64_FORMAT " bytes", max_size);
1370 read_length = max_size;
1371 } else if (queue->is_eos) {
1372 /* won't get any more data so read any data we have */
1374 GST_DEBUG_OBJECT (queue,
1375 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1377 read_length = level;
1385 if (read_length == 0) {
1386 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1387 GST_DEBUG_OBJECT (queue,
1388 "update current position [%" G_GUINT64_FORMAT "-%"
1389 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1390 update_cur_pos (queue, queue->current, rpos);
1391 GST_QUEUE2_SIGNAL_DEL (queue);
1394 if (queue->use_buffering)
1395 update_buffering (queue);
1397 GST_DEBUG_OBJECT (queue, "waiting for add");
1398 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1402 /* we have the requested data so read it */
1403 read_length = remaining;
1406 /* set range reading_pos to actual reading position for this read */
1407 queue->current->reading_pos = rpos;
1409 /* configure how much and from where to read */
1410 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1412 (queue->current->rb_offset + (rpos -
1413 queue->current->offset)) % rb_size;
1414 if (file_offset + read_length > rb_size) {
1415 block_length = rb_size - file_offset;
1417 block_length = read_length;
1421 block_length = read_length;
1424 /* while we still have data to read, we loop */
1425 while (read_length > 0) {
1429 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1430 data, &read_return);
1431 if (ret != GST_FLOW_OK)
1434 file_offset += read_return;
1435 if (QUEUE_IS_USING_RING_BUFFER (queue))
1436 file_offset %= rb_size;
1438 data += read_return;
1439 read_length -= read_return;
1440 block_length = read_length;
1441 remaining -= read_return;
1443 rpos = (queue->current->reading_pos += read_return);
1444 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1446 GST_QUEUE2_SIGNAL_DEL (queue);
1447 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1450 gst_buffer_unmap (buf, &info);
1451 gst_buffer_resize (buf, 0, length);
1453 GST_BUFFER_OFFSET (buf) = offset;
1454 GST_BUFFER_OFFSET_END (buf) = offset + length;
1463 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1464 gst_buffer_unmap (buf, &info);
1465 if (*buffer == NULL)
1466 gst_buffer_unref (buf);
1467 return GST_FLOW_EOS;
1471 GST_DEBUG_OBJECT (queue, "we are flushing");
1472 gst_buffer_unmap (buf, &info);
1473 if (*buffer == NULL)
1474 gst_buffer_unref (buf);
1475 return GST_FLOW_FLUSHING;
1479 GST_DEBUG_OBJECT (queue, "we have a read error");
1480 gst_buffer_unmap (buf, &info);
1481 if (*buffer == NULL)
1482 gst_buffer_unref (buf);
1487 /* should be called with QUEUE_LOCK */
1488 static GstMiniObject *
1489 gst_queue2_read_item_from_file (GstQueue2 * queue)
1491 GstMiniObject *item;
1493 if (queue->stream_start_event != NULL) {
1494 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1495 queue->stream_start_event = NULL;
1496 } else if (queue->starting_segment != NULL) {
1497 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1498 queue->starting_segment = NULL;
1501 GstBuffer *buffer = NULL;
1502 guint64 reading_pos;
1504 reading_pos = queue->current->reading_pos;
1507 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1512 item = GST_MINI_OBJECT_CAST (buffer);
1515 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1525 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1526 * the temp filename. */
1528 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1533 if (queue->temp_file)
1534 goto already_opened;
1536 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1538 /* If temp_template was set, allocate a filename and open that filen */
1541 if (queue->temp_template == NULL)
1544 /* make copy of the template, we don't want to change this */
1545 name = g_strdup (queue->temp_template);
1546 fd = g_mkstemp (name);
1548 goto mkstemp_failed;
1550 /* open the file for update/writing */
1551 queue->temp_file = fdopen (fd, "wb+");
1552 /* error creating file */
1553 if (queue->temp_file == NULL)
1556 g_free (queue->temp_location);
1557 queue->temp_location = name;
1559 GST_QUEUE2_MUTEX_UNLOCK (queue);
1561 /* we can't emit the notify with the lock */
1562 g_object_notify (G_OBJECT (queue), "temp-location");
1564 GST_QUEUE2_MUTEX_LOCK (queue);
1566 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1573 GST_DEBUG_OBJECT (queue, "temp file was already open");
1578 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1579 (_("No Temp directory specified.")), (NULL));
1584 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1585 (_("Could not create temp file \"%s\"."), queue->temp_template),
1592 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1593 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1602 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1605 if (queue->temp_file == NULL)
1608 GST_DEBUG_OBJECT (queue, "closing temp file");
1610 fflush (queue->temp_file);
1611 fclose (queue->temp_file);
1613 if (queue->temp_remove) {
1614 if (remove (queue->temp_location) < 0) {
1615 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1616 queue->temp_location, g_strerror (errno));
1620 queue->temp_file = NULL;
1621 clean_ranges (queue);
1625 gst_queue2_flush_temp_file (GstQueue2 * queue)
1627 if (queue->temp_file == NULL)
1630 GST_DEBUG_OBJECT (queue, "flushing temp file");
1632 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1636 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1638 if (!QUEUE_IS_USING_QUEUE (queue)) {
1639 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1640 gst_queue2_flush_temp_file (queue);
1641 init_ranges (queue);
1643 while (!g_queue_is_empty (&queue->queue)) {
1644 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1646 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1647 && GST_EVENT_IS_STICKY (qitem->item)
1648 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1649 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1650 gst_pad_store_sticky_event (queue->srcpad,
1651 GST_EVENT_CAST (qitem->item));
1654 /* Then lose another reference because we are supposed to destroy that
1655 data when flushing */
1656 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1657 gst_mini_object_unref (qitem->item);
1658 g_slice_free (GstQueue2Item, qitem);
1661 queue->last_query = FALSE;
1662 g_cond_signal (&queue->query_handled);
1663 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1664 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1665 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1666 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1667 queue->sink_tainted = queue->src_tainted = TRUE;
1668 if (queue->starting_segment != NULL)
1669 gst_event_unref (queue->starting_segment);
1670 queue->starting_segment = NULL;
1671 queue->segment_event_received = FALSE;
1672 gst_event_replace (&queue->stream_start_event, NULL);
1674 /* we deleted a lot of something */
1675 GST_QUEUE2_SIGNAL_DEL (queue);
1679 gst_queue2_wait_free_space (GstQueue2 * queue)
1681 /* We make space available if we're "full" according to whatever
1682 * the user defined as "full". */
1683 if (gst_queue2_is_filled (queue)) {
1686 /* pause the timer while we wait. The fact that we are waiting does not mean
1687 * the byterate on the input pad is lower */
1688 if ((started = queue->in_timer_started))
1689 g_timer_stop (queue->in_timer);
1691 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1692 "queue is full, waiting for free space");
1694 GST_QUEUE2_MUTEX_UNLOCK (queue);
1695 g_signal_emit (queue, gst_queue2_signals[SIGNAL_OVERRUN], 0);
1696 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
1697 /* we recheck, the signal could have changed the thresholds */
1698 if (!gst_queue2_is_filled (queue))
1701 /* Wait for space to be available, we could be unlocked because of a flush. */
1702 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1704 while (gst_queue2_is_filled (queue));
1706 /* and continue if we were running before */
1708 g_timer_continue (queue->in_timer);
1715 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1721 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1724 guint8 *data, *ring_buffer;
1725 guint size, rb_size;
1726 guint64 writing_pos, new_writing_pos;
1727 GstQueue2Range *range, *prev, *next;
1728 gboolean do_seek = FALSE;
1730 if (QUEUE_IS_USING_RING_BUFFER (queue))
1731 writing_pos = queue->current->rb_writing_pos;
1733 writing_pos = queue->current->writing_pos;
1734 ring_buffer = queue->ring_buffer;
1735 rb_size = queue->ring_buffer_max_size;
1737 gst_buffer_map (buffer, &info, GST_MAP_READ);
1742 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1746 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1747 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1748 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1749 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1750 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1756 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1759 /* calculate the space in the ring buffer not used by data from
1760 * the current range */
1761 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1762 /* wait until there is some free space */
1763 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1765 /* get the amount of space we have */
1766 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1768 /* calculate if we need to split or if we can write the entire
1770 to_write = MIN (size, space);
1772 /* the writing position in the ring buffer after writing (part
1773 * or all of) the buffer */
1774 new_writing_pos = (writing_pos + to_write) % rb_size;
1777 range = queue->ranges;
1779 /* if we need to overwrite data in the ring buffer, we need to
1782 * warning: this code is complicated and includes some
1783 * simplifications - pen, paper and diagrams for the cases
1786 guint64 range_data_start, range_data_end;
1787 GstQueue2Range *range_to_destroy = NULL;
1789 range_data_start = range->rb_offset;
1790 range_data_end = range->rb_writing_pos;
1792 /* handle the special case where the range has no data in it */
1793 if (range->writing_pos == range->offset) {
1794 if (range != queue->current) {
1795 GST_DEBUG_OBJECT (queue,
1796 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1797 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1799 range_to_destroy = range;
1801 prev->next = range->next;
1806 if (range_data_end > range_data_start) {
1807 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1810 if (new_writing_pos > range_data_start) {
1811 if (new_writing_pos >= range_data_end) {
1812 GST_DEBUG_OBJECT (queue,
1813 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1814 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1816 range_to_destroy = range;
1818 prev->next = range->next;
1820 GST_DEBUG_OBJECT (queue,
1821 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1822 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1823 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1824 range->offset + new_writing_pos - range_data_start,
1826 range->offset += (new_writing_pos - range_data_start);
1827 range->rb_offset = new_writing_pos;
1831 guint64 new_wpos_virt = writing_pos + to_write;
1833 if (new_wpos_virt <= range_data_start)
1836 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1837 GST_DEBUG_OBJECT (queue,
1838 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1839 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1841 range_to_destroy = range;
1843 prev->next = range->next;
1845 GST_DEBUG_OBJECT (queue,
1846 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1847 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1848 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1849 range->offset + new_writing_pos - range_data_start,
1851 range->offset += (new_wpos_virt - range_data_start);
1852 range->rb_offset = new_writing_pos;
1857 if (!range_to_destroy)
1860 range = range->next;
1861 if (range_to_destroy) {
1862 if (range_to_destroy == queue->ranges)
1863 queue->ranges = range;
1864 g_slice_free (GstQueue2Range, range_to_destroy);
1865 range_to_destroy = NULL;
1870 new_writing_pos = writing_pos + to_write;
1873 if (QUEUE_IS_USING_TEMP_FILE (queue)
1874 && FSEEK_FILE (queue->temp_file, writing_pos))
1877 if (new_writing_pos > writing_pos) {
1878 GST_INFO_OBJECT (queue,
1879 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1880 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1881 queue->current->writing_pos, queue->current->rb_writing_pos);
1882 /* either not using ring buffer or no wrapping, just write */
1883 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1884 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1887 memcpy (ring_buffer + writing_pos, data, to_write);
1890 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1891 /* try to merge with next range */
1892 while ((next = queue->current->next)) {
1893 GST_INFO_OBJECT (queue,
1894 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1895 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1896 if (new_writing_pos < next->offset)
1899 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1902 /* remove the group */
1903 queue->current->next = next->next;
1905 /* We use the threshold to decide if we want to do a seek or simply
1906 * read the data again. If there is not so much data in the range we
1907 * prefer to avoid to seek and read it again. */
1908 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1909 /* the new range had more data than the threshold, it's worth keeping
1910 * it and doing a seek. */
1911 new_writing_pos = next->writing_pos;
1914 g_slice_free (GstQueue2Range, next);
1916 goto update_and_signal;
1920 guint block_one, block_two;
1922 block_one = rb_size - writing_pos;
1923 block_two = to_write - block_one;
1925 if (block_one > 0) {
1926 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1927 /* write data to end of ring buffer */
1928 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1929 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1932 memcpy (ring_buffer + writing_pos, data, block_one);
1936 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1939 if (block_two > 0) {
1940 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1941 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1942 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1945 memcpy (ring_buffer, data + block_one, block_two);
1951 /* update the writing positions */
1953 GST_INFO_OBJECT (queue,
1954 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1955 to_write, writing_pos, size);
1957 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1959 queue->current->writing_pos += to_write;
1960 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1962 queue->current->writing_pos = writing_pos = new_writing_pos;
1965 perform_seek_to_offset (queue, new_writing_pos);
1967 update_cur_level (queue, queue->current);
1969 /* update the buffering status */
1970 if (queue->use_buffering)
1971 update_buffering (queue);
1973 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1974 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1976 GST_QUEUE2_SIGNAL_ADD (queue);
1979 gst_buffer_unmap (buffer, &info);
1986 GST_DEBUG_OBJECT (queue, "we are flushing");
1987 gst_buffer_unmap (buffer, &info);
1988 /* FIXME - GST_FLOW_EOS ? */
1993 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1994 gst_buffer_unmap (buffer, &info);
2001 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2005 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2006 (_("Error while writing to download file.")),
2007 ("%s", g_strerror (errno)));
2010 gst_buffer_unmap (buffer, &info);
2016 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2018 GstQueue2 *queue = q;
2020 GST_TRACE_OBJECT (queue,
2021 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2022 gst_buffer_get_size (*buf));
2024 if (!gst_queue2_create_write (queue, *buf)) {
2025 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2032 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2034 guint *p_size = data;
2037 buf_size = gst_buffer_get_size (*buf);
2038 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2039 *p_size += buf_size;
2043 /* enqueue an item an update the level stats */
2045 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2046 GstQueue2ItemType item_type)
2048 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2052 buffer = GST_BUFFER_CAST (item);
2053 size = gst_buffer_get_size (buffer);
2055 /* add buffer to the statistics */
2056 if (QUEUE_IS_USING_QUEUE (queue)) {
2057 queue->cur_level.buffers++;
2058 queue->cur_level.bytes += size;
2060 queue->bytes_in += size;
2062 /* apply new buffer to segment stats */
2063 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
2064 /* update the byterate stats */
2065 update_in_rates (queue);
2067 if (!QUEUE_IS_USING_QUEUE (queue)) {
2068 /* FIXME - check return value? */
2069 gst_queue2_create_write (queue, buffer);
2071 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2072 GstBufferList *buffer_list;
2075 buffer_list = GST_BUFFER_LIST_CAST (item);
2077 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2078 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2080 /* add buffer to the statistics */
2081 if (QUEUE_IS_USING_QUEUE (queue)) {
2082 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2083 queue->cur_level.bytes += size;
2085 queue->bytes_in += size;
2087 /* apply new buffer to segment stats */
2088 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2090 /* update the byterate stats */
2091 update_in_rates (queue);
2093 if (!QUEUE_IS_USING_QUEUE (queue)) {
2094 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2096 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2099 event = GST_EVENT_CAST (item);
2101 switch (GST_EVENT_TYPE (event)) {
2103 /* Zero the thresholds, this makes sure the queue is completely
2104 * filled and we can read all data from the queue. */
2105 GST_DEBUG_OBJECT (queue, "we have EOS");
2106 queue->is_eos = TRUE;
2108 case GST_EVENT_SEGMENT:
2109 apply_segment (queue, event, &queue->sink_segment, TRUE);
2110 /* This is our first new segment, we hold it
2111 * as we can't save it on the temp file */
2112 if (!QUEUE_IS_USING_QUEUE (queue)) {
2113 if (queue->segment_event_received)
2114 goto unexpected_event;
2116 queue->segment_event_received = TRUE;
2117 if (queue->starting_segment != NULL)
2118 gst_event_unref (queue->starting_segment);
2119 queue->starting_segment = event;
2122 /* a new segment allows us to accept more buffers if we got EOS
2123 * from downstream */
2124 queue->unexpected = FALSE;
2127 apply_gap (queue, event, &queue->sink_segment, TRUE);
2129 case GST_EVENT_STREAM_START:
2130 if (!QUEUE_IS_USING_QUEUE (queue)) {
2131 gst_event_replace (&queue->stream_start_event, event);
2132 gst_event_unref (event);
2136 case GST_EVENT_CAPS:{
2139 gst_event_parse_caps (event, &caps);
2140 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2142 if (!QUEUE_IS_USING_QUEUE (queue)) {
2143 GST_LOG ("Dropping caps event, not using queue");
2144 gst_event_unref (event);
2150 if (!QUEUE_IS_USING_QUEUE (queue))
2151 goto unexpected_event;
2154 } else if (GST_IS_QUERY (item)) {
2155 /* Can't happen as we check that in the caller */
2156 if (!QUEUE_IS_USING_QUEUE (queue))
2157 g_assert_not_reached ();
2159 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2160 item, GST_OBJECT_NAME (queue));
2161 /* we can't really unref since we don't know what it is */
2166 /* update the buffering status */
2167 if (queue->use_buffering)
2168 update_buffering (queue);
2170 if (QUEUE_IS_USING_QUEUE (queue)) {
2171 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2172 qitem->type = item_type;
2174 g_queue_push_tail (&queue->queue, qitem);
2176 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2179 GST_QUEUE2_SIGNAL_ADD (queue);
2187 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2189 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2190 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2191 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2192 gst_event_unref (GST_EVENT_CAST (item));
2197 /* dequeue an item from the queue and update level stats */
2198 static GstMiniObject *
2199 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2201 GstMiniObject *item;
2203 if (!QUEUE_IS_USING_QUEUE (queue)) {
2204 item = gst_queue2_read_item_from_file (queue);
2206 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2212 g_slice_free (GstQueue2Item, qitem);
2218 if (GST_IS_BUFFER (item)) {
2222 buffer = GST_BUFFER_CAST (item);
2223 size = gst_buffer_get_size (buffer);
2224 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2226 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2227 "retrieved buffer %p from queue", buffer);
2229 if (QUEUE_IS_USING_QUEUE (queue)) {
2230 queue->cur_level.buffers--;
2231 queue->cur_level.bytes -= size;
2233 queue->bytes_out += size;
2235 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2236 /* update the byterate stats */
2237 update_out_rates (queue);
2238 /* update the buffering */
2239 if (queue->use_buffering)
2240 update_buffering (queue);
2242 } else if (GST_IS_EVENT (item)) {
2243 GstEvent *event = GST_EVENT_CAST (item);
2245 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2247 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2248 "retrieved event %p from queue", event);
2250 switch (GST_EVENT_TYPE (event)) {
2252 /* queue is empty now that we dequeued the EOS */
2253 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2255 case GST_EVENT_SEGMENT:
2256 apply_segment (queue, event, &queue->src_segment, FALSE);
2259 apply_gap (queue, event, &queue->src_segment, FALSE);
2264 } else if (GST_IS_BUFFER_LIST (item)) {
2265 GstBufferList *buffer_list;
2268 buffer_list = GST_BUFFER_LIST_CAST (item);
2269 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2270 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2272 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2273 "retrieved buffer list %p from queue", buffer_list);
2275 if (QUEUE_IS_USING_QUEUE (queue)) {
2276 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2277 queue->cur_level.bytes -= size;
2279 queue->bytes_out += size;
2281 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2282 /* update the byterate stats */
2283 update_out_rates (queue);
2284 /* update the buffering */
2285 if (queue->use_buffering)
2286 update_buffering (queue);
2287 } else if (GST_IS_QUERY (item)) {
2288 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2289 "retrieved query %p from queue", item);
2290 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2293 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2294 item, GST_OBJECT_NAME (queue));
2296 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2298 GST_QUEUE2_SIGNAL_DEL (queue);
2305 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2311 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2314 gboolean ret = TRUE;
2317 queue = GST_QUEUE2 (parent);
2319 switch (GST_EVENT_TYPE (event)) {
2320 case GST_EVENT_FLUSH_START:
2322 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2323 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2325 ret = gst_pad_push_event (queue->srcpad, event);
2327 /* now unblock the chain function */
2328 GST_QUEUE2_MUTEX_LOCK (queue);
2329 queue->srcresult = GST_FLOW_FLUSHING;
2330 queue->sinkresult = GST_FLOW_FLUSHING;
2331 /* unblock the loop and chain functions */
2332 GST_QUEUE2_SIGNAL_ADD (queue);
2333 GST_QUEUE2_SIGNAL_DEL (queue);
2334 queue->last_query = FALSE;
2335 g_cond_signal (&queue->query_handled);
2336 GST_QUEUE2_MUTEX_UNLOCK (queue);
2338 /* make sure it pauses, this should happen since we sent
2339 * flush_start downstream. */
2340 gst_pad_pause_task (queue->srcpad);
2341 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2343 GST_QUEUE2_MUTEX_LOCK (queue);
2344 /* flush the sink pad */
2345 queue->sinkresult = GST_FLOW_FLUSHING;
2346 GST_QUEUE2_SIGNAL_DEL (queue);
2347 queue->last_query = FALSE;
2348 g_cond_signal (&queue->query_handled);
2349 GST_QUEUE2_MUTEX_UNLOCK (queue);
2351 gst_event_unref (event);
2355 case GST_EVENT_FLUSH_STOP:
2357 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2359 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2361 ret = gst_pad_push_event (queue->srcpad, event);
2363 GST_QUEUE2_MUTEX_LOCK (queue);
2364 gst_queue2_locked_flush (queue, FALSE, TRUE);
2365 queue->srcresult = GST_FLOW_OK;
2366 queue->sinkresult = GST_FLOW_OK;
2367 queue->is_eos = FALSE;
2368 queue->unexpected = FALSE;
2369 queue->seeking = FALSE;
2370 /* reset rate counters */
2371 reset_rate_timer (queue);
2372 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2373 queue->srcpad, NULL);
2374 GST_QUEUE2_MUTEX_UNLOCK (queue);
2376 GST_QUEUE2_MUTEX_LOCK (queue);
2377 queue->segment_event_received = FALSE;
2378 queue->is_eos = FALSE;
2379 queue->unexpected = FALSE;
2380 queue->sinkresult = GST_FLOW_OK;
2381 queue->seeking = FALSE;
2382 GST_QUEUE2_MUTEX_UNLOCK (queue);
2384 gst_event_unref (event);
2389 if (GST_EVENT_IS_SERIALIZED (event)) {
2390 /* serialized events go in the queue */
2391 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2392 if (queue->srcresult != GST_FLOW_OK) {
2393 /* Errors in sticky event pushing are no problem and ignored here
2394 * as they will cause more meaningful errors during data flow.
2395 * For EOS events, that are not followed by data flow, we still
2396 * return FALSE here though and report an error.
2398 if (!GST_EVENT_IS_STICKY (event)) {
2399 goto out_flow_error;
2400 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2401 if (queue->srcresult == GST_FLOW_NOT_LINKED
2402 || queue->srcresult < GST_FLOW_EOS) {
2403 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2404 (_("Internal data flow error.")),
2405 ("streaming task paused, reason %s (%d)",
2406 gst_flow_get_name (queue->srcresult), queue->srcresult));
2408 goto out_flow_error;
2411 /* refuse more events on EOS */
2414 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2415 GST_QUEUE2_MUTEX_UNLOCK (queue);
2416 gst_queue2_post_buffering (queue);
2418 /* non-serialized events are passed upstream. */
2419 ret = gst_pad_push_event (queue->srcpad, event);
2428 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2429 GST_QUEUE2_MUTEX_UNLOCK (queue);
2430 gst_event_unref (event);
2435 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2436 GST_QUEUE2_MUTEX_UNLOCK (queue);
2437 gst_event_unref (event);
2442 GST_LOG_OBJECT (queue,
2443 "refusing event, we have a downstream flow error: %s",
2444 gst_flow_get_name (queue->srcresult));
2445 GST_QUEUE2_MUTEX_UNLOCK (queue);
2446 gst_event_unref (event);
2452 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2458 queue = GST_QUEUE2 (parent);
2460 switch (GST_QUERY_TYPE (query)) {
2462 if (GST_QUERY_IS_SERIALIZED (query)) {
2463 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2464 /* serialized events go in the queue. We need to be certain that we
2465 * don't cause deadlocks waiting for the query return value. We check if
2466 * the queue is empty (nothing is blocking downstream and the query can
2467 * be pushed for sure) or we are not buffering. If we are buffering,
2468 * the pipeline waits to unblock downstream until our queue fills up
2469 * completely, which can not happen if we block on the query..
2470 * Therefore we only potentially block when we are not buffering. */
2471 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2472 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2473 || !queue->use_buffering)) {
2474 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2475 gst_queue2_locked_enqueue (queue, query,
2476 GST_QUEUE2_ITEM_TYPE_QUERY);
2478 STATUS (queue, queue->sinkpad, "wait for QUERY");
2479 g_cond_wait (&queue->query_handled, &queue->qlock);
2480 if (queue->sinkresult != GST_FLOW_OK)
2482 res = queue->last_query;
2484 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2488 GST_DEBUG_OBJECT (queue,
2489 "refusing query, we are not using the queue");
2492 GST_QUEUE2_MUTEX_UNLOCK (queue);
2493 gst_queue2_post_buffering (queue);
2495 res = gst_pad_query_default (pad, parent, query);
2504 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2505 GST_QUEUE2_MUTEX_UNLOCK (queue);
2511 gst_queue2_is_empty (GstQueue2 * queue)
2513 /* never empty on EOS */
2517 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2518 return queue->current->writing_pos <= queue->current->max_reading_pos;
2520 if (queue->queue.length == 0)
2528 gst_queue2_is_filled (GstQueue2 * queue)
2532 /* always filled on EOS */
2536 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2537 (queue->cur_level.format) >= ((alt_max) ? \
2538 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2540 /* if using a ring buffer we're filled if all ring buffer space is used
2541 * _by the current range_ */
2542 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2543 guint64 rb_size = queue->ring_buffer_max_size;
2544 GST_DEBUG_OBJECT (queue,
2545 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2546 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2547 return CHECK_FILLED (bytes, rb_size);
2550 /* if using file, we're never filled if we don't have EOS */
2551 if (QUEUE_IS_USING_TEMP_FILE (queue))
2554 /* we are never filled when we have no buffers at all */
2555 if (queue->cur_level.buffers == 0)
2558 /* we are filled if one of the current levels exceeds the max */
2559 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2560 || CHECK_FILLED (time, 0);
2562 /* if we need to, use the rate estimate to check against the max time we are
2563 * allowed to queue */
2564 if (queue->use_rate_estimate)
2565 res |= CHECK_FILLED (rate_time, 0);
2571 static GstFlowReturn
2572 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2573 GstMiniObject * item, GstQueue2ItemType item_type)
2575 /* we have to lock the queue since we span threads */
2576 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2577 /* when we received EOS, we refuse more data */
2580 /* when we received unexpected from downstream, refuse more buffers */
2581 if (queue->unexpected)
2582 goto out_unexpected;
2584 /* while we didn't receive the newsegment, we're seeking and we skip data */
2588 if (!gst_queue2_wait_free_space (queue))
2591 /* put buffer in queue now */
2592 gst_queue2_locked_enqueue (queue, item, item_type);
2593 GST_QUEUE2_MUTEX_UNLOCK (queue);
2594 gst_queue2_post_buffering (queue);
2598 /* special conditions */
2601 GstFlowReturn ret = queue->sinkresult;
2603 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2604 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2605 GST_QUEUE2_MUTEX_UNLOCK (queue);
2606 gst_mini_object_unref (item);
2612 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2613 GST_QUEUE2_MUTEX_UNLOCK (queue);
2614 gst_mini_object_unref (item);
2616 return GST_FLOW_EOS;
2620 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2621 GST_QUEUE2_MUTEX_UNLOCK (queue);
2622 gst_mini_object_unref (item);
2628 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2629 GST_QUEUE2_MUTEX_UNLOCK (queue);
2630 gst_mini_object_unref (item);
2632 return GST_FLOW_EOS;
2636 static GstFlowReturn
2637 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2641 queue = GST_QUEUE2 (parent);
2643 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2644 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2645 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2646 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2647 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2649 return gst_queue2_chain_buffer_or_buffer_list (queue,
2650 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2653 static GstFlowReturn
2654 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2655 GstBufferList * buffer_list)
2659 queue = GST_QUEUE2 (parent);
2661 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2662 "received buffer list %p", buffer_list);
2664 return gst_queue2_chain_buffer_or_buffer_list (queue,
2665 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2668 static GstMiniObject *
2669 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2671 GstMiniObject *data;
2673 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2675 /* stop pushing buffers, we dequeue all items until we see an item that we
2676 * can push again, which is EOS or SEGMENT. If there is nothing in the
2677 * queue we can push, we set a flag to make the sinkpad refuse more
2678 * buffers with an EOS return value until we receive something
2679 * pushable again or we get flushed. */
2680 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2681 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2682 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2683 "dropping EOS buffer %p", data);
2684 gst_buffer_unref (GST_BUFFER_CAST (data));
2685 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2686 GstEvent *event = GST_EVENT_CAST (data);
2687 GstEventType type = GST_EVENT_TYPE (event);
2689 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2690 /* we found a pushable item in the queue, push it out */
2691 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2692 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2695 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2696 "dropping EOS event %p", event);
2697 gst_event_unref (event);
2698 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2699 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2700 "dropping EOS buffer list %p", data);
2701 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2702 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2703 queue->last_query = FALSE;
2704 g_cond_signal (&queue->query_handled);
2705 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2708 /* no more items in the queue. Set the unexpected flag so that upstream
2709 * make us refuse any more buffers on the sinkpad. Since we will still
2710 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2711 * task function does not shut down. */
2712 queue->unexpected = TRUE;
2716 /* dequeue an item from the queue an push it downstream. This functions returns
2717 * the result of the push. */
2718 static GstFlowReturn
2719 gst_queue2_push_one (GstQueue2 * queue)
2721 GstFlowReturn result = queue->srcresult;
2722 GstMiniObject *data;
2723 GstQueue2ItemType item_type;
2725 data = gst_queue2_locked_dequeue (queue, &item_type);
2730 STATUS (queue, queue->srcpad, "We have something dequeud");
2731 g_atomic_int_set (&queue->downstream_may_block,
2732 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2733 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2734 GST_QUEUE2_MUTEX_UNLOCK (queue);
2735 gst_queue2_post_buffering (queue);
2737 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2740 buffer = GST_BUFFER_CAST (data);
2742 result = gst_pad_push (queue->srcpad, buffer);
2743 g_atomic_int_set (&queue->downstream_may_block, 0);
2745 /* need to check for srcresult here as well */
2746 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2747 if (result == GST_FLOW_EOS) {
2748 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2751 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2752 * to the caller so that the task function does not shut down */
2753 result = GST_FLOW_OK;
2755 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2756 GstEvent *event = GST_EVENT_CAST (data);
2757 GstEventType type = GST_EVENT_TYPE (event);
2759 gst_pad_push_event (queue->srcpad, event);
2761 /* if we're EOS, return EOS so that the task pauses. */
2762 if (type == GST_EVENT_EOS) {
2763 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2764 "pushed EOS event %p, return EOS", event);
2765 result = GST_FLOW_EOS;
2768 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2769 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2770 GstBufferList *buffer_list;
2772 buffer_list = GST_BUFFER_LIST_CAST (data);
2774 result = gst_pad_push_list (queue->srcpad, buffer_list);
2775 g_atomic_int_set (&queue->downstream_may_block, 0);
2777 /* need to check for srcresult here as well */
2778 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2779 if (result == GST_FLOW_EOS) {
2780 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2783 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2784 * to the caller so that the task function does not shut down */
2785 result = GST_FLOW_OK;
2787 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2788 GstQuery *query = GST_QUERY_CAST (data);
2790 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2791 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2792 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2793 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2794 "did query %p, return %d", query, queue->last_query);
2795 g_cond_signal (&queue->query_handled);
2796 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2797 result = GST_FLOW_OK;
2804 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2805 "exit because we have no item in the queue");
2806 return GST_FLOW_ERROR;
2810 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2811 return GST_FLOW_FLUSHING;
2815 /* called repeatedly with @pad as the source pad. This function should push out
2816 * data to the peer element. */
2818 gst_queue2_loop (GstPad * pad)
2823 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2825 /* have to lock for thread-safety */
2826 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2828 if (gst_queue2_is_empty (queue)) {
2831 /* pause the timer while we wait. The fact that we are waiting does not mean
2832 * the byterate on the output pad is lower */
2833 if ((started = queue->out_timer_started))
2834 g_timer_stop (queue->out_timer);
2836 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2837 "queue is empty, waiting for new data");
2839 /* Wait for data to be available, we could be unlocked because of a flush. */
2840 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2842 while (gst_queue2_is_empty (queue));
2844 /* and continue if we were running before */
2846 g_timer_continue (queue->out_timer);
2848 ret = gst_queue2_push_one (queue);
2849 queue->srcresult = ret;
2850 queue->sinkresult = ret;
2851 if (ret != GST_FLOW_OK)
2854 GST_QUEUE2_MUTEX_UNLOCK (queue);
2855 gst_queue2_post_buffering (queue);
2862 gboolean eos = queue->is_eos;
2863 GstFlowReturn ret = queue->srcresult;
2865 gst_pad_pause_task (queue->srcpad);
2866 if (ret == GST_FLOW_FLUSHING) {
2867 gst_queue2_locked_flush (queue, FALSE, FALSE);
2869 GST_QUEUE2_SIGNAL_DEL (queue);
2870 queue->last_query = FALSE;
2871 g_cond_signal (&queue->query_handled);
2873 GST_QUEUE2_MUTEX_UNLOCK (queue);
2874 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2875 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2876 /* let app know about us giving up if upstream is not expected to do so */
2877 /* EOS is already taken care of elsewhere */
2878 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2879 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2880 (_("Internal data flow error.")),
2881 ("streaming task paused, reason %s (%d)",
2882 gst_flow_get_name (ret), ret));
2883 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2890 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2892 gboolean res = TRUE;
2893 GstQueue2 *queue = GST_QUEUE2 (parent);
2895 #ifndef GST_DISABLE_GST_DEBUG
2896 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2897 event, GST_EVENT_TYPE_NAME (event));
2900 switch (GST_EVENT_TYPE (event)) {
2901 case GST_EVENT_FLUSH_START:
2902 if (QUEUE_IS_USING_QUEUE (queue)) {
2903 /* just forward upstream */
2904 res = gst_pad_push_event (queue->sinkpad, event);
2906 /* now unblock the getrange function */
2907 GST_QUEUE2_MUTEX_LOCK (queue);
2908 GST_DEBUG_OBJECT (queue, "flushing");
2909 queue->srcresult = GST_FLOW_FLUSHING;
2910 GST_QUEUE2_SIGNAL_ADD (queue);
2911 GST_QUEUE2_MUTEX_UNLOCK (queue);
2913 /* when using a temp file, we eat the event */
2915 gst_event_unref (event);
2918 case GST_EVENT_FLUSH_STOP:
2919 if (QUEUE_IS_USING_QUEUE (queue)) {
2920 /* just forward upstream */
2921 res = gst_pad_push_event (queue->sinkpad, event);
2923 /* now unblock the getrange function */
2924 GST_QUEUE2_MUTEX_LOCK (queue);
2925 queue->srcresult = GST_FLOW_OK;
2926 GST_QUEUE2_MUTEX_UNLOCK (queue);
2928 /* when using a temp file, we eat the event */
2930 gst_event_unref (event);
2933 case GST_EVENT_RECONFIGURE:
2934 GST_QUEUE2_MUTEX_LOCK (queue);
2935 /* assume downstream is linked now and try to push again */
2936 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2937 queue->srcresult = GST_FLOW_OK;
2938 queue->sinkresult = GST_FLOW_OK;
2939 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2940 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2944 GST_QUEUE2_MUTEX_UNLOCK (queue);
2946 res = gst_pad_push_event (queue->sinkpad, event);
2949 res = gst_pad_push_event (queue->sinkpad, event);
2957 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2961 queue = GST_QUEUE2 (parent);
2963 switch (GST_QUERY_TYPE (query)) {
2964 case GST_QUERY_POSITION:
2969 if (!gst_pad_peer_query (queue->sinkpad, query))
2972 /* get peer position */
2973 gst_query_parse_position (query, &format, &peer_pos);
2975 /* FIXME: this code assumes that there's no discont in the queue */
2977 case GST_FORMAT_BYTES:
2978 peer_pos -= queue->cur_level.bytes;
2980 case GST_FORMAT_TIME:
2981 peer_pos -= queue->cur_level.time;
2984 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2985 "know how to adjust value", gst_format_get_name (format));
2988 /* set updated position */
2989 gst_query_set_position (query, format, peer_pos);
2992 case GST_QUERY_DURATION:
2994 GST_DEBUG_OBJECT (queue, "doing peer query");
2996 if (!gst_pad_peer_query (queue->sinkpad, query))
2999 GST_DEBUG_OBJECT (queue, "peer query success");
3002 case GST_QUERY_BUFFERING:
3005 gboolean is_buffering;
3006 GstBufferingMode mode;
3007 gint avg_in, avg_out;
3008 gint64 buffering_left;
3010 GST_DEBUG_OBJECT (queue, "query buffering");
3012 get_buffering_percent (queue, &is_buffering, &percent);
3013 gst_query_set_buffering_percent (query, is_buffering, percent);
3015 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3017 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3020 if (!QUEUE_IS_USING_QUEUE (queue)) {
3021 /* add ranges for download and ringbuffer buffering */
3023 gint64 start, stop, range_start, range_stop;
3024 guint64 writing_pos;
3025 gint64 estimated_total;
3027 gboolean peer_res, is_eos;
3028 GstQueue2Range *queued_ranges;
3030 /* we need a current download region */
3031 if (queue->current == NULL)
3034 writing_pos = queue->current->writing_pos;
3035 is_eos = queue->is_eos;
3038 /* we're EOS, we know the duration in bytes now */
3040 duration = writing_pos;
3042 /* get duration of upstream in bytes */
3043 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3044 GST_FORMAT_BYTES, &duration);
3047 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3048 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3050 /* calculate remaining and total download time */
3051 if (peer_res && avg_in > 0.0)
3052 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3054 estimated_total = -1;
3056 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3059 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3062 case GST_FORMAT_PERCENT:
3063 /* we need duration */
3068 /* get our available data relative to the duration */
3071 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3076 case GST_FORMAT_BYTES:
3086 /* fill out the buffered ranges */
3087 for (queued_ranges = queue->ranges; queued_ranges;
3088 queued_ranges = queued_ranges->next) {
3090 case GST_FORMAT_PERCENT:
3091 if (duration == -1) {
3097 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3098 queued_ranges->offset, duration);
3100 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3101 queued_ranges->writing_pos, duration);
3103 case GST_FORMAT_BYTES:
3104 range_start = queued_ranges->offset;
3105 range_stop = queued_ranges->writing_pos;
3112 if (range_start == range_stop)
3114 GST_DEBUG_OBJECT (queue,
3115 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3116 G_GINT64_FORMAT, range_start, range_stop);
3117 gst_query_add_buffering_range (query, range_start, range_stop);
3120 gst_query_set_buffering_range (query, format, start, stop,
3125 case GST_QUERY_SCHEDULING:
3128 GstSchedulingFlags flags = 0;
3130 if (!gst_pad_peer_query (queue->sinkpad, query))
3133 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3135 /* we can operate in pull mode when we are using a tempfile */
3136 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3139 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3140 gst_query_set_scheduling (query, flags, 0, -1, 0);
3142 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3143 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3147 /* peer handled other queries */
3148 if (!gst_pad_query_default (pad, parent, query))
3158 GST_DEBUG_OBJECT (queue, "failed peer query");
3164 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3166 GstQueue2 *queue = GST_QUEUE2 (element);
3168 /* simply forward to the srcpad query function */
3169 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3174 gst_queue2_update_upstream_size (GstQueue2 * queue)
3176 gint64 upstream_size = -1;
3178 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3180 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3182 /* upstream_size can be negative but queue->upstream_size is unsigned.
3183 * Prevent setting negative values to it (the query can return -1) */
3184 if (upstream_size >= 0)
3185 queue->upstream_size = upstream_size;
3187 queue->upstream_size = 0;
3191 static GstFlowReturn
3192 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3193 guint length, GstBuffer ** buffer)
3198 queue = GST_QUEUE2_CAST (parent);
3200 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3201 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3202 offset = (offset == -1) ? queue->current->reading_pos : offset;
3204 GST_DEBUG_OBJECT (queue,
3205 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3207 /* catch any reads beyond the size of the file here to make sure queue2
3208 * doesn't send seek events beyond the size of the file upstream, since
3209 * that would confuse elements such as souphttpsrc and/or http servers.
3210 * Demuxers often just loop until EOS at the end of the file to figure out
3211 * when they've read all the end-headers or index chunks. */
3212 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3213 gst_queue2_update_upstream_size (queue);
3214 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3215 goto out_unexpected;
3218 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3219 gst_queue2_update_upstream_size (queue);
3220 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3221 length = queue->upstream_size - offset;
3222 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3226 /* FIXME - function will block when the range is not yet available */
3227 ret = gst_queue2_create_read (queue, offset, length, buffer);
3228 GST_QUEUE2_MUTEX_UNLOCK (queue);
3229 gst_queue2_post_buffering (queue);
3236 ret = queue->srcresult;
3238 GST_DEBUG_OBJECT (queue, "we are flushing");
3239 GST_QUEUE2_MUTEX_UNLOCK (queue);
3244 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3245 GST_QUEUE2_MUTEX_UNLOCK (queue);
3246 return GST_FLOW_EOS;
3250 /* sink currently only operates in push mode */
3252 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3253 GstPadMode mode, gboolean active)
3258 queue = GST_QUEUE2 (parent);
3261 case GST_PAD_MODE_PUSH:
3263 GST_QUEUE2_MUTEX_LOCK (queue);
3264 GST_DEBUG_OBJECT (queue, "activating push mode");
3265 queue->srcresult = GST_FLOW_OK;
3266 queue->sinkresult = GST_FLOW_OK;
3267 queue->is_eos = FALSE;
3268 queue->unexpected = FALSE;
3269 reset_rate_timer (queue);
3270 GST_QUEUE2_MUTEX_UNLOCK (queue);
3272 /* unblock chain function */
3273 GST_QUEUE2_MUTEX_LOCK (queue);
3274 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3275 queue->srcresult = GST_FLOW_FLUSHING;
3276 queue->sinkresult = GST_FLOW_FLUSHING;
3277 GST_QUEUE2_SIGNAL_DEL (queue);
3278 /* Unblock query handler */
3279 queue->last_query = FALSE;
3280 g_cond_signal (&queue->query_handled);
3281 GST_QUEUE2_MUTEX_UNLOCK (queue);
3283 /* wait until it is unblocked and clean up */
3284 GST_PAD_STREAM_LOCK (pad);
3285 GST_QUEUE2_MUTEX_LOCK (queue);
3286 gst_queue2_locked_flush (queue, TRUE, FALSE);
3287 GST_QUEUE2_MUTEX_UNLOCK (queue);
3288 GST_PAD_STREAM_UNLOCK (pad);
3299 /* src operating in push mode, we start a task on the source pad that pushes out
3300 * buffers from the queue */
3302 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3304 gboolean result = FALSE;
3307 queue = GST_QUEUE2 (parent);
3310 GST_QUEUE2_MUTEX_LOCK (queue);
3311 GST_DEBUG_OBJECT (queue, "activating push mode");
3312 queue->srcresult = GST_FLOW_OK;
3313 queue->sinkresult = GST_FLOW_OK;
3314 queue->is_eos = FALSE;
3315 queue->unexpected = FALSE;
3317 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3318 GST_QUEUE2_MUTEX_UNLOCK (queue);
3320 /* unblock loop function */
3321 GST_QUEUE2_MUTEX_LOCK (queue);
3322 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3323 queue->srcresult = GST_FLOW_FLUSHING;
3324 queue->sinkresult = GST_FLOW_FLUSHING;
3325 /* the item add signal will unblock */
3326 GST_QUEUE2_SIGNAL_ADD (queue);
3327 GST_QUEUE2_MUTEX_UNLOCK (queue);
3329 /* step 2, make sure streaming finishes */
3330 result = gst_pad_stop_task (pad);
3336 /* pull mode, downstream will call our getrange function */
3338 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3343 queue = GST_QUEUE2 (parent);
3346 GST_QUEUE2_MUTEX_LOCK (queue);
3347 if (!QUEUE_IS_USING_QUEUE (queue)) {
3348 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3349 /* open the temp file now */
3350 result = gst_queue2_open_temp_location_file (queue);
3351 } else if (!queue->ring_buffer) {
3352 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3353 result = ! !queue->ring_buffer;
3358 GST_DEBUG_OBJECT (queue, "activating pull mode");
3359 init_ranges (queue);
3360 queue->srcresult = GST_FLOW_OK;
3361 queue->sinkresult = GST_FLOW_OK;
3362 queue->is_eos = FALSE;
3363 queue->unexpected = FALSE;
3364 queue->upstream_size = 0;
3366 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3367 /* this is not allowed, we cannot operate in pull mode without a temp
3369 queue->srcresult = GST_FLOW_FLUSHING;
3370 queue->sinkresult = GST_FLOW_FLUSHING;
3373 GST_QUEUE2_MUTEX_UNLOCK (queue);
3375 GST_QUEUE2_MUTEX_LOCK (queue);
3376 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3377 queue->srcresult = GST_FLOW_FLUSHING;
3378 queue->sinkresult = GST_FLOW_FLUSHING;
3379 /* this will unlock getrange */
3380 GST_QUEUE2_SIGNAL_ADD (queue);
3382 GST_QUEUE2_MUTEX_UNLOCK (queue);
3389 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3395 case GST_PAD_MODE_PULL:
3396 res = gst_queue2_src_activate_pull (pad, parent, active);
3398 case GST_PAD_MODE_PUSH:
3399 res = gst_queue2_src_activate_push (pad, parent, active);
3402 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3409 static GstStateChangeReturn
3410 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3413 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3415 queue = GST_QUEUE2 (element);
3417 switch (transition) {
3418 case GST_STATE_CHANGE_NULL_TO_READY:
3420 case GST_STATE_CHANGE_READY_TO_PAUSED:
3421 GST_QUEUE2_MUTEX_LOCK (queue);
3422 if (!QUEUE_IS_USING_QUEUE (queue)) {
3423 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3424 if (!gst_queue2_open_temp_location_file (queue))
3425 ret = GST_STATE_CHANGE_FAILURE;
3427 if (queue->ring_buffer) {
3428 g_free (queue->ring_buffer);
3429 queue->ring_buffer = NULL;
3431 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3432 ret = GST_STATE_CHANGE_FAILURE;
3434 init_ranges (queue);
3436 queue->segment_event_received = FALSE;
3437 queue->starting_segment = NULL;
3438 gst_event_replace (&queue->stream_start_event, NULL);
3439 GST_QUEUE2_MUTEX_UNLOCK (queue);
3441 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3447 if (ret == GST_STATE_CHANGE_FAILURE)
3450 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3452 if (ret == GST_STATE_CHANGE_FAILURE)
3455 switch (transition) {
3456 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3458 case GST_STATE_CHANGE_PAUSED_TO_READY:
3459 GST_QUEUE2_MUTEX_LOCK (queue);
3460 if (!QUEUE_IS_USING_QUEUE (queue)) {
3461 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3462 gst_queue2_close_temp_location_file (queue);
3463 } else if (queue->ring_buffer) {
3464 g_free (queue->ring_buffer);
3465 queue->ring_buffer = NULL;
3467 clean_ranges (queue);
3469 if (queue->starting_segment != NULL) {
3470 gst_event_unref (queue->starting_segment);
3471 queue->starting_segment = NULL;
3473 gst_event_replace (&queue->stream_start_event, NULL);
3474 GST_QUEUE2_MUTEX_UNLOCK (queue);
3476 case GST_STATE_CHANGE_READY_TO_NULL:
3485 /* changing the capacity of the queue must wake up
3486 * the _chain function, it might have more room now
3487 * to store the buffer/event in the queue */
3488 #define QUEUE_CAPACITY_CHANGE(q) \
3489 GST_QUEUE2_SIGNAL_DEL (queue); \
3490 if (queue->use_buffering) \
3491 update_buffering (queue);
3493 /* Changing the minimum required fill level must
3494 * wake up the _loop function as it might now
3495 * be able to preceed.
3497 #define QUEUE_THRESHOLD_CHANGE(q)\
3498 GST_QUEUE2_SIGNAL_ADD (queue);
3501 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3505 /* the element must be stopped in order to do this */
3506 GST_OBJECT_LOCK (queue);
3507 state = GST_STATE (queue);
3508 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3510 GST_OBJECT_UNLOCK (queue);
3512 /* set new location */
3513 g_free (queue->temp_template);
3514 queue->temp_template = g_strdup (template);
3521 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3522 GST_OBJECT_UNLOCK (queue);
3527 gst_queue2_set_property (GObject * object,
3528 guint prop_id, const GValue * value, GParamSpec * pspec)
3530 GstQueue2 *queue = GST_QUEUE2 (object);
3532 /* someone could change levels here, and since this
3533 * affects the get/put funcs, we need to lock for safety. */
3534 GST_QUEUE2_MUTEX_LOCK (queue);
3537 case PROP_MAX_SIZE_BYTES:
3538 queue->max_level.bytes = g_value_get_uint (value);
3539 QUEUE_CAPACITY_CHANGE (queue);
3541 case PROP_MAX_SIZE_BUFFERS:
3542 queue->max_level.buffers = g_value_get_uint (value);
3543 QUEUE_CAPACITY_CHANGE (queue);
3545 case PROP_MAX_SIZE_TIME:
3546 queue->max_level.time = g_value_get_uint64 (value);
3547 /* set rate_time to the same value. We use an extra field in the level
3548 * structure so that we can easily access and compare it */
3549 queue->max_level.rate_time = queue->max_level.time;
3550 QUEUE_CAPACITY_CHANGE (queue);
3552 case PROP_USE_BUFFERING:
3553 queue->use_buffering = g_value_get_boolean (value);
3554 if (!queue->use_buffering && queue->is_buffering) {
3555 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3556 "posting 100%% message");
3557 SET_PERCENT (queue, 100);
3558 queue->is_buffering = FALSE;
3561 if (queue->use_buffering) {
3562 queue->is_buffering = TRUE;
3563 update_buffering (queue);
3566 case PROP_USE_RATE_ESTIMATE:
3567 queue->use_rate_estimate = g_value_get_boolean (value);
3569 case PROP_LOW_PERCENT:
3570 queue->low_percent = g_value_get_int (value);
3572 case PROP_HIGH_PERCENT:
3573 queue->high_percent = g_value_get_int (value);
3575 case PROP_TEMP_TEMPLATE:
3576 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3578 case PROP_TEMP_REMOVE:
3579 queue->temp_remove = g_value_get_boolean (value);
3581 case PROP_RING_BUFFER_MAX_SIZE:
3582 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3585 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3589 GST_QUEUE2_MUTEX_UNLOCK (queue);
3590 gst_queue2_post_buffering (queue);
3594 gst_queue2_get_property (GObject * object,
3595 guint prop_id, GValue * value, GParamSpec * pspec)
3597 GstQueue2 *queue = GST_QUEUE2 (object);
3599 GST_QUEUE2_MUTEX_LOCK (queue);
3602 case PROP_CUR_LEVEL_BYTES:
3603 g_value_set_uint (value, queue->cur_level.bytes);
3605 case PROP_CUR_LEVEL_BUFFERS:
3606 g_value_set_uint (value, queue->cur_level.buffers);
3608 case PROP_CUR_LEVEL_TIME:
3609 g_value_set_uint64 (value, queue->cur_level.time);
3611 case PROP_MAX_SIZE_BYTES:
3612 g_value_set_uint (value, queue->max_level.bytes);
3614 case PROP_MAX_SIZE_BUFFERS:
3615 g_value_set_uint (value, queue->max_level.buffers);
3617 case PROP_MAX_SIZE_TIME:
3618 g_value_set_uint64 (value, queue->max_level.time);
3620 case PROP_USE_BUFFERING:
3621 g_value_set_boolean (value, queue->use_buffering);
3623 case PROP_USE_RATE_ESTIMATE:
3624 g_value_set_boolean (value, queue->use_rate_estimate);
3626 case PROP_LOW_PERCENT:
3627 g_value_set_int (value, queue->low_percent);
3629 case PROP_HIGH_PERCENT:
3630 g_value_set_int (value, queue->high_percent);
3632 case PROP_TEMP_TEMPLATE:
3633 g_value_set_string (value, queue->temp_template);
3635 case PROP_TEMP_LOCATION:
3636 g_value_set_string (value, queue->temp_location);
3638 case PROP_TEMP_REMOVE:
3639 g_value_set_boolean (value, queue->temp_remove);
3641 case PROP_RING_BUFFER_MAX_SIZE:
3642 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3644 case PROP_AVG_IN_RATE:
3646 gdouble in_rate = queue->byte_in_rate;
3648 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3649 * calculated, so calculate it here. */
3650 if (in_rate == 0.0 && queue->bytes_in
3651 && queue->last_update_in_rates_elapsed > 0.0)
3652 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3654 g_value_set_int64 (value, (gint64) in_rate);
3658 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3662 GST_QUEUE2_MUTEX_UNLOCK (queue);