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"
70 #include <io.h> /* lseek, open, close, read */
72 #define lseek _lseeki64
79 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
84 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
89 GST_DEBUG_CATEGORY_STATIC (queue_debug);
90 #define GST_CAT_DEFAULT (queue_debug)
91 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
99 #define DEFAULT_BUFFER_SIZE 4096
100 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_location_set || (queue)->temp_template != NULL)
101 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
102 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
104 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
106 /* default property values */
107 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
108 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
109 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
110 #define DEFAULT_USE_BUFFERING FALSE
111 #define DEFAULT_USE_RATE_ESTIMATE TRUE
112 #define DEFAULT_LOW_PERCENT 10
113 #define DEFAULT_HIGH_PERCENT 99
114 #define DEFAULT_TEMP_REMOVE TRUE
115 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
120 PROP_CUR_LEVEL_BUFFERS,
121 PROP_CUR_LEVEL_BYTES,
123 PROP_MAX_SIZE_BUFFERS,
127 PROP_USE_RATE_ESTIMATE,
133 PROP_RING_BUFFER_MAX_SIZE,
137 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
144 #define STATUS(queue, pad, msg) \
145 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
146 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
147 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
148 " ns, %"G_GUINT64_FORMAT" items", \
149 GST_DEBUG_PAD_NAME (pad), \
150 queue->cur_level.buffers, \
151 queue->max_level.buffers, \
152 queue->cur_level.bytes, \
153 queue->max_level.bytes, \
154 queue->cur_level.time, \
155 queue->max_level.time, \
156 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
157 queue->current->writing_pos - queue->current->max_reading_pos : \
158 queue->queue->length))
160 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
161 g_mutex_lock (q->qlock); \
164 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
165 GST_QUEUE2_MUTEX_LOCK (q); \
166 if (res != GST_FLOW_OK) \
170 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
171 g_mutex_unlock (q->qlock); \
174 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
175 STATUS (queue, q->sinkpad, "wait for DEL"); \
176 q->waiting_del = TRUE; \
177 g_cond_wait (q->item_del, queue->qlock); \
178 q->waiting_del = FALSE; \
179 if (res != GST_FLOW_OK) { \
180 STATUS (queue, q->srcpad, "received DEL wakeup"); \
183 STATUS (queue, q->sinkpad, "received DEL"); \
186 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
187 STATUS (queue, q->srcpad, "wait for ADD"); \
188 q->waiting_add = TRUE; \
189 g_cond_wait (q->item_add, q->qlock); \
190 q->waiting_add = FALSE; \
191 if (res != GST_FLOW_OK) { \
192 STATUS (queue, q->srcpad, "received ADD wakeup"); \
195 STATUS (queue, q->srcpad, "received ADD"); \
198 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
199 if (q->waiting_del) { \
200 STATUS (q, q->srcpad, "signal DEL"); \
201 g_cond_signal (q->item_del); \
205 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
206 if (q->waiting_add) { \
207 STATUS (q, q->sinkpad, "signal ADD"); \
208 g_cond_signal (q->item_add); \
213 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
214 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
215 "dataflow inside the queue element");
216 #define gst_queue2_parent_class parent_class
217 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
219 static void gst_queue2_finalize (GObject * object);
221 static void gst_queue2_set_property (GObject * object,
222 guint prop_id, const GValue * value, GParamSpec * pspec);
223 static void gst_queue2_get_property (GObject * object,
224 guint prop_id, GValue * value, GParamSpec * pspec);
226 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstBuffer * buffer);
227 static GstFlowReturn gst_queue2_bufferalloc (GstPad * pad, guint64 offset,
228 guint size, GstCaps * caps, GstBuffer ** buf);
229 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
230 static void gst_queue2_loop (GstPad * pad);
232 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event);
234 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstEvent * event);
235 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstQuery * query);
236 static gboolean gst_queue2_handle_query (GstElement * element,
239 static GstCaps *gst_queue2_getcaps (GstPad * pad);
240 static gboolean gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps);
242 static GstFlowReturn gst_queue2_get_range (GstPad * pad, guint64 offset,
243 guint length, GstBuffer ** buffer);
244 static gboolean gst_queue2_src_checkgetrange_function (GstPad * pad);
246 static gboolean gst_queue2_src_activate_pull (GstPad * pad, gboolean active);
247 static gboolean gst_queue2_src_activate_push (GstPad * pad, gboolean active);
248 static gboolean gst_queue2_sink_activate_push (GstPad * pad, gboolean active);
249 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
250 GstStateChange transition);
252 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
253 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
255 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
257 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
260 gst_queue2_class_init (GstQueue2Class * klass)
262 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
263 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
265 gobject_class->set_property = gst_queue2_set_property;
266 gobject_class->get_property = gst_queue2_get_property;
269 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
270 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
271 "Current amount of data in the queue (bytes)",
272 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
273 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
274 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
275 "Current number of buffers in the queue",
276 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
277 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
278 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
279 "Current amount of data in the queue (in ns)",
280 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
282 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
283 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
284 "Max. amount of data in the queue (bytes, 0=disable)",
285 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
286 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
288 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
289 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
290 DEFAULT_MAX_SIZE_BUFFERS,
291 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
293 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
294 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
295 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
298 g_param_spec_boolean ("use-buffering", "Use buffering",
299 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
300 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
302 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
303 "Estimate the bitrate of the stream to calculate time level",
304 DEFAULT_USE_RATE_ESTIMATE,
305 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
307 g_param_spec_int ("low-percent", "Low percent",
308 "Low threshold for buffering to start. Only used if use-buffering is True",
309 0, 100, DEFAULT_LOW_PERCENT,
310 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
312 g_param_spec_int ("high-percent", "High percent",
313 "High threshold for buffering to finish. Only used if use-buffering is True",
314 0, 100, DEFAULT_HIGH_PERCENT,
315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
318 g_param_spec_string ("temp-template", "Temporary File Template",
319 "File template to store temporary files in, should contain directory "
320 "and XXXXXX. (NULL == disabled)",
321 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
324 g_param_spec_string ("temp-location", "Temporary File Location",
325 "Location to store temporary files in (Deprecated: Only read this "
326 "property, use temp-template to configure the name template)",
327 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 * GstQueue2:temp-remove
332 * When temp-template is set, remove the temporary file when going to READY.
336 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
337 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
338 "Remove the temp-location after use",
339 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 * GstQueue2:ring-buffer-max-size
344 * The maximum size of the ring buffer in bytes. If set to 0, the ring
345 * buffer is disabled. Default 0.
349 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
350 g_param_spec_uint64 ("ring-buffer-max-size",
351 "Max. ring buffer size (bytes)",
352 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
353 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
354 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 /* set several parent class virtual functions */
357 gobject_class->finalize = gst_queue2_finalize;
359 gst_element_class_add_pad_template (gstelement_class,
360 gst_static_pad_template_get (&srctemplate));
361 gst_element_class_add_pad_template (gstelement_class,
362 gst_static_pad_template_get (&sinktemplate));
364 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
367 "Erik Walthinsen <omega@cse.ogi.edu>, "
368 "Wim Taymans <wim.taymans@gmail.com>");
370 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
371 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
375 gst_queue2_init (GstQueue2 * queue)
377 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
379 gst_pad_set_chain_function (queue->sinkpad,
380 GST_DEBUG_FUNCPTR (gst_queue2_chain));
381 gst_pad_set_activatepush_function (queue->sinkpad,
382 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_push));
383 gst_pad_set_event_function (queue->sinkpad,
384 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
385 gst_pad_set_getcaps_function (queue->sinkpad,
386 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
387 gst_pad_set_acceptcaps_function (queue->sinkpad,
388 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
389 gst_pad_set_bufferalloc_function (queue->sinkpad,
390 GST_DEBUG_FUNCPTR (gst_queue2_bufferalloc));
391 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
393 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
395 gst_pad_set_activatepull_function (queue->srcpad,
396 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_pull));
397 gst_pad_set_activatepush_function (queue->srcpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_push));
399 gst_pad_set_getrange_function (queue->srcpad,
400 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
401 gst_pad_set_checkgetrange_function (queue->srcpad,
402 GST_DEBUG_FUNCPTR (gst_queue2_src_checkgetrange_function));
403 gst_pad_set_getcaps_function (queue->srcpad,
404 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
405 gst_pad_set_acceptcaps_function (queue->srcpad,
406 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
407 gst_pad_set_event_function (queue->srcpad,
408 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
409 gst_pad_set_query_function (queue->srcpad,
410 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
411 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
414 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
415 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
416 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
417 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
418 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
419 queue->use_buffering = DEFAULT_USE_BUFFERING;
420 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
421 queue->low_percent = DEFAULT_LOW_PERCENT;
422 queue->high_percent = DEFAULT_HIGH_PERCENT;
424 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
425 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
427 queue->sinktime = GST_CLOCK_TIME_NONE;
428 queue->srctime = GST_CLOCK_TIME_NONE;
429 queue->sink_tainted = TRUE;
430 queue->src_tainted = TRUE;
432 queue->srcresult = GST_FLOW_WRONG_STATE;
433 queue->sinkresult = GST_FLOW_WRONG_STATE;
434 queue->is_eos = FALSE;
435 queue->in_timer = g_timer_new ();
436 queue->out_timer = g_timer_new ();
438 queue->qlock = g_mutex_new ();
439 queue->waiting_add = FALSE;
440 queue->item_add = g_cond_new ();
441 queue->waiting_del = FALSE;
442 queue->item_del = g_cond_new ();
443 queue->queue = g_queue_new ();
445 queue->buffering_percent = 100;
447 /* tempfile related */
448 queue->temp_template = NULL;
449 queue->temp_location = NULL;
450 queue->temp_location_set = FALSE;
451 queue->temp_remove = DEFAULT_TEMP_REMOVE;
453 queue->ring_buffer = NULL;
454 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
456 GST_DEBUG_OBJECT (queue,
457 "initialized queue's not_empty & not_full conditions");
460 /* called only once, as opposed to dispose */
462 gst_queue2_finalize (GObject * object)
464 GstQueue2 *queue = GST_QUEUE2 (object);
466 GST_DEBUG_OBJECT (queue, "finalizing queue");
468 while (!g_queue_is_empty (queue->queue)) {
469 GstMiniObject *data = g_queue_pop_head (queue->queue);
471 gst_mini_object_unref (data);
474 g_queue_free (queue->queue);
475 g_mutex_free (queue->qlock);
476 g_cond_free (queue->item_add);
477 g_cond_free (queue->item_del);
478 g_timer_destroy (queue->in_timer);
479 g_timer_destroy (queue->out_timer);
481 /* temp_file path cleanup */
482 g_free (queue->temp_template);
483 g_free (queue->temp_location);
485 G_OBJECT_CLASS (parent_class)->finalize (object);
489 debug_ranges (GstQueue2 * queue)
491 GstQueue2Range *walk;
493 for (walk = queue->ranges; walk; walk = walk->next) {
494 GST_DEBUG_OBJECT (queue,
495 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
496 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
497 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
498 walk->rb_writing_pos, walk->reading_pos,
499 walk == queue->current ? "**y**" : " n ");
503 /* clear all the downloaded ranges */
505 clean_ranges (GstQueue2 * queue)
507 GST_DEBUG_OBJECT (queue, "clean queue ranges");
509 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
510 queue->ranges = NULL;
511 queue->current = NULL;
514 /* find a range that contains @offset or NULL when nothing does */
515 static GstQueue2Range *
516 find_range (GstQueue2 * queue, guint64 offset)
518 GstQueue2Range *range = NULL;
519 GstQueue2Range *walk;
521 /* first do a quick check for the current range */
522 for (walk = queue->ranges; walk; walk = walk->next) {
523 if (offset >= walk->offset && offset <= walk->writing_pos) {
524 /* we can reuse an existing range */
530 GST_DEBUG_OBJECT (queue,
531 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
532 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
534 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
540 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
542 guint64 max_reading_pos, writing_pos;
544 writing_pos = range->writing_pos;
545 max_reading_pos = range->max_reading_pos;
547 if (writing_pos > max_reading_pos)
548 queue->cur_level.bytes = writing_pos - max_reading_pos;
550 queue->cur_level.bytes = 0;
553 /* make a new range for @offset or reuse an existing range */
554 static GstQueue2Range *
555 add_range (GstQueue2 * queue, guint64 offset)
557 GstQueue2Range *range, *prev, *next;
559 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
561 if ((range = find_range (queue, offset))) {
562 GST_DEBUG_OBJECT (queue,
563 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
565 range->writing_pos = offset;
567 GST_DEBUG_OBJECT (queue,
568 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
570 range = g_slice_new0 (GstQueue2Range);
571 range->offset = offset;
572 /* we want to write to the next location in the ring buffer */
573 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
574 range->writing_pos = offset;
575 range->rb_writing_pos = range->rb_offset;
576 range->reading_pos = offset;
577 range->max_reading_pos = offset;
581 next = queue->ranges;
583 if (next->offset > offset) {
584 /* insert before next */
585 GST_DEBUG_OBJECT (queue,
586 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
598 queue->ranges = range;
600 debug_ranges (queue);
602 /* update the stats for this range */
603 update_cur_level (queue, range);
609 /* clear and init the download ranges for offset 0 */
611 init_ranges (GstQueue2 * queue)
613 GST_DEBUG_OBJECT (queue, "init queue ranges");
615 /* get rid of all the current ranges */
616 clean_ranges (queue);
617 /* make a range for offset 0 */
618 queue->current = add_range (queue, 0);
622 gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps)
628 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
630 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
631 result = gst_pad_peer_accept_caps (otherpad, caps);
637 gst_queue2_getcaps (GstPad * pad)
643 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
644 if (G_UNLIKELY (queue == NULL))
645 return gst_caps_new_any ();
647 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
648 result = gst_pad_peer_get_caps (otherpad);
650 result = gst_caps_new_any ();
652 gst_object_unref (queue);
658 gst_queue2_bufferalloc (GstPad * pad, guint64 offset, guint size,
659 GstCaps * caps, GstBuffer ** buf)
662 GstFlowReturn result;
664 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
666 /* Forward to src pad, without setting caps on the src pad */
667 result = gst_pad_alloc_buffer (queue->srcpad, offset, size, caps, buf);
672 /* calculate the diff between running time on the sink and src of the queue.
673 * This is the total amount of time in the queue. */
675 update_time_level (GstQueue2 * queue)
677 if (queue->sink_tainted) {
679 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
680 queue->sink_segment.last_stop);
681 queue->sink_tainted = FALSE;
684 if (queue->src_tainted) {
686 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
687 queue->src_segment.last_stop);
688 queue->src_tainted = FALSE;
691 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
692 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
694 if (queue->sinktime != GST_CLOCK_TIME_NONE
695 && queue->srctime != GST_CLOCK_TIME_NONE
696 && queue->sinktime >= queue->srctime)
697 queue->cur_level.time = queue->sinktime - queue->srctime;
699 queue->cur_level.time = 0;
702 /* take a NEWSEGMENT event and apply the values to segment, updating the time
705 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
711 gint64 start, stop, time;
713 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
714 &format, &start, &stop, &time);
716 GST_DEBUG_OBJECT (queue,
717 "received NEWSEGMENT update %d, rate %lf, applied rate %lf, "
719 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
720 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
722 if (format == GST_FORMAT_BYTES) {
723 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
724 /* start is where we'll be getting from and as such writing next */
725 queue->current = add_range (queue, start);
726 /* update the stats for this range */
727 update_cur_level (queue, queue->current);
731 /* now configure the values, we use these to track timestamps on the
733 if (format != GST_FORMAT_TIME) {
734 /* non-time format, pretent the current time segment is closed with a
735 * 0 start and unknown stop time. */
737 format = GST_FORMAT_TIME;
742 gst_segment_set_newsegment_full (segment, update,
743 rate, arate, format, start, stop, time);
745 GST_DEBUG_OBJECT (queue,
746 "configured NEWSEGMENT %" GST_SEGMENT_FORMAT, segment);
749 queue->sink_tainted = TRUE;
751 queue->src_tainted = TRUE;
753 /* segment can update the time level of the queue */
754 update_time_level (queue);
757 /* take a buffer and update segment, updating the time level of the queue. */
759 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
762 GstClockTime duration, timestamp;
764 timestamp = GST_BUFFER_TIMESTAMP (buffer);
765 duration = GST_BUFFER_DURATION (buffer);
767 /* if no timestamp is set, assume it's continuous with the previous
769 if (timestamp == GST_CLOCK_TIME_NONE)
770 timestamp = segment->last_stop;
773 if (duration != GST_CLOCK_TIME_NONE)
774 timestamp += duration;
776 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
777 GST_TIME_ARGS (timestamp));
779 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
782 queue->sink_tainted = TRUE;
784 queue->src_tainted = TRUE;
786 /* calc diff with other end */
787 update_time_level (queue);
791 update_buffering (GstQueue2 * queue)
794 gboolean post = FALSE;
796 if (queue->high_percent <= 0)
799 #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)
802 /* on EOS we are always 100% full, we set the var here so that it we can
803 * reuse the logic below to stop buffering */
805 GST_LOG_OBJECT (queue, "we are EOS");
807 /* figure out the percent we are filled, we take the max of all formats. */
809 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
810 percent = GET_PERCENT (bytes, 0);
812 guint64 rb_size = queue->ring_buffer_max_size;
813 percent = GET_PERCENT (bytes, rb_size);
815 percent = MAX (percent, GET_PERCENT (time, 0));
816 percent = MAX (percent, GET_PERCENT (buffers, 0));
818 /* also apply the rate estimate when we need to */
819 if (queue->use_rate_estimate)
820 percent = MAX (percent, GET_PERCENT (rate_time, 0));
823 if (queue->is_buffering) {
825 /* if we were buffering see if we reached the high watermark */
826 if (percent >= queue->high_percent)
827 queue->is_buffering = FALSE;
829 /* we were not buffering, check if we need to start buffering if we drop
830 * below the low threshold */
831 if (percent < queue->low_percent) {
832 queue->is_buffering = TRUE;
833 queue->buffering_iteration++;
839 GstBufferingMode mode;
840 gint64 buffering_left = -1;
842 /* scale to high percent so that it becomes the 100% mark */
843 percent = percent * 100 / queue->high_percent;
848 if (percent != queue->buffering_percent) {
849 queue->buffering_percent = percent;
851 if (!QUEUE_IS_USING_QUEUE (queue)) {
852 GstFormat fmt = GST_FORMAT_BYTES;
855 if (QUEUE_IS_USING_RING_BUFFER (queue))
856 mode = GST_BUFFERING_TIMESHIFT;
858 mode = GST_BUFFERING_DOWNLOAD;
860 if (queue->byte_in_rate > 0) {
861 if (gst_pad_query_peer_duration (queue->sinkpad, &fmt, &duration))
863 (gdouble) ((duration -
864 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
866 buffering_left = G_MAXINT64;
869 mode = GST_BUFFERING_STREAM;
872 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
873 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
875 gst_message_set_buffering_stats (message, mode,
876 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
878 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
881 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
888 reset_rate_timer (GstQueue2 * queue)
891 queue->bytes_out = 0;
892 queue->byte_in_rate = 0.0;
893 queue->byte_out_rate = 0.0;
894 queue->last_in_elapsed = 0.0;
895 queue->last_out_elapsed = 0.0;
896 queue->in_timer_started = FALSE;
897 queue->out_timer_started = FALSE;
900 /* the interval in seconds to recalculate the rate */
901 #define RATE_INTERVAL 0.2
902 /* Tuning for rate estimation. We use a large window for the input rate because
903 * it should be stable when connected to a network. The output rate is less
904 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
905 * therefore adapt more quickly. */
906 #define AVG_IN(avg,val) ((avg) * 15.0 + (val)) / 16.0
907 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
910 update_in_rates (GstQueue2 * queue)
912 gdouble elapsed, period;
913 gdouble byte_in_rate;
915 if (!queue->in_timer_started) {
916 queue->in_timer_started = TRUE;
917 g_timer_start (queue->in_timer);
921 elapsed = g_timer_elapsed (queue->in_timer, NULL);
923 /* recalc after each interval. */
924 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
925 period = elapsed - queue->last_in_elapsed;
927 GST_DEBUG_OBJECT (queue,
928 "rates: period %f, in %" G_GUINT64_FORMAT, period, queue->bytes_in);
930 byte_in_rate = queue->bytes_in / period;
932 if (queue->byte_in_rate == 0.0)
933 queue->byte_in_rate = byte_in_rate;
935 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate);
937 /* reset the values to calculate rate over the next interval */
938 queue->last_in_elapsed = elapsed;
942 if (queue->byte_in_rate > 0.0) {
943 queue->cur_level.rate_time =
944 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
946 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
947 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
951 update_out_rates (GstQueue2 * queue)
953 gdouble elapsed, period;
954 gdouble byte_out_rate;
956 if (!queue->out_timer_started) {
957 queue->out_timer_started = TRUE;
958 g_timer_start (queue->out_timer);
962 elapsed = g_timer_elapsed (queue->out_timer, NULL);
964 /* recalc after each interval. */
965 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
966 period = elapsed - queue->last_out_elapsed;
968 GST_DEBUG_OBJECT (queue,
969 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
971 byte_out_rate = queue->bytes_out / period;
973 if (queue->byte_out_rate == 0.0)
974 queue->byte_out_rate = byte_out_rate;
976 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
978 /* reset the values to calculate rate over the next interval */
979 queue->last_out_elapsed = elapsed;
980 queue->bytes_out = 0;
982 if (queue->byte_in_rate > 0.0) {
983 queue->cur_level.rate_time =
984 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
986 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
987 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
991 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
993 guint64 reading_pos, max_reading_pos;
996 max_reading_pos = range->max_reading_pos;
998 max_reading_pos = MAX (max_reading_pos, reading_pos);
1000 GST_DEBUG_OBJECT (queue,
1001 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1002 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1003 range->max_reading_pos = max_reading_pos;
1005 update_cur_level (queue, range);
1009 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1014 GST_QUEUE2_MUTEX_UNLOCK (queue);
1016 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1019 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1020 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1021 GST_SEEK_TYPE_NONE, -1);
1023 res = gst_pad_push_event (queue->sinkpad, event);
1024 GST_QUEUE2_MUTEX_LOCK (queue);
1027 queue->current = add_range (queue, offset);
1032 /* see if there is enough data in the file to read a full buffer */
1034 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1036 GstQueue2Range *range;
1038 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1041 if ((range = find_range (queue, offset))) {
1042 if (queue->current != range) {
1043 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1044 perform_seek_to_offset (queue, range->writing_pos);
1047 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1048 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1050 /* we have a range for offset */
1051 GST_DEBUG_OBJECT (queue,
1052 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1053 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1055 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1058 if (offset + length <= range->writing_pos)
1061 GST_DEBUG_OBJECT (queue,
1062 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1063 (offset + length) - range->writing_pos);
1066 GST_INFO_OBJECT (queue, "not found in any range");
1067 /* we don't have the range, see how far away we are, FIXME, find a good
1068 * threshold based on the incomming rate. */
1069 if (!queue->is_eos && queue->current) {
1070 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1071 if (offset < queue->current->offset || offset >
1072 queue->current->writing_pos + QUEUE_MAX_BYTES (queue) -
1073 queue->cur_level.bytes) {
1074 perform_seek_to_offset (queue, offset);
1076 GST_INFO_OBJECT (queue,
1077 "requested data is within range, wait for data");
1079 } else if (offset < queue->current->writing_pos + 200000) {
1080 update_cur_pos (queue, queue->current, offset + length);
1081 GST_INFO_OBJECT (queue, "wait for data");
1086 /* too far away, do a seek */
1087 perform_seek_to_offset (queue, offset);
1094 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1095 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1096 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1098 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1101 static GstFlowReturn
1102 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1103 guint8 * dst, gint64 * read_return)
1105 guint8 *ring_buffer;
1108 ring_buffer = queue->ring_buffer;
1110 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1113 /* this should not block */
1114 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1116 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1117 res = fread (dst, 1, length, queue->temp_file);
1119 memcpy (dst, ring_buffer + offset, length);
1123 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1125 if (G_UNLIKELY (res < length)) {
1126 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1127 goto could_not_read;
1128 /* check for errors or EOF */
1129 if (ferror (queue->temp_file))
1130 goto could_not_read;
1131 if (feof (queue->temp_file) && length > 0)
1141 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1142 return GST_FLOW_ERROR;
1146 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1147 return GST_FLOW_ERROR;
1151 GST_DEBUG ("non-regular file hits EOS");
1152 return GST_FLOW_UNEXPECTED;
1156 static GstFlowReturn
1157 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1158 GstBuffer ** buffer)
1162 guint64 file_offset;
1163 guint block_length, remaining, read_length;
1166 GstFlowReturn ret = GST_FLOW_OK;
1168 /* allocate the output buffer of the requested size */
1169 buf = gst_buffer_new_and_alloc (length);
1170 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
1172 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1176 rb_size = queue->ring_buffer_max_size;
1179 while (remaining > 0) {
1180 /* configure how much/whether to read */
1181 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1184 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1187 /* calculate how far away the offset is */
1188 if (queue->current->writing_pos > rpos)
1189 level = queue->current->writing_pos - rpos;
1193 GST_DEBUG_OBJECT (queue,
1194 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1195 ", level %" G_GUINT64_FORMAT,
1196 rpos, queue->current->writing_pos, level);
1198 if (level >= rb_size) {
1199 /* we don't have the data but if we have a ring buffer that is full, we
1201 GST_DEBUG_OBJECT (queue,
1202 "ring buffer full, reading ring-buffer-max-size %"
1203 G_GUINT64_FORMAT " bytes", rb_size);
1204 read_length = rb_size;
1205 } else if (queue->is_eos) {
1206 /* won't get any more data so read any data we have */
1208 GST_DEBUG_OBJECT (queue,
1209 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1211 read_length = level;
1217 if (read_length == 0) {
1218 if (QUEUE_IS_USING_RING_BUFFER (queue)
1219 && queue->current->max_reading_pos > rpos) {
1220 /* protect cached data (data between offset and max_reading_pos)
1221 * and update current level */
1222 GST_DEBUG_OBJECT (queue,
1223 "protecting cached data [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1224 "]", rpos, queue->current->max_reading_pos);
1225 queue->current->max_reading_pos = rpos;
1226 update_cur_level (queue, queue->current);
1228 GST_DEBUG_OBJECT (queue, "waiting for add");
1229 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1233 /* we have the requested data so read it */
1234 read_length = remaining;
1237 /* set range reading_pos to actual reading position for this read */
1238 queue->current->reading_pos = rpos;
1240 /* congfigure how much and from where to read */
1241 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1243 (queue->current->rb_offset + (rpos -
1244 queue->current->offset)) % rb_size;
1245 if (file_offset + read_length > rb_size) {
1246 block_length = rb_size - file_offset;
1248 block_length = read_length;
1252 block_length = read_length;
1255 /* while we still have data to read, we loop */
1256 while (read_length > 0) {
1260 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1261 data, &read_return);
1262 if (ret != GST_FLOW_OK)
1265 file_offset += read_return;
1266 if (QUEUE_IS_USING_RING_BUFFER (queue))
1267 file_offset %= rb_size;
1269 data += read_return;
1270 read_length -= read_return;
1271 block_length = read_length;
1272 remaining -= read_return;
1274 rpos = (queue->current->reading_pos += read_return);
1275 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1277 GST_QUEUE2_SIGNAL_DEL (queue);
1278 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1281 gst_buffer_unmap (buf, data, length);
1283 GST_BUFFER_OFFSET (buf) = offset;
1284 GST_BUFFER_OFFSET_END (buf) = offset + length;
1293 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1294 gst_buffer_unref (buf);
1295 return GST_FLOW_UNEXPECTED;
1299 GST_DEBUG_OBJECT (queue, "we are flushing");
1300 gst_buffer_unref (buf);
1301 return GST_FLOW_WRONG_STATE;
1305 GST_DEBUG_OBJECT (queue, "we have a read error");
1306 gst_buffer_unmap (buf, data, 0);
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->starting_segment != NULL) {
1319 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1320 queue->starting_segment = NULL;
1324 guint64 reading_pos;
1326 reading_pos = queue->current->reading_pos;
1329 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1334 item = GST_MINI_OBJECT_CAST (buffer);
1336 case GST_FLOW_UNEXPECTED:
1337 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1347 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1348 * the temp filename. */
1350 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1355 if (queue->temp_file)
1356 goto already_opened;
1358 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1360 /* we have two cases:
1361 * - temp_location was set to something !NULL (Deprecated). in this case we
1362 * open the specified filename.
1363 * - temp_template was set, allocate a filename and open that filename
1365 if (!queue->temp_location_set) {
1367 if (queue->temp_template == NULL)
1370 /* make copy of the template, we don't want to change this */
1371 name = g_strdup (queue->temp_template);
1372 fd = g_mkstemp (name);
1374 goto mkstemp_failed;
1376 /* open the file for update/writing */
1377 queue->temp_file = fdopen (fd, "wb+");
1378 /* error creating file */
1379 if (queue->temp_file == NULL)
1382 g_free (queue->temp_location);
1383 queue->temp_location = name;
1385 GST_QUEUE2_MUTEX_UNLOCK (queue);
1387 /* we can't emit the notify with the lock */
1388 g_object_notify (G_OBJECT (queue), "temp-location");
1390 GST_QUEUE2_MUTEX_LOCK (queue);
1392 /* open the file for update/writing, this is deprecated but we still need to
1393 * support it for API/ABI compatibility */
1394 queue->temp_file = g_fopen (queue->temp_location, "wb+");
1395 /* error creating file */
1396 if (queue->temp_file == NULL)
1399 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1406 GST_DEBUG_OBJECT (queue, "temp file was already open");
1411 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1412 (_("No Temp directory specified.")), (NULL));
1417 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1418 (_("Could not create temp file \"%s\"."), queue->temp_template),
1425 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1426 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1435 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1438 if (queue->temp_file == NULL)
1441 GST_DEBUG_OBJECT (queue, "closing temp file");
1443 fflush (queue->temp_file);
1444 fclose (queue->temp_file);
1446 if (queue->temp_remove)
1447 remove (queue->temp_location);
1449 queue->temp_file = NULL;
1450 clean_ranges (queue);
1454 gst_queue2_flush_temp_file (GstQueue2 * queue)
1456 if (queue->temp_file == NULL)
1459 GST_DEBUG_OBJECT (queue, "flushing temp file");
1461 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1465 gst_queue2_locked_flush (GstQueue2 * queue)
1467 if (!QUEUE_IS_USING_QUEUE (queue)) {
1468 if (QUEUE_IS_USING_TEMP_FILE (queue))
1469 gst_queue2_flush_temp_file (queue);
1470 init_ranges (queue);
1472 while (!g_queue_is_empty (queue->queue)) {
1473 GstMiniObject *data = g_queue_pop_head (queue->queue);
1475 /* Then lose another reference because we are supposed to destroy that
1476 data when flushing */
1477 gst_mini_object_unref (data);
1480 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1481 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1482 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1483 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1484 queue->sink_tainted = queue->src_tainted = TRUE;
1485 if (queue->starting_segment != NULL)
1486 gst_event_unref (queue->starting_segment);
1487 queue->starting_segment = NULL;
1488 queue->segment_event_received = FALSE;
1490 /* we deleted a lot of something */
1491 GST_QUEUE2_SIGNAL_DEL (queue);
1495 gst_queue2_wait_free_space (GstQueue2 * queue)
1497 /* We make space available if we're "full" according to whatever
1498 * the user defined as "full". */
1499 if (gst_queue2_is_filled (queue)) {
1502 /* pause the timer while we wait. The fact that we are waiting does not mean
1503 * the byterate on the input pad is lower */
1504 if ((started = queue->in_timer_started))
1505 g_timer_stop (queue->in_timer);
1507 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1508 "queue is full, waiting for free space");
1510 /* Wait for space to be available, we could be unlocked because of a flush. */
1511 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1513 while (gst_queue2_is_filled (queue));
1515 /* and continue if we were running before */
1517 g_timer_continue (queue->in_timer);
1524 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1530 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1532 guint8 *odata, *data, *ring_buffer;
1533 guint size, rb_size;
1535 guint64 writing_pos, new_writing_pos;
1536 GstQueue2Range *range, *prev, *next;
1538 if (QUEUE_IS_USING_RING_BUFFER (queue))
1539 writing_pos = queue->current->rb_writing_pos;
1541 writing_pos = queue->current->writing_pos;
1542 ring_buffer = queue->ring_buffer;
1543 rb_size = queue->ring_buffer_max_size;
1545 odata = gst_buffer_map (buffer, &osize, NULL, GST_MAP_READ);
1550 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1551 GST_BUFFER_OFFSET (buffer));
1556 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1559 /* calculate the space in the ring buffer not used by data from
1560 * the current range */
1561 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1562 /* wait until there is some free space */
1563 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1565 /* get the amount of space we have */
1566 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1568 /* calculate if we need to split or if we can write the entire
1570 to_write = MIN (size, space);
1572 /* the writing position in the ring buffer after writing (part
1573 * or all of) the buffer */
1574 new_writing_pos = (writing_pos + to_write) % rb_size;
1577 range = queue->ranges;
1579 /* if we need to overwrite data in the ring buffer, we need to
1582 * warning: this code is complicated and includes some
1583 * simplifications - pen, paper and diagrams for the cases
1586 guint64 range_data_start, range_data_end;
1587 GstQueue2Range *range_to_destroy = NULL;
1589 range_data_start = range->rb_offset;
1590 range_data_end = range->rb_writing_pos;
1592 /* handle the special case where the range has no data in it */
1593 if (range->writing_pos == range->offset) {
1594 if (range != queue->current) {
1595 GST_DEBUG_OBJECT (queue,
1596 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1597 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1599 range_to_destroy = range;
1601 prev->next = range->next;
1606 if (range_data_end > range_data_start) {
1607 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1610 if (new_writing_pos > range_data_start) {
1611 if (new_writing_pos >= range_data_end) {
1612 GST_DEBUG_OBJECT (queue,
1613 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1614 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1616 range_to_destroy = range;
1618 prev->next = range->next;
1620 GST_DEBUG_OBJECT (queue,
1621 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1622 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1623 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1624 range->offset + new_writing_pos - range_data_start,
1626 range->offset += (new_writing_pos - range_data_start);
1627 range->rb_offset = new_writing_pos;
1631 guint64 new_wpos_virt = writing_pos + to_write;
1633 if (new_wpos_virt <= range_data_start)
1636 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1637 GST_DEBUG_OBJECT (queue,
1638 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1639 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1641 range_to_destroy = range;
1643 prev->next = range->next;
1645 GST_DEBUG_OBJECT (queue,
1646 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1647 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1648 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1649 range->offset + new_writing_pos - range_data_start,
1651 range->offset += (new_wpos_virt - range_data_start);
1652 range->rb_offset = new_writing_pos;
1657 if (!range_to_destroy)
1660 range = range->next;
1661 if (range_to_destroy) {
1662 if (range_to_destroy == queue->ranges)
1663 queue->ranges = range;
1664 g_slice_free (GstQueue2Range, range_to_destroy);
1665 range_to_destroy = NULL;
1670 new_writing_pos = writing_pos + to_write;
1673 if (QUEUE_IS_USING_TEMP_FILE (queue)
1674 && FSEEK_FILE (queue->temp_file, writing_pos))
1677 if (new_writing_pos > writing_pos) {
1678 GST_INFO_OBJECT (queue,
1679 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1680 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1681 queue->current->writing_pos, queue->current->rb_writing_pos);
1682 /* either not using ring buffer or no wrapping, just write */
1683 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1684 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1687 memcpy (ring_buffer + writing_pos, data, to_write);
1690 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1691 /* try to merge with next range */
1692 while ((next = queue->current->next)) {
1693 GST_INFO_OBJECT (queue,
1694 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1695 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1696 if (new_writing_pos < next->offset)
1699 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1702 /* remove the group, we could choose to not read the data in this range
1703 * again. This would involve us doing a seek to the current writing position
1704 * in the range. FIXME, It would probably make sense to do a seek when there
1705 * is a lot of data in the range we merged with to avoid reading it all
1707 queue->current->next = next->next;
1708 g_slice_free (GstQueue2Range, next);
1710 debug_ranges (queue);
1712 goto update_and_signal;
1716 guint block_one, block_two;
1718 block_one = rb_size - writing_pos;
1719 block_two = to_write - block_one;
1721 if (block_one > 0) {
1722 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1723 /* write data to end of ring buffer */
1724 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1725 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1728 memcpy (ring_buffer + writing_pos, data, block_one);
1732 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1735 if (block_two > 0) {
1736 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1737 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1738 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1741 memcpy (ring_buffer, data + block_one, block_two);
1747 /* update the writing positions */
1749 GST_INFO_OBJECT (queue,
1750 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1751 to_write, writing_pos, size);
1753 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1755 queue->current->writing_pos += to_write;
1756 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1758 queue->current->writing_pos = writing_pos = new_writing_pos;
1760 update_cur_level (queue, queue->current);
1762 /* update the buffering status */
1763 if (queue->use_buffering)
1764 update_buffering (queue);
1766 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1767 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1769 GST_QUEUE2_SIGNAL_ADD (queue);
1772 gst_buffer_unmap (buffer, odata, osize);
1779 GST_DEBUG_OBJECT (queue, "we are flushing");
1780 gst_buffer_unmap (buffer, odata, osize);
1781 /* FIXME - GST_FLOW_UNEXPECTED ? */
1786 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1787 gst_buffer_unmap (buffer, odata, osize);
1794 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1798 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1799 (_("Error while writing to download file.")),
1800 ("%s", g_strerror (errno)));
1803 gst_buffer_unmap (buffer, odata, osize);
1808 /* enqueue an item an update the level stats */
1810 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item, gboolean isbuffer)
1816 buffer = GST_BUFFER_CAST (item);
1817 size = gst_buffer_get_size (buffer);
1819 /* add buffer to the statistics */
1820 if (QUEUE_IS_USING_QUEUE (queue)) {
1821 queue->cur_level.buffers++;
1822 queue->cur_level.bytes += size;
1824 queue->bytes_in += size;
1826 /* apply new buffer to segment stats */
1827 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1828 /* update the byterate stats */
1829 update_in_rates (queue);
1831 if (!QUEUE_IS_USING_QUEUE (queue)) {
1832 /* FIXME - check return value? */
1833 gst_queue2_create_write (queue, buffer);
1835 } else if (GST_IS_EVENT (item)) {
1838 event = GST_EVENT_CAST (item);
1840 switch (GST_EVENT_TYPE (event)) {
1842 /* Zero the thresholds, this makes sure the queue is completely
1843 * filled and we can read all data from the queue. */
1844 GST_DEBUG_OBJECT (queue, "we have EOS");
1845 queue->is_eos = TRUE;
1847 case GST_EVENT_NEWSEGMENT:
1848 apply_segment (queue, event, &queue->sink_segment, TRUE);
1849 /* This is our first new segment, we hold it
1850 * as we can't save it on the temp file */
1851 if (!QUEUE_IS_USING_QUEUE (queue)) {
1852 if (queue->segment_event_received)
1853 goto unexpected_event;
1855 queue->segment_event_received = TRUE;
1856 if (queue->starting_segment != NULL)
1857 gst_event_unref (queue->starting_segment);
1858 queue->starting_segment = event;
1861 /* a new segment allows us to accept more buffers if we got UNEXPECTED
1862 * from downstream */
1863 queue->unexpected = FALSE;
1866 if (!QUEUE_IS_USING_QUEUE (queue))
1867 goto unexpected_event;
1871 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1872 item, GST_OBJECT_NAME (queue));
1873 /* we can't really unref since we don't know what it is */
1878 /* update the buffering status */
1879 if (queue->use_buffering)
1880 update_buffering (queue);
1882 if (QUEUE_IS_USING_QUEUE (queue)) {
1883 g_queue_push_tail (queue->queue, item);
1885 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
1888 GST_QUEUE2_SIGNAL_ADD (queue);
1897 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
1898 gst_event_type_get_name (GST_EVENT_TYPE (item)),
1899 GST_OBJECT_NAME (queue));
1900 gst_event_unref (GST_EVENT_CAST (item));
1905 /* dequeue an item from the queue and update level stats */
1906 static GstMiniObject *
1907 gst_queue2_locked_dequeue (GstQueue2 * queue, gboolean * is_buffer)
1909 GstMiniObject *item;
1911 if (!QUEUE_IS_USING_QUEUE (queue))
1912 item = gst_queue2_read_item_from_file (queue);
1914 item = g_queue_pop_head (queue->queue);
1919 if (GST_IS_BUFFER (item)) {
1923 buffer = GST_BUFFER_CAST (item);
1924 size = gst_buffer_get_size (buffer);
1927 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1928 "retrieved buffer %p from queue", buffer);
1930 if (QUEUE_IS_USING_QUEUE (queue)) {
1931 queue->cur_level.buffers--;
1932 queue->cur_level.bytes -= size;
1934 queue->bytes_out += size;
1936 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
1937 /* update the byterate stats */
1938 update_out_rates (queue);
1939 /* update the buffering */
1940 if (queue->use_buffering)
1941 update_buffering (queue);
1943 } else if (GST_IS_EVENT (item)) {
1944 GstEvent *event = GST_EVENT_CAST (item);
1948 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1949 "retrieved event %p from queue", event);
1951 switch (GST_EVENT_TYPE (event)) {
1953 /* queue is empty now that we dequeued the EOS */
1954 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1956 case GST_EVENT_NEWSEGMENT:
1957 apply_segment (queue, event, &queue->src_segment, FALSE);
1964 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
1965 item, GST_OBJECT_NAME (queue));
1968 GST_QUEUE2_SIGNAL_DEL (queue);
1975 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
1981 gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event)
1985 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
1987 switch (GST_EVENT_TYPE (event)) {
1988 case GST_EVENT_FLUSH_START:
1990 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
1991 if (QUEUE_IS_USING_QUEUE (queue)) {
1993 gst_pad_push_event (queue->srcpad, event);
1995 /* now unblock the chain function */
1996 GST_QUEUE2_MUTEX_LOCK (queue);
1997 queue->srcresult = GST_FLOW_WRONG_STATE;
1998 queue->sinkresult = GST_FLOW_WRONG_STATE;
1999 /* unblock the loop and chain functions */
2000 GST_QUEUE2_SIGNAL_ADD (queue);
2001 GST_QUEUE2_SIGNAL_DEL (queue);
2002 GST_QUEUE2_MUTEX_UNLOCK (queue);
2004 /* make sure it pauses, this should happen since we sent
2005 * flush_start downstream. */
2006 gst_pad_pause_task (queue->srcpad);
2007 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2009 GST_QUEUE2_MUTEX_LOCK (queue);
2010 /* flush the sink pad */
2011 queue->sinkresult = GST_FLOW_WRONG_STATE;
2012 GST_QUEUE2_SIGNAL_DEL (queue);
2013 GST_QUEUE2_MUTEX_UNLOCK (queue);
2015 gst_event_unref (event);
2019 case GST_EVENT_FLUSH_STOP:
2021 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2023 if (QUEUE_IS_USING_QUEUE (queue)) {
2025 gst_pad_push_event (queue->srcpad, event);
2027 GST_QUEUE2_MUTEX_LOCK (queue);
2028 gst_queue2_locked_flush (queue);
2029 queue->srcresult = GST_FLOW_OK;
2030 queue->sinkresult = GST_FLOW_OK;
2031 queue->is_eos = FALSE;
2032 queue->unexpected = FALSE;
2033 /* reset rate counters */
2034 reset_rate_timer (queue);
2035 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2037 GST_QUEUE2_MUTEX_UNLOCK (queue);
2039 GST_QUEUE2_MUTEX_LOCK (queue);
2040 queue->segment_event_received = FALSE;
2041 queue->is_eos = FALSE;
2042 queue->unexpected = FALSE;
2043 queue->sinkresult = GST_FLOW_OK;
2044 GST_QUEUE2_MUTEX_UNLOCK (queue);
2046 gst_event_unref (event);
2051 if (GST_EVENT_IS_SERIALIZED (event)) {
2052 /* serialized events go in the queue */
2053 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2054 /* refuse more events on EOS */
2057 gst_queue2_locked_enqueue (queue, event, FALSE);
2058 GST_QUEUE2_MUTEX_UNLOCK (queue);
2060 /* non-serialized events are passed upstream. */
2061 gst_pad_push_event (queue->srcpad, event);
2071 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2072 GST_QUEUE2_MUTEX_UNLOCK (queue);
2073 gst_event_unref (event);
2078 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2079 GST_QUEUE2_MUTEX_UNLOCK (queue);
2080 gst_event_unref (event);
2086 gst_queue2_is_empty (GstQueue2 * queue)
2088 /* never empty on EOS */
2092 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2093 return queue->current->writing_pos <= queue->current->max_reading_pos;
2095 if (queue->queue->length == 0)
2103 gst_queue2_is_filled (GstQueue2 * queue)
2107 /* always filled on EOS */
2111 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2112 (queue->cur_level.format) >= ((alt_max) ? \
2113 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2115 /* if using a ring buffer we're filled if all ring buffer space is used
2116 * _by the current range_ */
2117 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2118 guint64 rb_size = queue->ring_buffer_max_size;
2119 GST_DEBUG_OBJECT (queue,
2120 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2121 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2122 return CHECK_FILLED (bytes, rb_size);
2125 /* if using file, we're never filled if we don't have EOS */
2126 if (QUEUE_IS_USING_TEMP_FILE (queue))
2129 /* we are never filled when we have no buffers at all */
2130 if (queue->cur_level.buffers == 0)
2133 /* we are filled if one of the current levels exceeds the max */
2134 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2135 || CHECK_FILLED (time, 0);
2137 /* if we need to, use the rate estimate to check against the max time we are
2138 * allowed to queue */
2139 if (queue->use_rate_estimate)
2140 res |= CHECK_FILLED (rate_time, 0);
2146 static GstFlowReturn
2147 gst_queue2_chain (GstPad * pad, GstBuffer * buffer)
2151 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2153 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2154 "received buffer %p of size %d, time %" GST_TIME_FORMAT ", duration %"
2155 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2156 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2157 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2159 /* we have to lock the queue since we span threads */
2160 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2161 /* when we received EOS, we refuse more data */
2164 /* when we received unexpected from downstream, refuse more buffers */
2165 if (queue->unexpected)
2166 goto out_unexpected;
2168 if (!gst_queue2_wait_free_space (queue))
2171 /* put buffer in queue now */
2172 gst_queue2_locked_enqueue (queue, buffer, TRUE);
2173 GST_QUEUE2_MUTEX_UNLOCK (queue);
2177 /* special conditions */
2180 GstFlowReturn ret = queue->sinkresult;
2182 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2183 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2184 GST_QUEUE2_MUTEX_UNLOCK (queue);
2185 gst_buffer_unref (buffer);
2191 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2192 GST_QUEUE2_MUTEX_UNLOCK (queue);
2193 gst_buffer_unref (buffer);
2195 return GST_FLOW_UNEXPECTED;
2199 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2200 "exit because we received UNEXPECTED");
2201 GST_QUEUE2_MUTEX_UNLOCK (queue);
2202 gst_buffer_unref (buffer);
2204 return GST_FLOW_UNEXPECTED;
2208 /* dequeue an item from the queue an push it downstream. This functions returns
2209 * the result of the push. */
2210 static GstFlowReturn
2211 gst_queue2_push_one (GstQueue2 * queue)
2213 GstFlowReturn result = GST_FLOW_OK;
2214 GstMiniObject *data;
2215 gboolean is_buffer = FALSE;
2217 data = gst_queue2_locked_dequeue (queue, &is_buffer);
2222 GST_QUEUE2_MUTEX_UNLOCK (queue);
2228 buffer = GST_BUFFER_CAST (data);
2229 caps = GST_BUFFER_CAPS (buffer);
2231 /* set caps before pushing the buffer so that core does not try to do
2232 * something fancy to check if this is possible. */
2233 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2234 gst_pad_set_caps (queue->srcpad, caps);
2236 result = gst_pad_push (queue->srcpad, buffer);
2238 /* need to check for srcresult here as well */
2239 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2240 if (result == GST_FLOW_UNEXPECTED) {
2241 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2242 "got UNEXPECTED from downstream");
2243 /* stop pushing buffers, we dequeue all items until we see an item that we
2244 * can push again, which is EOS or NEWSEGMENT. If there is nothing in the
2245 * queue we can push, we set a flag to make the sinkpad refuse more
2246 * buffers with an UNEXPECTED return value until we receive something
2247 * pushable again or we get flushed. */
2248 while ((data = gst_queue2_locked_dequeue (queue, &is_buffer))) {
2250 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2251 "dropping UNEXPECTED buffer %p", data);
2252 gst_buffer_unref (GST_BUFFER_CAST (data));
2253 } else if (GST_IS_EVENT (data)) {
2254 GstEvent *event = GST_EVENT_CAST (data);
2255 GstEventType type = GST_EVENT_TYPE (event);
2257 if (type == GST_EVENT_EOS || type == GST_EVENT_NEWSEGMENT) {
2258 /* we found a pushable item in the queue, push it out */
2259 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2260 "pushing pushable event %s after UNEXPECTED",
2261 GST_EVENT_TYPE_NAME (event));
2264 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2265 "dropping UNEXPECTED event %p", event);
2266 gst_event_unref (event);
2269 /* no more items in the queue. Set the unexpected flag so that upstream
2270 * make us refuse any more buffers on the sinkpad. Since we will still
2271 * accept EOS and NEWSEGMENT we return _FLOW_OK to the caller so that the
2272 * task function does not shut down. */
2273 queue->unexpected = TRUE;
2274 result = GST_FLOW_OK;
2276 } else if (GST_IS_EVENT (data)) {
2277 GstEvent *event = GST_EVENT_CAST (data);
2278 GstEventType type = GST_EVENT_TYPE (event);
2280 gst_pad_push_event (queue->srcpad, event);
2282 /* if we're EOS, return UNEXPECTED so that the task pauses. */
2283 if (type == GST_EVENT_EOS) {
2284 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2285 "pushed EOS event %p, return UNEXPECTED", event);
2286 result = GST_FLOW_UNEXPECTED;
2289 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2296 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2297 "exit because we have no item in the queue");
2298 return GST_FLOW_ERROR;
2302 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2303 return GST_FLOW_WRONG_STATE;
2307 /* called repeadedly with @pad as the source pad. This function should push out
2308 * data to the peer element. */
2310 gst_queue2_loop (GstPad * pad)
2315 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2317 /* have to lock for thread-safety */
2318 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2320 if (gst_queue2_is_empty (queue)) {
2323 /* pause the timer while we wait. The fact that we are waiting does not mean
2324 * the byterate on the output pad is lower */
2325 if ((started = queue->out_timer_started))
2326 g_timer_stop (queue->out_timer);
2328 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2329 "queue is empty, waiting for new data");
2331 /* Wait for data to be available, we could be unlocked because of a flush. */
2332 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2334 while (gst_queue2_is_empty (queue));
2336 /* and continue if we were running before */
2338 g_timer_continue (queue->out_timer);
2340 ret = gst_queue2_push_one (queue);
2341 queue->srcresult = ret;
2342 queue->sinkresult = ret;
2343 if (ret != GST_FLOW_OK)
2346 GST_QUEUE2_MUTEX_UNLOCK (queue);
2353 gboolean eos = queue->is_eos;
2354 GstFlowReturn ret = queue->srcresult;
2356 gst_pad_pause_task (queue->srcpad);
2357 GST_QUEUE2_MUTEX_UNLOCK (queue);
2358 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2359 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2360 /* let app know about us giving up if upstream is not expected to do so */
2361 /* UNEXPECTED is already taken care of elsewhere */
2362 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED)) {
2363 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2364 (_("Internal data flow error.")),
2365 ("streaming task paused, reason %s (%d)",
2366 gst_flow_get_name (ret), ret));
2367 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2374 gst_queue2_handle_src_event (GstPad * pad, GstEvent * event)
2376 gboolean res = TRUE;
2377 GstQueue2 *queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2379 if (G_UNLIKELY (queue == NULL)) {
2380 gst_event_unref (event);
2383 #ifndef GST_DISABLE_GST_DEBUG
2384 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2385 event, GST_EVENT_TYPE_NAME (event));
2388 switch (GST_EVENT_TYPE (event)) {
2389 case GST_EVENT_FLUSH_START:
2390 if (QUEUE_IS_USING_QUEUE (queue)) {
2391 /* just forward upstream */
2392 res = gst_pad_push_event (queue->sinkpad, event);
2394 /* now unblock the getrange function */
2395 GST_QUEUE2_MUTEX_LOCK (queue);
2396 GST_DEBUG_OBJECT (queue, "flushing");
2397 queue->srcresult = GST_FLOW_WRONG_STATE;
2398 GST_QUEUE2_SIGNAL_ADD (queue);
2399 GST_QUEUE2_MUTEX_UNLOCK (queue);
2401 /* when using a temp file, we eat the event */
2403 gst_event_unref (event);
2406 case GST_EVENT_FLUSH_STOP:
2407 if (QUEUE_IS_USING_QUEUE (queue)) {
2408 /* just forward upstream */
2409 res = gst_pad_push_event (queue->sinkpad, event);
2411 /* now unblock the getrange function */
2412 GST_QUEUE2_MUTEX_LOCK (queue);
2413 queue->srcresult = GST_FLOW_OK;
2414 if (queue->current) {
2415 /* forget the highest read offset, we'll calculate a new one when we
2416 * get the next getrange request. We need to do this in order to reset
2417 * the buffering percentage */
2418 queue->current->max_reading_pos = 0;
2420 GST_QUEUE2_MUTEX_UNLOCK (queue);
2422 /* when using a temp file, we eat the event */
2424 gst_event_unref (event);
2428 res = gst_pad_push_event (queue->sinkpad, event);
2432 gst_object_unref (queue);
2437 gst_queue2_peer_query (GstQueue2 * queue, GstPad * pad, GstQuery * query)
2439 gboolean ret = FALSE;
2442 if ((peer = gst_pad_get_peer (pad))) {
2443 ret = gst_pad_query (peer, query);
2444 gst_object_unref (peer);
2450 gst_queue2_handle_src_query (GstPad * pad, GstQuery * query)
2454 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2455 if (G_UNLIKELY (queue == NULL))
2458 switch (GST_QUERY_TYPE (query)) {
2459 case GST_QUERY_POSITION:
2464 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2467 /* get peer position */
2468 gst_query_parse_position (query, &format, &peer_pos);
2470 /* FIXME: this code assumes that there's no discont in the queue */
2472 case GST_FORMAT_BYTES:
2473 peer_pos -= queue->cur_level.bytes;
2475 case GST_FORMAT_TIME:
2476 peer_pos -= queue->cur_level.time;
2479 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2480 "know how to adjust value", gst_format_get_name (format));
2483 /* set updated position */
2484 gst_query_set_position (query, format, peer_pos);
2487 case GST_QUERY_DURATION:
2489 GST_DEBUG_OBJECT (queue, "doing peer query");
2491 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2494 GST_DEBUG_OBJECT (queue, "peer query success");
2497 case GST_QUERY_BUFFERING:
2501 GST_DEBUG_OBJECT (queue, "query buffering");
2503 /* FIXME - is this condition correct? what should ring buffer do? */
2504 if (QUEUE_IS_USING_QUEUE (queue)) {
2505 /* no temp file, just forward to the peer */
2506 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2508 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2510 gint64 start, stop, range_start, range_stop;
2511 guint64 writing_pos;
2513 gint64 estimated_total, buffering_left;
2516 gboolean peer_res, is_buffering, is_eos;
2517 gdouble byte_in_rate, byte_out_rate;
2518 GstQueue2Range *queued_ranges;
2520 /* we need a current download region */
2521 if (queue->current == NULL)
2524 writing_pos = queue->current->writing_pos;
2525 byte_in_rate = queue->byte_in_rate;
2526 byte_out_rate = queue->byte_out_rate;
2527 is_buffering = queue->is_buffering;
2528 is_eos = queue->is_eos;
2529 percent = queue->buffering_percent;
2532 /* we're EOS, we know the duration in bytes now */
2534 duration = writing_pos;
2536 /* get duration of upstream in bytes */
2537 peer_fmt = GST_FORMAT_BYTES;
2538 peer_res = gst_pad_query_peer_duration (queue->sinkpad, &peer_fmt,
2542 /* calculate remaining and total download time */
2543 if (peer_res && byte_in_rate > 0.0) {
2544 estimated_total = (duration * 1000) / byte_in_rate;
2545 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2547 estimated_total = -1;
2548 buffering_left = -1;
2550 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2551 G_GINT64_FORMAT, estimated_total, buffering_left);
2553 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2556 case GST_FORMAT_PERCENT:
2557 /* we need duration */
2561 GST_DEBUG_OBJECT (queue,
2562 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2563 duration, writing_pos);
2566 /* get our available data relative to the duration */
2568 stop = GST_FORMAT_PERCENT_MAX * writing_pos / duration;
2572 case GST_FORMAT_BYTES:
2582 /* fill out the buffered ranges */
2583 for (queued_ranges = queue->ranges; queued_ranges;
2584 queued_ranges = queued_ranges->next) {
2586 case GST_FORMAT_PERCENT:
2587 if (duration == -1) {
2592 range_start = 100 * queued_ranges->offset / duration;
2593 range_stop = 100 * queued_ranges->writing_pos / duration;
2595 case GST_FORMAT_BYTES:
2596 range_start = queued_ranges->offset;
2597 range_stop = queued_ranges->writing_pos;
2604 if (range_start == range_stop)
2606 GST_DEBUG_OBJECT (queue,
2607 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2608 G_GINT64_FORMAT, range_start, range_stop);
2609 gst_query_add_buffering_range (query, range_start, range_stop);
2612 gst_query_set_buffering_percent (query, is_buffering, percent);
2613 gst_query_set_buffering_range (query, format, start, stop,
2615 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2616 byte_in_rate, byte_out_rate, buffering_left);
2621 /* peer handled other queries */
2622 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2627 gst_object_unref (queue);
2633 GST_DEBUG_OBJECT (queue, "failed peer query");
2634 gst_object_unref (queue);
2640 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2642 /* simply forward to the srcpad query function */
2643 return gst_queue2_handle_src_query (GST_QUEUE2_CAST (element)->srcpad, query);
2647 gst_queue2_update_upstream_size (GstQueue2 * queue)
2649 GstFormat fmt = GST_FORMAT_BYTES;
2650 gint64 upstream_size = -1;
2652 if (gst_pad_query_peer_duration (queue->sinkpad, &fmt, &upstream_size)) {
2653 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2654 queue->upstream_size = upstream_size;
2658 static GstFlowReturn
2659 gst_queue2_get_range (GstPad * pad, guint64 offset, guint length,
2660 GstBuffer ** buffer)
2665 queue = GST_QUEUE2_CAST (gst_pad_get_parent (pad));
2667 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2668 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2669 offset = (offset == -1) ? queue->current->reading_pos : offset;
2671 GST_DEBUG_OBJECT (queue,
2672 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2674 /* catch any reads beyond the size of the file here to make sure queue2
2675 * doesn't send seek events beyond the size of the file upstream, since
2676 * that would confuse elements such as souphttpsrc and/or http servers.
2677 * Demuxers often just loop until EOS at the end of the file to figure out
2678 * when they've read all the end-headers or index chunks. */
2679 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2680 gst_queue2_update_upstream_size (queue);
2681 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2682 goto out_unexpected;
2685 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2686 gst_queue2_update_upstream_size (queue);
2687 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2688 length = queue->upstream_size - offset;
2689 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2693 /* FIXME - function will block when the range is not yet available */
2694 ret = gst_queue2_create_read (queue, offset, length, buffer);
2695 GST_QUEUE2_MUTEX_UNLOCK (queue);
2697 gst_object_unref (queue);
2704 ret = queue->srcresult;
2706 GST_DEBUG_OBJECT (queue, "we are flushing");
2707 GST_QUEUE2_MUTEX_UNLOCK (queue);
2708 gst_object_unref (queue);
2713 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2714 GST_QUEUE2_MUTEX_UNLOCK (queue);
2715 gst_object_unref (queue);
2716 return GST_FLOW_UNEXPECTED;
2721 gst_queue2_src_checkgetrange_function (GstPad * pad)
2726 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2728 /* we can operate in pull mode when we are using a tempfile */
2729 ret = !QUEUE_IS_USING_QUEUE (queue);
2731 gst_object_unref (GST_OBJECT (queue));
2736 /* sink currently only operates in push mode */
2738 gst_queue2_sink_activate_push (GstPad * pad, gboolean active)
2740 gboolean result = TRUE;
2743 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2746 GST_QUEUE2_MUTEX_LOCK (queue);
2747 GST_DEBUG_OBJECT (queue, "activating push mode");
2748 queue->srcresult = GST_FLOW_OK;
2749 queue->sinkresult = GST_FLOW_OK;
2750 queue->is_eos = FALSE;
2751 queue->unexpected = FALSE;
2752 reset_rate_timer (queue);
2753 GST_QUEUE2_MUTEX_UNLOCK (queue);
2755 /* unblock chain function */
2756 GST_QUEUE2_MUTEX_LOCK (queue);
2757 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2758 queue->srcresult = GST_FLOW_WRONG_STATE;
2759 queue->sinkresult = GST_FLOW_WRONG_STATE;
2760 gst_queue2_locked_flush (queue);
2761 GST_QUEUE2_MUTEX_UNLOCK (queue);
2764 gst_object_unref (queue);
2769 /* src operating in push mode, we start a task on the source pad that pushes out
2770 * buffers from the queue */
2772 gst_queue2_src_activate_push (GstPad * pad, gboolean active)
2774 gboolean result = FALSE;
2777 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2780 GST_QUEUE2_MUTEX_LOCK (queue);
2781 GST_DEBUG_OBJECT (queue, "activating push mode");
2782 queue->srcresult = GST_FLOW_OK;
2783 queue->sinkresult = GST_FLOW_OK;
2784 queue->is_eos = FALSE;
2785 queue->unexpected = FALSE;
2786 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
2787 GST_QUEUE2_MUTEX_UNLOCK (queue);
2789 /* unblock loop function */
2790 GST_QUEUE2_MUTEX_LOCK (queue);
2791 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2792 queue->srcresult = GST_FLOW_WRONG_STATE;
2793 queue->sinkresult = GST_FLOW_WRONG_STATE;
2794 /* the item add signal will unblock */
2795 GST_QUEUE2_SIGNAL_ADD (queue);
2796 GST_QUEUE2_MUTEX_UNLOCK (queue);
2798 /* step 2, make sure streaming finishes */
2799 result = gst_pad_stop_task (pad);
2802 gst_object_unref (queue);
2807 /* pull mode, downstream will call our getrange function */
2809 gst_queue2_src_activate_pull (GstPad * pad, gboolean active)
2814 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2817 GST_QUEUE2_MUTEX_LOCK (queue);
2818 if (!QUEUE_IS_USING_QUEUE (queue)) {
2819 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2820 /* open the temp file now */
2821 result = gst_queue2_open_temp_location_file (queue);
2822 } else if (!queue->ring_buffer) {
2823 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
2824 result = !!queue->ring_buffer;
2829 GST_DEBUG_OBJECT (queue, "activating pull mode");
2830 init_ranges (queue);
2831 queue->srcresult = GST_FLOW_OK;
2832 queue->sinkresult = GST_FLOW_OK;
2833 queue->is_eos = FALSE;
2834 queue->unexpected = FALSE;
2835 queue->upstream_size = 0;
2836 GST_QUEUE2_MUTEX_UNLOCK (queue);
2838 GST_QUEUE2_MUTEX_LOCK (queue);
2839 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
2840 /* this is not allowed, we cannot operate in pull mode without a temp
2842 queue->srcresult = GST_FLOW_WRONG_STATE;
2843 queue->sinkresult = GST_FLOW_WRONG_STATE;
2845 GST_QUEUE2_MUTEX_UNLOCK (queue);
2848 GST_QUEUE2_MUTEX_LOCK (queue);
2849 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
2850 queue->srcresult = GST_FLOW_WRONG_STATE;
2851 queue->sinkresult = GST_FLOW_WRONG_STATE;
2852 /* this will unlock getrange */
2853 GST_QUEUE2_SIGNAL_ADD (queue);
2855 GST_QUEUE2_MUTEX_UNLOCK (queue);
2857 gst_object_unref (queue);
2862 static GstStateChangeReturn
2863 gst_queue2_change_state (GstElement * element, GstStateChange transition)
2866 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2868 queue = GST_QUEUE2 (element);
2870 switch (transition) {
2871 case GST_STATE_CHANGE_NULL_TO_READY:
2873 case GST_STATE_CHANGE_READY_TO_PAUSED:
2874 GST_QUEUE2_MUTEX_LOCK (queue);
2875 if (!QUEUE_IS_USING_QUEUE (queue)) {
2876 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2877 if (!gst_queue2_open_temp_location_file (queue))
2878 ret = GST_STATE_CHANGE_FAILURE;
2880 if (queue->ring_buffer) {
2881 g_free (queue->ring_buffer);
2882 queue->ring_buffer = NULL;
2884 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
2885 ret = GST_STATE_CHANGE_FAILURE;
2887 init_ranges (queue);
2889 queue->segment_event_received = FALSE;
2890 queue->starting_segment = NULL;
2891 GST_QUEUE2_MUTEX_UNLOCK (queue);
2893 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2899 if (ret == GST_STATE_CHANGE_FAILURE)
2902 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2904 if (ret == GST_STATE_CHANGE_FAILURE)
2907 switch (transition) {
2908 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2910 case GST_STATE_CHANGE_PAUSED_TO_READY:
2911 GST_QUEUE2_MUTEX_LOCK (queue);
2912 if (!QUEUE_IS_USING_QUEUE (queue)) {
2913 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2914 gst_queue2_close_temp_location_file (queue);
2915 } else if (queue->ring_buffer) {
2916 g_free (queue->ring_buffer);
2917 queue->ring_buffer = NULL;
2919 clean_ranges (queue);
2921 if (queue->starting_segment != NULL) {
2922 gst_event_unref (queue->starting_segment);
2923 queue->starting_segment = NULL;
2925 GST_QUEUE2_MUTEX_UNLOCK (queue);
2927 case GST_STATE_CHANGE_READY_TO_NULL:
2936 /* changing the capacity of the queue must wake up
2937 * the _chain function, it might have more room now
2938 * to store the buffer/event in the queue */
2939 #define QUEUE_CAPACITY_CHANGE(q)\
2940 GST_QUEUE2_SIGNAL_DEL (queue);
2942 /* Changing the minimum required fill level must
2943 * wake up the _loop function as it might now
2944 * be able to preceed.
2946 #define QUEUE_THRESHOLD_CHANGE(q)\
2947 GST_QUEUE2_SIGNAL_ADD (queue);
2950 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
2954 /* the element must be stopped in order to do this */
2955 GST_OBJECT_LOCK (queue);
2956 state = GST_STATE (queue);
2957 if (state != GST_STATE_READY && state != GST_STATE_NULL)
2959 GST_OBJECT_UNLOCK (queue);
2961 /* set new location */
2962 g_free (queue->temp_template);
2963 queue->temp_template = g_strdup (template);
2970 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
2971 GST_OBJECT_UNLOCK (queue);
2976 gst_queue2_set_property (GObject * object,
2977 guint prop_id, const GValue * value, GParamSpec * pspec)
2979 GstQueue2 *queue = GST_QUEUE2 (object);
2981 /* someone could change levels here, and since this
2982 * affects the get/put funcs, we need to lock for safety. */
2983 GST_QUEUE2_MUTEX_LOCK (queue);
2986 case PROP_MAX_SIZE_BYTES:
2987 queue->max_level.bytes = g_value_get_uint (value);
2988 QUEUE_CAPACITY_CHANGE (queue);
2990 case PROP_MAX_SIZE_BUFFERS:
2991 queue->max_level.buffers = g_value_get_uint (value);
2992 QUEUE_CAPACITY_CHANGE (queue);
2994 case PROP_MAX_SIZE_TIME:
2995 queue->max_level.time = g_value_get_uint64 (value);
2996 /* set rate_time to the same value. We use an extra field in the level
2997 * structure so that we can easily access and compare it */
2998 queue->max_level.rate_time = queue->max_level.time;
2999 QUEUE_CAPACITY_CHANGE (queue);
3001 case PROP_USE_BUFFERING:
3002 queue->use_buffering = g_value_get_boolean (value);
3004 case PROP_USE_RATE_ESTIMATE:
3005 queue->use_rate_estimate = g_value_get_boolean (value);
3007 case PROP_LOW_PERCENT:
3008 queue->low_percent = g_value_get_int (value);
3010 case PROP_HIGH_PERCENT:
3011 queue->high_percent = g_value_get_int (value);
3013 case PROP_TEMP_TEMPLATE:
3014 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3016 case PROP_TEMP_LOCATION:
3017 g_free (queue->temp_location);
3018 queue->temp_location = g_value_dup_string (value);
3019 /* you can set the property back to NULL to make it use the temp-tmpl
3021 queue->temp_location_set = queue->temp_location != NULL;
3023 case PROP_TEMP_REMOVE:
3024 queue->temp_remove = g_value_get_boolean (value);
3026 case PROP_RING_BUFFER_MAX_SIZE:
3027 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3030 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3034 GST_QUEUE2_MUTEX_UNLOCK (queue);
3038 gst_queue2_get_property (GObject * object,
3039 guint prop_id, GValue * value, GParamSpec * pspec)
3041 GstQueue2 *queue = GST_QUEUE2 (object);
3043 GST_QUEUE2_MUTEX_LOCK (queue);
3046 case PROP_CUR_LEVEL_BYTES:
3047 g_value_set_uint (value, queue->cur_level.bytes);
3049 case PROP_CUR_LEVEL_BUFFERS:
3050 g_value_set_uint (value, queue->cur_level.buffers);
3052 case PROP_CUR_LEVEL_TIME:
3053 g_value_set_uint64 (value, queue->cur_level.time);
3055 case PROP_MAX_SIZE_BYTES:
3056 g_value_set_uint (value, queue->max_level.bytes);
3058 case PROP_MAX_SIZE_BUFFERS:
3059 g_value_set_uint (value, queue->max_level.buffers);
3061 case PROP_MAX_SIZE_TIME:
3062 g_value_set_uint64 (value, queue->max_level.time);
3064 case PROP_USE_BUFFERING:
3065 g_value_set_boolean (value, queue->use_buffering);
3067 case PROP_USE_RATE_ESTIMATE:
3068 g_value_set_boolean (value, queue->use_rate_estimate);
3070 case PROP_LOW_PERCENT:
3071 g_value_set_int (value, queue->low_percent);
3073 case PROP_HIGH_PERCENT:
3074 g_value_set_int (value, queue->high_percent);
3076 case PROP_TEMP_TEMPLATE:
3077 g_value_set_string (value, queue->temp_template);
3079 case PROP_TEMP_LOCATION:
3080 g_value_set_string (value, queue->temp_location);
3082 case PROP_TEMP_REMOVE:
3083 g_value_set_boolean (value, queue->temp_remove);
3085 case PROP_RING_BUFFER_MAX_SIZE:
3086 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3093 GST_QUEUE2_MUTEX_UNLOCK (queue);