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., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, 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-tmpl 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 * Since 0.10.24, setting the temp-location property with a filename is deprecated
50 * because it's impossible to securely open a temporary file in this way. The
51 * property will still be used to notify the application of the allocated
54 * Last reviewed on 2009-07-10 (0.10.24)
61 #include "gstqueue2.h"
63 #include <glib/gstdio.h>
65 #include "gst/gst-i18n-lib.h"
66 #include "gst/glib-compat-private.h"
71 #include <io.h> /* lseek, open, close, read */
73 #define lseek _lseeki64
80 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
85 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
90 GST_DEBUG_CATEGORY_STATIC (queue_debug);
91 #define GST_CAT_DEFAULT (queue_debug)
92 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
100 #define DEFAULT_BUFFER_SIZE 4096
101 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_location_set || (queue)->temp_template != NULL)
102 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
103 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
105 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
107 /* default property values */
108 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
109 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
110 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
111 #define DEFAULT_USE_BUFFERING FALSE
112 #define DEFAULT_USE_RATE_ESTIMATE TRUE
113 #define DEFAULT_LOW_PERCENT 10
114 #define DEFAULT_HIGH_PERCENT 99
115 #define DEFAULT_TEMP_REMOVE TRUE
116 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
121 PROP_CUR_LEVEL_BUFFERS,
122 PROP_CUR_LEVEL_BYTES,
124 PROP_MAX_SIZE_BUFFERS,
128 PROP_USE_RATE_ESTIMATE,
134 PROP_RING_BUFFER_MAX_SIZE,
138 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
145 #define STATUS(queue, pad, msg) \
146 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
147 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
148 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
149 " ns, %"G_GUINT64_FORMAT" items", \
150 GST_DEBUG_PAD_NAME (pad), \
151 queue->cur_level.buffers, \
152 queue->max_level.buffers, \
153 queue->cur_level.bytes, \
154 queue->max_level.bytes, \
155 queue->cur_level.time, \
156 queue->max_level.time, \
157 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
158 queue->current->writing_pos - queue->current->max_reading_pos : \
159 queue->queue.length))
161 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
162 g_mutex_lock (&q->qlock); \
165 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
166 GST_QUEUE2_MUTEX_LOCK (q); \
167 if (res != GST_FLOW_OK) \
171 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
172 g_mutex_unlock (&q->qlock); \
175 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
176 STATUS (queue, q->sinkpad, "wait for DEL"); \
177 q->waiting_del = TRUE; \
178 g_cond_wait (&q->item_del, &queue->qlock); \
179 q->waiting_del = FALSE; \
180 if (res != GST_FLOW_OK) { \
181 STATUS (queue, q->srcpad, "received DEL wakeup"); \
184 STATUS (queue, q->sinkpad, "received DEL"); \
187 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
188 STATUS (queue, q->srcpad, "wait for ADD"); \
189 q->waiting_add = TRUE; \
190 g_cond_wait (&q->item_add, &q->qlock); \
191 q->waiting_add = FALSE; \
192 if (res != GST_FLOW_OK) { \
193 STATUS (queue, q->srcpad, "received ADD wakeup"); \
196 STATUS (queue, q->srcpad, "received ADD"); \
199 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
200 if (q->waiting_del) { \
201 STATUS (q, q->srcpad, "signal DEL"); \
202 g_cond_signal (&q->item_del); \
206 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
207 if (q->waiting_add) { \
208 STATUS (q, q->sinkpad, "signal ADD"); \
209 g_cond_signal (&q->item_add); \
214 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
215 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
216 "dataflow inside the queue element");
217 #define gst_queue2_parent_class parent_class
218 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
220 static void gst_queue2_finalize (GObject * object);
222 static void gst_queue2_set_property (GObject * object,
223 guint prop_id, const GValue * value, GParamSpec * pspec);
224 static void gst_queue2_get_property (GObject * object,
225 guint prop_id, GValue * value, GParamSpec * pspec);
227 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
229 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
230 GstBufferList * buffer_list);
231 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
232 static void gst_queue2_loop (GstPad * pad);
234 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
236 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
239 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
241 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
243 static gboolean gst_queue2_handle_query (GstElement * element,
246 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
247 guint64 offset, guint length, GstBuffer ** buffer);
249 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
250 GstPadMode mode, gboolean active);
251 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
252 GstPadMode mode, gboolean active);
253 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
254 GstStateChange transition);
256 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
257 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
259 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
263 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
264 GST_QUEUE2_ITEM_TYPE_BUFFER,
265 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
266 GST_QUEUE2_ITEM_TYPE_EVENT
269 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
272 gst_queue2_class_init (GstQueue2Class * klass)
274 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
275 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
277 gobject_class->set_property = gst_queue2_set_property;
278 gobject_class->get_property = gst_queue2_get_property;
281 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
282 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
283 "Current amount of data in the queue (bytes)",
284 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
285 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
286 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
287 "Current number of buffers in the queue",
288 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
290 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
291 "Current amount of data in the queue (in ns)",
292 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
295 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
296 "Max. amount of data in the queue (bytes, 0=disable)",
297 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
298 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
300 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
301 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
302 DEFAULT_MAX_SIZE_BUFFERS,
303 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
305 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
306 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
307 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
310 g_param_spec_boolean ("use-buffering", "Use buffering",
311 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
312 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
314 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
315 "Estimate the bitrate of the stream to calculate time level",
316 DEFAULT_USE_RATE_ESTIMATE,
317 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
319 g_param_spec_int ("low-percent", "Low percent",
320 "Low threshold for buffering to start. Only used if use-buffering is True",
321 0, 100, DEFAULT_LOW_PERCENT,
322 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
324 g_param_spec_int ("high-percent", "High percent",
325 "High threshold for buffering to finish. Only used if use-buffering is True",
326 0, 100, DEFAULT_HIGH_PERCENT,
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
330 g_param_spec_string ("temp-template", "Temporary File Template",
331 "File template to store temporary files in, should contain directory "
332 "and XXXXXX. (NULL == disabled)",
333 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
336 g_param_spec_string ("temp-location", "Temporary File Location",
337 "Location to store temporary files in (Deprecated: Only read this "
338 "property, use temp-template to configure the name template)",
339 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 * GstQueue2:temp-remove
344 * When temp-template is set, remove the temporary file when going to READY.
348 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
349 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
350 "Remove the temp-location after use",
351 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 * GstQueue2:ring-buffer-max-size
356 * The maximum size of the ring buffer in bytes. If set to 0, the ring
357 * buffer is disabled. Default 0.
361 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
362 g_param_spec_uint64 ("ring-buffer-max-size",
363 "Max. ring buffer size (bytes)",
364 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
365 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
366 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 /* set several parent class virtual functions */
369 gobject_class->finalize = gst_queue2_finalize;
371 gst_element_class_add_pad_template (gstelement_class,
372 gst_static_pad_template_get (&srctemplate));
373 gst_element_class_add_pad_template (gstelement_class,
374 gst_static_pad_template_get (&sinktemplate));
376 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
379 "Erik Walthinsen <omega@cse.ogi.edu>, "
380 "Wim Taymans <wim.taymans@gmail.com>");
382 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
383 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
387 gst_queue2_init (GstQueue2 * queue)
389 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
391 gst_pad_set_chain_function (queue->sinkpad,
392 GST_DEBUG_FUNCPTR (gst_queue2_chain));
393 gst_pad_set_chain_list_function (queue->sinkpad,
394 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
395 gst_pad_set_activatemode_function (queue->sinkpad,
396 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
397 gst_pad_set_event_function (queue->sinkpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
399 gst_pad_set_query_function (queue->sinkpad,
400 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
401 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
402 GST_PAD_SET_PROXY_ALLOCATION (queue->sinkpad);
403 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
405 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
407 gst_pad_set_activatemode_function (queue->srcpad,
408 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
409 gst_pad_set_getrange_function (queue->srcpad,
410 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
411 gst_pad_set_event_function (queue->srcpad,
412 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
413 gst_pad_set_query_function (queue->srcpad,
414 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
415 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
416 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
419 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
420 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
421 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
422 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
423 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
424 queue->use_buffering = DEFAULT_USE_BUFFERING;
425 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
426 queue->low_percent = DEFAULT_LOW_PERCENT;
427 queue->high_percent = DEFAULT_HIGH_PERCENT;
429 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
430 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
432 queue->sinktime = GST_CLOCK_TIME_NONE;
433 queue->srctime = GST_CLOCK_TIME_NONE;
434 queue->sink_tainted = TRUE;
435 queue->src_tainted = TRUE;
437 queue->srcresult = GST_FLOW_FLUSHING;
438 queue->sinkresult = GST_FLOW_FLUSHING;
439 queue->is_eos = FALSE;
440 queue->in_timer = g_timer_new ();
441 queue->out_timer = g_timer_new ();
443 g_mutex_init (&queue->qlock);
444 queue->waiting_add = FALSE;
445 g_cond_init (&queue->item_add);
446 queue->waiting_del = FALSE;
447 g_cond_init (&queue->item_del);
448 g_queue_init (&queue->queue);
450 queue->buffering_percent = 100;
452 /* tempfile related */
453 queue->temp_template = NULL;
454 queue->temp_location = NULL;
455 queue->temp_location_set = FALSE;
456 queue->temp_remove = DEFAULT_TEMP_REMOVE;
458 queue->ring_buffer = NULL;
459 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
461 GST_DEBUG_OBJECT (queue,
462 "initialized queue's not_empty & not_full conditions");
465 /* called only once, as opposed to dispose */
467 gst_queue2_finalize (GObject * object)
469 GstQueue2 *queue = GST_QUEUE2 (object);
471 GST_DEBUG_OBJECT (queue, "finalizing queue");
473 while (!g_queue_is_empty (&queue->queue)) {
474 GstMiniObject *data = g_queue_pop_head (&queue->queue);
476 gst_mini_object_unref (data);
479 g_queue_clear (&queue->queue);
480 g_mutex_clear (&queue->qlock);
481 g_cond_clear (&queue->item_add);
482 g_cond_clear (&queue->item_del);
483 g_timer_destroy (queue->in_timer);
484 g_timer_destroy (queue->out_timer);
486 /* temp_file path cleanup */
487 g_free (queue->temp_template);
488 g_free (queue->temp_location);
490 G_OBJECT_CLASS (parent_class)->finalize (object);
494 debug_ranges (GstQueue2 * queue)
496 GstQueue2Range *walk;
498 for (walk = queue->ranges; walk; walk = walk->next) {
499 GST_DEBUG_OBJECT (queue,
500 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
501 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
502 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
503 walk->rb_writing_pos, walk->reading_pos,
504 walk == queue->current ? "**y**" : " n ");
508 /* clear all the downloaded ranges */
510 clean_ranges (GstQueue2 * queue)
512 GST_DEBUG_OBJECT (queue, "clean queue ranges");
514 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
515 queue->ranges = NULL;
516 queue->current = NULL;
519 /* find a range that contains @offset or NULL when nothing does */
520 static GstQueue2Range *
521 find_range (GstQueue2 * queue, guint64 offset)
523 GstQueue2Range *range = NULL;
524 GstQueue2Range *walk;
526 /* first do a quick check for the current range */
527 for (walk = queue->ranges; walk; walk = walk->next) {
528 if (offset >= walk->offset && offset <= walk->writing_pos) {
529 /* we can reuse an existing range */
535 GST_DEBUG_OBJECT (queue,
536 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
537 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
539 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
545 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
547 guint64 max_reading_pos, writing_pos;
549 writing_pos = range->writing_pos;
550 max_reading_pos = range->max_reading_pos;
552 if (writing_pos > max_reading_pos)
553 queue->cur_level.bytes = writing_pos - max_reading_pos;
555 queue->cur_level.bytes = 0;
558 /* make a new range for @offset or reuse an existing range */
559 static GstQueue2Range *
560 add_range (GstQueue2 * queue, guint64 offset)
562 GstQueue2Range *range, *prev, *next;
564 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
566 if ((range = find_range (queue, offset))) {
567 GST_DEBUG_OBJECT (queue,
568 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
570 range->writing_pos = offset;
572 GST_DEBUG_OBJECT (queue,
573 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
575 range = g_slice_new0 (GstQueue2Range);
576 range->offset = offset;
577 /* we want to write to the next location in the ring buffer */
578 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
579 range->writing_pos = offset;
580 range->rb_writing_pos = range->rb_offset;
581 range->reading_pos = offset;
582 range->max_reading_pos = offset;
586 next = queue->ranges;
588 if (next->offset > offset) {
589 /* insert before next */
590 GST_DEBUG_OBJECT (queue,
591 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
603 queue->ranges = range;
605 debug_ranges (queue);
607 /* update the stats for this range */
608 update_cur_level (queue, range);
614 /* clear and init the download ranges for offset 0 */
616 init_ranges (GstQueue2 * queue)
618 GST_DEBUG_OBJECT (queue, "init queue ranges");
620 /* get rid of all the current ranges */
621 clean_ranges (queue);
622 /* make a range for offset 0 */
623 queue->current = add_range (queue, 0);
626 /* calculate the diff between running time on the sink and src of the queue.
627 * This is the total amount of time in the queue. */
629 update_time_level (GstQueue2 * queue)
631 if (queue->sink_tainted) {
633 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
634 queue->sink_segment.position);
635 queue->sink_tainted = FALSE;
638 if (queue->src_tainted) {
640 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
641 queue->src_segment.position);
642 queue->src_tainted = FALSE;
645 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
646 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
648 if (queue->sinktime != GST_CLOCK_TIME_NONE
649 && queue->srctime != GST_CLOCK_TIME_NONE
650 && queue->sinktime >= queue->srctime)
651 queue->cur_level.time = queue->sinktime - queue->srctime;
653 queue->cur_level.time = 0;
656 /* take a SEGMENT event and apply the values to segment, updating the time
659 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
662 gst_event_copy_segment (event, segment);
664 if (segment->format == GST_FORMAT_BYTES) {
665 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
666 /* start is where we'll be getting from and as such writing next */
667 queue->current = add_range (queue, segment->start);
668 /* update the stats for this range */
669 update_cur_level (queue, queue->current);
673 /* now configure the values, we use these to track timestamps on the
675 if (segment->format != GST_FORMAT_TIME) {
676 /* non-time format, pretent the current time segment is closed with a
677 * 0 start and unknown stop time. */
678 segment->format = GST_FORMAT_TIME;
684 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
687 queue->sink_tainted = TRUE;
689 queue->src_tainted = TRUE;
691 /* segment can update the time level of the queue */
692 update_time_level (queue);
695 /* take a buffer and update segment, updating the time level of the queue. */
697 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
700 GstClockTime duration, timestamp;
702 timestamp = GST_BUFFER_TIMESTAMP (buffer);
703 duration = GST_BUFFER_DURATION (buffer);
705 /* if no timestamp is set, assume it's continuous with the previous
707 if (timestamp == GST_CLOCK_TIME_NONE)
708 timestamp = segment->position;
711 if (duration != GST_CLOCK_TIME_NONE)
712 timestamp += duration;
714 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
715 GST_TIME_ARGS (timestamp));
717 segment->position = timestamp;
720 queue->sink_tainted = TRUE;
722 queue->src_tainted = TRUE;
724 /* calc diff with other end */
725 update_time_level (queue);
729 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
731 GstClockTime *timestamp = data;
733 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
734 " duration %" GST_TIME_FORMAT, idx,
735 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
736 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
738 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
739 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
741 if (GST_BUFFER_DURATION_IS_VALID (*buf))
742 *timestamp += GST_BUFFER_DURATION (*buf);
744 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
748 /* take a buffer list and update segment, updating the time level of the queue */
750 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
751 GstSegment * segment, gboolean is_sink)
753 GstClockTime timestamp;
755 /* if no timestamp is set, assume it's continuous with the previous time */
756 timestamp = segment->position;
758 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
760 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
761 GST_TIME_ARGS (timestamp));
763 segment->position = timestamp;
766 queue->sink_tainted = TRUE;
768 queue->src_tainted = TRUE;
770 /* calc diff with other end */
771 update_time_level (queue);
775 update_buffering (GstQueue2 * queue)
778 gboolean post = FALSE;
780 if (queue->high_percent <= 0)
783 #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)
786 /* on EOS we are always 100% full, we set the var here so that it we can
787 * reuse the logic below to stop buffering */
789 GST_LOG_OBJECT (queue, "we are EOS");
791 /* figure out the percent we are filled, we take the max of all formats. */
793 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
794 percent = GET_PERCENT (bytes, 0);
796 guint64 rb_size = queue->ring_buffer_max_size;
797 percent = GET_PERCENT (bytes, rb_size);
799 percent = MAX (percent, GET_PERCENT (time, 0));
800 percent = MAX (percent, GET_PERCENT (buffers, 0));
802 /* also apply the rate estimate when we need to */
803 if (queue->use_rate_estimate)
804 percent = MAX (percent, GET_PERCENT (rate_time, 0));
807 if (queue->is_buffering) {
809 /* if we were buffering see if we reached the high watermark */
810 if (percent >= queue->high_percent)
811 queue->is_buffering = FALSE;
813 /* we were not buffering, check if we need to start buffering if we drop
814 * below the low threshold */
815 if (percent < queue->low_percent) {
816 queue->is_buffering = TRUE;
817 queue->buffering_iteration++;
823 GstBufferingMode mode;
824 gint64 buffering_left = -1;
826 /* scale to high percent so that it becomes the 100% mark */
827 percent = percent * 100 / queue->high_percent;
832 if (percent != queue->buffering_percent) {
833 queue->buffering_percent = percent;
835 if (!QUEUE_IS_USING_QUEUE (queue)) {
838 if (QUEUE_IS_USING_RING_BUFFER (queue))
839 mode = GST_BUFFERING_TIMESHIFT;
841 mode = GST_BUFFERING_DOWNLOAD;
843 if (queue->byte_in_rate > 0) {
844 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
847 (gdouble) ((duration -
848 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
851 buffering_left = G_MAXINT64;
854 mode = GST_BUFFERING_STREAM;
857 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
858 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
860 gst_message_set_buffering_stats (message, mode,
861 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
863 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
866 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
873 reset_rate_timer (GstQueue2 * queue)
876 queue->bytes_out = 0;
877 queue->byte_in_rate = 0.0;
878 queue->byte_in_period = 0;
879 queue->byte_out_rate = 0.0;
880 queue->last_in_elapsed = 0.0;
881 queue->last_out_elapsed = 0.0;
882 queue->in_timer_started = FALSE;
883 queue->out_timer_started = FALSE;
886 /* the interval in seconds to recalculate the rate */
887 #define RATE_INTERVAL 0.2
888 /* Tuning for rate estimation. We use a large window for the input rate because
889 * it should be stable when connected to a network. The output rate is less
890 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
891 * therefore adapt more quickly.
892 * However, initial input rate may be subject to a burst, and should therefore
893 * initially also adapt more quickly to changes, and only later on give higher
894 * weight to previous values. */
895 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
896 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
899 update_in_rates (GstQueue2 * queue)
901 gdouble elapsed, period;
902 gdouble byte_in_rate;
904 if (!queue->in_timer_started) {
905 queue->in_timer_started = TRUE;
906 g_timer_start (queue->in_timer);
910 elapsed = g_timer_elapsed (queue->in_timer, NULL);
912 /* recalc after each interval. */
913 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
914 period = elapsed - queue->last_in_elapsed;
916 GST_DEBUG_OBJECT (queue,
917 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
918 period, queue->bytes_in, queue->byte_in_period);
920 byte_in_rate = queue->bytes_in / period;
922 if (queue->byte_in_rate == 0.0)
923 queue->byte_in_rate = byte_in_rate;
925 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
926 (double) queue->byte_in_period, period);
928 /* another data point, cap at 16 for long time running average */
929 if (queue->byte_in_period < 16 * RATE_INTERVAL)
930 queue->byte_in_period += period;
932 /* reset the values to calculate rate over the next interval */
933 queue->last_in_elapsed = elapsed;
937 if (queue->byte_in_rate > 0.0) {
938 queue->cur_level.rate_time =
939 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
941 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
942 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
946 update_out_rates (GstQueue2 * queue)
948 gdouble elapsed, period;
949 gdouble byte_out_rate;
951 if (!queue->out_timer_started) {
952 queue->out_timer_started = TRUE;
953 g_timer_start (queue->out_timer);
957 elapsed = g_timer_elapsed (queue->out_timer, NULL);
959 /* recalc after each interval. */
960 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
961 period = elapsed - queue->last_out_elapsed;
963 GST_DEBUG_OBJECT (queue,
964 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
966 byte_out_rate = queue->bytes_out / period;
968 if (queue->byte_out_rate == 0.0)
969 queue->byte_out_rate = byte_out_rate;
971 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
973 /* reset the values to calculate rate over the next interval */
974 queue->last_out_elapsed = elapsed;
975 queue->bytes_out = 0;
977 if (queue->byte_in_rate > 0.0) {
978 queue->cur_level.rate_time =
979 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
981 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
982 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
986 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
988 guint64 reading_pos, max_reading_pos;
991 max_reading_pos = range->max_reading_pos;
993 max_reading_pos = MAX (max_reading_pos, reading_pos);
995 GST_DEBUG_OBJECT (queue,
996 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
997 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
998 range->max_reading_pos = max_reading_pos;
1000 update_cur_level (queue, range);
1004 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1009 GST_QUEUE2_MUTEX_UNLOCK (queue);
1011 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1014 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1015 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1016 GST_SEEK_TYPE_NONE, -1);
1018 res = gst_pad_push_event (queue->sinkpad, event);
1019 GST_QUEUE2_MUTEX_LOCK (queue);
1022 queue->current = add_range (queue, offset);
1027 /* see if there is enough data in the file to read a full buffer */
1029 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1031 GstQueue2Range *range;
1033 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1036 if ((range = find_range (queue, offset))) {
1037 if (queue->current != range) {
1038 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1039 perform_seek_to_offset (queue, range->writing_pos);
1042 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1043 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1045 /* we have a range for offset */
1046 GST_DEBUG_OBJECT (queue,
1047 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1048 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1050 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1053 if (offset + length <= range->writing_pos)
1056 GST_DEBUG_OBJECT (queue,
1057 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1058 (offset + length) - range->writing_pos);
1061 GST_INFO_OBJECT (queue, "not found in any range");
1062 /* we don't have the range, see how far away we are, FIXME, find a good
1063 * threshold based on the incoming rate. */
1064 if (!queue->is_eos && queue->current) {
1065 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1066 if (offset < queue->current->offset || offset >
1067 queue->current->writing_pos + QUEUE_MAX_BYTES (queue) -
1068 queue->cur_level.bytes) {
1069 perform_seek_to_offset (queue, offset);
1071 GST_INFO_OBJECT (queue,
1072 "requested data is within range, wait for data");
1074 } else if (offset < queue->current->writing_pos + 200000) {
1075 update_cur_pos (queue, queue->current, offset + length);
1076 GST_INFO_OBJECT (queue, "wait for data");
1081 /* too far away, do a seek */
1082 perform_seek_to_offset (queue, offset);
1089 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1090 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1091 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1093 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1096 static GstFlowReturn
1097 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1098 guint8 * dst, gint64 * read_return)
1100 guint8 *ring_buffer;
1103 ring_buffer = queue->ring_buffer;
1105 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1108 /* this should not block */
1109 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1111 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1112 res = fread (dst, 1, length, queue->temp_file);
1114 memcpy (dst, ring_buffer + offset, length);
1118 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1120 if (G_UNLIKELY (res < length)) {
1121 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1122 goto could_not_read;
1123 /* check for errors or EOF */
1124 if (ferror (queue->temp_file))
1125 goto could_not_read;
1126 if (feof (queue->temp_file) && length > 0)
1136 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1137 return GST_FLOW_ERROR;
1141 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1142 return GST_FLOW_ERROR;
1146 GST_DEBUG ("non-regular file hits EOS");
1147 return GST_FLOW_EOS;
1151 static GstFlowReturn
1152 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1153 GstBuffer ** buffer)
1158 guint64 file_offset;
1159 guint block_length, remaining, read_length;
1162 GstFlowReturn ret = GST_FLOW_OK;
1164 /* allocate the output buffer of the requested size */
1165 buf = gst_buffer_new_allocate (NULL, length, 0);
1166 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1169 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1173 rb_size = queue->ring_buffer_max_size;
1176 while (remaining > 0) {
1177 /* configure how much/whether to read */
1178 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1181 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1184 /* calculate how far away the offset is */
1185 if (queue->current->writing_pos > rpos)
1186 level = queue->current->writing_pos - rpos;
1190 GST_DEBUG_OBJECT (queue,
1191 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1192 ", level %" G_GUINT64_FORMAT,
1193 rpos, queue->current->writing_pos, level);
1195 if (level >= rb_size) {
1196 /* we don't have the data but if we have a ring buffer that is full, we
1198 GST_DEBUG_OBJECT (queue,
1199 "ring buffer full, reading ring-buffer-max-size %"
1200 G_GUINT64_FORMAT " bytes", rb_size);
1201 read_length = rb_size;
1202 } else if (queue->is_eos) {
1203 /* won't get any more data so read any data we have */
1205 GST_DEBUG_OBJECT (queue,
1206 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1208 read_length = level;
1214 if (read_length == 0) {
1215 if (QUEUE_IS_USING_RING_BUFFER (queue)
1216 && queue->current->max_reading_pos > rpos) {
1217 /* protect cached data (data between offset and max_reading_pos)
1218 * and update current level */
1219 GST_DEBUG_OBJECT (queue,
1220 "protecting cached data [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1221 "]", rpos, queue->current->max_reading_pos);
1222 queue->current->max_reading_pos = rpos;
1223 update_cur_level (queue, queue->current);
1225 GST_DEBUG_OBJECT (queue, "waiting for add");
1226 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1230 /* we have the requested data so read it */
1231 read_length = remaining;
1234 /* set range reading_pos to actual reading position for this read */
1235 queue->current->reading_pos = rpos;
1237 /* configure how much and from where to read */
1238 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1240 (queue->current->rb_offset + (rpos -
1241 queue->current->offset)) % rb_size;
1242 if (file_offset + read_length > rb_size) {
1243 block_length = rb_size - file_offset;
1245 block_length = read_length;
1249 block_length = read_length;
1252 /* while we still have data to read, we loop */
1253 while (read_length > 0) {
1257 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1258 data, &read_return);
1259 if (ret != GST_FLOW_OK)
1262 file_offset += read_return;
1263 if (QUEUE_IS_USING_RING_BUFFER (queue))
1264 file_offset %= rb_size;
1266 data += read_return;
1267 read_length -= read_return;
1268 block_length = read_length;
1269 remaining -= read_return;
1271 rpos = (queue->current->reading_pos += read_return);
1272 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1274 GST_QUEUE2_SIGNAL_DEL (queue);
1275 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1278 gst_buffer_unmap (buf, &info);
1279 gst_buffer_resize (buf, 0, length);
1281 GST_BUFFER_OFFSET (buf) = offset;
1282 GST_BUFFER_OFFSET_END (buf) = offset + length;
1291 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1292 gst_buffer_unmap (buf, &info);
1293 gst_buffer_unref (buf);
1294 return GST_FLOW_EOS;
1298 GST_DEBUG_OBJECT (queue, "we are flushing");
1299 gst_buffer_unmap (buf, &info);
1300 gst_buffer_unref (buf);
1301 return GST_FLOW_FLUSHING;
1305 GST_DEBUG_OBJECT (queue, "we have a read error");
1306 gst_buffer_unmap (buf, &info);
1307 gst_buffer_unref (buf);
1312 /* should be called with QUEUE_LOCK */
1313 static GstMiniObject *
1314 gst_queue2_read_item_from_file (GstQueue2 * queue)
1316 GstMiniObject *item;
1318 if (queue->stream_start_event != NULL) {
1319 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1320 queue->stream_start_event = NULL;
1321 } else if (queue->starting_segment != NULL) {
1322 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1323 queue->starting_segment = NULL;
1327 guint64 reading_pos;
1329 reading_pos = queue->current->reading_pos;
1332 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1337 item = GST_MINI_OBJECT_CAST (buffer);
1340 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1350 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1351 * the temp filename. */
1353 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1358 if (queue->temp_file)
1359 goto already_opened;
1361 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1363 /* we have two cases:
1364 * - temp_location was set to something !NULL (Deprecated). in this case we
1365 * open the specified filename.
1366 * - temp_template was set, allocate a filename and open that filename
1368 if (!queue->temp_location_set) {
1370 if (queue->temp_template == NULL)
1373 /* make copy of the template, we don't want to change this */
1374 name = g_strdup (queue->temp_template);
1375 fd = g_mkstemp (name);
1377 goto mkstemp_failed;
1379 /* open the file for update/writing */
1380 queue->temp_file = fdopen (fd, "wb+");
1381 /* error creating file */
1382 if (queue->temp_file == NULL)
1385 g_free (queue->temp_location);
1386 queue->temp_location = name;
1388 GST_QUEUE2_MUTEX_UNLOCK (queue);
1390 /* we can't emit the notify with the lock */
1391 g_object_notify (G_OBJECT (queue), "temp-location");
1393 GST_QUEUE2_MUTEX_LOCK (queue);
1395 /* open the file for update/writing, this is deprecated but we still need to
1396 * support it for API/ABI compatibility */
1397 queue->temp_file = g_fopen (queue->temp_location, "wb+");
1398 /* error creating file */
1399 if (queue->temp_file == NULL)
1402 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1409 GST_DEBUG_OBJECT (queue, "temp file was already open");
1414 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1415 (_("No Temp directory specified.")), (NULL));
1420 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1421 (_("Could not create temp file \"%s\"."), queue->temp_template),
1428 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1429 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1438 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1441 if (queue->temp_file == NULL)
1444 GST_DEBUG_OBJECT (queue, "closing temp file");
1446 fflush (queue->temp_file);
1447 fclose (queue->temp_file);
1449 if (queue->temp_remove)
1450 remove (queue->temp_location);
1452 queue->temp_file = NULL;
1453 clean_ranges (queue);
1457 gst_queue2_flush_temp_file (GstQueue2 * queue)
1459 if (queue->temp_file == NULL)
1462 GST_DEBUG_OBJECT (queue, "flushing temp file");
1464 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1468 gst_queue2_locked_flush (GstQueue2 * queue)
1470 if (!QUEUE_IS_USING_QUEUE (queue)) {
1471 if (QUEUE_IS_USING_TEMP_FILE (queue))
1472 gst_queue2_flush_temp_file (queue);
1473 init_ranges (queue);
1475 while (!g_queue_is_empty (&queue->queue)) {
1476 GstMiniObject *data = g_queue_pop_head (&queue->queue);
1478 /* Then lose another reference because we are supposed to destroy that
1479 data when flushing */
1480 gst_mini_object_unref (data);
1483 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1484 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1485 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1486 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1487 queue->sink_tainted = queue->src_tainted = TRUE;
1488 if (queue->starting_segment != NULL)
1489 gst_event_unref (queue->starting_segment);
1490 queue->starting_segment = NULL;
1491 queue->segment_event_received = FALSE;
1492 gst_event_replace (&queue->stream_start_event, NULL);
1494 /* we deleted a lot of something */
1495 GST_QUEUE2_SIGNAL_DEL (queue);
1499 gst_queue2_wait_free_space (GstQueue2 * queue)
1501 /* We make space available if we're "full" according to whatever
1502 * the user defined as "full". */
1503 if (gst_queue2_is_filled (queue)) {
1506 /* pause the timer while we wait. The fact that we are waiting does not mean
1507 * the byterate on the input pad is lower */
1508 if ((started = queue->in_timer_started))
1509 g_timer_stop (queue->in_timer);
1511 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1512 "queue is full, waiting for free space");
1514 /* Wait for space to be available, we could be unlocked because of a flush. */
1515 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1517 while (gst_queue2_is_filled (queue));
1519 /* and continue if we were running before */
1521 g_timer_continue (queue->in_timer);
1528 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1534 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1537 guint8 *data, *ring_buffer;
1538 guint size, rb_size;
1539 guint64 writing_pos, new_writing_pos;
1540 GstQueue2Range *range, *prev, *next;
1542 if (QUEUE_IS_USING_RING_BUFFER (queue))
1543 writing_pos = queue->current->rb_writing_pos;
1545 writing_pos = queue->current->writing_pos;
1546 ring_buffer = queue->ring_buffer;
1547 rb_size = queue->ring_buffer_max_size;
1549 gst_buffer_map (buffer, &info, GST_MAP_READ);
1554 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1555 GST_BUFFER_OFFSET (buffer));
1560 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1563 /* calculate the space in the ring buffer not used by data from
1564 * the current range */
1565 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1566 /* wait until there is some free space */
1567 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1569 /* get the amount of space we have */
1570 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1572 /* calculate if we need to split or if we can write the entire
1574 to_write = MIN (size, space);
1576 /* the writing position in the ring buffer after writing (part
1577 * or all of) the buffer */
1578 new_writing_pos = (writing_pos + to_write) % rb_size;
1581 range = queue->ranges;
1583 /* if we need to overwrite data in the ring buffer, we need to
1586 * warning: this code is complicated and includes some
1587 * simplifications - pen, paper and diagrams for the cases
1590 guint64 range_data_start, range_data_end;
1591 GstQueue2Range *range_to_destroy = NULL;
1593 range_data_start = range->rb_offset;
1594 range_data_end = range->rb_writing_pos;
1596 /* handle the special case where the range has no data in it */
1597 if (range->writing_pos == range->offset) {
1598 if (range != queue->current) {
1599 GST_DEBUG_OBJECT (queue,
1600 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1601 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1603 range_to_destroy = range;
1605 prev->next = range->next;
1610 if (range_data_end > range_data_start) {
1611 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1614 if (new_writing_pos > range_data_start) {
1615 if (new_writing_pos >= range_data_end) {
1616 GST_DEBUG_OBJECT (queue,
1617 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1618 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1620 range_to_destroy = range;
1622 prev->next = range->next;
1624 GST_DEBUG_OBJECT (queue,
1625 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1626 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1627 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1628 range->offset + new_writing_pos - range_data_start,
1630 range->offset += (new_writing_pos - range_data_start);
1631 range->rb_offset = new_writing_pos;
1635 guint64 new_wpos_virt = writing_pos + to_write;
1637 if (new_wpos_virt <= range_data_start)
1640 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1641 GST_DEBUG_OBJECT (queue,
1642 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1643 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1645 range_to_destroy = range;
1647 prev->next = range->next;
1649 GST_DEBUG_OBJECT (queue,
1650 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1651 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1652 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1653 range->offset + new_writing_pos - range_data_start,
1655 range->offset += (new_wpos_virt - range_data_start);
1656 range->rb_offset = new_writing_pos;
1661 if (!range_to_destroy)
1664 range = range->next;
1665 if (range_to_destroy) {
1666 if (range_to_destroy == queue->ranges)
1667 queue->ranges = range;
1668 g_slice_free (GstQueue2Range, range_to_destroy);
1669 range_to_destroy = NULL;
1674 new_writing_pos = writing_pos + to_write;
1677 if (QUEUE_IS_USING_TEMP_FILE (queue)
1678 && FSEEK_FILE (queue->temp_file, writing_pos))
1681 if (new_writing_pos > writing_pos) {
1682 GST_INFO_OBJECT (queue,
1683 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1684 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1685 queue->current->writing_pos, queue->current->rb_writing_pos);
1686 /* either not using ring buffer or no wrapping, just write */
1687 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1688 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1691 memcpy (ring_buffer + writing_pos, data, to_write);
1694 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1695 /* try to merge with next range */
1696 while ((next = queue->current->next)) {
1697 GST_INFO_OBJECT (queue,
1698 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1699 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1700 if (new_writing_pos < next->offset)
1703 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1706 /* remove the group, we could choose to not read the data in this range
1707 * again. This would involve us doing a seek to the current writing position
1708 * in the range. FIXME, It would probably make sense to do a seek when there
1709 * is a lot of data in the range we merged with to avoid reading it all
1711 queue->current->next = next->next;
1712 g_slice_free (GstQueue2Range, next);
1714 debug_ranges (queue);
1716 goto update_and_signal;
1720 guint block_one, block_two;
1722 block_one = rb_size - writing_pos;
1723 block_two = to_write - block_one;
1725 if (block_one > 0) {
1726 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1727 /* write data to end of ring buffer */
1728 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1729 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1732 memcpy (ring_buffer + writing_pos, data, block_one);
1736 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1739 if (block_two > 0) {
1740 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1741 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1742 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1745 memcpy (ring_buffer, data + block_one, block_two);
1751 /* update the writing positions */
1753 GST_INFO_OBJECT (queue,
1754 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1755 to_write, writing_pos, size);
1757 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1759 queue->current->writing_pos += to_write;
1760 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1762 queue->current->writing_pos = writing_pos = new_writing_pos;
1764 update_cur_level (queue, queue->current);
1766 /* update the buffering status */
1767 if (queue->use_buffering)
1768 update_buffering (queue);
1770 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1771 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1773 GST_QUEUE2_SIGNAL_ADD (queue);
1776 gst_buffer_unmap (buffer, &info);
1783 GST_DEBUG_OBJECT (queue, "we are flushing");
1784 gst_buffer_unmap (buffer, &info);
1785 /* FIXME - GST_FLOW_EOS ? */
1790 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1791 gst_buffer_unmap (buffer, &info);
1798 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1802 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1803 (_("Error while writing to download file.")),
1804 ("%s", g_strerror (errno)));
1807 gst_buffer_unmap (buffer, &info);
1813 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1815 GstQueue2 *queue = q;
1817 GST_TRACE_OBJECT (queue,
1818 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1819 gst_buffer_get_size (*buf));
1821 if (!gst_queue2_create_write (queue, *buf)) {
1822 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1829 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1831 guint *p_size = data;
1834 buf_size = gst_buffer_get_size (*buf);
1835 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1836 *p_size += buf_size;
1840 /* enqueue an item an update the level stats */
1842 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1843 GstQueue2ItemType item_type)
1845 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1849 buffer = GST_BUFFER_CAST (item);
1850 size = gst_buffer_get_size (buffer);
1852 /* add buffer to the statistics */
1853 if (QUEUE_IS_USING_QUEUE (queue)) {
1854 queue->cur_level.buffers++;
1855 queue->cur_level.bytes += size;
1857 queue->bytes_in += size;
1859 /* apply new buffer to segment stats */
1860 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1861 /* update the byterate stats */
1862 update_in_rates (queue);
1864 if (!QUEUE_IS_USING_QUEUE (queue)) {
1865 /* FIXME - check return value? */
1866 gst_queue2_create_write (queue, buffer);
1868 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1869 GstBufferList *buffer_list;
1872 buffer_list = GST_BUFFER_LIST_CAST (item);
1874 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1875 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1877 /* add buffer to the statistics */
1878 if (QUEUE_IS_USING_QUEUE (queue)) {
1879 queue->cur_level.buffers++;
1880 queue->cur_level.bytes += size;
1882 queue->bytes_in += size;
1884 /* apply new buffer to segment stats */
1885 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1887 /* update the byterate stats */
1888 update_in_rates (queue);
1890 if (!QUEUE_IS_USING_QUEUE (queue)) {
1891 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1893 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
1896 event = GST_EVENT_CAST (item);
1898 switch (GST_EVENT_TYPE (event)) {
1900 /* Zero the thresholds, this makes sure the queue is completely
1901 * filled and we can read all data from the queue. */
1902 GST_DEBUG_OBJECT (queue, "we have EOS");
1903 queue->is_eos = TRUE;
1905 case GST_EVENT_SEGMENT:
1906 apply_segment (queue, event, &queue->sink_segment, TRUE);
1907 /* This is our first new segment, we hold it
1908 * as we can't save it on the temp file */
1909 if (!QUEUE_IS_USING_QUEUE (queue)) {
1910 if (queue->segment_event_received)
1911 goto unexpected_event;
1913 queue->segment_event_received = TRUE;
1914 if (queue->starting_segment != NULL)
1915 gst_event_unref (queue->starting_segment);
1916 queue->starting_segment = event;
1919 /* a new segment allows us to accept more buffers if we got EOS
1920 * from downstream */
1921 queue->unexpected = FALSE;
1923 case GST_EVENT_STREAM_START:
1924 if (!QUEUE_IS_USING_QUEUE (queue)) {
1925 gst_event_replace (&queue->stream_start_event, event);
1926 gst_event_unref (event);
1931 if (!QUEUE_IS_USING_QUEUE (queue))
1932 goto unexpected_event;
1936 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1937 item, GST_OBJECT_NAME (queue));
1938 /* we can't really unref since we don't know what it is */
1943 /* update the buffering status */
1944 if (queue->use_buffering)
1945 update_buffering (queue);
1947 if (QUEUE_IS_USING_QUEUE (queue)) {
1948 g_queue_push_tail (&queue->queue, item);
1950 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
1953 GST_QUEUE2_SIGNAL_ADD (queue);
1962 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
1963 gst_event_type_get_name (GST_EVENT_TYPE (item)),
1964 GST_OBJECT_NAME (queue));
1965 gst_event_unref (GST_EVENT_CAST (item));
1970 /* dequeue an item from the queue and update level stats */
1971 static GstMiniObject *
1972 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
1974 GstMiniObject *item;
1976 if (!QUEUE_IS_USING_QUEUE (queue))
1977 item = gst_queue2_read_item_from_file (queue);
1979 item = g_queue_pop_head (&queue->queue);
1984 if (GST_IS_BUFFER (item)) {
1988 buffer = GST_BUFFER_CAST (item);
1989 size = gst_buffer_get_size (buffer);
1990 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
1992 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1993 "retrieved buffer %p from queue", buffer);
1995 if (QUEUE_IS_USING_QUEUE (queue)) {
1996 queue->cur_level.buffers--;
1997 queue->cur_level.bytes -= size;
1999 queue->bytes_out += size;
2001 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2002 /* update the byterate stats */
2003 update_out_rates (queue);
2004 /* update the buffering */
2005 if (queue->use_buffering)
2006 update_buffering (queue);
2008 } else if (GST_IS_EVENT (item)) {
2009 GstEvent *event = GST_EVENT_CAST (item);
2011 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2013 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2014 "retrieved event %p from queue", event);
2016 switch (GST_EVENT_TYPE (event)) {
2018 /* queue is empty now that we dequeued the EOS */
2019 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2021 case GST_EVENT_SEGMENT:
2022 apply_segment (queue, event, &queue->src_segment, FALSE);
2027 } else if (GST_IS_BUFFER_LIST (item)) {
2028 GstBufferList *buffer_list;
2031 buffer_list = GST_BUFFER_LIST_CAST (item);
2032 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2033 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2035 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2036 "retrieved buffer list %p from queue", buffer_list);
2038 if (QUEUE_IS_USING_QUEUE (queue)) {
2039 queue->cur_level.buffers--;
2040 queue->cur_level.bytes -= size;
2042 queue->bytes_out += size;
2044 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2045 /* update the byterate stats */
2046 update_out_rates (queue);
2047 /* update the buffering */
2048 if (queue->use_buffering)
2049 update_buffering (queue);
2053 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2054 item, GST_OBJECT_NAME (queue));
2056 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2058 GST_QUEUE2_SIGNAL_DEL (queue);
2065 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2071 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2076 queue = GST_QUEUE2 (parent);
2078 switch (GST_EVENT_TYPE (event)) {
2079 case GST_EVENT_FLUSH_START:
2081 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2082 if (QUEUE_IS_USING_QUEUE (queue)) {
2084 gst_pad_push_event (queue->srcpad, event);
2086 /* now unblock the chain function */
2087 GST_QUEUE2_MUTEX_LOCK (queue);
2088 queue->srcresult = GST_FLOW_FLUSHING;
2089 queue->sinkresult = GST_FLOW_FLUSHING;
2090 /* unblock the loop and chain functions */
2091 GST_QUEUE2_SIGNAL_ADD (queue);
2092 GST_QUEUE2_SIGNAL_DEL (queue);
2093 GST_QUEUE2_MUTEX_UNLOCK (queue);
2095 /* make sure it pauses, this should happen since we sent
2096 * flush_start downstream. */
2097 gst_pad_pause_task (queue->srcpad);
2098 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2100 GST_QUEUE2_MUTEX_LOCK (queue);
2101 /* flush the sink pad */
2102 queue->sinkresult = GST_FLOW_FLUSHING;
2103 GST_QUEUE2_SIGNAL_DEL (queue);
2104 GST_QUEUE2_MUTEX_UNLOCK (queue);
2106 gst_event_unref (event);
2110 case GST_EVENT_FLUSH_STOP:
2112 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2114 if (QUEUE_IS_USING_QUEUE (queue)) {
2116 gst_pad_push_event (queue->srcpad, event);
2118 GST_QUEUE2_MUTEX_LOCK (queue);
2119 gst_queue2_locked_flush (queue);
2120 queue->srcresult = GST_FLOW_OK;
2121 queue->sinkresult = GST_FLOW_OK;
2122 queue->is_eos = FALSE;
2123 queue->unexpected = FALSE;
2124 /* reset rate counters */
2125 reset_rate_timer (queue);
2126 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2128 GST_QUEUE2_MUTEX_UNLOCK (queue);
2130 GST_QUEUE2_MUTEX_LOCK (queue);
2131 queue->segment_event_received = FALSE;
2132 queue->is_eos = FALSE;
2133 queue->unexpected = FALSE;
2134 queue->sinkresult = GST_FLOW_OK;
2135 GST_QUEUE2_MUTEX_UNLOCK (queue);
2137 gst_event_unref (event);
2142 if (GST_EVENT_IS_SERIALIZED (event)) {
2143 /* serialized events go in the queue */
2144 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2145 /* refuse more events on EOS */
2148 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2149 GST_QUEUE2_MUTEX_UNLOCK (queue);
2151 /* non-serialized events are passed upstream. */
2152 gst_pad_push_event (queue->srcpad, event);
2162 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2163 GST_QUEUE2_MUTEX_UNLOCK (queue);
2164 gst_event_unref (event);
2169 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2170 GST_QUEUE2_MUTEX_UNLOCK (queue);
2171 gst_event_unref (event);
2177 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2182 switch (GST_QUERY_TYPE (query)) {
2184 res = gst_pad_query_default (pad, parent, query);
2191 gst_queue2_is_empty (GstQueue2 * queue)
2193 /* never empty on EOS */
2197 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2198 return queue->current->writing_pos <= queue->current->max_reading_pos;
2200 if (queue->queue.length == 0)
2208 gst_queue2_is_filled (GstQueue2 * queue)
2212 /* always filled on EOS */
2216 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2217 (queue->cur_level.format) >= ((alt_max) ? \
2218 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2220 /* if using a ring buffer we're filled if all ring buffer space is used
2221 * _by the current range_ */
2222 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2223 guint64 rb_size = queue->ring_buffer_max_size;
2224 GST_DEBUG_OBJECT (queue,
2225 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2226 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2227 return CHECK_FILLED (bytes, rb_size);
2230 /* if using file, we're never filled if we don't have EOS */
2231 if (QUEUE_IS_USING_TEMP_FILE (queue))
2234 /* we are never filled when we have no buffers at all */
2235 if (queue->cur_level.buffers == 0)
2238 /* we are filled if one of the current levels exceeds the max */
2239 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2240 || CHECK_FILLED (time, 0);
2242 /* if we need to, use the rate estimate to check against the max time we are
2243 * allowed to queue */
2244 if (queue->use_rate_estimate)
2245 res |= CHECK_FILLED (rate_time, 0);
2251 static GstFlowReturn
2252 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2253 GstMiniObject * item, GstQueue2ItemType item_type)
2255 /* we have to lock the queue since we span threads */
2256 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2257 /* when we received EOS, we refuse more data */
2260 /* when we received unexpected from downstream, refuse more buffers */
2261 if (queue->unexpected)
2262 goto out_unexpected;
2264 if (!gst_queue2_wait_free_space (queue))
2267 /* put buffer in queue now */
2268 gst_queue2_locked_enqueue (queue, item, item_type);
2269 GST_QUEUE2_MUTEX_UNLOCK (queue);
2273 /* special conditions */
2276 GstFlowReturn ret = queue->sinkresult;
2278 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2279 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2280 GST_QUEUE2_MUTEX_UNLOCK (queue);
2281 gst_mini_object_unref (item);
2287 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2288 GST_QUEUE2_MUTEX_UNLOCK (queue);
2289 gst_mini_object_unref (item);
2291 return GST_FLOW_EOS;
2295 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2296 GST_QUEUE2_MUTEX_UNLOCK (queue);
2297 gst_mini_object_unref (item);
2299 return GST_FLOW_EOS;
2303 static GstFlowReturn
2304 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2308 queue = GST_QUEUE2 (parent);
2310 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2311 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2312 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2313 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2314 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2316 return gst_queue2_chain_buffer_or_buffer_list (queue,
2317 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2320 static GstFlowReturn
2321 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2322 GstBufferList * buffer_list)
2326 queue = GST_QUEUE2 (parent);
2328 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2329 "received buffer list %p", buffer_list);
2331 return gst_queue2_chain_buffer_or_buffer_list (queue,
2332 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2335 static GstMiniObject *
2336 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2338 GstMiniObject *data;
2340 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2342 /* stop pushing buffers, we dequeue all items until we see an item that we
2343 * can push again, which is EOS or SEGMENT. If there is nothing in the
2344 * queue we can push, we set a flag to make the sinkpad refuse more
2345 * buffers with an EOS return value until we receive something
2346 * pushable again or we get flushed. */
2347 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2348 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2349 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2350 "dropping EOS buffer %p", data);
2351 gst_buffer_unref (GST_BUFFER_CAST (data));
2352 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2353 GstEvent *event = GST_EVENT_CAST (data);
2354 GstEventType type = GST_EVENT_TYPE (event);
2356 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2357 /* we found a pushable item in the queue, push it out */
2358 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2359 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2362 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2363 "dropping EOS event %p", event);
2364 gst_event_unref (event);
2365 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2366 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2367 "dropping EOS buffer list %p", data);
2368 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2371 /* no more items in the queue. Set the unexpected flag so that upstream
2372 * make us refuse any more buffers on the sinkpad. Since we will still
2373 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2374 * task function does not shut down. */
2375 queue->unexpected = TRUE;
2379 /* dequeue an item from the queue an push it downstream. This functions returns
2380 * the result of the push. */
2381 static GstFlowReturn
2382 gst_queue2_push_one (GstQueue2 * queue)
2384 GstFlowReturn result = GST_FLOW_OK;
2385 GstMiniObject *data;
2386 GstQueue2ItemType item_type;
2388 data = gst_queue2_locked_dequeue (queue, &item_type);
2393 GST_QUEUE2_MUTEX_UNLOCK (queue);
2395 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2401 buffer = GST_BUFFER_CAST (data);
2403 caps = GST_BUFFER_CAPS (buffer);
2407 /* set caps before pushing the buffer so that core does not try to do
2408 * something fancy to check if this is possible. */
2409 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2410 gst_pad_set_caps (queue->srcpad, caps);
2413 result = gst_pad_push (queue->srcpad, buffer);
2415 /* need to check for srcresult here as well */
2416 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2417 if (result == GST_FLOW_EOS) {
2418 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2421 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2422 * to the caller so that the task function does not shut down */
2423 result = GST_FLOW_OK;
2425 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2426 GstEvent *event = GST_EVENT_CAST (data);
2427 GstEventType type = GST_EVENT_TYPE (event);
2429 gst_pad_push_event (queue->srcpad, event);
2431 /* if we're EOS, return EOS so that the task pauses. */
2432 if (type == GST_EVENT_EOS) {
2433 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2434 "pushed EOS event %p, return EOS", event);
2435 result = GST_FLOW_EOS;
2438 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2439 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2440 GstBufferList *buffer_list;
2442 GstBuffer *first_buf;
2446 buffer_list = GST_BUFFER_LIST_CAST (data);
2449 first_buf = gst_buffer_list_get (buffer_list, 0);
2450 caps = (first_buf != NULL) ? GST_BUFFER_CAPS (first_buf) : NULL;
2452 /* set caps before pushing the buffer so that core does not try to do
2453 * something fancy to check if this is possible. */
2454 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2455 gst_pad_set_caps (queue->srcpad, caps);
2458 result = gst_pad_push_list (queue->srcpad, buffer_list);
2460 /* need to check for srcresult here as well */
2461 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2462 if (result == GST_FLOW_EOS) {
2463 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2466 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2467 * to the caller so that the task function does not shut down */
2468 result = GST_FLOW_OK;
2476 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2477 "exit because we have no item in the queue");
2478 return GST_FLOW_ERROR;
2482 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2483 return GST_FLOW_FLUSHING;
2487 /* called repeatedly with @pad as the source pad. This function should push out
2488 * data to the peer element. */
2490 gst_queue2_loop (GstPad * pad)
2495 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2497 /* have to lock for thread-safety */
2498 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2500 if (gst_queue2_is_empty (queue)) {
2503 /* pause the timer while we wait. The fact that we are waiting does not mean
2504 * the byterate on the output pad is lower */
2505 if ((started = queue->out_timer_started))
2506 g_timer_stop (queue->out_timer);
2508 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2509 "queue is empty, waiting for new data");
2511 /* Wait for data to be available, we could be unlocked because of a flush. */
2512 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2514 while (gst_queue2_is_empty (queue));
2516 /* and continue if we were running before */
2518 g_timer_continue (queue->out_timer);
2520 ret = gst_queue2_push_one (queue);
2521 queue->srcresult = ret;
2522 queue->sinkresult = ret;
2523 if (ret != GST_FLOW_OK)
2526 GST_QUEUE2_MUTEX_UNLOCK (queue);
2533 gboolean eos = queue->is_eos;
2534 GstFlowReturn ret = queue->srcresult;
2536 gst_pad_pause_task (queue->srcpad);
2537 GST_QUEUE2_MUTEX_UNLOCK (queue);
2538 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2539 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2540 /* let app know about us giving up if upstream is not expected to do so */
2541 /* EOS is already taken care of elsewhere */
2542 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2543 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2544 (_("Internal data flow error.")),
2545 ("streaming task paused, reason %s (%d)",
2546 gst_flow_get_name (ret), ret));
2547 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2554 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2556 gboolean res = TRUE;
2557 GstQueue2 *queue = GST_QUEUE2 (parent);
2559 #ifndef GST_DISABLE_GST_DEBUG
2560 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2561 event, GST_EVENT_TYPE_NAME (event));
2564 switch (GST_EVENT_TYPE (event)) {
2565 case GST_EVENT_FLUSH_START:
2566 if (QUEUE_IS_USING_QUEUE (queue)) {
2567 /* just forward upstream */
2568 res = gst_pad_push_event (queue->sinkpad, event);
2570 /* now unblock the getrange function */
2571 GST_QUEUE2_MUTEX_LOCK (queue);
2572 GST_DEBUG_OBJECT (queue, "flushing");
2573 queue->srcresult = GST_FLOW_FLUSHING;
2574 GST_QUEUE2_SIGNAL_ADD (queue);
2575 GST_QUEUE2_MUTEX_UNLOCK (queue);
2577 /* when using a temp file, we eat the event */
2579 gst_event_unref (event);
2582 case GST_EVENT_FLUSH_STOP:
2583 if (QUEUE_IS_USING_QUEUE (queue)) {
2584 /* just forward upstream */
2585 res = gst_pad_push_event (queue->sinkpad, event);
2587 /* now unblock the getrange function */
2588 GST_QUEUE2_MUTEX_LOCK (queue);
2589 queue->srcresult = GST_FLOW_OK;
2590 if (queue->current) {
2591 /* forget the highest read offset, we'll calculate a new one when we
2592 * get the next getrange request. We need to do this in order to reset
2593 * the buffering percentage */
2594 queue->current->max_reading_pos = 0;
2596 GST_QUEUE2_MUTEX_UNLOCK (queue);
2598 /* when using a temp file, we eat the event */
2600 gst_event_unref (event);
2604 res = gst_pad_push_event (queue->sinkpad, event);
2612 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2616 queue = GST_QUEUE2 (parent);
2618 switch (GST_QUERY_TYPE (query)) {
2619 case GST_QUERY_POSITION:
2624 if (!gst_pad_peer_query (queue->sinkpad, query))
2627 /* get peer position */
2628 gst_query_parse_position (query, &format, &peer_pos);
2630 /* FIXME: this code assumes that there's no discont in the queue */
2632 case GST_FORMAT_BYTES:
2633 peer_pos -= queue->cur_level.bytes;
2635 case GST_FORMAT_TIME:
2636 peer_pos -= queue->cur_level.time;
2639 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2640 "know how to adjust value", gst_format_get_name (format));
2643 /* set updated position */
2644 gst_query_set_position (query, format, peer_pos);
2647 case GST_QUERY_DURATION:
2649 GST_DEBUG_OBJECT (queue, "doing peer query");
2651 if (!gst_pad_peer_query (queue->sinkpad, query))
2654 GST_DEBUG_OBJECT (queue, "peer query success");
2657 case GST_QUERY_BUFFERING:
2661 GST_DEBUG_OBJECT (queue, "query buffering");
2663 /* FIXME - is this condition correct? what should ring buffer do? */
2664 if (QUEUE_IS_USING_QUEUE (queue)) {
2665 /* no temp file, just forward to the peer */
2666 if (!gst_pad_peer_query (queue->sinkpad, query))
2668 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2670 gint64 start, stop, range_start, range_stop;
2671 guint64 writing_pos;
2673 gint64 estimated_total, buffering_left;
2675 gboolean peer_res, is_buffering, is_eos;
2676 gdouble byte_in_rate, byte_out_rate;
2677 GstQueue2Range *queued_ranges;
2679 /* we need a current download region */
2680 if (queue->current == NULL)
2683 writing_pos = queue->current->writing_pos;
2684 byte_in_rate = queue->byte_in_rate;
2685 byte_out_rate = queue->byte_out_rate;
2686 is_buffering = queue->is_buffering;
2687 is_eos = queue->is_eos;
2688 percent = queue->buffering_percent;
2691 /* we're EOS, we know the duration in bytes now */
2693 duration = writing_pos;
2695 /* get duration of upstream in bytes */
2696 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2697 GST_FORMAT_BYTES, &duration);
2700 /* calculate remaining and total download time */
2701 if (peer_res && byte_in_rate > 0.0) {
2702 estimated_total = (duration * 1000) / byte_in_rate;
2703 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2705 estimated_total = -1;
2706 buffering_left = -1;
2708 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2709 G_GINT64_FORMAT, estimated_total, buffering_left);
2711 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2714 case GST_FORMAT_PERCENT:
2715 /* we need duration */
2719 GST_DEBUG_OBJECT (queue,
2720 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2721 duration, writing_pos);
2724 /* get our available data relative to the duration */
2726 stop = GST_FORMAT_PERCENT_MAX * writing_pos / duration;
2730 case GST_FORMAT_BYTES:
2740 /* fill out the buffered ranges */
2741 for (queued_ranges = queue->ranges; queued_ranges;
2742 queued_ranges = queued_ranges->next) {
2744 case GST_FORMAT_PERCENT:
2745 if (duration == -1) {
2750 range_start = 100 * queued_ranges->offset / duration;
2751 range_stop = 100 * queued_ranges->writing_pos / duration;
2753 case GST_FORMAT_BYTES:
2754 range_start = queued_ranges->offset;
2755 range_stop = queued_ranges->writing_pos;
2762 if (range_start == range_stop)
2764 GST_DEBUG_OBJECT (queue,
2765 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2766 G_GINT64_FORMAT, range_start, range_stop);
2767 gst_query_add_buffering_range (query, range_start, range_stop);
2770 gst_query_set_buffering_percent (query, is_buffering, percent);
2771 gst_query_set_buffering_range (query, format, start, stop,
2773 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2774 byte_in_rate, byte_out_rate, buffering_left);
2778 case GST_QUERY_SCHEDULING:
2781 GstSchedulingFlags flags = 0;
2783 /* we can operate in pull mode when we are using a tempfile */
2784 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2787 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2788 gst_query_set_scheduling (query, flags, 0, -1, 0);
2790 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
2791 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
2795 /* peer handled other queries */
2796 if (!gst_pad_query_default (pad, parent, query))
2806 GST_DEBUG_OBJECT (queue, "failed peer query");
2812 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2814 GstQueue2 *queue = GST_QUEUE2 (element);
2816 /* simply forward to the srcpad query function */
2817 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
2822 gst_queue2_update_upstream_size (GstQueue2 * queue)
2824 gint64 upstream_size = -1;
2826 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
2828 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2829 queue->upstream_size = upstream_size;
2833 static GstFlowReturn
2834 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
2835 guint length, GstBuffer ** buffer)
2840 queue = GST_QUEUE2_CAST (parent);
2842 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2843 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2844 offset = (offset == -1) ? queue->current->reading_pos : offset;
2846 GST_DEBUG_OBJECT (queue,
2847 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2849 /* catch any reads beyond the size of the file here to make sure queue2
2850 * doesn't send seek events beyond the size of the file upstream, since
2851 * that would confuse elements such as souphttpsrc and/or http servers.
2852 * Demuxers often just loop until EOS at the end of the file to figure out
2853 * when they've read all the end-headers or index chunks. */
2854 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2855 gst_queue2_update_upstream_size (queue);
2856 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2857 goto out_unexpected;
2860 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2861 gst_queue2_update_upstream_size (queue);
2862 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2863 length = queue->upstream_size - offset;
2864 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2868 /* FIXME - function will block when the range is not yet available */
2869 ret = gst_queue2_create_read (queue, offset, length, buffer);
2870 GST_QUEUE2_MUTEX_UNLOCK (queue);
2877 ret = queue->srcresult;
2879 GST_DEBUG_OBJECT (queue, "we are flushing");
2880 GST_QUEUE2_MUTEX_UNLOCK (queue);
2885 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2886 GST_QUEUE2_MUTEX_UNLOCK (queue);
2887 return GST_FLOW_EOS;
2891 /* sink currently only operates in push mode */
2893 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
2894 GstPadMode mode, gboolean active)
2899 queue = GST_QUEUE2 (parent);
2902 case GST_PAD_MODE_PUSH:
2904 GST_QUEUE2_MUTEX_LOCK (queue);
2905 GST_DEBUG_OBJECT (queue, "activating push mode");
2906 queue->srcresult = GST_FLOW_OK;
2907 queue->sinkresult = GST_FLOW_OK;
2908 queue->is_eos = FALSE;
2909 queue->unexpected = FALSE;
2910 reset_rate_timer (queue);
2911 GST_QUEUE2_MUTEX_UNLOCK (queue);
2913 /* unblock chain function */
2914 GST_QUEUE2_MUTEX_LOCK (queue);
2915 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2916 queue->srcresult = GST_FLOW_FLUSHING;
2917 queue->sinkresult = GST_FLOW_FLUSHING;
2918 gst_queue2_locked_flush (queue);
2919 GST_QUEUE2_MUTEX_UNLOCK (queue);
2930 /* src operating in push mode, we start a task on the source pad that pushes out
2931 * buffers from the queue */
2933 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
2935 gboolean result = FALSE;
2938 queue = GST_QUEUE2 (parent);
2941 GST_QUEUE2_MUTEX_LOCK (queue);
2942 GST_DEBUG_OBJECT (queue, "activating push mode");
2943 queue->srcresult = GST_FLOW_OK;
2944 queue->sinkresult = GST_FLOW_OK;
2945 queue->is_eos = FALSE;
2946 queue->unexpected = FALSE;
2947 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
2948 GST_QUEUE2_MUTEX_UNLOCK (queue);
2950 /* unblock loop function */
2951 GST_QUEUE2_MUTEX_LOCK (queue);
2952 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2953 queue->srcresult = GST_FLOW_FLUSHING;
2954 queue->sinkresult = GST_FLOW_FLUSHING;
2955 /* the item add signal will unblock */
2956 GST_QUEUE2_SIGNAL_ADD (queue);
2957 GST_QUEUE2_MUTEX_UNLOCK (queue);
2959 /* step 2, make sure streaming finishes */
2960 result = gst_pad_stop_task (pad);
2966 /* pull mode, downstream will call our getrange function */
2968 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
2973 queue = GST_QUEUE2 (parent);
2976 GST_QUEUE2_MUTEX_LOCK (queue);
2977 if (!QUEUE_IS_USING_QUEUE (queue)) {
2978 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2979 /* open the temp file now */
2980 result = gst_queue2_open_temp_location_file (queue);
2981 } else if (!queue->ring_buffer) {
2982 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
2983 result = ! !queue->ring_buffer;
2988 GST_DEBUG_OBJECT (queue, "activating pull mode");
2989 init_ranges (queue);
2990 queue->srcresult = GST_FLOW_OK;
2991 queue->sinkresult = GST_FLOW_OK;
2992 queue->is_eos = FALSE;
2993 queue->unexpected = FALSE;
2994 queue->upstream_size = 0;
2996 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
2997 /* this is not allowed, we cannot operate in pull mode without a temp
2999 queue->srcresult = GST_FLOW_FLUSHING;
3000 queue->sinkresult = GST_FLOW_FLUSHING;
3003 GST_QUEUE2_MUTEX_UNLOCK (queue);
3005 GST_QUEUE2_MUTEX_LOCK (queue);
3006 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3007 queue->srcresult = GST_FLOW_FLUSHING;
3008 queue->sinkresult = GST_FLOW_FLUSHING;
3009 /* this will unlock getrange */
3010 GST_QUEUE2_SIGNAL_ADD (queue);
3012 GST_QUEUE2_MUTEX_UNLOCK (queue);
3019 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3025 case GST_PAD_MODE_PULL:
3026 res = gst_queue2_src_activate_pull (pad, parent, active);
3028 case GST_PAD_MODE_PUSH:
3029 res = gst_queue2_src_activate_push (pad, parent, active);
3032 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3039 static GstStateChangeReturn
3040 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3043 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3045 queue = GST_QUEUE2 (element);
3047 switch (transition) {
3048 case GST_STATE_CHANGE_NULL_TO_READY:
3050 case GST_STATE_CHANGE_READY_TO_PAUSED:
3051 GST_QUEUE2_MUTEX_LOCK (queue);
3052 if (!QUEUE_IS_USING_QUEUE (queue)) {
3053 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3054 if (!gst_queue2_open_temp_location_file (queue))
3055 ret = GST_STATE_CHANGE_FAILURE;
3057 if (queue->ring_buffer) {
3058 g_free (queue->ring_buffer);
3059 queue->ring_buffer = NULL;
3061 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3062 ret = GST_STATE_CHANGE_FAILURE;
3064 init_ranges (queue);
3066 queue->segment_event_received = FALSE;
3067 queue->starting_segment = NULL;
3068 gst_event_replace (&queue->stream_start_event, NULL);
3069 GST_QUEUE2_MUTEX_UNLOCK (queue);
3071 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3077 if (ret == GST_STATE_CHANGE_FAILURE)
3080 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3082 if (ret == GST_STATE_CHANGE_FAILURE)
3085 switch (transition) {
3086 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3088 case GST_STATE_CHANGE_PAUSED_TO_READY:
3089 GST_QUEUE2_MUTEX_LOCK (queue);
3090 if (!QUEUE_IS_USING_QUEUE (queue)) {
3091 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3092 gst_queue2_close_temp_location_file (queue);
3093 } else if (queue->ring_buffer) {
3094 g_free (queue->ring_buffer);
3095 queue->ring_buffer = NULL;
3097 clean_ranges (queue);
3099 if (queue->starting_segment != NULL) {
3100 gst_event_unref (queue->starting_segment);
3101 queue->starting_segment = NULL;
3103 gst_event_replace (&queue->stream_start_event, NULL);
3104 GST_QUEUE2_MUTEX_UNLOCK (queue);
3106 case GST_STATE_CHANGE_READY_TO_NULL:
3115 /* changing the capacity of the queue must wake up
3116 * the _chain function, it might have more room now
3117 * to store the buffer/event in the queue */
3118 #define QUEUE_CAPACITY_CHANGE(q)\
3119 GST_QUEUE2_SIGNAL_DEL (queue);
3121 /* Changing the minimum required fill level must
3122 * wake up the _loop function as it might now
3123 * be able to preceed.
3125 #define QUEUE_THRESHOLD_CHANGE(q)\
3126 GST_QUEUE2_SIGNAL_ADD (queue);
3129 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3133 /* the element must be stopped in order to do this */
3134 GST_OBJECT_LOCK (queue);
3135 state = GST_STATE (queue);
3136 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3138 GST_OBJECT_UNLOCK (queue);
3140 /* set new location */
3141 g_free (queue->temp_template);
3142 queue->temp_template = g_strdup (template);
3149 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3150 GST_OBJECT_UNLOCK (queue);
3155 gst_queue2_set_property (GObject * object,
3156 guint prop_id, const GValue * value, GParamSpec * pspec)
3158 GstQueue2 *queue = GST_QUEUE2 (object);
3160 /* someone could change levels here, and since this
3161 * affects the get/put funcs, we need to lock for safety. */
3162 GST_QUEUE2_MUTEX_LOCK (queue);
3165 case PROP_MAX_SIZE_BYTES:
3166 queue->max_level.bytes = g_value_get_uint (value);
3167 QUEUE_CAPACITY_CHANGE (queue);
3169 case PROP_MAX_SIZE_BUFFERS:
3170 queue->max_level.buffers = g_value_get_uint (value);
3171 QUEUE_CAPACITY_CHANGE (queue);
3173 case PROP_MAX_SIZE_TIME:
3174 queue->max_level.time = g_value_get_uint64 (value);
3175 /* set rate_time to the same value. We use an extra field in the level
3176 * structure so that we can easily access and compare it */
3177 queue->max_level.rate_time = queue->max_level.time;
3178 QUEUE_CAPACITY_CHANGE (queue);
3180 case PROP_USE_BUFFERING:
3181 queue->use_buffering = g_value_get_boolean (value);
3183 case PROP_USE_RATE_ESTIMATE:
3184 queue->use_rate_estimate = g_value_get_boolean (value);
3186 case PROP_LOW_PERCENT:
3187 queue->low_percent = g_value_get_int (value);
3189 case PROP_HIGH_PERCENT:
3190 queue->high_percent = g_value_get_int (value);
3192 case PROP_TEMP_TEMPLATE:
3193 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3195 case PROP_TEMP_LOCATION:
3196 g_free (queue->temp_location);
3197 queue->temp_location = g_value_dup_string (value);
3198 /* you can set the property back to NULL to make it use the temp-tmpl
3200 queue->temp_location_set = queue->temp_location != NULL;
3202 case PROP_TEMP_REMOVE:
3203 queue->temp_remove = g_value_get_boolean (value);
3205 case PROP_RING_BUFFER_MAX_SIZE:
3206 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3213 GST_QUEUE2_MUTEX_UNLOCK (queue);
3217 gst_queue2_get_property (GObject * object,
3218 guint prop_id, GValue * value, GParamSpec * pspec)
3220 GstQueue2 *queue = GST_QUEUE2 (object);
3222 GST_QUEUE2_MUTEX_LOCK (queue);
3225 case PROP_CUR_LEVEL_BYTES:
3226 g_value_set_uint (value, queue->cur_level.bytes);
3228 case PROP_CUR_LEVEL_BUFFERS:
3229 g_value_set_uint (value, queue->cur_level.buffers);
3231 case PROP_CUR_LEVEL_TIME:
3232 g_value_set_uint64 (value, queue->cur_level.time);
3234 case PROP_MAX_SIZE_BYTES:
3235 g_value_set_uint (value, queue->max_level.bytes);
3237 case PROP_MAX_SIZE_BUFFERS:
3238 g_value_set_uint (value, queue->max_level.buffers);
3240 case PROP_MAX_SIZE_TIME:
3241 g_value_set_uint64 (value, queue->max_level.time);
3243 case PROP_USE_BUFFERING:
3244 g_value_set_boolean (value, queue->use_buffering);
3246 case PROP_USE_RATE_ESTIMATE:
3247 g_value_set_boolean (value, queue->use_rate_estimate);
3249 case PROP_LOW_PERCENT:
3250 g_value_set_int (value, queue->low_percent);
3252 case PROP_HIGH_PERCENT:
3253 g_value_set_int (value, queue->high_percent);
3255 case PROP_TEMP_TEMPLATE:
3256 g_value_set_string (value, queue->temp_template);
3258 case PROP_TEMP_LOCATION:
3259 g_value_set_string (value, queue->temp_location);
3261 case PROP_TEMP_REMOVE:
3262 g_value_set_boolean (value, queue->temp_remove);
3264 case PROP_RING_BUFFER_MAX_SIZE:
3265 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3272 GST_QUEUE2_MUTEX_UNLOCK (queue);