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_push_one (GstQueue2 * queue);
228 static void gst_queue2_loop (GstPad * pad);
230 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event);
231 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstQuery * query);
233 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstEvent * event);
234 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstQuery * query);
235 static gboolean gst_queue2_handle_query (GstElement * element,
238 static GstFlowReturn gst_queue2_get_range (GstPad * pad, guint64 offset,
239 guint length, GstBuffer ** buffer);
241 static gboolean gst_queue2_src_activate_pull (GstPad * pad, gboolean active);
242 static gboolean gst_queue2_src_activate_push (GstPad * pad, gboolean active);
243 static gboolean gst_queue2_sink_activate_push (GstPad * pad, gboolean active);
244 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
245 GstStateChange transition);
247 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
248 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
250 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
252 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
255 gst_queue2_class_init (GstQueue2Class * klass)
257 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
258 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
260 gobject_class->set_property = gst_queue2_set_property;
261 gobject_class->get_property = gst_queue2_get_property;
264 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
265 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
266 "Current amount of data in the queue (bytes)",
267 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
268 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
269 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
270 "Current number of buffers in the queue",
271 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
272 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
273 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
274 "Current amount of data in the queue (in ns)",
275 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
277 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
278 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
279 "Max. amount of data in the queue (bytes, 0=disable)",
280 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
281 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
283 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
284 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
285 DEFAULT_MAX_SIZE_BUFFERS,
286 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
288 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
289 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
290 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
293 g_param_spec_boolean ("use-buffering", "Use buffering",
294 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
295 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
297 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
298 "Estimate the bitrate of the stream to calculate time level",
299 DEFAULT_USE_RATE_ESTIMATE,
300 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
302 g_param_spec_int ("low-percent", "Low percent",
303 "Low threshold for buffering to start. Only used if use-buffering is True",
304 0, 100, DEFAULT_LOW_PERCENT,
305 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
307 g_param_spec_int ("high-percent", "High percent",
308 "High threshold for buffering to finish. Only used if use-buffering is True",
309 0, 100, DEFAULT_HIGH_PERCENT,
310 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
313 g_param_spec_string ("temp-template", "Temporary File Template",
314 "File template to store temporary files in, should contain directory "
315 "and XXXXXX. (NULL == disabled)",
316 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
319 g_param_spec_string ("temp-location", "Temporary File Location",
320 "Location to store temporary files in (Deprecated: Only read this "
321 "property, use temp-template to configure the name template)",
322 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
325 * GstQueue2:temp-remove
327 * When temp-template is set, remove the temporary file when going to READY.
331 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
332 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
333 "Remove the temp-location after use",
334 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 * GstQueue2:ring-buffer-max-size
339 * The maximum size of the ring buffer in bytes. If set to 0, the ring
340 * buffer is disabled. Default 0.
344 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
345 g_param_spec_uint64 ("ring-buffer-max-size",
346 "Max. ring buffer size (bytes)",
347 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
348 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 /* set several parent class virtual functions */
352 gobject_class->finalize = gst_queue2_finalize;
354 gst_element_class_add_pad_template (gstelement_class,
355 gst_static_pad_template_get (&srctemplate));
356 gst_element_class_add_pad_template (gstelement_class,
357 gst_static_pad_template_get (&sinktemplate));
359 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
362 "Erik Walthinsen <omega@cse.ogi.edu>, "
363 "Wim Taymans <wim.taymans@gmail.com>");
365 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
366 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
370 gst_queue2_init (GstQueue2 * queue)
372 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
374 gst_pad_set_chain_function (queue->sinkpad,
375 GST_DEBUG_FUNCPTR (gst_queue2_chain));
376 gst_pad_set_activatepush_function (queue->sinkpad,
377 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_push));
378 gst_pad_set_event_function (queue->sinkpad,
379 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
380 gst_pad_set_query_function (queue->sinkpad,
381 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
382 GST_OBJECT_FLAG_SET (queue->sinkpad, GST_PAD_PROXY_CAPS);
383 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
385 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
387 gst_pad_set_activatepull_function (queue->srcpad,
388 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_pull));
389 gst_pad_set_activatepush_function (queue->srcpad,
390 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_push));
391 gst_pad_set_getrange_function (queue->srcpad,
392 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
393 gst_pad_set_event_function (queue->srcpad,
394 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
395 gst_pad_set_query_function (queue->srcpad,
396 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
397 GST_OBJECT_FLAG_SET (queue->srcpad, GST_PAD_PROXY_CAPS);
398 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
401 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
402 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
403 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
404 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
405 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
406 queue->use_buffering = DEFAULT_USE_BUFFERING;
407 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
408 queue->low_percent = DEFAULT_LOW_PERCENT;
409 queue->high_percent = DEFAULT_HIGH_PERCENT;
411 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
412 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
414 queue->sinktime = GST_CLOCK_TIME_NONE;
415 queue->srctime = GST_CLOCK_TIME_NONE;
416 queue->sink_tainted = TRUE;
417 queue->src_tainted = TRUE;
419 queue->srcresult = GST_FLOW_WRONG_STATE;
420 queue->sinkresult = GST_FLOW_WRONG_STATE;
421 queue->is_eos = FALSE;
422 queue->in_timer = g_timer_new ();
423 queue->out_timer = g_timer_new ();
425 queue->qlock = g_mutex_new ();
426 queue->waiting_add = FALSE;
427 queue->item_add = g_cond_new ();
428 queue->waiting_del = FALSE;
429 queue->item_del = g_cond_new ();
430 g_queue_init (&queue->queue);
432 queue->buffering_percent = 100;
434 /* tempfile related */
435 queue->temp_template = NULL;
436 queue->temp_location = NULL;
437 queue->temp_location_set = FALSE;
438 queue->temp_remove = DEFAULT_TEMP_REMOVE;
440 queue->ring_buffer = NULL;
441 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
443 GST_DEBUG_OBJECT (queue,
444 "initialized queue's not_empty & not_full conditions");
447 /* called only once, as opposed to dispose */
449 gst_queue2_finalize (GObject * object)
451 GstQueue2 *queue = GST_QUEUE2 (object);
453 GST_DEBUG_OBJECT (queue, "finalizing queue");
455 while (!g_queue_is_empty (&queue->queue)) {
456 GstMiniObject *data = g_queue_pop_head (&queue->queue);
458 gst_mini_object_unref (data);
461 g_queue_clear (&queue->queue);
462 g_mutex_free (queue->qlock);
463 g_cond_free (queue->item_add);
464 g_cond_free (queue->item_del);
465 g_timer_destroy (queue->in_timer);
466 g_timer_destroy (queue->out_timer);
468 /* temp_file path cleanup */
469 g_free (queue->temp_template);
470 g_free (queue->temp_location);
472 G_OBJECT_CLASS (parent_class)->finalize (object);
476 debug_ranges (GstQueue2 * queue)
478 GstQueue2Range *walk;
480 for (walk = queue->ranges; walk; walk = walk->next) {
481 GST_DEBUG_OBJECT (queue,
482 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
483 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
484 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
485 walk->rb_writing_pos, walk->reading_pos,
486 walk == queue->current ? "**y**" : " n ");
490 /* clear all the downloaded ranges */
492 clean_ranges (GstQueue2 * queue)
494 GST_DEBUG_OBJECT (queue, "clean queue ranges");
496 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
497 queue->ranges = NULL;
498 queue->current = NULL;
501 /* find a range that contains @offset or NULL when nothing does */
502 static GstQueue2Range *
503 find_range (GstQueue2 * queue, guint64 offset)
505 GstQueue2Range *range = NULL;
506 GstQueue2Range *walk;
508 /* first do a quick check for the current range */
509 for (walk = queue->ranges; walk; walk = walk->next) {
510 if (offset >= walk->offset && offset <= walk->writing_pos) {
511 /* we can reuse an existing range */
517 GST_DEBUG_OBJECT (queue,
518 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
519 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
521 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
527 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
529 guint64 max_reading_pos, writing_pos;
531 writing_pos = range->writing_pos;
532 max_reading_pos = range->max_reading_pos;
534 if (writing_pos > max_reading_pos)
535 queue->cur_level.bytes = writing_pos - max_reading_pos;
537 queue->cur_level.bytes = 0;
540 /* make a new range for @offset or reuse an existing range */
541 static GstQueue2Range *
542 add_range (GstQueue2 * queue, guint64 offset)
544 GstQueue2Range *range, *prev, *next;
546 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
548 if ((range = find_range (queue, offset))) {
549 GST_DEBUG_OBJECT (queue,
550 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
552 range->writing_pos = offset;
554 GST_DEBUG_OBJECT (queue,
555 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
557 range = g_slice_new0 (GstQueue2Range);
558 range->offset = offset;
559 /* we want to write to the next location in the ring buffer */
560 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
561 range->writing_pos = offset;
562 range->rb_writing_pos = range->rb_offset;
563 range->reading_pos = offset;
564 range->max_reading_pos = offset;
568 next = queue->ranges;
570 if (next->offset > offset) {
571 /* insert before next */
572 GST_DEBUG_OBJECT (queue,
573 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
585 queue->ranges = range;
587 debug_ranges (queue);
589 /* update the stats for this range */
590 update_cur_level (queue, range);
596 /* clear and init the download ranges for offset 0 */
598 init_ranges (GstQueue2 * queue)
600 GST_DEBUG_OBJECT (queue, "init queue ranges");
602 /* get rid of all the current ranges */
603 clean_ranges (queue);
604 /* make a range for offset 0 */
605 queue->current = add_range (queue, 0);
608 /* calculate the diff between running time on the sink and src of the queue.
609 * This is the total amount of time in the queue. */
611 update_time_level (GstQueue2 * queue)
613 if (queue->sink_tainted) {
615 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
616 queue->sink_segment.position);
617 queue->sink_tainted = FALSE;
620 if (queue->src_tainted) {
622 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
623 queue->src_segment.position);
624 queue->src_tainted = FALSE;
627 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
628 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
630 if (queue->sinktime != GST_CLOCK_TIME_NONE
631 && queue->srctime != GST_CLOCK_TIME_NONE
632 && queue->sinktime >= queue->srctime)
633 queue->cur_level.time = queue->sinktime - queue->srctime;
635 queue->cur_level.time = 0;
638 /* take a SEGMENT event and apply the values to segment, updating the time
641 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
644 gst_event_copy_segment (event, segment);
646 if (segment->format == GST_FORMAT_BYTES) {
647 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
648 /* start is where we'll be getting from and as such writing next */
649 queue->current = add_range (queue, segment->start);
650 /* update the stats for this range */
651 update_cur_level (queue, queue->current);
655 /* now configure the values, we use these to track timestamps on the
657 if (segment->format != GST_FORMAT_TIME) {
658 /* non-time format, pretent the current time segment is closed with a
659 * 0 start and unknown stop time. */
660 segment->format = GST_FORMAT_TIME;
666 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
669 queue->sink_tainted = TRUE;
671 queue->src_tainted = TRUE;
673 /* segment can update the time level of the queue */
674 update_time_level (queue);
677 /* take a buffer and update segment, updating the time level of the queue. */
679 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
682 GstClockTime duration, timestamp;
684 timestamp = GST_BUFFER_TIMESTAMP (buffer);
685 duration = GST_BUFFER_DURATION (buffer);
687 /* if no timestamp is set, assume it's continuous with the previous
689 if (timestamp == GST_CLOCK_TIME_NONE)
690 timestamp = segment->position;
693 if (duration != GST_CLOCK_TIME_NONE)
694 timestamp += duration;
696 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
697 GST_TIME_ARGS (timestamp));
699 segment->position = timestamp;
702 queue->sink_tainted = TRUE;
704 queue->src_tainted = TRUE;
706 /* calc diff with other end */
707 update_time_level (queue);
711 update_buffering (GstQueue2 * queue)
714 gboolean post = FALSE;
716 if (queue->high_percent <= 0)
719 #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)
722 /* on EOS we are always 100% full, we set the var here so that it we can
723 * reuse the logic below to stop buffering */
725 GST_LOG_OBJECT (queue, "we are EOS");
727 /* figure out the percent we are filled, we take the max of all formats. */
729 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
730 percent = GET_PERCENT (bytes, 0);
732 guint64 rb_size = queue->ring_buffer_max_size;
733 percent = GET_PERCENT (bytes, rb_size);
735 percent = MAX (percent, GET_PERCENT (time, 0));
736 percent = MAX (percent, GET_PERCENT (buffers, 0));
738 /* also apply the rate estimate when we need to */
739 if (queue->use_rate_estimate)
740 percent = MAX (percent, GET_PERCENT (rate_time, 0));
743 if (queue->is_buffering) {
745 /* if we were buffering see if we reached the high watermark */
746 if (percent >= queue->high_percent)
747 queue->is_buffering = FALSE;
749 /* we were not buffering, check if we need to start buffering if we drop
750 * below the low threshold */
751 if (percent < queue->low_percent) {
752 queue->is_buffering = TRUE;
753 queue->buffering_iteration++;
759 GstBufferingMode mode;
760 gint64 buffering_left = -1;
762 /* scale to high percent so that it becomes the 100% mark */
763 percent = percent * 100 / queue->high_percent;
768 if (percent != queue->buffering_percent) {
769 queue->buffering_percent = percent;
771 if (!QUEUE_IS_USING_QUEUE (queue)) {
774 if (QUEUE_IS_USING_RING_BUFFER (queue))
775 mode = GST_BUFFERING_TIMESHIFT;
777 mode = GST_BUFFERING_DOWNLOAD;
779 if (queue->byte_in_rate > 0) {
780 if (gst_pad_query_peer_duration (queue->sinkpad, GST_FORMAT_BYTES,
783 (gdouble) ((duration -
784 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
787 buffering_left = G_MAXINT64;
790 mode = GST_BUFFERING_STREAM;
793 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
794 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
796 gst_message_set_buffering_stats (message, mode,
797 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
799 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
802 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
809 reset_rate_timer (GstQueue2 * queue)
812 queue->bytes_out = 0;
813 queue->byte_in_rate = 0.0;
814 queue->byte_in_period = 0;
815 queue->byte_out_rate = 0.0;
816 queue->last_in_elapsed = 0.0;
817 queue->last_out_elapsed = 0.0;
818 queue->in_timer_started = FALSE;
819 queue->out_timer_started = FALSE;
822 /* the interval in seconds to recalculate the rate */
823 #define RATE_INTERVAL 0.2
824 /* Tuning for rate estimation. We use a large window for the input rate because
825 * it should be stable when connected to a network. The output rate is less
826 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
827 * therefore adapt more quickly.
828 * However, initial input rate may be subject to a burst, and should therefore
829 * initially also adapt more quickly to changes, and only later on give higher
830 * weight to previous values. */
831 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
832 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
835 update_in_rates (GstQueue2 * queue)
837 gdouble elapsed, period;
838 gdouble byte_in_rate;
840 if (!queue->in_timer_started) {
841 queue->in_timer_started = TRUE;
842 g_timer_start (queue->in_timer);
846 elapsed = g_timer_elapsed (queue->in_timer, NULL);
848 /* recalc after each interval. */
849 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
850 period = elapsed - queue->last_in_elapsed;
852 GST_DEBUG_OBJECT (queue,
853 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
854 period, queue->bytes_in, queue->byte_in_period);
856 byte_in_rate = queue->bytes_in / period;
858 if (queue->byte_in_rate == 0.0)
859 queue->byte_in_rate = byte_in_rate;
861 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
862 (double) queue->byte_in_period, period);
864 /* another data point, cap at 16 for long time running average */
865 if (queue->byte_in_period < 16 * RATE_INTERVAL)
866 queue->byte_in_period += period;
868 /* reset the values to calculate rate over the next interval */
869 queue->last_in_elapsed = elapsed;
873 if (queue->byte_in_rate > 0.0) {
874 queue->cur_level.rate_time =
875 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
877 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
878 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
882 update_out_rates (GstQueue2 * queue)
884 gdouble elapsed, period;
885 gdouble byte_out_rate;
887 if (!queue->out_timer_started) {
888 queue->out_timer_started = TRUE;
889 g_timer_start (queue->out_timer);
893 elapsed = g_timer_elapsed (queue->out_timer, NULL);
895 /* recalc after each interval. */
896 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
897 period = elapsed - queue->last_out_elapsed;
899 GST_DEBUG_OBJECT (queue,
900 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
902 byte_out_rate = queue->bytes_out / period;
904 if (queue->byte_out_rate == 0.0)
905 queue->byte_out_rate = byte_out_rate;
907 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
909 /* reset the values to calculate rate over the next interval */
910 queue->last_out_elapsed = elapsed;
911 queue->bytes_out = 0;
913 if (queue->byte_in_rate > 0.0) {
914 queue->cur_level.rate_time =
915 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
917 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
918 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
922 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
924 guint64 reading_pos, max_reading_pos;
927 max_reading_pos = range->max_reading_pos;
929 max_reading_pos = MAX (max_reading_pos, reading_pos);
931 GST_DEBUG_OBJECT (queue,
932 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
933 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
934 range->max_reading_pos = max_reading_pos;
936 update_cur_level (queue, range);
940 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
945 GST_QUEUE2_MUTEX_UNLOCK (queue);
947 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
950 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
951 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
952 GST_SEEK_TYPE_NONE, -1);
954 res = gst_pad_push_event (queue->sinkpad, event);
955 GST_QUEUE2_MUTEX_LOCK (queue);
958 queue->current = add_range (queue, offset);
963 /* see if there is enough data in the file to read a full buffer */
965 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
967 GstQueue2Range *range;
969 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
972 if ((range = find_range (queue, offset))) {
973 if (queue->current != range) {
974 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
975 perform_seek_to_offset (queue, range->writing_pos);
978 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
979 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
981 /* we have a range for offset */
982 GST_DEBUG_OBJECT (queue,
983 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
984 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
986 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
989 if (offset + length <= range->writing_pos)
992 GST_DEBUG_OBJECT (queue,
993 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
994 (offset + length) - range->writing_pos);
997 GST_INFO_OBJECT (queue, "not found in any range");
998 /* we don't have the range, see how far away we are, FIXME, find a good
999 * threshold based on the incoming rate. */
1000 if (!queue->is_eos && queue->current) {
1001 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1002 if (offset < queue->current->offset || offset >
1003 queue->current->writing_pos + QUEUE_MAX_BYTES (queue) -
1004 queue->cur_level.bytes) {
1005 perform_seek_to_offset (queue, offset);
1007 GST_INFO_OBJECT (queue,
1008 "requested data is within range, wait for data");
1010 } else if (offset < queue->current->writing_pos + 200000) {
1011 update_cur_pos (queue, queue->current, offset + length);
1012 GST_INFO_OBJECT (queue, "wait for data");
1017 /* too far away, do a seek */
1018 perform_seek_to_offset (queue, offset);
1025 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1026 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1027 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1029 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1032 static GstFlowReturn
1033 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1034 guint8 * dst, gint64 * read_return)
1036 guint8 *ring_buffer;
1039 ring_buffer = queue->ring_buffer;
1041 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1044 /* this should not block */
1045 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1047 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1048 res = fread (dst, 1, length, queue->temp_file);
1050 memcpy (dst, ring_buffer + offset, length);
1054 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1056 if (G_UNLIKELY (res < length)) {
1057 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1058 goto could_not_read;
1059 /* check for errors or EOF */
1060 if (ferror (queue->temp_file))
1061 goto could_not_read;
1062 if (feof (queue->temp_file) && length > 0)
1072 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1073 return GST_FLOW_ERROR;
1077 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1078 return GST_FLOW_ERROR;
1082 GST_DEBUG ("non-regular file hits EOS");
1083 return GST_FLOW_EOS;
1087 static GstFlowReturn
1088 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1089 GstBuffer ** buffer)
1093 guint64 file_offset;
1094 guint block_length, remaining, read_length;
1097 GstFlowReturn ret = GST_FLOW_OK;
1099 /* allocate the output buffer of the requested size */
1100 buf = gst_buffer_new_allocate (NULL, length, 0);
1101 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
1103 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1107 rb_size = queue->ring_buffer_max_size;
1110 while (remaining > 0) {
1111 /* configure how much/whether to read */
1112 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1115 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1118 /* calculate how far away the offset is */
1119 if (queue->current->writing_pos > rpos)
1120 level = queue->current->writing_pos - rpos;
1124 GST_DEBUG_OBJECT (queue,
1125 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1126 ", level %" G_GUINT64_FORMAT,
1127 rpos, queue->current->writing_pos, level);
1129 if (level >= rb_size) {
1130 /* we don't have the data but if we have a ring buffer that is full, we
1132 GST_DEBUG_OBJECT (queue,
1133 "ring buffer full, reading ring-buffer-max-size %"
1134 G_GUINT64_FORMAT " bytes", rb_size);
1135 read_length = rb_size;
1136 } else if (queue->is_eos) {
1137 /* won't get any more data so read any data we have */
1139 GST_DEBUG_OBJECT (queue,
1140 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1142 read_length = level;
1148 if (read_length == 0) {
1149 if (QUEUE_IS_USING_RING_BUFFER (queue)
1150 && queue->current->max_reading_pos > rpos) {
1151 /* protect cached data (data between offset and max_reading_pos)
1152 * and update current level */
1153 GST_DEBUG_OBJECT (queue,
1154 "protecting cached data [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1155 "]", rpos, queue->current->max_reading_pos);
1156 queue->current->max_reading_pos = rpos;
1157 update_cur_level (queue, queue->current);
1159 GST_DEBUG_OBJECT (queue, "waiting for add");
1160 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1164 /* we have the requested data so read it */
1165 read_length = remaining;
1168 /* set range reading_pos to actual reading position for this read */
1169 queue->current->reading_pos = rpos;
1171 /* configure how much and from where to read */
1172 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1174 (queue->current->rb_offset + (rpos -
1175 queue->current->offset)) % rb_size;
1176 if (file_offset + read_length > rb_size) {
1177 block_length = rb_size - file_offset;
1179 block_length = read_length;
1183 block_length = read_length;
1186 /* while we still have data to read, we loop */
1187 while (read_length > 0) {
1191 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1192 data, &read_return);
1193 if (ret != GST_FLOW_OK)
1196 file_offset += read_return;
1197 if (QUEUE_IS_USING_RING_BUFFER (queue))
1198 file_offset %= rb_size;
1200 data += read_return;
1201 read_length -= read_return;
1202 block_length = read_length;
1203 remaining -= read_return;
1205 rpos = (queue->current->reading_pos += read_return);
1206 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1208 GST_QUEUE2_SIGNAL_DEL (queue);
1209 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1212 gst_buffer_unmap (buf, data, length);
1214 GST_BUFFER_OFFSET (buf) = offset;
1215 GST_BUFFER_OFFSET_END (buf) = offset + length;
1224 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1225 gst_buffer_unref (buf);
1226 return GST_FLOW_EOS;
1230 GST_DEBUG_OBJECT (queue, "we are flushing");
1231 gst_buffer_unref (buf);
1232 return GST_FLOW_WRONG_STATE;
1236 GST_DEBUG_OBJECT (queue, "we have a read error");
1237 gst_buffer_unmap (buf, data, 0);
1238 gst_buffer_unref (buf);
1243 /* should be called with QUEUE_LOCK */
1244 static GstMiniObject *
1245 gst_queue2_read_item_from_file (GstQueue2 * queue)
1247 GstMiniObject *item;
1249 if (queue->starting_segment != NULL) {
1250 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1251 queue->starting_segment = NULL;
1255 guint64 reading_pos;
1257 reading_pos = queue->current->reading_pos;
1260 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1265 item = GST_MINI_OBJECT_CAST (buffer);
1268 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1278 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1279 * the temp filename. */
1281 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1286 if (queue->temp_file)
1287 goto already_opened;
1289 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1291 /* we have two cases:
1292 * - temp_location was set to something !NULL (Deprecated). in this case we
1293 * open the specified filename.
1294 * - temp_template was set, allocate a filename and open that filename
1296 if (!queue->temp_location_set) {
1298 if (queue->temp_template == NULL)
1301 /* make copy of the template, we don't want to change this */
1302 name = g_strdup (queue->temp_template);
1303 fd = g_mkstemp (name);
1305 goto mkstemp_failed;
1307 /* open the file for update/writing */
1308 queue->temp_file = fdopen (fd, "wb+");
1309 /* error creating file */
1310 if (queue->temp_file == NULL)
1313 g_free (queue->temp_location);
1314 queue->temp_location = name;
1316 GST_QUEUE2_MUTEX_UNLOCK (queue);
1318 /* we can't emit the notify with the lock */
1319 g_object_notify (G_OBJECT (queue), "temp-location");
1321 GST_QUEUE2_MUTEX_LOCK (queue);
1323 /* open the file for update/writing, this is deprecated but we still need to
1324 * support it for API/ABI compatibility */
1325 queue->temp_file = g_fopen (queue->temp_location, "wb+");
1326 /* error creating file */
1327 if (queue->temp_file == NULL)
1330 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1337 GST_DEBUG_OBJECT (queue, "temp file was already open");
1342 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1343 (_("No Temp directory specified.")), (NULL));
1348 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1349 (_("Could not create temp file \"%s\"."), queue->temp_template),
1356 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1357 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1366 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1369 if (queue->temp_file == NULL)
1372 GST_DEBUG_OBJECT (queue, "closing temp file");
1374 fflush (queue->temp_file);
1375 fclose (queue->temp_file);
1377 if (queue->temp_remove)
1378 remove (queue->temp_location);
1380 queue->temp_file = NULL;
1381 clean_ranges (queue);
1385 gst_queue2_flush_temp_file (GstQueue2 * queue)
1387 if (queue->temp_file == NULL)
1390 GST_DEBUG_OBJECT (queue, "flushing temp file");
1392 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1396 gst_queue2_locked_flush (GstQueue2 * queue)
1398 if (!QUEUE_IS_USING_QUEUE (queue)) {
1399 if (QUEUE_IS_USING_TEMP_FILE (queue))
1400 gst_queue2_flush_temp_file (queue);
1401 init_ranges (queue);
1403 while (!g_queue_is_empty (&queue->queue)) {
1404 GstMiniObject *data = g_queue_pop_head (&queue->queue);
1406 /* Then lose another reference because we are supposed to destroy that
1407 data when flushing */
1408 gst_mini_object_unref (data);
1411 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1412 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1413 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1414 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1415 queue->sink_tainted = queue->src_tainted = TRUE;
1416 if (queue->starting_segment != NULL)
1417 gst_event_unref (queue->starting_segment);
1418 queue->starting_segment = NULL;
1419 queue->segment_event_received = FALSE;
1421 /* we deleted a lot of something */
1422 GST_QUEUE2_SIGNAL_DEL (queue);
1426 gst_queue2_wait_free_space (GstQueue2 * queue)
1428 /* We make space available if we're "full" according to whatever
1429 * the user defined as "full". */
1430 if (gst_queue2_is_filled (queue)) {
1433 /* pause the timer while we wait. The fact that we are waiting does not mean
1434 * the byterate on the input pad is lower */
1435 if ((started = queue->in_timer_started))
1436 g_timer_stop (queue->in_timer);
1438 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1439 "queue is full, waiting for free space");
1441 /* Wait for space to be available, we could be unlocked because of a flush. */
1442 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1444 while (gst_queue2_is_filled (queue));
1446 /* and continue if we were running before */
1448 g_timer_continue (queue->in_timer);
1455 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1461 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1463 guint8 *odata, *data, *ring_buffer;
1464 guint size, rb_size;
1466 guint64 writing_pos, new_writing_pos;
1467 GstQueue2Range *range, *prev, *next;
1469 if (QUEUE_IS_USING_RING_BUFFER (queue))
1470 writing_pos = queue->current->rb_writing_pos;
1472 writing_pos = queue->current->writing_pos;
1473 ring_buffer = queue->ring_buffer;
1474 rb_size = queue->ring_buffer_max_size;
1476 odata = gst_buffer_map (buffer, &osize, NULL, GST_MAP_READ);
1481 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1482 GST_BUFFER_OFFSET (buffer));
1487 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1490 /* calculate the space in the ring buffer not used by data from
1491 * the current range */
1492 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1493 /* wait until there is some free space */
1494 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1496 /* get the amount of space we have */
1497 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1499 /* calculate if we need to split or if we can write the entire
1501 to_write = MIN (size, space);
1503 /* the writing position in the ring buffer after writing (part
1504 * or all of) the buffer */
1505 new_writing_pos = (writing_pos + to_write) % rb_size;
1508 range = queue->ranges;
1510 /* if we need to overwrite data in the ring buffer, we need to
1513 * warning: this code is complicated and includes some
1514 * simplifications - pen, paper and diagrams for the cases
1517 guint64 range_data_start, range_data_end;
1518 GstQueue2Range *range_to_destroy = NULL;
1520 range_data_start = range->rb_offset;
1521 range_data_end = range->rb_writing_pos;
1523 /* handle the special case where the range has no data in it */
1524 if (range->writing_pos == range->offset) {
1525 if (range != queue->current) {
1526 GST_DEBUG_OBJECT (queue,
1527 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1528 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1530 range_to_destroy = range;
1532 prev->next = range->next;
1537 if (range_data_end > range_data_start) {
1538 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1541 if (new_writing_pos > range_data_start) {
1542 if (new_writing_pos >= range_data_end) {
1543 GST_DEBUG_OBJECT (queue,
1544 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1545 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1547 range_to_destroy = range;
1549 prev->next = range->next;
1551 GST_DEBUG_OBJECT (queue,
1552 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1553 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1554 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1555 range->offset + new_writing_pos - range_data_start,
1557 range->offset += (new_writing_pos - range_data_start);
1558 range->rb_offset = new_writing_pos;
1562 guint64 new_wpos_virt = writing_pos + to_write;
1564 if (new_wpos_virt <= range_data_start)
1567 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1568 GST_DEBUG_OBJECT (queue,
1569 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1570 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1572 range_to_destroy = range;
1574 prev->next = range->next;
1576 GST_DEBUG_OBJECT (queue,
1577 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1578 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1579 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1580 range->offset + new_writing_pos - range_data_start,
1582 range->offset += (new_wpos_virt - range_data_start);
1583 range->rb_offset = new_writing_pos;
1588 if (!range_to_destroy)
1591 range = range->next;
1592 if (range_to_destroy) {
1593 if (range_to_destroy == queue->ranges)
1594 queue->ranges = range;
1595 g_slice_free (GstQueue2Range, range_to_destroy);
1596 range_to_destroy = NULL;
1601 new_writing_pos = writing_pos + to_write;
1604 if (QUEUE_IS_USING_TEMP_FILE (queue)
1605 && FSEEK_FILE (queue->temp_file, writing_pos))
1608 if (new_writing_pos > writing_pos) {
1609 GST_INFO_OBJECT (queue,
1610 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1611 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1612 queue->current->writing_pos, queue->current->rb_writing_pos);
1613 /* either not using ring buffer or no wrapping, just write */
1614 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1615 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1618 memcpy (ring_buffer + writing_pos, data, to_write);
1621 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1622 /* try to merge with next range */
1623 while ((next = queue->current->next)) {
1624 GST_INFO_OBJECT (queue,
1625 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1626 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1627 if (new_writing_pos < next->offset)
1630 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1633 /* remove the group, we could choose to not read the data in this range
1634 * again. This would involve us doing a seek to the current writing position
1635 * in the range. FIXME, It would probably make sense to do a seek when there
1636 * is a lot of data in the range we merged with to avoid reading it all
1638 queue->current->next = next->next;
1639 g_slice_free (GstQueue2Range, next);
1641 debug_ranges (queue);
1643 goto update_and_signal;
1647 guint block_one, block_two;
1649 block_one = rb_size - writing_pos;
1650 block_two = to_write - block_one;
1652 if (block_one > 0) {
1653 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1654 /* write data to end of ring buffer */
1655 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1656 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1659 memcpy (ring_buffer + writing_pos, data, block_one);
1663 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1666 if (block_two > 0) {
1667 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1668 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1669 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1672 memcpy (ring_buffer, data + block_one, block_two);
1678 /* update the writing positions */
1680 GST_INFO_OBJECT (queue,
1681 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1682 to_write, writing_pos, size);
1684 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1686 queue->current->writing_pos += to_write;
1687 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1689 queue->current->writing_pos = writing_pos = new_writing_pos;
1691 update_cur_level (queue, queue->current);
1693 /* update the buffering status */
1694 if (queue->use_buffering)
1695 update_buffering (queue);
1697 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1698 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1700 GST_QUEUE2_SIGNAL_ADD (queue);
1703 gst_buffer_unmap (buffer, odata, osize);
1710 GST_DEBUG_OBJECT (queue, "we are flushing");
1711 gst_buffer_unmap (buffer, odata, osize);
1712 /* FIXME - GST_FLOW_EOS ? */
1717 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1718 gst_buffer_unmap (buffer, odata, osize);
1725 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1729 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1730 (_("Error while writing to download file.")),
1731 ("%s", g_strerror (errno)));
1734 gst_buffer_unmap (buffer, odata, osize);
1739 /* enqueue an item an update the level stats */
1741 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item, gboolean isbuffer)
1747 buffer = GST_BUFFER_CAST (item);
1748 size = gst_buffer_get_size (buffer);
1750 /* add buffer to the statistics */
1751 if (QUEUE_IS_USING_QUEUE (queue)) {
1752 queue->cur_level.buffers++;
1753 queue->cur_level.bytes += size;
1755 queue->bytes_in += size;
1757 /* apply new buffer to segment stats */
1758 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1759 /* update the byterate stats */
1760 update_in_rates (queue);
1762 if (!QUEUE_IS_USING_QUEUE (queue)) {
1763 /* FIXME - check return value? */
1764 gst_queue2_create_write (queue, buffer);
1766 } else if (GST_IS_EVENT (item)) {
1769 event = GST_EVENT_CAST (item);
1771 switch (GST_EVENT_TYPE (event)) {
1773 /* Zero the thresholds, this makes sure the queue is completely
1774 * filled and we can read all data from the queue. */
1775 GST_DEBUG_OBJECT (queue, "we have EOS");
1776 queue->is_eos = TRUE;
1778 case GST_EVENT_SEGMENT:
1779 apply_segment (queue, event, &queue->sink_segment, TRUE);
1780 /* This is our first new segment, we hold it
1781 * as we can't save it on the temp file */
1782 if (!QUEUE_IS_USING_QUEUE (queue)) {
1783 if (queue->segment_event_received)
1784 goto unexpected_event;
1786 queue->segment_event_received = TRUE;
1787 if (queue->starting_segment != NULL)
1788 gst_event_unref (queue->starting_segment);
1789 queue->starting_segment = event;
1792 /* a new segment allows us to accept more buffers if we got EOS
1793 * from downstream */
1794 queue->unexpected = FALSE;
1797 if (!QUEUE_IS_USING_QUEUE (queue))
1798 goto unexpected_event;
1802 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1803 item, GST_OBJECT_NAME (queue));
1804 /* we can't really unref since we don't know what it is */
1809 /* update the buffering status */
1810 if (queue->use_buffering)
1811 update_buffering (queue);
1813 if (QUEUE_IS_USING_QUEUE (queue)) {
1814 g_queue_push_tail (&queue->queue, item);
1816 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
1819 GST_QUEUE2_SIGNAL_ADD (queue);
1828 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
1829 gst_event_type_get_name (GST_EVENT_TYPE (item)),
1830 GST_OBJECT_NAME (queue));
1831 gst_event_unref (GST_EVENT_CAST (item));
1836 /* dequeue an item from the queue and update level stats */
1837 static GstMiniObject *
1838 gst_queue2_locked_dequeue (GstQueue2 * queue, gboolean * is_buffer)
1840 GstMiniObject *item;
1842 if (!QUEUE_IS_USING_QUEUE (queue))
1843 item = gst_queue2_read_item_from_file (queue);
1845 item = g_queue_pop_head (&queue->queue);
1850 if (GST_IS_BUFFER (item)) {
1854 buffer = GST_BUFFER_CAST (item);
1855 size = gst_buffer_get_size (buffer);
1858 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1859 "retrieved buffer %p from queue", buffer);
1861 if (QUEUE_IS_USING_QUEUE (queue)) {
1862 queue->cur_level.buffers--;
1863 queue->cur_level.bytes -= size;
1865 queue->bytes_out += size;
1867 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
1868 /* update the byterate stats */
1869 update_out_rates (queue);
1870 /* update the buffering */
1871 if (queue->use_buffering)
1872 update_buffering (queue);
1874 } else if (GST_IS_EVENT (item)) {
1875 GstEvent *event = GST_EVENT_CAST (item);
1879 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1880 "retrieved event %p from queue", event);
1882 switch (GST_EVENT_TYPE (event)) {
1884 /* queue is empty now that we dequeued the EOS */
1885 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1887 case GST_EVENT_SEGMENT:
1888 apply_segment (queue, event, &queue->src_segment, FALSE);
1895 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
1896 item, GST_OBJECT_NAME (queue));
1899 GST_QUEUE2_SIGNAL_DEL (queue);
1906 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
1912 gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event)
1916 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
1918 switch (GST_EVENT_TYPE (event)) {
1919 case GST_EVENT_FLUSH_START:
1921 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
1922 if (QUEUE_IS_USING_QUEUE (queue)) {
1924 gst_pad_push_event (queue->srcpad, event);
1926 /* now unblock the chain function */
1927 GST_QUEUE2_MUTEX_LOCK (queue);
1928 queue->srcresult = GST_FLOW_WRONG_STATE;
1929 queue->sinkresult = GST_FLOW_WRONG_STATE;
1930 /* unblock the loop and chain functions */
1931 GST_QUEUE2_SIGNAL_ADD (queue);
1932 GST_QUEUE2_SIGNAL_DEL (queue);
1933 GST_QUEUE2_MUTEX_UNLOCK (queue);
1935 /* make sure it pauses, this should happen since we sent
1936 * flush_start downstream. */
1937 gst_pad_pause_task (queue->srcpad);
1938 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
1940 GST_QUEUE2_MUTEX_LOCK (queue);
1941 /* flush the sink pad */
1942 queue->sinkresult = GST_FLOW_WRONG_STATE;
1943 GST_QUEUE2_SIGNAL_DEL (queue);
1944 GST_QUEUE2_MUTEX_UNLOCK (queue);
1946 gst_event_unref (event);
1950 case GST_EVENT_FLUSH_STOP:
1952 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
1954 if (QUEUE_IS_USING_QUEUE (queue)) {
1956 gst_pad_push_event (queue->srcpad, event);
1958 GST_QUEUE2_MUTEX_LOCK (queue);
1959 gst_queue2_locked_flush (queue);
1960 queue->srcresult = GST_FLOW_OK;
1961 queue->sinkresult = GST_FLOW_OK;
1962 queue->is_eos = FALSE;
1963 queue->unexpected = FALSE;
1964 /* reset rate counters */
1965 reset_rate_timer (queue);
1966 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
1968 GST_QUEUE2_MUTEX_UNLOCK (queue);
1970 GST_QUEUE2_MUTEX_LOCK (queue);
1971 queue->segment_event_received = FALSE;
1972 queue->is_eos = FALSE;
1973 queue->unexpected = FALSE;
1974 queue->sinkresult = GST_FLOW_OK;
1975 GST_QUEUE2_MUTEX_UNLOCK (queue);
1977 gst_event_unref (event);
1982 if (GST_EVENT_IS_SERIALIZED (event)) {
1983 /* serialized events go in the queue */
1984 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
1985 /* refuse more events on EOS */
1988 gst_queue2_locked_enqueue (queue, event, FALSE);
1989 GST_QUEUE2_MUTEX_UNLOCK (queue);
1991 /* non-serialized events are passed upstream. */
1992 gst_pad_push_event (queue->srcpad, event);
2002 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2003 GST_QUEUE2_MUTEX_UNLOCK (queue);
2004 gst_event_unref (event);
2009 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2010 GST_QUEUE2_MUTEX_UNLOCK (queue);
2011 gst_event_unref (event);
2017 gst_queue2_handle_sink_query (GstPad * pad, GstQuery * query)
2022 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2023 if (G_UNLIKELY (queue == NULL))
2026 switch (GST_QUERY_TYPE (query)) {
2028 res = gst_pad_query_default (pad, query);
2031 gst_object_unref (queue);
2037 gst_queue2_is_empty (GstQueue2 * queue)
2039 /* never empty on EOS */
2043 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2044 return queue->current->writing_pos <= queue->current->max_reading_pos;
2046 if (queue->queue.length == 0)
2054 gst_queue2_is_filled (GstQueue2 * queue)
2058 /* always filled on EOS */
2062 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2063 (queue->cur_level.format) >= ((alt_max) ? \
2064 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2066 /* if using a ring buffer we're filled if all ring buffer space is used
2067 * _by the current range_ */
2068 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2069 guint64 rb_size = queue->ring_buffer_max_size;
2070 GST_DEBUG_OBJECT (queue,
2071 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2072 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2073 return CHECK_FILLED (bytes, rb_size);
2076 /* if using file, we're never filled if we don't have EOS */
2077 if (QUEUE_IS_USING_TEMP_FILE (queue))
2080 /* we are never filled when we have no buffers at all */
2081 if (queue->cur_level.buffers == 0)
2084 /* we are filled if one of the current levels exceeds the max */
2085 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2086 || CHECK_FILLED (time, 0);
2088 /* if we need to, use the rate estimate to check against the max time we are
2089 * allowed to queue */
2090 if (queue->use_rate_estimate)
2091 res |= CHECK_FILLED (rate_time, 0);
2097 static GstFlowReturn
2098 gst_queue2_chain (GstPad * pad, GstBuffer * buffer)
2102 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2104 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
2105 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2106 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2107 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2108 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2110 /* we have to lock the queue since we span threads */
2111 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2112 /* when we received EOS, we refuse more data */
2115 /* when we received unexpected from downstream, refuse more buffers */
2116 if (queue->unexpected)
2117 goto out_unexpected;
2119 if (!gst_queue2_wait_free_space (queue))
2122 /* put buffer in queue now */
2123 gst_queue2_locked_enqueue (queue, buffer, TRUE);
2124 GST_QUEUE2_MUTEX_UNLOCK (queue);
2128 /* special conditions */
2131 GstFlowReturn ret = queue->sinkresult;
2133 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2134 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2135 GST_QUEUE2_MUTEX_UNLOCK (queue);
2136 gst_buffer_unref (buffer);
2142 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2143 GST_QUEUE2_MUTEX_UNLOCK (queue);
2144 gst_buffer_unref (buffer);
2146 return GST_FLOW_EOS;
2150 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2151 GST_QUEUE2_MUTEX_UNLOCK (queue);
2152 gst_buffer_unref (buffer);
2154 return GST_FLOW_EOS;
2158 /* dequeue an item from the queue an push it downstream. This functions returns
2159 * the result of the push. */
2160 static GstFlowReturn
2161 gst_queue2_push_one (GstQueue2 * queue)
2163 GstFlowReturn result = GST_FLOW_OK;
2164 GstMiniObject *data;
2165 gboolean is_buffer = FALSE;
2167 data = gst_queue2_locked_dequeue (queue, &is_buffer);
2172 GST_QUEUE2_MUTEX_UNLOCK (queue);
2180 buffer = GST_BUFFER_CAST (data);
2182 caps = GST_BUFFER_CAPS (buffer);
2186 /* set caps before pushing the buffer so that core does not try to do
2187 * something fancy to check if this is possible. */
2188 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2189 gst_pad_set_caps (queue->srcpad, caps);
2192 result = gst_pad_push (queue->srcpad, buffer);
2194 /* need to check for srcresult here as well */
2195 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2196 if (result == GST_FLOW_EOS) {
2197 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2198 /* stop pushing buffers, we dequeue all items until we see an item that we
2199 * can push again, which is EOS or SEGMENT. If there is nothing in the
2200 * queue we can push, we set a flag to make the sinkpad refuse more
2201 * buffers with an EOS return value until we receive something
2202 * pushable again or we get flushed. */
2203 while ((data = gst_queue2_locked_dequeue (queue, &is_buffer))) {
2205 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2206 "dropping EOS buffer %p", data);
2207 gst_buffer_unref (GST_BUFFER_CAST (data));
2208 } else if (GST_IS_EVENT (data)) {
2209 GstEvent *event = GST_EVENT_CAST (data);
2210 GstEventType type = GST_EVENT_TYPE (event);
2212 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2213 /* we found a pushable item in the queue, push it out */
2214 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2215 "pushing pushable event %s after EOS",
2216 GST_EVENT_TYPE_NAME (event));
2219 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2220 "dropping EOS event %p", event);
2221 gst_event_unref (event);
2224 /* no more items in the queue. Set the unexpected flag so that upstream
2225 * make us refuse any more buffers on the sinkpad. Since we will still
2226 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2227 * task function does not shut down. */
2228 queue->unexpected = TRUE;
2229 result = GST_FLOW_OK;
2231 } else if (GST_IS_EVENT (data)) {
2232 GstEvent *event = GST_EVENT_CAST (data);
2233 GstEventType type = GST_EVENT_TYPE (event);
2235 gst_pad_push_event (queue->srcpad, event);
2237 /* if we're EOS, return EOS so that the task pauses. */
2238 if (type == GST_EVENT_EOS) {
2239 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2240 "pushed EOS event %p, return EOS", event);
2241 result = GST_FLOW_EOS;
2244 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2251 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2252 "exit because we have no item in the queue");
2253 return GST_FLOW_ERROR;
2257 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2258 return GST_FLOW_WRONG_STATE;
2262 /* called repeatedly with @pad as the source pad. This function should push out
2263 * data to the peer element. */
2265 gst_queue2_loop (GstPad * pad)
2270 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2272 /* have to lock for thread-safety */
2273 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2275 if (gst_queue2_is_empty (queue)) {
2278 /* pause the timer while we wait. The fact that we are waiting does not mean
2279 * the byterate on the output pad is lower */
2280 if ((started = queue->out_timer_started))
2281 g_timer_stop (queue->out_timer);
2283 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2284 "queue is empty, waiting for new data");
2286 /* Wait for data to be available, we could be unlocked because of a flush. */
2287 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2289 while (gst_queue2_is_empty (queue));
2291 /* and continue if we were running before */
2293 g_timer_continue (queue->out_timer);
2295 ret = gst_queue2_push_one (queue);
2296 queue->srcresult = ret;
2297 queue->sinkresult = ret;
2298 if (ret != GST_FLOW_OK)
2301 GST_QUEUE2_MUTEX_UNLOCK (queue);
2308 gboolean eos = queue->is_eos;
2309 GstFlowReturn ret = queue->srcresult;
2311 gst_pad_pause_task (queue->srcpad);
2312 GST_QUEUE2_MUTEX_UNLOCK (queue);
2313 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2314 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2315 /* let app know about us giving up if upstream is not expected to do so */
2316 /* EOS is already taken care of elsewhere */
2317 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2318 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2319 (_("Internal data flow error.")),
2320 ("streaming task paused, reason %s (%d)",
2321 gst_flow_get_name (ret), ret));
2322 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2329 gst_queue2_handle_src_event (GstPad * pad, GstEvent * event)
2331 gboolean res = TRUE;
2332 GstQueue2 *queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2334 if (G_UNLIKELY (queue == NULL)) {
2335 gst_event_unref (event);
2338 #ifndef GST_DISABLE_GST_DEBUG
2339 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2340 event, GST_EVENT_TYPE_NAME (event));
2343 switch (GST_EVENT_TYPE (event)) {
2344 case GST_EVENT_FLUSH_START:
2345 if (QUEUE_IS_USING_QUEUE (queue)) {
2346 /* just forward upstream */
2347 res = gst_pad_push_event (queue->sinkpad, event);
2349 /* now unblock the getrange function */
2350 GST_QUEUE2_MUTEX_LOCK (queue);
2351 GST_DEBUG_OBJECT (queue, "flushing");
2352 queue->srcresult = GST_FLOW_WRONG_STATE;
2353 GST_QUEUE2_SIGNAL_ADD (queue);
2354 GST_QUEUE2_MUTEX_UNLOCK (queue);
2356 /* when using a temp file, we eat the event */
2358 gst_event_unref (event);
2361 case GST_EVENT_FLUSH_STOP:
2362 if (QUEUE_IS_USING_QUEUE (queue)) {
2363 /* just forward upstream */
2364 res = gst_pad_push_event (queue->sinkpad, event);
2366 /* now unblock the getrange function */
2367 GST_QUEUE2_MUTEX_LOCK (queue);
2368 queue->srcresult = GST_FLOW_OK;
2369 if (queue->current) {
2370 /* forget the highest read offset, we'll calculate a new one when we
2371 * get the next getrange request. We need to do this in order to reset
2372 * the buffering percentage */
2373 queue->current->max_reading_pos = 0;
2375 GST_QUEUE2_MUTEX_UNLOCK (queue);
2377 /* when using a temp file, we eat the event */
2379 gst_event_unref (event);
2383 res = gst_pad_push_event (queue->sinkpad, event);
2387 gst_object_unref (queue);
2392 gst_queue2_handle_src_query (GstPad * pad, GstQuery * query)
2396 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2397 if (G_UNLIKELY (queue == NULL))
2400 switch (GST_QUERY_TYPE (query)) {
2401 case GST_QUERY_POSITION:
2406 if (!gst_pad_peer_query (queue->sinkpad, query))
2409 /* get peer position */
2410 gst_query_parse_position (query, &format, &peer_pos);
2412 /* FIXME: this code assumes that there's no discont in the queue */
2414 case GST_FORMAT_BYTES:
2415 peer_pos -= queue->cur_level.bytes;
2417 case GST_FORMAT_TIME:
2418 peer_pos -= queue->cur_level.time;
2421 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2422 "know how to adjust value", gst_format_get_name (format));
2425 /* set updated position */
2426 gst_query_set_position (query, format, peer_pos);
2429 case GST_QUERY_DURATION:
2431 GST_DEBUG_OBJECT (queue, "doing peer query");
2433 if (!gst_pad_peer_query (queue->sinkpad, query))
2436 GST_DEBUG_OBJECT (queue, "peer query success");
2439 case GST_QUERY_BUFFERING:
2443 GST_DEBUG_OBJECT (queue, "query buffering");
2445 /* FIXME - is this condition correct? what should ring buffer do? */
2446 if (QUEUE_IS_USING_QUEUE (queue)) {
2447 /* no temp file, just forward to the peer */
2448 if (!gst_pad_peer_query (queue->sinkpad, query))
2450 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2452 gint64 start, stop, range_start, range_stop;
2453 guint64 writing_pos;
2455 gint64 estimated_total, buffering_left;
2457 gboolean peer_res, is_buffering, is_eos;
2458 gdouble byte_in_rate, byte_out_rate;
2459 GstQueue2Range *queued_ranges;
2461 /* we need a current download region */
2462 if (queue->current == NULL)
2465 writing_pos = queue->current->writing_pos;
2466 byte_in_rate = queue->byte_in_rate;
2467 byte_out_rate = queue->byte_out_rate;
2468 is_buffering = queue->is_buffering;
2469 is_eos = queue->is_eos;
2470 percent = queue->buffering_percent;
2473 /* we're EOS, we know the duration in bytes now */
2475 duration = writing_pos;
2477 /* get duration of upstream in bytes */
2478 peer_res = gst_pad_query_peer_duration (queue->sinkpad,
2479 GST_FORMAT_BYTES, &duration);
2482 /* calculate remaining and total download time */
2483 if (peer_res && byte_in_rate > 0.0) {
2484 estimated_total = (duration * 1000) / byte_in_rate;
2485 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2487 estimated_total = -1;
2488 buffering_left = -1;
2490 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2491 G_GINT64_FORMAT, estimated_total, buffering_left);
2493 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2496 case GST_FORMAT_PERCENT:
2497 /* we need duration */
2501 GST_DEBUG_OBJECT (queue,
2502 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2503 duration, writing_pos);
2506 /* get our available data relative to the duration */
2508 stop = GST_FORMAT_PERCENT_MAX * writing_pos / duration;
2512 case GST_FORMAT_BYTES:
2522 /* fill out the buffered ranges */
2523 for (queued_ranges = queue->ranges; queued_ranges;
2524 queued_ranges = queued_ranges->next) {
2526 case GST_FORMAT_PERCENT:
2527 if (duration == -1) {
2532 range_start = 100 * queued_ranges->offset / duration;
2533 range_stop = 100 * queued_ranges->writing_pos / duration;
2535 case GST_FORMAT_BYTES:
2536 range_start = queued_ranges->offset;
2537 range_stop = queued_ranges->writing_pos;
2544 if (range_start == range_stop)
2546 GST_DEBUG_OBJECT (queue,
2547 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2548 G_GINT64_FORMAT, range_start, range_stop);
2549 gst_query_add_buffering_range (query, range_start, range_stop);
2552 gst_query_set_buffering_percent (query, is_buffering, percent);
2553 gst_query_set_buffering_range (query, format, start, stop,
2555 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2556 byte_in_rate, byte_out_rate, buffering_left);
2560 case GST_QUERY_SCHEDULING:
2564 /* we can operate in pull mode when we are using a tempfile */
2565 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2567 gst_query_set_scheduling (query, pull_mode, pull_mode, FALSE, 0, -1, 1);
2571 /* peer handled other queries */
2572 if (!gst_pad_query_default (pad, query))
2577 gst_object_unref (queue);
2583 GST_DEBUG_OBJECT (queue, "failed peer query");
2584 gst_object_unref (queue);
2590 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2592 /* simply forward to the srcpad query function */
2593 return gst_queue2_handle_src_query (GST_QUEUE2_CAST (element)->srcpad, query);
2597 gst_queue2_update_upstream_size (GstQueue2 * queue)
2599 gint64 upstream_size = -1;
2601 if (gst_pad_query_peer_duration (queue->sinkpad, GST_FORMAT_BYTES,
2603 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2604 queue->upstream_size = upstream_size;
2608 static GstFlowReturn
2609 gst_queue2_get_range (GstPad * pad, guint64 offset, guint length,
2610 GstBuffer ** buffer)
2615 queue = GST_QUEUE2_CAST (gst_pad_get_parent (pad));
2617 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2618 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2619 offset = (offset == -1) ? queue->current->reading_pos : offset;
2621 GST_DEBUG_OBJECT (queue,
2622 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2624 /* catch any reads beyond the size of the file here to make sure queue2
2625 * doesn't send seek events beyond the size of the file upstream, since
2626 * that would confuse elements such as souphttpsrc and/or http servers.
2627 * Demuxers often just loop until EOS at the end of the file to figure out
2628 * when they've read all the end-headers or index chunks. */
2629 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2630 gst_queue2_update_upstream_size (queue);
2631 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2632 goto out_unexpected;
2635 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2636 gst_queue2_update_upstream_size (queue);
2637 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2638 length = queue->upstream_size - offset;
2639 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2643 /* FIXME - function will block when the range is not yet available */
2644 ret = gst_queue2_create_read (queue, offset, length, buffer);
2645 GST_QUEUE2_MUTEX_UNLOCK (queue);
2647 gst_object_unref (queue);
2654 ret = queue->srcresult;
2656 GST_DEBUG_OBJECT (queue, "we are flushing");
2657 GST_QUEUE2_MUTEX_UNLOCK (queue);
2658 gst_object_unref (queue);
2663 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2664 GST_QUEUE2_MUTEX_UNLOCK (queue);
2665 gst_object_unref (queue);
2666 return GST_FLOW_EOS;
2670 /* sink currently only operates in push mode */
2672 gst_queue2_sink_activate_push (GstPad * pad, gboolean active)
2674 gboolean result = TRUE;
2677 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2680 GST_QUEUE2_MUTEX_LOCK (queue);
2681 GST_DEBUG_OBJECT (queue, "activating push mode");
2682 queue->srcresult = GST_FLOW_OK;
2683 queue->sinkresult = GST_FLOW_OK;
2684 queue->is_eos = FALSE;
2685 queue->unexpected = FALSE;
2686 reset_rate_timer (queue);
2687 GST_QUEUE2_MUTEX_UNLOCK (queue);
2689 /* unblock chain function */
2690 GST_QUEUE2_MUTEX_LOCK (queue);
2691 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2692 queue->srcresult = GST_FLOW_WRONG_STATE;
2693 queue->sinkresult = GST_FLOW_WRONG_STATE;
2694 gst_queue2_locked_flush (queue);
2695 GST_QUEUE2_MUTEX_UNLOCK (queue);
2698 gst_object_unref (queue);
2703 /* src operating in push mode, we start a task on the source pad that pushes out
2704 * buffers from the queue */
2706 gst_queue2_src_activate_push (GstPad * pad, gboolean active)
2708 gboolean result = FALSE;
2711 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2714 GST_QUEUE2_MUTEX_LOCK (queue);
2715 GST_DEBUG_OBJECT (queue, "activating push mode");
2716 queue->srcresult = GST_FLOW_OK;
2717 queue->sinkresult = GST_FLOW_OK;
2718 queue->is_eos = FALSE;
2719 queue->unexpected = FALSE;
2720 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
2721 GST_QUEUE2_MUTEX_UNLOCK (queue);
2723 /* unblock loop function */
2724 GST_QUEUE2_MUTEX_LOCK (queue);
2725 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2726 queue->srcresult = GST_FLOW_WRONG_STATE;
2727 queue->sinkresult = GST_FLOW_WRONG_STATE;
2728 /* the item add signal will unblock */
2729 GST_QUEUE2_SIGNAL_ADD (queue);
2730 GST_QUEUE2_MUTEX_UNLOCK (queue);
2732 /* step 2, make sure streaming finishes */
2733 result = gst_pad_stop_task (pad);
2736 gst_object_unref (queue);
2741 /* pull mode, downstream will call our getrange function */
2743 gst_queue2_src_activate_pull (GstPad * pad, gboolean active)
2748 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2751 GST_QUEUE2_MUTEX_LOCK (queue);
2752 if (!QUEUE_IS_USING_QUEUE (queue)) {
2753 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2754 /* open the temp file now */
2755 result = gst_queue2_open_temp_location_file (queue);
2756 } else if (!queue->ring_buffer) {
2757 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
2758 result = ! !queue->ring_buffer;
2763 GST_DEBUG_OBJECT (queue, "activating pull mode");
2764 init_ranges (queue);
2765 queue->srcresult = GST_FLOW_OK;
2766 queue->sinkresult = GST_FLOW_OK;
2767 queue->is_eos = FALSE;
2768 queue->unexpected = FALSE;
2769 queue->upstream_size = 0;
2771 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
2772 /* this is not allowed, we cannot operate in pull mode without a temp
2774 queue->srcresult = GST_FLOW_WRONG_STATE;
2775 queue->sinkresult = GST_FLOW_WRONG_STATE;
2778 GST_QUEUE2_MUTEX_UNLOCK (queue);
2780 GST_QUEUE2_MUTEX_LOCK (queue);
2781 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
2782 queue->srcresult = GST_FLOW_WRONG_STATE;
2783 queue->sinkresult = GST_FLOW_WRONG_STATE;
2784 /* this will unlock getrange */
2785 GST_QUEUE2_SIGNAL_ADD (queue);
2787 GST_QUEUE2_MUTEX_UNLOCK (queue);
2789 gst_object_unref (queue);
2794 static GstStateChangeReturn
2795 gst_queue2_change_state (GstElement * element, GstStateChange transition)
2798 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2800 queue = GST_QUEUE2 (element);
2802 switch (transition) {
2803 case GST_STATE_CHANGE_NULL_TO_READY:
2805 case GST_STATE_CHANGE_READY_TO_PAUSED:
2806 GST_QUEUE2_MUTEX_LOCK (queue);
2807 if (!QUEUE_IS_USING_QUEUE (queue)) {
2808 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2809 if (!gst_queue2_open_temp_location_file (queue))
2810 ret = GST_STATE_CHANGE_FAILURE;
2812 if (queue->ring_buffer) {
2813 g_free (queue->ring_buffer);
2814 queue->ring_buffer = NULL;
2816 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
2817 ret = GST_STATE_CHANGE_FAILURE;
2819 init_ranges (queue);
2821 queue->segment_event_received = FALSE;
2822 queue->starting_segment = NULL;
2823 GST_QUEUE2_MUTEX_UNLOCK (queue);
2825 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2831 if (ret == GST_STATE_CHANGE_FAILURE)
2834 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2836 if (ret == GST_STATE_CHANGE_FAILURE)
2839 switch (transition) {
2840 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2842 case GST_STATE_CHANGE_PAUSED_TO_READY:
2843 GST_QUEUE2_MUTEX_LOCK (queue);
2844 if (!QUEUE_IS_USING_QUEUE (queue)) {
2845 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2846 gst_queue2_close_temp_location_file (queue);
2847 } else if (queue->ring_buffer) {
2848 g_free (queue->ring_buffer);
2849 queue->ring_buffer = NULL;
2851 clean_ranges (queue);
2853 if (queue->starting_segment != NULL) {
2854 gst_event_unref (queue->starting_segment);
2855 queue->starting_segment = NULL;
2857 GST_QUEUE2_MUTEX_UNLOCK (queue);
2859 case GST_STATE_CHANGE_READY_TO_NULL:
2868 /* changing the capacity of the queue must wake up
2869 * the _chain function, it might have more room now
2870 * to store the buffer/event in the queue */
2871 #define QUEUE_CAPACITY_CHANGE(q)\
2872 GST_QUEUE2_SIGNAL_DEL (queue);
2874 /* Changing the minimum required fill level must
2875 * wake up the _loop function as it might now
2876 * be able to preceed.
2878 #define QUEUE_THRESHOLD_CHANGE(q)\
2879 GST_QUEUE2_SIGNAL_ADD (queue);
2882 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
2886 /* the element must be stopped in order to do this */
2887 GST_OBJECT_LOCK (queue);
2888 state = GST_STATE (queue);
2889 if (state != GST_STATE_READY && state != GST_STATE_NULL)
2891 GST_OBJECT_UNLOCK (queue);
2893 /* set new location */
2894 g_free (queue->temp_template);
2895 queue->temp_template = g_strdup (template);
2902 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
2903 GST_OBJECT_UNLOCK (queue);
2908 gst_queue2_set_property (GObject * object,
2909 guint prop_id, const GValue * value, GParamSpec * pspec)
2911 GstQueue2 *queue = GST_QUEUE2 (object);
2913 /* someone could change levels here, and since this
2914 * affects the get/put funcs, we need to lock for safety. */
2915 GST_QUEUE2_MUTEX_LOCK (queue);
2918 case PROP_MAX_SIZE_BYTES:
2919 queue->max_level.bytes = g_value_get_uint (value);
2920 QUEUE_CAPACITY_CHANGE (queue);
2922 case PROP_MAX_SIZE_BUFFERS:
2923 queue->max_level.buffers = g_value_get_uint (value);
2924 QUEUE_CAPACITY_CHANGE (queue);
2926 case PROP_MAX_SIZE_TIME:
2927 queue->max_level.time = g_value_get_uint64 (value);
2928 /* set rate_time to the same value. We use an extra field in the level
2929 * structure so that we can easily access and compare it */
2930 queue->max_level.rate_time = queue->max_level.time;
2931 QUEUE_CAPACITY_CHANGE (queue);
2933 case PROP_USE_BUFFERING:
2934 queue->use_buffering = g_value_get_boolean (value);
2936 case PROP_USE_RATE_ESTIMATE:
2937 queue->use_rate_estimate = g_value_get_boolean (value);
2939 case PROP_LOW_PERCENT:
2940 queue->low_percent = g_value_get_int (value);
2942 case PROP_HIGH_PERCENT:
2943 queue->high_percent = g_value_get_int (value);
2945 case PROP_TEMP_TEMPLATE:
2946 gst_queue2_set_temp_template (queue, g_value_get_string (value));
2948 case PROP_TEMP_LOCATION:
2949 g_free (queue->temp_location);
2950 queue->temp_location = g_value_dup_string (value);
2951 /* you can set the property back to NULL to make it use the temp-tmpl
2953 queue->temp_location_set = queue->temp_location != NULL;
2955 case PROP_TEMP_REMOVE:
2956 queue->temp_remove = g_value_get_boolean (value);
2958 case PROP_RING_BUFFER_MAX_SIZE:
2959 queue->ring_buffer_max_size = g_value_get_uint64 (value);
2962 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2966 GST_QUEUE2_MUTEX_UNLOCK (queue);
2970 gst_queue2_get_property (GObject * object,
2971 guint prop_id, GValue * value, GParamSpec * pspec)
2973 GstQueue2 *queue = GST_QUEUE2 (object);
2975 GST_QUEUE2_MUTEX_LOCK (queue);
2978 case PROP_CUR_LEVEL_BYTES:
2979 g_value_set_uint (value, queue->cur_level.bytes);
2981 case PROP_CUR_LEVEL_BUFFERS:
2982 g_value_set_uint (value, queue->cur_level.buffers);
2984 case PROP_CUR_LEVEL_TIME:
2985 g_value_set_uint64 (value, queue->cur_level.time);
2987 case PROP_MAX_SIZE_BYTES:
2988 g_value_set_uint (value, queue->max_level.bytes);
2990 case PROP_MAX_SIZE_BUFFERS:
2991 g_value_set_uint (value, queue->max_level.buffers);
2993 case PROP_MAX_SIZE_TIME:
2994 g_value_set_uint64 (value, queue->max_level.time);
2996 case PROP_USE_BUFFERING:
2997 g_value_set_boolean (value, queue->use_buffering);
2999 case PROP_USE_RATE_ESTIMATE:
3000 g_value_set_boolean (value, queue->use_rate_estimate);
3002 case PROP_LOW_PERCENT:
3003 g_value_set_int (value, queue->low_percent);
3005 case PROP_HIGH_PERCENT:
3006 g_value_set_int (value, queue->high_percent);
3008 case PROP_TEMP_TEMPLATE:
3009 g_value_set_string (value, queue->temp_template);
3011 case PROP_TEMP_LOCATION:
3012 g_value_set_string (value, queue->temp_location);
3014 case PROP_TEMP_REMOVE:
3015 g_value_set_boolean (value, queue->temp_remove);
3017 case PROP_RING_BUFFER_MAX_SIZE:
3018 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3021 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3025 GST_QUEUE2_MUTEX_UNLOCK (queue);