2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
29 * Data is queued until one of the limits specified by the
30 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
31 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
38 * You can query how many buffers are queued by reading the
39 * #GstQueue2:current-level-buffers property.
41 * The default queue size limits are 100 buffers, 2MB of data, or
42 * two seconds worth of data, whichever is reached first.
44 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
45 * will allocate a random free filename and buffer data in the file.
46 * By using this, it will buffer the entire stream data on the file independently
47 * of the queue size limits, they will only be used for buffering statistics.
49 * The temp-location property will be used to notify the application of the
57 #include "gstqueue2.h"
59 #include <glib/gstdio.h>
61 #include "gst/gst-i18n-lib.h"
62 #include "gst/glib-compat-private.h"
67 #include <io.h> /* lseek, open, close, read */
69 #define lseek _lseeki64
76 #ifdef __BIONIC__ /* Android */
84 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
94 GST_DEBUG_CATEGORY_STATIC (queue_debug);
95 #define GST_CAT_DEFAULT (queue_debug)
96 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
104 #define DEFAULT_BUFFER_SIZE 4096
105 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
106 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
107 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
109 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
111 /* default property values */
112 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
113 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
114 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
115 #define DEFAULT_USE_BUFFERING FALSE
116 #define DEFAULT_USE_TAGS_BITRATE FALSE
117 #define DEFAULT_USE_RATE_ESTIMATE TRUE
118 #define DEFAULT_LOW_PERCENT 10
119 #define DEFAULT_HIGH_PERCENT 99
120 #define DEFAULT_LOW_WATERMARK 0.01
121 #define DEFAULT_HIGH_WATERMARK 0.99
122 #define DEFAULT_TEMP_REMOVE TRUE
123 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
128 PROP_CUR_LEVEL_BUFFERS,
129 PROP_CUR_LEVEL_BYTES,
131 PROP_MAX_SIZE_BUFFERS,
135 PROP_USE_TAGS_BITRATE,
136 PROP_USE_RATE_ESTIMATE,
144 PROP_RING_BUFFER_MAX_SIZE,
149 /* Explanation for buffer levels and percentages:
151 * The buffering_level functions here return a value in a normalized range
152 * that specifies the queue's current fill level. The range goes from 0 to
153 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
155 * This is not to be confused with the buffering_percent value, which is
156 * a *relative* quantity - relative to the low/high watermarks.
157 * buffering_percent = 0% means buffering_level is at the low watermark.
158 * buffering_percent = 100% means buffering_level is at the high watermark.
159 * buffering_percent is used for determining if the fill level has reached
160 * the high watermark, and for producing BUFFERING messages. This value
161 * always uses a 0..100 range (since it is a percentage).
163 * To avoid future confusions, whenever "buffering level" is mentioned, it
164 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
165 * range. Whenever "buffering_percent" is mentioned, it refers to the
166 * percentage value that is relative to the low/high watermark. */
168 /* Using a buffering level range of 0..1000000 to allow for a
169 * resolution in ppm (1 ppm = 0.0001%) */
170 #define MAX_BUFFERING_LEVEL 1000000
172 /* How much 1% makes up in the buffer level range */
173 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
175 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
182 #define STATUS(queue, pad, msg) \
183 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
184 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
185 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
186 " ns, %"G_GUINT64_FORMAT" items", \
187 GST_DEBUG_PAD_NAME (pad), \
188 queue->cur_level.buffers, \
189 queue->max_level.buffers, \
190 queue->cur_level.bytes, \
191 queue->max_level.bytes, \
192 queue->cur_level.time, \
193 queue->max_level.time, \
194 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
195 queue->current->writing_pos - queue->current->max_reading_pos : \
196 queue->queue.length))
198 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
199 g_mutex_lock (&q->qlock); \
202 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
203 GST_QUEUE2_MUTEX_LOCK (q); \
204 if (res != GST_FLOW_OK) \
208 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
209 g_mutex_unlock (&q->qlock); \
212 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
213 STATUS (queue, q->sinkpad, "wait for DEL"); \
214 q->waiting_del = TRUE; \
215 g_cond_wait (&q->item_del, &queue->qlock); \
216 q->waiting_del = FALSE; \
217 if (res != GST_FLOW_OK) { \
218 STATUS (queue, q->srcpad, "received DEL wakeup"); \
221 STATUS (queue, q->sinkpad, "received DEL"); \
224 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
225 STATUS (queue, q->srcpad, "wait for ADD"); \
226 q->waiting_add = TRUE; \
227 g_cond_wait (&q->item_add, &q->qlock); \
228 q->waiting_add = FALSE; \
229 if (res != GST_FLOW_OK) { \
230 STATUS (queue, q->srcpad, "received ADD wakeup"); \
233 STATUS (queue, q->srcpad, "received ADD"); \
236 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
237 if (q->waiting_del) { \
238 STATUS (q, q->srcpad, "signal DEL"); \
239 g_cond_signal (&q->item_del); \
243 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
244 if (q->waiting_add) { \
245 STATUS (q, q->sinkpad, "signal ADD"); \
246 g_cond_signal (&q->item_add); \
250 #define SET_PERCENT(q, perc) G_STMT_START { \
251 if (perc != q->buffering_percent) { \
252 q->buffering_percent = perc; \
253 q->percent_changed = TRUE; \
254 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
255 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
256 &q->buffering_left); \
261 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
262 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
263 "dataflow inside the queue element");
264 #define gst_queue2_parent_class parent_class
265 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
267 static void gst_queue2_finalize (GObject * object);
269 static void gst_queue2_set_property (GObject * object,
270 guint prop_id, const GValue * value, GParamSpec * pspec);
271 static void gst_queue2_get_property (GObject * object,
272 guint prop_id, GValue * value, GParamSpec * pspec);
274 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
276 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
277 GstBufferList * buffer_list);
278 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
279 static void gst_queue2_loop (GstPad * pad);
281 static GstFlowReturn gst_queue2_handle_sink_event (GstPad * pad,
282 GstObject * parent, GstEvent * event);
283 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
286 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
288 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
290 static gboolean gst_queue2_handle_query (GstElement * element,
293 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
294 guint64 offset, guint length, GstBuffer ** buffer);
296 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
297 GstPadMode mode, gboolean active);
298 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
299 GstPadMode mode, gboolean active);
300 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
301 GstStateChange transition);
303 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
304 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
306 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
307 static void update_in_rates (GstQueue2 * queue, gboolean force);
308 static GstMessage *gst_queue2_get_buffering_message (GstQueue2 * queue);
309 static void gst_queue2_post_buffering (GstQueue2 * queue);
313 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
314 GST_QUEUE2_ITEM_TYPE_BUFFER,
315 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
316 GST_QUEUE2_ITEM_TYPE_EVENT,
317 GST_QUEUE2_ITEM_TYPE_QUERY
322 GstQueue2ItemType type;
326 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
329 gst_queue2_class_init (GstQueue2Class * klass)
331 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
332 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
334 gobject_class->set_property = gst_queue2_set_property;
335 gobject_class->get_property = gst_queue2_get_property;
338 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
339 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
340 "Current amount of data in the queue (bytes)",
341 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
342 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
343 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
344 "Current number of buffers in the queue",
345 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
347 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
348 "Current amount of data in the queue (in ns)",
349 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
351 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
352 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
353 "Max. amount of data in the queue (bytes, 0=disable)",
354 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
355 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
356 G_PARAM_STATIC_STRINGS));
357 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
358 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
359 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
360 DEFAULT_MAX_SIZE_BUFFERS,
361 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
362 G_PARAM_STATIC_STRINGS));
363 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
364 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
365 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
366 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
367 G_PARAM_STATIC_STRINGS));
369 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
370 g_param_spec_boolean ("use-buffering", "Use buffering",
371 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
372 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
373 G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (gobject_class, PROP_USE_TAGS_BITRATE,
375 g_param_spec_boolean ("use-tags-bitrate", "Use bitrate from tags",
376 "Use a bitrate from upstream tags to estimate buffer duration if not provided",
377 DEFAULT_USE_TAGS_BITRATE,
378 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
379 G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
381 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
382 "Estimate the bitrate of the stream to calculate time level",
383 DEFAULT_USE_RATE_ESTIMATE,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
386 g_param_spec_int ("low-percent", "Low percent",
387 "Low threshold for buffering to start. Only used if use-buffering is True "
388 "(Deprecated: use low-watermark instead)",
389 0, 100, DEFAULT_LOW_WATERMARK * 100,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
392 g_param_spec_int ("high-percent", "High percent",
393 "High threshold for buffering to finish. Only used if use-buffering is True "
394 "(Deprecated: use high-watermark instead)",
395 0, 100, DEFAULT_HIGH_WATERMARK * 100,
396 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
398 g_param_spec_double ("low-watermark", "Low watermark",
399 "Low threshold for buffering to start. Only used if use-buffering is True",
400 0.0, 1.0, DEFAULT_LOW_WATERMARK,
401 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
403 g_param_spec_double ("high-watermark", "High watermark",
404 "High threshold for buffering to finish. Only used if use-buffering is True",
405 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
406 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
409 g_param_spec_string ("temp-template", "Temporary File Template",
410 "File template to store temporary files in, should contain directory "
411 "and XXXXXX. (NULL == disabled)",
412 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
415 g_param_spec_string ("temp-location", "Temporary File Location",
416 "Location to store temporary files in (Only read this property, "
417 "use temp-template to configure the name template)",
418 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
421 * GstQueue2:temp-remove
423 * When temp-template is set, remove the temporary file when going to READY.
425 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
426 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
427 "Remove the temp-location after use",
428 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 * GstQueue2:ring-buffer-max-size
433 * The maximum size of the ring buffer in bytes. If set to 0, the ring
434 * buffer is disabled. Default 0.
436 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
437 g_param_spec_uint64 ("ring-buffer-max-size",
438 "Max. ring buffer size (bytes)",
439 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
440 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 * GstQueue2:avg-in-rate
446 * The average input data rate.
448 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
449 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
450 "Average input data rate (bytes/s)",
451 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
453 /* set several parent class virtual functions */
454 gobject_class->finalize = gst_queue2_finalize;
456 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
457 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
459 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
462 "Erik Walthinsen <omega@cse.ogi.edu>, "
463 "Wim Taymans <wim.taymans@gmail.com>");
465 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
466 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
470 gst_queue2_init (GstQueue2 * queue)
472 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
474 gst_pad_set_chain_function (queue->sinkpad,
475 GST_DEBUG_FUNCPTR (gst_queue2_chain));
476 gst_pad_set_chain_list_function (queue->sinkpad,
477 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
478 gst_pad_set_activatemode_function (queue->sinkpad,
479 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
480 gst_pad_set_event_full_function (queue->sinkpad,
481 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
482 gst_pad_set_query_function (queue->sinkpad,
483 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
484 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
485 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
487 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
489 gst_pad_set_activatemode_function (queue->srcpad,
490 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
491 gst_pad_set_getrange_function (queue->srcpad,
492 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
493 gst_pad_set_event_function (queue->srcpad,
494 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
495 gst_pad_set_query_function (queue->srcpad,
496 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
497 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
498 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
501 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
502 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
503 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
504 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
505 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
506 queue->use_buffering = DEFAULT_USE_BUFFERING;
507 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
508 queue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
509 queue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
511 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
512 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
514 queue->sinktime = GST_CLOCK_TIME_NONE;
515 queue->srctime = GST_CLOCK_TIME_NONE;
516 queue->sink_tainted = TRUE;
517 queue->src_tainted = TRUE;
519 queue->srcresult = GST_FLOW_FLUSHING;
520 queue->sinkresult = GST_FLOW_FLUSHING;
521 queue->is_eos = FALSE;
522 queue->in_timer = g_timer_new ();
523 queue->out_timer = g_timer_new ();
525 g_mutex_init (&queue->qlock);
526 queue->waiting_add = FALSE;
527 g_cond_init (&queue->item_add);
528 queue->waiting_del = FALSE;
529 g_cond_init (&queue->item_del);
530 g_queue_init (&queue->queue);
532 g_cond_init (&queue->query_handled);
533 queue->last_query = FALSE;
535 g_mutex_init (&queue->buffering_post_lock);
536 queue->buffering_percent = 100;
538 /* tempfile related */
539 queue->temp_template = NULL;
540 queue->temp_location = NULL;
541 queue->temp_remove = DEFAULT_TEMP_REMOVE;
543 queue->ring_buffer = NULL;
544 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
546 GST_DEBUG_OBJECT (queue,
547 "initialized queue's not_empty & not_full conditions");
550 /* called only once, as opposed to dispose */
552 gst_queue2_finalize (GObject * object)
554 GstQueue2 *queue = GST_QUEUE2 (object);
556 GST_DEBUG_OBJECT (queue, "finalizing queue");
558 while (!g_queue_is_empty (&queue->queue)) {
559 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
561 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
562 gst_mini_object_unref (qitem->item);
563 g_slice_free (GstQueue2Item, qitem);
566 queue->last_query = FALSE;
567 g_queue_clear (&queue->queue);
568 g_mutex_clear (&queue->qlock);
569 g_mutex_clear (&queue->buffering_post_lock);
570 g_cond_clear (&queue->item_add);
571 g_cond_clear (&queue->item_del);
572 g_cond_clear (&queue->query_handled);
573 g_timer_destroy (queue->in_timer);
574 g_timer_destroy (queue->out_timer);
576 /* temp_file path cleanup */
577 g_free (queue->temp_template);
578 g_free (queue->temp_location);
580 G_OBJECT_CLASS (parent_class)->finalize (object);
584 debug_ranges (GstQueue2 * queue)
586 GstQueue2Range *walk;
588 for (walk = queue->ranges; walk; walk = walk->next) {
589 GST_DEBUG_OBJECT (queue,
590 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
591 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
592 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
593 walk->rb_writing_pos, walk->reading_pos,
594 walk == queue->current ? "**y**" : " n ");
598 /* clear all the downloaded ranges */
600 clean_ranges (GstQueue2 * queue)
602 GST_DEBUG_OBJECT (queue, "clean queue ranges");
604 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
605 queue->ranges = NULL;
606 queue->current = NULL;
609 /* find a range that contains @offset or NULL when nothing does */
610 static GstQueue2Range *
611 find_range (GstQueue2 * queue, guint64 offset)
613 GstQueue2Range *range = NULL;
614 GstQueue2Range *walk;
616 /* first do a quick check for the current range */
617 for (walk = queue->ranges; walk; walk = walk->next) {
618 if (offset >= walk->offset && offset <= walk->writing_pos) {
619 /* we can reuse an existing range */
625 GST_DEBUG_OBJECT (queue,
626 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
627 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
629 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
635 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
637 guint64 max_reading_pos, writing_pos;
639 writing_pos = range->writing_pos;
640 max_reading_pos = range->max_reading_pos;
642 if (writing_pos > max_reading_pos)
643 queue->cur_level.bytes = writing_pos - max_reading_pos;
645 queue->cur_level.bytes = 0;
648 /* make a new range for @offset or reuse an existing range */
649 static GstQueue2Range *
650 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
652 GstQueue2Range *range, *prev, *next;
654 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
656 if ((range = find_range (queue, offset))) {
657 GST_DEBUG_OBJECT (queue,
658 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
660 if (update_existing && range->writing_pos != offset) {
661 GST_DEBUG_OBJECT (queue, "updating range writing position to "
662 "%" G_GUINT64_FORMAT, offset);
663 range->writing_pos = offset;
666 GST_DEBUG_OBJECT (queue,
667 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
669 range = g_slice_new0 (GstQueue2Range);
670 range->offset = offset;
671 /* we want to write to the next location in the ring buffer */
672 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
673 range->writing_pos = offset;
674 range->rb_writing_pos = range->rb_offset;
675 range->reading_pos = offset;
676 range->max_reading_pos = offset;
680 next = queue->ranges;
682 if (next->offset > offset) {
683 /* insert before next */
684 GST_DEBUG_OBJECT (queue,
685 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
697 queue->ranges = range;
699 debug_ranges (queue);
701 /* update the stats for this range */
702 update_cur_level (queue, range);
708 /* clear and init the download ranges for offset 0 */
710 init_ranges (GstQueue2 * queue)
712 GST_DEBUG_OBJECT (queue, "init queue ranges");
714 /* get rid of all the current ranges */
715 clean_ranges (queue);
716 /* make a range for offset 0 */
717 queue->current = add_range (queue, 0, TRUE);
720 /* calculate the diff between running time on the sink and src of the queue.
721 * This is the total amount of time in the queue. */
723 update_time_level (GstQueue2 * queue)
725 if (queue->sink_tainted) {
727 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
728 queue->sink_segment.position);
729 queue->sink_tainted = FALSE;
732 if (queue->src_tainted) {
734 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
735 queue->src_segment.position);
736 queue->src_tainted = FALSE;
739 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
740 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
742 if (queue->sinktime != GST_CLOCK_TIME_NONE
743 && queue->srctime != GST_CLOCK_TIME_NONE
744 && queue->sinktime >= queue->srctime)
745 queue->cur_level.time = queue->sinktime - queue->srctime;
747 queue->cur_level.time = 0;
750 /* take a SEGMENT event and apply the values to segment, updating the time
753 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
756 gst_event_copy_segment (event, segment);
758 if (segment->format == GST_FORMAT_BYTES) {
759 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
760 /* start is where we'll be getting from and as such writing next */
761 queue->current = add_range (queue, segment->start, TRUE);
765 /* now configure the values, we use these to track timestamps on the
767 if (segment->format != GST_FORMAT_TIME) {
768 /* non-time format, pretend the current time segment is closed with a
769 * 0 start and unknown stop time. */
770 segment->format = GST_FORMAT_TIME;
776 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
779 queue->sink_tainted = TRUE;
781 queue->src_tainted = TRUE;
783 /* segment can update the time level of the queue */
784 update_time_level (queue);
788 apply_gap (GstQueue2 * queue, GstEvent * event,
789 GstSegment * segment, gboolean is_sink)
791 GstClockTime timestamp;
792 GstClockTime duration;
794 gst_event_parse_gap (event, ×tamp, &duration);
796 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
798 if (GST_CLOCK_TIME_IS_VALID (duration)) {
799 timestamp += duration;
802 segment->position = timestamp;
805 queue->sink_tainted = TRUE;
807 queue->src_tainted = TRUE;
809 /* calc diff with other end */
810 update_time_level (queue);
814 /* take a buffer and update segment, updating the time level of the queue. */
816 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
817 guint64 size, gboolean is_sink)
819 GstClockTime duration, timestamp;
821 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
822 duration = GST_BUFFER_DURATION (buffer);
824 /* If we have no duration, pick one from the bitrate if we can */
825 if (duration == GST_CLOCK_TIME_NONE && queue->use_tags_bitrate) {
827 is_sink ? queue->sink_tags_bitrate : queue->src_tags_bitrate;
829 duration = gst_util_uint64_scale (size, 8 * GST_SECOND, bitrate);
832 /* if no timestamp is set, assume it's continuous with the previous
834 if (timestamp == GST_CLOCK_TIME_NONE)
835 timestamp = segment->position;
838 if (duration != GST_CLOCK_TIME_NONE)
839 timestamp += duration;
841 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
842 GST_TIME_ARGS (timestamp));
844 segment->position = timestamp;
847 queue->sink_tainted = TRUE;
849 queue->src_tainted = TRUE;
851 /* calc diff with other end */
852 update_time_level (queue);
857 GstClockTime timestamp;
862 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
864 struct BufListData *bld = data;
865 GstClockTime *timestamp = &bld->timestamp;
868 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
869 " duration %" GST_TIME_FORMAT, idx,
870 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
871 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
872 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
874 btime = GST_BUFFER_DTS_OR_PTS (*buf);
875 if (GST_CLOCK_TIME_IS_VALID (btime))
878 if (GST_BUFFER_DURATION_IS_VALID (*buf))
879 *timestamp += GST_BUFFER_DURATION (*buf);
880 else if (bld->bitrate != 0) {
881 guint64 size = gst_buffer_get_size (*buf);
883 /* If we have no duration, pick one from the bitrate if we can */
884 *timestamp += gst_util_uint64_scale (bld->bitrate, 8 * GST_SECOND, size);
888 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
892 /* take a buffer list and update segment, updating the time level of the queue */
894 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
895 GstSegment * segment, gboolean is_sink)
897 struct BufListData bld;
899 /* if no timestamp is set, assume it's continuous with the previous time */
900 bld.timestamp = segment->position;
902 if (queue->use_tags_bitrate) {
904 bld.bitrate = queue->sink_tags_bitrate;
906 bld.bitrate = queue->src_tags_bitrate;
910 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &bld);
912 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
913 GST_TIME_ARGS (bld.timestamp));
915 segment->position = bld.timestamp;
918 queue->sink_tainted = TRUE;
920 queue->src_tainted = TRUE;
922 /* calc diff with other end */
923 update_time_level (queue);
927 normalize_to_buffering_level (guint64 cur_level, guint64 max_level,
936 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL,
937 MIN (max_level, alt_max));
939 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL, max_level);
941 return MIN (p, MAX_BUFFERING_LEVEL);
945 get_buffering_level (GstQueue2 * queue, gboolean * is_buffering,
946 gint * buffering_level)
948 gint buflevel, buflevel2;
950 if (queue->high_watermark <= 0) {
952 *buffering_level = MAX_BUFFERING_LEVEL;
954 *is_buffering = FALSE;
957 #define GET_BUFFER_LEVEL_FOR_QUANTITY(format,alt_max) \
958 normalize_to_buffering_level (queue->cur_level.format,queue->max_level.format,(alt_max))
961 /* on EOS we are always 100% full, we set the var here so that it we can
962 * reuse the logic below to stop buffering */
963 buflevel = MAX_BUFFERING_LEVEL;
964 GST_LOG_OBJECT (queue, "we are EOS");
966 GST_LOG_OBJECT (queue,
967 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
968 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
969 queue->cur_level.buffers);
971 /* figure out the buffering level we are filled, we take the max of all formats. */
972 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
973 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, 0);
975 guint64 rb_size = queue->ring_buffer_max_size;
976 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, rb_size);
979 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (time, 0);
980 buflevel = MAX (buflevel, buflevel2);
982 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (buffers, 0);
983 buflevel = MAX (buflevel, buflevel2);
985 /* also apply the rate estimate when we need to */
986 if (queue->use_rate_estimate) {
987 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (rate_time, 0);
988 buflevel = MAX (buflevel, buflevel2);
991 /* Don't get to 0% unless we're really empty */
992 if (queue->cur_level.bytes > 0)
993 buflevel = MAX (1, buflevel);
995 #undef GET_BUFFER_LEVEL_FOR_QUANTITY
998 *is_buffering = queue->is_buffering;
1000 if (buffering_level)
1001 *buffering_level = buflevel;
1003 GST_DEBUG_OBJECT (queue, "buffering %d, level %d", queue->is_buffering,
1010 convert_to_buffering_percent (GstQueue2 * queue, gint buffering_level)
1014 /* scale so that if buffering_level equals the high watermark,
1015 * the percentage is 100% */
1016 percent = buffering_level * 100 / queue->high_watermark;
1025 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
1026 gint * avg_in, gint * avg_out, gint64 * buffering_left)
1029 if (!QUEUE_IS_USING_QUEUE (queue)) {
1030 if (QUEUE_IS_USING_RING_BUFFER (queue))
1031 *mode = GST_BUFFERING_TIMESHIFT;
1033 *mode = GST_BUFFERING_DOWNLOAD;
1035 *mode = GST_BUFFERING_STREAM;
1040 *avg_in = queue->byte_in_rate;
1042 *avg_out = queue->byte_out_rate;
1044 if (buffering_left) {
1045 *buffering_left = (percent == 100 ? 0 : -1);
1047 if (queue->use_rate_estimate) {
1050 max = queue->max_level.rate_time;
1051 cur = queue->cur_level.rate_time;
1053 if (percent != 100 && max > cur)
1054 *buffering_left = (max - cur) / 1000000;
1059 /* Called with the lock taken */
1061 gst_queue2_get_buffering_message (GstQueue2 * queue)
1063 GstMessage *msg = NULL;
1065 if (queue->percent_changed) {
1066 gint percent = queue->buffering_percent;
1068 queue->percent_changed = FALSE;
1070 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
1071 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
1073 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
1074 queue->avg_out, queue->buffering_left);
1081 gst_queue2_post_buffering (GstQueue2 * queue)
1083 GstMessage *msg = NULL;
1085 g_mutex_lock (&queue->buffering_post_lock);
1086 GST_QUEUE2_MUTEX_LOCK (queue);
1087 msg = gst_queue2_get_buffering_message (queue);
1088 GST_QUEUE2_MUTEX_UNLOCK (queue);
1091 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1093 g_mutex_unlock (&queue->buffering_post_lock);
1097 update_buffering (GstQueue2 * queue)
1099 gint buffering_level, percent;
1101 /* Ensure the variables used to calculate buffering state are up-to-date. */
1103 update_cur_level (queue, queue->current);
1104 update_in_rates (queue, FALSE);
1106 if (!get_buffering_level (queue, NULL, &buffering_level))
1109 percent = convert_to_buffering_percent (queue, buffering_level);
1111 if (queue->is_buffering) {
1112 /* if we were buffering see if we reached the high watermark */
1114 queue->is_buffering = FALSE;
1116 SET_PERCENT (queue, percent);
1118 /* we were not buffering, check if we need to start buffering if we drop
1119 * below the low threshold */
1120 if (buffering_level < queue->low_watermark) {
1121 queue->is_buffering = TRUE;
1122 SET_PERCENT (queue, percent);
1128 reset_rate_timer (GstQueue2 * queue)
1130 queue->bytes_in = 0;
1131 queue->bytes_out = 0;
1132 queue->byte_in_rate = 0.0;
1133 queue->byte_in_period = 0;
1134 queue->byte_out_rate = 0.0;
1135 queue->last_update_in_rates_elapsed = 0.0;
1136 queue->last_in_elapsed = 0.0;
1137 queue->last_out_elapsed = 0.0;
1138 queue->in_timer_started = FALSE;
1139 queue->out_timer_started = FALSE;
1142 /* the interval in seconds to recalculate the rate */
1143 #define RATE_INTERVAL 0.2
1144 /* Tuning for rate estimation. We use a large window for the input rate because
1145 * it should be stable when connected to a network. The output rate is less
1146 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1147 * therefore adapt more quickly.
1148 * However, initial input rate may be subject to a burst, and should therefore
1149 * initially also adapt more quickly to changes, and only later on give higher
1150 * weight to previous values. */
1151 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1152 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1155 update_in_rates (GstQueue2 * queue, gboolean force)
1157 gdouble elapsed, period;
1158 gdouble byte_in_rate;
1160 if (!queue->in_timer_started) {
1161 queue->in_timer_started = TRUE;
1162 g_timer_start (queue->in_timer);
1166 queue->last_update_in_rates_elapsed = elapsed =
1167 g_timer_elapsed (queue->in_timer, NULL);
1169 /* recalc after each interval. */
1170 if (force || queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1171 period = elapsed - queue->last_in_elapsed;
1173 GST_DEBUG_OBJECT (queue,
1174 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1175 period, queue->bytes_in, queue->byte_in_period);
1177 byte_in_rate = queue->bytes_in / period;
1179 if (queue->byte_in_rate == 0.0)
1180 queue->byte_in_rate = byte_in_rate;
1182 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1183 (double) queue->byte_in_period, period);
1185 /* another data point, cap at 16 for long time running average */
1186 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1187 queue->byte_in_period += period;
1189 /* reset the values to calculate rate over the next interval */
1190 queue->last_in_elapsed = elapsed;
1191 queue->bytes_in = 0;
1194 if (queue->byte_in_rate > 0.0) {
1195 queue->cur_level.rate_time =
1196 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1198 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1199 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1203 update_out_rates (GstQueue2 * queue)
1205 gdouble elapsed, period;
1206 gdouble byte_out_rate;
1208 if (!queue->out_timer_started) {
1209 queue->out_timer_started = TRUE;
1210 g_timer_start (queue->out_timer);
1214 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1216 /* recalc after each interval. */
1217 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1218 period = elapsed - queue->last_out_elapsed;
1220 GST_DEBUG_OBJECT (queue,
1221 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1223 byte_out_rate = queue->bytes_out / period;
1225 if (queue->byte_out_rate == 0.0)
1226 queue->byte_out_rate = byte_out_rate;
1228 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1230 /* reset the values to calculate rate over the next interval */
1231 queue->last_out_elapsed = elapsed;
1232 queue->bytes_out = 0;
1234 if (queue->byte_in_rate > 0.0) {
1235 queue->cur_level.rate_time =
1236 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1238 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1239 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1243 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1245 guint64 reading_pos, max_reading_pos;
1248 max_reading_pos = range->max_reading_pos;
1250 max_reading_pos = MAX (max_reading_pos, reading_pos);
1252 GST_DEBUG_OBJECT (queue,
1253 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1254 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1255 range->max_reading_pos = max_reading_pos;
1257 update_cur_level (queue, range);
1261 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1266 /* until we receive the FLUSH_STOP from this seek, we skip data */
1267 queue->seeking = TRUE;
1268 GST_QUEUE2_MUTEX_UNLOCK (queue);
1270 debug_ranges (queue);
1272 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1275 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1276 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1277 GST_SEEK_TYPE_NONE, -1);
1279 res = gst_pad_push_event (queue->sinkpad, event);
1280 GST_QUEUE2_MUTEX_LOCK (queue);
1283 /* Between us sending the seek event and re-acquiring the lock, the source
1284 * thread might already have pushed data and moved along the range's
1285 * writing_pos beyond the seek offset. In that case we don't want to set
1286 * the writing position back to the requested seek position, as it would
1287 * cause data to be written to the wrong offset in the file or ring buffer.
1288 * We still do the add_range call to switch the current range to the
1289 * requested range, or create one if one doesn't exist yet. */
1290 queue->current = add_range (queue, offset, FALSE);
1296 /* get the threshold for when we decide to seek rather than wait */
1298 get_seek_threshold (GstQueue2 * queue)
1302 /* FIXME, find a good threshold based on the incoming rate. */
1303 threshold = 1024 * 512;
1305 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1306 threshold = MIN (threshold,
1307 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1312 /* see if there is enough data in the file to read a full buffer */
1314 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1316 GstQueue2Range *range;
1318 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1321 if ((range = find_range (queue, offset))) {
1322 if (queue->current != range) {
1323 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1324 perform_seek_to_offset (queue, range->writing_pos);
1327 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1328 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1330 /* we have a range for offset */
1331 GST_DEBUG_OBJECT (queue,
1332 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1333 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1335 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1338 if (offset + length <= range->writing_pos)
1341 GST_DEBUG_OBJECT (queue,
1342 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1343 (offset + length) - range->writing_pos);
1346 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1347 " len %u", offset, length);
1348 /* we don't have the range, see how far away we are */
1349 if (!queue->is_eos && queue->current) {
1350 guint64 threshold = get_seek_threshold (queue);
1352 if (offset >= queue->current->offset && offset <=
1353 queue->current->writing_pos + threshold) {
1354 GST_INFO_OBJECT (queue,
1355 "requested data is within range, wait for data");
1360 /* too far away, do a seek */
1361 perform_seek_to_offset (queue, offset);
1368 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1369 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1370 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1372 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1375 static GstFlowReturn
1376 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1377 guint8 * dst, gint64 * read_return)
1379 guint8 *ring_buffer;
1382 ring_buffer = queue->ring_buffer;
1384 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1387 /* this should not block */
1388 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1390 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1391 res = fread (dst, 1, length, queue->temp_file);
1393 memcpy (dst, ring_buffer + offset, length);
1397 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1399 if (G_UNLIKELY (res < length)) {
1400 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1401 goto could_not_read;
1402 /* check for errors or EOF */
1403 if (ferror (queue->temp_file))
1404 goto could_not_read;
1405 if (feof (queue->temp_file) && length > 0)
1415 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1416 return GST_FLOW_ERROR;
1420 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1421 return GST_FLOW_ERROR;
1425 GST_DEBUG ("non-regular file hits EOS");
1426 return GST_FLOW_EOS;
1430 static GstFlowReturn
1431 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1432 GstBuffer ** buffer)
1437 guint64 file_offset;
1438 guint block_length, remaining, read_length;
1442 GstFlowReturn ret = GST_FLOW_OK;
1444 /* allocate the output buffer of the requested size */
1445 if (*buffer == NULL)
1446 buf = gst_buffer_new_allocate (NULL, length, NULL);
1450 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
1451 goto buffer_write_fail;
1454 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1458 rb_size = queue->ring_buffer_max_size;
1459 max_size = QUEUE_MAX_BYTES (queue);
1462 while (remaining > 0) {
1463 /* configure how much/whether to read */
1464 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1467 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1470 /* calculate how far away the offset is */
1471 if (queue->current->writing_pos > rpos)
1472 level = queue->current->writing_pos - rpos;
1476 GST_DEBUG_OBJECT (queue,
1477 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1478 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1479 rpos, queue->current->writing_pos, level, max_size);
1481 if (level >= max_size) {
1482 /* we don't have the data but if we have a ring buffer that is full, we
1484 GST_DEBUG_OBJECT (queue,
1485 "ring buffer full, reading QUEUE_MAX_BYTES %"
1486 G_GUINT64_FORMAT " bytes", max_size);
1487 read_length = max_size;
1488 } else if (queue->is_eos) {
1489 /* won't get any more data so read any data we have */
1491 GST_DEBUG_OBJECT (queue,
1492 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1494 read_length = level;
1502 if (read_length == 0) {
1503 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1504 GST_DEBUG_OBJECT (queue,
1505 "update current position [%" G_GUINT64_FORMAT "-%"
1506 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1507 update_cur_pos (queue, queue->current, rpos);
1508 GST_QUEUE2_SIGNAL_DEL (queue);
1511 if (queue->use_buffering)
1512 update_buffering (queue);
1514 GST_DEBUG_OBJECT (queue, "waiting for add");
1515 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1519 /* we have the requested data so read it */
1520 read_length = remaining;
1523 /* set range reading_pos to actual reading position for this read */
1524 queue->current->reading_pos = rpos;
1526 /* configure how much and from where to read */
1527 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1529 (queue->current->rb_offset + (rpos -
1530 queue->current->offset)) % rb_size;
1531 if (file_offset + read_length > rb_size) {
1532 block_length = rb_size - file_offset;
1534 block_length = read_length;
1538 block_length = read_length;
1541 /* while we still have data to read, we loop */
1542 while (read_length > 0) {
1546 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1547 data, &read_return);
1548 if (ret != GST_FLOW_OK)
1551 file_offset += read_return;
1552 if (QUEUE_IS_USING_RING_BUFFER (queue))
1553 file_offset %= rb_size;
1555 data += read_return;
1556 read_length -= read_return;
1557 block_length = read_length;
1558 remaining -= read_return;
1560 rpos = (queue->current->reading_pos += read_return);
1561 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1563 GST_QUEUE2_SIGNAL_DEL (queue);
1564 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1567 gst_buffer_unmap (buf, &info);
1568 gst_buffer_resize (buf, 0, length);
1570 GST_BUFFER_OFFSET (buf) = offset;
1571 GST_BUFFER_OFFSET_END (buf) = offset + length;
1580 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1581 gst_buffer_unmap (buf, &info);
1582 if (*buffer == NULL)
1583 gst_buffer_unref (buf);
1584 return GST_FLOW_EOS;
1588 GST_DEBUG_OBJECT (queue, "we are flushing");
1589 gst_buffer_unmap (buf, &info);
1590 if (*buffer == NULL)
1591 gst_buffer_unref (buf);
1592 return GST_FLOW_FLUSHING;
1596 GST_DEBUG_OBJECT (queue, "we have a read error");
1597 gst_buffer_unmap (buf, &info);
1598 if (*buffer == NULL)
1599 gst_buffer_unref (buf);
1604 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE, (NULL),
1605 ("Can't write to buffer"));
1606 if (*buffer == NULL)
1607 gst_buffer_unref (buf);
1608 return GST_FLOW_ERROR;
1612 /* should be called with QUEUE_LOCK */
1613 static GstMiniObject *
1614 gst_queue2_read_item_from_file (GstQueue2 * queue)
1616 GstMiniObject *item;
1618 if (queue->stream_start_event != NULL) {
1619 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1620 queue->stream_start_event = NULL;
1621 } else if (queue->starting_segment != NULL) {
1622 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1623 queue->starting_segment = NULL;
1626 GstBuffer *buffer = NULL;
1627 guint64 reading_pos;
1629 reading_pos = queue->current->reading_pos;
1632 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1637 item = GST_MINI_OBJECT_CAST (buffer);
1640 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1650 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1651 * the temp filename. */
1653 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1658 if (queue->temp_file)
1659 goto already_opened;
1661 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1663 /* If temp_template was set, allocate a filename and open that file */
1666 if (queue->temp_template == NULL)
1669 /* make copy of the template, we don't want to change this */
1670 name = g_strdup (queue->temp_template);
1673 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1675 fd = g_mkstemp (name);
1679 goto mkstemp_failed;
1681 /* open the file for update/writing */
1682 queue->temp_file = fdopen (fd, "wb+");
1683 /* error creating file */
1684 if (queue->temp_file == NULL)
1687 g_free (queue->temp_location);
1688 queue->temp_location = name;
1690 GST_QUEUE2_MUTEX_UNLOCK (queue);
1692 /* we can't emit the notify with the lock */
1693 g_object_notify (G_OBJECT (queue), "temp-location");
1695 GST_QUEUE2_MUTEX_LOCK (queue);
1697 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1704 GST_DEBUG_OBJECT (queue, "temp file was already open");
1709 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1710 (_("No Temp directory specified.")), (NULL));
1715 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1716 (_("Could not create temp file \"%s\"."), queue->temp_template),
1723 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1724 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1733 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1736 if (queue->temp_file == NULL)
1739 GST_DEBUG_OBJECT (queue, "closing temp file");
1741 fflush (queue->temp_file);
1742 fclose (queue->temp_file);
1744 if (queue->temp_remove) {
1745 if (remove (queue->temp_location) < 0) {
1746 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1747 queue->temp_location, g_strerror (errno));
1751 queue->temp_file = NULL;
1752 clean_ranges (queue);
1756 gst_queue2_flush_temp_file (GstQueue2 * queue)
1758 if (queue->temp_file == NULL)
1761 GST_DEBUG_OBJECT (queue, "flushing temp file");
1763 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1767 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1769 if (!QUEUE_IS_USING_QUEUE (queue)) {
1770 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1771 gst_queue2_flush_temp_file (queue);
1772 init_ranges (queue);
1774 while (!g_queue_is_empty (&queue->queue)) {
1775 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1777 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1778 && GST_EVENT_IS_STICKY (qitem->item)
1779 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1780 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1781 gst_pad_store_sticky_event (queue->srcpad,
1782 GST_EVENT_CAST (qitem->item));
1785 /* Then lose another reference because we are supposed to destroy that
1786 data when flushing */
1787 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1788 gst_mini_object_unref (qitem->item);
1789 g_slice_free (GstQueue2Item, qitem);
1792 queue->last_query = FALSE;
1793 g_cond_signal (&queue->query_handled);
1794 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1795 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1796 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1797 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1798 queue->sink_tainted = queue->src_tainted = TRUE;
1799 if (queue->starting_segment != NULL)
1800 gst_event_unref (queue->starting_segment);
1801 queue->starting_segment = NULL;
1802 queue->segment_event_received = FALSE;
1803 gst_event_replace (&queue->stream_start_event, NULL);
1805 /* we deleted a lot of something */
1806 GST_QUEUE2_SIGNAL_DEL (queue);
1810 gst_queue2_wait_free_space (GstQueue2 * queue)
1812 /* We make space available if we're "full" according to whatever
1813 * the user defined as "full". */
1814 if (gst_queue2_is_filled (queue)) {
1817 /* pause the timer while we wait. The fact that we are waiting does not mean
1818 * the byterate on the input pad is lower */
1819 if ((started = queue->in_timer_started))
1820 g_timer_stop (queue->in_timer);
1822 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1823 "queue is full, waiting for free space");
1825 /* Wait for space to be available, we could be unlocked because of a flush. */
1826 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1828 while (gst_queue2_is_filled (queue));
1830 /* and continue if we were running before */
1832 g_timer_continue (queue->in_timer);
1839 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1845 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1848 guint8 *data, *ring_buffer;
1849 guint size, rb_size;
1850 guint64 writing_pos, new_writing_pos;
1851 GstQueue2Range *range, *prev, *next;
1852 gboolean do_seek = FALSE;
1854 if (QUEUE_IS_USING_RING_BUFFER (queue))
1855 writing_pos = queue->current->rb_writing_pos;
1857 writing_pos = queue->current->writing_pos;
1858 ring_buffer = queue->ring_buffer;
1859 rb_size = queue->ring_buffer_max_size;
1861 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
1862 goto buffer_read_error;
1867 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1871 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1872 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
1873 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
1874 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1875 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
1881 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1884 /* calculate the space in the ring buffer not used by data from
1885 * the current range */
1886 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1887 /* wait until there is some free space */
1888 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1890 /* get the amount of space we have */
1891 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1893 /* calculate if we need to split or if we can write the entire
1895 to_write = MIN (size, space);
1897 /* the writing position in the ring buffer after writing (part
1898 * or all of) the buffer */
1899 new_writing_pos = (writing_pos + to_write) % rb_size;
1902 range = queue->ranges;
1904 /* if we need to overwrite data in the ring buffer, we need to
1907 * warning: this code is complicated and includes some
1908 * simplifications - pen, paper and diagrams for the cases
1911 guint64 range_data_start, range_data_end;
1912 GstQueue2Range *range_to_destroy = NULL;
1914 if (range == queue->current)
1917 range_data_start = range->rb_offset;
1918 range_data_end = range->rb_writing_pos;
1920 /* handle the special case where the range has no data in it */
1921 if (range->writing_pos == range->offset) {
1922 if (range != queue->current) {
1923 GST_DEBUG_OBJECT (queue,
1924 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1925 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1927 range_to_destroy = range;
1929 prev->next = range->next;
1934 if (range_data_end > range_data_start) {
1935 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1938 if (new_writing_pos > range_data_start) {
1939 if (new_writing_pos >= range_data_end) {
1940 GST_DEBUG_OBJECT (queue,
1941 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1942 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1944 range_to_destroy = range;
1946 prev->next = range->next;
1948 GST_DEBUG_OBJECT (queue,
1949 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1950 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1951 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1952 range->offset + new_writing_pos - range_data_start,
1954 range->offset += (new_writing_pos - range_data_start);
1955 range->rb_offset = new_writing_pos;
1959 guint64 new_wpos_virt = writing_pos + to_write;
1961 if (new_wpos_virt <= range_data_start)
1964 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1965 GST_DEBUG_OBJECT (queue,
1966 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1967 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1969 range_to_destroy = range;
1971 prev->next = range->next;
1973 GST_DEBUG_OBJECT (queue,
1974 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1975 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1976 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1977 range->offset + new_writing_pos - range_data_start,
1979 range->offset += (new_wpos_virt - range_data_start);
1980 range->rb_offset = new_writing_pos;
1985 if (!range_to_destroy)
1988 range = range->next;
1989 if (range_to_destroy) {
1990 if (range_to_destroy == queue->ranges)
1991 queue->ranges = range;
1992 g_slice_free (GstQueue2Range, range_to_destroy);
1993 range_to_destroy = NULL;
1998 new_writing_pos = writing_pos + to_write;
2001 if (QUEUE_IS_USING_TEMP_FILE (queue)
2002 && FSEEK_FILE (queue->temp_file, writing_pos))
2005 if (new_writing_pos > writing_pos) {
2006 GST_INFO_OBJECT (queue,
2007 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2008 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
2009 queue->current->writing_pos, queue->current->rb_writing_pos);
2010 /* either not using ring buffer or no wrapping, just write */
2011 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2012 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
2015 memcpy (ring_buffer + writing_pos, data, to_write);
2018 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
2019 /* try to merge with next range */
2020 while ((next = queue->current->next)) {
2021 GST_INFO_OBJECT (queue,
2022 "checking merge with next range %" G_GUINT64_FORMAT " < %"
2023 G_GUINT64_FORMAT, new_writing_pos, next->offset);
2024 if (new_writing_pos < next->offset)
2027 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
2030 /* remove the group */
2031 queue->current->next = next->next;
2033 /* We use the threshold to decide if we want to do a seek or simply
2034 * read the data again. If there is not so much data in the range we
2035 * prefer to avoid to seek and read it again. */
2036 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
2037 /* the new range had more data than the threshold, it's worth keeping
2038 * it and doing a seek. */
2039 new_writing_pos = next->writing_pos;
2042 g_slice_free (GstQueue2Range, next);
2044 goto update_and_signal;
2048 guint block_one, block_two;
2050 block_one = rb_size - writing_pos;
2051 block_two = to_write - block_one;
2053 if (block_one > 0) {
2054 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
2055 /* write data to end of ring buffer */
2056 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2057 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
2060 memcpy (ring_buffer + writing_pos, data, block_one);
2064 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
2067 if (block_two > 0) {
2068 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
2069 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2070 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
2073 memcpy (ring_buffer, data + block_one, block_two);
2079 /* update the writing positions */
2081 GST_INFO_OBJECT (queue,
2082 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2083 to_write, writing_pos, size);
2085 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2087 queue->current->writing_pos += to_write;
2088 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2090 queue->current->writing_pos = writing_pos = new_writing_pos;
2093 perform_seek_to_offset (queue, new_writing_pos);
2095 update_cur_level (queue, queue->current);
2097 /* update the buffering status */
2098 if (queue->use_buffering) {
2100 update_buffering (queue);
2101 msg = gst_queue2_get_buffering_message (queue);
2103 GST_QUEUE2_MUTEX_UNLOCK (queue);
2104 g_mutex_lock (&queue->buffering_post_lock);
2105 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
2106 g_mutex_unlock (&queue->buffering_post_lock);
2107 GST_QUEUE2_MUTEX_LOCK (queue);
2111 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2112 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2114 GST_QUEUE2_SIGNAL_ADD (queue);
2117 gst_buffer_unmap (buffer, &info);
2124 GST_DEBUG_OBJECT (queue, "we are flushing");
2125 gst_buffer_unmap (buffer, &info);
2126 /* FIXME - GST_FLOW_EOS ? */
2131 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2132 gst_buffer_unmap (buffer, &info);
2139 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2143 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2144 (_("Error while writing to download file.")),
2145 ("%s", g_strerror (errno)));
2148 gst_buffer_unmap (buffer, &info);
2153 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL),
2154 ("Can't read from buffer"));
2160 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2162 GstQueue2 *queue = q;
2164 GST_TRACE_OBJECT (queue,
2165 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2166 gst_buffer_get_size (*buf));
2168 if (!gst_queue2_create_write (queue, *buf)) {
2169 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2176 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2178 guint *p_size = data;
2181 buf_size = gst_buffer_get_size (*buf);
2182 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2183 *p_size += buf_size;
2187 /* enqueue an item an update the level stats */
2189 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2190 GstQueue2ItemType item_type)
2192 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2196 buffer = GST_BUFFER_CAST (item);
2197 size = gst_buffer_get_size (buffer);
2199 /* add buffer to the statistics */
2200 if (QUEUE_IS_USING_QUEUE (queue)) {
2201 queue->cur_level.buffers++;
2202 queue->cur_level.bytes += size;
2204 queue->bytes_in += size;
2206 /* apply new buffer to segment stats */
2207 apply_buffer (queue, buffer, &queue->sink_segment, size, TRUE);
2208 /* update the byterate stats */
2209 update_in_rates (queue, FALSE);
2211 if (!QUEUE_IS_USING_QUEUE (queue)) {
2212 /* FIXME - check return value? */
2213 gst_queue2_create_write (queue, buffer);
2215 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2216 GstBufferList *buffer_list;
2219 buffer_list = GST_BUFFER_LIST_CAST (item);
2221 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2222 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2224 /* add buffer to the statistics */
2225 if (QUEUE_IS_USING_QUEUE (queue)) {
2226 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2227 queue->cur_level.bytes += size;
2229 queue->bytes_in += size;
2231 /* apply new buffer to segment stats */
2232 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2234 /* update the byterate stats */
2235 update_in_rates (queue, FALSE);
2237 if (!QUEUE_IS_USING_QUEUE (queue)) {
2238 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2240 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2243 event = GST_EVENT_CAST (item);
2245 switch (GST_EVENT_TYPE (event)) {
2247 /* Zero the thresholds, this makes sure the queue is completely
2248 * filled and we can read all data from the queue. */
2249 GST_DEBUG_OBJECT (queue, "we have EOS");
2250 queue->is_eos = TRUE;
2251 /* Force updating the input bitrate */
2252 update_in_rates (queue, TRUE);
2254 case GST_EVENT_SEGMENT:
2255 apply_segment (queue, event, &queue->sink_segment, TRUE);
2256 /* This is our first new segment, we hold it
2257 * as we can't save it on the temp file */
2258 if (!QUEUE_IS_USING_QUEUE (queue)) {
2259 if (queue->segment_event_received)
2260 goto unexpected_event;
2262 queue->segment_event_received = TRUE;
2263 if (queue->starting_segment != NULL)
2264 gst_event_unref (queue->starting_segment);
2265 queue->starting_segment = event;
2268 /* a new segment allows us to accept more buffers if we got EOS
2269 * from downstream */
2270 queue->unexpected = FALSE;
2273 apply_gap (queue, event, &queue->sink_segment, TRUE);
2275 case GST_EVENT_STREAM_START:
2276 if (!QUEUE_IS_USING_QUEUE (queue)) {
2277 gst_event_replace (&queue->stream_start_event, event);
2278 gst_event_unref (event);
2282 case GST_EVENT_CAPS:{
2285 gst_event_parse_caps (event, &caps);
2286 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2288 if (!QUEUE_IS_USING_QUEUE (queue)) {
2289 GST_LOG ("Dropping caps event, not using queue");
2290 gst_event_unref (event);
2296 if (!QUEUE_IS_USING_QUEUE (queue))
2297 goto unexpected_event;
2300 } else if (GST_IS_QUERY (item)) {
2301 /* Can't happen as we check that in the caller */
2302 if (!QUEUE_IS_USING_QUEUE (queue))
2303 g_assert_not_reached ();
2305 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2306 item, GST_OBJECT_NAME (queue));
2307 /* we can't really unref since we don't know what it is */
2312 /* update the buffering status */
2313 if (queue->use_buffering)
2314 update_buffering (queue);
2316 if (QUEUE_IS_USING_QUEUE (queue)) {
2317 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2318 qitem->type = item_type;
2320 g_queue_push_tail (&queue->queue, qitem);
2322 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2325 GST_QUEUE2_SIGNAL_ADD (queue);
2333 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2335 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2336 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2337 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2338 gst_event_unref (GST_EVENT_CAST (item));
2343 /* dequeue an item from the queue and update level stats */
2344 static GstMiniObject *
2345 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2347 GstMiniObject *item;
2349 if (!QUEUE_IS_USING_QUEUE (queue)) {
2350 item = gst_queue2_read_item_from_file (queue);
2352 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2358 g_slice_free (GstQueue2Item, qitem);
2364 if (GST_IS_BUFFER (item)) {
2368 buffer = GST_BUFFER_CAST (item);
2369 size = gst_buffer_get_size (buffer);
2370 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2372 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2373 "retrieved buffer %p from queue", buffer);
2375 if (QUEUE_IS_USING_QUEUE (queue)) {
2376 queue->cur_level.buffers--;
2377 queue->cur_level.bytes -= size;
2379 queue->bytes_out += size;
2381 apply_buffer (queue, buffer, &queue->src_segment, size, FALSE);
2382 /* update the byterate stats */
2383 update_out_rates (queue);
2384 /* update the buffering */
2385 if (queue->use_buffering)
2386 update_buffering (queue);
2388 } else if (GST_IS_EVENT (item)) {
2389 GstEvent *event = GST_EVENT_CAST (item);
2391 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2393 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2394 "retrieved event %p from queue", event);
2396 switch (GST_EVENT_TYPE (event)) {
2398 /* queue is empty now that we dequeued the EOS */
2399 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2401 case GST_EVENT_SEGMENT:
2402 apply_segment (queue, event, &queue->src_segment, FALSE);
2405 apply_gap (queue, event, &queue->src_segment, FALSE);
2410 } else if (GST_IS_BUFFER_LIST (item)) {
2411 GstBufferList *buffer_list;
2414 buffer_list = GST_BUFFER_LIST_CAST (item);
2415 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2416 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2418 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2419 "retrieved buffer list %p from queue", buffer_list);
2421 if (QUEUE_IS_USING_QUEUE (queue)) {
2422 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2423 queue->cur_level.bytes -= size;
2425 queue->bytes_out += size;
2427 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2428 /* update the byterate stats */
2429 update_out_rates (queue);
2430 /* update the buffering */
2431 if (queue->use_buffering)
2432 update_buffering (queue);
2433 } else if (GST_IS_QUERY (item)) {
2434 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2435 "retrieved query %p from queue", item);
2436 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2439 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2440 item, GST_OBJECT_NAME (queue));
2442 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2444 GST_QUEUE2_SIGNAL_DEL (queue);
2451 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2456 static GstFlowReturn
2457 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2460 gboolean ret = TRUE;
2463 queue = GST_QUEUE2 (parent);
2465 switch (GST_EVENT_TYPE (event)) {
2466 case GST_EVENT_FLUSH_START:
2468 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2469 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2471 ret = gst_pad_push_event (queue->srcpad, event);
2473 /* now unblock the chain function */
2474 GST_QUEUE2_MUTEX_LOCK (queue);
2475 queue->srcresult = GST_FLOW_FLUSHING;
2476 queue->sinkresult = GST_FLOW_FLUSHING;
2477 /* unblock the loop and chain functions */
2478 GST_QUEUE2_SIGNAL_ADD (queue);
2479 GST_QUEUE2_SIGNAL_DEL (queue);
2480 GST_QUEUE2_MUTEX_UNLOCK (queue);
2482 /* make sure it pauses, this should happen since we sent
2483 * flush_start downstream. */
2484 gst_pad_pause_task (queue->srcpad);
2485 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2487 GST_QUEUE2_MUTEX_LOCK (queue);
2488 queue->last_query = FALSE;
2489 g_cond_signal (&queue->query_handled);
2490 GST_QUEUE2_MUTEX_UNLOCK (queue);
2492 GST_QUEUE2_MUTEX_LOCK (queue);
2493 /* flush the sink pad */
2494 queue->sinkresult = GST_FLOW_FLUSHING;
2495 GST_QUEUE2_SIGNAL_DEL (queue);
2496 queue->last_query = FALSE;
2497 g_cond_signal (&queue->query_handled);
2498 GST_QUEUE2_MUTEX_UNLOCK (queue);
2500 gst_event_unref (event);
2504 case GST_EVENT_FLUSH_STOP:
2506 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2508 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2510 ret = gst_pad_push_event (queue->srcpad, event);
2512 GST_QUEUE2_MUTEX_LOCK (queue);
2513 gst_queue2_locked_flush (queue, FALSE, TRUE);
2514 queue->srcresult = GST_FLOW_OK;
2515 queue->sinkresult = GST_FLOW_OK;
2516 queue->is_eos = FALSE;
2517 queue->unexpected = FALSE;
2518 queue->seeking = FALSE;
2519 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2520 /* reset rate counters */
2521 reset_rate_timer (queue);
2522 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2523 queue->srcpad, NULL);
2524 GST_QUEUE2_MUTEX_UNLOCK (queue);
2526 GST_QUEUE2_MUTEX_LOCK (queue);
2527 queue->segment_event_received = FALSE;
2528 queue->is_eos = FALSE;
2529 queue->unexpected = FALSE;
2530 queue->sinkresult = GST_FLOW_OK;
2531 queue->seeking = FALSE;
2532 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2533 GST_QUEUE2_MUTEX_UNLOCK (queue);
2535 gst_event_unref (event);
2539 case GST_EVENT_TAG:{
2540 if (queue->use_tags_bitrate) {
2544 gst_event_parse_tag (event, &tags);
2545 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2546 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2547 GST_QUEUE2_MUTEX_LOCK (queue);
2548 queue->sink_tags_bitrate = bitrate;
2549 GST_QUEUE2_MUTEX_UNLOCK (queue);
2550 GST_LOG_OBJECT (queue, "Sink pad bitrate from tags now %u", bitrate);
2556 if (GST_EVENT_IS_SERIALIZED (event)) {
2557 /* serialized events go in the queue */
2558 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2559 if (queue->srcresult != GST_FLOW_OK) {
2560 /* Errors in sticky event pushing are no problem and ignored here
2561 * as they will cause more meaningful errors during data flow.
2562 * For EOS events, that are not followed by data flow, we still
2563 * return FALSE here though and report an error.
2565 if (!GST_EVENT_IS_STICKY (event)) {
2566 goto out_flow_error;
2567 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2568 if (queue->srcresult == GST_FLOW_NOT_LINKED
2569 || queue->srcresult < GST_FLOW_EOS) {
2570 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
2572 goto out_flow_error;
2575 /* refuse more events on EOS */
2578 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2579 GST_QUEUE2_MUTEX_UNLOCK (queue);
2580 gst_queue2_post_buffering (queue);
2582 /* non-serialized events are passed upstream. */
2583 ret = gst_pad_push_event (queue->srcpad, event);
2588 return GST_FLOW_ERROR;
2594 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2595 GST_QUEUE2_MUTEX_UNLOCK (queue);
2596 gst_event_unref (event);
2597 return GST_FLOW_FLUSHING;
2601 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2602 GST_QUEUE2_MUTEX_UNLOCK (queue);
2603 gst_event_unref (event);
2604 return GST_FLOW_EOS;
2608 GST_LOG_OBJECT (queue,
2609 "refusing event, we have a downstream flow error: %s",
2610 gst_flow_get_name (queue->srcresult));
2611 GST_QUEUE2_MUTEX_UNLOCK (queue);
2612 gst_event_unref (event);
2613 return queue->srcresult;
2618 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2624 queue = GST_QUEUE2 (parent);
2626 switch (GST_QUERY_TYPE (query)) {
2628 if (GST_QUERY_IS_SERIALIZED (query)) {
2629 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2630 /* serialized events go in the queue. We need to be certain that we
2631 * don't cause deadlocks waiting for the query return value. We check if
2632 * the queue is empty (nothing is blocking downstream and the query can
2633 * be pushed for sure) or we are not buffering. If we are buffering,
2634 * the pipeline waits to unblock downstream until our queue fills up
2635 * completely, which can not happen if we block on the query..
2636 * Therefore we only potentially block when we are not buffering. */
2637 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2638 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2639 || !queue->use_buffering)) {
2640 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2641 gst_queue2_locked_enqueue (queue, query,
2642 GST_QUEUE2_ITEM_TYPE_QUERY);
2644 STATUS (queue, queue->sinkpad, "wait for QUERY");
2645 while (queue->sinkresult == GST_FLOW_OK &&
2646 queue->last_handled_query != query)
2647 g_cond_wait (&queue->query_handled, &queue->qlock);
2648 queue->last_handled_query = NULL;
2649 if (queue->sinkresult != GST_FLOW_OK)
2651 res = queue->last_query;
2653 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2657 GST_DEBUG_OBJECT (queue,
2658 "refusing query, we are not using the queue");
2661 GST_QUEUE2_MUTEX_UNLOCK (queue);
2662 gst_queue2_post_buffering (queue);
2664 res = gst_pad_query_default (pad, parent, query);
2673 GST_DEBUG_OBJECT (queue, "refusing query, we are flushing");
2674 GST_QUEUE2_MUTEX_UNLOCK (queue);
2680 gst_queue2_is_empty (GstQueue2 * queue)
2682 /* never empty on EOS */
2686 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2687 return queue->current->writing_pos <= queue->current->max_reading_pos;
2689 if (queue->queue.length == 0)
2697 gst_queue2_is_filled (GstQueue2 * queue)
2701 /* always filled on EOS */
2705 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2706 (queue->cur_level.format) >= ((alt_max) ? \
2707 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2709 /* if using a ring buffer we're filled if all ring buffer space is used
2710 * _by the current range_ */
2711 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2712 guint64 rb_size = queue->ring_buffer_max_size;
2713 GST_DEBUG_OBJECT (queue,
2714 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2715 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2716 return CHECK_FILLED (bytes, rb_size);
2719 /* if using file, we're never filled if we don't have EOS */
2720 if (QUEUE_IS_USING_TEMP_FILE (queue))
2723 /* we are never filled when we have no buffers at all */
2724 if (queue->cur_level.buffers == 0)
2727 /* we are filled if one of the current levels exceeds the max */
2728 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2729 || CHECK_FILLED (time, 0);
2731 /* if we need to, use the rate estimate to check against the max time we are
2732 * allowed to queue */
2733 if (queue->use_rate_estimate)
2734 res |= CHECK_FILLED (rate_time, 0);
2740 static GstFlowReturn
2741 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2742 GstMiniObject * item, GstQueue2ItemType item_type)
2744 /* we have to lock the queue since we span threads */
2745 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2746 /* when we received EOS, we refuse more data */
2749 /* when we received unexpected from downstream, refuse more buffers */
2750 if (queue->unexpected)
2751 goto out_unexpected;
2753 /* while we didn't receive the newsegment, we're seeking and we skip data */
2757 if (!gst_queue2_wait_free_space (queue))
2760 /* put buffer in queue now */
2761 gst_queue2_locked_enqueue (queue, item, item_type);
2762 GST_QUEUE2_MUTEX_UNLOCK (queue);
2763 gst_queue2_post_buffering (queue);
2767 /* special conditions */
2770 GstFlowReturn ret = queue->sinkresult;
2772 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2773 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2774 GST_QUEUE2_MUTEX_UNLOCK (queue);
2775 gst_mini_object_unref (item);
2781 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2782 GST_QUEUE2_MUTEX_UNLOCK (queue);
2783 gst_mini_object_unref (item);
2785 return GST_FLOW_EOS;
2789 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2790 GST_QUEUE2_MUTEX_UNLOCK (queue);
2791 gst_mini_object_unref (item);
2797 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2798 GST_QUEUE2_MUTEX_UNLOCK (queue);
2799 gst_mini_object_unref (item);
2801 return GST_FLOW_EOS;
2805 static GstFlowReturn
2806 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2810 queue = GST_QUEUE2 (parent);
2812 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2813 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2814 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2815 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2816 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2818 return gst_queue2_chain_buffer_or_buffer_list (queue,
2819 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2822 static GstFlowReturn
2823 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2824 GstBufferList * buffer_list)
2828 queue = GST_QUEUE2 (parent);
2830 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2831 "received buffer list %p", buffer_list);
2833 return gst_queue2_chain_buffer_or_buffer_list (queue,
2834 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2837 static GstMiniObject *
2838 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2840 GstMiniObject *data;
2842 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2844 /* stop pushing buffers, we dequeue all items until we see an item that we
2845 * can push again, which is EOS or SEGMENT. If there is nothing in the
2846 * queue we can push, we set a flag to make the sinkpad refuse more
2847 * buffers with an EOS return value until we receive something
2848 * pushable again or we get flushed. */
2849 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2850 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2851 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2852 "dropping EOS buffer %p", data);
2853 gst_buffer_unref (GST_BUFFER_CAST (data));
2854 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2855 GstEvent *event = GST_EVENT_CAST (data);
2856 GstEventType type = GST_EVENT_TYPE (event);
2858 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2859 /* we found a pushable item in the queue, push it out */
2860 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2861 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2864 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2865 "dropping EOS event %p", event);
2866 gst_event_unref (event);
2867 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2868 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2869 "dropping EOS buffer list %p", data);
2870 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2871 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2872 queue->last_query = FALSE;
2873 g_cond_signal (&queue->query_handled);
2874 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
2877 /* no more items in the queue. Set the unexpected flag so that upstream
2878 * make us refuse any more buffers on the sinkpad. Since we will still
2879 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2880 * task function does not shut down. */
2881 queue->unexpected = TRUE;
2885 /* dequeue an item from the queue an push it downstream. This functions returns
2886 * the result of the push. */
2887 static GstFlowReturn
2888 gst_queue2_push_one (GstQueue2 * queue)
2890 GstFlowReturn result = queue->srcresult;
2891 GstMiniObject *data;
2892 GstQueue2ItemType item_type;
2894 data = gst_queue2_locked_dequeue (queue, &item_type);
2899 STATUS (queue, queue->srcpad, "We have something dequeud");
2900 g_atomic_int_set (&queue->downstream_may_block,
2901 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
2902 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2903 GST_QUEUE2_MUTEX_UNLOCK (queue);
2904 gst_queue2_post_buffering (queue);
2906 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2909 buffer = GST_BUFFER_CAST (data);
2911 result = gst_pad_push (queue->srcpad, buffer);
2912 g_atomic_int_set (&queue->downstream_may_block, 0);
2914 /* need to check for srcresult here as well */
2915 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2916 if (result == GST_FLOW_EOS) {
2917 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2920 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2921 * to the caller so that the task function does not shut down */
2922 result = GST_FLOW_OK;
2924 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2925 GstEvent *event = GST_EVENT_CAST (data);
2926 GstEventType type = GST_EVENT_TYPE (event);
2928 if (type == GST_EVENT_TAG) {
2929 if (queue->use_tags_bitrate) {
2933 gst_event_parse_tag (event, &tags);
2934 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2935 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2936 GST_QUEUE2_MUTEX_LOCK (queue);
2937 queue->src_tags_bitrate = bitrate;
2938 GST_QUEUE2_MUTEX_UNLOCK (queue);
2939 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
2944 gst_pad_push_event (queue->srcpad, event);
2946 /* if we're EOS, return EOS so that the task pauses. */
2947 if (type == GST_EVENT_EOS) {
2948 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2949 "pushed EOS event %p, return EOS", event);
2950 result = GST_FLOW_EOS;
2953 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2954 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2955 GstBufferList *buffer_list;
2957 buffer_list = GST_BUFFER_LIST_CAST (data);
2959 result = gst_pad_push_list (queue->srcpad, buffer_list);
2960 g_atomic_int_set (&queue->downstream_may_block, 0);
2962 /* need to check for srcresult here as well */
2963 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2964 if (result == GST_FLOW_EOS) {
2965 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2968 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2969 * to the caller so that the task function does not shut down */
2970 result = GST_FLOW_OK;
2972 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
2973 GstQuery *query = GST_QUERY_CAST (data);
2975 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
2976 queue->last_handled_query = query;
2977 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
2978 GST_LOG_OBJECT (queue->srcpad, "Peered query");
2979 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2980 "did query %p, return %d", query, queue->last_query);
2981 g_cond_signal (&queue->query_handled);
2982 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2983 result = GST_FLOW_OK;
2990 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2991 "exit because we have no item in the queue");
2992 return GST_FLOW_ERROR;
2996 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2997 return GST_FLOW_FLUSHING;
3001 /* called repeatedly with @pad as the source pad. This function should push out
3002 * data to the peer element. */
3004 gst_queue2_loop (GstPad * pad)
3009 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
3011 /* have to lock for thread-safety */
3012 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3014 if (gst_queue2_is_empty (queue)) {
3017 /* pause the timer while we wait. The fact that we are waiting does not mean
3018 * the byterate on the output pad is lower */
3019 if ((started = queue->out_timer_started))
3020 g_timer_stop (queue->out_timer);
3022 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
3023 "queue is empty, waiting for new data");
3025 /* Wait for data to be available, we could be unlocked because of a flush. */
3026 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
3028 while (gst_queue2_is_empty (queue));
3030 /* and continue if we were running before */
3032 g_timer_continue (queue->out_timer);
3034 ret = gst_queue2_push_one (queue);
3035 queue->srcresult = ret;
3036 queue->sinkresult = ret;
3037 if (ret != GST_FLOW_OK)
3040 GST_QUEUE2_MUTEX_UNLOCK (queue);
3041 gst_queue2_post_buffering (queue);
3048 gboolean eos = queue->is_eos;
3049 GstFlowReturn ret = queue->srcresult;
3051 gst_pad_pause_task (queue->srcpad);
3052 if (ret == GST_FLOW_FLUSHING) {
3053 gst_queue2_locked_flush (queue, FALSE, FALSE);
3055 GST_QUEUE2_SIGNAL_DEL (queue);
3056 queue->last_query = FALSE;
3057 g_cond_signal (&queue->query_handled);
3059 GST_QUEUE2_MUTEX_UNLOCK (queue);
3060 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3061 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
3062 /* let app know about us giving up if upstream is not expected to do so */
3063 /* EOS is already taken care of elsewhere */
3064 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
3065 GST_ELEMENT_FLOW_ERROR (queue, ret);
3066 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
3073 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
3075 gboolean res = TRUE;
3076 GstQueue2 *queue = GST_QUEUE2 (parent);
3078 #ifndef GST_DISABLE_GST_DEBUG
3079 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
3080 event, GST_EVENT_TYPE_NAME (event));
3083 switch (GST_EVENT_TYPE (event)) {
3084 case GST_EVENT_FLUSH_START:
3085 if (QUEUE_IS_USING_QUEUE (queue)) {
3086 /* just forward upstream */
3087 res = gst_pad_push_event (queue->sinkpad, event);
3089 /* now unblock the getrange function */
3090 GST_QUEUE2_MUTEX_LOCK (queue);
3091 GST_DEBUG_OBJECT (queue, "flushing");
3092 queue->srcresult = GST_FLOW_FLUSHING;
3093 GST_QUEUE2_SIGNAL_ADD (queue);
3094 GST_QUEUE2_MUTEX_UNLOCK (queue);
3096 /* when using a temp file, we eat the event */
3098 gst_event_unref (event);
3101 case GST_EVENT_FLUSH_STOP:
3102 if (QUEUE_IS_USING_QUEUE (queue)) {
3103 /* just forward upstream */
3104 res = gst_pad_push_event (queue->sinkpad, event);
3106 /* now unblock the getrange function */
3107 GST_QUEUE2_MUTEX_LOCK (queue);
3108 queue->srcresult = GST_FLOW_OK;
3109 GST_QUEUE2_MUTEX_UNLOCK (queue);
3111 /* when using a temp file, we eat the event */
3113 gst_event_unref (event);
3116 case GST_EVENT_RECONFIGURE:
3117 GST_QUEUE2_MUTEX_LOCK (queue);
3118 /* assume downstream is linked now and try to push again */
3119 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3120 queue->srcresult = GST_FLOW_OK;
3121 queue->sinkresult = GST_FLOW_OK;
3122 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3123 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3127 GST_QUEUE2_MUTEX_UNLOCK (queue);
3129 res = gst_pad_push_event (queue->sinkpad, event);
3132 res = gst_pad_push_event (queue->sinkpad, event);
3140 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3144 queue = GST_QUEUE2 (parent);
3146 switch (GST_QUERY_TYPE (query)) {
3147 case GST_QUERY_POSITION:
3152 if (!gst_pad_peer_query (queue->sinkpad, query))
3155 /* get peer position */
3156 gst_query_parse_position (query, &format, &peer_pos);
3158 /* FIXME: this code assumes that there's no discont in the queue */
3160 case GST_FORMAT_BYTES:
3161 peer_pos -= queue->cur_level.bytes;
3162 if (peer_pos < 0) /* Clamp result to 0 */
3165 case GST_FORMAT_TIME:
3166 peer_pos -= queue->cur_level.time;
3167 if (peer_pos < 0) /* Clamp result to 0 */
3171 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3172 "know how to adjust value", gst_format_get_name (format));
3175 /* set updated position */
3176 gst_query_set_position (query, format, peer_pos);
3179 case GST_QUERY_DURATION:
3181 GST_DEBUG_OBJECT (queue, "doing peer query");
3183 if (!gst_pad_peer_query (queue->sinkpad, query))
3186 GST_DEBUG_OBJECT (queue, "peer query success");
3189 case GST_QUERY_BUFFERING:
3192 gboolean is_buffering;
3193 GstBufferingMode mode;
3194 gint avg_in, avg_out;
3195 gint64 buffering_left;
3197 GST_DEBUG_OBJECT (queue, "query buffering");
3199 get_buffering_level (queue, &is_buffering, &percent);
3200 percent = convert_to_buffering_percent (queue, percent);
3201 gst_query_set_buffering_percent (query, is_buffering, percent);
3203 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3205 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3208 if (!QUEUE_IS_USING_QUEUE (queue)) {
3209 /* add ranges for download and ringbuffer buffering */
3211 gint64 start, stop, range_start, range_stop;
3212 guint64 writing_pos;
3213 gint64 estimated_total;
3215 gboolean peer_res, is_eos;
3216 GstQueue2Range *queued_ranges;
3218 /* we need a current download region */
3219 if (queue->current == NULL)
3222 writing_pos = queue->current->writing_pos;
3223 is_eos = queue->is_eos;
3226 /* we're EOS, we know the duration in bytes now */
3228 duration = writing_pos;
3230 /* get duration of upstream in bytes */
3231 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3232 GST_FORMAT_BYTES, &duration);
3235 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3236 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3238 /* calculate remaining and total download time */
3239 if (peer_res && avg_in > 0.0)
3240 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3242 estimated_total = -1;
3244 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3247 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3250 case GST_FORMAT_PERCENT:
3251 /* we need duration */
3256 /* get our available data relative to the duration */
3259 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3264 case GST_FORMAT_BYTES:
3274 /* fill out the buffered ranges */
3275 for (queued_ranges = queue->ranges; queued_ranges;
3276 queued_ranges = queued_ranges->next) {
3278 case GST_FORMAT_PERCENT:
3279 if (duration == -1) {
3285 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3286 queued_ranges->offset, duration);
3288 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3289 queued_ranges->writing_pos, duration);
3291 case GST_FORMAT_BYTES:
3292 range_start = queued_ranges->offset;
3293 range_stop = queued_ranges->writing_pos;
3300 if (range_start == range_stop)
3302 GST_DEBUG_OBJECT (queue,
3303 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3304 G_GINT64_FORMAT, range_start, range_stop);
3305 gst_query_add_buffering_range (query, range_start, range_stop);
3308 gst_query_set_buffering_range (query, format, start, stop,
3313 case GST_QUERY_SCHEDULING:
3316 GstSchedulingFlags flags = 0;
3318 if (!gst_pad_peer_query (queue->sinkpad, query))
3321 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3323 /* we can operate in pull mode when we are using a tempfile */
3324 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3327 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3328 gst_query_set_scheduling (query, flags, 0, -1, 0);
3330 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3331 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3335 /* peer handled other queries */
3336 if (!gst_pad_query_default (pad, parent, query))
3346 GST_DEBUG_OBJECT (queue, "failed peer query");
3352 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3354 GstQueue2 *queue = GST_QUEUE2 (element);
3356 /* simply forward to the srcpad query function */
3357 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3362 gst_queue2_update_upstream_size (GstQueue2 * queue)
3364 gint64 upstream_size = -1;
3366 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3368 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3370 /* upstream_size can be negative but queue->upstream_size is unsigned.
3371 * Prevent setting negative values to it (the query can return -1) */
3372 if (upstream_size >= 0)
3373 queue->upstream_size = upstream_size;
3375 queue->upstream_size = 0;
3379 static GstFlowReturn
3380 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3381 guint length, GstBuffer ** buffer)
3386 queue = GST_QUEUE2_CAST (parent);
3388 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3389 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3390 offset = (offset == -1) ? queue->current->reading_pos : offset;
3392 GST_DEBUG_OBJECT (queue,
3393 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3395 /* catch any reads beyond the size of the file here to make sure queue2
3396 * doesn't send seek events beyond the size of the file upstream, since
3397 * that would confuse elements such as souphttpsrc and/or http servers.
3398 * Demuxers often just loop until EOS at the end of the file to figure out
3399 * when they've read all the end-headers or index chunks. */
3400 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3401 gst_queue2_update_upstream_size (queue);
3402 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3403 goto out_unexpected;
3406 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3407 gst_queue2_update_upstream_size (queue);
3408 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3409 length = queue->upstream_size - offset;
3410 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3414 /* FIXME - function will block when the range is not yet available */
3415 ret = gst_queue2_create_read (queue, offset, length, buffer);
3416 GST_QUEUE2_MUTEX_UNLOCK (queue);
3417 gst_queue2_post_buffering (queue);
3424 ret = queue->srcresult;
3426 GST_DEBUG_OBJECT (queue, "we are flushing");
3427 GST_QUEUE2_MUTEX_UNLOCK (queue);
3432 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3433 GST_QUEUE2_MUTEX_UNLOCK (queue);
3434 return GST_FLOW_EOS;
3438 /* sink currently only operates in push mode */
3440 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3441 GstPadMode mode, gboolean active)
3446 queue = GST_QUEUE2 (parent);
3449 case GST_PAD_MODE_PUSH:
3451 GST_QUEUE2_MUTEX_LOCK (queue);
3452 GST_DEBUG_OBJECT (queue, "activating push mode");
3453 queue->srcresult = GST_FLOW_OK;
3454 queue->sinkresult = GST_FLOW_OK;
3455 queue->is_eos = FALSE;
3456 queue->unexpected = FALSE;
3457 reset_rate_timer (queue);
3458 GST_QUEUE2_MUTEX_UNLOCK (queue);
3460 /* unblock chain function */
3461 GST_QUEUE2_MUTEX_LOCK (queue);
3462 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3463 queue->srcresult = GST_FLOW_FLUSHING;
3464 queue->sinkresult = GST_FLOW_FLUSHING;
3465 GST_QUEUE2_SIGNAL_DEL (queue);
3466 GST_QUEUE2_MUTEX_UNLOCK (queue);
3468 /* wait until it is unblocked and clean up */
3469 GST_PAD_STREAM_LOCK (pad);
3470 GST_QUEUE2_MUTEX_LOCK (queue);
3471 gst_queue2_locked_flush (queue, TRUE, FALSE);
3472 GST_QUEUE2_MUTEX_UNLOCK (queue);
3473 GST_PAD_STREAM_UNLOCK (pad);
3484 /* src operating in push mode, we start a task on the source pad that pushes out
3485 * buffers from the queue */
3487 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3489 gboolean result = FALSE;
3492 queue = GST_QUEUE2 (parent);
3495 GST_QUEUE2_MUTEX_LOCK (queue);
3496 GST_DEBUG_OBJECT (queue, "activating push mode");
3497 queue->srcresult = GST_FLOW_OK;
3498 queue->sinkresult = GST_FLOW_OK;
3499 queue->is_eos = FALSE;
3500 queue->unexpected = FALSE;
3502 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3503 GST_QUEUE2_MUTEX_UNLOCK (queue);
3505 /* unblock loop function */
3506 GST_QUEUE2_MUTEX_LOCK (queue);
3507 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3508 queue->srcresult = GST_FLOW_FLUSHING;
3509 queue->sinkresult = GST_FLOW_FLUSHING;
3510 /* the item add signal will unblock */
3511 GST_QUEUE2_SIGNAL_ADD (queue);
3512 GST_QUEUE2_MUTEX_UNLOCK (queue);
3514 /* step 2, make sure streaming finishes */
3515 result = gst_pad_stop_task (pad);
3517 GST_QUEUE2_MUTEX_LOCK (queue);
3518 gst_queue2_locked_flush (queue, FALSE, FALSE);
3519 GST_QUEUE2_MUTEX_UNLOCK (queue);
3525 /* pull mode, downstream will call our getrange function */
3527 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3532 queue = GST_QUEUE2 (parent);
3535 GST_QUEUE2_MUTEX_LOCK (queue);
3536 if (!QUEUE_IS_USING_QUEUE (queue)) {
3537 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3538 /* open the temp file now */
3539 result = gst_queue2_open_temp_location_file (queue);
3540 } else if (!queue->ring_buffer) {
3541 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3542 result = ! !queue->ring_buffer;
3547 GST_DEBUG_OBJECT (queue, "activating pull mode");
3548 init_ranges (queue);
3549 queue->srcresult = GST_FLOW_OK;
3550 queue->sinkresult = GST_FLOW_OK;
3551 queue->is_eos = FALSE;
3552 queue->unexpected = FALSE;
3553 queue->upstream_size = 0;
3555 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3556 /* this is not allowed, we cannot operate in pull mode without a temp
3558 queue->srcresult = GST_FLOW_FLUSHING;
3559 queue->sinkresult = GST_FLOW_FLUSHING;
3562 GST_QUEUE2_MUTEX_UNLOCK (queue);
3564 GST_QUEUE2_MUTEX_LOCK (queue);
3565 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3566 queue->srcresult = GST_FLOW_FLUSHING;
3567 queue->sinkresult = GST_FLOW_FLUSHING;
3568 /* this will unlock getrange */
3569 GST_QUEUE2_SIGNAL_ADD (queue);
3571 GST_QUEUE2_MUTEX_UNLOCK (queue);
3578 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3584 case GST_PAD_MODE_PULL:
3585 res = gst_queue2_src_activate_pull (pad, parent, active);
3587 case GST_PAD_MODE_PUSH:
3588 res = gst_queue2_src_activate_push (pad, parent, active);
3591 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3598 static GstStateChangeReturn
3599 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3602 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3604 queue = GST_QUEUE2 (element);
3606 switch (transition) {
3607 case GST_STATE_CHANGE_NULL_TO_READY:
3609 case GST_STATE_CHANGE_READY_TO_PAUSED:
3610 GST_QUEUE2_MUTEX_LOCK (queue);
3611 if (!QUEUE_IS_USING_QUEUE (queue)) {
3612 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3613 if (!gst_queue2_open_temp_location_file (queue))
3614 ret = GST_STATE_CHANGE_FAILURE;
3616 if (queue->ring_buffer) {
3617 g_free (queue->ring_buffer);
3618 queue->ring_buffer = NULL;
3620 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3621 ret = GST_STATE_CHANGE_FAILURE;
3623 init_ranges (queue);
3625 queue->segment_event_received = FALSE;
3626 queue->starting_segment = NULL;
3627 gst_event_replace (&queue->stream_start_event, NULL);
3628 GST_QUEUE2_MUTEX_UNLOCK (queue);
3630 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3636 if (ret == GST_STATE_CHANGE_FAILURE)
3639 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3641 if (ret == GST_STATE_CHANGE_FAILURE)
3644 switch (transition) {
3645 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3647 case GST_STATE_CHANGE_PAUSED_TO_READY:
3648 GST_QUEUE2_MUTEX_LOCK (queue);
3649 if (!QUEUE_IS_USING_QUEUE (queue)) {
3650 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3651 gst_queue2_close_temp_location_file (queue);
3652 } else if (queue->ring_buffer) {
3653 g_free (queue->ring_buffer);
3654 queue->ring_buffer = NULL;
3656 clean_ranges (queue);
3658 if (queue->starting_segment != NULL) {
3659 gst_event_unref (queue->starting_segment);
3660 queue->starting_segment = NULL;
3662 gst_event_replace (&queue->stream_start_event, NULL);
3663 GST_QUEUE2_MUTEX_UNLOCK (queue);
3665 case GST_STATE_CHANGE_READY_TO_NULL:
3674 /* changing the capacity of the queue must wake up
3675 * the _chain function, it might have more room now
3676 * to store the buffer/event in the queue */
3677 #define QUEUE_CAPACITY_CHANGE(q) \
3678 GST_QUEUE2_SIGNAL_DEL (queue); \
3679 if (queue->use_buffering) \
3680 update_buffering (queue);
3682 /* Changing the minimum required fill level must
3683 * wake up the _loop function as it might now
3684 * be able to preceed.
3686 #define QUEUE_THRESHOLD_CHANGE(q)\
3687 GST_QUEUE2_SIGNAL_ADD (queue);
3690 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3694 /* the element must be stopped in order to do this */
3695 GST_OBJECT_LOCK (queue);
3696 state = GST_STATE (queue);
3697 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3699 GST_OBJECT_UNLOCK (queue);
3701 /* set new location */
3702 g_free (queue->temp_template);
3703 queue->temp_template = g_strdup (template);
3710 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3711 GST_OBJECT_UNLOCK (queue);
3716 gst_queue2_set_property (GObject * object,
3717 guint prop_id, const GValue * value, GParamSpec * pspec)
3719 GstQueue2 *queue = GST_QUEUE2 (object);
3721 /* someone could change levels here, and since this
3722 * affects the get/put funcs, we need to lock for safety. */
3723 GST_QUEUE2_MUTEX_LOCK (queue);
3726 case PROP_MAX_SIZE_BYTES:
3727 queue->max_level.bytes = g_value_get_uint (value);
3728 QUEUE_CAPACITY_CHANGE (queue);
3730 case PROP_MAX_SIZE_BUFFERS:
3731 queue->max_level.buffers = g_value_get_uint (value);
3732 QUEUE_CAPACITY_CHANGE (queue);
3734 case PROP_MAX_SIZE_TIME:
3735 queue->max_level.time = g_value_get_uint64 (value);
3736 /* set rate_time to the same value. We use an extra field in the level
3737 * structure so that we can easily access and compare it */
3738 queue->max_level.rate_time = queue->max_level.time;
3739 QUEUE_CAPACITY_CHANGE (queue);
3741 case PROP_USE_BUFFERING:
3742 queue->use_buffering = g_value_get_boolean (value);
3743 if (!queue->use_buffering && queue->is_buffering) {
3744 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3745 "posting 100%% message");
3746 SET_PERCENT (queue, 100);
3747 queue->is_buffering = FALSE;
3750 if (queue->use_buffering) {
3751 queue->is_buffering = TRUE;
3752 update_buffering (queue);
3755 case PROP_USE_TAGS_BITRATE:
3756 queue->use_tags_bitrate = g_value_get_boolean (value);
3758 case PROP_USE_RATE_ESTIMATE:
3759 queue->use_rate_estimate = g_value_get_boolean (value);
3761 case PROP_LOW_PERCENT:
3762 queue->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3763 if (queue->is_buffering)
3764 update_buffering (queue);
3766 case PROP_HIGH_PERCENT:
3767 queue->high_watermark =
3768 g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3769 if (queue->is_buffering)
3770 update_buffering (queue);
3772 case PROP_LOW_WATERMARK:
3773 queue->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3774 if (queue->is_buffering)
3775 update_buffering (queue);
3777 case PROP_HIGH_WATERMARK:
3778 queue->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3779 if (queue->is_buffering)
3780 update_buffering (queue);
3782 case PROP_TEMP_TEMPLATE:
3783 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3785 case PROP_TEMP_REMOVE:
3786 queue->temp_remove = g_value_get_boolean (value);
3788 case PROP_RING_BUFFER_MAX_SIZE:
3789 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3792 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3796 GST_QUEUE2_MUTEX_UNLOCK (queue);
3797 gst_queue2_post_buffering (queue);
3801 gst_queue2_get_property (GObject * object,
3802 guint prop_id, GValue * value, GParamSpec * pspec)
3804 GstQueue2 *queue = GST_QUEUE2 (object);
3806 GST_QUEUE2_MUTEX_LOCK (queue);
3809 case PROP_CUR_LEVEL_BYTES:
3810 g_value_set_uint (value, queue->cur_level.bytes);
3812 case PROP_CUR_LEVEL_BUFFERS:
3813 g_value_set_uint (value, queue->cur_level.buffers);
3815 case PROP_CUR_LEVEL_TIME:
3816 g_value_set_uint64 (value, queue->cur_level.time);
3818 case PROP_MAX_SIZE_BYTES:
3819 g_value_set_uint (value, queue->max_level.bytes);
3821 case PROP_MAX_SIZE_BUFFERS:
3822 g_value_set_uint (value, queue->max_level.buffers);
3824 case PROP_MAX_SIZE_TIME:
3825 g_value_set_uint64 (value, queue->max_level.time);
3827 case PROP_USE_BUFFERING:
3828 g_value_set_boolean (value, queue->use_buffering);
3830 case PROP_USE_TAGS_BITRATE:
3831 g_value_set_boolean (value, queue->use_tags_bitrate);
3833 case PROP_USE_RATE_ESTIMATE:
3834 g_value_set_boolean (value, queue->use_rate_estimate);
3836 case PROP_LOW_PERCENT:
3837 g_value_set_int (value, queue->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
3839 case PROP_HIGH_PERCENT:
3840 g_value_set_int (value, queue->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
3842 case PROP_LOW_WATERMARK:
3843 g_value_set_double (value, queue->low_watermark /
3844 (gdouble) MAX_BUFFERING_LEVEL);
3846 case PROP_HIGH_WATERMARK:
3847 g_value_set_double (value, queue->high_watermark /
3848 (gdouble) MAX_BUFFERING_LEVEL);
3850 case PROP_TEMP_TEMPLATE:
3851 g_value_set_string (value, queue->temp_template);
3853 case PROP_TEMP_LOCATION:
3854 g_value_set_string (value, queue->temp_location);
3856 case PROP_TEMP_REMOVE:
3857 g_value_set_boolean (value, queue->temp_remove);
3859 case PROP_RING_BUFFER_MAX_SIZE:
3860 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3862 case PROP_AVG_IN_RATE:
3864 gdouble in_rate = queue->byte_in_rate;
3866 /* During the first RATE_INTERVAL, byte_in_rate will not have been
3867 * calculated, so calculate it here. */
3868 if (in_rate == 0.0 && queue->bytes_in
3869 && queue->last_update_in_rates_elapsed > 0.0)
3870 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
3872 g_value_set_int64 (value, (gint64) in_rate);
3876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3880 GST_QUEUE2_MUTEX_UNLOCK (queue);