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