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
121 #define DEFAULT_USE_BITRATE_QUERY TRUE
126 PROP_CUR_LEVEL_BUFFERS,
127 PROP_CUR_LEVEL_BYTES,
129 PROP_MAX_SIZE_BUFFERS,
133 PROP_USE_TAGS_BITRATE,
134 PROP_USE_RATE_ESTIMATE,
142 PROP_RING_BUFFER_MAX_SIZE,
144 PROP_USE_BITRATE_QUERY,
149 /* Explanation for buffer levels and percentages:
151 * The buffering_level functions here return a value in a normalized range
152 * that specifies the queue's current fill level. The range goes from 0 to
153 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
155 * This is not to be confused with the buffering_percent value, which is
156 * a *relative* quantity - relative to the low/high watermarks.
157 * buffering_percent = 0% means buffering_level is at the low watermark.
158 * buffering_percent = 100% means buffering_level is at the high watermark.
159 * buffering_percent is used for determining if the fill level has reached
160 * the high watermark, and for producing BUFFERING messages. This value
161 * always uses a 0..100 range (since it is a percentage).
163 * To avoid future confusions, whenever "buffering level" is mentioned, it
164 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
165 * range. Whenever "buffering_percent" is mentioned, it refers to the
166 * percentage value that is relative to the low/high watermark. */
168 /* Using a buffering level range of 0..1000000 to allow for a
169 * resolution in ppm (1 ppm = 0.0001%) */
170 #define MAX_BUFFERING_LEVEL 1000000
172 /* How much 1% makes up in the buffer level range */
173 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
175 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
182 #define STATUS(queue, pad, msg) \
183 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
184 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
185 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
186 " ns, %"G_GUINT64_FORMAT" items", \
187 GST_DEBUG_PAD_NAME (pad), \
188 queue->cur_level.buffers, \
189 queue->max_level.buffers, \
190 queue->cur_level.bytes, \
191 queue->max_level.bytes, \
192 queue->cur_level.time, \
193 queue->max_level.time, \
194 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
195 queue->current->writing_pos - queue->current->max_reading_pos : \
196 gst_queue_array_get_length(queue->queue)))
198 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
199 g_mutex_lock (&q->qlock); \
202 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
203 GST_QUEUE2_MUTEX_LOCK (q); \
204 if (res != GST_FLOW_OK) \
208 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
209 g_mutex_unlock (&q->qlock); \
212 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
213 STATUS (queue, q->sinkpad, "wait for DEL"); \
214 q->waiting_del = TRUE; \
215 g_cond_wait (&q->item_del, &queue->qlock); \
216 q->waiting_del = FALSE; \
217 if (res != GST_FLOW_OK) { \
218 STATUS (queue, q->srcpad, "received DEL wakeup"); \
221 STATUS (queue, q->sinkpad, "received DEL"); \
224 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
225 STATUS (queue, q->srcpad, "wait for ADD"); \
226 q->waiting_add = TRUE; \
227 g_cond_wait (&q->item_add, &q->qlock); \
228 q->waiting_add = FALSE; \
229 if (res != GST_FLOW_OK) { \
230 STATUS (queue, q->srcpad, "received ADD wakeup"); \
233 STATUS (queue, q->srcpad, "received ADD"); \
236 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
237 if (q->waiting_del) { \
238 STATUS (q, q->srcpad, "signal DEL"); \
239 g_cond_signal (&q->item_del); \
243 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
244 if (q->waiting_add) { \
245 STATUS (q, q->sinkpad, "signal ADD"); \
246 g_cond_signal (&q->item_add); \
250 #define SET_PERCENT(q, perc) G_STMT_START { \
251 if (perc != q->buffering_percent) { \
252 q->buffering_percent = perc; \
253 q->percent_changed = TRUE; \
254 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
255 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
256 &q->buffering_left); \
261 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
262 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
263 "dataflow inside the queue element");
264 #define gst_queue2_parent_class parent_class
265 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
267 static void gst_queue2_finalize (GObject * object);
269 static void gst_queue2_set_property (GObject * object,
270 guint prop_id, const GValue * value, GParamSpec * pspec);
271 static void gst_queue2_get_property (GObject * object,
272 guint prop_id, GValue * value, GParamSpec * pspec);
274 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
276 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
277 GstBufferList * buffer_list);
278 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
279 static void gst_queue2_loop (GstPad * pad);
281 static GstFlowReturn gst_queue2_handle_sink_event (GstPad * pad,
282 GstObject * parent, GstEvent * event);
283 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
286 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
288 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
290 static gboolean gst_queue2_handle_query (GstElement * element,
293 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
294 guint64 offset, guint length, GstBuffer ** buffer);
296 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
297 GstPadMode mode, gboolean active);
298 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
299 GstPadMode mode, gboolean active);
300 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
301 GstStateChange transition);
303 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
304 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
306 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
307 static void update_in_rates (GstQueue2 * queue, gboolean force);
308 static GstMessage *gst_queue2_get_buffering_message (GstQueue2 * queue);
309 static void gst_queue2_post_buffering (GstQueue2 * queue);
313 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
314 GST_QUEUE2_ITEM_TYPE_BUFFER,
315 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
316 GST_QUEUE2_ITEM_TYPE_EVENT,
317 GST_QUEUE2_ITEM_TYPE_QUERY
322 GstQueue2ItemType type;
326 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
329 gst_queue2_class_init (GstQueue2Class * klass)
331 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
332 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
334 gobject_class->set_property = gst_queue2_set_property;
335 gobject_class->get_property = gst_queue2_get_property;
338 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
339 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
340 "Current amount of data in the queue (bytes)",
341 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
343 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
344 "Current number of buffers in the queue",
345 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
347 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
348 "Current amount of data in the queue (in ns)",
349 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
352 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
353 "Max. amount of data in the queue (bytes, 0=disable)",
354 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
355 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
356 G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
358 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
359 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
360 DEFAULT_MAX_SIZE_BUFFERS,
361 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
362 G_PARAM_STATIC_STRINGS));
363 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
364 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
365 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
366 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
367 G_PARAM_STATIC_STRINGS));
369 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
370 g_param_spec_boolean ("use-buffering", "Use buffering",
371 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
372 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
373 G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (gobject_class, PROP_USE_TAGS_BITRATE,
375 g_param_spec_boolean ("use-tags-bitrate", "Use bitrate from tags",
376 "Use a bitrate from upstream tags to estimate buffer duration if not provided",
377 DEFAULT_USE_TAGS_BITRATE,
378 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
379 G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
381 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
382 "Estimate the bitrate of the stream to calculate time level",
383 DEFAULT_USE_RATE_ESTIMATE,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
386 g_param_spec_int ("low-percent", "Low percent",
387 "Low threshold for buffering to start. Only used if use-buffering is True "
388 "(Deprecated: use low-watermark instead)",
389 0, 100, DEFAULT_LOW_WATERMARK * 100,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
392 g_param_spec_int ("high-percent", "High percent",
393 "High threshold for buffering to finish. Only used if use-buffering is True "
394 "(Deprecated: use high-watermark instead)",
395 0, 100, DEFAULT_HIGH_WATERMARK * 100,
396 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
398 g_param_spec_double ("low-watermark", "Low watermark",
399 "Low threshold for buffering to start. Only used if use-buffering is True",
400 0.0, 1.0, DEFAULT_LOW_WATERMARK,
401 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
403 g_param_spec_double ("high-watermark", "High watermark",
404 "High threshold for buffering to finish. Only used if use-buffering is True",
405 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
406 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
409 g_param_spec_string ("temp-template", "Temporary File Template",
410 "File template to store temporary files in, should contain directory "
411 "and XXXXXX. (NULL == disabled)",
412 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
415 g_param_spec_string ("temp-location", "Temporary File Location",
416 "Location to store temporary files in (Only read this property, "
417 "use temp-template to configure the name template)",
418 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
419 g_object_class_install_property (gobject_class, PROP_USE_BITRATE_QUERY,
420 g_param_spec_boolean ("use-bitrate-query",
421 "Use bitrate from downstream query",
422 "Use a bitrate from a downstream query to estimate buffer duration if not provided",
423 DEFAULT_USE_BITRATE_QUERY,
424 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
425 G_PARAM_STATIC_STRINGS));
428 * GstQueue2:temp-remove
430 * When temp-template is set, remove the temporary file when going to READY.
432 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
433 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
434 "Remove the temp-location after use",
435 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 * GstQueue2:ring-buffer-max-size
440 * The maximum size of the ring buffer in bytes. If set to 0, the ring
441 * buffer is disabled. Default 0.
443 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
444 g_param_spec_uint64 ("ring-buffer-max-size",
445 "Max. ring buffer size (bytes)",
446 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
447 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 * GstQueue2:avg-in-rate
453 * The average input data rate.
455 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
456 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
457 "Average input data rate (bytes/s)",
458 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
463 * The value used to convert between byte and time values for limiting
464 * the size of the queue. Values are taken from either the upstream tags
465 * or from the downstream bitrate query.
467 g_object_class_install_property (gobject_class, PROP_BITRATE,
468 g_param_spec_uint64 ("bitrate", "Bitrate (bits/s)",
469 "Conversion value between data size and time",
470 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
472 /* set several parent class virtual functions */
473 gobject_class->finalize = gst_queue2_finalize;
475 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
476 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
478 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
481 "Erik Walthinsen <omega@cse.ogi.edu>, "
482 "Wim Taymans <wim.taymans@gmail.com>");
484 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
485 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
489 gst_queue2_init (GstQueue2 * queue)
491 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
493 gst_pad_set_chain_function (queue->sinkpad,
494 GST_DEBUG_FUNCPTR (gst_queue2_chain));
495 gst_pad_set_chain_list_function (queue->sinkpad,
496 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
497 gst_pad_set_activatemode_function (queue->sinkpad,
498 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
499 gst_pad_set_event_full_function (queue->sinkpad,
500 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
501 gst_pad_set_query_function (queue->sinkpad,
502 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
503 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
504 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
506 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
508 gst_pad_set_activatemode_function (queue->srcpad,
509 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
510 gst_pad_set_getrange_function (queue->srcpad,
511 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
512 gst_pad_set_event_function (queue->srcpad,
513 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
514 gst_pad_set_query_function (queue->srcpad,
515 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
516 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
517 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
520 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
521 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
522 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
523 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
524 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
525 queue->use_buffering = DEFAULT_USE_BUFFERING;
526 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
527 queue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
528 queue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
530 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
531 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
533 queue->sinktime = GST_CLOCK_TIME_NONE;
534 queue->srctime = GST_CLOCK_TIME_NONE;
535 queue->sink_tainted = TRUE;
536 queue->src_tainted = TRUE;
538 queue->srcresult = GST_FLOW_FLUSHING;
539 queue->sinkresult = GST_FLOW_FLUSHING;
540 queue->is_eos = FALSE;
541 queue->in_timer = g_timer_new ();
542 queue->out_timer = g_timer_new ();
544 g_mutex_init (&queue->qlock);
545 queue->waiting_add = FALSE;
546 g_cond_init (&queue->item_add);
547 queue->waiting_del = FALSE;
548 g_cond_init (&queue->item_del);
549 queue->queue = gst_queue_array_new_for_struct (sizeof (GstQueue2Item), 32);
551 g_cond_init (&queue->query_handled);
552 queue->last_query = FALSE;
554 g_mutex_init (&queue->buffering_post_lock);
555 queue->buffering_percent = 100;
556 queue->last_posted_buffering_percent = -1;
558 /* tempfile related */
559 queue->temp_template = NULL;
560 queue->temp_location = NULL;
561 queue->temp_remove = DEFAULT_TEMP_REMOVE;
563 queue->ring_buffer = NULL;
564 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
566 queue->use_bitrate_query = DEFAULT_USE_BITRATE_QUERY;
568 GST_DEBUG_OBJECT (queue,
569 "initialized queue's not_empty & not_full conditions");
572 /* called only once, as opposed to dispose */
574 gst_queue2_finalize (GObject * object)
576 GstQueue2 *queue = GST_QUEUE2 (object);
577 GstQueue2Item *qitem;
579 GST_DEBUG_OBJECT (queue, "finalizing queue");
581 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
582 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
583 gst_mini_object_unref (qitem->item);
585 gst_queue_array_free (queue->queue);
587 queue->last_query = FALSE;
588 g_mutex_clear (&queue->qlock);
589 g_mutex_clear (&queue->buffering_post_lock);
590 g_cond_clear (&queue->item_add);
591 g_cond_clear (&queue->item_del);
592 g_cond_clear (&queue->query_handled);
593 g_timer_destroy (queue->in_timer);
594 g_timer_destroy (queue->out_timer);
596 /* temp_file path cleanup */
597 g_free (queue->temp_template);
598 g_free (queue->temp_location);
600 G_OBJECT_CLASS (parent_class)->finalize (object);
604 debug_ranges (GstQueue2 * queue)
606 GstQueue2Range *walk;
608 for (walk = queue->ranges; walk; walk = walk->next) {
609 GST_DEBUG_OBJECT (queue,
610 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
611 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
612 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
613 walk->rb_writing_pos, walk->reading_pos,
614 walk == queue->current ? "**y**" : " n ");
618 /* clear all the downloaded ranges */
620 clean_ranges (GstQueue2 * queue)
622 GST_DEBUG_OBJECT (queue, "clean queue ranges");
624 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
625 queue->ranges = NULL;
626 queue->current = NULL;
629 /* find a range that contains @offset or NULL when nothing does */
630 static GstQueue2Range *
631 find_range (GstQueue2 * queue, guint64 offset)
633 GstQueue2Range *range = NULL;
634 GstQueue2Range *walk;
636 /* first do a quick check for the current range */
637 for (walk = queue->ranges; walk; walk = walk->next) {
638 if (offset >= walk->offset && offset <= walk->writing_pos) {
639 /* we can reuse an existing range */
645 GST_DEBUG_OBJECT (queue,
646 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
647 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
649 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
655 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
657 guint64 max_reading_pos, writing_pos;
659 writing_pos = range->writing_pos;
660 max_reading_pos = range->max_reading_pos;
662 if (writing_pos > max_reading_pos)
663 queue->cur_level.bytes = writing_pos - max_reading_pos;
665 queue->cur_level.bytes = 0;
668 /* make a new range for @offset or reuse an existing range */
669 static GstQueue2Range *
670 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
672 GstQueue2Range *range, *prev, *next;
674 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
676 if ((range = find_range (queue, offset))) {
677 GST_DEBUG_OBJECT (queue,
678 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
680 if (update_existing && range->writing_pos != offset) {
681 GST_DEBUG_OBJECT (queue, "updating range writing position to "
682 "%" G_GUINT64_FORMAT, offset);
683 range->writing_pos = offset;
686 GST_DEBUG_OBJECT (queue,
687 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
689 range = g_slice_new0 (GstQueue2Range);
690 range->offset = offset;
691 /* we want to write to the next location in the ring buffer */
692 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
693 range->writing_pos = offset;
694 range->rb_writing_pos = range->rb_offset;
695 range->reading_pos = offset;
696 range->max_reading_pos = offset;
700 next = queue->ranges;
702 if (next->offset > offset) {
703 /* insert before next */
704 GST_DEBUG_OBJECT (queue,
705 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
717 queue->ranges = range;
719 debug_ranges (queue);
721 /* update the stats for this range */
722 update_cur_level (queue, range);
728 /* clear and init the download ranges for offset 0 */
730 init_ranges (GstQueue2 * queue)
732 GST_DEBUG_OBJECT (queue, "init queue ranges");
734 /* get rid of all the current ranges */
735 clean_ranges (queue);
736 /* make a range for offset 0 */
737 queue->current = add_range (queue, 0, TRUE);
740 /* calculate the diff between running time on the sink and src of the queue.
741 * This is the total amount of time in the queue. */
743 update_time_level (GstQueue2 * queue)
745 if (queue->sink_tainted) {
747 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
748 queue->sink_segment.position);
749 queue->sink_tainted = FALSE;
752 if (queue->src_tainted) {
754 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
755 queue->src_segment.position);
756 queue->src_tainted = FALSE;
759 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
760 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
762 if (queue->sinktime != GST_CLOCK_TIME_NONE
763 && queue->srctime != GST_CLOCK_TIME_NONE
764 && queue->sinktime >= queue->srctime)
765 queue->cur_level.time = queue->sinktime - queue->srctime;
767 queue->cur_level.time = 0;
770 /* take a SEGMENT event and apply the values to segment, updating the time
773 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
776 gst_event_copy_segment (event, segment);
778 if (segment->format == GST_FORMAT_BYTES) {
779 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
780 /* start is where we'll be getting from and as such writing next */
781 queue->current = add_range (queue, segment->start, TRUE);
785 /* now configure the values, we use these to track timestamps on the
787 if (segment->format != GST_FORMAT_TIME) {
788 /* non-time format, pretend the current time segment is closed with a
789 * 0 start and unknown stop time. */
790 segment->format = GST_FORMAT_TIME;
796 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
799 queue->sink_tainted = TRUE;
801 queue->src_tainted = TRUE;
803 /* segment can update the time level of the queue */
804 update_time_level (queue);
808 apply_gap (GstQueue2 * queue, GstEvent * event,
809 GstSegment * segment, gboolean is_sink)
811 GstClockTime timestamp;
812 GstClockTime duration;
814 gst_event_parse_gap (event, ×tamp, &duration);
816 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
818 if (GST_CLOCK_TIME_IS_VALID (duration)) {
819 timestamp += duration;
822 segment->position = timestamp;
825 queue->sink_tainted = TRUE;
827 queue->src_tainted = TRUE;
829 /* calc diff with other end */
830 update_time_level (queue);
835 query_downstream_bitrate (GstQueue2 * queue)
837 GstQuery *query = gst_query_new_bitrate ();
838 guint downstream_bitrate = 0;
840 if (gst_pad_peer_query (queue->srcpad, query)) {
841 gst_query_parse_bitrate (query, &downstream_bitrate);
842 GST_DEBUG_OBJECT (queue, "Got bitrate of %u from downstream",
845 GST_DEBUG_OBJECT (queue, "Failed to query bitrate from downstream");
848 gst_query_unref (query);
850 GST_QUEUE2_MUTEX_LOCK (queue);
851 queue->downstream_bitrate = downstream_bitrate;
852 GST_QUEUE2_MUTEX_UNLOCK (queue);
854 g_object_notify (G_OBJECT (queue), "bitrate");
857 /* take a buffer and update segment, updating the time level of the queue. */
859 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
860 guint64 size, gboolean is_sink)
862 GstClockTime duration, timestamp;
864 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
865 duration = GST_BUFFER_DURATION (buffer);
867 /* If we have no duration, pick one from the bitrate if we can */
868 if (duration == GST_CLOCK_TIME_NONE) {
869 if (queue->use_tags_bitrate) {
871 is_sink ? queue->sink_tags_bitrate : queue->src_tags_bitrate;
873 duration = gst_util_uint64_scale (size, 8 * GST_SECOND, bitrate);
875 if (duration == GST_CLOCK_TIME_NONE && !is_sink && queue->use_bitrate_query) {
876 if (queue->downstream_bitrate > 0) {
878 gst_util_uint64_scale (size, 8 * GST_SECOND,
879 queue->downstream_bitrate);
881 GST_LOG_OBJECT (queue, "got bitrate %u resulting in estimated "
882 "duration %" GST_TIME_FORMAT, queue->downstream_bitrate,
883 GST_TIME_ARGS (duration));
888 /* if no timestamp is set, assume it's continuous with the previous
890 if (timestamp == GST_CLOCK_TIME_NONE)
891 timestamp = segment->position;
894 if (duration != GST_CLOCK_TIME_NONE)
895 timestamp += duration;
897 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
898 GST_TIME_ARGS (timestamp));
900 segment->position = timestamp;
903 queue->sink_tainted = TRUE;
905 queue->src_tainted = TRUE;
907 /* calc diff with other end */
908 update_time_level (queue);
913 GstClockTime timestamp;
918 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
920 struct BufListData *bld = data;
921 GstClockTime *timestamp = &bld->timestamp;
924 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
925 " duration %" GST_TIME_FORMAT, idx,
926 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
927 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
928 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
930 btime = GST_BUFFER_DTS_OR_PTS (*buf);
931 if (GST_CLOCK_TIME_IS_VALID (btime))
934 if (GST_BUFFER_DURATION_IS_VALID (*buf))
935 *timestamp += GST_BUFFER_DURATION (*buf);
936 else if (bld->bitrate != 0) {
937 guint64 size = gst_buffer_get_size (*buf);
939 /* If we have no duration, pick one from the bitrate if we can */
940 *timestamp += gst_util_uint64_scale (bld->bitrate, 8 * GST_SECOND, size);
944 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
948 /* take a buffer list and update segment, updating the time level of the queue */
950 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
951 GstSegment * segment, gboolean is_sink)
953 struct BufListData bld;
955 /* if no timestamp is set, assume it's continuous with the previous time */
956 bld.timestamp = segment->position;
959 if (queue->use_tags_bitrate) {
961 bld.bitrate = queue->sink_tags_bitrate;
963 bld.bitrate = queue->src_tags_bitrate;
965 if (!is_sink && bld.bitrate == 0 && queue->use_bitrate_query) {
966 bld.bitrate = queue->downstream_bitrate;
969 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &bld);
971 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
972 GST_TIME_ARGS (bld.timestamp));
974 segment->position = bld.timestamp;
977 queue->sink_tainted = TRUE;
979 queue->src_tainted = TRUE;
981 /* calc diff with other end */
982 update_time_level (queue);
986 normalize_to_buffering_level (guint64 cur_level, guint64 max_level,
995 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL,
996 MIN (max_level, alt_max));
998 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL, max_level);
1000 return MIN (p, MAX_BUFFERING_LEVEL);
1004 get_buffering_level (GstQueue2 * queue, gboolean * is_buffering,
1005 gint * buffering_level)
1007 gint buflevel, buflevel2;
1009 if (queue->high_watermark <= 0) {
1010 if (buffering_level)
1011 *buffering_level = MAX_BUFFERING_LEVEL;
1013 *is_buffering = FALSE;
1016 #define GET_BUFFER_LEVEL_FOR_QUANTITY(format,alt_max) \
1017 normalize_to_buffering_level (queue->cur_level.format,queue->max_level.format,(alt_max))
1019 if (queue->is_eos || queue->srcresult == GST_FLOW_NOT_LINKED) {
1020 /* on EOS and NOT_LINKED we are always 100% full, we set the var
1021 * here so that we can reuse the logic below to stop buffering */
1022 buflevel = MAX_BUFFERING_LEVEL;
1023 GST_LOG_OBJECT (queue, "we are %s", queue->is_eos ? "EOS" : "NOT_LINKED");
1025 GST_LOG_OBJECT (queue,
1026 "Cur level bytes/time/rate-time/buffers %u/%" GST_TIME_FORMAT "/%"
1027 GST_TIME_FORMAT "/%u", queue->cur_level.bytes,
1028 GST_TIME_ARGS (queue->cur_level.time),
1029 GST_TIME_ARGS (queue->cur_level.rate_time), queue->cur_level.buffers);
1031 /* figure out the buffering level we are filled, we take the max of all formats. */
1032 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1033 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, 0);
1035 guint64 rb_size = queue->ring_buffer_max_size;
1036 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, rb_size);
1039 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (time, 0);
1040 buflevel = MAX (buflevel, buflevel2);
1042 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (buffers, 0);
1043 buflevel = MAX (buflevel, buflevel2);
1045 /* also apply the rate estimate when we need to */
1046 if (queue->use_rate_estimate) {
1047 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (rate_time, 0);
1048 buflevel = MAX (buflevel, buflevel2);
1051 /* Don't get to 0% unless we're really empty */
1052 if (queue->cur_level.bytes > 0)
1053 buflevel = MAX (1, buflevel);
1055 #undef GET_BUFFER_LEVEL_FOR_QUANTITY
1058 *is_buffering = queue->is_buffering;
1060 if (buffering_level)
1061 *buffering_level = buflevel;
1063 GST_DEBUG_OBJECT (queue, "buffering %d, level %d", queue->is_buffering,
1070 convert_to_buffering_percent (GstQueue2 * queue, gint buffering_level)
1074 /* scale so that if buffering_level equals the high watermark,
1075 * the percentage is 100% */
1076 percent = buffering_level * 100 / queue->high_watermark;
1085 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
1086 gint * avg_in, gint * avg_out, gint64 * buffering_left)
1089 if (!QUEUE_IS_USING_QUEUE (queue)) {
1090 if (QUEUE_IS_USING_RING_BUFFER (queue))
1091 *mode = GST_BUFFERING_TIMESHIFT;
1093 *mode = GST_BUFFERING_DOWNLOAD;
1095 *mode = GST_BUFFERING_STREAM;
1100 *avg_in = queue->byte_in_rate;
1102 *avg_out = queue->byte_out_rate;
1104 if (buffering_left) {
1105 *buffering_left = (percent == 100 ? 0 : -1);
1107 if (queue->use_rate_estimate) {
1110 max = queue->max_level.rate_time;
1111 cur = queue->cur_level.rate_time;
1113 if (percent != 100 && max > cur)
1114 *buffering_left = (max - cur) / 1000000;
1119 /* Called with the lock taken */
1121 gst_queue2_get_buffering_message (GstQueue2 * queue)
1123 GstMessage *msg = NULL;
1124 if (queue->percent_changed) {
1125 /* Don't change the buffering level if the sinkpad is waiting for
1126 * space to become available. This prevents the situation where,
1127 * upstream is pushing buffers larger than our limits so only 1 buffer
1128 * is ever in the queue at a time.
1129 * Changing the level causes a buffering message to be posted saying that
1130 * we are buffering which the application may pause to wait for another
1131 * 100% buffering message which would be posted very soon after the
1132 * waiting sink thread adds it's buffer to the queue */
1133 /* FIXME: This situation above can still occur later if
1134 * the sink pad is waiting to push a serialized event into the queue and
1135 * the queue becomes empty for a short period of time. */
1136 if (!queue->waiting_del
1137 && queue->last_posted_buffering_percent != queue->buffering_percent) {
1138 gint percent = queue->buffering_percent;
1140 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
1141 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
1143 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
1144 queue->avg_out, queue->buffering_left);
1146 queue->last_posted_buffering_percent = percent;
1148 queue->percent_changed = FALSE;
1155 gst_queue2_post_buffering (GstQueue2 * queue)
1157 GstMessage *msg = NULL;
1159 g_mutex_lock (&queue->buffering_post_lock);
1160 GST_QUEUE2_MUTEX_LOCK (queue);
1161 msg = gst_queue2_get_buffering_message (queue);
1162 GST_QUEUE2_MUTEX_UNLOCK (queue);
1165 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1167 g_mutex_unlock (&queue->buffering_post_lock);
1171 update_buffering (GstQueue2 * queue)
1173 gint buffering_level, percent;
1175 /* Ensure the variables used to calculate buffering state are up-to-date. */
1177 update_cur_level (queue, queue->current);
1178 update_in_rates (queue, FALSE);
1180 if (!get_buffering_level (queue, NULL, &buffering_level))
1183 percent = convert_to_buffering_percent (queue, buffering_level);
1185 if (queue->is_buffering) {
1186 /* if we were buffering see if we reached the high watermark */
1188 queue->is_buffering = FALSE;
1190 SET_PERCENT (queue, percent);
1192 /* we were not buffering, check if we need to start buffering if we drop
1193 * below the low threshold */
1194 if (buffering_level < queue->low_watermark) {
1195 queue->is_buffering = TRUE;
1196 SET_PERCENT (queue, percent);
1202 reset_rate_timer (GstQueue2 * queue)
1204 queue->bytes_in = 0;
1205 queue->bytes_out = 0;
1206 queue->byte_in_rate = 0.0;
1207 queue->byte_in_period = 0;
1208 queue->byte_out_rate = 0.0;
1209 queue->last_update_in_rates_elapsed = 0.0;
1210 queue->last_in_elapsed = 0.0;
1211 queue->last_out_elapsed = 0.0;
1212 queue->in_timer_started = FALSE;
1213 queue->out_timer_started = FALSE;
1216 /* the interval in seconds to recalculate the rate */
1217 #define RATE_INTERVAL 0.2
1218 /* Tuning for rate estimation. We use a large window for the input rate because
1219 * it should be stable when connected to a network. The output rate is less
1220 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1221 * therefore adapt more quickly.
1222 * However, initial input rate may be subject to a burst, and should therefore
1223 * initially also adapt more quickly to changes, and only later on give higher
1224 * weight to previous values. */
1225 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1226 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1229 update_in_rates (GstQueue2 * queue, gboolean force)
1231 gdouble elapsed, period;
1232 gdouble byte_in_rate;
1234 if (!queue->in_timer_started) {
1235 queue->in_timer_started = TRUE;
1236 g_timer_start (queue->in_timer);
1240 queue->last_update_in_rates_elapsed = elapsed =
1241 g_timer_elapsed (queue->in_timer, NULL);
1243 /* recalc after each interval. */
1244 if (force || queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1245 period = elapsed - queue->last_in_elapsed;
1247 GST_DEBUG_OBJECT (queue,
1248 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1249 period, queue->bytes_in, queue->byte_in_period);
1251 byte_in_rate = queue->bytes_in / period;
1253 if (queue->byte_in_rate == 0.0)
1254 queue->byte_in_rate = byte_in_rate;
1256 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1257 (double) queue->byte_in_period, period);
1259 /* another data point, cap at 16 for long time running average */
1260 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1261 queue->byte_in_period += period;
1263 /* reset the values to calculate rate over the next interval */
1264 queue->last_in_elapsed = elapsed;
1265 queue->bytes_in = 0;
1268 if (queue->use_bitrate_query && queue->downstream_bitrate > 0) {
1269 queue->cur_level.rate_time =
1270 gst_util_uint64_scale (8 * queue->cur_level.bytes, GST_SECOND,
1271 queue->downstream_bitrate);
1272 GST_LOG_OBJECT (queue,
1273 "got bitrate %u with byte level %u resulting in time %"
1274 GST_TIME_FORMAT, queue->downstream_bitrate, queue->cur_level.bytes,
1275 GST_TIME_ARGS (queue->cur_level.rate_time));
1276 } else if (queue->byte_in_rate > 0.0) {
1277 queue->cur_level.rate_time =
1278 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1280 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1281 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1285 update_out_rates (GstQueue2 * queue)
1287 gdouble elapsed, period;
1288 gdouble byte_out_rate;
1290 if (!queue->out_timer_started) {
1291 queue->out_timer_started = TRUE;
1292 g_timer_start (queue->out_timer);
1296 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1298 /* recalc after each interval. */
1299 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1300 period = elapsed - queue->last_out_elapsed;
1302 GST_DEBUG_OBJECT (queue,
1303 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1305 byte_out_rate = queue->bytes_out / period;
1307 if (queue->byte_out_rate == 0.0)
1308 queue->byte_out_rate = byte_out_rate;
1310 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1312 /* reset the values to calculate rate over the next interval */
1313 queue->last_out_elapsed = elapsed;
1314 queue->bytes_out = 0;
1316 if (queue->byte_in_rate > 0.0) {
1317 queue->cur_level.rate_time =
1318 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1320 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1321 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1325 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1327 guint64 reading_pos, max_reading_pos;
1330 max_reading_pos = range->max_reading_pos;
1332 max_reading_pos = MAX (max_reading_pos, reading_pos);
1334 GST_DEBUG_OBJECT (queue,
1335 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1336 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1337 range->max_reading_pos = max_reading_pos;
1339 update_cur_level (queue, range);
1343 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1348 /* until we receive the FLUSH_STOP from this seek, we skip data */
1349 queue->seeking = TRUE;
1350 GST_QUEUE2_MUTEX_UNLOCK (queue);
1352 debug_ranges (queue);
1354 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1357 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1358 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1359 GST_SEEK_TYPE_NONE, -1);
1361 res = gst_pad_push_event (queue->sinkpad, event);
1362 GST_QUEUE2_MUTEX_LOCK (queue);
1365 /* Between us sending the seek event and re-acquiring the lock, the source
1366 * thread might already have pushed data and moved along the range's
1367 * writing_pos beyond the seek offset. In that case we don't want to set
1368 * the writing position back to the requested seek position, as it would
1369 * cause data to be written to the wrong offset in the file or ring buffer.
1370 * We still do the add_range call to switch the current range to the
1371 * requested range, or create one if one doesn't exist yet. */
1372 queue->current = add_range (queue, offset, FALSE);
1378 /* get the threshold for when we decide to seek rather than wait */
1380 get_seek_threshold (GstQueue2 * queue)
1384 /* FIXME, find a good threshold based on the incoming rate. */
1385 threshold = 1024 * 512;
1387 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1388 threshold = MIN (threshold,
1389 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1394 /* see if there is enough data in the file to read a full buffer */
1396 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1398 GstQueue2Range *range;
1400 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1403 if ((range = find_range (queue, offset))) {
1404 if (queue->current != range) {
1405 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1406 perform_seek_to_offset (queue, range->writing_pos);
1409 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1410 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1412 /* we have a range for offset */
1413 GST_DEBUG_OBJECT (queue,
1414 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1415 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1417 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1420 if (offset + length <= range->writing_pos)
1423 GST_DEBUG_OBJECT (queue,
1424 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1425 (offset + length) - range->writing_pos);
1428 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1429 " len %u", offset, length);
1430 /* we don't have the range, see how far away we are */
1431 if (!queue->is_eos && queue->current) {
1432 guint64 threshold = get_seek_threshold (queue);
1434 if (offset >= queue->current->offset && offset <=
1435 queue->current->writing_pos + threshold) {
1436 GST_INFO_OBJECT (queue,
1437 "requested data is within range, wait for data");
1442 /* too far away, do a seek */
1443 perform_seek_to_offset (queue, offset);
1450 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1451 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1452 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1454 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1457 static GstFlowReturn
1458 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1459 guint8 * dst, gint64 * read_return)
1461 guint8 *ring_buffer;
1464 ring_buffer = queue->ring_buffer;
1466 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1469 /* this should not block */
1470 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1472 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1473 res = fread (dst, 1, length, queue->temp_file);
1475 memcpy (dst, ring_buffer + offset, length);
1479 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1481 if (G_UNLIKELY (res < length)) {
1482 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1483 goto could_not_read;
1484 /* check for errors or EOF */
1485 if (ferror (queue->temp_file))
1486 goto could_not_read;
1487 if (feof (queue->temp_file) && length > 0)
1497 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1498 return GST_FLOW_ERROR;
1502 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1503 return GST_FLOW_ERROR;
1507 GST_DEBUG ("non-regular file hits EOS");
1508 return GST_FLOW_EOS;
1512 static GstFlowReturn
1513 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1514 GstBuffer ** buffer)
1519 guint64 file_offset;
1520 guint block_length, remaining, read_length;
1524 GstFlowReturn ret = GST_FLOW_OK;
1526 /* allocate the output buffer of the requested size */
1527 if (*buffer == NULL)
1528 buf = gst_buffer_new_allocate (NULL, length, NULL);
1532 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
1533 goto buffer_write_fail;
1536 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1540 rb_size = queue->ring_buffer_max_size;
1541 max_size = QUEUE_MAX_BYTES (queue);
1544 while (remaining > 0) {
1545 /* configure how much/whether to read */
1546 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1549 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1552 /* calculate how far away the offset is */
1553 if (queue->current->writing_pos > rpos)
1554 level = queue->current->writing_pos - rpos;
1558 GST_DEBUG_OBJECT (queue,
1559 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1560 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1561 rpos, queue->current->writing_pos, level, max_size);
1563 if (level >= max_size) {
1564 /* we don't have the data but if we have a ring buffer that is full, we
1566 GST_DEBUG_OBJECT (queue,
1567 "ring buffer full, reading QUEUE_MAX_BYTES %"
1568 G_GUINT64_FORMAT " bytes", max_size);
1569 read_length = max_size;
1570 } else if (queue->is_eos) {
1571 /* won't get any more data so read any data we have */
1573 GST_DEBUG_OBJECT (queue,
1574 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1576 read_length = level;
1584 if (read_length == 0) {
1585 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1586 GST_DEBUG_OBJECT (queue,
1587 "update current position [%" G_GUINT64_FORMAT "-%"
1588 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1589 update_cur_pos (queue, queue->current, rpos);
1590 GST_QUEUE2_SIGNAL_DEL (queue);
1593 if (queue->use_buffering)
1594 update_buffering (queue);
1596 GST_DEBUG_OBJECT (queue, "waiting for add");
1597 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1601 /* we have the requested data so read it */
1602 read_length = remaining;
1605 /* set range reading_pos to actual reading position for this read */
1606 queue->current->reading_pos = rpos;
1608 /* configure how much and from where to read */
1609 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1611 (queue->current->rb_offset + (rpos -
1612 queue->current->offset)) % rb_size;
1613 if (file_offset + read_length > rb_size) {
1614 block_length = rb_size - file_offset;
1616 block_length = read_length;
1620 block_length = read_length;
1623 /* while we still have data to read, we loop */
1624 while (read_length > 0) {
1628 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1629 data, &read_return);
1630 if (ret != GST_FLOW_OK)
1633 file_offset += read_return;
1634 if (QUEUE_IS_USING_RING_BUFFER (queue))
1635 file_offset %= rb_size;
1637 data += read_return;
1638 read_length -= read_return;
1639 block_length = read_length;
1640 remaining -= read_return;
1642 rpos = (queue->current->reading_pos += read_return);
1643 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1645 GST_QUEUE2_SIGNAL_DEL (queue);
1646 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1649 gst_buffer_unmap (buf, &info);
1650 gst_buffer_resize (buf, 0, length);
1652 GST_BUFFER_OFFSET (buf) = offset;
1653 GST_BUFFER_OFFSET_END (buf) = offset + length;
1662 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1663 gst_buffer_unmap (buf, &info);
1664 if (*buffer == NULL)
1665 gst_buffer_unref (buf);
1666 return GST_FLOW_EOS;
1670 GST_DEBUG_OBJECT (queue, "we are flushing");
1671 gst_buffer_unmap (buf, &info);
1672 if (*buffer == NULL)
1673 gst_buffer_unref (buf);
1674 return GST_FLOW_FLUSHING;
1678 GST_DEBUG_OBJECT (queue, "we have a read error");
1679 gst_buffer_unmap (buf, &info);
1680 if (*buffer == NULL)
1681 gst_buffer_unref (buf);
1686 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE, (NULL),
1687 ("Can't write to buffer"));
1688 if (*buffer == NULL)
1689 gst_buffer_unref (buf);
1690 return GST_FLOW_ERROR;
1694 /* should be called with QUEUE_LOCK */
1695 static GstMiniObject *
1696 gst_queue2_read_item_from_file (GstQueue2 * queue)
1698 GstMiniObject *item;
1700 if (queue->stream_start_event != NULL) {
1701 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1702 queue->stream_start_event = NULL;
1703 } else if (queue->starting_segment != NULL) {
1704 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1705 queue->starting_segment = NULL;
1708 GstBuffer *buffer = NULL;
1709 guint64 reading_pos;
1711 reading_pos = queue->current->reading_pos;
1714 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1719 item = GST_MINI_OBJECT_CAST (buffer);
1722 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1732 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1733 * the temp filename. */
1735 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1740 if (queue->temp_file)
1741 goto already_opened;
1743 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1745 /* If temp_template was set, allocate a filename and open that file */
1748 if (queue->temp_template == NULL)
1751 /* make copy of the template, we don't want to change this */
1752 name = g_strdup (queue->temp_template);
1755 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1757 fd = g_mkstemp (name);
1761 goto mkstemp_failed;
1763 /* open the file for update/writing */
1764 queue->temp_file = fdopen (fd, "wb+");
1765 /* error creating file */
1766 if (queue->temp_file == NULL)
1769 g_free (queue->temp_location);
1770 queue->temp_location = name;
1772 GST_QUEUE2_MUTEX_UNLOCK (queue);
1774 /* we can't emit the notify with the lock */
1775 g_object_notify (G_OBJECT (queue), "temp-location");
1777 GST_QUEUE2_MUTEX_LOCK (queue);
1779 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1786 GST_DEBUG_OBJECT (queue, "temp file was already open");
1791 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1792 (_("No Temp directory specified.")), (NULL));
1797 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1798 (_("Could not create temp file \"%s\"."), queue->temp_template),
1805 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1806 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1815 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1818 if (queue->temp_file == NULL)
1821 GST_DEBUG_OBJECT (queue, "closing temp file");
1823 fflush (queue->temp_file);
1824 fclose (queue->temp_file);
1826 if (queue->temp_remove) {
1827 if (remove (queue->temp_location) < 0) {
1828 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1829 queue->temp_location, g_strerror (errno));
1833 queue->temp_file = NULL;
1834 clean_ranges (queue);
1838 gst_queue2_flush_temp_file (GstQueue2 * queue)
1840 if (queue->temp_file == NULL)
1843 GST_DEBUG_OBJECT (queue, "flushing temp file");
1845 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1849 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1851 if (!QUEUE_IS_USING_QUEUE (queue)) {
1852 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1853 gst_queue2_flush_temp_file (queue);
1854 init_ranges (queue);
1856 GstQueue2Item *qitem;
1858 while ((qitem = gst_queue_array_pop_head_struct (queue->queue))) {
1859 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1860 && GST_EVENT_IS_STICKY (qitem->item)
1861 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1862 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1863 gst_pad_store_sticky_event (queue->srcpad,
1864 GST_EVENT_CAST (qitem->item));
1867 /* Then lose another reference because we are supposed to destroy that
1868 data when flushing */
1869 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1870 gst_mini_object_unref (qitem->item);
1873 queue->last_query = FALSE;
1874 g_cond_signal (&queue->query_handled);
1875 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1876 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1877 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1878 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1879 queue->sink_tainted = queue->src_tainted = TRUE;
1880 if (queue->starting_segment != NULL)
1881 gst_event_unref (queue->starting_segment);
1882 queue->starting_segment = NULL;
1883 queue->segment_event_received = FALSE;
1884 gst_event_replace (&queue->stream_start_event, NULL);
1886 /* we deleted a lot of something */
1887 GST_QUEUE2_SIGNAL_DEL (queue);
1891 gst_queue2_wait_free_space (GstQueue2 * queue)
1893 /* We make space available if we're "full" according to whatever
1894 * the user defined as "full". */
1895 if (gst_queue2_is_filled (queue)) {
1898 /* pause the timer while we wait. The fact that we are waiting does not mean
1899 * the byterate on the input pad is lower */
1900 if ((started = queue->in_timer_started))
1901 g_timer_stop (queue->in_timer);
1903 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1904 "queue is full, waiting for free space");
1906 /* Wait for space to be available, we could be unlocked because of a flush. */
1907 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1909 while (gst_queue2_is_filled (queue));
1911 /* and continue if we were running before */
1913 g_timer_continue (queue->in_timer);
1920 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1926 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1929 guint8 *data, *ring_buffer;
1930 guint size, rb_size;
1931 guint64 writing_pos, new_writing_pos;
1932 GstQueue2Range *range, *prev, *next;
1933 gboolean do_seek = FALSE;
1935 if (QUEUE_IS_USING_RING_BUFFER (queue))
1936 writing_pos = queue->current->rb_writing_pos;
1938 writing_pos = queue->current->writing_pos;
1939 ring_buffer = queue->ring_buffer;
1940 rb_size = queue->ring_buffer_max_size;
1942 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
1943 goto buffer_read_error;
1948 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1952 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1953 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1954 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1955 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1956 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1962 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1965 /* calculate the space in the ring buffer not used by data from
1966 * the current range */
1967 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1968 /* wait until there is some free space */
1969 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1971 /* get the amount of space we have */
1972 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1974 /* calculate if we need to split or if we can write the entire
1976 to_write = MIN (size, space);
1978 /* the writing position in the ring buffer after writing (part
1979 * or all of) the buffer */
1980 new_writing_pos = (writing_pos + to_write) % rb_size;
1983 range = queue->ranges;
1985 /* if we need to overwrite data in the ring buffer, we need to
1988 * warning: this code is complicated and includes some
1989 * simplifications - pen, paper and diagrams for the cases
1992 guint64 range_data_start, range_data_end;
1993 GstQueue2Range *range_to_destroy = NULL;
1995 if (range == queue->current)
1998 range_data_start = range->rb_offset;
1999 range_data_end = range->rb_writing_pos;
2001 /* handle the special case where the range has no data in it */
2002 if (range->writing_pos == range->offset) {
2003 if (range != queue->current) {
2004 GST_DEBUG_OBJECT (queue,
2005 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
2006 G_GUINT64_FORMAT, range->offset, range->writing_pos);
2008 range_to_destroy = range;
2010 prev->next = range->next;
2015 if (range_data_end > range_data_start) {
2016 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
2019 if (new_writing_pos > range_data_start) {
2020 if (new_writing_pos >= range_data_end) {
2021 GST_DEBUG_OBJECT (queue,
2022 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
2023 G_GUINT64_FORMAT, range->offset, range->writing_pos);
2025 range_to_destroy = range;
2027 prev->next = range->next;
2029 GST_DEBUG_OBJECT (queue,
2030 "advancing offsets from %" G_GUINT64_FORMAT " (%"
2031 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
2032 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
2033 range->offset + new_writing_pos - range_data_start,
2035 range->offset += (new_writing_pos - range_data_start);
2036 range->rb_offset = new_writing_pos;
2040 guint64 new_wpos_virt = writing_pos + to_write;
2042 if (new_wpos_virt <= range_data_start)
2045 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
2046 GST_DEBUG_OBJECT (queue,
2047 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
2048 G_GUINT64_FORMAT, range->offset, range->writing_pos);
2050 range_to_destroy = range;
2052 prev->next = range->next;
2054 GST_DEBUG_OBJECT (queue,
2055 "advancing offsets from %" G_GUINT64_FORMAT " (%"
2056 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
2057 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
2058 range->offset + new_writing_pos - range_data_start,
2060 range->offset += (new_wpos_virt - range_data_start);
2061 range->rb_offset = new_writing_pos;
2066 if (!range_to_destroy)
2069 range = range->next;
2070 if (range_to_destroy) {
2071 if (range_to_destroy == queue->ranges)
2072 queue->ranges = range;
2073 g_slice_free (GstQueue2Range, range_to_destroy);
2074 range_to_destroy = NULL;
2079 new_writing_pos = writing_pos + to_write;
2082 if (QUEUE_IS_USING_TEMP_FILE (queue)
2083 && FSEEK_FILE (queue->temp_file, writing_pos))
2086 if (new_writing_pos > writing_pos) {
2087 GST_INFO_OBJECT (queue,
2088 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2089 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
2090 queue->current->writing_pos, queue->current->rb_writing_pos);
2091 /* either not using ring buffer or no wrapping, just write */
2092 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2093 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
2096 memcpy (ring_buffer + writing_pos, data, to_write);
2099 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
2100 /* try to merge with next range */
2101 while ((next = queue->current->next)) {
2102 GST_INFO_OBJECT (queue,
2103 "checking merge with next range %" G_GUINT64_FORMAT " < %"
2104 G_GUINT64_FORMAT, new_writing_pos, next->offset);
2105 if (new_writing_pos < next->offset)
2108 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
2111 /* remove the group */
2112 queue->current->next = next->next;
2114 /* We use the threshold to decide if we want to do a seek or simply
2115 * read the data again. If there is not so much data in the range we
2116 * prefer to avoid to seek and read it again. */
2117 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
2118 /* the new range had more data than the threshold, it's worth keeping
2119 * it and doing a seek. */
2120 new_writing_pos = next->writing_pos;
2123 g_slice_free (GstQueue2Range, next);
2125 goto update_and_signal;
2129 guint block_one, block_two;
2131 block_one = rb_size - writing_pos;
2132 block_two = to_write - block_one;
2134 if (block_one > 0) {
2135 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
2136 /* write data to end of ring buffer */
2137 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2138 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
2141 memcpy (ring_buffer + writing_pos, data, block_one);
2145 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
2148 if (block_two > 0) {
2149 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
2150 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2151 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
2154 memcpy (ring_buffer, data + block_one, block_two);
2160 /* update the writing positions */
2162 GST_INFO_OBJECT (queue,
2163 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2164 to_write, writing_pos, size);
2166 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2168 queue->current->writing_pos += to_write;
2169 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2171 queue->current->writing_pos = writing_pos = new_writing_pos;
2174 perform_seek_to_offset (queue, new_writing_pos);
2176 update_cur_level (queue, queue->current);
2178 /* update the buffering status */
2179 if (queue->use_buffering) {
2181 update_buffering (queue);
2182 msg = gst_queue2_get_buffering_message (queue);
2184 GST_QUEUE2_MUTEX_UNLOCK (queue);
2185 g_mutex_lock (&queue->buffering_post_lock);
2186 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
2187 g_mutex_unlock (&queue->buffering_post_lock);
2188 GST_QUEUE2_MUTEX_LOCK (queue);
2192 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2193 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2195 GST_QUEUE2_SIGNAL_ADD (queue);
2198 gst_buffer_unmap (buffer, &info);
2205 GST_DEBUG_OBJECT (queue, "we are flushing");
2206 gst_buffer_unmap (buffer, &info);
2207 /* FIXME - GST_FLOW_EOS ? */
2212 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2213 gst_buffer_unmap (buffer, &info);
2220 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2224 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2225 (_("Error while writing to download file.")),
2226 ("%s", g_strerror (errno)));
2229 gst_buffer_unmap (buffer, &info);
2234 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL),
2235 ("Can't read from buffer"));
2241 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2243 GstQueue2 *queue = q;
2245 GST_TRACE_OBJECT (queue,
2246 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2247 gst_buffer_get_size (*buf));
2249 if (!gst_queue2_create_write (queue, *buf)) {
2250 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2256 /* enqueue an item an update the level stats */
2258 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2259 GstQueue2ItemType item_type)
2261 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2265 buffer = GST_BUFFER_CAST (item);
2266 size = gst_buffer_get_size (buffer);
2268 /* add buffer to the statistics */
2269 if (QUEUE_IS_USING_QUEUE (queue)) {
2270 queue->cur_level.buffers++;
2271 queue->cur_level.bytes += size;
2273 queue->bytes_in += size;
2275 /* apply new buffer to segment stats */
2276 apply_buffer (queue, buffer, &queue->sink_segment, size, TRUE);
2277 /* update the byterate stats */
2278 update_in_rates (queue, FALSE);
2280 if (!QUEUE_IS_USING_QUEUE (queue)) {
2281 /* FIXME - check return value? */
2282 gst_queue2_create_write (queue, buffer);
2284 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2285 GstBufferList *buffer_list;
2288 buffer_list = GST_BUFFER_LIST_CAST (item);
2290 size = gst_buffer_list_calculate_size (buffer_list);
2291 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2293 /* add buffer to the statistics */
2294 if (QUEUE_IS_USING_QUEUE (queue)) {
2295 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2296 queue->cur_level.bytes += size;
2298 queue->bytes_in += size;
2300 /* apply new buffer to segment stats */
2301 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2303 /* update the byterate stats */
2304 update_in_rates (queue, FALSE);
2306 if (!QUEUE_IS_USING_QUEUE (queue)) {
2307 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2309 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2312 event = GST_EVENT_CAST (item);
2314 switch (GST_EVENT_TYPE (event)) {
2316 /* Zero the thresholds, this makes sure the queue is completely
2317 * filled and we can read all data from the queue. */
2318 GST_DEBUG_OBJECT (queue, "we have EOS");
2319 queue->is_eos = TRUE;
2320 /* Force updating the input bitrate */
2321 update_in_rates (queue, TRUE);
2323 case GST_EVENT_SEGMENT:
2324 apply_segment (queue, event, &queue->sink_segment, TRUE);
2325 /* This is our first new segment, we hold it
2326 * as we can't save it on the temp file */
2327 if (!QUEUE_IS_USING_QUEUE (queue)) {
2328 if (queue->segment_event_received)
2329 goto unexpected_event;
2331 queue->segment_event_received = TRUE;
2332 if (queue->starting_segment != NULL)
2333 gst_event_unref (queue->starting_segment);
2334 queue->starting_segment = event;
2337 /* a new segment allows us to accept more buffers if we got EOS
2338 * from downstream */
2339 queue->unexpected = FALSE;
2342 apply_gap (queue, event, &queue->sink_segment, TRUE);
2344 case GST_EVENT_STREAM_START:
2345 if (!QUEUE_IS_USING_QUEUE (queue)) {
2346 gst_event_replace (&queue->stream_start_event, event);
2347 gst_event_unref (event);
2351 case GST_EVENT_CAPS:{
2354 gst_event_parse_caps (event, &caps);
2355 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2357 if (!QUEUE_IS_USING_QUEUE (queue)) {
2358 GST_LOG ("Dropping caps event, not using queue");
2359 gst_event_unref (event);
2365 if (!QUEUE_IS_USING_QUEUE (queue))
2366 goto unexpected_event;
2369 } else if (GST_IS_QUERY (item)) {
2370 /* Can't happen as we check that in the caller */
2371 if (!QUEUE_IS_USING_QUEUE (queue))
2372 g_assert_not_reached ();
2374 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2375 item, GST_OBJECT_NAME (queue));
2376 /* we can't really unref since we don't know what it is */
2381 /* update the buffering status */
2382 if (queue->use_buffering)
2383 update_buffering (queue);
2385 if (QUEUE_IS_USING_QUEUE (queue)) {
2386 GstQueue2Item qitem;
2388 qitem.type = item_type;
2390 gst_queue_array_push_tail_struct (queue->queue, &qitem);
2392 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2395 GST_QUEUE2_SIGNAL_ADD (queue);
2403 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2405 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2406 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2407 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2408 gst_event_unref (GST_EVENT_CAST (item));
2413 /* dequeue an item from the queue and update level stats */
2414 static GstMiniObject *
2415 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2417 GstMiniObject *item;
2419 if (!QUEUE_IS_USING_QUEUE (queue)) {
2420 item = gst_queue2_read_item_from_file (queue);
2422 GstQueue2Item *qitem = gst_queue_array_pop_head_struct (queue->queue);
2433 if (GST_IS_BUFFER (item)) {
2437 buffer = GST_BUFFER_CAST (item);
2438 size = gst_buffer_get_size (buffer);
2439 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2441 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2442 "retrieved buffer %p from queue", buffer);
2444 if (QUEUE_IS_USING_QUEUE (queue)) {
2445 queue->cur_level.buffers--;
2446 queue->cur_level.bytes -= size;
2448 queue->bytes_out += size;
2450 apply_buffer (queue, buffer, &queue->src_segment, size, FALSE);
2451 /* update the byterate stats */
2452 update_out_rates (queue);
2453 /* update the buffering */
2454 if (queue->use_buffering)
2455 update_buffering (queue);
2457 } else if (GST_IS_EVENT (item)) {
2458 GstEvent *event = GST_EVENT_CAST (item);
2460 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2462 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2463 "retrieved event %p from queue", event);
2465 switch (GST_EVENT_TYPE (event)) {
2467 /* queue is empty now that we dequeued the EOS */
2468 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2470 case GST_EVENT_SEGMENT:
2471 apply_segment (queue, event, &queue->src_segment, FALSE);
2474 apply_gap (queue, event, &queue->src_segment, FALSE);
2479 } else if (GST_IS_BUFFER_LIST (item)) {
2480 GstBufferList *buffer_list;
2483 buffer_list = GST_BUFFER_LIST_CAST (item);
2484 size = gst_buffer_list_calculate_size (buffer_list);
2485 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2487 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2488 "retrieved buffer list %p from queue", buffer_list);
2490 if (QUEUE_IS_USING_QUEUE (queue)) {
2491 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2492 queue->cur_level.bytes -= size;
2494 queue->bytes_out += size;
2496 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2497 /* update the byterate stats */
2498 update_out_rates (queue);
2499 /* update the buffering */
2500 if (queue->use_buffering)
2501 update_buffering (queue);
2502 } else if (GST_IS_QUERY (item)) {
2503 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2504 "retrieved query %p from queue", item);
2505 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2508 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2509 item, GST_OBJECT_NAME (queue));
2511 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2513 GST_QUEUE2_SIGNAL_DEL (queue);
2520 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2525 static GstFlowReturn
2526 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2529 gboolean ret = TRUE;
2532 queue = GST_QUEUE2 (parent);
2534 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Received event '%s'",
2535 GST_EVENT_TYPE_NAME (event));
2537 switch (GST_EVENT_TYPE (event)) {
2538 case GST_EVENT_FLUSH_START:
2540 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2542 ret = gst_pad_push_event (queue->srcpad, event);
2544 /* now unblock the chain function */
2545 GST_QUEUE2_MUTEX_LOCK (queue);
2546 queue->srcresult = GST_FLOW_FLUSHING;
2547 queue->sinkresult = GST_FLOW_FLUSHING;
2548 /* unblock the loop and chain functions */
2549 GST_QUEUE2_SIGNAL_ADD (queue);
2550 GST_QUEUE2_SIGNAL_DEL (queue);
2551 GST_QUEUE2_MUTEX_UNLOCK (queue);
2553 /* make sure it pauses, this should happen since we sent
2554 * flush_start downstream. */
2555 gst_pad_pause_task (queue->srcpad);
2556 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2558 GST_QUEUE2_MUTEX_LOCK (queue);
2559 queue->last_query = FALSE;
2560 g_cond_signal (&queue->query_handled);
2561 GST_QUEUE2_MUTEX_UNLOCK (queue);
2563 GST_QUEUE2_MUTEX_LOCK (queue);
2564 /* flush the sink pad */
2565 queue->sinkresult = GST_FLOW_FLUSHING;
2566 GST_QUEUE2_SIGNAL_DEL (queue);
2567 queue->last_query = FALSE;
2568 g_cond_signal (&queue->query_handled);
2569 GST_QUEUE2_MUTEX_UNLOCK (queue);
2571 gst_event_unref (event);
2575 case GST_EVENT_FLUSH_STOP:
2577 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2579 ret = gst_pad_push_event (queue->srcpad, event);
2581 GST_QUEUE2_MUTEX_LOCK (queue);
2582 gst_queue2_locked_flush (queue, FALSE, TRUE);
2583 queue->srcresult = GST_FLOW_OK;
2584 queue->sinkresult = GST_FLOW_OK;
2585 queue->is_eos = FALSE;
2586 queue->unexpected = FALSE;
2587 queue->seeking = FALSE;
2588 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2589 /* reset rate counters */
2590 reset_rate_timer (queue);
2591 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2592 queue->srcpad, NULL);
2593 GST_QUEUE2_MUTEX_UNLOCK (queue);
2595 GST_QUEUE2_MUTEX_LOCK (queue);
2596 queue->segment_event_received = FALSE;
2597 queue->is_eos = FALSE;
2598 queue->unexpected = FALSE;
2599 queue->sinkresult = GST_FLOW_OK;
2600 queue->seeking = FALSE;
2601 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2602 GST_QUEUE2_MUTEX_UNLOCK (queue);
2604 gst_event_unref (event);
2606 g_object_notify (G_OBJECT (queue), "bitrate");
2609 case GST_EVENT_TAG:{
2610 if (queue->use_tags_bitrate) {
2614 gst_event_parse_tag (event, &tags);
2615 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2616 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2617 GST_QUEUE2_MUTEX_LOCK (queue);
2618 queue->sink_tags_bitrate = bitrate;
2619 GST_QUEUE2_MUTEX_UNLOCK (queue);
2620 GST_LOG_OBJECT (queue, "Sink pad bitrate from tags now %u", bitrate);
2621 g_object_notify (G_OBJECT (queue), "bitrate");
2627 if (GST_EVENT_IS_SERIALIZED (event)) {
2628 gboolean bitrate_changed = TRUE;
2629 /* serialized events go in the queue */
2631 /* STREAM_START and SEGMENT reset the EOS status of a
2632 * pad. Change the cached sinkpad flow result accordingly */
2633 if (queue->sinkresult == GST_FLOW_EOS
2634 && (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START
2635 || GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT))
2636 queue->sinkresult = GST_FLOW_OK;
2638 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2639 if (queue->srcresult != GST_FLOW_OK) {
2640 /* Errors in sticky event pushing are no problem and ignored here
2641 * as they will cause more meaningful errors during data flow.
2642 * For EOS events, that are not followed by data flow, we still
2643 * return FALSE here though and report an error.
2645 if (!GST_EVENT_IS_STICKY (event)) {
2646 goto out_flow_error;
2647 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2648 if (queue->srcresult == GST_FLOW_NOT_LINKED
2649 || queue->srcresult < GST_FLOW_EOS) {
2650 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
2652 goto out_flow_error;
2656 /* refuse more events on EOS unless they unset the EOS status */
2657 if (queue->is_eos) {
2658 switch (GST_EVENT_TYPE (event)) {
2659 case GST_EVENT_STREAM_START:
2660 case GST_EVENT_SEGMENT:
2661 /* Restart the loop */
2662 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2663 queue->srcresult = GST_FLOW_OK;
2664 queue->is_eos = FALSE;
2665 queue->unexpected = FALSE;
2666 queue->seeking = FALSE;
2667 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2668 /* reset rate counters */
2669 reset_rate_timer (queue);
2670 gst_pad_start_task (queue->srcpad,
2671 (GstTaskFunction) gst_queue2_loop, queue->srcpad, NULL);
2673 queue->is_eos = FALSE;
2674 queue->unexpected = FALSE;
2675 queue->seeking = FALSE;
2676 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2678 bitrate_changed = TRUE;
2686 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2687 GST_QUEUE2_MUTEX_UNLOCK (queue);
2688 gst_queue2_post_buffering (queue);
2689 if (bitrate_changed)
2690 g_object_notify (G_OBJECT (queue), "bitrate");
2692 /* non-serialized events are passed downstream. */
2693 ret = gst_pad_push_event (queue->srcpad, event);
2698 return GST_FLOW_ERROR;
2704 GstFlowReturn ret = queue->sinkresult;
2705 GST_DEBUG_OBJECT (queue, "refusing event, we are %s",
2706 gst_flow_get_name (ret));
2707 GST_QUEUE2_MUTEX_UNLOCK (queue);
2708 gst_event_unref (event);
2713 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2714 GST_QUEUE2_MUTEX_UNLOCK (queue);
2715 gst_event_unref (event);
2716 return GST_FLOW_EOS;
2720 GST_LOG_OBJECT (queue,
2721 "refusing event, we have a downstream flow error: %s",
2722 gst_flow_get_name (queue->srcresult));
2723 GST_QUEUE2_MUTEX_UNLOCK (queue);
2724 gst_event_unref (event);
2725 return queue->srcresult;
2730 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2736 queue = GST_QUEUE2 (parent);
2738 switch (GST_QUERY_TYPE (query)) {
2740 if (GST_QUERY_IS_SERIALIZED (query)) {
2741 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2742 /* serialized events go in the queue. We need to be certain that we
2743 * don't cause deadlocks waiting for the query return value. We check if
2744 * the queue is empty (nothing is blocking downstream and the query can
2745 * be pushed for sure) or we are not buffering. If we are buffering,
2746 * the pipeline waits to unblock downstream until our queue fills up
2747 * completely, which can not happen if we block on the query..
2748 * Therefore we only potentially block when we are not buffering. */
2749 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2750 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2751 || !queue->use_buffering)) {
2752 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2753 gst_queue2_locked_enqueue (queue, query,
2754 GST_QUEUE2_ITEM_TYPE_QUERY);
2756 STATUS (queue, queue->sinkpad, "wait for QUERY");
2757 while (queue->sinkresult == GST_FLOW_OK &&
2758 queue->last_handled_query != query)
2759 g_cond_wait (&queue->query_handled, &queue->qlock);
2760 queue->last_handled_query = NULL;
2761 if (queue->sinkresult != GST_FLOW_OK)
2763 res = queue->last_query;
2765 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2769 GST_DEBUG_OBJECT (queue,
2770 "refusing query, we are not using the queue");
2773 GST_QUEUE2_MUTEX_UNLOCK (queue);
2774 gst_queue2_post_buffering (queue);
2776 res = gst_pad_query_default (pad, parent, query);
2785 GST_DEBUG_OBJECT (queue, "refusing query, we are %s",
2786 gst_flow_get_name (queue->sinkresult));
2787 GST_QUEUE2_MUTEX_UNLOCK (queue);
2793 gst_queue2_is_empty (GstQueue2 * queue)
2795 /* never empty on EOS */
2799 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2800 return queue->current->writing_pos <= queue->current->max_reading_pos;
2802 if (gst_queue_array_get_length (queue->queue) == 0)
2810 gst_queue2_is_filled (GstQueue2 * queue)
2814 /* always filled on EOS */
2818 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2819 (queue->cur_level.format) >= ((alt_max) ? \
2820 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2822 /* if using a ring buffer we're filled if all ring buffer space is used
2823 * _by the current range_ */
2824 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2825 guint64 rb_size = queue->ring_buffer_max_size;
2826 GST_DEBUG_OBJECT (queue,
2827 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2828 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2829 return CHECK_FILLED (bytes, rb_size);
2832 /* if using file, we're never filled if we don't have EOS */
2833 if (QUEUE_IS_USING_TEMP_FILE (queue))
2836 /* we are never filled when we have no buffers at all */
2837 if (queue->cur_level.buffers == 0)
2840 /* we are filled if one of the current levels exceeds the max */
2841 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2842 || CHECK_FILLED (time, 0);
2844 /* if we need to, use the rate estimate to check against the max time we are
2845 * allowed to queue */
2846 if (queue->use_rate_estimate)
2847 res |= CHECK_FILLED (rate_time, 0);
2853 static GstFlowReturn
2854 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2855 GstMiniObject * item, GstQueue2ItemType item_type)
2857 /* we have to lock the queue since we span threads */
2858 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2859 /* when we received EOS, we refuse more data */
2862 /* when we received unexpected from downstream, refuse more buffers */
2863 if (queue->unexpected)
2864 goto out_unexpected;
2866 /* while we didn't receive the newsegment, we're seeking and we skip data */
2870 if (!gst_queue2_wait_free_space (queue))
2873 /* put buffer in queue now */
2874 gst_queue2_locked_enqueue (queue, item, item_type);
2875 GST_QUEUE2_MUTEX_UNLOCK (queue);
2876 gst_queue2_post_buffering (queue);
2880 /* special conditions */
2883 GstFlowReturn ret = queue->sinkresult;
2885 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2886 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2887 GST_QUEUE2_MUTEX_UNLOCK (queue);
2888 gst_mini_object_unref (item);
2894 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2895 GST_QUEUE2_MUTEX_UNLOCK (queue);
2896 gst_mini_object_unref (item);
2898 return GST_FLOW_EOS;
2902 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2903 GST_QUEUE2_MUTEX_UNLOCK (queue);
2904 gst_mini_object_unref (item);
2910 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2911 GST_QUEUE2_MUTEX_UNLOCK (queue);
2912 gst_mini_object_unref (item);
2914 return GST_FLOW_EOS;
2918 static GstFlowReturn
2919 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2923 queue = GST_QUEUE2 (parent);
2925 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2926 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2927 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2928 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2929 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2931 return gst_queue2_chain_buffer_or_buffer_list (queue,
2932 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2935 static GstFlowReturn
2936 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2937 GstBufferList * buffer_list)
2941 queue = GST_QUEUE2 (parent);
2943 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2944 "received buffer list %p", buffer_list);
2946 return gst_queue2_chain_buffer_or_buffer_list (queue,
2947 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2950 static GstMiniObject *
2951 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2953 GstMiniObject *data;
2955 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2957 /* stop pushing buffers, we dequeue all items until we see an item that we
2958 * can push again, which is EOS or SEGMENT. If there is nothing in the
2959 * queue we can push, we set a flag to make the sinkpad refuse more
2960 * buffers with an EOS return value until we receive something
2961 * pushable again or we get flushed. */
2962 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2963 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2964 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2965 "dropping EOS buffer %p", data);
2966 gst_buffer_unref (GST_BUFFER_CAST (data));
2967 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2968 GstEvent *event = GST_EVENT_CAST (data);
2969 GstEventType type = GST_EVENT_TYPE (event);
2971 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT
2972 || type == GST_EVENT_STREAM_START) {
2973 /* we found a pushable item in the queue, push it out */
2974 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2975 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2978 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2979 "dropping EOS event %p", event);
2980 gst_event_unref (event);
2981 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2982 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2983 "dropping EOS buffer list %p", data);
2984 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2985 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2986 queue->last_query = FALSE;
2987 g_cond_signal (&queue->query_handled);
2988 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2991 /* no more items in the queue. Set the unexpected flag so that upstream
2992 * make us refuse any more buffers on the sinkpad. Since we will still
2993 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2994 * task function does not shut down. */
2995 queue->unexpected = TRUE;
2999 /* dequeue an item from the queue an push it downstream. This functions returns
3000 * the result of the push. */
3001 static GstFlowReturn
3002 gst_queue2_push_one (GstQueue2 * queue)
3004 GstFlowReturn result;
3005 GstMiniObject *data;
3006 GstQueue2ItemType item_type;
3008 data = gst_queue2_locked_dequeue (queue, &item_type);
3013 result = queue->srcresult;
3014 STATUS (queue, queue->srcpad, "We have something dequeud");
3015 g_atomic_int_set (&queue->downstream_may_block,
3016 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
3017 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
3018 GST_QUEUE2_MUTEX_UNLOCK (queue);
3019 gst_queue2_post_buffering (queue);
3021 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
3024 buffer = GST_BUFFER_CAST (data);
3026 result = gst_pad_push (queue->srcpad, buffer);
3027 g_atomic_int_set (&queue->downstream_may_block, 0);
3029 /* need to check for srcresult here as well */
3030 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3031 if (result == GST_FLOW_EOS) {
3032 data = gst_queue2_dequeue_on_eos (queue, &item_type);
3035 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
3036 * to the caller so that the task function does not shut down */
3037 result = GST_FLOW_OK;
3039 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
3040 GstEvent *event = GST_EVENT_CAST (data);
3041 GstEventType type = GST_EVENT_TYPE (event);
3043 if (type == GST_EVENT_TAG) {
3044 if (queue->use_tags_bitrate) {
3048 gst_event_parse_tag (event, &tags);
3049 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
3050 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
3051 GST_QUEUE2_MUTEX_LOCK (queue);
3052 queue->src_tags_bitrate = bitrate;
3053 GST_QUEUE2_MUTEX_UNLOCK (queue);
3054 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
3055 g_object_notify (G_OBJECT (queue), "bitrate");
3060 gst_pad_push_event (queue->srcpad, event);
3062 /* if we're EOS, return EOS so that the task pauses. */
3063 if (type == GST_EVENT_EOS) {
3064 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3065 "pushed EOS event %p, return EOS", event);
3066 result = GST_FLOW_EOS;
3069 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3070 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
3071 GstBufferList *buffer_list;
3073 buffer_list = GST_BUFFER_LIST_CAST (data);
3075 result = gst_pad_push_list (queue->srcpad, buffer_list);
3076 g_atomic_int_set (&queue->downstream_may_block, 0);
3078 /* need to check for srcresult here as well */
3079 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3080 if (result == GST_FLOW_EOS) {
3081 data = gst_queue2_dequeue_on_eos (queue, &item_type);
3084 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
3085 * to the caller so that the task function does not shut down */
3086 result = GST_FLOW_OK;
3088 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
3089 GstQuery *query = GST_QUERY_CAST (data);
3091 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
3092 queue->last_handled_query = query;
3093 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
3094 GST_LOG_OBJECT (queue->srcpad, "Peered query");
3095 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3096 "did query %p, return %d", query, queue->last_query);
3097 g_cond_signal (&queue->query_handled);
3098 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3099 result = GST_FLOW_OK;
3106 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3107 "exit because we have no item in the queue");
3108 return GST_FLOW_ERROR;
3112 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are %s",
3113 gst_flow_get_name (queue->srcresult));
3114 return queue->srcresult;
3118 /* called repeatedly with @pad as the source pad. This function should push out
3119 * data to the peer element. */
3121 gst_queue2_loop (GstPad * pad)
3126 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
3128 /* have to lock for thread-safety */
3129 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3131 if (gst_queue2_is_empty (queue)) {
3134 /* pause the timer while we wait. The fact that we are waiting does not mean
3135 * the byterate on the output pad is lower */
3136 if ((started = queue->out_timer_started))
3137 g_timer_stop (queue->out_timer);
3139 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
3140 "queue is empty, waiting for new data");
3142 /* Wait for data to be available, we could be unlocked because of a flush. */
3143 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
3145 while (gst_queue2_is_empty (queue));
3147 /* and continue if we were running before */
3149 g_timer_continue (queue->out_timer);
3151 ret = gst_queue2_push_one (queue);
3152 queue->srcresult = ret;
3153 queue->sinkresult = ret;
3154 if (ret != GST_FLOW_OK)
3157 GST_QUEUE2_MUTEX_UNLOCK (queue);
3158 gst_queue2_post_buffering (queue);
3165 gboolean eos = queue->is_eos;
3166 GstFlowReturn ret = queue->srcresult;
3168 gst_pad_pause_task (queue->srcpad);
3169 if (ret == GST_FLOW_FLUSHING) {
3170 gst_queue2_locked_flush (queue, FALSE, FALSE);
3172 GST_QUEUE2_SIGNAL_DEL (queue);
3173 queue->last_query = FALSE;
3174 g_cond_signal (&queue->query_handled);
3176 GST_QUEUE2_MUTEX_UNLOCK (queue);
3177 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3178 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
3179 /* Recalculate buffering levels before stopping since the source flow
3180 * might cause a different buffering level (like NOT_LINKED making
3181 * the queue appear as full) */
3182 if (queue->use_buffering)
3183 update_buffering (queue);
3184 gst_queue2_post_buffering (queue);
3185 /* let app know about us giving up if upstream is not expected to do so */
3186 /* EOS is already taken care of elsewhere */
3187 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
3188 GST_ELEMENT_FLOW_ERROR (queue, ret);
3189 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
3196 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
3198 gboolean res = TRUE;
3199 GstQueue2 *queue = GST_QUEUE2 (parent);
3201 #ifndef GST_DISABLE_GST_DEBUG
3202 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
3203 event, GST_EVENT_TYPE_NAME (event));
3206 switch (GST_EVENT_TYPE (event)) {
3207 case GST_EVENT_FLUSH_START:
3208 if (QUEUE_IS_USING_QUEUE (queue)) {
3209 /* just forward upstream */
3210 res = gst_pad_push_event (queue->sinkpad, event);
3212 /* now unblock the getrange function */
3213 GST_QUEUE2_MUTEX_LOCK (queue);
3214 GST_DEBUG_OBJECT (queue, "flushing");
3215 queue->srcresult = GST_FLOW_FLUSHING;
3216 GST_QUEUE2_SIGNAL_ADD (queue);
3217 GST_QUEUE2_MUTEX_UNLOCK (queue);
3219 /* when using a temp file, we eat the event */
3221 gst_event_unref (event);
3224 case GST_EVENT_FLUSH_STOP:
3225 if (QUEUE_IS_USING_QUEUE (queue)) {
3226 /* just forward upstream */
3227 res = gst_pad_push_event (queue->sinkpad, event);
3229 /* now unblock the getrange function */
3230 GST_QUEUE2_MUTEX_LOCK (queue);
3231 queue->srcresult = GST_FLOW_OK;
3232 GST_QUEUE2_MUTEX_UNLOCK (queue);
3234 /* when using a temp file, we eat the event */
3236 gst_event_unref (event);
3239 case GST_EVENT_RECONFIGURE:
3240 GST_QUEUE2_MUTEX_LOCK (queue);
3241 /* assume downstream is linked now and try to push again */
3242 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3243 queue->srcresult = GST_FLOW_OK;
3244 queue->sinkresult = GST_FLOW_OK;
3245 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3246 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3251 GST_QUEUE2_MUTEX_UNLOCK (queue);
3253 /* force a new bitrate query to be performed */
3254 query_downstream_bitrate (queue);
3256 res = gst_pad_push_event (queue->sinkpad, event);
3259 res = gst_pad_push_event (queue->sinkpad, event);
3267 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3271 queue = GST_QUEUE2 (parent);
3273 switch (GST_QUERY_TYPE (query)) {
3274 case GST_QUERY_POSITION:
3279 if (!gst_pad_peer_query (queue->sinkpad, query))
3282 /* get peer position */
3283 gst_query_parse_position (query, &format, &peer_pos);
3285 /* FIXME: this code assumes that there's no discont in the queue */
3287 case GST_FORMAT_BYTES:
3288 peer_pos -= queue->cur_level.bytes;
3289 if (peer_pos < 0) /* Clamp result to 0 */
3292 case GST_FORMAT_TIME:
3293 peer_pos -= queue->cur_level.time;
3294 if (peer_pos < 0) /* Clamp result to 0 */
3298 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3299 "know how to adjust value", gst_format_get_name (format));
3302 /* set updated position */
3303 gst_query_set_position (query, format, peer_pos);
3306 case GST_QUERY_DURATION:
3308 GST_DEBUG_OBJECT (queue, "doing peer query");
3310 if (!gst_pad_peer_query (queue->sinkpad, query))
3313 GST_DEBUG_OBJECT (queue, "peer query success");
3316 case GST_QUERY_BUFFERING:
3319 gboolean is_buffering;
3320 GstBufferingMode mode;
3321 gint avg_in, avg_out;
3322 gint64 buffering_left;
3324 GST_DEBUG_OBJECT (queue, "query buffering");
3326 get_buffering_level (queue, &is_buffering, &percent);
3327 percent = convert_to_buffering_percent (queue, percent);
3328 gst_query_set_buffering_percent (query, is_buffering, percent);
3330 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3332 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3335 if (!QUEUE_IS_USING_QUEUE (queue)) {
3336 /* add ranges for download and ringbuffer buffering */
3338 gint64 start, stop, range_start, range_stop;
3339 guint64 writing_pos;
3340 gint64 estimated_total;
3342 gboolean peer_res, is_eos;
3343 GstQueue2Range *queued_ranges;
3345 /* we need a current download region */
3346 if (queue->current == NULL)
3349 writing_pos = queue->current->writing_pos;
3350 is_eos = queue->is_eos;
3353 /* we're EOS, we know the duration in bytes now */
3355 duration = writing_pos;
3357 /* get duration of upstream in bytes */
3358 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3359 GST_FORMAT_BYTES, &duration);
3362 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3363 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3365 /* calculate remaining and total download time */
3366 if (peer_res && avg_in > 0.0)
3367 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3369 estimated_total = -1;
3371 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3374 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3377 case GST_FORMAT_PERCENT:
3378 /* we need duration */
3383 /* get our available data relative to the duration */
3386 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3391 case GST_FORMAT_BYTES:
3401 /* fill out the buffered ranges */
3402 for (queued_ranges = queue->ranges; queued_ranges;
3403 queued_ranges = queued_ranges->next) {
3405 case GST_FORMAT_PERCENT:
3406 if (duration == -1) {
3412 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3413 queued_ranges->offset, duration);
3415 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3416 queued_ranges->writing_pos, duration);
3418 case GST_FORMAT_BYTES:
3419 range_start = queued_ranges->offset;
3420 range_stop = queued_ranges->writing_pos;
3427 if (range_start == range_stop)
3429 GST_DEBUG_OBJECT (queue,
3430 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3431 G_GINT64_FORMAT, range_start, range_stop);
3432 gst_query_add_buffering_range (query, range_start, range_stop);
3435 gst_query_set_buffering_range (query, format, start, stop,
3440 case GST_QUERY_SCHEDULING:
3443 GstSchedulingFlags flags = 0;
3445 if (!gst_pad_peer_query (queue->sinkpad, query))
3448 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3450 /* we can operate in pull mode when we are using a tempfile */
3451 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3454 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3455 gst_query_set_scheduling (query, flags, 0, -1, 0);
3457 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3458 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3462 /* peer handled other queries */
3463 if (!gst_pad_query_default (pad, parent, query))
3473 GST_DEBUG_OBJECT (queue, "failed peer query");
3479 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3481 GstQueue2 *queue = GST_QUEUE2 (element);
3483 /* simply forward to the srcpad query function */
3484 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3489 gst_queue2_update_upstream_size (GstQueue2 * queue)
3491 gint64 upstream_size = -1;
3493 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3495 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3497 /* upstream_size can be negative but queue->upstream_size is unsigned.
3498 * Prevent setting negative values to it (the query can return -1) */
3499 if (upstream_size >= 0)
3500 queue->upstream_size = upstream_size;
3502 queue->upstream_size = 0;
3506 static GstFlowReturn
3507 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3508 guint length, GstBuffer ** buffer)
3513 queue = GST_QUEUE2_CAST (parent);
3515 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3516 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3517 offset = (offset == -1) ? queue->current->reading_pos : offset;
3519 GST_DEBUG_OBJECT (queue,
3520 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3522 /* catch any reads beyond the size of the file here to make sure queue2
3523 * doesn't send seek events beyond the size of the file upstream, since
3524 * that would confuse elements such as souphttpsrc and/or http servers.
3525 * Demuxers often just loop until EOS at the end of the file to figure out
3526 * when they've read all the end-headers or index chunks. */
3527 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3528 gst_queue2_update_upstream_size (queue);
3529 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3530 goto out_unexpected;
3533 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3534 gst_queue2_update_upstream_size (queue);
3535 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3536 length = queue->upstream_size - offset;
3537 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3541 /* FIXME - function will block when the range is not yet available */
3542 ret = gst_queue2_create_read (queue, offset, length, buffer);
3543 GST_QUEUE2_MUTEX_UNLOCK (queue);
3544 gst_queue2_post_buffering (queue);
3551 ret = queue->srcresult;
3553 GST_DEBUG_OBJECT (queue, "we are %s", gst_flow_get_name (ret));
3554 GST_QUEUE2_MUTEX_UNLOCK (queue);
3559 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3560 GST_QUEUE2_MUTEX_UNLOCK (queue);
3561 return GST_FLOW_EOS;
3565 /* sink currently only operates in push mode */
3567 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3568 GstPadMode mode, gboolean active)
3573 queue = GST_QUEUE2 (parent);
3576 case GST_PAD_MODE_PUSH:
3578 GST_QUEUE2_MUTEX_LOCK (queue);
3579 GST_DEBUG_OBJECT (queue, "activating push mode");
3580 queue->srcresult = GST_FLOW_OK;
3581 queue->sinkresult = GST_FLOW_OK;
3582 queue->is_eos = FALSE;
3583 queue->unexpected = FALSE;
3584 reset_rate_timer (queue);
3585 GST_QUEUE2_MUTEX_UNLOCK (queue);
3587 /* unblock chain function */
3588 GST_QUEUE2_MUTEX_LOCK (queue);
3589 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3590 queue->srcresult = GST_FLOW_FLUSHING;
3591 queue->sinkresult = GST_FLOW_FLUSHING;
3592 GST_QUEUE2_SIGNAL_DEL (queue);
3593 GST_QUEUE2_MUTEX_UNLOCK (queue);
3595 /* wait until it is unblocked and clean up */
3596 GST_PAD_STREAM_LOCK (pad);
3597 GST_QUEUE2_MUTEX_LOCK (queue);
3598 gst_queue2_locked_flush (queue, TRUE, FALSE);
3599 GST_QUEUE2_MUTEX_UNLOCK (queue);
3600 GST_PAD_STREAM_UNLOCK (pad);
3611 /* src operating in push mode, we start a task on the source pad that pushes out
3612 * buffers from the queue */
3614 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3616 gboolean result = FALSE;
3619 queue = GST_QUEUE2 (parent);
3622 GST_QUEUE2_MUTEX_LOCK (queue);
3623 GST_DEBUG_OBJECT (queue, "activating push mode");
3624 queue->srcresult = GST_FLOW_OK;
3625 queue->sinkresult = GST_FLOW_OK;
3626 queue->is_eos = FALSE;
3627 queue->unexpected = FALSE;
3629 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3630 GST_QUEUE2_MUTEX_UNLOCK (queue);
3632 /* unblock loop function */
3633 GST_QUEUE2_MUTEX_LOCK (queue);
3634 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3635 queue->srcresult = GST_FLOW_FLUSHING;
3636 queue->sinkresult = GST_FLOW_FLUSHING;
3637 /* the item add signal will unblock */
3638 GST_QUEUE2_SIGNAL_ADD (queue);
3639 GST_QUEUE2_MUTEX_UNLOCK (queue);
3641 /* step 2, make sure streaming finishes */
3642 result = gst_pad_stop_task (pad);
3644 GST_QUEUE2_MUTEX_LOCK (queue);
3645 gst_queue2_locked_flush (queue, FALSE, FALSE);
3646 GST_QUEUE2_MUTEX_UNLOCK (queue);
3652 /* pull mode, downstream will call our getrange function */
3654 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3659 queue = GST_QUEUE2 (parent);
3662 GST_QUEUE2_MUTEX_LOCK (queue);
3663 if (!QUEUE_IS_USING_QUEUE (queue)) {
3664 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3665 /* open the temp file now */
3666 result = gst_queue2_open_temp_location_file (queue);
3667 } else if (!queue->ring_buffer) {
3668 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3669 result = ! !queue->ring_buffer;
3674 GST_DEBUG_OBJECT (queue, "activating pull mode");
3675 init_ranges (queue);
3676 queue->srcresult = GST_FLOW_OK;
3677 queue->sinkresult = GST_FLOW_OK;
3678 queue->is_eos = FALSE;
3679 queue->unexpected = FALSE;
3680 queue->upstream_size = 0;
3682 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3683 /* this is not allowed, we cannot operate in pull mode without a temp
3685 queue->srcresult = GST_FLOW_FLUSHING;
3686 queue->sinkresult = GST_FLOW_FLUSHING;
3689 GST_QUEUE2_MUTEX_UNLOCK (queue);
3691 GST_QUEUE2_MUTEX_LOCK (queue);
3692 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3693 queue->srcresult = GST_FLOW_FLUSHING;
3694 queue->sinkresult = GST_FLOW_FLUSHING;
3695 /* this will unlock getrange */
3696 GST_QUEUE2_SIGNAL_ADD (queue);
3698 GST_QUEUE2_MUTEX_UNLOCK (queue);
3705 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3711 case GST_PAD_MODE_PULL:
3712 res = gst_queue2_src_activate_pull (pad, parent, active);
3714 case GST_PAD_MODE_PUSH:
3715 res = gst_queue2_src_activate_push (pad, parent, active);
3718 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3725 static GstStateChangeReturn
3726 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3729 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3731 queue = GST_QUEUE2 (element);
3733 switch (transition) {
3734 case GST_STATE_CHANGE_NULL_TO_READY:
3736 case GST_STATE_CHANGE_READY_TO_PAUSED:
3737 GST_QUEUE2_MUTEX_LOCK (queue);
3738 if (!QUEUE_IS_USING_QUEUE (queue)) {
3739 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3740 if (!gst_queue2_open_temp_location_file (queue))
3741 ret = GST_STATE_CHANGE_FAILURE;
3743 if (queue->ring_buffer) {
3744 g_free (queue->ring_buffer);
3745 queue->ring_buffer = NULL;
3747 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3748 ret = GST_STATE_CHANGE_FAILURE;
3750 init_ranges (queue);
3752 queue->segment_event_received = FALSE;
3753 queue->starting_segment = NULL;
3754 gst_event_replace (&queue->stream_start_event, NULL);
3755 GST_QUEUE2_MUTEX_UNLOCK (queue);
3756 query_downstream_bitrate (queue);
3758 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3764 if (ret == GST_STATE_CHANGE_FAILURE)
3767 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3769 if (ret == GST_STATE_CHANGE_FAILURE)
3772 switch (transition) {
3773 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3775 case GST_STATE_CHANGE_PAUSED_TO_READY:
3776 GST_QUEUE2_MUTEX_LOCK (queue);
3777 if (!QUEUE_IS_USING_QUEUE (queue)) {
3778 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3779 gst_queue2_close_temp_location_file (queue);
3780 } else if (queue->ring_buffer) {
3781 g_free (queue->ring_buffer);
3782 queue->ring_buffer = NULL;
3784 clean_ranges (queue);
3786 if (queue->starting_segment != NULL) {
3787 gst_event_unref (queue->starting_segment);
3788 queue->starting_segment = NULL;
3790 gst_event_replace (&queue->stream_start_event, NULL);
3791 GST_QUEUE2_MUTEX_UNLOCK (queue);
3793 case GST_STATE_CHANGE_READY_TO_NULL:
3802 /* changing the capacity of the queue must wake up
3803 * the _chain function, it might have more room now
3804 * to store the buffer/event in the queue */
3805 #define QUEUE_CAPACITY_CHANGE(q) \
3806 GST_QUEUE2_SIGNAL_DEL (queue); \
3807 if (queue->use_buffering) \
3808 update_buffering (queue);
3810 /* Changing the minimum required fill level must
3811 * wake up the _loop function as it might now
3812 * be able to preceed.
3814 #define QUEUE_THRESHOLD_CHANGE(q)\
3815 GST_QUEUE2_SIGNAL_ADD (queue);
3818 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3822 /* the element must be stopped in order to do this */
3823 GST_OBJECT_LOCK (queue);
3824 state = GST_STATE (queue);
3825 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3827 GST_OBJECT_UNLOCK (queue);
3829 /* set new location */
3830 g_free (queue->temp_template);
3831 queue->temp_template = g_strdup (template);
3838 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3839 GST_OBJECT_UNLOCK (queue);
3844 gst_queue2_set_property (GObject * object,
3845 guint prop_id, const GValue * value, GParamSpec * pspec)
3847 GstQueue2 *queue = GST_QUEUE2 (object);
3849 /* someone could change levels here, and since this
3850 * affects the get/put funcs, we need to lock for safety. */
3851 GST_QUEUE2_MUTEX_LOCK (queue);
3854 case PROP_MAX_SIZE_BYTES:
3855 queue->max_level.bytes = g_value_get_uint (value);
3856 QUEUE_CAPACITY_CHANGE (queue);
3858 case PROP_MAX_SIZE_BUFFERS:
3859 queue->max_level.buffers = g_value_get_uint (value);
3860 QUEUE_CAPACITY_CHANGE (queue);
3862 case PROP_MAX_SIZE_TIME:
3863 queue->max_level.time = g_value_get_uint64 (value);
3864 /* set rate_time to the same value. We use an extra field in the level
3865 * structure so that we can easily access and compare it */
3866 queue->max_level.rate_time = queue->max_level.time;
3867 QUEUE_CAPACITY_CHANGE (queue);
3869 case PROP_USE_BUFFERING:
3870 queue->use_buffering = g_value_get_boolean (value);
3871 if (!queue->use_buffering && queue->is_buffering) {
3872 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3873 "posting 100%% message");
3874 SET_PERCENT (queue, 100);
3875 queue->is_buffering = FALSE;
3878 if (queue->use_buffering) {
3879 queue->is_buffering = TRUE;
3880 update_buffering (queue);
3883 case PROP_USE_TAGS_BITRATE:
3884 queue->use_tags_bitrate = g_value_get_boolean (value);
3886 case PROP_USE_RATE_ESTIMATE:
3887 queue->use_rate_estimate = g_value_get_boolean (value);
3889 case PROP_LOW_PERCENT:
3890 queue->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3891 if (queue->is_buffering)
3892 update_buffering (queue);
3894 case PROP_HIGH_PERCENT:
3895 queue->high_watermark =
3896 g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3897 if (queue->is_buffering)
3898 update_buffering (queue);
3900 case PROP_LOW_WATERMARK:
3901 queue->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3902 if (queue->is_buffering)
3903 update_buffering (queue);
3905 case PROP_HIGH_WATERMARK:
3906 queue->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3907 if (queue->is_buffering)
3908 update_buffering (queue);
3910 case PROP_TEMP_TEMPLATE:
3911 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3913 case PROP_TEMP_REMOVE:
3914 queue->temp_remove = g_value_get_boolean (value);
3916 case PROP_RING_BUFFER_MAX_SIZE:
3917 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3919 case PROP_USE_BITRATE_QUERY:
3920 queue->use_bitrate_query = g_value_get_boolean (value);
3923 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3927 GST_QUEUE2_MUTEX_UNLOCK (queue);
3928 gst_queue2_post_buffering (queue);
3932 gst_queue2_get_property (GObject * object,
3933 guint prop_id, GValue * value, GParamSpec * pspec)
3935 GstQueue2 *queue = GST_QUEUE2 (object);
3937 GST_QUEUE2_MUTEX_LOCK (queue);
3940 case PROP_CUR_LEVEL_BYTES:
3941 g_value_set_uint (value, queue->cur_level.bytes);
3943 case PROP_CUR_LEVEL_BUFFERS:
3944 g_value_set_uint (value, queue->cur_level.buffers);
3946 case PROP_CUR_LEVEL_TIME:
3947 g_value_set_uint64 (value, queue->cur_level.time);
3949 case PROP_MAX_SIZE_BYTES:
3950 g_value_set_uint (value, queue->max_level.bytes);
3952 case PROP_MAX_SIZE_BUFFERS:
3953 g_value_set_uint (value, queue->max_level.buffers);
3955 case PROP_MAX_SIZE_TIME:
3956 g_value_set_uint64 (value, queue->max_level.time);
3958 case PROP_USE_BUFFERING:
3959 g_value_set_boolean (value, queue->use_buffering);
3961 case PROP_USE_TAGS_BITRATE:
3962 g_value_set_boolean (value, queue->use_tags_bitrate);
3964 case PROP_USE_RATE_ESTIMATE:
3965 g_value_set_boolean (value, queue->use_rate_estimate);
3967 case PROP_LOW_PERCENT:
3968 g_value_set_int (value, queue->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
3970 case PROP_HIGH_PERCENT:
3971 g_value_set_int (value, queue->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
3973 case PROP_LOW_WATERMARK:
3974 g_value_set_double (value, queue->low_watermark /
3975 (gdouble) MAX_BUFFERING_LEVEL);
3977 case PROP_HIGH_WATERMARK:
3978 g_value_set_double (value, queue->high_watermark /
3979 (gdouble) MAX_BUFFERING_LEVEL);
3981 case PROP_TEMP_TEMPLATE:
3982 g_value_set_string (value, queue->temp_template);
3984 case PROP_TEMP_LOCATION:
3985 g_value_set_string (value, queue->temp_location);
3987 case PROP_TEMP_REMOVE:
3988 g_value_set_boolean (value, queue->temp_remove);
3990 case PROP_RING_BUFFER_MAX_SIZE:
3991 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3993 case PROP_AVG_IN_RATE:
3995 gdouble in_rate = queue->byte_in_rate;
3997 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3998 * calculated, so calculate it here. */
3999 if (in_rate == 0.0 && queue->bytes_in
4000 && queue->last_update_in_rates_elapsed > 0.0)
4001 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
4003 g_value_set_int64 (value, (gint64) in_rate);
4006 case PROP_USE_BITRATE_QUERY:
4007 g_value_set_boolean (value, queue->use_bitrate_query);
4010 guint64 bitrate = 0;
4011 if (bitrate == 0 && queue->use_tags_bitrate) {
4012 if (queue->sink_tags_bitrate > 0)
4013 bitrate = queue->sink_tags_bitrate;
4014 else if (queue->src_tags_bitrate)
4015 bitrate = queue->src_tags_bitrate;
4017 if (bitrate == 0 && queue->use_bitrate_query) {
4018 bitrate = queue->downstream_bitrate;
4020 g_value_set_uint64 (value, (guint64) bitrate);
4024 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4028 GST_QUEUE2_MUTEX_UNLOCK (queue);