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);
261 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
262 GST_QUEUE2_ITEM_TYPE_BUFFER,
263 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
264 GST_QUEUE2_ITEM_TYPE_EVENT,
265 GST_QUEUE2_ITEM_TYPE_QUERY
270 GstQueue2ItemType type;
274 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
277 gst_queue2_class_init (GstQueue2Class * klass)
279 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
280 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
282 gobject_class->set_property = gst_queue2_set_property;
283 gobject_class->get_property = gst_queue2_get_property;
286 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
287 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
288 "Current amount of data in the queue (bytes)",
289 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
291 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
292 "Current number of buffers in the queue",
293 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
295 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
296 "Current amount of data in the queue (in ns)",
297 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
300 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
301 "Max. amount of data in the queue (bytes, 0=disable)",
302 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
303 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
305 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
306 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
307 DEFAULT_MAX_SIZE_BUFFERS,
308 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
310 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
311 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
312 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
315 g_param_spec_boolean ("use-buffering", "Use buffering",
316 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
317 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
319 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
320 "Estimate the bitrate of the stream to calculate time level",
321 DEFAULT_USE_RATE_ESTIMATE,
322 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
324 g_param_spec_int ("low-percent", "Low percent",
325 "Low threshold for buffering to start. Only used if use-buffering is True",
326 0, 100, DEFAULT_LOW_PERCENT,
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
329 g_param_spec_int ("high-percent", "High percent",
330 "High threshold for buffering to finish. Only used if use-buffering is True",
331 0, 100, DEFAULT_HIGH_PERCENT,
332 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
334 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
335 g_param_spec_string ("temp-template", "Temporary File Template",
336 "File template to store temporary files in, should contain directory "
337 "and XXXXXX. (NULL == disabled)",
338 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
341 g_param_spec_string ("temp-location", "Temporary File Location",
342 "Location to store temporary files in (Only read this property, "
343 "use temp-template to configure the name template)",
344 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
347 * GstQueue2:temp-remove
349 * When temp-template is set, remove the temporary file when going to READY.
351 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
352 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
353 "Remove the temp-location after use",
354 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 * GstQueue2:ring-buffer-max-size
359 * The maximum size of the ring buffer in bytes. If set to 0, the ring
360 * buffer is disabled. Default 0.
362 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
363 g_param_spec_uint64 ("ring-buffer-max-size",
364 "Max. ring buffer size (bytes)",
365 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
366 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
367 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
369 /* set several parent class virtual functions */
370 gobject_class->finalize = gst_queue2_finalize;
372 gst_element_class_add_pad_template (gstelement_class,
373 gst_static_pad_template_get (&srctemplate));
374 gst_element_class_add_pad_template (gstelement_class,
375 gst_static_pad_template_get (&sinktemplate));
377 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
380 "Erik Walthinsen <omega@cse.ogi.edu>, "
381 "Wim Taymans <wim.taymans@gmail.com>");
383 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
384 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
388 gst_queue2_init (GstQueue2 * queue)
390 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
392 gst_pad_set_chain_function (queue->sinkpad,
393 GST_DEBUG_FUNCPTR (gst_queue2_chain));
394 gst_pad_set_chain_list_function (queue->sinkpad,
395 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
396 gst_pad_set_activatemode_function (queue->sinkpad,
397 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
398 gst_pad_set_event_function (queue->sinkpad,
399 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
400 gst_pad_set_query_function (queue->sinkpad,
401 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
402 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
403 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
405 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
407 gst_pad_set_activatemode_function (queue->srcpad,
408 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
409 gst_pad_set_getrange_function (queue->srcpad,
410 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
411 gst_pad_set_event_function (queue->srcpad,
412 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
413 gst_pad_set_query_function (queue->srcpad,
414 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
415 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
416 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
419 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
420 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
421 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
422 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
423 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
424 queue->use_buffering = DEFAULT_USE_BUFFERING;
425 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
426 queue->low_percent = DEFAULT_LOW_PERCENT;
427 queue->high_percent = DEFAULT_HIGH_PERCENT;
429 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
430 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
432 queue->sinktime = GST_CLOCK_TIME_NONE;
433 queue->srctime = GST_CLOCK_TIME_NONE;
434 queue->sink_tainted = TRUE;
435 queue->src_tainted = TRUE;
437 queue->srcresult = GST_FLOW_FLUSHING;
438 queue->sinkresult = GST_FLOW_FLUSHING;
439 queue->is_eos = FALSE;
440 queue->in_timer = g_timer_new ();
441 queue->out_timer = g_timer_new ();
443 g_mutex_init (&queue->qlock);
444 queue->waiting_add = FALSE;
445 g_cond_init (&queue->item_add);
446 queue->waiting_del = FALSE;
447 g_cond_init (&queue->item_del);
448 g_queue_init (&queue->queue);
450 g_cond_init (&queue->query_handled);
451 queue->last_query = FALSE;
453 queue->buffering_percent = 100;
455 /* tempfile related */
456 queue->temp_template = NULL;
457 queue->temp_location = NULL;
458 queue->temp_remove = DEFAULT_TEMP_REMOVE;
460 queue->ring_buffer = NULL;
461 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
463 GST_DEBUG_OBJECT (queue,
464 "initialized queue's not_empty & not_full conditions");
467 /* called only once, as opposed to dispose */
469 gst_queue2_finalize (GObject * object)
471 GstQueue2 *queue = GST_QUEUE2 (object);
473 GST_DEBUG_OBJECT (queue, "finalizing queue");
475 while (!g_queue_is_empty (&queue->queue)) {
476 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
478 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
479 gst_mini_object_unref (qitem->item);
480 g_slice_free (GstQueue2Item, qitem);
483 queue->last_query = FALSE;
484 g_queue_clear (&queue->queue);
485 g_mutex_clear (&queue->qlock);
486 g_cond_clear (&queue->item_add);
487 g_cond_clear (&queue->item_del);
488 g_cond_clear (&queue->query_handled);
489 g_timer_destroy (queue->in_timer);
490 g_timer_destroy (queue->out_timer);
492 /* temp_file path cleanup */
493 g_free (queue->temp_template);
494 g_free (queue->temp_location);
496 G_OBJECT_CLASS (parent_class)->finalize (object);
500 debug_ranges (GstQueue2 * queue)
502 GstQueue2Range *walk;
504 for (walk = queue->ranges; walk; walk = walk->next) {
505 GST_DEBUG_OBJECT (queue,
506 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
507 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
508 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
509 walk->rb_writing_pos, walk->reading_pos,
510 walk == queue->current ? "**y**" : " n ");
514 /* clear all the downloaded ranges */
516 clean_ranges (GstQueue2 * queue)
518 GST_DEBUG_OBJECT (queue, "clean queue ranges");
520 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
521 queue->ranges = NULL;
522 queue->current = NULL;
525 /* find a range that contains @offset or NULL when nothing does */
526 static GstQueue2Range *
527 find_range (GstQueue2 * queue, guint64 offset)
529 GstQueue2Range *range = NULL;
530 GstQueue2Range *walk;
532 /* first do a quick check for the current range */
533 for (walk = queue->ranges; walk; walk = walk->next) {
534 if (offset >= walk->offset && offset <= walk->writing_pos) {
535 /* we can reuse an existing range */
541 GST_DEBUG_OBJECT (queue,
542 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
543 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
545 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
551 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
553 guint64 max_reading_pos, writing_pos;
555 writing_pos = range->writing_pos;
556 max_reading_pos = range->max_reading_pos;
558 if (writing_pos > max_reading_pos)
559 queue->cur_level.bytes = writing_pos - max_reading_pos;
561 queue->cur_level.bytes = 0;
564 /* make a new range for @offset or reuse an existing range */
565 static GstQueue2Range *
566 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
568 GstQueue2Range *range, *prev, *next;
570 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
572 if ((range = find_range (queue, offset))) {
573 GST_DEBUG_OBJECT (queue,
574 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
576 if (update_existing && range->writing_pos != offset) {
577 GST_DEBUG_OBJECT (queue, "updating range writing position to "
578 "%" G_GUINT64_FORMAT, offset);
579 range->writing_pos = offset;
582 GST_DEBUG_OBJECT (queue,
583 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
585 range = g_slice_new0 (GstQueue2Range);
586 range->offset = offset;
587 /* we want to write to the next location in the ring buffer */
588 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
589 range->writing_pos = offset;
590 range->rb_writing_pos = range->rb_offset;
591 range->reading_pos = offset;
592 range->max_reading_pos = offset;
596 next = queue->ranges;
598 if (next->offset > offset) {
599 /* insert before next */
600 GST_DEBUG_OBJECT (queue,
601 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
613 queue->ranges = range;
615 debug_ranges (queue);
617 /* update the stats for this range */
618 update_cur_level (queue, range);
624 /* clear and init the download ranges for offset 0 */
626 init_ranges (GstQueue2 * queue)
628 GST_DEBUG_OBJECT (queue, "init queue ranges");
630 /* get rid of all the current ranges */
631 clean_ranges (queue);
632 /* make a range for offset 0 */
633 queue->current = add_range (queue, 0, TRUE);
636 /* calculate the diff between running time on the sink and src of the queue.
637 * This is the total amount of time in the queue. */
639 update_time_level (GstQueue2 * queue)
641 if (queue->sink_tainted) {
643 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
644 queue->sink_segment.position);
645 queue->sink_tainted = FALSE;
648 if (queue->src_tainted) {
650 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
651 queue->src_segment.position);
652 queue->src_tainted = FALSE;
655 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
656 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
658 if (queue->sinktime != GST_CLOCK_TIME_NONE
659 && queue->srctime != GST_CLOCK_TIME_NONE
660 && queue->sinktime >= queue->srctime)
661 queue->cur_level.time = queue->sinktime - queue->srctime;
663 queue->cur_level.time = 0;
666 /* take a SEGMENT event and apply the values to segment, updating the time
669 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
672 gst_event_copy_segment (event, segment);
674 if (segment->format == GST_FORMAT_BYTES) {
675 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
676 /* start is where we'll be getting from and as such writing next */
677 queue->current = add_range (queue, segment->start, TRUE);
681 /* now configure the values, we use these to track timestamps on the
683 if (segment->format != GST_FORMAT_TIME) {
684 /* non-time format, pretent the current time segment is closed with a
685 * 0 start and unknown stop time. */
686 segment->format = GST_FORMAT_TIME;
692 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
695 queue->sink_tainted = TRUE;
697 queue->src_tainted = TRUE;
699 /* segment can update the time level of the queue */
700 update_time_level (queue);
703 /* take a buffer and update segment, updating the time level of the queue. */
705 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
708 GstClockTime duration, timestamp;
710 timestamp = GST_BUFFER_TIMESTAMP (buffer);
711 duration = GST_BUFFER_DURATION (buffer);
713 /* if no timestamp is set, assume it's continuous with the previous
715 if (timestamp == GST_CLOCK_TIME_NONE)
716 timestamp = segment->position;
719 if (duration != GST_CLOCK_TIME_NONE)
720 timestamp += duration;
722 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
723 GST_TIME_ARGS (timestamp));
725 segment->position = timestamp;
728 queue->sink_tainted = TRUE;
730 queue->src_tainted = TRUE;
732 /* calc diff with other end */
733 update_time_level (queue);
737 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
739 GstClockTime *timestamp = data;
741 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
742 " duration %" GST_TIME_FORMAT, idx,
743 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
744 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
746 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
747 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
749 if (GST_BUFFER_DURATION_IS_VALID (*buf))
750 *timestamp += GST_BUFFER_DURATION (*buf);
752 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
756 /* take a buffer list and update segment, updating the time level of the queue */
758 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
759 GstSegment * segment, gboolean is_sink)
761 GstClockTime timestamp;
763 /* if no timestamp is set, assume it's continuous with the previous time */
764 timestamp = segment->position;
766 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
768 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
769 GST_TIME_ARGS (timestamp));
771 segment->position = timestamp;
774 queue->sink_tainted = TRUE;
776 queue->src_tainted = TRUE;
778 /* calc diff with other end */
779 update_time_level (queue);
783 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
786 gboolean post = FALSE;
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));
820 if (queue->is_buffering) {
822 /* if we were buffering see if we reached the high watermark */
823 if (perc >= queue->high_percent)
824 queue->is_buffering = FALSE;
826 /* we were not buffering, check if we need to start buffering if we drop
827 * below the low threshold */
828 if (perc < queue->low_percent) {
829 queue->is_buffering = TRUE;
830 queue->buffering_iteration++;
836 *is_buffering = queue->is_buffering;
838 /* scale to high percent so that it becomes the 100% mark */
839 perc = perc * 100 / queue->high_percent;
845 if (perc == queue->buffering_percent)
848 queue->buffering_percent = perc;
853 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
860 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
861 gint * avg_in, gint * avg_out, gint64 * buffering_left)
864 if (!QUEUE_IS_USING_QUEUE (queue)) {
865 if (QUEUE_IS_USING_RING_BUFFER (queue))
866 *mode = GST_BUFFERING_TIMESHIFT;
868 *mode = GST_BUFFERING_DOWNLOAD;
870 *mode = GST_BUFFERING_STREAM;
875 *avg_in = queue->byte_in_rate;
877 *avg_out = queue->byte_out_rate;
879 if (buffering_left) {
880 *buffering_left = (percent == 100 ? 0 : -1);
882 if (queue->use_rate_estimate) {
885 max = queue->max_level.rate_time;
886 cur = queue->cur_level.rate_time;
888 if (percent != 100 && max > cur)
889 *buffering_left = (max - cur) / 1000000;
895 update_buffering (GstQueue2 * queue)
898 gboolean post = FALSE;
900 post = get_buffering_percent (queue, NULL, &percent);
904 GstBufferingMode mode;
905 gint avg_in, avg_out;
906 gint64 buffering_left;
908 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
911 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
913 gst_message_set_buffering_stats (message, mode,
914 avg_in, avg_out, buffering_left);
916 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
921 reset_rate_timer (GstQueue2 * queue)
924 queue->bytes_out = 0;
925 queue->byte_in_rate = 0.0;
926 queue->byte_in_period = 0;
927 queue->byte_out_rate = 0.0;
928 queue->last_in_elapsed = 0.0;
929 queue->last_out_elapsed = 0.0;
930 queue->in_timer_started = FALSE;
931 queue->out_timer_started = FALSE;
934 /* the interval in seconds to recalculate the rate */
935 #define RATE_INTERVAL 0.2
936 /* Tuning for rate estimation. We use a large window for the input rate because
937 * it should be stable when connected to a network. The output rate is less
938 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
939 * therefore adapt more quickly.
940 * However, initial input rate may be subject to a burst, and should therefore
941 * initially also adapt more quickly to changes, and only later on give higher
942 * weight to previous values. */
943 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
944 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
947 update_in_rates (GstQueue2 * queue)
949 gdouble elapsed, period;
950 gdouble byte_in_rate;
952 if (!queue->in_timer_started) {
953 queue->in_timer_started = TRUE;
954 g_timer_start (queue->in_timer);
958 elapsed = g_timer_elapsed (queue->in_timer, NULL);
960 /* recalc after each interval. */
961 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
962 period = elapsed - queue->last_in_elapsed;
964 GST_DEBUG_OBJECT (queue,
965 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
966 period, queue->bytes_in, queue->byte_in_period);
968 byte_in_rate = queue->bytes_in / period;
970 if (queue->byte_in_rate == 0.0)
971 queue->byte_in_rate = byte_in_rate;
973 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
974 (double) queue->byte_in_period, period);
976 /* another data point, cap at 16 for long time running average */
977 if (queue->byte_in_period < 16 * RATE_INTERVAL)
978 queue->byte_in_period += period;
980 /* reset the values to calculate rate over the next interval */
981 queue->last_in_elapsed = elapsed;
985 if (queue->byte_in_rate > 0.0) {
986 queue->cur_level.rate_time =
987 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
989 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
990 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
994 update_out_rates (GstQueue2 * queue)
996 gdouble elapsed, period;
997 gdouble byte_out_rate;
999 if (!queue->out_timer_started) {
1000 queue->out_timer_started = TRUE;
1001 g_timer_start (queue->out_timer);
1005 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1007 /* recalc after each interval. */
1008 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1009 period = elapsed - queue->last_out_elapsed;
1011 GST_DEBUG_OBJECT (queue,
1012 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1014 byte_out_rate = queue->bytes_out / period;
1016 if (queue->byte_out_rate == 0.0)
1017 queue->byte_out_rate = byte_out_rate;
1019 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1021 /* reset the values to calculate rate over the next interval */
1022 queue->last_out_elapsed = elapsed;
1023 queue->bytes_out = 0;
1025 if (queue->byte_in_rate > 0.0) {
1026 queue->cur_level.rate_time =
1027 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1029 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1030 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1034 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1036 guint64 reading_pos, max_reading_pos;
1039 max_reading_pos = range->max_reading_pos;
1041 max_reading_pos = MAX (max_reading_pos, reading_pos);
1043 GST_DEBUG_OBJECT (queue,
1044 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1045 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1046 range->max_reading_pos = max_reading_pos;
1048 update_cur_level (queue, range);
1052 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1057 /* until we receive the FLUSH_STOP from this seek, we skip data */
1058 queue->seeking = TRUE;
1059 GST_QUEUE2_MUTEX_UNLOCK (queue);
1061 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1064 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1065 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1066 GST_SEEK_TYPE_NONE, -1);
1068 res = gst_pad_push_event (queue->sinkpad, event);
1069 GST_QUEUE2_MUTEX_LOCK (queue);
1072 /* Between us sending the seek event and re-acquiring the lock, the source
1073 * thread might already have pushed data and moved along the range's
1074 * writing_pos beyond the seek offset. In that case we don't want to set
1075 * the writing position back to the requested seek position, as it would
1076 * cause data to be written to the wrong offset in the file or ring buffer.
1077 * We still do the add_range call to switch the current range to the
1078 * requested range, or create one if one doesn't exist yet. */
1079 queue->current = add_range (queue, offset, FALSE);
1085 /* see if there is enough data in the file to read a full buffer */
1087 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1089 GstQueue2Range *range;
1091 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1094 if ((range = find_range (queue, offset))) {
1095 if (queue->current != range) {
1096 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1097 perform_seek_to_offset (queue, range->writing_pos);
1100 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1101 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1103 /* we have a range for offset */
1104 GST_DEBUG_OBJECT (queue,
1105 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1106 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1108 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1111 if (offset + length <= range->writing_pos)
1114 GST_DEBUG_OBJECT (queue,
1115 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1116 (offset + length) - range->writing_pos);
1119 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1120 " len %u", offset, length);
1121 /* we don't have the range, see how far away we are */
1122 if (!queue->is_eos && queue->current) {
1123 /* FIXME, find a good threshold based on the incoming rate. */
1124 guint64 threshold = 1024 * 512;
1126 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1129 distance = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1130 /* don't wait for the complete buffer to fill */
1131 distance = MIN (distance, threshold);
1133 if (offset >= queue->current->offset && offset <=
1134 queue->current->writing_pos + distance) {
1135 GST_INFO_OBJECT (queue,
1136 "requested data is within range, wait for data");
1139 } else if (offset < queue->current->writing_pos + threshold) {
1140 update_cur_pos (queue, queue->current, offset + length);
1141 GST_INFO_OBJECT (queue, "wait for data");
1146 /* too far away, do a seek */
1147 perform_seek_to_offset (queue, offset);
1154 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1155 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1156 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1158 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1161 static GstFlowReturn
1162 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1163 guint8 * dst, gint64 * read_return)
1165 guint8 *ring_buffer;
1168 ring_buffer = queue->ring_buffer;
1170 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1173 /* this should not block */
1174 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1176 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1177 res = fread (dst, 1, length, queue->temp_file);
1179 memcpy (dst, ring_buffer + offset, length);
1183 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1185 if (G_UNLIKELY (res < length)) {
1186 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1187 goto could_not_read;
1188 /* check for errors or EOF */
1189 if (ferror (queue->temp_file))
1190 goto could_not_read;
1191 if (feof (queue->temp_file) && length > 0)
1201 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1202 return GST_FLOW_ERROR;
1206 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1207 return GST_FLOW_ERROR;
1211 GST_DEBUG ("non-regular file hits EOS");
1212 return GST_FLOW_EOS;
1216 static GstFlowReturn
1217 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1218 GstBuffer ** buffer)
1223 guint64 file_offset;
1224 guint block_length, remaining, read_length;
1228 GstFlowReturn ret = GST_FLOW_OK;
1230 /* allocate the output buffer of the requested size */
1231 if (*buffer == NULL)
1232 buf = gst_buffer_new_allocate (NULL, length, NULL);
1236 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1239 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1243 rb_size = queue->ring_buffer_max_size;
1244 max_size = QUEUE_MAX_BYTES (queue);
1247 while (remaining > 0) {
1248 /* configure how much/whether to read */
1249 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1252 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1255 /* calculate how far away the offset is */
1256 if (queue->current->writing_pos > rpos)
1257 level = queue->current->writing_pos - rpos;
1261 GST_DEBUG_OBJECT (queue,
1262 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1263 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1264 rpos, queue->current->writing_pos, level, max_size);
1266 if (level >= max_size) {
1267 /* we don't have the data but if we have a ring buffer that is full, we
1269 GST_DEBUG_OBJECT (queue,
1270 "ring buffer full, reading QUEUE_MAX_BYTES %"
1271 G_GUINT64_FORMAT " bytes", max_size);
1272 read_length = max_size;
1273 } else if (queue->is_eos) {
1274 /* won't get any more data so read any data we have */
1276 GST_DEBUG_OBJECT (queue,
1277 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1279 read_length = level;
1287 if (read_length == 0) {
1288 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1289 GST_DEBUG_OBJECT (queue,
1290 "update current position [%" G_GUINT64_FORMAT "-%"
1291 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1292 update_cur_pos (queue, queue->current, rpos);
1293 GST_QUEUE2_SIGNAL_DEL (queue);
1295 GST_DEBUG_OBJECT (queue, "waiting for add");
1296 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1300 /* we have the requested data so read it */
1301 read_length = remaining;
1304 /* set range reading_pos to actual reading position for this read */
1305 queue->current->reading_pos = rpos;
1307 /* configure how much and from where to read */
1308 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1310 (queue->current->rb_offset + (rpos -
1311 queue->current->offset)) % rb_size;
1312 if (file_offset + read_length > rb_size) {
1313 block_length = rb_size - file_offset;
1315 block_length = read_length;
1319 block_length = read_length;
1322 /* while we still have data to read, we loop */
1323 while (read_length > 0) {
1327 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1328 data, &read_return);
1329 if (ret != GST_FLOW_OK)
1332 file_offset += read_return;
1333 if (QUEUE_IS_USING_RING_BUFFER (queue))
1334 file_offset %= rb_size;
1336 data += read_return;
1337 read_length -= read_return;
1338 block_length = read_length;
1339 remaining -= read_return;
1341 rpos = (queue->current->reading_pos += read_return);
1342 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1344 GST_QUEUE2_SIGNAL_DEL (queue);
1345 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1348 gst_buffer_unmap (buf, &info);
1349 gst_buffer_resize (buf, 0, length);
1351 GST_BUFFER_OFFSET (buf) = offset;
1352 GST_BUFFER_OFFSET_END (buf) = offset + length;
1361 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1362 gst_buffer_unmap (buf, &info);
1363 if (*buffer == NULL)
1364 gst_buffer_unref (buf);
1365 return GST_FLOW_EOS;
1369 GST_DEBUG_OBJECT (queue, "we are flushing");
1370 gst_buffer_unmap (buf, &info);
1371 if (*buffer == NULL)
1372 gst_buffer_unref (buf);
1373 return GST_FLOW_FLUSHING;
1377 GST_DEBUG_OBJECT (queue, "we have a read error");
1378 gst_buffer_unmap (buf, &info);
1379 if (*buffer == NULL)
1380 gst_buffer_unref (buf);
1385 /* should be called with QUEUE_LOCK */
1386 static GstMiniObject *
1387 gst_queue2_read_item_from_file (GstQueue2 * queue)
1389 GstMiniObject *item;
1391 if (queue->stream_start_event != NULL) {
1392 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1393 queue->stream_start_event = NULL;
1394 } else if (queue->starting_segment != NULL) {
1395 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1396 queue->starting_segment = NULL;
1399 GstBuffer *buffer = NULL;
1400 guint64 reading_pos;
1402 reading_pos = queue->current->reading_pos;
1405 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1410 item = GST_MINI_OBJECT_CAST (buffer);
1413 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1423 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1424 * the temp filename. */
1426 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1431 if (queue->temp_file)
1432 goto already_opened;
1434 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1436 /* If temp_template was set, allocate a filename and open that filen */
1439 if (queue->temp_template == NULL)
1442 /* make copy of the template, we don't want to change this */
1443 name = g_strdup (queue->temp_template);
1444 fd = g_mkstemp (name);
1446 goto mkstemp_failed;
1448 /* open the file for update/writing */
1449 queue->temp_file = fdopen (fd, "wb+");
1450 /* error creating file */
1451 if (queue->temp_file == NULL)
1454 g_free (queue->temp_location);
1455 queue->temp_location = name;
1457 GST_QUEUE2_MUTEX_UNLOCK (queue);
1459 /* we can't emit the notify with the lock */
1460 g_object_notify (G_OBJECT (queue), "temp-location");
1462 GST_QUEUE2_MUTEX_LOCK (queue);
1464 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1471 GST_DEBUG_OBJECT (queue, "temp file was already open");
1476 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1477 (_("No Temp directory specified.")), (NULL));
1482 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1483 (_("Could not create temp file \"%s\"."), queue->temp_template),
1490 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1491 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1500 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1503 if (queue->temp_file == NULL)
1506 GST_DEBUG_OBJECT (queue, "closing temp file");
1508 fflush (queue->temp_file);
1509 fclose (queue->temp_file);
1511 if (queue->temp_remove)
1512 remove (queue->temp_location);
1514 queue->temp_file = NULL;
1515 clean_ranges (queue);
1519 gst_queue2_flush_temp_file (GstQueue2 * queue)
1521 if (queue->temp_file == NULL)
1524 GST_DEBUG_OBJECT (queue, "flushing temp file");
1526 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1530 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full)
1532 if (!QUEUE_IS_USING_QUEUE (queue)) {
1533 if (QUEUE_IS_USING_TEMP_FILE (queue))
1534 gst_queue2_flush_temp_file (queue);
1535 init_ranges (queue);
1537 while (!g_queue_is_empty (&queue->queue)) {
1538 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1540 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1541 && GST_EVENT_IS_STICKY (qitem->item)
1542 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1543 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1544 gst_pad_store_sticky_event (queue->srcpad,
1545 GST_EVENT_CAST (qitem->item));
1548 /* Then lose another reference because we are supposed to destroy that
1549 data when flushing */
1550 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1551 gst_mini_object_unref (qitem->item);
1552 g_slice_free (GstQueue2Item, qitem);
1555 queue->last_query = FALSE;
1556 g_cond_signal (&queue->query_handled);
1557 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1558 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1559 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1560 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1561 queue->sink_tainted = queue->src_tainted = TRUE;
1562 if (queue->starting_segment != NULL)
1563 gst_event_unref (queue->starting_segment);
1564 queue->starting_segment = NULL;
1565 queue->segment_event_received = FALSE;
1566 gst_event_replace (&queue->stream_start_event, NULL);
1568 /* we deleted a lot of something */
1569 GST_QUEUE2_SIGNAL_DEL (queue);
1573 gst_queue2_wait_free_space (GstQueue2 * queue)
1575 /* We make space available if we're "full" according to whatever
1576 * the user defined as "full". */
1577 if (gst_queue2_is_filled (queue)) {
1580 /* pause the timer while we wait. The fact that we are waiting does not mean
1581 * the byterate on the input pad is lower */
1582 if ((started = queue->in_timer_started))
1583 g_timer_stop (queue->in_timer);
1585 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1586 "queue is full, waiting for free space");
1588 /* Wait for space to be available, we could be unlocked because of a flush. */
1589 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1591 while (gst_queue2_is_filled (queue));
1593 /* and continue if we were running before */
1595 g_timer_continue (queue->in_timer);
1602 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1608 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1611 guint8 *data, *ring_buffer;
1612 guint size, rb_size;
1613 guint64 writing_pos, new_writing_pos;
1614 GstQueue2Range *range, *prev, *next;
1616 if (QUEUE_IS_USING_RING_BUFFER (queue))
1617 writing_pos = queue->current->rb_writing_pos;
1619 writing_pos = queue->current->writing_pos;
1620 ring_buffer = queue->ring_buffer;
1621 rb_size = queue->ring_buffer_max_size;
1623 gst_buffer_map (buffer, &info, GST_MAP_READ);
1628 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1632 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1633 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1634 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1635 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1636 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1642 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1645 /* calculate the space in the ring buffer not used by data from
1646 * the current range */
1647 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1648 /* wait until there is some free space */
1649 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1651 /* get the amount of space we have */
1652 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1654 /* calculate if we need to split or if we can write the entire
1656 to_write = MIN (size, space);
1658 /* the writing position in the ring buffer after writing (part
1659 * or all of) the buffer */
1660 new_writing_pos = (writing_pos + to_write) % rb_size;
1663 range = queue->ranges;
1665 /* if we need to overwrite data in the ring buffer, we need to
1668 * warning: this code is complicated and includes some
1669 * simplifications - pen, paper and diagrams for the cases
1672 guint64 range_data_start, range_data_end;
1673 GstQueue2Range *range_to_destroy = NULL;
1675 range_data_start = range->rb_offset;
1676 range_data_end = range->rb_writing_pos;
1678 /* handle the special case where the range has no data in it */
1679 if (range->writing_pos == range->offset) {
1680 if (range != queue->current) {
1681 GST_DEBUG_OBJECT (queue,
1682 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1683 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1685 range_to_destroy = range;
1687 prev->next = range->next;
1692 if (range_data_end > range_data_start) {
1693 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1696 if (new_writing_pos > range_data_start) {
1697 if (new_writing_pos >= range_data_end) {
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;
1706 GST_DEBUG_OBJECT (queue,
1707 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1708 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1709 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1710 range->offset + new_writing_pos - range_data_start,
1712 range->offset += (new_writing_pos - range_data_start);
1713 range->rb_offset = new_writing_pos;
1717 guint64 new_wpos_virt = writing_pos + to_write;
1719 if (new_wpos_virt <= range_data_start)
1722 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1723 GST_DEBUG_OBJECT (queue,
1724 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1725 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1727 range_to_destroy = range;
1729 prev->next = range->next;
1731 GST_DEBUG_OBJECT (queue,
1732 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1733 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1734 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1735 range->offset + new_writing_pos - range_data_start,
1737 range->offset += (new_wpos_virt - range_data_start);
1738 range->rb_offset = new_writing_pos;
1743 if (!range_to_destroy)
1746 range = range->next;
1747 if (range_to_destroy) {
1748 if (range_to_destroy == queue->ranges)
1749 queue->ranges = range;
1750 g_slice_free (GstQueue2Range, range_to_destroy);
1751 range_to_destroy = NULL;
1756 new_writing_pos = writing_pos + to_write;
1759 if (QUEUE_IS_USING_TEMP_FILE (queue)
1760 && FSEEK_FILE (queue->temp_file, writing_pos))
1763 if (new_writing_pos > writing_pos) {
1764 GST_INFO_OBJECT (queue,
1765 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1766 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1767 queue->current->writing_pos, queue->current->rb_writing_pos);
1768 /* either not using ring buffer or no wrapping, just write */
1769 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1770 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1773 memcpy (ring_buffer + writing_pos, data, to_write);
1776 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1777 /* try to merge with next range */
1778 while ((next = queue->current->next)) {
1779 GST_INFO_OBJECT (queue,
1780 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1781 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1782 if (new_writing_pos < next->offset)
1785 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1788 /* remove the group, we could choose to not read the data in this range
1789 * again. This would involve us doing a seek to the current writing position
1790 * in the range. FIXME, It would probably make sense to do a seek when there
1791 * is a lot of data in the range we merged with to avoid reading it all
1793 queue->current->next = next->next;
1794 g_slice_free (GstQueue2Range, next);
1796 debug_ranges (queue);
1798 goto update_and_signal;
1802 guint block_one, block_two;
1804 block_one = rb_size - writing_pos;
1805 block_two = to_write - block_one;
1807 if (block_one > 0) {
1808 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1809 /* write data to end of ring buffer */
1810 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1811 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1814 memcpy (ring_buffer + writing_pos, data, block_one);
1818 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1821 if (block_two > 0) {
1822 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1823 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1824 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1827 memcpy (ring_buffer, data + block_one, block_two);
1833 /* update the writing positions */
1835 GST_INFO_OBJECT (queue,
1836 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1837 to_write, writing_pos, size);
1839 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1841 queue->current->writing_pos += to_write;
1842 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1844 queue->current->writing_pos = writing_pos = new_writing_pos;
1846 update_cur_level (queue, queue->current);
1848 /* update the buffering status */
1849 if (queue->use_buffering)
1850 update_buffering (queue);
1852 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1853 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1855 GST_QUEUE2_SIGNAL_ADD (queue);
1858 gst_buffer_unmap (buffer, &info);
1865 GST_DEBUG_OBJECT (queue, "we are flushing");
1866 gst_buffer_unmap (buffer, &info);
1867 /* FIXME - GST_FLOW_EOS ? */
1872 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1873 gst_buffer_unmap (buffer, &info);
1880 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1884 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1885 (_("Error while writing to download file.")),
1886 ("%s", g_strerror (errno)));
1889 gst_buffer_unmap (buffer, &info);
1895 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1897 GstQueue2 *queue = q;
1899 GST_TRACE_OBJECT (queue,
1900 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1901 gst_buffer_get_size (*buf));
1903 if (!gst_queue2_create_write (queue, *buf)) {
1904 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1911 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1913 guint *p_size = data;
1916 buf_size = gst_buffer_get_size (*buf);
1917 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1918 *p_size += buf_size;
1922 /* enqueue an item an update the level stats */
1924 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1925 GstQueue2ItemType item_type)
1927 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1931 buffer = GST_BUFFER_CAST (item);
1932 size = gst_buffer_get_size (buffer);
1934 /* add buffer to the statistics */
1935 if (QUEUE_IS_USING_QUEUE (queue)) {
1936 queue->cur_level.buffers++;
1937 queue->cur_level.bytes += size;
1939 queue->bytes_in += size;
1941 /* apply new buffer to segment stats */
1942 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1943 /* update the byterate stats */
1944 update_in_rates (queue);
1946 if (!QUEUE_IS_USING_QUEUE (queue)) {
1947 /* FIXME - check return value? */
1948 gst_queue2_create_write (queue, buffer);
1950 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1951 GstBufferList *buffer_list;
1954 buffer_list = GST_BUFFER_LIST_CAST (item);
1956 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1957 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1959 /* add buffer to the statistics */
1960 if (QUEUE_IS_USING_QUEUE (queue)) {
1961 queue->cur_level.buffers++;
1962 queue->cur_level.bytes += size;
1964 queue->bytes_in += size;
1966 /* apply new buffer to segment stats */
1967 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1969 /* update the byterate stats */
1970 update_in_rates (queue);
1972 if (!QUEUE_IS_USING_QUEUE (queue)) {
1973 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1975 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
1978 event = GST_EVENT_CAST (item);
1980 switch (GST_EVENT_TYPE (event)) {
1982 /* Zero the thresholds, this makes sure the queue is completely
1983 * filled and we can read all data from the queue. */
1984 GST_DEBUG_OBJECT (queue, "we have EOS");
1985 queue->is_eos = TRUE;
1987 case GST_EVENT_SEGMENT:
1988 apply_segment (queue, event, &queue->sink_segment, TRUE);
1989 /* This is our first new segment, we hold it
1990 * as we can't save it on the temp file */
1991 if (!QUEUE_IS_USING_QUEUE (queue)) {
1992 if (queue->segment_event_received)
1993 goto unexpected_event;
1995 queue->segment_event_received = TRUE;
1996 if (queue->starting_segment != NULL)
1997 gst_event_unref (queue->starting_segment);
1998 queue->starting_segment = event;
2001 /* a new segment allows us to accept more buffers if we got EOS
2002 * from downstream */
2003 queue->unexpected = FALSE;
2005 case GST_EVENT_STREAM_START:
2006 if (!QUEUE_IS_USING_QUEUE (queue)) {
2007 gst_event_replace (&queue->stream_start_event, event);
2008 gst_event_unref (event);
2012 case GST_EVENT_CAPS:{
2015 gst_event_parse_caps (event, &caps);
2016 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2018 if (!QUEUE_IS_USING_QUEUE (queue)) {
2019 GST_LOG ("Dropping caps event, not using queue");
2020 gst_event_unref (event);
2026 if (!QUEUE_IS_USING_QUEUE (queue))
2027 goto unexpected_event;
2030 } else if (GST_IS_QUERY (item)) {
2031 /* Can't happen as we check that in the caller */
2032 if (!QUEUE_IS_USING_QUEUE (queue))
2033 g_assert_not_reached ();
2035 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2036 item, GST_OBJECT_NAME (queue));
2037 /* we can't really unref since we don't know what it is */
2042 /* update the buffering status */
2043 if (queue->use_buffering)
2044 update_buffering (queue);
2046 if (QUEUE_IS_USING_QUEUE (queue)) {
2047 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2048 qitem->type = item_type;
2050 g_queue_push_tail (&queue->queue, qitem);
2052 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2055 GST_QUEUE2_SIGNAL_ADD (queue);
2064 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2065 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2066 GST_OBJECT_NAME (queue));
2067 gst_event_unref (GST_EVENT_CAST (item));
2072 /* dequeue an item from the queue and update level stats */
2073 static GstMiniObject *
2074 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2076 GstMiniObject *item;
2078 if (!QUEUE_IS_USING_QUEUE (queue)) {
2079 item = gst_queue2_read_item_from_file (queue);
2081 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2083 g_slice_free (GstQueue2Item, qitem);
2089 if (GST_IS_BUFFER (item)) {
2093 buffer = GST_BUFFER_CAST (item);
2094 size = gst_buffer_get_size (buffer);
2095 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2097 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2098 "retrieved buffer %p from queue", buffer);
2100 if (QUEUE_IS_USING_QUEUE (queue)) {
2101 queue->cur_level.buffers--;
2102 queue->cur_level.bytes -= size;
2104 queue->bytes_out += size;
2106 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2107 /* update the byterate stats */
2108 update_out_rates (queue);
2109 /* update the buffering */
2110 if (queue->use_buffering)
2111 update_buffering (queue);
2113 } else if (GST_IS_EVENT (item)) {
2114 GstEvent *event = GST_EVENT_CAST (item);
2116 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2118 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2119 "retrieved event %p from queue", event);
2121 switch (GST_EVENT_TYPE (event)) {
2123 /* queue is empty now that we dequeued the EOS */
2124 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2126 case GST_EVENT_SEGMENT:
2127 apply_segment (queue, event, &queue->src_segment, FALSE);
2132 } else if (GST_IS_BUFFER_LIST (item)) {
2133 GstBufferList *buffer_list;
2136 buffer_list = GST_BUFFER_LIST_CAST (item);
2137 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2138 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2140 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2141 "retrieved buffer list %p from queue", buffer_list);
2143 if (QUEUE_IS_USING_QUEUE (queue)) {
2144 queue->cur_level.buffers--;
2145 queue->cur_level.bytes -= size;
2147 queue->bytes_out += size;
2149 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2150 /* update the byterate stats */
2151 update_out_rates (queue);
2152 /* update the buffering */
2153 if (queue->use_buffering)
2154 update_buffering (queue);
2155 } else if (GST_IS_QUERY (item)) {
2156 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2157 "retrieved query %p from queue", item);
2158 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2161 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2162 item, GST_OBJECT_NAME (queue));
2164 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2166 GST_QUEUE2_SIGNAL_DEL (queue);
2173 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2179 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2184 queue = GST_QUEUE2 (parent);
2186 switch (GST_EVENT_TYPE (event)) {
2187 case GST_EVENT_FLUSH_START:
2189 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2190 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2192 gst_pad_push_event (queue->srcpad, event);
2194 /* now unblock the chain function */
2195 GST_QUEUE2_MUTEX_LOCK (queue);
2196 queue->srcresult = GST_FLOW_FLUSHING;
2197 queue->sinkresult = GST_FLOW_FLUSHING;
2198 /* unblock the loop and chain functions */
2199 GST_QUEUE2_SIGNAL_ADD (queue);
2200 GST_QUEUE2_SIGNAL_DEL (queue);
2201 queue->last_query = FALSE;
2202 g_cond_signal (&queue->query_handled);
2203 GST_QUEUE2_MUTEX_UNLOCK (queue);
2205 /* make sure it pauses, this should happen since we sent
2206 * flush_start downstream. */
2207 gst_pad_pause_task (queue->srcpad);
2208 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2210 GST_QUEUE2_MUTEX_LOCK (queue);
2211 /* flush the sink pad */
2212 queue->sinkresult = GST_FLOW_FLUSHING;
2213 GST_QUEUE2_SIGNAL_DEL (queue);
2214 queue->last_query = FALSE;
2215 g_cond_signal (&queue->query_handled);
2216 GST_QUEUE2_MUTEX_UNLOCK (queue);
2218 gst_event_unref (event);
2222 case GST_EVENT_FLUSH_STOP:
2224 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2226 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2228 gst_pad_push_event (queue->srcpad, event);
2230 GST_QUEUE2_MUTEX_LOCK (queue);
2231 gst_queue2_locked_flush (queue, FALSE);
2232 queue->srcresult = GST_FLOW_OK;
2233 queue->sinkresult = GST_FLOW_OK;
2234 queue->is_eos = FALSE;
2235 queue->unexpected = FALSE;
2236 queue->seeking = FALSE;
2237 /* reset rate counters */
2238 reset_rate_timer (queue);
2239 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2240 queue->srcpad, NULL);
2241 GST_QUEUE2_MUTEX_UNLOCK (queue);
2243 GST_QUEUE2_MUTEX_LOCK (queue);
2244 queue->segment_event_received = FALSE;
2245 queue->is_eos = FALSE;
2246 queue->unexpected = FALSE;
2247 queue->sinkresult = GST_FLOW_OK;
2248 queue->seeking = FALSE;
2249 GST_QUEUE2_MUTEX_UNLOCK (queue);
2251 gst_event_unref (event);
2256 if (GST_EVENT_IS_SERIALIZED (event)) {
2257 /* serialized events go in the queue */
2258 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2259 /* refuse more events on EOS */
2262 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2263 GST_QUEUE2_MUTEX_UNLOCK (queue);
2265 /* non-serialized events are passed upstream. */
2266 gst_pad_push_event (queue->srcpad, event);
2276 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2277 GST_QUEUE2_MUTEX_UNLOCK (queue);
2278 gst_event_unref (event);
2283 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2284 GST_QUEUE2_MUTEX_UNLOCK (queue);
2285 gst_event_unref (event);
2291 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2297 queue = GST_QUEUE2 (parent);
2299 switch (GST_QUERY_TYPE (query)) {
2301 if (GST_QUERY_IS_SERIALIZED (query)) {
2302 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2303 /* serialized events go in the queue */
2304 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2305 if (QUEUE_IS_USING_QUEUE (queue)) {
2306 gst_queue2_locked_enqueue (queue, query, GST_QUEUE2_ITEM_TYPE_QUERY);
2308 STATUS (queue, queue->sinkpad, "wait for QUERY");
2309 g_cond_wait (&queue->query_handled, &queue->qlock);
2310 if (queue->sinkresult != GST_FLOW_OK)
2312 res = queue->last_query;
2314 GST_DEBUG_OBJECT (queue,
2315 "refusing query, we are not using the queue");
2318 GST_QUEUE2_MUTEX_UNLOCK (queue);
2320 res = gst_pad_query_default (pad, parent, query);
2329 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2330 GST_QUEUE2_MUTEX_UNLOCK (queue);
2336 gst_queue2_is_empty (GstQueue2 * queue)
2338 /* never empty on EOS */
2342 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2343 return queue->current->writing_pos <= queue->current->max_reading_pos;
2345 if (queue->queue.length == 0)
2353 gst_queue2_is_filled (GstQueue2 * queue)
2357 /* always filled on EOS */
2361 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2362 (queue->cur_level.format) >= ((alt_max) ? \
2363 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2365 /* if using a ring buffer we're filled if all ring buffer space is used
2366 * _by the current range_ */
2367 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2368 guint64 rb_size = queue->ring_buffer_max_size;
2369 GST_DEBUG_OBJECT (queue,
2370 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2371 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2372 return CHECK_FILLED (bytes, rb_size);
2375 /* if using file, we're never filled if we don't have EOS */
2376 if (QUEUE_IS_USING_TEMP_FILE (queue))
2379 /* we are never filled when we have no buffers at all */
2380 if (queue->cur_level.buffers == 0)
2383 /* we are filled if one of the current levels exceeds the max */
2384 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2385 || CHECK_FILLED (time, 0);
2387 /* if we need to, use the rate estimate to check against the max time we are
2388 * allowed to queue */
2389 if (queue->use_rate_estimate)
2390 res |= CHECK_FILLED (rate_time, 0);
2396 static GstFlowReturn
2397 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2398 GstMiniObject * item, GstQueue2ItemType item_type)
2400 /* we have to lock the queue since we span threads */
2401 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2402 /* when we received EOS, we refuse more data */
2405 /* when we received unexpected from downstream, refuse more buffers */
2406 if (queue->unexpected)
2407 goto out_unexpected;
2409 /* while we didn't receive the newsegment, we're seeking and we skip data */
2413 if (!gst_queue2_wait_free_space (queue))
2416 /* put buffer in queue now */
2417 gst_queue2_locked_enqueue (queue, item, item_type);
2418 GST_QUEUE2_MUTEX_UNLOCK (queue);
2422 /* special conditions */
2425 GstFlowReturn ret = queue->sinkresult;
2427 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2428 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2429 GST_QUEUE2_MUTEX_UNLOCK (queue);
2430 gst_mini_object_unref (item);
2436 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2437 GST_QUEUE2_MUTEX_UNLOCK (queue);
2438 gst_mini_object_unref (item);
2440 return GST_FLOW_EOS;
2444 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2445 GST_QUEUE2_MUTEX_UNLOCK (queue);
2446 gst_mini_object_unref (item);
2452 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2453 GST_QUEUE2_MUTEX_UNLOCK (queue);
2454 gst_mini_object_unref (item);
2456 return GST_FLOW_EOS;
2460 static GstFlowReturn
2461 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2465 queue = GST_QUEUE2 (parent);
2467 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2468 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2469 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2470 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2471 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2473 return gst_queue2_chain_buffer_or_buffer_list (queue,
2474 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2477 static GstFlowReturn
2478 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2479 GstBufferList * buffer_list)
2483 queue = GST_QUEUE2 (parent);
2485 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2486 "received buffer list %p", buffer_list);
2488 return gst_queue2_chain_buffer_or_buffer_list (queue,
2489 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2492 static GstMiniObject *
2493 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2495 GstMiniObject *data;
2497 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2499 /* stop pushing buffers, we dequeue all items until we see an item that we
2500 * can push again, which is EOS or SEGMENT. If there is nothing in the
2501 * queue we can push, we set a flag to make the sinkpad refuse more
2502 * buffers with an EOS return value until we receive something
2503 * pushable again or we get flushed. */
2504 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2505 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2506 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2507 "dropping EOS buffer %p", data);
2508 gst_buffer_unref (GST_BUFFER_CAST (data));
2509 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2510 GstEvent *event = GST_EVENT_CAST (data);
2511 GstEventType type = GST_EVENT_TYPE (event);
2513 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2514 /* we found a pushable item in the queue, push it out */
2515 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2516 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2519 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2520 "dropping EOS event %p", event);
2521 gst_event_unref (event);
2522 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2523 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2524 "dropping EOS buffer list %p", data);
2525 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2526 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2527 queue->last_query = FALSE;
2528 g_cond_signal (&queue->query_handled);
2529 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2532 /* no more items in the queue. Set the unexpected flag so that upstream
2533 * make us refuse any more buffers on the sinkpad. Since we will still
2534 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2535 * task function does not shut down. */
2536 queue->unexpected = TRUE;
2540 /* dequeue an item from the queue an push it downstream. This functions returns
2541 * the result of the push. */
2542 static GstFlowReturn
2543 gst_queue2_push_one (GstQueue2 * queue)
2545 GstFlowReturn result = GST_FLOW_OK;
2546 GstMiniObject *data;
2547 GstQueue2ItemType item_type;
2549 data = gst_queue2_locked_dequeue (queue, &item_type);
2554 GST_QUEUE2_MUTEX_UNLOCK (queue);
2556 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2559 buffer = GST_BUFFER_CAST (data);
2561 result = gst_pad_push (queue->srcpad, buffer);
2563 /* need to check for srcresult here as well */
2564 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2565 if (result == GST_FLOW_EOS) {
2566 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2569 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2570 * to the caller so that the task function does not shut down */
2571 result = GST_FLOW_OK;
2573 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2574 GstEvent *event = GST_EVENT_CAST (data);
2575 GstEventType type = GST_EVENT_TYPE (event);
2577 gst_pad_push_event (queue->srcpad, event);
2579 /* if we're EOS, return EOS so that the task pauses. */
2580 if (type == GST_EVENT_EOS) {
2581 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2582 "pushed EOS event %p, return EOS", event);
2583 result = GST_FLOW_EOS;
2586 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2587 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2588 GstBufferList *buffer_list;
2590 buffer_list = GST_BUFFER_LIST_CAST (data);
2592 result = gst_pad_push_list (queue->srcpad, buffer_list);
2594 /* need to check for srcresult here as well */
2595 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2596 if (result == GST_FLOW_EOS) {
2597 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2600 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2601 * to the caller so that the task function does not shut down */
2602 result = GST_FLOW_OK;
2604 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2605 GstQuery *query = GST_QUERY_CAST (data);
2607 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2608 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2609 "did query %p, return %d", query, queue->last_query);
2610 g_cond_signal (&queue->query_handled);
2611 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2612 result = GST_FLOW_OK;
2619 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2620 "exit because we have no item in the queue");
2621 return GST_FLOW_ERROR;
2625 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2626 return GST_FLOW_FLUSHING;
2630 /* called repeatedly with @pad as the source pad. This function should push out
2631 * data to the peer element. */
2633 gst_queue2_loop (GstPad * pad)
2638 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2640 /* have to lock for thread-safety */
2641 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2643 if (gst_queue2_is_empty (queue)) {
2646 /* pause the timer while we wait. The fact that we are waiting does not mean
2647 * the byterate on the output pad is lower */
2648 if ((started = queue->out_timer_started))
2649 g_timer_stop (queue->out_timer);
2651 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2652 "queue is empty, waiting for new data");
2654 /* Wait for data to be available, we could be unlocked because of a flush. */
2655 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2657 while (gst_queue2_is_empty (queue));
2659 /* and continue if we were running before */
2661 g_timer_continue (queue->out_timer);
2663 ret = gst_queue2_push_one (queue);
2664 queue->srcresult = ret;
2665 queue->sinkresult = ret;
2666 if (ret != GST_FLOW_OK)
2669 GST_QUEUE2_MUTEX_UNLOCK (queue);
2676 gboolean eos = queue->is_eos;
2677 GstFlowReturn ret = queue->srcresult;
2679 gst_pad_pause_task (queue->srcpad);
2680 GST_QUEUE2_MUTEX_UNLOCK (queue);
2681 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2682 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2683 /* let app know about us giving up if upstream is not expected to do so */
2684 /* EOS is already taken care of elsewhere */
2685 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2686 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2687 (_("Internal data flow error.")),
2688 ("streaming task paused, reason %s (%d)",
2689 gst_flow_get_name (ret), ret));
2690 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2697 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2699 gboolean res = TRUE;
2700 GstQueue2 *queue = GST_QUEUE2 (parent);
2702 #ifndef GST_DISABLE_GST_DEBUG
2703 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2704 event, GST_EVENT_TYPE_NAME (event));
2707 switch (GST_EVENT_TYPE (event)) {
2708 case GST_EVENT_FLUSH_START:
2709 if (QUEUE_IS_USING_QUEUE (queue)) {
2710 /* just forward upstream */
2711 res = gst_pad_push_event (queue->sinkpad, event);
2713 /* now unblock the getrange function */
2714 GST_QUEUE2_MUTEX_LOCK (queue);
2715 GST_DEBUG_OBJECT (queue, "flushing");
2716 queue->srcresult = GST_FLOW_FLUSHING;
2717 GST_QUEUE2_SIGNAL_ADD (queue);
2718 GST_QUEUE2_MUTEX_UNLOCK (queue);
2720 /* when using a temp file, we eat the event */
2722 gst_event_unref (event);
2725 case GST_EVENT_FLUSH_STOP:
2726 if (QUEUE_IS_USING_QUEUE (queue)) {
2727 /* just forward upstream */
2728 res = gst_pad_push_event (queue->sinkpad, event);
2730 /* now unblock the getrange function */
2731 GST_QUEUE2_MUTEX_LOCK (queue);
2732 queue->srcresult = GST_FLOW_OK;
2733 GST_QUEUE2_MUTEX_UNLOCK (queue);
2735 /* when using a temp file, we eat the event */
2737 gst_event_unref (event);
2740 case GST_EVENT_RECONFIGURE:
2741 GST_QUEUE2_MUTEX_LOCK (queue);
2742 /* assume downstream is linked now and try to push again */
2743 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2744 queue->srcresult = GST_FLOW_OK;
2745 queue->sinkresult = GST_FLOW_OK;
2746 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2747 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2751 GST_QUEUE2_MUTEX_UNLOCK (queue);
2753 res = gst_pad_push_event (queue->sinkpad, event);
2756 res = gst_pad_push_event (queue->sinkpad, event);
2764 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2768 queue = GST_QUEUE2 (parent);
2770 switch (GST_QUERY_TYPE (query)) {
2771 case GST_QUERY_POSITION:
2776 if (!gst_pad_peer_query (queue->sinkpad, query))
2779 /* get peer position */
2780 gst_query_parse_position (query, &format, &peer_pos);
2782 /* FIXME: this code assumes that there's no discont in the queue */
2784 case GST_FORMAT_BYTES:
2785 peer_pos -= queue->cur_level.bytes;
2787 case GST_FORMAT_TIME:
2788 peer_pos -= queue->cur_level.time;
2791 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2792 "know how to adjust value", gst_format_get_name (format));
2795 /* set updated position */
2796 gst_query_set_position (query, format, peer_pos);
2799 case GST_QUERY_DURATION:
2801 GST_DEBUG_OBJECT (queue, "doing peer query");
2803 if (!gst_pad_peer_query (queue->sinkpad, query))
2806 GST_DEBUG_OBJECT (queue, "peer query success");
2809 case GST_QUERY_BUFFERING:
2812 gboolean is_buffering;
2813 GstBufferingMode mode;
2814 gint avg_in, avg_out;
2815 gint64 buffering_left;
2817 GST_DEBUG_OBJECT (queue, "query buffering");
2819 get_buffering_percent (queue, &is_buffering, &percent);
2820 gst_query_set_buffering_percent (query, is_buffering, percent);
2822 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
2824 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
2827 if (!QUEUE_IS_USING_QUEUE (queue)) {
2828 /* add ranges for download and ringbuffer buffering */
2830 gint64 start, stop, range_start, range_stop;
2831 guint64 writing_pos;
2832 gint64 estimated_total;
2834 gboolean peer_res, is_eos;
2835 GstQueue2Range *queued_ranges;
2837 /* we need a current download region */
2838 if (queue->current == NULL)
2841 writing_pos = queue->current->writing_pos;
2842 is_eos = queue->is_eos;
2845 /* we're EOS, we know the duration in bytes now */
2847 duration = writing_pos;
2849 /* get duration of upstream in bytes */
2850 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2851 GST_FORMAT_BYTES, &duration);
2854 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
2855 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
2857 /* calculate remaining and total download time */
2858 if (peer_res && avg_in > 0.0)
2859 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
2861 estimated_total = -1;
2863 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
2866 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2869 case GST_FORMAT_PERCENT:
2870 /* we need duration */
2875 /* get our available data relative to the duration */
2878 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
2883 case GST_FORMAT_BYTES:
2893 /* fill out the buffered ranges */
2894 for (queued_ranges = queue->ranges; queued_ranges;
2895 queued_ranges = queued_ranges->next) {
2897 case GST_FORMAT_PERCENT:
2898 if (duration == -1) {
2904 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2905 queued_ranges->offset, duration);
2907 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2908 queued_ranges->writing_pos, duration);
2910 case GST_FORMAT_BYTES:
2911 range_start = queued_ranges->offset;
2912 range_stop = queued_ranges->writing_pos;
2919 if (range_start == range_stop)
2921 GST_DEBUG_OBJECT (queue,
2922 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2923 G_GINT64_FORMAT, range_start, range_stop);
2924 gst_query_add_buffering_range (query, range_start, range_stop);
2927 gst_query_set_buffering_range (query, format, start, stop,
2932 case GST_QUERY_SCHEDULING:
2935 GstSchedulingFlags flags = 0;
2937 /* we can operate in pull mode when we are using a tempfile */
2938 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2941 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2942 gst_query_set_scheduling (query, flags, 0, -1, 0);
2944 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
2945 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
2949 /* peer handled other queries */
2950 if (!gst_pad_query_default (pad, parent, query))
2960 GST_DEBUG_OBJECT (queue, "failed peer query");
2966 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2968 GstQueue2 *queue = GST_QUEUE2 (element);
2970 /* simply forward to the srcpad query function */
2971 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
2976 gst_queue2_update_upstream_size (GstQueue2 * queue)
2978 gint64 upstream_size = -1;
2980 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
2982 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2983 queue->upstream_size = upstream_size;
2987 static GstFlowReturn
2988 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
2989 guint length, GstBuffer ** buffer)
2994 queue = GST_QUEUE2_CAST (parent);
2996 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2997 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2998 offset = (offset == -1) ? queue->current->reading_pos : offset;
3000 GST_DEBUG_OBJECT (queue,
3001 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3003 /* catch any reads beyond the size of the file here to make sure queue2
3004 * doesn't send seek events beyond the size of the file upstream, since
3005 * that would confuse elements such as souphttpsrc and/or http servers.
3006 * Demuxers often just loop until EOS at the end of the file to figure out
3007 * when they've read all the end-headers or index chunks. */
3008 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3009 gst_queue2_update_upstream_size (queue);
3010 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3011 goto out_unexpected;
3014 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3015 gst_queue2_update_upstream_size (queue);
3016 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3017 length = queue->upstream_size - offset;
3018 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3022 /* FIXME - function will block when the range is not yet available */
3023 ret = gst_queue2_create_read (queue, offset, length, buffer);
3024 GST_QUEUE2_MUTEX_UNLOCK (queue);
3031 ret = queue->srcresult;
3033 GST_DEBUG_OBJECT (queue, "we are flushing");
3034 GST_QUEUE2_MUTEX_UNLOCK (queue);
3039 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3040 GST_QUEUE2_MUTEX_UNLOCK (queue);
3041 return GST_FLOW_EOS;
3045 /* sink currently only operates in push mode */
3047 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3048 GstPadMode mode, gboolean active)
3053 queue = GST_QUEUE2 (parent);
3056 case GST_PAD_MODE_PUSH:
3058 GST_QUEUE2_MUTEX_LOCK (queue);
3059 GST_DEBUG_OBJECT (queue, "activating push mode");
3060 queue->srcresult = GST_FLOW_OK;
3061 queue->sinkresult = GST_FLOW_OK;
3062 queue->is_eos = FALSE;
3063 queue->unexpected = FALSE;
3064 reset_rate_timer (queue);
3065 GST_QUEUE2_MUTEX_UNLOCK (queue);
3067 /* unblock chain function */
3068 GST_QUEUE2_MUTEX_LOCK (queue);
3069 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3070 queue->srcresult = GST_FLOW_FLUSHING;
3071 queue->sinkresult = GST_FLOW_FLUSHING;
3072 gst_queue2_locked_flush (queue, TRUE);
3073 GST_QUEUE2_MUTEX_UNLOCK (queue);
3084 /* src operating in push mode, we start a task on the source pad that pushes out
3085 * buffers from the queue */
3087 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3089 gboolean result = FALSE;
3092 queue = GST_QUEUE2 (parent);
3095 GST_QUEUE2_MUTEX_LOCK (queue);
3096 GST_DEBUG_OBJECT (queue, "activating push mode");
3097 queue->srcresult = GST_FLOW_OK;
3098 queue->sinkresult = GST_FLOW_OK;
3099 queue->is_eos = FALSE;
3100 queue->unexpected = FALSE;
3102 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3103 GST_QUEUE2_MUTEX_UNLOCK (queue);
3105 /* unblock loop function */
3106 GST_QUEUE2_MUTEX_LOCK (queue);
3107 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3108 queue->srcresult = GST_FLOW_FLUSHING;
3109 queue->sinkresult = GST_FLOW_FLUSHING;
3110 /* the item add signal will unblock */
3111 GST_QUEUE2_SIGNAL_ADD (queue);
3112 GST_QUEUE2_MUTEX_UNLOCK (queue);
3114 /* step 2, make sure streaming finishes */
3115 result = gst_pad_stop_task (pad);
3121 /* pull mode, downstream will call our getrange function */
3123 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3128 queue = GST_QUEUE2 (parent);
3131 GST_QUEUE2_MUTEX_LOCK (queue);
3132 if (!QUEUE_IS_USING_QUEUE (queue)) {
3133 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3134 /* open the temp file now */
3135 result = gst_queue2_open_temp_location_file (queue);
3136 } else if (!queue->ring_buffer) {
3137 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3138 result = ! !queue->ring_buffer;
3143 GST_DEBUG_OBJECT (queue, "activating pull mode");
3144 init_ranges (queue);
3145 queue->srcresult = GST_FLOW_OK;
3146 queue->sinkresult = GST_FLOW_OK;
3147 queue->is_eos = FALSE;
3148 queue->unexpected = FALSE;
3149 queue->upstream_size = 0;
3151 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3152 /* this is not allowed, we cannot operate in pull mode without a temp
3154 queue->srcresult = GST_FLOW_FLUSHING;
3155 queue->sinkresult = GST_FLOW_FLUSHING;
3158 GST_QUEUE2_MUTEX_UNLOCK (queue);
3160 GST_QUEUE2_MUTEX_LOCK (queue);
3161 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3162 queue->srcresult = GST_FLOW_FLUSHING;
3163 queue->sinkresult = GST_FLOW_FLUSHING;
3164 /* this will unlock getrange */
3165 GST_QUEUE2_SIGNAL_ADD (queue);
3167 GST_QUEUE2_MUTEX_UNLOCK (queue);
3174 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3180 case GST_PAD_MODE_PULL:
3181 res = gst_queue2_src_activate_pull (pad, parent, active);
3183 case GST_PAD_MODE_PUSH:
3184 res = gst_queue2_src_activate_push (pad, parent, active);
3187 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3194 static GstStateChangeReturn
3195 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3198 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3200 queue = GST_QUEUE2 (element);
3202 switch (transition) {
3203 case GST_STATE_CHANGE_NULL_TO_READY:
3205 case GST_STATE_CHANGE_READY_TO_PAUSED:
3206 GST_QUEUE2_MUTEX_LOCK (queue);
3207 if (!QUEUE_IS_USING_QUEUE (queue)) {
3208 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3209 if (!gst_queue2_open_temp_location_file (queue))
3210 ret = GST_STATE_CHANGE_FAILURE;
3212 if (queue->ring_buffer) {
3213 g_free (queue->ring_buffer);
3214 queue->ring_buffer = NULL;
3216 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3217 ret = GST_STATE_CHANGE_FAILURE;
3219 init_ranges (queue);
3221 queue->segment_event_received = FALSE;
3222 queue->starting_segment = NULL;
3223 gst_event_replace (&queue->stream_start_event, NULL);
3224 GST_QUEUE2_MUTEX_UNLOCK (queue);
3226 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3232 if (ret == GST_STATE_CHANGE_FAILURE)
3235 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3237 if (ret == GST_STATE_CHANGE_FAILURE)
3240 switch (transition) {
3241 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3243 case GST_STATE_CHANGE_PAUSED_TO_READY:
3244 GST_QUEUE2_MUTEX_LOCK (queue);
3245 if (!QUEUE_IS_USING_QUEUE (queue)) {
3246 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3247 gst_queue2_close_temp_location_file (queue);
3248 } else if (queue->ring_buffer) {
3249 g_free (queue->ring_buffer);
3250 queue->ring_buffer = NULL;
3252 clean_ranges (queue);
3254 if (queue->starting_segment != NULL) {
3255 gst_event_unref (queue->starting_segment);
3256 queue->starting_segment = NULL;
3258 gst_event_replace (&queue->stream_start_event, NULL);
3259 GST_QUEUE2_MUTEX_UNLOCK (queue);
3261 case GST_STATE_CHANGE_READY_TO_NULL:
3270 /* changing the capacity of the queue must wake up
3271 * the _chain function, it might have more room now
3272 * to store the buffer/event in the queue */
3273 #define QUEUE_CAPACITY_CHANGE(q)\
3274 GST_QUEUE2_SIGNAL_DEL (queue);
3276 /* Changing the minimum required fill level must
3277 * wake up the _loop function as it might now
3278 * be able to preceed.
3280 #define QUEUE_THRESHOLD_CHANGE(q)\
3281 GST_QUEUE2_SIGNAL_ADD (queue);
3284 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3288 /* the element must be stopped in order to do this */
3289 GST_OBJECT_LOCK (queue);
3290 state = GST_STATE (queue);
3291 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3293 GST_OBJECT_UNLOCK (queue);
3295 /* set new location */
3296 g_free (queue->temp_template);
3297 queue->temp_template = g_strdup (template);
3304 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3305 GST_OBJECT_UNLOCK (queue);
3310 gst_queue2_set_property (GObject * object,
3311 guint prop_id, const GValue * value, GParamSpec * pspec)
3313 GstQueue2 *queue = GST_QUEUE2 (object);
3315 /* someone could change levels here, and since this
3316 * affects the get/put funcs, we need to lock for safety. */
3317 GST_QUEUE2_MUTEX_LOCK (queue);
3320 case PROP_MAX_SIZE_BYTES:
3321 queue->max_level.bytes = g_value_get_uint (value);
3322 QUEUE_CAPACITY_CHANGE (queue);
3324 case PROP_MAX_SIZE_BUFFERS:
3325 queue->max_level.buffers = g_value_get_uint (value);
3326 QUEUE_CAPACITY_CHANGE (queue);
3328 case PROP_MAX_SIZE_TIME:
3329 queue->max_level.time = g_value_get_uint64 (value);
3330 /* set rate_time to the same value. We use an extra field in the level
3331 * structure so that we can easily access and compare it */
3332 queue->max_level.rate_time = queue->max_level.time;
3333 QUEUE_CAPACITY_CHANGE (queue);
3335 case PROP_USE_BUFFERING:
3336 queue->use_buffering = g_value_get_boolean (value);
3338 case PROP_USE_RATE_ESTIMATE:
3339 queue->use_rate_estimate = g_value_get_boolean (value);
3341 case PROP_LOW_PERCENT:
3342 queue->low_percent = g_value_get_int (value);
3344 case PROP_HIGH_PERCENT:
3345 queue->high_percent = g_value_get_int (value);
3347 case PROP_TEMP_TEMPLATE:
3348 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3350 case PROP_TEMP_REMOVE:
3351 queue->temp_remove = g_value_get_boolean (value);
3353 case PROP_RING_BUFFER_MAX_SIZE:
3354 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3361 GST_QUEUE2_MUTEX_UNLOCK (queue);
3365 gst_queue2_get_property (GObject * object,
3366 guint prop_id, GValue * value, GParamSpec * pspec)
3368 GstQueue2 *queue = GST_QUEUE2 (object);
3370 GST_QUEUE2_MUTEX_LOCK (queue);
3373 case PROP_CUR_LEVEL_BYTES:
3374 g_value_set_uint (value, queue->cur_level.bytes);
3376 case PROP_CUR_LEVEL_BUFFERS:
3377 g_value_set_uint (value, queue->cur_level.buffers);
3379 case PROP_CUR_LEVEL_TIME:
3380 g_value_set_uint64 (value, queue->cur_level.time);
3382 case PROP_MAX_SIZE_BYTES:
3383 g_value_set_uint (value, queue->max_level.bytes);
3385 case PROP_MAX_SIZE_BUFFERS:
3386 g_value_set_uint (value, queue->max_level.buffers);
3388 case PROP_MAX_SIZE_TIME:
3389 g_value_set_uint64 (value, queue->max_level.time);
3391 case PROP_USE_BUFFERING:
3392 g_value_set_boolean (value, queue->use_buffering);
3394 case PROP_USE_RATE_ESTIMATE:
3395 g_value_set_boolean (value, queue->use_rate_estimate);
3397 case PROP_LOW_PERCENT:
3398 g_value_set_int (value, queue->low_percent);
3400 case PROP_HIGH_PERCENT:
3401 g_value_set_int (value, queue->high_percent);
3403 case PROP_TEMP_TEMPLATE:
3404 g_value_set_string (value, queue->temp_template);
3406 case PROP_TEMP_LOCATION:
3407 g_value_set_string (value, queue->temp_location);
3409 case PROP_TEMP_REMOVE:
3410 g_value_set_boolean (value, queue->temp_remove);
3412 case PROP_RING_BUFFER_MAX_SIZE:
3413 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3420 GST_QUEUE2_MUTEX_UNLOCK (queue);