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); \
213 #define _do_init(bla) \
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");
218 GST_BOILERPLATE_FULL (GstQueue2, gst_queue2, GstElement, GST_TYPE_ELEMENT,
221 static void gst_queue2_finalize (GObject * object);
223 static void gst_queue2_set_property (GObject * object,
224 guint prop_id, const GValue * value, GParamSpec * pspec);
225 static void gst_queue2_get_property (GObject * object,
226 guint prop_id, GValue * value, GParamSpec * pspec);
228 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstBuffer * buffer);
229 static GstFlowReturn gst_queue2_chain_list (GstPad * pad,
230 GstBufferList * buffer_list);
231 static GstFlowReturn gst_queue2_bufferalloc (GstPad * pad, guint64 offset,
232 guint size, GstCaps * caps, GstBuffer ** buf);
233 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
234 static void gst_queue2_loop (GstPad * pad);
236 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event);
238 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstEvent * event);
239 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstQuery * query);
240 static gboolean gst_queue2_handle_query (GstElement * element,
243 static GstCaps *gst_queue2_getcaps (GstPad * pad);
244 static gboolean gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps);
246 static GstFlowReturn gst_queue2_get_range (GstPad * pad, guint64 offset,
247 guint length, GstBuffer ** buffer);
248 static gboolean gst_queue2_src_checkgetrange_function (GstPad * pad);
250 static gboolean gst_queue2_src_activate_pull (GstPad * pad, gboolean active);
251 static gboolean gst_queue2_src_activate_push (GstPad * pad, gboolean active);
252 static gboolean gst_queue2_sink_activate_push (GstPad * pad, 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_base_init (gpointer g_class)
274 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
276 gst_element_class_add_pad_template (gstelement_class,
277 gst_static_pad_template_get (&srctemplate));
278 gst_element_class_add_pad_template (gstelement_class,
279 gst_static_pad_template_get (&sinktemplate));
281 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
284 "Erik Walthinsen <omega@cse.ogi.edu>, "
285 "Wim Taymans <wim.taymans@gmail.com>");
289 gst_queue2_class_init (GstQueue2Class * klass)
291 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
292 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
294 gobject_class->set_property = gst_queue2_set_property;
295 gobject_class->get_property = gst_queue2_get_property;
298 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
299 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
300 "Current amount of data in the queue (bytes)",
301 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
303 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
304 "Current number of buffers in the queue",
305 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
307 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
308 "Current amount of data in the queue (in ns)",
309 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
312 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
313 "Max. amount of data in the queue (bytes, 0=disable)",
314 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
317 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
318 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
319 DEFAULT_MAX_SIZE_BUFFERS,
320 G_PARAM_READWRITE | 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 | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
327 g_param_spec_boolean ("use-buffering", "Use buffering",
328 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
329 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
331 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
332 "Estimate the bitrate of the stream to calculate time level",
333 DEFAULT_USE_RATE_ESTIMATE,
334 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
336 g_param_spec_int ("low-percent", "Low percent",
337 "Low threshold for buffering to start. Only used if use-buffering is True",
338 0, 100, DEFAULT_LOW_PERCENT,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
341 g_param_spec_int ("high-percent", "High percent",
342 "High threshold for buffering to finish. Only used if use-buffering is True",
343 0, 100, DEFAULT_HIGH_PERCENT,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
347 g_param_spec_string ("temp-template", "Temporary File Template",
348 "File template to store temporary files in, should contain directory "
349 "and XXXXXX. (NULL == disabled)",
350 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
353 g_param_spec_string ("temp-location", "Temporary File Location",
354 "Location to store temporary files in (Deprecated: Only read this "
355 "property, use temp-template to configure the name template)",
356 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 * GstQueue2:temp-remove
361 * 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.
378 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
379 g_param_spec_uint64 ("ring-buffer-max-size",
380 "Max. ring buffer size (bytes)",
381 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
382 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
383 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 /* set several parent class virtual functions */
386 gobject_class->finalize = gst_queue2_finalize;
388 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
389 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
393 gst_queue2_init (GstQueue2 * queue, GstQueue2Class * g_class)
395 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
397 gst_pad_set_chain_function (queue->sinkpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_chain));
399 gst_pad_set_chain_list_function (queue->sinkpad,
400 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
401 gst_pad_set_activatepush_function (queue->sinkpad,
402 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_push));
403 gst_pad_set_event_function (queue->sinkpad,
404 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
405 gst_pad_set_getcaps_function (queue->sinkpad,
406 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
407 gst_pad_set_acceptcaps_function (queue->sinkpad,
408 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
409 gst_pad_set_bufferalloc_function (queue->sinkpad,
410 GST_DEBUG_FUNCPTR (gst_queue2_bufferalloc));
411 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
413 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
415 gst_pad_set_activatepull_function (queue->srcpad,
416 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_pull));
417 gst_pad_set_activatepush_function (queue->srcpad,
418 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_push));
419 gst_pad_set_getrange_function (queue->srcpad,
420 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
421 gst_pad_set_checkgetrange_function (queue->srcpad,
422 GST_DEBUG_FUNCPTR (gst_queue2_src_checkgetrange_function));
423 gst_pad_set_getcaps_function (queue->srcpad,
424 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
425 gst_pad_set_acceptcaps_function (queue->srcpad,
426 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
427 gst_pad_set_event_function (queue->srcpad,
428 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
429 gst_pad_set_query_function (queue->srcpad,
430 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
431 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
434 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
435 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
436 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
437 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
438 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
439 queue->use_buffering = DEFAULT_USE_BUFFERING;
440 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
441 queue->low_percent = DEFAULT_LOW_PERCENT;
442 queue->high_percent = DEFAULT_HIGH_PERCENT;
444 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
445 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
447 queue->sinktime = GST_CLOCK_TIME_NONE;
448 queue->srctime = GST_CLOCK_TIME_NONE;
449 queue->sink_tainted = TRUE;
450 queue->src_tainted = TRUE;
452 queue->srcresult = GST_FLOW_WRONG_STATE;
453 queue->sinkresult = GST_FLOW_WRONG_STATE;
454 queue->is_eos = FALSE;
455 queue->in_timer = g_timer_new ();
456 queue->out_timer = g_timer_new ();
458 queue->qlock = g_mutex_new ();
459 queue->waiting_add = FALSE;
460 queue->item_add = g_cond_new ();
461 queue->waiting_del = FALSE;
462 queue->item_del = g_cond_new ();
463 g_queue_init (&queue->queue);
465 queue->buffering_percent = 100;
467 /* tempfile related */
468 queue->temp_template = NULL;
469 queue->temp_location = NULL;
470 queue->temp_location_set = FALSE;
471 queue->temp_remove = DEFAULT_TEMP_REMOVE;
473 queue->ring_buffer = NULL;
474 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
476 GST_DEBUG_OBJECT (queue,
477 "initialized queue's not_empty & not_full conditions");
480 /* called only once, as opposed to dispose */
482 gst_queue2_finalize (GObject * object)
484 GstQueue2 *queue = GST_QUEUE2 (object);
486 GST_DEBUG_OBJECT (queue, "finalizing queue");
488 while (!g_queue_is_empty (&queue->queue)) {
489 GstMiniObject *data = g_queue_pop_head (&queue->queue);
491 gst_mini_object_unref (data);
494 g_queue_clear (&queue->queue);
495 g_mutex_free (queue->qlock);
496 g_cond_free (queue->item_add);
497 g_cond_free (queue->item_del);
498 g_timer_destroy (queue->in_timer);
499 g_timer_destroy (queue->out_timer);
501 /* temp_file path cleanup */
502 g_free (queue->temp_template);
503 g_free (queue->temp_location);
505 G_OBJECT_CLASS (parent_class)->finalize (object);
509 debug_ranges (GstQueue2 * queue)
511 GstQueue2Range *walk;
513 for (walk = queue->ranges; walk; walk = walk->next) {
514 GST_DEBUG_OBJECT (queue,
515 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
516 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
517 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
518 walk->rb_writing_pos, walk->reading_pos,
519 walk == queue->current ? "**y**" : " n ");
523 /* clear all the downloaded ranges */
525 clean_ranges (GstQueue2 * queue)
527 GST_DEBUG_OBJECT (queue, "clean queue ranges");
529 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
530 queue->ranges = NULL;
531 queue->current = NULL;
534 /* find a range that contains @offset or NULL when nothing does */
535 static GstQueue2Range *
536 find_range (GstQueue2 * queue, guint64 offset)
538 GstQueue2Range *range = NULL;
539 GstQueue2Range *walk;
541 /* first do a quick check for the current range */
542 for (walk = queue->ranges; walk; walk = walk->next) {
543 if (offset >= walk->offset && offset <= walk->writing_pos) {
544 /* we can reuse an existing range */
550 GST_DEBUG_OBJECT (queue,
551 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
552 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
554 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
560 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
562 guint64 max_reading_pos, writing_pos;
564 writing_pos = range->writing_pos;
565 max_reading_pos = range->max_reading_pos;
567 if (writing_pos > max_reading_pos)
568 queue->cur_level.bytes = writing_pos - max_reading_pos;
570 queue->cur_level.bytes = 0;
573 /* make a new range for @offset or reuse an existing range */
574 static GstQueue2Range *
575 add_range (GstQueue2 * queue, guint64 offset)
577 GstQueue2Range *range, *prev, *next;
579 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
581 if ((range = find_range (queue, offset))) {
582 GST_DEBUG_OBJECT (queue,
583 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
585 range->writing_pos = offset;
587 GST_DEBUG_OBJECT (queue,
588 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
590 range = g_slice_new0 (GstQueue2Range);
591 range->offset = offset;
592 /* we want to write to the next location in the ring buffer */
593 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
594 range->writing_pos = offset;
595 range->rb_writing_pos = range->rb_offset;
596 range->reading_pos = offset;
597 range->max_reading_pos = offset;
601 next = queue->ranges;
603 if (next->offset > offset) {
604 /* insert before next */
605 GST_DEBUG_OBJECT (queue,
606 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
618 queue->ranges = range;
620 debug_ranges (queue);
622 /* update the stats for this range */
623 update_cur_level (queue, range);
629 /* clear and init the download ranges for offset 0 */
631 init_ranges (GstQueue2 * queue)
633 GST_DEBUG_OBJECT (queue, "init queue ranges");
635 /* get rid of all the current ranges */
636 clean_ranges (queue);
637 /* make a range for offset 0 */
638 queue->current = add_range (queue, 0);
642 gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps)
648 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
650 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
651 result = gst_pad_peer_accept_caps (otherpad, caps);
657 gst_queue2_getcaps (GstPad * pad)
663 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
664 if (G_UNLIKELY (queue == NULL))
665 return gst_caps_new_any ();
667 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
668 result = gst_pad_peer_get_caps (otherpad);
670 result = gst_caps_new_any ();
672 gst_object_unref (queue);
678 gst_queue2_bufferalloc (GstPad * pad, guint64 offset, guint size,
679 GstCaps * caps, GstBuffer ** buf)
682 GstFlowReturn result;
684 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
686 /* Forward to src pad, without setting caps on the src pad */
687 result = gst_pad_alloc_buffer (queue->srcpad, offset, size, caps, buf);
692 /* calculate the diff between running time on the sink and src of the queue.
693 * This is the total amount of time in the queue. */
695 update_time_level (GstQueue2 * queue)
697 if (queue->sink_tainted) {
699 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
700 queue->sink_segment.last_stop);
701 queue->sink_tainted = FALSE;
704 if (queue->src_tainted) {
706 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
707 queue->src_segment.last_stop);
708 queue->src_tainted = FALSE;
711 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
712 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
714 if (queue->sinktime != GST_CLOCK_TIME_NONE
715 && queue->srctime != GST_CLOCK_TIME_NONE
716 && queue->sinktime >= queue->srctime)
717 queue->cur_level.time = queue->sinktime - queue->srctime;
719 queue->cur_level.time = 0;
722 /* take a NEWSEGMENT event and apply the values to segment, updating the time
725 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
731 gint64 start, stop, time;
733 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
734 &format, &start, &stop, &time);
736 GST_DEBUG_OBJECT (queue,
737 "received NEWSEGMENT update %d, rate %lf, applied rate %lf, "
739 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
740 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
742 if (format == GST_FORMAT_BYTES) {
743 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
744 /* start is where we'll be getting from and as such writing next */
745 queue->current = add_range (queue, start);
746 /* update the stats for this range */
747 update_cur_level (queue, queue->current);
751 /* now configure the values, we use these to track timestamps on the
753 if (format != GST_FORMAT_TIME) {
754 /* non-time format, pretent the current time segment is closed with a
755 * 0 start and unknown stop time. */
757 format = GST_FORMAT_TIME;
762 gst_segment_set_newsegment_full (segment, update,
763 rate, arate, format, start, stop, time);
765 GST_DEBUG_OBJECT (queue,
766 "configured NEWSEGMENT %" GST_SEGMENT_FORMAT, segment);
769 queue->sink_tainted = TRUE;
771 queue->src_tainted = TRUE;
773 /* segment can update the time level of the queue */
774 update_time_level (queue);
777 /* take a buffer and update segment, updating the time level of the queue. */
779 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
782 GstClockTime duration, timestamp;
784 timestamp = GST_BUFFER_TIMESTAMP (buffer);
785 duration = GST_BUFFER_DURATION (buffer);
787 /* if no timestamp is set, assume it's continuous with the previous
789 if (timestamp == GST_CLOCK_TIME_NONE)
790 timestamp = segment->last_stop;
793 if (duration != GST_CLOCK_TIME_NONE)
794 timestamp += duration;
796 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
797 GST_TIME_ARGS (timestamp));
799 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
802 queue->sink_tainted = TRUE;
804 queue->src_tainted = TRUE;
806 /* calc diff with other end */
807 update_time_level (queue);
810 static GstBufferListItem
811 buffer_list_apply_time (GstBuffer ** buf, guint group, guint idx, gpointer data)
813 GstClockTime *timestamp = data;
815 GST_TRACE ("buffer %u in group %u has ts %" GST_TIME_FORMAT
816 " duration %" GST_TIME_FORMAT, idx, group,
817 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
818 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
820 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
821 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
823 if (GST_BUFFER_DURATION_IS_VALID (*buf))
824 *timestamp += GST_BUFFER_DURATION (*buf);
826 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
827 return GST_BUFFER_LIST_CONTINUE;
830 /* take a buffer list and update segment, updating the time level of the queue */
832 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
833 GstSegment * segment, gboolean is_sink)
835 GstClockTime timestamp;
837 /* if no timestamp is set, assume it's continuous with the previous time */
838 timestamp = segment->last_stop;
840 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
842 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
843 GST_TIME_ARGS (timestamp));
845 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
848 queue->sink_tainted = TRUE;
850 queue->src_tainted = TRUE;
852 /* calc diff with other end */
853 update_time_level (queue);
857 update_buffering (GstQueue2 * queue)
860 gboolean post = FALSE;
862 if (queue->high_percent <= 0)
865 #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)
868 /* on EOS we are always 100% full, we set the var here so that it we can
869 * reuse the logic below to stop buffering */
871 GST_LOG_OBJECT (queue, "we are EOS");
873 /* figure out the percent we are filled, we take the max of all formats. */
875 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
876 percent = GET_PERCENT (bytes, 0);
878 guint64 rb_size = queue->ring_buffer_max_size;
879 percent = GET_PERCENT (bytes, rb_size);
881 percent = MAX (percent, GET_PERCENT (time, 0));
882 percent = MAX (percent, GET_PERCENT (buffers, 0));
884 /* also apply the rate estimate when we need to */
885 if (queue->use_rate_estimate)
886 percent = MAX (percent, GET_PERCENT (rate_time, 0));
889 if (queue->is_buffering) {
891 /* if we were buffering see if we reached the high watermark */
892 if (percent >= queue->high_percent)
893 queue->is_buffering = FALSE;
895 /* we were not buffering, check if we need to start buffering if we drop
896 * below the low threshold */
897 if (percent < queue->low_percent) {
898 queue->is_buffering = TRUE;
899 queue->buffering_iteration++;
905 GstBufferingMode mode;
906 gint64 buffering_left = -1;
908 /* scale to high percent so that it becomes the 100% mark */
909 percent = percent * 100 / queue->high_percent;
914 if (percent != queue->buffering_percent) {
915 queue->buffering_percent = percent;
917 if (!QUEUE_IS_USING_QUEUE (queue)) {
918 GstFormat fmt = GST_FORMAT_BYTES;
921 if (QUEUE_IS_USING_RING_BUFFER (queue))
922 mode = GST_BUFFERING_TIMESHIFT;
924 mode = GST_BUFFERING_DOWNLOAD;
926 if (queue->byte_in_rate > 0) {
927 if (gst_pad_query_peer_duration (queue->sinkpad, &fmt, &duration))
929 (gdouble) ((duration -
930 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
932 buffering_left = G_MAXINT64;
935 mode = GST_BUFFERING_STREAM;
938 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
939 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
941 gst_message_set_buffering_stats (message, mode,
942 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
944 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
947 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
954 reset_rate_timer (GstQueue2 * queue)
957 queue->bytes_out = 0;
958 queue->byte_in_rate = 0.0;
959 queue->byte_in_period = 0;
960 queue->byte_out_rate = 0.0;
961 queue->last_in_elapsed = 0.0;
962 queue->last_out_elapsed = 0.0;
963 queue->in_timer_started = FALSE;
964 queue->out_timer_started = FALSE;
967 /* the interval in seconds to recalculate the rate */
968 #define RATE_INTERVAL 0.2
969 /* Tuning for rate estimation. We use a large window for the input rate because
970 * it should be stable when connected to a network. The output rate is less
971 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
972 * therefore adapt more quickly.
973 * However, initial input rate may be subject to a burst, and should therefore
974 * initially also adapt more quickly to changes, and only later on give higher
975 * weight to previous values. */
976 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
977 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
980 update_in_rates (GstQueue2 * queue)
982 gdouble elapsed, period;
983 gdouble byte_in_rate;
985 if (!queue->in_timer_started) {
986 queue->in_timer_started = TRUE;
987 g_timer_start (queue->in_timer);
991 elapsed = g_timer_elapsed (queue->in_timer, NULL);
993 /* recalc after each interval. */
994 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
995 period = elapsed - queue->last_in_elapsed;
997 GST_DEBUG_OBJECT (queue,
998 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
999 period, queue->bytes_in, queue->byte_in_period);
1001 byte_in_rate = queue->bytes_in / period;
1003 if (queue->byte_in_rate == 0.0)
1004 queue->byte_in_rate = byte_in_rate;
1006 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1007 (double) queue->byte_in_period, period);
1009 /* another data point, cap at 16 for long time running average */
1010 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1011 queue->byte_in_period += period;
1013 /* reset the values to calculate rate over the next interval */
1014 queue->last_in_elapsed = elapsed;
1015 queue->bytes_in = 0;
1018 if (queue->byte_in_rate > 0.0) {
1019 queue->cur_level.rate_time =
1020 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1022 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1023 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1027 update_out_rates (GstQueue2 * queue)
1029 gdouble elapsed, period;
1030 gdouble byte_out_rate;
1032 if (!queue->out_timer_started) {
1033 queue->out_timer_started = TRUE;
1034 g_timer_start (queue->out_timer);
1038 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1040 /* recalc after each interval. */
1041 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1042 period = elapsed - queue->last_out_elapsed;
1044 GST_DEBUG_OBJECT (queue,
1045 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1047 byte_out_rate = queue->bytes_out / period;
1049 if (queue->byte_out_rate == 0.0)
1050 queue->byte_out_rate = byte_out_rate;
1052 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1054 /* reset the values to calculate rate over the next interval */
1055 queue->last_out_elapsed = elapsed;
1056 queue->bytes_out = 0;
1058 if (queue->byte_in_rate > 0.0) {
1059 queue->cur_level.rate_time =
1060 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1062 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1063 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1067 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1069 guint64 reading_pos, max_reading_pos;
1072 max_reading_pos = range->max_reading_pos;
1074 max_reading_pos = MAX (max_reading_pos, reading_pos);
1076 GST_DEBUG_OBJECT (queue,
1077 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1078 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1079 range->max_reading_pos = max_reading_pos;
1081 update_cur_level (queue, range);
1085 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1090 GST_QUEUE2_MUTEX_UNLOCK (queue);
1092 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1095 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1096 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1097 GST_SEEK_TYPE_NONE, -1);
1099 res = gst_pad_push_event (queue->sinkpad, event);
1100 GST_QUEUE2_MUTEX_LOCK (queue);
1103 queue->current = add_range (queue, offset);
1108 /* see if there is enough data in the file to read a full buffer */
1110 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1112 GstQueue2Range *range;
1114 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1117 if ((range = find_range (queue, offset))) {
1118 if (queue->current != range) {
1119 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1120 perform_seek_to_offset (queue, range->writing_pos);
1123 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1124 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1126 /* we have a range for offset */
1127 GST_DEBUG_OBJECT (queue,
1128 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1129 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1131 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1134 if (offset + length <= range->writing_pos)
1137 GST_DEBUG_OBJECT (queue,
1138 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1139 (offset + length) - range->writing_pos);
1142 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1143 " len %u", offset, length);
1144 /* we don't have the range, see how far away we are */
1145 if (!queue->is_eos && queue->current) {
1146 /* FIXME, find a good threshold based on the incoming rate. */
1147 guint64 threshold = 1024 * 512;
1149 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1152 distance = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1153 /* don't wait for the complete buffer to fill */
1154 distance = MIN (distance, threshold);
1156 if (offset >= queue->current->offset && offset <=
1157 queue->current->writing_pos + distance) {
1158 GST_INFO_OBJECT (queue,
1159 "requested data is within range, wait for data");
1162 } else if (offset < queue->current->writing_pos + threshold) {
1163 update_cur_pos (queue, queue->current, offset + length);
1164 GST_INFO_OBJECT (queue, "wait for data");
1169 /* too far away, do a seek */
1170 perform_seek_to_offset (queue, offset);
1177 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1178 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1179 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1181 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1185 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1188 guint8 *ring_buffer;
1191 ring_buffer = queue->ring_buffer;
1193 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1196 /* this should not block */
1197 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1199 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1200 res = fread (dst, 1, length, queue->temp_file);
1202 memcpy (dst, ring_buffer + offset, length);
1206 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1208 if (G_UNLIKELY (res < length)) {
1209 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1210 goto could_not_read;
1211 /* check for errors or EOF */
1212 if (ferror (queue->temp_file))
1213 goto could_not_read;
1214 if (feof (queue->temp_file) && length > 0)
1222 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1223 return GST_FLOW_ERROR;
1227 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1228 return GST_FLOW_ERROR;
1232 GST_DEBUG ("non-regular file hits EOS");
1233 return GST_FLOW_UNEXPECTED;
1237 static GstFlowReturn
1238 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1239 GstBuffer ** buffer)
1243 guint64 file_offset;
1244 guint block_length, remaining, read_length;
1249 /* allocate the output buffer of the requested size */
1250 buf = gst_buffer_new_and_alloc (length);
1251 data = GST_BUFFER_DATA (buf);
1253 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1257 rb_size = queue->ring_buffer_max_size;
1260 while (remaining > 0) {
1261 /* configure how much/whether to read */
1262 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1265 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1268 /* calculate how far away the offset is */
1269 if (queue->current->writing_pos > rpos)
1270 level = queue->current->writing_pos - rpos;
1274 GST_DEBUG_OBJECT (queue,
1275 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1276 ", level %" G_GUINT64_FORMAT,
1277 rpos, queue->current->writing_pos, level);
1279 if (level >= rb_size) {
1280 /* we don't have the data but if we have a ring buffer that is full, we
1282 GST_DEBUG_OBJECT (queue,
1283 "ring buffer full, reading ring-buffer-max-size %"
1284 G_GUINT64_FORMAT " bytes", rb_size);
1285 read_length = rb_size;
1286 } else if (queue->is_eos) {
1287 /* won't get any more data so read any data we have */
1289 GST_DEBUG_OBJECT (queue,
1290 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1292 read_length = level;
1294 GST_DEBUG_OBJECT (queue,
1295 "EOS hit and we don't have any requested data");
1296 gst_buffer_unref (buf);
1297 return GST_FLOW_UNEXPECTED;
1302 if (read_length == 0) {
1303 if (QUEUE_IS_USING_RING_BUFFER (queue)
1304 && queue->current->max_reading_pos > rpos) {
1305 /* protect cached data (data between offset and max_reading_pos)
1306 * and update current level */
1307 GST_DEBUG_OBJECT (queue,
1308 "protecting cached data [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1309 "]", rpos, queue->current->max_reading_pos);
1310 queue->current->max_reading_pos = rpos;
1311 update_cur_level (queue, queue->current);
1313 GST_DEBUG_OBJECT (queue, "waiting for add");
1314 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1318 /* we have the requested data so read it */
1319 read_length = remaining;
1322 /* set range reading_pos to actual reading position for this read */
1323 queue->current->reading_pos = rpos;
1325 /* configure how much and from where to read */
1326 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1328 (queue->current->rb_offset + (rpos -
1329 queue->current->offset)) % rb_size;
1330 if (file_offset + read_length > rb_size) {
1331 block_length = rb_size - file_offset;
1333 block_length = read_length;
1337 block_length = read_length;
1340 /* while we still have data to read, we loop */
1341 while (read_length > 0) {
1343 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1345 if (read_return < 0)
1348 file_offset += read_return;
1349 if (QUEUE_IS_USING_RING_BUFFER (queue))
1350 file_offset %= rb_size;
1352 data += read_return;
1353 read_length -= read_return;
1354 block_length = read_length;
1355 remaining -= read_return;
1357 rpos = (queue->current->reading_pos += read_return);
1358 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1360 GST_QUEUE2_SIGNAL_DEL (queue);
1361 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1364 GST_BUFFER_SIZE (buf) = length;
1365 GST_BUFFER_OFFSET (buf) = offset;
1366 GST_BUFFER_OFFSET_END (buf) = offset + length;
1375 GST_DEBUG_OBJECT (queue, "we are flushing");
1376 gst_buffer_unref (buf);
1377 return GST_FLOW_WRONG_STATE;
1381 GST_DEBUG_OBJECT (queue, "we have a read error");
1382 gst_buffer_unref (buf);
1387 /* should be called with QUEUE_LOCK */
1388 static GstMiniObject *
1389 gst_queue2_read_item_from_file (GstQueue2 * queue)
1391 GstMiniObject *item;
1393 if (queue->starting_segment != NULL) {
1394 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1395 queue->starting_segment = NULL;
1399 guint64 reading_pos;
1401 reading_pos = queue->current->reading_pos;
1404 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1409 item = GST_MINI_OBJECT_CAST (buffer);
1411 case GST_FLOW_UNEXPECTED:
1412 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1422 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1423 * the temp filename. */
1425 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1430 if (queue->temp_file)
1431 goto already_opened;
1433 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1435 /* we have two cases:
1436 * - temp_location was set to something !NULL (Deprecated). in this case we
1437 * open the specified filename.
1438 * - temp_template was set, allocate a filename and open that filename
1440 if (!queue->temp_location_set) {
1442 if (queue->temp_template == NULL)
1445 /* make copy of the template, we don't want to change this */
1446 name = g_strdup (queue->temp_template);
1447 fd = g_mkstemp (name);
1449 goto mkstemp_failed;
1451 /* open the file for update/writing */
1452 queue->temp_file = fdopen (fd, "wb+");
1453 /* error creating file */
1454 if (queue->temp_file == NULL)
1457 g_free (queue->temp_location);
1458 queue->temp_location = name;
1460 GST_QUEUE2_MUTEX_UNLOCK (queue);
1462 /* we can't emit the notify with the lock */
1463 g_object_notify (G_OBJECT (queue), "temp-location");
1465 GST_QUEUE2_MUTEX_LOCK (queue);
1467 /* open the file for update/writing, this is deprecated but we still need to
1468 * support it for API/ABI compatibility */
1469 queue->temp_file = g_fopen (queue->temp_location, "wb+");
1470 /* error creating file */
1471 if (queue->temp_file == NULL)
1474 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1481 GST_DEBUG_OBJECT (queue, "temp file was already open");
1486 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1487 (_("No Temp directory specified.")), (NULL));
1492 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1493 (_("Could not create temp file \"%s\"."), queue->temp_template),
1500 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1501 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1510 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1513 if (queue->temp_file == NULL)
1516 GST_DEBUG_OBJECT (queue, "closing temp file");
1518 fflush (queue->temp_file);
1519 fclose (queue->temp_file);
1521 if (queue->temp_remove)
1522 remove (queue->temp_location);
1524 queue->temp_file = NULL;
1525 clean_ranges (queue);
1529 gst_queue2_flush_temp_file (GstQueue2 * queue)
1531 if (queue->temp_file == NULL)
1534 GST_DEBUG_OBJECT (queue, "flushing temp file");
1536 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1540 gst_queue2_locked_flush (GstQueue2 * queue)
1542 if (!QUEUE_IS_USING_QUEUE (queue)) {
1543 if (QUEUE_IS_USING_TEMP_FILE (queue))
1544 gst_queue2_flush_temp_file (queue);
1545 init_ranges (queue);
1547 while (!g_queue_is_empty (&queue->queue)) {
1548 GstMiniObject *data = g_queue_pop_head (&queue->queue);
1550 /* Then lose another reference because we are supposed to destroy that
1551 data when flushing */
1552 gst_mini_object_unref (data);
1555 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1556 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1557 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1558 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1559 queue->sink_tainted = queue->src_tainted = TRUE;
1560 if (queue->starting_segment != NULL)
1561 gst_event_unref (queue->starting_segment);
1562 queue->starting_segment = NULL;
1563 queue->segment_event_received = FALSE;
1565 /* we deleted a lot of something */
1566 GST_QUEUE2_SIGNAL_DEL (queue);
1570 gst_queue2_wait_free_space (GstQueue2 * queue)
1572 /* We make space available if we're "full" according to whatever
1573 * the user defined as "full". */
1574 if (gst_queue2_is_filled (queue)) {
1577 /* pause the timer while we wait. The fact that we are waiting does not mean
1578 * the byterate on the input pad is lower */
1579 if ((started = queue->in_timer_started))
1580 g_timer_stop (queue->in_timer);
1582 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1583 "queue is full, waiting for free space");
1585 /* Wait for space to be available, we could be unlocked because of a flush. */
1586 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1588 while (gst_queue2_is_filled (queue));
1590 /* and continue if we were running before */
1592 g_timer_continue (queue->in_timer);
1599 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1605 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1607 guint8 *data, *ring_buffer;
1608 guint size, rb_size;
1609 guint64 writing_pos, new_writing_pos;
1610 GstQueue2Range *range, *prev, *next;
1612 if (QUEUE_IS_USING_RING_BUFFER (queue))
1613 writing_pos = queue->current->rb_writing_pos;
1615 writing_pos = queue->current->writing_pos;
1616 ring_buffer = queue->ring_buffer;
1617 rb_size = queue->ring_buffer_max_size;
1619 size = GST_BUFFER_SIZE (buffer);
1620 data = GST_BUFFER_DATA (buffer);
1622 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1623 GST_BUFFER_OFFSET (buffer));
1628 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1631 /* calculate the space in the ring buffer not used by data from
1632 * the current range */
1633 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1634 /* wait until there is some free space */
1635 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1637 /* get the amount of space we have */
1638 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1640 /* calculate if we need to split or if we can write the entire
1642 to_write = MIN (size, space);
1644 /* the writing position in the ring buffer after writing (part
1645 * or all of) the buffer */
1646 new_writing_pos = (writing_pos + to_write) % rb_size;
1649 range = queue->ranges;
1651 /* if we need to overwrite data in the ring buffer, we need to
1654 * warning: this code is complicated and includes some
1655 * simplifications - pen, paper and diagrams for the cases
1658 guint64 range_data_start, range_data_end;
1659 GstQueue2Range *range_to_destroy = NULL;
1661 range_data_start = range->rb_offset;
1662 range_data_end = range->rb_writing_pos;
1664 /* handle the special case where the range has no data in it */
1665 if (range->writing_pos == range->offset) {
1666 if (range != queue->current) {
1667 GST_DEBUG_OBJECT (queue,
1668 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1669 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1671 range_to_destroy = range;
1673 prev->next = range->next;
1678 if (range_data_end > range_data_start) {
1679 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1682 if (new_writing_pos > range_data_start) {
1683 if (new_writing_pos >= range_data_end) {
1684 GST_DEBUG_OBJECT (queue,
1685 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1686 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1688 range_to_destroy = range;
1690 prev->next = range->next;
1692 GST_DEBUG_OBJECT (queue,
1693 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1694 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1695 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1696 range->offset + new_writing_pos - range_data_start,
1698 range->offset += (new_writing_pos - range_data_start);
1699 range->rb_offset = new_writing_pos;
1703 guint64 new_wpos_virt = writing_pos + to_write;
1705 if (new_wpos_virt <= range_data_start)
1708 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1709 GST_DEBUG_OBJECT (queue,
1710 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1711 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1713 range_to_destroy = range;
1715 prev->next = range->next;
1717 GST_DEBUG_OBJECT (queue,
1718 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1719 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1720 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1721 range->offset + new_writing_pos - range_data_start,
1723 range->offset += (new_wpos_virt - range_data_start);
1724 range->rb_offset = new_writing_pos;
1729 if (!range_to_destroy)
1732 range = range->next;
1733 if (range_to_destroy) {
1734 if (range_to_destroy == queue->ranges)
1735 queue->ranges = range;
1736 g_slice_free (GstQueue2Range, range_to_destroy);
1737 range_to_destroy = NULL;
1742 new_writing_pos = writing_pos + to_write;
1745 if (QUEUE_IS_USING_TEMP_FILE (queue)
1746 && FSEEK_FILE (queue->temp_file, writing_pos))
1749 if (new_writing_pos > writing_pos) {
1750 GST_INFO_OBJECT (queue,
1751 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1752 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1753 queue->current->writing_pos, queue->current->rb_writing_pos);
1754 /* either not using ring buffer or no wrapping, just write */
1755 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1756 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1759 memcpy (ring_buffer + writing_pos, data, to_write);
1762 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1763 /* try to merge with next range */
1764 while ((next = queue->current->next)) {
1765 GST_INFO_OBJECT (queue,
1766 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1767 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1768 if (new_writing_pos < next->offset)
1771 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1774 /* remove the group, we could choose to not read the data in this range
1775 * again. This would involve us doing a seek to the current writing position
1776 * in the range. FIXME, It would probably make sense to do a seek when there
1777 * is a lot of data in the range we merged with to avoid reading it all
1779 queue->current->next = next->next;
1780 g_slice_free (GstQueue2Range, next);
1782 debug_ranges (queue);
1784 goto update_and_signal;
1788 guint block_one, block_two;
1790 block_one = rb_size - writing_pos;
1791 block_two = to_write - block_one;
1793 if (block_one > 0) {
1794 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1795 /* write data to end of ring buffer */
1796 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1797 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1800 memcpy (ring_buffer + writing_pos, data, block_one);
1804 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1807 if (block_two > 0) {
1808 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1809 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1810 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1813 memcpy (ring_buffer, data + block_one, block_two);
1819 /* update the writing positions */
1821 GST_INFO_OBJECT (queue,
1822 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1823 to_write, writing_pos, size);
1825 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1827 queue->current->writing_pos += to_write;
1828 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1830 queue->current->writing_pos = writing_pos = new_writing_pos;
1832 update_cur_level (queue, queue->current);
1834 /* update the buffering status */
1835 if (queue->use_buffering)
1836 update_buffering (queue);
1838 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1839 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1841 GST_QUEUE2_SIGNAL_ADD (queue);
1849 GST_DEBUG_OBJECT (queue, "we are flushing");
1850 /* FIXME - GST_FLOW_UNEXPECTED ? */
1855 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1862 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1866 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1867 (_("Error while writing to download file.")),
1868 ("%s", g_strerror (errno)));
1875 static GstBufferListItem
1876 buffer_list_create_write (GstBuffer ** buf, guint group, guint idx, gpointer q)
1878 GstQueue2 *queue = q;
1880 GST_TRACE_OBJECT (queue, "writing buffer %u in group %u of size %u bytes",
1881 idx, group, GST_BUFFER_SIZE (*buf));
1883 if (!gst_queue2_create_write (queue, *buf)) {
1884 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1885 return GST_BUFFER_LIST_END;
1888 return GST_BUFFER_LIST_CONTINUE;
1891 static GstBufferListItem
1892 buffer_list_calc_size (GstBuffer ** buf, guint group, guint idx, gpointer data)
1894 guint *p_size = data;
1897 buf_size = GST_BUFFER_SIZE (*buf);
1898 GST_TRACE ("buffer %u in group %u has size %u", idx, group, buf_size);
1899 *p_size += buf_size;
1901 return GST_BUFFER_LIST_CONTINUE;
1904 /* enqueue an item an update the level stats */
1906 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1907 GstQueue2ItemType item_type)
1909 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1913 buffer = GST_BUFFER_CAST (item);
1914 size = GST_BUFFER_SIZE (buffer);
1916 /* add buffer to the statistics */
1917 if (QUEUE_IS_USING_QUEUE (queue)) {
1918 queue->cur_level.buffers++;
1919 queue->cur_level.bytes += size;
1921 queue->bytes_in += size;
1923 /* apply new buffer to segment stats */
1924 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1925 /* update the byterate stats */
1926 update_in_rates (queue);
1928 if (!QUEUE_IS_USING_QUEUE (queue)) {
1929 /* FIXME - check return value? */
1930 gst_queue2_create_write (queue, buffer);
1932 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1933 GstBufferList *buffer_list;
1936 buffer_list = GST_BUFFER_LIST_CAST (item);
1938 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1939 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1941 /* add buffer to the statistics */
1942 if (QUEUE_IS_USING_QUEUE (queue)) {
1943 queue->cur_level.buffers++;
1944 queue->cur_level.bytes += size;
1946 queue->bytes_in += size;
1948 /* apply new buffer to segment stats */
1949 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1951 /* update the byterate stats */
1952 update_in_rates (queue);
1954 if (!QUEUE_IS_USING_QUEUE (queue)) {
1955 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1957 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
1960 event = GST_EVENT_CAST (item);
1962 switch (GST_EVENT_TYPE (event)) {
1964 /* Zero the thresholds, this makes sure the queue is completely
1965 * filled and we can read all data from the queue. */
1966 GST_DEBUG_OBJECT (queue, "we have EOS");
1967 queue->is_eos = TRUE;
1969 case GST_EVENT_NEWSEGMENT:
1970 apply_segment (queue, event, &queue->sink_segment, TRUE);
1971 /* This is our first new segment, we hold it
1972 * as we can't save it on the temp file */
1973 if (!QUEUE_IS_USING_QUEUE (queue)) {
1974 if (queue->segment_event_received)
1975 goto unexpected_event;
1977 queue->segment_event_received = TRUE;
1978 if (queue->starting_segment != NULL)
1979 gst_event_unref (queue->starting_segment);
1980 queue->starting_segment = event;
1983 /* a new segment allows us to accept more buffers if we got UNEXPECTED
1984 * from downstream */
1985 queue->unexpected = FALSE;
1988 if (!QUEUE_IS_USING_QUEUE (queue))
1989 goto unexpected_event;
1993 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1994 item, GST_OBJECT_NAME (queue));
1995 /* we can't really unref since we don't know what it is */
2000 /* update the buffering status */
2001 if (queue->use_buffering)
2002 update_buffering (queue);
2004 if (QUEUE_IS_USING_QUEUE (queue)) {
2005 g_queue_push_tail (&queue->queue, item);
2007 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2010 GST_QUEUE2_SIGNAL_ADD (queue);
2019 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2020 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2021 GST_OBJECT_NAME (queue));
2022 gst_event_unref (GST_EVENT_CAST (item));
2027 /* dequeue an item from the queue and update level stats */
2028 static GstMiniObject *
2029 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2031 GstMiniObject *item;
2033 if (!QUEUE_IS_USING_QUEUE (queue))
2034 item = gst_queue2_read_item_from_file (queue);
2036 item = g_queue_pop_head (&queue->queue);
2041 if (GST_IS_BUFFER (item)) {
2045 buffer = GST_BUFFER_CAST (item);
2046 size = GST_BUFFER_SIZE (buffer);
2047 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2049 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2050 "retrieved buffer %p from queue", buffer);
2052 if (QUEUE_IS_USING_QUEUE (queue)) {
2053 queue->cur_level.buffers--;
2054 queue->cur_level.bytes -= size;
2056 queue->bytes_out += size;
2058 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2059 /* update the byterate stats */
2060 update_out_rates (queue);
2061 /* update the buffering */
2062 if (queue->use_buffering)
2063 update_buffering (queue);
2065 } else if (GST_IS_EVENT (item)) {
2066 GstEvent *event = GST_EVENT_CAST (item);
2068 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2070 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2071 "retrieved event %p from queue", event);
2073 switch (GST_EVENT_TYPE (event)) {
2075 /* queue is empty now that we dequeued the EOS */
2076 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2078 case GST_EVENT_NEWSEGMENT:
2079 apply_segment (queue, event, &queue->src_segment, FALSE);
2084 } else if (GST_IS_BUFFER_LIST (item)) {
2085 GstBufferList *buffer_list;
2088 buffer_list = GST_BUFFER_LIST_CAST (item);
2089 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2090 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2092 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2093 "retrieved buffer list %p from queue", buffer_list);
2095 if (QUEUE_IS_USING_QUEUE (queue)) {
2096 queue->cur_level.buffers--;
2097 queue->cur_level.bytes -= size;
2099 queue->bytes_out += size;
2101 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2102 /* update the byterate stats */
2103 update_out_rates (queue);
2104 /* update the buffering */
2105 if (queue->use_buffering)
2106 update_buffering (queue);
2110 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2111 item, GST_OBJECT_NAME (queue));
2113 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2115 GST_QUEUE2_SIGNAL_DEL (queue);
2122 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2128 gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event)
2132 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2134 switch (GST_EVENT_TYPE (event)) {
2135 case GST_EVENT_FLUSH_START:
2137 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2138 if (QUEUE_IS_USING_QUEUE (queue)) {
2140 gst_pad_push_event (queue->srcpad, event);
2142 /* now unblock the chain function */
2143 GST_QUEUE2_MUTEX_LOCK (queue);
2144 queue->srcresult = GST_FLOW_WRONG_STATE;
2145 queue->sinkresult = GST_FLOW_WRONG_STATE;
2146 /* unblock the loop and chain functions */
2147 GST_QUEUE2_SIGNAL_ADD (queue);
2148 GST_QUEUE2_SIGNAL_DEL (queue);
2149 GST_QUEUE2_MUTEX_UNLOCK (queue);
2151 /* make sure it pauses, this should happen since we sent
2152 * flush_start downstream. */
2153 gst_pad_pause_task (queue->srcpad);
2154 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2156 GST_QUEUE2_MUTEX_LOCK (queue);
2157 /* flush the sink pad */
2158 queue->sinkresult = GST_FLOW_WRONG_STATE;
2159 GST_QUEUE2_SIGNAL_DEL (queue);
2160 GST_QUEUE2_MUTEX_UNLOCK (queue);
2162 gst_event_unref (event);
2166 case GST_EVENT_FLUSH_STOP:
2168 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2170 if (QUEUE_IS_USING_QUEUE (queue)) {
2172 gst_pad_push_event (queue->srcpad, event);
2174 GST_QUEUE2_MUTEX_LOCK (queue);
2175 gst_queue2_locked_flush (queue);
2176 queue->srcresult = GST_FLOW_OK;
2177 queue->sinkresult = GST_FLOW_OK;
2178 queue->is_eos = FALSE;
2179 queue->unexpected = FALSE;
2180 /* reset rate counters */
2181 reset_rate_timer (queue);
2182 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2184 GST_QUEUE2_MUTEX_UNLOCK (queue);
2186 GST_QUEUE2_MUTEX_LOCK (queue);
2187 queue->segment_event_received = FALSE;
2188 queue->is_eos = FALSE;
2189 queue->unexpected = FALSE;
2190 queue->sinkresult = GST_FLOW_OK;
2191 GST_QUEUE2_MUTEX_UNLOCK (queue);
2193 gst_event_unref (event);
2198 if (GST_EVENT_IS_SERIALIZED (event)) {
2199 /* serialized events go in the queue */
2200 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2201 /* refuse more events on EOS */
2204 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2205 GST_QUEUE2_MUTEX_UNLOCK (queue);
2207 /* non-serialized events are passed upstream. */
2208 gst_pad_push_event (queue->srcpad, event);
2218 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2219 GST_QUEUE2_MUTEX_UNLOCK (queue);
2220 gst_event_unref (event);
2225 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2226 GST_QUEUE2_MUTEX_UNLOCK (queue);
2227 gst_event_unref (event);
2233 gst_queue2_is_empty (GstQueue2 * queue)
2235 /* never empty on EOS */
2239 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2240 return queue->current->writing_pos <= queue->current->max_reading_pos;
2242 if (queue->queue.length == 0)
2250 gst_queue2_is_filled (GstQueue2 * queue)
2254 /* always filled on EOS */
2258 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2259 (queue->cur_level.format) >= ((alt_max) ? \
2260 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2262 /* if using a ring buffer we're filled if all ring buffer space is used
2263 * _by the current range_ */
2264 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2265 guint64 rb_size = queue->ring_buffer_max_size;
2266 GST_DEBUG_OBJECT (queue,
2267 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2268 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2269 return CHECK_FILLED (bytes, rb_size);
2272 /* if using file, we're never filled if we don't have EOS */
2273 if (QUEUE_IS_USING_TEMP_FILE (queue))
2276 /* we are never filled when we have no buffers at all */
2277 if (queue->cur_level.buffers == 0)
2280 /* we are filled if one of the current levels exceeds the max */
2281 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2282 || CHECK_FILLED (time, 0);
2284 /* if we need to, use the rate estimate to check against the max time we are
2285 * allowed to queue */
2286 if (queue->use_rate_estimate)
2287 res |= CHECK_FILLED (rate_time, 0);
2293 static GstFlowReturn
2294 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2295 GstMiniObject * item, GstQueue2ItemType item_type)
2297 /* we have to lock the queue since we span threads */
2298 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2299 /* when we received EOS, we refuse more data */
2302 /* when we received unexpected from downstream, refuse more buffers */
2303 if (queue->unexpected)
2304 goto out_unexpected;
2306 if (!gst_queue2_wait_free_space (queue))
2309 /* put buffer in queue now */
2310 gst_queue2_locked_enqueue (queue, item, item_type);
2311 GST_QUEUE2_MUTEX_UNLOCK (queue);
2315 /* special conditions */
2318 GstFlowReturn ret = queue->sinkresult;
2320 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2321 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2322 GST_QUEUE2_MUTEX_UNLOCK (queue);
2323 gst_mini_object_unref (item);
2329 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2330 GST_QUEUE2_MUTEX_UNLOCK (queue);
2331 gst_mini_object_unref (item);
2333 return GST_FLOW_UNEXPECTED;
2337 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2338 "exit because we received UNEXPECTED");
2339 GST_QUEUE2_MUTEX_UNLOCK (queue);
2340 gst_mini_object_unref (item);
2342 return GST_FLOW_UNEXPECTED;
2346 static GstFlowReturn
2347 gst_queue2_chain (GstPad * pad, GstBuffer * buffer)
2351 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2353 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2354 "received buffer %p of size %d, time %" GST_TIME_FORMAT ", duration %"
2355 GST_TIME_FORMAT, buffer, GST_BUFFER_SIZE (buffer),
2356 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2357 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2359 return gst_queue2_chain_buffer_or_buffer_list (queue,
2360 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2363 static GstFlowReturn
2364 gst_queue2_chain_list (GstPad * pad, GstBufferList * buffer_list)
2368 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2370 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2371 "received buffer list %p", buffer_list);
2373 return gst_queue2_chain_buffer_or_buffer_list (queue,
2374 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2377 static GstMiniObject *
2378 gst_queue2_dequeue_on_unexpected (GstQueue2 * queue,
2379 GstQueue2ItemType * item_type)
2381 GstMiniObject *data;
2383 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got UNEXPECTED from downstream");
2385 /* stop pushing buffers, we dequeue all items until we see an item that we
2386 * can push again, which is EOS or NEWSEGMENT. If there is nothing in the
2387 * queue we can push, we set a flag to make the sinkpad refuse more
2388 * buffers with an UNEXPECTED return value until we receive something
2389 * pushable again or we get flushed. */
2390 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2391 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2392 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2393 "dropping UNEXPECTED buffer %p", data);
2394 gst_buffer_unref (GST_BUFFER_CAST (data));
2395 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2396 GstEvent *event = GST_EVENT_CAST (data);
2397 GstEventType type = GST_EVENT_TYPE (event);
2399 if (type == GST_EVENT_EOS || type == GST_EVENT_NEWSEGMENT) {
2400 /* we found a pushable item in the queue, push it out */
2401 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2402 "pushing pushable event %s after UNEXPECTED",
2403 GST_EVENT_TYPE_NAME (event));
2406 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2407 "dropping UNEXPECTED event %p", event);
2408 gst_event_unref (event);
2409 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2410 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2411 "dropping UNEXPECTED buffer list %p", data);
2412 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2415 /* no more items in the queue. Set the unexpected flag so that upstream
2416 * make us refuse any more buffers on the sinkpad. Since we will still
2417 * accept EOS and NEWSEGMENT we return _FLOW_OK to the caller so that the
2418 * task function does not shut down. */
2419 queue->unexpected = TRUE;
2423 /* dequeue an item from the queue an push it downstream. This functions returns
2424 * the result of the push. */
2425 static GstFlowReturn
2426 gst_queue2_push_one (GstQueue2 * queue)
2428 GstFlowReturn result = GST_FLOW_OK;
2429 GstMiniObject *data;
2430 GstQueue2ItemType item_type;
2432 data = gst_queue2_locked_dequeue (queue, &item_type);
2437 GST_QUEUE2_MUTEX_UNLOCK (queue);
2439 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2443 buffer = GST_BUFFER_CAST (data);
2444 caps = GST_BUFFER_CAPS (buffer);
2446 /* set caps before pushing the buffer so that core does not try to do
2447 * something fancy to check if this is possible. */
2448 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2449 gst_pad_set_caps (queue->srcpad, caps);
2451 result = gst_pad_push (queue->srcpad, buffer);
2453 /* need to check for srcresult here as well */
2454 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2455 if (result == GST_FLOW_UNEXPECTED) {
2456 data = gst_queue2_dequeue_on_unexpected (queue, &item_type);
2459 /* Since we will still accept EOS and NEWSEGMENT we return _FLOW_OK
2460 * to the caller so that the task function does not shut down */
2461 result = GST_FLOW_OK;
2463 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2464 GstEvent *event = GST_EVENT_CAST (data);
2465 GstEventType type = GST_EVENT_TYPE (event);
2467 gst_pad_push_event (queue->srcpad, event);
2469 /* if we're EOS, return UNEXPECTED so that the task pauses. */
2470 if (type == GST_EVENT_EOS) {
2471 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2472 "pushed EOS event %p, return UNEXPECTED", event);
2473 result = GST_FLOW_UNEXPECTED;
2476 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2477 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2478 GstBufferList *buffer_list;
2479 GstBuffer *first_buf;
2482 buffer_list = GST_BUFFER_LIST_CAST (data);
2484 first_buf = gst_buffer_list_get (buffer_list, 0, 0);
2485 caps = (first_buf != NULL) ? GST_BUFFER_CAPS (first_buf) : NULL;
2487 /* set caps before pushing the buffer so that core does not try to do
2488 * something fancy to check if this is possible. */
2489 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2490 gst_pad_set_caps (queue->srcpad, caps);
2492 result = gst_pad_push_list (queue->srcpad, buffer_list);
2494 /* need to check for srcresult here as well */
2495 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2496 if (result == GST_FLOW_UNEXPECTED) {
2497 data = gst_queue2_dequeue_on_unexpected (queue, &item_type);
2500 /* Since we will still accept EOS and NEWSEGMENT we return _FLOW_OK
2501 * to the caller so that the task function does not shut down */
2502 result = GST_FLOW_OK;
2510 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2511 "exit because we have no item in the queue");
2512 return GST_FLOW_ERROR;
2516 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2517 return GST_FLOW_WRONG_STATE;
2521 /* called repeatedly with @pad as the source pad. This function should push out
2522 * data to the peer element. */
2524 gst_queue2_loop (GstPad * pad)
2529 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2531 /* have to lock for thread-safety */
2532 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2534 if (gst_queue2_is_empty (queue)) {
2537 /* pause the timer while we wait. The fact that we are waiting does not mean
2538 * the byterate on the output pad is lower */
2539 if ((started = queue->out_timer_started))
2540 g_timer_stop (queue->out_timer);
2542 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2543 "queue is empty, waiting for new data");
2545 /* Wait for data to be available, we could be unlocked because of a flush. */
2546 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2548 while (gst_queue2_is_empty (queue));
2550 /* and continue if we were running before */
2552 g_timer_continue (queue->out_timer);
2554 ret = gst_queue2_push_one (queue);
2555 queue->srcresult = ret;
2556 queue->sinkresult = ret;
2557 if (ret != GST_FLOW_OK)
2560 GST_QUEUE2_MUTEX_UNLOCK (queue);
2567 gboolean eos = queue->is_eos;
2568 GstFlowReturn ret = queue->srcresult;
2570 gst_pad_pause_task (queue->srcpad);
2571 GST_QUEUE2_MUTEX_UNLOCK (queue);
2572 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2573 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2574 /* let app know about us giving up if upstream is not expected to do so */
2575 /* UNEXPECTED is already taken care of elsewhere */
2576 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED)) {
2577 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2578 (_("Internal data flow error.")),
2579 ("streaming task paused, reason %s (%d)",
2580 gst_flow_get_name (ret), ret));
2581 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2588 gst_queue2_handle_src_event (GstPad * pad, GstEvent * event)
2590 gboolean res = TRUE;
2591 GstQueue2 *queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2593 if (G_UNLIKELY (queue == NULL)) {
2594 gst_event_unref (event);
2597 #ifndef GST_DISABLE_GST_DEBUG
2598 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2599 event, GST_EVENT_TYPE_NAME (event));
2602 switch (GST_EVENT_TYPE (event)) {
2603 case GST_EVENT_FLUSH_START:
2604 if (QUEUE_IS_USING_QUEUE (queue)) {
2605 /* just forward upstream */
2606 res = gst_pad_push_event (queue->sinkpad, event);
2608 /* now unblock the getrange function */
2609 GST_QUEUE2_MUTEX_LOCK (queue);
2610 GST_DEBUG_OBJECT (queue, "flushing");
2611 queue->srcresult = GST_FLOW_WRONG_STATE;
2612 GST_QUEUE2_SIGNAL_ADD (queue);
2613 GST_QUEUE2_MUTEX_UNLOCK (queue);
2615 /* when using a temp file, we eat the event */
2617 gst_event_unref (event);
2620 case GST_EVENT_FLUSH_STOP:
2621 if (QUEUE_IS_USING_QUEUE (queue)) {
2622 /* just forward upstream */
2623 res = gst_pad_push_event (queue->sinkpad, event);
2625 /* now unblock the getrange function */
2626 GST_QUEUE2_MUTEX_LOCK (queue);
2627 queue->srcresult = GST_FLOW_OK;
2628 if (queue->current) {
2629 /* forget the highest read offset, we'll calculate a new one when we
2630 * get the next getrange request. We need to do this in order to reset
2631 * the buffering percentage */
2632 queue->current->max_reading_pos = 0;
2634 GST_QUEUE2_MUTEX_UNLOCK (queue);
2636 /* when using a temp file, we eat the event */
2638 gst_event_unref (event);
2642 res = gst_pad_push_event (queue->sinkpad, event);
2646 gst_object_unref (queue);
2651 gst_queue2_peer_query (GstQueue2 * queue, GstPad * pad, GstQuery * query)
2653 gboolean ret = FALSE;
2656 if ((peer = gst_pad_get_peer (pad))) {
2657 ret = gst_pad_query (peer, query);
2658 gst_object_unref (peer);
2664 gst_queue2_handle_src_query (GstPad * pad, GstQuery * query)
2668 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2669 if (G_UNLIKELY (queue == NULL))
2672 switch (GST_QUERY_TYPE (query)) {
2673 case GST_QUERY_POSITION:
2678 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2681 /* get peer position */
2682 gst_query_parse_position (query, &format, &peer_pos);
2684 /* FIXME: this code assumes that there's no discont in the queue */
2686 case GST_FORMAT_BYTES:
2687 peer_pos -= queue->cur_level.bytes;
2689 case GST_FORMAT_TIME:
2690 peer_pos -= queue->cur_level.time;
2693 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2694 "know how to adjust value", gst_format_get_name (format));
2697 /* set updated position */
2698 gst_query_set_position (query, format, peer_pos);
2701 case GST_QUERY_DURATION:
2703 GST_DEBUG_OBJECT (queue, "doing peer query");
2705 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2708 GST_DEBUG_OBJECT (queue, "peer query success");
2711 case GST_QUERY_BUFFERING:
2715 GST_DEBUG_OBJECT (queue, "query buffering");
2717 /* FIXME - is this condition correct? what should ring buffer do? */
2718 if (QUEUE_IS_USING_QUEUE (queue)) {
2719 /* no temp file, just forward to the peer */
2720 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2722 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2724 gint64 start, stop, range_start, range_stop;
2725 guint64 writing_pos;
2727 gint64 estimated_total, buffering_left;
2730 gboolean peer_res, is_buffering, is_eos;
2731 gdouble byte_in_rate, byte_out_rate;
2732 GstQueue2Range *queued_ranges;
2734 /* we need a current download region */
2735 if (queue->current == NULL)
2738 writing_pos = queue->current->writing_pos;
2739 byte_in_rate = queue->byte_in_rate;
2740 byte_out_rate = queue->byte_out_rate;
2741 is_buffering = queue->is_buffering;
2742 is_eos = queue->is_eos;
2743 percent = queue->buffering_percent;
2746 /* we're EOS, we know the duration in bytes now */
2748 duration = writing_pos;
2750 /* get duration of upstream in bytes */
2751 peer_fmt = GST_FORMAT_BYTES;
2752 peer_res = gst_pad_query_peer_duration (queue->sinkpad, &peer_fmt,
2756 /* calculate remaining and total download time */
2757 if (peer_res && byte_in_rate > 0.0) {
2758 estimated_total = (duration * 1000) / byte_in_rate;
2759 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2761 estimated_total = -1;
2762 buffering_left = -1;
2764 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2765 G_GINT64_FORMAT, estimated_total, buffering_left);
2767 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2770 case GST_FORMAT_PERCENT:
2771 /* we need duration */
2775 GST_DEBUG_OBJECT (queue,
2776 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2777 duration, writing_pos);
2780 /* get our available data relative to the duration */
2782 stop = GST_FORMAT_PERCENT_MAX * writing_pos / duration;
2786 case GST_FORMAT_BYTES:
2796 /* fill out the buffered ranges */
2797 for (queued_ranges = queue->ranges; queued_ranges;
2798 queued_ranges = queued_ranges->next) {
2800 case GST_FORMAT_PERCENT:
2801 if (duration == -1) {
2806 range_start = 100 * queued_ranges->offset / duration;
2807 range_stop = 100 * queued_ranges->writing_pos / duration;
2809 case GST_FORMAT_BYTES:
2810 range_start = queued_ranges->offset;
2811 range_stop = queued_ranges->writing_pos;
2818 if (range_start == range_stop)
2820 GST_DEBUG_OBJECT (queue,
2821 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2822 G_GINT64_FORMAT, range_start, range_stop);
2823 gst_query_add_buffering_range (query, range_start, range_stop);
2826 gst_query_set_buffering_percent (query, is_buffering, percent);
2827 gst_query_set_buffering_range (query, format, start, stop,
2829 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2830 byte_in_rate, byte_out_rate, buffering_left);
2835 /* peer handled other queries */
2836 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2841 gst_object_unref (queue);
2847 GST_DEBUG_OBJECT (queue, "failed peer query");
2848 gst_object_unref (queue);
2854 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2856 /* simply forward to the srcpad query function */
2857 return gst_queue2_handle_src_query (GST_QUEUE2_CAST (element)->srcpad, query);
2861 gst_queue2_update_upstream_size (GstQueue2 * queue)
2863 GstFormat fmt = GST_FORMAT_BYTES;
2864 gint64 upstream_size = -1;
2866 if (gst_pad_query_peer_duration (queue->sinkpad, &fmt, &upstream_size)) {
2867 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2868 queue->upstream_size = upstream_size;
2872 static GstFlowReturn
2873 gst_queue2_get_range (GstPad * pad, guint64 offset, guint length,
2874 GstBuffer ** buffer)
2879 queue = GST_QUEUE2_CAST (gst_pad_get_parent (pad));
2881 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2882 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2883 offset = (offset == -1) ? queue->current->reading_pos : offset;
2885 GST_DEBUG_OBJECT (queue,
2886 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2888 /* catch any reads beyond the size of the file here to make sure queue2
2889 * doesn't send seek events beyond the size of the file upstream, since
2890 * that would confuse elements such as souphttpsrc and/or http servers.
2891 * Demuxers often just loop until EOS at the end of the file to figure out
2892 * when they've read all the end-headers or index chunks. */
2893 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2894 gst_queue2_update_upstream_size (queue);
2895 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2896 goto out_unexpected;
2899 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2900 gst_queue2_update_upstream_size (queue);
2901 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2902 length = queue->upstream_size - offset;
2903 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2907 /* FIXME - function will block when the range is not yet available */
2908 ret = gst_queue2_create_read (queue, offset, length, buffer);
2909 GST_QUEUE2_MUTEX_UNLOCK (queue);
2911 gst_object_unref (queue);
2918 ret = queue->srcresult;
2920 GST_DEBUG_OBJECT (queue, "we are flushing");
2921 GST_QUEUE2_MUTEX_UNLOCK (queue);
2922 gst_object_unref (queue);
2927 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2928 GST_QUEUE2_MUTEX_UNLOCK (queue);
2929 gst_object_unref (queue);
2930 return GST_FLOW_UNEXPECTED;
2935 gst_queue2_src_checkgetrange_function (GstPad * pad)
2940 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2942 /* we can operate in pull mode when we are using a tempfile */
2943 ret = !QUEUE_IS_USING_QUEUE (queue);
2945 gst_object_unref (GST_OBJECT (queue));
2950 /* sink currently only operates in push mode */
2952 gst_queue2_sink_activate_push (GstPad * pad, gboolean active)
2954 gboolean result = TRUE;
2957 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2960 GST_QUEUE2_MUTEX_LOCK (queue);
2961 GST_DEBUG_OBJECT (queue, "activating push mode");
2962 queue->srcresult = GST_FLOW_OK;
2963 queue->sinkresult = GST_FLOW_OK;
2964 queue->is_eos = FALSE;
2965 queue->unexpected = FALSE;
2966 reset_rate_timer (queue);
2967 GST_QUEUE2_MUTEX_UNLOCK (queue);
2969 /* unblock chain function */
2970 GST_QUEUE2_MUTEX_LOCK (queue);
2971 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2972 queue->srcresult = GST_FLOW_WRONG_STATE;
2973 queue->sinkresult = GST_FLOW_WRONG_STATE;
2974 gst_queue2_locked_flush (queue);
2975 GST_QUEUE2_MUTEX_UNLOCK (queue);
2978 gst_object_unref (queue);
2983 /* src operating in push mode, we start a task on the source pad that pushes out
2984 * buffers from the queue */
2986 gst_queue2_src_activate_push (GstPad * pad, gboolean active)
2988 gboolean result = FALSE;
2991 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2994 GST_QUEUE2_MUTEX_LOCK (queue);
2995 GST_DEBUG_OBJECT (queue, "activating push mode");
2996 queue->srcresult = GST_FLOW_OK;
2997 queue->sinkresult = GST_FLOW_OK;
2998 queue->is_eos = FALSE;
2999 queue->unexpected = FALSE;
3000 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
3001 GST_QUEUE2_MUTEX_UNLOCK (queue);
3003 /* unblock loop function */
3004 GST_QUEUE2_MUTEX_LOCK (queue);
3005 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3006 queue->srcresult = GST_FLOW_WRONG_STATE;
3007 queue->sinkresult = GST_FLOW_WRONG_STATE;
3008 /* the item add signal will unblock */
3009 GST_QUEUE2_SIGNAL_ADD (queue);
3010 GST_QUEUE2_MUTEX_UNLOCK (queue);
3012 /* step 2, make sure streaming finishes */
3013 result = gst_pad_stop_task (pad);
3016 gst_object_unref (queue);
3021 /* pull mode, downstream will call our getrange function */
3023 gst_queue2_src_activate_pull (GstPad * pad, gboolean active)
3028 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
3031 GST_QUEUE2_MUTEX_LOCK (queue);
3032 if (!QUEUE_IS_USING_QUEUE (queue)) {
3033 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3034 /* open the temp file now */
3035 result = gst_queue2_open_temp_location_file (queue);
3036 } else if (!queue->ring_buffer) {
3037 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3038 result = ! !queue->ring_buffer;
3043 GST_DEBUG_OBJECT (queue, "activating pull mode");
3044 init_ranges (queue);
3045 queue->srcresult = GST_FLOW_OK;
3046 queue->sinkresult = GST_FLOW_OK;
3047 queue->is_eos = FALSE;
3048 queue->unexpected = FALSE;
3049 queue->upstream_size = 0;
3051 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3052 /* this is not allowed, we cannot operate in pull mode without a temp
3054 queue->srcresult = GST_FLOW_WRONG_STATE;
3055 queue->sinkresult = GST_FLOW_WRONG_STATE;
3058 GST_QUEUE2_MUTEX_UNLOCK (queue);
3060 GST_QUEUE2_MUTEX_LOCK (queue);
3061 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3062 queue->srcresult = GST_FLOW_WRONG_STATE;
3063 queue->sinkresult = GST_FLOW_WRONG_STATE;
3064 /* this will unlock getrange */
3065 GST_QUEUE2_SIGNAL_ADD (queue);
3067 GST_QUEUE2_MUTEX_UNLOCK (queue);
3069 gst_object_unref (queue);
3074 static GstStateChangeReturn
3075 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3078 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3080 queue = GST_QUEUE2 (element);
3082 switch (transition) {
3083 case GST_STATE_CHANGE_NULL_TO_READY:
3085 case GST_STATE_CHANGE_READY_TO_PAUSED:
3086 GST_QUEUE2_MUTEX_LOCK (queue);
3087 if (!QUEUE_IS_USING_QUEUE (queue)) {
3088 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3089 if (!gst_queue2_open_temp_location_file (queue))
3090 ret = GST_STATE_CHANGE_FAILURE;
3092 if (queue->ring_buffer) {
3093 g_free (queue->ring_buffer);
3094 queue->ring_buffer = NULL;
3096 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3097 ret = GST_STATE_CHANGE_FAILURE;
3099 init_ranges (queue);
3101 queue->segment_event_received = FALSE;
3102 queue->starting_segment = NULL;
3103 GST_QUEUE2_MUTEX_UNLOCK (queue);
3105 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3111 if (ret == GST_STATE_CHANGE_FAILURE)
3114 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3116 if (ret == GST_STATE_CHANGE_FAILURE)
3119 switch (transition) {
3120 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3122 case GST_STATE_CHANGE_PAUSED_TO_READY:
3123 GST_QUEUE2_MUTEX_LOCK (queue);
3124 if (!QUEUE_IS_USING_QUEUE (queue)) {
3125 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3126 gst_queue2_close_temp_location_file (queue);
3127 } else if (queue->ring_buffer) {
3128 g_free (queue->ring_buffer);
3129 queue->ring_buffer = NULL;
3131 clean_ranges (queue);
3133 if (queue->starting_segment != NULL) {
3134 gst_event_unref (queue->starting_segment);
3135 queue->starting_segment = NULL;
3137 GST_QUEUE2_MUTEX_UNLOCK (queue);
3139 case GST_STATE_CHANGE_READY_TO_NULL:
3148 /* changing the capacity of the queue must wake up
3149 * the _chain function, it might have more room now
3150 * to store the buffer/event in the queue */
3151 #define QUEUE_CAPACITY_CHANGE(q)\
3152 GST_QUEUE2_SIGNAL_DEL (queue);
3154 /* Changing the minimum required fill level must
3155 * wake up the _loop function as it might now
3156 * be able to preceed.
3158 #define QUEUE_THRESHOLD_CHANGE(q)\
3159 GST_QUEUE2_SIGNAL_ADD (queue);
3162 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3166 /* the element must be stopped in order to do this */
3167 GST_OBJECT_LOCK (queue);
3168 state = GST_STATE (queue);
3169 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3171 GST_OBJECT_UNLOCK (queue);
3173 /* set new location */
3174 g_free (queue->temp_template);
3175 queue->temp_template = g_strdup (template);
3182 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3183 GST_OBJECT_UNLOCK (queue);
3188 gst_queue2_set_property (GObject * object,
3189 guint prop_id, const GValue * value, GParamSpec * pspec)
3191 GstQueue2 *queue = GST_QUEUE2 (object);
3193 /* someone could change levels here, and since this
3194 * affects the get/put funcs, we need to lock for safety. */
3195 GST_QUEUE2_MUTEX_LOCK (queue);
3198 case PROP_MAX_SIZE_BYTES:
3199 queue->max_level.bytes = g_value_get_uint (value);
3200 QUEUE_CAPACITY_CHANGE (queue);
3202 case PROP_MAX_SIZE_BUFFERS:
3203 queue->max_level.buffers = g_value_get_uint (value);
3204 QUEUE_CAPACITY_CHANGE (queue);
3206 case PROP_MAX_SIZE_TIME:
3207 queue->max_level.time = g_value_get_uint64 (value);
3208 /* set rate_time to the same value. We use an extra field in the level
3209 * structure so that we can easily access and compare it */
3210 queue->max_level.rate_time = queue->max_level.time;
3211 QUEUE_CAPACITY_CHANGE (queue);
3213 case PROP_USE_BUFFERING:
3214 queue->use_buffering = g_value_get_boolean (value);
3216 case PROP_USE_RATE_ESTIMATE:
3217 queue->use_rate_estimate = g_value_get_boolean (value);
3219 case PROP_LOW_PERCENT:
3220 queue->low_percent = g_value_get_int (value);
3222 case PROP_HIGH_PERCENT:
3223 queue->high_percent = g_value_get_int (value);
3225 case PROP_TEMP_TEMPLATE:
3226 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3228 case PROP_TEMP_LOCATION:
3229 g_free (queue->temp_location);
3230 queue->temp_location = g_value_dup_string (value);
3231 /* you can set the property back to NULL to make it use the temp-tmpl
3233 queue->temp_location_set = queue->temp_location != NULL;
3235 case PROP_TEMP_REMOVE:
3236 queue->temp_remove = g_value_get_boolean (value);
3238 case PROP_RING_BUFFER_MAX_SIZE:
3239 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3246 GST_QUEUE2_MUTEX_UNLOCK (queue);
3250 gst_queue2_get_property (GObject * object,
3251 guint prop_id, GValue * value, GParamSpec * pspec)
3253 GstQueue2 *queue = GST_QUEUE2 (object);
3255 GST_QUEUE2_MUTEX_LOCK (queue);
3258 case PROP_CUR_LEVEL_BYTES:
3259 g_value_set_uint (value, queue->cur_level.bytes);
3261 case PROP_CUR_LEVEL_BUFFERS:
3262 g_value_set_uint (value, queue->cur_level.buffers);
3264 case PROP_CUR_LEVEL_TIME:
3265 g_value_set_uint64 (value, queue->cur_level.time);
3267 case PROP_MAX_SIZE_BYTES:
3268 g_value_set_uint (value, queue->max_level.bytes);
3270 case PROP_MAX_SIZE_BUFFERS:
3271 g_value_set_uint (value, queue->max_level.buffers);
3273 case PROP_MAX_SIZE_TIME:
3274 g_value_set_uint64 (value, queue->max_level.time);
3276 case PROP_USE_BUFFERING:
3277 g_value_set_boolean (value, queue->use_buffering);
3279 case PROP_USE_RATE_ESTIMATE:
3280 g_value_set_boolean (value, queue->use_rate_estimate);
3282 case PROP_LOW_PERCENT:
3283 g_value_set_int (value, queue->low_percent);
3285 case PROP_HIGH_PERCENT:
3286 g_value_set_int (value, queue->high_percent);
3288 case PROP_TEMP_TEMPLATE:
3289 g_value_set_string (value, queue->temp_template);
3291 case PROP_TEMP_LOCATION:
3292 g_value_set_string (value, queue->temp_location);
3294 case PROP_TEMP_REMOVE:
3295 g_value_set_boolean (value, queue->temp_remove);
3297 case PROP_RING_BUFFER_MAX_SIZE:
3298 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3305 GST_QUEUE2_MUTEX_UNLOCK (queue);