2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2003 Colin Walters <cwalters@gnome.org>
4 * 2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5 * 2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6 * SA 2010 ST-Ericsson <benjamin.gaignard@stericsson.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-queue2
30 * Data is queued until one of the limits specified by the
31 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
32 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
33 * more buffers into the queue will block the pushing thread until more space
36 * The queue will create a new thread on the source pad to decouple the
37 * processing on sink and source pad.
39 * You can query how many buffers are queued by reading the
40 * #GstQueue2:current-level-buffers property.
42 * The default queue size limits are 100 buffers, 2MB of data, or
43 * two seconds worth of data, whichever is reached first.
45 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
46 * will allocate a random free filename and buffer data in the file.
47 * By using this, it will buffer the entire stream data on the file independently
48 * of the queue size limits, they will only be used for buffering statistics.
50 * The temp-location property will be used to notify the application of the
58 #include "gstqueue2.h"
60 #include <glib/gstdio.h>
62 #include "gst/gst-i18n-lib.h"
63 #include "gst/glib-compat-private.h"
68 #include <io.h> /* lseek, open, close, read */
70 #define lseek _lseeki64
77 #ifdef __BIONIC__ /* Android */
85 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
90 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
95 GST_DEBUG_CATEGORY_STATIC (queue_debug);
96 #define GST_CAT_DEFAULT (queue_debug)
97 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
105 #define DEFAULT_BUFFER_SIZE 4096
106 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_template != NULL)
107 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
108 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
110 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
112 /* default property values */
113 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
114 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
115 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
116 #define DEFAULT_USE_BUFFERING FALSE
117 #define DEFAULT_USE_TAGS_BITRATE FALSE
118 #define DEFAULT_USE_RATE_ESTIMATE TRUE
119 #define DEFAULT_LOW_PERCENT 10
120 #define DEFAULT_HIGH_PERCENT 99
121 #define DEFAULT_LOW_WATERMARK 0.01
122 #define DEFAULT_HIGH_WATERMARK 0.99
123 #define DEFAULT_TEMP_REMOVE TRUE
124 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
129 PROP_CUR_LEVEL_BUFFERS,
130 PROP_CUR_LEVEL_BYTES,
132 PROP_MAX_SIZE_BUFFERS,
136 PROP_USE_TAGS_BITRATE,
137 PROP_USE_RATE_ESTIMATE,
145 PROP_RING_BUFFER_MAX_SIZE,
147 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
153 /* Explanation for buffer levels and percentages:
155 * The buffering_level functions here return a value in a normalized range
156 * that specifies the queue's current fill level. The range goes from 0 to
157 * MAX_BUFFERING_LEVEL. The low/high watermarks also use this same range.
159 * This is not to be confused with the buffering_percent value, which is
160 * a *relative* quantity - relative to the low/high watermarks.
161 * buffering_percent = 0% means buffering_level is at the low watermark.
162 * buffering_percent = 100% means buffering_level is at the high watermark.
163 * buffering_percent is used for determining if the fill level has reached
164 * the high watermark, and for producing BUFFERING messages. This value
165 * always uses a 0..100 range (since it is a percentage).
167 * To avoid future confusions, whenever "buffering level" is mentioned, it
168 * refers to the absolute level which is in the 0..MAX_BUFFERING_LEVEL
169 * range. Whenever "buffering_percent" is mentioned, it refers to the
170 * percentage value that is relative to the low/high watermark. */
172 /* Using a buffering level range of 0..1000000 to allow for a
173 * resolution in ppm (1 ppm = 0.0001%) */
174 #define MAX_BUFFERING_LEVEL 1000000
176 /* How much 1% makes up in the buffer level range */
177 #define BUF_LEVEL_PERCENT_FACTOR ((MAX_BUFFERING_LEVEL) / 100)
179 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
186 #define STATUS(queue, pad, msg) \
187 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
188 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
189 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
190 " ns, %"G_GUINT64_FORMAT" items", \
191 GST_DEBUG_PAD_NAME (pad), \
192 queue->cur_level.buffers, \
193 queue->max_level.buffers, \
194 queue->cur_level.bytes, \
195 queue->max_level.bytes, \
196 queue->cur_level.time, \
197 queue->max_level.time, \
198 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
199 queue->current->writing_pos - queue->current->max_reading_pos : \
200 queue->queue.length))
202 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
203 g_mutex_lock (&q->qlock); \
206 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
207 GST_QUEUE2_MUTEX_LOCK (q); \
208 if (res != GST_FLOW_OK) \
212 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
213 g_mutex_unlock (&q->qlock); \
216 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
217 STATUS (queue, q->sinkpad, "wait for DEL"); \
218 q->waiting_del = TRUE; \
219 g_cond_wait (&q->item_del, &queue->qlock); \
220 q->waiting_del = FALSE; \
221 if (res != GST_FLOW_OK) { \
222 STATUS (queue, q->srcpad, "received DEL wakeup"); \
225 STATUS (queue, q->sinkpad, "received DEL"); \
228 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
229 STATUS (queue, q->srcpad, "wait for ADD"); \
230 q->waiting_add = TRUE; \
231 g_cond_wait (&q->item_add, &q->qlock); \
232 q->waiting_add = FALSE; \
233 if (res != GST_FLOW_OK) { \
234 STATUS (queue, q->srcpad, "received ADD wakeup"); \
237 STATUS (queue, q->srcpad, "received ADD"); \
240 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
241 if (q->waiting_del) { \
242 STATUS (q, q->srcpad, "signal DEL"); \
243 g_cond_signal (&q->item_del); \
247 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
248 if (q->waiting_add) { \
249 STATUS (q, q->sinkpad, "signal ADD"); \
250 g_cond_signal (&q->item_add); \
254 #define SET_PERCENT(q, perc) G_STMT_START { \
255 if (perc != q->buffering_percent) { \
256 q->buffering_percent = perc; \
257 q->percent_changed = TRUE; \
258 GST_DEBUG_OBJECT (q, "buffering %d percent", perc); \
259 get_buffering_stats (q, perc, &q->mode, &q->avg_in, &q->avg_out, \
260 &q->buffering_left); \
265 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
266 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
267 "dataflow inside the queue element");
268 #define gst_queue2_parent_class parent_class
269 G_DEFINE_TYPE_WITH_CODE (GstQueue2, gst_queue2, GST_TYPE_ELEMENT, _do_init);
271 static void gst_queue2_finalize (GObject * object);
273 static void gst_queue2_set_property (GObject * object,
274 guint prop_id, const GValue * value, GParamSpec * pspec);
275 static void gst_queue2_get_property (GObject * object,
276 guint prop_id, GValue * value, GParamSpec * pspec);
278 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstObject * parent,
280 static GstFlowReturn gst_queue2_chain_list (GstPad * pad, GstObject * parent,
281 GstBufferList * buffer_list);
282 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
283 static void gst_queue2_loop (GstPad * pad);
285 static GstFlowReturn gst_queue2_handle_sink_event (GstPad * pad,
286 GstObject * parent, GstEvent * event);
287 static gboolean gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
290 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstObject * parent,
292 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstObject * parent,
294 static gboolean gst_queue2_handle_query (GstElement * element,
297 static GstFlowReturn gst_queue2_get_range (GstPad * pad, GstObject * parent,
298 guint64 offset, guint length, GstBuffer ** buffer);
300 static gboolean gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent,
301 GstPadMode mode, gboolean active);
302 static gboolean gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
303 GstPadMode mode, gboolean active);
304 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
305 GstStateChange transition);
307 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
308 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
310 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
311 static void update_in_rates (GstQueue2 * queue, gboolean force);
312 static GstMessage *gst_queue2_get_buffering_message (GstQueue2 * queue);
313 static void gst_queue2_post_buffering (GstQueue2 * queue);
314 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
315 static gboolean change_current_range(GstQueue2 * queue, GstQueue2Range *req_range, guint64 offset, guint length);
320 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
321 GST_QUEUE2_ITEM_TYPE_BUFFER,
322 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
323 GST_QUEUE2_ITEM_TYPE_EVENT,
324 GST_QUEUE2_ITEM_TYPE_QUERY
329 GstQueue2ItemType type;
333 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
336 gst_queue2_class_init (GstQueue2Class * klass)
338 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
339 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
341 gobject_class->set_property = gst_queue2_set_property;
342 gobject_class->get_property = gst_queue2_get_property;
345 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
346 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
347 "Current amount of data in the queue (bytes)",
348 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
350 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
351 "Current number of buffers in the queue",
352 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
354 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
355 "Current amount of data in the queue (in ns)",
356 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
358 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
359 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
360 "Max. amount of data in the queue (bytes, 0=disable)",
361 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
362 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
363 G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
365 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
366 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
367 DEFAULT_MAX_SIZE_BUFFERS,
368 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
369 G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
371 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
372 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
373 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
374 G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
377 g_param_spec_boolean ("use-buffering", "Use buffering",
378 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
379 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
380 G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_USE_TAGS_BITRATE,
382 g_param_spec_boolean ("use-tags-bitrate", "Use bitrate from tags",
383 "Use a bitrate from upstream tags to estimate buffer duration if not provided",
384 DEFAULT_USE_TAGS_BITRATE,
385 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
386 G_PARAM_STATIC_STRINGS));
387 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
388 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
389 "Estimate the bitrate of the stream to calculate time level",
390 DEFAULT_USE_RATE_ESTIMATE,
391 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
393 g_param_spec_int ("low-percent", "Low percent",
394 "Low threshold for buffering to start. Only used if use-buffering is True "
395 "(Deprecated: use low-watermark instead)",
396 0, 100, DEFAULT_LOW_WATERMARK * 100,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
399 g_param_spec_int ("high-percent", "High percent",
400 "High threshold for buffering to finish. Only used if use-buffering is True "
401 "(Deprecated: use high-watermark instead)",
402 0, 100, DEFAULT_HIGH_WATERMARK * 100,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 g_object_class_install_property (gobject_class, PROP_LOW_WATERMARK,
405 g_param_spec_double ("low-watermark", "Low watermark",
406 "Low threshold for buffering to start. Only used if use-buffering is True",
407 0.0, 1.0, DEFAULT_LOW_WATERMARK,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_HIGH_WATERMARK,
410 g_param_spec_double ("high-watermark", "High watermark",
411 "High threshold for buffering to finish. Only used if use-buffering is True",
412 0.0, 1.0, DEFAULT_HIGH_WATERMARK,
413 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
416 g_param_spec_string ("temp-template", "Temporary File Template",
417 "File template to store temporary files in, should contain directory "
418 "and XXXXXX. (NULL == disabled)",
419 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
422 g_param_spec_string ("temp-location", "Temporary File Location",
423 "Location to store temporary files in (Only read this property, "
424 "use temp-template to configure the name template)",
425 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
428 * GstQueue2:temp-remove
430 * When temp-template is set, remove the temporary file when going to READY.
432 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
433 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
434 "Remove the temp-location after use",
435 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 * GstQueue2:ring-buffer-max-size
440 * The maximum size of the ring buffer in bytes. If set to 0, the ring
441 * buffer is disabled. Default 0.
443 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
444 g_param_spec_uint64 ("ring-buffer-max-size",
445 "Max. ring buffer size (bytes)",
446 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
447 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 * GstQueue2:avg-in-rate
453 * The average input data rate.
455 g_object_class_install_property (gobject_class, PROP_AVG_IN_RATE,
456 g_param_spec_int64 ("avg-in-rate", "Input data rate (bytes/s)",
457 "Average input data rate (bytes/s)",
458 0, G_MAXINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
460 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
462 * GstQueue2:buffer-mode:
464 * Control the buffering mode used by the queue2.
466 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
467 g_param_spec_enum ("buffer-mode", "Buffer Mode",
468 "Control the buffering algorithm in use",
469 GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
470 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
472 /* set several parent class virtual functions */
473 gobject_class->finalize = gst_queue2_finalize;
475 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
476 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
478 gst_element_class_set_static_metadata (gstelement_class, "Queue 2",
481 "Erik Walthinsen <omega@cse.ogi.edu>, "
482 "Wim Taymans <wim.taymans@gmail.com>");
484 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
485 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
489 gst_queue2_init (GstQueue2 * queue)
491 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
493 gst_pad_set_chain_function (queue->sinkpad,
494 GST_DEBUG_FUNCPTR (gst_queue2_chain));
495 gst_pad_set_chain_list_function (queue->sinkpad,
496 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
497 gst_pad_set_activatemode_function (queue->sinkpad,
498 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_mode));
499 gst_pad_set_event_full_function (queue->sinkpad,
500 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
501 gst_pad_set_query_function (queue->sinkpad,
502 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_query));
503 GST_PAD_SET_PROXY_CAPS (queue->sinkpad);
504 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
506 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
508 gst_pad_set_activatemode_function (queue->srcpad,
509 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_mode));
510 gst_pad_set_getrange_function (queue->srcpad,
511 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
512 gst_pad_set_event_function (queue->srcpad,
513 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
514 gst_pad_set_query_function (queue->srcpad,
515 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
516 GST_PAD_SET_PROXY_CAPS (queue->srcpad);
517 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
520 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
521 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
522 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
523 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
524 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
525 queue->use_buffering = DEFAULT_USE_BUFFERING;
526 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
527 queue->low_watermark = DEFAULT_LOW_WATERMARK * MAX_BUFFERING_LEVEL;
528 queue->high_watermark = DEFAULT_HIGH_WATERMARK * MAX_BUFFERING_LEVEL;
530 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
531 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
533 queue->sinktime = GST_CLOCK_TIME_NONE;
534 queue->srctime = GST_CLOCK_TIME_NONE;
535 queue->sink_tainted = TRUE;
536 queue->src_tainted = TRUE;
538 queue->srcresult = GST_FLOW_FLUSHING;
539 queue->sinkresult = GST_FLOW_FLUSHING;
540 queue->is_eos = FALSE;
541 queue->in_timer = g_timer_new ();
542 queue->out_timer = g_timer_new ();
544 g_mutex_init (&queue->qlock);
545 queue->waiting_add = FALSE;
546 g_cond_init (&queue->item_add);
547 queue->waiting_del = FALSE;
548 g_cond_init (&queue->item_del);
549 g_queue_init (&queue->queue);
551 g_cond_init (&queue->query_handled);
552 queue->last_query = FALSE;
554 g_mutex_init (&queue->buffering_post_lock);
555 queue->buffering_percent = 100;
557 /* tempfile related */
558 queue->temp_template = NULL;
559 queue->temp_location = NULL;
560 queue->temp_remove = DEFAULT_TEMP_REMOVE;
562 queue->ring_buffer = NULL;
563 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
564 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
567 GST_DEBUG_OBJECT (queue,
568 "initialized queue's not_empty & not_full conditions");
571 /* called only once, as opposed to dispose */
573 gst_queue2_finalize (GObject * object)
575 GstQueue2 *queue = GST_QUEUE2 (object);
577 GST_DEBUG_OBJECT (queue, "finalizing queue");
579 while (!g_queue_is_empty (&queue->queue)) {
580 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
582 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
583 gst_mini_object_unref (qitem->item);
584 g_slice_free (GstQueue2Item, qitem);
587 queue->last_query = FALSE;
588 g_queue_clear (&queue->queue);
589 g_mutex_clear (&queue->qlock);
590 g_mutex_clear (&queue->buffering_post_lock);
591 g_cond_clear (&queue->item_add);
592 g_cond_clear (&queue->item_del);
593 g_cond_clear (&queue->query_handled);
594 g_timer_destroy (queue->in_timer);
595 g_timer_destroy (queue->out_timer);
597 /* temp_file path cleanup */
598 g_free (queue->temp_template);
599 g_free (queue->temp_location);
601 G_OBJECT_CLASS (parent_class)->finalize (object);
605 debug_ranges (GstQueue2 * queue)
607 GstQueue2Range *walk;
609 for (walk = queue->ranges; walk; walk = walk->next) {
610 GST_DEBUG_OBJECT (queue,
611 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
612 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
613 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
614 walk->rb_writing_pos, walk->reading_pos,
615 walk == queue->current ? "**y**" : " n ");
619 /* clear all the downloaded ranges */
621 clean_ranges (GstQueue2 * queue)
623 GST_DEBUG_OBJECT (queue, "clean queue ranges");
625 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
626 queue->ranges = NULL;
627 queue->current = NULL;
628 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
633 /* find a range that contains @offset or NULL when nothing does */
634 static GstQueue2Range *
635 find_range (GstQueue2 * queue, guint64 offset)
637 GstQueue2Range *range = NULL;
638 GstQueue2Range *walk;
640 /* first do a quick check for the current range */
641 for (walk = queue->ranges; walk; walk = walk->next) {
642 if (offset >= walk->offset && offset <= walk->writing_pos) {
643 /* we can reuse an existing range */
649 GST_DEBUG_OBJECT (queue,
650 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
651 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
653 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
659 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
661 guint64 max_reading_pos, writing_pos;
663 writing_pos = range->writing_pos;
664 max_reading_pos = range->max_reading_pos;
666 if (writing_pos > max_reading_pos)
667 queue->cur_level.bytes = writing_pos - max_reading_pos;
669 queue->cur_level.bytes = 0;
672 /* make a new range for @offset or reuse an existing range */
673 static GstQueue2Range *
674 add_range (GstQueue2 * queue, guint64 offset, gboolean update_existing)
676 GstQueue2Range *range, *prev, *next;
678 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
680 if ((range = find_range (queue, offset))) {
681 GST_DEBUG_OBJECT (queue,
682 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
684 if (update_existing && range->writing_pos != offset) {
685 GST_DEBUG_OBJECT (queue, "updating range writing position to "
686 "%" G_GUINT64_FORMAT, offset);
687 range->writing_pos = offset;
690 GST_DEBUG_OBJECT (queue,
691 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
693 range = g_slice_new0 (GstQueue2Range);
694 range->offset = offset;
695 /* we want to write to the next location in the ring buffer */
696 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
697 range->writing_pos = offset;
698 range->rb_writing_pos = range->rb_offset;
699 range->reading_pos = offset;
700 range->max_reading_pos = offset;
704 next = queue->ranges;
706 if (next->offset > offset) {
707 /* insert before next */
708 GST_DEBUG_OBJECT (queue,
709 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
721 queue->ranges = range;
723 debug_ranges (queue);
725 /* update the stats for this range */
726 update_cur_level (queue, range);
732 /* clear and init the download ranges for offset 0 */
734 init_ranges (GstQueue2 * queue)
736 GST_DEBUG_OBJECT (queue, "init queue ranges");
738 /* get rid of all the current ranges */
739 clean_ranges (queue);
740 /* make a range for offset 0 */
741 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
742 queue->current = queue->read = add_range (queue, 0, TRUE);
744 queue->current = add_range (queue, 0, TRUE);
748 /* calculate the diff between running time on the sink and src of the queue.
749 * This is the total amount of time in the queue. */
751 update_time_level (GstQueue2 * queue)
753 if (queue->sink_tainted) {
755 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
756 queue->sink_segment.position);
757 queue->sink_tainted = FALSE;
760 if (queue->src_tainted) {
762 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
763 queue->src_segment.position);
764 queue->src_tainted = FALSE;
767 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
768 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
770 if (queue->sinktime != GST_CLOCK_TIME_NONE
771 && queue->srctime != GST_CLOCK_TIME_NONE
772 && queue->sinktime >= queue->srctime)
773 queue->cur_level.time = queue->sinktime - queue->srctime;
775 queue->cur_level.time = 0;
778 /* take a SEGMENT event and apply the values to segment, updating the time
781 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
784 gst_event_copy_segment (event, segment);
786 if (segment->format == GST_FORMAT_BYTES) {
787 if (!QUEUE_IS_USING_QUEUE (queue) && is_sink) {
788 /* start is where we'll be getting from and as such writing next */
789 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
790 queue->current = queue->read = add_range (queue, segment->start, TRUE);
792 queue->current = add_range (queue, segment->start, TRUE);
797 /* now configure the values, we use these to track timestamps on the
799 if (segment->format != GST_FORMAT_TIME) {
800 /* non-time format, pretend the current time segment is closed with a
801 * 0 start and unknown stop time. */
802 segment->format = GST_FORMAT_TIME;
808 GST_DEBUG_OBJECT (queue, "configured SEGMENT %" GST_SEGMENT_FORMAT, segment);
811 queue->sink_tainted = TRUE;
813 queue->src_tainted = TRUE;
815 /* segment can update the time level of the queue */
816 update_time_level (queue);
820 apply_gap (GstQueue2 * queue, GstEvent * event,
821 GstSegment * segment, gboolean is_sink)
823 GstClockTime timestamp;
824 GstClockTime duration;
826 gst_event_parse_gap (event, ×tamp, &duration);
828 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
830 if (GST_CLOCK_TIME_IS_VALID (duration)) {
831 timestamp += duration;
834 segment->position = timestamp;
837 queue->sink_tainted = TRUE;
839 queue->src_tainted = TRUE;
841 /* calc diff with other end */
842 update_time_level (queue);
846 /* take a buffer and update segment, updating the time level of the queue. */
848 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
849 guint64 size, gboolean is_sink)
851 GstClockTime duration, timestamp;
853 timestamp = GST_BUFFER_DTS_OR_PTS (buffer);
854 duration = GST_BUFFER_DURATION (buffer);
856 /* If we have no duration, pick one from the bitrate if we can */
857 if (duration == GST_CLOCK_TIME_NONE && queue->use_tags_bitrate) {
859 is_sink ? queue->sink_tags_bitrate : queue->src_tags_bitrate;
861 duration = gst_util_uint64_scale (size, 8 * GST_SECOND, bitrate);
864 /* if no timestamp is set, assume it's continuous with the previous
866 if (timestamp == GST_CLOCK_TIME_NONE)
867 timestamp = segment->position;
870 if (duration != GST_CLOCK_TIME_NONE)
871 timestamp += duration;
873 GST_DEBUG_OBJECT (queue, "position updated to %" GST_TIME_FORMAT,
874 GST_TIME_ARGS (timestamp));
876 segment->position = timestamp;
879 queue->sink_tainted = TRUE;
881 queue->src_tainted = TRUE;
883 /* calc diff with other end */
884 update_time_level (queue);
889 GstClockTime timestamp;
894 buffer_list_apply_time (GstBuffer ** buf, guint idx, gpointer data)
896 struct BufListData *bld = data;
897 GstClockTime *timestamp = &bld->timestamp;
900 GST_TRACE ("buffer %u has pts %" GST_TIME_FORMAT " dts %" GST_TIME_FORMAT
901 " duration %" GST_TIME_FORMAT, idx,
902 GST_TIME_ARGS (GST_BUFFER_PTS (*buf)),
903 GST_TIME_ARGS (GST_BUFFER_DTS (*buf)),
904 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
906 btime = GST_BUFFER_DTS_OR_PTS (*buf);
907 if (GST_CLOCK_TIME_IS_VALID (btime))
910 if (GST_BUFFER_DURATION_IS_VALID (*buf))
911 *timestamp += GST_BUFFER_DURATION (*buf);
912 else if (bld->bitrate != 0) {
913 guint64 size = gst_buffer_get_size (*buf);
915 /* If we have no duration, pick one from the bitrate if we can */
916 *timestamp += gst_util_uint64_scale (bld->bitrate, 8 * GST_SECOND, size);
920 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
924 /* take a buffer list and update segment, updating the time level of the queue */
926 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
927 GstSegment * segment, gboolean is_sink)
929 struct BufListData bld;
931 /* if no timestamp is set, assume it's continuous with the previous time */
932 bld.timestamp = segment->position;
934 if (queue->use_tags_bitrate) {
936 bld.bitrate = queue->sink_tags_bitrate;
938 bld.bitrate = queue->src_tags_bitrate;
942 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, &bld);
944 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
945 GST_TIME_ARGS (bld.timestamp));
947 segment->position = bld.timestamp;
950 queue->sink_tainted = TRUE;
952 queue->src_tainted = TRUE;
954 /* calc diff with other end */
955 update_time_level (queue);
959 normalize_to_buffering_level (guint64 cur_level, guint64 max_level,
968 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL,
969 MIN (max_level, alt_max));
971 p = gst_util_uint64_scale (cur_level, MAX_BUFFERING_LEVEL, max_level);
973 return MIN (p, MAX_BUFFERING_LEVEL);
977 get_buffering_level (GstQueue2 * queue, gboolean * is_buffering,
978 gint * buffering_level)
980 gint buflevel, buflevel2;
982 if (queue->high_watermark <= 0) {
984 *buffering_level = MAX_BUFFERING_LEVEL;
986 *is_buffering = FALSE;
989 #define GET_BUFFER_LEVEL_FOR_QUANTITY(format,alt_max) \
990 normalize_to_buffering_level (queue->cur_level.format,queue->max_level.format,(alt_max))
993 /* on EOS we are always 100% full, we set the var here so that it we can
994 * reuse the logic below to stop buffering */
995 buflevel = MAX_BUFFERING_LEVEL;
996 GST_LOG_OBJECT (queue, "we are EOS");
998 GST_LOG_OBJECT (queue,
999 "Cur level bytes/time/buffers %u/%" GST_TIME_FORMAT "/%u",
1000 queue->cur_level.bytes, GST_TIME_ARGS (queue->cur_level.time),
1001 queue->cur_level.buffers);
1003 /* figure out the buffering level we are filled, we take the max of all formats. */
1004 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1005 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, 0);
1007 guint64 rb_size = queue->ring_buffer_max_size;
1008 buflevel = GET_BUFFER_LEVEL_FOR_QUANTITY (bytes, rb_size);
1011 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (time, 0);
1012 buflevel = MAX (buflevel, buflevel2);
1014 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (buffers, 0);
1015 buflevel = MAX (buflevel, buflevel2);
1017 /* also apply the rate estimate when we need to */
1018 if (queue->use_rate_estimate) {
1019 buflevel2 = GET_BUFFER_LEVEL_FOR_QUANTITY (rate_time, 0);
1020 buflevel = MAX (buflevel, buflevel2);
1023 /* Don't get to 0% unless we're really empty */
1024 if (queue->cur_level.bytes > 0)
1025 buflevel = MAX (1, buflevel);
1027 #undef GET_BUFFER_LEVEL_FOR_QUANTITY
1030 *is_buffering = queue->is_buffering;
1032 if (buffering_level)
1033 *buffering_level = buflevel;
1035 GST_DEBUG_OBJECT (queue, "buffering %d, level %d", queue->is_buffering,
1042 convert_to_buffering_percent (GstQueue2 * queue, gint buffering_level)
1046 /* scale so that if buffering_level equals the high watermark,
1047 * the percentage is 100% */
1048 percent = buffering_level * 100 / queue->high_watermark;
1057 get_buffering_stats (GstQueue2 * queue, gint percent, GstBufferingMode * mode,
1058 gint * avg_in, gint * avg_out, gint64 * buffering_left)
1060 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
1061 if (queue->mode != -1) {
1062 *mode = queue->mode;
1065 if (!QUEUE_IS_USING_QUEUE (queue)) {
1066 if (QUEUE_IS_USING_RING_BUFFER (queue))
1067 *mode = GST_BUFFERING_TIMESHIFT;
1069 *mode = GST_BUFFERING_DOWNLOAD;
1071 *mode = GST_BUFFERING_STREAM;
1077 if (!QUEUE_IS_USING_QUEUE (queue)) {
1078 if (QUEUE_IS_USING_RING_BUFFER (queue))
1079 *mode = GST_BUFFERING_TIMESHIFT;
1081 *mode = GST_BUFFERING_DOWNLOAD;
1083 *mode = GST_BUFFERING_STREAM;
1089 *avg_in = queue->byte_in_rate;
1091 *avg_out = queue->byte_out_rate;
1093 if (buffering_left) {
1094 *buffering_left = (percent == 100 ? 0 : -1);
1096 if (queue->use_rate_estimate) {
1099 max = queue->max_level.rate_time;
1100 cur = queue->cur_level.rate_time;
1102 if (percent != 100 && max > cur)
1103 *buffering_left = (max - cur) / 1000000;
1108 /* Called with the lock taken */
1110 gst_queue2_get_buffering_message (GstQueue2 * queue)
1112 GstMessage *msg = NULL;
1114 if (queue->percent_changed) {
1115 gint percent = queue->buffering_percent;
1117 queue->percent_changed = FALSE;
1119 GST_DEBUG_OBJECT (queue, "Going to post buffering: %d%%", percent);
1120 msg = gst_message_new_buffering (GST_OBJECT_CAST (queue), percent);
1122 gst_message_set_buffering_stats (msg, queue->mode, queue->avg_in,
1123 queue->avg_out, queue->buffering_left);
1130 gst_queue2_post_buffering (GstQueue2 * queue)
1132 GstMessage *msg = NULL;
1134 g_mutex_lock (&queue->buffering_post_lock);
1135 GST_QUEUE2_MUTEX_LOCK (queue);
1136 msg = gst_queue2_get_buffering_message (queue);
1137 GST_QUEUE2_MUTEX_UNLOCK (queue);
1140 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
1142 g_mutex_unlock (&queue->buffering_post_lock);
1146 update_buffering (GstQueue2 * queue)
1148 gint buffering_level, percent;
1149 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1150 GstQueue2Range *range;
1153 range = queue->read;
1155 range = queue->current;
1158 /* Ensure the variables used to calculate buffering state are up-to-date. */
1159 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1161 update_cur_level (queue, range);
1164 update_cur_level (queue, queue->current);
1166 update_in_rates (queue, FALSE);
1168 if (!get_buffering_level (queue, NULL, &buffering_level))
1171 percent = convert_to_buffering_percent (queue, buffering_level);
1173 if (queue->is_buffering) {
1174 /* if we were buffering see if we reached the high watermark */
1176 queue->is_buffering = FALSE;
1178 SET_PERCENT (queue, percent);
1180 /* we were not buffering, check if we need to start buffering if we drop
1181 * below the low threshold */
1182 if (buffering_level < queue->low_watermark) {
1183 queue->is_buffering = TRUE;
1184 SET_PERCENT (queue, percent);
1190 reset_rate_timer (GstQueue2 * queue)
1192 queue->bytes_in = 0;
1193 queue->bytes_out = 0;
1194 queue->byte_in_rate = 0.0;
1195 queue->byte_in_period = 0;
1196 queue->byte_out_rate = 0.0;
1197 queue->last_update_in_rates_elapsed = 0.0;
1198 queue->last_in_elapsed = 0.0;
1199 queue->last_out_elapsed = 0.0;
1200 queue->in_timer_started = FALSE;
1201 queue->out_timer_started = FALSE;
1204 /* the interval in seconds to recalculate the rate */
1205 #define RATE_INTERVAL 0.2
1206 /* Tuning for rate estimation. We use a large window for the input rate because
1207 * it should be stable when connected to a network. The output rate is less
1208 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
1209 * therefore adapt more quickly.
1210 * However, initial input rate may be subject to a burst, and should therefore
1211 * initially also adapt more quickly to changes, and only later on give higher
1212 * weight to previous values. */
1213 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
1214 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
1217 update_in_rates (GstQueue2 * queue, gboolean force)
1219 gdouble elapsed, period;
1220 gdouble byte_in_rate;
1222 if (!queue->in_timer_started) {
1223 queue->in_timer_started = TRUE;
1224 g_timer_start (queue->in_timer);
1228 queue->last_update_in_rates_elapsed = elapsed =
1229 g_timer_elapsed (queue->in_timer, NULL);
1231 /* recalc after each interval. */
1232 if (force || queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
1233 period = elapsed - queue->last_in_elapsed;
1235 GST_DEBUG_OBJECT (queue,
1236 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
1237 period, queue->bytes_in, queue->byte_in_period);
1239 byte_in_rate = queue->bytes_in / period;
1241 if (queue->byte_in_rate == 0.0)
1242 queue->byte_in_rate = byte_in_rate;
1244 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1245 (double) queue->byte_in_period, period);
1247 /* another data point, cap at 16 for long time running average */
1248 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1249 queue->byte_in_period += period;
1251 /* reset the values to calculate rate over the next interval */
1252 queue->last_in_elapsed = elapsed;
1253 queue->bytes_in = 0;
1256 if (queue->byte_in_rate > 0.0) {
1257 queue->cur_level.rate_time =
1258 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1260 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1261 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1265 update_out_rates (GstQueue2 * queue)
1267 gdouble elapsed, period;
1268 gdouble byte_out_rate;
1270 if (!queue->out_timer_started) {
1271 queue->out_timer_started = TRUE;
1272 g_timer_start (queue->out_timer);
1276 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1278 /* recalc after each interval. */
1279 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1280 period = elapsed - queue->last_out_elapsed;
1282 GST_DEBUG_OBJECT (queue,
1283 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1285 byte_out_rate = queue->bytes_out / period;
1287 if (queue->byte_out_rate == 0.0)
1288 queue->byte_out_rate = byte_out_rate;
1290 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1292 /* reset the values to calculate rate over the next interval */
1293 queue->last_out_elapsed = elapsed;
1294 queue->bytes_out = 0;
1296 if (queue->byte_in_rate > 0.0) {
1297 queue->cur_level.rate_time =
1298 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1300 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1301 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1305 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1307 guint64 reading_pos, max_reading_pos;
1310 max_reading_pos = range->max_reading_pos;
1312 max_reading_pos = MAX (max_reading_pos, reading_pos);
1314 GST_DEBUG_OBJECT (queue,
1315 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1316 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1317 range->max_reading_pos = max_reading_pos;
1319 update_cur_level (queue, range);
1323 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1328 /* until we receive the FLUSH_STOP from this seek, we skip data */
1329 queue->seeking = TRUE;
1330 GST_QUEUE2_MUTEX_UNLOCK (queue);
1332 debug_ranges (queue);
1334 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1337 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1338 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1339 GST_SEEK_TYPE_NONE, -1);
1341 res = gst_pad_push_event (queue->sinkpad, event);
1342 GST_QUEUE2_MUTEX_LOCK (queue);
1345 /* Between us sending the seek event and re-acquiring the lock, the source
1346 * thread might already have pushed data and moved along the range's
1347 * writing_pos beyond the seek offset. In that case we don't want to set
1348 * the writing position back to the requested seek position, as it would
1349 * cause data to be written to the wrong offset in the file or ring buffer.
1350 * We still do the add_range call to switch the current range to the
1351 * requested range, or create one if one doesn't exist yet. */
1352 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1353 queue->current = queue->read = add_range (queue, offset, FALSE);
1355 queue->current = add_range (queue, offset, FALSE);
1362 /* get the threshold for when we decide to seek rather than wait */
1364 get_seek_threshold (GstQueue2 * queue)
1368 /* FIXME, find a good threshold based on the incoming rate. */
1369 threshold = 1024 * 512;
1371 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1372 threshold = MIN (threshold,
1373 QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes);
1378 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1379 /* check the buffered data size, not to change range repeatedly.
1380 * changing range cause the new http connection and it makes buffering state frequently. */
1382 change_current_range(GstQueue2 * queue, GstQueue2Range *req_range, guint64 offset, guint length)
1384 guint64 curr_level = 0;
1385 guint64 curr_min_level = 0;
1387 if (queue->current->writing_pos > queue->current->reading_pos)
1388 curr_level = queue->current->writing_pos - queue->current->reading_pos;
1390 /* FIXME, find a good threshold based on the incoming rate and content bitrate. */
1391 curr_min_level = MIN((queue->max_level.bytes*0.05), get_seek_threshold(queue));
1393 GST_DEBUG_OBJECT(queue, " curr[w%"G_GUINT64_FORMAT", r%"G_GUINT64_FORMAT
1394 ", d%"G_GUINT64_FORMAT", m%"G_GUINT64_FORMAT
1395 "] u[%"G_GUINT64_FORMAT"][EOS:%d]",
1396 queue->current->writing_pos, queue->current->reading_pos,
1397 curr_level, curr_min_level,
1398 queue->upstream_size, queue->is_eos);
1400 /* FIXME, find a good threshold based on the incoming rate and content bitrate. */
1401 if ((queue->is_eos) ||
1402 (queue->upstream_size > 0 && queue->current->writing_pos >= queue->upstream_size) ||
1403 (curr_level >= curr_min_level)) {
1404 GST_DEBUG_OBJECT (queue, "Range Switching");
1407 GST_DEBUG_OBJECT (queue, "keep receiving data without range switching.");
1413 /* see if there is enough data in the file to read a full buffer */
1415 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1417 GstQueue2Range *range;
1418 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1419 gboolean need_to_seek = TRUE;
1421 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1424 if ((range = find_range (queue, offset))) {
1425 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1426 queue->read = range;
1428 if (queue->current != range) {
1429 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1430 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1431 if (QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER(queue))
1432 need_to_seek = change_current_range(queue, range, offset, length);
1434 if (need_to_seek == TRUE)
1435 perform_seek_to_offset (queue, range->writing_pos);
1437 perform_seek_to_offset (queue, range->writing_pos);
1441 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1442 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1444 /* we have a range for offset */
1445 GST_DEBUG_OBJECT (queue,
1446 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1447 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1449 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1452 if (offset + length <= range->writing_pos)
1455 GST_DEBUG_OBJECT (queue,
1456 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1457 (offset + length) - range->writing_pos);
1460 GST_INFO_OBJECT (queue, "not found in any range off %" G_GUINT64_FORMAT
1461 " len %u", offset, length);
1463 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1464 queue->read = queue->current;
1467 /* we don't have the range, see how far away we are */
1468 if (!queue->is_eos && queue->current) {
1469 guint64 threshold = get_seek_threshold (queue);
1471 if (offset >= queue->current->offset && offset <=
1472 queue->current->writing_pos + threshold) {
1473 GST_INFO_OBJECT (queue,
1474 "requested data is within range, wait for data");
1479 /* too far away, do a seek */
1480 perform_seek_to_offset (queue, offset);
1487 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1488 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1489 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1491 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1494 static GstFlowReturn
1495 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1496 guint8 * dst, gint64 * read_return)
1498 guint8 *ring_buffer;
1501 ring_buffer = queue->ring_buffer;
1503 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1506 /* this should not block */
1507 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1509 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1510 res = fread (dst, 1, length, queue->temp_file);
1512 memcpy (dst, ring_buffer + offset, length);
1516 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1518 if (G_UNLIKELY (res < length)) {
1519 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1520 goto could_not_read;
1521 /* check for errors or EOF */
1522 if (ferror (queue->temp_file))
1523 goto could_not_read;
1524 if (feof (queue->temp_file) && length > 0)
1534 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1535 return GST_FLOW_ERROR;
1539 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1540 return GST_FLOW_ERROR;
1544 GST_DEBUG ("non-regular file hits EOS");
1545 return GST_FLOW_EOS;
1549 static GstFlowReturn
1550 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1551 GstBuffer ** buffer)
1556 guint64 file_offset;
1557 guint block_length, remaining, read_length;
1561 GstFlowReturn ret = GST_FLOW_OK;
1563 /* allocate the output buffer of the requested size */
1564 if (*buffer == NULL)
1565 buf = gst_buffer_new_allocate (NULL, length, NULL);
1569 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
1570 goto buffer_write_fail;
1573 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1577 rb_size = queue->ring_buffer_max_size;
1578 max_size = QUEUE_MAX_BYTES (queue);
1581 while (remaining > 0) {
1582 /* configure how much/whether to read */
1583 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1586 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1589 /* calculate how far away the offset is */
1590 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1591 if (queue->read->writing_pos > rpos)
1592 level = queue->read->writing_pos - rpos;
1594 if (queue->current->writing_pos > rpos)
1595 level = queue->current->writing_pos - rpos;
1600 GST_DEBUG_OBJECT (queue,
1601 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1602 ", level %" G_GUINT64_FORMAT ", max %" G_GUINT64_FORMAT,
1603 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1604 rpos, queue->read->writing_pos, level, max_size);
1606 rpos, queue->current->writing_pos, level, max_size);
1609 if (level >= max_size) {
1610 /* we don't have the data but if we have a ring buffer that is full, we
1612 GST_DEBUG_OBJECT (queue,
1613 "ring buffer full, reading QUEUE_MAX_BYTES %"
1614 G_GUINT64_FORMAT " bytes", max_size);
1615 read_length = max_size;
1616 } else if (queue->is_eos) {
1617 /* won't get any more data so read any data we have */
1619 GST_DEBUG_OBJECT (queue,
1620 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1622 read_length = level;
1630 if (read_length == 0) {
1631 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1632 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1633 GST_DEBUG_OBJECT (queue,
1634 "update current position [%" G_GUINT64_FORMAT "-%"
1635 G_GUINT64_FORMAT "]", rpos, queue->read->max_reading_pos);
1636 update_cur_pos (queue, queue->read, rpos);
1638 GST_DEBUG_OBJECT (queue,
1639 "update current position [%" G_GUINT64_FORMAT "-%"
1640 G_GUINT64_FORMAT "]", rpos, queue->current->max_reading_pos);
1641 update_cur_pos (queue, queue->current, rpos);
1643 GST_QUEUE2_SIGNAL_DEL (queue);
1646 if (queue->use_buffering)
1647 update_buffering (queue);
1649 GST_DEBUG_OBJECT (queue, "waiting for add");
1650 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1654 /* we have the requested data so read it */
1655 read_length = remaining;
1658 /* set range reading_pos to actual reading position for this read */
1659 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1660 queue->read->reading_pos = rpos;
1662 queue->current->reading_pos = rpos;
1665 /* configure how much and from where to read */
1666 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1668 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1669 (queue->read->rb_offset + (rpos -
1670 queue->read->offset)) % rb_size;
1672 (queue->current->rb_offset + (rpos -
1673 queue->current->offset)) % rb_size;
1675 if (file_offset + read_length > rb_size) {
1676 block_length = rb_size - file_offset;
1678 block_length = read_length;
1682 block_length = read_length;
1685 /* while we still have data to read, we loop */
1686 while (read_length > 0) {
1690 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1691 data, &read_return);
1692 if (ret != GST_FLOW_OK)
1695 file_offset += read_return;
1696 if (QUEUE_IS_USING_RING_BUFFER (queue))
1697 file_offset %= rb_size;
1699 data += read_return;
1700 read_length -= read_return;
1701 block_length = read_length;
1702 remaining -= read_return;
1704 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1705 rpos = (queue->read->reading_pos += read_return);
1706 update_cur_pos (queue, queue->read, queue->read->reading_pos);
1708 rpos = (queue->current->reading_pos += read_return);
1709 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1712 GST_QUEUE2_SIGNAL_DEL (queue);
1713 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1716 gst_buffer_unmap (buf, &info);
1717 gst_buffer_resize (buf, 0, length);
1719 GST_BUFFER_OFFSET (buf) = offset;
1720 GST_BUFFER_OFFSET_END (buf) = offset + length;
1729 GST_DEBUG_OBJECT (queue, "EOS hit and we don't have any requested data");
1730 gst_buffer_unmap (buf, &info);
1731 if (*buffer == NULL)
1732 gst_buffer_unref (buf);
1733 return GST_FLOW_EOS;
1737 GST_DEBUG_OBJECT (queue, "we are flushing");
1738 gst_buffer_unmap (buf, &info);
1739 if (*buffer == NULL)
1740 gst_buffer_unref (buf);
1741 return GST_FLOW_FLUSHING;
1745 GST_DEBUG_OBJECT (queue, "we have a read error");
1746 gst_buffer_unmap (buf, &info);
1747 if (*buffer == NULL)
1748 gst_buffer_unref (buf);
1753 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE, (NULL),
1754 ("Can't write to buffer"));
1755 if (*buffer == NULL)
1756 gst_buffer_unref (buf);
1757 return GST_FLOW_ERROR;
1761 /* should be called with QUEUE_LOCK */
1762 static GstMiniObject *
1763 gst_queue2_read_item_from_file (GstQueue2 * queue)
1765 GstMiniObject *item;
1767 if (queue->stream_start_event != NULL) {
1768 item = GST_MINI_OBJECT_CAST (queue->stream_start_event);
1769 queue->stream_start_event = NULL;
1770 } else if (queue->starting_segment != NULL) {
1771 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1772 queue->starting_segment = NULL;
1775 GstBuffer *buffer = NULL;
1776 guint64 reading_pos;
1778 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
1779 reading_pos = queue->read->reading_pos;
1781 reading_pos = queue->current->reading_pos;
1785 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1790 item = GST_MINI_OBJECT_CAST (buffer);
1793 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1803 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1804 * the temp filename. */
1806 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1811 if (queue->temp_file)
1812 goto already_opened;
1814 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1816 /* If temp_template was set, allocate a filename and open that file */
1819 if (queue->temp_template == NULL)
1822 /* make copy of the template, we don't want to change this */
1823 name = g_strdup (queue->temp_template);
1826 fd = g_mkstemp_full (name, O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);
1828 fd = g_mkstemp (name);
1832 goto mkstemp_failed;
1834 /* open the file for update/writing */
1835 queue->temp_file = fdopen (fd, "wb+");
1836 /* error creating file */
1837 if (queue->temp_file == NULL)
1840 g_free (queue->temp_location);
1841 queue->temp_location = name;
1843 GST_QUEUE2_MUTEX_UNLOCK (queue);
1845 /* we can't emit the notify with the lock */
1846 g_object_notify (G_OBJECT (queue), "temp-location");
1848 GST_QUEUE2_MUTEX_LOCK (queue);
1850 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1857 GST_DEBUG_OBJECT (queue, "temp file was already open");
1862 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1863 (_("No Temp directory specified.")), (NULL));
1868 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1869 (_("Could not create temp file \"%s\"."), queue->temp_template),
1876 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1877 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1886 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1889 if (queue->temp_file == NULL)
1892 GST_DEBUG_OBJECT (queue, "closing temp file");
1894 fflush (queue->temp_file);
1895 fclose (queue->temp_file);
1897 if (queue->temp_remove) {
1898 if (remove (queue->temp_location) < 0) {
1899 GST_WARNING_OBJECT (queue, "Failed to remove temporary file %s: %s",
1900 queue->temp_location, g_strerror (errno));
1904 queue->temp_file = NULL;
1905 clean_ranges (queue);
1909 gst_queue2_flush_temp_file (GstQueue2 * queue)
1911 if (queue->temp_file == NULL)
1914 GST_DEBUG_OBJECT (queue, "flushing temp file");
1916 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1920 gst_queue2_locked_flush (GstQueue2 * queue, gboolean full, gboolean clear_temp)
1922 if (!QUEUE_IS_USING_QUEUE (queue)) {
1923 if (QUEUE_IS_USING_TEMP_FILE (queue) && clear_temp)
1924 gst_queue2_flush_temp_file (queue);
1925 init_ranges (queue);
1927 while (!g_queue_is_empty (&queue->queue)) {
1928 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
1930 if (!full && qitem->type == GST_QUEUE2_ITEM_TYPE_EVENT
1931 && GST_EVENT_IS_STICKY (qitem->item)
1932 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_SEGMENT
1933 && GST_EVENT_TYPE (qitem->item) != GST_EVENT_EOS) {
1934 gst_pad_store_sticky_event (queue->srcpad,
1935 GST_EVENT_CAST (qitem->item));
1938 /* Then lose another reference because we are supposed to destroy that
1939 data when flushing */
1940 if (qitem->type != GST_QUEUE2_ITEM_TYPE_QUERY)
1941 gst_mini_object_unref (qitem->item);
1942 g_slice_free (GstQueue2Item, qitem);
1945 queue->last_query = FALSE;
1946 g_cond_signal (&queue->query_handled);
1947 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1948 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1949 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1950 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1951 queue->sink_tainted = queue->src_tainted = TRUE;
1952 if (queue->starting_segment != NULL)
1953 gst_event_unref (queue->starting_segment);
1954 queue->starting_segment = NULL;
1955 queue->segment_event_received = FALSE;
1956 gst_event_replace (&queue->stream_start_event, NULL);
1958 /* we deleted a lot of something */
1959 GST_QUEUE2_SIGNAL_DEL (queue);
1963 gst_queue2_wait_free_space (GstQueue2 * queue)
1965 /* We make space available if we're "full" according to whatever
1966 * the user defined as "full". */
1967 if (gst_queue2_is_filled (queue)) {
1970 /* pause the timer while we wait. The fact that we are waiting does not mean
1971 * the byterate on the input pad is lower */
1972 if ((started = queue->in_timer_started))
1973 g_timer_stop (queue->in_timer);
1975 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1976 "queue is full, waiting for free space");
1978 /* Wait for space to be available, we could be unlocked because of a flush. */
1979 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1981 while (gst_queue2_is_filled (queue));
1983 /* and continue if we were running before */
1985 g_timer_continue (queue->in_timer);
1992 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1998 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
2001 guint8 *data, *ring_buffer;
2002 guint size, rb_size;
2003 guint64 writing_pos, new_writing_pos;
2004 GstQueue2Range *range, *prev, *next;
2005 gboolean do_seek = FALSE;
2007 if (QUEUE_IS_USING_RING_BUFFER (queue))
2008 writing_pos = queue->current->rb_writing_pos;
2010 writing_pos = queue->current->writing_pos;
2011 ring_buffer = queue->ring_buffer;
2012 rb_size = queue->ring_buffer_max_size;
2014 if (!gst_buffer_map (buffer, &info, GST_MAP_READ))
2015 goto buffer_read_error;
2020 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
2024 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
2025 GST_BUFFER_OFFSET (buffer) != queue->current->writing_pos) {
2026 GST_WARNING_OBJECT (queue, "buffer offset does not match current writing "
2027 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
2028 GST_BUFFER_OFFSET (buffer), queue->current->writing_pos);
2034 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2037 /* calculate the space in the ring buffer not used by data from
2038 * the current range */
2039 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
2040 /* wait until there is some free space */
2041 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
2043 /* get the amount of space we have */
2044 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
2046 /* calculate if we need to split or if we can write the entire
2048 to_write = MIN (size, space);
2050 /* the writing position in the ring buffer after writing (part
2051 * or all of) the buffer */
2052 new_writing_pos = (writing_pos + to_write) % rb_size;
2055 range = queue->ranges;
2057 /* if we need to overwrite data in the ring buffer, we need to
2060 * warning: this code is complicated and includes some
2061 * simplifications - pen, paper and diagrams for the cases
2064 guint64 range_data_start, range_data_end;
2065 GstQueue2Range *range_to_destroy = NULL;
2067 if (range == queue->current)
2070 range_data_start = range->rb_offset;
2071 range_data_end = range->rb_writing_pos;
2073 /* handle the special case where the range has no data in it */
2074 if (range->writing_pos == range->offset) {
2075 if (range != queue->current) {
2076 GST_DEBUG_OBJECT (queue,
2077 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
2078 G_GUINT64_FORMAT, range->offset, range->writing_pos);
2080 range_to_destroy = range;
2082 prev->next = range->next;
2087 if (range_data_end > range_data_start) {
2088 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
2091 if (new_writing_pos > range_data_start) {
2092 if (new_writing_pos >= range_data_end) {
2093 GST_DEBUG_OBJECT (queue,
2094 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
2095 G_GUINT64_FORMAT, range->offset, range->writing_pos);
2097 range_to_destroy = range;
2099 prev->next = range->next;
2101 GST_DEBUG_OBJECT (queue,
2102 "advancing offsets from %" G_GUINT64_FORMAT " (%"
2103 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
2104 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
2105 range->offset + new_writing_pos - range_data_start,
2107 range->offset += (new_writing_pos - range_data_start);
2108 range->rb_offset = new_writing_pos;
2112 guint64 new_wpos_virt = writing_pos + to_write;
2114 if (new_wpos_virt <= range_data_start)
2117 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
2118 GST_DEBUG_OBJECT (queue,
2119 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
2120 G_GUINT64_FORMAT, range->offset, range->writing_pos);
2122 range_to_destroy = range;
2124 prev->next = range->next;
2126 GST_DEBUG_OBJECT (queue,
2127 "advancing offsets from %" G_GUINT64_FORMAT " (%"
2128 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
2129 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
2130 range->offset + new_writing_pos - range_data_start,
2132 range->offset += (new_wpos_virt - range_data_start);
2133 range->rb_offset = new_writing_pos;
2138 if (!range_to_destroy)
2141 range = range->next;
2142 if (range_to_destroy) {
2143 if (range_to_destroy == queue->ranges)
2144 queue->ranges = range;
2145 g_slice_free (GstQueue2Range, range_to_destroy);
2146 range_to_destroy = NULL;
2151 new_writing_pos = writing_pos + to_write;
2154 if (QUEUE_IS_USING_TEMP_FILE (queue)
2155 && FSEEK_FILE (queue->temp_file, writing_pos))
2158 if (new_writing_pos > writing_pos) {
2159 GST_INFO_OBJECT (queue,
2160 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
2161 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
2162 queue->current->writing_pos, queue->current->rb_writing_pos);
2163 /* either not using ring buffer or no wrapping, just write */
2164 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2165 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
2168 memcpy (ring_buffer + writing_pos, data, to_write);
2171 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
2172 /* try to merge with next range */
2173 while ((next = queue->current->next)) {
2174 GST_INFO_OBJECT (queue,
2175 "checking merge with next range %" G_GUINT64_FORMAT " < %"
2176 G_GUINT64_FORMAT, new_writing_pos, next->offset);
2177 if (new_writing_pos < next->offset)
2180 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
2183 /* remove the group */
2184 queue->current->next = next->next;
2186 /* We use the threshold to decide if we want to do a seek or simply
2187 * read the data again. If there is not so much data in the range we
2188 * prefer to avoid to seek and read it again. */
2189 if (next->writing_pos > new_writing_pos + get_seek_threshold (queue)) {
2190 /* the new range had more data than the threshold, it's worth keeping
2191 * it and doing a seek. */
2192 new_writing_pos = next->writing_pos;
2195 g_slice_free (GstQueue2Range, next);
2197 goto update_and_signal;
2201 guint block_one, block_two;
2203 block_one = rb_size - writing_pos;
2204 block_two = to_write - block_one;
2206 if (block_one > 0) {
2207 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
2208 /* write data to end of ring buffer */
2209 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2210 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
2213 memcpy (ring_buffer + writing_pos, data, block_one);
2217 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
2220 if (block_two > 0) {
2221 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
2222 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
2223 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
2226 memcpy (ring_buffer, data + block_one, block_two);
2232 /* update the writing positions */
2234 GST_INFO_OBJECT (queue,
2235 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
2236 to_write, writing_pos, size);
2238 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2240 queue->current->writing_pos += to_write;
2241 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
2243 queue->current->writing_pos = writing_pos = new_writing_pos;
2244 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
2246 if (queue->upstream_size == 0 || new_writing_pos < queue->upstream_size)
2247 perform_seek_to_offset (queue, new_writing_pos);
2249 queue->is_eos = TRUE;
2254 #ifndef TIZEN_FEATURE_QUEUE2_MODIFICATION
2256 perform_seek_to_offset (queue, new_writing_pos);
2259 update_cur_level (queue, queue->current);
2261 /* update the buffering status */
2262 if (queue->use_buffering) {
2264 update_buffering (queue);
2265 msg = gst_queue2_get_buffering_message (queue);
2267 GST_QUEUE2_MUTEX_UNLOCK (queue);
2268 g_mutex_lock (&queue->buffering_post_lock);
2269 gst_element_post_message (GST_ELEMENT_CAST (queue), msg);
2270 g_mutex_unlock (&queue->buffering_post_lock);
2271 GST_QUEUE2_MUTEX_LOCK (queue);
2275 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
2276 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
2278 GST_QUEUE2_SIGNAL_ADD (queue);
2281 gst_buffer_unmap (buffer, &info);
2288 GST_DEBUG_OBJECT (queue, "we are flushing");
2289 gst_buffer_unmap (buffer, &info);
2290 /* FIXME - GST_FLOW_EOS ? */
2295 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
2296 gst_buffer_unmap (buffer, &info);
2303 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
2307 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
2308 (_("Error while writing to download file.")),
2309 ("%s", g_strerror (errno)));
2312 gst_buffer_unmap (buffer, &info);
2317 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL),
2318 ("Can't read from buffer"));
2324 buffer_list_create_write (GstBuffer ** buf, guint idx, gpointer q)
2326 GstQueue2 *queue = q;
2328 GST_TRACE_OBJECT (queue,
2329 "writing buffer %u of size %" G_GSIZE_FORMAT " bytes", idx,
2330 gst_buffer_get_size (*buf));
2332 if (!gst_queue2_create_write (queue, *buf)) {
2333 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
2340 buffer_list_calc_size (GstBuffer ** buf, guint idx, gpointer data)
2342 guint *p_size = data;
2345 buf_size = gst_buffer_get_size (*buf);
2346 GST_TRACE ("buffer %u in has size %" G_GSIZE_FORMAT, idx, buf_size);
2347 *p_size += buf_size;
2351 /* enqueue an item an update the level stats */
2353 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
2354 GstQueue2ItemType item_type)
2356 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2360 buffer = GST_BUFFER_CAST (item);
2361 size = gst_buffer_get_size (buffer);
2363 /* add buffer to the statistics */
2364 if (QUEUE_IS_USING_QUEUE (queue)) {
2365 queue->cur_level.buffers++;
2366 queue->cur_level.bytes += size;
2368 queue->bytes_in += size;
2370 /* apply new buffer to segment stats */
2371 apply_buffer (queue, buffer, &queue->sink_segment, size, TRUE);
2372 /* update the byterate stats */
2373 update_in_rates (queue, FALSE);
2375 if (!QUEUE_IS_USING_QUEUE (queue)) {
2376 /* FIXME - check return value? */
2377 gst_queue2_create_write (queue, buffer);
2379 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2380 GstBufferList *buffer_list;
2383 buffer_list = GST_BUFFER_LIST_CAST (item);
2385 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2386 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
2388 /* add buffer to the statistics */
2389 if (QUEUE_IS_USING_QUEUE (queue)) {
2390 queue->cur_level.buffers += gst_buffer_list_length (buffer_list);
2391 queue->cur_level.bytes += size;
2393 queue->bytes_in += size;
2395 /* apply new buffer to segment stats */
2396 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
2398 /* update the byterate stats */
2399 update_in_rates (queue, FALSE);
2401 if (!QUEUE_IS_USING_QUEUE (queue)) {
2402 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
2404 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2407 event = GST_EVENT_CAST (item);
2409 switch (GST_EVENT_TYPE (event)) {
2411 /* Zero the thresholds, this makes sure the queue is completely
2412 * filled and we can read all data from the queue. */
2413 GST_DEBUG_OBJECT (queue, "we have EOS");
2414 queue->is_eos = TRUE;
2415 /* Force updating the input bitrate */
2416 update_in_rates (queue, TRUE);
2418 case GST_EVENT_SEGMENT:
2419 apply_segment (queue, event, &queue->sink_segment, TRUE);
2420 /* This is our first new segment, we hold it
2421 * as we can't save it on the temp file */
2422 if (!QUEUE_IS_USING_QUEUE (queue)) {
2423 if (queue->segment_event_received)
2424 goto unexpected_event;
2426 queue->segment_event_received = TRUE;
2427 if (queue->starting_segment != NULL)
2428 gst_event_unref (queue->starting_segment);
2429 queue->starting_segment = event;
2432 /* a new segment allows us to accept more buffers if we got EOS
2433 * from downstream */
2434 queue->unexpected = FALSE;
2437 apply_gap (queue, event, &queue->sink_segment, TRUE);
2439 case GST_EVENT_STREAM_START:
2440 if (!QUEUE_IS_USING_QUEUE (queue)) {
2441 gst_event_replace (&queue->stream_start_event, event);
2442 gst_event_unref (event);
2446 case GST_EVENT_CAPS:{
2449 gst_event_parse_caps (event, &caps);
2450 GST_INFO ("got caps: %" GST_PTR_FORMAT, caps);
2452 if (!QUEUE_IS_USING_QUEUE (queue)) {
2453 GST_LOG ("Dropping caps event, not using queue");
2454 gst_event_unref (event);
2460 if (!QUEUE_IS_USING_QUEUE (queue))
2461 goto unexpected_event;
2464 } else if (GST_IS_QUERY (item)) {
2465 /* Can't happen as we check that in the caller */
2466 if (!QUEUE_IS_USING_QUEUE (queue))
2467 g_assert_not_reached ();
2469 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
2470 item, GST_OBJECT_NAME (queue));
2471 /* we can't really unref since we don't know what it is */
2476 /* update the buffering status */
2477 if (queue->use_buffering)
2478 update_buffering (queue);
2480 if (QUEUE_IS_USING_QUEUE (queue)) {
2481 GstQueue2Item *qitem = g_slice_new (GstQueue2Item);
2482 qitem->type = item_type;
2484 g_queue_push_tail (&queue->queue, qitem);
2486 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2489 GST_QUEUE2_SIGNAL_ADD (queue);
2497 gboolean is_custom = GST_EVENT_TYPE (item) < GST_EVENT_CUSTOM_UPSTREAM;
2499 GST_WARNING_OBJECT (queue, "%s%s event can't be added to temp file: "
2500 "%" GST_PTR_FORMAT, is_custom ? "Unexpected " : "",
2501 GST_EVENT_TYPE_NAME (item), GST_EVENT_CAST (item));
2502 gst_event_unref (GST_EVENT_CAST (item));
2507 /* dequeue an item from the queue and update level stats */
2508 static GstMiniObject *
2509 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2511 GstMiniObject *item;
2513 if (!QUEUE_IS_USING_QUEUE (queue)) {
2514 item = gst_queue2_read_item_from_file (queue);
2516 GstQueue2Item *qitem = g_queue_pop_head (&queue->queue);
2522 g_slice_free (GstQueue2Item, qitem);
2528 if (GST_IS_BUFFER (item)) {
2532 buffer = GST_BUFFER_CAST (item);
2533 size = gst_buffer_get_size (buffer);
2534 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2536 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2537 "retrieved buffer %p from queue", buffer);
2539 if (QUEUE_IS_USING_QUEUE (queue)) {
2540 queue->cur_level.buffers--;
2541 queue->cur_level.bytes -= size;
2543 queue->bytes_out += size;
2545 apply_buffer (queue, buffer, &queue->src_segment, size, FALSE);
2546 /* update the byterate stats */
2547 update_out_rates (queue);
2548 /* update the buffering */
2549 if (queue->use_buffering)
2550 update_buffering (queue);
2552 } else if (GST_IS_EVENT (item)) {
2553 GstEvent *event = GST_EVENT_CAST (item);
2555 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2557 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2558 "retrieved event %p from queue", event);
2560 switch (GST_EVENT_TYPE (event)) {
2562 /* queue is empty now that we dequeued the EOS */
2563 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2565 case GST_EVENT_SEGMENT:
2566 apply_segment (queue, event, &queue->src_segment, FALSE);
2569 apply_gap (queue, event, &queue->src_segment, FALSE);
2574 } else if (GST_IS_BUFFER_LIST (item)) {
2575 GstBufferList *buffer_list;
2578 buffer_list = GST_BUFFER_LIST_CAST (item);
2579 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2580 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2582 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2583 "retrieved buffer list %p from queue", buffer_list);
2585 if (QUEUE_IS_USING_QUEUE (queue)) {
2586 queue->cur_level.buffers -= gst_buffer_list_length (buffer_list);
2587 queue->cur_level.bytes -= size;
2589 queue->bytes_out += size;
2591 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2592 /* update the byterate stats */
2593 update_out_rates (queue);
2594 /* update the buffering */
2595 if (queue->use_buffering)
2596 update_buffering (queue);
2597 } else if (GST_IS_QUERY (item)) {
2598 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2599 "retrieved query %p from queue", item);
2600 *item_type = GST_QUEUE2_ITEM_TYPE_QUERY;
2603 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2604 item, GST_OBJECT_NAME (queue));
2606 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2608 GST_QUEUE2_SIGNAL_DEL (queue);
2615 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2620 static GstFlowReturn
2621 gst_queue2_handle_sink_event (GstPad * pad, GstObject * parent,
2624 gboolean ret = TRUE;
2627 queue = GST_QUEUE2 (parent);
2629 switch (GST_EVENT_TYPE (event)) {
2630 case GST_EVENT_FLUSH_START:
2632 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2633 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2635 ret = gst_pad_push_event (queue->srcpad, event);
2637 /* now unblock the chain function */
2638 GST_QUEUE2_MUTEX_LOCK (queue);
2639 queue->srcresult = GST_FLOW_FLUSHING;
2640 queue->sinkresult = GST_FLOW_FLUSHING;
2641 /* unblock the loop and chain functions */
2642 GST_QUEUE2_SIGNAL_ADD (queue);
2643 GST_QUEUE2_SIGNAL_DEL (queue);
2644 GST_QUEUE2_MUTEX_UNLOCK (queue);
2646 /* make sure it pauses, this should happen since we sent
2647 * flush_start downstream. */
2648 gst_pad_pause_task (queue->srcpad);
2649 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2651 GST_QUEUE2_MUTEX_LOCK (queue);
2652 queue->last_query = FALSE;
2653 g_cond_signal (&queue->query_handled);
2654 GST_QUEUE2_MUTEX_UNLOCK (queue);
2656 GST_QUEUE2_MUTEX_LOCK (queue);
2657 /* flush the sink pad */
2658 queue->sinkresult = GST_FLOW_FLUSHING;
2659 GST_QUEUE2_SIGNAL_DEL (queue);
2660 queue->last_query = FALSE;
2661 g_cond_signal (&queue->query_handled);
2662 GST_QUEUE2_MUTEX_UNLOCK (queue);
2664 gst_event_unref (event);
2668 case GST_EVENT_FLUSH_STOP:
2670 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2672 if (GST_PAD_MODE (queue->srcpad) == GST_PAD_MODE_PUSH) {
2674 ret = gst_pad_push_event (queue->srcpad, event);
2676 GST_QUEUE2_MUTEX_LOCK (queue);
2677 gst_queue2_locked_flush (queue, FALSE, TRUE);
2678 queue->srcresult = GST_FLOW_OK;
2679 queue->sinkresult = GST_FLOW_OK;
2680 queue->is_eos = FALSE;
2681 queue->unexpected = FALSE;
2682 queue->seeking = FALSE;
2683 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2684 /* reset rate counters */
2685 reset_rate_timer (queue);
2686 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2687 queue->srcpad, NULL);
2688 GST_QUEUE2_MUTEX_UNLOCK (queue);
2690 GST_QUEUE2_MUTEX_LOCK (queue);
2691 queue->segment_event_received = FALSE;
2692 queue->is_eos = FALSE;
2693 queue->unexpected = FALSE;
2694 queue->sinkresult = GST_FLOW_OK;
2695 queue->seeking = FALSE;
2696 queue->src_tags_bitrate = queue->sink_tags_bitrate = 0;
2697 GST_QUEUE2_MUTEX_UNLOCK (queue);
2699 gst_event_unref (event);
2703 case GST_EVENT_TAG:{
2704 if (queue->use_tags_bitrate) {
2708 gst_event_parse_tag (event, &tags);
2709 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
2710 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
2711 GST_QUEUE2_MUTEX_LOCK (queue);
2712 queue->sink_tags_bitrate = bitrate;
2713 GST_QUEUE2_MUTEX_UNLOCK (queue);
2714 GST_LOG_OBJECT (queue, "Sink pad bitrate from tags now %u", bitrate);
2720 if (GST_EVENT_IS_SERIALIZED (event)) {
2721 /* serialized events go in the queue */
2722 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2723 if (queue->srcresult != GST_FLOW_OK) {
2724 /* Errors in sticky event pushing are no problem and ignored here
2725 * as they will cause more meaningful errors during data flow.
2726 * For EOS events, that are not followed by data flow, we still
2727 * return FALSE here though and report an error.
2729 if (!GST_EVENT_IS_STICKY (event)) {
2730 goto out_flow_error;
2731 } else if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
2732 if (queue->srcresult == GST_FLOW_NOT_LINKED
2733 || queue->srcresult < GST_FLOW_EOS) {
2734 GST_ELEMENT_FLOW_ERROR (queue, queue->srcresult);
2736 goto out_flow_error;
2739 /* refuse more events on EOS */
2742 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2743 GST_QUEUE2_MUTEX_UNLOCK (queue);
2744 gst_queue2_post_buffering (queue);
2746 /* non-serialized events are passed downstream. */
2747 ret = gst_pad_push_event (queue->srcpad, event);
2752 return GST_FLOW_ERROR;
2758 GstFlowReturn ret = queue->sinkresult;
2759 GST_DEBUG_OBJECT (queue, "refusing event, we are %s",
2760 gst_flow_get_name (ret));
2761 GST_QUEUE2_MUTEX_UNLOCK (queue);
2762 gst_event_unref (event);
2767 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2768 GST_QUEUE2_MUTEX_UNLOCK (queue);
2769 gst_event_unref (event);
2770 return GST_FLOW_EOS;
2774 GST_LOG_OBJECT (queue,
2775 "refusing event, we have a downstream flow error: %s",
2776 gst_flow_get_name (queue->srcresult));
2777 GST_QUEUE2_MUTEX_UNLOCK (queue);
2778 gst_event_unref (event);
2779 return queue->srcresult;
2784 gst_queue2_handle_sink_query (GstPad * pad, GstObject * parent,
2790 queue = GST_QUEUE2 (parent);
2792 switch (GST_QUERY_TYPE (query)) {
2794 if (GST_QUERY_IS_SERIALIZED (query)) {
2795 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received query %p", query);
2796 /* serialized events go in the queue. We need to be certain that we
2797 * don't cause deadlocks waiting for the query return value. We check if
2798 * the queue is empty (nothing is blocking downstream and the query can
2799 * be pushed for sure) or we are not buffering. If we are buffering,
2800 * the pipeline waits to unblock downstream until our queue fills up
2801 * completely, which can not happen if we block on the query..
2802 * Therefore we only potentially block when we are not buffering. */
2803 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2804 if (QUEUE_IS_USING_QUEUE (queue) && (gst_queue2_is_empty (queue)
2805 || !queue->use_buffering)) {
2806 if (!g_atomic_int_get (&queue->downstream_may_block)) {
2807 gst_queue2_locked_enqueue (queue, query,
2808 GST_QUEUE2_ITEM_TYPE_QUERY);
2810 STATUS (queue, queue->sinkpad, "wait for QUERY");
2811 while (queue->sinkresult == GST_FLOW_OK &&
2812 queue->last_handled_query != query)
2813 g_cond_wait (&queue->query_handled, &queue->qlock);
2814 queue->last_handled_query = NULL;
2815 if (queue->sinkresult != GST_FLOW_OK)
2817 res = queue->last_query;
2819 GST_DEBUG_OBJECT (queue, "refusing query, downstream might block");
2823 GST_DEBUG_OBJECT (queue,
2824 "refusing query, we are not using the queue");
2827 GST_QUEUE2_MUTEX_UNLOCK (queue);
2828 gst_queue2_post_buffering (queue);
2830 res = gst_pad_query_default (pad, parent, query);
2839 GST_DEBUG_OBJECT (queue, "refusing query, we are %s",
2840 gst_flow_get_name (queue->sinkresult));
2841 GST_QUEUE2_MUTEX_UNLOCK (queue);
2847 gst_queue2_is_empty (GstQueue2 * queue)
2849 /* never empty on EOS */
2853 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2854 return queue->current->writing_pos <= queue->current->max_reading_pos;
2856 if (queue->queue.length == 0)
2864 gst_queue2_is_filled (GstQueue2 * queue)
2868 /* always filled on EOS */
2872 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2873 (queue->cur_level.format) >= ((alt_max) ? \
2874 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2876 /* if using a ring buffer we're filled if all ring buffer space is used
2877 * _by the current range_ */
2878 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2879 guint64 rb_size = queue->ring_buffer_max_size;
2880 GST_DEBUG_OBJECT (queue,
2881 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2882 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2883 return CHECK_FILLED (bytes, rb_size);
2886 /* if using file, we're never filled if we don't have EOS */
2887 if (QUEUE_IS_USING_TEMP_FILE (queue))
2890 /* we are never filled when we have no buffers at all */
2891 if (queue->cur_level.buffers == 0)
2894 /* we are filled if one of the current levels exceeds the max */
2895 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2896 || CHECK_FILLED (time, 0);
2898 /* if we need to, use the rate estimate to check against the max time we are
2899 * allowed to queue */
2900 if (queue->use_rate_estimate)
2901 res |= CHECK_FILLED (rate_time, 0);
2907 static GstFlowReturn
2908 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2909 GstMiniObject * item, GstQueue2ItemType item_type)
2911 /* we have to lock the queue since we span threads */
2912 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2913 /* when we received EOS, we refuse more data */
2916 /* when we received unexpected from downstream, refuse more buffers */
2917 if (queue->unexpected)
2918 goto out_unexpected;
2920 /* while we didn't receive the newsegment, we're seeking and we skip data */
2924 if (!gst_queue2_wait_free_space (queue))
2927 /* put buffer in queue now */
2928 gst_queue2_locked_enqueue (queue, item, item_type);
2929 GST_QUEUE2_MUTEX_UNLOCK (queue);
2930 gst_queue2_post_buffering (queue);
2934 /* special conditions */
2937 GstFlowReturn ret = queue->sinkresult;
2939 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2940 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2941 GST_QUEUE2_MUTEX_UNLOCK (queue);
2942 gst_mini_object_unref (item);
2948 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2949 GST_QUEUE2_MUTEX_UNLOCK (queue);
2950 gst_mini_object_unref (item);
2952 return GST_FLOW_EOS;
2956 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are seeking");
2957 GST_QUEUE2_MUTEX_UNLOCK (queue);
2958 gst_mini_object_unref (item);
2964 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2965 GST_QUEUE2_MUTEX_UNLOCK (queue);
2966 gst_mini_object_unref (item);
2968 return GST_FLOW_EOS;
2972 static GstFlowReturn
2973 gst_queue2_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2977 queue = GST_QUEUE2 (parent);
2979 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received buffer %p of "
2980 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
2981 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
2982 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2983 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2985 return gst_queue2_chain_buffer_or_buffer_list (queue,
2986 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2989 static GstFlowReturn
2990 gst_queue2_chain_list (GstPad * pad, GstObject * parent,
2991 GstBufferList * buffer_list)
2995 queue = GST_QUEUE2 (parent);
2997 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2998 "received buffer list %p", buffer_list);
3000 return gst_queue2_chain_buffer_or_buffer_list (queue,
3001 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
3004 static GstMiniObject *
3005 gst_queue2_dequeue_on_eos (GstQueue2 * queue, GstQueue2ItemType * item_type)
3007 GstMiniObject *data;
3009 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got EOS from downstream");
3011 /* stop pushing buffers, we dequeue all items until we see an item that we
3012 * can push again, which is EOS or SEGMENT. If there is nothing in the
3013 * queue we can push, we set a flag to make the sinkpad refuse more
3014 * buffers with an EOS return value until we receive something
3015 * pushable again or we get flushed. */
3016 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
3017 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
3018 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3019 "dropping EOS buffer %p", data);
3020 gst_buffer_unref (GST_BUFFER_CAST (data));
3021 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
3022 GstEvent *event = GST_EVENT_CAST (data);
3023 GstEventType type = GST_EVENT_TYPE (event);
3025 if (type == GST_EVENT_EOS || type == GST_EVENT_SEGMENT) {
3026 /* we found a pushable item in the queue, push it out */
3027 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3028 "pushing pushable event %s after EOS", GST_EVENT_TYPE_NAME (event));
3031 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3032 "dropping EOS event %p", event);
3033 gst_event_unref (event);
3034 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
3035 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3036 "dropping EOS buffer list %p", data);
3037 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
3038 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
3039 queue->last_query = FALSE;
3040 g_cond_signal (&queue->query_handled);
3041 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "dropping EOS query %p", data);
3044 /* no more items in the queue. Set the unexpected flag so that upstream
3045 * make us refuse any more buffers on the sinkpad. Since we will still
3046 * accept EOS and SEGMENT we return _FLOW_OK to the caller so that the
3047 * task function does not shut down. */
3048 queue->unexpected = TRUE;
3052 /* dequeue an item from the queue an push it downstream. This functions returns
3053 * the result of the push. */
3054 static GstFlowReturn
3055 gst_queue2_push_one (GstQueue2 * queue)
3057 GstFlowReturn result = queue->srcresult;
3058 GstMiniObject *data;
3059 GstQueue2ItemType item_type;
3061 data = gst_queue2_locked_dequeue (queue, &item_type);
3066 STATUS (queue, queue->srcpad, "We have something dequeud");
3067 g_atomic_int_set (&queue->downstream_may_block,
3068 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER ||
3069 item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
3070 GST_QUEUE2_MUTEX_UNLOCK (queue);
3071 gst_queue2_post_buffering (queue);
3073 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
3076 buffer = GST_BUFFER_CAST (data);
3078 result = gst_pad_push (queue->srcpad, buffer);
3079 g_atomic_int_set (&queue->downstream_may_block, 0);
3081 /* need to check for srcresult here as well */
3082 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3083 if (result == GST_FLOW_EOS) {
3084 data = gst_queue2_dequeue_on_eos (queue, &item_type);
3087 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
3088 * to the caller so that the task function does not shut down */
3089 result = GST_FLOW_OK;
3091 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
3092 GstEvent *event = GST_EVENT_CAST (data);
3093 GstEventType type = GST_EVENT_TYPE (event);
3095 if (type == GST_EVENT_TAG) {
3096 if (queue->use_tags_bitrate) {
3100 gst_event_parse_tag (event, &tags);
3101 if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &bitrate) ||
3102 gst_tag_list_get_uint (tags, GST_TAG_NOMINAL_BITRATE, &bitrate)) {
3103 GST_QUEUE2_MUTEX_LOCK (queue);
3104 queue->src_tags_bitrate = bitrate;
3105 GST_QUEUE2_MUTEX_UNLOCK (queue);
3106 GST_LOG_OBJECT (queue, "src pad bitrate from tags now %u", bitrate);
3111 gst_pad_push_event (queue->srcpad, event);
3113 /* if we're EOS, return EOS so that the task pauses. */
3114 if (type == GST_EVENT_EOS) {
3115 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3116 "pushed EOS event %p, return EOS", event);
3117 result = GST_FLOW_EOS;
3120 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3121 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
3122 GstBufferList *buffer_list;
3124 buffer_list = GST_BUFFER_LIST_CAST (data);
3126 result = gst_pad_push_list (queue->srcpad, buffer_list);
3127 g_atomic_int_set (&queue->downstream_may_block, 0);
3129 /* need to check for srcresult here as well */
3130 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3131 if (result == GST_FLOW_EOS) {
3132 data = gst_queue2_dequeue_on_eos (queue, &item_type);
3135 /* Since we will still accept EOS and SEGMENT we return _FLOW_OK
3136 * to the caller so that the task function does not shut down */
3137 result = GST_FLOW_OK;
3139 } else if (item_type == GST_QUEUE2_ITEM_TYPE_QUERY) {
3140 GstQuery *query = GST_QUERY_CAST (data);
3142 GST_LOG_OBJECT (queue->srcpad, "Peering query %p", query);
3143 queue->last_handled_query = query;
3144 queue->last_query = gst_pad_peer_query (queue->srcpad, query);
3145 GST_LOG_OBJECT (queue->srcpad, "Peered query");
3146 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3147 "did query %p, return %d", query, queue->last_query);
3148 g_cond_signal (&queue->query_handled);
3149 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3150 result = GST_FLOW_OK;
3157 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3158 "exit because we have no item in the queue");
3159 return GST_FLOW_ERROR;
3163 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are %s",
3164 gst_flow_get_name (queue->srcresult));
3165 return queue->srcresult;
3169 /* called repeatedly with @pad as the source pad. This function should push out
3170 * data to the peer element. */
3172 gst_queue2_loop (GstPad * pad)
3175 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
3176 GstFlowReturn ret = GST_FLOW_OK;
3181 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
3183 /* have to lock for thread-safety */
3184 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3186 if (gst_queue2_is_empty (queue)) {
3189 /* pause the timer while we wait. The fact that we are waiting does not mean
3190 * the byterate on the output pad is lower */
3191 if ((started = queue->out_timer_started))
3192 g_timer_stop (queue->out_timer);
3194 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
3195 "queue is empty, waiting for new data");
3197 /* Wait for data to be available, we could be unlocked because of a flush. */
3198 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
3200 while (gst_queue2_is_empty (queue));
3202 /* and continue if we were running before */
3204 g_timer_continue (queue->out_timer);
3206 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
3207 /* if buffering mode is GST_BUFFERING_LIVE, it is rtsp streaming */
3208 if (!((queue->mode == GST_BUFFERING_LIVE) && queue->is_buffering)) {
3209 ret = gst_queue2_push_one (queue);
3212 ret = gst_queue2_push_one (queue);
3214 queue->srcresult = ret;
3215 queue->sinkresult = ret;
3216 if (ret != GST_FLOW_OK)
3219 GST_QUEUE2_MUTEX_UNLOCK (queue);
3220 gst_queue2_post_buffering (queue);
3227 gboolean eos = queue->is_eos;
3228 GstFlowReturn ret = queue->srcresult;
3230 gst_pad_pause_task (queue->srcpad);
3231 if (ret == GST_FLOW_FLUSHING) {
3232 gst_queue2_locked_flush (queue, FALSE, FALSE);
3234 GST_QUEUE2_SIGNAL_DEL (queue);
3235 queue->last_query = FALSE;
3236 g_cond_signal (&queue->query_handled);
3238 GST_QUEUE2_MUTEX_UNLOCK (queue);
3239 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
3240 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
3241 /* let app know about us giving up if upstream is not expected to do so */
3242 /* EOS is already taken care of elsewhere */
3243 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
3244 GST_ELEMENT_FLOW_ERROR (queue, ret);
3245 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
3252 gst_queue2_handle_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
3254 gboolean res = TRUE;
3255 GstQueue2 *queue = GST_QUEUE2 (parent);
3257 #ifndef GST_DISABLE_GST_DEBUG
3258 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
3259 event, GST_EVENT_TYPE_NAME (event));
3262 switch (GST_EVENT_TYPE (event)) {
3263 case GST_EVENT_FLUSH_START:
3264 if (QUEUE_IS_USING_QUEUE (queue)) {
3265 /* just forward upstream */
3266 res = gst_pad_push_event (queue->sinkpad, event);
3268 /* now unblock the getrange function */
3269 GST_QUEUE2_MUTEX_LOCK (queue);
3270 GST_DEBUG_OBJECT (queue, "flushing");
3271 queue->srcresult = GST_FLOW_FLUSHING;
3272 GST_QUEUE2_SIGNAL_ADD (queue);
3273 GST_QUEUE2_MUTEX_UNLOCK (queue);
3275 /* when using a temp file, we eat the event */
3277 gst_event_unref (event);
3280 case GST_EVENT_FLUSH_STOP:
3281 if (QUEUE_IS_USING_QUEUE (queue)) {
3282 /* just forward upstream */
3283 res = gst_pad_push_event (queue->sinkpad, event);
3285 /* now unblock the getrange function */
3286 GST_QUEUE2_MUTEX_LOCK (queue);
3287 queue->srcresult = GST_FLOW_OK;
3288 GST_QUEUE2_MUTEX_UNLOCK (queue);
3290 /* when using a temp file, we eat the event */
3292 gst_event_unref (event);
3295 case GST_EVENT_RECONFIGURE:
3296 GST_QUEUE2_MUTEX_LOCK (queue);
3297 /* assume downstream is linked now and try to push again */
3298 if (queue->srcresult == GST_FLOW_NOT_LINKED) {
3299 queue->srcresult = GST_FLOW_OK;
3300 queue->sinkresult = GST_FLOW_OK;
3301 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
3302 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad,
3306 GST_QUEUE2_MUTEX_UNLOCK (queue);
3308 res = gst_pad_push_event (queue->sinkpad, event);
3311 res = gst_pad_push_event (queue->sinkpad, event);
3319 gst_queue2_handle_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
3323 queue = GST_QUEUE2 (parent);
3325 switch (GST_QUERY_TYPE (query)) {
3326 case GST_QUERY_POSITION:
3331 if (!gst_pad_peer_query (queue->sinkpad, query))
3334 /* get peer position */
3335 gst_query_parse_position (query, &format, &peer_pos);
3337 /* FIXME: this code assumes that there's no discont in the queue */
3339 case GST_FORMAT_BYTES:
3340 peer_pos -= queue->cur_level.bytes;
3341 if (peer_pos < 0) /* Clamp result to 0 */
3344 case GST_FORMAT_TIME:
3345 peer_pos -= queue->cur_level.time;
3346 if (peer_pos < 0) /* Clamp result to 0 */
3350 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
3351 "know how to adjust value", gst_format_get_name (format));
3354 /* set updated position */
3355 gst_query_set_position (query, format, peer_pos);
3358 case GST_QUERY_DURATION:
3360 GST_DEBUG_OBJECT (queue, "doing peer query");
3362 if (!gst_pad_peer_query (queue->sinkpad, query))
3365 GST_DEBUG_OBJECT (queue, "peer query success");
3368 case GST_QUERY_BUFFERING:
3371 gboolean is_buffering;
3372 GstBufferingMode mode;
3373 gint avg_in, avg_out;
3374 gint64 buffering_left;
3376 GST_DEBUG_OBJECT (queue, "query buffering");
3378 get_buffering_level (queue, &is_buffering, &percent);
3379 percent = convert_to_buffering_percent (queue, percent);
3380 gst_query_set_buffering_percent (query, is_buffering, percent);
3382 get_buffering_stats (queue, percent, &mode, &avg_in, &avg_out,
3384 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
3387 if (!QUEUE_IS_USING_QUEUE (queue)) {
3388 /* add ranges for download and ringbuffer buffering */
3390 gint64 start, stop, range_start, range_stop;
3391 guint64 writing_pos;
3392 gint64 estimated_total;
3394 gboolean peer_res, is_eos;
3395 GstQueue2Range *queued_ranges;
3397 /* we need a current download region */
3398 if (queue->current == NULL)
3401 writing_pos = queue->current->writing_pos;
3402 is_eos = queue->is_eos;
3405 /* we're EOS, we know the duration in bytes now */
3407 duration = writing_pos;
3409 /* get duration of upstream in bytes */
3410 peer_res = gst_pad_peer_query_duration (queue->sinkpad,
3411 GST_FORMAT_BYTES, &duration);
3414 GST_DEBUG_OBJECT (queue, "percent %d, duration %" G_GINT64_FORMAT
3415 ", writing %" G_GINT64_FORMAT, percent, duration, writing_pos);
3417 /* calculate remaining and total download time */
3418 if (peer_res && avg_in > 0.0)
3419 estimated_total = ((duration - writing_pos) * 1000) / avg_in;
3421 estimated_total = -1;
3423 GST_DEBUG_OBJECT (queue, "estimated-total %" G_GINT64_FORMAT,
3426 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
3429 case GST_FORMAT_PERCENT:
3430 /* we need duration */
3435 /* get our available data relative to the duration */
3438 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, writing_pos,
3443 case GST_FORMAT_BYTES:
3453 /* fill out the buffered ranges */
3454 for (queued_ranges = queue->ranges; queued_ranges;
3455 queued_ranges = queued_ranges->next) {
3457 case GST_FORMAT_PERCENT:
3458 if (duration == -1) {
3463 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3465 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3466 queued_ranges->reading_pos, duration);
3469 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3470 queued_ranges->offset, duration);
3473 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
3474 queued_ranges->writing_pos, duration);
3476 case GST_FORMAT_BYTES:
3477 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3478 range_start = queued_ranges->reading_pos;
3480 range_start = queued_ranges->offset;
3482 range_stop = queued_ranges->writing_pos;
3489 if (range_start == range_stop)
3491 GST_DEBUG_OBJECT (queue,
3492 "range starting at %" G_GINT64_FORMAT " and finishing at %"
3493 G_GINT64_FORMAT, range_start, range_stop);
3494 gst_query_add_buffering_range (query, range_start, range_stop);
3497 gst_query_set_buffering_range (query, format, start, stop,
3502 case GST_QUERY_SCHEDULING:
3505 GstSchedulingFlags flags = 0;
3506 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3507 if ((!QUEUE_IS_USING_QUEUE (queue)) && !gst_pad_peer_query (queue->sinkpad, query))
3509 if (!gst_pad_peer_query (queue->sinkpad, query))
3513 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
3515 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3516 #ifndef TIZEN_PROFILE_TV
3517 if (!(flags & GST_SCHEDULING_FLAG_SEEKABLE)) {
3518 GST_DEBUG_OBJECT(queue, "peer can support only push mode");
3519 gst_query_set_scheduling (query, flags, 0, -1, 0);
3520 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3525 GST_DEBUG_OBJECT(queue, "peer can support pull mode");
3527 /* we can operate in pull mode when we are using a tempfile */
3528 pull_mode = !QUEUE_IS_USING_QUEUE (queue);
3531 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
3532 gst_query_set_scheduling (query, flags, 0, -1, 0);
3534 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
3535 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
3539 /* peer handled other queries */
3540 if (!gst_pad_query_default (pad, parent, query))
3550 GST_DEBUG_OBJECT (queue, "failed peer query");
3556 gst_queue2_handle_query (GstElement * element, GstQuery * query)
3558 GstQueue2 *queue = GST_QUEUE2 (element);
3560 /* simply forward to the srcpad query function */
3561 return gst_queue2_handle_src_query (queue->srcpad, GST_OBJECT_CAST (element),
3566 gst_queue2_update_upstream_size (GstQueue2 * queue)
3568 gint64 upstream_size = -1;
3570 if (gst_pad_peer_query_duration (queue->sinkpad, GST_FORMAT_BYTES,
3572 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
3574 /* upstream_size can be negative but queue->upstream_size is unsigned.
3575 * Prevent setting negative values to it (the query can return -1) */
3576 if (upstream_size >= 0)
3577 queue->upstream_size = upstream_size;
3579 queue->upstream_size = 0;
3583 static GstFlowReturn
3584 gst_queue2_get_range (GstPad * pad, GstObject * parent, guint64 offset,
3585 guint length, GstBuffer ** buffer)
3590 queue = GST_QUEUE2_CAST (parent);
3592 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
3593 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
3594 offset = (offset == -1) ? queue->current->reading_pos : offset;
3596 GST_DEBUG_OBJECT (queue,
3597 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
3599 /* catch any reads beyond the size of the file here to make sure queue2
3600 * doesn't send seek events beyond the size of the file upstream, since
3601 * that would confuse elements such as souphttpsrc and/or http servers.
3602 * Demuxers often just loop until EOS at the end of the file to figure out
3603 * when they've read all the end-headers or index chunks. */
3604 if (G_UNLIKELY (offset >= queue->upstream_size)) {
3605 gst_queue2_update_upstream_size (queue);
3606 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
3607 goto out_unexpected;
3610 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
3611 gst_queue2_update_upstream_size (queue);
3612 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
3613 length = queue->upstream_size - offset;
3614 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
3618 /* FIXME - function will block when the range is not yet available */
3619 ret = gst_queue2_create_read (queue, offset, length, buffer);
3620 GST_QUEUE2_MUTEX_UNLOCK (queue);
3621 gst_queue2_post_buffering (queue);
3628 ret = queue->srcresult;
3630 GST_DEBUG_OBJECT (queue, "we are %s", gst_flow_get_name (ret));
3631 GST_QUEUE2_MUTEX_UNLOCK (queue);
3636 GST_DEBUG_OBJECT (queue, "read beyond end of file");
3637 GST_QUEUE2_MUTEX_UNLOCK (queue);
3638 return GST_FLOW_EOS;
3642 /* sink currently only operates in push mode */
3644 gst_queue2_sink_activate_mode (GstPad * pad, GstObject * parent,
3645 GstPadMode mode, gboolean active)
3650 queue = GST_QUEUE2 (parent);
3653 case GST_PAD_MODE_PUSH:
3655 GST_QUEUE2_MUTEX_LOCK (queue);
3656 GST_DEBUG_OBJECT (queue, "activating push mode");
3657 queue->srcresult = GST_FLOW_OK;
3658 queue->sinkresult = GST_FLOW_OK;
3659 queue->is_eos = FALSE;
3660 queue->unexpected = FALSE;
3661 reset_rate_timer (queue);
3662 GST_QUEUE2_MUTEX_UNLOCK (queue);
3664 /* unblock chain function */
3665 GST_QUEUE2_MUTEX_LOCK (queue);
3666 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3667 queue->srcresult = GST_FLOW_FLUSHING;
3668 queue->sinkresult = GST_FLOW_FLUSHING;
3669 GST_QUEUE2_SIGNAL_DEL (queue);
3670 GST_QUEUE2_MUTEX_UNLOCK (queue);
3672 /* wait until it is unblocked and clean up */
3673 GST_PAD_STREAM_LOCK (pad);
3674 GST_QUEUE2_MUTEX_LOCK (queue);
3675 gst_queue2_locked_flush (queue, TRUE, FALSE);
3676 GST_QUEUE2_MUTEX_UNLOCK (queue);
3677 GST_PAD_STREAM_UNLOCK (pad);
3688 /* src operating in push mode, we start a task on the source pad that pushes out
3689 * buffers from the queue */
3691 gst_queue2_src_activate_push (GstPad * pad, GstObject * parent, gboolean active)
3693 gboolean result = FALSE;
3696 queue = GST_QUEUE2 (parent);
3699 GST_QUEUE2_MUTEX_LOCK (queue);
3700 GST_DEBUG_OBJECT (queue, "activating push mode");
3701 queue->srcresult = GST_FLOW_OK;
3702 queue->sinkresult = GST_FLOW_OK;
3703 queue->is_eos = FALSE;
3704 queue->unexpected = FALSE;
3706 gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad, NULL);
3707 GST_QUEUE2_MUTEX_UNLOCK (queue);
3709 /* unblock loop function */
3710 GST_QUEUE2_MUTEX_LOCK (queue);
3711 GST_DEBUG_OBJECT (queue, "deactivating push mode");
3712 queue->srcresult = GST_FLOW_FLUSHING;
3713 queue->sinkresult = GST_FLOW_FLUSHING;
3714 /* the item add signal will unblock */
3715 GST_QUEUE2_SIGNAL_ADD (queue);
3716 GST_QUEUE2_MUTEX_UNLOCK (queue);
3718 /* step 2, make sure streaming finishes */
3719 result = gst_pad_stop_task (pad);
3721 GST_QUEUE2_MUTEX_LOCK (queue);
3722 gst_queue2_locked_flush (queue, FALSE, FALSE);
3723 GST_QUEUE2_MUTEX_UNLOCK (queue);
3729 /* pull mode, downstream will call our getrange function */
3731 gst_queue2_src_activate_pull (GstPad * pad, GstObject * parent, gboolean active)
3736 queue = GST_QUEUE2 (parent);
3739 GST_QUEUE2_MUTEX_LOCK (queue);
3740 if (!QUEUE_IS_USING_QUEUE (queue)) {
3741 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3742 /* open the temp file now */
3743 result = gst_queue2_open_temp_location_file (queue);
3744 } else if (!queue->ring_buffer) {
3745 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3746 result = ! !queue->ring_buffer;
3751 GST_DEBUG_OBJECT (queue, "activating pull mode");
3752 init_ranges (queue);
3753 queue->srcresult = GST_FLOW_OK;
3754 queue->sinkresult = GST_FLOW_OK;
3755 queue->is_eos = FALSE;
3756 queue->unexpected = FALSE;
3757 queue->upstream_size = 0;
3759 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3760 /* this is not allowed, we cannot operate in pull mode without a temp
3762 queue->srcresult = GST_FLOW_FLUSHING;
3763 queue->sinkresult = GST_FLOW_FLUSHING;
3766 GST_QUEUE2_MUTEX_UNLOCK (queue);
3768 GST_QUEUE2_MUTEX_LOCK (queue);
3769 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3770 queue->srcresult = GST_FLOW_FLUSHING;
3771 queue->sinkresult = GST_FLOW_FLUSHING;
3772 /* this will unlock getrange */
3773 GST_QUEUE2_SIGNAL_ADD (queue);
3775 GST_QUEUE2_MUTEX_UNLOCK (queue);
3782 gst_queue2_src_activate_mode (GstPad * pad, GstObject * parent, GstPadMode mode,
3788 case GST_PAD_MODE_PULL:
3789 res = gst_queue2_src_activate_pull (pad, parent, active);
3791 case GST_PAD_MODE_PUSH:
3792 res = gst_queue2_src_activate_push (pad, parent, active);
3795 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
3802 static GstStateChangeReturn
3803 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3806 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3808 queue = GST_QUEUE2 (element);
3810 switch (transition) {
3811 case GST_STATE_CHANGE_NULL_TO_READY:
3813 case GST_STATE_CHANGE_READY_TO_PAUSED:
3814 GST_QUEUE2_MUTEX_LOCK (queue);
3815 if (!QUEUE_IS_USING_QUEUE (queue)) {
3816 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3817 if (!gst_queue2_open_temp_location_file (queue))
3818 ret = GST_STATE_CHANGE_FAILURE;
3820 if (queue->ring_buffer) {
3821 g_free (queue->ring_buffer);
3822 queue->ring_buffer = NULL;
3824 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3825 ret = GST_STATE_CHANGE_FAILURE;
3827 init_ranges (queue);
3829 queue->segment_event_received = FALSE;
3830 queue->starting_segment = NULL;
3831 gst_event_replace (&queue->stream_start_event, NULL);
3832 GST_QUEUE2_MUTEX_UNLOCK (queue);
3834 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3835 #ifdef TIZEN_FEATURE_QUEUE2_MODIFICATION
3836 GST_QUEUE2_MUTEX_LOCK (queue);
3837 queue->is_buffering = FALSE;
3838 GST_QUEUE2_MUTEX_UNLOCK (queue);
3845 if (ret == GST_STATE_CHANGE_FAILURE)
3848 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3850 if (ret == GST_STATE_CHANGE_FAILURE)
3853 switch (transition) {
3854 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3856 case GST_STATE_CHANGE_PAUSED_TO_READY:
3857 GST_QUEUE2_MUTEX_LOCK (queue);
3858 if (!QUEUE_IS_USING_QUEUE (queue)) {
3859 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3860 gst_queue2_close_temp_location_file (queue);
3861 } else if (queue->ring_buffer) {
3862 g_free (queue->ring_buffer);
3863 queue->ring_buffer = NULL;
3865 clean_ranges (queue);
3867 if (queue->starting_segment != NULL) {
3868 gst_event_unref (queue->starting_segment);
3869 queue->starting_segment = NULL;
3871 gst_event_replace (&queue->stream_start_event, NULL);
3872 GST_QUEUE2_MUTEX_UNLOCK (queue);
3874 case GST_STATE_CHANGE_READY_TO_NULL:
3883 /* changing the capacity of the queue must wake up
3884 * the _chain function, it might have more room now
3885 * to store the buffer/event in the queue */
3886 #define QUEUE_CAPACITY_CHANGE(q) \
3887 GST_QUEUE2_SIGNAL_DEL (queue); \
3888 if (queue->use_buffering) \
3889 update_buffering (queue);
3891 /* Changing the minimum required fill level must
3892 * wake up the _loop function as it might now
3893 * be able to preceed.
3895 #define QUEUE_THRESHOLD_CHANGE(q)\
3896 GST_QUEUE2_SIGNAL_ADD (queue);
3899 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3903 /* the element must be stopped in order to do this */
3904 GST_OBJECT_LOCK (queue);
3905 state = GST_STATE (queue);
3906 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3908 GST_OBJECT_UNLOCK (queue);
3910 /* set new location */
3911 g_free (queue->temp_template);
3912 queue->temp_template = g_strdup (template);
3919 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3920 GST_OBJECT_UNLOCK (queue);
3925 gst_queue2_set_property (GObject * object,
3926 guint prop_id, const GValue * value, GParamSpec * pspec)
3928 GstQueue2 *queue = GST_QUEUE2 (object);
3930 /* someone could change levels here, and since this
3931 * affects the get/put funcs, we need to lock for safety. */
3932 GST_QUEUE2_MUTEX_LOCK (queue);
3935 case PROP_MAX_SIZE_BYTES:
3936 queue->max_level.bytes = g_value_get_uint (value);
3937 QUEUE_CAPACITY_CHANGE (queue);
3939 case PROP_MAX_SIZE_BUFFERS:
3940 queue->max_level.buffers = g_value_get_uint (value);
3941 QUEUE_CAPACITY_CHANGE (queue);
3943 case PROP_MAX_SIZE_TIME:
3944 queue->max_level.time = g_value_get_uint64 (value);
3945 /* set rate_time to the same value. We use an extra field in the level
3946 * structure so that we can easily access and compare it */
3947 queue->max_level.rate_time = queue->max_level.time;
3948 QUEUE_CAPACITY_CHANGE (queue);
3950 case PROP_USE_BUFFERING:
3951 queue->use_buffering = g_value_get_boolean (value);
3952 if (!queue->use_buffering && queue->is_buffering) {
3953 GST_DEBUG_OBJECT (queue, "Disabled buffering while buffering, "
3954 "posting 100%% message");
3955 SET_PERCENT (queue, 100);
3956 queue->is_buffering = FALSE;
3959 if (queue->use_buffering) {
3960 queue->is_buffering = TRUE;
3961 update_buffering (queue);
3964 case PROP_USE_TAGS_BITRATE:
3965 queue->use_tags_bitrate = g_value_get_boolean (value);
3967 case PROP_USE_RATE_ESTIMATE:
3968 queue->use_rate_estimate = g_value_get_boolean (value);
3970 case PROP_LOW_PERCENT:
3971 queue->low_watermark = g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3972 if (queue->is_buffering)
3973 update_buffering (queue);
3975 case PROP_HIGH_PERCENT:
3976 queue->high_watermark =
3977 g_value_get_int (value) * BUF_LEVEL_PERCENT_FACTOR;
3978 if (queue->is_buffering)
3979 update_buffering (queue);
3981 case PROP_LOW_WATERMARK:
3982 queue->low_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3983 if (queue->is_buffering)
3984 update_buffering (queue);
3986 case PROP_HIGH_WATERMARK:
3987 queue->high_watermark = g_value_get_double (value) * MAX_BUFFERING_LEVEL;
3988 if (queue->is_buffering)
3989 update_buffering (queue);
3991 case PROP_TEMP_TEMPLATE:
3992 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3994 case PROP_TEMP_REMOVE:
3995 queue->temp_remove = g_value_get_boolean (value);
3997 case PROP_RING_BUFFER_MAX_SIZE:
3998 queue->ring_buffer_max_size = g_value_get_uint64 (value);
4000 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
4001 case PROP_BUFFER_MODE:
4002 queue->mode = g_value_get_enum (value);
4006 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4010 GST_QUEUE2_MUTEX_UNLOCK (queue);
4011 gst_queue2_post_buffering (queue);
4015 gst_queue2_get_property (GObject * object,
4016 guint prop_id, GValue * value, GParamSpec * pspec)
4018 GstQueue2 *queue = GST_QUEUE2 (object);
4020 GST_QUEUE2_MUTEX_LOCK (queue);
4023 case PROP_CUR_LEVEL_BYTES:
4024 g_value_set_uint (value, queue->cur_level.bytes);
4026 case PROP_CUR_LEVEL_BUFFERS:
4027 g_value_set_uint (value, queue->cur_level.buffers);
4029 case PROP_CUR_LEVEL_TIME:
4030 g_value_set_uint64 (value, queue->cur_level.time);
4032 case PROP_MAX_SIZE_BYTES:
4033 g_value_set_uint (value, queue->max_level.bytes);
4035 case PROP_MAX_SIZE_BUFFERS:
4036 g_value_set_uint (value, queue->max_level.buffers);
4038 case PROP_MAX_SIZE_TIME:
4039 g_value_set_uint64 (value, queue->max_level.time);
4041 case PROP_USE_BUFFERING:
4042 g_value_set_boolean (value, queue->use_buffering);
4044 case PROP_USE_TAGS_BITRATE:
4045 g_value_set_boolean (value, queue->use_tags_bitrate);
4047 case PROP_USE_RATE_ESTIMATE:
4048 g_value_set_boolean (value, queue->use_rate_estimate);
4050 case PROP_LOW_PERCENT:
4051 g_value_set_int (value, queue->low_watermark / BUF_LEVEL_PERCENT_FACTOR);
4053 case PROP_HIGH_PERCENT:
4054 g_value_set_int (value, queue->high_watermark / BUF_LEVEL_PERCENT_FACTOR);
4056 case PROP_LOW_WATERMARK:
4057 g_value_set_double (value, queue->low_watermark /
4058 (gdouble) MAX_BUFFERING_LEVEL);
4060 case PROP_HIGH_WATERMARK:
4061 g_value_set_double (value, queue->high_watermark /
4062 (gdouble) MAX_BUFFERING_LEVEL);
4064 case PROP_TEMP_TEMPLATE:
4065 g_value_set_string (value, queue->temp_template);
4067 case PROP_TEMP_LOCATION:
4068 g_value_set_string (value, queue->temp_location);
4070 case PROP_TEMP_REMOVE:
4071 g_value_set_boolean (value, queue->temp_remove);
4073 case PROP_RING_BUFFER_MAX_SIZE:
4074 g_value_set_uint64 (value, queue->ring_buffer_max_size);
4076 case PROP_AVG_IN_RATE:
4078 gdouble in_rate = queue->byte_in_rate;
4080 /* During the first RATE_INTERVAL, byte_in_rate will not have been
4081 * calculated, so calculate it here. */
4082 if (in_rate == 0.0 && queue->bytes_in
4083 && queue->last_update_in_rates_elapsed > 0.0)
4084 in_rate = queue->bytes_in / queue->last_update_in_rates_elapsed;
4086 g_value_set_int64 (value, (gint64) in_rate);
4089 #ifdef TIZEN_FEATURE_RTSPSRC_MODIFICATION
4090 case PROP_BUFFER_MODE:
4091 g_value_set_enum (value, queue->mode);
4095 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
4099 GST_QUEUE2_MUTEX_UNLOCK (queue);