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., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, 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
52 * Last reviewed on 2009-07-10 (0.10.24)
59 #include "gstqueue2.h"
61 #include <glib/gstdio.h>
63 #include "gst/gst-i18n-lib.h"
64 #include "gst/glib-compat-private.h"
69 #include <io.h> /* lseek, open, close, read */
71 #define lseek _lseeki64
78 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
83 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
88 GST_DEBUG_CATEGORY_STATIC (queue_debug);
89 #define GST_CAT_DEFAULT (queue_debug)
90 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
98 #define DEFAULT_BUFFER_SIZE 4096
99 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
100 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
101 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
103 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
105 /* default property values */
106 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
107 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
108 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
109 #define DEFAULT_USE_BUFFERING FALSE
110 #define DEFAULT_USE_RATE_ESTIMATE TRUE
111 #define DEFAULT_LOW_PERCENT 10
112 #define DEFAULT_HIGH_PERCENT 99
113 #define DEFAULT_TEMP_REMOVE TRUE
114 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
119 PROP_CUR_LEVEL_BUFFERS,
120 PROP_CUR_LEVEL_BYTES,
122 PROP_MAX_SIZE_BUFFERS,
126 PROP_USE_RATE_ESTIMATE,
132 PROP_RING_BUFFER_MAX_SIZE,
136 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
143 #define STATUS(queue, pad, msg) \
144 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
145 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
146 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
147 " ns, %"G_GUINT64_FORMAT" items", \
148 GST_DEBUG_PAD_NAME (pad), \
149 queue->cur_level.buffers, \
150 queue->max_level.buffers, \
151 queue->cur_level.bytes, \
152 queue->max_level.bytes, \
153 queue->cur_level.time, \
154 queue->max_level.time, \
155 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
156 queue->current->writing_pos - queue->current->max_reading_pos : \
157 queue->queue.length))
159 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
160 g_mutex_lock (&q->qlock); \
163 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
164 GST_QUEUE2_MUTEX_LOCK (q); \
165 if (res != GST_FLOW_OK) \
169 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
170 g_mutex_unlock (&q->qlock); \
173 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
174 STATUS (queue, q->sinkpad, "wait for DEL"); \
175 q->waiting_del = TRUE; \
176 g_cond_wait (&q->item_del, &queue->qlock); \
177 q->waiting_del = FALSE; \
178 if (res != GST_FLOW_OK) { \
179 STATUS (queue, q->srcpad, "received DEL wakeup"); \
182 STATUS (queue, q->sinkpad, "received DEL"); \
185 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
186 STATUS (queue, q->srcpad, "wait for ADD"); \
187 q->waiting_add = TRUE; \
188 g_cond_wait (&q->item_add, &q->qlock); \
189 q->waiting_add = FALSE; \
190 if (res != GST_FLOW_OK) { \
191 STATUS (queue, q->srcpad, "received ADD wakeup"); \
194 STATUS (queue, q->srcpad, "received ADD"); \
197 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
198 if (q->waiting_del) { \
199 STATUS (q, q->srcpad, "signal DEL"); \
200 g_cond_signal (&q->item_del); \
204 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
205 if (q->waiting_add) { \
206 STATUS (q, q->sinkpad, "signal ADD"); \
207 g_cond_signal (&q->item_add); \
212 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
213 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
214 "dataflow inside the queue element");
215 #define gst_queue2_parent_class parent_class
216 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
218 static void gst_queue2_finalize (GObject * object);
220 static void gst_queue2_set_property (GObject * object,
221 guint prop_id, const GValue * value, GParamSpec * pspec);
222 static void gst_queue2_get_property (GObject * object,
223 guint prop_id, GValue * value, GParamSpec * pspec);
225 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
227 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
228 GstBufferList * buffer_list);
229 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
230 static void gst_queue2_loop (GstPad * pad);
232 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
234 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
237 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
239 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
241 static gboolean gst_queue2_handle_query (GstElement * element,
244 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
245 guint64 offset, guint length, GstBuffer ** buffer);
247 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
248 GstPadMode mode, gboolean active);
249 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
250 GstPadMode mode, gboolean active);
251 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
252 GstStateChange transition);
254 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
255 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
257 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
261 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
262 GST_QUEUE2_ITEM_TYPE_BUFFER,
263 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
264 GST_QUEUE2_ITEM_TYPE_EVENT
267 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
270 gst_queue2_class_init (GstQueue2Class * klass)
272 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
273 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
275 gobject_class->set_property = gst_queue2_set_property;
276 gobject_class->get_property = gst_queue2_get_property;
279 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
280 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
281 "Current amount of data in the queue (bytes)",
282 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
283 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
284 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
285 "Current number of buffers in the queue",
286 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
287 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
288 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
289 "Current amount of data in the queue (in ns)",
290 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
292 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
293 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
294 "Max. amount of data in the queue (bytes, 0=disable)",
295 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
296 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
298 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
299 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
300 DEFAULT_MAX_SIZE_BUFFERS,
301 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
303 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
304 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
305 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
308 g_param_spec_boolean ("use-buffering", "Use buffering",
309 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
310 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
312 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
313 "Estimate the bitrate of the stream to calculate time level",
314 DEFAULT_USE_RATE_ESTIMATE,
315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
317 g_param_spec_int ("low-percent", "Low percent",
318 "Low threshold for buffering to start. Only used if use-buffering is True",
319 0, 100, DEFAULT_LOW_PERCENT,
320 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
322 g_param_spec_int ("high-percent", "High percent",
323 "High threshold for buffering to finish. Only used if use-buffering is True",
324 0, 100, DEFAULT_HIGH_PERCENT,
325 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
328 g_param_spec_string ("temp-template", "Temporary File Template",
329 "File template to store temporary files in, should contain directory "
330 "and XXXXXX. (NULL == disabled)",
331 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
334 g_param_spec_string ("temp-location", "Temporary File Location",
335 "Location to store temporary files in (Only read this property, "
336 "use temp-template to configure the name template)",
337 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
340 * GstQueue2:temp-remove
342 * When temp-template is set, remove the temporary file when going to READY.
346 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
347 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
348 "Remove the temp-location after use",
349 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 * GstQueue2:ring-buffer-max-size
354 * The maximum size of the ring buffer in bytes. If set to 0, the ring
355 * buffer is disabled. Default 0.
359 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
360 g_param_spec_uint64 ("ring-buffer-max-size",
361 "Max. ring buffer size (bytes)",
362 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
363 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 /* set several parent class virtual functions */
367 gobject_class->finalize = gst_queue2_finalize;
369 gst_element_class_add_pad_template (gstelement_class,
370 gst_static_pad_template_get (&srctemplate));
371 gst_element_class_add_pad_template (gstelement_class,
372 gst_static_pad_template_get (&sinktemplate));
374 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
377 "Erik Walthinsen <omega@cse.ogi.edu>, "
378 "Wim Taymans <wim.taymans@gmail.com>");
380 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
381 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
385 gst_queue2_init (GstQueue2 * queue)
387 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
389 gst_pad_set_chain_function (queue->sinkpad,
390 GST_DEBUG_FUNCPTR (gst_queue2_chain));
391 gst_pad_set_chain_list_function (queue->sinkpad,
392 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
393 gst_pad_set_activatemode_function (queue->sinkpad,
394 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
395 gst_pad_set_event_function (queue->sinkpad,
396 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
397 gst_pad_set_query_function (queue->sinkpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
399 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
400 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
402 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
404 gst_pad_set_activatemode_function (queue->srcpad,
405 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
406 gst_pad_set_getrange_function (queue->srcpad,
407 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
408 gst_pad_set_event_function (queue->srcpad,
409 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
410 gst_pad_set_query_function (queue->srcpad,
411 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
412 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
413 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
416 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
417 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
418 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
419 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
420 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
421 queue->use_buffering = DEFAULT_USE_BUFFERING;
422 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
423 queue->low_percent = DEFAULT_LOW_PERCENT;
424 queue->high_percent = DEFAULT_HIGH_PERCENT;
426 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
427 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
429 queue->sinktime = GST_CLOCK_TIME_NONE;
430 queue->srctime = GST_CLOCK_TIME_NONE;
431 queue->sink_tainted = TRUE;
432 queue->src_tainted = TRUE;
434 queue->srcresult = GST_FLOW_FLUSHING;
435 queue->sinkresult = GST_FLOW_FLUSHING;
436 queue->is_eos = FALSE;
437 queue->in_timer = g_timer_new ();
438 queue->out_timer = g_timer_new ();
440 g_mutex_init (&queue->qlock);
441 queue->waiting_add = FALSE;
442 g_cond_init (&queue->item_add);
443 queue->waiting_del = FALSE;
444 g_cond_init (&queue->item_del);
445 g_queue_init (&queue->queue);
447 queue->buffering_percent = 100;
449 /* tempfile related */
450 queue->temp_template = NULL;
451 queue->temp_location = NULL;
452 queue->temp_remove = DEFAULT_TEMP_REMOVE;
454 queue->ring_buffer = NULL;
455 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
457 GST_DEBUG_OBJECT (queue,
458 "initialized queue's not_empty & not_full conditions");
461 /* called only once, as opposed to dispose */
463 gst_queue2_finalize (GObject * object)
465 GstQueue2 *queue = GST_QUEUE2 (object);
467 GST_DEBUG_OBJECT (queue, "finalizing queue");
469 while (!g_queue_is_empty (&queue->queue)) {
470 GstMiniObject *data = g_queue_pop_head (&queue->queue);
472 gst_mini_object_unref (data);
475 g_queue_clear (&queue->queue);
476 g_mutex_clear (&queue->qlock);
477 g_cond_clear (&queue->item_add);
478 g_cond_clear (&queue->item_del);
479 g_timer_destroy (queue->in_timer);
480 g_timer_destroy (queue->out_timer);
482 /* temp_file path cleanup */
483 g_free (queue->temp_template);
484 g_free (queue->temp_location);
486 G_OBJECT_CLASS (parent_class)->finalize (object);
490 debug_ranges (GstQueue2 * queue)
492 GstQueue2Range *walk;
494 for (walk = queue->ranges; walk; walk = walk->next) {
495 GST_DEBUG_OBJECT (queue,
496 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
497 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
498 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
499 walk->rb_writing_pos, walk->reading_pos,
500 walk == queue->current ? "**y**" : " n ");
504 /* clear all the downloaded ranges */
506 clean_ranges (GstQueue2 * queue)
508 GST_DEBUG_OBJECT (queue, "clean queue ranges");
510 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
511 queue->ranges = NULL;
512 queue->current = NULL;
515 /* find a range that contains @offset or NULL when nothing does */
516 static GstQueue2Range *
517 find_range (GstQueue2 * queue, guint64 offset)
519 GstQueue2Range *range = NULL;
520 GstQueue2Range *walk;
522 /* first do a quick check for the current range */
523 for (walk = queue->ranges; walk; walk = walk->next) {
524 if (offset >= walk->offset && offset <= walk->writing_pos) {
525 /* we can reuse an existing range */
531 GST_DEBUG_OBJECT (queue,
532 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
533 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
535 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
541 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
543 guint64 max_reading_pos, writing_pos;
545 writing_pos = range->writing_pos;
546 max_reading_pos = range->max_reading_pos;
548 if (writing_pos > max_reading_pos)
549 queue->cur_level.bytes = writing_pos - max_reading_pos;
551 queue->cur_level.bytes = 0;
554 /* make a new range for @offset or reuse an existing range */
555 static GstQueue2Range *
556 add_range (GstQueue2 * queue, guint64 offset)
558 GstQueue2Range *range, *prev, *next;
560 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
562 if ((range = find_range (queue, offset))) {
563 GST_DEBUG_OBJECT (queue,
564 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
566 range->writing_pos = offset;
568 GST_DEBUG_OBJECT (queue,
569 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
571 range = g_slice_new0 (GstQueue2Range);
572 range->offset = offset;
573 /* we want to write to the next location in the ring buffer */
574 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
575 range->writing_pos = offset;
576 range->rb_writing_pos = range->rb_offset;
577 range->reading_pos = offset;
578 range->max_reading_pos = offset;
582 next = queue->ranges;
584 if (next->offset > offset) {
585 /* insert before next */
586 GST_DEBUG_OBJECT (queue,
587 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
599 queue->ranges = range;
601 debug_ranges (queue);
603 /* update the stats for this range */
604 update_cur_level (queue, range);
610 /* clear and init the download ranges for offset 0 */
612 init_ranges (GstQueue2 * queue)
614 GST_DEBUG_OBJECT (queue, "init queue ranges");
616 /* get rid of all the current ranges */
617 clean_ranges (queue);
618 /* make a range for offset 0 */
619 queue->current = add_range (queue, 0);
622 /* calculate the diff between running time on the sink and src of the queue.
623 * This is the total amount of time in the queue. */
625 update_time_level (GstQueue2 * queue)
627 if (queue->sink_tainted) {
629 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
630 queue->sink_segment.position);
631 queue->sink_tainted = FALSE;
634 if (queue->src_tainted) {
636 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
637 queue->src_segment.position);
638 queue->src_tainted = FALSE;
641 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
642 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
644 if (queue->sinktime != GST_CLOCK_TIME_NONE
645 && queue->srctime != GST_CLOCK_TIME_NONE
646 && queue->sinktime >= queue->srctime)
647 queue->cur_level.time = queue->sinktime - queue->srctime;
649 queue->cur_level.time = 0;
652 /* take a SEGMENT event and apply the values to segment, updating the time
655 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
658 gst_event_copy_segment (event, segment);
660 if (segment->format == GST_FORMAT_BYTES) {
661 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
662 /* start is where we'll be getting from and as such writing next */
663 queue->current = add_range (queue, segment->start);
667 /* now configure the values, we use these to track timestamps on the
669 if (segment->format != GST_FORMAT_TIME) {
670 /* non-time format, pretent the current time segment is closed with a
671 * 0 start and unknown stop time. */
672 segment->format = GST_FORMAT_TIME;
678 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
681 queue->sink_tainted = TRUE;
683 queue->src_tainted = TRUE;
685 /* segment can update the time level of the queue */
686 update_time_level (queue);
689 /* take a buffer and update segment, updating the time level of the queue. */
691 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
694 GstClockTime duration, timestamp;
696 timestamp = GST_BUFFER_TIMESTAMP (buffer);
697 duration = GST_BUFFER_DURATION (buffer);
699 /* if no timestamp is set, assume it's continuous with the previous
701 if (timestamp == GST_CLOCK_TIME_NONE)
702 timestamp = segment->position;
705 if (duration != GST_CLOCK_TIME_NONE)
706 timestamp += duration;
708 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
709 GST_TIME_ARGS (timestamp));
711 segment->position = timestamp;
714 queue->sink_tainted = TRUE;
716 queue->src_tainted = TRUE;
718 /* calc diff with other end */
719 update_time_level (queue);
723 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
725 GstClockTime *timestamp = data;
727 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
728 " duration %" GST_TIME_FORMAT, idx,
729 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
730 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
732 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
733 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
735 if (GST_BUFFER_DURATION_IS_VALID (*buf))
736 *timestamp += GST_BUFFER_DURATION (*buf);
738 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
742 /* take a buffer list and update segment, updating the time level of the queue */
744 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
745 GstSegment * segment, gboolean is_sink)
747 GstClockTime timestamp;
749 /* if no timestamp is set, assume it's continuous with the previous time */
750 timestamp = segment->position;
752 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
754 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
755 GST_TIME_ARGS (timestamp));
757 segment->position = timestamp;
760 queue->sink_tainted = TRUE;
762 queue->src_tainted = TRUE;
764 /* calc diff with other end */
765 update_time_level (queue);
769 update_buffering (GstQueue2 * queue)
772 gboolean post = FALSE;
774 if (queue->high_percent <= 0)
777 #define GET_PERCENT(format,alt_max) ((queue->max_level.format) > 0 ? (queue->cur_level.format) * 100 / ((alt_max) > 0 ? MIN ((alt_max), (queue->max_level.format)) : (queue->max_level.format)) : 0)
780 /* on EOS we are always 100% full, we set the var here so that it we can
781 * reuse the logic below to stop buffering */
783 GST_LOG_OBJECT (queue, "we are EOS");
785 /* figure out the percent we are filled, we take the max of all formats. */
787 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
788 percent = GET_PERCENT (bytes, 0);
790 guint64 rb_size = queue->ring_buffer_max_size;
791 percent = GET_PERCENT (bytes, rb_size);
793 percent = MAX (percent, GET_PERCENT (time, 0));
794 percent = MAX (percent, GET_PERCENT (buffers, 0));
796 /* also apply the rate estimate when we need to */
797 if (queue->use_rate_estimate)
798 percent = MAX (percent, GET_PERCENT (rate_time, 0));
801 if (queue->is_buffering) {
803 /* if we were buffering see if we reached the high watermark */
804 if (percent >= queue->high_percent)
805 queue->is_buffering = FALSE;
807 /* we were not buffering, check if we need to start buffering if we drop
808 * below the low threshold */
809 if (percent < queue->low_percent) {
810 queue->is_buffering = TRUE;
811 queue->buffering_iteration++;
817 GstBufferingMode mode;
818 gint64 buffering_left = -1;
820 /* scale to high percent so that it becomes the 100% mark */
821 percent = percent * 100 / queue->high_percent;
826 if (percent != queue->buffering_percent) {
827 queue->buffering_percent = percent;
829 if (!QUEUE_IS_USING_QUEUE (queue)) {
832 if (QUEUE_IS_USING_RING_BUFFER (queue))
833 mode = GST_BUFFERING_TIMESHIFT;
835 mode = GST_BUFFERING_DOWNLOAD;
837 if (queue->byte_in_rate > 0) {
838 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
841 (gdouble) ((duration -
842 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
845 buffering_left = G_MAXINT64;
848 mode = GST_BUFFERING_STREAM;
851 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
852 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
854 gst_message_set_buffering_stats (message, mode,
855 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
857 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
860 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
867 reset_rate_timer (GstQueue2 * queue)
870 queue->bytes_out = 0;
871 queue->byte_in_rate = 0.0;
872 queue->byte_in_period = 0;
873 queue->byte_out_rate = 0.0;
874 queue->last_in_elapsed = 0.0;
875 queue->last_out_elapsed = 0.0;
876 queue->in_timer_started = FALSE;
877 queue->out_timer_started = FALSE;
880 /* the interval in seconds to recalculate the rate */
881 #define RATE_INTERVAL 0.2
882 /* Tuning for rate estimation. We use a large window for the input rate because
883 * it should be stable when connected to a network. The output rate is less
884 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
885 * therefore adapt more quickly.
886 * However, initial input rate may be subject to a burst, and should therefore
887 * initially also adapt more quickly to changes, and only later on give higher
888 * weight to previous values. */
889 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
890 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
893 update_in_rates (GstQueue2 * queue)
895 gdouble elapsed, period;
896 gdouble byte_in_rate;
898 if (!queue->in_timer_started) {
899 queue->in_timer_started = TRUE;
900 g_timer_start (queue->in_timer);
904 elapsed = g_timer_elapsed (queue->in_timer, NULL);
906 /* recalc after each interval. */
907 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
908 period = elapsed - queue->last_in_elapsed;
910 GST_DEBUG_OBJECT (queue,
911 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
912 period, queue->bytes_in, queue->byte_in_period);
914 byte_in_rate = queue->bytes_in / period;
916 if (queue->byte_in_rate == 0.0)
917 queue->byte_in_rate = byte_in_rate;
919 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
920 (double) queue->byte_in_period, period);
922 /* another data point, cap at 16 for long time running average */
923 if (queue->byte_in_period < 16 * RATE_INTERVAL)
924 queue->byte_in_period += period;
926 /* reset the values to calculate rate over the next interval */
927 queue->last_in_elapsed = elapsed;
931 if (queue->byte_in_rate > 0.0) {
932 queue->cur_level.rate_time =
933 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
935 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
936 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
940 update_out_rates (GstQueue2 * queue)
942 gdouble elapsed, period;
943 gdouble byte_out_rate;
945 if (!queue->out_timer_started) {
946 queue->out_timer_started = TRUE;
947 g_timer_start (queue->out_timer);
951 elapsed = g_timer_elapsed (queue->out_timer, NULL);
953 /* recalc after each interval. */
954 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
955 period = elapsed - queue->last_out_elapsed;
957 GST_DEBUG_OBJECT (queue,
958 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
960 byte_out_rate = queue->bytes_out / period;
962 if (queue->byte_out_rate == 0.0)
963 queue->byte_out_rate = byte_out_rate;
965 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
967 /* reset the values to calculate rate over the next interval */
968 queue->last_out_elapsed = elapsed;
969 queue->bytes_out = 0;
971 if (queue->byte_in_rate > 0.0) {
972 queue->cur_level.rate_time =
973 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
975 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
976 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
980 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
982 guint64 reading_pos, max_reading_pos;
985 max_reading_pos = range->max_reading_pos;
987 max_reading_pos = MAX (max_reading_pos, reading_pos);
989 GST_DEBUG_OBJECT (queue,
990 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
991 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
992 range->max_reading_pos = max_reading_pos;
994 update_cur_level (queue, range);
998 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1003 /* until we receive the FLUSH_STOP from this seek, we skip data */
1004 queue->seeking = TRUE;
1005 GST_QUEUE2_MUTEX_UNLOCK (queue);
1007 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1010 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1011 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1012 GST_SEEK_TYPE_NONE, -1);
1014 res = gst_pad_push_event (queue->sinkpad, event);
1015 GST_QUEUE2_MUTEX_LOCK (queue);
1018 queue->current = add_range (queue, offset);
1023 /* see if there is enough data in the file to read a full buffer */
1025 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1027 GstQueue2Range *range;
1029 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1032 if ((range = find_range (queue, offset))) {
1033 if (queue->current != range) {
1034 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1035 perform_seek_to_offset (queue, range->writing_pos);
1038 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1039 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1041 /* we have a range for offset */
1042 GST_DEBUG_OBJECT (queue,
1043 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1044 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1046 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1049 if (offset + length <= range->writing_pos)
1052 GST_DEBUG_OBJECT (queue,
1053 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1054 (offset + length) - range->writing_pos);
1057 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1058 " len %u", offset, length);
1059 /* we don't have the range, see how far away we are */
1060 if (!queue->is_eos && queue->current) {
1061 /* FIXME, find a good threshold based on the incoming rate. */
1062 guint64 threshold = 1024 * 512;
1064 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1067 distance = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1068 /* don't wait for the complete buffer to fill */
1069 distance = MIN (distance, threshold);
1071 if (offset >= queue->current->offset && offset <=
1072 queue->current->writing_pos + distance) {
1073 GST_INFO_OBJECT (queue,
1074 "requested data is within range, wait for data");
1077 } else if (offset < queue->current->writing_pos + threshold) {
1078 update_cur_pos (queue, queue->current, offset + length);
1079 GST_INFO_OBJECT (queue, "wait for data");
1084 /* too far away, do a seek */
1085 perform_seek_to_offset (queue, offset);
1092 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1093 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1094 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1096 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1099 static GstFlowReturn
1100 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1101 guint8 * dst, gint64 * read_return)
1103 guint8 *ring_buffer;
1106 ring_buffer = queue->ring_buffer;
1108 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1111 /* this should not block */
1112 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1114 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1115 res = fread (dst, 1, length, queue->temp_file);
1117 memcpy (dst, ring_buffer + offset, length);
1121 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1123 if (G_UNLIKELY (res < length)) {
1124 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1125 goto could_not_read;
1126 /* check for errors or EOF */
1127 if (ferror (queue->temp_file))
1128 goto could_not_read;
1129 if (feof (queue->temp_file) && length > 0)
1139 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1140 return GST_FLOW_ERROR;
1144 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1145 return GST_FLOW_ERROR;
1149 GST_DEBUG ("non-regular file hits EOS");
1150 return GST_FLOW_EOS;
1154 static GstFlowReturn
1155 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1156 GstBuffer ** buffer)
1161 guint64 file_offset;
1162 guint block_length, remaining, read_length;
1166 GstFlowReturn ret = GST_FLOW_OK;
1168 /* allocate the output buffer of the requested size */
1169 if (*buffer == NULL)
1170 buf = gst_buffer_new_allocate (NULL, length, NULL);
1174 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1177 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1181 rb_size = queue->ring_buffer_max_size;
1182 max_size = QUEUE_MAX_BYTES (queue);
1185 while (remaining > 0) {
1186 /* configure how much/whether to read */
1187 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1190 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1193 /* calculate how far away the offset is */
1194 if (queue->current->writing_pos > rpos)
1195 level = queue->current->writing_pos - rpos;
1199 GST_DEBUG_OBJECT (queue,
1200 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1201 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1202 rpos, queue->current->writing_pos, level, max_size);
1204 if (level >= max_size) {
1205 /* we don't have the data but if we have a ring buffer that is full, we
1207 GST_DEBUG_OBJECT (queue,
1208 "ring buffer full, reading QUEUE_MAX_BYTES %"
1209 G_GUINT64_FORMAT " bytes", max_size);
1210 read_length = max_size;
1211 } else if (queue->is_eos) {
1212 /* won't get any more data so read any data we have */
1214 GST_DEBUG_OBJECT (queue,
1215 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1217 read_length = level;
1225 if (read_length == 0) {
1226 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1227 GST_DEBUG_OBJECT (queue,
1228 "update current position [%" G_GUINT64_FORMAT "-%"
1229 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1230 update_cur_pos (queue, queue->current, rpos);
1231 GST_QUEUE2_SIGNAL_DEL (queue);
1233 GST_DEBUG_OBJECT (queue, "waiting for add");
1234 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1238 /* we have the requested data so read it */
1239 read_length = remaining;
1242 /* set range reading_pos to actual reading position for this read */
1243 queue->current->reading_pos = rpos;
1245 /* configure how much and from where to read */
1246 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1248 (queue->current->rb_offset + (rpos -
1249 queue->current->offset)) % rb_size;
1250 if (file_offset + read_length > rb_size) {
1251 block_length = rb_size - file_offset;
1253 block_length = read_length;
1257 block_length = read_length;
1260 /* while we still have data to read, we loop */
1261 while (read_length > 0) {
1265 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1266 data, &read_return);
1267 if (ret != GST_FLOW_OK)
1270 file_offset += read_return;
1271 if (QUEUE_IS_USING_RING_BUFFER (queue))
1272 file_offset %= rb_size;
1274 data += read_return;
1275 read_length -= read_return;
1276 block_length = read_length;
1277 remaining -= read_return;
1279 rpos = (queue->current->reading_pos += read_return);
1280 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1282 GST_QUEUE2_SIGNAL_DEL (queue);
1283 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1286 gst_buffer_unmap (buf, &info);
1287 gst_buffer_resize (buf, 0, length);
1289 GST_BUFFER_OFFSET (buf) = offset;
1290 GST_BUFFER_OFFSET_END (buf) = offset + length;
1299 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1300 gst_buffer_unmap (buf, &info);
1301 if (*buffer == NULL)
1302 gst_buffer_unref (buf);
1303 return GST_FLOW_EOS;
1307 GST_DEBUG_OBJECT (queue, "we are flushing");
1308 gst_buffer_unmap (buf, &info);
1309 if (*buffer == NULL)
1310 gst_buffer_unref (buf);
1311 return GST_FLOW_FLUSHING;
1315 GST_DEBUG_OBJECT (queue, "we have a read error");
1316 gst_buffer_unmap (buf, &info);
1317 if (*buffer == NULL)
1318 gst_buffer_unref (buf);
1323 /* should be called with QUEUE_LOCK */
1324 static GstMiniObject *
1325 gst_queue2_read_item_from_file (GstQueue2 * queue)
1327 GstMiniObject *item;
1329 if (queue->stream_start_event != NULL) {
1330 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1331 queue->stream_start_event = NULL;
1332 } else if (queue->starting_segment != NULL) {
1333 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1334 queue->starting_segment = NULL;
1337 GstBuffer *buffer = NULL;
1338 guint64 reading_pos;
1340 reading_pos = queue->current->reading_pos;
1343 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1348 item = GST_MINI_OBJECT_CAST (buffer);
1351 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1361 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1362 * the temp filename. */
1364 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1369 if (queue->temp_file)
1370 goto already_opened;
1372 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1374 /* If temp_template was set, allocate a filename and open that filen */
1377 if (queue->temp_template == NULL)
1380 /* make copy of the template, we don't want to change this */
1381 name = g_strdup (queue->temp_template);
1382 fd = g_mkstemp (name);
1384 goto mkstemp_failed;
1386 /* open the file for update/writing */
1387 queue->temp_file = fdopen (fd, "wb+");
1388 /* error creating file */
1389 if (queue->temp_file == NULL)
1392 g_free (queue->temp_location);
1393 queue->temp_location = name;
1395 GST_QUEUE2_MUTEX_UNLOCK (queue);
1397 /* we can't emit the notify with the lock */
1398 g_object_notify (G_OBJECT (queue), "temp-location");
1400 GST_QUEUE2_MUTEX_LOCK (queue);
1402 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1409 GST_DEBUG_OBJECT (queue, "temp file was already open");
1414 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1415 (_("No Temp directory specified.")), (NULL));
1420 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1421 (_("Could not create temp file \"%s\"."), queue->temp_template),
1428 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1429 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1438 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1441 if (queue->temp_file == NULL)
1444 GST_DEBUG_OBJECT (queue, "closing temp file");
1446 fflush (queue->temp_file);
1447 fclose (queue->temp_file);
1449 if (queue->temp_remove)
1450 remove (queue->temp_location);
1452 queue->temp_file = NULL;
1453 clean_ranges (queue);
1457 gst_queue2_flush_temp_file (GstQueue2 * queue)
1459 if (queue->temp_file == NULL)
1462 GST_DEBUG_OBJECT (queue, "flushing temp file");
1464 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1468 gst_queue2_locked_flush (GstQueue2 * queue)
1470 if (!QUEUE_IS_USING_QUEUE (queue)) {
1471 if (QUEUE_IS_USING_TEMP_FILE (queue))
1472 gst_queue2_flush_temp_file (queue);
1473 init_ranges (queue);
1475 while (!g_queue_is_empty (&queue->queue)) {
1476 GstMiniObject *data = g_queue_pop_head (&queue->queue);
1478 /* Then lose another reference because we are supposed to destroy that
1479 data when flushing */
1480 gst_mini_object_unref (data);
1483 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1484 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1485 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1486 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1487 queue->sink_tainted = queue->src_tainted = TRUE;
1488 if (queue->starting_segment != NULL)
1489 gst_event_unref (queue->starting_segment);
1490 queue->starting_segment = NULL;
1491 queue->segment_event_received = FALSE;
1492 gst_event_replace (&queue->stream_start_event, NULL);
1494 /* we deleted a lot of something */
1495 GST_QUEUE2_SIGNAL_DEL (queue);
1499 gst_queue2_wait_free_space (GstQueue2 * queue)
1501 /* We make space available if we're "full" according to whatever
1502 * the user defined as "full". */
1503 if (gst_queue2_is_filled (queue)) {
1506 /* pause the timer while we wait. The fact that we are waiting does not mean
1507 * the byterate on the input pad is lower */
1508 if ((started = queue->in_timer_started))
1509 g_timer_stop (queue->in_timer);
1511 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1512 "queue is full, waiting for free space");
1514 /* Wait for space to be available, we could be unlocked because of a flush. */
1515 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1517 while (gst_queue2_is_filled (queue));
1519 /* and continue if we were running before */
1521 g_timer_continue (queue->in_timer);
1528 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1534 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1537 guint8 *data, *ring_buffer;
1538 guint size, rb_size;
1539 guint64 writing_pos, new_writing_pos;
1540 GstQueue2Range *range, *prev, *next;
1542 if (QUEUE_IS_USING_RING_BUFFER (queue))
1543 writing_pos = queue->current->rb_writing_pos;
1545 writing_pos = queue->current->writing_pos;
1546 ring_buffer = queue->ring_buffer;
1547 rb_size = queue->ring_buffer_max_size;
1549 gst_buffer_map (buffer, &info, GST_MAP_READ);
1554 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1560 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1563 /* calculate the space in the ring buffer not used by data from
1564 * the current range */
1565 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1566 /* wait until there is some free space */
1567 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1569 /* get the amount of space we have */
1570 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1572 /* calculate if we need to split or if we can write the entire
1574 to_write = MIN (size, space);
1576 /* the writing position in the ring buffer after writing (part
1577 * or all of) the buffer */
1578 new_writing_pos = (writing_pos + to_write) % rb_size;
1581 range = queue->ranges;
1583 /* if we need to overwrite data in the ring buffer, we need to
1586 * warning: this code is complicated and includes some
1587 * simplifications - pen, paper and diagrams for the cases
1590 guint64 range_data_start, range_data_end;
1591 GstQueue2Range *range_to_destroy = NULL;
1593 range_data_start = range->rb_offset;
1594 range_data_end = range->rb_writing_pos;
1596 /* handle the special case where the range has no data in it */
1597 if (range->writing_pos == range->offset) {
1598 if (range != queue->current) {
1599 GST_DEBUG_OBJECT (queue,
1600 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1601 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1603 range_to_destroy = range;
1605 prev->next = range->next;
1610 if (range_data_end > range_data_start) {
1611 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1614 if (new_writing_pos > range_data_start) {
1615 if (new_writing_pos >= range_data_end) {
1616 GST_DEBUG_OBJECT (queue,
1617 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1618 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1620 range_to_destroy = range;
1622 prev->next = range->next;
1624 GST_DEBUG_OBJECT (queue,
1625 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1626 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1627 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1628 range->offset + new_writing_pos - range_data_start,
1630 range->offset += (new_writing_pos - range_data_start);
1631 range->rb_offset = new_writing_pos;
1635 guint64 new_wpos_virt = writing_pos + to_write;
1637 if (new_wpos_virt <= range_data_start)
1640 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1641 GST_DEBUG_OBJECT (queue,
1642 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1643 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1645 range_to_destroy = range;
1647 prev->next = range->next;
1649 GST_DEBUG_OBJECT (queue,
1650 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1651 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1652 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1653 range->offset + new_writing_pos - range_data_start,
1655 range->offset += (new_wpos_virt - range_data_start);
1656 range->rb_offset = new_writing_pos;
1661 if (!range_to_destroy)
1664 range = range->next;
1665 if (range_to_destroy) {
1666 if (range_to_destroy == queue->ranges)
1667 queue->ranges = range;
1668 g_slice_free (GstQueue2Range, range_to_destroy);
1669 range_to_destroy = NULL;
1674 new_writing_pos = writing_pos + to_write;
1677 if (QUEUE_IS_USING_TEMP_FILE (queue)
1678 && FSEEK_FILE (queue->temp_file, writing_pos))
1681 if (new_writing_pos > writing_pos) {
1682 GST_INFO_OBJECT (queue,
1683 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1684 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1685 queue->current->writing_pos, queue->current->rb_writing_pos);
1686 /* either not using ring buffer or no wrapping, just write */
1687 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1688 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1691 memcpy (ring_buffer + writing_pos, data, to_write);
1694 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1695 /* try to merge with next range */
1696 while ((next = queue->current->next)) {
1697 GST_INFO_OBJECT (queue,
1698 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1699 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1700 if (new_writing_pos < next->offset)
1703 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1706 /* remove the group, we could choose to not read the data in this range
1707 * again. This would involve us doing a seek to the current writing position
1708 * in the range. FIXME, It would probably make sense to do a seek when there
1709 * is a lot of data in the range we merged with to avoid reading it all
1711 queue->current->next = next->next;
1712 g_slice_free (GstQueue2Range, next);
1714 debug_ranges (queue);
1716 goto update_and_signal;
1720 guint block_one, block_two;
1722 block_one = rb_size - writing_pos;
1723 block_two = to_write - block_one;
1725 if (block_one > 0) {
1726 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1727 /* write data to end of ring buffer */
1728 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1729 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1732 memcpy (ring_buffer + writing_pos, data, block_one);
1736 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1739 if (block_two > 0) {
1740 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1741 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1742 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1745 memcpy (ring_buffer, data + block_one, block_two);
1751 /* update the writing positions */
1753 GST_INFO_OBJECT (queue,
1754 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1755 to_write, writing_pos, size);
1757 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1759 queue->current->writing_pos += to_write;
1760 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1762 queue->current->writing_pos = writing_pos = new_writing_pos;
1764 update_cur_level (queue, queue->current);
1766 /* update the buffering status */
1767 if (queue->use_buffering)
1768 update_buffering (queue);
1770 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1771 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1773 GST_QUEUE2_SIGNAL_ADD (queue);
1776 gst_buffer_unmap (buffer, &info);
1783 GST_DEBUG_OBJECT (queue, "we are flushing");
1784 gst_buffer_unmap (buffer, &info);
1785 /* FIXME - GST_FLOW_EOS ? */
1790 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1791 gst_buffer_unmap (buffer, &info);
1798 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1802 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1803 (_("Error while writing to download file.")),
1804 ("%s", g_strerror (errno)));
1807 gst_buffer_unmap (buffer, &info);
1813 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1815 GstQueue2 *queue = q;
1817 GST_TRACE_OBJECT (queue,
1818 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1819 gst_buffer_get_size (*buf));
1821 if (!gst_queue2_create_write (queue, *buf)) {
1822 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1829 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1831 guint *p_size = data;
1834 buf_size = gst_buffer_get_size (*buf);
1835 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1836 *p_size += buf_size;
1840 /* enqueue an item an update the level stats */
1842 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1843 GstQueue2ItemType item_type)
1845 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1849 buffer = GST_BUFFER_CAST (item);
1850 size = gst_buffer_get_size (buffer);
1852 /* add buffer to the statistics */
1853 if (QUEUE_IS_USING_QUEUE (queue)) {
1854 queue->cur_level.buffers++;
1855 queue->cur_level.bytes += size;
1857 queue->bytes_in += size;
1859 /* apply new buffer to segment stats */
1860 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1861 /* update the byterate stats */
1862 update_in_rates (queue);
1864 if (!QUEUE_IS_USING_QUEUE (queue)) {
1865 /* FIXME - check return value? */
1866 gst_queue2_create_write (queue, buffer);
1868 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1869 GstBufferList *buffer_list;
1872 buffer_list = GST_BUFFER_LIST_CAST (item);
1874 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1875 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1877 /* add buffer to the statistics */
1878 if (QUEUE_IS_USING_QUEUE (queue)) {
1879 queue->cur_level.buffers++;
1880 queue->cur_level.bytes += size;
1882 queue->bytes_in += size;
1884 /* apply new buffer to segment stats */
1885 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1887 /* update the byterate stats */
1888 update_in_rates (queue);
1890 if (!QUEUE_IS_USING_QUEUE (queue)) {
1891 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1893 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
1896 event = GST_EVENT_CAST (item);
1898 switch (GST_EVENT_TYPE (event)) {
1900 /* Zero the thresholds, this makes sure the queue is completely
1901 * filled and we can read all data from the queue. */
1902 GST_DEBUG_OBJECT (queue, "we have EOS");
1903 queue->is_eos = TRUE;
1905 case GST_EVENT_SEGMENT:
1906 apply_segment (queue, event, &queue->sink_segment, TRUE);
1907 /* This is our first new segment, we hold it
1908 * as we can't save it on the temp file */
1909 if (!QUEUE_IS_USING_QUEUE (queue)) {
1910 if (queue->segment_event_received)
1911 goto unexpected_event;
1913 queue->segment_event_received = TRUE;
1914 if (queue->starting_segment != NULL)
1915 gst_event_unref (queue->starting_segment);
1916 queue->starting_segment = event;
1919 /* a new segment allows us to accept more buffers if we got EOS
1920 * from downstream */
1921 queue->unexpected = FALSE;
1923 case GST_EVENT_STREAM_START:
1924 if (!QUEUE_IS_USING_QUEUE (queue)) {
1925 gst_event_replace (&queue->stream_start_event, event);
1926 gst_event_unref (event);
1931 if (!QUEUE_IS_USING_QUEUE (queue))
1932 goto unexpected_event;
1936 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1937 item, GST_OBJECT_NAME (queue));
1938 /* we can't really unref since we don't know what it is */
1943 /* update the buffering status */
1944 if (queue->use_buffering)
1945 update_buffering (queue);
1947 if (QUEUE_IS_USING_QUEUE (queue)) {
1948 g_queue_push_tail (&queue->queue, item);
1950 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
1953 GST_QUEUE2_SIGNAL_ADD (queue);
1962 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
1963 gst_event_type_get_name (GST_EVENT_TYPE (item)),
1964 GST_OBJECT_NAME (queue));
1965 gst_event_unref (GST_EVENT_CAST (item));
1970 /* dequeue an item from the queue and update level stats */
1971 static GstMiniObject *
1972 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
1974 GstMiniObject *item;
1976 if (!QUEUE_IS_USING_QUEUE (queue))
1977 item = gst_queue2_read_item_from_file (queue);
1979 item = g_queue_pop_head (&queue->queue);
1984 if (GST_IS_BUFFER (item)) {
1988 buffer = GST_BUFFER_CAST (item);
1989 size = gst_buffer_get_size (buffer);
1990 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
1992 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1993 "retrieved buffer %p from queue", buffer);
1995 if (QUEUE_IS_USING_QUEUE (queue)) {
1996 queue->cur_level.buffers--;
1997 queue->cur_level.bytes -= size;
1999 queue->bytes_out += size;
2001 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2002 /* update the byterate stats */
2003 update_out_rates (queue);
2004 /* update the buffering */
2005 if (queue->use_buffering)
2006 update_buffering (queue);
2008 } else if (GST_IS_EVENT (item)) {
2009 GstEvent *event = GST_EVENT_CAST (item);
2011 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2013 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2014 "retrieved event %p from queue", event);
2016 switch (GST_EVENT_TYPE (event)) {
2018 /* queue is empty now that we dequeued the EOS */
2019 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2021 case GST_EVENT_SEGMENT:
2022 apply_segment (queue, event, &queue->src_segment, FALSE);
2027 } else if (GST_IS_BUFFER_LIST (item)) {
2028 GstBufferList *buffer_list;
2031 buffer_list = GST_BUFFER_LIST_CAST (item);
2032 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2033 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2035 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2036 "retrieved buffer list %p from queue", buffer_list);
2038 if (QUEUE_IS_USING_QUEUE (queue)) {
2039 queue->cur_level.buffers--;
2040 queue->cur_level.bytes -= size;
2042 queue->bytes_out += size;
2044 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2045 /* update the byterate stats */
2046 update_out_rates (queue);
2047 /* update the buffering */
2048 if (queue->use_buffering)
2049 update_buffering (queue);
2053 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2054 item, GST_OBJECT_NAME (queue));
2056 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2058 GST_QUEUE2_SIGNAL_DEL (queue);
2065 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2071 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2076 queue = GST_QUEUE2 (parent);
2078 switch (GST_EVENT_TYPE (event)) {
2079 case GST_EVENT_FLUSH_START:
2081 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2082 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2084 gst_pad_push_event (queue->srcpad, event);
2086 /* now unblock the chain function */
2087 GST_QUEUE2_MUTEX_LOCK (queue);
2088 queue->srcresult = GST_FLOW_FLUSHING;
2089 queue->sinkresult = GST_FLOW_FLUSHING;
2090 /* unblock the loop and chain functions */
2091 GST_QUEUE2_SIGNAL_ADD (queue);
2092 GST_QUEUE2_SIGNAL_DEL (queue);
2093 GST_QUEUE2_MUTEX_UNLOCK (queue);
2095 /* make sure it pauses, this should happen since we sent
2096 * flush_start downstream. */
2097 gst_pad_pause_task (queue->srcpad);
2098 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2100 GST_QUEUE2_MUTEX_LOCK (queue);
2101 /* flush the sink pad */
2102 queue->sinkresult = GST_FLOW_FLUSHING;
2103 GST_QUEUE2_SIGNAL_DEL (queue);
2104 GST_QUEUE2_MUTEX_UNLOCK (queue);
2106 gst_event_unref (event);
2110 case GST_EVENT_FLUSH_STOP:
2112 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2114 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2116 gst_pad_push_event (queue->srcpad, event);
2118 GST_QUEUE2_MUTEX_LOCK (queue);
2119 gst_queue2_locked_flush (queue);
2120 queue->srcresult = GST_FLOW_OK;
2121 queue->sinkresult = GST_FLOW_OK;
2122 queue->is_eos = FALSE;
2123 queue->unexpected = FALSE;
2124 queue->seeking = FALSE;
2125 /* reset rate counters */
2126 reset_rate_timer (queue);
2127 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2128 queue->srcpad, NULL);
2129 GST_QUEUE2_MUTEX_UNLOCK (queue);
2131 GST_QUEUE2_MUTEX_LOCK (queue);
2132 queue->segment_event_received = FALSE;
2133 queue->is_eos = FALSE;
2134 queue->unexpected = FALSE;
2135 queue->sinkresult = GST_FLOW_OK;
2136 queue->seeking = FALSE;
2137 GST_QUEUE2_MUTEX_UNLOCK (queue);
2139 gst_event_unref (event);
2144 if (GST_EVENT_IS_SERIALIZED (event)) {
2145 /* serialized events go in the queue */
2146 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2147 /* refuse more events on EOS */
2150 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2151 GST_QUEUE2_MUTEX_UNLOCK (queue);
2153 /* non-serialized events are passed upstream. */
2154 gst_pad_push_event (queue->srcpad, event);
2164 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2165 GST_QUEUE2_MUTEX_UNLOCK (queue);
2166 gst_event_unref (event);
2171 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2172 GST_QUEUE2_MUTEX_UNLOCK (queue);
2173 gst_event_unref (event);
2179 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2184 switch (GST_QUERY_TYPE (query)) {
2186 if (GST_QUERY_IS_SERIALIZED (query)) {
2187 GST_WARNING_OBJECT (pad, "unhandled serialized query");
2190 res = gst_pad_query_default (pad, parent, query);
2198 gst_queue2_is_empty (GstQueue2 * queue)
2200 /* never empty on EOS */
2204 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2205 return queue->current->writing_pos <= queue->current->max_reading_pos;
2207 if (queue->queue.length == 0)
2215 gst_queue2_is_filled (GstQueue2 * queue)
2219 /* always filled on EOS */
2223 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2224 (queue->cur_level.format) >= ((alt_max) ? \
2225 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2227 /* if using a ring buffer we're filled if all ring buffer space is used
2228 * _by the current range_ */
2229 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2230 guint64 rb_size = queue->ring_buffer_max_size;
2231 GST_DEBUG_OBJECT (queue,
2232 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2233 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2234 return CHECK_FILLED (bytes, rb_size);
2237 /* if using file, we're never filled if we don't have EOS */
2238 if (QUEUE_IS_USING_TEMP_FILE (queue))
2241 /* we are never filled when we have no buffers at all */
2242 if (queue->cur_level.buffers == 0)
2245 /* we are filled if one of the current levels exceeds the max */
2246 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2247 || CHECK_FILLED (time, 0);
2249 /* if we need to, use the rate estimate to check against the max time we are
2250 * allowed to queue */
2251 if (queue->use_rate_estimate)
2252 res |= CHECK_FILLED (rate_time, 0);
2258 static GstFlowReturn
2259 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2260 GstMiniObject * item, GstQueue2ItemType item_type)
2262 /* we have to lock the queue since we span threads */
2263 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2264 /* when we received EOS, we refuse more data */
2267 /* when we received unexpected from downstream, refuse more buffers */
2268 if (queue->unexpected)
2269 goto out_unexpected;
2271 /* while we didn't receive the newsegment, we're seeking and we skip data */
2275 if (!gst_queue2_wait_free_space (queue))
2278 /* put buffer in queue now */
2279 gst_queue2_locked_enqueue (queue, item, item_type);
2280 GST_QUEUE2_MUTEX_UNLOCK (queue);
2284 /* special conditions */
2287 GstFlowReturn ret = queue->sinkresult;
2289 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2290 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2291 GST_QUEUE2_MUTEX_UNLOCK (queue);
2292 gst_mini_object_unref (item);
2298 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2299 GST_QUEUE2_MUTEX_UNLOCK (queue);
2300 gst_mini_object_unref (item);
2302 return GST_FLOW_EOS;
2306 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2307 GST_QUEUE2_MUTEX_UNLOCK (queue);
2308 gst_mini_object_unref (item);
2314 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2315 GST_QUEUE2_MUTEX_UNLOCK (queue);
2316 gst_mini_object_unref (item);
2318 return GST_FLOW_EOS;
2322 static GstFlowReturn
2323 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2327 queue = GST_QUEUE2 (parent);
2329 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2330 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2331 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2332 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2333 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2335 return gst_queue2_chain_buffer_or_buffer_list (queue,
2336 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2339 static GstFlowReturn
2340 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2341 GstBufferList * buffer_list)
2345 queue = GST_QUEUE2 (parent);
2347 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2348 "received buffer list %p", buffer_list);
2350 return gst_queue2_chain_buffer_or_buffer_list (queue,
2351 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2354 static GstMiniObject *
2355 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2357 GstMiniObject *data;
2359 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2361 /* stop pushing buffers, we dequeue all items until we see an item that we
2362 * can push again, which is EOS or SEGMENT. If there is nothing in the
2363 * queue we can push, we set a flag to make the sinkpad refuse more
2364 * buffers with an EOS return value until we receive something
2365 * pushable again or we get flushed. */
2366 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2367 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2368 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2369 "dropping EOS buffer %p", data);
2370 gst_buffer_unref (GST_BUFFER_CAST (data));
2371 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2372 GstEvent *event = GST_EVENT_CAST (data);
2373 GstEventType type = GST_EVENT_TYPE (event);
2375 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2376 /* we found a pushable item in the queue, push it out */
2377 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2378 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2381 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2382 "dropping EOS event %p", event);
2383 gst_event_unref (event);
2384 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2385 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2386 "dropping EOS buffer list %p", data);
2387 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2390 /* no more items in the queue. Set the unexpected flag so that upstream
2391 * make us refuse any more buffers on the sinkpad. Since we will still
2392 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2393 * task function does not shut down. */
2394 queue->unexpected = TRUE;
2398 /* dequeue an item from the queue an push it downstream. This functions returns
2399 * the result of the push. */
2400 static GstFlowReturn
2401 gst_queue2_push_one (GstQueue2 * queue)
2403 GstFlowReturn result = GST_FLOW_OK;
2404 GstMiniObject *data;
2405 GstQueue2ItemType item_type;
2407 data = gst_queue2_locked_dequeue (queue, &item_type);
2412 GST_QUEUE2_MUTEX_UNLOCK (queue);
2414 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2417 buffer = GST_BUFFER_CAST (data);
2419 result = gst_pad_push (queue->srcpad, buffer);
2421 /* need to check for srcresult here as well */
2422 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2423 if (result == GST_FLOW_EOS) {
2424 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2427 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2428 * to the caller so that the task function does not shut down */
2429 result = GST_FLOW_OK;
2431 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2432 GstEvent *event = GST_EVENT_CAST (data);
2433 GstEventType type = GST_EVENT_TYPE (event);
2435 gst_pad_push_event (queue->srcpad, event);
2437 /* if we're EOS, return EOS so that the task pauses. */
2438 if (type == GST_EVENT_EOS) {
2439 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2440 "pushed EOS event %p, return EOS", event);
2441 result = GST_FLOW_EOS;
2444 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2445 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2446 GstBufferList *buffer_list;
2448 buffer_list = GST_BUFFER_LIST_CAST (data);
2450 result = gst_pad_push_list (queue->srcpad, buffer_list);
2452 /* need to check for srcresult here as well */
2453 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2454 if (result == GST_FLOW_EOS) {
2455 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2458 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2459 * to the caller so that the task function does not shut down */
2460 result = GST_FLOW_OK;
2468 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2469 "exit because we have no item in the queue");
2470 return GST_FLOW_ERROR;
2474 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2475 return GST_FLOW_FLUSHING;
2479 /* called repeatedly with @pad as the source pad. This function should push out
2480 * data to the peer element. */
2482 gst_queue2_loop (GstPad * pad)
2487 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2489 /* have to lock for thread-safety */
2490 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2492 if (gst_queue2_is_empty (queue)) {
2495 /* pause the timer while we wait. The fact that we are waiting does not mean
2496 * the byterate on the output pad is lower */
2497 if ((started = queue->out_timer_started))
2498 g_timer_stop (queue->out_timer);
2500 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2501 "queue is empty, waiting for new data");
2503 /* Wait for data to be available, we could be unlocked because of a flush. */
2504 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2506 while (gst_queue2_is_empty (queue));
2508 /* and continue if we were running before */
2510 g_timer_continue (queue->out_timer);
2512 ret = gst_queue2_push_one (queue);
2513 queue->srcresult = ret;
2514 queue->sinkresult = ret;
2515 if (ret != GST_FLOW_OK)
2518 GST_QUEUE2_MUTEX_UNLOCK (queue);
2525 gboolean eos = queue->is_eos;
2526 GstFlowReturn ret = queue->srcresult;
2528 gst_pad_pause_task (queue->srcpad);
2529 GST_QUEUE2_MUTEX_UNLOCK (queue);
2530 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2531 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2532 /* let app know about us giving up if upstream is not expected to do so */
2533 /* EOS is already taken care of elsewhere */
2534 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2535 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2536 (_("Internal data flow error.")),
2537 ("streaming task paused, reason %s (%d)",
2538 gst_flow_get_name (ret), ret));
2539 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2546 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2548 gboolean res = TRUE;
2549 GstQueue2 *queue = GST_QUEUE2 (parent);
2551 #ifndef GST_DISABLE_GST_DEBUG
2552 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2553 event, GST_EVENT_TYPE_NAME (event));
2556 switch (GST_EVENT_TYPE (event)) {
2557 case GST_EVENT_FLUSH_START:
2558 if (QUEUE_IS_USING_QUEUE (queue)) {
2559 /* just forward upstream */
2560 res = gst_pad_push_event (queue->sinkpad, event);
2562 /* now unblock the getrange function */
2563 GST_QUEUE2_MUTEX_LOCK (queue);
2564 GST_DEBUG_OBJECT (queue, "flushing");
2565 queue->srcresult = GST_FLOW_FLUSHING;
2566 GST_QUEUE2_SIGNAL_ADD (queue);
2567 GST_QUEUE2_MUTEX_UNLOCK (queue);
2569 /* when using a temp file, we eat the event */
2571 gst_event_unref (event);
2574 case GST_EVENT_FLUSH_STOP:
2575 if (QUEUE_IS_USING_QUEUE (queue)) {
2576 /* just forward upstream */
2577 res = gst_pad_push_event (queue->sinkpad, event);
2579 /* now unblock the getrange function */
2580 GST_QUEUE2_MUTEX_LOCK (queue);
2581 queue->srcresult = GST_FLOW_OK;
2582 GST_QUEUE2_MUTEX_UNLOCK (queue);
2584 /* when using a temp file, we eat the event */
2586 gst_event_unref (event);
2590 res = gst_pad_push_event (queue->sinkpad, event);
2598 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2602 queue = GST_QUEUE2 (parent);
2604 switch (GST_QUERY_TYPE (query)) {
2605 case GST_QUERY_POSITION:
2610 if (!gst_pad_peer_query (queue->sinkpad, query))
2613 /* get peer position */
2614 gst_query_parse_position (query, &format, &peer_pos);
2616 /* FIXME: this code assumes that there's no discont in the queue */
2618 case GST_FORMAT_BYTES:
2619 peer_pos -= queue->cur_level.bytes;
2621 case GST_FORMAT_TIME:
2622 peer_pos -= queue->cur_level.time;
2625 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2626 "know how to adjust value", gst_format_get_name (format));
2629 /* set updated position */
2630 gst_query_set_position (query, format, peer_pos);
2633 case GST_QUERY_DURATION:
2635 GST_DEBUG_OBJECT (queue, "doing peer query");
2637 if (!gst_pad_peer_query (queue->sinkpad, query))
2640 GST_DEBUG_OBJECT (queue, "peer query success");
2643 case GST_QUERY_BUFFERING:
2647 GST_DEBUG_OBJECT (queue, "query buffering");
2649 /* FIXME - is this condition correct? what should ring buffer do? */
2650 if (QUEUE_IS_USING_QUEUE (queue)) {
2651 /* no temp file, just forward to the peer */
2652 if (!gst_pad_peer_query (queue->sinkpad, query))
2654 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2656 gint64 start, stop, range_start, range_stop;
2657 guint64 writing_pos;
2659 gint64 estimated_total, buffering_left;
2661 gboolean peer_res, is_buffering, is_eos;
2662 gdouble byte_in_rate, byte_out_rate;
2663 GstQueue2Range *queued_ranges;
2665 /* we need a current download region */
2666 if (queue->current == NULL)
2669 writing_pos = queue->current->writing_pos;
2670 byte_in_rate = queue->byte_in_rate;
2671 byte_out_rate = queue->byte_out_rate;
2672 is_buffering = queue->is_buffering;
2673 is_eos = queue->is_eos;
2674 percent = queue->buffering_percent;
2677 /* we're EOS, we know the duration in bytes now */
2679 duration = writing_pos;
2681 /* get duration of upstream in bytes */
2682 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2683 GST_FORMAT_BYTES, &duration);
2686 /* calculate remaining and total download time */
2687 if (peer_res && byte_in_rate > 0.0) {
2688 estimated_total = (duration * 1000) / byte_in_rate;
2689 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2691 estimated_total = -1;
2692 buffering_left = -1;
2694 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2695 G_GINT64_FORMAT, estimated_total, buffering_left);
2697 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2700 case GST_FORMAT_PERCENT:
2701 /* we need duration */
2705 GST_DEBUG_OBJECT (queue,
2706 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2707 duration, writing_pos);
2710 /* get our available data relative to the duration */
2713 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
2718 case GST_FORMAT_BYTES:
2728 /* fill out the buffered ranges */
2729 for (queued_ranges = queue->ranges; queued_ranges;
2730 queued_ranges = queued_ranges->next) {
2732 case GST_FORMAT_PERCENT:
2733 if (duration == -1) {
2739 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2740 queued_ranges->offset, duration);
2742 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
2743 queued_ranges->writing_pos, duration);
2745 case GST_FORMAT_BYTES:
2746 range_start = queued_ranges->offset;
2747 range_stop = queued_ranges->writing_pos;
2754 if (range_start == range_stop)
2756 GST_DEBUG_OBJECT (queue,
2757 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2758 G_GINT64_FORMAT, range_start, range_stop);
2759 gst_query_add_buffering_range (query, range_start, range_stop);
2762 gst_query_set_buffering_percent (query, is_buffering, percent);
2763 gst_query_set_buffering_range (query, format, start, stop,
2765 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2766 byte_in_rate, byte_out_rate, buffering_left);
2770 case GST_QUERY_SCHEDULING:
2773 GstSchedulingFlags flags = 0;
2775 /* we can operate in pull mode when we are using a tempfile */
2776 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2779 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2780 gst_query_set_scheduling (query, flags, 0, -1, 0);
2782 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
2783 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
2787 /* peer handled other queries */
2788 if (!gst_pad_query_default (pad, parent, query))
2798 GST_DEBUG_OBJECT (queue, "failed peer query");
2804 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2806 GstQueue2 *queue = GST_QUEUE2 (element);
2808 /* simply forward to the srcpad query function */
2809 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
2814 gst_queue2_update_upstream_size (GstQueue2 * queue)
2816 gint64 upstream_size = -1;
2818 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
2820 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2821 queue->upstream_size = upstream_size;
2825 static GstFlowReturn
2826 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
2827 guint length, GstBuffer ** buffer)
2832 queue = GST_QUEUE2_CAST (parent);
2834 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2835 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2836 offset = (offset == -1) ? queue->current->reading_pos : offset;
2838 GST_DEBUG_OBJECT (queue,
2839 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2841 /* catch any reads beyond the size of the file here to make sure queue2
2842 * doesn't send seek events beyond the size of the file upstream, since
2843 * that would confuse elements such as souphttpsrc and/or http servers.
2844 * Demuxers often just loop until EOS at the end of the file to figure out
2845 * when they've read all the end-headers or index chunks. */
2846 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2847 gst_queue2_update_upstream_size (queue);
2848 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2849 goto out_unexpected;
2852 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2853 gst_queue2_update_upstream_size (queue);
2854 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2855 length = queue->upstream_size - offset;
2856 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2860 /* FIXME - function will block when the range is not yet available */
2861 ret = gst_queue2_create_read (queue, offset, length, buffer);
2862 GST_QUEUE2_MUTEX_UNLOCK (queue);
2869 ret = queue->srcresult;
2871 GST_DEBUG_OBJECT (queue, "we are flushing");
2872 GST_QUEUE2_MUTEX_UNLOCK (queue);
2877 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2878 GST_QUEUE2_MUTEX_UNLOCK (queue);
2879 return GST_FLOW_EOS;
2883 /* sink currently only operates in push mode */
2885 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
2886 GstPadMode mode, gboolean active)
2891 queue = GST_QUEUE2 (parent);
2894 case GST_PAD_MODE_PUSH:
2896 GST_QUEUE2_MUTEX_LOCK (queue);
2897 GST_DEBUG_OBJECT (queue, "activating push mode");
2898 queue->srcresult = GST_FLOW_OK;
2899 queue->sinkresult = GST_FLOW_OK;
2900 queue->is_eos = FALSE;
2901 queue->unexpected = FALSE;
2902 reset_rate_timer (queue);
2903 GST_QUEUE2_MUTEX_UNLOCK (queue);
2905 /* unblock chain function */
2906 GST_QUEUE2_MUTEX_LOCK (queue);
2907 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2908 queue->srcresult = GST_FLOW_FLUSHING;
2909 queue->sinkresult = GST_FLOW_FLUSHING;
2910 gst_queue2_locked_flush (queue);
2911 GST_QUEUE2_MUTEX_UNLOCK (queue);
2922 /* src operating in push mode, we start a task on the source pad that pushes out
2923 * buffers from the queue */
2925 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
2927 gboolean result = FALSE;
2930 queue = GST_QUEUE2 (parent);
2933 GST_QUEUE2_MUTEX_LOCK (queue);
2934 GST_DEBUG_OBJECT (queue, "activating push mode");
2935 queue->srcresult = GST_FLOW_OK;
2936 queue->sinkresult = GST_FLOW_OK;
2937 queue->is_eos = FALSE;
2938 queue->unexpected = FALSE;
2940 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
2941 GST_QUEUE2_MUTEX_UNLOCK (queue);
2943 /* unblock loop function */
2944 GST_QUEUE2_MUTEX_LOCK (queue);
2945 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2946 queue->srcresult = GST_FLOW_FLUSHING;
2947 queue->sinkresult = GST_FLOW_FLUSHING;
2948 /* the item add signal will unblock */
2949 GST_QUEUE2_SIGNAL_ADD (queue);
2950 GST_QUEUE2_MUTEX_UNLOCK (queue);
2952 /* step 2, make sure streaming finishes */
2953 result = gst_pad_stop_task (pad);
2959 /* pull mode, downstream will call our getrange function */
2961 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
2966 queue = GST_QUEUE2 (parent);
2969 GST_QUEUE2_MUTEX_LOCK (queue);
2970 if (!QUEUE_IS_USING_QUEUE (queue)) {
2971 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2972 /* open the temp file now */
2973 result = gst_queue2_open_temp_location_file (queue);
2974 } else if (!queue->ring_buffer) {
2975 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
2976 result = ! !queue->ring_buffer;
2981 GST_DEBUG_OBJECT (queue, "activating pull mode");
2982 init_ranges (queue);
2983 queue->srcresult = GST_FLOW_OK;
2984 queue->sinkresult = GST_FLOW_OK;
2985 queue->is_eos = FALSE;
2986 queue->unexpected = FALSE;
2987 queue->upstream_size = 0;
2989 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
2990 /* this is not allowed, we cannot operate in pull mode without a temp
2992 queue->srcresult = GST_FLOW_FLUSHING;
2993 queue->sinkresult = GST_FLOW_FLUSHING;
2996 GST_QUEUE2_MUTEX_UNLOCK (queue);
2998 GST_QUEUE2_MUTEX_LOCK (queue);
2999 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3000 queue->srcresult = GST_FLOW_FLUSHING;
3001 queue->sinkresult = GST_FLOW_FLUSHING;
3002 /* this will unlock getrange */
3003 GST_QUEUE2_SIGNAL_ADD (queue);
3005 GST_QUEUE2_MUTEX_UNLOCK (queue);
3012 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3018 case GST_PAD_MODE_PULL:
3019 res = gst_queue2_src_activate_pull (pad, parent, active);
3021 case GST_PAD_MODE_PUSH:
3022 res = gst_queue2_src_activate_push (pad, parent, active);
3025 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3032 static GstStateChangeReturn
3033 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3036 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3038 queue = GST_QUEUE2 (element);
3040 switch (transition) {
3041 case GST_STATE_CHANGE_NULL_TO_READY:
3043 case GST_STATE_CHANGE_READY_TO_PAUSED:
3044 GST_QUEUE2_MUTEX_LOCK (queue);
3045 if (!QUEUE_IS_USING_QUEUE (queue)) {
3046 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3047 if (!gst_queue2_open_temp_location_file (queue))
3048 ret = GST_STATE_CHANGE_FAILURE;
3050 if (queue->ring_buffer) {
3051 g_free (queue->ring_buffer);
3052 queue->ring_buffer = NULL;
3054 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3055 ret = GST_STATE_CHANGE_FAILURE;
3057 init_ranges (queue);
3059 queue->segment_event_received = FALSE;
3060 queue->starting_segment = NULL;
3061 gst_event_replace (&queue->stream_start_event, NULL);
3062 GST_QUEUE2_MUTEX_UNLOCK (queue);
3064 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3070 if (ret == GST_STATE_CHANGE_FAILURE)
3073 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3075 if (ret == GST_STATE_CHANGE_FAILURE)
3078 switch (transition) {
3079 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3081 case GST_STATE_CHANGE_PAUSED_TO_READY:
3082 GST_QUEUE2_MUTEX_LOCK (queue);
3083 if (!QUEUE_IS_USING_QUEUE (queue)) {
3084 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3085 gst_queue2_close_temp_location_file (queue);
3086 } else if (queue->ring_buffer) {
3087 g_free (queue->ring_buffer);
3088 queue->ring_buffer = NULL;
3090 clean_ranges (queue);
3092 if (queue->starting_segment != NULL) {
3093 gst_event_unref (queue->starting_segment);
3094 queue->starting_segment = NULL;
3096 gst_event_replace (&queue->stream_start_event, NULL);
3097 GST_QUEUE2_MUTEX_UNLOCK (queue);
3099 case GST_STATE_CHANGE_READY_TO_NULL:
3108 /* changing the capacity of the queue must wake up
3109 * the _chain function, it might have more room now
3110 * to store the buffer/event in the queue */
3111 #define QUEUE_CAPACITY_CHANGE(q)\
3112 GST_QUEUE2_SIGNAL_DEL (queue);
3114 /* Changing the minimum required fill level must
3115 * wake up the _loop function as it might now
3116 * be able to preceed.
3118 #define QUEUE_THRESHOLD_CHANGE(q)\
3119 GST_QUEUE2_SIGNAL_ADD (queue);
3122 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3126 /* the element must be stopped in order to do this */
3127 GST_OBJECT_LOCK (queue);
3128 state = GST_STATE (queue);
3129 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3131 GST_OBJECT_UNLOCK (queue);
3133 /* set new location */
3134 g_free (queue->temp_template);
3135 queue->temp_template = g_strdup (template);
3142 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3143 GST_OBJECT_UNLOCK (queue);
3148 gst_queue2_set_property (GObject * object,
3149 guint prop_id, const GValue * value, GParamSpec * pspec)
3151 GstQueue2 *queue = GST_QUEUE2 (object);
3153 /* someone could change levels here, and since this
3154 * affects the get/put funcs, we need to lock for safety. */
3155 GST_QUEUE2_MUTEX_LOCK (queue);
3158 case PROP_MAX_SIZE_BYTES:
3159 queue->max_level.bytes = g_value_get_uint (value);
3160 QUEUE_CAPACITY_CHANGE (queue);
3162 case PROP_MAX_SIZE_BUFFERS:
3163 queue->max_level.buffers = g_value_get_uint (value);
3164 QUEUE_CAPACITY_CHANGE (queue);
3166 case PROP_MAX_SIZE_TIME:
3167 queue->max_level.time = g_value_get_uint64 (value);
3168 /* set rate_time to the same value. We use an extra field in the level
3169 * structure so that we can easily access and compare it */
3170 queue->max_level.rate_time = queue->max_level.time;
3171 QUEUE_CAPACITY_CHANGE (queue);
3173 case PROP_USE_BUFFERING:
3174 queue->use_buffering = g_value_get_boolean (value);
3176 case PROP_USE_RATE_ESTIMATE:
3177 queue->use_rate_estimate = g_value_get_boolean (value);
3179 case PROP_LOW_PERCENT:
3180 queue->low_percent = g_value_get_int (value);
3182 case PROP_HIGH_PERCENT:
3183 queue->high_percent = g_value_get_int (value);
3185 case PROP_TEMP_TEMPLATE:
3186 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3188 case PROP_TEMP_REMOVE:
3189 queue->temp_remove = g_value_get_boolean (value);
3191 case PROP_RING_BUFFER_MAX_SIZE:
3192 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3199 GST_QUEUE2_MUTEX_UNLOCK (queue);
3203 gst_queue2_get_property (GObject * object,
3204 guint prop_id, GValue * value, GParamSpec * pspec)
3206 GstQueue2 *queue = GST_QUEUE2 (object);
3208 GST_QUEUE2_MUTEX_LOCK (queue);
3211 case PROP_CUR_LEVEL_BYTES:
3212 g_value_set_uint (value, queue->cur_level.bytes);
3214 case PROP_CUR_LEVEL_BUFFERS:
3215 g_value_set_uint (value, queue->cur_level.buffers);
3217 case PROP_CUR_LEVEL_TIME:
3218 g_value_set_uint64 (value, queue->cur_level.time);
3220 case PROP_MAX_SIZE_BYTES:
3221 g_value_set_uint (value, queue->max_level.bytes);
3223 case PROP_MAX_SIZE_BUFFERS:
3224 g_value_set_uint (value, queue->max_level.buffers);
3226 case PROP_MAX_SIZE_TIME:
3227 g_value_set_uint64 (value, queue->max_level.time);
3229 case PROP_USE_BUFFERING:
3230 g_value_set_boolean (value, queue->use_buffering);
3232 case PROP_USE_RATE_ESTIMATE:
3233 g_value_set_boolean (value, queue->use_rate_estimate);
3235 case PROP_LOW_PERCENT:
3236 g_value_set_int (value, queue->low_percent);
3238 case PROP_HIGH_PERCENT:
3239 g_value_set_int (value, queue->high_percent);
3241 case PROP_TEMP_TEMPLATE:
3242 g_value_set_string (value, queue->temp_template);
3244 case PROP_TEMP_LOCATION:
3245 g_value_set_string (value, queue->temp_location);
3247 case PROP_TEMP_REMOVE:
3248 g_value_set_boolean (value, queue->temp_remove);
3250 case PROP_RING_BUFFER_MAX_SIZE:
3251 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3258 GST_QUEUE2_MUTEX_UNLOCK (queue);