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 switch (GST_EVENT_TYPE (event)) {
2467 case GST_EVENT_FLUSH_START:
2469 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2470 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2472 ret = gst_pad_push_event (queue->srcpad, event);
2474 /* now unblock the chain function */
2475 GST_QUEUE2_MUTEX_LOCK (queue);
2476 queue->srcresult = GST_FLOW_FLUSHING;
2477 queue->sinkresult = GST_FLOW_FLUSHING;
2478 /* unblock the loop and chain functions */
2479 GST_QUEUE2_SIGNAL_ADD (queue);
2480 GST_QUEUE2_SIGNAL_DEL (queue);
2481 GST_QUEUE2_MUTEX_UNLOCK (queue);
2483 /* make sure it pauses, this should happen since we sent
2484 * flush_start downstream. */
2485 gst_pad_pause_task (queue->srcpad);
2486 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2488 GST_QUEUE2_MUTEX_LOCK (queue);
2489 queue->last_query = FALSE;
2490 g_cond_signal (&queue->query_handled);
2491 GST_QUEUE2_MUTEX_UNLOCK (queue);
2493 GST_QUEUE2_MUTEX_LOCK (queue);
2494 /* flush the sink pad */
2495 queue->sinkresult = GST_FLOW_FLUSHING;
2496 GST_QUEUE2_SIGNAL_DEL (queue);
2497 queue->last_query = FALSE;
2498 g_cond_signal (&queue->query_handled);
2499 GST_QUEUE2_MUTEX_UNLOCK (queue);
2501 gst_event_unref (event);
2505 case GST_EVENT_FLUSH_STOP:
2507 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
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 */
2559 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2560 if (queue->srcresult != GST_FLOW_OK) {
2561 /* Errors in sticky event pushing are no problem and ignored here
2562 * as they will cause more meaningful errors during data flow.
2563 * For EOS events, that are not followed by data flow, we still
2564 * return FALSE here though and report an error.
2566 if (!GST_EVENT_IS_STICKY (event)) {
2567 goto out_flow_error;
2568 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2569 if (queue->srcresult == GST_FLOW_NOT_LINKED
2570 || queue->srcresult < GST_FLOW_EOS) {
2571 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
2573 goto out_flow_error;
2576 /* refuse more events on EOS */
2579 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2580 GST_QUEUE2_MUTEX_UNLOCK (queue);
2581 gst_queue2_post_buffering (queue);
2583 /* non-serialized events are passed downstream. */
2584 ret = gst_pad_push_event (queue->srcpad, event);
2589 return GST_FLOW_ERROR;
2595 GstFlowReturn ret = queue->sinkresult;
2596 GST_DEBUG_OBJECT (queue, "refusing event, we are %s",
2597 gst_flow_get_name (ret));
2598 GST_QUEUE2_MUTEX_UNLOCK (queue);
2599 gst_event_unref (event);
2604 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2605 GST_QUEUE2_MUTEX_UNLOCK (queue);
2606 gst_event_unref (event);
2607 return GST_FLOW_EOS;
2611 GST_LOG_OBJECT (queue,
2612 "refusing event, we have a downstream flow error: %s",
2613 gst_flow_get_name (queue->srcresult));
2614 GST_QUEUE2_MUTEX_UNLOCK (queue);
2615 gst_event_unref (event);
2616 return queue->srcresult;
2621 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2627 queue = GST_QUEUE2 (parent);
2629 switch (GST_QUERY_TYPE (query)) {
2631 if (GST_QUERY_IS_SERIALIZED (query)) {
2632 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2633 /* serialized events go in the queue. We need to be certain that we
2634 * don't cause deadlocks waiting for the query return value. We check if
2635 * the queue is empty (nothing is blocking downstream and the query can
2636 * be pushed for sure) or we are not buffering. If we are buffering,
2637 * the pipeline waits to unblock downstream until our queue fills up
2638 * completely, which can not happen if we block on the query..
2639 * Therefore we only potentially block when we are not buffering. */
2640 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2641 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2642 || !queue->use_buffering)) {
2643 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2644 gst_queue2_locked_enqueue (queue, query,
2645 GST_QUEUE2_ITEM_TYPE_QUERY);
2647 STATUS (queue, queue->sinkpad, "wait for QUERY");
2648 while (queue->sinkresult == GST_FLOW_OK &&
2649 queue->last_handled_query != query)
2650 g_cond_wait (&queue->query_handled, &queue->qlock);
2651 queue->last_handled_query = NULL;
2652 if (queue->sinkresult != GST_FLOW_OK)
2654 res = queue->last_query;
2656 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2660 GST_DEBUG_OBJECT (queue,
2661 "refusing query, we are not using the queue");
2664 GST_QUEUE2_MUTEX_UNLOCK (queue);
2665 gst_queue2_post_buffering (queue);
2667 res = gst_pad_query_default (pad, parent, query);
2676 GST_DEBUG_OBJECT (queue, "refusing query, we are %s",
2677 gst_flow_get_name (queue->sinkresult));
2678 GST_QUEUE2_MUTEX_UNLOCK (queue);
2684 gst_queue2_is_empty (GstQueue2 * queue)
2686 /* never empty on EOS */
2690 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2691 return queue->current->writing_pos <= queue->current->max_reading_pos;
2693 if (queue->queue.length == 0)
2701 gst_queue2_is_filled (GstQueue2 * queue)
2705 /* always filled on EOS */
2709 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2710 (queue->cur_level.format) >= ((alt_max) ? \
2711 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2713 /* if using a ring buffer we're filled if all ring buffer space is used
2714 * _by the current range_ */
2715 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2716 guint64 rb_size = queue->ring_buffer_max_size;
2717 GST_DEBUG_OBJECT (queue,
2718 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2719 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2720 return CHECK_FILLED (bytes, rb_size);
2723 /* if using file, we're never filled if we don't have EOS */
2724 if (QUEUE_IS_USING_TEMP_FILE (queue))
2727 /* we are never filled when we have no buffers at all */
2728 if (queue->cur_level.buffers == 0)
2731 /* we are filled if one of the current levels exceeds the max */
2732 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2733 || CHECK_FILLED (time, 0);
2735 /* if we need to, use the rate estimate to check against the max time we are
2736 * allowed to queue */
2737 if (queue->use_rate_estimate)
2738 res |= CHECK_FILLED (rate_time, 0);
2744 static GstFlowReturn
2745 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2746 GstMiniObject * item, GstQueue2ItemType item_type)
2748 /* we have to lock the queue since we span threads */
2749 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2750 /* when we received EOS, we refuse more data */
2753 /* when we received unexpected from downstream, refuse more buffers */
2754 if (queue->unexpected)
2755 goto out_unexpected;
2757 /* while we didn't receive the newsegment, we're seeking and we skip data */
2761 if (!gst_queue2_wait_free_space (queue))
2764 /* put buffer in queue now */
2765 gst_queue2_locked_enqueue (queue, item, item_type);
2766 GST_QUEUE2_MUTEX_UNLOCK (queue);
2767 gst_queue2_post_buffering (queue);
2771 /* special conditions */
2774 GstFlowReturn ret = queue->sinkresult;
2776 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2777 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2778 GST_QUEUE2_MUTEX_UNLOCK (queue);
2779 gst_mini_object_unref (item);
2785 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2786 GST_QUEUE2_MUTEX_UNLOCK (queue);
2787 gst_mini_object_unref (item);
2789 return GST_FLOW_EOS;
2793 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2794 GST_QUEUE2_MUTEX_UNLOCK (queue);
2795 gst_mini_object_unref (item);
2801 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2802 GST_QUEUE2_MUTEX_UNLOCK (queue);
2803 gst_mini_object_unref (item);
2805 return GST_FLOW_EOS;
2809 static GstFlowReturn
2810 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2814 queue = GST_QUEUE2 (parent);
2816 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2817 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2818 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2819 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2820 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2822 return gst_queue2_chain_buffer_or_buffer_list (queue,
2823 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2826 static GstFlowReturn
2827 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2828 GstBufferList * buffer_list)
2832 queue = GST_QUEUE2 (parent);
2834 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2835 "received buffer list %p", buffer_list);
2837 return gst_queue2_chain_buffer_or_buffer_list (queue,
2838 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2841 static GstMiniObject *
2842 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2844 GstMiniObject *data;
2846 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2848 /* stop pushing buffers, we dequeue all items until we see an item that we
2849 * can push again, which is EOS or SEGMENT. If there is nothing in the
2850 * queue we can push, we set a flag to make the sinkpad refuse more
2851 * buffers with an EOS return value until we receive something
2852 * pushable again or we get flushed. */
2853 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2854 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2855 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2856 "dropping EOS buffer %p", data);
2857 gst_buffer_unref (GST_BUFFER_CAST (data));
2858 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2859 GstEvent *event = GST_EVENT_CAST (data);
2860 GstEventType type = GST_EVENT_TYPE (event);
2862 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2863 /* we found a pushable item in the queue, push it out */
2864 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2865 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2868 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2869 "dropping EOS event %p", event);
2870 gst_event_unref (event);
2871 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2872 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2873 "dropping EOS buffer list %p", data);
2874 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2875 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2876 queue->last_query = FALSE;
2877 g_cond_signal (&queue->query_handled);
2878 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2881 /* no more items in the queue. Set the unexpected flag so that upstream
2882 * make us refuse any more buffers on the sinkpad. Since we will still
2883 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2884 * task function does not shut down. */
2885 queue->unexpected = TRUE;
2889 /* dequeue an item from the queue an push it downstream. This functions returns
2890 * the result of the push. */
2891 static GstFlowReturn
2892 gst_queue2_push_one (GstQueue2 * queue)
2894 GstFlowReturn result = queue->srcresult;
2895 GstMiniObject *data;
2896 GstQueue2ItemType item_type;
2898 data = gst_queue2_locked_dequeue (queue, &item_type);
2903 STATUS (queue, queue->srcpad, "We have something dequeud");
2904 g_atomic_int_set (&queue->downstream_may_block,
2905 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2906 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2907 GST_QUEUE2_MUTEX_UNLOCK (queue);
2908 gst_queue2_post_buffering (queue);
2910 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2913 buffer = GST_BUFFER_CAST (data);
2915 result = gst_pad_push (queue->srcpad, buffer);
2916 g_atomic_int_set (&queue->downstream_may_block, 0);
2918 /* need to check for srcresult here as well */
2919 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2920 if (result == GST_FLOW_EOS) {
2921 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2924 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2925 * to the caller so that the task function does not shut down */
2926 result = GST_FLOW_OK;
2928 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2929 GstEvent *event = GST_EVENT_CAST (data);
2930 GstEventType type = GST_EVENT_TYPE (event);
2932 if (type == GST_EVENT_TAG) {
2933 if (queue->use_tags_bitrate) {
2937 gst_event_parse_tag (event, &tags);
2938 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2939 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2940 GST_QUEUE2_MUTEX_LOCK (queue);
2941 queue->src_tags_bitrate = bitrate;
2942 GST_QUEUE2_MUTEX_UNLOCK (queue);
2943 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
2948 gst_pad_push_event (queue->srcpad, event);
2950 /* if we're EOS, return EOS so that the task pauses. */
2951 if (type == GST_EVENT_EOS) {
2952 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2953 "pushed EOS event %p, return EOS", event);
2954 result = GST_FLOW_EOS;
2957 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2958 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2959 GstBufferList *buffer_list;
2961 buffer_list = GST_BUFFER_LIST_CAST (data);
2963 result = gst_pad_push_list (queue->srcpad, buffer_list);
2964 g_atomic_int_set (&queue->downstream_may_block, 0);
2966 /* need to check for srcresult here as well */
2967 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2968 if (result == GST_FLOW_EOS) {
2969 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2972 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2973 * to the caller so that the task function does not shut down */
2974 result = GST_FLOW_OK;
2976 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2977 GstQuery *query = GST_QUERY_CAST (data);
2979 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2980 queue->last_handled_query = query;
2981 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2982 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2983 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2984 "did query %p, return %d", query, queue->last_query);
2985 g_cond_signal (&queue->query_handled);
2986 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2987 result = GST_FLOW_OK;
2994 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2995 "exit because we have no item in the queue");
2996 return GST_FLOW_ERROR;
3000 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are %s",
3001 gst_flow_get_name (queue->srcresult));
3002 return queue->srcresult;
3006 /* called repeatedly with @pad as the source pad. This function should push out
3007 * data to the peer element. */
3009 gst_queue2_loop (GstPad * pad)
3014 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
3016 /* have to lock for thread-safety */
3017 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3019 if (gst_queue2_is_empty (queue)) {
3022 /* pause the timer while we wait. The fact that we are waiting does not mean
3023 * the byterate on the output pad is lower */
3024 if ((started = queue->out_timer_started))
3025 g_timer_stop (queue->out_timer);
3027 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
3028 "queue is empty, waiting for new data");
3030 /* Wait for data to be available, we could be unlocked because of a flush. */
3031 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
3033 while (gst_queue2_is_empty (queue));
3035 /* and continue if we were running before */
3037 g_timer_continue (queue->out_timer);
3039 ret = gst_queue2_push_one (queue);
3040 queue->srcresult = ret;
3041 queue->sinkresult = ret;
3042 if (ret != GST_FLOW_OK)
3045 GST_QUEUE2_MUTEX_UNLOCK (queue);
3046 gst_queue2_post_buffering (queue);
3053 gboolean eos = queue->is_eos;
3054 GstFlowReturn ret = queue->srcresult;
3056 gst_pad_pause_task (queue->srcpad);
3057 if (ret == GST_FLOW_FLUSHING) {
3058 gst_queue2_locked_flush (queue, FALSE, FALSE);
3060 GST_QUEUE2_SIGNAL_DEL (queue);
3061 queue->last_query = FALSE;
3062 g_cond_signal (&queue->query_handled);
3064 GST_QUEUE2_MUTEX_UNLOCK (queue);
3065 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3066 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
3067 /* Recalculate buffering levels before stopping since the source flow
3068 * might cause a different buffering level (like NOT_LINKED making
3069 * the queue appear as full) */
3070 if (queue->use_buffering)
3071 update_buffering (queue);
3072 gst_queue2_post_buffering (queue);
3073 /* let app know about us giving up if upstream is not expected to do so */
3074 /* EOS is already taken care of elsewhere */
3075 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
3076 GST_ELEMENT_FLOW_ERROR (queue, ret);
3077 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
3084 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
3086 gboolean res = TRUE;
3087 GstQueue2 *queue = GST_QUEUE2 (parent);
3089 #ifndef GST_DISABLE_GST_DEBUG
3090 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
3091 event, GST_EVENT_TYPE_NAME (event));
3094 switch (GST_EVENT_TYPE (event)) {
3095 case GST_EVENT_FLUSH_START:
3096 if (QUEUE_IS_USING_QUEUE (queue)) {
3097 /* just forward upstream */
3098 res = gst_pad_push_event (queue->sinkpad, event);
3100 /* now unblock the getrange function */
3101 GST_QUEUE2_MUTEX_LOCK (queue);
3102 GST_DEBUG_OBJECT (queue, "flushing");
3103 queue->srcresult = GST_FLOW_FLUSHING;
3104 GST_QUEUE2_SIGNAL_ADD (queue);
3105 GST_QUEUE2_MUTEX_UNLOCK (queue);
3107 /* when using a temp file, we eat the event */
3109 gst_event_unref (event);
3112 case GST_EVENT_FLUSH_STOP:
3113 if (QUEUE_IS_USING_QUEUE (queue)) {
3114 /* just forward upstream */
3115 res = gst_pad_push_event (queue->sinkpad, event);
3117 /* now unblock the getrange function */
3118 GST_QUEUE2_MUTEX_LOCK (queue);
3119 queue->srcresult = GST_FLOW_OK;
3120 GST_QUEUE2_MUTEX_UNLOCK (queue);
3122 /* when using a temp file, we eat the event */
3124 gst_event_unref (event);
3127 case GST_EVENT_RECONFIGURE:
3128 GST_QUEUE2_MUTEX_LOCK (queue);
3129 /* assume downstream is linked now and try to push again */
3130 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3131 queue->srcresult = GST_FLOW_OK;
3132 queue->sinkresult = GST_FLOW_OK;
3133 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3134 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3138 GST_QUEUE2_MUTEX_UNLOCK (queue);
3140 res = gst_pad_push_event (queue->sinkpad, event);
3143 res = gst_pad_push_event (queue->sinkpad, event);
3151 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3155 queue = GST_QUEUE2 (parent);
3157 switch (GST_QUERY_TYPE (query)) {
3158 case GST_QUERY_POSITION:
3163 if (!gst_pad_peer_query (queue->sinkpad, query))
3166 /* get peer position */
3167 gst_query_parse_position (query, &format, &peer_pos);
3169 /* FIXME: this code assumes that there's no discont in the queue */
3171 case GST_FORMAT_BYTES:
3172 peer_pos -= queue->cur_level.bytes;
3173 if (peer_pos < 0) /* Clamp result to 0 */
3176 case GST_FORMAT_TIME:
3177 peer_pos -= queue->cur_level.time;
3178 if (peer_pos < 0) /* Clamp result to 0 */
3182 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3183 "know how to adjust value", gst_format_get_name (format));
3186 /* set updated position */
3187 gst_query_set_position (query, format, peer_pos);
3190 case GST_QUERY_DURATION:
3192 GST_DEBUG_OBJECT (queue, "doing peer query");
3194 if (!gst_pad_peer_query (queue->sinkpad, query))
3197 GST_DEBUG_OBJECT (queue, "peer query success");
3200 case GST_QUERY_BUFFERING:
3203 gboolean is_buffering;
3204 GstBufferingMode mode;
3205 gint avg_in, avg_out;
3206 gint64 buffering_left;
3208 GST_DEBUG_OBJECT (queue, "query buffering");
3210 get_buffering_level (queue, &is_buffering, &percent);
3211 percent = convert_to_buffering_percent (queue, percent);
3212 gst_query_set_buffering_percent (query, is_buffering, percent);
3214 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3216 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3219 if (!QUEUE_IS_USING_QUEUE (queue)) {
3220 /* add ranges for download and ringbuffer buffering */
3222 gint64 start, stop, range_start, range_stop;
3223 guint64 writing_pos;
3224 gint64 estimated_total;
3226 gboolean peer_res, is_eos;
3227 GstQueue2Range *queued_ranges;
3229 /* we need a current download region */
3230 if (queue->current == NULL)
3233 writing_pos = queue->current->writing_pos;
3234 is_eos = queue->is_eos;
3237 /* we're EOS, we know the duration in bytes now */
3239 duration = writing_pos;
3241 /* get duration of upstream in bytes */
3242 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3243 GST_FORMAT_BYTES, &duration);
3246 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3247 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3249 /* calculate remaining and total download time */
3250 if (peer_res && avg_in > 0.0)
3251 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3253 estimated_total = -1;
3255 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3258 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3261 case GST_FORMAT_PERCENT:
3262 /* we need duration */
3267 /* get our available data relative to the duration */
3270 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3275 case GST_FORMAT_BYTES:
3285 /* fill out the buffered ranges */
3286 for (queued_ranges = queue->ranges; queued_ranges;
3287 queued_ranges = queued_ranges->next) {
3289 case GST_FORMAT_PERCENT:
3290 if (duration == -1) {
3296 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3297 queued_ranges->offset, duration);
3299 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3300 queued_ranges->writing_pos, duration);
3302 case GST_FORMAT_BYTES:
3303 range_start = queued_ranges->offset;
3304 range_stop = queued_ranges->writing_pos;
3311 if (range_start == range_stop)
3313 GST_DEBUG_OBJECT (queue,
3314 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3315 G_GINT64_FORMAT, range_start, range_stop);
3316 gst_query_add_buffering_range (query, range_start, range_stop);
3319 gst_query_set_buffering_range (query, format, start, stop,
3324 case GST_QUERY_SCHEDULING:
3327 GstSchedulingFlags flags = 0;
3329 if (!gst_pad_peer_query (queue->sinkpad, query))
3332 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3334 /* we can operate in pull mode when we are using a tempfile */
3335 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3338 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3339 gst_query_set_scheduling (query, flags, 0, -1, 0);
3341 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3342 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3346 /* peer handled other queries */
3347 if (!gst_pad_query_default (pad, parent, query))
3357 GST_DEBUG_OBJECT (queue, "failed peer query");
3363 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3365 GstQueue2 *queue = GST_QUEUE2 (element);
3367 /* simply forward to the srcpad query function */
3368 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3373 gst_queue2_update_upstream_size (GstQueue2 * queue)
3375 gint64 upstream_size = -1;
3377 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3379 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3381 /* upstream_size can be negative but queue->upstream_size is unsigned.
3382 * Prevent setting negative values to it (the query can return -1) */
3383 if (upstream_size >= 0)
3384 queue->upstream_size = upstream_size;
3386 queue->upstream_size = 0;
3390 static GstFlowReturn
3391 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3392 guint length, GstBuffer ** buffer)
3397 queue = GST_QUEUE2_CAST (parent);
3399 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3400 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3401 offset = (offset == -1) ? queue->current->reading_pos : offset;
3403 GST_DEBUG_OBJECT (queue,
3404 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3406 /* catch any reads beyond the size of the file here to make sure queue2
3407 * doesn't send seek events beyond the size of the file upstream, since
3408 * that would confuse elements such as souphttpsrc and/or http servers.
3409 * Demuxers often just loop until EOS at the end of the file to figure out
3410 * when they've read all the end-headers or index chunks. */
3411 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3412 gst_queue2_update_upstream_size (queue);
3413 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3414 goto out_unexpected;
3417 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3418 gst_queue2_update_upstream_size (queue);
3419 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3420 length = queue->upstream_size - offset;
3421 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3425 /* FIXME - function will block when the range is not yet available */
3426 ret = gst_queue2_create_read (queue, offset, length, buffer);
3427 GST_QUEUE2_MUTEX_UNLOCK (queue);
3428 gst_queue2_post_buffering (queue);
3435 ret = queue->srcresult;
3437 GST_DEBUG_OBJECT (queue, "we are %s", gst_flow_get_name (ret));
3438 GST_QUEUE2_MUTEX_UNLOCK (queue);
3443 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3444 GST_QUEUE2_MUTEX_UNLOCK (queue);
3445 return GST_FLOW_EOS;
3449 /* sink currently only operates in push mode */
3451 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3452 GstPadMode mode, gboolean active)
3457 queue = GST_QUEUE2 (parent);
3460 case GST_PAD_MODE_PUSH:
3462 GST_QUEUE2_MUTEX_LOCK (queue);
3463 GST_DEBUG_OBJECT (queue, "activating push mode");
3464 queue->srcresult = GST_FLOW_OK;
3465 queue->sinkresult = GST_FLOW_OK;
3466 queue->is_eos = FALSE;
3467 queue->unexpected = FALSE;
3468 reset_rate_timer (queue);
3469 GST_QUEUE2_MUTEX_UNLOCK (queue);
3471 /* unblock chain function */
3472 GST_QUEUE2_MUTEX_LOCK (queue);
3473 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3474 queue->srcresult = GST_FLOW_FLUSHING;
3475 queue->sinkresult = GST_FLOW_FLUSHING;
3476 GST_QUEUE2_SIGNAL_DEL (queue);
3477 GST_QUEUE2_MUTEX_UNLOCK (queue);
3479 /* wait until it is unblocked and clean up */
3480 GST_PAD_STREAM_LOCK (pad);
3481 GST_QUEUE2_MUTEX_LOCK (queue);
3482 gst_queue2_locked_flush (queue, TRUE, FALSE);
3483 GST_QUEUE2_MUTEX_UNLOCK (queue);
3484 GST_PAD_STREAM_UNLOCK (pad);
3495 /* src operating in push mode, we start a task on the source pad that pushes out
3496 * buffers from the queue */
3498 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3500 gboolean result = FALSE;
3503 queue = GST_QUEUE2 (parent);
3506 GST_QUEUE2_MUTEX_LOCK (queue);
3507 GST_DEBUG_OBJECT (queue, "activating push mode");
3508 queue->srcresult = GST_FLOW_OK;
3509 queue->sinkresult = GST_FLOW_OK;
3510 queue->is_eos = FALSE;
3511 queue->unexpected = FALSE;
3513 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3514 GST_QUEUE2_MUTEX_UNLOCK (queue);
3516 /* unblock loop function */
3517 GST_QUEUE2_MUTEX_LOCK (queue);
3518 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3519 queue->srcresult = GST_FLOW_FLUSHING;
3520 queue->sinkresult = GST_FLOW_FLUSHING;
3521 /* the item add signal will unblock */
3522 GST_QUEUE2_SIGNAL_ADD (queue);
3523 GST_QUEUE2_MUTEX_UNLOCK (queue);
3525 /* step 2, make sure streaming finishes */
3526 result = gst_pad_stop_task (pad);
3528 GST_QUEUE2_MUTEX_LOCK (queue);
3529 gst_queue2_locked_flush (queue, FALSE, FALSE);
3530 GST_QUEUE2_MUTEX_UNLOCK (queue);
3536 /* pull mode, downstream will call our getrange function */
3538 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3543 queue = GST_QUEUE2 (parent);
3546 GST_QUEUE2_MUTEX_LOCK (queue);
3547 if (!QUEUE_IS_USING_QUEUE (queue)) {
3548 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3549 /* open the temp file now */
3550 result = gst_queue2_open_temp_location_file (queue);
3551 } else if (!queue->ring_buffer) {
3552 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3553 result = ! !queue->ring_buffer;
3558 GST_DEBUG_OBJECT (queue, "activating pull mode");
3559 init_ranges (queue);
3560 queue->srcresult = GST_FLOW_OK;
3561 queue->sinkresult = GST_FLOW_OK;
3562 queue->is_eos = FALSE;
3563 queue->unexpected = FALSE;
3564 queue->upstream_size = 0;
3566 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3567 /* this is not allowed, we cannot operate in pull mode without a temp
3569 queue->srcresult = GST_FLOW_FLUSHING;
3570 queue->sinkresult = GST_FLOW_FLUSHING;
3573 GST_QUEUE2_MUTEX_UNLOCK (queue);
3575 GST_QUEUE2_MUTEX_LOCK (queue);
3576 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3577 queue->srcresult = GST_FLOW_FLUSHING;
3578 queue->sinkresult = GST_FLOW_FLUSHING;
3579 /* this will unlock getrange */
3580 GST_QUEUE2_SIGNAL_ADD (queue);
3582 GST_QUEUE2_MUTEX_UNLOCK (queue);
3589 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3595 case GST_PAD_MODE_PULL:
3596 res = gst_queue2_src_activate_pull (pad, parent, active);
3598 case GST_PAD_MODE_PUSH:
3599 res = gst_queue2_src_activate_push (pad, parent, active);
3602 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3609 static GstStateChangeReturn
3610 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3613 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3615 queue = GST_QUEUE2 (element);
3617 switch (transition) {
3618 case GST_STATE_CHANGE_NULL_TO_READY:
3620 case GST_STATE_CHANGE_READY_TO_PAUSED:
3621 GST_QUEUE2_MUTEX_LOCK (queue);
3622 if (!QUEUE_IS_USING_QUEUE (queue)) {
3623 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3624 if (!gst_queue2_open_temp_location_file (queue))
3625 ret = GST_STATE_CHANGE_FAILURE;
3627 if (queue->ring_buffer) {
3628 g_free (queue->ring_buffer);
3629 queue->ring_buffer = NULL;
3631 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3632 ret = GST_STATE_CHANGE_FAILURE;
3634 init_ranges (queue);
3636 queue->segment_event_received = FALSE;
3637 queue->starting_segment = NULL;
3638 gst_event_replace (&queue->stream_start_event, NULL);
3639 GST_QUEUE2_MUTEX_UNLOCK (queue);
3641 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3647 if (ret == GST_STATE_CHANGE_FAILURE)
3650 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3652 if (ret == GST_STATE_CHANGE_FAILURE)
3655 switch (transition) {
3656 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3658 case GST_STATE_CHANGE_PAUSED_TO_READY:
3659 GST_QUEUE2_MUTEX_LOCK (queue);
3660 if (!QUEUE_IS_USING_QUEUE (queue)) {
3661 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3662 gst_queue2_close_temp_location_file (queue);
3663 } else if (queue->ring_buffer) {
3664 g_free (queue->ring_buffer);
3665 queue->ring_buffer = NULL;
3667 clean_ranges (queue);
3669 if (queue->starting_segment != NULL) {
3670 gst_event_unref (queue->starting_segment);
3671 queue->starting_segment = NULL;
3673 gst_event_replace (&queue->stream_start_event, NULL);
3674 GST_QUEUE2_MUTEX_UNLOCK (queue);
3676 case GST_STATE_CHANGE_READY_TO_NULL:
3685 /* changing the capacity of the queue must wake up
3686 * the _chain function, it might have more room now
3687 * to store the buffer/event in the queue */
3688 #define QUEUE_CAPACITY_CHANGE(q) \
3689 GST_QUEUE2_SIGNAL_DEL (queue); \
3690 if (queue->use_buffering) \
3691 update_buffering (queue);
3693 /* Changing the minimum required fill level must
3694 * wake up the _loop function as it might now
3695 * be able to preceed.
3697 #define QUEUE_THRESHOLD_CHANGE(q)\
3698 GST_QUEUE2_SIGNAL_ADD (queue);
3701 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3705 /* the element must be stopped in order to do this */
3706 GST_OBJECT_LOCK (queue);
3707 state = GST_STATE (queue);
3708 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3710 GST_OBJECT_UNLOCK (queue);
3712 /* set new location */
3713 g_free (queue->temp_template);
3714 queue->temp_template = g_strdup (template);
3721 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3722 GST_OBJECT_UNLOCK (queue);
3727 gst_queue2_set_property (GObject * object,
3728 guint prop_id, const GValue * value, GParamSpec * pspec)
3730 GstQueue2 *queue = GST_QUEUE2 (object);
3732 /* someone could change levels here, and since this
3733 * affects the get/put funcs, we need to lock for safety. */
3734 GST_QUEUE2_MUTEX_LOCK (queue);
3737 case PROP_MAX_SIZE_BYTES:
3738 queue->max_level.bytes = g_value_get_uint (value);
3739 QUEUE_CAPACITY_CHANGE (queue);
3741 case PROP_MAX_SIZE_BUFFERS:
3742 queue->max_level.buffers = g_value_get_uint (value);
3743 QUEUE_CAPACITY_CHANGE (queue);
3745 case PROP_MAX_SIZE_TIME:
3746 queue->max_level.time = g_value_get_uint64 (value);
3747 /* set rate_time to the same value. We use an extra field in the level
3748 * structure so that we can easily access and compare it */
3749 queue->max_level.rate_time = queue->max_level.time;
3750 QUEUE_CAPACITY_CHANGE (queue);
3752 case PROP_USE_BUFFERING:
3753 queue->use_buffering = g_value_get_boolean (value);
3754 if (!queue->use_buffering && queue->is_buffering) {
3755 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3756 "posting 100%% message");
3757 SET_PERCENT (queue, 100);
3758 queue->is_buffering = FALSE;
3761 if (queue->use_buffering) {
3762 queue->is_buffering = TRUE;
3763 update_buffering (queue);
3766 case PROP_USE_TAGS_BITRATE:
3767 queue->use_tags_bitrate = g_value_get_boolean (value);
3769 case PROP_USE_RATE_ESTIMATE:
3770 queue->use_rate_estimate = g_value_get_boolean (value);
3772 case PROP_LOW_PERCENT:
3773 queue->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3774 if (queue->is_buffering)
3775 update_buffering (queue);
3777 case PROP_HIGH_PERCENT:
3778 queue->high_watermark =
3779 g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3780 if (queue->is_buffering)
3781 update_buffering (queue);
3783 case PROP_LOW_WATERMARK:
3784 queue->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3785 if (queue->is_buffering)
3786 update_buffering (queue);
3788 case PROP_HIGH_WATERMARK:
3789 queue->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3790 if (queue->is_buffering)
3791 update_buffering (queue);
3793 case PROP_TEMP_TEMPLATE:
3794 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3796 case PROP_TEMP_REMOVE:
3797 queue->temp_remove = g_value_get_boolean (value);
3799 case PROP_RING_BUFFER_MAX_SIZE:
3800 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3803 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3807 GST_QUEUE2_MUTEX_UNLOCK (queue);
3808 gst_queue2_post_buffering (queue);
3812 gst_queue2_get_property (GObject * object,
3813 guint prop_id, GValue * value, GParamSpec * pspec)
3815 GstQueue2 *queue = GST_QUEUE2 (object);
3817 GST_QUEUE2_MUTEX_LOCK (queue);
3820 case PROP_CUR_LEVEL_BYTES:
3821 g_value_set_uint (value, queue->cur_level.bytes);
3823 case PROP_CUR_LEVEL_BUFFERS:
3824 g_value_set_uint (value, queue->cur_level.buffers);
3826 case PROP_CUR_LEVEL_TIME:
3827 g_value_set_uint64 (value, queue->cur_level.time);
3829 case PROP_MAX_SIZE_BYTES:
3830 g_value_set_uint (value, queue->max_level.bytes);
3832 case PROP_MAX_SIZE_BUFFERS:
3833 g_value_set_uint (value, queue->max_level.buffers);
3835 case PROP_MAX_SIZE_TIME:
3836 g_value_set_uint64 (value, queue->max_level.time);
3838 case PROP_USE_BUFFERING:
3839 g_value_set_boolean (value, queue->use_buffering);
3841 case PROP_USE_TAGS_BITRATE:
3842 g_value_set_boolean (value, queue->use_tags_bitrate);
3844 case PROP_USE_RATE_ESTIMATE:
3845 g_value_set_boolean (value, queue->use_rate_estimate);
3847 case PROP_LOW_PERCENT:
3848 g_value_set_int (value, queue->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
3850 case PROP_HIGH_PERCENT:
3851 g_value_set_int (value, queue->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
3853 case PROP_LOW_WATERMARK:
3854 g_value_set_double (value, queue->low_watermark /
3855 (gdouble) MAX_BUFFERING_LEVEL);
3857 case PROP_HIGH_WATERMARK:
3858 g_value_set_double (value, queue->high_watermark /
3859 (gdouble) MAX_BUFFERING_LEVEL);
3861 case PROP_TEMP_TEMPLATE:
3862 g_value_set_string (value, queue->temp_template);
3864 case PROP_TEMP_LOCATION:
3865 g_value_set_string (value, queue->temp_location);
3867 case PROP_TEMP_REMOVE:
3868 g_value_set_boolean (value, queue->temp_remove);
3870 case PROP_RING_BUFFER_MAX_SIZE:
3871 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3873 case PROP_AVG_IN_RATE:
3875 gdouble in_rate = queue->byte_in_rate;
3877 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3878 * calculated, so calculate it here. */
3879 if (in_rate == 0.0 && queue->bytes_in
3880 && queue->last_update_in_rates_elapsed > 0.0)
3881 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3883 g_value_set_int64 (value, (gint64) in_rate);
3887 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3891 GST_QUEUE2_MUTEX_UNLOCK (queue);