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-tmpl 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 * Since 0.10.24, setting the temp-location property with a filename is deprecated
50 * because it's impossible to securely open a temporary file in this way. The
51 * property will still be used to notify the application of the allocated
54 * Last reviewed on 2009-07-10 (0.10.24)
61 #include "gstqueue2.h"
63 #include <glib/gstdio.h>
65 #include "gst/gst-i18n-lib.h"
66 #include "gst/glib-compat-private.h"
71 #include <io.h> /* lseek, open, close, read */
73 #define lseek _lseeki64
80 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
85 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
90 GST_DEBUG_CATEGORY_STATIC (queue_debug);
91 #define GST_CAT_DEFAULT (queue_debug)
92 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
100 #define DEFAULT_BUFFER_SIZE 4096
101 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_location_set || (queue)->temp_template != NULL)
102 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
103 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
105 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
107 /* default property values */
108 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
109 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
110 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
111 #define DEFAULT_USE_BUFFERING FALSE
112 #define DEFAULT_USE_RATE_ESTIMATE TRUE
113 #define DEFAULT_LOW_PERCENT 10
114 #define DEFAULT_HIGH_PERCENT 99
115 #define DEFAULT_TEMP_REMOVE TRUE
116 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
121 PROP_CUR_LEVEL_BUFFERS,
122 PROP_CUR_LEVEL_BYTES,
124 PROP_MAX_SIZE_BUFFERS,
128 PROP_USE_RATE_ESTIMATE,
134 PROP_RING_BUFFER_MAX_SIZE,
138 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
145 #define STATUS(queue, pad, msg) \
146 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
147 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
148 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
149 " ns, %"G_GUINT64_FORMAT" items", \
150 GST_DEBUG_PAD_NAME (pad), \
151 queue->cur_level.buffers, \
152 queue->max_level.buffers, \
153 queue->cur_level.bytes, \
154 queue->max_level.bytes, \
155 queue->cur_level.time, \
156 queue->max_level.time, \
157 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
158 queue->current->writing_pos - queue->current->max_reading_pos : \
159 queue->queue.length))
161 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
162 g_mutex_lock (&q->qlock); \
165 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
166 GST_QUEUE2_MUTEX_LOCK (q); \
167 if (res != GST_FLOW_OK) \
171 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
172 g_mutex_unlock (&q->qlock); \
175 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
176 STATUS (queue, q->sinkpad, "wait for DEL"); \
177 q->waiting_del = TRUE; \
178 g_cond_wait (&q->item_del, &queue->qlock); \
179 q->waiting_del = FALSE; \
180 if (res != GST_FLOW_OK) { \
181 STATUS (queue, q->srcpad, "received DEL wakeup"); \
184 STATUS (queue, q->sinkpad, "received DEL"); \
187 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
188 STATUS (queue, q->srcpad, "wait for ADD"); \
189 q->waiting_add = TRUE; \
190 g_cond_wait (&q->item_add, &q->qlock); \
191 q->waiting_add = FALSE; \
192 if (res != GST_FLOW_OK) { \
193 STATUS (queue, q->srcpad, "received ADD wakeup"); \
196 STATUS (queue, q->srcpad, "received ADD"); \
199 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
200 if (q->waiting_del) { \
201 STATUS (q, q->srcpad, "signal DEL"); \
202 g_cond_signal (&q->item_del); \
206 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
207 if (q->waiting_add) { \
208 STATUS (q, q->sinkpad, "signal ADD"); \
209 g_cond_signal (&q->item_add); \
214 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
215 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
216 "dataflow inside the queue element");
217 #define gst_queue2_parent_class parent_class
218 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
220 static void gst_queue2_finalize (GObject * object);
222 static void gst_queue2_set_property (GObject * object,
223 guint prop_id, const GValue * value, GParamSpec * pspec);
224 static void gst_queue2_get_property (GObject * object,
225 guint prop_id, GValue * value, GParamSpec * pspec);
227 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
229 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
230 GstBufferList * buffer_list);
231 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
232 static void gst_queue2_loop (GstPad * pad);
234 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
236 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
239 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
241 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
243 static gboolean gst_queue2_handle_query (GstElement * element,
246 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
247 guint64 offset, guint length, GstBuffer ** buffer);
249 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
250 GstPadMode mode, gboolean active);
251 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
252 GstPadMode mode, gboolean active);
253 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
254 GstStateChange transition);
256 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
257 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
259 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
263 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
264 GST_QUEUE2_ITEM_TYPE_BUFFER,
265 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
266 GST_QUEUE2_ITEM_TYPE_EVENT
269 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
272 gst_queue2_class_init (GstQueue2Class * klass)
274 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
275 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
277 gobject_class->set_property = gst_queue2_set_property;
278 gobject_class->get_property = gst_queue2_get_property;
281 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
282 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
283 "Current amount of data in the queue (bytes)",
284 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
285 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
286 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
287 "Current number of buffers in the queue",
288 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
290 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
291 "Current amount of data in the queue (in ns)",
292 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
295 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
296 "Max. amount of data in the queue (bytes, 0=disable)",
297 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
298 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
300 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
301 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
302 DEFAULT_MAX_SIZE_BUFFERS,
303 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
305 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
306 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
307 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
310 g_param_spec_boolean ("use-buffering", "Use buffering",
311 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
312 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
314 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
315 "Estimate the bitrate of the stream to calculate time level",
316 DEFAULT_USE_RATE_ESTIMATE,
317 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
319 g_param_spec_int ("low-percent", "Low percent",
320 "Low threshold for buffering to start. Only used if use-buffering is True",
321 0, 100, DEFAULT_LOW_PERCENT,
322 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
324 g_param_spec_int ("high-percent", "High percent",
325 "High threshold for buffering to finish. Only used if use-buffering is True",
326 0, 100, DEFAULT_HIGH_PERCENT,
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
330 g_param_spec_string ("temp-template", "Temporary File Template",
331 "File template to store temporary files in, should contain directory "
332 "and XXXXXX. (NULL == disabled)",
333 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
336 g_param_spec_string ("temp-location", "Temporary File Location",
337 "Location to store temporary files in (Deprecated: Only read this "
338 "property, use temp-template to configure the name template)",
339 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
342 * GstQueue2:temp-remove
344 * When temp-template is set, remove the temporary file when going to READY.
348 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
349 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
350 "Remove the temp-location after use",
351 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 * GstQueue2:ring-buffer-max-size
356 * The maximum size of the ring buffer in bytes. If set to 0, the ring
357 * buffer is disabled. Default 0.
361 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
362 g_param_spec_uint64 ("ring-buffer-max-size",
363 "Max. ring buffer size (bytes)",
364 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
365 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
366 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 /* set several parent class virtual functions */
369 gobject_class->finalize = gst_queue2_finalize;
371 gst_element_class_add_pad_template (gstelement_class,
372 gst_static_pad_template_get (&srctemplate));
373 gst_element_class_add_pad_template (gstelement_class,
374 gst_static_pad_template_get (&sinktemplate));
376 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
379 "Erik Walthinsen <omega@cse.ogi.edu>, "
380 "Wim Taymans <wim.taymans@gmail.com>");
382 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
383 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
387 gst_queue2_init (GstQueue2 * queue)
389 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
391 gst_pad_set_chain_function (queue->sinkpad,
392 GST_DEBUG_FUNCPTR (gst_queue2_chain));
393 gst_pad_set_chain_list_function (queue->sinkpad,
394 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
395 gst_pad_set_activatemode_function (queue->sinkpad,
396 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
397 gst_pad_set_event_function (queue->sinkpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
399 gst_pad_set_query_function (queue->sinkpad,
400 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
401 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
402 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
404 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
406 gst_pad_set_activatemode_function (queue->srcpad,
407 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
408 gst_pad_set_getrange_function (queue->srcpad,
409 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
410 gst_pad_set_event_function (queue->srcpad,
411 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
412 gst_pad_set_query_function (queue->srcpad,
413 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
414 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
415 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
418 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
419 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
420 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
421 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
422 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
423 queue->use_buffering = DEFAULT_USE_BUFFERING;
424 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
425 queue->low_percent = DEFAULT_LOW_PERCENT;
426 queue->high_percent = DEFAULT_HIGH_PERCENT;
428 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
429 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
431 queue->sinktime = GST_CLOCK_TIME_NONE;
432 queue->srctime = GST_CLOCK_TIME_NONE;
433 queue->sink_tainted = TRUE;
434 queue->src_tainted = TRUE;
436 queue->srcresult = GST_FLOW_FLUSHING;
437 queue->sinkresult = GST_FLOW_FLUSHING;
438 queue->is_eos = FALSE;
439 queue->in_timer = g_timer_new ();
440 queue->out_timer = g_timer_new ();
442 g_mutex_init (&queue->qlock);
443 queue->waiting_add = FALSE;
444 g_cond_init (&queue->item_add);
445 queue->waiting_del = FALSE;
446 g_cond_init (&queue->item_del);
447 g_queue_init (&queue->queue);
449 queue->buffering_percent = 100;
451 /* tempfile related */
452 queue->temp_template = NULL;
453 queue->temp_location = NULL;
454 queue->temp_location_set = FALSE;
455 queue->temp_remove = DEFAULT_TEMP_REMOVE;
457 queue->ring_buffer = NULL;
458 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
460 GST_DEBUG_OBJECT (queue,
461 "initialized queue's not_empty & not_full conditions");
464 /* called only once, as opposed to dispose */
466 gst_queue2_finalize (GObject * object)
468 GstQueue2 *queue = GST_QUEUE2 (object);
470 GST_DEBUG_OBJECT (queue, "finalizing queue");
472 while (!g_queue_is_empty (&queue->queue)) {
473 GstMiniObject *data = g_queue_pop_head (&queue->queue);
475 gst_mini_object_unref (data);
478 g_queue_clear (&queue->queue);
479 g_mutex_clear (&queue->qlock);
480 g_cond_clear (&queue->item_add);
481 g_cond_clear (&queue->item_del);
482 g_timer_destroy (queue->in_timer);
483 g_timer_destroy (queue->out_timer);
485 /* temp_file path cleanup */
486 g_free (queue->temp_template);
487 g_free (queue->temp_location);
489 G_OBJECT_CLASS (parent_class)->finalize (object);
493 debug_ranges (GstQueue2 * queue)
495 GstQueue2Range *walk;
497 for (walk = queue->ranges; walk; walk = walk->next) {
498 GST_DEBUG_OBJECT (queue,
499 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
500 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
501 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
502 walk->rb_writing_pos, walk->reading_pos,
503 walk == queue->current ? "**y**" : " n ");
507 /* clear all the downloaded ranges */
509 clean_ranges (GstQueue2 * queue)
511 GST_DEBUG_OBJECT (queue, "clean queue ranges");
513 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
514 queue->ranges = NULL;
515 queue->current = NULL;
518 /* find a range that contains @offset or NULL when nothing does */
519 static GstQueue2Range *
520 find_range (GstQueue2 * queue, guint64 offset)
522 GstQueue2Range *range = NULL;
523 GstQueue2Range *walk;
525 /* first do a quick check for the current range */
526 for (walk = queue->ranges; walk; walk = walk->next) {
527 if (offset >= walk->offset && offset <= walk->writing_pos) {
528 /* we can reuse an existing range */
534 GST_DEBUG_OBJECT (queue,
535 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
536 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
538 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
544 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
546 guint64 max_reading_pos, writing_pos;
548 writing_pos = range->writing_pos;
549 max_reading_pos = range->max_reading_pos;
551 if (writing_pos > max_reading_pos)
552 queue->cur_level.bytes = writing_pos - max_reading_pos;
554 queue->cur_level.bytes = 0;
557 /* make a new range for @offset or reuse an existing range */
558 static GstQueue2Range *
559 add_range (GstQueue2 * queue, guint64 offset)
561 GstQueue2Range *range, *prev, *next;
563 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
565 if ((range = find_range (queue, offset))) {
566 GST_DEBUG_OBJECT (queue,
567 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
569 range->writing_pos = offset;
571 GST_DEBUG_OBJECT (queue,
572 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
574 range = g_slice_new0 (GstQueue2Range);
575 range->offset = offset;
576 /* we want to write to the next location in the ring buffer */
577 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
578 range->writing_pos = offset;
579 range->rb_writing_pos = range->rb_offset;
580 range->reading_pos = offset;
581 range->max_reading_pos = offset;
585 next = queue->ranges;
587 if (next->offset > offset) {
588 /* insert before next */
589 GST_DEBUG_OBJECT (queue,
590 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
602 queue->ranges = range;
604 debug_ranges (queue);
606 /* update the stats for this range */
607 update_cur_level (queue, range);
613 /* clear and init the download ranges for offset 0 */
615 init_ranges (GstQueue2 * queue)
617 GST_DEBUG_OBJECT (queue, "init queue ranges");
619 /* get rid of all the current ranges */
620 clean_ranges (queue);
621 /* make a range for offset 0 */
622 queue->current = add_range (queue, 0);
625 /* calculate the diff between running time on the sink and src of the queue.
626 * This is the total amount of time in the queue. */
628 update_time_level (GstQueue2 * queue)
630 if (queue->sink_tainted) {
632 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
633 queue->sink_segment.position);
634 queue->sink_tainted = FALSE;
637 if (queue->src_tainted) {
639 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
640 queue->src_segment.position);
641 queue->src_tainted = FALSE;
644 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
645 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
647 if (queue->sinktime != GST_CLOCK_TIME_NONE
648 && queue->srctime != GST_CLOCK_TIME_NONE
649 && queue->sinktime >= queue->srctime)
650 queue->cur_level.time = queue->sinktime - queue->srctime;
652 queue->cur_level.time = 0;
655 /* take a SEGMENT event and apply the values to segment, updating the time
658 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
661 gst_event_copy_segment (event, segment);
663 if (segment->format == GST_FORMAT_BYTES) {
664 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
665 /* start is where we'll be getting from and as such writing next */
666 queue->current = add_range (queue, segment->start);
667 /* update the stats for this range */
668 update_cur_level (queue, queue->current);
672 /* now configure the values, we use these to track timestamps on the
674 if (segment->format != GST_FORMAT_TIME) {
675 /* non-time format, pretent the current time segment is closed with a
676 * 0 start and unknown stop time. */
677 segment->format = GST_FORMAT_TIME;
683 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
686 queue->sink_tainted = TRUE;
688 queue->src_tainted = TRUE;
690 /* segment can update the time level of the queue */
691 update_time_level (queue);
694 /* take a buffer and update segment, updating the time level of the queue. */
696 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
699 GstClockTime duration, timestamp;
701 timestamp = GST_BUFFER_TIMESTAMP (buffer);
702 duration = GST_BUFFER_DURATION (buffer);
704 /* if no timestamp is set, assume it's continuous with the previous
706 if (timestamp == GST_CLOCK_TIME_NONE)
707 timestamp = segment->position;
710 if (duration != GST_CLOCK_TIME_NONE)
711 timestamp += duration;
713 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
714 GST_TIME_ARGS (timestamp));
716 segment->position = timestamp;
719 queue->sink_tainted = TRUE;
721 queue->src_tainted = TRUE;
723 /* calc diff with other end */
724 update_time_level (queue);
728 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
730 GstClockTime *timestamp = data;
732 GST_TRACE ("buffer %u has ts %" GST_TIME_FORMAT
733 " duration %" GST_TIME_FORMAT, idx,
734 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
735 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
737 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
738 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
740 if (GST_BUFFER_DURATION_IS_VALID (*buf))
741 *timestamp += GST_BUFFER_DURATION (*buf);
743 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
747 /* take a buffer list and update segment, updating the time level of the queue */
749 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
750 GstSegment * segment, gboolean is_sink)
752 GstClockTime timestamp;
754 /* if no timestamp is set, assume it's continuous with the previous time */
755 timestamp = segment->position;
757 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
759 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
760 GST_TIME_ARGS (timestamp));
762 segment->position = timestamp;
765 queue->sink_tainted = TRUE;
767 queue->src_tainted = TRUE;
769 /* calc diff with other end */
770 update_time_level (queue);
774 update_buffering (GstQueue2 * queue)
777 gboolean post = FALSE;
779 if (queue->high_percent <= 0)
782 #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)
785 /* on EOS we are always 100% full, we set the var here so that it we can
786 * reuse the logic below to stop buffering */
788 GST_LOG_OBJECT (queue, "we are EOS");
790 /* figure out the percent we are filled, we take the max of all formats. */
792 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
793 percent = GET_PERCENT (bytes, 0);
795 guint64 rb_size = queue->ring_buffer_max_size;
796 percent = GET_PERCENT (bytes, rb_size);
798 percent = MAX (percent, GET_PERCENT (time, 0));
799 percent = MAX (percent, GET_PERCENT (buffers, 0));
801 /* also apply the rate estimate when we need to */
802 if (queue->use_rate_estimate)
803 percent = MAX (percent, GET_PERCENT (rate_time, 0));
806 if (queue->is_buffering) {
808 /* if we were buffering see if we reached the high watermark */
809 if (percent >= queue->high_percent)
810 queue->is_buffering = FALSE;
812 /* we were not buffering, check if we need to start buffering if we drop
813 * below the low threshold */
814 if (percent < queue->low_percent) {
815 queue->is_buffering = TRUE;
816 queue->buffering_iteration++;
822 GstBufferingMode mode;
823 gint64 buffering_left = -1;
825 /* scale to high percent so that it becomes the 100% mark */
826 percent = percent * 100 / queue->high_percent;
831 if (percent != queue->buffering_percent) {
832 queue->buffering_percent = percent;
834 if (!QUEUE_IS_USING_QUEUE (queue)) {
837 if (QUEUE_IS_USING_RING_BUFFER (queue))
838 mode = GST_BUFFERING_TIMESHIFT;
840 mode = GST_BUFFERING_DOWNLOAD;
842 if (queue->byte_in_rate > 0) {
843 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
846 (gdouble) ((duration -
847 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
850 buffering_left = G_MAXINT64;
853 mode = GST_BUFFERING_STREAM;
856 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
857 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
859 gst_message_set_buffering_stats (message, mode,
860 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
862 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
865 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
872 reset_rate_timer (GstQueue2 * queue)
875 queue->bytes_out = 0;
876 queue->byte_in_rate = 0.0;
877 queue->byte_in_period = 0;
878 queue->byte_out_rate = 0.0;
879 queue->last_in_elapsed = 0.0;
880 queue->last_out_elapsed = 0.0;
881 queue->in_timer_started = FALSE;
882 queue->out_timer_started = FALSE;
885 /* the interval in seconds to recalculate the rate */
886 #define RATE_INTERVAL 0.2
887 /* Tuning for rate estimation. We use a large window for the input rate because
888 * it should be stable when connected to a network. The output rate is less
889 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
890 * therefore adapt more quickly.
891 * However, initial input rate may be subject to a burst, and should therefore
892 * initially also adapt more quickly to changes, and only later on give higher
893 * weight to previous values. */
894 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
895 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
898 update_in_rates (GstQueue2 * queue)
900 gdouble elapsed, period;
901 gdouble byte_in_rate;
903 if (!queue->in_timer_started) {
904 queue->in_timer_started = TRUE;
905 g_timer_start (queue->in_timer);
909 elapsed = g_timer_elapsed (queue->in_timer, NULL);
911 /* recalc after each interval. */
912 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
913 period = elapsed - queue->last_in_elapsed;
915 GST_DEBUG_OBJECT (queue,
916 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
917 period, queue->bytes_in, queue->byte_in_period);
919 byte_in_rate = queue->bytes_in / period;
921 if (queue->byte_in_rate == 0.0)
922 queue->byte_in_rate = byte_in_rate;
924 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
925 (double) queue->byte_in_period, period);
927 /* another data point, cap at 16 for long time running average */
928 if (queue->byte_in_period < 16 * RATE_INTERVAL)
929 queue->byte_in_period += period;
931 /* reset the values to calculate rate over the next interval */
932 queue->last_in_elapsed = elapsed;
936 if (queue->byte_in_rate > 0.0) {
937 queue->cur_level.rate_time =
938 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
940 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
941 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
945 update_out_rates (GstQueue2 * queue)
947 gdouble elapsed, period;
948 gdouble byte_out_rate;
950 if (!queue->out_timer_started) {
951 queue->out_timer_started = TRUE;
952 g_timer_start (queue->out_timer);
956 elapsed = g_timer_elapsed (queue->out_timer, NULL);
958 /* recalc after each interval. */
959 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
960 period = elapsed - queue->last_out_elapsed;
962 GST_DEBUG_OBJECT (queue,
963 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
965 byte_out_rate = queue->bytes_out / period;
967 if (queue->byte_out_rate == 0.0)
968 queue->byte_out_rate = byte_out_rate;
970 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
972 /* reset the values to calculate rate over the next interval */
973 queue->last_out_elapsed = elapsed;
974 queue->bytes_out = 0;
976 if (queue->byte_in_rate > 0.0) {
977 queue->cur_level.rate_time =
978 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
980 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
981 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
985 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
987 guint64 reading_pos, max_reading_pos;
990 max_reading_pos = range->max_reading_pos;
992 max_reading_pos = MAX (max_reading_pos, reading_pos);
994 GST_DEBUG_OBJECT (queue,
995 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
996 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
997 range->max_reading_pos = max_reading_pos;
999 update_cur_level (queue, range);
1003 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1008 GST_QUEUE2_MUTEX_UNLOCK (queue);
1010 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1013 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1014 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1015 GST_SEEK_TYPE_NONE, -1);
1017 res = gst_pad_push_event (queue->sinkpad, event);
1018 GST_QUEUE2_MUTEX_LOCK (queue);
1021 queue->current = add_range (queue, offset);
1026 /* see if there is enough data in the file to read a full buffer */
1028 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1030 GstQueue2Range *range;
1032 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1035 if ((range = find_range (queue, offset))) {
1036 if (queue->current != range) {
1037 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1038 perform_seek_to_offset (queue, range->writing_pos);
1041 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1042 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1044 /* we have a range for offset */
1045 GST_DEBUG_OBJECT (queue,
1046 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1047 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1049 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1052 if (offset + length <= range->writing_pos)
1055 GST_DEBUG_OBJECT (queue,
1056 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1057 (offset + length) - range->writing_pos);
1060 GST_INFO_OBJECT (queue, "not found in any range");
1061 /* we don't have the range, see how far away we are, FIXME, find a good
1062 * threshold based on the incoming rate. */
1063 if (!queue->is_eos && queue->current) {
1064 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1065 if (offset < queue->current->offset || offset >
1066 queue->current->writing_pos + QUEUE_MAX_BYTES (queue) -
1067 queue->cur_level.bytes) {
1068 perform_seek_to_offset (queue, offset);
1070 GST_INFO_OBJECT (queue,
1071 "requested data is within range, wait for data");
1073 } else if (offset < queue->current->writing_pos + 200000) {
1074 update_cur_pos (queue, queue->current, offset + length);
1075 GST_INFO_OBJECT (queue, "wait for data");
1080 /* too far away, do a seek */
1081 perform_seek_to_offset (queue, offset);
1088 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1089 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1090 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1092 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1095 static GstFlowReturn
1096 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1097 guint8 * dst, gint64 * read_return)
1099 guint8 *ring_buffer;
1102 ring_buffer = queue->ring_buffer;
1104 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1107 /* this should not block */
1108 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1110 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1111 res = fread (dst, 1, length, queue->temp_file);
1113 memcpy (dst, ring_buffer + offset, length);
1117 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1119 if (G_UNLIKELY (res < length)) {
1120 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1121 goto could_not_read;
1122 /* check for errors or EOF */
1123 if (ferror (queue->temp_file))
1124 goto could_not_read;
1125 if (feof (queue->temp_file) && length > 0)
1135 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1136 return GST_FLOW_ERROR;
1140 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1141 return GST_FLOW_ERROR;
1145 GST_DEBUG ("non-regular file hits EOS");
1146 return GST_FLOW_EOS;
1150 static GstFlowReturn
1151 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1152 GstBuffer ** buffer)
1157 guint64 file_offset;
1158 guint block_length, remaining, read_length;
1161 GstFlowReturn ret = GST_FLOW_OK;
1163 /* allocate the output buffer of the requested size */
1164 if (*buffer == NULL)
1165 buf = gst_buffer_new_allocate (NULL, length, NULL);
1169 gst_buffer_map (buf, &info, GST_MAP_WRITE);
1172 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1176 rb_size = queue->ring_buffer_max_size;
1179 while (remaining > 0) {
1180 /* configure how much/whether to read */
1181 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1184 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1187 /* calculate how far away the offset is */
1188 if (queue->current->writing_pos > rpos)
1189 level = queue->current->writing_pos - rpos;
1193 GST_DEBUG_OBJECT (queue,
1194 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1195 ", level %" G_GUINT64_FORMAT,
1196 rpos, queue->current->writing_pos, level);
1198 if (level >= rb_size) {
1199 /* we don't have the data but if we have a ring buffer that is full, we
1201 GST_DEBUG_OBJECT (queue,
1202 "ring buffer full, reading ring-buffer-max-size %"
1203 G_GUINT64_FORMAT " bytes", rb_size);
1204 read_length = rb_size;
1205 } else if (queue->is_eos) {
1206 /* won't get any more data so read any data we have */
1208 GST_DEBUG_OBJECT (queue,
1209 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1211 read_length = level;
1217 if (read_length == 0) {
1218 if (QUEUE_IS_USING_RING_BUFFER (queue)
1219 && queue->current->max_reading_pos > rpos) {
1220 /* protect cached data (data between offset and max_reading_pos)
1221 * and update current level */
1222 GST_DEBUG_OBJECT (queue,
1223 "protecting cached data [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1224 "]", rpos, queue->current->max_reading_pos);
1225 queue->current->max_reading_pos = rpos;
1226 update_cur_level (queue, queue->current);
1228 GST_DEBUG_OBJECT (queue, "waiting for add");
1229 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1233 /* we have the requested data so read it */
1234 read_length = remaining;
1237 /* set range reading_pos to actual reading position for this read */
1238 queue->current->reading_pos = rpos;
1240 /* configure how much and from where to read */
1241 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1243 (queue->current->rb_offset + (rpos -
1244 queue->current->offset)) % rb_size;
1245 if (file_offset + read_length > rb_size) {
1246 block_length = rb_size - file_offset;
1248 block_length = read_length;
1252 block_length = read_length;
1255 /* while we still have data to read, we loop */
1256 while (read_length > 0) {
1260 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1261 data, &read_return);
1262 if (ret != GST_FLOW_OK)
1265 file_offset += read_return;
1266 if (QUEUE_IS_USING_RING_BUFFER (queue))
1267 file_offset %= rb_size;
1269 data += read_return;
1270 read_length -= read_return;
1271 block_length = read_length;
1272 remaining -= read_return;
1274 rpos = (queue->current->reading_pos += read_return);
1275 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1277 GST_QUEUE2_SIGNAL_DEL (queue);
1278 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1281 gst_buffer_unmap (buf, &info);
1282 gst_buffer_resize (buf, 0, length);
1284 GST_BUFFER_OFFSET (buf) = offset;
1285 GST_BUFFER_OFFSET_END (buf) = offset + length;
1294 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1295 gst_buffer_unmap (buf, &info);
1296 if (*buffer == NULL)
1297 gst_buffer_unref (buf);
1298 return GST_FLOW_EOS;
1302 GST_DEBUG_OBJECT (queue, "we are flushing");
1303 gst_buffer_unmap (buf, &info);
1304 if (*buffer == NULL)
1305 gst_buffer_unref (buf);
1306 return GST_FLOW_FLUSHING;
1310 GST_DEBUG_OBJECT (queue, "we have a read error");
1311 gst_buffer_unmap (buf, &info);
1312 if (*buffer == NULL)
1313 gst_buffer_unref (buf);
1318 /* should be called with QUEUE_LOCK */
1319 static GstMiniObject *
1320 gst_queue2_read_item_from_file (GstQueue2 * queue)
1322 GstMiniObject *item;
1324 if (queue->stream_start_event != NULL) {
1325 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1326 queue->stream_start_event = NULL;
1327 } else if (queue->starting_segment != NULL) {
1328 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1329 queue->starting_segment = NULL;
1332 GstBuffer *buffer = NULL;
1333 guint64 reading_pos;
1335 reading_pos = queue->current->reading_pos;
1338 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1343 item = GST_MINI_OBJECT_CAST (buffer);
1346 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1356 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1357 * the temp filename. */
1359 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1364 if (queue->temp_file)
1365 goto already_opened;
1367 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1369 /* we have two cases:
1370 * - temp_location was set to something !NULL (Deprecated). in this case we
1371 * open the specified filename.
1372 * - temp_template was set, allocate a filename and open that filename
1374 if (!queue->temp_location_set) {
1376 if (queue->temp_template == NULL)
1379 /* make copy of the template, we don't want to change this */
1380 name = g_strdup (queue->temp_template);
1381 fd = g_mkstemp (name);
1383 goto mkstemp_failed;
1385 /* open the file for update/writing */
1386 queue->temp_file = fdopen (fd, "wb+");
1387 /* error creating file */
1388 if (queue->temp_file == NULL)
1391 g_free (queue->temp_location);
1392 queue->temp_location = name;
1394 GST_QUEUE2_MUTEX_UNLOCK (queue);
1396 /* we can't emit the notify with the lock */
1397 g_object_notify (G_OBJECT (queue), "temp-location");
1399 GST_QUEUE2_MUTEX_LOCK (queue);
1401 /* open the file for update/writing, this is deprecated but we still need to
1402 * support it for API/ABI compatibility */
1403 queue->temp_file = g_fopen (queue->temp_location, "wb+");
1404 /* error creating file */
1405 if (queue->temp_file == NULL)
1408 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1415 GST_DEBUG_OBJECT (queue, "temp file was already open");
1420 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1421 (_("No Temp directory specified.")), (NULL));
1426 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1427 (_("Could not create temp file \"%s\"."), queue->temp_template),
1434 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1435 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1444 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1447 if (queue->temp_file == NULL)
1450 GST_DEBUG_OBJECT (queue, "closing temp file");
1452 fflush (queue->temp_file);
1453 fclose (queue->temp_file);
1455 if (queue->temp_remove)
1456 remove (queue->temp_location);
1458 queue->temp_file = NULL;
1459 clean_ranges (queue);
1463 gst_queue2_flush_temp_file (GstQueue2 * queue)
1465 if (queue->temp_file == NULL)
1468 GST_DEBUG_OBJECT (queue, "flushing temp file");
1470 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1474 gst_queue2_locked_flush (GstQueue2 * queue)
1476 if (!QUEUE_IS_USING_QUEUE (queue)) {
1477 if (QUEUE_IS_USING_TEMP_FILE (queue))
1478 gst_queue2_flush_temp_file (queue);
1479 init_ranges (queue);
1481 while (!g_queue_is_empty (&queue->queue)) {
1482 GstMiniObject *data = g_queue_pop_head (&queue->queue);
1484 /* Then lose another reference because we are supposed to destroy that
1485 data when flushing */
1486 gst_mini_object_unref (data);
1489 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1490 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1491 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1492 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1493 queue->sink_tainted = queue->src_tainted = TRUE;
1494 if (queue->starting_segment != NULL)
1495 gst_event_unref (queue->starting_segment);
1496 queue->starting_segment = NULL;
1497 queue->segment_event_received = FALSE;
1498 gst_event_replace (&queue->stream_start_event, NULL);
1500 /* we deleted a lot of something */
1501 GST_QUEUE2_SIGNAL_DEL (queue);
1505 gst_queue2_wait_free_space (GstQueue2 * queue)
1507 /* We make space available if we're "full" according to whatever
1508 * the user defined as "full". */
1509 if (gst_queue2_is_filled (queue)) {
1512 /* pause the timer while we wait. The fact that we are waiting does not mean
1513 * the byterate on the input pad is lower */
1514 if ((started = queue->in_timer_started))
1515 g_timer_stop (queue->in_timer);
1517 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1518 "queue is full, waiting for free space");
1520 /* Wait for space to be available, we could be unlocked because of a flush. */
1521 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1523 while (gst_queue2_is_filled (queue));
1525 /* and continue if we were running before */
1527 g_timer_continue (queue->in_timer);
1534 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1540 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1543 guint8 *data, *ring_buffer;
1544 guint size, rb_size;
1545 guint64 writing_pos, new_writing_pos;
1546 GstQueue2Range *range, *prev, *next;
1548 if (QUEUE_IS_USING_RING_BUFFER (queue))
1549 writing_pos = queue->current->rb_writing_pos;
1551 writing_pos = queue->current->writing_pos;
1552 ring_buffer = queue->ring_buffer;
1553 rb_size = queue->ring_buffer_max_size;
1555 gst_buffer_map (buffer, &info, GST_MAP_READ);
1560 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1561 GST_BUFFER_OFFSET (buffer));
1566 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1569 /* calculate the space in the ring buffer not used by data from
1570 * the current range */
1571 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1572 /* wait until there is some free space */
1573 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1575 /* get the amount of space we have */
1576 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1578 /* calculate if we need to split or if we can write the entire
1580 to_write = MIN (size, space);
1582 /* the writing position in the ring buffer after writing (part
1583 * or all of) the buffer */
1584 new_writing_pos = (writing_pos + to_write) % rb_size;
1587 range = queue->ranges;
1589 /* if we need to overwrite data in the ring buffer, we need to
1592 * warning: this code is complicated and includes some
1593 * simplifications - pen, paper and diagrams for the cases
1596 guint64 range_data_start, range_data_end;
1597 GstQueue2Range *range_to_destroy = NULL;
1599 range_data_start = range->rb_offset;
1600 range_data_end = range->rb_writing_pos;
1602 /* handle the special case where the range has no data in it */
1603 if (range->writing_pos == range->offset) {
1604 if (range != queue->current) {
1605 GST_DEBUG_OBJECT (queue,
1606 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1607 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1609 range_to_destroy = range;
1611 prev->next = range->next;
1616 if (range_data_end > range_data_start) {
1617 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1620 if (new_writing_pos > range_data_start) {
1621 if (new_writing_pos >= range_data_end) {
1622 GST_DEBUG_OBJECT (queue,
1623 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1624 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1626 range_to_destroy = range;
1628 prev->next = range->next;
1630 GST_DEBUG_OBJECT (queue,
1631 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1632 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1633 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1634 range->offset + new_writing_pos - range_data_start,
1636 range->offset += (new_writing_pos - range_data_start);
1637 range->rb_offset = new_writing_pos;
1641 guint64 new_wpos_virt = writing_pos + to_write;
1643 if (new_wpos_virt <= range_data_start)
1646 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1647 GST_DEBUG_OBJECT (queue,
1648 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1649 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1651 range_to_destroy = range;
1653 prev->next = range->next;
1655 GST_DEBUG_OBJECT (queue,
1656 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1657 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1658 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1659 range->offset + new_writing_pos - range_data_start,
1661 range->offset += (new_wpos_virt - range_data_start);
1662 range->rb_offset = new_writing_pos;
1667 if (!range_to_destroy)
1670 range = range->next;
1671 if (range_to_destroy) {
1672 if (range_to_destroy == queue->ranges)
1673 queue->ranges = range;
1674 g_slice_free (GstQueue2Range, range_to_destroy);
1675 range_to_destroy = NULL;
1680 new_writing_pos = writing_pos + to_write;
1683 if (QUEUE_IS_USING_TEMP_FILE (queue)
1684 && FSEEK_FILE (queue->temp_file, writing_pos))
1687 if (new_writing_pos > writing_pos) {
1688 GST_INFO_OBJECT (queue,
1689 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1690 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1691 queue->current->writing_pos, queue->current->rb_writing_pos);
1692 /* either not using ring buffer or no wrapping, just write */
1693 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1694 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1697 memcpy (ring_buffer + writing_pos, data, to_write);
1700 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1701 /* try to merge with next range */
1702 while ((next = queue->current->next)) {
1703 GST_INFO_OBJECT (queue,
1704 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1705 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1706 if (new_writing_pos < next->offset)
1709 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1712 /* remove the group, we could choose to not read the data in this range
1713 * again. This would involve us doing a seek to the current writing position
1714 * in the range. FIXME, It would probably make sense to do a seek when there
1715 * is a lot of data in the range we merged with to avoid reading it all
1717 queue->current->next = next->next;
1718 g_slice_free (GstQueue2Range, next);
1720 debug_ranges (queue);
1722 goto update_and_signal;
1726 guint block_one, block_two;
1728 block_one = rb_size - writing_pos;
1729 block_two = to_write - block_one;
1731 if (block_one > 0) {
1732 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1733 /* write data to end of ring buffer */
1734 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1735 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1738 memcpy (ring_buffer + writing_pos, data, block_one);
1742 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1745 if (block_two > 0) {
1746 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1747 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1748 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1751 memcpy (ring_buffer, data + block_one, block_two);
1757 /* update the writing positions */
1759 GST_INFO_OBJECT (queue,
1760 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1761 to_write, writing_pos, size);
1763 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1765 queue->current->writing_pos += to_write;
1766 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1768 queue->current->writing_pos = writing_pos = new_writing_pos;
1770 update_cur_level (queue, queue->current);
1772 /* update the buffering status */
1773 if (queue->use_buffering)
1774 update_buffering (queue);
1776 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1777 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1779 GST_QUEUE2_SIGNAL_ADD (queue);
1782 gst_buffer_unmap (buffer, &info);
1789 GST_DEBUG_OBJECT (queue, "we are flushing");
1790 gst_buffer_unmap (buffer, &info);
1791 /* FIXME - GST_FLOW_EOS ? */
1796 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1797 gst_buffer_unmap (buffer, &info);
1804 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1808 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1809 (_("Error while writing to download file.")),
1810 ("%s", g_strerror (errno)));
1813 gst_buffer_unmap (buffer, &info);
1819 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
1821 GstQueue2 *queue = q;
1823 GST_TRACE_OBJECT (queue,
1824 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
1825 gst_buffer_get_size (*buf));
1827 if (!gst_queue2_create_write (queue, *buf)) {
1828 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1835 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
1837 guint *p_size = data;
1840 buf_size = gst_buffer_get_size (*buf);
1841 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
1842 *p_size += buf_size;
1846 /* enqueue an item an update the level stats */
1848 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1849 GstQueue2ItemType item_type)
1851 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1855 buffer = GST_BUFFER_CAST (item);
1856 size = gst_buffer_get_size (buffer);
1858 /* add buffer to the statistics */
1859 if (QUEUE_IS_USING_QUEUE (queue)) {
1860 queue->cur_level.buffers++;
1861 queue->cur_level.bytes += size;
1863 queue->bytes_in += size;
1865 /* apply new buffer to segment stats */
1866 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1867 /* update the byterate stats */
1868 update_in_rates (queue);
1870 if (!QUEUE_IS_USING_QUEUE (queue)) {
1871 /* FIXME - check return value? */
1872 gst_queue2_create_write (queue, buffer);
1874 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1875 GstBufferList *buffer_list;
1878 buffer_list = GST_BUFFER_LIST_CAST (item);
1880 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1881 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1883 /* add buffer to the statistics */
1884 if (QUEUE_IS_USING_QUEUE (queue)) {
1885 queue->cur_level.buffers++;
1886 queue->cur_level.bytes += size;
1888 queue->bytes_in += size;
1890 /* apply new buffer to segment stats */
1891 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1893 /* update the byterate stats */
1894 update_in_rates (queue);
1896 if (!QUEUE_IS_USING_QUEUE (queue)) {
1897 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1899 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
1902 event = GST_EVENT_CAST (item);
1904 switch (GST_EVENT_TYPE (event)) {
1906 /* Zero the thresholds, this makes sure the queue is completely
1907 * filled and we can read all data from the queue. */
1908 GST_DEBUG_OBJECT (queue, "we have EOS");
1909 queue->is_eos = TRUE;
1911 case GST_EVENT_SEGMENT:
1912 apply_segment (queue, event, &queue->sink_segment, TRUE);
1913 /* This is our first new segment, we hold it
1914 * as we can't save it on the temp file */
1915 if (!QUEUE_IS_USING_QUEUE (queue)) {
1916 if (queue->segment_event_received)
1917 goto unexpected_event;
1919 queue->segment_event_received = TRUE;
1920 if (queue->starting_segment != NULL)
1921 gst_event_unref (queue->starting_segment);
1922 queue->starting_segment = event;
1925 /* a new segment allows us to accept more buffers if we got EOS
1926 * from downstream */
1927 queue->unexpected = FALSE;
1929 case GST_EVENT_STREAM_START:
1930 if (!QUEUE_IS_USING_QUEUE (queue)) {
1931 gst_event_replace (&queue->stream_start_event, event);
1932 gst_event_unref (event);
1937 if (!QUEUE_IS_USING_QUEUE (queue))
1938 goto unexpected_event;
1942 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1943 item, GST_OBJECT_NAME (queue));
1944 /* we can't really unref since we don't know what it is */
1949 /* update the buffering status */
1950 if (queue->use_buffering)
1951 update_buffering (queue);
1953 if (QUEUE_IS_USING_QUEUE (queue)) {
1954 g_queue_push_tail (&queue->queue, item);
1956 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
1959 GST_QUEUE2_SIGNAL_ADD (queue);
1968 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
1969 gst_event_type_get_name (GST_EVENT_TYPE (item)),
1970 GST_OBJECT_NAME (queue));
1971 gst_event_unref (GST_EVENT_CAST (item));
1976 /* dequeue an item from the queue and update level stats */
1977 static GstMiniObject *
1978 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
1980 GstMiniObject *item;
1982 if (!QUEUE_IS_USING_QUEUE (queue))
1983 item = gst_queue2_read_item_from_file (queue);
1985 item = g_queue_pop_head (&queue->queue);
1990 if (GST_IS_BUFFER (item)) {
1994 buffer = GST_BUFFER_CAST (item);
1995 size = gst_buffer_get_size (buffer);
1996 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
1998 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1999 "retrieved buffer %p from queue", buffer);
2001 if (QUEUE_IS_USING_QUEUE (queue)) {
2002 queue->cur_level.buffers--;
2003 queue->cur_level.bytes -= size;
2005 queue->bytes_out += size;
2007 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2008 /* update the byterate stats */
2009 update_out_rates (queue);
2010 /* update the buffering */
2011 if (queue->use_buffering)
2012 update_buffering (queue);
2014 } else if (GST_IS_EVENT (item)) {
2015 GstEvent *event = GST_EVENT_CAST (item);
2017 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2019 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2020 "retrieved event %p from queue", event);
2022 switch (GST_EVENT_TYPE (event)) {
2024 /* queue is empty now that we dequeued the EOS */
2025 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2027 case GST_EVENT_SEGMENT:
2028 apply_segment (queue, event, &queue->src_segment, FALSE);
2033 } else if (GST_IS_BUFFER_LIST (item)) {
2034 GstBufferList *buffer_list;
2037 buffer_list = GST_BUFFER_LIST_CAST (item);
2038 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2039 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2041 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2042 "retrieved buffer list %p from queue", buffer_list);
2044 if (QUEUE_IS_USING_QUEUE (queue)) {
2045 queue->cur_level.buffers--;
2046 queue->cur_level.bytes -= size;
2048 queue->bytes_out += size;
2050 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2051 /* update the byterate stats */
2052 update_out_rates (queue);
2053 /* update the buffering */
2054 if (queue->use_buffering)
2055 update_buffering (queue);
2059 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2060 item, GST_OBJECT_NAME (queue));
2062 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2064 GST_QUEUE2_SIGNAL_DEL (queue);
2071 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2077 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2082 queue = GST_QUEUE2 (parent);
2084 switch (GST_EVENT_TYPE (event)) {
2085 case GST_EVENT_FLUSH_START:
2087 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2088 if (QUEUE_IS_USING_QUEUE (queue)) {
2090 gst_pad_push_event (queue->srcpad, event);
2092 /* now unblock the chain function */
2093 GST_QUEUE2_MUTEX_LOCK (queue);
2094 queue->srcresult = GST_FLOW_FLUSHING;
2095 queue->sinkresult = GST_FLOW_FLUSHING;
2096 /* unblock the loop and chain functions */
2097 GST_QUEUE2_SIGNAL_ADD (queue);
2098 GST_QUEUE2_SIGNAL_DEL (queue);
2099 GST_QUEUE2_MUTEX_UNLOCK (queue);
2101 /* make sure it pauses, this should happen since we sent
2102 * flush_start downstream. */
2103 gst_pad_pause_task (queue->srcpad);
2104 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2106 GST_QUEUE2_MUTEX_LOCK (queue);
2107 /* flush the sink pad */
2108 queue->sinkresult = GST_FLOW_FLUSHING;
2109 GST_QUEUE2_SIGNAL_DEL (queue);
2110 GST_QUEUE2_MUTEX_UNLOCK (queue);
2112 gst_event_unref (event);
2116 case GST_EVENT_FLUSH_STOP:
2118 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2120 if (QUEUE_IS_USING_QUEUE (queue)) {
2122 gst_pad_push_event (queue->srcpad, event);
2124 GST_QUEUE2_MUTEX_LOCK (queue);
2125 gst_queue2_locked_flush (queue);
2126 queue->srcresult = GST_FLOW_OK;
2127 queue->sinkresult = GST_FLOW_OK;
2128 queue->is_eos = FALSE;
2129 queue->unexpected = FALSE;
2130 /* reset rate counters */
2131 reset_rate_timer (queue);
2132 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2134 GST_QUEUE2_MUTEX_UNLOCK (queue);
2136 GST_QUEUE2_MUTEX_LOCK (queue);
2137 queue->segment_event_received = FALSE;
2138 queue->is_eos = FALSE;
2139 queue->unexpected = FALSE;
2140 queue->sinkresult = GST_FLOW_OK;
2141 GST_QUEUE2_MUTEX_UNLOCK (queue);
2143 gst_event_unref (event);
2148 if (GST_EVENT_IS_SERIALIZED (event)) {
2149 /* serialized events go in the queue */
2150 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2151 /* refuse more events on EOS */
2154 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2155 GST_QUEUE2_MUTEX_UNLOCK (queue);
2157 /* non-serialized events are passed upstream. */
2158 gst_pad_push_event (queue->srcpad, event);
2168 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2169 GST_QUEUE2_MUTEX_UNLOCK (queue);
2170 gst_event_unref (event);
2175 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2176 GST_QUEUE2_MUTEX_UNLOCK (queue);
2177 gst_event_unref (event);
2183 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2188 switch (GST_QUERY_TYPE (query)) {
2190 if (GST_QUERY_IS_SERIALIZED (query)) {
2191 GST_WARNING_OBJECT (pad, "unhandled serialized query");
2194 res = gst_pad_query_default (pad, parent, query);
2202 gst_queue2_is_empty (GstQueue2 * queue)
2204 /* never empty on EOS */
2208 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2209 return queue->current->writing_pos <= queue->current->max_reading_pos;
2211 if (queue->queue.length == 0)
2219 gst_queue2_is_filled (GstQueue2 * queue)
2223 /* always filled on EOS */
2227 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2228 (queue->cur_level.format) >= ((alt_max) ? \
2229 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2231 /* if using a ring buffer we're filled if all ring buffer space is used
2232 * _by the current range_ */
2233 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2234 guint64 rb_size = queue->ring_buffer_max_size;
2235 GST_DEBUG_OBJECT (queue,
2236 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2237 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2238 return CHECK_FILLED (bytes, rb_size);
2241 /* if using file, we're never filled if we don't have EOS */
2242 if (QUEUE_IS_USING_TEMP_FILE (queue))
2245 /* we are never filled when we have no buffers at all */
2246 if (queue->cur_level.buffers == 0)
2249 /* we are filled if one of the current levels exceeds the max */
2250 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2251 || CHECK_FILLED (time, 0);
2253 /* if we need to, use the rate estimate to check against the max time we are
2254 * allowed to queue */
2255 if (queue->use_rate_estimate)
2256 res |= CHECK_FILLED (rate_time, 0);
2262 static GstFlowReturn
2263 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2264 GstMiniObject * item, GstQueue2ItemType item_type)
2266 /* we have to lock the queue since we span threads */
2267 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2268 /* when we received EOS, we refuse more data */
2271 /* when we received unexpected from downstream, refuse more buffers */
2272 if (queue->unexpected)
2273 goto out_unexpected;
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 received EOS");
2307 GST_QUEUE2_MUTEX_UNLOCK (queue);
2308 gst_mini_object_unref (item);
2310 return GST_FLOW_EOS;
2314 static GstFlowReturn
2315 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2319 queue = GST_QUEUE2 (parent);
2321 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2322 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2323 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2324 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2325 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2327 return gst_queue2_chain_buffer_or_buffer_list (queue,
2328 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2331 static GstFlowReturn
2332 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2333 GstBufferList * buffer_list)
2337 queue = GST_QUEUE2 (parent);
2339 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2340 "received buffer list %p", buffer_list);
2342 return gst_queue2_chain_buffer_or_buffer_list (queue,
2343 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2346 static GstMiniObject *
2347 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
2349 GstMiniObject *data;
2351 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
2353 /* stop pushing buffers, we dequeue all items until we see an item that we
2354 * can push again, which is EOS or SEGMENT. If there is nothing in the
2355 * queue we can push, we set a flag to make the sinkpad refuse more
2356 * buffers with an EOS return value until we receive something
2357 * pushable again or we get flushed. */
2358 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2359 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2360 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2361 "dropping EOS buffer %p", data);
2362 gst_buffer_unref (GST_BUFFER_CAST (data));
2363 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2364 GstEvent *event = GST_EVENT_CAST (data);
2365 GstEventType type = GST_EVENT_TYPE (event);
2367 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2368 /* we found a pushable item in the queue, push it out */
2369 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2370 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
2373 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2374 "dropping EOS event %p", event);
2375 gst_event_unref (event);
2376 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2377 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2378 "dropping EOS buffer list %p", data);
2379 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2382 /* no more items in the queue. Set the unexpected flag so that upstream
2383 * make us refuse any more buffers on the sinkpad. Since we will still
2384 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2385 * task function does not shut down. */
2386 queue->unexpected = TRUE;
2390 /* dequeue an item from the queue an push it downstream. This functions returns
2391 * the result of the push. */
2392 static GstFlowReturn
2393 gst_queue2_push_one (GstQueue2 * queue)
2395 GstFlowReturn result = GST_FLOW_OK;
2396 GstMiniObject *data;
2397 GstQueue2ItemType item_type;
2399 data = gst_queue2_locked_dequeue (queue, &item_type);
2404 GST_QUEUE2_MUTEX_UNLOCK (queue);
2406 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2412 buffer = GST_BUFFER_CAST (data);
2414 caps = GST_BUFFER_CAPS (buffer);
2418 /* set caps before pushing the buffer so that core does not try to do
2419 * something fancy to check if this is possible. */
2420 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2421 gst_pad_set_caps (queue->srcpad, caps);
2424 result = gst_pad_push (queue->srcpad, buffer);
2426 /* need to check for srcresult here as well */
2427 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2428 if (result == GST_FLOW_EOS) {
2429 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2432 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2433 * to the caller so that the task function does not shut down */
2434 result = GST_FLOW_OK;
2436 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2437 GstEvent *event = GST_EVENT_CAST (data);
2438 GstEventType type = GST_EVENT_TYPE (event);
2440 gst_pad_push_event (queue->srcpad, event);
2442 /* if we're EOS, return EOS so that the task pauses. */
2443 if (type == GST_EVENT_EOS) {
2444 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2445 "pushed EOS event %p, return EOS", event);
2446 result = GST_FLOW_EOS;
2449 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2450 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2451 GstBufferList *buffer_list;
2453 GstBuffer *first_buf;
2457 buffer_list = GST_BUFFER_LIST_CAST (data);
2460 first_buf = gst_buffer_list_get (buffer_list, 0);
2461 caps = (first_buf != NULL) ? GST_BUFFER_CAPS (first_buf) : NULL;
2463 /* set caps before pushing the buffer so that core does not try to do
2464 * something fancy to check if this is possible. */
2465 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2466 gst_pad_set_caps (queue->srcpad, caps);
2469 result = gst_pad_push_list (queue->srcpad, buffer_list);
2471 /* need to check for srcresult here as well */
2472 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2473 if (result == GST_FLOW_EOS) {
2474 data = gst_queue2_dequeue_on_eos (queue, &item_type);
2477 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
2478 * to the caller so that the task function does not shut down */
2479 result = GST_FLOW_OK;
2487 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2488 "exit because we have no item in the queue");
2489 return GST_FLOW_ERROR;
2493 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2494 return GST_FLOW_FLUSHING;
2498 /* called repeatedly with @pad as the source pad. This function should push out
2499 * data to the peer element. */
2501 gst_queue2_loop (GstPad * pad)
2506 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2508 /* have to lock for thread-safety */
2509 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2511 if (gst_queue2_is_empty (queue)) {
2514 /* pause the timer while we wait. The fact that we are waiting does not mean
2515 * the byterate on the output pad is lower */
2516 if ((started = queue->out_timer_started))
2517 g_timer_stop (queue->out_timer);
2519 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2520 "queue is empty, waiting for new data");
2522 /* Wait for data to be available, we could be unlocked because of a flush. */
2523 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2525 while (gst_queue2_is_empty (queue));
2527 /* and continue if we were running before */
2529 g_timer_continue (queue->out_timer);
2531 ret = gst_queue2_push_one (queue);
2532 queue->srcresult = ret;
2533 queue->sinkresult = ret;
2534 if (ret != GST_FLOW_OK)
2537 GST_QUEUE2_MUTEX_UNLOCK (queue);
2544 gboolean eos = queue->is_eos;
2545 GstFlowReturn ret = queue->srcresult;
2547 gst_pad_pause_task (queue->srcpad);
2548 GST_QUEUE2_MUTEX_UNLOCK (queue);
2549 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2550 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2551 /* let app know about us giving up if upstream is not expected to do so */
2552 /* EOS is already taken care of elsewhere */
2553 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
2554 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2555 (_("Internal data flow error.")),
2556 ("streaming task paused, reason %s (%d)",
2557 gst_flow_get_name (ret), ret));
2558 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2565 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
2567 gboolean res = TRUE;
2568 GstQueue2 *queue = GST_QUEUE2 (parent);
2570 #ifndef GST_DISABLE_GST_DEBUG
2571 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2572 event, GST_EVENT_TYPE_NAME (event));
2575 switch (GST_EVENT_TYPE (event)) {
2576 case GST_EVENT_FLUSH_START:
2577 if (QUEUE_IS_USING_QUEUE (queue)) {
2578 /* just forward upstream */
2579 res = gst_pad_push_event (queue->sinkpad, event);
2581 /* now unblock the getrange function */
2582 GST_QUEUE2_MUTEX_LOCK (queue);
2583 GST_DEBUG_OBJECT (queue, "flushing");
2584 queue->srcresult = GST_FLOW_FLUSHING;
2585 GST_QUEUE2_SIGNAL_ADD (queue);
2586 GST_QUEUE2_MUTEX_UNLOCK (queue);
2588 /* when using a temp file, we eat the event */
2590 gst_event_unref (event);
2593 case GST_EVENT_FLUSH_STOP:
2594 if (QUEUE_IS_USING_QUEUE (queue)) {
2595 /* just forward upstream */
2596 res = gst_pad_push_event (queue->sinkpad, event);
2598 /* now unblock the getrange function */
2599 GST_QUEUE2_MUTEX_LOCK (queue);
2600 queue->srcresult = GST_FLOW_OK;
2601 if (queue->current) {
2602 /* forget the highest read offset, we'll calculate a new one when we
2603 * get the next getrange request. We need to do this in order to reset
2604 * the buffering percentage */
2605 queue->current->max_reading_pos = 0;
2607 GST_QUEUE2_MUTEX_UNLOCK (queue);
2609 /* when using a temp file, we eat the event */
2611 gst_event_unref (event);
2615 res = gst_pad_push_event (queue->sinkpad, event);
2623 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
2627 queue = GST_QUEUE2 (parent);
2629 switch (GST_QUERY_TYPE (query)) {
2630 case GST_QUERY_POSITION:
2635 if (!gst_pad_peer_query (queue->sinkpad, query))
2638 /* get peer position */
2639 gst_query_parse_position (query, &format, &peer_pos);
2641 /* FIXME: this code assumes that there's no discont in the queue */
2643 case GST_FORMAT_BYTES:
2644 peer_pos -= queue->cur_level.bytes;
2646 case GST_FORMAT_TIME:
2647 peer_pos -= queue->cur_level.time;
2650 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2651 "know how to adjust value", gst_format_get_name (format));
2654 /* set updated position */
2655 gst_query_set_position (query, format, peer_pos);
2658 case GST_QUERY_DURATION:
2660 GST_DEBUG_OBJECT (queue, "doing peer query");
2662 if (!gst_pad_peer_query (queue->sinkpad, query))
2665 GST_DEBUG_OBJECT (queue, "peer query success");
2668 case GST_QUERY_BUFFERING:
2672 GST_DEBUG_OBJECT (queue, "query buffering");
2674 /* FIXME - is this condition correct? what should ring buffer do? */
2675 if (QUEUE_IS_USING_QUEUE (queue)) {
2676 /* no temp file, just forward to the peer */
2677 if (!gst_pad_peer_query (queue->sinkpad, query))
2679 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2681 gint64 start, stop, range_start, range_stop;
2682 guint64 writing_pos;
2684 gint64 estimated_total, buffering_left;
2686 gboolean peer_res, is_buffering, is_eos;
2687 gdouble byte_in_rate, byte_out_rate;
2688 GstQueue2Range *queued_ranges;
2690 /* we need a current download region */
2691 if (queue->current == NULL)
2694 writing_pos = queue->current->writing_pos;
2695 byte_in_rate = queue->byte_in_rate;
2696 byte_out_rate = queue->byte_out_rate;
2697 is_buffering = queue->is_buffering;
2698 is_eos = queue->is_eos;
2699 percent = queue->buffering_percent;
2702 /* we're EOS, we know the duration in bytes now */
2704 duration = writing_pos;
2706 /* get duration of upstream in bytes */
2707 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
2708 GST_FORMAT_BYTES, &duration);
2711 /* calculate remaining and total download time */
2712 if (peer_res && byte_in_rate > 0.0) {
2713 estimated_total = (duration * 1000) / byte_in_rate;
2714 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2716 estimated_total = -1;
2717 buffering_left = -1;
2719 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2720 G_GINT64_FORMAT, estimated_total, buffering_left);
2722 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2725 case GST_FORMAT_PERCENT:
2726 /* we need duration */
2730 GST_DEBUG_OBJECT (queue,
2731 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2732 duration, writing_pos);
2735 /* get our available data relative to the duration */
2737 stop = GST_FORMAT_PERCENT_MAX * writing_pos / duration;
2741 case GST_FORMAT_BYTES:
2751 /* fill out the buffered ranges */
2752 for (queued_ranges = queue->ranges; queued_ranges;
2753 queued_ranges = queued_ranges->next) {
2755 case GST_FORMAT_PERCENT:
2756 if (duration == -1) {
2761 range_start = 100 * queued_ranges->offset / duration;
2762 range_stop = 100 * queued_ranges->writing_pos / duration;
2764 case GST_FORMAT_BYTES:
2765 range_start = queued_ranges->offset;
2766 range_stop = queued_ranges->writing_pos;
2773 if (range_start == range_stop)
2775 GST_DEBUG_OBJECT (queue,
2776 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2777 G_GINT64_FORMAT, range_start, range_stop);
2778 gst_query_add_buffering_range (query, range_start, range_stop);
2781 gst_query_set_buffering_percent (query, is_buffering, percent);
2782 gst_query_set_buffering_range (query, format, start, stop,
2784 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2785 byte_in_rate, byte_out_rate, buffering_left);
2789 case GST_QUERY_SCHEDULING:
2792 GstSchedulingFlags flags = 0;
2794 /* we can operate in pull mode when we are using a tempfile */
2795 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2798 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
2799 gst_query_set_scheduling (query, flags, 0, -1, 0);
2801 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
2802 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
2806 /* peer handled other queries */
2807 if (!gst_pad_query_default (pad, parent, query))
2817 GST_DEBUG_OBJECT (queue, "failed peer query");
2823 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2825 GstQueue2 *queue = GST_QUEUE2 (element);
2827 /* simply forward to the srcpad query function */
2828 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
2833 gst_queue2_update_upstream_size (GstQueue2 * queue)
2835 gint64 upstream_size = -1;
2837 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
2839 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2840 queue->upstream_size = upstream_size;
2844 static GstFlowReturn
2845 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
2846 guint length, GstBuffer ** buffer)
2851 queue = GST_QUEUE2_CAST (parent);
2853 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2854 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2855 offset = (offset == -1) ? queue->current->reading_pos : offset;
2857 GST_DEBUG_OBJECT (queue,
2858 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2860 /* catch any reads beyond the size of the file here to make sure queue2
2861 * doesn't send seek events beyond the size of the file upstream, since
2862 * that would confuse elements such as souphttpsrc and/or http servers.
2863 * Demuxers often just loop until EOS at the end of the file to figure out
2864 * when they've read all the end-headers or index chunks. */
2865 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2866 gst_queue2_update_upstream_size (queue);
2867 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2868 goto out_unexpected;
2871 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2872 gst_queue2_update_upstream_size (queue);
2873 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2874 length = queue->upstream_size - offset;
2875 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2879 /* FIXME - function will block when the range is not yet available */
2880 ret = gst_queue2_create_read (queue, offset, length, buffer);
2881 GST_QUEUE2_MUTEX_UNLOCK (queue);
2888 ret = queue->srcresult;
2890 GST_DEBUG_OBJECT (queue, "we are flushing");
2891 GST_QUEUE2_MUTEX_UNLOCK (queue);
2896 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2897 GST_QUEUE2_MUTEX_UNLOCK (queue);
2898 return GST_FLOW_EOS;
2902 /* sink currently only operates in push mode */
2904 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
2905 GstPadMode mode, gboolean active)
2910 queue = GST_QUEUE2 (parent);
2913 case GST_PAD_MODE_PUSH:
2915 GST_QUEUE2_MUTEX_LOCK (queue);
2916 GST_DEBUG_OBJECT (queue, "activating push mode");
2917 queue->srcresult = GST_FLOW_OK;
2918 queue->sinkresult = GST_FLOW_OK;
2919 queue->is_eos = FALSE;
2920 queue->unexpected = FALSE;
2921 reset_rate_timer (queue);
2922 GST_QUEUE2_MUTEX_UNLOCK (queue);
2924 /* unblock chain function */
2925 GST_QUEUE2_MUTEX_LOCK (queue);
2926 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2927 queue->srcresult = GST_FLOW_FLUSHING;
2928 queue->sinkresult = GST_FLOW_FLUSHING;
2929 gst_queue2_locked_flush (queue);
2930 GST_QUEUE2_MUTEX_UNLOCK (queue);
2941 /* src operating in push mode, we start a task on the source pad that pushes out
2942 * buffers from the queue */
2944 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
2946 gboolean result = FALSE;
2949 queue = GST_QUEUE2 (parent);
2952 GST_QUEUE2_MUTEX_LOCK (queue);
2953 GST_DEBUG_OBJECT (queue, "activating push mode");
2954 queue->srcresult = GST_FLOW_OK;
2955 queue->sinkresult = GST_FLOW_OK;
2956 queue->is_eos = FALSE;
2957 queue->unexpected = FALSE;
2958 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
2959 GST_QUEUE2_MUTEX_UNLOCK (queue);
2961 /* unblock loop function */
2962 GST_QUEUE2_MUTEX_LOCK (queue);
2963 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2964 queue->srcresult = GST_FLOW_FLUSHING;
2965 queue->sinkresult = GST_FLOW_FLUSHING;
2966 /* the item add signal will unblock */
2967 GST_QUEUE2_SIGNAL_ADD (queue);
2968 GST_QUEUE2_MUTEX_UNLOCK (queue);
2970 /* step 2, make sure streaming finishes */
2971 result = gst_pad_stop_task (pad);
2977 /* pull mode, downstream will call our getrange function */
2979 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
2984 queue = GST_QUEUE2 (parent);
2987 GST_QUEUE2_MUTEX_LOCK (queue);
2988 if (!QUEUE_IS_USING_QUEUE (queue)) {
2989 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2990 /* open the temp file now */
2991 result = gst_queue2_open_temp_location_file (queue);
2992 } else if (!queue->ring_buffer) {
2993 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
2994 result = ! !queue->ring_buffer;
2999 GST_DEBUG_OBJECT (queue, "activating pull mode");
3000 init_ranges (queue);
3001 queue->srcresult = GST_FLOW_OK;
3002 queue->sinkresult = GST_FLOW_OK;
3003 queue->is_eos = FALSE;
3004 queue->unexpected = FALSE;
3005 queue->upstream_size = 0;
3007 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3008 /* this is not allowed, we cannot operate in pull mode without a temp
3010 queue->srcresult = GST_FLOW_FLUSHING;
3011 queue->sinkresult = GST_FLOW_FLUSHING;
3014 GST_QUEUE2_MUTEX_UNLOCK (queue);
3016 GST_QUEUE2_MUTEX_LOCK (queue);
3017 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3018 queue->srcresult = GST_FLOW_FLUSHING;
3019 queue->sinkresult = GST_FLOW_FLUSHING;
3020 /* this will unlock getrange */
3021 GST_QUEUE2_SIGNAL_ADD (queue);
3023 GST_QUEUE2_MUTEX_UNLOCK (queue);
3030 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3036 case GST_PAD_MODE_PULL:
3037 res = gst_queue2_src_activate_pull (pad, parent, active);
3039 case GST_PAD_MODE_PUSH:
3040 res = gst_queue2_src_activate_push (pad, parent, active);
3043 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3050 static GstStateChangeReturn
3051 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3054 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3056 queue = GST_QUEUE2 (element);
3058 switch (transition) {
3059 case GST_STATE_CHANGE_NULL_TO_READY:
3061 case GST_STATE_CHANGE_READY_TO_PAUSED:
3062 GST_QUEUE2_MUTEX_LOCK (queue);
3063 if (!QUEUE_IS_USING_QUEUE (queue)) {
3064 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3065 if (!gst_queue2_open_temp_location_file (queue))
3066 ret = GST_STATE_CHANGE_FAILURE;
3068 if (queue->ring_buffer) {
3069 g_free (queue->ring_buffer);
3070 queue->ring_buffer = NULL;
3072 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3073 ret = GST_STATE_CHANGE_FAILURE;
3075 init_ranges (queue);
3077 queue->segment_event_received = FALSE;
3078 queue->starting_segment = NULL;
3079 gst_event_replace (&queue->stream_start_event, NULL);
3080 GST_QUEUE2_MUTEX_UNLOCK (queue);
3082 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3088 if (ret == GST_STATE_CHANGE_FAILURE)
3091 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3093 if (ret == GST_STATE_CHANGE_FAILURE)
3096 switch (transition) {
3097 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3099 case GST_STATE_CHANGE_PAUSED_TO_READY:
3100 GST_QUEUE2_MUTEX_LOCK (queue);
3101 if (!QUEUE_IS_USING_QUEUE (queue)) {
3102 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3103 gst_queue2_close_temp_location_file (queue);
3104 } else if (queue->ring_buffer) {
3105 g_free (queue->ring_buffer);
3106 queue->ring_buffer = NULL;
3108 clean_ranges (queue);
3110 if (queue->starting_segment != NULL) {
3111 gst_event_unref (queue->starting_segment);
3112 queue->starting_segment = NULL;
3114 gst_event_replace (&queue->stream_start_event, NULL);
3115 GST_QUEUE2_MUTEX_UNLOCK (queue);
3117 case GST_STATE_CHANGE_READY_TO_NULL:
3126 /* changing the capacity of the queue must wake up
3127 * the _chain function, it might have more room now
3128 * to store the buffer/event in the queue */
3129 #define QUEUE_CAPACITY_CHANGE(q)\
3130 GST_QUEUE2_SIGNAL_DEL (queue);
3132 /* Changing the minimum required fill level must
3133 * wake up the _loop function as it might now
3134 * be able to preceed.
3136 #define QUEUE_THRESHOLD_CHANGE(q)\
3137 GST_QUEUE2_SIGNAL_ADD (queue);
3140 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3144 /* the element must be stopped in order to do this */
3145 GST_OBJECT_LOCK (queue);
3146 state = GST_STATE (queue);
3147 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3149 GST_OBJECT_UNLOCK (queue);
3151 /* set new location */
3152 g_free (queue->temp_template);
3153 queue->temp_template = g_strdup (template);
3160 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3161 GST_OBJECT_UNLOCK (queue);
3166 gst_queue2_set_property (GObject * object,
3167 guint prop_id, const GValue * value, GParamSpec * pspec)
3169 GstQueue2 *queue = GST_QUEUE2 (object);
3171 /* someone could change levels here, and since this
3172 * affects the get/put funcs, we need to lock for safety. */
3173 GST_QUEUE2_MUTEX_LOCK (queue);
3176 case PROP_MAX_SIZE_BYTES:
3177 queue->max_level.bytes = g_value_get_uint (value);
3178 QUEUE_CAPACITY_CHANGE (queue);
3180 case PROP_MAX_SIZE_BUFFERS:
3181 queue->max_level.buffers = g_value_get_uint (value);
3182 QUEUE_CAPACITY_CHANGE (queue);
3184 case PROP_MAX_SIZE_TIME:
3185 queue->max_level.time = g_value_get_uint64 (value);
3186 /* set rate_time to the same value. We use an extra field in the level
3187 * structure so that we can easily access and compare it */
3188 queue->max_level.rate_time = queue->max_level.time;
3189 QUEUE_CAPACITY_CHANGE (queue);
3191 case PROP_USE_BUFFERING:
3192 queue->use_buffering = g_value_get_boolean (value);
3194 case PROP_USE_RATE_ESTIMATE:
3195 queue->use_rate_estimate = g_value_get_boolean (value);
3197 case PROP_LOW_PERCENT:
3198 queue->low_percent = g_value_get_int (value);
3200 case PROP_HIGH_PERCENT:
3201 queue->high_percent = g_value_get_int (value);
3203 case PROP_TEMP_TEMPLATE:
3204 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3206 case PROP_TEMP_LOCATION:
3207 g_free (queue->temp_location);
3208 queue->temp_location = g_value_dup_string (value);
3209 /* you can set the property back to NULL to make it use the temp-tmpl
3211 queue->temp_location_set = queue->temp_location != NULL;
3213 case PROP_TEMP_REMOVE:
3214 queue->temp_remove = g_value_get_boolean (value);
3216 case PROP_RING_BUFFER_MAX_SIZE:
3217 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3224 GST_QUEUE2_MUTEX_UNLOCK (queue);
3228 gst_queue2_get_property (GObject * object,
3229 guint prop_id, GValue * value, GParamSpec * pspec)
3231 GstQueue2 *queue = GST_QUEUE2 (object);
3233 GST_QUEUE2_MUTEX_LOCK (queue);
3236 case PROP_CUR_LEVEL_BYTES:
3237 g_value_set_uint (value, queue->cur_level.bytes);
3239 case PROP_CUR_LEVEL_BUFFERS:
3240 g_value_set_uint (value, queue->cur_level.buffers);
3242 case PROP_CUR_LEVEL_TIME:
3243 g_value_set_uint64 (value, queue->cur_level.time);
3245 case PROP_MAX_SIZE_BYTES:
3246 g_value_set_uint (value, queue->max_level.bytes);
3248 case PROP_MAX_SIZE_BUFFERS:
3249 g_value_set_uint (value, queue->max_level.buffers);
3251 case PROP_MAX_SIZE_TIME:
3252 g_value_set_uint64 (value, queue->max_level.time);
3254 case PROP_USE_BUFFERING:
3255 g_value_set_boolean (value, queue->use_buffering);
3257 case PROP_USE_RATE_ESTIMATE:
3258 g_value_set_boolean (value, queue->use_rate_estimate);
3260 case PROP_LOW_PERCENT:
3261 g_value_set_int (value, queue->low_percent);
3263 case PROP_HIGH_PERCENT:
3264 g_value_set_int (value, queue->high_percent);
3266 case PROP_TEMP_TEMPLATE:
3267 g_value_set_string (value, queue->temp_template);
3269 case PROP_TEMP_LOCATION:
3270 g_value_set_string (value, queue->temp_location);
3272 case PROP_TEMP_REMOVE:
3273 g_value_set_boolean (value, queue->temp_remove);
3275 case PROP_RING_BUFFER_MAX_SIZE:
3276 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3283 GST_QUEUE2_MUTEX_UNLOCK (queue);