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
30 * Data is queued until one of the limits specified by the
31 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
32 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
33 * more buffers into the queue will block the pushing thread until more space
36 * The queue will create a new thread on the source pad to decouple the
37 * processing on sink and source pad.
39 * You can query how many buffers are queued by reading the
40 * #GstQueue2:current-level-buffers property.
42 * The default queue size limits are 100 buffers, 2MB of data, or
43 * two seconds worth of data, whichever is reached first.
45 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
46 * will allocate a random free filename and buffer data in the file.
47 * By using this, it will buffer the entire stream data on the file independently
48 * of the queue size limits, they will only be used for buffering statistics.
50 * The temp-location property will be used to notify the application of the
58 #include "gstqueue2.h"
60 #include <glib/gstdio.h>
62 #include "gst/gst-i18n-lib.h"
63 #include "gst/glib-compat-private.h"
68 #include <io.h> /* lseek, open, close, read */
70 #define lseek _lseeki64
77 #ifdef __BIONIC__ /* Android */
81 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
86 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
91 GST_DEBUG_CATEGORY_STATIC (queue_debug);
92 #define GST_CAT_DEFAULT (queue_debug)
93 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
101 #define DEFAULT_BUFFER_SIZE 4096
102 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
103 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
104 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
106 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
108 /* default property values */
109 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
110 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
111 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
112 #define DEFAULT_USE_BUFFERING FALSE
113 #define DEFAULT_USE_TAGS_BITRATE FALSE
114 #define DEFAULT_USE_RATE_ESTIMATE TRUE
115 #define DEFAULT_LOW_PERCENT 10
116 #define DEFAULT_HIGH_PERCENT 99
117 #define DEFAULT_LOW_WATERMARK 0.01
118 #define DEFAULT_HIGH_WATERMARK 0.99
119 #define DEFAULT_TEMP_REMOVE TRUE
120 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
125 PROP_CUR_LEVEL_BUFFERS,
126 PROP_CUR_LEVEL_BYTES,
128 PROP_MAX_SIZE_BUFFERS,
132 PROP_USE_TAGS_BITRATE,
133 PROP_USE_RATE_ESTIMATE,
141 PROP_RING_BUFFER_MAX_SIZE,
146 /* Explanation for buffer levels and percentages:
148 * The buffering_level functions here return a value in a normalized range
149 * that specifies the queue's current fill level. The range goes from 0 to
150 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
152 * This is not to be confused with the buffering_percent value, which is
153 * a *relative* quantity - relative to the low/high watermarks.
154 * buffering_percent = 0% means buffering_level is at the low watermark.
155 * buffering_percent = 100% means buffering_level is at the high watermark.
156 * buffering_percent is used for determining if the fill level has reached
157 * the high watermark, and for producing BUFFERING messages. This value
158 * always uses a 0..100 range (since it is a percentage).
160 * To avoid future confusions, whenever "buffering level" is mentioned, it
161 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
162 * range. Whenever "buffering_percent" is mentioned, it refers to the
163 * percentage value that is relative to the low/high watermark. */
165 /* Using a buffering level range of 0..1000000 to allow for a
166 * resolution in ppm (1 ppm = 0.0001%) */
167 #define MAX_BUFFERING_LEVEL 1000000
169 /* How much 1% makes up in the buffer level range */
170 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
172 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
179 #define STATUS(queue, pad, msg) \
180 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
181 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
182 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
183 " ns, %"G_GUINT64_FORMAT" items", \
184 GST_DEBUG_PAD_NAME (pad), \
185 queue->cur_level.buffers, \
186 queue->max_level.buffers, \
187 queue->cur_level.bytes, \
188 queue->max_level.bytes, \
189 queue->cur_level.time, \
190 queue->max_level.time, \
191 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
192 queue->current->writing_pos - queue->current->max_reading_pos : \
193 gst_queue_array_get_length(queue->queue)))
195 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
196 g_mutex_lock (&q->qlock); \
199 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
200 GST_QUEUE2_MUTEX_LOCK (q); \
201 if (res != GST_FLOW_OK) \
205 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
206 g_mutex_unlock (&q->qlock); \
209 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
210 STATUS (queue, q->sinkpad, "wait for DEL"); \
211 q->waiting_del = TRUE; \
212 g_cond_wait (&q->item_del, &queue->qlock); \
213 q->waiting_del = FALSE; \
214 if (res != GST_FLOW_OK) { \
215 STATUS (queue, q->srcpad, "received DEL wakeup"); \
218 STATUS (queue, q->sinkpad, "received DEL"); \
221 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
222 STATUS (queue, q->srcpad, "wait for ADD"); \
223 q->waiting_add = TRUE; \
224 g_cond_wait (&q->item_add, &q->qlock); \
225 q->waiting_add = FALSE; \
226 if (res != GST_FLOW_OK) { \
227 STATUS (queue, q->srcpad, "received ADD wakeup"); \
230 STATUS (queue, q->srcpad, "received ADD"); \
233 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
234 if (q->waiting_del) { \
235 STATUS (q, q->srcpad, "signal DEL"); \
236 g_cond_signal (&q->item_del); \
240 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
241 if (q->waiting_add) { \
242 STATUS (q, q->sinkpad, "signal ADD"); \
243 g_cond_signal (&q->item_add); \
247 #define SET_PERCENT(q, perc) G_STMT_START { \
248 if (perc != q->buffering_percent) { \
249 q->buffering_percent = perc; \
250 q->percent_changed = TRUE; \
251 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
252 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
253 &q->buffering_left); \
258 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
259 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
260 "dataflow inside the queue element");
261 #define gst_queue2_parent_class parent_class
262 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
264 static void gst_queue2_finalize (GObject * object);
266 static void gst_queue2_set_property (GObject * object,
267 guint prop_id, const GValue * value, GParamSpec * pspec);
268 static void gst_queue2_get_property (GObject * object,
269 guint prop_id, GValue * value, GParamSpec * pspec);
271 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
273 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
274 GstBufferList * buffer_list);
275 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
276 static void gst_queue2_loop (GstPad * pad);
278 static GstFlowReturn gst_queue2_handle_sink_event (GstPad * pad,
279 GstObject * parent, GstEvent * event);
280 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
283 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
285 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
287 static gboolean gst_queue2_handle_query (GstElement * element,
290 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
291 guint64 offset, guint length, GstBuffer ** buffer);
293 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
294 GstPadMode mode, gboolean active);
295 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
296 GstPadMode mode, gboolean active);
297 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
298 GstStateChange transition);
300 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
301 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
303 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
304 static void update_in_rates (GstQueue2 * queue, gboolean force);
305 static GstMessage *gst_queue2_get_buffering_message (GstQueue2 * queue);
306 static void gst_queue2_post_buffering (GstQueue2 * queue);
310 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
311 GST_QUEUE2_ITEM_TYPE_BUFFER,
312 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
313 GST_QUEUE2_ITEM_TYPE_EVENT,
314 GST_QUEUE2_ITEM_TYPE_QUERY
319 GstQueue2ItemType type;
323 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
326 gst_queue2_class_init (GstQueue2Class * klass)
328 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
329 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
331 gobject_class->set_property = gst_queue2_set_property;
332 gobject_class->get_property = gst_queue2_get_property;
335 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
336 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
337 "Current amount of data in the queue (bytes)",
338 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
340 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
341 "Current number of buffers in the queue",
342 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
344 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
345 "Current amount of data in the queue (in ns)",
346 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
349 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
350 "Max. amount of data in the queue (bytes, 0=disable)",
351 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
352 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
353 G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
355 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
356 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
357 DEFAULT_MAX_SIZE_BUFFERS,
358 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
359 G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
361 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
362 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
363 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
364 G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
367 g_param_spec_boolean ("use-buffering", "Use buffering",
368 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
369 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
370 G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_USE_TAGS_BITRATE,
372 g_param_spec_boolean ("use-tags-bitrate", "Use bitrate from tags",
373 "Use a bitrate from upstream tags to estimate buffer duration if not provided",
374 DEFAULT_USE_TAGS_BITRATE,
375 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
376 G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
378 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
379 "Estimate the bitrate of the stream to calculate time level",
380 DEFAULT_USE_RATE_ESTIMATE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
382 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
383 g_param_spec_int ("low-percent", "Low percent",
384 "Low threshold for buffering to start. Only used if use-buffering is True "
385 "(Deprecated: use low-watermark instead)",
386 0, 100, DEFAULT_LOW_WATERMARK * 100,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
389 g_param_spec_int ("high-percent", "High percent",
390 "High threshold for buffering to finish. Only used if use-buffering is True "
391 "(Deprecated: use high-watermark instead)",
392 0, 100, DEFAULT_HIGH_WATERMARK * 100,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
395 g_param_spec_double ("low-watermark", "Low watermark",
396 "Low threshold for buffering to start. Only used if use-buffering is True",
397 0.0, 1.0, DEFAULT_LOW_WATERMARK,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
400 g_param_spec_double ("high-watermark", "High watermark",
401 "High threshold for buffering to finish. Only used if use-buffering is True",
402 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
406 g_param_spec_string ("temp-template", "Temporary File Template",
407 "File template to store temporary files in, should contain directory "
408 "and XXXXXX. (NULL == disabled)",
409 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
412 g_param_spec_string ("temp-location", "Temporary File Location",
413 "Location to store temporary files in (Only read this property, "
414 "use temp-template to configure the name template)",
415 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
418 * GstQueue2:temp-remove
420 * When temp-template is set, remove the temporary file when going to READY.
422 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
423 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
424 "Remove the temp-location after use",
425 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428 * GstQueue2:ring-buffer-max-size
430 * The maximum size of the ring buffer in bytes. If set to 0, the ring
431 * buffer is disabled. Default 0.
433 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
434 g_param_spec_uint64 ("ring-buffer-max-size",
435 "Max. ring buffer size (bytes)",
436 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
437 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
438 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 * GstQueue2:avg-in-rate
443 * The average input data rate.
445 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
446 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
447 "Average input data rate (bytes/s)",
448 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
450 /* set several parent class virtual functions */
451 gobject_class->finalize = gst_queue2_finalize;
453 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
454 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
456 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
459 "Erik Walthinsen <omega@cse.ogi.edu>, "
460 "Wim Taymans <wim.taymans@gmail.com>");
462 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
463 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
467 gst_queue2_init (GstQueue2 * queue)
469 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
471 gst_pad_set_chain_function (queue->sinkpad,
472 GST_DEBUG_FUNCPTR (gst_queue2_chain));
473 gst_pad_set_chain_list_function (queue->sinkpad,
474 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
475 gst_pad_set_activatemode_function (queue->sinkpad,
476 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
477 gst_pad_set_event_full_function (queue->sinkpad,
478 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
479 gst_pad_set_query_function (queue->sinkpad,
480 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
481 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
482 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
484 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
486 gst_pad_set_activatemode_function (queue->srcpad,
487 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
488 gst_pad_set_getrange_function (queue->srcpad,
489 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
490 gst_pad_set_event_function (queue->srcpad,
491 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
492 gst_pad_set_query_function (queue->srcpad,
493 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
494 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
495 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
498 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
499 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
500 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
501 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
502 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
503 queue->use_buffering = DEFAULT_USE_BUFFERING;
504 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
505 queue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
506 queue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
508 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
509 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
511 queue->sinktime = GST_CLOCK_TIME_NONE;
512 queue->srctime = GST_CLOCK_TIME_NONE;
513 queue->sink_tainted = TRUE;
514 queue->src_tainted = TRUE;
516 queue->srcresult = GST_FLOW_FLUSHING;
517 queue->sinkresult = GST_FLOW_FLUSHING;
518 queue->is_eos = FALSE;
519 queue->in_timer = g_timer_new ();
520 queue->out_timer = g_timer_new ();
522 g_mutex_init (&queue->qlock);
523 queue->waiting_add = FALSE;
524 g_cond_init (&queue->item_add);
525 queue->waiting_del = FALSE;
526 g_cond_init (&queue->item_del);
527 queue->queue = gst_queue_array_new_for_struct (sizeof (GstQueue2Item), 32);
529 g_cond_init (&queue->query_handled);
530 queue->last_query = FALSE;
532 g_mutex_init (&queue->buffering_post_lock);
533 queue->buffering_percent = 100;
535 /* tempfile related */
536 queue->temp_template = NULL;
537 queue->temp_location = NULL;
538 queue->temp_remove = DEFAULT_TEMP_REMOVE;
540 queue->ring_buffer = NULL;
541 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
543 GST_DEBUG_OBJECT (queue,
544 "initialized queue's not_empty & not_full conditions");
547 /* called only once, as opposed to dispose */
549 gst_queue2_finalize (GObject * object)
551 GstQueue2 *queue = GST_QUEUE2 (object);
552 GstQueue2Item *qitem;
554 GST_DEBUG_OBJECT (queue, "finalizing queue");
556 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
557 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
558 gst_mini_object_unref (qitem->item);
560 gst_queue_array_free (queue->queue);
562 queue->last_query = FALSE;
563 g_mutex_clear (&queue->qlock);
564 g_mutex_clear (&queue->buffering_post_lock);
565 g_cond_clear (&queue->item_add);
566 g_cond_clear (&queue->item_del);
567 g_cond_clear (&queue->query_handled);
568 g_timer_destroy (queue->in_timer);
569 g_timer_destroy (queue->out_timer);
571 /* temp_file path cleanup */
572 g_free (queue->temp_template);
573 g_free (queue->temp_location);
575 G_OBJECT_CLASS (parent_class)->finalize (object);
579 debug_ranges (GstQueue2 * queue)
581 GstQueue2Range *walk;
583 for (walk = queue->ranges; walk; walk = walk->next) {
584 GST_DEBUG_OBJECT (queue,
585 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
586 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
587 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
588 walk->rb_writing_pos, walk->reading_pos,
589 walk == queue->current ? "**y**" : " n ");
593 /* clear all the downloaded ranges */
595 clean_ranges (GstQueue2 * queue)
597 GST_DEBUG_OBJECT (queue, "clean queue ranges");
599 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
600 queue->ranges = NULL;
601 queue->current = NULL;
604 /* find a range that contains @offset or NULL when nothing does */
605 static GstQueue2Range *
606 find_range (GstQueue2 * queue, guint64 offset)
608 GstQueue2Range *range = NULL;
609 GstQueue2Range *walk;
611 /* first do a quick check for the current range */
612 for (walk = queue->ranges; walk; walk = walk->next) {
613 if (offset >= walk->offset && offset <= walk->writing_pos) {
614 /* we can reuse an existing range */
620 GST_DEBUG_OBJECT (queue,
621 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
622 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
624 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
630 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
632 guint64 max_reading_pos, writing_pos;
634 writing_pos = range->writing_pos;
635 max_reading_pos = range->max_reading_pos;
637 if (writing_pos > max_reading_pos)
638 queue->cur_level.bytes = writing_pos - max_reading_pos;
640 queue->cur_level.bytes = 0;
643 /* make a new range for @offset or reuse an existing range */
644 static GstQueue2Range *
645 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
647 GstQueue2Range *range, *prev, *next;
649 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
651 if ((range = find_range (queue, offset))) {
652 GST_DEBUG_OBJECT (queue,
653 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
655 if (update_existing && range->writing_pos != offset) {
656 GST_DEBUG_OBJECT (queue, "updating range writing position to "
657 "%" G_GUINT64_FORMAT, offset);
658 range->writing_pos = offset;
661 GST_DEBUG_OBJECT (queue,
662 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
664 range = g_slice_new0 (GstQueue2Range);
665 range->offset = offset;
666 /* we want to write to the next location in the ring buffer */
667 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
668 range->writing_pos = offset;
669 range->rb_writing_pos = range->rb_offset;
670 range->reading_pos = offset;
671 range->max_reading_pos = offset;
675 next = queue->ranges;
677 if (next->offset > offset) {
678 /* insert before next */
679 GST_DEBUG_OBJECT (queue,
680 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
692 queue->ranges = range;
694 debug_ranges (queue);
696 /* update the stats for this range */
697 update_cur_level (queue, range);
703 /* clear and init the download ranges for offset 0 */
705 init_ranges (GstQueue2 * queue)
707 GST_DEBUG_OBJECT (queue, "init queue ranges");
709 /* get rid of all the current ranges */
710 clean_ranges (queue);
711 /* make a range for offset 0 */
712 queue->current = add_range (queue, 0, TRUE);
715 /* calculate the diff between running time on the sink and src of the queue.
716 * This is the total amount of time in the queue. */
718 update_time_level (GstQueue2 * queue)
720 if (queue->sink_tainted) {
722 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
723 queue->sink_segment.position);
724 queue->sink_tainted = FALSE;
727 if (queue->src_tainted) {
729 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
730 queue->src_segment.position);
731 queue->src_tainted = FALSE;
734 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
735 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
737 if (queue->sinktime != GST_CLOCK_TIME_NONE
738 && queue->srctime != GST_CLOCK_TIME_NONE
739 && queue->sinktime >= queue->srctime)
740 queue->cur_level.time = queue->sinktime - queue->srctime;
742 queue->cur_level.time = 0;
745 /* take a SEGMENT event and apply the values to segment, updating the time
748 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
751 gst_event_copy_segment (event, segment);
753 if (segment->format == GST_FORMAT_BYTES) {
754 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
755 /* start is where we'll be getting from and as such writing next */
756 queue->current = add_range (queue, segment->start, TRUE);
760 /* now configure the values, we use these to track timestamps on the
762 if (segment->format != GST_FORMAT_TIME) {
763 /* non-time format, pretend the current time segment is closed with a
764 * 0 start and unknown stop time. */
765 segment->format = GST_FORMAT_TIME;
771 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
774 queue->sink_tainted = TRUE;
776 queue->src_tainted = TRUE;
778 /* segment can update the time level of the queue */
779 update_time_level (queue);
783 apply_gap (GstQueue2 * queue, GstEvent * event,
784 GstSegment * segment, gboolean is_sink)
786 GstClockTime timestamp;
787 GstClockTime duration;
789 gst_event_parse_gap (event, ×tamp, &duration);
791 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
793 if (GST_CLOCK_TIME_IS_VALID (duration)) {
794 timestamp += duration;
797 segment->position = timestamp;
800 queue->sink_tainted = TRUE;
802 queue->src_tainted = TRUE;
804 /* calc diff with other end */
805 update_time_level (queue);
809 /* take a buffer and update segment, updating the time level of the queue. */
811 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
812 guint64 size, gboolean is_sink)
814 GstClockTime duration, timestamp;
816 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
817 duration = GST_BUFFER_DURATION (buffer);
819 /* If we have no duration, pick one from the bitrate if we can */
820 if (duration == GST_CLOCK_TIME_NONE && queue->use_tags_bitrate) {
822 is_sink ? queue->sink_tags_bitrate : queue->src_tags_bitrate;
824 duration = gst_util_uint64_scale (size, 8 * GST_SECOND, bitrate);
827 /* if no timestamp is set, assume it's continuous with the previous
829 if (timestamp == GST_CLOCK_TIME_NONE)
830 timestamp = segment->position;
833 if (duration != GST_CLOCK_TIME_NONE)
834 timestamp += duration;
836 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
837 GST_TIME_ARGS (timestamp));
839 segment->position = timestamp;
842 queue->sink_tainted = TRUE;
844 queue->src_tainted = TRUE;
846 /* calc diff with other end */
847 update_time_level (queue);
852 GstClockTime timestamp;
857 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
859 struct BufListData *bld = data;
860 GstClockTime *timestamp = &bld->timestamp;
863 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
864 " duration %" GST_TIME_FORMAT, idx,
865 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
866 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
867 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
869 btime = GST_BUFFER_DTS_OR_PTS (*buf);
870 if (GST_CLOCK_TIME_IS_VALID (btime))
873 if (GST_BUFFER_DURATION_IS_VALID (*buf))
874 *timestamp += GST_BUFFER_DURATION (*buf);
875 else if (bld->bitrate != 0) {
876 guint64 size = gst_buffer_get_size (*buf);
878 /* If we have no duration, pick one from the bitrate if we can */
879 *timestamp += gst_util_uint64_scale (bld->bitrate, 8 * GST_SECOND, size);
883 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
887 /* take a buffer list and update segment, updating the time level of the queue */
889 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
890 GstSegment * segment, gboolean is_sink)
892 struct BufListData bld;
894 /* if no timestamp is set, assume it's continuous with the previous time */
895 bld.timestamp = segment->position;
897 if (queue->use_tags_bitrate) {
899 bld.bitrate = queue->sink_tags_bitrate;
901 bld.bitrate = queue->src_tags_bitrate;
905 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &bld);
907 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
908 GST_TIME_ARGS (bld.timestamp));
910 segment->position = bld.timestamp;
913 queue->sink_tainted = TRUE;
915 queue->src_tainted = TRUE;
917 /* calc diff with other end */
918 update_time_level (queue);
922 normalize_to_buffering_level (guint64 cur_level, guint64 max_level,
931 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL,
932 MIN (max_level, alt_max));
934 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL, max_level);
936 return MIN (p, MAX_BUFFERING_LEVEL);
940 get_buffering_level (GstQueue2 * queue, gboolean * is_buffering,
941 gint * buffering_level)
943 gint buflevel, buflevel2;
945 if (queue->high_watermark <= 0) {
947 *buffering_level = MAX_BUFFERING_LEVEL;
949 *is_buffering = FALSE;
952 #define GET_BUFFER_LEVEL_FOR_QUANTITY(format,alt_max) \
953 normalize_to_buffering_level (queue->cur_level.format,queue->max_level.format,(alt_max))
955 if (queue->is_eos || queue->srcresult == GST_FLOW_NOT_LINKED) {
956 /* on EOS and NOT_LINKED we are always 100% full, we set the var
957 * here so that we can reuse the logic below to stop buffering */
958 buflevel = MAX_BUFFERING_LEVEL;
959 GST_LOG_OBJECT (queue, "we are %s", queue->is_eos ? "EOS" : "NOT_LINKED");
961 GST_LOG_OBJECT (queue,
962 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
963 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
964 queue->cur_level.buffers);
966 /* figure out the buffering level we are filled, we take the max of all formats. */
967 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
968 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, 0);
970 guint64 rb_size = queue->ring_buffer_max_size;
971 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, rb_size);
974 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (time, 0);
975 buflevel = MAX (buflevel, buflevel2);
977 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (buffers, 0);
978 buflevel = MAX (buflevel, buflevel2);
980 /* also apply the rate estimate when we need to */
981 if (queue->use_rate_estimate) {
982 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (rate_time, 0);
983 buflevel = MAX (buflevel, buflevel2);
986 /* Don't get to 0% unless we're really empty */
987 if (queue->cur_level.bytes > 0)
988 buflevel = MAX (1, buflevel);
990 #undef GET_BUFFER_LEVEL_FOR_QUANTITY
993 *is_buffering = queue->is_buffering;
996 *buffering_level = buflevel;
998 GST_DEBUG_OBJECT (queue, "buffering %d, level %d", queue->is_buffering,
1005 convert_to_buffering_percent (GstQueue2 * queue, gint buffering_level)
1009 /* scale so that if buffering_level equals the high watermark,
1010 * the percentage is 100% */
1011 percent = buffering_level * 100 / queue->high_watermark;
1020 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
1021 gint * avg_in, gint * avg_out, gint64 * buffering_left)
1024 if (!QUEUE_IS_USING_QUEUE (queue)) {
1025 if (QUEUE_IS_USING_RING_BUFFER (queue))
1026 *mode = GST_BUFFERING_TIMESHIFT;
1028 *mode = GST_BUFFERING_DOWNLOAD;
1030 *mode = GST_BUFFERING_STREAM;
1035 *avg_in = queue->byte_in_rate;
1037 *avg_out = queue->byte_out_rate;
1039 if (buffering_left) {
1040 *buffering_left = (percent == 100 ? 0 : -1);
1042 if (queue->use_rate_estimate) {
1045 max = queue->max_level.rate_time;
1046 cur = queue->cur_level.rate_time;
1048 if (percent != 100 && max > cur)
1049 *buffering_left = (max - cur) / 1000000;
1054 /* Called with the lock taken */
1056 gst_queue2_get_buffering_message (GstQueue2 * queue)
1058 GstMessage *msg = NULL;
1060 if (queue->percent_changed) {
1061 gint percent = queue->buffering_percent;
1063 queue->percent_changed = FALSE;
1065 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
1066 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
1068 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
1069 queue->avg_out, queue->buffering_left);
1076 gst_queue2_post_buffering (GstQueue2 * queue)
1078 GstMessage *msg = NULL;
1080 g_mutex_lock (&queue->buffering_post_lock);
1081 GST_QUEUE2_MUTEX_LOCK (queue);
1082 msg = gst_queue2_get_buffering_message (queue);
1083 GST_QUEUE2_MUTEX_UNLOCK (queue);
1086 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1088 g_mutex_unlock (&queue->buffering_post_lock);
1092 update_buffering (GstQueue2 * queue)
1094 gint buffering_level, percent;
1096 /* Ensure the variables used to calculate buffering state are up-to-date. */
1098 update_cur_level (queue, queue->current);
1099 update_in_rates (queue, FALSE);
1101 if (!get_buffering_level (queue, NULL, &buffering_level))
1104 percent = convert_to_buffering_percent (queue, buffering_level);
1106 if (queue->is_buffering) {
1107 /* if we were buffering see if we reached the high watermark */
1109 queue->is_buffering = FALSE;
1111 SET_PERCENT (queue, percent);
1113 /* we were not buffering, check if we need to start buffering if we drop
1114 * below the low threshold */
1115 if (buffering_level < queue->low_watermark) {
1116 queue->is_buffering = TRUE;
1117 SET_PERCENT (queue, percent);
1123 reset_rate_timer (GstQueue2 * queue)
1125 queue->bytes_in = 0;
1126 queue->bytes_out = 0;
1127 queue->byte_in_rate = 0.0;
1128 queue->byte_in_period = 0;
1129 queue->byte_out_rate = 0.0;
1130 queue->last_update_in_rates_elapsed = 0.0;
1131 queue->last_in_elapsed = 0.0;
1132 queue->last_out_elapsed = 0.0;
1133 queue->in_timer_started = FALSE;
1134 queue->out_timer_started = FALSE;
1137 /* the interval in seconds to recalculate the rate */
1138 #define RATE_INTERVAL 0.2
1139 /* Tuning for rate estimation. We use a large window for the input rate because
1140 * it should be stable when connected to a network. The output rate is less
1141 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1142 * therefore adapt more quickly.
1143 * However, initial input rate may be subject to a burst, and should therefore
1144 * initially also adapt more quickly to changes, and only later on give higher
1145 * weight to previous values. */
1146 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1147 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1150 update_in_rates (GstQueue2 * queue, gboolean force)
1152 gdouble elapsed, period;
1153 gdouble byte_in_rate;
1155 if (!queue->in_timer_started) {
1156 queue->in_timer_started = TRUE;
1157 g_timer_start (queue->in_timer);
1161 queue->last_update_in_rates_elapsed = elapsed =
1162 g_timer_elapsed (queue->in_timer, NULL);
1164 /* recalc after each interval. */
1165 if (force || queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1166 period = elapsed - queue->last_in_elapsed;
1168 GST_DEBUG_OBJECT (queue,
1169 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1170 period, queue->bytes_in, queue->byte_in_period);
1172 byte_in_rate = queue->bytes_in / period;
1174 if (queue->byte_in_rate == 0.0)
1175 queue->byte_in_rate = byte_in_rate;
1177 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1178 (double) queue->byte_in_period, period);
1180 /* another data point, cap at 16 for long time running average */
1181 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1182 queue->byte_in_period += period;
1184 /* reset the values to calculate rate over the next interval */
1185 queue->last_in_elapsed = elapsed;
1186 queue->bytes_in = 0;
1189 if (queue->byte_in_rate > 0.0) {
1190 queue->cur_level.rate_time =
1191 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1193 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1194 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1198 update_out_rates (GstQueue2 * queue)
1200 gdouble elapsed, period;
1201 gdouble byte_out_rate;
1203 if (!queue->out_timer_started) {
1204 queue->out_timer_started = TRUE;
1205 g_timer_start (queue->out_timer);
1209 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1211 /* recalc after each interval. */
1212 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1213 period = elapsed - queue->last_out_elapsed;
1215 GST_DEBUG_OBJECT (queue,
1216 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1218 byte_out_rate = queue->bytes_out / period;
1220 if (queue->byte_out_rate == 0.0)
1221 queue->byte_out_rate = byte_out_rate;
1223 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1225 /* reset the values to calculate rate over the next interval */
1226 queue->last_out_elapsed = elapsed;
1227 queue->bytes_out = 0;
1229 if (queue->byte_in_rate > 0.0) {
1230 queue->cur_level.rate_time =
1231 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1233 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1234 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1238 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1240 guint64 reading_pos, max_reading_pos;
1243 max_reading_pos = range->max_reading_pos;
1245 max_reading_pos = MAX (max_reading_pos, reading_pos);
1247 GST_DEBUG_OBJECT (queue,
1248 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1249 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1250 range->max_reading_pos = max_reading_pos;
1252 update_cur_level (queue, range);
1256 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1261 /* until we receive the FLUSH_STOP from this seek, we skip data */
1262 queue->seeking = TRUE;
1263 GST_QUEUE2_MUTEX_UNLOCK (queue);
1265 debug_ranges (queue);
1267 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1270 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1271 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1272 GST_SEEK_TYPE_NONE, -1);
1274 res = gst_pad_push_event (queue->sinkpad, event);
1275 GST_QUEUE2_MUTEX_LOCK (queue);
1278 /* Between us sending the seek event and re-acquiring the lock, the source
1279 * thread might already have pushed data and moved along the range's
1280 * writing_pos beyond the seek offset. In that case we don't want to set
1281 * the writing position back to the requested seek position, as it would
1282 * cause data to be written to the wrong offset in the file or ring buffer.
1283 * We still do the add_range call to switch the current range to the
1284 * requested range, or create one if one doesn't exist yet. */
1285 queue->current = add_range (queue, offset, FALSE);
1291 /* get the threshold for when we decide to seek rather than wait */
1293 get_seek_threshold (GstQueue2 * queue)
1297 /* FIXME, find a good threshold based on the incoming rate. */
1298 threshold = 1024 * 512;
1300 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1301 threshold = MIN (threshold,
1302 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1307 /* see if there is enough data in the file to read a full buffer */
1309 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1311 GstQueue2Range *range;
1313 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1316 if ((range = find_range (queue, offset))) {
1317 if (queue->current != range) {
1318 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1319 perform_seek_to_offset (queue, range->writing_pos);
1322 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1323 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1325 /* we have a range for offset */
1326 GST_DEBUG_OBJECT (queue,
1327 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1328 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1330 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1333 if (offset + length <= range->writing_pos)
1336 GST_DEBUG_OBJECT (queue,
1337 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1338 (offset + length) - range->writing_pos);
1341 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1342 " len %u", offset, length);
1343 /* we don't have the range, see how far away we are */
1344 if (!queue->is_eos && queue->current) {
1345 guint64 threshold = get_seek_threshold (queue);
1347 if (offset >= queue->current->offset && offset <=
1348 queue->current->writing_pos + threshold) {
1349 GST_INFO_OBJECT (queue,
1350 "requested data is within range, wait for data");
1355 /* too far away, do a seek */
1356 perform_seek_to_offset (queue, offset);
1363 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1364 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1365 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1367 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1370 static GstFlowReturn
1371 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1372 guint8 * dst, gint64 * read_return)
1374 guint8 *ring_buffer;
1377 ring_buffer = queue->ring_buffer;
1379 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1382 /* this should not block */
1383 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1385 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1386 res = fread (dst, 1, length, queue->temp_file);
1388 memcpy (dst, ring_buffer + offset, length);
1392 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1394 if (G_UNLIKELY (res < length)) {
1395 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1396 goto could_not_read;
1397 /* check for errors or EOF */
1398 if (ferror (queue->temp_file))
1399 goto could_not_read;
1400 if (feof (queue->temp_file) && length > 0)
1410 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1411 return GST_FLOW_ERROR;
1415 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1416 return GST_FLOW_ERROR;
1420 GST_DEBUG ("non-regular file hits EOS");
1421 return GST_FLOW_EOS;
1425 static GstFlowReturn
1426 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1427 GstBuffer ** buffer)
1432 guint64 file_offset;
1433 guint block_length, remaining, read_length;
1437 GstFlowReturn ret = GST_FLOW_OK;
1439 /* allocate the output buffer of the requested size */
1440 if (*buffer == NULL)
1441 buf = gst_buffer_new_allocate (NULL, length, NULL);
1445 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
1446 goto buffer_write_fail;
1449 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1453 rb_size = queue->ring_buffer_max_size;
1454 max_size = QUEUE_MAX_BYTES (queue);
1457 while (remaining > 0) {
1458 /* configure how much/whether to read */
1459 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1462 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1465 /* calculate how far away the offset is */
1466 if (queue->current->writing_pos > rpos)
1467 level = queue->current->writing_pos - rpos;
1471 GST_DEBUG_OBJECT (queue,
1472 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1473 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1474 rpos, queue->current->writing_pos, level, max_size);
1476 if (level >= max_size) {
1477 /* we don't have the data but if we have a ring buffer that is full, we
1479 GST_DEBUG_OBJECT (queue,
1480 "ring buffer full, reading QUEUE_MAX_BYTES %"
1481 G_GUINT64_FORMAT " bytes", max_size);
1482 read_length = max_size;
1483 } else if (queue->is_eos) {
1484 /* won't get any more data so read any data we have */
1486 GST_DEBUG_OBJECT (queue,
1487 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1489 read_length = level;
1497 if (read_length == 0) {
1498 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1499 GST_DEBUG_OBJECT (queue,
1500 "update current position [%" G_GUINT64_FORMAT "-%"
1501 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1502 update_cur_pos (queue, queue->current, rpos);
1503 GST_QUEUE2_SIGNAL_DEL (queue);
1506 if (queue->use_buffering)
1507 update_buffering (queue);
1509 GST_DEBUG_OBJECT (queue, "waiting for add");
1510 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1514 /* we have the requested data so read it */
1515 read_length = remaining;
1518 /* set range reading_pos to actual reading position for this read */
1519 queue->current->reading_pos = rpos;
1521 /* configure how much and from where to read */
1522 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1524 (queue->current->rb_offset + (rpos -
1525 queue->current->offset)) % rb_size;
1526 if (file_offset + read_length > rb_size) {
1527 block_length = rb_size - file_offset;
1529 block_length = read_length;
1533 block_length = read_length;
1536 /* while we still have data to read, we loop */
1537 while (read_length > 0) {
1541 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1542 data, &read_return);
1543 if (ret != GST_FLOW_OK)
1546 file_offset += read_return;
1547 if (QUEUE_IS_USING_RING_BUFFER (queue))
1548 file_offset %= rb_size;
1550 data += read_return;
1551 read_length -= read_return;
1552 block_length = read_length;
1553 remaining -= read_return;
1555 rpos = (queue->current->reading_pos += read_return);
1556 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1558 GST_QUEUE2_SIGNAL_DEL (queue);
1559 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1562 gst_buffer_unmap (buf, &info);
1563 gst_buffer_resize (buf, 0, length);
1565 GST_BUFFER_OFFSET (buf) = offset;
1566 GST_BUFFER_OFFSET_END (buf) = offset + length;
1575 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1576 gst_buffer_unmap (buf, &info);
1577 if (*buffer == NULL)
1578 gst_buffer_unref (buf);
1579 return GST_FLOW_EOS;
1583 GST_DEBUG_OBJECT (queue, "we are flushing");
1584 gst_buffer_unmap (buf, &info);
1585 if (*buffer == NULL)
1586 gst_buffer_unref (buf);
1587 return GST_FLOW_FLUSHING;
1591 GST_DEBUG_OBJECT (queue, "we have a read error");
1592 gst_buffer_unmap (buf, &info);
1593 if (*buffer == NULL)
1594 gst_buffer_unref (buf);
1599 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE, (NULL),
1600 ("Can't write to buffer"));
1601 if (*buffer == NULL)
1602 gst_buffer_unref (buf);
1603 return GST_FLOW_ERROR;
1607 /* should be called with QUEUE_LOCK */
1608 static GstMiniObject *
1609 gst_queue2_read_item_from_file (GstQueue2 * queue)
1611 GstMiniObject *item;
1613 if (queue->stream_start_event != NULL) {
1614 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1615 queue->stream_start_event = NULL;
1616 } else if (queue->starting_segment != NULL) {
1617 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1618 queue->starting_segment = NULL;
1621 GstBuffer *buffer = NULL;
1622 guint64 reading_pos;
1624 reading_pos = queue->current->reading_pos;
1627 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1632 item = GST_MINI_OBJECT_CAST (buffer);
1635 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1645 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1646 * the temp filename. */
1648 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1653 if (queue->temp_file)
1654 goto already_opened;
1656 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1658 /* If temp_template was set, allocate a filename and open that file */
1661 if (queue->temp_template == NULL)
1664 /* make copy of the template, we don't want to change this */
1665 name = g_strdup (queue->temp_template);
1668 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1670 fd = g_mkstemp (name);
1674 goto mkstemp_failed;
1676 /* open the file for update/writing */
1677 queue->temp_file = fdopen (fd, "wb+");
1678 /* error creating file */
1679 if (queue->temp_file == NULL)
1682 g_free (queue->temp_location);
1683 queue->temp_location = name;
1685 GST_QUEUE2_MUTEX_UNLOCK (queue);
1687 /* we can't emit the notify with the lock */
1688 g_object_notify (G_OBJECT (queue), "temp-location");
1690 GST_QUEUE2_MUTEX_LOCK (queue);
1692 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1699 GST_DEBUG_OBJECT (queue, "temp file was already open");
1704 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1705 (_("No Temp directory specified.")), (NULL));
1710 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1711 (_("Could not create temp file \"%s\"."), queue->temp_template),
1718 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1719 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1728 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1731 if (queue->temp_file == NULL)
1734 GST_DEBUG_OBJECT (queue, "closing temp file");
1736 fflush (queue->temp_file);
1737 fclose (queue->temp_file);
1739 if (queue->temp_remove) {
1740 if (remove (queue->temp_location) < 0) {
1741 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1742 queue->temp_location, g_strerror (errno));
1746 queue->temp_file = NULL;
1747 clean_ranges (queue);
1751 gst_queue2_flush_temp_file (GstQueue2 * queue)
1753 if (queue->temp_file == NULL)
1756 GST_DEBUG_OBJECT (queue, "flushing temp file");
1758 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1762 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1764 if (!QUEUE_IS_USING_QUEUE (queue)) {
1765 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1766 gst_queue2_flush_temp_file (queue);
1767 init_ranges (queue);
1769 GstQueue2Item *qitem;
1771 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
1772 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1773 && GST_EVENT_IS_STICKY (qitem->item)
1774 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1775 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1776 gst_pad_store_sticky_event (queue->srcpad,
1777 GST_EVENT_CAST (qitem->item));
1780 /* Then lose another reference because we are supposed to destroy that
1781 data when flushing */
1782 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1783 gst_mini_object_unref (qitem->item);
1786 queue->last_query = FALSE;
1787 g_cond_signal (&queue->query_handled);
1788 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1789 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1790 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1791 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1792 queue->sink_tainted = queue->src_tainted = TRUE;
1793 if (queue->starting_segment != NULL)
1794 gst_event_unref (queue->starting_segment);
1795 queue->starting_segment = NULL;
1796 queue->segment_event_received = FALSE;
1797 gst_event_replace (&queue->stream_start_event, NULL);
1799 /* we deleted a lot of something */
1800 GST_QUEUE2_SIGNAL_DEL (queue);
1804 gst_queue2_wait_free_space (GstQueue2 * queue)
1806 /* We make space available if we're "full" according to whatever
1807 * the user defined as "full". */
1808 if (gst_queue2_is_filled (queue)) {
1811 /* pause the timer while we wait. The fact that we are waiting does not mean
1812 * the byterate on the input pad is lower */
1813 if ((started = queue->in_timer_started))
1814 g_timer_stop (queue->in_timer);
1816 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1817 "queue is full, waiting for free space");
1819 /* Wait for space to be available, we could be unlocked because of a flush. */
1820 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1822 while (gst_queue2_is_filled (queue));
1824 /* and continue if we were running before */
1826 g_timer_continue (queue->in_timer);
1833 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1839 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1842 guint8 *data, *ring_buffer;
1843 guint size, rb_size;
1844 guint64 writing_pos, new_writing_pos;
1845 GstQueue2Range *range, *prev, *next;
1846 gboolean do_seek = FALSE;
1848 if (QUEUE_IS_USING_RING_BUFFER (queue))
1849 writing_pos = queue->current->rb_writing_pos;
1851 writing_pos = queue->current->writing_pos;
1852 ring_buffer = queue->ring_buffer;
1853 rb_size = queue->ring_buffer_max_size;
1855 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
1856 goto buffer_read_error;
1861 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1865 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1866 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1867 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1868 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1869 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1875 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1878 /* calculate the space in the ring buffer not used by data from
1879 * the current range */
1880 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1881 /* wait until there is some free space */
1882 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1884 /* get the amount of space we have */
1885 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1887 /* calculate if we need to split or if we can write the entire
1889 to_write = MIN (size, space);
1891 /* the writing position in the ring buffer after writing (part
1892 * or all of) the buffer */
1893 new_writing_pos = (writing_pos + to_write) % rb_size;
1896 range = queue->ranges;
1898 /* if we need to overwrite data in the ring buffer, we need to
1901 * warning: this code is complicated and includes some
1902 * simplifications - pen, paper and diagrams for the cases
1905 guint64 range_data_start, range_data_end;
1906 GstQueue2Range *range_to_destroy = NULL;
1908 if (range == queue->current)
1911 range_data_start = range->rb_offset;
1912 range_data_end = range->rb_writing_pos;
1914 /* handle the special case where the range has no data in it */
1915 if (range->writing_pos == range->offset) {
1916 if (range != queue->current) {
1917 GST_DEBUG_OBJECT (queue,
1918 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1919 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1921 range_to_destroy = range;
1923 prev->next = range->next;
1928 if (range_data_end > range_data_start) {
1929 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1932 if (new_writing_pos > range_data_start) {
1933 if (new_writing_pos >= range_data_end) {
1934 GST_DEBUG_OBJECT (queue,
1935 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1936 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1938 range_to_destroy = range;
1940 prev->next = range->next;
1942 GST_DEBUG_OBJECT (queue,
1943 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1944 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1945 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1946 range->offset + new_writing_pos - range_data_start,
1948 range->offset += (new_writing_pos - range_data_start);
1949 range->rb_offset = new_writing_pos;
1953 guint64 new_wpos_virt = writing_pos + to_write;
1955 if (new_wpos_virt <= range_data_start)
1958 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1959 GST_DEBUG_OBJECT (queue,
1960 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1961 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1963 range_to_destroy = range;
1965 prev->next = range->next;
1967 GST_DEBUG_OBJECT (queue,
1968 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1969 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1970 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1971 range->offset + new_writing_pos - range_data_start,
1973 range->offset += (new_wpos_virt - range_data_start);
1974 range->rb_offset = new_writing_pos;
1979 if (!range_to_destroy)
1982 range = range->next;
1983 if (range_to_destroy) {
1984 if (range_to_destroy == queue->ranges)
1985 queue->ranges = range;
1986 g_slice_free (GstQueue2Range, range_to_destroy);
1987 range_to_destroy = NULL;
1992 new_writing_pos = writing_pos + to_write;
1995 if (QUEUE_IS_USING_TEMP_FILE (queue)
1996 && FSEEK_FILE (queue->temp_file, writing_pos))
1999 if (new_writing_pos > writing_pos) {
2000 GST_INFO_OBJECT (queue,
2001 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2002 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
2003 queue->current->writing_pos, queue->current->rb_writing_pos);
2004 /* either not using ring buffer or no wrapping, just write */
2005 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2006 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
2009 memcpy (ring_buffer + writing_pos, data, to_write);
2012 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
2013 /* try to merge with next range */
2014 while ((next = queue->current->next)) {
2015 GST_INFO_OBJECT (queue,
2016 "checking merge with next range %" G_GUINT64_FORMAT " < %"
2017 G_GUINT64_FORMAT, new_writing_pos, next->offset);
2018 if (new_writing_pos < next->offset)
2021 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
2024 /* remove the group */
2025 queue->current->next = next->next;
2027 /* We use the threshold to decide if we want to do a seek or simply
2028 * read the data again. If there is not so much data in the range we
2029 * prefer to avoid to seek and read it again. */
2030 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
2031 /* the new range had more data than the threshold, it's worth keeping
2032 * it and doing a seek. */
2033 new_writing_pos = next->writing_pos;
2036 g_slice_free (GstQueue2Range, next);
2038 goto update_and_signal;
2042 guint block_one, block_two;
2044 block_one = rb_size - writing_pos;
2045 block_two = to_write - block_one;
2047 if (block_one > 0) {
2048 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
2049 /* write data to end of ring buffer */
2050 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2051 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
2054 memcpy (ring_buffer + writing_pos, data, block_one);
2058 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
2061 if (block_two > 0) {
2062 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
2063 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2064 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
2067 memcpy (ring_buffer, data + block_one, block_two);
2073 /* update the writing positions */
2075 GST_INFO_OBJECT (queue,
2076 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2077 to_write, writing_pos, size);
2079 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2081 queue->current->writing_pos += to_write;
2082 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2084 queue->current->writing_pos = writing_pos = new_writing_pos;
2087 perform_seek_to_offset (queue, new_writing_pos);
2089 update_cur_level (queue, queue->current);
2091 /* update the buffering status */
2092 if (queue->use_buffering) {
2094 update_buffering (queue);
2095 msg = gst_queue2_get_buffering_message (queue);
2097 GST_QUEUE2_MUTEX_UNLOCK (queue);
2098 g_mutex_lock (&queue->buffering_post_lock);
2099 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
2100 g_mutex_unlock (&queue->buffering_post_lock);
2101 GST_QUEUE2_MUTEX_LOCK (queue);
2105 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2106 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2108 GST_QUEUE2_SIGNAL_ADD (queue);
2111 gst_buffer_unmap (buffer, &info);
2118 GST_DEBUG_OBJECT (queue, "we are flushing");
2119 gst_buffer_unmap (buffer, &info);
2120 /* FIXME - GST_FLOW_EOS ? */
2125 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2126 gst_buffer_unmap (buffer, &info);
2133 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2137 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2138 (_("Error while writing to download file.")),
2139 ("%s", g_strerror (errno)));
2142 gst_buffer_unmap (buffer, &info);
2147 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL),
2148 ("Can't read from buffer"));
2154 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2156 GstQueue2 *queue = q;
2158 GST_TRACE_OBJECT (queue,
2159 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2160 gst_buffer_get_size (*buf));
2162 if (!gst_queue2_create_write (queue, *buf)) {
2163 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2169 /* enqueue an item an update the level stats */
2171 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2172 GstQueue2ItemType item_type)
2174 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2178 buffer = GST_BUFFER_CAST (item);
2179 size = gst_buffer_get_size (buffer);
2181 /* add buffer to the statistics */
2182 if (QUEUE_IS_USING_QUEUE (queue)) {
2183 queue->cur_level.buffers++;
2184 queue->cur_level.bytes += size;
2186 queue->bytes_in += size;
2188 /* apply new buffer to segment stats */
2189 apply_buffer (queue, buffer, &queue->sink_segment, size, TRUE);
2190 /* update the byterate stats */
2191 update_in_rates (queue, FALSE);
2193 if (!QUEUE_IS_USING_QUEUE (queue)) {
2194 /* FIXME - check return value? */
2195 gst_queue2_create_write (queue, buffer);
2197 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2198 GstBufferList *buffer_list;
2201 buffer_list = GST_BUFFER_LIST_CAST (item);
2203 size = gst_buffer_list_calculate_size (buffer_list);
2204 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2206 /* add buffer to the statistics */
2207 if (QUEUE_IS_USING_QUEUE (queue)) {
2208 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2209 queue->cur_level.bytes += size;
2211 queue->bytes_in += size;
2213 /* apply new buffer to segment stats */
2214 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2216 /* update the byterate stats */
2217 update_in_rates (queue, FALSE);
2219 if (!QUEUE_IS_USING_QUEUE (queue)) {
2220 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2222 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2225 event = GST_EVENT_CAST (item);
2227 switch (GST_EVENT_TYPE (event)) {
2229 /* Zero the thresholds, this makes sure the queue is completely
2230 * filled and we can read all data from the queue. */
2231 GST_DEBUG_OBJECT (queue, "we have EOS");
2232 queue->is_eos = TRUE;
2233 /* Force updating the input bitrate */
2234 update_in_rates (queue, TRUE);
2236 case GST_EVENT_SEGMENT:
2237 apply_segment (queue, event, &queue->sink_segment, TRUE);
2238 /* This is our first new segment, we hold it
2239 * as we can't save it on the temp file */
2240 if (!QUEUE_IS_USING_QUEUE (queue)) {
2241 if (queue->segment_event_received)
2242 goto unexpected_event;
2244 queue->segment_event_received = TRUE;
2245 if (queue->starting_segment != NULL)
2246 gst_event_unref (queue->starting_segment);
2247 queue->starting_segment = event;
2250 /* a new segment allows us to accept more buffers if we got EOS
2251 * from downstream */
2252 queue->unexpected = FALSE;
2255 apply_gap (queue, event, &queue->sink_segment, TRUE);
2257 case GST_EVENT_STREAM_START:
2258 if (!QUEUE_IS_USING_QUEUE (queue)) {
2259 gst_event_replace (&queue->stream_start_event, event);
2260 gst_event_unref (event);
2264 case GST_EVENT_CAPS:{
2267 gst_event_parse_caps (event, &caps);
2268 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2270 if (!QUEUE_IS_USING_QUEUE (queue)) {
2271 GST_LOG ("Dropping caps event, not using queue");
2272 gst_event_unref (event);
2278 if (!QUEUE_IS_USING_QUEUE (queue))
2279 goto unexpected_event;
2282 } else if (GST_IS_QUERY (item)) {
2283 /* Can't happen as we check that in the caller */
2284 if (!QUEUE_IS_USING_QUEUE (queue))
2285 g_assert_not_reached ();
2287 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2288 item, GST_OBJECT_NAME (queue));
2289 /* we can't really unref since we don't know what it is */
2294 /* update the buffering status */
2295 if (queue->use_buffering)
2296 update_buffering (queue);
2298 if (QUEUE_IS_USING_QUEUE (queue)) {
2299 GstQueue2Item qitem;
2301 qitem.type = item_type;
2303 gst_queue_array_push_tail_struct (queue->queue, &qitem);
2305 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2308 GST_QUEUE2_SIGNAL_ADD (queue);
2316 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2318 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2319 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2320 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2321 gst_event_unref (GST_EVENT_CAST (item));
2326 /* dequeue an item from the queue and update level stats */
2327 static GstMiniObject *
2328 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2330 GstMiniObject *item;
2332 if (!QUEUE_IS_USING_QUEUE (queue)) {
2333 item = gst_queue2_read_item_from_file (queue);
2335 GstQueue2Item *qitem = gst_queue_array_pop_head_struct (queue->queue);
2346 if (GST_IS_BUFFER (item)) {
2350 buffer = GST_BUFFER_CAST (item);
2351 size = gst_buffer_get_size (buffer);
2352 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2354 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2355 "retrieved buffer %p from queue", buffer);
2357 if (QUEUE_IS_USING_QUEUE (queue)) {
2358 queue->cur_level.buffers--;
2359 queue->cur_level.bytes -= size;
2361 queue->bytes_out += size;
2363 apply_buffer (queue, buffer, &queue->src_segment, size, FALSE);
2364 /* update the byterate stats */
2365 update_out_rates (queue);
2366 /* update the buffering */
2367 if (queue->use_buffering)
2368 update_buffering (queue);
2370 } else if (GST_IS_EVENT (item)) {
2371 GstEvent *event = GST_EVENT_CAST (item);
2373 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2375 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2376 "retrieved event %p from queue", event);
2378 switch (GST_EVENT_TYPE (event)) {
2380 /* queue is empty now that we dequeued the EOS */
2381 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2383 case GST_EVENT_SEGMENT:
2384 apply_segment (queue, event, &queue->src_segment, FALSE);
2387 apply_gap (queue, event, &queue->src_segment, FALSE);
2392 } else if (GST_IS_BUFFER_LIST (item)) {
2393 GstBufferList *buffer_list;
2396 buffer_list = GST_BUFFER_LIST_CAST (item);
2397 size = gst_buffer_list_calculate_size (buffer_list);
2398 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2400 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2401 "retrieved buffer list %p from queue", buffer_list);
2403 if (QUEUE_IS_USING_QUEUE (queue)) {
2404 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2405 queue->cur_level.bytes -= size;
2407 queue->bytes_out += size;
2409 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2410 /* update the byterate stats */
2411 update_out_rates (queue);
2412 /* update the buffering */
2413 if (queue->use_buffering)
2414 update_buffering (queue);
2415 } else if (GST_IS_QUERY (item)) {
2416 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2417 "retrieved query %p from queue", item);
2418 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2421 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2422 item, GST_OBJECT_NAME (queue));
2424 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2426 GST_QUEUE2_SIGNAL_DEL (queue);
2433 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2438 static GstFlowReturn
2439 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2442 gboolean ret = TRUE;
2445 queue = GST_QUEUE2 (parent);
2447 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Received event '%s'",
2448 GST_EVENT_TYPE_NAME (event));
2450 switch (GST_EVENT_TYPE (event)) {
2451 case GST_EVENT_FLUSH_START:
2453 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2455 ret = gst_pad_push_event (queue->srcpad, event);
2457 /* now unblock the chain function */
2458 GST_QUEUE2_MUTEX_LOCK (queue);
2459 queue->srcresult = GST_FLOW_FLUSHING;
2460 queue->sinkresult = GST_FLOW_FLUSHING;
2461 /* unblock the loop and chain functions */
2462 GST_QUEUE2_SIGNAL_ADD (queue);
2463 GST_QUEUE2_SIGNAL_DEL (queue);
2464 GST_QUEUE2_MUTEX_UNLOCK (queue);
2466 /* make sure it pauses, this should happen since we sent
2467 * flush_start downstream. */
2468 gst_pad_pause_task (queue->srcpad);
2469 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2471 GST_QUEUE2_MUTEX_LOCK (queue);
2472 queue->last_query = FALSE;
2473 g_cond_signal (&queue->query_handled);
2474 GST_QUEUE2_MUTEX_UNLOCK (queue);
2476 GST_QUEUE2_MUTEX_LOCK (queue);
2477 /* flush the sink pad */
2478 queue->sinkresult = GST_FLOW_FLUSHING;
2479 GST_QUEUE2_SIGNAL_DEL (queue);
2480 queue->last_query = FALSE;
2481 g_cond_signal (&queue->query_handled);
2482 GST_QUEUE2_MUTEX_UNLOCK (queue);
2484 gst_event_unref (event);
2488 case GST_EVENT_FLUSH_STOP:
2490 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2492 ret = gst_pad_push_event (queue->srcpad, event);
2494 GST_QUEUE2_MUTEX_LOCK (queue);
2495 gst_queue2_locked_flush (queue, FALSE, TRUE);
2496 queue->srcresult = GST_FLOW_OK;
2497 queue->sinkresult = GST_FLOW_OK;
2498 queue->is_eos = FALSE;
2499 queue->unexpected = FALSE;
2500 queue->seeking = FALSE;
2501 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2502 /* reset rate counters */
2503 reset_rate_timer (queue);
2504 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2505 queue->srcpad, NULL);
2506 GST_QUEUE2_MUTEX_UNLOCK (queue);
2508 GST_QUEUE2_MUTEX_LOCK (queue);
2509 queue->segment_event_received = FALSE;
2510 queue->is_eos = FALSE;
2511 queue->unexpected = FALSE;
2512 queue->sinkresult = GST_FLOW_OK;
2513 queue->seeking = FALSE;
2514 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2515 GST_QUEUE2_MUTEX_UNLOCK (queue);
2517 gst_event_unref (event);
2521 case GST_EVENT_TAG:{
2522 if (queue->use_tags_bitrate) {
2526 gst_event_parse_tag (event, &tags);
2527 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2528 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2529 GST_QUEUE2_MUTEX_LOCK (queue);
2530 queue->sink_tags_bitrate = bitrate;
2531 GST_QUEUE2_MUTEX_UNLOCK (queue);
2532 GST_LOG_OBJECT (queue, "Sink pad bitrate from tags now %u", bitrate);
2538 if (GST_EVENT_IS_SERIALIZED (event)) {
2539 /* serialized events go in the queue */
2541 /* STREAM_START and SEGMENT reset the EOS status of a
2542 * pad. Change the cached sinkpad flow result accordingly */
2543 if (queue->sinkresult == GST_FLOW_EOS
2544 && (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START
2545 || GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT))
2546 queue->sinkresult = GST_FLOW_OK;
2548 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2549 if (queue->srcresult != GST_FLOW_OK) {
2550 /* Errors in sticky event pushing are no problem and ignored here
2551 * as they will cause more meaningful errors during data flow.
2552 * For EOS events, that are not followed by data flow, we still
2553 * return FALSE here though and report an error.
2555 if (!GST_EVENT_IS_STICKY (event)) {
2556 goto out_flow_error;
2557 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2558 if (queue->srcresult == GST_FLOW_NOT_LINKED
2559 || queue->srcresult < GST_FLOW_EOS) {
2560 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
2562 goto out_flow_error;
2566 /* refuse more events on EOS unless they unset the EOS status */
2567 if (queue->is_eos) {
2568 switch (GST_EVENT_TYPE (event)) {
2569 case GST_EVENT_STREAM_START:
2570 case GST_EVENT_SEGMENT:
2571 /* Restart the loop */
2572 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2573 queue->srcresult = GST_FLOW_OK;
2574 queue->is_eos = FALSE;
2575 queue->unexpected = FALSE;
2576 queue->seeking = FALSE;
2577 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2578 /* reset rate counters */
2579 reset_rate_timer (queue);
2580 gst_pad_start_task (queue->srcpad,
2581 (GstTaskFunction) gst_queue2_loop, queue->srcpad, NULL);
2583 queue->is_eos = FALSE;
2584 queue->unexpected = FALSE;
2585 queue->seeking = FALSE;
2586 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2595 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2596 GST_QUEUE2_MUTEX_UNLOCK (queue);
2597 gst_queue2_post_buffering (queue);
2599 /* non-serialized events are passed downstream. */
2600 ret = gst_pad_push_event (queue->srcpad, event);
2605 return GST_FLOW_ERROR;
2611 GstFlowReturn ret = queue->sinkresult;
2612 GST_DEBUG_OBJECT (queue, "refusing event, we are %s",
2613 gst_flow_get_name (ret));
2614 GST_QUEUE2_MUTEX_UNLOCK (queue);
2615 gst_event_unref (event);
2620 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2621 GST_QUEUE2_MUTEX_UNLOCK (queue);
2622 gst_event_unref (event);
2623 return GST_FLOW_EOS;
2627 GST_LOG_OBJECT (queue,
2628 "refusing event, we have a downstream flow error: %s",
2629 gst_flow_get_name (queue->srcresult));
2630 GST_QUEUE2_MUTEX_UNLOCK (queue);
2631 gst_event_unref (event);
2632 return queue->srcresult;
2637 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2643 queue = GST_QUEUE2 (parent);
2645 switch (GST_QUERY_TYPE (query)) {
2647 if (GST_QUERY_IS_SERIALIZED (query)) {
2648 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2649 /* serialized events go in the queue. We need to be certain that we
2650 * don't cause deadlocks waiting for the query return value. We check if
2651 * the queue is empty (nothing is blocking downstream and the query can
2652 * be pushed for sure) or we are not buffering. If we are buffering,
2653 * the pipeline waits to unblock downstream until our queue fills up
2654 * completely, which can not happen if we block on the query..
2655 * Therefore we only potentially block when we are not buffering. */
2656 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2657 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2658 || !queue->use_buffering)) {
2659 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2660 gst_queue2_locked_enqueue (queue, query,
2661 GST_QUEUE2_ITEM_TYPE_QUERY);
2663 STATUS (queue, queue->sinkpad, "wait for QUERY");
2664 while (queue->sinkresult == GST_FLOW_OK &&
2665 queue->last_handled_query != query)
2666 g_cond_wait (&queue->query_handled, &queue->qlock);
2667 queue->last_handled_query = NULL;
2668 if (queue->sinkresult != GST_FLOW_OK)
2670 res = queue->last_query;
2672 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2676 GST_DEBUG_OBJECT (queue,
2677 "refusing query, we are not using the queue");
2680 GST_QUEUE2_MUTEX_UNLOCK (queue);
2681 gst_queue2_post_buffering (queue);
2683 res = gst_pad_query_default (pad, parent, query);
2692 GST_DEBUG_OBJECT (queue, "refusing query, we are %s",
2693 gst_flow_get_name (queue->sinkresult));
2694 GST_QUEUE2_MUTEX_UNLOCK (queue);
2700 gst_queue2_is_empty (GstQueue2 * queue)
2702 /* never empty on EOS */
2706 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2707 return queue->current->writing_pos <= queue->current->max_reading_pos;
2709 if (gst_queue_array_get_length (queue->queue) == 0)
2717 gst_queue2_is_filled (GstQueue2 * queue)
2721 /* always filled on EOS */
2725 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2726 (queue->cur_level.format) >= ((alt_max) ? \
2727 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2729 /* if using a ring buffer we're filled if all ring buffer space is used
2730 * _by the current range_ */
2731 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2732 guint64 rb_size = queue->ring_buffer_max_size;
2733 GST_DEBUG_OBJECT (queue,
2734 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2735 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2736 return CHECK_FILLED (bytes, rb_size);
2739 /* if using file, we're never filled if we don't have EOS */
2740 if (QUEUE_IS_USING_TEMP_FILE (queue))
2743 /* we are never filled when we have no buffers at all */
2744 if (queue->cur_level.buffers == 0)
2747 /* we are filled if one of the current levels exceeds the max */
2748 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2749 || CHECK_FILLED (time, 0);
2751 /* if we need to, use the rate estimate to check against the max time we are
2752 * allowed to queue */
2753 if (queue->use_rate_estimate)
2754 res |= CHECK_FILLED (rate_time, 0);
2760 static GstFlowReturn
2761 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2762 GstMiniObject * item, GstQueue2ItemType item_type)
2764 /* we have to lock the queue since we span threads */
2765 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2766 /* when we received EOS, we refuse more data */
2769 /* when we received unexpected from downstream, refuse more buffers */
2770 if (queue->unexpected)
2771 goto out_unexpected;
2773 /* while we didn't receive the newsegment, we're seeking and we skip data */
2777 if (!gst_queue2_wait_free_space (queue))
2780 /* put buffer in queue now */
2781 gst_queue2_locked_enqueue (queue, item, item_type);
2782 GST_QUEUE2_MUTEX_UNLOCK (queue);
2783 gst_queue2_post_buffering (queue);
2787 /* special conditions */
2790 GstFlowReturn ret = queue->sinkresult;
2792 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2793 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2794 GST_QUEUE2_MUTEX_UNLOCK (queue);
2795 gst_mini_object_unref (item);
2801 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2802 GST_QUEUE2_MUTEX_UNLOCK (queue);
2803 gst_mini_object_unref (item);
2805 return GST_FLOW_EOS;
2809 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2810 GST_QUEUE2_MUTEX_UNLOCK (queue);
2811 gst_mini_object_unref (item);
2817 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2818 GST_QUEUE2_MUTEX_UNLOCK (queue);
2819 gst_mini_object_unref (item);
2821 return GST_FLOW_EOS;
2825 static GstFlowReturn
2826 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2830 queue = GST_QUEUE2 (parent);
2832 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2833 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2834 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2835 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2836 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2838 return gst_queue2_chain_buffer_or_buffer_list (queue,
2839 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2842 static GstFlowReturn
2843 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2844 GstBufferList * buffer_list)
2848 queue = GST_QUEUE2 (parent);
2850 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2851 "received buffer list %p", buffer_list);
2853 return gst_queue2_chain_buffer_or_buffer_list (queue,
2854 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2857 static GstMiniObject *
2858 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2860 GstMiniObject *data;
2862 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2864 /* stop pushing buffers, we dequeue all items until we see an item that we
2865 * can push again, which is EOS or SEGMENT. If there is nothing in the
2866 * queue we can push, we set a flag to make the sinkpad refuse more
2867 * buffers with an EOS return value until we receive something
2868 * pushable again or we get flushed. */
2869 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2870 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2871 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2872 "dropping EOS buffer %p", data);
2873 gst_buffer_unref (GST_BUFFER_CAST (data));
2874 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2875 GstEvent *event = GST_EVENT_CAST (data);
2876 GstEventType type = GST_EVENT_TYPE (event);
2878 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT
2879 || type == GST_EVENT_STREAM_START) {
2880 /* we found a pushable item in the queue, push it out */
2881 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2882 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2885 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2886 "dropping EOS event %p", event);
2887 gst_event_unref (event);
2888 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2889 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2890 "dropping EOS buffer list %p", data);
2891 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2892 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2893 queue->last_query = FALSE;
2894 g_cond_signal (&queue->query_handled);
2895 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2898 /* no more items in the queue. Set the unexpected flag so that upstream
2899 * make us refuse any more buffers on the sinkpad. Since we will still
2900 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2901 * task function does not shut down. */
2902 queue->unexpected = TRUE;
2906 /* dequeue an item from the queue an push it downstream. This functions returns
2907 * the result of the push. */
2908 static GstFlowReturn
2909 gst_queue2_push_one (GstQueue2 * queue)
2911 GstFlowReturn result = queue->srcresult;
2912 GstMiniObject *data;
2913 GstQueue2ItemType item_type;
2915 data = gst_queue2_locked_dequeue (queue, &item_type);
2920 STATUS (queue, queue->srcpad, "We have something dequeud");
2921 g_atomic_int_set (&queue->downstream_may_block,
2922 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2923 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2924 GST_QUEUE2_MUTEX_UNLOCK (queue);
2925 gst_queue2_post_buffering (queue);
2927 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2930 buffer = GST_BUFFER_CAST (data);
2932 result = gst_pad_push (queue->srcpad, buffer);
2933 g_atomic_int_set (&queue->downstream_may_block, 0);
2935 /* need to check for srcresult here as well */
2936 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2937 if (result == GST_FLOW_EOS) {
2938 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2941 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2942 * to the caller so that the task function does not shut down */
2943 result = GST_FLOW_OK;
2945 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2946 GstEvent *event = GST_EVENT_CAST (data);
2947 GstEventType type = GST_EVENT_TYPE (event);
2949 if (type == GST_EVENT_TAG) {
2950 if (queue->use_tags_bitrate) {
2954 gst_event_parse_tag (event, &tags);
2955 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2956 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2957 GST_QUEUE2_MUTEX_LOCK (queue);
2958 queue->src_tags_bitrate = bitrate;
2959 GST_QUEUE2_MUTEX_UNLOCK (queue);
2960 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
2965 gst_pad_push_event (queue->srcpad, event);
2967 /* if we're EOS, return EOS so that the task pauses. */
2968 if (type == GST_EVENT_EOS) {
2969 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2970 "pushed EOS event %p, return EOS", event);
2971 result = GST_FLOW_EOS;
2974 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2975 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2976 GstBufferList *buffer_list;
2978 buffer_list = GST_BUFFER_LIST_CAST (data);
2980 result = gst_pad_push_list (queue->srcpad, buffer_list);
2981 g_atomic_int_set (&queue->downstream_may_block, 0);
2983 /* need to check for srcresult here as well */
2984 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2985 if (result == GST_FLOW_EOS) {
2986 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2989 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2990 * to the caller so that the task function does not shut down */
2991 result = GST_FLOW_OK;
2993 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2994 GstQuery *query = GST_QUERY_CAST (data);
2996 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2997 queue->last_handled_query = query;
2998 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2999 GST_LOG_OBJECT (queue->srcpad, "Peered query");
3000 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3001 "did query %p, return %d", query, queue->last_query);
3002 g_cond_signal (&queue->query_handled);
3003 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3004 result = GST_FLOW_OK;
3011 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3012 "exit because we have no item in the queue");
3013 return GST_FLOW_ERROR;
3017 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are %s",
3018 gst_flow_get_name (queue->srcresult));
3019 return queue->srcresult;
3023 /* called repeatedly with @pad as the source pad. This function should push out
3024 * data to the peer element. */
3026 gst_queue2_loop (GstPad * pad)
3031 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
3033 /* have to lock for thread-safety */
3034 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3036 if (gst_queue2_is_empty (queue)) {
3039 /* pause the timer while we wait. The fact that we are waiting does not mean
3040 * the byterate on the output pad is lower */
3041 if ((started = queue->out_timer_started))
3042 g_timer_stop (queue->out_timer);
3044 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
3045 "queue is empty, waiting for new data");
3047 /* Wait for data to be available, we could be unlocked because of a flush. */
3048 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
3050 while (gst_queue2_is_empty (queue));
3052 /* and continue if we were running before */
3054 g_timer_continue (queue->out_timer);
3056 ret = gst_queue2_push_one (queue);
3057 queue->srcresult = ret;
3058 queue->sinkresult = ret;
3059 if (ret != GST_FLOW_OK)
3062 GST_QUEUE2_MUTEX_UNLOCK (queue);
3063 gst_queue2_post_buffering (queue);
3070 gboolean eos = queue->is_eos;
3071 GstFlowReturn ret = queue->srcresult;
3073 gst_pad_pause_task (queue->srcpad);
3074 if (ret == GST_FLOW_FLUSHING) {
3075 gst_queue2_locked_flush (queue, FALSE, FALSE);
3077 GST_QUEUE2_SIGNAL_DEL (queue);
3078 queue->last_query = FALSE;
3079 g_cond_signal (&queue->query_handled);
3081 GST_QUEUE2_MUTEX_UNLOCK (queue);
3082 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3083 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
3084 /* Recalculate buffering levels before stopping since the source flow
3085 * might cause a different buffering level (like NOT_LINKED making
3086 * the queue appear as full) */
3087 if (queue->use_buffering)
3088 update_buffering (queue);
3089 gst_queue2_post_buffering (queue);
3090 /* let app know about us giving up if upstream is not expected to do so */
3091 /* EOS is already taken care of elsewhere */
3092 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
3093 GST_ELEMENT_FLOW_ERROR (queue, ret);
3094 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
3101 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
3103 gboolean res = TRUE;
3104 GstQueue2 *queue = GST_QUEUE2 (parent);
3106 #ifndef GST_DISABLE_GST_DEBUG
3107 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
3108 event, GST_EVENT_TYPE_NAME (event));
3111 switch (GST_EVENT_TYPE (event)) {
3112 case GST_EVENT_FLUSH_START:
3113 if (QUEUE_IS_USING_QUEUE (queue)) {
3114 /* just forward upstream */
3115 res = gst_pad_push_event (queue->sinkpad, event);
3117 /* now unblock the getrange function */
3118 GST_QUEUE2_MUTEX_LOCK (queue);
3119 GST_DEBUG_OBJECT (queue, "flushing");
3120 queue->srcresult = GST_FLOW_FLUSHING;
3121 GST_QUEUE2_SIGNAL_ADD (queue);
3122 GST_QUEUE2_MUTEX_UNLOCK (queue);
3124 /* when using a temp file, we eat the event */
3126 gst_event_unref (event);
3129 case GST_EVENT_FLUSH_STOP:
3130 if (QUEUE_IS_USING_QUEUE (queue)) {
3131 /* just forward upstream */
3132 res = gst_pad_push_event (queue->sinkpad, event);
3134 /* now unblock the getrange function */
3135 GST_QUEUE2_MUTEX_LOCK (queue);
3136 queue->srcresult = GST_FLOW_OK;
3137 GST_QUEUE2_MUTEX_UNLOCK (queue);
3139 /* when using a temp file, we eat the event */
3141 gst_event_unref (event);
3144 case GST_EVENT_RECONFIGURE:
3145 GST_QUEUE2_MUTEX_LOCK (queue);
3146 /* assume downstream is linked now and try to push again */
3147 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3148 queue->srcresult = GST_FLOW_OK;
3149 queue->sinkresult = GST_FLOW_OK;
3150 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3151 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3155 GST_QUEUE2_MUTEX_UNLOCK (queue);
3157 res = gst_pad_push_event (queue->sinkpad, event);
3160 res = gst_pad_push_event (queue->sinkpad, event);
3168 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3172 queue = GST_QUEUE2 (parent);
3174 switch (GST_QUERY_TYPE (query)) {
3175 case GST_QUERY_POSITION:
3180 if (!gst_pad_peer_query (queue->sinkpad, query))
3183 /* get peer position */
3184 gst_query_parse_position (query, &format, &peer_pos);
3186 /* FIXME: this code assumes that there's no discont in the queue */
3188 case GST_FORMAT_BYTES:
3189 peer_pos -= queue->cur_level.bytes;
3190 if (peer_pos < 0) /* Clamp result to 0 */
3193 case GST_FORMAT_TIME:
3194 peer_pos -= queue->cur_level.time;
3195 if (peer_pos < 0) /* Clamp result to 0 */
3199 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3200 "know how to adjust value", gst_format_get_name (format));
3203 /* set updated position */
3204 gst_query_set_position (query, format, peer_pos);
3207 case GST_QUERY_DURATION:
3209 GST_DEBUG_OBJECT (queue, "doing peer query");
3211 if (!gst_pad_peer_query (queue->sinkpad, query))
3214 GST_DEBUG_OBJECT (queue, "peer query success");
3217 case GST_QUERY_BUFFERING:
3220 gboolean is_buffering;
3221 GstBufferingMode mode;
3222 gint avg_in, avg_out;
3223 gint64 buffering_left;
3225 GST_DEBUG_OBJECT (queue, "query buffering");
3227 get_buffering_level (queue, &is_buffering, &percent);
3228 percent = convert_to_buffering_percent (queue, percent);
3229 gst_query_set_buffering_percent (query, is_buffering, percent);
3231 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3233 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3236 if (!QUEUE_IS_USING_QUEUE (queue)) {
3237 /* add ranges for download and ringbuffer buffering */
3239 gint64 start, stop, range_start, range_stop;
3240 guint64 writing_pos;
3241 gint64 estimated_total;
3243 gboolean peer_res, is_eos;
3244 GstQueue2Range *queued_ranges;
3246 /* we need a current download region */
3247 if (queue->current == NULL)
3250 writing_pos = queue->current->writing_pos;
3251 is_eos = queue->is_eos;
3254 /* we're EOS, we know the duration in bytes now */
3256 duration = writing_pos;
3258 /* get duration of upstream in bytes */
3259 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3260 GST_FORMAT_BYTES, &duration);
3263 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3264 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3266 /* calculate remaining and total download time */
3267 if (peer_res && avg_in > 0.0)
3268 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3270 estimated_total = -1;
3272 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3275 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3278 case GST_FORMAT_PERCENT:
3279 /* we need duration */
3284 /* get our available data relative to the duration */
3287 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3292 case GST_FORMAT_BYTES:
3302 /* fill out the buffered ranges */
3303 for (queued_ranges = queue->ranges; queued_ranges;
3304 queued_ranges = queued_ranges->next) {
3306 case GST_FORMAT_PERCENT:
3307 if (duration == -1) {
3313 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3314 queued_ranges->offset, duration);
3316 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3317 queued_ranges->writing_pos, duration);
3319 case GST_FORMAT_BYTES:
3320 range_start = queued_ranges->offset;
3321 range_stop = queued_ranges->writing_pos;
3328 if (range_start == range_stop)
3330 GST_DEBUG_OBJECT (queue,
3331 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3332 G_GINT64_FORMAT, range_start, range_stop);
3333 gst_query_add_buffering_range (query, range_start, range_stop);
3336 gst_query_set_buffering_range (query, format, start, stop,
3341 case GST_QUERY_SCHEDULING:
3344 GstSchedulingFlags flags = 0;
3346 if (!gst_pad_peer_query (queue->sinkpad, query))
3349 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3351 /* we can operate in pull mode when we are using a tempfile */
3352 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3355 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3356 gst_query_set_scheduling (query, flags, 0, -1, 0);
3358 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3359 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3363 /* peer handled other queries */
3364 if (!gst_pad_query_default (pad, parent, query))
3374 GST_DEBUG_OBJECT (queue, "failed peer query");
3380 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3382 GstQueue2 *queue = GST_QUEUE2 (element);
3384 /* simply forward to the srcpad query function */
3385 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3390 gst_queue2_update_upstream_size (GstQueue2 * queue)
3392 gint64 upstream_size = -1;
3394 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3396 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3398 /* upstream_size can be negative but queue->upstream_size is unsigned.
3399 * Prevent setting negative values to it (the query can return -1) */
3400 if (upstream_size >= 0)
3401 queue->upstream_size = upstream_size;
3403 queue->upstream_size = 0;
3407 static GstFlowReturn
3408 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3409 guint length, GstBuffer ** buffer)
3414 queue = GST_QUEUE2_CAST (parent);
3416 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3417 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3418 offset = (offset == -1) ? queue->current->reading_pos : offset;
3420 GST_DEBUG_OBJECT (queue,
3421 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3423 /* catch any reads beyond the size of the file here to make sure queue2
3424 * doesn't send seek events beyond the size of the file upstream, since
3425 * that would confuse elements such as souphttpsrc and/or http servers.
3426 * Demuxers often just loop until EOS at the end of the file to figure out
3427 * when they've read all the end-headers or index chunks. */
3428 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3429 gst_queue2_update_upstream_size (queue);
3430 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3431 goto out_unexpected;
3434 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3435 gst_queue2_update_upstream_size (queue);
3436 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3437 length = queue->upstream_size - offset;
3438 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3442 /* FIXME - function will block when the range is not yet available */
3443 ret = gst_queue2_create_read (queue, offset, length, buffer);
3444 GST_QUEUE2_MUTEX_UNLOCK (queue);
3445 gst_queue2_post_buffering (queue);
3452 ret = queue->srcresult;
3454 GST_DEBUG_OBJECT (queue, "we are %s", gst_flow_get_name (ret));
3455 GST_QUEUE2_MUTEX_UNLOCK (queue);
3460 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3461 GST_QUEUE2_MUTEX_UNLOCK (queue);
3462 return GST_FLOW_EOS;
3466 /* sink currently only operates in push mode */
3468 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3469 GstPadMode mode, gboolean active)
3474 queue = GST_QUEUE2 (parent);
3477 case GST_PAD_MODE_PUSH:
3479 GST_QUEUE2_MUTEX_LOCK (queue);
3480 GST_DEBUG_OBJECT (queue, "activating push mode");
3481 queue->srcresult = GST_FLOW_OK;
3482 queue->sinkresult = GST_FLOW_OK;
3483 queue->is_eos = FALSE;
3484 queue->unexpected = FALSE;
3485 reset_rate_timer (queue);
3486 GST_QUEUE2_MUTEX_UNLOCK (queue);
3488 /* unblock chain function */
3489 GST_QUEUE2_MUTEX_LOCK (queue);
3490 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3491 queue->srcresult = GST_FLOW_FLUSHING;
3492 queue->sinkresult = GST_FLOW_FLUSHING;
3493 GST_QUEUE2_SIGNAL_DEL (queue);
3494 GST_QUEUE2_MUTEX_UNLOCK (queue);
3496 /* wait until it is unblocked and clean up */
3497 GST_PAD_STREAM_LOCK (pad);
3498 GST_QUEUE2_MUTEX_LOCK (queue);
3499 gst_queue2_locked_flush (queue, TRUE, FALSE);
3500 GST_QUEUE2_MUTEX_UNLOCK (queue);
3501 GST_PAD_STREAM_UNLOCK (pad);
3512 /* src operating in push mode, we start a task on the source pad that pushes out
3513 * buffers from the queue */
3515 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3517 gboolean result = FALSE;
3520 queue = GST_QUEUE2 (parent);
3523 GST_QUEUE2_MUTEX_LOCK (queue);
3524 GST_DEBUG_OBJECT (queue, "activating push mode");
3525 queue->srcresult = GST_FLOW_OK;
3526 queue->sinkresult = GST_FLOW_OK;
3527 queue->is_eos = FALSE;
3528 queue->unexpected = FALSE;
3530 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3531 GST_QUEUE2_MUTEX_UNLOCK (queue);
3533 /* unblock loop function */
3534 GST_QUEUE2_MUTEX_LOCK (queue);
3535 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3536 queue->srcresult = GST_FLOW_FLUSHING;
3537 queue->sinkresult = GST_FLOW_FLUSHING;
3538 /* the item add signal will unblock */
3539 GST_QUEUE2_SIGNAL_ADD (queue);
3540 GST_QUEUE2_MUTEX_UNLOCK (queue);
3542 /* step 2, make sure streaming finishes */
3543 result = gst_pad_stop_task (pad);
3545 GST_QUEUE2_MUTEX_LOCK (queue);
3546 gst_queue2_locked_flush (queue, FALSE, FALSE);
3547 GST_QUEUE2_MUTEX_UNLOCK (queue);
3553 /* pull mode, downstream will call our getrange function */
3555 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3560 queue = GST_QUEUE2 (parent);
3563 GST_QUEUE2_MUTEX_LOCK (queue);
3564 if (!QUEUE_IS_USING_QUEUE (queue)) {
3565 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3566 /* open the temp file now */
3567 result = gst_queue2_open_temp_location_file (queue);
3568 } else if (!queue->ring_buffer) {
3569 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3570 result = ! !queue->ring_buffer;
3575 GST_DEBUG_OBJECT (queue, "activating pull mode");
3576 init_ranges (queue);
3577 queue->srcresult = GST_FLOW_OK;
3578 queue->sinkresult = GST_FLOW_OK;
3579 queue->is_eos = FALSE;
3580 queue->unexpected = FALSE;
3581 queue->upstream_size = 0;
3583 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3584 /* this is not allowed, we cannot operate in pull mode without a temp
3586 queue->srcresult = GST_FLOW_FLUSHING;
3587 queue->sinkresult = GST_FLOW_FLUSHING;
3590 GST_QUEUE2_MUTEX_UNLOCK (queue);
3592 GST_QUEUE2_MUTEX_LOCK (queue);
3593 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3594 queue->srcresult = GST_FLOW_FLUSHING;
3595 queue->sinkresult = GST_FLOW_FLUSHING;
3596 /* this will unlock getrange */
3597 GST_QUEUE2_SIGNAL_ADD (queue);
3599 GST_QUEUE2_MUTEX_UNLOCK (queue);
3606 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3612 case GST_PAD_MODE_PULL:
3613 res = gst_queue2_src_activate_pull (pad, parent, active);
3615 case GST_PAD_MODE_PUSH:
3616 res = gst_queue2_src_activate_push (pad, parent, active);
3619 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3626 static GstStateChangeReturn
3627 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3630 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3632 queue = GST_QUEUE2 (element);
3634 switch (transition) {
3635 case GST_STATE_CHANGE_NULL_TO_READY:
3637 case GST_STATE_CHANGE_READY_TO_PAUSED:
3638 GST_QUEUE2_MUTEX_LOCK (queue);
3639 if (!QUEUE_IS_USING_QUEUE (queue)) {
3640 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3641 if (!gst_queue2_open_temp_location_file (queue))
3642 ret = GST_STATE_CHANGE_FAILURE;
3644 if (queue->ring_buffer) {
3645 g_free (queue->ring_buffer);
3646 queue->ring_buffer = NULL;
3648 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3649 ret = GST_STATE_CHANGE_FAILURE;
3651 init_ranges (queue);
3653 queue->segment_event_received = FALSE;
3654 queue->starting_segment = NULL;
3655 gst_event_replace (&queue->stream_start_event, NULL);
3656 GST_QUEUE2_MUTEX_UNLOCK (queue);
3658 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3664 if (ret == GST_STATE_CHANGE_FAILURE)
3667 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3669 if (ret == GST_STATE_CHANGE_FAILURE)
3672 switch (transition) {
3673 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3675 case GST_STATE_CHANGE_PAUSED_TO_READY:
3676 GST_QUEUE2_MUTEX_LOCK (queue);
3677 if (!QUEUE_IS_USING_QUEUE (queue)) {
3678 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3679 gst_queue2_close_temp_location_file (queue);
3680 } else if (queue->ring_buffer) {
3681 g_free (queue->ring_buffer);
3682 queue->ring_buffer = NULL;
3684 clean_ranges (queue);
3686 if (queue->starting_segment != NULL) {
3687 gst_event_unref (queue->starting_segment);
3688 queue->starting_segment = NULL;
3690 gst_event_replace (&queue->stream_start_event, NULL);
3691 GST_QUEUE2_MUTEX_UNLOCK (queue);
3693 case GST_STATE_CHANGE_READY_TO_NULL:
3702 /* changing the capacity of the queue must wake up
3703 * the _chain function, it might have more room now
3704 * to store the buffer/event in the queue */
3705 #define QUEUE_CAPACITY_CHANGE(q) \
3706 GST_QUEUE2_SIGNAL_DEL (queue); \
3707 if (queue->use_buffering) \
3708 update_buffering (queue);
3710 /* Changing the minimum required fill level must
3711 * wake up the _loop function as it might now
3712 * be able to preceed.
3714 #define QUEUE_THRESHOLD_CHANGE(q)\
3715 GST_QUEUE2_SIGNAL_ADD (queue);
3718 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3722 /* the element must be stopped in order to do this */
3723 GST_OBJECT_LOCK (queue);
3724 state = GST_STATE (queue);
3725 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3727 GST_OBJECT_UNLOCK (queue);
3729 /* set new location */
3730 g_free (queue->temp_template);
3731 queue->temp_template = g_strdup (template);
3738 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3739 GST_OBJECT_UNLOCK (queue);
3744 gst_queue2_set_property (GObject * object,
3745 guint prop_id, const GValue * value, GParamSpec * pspec)
3747 GstQueue2 *queue = GST_QUEUE2 (object);
3749 /* someone could change levels here, and since this
3750 * affects the get/put funcs, we need to lock for safety. */
3751 GST_QUEUE2_MUTEX_LOCK (queue);
3754 case PROP_MAX_SIZE_BYTES:
3755 queue->max_level.bytes = g_value_get_uint (value);
3756 QUEUE_CAPACITY_CHANGE (queue);
3758 case PROP_MAX_SIZE_BUFFERS:
3759 queue->max_level.buffers = g_value_get_uint (value);
3760 QUEUE_CAPACITY_CHANGE (queue);
3762 case PROP_MAX_SIZE_TIME:
3763 queue->max_level.time = g_value_get_uint64 (value);
3764 /* set rate_time to the same value. We use an extra field in the level
3765 * structure so that we can easily access and compare it */
3766 queue->max_level.rate_time = queue->max_level.time;
3767 QUEUE_CAPACITY_CHANGE (queue);
3769 case PROP_USE_BUFFERING:
3770 queue->use_buffering = g_value_get_boolean (value);
3771 if (!queue->use_buffering && queue->is_buffering) {
3772 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3773 "posting 100%% message");
3774 SET_PERCENT (queue, 100);
3775 queue->is_buffering = FALSE;
3778 if (queue->use_buffering) {
3779 queue->is_buffering = TRUE;
3780 update_buffering (queue);
3783 case PROP_USE_TAGS_BITRATE:
3784 queue->use_tags_bitrate = g_value_get_boolean (value);
3786 case PROP_USE_RATE_ESTIMATE:
3787 queue->use_rate_estimate = g_value_get_boolean (value);
3789 case PROP_LOW_PERCENT:
3790 queue->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3791 if (queue->is_buffering)
3792 update_buffering (queue);
3794 case PROP_HIGH_PERCENT:
3795 queue->high_watermark =
3796 g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3797 if (queue->is_buffering)
3798 update_buffering (queue);
3800 case PROP_LOW_WATERMARK:
3801 queue->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3802 if (queue->is_buffering)
3803 update_buffering (queue);
3805 case PROP_HIGH_WATERMARK:
3806 queue->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3807 if (queue->is_buffering)
3808 update_buffering (queue);
3810 case PROP_TEMP_TEMPLATE:
3811 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3813 case PROP_TEMP_REMOVE:
3814 queue->temp_remove = g_value_get_boolean (value);
3816 case PROP_RING_BUFFER_MAX_SIZE:
3817 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3820 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3824 GST_QUEUE2_MUTEX_UNLOCK (queue);
3825 gst_queue2_post_buffering (queue);
3829 gst_queue2_get_property (GObject * object,
3830 guint prop_id, GValue * value, GParamSpec * pspec)
3832 GstQueue2 *queue = GST_QUEUE2 (object);
3834 GST_QUEUE2_MUTEX_LOCK (queue);
3837 case PROP_CUR_LEVEL_BYTES:
3838 g_value_set_uint (value, queue->cur_level.bytes);
3840 case PROP_CUR_LEVEL_BUFFERS:
3841 g_value_set_uint (value, queue->cur_level.buffers);
3843 case PROP_CUR_LEVEL_TIME:
3844 g_value_set_uint64 (value, queue->cur_level.time);
3846 case PROP_MAX_SIZE_BYTES:
3847 g_value_set_uint (value, queue->max_level.bytes);
3849 case PROP_MAX_SIZE_BUFFERS:
3850 g_value_set_uint (value, queue->max_level.buffers);
3852 case PROP_MAX_SIZE_TIME:
3853 g_value_set_uint64 (value, queue->max_level.time);
3855 case PROP_USE_BUFFERING:
3856 g_value_set_boolean (value, queue->use_buffering);
3858 case PROP_USE_TAGS_BITRATE:
3859 g_value_set_boolean (value, queue->use_tags_bitrate);
3861 case PROP_USE_RATE_ESTIMATE:
3862 g_value_set_boolean (value, queue->use_rate_estimate);
3864 case PROP_LOW_PERCENT:
3865 g_value_set_int (value, queue->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
3867 case PROP_HIGH_PERCENT:
3868 g_value_set_int (value, queue->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
3870 case PROP_LOW_WATERMARK:
3871 g_value_set_double (value, queue->low_watermark /
3872 (gdouble) MAX_BUFFERING_LEVEL);
3874 case PROP_HIGH_WATERMARK:
3875 g_value_set_double (value, queue->high_watermark /
3876 (gdouble) MAX_BUFFERING_LEVEL);
3878 case PROP_TEMP_TEMPLATE:
3879 g_value_set_string (value, queue->temp_template);
3881 case PROP_TEMP_LOCATION:
3882 g_value_set_string (value, queue->temp_location);
3884 case PROP_TEMP_REMOVE:
3885 g_value_set_boolean (value, queue->temp_remove);
3887 case PROP_RING_BUFFER_MAX_SIZE:
3888 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3890 case PROP_AVG_IN_RATE:
3892 gdouble in_rate = queue->byte_in_rate;
3894 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3895 * calculated, so calculate it here. */
3896 if (in_rate == 0.0 && queue->bytes_in
3897 && queue->last_update_in_rates_elapsed > 0.0)
3898 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3900 g_value_set_int64 (value, (gint64) in_rate);
3904 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3908 GST_QUEUE2_MUTEX_UNLOCK (queue);