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"
70 #include <io.h> /* lseek, open, close, read */
72 #define lseek _lseeki64
79 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
84 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
89 GST_DEBUG_CATEGORY_STATIC (queue_debug);
90 #define GST_CAT_DEFAULT (queue_debug)
91 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
99 #define DEFAULT_BUFFER_SIZE 4096
100 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_location_set || (queue)->temp_template != NULL)
101 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
102 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
104 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
106 /* default property values */
107 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
108 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
109 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
110 #define DEFAULT_USE_BUFFERING FALSE
111 #define DEFAULT_USE_RATE_ESTIMATE TRUE
112 #define DEFAULT_LOW_PERCENT 10
113 #define DEFAULT_HIGH_PERCENT 99
114 #define DEFAULT_TEMP_REMOVE TRUE
115 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
120 PROP_CUR_LEVEL_BUFFERS,
121 PROP_CUR_LEVEL_BYTES,
123 PROP_MAX_SIZE_BUFFERS,
127 PROP_USE_RATE_ESTIMATE,
133 PROP_RING_BUFFER_MAX_SIZE,
137 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
144 #define STATUS(queue, pad, msg) \
145 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
146 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
147 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
148 " ns, %"G_GUINT64_FORMAT" items", \
149 GST_DEBUG_PAD_NAME (pad), \
150 queue->cur_level.buffers, \
151 queue->max_level.buffers, \
152 queue->cur_level.bytes, \
153 queue->max_level.bytes, \
154 queue->cur_level.time, \
155 queue->max_level.time, \
156 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
157 queue->current->writing_pos - queue->current->max_reading_pos : \
158 queue->queue->length))
160 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
161 g_mutex_lock (q->qlock); \
164 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
165 GST_QUEUE2_MUTEX_LOCK (q); \
166 if (res != GST_FLOW_OK) \
170 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
171 g_mutex_unlock (q->qlock); \
174 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
175 STATUS (queue, q->sinkpad, "wait for DEL"); \
176 q->waiting_del = TRUE; \
177 g_cond_wait (q->item_del, queue->qlock); \
178 q->waiting_del = FALSE; \
179 if (res != GST_FLOW_OK) { \
180 STATUS (queue, q->srcpad, "received DEL wakeup"); \
183 STATUS (queue, q->sinkpad, "received DEL"); \
186 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
187 STATUS (queue, q->srcpad, "wait for ADD"); \
188 q->waiting_add = TRUE; \
189 g_cond_wait (q->item_add, q->qlock); \
190 q->waiting_add = FALSE; \
191 if (res != GST_FLOW_OK) { \
192 STATUS (queue, q->srcpad, "received ADD wakeup"); \
195 STATUS (queue, q->srcpad, "received ADD"); \
198 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
199 if (q->waiting_del) { \
200 STATUS (q, q->srcpad, "signal DEL"); \
201 g_cond_signal (q->item_del); \
205 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
206 if (q->waiting_add) { \
207 STATUS (q, q->sinkpad, "signal ADD"); \
208 g_cond_signal (q->item_add); \
213 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
214 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
215 "dataflow inside the queue element");
216 #define gst_queue2_parent_class parent_class
217 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
219 static void gst_queue2_finalize (GObject * object);
221 static void gst_queue2_set_property (GObject * object,
222 guint prop_id, const GValue * value, GParamSpec * pspec);
223 static void gst_queue2_get_property (GObject * object,
224 guint prop_id, GValue * value, GParamSpec * pspec);
226 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstBuffer * buffer);
227 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
228 static void gst_queue2_loop (GstPad * pad);
230 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event);
232 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstEvent * event);
233 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstQuery * query);
234 static gboolean gst_queue2_handle_query (GstElement * element,
237 static GstCaps *gst_queue2_getcaps (GstPad * pad, GstCaps * filter);
238 static gboolean gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps);
240 static GstFlowReturn gst_queue2_get_range (GstPad * pad, guint64 offset,
241 guint length, GstBuffer ** buffer);
243 static gboolean gst_queue2_src_activate_pull (GstPad * pad, gboolean active);
244 static gboolean gst_queue2_src_activate_push (GstPad * pad, gboolean active);
245 static gboolean gst_queue2_sink_activate_push (GstPad * pad, gboolean active);
246 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
247 GstStateChange transition);
249 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
250 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
252 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
254 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
257 gst_queue2_class_init (GstQueue2Class * klass)
259 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
260 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
262 gobject_class->set_property = gst_queue2_set_property;
263 gobject_class->get_property = gst_queue2_get_property;
266 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
267 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
268 "Current amount of data in the queue (bytes)",
269 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
270 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
271 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
272 "Current number of buffers in the queue",
273 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
275 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
276 "Current amount of data in the queue (in ns)",
277 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
280 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
281 "Max. amount of data in the queue (bytes, 0=disable)",
282 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
283 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
285 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
286 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
287 DEFAULT_MAX_SIZE_BUFFERS,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
290 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
291 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
292 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
295 g_param_spec_boolean ("use-buffering", "Use buffering",
296 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
297 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
298 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
299 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
300 "Estimate the bitrate of the stream to calculate time level",
301 DEFAULT_USE_RATE_ESTIMATE,
302 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
304 g_param_spec_int ("low-percent", "Low percent",
305 "Low threshold for buffering to start. Only used if use-buffering is True",
306 0, 100, DEFAULT_LOW_PERCENT,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
309 g_param_spec_int ("high-percent", "High percent",
310 "High threshold for buffering to finish. Only used if use-buffering is True",
311 0, 100, DEFAULT_HIGH_PERCENT,
312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
315 g_param_spec_string ("temp-template", "Temporary File Template",
316 "File template to store temporary files in, should contain directory "
317 "and XXXXXX. (NULL == disabled)",
318 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
320 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
321 g_param_spec_string ("temp-location", "Temporary File Location",
322 "Location to store temporary files in (Deprecated: Only read this "
323 "property, use temp-template to configure the name template)",
324 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 * GstQueue2:temp-remove
329 * When temp-template is set, remove the temporary file when going to READY.
333 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
334 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
335 "Remove the temp-location after use",
336 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 * GstQueue2:ring-buffer-max-size
341 * The maximum size of the ring buffer in bytes. If set to 0, the ring
342 * buffer is disabled. Default 0.
346 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
347 g_param_spec_uint64 ("ring-buffer-max-size",
348 "Max. ring buffer size (bytes)",
349 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
350 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
351 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353 /* set several parent class virtual functions */
354 gobject_class->finalize = gst_queue2_finalize;
356 gst_element_class_add_pad_template (gstelement_class,
357 gst_static_pad_template_get (&srctemplate));
358 gst_element_class_add_pad_template (gstelement_class,
359 gst_static_pad_template_get (&sinktemplate));
361 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
364 "Erik Walthinsen <omega@cse.ogi.edu>, "
365 "Wim Taymans <wim.taymans@gmail.com>");
367 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
368 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
372 gst_queue2_init (GstQueue2 * queue)
374 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
376 gst_pad_set_chain_function (queue->sinkpad,
377 GST_DEBUG_FUNCPTR (gst_queue2_chain));
378 gst_pad_set_activatepush_function (queue->sinkpad,
379 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_push));
380 gst_pad_set_event_function (queue->sinkpad,
381 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
382 gst_pad_set_getcaps_function (queue->sinkpad,
383 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
384 gst_pad_set_acceptcaps_function (queue->sinkpad,
385 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
386 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
388 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
390 gst_pad_set_activatepull_function (queue->srcpad,
391 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_pull));
392 gst_pad_set_activatepush_function (queue->srcpad,
393 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_push));
394 gst_pad_set_getrange_function (queue->srcpad,
395 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
396 gst_pad_set_getcaps_function (queue->srcpad,
397 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
398 gst_pad_set_acceptcaps_function (queue->srcpad,
399 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
400 gst_pad_set_event_function (queue->srcpad,
401 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
402 gst_pad_set_query_function (queue->srcpad,
403 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
404 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
407 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
408 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
409 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
410 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
411 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
412 queue->use_buffering = DEFAULT_USE_BUFFERING;
413 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
414 queue->low_percent = DEFAULT_LOW_PERCENT;
415 queue->high_percent = DEFAULT_HIGH_PERCENT;
417 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
418 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
420 queue->sinktime = GST_CLOCK_TIME_NONE;
421 queue->srctime = GST_CLOCK_TIME_NONE;
422 queue->sink_tainted = TRUE;
423 queue->src_tainted = TRUE;
425 queue->srcresult = GST_FLOW_WRONG_STATE;
426 queue->sinkresult = GST_FLOW_WRONG_STATE;
427 queue->is_eos = FALSE;
428 queue->in_timer = g_timer_new ();
429 queue->out_timer = g_timer_new ();
431 queue->qlock = g_mutex_new ();
432 queue->waiting_add = FALSE;
433 queue->item_add = g_cond_new ();
434 queue->waiting_del = FALSE;
435 queue->item_del = g_cond_new ();
436 queue->queue = g_queue_new ();
438 queue->buffering_percent = 100;
440 /* tempfile related */
441 queue->temp_template = NULL;
442 queue->temp_location = NULL;
443 queue->temp_location_set = FALSE;
444 queue->temp_remove = DEFAULT_TEMP_REMOVE;
446 queue->ring_buffer = NULL;
447 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
449 GST_DEBUG_OBJECT (queue,
450 "initialized queue's not_empty & not_full conditions");
453 /* called only once, as opposed to dispose */
455 gst_queue2_finalize (GObject * object)
457 GstQueue2 *queue = GST_QUEUE2 (object);
459 GST_DEBUG_OBJECT (queue, "finalizing queue");
461 while (!g_queue_is_empty (queue->queue)) {
462 GstMiniObject *data = g_queue_pop_head (queue->queue);
464 gst_mini_object_unref (data);
467 g_queue_free (queue->queue);
468 g_mutex_free (queue->qlock);
469 g_cond_free (queue->item_add);
470 g_cond_free (queue->item_del);
471 g_timer_destroy (queue->in_timer);
472 g_timer_destroy (queue->out_timer);
474 /* temp_file path cleanup */
475 g_free (queue->temp_template);
476 g_free (queue->temp_location);
478 G_OBJECT_CLASS (parent_class)->finalize (object);
482 debug_ranges (GstQueue2 * queue)
484 GstQueue2Range *walk;
486 for (walk = queue->ranges; walk; walk = walk->next) {
487 GST_DEBUG_OBJECT (queue,
488 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
489 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
490 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
491 walk->rb_writing_pos, walk->reading_pos,
492 walk == queue->current ? "**y**" : " n ");
496 /* clear all the downloaded ranges */
498 clean_ranges (GstQueue2 * queue)
500 GST_DEBUG_OBJECT (queue, "clean queue ranges");
502 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
503 queue->ranges = NULL;
504 queue->current = NULL;
507 /* find a range that contains @offset or NULL when nothing does */
508 static GstQueue2Range *
509 find_range (GstQueue2 * queue, guint64 offset)
511 GstQueue2Range *range = NULL;
512 GstQueue2Range *walk;
514 /* first do a quick check for the current range */
515 for (walk = queue->ranges; walk; walk = walk->next) {
516 if (offset >= walk->offset && offset <= walk->writing_pos) {
517 /* we can reuse an existing range */
523 GST_DEBUG_OBJECT (queue,
524 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
525 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
527 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
533 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
535 guint64 max_reading_pos, writing_pos;
537 writing_pos = range->writing_pos;
538 max_reading_pos = range->max_reading_pos;
540 if (writing_pos > max_reading_pos)
541 queue->cur_level.bytes = writing_pos - max_reading_pos;
543 queue->cur_level.bytes = 0;
546 /* make a new range for @offset or reuse an existing range */
547 static GstQueue2Range *
548 add_range (GstQueue2 * queue, guint64 offset)
550 GstQueue2Range *range, *prev, *next;
552 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
554 if ((range = find_range (queue, offset))) {
555 GST_DEBUG_OBJECT (queue,
556 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
558 range->writing_pos = offset;
560 GST_DEBUG_OBJECT (queue,
561 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
563 range = g_slice_new0 (GstQueue2Range);
564 range->offset = offset;
565 /* we want to write to the next location in the ring buffer */
566 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
567 range->writing_pos = offset;
568 range->rb_writing_pos = range->rb_offset;
569 range->reading_pos = offset;
570 range->max_reading_pos = offset;
574 next = queue->ranges;
576 if (next->offset > offset) {
577 /* insert before next */
578 GST_DEBUG_OBJECT (queue,
579 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
591 queue->ranges = range;
593 debug_ranges (queue);
595 /* update the stats for this range */
596 update_cur_level (queue, range);
602 /* clear and init the download ranges for offset 0 */
604 init_ranges (GstQueue2 * queue)
606 GST_DEBUG_OBJECT (queue, "init queue ranges");
608 /* get rid of all the current ranges */
609 clean_ranges (queue);
610 /* make a range for offset 0 */
611 queue->current = add_range (queue, 0);
615 gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps)
621 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
623 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
624 result = gst_pad_peer_accept_caps (otherpad, caps);
630 gst_queue2_getcaps (GstPad * pad, GstCaps * filter)
636 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
637 if (G_UNLIKELY (queue == NULL))
638 return (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
640 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
641 result = gst_pad_peer_get_caps (otherpad, filter);
643 result = (filter ? gst_caps_ref (filter) : gst_caps_new_any ());
645 gst_object_unref (queue);
650 /* calculate the diff between running time on the sink and src of the queue.
651 * This is the total amount of time in the queue. */
653 update_time_level (GstQueue2 * queue)
655 if (queue->sink_tainted) {
657 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
658 queue->sink_segment.position);
659 queue->sink_tainted = FALSE;
662 if (queue->src_tainted) {
664 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
665 queue->src_segment.position);
666 queue->src_tainted = FALSE;
669 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
670 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
672 if (queue->sinktime != GST_CLOCK_TIME_NONE
673 && queue->srctime != GST_CLOCK_TIME_NONE
674 && queue->sinktime >= queue->srctime)
675 queue->cur_level.time = queue->sinktime - queue->srctime;
677 queue->cur_level.time = 0;
680 /* take a SEGMENT event and apply the values to segment, updating the time
683 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
686 gst_event_copy_segment (event, segment);
688 if (segment->format == GST_FORMAT_BYTES) {
689 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
690 /* start is where we'll be getting from and as such writing next */
691 queue->current = add_range (queue, segment->start);
692 /* update the stats for this range */
693 update_cur_level (queue, queue->current);
697 /* now configure the values, we use these to track timestamps on the
699 if (segment->format != GST_FORMAT_TIME) {
700 /* non-time format, pretent the current time segment is closed with a
701 * 0 start and unknown stop time. */
702 segment->format = GST_FORMAT_TIME;
708 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
711 queue->sink_tainted = TRUE;
713 queue->src_tainted = TRUE;
715 /* segment can update the time level of the queue */
716 update_time_level (queue);
719 /* take a buffer and update segment, updating the time level of the queue. */
721 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
724 GstClockTime duration, timestamp;
726 timestamp = GST_BUFFER_TIMESTAMP (buffer);
727 duration = GST_BUFFER_DURATION (buffer);
729 /* if no timestamp is set, assume it's continuous with the previous
731 if (timestamp == GST_CLOCK_TIME_NONE)
732 timestamp = segment->position;
735 if (duration != GST_CLOCK_TIME_NONE)
736 timestamp += duration;
738 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
739 GST_TIME_ARGS (timestamp));
741 segment->position = timestamp;
744 queue->sink_tainted = TRUE;
746 queue->src_tainted = TRUE;
748 /* calc diff with other end */
749 update_time_level (queue);
753 update_buffering (GstQueue2 * queue)
756 gboolean post = FALSE;
758 if (queue->high_percent <= 0)
761 #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)
764 /* on EOS we are always 100% full, we set the var here so that it we can
765 * reuse the logic below to stop buffering */
767 GST_LOG_OBJECT (queue, "we are EOS");
769 /* figure out the percent we are filled, we take the max of all formats. */
771 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
772 percent = GET_PERCENT (bytes, 0);
774 guint64 rb_size = queue->ring_buffer_max_size;
775 percent = GET_PERCENT (bytes, rb_size);
777 percent = MAX (percent, GET_PERCENT (time, 0));
778 percent = MAX (percent, GET_PERCENT (buffers, 0));
780 /* also apply the rate estimate when we need to */
781 if (queue->use_rate_estimate)
782 percent = MAX (percent, GET_PERCENT (rate_time, 0));
785 if (queue->is_buffering) {
787 /* if we were buffering see if we reached the high watermark */
788 if (percent >= queue->high_percent)
789 queue->is_buffering = FALSE;
791 /* we were not buffering, check if we need to start buffering if we drop
792 * below the low threshold */
793 if (percent < queue->low_percent) {
794 queue->is_buffering = TRUE;
795 queue->buffering_iteration++;
801 GstBufferingMode mode;
802 gint64 buffering_left = -1;
804 /* scale to high percent so that it becomes the 100% mark */
805 percent = percent * 100 / queue->high_percent;
810 if (percent != queue->buffering_percent) {
811 queue->buffering_percent = percent;
813 if (!QUEUE_IS_USING_QUEUE (queue)) {
816 if (QUEUE_IS_USING_RING_BUFFER (queue))
817 mode = GST_BUFFERING_TIMESHIFT;
819 mode = GST_BUFFERING_DOWNLOAD;
821 if (queue->byte_in_rate > 0) {
822 if (gst_pad_query_peer_duration (queue->sinkpad, GST_FORMAT_BYTES,
825 (gdouble) ((duration -
826 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
829 buffering_left = G_MAXINT64;
832 mode = GST_BUFFERING_STREAM;
835 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
836 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
838 gst_message_set_buffering_stats (message, mode,
839 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
841 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
844 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
851 reset_rate_timer (GstQueue2 * queue)
854 queue->bytes_out = 0;
855 queue->byte_in_rate = 0.0;
856 queue->byte_in_period = 0;
857 queue->byte_out_rate = 0.0;
858 queue->last_in_elapsed = 0.0;
859 queue->last_out_elapsed = 0.0;
860 queue->in_timer_started = FALSE;
861 queue->out_timer_started = FALSE;
864 /* the interval in seconds to recalculate the rate */
865 #define RATE_INTERVAL 0.2
866 /* Tuning for rate estimation. We use a large window for the input rate because
867 * it should be stable when connected to a network. The output rate is less
868 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
869 * therefore adapt more quickly.
870 * However, initial input rate may be subject to a burst, and should therefore
871 * initially also adapt more quickly to changes, and only later on give higher
872 * weight to previous values. */
873 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
874 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
877 update_in_rates (GstQueue2 * queue)
879 gdouble elapsed, period;
880 gdouble byte_in_rate;
882 if (!queue->in_timer_started) {
883 queue->in_timer_started = TRUE;
884 g_timer_start (queue->in_timer);
888 elapsed = g_timer_elapsed (queue->in_timer, NULL);
890 /* recalc after each interval. */
891 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
892 period = elapsed - queue->last_in_elapsed;
894 GST_DEBUG_OBJECT (queue,
895 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
896 period, queue->bytes_in, queue->byte_in_period);
898 byte_in_rate = queue->bytes_in / period;
900 if (queue->byte_in_rate == 0.0)
901 queue->byte_in_rate = byte_in_rate;
903 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
904 (double) queue->byte_in_period, period);
906 /* another data point, cap at 16 for long time running average */
907 if (queue->byte_in_period < 16 * RATE_INTERVAL)
908 queue->byte_in_period += period;
910 /* reset the values to calculate rate over the next interval */
911 queue->last_in_elapsed = elapsed;
915 if (queue->byte_in_rate > 0.0) {
916 queue->cur_level.rate_time =
917 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
919 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
920 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
924 update_out_rates (GstQueue2 * queue)
926 gdouble elapsed, period;
927 gdouble byte_out_rate;
929 if (!queue->out_timer_started) {
930 queue->out_timer_started = TRUE;
931 g_timer_start (queue->out_timer);
935 elapsed = g_timer_elapsed (queue->out_timer, NULL);
937 /* recalc after each interval. */
938 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
939 period = elapsed - queue->last_out_elapsed;
941 GST_DEBUG_OBJECT (queue,
942 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
944 byte_out_rate = queue->bytes_out / period;
946 if (queue->byte_out_rate == 0.0)
947 queue->byte_out_rate = byte_out_rate;
949 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
951 /* reset the values to calculate rate over the next interval */
952 queue->last_out_elapsed = elapsed;
953 queue->bytes_out = 0;
955 if (queue->byte_in_rate > 0.0) {
956 queue->cur_level.rate_time =
957 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
959 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
960 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
964 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
966 guint64 reading_pos, max_reading_pos;
969 max_reading_pos = range->max_reading_pos;
971 max_reading_pos = MAX (max_reading_pos, reading_pos);
973 GST_DEBUG_OBJECT (queue,
974 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
975 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
976 range->max_reading_pos = max_reading_pos;
978 update_cur_level (queue, range);
982 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
987 GST_QUEUE2_MUTEX_UNLOCK (queue);
989 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
992 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
993 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
994 GST_SEEK_TYPE_NONE, -1);
996 res = gst_pad_push_event (queue->sinkpad, event);
997 GST_QUEUE2_MUTEX_LOCK (queue);
1000 queue->current = add_range (queue, offset);
1005 /* see if there is enough data in the file to read a full buffer */
1007 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1009 GstQueue2Range *range;
1011 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1014 if ((range = find_range (queue, offset))) {
1015 if (queue->current != range) {
1016 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1017 perform_seek_to_offset (queue, range->writing_pos);
1020 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1021 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1023 /* we have a range for offset */
1024 GST_DEBUG_OBJECT (queue,
1025 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1026 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1028 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1031 if (offset + length <= range->writing_pos)
1034 GST_DEBUG_OBJECT (queue,
1035 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1036 (offset + length) - range->writing_pos);
1039 GST_INFO_OBJECT (queue, "not found in any range");
1040 /* we don't have the range, see how far away we are, FIXME, find a good
1041 * threshold based on the incoming rate. */
1042 if (!queue->is_eos && queue->current) {
1043 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1044 if (offset < queue->current->offset || offset >
1045 queue->current->writing_pos + QUEUE_MAX_BYTES (queue) -
1046 queue->cur_level.bytes) {
1047 perform_seek_to_offset (queue, offset);
1049 GST_INFO_OBJECT (queue,
1050 "requested data is within range, wait for data");
1052 } else if (offset < queue->current->writing_pos + 200000) {
1053 update_cur_pos (queue, queue->current, offset + length);
1054 GST_INFO_OBJECT (queue, "wait for data");
1059 /* too far away, do a seek */
1060 perform_seek_to_offset (queue, offset);
1067 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1068 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1069 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1071 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1074 static GstFlowReturn
1075 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1076 guint8 * dst, gint64 * read_return)
1078 guint8 *ring_buffer;
1081 ring_buffer = queue->ring_buffer;
1083 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1086 /* this should not block */
1087 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1089 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1090 res = fread (dst, 1, length, queue->temp_file);
1092 memcpy (dst, ring_buffer + offset, length);
1096 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1098 if (G_UNLIKELY (res < length)) {
1099 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1100 goto could_not_read;
1101 /* check for errors or EOF */
1102 if (ferror (queue->temp_file))
1103 goto could_not_read;
1104 if (feof (queue->temp_file) && length > 0)
1114 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1115 return GST_FLOW_ERROR;
1119 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1120 return GST_FLOW_ERROR;
1124 GST_DEBUG ("non-regular file hits EOS");
1125 return GST_FLOW_UNEXPECTED;
1129 static GstFlowReturn
1130 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1131 GstBuffer ** buffer)
1135 guint64 file_offset;
1136 guint block_length, remaining, read_length;
1139 GstFlowReturn ret = GST_FLOW_OK;
1141 /* allocate the output buffer of the requested size */
1142 buf = gst_buffer_new_allocate (NULL, length, 0);
1143 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
1145 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1149 rb_size = queue->ring_buffer_max_size;
1152 while (remaining > 0) {
1153 /* configure how much/whether to read */
1154 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1157 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1160 /* calculate how far away the offset is */
1161 if (queue->current->writing_pos > rpos)
1162 level = queue->current->writing_pos - rpos;
1166 GST_DEBUG_OBJECT (queue,
1167 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1168 ", level %" G_GUINT64_FORMAT,
1169 rpos, queue->current->writing_pos, level);
1171 if (level >= rb_size) {
1172 /* we don't have the data but if we have a ring buffer that is full, we
1174 GST_DEBUG_OBJECT (queue,
1175 "ring buffer full, reading ring-buffer-max-size %"
1176 G_GUINT64_FORMAT " bytes", rb_size);
1177 read_length = rb_size;
1178 } else if (queue->is_eos) {
1179 /* won't get any more data so read any data we have */
1181 GST_DEBUG_OBJECT (queue,
1182 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1184 read_length = level;
1190 if (read_length == 0) {
1191 if (QUEUE_IS_USING_RING_BUFFER (queue)
1192 && queue->current->max_reading_pos > rpos) {
1193 /* protect cached data (data between offset and max_reading_pos)
1194 * and update current level */
1195 GST_DEBUG_OBJECT (queue,
1196 "protecting cached data [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1197 "]", rpos, queue->current->max_reading_pos);
1198 queue->current->max_reading_pos = rpos;
1199 update_cur_level (queue, queue->current);
1201 GST_DEBUG_OBJECT (queue, "waiting for add");
1202 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1206 /* we have the requested data so read it */
1207 read_length = remaining;
1210 /* set range reading_pos to actual reading position for this read */
1211 queue->current->reading_pos = rpos;
1213 /* configure how much and from where to read */
1214 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1216 (queue->current->rb_offset + (rpos -
1217 queue->current->offset)) % rb_size;
1218 if (file_offset + read_length > rb_size) {
1219 block_length = rb_size - file_offset;
1221 block_length = read_length;
1225 block_length = read_length;
1228 /* while we still have data to read, we loop */
1229 while (read_length > 0) {
1233 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1234 data, &read_return);
1235 if (ret != GST_FLOW_OK)
1238 file_offset += read_return;
1239 if (QUEUE_IS_USING_RING_BUFFER (queue))
1240 file_offset %= rb_size;
1242 data += read_return;
1243 read_length -= read_return;
1244 block_length = read_length;
1245 remaining -= read_return;
1247 rpos = (queue->current->reading_pos += read_return);
1248 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1250 GST_QUEUE2_SIGNAL_DEL (queue);
1251 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1254 gst_buffer_unmap (buf, data, length);
1256 GST_BUFFER_OFFSET (buf) = offset;
1257 GST_BUFFER_OFFSET_END (buf) = offset + length;
1266 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1267 gst_buffer_unref (buf);
1268 return GST_FLOW_UNEXPECTED;
1272 GST_DEBUG_OBJECT (queue, "we are flushing");
1273 gst_buffer_unref (buf);
1274 return GST_FLOW_WRONG_STATE;
1278 GST_DEBUG_OBJECT (queue, "we have a read error");
1279 gst_buffer_unmap (buf, data, 0);
1280 gst_buffer_unref (buf);
1285 /* should be called with QUEUE_LOCK */
1286 static GstMiniObject *
1287 gst_queue2_read_item_from_file (GstQueue2 * queue)
1289 GstMiniObject *item;
1291 if (queue->starting_segment != NULL) {
1292 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1293 queue->starting_segment = NULL;
1297 guint64 reading_pos;
1299 reading_pos = queue->current->reading_pos;
1302 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1307 item = GST_MINI_OBJECT_CAST (buffer);
1309 case GST_FLOW_UNEXPECTED:
1310 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1320 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1321 * the temp filename. */
1323 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1328 if (queue->temp_file)
1329 goto already_opened;
1331 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1333 /* we have two cases:
1334 * - temp_location was set to something !NULL (Deprecated). in this case we
1335 * open the specified filename.
1336 * - temp_template was set, allocate a filename and open that filename
1338 if (!queue->temp_location_set) {
1340 if (queue->temp_template == NULL)
1343 /* make copy of the template, we don't want to change this */
1344 name = g_strdup (queue->temp_template);
1345 fd = g_mkstemp (name);
1347 goto mkstemp_failed;
1349 /* open the file for update/writing */
1350 queue->temp_file = fdopen (fd, "wb+");
1351 /* error creating file */
1352 if (queue->temp_file == NULL)
1355 g_free (queue->temp_location);
1356 queue->temp_location = name;
1358 GST_QUEUE2_MUTEX_UNLOCK (queue);
1360 /* we can't emit the notify with the lock */
1361 g_object_notify (G_OBJECT (queue), "temp-location");
1363 GST_QUEUE2_MUTEX_LOCK (queue);
1365 /* open the file for update/writing, this is deprecated but we still need to
1366 * support it for API/ABI compatibility */
1367 queue->temp_file = g_fopen (queue->temp_location, "wb+");
1368 /* error creating file */
1369 if (queue->temp_file == NULL)
1372 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1379 GST_DEBUG_OBJECT (queue, "temp file was already open");
1384 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1385 (_("No Temp directory specified.")), (NULL));
1390 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1391 (_("Could not create temp file \"%s\"."), queue->temp_template),
1398 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1399 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1408 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1411 if (queue->temp_file == NULL)
1414 GST_DEBUG_OBJECT (queue, "closing temp file");
1416 fflush (queue->temp_file);
1417 fclose (queue->temp_file);
1419 if (queue->temp_remove)
1420 remove (queue->temp_location);
1422 queue->temp_file = NULL;
1423 clean_ranges (queue);
1427 gst_queue2_flush_temp_file (GstQueue2 * queue)
1429 if (queue->temp_file == NULL)
1432 GST_DEBUG_OBJECT (queue, "flushing temp file");
1434 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1438 gst_queue2_locked_flush (GstQueue2 * queue)
1440 if (!QUEUE_IS_USING_QUEUE (queue)) {
1441 if (QUEUE_IS_USING_TEMP_FILE (queue))
1442 gst_queue2_flush_temp_file (queue);
1443 init_ranges (queue);
1445 while (!g_queue_is_empty (queue->queue)) {
1446 GstMiniObject *data = g_queue_pop_head (queue->queue);
1448 /* Then lose another reference because we are supposed to destroy that
1449 data when flushing */
1450 gst_mini_object_unref (data);
1453 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1454 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1455 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1456 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1457 queue->sink_tainted = queue->src_tainted = TRUE;
1458 if (queue->starting_segment != NULL)
1459 gst_event_unref (queue->starting_segment);
1460 queue->starting_segment = NULL;
1461 queue->segment_event_received = FALSE;
1463 /* we deleted a lot of something */
1464 GST_QUEUE2_SIGNAL_DEL (queue);
1468 gst_queue2_wait_free_space (GstQueue2 * queue)
1470 /* We make space available if we're "full" according to whatever
1471 * the user defined as "full". */
1472 if (gst_queue2_is_filled (queue)) {
1475 /* pause the timer while we wait. The fact that we are waiting does not mean
1476 * the byterate on the input pad is lower */
1477 if ((started = queue->in_timer_started))
1478 g_timer_stop (queue->in_timer);
1480 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1481 "queue is full, waiting for free space");
1483 /* Wait for space to be available, we could be unlocked because of a flush. */
1484 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1486 while (gst_queue2_is_filled (queue));
1488 /* and continue if we were running before */
1490 g_timer_continue (queue->in_timer);
1497 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1503 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1505 guint8 *odata, *data, *ring_buffer;
1506 guint size, rb_size;
1508 guint64 writing_pos, new_writing_pos;
1509 GstQueue2Range *range, *prev, *next;
1511 if (QUEUE_IS_USING_RING_BUFFER (queue))
1512 writing_pos = queue->current->rb_writing_pos;
1514 writing_pos = queue->current->writing_pos;
1515 ring_buffer = queue->ring_buffer;
1516 rb_size = queue->ring_buffer_max_size;
1518 odata = gst_buffer_map (buffer, &osize, NULL, GST_MAP_READ);
1523 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1524 GST_BUFFER_OFFSET (buffer));
1529 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1532 /* calculate the space in the ring buffer not used by data from
1533 * the current range */
1534 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1535 /* wait until there is some free space */
1536 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1538 /* get the amount of space we have */
1539 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1541 /* calculate if we need to split or if we can write the entire
1543 to_write = MIN (size, space);
1545 /* the writing position in the ring buffer after writing (part
1546 * or all of) the buffer */
1547 new_writing_pos = (writing_pos + to_write) % rb_size;
1550 range = queue->ranges;
1552 /* if we need to overwrite data in the ring buffer, we need to
1555 * warning: this code is complicated and includes some
1556 * simplifications - pen, paper and diagrams for the cases
1559 guint64 range_data_start, range_data_end;
1560 GstQueue2Range *range_to_destroy = NULL;
1562 range_data_start = range->rb_offset;
1563 range_data_end = range->rb_writing_pos;
1565 /* handle the special case where the range has no data in it */
1566 if (range->writing_pos == range->offset) {
1567 if (range != queue->current) {
1568 GST_DEBUG_OBJECT (queue,
1569 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1570 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1572 range_to_destroy = range;
1574 prev->next = range->next;
1579 if (range_data_end > range_data_start) {
1580 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1583 if (new_writing_pos > range_data_start) {
1584 if (new_writing_pos >= range_data_end) {
1585 GST_DEBUG_OBJECT (queue,
1586 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1587 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1589 range_to_destroy = range;
1591 prev->next = range->next;
1593 GST_DEBUG_OBJECT (queue,
1594 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1595 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1596 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1597 range->offset + new_writing_pos - range_data_start,
1599 range->offset += (new_writing_pos - range_data_start);
1600 range->rb_offset = new_writing_pos;
1604 guint64 new_wpos_virt = writing_pos + to_write;
1606 if (new_wpos_virt <= range_data_start)
1609 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1610 GST_DEBUG_OBJECT (queue,
1611 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1612 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1614 range_to_destroy = range;
1616 prev->next = range->next;
1618 GST_DEBUG_OBJECT (queue,
1619 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1620 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1621 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1622 range->offset + new_writing_pos - range_data_start,
1624 range->offset += (new_wpos_virt - range_data_start);
1625 range->rb_offset = new_writing_pos;
1630 if (!range_to_destroy)
1633 range = range->next;
1634 if (range_to_destroy) {
1635 if (range_to_destroy == queue->ranges)
1636 queue->ranges = range;
1637 g_slice_free (GstQueue2Range, range_to_destroy);
1638 range_to_destroy = NULL;
1643 new_writing_pos = writing_pos + to_write;
1646 if (QUEUE_IS_USING_TEMP_FILE (queue)
1647 && FSEEK_FILE (queue->temp_file, writing_pos))
1650 if (new_writing_pos > writing_pos) {
1651 GST_INFO_OBJECT (queue,
1652 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1653 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1654 queue->current->writing_pos, queue->current->rb_writing_pos);
1655 /* either not using ring buffer or no wrapping, just write */
1656 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1657 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1660 memcpy (ring_buffer + writing_pos, data, to_write);
1663 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1664 /* try to merge with next range */
1665 while ((next = queue->current->next)) {
1666 GST_INFO_OBJECT (queue,
1667 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1668 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1669 if (new_writing_pos < next->offset)
1672 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1675 /* remove the group, we could choose to not read the data in this range
1676 * again. This would involve us doing a seek to the current writing position
1677 * in the range. FIXME, It would probably make sense to do a seek when there
1678 * is a lot of data in the range we merged with to avoid reading it all
1680 queue->current->next = next->next;
1681 g_slice_free (GstQueue2Range, next);
1683 debug_ranges (queue);
1685 goto update_and_signal;
1689 guint block_one, block_two;
1691 block_one = rb_size - writing_pos;
1692 block_two = to_write - block_one;
1694 if (block_one > 0) {
1695 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1696 /* write data to end of ring buffer */
1697 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1698 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1701 memcpy (ring_buffer + writing_pos, data, block_one);
1705 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1708 if (block_two > 0) {
1709 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1710 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1711 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1714 memcpy (ring_buffer, data + block_one, block_two);
1720 /* update the writing positions */
1722 GST_INFO_OBJECT (queue,
1723 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1724 to_write, writing_pos, size);
1726 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1728 queue->current->writing_pos += to_write;
1729 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1731 queue->current->writing_pos = writing_pos = new_writing_pos;
1733 update_cur_level (queue, queue->current);
1735 /* update the buffering status */
1736 if (queue->use_buffering)
1737 update_buffering (queue);
1739 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1740 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1742 GST_QUEUE2_SIGNAL_ADD (queue);
1745 gst_buffer_unmap (buffer, odata, osize);
1752 GST_DEBUG_OBJECT (queue, "we are flushing");
1753 gst_buffer_unmap (buffer, odata, osize);
1754 /* FIXME - GST_FLOW_UNEXPECTED ? */
1759 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1760 gst_buffer_unmap (buffer, odata, osize);
1767 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1771 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1772 (_("Error while writing to download file.")),
1773 ("%s", g_strerror (errno)));
1776 gst_buffer_unmap (buffer, odata, osize);
1781 /* enqueue an item an update the level stats */
1783 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item, gboolean isbuffer)
1789 buffer = GST_BUFFER_CAST (item);
1790 size = gst_buffer_get_size (buffer);
1792 /* add buffer to the statistics */
1793 if (QUEUE_IS_USING_QUEUE (queue)) {
1794 queue->cur_level.buffers++;
1795 queue->cur_level.bytes += size;
1797 queue->bytes_in += size;
1799 /* apply new buffer to segment stats */
1800 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1801 /* update the byterate stats */
1802 update_in_rates (queue);
1804 if (!QUEUE_IS_USING_QUEUE (queue)) {
1805 /* FIXME - check return value? */
1806 gst_queue2_create_write (queue, buffer);
1808 } else if (GST_IS_EVENT (item)) {
1811 event = GST_EVENT_CAST (item);
1813 switch (GST_EVENT_TYPE (event)) {
1815 /* Zero the thresholds, this makes sure the queue is completely
1816 * filled and we can read all data from the queue. */
1817 GST_DEBUG_OBJECT (queue, "we have EOS");
1818 queue->is_eos = TRUE;
1820 case GST_EVENT_SEGMENT:
1821 apply_segment (queue, event, &queue->sink_segment, TRUE);
1822 /* This is our first new segment, we hold it
1823 * as we can't save it on the temp file */
1824 if (!QUEUE_IS_USING_QUEUE (queue)) {
1825 if (queue->segment_event_received)
1826 goto unexpected_event;
1828 queue->segment_event_received = TRUE;
1829 if (queue->starting_segment != NULL)
1830 gst_event_unref (queue->starting_segment);
1831 queue->starting_segment = event;
1834 /* a new segment allows us to accept more buffers if we got UNEXPECTED
1835 * from downstream */
1836 queue->unexpected = FALSE;
1839 if (!QUEUE_IS_USING_QUEUE (queue))
1840 goto unexpected_event;
1844 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1845 item, GST_OBJECT_NAME (queue));
1846 /* we can't really unref since we don't know what it is */
1851 /* update the buffering status */
1852 if (queue->use_buffering)
1853 update_buffering (queue);
1855 if (QUEUE_IS_USING_QUEUE (queue)) {
1856 g_queue_push_tail (queue->queue, item);
1858 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
1861 GST_QUEUE2_SIGNAL_ADD (queue);
1870 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
1871 gst_event_type_get_name (GST_EVENT_TYPE (item)),
1872 GST_OBJECT_NAME (queue));
1873 gst_event_unref (GST_EVENT_CAST (item));
1878 /* dequeue an item from the queue and update level stats */
1879 static GstMiniObject *
1880 gst_queue2_locked_dequeue (GstQueue2 * queue, gboolean * is_buffer)
1882 GstMiniObject *item;
1884 if (!QUEUE_IS_USING_QUEUE (queue))
1885 item = gst_queue2_read_item_from_file (queue);
1887 item = g_queue_pop_head (queue->queue);
1892 if (GST_IS_BUFFER (item)) {
1896 buffer = GST_BUFFER_CAST (item);
1897 size = gst_buffer_get_size (buffer);
1900 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1901 "retrieved buffer %p from queue", buffer);
1903 if (QUEUE_IS_USING_QUEUE (queue)) {
1904 queue->cur_level.buffers--;
1905 queue->cur_level.bytes -= size;
1907 queue->bytes_out += size;
1909 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
1910 /* update the byterate stats */
1911 update_out_rates (queue);
1912 /* update the buffering */
1913 if (queue->use_buffering)
1914 update_buffering (queue);
1916 } else if (GST_IS_EVENT (item)) {
1917 GstEvent *event = GST_EVENT_CAST (item);
1921 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1922 "retrieved event %p from queue", event);
1924 switch (GST_EVENT_TYPE (event)) {
1926 /* queue is empty now that we dequeued the EOS */
1927 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1929 case GST_EVENT_SEGMENT:
1930 apply_segment (queue, event, &queue->src_segment, FALSE);
1937 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
1938 item, GST_OBJECT_NAME (queue));
1941 GST_QUEUE2_SIGNAL_DEL (queue);
1948 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
1954 gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event)
1958 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
1960 switch (GST_EVENT_TYPE (event)) {
1961 case GST_EVENT_FLUSH_START:
1963 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
1964 if (QUEUE_IS_USING_QUEUE (queue)) {
1966 gst_pad_push_event (queue->srcpad, event);
1968 /* now unblock the chain function */
1969 GST_QUEUE2_MUTEX_LOCK (queue);
1970 queue->srcresult = GST_FLOW_WRONG_STATE;
1971 queue->sinkresult = GST_FLOW_WRONG_STATE;
1972 /* unblock the loop and chain functions */
1973 GST_QUEUE2_SIGNAL_ADD (queue);
1974 GST_QUEUE2_SIGNAL_DEL (queue);
1975 GST_QUEUE2_MUTEX_UNLOCK (queue);
1977 /* make sure it pauses, this should happen since we sent
1978 * flush_start downstream. */
1979 gst_pad_pause_task (queue->srcpad);
1980 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
1982 GST_QUEUE2_MUTEX_LOCK (queue);
1983 /* flush the sink pad */
1984 queue->sinkresult = GST_FLOW_WRONG_STATE;
1985 GST_QUEUE2_SIGNAL_DEL (queue);
1986 GST_QUEUE2_MUTEX_UNLOCK (queue);
1988 gst_event_unref (event);
1992 case GST_EVENT_FLUSH_STOP:
1994 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
1996 if (QUEUE_IS_USING_QUEUE (queue)) {
1998 gst_pad_push_event (queue->srcpad, event);
2000 GST_QUEUE2_MUTEX_LOCK (queue);
2001 gst_queue2_locked_flush (queue);
2002 queue->srcresult = GST_FLOW_OK;
2003 queue->sinkresult = GST_FLOW_OK;
2004 queue->is_eos = FALSE;
2005 queue->unexpected = FALSE;
2006 /* reset rate counters */
2007 reset_rate_timer (queue);
2008 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2010 GST_QUEUE2_MUTEX_UNLOCK (queue);
2012 GST_QUEUE2_MUTEX_LOCK (queue);
2013 queue->segment_event_received = FALSE;
2014 queue->is_eos = FALSE;
2015 queue->unexpected = FALSE;
2016 queue->sinkresult = GST_FLOW_OK;
2017 GST_QUEUE2_MUTEX_UNLOCK (queue);
2019 gst_event_unref (event);
2024 if (GST_EVENT_IS_SERIALIZED (event)) {
2025 /* serialized events go in the queue */
2026 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2027 /* refuse more events on EOS */
2030 gst_queue2_locked_enqueue (queue, event, FALSE);
2031 GST_QUEUE2_MUTEX_UNLOCK (queue);
2033 /* non-serialized events are passed upstream. */
2034 gst_pad_push_event (queue->srcpad, event);
2044 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2045 GST_QUEUE2_MUTEX_UNLOCK (queue);
2046 gst_event_unref (event);
2051 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2052 GST_QUEUE2_MUTEX_UNLOCK (queue);
2053 gst_event_unref (event);
2059 gst_queue2_is_empty (GstQueue2 * queue)
2061 /* never empty on EOS */
2065 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2066 return queue->current->writing_pos <= queue->current->max_reading_pos;
2068 if (queue->queue->length == 0)
2076 gst_queue2_is_filled (GstQueue2 * queue)
2080 /* always filled on EOS */
2084 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2085 (queue->cur_level.format) >= ((alt_max) ? \
2086 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2088 /* if using a ring buffer we're filled if all ring buffer space is used
2089 * _by the current range_ */
2090 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2091 guint64 rb_size = queue->ring_buffer_max_size;
2092 GST_DEBUG_OBJECT (queue,
2093 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2094 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2095 return CHECK_FILLED (bytes, rb_size);
2098 /* if using file, we're never filled if we don't have EOS */
2099 if (QUEUE_IS_USING_TEMP_FILE (queue))
2102 /* we are never filled when we have no buffers at all */
2103 if (queue->cur_level.buffers == 0)
2106 /* we are filled if one of the current levels exceeds the max */
2107 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2108 || CHECK_FILLED (time, 0);
2110 /* if we need to, use the rate estimate to check against the max time we are
2111 * allowed to queue */
2112 if (queue->use_rate_estimate)
2113 res |= CHECK_FILLED (rate_time, 0);
2119 static GstFlowReturn
2120 gst_queue2_chain (GstPad * pad, GstBuffer * buffer)
2124 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2126 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of size %"
2127 G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2128 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2129 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2130 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2132 /* we have to lock the queue since we span threads */
2133 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2134 /* when we received EOS, we refuse more data */
2137 /* when we received unexpected from downstream, refuse more buffers */
2138 if (queue->unexpected)
2139 goto out_unexpected;
2141 if (!gst_queue2_wait_free_space (queue))
2144 /* put buffer in queue now */
2145 gst_queue2_locked_enqueue (queue, buffer, TRUE);
2146 GST_QUEUE2_MUTEX_UNLOCK (queue);
2150 /* special conditions */
2153 GstFlowReturn ret = queue->sinkresult;
2155 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2156 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2157 GST_QUEUE2_MUTEX_UNLOCK (queue);
2158 gst_buffer_unref (buffer);
2164 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2165 GST_QUEUE2_MUTEX_UNLOCK (queue);
2166 gst_buffer_unref (buffer);
2168 return GST_FLOW_UNEXPECTED;
2172 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2173 "exit because we received UNEXPECTED");
2174 GST_QUEUE2_MUTEX_UNLOCK (queue);
2175 gst_buffer_unref (buffer);
2177 return GST_FLOW_UNEXPECTED;
2181 /* dequeue an item from the queue an push it downstream. This functions returns
2182 * the result of the push. */
2183 static GstFlowReturn
2184 gst_queue2_push_one (GstQueue2 * queue)
2186 GstFlowReturn result = GST_FLOW_OK;
2187 GstMiniObject *data;
2188 gboolean is_buffer = FALSE;
2190 data = gst_queue2_locked_dequeue (queue, &is_buffer);
2195 GST_QUEUE2_MUTEX_UNLOCK (queue);
2203 buffer = GST_BUFFER_CAST (data);
2205 caps = GST_BUFFER_CAPS (buffer);
2209 /* set caps before pushing the buffer so that core does not try to do
2210 * something fancy to check if this is possible. */
2211 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2212 gst_pad_set_caps (queue->srcpad, caps);
2215 result = gst_pad_push (queue->srcpad, buffer);
2217 /* need to check for srcresult here as well */
2218 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2219 if (result == GST_FLOW_UNEXPECTED) {
2220 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2221 "got UNEXPECTED from downstream");
2222 /* stop pushing buffers, we dequeue all items until we see an item that we
2223 * can push again, which is EOS or SEGMENT. If there is nothing in the
2224 * queue we can push, we set a flag to make the sinkpad refuse more
2225 * buffers with an UNEXPECTED return value until we receive something
2226 * pushable again or we get flushed. */
2227 while ((data = gst_queue2_locked_dequeue (queue, &is_buffer))) {
2229 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2230 "dropping UNEXPECTED buffer %p", data);
2231 gst_buffer_unref (GST_BUFFER_CAST (data));
2232 } else if (GST_IS_EVENT (data)) {
2233 GstEvent *event = GST_EVENT_CAST (data);
2234 GstEventType type = GST_EVENT_TYPE (event);
2236 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
2237 /* we found a pushable item in the queue, push it out */
2238 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2239 "pushing pushable event %s after UNEXPECTED",
2240 GST_EVENT_TYPE_NAME (event));
2243 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2244 "dropping UNEXPECTED event %p", event);
2245 gst_event_unref (event);
2248 /* no more items in the queue. Set the unexpected flag so that upstream
2249 * make us refuse any more buffers on the sinkpad. Since we will still
2250 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
2251 * task function does not shut down. */
2252 queue->unexpected = TRUE;
2253 result = GST_FLOW_OK;
2255 } else if (GST_IS_EVENT (data)) {
2256 GstEvent *event = GST_EVENT_CAST (data);
2257 GstEventType type = GST_EVENT_TYPE (event);
2259 gst_pad_push_event (queue->srcpad, event);
2261 /* if we're EOS, return UNEXPECTED so that the task pauses. */
2262 if (type == GST_EVENT_EOS) {
2263 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2264 "pushed EOS event %p, return UNEXPECTED", event);
2265 result = GST_FLOW_UNEXPECTED;
2268 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2275 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2276 "exit because we have no item in the queue");
2277 return GST_FLOW_ERROR;
2281 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2282 return GST_FLOW_WRONG_STATE;
2286 /* called repeatedly with @pad as the source pad. This function should push out
2287 * data to the peer element. */
2289 gst_queue2_loop (GstPad * pad)
2294 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2296 /* have to lock for thread-safety */
2297 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2299 if (gst_queue2_is_empty (queue)) {
2302 /* pause the timer while we wait. The fact that we are waiting does not mean
2303 * the byterate on the output pad is lower */
2304 if ((started = queue->out_timer_started))
2305 g_timer_stop (queue->out_timer);
2307 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2308 "queue is empty, waiting for new data");
2310 /* Wait for data to be available, we could be unlocked because of a flush. */
2311 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2313 while (gst_queue2_is_empty (queue));
2315 /* and continue if we were running before */
2317 g_timer_continue (queue->out_timer);
2319 ret = gst_queue2_push_one (queue);
2320 queue->srcresult = ret;
2321 queue->sinkresult = ret;
2322 if (ret != GST_FLOW_OK)
2325 GST_QUEUE2_MUTEX_UNLOCK (queue);
2332 gboolean eos = queue->is_eos;
2333 GstFlowReturn ret = queue->srcresult;
2335 gst_pad_pause_task (queue->srcpad);
2336 GST_QUEUE2_MUTEX_UNLOCK (queue);
2337 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2338 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2339 /* let app know about us giving up if upstream is not expected to do so */
2340 /* UNEXPECTED is already taken care of elsewhere */
2341 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED)) {
2342 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2343 (_("Internal data flow error.")),
2344 ("streaming task paused, reason %s (%d)",
2345 gst_flow_get_name (ret), ret));
2346 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2353 gst_queue2_handle_src_event (GstPad * pad, GstEvent * event)
2355 gboolean res = TRUE;
2356 GstQueue2 *queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2358 if (G_UNLIKELY (queue == NULL)) {
2359 gst_event_unref (event);
2362 #ifndef GST_DISABLE_GST_DEBUG
2363 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2364 event, GST_EVENT_TYPE_NAME (event));
2367 switch (GST_EVENT_TYPE (event)) {
2368 case GST_EVENT_FLUSH_START:
2369 if (QUEUE_IS_USING_QUEUE (queue)) {
2370 /* just forward upstream */
2371 res = gst_pad_push_event (queue->sinkpad, event);
2373 /* now unblock the getrange function */
2374 GST_QUEUE2_MUTEX_LOCK (queue);
2375 GST_DEBUG_OBJECT (queue, "flushing");
2376 queue->srcresult = GST_FLOW_WRONG_STATE;
2377 GST_QUEUE2_SIGNAL_ADD (queue);
2378 GST_QUEUE2_MUTEX_UNLOCK (queue);
2380 /* when using a temp file, we eat the event */
2382 gst_event_unref (event);
2385 case GST_EVENT_FLUSH_STOP:
2386 if (QUEUE_IS_USING_QUEUE (queue)) {
2387 /* just forward upstream */
2388 res = gst_pad_push_event (queue->sinkpad, event);
2390 /* now unblock the getrange function */
2391 GST_QUEUE2_MUTEX_LOCK (queue);
2392 queue->srcresult = GST_FLOW_OK;
2393 if (queue->current) {
2394 /* forget the highest read offset, we'll calculate a new one when we
2395 * get the next getrange request. We need to do this in order to reset
2396 * the buffering percentage */
2397 queue->current->max_reading_pos = 0;
2399 GST_QUEUE2_MUTEX_UNLOCK (queue);
2401 /* when using a temp file, we eat the event */
2403 gst_event_unref (event);
2407 res = gst_pad_push_event (queue->sinkpad, event);
2411 gst_object_unref (queue);
2416 gst_queue2_peer_query (GstQueue2 * queue, GstPad * pad, GstQuery * query)
2418 gboolean ret = FALSE;
2421 if ((peer = gst_pad_get_peer (pad))) {
2422 ret = gst_pad_query (peer, query);
2423 gst_object_unref (peer);
2429 gst_queue2_handle_src_query (GstPad * pad, GstQuery * query)
2433 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2434 if (G_UNLIKELY (queue == NULL))
2437 switch (GST_QUERY_TYPE (query)) {
2438 case GST_QUERY_POSITION:
2443 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2446 /* get peer position */
2447 gst_query_parse_position (query, &format, &peer_pos);
2449 /* FIXME: this code assumes that there's no discont in the queue */
2451 case GST_FORMAT_BYTES:
2452 peer_pos -= queue->cur_level.bytes;
2454 case GST_FORMAT_TIME:
2455 peer_pos -= queue->cur_level.time;
2458 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2459 "know how to adjust value", gst_format_get_name (format));
2462 /* set updated position */
2463 gst_query_set_position (query, format, peer_pos);
2466 case GST_QUERY_DURATION:
2468 GST_DEBUG_OBJECT (queue, "doing peer query");
2470 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2473 GST_DEBUG_OBJECT (queue, "peer query success");
2476 case GST_QUERY_BUFFERING:
2480 GST_DEBUG_OBJECT (queue, "query buffering");
2482 /* FIXME - is this condition correct? what should ring buffer do? */
2483 if (QUEUE_IS_USING_QUEUE (queue)) {
2484 /* no temp file, just forward to the peer */
2485 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2487 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2489 gint64 start, stop, range_start, range_stop;
2490 guint64 writing_pos;
2492 gint64 estimated_total, buffering_left;
2494 gboolean peer_res, is_buffering, is_eos;
2495 gdouble byte_in_rate, byte_out_rate;
2496 GstQueue2Range *queued_ranges;
2498 /* we need a current download region */
2499 if (queue->current == NULL)
2502 writing_pos = queue->current->writing_pos;
2503 byte_in_rate = queue->byte_in_rate;
2504 byte_out_rate = queue->byte_out_rate;
2505 is_buffering = queue->is_buffering;
2506 is_eos = queue->is_eos;
2507 percent = queue->buffering_percent;
2510 /* we're EOS, we know the duration in bytes now */
2512 duration = writing_pos;
2514 /* get duration of upstream in bytes */
2515 peer_res = gst_pad_query_peer_duration (queue->sinkpad,
2516 GST_FORMAT_BYTES, &duration);
2519 /* calculate remaining and total download time */
2520 if (peer_res && byte_in_rate > 0.0) {
2521 estimated_total = (duration * 1000) / byte_in_rate;
2522 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2524 estimated_total = -1;
2525 buffering_left = -1;
2527 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2528 G_GINT64_FORMAT, estimated_total, buffering_left);
2530 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2533 case GST_FORMAT_PERCENT:
2534 /* we need duration */
2538 GST_DEBUG_OBJECT (queue,
2539 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2540 duration, writing_pos);
2543 /* get our available data relative to the duration */
2545 stop = GST_FORMAT_PERCENT_MAX * writing_pos / duration;
2549 case GST_FORMAT_BYTES:
2559 /* fill out the buffered ranges */
2560 for (queued_ranges = queue->ranges; queued_ranges;
2561 queued_ranges = queued_ranges->next) {
2563 case GST_FORMAT_PERCENT:
2564 if (duration == -1) {
2569 range_start = 100 * queued_ranges->offset / duration;
2570 range_stop = 100 * queued_ranges->writing_pos / duration;
2572 case GST_FORMAT_BYTES:
2573 range_start = queued_ranges->offset;
2574 range_stop = queued_ranges->writing_pos;
2581 if (range_start == range_stop)
2583 GST_DEBUG_OBJECT (queue,
2584 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2585 G_GINT64_FORMAT, range_start, range_stop);
2586 gst_query_add_buffering_range (query, range_start, range_stop);
2589 gst_query_set_buffering_percent (query, is_buffering, percent);
2590 gst_query_set_buffering_range (query, format, start, stop,
2592 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2593 byte_in_rate, byte_out_rate, buffering_left);
2597 case GST_QUERY_SCHEDULING:
2601 /* we can operate in pull mode when we are using a tempfile */
2602 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
2604 gst_query_set_scheduling (query, pull_mode, pull_mode, FALSE, 0, -1, 1);
2608 /* peer handled other queries */
2609 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2614 gst_object_unref (queue);
2620 GST_DEBUG_OBJECT (queue, "failed peer query");
2621 gst_object_unref (queue);
2627 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2629 /* simply forward to the srcpad query function */
2630 return gst_queue2_handle_src_query (GST_QUEUE2_CAST (element)->srcpad, query);
2634 gst_queue2_update_upstream_size (GstQueue2 * queue)
2636 gint64 upstream_size = -1;
2638 if (gst_pad_query_peer_duration (queue->sinkpad, GST_FORMAT_BYTES,
2640 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2641 queue->upstream_size = upstream_size;
2645 static GstFlowReturn
2646 gst_queue2_get_range (GstPad * pad, guint64 offset, guint length,
2647 GstBuffer ** buffer)
2652 queue = GST_QUEUE2_CAST (gst_pad_get_parent (pad));
2654 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2655 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2656 offset = (offset == -1) ? queue->current->reading_pos : offset;
2658 GST_DEBUG_OBJECT (queue,
2659 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2661 /* catch any reads beyond the size of the file here to make sure queue2
2662 * doesn't send seek events beyond the size of the file upstream, since
2663 * that would confuse elements such as souphttpsrc and/or http servers.
2664 * Demuxers often just loop until EOS at the end of the file to figure out
2665 * when they've read all the end-headers or index chunks. */
2666 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2667 gst_queue2_update_upstream_size (queue);
2668 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2669 goto out_unexpected;
2672 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2673 gst_queue2_update_upstream_size (queue);
2674 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2675 length = queue->upstream_size - offset;
2676 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2680 /* FIXME - function will block when the range is not yet available */
2681 ret = gst_queue2_create_read (queue, offset, length, buffer);
2682 GST_QUEUE2_MUTEX_UNLOCK (queue);
2684 gst_object_unref (queue);
2691 ret = queue->srcresult;
2693 GST_DEBUG_OBJECT (queue, "we are flushing");
2694 GST_QUEUE2_MUTEX_UNLOCK (queue);
2695 gst_object_unref (queue);
2700 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2701 GST_QUEUE2_MUTEX_UNLOCK (queue);
2702 gst_object_unref (queue);
2703 return GST_FLOW_UNEXPECTED;
2707 /* sink currently only operates in push mode */
2709 gst_queue2_sink_activate_push (GstPad * pad, gboolean active)
2711 gboolean result = TRUE;
2714 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2717 GST_QUEUE2_MUTEX_LOCK (queue);
2718 GST_DEBUG_OBJECT (queue, "activating push mode");
2719 queue->srcresult = GST_FLOW_OK;
2720 queue->sinkresult = GST_FLOW_OK;
2721 queue->is_eos = FALSE;
2722 queue->unexpected = FALSE;
2723 reset_rate_timer (queue);
2724 GST_QUEUE2_MUTEX_UNLOCK (queue);
2726 /* unblock chain function */
2727 GST_QUEUE2_MUTEX_LOCK (queue);
2728 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2729 queue->srcresult = GST_FLOW_WRONG_STATE;
2730 queue->sinkresult = GST_FLOW_WRONG_STATE;
2731 gst_queue2_locked_flush (queue);
2732 GST_QUEUE2_MUTEX_UNLOCK (queue);
2735 gst_object_unref (queue);
2740 /* src operating in push mode, we start a task on the source pad that pushes out
2741 * buffers from the queue */
2743 gst_queue2_src_activate_push (GstPad * pad, gboolean active)
2745 gboolean result = FALSE;
2748 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2751 GST_QUEUE2_MUTEX_LOCK (queue);
2752 GST_DEBUG_OBJECT (queue, "activating push mode");
2753 queue->srcresult = GST_FLOW_OK;
2754 queue->sinkresult = GST_FLOW_OK;
2755 queue->is_eos = FALSE;
2756 queue->unexpected = FALSE;
2757 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
2758 GST_QUEUE2_MUTEX_UNLOCK (queue);
2760 /* unblock loop function */
2761 GST_QUEUE2_MUTEX_LOCK (queue);
2762 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2763 queue->srcresult = GST_FLOW_WRONG_STATE;
2764 queue->sinkresult = GST_FLOW_WRONG_STATE;
2765 /* the item add signal will unblock */
2766 GST_QUEUE2_SIGNAL_ADD (queue);
2767 GST_QUEUE2_MUTEX_UNLOCK (queue);
2769 /* step 2, make sure streaming finishes */
2770 result = gst_pad_stop_task (pad);
2773 gst_object_unref (queue);
2778 /* pull mode, downstream will call our getrange function */
2780 gst_queue2_src_activate_pull (GstPad * pad, gboolean active)
2785 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2788 GST_QUEUE2_MUTEX_LOCK (queue);
2789 if (!QUEUE_IS_USING_QUEUE (queue)) {
2790 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2791 /* open the temp file now */
2792 result = gst_queue2_open_temp_location_file (queue);
2793 } else if (!queue->ring_buffer) {
2794 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
2795 result = ! !queue->ring_buffer;
2800 GST_DEBUG_OBJECT (queue, "activating pull mode");
2801 init_ranges (queue);
2802 queue->srcresult = GST_FLOW_OK;
2803 queue->sinkresult = GST_FLOW_OK;
2804 queue->is_eos = FALSE;
2805 queue->unexpected = FALSE;
2806 queue->upstream_size = 0;
2808 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
2809 /* this is not allowed, we cannot operate in pull mode without a temp
2811 queue->srcresult = GST_FLOW_WRONG_STATE;
2812 queue->sinkresult = GST_FLOW_WRONG_STATE;
2815 GST_QUEUE2_MUTEX_UNLOCK (queue);
2817 GST_QUEUE2_MUTEX_LOCK (queue);
2818 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
2819 queue->srcresult = GST_FLOW_WRONG_STATE;
2820 queue->sinkresult = GST_FLOW_WRONG_STATE;
2821 /* this will unlock getrange */
2822 GST_QUEUE2_SIGNAL_ADD (queue);
2824 GST_QUEUE2_MUTEX_UNLOCK (queue);
2826 gst_object_unref (queue);
2831 static GstStateChangeReturn
2832 gst_queue2_change_state (GstElement * element, GstStateChange transition)
2835 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2837 queue = GST_QUEUE2 (element);
2839 switch (transition) {
2840 case GST_STATE_CHANGE_NULL_TO_READY:
2842 case GST_STATE_CHANGE_READY_TO_PAUSED:
2843 GST_QUEUE2_MUTEX_LOCK (queue);
2844 if (!QUEUE_IS_USING_QUEUE (queue)) {
2845 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2846 if (!gst_queue2_open_temp_location_file (queue))
2847 ret = GST_STATE_CHANGE_FAILURE;
2849 if (queue->ring_buffer) {
2850 g_free (queue->ring_buffer);
2851 queue->ring_buffer = NULL;
2853 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
2854 ret = GST_STATE_CHANGE_FAILURE;
2856 init_ranges (queue);
2858 queue->segment_event_received = FALSE;
2859 queue->starting_segment = NULL;
2860 GST_QUEUE2_MUTEX_UNLOCK (queue);
2862 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2868 if (ret == GST_STATE_CHANGE_FAILURE)
2871 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2873 if (ret == GST_STATE_CHANGE_FAILURE)
2876 switch (transition) {
2877 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2879 case GST_STATE_CHANGE_PAUSED_TO_READY:
2880 GST_QUEUE2_MUTEX_LOCK (queue);
2881 if (!QUEUE_IS_USING_QUEUE (queue)) {
2882 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2883 gst_queue2_close_temp_location_file (queue);
2884 } else if (queue->ring_buffer) {
2885 g_free (queue->ring_buffer);
2886 queue->ring_buffer = NULL;
2888 clean_ranges (queue);
2890 if (queue->starting_segment != NULL) {
2891 gst_event_unref (queue->starting_segment);
2892 queue->starting_segment = NULL;
2894 GST_QUEUE2_MUTEX_UNLOCK (queue);
2896 case GST_STATE_CHANGE_READY_TO_NULL:
2905 /* changing the capacity of the queue must wake up
2906 * the _chain function, it might have more room now
2907 * to store the buffer/event in the queue */
2908 #define QUEUE_CAPACITY_CHANGE(q)\
2909 GST_QUEUE2_SIGNAL_DEL (queue);
2911 /* Changing the minimum required fill level must
2912 * wake up the _loop function as it might now
2913 * be able to preceed.
2915 #define QUEUE_THRESHOLD_CHANGE(q)\
2916 GST_QUEUE2_SIGNAL_ADD (queue);
2919 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
2923 /* the element must be stopped in order to do this */
2924 GST_OBJECT_LOCK (queue);
2925 state = GST_STATE (queue);
2926 if (state != GST_STATE_READY && state != GST_STATE_NULL)
2928 GST_OBJECT_UNLOCK (queue);
2930 /* set new location */
2931 g_free (queue->temp_template);
2932 queue->temp_template = g_strdup (template);
2939 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
2940 GST_OBJECT_UNLOCK (queue);
2945 gst_queue2_set_property (GObject * object,
2946 guint prop_id, const GValue * value, GParamSpec * pspec)
2948 GstQueue2 *queue = GST_QUEUE2 (object);
2950 /* someone could change levels here, and since this
2951 * affects the get/put funcs, we need to lock for safety. */
2952 GST_QUEUE2_MUTEX_LOCK (queue);
2955 case PROP_MAX_SIZE_BYTES:
2956 queue->max_level.bytes = g_value_get_uint (value);
2957 QUEUE_CAPACITY_CHANGE (queue);
2959 case PROP_MAX_SIZE_BUFFERS:
2960 queue->max_level.buffers = g_value_get_uint (value);
2961 QUEUE_CAPACITY_CHANGE (queue);
2963 case PROP_MAX_SIZE_TIME:
2964 queue->max_level.time = g_value_get_uint64 (value);
2965 /* set rate_time to the same value. We use an extra field in the level
2966 * structure so that we can easily access and compare it */
2967 queue->max_level.rate_time = queue->max_level.time;
2968 QUEUE_CAPACITY_CHANGE (queue);
2970 case PROP_USE_BUFFERING:
2971 queue->use_buffering = g_value_get_boolean (value);
2973 case PROP_USE_RATE_ESTIMATE:
2974 queue->use_rate_estimate = g_value_get_boolean (value);
2976 case PROP_LOW_PERCENT:
2977 queue->low_percent = g_value_get_int (value);
2979 case PROP_HIGH_PERCENT:
2980 queue->high_percent = g_value_get_int (value);
2982 case PROP_TEMP_TEMPLATE:
2983 gst_queue2_set_temp_template (queue, g_value_get_string (value));
2985 case PROP_TEMP_LOCATION:
2986 g_free (queue->temp_location);
2987 queue->temp_location = g_value_dup_string (value);
2988 /* you can set the property back to NULL to make it use the temp-tmpl
2990 queue->temp_location_set = queue->temp_location != NULL;
2992 case PROP_TEMP_REMOVE:
2993 queue->temp_remove = g_value_get_boolean (value);
2995 case PROP_RING_BUFFER_MAX_SIZE:
2996 queue->ring_buffer_max_size = g_value_get_uint64 (value);
2999 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3003 GST_QUEUE2_MUTEX_UNLOCK (queue);
3007 gst_queue2_get_property (GObject * object,
3008 guint prop_id, GValue * value, GParamSpec * pspec)
3010 GstQueue2 *queue = GST_QUEUE2 (object);
3012 GST_QUEUE2_MUTEX_LOCK (queue);
3015 case PROP_CUR_LEVEL_BYTES:
3016 g_value_set_uint (value, queue->cur_level.bytes);
3018 case PROP_CUR_LEVEL_BUFFERS:
3019 g_value_set_uint (value, queue->cur_level.buffers);
3021 case PROP_CUR_LEVEL_TIME:
3022 g_value_set_uint64 (value, queue->cur_level.time);
3024 case PROP_MAX_SIZE_BYTES:
3025 g_value_set_uint (value, queue->max_level.bytes);
3027 case PROP_MAX_SIZE_BUFFERS:
3028 g_value_set_uint (value, queue->max_level.buffers);
3030 case PROP_MAX_SIZE_TIME:
3031 g_value_set_uint64 (value, queue->max_level.time);
3033 case PROP_USE_BUFFERING:
3034 g_value_set_boolean (value, queue->use_buffering);
3036 case PROP_USE_RATE_ESTIMATE:
3037 g_value_set_boolean (value, queue->use_rate_estimate);
3039 case PROP_LOW_PERCENT:
3040 g_value_set_int (value, queue->low_percent);
3042 case PROP_HIGH_PERCENT:
3043 g_value_set_int (value, queue->high_percent);
3045 case PROP_TEMP_TEMPLATE:
3046 g_value_set_string (value, queue->temp_template);
3048 case PROP_TEMP_LOCATION:
3049 g_value_set_string (value, queue->temp_location);
3051 case PROP_TEMP_REMOVE:
3052 g_value_set_boolean (value, queue->temp_remove);
3054 case PROP_RING_BUFFER_MAX_SIZE:
3055 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3058 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3062 GST_QUEUE2_MUTEX_UNLOCK (queue);