2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
30 * Data is queued until one of the limits specified by the
31 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
32 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
33 * more buffers into the queue will block the pushing thread until more space
36 * The queue will create a new thread on the source pad to decouple the
37 * processing on sink and source pad.
39 * You can query how many buffers are queued by reading the
40 * #GstQueue2:current-level-buffers property.
42 * The default queue size limits are 100 buffers, 2MB of data, or
43 * two seconds worth of data, whichever is reached first.
45 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
46 * will allocate a random free filename and buffer data in the file.
47 * By using this, it will buffer the entire stream data on the file independently
48 * of the queue size limits, they will only be used for buffering statistics.
50 * The temp-location property will be used to notify the application of the
58 #include "gstqueue2.h"
60 #include <glib/gstdio.h>
62 #include "gst/gst-i18n-lib.h"
63 #include "gst/glib-compat-private.h"
68 #include <io.h> /* lseek, open, close, read */
70 #define lseek _lseeki64
77 #ifdef __BIONIC__ /* Android */
85 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
90 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
95 GST_DEBUG_CATEGORY_STATIC (queue_debug);
96 #define GST_CAT_DEFAULT (queue_debug)
97 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
105 #define DEFAULT_BUFFER_SIZE 4096
106 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
107 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
108 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
110 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
112 /* default property values */
113 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
114 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
115 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
116 #define DEFAULT_USE_BUFFERING FALSE
117 #define DEFAULT_USE_TAGS_BITRATE FALSE
118 #define DEFAULT_USE_RATE_ESTIMATE TRUE
119 #define DEFAULT_LOW_PERCENT 10
120 #define DEFAULT_HIGH_PERCENT 99
121 #define DEFAULT_LOW_WATERMARK 0.01
122 #define DEFAULT_HIGH_WATERMARK 0.99
123 #define DEFAULT_TEMP_REMOVE TRUE
124 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
129 PROP_CUR_LEVEL_BUFFERS,
130 PROP_CUR_LEVEL_BYTES,
132 PROP_MAX_SIZE_BUFFERS,
136 PROP_USE_TAGS_BITRATE,
137 PROP_USE_RATE_ESTIMATE,
145 PROP_RING_BUFFER_MAX_SIZE,
150 /* Explanation for buffer levels and percentages:
152 * The buffering_level functions here return a value in a normalized range
153 * that specifies the queue's current fill level. The range goes from 0 to
154 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
156 * This is not to be confused with the buffering_percent value, which is
157 * a *relative* quantity - relative to the low/high watermarks.
158 * buffering_percent = 0% means buffering_level is at the low watermark.
159 * buffering_percent = 100% means buffering_level is at the high watermark.
160 * buffering_percent is used for determining if the fill level has reached
161 * the high watermark, and for producing BUFFERING messages. This value
162 * always uses a 0..100 range (since it is a percentage).
164 * To avoid future confusions, whenever "buffering level" is mentioned, it
165 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
166 * range. Whenever "buffering_percent" is mentioned, it refers to the
167 * percentage value that is relative to the low/high watermark. */
169 /* Using a buffering level range of 0..1000000 to allow for a
170 * resolution in ppm (1 ppm = 0.0001%) */
171 #define MAX_BUFFERING_LEVEL 1000000
173 /* How much 1% makes up in the buffer level range */
174 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
176 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
183 #define STATUS(queue, pad, msg) \
184 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
185 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
186 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
187 " ns, %"G_GUINT64_FORMAT" items", \
188 GST_DEBUG_PAD_NAME (pad), \
189 queue->cur_level.buffers, \
190 queue->max_level.buffers, \
191 queue->cur_level.bytes, \
192 queue->max_level.bytes, \
193 queue->cur_level.time, \
194 queue->max_level.time, \
195 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
196 queue->current->writing_pos - queue->current->max_reading_pos : \
197 queue->queue.length))
199 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
200 g_mutex_lock (&q->qlock); \
203 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
204 GST_QUEUE2_MUTEX_LOCK (q); \
205 if (res != GST_FLOW_OK) \
209 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
210 g_mutex_unlock (&q->qlock); \
213 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
214 STATUS (queue, q->sinkpad, "wait for DEL"); \
215 q->waiting_del = TRUE; \
216 g_cond_wait (&q->item_del, &queue->qlock); \
217 q->waiting_del = FALSE; \
218 if (res != GST_FLOW_OK) { \
219 STATUS (queue, q->srcpad, "received DEL wakeup"); \
222 STATUS (queue, q->sinkpad, "received DEL"); \
225 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
226 STATUS (queue, q->srcpad, "wait for ADD"); \
227 q->waiting_add = TRUE; \
228 g_cond_wait (&q->item_add, &q->qlock); \
229 q->waiting_add = FALSE; \
230 if (res != GST_FLOW_OK) { \
231 STATUS (queue, q->srcpad, "received ADD wakeup"); \
234 STATUS (queue, q->srcpad, "received ADD"); \
237 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
238 if (q->waiting_del) { \
239 STATUS (q, q->srcpad, "signal DEL"); \
240 g_cond_signal (&q->item_del); \
244 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
245 if (q->waiting_add) { \
246 STATUS (q, q->sinkpad, "signal ADD"); \
247 g_cond_signal (&q->item_add); \
251 #define SET_PERCENT(q, perc) G_STMT_START { \
252 if (perc != q->buffering_percent) { \
253 q->buffering_percent = perc; \
254 q->percent_changed = TRUE; \
255 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
256 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
257 &q->buffering_left); \
262 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
263 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
264 "dataflow inside the queue element");
265 #define gst_queue2_parent_class parent_class
266 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
268 static void gst_queue2_finalize (GObject * object);
270 static void gst_queue2_set_property (GObject * object,
271 guint prop_id, const GValue * value, GParamSpec * pspec);
272 static void gst_queue2_get_property (GObject * object,
273 guint prop_id, GValue * value, GParamSpec * pspec);
275 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
277 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
278 GstBufferList * buffer_list);
279 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
280 static void gst_queue2_loop (GstPad * pad);
282 static GstFlowReturn gst_queue2_handle_sink_event (GstPad * pad,
283 GstObject * parent, GstEvent * event);
284 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
287 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
289 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
291 static gboolean gst_queue2_handle_query (GstElement * element,
294 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
295 guint64 offset, guint length, GstBuffer ** buffer);
297 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
298 GstPadMode mode, gboolean active);
299 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
300 GstPadMode mode, gboolean active);
301 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
302 GstStateChange transition);
304 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
305 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
307 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
308 static void update_in_rates (GstQueue2 * queue, gboolean force);
309 static GstMessage *gst_queue2_get_buffering_message (GstQueue2 * queue);
310 static void gst_queue2_post_buffering (GstQueue2 * queue);
314 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
315 GST_QUEUE2_ITEM_TYPE_BUFFER,
316 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
317 GST_QUEUE2_ITEM_TYPE_EVENT,
318 GST_QUEUE2_ITEM_TYPE_QUERY
323 GstQueue2ItemType type;
327 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
330 gst_queue2_class_init (GstQueue2Class * klass)
332 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
333 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
335 gobject_class->set_property = gst_queue2_set_property;
336 gobject_class->get_property = gst_queue2_get_property;
339 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
340 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
341 "Current amount of data in the queue (bytes)",
342 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
344 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
345 "Current number of buffers in the queue",
346 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
347 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
348 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
349 "Current amount of data in the queue (in ns)",
350 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
353 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
354 "Max. amount of data in the queue (bytes, 0=disable)",
355 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
356 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
357 G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
359 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
360 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
361 DEFAULT_MAX_SIZE_BUFFERS,
362 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
363 G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
365 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
366 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
367 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
368 G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
371 g_param_spec_boolean ("use-buffering", "Use buffering",
372 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
373 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
374 G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_USE_TAGS_BITRATE,
376 g_param_spec_boolean ("use-tags-bitrate", "Use bitrate from tags",
377 "Use a bitrate from upstream tags to estimate buffer duration if not provided",
378 DEFAULT_USE_TAGS_BITRATE,
379 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
380 G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
382 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
383 "Estimate the bitrate of the stream to calculate time level",
384 DEFAULT_USE_RATE_ESTIMATE,
385 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
387 g_param_spec_int ("low-percent", "Low percent",
388 "Low threshold for buffering to start. Only used if use-buffering is True "
389 "(Deprecated: use low-watermark instead)",
390 0, 100, DEFAULT_LOW_WATERMARK * 100,
391 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
393 g_param_spec_int ("high-percent", "High percent",
394 "High threshold for buffering to finish. Only used if use-buffering is True "
395 "(Deprecated: use high-watermark instead)",
396 0, 100, DEFAULT_HIGH_WATERMARK * 100,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
399 g_param_spec_double ("low-watermark", "Low watermark",
400 "Low threshold for buffering to start. Only used if use-buffering is True",
401 0.0, 1.0, DEFAULT_LOW_WATERMARK,
402 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
404 g_param_spec_double ("high-watermark", "High watermark",
405 "High threshold for buffering to finish. Only used if use-buffering is True",
406 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
407 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
410 g_param_spec_string ("temp-template", "Temporary File Template",
411 "File template to store temporary files in, should contain directory "
412 "and XXXXXX. (NULL == disabled)",
413 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
416 g_param_spec_string ("temp-location", "Temporary File Location",
417 "Location to store temporary files in (Only read this property, "
418 "use temp-template to configure the name template)",
419 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
422 * GstQueue2:temp-remove
424 * When temp-template is set, remove the temporary file when going to READY.
426 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
427 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
428 "Remove the temp-location after use",
429 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
432 * GstQueue2:ring-buffer-max-size
434 * The maximum size of the ring buffer in bytes. If set to 0, the ring
435 * buffer is disabled. Default 0.
437 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
438 g_param_spec_uint64 ("ring-buffer-max-size",
439 "Max. ring buffer size (bytes)",
440 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
441 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
442 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 * GstQueue2:avg-in-rate
447 * The average input data rate.
449 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
450 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
451 "Average input data rate (bytes/s)",
452 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
454 /* set several parent class virtual functions */
455 gobject_class->finalize = gst_queue2_finalize;
457 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
458 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
460 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
463 "Erik Walthinsen <omega@cse.ogi.edu>, "
464 "Wim Taymans <wim.taymans@gmail.com>");
466 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
467 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
471 gst_queue2_init (GstQueue2 * queue)
473 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
475 gst_pad_set_chain_function (queue->sinkpad,
476 GST_DEBUG_FUNCPTR (gst_queue2_chain));
477 gst_pad_set_chain_list_function (queue->sinkpad,
478 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
479 gst_pad_set_activatemode_function (queue->sinkpad,
480 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
481 gst_pad_set_event_full_function (queue->sinkpad,
482 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
483 gst_pad_set_query_function (queue->sinkpad,
484 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
485 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
486 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
488 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
490 gst_pad_set_activatemode_function (queue->srcpad,
491 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
492 gst_pad_set_getrange_function (queue->srcpad,
493 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
494 gst_pad_set_event_function (queue->srcpad,
495 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
496 gst_pad_set_query_function (queue->srcpad,
497 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
498 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
499 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
502 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
503 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
504 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
505 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
506 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
507 queue->use_buffering = DEFAULT_USE_BUFFERING;
508 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
509 queue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
510 queue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
512 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
513 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
515 queue->sinktime = GST_CLOCK_TIME_NONE;
516 queue->srctime = GST_CLOCK_TIME_NONE;
517 queue->sink_tainted = TRUE;
518 queue->src_tainted = TRUE;
520 queue->srcresult = GST_FLOW_FLUSHING;
521 queue->sinkresult = GST_FLOW_FLUSHING;
522 queue->is_eos = FALSE;
523 queue->in_timer = g_timer_new ();
524 queue->out_timer = g_timer_new ();
526 g_mutex_init (&queue->qlock);
527 queue->waiting_add = FALSE;
528 g_cond_init (&queue->item_add);
529 queue->waiting_del = FALSE;
530 g_cond_init (&queue->item_del);
531 g_queue_init (&queue->queue);
533 g_cond_init (&queue->query_handled);
534 queue->last_query = FALSE;
536 g_mutex_init (&queue->buffering_post_lock);
537 queue->buffering_percent = 100;
539 /* tempfile related */
540 queue->temp_template = NULL;
541 queue->temp_location = NULL;
542 queue->temp_remove = DEFAULT_TEMP_REMOVE;
544 queue->ring_buffer = NULL;
545 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
547 GST_DEBUG_OBJECT (queue,
548 "initialized queue's not_empty & not_full conditions");
551 /* called only once, as opposed to dispose */
553 gst_queue2_finalize (GObject * object)
555 GstQueue2 *queue = GST_QUEUE2 (object);
557 GST_DEBUG_OBJECT (queue, "finalizing queue");
559 while (!g_queue_is_empty (&queue->queue)) {
560 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
562 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
563 gst_mini_object_unref (qitem->item);
564 g_slice_free (GstQueue2Item, qitem);
567 queue->last_query = FALSE;
568 g_queue_clear (&queue->queue);
569 g_mutex_clear (&queue->qlock);
570 g_mutex_clear (&queue->buffering_post_lock);
571 g_cond_clear (&queue->item_add);
572 g_cond_clear (&queue->item_del);
573 g_cond_clear (&queue->query_handled);
574 g_timer_destroy (queue->in_timer);
575 g_timer_destroy (queue->out_timer);
577 /* temp_file path cleanup */
578 g_free (queue->temp_template);
579 g_free (queue->temp_location);
581 G_OBJECT_CLASS (parent_class)->finalize (object);
585 debug_ranges (GstQueue2 * queue)
587 GstQueue2Range *walk;
589 for (walk = queue->ranges; walk; walk = walk->next) {
590 GST_DEBUG_OBJECT (queue,
591 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
592 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
593 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
594 walk->rb_writing_pos, walk->reading_pos,
595 walk == queue->current ? "**y**" : " n ");
599 /* clear all the downloaded ranges */
601 clean_ranges (GstQueue2 * queue)
603 GST_DEBUG_OBJECT (queue, "clean queue ranges");
605 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
606 queue->ranges = NULL;
607 queue->current = NULL;
610 /* find a range that contains @offset or NULL when nothing does */
611 static GstQueue2Range *
612 find_range (GstQueue2 * queue, guint64 offset)
614 GstQueue2Range *range = NULL;
615 GstQueue2Range *walk;
617 /* first do a quick check for the current range */
618 for (walk = queue->ranges; walk; walk = walk->next) {
619 if (offset >= walk->offset && offset <= walk->writing_pos) {
620 /* we can reuse an existing range */
626 GST_DEBUG_OBJECT (queue,
627 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
628 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
630 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
636 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
638 guint64 max_reading_pos, writing_pos;
640 writing_pos = range->writing_pos;
641 max_reading_pos = range->max_reading_pos;
643 if (writing_pos > max_reading_pos)
644 queue->cur_level.bytes = writing_pos - max_reading_pos;
646 queue->cur_level.bytes = 0;
649 /* make a new range for @offset or reuse an existing range */
650 static GstQueue2Range *
651 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
653 GstQueue2Range *range, *prev, *next;
655 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
657 if ((range = find_range (queue, offset))) {
658 GST_DEBUG_OBJECT (queue,
659 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
661 if (update_existing && range->writing_pos != offset) {
662 GST_DEBUG_OBJECT (queue, "updating range writing position to "
663 "%" G_GUINT64_FORMAT, offset);
664 range->writing_pos = offset;
667 GST_DEBUG_OBJECT (queue,
668 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
670 range = g_slice_new0 (GstQueue2Range);
671 range->offset = offset;
672 /* we want to write to the next location in the ring buffer */
673 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
674 range->writing_pos = offset;
675 range->rb_writing_pos = range->rb_offset;
676 range->reading_pos = offset;
677 range->max_reading_pos = offset;
681 next = queue->ranges;
683 if (next->offset > offset) {
684 /* insert before next */
685 GST_DEBUG_OBJECT (queue,
686 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
698 queue->ranges = range;
700 debug_ranges (queue);
702 /* update the stats for this range */
703 update_cur_level (queue, range);
709 /* clear and init the download ranges for offset 0 */
711 init_ranges (GstQueue2 * queue)
713 GST_DEBUG_OBJECT (queue, "init queue ranges");
715 /* get rid of all the current ranges */
716 clean_ranges (queue);
717 /* make a range for offset 0 */
718 queue->current = add_range (queue, 0, TRUE);
721 /* calculate the diff between running time on the sink and src of the queue.
722 * This is the total amount of time in the queue. */
724 update_time_level (GstQueue2 * queue)
726 if (queue->sink_tainted) {
728 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
729 queue->sink_segment.position);
730 queue->sink_tainted = FALSE;
733 if (queue->src_tainted) {
735 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
736 queue->src_segment.position);
737 queue->src_tainted = FALSE;
740 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
741 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
743 if (queue->sinktime != GST_CLOCK_TIME_NONE
744 && queue->srctime != GST_CLOCK_TIME_NONE
745 && queue->sinktime >= queue->srctime)
746 queue->cur_level.time = queue->sinktime - queue->srctime;
748 queue->cur_level.time = 0;
751 /* take a SEGMENT event and apply the values to segment, updating the time
754 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
757 gst_event_copy_segment (event, segment);
759 if (segment->format == GST_FORMAT_BYTES) {
760 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
761 /* start is where we'll be getting from and as such writing next */
762 queue->current = add_range (queue, segment->start, TRUE);
766 /* now configure the values, we use these to track timestamps on the
768 if (segment->format != GST_FORMAT_TIME) {
769 /* non-time format, pretend the current time segment is closed with a
770 * 0 start and unknown stop time. */
771 segment->format = GST_FORMAT_TIME;
777 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
780 queue->sink_tainted = TRUE;
782 queue->src_tainted = TRUE;
784 /* segment can update the time level of the queue */
785 update_time_level (queue);
789 apply_gap (GstQueue2 * queue, GstEvent * event,
790 GstSegment * segment, gboolean is_sink)
792 GstClockTime timestamp;
793 GstClockTime duration;
795 gst_event_parse_gap (event, ×tamp, &duration);
797 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
799 if (GST_CLOCK_TIME_IS_VALID (duration)) {
800 timestamp += duration;
803 segment->position = timestamp;
806 queue->sink_tainted = TRUE;
808 queue->src_tainted = TRUE;
810 /* calc diff with other end */
811 update_time_level (queue);
815 /* take a buffer and update segment, updating the time level of the queue. */
817 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
818 guint64 size, gboolean is_sink)
820 GstClockTime duration, timestamp;
822 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
823 duration = GST_BUFFER_DURATION (buffer);
825 /* If we have no duration, pick one from the bitrate if we can */
826 if (duration == GST_CLOCK_TIME_NONE && queue->use_tags_bitrate) {
828 is_sink ? queue->sink_tags_bitrate : queue->src_tags_bitrate;
830 duration = gst_util_uint64_scale (size, 8 * GST_SECOND, bitrate);
833 /* if no timestamp is set, assume it's continuous with the previous
835 if (timestamp == GST_CLOCK_TIME_NONE)
836 timestamp = segment->position;
839 if (duration != GST_CLOCK_TIME_NONE)
840 timestamp += duration;
842 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
843 GST_TIME_ARGS (timestamp));
845 segment->position = timestamp;
848 queue->sink_tainted = TRUE;
850 queue->src_tainted = TRUE;
852 /* calc diff with other end */
853 update_time_level (queue);
858 GstClockTime timestamp;
863 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
865 struct BufListData *bld = data;
866 GstClockTime *timestamp = &bld->timestamp;
869 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
870 " duration %" GST_TIME_FORMAT, idx,
871 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
872 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
873 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
875 btime = GST_BUFFER_DTS_OR_PTS (*buf);
876 if (GST_CLOCK_TIME_IS_VALID (btime))
879 if (GST_BUFFER_DURATION_IS_VALID (*buf))
880 *timestamp += GST_BUFFER_DURATION (*buf);
881 else if (bld->bitrate != 0) {
882 guint64 size = gst_buffer_get_size (*buf);
884 /* If we have no duration, pick one from the bitrate if we can */
885 *timestamp += gst_util_uint64_scale (bld->bitrate, 8 * GST_SECOND, size);
889 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
893 /* take a buffer list and update segment, updating the time level of the queue */
895 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
896 GstSegment * segment, gboolean is_sink)
898 struct BufListData bld;
900 /* if no timestamp is set, assume it's continuous with the previous time */
901 bld.timestamp = segment->position;
903 if (queue->use_tags_bitrate) {
905 bld.bitrate = queue->sink_tags_bitrate;
907 bld.bitrate = queue->src_tags_bitrate;
911 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &bld);
913 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
914 GST_TIME_ARGS (bld.timestamp));
916 segment->position = bld.timestamp;
919 queue->sink_tainted = TRUE;
921 queue->src_tainted = TRUE;
923 /* calc diff with other end */
924 update_time_level (queue);
928 normalize_to_buffering_level (guint64 cur_level, guint64 max_level,
937 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL,
938 MIN (max_level, alt_max));
940 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL, max_level);
942 return MIN (p, MAX_BUFFERING_LEVEL);
946 get_buffering_level (GstQueue2 * queue, gboolean * is_buffering,
947 gint * buffering_level)
949 gint buflevel, buflevel2;
951 if (queue->high_watermark <= 0) {
953 *buffering_level = MAX_BUFFERING_LEVEL;
955 *is_buffering = FALSE;
958 #define GET_BUFFER_LEVEL_FOR_QUANTITY(format,alt_max) \
959 normalize_to_buffering_level (queue->cur_level.format,queue->max_level.format,(alt_max))
961 if (queue->is_eos || queue->srcresult == GST_FLOW_NOT_LINKED) {
962 /* on EOS and NOT_LINKED we are always 100% full, we set the var
963 * here so that we can reuse the logic below to stop buffering */
964 buflevel = MAX_BUFFERING_LEVEL;
965 GST_LOG_OBJECT (queue, "we are %s", queue->is_eos ? "EOS" : "NOT_LINKED");
967 GST_LOG_OBJECT (queue,
968 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
969 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
970 queue->cur_level.buffers);
972 /* figure out the buffering level we are filled, we take the max of all formats. */
973 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
974 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, 0);
976 guint64 rb_size = queue->ring_buffer_max_size;
977 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, rb_size);
980 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (time, 0);
981 buflevel = MAX (buflevel, buflevel2);
983 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (buffers, 0);
984 buflevel = MAX (buflevel, buflevel2);
986 /* also apply the rate estimate when we need to */
987 if (queue->use_rate_estimate) {
988 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (rate_time, 0);
989 buflevel = MAX (buflevel, buflevel2);
992 /* Don't get to 0% unless we're really empty */
993 if (queue->cur_level.bytes > 0)
994 buflevel = MAX (1, buflevel);
996 #undef GET_BUFFER_LEVEL_FOR_QUANTITY
999 *is_buffering = queue->is_buffering;
1001 if (buffering_level)
1002 *buffering_level = buflevel;
1004 GST_DEBUG_OBJECT (queue, "buffering %d, level %d", queue->is_buffering,
1011 convert_to_buffering_percent (GstQueue2 * queue, gint buffering_level)
1015 /* scale so that if buffering_level equals the high watermark,
1016 * the percentage is 100% */
1017 percent = buffering_level * 100 / queue->high_watermark;
1026 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
1027 gint * avg_in, gint * avg_out, gint64 * buffering_left)
1030 if (!QUEUE_IS_USING_QUEUE (queue)) {
1031 if (QUEUE_IS_USING_RING_BUFFER (queue))
1032 *mode = GST_BUFFERING_TIMESHIFT;
1034 *mode = GST_BUFFERING_DOWNLOAD;
1036 *mode = GST_BUFFERING_STREAM;
1041 *avg_in = queue->byte_in_rate;
1043 *avg_out = queue->byte_out_rate;
1045 if (buffering_left) {
1046 *buffering_left = (percent == 100 ? 0 : -1);
1048 if (queue->use_rate_estimate) {
1051 max = queue->max_level.rate_time;
1052 cur = queue->cur_level.rate_time;
1054 if (percent != 100 && max > cur)
1055 *buffering_left = (max - cur) / 1000000;
1060 /* Called with the lock taken */
1062 gst_queue2_get_buffering_message (GstQueue2 * queue)
1064 GstMessage *msg = NULL;
1066 if (queue->percent_changed) {
1067 gint percent = queue->buffering_percent;
1069 queue->percent_changed = FALSE;
1071 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
1072 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
1074 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
1075 queue->avg_out, queue->buffering_left);
1082 gst_queue2_post_buffering (GstQueue2 * queue)
1084 GstMessage *msg = NULL;
1086 g_mutex_lock (&queue->buffering_post_lock);
1087 GST_QUEUE2_MUTEX_LOCK (queue);
1088 msg = gst_queue2_get_buffering_message (queue);
1089 GST_QUEUE2_MUTEX_UNLOCK (queue);
1092 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1094 g_mutex_unlock (&queue->buffering_post_lock);
1098 update_buffering (GstQueue2 * queue)
1100 gint buffering_level, percent;
1102 /* Ensure the variables used to calculate buffering state are up-to-date. */
1104 update_cur_level (queue, queue->current);
1105 update_in_rates (queue, FALSE);
1107 if (!get_buffering_level (queue, NULL, &buffering_level))
1110 percent = convert_to_buffering_percent (queue, buffering_level);
1112 if (queue->is_buffering) {
1113 /* if we were buffering see if we reached the high watermark */
1115 queue->is_buffering = FALSE;
1117 SET_PERCENT (queue, percent);
1119 /* we were not buffering, check if we need to start buffering if we drop
1120 * below the low threshold */
1121 if (buffering_level < queue->low_watermark) {
1122 queue->is_buffering = TRUE;
1123 SET_PERCENT (queue, percent);
1129 reset_rate_timer (GstQueue2 * queue)
1131 queue->bytes_in = 0;
1132 queue->bytes_out = 0;
1133 queue->byte_in_rate = 0.0;
1134 queue->byte_in_period = 0;
1135 queue->byte_out_rate = 0.0;
1136 queue->last_update_in_rates_elapsed = 0.0;
1137 queue->last_in_elapsed = 0.0;
1138 queue->last_out_elapsed = 0.0;
1139 queue->in_timer_started = FALSE;
1140 queue->out_timer_started = FALSE;
1143 /* the interval in seconds to recalculate the rate */
1144 #define RATE_INTERVAL 0.2
1145 /* Tuning for rate estimation. We use a large window for the input rate because
1146 * it should be stable when connected to a network. The output rate is less
1147 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1148 * therefore adapt more quickly.
1149 * However, initial input rate may be subject to a burst, and should therefore
1150 * initially also adapt more quickly to changes, and only later on give higher
1151 * weight to previous values. */
1152 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1153 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1156 update_in_rates (GstQueue2 * queue, gboolean force)
1158 gdouble elapsed, period;
1159 gdouble byte_in_rate;
1161 if (!queue->in_timer_started) {
1162 queue->in_timer_started = TRUE;
1163 g_timer_start (queue->in_timer);
1167 queue->last_update_in_rates_elapsed = elapsed =
1168 g_timer_elapsed (queue->in_timer, NULL);
1170 /* recalc after each interval. */
1171 if (force || queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1172 period = elapsed - queue->last_in_elapsed;
1174 GST_DEBUG_OBJECT (queue,
1175 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1176 period, queue->bytes_in, queue->byte_in_period);
1178 byte_in_rate = queue->bytes_in / period;
1180 if (queue->byte_in_rate == 0.0)
1181 queue->byte_in_rate = byte_in_rate;
1183 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1184 (double) queue->byte_in_period, period);
1186 /* another data point, cap at 16 for long time running average */
1187 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1188 queue->byte_in_period += period;
1190 /* reset the values to calculate rate over the next interval */
1191 queue->last_in_elapsed = elapsed;
1192 queue->bytes_in = 0;
1195 if (queue->byte_in_rate > 0.0) {
1196 queue->cur_level.rate_time =
1197 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1199 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1200 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1204 update_out_rates (GstQueue2 * queue)
1206 gdouble elapsed, period;
1207 gdouble byte_out_rate;
1209 if (!queue->out_timer_started) {
1210 queue->out_timer_started = TRUE;
1211 g_timer_start (queue->out_timer);
1215 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1217 /* recalc after each interval. */
1218 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1219 period = elapsed - queue->last_out_elapsed;
1221 GST_DEBUG_OBJECT (queue,
1222 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1224 byte_out_rate = queue->bytes_out / period;
1226 if (queue->byte_out_rate == 0.0)
1227 queue->byte_out_rate = byte_out_rate;
1229 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1231 /* reset the values to calculate rate over the next interval */
1232 queue->last_out_elapsed = elapsed;
1233 queue->bytes_out = 0;
1235 if (queue->byte_in_rate > 0.0) {
1236 queue->cur_level.rate_time =
1237 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1239 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1240 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1244 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1246 guint64 reading_pos, max_reading_pos;
1249 max_reading_pos = range->max_reading_pos;
1251 max_reading_pos = MAX (max_reading_pos, reading_pos);
1253 GST_DEBUG_OBJECT (queue,
1254 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1255 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1256 range->max_reading_pos = max_reading_pos;
1258 update_cur_level (queue, range);
1262 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1267 /* until we receive the FLUSH_STOP from this seek, we skip data */
1268 queue->seeking = TRUE;
1269 GST_QUEUE2_MUTEX_UNLOCK (queue);
1271 debug_ranges (queue);
1273 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1276 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1277 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1278 GST_SEEK_TYPE_NONE, -1);
1280 res = gst_pad_push_event (queue->sinkpad, event);
1281 GST_QUEUE2_MUTEX_LOCK (queue);
1284 /* Between us sending the seek event and re-acquiring the lock, the source
1285 * thread might already have pushed data and moved along the range's
1286 * writing_pos beyond the seek offset. In that case we don't want to set
1287 * the writing position back to the requested seek position, as it would
1288 * cause data to be written to the wrong offset in the file or ring buffer.
1289 * We still do the add_range call to switch the current range to the
1290 * requested range, or create one if one doesn't exist yet. */
1291 queue->current = add_range (queue, offset, FALSE);
1297 /* get the threshold for when we decide to seek rather than wait */
1299 get_seek_threshold (GstQueue2 * queue)
1303 /* FIXME, find a good threshold based on the incoming rate. */
1304 threshold = 1024 * 512;
1306 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1307 threshold = MIN (threshold,
1308 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1313 /* see if there is enough data in the file to read a full buffer */
1315 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1317 GstQueue2Range *range;
1319 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1322 if ((range = find_range (queue, offset))) {
1323 if (queue->current != range) {
1324 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1325 perform_seek_to_offset (queue, range->writing_pos);
1328 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1329 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1331 /* we have a range for offset */
1332 GST_DEBUG_OBJECT (queue,
1333 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1334 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1336 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1339 if (offset + length <= range->writing_pos)
1342 GST_DEBUG_OBJECT (queue,
1343 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1344 (offset + length) - range->writing_pos);
1347 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1348 " len %u", offset, length);
1349 /* we don't have the range, see how far away we are */
1350 if (!queue->is_eos && queue->current) {
1351 guint64 threshold = get_seek_threshold (queue);
1353 if (offset >= queue->current->offset && offset <=
1354 queue->current->writing_pos + threshold) {
1355 GST_INFO_OBJECT (queue,
1356 "requested data is within range, wait for data");
1361 /* too far away, do a seek */
1362 perform_seek_to_offset (queue, offset);
1369 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1370 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1371 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1373 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1376 static GstFlowReturn
1377 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1378 guint8 * dst, gint64 * read_return)
1380 guint8 *ring_buffer;
1383 ring_buffer = queue->ring_buffer;
1385 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1388 /* this should not block */
1389 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1391 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1392 res = fread (dst, 1, length, queue->temp_file);
1394 memcpy (dst, ring_buffer + offset, length);
1398 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1400 if (G_UNLIKELY (res < length)) {
1401 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1402 goto could_not_read;
1403 /* check for errors or EOF */
1404 if (ferror (queue->temp_file))
1405 goto could_not_read;
1406 if (feof (queue->temp_file) && length > 0)
1416 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1417 return GST_FLOW_ERROR;
1421 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1422 return GST_FLOW_ERROR;
1426 GST_DEBUG ("non-regular file hits EOS");
1427 return GST_FLOW_EOS;
1431 static GstFlowReturn
1432 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1433 GstBuffer ** buffer)
1438 guint64 file_offset;
1439 guint block_length, remaining, read_length;
1443 GstFlowReturn ret = GST_FLOW_OK;
1445 /* allocate the output buffer of the requested size */
1446 if (*buffer == NULL)
1447 buf = gst_buffer_new_allocate (NULL, length, NULL);
1451 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
1452 goto buffer_write_fail;
1455 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1459 rb_size = queue->ring_buffer_max_size;
1460 max_size = QUEUE_MAX_BYTES (queue);
1463 while (remaining > 0) {
1464 /* configure how much/whether to read */
1465 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1468 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1471 /* calculate how far away the offset is */
1472 if (queue->current->writing_pos > rpos)
1473 level = queue->current->writing_pos - rpos;
1477 GST_DEBUG_OBJECT (queue,
1478 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1479 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1480 rpos, queue->current->writing_pos, level, max_size);
1482 if (level >= max_size) {
1483 /* we don't have the data but if we have a ring buffer that is full, we
1485 GST_DEBUG_OBJECT (queue,
1486 "ring buffer full, reading QUEUE_MAX_BYTES %"
1487 G_GUINT64_FORMAT " bytes", max_size);
1488 read_length = max_size;
1489 } else if (queue->is_eos) {
1490 /* won't get any more data so read any data we have */
1492 GST_DEBUG_OBJECT (queue,
1493 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1495 read_length = level;
1503 if (read_length == 0) {
1504 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1505 GST_DEBUG_OBJECT (queue,
1506 "update current position [%" G_GUINT64_FORMAT "-%"
1507 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1508 update_cur_pos (queue, queue->current, rpos);
1509 GST_QUEUE2_SIGNAL_DEL (queue);
1512 if (queue->use_buffering)
1513 update_buffering (queue);
1515 GST_DEBUG_OBJECT (queue, "waiting for add");
1516 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1520 /* we have the requested data so read it */
1521 read_length = remaining;
1524 /* set range reading_pos to actual reading position for this read */
1525 queue->current->reading_pos = rpos;
1527 /* configure how much and from where to read */
1528 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1530 (queue->current->rb_offset + (rpos -
1531 queue->current->offset)) % rb_size;
1532 if (file_offset + read_length > rb_size) {
1533 block_length = rb_size - file_offset;
1535 block_length = read_length;
1539 block_length = read_length;
1542 /* while we still have data to read, we loop */
1543 while (read_length > 0) {
1547 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1548 data, &read_return);
1549 if (ret != GST_FLOW_OK)
1552 file_offset += read_return;
1553 if (QUEUE_IS_USING_RING_BUFFER (queue))
1554 file_offset %= rb_size;
1556 data += read_return;
1557 read_length -= read_return;
1558 block_length = read_length;
1559 remaining -= read_return;
1561 rpos = (queue->current->reading_pos += read_return);
1562 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1564 GST_QUEUE2_SIGNAL_DEL (queue);
1565 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1568 gst_buffer_unmap (buf, &info);
1569 gst_buffer_resize (buf, 0, length);
1571 GST_BUFFER_OFFSET (buf) = offset;
1572 GST_BUFFER_OFFSET_END (buf) = offset + length;
1581 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1582 gst_buffer_unmap (buf, &info);
1583 if (*buffer == NULL)
1584 gst_buffer_unref (buf);
1585 return GST_FLOW_EOS;
1589 GST_DEBUG_OBJECT (queue, "we are flushing");
1590 gst_buffer_unmap (buf, &info);
1591 if (*buffer == NULL)
1592 gst_buffer_unref (buf);
1593 return GST_FLOW_FLUSHING;
1597 GST_DEBUG_OBJECT (queue, "we have a read error");
1598 gst_buffer_unmap (buf, &info);
1599 if (*buffer == NULL)
1600 gst_buffer_unref (buf);
1605 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE, (NULL),
1606 ("Can't write to buffer"));
1607 if (*buffer == NULL)
1608 gst_buffer_unref (buf);
1609 return GST_FLOW_ERROR;
1613 /* should be called with QUEUE_LOCK */
1614 static GstMiniObject *
1615 gst_queue2_read_item_from_file (GstQueue2 * queue)
1617 GstMiniObject *item;
1619 if (queue->stream_start_event != NULL) {
1620 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1621 queue->stream_start_event = NULL;
1622 } else if (queue->starting_segment != NULL) {
1623 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1624 queue->starting_segment = NULL;
1627 GstBuffer *buffer = NULL;
1628 guint64 reading_pos;
1630 reading_pos = queue->current->reading_pos;
1633 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1638 item = GST_MINI_OBJECT_CAST (buffer);
1641 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1651 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1652 * the temp filename. */
1654 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1659 if (queue->temp_file)
1660 goto already_opened;
1662 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1664 /* If temp_template was set, allocate a filename and open that file */
1667 if (queue->temp_template == NULL)
1670 /* make copy of the template, we don't want to change this */
1671 name = g_strdup (queue->temp_template);
1674 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1676 fd = g_mkstemp (name);
1680 goto mkstemp_failed;
1682 /* open the file for update/writing */
1683 queue->temp_file = fdopen (fd, "wb+");
1684 /* error creating file */
1685 if (queue->temp_file == NULL)
1688 g_free (queue->temp_location);
1689 queue->temp_location = name;
1691 GST_QUEUE2_MUTEX_UNLOCK (queue);
1693 /* we can't emit the notify with the lock */
1694 g_object_notify (G_OBJECT (queue), "temp-location");
1696 GST_QUEUE2_MUTEX_LOCK (queue);
1698 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1705 GST_DEBUG_OBJECT (queue, "temp file was already open");
1710 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1711 (_("No Temp directory specified.")), (NULL));
1716 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1717 (_("Could not create temp file \"%s\"."), queue->temp_template),
1724 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1725 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1734 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1737 if (queue->temp_file == NULL)
1740 GST_DEBUG_OBJECT (queue, "closing temp file");
1742 fflush (queue->temp_file);
1743 fclose (queue->temp_file);
1745 if (queue->temp_remove) {
1746 if (remove (queue->temp_location) < 0) {
1747 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1748 queue->temp_location, g_strerror (errno));
1752 queue->temp_file = NULL;
1753 clean_ranges (queue);
1757 gst_queue2_flush_temp_file (GstQueue2 * queue)
1759 if (queue->temp_file == NULL)
1762 GST_DEBUG_OBJECT (queue, "flushing temp file");
1764 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1768 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1770 if (!QUEUE_IS_USING_QUEUE (queue)) {
1771 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1772 gst_queue2_flush_temp_file (queue);
1773 init_ranges (queue);
1775 while (!g_queue_is_empty (&queue->queue)) {
1776 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1778 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1779 && GST_EVENT_IS_STICKY (qitem->item)
1780 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1781 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1782 gst_pad_store_sticky_event (queue->srcpad,
1783 GST_EVENT_CAST (qitem->item));
1786 /* Then lose another reference because we are supposed to destroy that
1787 data when flushing */
1788 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1789 gst_mini_object_unref (qitem->item);
1790 g_slice_free (GstQueue2Item, qitem);
1793 queue->last_query = FALSE;
1794 g_cond_signal (&queue->query_handled);
1795 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1796 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1797 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1798 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1799 queue->sink_tainted = queue->src_tainted = TRUE;
1800 if (queue->starting_segment != NULL)
1801 gst_event_unref (queue->starting_segment);
1802 queue->starting_segment = NULL;
1803 queue->segment_event_received = FALSE;
1804 gst_event_replace (&queue->stream_start_event, NULL);
1806 /* we deleted a lot of something */
1807 GST_QUEUE2_SIGNAL_DEL (queue);
1811 gst_queue2_wait_free_space (GstQueue2 * queue)
1813 /* We make space available if we're "full" according to whatever
1814 * the user defined as "full". */
1815 if (gst_queue2_is_filled (queue)) {
1818 /* pause the timer while we wait. The fact that we are waiting does not mean
1819 * the byterate on the input pad is lower */
1820 if ((started = queue->in_timer_started))
1821 g_timer_stop (queue->in_timer);
1823 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1824 "queue is full, waiting for free space");
1826 /* Wait for space to be available, we could be unlocked because of a flush. */
1827 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1829 while (gst_queue2_is_filled (queue));
1831 /* and continue if we were running before */
1833 g_timer_continue (queue->in_timer);
1840 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1846 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1849 guint8 *data, *ring_buffer;
1850 guint size, rb_size;
1851 guint64 writing_pos, new_writing_pos;
1852 GstQueue2Range *range, *prev, *next;
1853 gboolean do_seek = FALSE;
1855 if (QUEUE_IS_USING_RING_BUFFER (queue))
1856 writing_pos = queue->current->rb_writing_pos;
1858 writing_pos = queue->current->writing_pos;
1859 ring_buffer = queue->ring_buffer;
1860 rb_size = queue->ring_buffer_max_size;
1862 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
1863 goto buffer_read_error;
1868 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1872 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1873 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1874 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1875 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1876 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1882 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1885 /* calculate the space in the ring buffer not used by data from
1886 * the current range */
1887 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1888 /* wait until there is some free space */
1889 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1891 /* get the amount of space we have */
1892 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1894 /* calculate if we need to split or if we can write the entire
1896 to_write = MIN (size, space);
1898 /* the writing position in the ring buffer after writing (part
1899 * or all of) the buffer */
1900 new_writing_pos = (writing_pos + to_write) % rb_size;
1903 range = queue->ranges;
1905 /* if we need to overwrite data in the ring buffer, we need to
1908 * warning: this code is complicated and includes some
1909 * simplifications - pen, paper and diagrams for the cases
1912 guint64 range_data_start, range_data_end;
1913 GstQueue2Range *range_to_destroy = NULL;
1915 if (range == queue->current)
1918 range_data_start = range->rb_offset;
1919 range_data_end = range->rb_writing_pos;
1921 /* handle the special case where the range has no data in it */
1922 if (range->writing_pos == range->offset) {
1923 if (range != queue->current) {
1924 GST_DEBUG_OBJECT (queue,
1925 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1926 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1928 range_to_destroy = range;
1930 prev->next = range->next;
1935 if (range_data_end > range_data_start) {
1936 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1939 if (new_writing_pos > range_data_start) {
1940 if (new_writing_pos >= range_data_end) {
1941 GST_DEBUG_OBJECT (queue,
1942 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1943 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1945 range_to_destroy = range;
1947 prev->next = range->next;
1949 GST_DEBUG_OBJECT (queue,
1950 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1951 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1952 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1953 range->offset + new_writing_pos - range_data_start,
1955 range->offset += (new_writing_pos - range_data_start);
1956 range->rb_offset = new_writing_pos;
1960 guint64 new_wpos_virt = writing_pos + to_write;
1962 if (new_wpos_virt <= range_data_start)
1965 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1966 GST_DEBUG_OBJECT (queue,
1967 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1968 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1970 range_to_destroy = range;
1972 prev->next = range->next;
1974 GST_DEBUG_OBJECT (queue,
1975 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1976 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1977 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1978 range->offset + new_writing_pos - range_data_start,
1980 range->offset += (new_wpos_virt - range_data_start);
1981 range->rb_offset = new_writing_pos;
1986 if (!range_to_destroy)
1989 range = range->next;
1990 if (range_to_destroy) {
1991 if (range_to_destroy == queue->ranges)
1992 queue->ranges = range;
1993 g_slice_free (GstQueue2Range, range_to_destroy);
1994 range_to_destroy = NULL;
1999 new_writing_pos = writing_pos + to_write;
2002 if (QUEUE_IS_USING_TEMP_FILE (queue)
2003 && FSEEK_FILE (queue->temp_file, writing_pos))
2006 if (new_writing_pos > writing_pos) {
2007 GST_INFO_OBJECT (queue,
2008 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2009 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
2010 queue->current->writing_pos, queue->current->rb_writing_pos);
2011 /* either not using ring buffer or no wrapping, just write */
2012 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2013 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
2016 memcpy (ring_buffer + writing_pos, data, to_write);
2019 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
2020 /* try to merge with next range */
2021 while ((next = queue->current->next)) {
2022 GST_INFO_OBJECT (queue,
2023 "checking merge with next range %" G_GUINT64_FORMAT " < %"
2024 G_GUINT64_FORMAT, new_writing_pos, next->offset);
2025 if (new_writing_pos < next->offset)
2028 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
2031 /* remove the group */
2032 queue->current->next = next->next;
2034 /* We use the threshold to decide if we want to do a seek or simply
2035 * read the data again. If there is not so much data in the range we
2036 * prefer to avoid to seek and read it again. */
2037 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
2038 /* the new range had more data than the threshold, it's worth keeping
2039 * it and doing a seek. */
2040 new_writing_pos = next->writing_pos;
2043 g_slice_free (GstQueue2Range, next);
2045 goto update_and_signal;
2049 guint block_one, block_two;
2051 block_one = rb_size - writing_pos;
2052 block_two = to_write - block_one;
2054 if (block_one > 0) {
2055 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
2056 /* write data to end of ring buffer */
2057 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2058 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
2061 memcpy (ring_buffer + writing_pos, data, block_one);
2065 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
2068 if (block_two > 0) {
2069 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
2070 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2071 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
2074 memcpy (ring_buffer, data + block_one, block_two);
2080 /* update the writing positions */
2082 GST_INFO_OBJECT (queue,
2083 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2084 to_write, writing_pos, size);
2086 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2088 queue->current->writing_pos += to_write;
2089 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2091 queue->current->writing_pos = writing_pos = new_writing_pos;
2094 perform_seek_to_offset (queue, new_writing_pos);
2096 update_cur_level (queue, queue->current);
2098 /* update the buffering status */
2099 if (queue->use_buffering) {
2101 update_buffering (queue);
2102 msg = gst_queue2_get_buffering_message (queue);
2104 GST_QUEUE2_MUTEX_UNLOCK (queue);
2105 g_mutex_lock (&queue->buffering_post_lock);
2106 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
2107 g_mutex_unlock (&queue->buffering_post_lock);
2108 GST_QUEUE2_MUTEX_LOCK (queue);
2112 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2113 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2115 GST_QUEUE2_SIGNAL_ADD (queue);
2118 gst_buffer_unmap (buffer, &info);
2125 GST_DEBUG_OBJECT (queue, "we are flushing");
2126 gst_buffer_unmap (buffer, &info);
2127 /* FIXME - GST_FLOW_EOS ? */
2132 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2133 gst_buffer_unmap (buffer, &info);
2140 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2144 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2145 (_("Error while writing to download file.")),
2146 ("%s", g_strerror (errno)));
2149 gst_buffer_unmap (buffer, &info);
2154 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL),
2155 ("Can't read from buffer"));
2161 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2163 GstQueue2 *queue = q;
2165 GST_TRACE_OBJECT (queue,
2166 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2167 gst_buffer_get_size (*buf));
2169 if (!gst_queue2_create_write (queue, *buf)) {
2170 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2177 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2179 guint *p_size = data;
2182 buf_size = gst_buffer_get_size (*buf);
2183 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2184 *p_size += buf_size;
2188 /* enqueue an item an update the level stats */
2190 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2191 GstQueue2ItemType item_type)
2193 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2197 buffer = GST_BUFFER_CAST (item);
2198 size = gst_buffer_get_size (buffer);
2200 /* add buffer to the statistics */
2201 if (QUEUE_IS_USING_QUEUE (queue)) {
2202 queue->cur_level.buffers++;
2203 queue->cur_level.bytes += size;
2205 queue->bytes_in += size;
2207 /* apply new buffer to segment stats */
2208 apply_buffer (queue, buffer, &queue->sink_segment, size, TRUE);
2209 /* update the byterate stats */
2210 update_in_rates (queue, FALSE);
2212 if (!QUEUE_IS_USING_QUEUE (queue)) {
2213 /* FIXME - check return value? */
2214 gst_queue2_create_write (queue, buffer);
2216 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2217 GstBufferList *buffer_list;
2220 buffer_list = GST_BUFFER_LIST_CAST (item);
2222 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2223 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2225 /* add buffer to the statistics */
2226 if (QUEUE_IS_USING_QUEUE (queue)) {
2227 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2228 queue->cur_level.bytes += size;
2230 queue->bytes_in += size;
2232 /* apply new buffer to segment stats */
2233 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2235 /* update the byterate stats */
2236 update_in_rates (queue, FALSE);
2238 if (!QUEUE_IS_USING_QUEUE (queue)) {
2239 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2241 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2244 event = GST_EVENT_CAST (item);
2246 switch (GST_EVENT_TYPE (event)) {
2248 /* Zero the thresholds, this makes sure the queue is completely
2249 * filled and we can read all data from the queue. */
2250 GST_DEBUG_OBJECT (queue, "we have EOS");
2251 queue->is_eos = TRUE;
2252 /* Force updating the input bitrate */
2253 update_in_rates (queue, TRUE);
2255 case GST_EVENT_SEGMENT:
2256 apply_segment (queue, event, &queue->sink_segment, TRUE);
2257 /* This is our first new segment, we hold it
2258 * as we can't save it on the temp file */
2259 if (!QUEUE_IS_USING_QUEUE (queue)) {
2260 if (queue->segment_event_received)
2261 goto unexpected_event;
2263 queue->segment_event_received = TRUE;
2264 if (queue->starting_segment != NULL)
2265 gst_event_unref (queue->starting_segment);
2266 queue->starting_segment = event;
2269 /* a new segment allows us to accept more buffers if we got EOS
2270 * from downstream */
2271 queue->unexpected = FALSE;
2274 apply_gap (queue, event, &queue->sink_segment, TRUE);
2276 case GST_EVENT_STREAM_START:
2277 if (!QUEUE_IS_USING_QUEUE (queue)) {
2278 gst_event_replace (&queue->stream_start_event, event);
2279 gst_event_unref (event);
2283 case GST_EVENT_CAPS:{
2286 gst_event_parse_caps (event, &caps);
2287 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2289 if (!QUEUE_IS_USING_QUEUE (queue)) {
2290 GST_LOG ("Dropping caps event, not using queue");
2291 gst_event_unref (event);
2297 if (!QUEUE_IS_USING_QUEUE (queue))
2298 goto unexpected_event;
2301 } else if (GST_IS_QUERY (item)) {
2302 /* Can't happen as we check that in the caller */
2303 if (!QUEUE_IS_USING_QUEUE (queue))
2304 g_assert_not_reached ();
2306 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2307 item, GST_OBJECT_NAME (queue));
2308 /* we can't really unref since we don't know what it is */
2313 /* update the buffering status */
2314 if (queue->use_buffering)
2315 update_buffering (queue);
2317 if (QUEUE_IS_USING_QUEUE (queue)) {
2318 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2319 qitem->type = item_type;
2321 g_queue_push_tail (&queue->queue, qitem);
2323 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2326 GST_QUEUE2_SIGNAL_ADD (queue);
2334 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2336 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2337 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2338 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2339 gst_event_unref (GST_EVENT_CAST (item));
2344 /* dequeue an item from the queue and update level stats */
2345 static GstMiniObject *
2346 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2348 GstMiniObject *item;
2350 if (!QUEUE_IS_USING_QUEUE (queue)) {
2351 item = gst_queue2_read_item_from_file (queue);
2353 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2359 g_slice_free (GstQueue2Item, qitem);
2365 if (GST_IS_BUFFER (item)) {
2369 buffer = GST_BUFFER_CAST (item);
2370 size = gst_buffer_get_size (buffer);
2371 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2373 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2374 "retrieved buffer %p from queue", buffer);
2376 if (QUEUE_IS_USING_QUEUE (queue)) {
2377 queue->cur_level.buffers--;
2378 queue->cur_level.bytes -= size;
2380 queue->bytes_out += size;
2382 apply_buffer (queue, buffer, &queue->src_segment, size, FALSE);
2383 /* update the byterate stats */
2384 update_out_rates (queue);
2385 /* update the buffering */
2386 if (queue->use_buffering)
2387 update_buffering (queue);
2389 } else if (GST_IS_EVENT (item)) {
2390 GstEvent *event = GST_EVENT_CAST (item);
2392 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2394 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2395 "retrieved event %p from queue", event);
2397 switch (GST_EVENT_TYPE (event)) {
2399 /* queue is empty now that we dequeued the EOS */
2400 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2402 case GST_EVENT_SEGMENT:
2403 apply_segment (queue, event, &queue->src_segment, FALSE);
2406 apply_gap (queue, event, &queue->src_segment, FALSE);
2411 } else if (GST_IS_BUFFER_LIST (item)) {
2412 GstBufferList *buffer_list;
2415 buffer_list = GST_BUFFER_LIST_CAST (item);
2416 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2417 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2419 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2420 "retrieved buffer list %p from queue", buffer_list);
2422 if (QUEUE_IS_USING_QUEUE (queue)) {
2423 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2424 queue->cur_level.bytes -= size;
2426 queue->bytes_out += size;
2428 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2429 /* update the byterate stats */
2430 update_out_rates (queue);
2431 /* update the buffering */
2432 if (queue->use_buffering)
2433 update_buffering (queue);
2434 } else if (GST_IS_QUERY (item)) {
2435 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2436 "retrieved query %p from queue", item);
2437 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2440 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2441 item, GST_OBJECT_NAME (queue));
2443 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2445 GST_QUEUE2_SIGNAL_DEL (queue);
2452 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2457 static GstFlowReturn
2458 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2461 gboolean ret = TRUE;
2464 queue = GST_QUEUE2 (parent);
2466 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "Received event '%s'",
2467 GST_EVENT_TYPE_NAME (event));
2469 switch (GST_EVENT_TYPE (event)) {
2470 case GST_EVENT_FLUSH_START:
2472 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2474 ret = gst_pad_push_event (queue->srcpad, event);
2476 /* now unblock the chain function */
2477 GST_QUEUE2_MUTEX_LOCK (queue);
2478 queue->srcresult = GST_FLOW_FLUSHING;
2479 queue->sinkresult = GST_FLOW_FLUSHING;
2480 /* unblock the loop and chain functions */
2481 GST_QUEUE2_SIGNAL_ADD (queue);
2482 GST_QUEUE2_SIGNAL_DEL (queue);
2483 GST_QUEUE2_MUTEX_UNLOCK (queue);
2485 /* make sure it pauses, this should happen since we sent
2486 * flush_start downstream. */
2487 gst_pad_pause_task (queue->srcpad);
2488 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2490 GST_QUEUE2_MUTEX_LOCK (queue);
2491 queue->last_query = FALSE;
2492 g_cond_signal (&queue->query_handled);
2493 GST_QUEUE2_MUTEX_UNLOCK (queue);
2495 GST_QUEUE2_MUTEX_LOCK (queue);
2496 /* flush the sink pad */
2497 queue->sinkresult = GST_FLOW_FLUSHING;
2498 GST_QUEUE2_SIGNAL_DEL (queue);
2499 queue->last_query = FALSE;
2500 g_cond_signal (&queue->query_handled);
2501 GST_QUEUE2_MUTEX_UNLOCK (queue);
2503 gst_event_unref (event);
2507 case GST_EVENT_FLUSH_STOP:
2509 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2511 ret = gst_pad_push_event (queue->srcpad, event);
2513 GST_QUEUE2_MUTEX_LOCK (queue);
2514 gst_queue2_locked_flush (queue, FALSE, TRUE);
2515 queue->srcresult = GST_FLOW_OK;
2516 queue->sinkresult = GST_FLOW_OK;
2517 queue->is_eos = FALSE;
2518 queue->unexpected = FALSE;
2519 queue->seeking = FALSE;
2520 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2521 /* reset rate counters */
2522 reset_rate_timer (queue);
2523 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2524 queue->srcpad, NULL);
2525 GST_QUEUE2_MUTEX_UNLOCK (queue);
2527 GST_QUEUE2_MUTEX_LOCK (queue);
2528 queue->segment_event_received = FALSE;
2529 queue->is_eos = FALSE;
2530 queue->unexpected = FALSE;
2531 queue->sinkresult = GST_FLOW_OK;
2532 queue->seeking = FALSE;
2533 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2534 GST_QUEUE2_MUTEX_UNLOCK (queue);
2536 gst_event_unref (event);
2540 case GST_EVENT_TAG:{
2541 if (queue->use_tags_bitrate) {
2545 gst_event_parse_tag (event, &tags);
2546 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2547 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2548 GST_QUEUE2_MUTEX_LOCK (queue);
2549 queue->sink_tags_bitrate = bitrate;
2550 GST_QUEUE2_MUTEX_UNLOCK (queue);
2551 GST_LOG_OBJECT (queue, "Sink pad bitrate from tags now %u", bitrate);
2557 if (GST_EVENT_IS_SERIALIZED (event)) {
2558 /* serialized events go in the queue */
2560 /* STREAM_START and SEGMENT reset the EOS status of a
2561 * pad. Change the cached sinkpad flow result accordingly */
2562 if (queue->sinkresult == GST_FLOW_EOS
2563 && (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START
2564 || GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT))
2565 queue->sinkresult = GST_FLOW_OK;
2567 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2568 if (queue->srcresult != GST_FLOW_OK) {
2569 /* Errors in sticky event pushing are no problem and ignored here
2570 * as they will cause more meaningful errors during data flow.
2571 * For EOS events, that are not followed by data flow, we still
2572 * return FALSE here though and report an error.
2574 if (!GST_EVENT_IS_STICKY (event)) {
2575 goto out_flow_error;
2576 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2577 if (queue->srcresult == GST_FLOW_NOT_LINKED
2578 || queue->srcresult < GST_FLOW_EOS) {
2579 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
2581 goto out_flow_error;
2585 /* refuse more events on EOS unless they unset the EOS status */
2586 if (queue->is_eos) {
2587 switch (GST_EVENT_TYPE (event)) {
2588 case GST_EVENT_STREAM_START:
2589 case GST_EVENT_SEGMENT:
2590 /* Restart the loop */
2591 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2592 queue->srcresult = GST_FLOW_OK;
2593 queue->is_eos = FALSE;
2594 queue->unexpected = FALSE;
2595 queue->seeking = FALSE;
2596 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2597 /* reset rate counters */
2598 reset_rate_timer (queue);
2599 gst_pad_start_task (queue->srcpad,
2600 (GstTaskFunction) gst_queue2_loop, queue->srcpad, NULL);
2602 queue->is_eos = FALSE;
2603 queue->unexpected = FALSE;
2604 queue->seeking = FALSE;
2605 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2614 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2615 GST_QUEUE2_MUTEX_UNLOCK (queue);
2616 gst_queue2_post_buffering (queue);
2618 /* non-serialized events are passed downstream. */
2619 ret = gst_pad_push_event (queue->srcpad, event);
2624 return GST_FLOW_ERROR;
2630 GstFlowReturn ret = queue->sinkresult;
2631 GST_DEBUG_OBJECT (queue, "refusing event, we are %s",
2632 gst_flow_get_name (ret));
2633 GST_QUEUE2_MUTEX_UNLOCK (queue);
2634 gst_event_unref (event);
2639 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2640 GST_QUEUE2_MUTEX_UNLOCK (queue);
2641 gst_event_unref (event);
2642 return GST_FLOW_EOS;
2646 GST_LOG_OBJECT (queue,
2647 "refusing event, we have a downstream flow error: %s",
2648 gst_flow_get_name (queue->srcresult));
2649 GST_QUEUE2_MUTEX_UNLOCK (queue);
2650 gst_event_unref (event);
2651 return queue->srcresult;
2656 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2662 queue = GST_QUEUE2 (parent);
2664 switch (GST_QUERY_TYPE (query)) {
2666 if (GST_QUERY_IS_SERIALIZED (query)) {
2667 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2668 /* serialized events go in the queue. We need to be certain that we
2669 * don't cause deadlocks waiting for the query return value. We check if
2670 * the queue is empty (nothing is blocking downstream and the query can
2671 * be pushed for sure) or we are not buffering. If we are buffering,
2672 * the pipeline waits to unblock downstream until our queue fills up
2673 * completely, which can not happen if we block on the query..
2674 * Therefore we only potentially block when we are not buffering. */
2675 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2676 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2677 || !queue->use_buffering)) {
2678 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2679 gst_queue2_locked_enqueue (queue, query,
2680 GST_QUEUE2_ITEM_TYPE_QUERY);
2682 STATUS (queue, queue->sinkpad, "wait for QUERY");
2683 while (queue->sinkresult == GST_FLOW_OK &&
2684 queue->last_handled_query != query)
2685 g_cond_wait (&queue->query_handled, &queue->qlock);
2686 queue->last_handled_query = NULL;
2687 if (queue->sinkresult != GST_FLOW_OK)
2689 res = queue->last_query;
2691 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2695 GST_DEBUG_OBJECT (queue,
2696 "refusing query, we are not using the queue");
2699 GST_QUEUE2_MUTEX_UNLOCK (queue);
2700 gst_queue2_post_buffering (queue);
2702 res = gst_pad_query_default (pad, parent, query);
2711 GST_DEBUG_OBJECT (queue, "refusing query, we are %s",
2712 gst_flow_get_name (queue->sinkresult));
2713 GST_QUEUE2_MUTEX_UNLOCK (queue);
2719 gst_queue2_is_empty (GstQueue2 * queue)
2721 /* never empty on EOS */
2725 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2726 return queue->current->writing_pos <= queue->current->max_reading_pos;
2728 if (queue->queue.length == 0)
2736 gst_queue2_is_filled (GstQueue2 * queue)
2740 /* always filled on EOS */
2744 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2745 (queue->cur_level.format) >= ((alt_max) ? \
2746 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2748 /* if using a ring buffer we're filled if all ring buffer space is used
2749 * _by the current range_ */
2750 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2751 guint64 rb_size = queue->ring_buffer_max_size;
2752 GST_DEBUG_OBJECT (queue,
2753 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2754 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2755 return CHECK_FILLED (bytes, rb_size);
2758 /* if using file, we're never filled if we don't have EOS */
2759 if (QUEUE_IS_USING_TEMP_FILE (queue))
2762 /* we are never filled when we have no buffers at all */
2763 if (queue->cur_level.buffers == 0)
2766 /* we are filled if one of the current levels exceeds the max */
2767 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2768 || CHECK_FILLED (time, 0);
2770 /* if we need to, use the rate estimate to check against the max time we are
2771 * allowed to queue */
2772 if (queue->use_rate_estimate)
2773 res |= CHECK_FILLED (rate_time, 0);
2779 static GstFlowReturn
2780 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2781 GstMiniObject * item, GstQueue2ItemType item_type)
2783 /* we have to lock the queue since we span threads */
2784 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2785 /* when we received EOS, we refuse more data */
2788 /* when we received unexpected from downstream, refuse more buffers */
2789 if (queue->unexpected)
2790 goto out_unexpected;
2792 /* while we didn't receive the newsegment, we're seeking and we skip data */
2796 if (!gst_queue2_wait_free_space (queue))
2799 /* put buffer in queue now */
2800 gst_queue2_locked_enqueue (queue, item, item_type);
2801 GST_QUEUE2_MUTEX_UNLOCK (queue);
2802 gst_queue2_post_buffering (queue);
2806 /* special conditions */
2809 GstFlowReturn ret = queue->sinkresult;
2811 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2812 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2813 GST_QUEUE2_MUTEX_UNLOCK (queue);
2814 gst_mini_object_unref (item);
2820 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2821 GST_QUEUE2_MUTEX_UNLOCK (queue);
2822 gst_mini_object_unref (item);
2824 return GST_FLOW_EOS;
2828 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2829 GST_QUEUE2_MUTEX_UNLOCK (queue);
2830 gst_mini_object_unref (item);
2836 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2837 GST_QUEUE2_MUTEX_UNLOCK (queue);
2838 gst_mini_object_unref (item);
2840 return GST_FLOW_EOS;
2844 static GstFlowReturn
2845 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2849 queue = GST_QUEUE2 (parent);
2851 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2852 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2853 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2854 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2855 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2857 return gst_queue2_chain_buffer_or_buffer_list (queue,
2858 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2861 static GstFlowReturn
2862 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2863 GstBufferList * buffer_list)
2867 queue = GST_QUEUE2 (parent);
2869 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2870 "received buffer list %p", buffer_list);
2872 return gst_queue2_chain_buffer_or_buffer_list (queue,
2873 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2876 static GstMiniObject *
2877 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2879 GstMiniObject *data;
2881 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2883 /* stop pushing buffers, we dequeue all items until we see an item that we
2884 * can push again, which is EOS or SEGMENT. If there is nothing in the
2885 * queue we can push, we set a flag to make the sinkpad refuse more
2886 * buffers with an EOS return value until we receive something
2887 * pushable again or we get flushed. */
2888 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2889 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2890 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2891 "dropping EOS buffer %p", data);
2892 gst_buffer_unref (GST_BUFFER_CAST (data));
2893 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2894 GstEvent *event = GST_EVENT_CAST (data);
2895 GstEventType type = GST_EVENT_TYPE (event);
2897 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT
2898 || type == GST_EVENT_STREAM_START) {
2899 /* we found a pushable item in the queue, push it out */
2900 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2901 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2904 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2905 "dropping EOS event %p", event);
2906 gst_event_unref (event);
2907 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2908 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2909 "dropping EOS buffer list %p", data);
2910 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2911 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2912 queue->last_query = FALSE;
2913 g_cond_signal (&queue->query_handled);
2914 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2917 /* no more items in the queue. Set the unexpected flag so that upstream
2918 * make us refuse any more buffers on the sinkpad. Since we will still
2919 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2920 * task function does not shut down. */
2921 queue->unexpected = TRUE;
2925 /* dequeue an item from the queue an push it downstream. This functions returns
2926 * the result of the push. */
2927 static GstFlowReturn
2928 gst_queue2_push_one (GstQueue2 * queue)
2930 GstFlowReturn result = queue->srcresult;
2931 GstMiniObject *data;
2932 GstQueue2ItemType item_type;
2934 data = gst_queue2_locked_dequeue (queue, &item_type);
2939 STATUS (queue, queue->srcpad, "We have something dequeud");
2940 g_atomic_int_set (&queue->downstream_may_block,
2941 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2942 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2943 GST_QUEUE2_MUTEX_UNLOCK (queue);
2944 gst_queue2_post_buffering (queue);
2946 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2949 buffer = GST_BUFFER_CAST (data);
2951 result = gst_pad_push (queue->srcpad, buffer);
2952 g_atomic_int_set (&queue->downstream_may_block, 0);
2954 /* need to check for srcresult here as well */
2955 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2956 if (result == GST_FLOW_EOS) {
2957 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2960 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2961 * to the caller so that the task function does not shut down */
2962 result = GST_FLOW_OK;
2964 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2965 GstEvent *event = GST_EVENT_CAST (data);
2966 GstEventType type = GST_EVENT_TYPE (event);
2968 if (type == GST_EVENT_TAG) {
2969 if (queue->use_tags_bitrate) {
2973 gst_event_parse_tag (event, &tags);
2974 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2975 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2976 GST_QUEUE2_MUTEX_LOCK (queue);
2977 queue->src_tags_bitrate = bitrate;
2978 GST_QUEUE2_MUTEX_UNLOCK (queue);
2979 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
2984 gst_pad_push_event (queue->srcpad, event);
2986 /* if we're EOS, return EOS so that the task pauses. */
2987 if (type == GST_EVENT_EOS) {
2988 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2989 "pushed EOS event %p, return EOS", event);
2990 result = GST_FLOW_EOS;
2993 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2994 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2995 GstBufferList *buffer_list;
2997 buffer_list = GST_BUFFER_LIST_CAST (data);
2999 result = gst_pad_push_list (queue->srcpad, buffer_list);
3000 g_atomic_int_set (&queue->downstream_may_block, 0);
3002 /* need to check for srcresult here as well */
3003 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3004 if (result == GST_FLOW_EOS) {
3005 data = gst_queue2_dequeue_on_eos (queue, &item_type);
3008 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
3009 * to the caller so that the task function does not shut down */
3010 result = GST_FLOW_OK;
3012 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
3013 GstQuery *query = GST_QUERY_CAST (data);
3015 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
3016 queue->last_handled_query = query;
3017 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
3018 GST_LOG_OBJECT (queue->srcpad, "Peered query");
3019 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3020 "did query %p, return %d", query, queue->last_query);
3021 g_cond_signal (&queue->query_handled);
3022 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3023 result = GST_FLOW_OK;
3030 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3031 "exit because we have no item in the queue");
3032 return GST_FLOW_ERROR;
3036 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are %s",
3037 gst_flow_get_name (queue->srcresult));
3038 return queue->srcresult;
3042 /* called repeatedly with @pad as the source pad. This function should push out
3043 * data to the peer element. */
3045 gst_queue2_loop (GstPad * pad)
3050 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
3052 /* have to lock for thread-safety */
3053 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3055 if (gst_queue2_is_empty (queue)) {
3058 /* pause the timer while we wait. The fact that we are waiting does not mean
3059 * the byterate on the output pad is lower */
3060 if ((started = queue->out_timer_started))
3061 g_timer_stop (queue->out_timer);
3063 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
3064 "queue is empty, waiting for new data");
3066 /* Wait for data to be available, we could be unlocked because of a flush. */
3067 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
3069 while (gst_queue2_is_empty (queue));
3071 /* and continue if we were running before */
3073 g_timer_continue (queue->out_timer);
3075 ret = gst_queue2_push_one (queue);
3076 queue->srcresult = ret;
3077 queue->sinkresult = ret;
3078 if (ret != GST_FLOW_OK)
3081 GST_QUEUE2_MUTEX_UNLOCK (queue);
3082 gst_queue2_post_buffering (queue);
3089 gboolean eos = queue->is_eos;
3090 GstFlowReturn ret = queue->srcresult;
3092 gst_pad_pause_task (queue->srcpad);
3093 if (ret == GST_FLOW_FLUSHING) {
3094 gst_queue2_locked_flush (queue, FALSE, FALSE);
3096 GST_QUEUE2_SIGNAL_DEL (queue);
3097 queue->last_query = FALSE;
3098 g_cond_signal (&queue->query_handled);
3100 GST_QUEUE2_MUTEX_UNLOCK (queue);
3101 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3102 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
3103 /* Recalculate buffering levels before stopping since the source flow
3104 * might cause a different buffering level (like NOT_LINKED making
3105 * the queue appear as full) */
3106 if (queue->use_buffering)
3107 update_buffering (queue);
3108 gst_queue2_post_buffering (queue);
3109 /* let app know about us giving up if upstream is not expected to do so */
3110 /* EOS is already taken care of elsewhere */
3111 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
3112 GST_ELEMENT_FLOW_ERROR (queue, ret);
3113 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
3120 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
3122 gboolean res = TRUE;
3123 GstQueue2 *queue = GST_QUEUE2 (parent);
3125 #ifndef GST_DISABLE_GST_DEBUG
3126 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
3127 event, GST_EVENT_TYPE_NAME (event));
3130 switch (GST_EVENT_TYPE (event)) {
3131 case GST_EVENT_FLUSH_START:
3132 if (QUEUE_IS_USING_QUEUE (queue)) {
3133 /* just forward upstream */
3134 res = gst_pad_push_event (queue->sinkpad, event);
3136 /* now unblock the getrange function */
3137 GST_QUEUE2_MUTEX_LOCK (queue);
3138 GST_DEBUG_OBJECT (queue, "flushing");
3139 queue->srcresult = GST_FLOW_FLUSHING;
3140 GST_QUEUE2_SIGNAL_ADD (queue);
3141 GST_QUEUE2_MUTEX_UNLOCK (queue);
3143 /* when using a temp file, we eat the event */
3145 gst_event_unref (event);
3148 case GST_EVENT_FLUSH_STOP:
3149 if (QUEUE_IS_USING_QUEUE (queue)) {
3150 /* just forward upstream */
3151 res = gst_pad_push_event (queue->sinkpad, event);
3153 /* now unblock the getrange function */
3154 GST_QUEUE2_MUTEX_LOCK (queue);
3155 queue->srcresult = GST_FLOW_OK;
3156 GST_QUEUE2_MUTEX_UNLOCK (queue);
3158 /* when using a temp file, we eat the event */
3160 gst_event_unref (event);
3163 case GST_EVENT_RECONFIGURE:
3164 GST_QUEUE2_MUTEX_LOCK (queue);
3165 /* assume downstream is linked now and try to push again */
3166 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3167 queue->srcresult = GST_FLOW_OK;
3168 queue->sinkresult = GST_FLOW_OK;
3169 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3170 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3174 GST_QUEUE2_MUTEX_UNLOCK (queue);
3176 res = gst_pad_push_event (queue->sinkpad, event);
3179 res = gst_pad_push_event (queue->sinkpad, event);
3187 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3191 queue = GST_QUEUE2 (parent);
3193 switch (GST_QUERY_TYPE (query)) {
3194 case GST_QUERY_POSITION:
3199 if (!gst_pad_peer_query (queue->sinkpad, query))
3202 /* get peer position */
3203 gst_query_parse_position (query, &format, &peer_pos);
3205 /* FIXME: this code assumes that there's no discont in the queue */
3207 case GST_FORMAT_BYTES:
3208 peer_pos -= queue->cur_level.bytes;
3209 if (peer_pos < 0) /* Clamp result to 0 */
3212 case GST_FORMAT_TIME:
3213 peer_pos -= queue->cur_level.time;
3214 if (peer_pos < 0) /* Clamp result to 0 */
3218 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3219 "know how to adjust value", gst_format_get_name (format));
3222 /* set updated position */
3223 gst_query_set_position (query, format, peer_pos);
3226 case GST_QUERY_DURATION:
3228 GST_DEBUG_OBJECT (queue, "doing peer query");
3230 if (!gst_pad_peer_query (queue->sinkpad, query))
3233 GST_DEBUG_OBJECT (queue, "peer query success");
3236 case GST_QUERY_BUFFERING:
3239 gboolean is_buffering;
3240 GstBufferingMode mode;
3241 gint avg_in, avg_out;
3242 gint64 buffering_left;
3244 GST_DEBUG_OBJECT (queue, "query buffering");
3246 get_buffering_level (queue, &is_buffering, &percent);
3247 percent = convert_to_buffering_percent (queue, percent);
3248 gst_query_set_buffering_percent (query, is_buffering, percent);
3250 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3252 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3255 if (!QUEUE_IS_USING_QUEUE (queue)) {
3256 /* add ranges for download and ringbuffer buffering */
3258 gint64 start, stop, range_start, range_stop;
3259 guint64 writing_pos;
3260 gint64 estimated_total;
3262 gboolean peer_res, is_eos;
3263 GstQueue2Range *queued_ranges;
3265 /* we need a current download region */
3266 if (queue->current == NULL)
3269 writing_pos = queue->current->writing_pos;
3270 is_eos = queue->is_eos;
3273 /* we're EOS, we know the duration in bytes now */
3275 duration = writing_pos;
3277 /* get duration of upstream in bytes */
3278 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3279 GST_FORMAT_BYTES, &duration);
3282 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3283 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3285 /* calculate remaining and total download time */
3286 if (peer_res && avg_in > 0.0)
3287 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3289 estimated_total = -1;
3291 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3294 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3297 case GST_FORMAT_PERCENT:
3298 /* we need duration */
3303 /* get our available data relative to the duration */
3306 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3311 case GST_FORMAT_BYTES:
3321 /* fill out the buffered ranges */
3322 for (queued_ranges = queue->ranges; queued_ranges;
3323 queued_ranges = queued_ranges->next) {
3325 case GST_FORMAT_PERCENT:
3326 if (duration == -1) {
3332 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3333 queued_ranges->offset, duration);
3335 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3336 queued_ranges->writing_pos, duration);
3338 case GST_FORMAT_BYTES:
3339 range_start = queued_ranges->offset;
3340 range_stop = queued_ranges->writing_pos;
3347 if (range_start == range_stop)
3349 GST_DEBUG_OBJECT (queue,
3350 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3351 G_GINT64_FORMAT, range_start, range_stop);
3352 gst_query_add_buffering_range (query, range_start, range_stop);
3355 gst_query_set_buffering_range (query, format, start, stop,
3360 case GST_QUERY_SCHEDULING:
3363 GstSchedulingFlags flags = 0;
3365 if (!gst_pad_peer_query (queue->sinkpad, query))
3368 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3370 /* we can operate in pull mode when we are using a tempfile */
3371 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3374 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3375 gst_query_set_scheduling (query, flags, 0, -1, 0);
3377 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3378 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3382 /* peer handled other queries */
3383 if (!gst_pad_query_default (pad, parent, query))
3393 GST_DEBUG_OBJECT (queue, "failed peer query");
3399 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3401 GstQueue2 *queue = GST_QUEUE2 (element);
3403 /* simply forward to the srcpad query function */
3404 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3409 gst_queue2_update_upstream_size (GstQueue2 * queue)
3411 gint64 upstream_size = -1;
3413 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3415 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3417 /* upstream_size can be negative but queue->upstream_size is unsigned.
3418 * Prevent setting negative values to it (the query can return -1) */
3419 if (upstream_size >= 0)
3420 queue->upstream_size = upstream_size;
3422 queue->upstream_size = 0;
3426 static GstFlowReturn
3427 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3428 guint length, GstBuffer ** buffer)
3433 queue = GST_QUEUE2_CAST (parent);
3435 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3436 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3437 offset = (offset == -1) ? queue->current->reading_pos : offset;
3439 GST_DEBUG_OBJECT (queue,
3440 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3442 /* catch any reads beyond the size of the file here to make sure queue2
3443 * doesn't send seek events beyond the size of the file upstream, since
3444 * that would confuse elements such as souphttpsrc and/or http servers.
3445 * Demuxers often just loop until EOS at the end of the file to figure out
3446 * when they've read all the end-headers or index chunks. */
3447 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3448 gst_queue2_update_upstream_size (queue);
3449 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3450 goto out_unexpected;
3453 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3454 gst_queue2_update_upstream_size (queue);
3455 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3456 length = queue->upstream_size - offset;
3457 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3461 /* FIXME - function will block when the range is not yet available */
3462 ret = gst_queue2_create_read (queue, offset, length, buffer);
3463 GST_QUEUE2_MUTEX_UNLOCK (queue);
3464 gst_queue2_post_buffering (queue);
3471 ret = queue->srcresult;
3473 GST_DEBUG_OBJECT (queue, "we are %s", gst_flow_get_name (ret));
3474 GST_QUEUE2_MUTEX_UNLOCK (queue);
3479 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3480 GST_QUEUE2_MUTEX_UNLOCK (queue);
3481 return GST_FLOW_EOS;
3485 /* sink currently only operates in push mode */
3487 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3488 GstPadMode mode, gboolean active)
3493 queue = GST_QUEUE2 (parent);
3496 case GST_PAD_MODE_PUSH:
3498 GST_QUEUE2_MUTEX_LOCK (queue);
3499 GST_DEBUG_OBJECT (queue, "activating push mode");
3500 queue->srcresult = GST_FLOW_OK;
3501 queue->sinkresult = GST_FLOW_OK;
3502 queue->is_eos = FALSE;
3503 queue->unexpected = FALSE;
3504 reset_rate_timer (queue);
3505 GST_QUEUE2_MUTEX_UNLOCK (queue);
3507 /* unblock chain function */
3508 GST_QUEUE2_MUTEX_LOCK (queue);
3509 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3510 queue->srcresult = GST_FLOW_FLUSHING;
3511 queue->sinkresult = GST_FLOW_FLUSHING;
3512 GST_QUEUE2_SIGNAL_DEL (queue);
3513 GST_QUEUE2_MUTEX_UNLOCK (queue);
3515 /* wait until it is unblocked and clean up */
3516 GST_PAD_STREAM_LOCK (pad);
3517 GST_QUEUE2_MUTEX_LOCK (queue);
3518 gst_queue2_locked_flush (queue, TRUE, FALSE);
3519 GST_QUEUE2_MUTEX_UNLOCK (queue);
3520 GST_PAD_STREAM_UNLOCK (pad);
3531 /* src operating in push mode, we start a task on the source pad that pushes out
3532 * buffers from the queue */
3534 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3536 gboolean result = FALSE;
3539 queue = GST_QUEUE2 (parent);
3542 GST_QUEUE2_MUTEX_LOCK (queue);
3543 GST_DEBUG_OBJECT (queue, "activating push mode");
3544 queue->srcresult = GST_FLOW_OK;
3545 queue->sinkresult = GST_FLOW_OK;
3546 queue->is_eos = FALSE;
3547 queue->unexpected = FALSE;
3549 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3550 GST_QUEUE2_MUTEX_UNLOCK (queue);
3552 /* unblock loop function */
3553 GST_QUEUE2_MUTEX_LOCK (queue);
3554 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3555 queue->srcresult = GST_FLOW_FLUSHING;
3556 queue->sinkresult = GST_FLOW_FLUSHING;
3557 /* the item add signal will unblock */
3558 GST_QUEUE2_SIGNAL_ADD (queue);
3559 GST_QUEUE2_MUTEX_UNLOCK (queue);
3561 /* step 2, make sure streaming finishes */
3562 result = gst_pad_stop_task (pad);
3564 GST_QUEUE2_MUTEX_LOCK (queue);
3565 gst_queue2_locked_flush (queue, FALSE, FALSE);
3566 GST_QUEUE2_MUTEX_UNLOCK (queue);
3572 /* pull mode, downstream will call our getrange function */
3574 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3579 queue = GST_QUEUE2 (parent);
3582 GST_QUEUE2_MUTEX_LOCK (queue);
3583 if (!QUEUE_IS_USING_QUEUE (queue)) {
3584 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3585 /* open the temp file now */
3586 result = gst_queue2_open_temp_location_file (queue);
3587 } else if (!queue->ring_buffer) {
3588 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3589 result = ! !queue->ring_buffer;
3594 GST_DEBUG_OBJECT (queue, "activating pull mode");
3595 init_ranges (queue);
3596 queue->srcresult = GST_FLOW_OK;
3597 queue->sinkresult = GST_FLOW_OK;
3598 queue->is_eos = FALSE;
3599 queue->unexpected = FALSE;
3600 queue->upstream_size = 0;
3602 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3603 /* this is not allowed, we cannot operate in pull mode without a temp
3605 queue->srcresult = GST_FLOW_FLUSHING;
3606 queue->sinkresult = GST_FLOW_FLUSHING;
3609 GST_QUEUE2_MUTEX_UNLOCK (queue);
3611 GST_QUEUE2_MUTEX_LOCK (queue);
3612 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3613 queue->srcresult = GST_FLOW_FLUSHING;
3614 queue->sinkresult = GST_FLOW_FLUSHING;
3615 /* this will unlock getrange */
3616 GST_QUEUE2_SIGNAL_ADD (queue);
3618 GST_QUEUE2_MUTEX_UNLOCK (queue);
3625 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3631 case GST_PAD_MODE_PULL:
3632 res = gst_queue2_src_activate_pull (pad, parent, active);
3634 case GST_PAD_MODE_PUSH:
3635 res = gst_queue2_src_activate_push (pad, parent, active);
3638 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3645 static GstStateChangeReturn
3646 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3649 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3651 queue = GST_QUEUE2 (element);
3653 switch (transition) {
3654 case GST_STATE_CHANGE_NULL_TO_READY:
3656 case GST_STATE_CHANGE_READY_TO_PAUSED:
3657 GST_QUEUE2_MUTEX_LOCK (queue);
3658 if (!QUEUE_IS_USING_QUEUE (queue)) {
3659 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3660 if (!gst_queue2_open_temp_location_file (queue))
3661 ret = GST_STATE_CHANGE_FAILURE;
3663 if (queue->ring_buffer) {
3664 g_free (queue->ring_buffer);
3665 queue->ring_buffer = NULL;
3667 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3668 ret = GST_STATE_CHANGE_FAILURE;
3670 init_ranges (queue);
3672 queue->segment_event_received = FALSE;
3673 queue->starting_segment = NULL;
3674 gst_event_replace (&queue->stream_start_event, NULL);
3675 GST_QUEUE2_MUTEX_UNLOCK (queue);
3677 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3683 if (ret == GST_STATE_CHANGE_FAILURE)
3686 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3688 if (ret == GST_STATE_CHANGE_FAILURE)
3691 switch (transition) {
3692 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3694 case GST_STATE_CHANGE_PAUSED_TO_READY:
3695 GST_QUEUE2_MUTEX_LOCK (queue);
3696 if (!QUEUE_IS_USING_QUEUE (queue)) {
3697 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3698 gst_queue2_close_temp_location_file (queue);
3699 } else if (queue->ring_buffer) {
3700 g_free (queue->ring_buffer);
3701 queue->ring_buffer = NULL;
3703 clean_ranges (queue);
3705 if (queue->starting_segment != NULL) {
3706 gst_event_unref (queue->starting_segment);
3707 queue->starting_segment = NULL;
3709 gst_event_replace (&queue->stream_start_event, NULL);
3710 GST_QUEUE2_MUTEX_UNLOCK (queue);
3712 case GST_STATE_CHANGE_READY_TO_NULL:
3721 /* changing the capacity of the queue must wake up
3722 * the _chain function, it might have more room now
3723 * to store the buffer/event in the queue */
3724 #define QUEUE_CAPACITY_CHANGE(q) \
3725 GST_QUEUE2_SIGNAL_DEL (queue); \
3726 if (queue->use_buffering) \
3727 update_buffering (queue);
3729 /* Changing the minimum required fill level must
3730 * wake up the _loop function as it might now
3731 * be able to preceed.
3733 #define QUEUE_THRESHOLD_CHANGE(q)\
3734 GST_QUEUE2_SIGNAL_ADD (queue);
3737 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3741 /* the element must be stopped in order to do this */
3742 GST_OBJECT_LOCK (queue);
3743 state = GST_STATE (queue);
3744 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3746 GST_OBJECT_UNLOCK (queue);
3748 /* set new location */
3749 g_free (queue->temp_template);
3750 queue->temp_template = g_strdup (template);
3757 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3758 GST_OBJECT_UNLOCK (queue);
3763 gst_queue2_set_property (GObject * object,
3764 guint prop_id, const GValue * value, GParamSpec * pspec)
3766 GstQueue2 *queue = GST_QUEUE2 (object);
3768 /* someone could change levels here, and since this
3769 * affects the get/put funcs, we need to lock for safety. */
3770 GST_QUEUE2_MUTEX_LOCK (queue);
3773 case PROP_MAX_SIZE_BYTES:
3774 queue->max_level.bytes = g_value_get_uint (value);
3775 QUEUE_CAPACITY_CHANGE (queue);
3777 case PROP_MAX_SIZE_BUFFERS:
3778 queue->max_level.buffers = g_value_get_uint (value);
3779 QUEUE_CAPACITY_CHANGE (queue);
3781 case PROP_MAX_SIZE_TIME:
3782 queue->max_level.time = g_value_get_uint64 (value);
3783 /* set rate_time to the same value. We use an extra field in the level
3784 * structure so that we can easily access and compare it */
3785 queue->max_level.rate_time = queue->max_level.time;
3786 QUEUE_CAPACITY_CHANGE (queue);
3788 case PROP_USE_BUFFERING:
3789 queue->use_buffering = g_value_get_boolean (value);
3790 if (!queue->use_buffering && queue->is_buffering) {
3791 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3792 "posting 100%% message");
3793 SET_PERCENT (queue, 100);
3794 queue->is_buffering = FALSE;
3797 if (queue->use_buffering) {
3798 queue->is_buffering = TRUE;
3799 update_buffering (queue);
3802 case PROP_USE_TAGS_BITRATE:
3803 queue->use_tags_bitrate = g_value_get_boolean (value);
3805 case PROP_USE_RATE_ESTIMATE:
3806 queue->use_rate_estimate = g_value_get_boolean (value);
3808 case PROP_LOW_PERCENT:
3809 queue->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3810 if (queue->is_buffering)
3811 update_buffering (queue);
3813 case PROP_HIGH_PERCENT:
3814 queue->high_watermark =
3815 g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3816 if (queue->is_buffering)
3817 update_buffering (queue);
3819 case PROP_LOW_WATERMARK:
3820 queue->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3821 if (queue->is_buffering)
3822 update_buffering (queue);
3824 case PROP_HIGH_WATERMARK:
3825 queue->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3826 if (queue->is_buffering)
3827 update_buffering (queue);
3829 case PROP_TEMP_TEMPLATE:
3830 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3832 case PROP_TEMP_REMOVE:
3833 queue->temp_remove = g_value_get_boolean (value);
3835 case PROP_RING_BUFFER_MAX_SIZE:
3836 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3839 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3843 GST_QUEUE2_MUTEX_UNLOCK (queue);
3844 gst_queue2_post_buffering (queue);
3848 gst_queue2_get_property (GObject * object,
3849 guint prop_id, GValue * value, GParamSpec * pspec)
3851 GstQueue2 *queue = GST_QUEUE2 (object);
3853 GST_QUEUE2_MUTEX_LOCK (queue);
3856 case PROP_CUR_LEVEL_BYTES:
3857 g_value_set_uint (value, queue->cur_level.bytes);
3859 case PROP_CUR_LEVEL_BUFFERS:
3860 g_value_set_uint (value, queue->cur_level.buffers);
3862 case PROP_CUR_LEVEL_TIME:
3863 g_value_set_uint64 (value, queue->cur_level.time);
3865 case PROP_MAX_SIZE_BYTES:
3866 g_value_set_uint (value, queue->max_level.bytes);
3868 case PROP_MAX_SIZE_BUFFERS:
3869 g_value_set_uint (value, queue->max_level.buffers);
3871 case PROP_MAX_SIZE_TIME:
3872 g_value_set_uint64 (value, queue->max_level.time);
3874 case PROP_USE_BUFFERING:
3875 g_value_set_boolean (value, queue->use_buffering);
3877 case PROP_USE_TAGS_BITRATE:
3878 g_value_set_boolean (value, queue->use_tags_bitrate);
3880 case PROP_USE_RATE_ESTIMATE:
3881 g_value_set_boolean (value, queue->use_rate_estimate);
3883 case PROP_LOW_PERCENT:
3884 g_value_set_int (value, queue->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
3886 case PROP_HIGH_PERCENT:
3887 g_value_set_int (value, queue->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
3889 case PROP_LOW_WATERMARK:
3890 g_value_set_double (value, queue->low_watermark /
3891 (gdouble) MAX_BUFFERING_LEVEL);
3893 case PROP_HIGH_WATERMARK:
3894 g_value_set_double (value, queue->high_watermark /
3895 (gdouble) MAX_BUFFERING_LEVEL);
3897 case PROP_TEMP_TEMPLATE:
3898 g_value_set_string (value, queue->temp_template);
3900 case PROP_TEMP_LOCATION:
3901 g_value_set_string (value, queue->temp_location);
3903 case PROP_TEMP_REMOVE:
3904 g_value_set_boolean (value, queue->temp_remove);
3906 case PROP_RING_BUFFER_MAX_SIZE:
3907 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3909 case PROP_AVG_IN_RATE:
3911 gdouble in_rate = queue->byte_in_rate;
3913 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3914 * calculated, so calculate it here. */
3915 if (in_rate == 0.0 && queue->bytes_in
3916 && queue->last_update_in_rates_elapsed > 0.0)
3917 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3919 g_value_set_int64 (value, (gint64) in_rate);
3923 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3927 GST_QUEUE2_MUTEX_UNLOCK (queue);