2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
29 * Data is queued until one of the limits specified by the
30 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
31 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
38 * You can query how many buffers are queued by reading the
39 * #GstQueue2:current-level-buffers property.
41 * The default queue size limits are 100 buffers, 2MB of data, or
42 * two seconds worth of data, whichever is reached first.
44 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
45 * will allocate a random free filename and buffer data in the file.
46 * By using this, it will buffer the entire stream data on the file independently
47 * of the queue size limits, they will only be used for buffering statistics.
49 * The temp-location property will be used to notify the application of the
57 #include "gstqueue2.h"
59 #include <glib/gstdio.h>
61 #include "gst/gst-i18n-lib.h"
62 #include "gst/glib-compat-private.h"
67 #include <io.h> /* lseek, open, close, read */
69 #define lseek _lseeki64
76 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
81 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_DEBUG_CATEGORY_STATIC (queue_debug);
87 #define GST_CAT_DEFAULT (queue_debug)
88 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
96 #define DEFAULT_BUFFER_SIZE 4096
97 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
98 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
99 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
101 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
103 /* default property values */
104 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
105 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
106 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
107 #define DEFAULT_USE_BUFFERING FALSE
108 #define DEFAULT_USE_RATE_ESTIMATE TRUE
109 #define DEFAULT_LOW_PERCENT 10
110 #define DEFAULT_HIGH_PERCENT 99
111 #define DEFAULT_TEMP_REMOVE TRUE
112 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
117 PROP_CUR_LEVEL_BUFFERS,
118 PROP_CUR_LEVEL_BYTES,
120 PROP_MAX_SIZE_BUFFERS,
124 PROP_USE_RATE_ESTIMATE,
130 PROP_RING_BUFFER_MAX_SIZE,
134 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
141 #define STATUS(queue, pad, msg) \
142 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
143 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
144 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
145 " ns, %"G_GUINT64_FORMAT" items", \
146 GST_DEBUG_PAD_NAME (pad), \
147 queue->cur_level.buffers, \
148 queue->max_level.buffers, \
149 queue->cur_level.bytes, \
150 queue->max_level.bytes, \
151 queue->cur_level.time, \
152 queue->max_level.time, \
153 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
154 queue->current->writing_pos - queue->current->max_reading_pos : \
155 queue->queue.length))
157 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
158 g_mutex_lock (&q->qlock); \
161 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
162 GST_QUEUE2_MUTEX_LOCK (q); \
163 if (res != GST_FLOW_OK) \
167 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
168 g_mutex_unlock (&q->qlock); \
171 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
172 STATUS (queue, q->sinkpad, "wait for DEL"); \
173 q->waiting_del = TRUE; \
174 g_cond_wait (&q->item_del, &queue->qlock); \
175 q->waiting_del = FALSE; \
176 if (res != GST_FLOW_OK) { \
177 STATUS (queue, q->srcpad, "received DEL wakeup"); \
180 STATUS (queue, q->sinkpad, "received DEL"); \
183 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
184 STATUS (queue, q->srcpad, "wait for ADD"); \
185 q->waiting_add = TRUE; \
186 g_cond_wait (&q->item_add, &q->qlock); \
187 q->waiting_add = FALSE; \
188 if (res != GST_FLOW_OK) { \
189 STATUS (queue, q->srcpad, "received ADD wakeup"); \
192 STATUS (queue, q->srcpad, "received ADD"); \
195 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
196 if (q->waiting_del) { \
197 STATUS (q, q->srcpad, "signal DEL"); \
198 g_cond_signal (&q->item_del); \
202 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
203 if (q->waiting_add) { \
204 STATUS (q, q->sinkpad, "signal ADD"); \
205 g_cond_signal (&q->item_add); \
210 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
211 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
212 "dataflow inside the queue element");
213 #define gst_queue2_parent_class parent_class
214 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
216 static void gst_queue2_finalize (GObject * object);
218 static void gst_queue2_set_property (GObject * object,
219 guint prop_id, const GValue * value, GParamSpec * pspec);
220 static void gst_queue2_get_property (GObject * object,
221 guint prop_id, GValue * value, GParamSpec * pspec);
223 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
225 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
226 GstBufferList * buffer_list);
227 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
228 static void gst_queue2_loop (GstPad * pad);
230 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
232 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
235 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
237 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
239 static gboolean gst_queue2_handle_query (GstElement * element,
242 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
243 guint64 offset, guint length, GstBuffer ** buffer);
245 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
246 GstPadMode mode, gboolean active);
247 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
248 GstPadMode mode, gboolean active);
249 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
250 GstStateChange transition);
252 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
253 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
255 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
256 static void update_in_rates (GstQueue2 * queue);
260 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
261 GST_QUEUE2_ITEM_TYPE_BUFFER,
262 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
263 GST_QUEUE2_ITEM_TYPE_EVENT,
264 GST_QUEUE2_ITEM_TYPE_QUERY
269 GstQueue2ItemType type;
273 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
276 gst_queue2_class_init (GstQueue2Class * klass)
278 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
279 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
281 gobject_class->set_property = gst_queue2_set_property;
282 gobject_class->get_property = gst_queue2_get_property;
285 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
286 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
287 "Current amount of data in the queue (bytes)",
288 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
290 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
291 "Current number of buffers in the queue",
292 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
293 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
294 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
295 "Current amount of data in the queue (in ns)",
296 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
299 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
300 "Max. amount of data in the queue (bytes, 0=disable)",
301 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
302 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
304 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
305 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
306 DEFAULT_MAX_SIZE_BUFFERS,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
309 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
310 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
311 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
314 g_param_spec_boolean ("use-buffering", "Use buffering",
315 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
316 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
318 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
319 "Estimate the bitrate of the stream to calculate time level",
320 DEFAULT_USE_RATE_ESTIMATE,
321 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
323 g_param_spec_int ("low-percent", "Low percent",
324 "Low threshold for buffering to start. Only used if use-buffering is True",
325 0, 100, DEFAULT_LOW_PERCENT,
326 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
328 g_param_spec_int ("high-percent", "High percent",
329 "High threshold for buffering to finish. Only used if use-buffering is True",
330 0, 100, DEFAULT_HIGH_PERCENT,
331 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
334 g_param_spec_string ("temp-template", "Temporary File Template",
335 "File template to store temporary files in, should contain directory "
336 "and XXXXXX. (NULL == disabled)",
337 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
340 g_param_spec_string ("temp-location", "Temporary File Location",
341 "Location to store temporary files in (Only read this property, "
342 "use temp-template to configure the name template)",
343 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
346 * GstQueue2:temp-remove
348 * When temp-template is set, remove the temporary file when going to READY.
350 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
351 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
352 "Remove the temp-location after use",
353 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 * GstQueue2:ring-buffer-max-size
358 * The maximum size of the ring buffer in bytes. If set to 0, the ring
359 * buffer is disabled. Default 0.
361 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
362 g_param_spec_uint64 ("ring-buffer-max-size",
363 "Max. ring buffer size (bytes)",
364 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
365 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
366 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 /* set several parent class virtual functions */
369 gobject_class->finalize = gst_queue2_finalize;
371 gst_element_class_add_pad_template (gstelement_class,
372 gst_static_pad_template_get (&srctemplate));
373 gst_element_class_add_pad_template (gstelement_class,
374 gst_static_pad_template_get (&sinktemplate));
376 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
379 "Erik Walthinsen <omega@cse.ogi.edu>, "
380 "Wim Taymans <wim.taymans@gmail.com>");
382 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
383 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
387 gst_queue2_init (GstQueue2 * queue)
389 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
391 gst_pad_set_chain_function (queue->sinkpad,
392 GST_DEBUG_FUNCPTR (gst_queue2_chain));
393 gst_pad_set_chain_list_function (queue->sinkpad,
394 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
395 gst_pad_set_activatemode_function (queue->sinkpad,
396 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
397 gst_pad_set_event_function (queue->sinkpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
399 gst_pad_set_query_function (queue->sinkpad,
400 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
401 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
402 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
404 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
406 gst_pad_set_activatemode_function (queue->srcpad,
407 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
408 gst_pad_set_getrange_function (queue->srcpad,
409 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
410 gst_pad_set_event_function (queue->srcpad,
411 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
412 gst_pad_set_query_function (queue->srcpad,
413 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
414 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
415 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
418 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
419 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
420 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
421 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
422 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
423 queue->use_buffering = DEFAULT_USE_BUFFERING;
424 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
425 queue->low_percent = DEFAULT_LOW_PERCENT;
426 queue->high_percent = DEFAULT_HIGH_PERCENT;
428 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
429 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
431 queue->sinktime = GST_CLOCK_TIME_NONE;
432 queue->srctime = GST_CLOCK_TIME_NONE;
433 queue->sink_tainted = TRUE;
434 queue->src_tainted = TRUE;
436 queue->srcresult = GST_FLOW_FLUSHING;
437 queue->sinkresult = GST_FLOW_FLUSHING;
438 queue->is_eos = FALSE;
439 queue->in_timer = g_timer_new ();
440 queue->out_timer = g_timer_new ();
442 g_mutex_init (&queue->qlock);
443 queue->waiting_add = FALSE;
444 g_cond_init (&queue->item_add);
445 queue->waiting_del = FALSE;
446 g_cond_init (&queue->item_del);
447 g_queue_init (&queue->queue);
449 g_cond_init (&queue->query_handled);
450 queue->last_query = FALSE;
452 queue->buffering_percent = 100;
454 /* tempfile related */
455 queue->temp_template = NULL;
456 queue->temp_location = NULL;
457 queue->temp_remove = DEFAULT_TEMP_REMOVE;
459 queue->ring_buffer = NULL;
460 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
462 GST_DEBUG_OBJECT (queue,
463 "initialized queue's not_empty & not_full conditions");
466 /* called only once, as opposed to dispose */
468 gst_queue2_finalize (GObject * object)
470 GstQueue2 *queue = GST_QUEUE2 (object);
472 GST_DEBUG_OBJECT (queue, "finalizing queue");
474 while (!g_queue_is_empty (&queue->queue)) {
475 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
477 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
478 gst_mini_object_unref (qitem->item);
479 g_slice_free (GstQueue2Item, qitem);
482 queue->last_query = FALSE;
483 g_queue_clear (&queue->queue);
484 g_mutex_clear (&queue->qlock);
485 g_cond_clear (&queue->item_add);
486 g_cond_clear (&queue->item_del);
487 g_cond_clear (&queue->query_handled);
488 g_timer_destroy (queue->in_timer);
489 g_timer_destroy (queue->out_timer);
491 /* temp_file path cleanup */
492 g_free (queue->temp_template);
493 g_free (queue->temp_location);
495 G_OBJECT_CLASS (parent_class)->finalize (object);
499 debug_ranges (GstQueue2 * queue)
501 GstQueue2Range *walk;
503 for (walk = queue->ranges; walk; walk = walk->next) {
504 GST_DEBUG_OBJECT (queue,
505 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
506 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
507 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
508 walk->rb_writing_pos, walk->reading_pos,
509 walk == queue->current ? "**y**" : " n ");
513 /* clear all the downloaded ranges */
515 clean_ranges (GstQueue2 * queue)
517 GST_DEBUG_OBJECT (queue, "clean queue ranges");
519 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
520 queue->ranges = NULL;
521 queue->current = NULL;
524 /* find a range that contains @offset or NULL when nothing does */
525 static GstQueue2Range *
526 find_range (GstQueue2 * queue, guint64 offset)
528 GstQueue2Range *range = NULL;
529 GstQueue2Range *walk;
531 /* first do a quick check for the current range */
532 for (walk = queue->ranges; walk; walk = walk->next) {
533 if (offset >= walk->offset && offset <= walk->writing_pos) {
534 /* we can reuse an existing range */
540 GST_DEBUG_OBJECT (queue,
541 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
542 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
544 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
550 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
552 guint64 max_reading_pos, writing_pos;
554 writing_pos = range->writing_pos;
555 max_reading_pos = range->max_reading_pos;
557 if (writing_pos > max_reading_pos)
558 queue->cur_level.bytes = writing_pos - max_reading_pos;
560 queue->cur_level.bytes = 0;
563 /* make a new range for @offset or reuse an existing range */
564 static GstQueue2Range *
565 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
567 GstQueue2Range *range, *prev, *next;
569 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
571 if ((range = find_range (queue, offset))) {
572 GST_DEBUG_OBJECT (queue,
573 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
575 if (update_existing && range->writing_pos != offset) {
576 GST_DEBUG_OBJECT (queue, "updating range writing position to "
577 "%" G_GUINT64_FORMAT, offset);
578 range->writing_pos = offset;
581 GST_DEBUG_OBJECT (queue,
582 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
584 range = g_slice_new0 (GstQueue2Range);
585 range->offset = offset;
586 /* we want to write to the next location in the ring buffer */
587 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
588 range->writing_pos = offset;
589 range->rb_writing_pos = range->rb_offset;
590 range->reading_pos = offset;
591 range->max_reading_pos = offset;
595 next = queue->ranges;
597 if (next->offset > offset) {
598 /* insert before next */
599 GST_DEBUG_OBJECT (queue,
600 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
612 queue->ranges = range;
614 debug_ranges (queue);
616 /* update the stats for this range */
617 update_cur_level (queue, range);
623 /* clear and init the download ranges for offset 0 */
625 init_ranges (GstQueue2 * queue)
627 GST_DEBUG_OBJECT (queue, "init queue ranges");
629 /* get rid of all the current ranges */
630 clean_ranges (queue);
631 /* make a range for offset 0 */
632 queue->current = add_range (queue, 0, TRUE);
635 /* calculate the diff between running time on the sink and src of the queue.
636 * This is the total amount of time in the queue. */
638 update_time_level (GstQueue2 * queue)
640 if (queue->sink_tainted) {
642 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
643 queue->sink_segment.position);
644 queue->sink_tainted = FALSE;
647 if (queue->src_tainted) {
649 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
650 queue->src_segment.position);
651 queue->src_tainted = FALSE;
654 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
655 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
657 if (queue->sinktime != GST_CLOCK_TIME_NONE
658 && queue->srctime != GST_CLOCK_TIME_NONE
659 && queue->sinktime >= queue->srctime)
660 queue->cur_level.time = queue->sinktime - queue->srctime;
662 queue->cur_level.time = 0;
665 /* take a SEGMENT event and apply the values to segment, updating the time
668 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
671 gst_event_copy_segment (event, segment);
673 if (segment->format == GST_FORMAT_BYTES) {
674 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
675 /* start is where we'll be getting from and as such writing next */
676 queue->current = add_range (queue, segment->start, TRUE);
680 /* now configure the values, we use these to track timestamps on the
682 if (segment->format != GST_FORMAT_TIME) {
683 /* non-time format, pretent the current time segment is closed with a
684 * 0 start and unknown stop time. */
685 segment->format = GST_FORMAT_TIME;
691 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
694 queue->sink_tainted = TRUE;
696 queue->src_tainted = TRUE;
698 /* segment can update the time level of the queue */
699 update_time_level (queue);
702 /* take a buffer and update segment, updating the time level of the queue. */
704 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
707 GstClockTime duration, timestamp;
709 timestamp = GST_BUFFER_TIMESTAMP (buffer);
710 duration = GST_BUFFER_DURATION (buffer);
712 /* if no timestamp is set, assume it's continuous with the previous
714 if (timestamp == GST_CLOCK_TIME_NONE)
715 timestamp = segment->position;
718 if (duration != GST_CLOCK_TIME_NONE)
719 timestamp += duration;
721 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
722 GST_TIME_ARGS (timestamp));
724 segment->position = timestamp;
727 queue->sink_tainted = TRUE;
729 queue->src_tainted = TRUE;
731 /* calc diff with other end */
732 update_time_level (queue);
736 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
738 GstClockTime *timestamp = data;
740 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
741 " duration %" GST_TIME_FORMAT, idx,
742 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
743 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
745 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
746 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
748 if (GST_BUFFER_DURATION_IS_VALID (*buf))
749 *timestamp += GST_BUFFER_DURATION (*buf);
751 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
755 /* take a buffer list and update segment, updating the time level of the queue */
757 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
758 GstSegment * segment, gboolean is_sink)
760 GstClockTime timestamp;
762 /* if no timestamp is set, assume it's continuous with the previous time */
763 timestamp = segment->position;
765 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
767 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
768 GST_TIME_ARGS (timestamp));
770 segment->position = timestamp;
773 queue->sink_tainted = TRUE;
775 queue->src_tainted = TRUE;
777 /* calc diff with other end */
778 update_time_level (queue);
782 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
787 if (queue->high_percent <= 0) {
791 *is_buffering = FALSE;
794 #define GET_PERCENT(format,alt_max) ((queue->max_level.format) > 0 ? (queue->cur_level.format) * 100 / ((alt_max) > 0 ? MIN ((alt_max), (queue->max_level.format)) : (queue->max_level.format)) : 0)
797 /* on EOS we are always 100% full, we set the var here so that it we can
798 * reuse the logic below to stop buffering */
800 GST_LOG_OBJECT (queue, "we are EOS");
802 /* figure out the percent we are filled, we take the max of all formats. */
803 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
804 perc = GET_PERCENT (bytes, 0);
806 guint64 rb_size = queue->ring_buffer_max_size;
807 perc = GET_PERCENT (bytes, rb_size);
809 perc = MAX (perc, GET_PERCENT (time, 0));
810 perc = MAX (perc, GET_PERCENT (buffers, 0));
812 /* also apply the rate estimate when we need to */
813 if (queue->use_rate_estimate)
814 perc = MAX (perc, GET_PERCENT (rate_time, 0));
819 *is_buffering = queue->is_buffering;
821 /* scale to high percent so that it becomes the 100% mark */
822 perc = perc * 100 / queue->high_percent;
830 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
837 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
838 gint * avg_in, gint * avg_out, gint64 * buffering_left)
841 if (!QUEUE_IS_USING_QUEUE (queue)) {
842 if (QUEUE_IS_USING_RING_BUFFER (queue))
843 *mode = GST_BUFFERING_TIMESHIFT;
845 *mode = GST_BUFFERING_DOWNLOAD;
847 *mode = GST_BUFFERING_STREAM;
852 *avg_in = queue->byte_in_rate;
854 *avg_out = queue->byte_out_rate;
856 if (buffering_left) {
857 *buffering_left = (percent == 100 ? 0 : -1);
859 if (queue->use_rate_estimate) {
862 max = queue->max_level.rate_time;
863 cur = queue->cur_level.rate_time;
865 if (percent != 100 && max > cur)
866 *buffering_left = (max - cur) / 1000000;
872 update_buffering (GstQueue2 * queue)
875 gboolean post = FALSE;
877 /* Ensure the variables used to calculate buffering state are up-to-date. */
879 update_cur_level (queue, queue->current);
880 update_in_rates (queue);
882 if (!get_buffering_percent (queue, NULL, &percent))
885 if (queue->is_buffering) {
887 /* if we were buffering see if we reached the high watermark */
888 if (percent >= queue->high_percent)
889 queue->is_buffering = FALSE;
891 /* we were not buffering, check if we need to start buffering if we drop
892 * below the low threshold */
893 if (percent < queue->low_percent) {
894 queue->is_buffering = TRUE;
900 if (percent == queue->buffering_percent)
903 queue->buffering_percent = percent;
908 GstBufferingMode mode;
909 gint avg_in, avg_out;
910 gint64 buffering_left;
912 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
915 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
917 gst_message_set_buffering_stats (message, mode,
918 avg_in, avg_out, buffering_left);
920 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
925 reset_rate_timer (GstQueue2 * queue)
928 queue->bytes_out = 0;
929 queue->byte_in_rate = 0.0;
930 queue->byte_in_period = 0;
931 queue->byte_out_rate = 0.0;
932 queue->last_in_elapsed = 0.0;
933 queue->last_out_elapsed = 0.0;
934 queue->in_timer_started = FALSE;
935 queue->out_timer_started = FALSE;
938 /* the interval in seconds to recalculate the rate */
939 #define RATE_INTERVAL 0.2
940 /* Tuning for rate estimation. We use a large window for the input rate because
941 * it should be stable when connected to a network. The output rate is less
942 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
943 * therefore adapt more quickly.
944 * However, initial input rate may be subject to a burst, and should therefore
945 * initially also adapt more quickly to changes, and only later on give higher
946 * weight to previous values. */
947 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
948 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
951 update_in_rates (GstQueue2 * queue)
953 gdouble elapsed, period;
954 gdouble byte_in_rate;
956 if (!queue->in_timer_started) {
957 queue->in_timer_started = TRUE;
958 g_timer_start (queue->in_timer);
962 elapsed = g_timer_elapsed (queue->in_timer, NULL);
964 /* recalc after each interval. */
965 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
966 period = elapsed - queue->last_in_elapsed;
968 GST_DEBUG_OBJECT (queue,
969 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
970 period, queue->bytes_in, queue->byte_in_period);
972 byte_in_rate = queue->bytes_in / period;
974 if (queue->byte_in_rate == 0.0)
975 queue->byte_in_rate = byte_in_rate;
977 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
978 (double) queue->byte_in_period, period);
980 /* another data point, cap at 16 for long time running average */
981 if (queue->byte_in_period < 16 * RATE_INTERVAL)
982 queue->byte_in_period += period;
984 /* reset the values to calculate rate over the next interval */
985 queue->last_in_elapsed = elapsed;
989 if (queue->byte_in_rate > 0.0) {
990 queue->cur_level.rate_time =
991 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
993 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
994 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
998 update_out_rates (GstQueue2 * queue)
1000 gdouble elapsed, period;
1001 gdouble byte_out_rate;
1003 if (!queue->out_timer_started) {
1004 queue->out_timer_started = TRUE;
1005 g_timer_start (queue->out_timer);
1009 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1011 /* recalc after each interval. */
1012 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1013 period = elapsed - queue->last_out_elapsed;
1015 GST_DEBUG_OBJECT (queue,
1016 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1018 byte_out_rate = queue->bytes_out / period;
1020 if (queue->byte_out_rate == 0.0)
1021 queue->byte_out_rate = byte_out_rate;
1023 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1025 /* reset the values to calculate rate over the next interval */
1026 queue->last_out_elapsed = elapsed;
1027 queue->bytes_out = 0;
1029 if (queue->byte_in_rate > 0.0) {
1030 queue->cur_level.rate_time =
1031 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1033 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1034 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1038 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1040 guint64 reading_pos, max_reading_pos;
1043 max_reading_pos = range->max_reading_pos;
1045 max_reading_pos = MAX (max_reading_pos, reading_pos);
1047 GST_DEBUG_OBJECT (queue,
1048 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1049 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1050 range->max_reading_pos = max_reading_pos;
1052 update_cur_level (queue, range);
1056 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1061 /* until we receive the FLUSH_STOP from this seek, we skip data */
1062 queue->seeking = TRUE;
1063 GST_QUEUE2_MUTEX_UNLOCK (queue);
1065 debug_ranges (queue);
1067 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1070 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1071 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1072 GST_SEEK_TYPE_NONE, -1);
1074 res = gst_pad_push_event (queue->sinkpad, event);
1075 GST_QUEUE2_MUTEX_LOCK (queue);
1078 /* Between us sending the seek event and re-acquiring the lock, the source
1079 * thread might already have pushed data and moved along the range's
1080 * writing_pos beyond the seek offset. In that case we don't want to set
1081 * the writing position back to the requested seek position, as it would
1082 * cause data to be written to the wrong offset in the file or ring buffer.
1083 * We still do the add_range call to switch the current range to the
1084 * requested range, or create one if one doesn't exist yet. */
1085 queue->current = add_range (queue, offset, FALSE);
1091 /* get the threshold for when we decide to seek rather than wait */
1093 get_seek_threshold (GstQueue2 * queue)
1097 /* FIXME, find a good threshold based on the incoming rate. */
1098 threshold = 1024 * 512;
1100 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1101 threshold = MIN (threshold,
1102 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1107 /* see if there is enough data in the file to read a full buffer */
1109 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1111 GstQueue2Range *range;
1113 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1116 if ((range = find_range (queue, offset))) {
1117 if (queue->current != range) {
1118 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1119 perform_seek_to_offset (queue, range->writing_pos);
1122 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1123 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1125 /* we have a range for offset */
1126 GST_DEBUG_OBJECT (queue,
1127 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1128 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1130 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1133 if (offset + length <= range->writing_pos)
1136 GST_DEBUG_OBJECT (queue,
1137 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1138 (offset + length) - range->writing_pos);
1141 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1142 " len %u", offset, length);
1143 /* we don't have the range, see how far away we are */
1144 if (!queue->is_eos && queue->current) {
1145 guint64 threshold = get_seek_threshold (queue);
1147 if (offset >= queue->current->offset && offset <=
1148 queue->current->writing_pos + threshold) {
1149 GST_INFO_OBJECT (queue,
1150 "requested data is within range, wait for data");
1155 /* too far away, do a seek */
1156 perform_seek_to_offset (queue, offset);
1163 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1164 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1165 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1167 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1170 static GstFlowReturn
1171 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1172 guint8 * dst, gint64 * read_return)
1174 guint8 *ring_buffer;
1177 ring_buffer = queue->ring_buffer;
1179 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1182 /* this should not block */
1183 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1185 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1186 res = fread (dst, 1, length, queue->temp_file);
1188 memcpy (dst, ring_buffer + offset, length);
1192 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1194 if (G_UNLIKELY (res < length)) {
1195 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1196 goto could_not_read;
1197 /* check for errors or EOF */
1198 if (ferror (queue->temp_file))
1199 goto could_not_read;
1200 if (feof (queue->temp_file) && length > 0)
1210 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1211 return GST_FLOW_ERROR;
1215 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1216 return GST_FLOW_ERROR;
1220 GST_DEBUG ("non-regular file hits EOS");
1221 return GST_FLOW_EOS;
1225 static GstFlowReturn
1226 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1227 GstBuffer ** buffer)
1232 guint64 file_offset;
1233 guint block_length, remaining, read_length;
1237 GstFlowReturn ret = GST_FLOW_OK;
1239 /* allocate the output buffer of the requested size */
1240 if (*buffer == NULL)
1241 buf = gst_buffer_new_allocate (NULL, length, NULL);
1245 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1248 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1252 rb_size = queue->ring_buffer_max_size;
1253 max_size = QUEUE_MAX_BYTES (queue);
1256 while (remaining > 0) {
1257 /* configure how much/whether to read */
1258 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1261 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1264 /* calculate how far away the offset is */
1265 if (queue->current->writing_pos > rpos)
1266 level = queue->current->writing_pos - rpos;
1270 GST_DEBUG_OBJECT (queue,
1271 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1272 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1273 rpos, queue->current->writing_pos, level, max_size);
1275 if (level >= max_size) {
1276 /* we don't have the data but if we have a ring buffer that is full, we
1278 GST_DEBUG_OBJECT (queue,
1279 "ring buffer full, reading QUEUE_MAX_BYTES %"
1280 G_GUINT64_FORMAT " bytes", max_size);
1281 read_length = max_size;
1282 } else if (queue->is_eos) {
1283 /* won't get any more data so read any data we have */
1285 GST_DEBUG_OBJECT (queue,
1286 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1288 read_length = level;
1296 if (read_length == 0) {
1297 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1298 GST_DEBUG_OBJECT (queue,
1299 "update current position [%" G_GUINT64_FORMAT "-%"
1300 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1301 update_cur_pos (queue, queue->current, rpos);
1302 GST_QUEUE2_SIGNAL_DEL (queue);
1305 if (queue->use_buffering)
1306 update_buffering (queue);
1308 GST_DEBUG_OBJECT (queue, "waiting for add");
1309 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1313 /* we have the requested data so read it */
1314 read_length = remaining;
1317 /* set range reading_pos to actual reading position for this read */
1318 queue->current->reading_pos = rpos;
1320 /* configure how much and from where to read */
1321 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1323 (queue->current->rb_offset + (rpos -
1324 queue->current->offset)) % rb_size;
1325 if (file_offset + read_length > rb_size) {
1326 block_length = rb_size - file_offset;
1328 block_length = read_length;
1332 block_length = read_length;
1335 /* while we still have data to read, we loop */
1336 while (read_length > 0) {
1340 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1341 data, &read_return);
1342 if (ret != GST_FLOW_OK)
1345 file_offset += read_return;
1346 if (QUEUE_IS_USING_RING_BUFFER (queue))
1347 file_offset %= rb_size;
1349 data += read_return;
1350 read_length -= read_return;
1351 block_length = read_length;
1352 remaining -= read_return;
1354 rpos = (queue->current->reading_pos += read_return);
1355 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1357 GST_QUEUE2_SIGNAL_DEL (queue);
1358 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1361 gst_buffer_unmap (buf, &info);
1362 gst_buffer_resize (buf, 0, length);
1364 GST_BUFFER_OFFSET (buf) = offset;
1365 GST_BUFFER_OFFSET_END (buf) = offset + length;
1374 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1375 gst_buffer_unmap (buf, &info);
1376 if (*buffer == NULL)
1377 gst_buffer_unref (buf);
1378 return GST_FLOW_EOS;
1382 GST_DEBUG_OBJECT (queue, "we are flushing");
1383 gst_buffer_unmap (buf, &info);
1384 if (*buffer == NULL)
1385 gst_buffer_unref (buf);
1386 return GST_FLOW_FLUSHING;
1390 GST_DEBUG_OBJECT (queue, "we have a read error");
1391 gst_buffer_unmap (buf, &info);
1392 if (*buffer == NULL)
1393 gst_buffer_unref (buf);
1398 /* should be called with QUEUE_LOCK */
1399 static GstMiniObject *
1400 gst_queue2_read_item_from_file (GstQueue2 * queue)
1402 GstMiniObject *item;
1404 if (queue->stream_start_event != NULL) {
1405 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1406 queue->stream_start_event = NULL;
1407 } else if (queue->starting_segment != NULL) {
1408 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1409 queue->starting_segment = NULL;
1412 GstBuffer *buffer = NULL;
1413 guint64 reading_pos;
1415 reading_pos = queue->current->reading_pos;
1418 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1423 item = GST_MINI_OBJECT_CAST (buffer);
1426 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1436 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1437 * the temp filename. */
1439 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1444 if (queue->temp_file)
1445 goto already_opened;
1447 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1449 /* If temp_template was set, allocate a filename and open that filen */
1452 if (queue->temp_template == NULL)
1455 /* make copy of the template, we don't want to change this */
1456 name = g_strdup (queue->temp_template);
1457 fd = g_mkstemp (name);
1459 goto mkstemp_failed;
1461 /* open the file for update/writing */
1462 queue->temp_file = fdopen (fd, "wb+");
1463 /* error creating file */
1464 if (queue->temp_file == NULL)
1467 g_free (queue->temp_location);
1468 queue->temp_location = name;
1470 GST_QUEUE2_MUTEX_UNLOCK (queue);
1472 /* we can't emit the notify with the lock */
1473 g_object_notify (G_OBJECT (queue), "temp-location");
1475 GST_QUEUE2_MUTEX_LOCK (queue);
1477 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1484 GST_DEBUG_OBJECT (queue, "temp file was already open");
1489 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1490 (_("No Temp directory specified.")), (NULL));
1495 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1496 (_("Could not create temp file \"%s\"."), queue->temp_template),
1503 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1504 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1513 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1516 if (queue->temp_file == NULL)
1519 GST_DEBUG_OBJECT (queue, "closing temp file");
1521 fflush (queue->temp_file);
1522 fclose (queue->temp_file);
1524 if (queue->temp_remove) {
1525 if (remove (queue->temp_location) < 0) {
1526 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1527 queue->temp_location, g_strerror (errno));
1531 queue->temp_file = NULL;
1532 clean_ranges (queue);
1536 gst_queue2_flush_temp_file (GstQueue2 * queue)
1538 if (queue->temp_file == NULL)
1541 GST_DEBUG_OBJECT (queue, "flushing temp file");
1543 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1547 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1549 if (!QUEUE_IS_USING_QUEUE (queue)) {
1550 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1551 gst_queue2_flush_temp_file (queue);
1552 init_ranges (queue);
1554 while (!g_queue_is_empty (&queue->queue)) {
1555 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1557 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1558 && GST_EVENT_IS_STICKY (qitem->item)
1559 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1560 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1561 gst_pad_store_sticky_event (queue->srcpad,
1562 GST_EVENT_CAST (qitem->item));
1565 /* Then lose another reference because we are supposed to destroy that
1566 data when flushing */
1567 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1568 gst_mini_object_unref (qitem->item);
1569 g_slice_free (GstQueue2Item, qitem);
1572 queue->last_query = FALSE;
1573 g_cond_signal (&queue->query_handled);
1574 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1575 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1576 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1577 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1578 queue->sink_tainted = queue->src_tainted = TRUE;
1579 if (queue->starting_segment != NULL)
1580 gst_event_unref (queue->starting_segment);
1581 queue->starting_segment = NULL;
1582 queue->segment_event_received = FALSE;
1583 gst_event_replace (&queue->stream_start_event, NULL);
1585 /* we deleted a lot of something */
1586 GST_QUEUE2_SIGNAL_DEL (queue);
1590 gst_queue2_wait_free_space (GstQueue2 * queue)
1592 /* We make space available if we're "full" according to whatever
1593 * the user defined as "full". */
1594 if (gst_queue2_is_filled (queue)) {
1597 /* pause the timer while we wait. The fact that we are waiting does not mean
1598 * the byterate on the input pad is lower */
1599 if ((started = queue->in_timer_started))
1600 g_timer_stop (queue->in_timer);
1602 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1603 "queue is full, waiting for free space");
1605 /* Wait for space to be available, we could be unlocked because of a flush. */
1606 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1608 while (gst_queue2_is_filled (queue));
1610 /* and continue if we were running before */
1612 g_timer_continue (queue->in_timer);
1619 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1625 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1628 guint8 *data, *ring_buffer;
1629 guint size, rb_size;
1630 guint64 writing_pos, new_writing_pos;
1631 GstQueue2Range *range, *prev, *next;
1632 gboolean do_seek = FALSE;
1634 if (QUEUE_IS_USING_RING_BUFFER (queue))
1635 writing_pos = queue->current->rb_writing_pos;
1637 writing_pos = queue->current->writing_pos;
1638 ring_buffer = queue->ring_buffer;
1639 rb_size = queue->ring_buffer_max_size;
1641 gst_buffer_map (buffer, &info, GST_MAP_READ);
1646 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1650 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1651 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1652 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1653 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1654 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1660 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1663 /* calculate the space in the ring buffer not used by data from
1664 * the current range */
1665 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1666 /* wait until there is some free space */
1667 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1669 /* get the amount of space we have */
1670 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1672 /* calculate if we need to split or if we can write the entire
1674 to_write = MIN (size, space);
1676 /* the writing position in the ring buffer after writing (part
1677 * or all of) the buffer */
1678 new_writing_pos = (writing_pos + to_write) % rb_size;
1681 range = queue->ranges;
1683 /* if we need to overwrite data in the ring buffer, we need to
1686 * warning: this code is complicated and includes some
1687 * simplifications - pen, paper and diagrams for the cases
1690 guint64 range_data_start, range_data_end;
1691 GstQueue2Range *range_to_destroy = NULL;
1693 range_data_start = range->rb_offset;
1694 range_data_end = range->rb_writing_pos;
1696 /* handle the special case where the range has no data in it */
1697 if (range->writing_pos == range->offset) {
1698 if (range != queue->current) {
1699 GST_DEBUG_OBJECT (queue,
1700 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1701 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1703 range_to_destroy = range;
1705 prev->next = range->next;
1710 if (range_data_end > range_data_start) {
1711 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1714 if (new_writing_pos > range_data_start) {
1715 if (new_writing_pos >= range_data_end) {
1716 GST_DEBUG_OBJECT (queue,
1717 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1718 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1720 range_to_destroy = range;
1722 prev->next = range->next;
1724 GST_DEBUG_OBJECT (queue,
1725 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1726 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1727 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1728 range->offset + new_writing_pos - range_data_start,
1730 range->offset += (new_writing_pos - range_data_start);
1731 range->rb_offset = new_writing_pos;
1735 guint64 new_wpos_virt = writing_pos + to_write;
1737 if (new_wpos_virt <= range_data_start)
1740 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1741 GST_DEBUG_OBJECT (queue,
1742 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1743 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1745 range_to_destroy = range;
1747 prev->next = range->next;
1749 GST_DEBUG_OBJECT (queue,
1750 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1751 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1752 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1753 range->offset + new_writing_pos - range_data_start,
1755 range->offset += (new_wpos_virt - range_data_start);
1756 range->rb_offset = new_writing_pos;
1761 if (!range_to_destroy)
1764 range = range->next;
1765 if (range_to_destroy) {
1766 if (range_to_destroy == queue->ranges)
1767 queue->ranges = range;
1768 g_slice_free (GstQueue2Range, range_to_destroy);
1769 range_to_destroy = NULL;
1774 new_writing_pos = writing_pos + to_write;
1777 if (QUEUE_IS_USING_TEMP_FILE (queue)
1778 && FSEEK_FILE (queue->temp_file, writing_pos))
1781 if (new_writing_pos > writing_pos) {
1782 GST_INFO_OBJECT (queue,
1783 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1784 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1785 queue->current->writing_pos, queue->current->rb_writing_pos);
1786 /* either not using ring buffer or no wrapping, just write */
1787 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1788 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1791 memcpy (ring_buffer + writing_pos, data, to_write);
1794 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1795 /* try to merge with next range */
1796 while ((next = queue->current->next)) {
1797 GST_INFO_OBJECT (queue,
1798 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1799 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1800 if (new_writing_pos < next->offset)
1803 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1806 /* remove the group */
1807 queue->current->next = next->next;
1809 /* We use the threshold to decide if we want to do a seek or simply
1810 * read the data again. If there is not so much data in the range we
1811 * prefer to avoid to seek and read it again. */
1812 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1813 /* the new range had more data than the threshold, it's worth keeping
1814 * it and doing a seek. */
1815 new_writing_pos = next->writing_pos;
1818 g_slice_free (GstQueue2Range, next);
1820 goto update_and_signal;
1824 guint block_one, block_two;
1826 block_one = rb_size - writing_pos;
1827 block_two = to_write - block_one;
1829 if (block_one > 0) {
1830 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1831 /* write data to end of ring buffer */
1832 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1833 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1836 memcpy (ring_buffer + writing_pos, data, block_one);
1840 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1843 if (block_two > 0) {
1844 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1845 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1846 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1849 memcpy (ring_buffer, data + block_one, block_two);
1855 /* update the writing positions */
1857 GST_INFO_OBJECT (queue,
1858 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1859 to_write, writing_pos, size);
1861 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1863 queue->current->writing_pos += to_write;
1864 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1866 queue->current->writing_pos = writing_pos = new_writing_pos;
1869 perform_seek_to_offset (queue, new_writing_pos);
1871 update_cur_level (queue, queue->current);
1873 /* update the buffering status */
1874 if (queue->use_buffering)
1875 update_buffering (queue);
1877 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1878 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1880 GST_QUEUE2_SIGNAL_ADD (queue);
1883 gst_buffer_unmap (buffer, &info);
1890 GST_DEBUG_OBJECT (queue, "we are flushing");
1891 gst_buffer_unmap (buffer, &info);
1892 /* FIXME - GST_FLOW_EOS ? */
1897 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1898 gst_buffer_unmap (buffer, &info);
1905 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1909 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1910 (_("Error while writing to download file.")),
1911 ("%s", g_strerror (errno)));
1914 gst_buffer_unmap (buffer, &info);
1920 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1922 GstQueue2 *queue = q;
1924 GST_TRACE_OBJECT (queue,
1925 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1926 gst_buffer_get_size (*buf));
1928 if (!gst_queue2_create_write (queue, *buf)) {
1929 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1936 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1938 guint *p_size = data;
1941 buf_size = gst_buffer_get_size (*buf);
1942 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1943 *p_size += buf_size;
1947 /* enqueue an item an update the level stats */
1949 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1950 GstQueue2ItemType item_type)
1952 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1956 buffer = GST_BUFFER_CAST (item);
1957 size = gst_buffer_get_size (buffer);
1959 /* add buffer to the statistics */
1960 if (QUEUE_IS_USING_QUEUE (queue)) {
1961 queue->cur_level.buffers++;
1962 queue->cur_level.bytes += size;
1964 queue->bytes_in += size;
1966 /* apply new buffer to segment stats */
1967 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1968 /* update the byterate stats */
1969 update_in_rates (queue);
1971 if (!QUEUE_IS_USING_QUEUE (queue)) {
1972 /* FIXME - check return value? */
1973 gst_queue2_create_write (queue, buffer);
1975 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1976 GstBufferList *buffer_list;
1979 buffer_list = GST_BUFFER_LIST_CAST (item);
1981 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1982 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1984 /* add buffer to the statistics */
1985 if (QUEUE_IS_USING_QUEUE (queue)) {
1986 queue->cur_level.buffers++;
1987 queue->cur_level.bytes += size;
1989 queue->bytes_in += size;
1991 /* apply new buffer to segment stats */
1992 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1994 /* update the byterate stats */
1995 update_in_rates (queue);
1997 if (!QUEUE_IS_USING_QUEUE (queue)) {
1998 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2000 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2003 event = GST_EVENT_CAST (item);
2005 switch (GST_EVENT_TYPE (event)) {
2007 /* Zero the thresholds, this makes sure the queue is completely
2008 * filled and we can read all data from the queue. */
2009 GST_DEBUG_OBJECT (queue, "we have EOS");
2010 queue->is_eos = TRUE;
2012 case GST_EVENT_SEGMENT:
2013 apply_segment (queue, event, &queue->sink_segment, TRUE);
2014 /* This is our first new segment, we hold it
2015 * as we can't save it on the temp file */
2016 if (!QUEUE_IS_USING_QUEUE (queue)) {
2017 if (queue->segment_event_received)
2018 goto unexpected_event;
2020 queue->segment_event_received = TRUE;
2021 if (queue->starting_segment != NULL)
2022 gst_event_unref (queue->starting_segment);
2023 queue->starting_segment = event;
2026 /* a new segment allows us to accept more buffers if we got EOS
2027 * from downstream */
2028 queue->unexpected = FALSE;
2030 case GST_EVENT_STREAM_START:
2031 if (!QUEUE_IS_USING_QUEUE (queue)) {
2032 gst_event_replace (&queue->stream_start_event, event);
2033 gst_event_unref (event);
2037 case GST_EVENT_CAPS:{
2040 gst_event_parse_caps (event, &caps);
2041 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2043 if (!QUEUE_IS_USING_QUEUE (queue)) {
2044 GST_LOG ("Dropping caps event, not using queue");
2045 gst_event_unref (event);
2051 if (!QUEUE_IS_USING_QUEUE (queue))
2052 goto unexpected_event;
2055 } else if (GST_IS_QUERY (item)) {
2056 /* Can't happen as we check that in the caller */
2057 if (!QUEUE_IS_USING_QUEUE (queue))
2058 g_assert_not_reached ();
2060 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2061 item, GST_OBJECT_NAME (queue));
2062 /* we can't really unref since we don't know what it is */
2067 /* update the buffering status */
2068 if (queue->use_buffering)
2069 update_buffering (queue);
2071 if (QUEUE_IS_USING_QUEUE (queue)) {
2072 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2073 qitem->type = item_type;
2075 g_queue_push_tail (&queue->queue, qitem);
2077 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2080 GST_QUEUE2_SIGNAL_ADD (queue);
2089 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2090 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2091 GST_OBJECT_NAME (queue));
2092 gst_event_unref (GST_EVENT_CAST (item));
2097 /* dequeue an item from the queue and update level stats */
2098 static GstMiniObject *
2099 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2101 GstMiniObject *item;
2103 if (!QUEUE_IS_USING_QUEUE (queue)) {
2104 item = gst_queue2_read_item_from_file (queue);
2106 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2112 g_slice_free (GstQueue2Item, qitem);
2118 if (GST_IS_BUFFER (item)) {
2122 buffer = GST_BUFFER_CAST (item);
2123 size = gst_buffer_get_size (buffer);
2124 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2126 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2127 "retrieved buffer %p from queue", buffer);
2129 if (QUEUE_IS_USING_QUEUE (queue)) {
2130 queue->cur_level.buffers--;
2131 queue->cur_level.bytes -= size;
2133 queue->bytes_out += size;
2135 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2136 /* update the byterate stats */
2137 update_out_rates (queue);
2138 /* update the buffering */
2139 if (queue->use_buffering)
2140 update_buffering (queue);
2142 } else if (GST_IS_EVENT (item)) {
2143 GstEvent *event = GST_EVENT_CAST (item);
2145 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2147 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2148 "retrieved event %p from queue", event);
2150 switch (GST_EVENT_TYPE (event)) {
2152 /* queue is empty now that we dequeued the EOS */
2153 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2155 case GST_EVENT_SEGMENT:
2156 apply_segment (queue, event, &queue->src_segment, FALSE);
2161 } else if (GST_IS_BUFFER_LIST (item)) {
2162 GstBufferList *buffer_list;
2165 buffer_list = GST_BUFFER_LIST_CAST (item);
2166 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2167 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2169 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2170 "retrieved buffer list %p from queue", buffer_list);
2172 if (QUEUE_IS_USING_QUEUE (queue)) {
2173 queue->cur_level.buffers--;
2174 queue->cur_level.bytes -= size;
2176 queue->bytes_out += size;
2178 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2179 /* update the byterate stats */
2180 update_out_rates (queue);
2181 /* update the buffering */
2182 if (queue->use_buffering)
2183 update_buffering (queue);
2184 } else if (GST_IS_QUERY (item)) {
2185 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2186 "retrieved query %p from queue", item);
2187 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2190 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2191 item, GST_OBJECT_NAME (queue));
2193 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2195 GST_QUEUE2_SIGNAL_DEL (queue);
2202 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2208 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2211 gboolean ret = TRUE;
2214 queue = GST_QUEUE2 (parent);
2216 switch (GST_EVENT_TYPE (event)) {
2217 case GST_EVENT_FLUSH_START:
2219 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2220 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2222 ret = gst_pad_push_event (queue->srcpad, event);
2224 /* now unblock the chain function */
2225 GST_QUEUE2_MUTEX_LOCK (queue);
2226 queue->srcresult = GST_FLOW_FLUSHING;
2227 queue->sinkresult = GST_FLOW_FLUSHING;
2228 /* unblock the loop and chain functions */
2229 GST_QUEUE2_SIGNAL_ADD (queue);
2230 GST_QUEUE2_SIGNAL_DEL (queue);
2231 queue->last_query = FALSE;
2232 g_cond_signal (&queue->query_handled);
2233 GST_QUEUE2_MUTEX_UNLOCK (queue);
2235 /* make sure it pauses, this should happen since we sent
2236 * flush_start downstream. */
2237 gst_pad_pause_task (queue->srcpad);
2238 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2240 GST_QUEUE2_MUTEX_LOCK (queue);
2241 /* flush the sink pad */
2242 queue->sinkresult = GST_FLOW_FLUSHING;
2243 GST_QUEUE2_SIGNAL_DEL (queue);
2244 queue->last_query = FALSE;
2245 g_cond_signal (&queue->query_handled);
2246 GST_QUEUE2_MUTEX_UNLOCK (queue);
2248 gst_event_unref (event);
2252 case GST_EVENT_FLUSH_STOP:
2254 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2256 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2258 ret = gst_pad_push_event (queue->srcpad, event);
2260 GST_QUEUE2_MUTEX_LOCK (queue);
2261 gst_queue2_locked_flush (queue, FALSE, TRUE);
2262 queue->srcresult = GST_FLOW_OK;
2263 queue->sinkresult = GST_FLOW_OK;
2264 queue->is_eos = FALSE;
2265 queue->unexpected = FALSE;
2266 queue->seeking = FALSE;
2267 /* reset rate counters */
2268 reset_rate_timer (queue);
2269 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2270 queue->srcpad, NULL);
2271 GST_QUEUE2_MUTEX_UNLOCK (queue);
2273 GST_QUEUE2_MUTEX_LOCK (queue);
2274 queue->segment_event_received = FALSE;
2275 queue->is_eos = FALSE;
2276 queue->unexpected = FALSE;
2277 queue->sinkresult = GST_FLOW_OK;
2278 queue->seeking = FALSE;
2279 GST_QUEUE2_MUTEX_UNLOCK (queue);
2281 gst_event_unref (event);
2286 if (GST_EVENT_IS_SERIALIZED (event)) {
2287 /* serialized events go in the queue */
2288 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2289 /* refuse more events on EOS */
2292 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2293 GST_QUEUE2_MUTEX_UNLOCK (queue);
2295 /* non-serialized events are passed upstream. */
2296 ret = gst_pad_push_event (queue->srcpad, event);
2305 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2306 GST_QUEUE2_MUTEX_UNLOCK (queue);
2307 gst_event_unref (event);
2312 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2313 GST_QUEUE2_MUTEX_UNLOCK (queue);
2314 gst_event_unref (event);
2320 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2326 queue = GST_QUEUE2 (parent);
2328 switch (GST_QUERY_TYPE (query)) {
2330 if (GST_QUERY_IS_SERIALIZED (query)) {
2331 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2332 /* serialized events go in the queue. We need to be certain that we
2333 * don't cause deadlocks waiting for the query return value. We check if
2334 * the queue is empty (nothing is blocking downstream and the query can
2335 * be pushed for sure) or we are not buffering. If we are buffering,
2336 * the pipeline waits to unblock downstream until our queue fills up
2337 * completely, which can not happen if we block on the query..
2338 * Therefore we only potentially block when we are not buffering. */
2339 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2340 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2341 || !queue->use_buffering)) {
2342 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2343 gst_queue2_locked_enqueue (queue, query,
2344 GST_QUEUE2_ITEM_TYPE_QUERY);
2346 STATUS (queue, queue->sinkpad, "wait for QUERY");
2347 g_cond_wait (&queue->query_handled, &queue->qlock);
2348 if (queue->sinkresult != GST_FLOW_OK)
2350 res = queue->last_query;
2352 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2356 GST_DEBUG_OBJECT (queue,
2357 "refusing query, we are not using the queue");
2360 GST_QUEUE2_MUTEX_UNLOCK (queue);
2362 res = gst_pad_query_default (pad, parent, query);
2371 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2372 GST_QUEUE2_MUTEX_UNLOCK (queue);
2378 gst_queue2_is_empty (GstQueue2 * queue)
2380 /* never empty on EOS */
2384 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2385 return queue->current->writing_pos <= queue->current->max_reading_pos;
2387 if (queue->queue.length == 0)
2395 gst_queue2_is_filled (GstQueue2 * queue)
2399 /* always filled on EOS */
2403 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2404 (queue->cur_level.format) >= ((alt_max) ? \
2405 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2407 /* if using a ring buffer we're filled if all ring buffer space is used
2408 * _by the current range_ */
2409 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2410 guint64 rb_size = queue->ring_buffer_max_size;
2411 GST_DEBUG_OBJECT (queue,
2412 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2413 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2414 return CHECK_FILLED (bytes, rb_size);
2417 /* if using file, we're never filled if we don't have EOS */
2418 if (QUEUE_IS_USING_TEMP_FILE (queue))
2421 /* we are never filled when we have no buffers at all */
2422 if (queue->cur_level.buffers == 0)
2425 /* we are filled if one of the current levels exceeds the max */
2426 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2427 || CHECK_FILLED (time, 0);
2429 /* if we need to, use the rate estimate to check against the max time we are
2430 * allowed to queue */
2431 if (queue->use_rate_estimate)
2432 res |= CHECK_FILLED (rate_time, 0);
2438 static GstFlowReturn
2439 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2440 GstMiniObject * item, GstQueue2ItemType item_type)
2442 /* we have to lock the queue since we span threads */
2443 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2444 /* when we received EOS, we refuse more data */
2447 /* when we received unexpected from downstream, refuse more buffers */
2448 if (queue->unexpected)
2449 goto out_unexpected;
2451 /* while we didn't receive the newsegment, we're seeking and we skip data */
2455 if (!gst_queue2_wait_free_space (queue))
2458 /* put buffer in queue now */
2459 gst_queue2_locked_enqueue (queue, item, item_type);
2460 GST_QUEUE2_MUTEX_UNLOCK (queue);
2464 /* special conditions */
2467 GstFlowReturn ret = queue->sinkresult;
2469 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2470 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2471 GST_QUEUE2_MUTEX_UNLOCK (queue);
2472 gst_mini_object_unref (item);
2478 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2479 GST_QUEUE2_MUTEX_UNLOCK (queue);
2480 gst_mini_object_unref (item);
2482 return GST_FLOW_EOS;
2486 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2487 GST_QUEUE2_MUTEX_UNLOCK (queue);
2488 gst_mini_object_unref (item);
2494 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2495 GST_QUEUE2_MUTEX_UNLOCK (queue);
2496 gst_mini_object_unref (item);
2498 return GST_FLOW_EOS;
2502 static GstFlowReturn
2503 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2507 queue = GST_QUEUE2 (parent);
2509 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2510 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2511 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2512 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2513 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2515 return gst_queue2_chain_buffer_or_buffer_list (queue,
2516 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2519 static GstFlowReturn
2520 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2521 GstBufferList * buffer_list)
2525 queue = GST_QUEUE2 (parent);
2527 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2528 "received buffer list %p", buffer_list);
2530 return gst_queue2_chain_buffer_or_buffer_list (queue,
2531 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2534 static GstMiniObject *
2535 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2537 GstMiniObject *data;
2539 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2541 /* stop pushing buffers, we dequeue all items until we see an item that we
2542 * can push again, which is EOS or SEGMENT. If there is nothing in the
2543 * queue we can push, we set a flag to make the sinkpad refuse more
2544 * buffers with an EOS return value until we receive something
2545 * pushable again or we get flushed. */
2546 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2547 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2548 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2549 "dropping EOS buffer %p", data);
2550 gst_buffer_unref (GST_BUFFER_CAST (data));
2551 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2552 GstEvent *event = GST_EVENT_CAST (data);
2553 GstEventType type = GST_EVENT_TYPE (event);
2555 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2556 /* we found a pushable item in the queue, push it out */
2557 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2558 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2561 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2562 "dropping EOS event %p", event);
2563 gst_event_unref (event);
2564 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2565 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2566 "dropping EOS buffer list %p", data);
2567 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2568 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2569 queue->last_query = FALSE;
2570 g_cond_signal (&queue->query_handled);
2571 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2574 /* no more items in the queue. Set the unexpected flag so that upstream
2575 * make us refuse any more buffers on the sinkpad. Since we will still
2576 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2577 * task function does not shut down. */
2578 queue->unexpected = TRUE;
2582 /* dequeue an item from the queue an push it downstream. This functions returns
2583 * the result of the push. */
2584 static GstFlowReturn
2585 gst_queue2_push_one (GstQueue2 * queue)
2587 GstFlowReturn result = queue->srcresult;
2588 GstMiniObject *data;
2589 GstQueue2ItemType item_type;
2591 data = gst_queue2_locked_dequeue (queue, &item_type);
2596 STATUS (queue, queue->srcpad, "We have something dequeud");
2597 g_atomic_int_set (&queue->downstream_may_block,
2598 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2599 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2600 GST_QUEUE2_MUTEX_UNLOCK (queue);
2602 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2605 buffer = GST_BUFFER_CAST (data);
2607 result = gst_pad_push (queue->srcpad, buffer);
2608 g_atomic_int_set (&queue->downstream_may_block, 0);
2610 /* need to check for srcresult here as well */
2611 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2612 if (result == GST_FLOW_EOS) {
2613 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2616 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2617 * to the caller so that the task function does not shut down */
2618 result = GST_FLOW_OK;
2620 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2621 GstEvent *event = GST_EVENT_CAST (data);
2622 GstEventType type = GST_EVENT_TYPE (event);
2624 gst_pad_push_event (queue->srcpad, event);
2626 /* if we're EOS, return EOS so that the task pauses. */
2627 if (type == GST_EVENT_EOS) {
2628 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2629 "pushed EOS event %p, return EOS", event);
2630 result = GST_FLOW_EOS;
2633 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2634 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2635 GstBufferList *buffer_list;
2637 buffer_list = GST_BUFFER_LIST_CAST (data);
2639 result = gst_pad_push_list (queue->srcpad, buffer_list);
2640 g_atomic_int_set (&queue->downstream_may_block, 0);
2642 /* need to check for srcresult here as well */
2643 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2644 if (result == GST_FLOW_EOS) {
2645 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2648 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2649 * to the caller so that the task function does not shut down */
2650 result = GST_FLOW_OK;
2652 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2653 GstQuery *query = GST_QUERY_CAST (data);
2655 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2656 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2657 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2658 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2659 "did query %p, return %d", query, queue->last_query);
2660 g_cond_signal (&queue->query_handled);
2661 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2662 result = GST_FLOW_OK;
2669 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2670 "exit because we have no item in the queue");
2671 return GST_FLOW_ERROR;
2675 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2676 return GST_FLOW_FLUSHING;
2680 /* called repeatedly with @pad as the source pad. This function should push out
2681 * data to the peer element. */
2683 gst_queue2_loop (GstPad * pad)
2688 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2690 /* have to lock for thread-safety */
2691 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2693 if (gst_queue2_is_empty (queue)) {
2696 /* pause the timer while we wait. The fact that we are waiting does not mean
2697 * the byterate on the output pad is lower */
2698 if ((started = queue->out_timer_started))
2699 g_timer_stop (queue->out_timer);
2701 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2702 "queue is empty, waiting for new data");
2704 /* Wait for data to be available, we could be unlocked because of a flush. */
2705 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2707 while (gst_queue2_is_empty (queue));
2709 /* and continue if we were running before */
2711 g_timer_continue (queue->out_timer);
2713 ret = gst_queue2_push_one (queue);
2714 queue->srcresult = ret;
2715 queue->sinkresult = ret;
2716 if (ret != GST_FLOW_OK)
2719 GST_QUEUE2_MUTEX_UNLOCK (queue);
2726 gboolean eos = queue->is_eos;
2727 GstFlowReturn ret = queue->srcresult;
2729 gst_pad_pause_task (queue->srcpad);
2730 GST_QUEUE2_MUTEX_UNLOCK (queue);
2731 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2732 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2733 /* let app know about us giving up if upstream is not expected to do so */
2734 /* EOS is already taken care of elsewhere */
2735 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2736 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2737 (_("Internal data flow error.")),
2738 ("streaming task paused, reason %s (%d)",
2739 gst_flow_get_name (ret), ret));
2740 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2747 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2749 gboolean res = TRUE;
2750 GstQueue2 *queue = GST_QUEUE2 (parent);
2752 #ifndef GST_DISABLE_GST_DEBUG
2753 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2754 event, GST_EVENT_TYPE_NAME (event));
2757 switch (GST_EVENT_TYPE (event)) {
2758 case GST_EVENT_FLUSH_START:
2759 if (QUEUE_IS_USING_QUEUE (queue)) {
2760 /* just forward upstream */
2761 res = gst_pad_push_event (queue->sinkpad, event);
2763 /* now unblock the getrange function */
2764 GST_QUEUE2_MUTEX_LOCK (queue);
2765 GST_DEBUG_OBJECT (queue, "flushing");
2766 queue->srcresult = GST_FLOW_FLUSHING;
2767 GST_QUEUE2_SIGNAL_ADD (queue);
2768 GST_QUEUE2_MUTEX_UNLOCK (queue);
2770 /* when using a temp file, we eat the event */
2772 gst_event_unref (event);
2775 case GST_EVENT_FLUSH_STOP:
2776 if (QUEUE_IS_USING_QUEUE (queue)) {
2777 /* just forward upstream */
2778 res = gst_pad_push_event (queue->sinkpad, event);
2780 /* now unblock the getrange function */
2781 GST_QUEUE2_MUTEX_LOCK (queue);
2782 queue->srcresult = GST_FLOW_OK;
2783 GST_QUEUE2_MUTEX_UNLOCK (queue);
2785 /* when using a temp file, we eat the event */
2787 gst_event_unref (event);
2790 case GST_EVENT_RECONFIGURE:
2791 GST_QUEUE2_MUTEX_LOCK (queue);
2792 /* assume downstream is linked now and try to push again */
2793 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2794 queue->srcresult = GST_FLOW_OK;
2795 queue->sinkresult = GST_FLOW_OK;
2796 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2797 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2801 GST_QUEUE2_MUTEX_UNLOCK (queue);
2803 res = gst_pad_push_event (queue->sinkpad, event);
2806 res = gst_pad_push_event (queue->sinkpad, event);
2814 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2818 queue = GST_QUEUE2 (parent);
2820 switch (GST_QUERY_TYPE (query)) {
2821 case GST_QUERY_POSITION:
2826 if (!gst_pad_peer_query (queue->sinkpad, query))
2829 /* get peer position */
2830 gst_query_parse_position (query, &format, &peer_pos);
2832 /* FIXME: this code assumes that there's no discont in the queue */
2834 case GST_FORMAT_BYTES:
2835 peer_pos -= queue->cur_level.bytes;
2837 case GST_FORMAT_TIME:
2838 peer_pos -= queue->cur_level.time;
2841 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2842 "know how to adjust value", gst_format_get_name (format));
2845 /* set updated position */
2846 gst_query_set_position (query, format, peer_pos);
2849 case GST_QUERY_DURATION:
2851 GST_DEBUG_OBJECT (queue, "doing peer query");
2853 if (!gst_pad_peer_query (queue->sinkpad, query))
2856 GST_DEBUG_OBJECT (queue, "peer query success");
2859 case GST_QUERY_BUFFERING:
2862 gboolean is_buffering;
2863 GstBufferingMode mode;
2864 gint avg_in, avg_out;
2865 gint64 buffering_left;
2867 GST_DEBUG_OBJECT (queue, "query buffering");
2869 get_buffering_percent (queue, &is_buffering, &percent);
2870 gst_query_set_buffering_percent (query, is_buffering, percent);
2872 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
2874 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
2877 if (!QUEUE_IS_USING_QUEUE (queue)) {
2878 /* add ranges for download and ringbuffer buffering */
2880 gint64 start, stop, range_start, range_stop;
2881 guint64 writing_pos;
2882 gint64 estimated_total;
2884 gboolean peer_res, is_eos;
2885 GstQueue2Range *queued_ranges;
2887 /* we need a current download region */
2888 if (queue->current == NULL)
2891 writing_pos = queue->current->writing_pos;
2892 is_eos = queue->is_eos;
2895 /* we're EOS, we know the duration in bytes now */
2897 duration = writing_pos;
2899 /* get duration of upstream in bytes */
2900 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2901 GST_FORMAT_BYTES, &duration);
2904 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
2905 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
2907 /* calculate remaining and total download time */
2908 if (peer_res && avg_in > 0.0)
2909 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
2911 estimated_total = -1;
2913 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
2916 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2919 case GST_FORMAT_PERCENT:
2920 /* we need duration */
2925 /* get our available data relative to the duration */
2928 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
2933 case GST_FORMAT_BYTES:
2943 /* fill out the buffered ranges */
2944 for (queued_ranges = queue->ranges; queued_ranges;
2945 queued_ranges = queued_ranges->next) {
2947 case GST_FORMAT_PERCENT:
2948 if (duration == -1) {
2954 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2955 queued_ranges->offset, duration);
2957 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2958 queued_ranges->writing_pos, duration);
2960 case GST_FORMAT_BYTES:
2961 range_start = queued_ranges->offset;
2962 range_stop = queued_ranges->writing_pos;
2969 if (range_start == range_stop)
2971 GST_DEBUG_OBJECT (queue,
2972 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2973 G_GINT64_FORMAT, range_start, range_stop);
2974 gst_query_add_buffering_range (query, range_start, range_stop);
2977 gst_query_set_buffering_range (query, format, start, stop,
2982 case GST_QUERY_SCHEDULING:
2985 GstSchedulingFlags flags = 0;
2987 if (!gst_pad_peer_query (queue->sinkpad, query))
2990 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
2992 /* we can operate in pull mode when we are using a tempfile */
2993 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2996 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2997 gst_query_set_scheduling (query, flags, 0, -1, 0);
2999 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3000 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3004 /* peer handled other queries */
3005 if (!gst_pad_query_default (pad, parent, query))
3015 GST_DEBUG_OBJECT (queue, "failed peer query");
3021 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3023 GstQueue2 *queue = GST_QUEUE2 (element);
3025 /* simply forward to the srcpad query function */
3026 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3031 gst_queue2_update_upstream_size (GstQueue2 * queue)
3033 gint64 upstream_size = -1;
3035 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3037 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3038 queue->upstream_size = upstream_size;
3042 static GstFlowReturn
3043 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3044 guint length, GstBuffer ** buffer)
3049 queue = GST_QUEUE2_CAST (parent);
3051 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3052 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3053 offset = (offset == -1) ? queue->current->reading_pos : offset;
3055 GST_DEBUG_OBJECT (queue,
3056 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3058 /* catch any reads beyond the size of the file here to make sure queue2
3059 * doesn't send seek events beyond the size of the file upstream, since
3060 * that would confuse elements such as souphttpsrc and/or http servers.
3061 * Demuxers often just loop until EOS at the end of the file to figure out
3062 * when they've read all the end-headers or index chunks. */
3063 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3064 gst_queue2_update_upstream_size (queue);
3065 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3066 goto out_unexpected;
3069 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3070 gst_queue2_update_upstream_size (queue);
3071 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3072 length = queue->upstream_size - offset;
3073 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3077 /* FIXME - function will block when the range is not yet available */
3078 ret = gst_queue2_create_read (queue, offset, length, buffer);
3079 GST_QUEUE2_MUTEX_UNLOCK (queue);
3086 ret = queue->srcresult;
3088 GST_DEBUG_OBJECT (queue, "we are flushing");
3089 GST_QUEUE2_MUTEX_UNLOCK (queue);
3094 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3095 GST_QUEUE2_MUTEX_UNLOCK (queue);
3096 return GST_FLOW_EOS;
3100 /* sink currently only operates in push mode */
3102 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3103 GstPadMode mode, gboolean active)
3108 queue = GST_QUEUE2 (parent);
3111 case GST_PAD_MODE_PUSH:
3113 GST_QUEUE2_MUTEX_LOCK (queue);
3114 GST_DEBUG_OBJECT (queue, "activating push mode");
3115 queue->srcresult = GST_FLOW_OK;
3116 queue->sinkresult = GST_FLOW_OK;
3117 queue->is_eos = FALSE;
3118 queue->unexpected = FALSE;
3119 reset_rate_timer (queue);
3120 GST_QUEUE2_MUTEX_UNLOCK (queue);
3122 /* unblock chain function */
3123 GST_QUEUE2_MUTEX_LOCK (queue);
3124 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3125 queue->srcresult = GST_FLOW_FLUSHING;
3126 queue->sinkresult = GST_FLOW_FLUSHING;
3127 GST_QUEUE2_SIGNAL_DEL (queue);
3128 /* Unblock query handler */
3129 queue->last_query = FALSE;
3130 g_cond_signal (&queue->query_handled);
3131 GST_QUEUE2_MUTEX_UNLOCK (queue);
3133 /* wait until it is unblocked and clean up */
3134 GST_PAD_STREAM_LOCK (pad);
3135 GST_QUEUE2_MUTEX_LOCK (queue);
3136 gst_queue2_locked_flush (queue, TRUE, FALSE);
3137 GST_QUEUE2_MUTEX_UNLOCK (queue);
3138 GST_PAD_STREAM_UNLOCK (pad);
3149 /* src operating in push mode, we start a task on the source pad that pushes out
3150 * buffers from the queue */
3152 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3154 gboolean result = FALSE;
3157 queue = GST_QUEUE2 (parent);
3160 GST_QUEUE2_MUTEX_LOCK (queue);
3161 GST_DEBUG_OBJECT (queue, "activating push mode");
3162 queue->srcresult = GST_FLOW_OK;
3163 queue->sinkresult = GST_FLOW_OK;
3164 queue->is_eos = FALSE;
3165 queue->unexpected = FALSE;
3167 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3168 GST_QUEUE2_MUTEX_UNLOCK (queue);
3170 /* unblock loop function */
3171 GST_QUEUE2_MUTEX_LOCK (queue);
3172 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3173 queue->srcresult = GST_FLOW_FLUSHING;
3174 queue->sinkresult = GST_FLOW_FLUSHING;
3175 /* the item add signal will unblock */
3176 GST_QUEUE2_SIGNAL_ADD (queue);
3177 GST_QUEUE2_MUTEX_UNLOCK (queue);
3179 /* step 2, make sure streaming finishes */
3180 result = gst_pad_stop_task (pad);
3186 /* pull mode, downstream will call our getrange function */
3188 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3193 queue = GST_QUEUE2 (parent);
3196 GST_QUEUE2_MUTEX_LOCK (queue);
3197 if (!QUEUE_IS_USING_QUEUE (queue)) {
3198 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3199 /* open the temp file now */
3200 result = gst_queue2_open_temp_location_file (queue);
3201 } else if (!queue->ring_buffer) {
3202 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3203 result = ! !queue->ring_buffer;
3208 GST_DEBUG_OBJECT (queue, "activating pull mode");
3209 init_ranges (queue);
3210 queue->srcresult = GST_FLOW_OK;
3211 queue->sinkresult = GST_FLOW_OK;
3212 queue->is_eos = FALSE;
3213 queue->unexpected = FALSE;
3214 queue->upstream_size = 0;
3216 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3217 /* this is not allowed, we cannot operate in pull mode without a temp
3219 queue->srcresult = GST_FLOW_FLUSHING;
3220 queue->sinkresult = GST_FLOW_FLUSHING;
3223 GST_QUEUE2_MUTEX_UNLOCK (queue);
3225 GST_QUEUE2_MUTEX_LOCK (queue);
3226 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3227 queue->srcresult = GST_FLOW_FLUSHING;
3228 queue->sinkresult = GST_FLOW_FLUSHING;
3229 /* this will unlock getrange */
3230 GST_QUEUE2_SIGNAL_ADD (queue);
3232 GST_QUEUE2_MUTEX_UNLOCK (queue);
3239 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3245 case GST_PAD_MODE_PULL:
3246 res = gst_queue2_src_activate_pull (pad, parent, active);
3248 case GST_PAD_MODE_PUSH:
3249 res = gst_queue2_src_activate_push (pad, parent, active);
3252 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3259 static GstStateChangeReturn
3260 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3263 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3265 queue = GST_QUEUE2 (element);
3267 switch (transition) {
3268 case GST_STATE_CHANGE_NULL_TO_READY:
3270 case GST_STATE_CHANGE_READY_TO_PAUSED:
3271 GST_QUEUE2_MUTEX_LOCK (queue);
3272 if (!QUEUE_IS_USING_QUEUE (queue)) {
3273 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3274 if (!gst_queue2_open_temp_location_file (queue))
3275 ret = GST_STATE_CHANGE_FAILURE;
3277 if (queue->ring_buffer) {
3278 g_free (queue->ring_buffer);
3279 queue->ring_buffer = NULL;
3281 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3282 ret = GST_STATE_CHANGE_FAILURE;
3284 init_ranges (queue);
3286 queue->segment_event_received = FALSE;
3287 queue->starting_segment = NULL;
3288 gst_event_replace (&queue->stream_start_event, NULL);
3289 GST_QUEUE2_MUTEX_UNLOCK (queue);
3291 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3297 if (ret == GST_STATE_CHANGE_FAILURE)
3300 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3302 if (ret == GST_STATE_CHANGE_FAILURE)
3305 switch (transition) {
3306 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3308 case GST_STATE_CHANGE_PAUSED_TO_READY:
3309 GST_QUEUE2_MUTEX_LOCK (queue);
3310 if (!QUEUE_IS_USING_QUEUE (queue)) {
3311 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3312 gst_queue2_close_temp_location_file (queue);
3313 } else if (queue->ring_buffer) {
3314 g_free (queue->ring_buffer);
3315 queue->ring_buffer = NULL;
3317 clean_ranges (queue);
3319 if (queue->starting_segment != NULL) {
3320 gst_event_unref (queue->starting_segment);
3321 queue->starting_segment = NULL;
3323 gst_event_replace (&queue->stream_start_event, NULL);
3324 GST_QUEUE2_MUTEX_UNLOCK (queue);
3326 case GST_STATE_CHANGE_READY_TO_NULL:
3335 /* changing the capacity of the queue must wake up
3336 * the _chain function, it might have more room now
3337 * to store the buffer/event in the queue */
3338 #define QUEUE_CAPACITY_CHANGE(q) \
3339 GST_QUEUE2_SIGNAL_DEL (queue); \
3340 if (queue->use_buffering) \
3341 update_buffering (queue);
3343 /* Changing the minimum required fill level must
3344 * wake up the _loop function as it might now
3345 * be able to preceed.
3347 #define QUEUE_THRESHOLD_CHANGE(q)\
3348 GST_QUEUE2_SIGNAL_ADD (queue);
3351 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3355 /* the element must be stopped in order to do this */
3356 GST_OBJECT_LOCK (queue);
3357 state = GST_STATE (queue);
3358 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3360 GST_OBJECT_UNLOCK (queue);
3362 /* set new location */
3363 g_free (queue->temp_template);
3364 queue->temp_template = g_strdup (template);
3371 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3372 GST_OBJECT_UNLOCK (queue);
3377 gst_queue2_set_property (GObject * object,
3378 guint prop_id, const GValue * value, GParamSpec * pspec)
3380 GstQueue2 *queue = GST_QUEUE2 (object);
3382 /* someone could change levels here, and since this
3383 * affects the get/put funcs, we need to lock for safety. */
3384 GST_QUEUE2_MUTEX_LOCK (queue);
3387 case PROP_MAX_SIZE_BYTES:
3388 queue->max_level.bytes = g_value_get_uint (value);
3389 QUEUE_CAPACITY_CHANGE (queue);
3391 case PROP_MAX_SIZE_BUFFERS:
3392 queue->max_level.buffers = g_value_get_uint (value);
3393 QUEUE_CAPACITY_CHANGE (queue);
3395 case PROP_MAX_SIZE_TIME:
3396 queue->max_level.time = g_value_get_uint64 (value);
3397 /* set rate_time to the same value. We use an extra field in the level
3398 * structure so that we can easily access and compare it */
3399 queue->max_level.rate_time = queue->max_level.time;
3400 QUEUE_CAPACITY_CHANGE (queue);
3402 case PROP_USE_BUFFERING:
3403 queue->use_buffering = g_value_get_boolean (value);
3404 if (!queue->use_buffering && queue->is_buffering) {
3405 GstMessage *msg = gst_message_new_buffering (GST_OBJECT_CAST (queue),
3408 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3409 "posting 100%% message");
3410 queue->is_buffering = FALSE;
3411 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
3414 if (queue->use_buffering) {
3415 queue->is_buffering = TRUE;
3416 update_buffering (queue);
3419 case PROP_USE_RATE_ESTIMATE:
3420 queue->use_rate_estimate = g_value_get_boolean (value);
3422 case PROP_LOW_PERCENT:
3423 queue->low_percent = g_value_get_int (value);
3425 case PROP_HIGH_PERCENT:
3426 queue->high_percent = g_value_get_int (value);
3428 case PROP_TEMP_TEMPLATE:
3429 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3431 case PROP_TEMP_REMOVE:
3432 queue->temp_remove = g_value_get_boolean (value);
3434 case PROP_RING_BUFFER_MAX_SIZE:
3435 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3438 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3442 GST_QUEUE2_MUTEX_UNLOCK (queue);
3446 gst_queue2_get_property (GObject * object,
3447 guint prop_id, GValue * value, GParamSpec * pspec)
3449 GstQueue2 *queue = GST_QUEUE2 (object);
3451 GST_QUEUE2_MUTEX_LOCK (queue);
3454 case PROP_CUR_LEVEL_BYTES:
3455 g_value_set_uint (value, queue->cur_level.bytes);
3457 case PROP_CUR_LEVEL_BUFFERS:
3458 g_value_set_uint (value, queue->cur_level.buffers);
3460 case PROP_CUR_LEVEL_TIME:
3461 g_value_set_uint64 (value, queue->cur_level.time);
3463 case PROP_MAX_SIZE_BYTES:
3464 g_value_set_uint (value, queue->max_level.bytes);
3466 case PROP_MAX_SIZE_BUFFERS:
3467 g_value_set_uint (value, queue->max_level.buffers);
3469 case PROP_MAX_SIZE_TIME:
3470 g_value_set_uint64 (value, queue->max_level.time);
3472 case PROP_USE_BUFFERING:
3473 g_value_set_boolean (value, queue->use_buffering);
3475 case PROP_USE_RATE_ESTIMATE:
3476 g_value_set_boolean (value, queue->use_rate_estimate);
3478 case PROP_LOW_PERCENT:
3479 g_value_set_int (value, queue->low_percent);
3481 case PROP_HIGH_PERCENT:
3482 g_value_set_int (value, queue->high_percent);
3484 case PROP_TEMP_TEMPLATE:
3485 g_value_set_string (value, queue->temp_template);
3487 case PROP_TEMP_LOCATION:
3488 g_value_set_string (value, queue->temp_location);
3490 case PROP_TEMP_REMOVE:
3491 g_value_set_boolean (value, queue->temp_remove);
3493 case PROP_RING_BUFFER_MAX_SIZE:
3494 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3501 GST_QUEUE2_MUTEX_UNLOCK (queue);