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); \
209 #define SET_PERCENT(q, perc) G_STMT_START { \
210 if (perc != q->buffering_percent) { \
211 q->buffering_percent = perc; \
212 q->percent_changed = TRUE; \
213 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
214 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
215 &q->buffering_left); \
220 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
221 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
222 "dataflow inside the queue element");
223 #define gst_queue2_parent_class parent_class
224 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
226 static void gst_queue2_finalize (GObject * object);
228 static void gst_queue2_set_property (GObject * object,
229 guint prop_id, const GValue * value, GParamSpec * pspec);
230 static void gst_queue2_get_property (GObject * object,
231 guint prop_id, GValue * value, GParamSpec * pspec);
233 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
235 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
236 GstBufferList * buffer_list);
237 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
238 static void gst_queue2_loop (GstPad * pad);
240 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
242 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
245 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
247 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
249 static gboolean gst_queue2_handle_query (GstElement * element,
252 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
253 guint64 offset, guint length, GstBuffer ** buffer);
255 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
256 GstPadMode mode, gboolean active);
257 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
258 GstPadMode mode, gboolean active);
259 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
260 GstStateChange transition);
262 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
263 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
265 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
266 static void update_in_rates (GstQueue2 * queue);
267 static void gst_queue2_post_buffering (GstQueue2 * queue);
271 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
272 GST_QUEUE2_ITEM_TYPE_BUFFER,
273 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
274 GST_QUEUE2_ITEM_TYPE_EVENT,
275 GST_QUEUE2_ITEM_TYPE_QUERY
280 GstQueue2ItemType type;
284 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
287 gst_queue2_class_init (GstQueue2Class * klass)
289 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
290 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
292 gobject_class->set_property = gst_queue2_set_property;
293 gobject_class->get_property = gst_queue2_get_property;
296 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
297 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
298 "Current amount of data in the queue (bytes)",
299 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
300 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
301 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
302 "Current number of buffers in the queue",
303 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
305 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
306 "Current amount of data in the queue (in ns)",
307 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
310 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
311 "Max. amount of data in the queue (bytes, 0=disable)",
312 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
313 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
314 G_PARAM_STATIC_STRINGS));
315 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
316 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
317 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
318 DEFAULT_MAX_SIZE_BUFFERS,
319 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
320 G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
322 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
323 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
324 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
325 G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
328 g_param_spec_boolean ("use-buffering", "Use buffering",
329 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
330 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
331 G_PARAM_STATIC_STRINGS));
332 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
333 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
334 "Estimate the bitrate of the stream to calculate time level",
335 DEFAULT_USE_RATE_ESTIMATE,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
338 g_param_spec_int ("low-percent", "Low percent",
339 "Low threshold for buffering to start. Only used if use-buffering is True",
340 0, 100, DEFAULT_LOW_PERCENT,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
343 g_param_spec_int ("high-percent", "High percent",
344 "High threshold for buffering to finish. Only used if use-buffering is True",
345 0, 100, DEFAULT_HIGH_PERCENT,
346 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
349 g_param_spec_string ("temp-template", "Temporary File Template",
350 "File template to store temporary files in, should contain directory "
351 "and XXXXXX. (NULL == disabled)",
352 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
355 g_param_spec_string ("temp-location", "Temporary File Location",
356 "Location to store temporary files in (Only read this property, "
357 "use temp-template to configure the name template)",
358 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
361 * GstQueue2:temp-remove
363 * When temp-template is set, remove the temporary file when going to READY.
365 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
366 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
367 "Remove the temp-location after use",
368 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 * GstQueue2:ring-buffer-max-size
373 * The maximum size of the ring buffer in bytes. If set to 0, the ring
374 * buffer is disabled. Default 0.
376 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
377 g_param_spec_uint64 ("ring-buffer-max-size",
378 "Max. ring buffer size (bytes)",
379 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
380 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 /* set several parent class virtual functions */
384 gobject_class->finalize = gst_queue2_finalize;
386 gst_element_class_add_pad_template (gstelement_class,
387 gst_static_pad_template_get (&srctemplate));
388 gst_element_class_add_pad_template (gstelement_class,
389 gst_static_pad_template_get (&sinktemplate));
391 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
394 "Erik Walthinsen <omega@cse.ogi.edu>, "
395 "Wim Taymans <wim.taymans@gmail.com>");
397 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
398 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
402 gst_queue2_init (GstQueue2 * queue)
404 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
406 gst_pad_set_chain_function (queue->sinkpad,
407 GST_DEBUG_FUNCPTR (gst_queue2_chain));
408 gst_pad_set_chain_list_function (queue->sinkpad,
409 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
410 gst_pad_set_activatemode_function (queue->sinkpad,
411 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
412 gst_pad_set_event_function (queue->sinkpad,
413 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
414 gst_pad_set_query_function (queue->sinkpad,
415 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
416 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
417 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
419 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
421 gst_pad_set_activatemode_function (queue->srcpad,
422 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
423 gst_pad_set_getrange_function (queue->srcpad,
424 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
425 gst_pad_set_event_function (queue->srcpad,
426 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
427 gst_pad_set_query_function (queue->srcpad,
428 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
429 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
430 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
433 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
434 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
435 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
436 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
437 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
438 queue->use_buffering = DEFAULT_USE_BUFFERING;
439 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
440 queue->low_percent = DEFAULT_LOW_PERCENT;
441 queue->high_percent = DEFAULT_HIGH_PERCENT;
443 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
444 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
446 queue->sinktime = GST_CLOCK_TIME_NONE;
447 queue->srctime = GST_CLOCK_TIME_NONE;
448 queue->sink_tainted = TRUE;
449 queue->src_tainted = TRUE;
451 queue->srcresult = GST_FLOW_FLUSHING;
452 queue->sinkresult = GST_FLOW_FLUSHING;
453 queue->is_eos = FALSE;
454 queue->in_timer = g_timer_new ();
455 queue->out_timer = g_timer_new ();
457 g_mutex_init (&queue->qlock);
458 queue->waiting_add = FALSE;
459 g_cond_init (&queue->item_add);
460 queue->waiting_del = FALSE;
461 g_cond_init (&queue->item_del);
462 g_queue_init (&queue->queue);
464 g_cond_init (&queue->query_handled);
465 queue->last_query = FALSE;
467 g_mutex_init (&queue->buffering_post_lock);
468 queue->buffering_percent = 100;
470 /* tempfile related */
471 queue->temp_template = NULL;
472 queue->temp_location = NULL;
473 queue->temp_remove = DEFAULT_TEMP_REMOVE;
475 queue->ring_buffer = NULL;
476 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
478 GST_DEBUG_OBJECT (queue,
479 "initialized queue's not_empty & not_full conditions");
482 /* called only once, as opposed to dispose */
484 gst_queue2_finalize (GObject * object)
486 GstQueue2 *queue = GST_QUEUE2 (object);
488 GST_DEBUG_OBJECT (queue, "finalizing queue");
490 while (!g_queue_is_empty (&queue->queue)) {
491 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
493 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
494 gst_mini_object_unref (qitem->item);
495 g_slice_free (GstQueue2Item, qitem);
498 queue->last_query = FALSE;
499 g_queue_clear (&queue->queue);
500 g_mutex_clear (&queue->qlock);
501 g_mutex_clear (&queue->buffering_post_lock);
502 g_cond_clear (&queue->item_add);
503 g_cond_clear (&queue->item_del);
504 g_cond_clear (&queue->query_handled);
505 g_timer_destroy (queue->in_timer);
506 g_timer_destroy (queue->out_timer);
508 /* temp_file path cleanup */
509 g_free (queue->temp_template);
510 g_free (queue->temp_location);
512 G_OBJECT_CLASS (parent_class)->finalize (object);
516 debug_ranges (GstQueue2 * queue)
518 GstQueue2Range *walk;
520 for (walk = queue->ranges; walk; walk = walk->next) {
521 GST_DEBUG_OBJECT (queue,
522 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
523 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
524 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
525 walk->rb_writing_pos, walk->reading_pos,
526 walk == queue->current ? "**y**" : " n ");
530 /* clear all the downloaded ranges */
532 clean_ranges (GstQueue2 * queue)
534 GST_DEBUG_OBJECT (queue, "clean queue ranges");
536 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
537 queue->ranges = NULL;
538 queue->current = NULL;
541 /* find a range that contains @offset or NULL when nothing does */
542 static GstQueue2Range *
543 find_range (GstQueue2 * queue, guint64 offset)
545 GstQueue2Range *range = NULL;
546 GstQueue2Range *walk;
548 /* first do a quick check for the current range */
549 for (walk = queue->ranges; walk; walk = walk->next) {
550 if (offset >= walk->offset && offset <= walk->writing_pos) {
551 /* we can reuse an existing range */
557 GST_DEBUG_OBJECT (queue,
558 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
559 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
561 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
567 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
569 guint64 max_reading_pos, writing_pos;
571 writing_pos = range->writing_pos;
572 max_reading_pos = range->max_reading_pos;
574 if (writing_pos > max_reading_pos)
575 queue->cur_level.bytes = writing_pos - max_reading_pos;
577 queue->cur_level.bytes = 0;
580 /* make a new range for @offset or reuse an existing range */
581 static GstQueue2Range *
582 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
584 GstQueue2Range *range, *prev, *next;
586 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
588 if ((range = find_range (queue, offset))) {
589 GST_DEBUG_OBJECT (queue,
590 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
592 if (update_existing && range->writing_pos != offset) {
593 GST_DEBUG_OBJECT (queue, "updating range writing position to "
594 "%" G_GUINT64_FORMAT, offset);
595 range->writing_pos = offset;
598 GST_DEBUG_OBJECT (queue,
599 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
601 range = g_slice_new0 (GstQueue2Range);
602 range->offset = offset;
603 /* we want to write to the next location in the ring buffer */
604 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
605 range->writing_pos = offset;
606 range->rb_writing_pos = range->rb_offset;
607 range->reading_pos = offset;
608 range->max_reading_pos = offset;
612 next = queue->ranges;
614 if (next->offset > offset) {
615 /* insert before next */
616 GST_DEBUG_OBJECT (queue,
617 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
629 queue->ranges = range;
631 debug_ranges (queue);
633 /* update the stats for this range */
634 update_cur_level (queue, range);
640 /* clear and init the download ranges for offset 0 */
642 init_ranges (GstQueue2 * queue)
644 GST_DEBUG_OBJECT (queue, "init queue ranges");
646 /* get rid of all the current ranges */
647 clean_ranges (queue);
648 /* make a range for offset 0 */
649 queue->current = add_range (queue, 0, TRUE);
652 /* calculate the diff between running time on the sink and src of the queue.
653 * This is the total amount of time in the queue. */
655 update_time_level (GstQueue2 * queue)
657 if (queue->sink_tainted) {
659 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
660 queue->sink_segment.position);
661 queue->sink_tainted = FALSE;
664 if (queue->src_tainted) {
666 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
667 queue->src_segment.position);
668 queue->src_tainted = FALSE;
671 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
672 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
674 if (queue->sinktime != GST_CLOCK_TIME_NONE
675 && queue->srctime != GST_CLOCK_TIME_NONE
676 && queue->sinktime >= queue->srctime)
677 queue->cur_level.time = queue->sinktime - queue->srctime;
679 queue->cur_level.time = 0;
682 /* take a SEGMENT event and apply the values to segment, updating the time
685 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
688 gst_event_copy_segment (event, segment);
690 if (segment->format == GST_FORMAT_BYTES) {
691 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
692 /* start is where we'll be getting from and as such writing next */
693 queue->current = add_range (queue, segment->start, TRUE);
697 /* now configure the values, we use these to track timestamps on the
699 if (segment->format != GST_FORMAT_TIME) {
700 /* non-time format, pretent the current time segment is closed with a
701 * 0 start and unknown stop time. */
702 segment->format = GST_FORMAT_TIME;
708 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
711 queue->sink_tainted = TRUE;
713 queue->src_tainted = TRUE;
715 /* segment can update the time level of the queue */
716 update_time_level (queue);
720 apply_gap (GstQueue2 * queue, GstEvent * event,
721 GstSegment * segment, gboolean is_sink)
723 GstClockTime timestamp;
724 GstClockTime duration;
726 gst_event_parse_gap (event, ×tamp, &duration);
728 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
730 if (GST_CLOCK_TIME_IS_VALID (duration)) {
731 timestamp += duration;
734 segment->position = timestamp;
737 queue->sink_tainted = TRUE;
739 queue->src_tainted = TRUE;
741 /* calc diff with other end */
742 update_time_level (queue);
746 /* take a buffer and update segment, updating the time level of the queue. */
748 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
751 GstClockTime duration, timestamp;
753 timestamp = GST_BUFFER_TIMESTAMP (buffer);
754 duration = GST_BUFFER_DURATION (buffer);
756 /* if no timestamp is set, assume it's continuous with the previous
758 if (timestamp == GST_CLOCK_TIME_NONE)
759 timestamp = segment->position;
762 if (duration != GST_CLOCK_TIME_NONE)
763 timestamp += duration;
765 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
766 GST_TIME_ARGS (timestamp));
768 segment->position = timestamp;
771 queue->sink_tainted = TRUE;
773 queue->src_tainted = TRUE;
775 /* calc diff with other end */
776 update_time_level (queue);
780 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
782 GstClockTime *timestamp = data;
784 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
785 " duration %" GST_TIME_FORMAT, idx,
786 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
787 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
789 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
790 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
792 if (GST_BUFFER_DURATION_IS_VALID (*buf))
793 *timestamp += GST_BUFFER_DURATION (*buf);
795 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
799 /* take a buffer list and update segment, updating the time level of the queue */
801 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
802 GstSegment * segment, gboolean is_sink)
804 GstClockTime timestamp;
806 /* if no timestamp is set, assume it's continuous with the previous time */
807 timestamp = segment->position;
809 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
811 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
812 GST_TIME_ARGS (timestamp));
814 segment->position = timestamp;
817 queue->sink_tainted = TRUE;
819 queue->src_tainted = TRUE;
821 /* calc diff with other end */
822 update_time_level (queue);
826 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
831 if (queue->high_percent <= 0) {
835 *is_buffering = FALSE;
838 #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)
841 /* on EOS we are always 100% full, we set the var here so that it we can
842 * reuse the logic below to stop buffering */
844 GST_LOG_OBJECT (queue, "we are EOS");
846 /* figure out the percent we are filled, we take the max of all formats. */
847 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
848 perc = GET_PERCENT (bytes, 0);
850 guint64 rb_size = queue->ring_buffer_max_size;
851 perc = GET_PERCENT (bytes, rb_size);
853 perc = MAX (perc, GET_PERCENT (time, 0));
854 perc = MAX (perc, GET_PERCENT (buffers, 0));
856 /* also apply the rate estimate when we need to */
857 if (queue->use_rate_estimate)
858 perc = MAX (perc, GET_PERCENT (rate_time, 0));
863 *is_buffering = queue->is_buffering;
865 /* scale to high percent so that it becomes the 100% mark */
866 perc = perc * 100 / queue->high_percent;
874 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
881 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
882 gint * avg_in, gint * avg_out, gint64 * buffering_left)
885 if (!QUEUE_IS_USING_QUEUE (queue)) {
886 if (QUEUE_IS_USING_RING_BUFFER (queue))
887 *mode = GST_BUFFERING_TIMESHIFT;
889 *mode = GST_BUFFERING_DOWNLOAD;
891 *mode = GST_BUFFERING_STREAM;
896 *avg_in = queue->byte_in_rate;
898 *avg_out = queue->byte_out_rate;
900 if (buffering_left) {
901 *buffering_left = (percent == 100 ? 0 : -1);
903 if (queue->use_rate_estimate) {
906 max = queue->max_level.rate_time;
907 cur = queue->cur_level.rate_time;
909 if (percent != 100 && max > cur)
910 *buffering_left = (max - cur) / 1000000;
916 gst_queue2_post_buffering (GstQueue2 * queue)
918 GstMessage *msg = NULL;
920 g_mutex_lock (&queue->buffering_post_lock);
921 GST_QUEUE2_MUTEX_LOCK (queue);
922 if (queue->percent_changed) {
923 gint percent = queue->buffering_percent;
925 queue->percent_changed = FALSE;
927 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
928 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
930 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
931 queue->avg_out, queue->buffering_left);
933 GST_QUEUE2_MUTEX_UNLOCK (queue);
936 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
938 g_mutex_unlock (&queue->buffering_post_lock);
942 update_buffering (GstQueue2 * queue)
946 /* Ensure the variables used to calculate buffering state are up-to-date. */
948 update_cur_level (queue, queue->current);
949 update_in_rates (queue);
951 if (!get_buffering_percent (queue, NULL, &percent))
954 if (queue->is_buffering) {
955 /* if we were buffering see if we reached the high watermark */
956 if (percent >= queue->high_percent)
957 queue->is_buffering = FALSE;
959 SET_PERCENT (queue, percent);
961 /* we were not buffering, check if we need to start buffering if we drop
962 * below the low threshold */
963 if (percent < queue->low_percent) {
964 queue->is_buffering = TRUE;
965 SET_PERCENT (queue, percent);
971 reset_rate_timer (GstQueue2 * queue)
974 queue->bytes_out = 0;
975 queue->byte_in_rate = 0.0;
976 queue->byte_in_period = 0;
977 queue->byte_out_rate = 0.0;
978 queue->last_in_elapsed = 0.0;
979 queue->last_out_elapsed = 0.0;
980 queue->in_timer_started = FALSE;
981 queue->out_timer_started = FALSE;
984 /* the interval in seconds to recalculate the rate */
985 #define RATE_INTERVAL 0.2
986 /* Tuning for rate estimation. We use a large window for the input rate because
987 * it should be stable when connected to a network. The output rate is less
988 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
989 * therefore adapt more quickly.
990 * However, initial input rate may be subject to a burst, and should therefore
991 * initially also adapt more quickly to changes, and only later on give higher
992 * weight to previous values. */
993 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
994 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
997 update_in_rates (GstQueue2 * queue)
999 gdouble elapsed, period;
1000 gdouble byte_in_rate;
1002 if (!queue->in_timer_started) {
1003 queue->in_timer_started = TRUE;
1004 g_timer_start (queue->in_timer);
1008 elapsed = g_timer_elapsed (queue->in_timer, NULL);
1010 /* recalc after each interval. */
1011 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1012 period = elapsed - queue->last_in_elapsed;
1014 GST_DEBUG_OBJECT (queue,
1015 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1016 period, queue->bytes_in, queue->byte_in_period);
1018 byte_in_rate = queue->bytes_in / period;
1020 if (queue->byte_in_rate == 0.0)
1021 queue->byte_in_rate = byte_in_rate;
1023 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1024 (double) queue->byte_in_period, period);
1026 /* another data point, cap at 16 for long time running average */
1027 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1028 queue->byte_in_period += period;
1030 /* reset the values to calculate rate over the next interval */
1031 queue->last_in_elapsed = elapsed;
1032 queue->bytes_in = 0;
1035 if (queue->byte_in_rate > 0.0) {
1036 queue->cur_level.rate_time =
1037 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1039 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1040 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1044 update_out_rates (GstQueue2 * queue)
1046 gdouble elapsed, period;
1047 gdouble byte_out_rate;
1049 if (!queue->out_timer_started) {
1050 queue->out_timer_started = TRUE;
1051 g_timer_start (queue->out_timer);
1055 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1057 /* recalc after each interval. */
1058 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1059 period = elapsed - queue->last_out_elapsed;
1061 GST_DEBUG_OBJECT (queue,
1062 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1064 byte_out_rate = queue->bytes_out / period;
1066 if (queue->byte_out_rate == 0.0)
1067 queue->byte_out_rate = byte_out_rate;
1069 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1071 /* reset the values to calculate rate over the next interval */
1072 queue->last_out_elapsed = elapsed;
1073 queue->bytes_out = 0;
1075 if (queue->byte_in_rate > 0.0) {
1076 queue->cur_level.rate_time =
1077 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1079 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1080 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1084 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1086 guint64 reading_pos, max_reading_pos;
1089 max_reading_pos = range->max_reading_pos;
1091 max_reading_pos = MAX (max_reading_pos, reading_pos);
1093 GST_DEBUG_OBJECT (queue,
1094 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1095 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1096 range->max_reading_pos = max_reading_pos;
1098 update_cur_level (queue, range);
1102 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1107 /* until we receive the FLUSH_STOP from this seek, we skip data */
1108 queue->seeking = TRUE;
1109 GST_QUEUE2_MUTEX_UNLOCK (queue);
1111 debug_ranges (queue);
1113 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1116 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1117 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1118 GST_SEEK_TYPE_NONE, -1);
1120 res = gst_pad_push_event (queue->sinkpad, event);
1121 GST_QUEUE2_MUTEX_LOCK (queue);
1124 /* Between us sending the seek event and re-acquiring the lock, the source
1125 * thread might already have pushed data and moved along the range's
1126 * writing_pos beyond the seek offset. In that case we don't want to set
1127 * the writing position back to the requested seek position, as it would
1128 * cause data to be written to the wrong offset in the file or ring buffer.
1129 * We still do the add_range call to switch the current range to the
1130 * requested range, or create one if one doesn't exist yet. */
1131 queue->current = add_range (queue, offset, FALSE);
1137 /* get the threshold for when we decide to seek rather than wait */
1139 get_seek_threshold (GstQueue2 * queue)
1143 /* FIXME, find a good threshold based on the incoming rate. */
1144 threshold = 1024 * 512;
1146 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1147 threshold = MIN (threshold,
1148 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1153 /* see if there is enough data in the file to read a full buffer */
1155 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1157 GstQueue2Range *range;
1159 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1162 if ((range = find_range (queue, offset))) {
1163 if (queue->current != range) {
1164 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1165 perform_seek_to_offset (queue, range->writing_pos);
1168 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1169 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1171 /* we have a range for offset */
1172 GST_DEBUG_OBJECT (queue,
1173 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1174 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1176 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1179 if (offset + length <= range->writing_pos)
1182 GST_DEBUG_OBJECT (queue,
1183 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1184 (offset + length) - range->writing_pos);
1187 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1188 " len %u", offset, length);
1189 /* we don't have the range, see how far away we are */
1190 if (!queue->is_eos && queue->current) {
1191 guint64 threshold = get_seek_threshold (queue);
1193 if (offset >= queue->current->offset && offset <=
1194 queue->current->writing_pos + threshold) {
1195 GST_INFO_OBJECT (queue,
1196 "requested data is within range, wait for data");
1201 /* too far away, do a seek */
1202 perform_seek_to_offset (queue, offset);
1209 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1210 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1211 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1213 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1216 static GstFlowReturn
1217 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1218 guint8 * dst, gint64 * read_return)
1220 guint8 *ring_buffer;
1223 ring_buffer = queue->ring_buffer;
1225 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1228 /* this should not block */
1229 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1231 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1232 res = fread (dst, 1, length, queue->temp_file);
1234 memcpy (dst, ring_buffer + offset, length);
1238 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1240 if (G_UNLIKELY (res < length)) {
1241 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1242 goto could_not_read;
1243 /* check for errors or EOF */
1244 if (ferror (queue->temp_file))
1245 goto could_not_read;
1246 if (feof (queue->temp_file) && length > 0)
1256 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1257 return GST_FLOW_ERROR;
1261 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1262 return GST_FLOW_ERROR;
1266 GST_DEBUG ("non-regular file hits EOS");
1267 return GST_FLOW_EOS;
1271 static GstFlowReturn
1272 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1273 GstBuffer ** buffer)
1278 guint64 file_offset;
1279 guint block_length, remaining, read_length;
1283 GstFlowReturn ret = GST_FLOW_OK;
1285 /* allocate the output buffer of the requested size */
1286 if (*buffer == NULL)
1287 buf = gst_buffer_new_allocate (NULL, length, NULL);
1291 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1294 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1298 rb_size = queue->ring_buffer_max_size;
1299 max_size = QUEUE_MAX_BYTES (queue);
1302 while (remaining > 0) {
1303 /* configure how much/whether to read */
1304 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1307 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1310 /* calculate how far away the offset is */
1311 if (queue->current->writing_pos > rpos)
1312 level = queue->current->writing_pos - rpos;
1316 GST_DEBUG_OBJECT (queue,
1317 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1318 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1319 rpos, queue->current->writing_pos, level, max_size);
1321 if (level >= max_size) {
1322 /* we don't have the data but if we have a ring buffer that is full, we
1324 GST_DEBUG_OBJECT (queue,
1325 "ring buffer full, reading QUEUE_MAX_BYTES %"
1326 G_GUINT64_FORMAT " bytes", max_size);
1327 read_length = max_size;
1328 } else if (queue->is_eos) {
1329 /* won't get any more data so read any data we have */
1331 GST_DEBUG_OBJECT (queue,
1332 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1334 read_length = level;
1342 if (read_length == 0) {
1343 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1344 GST_DEBUG_OBJECT (queue,
1345 "update current position [%" G_GUINT64_FORMAT "-%"
1346 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1347 update_cur_pos (queue, queue->current, rpos);
1348 GST_QUEUE2_SIGNAL_DEL (queue);
1351 if (queue->use_buffering)
1352 update_buffering (queue);
1354 GST_DEBUG_OBJECT (queue, "waiting for add");
1355 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1359 /* we have the requested data so read it */
1360 read_length = remaining;
1363 /* set range reading_pos to actual reading position for this read */
1364 queue->current->reading_pos = rpos;
1366 /* configure how much and from where to read */
1367 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1369 (queue->current->rb_offset + (rpos -
1370 queue->current->offset)) % rb_size;
1371 if (file_offset + read_length > rb_size) {
1372 block_length = rb_size - file_offset;
1374 block_length = read_length;
1378 block_length = read_length;
1381 /* while we still have data to read, we loop */
1382 while (read_length > 0) {
1386 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1387 data, &read_return);
1388 if (ret != GST_FLOW_OK)
1391 file_offset += read_return;
1392 if (QUEUE_IS_USING_RING_BUFFER (queue))
1393 file_offset %= rb_size;
1395 data += read_return;
1396 read_length -= read_return;
1397 block_length = read_length;
1398 remaining -= read_return;
1400 rpos = (queue->current->reading_pos += read_return);
1401 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1403 GST_QUEUE2_SIGNAL_DEL (queue);
1404 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1407 gst_buffer_unmap (buf, &info);
1408 gst_buffer_resize (buf, 0, length);
1410 GST_BUFFER_OFFSET (buf) = offset;
1411 GST_BUFFER_OFFSET_END (buf) = offset + length;
1420 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1421 gst_buffer_unmap (buf, &info);
1422 if (*buffer == NULL)
1423 gst_buffer_unref (buf);
1424 return GST_FLOW_EOS;
1428 GST_DEBUG_OBJECT (queue, "we are flushing");
1429 gst_buffer_unmap (buf, &info);
1430 if (*buffer == NULL)
1431 gst_buffer_unref (buf);
1432 return GST_FLOW_FLUSHING;
1436 GST_DEBUG_OBJECT (queue, "we have a read error");
1437 gst_buffer_unmap (buf, &info);
1438 if (*buffer == NULL)
1439 gst_buffer_unref (buf);
1444 /* should be called with QUEUE_LOCK */
1445 static GstMiniObject *
1446 gst_queue2_read_item_from_file (GstQueue2 * queue)
1448 GstMiniObject *item;
1450 if (queue->stream_start_event != NULL) {
1451 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1452 queue->stream_start_event = NULL;
1453 } else if (queue->starting_segment != NULL) {
1454 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1455 queue->starting_segment = NULL;
1458 GstBuffer *buffer = NULL;
1459 guint64 reading_pos;
1461 reading_pos = queue->current->reading_pos;
1464 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1469 item = GST_MINI_OBJECT_CAST (buffer);
1472 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1482 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1483 * the temp filename. */
1485 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1490 if (queue->temp_file)
1491 goto already_opened;
1493 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1495 /* If temp_template was set, allocate a filename and open that filen */
1498 if (queue->temp_template == NULL)
1501 /* make copy of the template, we don't want to change this */
1502 name = g_strdup (queue->temp_template);
1503 fd = g_mkstemp (name);
1505 goto mkstemp_failed;
1507 /* open the file for update/writing */
1508 queue->temp_file = fdopen (fd, "wb+");
1509 /* error creating file */
1510 if (queue->temp_file == NULL)
1513 g_free (queue->temp_location);
1514 queue->temp_location = name;
1516 GST_QUEUE2_MUTEX_UNLOCK (queue);
1518 /* we can't emit the notify with the lock */
1519 g_object_notify (G_OBJECT (queue), "temp-location");
1521 GST_QUEUE2_MUTEX_LOCK (queue);
1523 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1530 GST_DEBUG_OBJECT (queue, "temp file was already open");
1535 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1536 (_("No Temp directory specified.")), (NULL));
1541 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1542 (_("Could not create temp file \"%s\"."), queue->temp_template),
1549 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1550 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1559 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1562 if (queue->temp_file == NULL)
1565 GST_DEBUG_OBJECT (queue, "closing temp file");
1567 fflush (queue->temp_file);
1568 fclose (queue->temp_file);
1570 if (queue->temp_remove) {
1571 if (remove (queue->temp_location) < 0) {
1572 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1573 queue->temp_location, g_strerror (errno));
1577 queue->temp_file = NULL;
1578 clean_ranges (queue);
1582 gst_queue2_flush_temp_file (GstQueue2 * queue)
1584 if (queue->temp_file == NULL)
1587 GST_DEBUG_OBJECT (queue, "flushing temp file");
1589 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1593 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1595 if (!QUEUE_IS_USING_QUEUE (queue)) {
1596 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1597 gst_queue2_flush_temp_file (queue);
1598 init_ranges (queue);
1600 while (!g_queue_is_empty (&queue->queue)) {
1601 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1603 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1604 && GST_EVENT_IS_STICKY (qitem->item)
1605 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1606 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1607 gst_pad_store_sticky_event (queue->srcpad,
1608 GST_EVENT_CAST (qitem->item));
1611 /* Then lose another reference because we are supposed to destroy that
1612 data when flushing */
1613 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1614 gst_mini_object_unref (qitem->item);
1615 g_slice_free (GstQueue2Item, qitem);
1618 queue->last_query = FALSE;
1619 g_cond_signal (&queue->query_handled);
1620 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1621 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1622 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1623 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1624 queue->sink_tainted = queue->src_tainted = TRUE;
1625 if (queue->starting_segment != NULL)
1626 gst_event_unref (queue->starting_segment);
1627 queue->starting_segment = NULL;
1628 queue->segment_event_received = FALSE;
1629 gst_event_replace (&queue->stream_start_event, NULL);
1631 /* we deleted a lot of something */
1632 GST_QUEUE2_SIGNAL_DEL (queue);
1636 gst_queue2_wait_free_space (GstQueue2 * queue)
1638 /* We make space available if we're "full" according to whatever
1639 * the user defined as "full". */
1640 if (gst_queue2_is_filled (queue)) {
1643 /* pause the timer while we wait. The fact that we are waiting does not mean
1644 * the byterate on the input pad is lower */
1645 if ((started = queue->in_timer_started))
1646 g_timer_stop (queue->in_timer);
1648 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1649 "queue is full, waiting for free space");
1651 /* Wait for space to be available, we could be unlocked because of a flush. */
1652 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1654 while (gst_queue2_is_filled (queue));
1656 /* and continue if we were running before */
1658 g_timer_continue (queue->in_timer);
1665 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1671 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1674 guint8 *data, *ring_buffer;
1675 guint size, rb_size;
1676 guint64 writing_pos, new_writing_pos;
1677 GstQueue2Range *range, *prev, *next;
1678 gboolean do_seek = FALSE;
1680 if (QUEUE_IS_USING_RING_BUFFER (queue))
1681 writing_pos = queue->current->rb_writing_pos;
1683 writing_pos = queue->current->writing_pos;
1684 ring_buffer = queue->ring_buffer;
1685 rb_size = queue->ring_buffer_max_size;
1687 gst_buffer_map (buffer, &info, GST_MAP_READ);
1692 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1696 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1697 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1698 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1699 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1700 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1706 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1709 /* calculate the space in the ring buffer not used by data from
1710 * the current range */
1711 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1712 /* wait until there is some free space */
1713 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1715 /* get the amount of space we have */
1716 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1718 /* calculate if we need to split or if we can write the entire
1720 to_write = MIN (size, space);
1722 /* the writing position in the ring buffer after writing (part
1723 * or all of) the buffer */
1724 new_writing_pos = (writing_pos + to_write) % rb_size;
1727 range = queue->ranges;
1729 /* if we need to overwrite data in the ring buffer, we need to
1732 * warning: this code is complicated and includes some
1733 * simplifications - pen, paper and diagrams for the cases
1736 guint64 range_data_start, range_data_end;
1737 GstQueue2Range *range_to_destroy = NULL;
1739 range_data_start = range->rb_offset;
1740 range_data_end = range->rb_writing_pos;
1742 /* handle the special case where the range has no data in it */
1743 if (range->writing_pos == range->offset) {
1744 if (range != queue->current) {
1745 GST_DEBUG_OBJECT (queue,
1746 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1747 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1749 range_to_destroy = range;
1751 prev->next = range->next;
1756 if (range_data_end > range_data_start) {
1757 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1760 if (new_writing_pos > range_data_start) {
1761 if (new_writing_pos >= range_data_end) {
1762 GST_DEBUG_OBJECT (queue,
1763 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1764 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1766 range_to_destroy = range;
1768 prev->next = range->next;
1770 GST_DEBUG_OBJECT (queue,
1771 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1772 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1773 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1774 range->offset + new_writing_pos - range_data_start,
1776 range->offset += (new_writing_pos - range_data_start);
1777 range->rb_offset = new_writing_pos;
1781 guint64 new_wpos_virt = writing_pos + to_write;
1783 if (new_wpos_virt <= range_data_start)
1786 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1787 GST_DEBUG_OBJECT (queue,
1788 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1789 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1791 range_to_destroy = range;
1793 prev->next = range->next;
1795 GST_DEBUG_OBJECT (queue,
1796 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1797 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1798 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1799 range->offset + new_writing_pos - range_data_start,
1801 range->offset += (new_wpos_virt - range_data_start);
1802 range->rb_offset = new_writing_pos;
1807 if (!range_to_destroy)
1810 range = range->next;
1811 if (range_to_destroy) {
1812 if (range_to_destroy == queue->ranges)
1813 queue->ranges = range;
1814 g_slice_free (GstQueue2Range, range_to_destroy);
1815 range_to_destroy = NULL;
1820 new_writing_pos = writing_pos + to_write;
1823 if (QUEUE_IS_USING_TEMP_FILE (queue)
1824 && FSEEK_FILE (queue->temp_file, writing_pos))
1827 if (new_writing_pos > writing_pos) {
1828 GST_INFO_OBJECT (queue,
1829 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1830 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1831 queue->current->writing_pos, queue->current->rb_writing_pos);
1832 /* either not using ring buffer or no wrapping, just write */
1833 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1834 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1837 memcpy (ring_buffer + writing_pos, data, to_write);
1840 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1841 /* try to merge with next range */
1842 while ((next = queue->current->next)) {
1843 GST_INFO_OBJECT (queue,
1844 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1845 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1846 if (new_writing_pos < next->offset)
1849 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1852 /* remove the group */
1853 queue->current->next = next->next;
1855 /* We use the threshold to decide if we want to do a seek or simply
1856 * read the data again. If there is not so much data in the range we
1857 * prefer to avoid to seek and read it again. */
1858 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1859 /* the new range had more data than the threshold, it's worth keeping
1860 * it and doing a seek. */
1861 new_writing_pos = next->writing_pos;
1864 g_slice_free (GstQueue2Range, next);
1866 goto update_and_signal;
1870 guint block_one, block_two;
1872 block_one = rb_size - writing_pos;
1873 block_two = to_write - block_one;
1875 if (block_one > 0) {
1876 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1877 /* write data to end of ring buffer */
1878 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1879 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1882 memcpy (ring_buffer + writing_pos, data, block_one);
1886 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1889 if (block_two > 0) {
1890 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1891 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1892 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1895 memcpy (ring_buffer, data + block_one, block_two);
1901 /* update the writing positions */
1903 GST_INFO_OBJECT (queue,
1904 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1905 to_write, writing_pos, size);
1907 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1909 queue->current->writing_pos += to_write;
1910 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1912 queue->current->writing_pos = writing_pos = new_writing_pos;
1915 perform_seek_to_offset (queue, new_writing_pos);
1917 update_cur_level (queue, queue->current);
1919 /* update the buffering status */
1920 if (queue->use_buffering)
1921 update_buffering (queue);
1923 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1924 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1926 GST_QUEUE2_SIGNAL_ADD (queue);
1929 gst_buffer_unmap (buffer, &info);
1936 GST_DEBUG_OBJECT (queue, "we are flushing");
1937 gst_buffer_unmap (buffer, &info);
1938 /* FIXME - GST_FLOW_EOS ? */
1943 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1944 gst_buffer_unmap (buffer, &info);
1951 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1955 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1956 (_("Error while writing to download file.")),
1957 ("%s", g_strerror (errno)));
1960 gst_buffer_unmap (buffer, &info);
1966 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1968 GstQueue2 *queue = q;
1970 GST_TRACE_OBJECT (queue,
1971 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1972 gst_buffer_get_size (*buf));
1974 if (!gst_queue2_create_write (queue, *buf)) {
1975 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1982 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1984 guint *p_size = data;
1987 buf_size = gst_buffer_get_size (*buf);
1988 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1989 *p_size += buf_size;
1993 /* enqueue an item an update the level stats */
1995 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1996 GstQueue2ItemType item_type)
1998 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2002 buffer = GST_BUFFER_CAST (item);
2003 size = gst_buffer_get_size (buffer);
2005 /* add buffer to the statistics */
2006 if (QUEUE_IS_USING_QUEUE (queue)) {
2007 queue->cur_level.buffers++;
2008 queue->cur_level.bytes += size;
2010 queue->bytes_in += size;
2012 /* apply new buffer to segment stats */
2013 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
2014 /* update the byterate stats */
2015 update_in_rates (queue);
2017 if (!QUEUE_IS_USING_QUEUE (queue)) {
2018 /* FIXME - check return value? */
2019 gst_queue2_create_write (queue, buffer);
2021 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2022 GstBufferList *buffer_list;
2025 buffer_list = GST_BUFFER_LIST_CAST (item);
2027 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2028 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2030 /* add buffer to the statistics */
2031 if (QUEUE_IS_USING_QUEUE (queue)) {
2032 queue->cur_level.buffers++;
2033 queue->cur_level.bytes += size;
2035 queue->bytes_in += size;
2037 /* apply new buffer to segment stats */
2038 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2040 /* update the byterate stats */
2041 update_in_rates (queue);
2043 if (!QUEUE_IS_USING_QUEUE (queue)) {
2044 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2046 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2049 event = GST_EVENT_CAST (item);
2051 switch (GST_EVENT_TYPE (event)) {
2053 /* Zero the thresholds, this makes sure the queue is completely
2054 * filled and we can read all data from the queue. */
2055 GST_DEBUG_OBJECT (queue, "we have EOS");
2056 queue->is_eos = TRUE;
2058 case GST_EVENT_SEGMENT:
2059 apply_segment (queue, event, &queue->sink_segment, TRUE);
2060 /* This is our first new segment, we hold it
2061 * as we can't save it on the temp file */
2062 if (!QUEUE_IS_USING_QUEUE (queue)) {
2063 if (queue->segment_event_received)
2064 goto unexpected_event;
2066 queue->segment_event_received = TRUE;
2067 if (queue->starting_segment != NULL)
2068 gst_event_unref (queue->starting_segment);
2069 queue->starting_segment = event;
2072 /* a new segment allows us to accept more buffers if we got EOS
2073 * from downstream */
2074 queue->unexpected = FALSE;
2077 apply_gap (queue, event, &queue->sink_segment, TRUE);
2079 case GST_EVENT_STREAM_START:
2080 if (!QUEUE_IS_USING_QUEUE (queue)) {
2081 gst_event_replace (&queue->stream_start_event, event);
2082 gst_event_unref (event);
2086 case GST_EVENT_CAPS:{
2089 gst_event_parse_caps (event, &caps);
2090 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2092 if (!QUEUE_IS_USING_QUEUE (queue)) {
2093 GST_LOG ("Dropping caps event, not using queue");
2094 gst_event_unref (event);
2100 if (!QUEUE_IS_USING_QUEUE (queue))
2101 goto unexpected_event;
2104 } else if (GST_IS_QUERY (item)) {
2105 /* Can't happen as we check that in the caller */
2106 if (!QUEUE_IS_USING_QUEUE (queue))
2107 g_assert_not_reached ();
2109 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2110 item, GST_OBJECT_NAME (queue));
2111 /* we can't really unref since we don't know what it is */
2116 /* update the buffering status */
2117 if (queue->use_buffering)
2118 update_buffering (queue);
2120 if (QUEUE_IS_USING_QUEUE (queue)) {
2121 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2122 qitem->type = item_type;
2124 g_queue_push_tail (&queue->queue, qitem);
2126 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2129 GST_QUEUE2_SIGNAL_ADD (queue);
2138 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2139 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2140 GST_OBJECT_NAME (queue));
2141 gst_event_unref (GST_EVENT_CAST (item));
2146 /* dequeue an item from the queue and update level stats */
2147 static GstMiniObject *
2148 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2150 GstMiniObject *item;
2152 if (!QUEUE_IS_USING_QUEUE (queue)) {
2153 item = gst_queue2_read_item_from_file (queue);
2155 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2161 g_slice_free (GstQueue2Item, qitem);
2167 if (GST_IS_BUFFER (item)) {
2171 buffer = GST_BUFFER_CAST (item);
2172 size = gst_buffer_get_size (buffer);
2173 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2175 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2176 "retrieved buffer %p from queue", buffer);
2178 if (QUEUE_IS_USING_QUEUE (queue)) {
2179 queue->cur_level.buffers--;
2180 queue->cur_level.bytes -= size;
2182 queue->bytes_out += size;
2184 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2185 /* update the byterate stats */
2186 update_out_rates (queue);
2187 /* update the buffering */
2188 if (queue->use_buffering)
2189 update_buffering (queue);
2191 } else if (GST_IS_EVENT (item)) {
2192 GstEvent *event = GST_EVENT_CAST (item);
2194 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2196 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2197 "retrieved event %p from queue", event);
2199 switch (GST_EVENT_TYPE (event)) {
2201 /* queue is empty now that we dequeued the EOS */
2202 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2204 case GST_EVENT_SEGMENT:
2205 apply_segment (queue, event, &queue->src_segment, FALSE);
2208 apply_gap (queue, event, &queue->src_segment, FALSE);
2213 } else if (GST_IS_BUFFER_LIST (item)) {
2214 GstBufferList *buffer_list;
2217 buffer_list = GST_BUFFER_LIST_CAST (item);
2218 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2219 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2221 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2222 "retrieved buffer list %p from queue", buffer_list);
2224 if (QUEUE_IS_USING_QUEUE (queue)) {
2225 queue->cur_level.buffers--;
2226 queue->cur_level.bytes -= size;
2228 queue->bytes_out += size;
2230 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2231 /* update the byterate stats */
2232 update_out_rates (queue);
2233 /* update the buffering */
2234 if (queue->use_buffering)
2235 update_buffering (queue);
2236 } else if (GST_IS_QUERY (item)) {
2237 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2238 "retrieved query %p from queue", item);
2239 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2242 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2243 item, GST_OBJECT_NAME (queue));
2245 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2247 GST_QUEUE2_SIGNAL_DEL (queue);
2254 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2260 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2263 gboolean ret = TRUE;
2266 queue = GST_QUEUE2 (parent);
2268 switch (GST_EVENT_TYPE (event)) {
2269 case GST_EVENT_FLUSH_START:
2271 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2272 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2274 ret = gst_pad_push_event (queue->srcpad, event);
2276 /* now unblock the chain function */
2277 GST_QUEUE2_MUTEX_LOCK (queue);
2278 queue->srcresult = GST_FLOW_FLUSHING;
2279 queue->sinkresult = GST_FLOW_FLUSHING;
2280 /* unblock the loop and chain functions */
2281 GST_QUEUE2_SIGNAL_ADD (queue);
2282 GST_QUEUE2_SIGNAL_DEL (queue);
2283 queue->last_query = FALSE;
2284 g_cond_signal (&queue->query_handled);
2285 GST_QUEUE2_MUTEX_UNLOCK (queue);
2287 /* make sure it pauses, this should happen since we sent
2288 * flush_start downstream. */
2289 gst_pad_pause_task (queue->srcpad);
2290 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2292 GST_QUEUE2_MUTEX_LOCK (queue);
2293 /* flush the sink pad */
2294 queue->sinkresult = GST_FLOW_FLUSHING;
2295 GST_QUEUE2_SIGNAL_DEL (queue);
2296 queue->last_query = FALSE;
2297 g_cond_signal (&queue->query_handled);
2298 GST_QUEUE2_MUTEX_UNLOCK (queue);
2300 gst_event_unref (event);
2304 case GST_EVENT_FLUSH_STOP:
2306 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2308 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2310 ret = gst_pad_push_event (queue->srcpad, event);
2312 GST_QUEUE2_MUTEX_LOCK (queue);
2313 gst_queue2_locked_flush (queue, FALSE, TRUE);
2314 queue->srcresult = GST_FLOW_OK;
2315 queue->sinkresult = GST_FLOW_OK;
2316 queue->is_eos = FALSE;
2317 queue->unexpected = FALSE;
2318 queue->seeking = FALSE;
2319 /* reset rate counters */
2320 reset_rate_timer (queue);
2321 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2322 queue->srcpad, NULL);
2323 GST_QUEUE2_MUTEX_UNLOCK (queue);
2325 GST_QUEUE2_MUTEX_LOCK (queue);
2326 queue->segment_event_received = FALSE;
2327 queue->is_eos = FALSE;
2328 queue->unexpected = FALSE;
2329 queue->sinkresult = GST_FLOW_OK;
2330 queue->seeking = FALSE;
2331 GST_QUEUE2_MUTEX_UNLOCK (queue);
2333 gst_event_unref (event);
2338 if (GST_EVENT_IS_SERIALIZED (event)) {
2339 /* serialized events go in the queue */
2340 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2341 if (queue->srcresult != GST_FLOW_OK) {
2342 /* Errors in sticky event pushing are no problem and ignored here
2343 * as they will cause more meaningful errors during data flow.
2344 * For EOS events, that are not followed by data flow, we still
2345 * return FALSE here though and report an error.
2347 if (!GST_EVENT_IS_STICKY (event)) {
2348 goto out_flow_error;
2349 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2350 if (queue->srcresult == GST_FLOW_NOT_LINKED
2351 || queue->srcresult < GST_FLOW_EOS) {
2352 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2353 (_("Internal data flow error.")),
2354 ("streaming task paused, reason %s (%d)",
2355 gst_flow_get_name (queue->srcresult), queue->srcresult));
2357 goto out_flow_error;
2360 /* refuse more events on EOS */
2363 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2364 GST_QUEUE2_MUTEX_UNLOCK (queue);
2365 gst_queue2_post_buffering (queue);
2367 /* non-serialized events are passed upstream. */
2368 ret = gst_pad_push_event (queue->srcpad, event);
2377 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2378 GST_QUEUE2_MUTEX_UNLOCK (queue);
2379 gst_event_unref (event);
2384 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2385 GST_QUEUE2_MUTEX_UNLOCK (queue);
2386 gst_event_unref (event);
2391 GST_LOG_OBJECT (queue,
2392 "refusing event, we have a downstream flow error: %s",
2393 gst_flow_get_name (queue->srcresult));
2394 GST_QUEUE2_MUTEX_UNLOCK (queue);
2395 gst_event_unref (event);
2401 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2407 queue = GST_QUEUE2 (parent);
2409 switch (GST_QUERY_TYPE (query)) {
2411 if (GST_QUERY_IS_SERIALIZED (query)) {
2412 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2413 /* serialized events go in the queue. We need to be certain that we
2414 * don't cause deadlocks waiting for the query return value. We check if
2415 * the queue is empty (nothing is blocking downstream and the query can
2416 * be pushed for sure) or we are not buffering. If we are buffering,
2417 * the pipeline waits to unblock downstream until our queue fills up
2418 * completely, which can not happen if we block on the query..
2419 * Therefore we only potentially block when we are not buffering. */
2420 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2421 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2422 || !queue->use_buffering)) {
2423 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2424 gst_queue2_locked_enqueue (queue, query,
2425 GST_QUEUE2_ITEM_TYPE_QUERY);
2427 STATUS (queue, queue->sinkpad, "wait for QUERY");
2428 g_cond_wait (&queue->query_handled, &queue->qlock);
2429 if (queue->sinkresult != GST_FLOW_OK)
2431 res = queue->last_query;
2433 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2437 GST_DEBUG_OBJECT (queue,
2438 "refusing query, we are not using the queue");
2441 GST_QUEUE2_MUTEX_UNLOCK (queue);
2442 gst_queue2_post_buffering (queue);
2444 res = gst_pad_query_default (pad, parent, query);
2453 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2454 GST_QUEUE2_MUTEX_UNLOCK (queue);
2460 gst_queue2_is_empty (GstQueue2 * queue)
2462 /* never empty on EOS */
2466 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2467 return queue->current->writing_pos <= queue->current->max_reading_pos;
2469 if (queue->queue.length == 0)
2477 gst_queue2_is_filled (GstQueue2 * queue)
2481 /* always filled on EOS */
2485 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2486 (queue->cur_level.format) >= ((alt_max) ? \
2487 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2489 /* if using a ring buffer we're filled if all ring buffer space is used
2490 * _by the current range_ */
2491 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2492 guint64 rb_size = queue->ring_buffer_max_size;
2493 GST_DEBUG_OBJECT (queue,
2494 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2495 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2496 return CHECK_FILLED (bytes, rb_size);
2499 /* if using file, we're never filled if we don't have EOS */
2500 if (QUEUE_IS_USING_TEMP_FILE (queue))
2503 /* we are never filled when we have no buffers at all */
2504 if (queue->cur_level.buffers == 0)
2507 /* we are filled if one of the current levels exceeds the max */
2508 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2509 || CHECK_FILLED (time, 0);
2511 /* if we need to, use the rate estimate to check against the max time we are
2512 * allowed to queue */
2513 if (queue->use_rate_estimate)
2514 res |= CHECK_FILLED (rate_time, 0);
2520 static GstFlowReturn
2521 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2522 GstMiniObject * item, GstQueue2ItemType item_type)
2524 /* we have to lock the queue since we span threads */
2525 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2526 /* when we received EOS, we refuse more data */
2529 /* when we received unexpected from downstream, refuse more buffers */
2530 if (queue->unexpected)
2531 goto out_unexpected;
2533 /* while we didn't receive the newsegment, we're seeking and we skip data */
2537 if (!gst_queue2_wait_free_space (queue))
2540 /* put buffer in queue now */
2541 gst_queue2_locked_enqueue (queue, item, item_type);
2542 GST_QUEUE2_MUTEX_UNLOCK (queue);
2543 gst_queue2_post_buffering (queue);
2547 /* special conditions */
2550 GstFlowReturn ret = queue->sinkresult;
2552 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2553 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2554 GST_QUEUE2_MUTEX_UNLOCK (queue);
2555 gst_mini_object_unref (item);
2561 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2562 GST_QUEUE2_MUTEX_UNLOCK (queue);
2563 gst_mini_object_unref (item);
2565 return GST_FLOW_EOS;
2569 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2570 GST_QUEUE2_MUTEX_UNLOCK (queue);
2571 gst_mini_object_unref (item);
2577 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2578 GST_QUEUE2_MUTEX_UNLOCK (queue);
2579 gst_mini_object_unref (item);
2581 return GST_FLOW_EOS;
2585 static GstFlowReturn
2586 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2590 queue = GST_QUEUE2 (parent);
2592 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2593 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2594 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2595 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2596 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2598 return gst_queue2_chain_buffer_or_buffer_list (queue,
2599 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2602 static GstFlowReturn
2603 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2604 GstBufferList * buffer_list)
2608 queue = GST_QUEUE2 (parent);
2610 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2611 "received buffer list %p", buffer_list);
2613 return gst_queue2_chain_buffer_or_buffer_list (queue,
2614 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2617 static GstMiniObject *
2618 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2620 GstMiniObject *data;
2622 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2624 /* stop pushing buffers, we dequeue all items until we see an item that we
2625 * can push again, which is EOS or SEGMENT. If there is nothing in the
2626 * queue we can push, we set a flag to make the sinkpad refuse more
2627 * buffers with an EOS return value until we receive something
2628 * pushable again or we get flushed. */
2629 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2630 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2631 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2632 "dropping EOS buffer %p", data);
2633 gst_buffer_unref (GST_BUFFER_CAST (data));
2634 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2635 GstEvent *event = GST_EVENT_CAST (data);
2636 GstEventType type = GST_EVENT_TYPE (event);
2638 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2639 /* we found a pushable item in the queue, push it out */
2640 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2641 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2644 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2645 "dropping EOS event %p", event);
2646 gst_event_unref (event);
2647 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2648 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2649 "dropping EOS buffer list %p", data);
2650 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2651 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2652 queue->last_query = FALSE;
2653 g_cond_signal (&queue->query_handled);
2654 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2657 /* no more items in the queue. Set the unexpected flag so that upstream
2658 * make us refuse any more buffers on the sinkpad. Since we will still
2659 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2660 * task function does not shut down. */
2661 queue->unexpected = TRUE;
2665 /* dequeue an item from the queue an push it downstream. This functions returns
2666 * the result of the push. */
2667 static GstFlowReturn
2668 gst_queue2_push_one (GstQueue2 * queue)
2670 GstFlowReturn result = queue->srcresult;
2671 GstMiniObject *data;
2672 GstQueue2ItemType item_type;
2674 data = gst_queue2_locked_dequeue (queue, &item_type);
2679 STATUS (queue, queue->srcpad, "We have something dequeud");
2680 g_atomic_int_set (&queue->downstream_may_block,
2681 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2682 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2683 GST_QUEUE2_MUTEX_UNLOCK (queue);
2684 gst_queue2_post_buffering (queue);
2686 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2689 buffer = GST_BUFFER_CAST (data);
2691 result = gst_pad_push (queue->srcpad, buffer);
2692 g_atomic_int_set (&queue->downstream_may_block, 0);
2694 /* need to check for srcresult here as well */
2695 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2696 if (result == GST_FLOW_EOS) {
2697 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2700 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2701 * to the caller so that the task function does not shut down */
2702 result = GST_FLOW_OK;
2704 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2705 GstEvent *event = GST_EVENT_CAST (data);
2706 GstEventType type = GST_EVENT_TYPE (event);
2708 gst_pad_push_event (queue->srcpad, event);
2710 /* if we're EOS, return EOS so that the task pauses. */
2711 if (type == GST_EVENT_EOS) {
2712 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2713 "pushed EOS event %p, return EOS", event);
2714 result = GST_FLOW_EOS;
2717 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2718 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2719 GstBufferList *buffer_list;
2721 buffer_list = GST_BUFFER_LIST_CAST (data);
2723 result = gst_pad_push_list (queue->srcpad, buffer_list);
2724 g_atomic_int_set (&queue->downstream_may_block, 0);
2726 /* need to check for srcresult here as well */
2727 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2728 if (result == GST_FLOW_EOS) {
2729 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2732 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2733 * to the caller so that the task function does not shut down */
2734 result = GST_FLOW_OK;
2736 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2737 GstQuery *query = GST_QUERY_CAST (data);
2739 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2740 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2741 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2742 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2743 "did query %p, return %d", query, queue->last_query);
2744 g_cond_signal (&queue->query_handled);
2745 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2746 result = GST_FLOW_OK;
2753 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2754 "exit because we have no item in the queue");
2755 return GST_FLOW_ERROR;
2759 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2760 return GST_FLOW_FLUSHING;
2764 /* called repeatedly with @pad as the source pad. This function should push out
2765 * data to the peer element. */
2767 gst_queue2_loop (GstPad * pad)
2772 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2774 /* have to lock for thread-safety */
2775 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2777 if (gst_queue2_is_empty (queue)) {
2780 /* pause the timer while we wait. The fact that we are waiting does not mean
2781 * the byterate on the output pad is lower */
2782 if ((started = queue->out_timer_started))
2783 g_timer_stop (queue->out_timer);
2785 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2786 "queue is empty, waiting for new data");
2788 /* Wait for data to be available, we could be unlocked because of a flush. */
2789 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2791 while (gst_queue2_is_empty (queue));
2793 /* and continue if we were running before */
2795 g_timer_continue (queue->out_timer);
2797 ret = gst_queue2_push_one (queue);
2798 queue->srcresult = ret;
2799 queue->sinkresult = ret;
2800 if (ret != GST_FLOW_OK)
2803 GST_QUEUE2_MUTEX_UNLOCK (queue);
2804 gst_queue2_post_buffering (queue);
2811 gboolean eos = queue->is_eos;
2812 GstFlowReturn ret = queue->srcresult;
2814 gst_pad_pause_task (queue->srcpad);
2815 GST_QUEUE2_MUTEX_UNLOCK (queue);
2816 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2817 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2818 /* let app know about us giving up if upstream is not expected to do so */
2819 /* EOS is already taken care of elsewhere */
2820 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2821 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2822 (_("Internal data flow error.")),
2823 ("streaming task paused, reason %s (%d)",
2824 gst_flow_get_name (ret), ret));
2825 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2832 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2834 gboolean res = TRUE;
2835 GstQueue2 *queue = GST_QUEUE2 (parent);
2837 #ifndef GST_DISABLE_GST_DEBUG
2838 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2839 event, GST_EVENT_TYPE_NAME (event));
2842 switch (GST_EVENT_TYPE (event)) {
2843 case GST_EVENT_FLUSH_START:
2844 if (QUEUE_IS_USING_QUEUE (queue)) {
2845 /* just forward upstream */
2846 res = gst_pad_push_event (queue->sinkpad, event);
2848 /* now unblock the getrange function */
2849 GST_QUEUE2_MUTEX_LOCK (queue);
2850 GST_DEBUG_OBJECT (queue, "flushing");
2851 queue->srcresult = GST_FLOW_FLUSHING;
2852 GST_QUEUE2_SIGNAL_ADD (queue);
2853 GST_QUEUE2_MUTEX_UNLOCK (queue);
2855 /* when using a temp file, we eat the event */
2857 gst_event_unref (event);
2860 case GST_EVENT_FLUSH_STOP:
2861 if (QUEUE_IS_USING_QUEUE (queue)) {
2862 /* just forward upstream */
2863 res = gst_pad_push_event (queue->sinkpad, event);
2865 /* now unblock the getrange function */
2866 GST_QUEUE2_MUTEX_LOCK (queue);
2867 queue->srcresult = GST_FLOW_OK;
2868 GST_QUEUE2_MUTEX_UNLOCK (queue);
2870 /* when using a temp file, we eat the event */
2872 gst_event_unref (event);
2875 case GST_EVENT_RECONFIGURE:
2876 GST_QUEUE2_MUTEX_LOCK (queue);
2877 /* assume downstream is linked now and try to push again */
2878 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
2879 queue->srcresult = GST_FLOW_OK;
2880 queue->sinkresult = GST_FLOW_OK;
2881 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
2882 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
2886 GST_QUEUE2_MUTEX_UNLOCK (queue);
2888 res = gst_pad_push_event (queue->sinkpad, event);
2891 res = gst_pad_push_event (queue->sinkpad, event);
2899 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2903 queue = GST_QUEUE2 (parent);
2905 switch (GST_QUERY_TYPE (query)) {
2906 case GST_QUERY_POSITION:
2911 if (!gst_pad_peer_query (queue->sinkpad, query))
2914 /* get peer position */
2915 gst_query_parse_position (query, &format, &peer_pos);
2917 /* FIXME: this code assumes that there's no discont in the queue */
2919 case GST_FORMAT_BYTES:
2920 peer_pos -= queue->cur_level.bytes;
2922 case GST_FORMAT_TIME:
2923 peer_pos -= queue->cur_level.time;
2926 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2927 "know how to adjust value", gst_format_get_name (format));
2930 /* set updated position */
2931 gst_query_set_position (query, format, peer_pos);
2934 case GST_QUERY_DURATION:
2936 GST_DEBUG_OBJECT (queue, "doing peer query");
2938 if (!gst_pad_peer_query (queue->sinkpad, query))
2941 GST_DEBUG_OBJECT (queue, "peer query success");
2944 case GST_QUERY_BUFFERING:
2947 gboolean is_buffering;
2948 GstBufferingMode mode;
2949 gint avg_in, avg_out;
2950 gint64 buffering_left;
2952 GST_DEBUG_OBJECT (queue, "query buffering");
2954 get_buffering_percent (queue, &is_buffering, &percent);
2955 gst_query_set_buffering_percent (query, is_buffering, percent);
2957 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
2959 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
2962 if (!QUEUE_IS_USING_QUEUE (queue)) {
2963 /* add ranges for download and ringbuffer buffering */
2965 gint64 start, stop, range_start, range_stop;
2966 guint64 writing_pos;
2967 gint64 estimated_total;
2969 gboolean peer_res, is_eos;
2970 GstQueue2Range *queued_ranges;
2972 /* we need a current download region */
2973 if (queue->current == NULL)
2976 writing_pos = queue->current->writing_pos;
2977 is_eos = queue->is_eos;
2980 /* we're EOS, we know the duration in bytes now */
2982 duration = writing_pos;
2984 /* get duration of upstream in bytes */
2985 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2986 GST_FORMAT_BYTES, &duration);
2989 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
2990 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
2992 /* calculate remaining and total download time */
2993 if (peer_res && avg_in > 0.0)
2994 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
2996 estimated_total = -1;
2998 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3001 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3004 case GST_FORMAT_PERCENT:
3005 /* we need duration */
3010 /* get our available data relative to the duration */
3013 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3018 case GST_FORMAT_BYTES:
3028 /* fill out the buffered ranges */
3029 for (queued_ranges = queue->ranges; queued_ranges;
3030 queued_ranges = queued_ranges->next) {
3032 case GST_FORMAT_PERCENT:
3033 if (duration == -1) {
3039 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3040 queued_ranges->offset, duration);
3042 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3043 queued_ranges->writing_pos, duration);
3045 case GST_FORMAT_BYTES:
3046 range_start = queued_ranges->offset;
3047 range_stop = queued_ranges->writing_pos;
3054 if (range_start == range_stop)
3056 GST_DEBUG_OBJECT (queue,
3057 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3058 G_GINT64_FORMAT, range_start, range_stop);
3059 gst_query_add_buffering_range (query, range_start, range_stop);
3062 gst_query_set_buffering_range (query, format, start, stop,
3067 case GST_QUERY_SCHEDULING:
3070 GstSchedulingFlags flags = 0;
3072 if (!gst_pad_peer_query (queue->sinkpad, query))
3075 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3077 /* we can operate in pull mode when we are using a tempfile */
3078 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3081 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3082 gst_query_set_scheduling (query, flags, 0, -1, 0);
3084 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3085 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3089 /* peer handled other queries */
3090 if (!gst_pad_query_default (pad, parent, query))
3100 GST_DEBUG_OBJECT (queue, "failed peer query");
3106 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3108 GstQueue2 *queue = GST_QUEUE2 (element);
3110 /* simply forward to the srcpad query function */
3111 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3116 gst_queue2_update_upstream_size (GstQueue2 * queue)
3118 gint64 upstream_size = -1;
3120 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3122 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3123 queue->upstream_size = upstream_size;
3127 static GstFlowReturn
3128 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3129 guint length, GstBuffer ** buffer)
3134 queue = GST_QUEUE2_CAST (parent);
3136 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3137 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3138 offset = (offset == -1) ? queue->current->reading_pos : offset;
3140 GST_DEBUG_OBJECT (queue,
3141 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3143 /* catch any reads beyond the size of the file here to make sure queue2
3144 * doesn't send seek events beyond the size of the file upstream, since
3145 * that would confuse elements such as souphttpsrc and/or http servers.
3146 * Demuxers often just loop until EOS at the end of the file to figure out
3147 * when they've read all the end-headers or index chunks. */
3148 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3149 gst_queue2_update_upstream_size (queue);
3150 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3151 goto out_unexpected;
3154 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3155 gst_queue2_update_upstream_size (queue);
3156 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3157 length = queue->upstream_size - offset;
3158 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3162 /* FIXME - function will block when the range is not yet available */
3163 ret = gst_queue2_create_read (queue, offset, length, buffer);
3164 GST_QUEUE2_MUTEX_UNLOCK (queue);
3165 gst_queue2_post_buffering (queue);
3172 ret = queue->srcresult;
3174 GST_DEBUG_OBJECT (queue, "we are flushing");
3175 GST_QUEUE2_MUTEX_UNLOCK (queue);
3180 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3181 GST_QUEUE2_MUTEX_UNLOCK (queue);
3182 return GST_FLOW_EOS;
3186 /* sink currently only operates in push mode */
3188 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3189 GstPadMode mode, gboolean active)
3194 queue = GST_QUEUE2 (parent);
3197 case GST_PAD_MODE_PUSH:
3199 GST_QUEUE2_MUTEX_LOCK (queue);
3200 GST_DEBUG_OBJECT (queue, "activating push mode");
3201 queue->srcresult = GST_FLOW_OK;
3202 queue->sinkresult = GST_FLOW_OK;
3203 queue->is_eos = FALSE;
3204 queue->unexpected = FALSE;
3205 reset_rate_timer (queue);
3206 GST_QUEUE2_MUTEX_UNLOCK (queue);
3208 /* unblock chain function */
3209 GST_QUEUE2_MUTEX_LOCK (queue);
3210 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3211 queue->srcresult = GST_FLOW_FLUSHING;
3212 queue->sinkresult = GST_FLOW_FLUSHING;
3213 GST_QUEUE2_SIGNAL_DEL (queue);
3214 /* Unblock query handler */
3215 queue->last_query = FALSE;
3216 g_cond_signal (&queue->query_handled);
3217 GST_QUEUE2_MUTEX_UNLOCK (queue);
3219 /* wait until it is unblocked and clean up */
3220 GST_PAD_STREAM_LOCK (pad);
3221 GST_QUEUE2_MUTEX_LOCK (queue);
3222 gst_queue2_locked_flush (queue, TRUE, FALSE);
3223 GST_QUEUE2_MUTEX_UNLOCK (queue);
3224 GST_PAD_STREAM_UNLOCK (pad);
3235 /* src operating in push mode, we start a task on the source pad that pushes out
3236 * buffers from the queue */
3238 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3240 gboolean result = FALSE;
3243 queue = GST_QUEUE2 (parent);
3246 GST_QUEUE2_MUTEX_LOCK (queue);
3247 GST_DEBUG_OBJECT (queue, "activating push mode");
3248 queue->srcresult = GST_FLOW_OK;
3249 queue->sinkresult = GST_FLOW_OK;
3250 queue->is_eos = FALSE;
3251 queue->unexpected = FALSE;
3253 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3254 GST_QUEUE2_MUTEX_UNLOCK (queue);
3256 /* unblock loop function */
3257 GST_QUEUE2_MUTEX_LOCK (queue);
3258 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3259 queue->srcresult = GST_FLOW_FLUSHING;
3260 queue->sinkresult = GST_FLOW_FLUSHING;
3261 /* the item add signal will unblock */
3262 GST_QUEUE2_SIGNAL_ADD (queue);
3263 GST_QUEUE2_MUTEX_UNLOCK (queue);
3265 /* step 2, make sure streaming finishes */
3266 result = gst_pad_stop_task (pad);
3272 /* pull mode, downstream will call our getrange function */
3274 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3279 queue = GST_QUEUE2 (parent);
3282 GST_QUEUE2_MUTEX_LOCK (queue);
3283 if (!QUEUE_IS_USING_QUEUE (queue)) {
3284 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3285 /* open the temp file now */
3286 result = gst_queue2_open_temp_location_file (queue);
3287 } else if (!queue->ring_buffer) {
3288 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3289 result = ! !queue->ring_buffer;
3294 GST_DEBUG_OBJECT (queue, "activating pull mode");
3295 init_ranges (queue);
3296 queue->srcresult = GST_FLOW_OK;
3297 queue->sinkresult = GST_FLOW_OK;
3298 queue->is_eos = FALSE;
3299 queue->unexpected = FALSE;
3300 queue->upstream_size = 0;
3302 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3303 /* this is not allowed, we cannot operate in pull mode without a temp
3305 queue->srcresult = GST_FLOW_FLUSHING;
3306 queue->sinkresult = GST_FLOW_FLUSHING;
3309 GST_QUEUE2_MUTEX_UNLOCK (queue);
3311 GST_QUEUE2_MUTEX_LOCK (queue);
3312 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3313 queue->srcresult = GST_FLOW_FLUSHING;
3314 queue->sinkresult = GST_FLOW_FLUSHING;
3315 /* this will unlock getrange */
3316 GST_QUEUE2_SIGNAL_ADD (queue);
3318 GST_QUEUE2_MUTEX_UNLOCK (queue);
3325 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3331 case GST_PAD_MODE_PULL:
3332 res = gst_queue2_src_activate_pull (pad, parent, active);
3334 case GST_PAD_MODE_PUSH:
3335 res = gst_queue2_src_activate_push (pad, parent, active);
3338 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3345 static GstStateChangeReturn
3346 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3349 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3351 queue = GST_QUEUE2 (element);
3353 switch (transition) {
3354 case GST_STATE_CHANGE_NULL_TO_READY:
3356 case GST_STATE_CHANGE_READY_TO_PAUSED:
3357 GST_QUEUE2_MUTEX_LOCK (queue);
3358 if (!QUEUE_IS_USING_QUEUE (queue)) {
3359 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3360 if (!gst_queue2_open_temp_location_file (queue))
3361 ret = GST_STATE_CHANGE_FAILURE;
3363 if (queue->ring_buffer) {
3364 g_free (queue->ring_buffer);
3365 queue->ring_buffer = NULL;
3367 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3368 ret = GST_STATE_CHANGE_FAILURE;
3370 init_ranges (queue);
3372 queue->segment_event_received = FALSE;
3373 queue->starting_segment = NULL;
3374 gst_event_replace (&queue->stream_start_event, NULL);
3375 GST_QUEUE2_MUTEX_UNLOCK (queue);
3377 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3383 if (ret == GST_STATE_CHANGE_FAILURE)
3386 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3388 if (ret == GST_STATE_CHANGE_FAILURE)
3391 switch (transition) {
3392 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3394 case GST_STATE_CHANGE_PAUSED_TO_READY:
3395 GST_QUEUE2_MUTEX_LOCK (queue);
3396 if (!QUEUE_IS_USING_QUEUE (queue)) {
3397 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3398 gst_queue2_close_temp_location_file (queue);
3399 } else if (queue->ring_buffer) {
3400 g_free (queue->ring_buffer);
3401 queue->ring_buffer = NULL;
3403 clean_ranges (queue);
3405 if (queue->starting_segment != NULL) {
3406 gst_event_unref (queue->starting_segment);
3407 queue->starting_segment = NULL;
3409 gst_event_replace (&queue->stream_start_event, NULL);
3410 GST_QUEUE2_MUTEX_UNLOCK (queue);
3412 case GST_STATE_CHANGE_READY_TO_NULL:
3421 /* changing the capacity of the queue must wake up
3422 * the _chain function, it might have more room now
3423 * to store the buffer/event in the queue */
3424 #define QUEUE_CAPACITY_CHANGE(q) \
3425 GST_QUEUE2_SIGNAL_DEL (queue); \
3426 if (queue->use_buffering) \
3427 update_buffering (queue);
3429 /* Changing the minimum required fill level must
3430 * wake up the _loop function as it might now
3431 * be able to preceed.
3433 #define QUEUE_THRESHOLD_CHANGE(q)\
3434 GST_QUEUE2_SIGNAL_ADD (queue);
3437 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3441 /* the element must be stopped in order to do this */
3442 GST_OBJECT_LOCK (queue);
3443 state = GST_STATE (queue);
3444 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3446 GST_OBJECT_UNLOCK (queue);
3448 /* set new location */
3449 g_free (queue->temp_template);
3450 queue->temp_template = g_strdup (template);
3457 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3458 GST_OBJECT_UNLOCK (queue);
3463 gst_queue2_set_property (GObject * object,
3464 guint prop_id, const GValue * value, GParamSpec * pspec)
3466 GstQueue2 *queue = GST_QUEUE2 (object);
3468 /* someone could change levels here, and since this
3469 * affects the get/put funcs, we need to lock for safety. */
3470 GST_QUEUE2_MUTEX_LOCK (queue);
3473 case PROP_MAX_SIZE_BYTES:
3474 queue->max_level.bytes = g_value_get_uint (value);
3475 QUEUE_CAPACITY_CHANGE (queue);
3477 case PROP_MAX_SIZE_BUFFERS:
3478 queue->max_level.buffers = g_value_get_uint (value);
3479 QUEUE_CAPACITY_CHANGE (queue);
3481 case PROP_MAX_SIZE_TIME:
3482 queue->max_level.time = g_value_get_uint64 (value);
3483 /* set rate_time to the same value. We use an extra field in the level
3484 * structure so that we can easily access and compare it */
3485 queue->max_level.rate_time = queue->max_level.time;
3486 QUEUE_CAPACITY_CHANGE (queue);
3488 case PROP_USE_BUFFERING:
3489 queue->use_buffering = g_value_get_boolean (value);
3490 if (!queue->use_buffering && queue->is_buffering) {
3491 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3492 "posting 100%% message");
3493 SET_PERCENT (queue, 100);
3494 queue->is_buffering = FALSE;
3497 if (queue->use_buffering) {
3498 queue->is_buffering = TRUE;
3499 update_buffering (queue);
3502 case PROP_USE_RATE_ESTIMATE:
3503 queue->use_rate_estimate = g_value_get_boolean (value);
3505 case PROP_LOW_PERCENT:
3506 queue->low_percent = g_value_get_int (value);
3508 case PROP_HIGH_PERCENT:
3509 queue->high_percent = g_value_get_int (value);
3511 case PROP_TEMP_TEMPLATE:
3512 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3514 case PROP_TEMP_REMOVE:
3515 queue->temp_remove = g_value_get_boolean (value);
3517 case PROP_RING_BUFFER_MAX_SIZE:
3518 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3521 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3525 GST_QUEUE2_MUTEX_UNLOCK (queue);
3526 gst_queue2_post_buffering (queue);
3530 gst_queue2_get_property (GObject * object,
3531 guint prop_id, GValue * value, GParamSpec * pspec)
3533 GstQueue2 *queue = GST_QUEUE2 (object);
3535 GST_QUEUE2_MUTEX_LOCK (queue);
3538 case PROP_CUR_LEVEL_BYTES:
3539 g_value_set_uint (value, queue->cur_level.bytes);
3541 case PROP_CUR_LEVEL_BUFFERS:
3542 g_value_set_uint (value, queue->cur_level.buffers);
3544 case PROP_CUR_LEVEL_TIME:
3545 g_value_set_uint64 (value, queue->cur_level.time);
3547 case PROP_MAX_SIZE_BYTES:
3548 g_value_set_uint (value, queue->max_level.bytes);
3550 case PROP_MAX_SIZE_BUFFERS:
3551 g_value_set_uint (value, queue->max_level.buffers);
3553 case PROP_MAX_SIZE_TIME:
3554 g_value_set_uint64 (value, queue->max_level.time);
3556 case PROP_USE_BUFFERING:
3557 g_value_set_boolean (value, queue->use_buffering);
3559 case PROP_USE_RATE_ESTIMATE:
3560 g_value_set_boolean (value, queue->use_rate_estimate);
3562 case PROP_LOW_PERCENT:
3563 g_value_set_int (value, queue->low_percent);
3565 case PROP_HIGH_PERCENT:
3566 g_value_set_int (value, queue->high_percent);
3568 case PROP_TEMP_TEMPLATE:
3569 g_value_set_string (value, queue->temp_template);
3571 case PROP_TEMP_LOCATION:
3572 g_value_set_string (value, queue->temp_location);
3574 case PROP_TEMP_REMOVE:
3575 g_value_set_boolean (value, queue->temp_remove);
3577 case PROP_RING_BUFFER_MAX_SIZE:
3578 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3581 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3585 GST_QUEUE2_MUTEX_UNLOCK (queue);