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 /* figure out the percent we are filled, we take the max of all formats. */
879 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
880 perc = GET_PERCENT (bytes, 0);
882 guint64 rb_size = queue->ring_buffer_max_size;
883 perc = GET_PERCENT (bytes, rb_size);
885 perc = MAX (perc, GET_PERCENT (time, 0));
886 perc = MAX (perc, GET_PERCENT (buffers, 0));
888 /* also apply the rate estimate when we need to */
889 if (queue->use_rate_estimate)
890 perc = MAX (perc, GET_PERCENT (rate_time, 0));
895 *is_buffering = queue->is_buffering;
897 /* scale to high percent so that it becomes the 100% mark */
898 perc = perc * 100 / queue->high_percent;
906 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
913 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
914 gint * avg_in, gint * avg_out, gint64 * buffering_left)
917 if (!QUEUE_IS_USING_QUEUE (queue)) {
918 if (QUEUE_IS_USING_RING_BUFFER (queue))
919 *mode = GST_BUFFERING_TIMESHIFT;
921 *mode = GST_BUFFERING_DOWNLOAD;
923 *mode = GST_BUFFERING_STREAM;
928 *avg_in = queue->byte_in_rate;
930 *avg_out = queue->byte_out_rate;
932 if (buffering_left) {
933 *buffering_left = (percent == 100 ? 0 : -1);
935 if (queue->use_rate_estimate) {
938 max = queue->max_level.rate_time;
939 cur = queue->cur_level.rate_time;
941 if (percent != 100 && max > cur)
942 *buffering_left = (max - cur) / 1000000;
948 gst_queue2_post_buffering (GstQueue2 * queue)
950 GstMessage *msg = NULL;
952 g_mutex_lock (&queue->buffering_post_lock);
953 GST_QUEUE2_MUTEX_LOCK (queue);
954 if (queue->percent_changed) {
955 gint percent = queue->buffering_percent;
957 queue->percent_changed = FALSE;
959 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
960 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
962 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
963 queue->avg_out, queue->buffering_left);
965 GST_QUEUE2_MUTEX_UNLOCK (queue);
968 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
970 g_mutex_unlock (&queue->buffering_post_lock);
974 update_buffering (GstQueue2 * queue)
978 /* Ensure the variables used to calculate buffering state are up-to-date. */
980 update_cur_level (queue, queue->current);
981 update_in_rates (queue);
983 if (!get_buffering_percent (queue, NULL, &percent))
986 if (queue->is_buffering) {
987 /* if we were buffering see if we reached the high watermark */
989 queue->is_buffering = FALSE;
991 SET_PERCENT (queue, percent);
993 /* we were not buffering, check if we need to start buffering if we drop
994 * below the low threshold */
995 if (percent < queue->low_percent) {
996 queue->is_buffering = TRUE;
997 SET_PERCENT (queue, percent);
1003 reset_rate_timer (GstQueue2 * queue)
1005 queue->bytes_in = 0;
1006 queue->bytes_out = 0;
1007 queue->byte_in_rate = 0.0;
1008 queue->byte_in_period = 0;
1009 queue->byte_out_rate = 0.0;
1010 queue->last_update_in_rates_elapsed = 0.0;
1011 queue->last_in_elapsed = 0.0;
1012 queue->last_out_elapsed = 0.0;
1013 queue->in_timer_started = FALSE;
1014 queue->out_timer_started = FALSE;
1017 /* the interval in seconds to recalculate the rate */
1018 #define RATE_INTERVAL 0.2
1019 /* Tuning for rate estimation. We use a large window for the input rate because
1020 * it should be stable when connected to a network. The output rate is less
1021 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1022 * therefore adapt more quickly.
1023 * However, initial input rate may be subject to a burst, and should therefore
1024 * initially also adapt more quickly to changes, and only later on give higher
1025 * weight to previous values. */
1026 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1027 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1030 update_in_rates (GstQueue2 * queue)
1032 gdouble elapsed, period;
1033 gdouble byte_in_rate;
1035 if (!queue->in_timer_started) {
1036 queue->in_timer_started = TRUE;
1037 g_timer_start (queue->in_timer);
1041 queue->last_update_in_rates_elapsed = elapsed =
1042 g_timer_elapsed (queue->in_timer, NULL);
1044 /* recalc after each interval. */
1045 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1046 period = elapsed - queue->last_in_elapsed;
1048 GST_DEBUG_OBJECT (queue,
1049 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1050 period, queue->bytes_in, queue->byte_in_period);
1052 byte_in_rate = queue->bytes_in / period;
1054 if (queue->byte_in_rate == 0.0)
1055 queue->byte_in_rate = byte_in_rate;
1057 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1058 (double) queue->byte_in_period, period);
1060 /* another data point, cap at 16 for long time running average */
1061 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1062 queue->byte_in_period += period;
1064 /* reset the values to calculate rate over the next interval */
1065 queue->last_in_elapsed = elapsed;
1066 queue->bytes_in = 0;
1069 if (queue->byte_in_rate > 0.0) {
1070 queue->cur_level.rate_time =
1071 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1073 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1074 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1078 update_out_rates (GstQueue2 * queue)
1080 gdouble elapsed, period;
1081 gdouble byte_out_rate;
1083 if (!queue->out_timer_started) {
1084 queue->out_timer_started = TRUE;
1085 g_timer_start (queue->out_timer);
1089 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1091 /* recalc after each interval. */
1092 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1093 period = elapsed - queue->last_out_elapsed;
1095 GST_DEBUG_OBJECT (queue,
1096 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1098 byte_out_rate = queue->bytes_out / period;
1100 if (queue->byte_out_rate == 0.0)
1101 queue->byte_out_rate = byte_out_rate;
1103 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1105 /* reset the values to calculate rate over the next interval */
1106 queue->last_out_elapsed = elapsed;
1107 queue->bytes_out = 0;
1109 if (queue->byte_in_rate > 0.0) {
1110 queue->cur_level.rate_time =
1111 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1113 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1114 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1118 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1120 guint64 reading_pos, max_reading_pos;
1123 max_reading_pos = range->max_reading_pos;
1125 max_reading_pos = MAX (max_reading_pos, reading_pos);
1127 GST_DEBUG_OBJECT (queue,
1128 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1129 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1130 range->max_reading_pos = max_reading_pos;
1132 update_cur_level (queue, range);
1136 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1141 /* until we receive the FLUSH_STOP from this seek, we skip data */
1142 queue->seeking = TRUE;
1143 GST_QUEUE2_MUTEX_UNLOCK (queue);
1145 debug_ranges (queue);
1147 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1150 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1151 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1152 GST_SEEK_TYPE_NONE, -1);
1154 res = gst_pad_push_event (queue->sinkpad, event);
1155 GST_QUEUE2_MUTEX_LOCK (queue);
1158 /* Between us sending the seek event and re-acquiring the lock, the source
1159 * thread might already have pushed data and moved along the range's
1160 * writing_pos beyond the seek offset. In that case we don't want to set
1161 * the writing position back to the requested seek position, as it would
1162 * cause data to be written to the wrong offset in the file or ring buffer.
1163 * We still do the add_range call to switch the current range to the
1164 * requested range, or create one if one doesn't exist yet. */
1165 queue->current = add_range (queue, offset, FALSE);
1171 /* get the threshold for when we decide to seek rather than wait */
1173 get_seek_threshold (GstQueue2 * queue)
1177 /* FIXME, find a good threshold based on the incoming rate. */
1178 threshold = 1024 * 512;
1180 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1181 threshold = MIN (threshold,
1182 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1187 /* see if there is enough data in the file to read a full buffer */
1189 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1191 GstQueue2Range *range;
1193 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1196 if ((range = find_range (queue, offset))) {
1197 if (queue->current != range) {
1198 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1199 perform_seek_to_offset (queue, range->writing_pos);
1202 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1203 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1205 /* we have a range for offset */
1206 GST_DEBUG_OBJECT (queue,
1207 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1208 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1210 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1213 if (offset + length <= range->writing_pos)
1216 GST_DEBUG_OBJECT (queue,
1217 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1218 (offset + length) - range->writing_pos);
1221 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1222 " len %u", offset, length);
1223 /* we don't have the range, see how far away we are */
1224 if (!queue->is_eos && queue->current) {
1225 guint64 threshold = get_seek_threshold (queue);
1227 if (offset >= queue->current->offset && offset <=
1228 queue->current->writing_pos + threshold) {
1229 GST_INFO_OBJECT (queue,
1230 "requested data is within range, wait for data");
1235 /* too far away, do a seek */
1236 perform_seek_to_offset (queue, offset);
1243 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1244 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1245 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1247 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1250 static GstFlowReturn
1251 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1252 guint8 * dst, gint64 * read_return)
1254 guint8 *ring_buffer;
1257 ring_buffer = queue->ring_buffer;
1259 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1262 /* this should not block */
1263 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1265 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1266 res = fread (dst, 1, length, queue->temp_file);
1268 memcpy (dst, ring_buffer + offset, length);
1272 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1274 if (G_UNLIKELY (res < length)) {
1275 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1276 goto could_not_read;
1277 /* check for errors or EOF */
1278 if (ferror (queue->temp_file))
1279 goto could_not_read;
1280 if (feof (queue->temp_file) && length > 0)
1290 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1291 return GST_FLOW_ERROR;
1295 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1296 return GST_FLOW_ERROR;
1300 GST_DEBUG ("non-regular file hits EOS");
1301 return GST_FLOW_EOS;
1305 static GstFlowReturn
1306 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1307 GstBuffer ** buffer)
1312 guint64 file_offset;
1313 guint block_length, remaining, read_length;
1317 GstFlowReturn ret = GST_FLOW_OK;
1319 /* allocate the output buffer of the requested size */
1320 if (*buffer == NULL)
1321 buf = gst_buffer_new_allocate (NULL, length, NULL);
1325 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1328 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1332 rb_size = queue->ring_buffer_max_size;
1333 max_size = QUEUE_MAX_BYTES (queue);
1336 while (remaining > 0) {
1337 /* configure how much/whether to read */
1338 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1341 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1344 /* calculate how far away the offset is */
1345 if (queue->current->writing_pos > rpos)
1346 level = queue->current->writing_pos - rpos;
1350 GST_DEBUG_OBJECT (queue,
1351 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1352 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1353 rpos, queue->current->writing_pos, level, max_size);
1355 if (level >= max_size) {
1356 /* we don't have the data but if we have a ring buffer that is full, we
1358 GST_DEBUG_OBJECT (queue,
1359 "ring buffer full, reading QUEUE_MAX_BYTES %"
1360 G_GUINT64_FORMAT " bytes", max_size);
1361 read_length = max_size;
1362 } else if (queue->is_eos) {
1363 /* won't get any more data so read any data we have */
1365 GST_DEBUG_OBJECT (queue,
1366 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1368 read_length = level;
1376 if (read_length == 0) {
1377 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1378 GST_DEBUG_OBJECT (queue,
1379 "update current position [%" G_GUINT64_FORMAT "-%"
1380 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1381 update_cur_pos (queue, queue->current, rpos);
1382 GST_QUEUE2_SIGNAL_DEL (queue);
1385 if (queue->use_buffering)
1386 update_buffering (queue);
1388 GST_DEBUG_OBJECT (queue, "waiting for add");
1389 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1393 /* we have the requested data so read it */
1394 read_length = remaining;
1397 /* set range reading_pos to actual reading position for this read */
1398 queue->current->reading_pos = rpos;
1400 /* configure how much and from where to read */
1401 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1403 (queue->current->rb_offset + (rpos -
1404 queue->current->offset)) % rb_size;
1405 if (file_offset + read_length > rb_size) {
1406 block_length = rb_size - file_offset;
1408 block_length = read_length;
1412 block_length = read_length;
1415 /* while we still have data to read, we loop */
1416 while (read_length > 0) {
1420 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1421 data, &read_return);
1422 if (ret != GST_FLOW_OK)
1425 file_offset += read_return;
1426 if (QUEUE_IS_USING_RING_BUFFER (queue))
1427 file_offset %= rb_size;
1429 data += read_return;
1430 read_length -= read_return;
1431 block_length = read_length;
1432 remaining -= read_return;
1434 rpos = (queue->current->reading_pos += read_return);
1435 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1437 GST_QUEUE2_SIGNAL_DEL (queue);
1438 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1441 gst_buffer_unmap (buf, &info);
1442 gst_buffer_resize (buf, 0, length);
1444 GST_BUFFER_OFFSET (buf) = offset;
1445 GST_BUFFER_OFFSET_END (buf) = offset + length;
1454 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1455 gst_buffer_unmap (buf, &info);
1456 if (*buffer == NULL)
1457 gst_buffer_unref (buf);
1458 return GST_FLOW_EOS;
1462 GST_DEBUG_OBJECT (queue, "we are flushing");
1463 gst_buffer_unmap (buf, &info);
1464 if (*buffer == NULL)
1465 gst_buffer_unref (buf);
1466 return GST_FLOW_FLUSHING;
1470 GST_DEBUG_OBJECT (queue, "we have a read error");
1471 gst_buffer_unmap (buf, &info);
1472 if (*buffer == NULL)
1473 gst_buffer_unref (buf);
1478 /* should be called with QUEUE_LOCK */
1479 static GstMiniObject *
1480 gst_queue2_read_item_from_file (GstQueue2 * queue)
1482 GstMiniObject *item;
1484 if (queue->stream_start_event != NULL) {
1485 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1486 queue->stream_start_event = NULL;
1487 } else if (queue->starting_segment != NULL) {
1488 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1489 queue->starting_segment = NULL;
1492 GstBuffer *buffer = NULL;
1493 guint64 reading_pos;
1495 reading_pos = queue->current->reading_pos;
1498 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1503 item = GST_MINI_OBJECT_CAST (buffer);
1506 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1516 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1517 * the temp filename. */
1519 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1524 if (queue->temp_file)
1525 goto already_opened;
1527 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1529 /* If temp_template was set, allocate a filename and open that filen */
1532 if (queue->temp_template == NULL)
1535 /* make copy of the template, we don't want to change this */
1536 name = g_strdup (queue->temp_template);
1537 fd = g_mkstemp (name);
1539 goto mkstemp_failed;
1541 /* open the file for update/writing */
1542 queue->temp_file = fdopen (fd, "wb+");
1543 /* error creating file */
1544 if (queue->temp_file == NULL)
1547 g_free (queue->temp_location);
1548 queue->temp_location = name;
1550 GST_QUEUE2_MUTEX_UNLOCK (queue);
1552 /* we can't emit the notify with the lock */
1553 g_object_notify (G_OBJECT (queue), "temp-location");
1555 GST_QUEUE2_MUTEX_LOCK (queue);
1557 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1564 GST_DEBUG_OBJECT (queue, "temp file was already open");
1569 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1570 (_("No Temp directory specified.")), (NULL));
1575 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1576 (_("Could not create temp file \"%s\"."), queue->temp_template),
1583 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1584 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1593 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1596 if (queue->temp_file == NULL)
1599 GST_DEBUG_OBJECT (queue, "closing temp file");
1601 fflush (queue->temp_file);
1602 fclose (queue->temp_file);
1604 if (queue->temp_remove) {
1605 if (remove (queue->temp_location) < 0) {
1606 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1607 queue->temp_location, g_strerror (errno));
1611 queue->temp_file = NULL;
1612 clean_ranges (queue);
1616 gst_queue2_flush_temp_file (GstQueue2 * queue)
1618 if (queue->temp_file == NULL)
1621 GST_DEBUG_OBJECT (queue, "flushing temp file");
1623 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1627 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1629 if (!QUEUE_IS_USING_QUEUE (queue)) {
1630 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1631 gst_queue2_flush_temp_file (queue);
1632 init_ranges (queue);
1634 while (!g_queue_is_empty (&queue->queue)) {
1635 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1637 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1638 && GST_EVENT_IS_STICKY (qitem->item)
1639 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1640 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1641 gst_pad_store_sticky_event (queue->srcpad,
1642 GST_EVENT_CAST (qitem->item));
1645 /* Then lose another reference because we are supposed to destroy that
1646 data when flushing */
1647 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1648 gst_mini_object_unref (qitem->item);
1649 g_slice_free (GstQueue2Item, qitem);
1652 queue->last_query = FALSE;
1653 g_cond_signal (&queue->query_handled);
1654 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1655 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1656 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1657 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1658 queue->sink_tainted = queue->src_tainted = TRUE;
1659 if (queue->starting_segment != NULL)
1660 gst_event_unref (queue->starting_segment);
1661 queue->starting_segment = NULL;
1662 queue->segment_event_received = FALSE;
1663 gst_event_replace (&queue->stream_start_event, NULL);
1665 /* we deleted a lot of something */
1666 GST_QUEUE2_SIGNAL_DEL (queue);
1670 gst_queue2_wait_free_space (GstQueue2 * queue)
1672 /* We make space available if we're "full" according to whatever
1673 * the user defined as "full". */
1674 if (gst_queue2_is_filled (queue)) {
1677 /* pause the timer while we wait. The fact that we are waiting does not mean
1678 * the byterate on the input pad is lower */
1679 if ((started = queue->in_timer_started))
1680 g_timer_stop (queue->in_timer);
1682 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1683 "queue is full, waiting for free space");
1685 GST_QUEUE2_MUTEX_UNLOCK (queue);
1686 g_signal_emit (queue, gst_queue2_signals[SIGNAL_OVERRUN], 0);
1687 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
1688 /* we recheck, the signal could have changed the thresholds */
1689 if (!gst_queue2_is_filled (queue))
1692 /* Wait for space to be available, we could be unlocked because of a flush. */
1693 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1695 while (gst_queue2_is_filled (queue));
1697 /* and continue if we were running before */
1699 g_timer_continue (queue->in_timer);
1706 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1712 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1715 guint8 *data, *ring_buffer;
1716 guint size, rb_size;
1717 guint64 writing_pos, new_writing_pos;
1718 GstQueue2Range *range, *prev, *next;
1719 gboolean do_seek = FALSE;
1721 if (QUEUE_IS_USING_RING_BUFFER (queue))
1722 writing_pos = queue->current->rb_writing_pos;
1724 writing_pos = queue->current->writing_pos;
1725 ring_buffer = queue->ring_buffer;
1726 rb_size = queue->ring_buffer_max_size;
1728 gst_buffer_map (buffer, &info, GST_MAP_READ);
1733 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1737 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1738 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1739 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1740 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1741 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1747 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1750 /* calculate the space in the ring buffer not used by data from
1751 * the current range */
1752 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1753 /* wait until there is some free space */
1754 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1756 /* get the amount of space we have */
1757 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1759 /* calculate if we need to split or if we can write the entire
1761 to_write = MIN (size, space);
1763 /* the writing position in the ring buffer after writing (part
1764 * or all of) the buffer */
1765 new_writing_pos = (writing_pos + to_write) % rb_size;
1768 range = queue->ranges;
1770 /* if we need to overwrite data in the ring buffer, we need to
1773 * warning: this code is complicated and includes some
1774 * simplifications - pen, paper and diagrams for the cases
1777 guint64 range_data_start, range_data_end;
1778 GstQueue2Range *range_to_destroy = NULL;
1780 range_data_start = range->rb_offset;
1781 range_data_end = range->rb_writing_pos;
1783 /* handle the special case where the range has no data in it */
1784 if (range->writing_pos == range->offset) {
1785 if (range != queue->current) {
1786 GST_DEBUG_OBJECT (queue,
1787 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1788 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1790 range_to_destroy = range;
1792 prev->next = range->next;
1797 if (range_data_end > range_data_start) {
1798 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1801 if (new_writing_pos > range_data_start) {
1802 if (new_writing_pos >= range_data_end) {
1803 GST_DEBUG_OBJECT (queue,
1804 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1805 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1807 range_to_destroy = range;
1809 prev->next = range->next;
1811 GST_DEBUG_OBJECT (queue,
1812 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1813 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1814 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1815 range->offset + new_writing_pos - range_data_start,
1817 range->offset += (new_writing_pos - range_data_start);
1818 range->rb_offset = new_writing_pos;
1822 guint64 new_wpos_virt = writing_pos + to_write;
1824 if (new_wpos_virt <= range_data_start)
1827 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1828 GST_DEBUG_OBJECT (queue,
1829 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1830 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1832 range_to_destroy = range;
1834 prev->next = range->next;
1836 GST_DEBUG_OBJECT (queue,
1837 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1838 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1839 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1840 range->offset + new_writing_pos - range_data_start,
1842 range->offset += (new_wpos_virt - range_data_start);
1843 range->rb_offset = new_writing_pos;
1848 if (!range_to_destroy)
1851 range = range->next;
1852 if (range_to_destroy) {
1853 if (range_to_destroy == queue->ranges)
1854 queue->ranges = range;
1855 g_slice_free (GstQueue2Range, range_to_destroy);
1856 range_to_destroy = NULL;
1861 new_writing_pos = writing_pos + to_write;
1864 if (QUEUE_IS_USING_TEMP_FILE (queue)
1865 && FSEEK_FILE (queue->temp_file, writing_pos))
1868 if (new_writing_pos > writing_pos) {
1869 GST_INFO_OBJECT (queue,
1870 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1871 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1872 queue->current->writing_pos, queue->current->rb_writing_pos);
1873 /* either not using ring buffer or no wrapping, just write */
1874 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1875 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1878 memcpy (ring_buffer + writing_pos, data, to_write);
1881 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1882 /* try to merge with next range */
1883 while ((next = queue->current->next)) {
1884 GST_INFO_OBJECT (queue,
1885 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1886 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1887 if (new_writing_pos < next->offset)
1890 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1893 /* remove the group */
1894 queue->current->next = next->next;
1896 /* We use the threshold to decide if we want to do a seek or simply
1897 * read the data again. If there is not so much data in the range we
1898 * prefer to avoid to seek and read it again. */
1899 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1900 /* the new range had more data than the threshold, it's worth keeping
1901 * it and doing a seek. */
1902 new_writing_pos = next->writing_pos;
1905 g_slice_free (GstQueue2Range, next);
1907 goto update_and_signal;
1911 guint block_one, block_two;
1913 block_one = rb_size - writing_pos;
1914 block_two = to_write - block_one;
1916 if (block_one > 0) {
1917 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1918 /* write data to end of ring buffer */
1919 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1920 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1923 memcpy (ring_buffer + writing_pos, data, block_one);
1927 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1930 if (block_two > 0) {
1931 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1932 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1933 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1936 memcpy (ring_buffer, data + block_one, block_two);
1942 /* update the writing positions */
1944 GST_INFO_OBJECT (queue,
1945 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1946 to_write, writing_pos, size);
1948 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1950 queue->current->writing_pos += to_write;
1951 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1953 queue->current->writing_pos = writing_pos = new_writing_pos;
1956 perform_seek_to_offset (queue, new_writing_pos);
1958 update_cur_level (queue, queue->current);
1960 /* update the buffering status */
1961 if (queue->use_buffering)
1962 update_buffering (queue);
1964 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1965 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1967 GST_QUEUE2_SIGNAL_ADD (queue);
1970 gst_buffer_unmap (buffer, &info);
1977 GST_DEBUG_OBJECT (queue, "we are flushing");
1978 gst_buffer_unmap (buffer, &info);
1979 /* FIXME - GST_FLOW_EOS ? */
1984 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1985 gst_buffer_unmap (buffer, &info);
1992 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1996 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1997 (_("Error while writing to download file.")),
1998 ("%s", g_strerror (errno)));
2001 gst_buffer_unmap (buffer, &info);
2007 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2009 GstQueue2 *queue = q;
2011 GST_TRACE_OBJECT (queue,
2012 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2013 gst_buffer_get_size (*buf));
2015 if (!gst_queue2_create_write (queue, *buf)) {
2016 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2023 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2025 guint *p_size = data;
2028 buf_size = gst_buffer_get_size (*buf);
2029 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2030 *p_size += buf_size;
2034 /* enqueue an item an update the level stats */
2036 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2037 GstQueue2ItemType item_type)
2039 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2043 buffer = GST_BUFFER_CAST (item);
2044 size = gst_buffer_get_size (buffer);
2046 /* add buffer to the statistics */
2047 if (QUEUE_IS_USING_QUEUE (queue)) {
2048 queue->cur_level.buffers++;
2049 queue->cur_level.bytes += size;
2051 queue->bytes_in += size;
2053 /* apply new buffer to segment stats */
2054 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
2055 /* update the byterate stats */
2056 update_in_rates (queue);
2058 if (!QUEUE_IS_USING_QUEUE (queue)) {
2059 /* FIXME - check return value? */
2060 gst_queue2_create_write (queue, buffer);
2062 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2063 GstBufferList *buffer_list;
2066 buffer_list = GST_BUFFER_LIST_CAST (item);
2068 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2069 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2071 /* add buffer to the statistics */
2072 if (QUEUE_IS_USING_QUEUE (queue)) {
2073 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2074 queue->cur_level.bytes += size;
2076 queue->bytes_in += size;
2078 /* apply new buffer to segment stats */
2079 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2081 /* update the byterate stats */
2082 update_in_rates (queue);
2084 if (!QUEUE_IS_USING_QUEUE (queue)) {
2085 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2087 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2090 event = GST_EVENT_CAST (item);
2092 switch (GST_EVENT_TYPE (event)) {
2094 /* Zero the thresholds, this makes sure the queue is completely
2095 * filled and we can read all data from the queue. */
2096 GST_DEBUG_OBJECT (queue, "we have EOS");
2097 queue->is_eos = TRUE;
2099 case GST_EVENT_SEGMENT:
2100 apply_segment (queue, event, &queue->sink_segment, TRUE);
2101 /* This is our first new segment, we hold it
2102 * as we can't save it on the temp file */
2103 if (!QUEUE_IS_USING_QUEUE (queue)) {
2104 if (queue->segment_event_received)
2105 goto unexpected_event;
2107 queue->segment_event_received = TRUE;
2108 if (queue->starting_segment != NULL)
2109 gst_event_unref (queue->starting_segment);
2110 queue->starting_segment = event;
2113 /* a new segment allows us to accept more buffers if we got EOS
2114 * from downstream */
2115 queue->unexpected = FALSE;
2118 apply_gap (queue, event, &queue->sink_segment, TRUE);
2120 case GST_EVENT_STREAM_START:
2121 if (!QUEUE_IS_USING_QUEUE (queue)) {
2122 gst_event_replace (&queue->stream_start_event, event);
2123 gst_event_unref (event);
2127 case GST_EVENT_CAPS:{
2130 gst_event_parse_caps (event, &caps);
2131 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2133 if (!QUEUE_IS_USING_QUEUE (queue)) {
2134 GST_LOG ("Dropping caps event, not using queue");
2135 gst_event_unref (event);
2141 if (!QUEUE_IS_USING_QUEUE (queue))
2142 goto unexpected_event;
2145 } else if (GST_IS_QUERY (item)) {
2146 /* Can't happen as we check that in the caller */
2147 if (!QUEUE_IS_USING_QUEUE (queue))
2148 g_assert_not_reached ();
2150 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2151 item, GST_OBJECT_NAME (queue));
2152 /* we can't really unref since we don't know what it is */
2157 /* update the buffering status */
2158 if (queue->use_buffering)
2159 update_buffering (queue);
2161 if (QUEUE_IS_USING_QUEUE (queue)) {
2162 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2163 qitem->type = item_type;
2165 g_queue_push_tail (&queue->queue, qitem);
2167 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2170 GST_QUEUE2_SIGNAL_ADD (queue);
2179 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2180 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2181 GST_OBJECT_NAME (queue));
2182 gst_event_unref (GST_EVENT_CAST (item));
2187 /* dequeue an item from the queue and update level stats */
2188 static GstMiniObject *
2189 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2191 GstMiniObject *item;
2193 if (!QUEUE_IS_USING_QUEUE (queue)) {
2194 item = gst_queue2_read_item_from_file (queue);
2196 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2202 g_slice_free (GstQueue2Item, qitem);
2208 if (GST_IS_BUFFER (item)) {
2212 buffer = GST_BUFFER_CAST (item);
2213 size = gst_buffer_get_size (buffer);
2214 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2216 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2217 "retrieved buffer %p from queue", buffer);
2219 if (QUEUE_IS_USING_QUEUE (queue)) {
2220 queue->cur_level.buffers--;
2221 queue->cur_level.bytes -= size;
2223 queue->bytes_out += size;
2225 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2226 /* update the byterate stats */
2227 update_out_rates (queue);
2228 /* update the buffering */
2229 if (queue->use_buffering)
2230 update_buffering (queue);
2232 } else if (GST_IS_EVENT (item)) {
2233 GstEvent *event = GST_EVENT_CAST (item);
2235 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2237 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2238 "retrieved event %p from queue", event);
2240 switch (GST_EVENT_TYPE (event)) {
2242 /* queue is empty now that we dequeued the EOS */
2243 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2245 case GST_EVENT_SEGMENT:
2246 apply_segment (queue, event, &queue->src_segment, FALSE);
2249 apply_gap (queue, event, &queue->src_segment, FALSE);
2254 } else if (GST_IS_BUFFER_LIST (item)) {
2255 GstBufferList *buffer_list;
2258 buffer_list = GST_BUFFER_LIST_CAST (item);
2259 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2260 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2262 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2263 "retrieved buffer list %p from queue", buffer_list);
2265 if (QUEUE_IS_USING_QUEUE (queue)) {
2266 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2267 queue->cur_level.bytes -= size;
2269 queue->bytes_out += size;
2271 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2272 /* update the byterate stats */
2273 update_out_rates (queue);
2274 /* update the buffering */
2275 if (queue->use_buffering)
2276 update_buffering (queue);
2277 } else if (GST_IS_QUERY (item)) {
2278 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2279 "retrieved query %p from queue", item);
2280 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2283 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2284 item, GST_OBJECT_NAME (queue));
2286 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2288 GST_QUEUE2_SIGNAL_DEL (queue);
2295 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2301 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2304 gboolean ret = TRUE;
2307 queue = GST_QUEUE2 (parent);
2309 switch (GST_EVENT_TYPE (event)) {
2310 case GST_EVENT_FLUSH_START:
2312 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2313 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2315 ret = gst_pad_push_event (queue->srcpad, event);
2317 /* now unblock the chain function */
2318 GST_QUEUE2_MUTEX_LOCK (queue);
2319 queue->srcresult = GST_FLOW_FLUSHING;
2320 queue->sinkresult = GST_FLOW_FLUSHING;
2321 /* unblock the loop and chain functions */
2322 GST_QUEUE2_SIGNAL_ADD (queue);
2323 GST_QUEUE2_SIGNAL_DEL (queue);
2324 queue->last_query = FALSE;
2325 g_cond_signal (&queue->query_handled);
2326 GST_QUEUE2_MUTEX_UNLOCK (queue);
2328 /* make sure it pauses, this should happen since we sent
2329 * flush_start downstream. */
2330 gst_pad_pause_task (queue->srcpad);
2331 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2333 GST_QUEUE2_MUTEX_LOCK (queue);
2334 /* flush the sink pad */
2335 queue->sinkresult = GST_FLOW_FLUSHING;
2336 GST_QUEUE2_SIGNAL_DEL (queue);
2337 queue->last_query = FALSE;
2338 g_cond_signal (&queue->query_handled);
2339 GST_QUEUE2_MUTEX_UNLOCK (queue);
2341 gst_event_unref (event);
2345 case GST_EVENT_FLUSH_STOP:
2347 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2349 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2351 ret = gst_pad_push_event (queue->srcpad, event);
2353 GST_QUEUE2_MUTEX_LOCK (queue);
2354 gst_queue2_locked_flush (queue, FALSE, TRUE);
2355 queue->srcresult = GST_FLOW_OK;
2356 queue->sinkresult = GST_FLOW_OK;
2357 queue->is_eos = FALSE;
2358 queue->unexpected = FALSE;
2359 queue->seeking = FALSE;
2360 /* reset rate counters */
2361 reset_rate_timer (queue);
2362 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2363 queue->srcpad, NULL);
2364 GST_QUEUE2_MUTEX_UNLOCK (queue);
2366 GST_QUEUE2_MUTEX_LOCK (queue);
2367 queue->segment_event_received = FALSE;
2368 queue->is_eos = FALSE;
2369 queue->unexpected = FALSE;
2370 queue->sinkresult = GST_FLOW_OK;
2371 queue->seeking = FALSE;
2372 GST_QUEUE2_MUTEX_UNLOCK (queue);
2374 gst_event_unref (event);
2379 if (GST_EVENT_IS_SERIALIZED (event)) {
2380 /* serialized events go in the queue */
2381 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2382 if (queue->srcresult != GST_FLOW_OK) {
2383 /* Errors in sticky event pushing are no problem and ignored here
2384 * as they will cause more meaningful errors during data flow.
2385 * For EOS events, that are not followed by data flow, we still
2386 * return FALSE here though and report an error.
2388 if (!GST_EVENT_IS_STICKY (event)) {
2389 goto out_flow_error;
2390 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2391 if (queue->srcresult == GST_FLOW_NOT_LINKED
2392 || queue->srcresult < GST_FLOW_EOS) {
2393 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2394 (_("Internal data flow error.")),
2395 ("streaming task paused, reason %s (%d)",
2396 gst_flow_get_name (queue->srcresult), queue->srcresult));
2398 goto out_flow_error;
2401 /* refuse more events on EOS */
2404 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2405 GST_QUEUE2_MUTEX_UNLOCK (queue);
2406 gst_queue2_post_buffering (queue);
2408 /* non-serialized events are passed upstream. */
2409 ret = gst_pad_push_event (queue->srcpad, event);
2418 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2419 GST_QUEUE2_MUTEX_UNLOCK (queue);
2420 gst_event_unref (event);
2425 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2426 GST_QUEUE2_MUTEX_UNLOCK (queue);
2427 gst_event_unref (event);
2432 GST_LOG_OBJECT (queue,
2433 "refusing event, we have a downstream flow error: %s",
2434 gst_flow_get_name (queue->srcresult));
2435 GST_QUEUE2_MUTEX_UNLOCK (queue);
2436 gst_event_unref (event);
2442 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2448 queue = GST_QUEUE2 (parent);
2450 switch (GST_QUERY_TYPE (query)) {
2452 if (GST_QUERY_IS_SERIALIZED (query)) {
2453 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2454 /* serialized events go in the queue. We need to be certain that we
2455 * don't cause deadlocks waiting for the query return value. We check if
2456 * the queue is empty (nothing is blocking downstream and the query can
2457 * be pushed for sure) or we are not buffering. If we are buffering,
2458 * the pipeline waits to unblock downstream until our queue fills up
2459 * completely, which can not happen if we block on the query..
2460 * Therefore we only potentially block when we are not buffering. */
2461 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2462 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2463 || !queue->use_buffering)) {
2464 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2465 gst_queue2_locked_enqueue (queue, query,
2466 GST_QUEUE2_ITEM_TYPE_QUERY);
2468 STATUS (queue, queue->sinkpad, "wait for QUERY");
2469 g_cond_wait (&queue->query_handled, &queue->qlock);
2470 if (queue->sinkresult != GST_FLOW_OK)
2472 res = queue->last_query;
2474 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2478 GST_DEBUG_OBJECT (queue,
2479 "refusing query, we are not using the queue");
2482 GST_QUEUE2_MUTEX_UNLOCK (queue);
2483 gst_queue2_post_buffering (queue);
2485 res = gst_pad_query_default (pad, parent, query);
2494 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2495 GST_QUEUE2_MUTEX_UNLOCK (queue);
2501 gst_queue2_is_empty (GstQueue2 * queue)
2503 /* never empty on EOS */
2507 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2508 return queue->current->writing_pos <= queue->current->max_reading_pos;
2510 if (queue->queue.length == 0)
2518 gst_queue2_is_filled (GstQueue2 * queue)
2522 /* always filled on EOS */
2526 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2527 (queue->cur_level.format) >= ((alt_max) ? \
2528 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2530 /* if using a ring buffer we're filled if all ring buffer space is used
2531 * _by the current range_ */
2532 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2533 guint64 rb_size = queue->ring_buffer_max_size;
2534 GST_DEBUG_OBJECT (queue,
2535 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2536 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2537 return CHECK_FILLED (bytes, rb_size);
2540 /* if using file, we're never filled if we don't have EOS */
2541 if (QUEUE_IS_USING_TEMP_FILE (queue))
2544 /* we are never filled when we have no buffers at all */
2545 if (queue->cur_level.buffers == 0)
2548 /* we are filled if one of the current levels exceeds the max */
2549 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2550 || CHECK_FILLED (time, 0);
2552 /* if we need to, use the rate estimate to check against the max time we are
2553 * allowed to queue */
2554 if (queue->use_rate_estimate)
2555 res |= CHECK_FILLED (rate_time, 0);
2561 static GstFlowReturn
2562 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2563 GstMiniObject * item, GstQueue2ItemType item_type)
2565 /* we have to lock the queue since we span threads */
2566 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2567 /* when we received EOS, we refuse more data */
2570 /* when we received unexpected from downstream, refuse more buffers */
2571 if (queue->unexpected)
2572 goto out_unexpected;
2574 /* while we didn't receive the newsegment, we're seeking and we skip data */
2578 if (!gst_queue2_wait_free_space (queue))
2581 /* put buffer in queue now */
2582 gst_queue2_locked_enqueue (queue, item, item_type);
2583 GST_QUEUE2_MUTEX_UNLOCK (queue);
2584 gst_queue2_post_buffering (queue);
2588 /* special conditions */
2591 GstFlowReturn ret = queue->sinkresult;
2593 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2594 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2595 GST_QUEUE2_MUTEX_UNLOCK (queue);
2596 gst_mini_object_unref (item);
2602 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2603 GST_QUEUE2_MUTEX_UNLOCK (queue);
2604 gst_mini_object_unref (item);
2606 return GST_FLOW_EOS;
2610 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2611 GST_QUEUE2_MUTEX_UNLOCK (queue);
2612 gst_mini_object_unref (item);
2618 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2619 GST_QUEUE2_MUTEX_UNLOCK (queue);
2620 gst_mini_object_unref (item);
2622 return GST_FLOW_EOS;
2626 static GstFlowReturn
2627 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2631 queue = GST_QUEUE2 (parent);
2633 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2634 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2635 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2636 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2637 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2639 return gst_queue2_chain_buffer_or_buffer_list (queue,
2640 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2643 static GstFlowReturn
2644 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2645 GstBufferList * buffer_list)
2649 queue = GST_QUEUE2 (parent);
2651 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2652 "received buffer list %p", buffer_list);
2654 return gst_queue2_chain_buffer_or_buffer_list (queue,
2655 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2658 static GstMiniObject *
2659 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2661 GstMiniObject *data;
2663 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2665 /* stop pushing buffers, we dequeue all items until we see an item that we
2666 * can push again, which is EOS or SEGMENT. If there is nothing in the
2667 * queue we can push, we set a flag to make the sinkpad refuse more
2668 * buffers with an EOS return value until we receive something
2669 * pushable again or we get flushed. */
2670 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2671 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2672 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2673 "dropping EOS buffer %p", data);
2674 gst_buffer_unref (GST_BUFFER_CAST (data));
2675 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2676 GstEvent *event = GST_EVENT_CAST (data);
2677 GstEventType type = GST_EVENT_TYPE (event);
2679 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2680 /* we found a pushable item in the queue, push it out */
2681 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2682 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2685 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2686 "dropping EOS event %p", event);
2687 gst_event_unref (event);
2688 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2689 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2690 "dropping EOS buffer list %p", data);
2691 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2692 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2693 queue->last_query = FALSE;
2694 g_cond_signal (&queue->query_handled);
2695 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2698 /* no more items in the queue. Set the unexpected flag so that upstream
2699 * make us refuse any more buffers on the sinkpad. Since we will still
2700 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2701 * task function does not shut down. */
2702 queue->unexpected = TRUE;
2706 /* dequeue an item from the queue an push it downstream. This functions returns
2707 * the result of the push. */
2708 static GstFlowReturn
2709 gst_queue2_push_one (GstQueue2 * queue)
2711 GstFlowReturn result = queue->srcresult;
2712 GstMiniObject *data;
2713 GstQueue2ItemType item_type;
2715 data = gst_queue2_locked_dequeue (queue, &item_type);
2720 STATUS (queue, queue->srcpad, "We have something dequeud");
2721 g_atomic_int_set (&queue->downstream_may_block,
2722 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2723 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2724 GST_QUEUE2_MUTEX_UNLOCK (queue);
2725 gst_queue2_post_buffering (queue);
2727 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2730 buffer = GST_BUFFER_CAST (data);
2732 result = gst_pad_push (queue->srcpad, buffer);
2733 g_atomic_int_set (&queue->downstream_may_block, 0);
2735 /* need to check for srcresult here as well */
2736 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2737 if (result == GST_FLOW_EOS) {
2738 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2741 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2742 * to the caller so that the task function does not shut down */
2743 result = GST_FLOW_OK;
2745 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2746 GstEvent *event = GST_EVENT_CAST (data);
2747 GstEventType type = GST_EVENT_TYPE (event);
2749 gst_pad_push_event (queue->srcpad, event);
2751 /* if we're EOS, return EOS so that the task pauses. */
2752 if (type == GST_EVENT_EOS) {
2753 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2754 "pushed EOS event %p, return EOS", event);
2755 result = GST_FLOW_EOS;
2758 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2759 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2760 GstBufferList *buffer_list;
2762 buffer_list = GST_BUFFER_LIST_CAST (data);
2764 result = gst_pad_push_list (queue->srcpad, buffer_list);
2765 g_atomic_int_set (&queue->downstream_may_block, 0);
2767 /* need to check for srcresult here as well */
2768 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2769 if (result == GST_FLOW_EOS) {
2770 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2773 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2774 * to the caller so that the task function does not shut down */
2775 result = GST_FLOW_OK;
2777 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2778 GstQuery *query = GST_QUERY_CAST (data);
2780 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2781 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2782 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2783 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2784 "did query %p, return %d", query, queue->last_query);
2785 g_cond_signal (&queue->query_handled);
2786 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2787 result = GST_FLOW_OK;
2794 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2795 "exit because we have no item in the queue");
2796 return GST_FLOW_ERROR;
2800 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2801 return GST_FLOW_FLUSHING;
2805 /* called repeatedly with @pad as the source pad. This function should push out
2806 * data to the peer element. */
2808 gst_queue2_loop (GstPad * pad)
2813 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2815 /* have to lock for thread-safety */
2816 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2818 if (gst_queue2_is_empty (queue)) {
2821 /* pause the timer while we wait. The fact that we are waiting does not mean
2822 * the byterate on the output pad is lower */
2823 if ((started = queue->out_timer_started))
2824 g_timer_stop (queue->out_timer);
2826 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2827 "queue is empty, waiting for new data");
2829 /* Wait for data to be available, we could be unlocked because of a flush. */
2830 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2832 while (gst_queue2_is_empty (queue));
2834 /* and continue if we were running before */
2836 g_timer_continue (queue->out_timer);
2838 ret = gst_queue2_push_one (queue);
2839 queue->srcresult = ret;
2840 queue->sinkresult = ret;
2841 if (ret != GST_FLOW_OK)
2844 GST_QUEUE2_MUTEX_UNLOCK (queue);
2845 gst_queue2_post_buffering (queue);
2852 gboolean eos = queue->is_eos;
2853 GstFlowReturn ret = queue->srcresult;
2855 gst_pad_pause_task (queue->srcpad);
2856 if (ret == GST_FLOW_FLUSHING) {
2857 gst_queue2_locked_flush (queue, FALSE, FALSE);
2859 GST_QUEUE2_SIGNAL_DEL (queue);
2860 queue->last_query = FALSE;
2861 g_cond_signal (&queue->query_handled);
2863 GST_QUEUE2_MUTEX_UNLOCK (queue);
2864 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2865 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2866 /* let app know about us giving up if upstream is not expected to do so */
2867 /* EOS is already taken care of elsewhere */
2868 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2869 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2870 (_("Internal data flow error.")),
2871 ("streaming task paused, reason %s (%d)",
2872 gst_flow_get_name (ret), ret));
2873 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2880 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2882 gboolean res = TRUE;
2883 GstQueue2 *queue = GST_QUEUE2 (parent);
2885 #ifndef GST_DISABLE_GST_DEBUG
2886 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2887 event, GST_EVENT_TYPE_NAME (event));
2890 switch (GST_EVENT_TYPE (event)) {
2891 case GST_EVENT_FLUSH_START:
2892 if (QUEUE_IS_USING_QUEUE (queue)) {
2893 /* just forward upstream */
2894 res = gst_pad_push_event (queue->sinkpad, event);
2896 /* now unblock the getrange function */
2897 GST_QUEUE2_MUTEX_LOCK (queue);
2898 GST_DEBUG_OBJECT (queue, "flushing");
2899 queue->srcresult = GST_FLOW_FLUSHING;
2900 GST_QUEUE2_SIGNAL_ADD (queue);
2901 GST_QUEUE2_MUTEX_UNLOCK (queue);
2903 /* when using a temp file, we eat the event */
2905 gst_event_unref (event);
2908 case GST_EVENT_FLUSH_STOP:
2909 if (QUEUE_IS_USING_QUEUE (queue)) {
2910 /* just forward upstream */
2911 res = gst_pad_push_event (queue->sinkpad, event);
2913 /* now unblock the getrange function */
2914 GST_QUEUE2_MUTEX_LOCK (queue);
2915 queue->srcresult = GST_FLOW_OK;
2916 GST_QUEUE2_MUTEX_UNLOCK (queue);
2918 /* when using a temp file, we eat the event */
2920 gst_event_unref (event);
2923 case GST_EVENT_RECONFIGURE:
2924 GST_QUEUE2_MUTEX_LOCK (queue);
2925 /* assume downstream is linked now and try to push again */
2926 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2927 queue->srcresult = GST_FLOW_OK;
2928 queue->sinkresult = GST_FLOW_OK;
2929 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2930 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2934 GST_QUEUE2_MUTEX_UNLOCK (queue);
2936 res = gst_pad_push_event (queue->sinkpad, event);
2939 res = gst_pad_push_event (queue->sinkpad, event);
2947 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2951 queue = GST_QUEUE2 (parent);
2953 switch (GST_QUERY_TYPE (query)) {
2954 case GST_QUERY_POSITION:
2959 if (!gst_pad_peer_query (queue->sinkpad, query))
2962 /* get peer position */
2963 gst_query_parse_position (query, &format, &peer_pos);
2965 /* FIXME: this code assumes that there's no discont in the queue */
2967 case GST_FORMAT_BYTES:
2968 peer_pos -= queue->cur_level.bytes;
2970 case GST_FORMAT_TIME:
2971 peer_pos -= queue->cur_level.time;
2974 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2975 "know how to adjust value", gst_format_get_name (format));
2978 /* set updated position */
2979 gst_query_set_position (query, format, peer_pos);
2982 case GST_QUERY_DURATION:
2984 GST_DEBUG_OBJECT (queue, "doing peer query");
2986 if (!gst_pad_peer_query (queue->sinkpad, query))
2989 GST_DEBUG_OBJECT (queue, "peer query success");
2992 case GST_QUERY_BUFFERING:
2995 gboolean is_buffering;
2996 GstBufferingMode mode;
2997 gint avg_in, avg_out;
2998 gint64 buffering_left;
3000 GST_DEBUG_OBJECT (queue, "query buffering");
3002 get_buffering_percent (queue, &is_buffering, &percent);
3003 gst_query_set_buffering_percent (query, is_buffering, percent);
3005 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3007 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3010 if (!QUEUE_IS_USING_QUEUE (queue)) {
3011 /* add ranges for download and ringbuffer buffering */
3013 gint64 start, stop, range_start, range_stop;
3014 guint64 writing_pos;
3015 gint64 estimated_total;
3017 gboolean peer_res, is_eos;
3018 GstQueue2Range *queued_ranges;
3020 /* we need a current download region */
3021 if (queue->current == NULL)
3024 writing_pos = queue->current->writing_pos;
3025 is_eos = queue->is_eos;
3028 /* we're EOS, we know the duration in bytes now */
3030 duration = writing_pos;
3032 /* get duration of upstream in bytes */
3033 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3034 GST_FORMAT_BYTES, &duration);
3037 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3038 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3040 /* calculate remaining and total download time */
3041 if (peer_res && avg_in > 0.0)
3042 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3044 estimated_total = -1;
3046 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3049 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3052 case GST_FORMAT_PERCENT:
3053 /* we need duration */
3058 /* get our available data relative to the duration */
3061 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3066 case GST_FORMAT_BYTES:
3076 /* fill out the buffered ranges */
3077 for (queued_ranges = queue->ranges; queued_ranges;
3078 queued_ranges = queued_ranges->next) {
3080 case GST_FORMAT_PERCENT:
3081 if (duration == -1) {
3087 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3088 queued_ranges->offset, duration);
3090 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3091 queued_ranges->writing_pos, duration);
3093 case GST_FORMAT_BYTES:
3094 range_start = queued_ranges->offset;
3095 range_stop = queued_ranges->writing_pos;
3102 if (range_start == range_stop)
3104 GST_DEBUG_OBJECT (queue,
3105 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3106 G_GINT64_FORMAT, range_start, range_stop);
3107 gst_query_add_buffering_range (query, range_start, range_stop);
3110 gst_query_set_buffering_range (query, format, start, stop,
3115 case GST_QUERY_SCHEDULING:
3118 GstSchedulingFlags flags = 0;
3120 if (!gst_pad_peer_query (queue->sinkpad, query))
3123 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3125 /* we can operate in pull mode when we are using a tempfile */
3126 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3129 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3130 gst_query_set_scheduling (query, flags, 0, -1, 0);
3132 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3133 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3137 /* peer handled other queries */
3138 if (!gst_pad_query_default (pad, parent, query))
3148 GST_DEBUG_OBJECT (queue, "failed peer query");
3154 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3156 GstQueue2 *queue = GST_QUEUE2 (element);
3158 /* simply forward to the srcpad query function */
3159 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3164 gst_queue2_update_upstream_size (GstQueue2 * queue)
3166 gint64 upstream_size = -1;
3168 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3170 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3172 /* upstream_size can be negative but queue->upstream_size is unsigned.
3173 * Prevent setting negative values to it (the query can return -1) */
3174 if (upstream_size >= 0)
3175 queue->upstream_size = upstream_size;
3177 queue->upstream_size = 0;
3181 static GstFlowReturn
3182 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3183 guint length, GstBuffer ** buffer)
3188 queue = GST_QUEUE2_CAST (parent);
3190 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3191 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3192 offset = (offset == -1) ? queue->current->reading_pos : offset;
3194 GST_DEBUG_OBJECT (queue,
3195 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3197 /* catch any reads beyond the size of the file here to make sure queue2
3198 * doesn't send seek events beyond the size of the file upstream, since
3199 * that would confuse elements such as souphttpsrc and/or http servers.
3200 * Demuxers often just loop until EOS at the end of the file to figure out
3201 * when they've read all the end-headers or index chunks. */
3202 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3203 gst_queue2_update_upstream_size (queue);
3204 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3205 goto out_unexpected;
3208 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3209 gst_queue2_update_upstream_size (queue);
3210 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3211 length = queue->upstream_size - offset;
3212 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3216 /* FIXME - function will block when the range is not yet available */
3217 ret = gst_queue2_create_read (queue, offset, length, buffer);
3218 GST_QUEUE2_MUTEX_UNLOCK (queue);
3219 gst_queue2_post_buffering (queue);
3226 ret = queue->srcresult;
3228 GST_DEBUG_OBJECT (queue, "we are flushing");
3229 GST_QUEUE2_MUTEX_UNLOCK (queue);
3234 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3235 GST_QUEUE2_MUTEX_UNLOCK (queue);
3236 return GST_FLOW_EOS;
3240 /* sink currently only operates in push mode */
3242 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3243 GstPadMode mode, gboolean active)
3248 queue = GST_QUEUE2 (parent);
3251 case GST_PAD_MODE_PUSH:
3253 GST_QUEUE2_MUTEX_LOCK (queue);
3254 GST_DEBUG_OBJECT (queue, "activating push mode");
3255 queue->srcresult = GST_FLOW_OK;
3256 queue->sinkresult = GST_FLOW_OK;
3257 queue->is_eos = FALSE;
3258 queue->unexpected = FALSE;
3259 reset_rate_timer (queue);
3260 GST_QUEUE2_MUTEX_UNLOCK (queue);
3262 /* unblock chain function */
3263 GST_QUEUE2_MUTEX_LOCK (queue);
3264 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3265 queue->srcresult = GST_FLOW_FLUSHING;
3266 queue->sinkresult = GST_FLOW_FLUSHING;
3267 GST_QUEUE2_SIGNAL_DEL (queue);
3268 /* Unblock query handler */
3269 queue->last_query = FALSE;
3270 g_cond_signal (&queue->query_handled);
3271 GST_QUEUE2_MUTEX_UNLOCK (queue);
3273 /* wait until it is unblocked and clean up */
3274 GST_PAD_STREAM_LOCK (pad);
3275 GST_QUEUE2_MUTEX_LOCK (queue);
3276 gst_queue2_locked_flush (queue, TRUE, FALSE);
3277 GST_QUEUE2_MUTEX_UNLOCK (queue);
3278 GST_PAD_STREAM_UNLOCK (pad);
3289 /* src operating in push mode, we start a task on the source pad that pushes out
3290 * buffers from the queue */
3292 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3294 gboolean result = FALSE;
3297 queue = GST_QUEUE2 (parent);
3300 GST_QUEUE2_MUTEX_LOCK (queue);
3301 GST_DEBUG_OBJECT (queue, "activating push mode");
3302 queue->srcresult = GST_FLOW_OK;
3303 queue->sinkresult = GST_FLOW_OK;
3304 queue->is_eos = FALSE;
3305 queue->unexpected = FALSE;
3307 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3308 GST_QUEUE2_MUTEX_UNLOCK (queue);
3310 /* unblock loop function */
3311 GST_QUEUE2_MUTEX_LOCK (queue);
3312 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3313 queue->srcresult = GST_FLOW_FLUSHING;
3314 queue->sinkresult = GST_FLOW_FLUSHING;
3315 /* the item add signal will unblock */
3316 GST_QUEUE2_SIGNAL_ADD (queue);
3317 GST_QUEUE2_MUTEX_UNLOCK (queue);
3319 /* step 2, make sure streaming finishes */
3320 result = gst_pad_stop_task (pad);
3326 /* pull mode, downstream will call our getrange function */
3328 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3333 queue = GST_QUEUE2 (parent);
3336 GST_QUEUE2_MUTEX_LOCK (queue);
3337 if (!QUEUE_IS_USING_QUEUE (queue)) {
3338 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3339 /* open the temp file now */
3340 result = gst_queue2_open_temp_location_file (queue);
3341 } else if (!queue->ring_buffer) {
3342 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3343 result = ! !queue->ring_buffer;
3348 GST_DEBUG_OBJECT (queue, "activating pull mode");
3349 init_ranges (queue);
3350 queue->srcresult = GST_FLOW_OK;
3351 queue->sinkresult = GST_FLOW_OK;
3352 queue->is_eos = FALSE;
3353 queue->unexpected = FALSE;
3354 queue->upstream_size = 0;
3356 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3357 /* this is not allowed, we cannot operate in pull mode without a temp
3359 queue->srcresult = GST_FLOW_FLUSHING;
3360 queue->sinkresult = GST_FLOW_FLUSHING;
3363 GST_QUEUE2_MUTEX_UNLOCK (queue);
3365 GST_QUEUE2_MUTEX_LOCK (queue);
3366 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3367 queue->srcresult = GST_FLOW_FLUSHING;
3368 queue->sinkresult = GST_FLOW_FLUSHING;
3369 /* this will unlock getrange */
3370 GST_QUEUE2_SIGNAL_ADD (queue);
3372 GST_QUEUE2_MUTEX_UNLOCK (queue);
3379 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3385 case GST_PAD_MODE_PULL:
3386 res = gst_queue2_src_activate_pull (pad, parent, active);
3388 case GST_PAD_MODE_PUSH:
3389 res = gst_queue2_src_activate_push (pad, parent, active);
3392 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3399 static GstStateChangeReturn
3400 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3403 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3405 queue = GST_QUEUE2 (element);
3407 switch (transition) {
3408 case GST_STATE_CHANGE_NULL_TO_READY:
3410 case GST_STATE_CHANGE_READY_TO_PAUSED:
3411 GST_QUEUE2_MUTEX_LOCK (queue);
3412 if (!QUEUE_IS_USING_QUEUE (queue)) {
3413 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3414 if (!gst_queue2_open_temp_location_file (queue))
3415 ret = GST_STATE_CHANGE_FAILURE;
3417 if (queue->ring_buffer) {
3418 g_free (queue->ring_buffer);
3419 queue->ring_buffer = NULL;
3421 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3422 ret = GST_STATE_CHANGE_FAILURE;
3424 init_ranges (queue);
3426 queue->segment_event_received = FALSE;
3427 queue->starting_segment = NULL;
3428 gst_event_replace (&queue->stream_start_event, NULL);
3429 GST_QUEUE2_MUTEX_UNLOCK (queue);
3431 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3437 if (ret == GST_STATE_CHANGE_FAILURE)
3440 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3442 if (ret == GST_STATE_CHANGE_FAILURE)
3445 switch (transition) {
3446 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3448 case GST_STATE_CHANGE_PAUSED_TO_READY:
3449 GST_QUEUE2_MUTEX_LOCK (queue);
3450 if (!QUEUE_IS_USING_QUEUE (queue)) {
3451 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3452 gst_queue2_close_temp_location_file (queue);
3453 } else if (queue->ring_buffer) {
3454 g_free (queue->ring_buffer);
3455 queue->ring_buffer = NULL;
3457 clean_ranges (queue);
3459 if (queue->starting_segment != NULL) {
3460 gst_event_unref (queue->starting_segment);
3461 queue->starting_segment = NULL;
3463 gst_event_replace (&queue->stream_start_event, NULL);
3464 GST_QUEUE2_MUTEX_UNLOCK (queue);
3466 case GST_STATE_CHANGE_READY_TO_NULL:
3475 /* changing the capacity of the queue must wake up
3476 * the _chain function, it might have more room now
3477 * to store the buffer/event in the queue */
3478 #define QUEUE_CAPACITY_CHANGE(q) \
3479 GST_QUEUE2_SIGNAL_DEL (queue); \
3480 if (queue->use_buffering) \
3481 update_buffering (queue);
3483 /* Changing the minimum required fill level must
3484 * wake up the _loop function as it might now
3485 * be able to preceed.
3487 #define QUEUE_THRESHOLD_CHANGE(q)\
3488 GST_QUEUE2_SIGNAL_ADD (queue);
3491 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3495 /* the element must be stopped in order to do this */
3496 GST_OBJECT_LOCK (queue);
3497 state = GST_STATE (queue);
3498 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3500 GST_OBJECT_UNLOCK (queue);
3502 /* set new location */
3503 g_free (queue->temp_template);
3504 queue->temp_template = g_strdup (template);
3511 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3512 GST_OBJECT_UNLOCK (queue);
3517 gst_queue2_set_property (GObject * object,
3518 guint prop_id, const GValue * value, GParamSpec * pspec)
3520 GstQueue2 *queue = GST_QUEUE2 (object);
3522 /* someone could change levels here, and since this
3523 * affects the get/put funcs, we need to lock for safety. */
3524 GST_QUEUE2_MUTEX_LOCK (queue);
3527 case PROP_MAX_SIZE_BYTES:
3528 queue->max_level.bytes = g_value_get_uint (value);
3529 QUEUE_CAPACITY_CHANGE (queue);
3531 case PROP_MAX_SIZE_BUFFERS:
3532 queue->max_level.buffers = g_value_get_uint (value);
3533 QUEUE_CAPACITY_CHANGE (queue);
3535 case PROP_MAX_SIZE_TIME:
3536 queue->max_level.time = g_value_get_uint64 (value);
3537 /* set rate_time to the same value. We use an extra field in the level
3538 * structure so that we can easily access and compare it */
3539 queue->max_level.rate_time = queue->max_level.time;
3540 QUEUE_CAPACITY_CHANGE (queue);
3542 case PROP_USE_BUFFERING:
3543 queue->use_buffering = g_value_get_boolean (value);
3544 if (!queue->use_buffering && queue->is_buffering) {
3545 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3546 "posting 100%% message");
3547 SET_PERCENT (queue, 100);
3548 queue->is_buffering = FALSE;
3551 if (queue->use_buffering) {
3552 queue->is_buffering = TRUE;
3553 update_buffering (queue);
3556 case PROP_USE_RATE_ESTIMATE:
3557 queue->use_rate_estimate = g_value_get_boolean (value);
3559 case PROP_LOW_PERCENT:
3560 queue->low_percent = g_value_get_int (value);
3562 case PROP_HIGH_PERCENT:
3563 queue->high_percent = g_value_get_int (value);
3565 case PROP_TEMP_TEMPLATE:
3566 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3568 case PROP_TEMP_REMOVE:
3569 queue->temp_remove = g_value_get_boolean (value);
3571 case PROP_RING_BUFFER_MAX_SIZE:
3572 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3575 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3579 GST_QUEUE2_MUTEX_UNLOCK (queue);
3580 gst_queue2_post_buffering (queue);
3584 gst_queue2_get_property (GObject * object,
3585 guint prop_id, GValue * value, GParamSpec * pspec)
3587 GstQueue2 *queue = GST_QUEUE2 (object);
3589 GST_QUEUE2_MUTEX_LOCK (queue);
3592 case PROP_CUR_LEVEL_BYTES:
3593 g_value_set_uint (value, queue->cur_level.bytes);
3595 case PROP_CUR_LEVEL_BUFFERS:
3596 g_value_set_uint (value, queue->cur_level.buffers);
3598 case PROP_CUR_LEVEL_TIME:
3599 g_value_set_uint64 (value, queue->cur_level.time);
3601 case PROP_MAX_SIZE_BYTES:
3602 g_value_set_uint (value, queue->max_level.bytes);
3604 case PROP_MAX_SIZE_BUFFERS:
3605 g_value_set_uint (value, queue->max_level.buffers);
3607 case PROP_MAX_SIZE_TIME:
3608 g_value_set_uint64 (value, queue->max_level.time);
3610 case PROP_USE_BUFFERING:
3611 g_value_set_boolean (value, queue->use_buffering);
3613 case PROP_USE_RATE_ESTIMATE:
3614 g_value_set_boolean (value, queue->use_rate_estimate);
3616 case PROP_LOW_PERCENT:
3617 g_value_set_int (value, queue->low_percent);
3619 case PROP_HIGH_PERCENT:
3620 g_value_set_int (value, queue->high_percent);
3622 case PROP_TEMP_TEMPLATE:
3623 g_value_set_string (value, queue->temp_template);
3625 case PROP_TEMP_LOCATION:
3626 g_value_set_string (value, queue->temp_location);
3628 case PROP_TEMP_REMOVE:
3629 g_value_set_boolean (value, queue->temp_remove);
3631 case PROP_RING_BUFFER_MAX_SIZE:
3632 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3634 case PROP_AVG_IN_RATE:
3636 gdouble in_rate = queue->byte_in_rate;
3638 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3639 * calculated, so calculate it here. */
3640 if (in_rate == 0.0 && queue->bytes_in
3641 && queue->last_update_in_rates_elapsed > 0.0)
3642 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3644 g_value_set_int64 (value, (gint64) in_rate);
3648 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3652 GST_QUEUE2_MUTEX_UNLOCK (queue);