2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
29 * Data is queued until one of the limits specified by the
30 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
31 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
38 * You can query how many buffers are queued by reading the
39 * #GstQueue2:current-level-buffers property.
41 * The default queue size limits are 100 buffers, 2MB of data, or
42 * two seconds worth of data, whichever is reached first.
44 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
45 * will allocate a random free filename and buffer data in the file.
46 * By using this, it will buffer the entire stream data on the file independently
47 * of the queue size limits, they will only be used for buffering statistics.
49 * The temp-location property will be used to notify the application of the
57 #include "gstqueue2.h"
59 #include <glib/gstdio.h>
61 #include "gst/gst-i18n-lib.h"
62 #include "gst/glib-compat-private.h"
67 #include <io.h> /* lseek, open, close, read */
69 #define lseek _lseeki64
76 #ifdef __BIONIC__ /* Android */
84 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
94 GST_DEBUG_CATEGORY_STATIC (queue_debug);
95 #define GST_CAT_DEFAULT (queue_debug)
96 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
104 #define DEFAULT_BUFFER_SIZE 4096
105 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
106 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
107 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
109 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
111 /* default property values */
112 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
113 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
114 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
115 #define DEFAULT_USE_BUFFERING FALSE
116 #define DEFAULT_USE_TAGS_BITRATE FALSE
117 #define DEFAULT_USE_RATE_ESTIMATE TRUE
118 #define DEFAULT_LOW_PERCENT 10
119 #define DEFAULT_HIGH_PERCENT 99
120 #define DEFAULT_TEMP_REMOVE TRUE
121 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
126 PROP_CUR_LEVEL_BUFFERS,
127 PROP_CUR_LEVEL_BYTES,
129 PROP_MAX_SIZE_BUFFERS,
133 PROP_USE_TAGS_BITRATE,
134 PROP_USE_RATE_ESTIMATE,
140 PROP_RING_BUFFER_MAX_SIZE,
145 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
152 #define STATUS(queue, pad, msg) \
153 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
154 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
155 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
156 " ns, %"G_GUINT64_FORMAT" items", \
157 GST_DEBUG_PAD_NAME (pad), \
158 queue->cur_level.buffers, \
159 queue->max_level.buffers, \
160 queue->cur_level.bytes, \
161 queue->max_level.bytes, \
162 queue->cur_level.time, \
163 queue->max_level.time, \
164 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
165 queue->current->writing_pos - queue->current->max_reading_pos : \
166 queue->queue.length))
168 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
169 g_mutex_lock (&q->qlock); \
172 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
173 GST_QUEUE2_MUTEX_LOCK (q); \
174 if (res != GST_FLOW_OK) \
178 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
179 g_mutex_unlock (&q->qlock); \
182 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
183 STATUS (queue, q->sinkpad, "wait for DEL"); \
184 q->waiting_del = TRUE; \
185 g_cond_wait (&q->item_del, &queue->qlock); \
186 q->waiting_del = FALSE; \
187 if (res != GST_FLOW_OK) { \
188 STATUS (queue, q->srcpad, "received DEL wakeup"); \
191 STATUS (queue, q->sinkpad, "received DEL"); \
194 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
195 STATUS (queue, q->srcpad, "wait for ADD"); \
196 q->waiting_add = TRUE; \
197 g_cond_wait (&q->item_add, &q->qlock); \
198 q->waiting_add = FALSE; \
199 if (res != GST_FLOW_OK) { \
200 STATUS (queue, q->srcpad, "received ADD wakeup"); \
203 STATUS (queue, q->srcpad, "received ADD"); \
206 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
207 if (q->waiting_del) { \
208 STATUS (q, q->srcpad, "signal DEL"); \
209 g_cond_signal (&q->item_del); \
213 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
214 if (q->waiting_add) { \
215 STATUS (q, q->sinkpad, "signal ADD"); \
216 g_cond_signal (&q->item_add); \
220 #define SET_PERCENT(q, perc) G_STMT_START { \
221 if (perc != q->buffering_percent) { \
222 q->buffering_percent = perc; \
223 q->percent_changed = TRUE; \
224 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
225 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
226 &q->buffering_left); \
231 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
232 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
233 "dataflow inside the queue element");
234 #define gst_queue2_parent_class parent_class
235 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
237 static void gst_queue2_finalize (GObject * object);
239 static void gst_queue2_set_property (GObject * object,
240 guint prop_id, const GValue * value, GParamSpec * pspec);
241 static void gst_queue2_get_property (GObject * object,
242 guint prop_id, GValue * value, GParamSpec * pspec);
244 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
246 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
247 GstBufferList * buffer_list);
248 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
249 static void gst_queue2_loop (GstPad * pad);
251 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
253 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
256 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
258 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
260 static gboolean gst_queue2_handle_query (GstElement * element,
263 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
264 guint64 offset, guint length, GstBuffer ** buffer);
266 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
267 GstPadMode mode, gboolean active);
268 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
269 GstPadMode mode, gboolean active);
270 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
271 GstStateChange transition);
273 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
274 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
276 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
277 static void update_in_rates (GstQueue2 * queue);
278 static void gst_queue2_post_buffering (GstQueue2 * queue);
282 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
283 GST_QUEUE2_ITEM_TYPE_BUFFER,
284 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
285 GST_QUEUE2_ITEM_TYPE_EVENT,
286 GST_QUEUE2_ITEM_TYPE_QUERY
291 GstQueue2ItemType type;
295 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
298 gst_queue2_class_init (GstQueue2Class * klass)
300 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
301 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
303 gobject_class->set_property = gst_queue2_set_property;
304 gobject_class->get_property = gst_queue2_get_property;
307 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
308 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
309 "Current amount of data in the queue (bytes)",
310 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
312 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
313 "Current number of buffers in the queue",
314 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
316 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
317 "Current amount of data in the queue (in ns)",
318 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
321 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
322 "Max. amount of data in the queue (bytes, 0=disable)",
323 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
324 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
325 G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
327 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
328 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
329 DEFAULT_MAX_SIZE_BUFFERS,
330 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
331 G_PARAM_STATIC_STRINGS));
332 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
333 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
334 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
335 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
336 G_PARAM_STATIC_STRINGS));
338 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
339 g_param_spec_boolean ("use-buffering", "Use buffering",
340 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
341 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
342 G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_USE_TAGS_BITRATE,
344 g_param_spec_boolean ("use-tags-bitrate", "Use bitrate from tags",
345 "Use a bitrate from upstream tags to estimate buffer duration if not provided",
346 DEFAULT_USE_TAGS_BITRATE,
347 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
348 G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
350 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
351 "Estimate the bitrate of the stream to calculate time level",
352 DEFAULT_USE_RATE_ESTIMATE,
353 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
355 g_param_spec_int ("low-percent", "Low percent",
356 "Low threshold for buffering to start. Only used if use-buffering is True",
357 0, 100, DEFAULT_LOW_PERCENT,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
360 g_param_spec_int ("high-percent", "High percent",
361 "High threshold for buffering to finish. Only used if use-buffering is True",
362 0, 100, DEFAULT_HIGH_PERCENT,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
366 g_param_spec_string ("temp-template", "Temporary File Template",
367 "File template to store temporary files in, should contain directory "
368 "and XXXXXX. (NULL == disabled)",
369 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
372 g_param_spec_string ("temp-location", "Temporary File Location",
373 "Location to store temporary files in (Only read this property, "
374 "use temp-template to configure the name template)",
375 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
378 * GstQueue2:temp-remove
380 * When temp-template is set, remove the temporary file when going to READY.
382 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
383 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
384 "Remove the temp-location after use",
385 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 * GstQueue2:ring-buffer-max-size
390 * The maximum size of the ring buffer in bytes. If set to 0, the ring
391 * buffer is disabled. Default 0.
393 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
394 g_param_spec_uint64 ("ring-buffer-max-size",
395 "Max. ring buffer size (bytes)",
396 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
397 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
401 * GstQueue2:avg-in-rate
403 * The average input data rate.
405 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
406 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
407 "Average input data rate (bytes/s)",
408 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
410 /* set several parent class virtual functions */
411 gobject_class->finalize = gst_queue2_finalize;
413 gst_element_class_add_pad_template (gstelement_class,
414 gst_static_pad_template_get (&srctemplate));
415 gst_element_class_add_pad_template (gstelement_class,
416 gst_static_pad_template_get (&sinktemplate));
418 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
421 "Erik Walthinsen <omega@cse.ogi.edu>, "
422 "Wim Taymans <wim.taymans@gmail.com>");
424 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
425 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
429 gst_queue2_init (GstQueue2 * queue)
431 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
433 gst_pad_set_chain_function (queue->sinkpad,
434 GST_DEBUG_FUNCPTR (gst_queue2_chain));
435 gst_pad_set_chain_list_function (queue->sinkpad,
436 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
437 gst_pad_set_activatemode_function (queue->sinkpad,
438 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
439 gst_pad_set_event_function (queue->sinkpad,
440 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
441 gst_pad_set_query_function (queue->sinkpad,
442 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
443 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
444 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
446 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
448 gst_pad_set_activatemode_function (queue->srcpad,
449 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
450 gst_pad_set_getrange_function (queue->srcpad,
451 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
452 gst_pad_set_event_function (queue->srcpad,
453 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
454 gst_pad_set_query_function (queue->srcpad,
455 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
456 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
457 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
460 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
461 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
462 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
463 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
464 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
465 queue->use_buffering = DEFAULT_USE_BUFFERING;
466 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
467 queue->low_percent = DEFAULT_LOW_PERCENT;
468 queue->high_percent = DEFAULT_HIGH_PERCENT;
470 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
471 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
473 queue->sinktime = GST_CLOCK_TIME_NONE;
474 queue->srctime = GST_CLOCK_TIME_NONE;
475 queue->sink_tainted = TRUE;
476 queue->src_tainted = TRUE;
478 queue->srcresult = GST_FLOW_FLUSHING;
479 queue->sinkresult = GST_FLOW_FLUSHING;
480 queue->is_eos = FALSE;
481 queue->in_timer = g_timer_new ();
482 queue->out_timer = g_timer_new ();
484 g_mutex_init (&queue->qlock);
485 queue->waiting_add = FALSE;
486 g_cond_init (&queue->item_add);
487 queue->waiting_del = FALSE;
488 g_cond_init (&queue->item_del);
489 g_queue_init (&queue->queue);
491 g_cond_init (&queue->query_handled);
492 queue->last_query = FALSE;
494 g_mutex_init (&queue->buffering_post_lock);
495 queue->buffering_percent = 100;
497 /* tempfile related */
498 queue->temp_template = NULL;
499 queue->temp_location = NULL;
500 queue->temp_remove = DEFAULT_TEMP_REMOVE;
502 queue->ring_buffer = NULL;
503 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
505 GST_DEBUG_OBJECT (queue,
506 "initialized queue's not_empty & not_full conditions");
509 /* called only once, as opposed to dispose */
511 gst_queue2_finalize (GObject * object)
513 GstQueue2 *queue = GST_QUEUE2 (object);
515 GST_DEBUG_OBJECT (queue, "finalizing queue");
517 while (!g_queue_is_empty (&queue->queue)) {
518 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
520 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
521 gst_mini_object_unref (qitem->item);
522 g_slice_free (GstQueue2Item, qitem);
525 queue->last_query = FALSE;
526 g_queue_clear (&queue->queue);
527 g_mutex_clear (&queue->qlock);
528 g_mutex_clear (&queue->buffering_post_lock);
529 g_cond_clear (&queue->item_add);
530 g_cond_clear (&queue->item_del);
531 g_cond_clear (&queue->query_handled);
532 g_timer_destroy (queue->in_timer);
533 g_timer_destroy (queue->out_timer);
535 /* temp_file path cleanup */
536 g_free (queue->temp_template);
537 g_free (queue->temp_location);
539 G_OBJECT_CLASS (parent_class)->finalize (object);
543 debug_ranges (GstQueue2 * queue)
545 GstQueue2Range *walk;
547 for (walk = queue->ranges; walk; walk = walk->next) {
548 GST_DEBUG_OBJECT (queue,
549 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
550 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
551 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
552 walk->rb_writing_pos, walk->reading_pos,
553 walk == queue->current ? "**y**" : " n ");
557 /* clear all the downloaded ranges */
559 clean_ranges (GstQueue2 * queue)
561 GST_DEBUG_OBJECT (queue, "clean queue ranges");
563 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
564 queue->ranges = NULL;
565 queue->current = NULL;
568 /* find a range that contains @offset or NULL when nothing does */
569 static GstQueue2Range *
570 find_range (GstQueue2 * queue, guint64 offset)
572 GstQueue2Range *range = NULL;
573 GstQueue2Range *walk;
575 /* first do a quick check for the current range */
576 for (walk = queue->ranges; walk; walk = walk->next) {
577 if (offset >= walk->offset && offset <= walk->writing_pos) {
578 /* we can reuse an existing range */
584 GST_DEBUG_OBJECT (queue,
585 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
586 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
588 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
594 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
596 guint64 max_reading_pos, writing_pos;
598 writing_pos = range->writing_pos;
599 max_reading_pos = range->max_reading_pos;
601 if (writing_pos > max_reading_pos)
602 queue->cur_level.bytes = writing_pos - max_reading_pos;
604 queue->cur_level.bytes = 0;
607 /* make a new range for @offset or reuse an existing range */
608 static GstQueue2Range *
609 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
611 GstQueue2Range *range, *prev, *next;
613 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
615 if ((range = find_range (queue, offset))) {
616 GST_DEBUG_OBJECT (queue,
617 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
619 if (update_existing && range->writing_pos != offset) {
620 GST_DEBUG_OBJECT (queue, "updating range writing position to "
621 "%" G_GUINT64_FORMAT, offset);
622 range->writing_pos = offset;
625 GST_DEBUG_OBJECT (queue,
626 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
628 range = g_slice_new0 (GstQueue2Range);
629 range->offset = offset;
630 /* we want to write to the next location in the ring buffer */
631 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
632 range->writing_pos = offset;
633 range->rb_writing_pos = range->rb_offset;
634 range->reading_pos = offset;
635 range->max_reading_pos = offset;
639 next = queue->ranges;
641 if (next->offset > offset) {
642 /* insert before next */
643 GST_DEBUG_OBJECT (queue,
644 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
656 queue->ranges = range;
658 debug_ranges (queue);
660 /* update the stats for this range */
661 update_cur_level (queue, range);
667 /* clear and init the download ranges for offset 0 */
669 init_ranges (GstQueue2 * queue)
671 GST_DEBUG_OBJECT (queue, "init queue ranges");
673 /* get rid of all the current ranges */
674 clean_ranges (queue);
675 /* make a range for offset 0 */
676 queue->current = add_range (queue, 0, TRUE);
679 /* calculate the diff between running time on the sink and src of the queue.
680 * This is the total amount of time in the queue. */
682 update_time_level (GstQueue2 * queue)
684 if (queue->sink_tainted) {
686 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
687 queue->sink_segment.position);
688 queue->sink_tainted = FALSE;
691 if (queue->src_tainted) {
693 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
694 queue->src_segment.position);
695 queue->src_tainted = FALSE;
698 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
699 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
701 if (queue->sinktime != GST_CLOCK_TIME_NONE
702 && queue->srctime != GST_CLOCK_TIME_NONE
703 && queue->sinktime >= queue->srctime)
704 queue->cur_level.time = queue->sinktime - queue->srctime;
706 queue->cur_level.time = 0;
709 /* take a SEGMENT event and apply the values to segment, updating the time
712 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
715 gst_event_copy_segment (event, segment);
717 if (segment->format == GST_FORMAT_BYTES) {
718 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
719 /* start is where we'll be getting from and as such writing next */
720 queue->current = add_range (queue, segment->start, TRUE);
724 /* now configure the values, we use these to track timestamps on the
726 if (segment->format != GST_FORMAT_TIME) {
727 /* non-time format, pretend the current time segment is closed with a
728 * 0 start and unknown stop time. */
729 segment->format = GST_FORMAT_TIME;
735 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
738 queue->sink_tainted = TRUE;
740 queue->src_tainted = TRUE;
742 /* segment can update the time level of the queue */
743 update_time_level (queue);
747 apply_gap (GstQueue2 * queue, GstEvent * event,
748 GstSegment * segment, gboolean is_sink)
750 GstClockTime timestamp;
751 GstClockTime duration;
753 gst_event_parse_gap (event, ×tamp, &duration);
755 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
757 if (GST_CLOCK_TIME_IS_VALID (duration)) {
758 timestamp += duration;
761 segment->position = timestamp;
764 queue->sink_tainted = TRUE;
766 queue->src_tainted = TRUE;
768 /* calc diff with other end */
769 update_time_level (queue);
773 /* take a buffer and update segment, updating the time level of the queue. */
775 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
776 guint64 size, gboolean is_sink)
778 GstClockTime duration, timestamp;
780 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
781 duration = GST_BUFFER_DURATION (buffer);
783 /* If we have no duration, pick one from the bitrate if we can */
784 if (duration == GST_CLOCK_TIME_NONE && queue->use_tags_bitrate) {
786 is_sink ? queue->sink_tags_bitrate : queue->src_tags_bitrate;
788 duration = gst_util_uint64_scale (size, 8 * GST_SECOND, bitrate);
791 /* if no timestamp is set, assume it's continuous with the previous
793 if (timestamp == GST_CLOCK_TIME_NONE)
794 timestamp = segment->position;
797 if (duration != GST_CLOCK_TIME_NONE)
798 timestamp += duration;
800 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
801 GST_TIME_ARGS (timestamp));
803 segment->position = timestamp;
806 queue->sink_tainted = TRUE;
808 queue->src_tainted = TRUE;
810 /* calc diff with other end */
811 update_time_level (queue);
816 GstClockTime timestamp;
821 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
823 struct BufListData *bld = data;
824 GstClockTime *timestamp = &bld->timestamp;
827 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
828 " duration %" GST_TIME_FORMAT, idx,
829 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
830 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
831 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
833 btime = GST_BUFFER_DTS_OR_PTS (*buf);
834 if (GST_CLOCK_TIME_IS_VALID (btime))
837 if (GST_BUFFER_DURATION_IS_VALID (*buf))
838 *timestamp += GST_BUFFER_DURATION (*buf);
839 else if (bld->bitrate != 0) {
840 guint64 size = gst_buffer_get_size (*buf);
842 /* If we have no duration, pick one from the bitrate if we can */
843 *timestamp += gst_util_uint64_scale (bld->bitrate, 8 * GST_SECOND, size);
847 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
851 /* take a buffer list and update segment, updating the time level of the queue */
853 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
854 GstSegment * segment, gboolean is_sink)
856 struct BufListData bld;
858 /* if no timestamp is set, assume it's continuous with the previous time */
859 bld.timestamp = segment->position;
861 if (queue->use_tags_bitrate) {
863 bld.bitrate = queue->sink_tags_bitrate;
865 bld.bitrate = queue->src_tags_bitrate;
869 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &bld);
871 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
872 GST_TIME_ARGS (bld.timestamp));
874 segment->position = bld.timestamp;
877 queue->sink_tainted = TRUE;
879 queue->src_tainted = TRUE;
881 /* calc diff with other end */
882 update_time_level (queue);
886 get_percent (guint64 cur_level, guint64 max_level, guint64 alt_max)
894 p = gst_util_uint64_scale (cur_level, 100, MIN (max_level, alt_max));
896 p = gst_util_uint64_scale (cur_level, 100, max_level);
902 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
907 if (queue->high_percent <= 0) {
911 *is_buffering = FALSE;
914 #define GET_PERCENT(format,alt_max) \
915 get_percent(queue->cur_level.format,queue->max_level.format,(alt_max))
918 /* on EOS we are always 100% full, we set the var here so that it we can
919 * reuse the logic below to stop buffering */
921 GST_LOG_OBJECT (queue, "we are EOS");
923 GST_LOG_OBJECT (queue,
924 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
925 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
926 queue->cur_level.buffers);
928 /* figure out the percent we are filled, we take the max of all formats. */
929 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
930 perc = GET_PERCENT (bytes, 0);
932 guint64 rb_size = queue->ring_buffer_max_size;
933 perc = GET_PERCENT (bytes, rb_size);
936 perc2 = GET_PERCENT (time, 0);
937 perc = MAX (perc, perc2);
939 perc2 = GET_PERCENT (buffers, 0);
940 perc = MAX (perc, perc2);
942 /* also apply the rate estimate when we need to */
943 if (queue->use_rate_estimate) {
944 perc2 = GET_PERCENT (rate_time, 0);
945 perc = MAX (perc, perc2);
948 /* Don't get to 0% unless we're really empty */
949 if (queue->cur_level.bytes > 0)
950 perc = MAX (1, perc);
955 *is_buffering = queue->is_buffering;
957 /* scale to high percent so that it becomes the 100% mark */
958 perc = perc * 100 / queue->high_percent;
966 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
973 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
974 gint * avg_in, gint * avg_out, gint64 * buffering_left)
977 if (!QUEUE_IS_USING_QUEUE (queue)) {
978 if (QUEUE_IS_USING_RING_BUFFER (queue))
979 *mode = GST_BUFFERING_TIMESHIFT;
981 *mode = GST_BUFFERING_DOWNLOAD;
983 *mode = GST_BUFFERING_STREAM;
988 *avg_in = queue->byte_in_rate;
990 *avg_out = queue->byte_out_rate;
992 if (buffering_left) {
993 *buffering_left = (percent == 100 ? 0 : -1);
995 if (queue->use_rate_estimate) {
998 max = queue->max_level.rate_time;
999 cur = queue->cur_level.rate_time;
1001 if (percent != 100 && max > cur)
1002 *buffering_left = (max - cur) / 1000000;
1008 gst_queue2_post_buffering (GstQueue2 * queue)
1010 GstMessage *msg = NULL;
1012 g_mutex_lock (&queue->buffering_post_lock);
1013 GST_QUEUE2_MUTEX_LOCK (queue);
1014 if (queue->percent_changed) {
1015 gint percent = queue->buffering_percent;
1017 queue->percent_changed = FALSE;
1019 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
1020 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
1022 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
1023 queue->avg_out, queue->buffering_left);
1025 GST_QUEUE2_MUTEX_UNLOCK (queue);
1028 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1030 g_mutex_unlock (&queue->buffering_post_lock);
1034 update_buffering (GstQueue2 * queue)
1038 /* Ensure the variables used to calculate buffering state are up-to-date. */
1040 update_cur_level (queue, queue->current);
1041 update_in_rates (queue);
1043 if (!get_buffering_percent (queue, NULL, &percent))
1046 if (queue->is_buffering) {
1047 /* if we were buffering see if we reached the high watermark */
1049 queue->is_buffering = FALSE;
1051 SET_PERCENT (queue, percent);
1053 /* we were not buffering, check if we need to start buffering if we drop
1054 * below the low threshold */
1055 if (percent < queue->low_percent) {
1056 queue->is_buffering = TRUE;
1057 SET_PERCENT (queue, percent);
1063 reset_rate_timer (GstQueue2 * queue)
1065 queue->bytes_in = 0;
1066 queue->bytes_out = 0;
1067 queue->byte_in_rate = 0.0;
1068 queue->byte_in_period = 0;
1069 queue->byte_out_rate = 0.0;
1070 queue->last_update_in_rates_elapsed = 0.0;
1071 queue->last_in_elapsed = 0.0;
1072 queue->last_out_elapsed = 0.0;
1073 queue->in_timer_started = FALSE;
1074 queue->out_timer_started = FALSE;
1077 /* the interval in seconds to recalculate the rate */
1078 #define RATE_INTERVAL 0.2
1079 /* Tuning for rate estimation. We use a large window for the input rate because
1080 * it should be stable when connected to a network. The output rate is less
1081 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1082 * therefore adapt more quickly.
1083 * However, initial input rate may be subject to a burst, and should therefore
1084 * initially also adapt more quickly to changes, and only later on give higher
1085 * weight to previous values. */
1086 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1087 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1090 update_in_rates (GstQueue2 * queue)
1092 gdouble elapsed, period;
1093 gdouble byte_in_rate;
1095 if (!queue->in_timer_started) {
1096 queue->in_timer_started = TRUE;
1097 g_timer_start (queue->in_timer);
1101 queue->last_update_in_rates_elapsed = elapsed =
1102 g_timer_elapsed (queue->in_timer, NULL);
1104 /* recalc after each interval. */
1105 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1106 period = elapsed - queue->last_in_elapsed;
1108 GST_DEBUG_OBJECT (queue,
1109 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1110 period, queue->bytes_in, queue->byte_in_period);
1112 byte_in_rate = queue->bytes_in / period;
1114 if (queue->byte_in_rate == 0.0)
1115 queue->byte_in_rate = byte_in_rate;
1117 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1118 (double) queue->byte_in_period, period);
1120 /* another data point, cap at 16 for long time running average */
1121 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1122 queue->byte_in_period += period;
1124 /* reset the values to calculate rate over the next interval */
1125 queue->last_in_elapsed = elapsed;
1126 queue->bytes_in = 0;
1129 if (queue->byte_in_rate > 0.0) {
1130 queue->cur_level.rate_time =
1131 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1133 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1134 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1138 update_out_rates (GstQueue2 * queue)
1140 gdouble elapsed, period;
1141 gdouble byte_out_rate;
1143 if (!queue->out_timer_started) {
1144 queue->out_timer_started = TRUE;
1145 g_timer_start (queue->out_timer);
1149 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1151 /* recalc after each interval. */
1152 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1153 period = elapsed - queue->last_out_elapsed;
1155 GST_DEBUG_OBJECT (queue,
1156 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1158 byte_out_rate = queue->bytes_out / period;
1160 if (queue->byte_out_rate == 0.0)
1161 queue->byte_out_rate = byte_out_rate;
1163 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1165 /* reset the values to calculate rate over the next interval */
1166 queue->last_out_elapsed = elapsed;
1167 queue->bytes_out = 0;
1169 if (queue->byte_in_rate > 0.0) {
1170 queue->cur_level.rate_time =
1171 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1173 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1174 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1178 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1180 guint64 reading_pos, max_reading_pos;
1183 max_reading_pos = range->max_reading_pos;
1185 max_reading_pos = MAX (max_reading_pos, reading_pos);
1187 GST_DEBUG_OBJECT (queue,
1188 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1189 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1190 range->max_reading_pos = max_reading_pos;
1192 update_cur_level (queue, range);
1196 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1201 /* until we receive the FLUSH_STOP from this seek, we skip data */
1202 queue->seeking = TRUE;
1203 GST_QUEUE2_MUTEX_UNLOCK (queue);
1205 debug_ranges (queue);
1207 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1210 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1211 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1212 GST_SEEK_TYPE_NONE, -1);
1214 res = gst_pad_push_event (queue->sinkpad, event);
1215 GST_QUEUE2_MUTEX_LOCK (queue);
1218 /* Between us sending the seek event and re-acquiring the lock, the source
1219 * thread might already have pushed data and moved along the range's
1220 * writing_pos beyond the seek offset. In that case we don't want to set
1221 * the writing position back to the requested seek position, as it would
1222 * cause data to be written to the wrong offset in the file or ring buffer.
1223 * We still do the add_range call to switch the current range to the
1224 * requested range, or create one if one doesn't exist yet. */
1225 queue->current = add_range (queue, offset, FALSE);
1231 /* get the threshold for when we decide to seek rather than wait */
1233 get_seek_threshold (GstQueue2 * queue)
1237 /* FIXME, find a good threshold based on the incoming rate. */
1238 threshold = 1024 * 512;
1240 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1241 threshold = MIN (threshold,
1242 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1247 /* see if there is enough data in the file to read a full buffer */
1249 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1251 GstQueue2Range *range;
1253 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1256 if ((range = find_range (queue, offset))) {
1257 if (queue->current != range) {
1258 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1259 perform_seek_to_offset (queue, range->writing_pos);
1262 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1263 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1265 /* we have a range for offset */
1266 GST_DEBUG_OBJECT (queue,
1267 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1268 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1270 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1273 if (offset + length <= range->writing_pos)
1276 GST_DEBUG_OBJECT (queue,
1277 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1278 (offset + length) - range->writing_pos);
1281 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1282 " len %u", offset, length);
1283 /* we don't have the range, see how far away we are */
1284 if (!queue->is_eos && queue->current) {
1285 guint64 threshold = get_seek_threshold (queue);
1287 if (offset >= queue->current->offset && offset <=
1288 queue->current->writing_pos + threshold) {
1289 GST_INFO_OBJECT (queue,
1290 "requested data is within range, wait for data");
1295 /* too far away, do a seek */
1296 perform_seek_to_offset (queue, offset);
1303 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1304 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1305 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1307 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1310 static GstFlowReturn
1311 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1312 guint8 * dst, gint64 * read_return)
1314 guint8 *ring_buffer;
1317 ring_buffer = queue->ring_buffer;
1319 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1322 /* this should not block */
1323 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1325 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1326 res = fread (dst, 1, length, queue->temp_file);
1328 memcpy (dst, ring_buffer + offset, length);
1332 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1334 if (G_UNLIKELY (res < length)) {
1335 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1336 goto could_not_read;
1337 /* check for errors or EOF */
1338 if (ferror (queue->temp_file))
1339 goto could_not_read;
1340 if (feof (queue->temp_file) && length > 0)
1350 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1351 return GST_FLOW_ERROR;
1355 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1356 return GST_FLOW_ERROR;
1360 GST_DEBUG ("non-regular file hits EOS");
1361 return GST_FLOW_EOS;
1365 static GstFlowReturn
1366 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1367 GstBuffer ** buffer)
1372 guint64 file_offset;
1373 guint block_length, remaining, read_length;
1377 GstFlowReturn ret = GST_FLOW_OK;
1379 /* allocate the output buffer of the requested size */
1380 if (*buffer == NULL)
1381 buf = gst_buffer_new_allocate (NULL, length, NULL);
1385 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1388 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1392 rb_size = queue->ring_buffer_max_size;
1393 max_size = QUEUE_MAX_BYTES (queue);
1396 while (remaining > 0) {
1397 /* configure how much/whether to read */
1398 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1401 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1404 /* calculate how far away the offset is */
1405 if (queue->current->writing_pos > rpos)
1406 level = queue->current->writing_pos - rpos;
1410 GST_DEBUG_OBJECT (queue,
1411 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1412 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1413 rpos, queue->current->writing_pos, level, max_size);
1415 if (level >= max_size) {
1416 /* we don't have the data but if we have a ring buffer that is full, we
1418 GST_DEBUG_OBJECT (queue,
1419 "ring buffer full, reading QUEUE_MAX_BYTES %"
1420 G_GUINT64_FORMAT " bytes", max_size);
1421 read_length = max_size;
1422 } else if (queue->is_eos) {
1423 /* won't get any more data so read any data we have */
1425 GST_DEBUG_OBJECT (queue,
1426 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1428 read_length = level;
1436 if (read_length == 0) {
1437 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1438 GST_DEBUG_OBJECT (queue,
1439 "update current position [%" G_GUINT64_FORMAT "-%"
1440 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1441 update_cur_pos (queue, queue->current, rpos);
1442 GST_QUEUE2_SIGNAL_DEL (queue);
1445 if (queue->use_buffering)
1446 update_buffering (queue);
1448 GST_DEBUG_OBJECT (queue, "waiting for add");
1449 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1453 /* we have the requested data so read it */
1454 read_length = remaining;
1457 /* set range reading_pos to actual reading position for this read */
1458 queue->current->reading_pos = rpos;
1460 /* configure how much and from where to read */
1461 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1463 (queue->current->rb_offset + (rpos -
1464 queue->current->offset)) % rb_size;
1465 if (file_offset + read_length > rb_size) {
1466 block_length = rb_size - file_offset;
1468 block_length = read_length;
1472 block_length = read_length;
1475 /* while we still have data to read, we loop */
1476 while (read_length > 0) {
1480 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1481 data, &read_return);
1482 if (ret != GST_FLOW_OK)
1485 file_offset += read_return;
1486 if (QUEUE_IS_USING_RING_BUFFER (queue))
1487 file_offset %= rb_size;
1489 data += read_return;
1490 read_length -= read_return;
1491 block_length = read_length;
1492 remaining -= read_return;
1494 rpos = (queue->current->reading_pos += read_return);
1495 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1497 GST_QUEUE2_SIGNAL_DEL (queue);
1498 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1501 gst_buffer_unmap (buf, &info);
1502 gst_buffer_resize (buf, 0, length);
1504 GST_BUFFER_OFFSET (buf) = offset;
1505 GST_BUFFER_OFFSET_END (buf) = offset + length;
1514 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1515 gst_buffer_unmap (buf, &info);
1516 if (*buffer == NULL)
1517 gst_buffer_unref (buf);
1518 return GST_FLOW_EOS;
1522 GST_DEBUG_OBJECT (queue, "we are flushing");
1523 gst_buffer_unmap (buf, &info);
1524 if (*buffer == NULL)
1525 gst_buffer_unref (buf);
1526 return GST_FLOW_FLUSHING;
1530 GST_DEBUG_OBJECT (queue, "we have a read error");
1531 gst_buffer_unmap (buf, &info);
1532 if (*buffer == NULL)
1533 gst_buffer_unref (buf);
1538 /* should be called with QUEUE_LOCK */
1539 static GstMiniObject *
1540 gst_queue2_read_item_from_file (GstQueue2 * queue)
1542 GstMiniObject *item;
1544 if (queue->stream_start_event != NULL) {
1545 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1546 queue->stream_start_event = NULL;
1547 } else if (queue->starting_segment != NULL) {
1548 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1549 queue->starting_segment = NULL;
1552 GstBuffer *buffer = NULL;
1553 guint64 reading_pos;
1555 reading_pos = queue->current->reading_pos;
1558 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1563 item = GST_MINI_OBJECT_CAST (buffer);
1566 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1576 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1577 * the temp filename. */
1579 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1584 if (queue->temp_file)
1585 goto already_opened;
1587 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1589 /* If temp_template was set, allocate a filename and open that file */
1592 if (queue->temp_template == NULL)
1595 /* make copy of the template, we don't want to change this */
1596 name = g_strdup (queue->temp_template);
1599 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1601 fd = g_mkstemp (name);
1605 goto mkstemp_failed;
1607 /* open the file for update/writing */
1608 queue->temp_file = fdopen (fd, "wb+");
1609 /* error creating file */
1610 if (queue->temp_file == NULL)
1613 g_free (queue->temp_location);
1614 queue->temp_location = name;
1616 GST_QUEUE2_MUTEX_UNLOCK (queue);
1618 /* we can't emit the notify with the lock */
1619 g_object_notify (G_OBJECT (queue), "temp-location");
1621 GST_QUEUE2_MUTEX_LOCK (queue);
1623 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1630 GST_DEBUG_OBJECT (queue, "temp file was already open");
1635 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1636 (_("No Temp directory specified.")), (NULL));
1641 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1642 (_("Could not create temp file \"%s\"."), queue->temp_template),
1649 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1650 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1659 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1662 if (queue->temp_file == NULL)
1665 GST_DEBUG_OBJECT (queue, "closing temp file");
1667 fflush (queue->temp_file);
1668 fclose (queue->temp_file);
1670 if (queue->temp_remove) {
1671 if (remove (queue->temp_location) < 0) {
1672 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1673 queue->temp_location, g_strerror (errno));
1677 queue->temp_file = NULL;
1678 clean_ranges (queue);
1682 gst_queue2_flush_temp_file (GstQueue2 * queue)
1684 if (queue->temp_file == NULL)
1687 GST_DEBUG_OBJECT (queue, "flushing temp file");
1689 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1693 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1695 if (!QUEUE_IS_USING_QUEUE (queue)) {
1696 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1697 gst_queue2_flush_temp_file (queue);
1698 init_ranges (queue);
1700 while (!g_queue_is_empty (&queue->queue)) {
1701 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1703 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1704 && GST_EVENT_IS_STICKY (qitem->item)
1705 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1706 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1707 gst_pad_store_sticky_event (queue->srcpad,
1708 GST_EVENT_CAST (qitem->item));
1711 /* Then lose another reference because we are supposed to destroy that
1712 data when flushing */
1713 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1714 gst_mini_object_unref (qitem->item);
1715 g_slice_free (GstQueue2Item, qitem);
1718 queue->last_query = FALSE;
1719 g_cond_signal (&queue->query_handled);
1720 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1721 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1722 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1723 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1724 queue->sink_tainted = queue->src_tainted = TRUE;
1725 if (queue->starting_segment != NULL)
1726 gst_event_unref (queue->starting_segment);
1727 queue->starting_segment = NULL;
1728 queue->segment_event_received = FALSE;
1729 gst_event_replace (&queue->stream_start_event, NULL);
1731 /* we deleted a lot of something */
1732 GST_QUEUE2_SIGNAL_DEL (queue);
1736 gst_queue2_wait_free_space (GstQueue2 * queue)
1738 /* We make space available if we're "full" according to whatever
1739 * the user defined as "full". */
1740 if (gst_queue2_is_filled (queue)) {
1743 /* pause the timer while we wait. The fact that we are waiting does not mean
1744 * the byterate on the input pad is lower */
1745 if ((started = queue->in_timer_started))
1746 g_timer_stop (queue->in_timer);
1748 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1749 "queue is full, waiting for free space");
1751 /* Wait for space to be available, we could be unlocked because of a flush. */
1752 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1754 while (gst_queue2_is_filled (queue));
1756 /* and continue if we were running before */
1758 g_timer_continue (queue->in_timer);
1765 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1771 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1774 guint8 *data, *ring_buffer;
1775 guint size, rb_size;
1776 guint64 writing_pos, new_writing_pos;
1777 GstQueue2Range *range, *prev, *next;
1778 gboolean do_seek = FALSE;
1780 if (QUEUE_IS_USING_RING_BUFFER (queue))
1781 writing_pos = queue->current->rb_writing_pos;
1783 writing_pos = queue->current->writing_pos;
1784 ring_buffer = queue->ring_buffer;
1785 rb_size = queue->ring_buffer_max_size;
1787 gst_buffer_map (buffer, &info, GST_MAP_READ);
1792 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1796 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1797 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1798 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1799 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1800 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1806 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1809 /* calculate the space in the ring buffer not used by data from
1810 * the current range */
1811 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1812 /* wait until there is some free space */
1813 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1815 /* get the amount of space we have */
1816 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1818 /* calculate if we need to split or if we can write the entire
1820 to_write = MIN (size, space);
1822 /* the writing position in the ring buffer after writing (part
1823 * or all of) the buffer */
1824 new_writing_pos = (writing_pos + to_write) % rb_size;
1827 range = queue->ranges;
1829 /* if we need to overwrite data in the ring buffer, we need to
1832 * warning: this code is complicated and includes some
1833 * simplifications - pen, paper and diagrams for the cases
1836 guint64 range_data_start, range_data_end;
1837 GstQueue2Range *range_to_destroy = NULL;
1839 range_data_start = range->rb_offset;
1840 range_data_end = range->rb_writing_pos;
1842 /* handle the special case where the range has no data in it */
1843 if (range->writing_pos == range->offset) {
1844 if (range != queue->current) {
1845 GST_DEBUG_OBJECT (queue,
1846 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1847 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1849 range_to_destroy = range;
1851 prev->next = range->next;
1856 if (range_data_end > range_data_start) {
1857 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1860 if (new_writing_pos > range_data_start) {
1861 if (new_writing_pos >= range_data_end) {
1862 GST_DEBUG_OBJECT (queue,
1863 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1864 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1866 range_to_destroy = range;
1868 prev->next = range->next;
1870 GST_DEBUG_OBJECT (queue,
1871 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1872 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1873 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1874 range->offset + new_writing_pos - range_data_start,
1876 range->offset += (new_writing_pos - range_data_start);
1877 range->rb_offset = new_writing_pos;
1881 guint64 new_wpos_virt = writing_pos + to_write;
1883 if (new_wpos_virt <= range_data_start)
1886 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1887 GST_DEBUG_OBJECT (queue,
1888 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1889 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1891 range_to_destroy = range;
1893 prev->next = range->next;
1895 GST_DEBUG_OBJECT (queue,
1896 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1897 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1898 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1899 range->offset + new_writing_pos - range_data_start,
1901 range->offset += (new_wpos_virt - range_data_start);
1902 range->rb_offset = new_writing_pos;
1907 if (!range_to_destroy)
1910 range = range->next;
1911 if (range_to_destroy) {
1912 if (range_to_destroy == queue->ranges)
1913 queue->ranges = range;
1914 g_slice_free (GstQueue2Range, range_to_destroy);
1915 range_to_destroy = NULL;
1920 new_writing_pos = writing_pos + to_write;
1923 if (QUEUE_IS_USING_TEMP_FILE (queue)
1924 && FSEEK_FILE (queue->temp_file, writing_pos))
1927 if (new_writing_pos > writing_pos) {
1928 GST_INFO_OBJECT (queue,
1929 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1930 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1931 queue->current->writing_pos, queue->current->rb_writing_pos);
1932 /* either not using ring buffer or no wrapping, just write */
1933 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1934 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1937 memcpy (ring_buffer + writing_pos, data, to_write);
1940 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1941 /* try to merge with next range */
1942 while ((next = queue->current->next)) {
1943 GST_INFO_OBJECT (queue,
1944 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1945 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1946 if (new_writing_pos < next->offset)
1949 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1952 /* remove the group */
1953 queue->current->next = next->next;
1955 /* We use the threshold to decide if we want to do a seek or simply
1956 * read the data again. If there is not so much data in the range we
1957 * prefer to avoid to seek and read it again. */
1958 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1959 /* the new range had more data than the threshold, it's worth keeping
1960 * it and doing a seek. */
1961 new_writing_pos = next->writing_pos;
1964 g_slice_free (GstQueue2Range, next);
1966 goto update_and_signal;
1970 guint block_one, block_two;
1972 block_one = rb_size - writing_pos;
1973 block_two = to_write - block_one;
1975 if (block_one > 0) {
1976 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1977 /* write data to end of ring buffer */
1978 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1979 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1982 memcpy (ring_buffer + writing_pos, data, block_one);
1986 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1989 if (block_two > 0) {
1990 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1991 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1992 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1995 memcpy (ring_buffer, data + block_one, block_two);
2001 /* update the writing positions */
2003 GST_INFO_OBJECT (queue,
2004 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2005 to_write, writing_pos, size);
2007 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2009 queue->current->writing_pos += to_write;
2010 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2012 queue->current->writing_pos = writing_pos = new_writing_pos;
2015 perform_seek_to_offset (queue, new_writing_pos);
2017 update_cur_level (queue, queue->current);
2019 /* update the buffering status */
2020 if (queue->use_buffering)
2021 update_buffering (queue);
2023 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2024 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2026 GST_QUEUE2_SIGNAL_ADD (queue);
2029 gst_buffer_unmap (buffer, &info);
2036 GST_DEBUG_OBJECT (queue, "we are flushing");
2037 gst_buffer_unmap (buffer, &info);
2038 /* FIXME - GST_FLOW_EOS ? */
2043 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2044 gst_buffer_unmap (buffer, &info);
2051 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2055 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2056 (_("Error while writing to download file.")),
2057 ("%s", g_strerror (errno)));
2060 gst_buffer_unmap (buffer, &info);
2066 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2068 GstQueue2 *queue = q;
2070 GST_TRACE_OBJECT (queue,
2071 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2072 gst_buffer_get_size (*buf));
2074 if (!gst_queue2_create_write (queue, *buf)) {
2075 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2082 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2084 guint *p_size = data;
2087 buf_size = gst_buffer_get_size (*buf);
2088 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2089 *p_size += buf_size;
2093 /* enqueue an item an update the level stats */
2095 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2096 GstQueue2ItemType item_type)
2098 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2102 buffer = GST_BUFFER_CAST (item);
2103 size = gst_buffer_get_size (buffer);
2105 /* add buffer to the statistics */
2106 if (QUEUE_IS_USING_QUEUE (queue)) {
2107 queue->cur_level.buffers++;
2108 queue->cur_level.bytes += size;
2110 queue->bytes_in += size;
2112 /* apply new buffer to segment stats */
2113 apply_buffer (queue, buffer, &queue->sink_segment, size, TRUE);
2114 /* update the byterate stats */
2115 update_in_rates (queue);
2117 if (!QUEUE_IS_USING_QUEUE (queue)) {
2118 /* FIXME - check return value? */
2119 gst_queue2_create_write (queue, buffer);
2121 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2122 GstBufferList *buffer_list;
2125 buffer_list = GST_BUFFER_LIST_CAST (item);
2127 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2128 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2130 /* add buffer to the statistics */
2131 if (QUEUE_IS_USING_QUEUE (queue)) {
2132 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2133 queue->cur_level.bytes += size;
2135 queue->bytes_in += size;
2137 /* apply new buffer to segment stats */
2138 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2140 /* update the byterate stats */
2141 update_in_rates (queue);
2143 if (!QUEUE_IS_USING_QUEUE (queue)) {
2144 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2146 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2149 event = GST_EVENT_CAST (item);
2151 switch (GST_EVENT_TYPE (event)) {
2153 /* Zero the thresholds, this makes sure the queue is completely
2154 * filled and we can read all data from the queue. */
2155 GST_DEBUG_OBJECT (queue, "we have EOS");
2156 queue->is_eos = TRUE;
2158 case GST_EVENT_SEGMENT:
2159 apply_segment (queue, event, &queue->sink_segment, TRUE);
2160 /* This is our first new segment, we hold it
2161 * as we can't save it on the temp file */
2162 if (!QUEUE_IS_USING_QUEUE (queue)) {
2163 if (queue->segment_event_received)
2164 goto unexpected_event;
2166 queue->segment_event_received = TRUE;
2167 if (queue->starting_segment != NULL)
2168 gst_event_unref (queue->starting_segment);
2169 queue->starting_segment = event;
2172 /* a new segment allows us to accept more buffers if we got EOS
2173 * from downstream */
2174 queue->unexpected = FALSE;
2177 apply_gap (queue, event, &queue->sink_segment, TRUE);
2179 case GST_EVENT_STREAM_START:
2180 if (!QUEUE_IS_USING_QUEUE (queue)) {
2181 gst_event_replace (&queue->stream_start_event, event);
2182 gst_event_unref (event);
2186 case GST_EVENT_CAPS:{
2189 gst_event_parse_caps (event, &caps);
2190 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2192 if (!QUEUE_IS_USING_QUEUE (queue)) {
2193 GST_LOG ("Dropping caps event, not using queue");
2194 gst_event_unref (event);
2200 if (!QUEUE_IS_USING_QUEUE (queue))
2201 goto unexpected_event;
2204 } else if (GST_IS_QUERY (item)) {
2205 /* Can't happen as we check that in the caller */
2206 if (!QUEUE_IS_USING_QUEUE (queue))
2207 g_assert_not_reached ();
2209 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2210 item, GST_OBJECT_NAME (queue));
2211 /* we can't really unref since we don't know what it is */
2216 /* update the buffering status */
2217 if (queue->use_buffering)
2218 update_buffering (queue);
2220 if (QUEUE_IS_USING_QUEUE (queue)) {
2221 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2222 qitem->type = item_type;
2224 g_queue_push_tail (&queue->queue, qitem);
2226 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2229 GST_QUEUE2_SIGNAL_ADD (queue);
2237 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2239 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2240 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2241 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2242 gst_event_unref (GST_EVENT_CAST (item));
2247 /* dequeue an item from the queue and update level stats */
2248 static GstMiniObject *
2249 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2251 GstMiniObject *item;
2253 if (!QUEUE_IS_USING_QUEUE (queue)) {
2254 item = gst_queue2_read_item_from_file (queue);
2256 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2262 g_slice_free (GstQueue2Item, qitem);
2268 if (GST_IS_BUFFER (item)) {
2272 buffer = GST_BUFFER_CAST (item);
2273 size = gst_buffer_get_size (buffer);
2274 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2276 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2277 "retrieved buffer %p from queue", buffer);
2279 if (QUEUE_IS_USING_QUEUE (queue)) {
2280 queue->cur_level.buffers--;
2281 queue->cur_level.bytes -= size;
2283 queue->bytes_out += size;
2285 apply_buffer (queue, buffer, &queue->src_segment, size, FALSE);
2286 /* update the byterate stats */
2287 update_out_rates (queue);
2288 /* update the buffering */
2289 if (queue->use_buffering)
2290 update_buffering (queue);
2292 } else if (GST_IS_EVENT (item)) {
2293 GstEvent *event = GST_EVENT_CAST (item);
2295 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2297 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2298 "retrieved event %p from queue", event);
2300 switch (GST_EVENT_TYPE (event)) {
2302 /* queue is empty now that we dequeued the EOS */
2303 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2305 case GST_EVENT_SEGMENT:
2306 apply_segment (queue, event, &queue->src_segment, FALSE);
2309 apply_gap (queue, event, &queue->src_segment, FALSE);
2314 } else if (GST_IS_BUFFER_LIST (item)) {
2315 GstBufferList *buffer_list;
2318 buffer_list = GST_BUFFER_LIST_CAST (item);
2319 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2320 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2322 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2323 "retrieved buffer list %p from queue", buffer_list);
2325 if (QUEUE_IS_USING_QUEUE (queue)) {
2326 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2327 queue->cur_level.bytes -= size;
2329 queue->bytes_out += size;
2331 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2332 /* update the byterate stats */
2333 update_out_rates (queue);
2334 /* update the buffering */
2335 if (queue->use_buffering)
2336 update_buffering (queue);
2337 } else if (GST_IS_QUERY (item)) {
2338 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2339 "retrieved query %p from queue", item);
2340 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2343 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2344 item, GST_OBJECT_NAME (queue));
2346 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2348 GST_QUEUE2_SIGNAL_DEL (queue);
2355 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2361 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2364 gboolean ret = TRUE;
2367 queue = GST_QUEUE2 (parent);
2369 switch (GST_EVENT_TYPE (event)) {
2370 case GST_EVENT_FLUSH_START:
2372 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2373 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2375 ret = gst_pad_push_event (queue->srcpad, event);
2377 /* now unblock the chain function */
2378 GST_QUEUE2_MUTEX_LOCK (queue);
2379 queue->srcresult = GST_FLOW_FLUSHING;
2380 queue->sinkresult = GST_FLOW_FLUSHING;
2381 /* unblock the loop and chain functions */
2382 GST_QUEUE2_SIGNAL_ADD (queue);
2383 GST_QUEUE2_SIGNAL_DEL (queue);
2384 queue->last_query = FALSE;
2385 g_cond_signal (&queue->query_handled);
2386 GST_QUEUE2_MUTEX_UNLOCK (queue);
2388 /* make sure it pauses, this should happen since we sent
2389 * flush_start downstream. */
2390 gst_pad_pause_task (queue->srcpad);
2391 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2393 GST_QUEUE2_MUTEX_LOCK (queue);
2394 /* flush the sink pad */
2395 queue->sinkresult = GST_FLOW_FLUSHING;
2396 GST_QUEUE2_SIGNAL_DEL (queue);
2397 queue->last_query = FALSE;
2398 g_cond_signal (&queue->query_handled);
2399 GST_QUEUE2_MUTEX_UNLOCK (queue);
2401 gst_event_unref (event);
2405 case GST_EVENT_FLUSH_STOP:
2407 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2409 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2411 ret = gst_pad_push_event (queue->srcpad, event);
2413 GST_QUEUE2_MUTEX_LOCK (queue);
2414 gst_queue2_locked_flush (queue, FALSE, TRUE);
2415 queue->srcresult = GST_FLOW_OK;
2416 queue->sinkresult = GST_FLOW_OK;
2417 queue->is_eos = FALSE;
2418 queue->unexpected = FALSE;
2419 queue->seeking = FALSE;
2420 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2421 /* reset rate counters */
2422 reset_rate_timer (queue);
2423 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2424 queue->srcpad, NULL);
2425 GST_QUEUE2_MUTEX_UNLOCK (queue);
2427 GST_QUEUE2_MUTEX_LOCK (queue);
2428 queue->segment_event_received = FALSE;
2429 queue->is_eos = FALSE;
2430 queue->unexpected = FALSE;
2431 queue->sinkresult = GST_FLOW_OK;
2432 queue->seeking = FALSE;
2433 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2434 GST_QUEUE2_MUTEX_UNLOCK (queue);
2436 gst_event_unref (event);
2440 case GST_EVENT_TAG:{
2441 if (queue->use_tags_bitrate) {
2445 gst_event_parse_tag (event, &tags);
2446 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2447 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2448 GST_QUEUE2_MUTEX_LOCK (queue);
2449 queue->sink_tags_bitrate = bitrate;
2450 GST_QUEUE2_MUTEX_UNLOCK (queue);
2451 GST_LOG_OBJECT (queue, "Sink pad bitrate from tags now %u", bitrate);
2457 if (GST_EVENT_IS_SERIALIZED (event)) {
2458 /* serialized events go in the queue */
2459 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2460 if (queue->srcresult != GST_FLOW_OK) {
2461 /* Errors in sticky event pushing are no problem and ignored here
2462 * as they will cause more meaningful errors during data flow.
2463 * For EOS events, that are not followed by data flow, we still
2464 * return FALSE here though and report an error.
2466 if (!GST_EVENT_IS_STICKY (event)) {
2467 goto out_flow_error;
2468 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2469 if (queue->srcresult == GST_FLOW_NOT_LINKED
2470 || queue->srcresult < GST_FLOW_EOS) {
2471 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2472 (_("Internal data flow error.")),
2473 ("streaming task paused, reason %s (%d)",
2474 gst_flow_get_name (queue->srcresult), queue->srcresult));
2476 goto out_flow_error;
2479 /* refuse more events on EOS */
2482 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2483 GST_QUEUE2_MUTEX_UNLOCK (queue);
2484 gst_queue2_post_buffering (queue);
2486 /* non-serialized events are passed upstream. */
2487 ret = gst_pad_push_event (queue->srcpad, event);
2496 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2497 GST_QUEUE2_MUTEX_UNLOCK (queue);
2498 gst_event_unref (event);
2503 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2504 GST_QUEUE2_MUTEX_UNLOCK (queue);
2505 gst_event_unref (event);
2510 GST_LOG_OBJECT (queue,
2511 "refusing event, we have a downstream flow error: %s",
2512 gst_flow_get_name (queue->srcresult));
2513 GST_QUEUE2_MUTEX_UNLOCK (queue);
2514 gst_event_unref (event);
2520 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2526 queue = GST_QUEUE2 (parent);
2528 switch (GST_QUERY_TYPE (query)) {
2530 if (GST_QUERY_IS_SERIALIZED (query)) {
2531 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2532 /* serialized events go in the queue. We need to be certain that we
2533 * don't cause deadlocks waiting for the query return value. We check if
2534 * the queue is empty (nothing is blocking downstream and the query can
2535 * be pushed for sure) or we are not buffering. If we are buffering,
2536 * the pipeline waits to unblock downstream until our queue fills up
2537 * completely, which can not happen if we block on the query..
2538 * Therefore we only potentially block when we are not buffering. */
2539 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2540 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2541 || !queue->use_buffering)) {
2542 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2543 gst_queue2_locked_enqueue (queue, query,
2544 GST_QUEUE2_ITEM_TYPE_QUERY);
2546 STATUS (queue, queue->sinkpad, "wait for QUERY");
2547 g_cond_wait (&queue->query_handled, &queue->qlock);
2548 if (queue->sinkresult != GST_FLOW_OK)
2550 res = queue->last_query;
2552 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2556 GST_DEBUG_OBJECT (queue,
2557 "refusing query, we are not using the queue");
2560 GST_QUEUE2_MUTEX_UNLOCK (queue);
2561 gst_queue2_post_buffering (queue);
2563 res = gst_pad_query_default (pad, parent, query);
2572 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2573 GST_QUEUE2_MUTEX_UNLOCK (queue);
2579 gst_queue2_is_empty (GstQueue2 * queue)
2581 /* never empty on EOS */
2585 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2586 return queue->current->writing_pos <= queue->current->max_reading_pos;
2588 if (queue->queue.length == 0)
2596 gst_queue2_is_filled (GstQueue2 * queue)
2600 /* always filled on EOS */
2604 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2605 (queue->cur_level.format) >= ((alt_max) ? \
2606 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2608 /* if using a ring buffer we're filled if all ring buffer space is used
2609 * _by the current range_ */
2610 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2611 guint64 rb_size = queue->ring_buffer_max_size;
2612 GST_DEBUG_OBJECT (queue,
2613 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2614 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2615 return CHECK_FILLED (bytes, rb_size);
2618 /* if using file, we're never filled if we don't have EOS */
2619 if (QUEUE_IS_USING_TEMP_FILE (queue))
2622 /* we are never filled when we have no buffers at all */
2623 if (queue->cur_level.buffers == 0)
2626 /* we are filled if one of the current levels exceeds the max */
2627 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2628 || CHECK_FILLED (time, 0);
2630 /* if we need to, use the rate estimate to check against the max time we are
2631 * allowed to queue */
2632 if (queue->use_rate_estimate)
2633 res |= CHECK_FILLED (rate_time, 0);
2639 static GstFlowReturn
2640 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2641 GstMiniObject * item, GstQueue2ItemType item_type)
2643 /* we have to lock the queue since we span threads */
2644 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2645 /* when we received EOS, we refuse more data */
2648 /* when we received unexpected from downstream, refuse more buffers */
2649 if (queue->unexpected)
2650 goto out_unexpected;
2652 /* while we didn't receive the newsegment, we're seeking and we skip data */
2656 if (!gst_queue2_wait_free_space (queue))
2659 /* put buffer in queue now */
2660 gst_queue2_locked_enqueue (queue, item, item_type);
2661 GST_QUEUE2_MUTEX_UNLOCK (queue);
2662 gst_queue2_post_buffering (queue);
2666 /* special conditions */
2669 GstFlowReturn ret = queue->sinkresult;
2671 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2672 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2673 GST_QUEUE2_MUTEX_UNLOCK (queue);
2674 gst_mini_object_unref (item);
2680 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2681 GST_QUEUE2_MUTEX_UNLOCK (queue);
2682 gst_mini_object_unref (item);
2684 return GST_FLOW_EOS;
2688 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2689 GST_QUEUE2_MUTEX_UNLOCK (queue);
2690 gst_mini_object_unref (item);
2696 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2697 GST_QUEUE2_MUTEX_UNLOCK (queue);
2698 gst_mini_object_unref (item);
2700 return GST_FLOW_EOS;
2704 static GstFlowReturn
2705 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2709 queue = GST_QUEUE2 (parent);
2711 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2712 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2713 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2714 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2715 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2717 return gst_queue2_chain_buffer_or_buffer_list (queue,
2718 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2721 static GstFlowReturn
2722 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2723 GstBufferList * buffer_list)
2727 queue = GST_QUEUE2 (parent);
2729 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2730 "received buffer list %p", buffer_list);
2732 return gst_queue2_chain_buffer_or_buffer_list (queue,
2733 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2736 static GstMiniObject *
2737 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2739 GstMiniObject *data;
2741 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2743 /* stop pushing buffers, we dequeue all items until we see an item that we
2744 * can push again, which is EOS or SEGMENT. If there is nothing in the
2745 * queue we can push, we set a flag to make the sinkpad refuse more
2746 * buffers with an EOS return value until we receive something
2747 * pushable again or we get flushed. */
2748 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2749 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2750 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2751 "dropping EOS buffer %p", data);
2752 gst_buffer_unref (GST_BUFFER_CAST (data));
2753 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2754 GstEvent *event = GST_EVENT_CAST (data);
2755 GstEventType type = GST_EVENT_TYPE (event);
2757 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2758 /* we found a pushable item in the queue, push it out */
2759 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2760 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2763 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2764 "dropping EOS event %p", event);
2765 gst_event_unref (event);
2766 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2767 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2768 "dropping EOS buffer list %p", data);
2769 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2770 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2771 queue->last_query = FALSE;
2772 g_cond_signal (&queue->query_handled);
2773 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2776 /* no more items in the queue. Set the unexpected flag so that upstream
2777 * make us refuse any more buffers on the sinkpad. Since we will still
2778 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2779 * task function does not shut down. */
2780 queue->unexpected = TRUE;
2784 /* dequeue an item from the queue an push it downstream. This functions returns
2785 * the result of the push. */
2786 static GstFlowReturn
2787 gst_queue2_push_one (GstQueue2 * queue)
2789 GstFlowReturn result = queue->srcresult;
2790 GstMiniObject *data;
2791 GstQueue2ItemType item_type;
2793 data = gst_queue2_locked_dequeue (queue, &item_type);
2798 STATUS (queue, queue->srcpad, "We have something dequeud");
2799 g_atomic_int_set (&queue->downstream_may_block,
2800 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2801 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2802 GST_QUEUE2_MUTEX_UNLOCK (queue);
2803 gst_queue2_post_buffering (queue);
2805 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2808 buffer = GST_BUFFER_CAST (data);
2810 result = gst_pad_push (queue->srcpad, buffer);
2811 g_atomic_int_set (&queue->downstream_may_block, 0);
2813 /* need to check for srcresult here as well */
2814 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2815 if (result == GST_FLOW_EOS) {
2816 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2819 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2820 * to the caller so that the task function does not shut down */
2821 result = GST_FLOW_OK;
2823 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2824 GstEvent *event = GST_EVENT_CAST (data);
2825 GstEventType type = GST_EVENT_TYPE (event);
2827 if (type == GST_EVENT_TAG) {
2828 if (queue->use_tags_bitrate) {
2832 gst_event_parse_tag (event, &tags);
2833 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2834 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2835 GST_QUEUE2_MUTEX_LOCK (queue);
2836 queue->src_tags_bitrate = bitrate;
2837 GST_QUEUE2_MUTEX_UNLOCK (queue);
2838 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
2843 gst_pad_push_event (queue->srcpad, event);
2845 /* if we're EOS, return EOS so that the task pauses. */
2846 if (type == GST_EVENT_EOS) {
2847 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2848 "pushed EOS event %p, return EOS", event);
2849 result = GST_FLOW_EOS;
2852 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2853 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2854 GstBufferList *buffer_list;
2856 buffer_list = GST_BUFFER_LIST_CAST (data);
2858 result = gst_pad_push_list (queue->srcpad, buffer_list);
2859 g_atomic_int_set (&queue->downstream_may_block, 0);
2861 /* need to check for srcresult here as well */
2862 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2863 if (result == GST_FLOW_EOS) {
2864 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2867 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2868 * to the caller so that the task function does not shut down */
2869 result = GST_FLOW_OK;
2871 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2872 GstQuery *query = GST_QUERY_CAST (data);
2874 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2875 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2876 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2877 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2878 "did query %p, return %d", query, queue->last_query);
2879 g_cond_signal (&queue->query_handled);
2880 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2881 result = GST_FLOW_OK;
2888 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2889 "exit because we have no item in the queue");
2890 return GST_FLOW_ERROR;
2894 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2895 return GST_FLOW_FLUSHING;
2899 /* called repeatedly with @pad as the source pad. This function should push out
2900 * data to the peer element. */
2902 gst_queue2_loop (GstPad * pad)
2907 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2909 /* have to lock for thread-safety */
2910 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2912 if (gst_queue2_is_empty (queue)) {
2915 /* pause the timer while we wait. The fact that we are waiting does not mean
2916 * the byterate on the output pad is lower */
2917 if ((started = queue->out_timer_started))
2918 g_timer_stop (queue->out_timer);
2920 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2921 "queue is empty, waiting for new data");
2923 /* Wait for data to be available, we could be unlocked because of a flush. */
2924 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2926 while (gst_queue2_is_empty (queue));
2928 /* and continue if we were running before */
2930 g_timer_continue (queue->out_timer);
2932 ret = gst_queue2_push_one (queue);
2933 queue->srcresult = ret;
2934 queue->sinkresult = ret;
2935 if (ret != GST_FLOW_OK)
2938 GST_QUEUE2_MUTEX_UNLOCK (queue);
2939 gst_queue2_post_buffering (queue);
2946 gboolean eos = queue->is_eos;
2947 GstFlowReturn ret = queue->srcresult;
2949 gst_pad_pause_task (queue->srcpad);
2950 if (ret == GST_FLOW_FLUSHING) {
2951 gst_queue2_locked_flush (queue, FALSE, FALSE);
2953 GST_QUEUE2_SIGNAL_DEL (queue);
2954 queue->last_query = FALSE;
2955 g_cond_signal (&queue->query_handled);
2957 GST_QUEUE2_MUTEX_UNLOCK (queue);
2958 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2959 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2960 /* let app know about us giving up if upstream is not expected to do so */
2961 /* EOS is already taken care of elsewhere */
2962 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2963 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2964 (_("Internal data flow error.")),
2965 ("streaming task paused, reason %s (%d)",
2966 gst_flow_get_name (ret), ret));
2967 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2974 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2976 gboolean res = TRUE;
2977 GstQueue2 *queue = GST_QUEUE2 (parent);
2979 #ifndef GST_DISABLE_GST_DEBUG
2980 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2981 event, GST_EVENT_TYPE_NAME (event));
2984 switch (GST_EVENT_TYPE (event)) {
2985 case GST_EVENT_FLUSH_START:
2986 if (QUEUE_IS_USING_QUEUE (queue)) {
2987 /* just forward upstream */
2988 res = gst_pad_push_event (queue->sinkpad, event);
2990 /* now unblock the getrange function */
2991 GST_QUEUE2_MUTEX_LOCK (queue);
2992 GST_DEBUG_OBJECT (queue, "flushing");
2993 queue->srcresult = GST_FLOW_FLUSHING;
2994 GST_QUEUE2_SIGNAL_ADD (queue);
2995 GST_QUEUE2_MUTEX_UNLOCK (queue);
2997 /* when using a temp file, we eat the event */
2999 gst_event_unref (event);
3002 case GST_EVENT_FLUSH_STOP:
3003 if (QUEUE_IS_USING_QUEUE (queue)) {
3004 /* just forward upstream */
3005 res = gst_pad_push_event (queue->sinkpad, event);
3007 /* now unblock the getrange function */
3008 GST_QUEUE2_MUTEX_LOCK (queue);
3009 queue->srcresult = GST_FLOW_OK;
3010 GST_QUEUE2_MUTEX_UNLOCK (queue);
3012 /* when using a temp file, we eat the event */
3014 gst_event_unref (event);
3017 case GST_EVENT_RECONFIGURE:
3018 GST_QUEUE2_MUTEX_LOCK (queue);
3019 /* assume downstream is linked now and try to push again */
3020 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3021 queue->srcresult = GST_FLOW_OK;
3022 queue->sinkresult = GST_FLOW_OK;
3023 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3024 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3028 GST_QUEUE2_MUTEX_UNLOCK (queue);
3030 res = gst_pad_push_event (queue->sinkpad, event);
3033 res = gst_pad_push_event (queue->sinkpad, event);
3041 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3045 queue = GST_QUEUE2 (parent);
3047 switch (GST_QUERY_TYPE (query)) {
3048 case GST_QUERY_POSITION:
3053 if (!gst_pad_peer_query (queue->sinkpad, query))
3056 /* get peer position */
3057 gst_query_parse_position (query, &format, &peer_pos);
3059 /* FIXME: this code assumes that there's no discont in the queue */
3061 case GST_FORMAT_BYTES:
3062 peer_pos -= queue->cur_level.bytes;
3064 case GST_FORMAT_TIME:
3065 peer_pos -= queue->cur_level.time;
3068 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3069 "know how to adjust value", gst_format_get_name (format));
3072 /* set updated position */
3073 gst_query_set_position (query, format, peer_pos);
3076 case GST_QUERY_DURATION:
3078 GST_DEBUG_OBJECT (queue, "doing peer query");
3080 if (!gst_pad_peer_query (queue->sinkpad, query))
3083 GST_DEBUG_OBJECT (queue, "peer query success");
3086 case GST_QUERY_BUFFERING:
3089 gboolean is_buffering;
3090 GstBufferingMode mode;
3091 gint avg_in, avg_out;
3092 gint64 buffering_left;
3094 GST_DEBUG_OBJECT (queue, "query buffering");
3096 get_buffering_percent (queue, &is_buffering, &percent);
3097 gst_query_set_buffering_percent (query, is_buffering, percent);
3099 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3101 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3104 if (!QUEUE_IS_USING_QUEUE (queue)) {
3105 /* add ranges for download and ringbuffer buffering */
3107 gint64 start, stop, range_start, range_stop;
3108 guint64 writing_pos;
3109 gint64 estimated_total;
3111 gboolean peer_res, is_eos;
3112 GstQueue2Range *queued_ranges;
3114 /* we need a current download region */
3115 if (queue->current == NULL)
3118 writing_pos = queue->current->writing_pos;
3119 is_eos = queue->is_eos;
3122 /* we're EOS, we know the duration in bytes now */
3124 duration = writing_pos;
3126 /* get duration of upstream in bytes */
3127 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3128 GST_FORMAT_BYTES, &duration);
3131 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3132 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3134 /* calculate remaining and total download time */
3135 if (peer_res && avg_in > 0.0)
3136 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3138 estimated_total = -1;
3140 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3143 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3146 case GST_FORMAT_PERCENT:
3147 /* we need duration */
3152 /* get our available data relative to the duration */
3155 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3160 case GST_FORMAT_BYTES:
3170 /* fill out the buffered ranges */
3171 for (queued_ranges = queue->ranges; queued_ranges;
3172 queued_ranges = queued_ranges->next) {
3174 case GST_FORMAT_PERCENT:
3175 if (duration == -1) {
3181 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3182 queued_ranges->offset, duration);
3184 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3185 queued_ranges->writing_pos, duration);
3187 case GST_FORMAT_BYTES:
3188 range_start = queued_ranges->offset;
3189 range_stop = queued_ranges->writing_pos;
3196 if (range_start == range_stop)
3198 GST_DEBUG_OBJECT (queue,
3199 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3200 G_GINT64_FORMAT, range_start, range_stop);
3201 gst_query_add_buffering_range (query, range_start, range_stop);
3204 gst_query_set_buffering_range (query, format, start, stop,
3209 case GST_QUERY_SCHEDULING:
3212 GstSchedulingFlags flags = 0;
3214 if (!gst_pad_peer_query (queue->sinkpad, query))
3217 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3219 /* we can operate in pull mode when we are using a tempfile */
3220 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3223 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3224 gst_query_set_scheduling (query, flags, 0, -1, 0);
3226 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3227 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3231 /* peer handled other queries */
3232 if (!gst_pad_query_default (pad, parent, query))
3242 GST_DEBUG_OBJECT (queue, "failed peer query");
3248 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3250 GstQueue2 *queue = GST_QUEUE2 (element);
3252 /* simply forward to the srcpad query function */
3253 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3258 gst_queue2_update_upstream_size (GstQueue2 * queue)
3260 gint64 upstream_size = -1;
3262 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3264 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3266 /* upstream_size can be negative but queue->upstream_size is unsigned.
3267 * Prevent setting negative values to it (the query can return -1) */
3268 if (upstream_size >= 0)
3269 queue->upstream_size = upstream_size;
3271 queue->upstream_size = 0;
3275 static GstFlowReturn
3276 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3277 guint length, GstBuffer ** buffer)
3282 queue = GST_QUEUE2_CAST (parent);
3284 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3285 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3286 offset = (offset == -1) ? queue->current->reading_pos : offset;
3288 GST_DEBUG_OBJECT (queue,
3289 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3291 /* catch any reads beyond the size of the file here to make sure queue2
3292 * doesn't send seek events beyond the size of the file upstream, since
3293 * that would confuse elements such as souphttpsrc and/or http servers.
3294 * Demuxers often just loop until EOS at the end of the file to figure out
3295 * when they've read all the end-headers or index chunks. */
3296 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3297 gst_queue2_update_upstream_size (queue);
3298 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3299 goto out_unexpected;
3302 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3303 gst_queue2_update_upstream_size (queue);
3304 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3305 length = queue->upstream_size - offset;
3306 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3310 /* FIXME - function will block when the range is not yet available */
3311 ret = gst_queue2_create_read (queue, offset, length, buffer);
3312 GST_QUEUE2_MUTEX_UNLOCK (queue);
3313 gst_queue2_post_buffering (queue);
3320 ret = queue->srcresult;
3322 GST_DEBUG_OBJECT (queue, "we are flushing");
3323 GST_QUEUE2_MUTEX_UNLOCK (queue);
3328 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3329 GST_QUEUE2_MUTEX_UNLOCK (queue);
3330 return GST_FLOW_EOS;
3334 /* sink currently only operates in push mode */
3336 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3337 GstPadMode mode, gboolean active)
3342 queue = GST_QUEUE2 (parent);
3345 case GST_PAD_MODE_PUSH:
3347 GST_QUEUE2_MUTEX_LOCK (queue);
3348 GST_DEBUG_OBJECT (queue, "activating push mode");
3349 queue->srcresult = GST_FLOW_OK;
3350 queue->sinkresult = GST_FLOW_OK;
3351 queue->is_eos = FALSE;
3352 queue->unexpected = FALSE;
3353 reset_rate_timer (queue);
3354 GST_QUEUE2_MUTEX_UNLOCK (queue);
3356 /* unblock chain function */
3357 GST_QUEUE2_MUTEX_LOCK (queue);
3358 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3359 queue->srcresult = GST_FLOW_FLUSHING;
3360 queue->sinkresult = GST_FLOW_FLUSHING;
3361 GST_QUEUE2_SIGNAL_DEL (queue);
3362 /* Unblock query handler */
3363 queue->last_query = FALSE;
3364 g_cond_signal (&queue->query_handled);
3365 GST_QUEUE2_MUTEX_UNLOCK (queue);
3367 /* wait until it is unblocked and clean up */
3368 GST_PAD_STREAM_LOCK (pad);
3369 GST_QUEUE2_MUTEX_LOCK (queue);
3370 gst_queue2_locked_flush (queue, TRUE, FALSE);
3371 GST_QUEUE2_MUTEX_UNLOCK (queue);
3372 GST_PAD_STREAM_UNLOCK (pad);
3383 /* src operating in push mode, we start a task on the source pad that pushes out
3384 * buffers from the queue */
3386 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3388 gboolean result = FALSE;
3391 queue = GST_QUEUE2 (parent);
3394 GST_QUEUE2_MUTEX_LOCK (queue);
3395 GST_DEBUG_OBJECT (queue, "activating push mode");
3396 queue->srcresult = GST_FLOW_OK;
3397 queue->sinkresult = GST_FLOW_OK;
3398 queue->is_eos = FALSE;
3399 queue->unexpected = FALSE;
3401 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3402 GST_QUEUE2_MUTEX_UNLOCK (queue);
3404 /* unblock loop function */
3405 GST_QUEUE2_MUTEX_LOCK (queue);
3406 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3407 queue->srcresult = GST_FLOW_FLUSHING;
3408 queue->sinkresult = GST_FLOW_FLUSHING;
3409 /* the item add signal will unblock */
3410 GST_QUEUE2_SIGNAL_ADD (queue);
3411 GST_QUEUE2_MUTEX_UNLOCK (queue);
3413 /* step 2, make sure streaming finishes */
3414 result = gst_pad_stop_task (pad);
3420 /* pull mode, downstream will call our getrange function */
3422 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3427 queue = GST_QUEUE2 (parent);
3430 GST_QUEUE2_MUTEX_LOCK (queue);
3431 if (!QUEUE_IS_USING_QUEUE (queue)) {
3432 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3433 /* open the temp file now */
3434 result = gst_queue2_open_temp_location_file (queue);
3435 } else if (!queue->ring_buffer) {
3436 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3437 result = ! !queue->ring_buffer;
3442 GST_DEBUG_OBJECT (queue, "activating pull mode");
3443 init_ranges (queue);
3444 queue->srcresult = GST_FLOW_OK;
3445 queue->sinkresult = GST_FLOW_OK;
3446 queue->is_eos = FALSE;
3447 queue->unexpected = FALSE;
3448 queue->upstream_size = 0;
3450 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3451 /* this is not allowed, we cannot operate in pull mode without a temp
3453 queue->srcresult = GST_FLOW_FLUSHING;
3454 queue->sinkresult = GST_FLOW_FLUSHING;
3457 GST_QUEUE2_MUTEX_UNLOCK (queue);
3459 GST_QUEUE2_MUTEX_LOCK (queue);
3460 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3461 queue->srcresult = GST_FLOW_FLUSHING;
3462 queue->sinkresult = GST_FLOW_FLUSHING;
3463 /* this will unlock getrange */
3464 GST_QUEUE2_SIGNAL_ADD (queue);
3466 GST_QUEUE2_MUTEX_UNLOCK (queue);
3473 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3479 case GST_PAD_MODE_PULL:
3480 res = gst_queue2_src_activate_pull (pad, parent, active);
3482 case GST_PAD_MODE_PUSH:
3483 res = gst_queue2_src_activate_push (pad, parent, active);
3486 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3493 static GstStateChangeReturn
3494 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3497 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3499 queue = GST_QUEUE2 (element);
3501 switch (transition) {
3502 case GST_STATE_CHANGE_NULL_TO_READY:
3504 case GST_STATE_CHANGE_READY_TO_PAUSED:
3505 GST_QUEUE2_MUTEX_LOCK (queue);
3506 if (!QUEUE_IS_USING_QUEUE (queue)) {
3507 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3508 if (!gst_queue2_open_temp_location_file (queue))
3509 ret = GST_STATE_CHANGE_FAILURE;
3511 if (queue->ring_buffer) {
3512 g_free (queue->ring_buffer);
3513 queue->ring_buffer = NULL;
3515 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3516 ret = GST_STATE_CHANGE_FAILURE;
3518 init_ranges (queue);
3520 queue->segment_event_received = FALSE;
3521 queue->starting_segment = NULL;
3522 gst_event_replace (&queue->stream_start_event, NULL);
3523 GST_QUEUE2_MUTEX_UNLOCK (queue);
3525 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3531 if (ret == GST_STATE_CHANGE_FAILURE)
3534 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3536 if (ret == GST_STATE_CHANGE_FAILURE)
3539 switch (transition) {
3540 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3542 case GST_STATE_CHANGE_PAUSED_TO_READY:
3543 GST_QUEUE2_MUTEX_LOCK (queue);
3544 if (!QUEUE_IS_USING_QUEUE (queue)) {
3545 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3546 gst_queue2_close_temp_location_file (queue);
3547 } else if (queue->ring_buffer) {
3548 g_free (queue->ring_buffer);
3549 queue->ring_buffer = NULL;
3551 clean_ranges (queue);
3553 if (queue->starting_segment != NULL) {
3554 gst_event_unref (queue->starting_segment);
3555 queue->starting_segment = NULL;
3557 gst_event_replace (&queue->stream_start_event, NULL);
3558 GST_QUEUE2_MUTEX_UNLOCK (queue);
3560 case GST_STATE_CHANGE_READY_TO_NULL:
3569 /* changing the capacity of the queue must wake up
3570 * the _chain function, it might have more room now
3571 * to store the buffer/event in the queue */
3572 #define QUEUE_CAPACITY_CHANGE(q) \
3573 GST_QUEUE2_SIGNAL_DEL (queue); \
3574 if (queue->use_buffering) \
3575 update_buffering (queue);
3577 /* Changing the minimum required fill level must
3578 * wake up the _loop function as it might now
3579 * be able to preceed.
3581 #define QUEUE_THRESHOLD_CHANGE(q)\
3582 GST_QUEUE2_SIGNAL_ADD (queue);
3585 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3589 /* the element must be stopped in order to do this */
3590 GST_OBJECT_LOCK (queue);
3591 state = GST_STATE (queue);
3592 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3594 GST_OBJECT_UNLOCK (queue);
3596 /* set new location */
3597 g_free (queue->temp_template);
3598 queue->temp_template = g_strdup (template);
3605 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3606 GST_OBJECT_UNLOCK (queue);
3611 gst_queue2_set_property (GObject * object,
3612 guint prop_id, const GValue * value, GParamSpec * pspec)
3614 GstQueue2 *queue = GST_QUEUE2 (object);
3616 /* someone could change levels here, and since this
3617 * affects the get/put funcs, we need to lock for safety. */
3618 GST_QUEUE2_MUTEX_LOCK (queue);
3621 case PROP_MAX_SIZE_BYTES:
3622 queue->max_level.bytes = g_value_get_uint (value);
3623 QUEUE_CAPACITY_CHANGE (queue);
3625 case PROP_MAX_SIZE_BUFFERS:
3626 queue->max_level.buffers = g_value_get_uint (value);
3627 QUEUE_CAPACITY_CHANGE (queue);
3629 case PROP_MAX_SIZE_TIME:
3630 queue->max_level.time = g_value_get_uint64 (value);
3631 /* set rate_time to the same value. We use an extra field in the level
3632 * structure so that we can easily access and compare it */
3633 queue->max_level.rate_time = queue->max_level.time;
3634 QUEUE_CAPACITY_CHANGE (queue);
3636 case PROP_USE_BUFFERING:
3637 queue->use_buffering = g_value_get_boolean (value);
3638 if (!queue->use_buffering && queue->is_buffering) {
3639 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3640 "posting 100%% message");
3641 SET_PERCENT (queue, 100);
3642 queue->is_buffering = FALSE;
3645 if (queue->use_buffering) {
3646 queue->is_buffering = TRUE;
3647 update_buffering (queue);
3650 case PROP_USE_TAGS_BITRATE:
3651 queue->use_tags_bitrate = g_value_get_boolean (value);
3653 case PROP_USE_RATE_ESTIMATE:
3654 queue->use_rate_estimate = g_value_get_boolean (value);
3656 case PROP_LOW_PERCENT:
3657 queue->low_percent = g_value_get_int (value);
3659 case PROP_HIGH_PERCENT:
3660 queue->high_percent = g_value_get_int (value);
3662 case PROP_TEMP_TEMPLATE:
3663 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3665 case PROP_TEMP_REMOVE:
3666 queue->temp_remove = g_value_get_boolean (value);
3668 case PROP_RING_BUFFER_MAX_SIZE:
3669 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3672 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3676 GST_QUEUE2_MUTEX_UNLOCK (queue);
3677 gst_queue2_post_buffering (queue);
3681 gst_queue2_get_property (GObject * object,
3682 guint prop_id, GValue * value, GParamSpec * pspec)
3684 GstQueue2 *queue = GST_QUEUE2 (object);
3686 GST_QUEUE2_MUTEX_LOCK (queue);
3689 case PROP_CUR_LEVEL_BYTES:
3690 g_value_set_uint (value, queue->cur_level.bytes);
3692 case PROP_CUR_LEVEL_BUFFERS:
3693 g_value_set_uint (value, queue->cur_level.buffers);
3695 case PROP_CUR_LEVEL_TIME:
3696 g_value_set_uint64 (value, queue->cur_level.time);
3698 case PROP_MAX_SIZE_BYTES:
3699 g_value_set_uint (value, queue->max_level.bytes);
3701 case PROP_MAX_SIZE_BUFFERS:
3702 g_value_set_uint (value, queue->max_level.buffers);
3704 case PROP_MAX_SIZE_TIME:
3705 g_value_set_uint64 (value, queue->max_level.time);
3707 case PROP_USE_BUFFERING:
3708 g_value_set_boolean (value, queue->use_buffering);
3710 case PROP_USE_TAGS_BITRATE:
3711 g_value_set_boolean (value, queue->use_tags_bitrate);
3713 case PROP_USE_RATE_ESTIMATE:
3714 g_value_set_boolean (value, queue->use_rate_estimate);
3716 case PROP_LOW_PERCENT:
3717 g_value_set_int (value, queue->low_percent);
3719 case PROP_HIGH_PERCENT:
3720 g_value_set_int (value, queue->high_percent);
3722 case PROP_TEMP_TEMPLATE:
3723 g_value_set_string (value, queue->temp_template);
3725 case PROP_TEMP_LOCATION:
3726 g_value_set_string (value, queue->temp_location);
3728 case PROP_TEMP_REMOVE:
3729 g_value_set_boolean (value, queue->temp_remove);
3731 case PROP_RING_BUFFER_MAX_SIZE:
3732 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3734 case PROP_AVG_IN_RATE:
3736 gdouble in_rate = queue->byte_in_rate;
3738 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3739 * calculated, so calculate it here. */
3740 if (in_rate == 0.0 && queue->bytes_in
3741 && queue->last_update_in_rates_elapsed > 0.0)
3742 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3744 g_value_set_int64 (value, (gint64) in_rate);
3748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3752 GST_QUEUE2_MUTEX_UNLOCK (queue);