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
52 * Last reviewed on 2009-07-10 (0.10.24)
59 #include "gstqueue2.h"
61 #include <glib/gstdio.h>
63 #include "gst/gst-i18n-lib.h"
64 #include "gst/glib-compat-private.h"
69 #include <io.h> /* lseek, open, close, read */
71 #define lseek _lseeki64
78 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
83 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
88 GST_DEBUG_CATEGORY_STATIC (queue_debug);
89 #define GST_CAT_DEFAULT (queue_debug)
90 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
98 #define DEFAULT_BUFFER_SIZE 4096
99 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
100 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
101 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
103 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
105 /* default property values */
106 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
107 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
108 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
109 #define DEFAULT_USE_BUFFERING FALSE
110 #define DEFAULT_USE_RATE_ESTIMATE TRUE
111 #define DEFAULT_LOW_PERCENT 10
112 #define DEFAULT_HIGH_PERCENT 99
113 #define DEFAULT_TEMP_REMOVE TRUE
114 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
119 PROP_CUR_LEVEL_BUFFERS,
120 PROP_CUR_LEVEL_BYTES,
122 PROP_MAX_SIZE_BUFFERS,
126 PROP_USE_RATE_ESTIMATE,
132 PROP_RING_BUFFER_MAX_SIZE,
136 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
143 #define STATUS(queue, pad, msg) \
144 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
145 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
146 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
147 " ns, %"G_GUINT64_FORMAT" items", \
148 GST_DEBUG_PAD_NAME (pad), \
149 queue->cur_level.buffers, \
150 queue->max_level.buffers, \
151 queue->cur_level.bytes, \
152 queue->max_level.bytes, \
153 queue->cur_level.time, \
154 queue->max_level.time, \
155 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
156 queue->current->writing_pos - queue->current->max_reading_pos : \
157 queue->queue.length))
159 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
160 g_mutex_lock (&q->qlock); \
163 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
164 GST_QUEUE2_MUTEX_LOCK (q); \
165 if (res != GST_FLOW_OK) \
169 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
170 g_mutex_unlock (&q->qlock); \
173 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
174 STATUS (queue, q->sinkpad, "wait for DEL"); \
175 q->waiting_del = TRUE; \
176 g_cond_wait (&q->item_del, &queue->qlock); \
177 q->waiting_del = FALSE; \
178 if (res != GST_FLOW_OK) { \
179 STATUS (queue, q->srcpad, "received DEL wakeup"); \
182 STATUS (queue, q->sinkpad, "received DEL"); \
185 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
186 STATUS (queue, q->srcpad, "wait for ADD"); \
187 q->waiting_add = TRUE; \
188 g_cond_wait (&q->item_add, &q->qlock); \
189 q->waiting_add = FALSE; \
190 if (res != GST_FLOW_OK) { \
191 STATUS (queue, q->srcpad, "received ADD wakeup"); \
194 STATUS (queue, q->srcpad, "received ADD"); \
197 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
198 if (q->waiting_del) { \
199 STATUS (q, q->srcpad, "signal DEL"); \
200 g_cond_signal (&q->item_del); \
204 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
205 if (q->waiting_add) { \
206 STATUS (q, q->sinkpad, "signal ADD"); \
207 g_cond_signal (&q->item_add); \
212 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
213 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
214 "dataflow inside the queue element");
215 #define gst_queue2_parent_class parent_class
216 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
218 static void gst_queue2_finalize (GObject * object);
220 static void gst_queue2_set_property (GObject * object,
221 guint prop_id, const GValue * value, GParamSpec * pspec);
222 static void gst_queue2_get_property (GObject * object,
223 guint prop_id, GValue * value, GParamSpec * pspec);
225 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
227 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
228 GstBufferList * buffer_list);
229 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
230 static void gst_queue2_loop (GstPad * pad);
232 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
234 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
237 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
239 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
241 static gboolean gst_queue2_handle_query (GstElement * element,
244 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
245 guint64 offset, guint length, GstBuffer ** buffer);
247 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
248 GstPadMode mode, gboolean active);
249 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
250 GstPadMode mode, gboolean active);
251 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
252 GstStateChange transition);
254 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
255 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
257 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
258 static void update_in_rates (GstQueue2 * queue);
262 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
263 GST_QUEUE2_ITEM_TYPE_BUFFER,
264 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
265 GST_QUEUE2_ITEM_TYPE_EVENT,
266 GST_QUEUE2_ITEM_TYPE_QUERY
271 GstQueue2ItemType type;
275 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
278 gst_queue2_class_init (GstQueue2Class * klass)
280 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
281 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
283 gobject_class->set_property = gst_queue2_set_property;
284 gobject_class->get_property = gst_queue2_get_property;
287 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
288 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
289 "Current amount of data in the queue (bytes)",
290 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
292 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
293 "Current number of buffers in the queue",
294 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
296 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
297 "Current amount of data in the queue (in ns)",
298 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
300 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
301 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
302 "Max. amount of data in the queue (bytes, 0=disable)",
303 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
304 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
306 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
307 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
308 DEFAULT_MAX_SIZE_BUFFERS,
309 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
310 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
311 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
312 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
313 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
316 g_param_spec_boolean ("use-buffering", "Use buffering",
317 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
318 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
320 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
321 "Estimate the bitrate of the stream to calculate time level",
322 DEFAULT_USE_RATE_ESTIMATE,
323 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
324 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
325 g_param_spec_int ("low-percent", "Low percent",
326 "Low threshold for buffering to start. Only used if use-buffering is True",
327 0, 100, DEFAULT_LOW_PERCENT,
328 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
330 g_param_spec_int ("high-percent", "High percent",
331 "High threshold for buffering to finish. Only used if use-buffering is True",
332 0, 100, DEFAULT_HIGH_PERCENT,
333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
336 g_param_spec_string ("temp-template", "Temporary File Template",
337 "File template to store temporary files in, should contain directory "
338 "and XXXXXX. (NULL == disabled)",
339 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
342 g_param_spec_string ("temp-location", "Temporary File Location",
343 "Location to store temporary files in (Only read this property, "
344 "use temp-template to configure the name template)",
345 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
348 * GstQueue2:temp-remove
350 * When temp-template is set, remove the temporary file when going to READY.
352 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
353 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
354 "Remove the temp-location after use",
355 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 * GstQueue2:ring-buffer-max-size
360 * The maximum size of the ring buffer in bytes. If set to 0, the ring
361 * buffer is disabled. Default 0.
363 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
364 g_param_spec_uint64 ("ring-buffer-max-size",
365 "Max. ring buffer size (bytes)",
366 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
367 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 /* set several parent class virtual functions */
371 gobject_class->finalize = gst_queue2_finalize;
373 gst_element_class_add_pad_template (gstelement_class,
374 gst_static_pad_template_get (&srctemplate));
375 gst_element_class_add_pad_template (gstelement_class,
376 gst_static_pad_template_get (&sinktemplate));
378 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
381 "Erik Walthinsen <omega@cse.ogi.edu>, "
382 "Wim Taymans <wim.taymans@gmail.com>");
384 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
385 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
389 gst_queue2_init (GstQueue2 * queue)
391 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
393 gst_pad_set_chain_function (queue->sinkpad,
394 GST_DEBUG_FUNCPTR (gst_queue2_chain));
395 gst_pad_set_chain_list_function (queue->sinkpad,
396 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
397 gst_pad_set_activatemode_function (queue->sinkpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
399 gst_pad_set_event_function (queue->sinkpad,
400 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
401 gst_pad_set_query_function (queue->sinkpad,
402 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
403 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
404 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
406 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
408 gst_pad_set_activatemode_function (queue->srcpad,
409 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
410 gst_pad_set_getrange_function (queue->srcpad,
411 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
412 gst_pad_set_event_function (queue->srcpad,
413 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
414 gst_pad_set_query_function (queue->srcpad,
415 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
416 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
417 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
420 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
421 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
422 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
423 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
424 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
425 queue->use_buffering = DEFAULT_USE_BUFFERING;
426 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
427 queue->low_percent = DEFAULT_LOW_PERCENT;
428 queue->high_percent = DEFAULT_HIGH_PERCENT;
430 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
431 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
433 queue->sinktime = GST_CLOCK_TIME_NONE;
434 queue->srctime = GST_CLOCK_TIME_NONE;
435 queue->sink_tainted = TRUE;
436 queue->src_tainted = TRUE;
438 queue->srcresult = GST_FLOW_FLUSHING;
439 queue->sinkresult = GST_FLOW_FLUSHING;
440 queue->is_eos = FALSE;
441 queue->in_timer = g_timer_new ();
442 queue->out_timer = g_timer_new ();
444 g_mutex_init (&queue->qlock);
445 queue->waiting_add = FALSE;
446 g_cond_init (&queue->item_add);
447 queue->waiting_del = FALSE;
448 g_cond_init (&queue->item_del);
449 g_queue_init (&queue->queue);
451 g_cond_init (&queue->query_handled);
452 queue->last_query = FALSE;
454 queue->buffering_percent = 100;
456 /* tempfile related */
457 queue->temp_template = NULL;
458 queue->temp_location = NULL;
459 queue->temp_remove = DEFAULT_TEMP_REMOVE;
461 queue->ring_buffer = NULL;
462 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
464 GST_DEBUG_OBJECT (queue,
465 "initialized queue's not_empty & not_full conditions");
468 /* called only once, as opposed to dispose */
470 gst_queue2_finalize (GObject * object)
472 GstQueue2 *queue = GST_QUEUE2 (object);
474 GST_DEBUG_OBJECT (queue, "finalizing queue");
476 while (!g_queue_is_empty (&queue->queue)) {
477 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
479 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
480 gst_mini_object_unref (qitem->item);
481 g_slice_free (GstQueue2Item, qitem);
484 queue->last_query = FALSE;
485 g_queue_clear (&queue->queue);
486 g_mutex_clear (&queue->qlock);
487 g_cond_clear (&queue->item_add);
488 g_cond_clear (&queue->item_del);
489 g_cond_clear (&queue->query_handled);
490 g_timer_destroy (queue->in_timer);
491 g_timer_destroy (queue->out_timer);
493 /* temp_file path cleanup */
494 g_free (queue->temp_template);
495 g_free (queue->temp_location);
497 G_OBJECT_CLASS (parent_class)->finalize (object);
501 debug_ranges (GstQueue2 * queue)
503 GstQueue2Range *walk;
505 for (walk = queue->ranges; walk; walk = walk->next) {
506 GST_DEBUG_OBJECT (queue,
507 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
508 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
509 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
510 walk->rb_writing_pos, walk->reading_pos,
511 walk == queue->current ? "**y**" : " n ");
515 /* clear all the downloaded ranges */
517 clean_ranges (GstQueue2 * queue)
519 GST_DEBUG_OBJECT (queue, "clean queue ranges");
521 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
522 queue->ranges = NULL;
523 queue->current = NULL;
526 /* find a range that contains @offset or NULL when nothing does */
527 static GstQueue2Range *
528 find_range (GstQueue2 * queue, guint64 offset)
530 GstQueue2Range *range = NULL;
531 GstQueue2Range *walk;
533 /* first do a quick check for the current range */
534 for (walk = queue->ranges; walk; walk = walk->next) {
535 if (offset >= walk->offset && offset <= walk->writing_pos) {
536 /* we can reuse an existing range */
542 GST_DEBUG_OBJECT (queue,
543 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
544 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
546 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
552 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
554 guint64 max_reading_pos, writing_pos;
556 writing_pos = range->writing_pos;
557 max_reading_pos = range->max_reading_pos;
559 if (writing_pos > max_reading_pos)
560 queue->cur_level.bytes = writing_pos - max_reading_pos;
562 queue->cur_level.bytes = 0;
565 /* make a new range for @offset or reuse an existing range */
566 static GstQueue2Range *
567 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
569 GstQueue2Range *range, *prev, *next;
571 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
573 if ((range = find_range (queue, offset))) {
574 GST_DEBUG_OBJECT (queue,
575 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
577 if (update_existing && range->writing_pos != offset) {
578 GST_DEBUG_OBJECT (queue, "updating range writing position to "
579 "%" G_GUINT64_FORMAT, offset);
580 range->writing_pos = offset;
583 GST_DEBUG_OBJECT (queue,
584 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
586 range = g_slice_new0 (GstQueue2Range);
587 range->offset = offset;
588 /* we want to write to the next location in the ring buffer */
589 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
590 range->writing_pos = offset;
591 range->rb_writing_pos = range->rb_offset;
592 range->reading_pos = offset;
593 range->max_reading_pos = offset;
597 next = queue->ranges;
599 if (next->offset > offset) {
600 /* insert before next */
601 GST_DEBUG_OBJECT (queue,
602 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
614 queue->ranges = range;
616 debug_ranges (queue);
618 /* update the stats for this range */
619 update_cur_level (queue, range);
625 /* clear and init the download ranges for offset 0 */
627 init_ranges (GstQueue2 * queue)
629 GST_DEBUG_OBJECT (queue, "init queue ranges");
631 /* get rid of all the current ranges */
632 clean_ranges (queue);
633 /* make a range for offset 0 */
634 queue->current = add_range (queue, 0, TRUE);
637 /* calculate the diff between running time on the sink and src of the queue.
638 * This is the total amount of time in the queue. */
640 update_time_level (GstQueue2 * queue)
642 if (queue->sink_tainted) {
644 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
645 queue->sink_segment.position);
646 queue->sink_tainted = FALSE;
649 if (queue->src_tainted) {
651 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
652 queue->src_segment.position);
653 queue->src_tainted = FALSE;
656 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
657 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
659 if (queue->sinktime != GST_CLOCK_TIME_NONE
660 && queue->srctime != GST_CLOCK_TIME_NONE
661 && queue->sinktime >= queue->srctime)
662 queue->cur_level.time = queue->sinktime - queue->srctime;
664 queue->cur_level.time = 0;
667 /* take a SEGMENT event and apply the values to segment, updating the time
670 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
673 gst_event_copy_segment (event, segment);
675 if (segment->format == GST_FORMAT_BYTES) {
676 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
677 /* start is where we'll be getting from and as such writing next */
678 queue->current = add_range (queue, segment->start, TRUE);
682 /* now configure the values, we use these to track timestamps on the
684 if (segment->format != GST_FORMAT_TIME) {
685 /* non-time format, pretent the current time segment is closed with a
686 * 0 start and unknown stop time. */
687 segment->format = GST_FORMAT_TIME;
693 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
696 queue->sink_tainted = TRUE;
698 queue->src_tainted = TRUE;
700 /* segment can update the time level of the queue */
701 update_time_level (queue);
704 /* take a buffer and update segment, updating the time level of the queue. */
706 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
709 GstClockTime duration, timestamp;
711 timestamp = GST_BUFFER_TIMESTAMP (buffer);
712 duration = GST_BUFFER_DURATION (buffer);
714 /* if no timestamp is set, assume it's continuous with the previous
716 if (timestamp == GST_CLOCK_TIME_NONE)
717 timestamp = segment->position;
720 if (duration != GST_CLOCK_TIME_NONE)
721 timestamp += duration;
723 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
724 GST_TIME_ARGS (timestamp));
726 segment->position = timestamp;
729 queue->sink_tainted = TRUE;
731 queue->src_tainted = TRUE;
733 /* calc diff with other end */
734 update_time_level (queue);
738 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
740 GstClockTime *timestamp = data;
742 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
743 " duration %" GST_TIME_FORMAT, idx,
744 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
745 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
747 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
748 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
750 if (GST_BUFFER_DURATION_IS_VALID (*buf))
751 *timestamp += GST_BUFFER_DURATION (*buf);
753 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
757 /* take a buffer list and update segment, updating the time level of the queue */
759 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
760 GstSegment * segment, gboolean is_sink)
762 GstClockTime timestamp;
764 /* if no timestamp is set, assume it's continuous with the previous time */
765 timestamp = segment->position;
767 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
769 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
770 GST_TIME_ARGS (timestamp));
772 segment->position = timestamp;
775 queue->sink_tainted = TRUE;
777 queue->src_tainted = TRUE;
779 /* calc diff with other end */
780 update_time_level (queue);
784 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
789 if (queue->high_percent <= 0) {
793 *is_buffering = FALSE;
796 #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)
799 /* on EOS we are always 100% full, we set the var here so that it we can
800 * reuse the logic below to stop buffering */
802 GST_LOG_OBJECT (queue, "we are EOS");
804 /* figure out the percent we are filled, we take the max of all formats. */
805 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
806 perc = GET_PERCENT (bytes, 0);
808 guint64 rb_size = queue->ring_buffer_max_size;
809 perc = GET_PERCENT (bytes, rb_size);
811 perc = MAX (perc, GET_PERCENT (time, 0));
812 perc = MAX (perc, GET_PERCENT (buffers, 0));
814 /* also apply the rate estimate when we need to */
815 if (queue->use_rate_estimate)
816 perc = MAX (perc, GET_PERCENT (rate_time, 0));
821 *is_buffering = queue->is_buffering;
823 /* scale to high percent so that it becomes the 100% mark */
824 perc = perc * 100 / queue->high_percent;
832 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
839 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
840 gint * avg_in, gint * avg_out, gint64 * buffering_left)
843 if (!QUEUE_IS_USING_QUEUE (queue)) {
844 if (QUEUE_IS_USING_RING_BUFFER (queue))
845 *mode = GST_BUFFERING_TIMESHIFT;
847 *mode = GST_BUFFERING_DOWNLOAD;
849 *mode = GST_BUFFERING_STREAM;
854 *avg_in = queue->byte_in_rate;
856 *avg_out = queue->byte_out_rate;
858 if (buffering_left) {
859 *buffering_left = (percent == 100 ? 0 : -1);
861 if (queue->use_rate_estimate) {
864 max = queue->max_level.rate_time;
865 cur = queue->cur_level.rate_time;
867 if (percent != 100 && max > cur)
868 *buffering_left = (max - cur) / 1000000;
874 update_buffering (GstQueue2 * queue)
877 gboolean post = FALSE;
879 /* Ensure the variables used to calculate buffering state are up-to-date. */
881 update_cur_level (queue, queue->current);
882 update_in_rates (queue);
884 if (!get_buffering_percent (queue, NULL, &percent))
887 if (queue->is_buffering) {
889 /* if we were buffering see if we reached the high watermark */
890 if (percent >= queue->high_percent)
891 queue->is_buffering = FALSE;
893 /* we were not buffering, check if we need to start buffering if we drop
894 * below the low threshold */
895 if (percent < queue->low_percent) {
896 queue->is_buffering = TRUE;
902 if (percent == queue->buffering_percent)
905 queue->buffering_percent = percent;
910 GstBufferingMode mode;
911 gint avg_in, avg_out;
912 gint64 buffering_left;
914 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
917 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
919 gst_message_set_buffering_stats (message, mode,
920 avg_in, avg_out, buffering_left);
922 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
927 reset_rate_timer (GstQueue2 * queue)
930 queue->bytes_out = 0;
931 queue->byte_in_rate = 0.0;
932 queue->byte_in_period = 0;
933 queue->byte_out_rate = 0.0;
934 queue->last_in_elapsed = 0.0;
935 queue->last_out_elapsed = 0.0;
936 queue->in_timer_started = FALSE;
937 queue->out_timer_started = FALSE;
940 /* the interval in seconds to recalculate the rate */
941 #define RATE_INTERVAL 0.2
942 /* Tuning for rate estimation. We use a large window for the input rate because
943 * it should be stable when connected to a network. The output rate is less
944 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
945 * therefore adapt more quickly.
946 * However, initial input rate may be subject to a burst, and should therefore
947 * initially also adapt more quickly to changes, and only later on give higher
948 * weight to previous values. */
949 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
950 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
953 update_in_rates (GstQueue2 * queue)
955 gdouble elapsed, period;
956 gdouble byte_in_rate;
958 if (!queue->in_timer_started) {
959 queue->in_timer_started = TRUE;
960 g_timer_start (queue->in_timer);
964 elapsed = g_timer_elapsed (queue->in_timer, NULL);
966 /* recalc after each interval. */
967 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
968 period = elapsed - queue->last_in_elapsed;
970 GST_DEBUG_OBJECT (queue,
971 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
972 period, queue->bytes_in, queue->byte_in_period);
974 byte_in_rate = queue->bytes_in / period;
976 if (queue->byte_in_rate == 0.0)
977 queue->byte_in_rate = byte_in_rate;
979 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
980 (double) queue->byte_in_period, period);
982 /* another data point, cap at 16 for long time running average */
983 if (queue->byte_in_period < 16 * RATE_INTERVAL)
984 queue->byte_in_period += period;
986 /* reset the values to calculate rate over the next interval */
987 queue->last_in_elapsed = elapsed;
991 if (queue->byte_in_rate > 0.0) {
992 queue->cur_level.rate_time =
993 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
995 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
996 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1000 update_out_rates (GstQueue2 * queue)
1002 gdouble elapsed, period;
1003 gdouble byte_out_rate;
1005 if (!queue->out_timer_started) {
1006 queue->out_timer_started = TRUE;
1007 g_timer_start (queue->out_timer);
1011 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1013 /* recalc after each interval. */
1014 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1015 period = elapsed - queue->last_out_elapsed;
1017 GST_DEBUG_OBJECT (queue,
1018 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1020 byte_out_rate = queue->bytes_out / period;
1022 if (queue->byte_out_rate == 0.0)
1023 queue->byte_out_rate = byte_out_rate;
1025 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1027 /* reset the values to calculate rate over the next interval */
1028 queue->last_out_elapsed = elapsed;
1029 queue->bytes_out = 0;
1031 if (queue->byte_in_rate > 0.0) {
1032 queue->cur_level.rate_time =
1033 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1035 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1036 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1040 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1042 guint64 reading_pos, max_reading_pos;
1045 max_reading_pos = range->max_reading_pos;
1047 max_reading_pos = MAX (max_reading_pos, reading_pos);
1049 GST_DEBUG_OBJECT (queue,
1050 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1051 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1052 range->max_reading_pos = max_reading_pos;
1054 update_cur_level (queue, range);
1058 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1063 /* until we receive the FLUSH_STOP from this seek, we skip data */
1064 queue->seeking = TRUE;
1065 GST_QUEUE2_MUTEX_UNLOCK (queue);
1067 debug_ranges (queue);
1069 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1072 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1073 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1074 GST_SEEK_TYPE_NONE, -1);
1076 res = gst_pad_push_event (queue->sinkpad, event);
1077 GST_QUEUE2_MUTEX_LOCK (queue);
1080 /* Between us sending the seek event and re-acquiring the lock, the source
1081 * thread might already have pushed data and moved along the range's
1082 * writing_pos beyond the seek offset. In that case we don't want to set
1083 * the writing position back to the requested seek position, as it would
1084 * cause data to be written to the wrong offset in the file or ring buffer.
1085 * We still do the add_range call to switch the current range to the
1086 * requested range, or create one if one doesn't exist yet. */
1087 queue->current = add_range (queue, offset, FALSE);
1093 /* get the threshold for when we decide to seek rather than wait */
1095 get_seek_threshold (GstQueue2 * queue)
1099 /* FIXME, find a good threshold based on the incoming rate. */
1100 threshold = 1024 * 512;
1102 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1103 threshold = MIN (threshold,
1104 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1109 /* see if there is enough data in the file to read a full buffer */
1111 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1113 GstQueue2Range *range;
1115 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1118 if ((range = find_range (queue, offset))) {
1119 if (queue->current != range) {
1120 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1121 perform_seek_to_offset (queue, range->writing_pos);
1124 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1125 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1127 /* we have a range for offset */
1128 GST_DEBUG_OBJECT (queue,
1129 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1130 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1132 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1135 if (offset + length <= range->writing_pos)
1138 GST_DEBUG_OBJECT (queue,
1139 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1140 (offset + length) - range->writing_pos);
1143 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1144 " len %u", offset, length);
1145 /* we don't have the range, see how far away we are */
1146 if (!queue->is_eos && queue->current) {
1147 guint64 threshold = get_seek_threshold (queue);
1149 if (offset >= queue->current->offset && offset <=
1150 queue->current->writing_pos + threshold) {
1151 GST_INFO_OBJECT (queue,
1152 "requested data is within range, wait for data");
1157 /* too far away, do a seek */
1158 perform_seek_to_offset (queue, offset);
1165 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1166 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1167 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1169 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1172 static GstFlowReturn
1173 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1174 guint8 * dst, gint64 * read_return)
1176 guint8 *ring_buffer;
1179 ring_buffer = queue->ring_buffer;
1181 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1184 /* this should not block */
1185 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1187 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1188 res = fread (dst, 1, length, queue->temp_file);
1190 memcpy (dst, ring_buffer + offset, length);
1194 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1196 if (G_UNLIKELY (res < length)) {
1197 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1198 goto could_not_read;
1199 /* check for errors or EOF */
1200 if (ferror (queue->temp_file))
1201 goto could_not_read;
1202 if (feof (queue->temp_file) && length > 0)
1212 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1213 return GST_FLOW_ERROR;
1217 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1218 return GST_FLOW_ERROR;
1222 GST_DEBUG ("non-regular file hits EOS");
1223 return GST_FLOW_EOS;
1227 static GstFlowReturn
1228 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1229 GstBuffer ** buffer)
1234 guint64 file_offset;
1235 guint block_length, remaining, read_length;
1239 GstFlowReturn ret = GST_FLOW_OK;
1241 /* allocate the output buffer of the requested size */
1242 if (*buffer == NULL)
1243 buf = gst_buffer_new_allocate (NULL, length, NULL);
1247 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1250 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1254 rb_size = queue->ring_buffer_max_size;
1255 max_size = QUEUE_MAX_BYTES (queue);
1258 while (remaining > 0) {
1259 /* configure how much/whether to read */
1260 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1263 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1266 /* calculate how far away the offset is */
1267 if (queue->current->writing_pos > rpos)
1268 level = queue->current->writing_pos - rpos;
1272 GST_DEBUG_OBJECT (queue,
1273 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1274 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1275 rpos, queue->current->writing_pos, level, max_size);
1277 if (level >= max_size) {
1278 /* we don't have the data but if we have a ring buffer that is full, we
1280 GST_DEBUG_OBJECT (queue,
1281 "ring buffer full, reading QUEUE_MAX_BYTES %"
1282 G_GUINT64_FORMAT " bytes", max_size);
1283 read_length = max_size;
1284 } else if (queue->is_eos) {
1285 /* won't get any more data so read any data we have */
1287 GST_DEBUG_OBJECT (queue,
1288 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1290 read_length = level;
1298 if (read_length == 0) {
1299 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1300 GST_DEBUG_OBJECT (queue,
1301 "update current position [%" G_GUINT64_FORMAT "-%"
1302 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1303 update_cur_pos (queue, queue->current, rpos);
1304 GST_QUEUE2_SIGNAL_DEL (queue);
1307 if (queue->use_buffering)
1308 update_buffering (queue);
1310 GST_DEBUG_OBJECT (queue, "waiting for add");
1311 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1315 /* we have the requested data so read it */
1316 read_length = remaining;
1319 /* set range reading_pos to actual reading position for this read */
1320 queue->current->reading_pos = rpos;
1322 /* configure how much and from where to read */
1323 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1325 (queue->current->rb_offset + (rpos -
1326 queue->current->offset)) % rb_size;
1327 if (file_offset + read_length > rb_size) {
1328 block_length = rb_size - file_offset;
1330 block_length = read_length;
1334 block_length = read_length;
1337 /* while we still have data to read, we loop */
1338 while (read_length > 0) {
1342 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1343 data, &read_return);
1344 if (ret != GST_FLOW_OK)
1347 file_offset += read_return;
1348 if (QUEUE_IS_USING_RING_BUFFER (queue))
1349 file_offset %= rb_size;
1351 data += read_return;
1352 read_length -= read_return;
1353 block_length = read_length;
1354 remaining -= read_return;
1356 rpos = (queue->current->reading_pos += read_return);
1357 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1359 GST_QUEUE2_SIGNAL_DEL (queue);
1360 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1363 gst_buffer_unmap (buf, &info);
1364 gst_buffer_resize (buf, 0, length);
1366 GST_BUFFER_OFFSET (buf) = offset;
1367 GST_BUFFER_OFFSET_END (buf) = offset + length;
1376 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1377 gst_buffer_unmap (buf, &info);
1378 if (*buffer == NULL)
1379 gst_buffer_unref (buf);
1380 return GST_FLOW_EOS;
1384 GST_DEBUG_OBJECT (queue, "we are flushing");
1385 gst_buffer_unmap (buf, &info);
1386 if (*buffer == NULL)
1387 gst_buffer_unref (buf);
1388 return GST_FLOW_FLUSHING;
1392 GST_DEBUG_OBJECT (queue, "we have a read error");
1393 gst_buffer_unmap (buf, &info);
1394 if (*buffer == NULL)
1395 gst_buffer_unref (buf);
1400 /* should be called with QUEUE_LOCK */
1401 static GstMiniObject *
1402 gst_queue2_read_item_from_file (GstQueue2 * queue)
1404 GstMiniObject *item;
1406 if (queue->stream_start_event != NULL) {
1407 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1408 queue->stream_start_event = NULL;
1409 } else if (queue->starting_segment != NULL) {
1410 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1411 queue->starting_segment = NULL;
1414 GstBuffer *buffer = NULL;
1415 guint64 reading_pos;
1417 reading_pos = queue->current->reading_pos;
1420 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1425 item = GST_MINI_OBJECT_CAST (buffer);
1428 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1438 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1439 * the temp filename. */
1441 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1446 if (queue->temp_file)
1447 goto already_opened;
1449 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1451 /* If temp_template was set, allocate a filename and open that filen */
1454 if (queue->temp_template == NULL)
1457 /* make copy of the template, we don't want to change this */
1458 name = g_strdup (queue->temp_template);
1459 fd = g_mkstemp (name);
1461 goto mkstemp_failed;
1463 /* open the file for update/writing */
1464 queue->temp_file = fdopen (fd, "wb+");
1465 /* error creating file */
1466 if (queue->temp_file == NULL)
1469 g_free (queue->temp_location);
1470 queue->temp_location = name;
1472 GST_QUEUE2_MUTEX_UNLOCK (queue);
1474 /* we can't emit the notify with the lock */
1475 g_object_notify (G_OBJECT (queue), "temp-location");
1477 GST_QUEUE2_MUTEX_LOCK (queue);
1479 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1486 GST_DEBUG_OBJECT (queue, "temp file was already open");
1491 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1492 (_("No Temp directory specified.")), (NULL));
1497 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1498 (_("Could not create temp file \"%s\"."), queue->temp_template),
1505 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1506 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1515 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1518 if (queue->temp_file == NULL)
1521 GST_DEBUG_OBJECT (queue, "closing temp file");
1523 fflush (queue->temp_file);
1524 fclose (queue->temp_file);
1526 if (queue->temp_remove)
1527 remove (queue->temp_location);
1529 queue->temp_file = NULL;
1530 clean_ranges (queue);
1534 gst_queue2_flush_temp_file (GstQueue2 * queue)
1536 if (queue->temp_file == NULL)
1539 GST_DEBUG_OBJECT (queue, "flushing temp file");
1541 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1545 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1547 if (!QUEUE_IS_USING_QUEUE (queue)) {
1548 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1549 gst_queue2_flush_temp_file (queue);
1550 init_ranges (queue);
1552 while (!g_queue_is_empty (&queue->queue)) {
1553 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1555 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1556 && GST_EVENT_IS_STICKY (qitem->item)
1557 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1558 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1559 gst_pad_store_sticky_event (queue->srcpad,
1560 GST_EVENT_CAST (qitem->item));
1563 /* Then lose another reference because we are supposed to destroy that
1564 data when flushing */
1565 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1566 gst_mini_object_unref (qitem->item);
1567 g_slice_free (GstQueue2Item, qitem);
1570 queue->last_query = FALSE;
1571 g_cond_signal (&queue->query_handled);
1572 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1573 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1574 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1575 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1576 queue->sink_tainted = queue->src_tainted = TRUE;
1577 if (queue->starting_segment != NULL)
1578 gst_event_unref (queue->starting_segment);
1579 queue->starting_segment = NULL;
1580 queue->segment_event_received = FALSE;
1581 gst_event_replace (&queue->stream_start_event, NULL);
1583 /* we deleted a lot of something */
1584 GST_QUEUE2_SIGNAL_DEL (queue);
1588 gst_queue2_wait_free_space (GstQueue2 * queue)
1590 /* We make space available if we're "full" according to whatever
1591 * the user defined as "full". */
1592 if (gst_queue2_is_filled (queue)) {
1595 /* pause the timer while we wait. The fact that we are waiting does not mean
1596 * the byterate on the input pad is lower */
1597 if ((started = queue->in_timer_started))
1598 g_timer_stop (queue->in_timer);
1600 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1601 "queue is full, waiting for free space");
1603 /* Wait for space to be available, we could be unlocked because of a flush. */
1604 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1606 while (gst_queue2_is_filled (queue));
1608 /* and continue if we were running before */
1610 g_timer_continue (queue->in_timer);
1617 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1623 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1626 guint8 *data, *ring_buffer;
1627 guint size, rb_size;
1628 guint64 writing_pos, new_writing_pos;
1629 GstQueue2Range *range, *prev, *next;
1630 gboolean do_seek = FALSE;
1632 if (QUEUE_IS_USING_RING_BUFFER (queue))
1633 writing_pos = queue->current->rb_writing_pos;
1635 writing_pos = queue->current->writing_pos;
1636 ring_buffer = queue->ring_buffer;
1637 rb_size = queue->ring_buffer_max_size;
1639 gst_buffer_map (buffer, &info, GST_MAP_READ);
1644 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1648 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1649 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1650 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1651 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1652 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1658 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1661 /* calculate the space in the ring buffer not used by data from
1662 * the current range */
1663 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1664 /* wait until there is some free space */
1665 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1667 /* get the amount of space we have */
1668 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1670 /* calculate if we need to split or if we can write the entire
1672 to_write = MIN (size, space);
1674 /* the writing position in the ring buffer after writing (part
1675 * or all of) the buffer */
1676 new_writing_pos = (writing_pos + to_write) % rb_size;
1679 range = queue->ranges;
1681 /* if we need to overwrite data in the ring buffer, we need to
1684 * warning: this code is complicated and includes some
1685 * simplifications - pen, paper and diagrams for the cases
1688 guint64 range_data_start, range_data_end;
1689 GstQueue2Range *range_to_destroy = NULL;
1691 range_data_start = range->rb_offset;
1692 range_data_end = range->rb_writing_pos;
1694 /* handle the special case where the range has no data in it */
1695 if (range->writing_pos == range->offset) {
1696 if (range != queue->current) {
1697 GST_DEBUG_OBJECT (queue,
1698 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1699 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1701 range_to_destroy = range;
1703 prev->next = range->next;
1708 if (range_data_end > range_data_start) {
1709 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1712 if (new_writing_pos > range_data_start) {
1713 if (new_writing_pos >= range_data_end) {
1714 GST_DEBUG_OBJECT (queue,
1715 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1716 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1718 range_to_destroy = range;
1720 prev->next = range->next;
1722 GST_DEBUG_OBJECT (queue,
1723 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1724 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1725 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1726 range->offset + new_writing_pos - range_data_start,
1728 range->offset += (new_writing_pos - range_data_start);
1729 range->rb_offset = new_writing_pos;
1733 guint64 new_wpos_virt = writing_pos + to_write;
1735 if (new_wpos_virt <= range_data_start)
1738 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1739 GST_DEBUG_OBJECT (queue,
1740 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1741 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1743 range_to_destroy = range;
1745 prev->next = range->next;
1747 GST_DEBUG_OBJECT (queue,
1748 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1749 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1750 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1751 range->offset + new_writing_pos - range_data_start,
1753 range->offset += (new_wpos_virt - range_data_start);
1754 range->rb_offset = new_writing_pos;
1759 if (!range_to_destroy)
1762 range = range->next;
1763 if (range_to_destroy) {
1764 if (range_to_destroy == queue->ranges)
1765 queue->ranges = range;
1766 g_slice_free (GstQueue2Range, range_to_destroy);
1767 range_to_destroy = NULL;
1772 new_writing_pos = writing_pos + to_write;
1775 if (QUEUE_IS_USING_TEMP_FILE (queue)
1776 && FSEEK_FILE (queue->temp_file, writing_pos))
1779 if (new_writing_pos > writing_pos) {
1780 GST_INFO_OBJECT (queue,
1781 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1782 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1783 queue->current->writing_pos, queue->current->rb_writing_pos);
1784 /* either not using ring buffer or no wrapping, just write */
1785 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1786 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1789 memcpy (ring_buffer + writing_pos, data, to_write);
1792 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1793 /* try to merge with next range */
1794 while ((next = queue->current->next)) {
1795 GST_INFO_OBJECT (queue,
1796 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1797 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1798 if (new_writing_pos < next->offset)
1801 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1804 /* remove the group */
1805 queue->current->next = next->next;
1807 /* We use the threshold to decide if we want to do a seek or simply
1808 * read the data again. If there is not so much data in the range we
1809 * prefer to avoid to seek and read it again. */
1810 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1811 /* the new range had more data than the threshold, it's worth keeping
1812 * it and doing a seek. */
1813 new_writing_pos = next->writing_pos;
1816 g_slice_free (GstQueue2Range, next);
1818 goto update_and_signal;
1822 guint block_one, block_two;
1824 block_one = rb_size - writing_pos;
1825 block_two = to_write - block_one;
1827 if (block_one > 0) {
1828 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1829 /* write data to end of ring buffer */
1830 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1831 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1834 memcpy (ring_buffer + writing_pos, data, block_one);
1838 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1841 if (block_two > 0) {
1842 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1843 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1844 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1847 memcpy (ring_buffer, data + block_one, block_two);
1853 /* update the writing positions */
1855 GST_INFO_OBJECT (queue,
1856 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1857 to_write, writing_pos, size);
1859 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1861 queue->current->writing_pos += to_write;
1862 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1864 queue->current->writing_pos = writing_pos = new_writing_pos;
1867 perform_seek_to_offset (queue, new_writing_pos);
1869 update_cur_level (queue, queue->current);
1871 /* update the buffering status */
1872 if (queue->use_buffering)
1873 update_buffering (queue);
1875 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1876 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1878 GST_QUEUE2_SIGNAL_ADD (queue);
1881 gst_buffer_unmap (buffer, &info);
1888 GST_DEBUG_OBJECT (queue, "we are flushing");
1889 gst_buffer_unmap (buffer, &info);
1890 /* FIXME - GST_FLOW_EOS ? */
1895 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1896 gst_buffer_unmap (buffer, &info);
1903 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1907 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1908 (_("Error while writing to download file.")),
1909 ("%s", g_strerror (errno)));
1912 gst_buffer_unmap (buffer, &info);
1918 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1920 GstQueue2 *queue = q;
1922 GST_TRACE_OBJECT (queue,
1923 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1924 gst_buffer_get_size (*buf));
1926 if (!gst_queue2_create_write (queue, *buf)) {
1927 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1934 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1936 guint *p_size = data;
1939 buf_size = gst_buffer_get_size (*buf);
1940 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1941 *p_size += buf_size;
1945 /* enqueue an item an update the level stats */
1947 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1948 GstQueue2ItemType item_type)
1950 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1954 buffer = GST_BUFFER_CAST (item);
1955 size = gst_buffer_get_size (buffer);
1957 /* add buffer to the statistics */
1958 if (QUEUE_IS_USING_QUEUE (queue)) {
1959 queue->cur_level.buffers++;
1960 queue->cur_level.bytes += size;
1962 queue->bytes_in += size;
1964 /* apply new buffer to segment stats */
1965 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1966 /* update the byterate stats */
1967 update_in_rates (queue);
1969 if (!QUEUE_IS_USING_QUEUE (queue)) {
1970 /* FIXME - check return value? */
1971 gst_queue2_create_write (queue, buffer);
1973 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1974 GstBufferList *buffer_list;
1977 buffer_list = GST_BUFFER_LIST_CAST (item);
1979 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1980 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1982 /* add buffer to the statistics */
1983 if (QUEUE_IS_USING_QUEUE (queue)) {
1984 queue->cur_level.buffers++;
1985 queue->cur_level.bytes += size;
1987 queue->bytes_in += size;
1989 /* apply new buffer to segment stats */
1990 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1992 /* update the byterate stats */
1993 update_in_rates (queue);
1995 if (!QUEUE_IS_USING_QUEUE (queue)) {
1996 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1998 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2001 event = GST_EVENT_CAST (item);
2003 switch (GST_EVENT_TYPE (event)) {
2005 /* Zero the thresholds, this makes sure the queue is completely
2006 * filled and we can read all data from the queue. */
2007 GST_DEBUG_OBJECT (queue, "we have EOS");
2008 queue->is_eos = TRUE;
2010 case GST_EVENT_SEGMENT:
2011 apply_segment (queue, event, &queue->sink_segment, TRUE);
2012 /* This is our first new segment, we hold it
2013 * as we can't save it on the temp file */
2014 if (!QUEUE_IS_USING_QUEUE (queue)) {
2015 if (queue->segment_event_received)
2016 goto unexpected_event;
2018 queue->segment_event_received = TRUE;
2019 if (queue->starting_segment != NULL)
2020 gst_event_unref (queue->starting_segment);
2021 queue->starting_segment = event;
2024 /* a new segment allows us to accept more buffers if we got EOS
2025 * from downstream */
2026 queue->unexpected = FALSE;
2028 case GST_EVENT_STREAM_START:
2029 if (!QUEUE_IS_USING_QUEUE (queue)) {
2030 gst_event_replace (&queue->stream_start_event, event);
2031 gst_event_unref (event);
2035 case GST_EVENT_CAPS:{
2038 gst_event_parse_caps (event, &caps);
2039 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2041 if (!QUEUE_IS_USING_QUEUE (queue)) {
2042 GST_LOG ("Dropping caps event, not using queue");
2043 gst_event_unref (event);
2049 if (!QUEUE_IS_USING_QUEUE (queue))
2050 goto unexpected_event;
2053 } else if (GST_IS_QUERY (item)) {
2054 /* Can't happen as we check that in the caller */
2055 if (!QUEUE_IS_USING_QUEUE (queue))
2056 g_assert_not_reached ();
2058 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2059 item, GST_OBJECT_NAME (queue));
2060 /* we can't really unref since we don't know what it is */
2065 /* update the buffering status */
2066 if (queue->use_buffering)
2067 update_buffering (queue);
2069 if (QUEUE_IS_USING_QUEUE (queue)) {
2070 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2071 qitem->type = item_type;
2073 g_queue_push_tail (&queue->queue, qitem);
2075 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2078 GST_QUEUE2_SIGNAL_ADD (queue);
2087 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2088 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2089 GST_OBJECT_NAME (queue));
2090 gst_event_unref (GST_EVENT_CAST (item));
2095 /* dequeue an item from the queue and update level stats */
2096 static GstMiniObject *
2097 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2099 GstMiniObject *item;
2101 if (!QUEUE_IS_USING_QUEUE (queue)) {
2102 item = gst_queue2_read_item_from_file (queue);
2104 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2110 g_slice_free (GstQueue2Item, qitem);
2116 if (GST_IS_BUFFER (item)) {
2120 buffer = GST_BUFFER_CAST (item);
2121 size = gst_buffer_get_size (buffer);
2122 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2124 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2125 "retrieved buffer %p from queue", buffer);
2127 if (QUEUE_IS_USING_QUEUE (queue)) {
2128 queue->cur_level.buffers--;
2129 queue->cur_level.bytes -= size;
2131 queue->bytes_out += size;
2133 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2134 /* update the byterate stats */
2135 update_out_rates (queue);
2136 /* update the buffering */
2137 if (queue->use_buffering)
2138 update_buffering (queue);
2140 } else if (GST_IS_EVENT (item)) {
2141 GstEvent *event = GST_EVENT_CAST (item);
2143 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2145 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2146 "retrieved event %p from queue", event);
2148 switch (GST_EVENT_TYPE (event)) {
2150 /* queue is empty now that we dequeued the EOS */
2151 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2153 case GST_EVENT_SEGMENT:
2154 apply_segment (queue, event, &queue->src_segment, FALSE);
2159 } else if (GST_IS_BUFFER_LIST (item)) {
2160 GstBufferList *buffer_list;
2163 buffer_list = GST_BUFFER_LIST_CAST (item);
2164 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2165 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2167 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2168 "retrieved buffer list %p from queue", buffer_list);
2170 if (QUEUE_IS_USING_QUEUE (queue)) {
2171 queue->cur_level.buffers--;
2172 queue->cur_level.bytes -= size;
2174 queue->bytes_out += size;
2176 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2177 /* update the byterate stats */
2178 update_out_rates (queue);
2179 /* update the buffering */
2180 if (queue->use_buffering)
2181 update_buffering (queue);
2182 } else if (GST_IS_QUERY (item)) {
2183 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2184 "retrieved query %p from queue", item);
2185 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2188 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2189 item, GST_OBJECT_NAME (queue));
2191 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2193 GST_QUEUE2_SIGNAL_DEL (queue);
2200 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2206 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2209 gboolean ret = TRUE;
2212 queue = GST_QUEUE2 (parent);
2214 switch (GST_EVENT_TYPE (event)) {
2215 case GST_EVENT_FLUSH_START:
2217 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2218 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2220 ret = gst_pad_push_event (queue->srcpad, event);
2222 /* now unblock the chain function */
2223 GST_QUEUE2_MUTEX_LOCK (queue);
2224 queue->srcresult = GST_FLOW_FLUSHING;
2225 queue->sinkresult = GST_FLOW_FLUSHING;
2226 /* unblock the loop and chain functions */
2227 GST_QUEUE2_SIGNAL_ADD (queue);
2228 GST_QUEUE2_SIGNAL_DEL (queue);
2229 queue->last_query = FALSE;
2230 g_cond_signal (&queue->query_handled);
2231 GST_QUEUE2_MUTEX_UNLOCK (queue);
2233 /* make sure it pauses, this should happen since we sent
2234 * flush_start downstream. */
2235 gst_pad_pause_task (queue->srcpad);
2236 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2238 GST_QUEUE2_MUTEX_LOCK (queue);
2239 /* flush the sink pad */
2240 queue->sinkresult = GST_FLOW_FLUSHING;
2241 GST_QUEUE2_SIGNAL_DEL (queue);
2242 queue->last_query = FALSE;
2243 g_cond_signal (&queue->query_handled);
2244 GST_QUEUE2_MUTEX_UNLOCK (queue);
2246 gst_event_unref (event);
2250 case GST_EVENT_FLUSH_STOP:
2252 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2254 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2256 ret = gst_pad_push_event (queue->srcpad, event);
2258 GST_QUEUE2_MUTEX_LOCK (queue);
2259 gst_queue2_locked_flush (queue, FALSE, TRUE);
2260 queue->srcresult = GST_FLOW_OK;
2261 queue->sinkresult = GST_FLOW_OK;
2262 queue->is_eos = FALSE;
2263 queue->unexpected = FALSE;
2264 queue->seeking = FALSE;
2265 /* reset rate counters */
2266 reset_rate_timer (queue);
2267 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2268 queue->srcpad, NULL);
2269 GST_QUEUE2_MUTEX_UNLOCK (queue);
2271 GST_QUEUE2_MUTEX_LOCK (queue);
2272 queue->segment_event_received = FALSE;
2273 queue->is_eos = FALSE;
2274 queue->unexpected = FALSE;
2275 queue->sinkresult = GST_FLOW_OK;
2276 queue->seeking = FALSE;
2277 GST_QUEUE2_MUTEX_UNLOCK (queue);
2279 gst_event_unref (event);
2284 if (GST_EVENT_IS_SERIALIZED (event)) {
2285 /* serialized events go in the queue */
2286 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2287 /* refuse more events on EOS */
2290 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2291 GST_QUEUE2_MUTEX_UNLOCK (queue);
2293 /* non-serialized events are passed upstream. */
2294 ret = gst_pad_push_event (queue->srcpad, event);
2303 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2304 GST_QUEUE2_MUTEX_UNLOCK (queue);
2305 gst_event_unref (event);
2310 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2311 GST_QUEUE2_MUTEX_UNLOCK (queue);
2312 gst_event_unref (event);
2318 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2324 queue = GST_QUEUE2 (parent);
2326 switch (GST_QUERY_TYPE (query)) {
2328 if (GST_QUERY_IS_SERIALIZED (query)) {
2329 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2330 /* serialized events go in the queue. We need to be certain that we
2331 * don't cause deadlocks waiting for the query return value. We check if
2332 * the queue is empty (nothing is blocking downstream and the query can
2333 * be pushed for sure) or we are not buffering. If we are buffering,
2334 * the pipeline waits to unblock downstream until our queue fills up
2335 * completely, which can not happen if we block on the query..
2336 * Therefore we only potentially block when we are not buffering. */
2337 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2338 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2339 || !queue->use_buffering)) {
2340 gst_queue2_locked_enqueue (queue, query, GST_QUEUE2_ITEM_TYPE_QUERY);
2342 STATUS (queue, queue->sinkpad, "wait for QUERY");
2343 g_cond_wait (&queue->query_handled, &queue->qlock);
2344 if (queue->sinkresult != GST_FLOW_OK)
2346 res = queue->last_query;
2348 GST_DEBUG_OBJECT (queue,
2349 "refusing query, we are not using the queue");
2352 GST_QUEUE2_MUTEX_UNLOCK (queue);
2354 res = gst_pad_query_default (pad, parent, query);
2363 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2364 GST_QUEUE2_MUTEX_UNLOCK (queue);
2370 gst_queue2_is_empty (GstQueue2 * queue)
2372 /* never empty on EOS */
2376 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2377 return queue->current->writing_pos <= queue->current->max_reading_pos;
2379 if (queue->queue.length == 0)
2387 gst_queue2_is_filled (GstQueue2 * queue)
2391 /* always filled on EOS */
2395 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2396 (queue->cur_level.format) >= ((alt_max) ? \
2397 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2399 /* if using a ring buffer we're filled if all ring buffer space is used
2400 * _by the current range_ */
2401 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2402 guint64 rb_size = queue->ring_buffer_max_size;
2403 GST_DEBUG_OBJECT (queue,
2404 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2405 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2406 return CHECK_FILLED (bytes, rb_size);
2409 /* if using file, we're never filled if we don't have EOS */
2410 if (QUEUE_IS_USING_TEMP_FILE (queue))
2413 /* we are never filled when we have no buffers at all */
2414 if (queue->cur_level.buffers == 0)
2417 /* we are filled if one of the current levels exceeds the max */
2418 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2419 || CHECK_FILLED (time, 0);
2421 /* if we need to, use the rate estimate to check against the max time we are
2422 * allowed to queue */
2423 if (queue->use_rate_estimate)
2424 res |= CHECK_FILLED (rate_time, 0);
2430 static GstFlowReturn
2431 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2432 GstMiniObject * item, GstQueue2ItemType item_type)
2434 /* we have to lock the queue since we span threads */
2435 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2436 /* when we received EOS, we refuse more data */
2439 /* when we received unexpected from downstream, refuse more buffers */
2440 if (queue->unexpected)
2441 goto out_unexpected;
2443 /* while we didn't receive the newsegment, we're seeking and we skip data */
2447 if (!gst_queue2_wait_free_space (queue))
2450 /* put buffer in queue now */
2451 gst_queue2_locked_enqueue (queue, item, item_type);
2452 GST_QUEUE2_MUTEX_UNLOCK (queue);
2456 /* special conditions */
2459 GstFlowReturn ret = queue->sinkresult;
2461 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2462 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2463 GST_QUEUE2_MUTEX_UNLOCK (queue);
2464 gst_mini_object_unref (item);
2470 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2471 GST_QUEUE2_MUTEX_UNLOCK (queue);
2472 gst_mini_object_unref (item);
2474 return GST_FLOW_EOS;
2478 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2479 GST_QUEUE2_MUTEX_UNLOCK (queue);
2480 gst_mini_object_unref (item);
2486 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2487 GST_QUEUE2_MUTEX_UNLOCK (queue);
2488 gst_mini_object_unref (item);
2490 return GST_FLOW_EOS;
2494 static GstFlowReturn
2495 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2499 queue = GST_QUEUE2 (parent);
2501 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2502 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2503 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2504 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2505 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2507 return gst_queue2_chain_buffer_or_buffer_list (queue,
2508 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2511 static GstFlowReturn
2512 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2513 GstBufferList * buffer_list)
2517 queue = GST_QUEUE2 (parent);
2519 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2520 "received buffer list %p", buffer_list);
2522 return gst_queue2_chain_buffer_or_buffer_list (queue,
2523 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2526 static GstMiniObject *
2527 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2529 GstMiniObject *data;
2531 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2533 /* stop pushing buffers, we dequeue all items until we see an item that we
2534 * can push again, which is EOS or SEGMENT. If there is nothing in the
2535 * queue we can push, we set a flag to make the sinkpad refuse more
2536 * buffers with an EOS return value until we receive something
2537 * pushable again or we get flushed. */
2538 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2539 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2540 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2541 "dropping EOS buffer %p", data);
2542 gst_buffer_unref (GST_BUFFER_CAST (data));
2543 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2544 GstEvent *event = GST_EVENT_CAST (data);
2545 GstEventType type = GST_EVENT_TYPE (event);
2547 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2548 /* we found a pushable item in the queue, push it out */
2549 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2550 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2553 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2554 "dropping EOS event %p", event);
2555 gst_event_unref (event);
2556 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2557 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2558 "dropping EOS buffer list %p", data);
2559 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2560 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2561 queue->last_query = FALSE;
2562 g_cond_signal (&queue->query_handled);
2563 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2566 /* no more items in the queue. Set the unexpected flag so that upstream
2567 * make us refuse any more buffers on the sinkpad. Since we will still
2568 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2569 * task function does not shut down. */
2570 queue->unexpected = TRUE;
2574 /* dequeue an item from the queue an push it downstream. This functions returns
2575 * the result of the push. */
2576 static GstFlowReturn
2577 gst_queue2_push_one (GstQueue2 * queue)
2579 GstFlowReturn result = queue->srcresult;
2580 GstMiniObject *data;
2581 GstQueue2ItemType item_type;
2583 data = gst_queue2_locked_dequeue (queue, &item_type);
2588 GST_QUEUE2_MUTEX_UNLOCK (queue);
2590 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2593 buffer = GST_BUFFER_CAST (data);
2595 result = gst_pad_push (queue->srcpad, buffer);
2597 /* need to check for srcresult here as well */
2598 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2599 if (result == GST_FLOW_EOS) {
2600 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2603 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2604 * to the caller so that the task function does not shut down */
2605 result = GST_FLOW_OK;
2607 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2608 GstEvent *event = GST_EVENT_CAST (data);
2609 GstEventType type = GST_EVENT_TYPE (event);
2611 gst_pad_push_event (queue->srcpad, event);
2613 /* if we're EOS, return EOS so that the task pauses. */
2614 if (type == GST_EVENT_EOS) {
2615 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2616 "pushed EOS event %p, return EOS", event);
2617 result = GST_FLOW_EOS;
2620 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2621 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2622 GstBufferList *buffer_list;
2624 buffer_list = GST_BUFFER_LIST_CAST (data);
2626 result = gst_pad_push_list (queue->srcpad, buffer_list);
2628 /* need to check for srcresult here as well */
2629 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2630 if (result == GST_FLOW_EOS) {
2631 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2634 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2635 * to the caller so that the task function does not shut down */
2636 result = GST_FLOW_OK;
2638 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2639 GstQuery *query = GST_QUERY_CAST (data);
2641 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2642 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2643 "did query %p, return %d", query, queue->last_query);
2644 g_cond_signal (&queue->query_handled);
2645 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2646 result = GST_FLOW_OK;
2653 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2654 "exit because we have no item in the queue");
2655 return GST_FLOW_ERROR;
2659 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2660 return GST_FLOW_FLUSHING;
2664 /* called repeatedly with @pad as the source pad. This function should push out
2665 * data to the peer element. */
2667 gst_queue2_loop (GstPad * pad)
2672 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2674 /* have to lock for thread-safety */
2675 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2677 if (gst_queue2_is_empty (queue)) {
2680 /* pause the timer while we wait. The fact that we are waiting does not mean
2681 * the byterate on the output pad is lower */
2682 if ((started = queue->out_timer_started))
2683 g_timer_stop (queue->out_timer);
2685 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2686 "queue is empty, waiting for new data");
2688 /* Wait for data to be available, we could be unlocked because of a flush. */
2689 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2691 while (gst_queue2_is_empty (queue));
2693 /* and continue if we were running before */
2695 g_timer_continue (queue->out_timer);
2697 ret = gst_queue2_push_one (queue);
2698 queue->srcresult = ret;
2699 queue->sinkresult = ret;
2700 if (ret != GST_FLOW_OK)
2703 GST_QUEUE2_MUTEX_UNLOCK (queue);
2710 gboolean eos = queue->is_eos;
2711 GstFlowReturn ret = queue->srcresult;
2713 gst_pad_pause_task (queue->srcpad);
2714 GST_QUEUE2_MUTEX_UNLOCK (queue);
2715 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2716 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2717 /* let app know about us giving up if upstream is not expected to do so */
2718 /* EOS is already taken care of elsewhere */
2719 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2720 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2721 (_("Internal data flow error.")),
2722 ("streaming task paused, reason %s (%d)",
2723 gst_flow_get_name (ret), ret));
2724 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2731 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2733 gboolean res = TRUE;
2734 GstQueue2 *queue = GST_QUEUE2 (parent);
2736 #ifndef GST_DISABLE_GST_DEBUG
2737 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2738 event, GST_EVENT_TYPE_NAME (event));
2741 switch (GST_EVENT_TYPE (event)) {
2742 case GST_EVENT_FLUSH_START:
2743 if (QUEUE_IS_USING_QUEUE (queue)) {
2744 /* just forward upstream */
2745 res = gst_pad_push_event (queue->sinkpad, event);
2747 /* now unblock the getrange function */
2748 GST_QUEUE2_MUTEX_LOCK (queue);
2749 GST_DEBUG_OBJECT (queue, "flushing");
2750 queue->srcresult = GST_FLOW_FLUSHING;
2751 GST_QUEUE2_SIGNAL_ADD (queue);
2752 GST_QUEUE2_MUTEX_UNLOCK (queue);
2754 /* when using a temp file, we eat the event */
2756 gst_event_unref (event);
2759 case GST_EVENT_FLUSH_STOP:
2760 if (QUEUE_IS_USING_QUEUE (queue)) {
2761 /* just forward upstream */
2762 res = gst_pad_push_event (queue->sinkpad, event);
2764 /* now unblock the getrange function */
2765 GST_QUEUE2_MUTEX_LOCK (queue);
2766 queue->srcresult = GST_FLOW_OK;
2767 GST_QUEUE2_MUTEX_UNLOCK (queue);
2769 /* when using a temp file, we eat the event */
2771 gst_event_unref (event);
2774 case GST_EVENT_RECONFIGURE:
2775 GST_QUEUE2_MUTEX_LOCK (queue);
2776 /* assume downstream is linked now and try to push again */
2777 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2778 queue->srcresult = GST_FLOW_OK;
2779 queue->sinkresult = GST_FLOW_OK;
2780 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2781 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2785 GST_QUEUE2_MUTEX_UNLOCK (queue);
2787 res = gst_pad_push_event (queue->sinkpad, event);
2790 res = gst_pad_push_event (queue->sinkpad, event);
2798 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2802 queue = GST_QUEUE2 (parent);
2804 switch (GST_QUERY_TYPE (query)) {
2805 case GST_QUERY_POSITION:
2810 if (!gst_pad_peer_query (queue->sinkpad, query))
2813 /* get peer position */
2814 gst_query_parse_position (query, &format, &peer_pos);
2816 /* FIXME: this code assumes that there's no discont in the queue */
2818 case GST_FORMAT_BYTES:
2819 peer_pos -= queue->cur_level.bytes;
2821 case GST_FORMAT_TIME:
2822 peer_pos -= queue->cur_level.time;
2825 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2826 "know how to adjust value", gst_format_get_name (format));
2829 /* set updated position */
2830 gst_query_set_position (query, format, peer_pos);
2833 case GST_QUERY_DURATION:
2835 GST_DEBUG_OBJECT (queue, "doing peer query");
2837 if (!gst_pad_peer_query (queue->sinkpad, query))
2840 GST_DEBUG_OBJECT (queue, "peer query success");
2843 case GST_QUERY_BUFFERING:
2846 gboolean is_buffering;
2847 GstBufferingMode mode;
2848 gint avg_in, avg_out;
2849 gint64 buffering_left;
2851 GST_DEBUG_OBJECT (queue, "query buffering");
2853 get_buffering_percent (queue, &is_buffering, &percent);
2854 gst_query_set_buffering_percent (query, is_buffering, percent);
2856 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
2858 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
2861 if (!QUEUE_IS_USING_QUEUE (queue)) {
2862 /* add ranges for download and ringbuffer buffering */
2864 gint64 start, stop, range_start, range_stop;
2865 guint64 writing_pos;
2866 gint64 estimated_total;
2868 gboolean peer_res, is_eos;
2869 GstQueue2Range *queued_ranges;
2871 /* we need a current download region */
2872 if (queue->current == NULL)
2875 writing_pos = queue->current->writing_pos;
2876 is_eos = queue->is_eos;
2879 /* we're EOS, we know the duration in bytes now */
2881 duration = writing_pos;
2883 /* get duration of upstream in bytes */
2884 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2885 GST_FORMAT_BYTES, &duration);
2888 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
2889 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
2891 /* calculate remaining and total download time */
2892 if (peer_res && avg_in > 0.0)
2893 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
2895 estimated_total = -1;
2897 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
2900 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2903 case GST_FORMAT_PERCENT:
2904 /* we need duration */
2909 /* get our available data relative to the duration */
2912 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
2917 case GST_FORMAT_BYTES:
2927 /* fill out the buffered ranges */
2928 for (queued_ranges = queue->ranges; queued_ranges;
2929 queued_ranges = queued_ranges->next) {
2931 case GST_FORMAT_PERCENT:
2932 if (duration == -1) {
2938 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2939 queued_ranges->offset, duration);
2941 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2942 queued_ranges->writing_pos, duration);
2944 case GST_FORMAT_BYTES:
2945 range_start = queued_ranges->offset;
2946 range_stop = queued_ranges->writing_pos;
2953 if (range_start == range_stop)
2955 GST_DEBUG_OBJECT (queue,
2956 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2957 G_GINT64_FORMAT, range_start, range_stop);
2958 gst_query_add_buffering_range (query, range_start, range_stop);
2961 gst_query_set_buffering_range (query, format, start, stop,
2966 case GST_QUERY_SCHEDULING:
2969 GstSchedulingFlags flags = 0;
2971 if (!gst_pad_peer_query (queue->sinkpad, query))
2974 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
2976 /* we can operate in pull mode when we are using a tempfile */
2977 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2980 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2981 gst_query_set_scheduling (query, flags, 0, -1, 0);
2983 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
2984 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
2988 /* peer handled other queries */
2989 if (!gst_pad_query_default (pad, parent, query))
2999 GST_DEBUG_OBJECT (queue, "failed peer query");
3005 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3007 GstQueue2 *queue = GST_QUEUE2 (element);
3009 /* simply forward to the srcpad query function */
3010 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3015 gst_queue2_update_upstream_size (GstQueue2 * queue)
3017 gint64 upstream_size = -1;
3019 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3021 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3022 queue->upstream_size = upstream_size;
3026 static GstFlowReturn
3027 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3028 guint length, GstBuffer ** buffer)
3033 queue = GST_QUEUE2_CAST (parent);
3035 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3036 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3037 offset = (offset == -1) ? queue->current->reading_pos : offset;
3039 GST_DEBUG_OBJECT (queue,
3040 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3042 /* catch any reads beyond the size of the file here to make sure queue2
3043 * doesn't send seek events beyond the size of the file upstream, since
3044 * that would confuse elements such as souphttpsrc and/or http servers.
3045 * Demuxers often just loop until EOS at the end of the file to figure out
3046 * when they've read all the end-headers or index chunks. */
3047 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3048 gst_queue2_update_upstream_size (queue);
3049 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3050 goto out_unexpected;
3053 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3054 gst_queue2_update_upstream_size (queue);
3055 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3056 length = queue->upstream_size - offset;
3057 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3061 /* FIXME - function will block when the range is not yet available */
3062 ret = gst_queue2_create_read (queue, offset, length, buffer);
3063 GST_QUEUE2_MUTEX_UNLOCK (queue);
3070 ret = queue->srcresult;
3072 GST_DEBUG_OBJECT (queue, "we are flushing");
3073 GST_QUEUE2_MUTEX_UNLOCK (queue);
3078 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3079 GST_QUEUE2_MUTEX_UNLOCK (queue);
3080 return GST_FLOW_EOS;
3084 /* sink currently only operates in push mode */
3086 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3087 GstPadMode mode, gboolean active)
3092 queue = GST_QUEUE2 (parent);
3095 case GST_PAD_MODE_PUSH:
3097 GST_QUEUE2_MUTEX_LOCK (queue);
3098 GST_DEBUG_OBJECT (queue, "activating push mode");
3099 queue->srcresult = GST_FLOW_OK;
3100 queue->sinkresult = GST_FLOW_OK;
3101 queue->is_eos = FALSE;
3102 queue->unexpected = FALSE;
3103 reset_rate_timer (queue);
3104 GST_QUEUE2_MUTEX_UNLOCK (queue);
3106 /* unblock chain function */
3107 GST_QUEUE2_MUTEX_LOCK (queue);
3108 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3109 queue->srcresult = GST_FLOW_FLUSHING;
3110 queue->sinkresult = GST_FLOW_FLUSHING;
3111 GST_QUEUE2_SIGNAL_DEL (queue);
3112 /* Unblock query handler */
3113 queue->last_query = FALSE;
3114 g_cond_signal (&queue->query_handled);
3115 GST_QUEUE2_MUTEX_UNLOCK (queue);
3117 /* wait until it is unblocked and clean up */
3118 GST_PAD_STREAM_LOCK (pad);
3119 GST_QUEUE2_MUTEX_LOCK (queue);
3120 gst_queue2_locked_flush (queue, TRUE, FALSE);
3121 GST_QUEUE2_MUTEX_UNLOCK (queue);
3122 GST_PAD_STREAM_UNLOCK (pad);
3133 /* src operating in push mode, we start a task on the source pad that pushes out
3134 * buffers from the queue */
3136 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3138 gboolean result = FALSE;
3141 queue = GST_QUEUE2 (parent);
3144 GST_QUEUE2_MUTEX_LOCK (queue);
3145 GST_DEBUG_OBJECT (queue, "activating push mode");
3146 queue->srcresult = GST_FLOW_OK;
3147 queue->sinkresult = GST_FLOW_OK;
3148 queue->is_eos = FALSE;
3149 queue->unexpected = FALSE;
3151 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3152 GST_QUEUE2_MUTEX_UNLOCK (queue);
3154 /* unblock loop function */
3155 GST_QUEUE2_MUTEX_LOCK (queue);
3156 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3157 queue->srcresult = GST_FLOW_FLUSHING;
3158 queue->sinkresult = GST_FLOW_FLUSHING;
3159 /* the item add signal will unblock */
3160 GST_QUEUE2_SIGNAL_ADD (queue);
3161 GST_QUEUE2_MUTEX_UNLOCK (queue);
3163 /* step 2, make sure streaming finishes */
3164 result = gst_pad_stop_task (pad);
3170 /* pull mode, downstream will call our getrange function */
3172 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3177 queue = GST_QUEUE2 (parent);
3180 GST_QUEUE2_MUTEX_LOCK (queue);
3181 if (!QUEUE_IS_USING_QUEUE (queue)) {
3182 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3183 /* open the temp file now */
3184 result = gst_queue2_open_temp_location_file (queue);
3185 } else if (!queue->ring_buffer) {
3186 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3187 result = ! !queue->ring_buffer;
3192 GST_DEBUG_OBJECT (queue, "activating pull mode");
3193 init_ranges (queue);
3194 queue->srcresult = GST_FLOW_OK;
3195 queue->sinkresult = GST_FLOW_OK;
3196 queue->is_eos = FALSE;
3197 queue->unexpected = FALSE;
3198 queue->upstream_size = 0;
3200 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3201 /* this is not allowed, we cannot operate in pull mode without a temp
3203 queue->srcresult = GST_FLOW_FLUSHING;
3204 queue->sinkresult = GST_FLOW_FLUSHING;
3207 GST_QUEUE2_MUTEX_UNLOCK (queue);
3209 GST_QUEUE2_MUTEX_LOCK (queue);
3210 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3211 queue->srcresult = GST_FLOW_FLUSHING;
3212 queue->sinkresult = GST_FLOW_FLUSHING;
3213 /* this will unlock getrange */
3214 GST_QUEUE2_SIGNAL_ADD (queue);
3216 GST_QUEUE2_MUTEX_UNLOCK (queue);
3223 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3229 case GST_PAD_MODE_PULL:
3230 res = gst_queue2_src_activate_pull (pad, parent, active);
3232 case GST_PAD_MODE_PUSH:
3233 res = gst_queue2_src_activate_push (pad, parent, active);
3236 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3243 static GstStateChangeReturn
3244 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3247 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3249 queue = GST_QUEUE2 (element);
3251 switch (transition) {
3252 case GST_STATE_CHANGE_NULL_TO_READY:
3254 case GST_STATE_CHANGE_READY_TO_PAUSED:
3255 GST_QUEUE2_MUTEX_LOCK (queue);
3256 if (!QUEUE_IS_USING_QUEUE (queue)) {
3257 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3258 if (!gst_queue2_open_temp_location_file (queue))
3259 ret = GST_STATE_CHANGE_FAILURE;
3261 if (queue->ring_buffer) {
3262 g_free (queue->ring_buffer);
3263 queue->ring_buffer = NULL;
3265 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3266 ret = GST_STATE_CHANGE_FAILURE;
3268 init_ranges (queue);
3270 queue->segment_event_received = FALSE;
3271 queue->starting_segment = NULL;
3272 gst_event_replace (&queue->stream_start_event, NULL);
3273 GST_QUEUE2_MUTEX_UNLOCK (queue);
3275 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3281 if (ret == GST_STATE_CHANGE_FAILURE)
3284 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3286 if (ret == GST_STATE_CHANGE_FAILURE)
3289 switch (transition) {
3290 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3292 case GST_STATE_CHANGE_PAUSED_TO_READY:
3293 GST_QUEUE2_MUTEX_LOCK (queue);
3294 if (!QUEUE_IS_USING_QUEUE (queue)) {
3295 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3296 gst_queue2_close_temp_location_file (queue);
3297 } else if (queue->ring_buffer) {
3298 g_free (queue->ring_buffer);
3299 queue->ring_buffer = NULL;
3301 clean_ranges (queue);
3303 if (queue->starting_segment != NULL) {
3304 gst_event_unref (queue->starting_segment);
3305 queue->starting_segment = NULL;
3307 gst_event_replace (&queue->stream_start_event, NULL);
3308 GST_QUEUE2_MUTEX_UNLOCK (queue);
3310 case GST_STATE_CHANGE_READY_TO_NULL:
3319 /* changing the capacity of the queue must wake up
3320 * the _chain function, it might have more room now
3321 * to store the buffer/event in the queue */
3322 #define QUEUE_CAPACITY_CHANGE(q) \
3323 GST_QUEUE2_SIGNAL_DEL (queue); \
3324 if (queue->use_buffering) \
3325 update_buffering (queue);
3327 /* Changing the minimum required fill level must
3328 * wake up the _loop function as it might now
3329 * be able to preceed.
3331 #define QUEUE_THRESHOLD_CHANGE(q)\
3332 GST_QUEUE2_SIGNAL_ADD (queue);
3335 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3339 /* the element must be stopped in order to do this */
3340 GST_OBJECT_LOCK (queue);
3341 state = GST_STATE (queue);
3342 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3344 GST_OBJECT_UNLOCK (queue);
3346 /* set new location */
3347 g_free (queue->temp_template);
3348 queue->temp_template = g_strdup (template);
3355 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3356 GST_OBJECT_UNLOCK (queue);
3361 gst_queue2_set_property (GObject * object,
3362 guint prop_id, const GValue * value, GParamSpec * pspec)
3364 GstQueue2 *queue = GST_QUEUE2 (object);
3366 /* someone could change levels here, and since this
3367 * affects the get/put funcs, we need to lock for safety. */
3368 GST_QUEUE2_MUTEX_LOCK (queue);
3371 case PROP_MAX_SIZE_BYTES:
3372 queue->max_level.bytes = g_value_get_uint (value);
3373 QUEUE_CAPACITY_CHANGE (queue);
3375 case PROP_MAX_SIZE_BUFFERS:
3376 queue->max_level.buffers = g_value_get_uint (value);
3377 QUEUE_CAPACITY_CHANGE (queue);
3379 case PROP_MAX_SIZE_TIME:
3380 queue->max_level.time = g_value_get_uint64 (value);
3381 /* set rate_time to the same value. We use an extra field in the level
3382 * structure so that we can easily access and compare it */
3383 queue->max_level.rate_time = queue->max_level.time;
3384 QUEUE_CAPACITY_CHANGE (queue);
3386 case PROP_USE_BUFFERING:
3387 queue->use_buffering = g_value_get_boolean (value);
3388 if (!queue->use_buffering && queue->is_buffering) {
3389 GstMessage *msg = gst_message_new_buffering (GST_OBJECT_CAST (queue),
3392 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3393 "posting 100%% message");
3394 queue->is_buffering = FALSE;
3395 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
3398 if (queue->use_buffering) {
3399 queue->is_buffering = TRUE;
3400 update_buffering (queue);
3403 case PROP_USE_RATE_ESTIMATE:
3404 queue->use_rate_estimate = g_value_get_boolean (value);
3406 case PROP_LOW_PERCENT:
3407 queue->low_percent = g_value_get_int (value);
3409 case PROP_HIGH_PERCENT:
3410 queue->high_percent = g_value_get_int (value);
3412 case PROP_TEMP_TEMPLATE:
3413 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3415 case PROP_TEMP_REMOVE:
3416 queue->temp_remove = g_value_get_boolean (value);
3418 case PROP_RING_BUFFER_MAX_SIZE:
3419 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3426 GST_QUEUE2_MUTEX_UNLOCK (queue);
3430 gst_queue2_get_property (GObject * object,
3431 guint prop_id, GValue * value, GParamSpec * pspec)
3433 GstQueue2 *queue = GST_QUEUE2 (object);
3435 GST_QUEUE2_MUTEX_LOCK (queue);
3438 case PROP_CUR_LEVEL_BYTES:
3439 g_value_set_uint (value, queue->cur_level.bytes);
3441 case PROP_CUR_LEVEL_BUFFERS:
3442 g_value_set_uint (value, queue->cur_level.buffers);
3444 case PROP_CUR_LEVEL_TIME:
3445 g_value_set_uint64 (value, queue->cur_level.time);
3447 case PROP_MAX_SIZE_BYTES:
3448 g_value_set_uint (value, queue->max_level.bytes);
3450 case PROP_MAX_SIZE_BUFFERS:
3451 g_value_set_uint (value, queue->max_level.buffers);
3453 case PROP_MAX_SIZE_TIME:
3454 g_value_set_uint64 (value, queue->max_level.time);
3456 case PROP_USE_BUFFERING:
3457 g_value_set_boolean (value, queue->use_buffering);
3459 case PROP_USE_RATE_ESTIMATE:
3460 g_value_set_boolean (value, queue->use_rate_estimate);
3462 case PROP_LOW_PERCENT:
3463 g_value_set_int (value, queue->low_percent);
3465 case PROP_HIGH_PERCENT:
3466 g_value_set_int (value, queue->high_percent);
3468 case PROP_TEMP_TEMPLATE:
3469 g_value_set_string (value, queue->temp_template);
3471 case PROP_TEMP_LOCATION:
3472 g_value_set_string (value, queue->temp_location);
3474 case PROP_TEMP_REMOVE:
3475 g_value_set_boolean (value, queue->temp_remove);
3477 case PROP_RING_BUFFER_MAX_SIZE:
3478 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3481 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3485 GST_QUEUE2_MUTEX_UNLOCK (queue);