2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
29 * Data is queued until one of the limits specified by the
30 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
31 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
38 * You can query how many buffers are queued by reading the
39 * #GstQueue2:current-level-buffers property.
41 * The default queue size limits are 100 buffers, 2MB of data, or
42 * two seconds worth of data, whichever is reached first.
44 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
45 * will allocate a random free filename and buffer data in the file.
46 * By using this, it will buffer the entire stream data on the file independently
47 * of the queue size limits, they will only be used for buffering statistics.
49 * The temp-location property will be used to notify the application of the
57 #include "gstqueue2.h"
59 #include <glib/gstdio.h>
61 #include "gst/gst-i18n-lib.h"
62 #include "gst/glib-compat-private.h"
67 #include <io.h> /* lseek, open, close, read */
69 #define lseek _lseeki64
76 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
81 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_DEBUG_CATEGORY_STATIC (queue_debug);
87 #define GST_CAT_DEFAULT (queue_debug)
88 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
96 #define DEFAULT_BUFFER_SIZE 4096
97 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
98 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
99 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
101 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
103 /* default property values */
104 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
105 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
106 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
107 #define DEFAULT_USE_BUFFERING FALSE
108 #define DEFAULT_USE_RATE_ESTIMATE TRUE
109 #define DEFAULT_LOW_PERCENT 10
110 #define DEFAULT_HIGH_PERCENT 99
111 #define DEFAULT_TEMP_REMOVE TRUE
112 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
117 PROP_CUR_LEVEL_BUFFERS,
118 PROP_CUR_LEVEL_BYTES,
120 PROP_MAX_SIZE_BUFFERS,
124 PROP_USE_RATE_ESTIMATE,
130 PROP_RING_BUFFER_MAX_SIZE,
134 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
141 #define STATUS(queue, pad, msg) \
142 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
143 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
144 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
145 " ns, %"G_GUINT64_FORMAT" items", \
146 GST_DEBUG_PAD_NAME (pad), \
147 queue->cur_level.buffers, \
148 queue->max_level.buffers, \
149 queue->cur_level.bytes, \
150 queue->max_level.bytes, \
151 queue->cur_level.time, \
152 queue->max_level.time, \
153 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
154 queue->current->writing_pos - queue->current->max_reading_pos : \
155 queue->queue.length))
157 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
158 g_mutex_lock (&q->qlock); \
161 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
162 GST_QUEUE2_MUTEX_LOCK (q); \
163 if (res != GST_FLOW_OK) \
167 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
168 g_mutex_unlock (&q->qlock); \
171 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
172 STATUS (queue, q->sinkpad, "wait for DEL"); \
173 q->waiting_del = TRUE; \
174 g_cond_wait (&q->item_del, &queue->qlock); \
175 q->waiting_del = FALSE; \
176 if (res != GST_FLOW_OK) { \
177 STATUS (queue, q->srcpad, "received DEL wakeup"); \
180 STATUS (queue, q->sinkpad, "received DEL"); \
183 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
184 STATUS (queue, q->srcpad, "wait for ADD"); \
185 q->waiting_add = TRUE; \
186 g_cond_wait (&q->item_add, &q->qlock); \
187 q->waiting_add = FALSE; \
188 if (res != GST_FLOW_OK) { \
189 STATUS (queue, q->srcpad, "received ADD wakeup"); \
192 STATUS (queue, q->srcpad, "received ADD"); \
195 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
196 if (q->waiting_del) { \
197 STATUS (q, q->srcpad, "signal DEL"); \
198 g_cond_signal (&q->item_del); \
202 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
203 if (q->waiting_add) { \
204 STATUS (q, q->sinkpad, "signal ADD"); \
205 g_cond_signal (&q->item_add); \
209 #define SET_PERCENT(q, perc) G_STMT_START { \
210 if (perc != q->buffering_percent) { \
211 q->buffering_percent = perc; \
212 q->percent_changed = TRUE; \
213 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
214 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
215 &q->buffering_left); \
220 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
221 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
222 "dataflow inside the queue element");
223 #define gst_queue2_parent_class parent_class
224 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
226 static void gst_queue2_finalize (GObject * object);
228 static void gst_queue2_set_property (GObject * object,
229 guint prop_id, const GValue * value, GParamSpec * pspec);
230 static void gst_queue2_get_property (GObject * object,
231 guint prop_id, GValue * value, GParamSpec * pspec);
233 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
235 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
236 GstBufferList * buffer_list);
237 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
238 static void gst_queue2_loop (GstPad * pad);
240 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
242 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
245 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
247 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
249 static gboolean gst_queue2_handle_query (GstElement * element,
252 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
253 guint64 offset, guint length, GstBuffer ** buffer);
255 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
256 GstPadMode mode, gboolean active);
257 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
258 GstPadMode mode, gboolean active);
259 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
260 GstStateChange transition);
262 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
263 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
265 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
266 static void update_in_rates (GstQueue2 * queue);
267 static void gst_queue2_post_buffering (GstQueue2 * queue);
268 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
269 static gboolean change_current_range(GstQueue2 * queue, GstQueue2Range *req_range, guint64 offset, guint length);
274 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
275 GST_QUEUE2_ITEM_TYPE_BUFFER,
276 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
277 GST_QUEUE2_ITEM_TYPE_EVENT,
278 GST_QUEUE2_ITEM_TYPE_QUERY
283 GstQueue2ItemType type;
287 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
290 gst_queue2_class_init (GstQueue2Class * klass)
292 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
293 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
295 gobject_class->set_property = gst_queue2_set_property;
296 gobject_class->get_property = gst_queue2_get_property;
299 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
300 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
301 "Current amount of data in the queue (bytes)",
302 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
304 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
305 "Current number of buffers in the queue",
306 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
308 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
309 "Current amount of data in the queue (in ns)",
310 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
313 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
314 "Max. amount of data in the queue (bytes, 0=disable)",
315 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
316 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
317 G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
319 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
320 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
321 DEFAULT_MAX_SIZE_BUFFERS,
322 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
323 G_PARAM_STATIC_STRINGS));
324 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
325 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
326 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
327 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
328 G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
331 g_param_spec_boolean ("use-buffering", "Use buffering",
332 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
333 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
334 G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
336 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
337 "Estimate the bitrate of the stream to calculate time level",
338 DEFAULT_USE_RATE_ESTIMATE,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
341 g_param_spec_int ("low-percent", "Low percent",
342 "Low threshold for buffering to start. Only used if use-buffering is True",
343 0, 100, DEFAULT_LOW_PERCENT,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
346 g_param_spec_int ("high-percent", "High percent",
347 "High threshold for buffering to finish. Only used if use-buffering is True",
348 0, 100, DEFAULT_HIGH_PERCENT,
349 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
352 g_param_spec_string ("temp-template", "Temporary File Template",
353 "File template to store temporary files in, should contain directory "
354 "and XXXXXX. (NULL == disabled)",
355 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
358 g_param_spec_string ("temp-location", "Temporary File Location",
359 "Location to store temporary files in (Only read this property, "
360 "use temp-template to configure the name template)",
361 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
364 * GstQueue2:temp-remove
366 * When temp-template is set, remove the temporary file when going to READY.
368 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
369 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
370 "Remove the temp-location after use",
371 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 * GstQueue2:ring-buffer-max-size
376 * The maximum size of the ring buffer in bytes. If set to 0, the ring
377 * buffer is disabled. Default 0.
379 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
380 g_param_spec_uint64 ("ring-buffer-max-size",
381 "Max. ring buffer size (bytes)",
382 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
383 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 /* set several parent class virtual functions */
387 gobject_class->finalize = gst_queue2_finalize;
389 gst_element_class_add_pad_template (gstelement_class,
390 gst_static_pad_template_get (&srctemplate));
391 gst_element_class_add_pad_template (gstelement_class,
392 gst_static_pad_template_get (&sinktemplate));
394 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
397 "Erik Walthinsen <omega@cse.ogi.edu>, "
398 "Wim Taymans <wim.taymans@gmail.com>");
400 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
401 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
405 gst_queue2_init (GstQueue2 * queue)
407 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
409 gst_pad_set_chain_function (queue->sinkpad,
410 GST_DEBUG_FUNCPTR (gst_queue2_chain));
411 gst_pad_set_chain_list_function (queue->sinkpad,
412 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
413 gst_pad_set_activatemode_function (queue->sinkpad,
414 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
415 gst_pad_set_event_function (queue->sinkpad,
416 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
417 gst_pad_set_query_function (queue->sinkpad,
418 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
419 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
420 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
422 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
424 gst_pad_set_activatemode_function (queue->srcpad,
425 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
426 gst_pad_set_getrange_function (queue->srcpad,
427 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
428 gst_pad_set_event_function (queue->srcpad,
429 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
430 gst_pad_set_query_function (queue->srcpad,
431 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
432 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
433 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
436 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
437 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
438 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
439 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
440 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
441 queue->use_buffering = DEFAULT_USE_BUFFERING;
442 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
443 queue->low_percent = DEFAULT_LOW_PERCENT;
444 queue->high_percent = DEFAULT_HIGH_PERCENT;
446 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
447 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
449 queue->sinktime = GST_CLOCK_TIME_NONE;
450 queue->srctime = GST_CLOCK_TIME_NONE;
451 queue->sink_tainted = TRUE;
452 queue->src_tainted = TRUE;
454 queue->srcresult = GST_FLOW_FLUSHING;
455 queue->sinkresult = GST_FLOW_FLUSHING;
456 queue->is_eos = FALSE;
457 queue->in_timer = g_timer_new ();
458 queue->out_timer = g_timer_new ();
460 g_mutex_init (&queue->qlock);
461 queue->waiting_add = FALSE;
462 g_cond_init (&queue->item_add);
463 queue->waiting_del = FALSE;
464 g_cond_init (&queue->item_del);
465 g_queue_init (&queue->queue);
467 g_cond_init (&queue->query_handled);
468 queue->last_query = FALSE;
470 g_mutex_init (&queue->buffering_post_lock);
471 queue->buffering_percent = 100;
473 /* tempfile related */
474 queue->temp_template = NULL;
475 queue->temp_location = NULL;
476 queue->temp_remove = DEFAULT_TEMP_REMOVE;
478 queue->ring_buffer = NULL;
479 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
481 GST_DEBUG_OBJECT (queue,
482 "initialized queue's not_empty & not_full conditions");
485 /* called only once, as opposed to dispose */
487 gst_queue2_finalize (GObject * object)
489 GstQueue2 *queue = GST_QUEUE2 (object);
491 GST_DEBUG_OBJECT (queue, "finalizing queue");
493 while (!g_queue_is_empty (&queue->queue)) {
494 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
496 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
497 gst_mini_object_unref (qitem->item);
498 g_slice_free (GstQueue2Item, qitem);
501 queue->last_query = FALSE;
502 g_queue_clear (&queue->queue);
503 g_mutex_clear (&queue->qlock);
504 g_mutex_clear (&queue->buffering_post_lock);
505 g_cond_clear (&queue->item_add);
506 g_cond_clear (&queue->item_del);
507 g_cond_clear (&queue->query_handled);
508 g_timer_destroy (queue->in_timer);
509 g_timer_destroy (queue->out_timer);
511 /* temp_file path cleanup */
512 g_free (queue->temp_template);
513 g_free (queue->temp_location);
515 G_OBJECT_CLASS (parent_class)->finalize (object);
519 debug_ranges (GstQueue2 * queue)
521 GstQueue2Range *walk;
523 for (walk = queue->ranges; walk; walk = walk->next) {
524 GST_DEBUG_OBJECT (queue,
525 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
526 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
527 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
528 walk->rb_writing_pos, walk->reading_pos,
529 walk == queue->current ? "**y**" : " n ");
533 /* clear all the downloaded ranges */
535 clean_ranges (GstQueue2 * queue)
537 GST_DEBUG_OBJECT (queue, "clean queue ranges");
539 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
540 queue->ranges = NULL;
541 queue->current = NULL;
542 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
547 /* find a range that contains @offset or NULL when nothing does */
548 static GstQueue2Range *
549 find_range (GstQueue2 * queue, guint64 offset)
551 GstQueue2Range *range = NULL;
552 GstQueue2Range *walk;
554 /* first do a quick check for the current range */
555 for (walk = queue->ranges; walk; walk = walk->next) {
556 if (offset >= walk->offset && offset <= walk->writing_pos) {
557 /* we can reuse an existing range */
563 GST_DEBUG_OBJECT (queue,
564 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
565 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
567 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
573 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
575 guint64 max_reading_pos, writing_pos;
577 writing_pos = range->writing_pos;
578 max_reading_pos = range->max_reading_pos;
580 if (writing_pos > max_reading_pos)
581 queue->cur_level.bytes = writing_pos - max_reading_pos;
583 queue->cur_level.bytes = 0;
586 /* make a new range for @offset or reuse an existing range */
587 static GstQueue2Range *
588 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
590 GstQueue2Range *range, *prev, *next;
592 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
594 if ((range = find_range (queue, offset))) {
595 GST_DEBUG_OBJECT (queue,
596 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
598 if (update_existing && range->writing_pos != offset) {
599 GST_DEBUG_OBJECT (queue, "updating range writing position to "
600 "%" G_GUINT64_FORMAT, offset);
601 range->writing_pos = offset;
604 GST_DEBUG_OBJECT (queue,
605 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
607 range = g_slice_new0 (GstQueue2Range);
608 range->offset = offset;
609 /* we want to write to the next location in the ring buffer */
610 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
611 range->writing_pos = offset;
612 range->rb_writing_pos = range->rb_offset;
613 range->reading_pos = offset;
614 range->max_reading_pos = offset;
618 next = queue->ranges;
620 if (next->offset > offset) {
621 /* insert before next */
622 GST_DEBUG_OBJECT (queue,
623 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
635 queue->ranges = range;
637 debug_ranges (queue);
639 /* update the stats for this range */
640 update_cur_level (queue, range);
646 /* clear and init the download ranges for offset 0 */
648 init_ranges (GstQueue2 * queue)
650 GST_DEBUG_OBJECT (queue, "init queue ranges");
652 /* get rid of all the current ranges */
653 clean_ranges (queue);
654 /* make a range for offset 0 */
655 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
656 queue->current = queue->read = add_range (queue, 0, TRUE);
658 queue->current = add_range (queue, 0, TRUE);
662 /* calculate the diff between running time on the sink and src of the queue.
663 * This is the total amount of time in the queue. */
665 update_time_level (GstQueue2 * queue)
667 if (queue->sink_tainted) {
669 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
670 queue->sink_segment.position);
671 queue->sink_tainted = FALSE;
674 if (queue->src_tainted) {
676 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
677 queue->src_segment.position);
678 queue->src_tainted = FALSE;
681 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
682 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
684 if (queue->sinktime != GST_CLOCK_TIME_NONE
685 && queue->srctime != GST_CLOCK_TIME_NONE
686 && queue->sinktime >= queue->srctime)
687 queue->cur_level.time = queue->sinktime - queue->srctime;
689 queue->cur_level.time = 0;
692 /* take a SEGMENT event and apply the values to segment, updating the time
695 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
698 gst_event_copy_segment (event, segment);
700 if (segment->format == GST_FORMAT_BYTES) {
701 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
702 /* start is where we'll be getting from and as such writing next */
703 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
704 queue->current = queue->read = add_range (queue, segment->start, TRUE);
706 queue->current = add_range (queue, segment->start, TRUE);
711 /* now configure the values, we use these to track timestamps on the
713 if (segment->format != GST_FORMAT_TIME) {
714 /* non-time format, pretent the current time segment is closed with a
715 * 0 start and unknown stop time. */
716 segment->format = GST_FORMAT_TIME;
722 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
725 queue->sink_tainted = TRUE;
727 queue->src_tainted = TRUE;
729 /* segment can update the time level of the queue */
730 update_time_level (queue);
734 apply_gap (GstQueue2 * queue, GstEvent * event,
735 GstSegment * segment, gboolean is_sink)
737 GstClockTime timestamp;
738 GstClockTime duration;
740 gst_event_parse_gap (event, ×tamp, &duration);
742 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
744 if (GST_CLOCK_TIME_IS_VALID (duration)) {
745 timestamp += duration;
748 segment->position = timestamp;
751 queue->sink_tainted = TRUE;
753 queue->src_tainted = TRUE;
755 /* calc diff with other end */
756 update_time_level (queue);
760 /* take a buffer and update segment, updating the time level of the queue. */
762 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
765 GstClockTime duration, timestamp;
767 timestamp = GST_BUFFER_TIMESTAMP (buffer);
768 duration = GST_BUFFER_DURATION (buffer);
770 /* if no timestamp is set, assume it's continuous with the previous
772 if (timestamp == GST_CLOCK_TIME_NONE)
773 timestamp = segment->position;
776 if (duration != GST_CLOCK_TIME_NONE)
777 timestamp += duration;
779 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
780 GST_TIME_ARGS (timestamp));
782 segment->position = timestamp;
785 queue->sink_tainted = TRUE;
787 queue->src_tainted = TRUE;
789 /* calc diff with other end */
790 update_time_level (queue);
794 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
796 GstClockTime *timestamp = data;
798 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
799 " duration %" GST_TIME_FORMAT, idx,
800 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
801 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
803 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
804 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
806 if (GST_BUFFER_DURATION_IS_VALID (*buf))
807 *timestamp += GST_BUFFER_DURATION (*buf);
809 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
813 /* take a buffer list and update segment, updating the time level of the queue */
815 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
816 GstSegment * segment, gboolean is_sink)
818 GstClockTime timestamp;
820 /* if no timestamp is set, assume it's continuous with the previous time */
821 timestamp = segment->position;
823 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
825 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
826 GST_TIME_ARGS (timestamp));
828 segment->position = timestamp;
831 queue->sink_tainted = TRUE;
833 queue->src_tainted = TRUE;
835 /* calc diff with other end */
836 update_time_level (queue);
840 get_buffering_percent (GstQueue2 * queue, gboolean * is_buffering,
845 if (queue->high_percent <= 0) {
849 *is_buffering = FALSE;
852 #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)
855 /* on EOS we are always 100% full, we set the var here so that it we can
856 * reuse the logic below to stop buffering */
858 GST_LOG_OBJECT (queue, "we are EOS");
860 /* figure out the percent we are filled, we take the max of all formats. */
861 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
862 perc = GET_PERCENT (bytes, 0);
864 guint64 rb_size = queue->ring_buffer_max_size;
865 perc = GET_PERCENT (bytes, rb_size);
867 perc = MAX (perc, GET_PERCENT (time, 0));
868 perc = MAX (perc, GET_PERCENT (buffers, 0));
870 /* also apply the rate estimate when we need to */
871 if (queue->use_rate_estimate)
872 perc = MAX (perc, GET_PERCENT (rate_time, 0));
877 *is_buffering = queue->is_buffering;
879 /* scale to high percent so that it becomes the 100% mark */
880 perc = perc * 100 / queue->high_percent;
888 GST_DEBUG_OBJECT (queue, "buffering %d, percent %d", queue->is_buffering,
895 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
896 gint * avg_in, gint * avg_out, gint64 * buffering_left)
899 if (!QUEUE_IS_USING_QUEUE (queue)) {
900 if (QUEUE_IS_USING_RING_BUFFER (queue))
901 *mode = GST_BUFFERING_TIMESHIFT;
903 *mode = GST_BUFFERING_DOWNLOAD;
905 *mode = GST_BUFFERING_STREAM;
910 *avg_in = queue->byte_in_rate;
912 *avg_out = queue->byte_out_rate;
914 if (buffering_left) {
915 *buffering_left = (percent == 100 ? 0 : -1);
917 if (queue->use_rate_estimate) {
920 max = queue->max_level.rate_time;
921 cur = queue->cur_level.rate_time;
923 if (percent != 100 && max > cur)
924 *buffering_left = (max - cur) / 1000000;
930 gst_queue2_post_buffering (GstQueue2 * queue)
932 GstMessage *msg = NULL;
934 g_mutex_lock (&queue->buffering_post_lock);
935 GST_QUEUE2_MUTEX_LOCK (queue);
936 if (queue->percent_changed) {
937 gint percent = queue->buffering_percent;
939 queue->percent_changed = FALSE;
941 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
942 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
944 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
945 queue->avg_out, queue->buffering_left);
947 GST_QUEUE2_MUTEX_UNLOCK (queue);
950 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
952 g_mutex_unlock (&queue->buffering_post_lock);
956 update_buffering (GstQueue2 * queue)
959 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
960 GstQueue2Range *range;
965 range = queue->current;
968 /* Ensure the variables used to calculate buffering state are up-to-date. */
969 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
971 update_cur_level (queue, range);
974 update_cur_level (queue, queue->current);
976 update_in_rates (queue);
978 if (!get_buffering_percent (queue, NULL, &percent))
981 if (queue->is_buffering) {
982 /* if we were buffering see if we reached the high watermark */
984 queue->is_buffering = FALSE;
986 SET_PERCENT (queue, percent);
988 /* we were not buffering, check if we need to start buffering if we drop
989 * below the low threshold */
990 if (percent < queue->low_percent) {
991 queue->is_buffering = TRUE;
992 SET_PERCENT (queue, percent);
998 reset_rate_timer (GstQueue2 * queue)
1000 queue->bytes_in = 0;
1001 queue->bytes_out = 0;
1002 queue->byte_in_rate = 0.0;
1003 queue->byte_in_period = 0;
1004 queue->byte_out_rate = 0.0;
1005 queue->last_in_elapsed = 0.0;
1006 queue->last_out_elapsed = 0.0;
1007 queue->in_timer_started = FALSE;
1008 queue->out_timer_started = FALSE;
1011 /* the interval in seconds to recalculate the rate */
1012 #define RATE_INTERVAL 0.2
1013 /* Tuning for rate estimation. We use a large window for the input rate because
1014 * it should be stable when connected to a network. The output rate is less
1015 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1016 * therefore adapt more quickly.
1017 * However, initial input rate may be subject to a burst, and should therefore
1018 * initially also adapt more quickly to changes, and only later on give higher
1019 * weight to previous values. */
1020 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1021 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1024 update_in_rates (GstQueue2 * queue)
1026 gdouble elapsed, period;
1027 gdouble byte_in_rate;
1029 if (!queue->in_timer_started) {
1030 queue->in_timer_started = TRUE;
1031 g_timer_start (queue->in_timer);
1035 elapsed = g_timer_elapsed (queue->in_timer, NULL);
1037 /* recalc after each interval. */
1038 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1039 period = elapsed - queue->last_in_elapsed;
1041 GST_DEBUG_OBJECT (queue,
1042 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1043 period, queue->bytes_in, queue->byte_in_period);
1045 byte_in_rate = queue->bytes_in / period;
1047 if (queue->byte_in_rate == 0.0)
1048 queue->byte_in_rate = byte_in_rate;
1050 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1051 (double) queue->byte_in_period, period);
1053 /* another data point, cap at 16 for long time running average */
1054 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1055 queue->byte_in_period += period;
1057 /* reset the values to calculate rate over the next interval */
1058 queue->last_in_elapsed = elapsed;
1059 queue->bytes_in = 0;
1062 if (queue->byte_in_rate > 0.0) {
1063 queue->cur_level.rate_time =
1064 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1066 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1067 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1071 update_out_rates (GstQueue2 * queue)
1073 gdouble elapsed, period;
1074 gdouble byte_out_rate;
1076 if (!queue->out_timer_started) {
1077 queue->out_timer_started = TRUE;
1078 g_timer_start (queue->out_timer);
1082 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1084 /* recalc after each interval. */
1085 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1086 period = elapsed - queue->last_out_elapsed;
1088 GST_DEBUG_OBJECT (queue,
1089 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1091 byte_out_rate = queue->bytes_out / period;
1093 if (queue->byte_out_rate == 0.0)
1094 queue->byte_out_rate = byte_out_rate;
1096 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1098 /* reset the values to calculate rate over the next interval */
1099 queue->last_out_elapsed = elapsed;
1100 queue->bytes_out = 0;
1102 if (queue->byte_in_rate > 0.0) {
1103 queue->cur_level.rate_time =
1104 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1106 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1107 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1111 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1113 guint64 reading_pos, max_reading_pos;
1116 max_reading_pos = range->max_reading_pos;
1118 max_reading_pos = MAX (max_reading_pos, reading_pos);
1120 GST_DEBUG_OBJECT (queue,
1121 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1122 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1123 range->max_reading_pos = max_reading_pos;
1125 update_cur_level (queue, range);
1129 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1134 /* until we receive the FLUSH_STOP from this seek, we skip data */
1135 queue->seeking = TRUE;
1136 GST_QUEUE2_MUTEX_UNLOCK (queue);
1138 debug_ranges (queue);
1140 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1143 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1144 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1145 GST_SEEK_TYPE_NONE, -1);
1147 res = gst_pad_push_event (queue->sinkpad, event);
1148 GST_QUEUE2_MUTEX_LOCK (queue);
1151 /* Between us sending the seek event and re-acquiring the lock, the source
1152 * thread might already have pushed data and moved along the range's
1153 * writing_pos beyond the seek offset. In that case we don't want to set
1154 * the writing position back to the requested seek position, as it would
1155 * cause data to be written to the wrong offset in the file or ring buffer.
1156 * We still do the add_range call to switch the current range to the
1157 * requested range, or create one if one doesn't exist yet. */
1158 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1159 queue->current = queue->read = add_range (queue, offset, FALSE);
1161 queue->current = add_range (queue, offset, FALSE);
1168 /* get the threshold for when we decide to seek rather than wait */
1170 get_seek_threshold (GstQueue2 * queue)
1174 /* FIXME, find a good threshold based on the incoming rate. */
1175 threshold = 1024 * 512;
1177 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1178 threshold = MIN (threshold,
1179 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1184 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1185 /* check the buffered data size, not to change range repeatedly.
1186 * changing range cause the new http connection and it makes buffering state frequently. */
1188 change_current_range(GstQueue2 * queue, GstQueue2Range *req_range, guint64 offset, guint length)
1190 guint64 curr_level = 0;
1191 guint64 curr_min_level = 0;
1193 if (queue->current->writing_pos > queue->current->reading_pos)
1194 curr_level = queue->current->writing_pos - queue->current->reading_pos;
1196 /* FIXME, find a good threshold based on the incoming rate and content bitrate. */
1197 curr_min_level = MIN((queue->max_level.bytes*0.05), get_seek_threshold(queue));
1199 GST_DEBUG_OBJECT(queue, " curr[w%"G_GUINT64_FORMAT", r%"G_GUINT64_FORMAT
1200 ", d%"G_GUINT64_FORMAT", m%"G_GUINT64_FORMAT
1201 "] u[%"G_GUINT64_FORMAT"][EOS:%d]",
1202 queue->current->writing_pos, queue->current->reading_pos,
1203 curr_level, curr_min_level,
1204 queue->upstream_size, queue->is_eos);
1206 /* FIXME, find a good threshold based on the incoming rate and content bitrate. */
1207 if ((queue->is_eos) ||
1208 (queue->upstream_size > 0 && queue->current->writing_pos >= queue->upstream_size) ||
1209 (curr_level >= curr_min_level)) {
1210 GST_DEBUG_OBJECT (queue, "Range Switching");
1213 GST_DEBUG_OBJECT (queue, "keep receiving data without range switching.");
1219 /* see if there is enough data in the file to read a full buffer */
1221 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1223 GstQueue2Range *range;
1224 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1225 gboolean need_to_seek = TRUE;
1227 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1230 if ((range = find_range (queue, offset))) {
1231 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1232 queue->read = range;
1234 if (queue->current != range) {
1235 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1236 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1237 if (QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER(queue))
1238 need_to_seek = change_current_range(queue, range, offset, length);
1240 if (need_to_seek == TRUE)
1241 perform_seek_to_offset (queue, range->writing_pos);
1243 perform_seek_to_offset (queue, range->writing_pos);
1247 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1248 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1250 /* we have a range for offset */
1251 GST_DEBUG_OBJECT (queue,
1252 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1253 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1255 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1258 if (offset + length <= range->writing_pos)
1261 GST_DEBUG_OBJECT (queue,
1262 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1263 (offset + length) - range->writing_pos);
1266 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1267 " len %u", offset, length);
1269 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1270 queue->read = queue->current;
1273 /* we don't have the range, see how far away we are */
1274 if (!queue->is_eos && queue->current) {
1275 guint64 threshold = get_seek_threshold (queue);
1277 if (offset >= queue->current->offset && offset <=
1278 queue->current->writing_pos + threshold) {
1279 GST_INFO_OBJECT (queue,
1280 "requested data is within range, wait for data");
1285 /* too far away, do a seek */
1286 perform_seek_to_offset (queue, offset);
1293 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1294 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1295 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1297 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1300 static GstFlowReturn
1301 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1302 guint8 * dst, gint64 * read_return)
1304 guint8 *ring_buffer;
1307 ring_buffer = queue->ring_buffer;
1309 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1312 /* this should not block */
1313 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1315 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1316 res = fread (dst, 1, length, queue->temp_file);
1318 memcpy (dst, ring_buffer + offset, length);
1322 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1324 if (G_UNLIKELY (res < length)) {
1325 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1326 goto could_not_read;
1327 /* check for errors or EOF */
1328 if (ferror (queue->temp_file))
1329 goto could_not_read;
1330 if (feof (queue->temp_file) && length > 0)
1340 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1341 return GST_FLOW_ERROR;
1345 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1346 return GST_FLOW_ERROR;
1350 GST_DEBUG ("non-regular file hits EOS");
1351 return GST_FLOW_EOS;
1355 static GstFlowReturn
1356 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1357 GstBuffer ** buffer)
1362 guint64 file_offset;
1363 guint block_length, remaining, read_length;
1367 GstFlowReturn ret = GST_FLOW_OK;
1369 /* allocate the output buffer of the requested size */
1370 if (*buffer == NULL)
1371 buf = gst_buffer_new_allocate (NULL, length, NULL);
1375 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1378 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1382 rb_size = queue->ring_buffer_max_size;
1383 max_size = QUEUE_MAX_BYTES (queue);
1386 while (remaining > 0) {
1387 /* configure how much/whether to read */
1388 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1391 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1394 /* calculate how far away the offset is */
1395 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1396 if (queue->read->writing_pos > rpos)
1397 level = queue->read->writing_pos - rpos;
1399 if (queue->current->writing_pos > rpos)
1400 level = queue->current->writing_pos - rpos;
1405 GST_DEBUG_OBJECT (queue,
1406 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1407 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1408 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1409 rpos, queue->read->writing_pos, level, max_size);
1411 rpos, queue->current->writing_pos, level, max_size);
1414 if (level >= max_size) {
1415 /* we don't have the data but if we have a ring buffer that is full, we
1417 GST_DEBUG_OBJECT (queue,
1418 "ring buffer full, reading QUEUE_MAX_BYTES %"
1419 G_GUINT64_FORMAT " bytes", max_size);
1420 read_length = max_size;
1421 } else if (queue->is_eos) {
1422 /* won't get any more data so read any data we have */
1424 GST_DEBUG_OBJECT (queue,
1425 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1427 read_length = level;
1435 if (read_length == 0) {
1436 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1437 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1438 GST_DEBUG_OBJECT (queue,
1439 "update current position [%" G_GUINT64_FORMAT "-%"
1440 G_GUINT64_FORMAT "]", rpos, queue->read->max_reading_pos);
1441 update_cur_pos (queue, queue->read, rpos);
1443 GST_DEBUG_OBJECT (queue,
1444 "update current position [%" G_GUINT64_FORMAT "-%"
1445 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1446 update_cur_pos (queue, queue->current, rpos);
1448 GST_QUEUE2_SIGNAL_DEL (queue);
1451 if (queue->use_buffering)
1452 update_buffering (queue);
1454 GST_DEBUG_OBJECT (queue, "waiting for add");
1455 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1459 /* we have the requested data so read it */
1460 read_length = remaining;
1463 /* set range reading_pos to actual reading position for this read */
1464 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1465 queue->read->reading_pos = rpos;
1467 queue->current->reading_pos = rpos;
1470 /* configure how much and from where to read */
1471 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1473 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1474 (queue->read->rb_offset + (rpos -
1475 queue->read->offset)) % rb_size;
1477 (queue->current->rb_offset + (rpos -
1478 queue->current->offset)) % rb_size;
1480 if (file_offset + read_length > rb_size) {
1481 block_length = rb_size - file_offset;
1483 block_length = read_length;
1487 block_length = read_length;
1490 /* while we still have data to read, we loop */
1491 while (read_length > 0) {
1495 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1496 data, &read_return);
1497 if (ret != GST_FLOW_OK)
1500 file_offset += read_return;
1501 if (QUEUE_IS_USING_RING_BUFFER (queue))
1502 file_offset %= rb_size;
1504 data += read_return;
1505 read_length -= read_return;
1506 block_length = read_length;
1507 remaining -= read_return;
1509 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1510 rpos = (queue->read->reading_pos += read_return);
1511 update_cur_pos (queue, queue->read, queue->read->reading_pos);
1513 rpos = (queue->current->reading_pos += read_return);
1514 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1517 GST_QUEUE2_SIGNAL_DEL (queue);
1518 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1521 gst_buffer_unmap (buf, &info);
1522 gst_buffer_resize (buf, 0, length);
1524 GST_BUFFER_OFFSET (buf) = offset;
1525 GST_BUFFER_OFFSET_END (buf) = offset + length;
1534 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1535 gst_buffer_unmap (buf, &info);
1536 if (*buffer == NULL)
1537 gst_buffer_unref (buf);
1538 return GST_FLOW_EOS;
1542 GST_DEBUG_OBJECT (queue, "we are flushing");
1543 gst_buffer_unmap (buf, &info);
1544 if (*buffer == NULL)
1545 gst_buffer_unref (buf);
1546 return GST_FLOW_FLUSHING;
1550 GST_DEBUG_OBJECT (queue, "we have a read error");
1551 gst_buffer_unmap (buf, &info);
1552 if (*buffer == NULL)
1553 gst_buffer_unref (buf);
1558 /* should be called with QUEUE_LOCK */
1559 static GstMiniObject *
1560 gst_queue2_read_item_from_file (GstQueue2 * queue)
1562 GstMiniObject *item;
1564 if (queue->stream_start_event != NULL) {
1565 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1566 queue->stream_start_event = NULL;
1567 } else if (queue->starting_segment != NULL) {
1568 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1569 queue->starting_segment = NULL;
1572 GstBuffer *buffer = NULL;
1573 guint64 reading_pos;
1575 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1576 reading_pos = queue->read->reading_pos;
1578 reading_pos = queue->current->reading_pos;
1582 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1587 item = GST_MINI_OBJECT_CAST (buffer);
1590 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1600 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1601 * the temp filename. */
1603 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1608 if (queue->temp_file)
1609 goto already_opened;
1611 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1613 /* If temp_template was set, allocate a filename and open that filen */
1616 if (queue->temp_template == NULL)
1619 /* make copy of the template, we don't want to change this */
1620 name = g_strdup (queue->temp_template);
1621 fd = g_mkstemp (name);
1623 goto mkstemp_failed;
1625 /* open the file for update/writing */
1626 queue->temp_file = fdopen (fd, "wb+");
1627 /* error creating file */
1628 if (queue->temp_file == NULL)
1631 g_free (queue->temp_location);
1632 queue->temp_location = name;
1634 GST_QUEUE2_MUTEX_UNLOCK (queue);
1636 /* we can't emit the notify with the lock */
1637 g_object_notify (G_OBJECT (queue), "temp-location");
1639 GST_QUEUE2_MUTEX_LOCK (queue);
1641 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1648 GST_DEBUG_OBJECT (queue, "temp file was already open");
1653 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1654 (_("No Temp directory specified.")), (NULL));
1659 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1660 (_("Could not create temp file \"%s\"."), queue->temp_template),
1667 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1668 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1677 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1680 if (queue->temp_file == NULL)
1683 GST_DEBUG_OBJECT (queue, "closing temp file");
1685 fflush (queue->temp_file);
1686 fclose (queue->temp_file);
1688 if (queue->temp_remove) {
1689 if (remove (queue->temp_location) < 0) {
1690 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1691 queue->temp_location, g_strerror (errno));
1695 queue->temp_file = NULL;
1696 clean_ranges (queue);
1700 gst_queue2_flush_temp_file (GstQueue2 * queue)
1702 if (queue->temp_file == NULL)
1705 GST_DEBUG_OBJECT (queue, "flushing temp file");
1707 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1711 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1713 if (!QUEUE_IS_USING_QUEUE (queue)) {
1714 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1715 gst_queue2_flush_temp_file (queue);
1716 init_ranges (queue);
1718 while (!g_queue_is_empty (&queue->queue)) {
1719 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1721 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1722 && GST_EVENT_IS_STICKY (qitem->item)
1723 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1724 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1725 gst_pad_store_sticky_event (queue->srcpad,
1726 GST_EVENT_CAST (qitem->item));
1729 /* Then lose another reference because we are supposed to destroy that
1730 data when flushing */
1731 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1732 gst_mini_object_unref (qitem->item);
1733 g_slice_free (GstQueue2Item, qitem);
1736 queue->last_query = FALSE;
1737 g_cond_signal (&queue->query_handled);
1738 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1739 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1740 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1741 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1742 queue->sink_tainted = queue->src_tainted = TRUE;
1743 if (queue->starting_segment != NULL)
1744 gst_event_unref (queue->starting_segment);
1745 queue->starting_segment = NULL;
1746 queue->segment_event_received = FALSE;
1747 gst_event_replace (&queue->stream_start_event, NULL);
1749 /* we deleted a lot of something */
1750 GST_QUEUE2_SIGNAL_DEL (queue);
1754 gst_queue2_wait_free_space (GstQueue2 * queue)
1756 /* We make space available if we're "full" according to whatever
1757 * the user defined as "full". */
1758 if (gst_queue2_is_filled (queue)) {
1761 /* pause the timer while we wait. The fact that we are waiting does not mean
1762 * the byterate on the input pad is lower */
1763 if ((started = queue->in_timer_started))
1764 g_timer_stop (queue->in_timer);
1766 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1767 "queue is full, waiting for free space");
1769 /* Wait for space to be available, we could be unlocked because of a flush. */
1770 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1772 while (gst_queue2_is_filled (queue));
1774 /* and continue if we were running before */
1776 g_timer_continue (queue->in_timer);
1783 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1789 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1792 guint8 *data, *ring_buffer;
1793 guint size, rb_size;
1794 guint64 writing_pos, new_writing_pos;
1795 GstQueue2Range *range, *prev, *next;
1796 gboolean do_seek = FALSE;
1798 if (QUEUE_IS_USING_RING_BUFFER (queue))
1799 writing_pos = queue->current->rb_writing_pos;
1801 writing_pos = queue->current->writing_pos;
1802 ring_buffer = queue->ring_buffer;
1803 rb_size = queue->ring_buffer_max_size;
1805 gst_buffer_map (buffer, &info, GST_MAP_READ);
1810 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1814 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1815 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1816 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1817 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1818 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1824 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1827 /* calculate the space in the ring buffer not used by data from
1828 * the current range */
1829 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1830 /* wait until there is some free space */
1831 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1833 /* get the amount of space we have */
1834 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1836 /* calculate if we need to split or if we can write the entire
1838 to_write = MIN (size, space);
1840 /* the writing position in the ring buffer after writing (part
1841 * or all of) the buffer */
1842 new_writing_pos = (writing_pos + to_write) % rb_size;
1845 range = queue->ranges;
1847 /* if we need to overwrite data in the ring buffer, we need to
1850 * warning: this code is complicated and includes some
1851 * simplifications - pen, paper and diagrams for the cases
1854 guint64 range_data_start, range_data_end;
1855 GstQueue2Range *range_to_destroy = NULL;
1857 range_data_start = range->rb_offset;
1858 range_data_end = range->rb_writing_pos;
1860 /* handle the special case where the range has no data in it */
1861 if (range->writing_pos == range->offset) {
1862 if (range != queue->current) {
1863 GST_DEBUG_OBJECT (queue,
1864 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1865 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1867 range_to_destroy = range;
1869 prev->next = range->next;
1874 if (range_data_end > range_data_start) {
1875 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1878 if (new_writing_pos > range_data_start) {
1879 if (new_writing_pos >= range_data_end) {
1880 GST_DEBUG_OBJECT (queue,
1881 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1882 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1884 range_to_destroy = range;
1886 prev->next = range->next;
1888 GST_DEBUG_OBJECT (queue,
1889 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1890 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1891 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1892 range->offset + new_writing_pos - range_data_start,
1894 range->offset += (new_writing_pos - range_data_start);
1895 range->rb_offset = new_writing_pos;
1899 guint64 new_wpos_virt = writing_pos + to_write;
1901 if (new_wpos_virt <= range_data_start)
1904 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1905 GST_DEBUG_OBJECT (queue,
1906 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1907 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1909 range_to_destroy = range;
1911 prev->next = range->next;
1913 GST_DEBUG_OBJECT (queue,
1914 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1915 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1916 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1917 range->offset + new_writing_pos - range_data_start,
1919 range->offset += (new_wpos_virt - range_data_start);
1920 range->rb_offset = new_writing_pos;
1925 if (!range_to_destroy)
1928 range = range->next;
1929 if (range_to_destroy) {
1930 if (range_to_destroy == queue->ranges)
1931 queue->ranges = range;
1932 g_slice_free (GstQueue2Range, range_to_destroy);
1933 range_to_destroy = NULL;
1938 new_writing_pos = writing_pos + to_write;
1941 if (QUEUE_IS_USING_TEMP_FILE (queue)
1942 && FSEEK_FILE (queue->temp_file, writing_pos))
1945 if (new_writing_pos > writing_pos) {
1946 GST_INFO_OBJECT (queue,
1947 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1948 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1949 queue->current->writing_pos, queue->current->rb_writing_pos);
1950 /* either not using ring buffer or no wrapping, just write */
1951 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1952 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1955 memcpy (ring_buffer + writing_pos, data, to_write);
1958 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1959 /* try to merge with next range */
1960 while ((next = queue->current->next)) {
1961 GST_INFO_OBJECT (queue,
1962 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1963 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1964 if (new_writing_pos < next->offset)
1967 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1970 /* remove the group */
1971 queue->current->next = next->next;
1973 /* We use the threshold to decide if we want to do a seek or simply
1974 * read the data again. If there is not so much data in the range we
1975 * prefer to avoid to seek and read it again. */
1976 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
1977 /* the new range had more data than the threshold, it's worth keeping
1978 * it and doing a seek. */
1979 new_writing_pos = next->writing_pos;
1982 g_slice_free (GstQueue2Range, next);
1984 goto update_and_signal;
1988 guint block_one, block_two;
1990 block_one = rb_size - writing_pos;
1991 block_two = to_write - block_one;
1993 if (block_one > 0) {
1994 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1995 /* write data to end of ring buffer */
1996 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1997 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
2000 memcpy (ring_buffer + writing_pos, data, block_one);
2004 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
2007 if (block_two > 0) {
2008 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
2009 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2010 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
2013 memcpy (ring_buffer, data + block_one, block_two);
2019 /* update the writing positions */
2021 GST_INFO_OBJECT (queue,
2022 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2023 to_write, writing_pos, size);
2025 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2027 queue->current->writing_pos += to_write;
2028 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2030 queue->current->writing_pos = writing_pos = new_writing_pos;
2031 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
2033 if (queue->upstream_size == 0 || new_writing_pos < queue->upstream_size)
2034 perform_seek_to_offset (queue, new_writing_pos);
2036 queue->is_eos = TRUE;
2041 #ifndef TIZEN_FEATURE_QUEUE2_MODIFICATION
2043 perform_seek_to_offset (queue, new_writing_pos);
2046 update_cur_level (queue, queue->current);
2048 /* update the buffering status */
2049 if (queue->use_buffering)
2050 update_buffering (queue);
2052 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2053 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2055 GST_QUEUE2_SIGNAL_ADD (queue);
2058 gst_buffer_unmap (buffer, &info);
2065 GST_DEBUG_OBJECT (queue, "we are flushing");
2066 gst_buffer_unmap (buffer, &info);
2067 /* FIXME - GST_FLOW_EOS ? */
2072 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2073 gst_buffer_unmap (buffer, &info);
2080 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2084 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2085 (_("Error while writing to download file.")),
2086 ("%s", g_strerror (errno)));
2089 gst_buffer_unmap (buffer, &info);
2095 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2097 GstQueue2 *queue = q;
2099 GST_TRACE_OBJECT (queue,
2100 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2101 gst_buffer_get_size (*buf));
2103 if (!gst_queue2_create_write (queue, *buf)) {
2104 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2111 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2113 guint *p_size = data;
2116 buf_size = gst_buffer_get_size (*buf);
2117 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2118 *p_size += buf_size;
2122 /* enqueue an item an update the level stats */
2124 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2125 GstQueue2ItemType item_type)
2127 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2131 buffer = GST_BUFFER_CAST (item);
2132 size = gst_buffer_get_size (buffer);
2134 /* add buffer to the statistics */
2135 if (QUEUE_IS_USING_QUEUE (queue)) {
2136 queue->cur_level.buffers++;
2137 queue->cur_level.bytes += size;
2139 queue->bytes_in += size;
2141 /* apply new buffer to segment stats */
2142 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
2143 /* update the byterate stats */
2144 update_in_rates (queue);
2146 if (!QUEUE_IS_USING_QUEUE (queue)) {
2147 /* FIXME - check return value? */
2148 gst_queue2_create_write (queue, buffer);
2150 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2151 GstBufferList *buffer_list;
2154 buffer_list = GST_BUFFER_LIST_CAST (item);
2156 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2157 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2159 /* add buffer to the statistics */
2160 if (QUEUE_IS_USING_QUEUE (queue)) {
2161 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2162 queue->cur_level.bytes += size;
2164 queue->bytes_in += size;
2166 /* apply new buffer to segment stats */
2167 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2169 /* update the byterate stats */
2170 update_in_rates (queue);
2172 if (!QUEUE_IS_USING_QUEUE (queue)) {
2173 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2175 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2178 event = GST_EVENT_CAST (item);
2180 switch (GST_EVENT_TYPE (event)) {
2182 /* Zero the thresholds, this makes sure the queue is completely
2183 * filled and we can read all data from the queue. */
2184 GST_DEBUG_OBJECT (queue, "we have EOS");
2185 queue->is_eos = TRUE;
2187 case GST_EVENT_SEGMENT:
2188 apply_segment (queue, event, &queue->sink_segment, TRUE);
2189 /* This is our first new segment, we hold it
2190 * as we can't save it on the temp file */
2191 if (!QUEUE_IS_USING_QUEUE (queue)) {
2192 if (queue->segment_event_received)
2193 goto unexpected_event;
2195 queue->segment_event_received = TRUE;
2196 if (queue->starting_segment != NULL)
2197 gst_event_unref (queue->starting_segment);
2198 queue->starting_segment = event;
2201 /* a new segment allows us to accept more buffers if we got EOS
2202 * from downstream */
2203 queue->unexpected = FALSE;
2206 apply_gap (queue, event, &queue->sink_segment, TRUE);
2208 case GST_EVENT_STREAM_START:
2209 if (!QUEUE_IS_USING_QUEUE (queue)) {
2210 gst_event_replace (&queue->stream_start_event, event);
2211 gst_event_unref (event);
2215 case GST_EVENT_CAPS:{
2218 gst_event_parse_caps (event, &caps);
2219 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2221 if (!QUEUE_IS_USING_QUEUE (queue)) {
2222 GST_LOG ("Dropping caps event, not using queue");
2223 gst_event_unref (event);
2229 if (!QUEUE_IS_USING_QUEUE (queue))
2230 goto unexpected_event;
2233 } else if (GST_IS_QUERY (item)) {
2234 /* Can't happen as we check that in the caller */
2235 if (!QUEUE_IS_USING_QUEUE (queue))
2236 g_assert_not_reached ();
2238 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2239 item, GST_OBJECT_NAME (queue));
2240 /* we can't really unref since we don't know what it is */
2245 /* update the buffering status */
2246 if (queue->use_buffering)
2247 update_buffering (queue);
2249 if (QUEUE_IS_USING_QUEUE (queue)) {
2250 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2251 qitem->type = item_type;
2253 g_queue_push_tail (&queue->queue, qitem);
2255 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2258 GST_QUEUE2_SIGNAL_ADD (queue);
2267 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2268 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2269 GST_OBJECT_NAME (queue));
2270 gst_event_unref (GST_EVENT_CAST (item));
2275 /* dequeue an item from the queue and update level stats */
2276 static GstMiniObject *
2277 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2279 GstMiniObject *item;
2281 if (!QUEUE_IS_USING_QUEUE (queue)) {
2282 item = gst_queue2_read_item_from_file (queue);
2284 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2290 g_slice_free (GstQueue2Item, qitem);
2296 if (GST_IS_BUFFER (item)) {
2300 buffer = GST_BUFFER_CAST (item);
2301 size = gst_buffer_get_size (buffer);
2302 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2304 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2305 "retrieved buffer %p from queue", buffer);
2307 if (QUEUE_IS_USING_QUEUE (queue)) {
2308 queue->cur_level.buffers--;
2309 queue->cur_level.bytes -= size;
2311 queue->bytes_out += size;
2313 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2314 /* update the byterate stats */
2315 update_out_rates (queue);
2316 /* update the buffering */
2317 if (queue->use_buffering)
2318 update_buffering (queue);
2320 } else if (GST_IS_EVENT (item)) {
2321 GstEvent *event = GST_EVENT_CAST (item);
2323 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2325 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2326 "retrieved event %p from queue", event);
2328 switch (GST_EVENT_TYPE (event)) {
2330 /* queue is empty now that we dequeued the EOS */
2331 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2333 case GST_EVENT_SEGMENT:
2334 apply_segment (queue, event, &queue->src_segment, FALSE);
2337 apply_gap (queue, event, &queue->src_segment, FALSE);
2342 } else if (GST_IS_BUFFER_LIST (item)) {
2343 GstBufferList *buffer_list;
2346 buffer_list = GST_BUFFER_LIST_CAST (item);
2347 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2348 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2350 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2351 "retrieved buffer list %p from queue", buffer_list);
2353 if (QUEUE_IS_USING_QUEUE (queue)) {
2354 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2355 queue->cur_level.bytes -= size;
2357 queue->bytes_out += size;
2359 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2360 /* update the byterate stats */
2361 update_out_rates (queue);
2362 /* update the buffering */
2363 if (queue->use_buffering)
2364 update_buffering (queue);
2365 } else if (GST_IS_QUERY (item)) {
2366 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2367 "retrieved query %p from queue", item);
2368 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2371 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2372 item, GST_OBJECT_NAME (queue));
2374 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2376 GST_QUEUE2_SIGNAL_DEL (queue);
2383 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2389 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2392 gboolean ret = TRUE;
2395 queue = GST_QUEUE2 (parent);
2397 switch (GST_EVENT_TYPE (event)) {
2398 case GST_EVENT_FLUSH_START:
2400 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2401 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2403 ret = gst_pad_push_event (queue->srcpad, event);
2405 /* now unblock the chain function */
2406 GST_QUEUE2_MUTEX_LOCK (queue);
2407 queue->srcresult = GST_FLOW_FLUSHING;
2408 queue->sinkresult = GST_FLOW_FLUSHING;
2409 /* unblock the loop and chain functions */
2410 GST_QUEUE2_SIGNAL_ADD (queue);
2411 GST_QUEUE2_SIGNAL_DEL (queue);
2412 queue->last_query = FALSE;
2413 g_cond_signal (&queue->query_handled);
2414 GST_QUEUE2_MUTEX_UNLOCK (queue);
2416 /* make sure it pauses, this should happen since we sent
2417 * flush_start downstream. */
2418 gst_pad_pause_task (queue->srcpad);
2419 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2421 GST_QUEUE2_MUTEX_LOCK (queue);
2422 /* flush the sink pad */
2423 queue->sinkresult = GST_FLOW_FLUSHING;
2424 GST_QUEUE2_SIGNAL_DEL (queue);
2425 queue->last_query = FALSE;
2426 g_cond_signal (&queue->query_handled);
2427 GST_QUEUE2_MUTEX_UNLOCK (queue);
2429 gst_event_unref (event);
2433 case GST_EVENT_FLUSH_STOP:
2435 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2437 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2439 ret = gst_pad_push_event (queue->srcpad, event);
2441 GST_QUEUE2_MUTEX_LOCK (queue);
2442 gst_queue2_locked_flush (queue, FALSE, TRUE);
2443 queue->srcresult = GST_FLOW_OK;
2444 queue->sinkresult = GST_FLOW_OK;
2445 queue->is_eos = FALSE;
2446 queue->unexpected = FALSE;
2447 queue->seeking = FALSE;
2448 /* reset rate counters */
2449 reset_rate_timer (queue);
2450 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2451 queue->srcpad, NULL);
2452 GST_QUEUE2_MUTEX_UNLOCK (queue);
2454 GST_QUEUE2_MUTEX_LOCK (queue);
2455 queue->segment_event_received = FALSE;
2456 queue->is_eos = FALSE;
2457 queue->unexpected = FALSE;
2458 queue->sinkresult = GST_FLOW_OK;
2459 queue->seeking = FALSE;
2460 GST_QUEUE2_MUTEX_UNLOCK (queue);
2462 gst_event_unref (event);
2467 if (GST_EVENT_IS_SERIALIZED (event)) {
2468 /* serialized events go in the queue */
2469 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2470 if (queue->srcresult != GST_FLOW_OK) {
2471 /* Errors in sticky event pushing are no problem and ignored here
2472 * as they will cause more meaningful errors during data flow.
2473 * For EOS events, that are not followed by data flow, we still
2474 * return FALSE here though and report an error.
2476 if (!GST_EVENT_IS_STICKY (event)) {
2477 goto out_flow_error;
2478 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2479 if (queue->srcresult == GST_FLOW_NOT_LINKED
2480 || queue->srcresult < GST_FLOW_EOS) {
2481 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2482 (_("Internal data flow error.")),
2483 ("streaming task paused, reason %s (%d)",
2484 gst_flow_get_name (queue->srcresult), queue->srcresult));
2486 goto out_flow_error;
2489 /* refuse more events on EOS */
2492 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2493 GST_QUEUE2_MUTEX_UNLOCK (queue);
2494 gst_queue2_post_buffering (queue);
2496 /* non-serialized events are passed upstream. */
2497 ret = gst_pad_push_event (queue->srcpad, event);
2506 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2507 GST_QUEUE2_MUTEX_UNLOCK (queue);
2508 gst_event_unref (event);
2513 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2514 GST_QUEUE2_MUTEX_UNLOCK (queue);
2515 gst_event_unref (event);
2520 GST_LOG_OBJECT (queue,
2521 "refusing event, we have a downstream flow error: %s",
2522 gst_flow_get_name (queue->srcresult));
2523 GST_QUEUE2_MUTEX_UNLOCK (queue);
2524 gst_event_unref (event);
2530 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2536 queue = GST_QUEUE2 (parent);
2538 switch (GST_QUERY_TYPE (query)) {
2540 if (GST_QUERY_IS_SERIALIZED (query)) {
2541 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2542 /* serialized events go in the queue. We need to be certain that we
2543 * don't cause deadlocks waiting for the query return value. We check if
2544 * the queue is empty (nothing is blocking downstream and the query can
2545 * be pushed for sure) or we are not buffering. If we are buffering,
2546 * the pipeline waits to unblock downstream until our queue fills up
2547 * completely, which can not happen if we block on the query..
2548 * Therefore we only potentially block when we are not buffering. */
2549 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2550 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2551 || !queue->use_buffering)) {
2552 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2553 gst_queue2_locked_enqueue (queue, query,
2554 GST_QUEUE2_ITEM_TYPE_QUERY);
2556 STATUS (queue, queue->sinkpad, "wait for QUERY");
2557 g_cond_wait (&queue->query_handled, &queue->qlock);
2558 if (queue->sinkresult != GST_FLOW_OK)
2560 res = queue->last_query;
2562 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2566 GST_DEBUG_OBJECT (queue,
2567 "refusing query, we are not using the queue");
2570 GST_QUEUE2_MUTEX_UNLOCK (queue);
2571 gst_queue2_post_buffering (queue);
2573 res = gst_pad_query_default (pad, parent, query);
2582 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2583 GST_QUEUE2_MUTEX_UNLOCK (queue);
2589 gst_queue2_is_empty (GstQueue2 * queue)
2591 /* never empty on EOS */
2595 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2596 return queue->current->writing_pos <= queue->current->max_reading_pos;
2598 if (queue->queue.length == 0)
2606 gst_queue2_is_filled (GstQueue2 * queue)
2610 /* always filled on EOS */
2614 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2615 (queue->cur_level.format) >= ((alt_max) ? \
2616 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2618 /* if using a ring buffer we're filled if all ring buffer space is used
2619 * _by the current range_ */
2620 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2621 guint64 rb_size = queue->ring_buffer_max_size;
2622 GST_DEBUG_OBJECT (queue,
2623 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2624 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2625 return CHECK_FILLED (bytes, rb_size);
2628 /* if using file, we're never filled if we don't have EOS */
2629 if (QUEUE_IS_USING_TEMP_FILE (queue))
2632 /* we are never filled when we have no buffers at all */
2633 if (queue->cur_level.buffers == 0)
2636 /* we are filled if one of the current levels exceeds the max */
2637 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2638 || CHECK_FILLED (time, 0);
2640 /* if we need to, use the rate estimate to check against the max time we are
2641 * allowed to queue */
2642 if (queue->use_rate_estimate)
2643 res |= CHECK_FILLED (rate_time, 0);
2649 static GstFlowReturn
2650 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2651 GstMiniObject * item, GstQueue2ItemType item_type)
2653 /* we have to lock the queue since we span threads */
2654 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2655 /* when we received EOS, we refuse more data */
2658 /* when we received unexpected from downstream, refuse more buffers */
2659 if (queue->unexpected)
2660 goto out_unexpected;
2662 /* while we didn't receive the newsegment, we're seeking and we skip data */
2666 if (!gst_queue2_wait_free_space (queue))
2669 /* put buffer in queue now */
2670 gst_queue2_locked_enqueue (queue, item, item_type);
2671 GST_QUEUE2_MUTEX_UNLOCK (queue);
2672 gst_queue2_post_buffering (queue);
2676 /* special conditions */
2679 GstFlowReturn ret = queue->sinkresult;
2681 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2682 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2683 GST_QUEUE2_MUTEX_UNLOCK (queue);
2684 gst_mini_object_unref (item);
2690 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2691 GST_QUEUE2_MUTEX_UNLOCK (queue);
2692 gst_mini_object_unref (item);
2694 return GST_FLOW_EOS;
2698 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2699 GST_QUEUE2_MUTEX_UNLOCK (queue);
2700 gst_mini_object_unref (item);
2706 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2707 GST_QUEUE2_MUTEX_UNLOCK (queue);
2708 gst_mini_object_unref (item);
2710 return GST_FLOW_EOS;
2714 static GstFlowReturn
2715 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2719 queue = GST_QUEUE2 (parent);
2721 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2722 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2723 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2724 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2725 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2727 return gst_queue2_chain_buffer_or_buffer_list (queue,
2728 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2731 static GstFlowReturn
2732 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2733 GstBufferList * buffer_list)
2737 queue = GST_QUEUE2 (parent);
2739 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2740 "received buffer list %p", buffer_list);
2742 return gst_queue2_chain_buffer_or_buffer_list (queue,
2743 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2746 static GstMiniObject *
2747 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2749 GstMiniObject *data;
2751 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2753 /* stop pushing buffers, we dequeue all items until we see an item that we
2754 * can push again, which is EOS or SEGMENT. If there is nothing in the
2755 * queue we can push, we set a flag to make the sinkpad refuse more
2756 * buffers with an EOS return value until we receive something
2757 * pushable again or we get flushed. */
2758 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2759 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2760 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2761 "dropping EOS buffer %p", data);
2762 gst_buffer_unref (GST_BUFFER_CAST (data));
2763 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2764 GstEvent *event = GST_EVENT_CAST (data);
2765 GstEventType type = GST_EVENT_TYPE (event);
2767 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2768 /* we found a pushable item in the queue, push it out */
2769 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2770 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2773 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2774 "dropping EOS event %p", event);
2775 gst_event_unref (event);
2776 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2777 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2778 "dropping EOS buffer list %p", data);
2779 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2780 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2781 queue->last_query = FALSE;
2782 g_cond_signal (&queue->query_handled);
2783 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2786 /* no more items in the queue. Set the unexpected flag so that upstream
2787 * make us refuse any more buffers on the sinkpad. Since we will still
2788 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2789 * task function does not shut down. */
2790 queue->unexpected = TRUE;
2794 /* dequeue an item from the queue an push it downstream. This functions returns
2795 * the result of the push. */
2796 static GstFlowReturn
2797 gst_queue2_push_one (GstQueue2 * queue)
2799 GstFlowReturn result = queue->srcresult;
2800 GstMiniObject *data;
2801 GstQueue2ItemType item_type;
2803 data = gst_queue2_locked_dequeue (queue, &item_type);
2808 STATUS (queue, queue->srcpad, "We have something dequeud");
2809 g_atomic_int_set (&queue->downstream_may_block,
2810 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2811 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2812 GST_QUEUE2_MUTEX_UNLOCK (queue);
2813 gst_queue2_post_buffering (queue);
2815 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2818 buffer = GST_BUFFER_CAST (data);
2820 result = gst_pad_push (queue->srcpad, buffer);
2821 g_atomic_int_set (&queue->downstream_may_block, 0);
2823 /* need to check for srcresult here as well */
2824 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2825 if (result == GST_FLOW_EOS) {
2826 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2829 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2830 * to the caller so that the task function does not shut down */
2831 result = GST_FLOW_OK;
2833 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2834 GstEvent *event = GST_EVENT_CAST (data);
2835 GstEventType type = GST_EVENT_TYPE (event);
2837 gst_pad_push_event (queue->srcpad, event);
2839 /* if we're EOS, return EOS so that the task pauses. */
2840 if (type == GST_EVENT_EOS) {
2841 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2842 "pushed EOS event %p, return EOS", event);
2843 result = GST_FLOW_EOS;
2846 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2847 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2848 GstBufferList *buffer_list;
2850 buffer_list = GST_BUFFER_LIST_CAST (data);
2852 result = gst_pad_push_list (queue->srcpad, buffer_list);
2853 g_atomic_int_set (&queue->downstream_may_block, 0);
2855 /* need to check for srcresult here as well */
2856 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2857 if (result == GST_FLOW_EOS) {
2858 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2861 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2862 * to the caller so that the task function does not shut down */
2863 result = GST_FLOW_OK;
2865 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2866 GstQuery *query = GST_QUERY_CAST (data);
2868 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2869 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2870 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2871 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2872 "did query %p, return %d", query, queue->last_query);
2873 g_cond_signal (&queue->query_handled);
2874 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2875 result = GST_FLOW_OK;
2882 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2883 "exit because we have no item in the queue");
2884 return GST_FLOW_ERROR;
2888 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2889 return GST_FLOW_FLUSHING;
2893 /* called repeatedly with @pad as the source pad. This function should push out
2894 * data to the peer element. */
2896 gst_queue2_loop (GstPad * pad)
2901 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2903 /* have to lock for thread-safety */
2904 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2906 if (gst_queue2_is_empty (queue)) {
2909 /* pause the timer while we wait. The fact that we are waiting does not mean
2910 * the byterate on the output pad is lower */
2911 if ((started = queue->out_timer_started))
2912 g_timer_stop (queue->out_timer);
2914 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2915 "queue is empty, waiting for new data");
2917 /* Wait for data to be available, we could be unlocked because of a flush. */
2918 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2920 while (gst_queue2_is_empty (queue));
2922 /* and continue if we were running before */
2924 g_timer_continue (queue->out_timer);
2926 ret = gst_queue2_push_one (queue);
2927 queue->srcresult = ret;
2928 queue->sinkresult = ret;
2929 if (ret != GST_FLOW_OK)
2932 GST_QUEUE2_MUTEX_UNLOCK (queue);
2933 gst_queue2_post_buffering (queue);
2940 gboolean eos = queue->is_eos;
2941 GstFlowReturn ret = queue->srcresult;
2943 gst_pad_pause_task (queue->srcpad);
2944 if (ret == GST_FLOW_FLUSHING) {
2945 gst_queue2_locked_flush (queue, FALSE, FALSE);
2947 GST_QUEUE2_SIGNAL_DEL (queue);
2948 queue->last_query = FALSE;
2949 g_cond_signal (&queue->query_handled);
2951 GST_QUEUE2_MUTEX_UNLOCK (queue);
2952 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2953 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2954 /* let app know about us giving up if upstream is not expected to do so */
2955 /* EOS is already taken care of elsewhere */
2956 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2957 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2958 (_("Internal data flow error.")),
2959 ("streaming task paused, reason %s (%d)",
2960 gst_flow_get_name (ret), ret));
2961 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2968 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2970 gboolean res = TRUE;
2971 GstQueue2 *queue = GST_QUEUE2 (parent);
2973 #ifndef GST_DISABLE_GST_DEBUG
2974 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2975 event, GST_EVENT_TYPE_NAME (event));
2978 switch (GST_EVENT_TYPE (event)) {
2979 case GST_EVENT_FLUSH_START:
2980 if (QUEUE_IS_USING_QUEUE (queue)) {
2981 /* just forward upstream */
2982 res = gst_pad_push_event (queue->sinkpad, event);
2984 /* now unblock the getrange function */
2985 GST_QUEUE2_MUTEX_LOCK (queue);
2986 GST_DEBUG_OBJECT (queue, "flushing");
2987 queue->srcresult = GST_FLOW_FLUSHING;
2988 GST_QUEUE2_SIGNAL_ADD (queue);
2989 GST_QUEUE2_MUTEX_UNLOCK (queue);
2991 /* when using a temp file, we eat the event */
2993 gst_event_unref (event);
2996 case GST_EVENT_FLUSH_STOP:
2997 if (QUEUE_IS_USING_QUEUE (queue)) {
2998 /* just forward upstream */
2999 res = gst_pad_push_event (queue->sinkpad, event);
3001 /* now unblock the getrange function */
3002 GST_QUEUE2_MUTEX_LOCK (queue);
3003 queue->srcresult = GST_FLOW_OK;
3004 GST_QUEUE2_MUTEX_UNLOCK (queue);
3006 /* when using a temp file, we eat the event */
3008 gst_event_unref (event);
3011 case GST_EVENT_RECONFIGURE:
3012 GST_QUEUE2_MUTEX_LOCK (queue);
3013 /* assume downstream is linked now and try to push again */
3014 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3015 queue->srcresult = GST_FLOW_OK;
3016 queue->sinkresult = GST_FLOW_OK;
3017 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3018 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3022 GST_QUEUE2_MUTEX_UNLOCK (queue);
3024 res = gst_pad_push_event (queue->sinkpad, event);
3027 res = gst_pad_push_event (queue->sinkpad, event);
3035 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3039 queue = GST_QUEUE2 (parent);
3041 switch (GST_QUERY_TYPE (query)) {
3042 case GST_QUERY_POSITION:
3047 if (!gst_pad_peer_query (queue->sinkpad, query))
3050 /* get peer position */
3051 gst_query_parse_position (query, &format, &peer_pos);
3053 /* FIXME: this code assumes that there's no discont in the queue */
3055 case GST_FORMAT_BYTES:
3056 peer_pos -= queue->cur_level.bytes;
3058 case GST_FORMAT_TIME:
3059 peer_pos -= queue->cur_level.time;
3062 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3063 "know how to adjust value", gst_format_get_name (format));
3066 /* set updated position */
3067 gst_query_set_position (query, format, peer_pos);
3070 case GST_QUERY_DURATION:
3072 GST_DEBUG_OBJECT (queue, "doing peer query");
3074 if (!gst_pad_peer_query (queue->sinkpad, query))
3077 GST_DEBUG_OBJECT (queue, "peer query success");
3080 case GST_QUERY_BUFFERING:
3083 gboolean is_buffering;
3084 GstBufferingMode mode;
3085 gint avg_in, avg_out;
3086 gint64 buffering_left;
3088 GST_DEBUG_OBJECT (queue, "query buffering");
3090 get_buffering_percent (queue, &is_buffering, &percent);
3091 gst_query_set_buffering_percent (query, is_buffering, percent);
3093 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3095 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3098 if (!QUEUE_IS_USING_QUEUE (queue)) {
3099 /* add ranges for download and ringbuffer buffering */
3101 gint64 start, stop, range_start, range_stop;
3102 guint64 writing_pos;
3103 gint64 estimated_total;
3105 gboolean peer_res, is_eos;
3106 GstQueue2Range *queued_ranges;
3108 /* we need a current download region */
3109 if (queue->current == NULL)
3112 writing_pos = queue->current->writing_pos;
3113 is_eos = queue->is_eos;
3116 /* we're EOS, we know the duration in bytes now */
3118 duration = writing_pos;
3120 /* get duration of upstream in bytes */
3121 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3122 GST_FORMAT_BYTES, &duration);
3125 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3126 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3128 /* calculate remaining and total download time */
3129 if (peer_res && avg_in > 0.0)
3130 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3132 estimated_total = -1;
3134 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3137 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3140 case GST_FORMAT_PERCENT:
3141 /* we need duration */
3146 /* get our available data relative to the duration */
3149 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3154 case GST_FORMAT_BYTES:
3164 /* fill out the buffered ranges */
3165 for (queued_ranges = queue->ranges; queued_ranges;
3166 queued_ranges = queued_ranges->next) {
3168 case GST_FORMAT_PERCENT:
3169 if (duration == -1) {
3174 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3176 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3177 queued_ranges->reading_pos, duration);
3180 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3181 queued_ranges->offset, duration);
3184 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3185 queued_ranges->writing_pos, duration);
3187 case GST_FORMAT_BYTES:
3188 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3189 range_start = queued_ranges->reading_pos;
3191 range_start = queued_ranges->offset;
3193 range_stop = queued_ranges->writing_pos;
3200 if (range_start == range_stop)
3202 GST_DEBUG_OBJECT (queue,
3203 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3204 G_GINT64_FORMAT, range_start, range_stop);
3205 gst_query_add_buffering_range (query, range_start, range_stop);
3208 gst_query_set_buffering_range (query, format, start, stop,
3213 case GST_QUERY_SCHEDULING:
3216 GstSchedulingFlags flags = 0;
3217 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3218 if ((!QUEUE_IS_USING_QUEUE (queue)) && !gst_pad_peer_query (queue->sinkpad, query))
3220 if (!gst_pad_peer_query (queue->sinkpad, query))
3224 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3226 /* we can operate in pull mode when we are using a tempfile */
3227 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3230 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3231 gst_query_set_scheduling (query, flags, 0, -1, 0);
3233 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3234 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3238 /* peer handled other queries */
3239 if (!gst_pad_query_default (pad, parent, query))
3249 GST_DEBUG_OBJECT (queue, "failed peer query");
3255 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3257 GstQueue2 *queue = GST_QUEUE2 (element);
3259 /* simply forward to the srcpad query function */
3260 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3265 gst_queue2_update_upstream_size (GstQueue2 * queue)
3267 gint64 upstream_size = -1;
3269 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3271 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3273 /* upstream_size can be negative but queue->upstream_size is unsigned.
3274 * Prevent setting negative values to it (the query can return -1) */
3275 if (upstream_size >= 0)
3276 queue->upstream_size = upstream_size;
3278 queue->upstream_size = 0;
3282 static GstFlowReturn
3283 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3284 guint length, GstBuffer ** buffer)
3289 queue = GST_QUEUE2_CAST (parent);
3291 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3292 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3293 offset = (offset == -1) ? queue->current->reading_pos : offset;
3295 GST_DEBUG_OBJECT (queue,
3296 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3298 /* catch any reads beyond the size of the file here to make sure queue2
3299 * doesn't send seek events beyond the size of the file upstream, since
3300 * that would confuse elements such as souphttpsrc and/or http servers.
3301 * Demuxers often just loop until EOS at the end of the file to figure out
3302 * when they've read all the end-headers or index chunks. */
3303 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3304 gst_queue2_update_upstream_size (queue);
3305 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3306 goto out_unexpected;
3309 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3310 gst_queue2_update_upstream_size (queue);
3311 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3312 length = queue->upstream_size - offset;
3313 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3317 /* FIXME - function will block when the range is not yet available */
3318 ret = gst_queue2_create_read (queue, offset, length, buffer);
3319 GST_QUEUE2_MUTEX_UNLOCK (queue);
3320 gst_queue2_post_buffering (queue);
3327 ret = queue->srcresult;
3329 GST_DEBUG_OBJECT (queue, "we are flushing");
3330 GST_QUEUE2_MUTEX_UNLOCK (queue);
3335 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3336 GST_QUEUE2_MUTEX_UNLOCK (queue);
3337 return GST_FLOW_EOS;
3341 /* sink currently only operates in push mode */
3343 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3344 GstPadMode mode, gboolean active)
3349 queue = GST_QUEUE2 (parent);
3352 case GST_PAD_MODE_PUSH:
3354 GST_QUEUE2_MUTEX_LOCK (queue);
3355 GST_DEBUG_OBJECT (queue, "activating push mode");
3356 queue->srcresult = GST_FLOW_OK;
3357 queue->sinkresult = GST_FLOW_OK;
3358 queue->is_eos = FALSE;
3359 queue->unexpected = FALSE;
3360 reset_rate_timer (queue);
3361 GST_QUEUE2_MUTEX_UNLOCK (queue);
3363 /* unblock chain function */
3364 GST_QUEUE2_MUTEX_LOCK (queue);
3365 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3366 queue->srcresult = GST_FLOW_FLUSHING;
3367 queue->sinkresult = GST_FLOW_FLUSHING;
3368 GST_QUEUE2_SIGNAL_DEL (queue);
3369 /* Unblock query handler */
3370 queue->last_query = FALSE;
3371 g_cond_signal (&queue->query_handled);
3372 GST_QUEUE2_MUTEX_UNLOCK (queue);
3374 /* wait until it is unblocked and clean up */
3375 GST_PAD_STREAM_LOCK (pad);
3376 GST_QUEUE2_MUTEX_LOCK (queue);
3377 gst_queue2_locked_flush (queue, TRUE, FALSE);
3378 GST_QUEUE2_MUTEX_UNLOCK (queue);
3379 GST_PAD_STREAM_UNLOCK (pad);
3390 /* src operating in push mode, we start a task on the source pad that pushes out
3391 * buffers from the queue */
3393 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3395 gboolean result = FALSE;
3398 queue = GST_QUEUE2 (parent);
3401 GST_QUEUE2_MUTEX_LOCK (queue);
3402 GST_DEBUG_OBJECT (queue, "activating push mode");
3403 queue->srcresult = GST_FLOW_OK;
3404 queue->sinkresult = GST_FLOW_OK;
3405 queue->is_eos = FALSE;
3406 queue->unexpected = FALSE;
3408 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3409 GST_QUEUE2_MUTEX_UNLOCK (queue);
3411 /* unblock loop function */
3412 GST_QUEUE2_MUTEX_LOCK (queue);
3413 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3414 queue->srcresult = GST_FLOW_FLUSHING;
3415 queue->sinkresult = GST_FLOW_FLUSHING;
3416 /* the item add signal will unblock */
3417 GST_QUEUE2_SIGNAL_ADD (queue);
3418 GST_QUEUE2_MUTEX_UNLOCK (queue);
3420 /* step 2, make sure streaming finishes */
3421 result = gst_pad_stop_task (pad);
3427 /* pull mode, downstream will call our getrange function */
3429 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3434 queue = GST_QUEUE2 (parent);
3437 GST_QUEUE2_MUTEX_LOCK (queue);
3438 if (!QUEUE_IS_USING_QUEUE (queue)) {
3439 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3440 /* open the temp file now */
3441 result = gst_queue2_open_temp_location_file (queue);
3442 } else if (!queue->ring_buffer) {
3443 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3444 result = ! !queue->ring_buffer;
3449 GST_DEBUG_OBJECT (queue, "activating pull mode");
3450 init_ranges (queue);
3451 queue->srcresult = GST_FLOW_OK;
3452 queue->sinkresult = GST_FLOW_OK;
3453 queue->is_eos = FALSE;
3454 queue->unexpected = FALSE;
3455 queue->upstream_size = 0;
3457 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3458 /* this is not allowed, we cannot operate in pull mode without a temp
3460 queue->srcresult = GST_FLOW_FLUSHING;
3461 queue->sinkresult = GST_FLOW_FLUSHING;
3464 GST_QUEUE2_MUTEX_UNLOCK (queue);
3466 GST_QUEUE2_MUTEX_LOCK (queue);
3467 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3468 queue->srcresult = GST_FLOW_FLUSHING;
3469 queue->sinkresult = GST_FLOW_FLUSHING;
3470 /* this will unlock getrange */
3471 GST_QUEUE2_SIGNAL_ADD (queue);
3473 GST_QUEUE2_MUTEX_UNLOCK (queue);
3480 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3486 case GST_PAD_MODE_PULL:
3487 res = gst_queue2_src_activate_pull (pad, parent, active);
3489 case GST_PAD_MODE_PUSH:
3490 res = gst_queue2_src_activate_push (pad, parent, active);
3493 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3500 static GstStateChangeReturn
3501 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3504 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3506 queue = GST_QUEUE2 (element);
3508 switch (transition) {
3509 case GST_STATE_CHANGE_NULL_TO_READY:
3511 case GST_STATE_CHANGE_READY_TO_PAUSED:
3512 GST_QUEUE2_MUTEX_LOCK (queue);
3513 if (!QUEUE_IS_USING_QUEUE (queue)) {
3514 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3515 if (!gst_queue2_open_temp_location_file (queue))
3516 ret = GST_STATE_CHANGE_FAILURE;
3518 if (queue->ring_buffer) {
3519 g_free (queue->ring_buffer);
3520 queue->ring_buffer = NULL;
3522 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3523 ret = GST_STATE_CHANGE_FAILURE;
3525 init_ranges (queue);
3527 queue->segment_event_received = FALSE;
3528 queue->starting_segment = NULL;
3529 gst_event_replace (&queue->stream_start_event, NULL);
3530 GST_QUEUE2_MUTEX_UNLOCK (queue);
3532 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3538 if (ret == GST_STATE_CHANGE_FAILURE)
3541 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3543 if (ret == GST_STATE_CHANGE_FAILURE)
3546 switch (transition) {
3547 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3549 case GST_STATE_CHANGE_PAUSED_TO_READY:
3550 GST_QUEUE2_MUTEX_LOCK (queue);
3551 if (!QUEUE_IS_USING_QUEUE (queue)) {
3552 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3553 gst_queue2_close_temp_location_file (queue);
3554 } else if (queue->ring_buffer) {
3555 g_free (queue->ring_buffer);
3556 queue->ring_buffer = NULL;
3558 clean_ranges (queue);
3560 if (queue->starting_segment != NULL) {
3561 gst_event_unref (queue->starting_segment);
3562 queue->starting_segment = NULL;
3564 gst_event_replace (&queue->stream_start_event, NULL);
3565 GST_QUEUE2_MUTEX_UNLOCK (queue);
3567 case GST_STATE_CHANGE_READY_TO_NULL:
3576 /* changing the capacity of the queue must wake up
3577 * the _chain function, it might have more room now
3578 * to store the buffer/event in the queue */
3579 #define QUEUE_CAPACITY_CHANGE(q) \
3580 GST_QUEUE2_SIGNAL_DEL (queue); \
3581 if (queue->use_buffering) \
3582 update_buffering (queue);
3584 /* Changing the minimum required fill level must
3585 * wake up the _loop function as it might now
3586 * be able to preceed.
3588 #define QUEUE_THRESHOLD_CHANGE(q)\
3589 GST_QUEUE2_SIGNAL_ADD (queue);
3592 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3596 /* the element must be stopped in order to do this */
3597 GST_OBJECT_LOCK (queue);
3598 state = GST_STATE (queue);
3599 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3601 GST_OBJECT_UNLOCK (queue);
3603 /* set new location */
3604 g_free (queue->temp_template);
3605 queue->temp_template = g_strdup (template);
3612 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3613 GST_OBJECT_UNLOCK (queue);
3618 gst_queue2_set_property (GObject * object,
3619 guint prop_id, const GValue * value, GParamSpec * pspec)
3621 GstQueue2 *queue = GST_QUEUE2 (object);
3623 /* someone could change levels here, and since this
3624 * affects the get/put funcs, we need to lock for safety. */
3625 GST_QUEUE2_MUTEX_LOCK (queue);
3628 case PROP_MAX_SIZE_BYTES:
3629 queue->max_level.bytes = g_value_get_uint (value);
3630 QUEUE_CAPACITY_CHANGE (queue);
3632 case PROP_MAX_SIZE_BUFFERS:
3633 queue->max_level.buffers = g_value_get_uint (value);
3634 QUEUE_CAPACITY_CHANGE (queue);
3636 case PROP_MAX_SIZE_TIME:
3637 queue->max_level.time = g_value_get_uint64 (value);
3638 /* set rate_time to the same value. We use an extra field in the level
3639 * structure so that we can easily access and compare it */
3640 queue->max_level.rate_time = queue->max_level.time;
3641 QUEUE_CAPACITY_CHANGE (queue);
3643 case PROP_USE_BUFFERING:
3644 queue->use_buffering = g_value_get_boolean (value);
3645 if (!queue->use_buffering && queue->is_buffering) {
3646 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3647 "posting 100%% message");
3648 SET_PERCENT (queue, 100);
3649 queue->is_buffering = FALSE;
3652 if (queue->use_buffering) {
3653 queue->is_buffering = TRUE;
3654 update_buffering (queue);
3657 case PROP_USE_RATE_ESTIMATE:
3658 queue->use_rate_estimate = g_value_get_boolean (value);
3660 case PROP_LOW_PERCENT:
3661 queue->low_percent = g_value_get_int (value);
3663 case PROP_HIGH_PERCENT:
3664 queue->high_percent = g_value_get_int (value);
3666 case PROP_TEMP_TEMPLATE:
3667 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3669 case PROP_TEMP_REMOVE:
3670 queue->temp_remove = g_value_get_boolean (value);
3672 case PROP_RING_BUFFER_MAX_SIZE:
3673 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3676 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3680 GST_QUEUE2_MUTEX_UNLOCK (queue);
3681 gst_queue2_post_buffering (queue);
3685 gst_queue2_get_property (GObject * object,
3686 guint prop_id, GValue * value, GParamSpec * pspec)
3688 GstQueue2 *queue = GST_QUEUE2 (object);
3690 GST_QUEUE2_MUTEX_LOCK (queue);
3693 case PROP_CUR_LEVEL_BYTES:
3694 g_value_set_uint (value, queue->cur_level.bytes);
3696 case PROP_CUR_LEVEL_BUFFERS:
3697 g_value_set_uint (value, queue->cur_level.buffers);
3699 case PROP_CUR_LEVEL_TIME:
3700 g_value_set_uint64 (value, queue->cur_level.time);
3702 case PROP_MAX_SIZE_BYTES:
3703 g_value_set_uint (value, queue->max_level.bytes);
3705 case PROP_MAX_SIZE_BUFFERS:
3706 g_value_set_uint (value, queue->max_level.buffers);
3708 case PROP_MAX_SIZE_TIME:
3709 g_value_set_uint64 (value, queue->max_level.time);
3711 case PROP_USE_BUFFERING:
3712 g_value_set_boolean (value, queue->use_buffering);
3714 case PROP_USE_RATE_ESTIMATE:
3715 g_value_set_boolean (value, queue->use_rate_estimate);
3717 case PROP_LOW_PERCENT:
3718 g_value_set_int (value, queue->low_percent);
3720 case PROP_HIGH_PERCENT:
3721 g_value_set_int (value, queue->high_percent);
3723 case PROP_TEMP_TEMPLATE:
3724 g_value_set_string (value, queue->temp_template);
3726 case PROP_TEMP_LOCATION:
3727 g_value_set_string (value, queue->temp_location);
3729 case PROP_TEMP_REMOVE:
3730 g_value_set_boolean (value, queue->temp_remove);
3732 case PROP_RING_BUFFER_MAX_SIZE:
3733 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3736 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3740 GST_QUEUE2_MUTEX_UNLOCK (queue);