2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
29 * Data is queued until one of the limits specified by the
30 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
31 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
38 * You can query how many buffers are queued by reading the
39 * #GstQueue2:current-level-buffers property.
41 * The default queue size limits are 100 buffers, 2MB of data, or
42 * two seconds worth of data, whichever is reached first.
44 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
45 * will allocate a random free filename and buffer data in the file.
46 * By using this, it will buffer the entire stream data on the file independently
47 * of the queue size limits, they will only be used for buffering statistics.
49 * The temp-location property will be used to notify the application of the
57 #include "gstqueue2.h"
59 #include <glib/gstdio.h>
61 #include "gst/gst-i18n-lib.h"
62 #include "gst/glib-compat-private.h"
67 #include <io.h> /* lseek, open, close, read */
69 #define lseek _lseeki64
76 #ifdef __BIONIC__ /* Android */
84 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
94 GST_DEBUG_CATEGORY_STATIC (queue_debug);
95 #define GST_CAT_DEFAULT (queue_debug)
96 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
104 #define DEFAULT_BUFFER_SIZE 4096
105 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
106 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
107 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
109 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
111 /* default property values */
112 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
113 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
114 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
115 #define DEFAULT_USE_BUFFERING FALSE
116 #define DEFAULT_USE_TAGS_BITRATE FALSE
117 #define DEFAULT_USE_RATE_ESTIMATE TRUE
118 #define DEFAULT_LOW_PERCENT 10
119 #define DEFAULT_HIGH_PERCENT 99
120 #define DEFAULT_LOW_WATERMARK 0.01
121 #define DEFAULT_HIGH_WATERMARK 0.99
122 #define DEFAULT_TEMP_REMOVE TRUE
123 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
128 PROP_CUR_LEVEL_BUFFERS,
129 PROP_CUR_LEVEL_BYTES,
131 PROP_MAX_SIZE_BUFFERS,
135 PROP_USE_TAGS_BITRATE,
136 PROP_USE_RATE_ESTIMATE,
144 PROP_RING_BUFFER_MAX_SIZE,
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 queue->queue.length))
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));
421 * GstQueue2:temp-remove
423 * When temp-template is set, remove the temporary file when going to READY.
425 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
426 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
427 "Remove the temp-location after use",
428 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 * GstQueue2:ring-buffer-max-size
433 * The maximum size of the ring buffer in bytes. If set to 0, the ring
434 * buffer is disabled. Default 0.
436 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
437 g_param_spec_uint64 ("ring-buffer-max-size",
438 "Max. ring buffer size (bytes)",
439 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
440 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 * GstQueue2:avg-in-rate
446 * The average input data rate.
448 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
449 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
450 "Average input data rate (bytes/s)",
451 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
453 /* set several parent class virtual functions */
454 gobject_class->finalize = gst_queue2_finalize;
456 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
457 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
459 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
462 "Erik Walthinsen <omega@cse.ogi.edu>, "
463 "Wim Taymans <wim.taymans@gmail.com>");
465 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
466 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
470 gst_queue2_init (GstQueue2 * queue)
472 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
474 gst_pad_set_chain_function (queue->sinkpad,
475 GST_DEBUG_FUNCPTR (gst_queue2_chain));
476 gst_pad_set_chain_list_function (queue->sinkpad,
477 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
478 gst_pad_set_activatemode_function (queue->sinkpad,
479 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
480 gst_pad_set_event_full_function (queue->sinkpad,
481 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
482 gst_pad_set_query_function (queue->sinkpad,
483 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
484 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
485 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
487 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
489 gst_pad_set_activatemode_function (queue->srcpad,
490 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
491 gst_pad_set_getrange_function (queue->srcpad,
492 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
493 gst_pad_set_event_function (queue->srcpad,
494 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
495 gst_pad_set_query_function (queue->srcpad,
496 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
497 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
498 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
501 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
502 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
503 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
504 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
505 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
506 queue->use_buffering = DEFAULT_USE_BUFFERING;
507 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
508 queue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
509 queue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
511 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
512 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
514 queue->sinktime = GST_CLOCK_TIME_NONE;
515 queue->srctime = GST_CLOCK_TIME_NONE;
516 queue->sink_tainted = TRUE;
517 queue->src_tainted = TRUE;
519 queue->srcresult = GST_FLOW_FLUSHING;
520 queue->sinkresult = GST_FLOW_FLUSHING;
521 queue->is_eos = FALSE;
522 queue->in_timer = g_timer_new ();
523 queue->out_timer = g_timer_new ();
525 g_mutex_init (&queue->qlock);
526 queue->waiting_add = FALSE;
527 g_cond_init (&queue->item_add);
528 queue->waiting_del = FALSE;
529 g_cond_init (&queue->item_del);
530 g_queue_init (&queue->queue);
532 g_cond_init (&queue->query_handled);
533 queue->last_query = FALSE;
535 g_mutex_init (&queue->buffering_post_lock);
536 queue->buffering_percent = 100;
538 /* tempfile related */
539 queue->temp_template = NULL;
540 queue->temp_location = NULL;
541 queue->temp_remove = DEFAULT_TEMP_REMOVE;
543 queue->ring_buffer = NULL;
544 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
546 GST_DEBUG_OBJECT (queue,
547 "initialized queue's not_empty & not_full conditions");
550 /* called only once, as opposed to dispose */
552 gst_queue2_finalize (GObject * object)
554 GstQueue2 *queue = GST_QUEUE2 (object);
556 GST_DEBUG_OBJECT (queue, "finalizing queue");
558 while (!g_queue_is_empty (&queue->queue)) {
559 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
561 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
562 gst_mini_object_unref (qitem->item);
563 g_slice_free (GstQueue2Item, qitem);
566 queue->last_query = FALSE;
567 g_queue_clear (&queue->queue);
568 g_mutex_clear (&queue->qlock);
569 g_mutex_clear (&queue->buffering_post_lock);
570 g_cond_clear (&queue->item_add);
571 g_cond_clear (&queue->item_del);
572 g_cond_clear (&queue->query_handled);
573 g_timer_destroy (queue->in_timer);
574 g_timer_destroy (queue->out_timer);
576 /* temp_file path cleanup */
577 g_free (queue->temp_template);
578 g_free (queue->temp_location);
580 G_OBJECT_CLASS (parent_class)->finalize (object);
584 debug_ranges (GstQueue2 * queue)
586 GstQueue2Range *walk;
588 for (walk = queue->ranges; walk; walk = walk->next) {
589 GST_DEBUG_OBJECT (queue,
590 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
591 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
592 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
593 walk->rb_writing_pos, walk->reading_pos,
594 walk == queue->current ? "**y**" : " n ");
598 /* clear all the downloaded ranges */
600 clean_ranges (GstQueue2 * queue)
602 GST_DEBUG_OBJECT (queue, "clean queue ranges");
604 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
605 queue->ranges = NULL;
606 queue->current = NULL;
609 /* find a range that contains @offset or NULL when nothing does */
610 static GstQueue2Range *
611 find_range (GstQueue2 * queue, guint64 offset)
613 GstQueue2Range *range = NULL;
614 GstQueue2Range *walk;
616 /* first do a quick check for the current range */
617 for (walk = queue->ranges; walk; walk = walk->next) {
618 if (offset >= walk->offset && offset <= walk->writing_pos) {
619 /* we can reuse an existing range */
625 GST_DEBUG_OBJECT (queue,
626 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
627 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
629 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
635 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
637 guint64 max_reading_pos, writing_pos;
639 writing_pos = range->writing_pos;
640 max_reading_pos = range->max_reading_pos;
642 if (writing_pos > max_reading_pos)
643 queue->cur_level.bytes = writing_pos - max_reading_pos;
645 queue->cur_level.bytes = 0;
648 /* make a new range for @offset or reuse an existing range */
649 static GstQueue2Range *
650 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
652 GstQueue2Range *range, *prev, *next;
654 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
656 if ((range = find_range (queue, offset))) {
657 GST_DEBUG_OBJECT (queue,
658 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
660 if (update_existing && range->writing_pos != offset) {
661 GST_DEBUG_OBJECT (queue, "updating range writing position to "
662 "%" G_GUINT64_FORMAT, offset);
663 range->writing_pos = offset;
666 GST_DEBUG_OBJECT (queue,
667 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
669 range = g_slice_new0 (GstQueue2Range);
670 range->offset = offset;
671 /* we want to write to the next location in the ring buffer */
672 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
673 range->writing_pos = offset;
674 range->rb_writing_pos = range->rb_offset;
675 range->reading_pos = offset;
676 range->max_reading_pos = offset;
680 next = queue->ranges;
682 if (next->offset > offset) {
683 /* insert before next */
684 GST_DEBUG_OBJECT (queue,
685 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
697 queue->ranges = range;
699 debug_ranges (queue);
701 /* update the stats for this range */
702 update_cur_level (queue, range);
708 /* clear and init the download ranges for offset 0 */
710 init_ranges (GstQueue2 * queue)
712 GST_DEBUG_OBJECT (queue, "init queue ranges");
714 /* get rid of all the current ranges */
715 clean_ranges (queue);
716 /* make a range for offset 0 */
717 queue->current = add_range (queue, 0, TRUE);
720 /* calculate the diff between running time on the sink and src of the queue.
721 * This is the total amount of time in the queue. */
723 update_time_level (GstQueue2 * queue)
725 if (queue->sink_tainted) {
727 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
728 queue->sink_segment.position);
729 queue->sink_tainted = FALSE;
732 if (queue->src_tainted) {
734 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
735 queue->src_segment.position);
736 queue->src_tainted = FALSE;
739 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
740 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
742 if (queue->sinktime != GST_CLOCK_TIME_NONE
743 && queue->srctime != GST_CLOCK_TIME_NONE
744 && queue->sinktime >= queue->srctime)
745 queue->cur_level.time = queue->sinktime - queue->srctime;
747 queue->cur_level.time = 0;
750 /* take a SEGMENT event and apply the values to segment, updating the time
753 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
756 gst_event_copy_segment (event, segment);
758 if (segment->format == GST_FORMAT_BYTES) {
759 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
760 /* start is where we'll be getting from and as such writing next */
761 queue->current = add_range (queue, segment->start, TRUE);
765 /* now configure the values, we use these to track timestamps on the
767 if (segment->format != GST_FORMAT_TIME) {
768 /* non-time format, pretend the current time segment is closed with a
769 * 0 start and unknown stop time. */
770 segment->format = GST_FORMAT_TIME;
776 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
779 queue->sink_tainted = TRUE;
781 queue->src_tainted = TRUE;
783 /* segment can update the time level of the queue */
784 update_time_level (queue);
788 apply_gap (GstQueue2 * queue, GstEvent * event,
789 GstSegment * segment, gboolean is_sink)
791 GstClockTime timestamp;
792 GstClockTime duration;
794 gst_event_parse_gap (event, ×tamp, &duration);
796 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
798 if (GST_CLOCK_TIME_IS_VALID (duration)) {
799 timestamp += duration;
802 segment->position = timestamp;
805 queue->sink_tainted = TRUE;
807 queue->src_tainted = TRUE;
809 /* calc diff with other end */
810 update_time_level (queue);
814 /* take a buffer and update segment, updating the time level of the queue. */
816 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
817 guint64 size, gboolean is_sink)
819 GstClockTime duration, timestamp;
821 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
822 duration = GST_BUFFER_DURATION (buffer);
824 /* If we have no duration, pick one from the bitrate if we can */
825 if (duration == GST_CLOCK_TIME_NONE && queue->use_tags_bitrate) {
827 is_sink ? queue->sink_tags_bitrate : queue->src_tags_bitrate;
829 duration = gst_util_uint64_scale (size, 8 * GST_SECOND, bitrate);
832 /* if no timestamp is set, assume it's continuous with the previous
834 if (timestamp == GST_CLOCK_TIME_NONE)
835 timestamp = segment->position;
838 if (duration != GST_CLOCK_TIME_NONE)
839 timestamp += duration;
841 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
842 GST_TIME_ARGS (timestamp));
844 segment->position = timestamp;
847 queue->sink_tainted = TRUE;
849 queue->src_tainted = TRUE;
851 /* calc diff with other end */
852 update_time_level (queue);
857 GstClockTime timestamp;
862 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
864 struct BufListData *bld = data;
865 GstClockTime *timestamp = &bld->timestamp;
868 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
869 " duration %" GST_TIME_FORMAT, idx,
870 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
871 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
872 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
874 btime = GST_BUFFER_DTS_OR_PTS (*buf);
875 if (GST_CLOCK_TIME_IS_VALID (btime))
878 if (GST_BUFFER_DURATION_IS_VALID (*buf))
879 *timestamp += GST_BUFFER_DURATION (*buf);
880 else if (bld->bitrate != 0) {
881 guint64 size = gst_buffer_get_size (*buf);
883 /* If we have no duration, pick one from the bitrate if we can */
884 *timestamp += gst_util_uint64_scale (bld->bitrate, 8 * GST_SECOND, size);
888 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
892 /* take a buffer list and update segment, updating the time level of the queue */
894 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
895 GstSegment * segment, gboolean is_sink)
897 struct BufListData bld;
899 /* if no timestamp is set, assume it's continuous with the previous time */
900 bld.timestamp = segment->position;
902 if (queue->use_tags_bitrate) {
904 bld.bitrate = queue->sink_tags_bitrate;
906 bld.bitrate = queue->src_tags_bitrate;
910 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &bld);
912 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
913 GST_TIME_ARGS (bld.timestamp));
915 segment->position = bld.timestamp;
918 queue->sink_tainted = TRUE;
920 queue->src_tainted = TRUE;
922 /* calc diff with other end */
923 update_time_level (queue);
927 normalize_to_buffering_level (guint64 cur_level, guint64 max_level,
936 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL,
937 MIN (max_level, alt_max));
939 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL, max_level);
941 return MIN (p, MAX_BUFFERING_LEVEL);
945 get_buffering_level (GstQueue2 * queue, gboolean * is_buffering,
946 gint * buffering_level)
948 gint buflevel, buflevel2;
950 if (queue->high_watermark <= 0) {
952 *buffering_level = MAX_BUFFERING_LEVEL;
954 *is_buffering = FALSE;
957 #define GET_BUFFER_LEVEL_FOR_QUANTITY(format,alt_max) \
958 normalize_to_buffering_level (queue->cur_level.format,queue->max_level.format,(alt_max))
961 /* on EOS we are always 100% full, we set the var here so that it we can
962 * reuse the logic below to stop buffering */
963 buflevel = MAX_BUFFERING_LEVEL;
964 GST_LOG_OBJECT (queue, "we are EOS");
966 GST_LOG_OBJECT (queue,
967 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
968 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
969 queue->cur_level.buffers);
971 /* figure out the buffering level we are filled, we take the max of all formats. */
972 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
973 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, 0);
975 guint64 rb_size = queue->ring_buffer_max_size;
976 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, rb_size);
979 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (time, 0);
980 buflevel = MAX (buflevel, buflevel2);
982 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (buffers, 0);
983 buflevel = MAX (buflevel, buflevel2);
985 /* also apply the rate estimate when we need to */
986 if (queue->use_rate_estimate) {
987 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (rate_time, 0);
988 buflevel = MAX (buflevel, buflevel2);
991 /* Don't get to 0% unless we're really empty */
992 if (queue->cur_level.bytes > 0)
993 buflevel = MAX (1, buflevel);
995 #undef GET_BUFFER_LEVEL_FOR_QUANTITY
998 *is_buffering = queue->is_buffering;
1000 if (buffering_level)
1001 *buffering_level = buflevel;
1003 GST_DEBUG_OBJECT (queue, "buffering %d, level %d", queue->is_buffering,
1010 convert_to_buffering_percent (GstQueue2 * queue, gint buffering_level)
1014 /* scale so that if buffering_level equals the high watermark,
1015 * the percentage is 100% */
1016 percent = buffering_level * 100 / queue->high_watermark;
1025 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
1026 gint * avg_in, gint * avg_out, gint64 * buffering_left)
1029 if (!QUEUE_IS_USING_QUEUE (queue)) {
1030 if (QUEUE_IS_USING_RING_BUFFER (queue))
1031 *mode = GST_BUFFERING_TIMESHIFT;
1033 *mode = GST_BUFFERING_DOWNLOAD;
1035 *mode = GST_BUFFERING_STREAM;
1040 *avg_in = queue->byte_in_rate;
1042 *avg_out = queue->byte_out_rate;
1044 if (buffering_left) {
1045 *buffering_left = (percent == 100 ? 0 : -1);
1047 if (queue->use_rate_estimate) {
1050 max = queue->max_level.rate_time;
1051 cur = queue->cur_level.rate_time;
1053 if (percent != 100 && max > cur)
1054 *buffering_left = (max - cur) / 1000000;
1059 /* Called with the lock taken */
1061 gst_queue2_get_buffering_message (GstQueue2 * queue)
1063 GstMessage *msg = NULL;
1065 if (queue->percent_changed) {
1066 gint percent = queue->buffering_percent;
1068 queue->percent_changed = FALSE;
1070 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
1071 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
1073 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
1074 queue->avg_out, queue->buffering_left);
1081 gst_queue2_post_buffering (GstQueue2 * queue)
1083 GstMessage *msg = NULL;
1085 g_mutex_lock (&queue->buffering_post_lock);
1086 GST_QUEUE2_MUTEX_LOCK (queue);
1087 msg = gst_queue2_get_buffering_message (queue);
1088 GST_QUEUE2_MUTEX_UNLOCK (queue);
1091 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1093 g_mutex_unlock (&queue->buffering_post_lock);
1097 update_buffering (GstQueue2 * queue)
1099 gint buffering_level, percent;
1101 /* Ensure the variables used to calculate buffering state are up-to-date. */
1103 update_cur_level (queue, queue->current);
1104 update_in_rates (queue, FALSE);
1106 if (!get_buffering_level (queue, NULL, &buffering_level))
1109 percent = convert_to_buffering_percent (queue, buffering_level);
1111 if (queue->is_buffering) {
1112 /* if we were buffering see if we reached the high watermark */
1114 queue->is_buffering = FALSE;
1116 SET_PERCENT (queue, percent);
1118 /* we were not buffering, check if we need to start buffering if we drop
1119 * below the low threshold */
1120 if (buffering_level < queue->low_watermark) {
1121 queue->is_buffering = TRUE;
1122 SET_PERCENT (queue, percent);
1128 reset_rate_timer (GstQueue2 * queue)
1130 queue->bytes_in = 0;
1131 queue->bytes_out = 0;
1132 queue->byte_in_rate = 0.0;
1133 queue->byte_in_period = 0;
1134 queue->byte_out_rate = 0.0;
1135 queue->last_update_in_rates_elapsed = 0.0;
1136 queue->last_in_elapsed = 0.0;
1137 queue->last_out_elapsed = 0.0;
1138 queue->in_timer_started = FALSE;
1139 queue->out_timer_started = FALSE;
1142 /* the interval in seconds to recalculate the rate */
1143 #define RATE_INTERVAL 0.2
1144 /* Tuning for rate estimation. We use a large window for the input rate because
1145 * it should be stable when connected to a network. The output rate is less
1146 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1147 * therefore adapt more quickly.
1148 * However, initial input rate may be subject to a burst, and should therefore
1149 * initially also adapt more quickly to changes, and only later on give higher
1150 * weight to previous values. */
1151 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1152 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1155 update_in_rates (GstQueue2 * queue, gboolean force)
1157 gdouble elapsed, period;
1158 gdouble byte_in_rate;
1160 if (!queue->in_timer_started) {
1161 queue->in_timer_started = TRUE;
1162 g_timer_start (queue->in_timer);
1166 queue->last_update_in_rates_elapsed = elapsed =
1167 g_timer_elapsed (queue->in_timer, NULL);
1169 /* recalc after each interval. */
1170 if (force || queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1171 period = elapsed - queue->last_in_elapsed;
1173 GST_DEBUG_OBJECT (queue,
1174 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1175 period, queue->bytes_in, queue->byte_in_period);
1177 byte_in_rate = queue->bytes_in / period;
1179 if (queue->byte_in_rate == 0.0)
1180 queue->byte_in_rate = byte_in_rate;
1182 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1183 (double) queue->byte_in_period, period);
1185 /* another data point, cap at 16 for long time running average */
1186 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1187 queue->byte_in_period += period;
1189 /* reset the values to calculate rate over the next interval */
1190 queue->last_in_elapsed = elapsed;
1191 queue->bytes_in = 0;
1194 if (queue->byte_in_rate > 0.0) {
1195 queue->cur_level.rate_time =
1196 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1198 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1199 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1203 update_out_rates (GstQueue2 * queue)
1205 gdouble elapsed, period;
1206 gdouble byte_out_rate;
1208 if (!queue->out_timer_started) {
1209 queue->out_timer_started = TRUE;
1210 g_timer_start (queue->out_timer);
1214 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1216 /* recalc after each interval. */
1217 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1218 period = elapsed - queue->last_out_elapsed;
1220 GST_DEBUG_OBJECT (queue,
1221 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1223 byte_out_rate = queue->bytes_out / period;
1225 if (queue->byte_out_rate == 0.0)
1226 queue->byte_out_rate = byte_out_rate;
1228 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1230 /* reset the values to calculate rate over the next interval */
1231 queue->last_out_elapsed = elapsed;
1232 queue->bytes_out = 0;
1234 if (queue->byte_in_rate > 0.0) {
1235 queue->cur_level.rate_time =
1236 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1238 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1239 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1243 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1245 guint64 reading_pos, max_reading_pos;
1248 max_reading_pos = range->max_reading_pos;
1250 max_reading_pos = MAX (max_reading_pos, reading_pos);
1252 GST_DEBUG_OBJECT (queue,
1253 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1254 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1255 range->max_reading_pos = max_reading_pos;
1257 update_cur_level (queue, range);
1261 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1266 /* until we receive the FLUSH_STOP from this seek, we skip data */
1267 queue->seeking = TRUE;
1268 GST_QUEUE2_MUTEX_UNLOCK (queue);
1270 debug_ranges (queue);
1272 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1275 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1276 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1277 GST_SEEK_TYPE_NONE, -1);
1279 res = gst_pad_push_event (queue->sinkpad, event);
1280 GST_QUEUE2_MUTEX_LOCK (queue);
1283 /* Between us sending the seek event and re-acquiring the lock, the source
1284 * thread might already have pushed data and moved along the range's
1285 * writing_pos beyond the seek offset. In that case we don't want to set
1286 * the writing position back to the requested seek position, as it would
1287 * cause data to be written to the wrong offset in the file or ring buffer.
1288 * We still do the add_range call to switch the current range to the
1289 * requested range, or create one if one doesn't exist yet. */
1290 queue->current = add_range (queue, offset, FALSE);
1296 /* get the threshold for when we decide to seek rather than wait */
1298 get_seek_threshold (GstQueue2 * queue)
1302 /* FIXME, find a good threshold based on the incoming rate. */
1303 threshold = 1024 * 512;
1305 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1306 threshold = MIN (threshold,
1307 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1312 /* see if there is enough data in the file to read a full buffer */
1314 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1316 GstQueue2Range *range;
1318 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1321 if ((range = find_range (queue, offset))) {
1322 if (queue->current != range) {
1323 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1324 perform_seek_to_offset (queue, range->writing_pos);
1327 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1328 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1330 /* we have a range for offset */
1331 GST_DEBUG_OBJECT (queue,
1332 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1333 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1335 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1338 if (offset + length <= range->writing_pos)
1341 GST_DEBUG_OBJECT (queue,
1342 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1343 (offset + length) - range->writing_pos);
1346 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1347 " len %u", offset, length);
1348 /* we don't have the range, see how far away we are */
1349 if (!queue->is_eos && queue->current) {
1350 guint64 threshold = get_seek_threshold (queue);
1352 if (offset >= queue->current->offset && offset <=
1353 queue->current->writing_pos + threshold) {
1354 GST_INFO_OBJECT (queue,
1355 "requested data is within range, wait for data");
1360 /* too far away, do a seek */
1361 perform_seek_to_offset (queue, offset);
1368 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1369 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1370 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1372 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1375 static GstFlowReturn
1376 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1377 guint8 * dst, gint64 * read_return)
1379 guint8 *ring_buffer;
1382 ring_buffer = queue->ring_buffer;
1384 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1387 /* this should not block */
1388 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1390 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1391 res = fread (dst, 1, length, queue->temp_file);
1393 memcpy (dst, ring_buffer + offset, length);
1397 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1399 if (G_UNLIKELY (res < length)) {
1400 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1401 goto could_not_read;
1402 /* check for errors or EOF */
1403 if (ferror (queue->temp_file))
1404 goto could_not_read;
1405 if (feof (queue->temp_file) && length > 0)
1415 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1416 return GST_FLOW_ERROR;
1420 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1421 return GST_FLOW_ERROR;
1425 GST_DEBUG ("non-regular file hits EOS");
1426 return GST_FLOW_EOS;
1430 static GstFlowReturn
1431 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1432 GstBuffer ** buffer)
1437 guint64 file_offset;
1438 guint block_length, remaining, read_length;
1442 GstFlowReturn ret = GST_FLOW_OK;
1444 /* allocate the output buffer of the requested size */
1445 if (*buffer == NULL)
1446 buf = gst_buffer_new_allocate (NULL, length, NULL);
1450 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
1451 goto buffer_write_fail;
1454 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1458 rb_size = queue->ring_buffer_max_size;
1459 max_size = QUEUE_MAX_BYTES (queue);
1462 while (remaining > 0) {
1463 /* configure how much/whether to read */
1464 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1467 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1470 /* calculate how far away the offset is */
1471 if (queue->current->writing_pos > rpos)
1472 level = queue->current->writing_pos - rpos;
1476 GST_DEBUG_OBJECT (queue,
1477 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1478 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1479 rpos, queue->current->writing_pos, level, max_size);
1481 if (level >= max_size) {
1482 /* we don't have the data but if we have a ring buffer that is full, we
1484 GST_DEBUG_OBJECT (queue,
1485 "ring buffer full, reading QUEUE_MAX_BYTES %"
1486 G_GUINT64_FORMAT " bytes", max_size);
1487 read_length = max_size;
1488 } else if (queue->is_eos) {
1489 /* won't get any more data so read any data we have */
1491 GST_DEBUG_OBJECT (queue,
1492 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1494 read_length = level;
1502 if (read_length == 0) {
1503 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1504 GST_DEBUG_OBJECT (queue,
1505 "update current position [%" G_GUINT64_FORMAT "-%"
1506 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1507 update_cur_pos (queue, queue->current, rpos);
1508 GST_QUEUE2_SIGNAL_DEL (queue);
1511 if (queue->use_buffering)
1512 update_buffering (queue);
1514 GST_DEBUG_OBJECT (queue, "waiting for add");
1515 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1519 /* we have the requested data so read it */
1520 read_length = remaining;
1523 /* set range reading_pos to actual reading position for this read */
1524 queue->current->reading_pos = rpos;
1526 /* configure how much and from where to read */
1527 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1529 (queue->current->rb_offset + (rpos -
1530 queue->current->offset)) % rb_size;
1531 if (file_offset + read_length > rb_size) {
1532 block_length = rb_size - file_offset;
1534 block_length = read_length;
1538 block_length = read_length;
1541 /* while we still have data to read, we loop */
1542 while (read_length > 0) {
1546 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1547 data, &read_return);
1548 if (ret != GST_FLOW_OK)
1551 file_offset += read_return;
1552 if (QUEUE_IS_USING_RING_BUFFER (queue))
1553 file_offset %= rb_size;
1555 data += read_return;
1556 read_length -= read_return;
1557 block_length = read_length;
1558 remaining -= read_return;
1560 rpos = (queue->current->reading_pos += read_return);
1561 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1563 GST_QUEUE2_SIGNAL_DEL (queue);
1564 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1567 gst_buffer_unmap (buf, &info);
1568 gst_buffer_resize (buf, 0, length);
1570 GST_BUFFER_OFFSET (buf) = offset;
1571 GST_BUFFER_OFFSET_END (buf) = offset + length;
1580 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1581 gst_buffer_unmap (buf, &info);
1582 if (*buffer == NULL)
1583 gst_buffer_unref (buf);
1584 return GST_FLOW_EOS;
1588 GST_DEBUG_OBJECT (queue, "we are flushing");
1589 gst_buffer_unmap (buf, &info);
1590 if (*buffer == NULL)
1591 gst_buffer_unref (buf);
1592 return GST_FLOW_FLUSHING;
1596 GST_DEBUG_OBJECT (queue, "we have a read error");
1597 gst_buffer_unmap (buf, &info);
1598 if (*buffer == NULL)
1599 gst_buffer_unref (buf);
1604 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE, (NULL),
1605 ("Can't write to buffer"));
1606 if (*buffer == NULL)
1607 gst_buffer_unref (buf);
1608 return GST_FLOW_ERROR;
1612 /* should be called with QUEUE_LOCK */
1613 static GstMiniObject *
1614 gst_queue2_read_item_from_file (GstQueue2 * queue)
1616 GstMiniObject *item;
1618 if (queue->stream_start_event != NULL) {
1619 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1620 queue->stream_start_event = NULL;
1621 } else if (queue->starting_segment != NULL) {
1622 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1623 queue->starting_segment = NULL;
1626 GstBuffer *buffer = NULL;
1627 guint64 reading_pos;
1629 reading_pos = queue->current->reading_pos;
1632 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1637 item = GST_MINI_OBJECT_CAST (buffer);
1640 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1650 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1651 * the temp filename. */
1653 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1658 if (queue->temp_file)
1659 goto already_opened;
1661 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1663 /* If temp_template was set, allocate a filename and open that file */
1666 if (queue->temp_template == NULL)
1669 /* make copy of the template, we don't want to change this */
1670 name = g_strdup (queue->temp_template);
1673 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1675 fd = g_mkstemp (name);
1679 goto mkstemp_failed;
1681 /* open the file for update/writing */
1682 queue->temp_file = fdopen (fd, "wb+");
1683 /* error creating file */
1684 if (queue->temp_file == NULL)
1687 g_free (queue->temp_location);
1688 queue->temp_location = name;
1690 GST_QUEUE2_MUTEX_UNLOCK (queue);
1692 /* we can't emit the notify with the lock */
1693 g_object_notify (G_OBJECT (queue), "temp-location");
1695 GST_QUEUE2_MUTEX_LOCK (queue);
1697 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1704 GST_DEBUG_OBJECT (queue, "temp file was already open");
1709 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1710 (_("No Temp directory specified.")), (NULL));
1715 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1716 (_("Could not create temp file \"%s\"."), queue->temp_template),
1723 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1724 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1733 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1736 if (queue->temp_file == NULL)
1739 GST_DEBUG_OBJECT (queue, "closing temp file");
1741 fflush (queue->temp_file);
1742 fclose (queue->temp_file);
1744 if (queue->temp_remove) {
1745 if (remove (queue->temp_location) < 0) {
1746 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1747 queue->temp_location, g_strerror (errno));
1751 queue->temp_file = NULL;
1752 clean_ranges (queue);
1756 gst_queue2_flush_temp_file (GstQueue2 * queue)
1758 if (queue->temp_file == NULL)
1761 GST_DEBUG_OBJECT (queue, "flushing temp file");
1763 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1767 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1769 if (!QUEUE_IS_USING_QUEUE (queue)) {
1770 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1771 gst_queue2_flush_temp_file (queue);
1772 init_ranges (queue);
1774 while (!g_queue_is_empty (&queue->queue)) {
1775 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1777 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1778 && GST_EVENT_IS_STICKY (qitem->item)
1779 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1780 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1781 gst_pad_store_sticky_event (queue->srcpad,
1782 GST_EVENT_CAST (qitem->item));
1785 /* Then lose another reference because we are supposed to destroy that
1786 data when flushing */
1787 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1788 gst_mini_object_unref (qitem->item);
1789 g_slice_free (GstQueue2Item, qitem);
1792 queue->last_query = FALSE;
1793 g_cond_signal (&queue->query_handled);
1794 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1795 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1796 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1797 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1798 queue->sink_tainted = queue->src_tainted = TRUE;
1799 if (queue->starting_segment != NULL)
1800 gst_event_unref (queue->starting_segment);
1801 queue->starting_segment = NULL;
1802 queue->segment_event_received = FALSE;
1803 gst_event_replace (&queue->stream_start_event, NULL);
1805 /* we deleted a lot of something */
1806 GST_QUEUE2_SIGNAL_DEL (queue);
1810 gst_queue2_wait_free_space (GstQueue2 * queue)
1812 /* We make space available if we're "full" according to whatever
1813 * the user defined as "full". */
1814 if (gst_queue2_is_filled (queue)) {
1817 /* pause the timer while we wait. The fact that we are waiting does not mean
1818 * the byterate on the input pad is lower */
1819 if ((started = queue->in_timer_started))
1820 g_timer_stop (queue->in_timer);
1822 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1823 "queue is full, waiting for free space");
1825 /* Wait for space to be available, we could be unlocked because of a flush. */
1826 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1828 while (gst_queue2_is_filled (queue));
1830 /* and continue if we were running before */
1832 g_timer_continue (queue->in_timer);
1839 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1845 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1848 guint8 *data, *ring_buffer;
1849 guint size, rb_size;
1850 guint64 writing_pos, new_writing_pos;
1851 GstQueue2Range *range, *prev, *next;
1852 gboolean do_seek = FALSE;
1854 if (QUEUE_IS_USING_RING_BUFFER (queue))
1855 writing_pos = queue->current->rb_writing_pos;
1857 writing_pos = queue->current->writing_pos;
1858 ring_buffer = queue->ring_buffer;
1859 rb_size = queue->ring_buffer_max_size;
1861 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
1862 goto buffer_read_error;
1867 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1871 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1872 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1873 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1874 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1875 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1881 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1884 /* calculate the space in the ring buffer not used by data from
1885 * the current range */
1886 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1887 /* wait until there is some free space */
1888 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1890 /* get the amount of space we have */
1891 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1893 /* calculate if we need to split or if we can write the entire
1895 to_write = MIN (size, space);
1897 /* the writing position in the ring buffer after writing (part
1898 * or all of) the buffer */
1899 new_writing_pos = (writing_pos + to_write) % rb_size;
1902 range = queue->ranges;
1904 /* if we need to overwrite data in the ring buffer, we need to
1907 * warning: this code is complicated and includes some
1908 * simplifications - pen, paper and diagrams for the cases
1911 guint64 range_data_start, range_data_end;
1912 GstQueue2Range *range_to_destroy = NULL;
1914 if (range == queue->current)
1917 range_data_start = range->rb_offset;
1918 range_data_end = range->rb_writing_pos;
1920 /* handle the special case where the range has no data in it */
1921 if (range->writing_pos == range->offset) {
1922 if (range != queue->current) {
1923 GST_DEBUG_OBJECT (queue,
1924 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1925 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1927 range_to_destroy = range;
1929 prev->next = range->next;
1934 if (range_data_end > range_data_start) {
1935 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1938 if (new_writing_pos > range_data_start) {
1939 if (new_writing_pos >= range_data_end) {
1940 GST_DEBUG_OBJECT (queue,
1941 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1942 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1944 range_to_destroy = range;
1946 prev->next = range->next;
1948 GST_DEBUG_OBJECT (queue,
1949 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1950 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1951 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1952 range->offset + new_writing_pos - range_data_start,
1954 range->offset += (new_writing_pos - range_data_start);
1955 range->rb_offset = new_writing_pos;
1959 guint64 new_wpos_virt = writing_pos + to_write;
1961 if (new_wpos_virt <= range_data_start)
1964 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1965 GST_DEBUG_OBJECT (queue,
1966 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1967 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1969 range_to_destroy = range;
1971 prev->next = range->next;
1973 GST_DEBUG_OBJECT (queue,
1974 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1975 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1976 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1977 range->offset + new_writing_pos - range_data_start,
1979 range->offset += (new_wpos_virt - range_data_start);
1980 range->rb_offset = new_writing_pos;
1985 if (!range_to_destroy)
1988 range = range->next;
1989 if (range_to_destroy) {
1990 if (range_to_destroy == queue->ranges)
1991 queue->ranges = range;
1992 g_slice_free (GstQueue2Range, range_to_destroy);
1993 range_to_destroy = NULL;
1998 new_writing_pos = writing_pos + to_write;
2001 if (QUEUE_IS_USING_TEMP_FILE (queue)
2002 && FSEEK_FILE (queue->temp_file, writing_pos))
2005 if (new_writing_pos > writing_pos) {
2006 GST_INFO_OBJECT (queue,
2007 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2008 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
2009 queue->current->writing_pos, queue->current->rb_writing_pos);
2010 /* either not using ring buffer or no wrapping, just write */
2011 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2012 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
2015 memcpy (ring_buffer + writing_pos, data, to_write);
2018 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
2019 /* try to merge with next range */
2020 while ((next = queue->current->next)) {
2021 GST_INFO_OBJECT (queue,
2022 "checking merge with next range %" G_GUINT64_FORMAT " < %"
2023 G_GUINT64_FORMAT, new_writing_pos, next->offset);
2024 if (new_writing_pos < next->offset)
2027 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
2030 /* remove the group */
2031 queue->current->next = next->next;
2033 /* We use the threshold to decide if we want to do a seek or simply
2034 * read the data again. If there is not so much data in the range we
2035 * prefer to avoid to seek and read it again. */
2036 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
2037 /* the new range had more data than the threshold, it's worth keeping
2038 * it and doing a seek. */
2039 new_writing_pos = next->writing_pos;
2042 g_slice_free (GstQueue2Range, next);
2044 goto update_and_signal;
2048 guint block_one, block_two;
2050 block_one = rb_size - writing_pos;
2051 block_two = to_write - block_one;
2053 if (block_one > 0) {
2054 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
2055 /* write data to end of ring buffer */
2056 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2057 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
2060 memcpy (ring_buffer + writing_pos, data, block_one);
2064 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
2067 if (block_two > 0) {
2068 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
2069 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2070 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
2073 memcpy (ring_buffer, data + block_one, block_two);
2079 /* update the writing positions */
2081 GST_INFO_OBJECT (queue,
2082 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2083 to_write, writing_pos, size);
2085 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2087 queue->current->writing_pos += to_write;
2088 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2090 queue->current->writing_pos = writing_pos = new_writing_pos;
2093 perform_seek_to_offset (queue, new_writing_pos);
2095 update_cur_level (queue, queue->current);
2097 /* update the buffering status */
2098 if (queue->use_buffering) {
2100 update_buffering (queue);
2101 msg = gst_queue2_get_buffering_message (queue);
2103 GST_QUEUE2_MUTEX_UNLOCK (queue);
2104 g_mutex_lock (&queue->buffering_post_lock);
2105 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
2106 g_mutex_unlock (&queue->buffering_post_lock);
2107 GST_QUEUE2_MUTEX_LOCK (queue);
2111 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2112 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2114 GST_QUEUE2_SIGNAL_ADD (queue);
2117 gst_buffer_unmap (buffer, &info);
2124 GST_DEBUG_OBJECT (queue, "we are flushing");
2125 gst_buffer_unmap (buffer, &info);
2126 /* FIXME - GST_FLOW_EOS ? */
2131 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2132 gst_buffer_unmap (buffer, &info);
2139 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2143 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2144 (_("Error while writing to download file.")),
2145 ("%s", g_strerror (errno)));
2148 gst_buffer_unmap (buffer, &info);
2153 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL),
2154 ("Can't read from buffer"));
2160 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2162 GstQueue2 *queue = q;
2164 GST_TRACE_OBJECT (queue,
2165 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2166 gst_buffer_get_size (*buf));
2168 if (!gst_queue2_create_write (queue, *buf)) {
2169 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2176 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2178 guint *p_size = data;
2181 buf_size = gst_buffer_get_size (*buf);
2182 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2183 *p_size += buf_size;
2187 /* enqueue an item an update the level stats */
2189 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2190 GstQueue2ItemType item_type)
2192 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2196 buffer = GST_BUFFER_CAST (item);
2197 size = gst_buffer_get_size (buffer);
2199 /* add buffer to the statistics */
2200 if (QUEUE_IS_USING_QUEUE (queue)) {
2201 queue->cur_level.buffers++;
2202 queue->cur_level.bytes += size;
2204 queue->bytes_in += size;
2206 /* apply new buffer to segment stats */
2207 apply_buffer (queue, buffer, &queue->sink_segment, size, TRUE);
2208 /* update the byterate stats */
2209 update_in_rates (queue, FALSE);
2211 if (!QUEUE_IS_USING_QUEUE (queue)) {
2212 /* FIXME - check return value? */
2213 gst_queue2_create_write (queue, buffer);
2215 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2216 GstBufferList *buffer_list;
2219 buffer_list = GST_BUFFER_LIST_CAST (item);
2221 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2222 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2224 /* add buffer to the statistics */
2225 if (QUEUE_IS_USING_QUEUE (queue)) {
2226 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2227 queue->cur_level.bytes += size;
2229 queue->bytes_in += size;
2231 /* apply new buffer to segment stats */
2232 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2234 /* update the byterate stats */
2235 update_in_rates (queue, FALSE);
2237 if (!QUEUE_IS_USING_QUEUE (queue)) {
2238 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2240 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2243 event = GST_EVENT_CAST (item);
2245 switch (GST_EVENT_TYPE (event)) {
2247 /* Zero the thresholds, this makes sure the queue is completely
2248 * filled and we can read all data from the queue. */
2249 GST_DEBUG_OBJECT (queue, "we have EOS");
2250 queue->is_eos = TRUE;
2251 /* Force updating the input bitrate */
2252 update_in_rates (queue, TRUE);
2254 case GST_EVENT_SEGMENT:
2255 apply_segment (queue, event, &queue->sink_segment, TRUE);
2256 /* This is our first new segment, we hold it
2257 * as we can't save it on the temp file */
2258 if (!QUEUE_IS_USING_QUEUE (queue)) {
2259 if (queue->segment_event_received)
2260 goto unexpected_event;
2262 queue->segment_event_received = TRUE;
2263 if (queue->starting_segment != NULL)
2264 gst_event_unref (queue->starting_segment);
2265 queue->starting_segment = event;
2268 /* a new segment allows us to accept more buffers if we got EOS
2269 * from downstream */
2270 queue->unexpected = FALSE;
2273 apply_gap (queue, event, &queue->sink_segment, TRUE);
2275 case GST_EVENT_STREAM_START:
2276 if (!QUEUE_IS_USING_QUEUE (queue)) {
2277 gst_event_replace (&queue->stream_start_event, event);
2278 gst_event_unref (event);
2282 case GST_EVENT_CAPS:{
2285 gst_event_parse_caps (event, &caps);
2286 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2288 if (!QUEUE_IS_USING_QUEUE (queue)) {
2289 GST_LOG ("Dropping caps event, not using queue");
2290 gst_event_unref (event);
2296 if (!QUEUE_IS_USING_QUEUE (queue))
2297 goto unexpected_event;
2300 } else if (GST_IS_QUERY (item)) {
2301 /* Can't happen as we check that in the caller */
2302 if (!QUEUE_IS_USING_QUEUE (queue))
2303 g_assert_not_reached ();
2305 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2306 item, GST_OBJECT_NAME (queue));
2307 /* we can't really unref since we don't know what it is */
2312 /* update the buffering status */
2313 if (queue->use_buffering)
2314 update_buffering (queue);
2316 if (QUEUE_IS_USING_QUEUE (queue)) {
2317 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2318 qitem->type = item_type;
2320 g_queue_push_tail (&queue->queue, qitem);
2322 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2325 GST_QUEUE2_SIGNAL_ADD (queue);
2333 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2335 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2336 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2337 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2338 gst_event_unref (GST_EVENT_CAST (item));
2343 /* dequeue an item from the queue and update level stats */
2344 static GstMiniObject *
2345 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2347 GstMiniObject *item;
2349 if (!QUEUE_IS_USING_QUEUE (queue)) {
2350 item = gst_queue2_read_item_from_file (queue);
2352 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2358 g_slice_free (GstQueue2Item, qitem);
2364 if (GST_IS_BUFFER (item)) {
2368 buffer = GST_BUFFER_CAST (item);
2369 size = gst_buffer_get_size (buffer);
2370 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2372 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2373 "retrieved buffer %p from queue", buffer);
2375 if (QUEUE_IS_USING_QUEUE (queue)) {
2376 queue->cur_level.buffers--;
2377 queue->cur_level.bytes -= size;
2379 queue->bytes_out += size;
2381 apply_buffer (queue, buffer, &queue->src_segment, size, FALSE);
2382 /* update the byterate stats */
2383 update_out_rates (queue);
2384 /* update the buffering */
2385 if (queue->use_buffering)
2386 update_buffering (queue);
2388 } else if (GST_IS_EVENT (item)) {
2389 GstEvent *event = GST_EVENT_CAST (item);
2391 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2393 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2394 "retrieved event %p from queue", event);
2396 switch (GST_EVENT_TYPE (event)) {
2398 /* queue is empty now that we dequeued the EOS */
2399 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2401 case GST_EVENT_SEGMENT:
2402 apply_segment (queue, event, &queue->src_segment, FALSE);
2405 apply_gap (queue, event, &queue->src_segment, FALSE);
2410 } else if (GST_IS_BUFFER_LIST (item)) {
2411 GstBufferList *buffer_list;
2414 buffer_list = GST_BUFFER_LIST_CAST (item);
2415 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2416 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2418 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2419 "retrieved buffer list %p from queue", buffer_list);
2421 if (QUEUE_IS_USING_QUEUE (queue)) {
2422 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2423 queue->cur_level.bytes -= size;
2425 queue->bytes_out += size;
2427 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2428 /* update the byterate stats */
2429 update_out_rates (queue);
2430 /* update the buffering */
2431 if (queue->use_buffering)
2432 update_buffering (queue);
2433 } else if (GST_IS_QUERY (item)) {
2434 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2435 "retrieved query %p from queue", item);
2436 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2439 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2440 item, GST_OBJECT_NAME (queue));
2442 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2444 GST_QUEUE2_SIGNAL_DEL (queue);
2451 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2456 static GstFlowReturn
2457 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2460 gboolean ret = TRUE;
2463 queue = GST_QUEUE2 (parent);
2465 switch (GST_EVENT_TYPE (event)) {
2466 case GST_EVENT_FLUSH_START:
2468 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2469 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2471 ret = gst_pad_push_event (queue->srcpad, event);
2473 /* now unblock the chain function */
2474 GST_QUEUE2_MUTEX_LOCK (queue);
2475 queue->srcresult = GST_FLOW_FLUSHING;
2476 queue->sinkresult = GST_FLOW_FLUSHING;
2477 /* unblock the loop and chain functions */
2478 GST_QUEUE2_SIGNAL_ADD (queue);
2479 GST_QUEUE2_SIGNAL_DEL (queue);
2480 GST_QUEUE2_MUTEX_UNLOCK (queue);
2482 /* make sure it pauses, this should happen since we sent
2483 * flush_start downstream. */
2484 gst_pad_pause_task (queue->srcpad);
2485 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2487 GST_QUEUE2_MUTEX_LOCK (queue);
2488 queue->last_query = FALSE;
2489 g_cond_signal (&queue->query_handled);
2490 GST_QUEUE2_MUTEX_UNLOCK (queue);
2492 GST_QUEUE2_MUTEX_LOCK (queue);
2493 /* flush the sink pad */
2494 queue->sinkresult = GST_FLOW_FLUSHING;
2495 GST_QUEUE2_SIGNAL_DEL (queue);
2496 queue->last_query = FALSE;
2497 g_cond_signal (&queue->query_handled);
2498 GST_QUEUE2_MUTEX_UNLOCK (queue);
2500 gst_event_unref (event);
2504 case GST_EVENT_FLUSH_STOP:
2506 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2508 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2510 ret = gst_pad_push_event (queue->srcpad, event);
2512 GST_QUEUE2_MUTEX_LOCK (queue);
2513 gst_queue2_locked_flush (queue, FALSE, TRUE);
2514 queue->srcresult = GST_FLOW_OK;
2515 queue->sinkresult = GST_FLOW_OK;
2516 queue->is_eos = FALSE;
2517 queue->unexpected = FALSE;
2518 queue->seeking = FALSE;
2519 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2520 /* reset rate counters */
2521 reset_rate_timer (queue);
2522 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2523 queue->srcpad, NULL);
2524 GST_QUEUE2_MUTEX_UNLOCK (queue);
2526 GST_QUEUE2_MUTEX_LOCK (queue);
2527 queue->segment_event_received = FALSE;
2528 queue->is_eos = FALSE;
2529 queue->unexpected = FALSE;
2530 queue->sinkresult = GST_FLOW_OK;
2531 queue->seeking = FALSE;
2532 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2533 GST_QUEUE2_MUTEX_UNLOCK (queue);
2535 gst_event_unref (event);
2539 case GST_EVENT_TAG:{
2540 if (queue->use_tags_bitrate) {
2544 gst_event_parse_tag (event, &tags);
2545 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2546 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2547 GST_QUEUE2_MUTEX_LOCK (queue);
2548 queue->sink_tags_bitrate = bitrate;
2549 GST_QUEUE2_MUTEX_UNLOCK (queue);
2550 GST_LOG_OBJECT (queue, "Sink pad bitrate from tags now %u", bitrate);
2556 if (GST_EVENT_IS_SERIALIZED (event)) {
2557 /* serialized events go in the queue */
2558 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2559 if (queue->srcresult != GST_FLOW_OK) {
2560 /* Errors in sticky event pushing are no problem and ignored here
2561 * as they will cause more meaningful errors during data flow.
2562 * For EOS events, that are not followed by data flow, we still
2563 * return FALSE here though and report an error.
2565 if (!GST_EVENT_IS_STICKY (event)) {
2566 goto out_flow_error;
2567 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2568 if (queue->srcresult == GST_FLOW_NOT_LINKED
2569 || queue->srcresult < GST_FLOW_EOS) {
2570 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
2572 goto out_flow_error;
2575 /* refuse more events on EOS */
2578 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2579 GST_QUEUE2_MUTEX_UNLOCK (queue);
2580 gst_queue2_post_buffering (queue);
2582 /* non-serialized events are passed upstream. */
2583 ret = gst_pad_push_event (queue->srcpad, event);
2588 return GST_FLOW_ERROR;
2594 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2595 GST_QUEUE2_MUTEX_UNLOCK (queue);
2596 gst_event_unref (event);
2597 return GST_FLOW_FLUSHING;
2601 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2602 GST_QUEUE2_MUTEX_UNLOCK (queue);
2603 gst_event_unref (event);
2604 return GST_FLOW_EOS;
2608 GST_LOG_OBJECT (queue,
2609 "refusing event, we have a downstream flow error: %s",
2610 gst_flow_get_name (queue->srcresult));
2611 GST_QUEUE2_MUTEX_UNLOCK (queue);
2612 gst_event_unref (event);
2613 return queue->srcresult;
2618 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2624 queue = GST_QUEUE2 (parent);
2626 switch (GST_QUERY_TYPE (query)) {
2628 if (GST_QUERY_IS_SERIALIZED (query)) {
2629 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2630 /* serialized events go in the queue. We need to be certain that we
2631 * don't cause deadlocks waiting for the query return value. We check if
2632 * the queue is empty (nothing is blocking downstream and the query can
2633 * be pushed for sure) or we are not buffering. If we are buffering,
2634 * the pipeline waits to unblock downstream until our queue fills up
2635 * completely, which can not happen if we block on the query..
2636 * Therefore we only potentially block when we are not buffering. */
2637 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2638 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2639 || !queue->use_buffering)) {
2640 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2641 gst_queue2_locked_enqueue (queue, query,
2642 GST_QUEUE2_ITEM_TYPE_QUERY);
2644 STATUS (queue, queue->sinkpad, "wait for QUERY");
2645 g_cond_wait (&queue->query_handled, &queue->qlock);
2646 if (queue->sinkresult != GST_FLOW_OK)
2648 res = queue->last_query;
2650 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2654 GST_DEBUG_OBJECT (queue,
2655 "refusing query, we are not using the queue");
2658 GST_QUEUE2_MUTEX_UNLOCK (queue);
2659 gst_queue2_post_buffering (queue);
2661 res = gst_pad_query_default (pad, parent, query);
2670 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2671 GST_QUEUE2_MUTEX_UNLOCK (queue);
2677 gst_queue2_is_empty (GstQueue2 * queue)
2679 /* never empty on EOS */
2683 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2684 return queue->current->writing_pos <= queue->current->max_reading_pos;
2686 if (queue->queue.length == 0)
2694 gst_queue2_is_filled (GstQueue2 * queue)
2698 /* always filled on EOS */
2702 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2703 (queue->cur_level.format) >= ((alt_max) ? \
2704 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2706 /* if using a ring buffer we're filled if all ring buffer space is used
2707 * _by the current range_ */
2708 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2709 guint64 rb_size = queue->ring_buffer_max_size;
2710 GST_DEBUG_OBJECT (queue,
2711 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2712 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2713 return CHECK_FILLED (bytes, rb_size);
2716 /* if using file, we're never filled if we don't have EOS */
2717 if (QUEUE_IS_USING_TEMP_FILE (queue))
2720 /* we are never filled when we have no buffers at all */
2721 if (queue->cur_level.buffers == 0)
2724 /* we are filled if one of the current levels exceeds the max */
2725 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2726 || CHECK_FILLED (time, 0);
2728 /* if we need to, use the rate estimate to check against the max time we are
2729 * allowed to queue */
2730 if (queue->use_rate_estimate)
2731 res |= CHECK_FILLED (rate_time, 0);
2737 static GstFlowReturn
2738 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2739 GstMiniObject * item, GstQueue2ItemType item_type)
2741 /* we have to lock the queue since we span threads */
2742 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2743 /* when we received EOS, we refuse more data */
2746 /* when we received unexpected from downstream, refuse more buffers */
2747 if (queue->unexpected)
2748 goto out_unexpected;
2750 /* while we didn't receive the newsegment, we're seeking and we skip data */
2754 if (!gst_queue2_wait_free_space (queue))
2757 /* put buffer in queue now */
2758 gst_queue2_locked_enqueue (queue, item, item_type);
2759 GST_QUEUE2_MUTEX_UNLOCK (queue);
2760 gst_queue2_post_buffering (queue);
2764 /* special conditions */
2767 GstFlowReturn ret = queue->sinkresult;
2769 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2770 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2771 GST_QUEUE2_MUTEX_UNLOCK (queue);
2772 gst_mini_object_unref (item);
2778 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2779 GST_QUEUE2_MUTEX_UNLOCK (queue);
2780 gst_mini_object_unref (item);
2782 return GST_FLOW_EOS;
2786 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2787 GST_QUEUE2_MUTEX_UNLOCK (queue);
2788 gst_mini_object_unref (item);
2794 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2795 GST_QUEUE2_MUTEX_UNLOCK (queue);
2796 gst_mini_object_unref (item);
2798 return GST_FLOW_EOS;
2802 static GstFlowReturn
2803 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2807 queue = GST_QUEUE2 (parent);
2809 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2810 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2811 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2812 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2813 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2815 return gst_queue2_chain_buffer_or_buffer_list (queue,
2816 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2819 static GstFlowReturn
2820 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2821 GstBufferList * buffer_list)
2825 queue = GST_QUEUE2 (parent);
2827 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2828 "received buffer list %p", buffer_list);
2830 return gst_queue2_chain_buffer_or_buffer_list (queue,
2831 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2834 static GstMiniObject *
2835 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2837 GstMiniObject *data;
2839 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2841 /* stop pushing buffers, we dequeue all items until we see an item that we
2842 * can push again, which is EOS or SEGMENT. If there is nothing in the
2843 * queue we can push, we set a flag to make the sinkpad refuse more
2844 * buffers with an EOS return value until we receive something
2845 * pushable again or we get flushed. */
2846 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2847 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2848 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2849 "dropping EOS buffer %p", data);
2850 gst_buffer_unref (GST_BUFFER_CAST (data));
2851 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2852 GstEvent *event = GST_EVENT_CAST (data);
2853 GstEventType type = GST_EVENT_TYPE (event);
2855 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2856 /* we found a pushable item in the queue, push it out */
2857 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2858 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2861 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2862 "dropping EOS event %p", event);
2863 gst_event_unref (event);
2864 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2865 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2866 "dropping EOS buffer list %p", data);
2867 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2868 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2869 queue->last_query = FALSE;
2870 g_cond_signal (&queue->query_handled);
2871 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2874 /* no more items in the queue. Set the unexpected flag so that upstream
2875 * make us refuse any more buffers on the sinkpad. Since we will still
2876 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2877 * task function does not shut down. */
2878 queue->unexpected = TRUE;
2882 /* dequeue an item from the queue an push it downstream. This functions returns
2883 * the result of the push. */
2884 static GstFlowReturn
2885 gst_queue2_push_one (GstQueue2 * queue)
2887 GstFlowReturn result = queue->srcresult;
2888 GstMiniObject *data;
2889 GstQueue2ItemType item_type;
2891 data = gst_queue2_locked_dequeue (queue, &item_type);
2896 STATUS (queue, queue->srcpad, "We have something dequeud");
2897 g_atomic_int_set (&queue->downstream_may_block,
2898 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2899 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2900 GST_QUEUE2_MUTEX_UNLOCK (queue);
2901 gst_queue2_post_buffering (queue);
2903 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2906 buffer = GST_BUFFER_CAST (data);
2908 result = gst_pad_push (queue->srcpad, buffer);
2909 g_atomic_int_set (&queue->downstream_may_block, 0);
2911 /* need to check for srcresult here as well */
2912 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2913 if (result == GST_FLOW_EOS) {
2914 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2917 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2918 * to the caller so that the task function does not shut down */
2919 result = GST_FLOW_OK;
2921 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2922 GstEvent *event = GST_EVENT_CAST (data);
2923 GstEventType type = GST_EVENT_TYPE (event);
2925 if (type == GST_EVENT_TAG) {
2926 if (queue->use_tags_bitrate) {
2930 gst_event_parse_tag (event, &tags);
2931 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2932 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2933 GST_QUEUE2_MUTEX_LOCK (queue);
2934 queue->src_tags_bitrate = bitrate;
2935 GST_QUEUE2_MUTEX_UNLOCK (queue);
2936 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
2941 gst_pad_push_event (queue->srcpad, event);
2943 /* if we're EOS, return EOS so that the task pauses. */
2944 if (type == GST_EVENT_EOS) {
2945 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2946 "pushed EOS event %p, return EOS", event);
2947 result = GST_FLOW_EOS;
2950 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2951 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2952 GstBufferList *buffer_list;
2954 buffer_list = GST_BUFFER_LIST_CAST (data);
2956 result = gst_pad_push_list (queue->srcpad, buffer_list);
2957 g_atomic_int_set (&queue->downstream_may_block, 0);
2959 /* need to check for srcresult here as well */
2960 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2961 if (result == GST_FLOW_EOS) {
2962 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2965 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2966 * to the caller so that the task function does not shut down */
2967 result = GST_FLOW_OK;
2969 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2970 GstQuery *query = GST_QUERY_CAST (data);
2972 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2973 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2974 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2975 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2976 "did query %p, return %d", query, queue->last_query);
2977 g_cond_signal (&queue->query_handled);
2978 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2979 result = GST_FLOW_OK;
2986 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2987 "exit because we have no item in the queue");
2988 return GST_FLOW_ERROR;
2992 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2993 return GST_FLOW_FLUSHING;
2997 /* called repeatedly with @pad as the source pad. This function should push out
2998 * data to the peer element. */
3000 gst_queue2_loop (GstPad * pad)
3005 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
3007 /* have to lock for thread-safety */
3008 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3010 if (gst_queue2_is_empty (queue)) {
3013 /* pause the timer while we wait. The fact that we are waiting does not mean
3014 * the byterate on the output pad is lower */
3015 if ((started = queue->out_timer_started))
3016 g_timer_stop (queue->out_timer);
3018 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
3019 "queue is empty, waiting for new data");
3021 /* Wait for data to be available, we could be unlocked because of a flush. */
3022 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
3024 while (gst_queue2_is_empty (queue));
3026 /* and continue if we were running before */
3028 g_timer_continue (queue->out_timer);
3030 ret = gst_queue2_push_one (queue);
3031 queue->srcresult = ret;
3032 queue->sinkresult = ret;
3033 if (ret != GST_FLOW_OK)
3036 GST_QUEUE2_MUTEX_UNLOCK (queue);
3037 gst_queue2_post_buffering (queue);
3044 gboolean eos = queue->is_eos;
3045 GstFlowReturn ret = queue->srcresult;
3047 gst_pad_pause_task (queue->srcpad);
3048 if (ret == GST_FLOW_FLUSHING) {
3049 gst_queue2_locked_flush (queue, FALSE, FALSE);
3051 GST_QUEUE2_SIGNAL_DEL (queue);
3052 queue->last_query = FALSE;
3053 g_cond_signal (&queue->query_handled);
3055 GST_QUEUE2_MUTEX_UNLOCK (queue);
3056 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3057 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
3058 /* let app know about us giving up if upstream is not expected to do so */
3059 /* EOS is already taken care of elsewhere */
3060 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
3061 GST_ELEMENT_FLOW_ERROR (queue, ret);
3062 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
3069 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
3071 gboolean res = TRUE;
3072 GstQueue2 *queue = GST_QUEUE2 (parent);
3074 #ifndef GST_DISABLE_GST_DEBUG
3075 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
3076 event, GST_EVENT_TYPE_NAME (event));
3079 switch (GST_EVENT_TYPE (event)) {
3080 case GST_EVENT_FLUSH_START:
3081 if (QUEUE_IS_USING_QUEUE (queue)) {
3082 /* just forward upstream */
3083 res = gst_pad_push_event (queue->sinkpad, event);
3085 /* now unblock the getrange function */
3086 GST_QUEUE2_MUTEX_LOCK (queue);
3087 GST_DEBUG_OBJECT (queue, "flushing");
3088 queue->srcresult = GST_FLOW_FLUSHING;
3089 GST_QUEUE2_SIGNAL_ADD (queue);
3090 GST_QUEUE2_MUTEX_UNLOCK (queue);
3092 /* when using a temp file, we eat the event */
3094 gst_event_unref (event);
3097 case GST_EVENT_FLUSH_STOP:
3098 if (QUEUE_IS_USING_QUEUE (queue)) {
3099 /* just forward upstream */
3100 res = gst_pad_push_event (queue->sinkpad, event);
3102 /* now unblock the getrange function */
3103 GST_QUEUE2_MUTEX_LOCK (queue);
3104 queue->srcresult = GST_FLOW_OK;
3105 GST_QUEUE2_MUTEX_UNLOCK (queue);
3107 /* when using a temp file, we eat the event */
3109 gst_event_unref (event);
3112 case GST_EVENT_RECONFIGURE:
3113 GST_QUEUE2_MUTEX_LOCK (queue);
3114 /* assume downstream is linked now and try to push again */
3115 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3116 queue->srcresult = GST_FLOW_OK;
3117 queue->sinkresult = GST_FLOW_OK;
3118 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3119 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3123 GST_QUEUE2_MUTEX_UNLOCK (queue);
3125 res = gst_pad_push_event (queue->sinkpad, event);
3128 res = gst_pad_push_event (queue->sinkpad, event);
3136 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3140 queue = GST_QUEUE2 (parent);
3142 switch (GST_QUERY_TYPE (query)) {
3143 case GST_QUERY_POSITION:
3148 if (!gst_pad_peer_query (queue->sinkpad, query))
3151 /* get peer position */
3152 gst_query_parse_position (query, &format, &peer_pos);
3154 /* FIXME: this code assumes that there's no discont in the queue */
3156 case GST_FORMAT_BYTES:
3157 peer_pos -= queue->cur_level.bytes;
3159 case GST_FORMAT_TIME:
3160 peer_pos -= queue->cur_level.time;
3163 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3164 "know how to adjust value", gst_format_get_name (format));
3167 /* set updated position */
3168 gst_query_set_position (query, format, peer_pos);
3171 case GST_QUERY_DURATION:
3173 GST_DEBUG_OBJECT (queue, "doing peer query");
3175 if (!gst_pad_peer_query (queue->sinkpad, query))
3178 GST_DEBUG_OBJECT (queue, "peer query success");
3181 case GST_QUERY_BUFFERING:
3184 gboolean is_buffering;
3185 GstBufferingMode mode;
3186 gint avg_in, avg_out;
3187 gint64 buffering_left;
3189 GST_DEBUG_OBJECT (queue, "query buffering");
3191 get_buffering_level (queue, &is_buffering, &percent);
3192 percent = convert_to_buffering_percent (queue, percent);
3193 gst_query_set_buffering_percent (query, is_buffering, percent);
3195 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3197 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3200 if (!QUEUE_IS_USING_QUEUE (queue)) {
3201 /* add ranges for download and ringbuffer buffering */
3203 gint64 start, stop, range_start, range_stop;
3204 guint64 writing_pos;
3205 gint64 estimated_total;
3207 gboolean peer_res, is_eos;
3208 GstQueue2Range *queued_ranges;
3210 /* we need a current download region */
3211 if (queue->current == NULL)
3214 writing_pos = queue->current->writing_pos;
3215 is_eos = queue->is_eos;
3218 /* we're EOS, we know the duration in bytes now */
3220 duration = writing_pos;
3222 /* get duration of upstream in bytes */
3223 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3224 GST_FORMAT_BYTES, &duration);
3227 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3228 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3230 /* calculate remaining and total download time */
3231 if (peer_res && avg_in > 0.0)
3232 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3234 estimated_total = -1;
3236 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3239 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3242 case GST_FORMAT_PERCENT:
3243 /* we need duration */
3248 /* get our available data relative to the duration */
3251 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3256 case GST_FORMAT_BYTES:
3266 /* fill out the buffered ranges */
3267 for (queued_ranges = queue->ranges; queued_ranges;
3268 queued_ranges = queued_ranges->next) {
3270 case GST_FORMAT_PERCENT:
3271 if (duration == -1) {
3277 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3278 queued_ranges->offset, duration);
3280 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3281 queued_ranges->writing_pos, duration);
3283 case GST_FORMAT_BYTES:
3284 range_start = queued_ranges->offset;
3285 range_stop = queued_ranges->writing_pos;
3292 if (range_start == range_stop)
3294 GST_DEBUG_OBJECT (queue,
3295 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3296 G_GINT64_FORMAT, range_start, range_stop);
3297 gst_query_add_buffering_range (query, range_start, range_stop);
3300 gst_query_set_buffering_range (query, format, start, stop,
3305 case GST_QUERY_SCHEDULING:
3308 GstSchedulingFlags flags = 0;
3310 if (!gst_pad_peer_query (queue->sinkpad, query))
3313 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3315 /* we can operate in pull mode when we are using a tempfile */
3316 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3319 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3320 gst_query_set_scheduling (query, flags, 0, -1, 0);
3322 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3323 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3327 /* peer handled other queries */
3328 if (!gst_pad_query_default (pad, parent, query))
3338 GST_DEBUG_OBJECT (queue, "failed peer query");
3344 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3346 GstQueue2 *queue = GST_QUEUE2 (element);
3348 /* simply forward to the srcpad query function */
3349 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3354 gst_queue2_update_upstream_size (GstQueue2 * queue)
3356 gint64 upstream_size = -1;
3358 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3360 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3362 /* upstream_size can be negative but queue->upstream_size is unsigned.
3363 * Prevent setting negative values to it (the query can return -1) */
3364 if (upstream_size >= 0)
3365 queue->upstream_size = upstream_size;
3367 queue->upstream_size = 0;
3371 static GstFlowReturn
3372 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3373 guint length, GstBuffer ** buffer)
3378 queue = GST_QUEUE2_CAST (parent);
3380 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3381 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3382 offset = (offset == -1) ? queue->current->reading_pos : offset;
3384 GST_DEBUG_OBJECT (queue,
3385 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3387 /* catch any reads beyond the size of the file here to make sure queue2
3388 * doesn't send seek events beyond the size of the file upstream, since
3389 * that would confuse elements such as souphttpsrc and/or http servers.
3390 * Demuxers often just loop until EOS at the end of the file to figure out
3391 * when they've read all the end-headers or index chunks. */
3392 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3393 gst_queue2_update_upstream_size (queue);
3394 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3395 goto out_unexpected;
3398 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3399 gst_queue2_update_upstream_size (queue);
3400 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3401 length = queue->upstream_size - offset;
3402 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3406 /* FIXME - function will block when the range is not yet available */
3407 ret = gst_queue2_create_read (queue, offset, length, buffer);
3408 GST_QUEUE2_MUTEX_UNLOCK (queue);
3409 gst_queue2_post_buffering (queue);
3416 ret = queue->srcresult;
3418 GST_DEBUG_OBJECT (queue, "we are flushing");
3419 GST_QUEUE2_MUTEX_UNLOCK (queue);
3424 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3425 GST_QUEUE2_MUTEX_UNLOCK (queue);
3426 return GST_FLOW_EOS;
3430 /* sink currently only operates in push mode */
3432 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3433 GstPadMode mode, gboolean active)
3438 queue = GST_QUEUE2 (parent);
3441 case GST_PAD_MODE_PUSH:
3443 GST_QUEUE2_MUTEX_LOCK (queue);
3444 GST_DEBUG_OBJECT (queue, "activating push mode");
3445 queue->srcresult = GST_FLOW_OK;
3446 queue->sinkresult = GST_FLOW_OK;
3447 queue->is_eos = FALSE;
3448 queue->unexpected = FALSE;
3449 reset_rate_timer (queue);
3450 GST_QUEUE2_MUTEX_UNLOCK (queue);
3452 /* unblock chain function */
3453 GST_QUEUE2_MUTEX_LOCK (queue);
3454 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3455 queue->srcresult = GST_FLOW_FLUSHING;
3456 queue->sinkresult = GST_FLOW_FLUSHING;
3457 GST_QUEUE2_SIGNAL_DEL (queue);
3458 GST_QUEUE2_MUTEX_UNLOCK (queue);
3460 /* wait until it is unblocked and clean up */
3461 GST_PAD_STREAM_LOCK (pad);
3462 GST_QUEUE2_MUTEX_LOCK (queue);
3463 gst_queue2_locked_flush (queue, TRUE, FALSE);
3464 GST_QUEUE2_MUTEX_UNLOCK (queue);
3465 GST_PAD_STREAM_UNLOCK (pad);
3476 /* src operating in push mode, we start a task on the source pad that pushes out
3477 * buffers from the queue */
3479 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3481 gboolean result = FALSE;
3484 queue = GST_QUEUE2 (parent);
3487 GST_QUEUE2_MUTEX_LOCK (queue);
3488 GST_DEBUG_OBJECT (queue, "activating push mode");
3489 queue->srcresult = GST_FLOW_OK;
3490 queue->sinkresult = GST_FLOW_OK;
3491 queue->is_eos = FALSE;
3492 queue->unexpected = FALSE;
3494 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3495 GST_QUEUE2_MUTEX_UNLOCK (queue);
3497 /* unblock loop function */
3498 GST_QUEUE2_MUTEX_LOCK (queue);
3499 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3500 queue->srcresult = GST_FLOW_FLUSHING;
3501 queue->sinkresult = GST_FLOW_FLUSHING;
3502 /* the item add signal will unblock */
3503 GST_QUEUE2_SIGNAL_ADD (queue);
3504 GST_QUEUE2_MUTEX_UNLOCK (queue);
3506 /* step 2, make sure streaming finishes */
3507 result = gst_pad_stop_task (pad);
3513 /* pull mode, downstream will call our getrange function */
3515 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3520 queue = GST_QUEUE2 (parent);
3523 GST_QUEUE2_MUTEX_LOCK (queue);
3524 if (!QUEUE_IS_USING_QUEUE (queue)) {
3525 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3526 /* open the temp file now */
3527 result = gst_queue2_open_temp_location_file (queue);
3528 } else if (!queue->ring_buffer) {
3529 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3530 result = ! !queue->ring_buffer;
3535 GST_DEBUG_OBJECT (queue, "activating pull mode");
3536 init_ranges (queue);
3537 queue->srcresult = GST_FLOW_OK;
3538 queue->sinkresult = GST_FLOW_OK;
3539 queue->is_eos = FALSE;
3540 queue->unexpected = FALSE;
3541 queue->upstream_size = 0;
3543 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3544 /* this is not allowed, we cannot operate in pull mode without a temp
3546 queue->srcresult = GST_FLOW_FLUSHING;
3547 queue->sinkresult = GST_FLOW_FLUSHING;
3550 GST_QUEUE2_MUTEX_UNLOCK (queue);
3552 GST_QUEUE2_MUTEX_LOCK (queue);
3553 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3554 queue->srcresult = GST_FLOW_FLUSHING;
3555 queue->sinkresult = GST_FLOW_FLUSHING;
3556 /* this will unlock getrange */
3557 GST_QUEUE2_SIGNAL_ADD (queue);
3559 GST_QUEUE2_MUTEX_UNLOCK (queue);
3566 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3572 case GST_PAD_MODE_PULL:
3573 res = gst_queue2_src_activate_pull (pad, parent, active);
3575 case GST_PAD_MODE_PUSH:
3576 res = gst_queue2_src_activate_push (pad, parent, active);
3579 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3586 static GstStateChangeReturn
3587 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3590 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3592 queue = GST_QUEUE2 (element);
3594 switch (transition) {
3595 case GST_STATE_CHANGE_NULL_TO_READY:
3597 case GST_STATE_CHANGE_READY_TO_PAUSED:
3598 GST_QUEUE2_MUTEX_LOCK (queue);
3599 if (!QUEUE_IS_USING_QUEUE (queue)) {
3600 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3601 if (!gst_queue2_open_temp_location_file (queue))
3602 ret = GST_STATE_CHANGE_FAILURE;
3604 if (queue->ring_buffer) {
3605 g_free (queue->ring_buffer);
3606 queue->ring_buffer = NULL;
3608 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3609 ret = GST_STATE_CHANGE_FAILURE;
3611 init_ranges (queue);
3613 queue->segment_event_received = FALSE;
3614 queue->starting_segment = NULL;
3615 gst_event_replace (&queue->stream_start_event, NULL);
3616 GST_QUEUE2_MUTEX_UNLOCK (queue);
3618 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3624 if (ret == GST_STATE_CHANGE_FAILURE)
3627 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3629 if (ret == GST_STATE_CHANGE_FAILURE)
3632 switch (transition) {
3633 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3635 case GST_STATE_CHANGE_PAUSED_TO_READY:
3636 GST_QUEUE2_MUTEX_LOCK (queue);
3637 if (!QUEUE_IS_USING_QUEUE (queue)) {
3638 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3639 gst_queue2_close_temp_location_file (queue);
3640 } else if (queue->ring_buffer) {
3641 g_free (queue->ring_buffer);
3642 queue->ring_buffer = NULL;
3644 clean_ranges (queue);
3646 if (queue->starting_segment != NULL) {
3647 gst_event_unref (queue->starting_segment);
3648 queue->starting_segment = NULL;
3650 gst_event_replace (&queue->stream_start_event, NULL);
3651 GST_QUEUE2_MUTEX_UNLOCK (queue);
3653 case GST_STATE_CHANGE_READY_TO_NULL:
3662 /* changing the capacity of the queue must wake up
3663 * the _chain function, it might have more room now
3664 * to store the buffer/event in the queue */
3665 #define QUEUE_CAPACITY_CHANGE(q) \
3666 GST_QUEUE2_SIGNAL_DEL (queue); \
3667 if (queue->use_buffering) \
3668 update_buffering (queue);
3670 /* Changing the minimum required fill level must
3671 * wake up the _loop function as it might now
3672 * be able to preceed.
3674 #define QUEUE_THRESHOLD_CHANGE(q)\
3675 GST_QUEUE2_SIGNAL_ADD (queue);
3678 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3682 /* the element must be stopped in order to do this */
3683 GST_OBJECT_LOCK (queue);
3684 state = GST_STATE (queue);
3685 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3687 GST_OBJECT_UNLOCK (queue);
3689 /* set new location */
3690 g_free (queue->temp_template);
3691 queue->temp_template = g_strdup (template);
3698 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3699 GST_OBJECT_UNLOCK (queue);
3704 gst_queue2_set_property (GObject * object,
3705 guint prop_id, const GValue * value, GParamSpec * pspec)
3707 GstQueue2 *queue = GST_QUEUE2 (object);
3709 /* someone could change levels here, and since this
3710 * affects the get/put funcs, we need to lock for safety. */
3711 GST_QUEUE2_MUTEX_LOCK (queue);
3714 case PROP_MAX_SIZE_BYTES:
3715 queue->max_level.bytes = g_value_get_uint (value);
3716 QUEUE_CAPACITY_CHANGE (queue);
3718 case PROP_MAX_SIZE_BUFFERS:
3719 queue->max_level.buffers = g_value_get_uint (value);
3720 QUEUE_CAPACITY_CHANGE (queue);
3722 case PROP_MAX_SIZE_TIME:
3723 queue->max_level.time = g_value_get_uint64 (value);
3724 /* set rate_time to the same value. We use an extra field in the level
3725 * structure so that we can easily access and compare it */
3726 queue->max_level.rate_time = queue->max_level.time;
3727 QUEUE_CAPACITY_CHANGE (queue);
3729 case PROP_USE_BUFFERING:
3730 queue->use_buffering = g_value_get_boolean (value);
3731 if (!queue->use_buffering && queue->is_buffering) {
3732 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3733 "posting 100%% message");
3734 SET_PERCENT (queue, 100);
3735 queue->is_buffering = FALSE;
3738 if (queue->use_buffering) {
3739 queue->is_buffering = TRUE;
3740 update_buffering (queue);
3743 case PROP_USE_TAGS_BITRATE:
3744 queue->use_tags_bitrate = g_value_get_boolean (value);
3746 case PROP_USE_RATE_ESTIMATE:
3747 queue->use_rate_estimate = g_value_get_boolean (value);
3749 case PROP_LOW_PERCENT:
3750 queue->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3751 if (queue->is_buffering)
3752 update_buffering (queue);
3754 case PROP_HIGH_PERCENT:
3755 queue->high_watermark =
3756 g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3757 if (queue->is_buffering)
3758 update_buffering (queue);
3760 case PROP_LOW_WATERMARK:
3761 queue->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3762 if (queue->is_buffering)
3763 update_buffering (queue);
3765 case PROP_HIGH_WATERMARK:
3766 queue->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3767 if (queue->is_buffering)
3768 update_buffering (queue);
3770 case PROP_TEMP_TEMPLATE:
3771 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3773 case PROP_TEMP_REMOVE:
3774 queue->temp_remove = g_value_get_boolean (value);
3776 case PROP_RING_BUFFER_MAX_SIZE:
3777 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3780 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3784 GST_QUEUE2_MUTEX_UNLOCK (queue);
3785 gst_queue2_post_buffering (queue);
3789 gst_queue2_get_property (GObject * object,
3790 guint prop_id, GValue * value, GParamSpec * pspec)
3792 GstQueue2 *queue = GST_QUEUE2 (object);
3794 GST_QUEUE2_MUTEX_LOCK (queue);
3797 case PROP_CUR_LEVEL_BYTES:
3798 g_value_set_uint (value, queue->cur_level.bytes);
3800 case PROP_CUR_LEVEL_BUFFERS:
3801 g_value_set_uint (value, queue->cur_level.buffers);
3803 case PROP_CUR_LEVEL_TIME:
3804 g_value_set_uint64 (value, queue->cur_level.time);
3806 case PROP_MAX_SIZE_BYTES:
3807 g_value_set_uint (value, queue->max_level.bytes);
3809 case PROP_MAX_SIZE_BUFFERS:
3810 g_value_set_uint (value, queue->max_level.buffers);
3812 case PROP_MAX_SIZE_TIME:
3813 g_value_set_uint64 (value, queue->max_level.time);
3815 case PROP_USE_BUFFERING:
3816 g_value_set_boolean (value, queue->use_buffering);
3818 case PROP_USE_TAGS_BITRATE:
3819 g_value_set_boolean (value, queue->use_tags_bitrate);
3821 case PROP_USE_RATE_ESTIMATE:
3822 g_value_set_boolean (value, queue->use_rate_estimate);
3824 case PROP_LOW_PERCENT:
3825 g_value_set_int (value, queue->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
3827 case PROP_HIGH_PERCENT:
3828 g_value_set_int (value, queue->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
3830 case PROP_LOW_WATERMARK:
3831 g_value_set_double (value, queue->low_watermark /
3832 (gdouble) MAX_BUFFERING_LEVEL);
3834 case PROP_HIGH_WATERMARK:
3835 g_value_set_double (value, queue->high_watermark /
3836 (gdouble) MAX_BUFFERING_LEVEL);
3838 case PROP_TEMP_TEMPLATE:
3839 g_value_set_string (value, queue->temp_template);
3841 case PROP_TEMP_LOCATION:
3842 g_value_set_string (value, queue->temp_location);
3844 case PROP_TEMP_REMOVE:
3845 g_value_set_boolean (value, queue->temp_remove);
3847 case PROP_RING_BUFFER_MAX_SIZE:
3848 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3850 case PROP_AVG_IN_RATE:
3852 gdouble in_rate = queue->byte_in_rate;
3854 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3855 * calculated, so calculate it here. */
3856 if (in_rate == 0.0 && queue->bytes_in
3857 && queue->last_update_in_rates_elapsed > 0.0)
3858 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3860 g_value_set_int64 (value, (gint64) in_rate);
3864 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3868 GST_QUEUE2_MUTEX_UNLOCK (queue);