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;
897 queue->buffering_iteration++;
903 if (percent == queue->buffering_percent)
906 queue->buffering_percent = percent;
911 GstBufferingMode mode;
912 gint avg_in, avg_out;
913 gint64 buffering_left;
915 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
918 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
920 gst_message_set_buffering_stats (message, mode,
921 avg_in, avg_out, buffering_left);
923 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
928 reset_rate_timer (GstQueue2 * queue)
931 queue->bytes_out = 0;
932 queue->byte_in_rate = 0.0;
933 queue->byte_in_period = 0;
934 queue->byte_out_rate = 0.0;
935 queue->last_in_elapsed = 0.0;
936 queue->last_out_elapsed = 0.0;
937 queue->in_timer_started = FALSE;
938 queue->out_timer_started = FALSE;
941 /* the interval in seconds to recalculate the rate */
942 #define RATE_INTERVAL 0.2
943 /* Tuning for rate estimation. We use a large window for the input rate because
944 * it should be stable when connected to a network. The output rate is less
945 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
946 * therefore adapt more quickly.
947 * However, initial input rate may be subject to a burst, and should therefore
948 * initially also adapt more quickly to changes, and only later on give higher
949 * weight to previous values. */
950 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
951 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
954 update_in_rates (GstQueue2 * queue)
956 gdouble elapsed, period;
957 gdouble byte_in_rate;
959 if (!queue->in_timer_started) {
960 queue->in_timer_started = TRUE;
961 g_timer_start (queue->in_timer);
965 elapsed = g_timer_elapsed (queue->in_timer, NULL);
967 /* recalc after each interval. */
968 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
969 period = elapsed - queue->last_in_elapsed;
971 GST_DEBUG_OBJECT (queue,
972 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
973 period, queue->bytes_in, queue->byte_in_period);
975 byte_in_rate = queue->bytes_in / period;
977 if (queue->byte_in_rate == 0.0)
978 queue->byte_in_rate = byte_in_rate;
980 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
981 (double) queue->byte_in_period, period);
983 /* another data point, cap at 16 for long time running average */
984 if (queue->byte_in_period < 16 * RATE_INTERVAL)
985 queue->byte_in_period += period;
987 /* reset the values to calculate rate over the next interval */
988 queue->last_in_elapsed = elapsed;
992 if (queue->byte_in_rate > 0.0) {
993 queue->cur_level.rate_time =
994 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
996 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
997 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1001 update_out_rates (GstQueue2 * queue)
1003 gdouble elapsed, period;
1004 gdouble byte_out_rate;
1006 if (!queue->out_timer_started) {
1007 queue->out_timer_started = TRUE;
1008 g_timer_start (queue->out_timer);
1012 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1014 /* recalc after each interval. */
1015 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1016 period = elapsed - queue->last_out_elapsed;
1018 GST_DEBUG_OBJECT (queue,
1019 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1021 byte_out_rate = queue->bytes_out / period;
1023 if (queue->byte_out_rate == 0.0)
1024 queue->byte_out_rate = byte_out_rate;
1026 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1028 /* reset the values to calculate rate over the next interval */
1029 queue->last_out_elapsed = elapsed;
1030 queue->bytes_out = 0;
1032 if (queue->byte_in_rate > 0.0) {
1033 queue->cur_level.rate_time =
1034 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1036 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1037 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1041 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1043 guint64 reading_pos, max_reading_pos;
1046 max_reading_pos = range->max_reading_pos;
1048 max_reading_pos = MAX (max_reading_pos, reading_pos);
1050 GST_DEBUG_OBJECT (queue,
1051 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1052 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1053 range->max_reading_pos = max_reading_pos;
1055 update_cur_level (queue, range);
1059 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1064 /* until we receive the FLUSH_STOP from this seek, we skip data */
1065 queue->seeking = TRUE;
1066 GST_QUEUE2_MUTEX_UNLOCK (queue);
1068 debug_ranges (queue);
1070 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1073 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1074 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1075 GST_SEEK_TYPE_NONE, -1);
1077 res = gst_pad_push_event (queue->sinkpad, event);
1078 GST_QUEUE2_MUTEX_LOCK (queue);
1081 /* Between us sending the seek event and re-acquiring the lock, the source
1082 * thread might already have pushed data and moved along the range's
1083 * writing_pos beyond the seek offset. In that case we don't want to set
1084 * the writing position back to the requested seek position, as it would
1085 * cause data to be written to the wrong offset in the file or ring buffer.
1086 * We still do the add_range call to switch the current range to the
1087 * requested range, or create one if one doesn't exist yet. */
1088 queue->current = add_range (queue, offset, FALSE);
1094 /* get the threshold for when we decide to seek rather than wait */
1096 get_seek_threshold (GstQueue2 * queue)
1100 /* FIXME, find a good threshold based on the incoming rate. */
1101 threshold = 1024 * 512;
1103 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1104 threshold = MIN (threshold,
1105 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1110 /* see if there is enough data in the file to read a full buffer */
1112 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1114 GstQueue2Range *range;
1116 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1119 if ((range = find_range (queue, offset))) {
1120 if (queue->current != range) {
1121 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1122 perform_seek_to_offset (queue, range->writing_pos);
1125 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1126 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1128 /* we have a range for offset */
1129 GST_DEBUG_OBJECT (queue,
1130 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1131 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1133 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1136 if (offset + length <= range->writing_pos)
1139 GST_DEBUG_OBJECT (queue,
1140 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1141 (offset + length) - range->writing_pos);
1144 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1145 " len %u", offset, length);
1146 /* we don't have the range, see how far away we are */
1147 if (!queue->is_eos && queue->current) {
1148 guint64 threshold = get_seek_threshold (queue);
1150 if (offset >= queue->current->offset && offset <=
1151 queue->current->writing_pos + threshold) {
1152 GST_INFO_OBJECT (queue,
1153 "requested data is within range, wait for data");
1158 /* too far away, do a seek */
1159 perform_seek_to_offset (queue, offset);
1166 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1167 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1168 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1170 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1173 static GstFlowReturn
1174 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1175 guint8 * dst, gint64 * read_return)
1177 guint8 *ring_buffer;
1180 ring_buffer = queue->ring_buffer;
1182 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1185 /* this should not block */
1186 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1188 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1189 res = fread (dst, 1, length, queue->temp_file);
1191 memcpy (dst, ring_buffer + offset, length);
1195 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1197 if (G_UNLIKELY (res < length)) {
1198 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1199 goto could_not_read;
1200 /* check for errors or EOF */
1201 if (ferror (queue->temp_file))
1202 goto could_not_read;
1203 if (feof (queue->temp_file) && length > 0)
1213 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1214 return GST_FLOW_ERROR;
1218 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1219 return GST_FLOW_ERROR;
1223 GST_DEBUG ("non-regular file hits EOS");
1224 return GST_FLOW_EOS;
1228 static GstFlowReturn
1229 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1230 GstBuffer ** buffer)
1235 guint64 file_offset;
1236 guint block_length, remaining, read_length;
1240 GstFlowReturn ret = GST_FLOW_OK;
1242 /* allocate the output buffer of the requested size */
1243 if (*buffer == NULL)
1244 buf = gst_buffer_new_allocate (NULL, length, NULL);
1248 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1251 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1255 rb_size = queue->ring_buffer_max_size;
1256 max_size = QUEUE_MAX_BYTES (queue);
1259 while (remaining > 0) {
1260 /* configure how much/whether to read */
1261 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1264 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1267 /* calculate how far away the offset is */
1268 if (queue->current->writing_pos > rpos)
1269 level = queue->current->writing_pos - rpos;
1273 GST_DEBUG_OBJECT (queue,
1274 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1275 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1276 rpos, queue->current->writing_pos, level, max_size);
1278 if (level >= max_size) {
1279 /* we don't have the data but if we have a ring buffer that is full, we
1281 GST_DEBUG_OBJECT (queue,
1282 "ring buffer full, reading QUEUE_MAX_BYTES %"
1283 G_GUINT64_FORMAT " bytes", max_size);
1284 read_length = max_size;
1285 } else if (queue->is_eos) {
1286 /* won't get any more data so read any data we have */
1288 GST_DEBUG_OBJECT (queue,
1289 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1291 read_length = level;
1299 if (read_length == 0) {
1300 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1301 GST_DEBUG_OBJECT (queue,
1302 "update current position [%" G_GUINT64_FORMAT "-%"
1303 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1304 update_cur_pos (queue, queue->current, rpos);
1305 GST_QUEUE2_SIGNAL_DEL (queue);
1308 if (queue->use_buffering)
1309 update_buffering (queue);
1311 GST_DEBUG_OBJECT (queue, "waiting for add");
1312 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1316 /* we have the requested data so read it */
1317 read_length = remaining;
1320 /* set range reading_pos to actual reading position for this read */
1321 queue->current->reading_pos = rpos;
1323 /* configure how much and from where to read */
1324 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1326 (queue->current->rb_offset + (rpos -
1327 queue->current->offset)) % rb_size;
1328 if (file_offset + read_length > rb_size) {
1329 block_length = rb_size - file_offset;
1331 block_length = read_length;
1335 block_length = read_length;
1338 /* while we still have data to read, we loop */
1339 while (read_length > 0) {
1343 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1344 data, &read_return);
1345 if (ret != GST_FLOW_OK)
1348 file_offset += read_return;
1349 if (QUEUE_IS_USING_RING_BUFFER (queue))
1350 file_offset %= rb_size;
1352 data += read_return;
1353 read_length -= read_return;
1354 block_length = read_length;
1355 remaining -= read_return;
1357 rpos = (queue->current->reading_pos += read_return);
1358 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1360 GST_QUEUE2_SIGNAL_DEL (queue);
1361 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1364 gst_buffer_unmap (buf, &info);
1365 gst_buffer_resize (buf, 0, length);
1367 GST_BUFFER_OFFSET (buf) = offset;
1368 GST_BUFFER_OFFSET_END (buf) = offset + length;
1377 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1378 gst_buffer_unmap (buf, &info);
1379 if (*buffer == NULL)
1380 gst_buffer_unref (buf);
1381 return GST_FLOW_EOS;
1385 GST_DEBUG_OBJECT (queue, "we are flushing");
1386 gst_buffer_unmap (buf, &info);
1387 if (*buffer == NULL)
1388 gst_buffer_unref (buf);
1389 return GST_FLOW_FLUSHING;
1393 GST_DEBUG_OBJECT (queue, "we have a read error");
1394 gst_buffer_unmap (buf, &info);
1395 if (*buffer == NULL)
1396 gst_buffer_unref (buf);
1401 /* should be called with QUEUE_LOCK */
1402 static GstMiniObject *
1403 gst_queue2_read_item_from_file (GstQueue2 * queue)
1405 GstMiniObject *item;
1407 if (queue->stream_start_event != NULL) {
1408 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1409 queue->stream_start_event = NULL;
1410 } else if (queue->starting_segment != NULL) {
1411 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1412 queue->starting_segment = NULL;
1415 GstBuffer *buffer = NULL;
1416 guint64 reading_pos;
1418 reading_pos = queue->current->reading_pos;
1421 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1426 item = GST_MINI_OBJECT_CAST (buffer);
1429 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1439 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1440 * the temp filename. */
1442 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1447 if (queue->temp_file)
1448 goto already_opened;
1450 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1452 /* If temp_template was set, allocate a filename and open that filen */
1455 if (queue->temp_template == NULL)
1458 /* make copy of the template, we don't want to change this */
1459 name = g_strdup (queue->temp_template);
1460 fd = g_mkstemp (name);
1462 goto mkstemp_failed;
1464 /* open the file for update/writing */
1465 queue->temp_file = fdopen (fd, "wb+");
1466 /* error creating file */
1467 if (queue->temp_file == NULL)
1470 g_free (queue->temp_location);
1471 queue->temp_location = name;
1473 GST_QUEUE2_MUTEX_UNLOCK (queue);
1475 /* we can't emit the notify with the lock */
1476 g_object_notify (G_OBJECT (queue), "temp-location");
1478 GST_QUEUE2_MUTEX_LOCK (queue);
1480 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1487 GST_DEBUG_OBJECT (queue, "temp file was already open");
1492 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1493 (_("No Temp directory specified.")), (NULL));
1498 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1499 (_("Could not create temp file \"%s\"."), queue->temp_template),
1506 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1507 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1516 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1519 if (queue->temp_file == NULL)
1522 GST_DEBUG_OBJECT (queue, "closing temp file");
1524 fflush (queue->temp_file);
1525 fclose (queue->temp_file);
1527 if (queue->temp_remove)
1528 remove (queue->temp_location);
1530 queue->temp_file = NULL;
1531 clean_ranges (queue);
1535 gst_queue2_flush_temp_file (GstQueue2 * queue)
1537 if (queue->temp_file == NULL)
1540 GST_DEBUG_OBJECT (queue, "flushing temp file");
1542 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1546 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1548 if (!QUEUE_IS_USING_QUEUE (queue)) {
1549 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1550 gst_queue2_flush_temp_file (queue);
1551 init_ranges (queue);
1553 while (!g_queue_is_empty (&queue->queue)) {
1554 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1556 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1557 && GST_EVENT_IS_STICKY (qitem->item)
1558 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1559 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1560 gst_pad_store_sticky_event (queue->srcpad,
1561 GST_EVENT_CAST (qitem->item));
1564 /* Then lose another reference because we are supposed to destroy that
1565 data when flushing */
1566 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1567 gst_mini_object_unref (qitem->item);
1568 g_slice_free (GstQueue2Item, qitem);
1571 queue->last_query = FALSE;
1572 g_cond_signal (&queue->query_handled);
1573 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1574 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1575 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1576 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1577 queue->sink_tainted = queue->src_tainted = TRUE;
1578 if (queue->starting_segment != NULL)
1579 gst_event_unref (queue->starting_segment);
1580 queue->starting_segment = NULL;
1581 queue->segment_event_received = FALSE;
1582 gst_event_replace (&queue->stream_start_event, NULL);
1584 /* we deleted a lot of something */
1585 GST_QUEUE2_SIGNAL_DEL (queue);
1589 gst_queue2_wait_free_space (GstQueue2 * queue)
1591 /* We make space available if we're "full" according to whatever
1592 * the user defined as "full". */
1593 if (gst_queue2_is_filled (queue)) {
1596 /* pause the timer while we wait. The fact that we are waiting does not mean
1597 * the byterate on the input pad is lower */
1598 if ((started = queue->in_timer_started))
1599 g_timer_stop (queue->in_timer);
1601 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1602 "queue is full, waiting for free space");
1604 /* Wait for space to be available, we could be unlocked because of a flush. */
1605 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1607 while (gst_queue2_is_filled (queue));
1609 /* and continue if we were running before */
1611 g_timer_continue (queue->in_timer);
1618 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1624 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1627 guint8 *data, *ring_buffer;
1628 guint size, rb_size;
1629 guint64 writing_pos, new_writing_pos;
1630 GstQueue2Range *range, *prev, *next;
1631 gboolean do_seek = FALSE;
1633 if (QUEUE_IS_USING_RING_BUFFER (queue))
1634 writing_pos = queue->current->rb_writing_pos;
1636 writing_pos = queue->current->writing_pos;
1637 ring_buffer = queue->ring_buffer;
1638 rb_size = queue->ring_buffer_max_size;
1640 gst_buffer_map (buffer, &info, GST_MAP_READ);
1645 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1649 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1650 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1651 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1652 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1653 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1659 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1662 /* calculate the space in the ring buffer not used by data from
1663 * the current range */
1664 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1665 /* wait until there is some free space */
1666 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1668 /* get the amount of space we have */
1669 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1671 /* calculate if we need to split or if we can write the entire
1673 to_write = MIN (size, space);
1675 /* the writing position in the ring buffer after writing (part
1676 * or all of) the buffer */
1677 new_writing_pos = (writing_pos + to_write) % rb_size;
1680 range = queue->ranges;
1682 /* if we need to overwrite data in the ring buffer, we need to
1685 * warning: this code is complicated and includes some
1686 * simplifications - pen, paper and diagrams for the cases
1689 guint64 range_data_start, range_data_end;
1690 GstQueue2Range *range_to_destroy = NULL;
1692 range_data_start = range->rb_offset;
1693 range_data_end = range->rb_writing_pos;
1695 /* handle the special case where the range has no data in it */
1696 if (range->writing_pos == range->offset) {
1697 if (range != queue->current) {
1698 GST_DEBUG_OBJECT (queue,
1699 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1700 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1702 range_to_destroy = range;
1704 prev->next = range->next;
1709 if (range_data_end > range_data_start) {
1710 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1713 if (new_writing_pos > range_data_start) {
1714 if (new_writing_pos >= range_data_end) {
1715 GST_DEBUG_OBJECT (queue,
1716 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1717 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1719 range_to_destroy = range;
1721 prev->next = range->next;
1723 GST_DEBUG_OBJECT (queue,
1724 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1725 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1726 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1727 range->offset + new_writing_pos - range_data_start,
1729 range->offset += (new_writing_pos - range_data_start);
1730 range->rb_offset = new_writing_pos;
1734 guint64 new_wpos_virt = writing_pos + to_write;
1736 if (new_wpos_virt <= range_data_start)
1739 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1740 GST_DEBUG_OBJECT (queue,
1741 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1742 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1744 range_to_destroy = range;
1746 prev->next = range->next;
1748 GST_DEBUG_OBJECT (queue,
1749 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1750 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1751 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1752 range->offset + new_writing_pos - range_data_start,
1754 range->offset += (new_wpos_virt - range_data_start);
1755 range->rb_offset = new_writing_pos;
1760 if (!range_to_destroy)
1763 range = range->next;
1764 if (range_to_destroy) {
1765 if (range_to_destroy == queue->ranges)
1766 queue->ranges = range;
1767 g_slice_free (GstQueue2Range, range_to_destroy);
1768 range_to_destroy = NULL;
1773 new_writing_pos = writing_pos + to_write;
1776 if (QUEUE_IS_USING_TEMP_FILE (queue)
1777 && FSEEK_FILE (queue->temp_file, writing_pos))
1780 if (new_writing_pos > writing_pos) {
1781 GST_INFO_OBJECT (queue,
1782 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1783 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1784 queue->current->writing_pos, queue->current->rb_writing_pos);
1785 /* either not using ring buffer or no wrapping, just write */
1786 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1787 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1790 memcpy (ring_buffer + writing_pos, data, to_write);
1793 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1794 /* try to merge with next range */
1795 while ((next = queue->current->next)) {
1796 GST_INFO_OBJECT (queue,
1797 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1798 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1799 if (new_writing_pos < next->offset)
1802 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1805 /* remove the group */
1806 queue->current->next = next->next;
1808 /* We use the threshold to decide if we want to do a seek or simply
1809 * read the data again. If there is not so much data in the range we
1810 * prefer to avoid to seek and read it again. */
1811 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1812 /* the new range had more data than the threshold, it's worth keeping
1813 * it and doing a seek. */
1814 new_writing_pos = next->writing_pos;
1817 g_slice_free (GstQueue2Range, next);
1819 goto update_and_signal;
1823 guint block_one, block_two;
1825 block_one = rb_size - writing_pos;
1826 block_two = to_write - block_one;
1828 if (block_one > 0) {
1829 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1830 /* write data to end of ring buffer */
1831 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1832 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1835 memcpy (ring_buffer + writing_pos, data, block_one);
1839 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1842 if (block_two > 0) {
1843 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1844 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1845 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1848 memcpy (ring_buffer, data + block_one, block_two);
1854 /* update the writing positions */
1856 GST_INFO_OBJECT (queue,
1857 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1858 to_write, writing_pos, size);
1860 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1862 queue->current->writing_pos += to_write;
1863 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1865 queue->current->writing_pos = writing_pos = new_writing_pos;
1868 perform_seek_to_offset (queue, new_writing_pos);
1870 update_cur_level (queue, queue->current);
1872 /* update the buffering status */
1873 if (queue->use_buffering)
1874 update_buffering (queue);
1876 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1877 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1879 GST_QUEUE2_SIGNAL_ADD (queue);
1882 gst_buffer_unmap (buffer, &info);
1889 GST_DEBUG_OBJECT (queue, "we are flushing");
1890 gst_buffer_unmap (buffer, &info);
1891 /* FIXME - GST_FLOW_EOS ? */
1896 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1897 gst_buffer_unmap (buffer, &info);
1904 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1908 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1909 (_("Error while writing to download file.")),
1910 ("%s", g_strerror (errno)));
1913 gst_buffer_unmap (buffer, &info);
1919 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1921 GstQueue2 *queue = q;
1923 GST_TRACE_OBJECT (queue,
1924 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1925 gst_buffer_get_size (*buf));
1927 if (!gst_queue2_create_write (queue, *buf)) {
1928 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1935 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1937 guint *p_size = data;
1940 buf_size = gst_buffer_get_size (*buf);
1941 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1942 *p_size += buf_size;
1946 /* enqueue an item an update the level stats */
1948 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1949 GstQueue2ItemType item_type)
1951 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1955 buffer = GST_BUFFER_CAST (item);
1956 size = gst_buffer_get_size (buffer);
1958 /* add buffer to the statistics */
1959 if (QUEUE_IS_USING_QUEUE (queue)) {
1960 queue->cur_level.buffers++;
1961 queue->cur_level.bytes += size;
1963 queue->bytes_in += size;
1965 /* apply new buffer to segment stats */
1966 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1967 /* update the byterate stats */
1968 update_in_rates (queue);
1970 if (!QUEUE_IS_USING_QUEUE (queue)) {
1971 /* FIXME - check return value? */
1972 gst_queue2_create_write (queue, buffer);
1974 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1975 GstBufferList *buffer_list;
1978 buffer_list = GST_BUFFER_LIST_CAST (item);
1980 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1981 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1983 /* add buffer to the statistics */
1984 if (QUEUE_IS_USING_QUEUE (queue)) {
1985 queue->cur_level.buffers++;
1986 queue->cur_level.bytes += size;
1988 queue->bytes_in += size;
1990 /* apply new buffer to segment stats */
1991 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1993 /* update the byterate stats */
1994 update_in_rates (queue);
1996 if (!QUEUE_IS_USING_QUEUE (queue)) {
1997 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1999 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2002 event = GST_EVENT_CAST (item);
2004 switch (GST_EVENT_TYPE (event)) {
2006 /* Zero the thresholds, this makes sure the queue is completely
2007 * filled and we can read all data from the queue. */
2008 GST_DEBUG_OBJECT (queue, "we have EOS");
2009 queue->is_eos = TRUE;
2011 case GST_EVENT_SEGMENT:
2012 apply_segment (queue, event, &queue->sink_segment, TRUE);
2013 /* This is our first new segment, we hold it
2014 * as we can't save it on the temp file */
2015 if (!QUEUE_IS_USING_QUEUE (queue)) {
2016 if (queue->segment_event_received)
2017 goto unexpected_event;
2019 queue->segment_event_received = TRUE;
2020 if (queue->starting_segment != NULL)
2021 gst_event_unref (queue->starting_segment);
2022 queue->starting_segment = event;
2025 /* a new segment allows us to accept more buffers if we got EOS
2026 * from downstream */
2027 queue->unexpected = FALSE;
2029 case GST_EVENT_STREAM_START:
2030 if (!QUEUE_IS_USING_QUEUE (queue)) {
2031 gst_event_replace (&queue->stream_start_event, event);
2032 gst_event_unref (event);
2036 case GST_EVENT_CAPS:{
2039 gst_event_parse_caps (event, &caps);
2040 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2042 if (!QUEUE_IS_USING_QUEUE (queue)) {
2043 GST_LOG ("Dropping caps event, not using queue");
2044 gst_event_unref (event);
2050 if (!QUEUE_IS_USING_QUEUE (queue))
2051 goto unexpected_event;
2054 } else if (GST_IS_QUERY (item)) {
2055 /* Can't happen as we check that in the caller */
2056 if (!QUEUE_IS_USING_QUEUE (queue))
2057 g_assert_not_reached ();
2059 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2060 item, GST_OBJECT_NAME (queue));
2061 /* we can't really unref since we don't know what it is */
2066 /* update the buffering status */
2067 if (queue->use_buffering)
2068 update_buffering (queue);
2070 if (QUEUE_IS_USING_QUEUE (queue)) {
2071 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2072 qitem->type = item_type;
2074 g_queue_push_tail (&queue->queue, qitem);
2076 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2079 GST_QUEUE2_SIGNAL_ADD (queue);
2088 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2089 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2090 GST_OBJECT_NAME (queue));
2091 gst_event_unref (GST_EVENT_CAST (item));
2096 /* dequeue an item from the queue and update level stats */
2097 static GstMiniObject *
2098 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2100 GstMiniObject *item;
2102 if (!QUEUE_IS_USING_QUEUE (queue)) {
2103 item = gst_queue2_read_item_from_file (queue);
2105 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2111 g_slice_free (GstQueue2Item, qitem);
2117 if (GST_IS_BUFFER (item)) {
2121 buffer = GST_BUFFER_CAST (item);
2122 size = gst_buffer_get_size (buffer);
2123 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2125 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2126 "retrieved buffer %p from queue", buffer);
2128 if (QUEUE_IS_USING_QUEUE (queue)) {
2129 queue->cur_level.buffers--;
2130 queue->cur_level.bytes -= size;
2132 queue->bytes_out += size;
2134 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2135 /* update the byterate stats */
2136 update_out_rates (queue);
2137 /* update the buffering */
2138 if (queue->use_buffering)
2139 update_buffering (queue);
2141 } else if (GST_IS_EVENT (item)) {
2142 GstEvent *event = GST_EVENT_CAST (item);
2144 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2146 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2147 "retrieved event %p from queue", event);
2149 switch (GST_EVENT_TYPE (event)) {
2151 /* queue is empty now that we dequeued the EOS */
2152 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2154 case GST_EVENT_SEGMENT:
2155 apply_segment (queue, event, &queue->src_segment, FALSE);
2160 } else if (GST_IS_BUFFER_LIST (item)) {
2161 GstBufferList *buffer_list;
2164 buffer_list = GST_BUFFER_LIST_CAST (item);
2165 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2166 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2168 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2169 "retrieved buffer list %p from queue", buffer_list);
2171 if (QUEUE_IS_USING_QUEUE (queue)) {
2172 queue->cur_level.buffers--;
2173 queue->cur_level.bytes -= size;
2175 queue->bytes_out += size;
2177 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2178 /* update the byterate stats */
2179 update_out_rates (queue);
2180 /* update the buffering */
2181 if (queue->use_buffering)
2182 update_buffering (queue);
2183 } else if (GST_IS_QUERY (item)) {
2184 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2185 "retrieved query %p from queue", item);
2186 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2189 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2190 item, GST_OBJECT_NAME (queue));
2192 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2194 GST_QUEUE2_SIGNAL_DEL (queue);
2201 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2207 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2210 gboolean ret = TRUE;
2213 queue = GST_QUEUE2 (parent);
2215 switch (GST_EVENT_TYPE (event)) {
2216 case GST_EVENT_FLUSH_START:
2218 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2219 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2221 ret = gst_pad_push_event (queue->srcpad, event);
2223 /* now unblock the chain function */
2224 GST_QUEUE2_MUTEX_LOCK (queue);
2225 queue->srcresult = GST_FLOW_FLUSHING;
2226 queue->sinkresult = GST_FLOW_FLUSHING;
2227 /* unblock the loop and chain functions */
2228 GST_QUEUE2_SIGNAL_ADD (queue);
2229 GST_QUEUE2_SIGNAL_DEL (queue);
2230 queue->last_query = FALSE;
2231 g_cond_signal (&queue->query_handled);
2232 GST_QUEUE2_MUTEX_UNLOCK (queue);
2234 /* make sure it pauses, this should happen since we sent
2235 * flush_start downstream. */
2236 gst_pad_pause_task (queue->srcpad);
2237 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2239 GST_QUEUE2_MUTEX_LOCK (queue);
2240 /* flush the sink pad */
2241 queue->sinkresult = GST_FLOW_FLUSHING;
2242 GST_QUEUE2_SIGNAL_DEL (queue);
2243 queue->last_query = FALSE;
2244 g_cond_signal (&queue->query_handled);
2245 GST_QUEUE2_MUTEX_UNLOCK (queue);
2247 gst_event_unref (event);
2251 case GST_EVENT_FLUSH_STOP:
2253 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2255 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2257 ret = gst_pad_push_event (queue->srcpad, event);
2259 GST_QUEUE2_MUTEX_LOCK (queue);
2260 gst_queue2_locked_flush (queue, FALSE, TRUE);
2261 queue->srcresult = GST_FLOW_OK;
2262 queue->sinkresult = GST_FLOW_OK;
2263 queue->is_eos = FALSE;
2264 queue->unexpected = FALSE;
2265 queue->seeking = FALSE;
2266 /* reset rate counters */
2267 reset_rate_timer (queue);
2268 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2269 queue->srcpad, NULL);
2270 GST_QUEUE2_MUTEX_UNLOCK (queue);
2272 GST_QUEUE2_MUTEX_LOCK (queue);
2273 queue->segment_event_received = FALSE;
2274 queue->is_eos = FALSE;
2275 queue->unexpected = FALSE;
2276 queue->sinkresult = GST_FLOW_OK;
2277 queue->seeking = FALSE;
2278 GST_QUEUE2_MUTEX_UNLOCK (queue);
2280 gst_event_unref (event);
2285 if (GST_EVENT_IS_SERIALIZED (event)) {
2286 /* serialized events go in the queue */
2287 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2288 /* refuse more events on EOS */
2291 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2292 GST_QUEUE2_MUTEX_UNLOCK (queue);
2294 /* non-serialized events are passed upstream. */
2295 ret = gst_pad_push_event (queue->srcpad, event);
2304 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2305 GST_QUEUE2_MUTEX_UNLOCK (queue);
2306 gst_event_unref (event);
2311 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2312 GST_QUEUE2_MUTEX_UNLOCK (queue);
2313 gst_event_unref (event);
2319 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2325 queue = GST_QUEUE2 (parent);
2327 switch (GST_QUERY_TYPE (query)) {
2329 if (GST_QUERY_IS_SERIALIZED (query)) {
2330 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2331 /* serialized events go in the queue. We need to be certain that we
2332 * don't cause deadlocks waiting for the query return value. We check if
2333 * the queue is empty (nothing is blocking downstream and the query can
2334 * be pushed for sure) or we are not buffering. If we are buffering,
2335 * the pipeline waits to unblock downstream until our queue fills up
2336 * completely, which can not happen if we block on the query..
2337 * Therefore we only potentially block when we are not buffering. */
2338 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2339 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2340 || !queue->use_buffering)) {
2341 gst_queue2_locked_enqueue (queue, query, GST_QUEUE2_ITEM_TYPE_QUERY);
2343 STATUS (queue, queue->sinkpad, "wait for QUERY");
2344 g_cond_wait (&queue->query_handled, &queue->qlock);
2345 if (queue->sinkresult != GST_FLOW_OK)
2347 res = queue->last_query;
2349 GST_DEBUG_OBJECT (queue,
2350 "refusing query, we are not using the queue");
2353 GST_QUEUE2_MUTEX_UNLOCK (queue);
2355 res = gst_pad_query_default (pad, parent, query);
2364 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2365 GST_QUEUE2_MUTEX_UNLOCK (queue);
2371 gst_queue2_is_empty (GstQueue2 * queue)
2373 /* never empty on EOS */
2377 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2378 return queue->current->writing_pos <= queue->current->max_reading_pos;
2380 if (queue->queue.length == 0)
2388 gst_queue2_is_filled (GstQueue2 * queue)
2392 /* always filled on EOS */
2396 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2397 (queue->cur_level.format) >= ((alt_max) ? \
2398 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2400 /* if using a ring buffer we're filled if all ring buffer space is used
2401 * _by the current range_ */
2402 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2403 guint64 rb_size = queue->ring_buffer_max_size;
2404 GST_DEBUG_OBJECT (queue,
2405 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2406 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2407 return CHECK_FILLED (bytes, rb_size);
2410 /* if using file, we're never filled if we don't have EOS */
2411 if (QUEUE_IS_USING_TEMP_FILE (queue))
2414 /* we are never filled when we have no buffers at all */
2415 if (queue->cur_level.buffers == 0)
2418 /* we are filled if one of the current levels exceeds the max */
2419 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2420 || CHECK_FILLED (time, 0);
2422 /* if we need to, use the rate estimate to check against the max time we are
2423 * allowed to queue */
2424 if (queue->use_rate_estimate)
2425 res |= CHECK_FILLED (rate_time, 0);
2431 static GstFlowReturn
2432 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2433 GstMiniObject * item, GstQueue2ItemType item_type)
2435 /* we have to lock the queue since we span threads */
2436 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2437 /* when we received EOS, we refuse more data */
2440 /* when we received unexpected from downstream, refuse more buffers */
2441 if (queue->unexpected)
2442 goto out_unexpected;
2444 /* while we didn't receive the newsegment, we're seeking and we skip data */
2448 if (!gst_queue2_wait_free_space (queue))
2451 /* put buffer in queue now */
2452 gst_queue2_locked_enqueue (queue, item, item_type);
2453 GST_QUEUE2_MUTEX_UNLOCK (queue);
2457 /* special conditions */
2460 GstFlowReturn ret = queue->sinkresult;
2462 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2463 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2464 GST_QUEUE2_MUTEX_UNLOCK (queue);
2465 gst_mini_object_unref (item);
2471 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2472 GST_QUEUE2_MUTEX_UNLOCK (queue);
2473 gst_mini_object_unref (item);
2475 return GST_FLOW_EOS;
2479 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2480 GST_QUEUE2_MUTEX_UNLOCK (queue);
2481 gst_mini_object_unref (item);
2487 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2488 GST_QUEUE2_MUTEX_UNLOCK (queue);
2489 gst_mini_object_unref (item);
2491 return GST_FLOW_EOS;
2495 static GstFlowReturn
2496 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2500 queue = GST_QUEUE2 (parent);
2502 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2503 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2504 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2505 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2506 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2508 return gst_queue2_chain_buffer_or_buffer_list (queue,
2509 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2512 static GstFlowReturn
2513 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2514 GstBufferList * buffer_list)
2518 queue = GST_QUEUE2 (parent);
2520 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2521 "received buffer list %p", buffer_list);
2523 return gst_queue2_chain_buffer_or_buffer_list (queue,
2524 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2527 static GstMiniObject *
2528 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2530 GstMiniObject *data;
2532 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2534 /* stop pushing buffers, we dequeue all items until we see an item that we
2535 * can push again, which is EOS or SEGMENT. If there is nothing in the
2536 * queue we can push, we set a flag to make the sinkpad refuse more
2537 * buffers with an EOS return value until we receive something
2538 * pushable again or we get flushed. */
2539 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2540 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2541 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2542 "dropping EOS buffer %p", data);
2543 gst_buffer_unref (GST_BUFFER_CAST (data));
2544 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2545 GstEvent *event = GST_EVENT_CAST (data);
2546 GstEventType type = GST_EVENT_TYPE (event);
2548 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2549 /* we found a pushable item in the queue, push it out */
2550 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2551 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2554 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2555 "dropping EOS event %p", event);
2556 gst_event_unref (event);
2557 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2558 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2559 "dropping EOS buffer list %p", data);
2560 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2561 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2562 queue->last_query = FALSE;
2563 g_cond_signal (&queue->query_handled);
2564 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2567 /* no more items in the queue. Set the unexpected flag so that upstream
2568 * make us refuse any more buffers on the sinkpad. Since we will still
2569 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2570 * task function does not shut down. */
2571 queue->unexpected = TRUE;
2575 /* dequeue an item from the queue an push it downstream. This functions returns
2576 * the result of the push. */
2577 static GstFlowReturn
2578 gst_queue2_push_one (GstQueue2 * queue)
2580 GstFlowReturn result = queue->srcresult;
2581 GstMiniObject *data;
2582 GstQueue2ItemType item_type;
2584 data = gst_queue2_locked_dequeue (queue, &item_type);
2589 GST_QUEUE2_MUTEX_UNLOCK (queue);
2591 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2594 buffer = GST_BUFFER_CAST (data);
2596 result = gst_pad_push (queue->srcpad, buffer);
2598 /* need to check for srcresult here as well */
2599 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2600 if (result == GST_FLOW_EOS) {
2601 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2604 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2605 * to the caller so that the task function does not shut down */
2606 result = GST_FLOW_OK;
2608 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2609 GstEvent *event = GST_EVENT_CAST (data);
2610 GstEventType type = GST_EVENT_TYPE (event);
2612 gst_pad_push_event (queue->srcpad, event);
2614 /* if we're EOS, return EOS so that the task pauses. */
2615 if (type == GST_EVENT_EOS) {
2616 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2617 "pushed EOS event %p, return EOS", event);
2618 result = GST_FLOW_EOS;
2621 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2622 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2623 GstBufferList *buffer_list;
2625 buffer_list = GST_BUFFER_LIST_CAST (data);
2627 result = gst_pad_push_list (queue->srcpad, buffer_list);
2629 /* need to check for srcresult here as well */
2630 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2631 if (result == GST_FLOW_EOS) {
2632 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2635 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2636 * to the caller so that the task function does not shut down */
2637 result = GST_FLOW_OK;
2639 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2640 GstQuery *query = GST_QUERY_CAST (data);
2642 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2643 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2644 "did query %p, return %d", query, queue->last_query);
2645 g_cond_signal (&queue->query_handled);
2646 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2647 result = GST_FLOW_OK;
2654 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2655 "exit because we have no item in the queue");
2656 return GST_FLOW_ERROR;
2660 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2661 return GST_FLOW_FLUSHING;
2665 /* called repeatedly with @pad as the source pad. This function should push out
2666 * data to the peer element. */
2668 gst_queue2_loop (GstPad * pad)
2673 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2675 /* have to lock for thread-safety */
2676 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2678 if (gst_queue2_is_empty (queue)) {
2681 /* pause the timer while we wait. The fact that we are waiting does not mean
2682 * the byterate on the output pad is lower */
2683 if ((started = queue->out_timer_started))
2684 g_timer_stop (queue->out_timer);
2686 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2687 "queue is empty, waiting for new data");
2689 /* Wait for data to be available, we could be unlocked because of a flush. */
2690 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2692 while (gst_queue2_is_empty (queue));
2694 /* and continue if we were running before */
2696 g_timer_continue (queue->out_timer);
2698 ret = gst_queue2_push_one (queue);
2699 queue->srcresult = ret;
2700 queue->sinkresult = ret;
2701 if (ret != GST_FLOW_OK)
2704 GST_QUEUE2_MUTEX_UNLOCK (queue);
2711 gboolean eos = queue->is_eos;
2712 GstFlowReturn ret = queue->srcresult;
2714 gst_pad_pause_task (queue->srcpad);
2715 GST_QUEUE2_MUTEX_UNLOCK (queue);
2716 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2717 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2718 /* let app know about us giving up if upstream is not expected to do so */
2719 /* EOS is already taken care of elsewhere */
2720 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2721 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2722 (_("Internal data flow error.")),
2723 ("streaming task paused, reason %s (%d)",
2724 gst_flow_get_name (ret), ret));
2725 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2732 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2734 gboolean res = TRUE;
2735 GstQueue2 *queue = GST_QUEUE2 (parent);
2737 #ifndef GST_DISABLE_GST_DEBUG
2738 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2739 event, GST_EVENT_TYPE_NAME (event));
2742 switch (GST_EVENT_TYPE (event)) {
2743 case GST_EVENT_FLUSH_START:
2744 if (QUEUE_IS_USING_QUEUE (queue)) {
2745 /* just forward upstream */
2746 res = gst_pad_push_event (queue->sinkpad, event);
2748 /* now unblock the getrange function */
2749 GST_QUEUE2_MUTEX_LOCK (queue);
2750 GST_DEBUG_OBJECT (queue, "flushing");
2751 queue->srcresult = GST_FLOW_FLUSHING;
2752 GST_QUEUE2_SIGNAL_ADD (queue);
2753 GST_QUEUE2_MUTEX_UNLOCK (queue);
2755 /* when using a temp file, we eat the event */
2757 gst_event_unref (event);
2760 case GST_EVENT_FLUSH_STOP:
2761 if (QUEUE_IS_USING_QUEUE (queue)) {
2762 /* just forward upstream */
2763 res = gst_pad_push_event (queue->sinkpad, event);
2765 /* now unblock the getrange function */
2766 GST_QUEUE2_MUTEX_LOCK (queue);
2767 queue->srcresult = GST_FLOW_OK;
2768 GST_QUEUE2_MUTEX_UNLOCK (queue);
2770 /* when using a temp file, we eat the event */
2772 gst_event_unref (event);
2775 case GST_EVENT_RECONFIGURE:
2776 GST_QUEUE2_MUTEX_LOCK (queue);
2777 /* assume downstream is linked now and try to push again */
2778 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2779 queue->srcresult = GST_FLOW_OK;
2780 queue->sinkresult = GST_FLOW_OK;
2781 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2782 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2786 GST_QUEUE2_MUTEX_UNLOCK (queue);
2788 res = gst_pad_push_event (queue->sinkpad, event);
2791 res = gst_pad_push_event (queue->sinkpad, event);
2799 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2803 queue = GST_QUEUE2 (parent);
2805 switch (GST_QUERY_TYPE (query)) {
2806 case GST_QUERY_POSITION:
2811 if (!gst_pad_peer_query (queue->sinkpad, query))
2814 /* get peer position */
2815 gst_query_parse_position (query, &format, &peer_pos);
2817 /* FIXME: this code assumes that there's no discont in the queue */
2819 case GST_FORMAT_BYTES:
2820 peer_pos -= queue->cur_level.bytes;
2822 case GST_FORMAT_TIME:
2823 peer_pos -= queue->cur_level.time;
2826 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2827 "know how to adjust value", gst_format_get_name (format));
2830 /* set updated position */
2831 gst_query_set_position (query, format, peer_pos);
2834 case GST_QUERY_DURATION:
2836 GST_DEBUG_OBJECT (queue, "doing peer query");
2838 if (!gst_pad_peer_query (queue->sinkpad, query))
2841 GST_DEBUG_OBJECT (queue, "peer query success");
2844 case GST_QUERY_BUFFERING:
2847 gboolean is_buffering;
2848 GstBufferingMode mode;
2849 gint avg_in, avg_out;
2850 gint64 buffering_left;
2852 GST_DEBUG_OBJECT (queue, "query buffering");
2854 get_buffering_percent (queue, &is_buffering, &percent);
2855 gst_query_set_buffering_percent (query, is_buffering, percent);
2857 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
2859 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
2862 if (!QUEUE_IS_USING_QUEUE (queue)) {
2863 /* add ranges for download and ringbuffer buffering */
2865 gint64 start, stop, range_start, range_stop;
2866 guint64 writing_pos;
2867 gint64 estimated_total;
2869 gboolean peer_res, is_eos;
2870 GstQueue2Range *queued_ranges;
2872 /* we need a current download region */
2873 if (queue->current == NULL)
2876 writing_pos = queue->current->writing_pos;
2877 is_eos = queue->is_eos;
2880 /* we're EOS, we know the duration in bytes now */
2882 duration = writing_pos;
2884 /* get duration of upstream in bytes */
2885 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2886 GST_FORMAT_BYTES, &duration);
2889 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
2890 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
2892 /* calculate remaining and total download time */
2893 if (peer_res && avg_in > 0.0)
2894 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
2896 estimated_total = -1;
2898 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
2901 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2904 case GST_FORMAT_PERCENT:
2905 /* we need duration */
2910 /* get our available data relative to the duration */
2913 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
2918 case GST_FORMAT_BYTES:
2928 /* fill out the buffered ranges */
2929 for (queued_ranges = queue->ranges; queued_ranges;
2930 queued_ranges = queued_ranges->next) {
2932 case GST_FORMAT_PERCENT:
2933 if (duration == -1) {
2939 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2940 queued_ranges->offset, duration);
2942 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2943 queued_ranges->writing_pos, duration);
2945 case GST_FORMAT_BYTES:
2946 range_start = queued_ranges->offset;
2947 range_stop = queued_ranges->writing_pos;
2954 if (range_start == range_stop)
2956 GST_DEBUG_OBJECT (queue,
2957 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2958 G_GINT64_FORMAT, range_start, range_stop);
2959 gst_query_add_buffering_range (query, range_start, range_stop);
2962 gst_query_set_buffering_range (query, format, start, stop,
2967 case GST_QUERY_SCHEDULING:
2970 GstSchedulingFlags flags = 0;
2972 if (!gst_pad_peer_query (queue->sinkpad, query))
2975 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
2977 /* we can operate in pull mode when we are using a tempfile */
2978 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2981 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2982 gst_query_set_scheduling (query, flags, 0, -1, 0);
2984 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
2985 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
2989 /* peer handled other queries */
2990 if (!gst_pad_query_default (pad, parent, query))
3000 GST_DEBUG_OBJECT (queue, "failed peer query");
3006 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3008 GstQueue2 *queue = GST_QUEUE2 (element);
3010 /* simply forward to the srcpad query function */
3011 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3016 gst_queue2_update_upstream_size (GstQueue2 * queue)
3018 gint64 upstream_size = -1;
3020 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3022 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3023 queue->upstream_size = upstream_size;
3027 static GstFlowReturn
3028 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3029 guint length, GstBuffer ** buffer)
3034 queue = GST_QUEUE2_CAST (parent);
3036 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3037 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3038 offset = (offset == -1) ? queue->current->reading_pos : offset;
3040 GST_DEBUG_OBJECT (queue,
3041 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3043 /* catch any reads beyond the size of the file here to make sure queue2
3044 * doesn't send seek events beyond the size of the file upstream, since
3045 * that would confuse elements such as souphttpsrc and/or http servers.
3046 * Demuxers often just loop until EOS at the end of the file to figure out
3047 * when they've read all the end-headers or index chunks. */
3048 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3049 gst_queue2_update_upstream_size (queue);
3050 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3051 goto out_unexpected;
3054 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3055 gst_queue2_update_upstream_size (queue);
3056 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3057 length = queue->upstream_size - offset;
3058 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3062 /* FIXME - function will block when the range is not yet available */
3063 ret = gst_queue2_create_read (queue, offset, length, buffer);
3064 GST_QUEUE2_MUTEX_UNLOCK (queue);
3071 ret = queue->srcresult;
3073 GST_DEBUG_OBJECT (queue, "we are flushing");
3074 GST_QUEUE2_MUTEX_UNLOCK (queue);
3079 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3080 GST_QUEUE2_MUTEX_UNLOCK (queue);
3081 return GST_FLOW_EOS;
3085 /* sink currently only operates in push mode */
3087 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3088 GstPadMode mode, gboolean active)
3093 queue = GST_QUEUE2 (parent);
3096 case GST_PAD_MODE_PUSH:
3098 GST_QUEUE2_MUTEX_LOCK (queue);
3099 GST_DEBUG_OBJECT (queue, "activating push mode");
3100 queue->srcresult = GST_FLOW_OK;
3101 queue->sinkresult = GST_FLOW_OK;
3102 queue->is_eos = FALSE;
3103 queue->unexpected = FALSE;
3104 reset_rate_timer (queue);
3105 GST_QUEUE2_MUTEX_UNLOCK (queue);
3107 /* unblock chain function */
3108 GST_QUEUE2_MUTEX_LOCK (queue);
3109 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3110 queue->srcresult = GST_FLOW_FLUSHING;
3111 queue->sinkresult = GST_FLOW_FLUSHING;
3112 GST_QUEUE2_SIGNAL_DEL (queue);
3113 /* Unblock query handler */
3114 queue->last_query = FALSE;
3115 g_cond_signal (&queue->query_handled);
3116 GST_QUEUE2_MUTEX_UNLOCK (queue);
3118 /* wait until it is unblocked and clean up */
3119 GST_PAD_STREAM_LOCK (pad);
3120 GST_QUEUE2_MUTEX_LOCK (queue);
3121 gst_queue2_locked_flush (queue, TRUE, FALSE);
3122 GST_QUEUE2_MUTEX_UNLOCK (queue);
3123 GST_PAD_STREAM_UNLOCK (pad);
3134 /* src operating in push mode, we start a task on the source pad that pushes out
3135 * buffers from the queue */
3137 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3139 gboolean result = FALSE;
3142 queue = GST_QUEUE2 (parent);
3145 GST_QUEUE2_MUTEX_LOCK (queue);
3146 GST_DEBUG_OBJECT (queue, "activating push mode");
3147 queue->srcresult = GST_FLOW_OK;
3148 queue->sinkresult = GST_FLOW_OK;
3149 queue->is_eos = FALSE;
3150 queue->unexpected = FALSE;
3152 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3153 GST_QUEUE2_MUTEX_UNLOCK (queue);
3155 /* unblock loop function */
3156 GST_QUEUE2_MUTEX_LOCK (queue);
3157 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3158 queue->srcresult = GST_FLOW_FLUSHING;
3159 queue->sinkresult = GST_FLOW_FLUSHING;
3160 /* the item add signal will unblock */
3161 GST_QUEUE2_SIGNAL_ADD (queue);
3162 GST_QUEUE2_MUTEX_UNLOCK (queue);
3164 /* step 2, make sure streaming finishes */
3165 result = gst_pad_stop_task (pad);
3171 /* pull mode, downstream will call our getrange function */
3173 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3178 queue = GST_QUEUE2 (parent);
3181 GST_QUEUE2_MUTEX_LOCK (queue);
3182 if (!QUEUE_IS_USING_QUEUE (queue)) {
3183 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3184 /* open the temp file now */
3185 result = gst_queue2_open_temp_location_file (queue);
3186 } else if (!queue->ring_buffer) {
3187 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3188 result = ! !queue->ring_buffer;
3193 GST_DEBUG_OBJECT (queue, "activating pull mode");
3194 init_ranges (queue);
3195 queue->srcresult = GST_FLOW_OK;
3196 queue->sinkresult = GST_FLOW_OK;
3197 queue->is_eos = FALSE;
3198 queue->unexpected = FALSE;
3199 queue->upstream_size = 0;
3201 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3202 /* this is not allowed, we cannot operate in pull mode without a temp
3204 queue->srcresult = GST_FLOW_FLUSHING;
3205 queue->sinkresult = GST_FLOW_FLUSHING;
3208 GST_QUEUE2_MUTEX_UNLOCK (queue);
3210 GST_QUEUE2_MUTEX_LOCK (queue);
3211 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3212 queue->srcresult = GST_FLOW_FLUSHING;
3213 queue->sinkresult = GST_FLOW_FLUSHING;
3214 /* this will unlock getrange */
3215 GST_QUEUE2_SIGNAL_ADD (queue);
3217 GST_QUEUE2_MUTEX_UNLOCK (queue);
3224 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3230 case GST_PAD_MODE_PULL:
3231 res = gst_queue2_src_activate_pull (pad, parent, active);
3233 case GST_PAD_MODE_PUSH:
3234 res = gst_queue2_src_activate_push (pad, parent, active);
3237 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3244 static GstStateChangeReturn
3245 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3248 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3250 queue = GST_QUEUE2 (element);
3252 switch (transition) {
3253 case GST_STATE_CHANGE_NULL_TO_READY:
3255 case GST_STATE_CHANGE_READY_TO_PAUSED:
3256 GST_QUEUE2_MUTEX_LOCK (queue);
3257 if (!QUEUE_IS_USING_QUEUE (queue)) {
3258 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3259 if (!gst_queue2_open_temp_location_file (queue))
3260 ret = GST_STATE_CHANGE_FAILURE;
3262 if (queue->ring_buffer) {
3263 g_free (queue->ring_buffer);
3264 queue->ring_buffer = NULL;
3266 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3267 ret = GST_STATE_CHANGE_FAILURE;
3269 init_ranges (queue);
3271 queue->segment_event_received = FALSE;
3272 queue->starting_segment = NULL;
3273 gst_event_replace (&queue->stream_start_event, NULL);
3274 GST_QUEUE2_MUTEX_UNLOCK (queue);
3276 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3282 if (ret == GST_STATE_CHANGE_FAILURE)
3285 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3287 if (ret == GST_STATE_CHANGE_FAILURE)
3290 switch (transition) {
3291 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3293 case GST_STATE_CHANGE_PAUSED_TO_READY:
3294 GST_QUEUE2_MUTEX_LOCK (queue);
3295 if (!QUEUE_IS_USING_QUEUE (queue)) {
3296 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3297 gst_queue2_close_temp_location_file (queue);
3298 } else if (queue->ring_buffer) {
3299 g_free (queue->ring_buffer);
3300 queue->ring_buffer = NULL;
3302 clean_ranges (queue);
3304 if (queue->starting_segment != NULL) {
3305 gst_event_unref (queue->starting_segment);
3306 queue->starting_segment = NULL;
3308 gst_event_replace (&queue->stream_start_event, NULL);
3309 GST_QUEUE2_MUTEX_UNLOCK (queue);
3311 case GST_STATE_CHANGE_READY_TO_NULL:
3320 /* changing the capacity of the queue must wake up
3321 * the _chain function, it might have more room now
3322 * to store the buffer/event in the queue */
3323 #define QUEUE_CAPACITY_CHANGE(q) \
3324 GST_QUEUE2_SIGNAL_DEL (queue); \
3325 if (queue->use_buffering) \
3326 update_buffering (queue);
3328 /* Changing the minimum required fill level must
3329 * wake up the _loop function as it might now
3330 * be able to preceed.
3332 #define QUEUE_THRESHOLD_CHANGE(q)\
3333 GST_QUEUE2_SIGNAL_ADD (queue);
3336 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3340 /* the element must be stopped in order to do this */
3341 GST_OBJECT_LOCK (queue);
3342 state = GST_STATE (queue);
3343 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3345 GST_OBJECT_UNLOCK (queue);
3347 /* set new location */
3348 g_free (queue->temp_template);
3349 queue->temp_template = g_strdup (template);
3356 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3357 GST_OBJECT_UNLOCK (queue);
3362 gst_queue2_set_property (GObject * object,
3363 guint prop_id, const GValue * value, GParamSpec * pspec)
3365 GstQueue2 *queue = GST_QUEUE2 (object);
3367 /* someone could change levels here, and since this
3368 * affects the get/put funcs, we need to lock for safety. */
3369 GST_QUEUE2_MUTEX_LOCK (queue);
3372 case PROP_MAX_SIZE_BYTES:
3373 queue->max_level.bytes = g_value_get_uint (value);
3374 QUEUE_CAPACITY_CHANGE (queue);
3376 case PROP_MAX_SIZE_BUFFERS:
3377 queue->max_level.buffers = g_value_get_uint (value);
3378 QUEUE_CAPACITY_CHANGE (queue);
3380 case PROP_MAX_SIZE_TIME:
3381 queue->max_level.time = g_value_get_uint64 (value);
3382 /* set rate_time to the same value. We use an extra field in the level
3383 * structure so that we can easily access and compare it */
3384 queue->max_level.rate_time = queue->max_level.time;
3385 QUEUE_CAPACITY_CHANGE (queue);
3387 case PROP_USE_BUFFERING:
3388 queue->use_buffering = g_value_get_boolean (value);
3390 case PROP_USE_RATE_ESTIMATE:
3391 queue->use_rate_estimate = g_value_get_boolean (value);
3393 case PROP_LOW_PERCENT:
3394 queue->low_percent = g_value_get_int (value);
3396 case PROP_HIGH_PERCENT:
3397 queue->high_percent = g_value_get_int (value);
3399 case PROP_TEMP_TEMPLATE:
3400 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3402 case PROP_TEMP_REMOVE:
3403 queue->temp_remove = g_value_get_boolean (value);
3405 case PROP_RING_BUFFER_MAX_SIZE:
3406 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3413 GST_QUEUE2_MUTEX_UNLOCK (queue);
3417 gst_queue2_get_property (GObject * object,
3418 guint prop_id, GValue * value, GParamSpec * pspec)
3420 GstQueue2 *queue = GST_QUEUE2 (object);
3422 GST_QUEUE2_MUTEX_LOCK (queue);
3425 case PROP_CUR_LEVEL_BYTES:
3426 g_value_set_uint (value, queue->cur_level.bytes);
3428 case PROP_CUR_LEVEL_BUFFERS:
3429 g_value_set_uint (value, queue->cur_level.buffers);
3431 case PROP_CUR_LEVEL_TIME:
3432 g_value_set_uint64 (value, queue->cur_level.time);
3434 case PROP_MAX_SIZE_BYTES:
3435 g_value_set_uint (value, queue->max_level.bytes);
3437 case PROP_MAX_SIZE_BUFFERS:
3438 g_value_set_uint (value, queue->max_level.buffers);
3440 case PROP_MAX_SIZE_TIME:
3441 g_value_set_uint64 (value, queue->max_level.time);
3443 case PROP_USE_BUFFERING:
3444 g_value_set_boolean (value, queue->use_buffering);
3446 case PROP_USE_RATE_ESTIMATE:
3447 g_value_set_boolean (value, queue->use_rate_estimate);
3449 case PROP_LOW_PERCENT:
3450 g_value_set_int (value, queue->low_percent);
3452 case PROP_HIGH_PERCENT:
3453 g_value_set_int (value, queue->high_percent);
3455 case PROP_TEMP_TEMPLATE:
3456 g_value_set_string (value, queue->temp_template);
3458 case PROP_TEMP_LOCATION:
3459 g_value_set_string (value, queue->temp_location);
3461 case PROP_TEMP_REMOVE:
3462 g_value_set_boolean (value, queue->temp_remove);
3464 case PROP_RING_BUFFER_MAX_SIZE:
3465 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3468 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3472 GST_QUEUE2_MUTEX_UNLOCK (queue);