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>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-queue2
28 * Data is queued until one of the limits specified by the
29 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
30 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
31 * more buffers into the queue will block the pushing thread until more space
34 * The queue will create a new thread on the source pad to decouple the
35 * processing on sink and source pad.
37 * You can query how many buffers are queued by reading the
38 * #GstQueue2:current-level-buffers property.
40 * The default queue size limits are 100 buffers, 2MB of data, or
41 * two seconds worth of data, whichever is reached first.
43 * If you set temp-tmpl to a value such as /tmp/gstreamer-XXXXXX, the element
44 * will allocate a random free filename and buffer data in the file.
45 * By using this, it will buffer the entire stream data on the file independently
46 * of the queue size limits, they will only be used for buffering statistics.
48 * Since 0.10.24, setting the temp-location property with a filename is deprecated
49 * because it's impossible to securely open a temporary file in this way. The
50 * property will still be used to notify the application of the allocated
53 * Last reviewed on 2009-07-10 (0.10.24)
60 #include "gstqueue2.h"
62 #include <glib/gstdio.h>
64 #include "gst/gst-i18n-lib.h"
67 #include <io.h> /* lseek, open, close, read */
69 #define lseek _lseeki64
76 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
81 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_DEBUG_CATEGORY_STATIC (queue_debug);
87 #define GST_CAT_DEFAULT (queue_debug)
88 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
95 /* default property values */
96 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
97 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
98 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
99 #define DEFAULT_USE_BUFFERING FALSE
100 #define DEFAULT_USE_RATE_ESTIMATE TRUE
101 #define DEFAULT_LOW_PERCENT 10
102 #define DEFAULT_HIGH_PERCENT 99
103 #define DEFAULT_TEMP_REMOVE TRUE
106 #define DEFAULT_BUFFER_SIZE 4096
107 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_location_set || (queue)->temp_template != NULL)
112 PROP_CUR_LEVEL_BUFFERS,
113 PROP_CUR_LEVEL_BYTES,
115 PROP_MAX_SIZE_BUFFERS,
119 PROP_USE_RATE_ESTIMATE,
128 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
135 #define STATUS(queue, pad, msg) \
136 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
137 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
138 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
139 " ns, %"G_GUINT64_FORMAT" items", \
140 GST_DEBUG_PAD_NAME (pad), \
141 queue->cur_level.buffers, \
142 queue->max_level.buffers, \
143 queue->cur_level.bytes, \
144 queue->max_level.bytes, \
145 queue->cur_level.time, \
146 queue->max_level.time, \
147 (guint64) (QUEUE_IS_USING_TEMP_FILE(queue) ? \
148 queue->writing_pos - queue->max_reading_pos : \
149 queue->queue->length))
151 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
152 g_mutex_lock (q->qlock); \
155 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,label) G_STMT_START { \
156 GST_QUEUE2_MUTEX_LOCK (q); \
157 if (q->srcresult != GST_FLOW_OK) \
161 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
162 g_mutex_unlock (q->qlock); \
165 #define GST_QUEUE2_WAIT_DEL_CHECK(q, label) G_STMT_START { \
166 STATUS (queue, q->sinkpad, "wait for DEL"); \
167 q->waiting_del = TRUE; \
168 g_cond_wait (q->item_del, queue->qlock); \
169 q->waiting_del = FALSE; \
170 if (q->srcresult != GST_FLOW_OK) { \
171 STATUS (queue, q->srcpad, "received DEL wakeup"); \
174 STATUS (queue, q->sinkpad, "received DEL"); \
177 #define GST_QUEUE2_WAIT_ADD_CHECK(q, label) G_STMT_START { \
178 STATUS (queue, q->srcpad, "wait for ADD"); \
179 q->waiting_add = TRUE; \
180 g_cond_wait (q->item_add, q->qlock); \
181 q->waiting_add = FALSE; \
182 if (q->srcresult != GST_FLOW_OK) { \
183 STATUS (queue, q->srcpad, "received ADD wakeup"); \
186 STATUS (queue, q->srcpad, "received ADD"); \
189 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
190 if (q->waiting_del) { \
191 STATUS (q, q->srcpad, "signal DEL"); \
192 g_cond_signal (q->item_del); \
196 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
197 if (q->waiting_add) { \
198 STATUS (q, q->sinkpad, "signal ADD"); \
199 g_cond_signal (q->item_add); \
203 #define _do_init(bla) \
204 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
205 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
206 "dataflow inside the queue element");
208 GST_BOILERPLATE_FULL (GstQueue2, gst_queue2, GstElement, GST_TYPE_ELEMENT,
211 static void gst_queue2_finalize (GObject * object);
213 static void gst_queue2_set_property (GObject * object,
214 guint prop_id, const GValue * value, GParamSpec * pspec);
215 static void gst_queue2_get_property (GObject * object,
216 guint prop_id, GValue * value, GParamSpec * pspec);
218 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstBuffer * buffer);
219 static GstFlowReturn gst_queue2_bufferalloc (GstPad * pad, guint64 offset,
220 guint size, GstCaps * caps, GstBuffer ** buf);
221 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
222 static void gst_queue2_loop (GstPad * pad);
224 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event);
226 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstEvent * event);
227 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstQuery * query);
229 static GstCaps *gst_queue2_getcaps (GstPad * pad);
230 static gboolean gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps);
232 static GstFlowReturn gst_queue2_get_range (GstPad * pad, guint64 offset,
233 guint length, GstBuffer ** buffer);
234 static gboolean gst_queue2_src_checkgetrange_function (GstPad * pad);
236 static gboolean gst_queue2_src_activate_pull (GstPad * pad, gboolean active);
237 static gboolean gst_queue2_src_activate_push (GstPad * pad, gboolean active);
238 static gboolean gst_queue2_sink_activate_push (GstPad * pad, gboolean active);
239 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
240 GstStateChange transition);
242 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
243 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
245 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
248 gst_queue2_base_init (gpointer g_class)
250 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
252 gst_element_class_add_pad_template (gstelement_class,
253 gst_static_pad_template_get (&srctemplate));
254 gst_element_class_add_pad_template (gstelement_class,
255 gst_static_pad_template_get (&sinktemplate));
257 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
260 "Erik Walthinsen <omega@cse.ogi.edu>, "
261 "Wim Taymans <wim.taymans@gmail.com>");
265 gst_queue2_class_init (GstQueue2Class * klass)
267 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
268 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
270 parent_class = g_type_class_peek_parent (klass);
272 gobject_class->set_property = gst_queue2_set_property;
273 gobject_class->get_property = gst_queue2_get_property;
276 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
277 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
278 "Current amount of data in the queue (bytes)",
279 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
281 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
282 "Current number of buffers in the queue",
283 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
285 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
286 "Current amount of data in the queue (in ns)",
287 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
290 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
291 "Max. amount of data in the queue (bytes, 0=disable)",
292 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
293 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
295 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
296 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
297 DEFAULT_MAX_SIZE_BUFFERS,
298 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
300 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
301 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
302 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
305 g_param_spec_boolean ("use-buffering", "Use buffering",
306 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
307 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
309 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
310 "Estimate the bitrate of the stream to calculate time level",
311 DEFAULT_USE_RATE_ESTIMATE,
312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
314 g_param_spec_int ("low-percent", "Low percent",
315 "Low threshold for buffering to start", 0, 100, DEFAULT_LOW_PERCENT,
316 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
318 g_param_spec_int ("high-percent", "High percent",
319 "High threshold for buffering to finish", 0, 100,
320 DEFAULT_HIGH_PERCENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
323 g_param_spec_string ("temp-template", "Temporary File Template",
324 "File template to store temporary files in, should contain directory "
325 "and XXXXXX. (NULL == disabled)",
326 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
329 g_param_spec_string ("temp-location", "Temporary File Location",
330 "Location to store temporary files in (Deprecated: Only read this "
331 "property, use temp-template to configure the name template)",
332 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 * GstQueue2:temp-remove
337 * When temp-template is set, remove the temporary file when going to READY.
341 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
342 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
343 "Remove the temp-location after use",
344 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 /* set several parent class virtual functions */
347 gobject_class->finalize = gst_queue2_finalize;
349 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
353 gst_queue2_init (GstQueue2 * queue, GstQueue2Class * g_class)
355 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
357 gst_pad_set_chain_function (queue->sinkpad,
358 GST_DEBUG_FUNCPTR (gst_queue2_chain));
359 gst_pad_set_activatepush_function (queue->sinkpad,
360 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_push));
361 gst_pad_set_event_function (queue->sinkpad,
362 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
363 gst_pad_set_getcaps_function (queue->sinkpad,
364 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
365 gst_pad_set_acceptcaps_function (queue->sinkpad,
366 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
367 gst_pad_set_bufferalloc_function (queue->sinkpad,
368 GST_DEBUG_FUNCPTR (gst_queue2_bufferalloc));
369 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
371 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
373 gst_pad_set_activatepull_function (queue->srcpad,
374 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_pull));
375 gst_pad_set_activatepush_function (queue->srcpad,
376 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_push));
377 gst_pad_set_getrange_function (queue->srcpad,
378 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
379 gst_pad_set_checkgetrange_function (queue->srcpad,
380 GST_DEBUG_FUNCPTR (gst_queue2_src_checkgetrange_function));
381 gst_pad_set_getcaps_function (queue->srcpad,
382 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
383 gst_pad_set_acceptcaps_function (queue->srcpad,
384 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
385 gst_pad_set_event_function (queue->srcpad,
386 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
387 gst_pad_set_query_function (queue->srcpad,
388 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
389 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
392 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
393 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
394 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
395 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
396 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
397 queue->use_buffering = DEFAULT_USE_BUFFERING;
398 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
399 queue->low_percent = DEFAULT_LOW_PERCENT;
400 queue->high_percent = DEFAULT_HIGH_PERCENT;
402 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
403 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
405 queue->srcresult = GST_FLOW_WRONG_STATE;
406 queue->is_eos = FALSE;
407 queue->in_timer = g_timer_new ();
408 queue->out_timer = g_timer_new ();
410 queue->qlock = g_mutex_new ();
411 queue->waiting_add = FALSE;
412 queue->item_add = g_cond_new ();
413 queue->waiting_del = FALSE;
414 queue->item_del = g_cond_new ();
415 queue->queue = g_queue_new ();
417 /* tempfile related */
418 queue->temp_template = NULL;
419 queue->temp_location = NULL;
420 queue->temp_location_set = FALSE;
421 queue->temp_remove = DEFAULT_TEMP_REMOVE;
423 GST_DEBUG_OBJECT (queue,
424 "initialized queue's not_empty & not_full conditions");
427 /* called only once, as opposed to dispose */
429 gst_queue2_finalize (GObject * object)
431 GstQueue2 *queue = GST_QUEUE2 (object);
433 GST_DEBUG_OBJECT (queue, "finalizing queue");
435 while (!g_queue_is_empty (queue->queue)) {
436 GstMiniObject *data = g_queue_pop_head (queue->queue);
438 gst_mini_object_unref (data);
441 g_queue_free (queue->queue);
442 g_mutex_free (queue->qlock);
443 g_cond_free (queue->item_add);
444 g_cond_free (queue->item_del);
445 g_timer_destroy (queue->in_timer);
446 g_timer_destroy (queue->out_timer);
448 /* temp_file path cleanup */
449 g_free (queue->temp_template);
450 g_free (queue->temp_location);
452 G_OBJECT_CLASS (parent_class)->finalize (object);
456 gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps)
462 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
464 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
465 result = gst_pad_peer_accept_caps (otherpad, caps);
471 gst_queue2_getcaps (GstPad * pad)
477 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
479 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
480 result = gst_pad_peer_get_caps (otherpad);
482 result = gst_caps_new_any ();
488 gst_queue2_bufferalloc (GstPad * pad, guint64 offset, guint size,
489 GstCaps * caps, GstBuffer ** buf)
492 GstFlowReturn result;
494 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
496 /* Forward to src pad, without setting caps on the src pad */
497 result = gst_pad_alloc_buffer (queue->srcpad, offset, size, caps, buf);
502 /* calculate the diff between running time on the sink and src of the queue.
503 * This is the total amount of time in the queue. */
505 update_time_level (GstQueue2 * queue)
507 gint64 sink_time, src_time;
510 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
511 queue->sink_segment.last_stop);
513 src_time = gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
514 queue->src_segment.last_stop);
516 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
517 GST_TIME_ARGS (sink_time), GST_TIME_ARGS (src_time));
519 if (sink_time >= src_time)
520 queue->cur_level.time = sink_time - src_time;
522 queue->cur_level.time = 0;
525 /* take a NEWSEGMENT event and apply the values to segment, updating the time
528 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment)
533 gint64 start, stop, time;
535 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
536 &format, &start, &stop, &time);
538 GST_DEBUG_OBJECT (queue,
539 "received NEWSEGMENT update %d, rate %lf, applied rate %lf, "
541 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
542 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
544 if (format == GST_FORMAT_BYTES) {
547 /* now configure the values, we use these to track timestamps on the
549 if (format != GST_FORMAT_TIME) {
550 /* non-time format, pretent the current time segment is closed with a
551 * 0 start and unknown stop time. */
553 format = GST_FORMAT_TIME;
558 gst_segment_set_newsegment_full (segment, update,
559 rate, arate, format, start, stop, time);
561 GST_DEBUG_OBJECT (queue,
562 "configured NEWSEGMENT %" GST_SEGMENT_FORMAT, segment);
564 /* segment can update the time level of the queue */
565 update_time_level (queue);
568 /* take a buffer and update segment, updating the time level of the queue. */
570 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment)
572 GstClockTime duration, timestamp;
574 timestamp = GST_BUFFER_TIMESTAMP (buffer);
575 duration = GST_BUFFER_DURATION (buffer);
577 /* if no timestamp is set, assume it's continuous with the previous
579 if (timestamp == GST_CLOCK_TIME_NONE)
580 timestamp = segment->last_stop;
583 if (duration != GST_CLOCK_TIME_NONE)
584 timestamp += duration;
586 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
587 GST_TIME_ARGS (timestamp));
589 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
591 /* calc diff with other end */
592 update_time_level (queue);
596 update_buffering (GstQueue2 * queue)
599 gboolean post = FALSE;
601 if (!queue->use_buffering || queue->high_percent <= 0)
604 #define GET_PERCENT(format) ((queue->max_level.format) > 0 ? \
605 (queue->cur_level.format) * 100 / (queue->max_level.format) : 0)
608 /* on EOS we are always 100% full, we set the var here so that it we can
609 * reuse the logic below to stop buffering */
612 /* figure out the percent we are filled, we take the max of all formats. */
613 percent = GET_PERCENT (bytes);
614 percent = MAX (percent, GET_PERCENT (time));
615 percent = MAX (percent, GET_PERCENT (buffers));
617 /* also apply the rate estimate when we need to */
618 if (queue->use_rate_estimate)
619 percent = MAX (percent, GET_PERCENT (rate_time));
622 if (queue->is_buffering) {
624 /* if we were buffering see if we reached the high watermark */
625 if (percent >= queue->high_percent)
626 queue->is_buffering = FALSE;
628 /* we were not buffering, check if we need to start buffering if we drop
629 * below the low threshold */
630 if (percent < queue->low_percent) {
631 queue->is_buffering = TRUE;
632 queue->buffering_iteration++;
638 GstBufferingMode mode;
639 gint64 buffering_left = -1;
641 /* scale to high percent so that it becomes the 100% mark */
642 percent = percent * 100 / queue->high_percent;
647 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
648 GstFormat fmt = GST_FORMAT_BYTES;
651 mode = GST_BUFFERING_DOWNLOAD;
652 if (queue->byte_in_rate > 0) {
653 if (gst_pad_query_peer_duration (queue->sinkpad, &fmt, &duration))
655 (gdouble) ((duration -
656 queue->writing_pos) * 1000) / queue->byte_in_rate;
658 buffering_left = G_MAXINT64;
661 mode = GST_BUFFERING_STREAM;
664 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
665 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
667 gst_message_set_buffering_stats (message, mode,
668 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
670 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
673 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
680 reset_rate_timer (GstQueue2 * queue)
683 queue->bytes_out = 0;
684 queue->byte_in_rate = 0.0;
685 queue->byte_out_rate = 0.0;
686 queue->last_in_elapsed = 0.0;
687 queue->last_out_elapsed = 0.0;
688 queue->in_timer_started = FALSE;
689 queue->out_timer_started = FALSE;
692 /* the interval in seconds to recalculate the rate */
693 #define RATE_INTERVAL 0.2
694 /* Tuning for rate estimation. We use a large window for the input rate because
695 * it should be stable when connected to a network. The output rate is less
696 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
697 * therefore adapt more quickly. */
698 #define AVG_IN(avg,val) ((avg) * 15.0 + (val)) / 16.0
699 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
702 update_in_rates (GstQueue2 * queue)
704 gdouble elapsed, period;
705 gdouble byte_in_rate;
707 if (!queue->in_timer_started) {
708 queue->in_timer_started = TRUE;
709 g_timer_start (queue->in_timer);
713 elapsed = g_timer_elapsed (queue->in_timer, NULL);
715 /* recalc after each interval. */
716 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
717 period = elapsed - queue->last_in_elapsed;
719 GST_DEBUG_OBJECT (queue,
720 "rates: period %f, in %" G_GUINT64_FORMAT, period, queue->bytes_in);
722 byte_in_rate = queue->bytes_in / period;
724 if (queue->byte_in_rate == 0.0)
725 queue->byte_in_rate = byte_in_rate;
727 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate);
729 /* reset the values to calculate rate over the next interval */
730 queue->last_in_elapsed = elapsed;
734 if (queue->byte_in_rate > 0.0) {
735 queue->cur_level.rate_time =
736 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
738 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
739 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
743 update_out_rates (GstQueue2 * queue)
745 gdouble elapsed, period;
746 gdouble byte_out_rate;
748 if (!queue->out_timer_started) {
749 queue->out_timer_started = TRUE;
750 g_timer_start (queue->out_timer);
754 elapsed = g_timer_elapsed (queue->out_timer, NULL);
756 /* recalc after each interval. */
757 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
758 period = elapsed - queue->last_out_elapsed;
760 GST_DEBUG_OBJECT (queue,
761 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
763 byte_out_rate = queue->bytes_out / period;
765 if (queue->byte_out_rate == 0.0)
766 queue->byte_out_rate = byte_out_rate;
768 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
770 /* reset the values to calculate rate over the next interval */
771 queue->last_out_elapsed = elapsed;
772 queue->bytes_out = 0;
774 if (queue->byte_in_rate > 0.0) {
775 queue->cur_level.rate_time =
776 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
778 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
779 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
783 gst_queue2_write_buffer_to_file (GstQueue2 * queue, GstBuffer * buffer)
790 fseeko (queue->temp_file, (off_t) queue->writing_pos, SEEK_SET);
791 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
792 lseek (fileno (queue->temp_file), (off_t) queue->writing_pos, SEEK_SET);
794 fseek (queue->temp_file, queue->writing_pos, SEEK_SET);
797 data = GST_BUFFER_DATA (buffer);
798 size = GST_BUFFER_SIZE (buffer);
800 ret = fwrite (data, 1, size, queue->temp_file);
802 /* FIXME do something useful here */
803 GST_ERROR_OBJECT (queue, "fwrite returned error");
805 queue->writing_pos += size;
807 if (queue->writing_pos > queue->max_reading_pos)
808 queue->cur_level.bytes = queue->writing_pos - queue->max_reading_pos;
810 queue->cur_level.bytes = 0;
813 /* see if there is enough data in the file to read a full buffer */
815 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
817 GST_DEBUG_OBJECT (queue,
818 "offset %" G_GUINT64_FORMAT ", len %u, write %" G_GUINT64_FORMAT, offset,
819 length, queue->writing_pos);
823 if (offset + length < queue->writing_pos)
830 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
836 /* check if we have enough data at @offset. If there is not enough data, we
838 while (!gst_queue2_have_data (queue, offset, length)) {
839 GST_QUEUE2_WAIT_ADD_CHECK (queue, out_flushing);
843 if (fseeko (queue->temp_file, (off_t) offset, SEEK_SET) != 0)
845 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
846 if (lseek (fileno (queue->temp_file), (off_t) offset,
847 SEEK_SET) == (off_t) - 1)
850 if (fseek (queue->temp_file, (long) offset, SEEK_SET) != 0)
854 buf = gst_buffer_new_and_alloc (length);
856 /* this should not block */
857 GST_LOG_OBJECT (queue, "Reading %d bytes", length);
858 res = fread (GST_BUFFER_DATA (buf), 1, length, queue->temp_file);
859 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
861 if (G_UNLIKELY (res == 0)) {
862 /* check for errors or EOF */
863 if (ferror (queue->temp_file))
865 if (feof (queue->temp_file) && length > 0)
871 GST_BUFFER_SIZE (buf) = length;
872 GST_BUFFER_OFFSET (buf) = offset;
873 GST_BUFFER_OFFSET_END (buf) = offset + length;
877 queue->reading_pos = offset + length;
878 queue->max_reading_pos = MAX (queue->max_reading_pos, queue->reading_pos);
880 if (queue->writing_pos > queue->max_reading_pos)
881 queue->cur_level.bytes = queue->writing_pos - queue->max_reading_pos;
883 queue->cur_level.bytes = 0;
890 GST_DEBUG_OBJECT (queue, "we are flushing");
891 return GST_FLOW_WRONG_STATE;
895 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
896 return GST_FLOW_ERROR;
900 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
901 gst_buffer_unref (buf);
902 return GST_FLOW_ERROR;
906 GST_DEBUG ("non-regular file hits EOS");
907 gst_buffer_unref (buf);
908 return GST_FLOW_UNEXPECTED;
912 /* should be called with QUEUE_LOCK */
913 static GstMiniObject *
914 gst_queue2_read_item_from_file (GstQueue2 * queue)
918 if (queue->starting_segment != NULL) {
919 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
920 queue->starting_segment = NULL;
926 gst_queue2_create_read (queue, queue->reading_pos, DEFAULT_BUFFER_SIZE,
930 item = GST_MINI_OBJECT_CAST (buffer);
932 case GST_FLOW_UNEXPECTED:
933 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
944 gst_queue2_open_temp_location_file (GstQueue2 * queue)
949 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
951 /* we have two cases:
952 * - temp_location was set to something !NULL (Deprecated). in this case we
953 * open the specified filename.
954 * - temp_template was set, allocate a filename and open that filename
956 if (!queue->temp_location_set) {
958 if (queue->temp_template == NULL)
961 /* make copy of the template, we don't want to change this */
962 name = g_strdup (queue->temp_template);
963 fd = g_mkstemp (name);
967 /* open the file for update/writing */
968 queue->temp_file = fdopen (fd, "wb+");
969 /* error creating file */
970 if (queue->temp_file == NULL)
973 g_free (queue->temp_location);
974 queue->temp_location = name;
976 g_object_notify (G_OBJECT (queue), "temp-location");
978 /* open the file for update/writing, this is deprecated but we still need to
979 * support it for API/ABI compatibility */
980 queue->temp_file = g_fopen (queue->temp_location, "wb+");
981 /* error creating file */
982 if (queue->temp_file == NULL)
986 queue->writing_pos = 0;
987 queue->reading_pos = 0;
988 queue->max_reading_pos = 0;
995 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
996 (_("No Temp directory specified.")), (NULL));
1001 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1002 (_("Could not create temp file \"%s\"."), queue->temp_template),
1009 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1010 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1019 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1022 if (queue->temp_file == NULL)
1025 GST_DEBUG_OBJECT (queue, "closing temp file");
1027 fflush (queue->temp_file);
1028 fclose (queue->temp_file);
1030 if (queue->temp_remove)
1031 remove (queue->temp_location);
1033 queue->temp_file = NULL;
1037 gst_queue2_flush_temp_file (GstQueue2 * queue)
1039 if (queue->temp_file == NULL)
1042 GST_DEBUG_OBJECT (queue, "flushing temp file");
1044 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1046 queue->writing_pos = 0;
1047 queue->reading_pos = 0;
1048 queue->max_reading_pos = 0;
1052 gst_queue2_locked_flush (GstQueue2 * queue)
1054 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1055 gst_queue2_flush_temp_file (queue);
1057 while (!g_queue_is_empty (queue->queue)) {
1058 GstMiniObject *data = g_queue_pop_head (queue->queue);
1060 /* Then lose another reference because we are supposed to destroy that
1061 data when flushing */
1062 gst_mini_object_unref (data);
1065 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1066 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1067 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1068 if (queue->starting_segment != NULL)
1069 gst_event_unref (queue->starting_segment);
1070 queue->starting_segment = NULL;
1071 queue->segment_event_received = FALSE;
1073 /* we deleted a lot of something */
1074 GST_QUEUE2_SIGNAL_DEL (queue);
1077 /* enqueue an item an update the level stats */
1079 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item)
1081 if (GST_IS_BUFFER (item)) {
1085 buffer = GST_BUFFER_CAST (item);
1086 size = GST_BUFFER_SIZE (buffer);
1088 /* add buffer to the statistics */
1089 queue->cur_level.buffers++;
1090 queue->cur_level.bytes += size;
1091 queue->bytes_in += size;
1093 /* apply new buffer to segment stats */
1094 apply_buffer (queue, buffer, &queue->sink_segment);
1095 /* update the byterate stats */
1096 update_in_rates (queue);
1098 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1099 gst_queue2_write_buffer_to_file (queue, buffer);
1102 } else if (GST_IS_EVENT (item)) {
1105 event = GST_EVENT_CAST (item);
1107 switch (GST_EVENT_TYPE (event)) {
1109 /* Zero the thresholds, this makes sure the queue is completely
1110 * filled and we can read all data from the queue. */
1111 queue->is_eos = TRUE;
1113 case GST_EVENT_NEWSEGMENT:
1114 apply_segment (queue, event, &queue->sink_segment);
1115 /* This is our first new segment, we hold it
1116 * as we can't save it on the temp file */
1117 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1118 if (queue->segment_event_received)
1119 goto unexpected_event;
1121 queue->segment_event_received = TRUE;
1122 if (queue->starting_segment != NULL)
1123 gst_event_unref (queue->starting_segment);
1124 queue->starting_segment = event;
1127 /* a new segment allows us to accept more buffers if we got UNEXPECTED
1128 * from downstream */
1129 queue->unexpected = FALSE;
1132 if (QUEUE_IS_USING_TEMP_FILE (queue))
1133 goto unexpected_event;
1137 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1138 item, GST_OBJECT_NAME (queue));
1139 /* we can't really unref since we don't know what it is */
1144 /* update the buffering status */
1145 update_buffering (queue);
1147 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1148 g_queue_push_tail (queue->queue, item);
1150 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
1152 GST_QUEUE2_SIGNAL_ADD (queue);
1161 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
1162 gst_event_type_get_name (GST_EVENT_TYPE (item)),
1163 GST_OBJECT_NAME (queue));
1164 gst_event_unref (GST_EVENT_CAST (item));
1169 /* dequeue an item from the queue and update level stats */
1170 static GstMiniObject *
1171 gst_queue2_locked_dequeue (GstQueue2 * queue)
1173 GstMiniObject *item;
1175 if (QUEUE_IS_USING_TEMP_FILE (queue))
1176 item = gst_queue2_read_item_from_file (queue);
1178 item = g_queue_pop_head (queue->queue);
1183 if (GST_IS_BUFFER (item)) {
1187 buffer = GST_BUFFER_CAST (item);
1188 size = GST_BUFFER_SIZE (buffer);
1190 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1191 "retrieved buffer %p from queue", buffer);
1193 queue->cur_level.buffers--;
1194 queue->cur_level.bytes -= size;
1195 queue->bytes_out += size;
1196 apply_buffer (queue, buffer, &queue->src_segment);
1197 /* update the byterate stats */
1198 update_out_rates (queue);
1199 /* update the buffering */
1200 update_buffering (queue);
1202 } else if (GST_IS_EVENT (item)) {
1203 GstEvent *event = GST_EVENT_CAST (item);
1205 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1206 "retrieved event %p from queue", event);
1208 switch (GST_EVENT_TYPE (event)) {
1210 /* queue is empty now that we dequeued the EOS */
1211 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1213 case GST_EVENT_NEWSEGMENT:
1214 apply_segment (queue, event, &queue->src_segment);
1221 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
1222 item, GST_OBJECT_NAME (queue));
1225 GST_QUEUE2_SIGNAL_DEL (queue);
1232 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
1238 gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event)
1242 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
1244 switch (GST_EVENT_TYPE (event)) {
1245 case GST_EVENT_FLUSH_START:
1247 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
1249 gst_pad_push_event (queue->srcpad, event);
1251 /* now unblock the chain function */
1252 GST_QUEUE2_MUTEX_LOCK (queue);
1253 queue->srcresult = GST_FLOW_WRONG_STATE;
1254 /* unblock the loop and chain functions */
1255 g_cond_signal (queue->item_add);
1256 g_cond_signal (queue->item_del);
1257 GST_QUEUE2_MUTEX_UNLOCK (queue);
1259 /* make sure it pauses, this should happen since we sent
1260 * flush_start downstream. */
1261 gst_pad_pause_task (queue->srcpad);
1262 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
1265 case GST_EVENT_FLUSH_STOP:
1267 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
1269 gst_pad_push_event (queue->srcpad, event);
1271 GST_QUEUE2_MUTEX_LOCK (queue);
1272 gst_queue2_locked_flush (queue);
1273 queue->srcresult = GST_FLOW_OK;
1274 queue->is_eos = FALSE;
1275 queue->unexpected = FALSE;
1276 /* reset rate counters */
1277 reset_rate_timer (queue);
1278 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
1280 GST_QUEUE2_MUTEX_UNLOCK (queue);
1284 if (GST_EVENT_IS_SERIALIZED (event)) {
1285 /* serialized events go in the queue */
1286 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, out_flushing);
1287 /* refuse more events on EOS */
1290 gst_queue2_locked_enqueue (queue, event);
1291 GST_QUEUE2_MUTEX_UNLOCK (queue);
1293 /* non-serialized events are passed upstream. */
1294 gst_pad_push_event (queue->srcpad, event);
1304 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
1305 GST_QUEUE2_MUTEX_UNLOCK (queue);
1306 gst_event_unref (event);
1311 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
1312 GST_QUEUE2_MUTEX_UNLOCK (queue);
1313 gst_event_unref (event);
1319 gst_queue2_is_empty (GstQueue2 * queue)
1321 /* never empty on EOS */
1325 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1326 return queue->writing_pos == queue->max_reading_pos;
1328 if (queue->queue->length == 0)
1336 gst_queue2_is_filled (GstQueue2 * queue)
1340 /* always filled on EOS */
1344 /* if using file, we're never filled if we don't have EOS */
1345 if (QUEUE_IS_USING_TEMP_FILE (queue))
1348 /* we are never filled when we have no buffers at all */
1349 if (queue->cur_level.buffers == 0)
1352 #define CHECK_FILLED(format) ((queue->max_level.format) > 0 && \
1353 (queue->cur_level.format) >= (queue->max_level.format))
1355 /* we are filled if one of the current levels exceeds the max */
1356 res = CHECK_FILLED (buffers) || CHECK_FILLED (bytes) || CHECK_FILLED (time);
1358 /* if we need to, use the rate estimate to check against the max time we are
1359 * allowed to queue */
1360 if (queue->use_rate_estimate)
1361 res |= CHECK_FILLED (rate_time);
1367 static GstFlowReturn
1368 gst_queue2_chain (GstPad * pad, GstBuffer * buffer)
1372 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
1374 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1375 "received buffer %p of size %d, time %" GST_TIME_FORMAT ", duration %"
1376 GST_TIME_FORMAT, buffer, GST_BUFFER_SIZE (buffer),
1377 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1378 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1380 /* we have to lock the queue since we span threads */
1381 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, out_flushing);
1382 /* when we received EOS, we refuse more data */
1385 /* when we received unexpected from downstream, refuse more buffers */
1386 if (queue->unexpected)
1387 goto out_unexpected;
1389 /* We make space available if we're "full" according to whatever
1390 * the user defined as "full". */
1391 if (gst_queue2_is_filled (queue)) {
1394 /* pause the timer while we wait. The fact that we are waiting does not mean
1395 * the byterate on the input pad is lower */
1396 if ((started = queue->in_timer_started))
1397 g_timer_stop (queue->in_timer);
1399 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1400 "queue is full, waiting for free space");
1402 /* Wait for space to be available, we could be unlocked because of a flush. */
1403 GST_QUEUE2_WAIT_DEL_CHECK (queue, out_flushing);
1405 while (gst_queue2_is_filled (queue));
1407 /* and continue if we were running before */
1409 g_timer_continue (queue->in_timer);
1412 /* put buffer in queue now */
1413 gst_queue2_locked_enqueue (queue, buffer);
1414 GST_QUEUE2_MUTEX_UNLOCK (queue);
1418 /* special conditions */
1421 GstFlowReturn ret = queue->srcresult;
1423 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1424 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1425 GST_QUEUE2_MUTEX_UNLOCK (queue);
1426 gst_buffer_unref (buffer);
1432 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
1433 GST_QUEUE2_MUTEX_UNLOCK (queue);
1434 gst_buffer_unref (buffer);
1436 return GST_FLOW_UNEXPECTED;
1440 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1441 "exit because we received UNEXPECTED");
1442 GST_QUEUE2_MUTEX_UNLOCK (queue);
1443 gst_buffer_unref (buffer);
1445 return GST_FLOW_UNEXPECTED;
1449 /* dequeue an item from the queue an push it downstream. This functions returns
1450 * the result of the push. */
1451 static GstFlowReturn
1452 gst_queue2_push_one (GstQueue2 * queue)
1454 GstFlowReturn result = GST_FLOW_OK;
1455 GstMiniObject *data;
1457 data = gst_queue2_locked_dequeue (queue);
1462 if (GST_IS_BUFFER (data)) {
1466 buffer = GST_BUFFER_CAST (data);
1467 caps = GST_BUFFER_CAPS (buffer);
1469 GST_QUEUE2_MUTEX_UNLOCK (queue);
1471 /* set caps before pushing the buffer so that core does not try to do
1472 * something fancy to check if this is possible. */
1473 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
1474 gst_pad_set_caps (queue->srcpad, caps);
1476 result = gst_pad_push (queue->srcpad, buffer);
1478 /* need to check for srcresult here as well */
1479 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, out_flushing);
1480 if (result == GST_FLOW_UNEXPECTED) {
1481 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1482 "got UNEXPECTED from downstream");
1483 /* stop pushing buffers, we dequeue all items until we see an item that we
1484 * can push again, which is EOS or NEWSEGMENT. If there is nothing in the
1485 * queue we can push, we set a flag to make the sinkpad refuse more
1486 * buffers with an UNEXPECTED return value until we receive something
1487 * pushable again or we get flushed. */
1488 while ((data = gst_queue2_locked_dequeue (queue))) {
1489 if (GST_IS_BUFFER (data)) {
1490 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1491 "dropping UNEXPECTED buffer %p", data);
1492 gst_buffer_unref (GST_BUFFER_CAST (data));
1493 } else if (GST_IS_EVENT (data)) {
1494 GstEvent *event = GST_EVENT_CAST (data);
1495 GstEventType type = GST_EVENT_TYPE (event);
1497 if (type == GST_EVENT_EOS || type == GST_EVENT_NEWSEGMENT) {
1498 /* we found a pushable item in the queue, push it out */
1499 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1500 "pushing pushable event %s after UNEXPECTED",
1501 GST_EVENT_TYPE_NAME (event));
1504 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1505 "dropping UNEXPECTED event %p", event);
1506 gst_event_unref (event);
1509 /* no more items in the queue. Set the unexpected flag so that upstream
1510 * make us refuse any more buffers on the sinkpad. Since we will still
1511 * accept EOS and NEWSEGMENT we return _FLOW_OK to the caller so that the
1512 * task function does not shut down. */
1513 queue->unexpected = TRUE;
1514 result = GST_FLOW_OK;
1516 } else if (GST_IS_EVENT (data)) {
1517 GstEvent *event = GST_EVENT_CAST (data);
1518 GstEventType type = GST_EVENT_TYPE (event);
1520 GST_QUEUE2_MUTEX_UNLOCK (queue);
1522 gst_pad_push_event (queue->srcpad, event);
1524 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, out_flushing);
1525 /* if we're EOS, return UNEXPECTED so that the task pauses. */
1526 if (type == GST_EVENT_EOS) {
1527 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1528 "pushed EOS event %p, return UNEXPECTED", event);
1529 result = GST_FLOW_UNEXPECTED;
1537 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1538 "exit because we have no item in the queue");
1539 return GST_FLOW_ERROR;
1543 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
1544 return GST_FLOW_WRONG_STATE;
1548 /* called repeadedly with @pad as the source pad. This function should push out
1549 * data to the peer element. */
1551 gst_queue2_loop (GstPad * pad)
1556 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
1558 /* have to lock for thread-safety */
1559 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, out_flushing);
1561 if (gst_queue2_is_empty (queue)) {
1564 /* pause the timer while we wait. The fact that we are waiting does not mean
1565 * the byterate on the output pad is lower */
1566 if ((started = queue->out_timer_started))
1567 g_timer_stop (queue->out_timer);
1569 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1570 "queue is empty, waiting for new data");
1572 /* Wait for data to be available, we could be unlocked because of a flush. */
1573 GST_QUEUE2_WAIT_ADD_CHECK (queue, out_flushing);
1575 while (gst_queue2_is_empty (queue));
1577 /* and continue if we were running before */
1579 g_timer_continue (queue->out_timer);
1581 ret = gst_queue2_push_one (queue);
1582 queue->srcresult = ret;
1583 if (ret != GST_FLOW_OK)
1586 GST_QUEUE2_MUTEX_UNLOCK (queue);
1593 gboolean eos = queue->is_eos;
1594 GstFlowReturn ret = queue->srcresult;
1596 gst_pad_pause_task (queue->srcpad);
1597 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
1598 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
1599 GST_QUEUE2_MUTEX_UNLOCK (queue);
1600 /* let app know about us giving up if upstream is not expected to do so */
1601 /* UNEXPECTED is already taken care of elsewhere */
1602 if (eos && (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) &&
1603 (ret != GST_FLOW_UNEXPECTED)) {
1604 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
1605 (_("Internal data flow error.")),
1606 ("streaming task paused, reason %s (%d)",
1607 gst_flow_get_name (ret), ret));
1608 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
1615 gst_queue2_handle_src_event (GstPad * pad, GstEvent * event)
1617 gboolean res = TRUE;
1618 GstQueue2 *queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
1620 #ifndef GST_DISABLE_GST_DEBUG
1621 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
1622 event, GST_EVENT_TYPE_NAME (event));
1625 if (!QUEUE_IS_USING_TEMP_FILE (queue)) {
1626 /* just forward upstream */
1627 res = gst_pad_push_event (queue->sinkpad, event);
1629 /* when using a temp file, we unblock the pending read */
1631 gst_event_unref (event);
1638 gst_queue2_peer_query (GstQueue2 * queue, GstPad * pad, GstQuery * query)
1640 gboolean ret = FALSE;
1643 if ((peer = gst_pad_get_peer (pad))) {
1644 ret = gst_pad_query (peer, query);
1645 gst_object_unref (peer);
1651 gst_queue2_handle_src_query (GstPad * pad, GstQuery * query)
1655 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
1657 switch (GST_QUERY_TYPE (query)) {
1658 case GST_QUERY_POSITION:
1663 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
1666 /* get peer position */
1667 gst_query_parse_position (query, &format, &peer_pos);
1669 /* FIXME: this code assumes that there's no discont in the queue */
1671 case GST_FORMAT_BYTES:
1672 peer_pos -= queue->cur_level.bytes;
1674 case GST_FORMAT_TIME:
1675 peer_pos -= queue->cur_level.time;
1678 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
1679 "know how to adjust value", gst_format_get_name (format));
1682 /* set updated position */
1683 gst_query_set_position (query, format, peer_pos);
1686 case GST_QUERY_DURATION:
1688 GST_DEBUG_OBJECT (queue, "doing peer query");
1690 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
1693 GST_DEBUG_OBJECT (queue, "peer query success");
1696 case GST_QUERY_BUFFERING:
1700 GST_DEBUG_OBJECT (queue, "query buffering");
1702 if (!QUEUE_IS_USING_TEMP_FILE (queue)) {
1703 /* no temp file, just forward to the peer */
1704 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
1706 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
1710 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1713 case GST_FORMAT_PERCENT:
1718 peer_fmt = GST_FORMAT_BYTES;
1720 if (!gst_pad_query_peer_duration (queue->sinkpad, &peer_fmt,
1724 GST_DEBUG_OBJECT (queue, "duration %" G_GINT64_FORMAT ", writing %"
1725 G_GINT64_FORMAT, duration, queue->writing_pos);
1728 /* get our available data relative to the duration */
1730 stop = GST_FORMAT_PERCENT_MAX * queue->writing_pos / duration;
1735 case GST_FORMAT_BYTES:
1737 stop = queue->writing_pos;
1744 gst_query_set_buffering_percent (query, queue->is_buffering, 100);
1745 gst_query_set_buffering_range (query, format, start, stop, -1);
1750 /* peer handled other queries */
1751 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
1761 GST_DEBUG_OBJECT (queue, "failed peer query");
1766 static GstFlowReturn
1767 gst_queue2_get_range (GstPad * pad, guint64 offset, guint length,
1768 GstBuffer ** buffer)
1773 queue = GST_QUEUE2_CAST (gst_pad_get_parent (pad));
1775 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, out_flushing);
1776 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
1777 offset = (offset == -1) ? queue->reading_pos : offset;
1779 /* function will block when the range is not yet available */
1780 ret = gst_queue2_create_read (queue, offset, length, buffer);
1781 GST_QUEUE2_MUTEX_UNLOCK (queue);
1783 gst_object_unref (queue);
1790 GST_DEBUG_OBJECT (queue, "we are flushing");
1791 GST_QUEUE2_MUTEX_UNLOCK (queue);
1792 return GST_FLOW_WRONG_STATE;
1797 gst_queue2_src_checkgetrange_function (GstPad * pad)
1802 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
1804 /* we can operate in pull mode when we are using a tempfile */
1805 ret = QUEUE_IS_USING_TEMP_FILE (queue);
1807 gst_object_unref (GST_OBJECT (queue));
1812 /* sink currently only operates in push mode */
1814 gst_queue2_sink_activate_push (GstPad * pad, gboolean active)
1816 gboolean result = TRUE;
1819 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
1822 GST_QUEUE2_MUTEX_LOCK (queue);
1823 GST_DEBUG_OBJECT (queue, "activating push mode");
1824 queue->srcresult = GST_FLOW_OK;
1825 queue->is_eos = FALSE;
1826 queue->unexpected = FALSE;
1827 reset_rate_timer (queue);
1828 GST_QUEUE2_MUTEX_UNLOCK (queue);
1830 /* unblock chain function */
1831 GST_QUEUE2_MUTEX_LOCK (queue);
1832 GST_DEBUG_OBJECT (queue, "deactivating push mode");
1833 queue->srcresult = GST_FLOW_WRONG_STATE;
1834 gst_queue2_locked_flush (queue);
1835 GST_QUEUE2_MUTEX_UNLOCK (queue);
1838 gst_object_unref (queue);
1843 /* src operating in push mode, we start a task on the source pad that pushes out
1844 * buffers from the queue */
1846 gst_queue2_src_activate_push (GstPad * pad, gboolean active)
1848 gboolean result = FALSE;
1851 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
1854 GST_QUEUE2_MUTEX_LOCK (queue);
1855 GST_DEBUG_OBJECT (queue, "activating push mode");
1856 queue->srcresult = GST_FLOW_OK;
1857 queue->is_eos = FALSE;
1858 queue->unexpected = FALSE;
1859 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
1860 GST_QUEUE2_MUTEX_UNLOCK (queue);
1862 /* unblock loop function */
1863 GST_QUEUE2_MUTEX_LOCK (queue);
1864 GST_DEBUG_OBJECT (queue, "deactivating push mode");
1865 queue->srcresult = GST_FLOW_WRONG_STATE;
1866 /* the item add signal will unblock */
1867 g_cond_signal (queue->item_add);
1868 GST_QUEUE2_MUTEX_UNLOCK (queue);
1870 /* step 2, make sure streaming finishes */
1871 result = gst_pad_stop_task (pad);
1874 gst_object_unref (queue);
1879 /* pull mode, downstream will call our getrange function */
1881 gst_queue2_src_activate_pull (GstPad * pad, gboolean active)
1886 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
1889 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1890 GST_QUEUE2_MUTEX_LOCK (queue);
1891 GST_DEBUG_OBJECT (queue, "activating pull mode");
1892 queue->srcresult = GST_FLOW_OK;
1893 queue->is_eos = FALSE;
1894 queue->unexpected = FALSE;
1896 GST_QUEUE2_MUTEX_UNLOCK (queue);
1898 GST_QUEUE2_MUTEX_LOCK (queue);
1899 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
1900 /* this is not allowed, we cannot operate in pull mode without a temp
1902 queue->srcresult = GST_FLOW_WRONG_STATE;
1904 GST_QUEUE2_MUTEX_UNLOCK (queue);
1907 GST_QUEUE2_MUTEX_LOCK (queue);
1908 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
1909 queue->srcresult = GST_FLOW_WRONG_STATE;
1910 /* this will unlock getrange */
1911 g_cond_signal (queue->item_add);
1913 GST_QUEUE2_MUTEX_UNLOCK (queue);
1915 gst_object_unref (queue);
1920 static GstStateChangeReturn
1921 gst_queue2_change_state (GstElement * element, GstStateChange transition)
1924 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1926 queue = GST_QUEUE2 (element);
1928 switch (transition) {
1929 case GST_STATE_CHANGE_NULL_TO_READY:
1931 case GST_STATE_CHANGE_READY_TO_PAUSED:
1932 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1933 if (!gst_queue2_open_temp_location_file (queue))
1934 ret = GST_STATE_CHANGE_FAILURE;
1936 queue->segment_event_received = FALSE;
1937 queue->starting_segment = NULL;
1939 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1945 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1947 switch (transition) {
1948 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1950 case GST_STATE_CHANGE_PAUSED_TO_READY:
1951 if (QUEUE_IS_USING_TEMP_FILE (queue))
1952 gst_queue2_close_temp_location_file (queue);
1953 if (queue->starting_segment != NULL) {
1954 gst_event_unref (queue->starting_segment);
1955 queue->starting_segment = NULL;
1958 case GST_STATE_CHANGE_READY_TO_NULL:
1967 /* changing the capacity of the queue must wake up
1968 * the _chain function, it might have more room now
1969 * to store the buffer/event in the queue */
1970 #define QUEUE_CAPACITY_CHANGE(q)\
1971 g_cond_signal (queue->item_del);
1973 /* Changing the minimum required fill level must
1974 * wake up the _loop function as it might now
1975 * be able to preceed.
1977 #define QUEUE_THRESHOLD_CHANGE(q)\
1978 g_cond_signal (queue->item_add);
1981 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
1985 /* the element must be stopped in order to do this */
1986 GST_OBJECT_LOCK (queue);
1987 state = GST_STATE (queue);
1988 if (state != GST_STATE_READY && state != GST_STATE_NULL)
1990 GST_OBJECT_UNLOCK (queue);
1992 /* set new location */
1993 g_free (queue->temp_template);
1994 queue->temp_template = g_strdup (template);
2001 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
2002 GST_OBJECT_UNLOCK (queue);
2007 gst_queue2_set_property (GObject * object,
2008 guint prop_id, const GValue * value, GParamSpec * pspec)
2010 GstQueue2 *queue = GST_QUEUE2 (object);
2012 /* someone could change levels here, and since this
2013 * affects the get/put funcs, we need to lock for safety. */
2014 GST_QUEUE2_MUTEX_LOCK (queue);
2017 case PROP_MAX_SIZE_BYTES:
2018 queue->max_level.bytes = g_value_get_uint (value);
2019 QUEUE_CAPACITY_CHANGE (queue);
2021 case PROP_MAX_SIZE_BUFFERS:
2022 queue->max_level.buffers = g_value_get_uint (value);
2023 QUEUE_CAPACITY_CHANGE (queue);
2025 case PROP_MAX_SIZE_TIME:
2026 queue->max_level.time = g_value_get_uint64 (value);
2027 /* set rate_time to the same value. We use an extra field in the level
2028 * structure so that we can easily access and compare it */
2029 queue->max_level.rate_time = queue->max_level.time;
2030 QUEUE_CAPACITY_CHANGE (queue);
2032 case PROP_USE_BUFFERING:
2033 queue->use_buffering = g_value_get_boolean (value);
2035 case PROP_USE_RATE_ESTIMATE:
2036 queue->use_rate_estimate = g_value_get_boolean (value);
2038 case PROP_LOW_PERCENT:
2039 queue->low_percent = g_value_get_int (value);
2041 case PROP_HIGH_PERCENT:
2042 queue->high_percent = g_value_get_int (value);
2044 case PROP_TEMP_TEMPLATE:
2045 gst_queue2_set_temp_template (queue, g_value_get_string (value));
2047 case PROP_TEMP_LOCATION:
2048 g_free (queue->temp_location);
2049 queue->temp_location = g_value_dup_string (value);
2050 /* you can set the property back to NULL to make it use the temp-tmpl
2052 queue->temp_location_set = queue->temp_location != NULL;
2054 case PROP_TEMP_REMOVE:
2055 queue->temp_remove = g_value_get_boolean (value);
2058 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2062 GST_QUEUE2_MUTEX_UNLOCK (queue);
2066 gst_queue2_get_property (GObject * object,
2067 guint prop_id, GValue * value, GParamSpec * pspec)
2069 GstQueue2 *queue = GST_QUEUE2 (object);
2071 GST_QUEUE2_MUTEX_LOCK (queue);
2074 case PROP_CUR_LEVEL_BYTES:
2075 g_value_set_uint (value, queue->cur_level.bytes);
2077 case PROP_CUR_LEVEL_BUFFERS:
2078 g_value_set_uint (value, queue->cur_level.buffers);
2080 case PROP_CUR_LEVEL_TIME:
2081 g_value_set_uint64 (value, queue->cur_level.time);
2083 case PROP_MAX_SIZE_BYTES:
2084 g_value_set_uint (value, queue->max_level.bytes);
2086 case PROP_MAX_SIZE_BUFFERS:
2087 g_value_set_uint (value, queue->max_level.buffers);
2089 case PROP_MAX_SIZE_TIME:
2090 g_value_set_uint64 (value, queue->max_level.time);
2092 case PROP_USE_BUFFERING:
2093 g_value_set_boolean (value, queue->use_buffering);
2095 case PROP_USE_RATE_ESTIMATE:
2096 g_value_set_boolean (value, queue->use_rate_estimate);
2098 case PROP_LOW_PERCENT:
2099 g_value_set_int (value, queue->low_percent);
2101 case PROP_HIGH_PERCENT:
2102 g_value_set_int (value, queue->high_percent);
2104 case PROP_TEMP_TEMPLATE:
2105 g_value_set_string (value, queue->temp_template);
2107 case PROP_TEMP_LOCATION:
2108 g_value_set_string (value, queue->temp_location);
2110 case PROP_TEMP_REMOVE:
2111 g_value_set_boolean (value, queue->temp_remove);
2114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2118 GST_QUEUE2_MUTEX_UNLOCK (queue);