1 /* GStreamer
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>
7 *
8 * gstqueue2.c:
9 *
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.
14 *
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.
19 *
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
24 */
26 /**
27 * SECTION:element-queue2
28 *
29 * Data is queued until one of the limits specified by the
30 * #GstQueue2:max-size-buffers, #GstQueue2:max-size-bytes and/or
31 * #GstQueue2:max-size-time properties has been reached. Any attempt to push
32 * more buffers into the queue will block the pushing thread until more space
33 * becomes available.
34 *
35 * The queue will create a new thread on the source pad to decouple the
36 * processing on sink and source pad.
37 *
38 * You can query how many buffers are queued by reading the
39 * #GstQueue2:current-level-buffers property.
40 *
41 * The default queue size limits are 100 buffers, 2MB of data, or
42 * two seconds worth of data, whichever is reached first.
43 *
44 * If you set temp-tmpl to a value such as /tmp/gstreamer-XXXXXX, the element
45 * will allocate a random free filename and buffer data in the file.
46 * By using this, it will buffer the entire stream data on the file independently
47 * of the queue size limits, they will only be used for buffering statistics.
48 *
49 * Since 0.10.24, setting the temp-location property with a filename is deprecated
50 * because it's impossible to securely open a temporary file in this way. The
51 * property will still be used to notify the application of the allocated
52 * filename, though.
53 *
54 * Last reviewed on 2009-07-10 (0.10.24)
55 */
57 #ifdef HAVE_CONFIG_H
58 #include "config.h"
59 #endif
61 #include "gstqueue2.h"
63 #include <glib/gstdio.h>
65 #include "gst/gst-i18n-lib.h"
66 #include "gst/glib-compat-private.h"
68 #include <string.h>
70 #ifdef G_OS_WIN32
71 #include <io.h> /* lseek, open, close, read */
72 #undef lseek
73 #define lseek _lseeki64
74 #undef off_t
75 #define off_t guint64
76 #else
77 #include <unistd.h>
78 #endif
80 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
81 GST_PAD_SINK,
82 GST_PAD_ALWAYS,
83 GST_STATIC_CAPS_ANY);
85 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86 GST_PAD_SRC,
87 GST_PAD_ALWAYS,
88 GST_STATIC_CAPS_ANY);
90 GST_DEBUG_CATEGORY_STATIC (queue_debug);
91 #define GST_CAT_DEFAULT (queue_debug)
92 GST_DEBUG_CATEGORY_STATIC (queue_dataflow);
94 enum
95 {
96 LAST_SIGNAL
97 };
99 /* other defines */
100 #define DEFAULT_BUFFER_SIZE 4096
101 #define QUEUE_IS_USING_TEMP_FILE(queue) ((queue)->temp_location_set || (queue)->temp_template != NULL)
102 #define QUEUE_IS_USING_RING_BUFFER(queue) ((queue)->ring_buffer_max_size != 0) /* for consistency with the above macro */
103 #define QUEUE_IS_USING_QUEUE(queue) (!QUEUE_IS_USING_TEMP_FILE(queue) && !QUEUE_IS_USING_RING_BUFFER (queue))
105 #define QUEUE_MAX_BYTES(queue) MIN((queue)->max_level.bytes, (queue)->ring_buffer_max_size)
107 /* default property values */
108 #define DEFAULT_MAX_SIZE_BUFFERS 100 /* 100 buffers */
109 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
110 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
111 #define DEFAULT_USE_BUFFERING FALSE
112 #define DEFAULT_USE_RATE_ESTIMATE TRUE
113 #define DEFAULT_LOW_PERCENT 10
114 #define DEFAULT_HIGH_PERCENT 99
115 #define DEFAULT_TEMP_REMOVE TRUE
116 #define DEFAULT_RING_BUFFER_MAX_SIZE 0
118 enum
119 {
120 PROP_0,
121 PROP_CUR_LEVEL_BUFFERS,
122 PROP_CUR_LEVEL_BYTES,
123 PROP_CUR_LEVEL_TIME,
124 PROP_MAX_SIZE_BUFFERS,
125 PROP_MAX_SIZE_BYTES,
126 PROP_MAX_SIZE_TIME,
127 PROP_USE_BUFFERING,
128 PROP_USE_RATE_ESTIMATE,
129 PROP_LOW_PERCENT,
130 PROP_HIGH_PERCENT,
131 PROP_TEMP_TEMPLATE,
132 PROP_TEMP_LOCATION,
133 PROP_TEMP_REMOVE,
134 PROP_RING_BUFFER_MAX_SIZE,
135 PROP_LAST
136 };
138 #define GST_QUEUE2_CLEAR_LEVEL(l) G_STMT_START { \
139 l.buffers = 0; \
140 l.bytes = 0; \
141 l.time = 0; \
142 l.rate_time = 0; \
143 } G_STMT_END
145 #define STATUS(queue, pad, msg) \
146 GST_CAT_LOG_OBJECT (queue_dataflow, queue, \
147 "(%s:%s) " msg ": %u of %u buffers, %u of %u " \
148 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
149 " ns, %"G_GUINT64_FORMAT" items", \
150 GST_DEBUG_PAD_NAME (pad), \
151 queue->cur_level.buffers, \
152 queue->max_level.buffers, \
153 queue->cur_level.bytes, \
154 queue->max_level.bytes, \
155 queue->cur_level.time, \
156 queue->max_level.time, \
157 (guint64) (!QUEUE_IS_USING_QUEUE(queue) ? \
158 queue->current->writing_pos - queue->current->max_reading_pos : \
159 queue->queue.length))
161 #define GST_QUEUE2_MUTEX_LOCK(q) G_STMT_START { \
162 g_mutex_lock (q->qlock); \
163 } G_STMT_END
165 #define GST_QUEUE2_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
166 GST_QUEUE2_MUTEX_LOCK (q); \
167 if (res != GST_FLOW_OK) \
168 goto label; \
169 } G_STMT_END
171 #define GST_QUEUE2_MUTEX_UNLOCK(q) G_STMT_START { \
172 g_mutex_unlock (q->qlock); \
173 } G_STMT_END
175 #define GST_QUEUE2_WAIT_DEL_CHECK(q, res, label) G_STMT_START { \
176 STATUS (queue, q->sinkpad, "wait for DEL"); \
177 q->waiting_del = TRUE; \
178 g_cond_wait (q->item_del, queue->qlock); \
179 q->waiting_del = FALSE; \
180 if (res != GST_FLOW_OK) { \
181 STATUS (queue, q->srcpad, "received DEL wakeup"); \
182 goto label; \
183 } \
184 STATUS (queue, q->sinkpad, "received DEL"); \
185 } G_STMT_END
187 #define GST_QUEUE2_WAIT_ADD_CHECK(q, res, label) G_STMT_START { \
188 STATUS (queue, q->srcpad, "wait for ADD"); \
189 q->waiting_add = TRUE; \
190 g_cond_wait (q->item_add, q->qlock); \
191 q->waiting_add = FALSE; \
192 if (res != GST_FLOW_OK) { \
193 STATUS (queue, q->srcpad, "received ADD wakeup"); \
194 goto label; \
195 } \
196 STATUS (queue, q->srcpad, "received ADD"); \
197 } G_STMT_END
199 #define GST_QUEUE2_SIGNAL_DEL(q) G_STMT_START { \
200 if (q->waiting_del) { \
201 STATUS (q, q->srcpad, "signal DEL"); \
202 g_cond_signal (q->item_del); \
203 } \
204 } G_STMT_END
206 #define GST_QUEUE2_SIGNAL_ADD(q) G_STMT_START { \
207 if (q->waiting_add) { \
208 STATUS (q, q->sinkpad, "signal ADD"); \
209 g_cond_signal (q->item_add); \
210 } \
211 } G_STMT_END
213 #define _do_init(bla) \
214 GST_DEBUG_CATEGORY_INIT (queue_debug, "queue2", 0, "queue element"); \
215 GST_DEBUG_CATEGORY_INIT (queue_dataflow, "queue2_dataflow", 0, \
216 "dataflow inside the queue element");
218 GST_BOILERPLATE_FULL (GstQueue2, gst_queue2, GstElement, GST_TYPE_ELEMENT,
219 _do_init);
221 static void gst_queue2_finalize (GObject * object);
223 static void gst_queue2_set_property (GObject * object,
224 guint prop_id, const GValue * value, GParamSpec * pspec);
225 static void gst_queue2_get_property (GObject * object,
226 guint prop_id, GValue * value, GParamSpec * pspec);
228 static GstFlowReturn gst_queue2_chain (GstPad * pad, GstBuffer * buffer);
229 static GstFlowReturn gst_queue2_chain_list (GstPad * pad,
230 GstBufferList * buffer_list);
231 static GstFlowReturn gst_queue2_bufferalloc (GstPad * pad, guint64 offset,
232 guint size, GstCaps * caps, GstBuffer ** buf);
233 static GstFlowReturn gst_queue2_push_one (GstQueue2 * queue);
234 static void gst_queue2_loop (GstPad * pad);
236 static gboolean gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event);
238 static gboolean gst_queue2_handle_src_event (GstPad * pad, GstEvent * event);
239 static gboolean gst_queue2_handle_src_query (GstPad * pad, GstQuery * query);
240 static gboolean gst_queue2_handle_query (GstElement * element,
241 GstQuery * query);
243 static GstCaps *gst_queue2_getcaps (GstPad * pad);
244 static gboolean gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps);
246 static GstFlowReturn gst_queue2_get_range (GstPad * pad, guint64 offset,
247 guint length, GstBuffer ** buffer);
248 static gboolean gst_queue2_src_checkgetrange_function (GstPad * pad);
250 static gboolean gst_queue2_src_activate_pull (GstPad * pad, gboolean active);
251 static gboolean gst_queue2_src_activate_push (GstPad * pad, gboolean active);
252 static gboolean gst_queue2_sink_activate_push (GstPad * pad, gboolean active);
253 static GstStateChangeReturn gst_queue2_change_state (GstElement * element,
254 GstStateChange transition);
256 static gboolean gst_queue2_is_empty (GstQueue2 * queue);
257 static gboolean gst_queue2_is_filled (GstQueue2 * queue);
259 static void update_cur_level (GstQueue2 * queue, GstQueue2Range * range);
261 typedef enum
262 {
263 GST_QUEUE2_ITEM_TYPE_UNKNOWN = 0,
264 GST_QUEUE2_ITEM_TYPE_BUFFER,
265 GST_QUEUE2_ITEM_TYPE_BUFFER_LIST,
266 GST_QUEUE2_ITEM_TYPE_EVENT
267 } GstQueue2ItemType;
269 /* static guint gst_queue2_signals[LAST_SIGNAL] = { 0 }; */
271 static void
272 gst_queue2_base_init (gpointer g_class)
273 {
274 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
276 gst_element_class_add_pad_template (gstelement_class,
277 gst_static_pad_template_get (&srctemplate));
278 gst_element_class_add_pad_template (gstelement_class,
279 gst_static_pad_template_get (&sinktemplate));
281 gst_element_class_set_details_simple (gstelement_class, "Queue 2",
282 "Generic",
283 "Simple data queue",
284 "Erik Walthinsen <omega@cse.ogi.edu>, "
285 "Wim Taymans <wim.taymans@gmail.com>");
286 }
288 static void
289 gst_queue2_class_init (GstQueue2Class * klass)
290 {
291 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
292 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
294 gobject_class->set_property = gst_queue2_set_property;
295 gobject_class->get_property = gst_queue2_get_property;
297 /* properties */
298 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BYTES,
299 g_param_spec_uint ("current-level-bytes", "Current level (kB)",
300 "Current amount of data in the queue (bytes)",
301 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_BUFFERS,
303 g_param_spec_uint ("current-level-buffers", "Current level (buffers)",
304 "Current number of buffers in the queue",
305 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_CUR_LEVEL_TIME,
307 g_param_spec_uint64 ("current-level-time", "Current level (ns)",
308 "Current amount of data in the queue (in ns)",
309 0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
312 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
313 "Max. amount of data in the queue (bytes, 0=disable)",
314 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BUFFERS,
317 g_param_spec_uint ("max-size-buffers", "Max. size (buffers)",
318 "Max. number of buffers in the queue (0=disable)", 0, G_MAXUINT,
319 DEFAULT_MAX_SIZE_BUFFERS,
320 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
322 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
323 "Max. amount of data in the queue (in ns, 0=disable)", 0, G_MAXUINT64,
324 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_USE_BUFFERING,
327 g_param_spec_boolean ("use-buffering", "Use buffering",
328 "Emit GST_MESSAGE_BUFFERING based on low-/high-percent thresholds",
329 DEFAULT_USE_BUFFERING, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 g_object_class_install_property (gobject_class, PROP_USE_RATE_ESTIMATE,
331 g_param_spec_boolean ("use-rate-estimate", "Use Rate Estimate",
332 "Estimate the bitrate of the stream to calculate time level",
333 DEFAULT_USE_RATE_ESTIMATE,
334 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
336 g_param_spec_int ("low-percent", "Low percent",
337 "Low threshold for buffering to start. Only used if use-buffering is True",
338 0, 100, DEFAULT_LOW_PERCENT,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
340 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
341 g_param_spec_int ("high-percent", "High percent",
342 "High threshold for buffering to finish. Only used if use-buffering is True",
343 0, 100, DEFAULT_HIGH_PERCENT,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
347 g_param_spec_string ("temp-template", "Temporary File Template",
348 "File template to store temporary files in, should contain directory "
349 "and XXXXXX. (NULL == disabled)",
350 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
353 g_param_spec_string ("temp-location", "Temporary File Location",
354 "Location to store temporary files in (Deprecated: Only read this "
355 "property, use temp-template to configure the name template)",
356 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358 /**
359 * GstQueue2:temp-remove
360 *
361 * When temp-template is set, remove the temporary file when going to READY.
362 *
363 * Since: 0.10.26
364 */
365 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
366 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
367 "Remove the temp-location after use",
368 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 /**
371 * GstQueue2:ring-buffer-max-size
372 *
373 * The maximum size of the ring buffer in bytes. If set to 0, the ring
374 * buffer is disabled. Default 0.
375 *
376 * Since: 0.10.31
377 */
378 g_object_class_install_property (gobject_class, PROP_RING_BUFFER_MAX_SIZE,
379 g_param_spec_uint64 ("ring-buffer-max-size",
380 "Max. ring buffer size (bytes)",
381 "Max. amount of data in the ring buffer (bytes, 0 = disabled)",
382 0, G_MAXUINT64, DEFAULT_RING_BUFFER_MAX_SIZE,
383 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 /* set several parent class virtual functions */
386 gobject_class->finalize = gst_queue2_finalize;
388 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_queue2_change_state);
389 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_queue2_handle_query);
390 }
392 static void
393 gst_queue2_init (GstQueue2 * queue, GstQueue2Class * g_class)
394 {
395 queue->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
397 gst_pad_set_chain_function (queue->sinkpad,
398 GST_DEBUG_FUNCPTR (gst_queue2_chain));
399 gst_pad_set_chain_list_function (queue->sinkpad,
400 GST_DEBUG_FUNCPTR (gst_queue2_chain_list));
401 gst_pad_set_activatepush_function (queue->sinkpad,
402 GST_DEBUG_FUNCPTR (gst_queue2_sink_activate_push));
403 gst_pad_set_event_function (queue->sinkpad,
404 GST_DEBUG_FUNCPTR (gst_queue2_handle_sink_event));
405 gst_pad_set_getcaps_function (queue->sinkpad,
406 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
407 gst_pad_set_acceptcaps_function (queue->sinkpad,
408 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
409 gst_pad_set_bufferalloc_function (queue->sinkpad,
410 GST_DEBUG_FUNCPTR (gst_queue2_bufferalloc));
411 gst_element_add_pad (GST_ELEMENT (queue), queue->sinkpad);
413 queue->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
415 gst_pad_set_activatepull_function (queue->srcpad,
416 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_pull));
417 gst_pad_set_activatepush_function (queue->srcpad,
418 GST_DEBUG_FUNCPTR (gst_queue2_src_activate_push));
419 gst_pad_set_getrange_function (queue->srcpad,
420 GST_DEBUG_FUNCPTR (gst_queue2_get_range));
421 gst_pad_set_checkgetrange_function (queue->srcpad,
422 GST_DEBUG_FUNCPTR (gst_queue2_src_checkgetrange_function));
423 gst_pad_set_getcaps_function (queue->srcpad,
424 GST_DEBUG_FUNCPTR (gst_queue2_getcaps));
425 gst_pad_set_acceptcaps_function (queue->srcpad,
426 GST_DEBUG_FUNCPTR (gst_queue2_acceptcaps));
427 gst_pad_set_event_function (queue->srcpad,
428 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_event));
429 gst_pad_set_query_function (queue->srcpad,
430 GST_DEBUG_FUNCPTR (gst_queue2_handle_src_query));
431 gst_element_add_pad (GST_ELEMENT (queue), queue->srcpad);
433 /* levels */
434 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
435 queue->max_level.buffers = DEFAULT_MAX_SIZE_BUFFERS;
436 queue->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
437 queue->max_level.time = DEFAULT_MAX_SIZE_TIME;
438 queue->max_level.rate_time = DEFAULT_MAX_SIZE_TIME;
439 queue->use_buffering = DEFAULT_USE_BUFFERING;
440 queue->use_rate_estimate = DEFAULT_USE_RATE_ESTIMATE;
441 queue->low_percent = DEFAULT_LOW_PERCENT;
442 queue->high_percent = DEFAULT_HIGH_PERCENT;
444 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
445 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
447 queue->sinktime = GST_CLOCK_TIME_NONE;
448 queue->srctime = GST_CLOCK_TIME_NONE;
449 queue->sink_tainted = TRUE;
450 queue->src_tainted = TRUE;
452 queue->srcresult = GST_FLOW_WRONG_STATE;
453 queue->sinkresult = GST_FLOW_WRONG_STATE;
454 queue->is_eos = FALSE;
455 queue->in_timer = g_timer_new ();
456 queue->out_timer = g_timer_new ();
458 queue->qlock = g_mutex_new ();
459 queue->waiting_add = FALSE;
460 queue->item_add = g_cond_new ();
461 queue->waiting_del = FALSE;
462 queue->item_del = g_cond_new ();
463 g_queue_init (&queue->queue);
465 queue->buffering_percent = 100;
467 /* tempfile related */
468 queue->temp_template = NULL;
469 queue->temp_location = NULL;
470 queue->temp_location_set = FALSE;
471 queue->temp_remove = DEFAULT_TEMP_REMOVE;
473 queue->ring_buffer = NULL;
474 queue->ring_buffer_max_size = DEFAULT_RING_BUFFER_MAX_SIZE;
476 GST_DEBUG_OBJECT (queue,
477 "initialized queue's not_empty & not_full conditions");
478 }
480 /* called only once, as opposed to dispose */
481 static void
482 gst_queue2_finalize (GObject * object)
483 {
484 GstQueue2 *queue = GST_QUEUE2 (object);
486 GST_DEBUG_OBJECT (queue, "finalizing queue");
488 while (!g_queue_is_empty (&queue->queue)) {
489 GstMiniObject *data = g_queue_pop_head (&queue->queue);
491 gst_mini_object_unref (data);
492 }
494 g_queue_clear (&queue->queue);
495 g_mutex_free (queue->qlock);
496 g_cond_free (queue->item_add);
497 g_cond_free (queue->item_del);
498 g_timer_destroy (queue->in_timer);
499 g_timer_destroy (queue->out_timer);
501 /* temp_file path cleanup */
502 g_free (queue->temp_template);
503 g_free (queue->temp_location);
505 G_OBJECT_CLASS (parent_class)->finalize (object);
506 }
508 static void
509 debug_ranges (GstQueue2 * queue)
510 {
511 GstQueue2Range *walk;
513 for (walk = queue->ranges; walk; walk = walk->next) {
514 GST_DEBUG_OBJECT (queue,
515 "range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "] (rb [%"
516 G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "]), reading %" G_GUINT64_FORMAT
517 " current range? %s", walk->offset, walk->writing_pos, walk->rb_offset,
518 walk->rb_writing_pos, walk->reading_pos,
519 walk == queue->current ? "**y**" : " n ");
520 }
521 }
523 /* clear all the downloaded ranges */
524 static void
525 clean_ranges (GstQueue2 * queue)
526 {
527 GST_DEBUG_OBJECT (queue, "clean queue ranges");
529 g_slice_free_chain (GstQueue2Range, queue->ranges, next);
530 queue->ranges = NULL;
531 queue->current = NULL;
532 }
534 /* find a range that contains @offset or NULL when nothing does */
535 static GstQueue2Range *
536 find_range (GstQueue2 * queue, guint64 offset)
537 {
538 GstQueue2Range *range = NULL;
539 GstQueue2Range *walk;
541 /* first do a quick check for the current range */
542 for (walk = queue->ranges; walk; walk = walk->next) {
543 if (offset >= walk->offset && offset <= walk->writing_pos) {
544 /* we can reuse an existing range */
545 range = walk;
546 break;
547 }
548 }
549 if (range) {
550 GST_DEBUG_OBJECT (queue,
551 "found range for %" G_GUINT64_FORMAT ": [%" G_GUINT64_FORMAT "-%"
552 G_GUINT64_FORMAT "]", offset, range->offset, range->writing_pos);
553 } else {
554 GST_DEBUG_OBJECT (queue, "no range for %" G_GUINT64_FORMAT, offset);
555 }
556 return range;
557 }
559 static void
560 update_cur_level (GstQueue2 * queue, GstQueue2Range * range)
561 {
562 guint64 max_reading_pos, writing_pos;
564 writing_pos = range->writing_pos;
565 max_reading_pos = range->max_reading_pos;
567 if (writing_pos > max_reading_pos)
568 queue->cur_level.bytes = writing_pos - max_reading_pos;
569 else
570 queue->cur_level.bytes = 0;
571 }
573 /* make a new range for @offset or reuse an existing range */
574 static GstQueue2Range *
575 add_range (GstQueue2 * queue, guint64 offset)
576 {
577 GstQueue2Range *range, *prev, *next;
579 GST_DEBUG_OBJECT (queue, "find range for %" G_GUINT64_FORMAT, offset);
581 if ((range = find_range (queue, offset))) {
582 GST_DEBUG_OBJECT (queue,
583 "reusing range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, range->offset,
584 range->writing_pos);
585 range->writing_pos = offset;
586 } else {
587 GST_DEBUG_OBJECT (queue,
588 "new range %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, offset, offset);
590 range = g_slice_new0 (GstQueue2Range);
591 range->offset = offset;
592 /* we want to write to the next location in the ring buffer */
593 range->rb_offset = queue->current ? queue->current->rb_writing_pos : 0;
594 range->writing_pos = offset;
595 range->rb_writing_pos = range->rb_offset;
596 range->reading_pos = offset;
597 range->max_reading_pos = offset;
599 /* insert sorted */
600 prev = NULL;
601 next = queue->ranges;
602 while (next) {
603 if (next->offset > offset) {
604 /* insert before next */
605 GST_DEBUG_OBJECT (queue,
606 "insert before range %p, offset %" G_GUINT64_FORMAT, next,
607 next->offset);
608 break;
609 }
610 /* try next */
611 prev = next;
612 next = next->next;
613 }
614 range->next = next;
615 if (prev)
616 prev->next = range;
617 else
618 queue->ranges = range;
619 }
620 debug_ranges (queue);
622 /* update the stats for this range */
623 update_cur_level (queue, range);
625 return range;
626 }
629 /* clear and init the download ranges for offset 0 */
630 static void
631 init_ranges (GstQueue2 * queue)
632 {
633 GST_DEBUG_OBJECT (queue, "init queue ranges");
635 /* get rid of all the current ranges */
636 clean_ranges (queue);
637 /* make a range for offset 0 */
638 queue->current = add_range (queue, 0);
639 }
641 static gboolean
642 gst_queue2_acceptcaps (GstPad * pad, GstCaps * caps)
643 {
644 GstQueue2 *queue;
645 GstPad *otherpad;
646 gboolean result;
648 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
650 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
651 result = gst_pad_peer_accept_caps (otherpad, caps);
653 return result;
654 }
656 static GstCaps *
657 gst_queue2_getcaps (GstPad * pad)
658 {
659 GstQueue2 *queue;
660 GstPad *otherpad;
661 GstCaps *result;
663 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
664 if (G_UNLIKELY (queue == NULL))
665 return gst_caps_new_any ();
667 otherpad = (pad == queue->srcpad ? queue->sinkpad : queue->srcpad);
668 result = gst_pad_peer_get_caps (otherpad);
669 if (result == NULL)
670 result = gst_caps_new_any ();
672 gst_object_unref (queue);
674 return result;
675 }
677 static GstFlowReturn
678 gst_queue2_bufferalloc (GstPad * pad, guint64 offset, guint size,
679 GstCaps * caps, GstBuffer ** buf)
680 {
681 GstQueue2 *queue;
682 GstFlowReturn result;
684 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
686 /* Forward to src pad, without setting caps on the src pad */
687 result = gst_pad_alloc_buffer (queue->srcpad, offset, size, caps, buf);
689 return result;
690 }
692 /* calculate the diff between running time on the sink and src of the queue.
693 * This is the total amount of time in the queue. */
694 static void
695 update_time_level (GstQueue2 * queue)
696 {
697 if (queue->sink_tainted) {
698 queue->sinktime =
699 gst_segment_to_running_time (&queue->sink_segment, GST_FORMAT_TIME,
700 queue->sink_segment.last_stop);
701 queue->sink_tainted = FALSE;
702 }
704 if (queue->src_tainted) {
705 queue->srctime =
706 gst_segment_to_running_time (&queue->src_segment, GST_FORMAT_TIME,
707 queue->src_segment.last_stop);
708 queue->src_tainted = FALSE;
709 }
711 GST_DEBUG_OBJECT (queue, "sink %" GST_TIME_FORMAT ", src %" GST_TIME_FORMAT,
712 GST_TIME_ARGS (queue->sinktime), GST_TIME_ARGS (queue->srctime));
714 if (queue->sinktime != GST_CLOCK_TIME_NONE
715 && queue->srctime != GST_CLOCK_TIME_NONE
716 && queue->sinktime >= queue->srctime)
717 queue->cur_level.time = queue->sinktime - queue->srctime;
718 else
719 queue->cur_level.time = 0;
720 }
722 /* take a NEWSEGMENT event and apply the values to segment, updating the time
723 * level of queue. */
724 static void
725 apply_segment (GstQueue2 * queue, GstEvent * event, GstSegment * segment,
726 gboolean is_sink)
727 {
728 gboolean update;
729 GstFormat format;
730 gdouble rate, arate;
731 gint64 start, stop, time;
733 gst_event_parse_new_segment_full (event, &update, &rate, &arate,
734 &format, &start, &stop, &time);
736 GST_DEBUG_OBJECT (queue,
737 "received NEWSEGMENT update %d, rate %lf, applied rate %lf, "
738 "format %d, "
739 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
740 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
742 if (format == GST_FORMAT_BYTES) {
743 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
744 /* start is where we'll be getting from and as such writing next */
745 queue->current = add_range (queue, start);
746 /* update the stats for this range */
747 update_cur_level (queue, queue->current);
748 }
749 }
751 /* now configure the values, we use these to track timestamps on the
752 * sinkpad. */
753 if (format != GST_FORMAT_TIME) {
754 /* non-time format, pretent the current time segment is closed with a
755 * 0 start and unknown stop time. */
756 update = FALSE;
757 format = GST_FORMAT_TIME;
758 start = 0;
759 stop = -1;
760 time = 0;
761 }
762 gst_segment_set_newsegment_full (segment, update,
763 rate, arate, format, start, stop, time);
765 GST_DEBUG_OBJECT (queue,
766 "configured NEWSEGMENT %" GST_SEGMENT_FORMAT, segment);
768 if (is_sink)
769 queue->sink_tainted = TRUE;
770 else
771 queue->src_tainted = TRUE;
773 /* segment can update the time level of the queue */
774 update_time_level (queue);
775 }
777 /* take a buffer and update segment, updating the time level of the queue. */
778 static void
779 apply_buffer (GstQueue2 * queue, GstBuffer * buffer, GstSegment * segment,
780 gboolean is_sink)
781 {
782 GstClockTime duration, timestamp;
784 timestamp = GST_BUFFER_TIMESTAMP (buffer);
785 duration = GST_BUFFER_DURATION (buffer);
787 /* if no timestamp is set, assume it's continuous with the previous
788 * time */
789 if (timestamp == GST_CLOCK_TIME_NONE)
790 timestamp = segment->last_stop;
792 /* add duration */
793 if (duration != GST_CLOCK_TIME_NONE)
794 timestamp += duration;
796 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
797 GST_TIME_ARGS (timestamp));
799 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
801 if (is_sink)
802 queue->sink_tainted = TRUE;
803 else
804 queue->src_tainted = TRUE;
806 /* calc diff with other end */
807 update_time_level (queue);
808 }
810 static GstBufferListItem
811 buffer_list_apply_time (GstBuffer ** buf, guint group, guint idx, gpointer data)
812 {
813 GstClockTime *timestamp = data;
815 GST_TRACE ("buffer %u in group %u has ts %" GST_TIME_FORMAT
816 " duration %" GST_TIME_FORMAT, idx, group,
817 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)),
818 GST_TIME_ARGS (GST_BUFFER_DURATION (*buf)));
820 if (GST_BUFFER_TIMESTAMP_IS_VALID (*buf))
821 *timestamp = GST_BUFFER_TIMESTAMP (*buf);
823 if (GST_BUFFER_DURATION_IS_VALID (*buf))
824 *timestamp += GST_BUFFER_DURATION (*buf);
826 GST_TRACE ("ts now %" GST_TIME_FORMAT, GST_TIME_ARGS (*timestamp));
827 return GST_BUFFER_LIST_CONTINUE;
828 }
830 /* take a buffer list and update segment, updating the time level of the queue */
831 static void
832 apply_buffer_list (GstQueue2 * queue, GstBufferList * buffer_list,
833 GstSegment * segment, gboolean is_sink)
834 {
835 GstClockTime timestamp;
837 /* if no timestamp is set, assume it's continuous with the previous time */
838 timestamp = segment->last_stop;
840 gst_buffer_list_foreach (buffer_list, buffer_list_apply_time, ×tamp);
842 GST_DEBUG_OBJECT (queue, "last_stop updated to %" GST_TIME_FORMAT,
843 GST_TIME_ARGS (timestamp));
845 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, timestamp);
847 if (is_sink)
848 queue->sink_tainted = TRUE;
849 else
850 queue->src_tainted = TRUE;
852 /* calc diff with other end */
853 update_time_level (queue);
854 }
856 static void
857 update_buffering (GstQueue2 * queue)
858 {
859 gint64 percent;
860 gboolean post = FALSE;
862 if (queue->high_percent <= 0)
863 return;
865 #define GET_PERCENT(format,alt_max) ((queue->max_level.format) > 0 ? (queue->cur_level.format) * 100 / ((alt_max) > 0 ? MIN ((alt_max), (queue->max_level.format)) : (queue->max_level.format)) : 0)
867 if (queue->is_eos) {
868 /* on EOS we are always 100% full, we set the var here so that it we can
869 * reuse the logic below to stop buffering */
870 percent = 100;
871 GST_LOG_OBJECT (queue, "we are EOS");
872 } else {
873 /* figure out the percent we are filled, we take the max of all formats. */
875 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
876 percent = GET_PERCENT (bytes, 0);
877 } else {
878 guint64 rb_size = queue->ring_buffer_max_size;
879 percent = GET_PERCENT (bytes, rb_size);
880 }
881 percent = MAX (percent, GET_PERCENT (time, 0));
882 percent = MAX (percent, GET_PERCENT (buffers, 0));
884 /* also apply the rate estimate when we need to */
885 if (queue->use_rate_estimate)
886 percent = MAX (percent, GET_PERCENT (rate_time, 0));
887 }
889 if (queue->is_buffering) {
890 post = TRUE;
891 /* if we were buffering see if we reached the high watermark */
892 if (percent >= queue->high_percent)
893 queue->is_buffering = FALSE;
894 } else {
895 /* we were not buffering, check if we need to start buffering if we drop
896 * below the low threshold */
897 if (percent < queue->low_percent) {
898 queue->is_buffering = TRUE;
899 queue->buffering_iteration++;
900 post = TRUE;
901 }
902 }
903 if (post) {
904 GstMessage *message;
905 GstBufferingMode mode;
906 gint64 buffering_left = -1;
908 /* scale to high percent so that it becomes the 100% mark */
909 percent = percent * 100 / queue->high_percent;
910 /* clip */
911 if (percent > 100)
912 percent = 100;
914 if (percent != queue->buffering_percent) {
915 queue->buffering_percent = percent;
917 if (!QUEUE_IS_USING_QUEUE (queue)) {
918 GstFormat fmt = GST_FORMAT_BYTES;
919 gint64 duration;
921 if (QUEUE_IS_USING_RING_BUFFER (queue))
922 mode = GST_BUFFERING_TIMESHIFT;
923 else
924 mode = GST_BUFFERING_DOWNLOAD;
926 if (queue->byte_in_rate > 0) {
927 if (gst_pad_query_peer_duration (queue->sinkpad, &fmt, &duration))
928 buffering_left =
929 (gdouble) ((duration -
930 queue->current->writing_pos) * 1000) / queue->byte_in_rate;
931 } else {
932 buffering_left = G_MAXINT64;
933 }
934 } else {
935 mode = GST_BUFFERING_STREAM;
936 }
938 GST_DEBUG_OBJECT (queue, "buffering %d percent", (gint) percent);
939 message = gst_message_new_buffering (GST_OBJECT_CAST (queue),
940 (gint) percent);
941 gst_message_set_buffering_stats (message, mode,
942 queue->byte_in_rate, queue->byte_out_rate, buffering_left);
944 gst_element_post_message (GST_ELEMENT_CAST (queue), message);
945 }
946 } else {
947 GST_DEBUG_OBJECT (queue, "filled %d percent", (gint) percent);
948 }
950 #undef GET_PERCENT
951 }
953 static void
954 reset_rate_timer (GstQueue2 * queue)
955 {
956 queue->bytes_in = 0;
957 queue->bytes_out = 0;
958 queue->byte_in_rate = 0.0;
959 queue->byte_in_period = 0;
960 queue->byte_out_rate = 0.0;
961 queue->last_in_elapsed = 0.0;
962 queue->last_out_elapsed = 0.0;
963 queue->in_timer_started = FALSE;
964 queue->out_timer_started = FALSE;
965 }
967 /* the interval in seconds to recalculate the rate */
968 #define RATE_INTERVAL 0.2
969 /* Tuning for rate estimation. We use a large window for the input rate because
970 * it should be stable when connected to a network. The output rate is less
971 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
972 * therefore adapt more quickly.
973 * However, initial input rate may be subject to a burst, and should therefore
974 * initially also adapt more quickly to changes, and only later on give higher
975 * weight to previous values. */
976 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
977 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
979 static void
980 update_in_rates (GstQueue2 * queue)
981 {
982 gdouble elapsed, period;
983 gdouble byte_in_rate;
985 if (!queue->in_timer_started) {
986 queue->in_timer_started = TRUE;
987 g_timer_start (queue->in_timer);
988 return;
989 }
991 elapsed = g_timer_elapsed (queue->in_timer, NULL);
993 /* recalc after each interval. */
994 if (queue->last_in_elapsed + RATE_INTERVAL < elapsed) {
995 period = elapsed - queue->last_in_elapsed;
997 GST_DEBUG_OBJECT (queue,
998 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
999 period, queue->bytes_in, queue->byte_in_period);
1001 byte_in_rate = queue->bytes_in / period;
1003 if (queue->byte_in_rate == 0.0)
1004 queue->byte_in_rate = byte_in_rate;
1005 else
1006 queue->byte_in_rate = AVG_IN (queue->byte_in_rate, byte_in_rate,
1007 (double) queue->byte_in_period, period);
1009 /* another data point, cap at 16 for long time running average */
1010 if (queue->byte_in_period < 16 * RATE_INTERVAL)
1011 queue->byte_in_period += period;
1013 /* reset the values to calculate rate over the next interval */
1014 queue->last_in_elapsed = elapsed;
1015 queue->bytes_in = 0;
1016 }
1018 if (queue->byte_in_rate > 0.0) {
1019 queue->cur_level.rate_time =
1020 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1021 }
1022 GST_DEBUG_OBJECT (queue, "rates: in %f, time %" GST_TIME_FORMAT,
1023 queue->byte_in_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1024 }
1026 static void
1027 update_out_rates (GstQueue2 * queue)
1028 {
1029 gdouble elapsed, period;
1030 gdouble byte_out_rate;
1032 if (!queue->out_timer_started) {
1033 queue->out_timer_started = TRUE;
1034 g_timer_start (queue->out_timer);
1035 return;
1036 }
1038 elapsed = g_timer_elapsed (queue->out_timer, NULL);
1040 /* recalc after each interval. */
1041 if (queue->last_out_elapsed + RATE_INTERVAL < elapsed) {
1042 period = elapsed - queue->last_out_elapsed;
1044 GST_DEBUG_OBJECT (queue,
1045 "rates: period %f, out %" G_GUINT64_FORMAT, period, queue->bytes_out);
1047 byte_out_rate = queue->bytes_out / period;
1049 if (queue->byte_out_rate == 0.0)
1050 queue->byte_out_rate = byte_out_rate;
1051 else
1052 queue->byte_out_rate = AVG_OUT (queue->byte_out_rate, byte_out_rate);
1054 /* reset the values to calculate rate over the next interval */
1055 queue->last_out_elapsed = elapsed;
1056 queue->bytes_out = 0;
1057 }
1058 if (queue->byte_in_rate > 0.0) {
1059 queue->cur_level.rate_time =
1060 queue->cur_level.bytes / queue->byte_in_rate * GST_SECOND;
1061 }
1062 GST_DEBUG_OBJECT (queue, "rates: out %f, time %" GST_TIME_FORMAT,
1063 queue->byte_out_rate, GST_TIME_ARGS (queue->cur_level.rate_time));
1064 }
1066 static void
1067 update_cur_pos (GstQueue2 * queue, GstQueue2Range * range, guint64 pos)
1068 {
1069 guint64 reading_pos, max_reading_pos;
1071 reading_pos = pos;
1072 max_reading_pos = range->max_reading_pos;
1074 max_reading_pos = MAX (max_reading_pos, reading_pos);
1076 GST_DEBUG_OBJECT (queue,
1077 "updating max_reading_pos from %" G_GUINT64_FORMAT " to %"
1078 G_GUINT64_FORMAT, range->max_reading_pos, max_reading_pos);
1079 range->max_reading_pos = max_reading_pos;
1081 update_cur_level (queue, range);
1082 }
1084 static gboolean
1085 perform_seek_to_offset (GstQueue2 * queue, guint64 offset)
1086 {
1087 GstEvent *event;
1088 gboolean res;
1090 GST_QUEUE2_MUTEX_UNLOCK (queue);
1092 GST_DEBUG_OBJECT (queue, "Seeking to %" G_GUINT64_FORMAT, offset);
1094 event =
1095 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
1096 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
1097 GST_SEEK_TYPE_NONE, -1);
1099 res = gst_pad_push_event (queue->sinkpad, event);
1100 GST_QUEUE2_MUTEX_LOCK (queue);
1102 if (res)
1103 queue->current = add_range (queue, offset);
1105 return res;
1106 }
1108 /* see if there is enough data in the file to read a full buffer */
1109 static gboolean
1110 gst_queue2_have_data (GstQueue2 * queue, guint64 offset, guint length)
1111 {
1112 GstQueue2Range *range;
1114 GST_DEBUG_OBJECT (queue, "looking for offset %" G_GUINT64_FORMAT ", len %u",
1115 offset, length);
1117 if ((range = find_range (queue, offset))) {
1118 if (queue->current != range) {
1119 GST_DEBUG_OBJECT (queue, "switching ranges, do seek to range position");
1120 perform_seek_to_offset (queue, range->writing_pos);
1121 }
1123 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1124 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1126 /* we have a range for offset */
1127 GST_DEBUG_OBJECT (queue,
1128 "we have a range %p, offset %" G_GUINT64_FORMAT ", writing_pos %"
1129 G_GUINT64_FORMAT, range, range->offset, range->writing_pos);
1131 if (!QUEUE_IS_USING_RING_BUFFER (queue) && queue->is_eos)
1132 return TRUE;
1134 if (offset + length <= range->writing_pos)
1135 return TRUE;
1136 else
1137 GST_DEBUG_OBJECT (queue,
1138 "Need more data (%" G_GUINT64_FORMAT " bytes more)",
1139 (offset + length) - range->writing_pos);
1141 } else {
1142 GST_INFO_OBJECT (queue, "not found in any range");
1143 /* we don't have the range, see how far away we are, FIXME, find a good
1144 * threshold based on the incoming rate. */
1145 if (!queue->is_eos && queue->current) {
1146 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1147 if (offset < queue->current->offset || offset >
1148 queue->current->writing_pos + QUEUE_MAX_BYTES (queue) -
1149 queue->cur_level.bytes) {
1150 perform_seek_to_offset (queue, offset);
1151 } else {
1152 GST_INFO_OBJECT (queue,
1153 "requested data is within range, wait for data");
1154 }
1155 } else if (offset < queue->current->writing_pos + 200000) {
1156 update_cur_pos (queue, queue->current, offset + length);
1157 GST_INFO_OBJECT (queue, "wait for data");
1158 return FALSE;
1159 }
1160 }
1162 /* too far away, do a seek */
1163 perform_seek_to_offset (queue, offset);
1164 }
1166 return FALSE;
1167 }
1169 #ifdef HAVE_FSEEKO
1170 #define FSEEK_FILE(file,offset) (fseeko (file, (off_t) offset, SEEK_SET) != 0)
1171 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1172 #define FSEEK_FILE(file,offset) (lseek (fileno (file), (off_t) offset, SEEK_SET) == (off_t) -1)
1173 #else
1174 #define FSEEK_FILE(file,offset) (fseek (file, offset, SEEK_SET) != 0)
1175 #endif
1177 static gint64
1178 gst_queue2_read_data_at_offset (GstQueue2 * queue, guint64 offset, guint length,
1179 guint8 * dst)
1180 {
1181 guint8 *ring_buffer;
1182 size_t res;
1184 ring_buffer = queue->ring_buffer;
1186 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, offset))
1187 goto seek_failed;
1189 /* this should not block */
1190 GST_LOG_OBJECT (queue, "Reading %d bytes from offset %" G_GUINT64_FORMAT,
1191 length, offset);
1192 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1193 res = fread (dst, 1, length, queue->temp_file);
1194 } else {
1195 memcpy (dst, ring_buffer + offset, length);
1196 res = length;
1197 }
1199 GST_LOG_OBJECT (queue, "read %" G_GSIZE_FORMAT " bytes", res);
1201 if (G_UNLIKELY (res < length)) {
1202 if (!QUEUE_IS_USING_TEMP_FILE (queue))
1203 goto could_not_read;
1204 /* check for errors or EOF */
1205 if (ferror (queue->temp_file))
1206 goto could_not_read;
1207 if (feof (queue->temp_file) && length > 0)
1208 goto eos;
1209 }
1211 return res;
1213 seek_failed:
1214 {
1215 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1216 return GST_FLOW_ERROR;
1217 }
1218 could_not_read:
1219 {
1220 GST_ELEMENT_ERROR (queue, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
1221 return GST_FLOW_ERROR;
1222 }
1223 eos:
1224 {
1225 GST_DEBUG ("non-regular file hits EOS");
1226 return GST_FLOW_UNEXPECTED;
1227 }
1228 }
1230 static GstFlowReturn
1231 gst_queue2_create_read (GstQueue2 * queue, guint64 offset, guint length,
1232 GstBuffer ** buffer)
1233 {
1234 GstBuffer *buf;
1235 guint8 *data;
1236 guint64 file_offset;
1237 guint block_length, remaining, read_length;
1238 gint64 read_return;
1239 guint64 rb_size;
1240 guint64 rpos;
1242 /* allocate the output buffer of the requested size */
1243 buf = gst_buffer_new_and_alloc (length);
1244 data = GST_BUFFER_DATA (buf);
1246 GST_DEBUG_OBJECT (queue, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
1247 offset);
1249 rpos = offset;
1250 rb_size = queue->ring_buffer_max_size;
1252 remaining = length;
1253 while (remaining > 0) {
1254 /* configure how much/whether to read */
1255 if (!gst_queue2_have_data (queue, rpos, remaining)) {
1256 read_length = 0;
1258 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1259 guint64 level;
1261 /* calculate how far away the offset is */
1262 if (queue->current->writing_pos > rpos)
1263 level = queue->current->writing_pos - rpos;
1264 else
1265 level = 0;
1267 GST_DEBUG_OBJECT (queue,
1268 "reading %" G_GUINT64_FORMAT ", writing %" G_GUINT64_FORMAT
1269 ", level %" G_GUINT64_FORMAT,
1270 rpos, queue->current->writing_pos, level);
1272 if (level >= rb_size) {
1273 /* we don't have the data but if we have a ring buffer that is full, we
1274 * need to read */
1275 GST_DEBUG_OBJECT (queue,
1276 "ring buffer full, reading ring-buffer-max-size %"
1277 G_GUINT64_FORMAT " bytes", rb_size);
1278 read_length = rb_size;
1279 } else if (queue->is_eos) {
1280 /* won't get any more data so read any data we have */
1281 if (level) {
1282 GST_DEBUG_OBJECT (queue,
1283 "EOS hit but read %" G_GUINT64_FORMAT " bytes that we have",
1284 level);
1285 read_length = level;
1286 } else {
1287 GST_DEBUG_OBJECT (queue,
1288 "EOS hit and we don't have any requested data");
1289 gst_buffer_unref (buf);
1290 return GST_FLOW_UNEXPECTED;
1291 }
1292 }
1293 }
1295 if (read_length == 0) {
1296 if (QUEUE_IS_USING_RING_BUFFER (queue)
1297 && queue->current->max_reading_pos > rpos) {
1298 /* protect cached data (data between offset and max_reading_pos)
1299 * and update current level */
1300 GST_DEBUG_OBJECT (queue,
1301 "protecting cached data [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1302 "]", rpos, queue->current->max_reading_pos);
1303 queue->current->max_reading_pos = rpos;
1304 update_cur_level (queue, queue->current);
1305 }
1306 GST_DEBUG_OBJECT (queue, "waiting for add");
1307 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
1308 continue;
1309 }
1310 } else {
1311 /* we have the requested data so read it */
1312 read_length = remaining;
1313 }
1315 /* set range reading_pos to actual reading position for this read */
1316 queue->current->reading_pos = rpos;
1318 /* configure how much and from where to read */
1319 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1320 file_offset =
1321 (queue->current->rb_offset + (rpos -
1322 queue->current->offset)) % rb_size;
1323 if (file_offset + read_length > rb_size) {
1324 block_length = rb_size - file_offset;
1325 } else {
1326 block_length = read_length;
1327 }
1328 } else {
1329 file_offset = rpos;
1330 block_length = read_length;
1331 }
1333 /* while we still have data to read, we loop */
1334 while (read_length > 0) {
1335 read_return =
1336 gst_queue2_read_data_at_offset (queue, file_offset, block_length,
1337 data);
1338 if (read_return < 0)
1339 goto read_error;
1341 file_offset += read_return;
1342 if (QUEUE_IS_USING_RING_BUFFER (queue))
1343 file_offset %= rb_size;
1345 data += read_return;
1346 read_length -= read_return;
1347 block_length = read_length;
1348 remaining -= read_return;
1350 rpos = (queue->current->reading_pos += read_return);
1351 update_cur_pos (queue, queue->current, queue->current->reading_pos);
1352 }
1353 GST_QUEUE2_SIGNAL_DEL (queue);
1354 GST_DEBUG_OBJECT (queue, "%u bytes left to read", remaining);
1355 }
1357 GST_BUFFER_SIZE (buf) = length;
1358 GST_BUFFER_OFFSET (buf) = offset;
1359 GST_BUFFER_OFFSET_END (buf) = offset + length;
1361 *buffer = buf;
1363 return GST_FLOW_OK;
1365 /* ERRORS */
1366 out_flushing:
1367 {
1368 GST_DEBUG_OBJECT (queue, "we are flushing");
1369 gst_buffer_unref (buf);
1370 return GST_FLOW_WRONG_STATE;
1371 }
1372 read_error:
1373 {
1374 GST_DEBUG_OBJECT (queue, "we have a read error");
1375 gst_buffer_unref (buf);
1376 return read_return;
1377 }
1378 }
1380 /* should be called with QUEUE_LOCK */
1381 static GstMiniObject *
1382 gst_queue2_read_item_from_file (GstQueue2 * queue)
1383 {
1384 GstMiniObject *item;
1386 if (queue->starting_segment != NULL) {
1387 item = GST_MINI_OBJECT_CAST (queue->starting_segment);
1388 queue->starting_segment = NULL;
1389 } else {
1390 GstFlowReturn ret;
1391 GstBuffer *buffer;
1392 guint64 reading_pos;
1394 reading_pos = queue->current->reading_pos;
1396 ret =
1397 gst_queue2_create_read (queue, reading_pos, DEFAULT_BUFFER_SIZE,
1398 &buffer);
1400 switch (ret) {
1401 case GST_FLOW_OK:
1402 item = GST_MINI_OBJECT_CAST (buffer);
1403 break;
1404 case GST_FLOW_UNEXPECTED:
1405 item = GST_MINI_OBJECT_CAST (gst_event_new_eos ());
1406 break;
1407 default:
1408 item = NULL;
1409 break;
1410 }
1411 }
1412 return item;
1413 }
1415 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
1416 * the temp filename. */
1417 static gboolean
1418 gst_queue2_open_temp_location_file (GstQueue2 * queue)
1419 {
1420 gint fd = -1;
1421 gchar *name = NULL;
1423 if (queue->temp_file)
1424 goto already_opened;
1426 GST_DEBUG_OBJECT (queue, "opening temp file %s", queue->temp_template);
1428 /* we have two cases:
1429 * - temp_location was set to something !NULL (Deprecated). in this case we
1430 * open the specified filename.
1431 * - temp_template was set, allocate a filename and open that filename
1432 */
1433 if (!queue->temp_location_set) {
1434 /* nothing to do */
1435 if (queue->temp_template == NULL)
1436 goto no_directory;
1438 /* make copy of the template, we don't want to change this */
1439 name = g_strdup (queue->temp_template);
1440 fd = g_mkstemp (name);
1441 if (fd == -1)
1442 goto mkstemp_failed;
1444 /* open the file for update/writing */
1445 queue->temp_file = fdopen (fd, "wb+");
1446 /* error creating file */
1447 if (queue->temp_file == NULL)
1448 goto open_failed;
1450 g_free (queue->temp_location);
1451 queue->temp_location = name;
1453 GST_QUEUE2_MUTEX_UNLOCK (queue);
1455 /* we can't emit the notify with the lock */
1456 g_object_notify (G_OBJECT (queue), "temp-location");
1458 GST_QUEUE2_MUTEX_LOCK (queue);
1459 } else {
1460 /* open the file for update/writing, this is deprecated but we still need to
1461 * support it for API/ABI compatibility */
1462 queue->temp_file = g_fopen (queue->temp_location, "wb+");
1463 /* error creating file */
1464 if (queue->temp_file == NULL)
1465 goto open_failed;
1466 }
1467 GST_DEBUG_OBJECT (queue, "opened temp file %s", queue->temp_template);
1469 return TRUE;
1471 /* ERRORS */
1472 already_opened:
1473 {
1474 GST_DEBUG_OBJECT (queue, "temp file was already open");
1475 return TRUE;
1476 }
1477 no_directory:
1478 {
1479 GST_ELEMENT_ERROR (queue, RESOURCE, NOT_FOUND,
1480 (_("No Temp directory specified.")), (NULL));
1481 return FALSE;
1482 }
1483 mkstemp_failed:
1484 {
1485 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1486 (_("Could not create temp file \"%s\"."), queue->temp_template),
1487 GST_ERROR_SYSTEM);
1488 g_free (name);
1489 return FALSE;
1490 }
1491 open_failed:
1492 {
1493 GST_ELEMENT_ERROR (queue, RESOURCE, OPEN_READ,
1494 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
1495 g_free (name);
1496 if (fd != -1)
1497 close (fd);
1498 return FALSE;
1499 }
1500 }
1502 static void
1503 gst_queue2_close_temp_location_file (GstQueue2 * queue)
1504 {
1505 /* nothing to do */
1506 if (queue->temp_file == NULL)
1507 return;
1509 GST_DEBUG_OBJECT (queue, "closing temp file");
1511 fflush (queue->temp_file);
1512 fclose (queue->temp_file);
1514 if (queue->temp_remove)
1515 remove (queue->temp_location);
1517 queue->temp_file = NULL;
1518 clean_ranges (queue);
1519 }
1521 static void
1522 gst_queue2_flush_temp_file (GstQueue2 * queue)
1523 {
1524 if (queue->temp_file == NULL)
1525 return;
1527 GST_DEBUG_OBJECT (queue, "flushing temp file");
1529 queue->temp_file = g_freopen (queue->temp_location, "wb+", queue->temp_file);
1530 }
1532 static void
1533 gst_queue2_locked_flush (GstQueue2 * queue)
1534 {
1535 if (!QUEUE_IS_USING_QUEUE (queue)) {
1536 if (QUEUE_IS_USING_TEMP_FILE (queue))
1537 gst_queue2_flush_temp_file (queue);
1538 init_ranges (queue);
1539 } else {
1540 while (!g_queue_is_empty (&queue->queue)) {
1541 GstMiniObject *data = g_queue_pop_head (&queue->queue);
1543 /* Then lose another reference because we are supposed to destroy that
1544 data when flushing */
1545 gst_mini_object_unref (data);
1546 }
1547 }
1548 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
1549 gst_segment_init (&queue->sink_segment, GST_FORMAT_TIME);
1550 gst_segment_init (&queue->src_segment, GST_FORMAT_TIME);
1551 queue->sinktime = queue->srctime = GST_CLOCK_TIME_NONE;
1552 queue->sink_tainted = queue->src_tainted = TRUE;
1553 if (queue->starting_segment != NULL)
1554 gst_event_unref (queue->starting_segment);
1555 queue->starting_segment = NULL;
1556 queue->segment_event_received = FALSE;
1558 /* we deleted a lot of something */
1559 GST_QUEUE2_SIGNAL_DEL (queue);
1560 }
1562 static gboolean
1563 gst_queue2_wait_free_space (GstQueue2 * queue)
1564 {
1565 /* We make space available if we're "full" according to whatever
1566 * the user defined as "full". */
1567 if (gst_queue2_is_filled (queue)) {
1568 gboolean started;
1570 /* pause the timer while we wait. The fact that we are waiting does not mean
1571 * the byterate on the input pad is lower */
1572 if ((started = queue->in_timer_started))
1573 g_timer_stop (queue->in_timer);
1575 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
1576 "queue is full, waiting for free space");
1577 do {
1578 /* Wait for space to be available, we could be unlocked because of a flush. */
1579 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1580 }
1581 while (gst_queue2_is_filled (queue));
1583 /* and continue if we were running before */
1584 if (started)
1585 g_timer_continue (queue->in_timer);
1586 }
1587 return TRUE;
1589 /* ERRORS */
1590 out_flushing:
1591 {
1592 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "queue is flushing");
1593 return FALSE;
1594 }
1595 }
1597 static gboolean
1598 gst_queue2_create_write (GstQueue2 * queue, GstBuffer * buffer)
1599 {
1600 guint8 *data, *ring_buffer;
1601 guint size, rb_size;
1602 guint64 writing_pos, new_writing_pos;
1603 GstQueue2Range *range, *prev, *next;
1605 if (QUEUE_IS_USING_RING_BUFFER (queue))
1606 writing_pos = queue->current->rb_writing_pos;
1607 else
1608 writing_pos = queue->current->writing_pos;
1609 ring_buffer = queue->ring_buffer;
1610 rb_size = queue->ring_buffer_max_size;
1612 size = GST_BUFFER_SIZE (buffer);
1613 data = GST_BUFFER_DATA (buffer);
1615 GST_DEBUG_OBJECT (queue, "Writing %u bytes to %" G_GUINT64_FORMAT, size,
1616 GST_BUFFER_OFFSET (buffer));
1618 while (size > 0) {
1619 guint to_write;
1621 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1622 gint64 space;
1624 /* calculate the space in the ring buffer not used by data from
1625 * the current range */
1626 while (QUEUE_MAX_BYTES (queue) <= queue->cur_level.bytes) {
1627 /* wait until there is some free space */
1628 GST_QUEUE2_WAIT_DEL_CHECK (queue, queue->sinkresult, out_flushing);
1629 }
1630 /* get the amount of space we have */
1631 space = QUEUE_MAX_BYTES (queue) - queue->cur_level.bytes;
1633 /* calculate if we need to split or if we can write the entire
1634 * buffer now */
1635 to_write = MIN (size, space);
1637 /* the writing position in the ring buffer after writing (part
1638 * or all of) the buffer */
1639 new_writing_pos = (writing_pos + to_write) % rb_size;
1641 prev = NULL;
1642 range = queue->ranges;
1644 /* if we need to overwrite data in the ring buffer, we need to
1645 * update the ranges
1646 *
1647 * warning: this code is complicated and includes some
1648 * simplifications - pen, paper and diagrams for the cases
1649 * recommended! */
1650 while (range) {
1651 guint64 range_data_start, range_data_end;
1652 GstQueue2Range *range_to_destroy = NULL;
1654 range_data_start = range->rb_offset;
1655 range_data_end = range->rb_writing_pos;
1657 /* handle the special case where the range has no data in it */
1658 if (range->writing_pos == range->offset) {
1659 if (range != queue->current) {
1660 GST_DEBUG_OBJECT (queue,
1661 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1662 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1663 /* remove range */
1664 range_to_destroy = range;
1665 if (prev)
1666 prev->next = range->next;
1667 }
1668 goto next_range;
1669 }
1671 if (range_data_end > range_data_start) {
1672 if (writing_pos >= range_data_end && new_writing_pos >= writing_pos)
1673 goto next_range;
1675 if (new_writing_pos > range_data_start) {
1676 if (new_writing_pos >= range_data_end) {
1677 GST_DEBUG_OBJECT (queue,
1678 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1679 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1680 /* remove range */
1681 range_to_destroy = range;
1682 if (prev)
1683 prev->next = range->next;
1684 } else {
1685 GST_DEBUG_OBJECT (queue,
1686 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1687 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1688 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1689 range->offset + new_writing_pos - range_data_start,
1690 new_writing_pos);
1691 range->offset += (new_writing_pos - range_data_start);
1692 range->rb_offset = new_writing_pos;
1693 }
1694 }
1695 } else {
1696 guint64 new_wpos_virt = writing_pos + to_write;
1698 if (new_wpos_virt <= range_data_start)
1699 goto next_range;
1701 if (new_wpos_virt > rb_size && new_writing_pos >= range_data_end) {
1702 GST_DEBUG_OBJECT (queue,
1703 "Removing range: offset %" G_GUINT64_FORMAT ", wpos %"
1704 G_GUINT64_FORMAT, range->offset, range->writing_pos);
1705 /* remove range */
1706 range_to_destroy = range;
1707 if (prev)
1708 prev->next = range->next;
1709 } else {
1710 GST_DEBUG_OBJECT (queue,
1711 "advancing offsets from %" G_GUINT64_FORMAT " (%"
1712 G_GUINT64_FORMAT ") to %" G_GUINT64_FORMAT " (%"
1713 G_GUINT64_FORMAT ")", range->offset, range->rb_offset,
1714 range->offset + new_writing_pos - range_data_start,
1715 new_writing_pos);
1716 range->offset += (new_wpos_virt - range_data_start);
1717 range->rb_offset = new_writing_pos;
1718 }
1719 }
1721 next_range:
1722 if (!range_to_destroy)
1723 prev = range;
1725 range = range->next;
1726 if (range_to_destroy) {
1727 if (range_to_destroy == queue->ranges)
1728 queue->ranges = range;
1729 g_slice_free (GstQueue2Range, range_to_destroy);
1730 range_to_destroy = NULL;
1731 }
1732 }
1733 } else {
1734 to_write = size;
1735 new_writing_pos = writing_pos + to_write;
1736 }
1738 if (QUEUE_IS_USING_TEMP_FILE (queue)
1739 && FSEEK_FILE (queue->temp_file, writing_pos))
1740 goto seek_failed;
1742 if (new_writing_pos > writing_pos) {
1743 GST_INFO_OBJECT (queue,
1744 "writing %u bytes to range [%" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT
1745 "] (rb wpos %" G_GUINT64_FORMAT ")", to_write, queue->current->offset,
1746 queue->current->writing_pos, queue->current->rb_writing_pos);
1747 /* either not using ring buffer or no wrapping, just write */
1748 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1749 if (fwrite (data, to_write, 1, queue->temp_file) != 1)
1750 goto handle_error;
1751 } else {
1752 memcpy (ring_buffer + writing_pos, data, to_write);
1753 }
1755 if (!QUEUE_IS_USING_RING_BUFFER (queue)) {
1756 /* try to merge with next range */
1757 while ((next = queue->current->next)) {
1758 GST_INFO_OBJECT (queue,
1759 "checking merge with next range %" G_GUINT64_FORMAT " < %"
1760 G_GUINT64_FORMAT, new_writing_pos, next->offset);
1761 if (new_writing_pos < next->offset)
1762 break;
1764 GST_DEBUG_OBJECT (queue, "merging ranges %" G_GUINT64_FORMAT,
1765 next->writing_pos);
1767 /* remove the group, we could choose to not read the data in this range
1768 * again. This would involve us doing a seek to the current writing position
1769 * in the range. FIXME, It would probably make sense to do a seek when there
1770 * is a lot of data in the range we merged with to avoid reading it all
1771 * again. */
1772 queue->current->next = next->next;
1773 g_slice_free (GstQueue2Range, next);
1775 debug_ranges (queue);
1776 }
1777 goto update_and_signal;
1778 }
1779 } else {
1780 /* wrapping */
1781 guint block_one, block_two;
1783 block_one = rb_size - writing_pos;
1784 block_two = to_write - block_one;
1786 if (block_one > 0) {
1787 GST_INFO_OBJECT (queue, "writing %u bytes", block_one);
1788 /* write data to end of ring buffer */
1789 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1790 if (fwrite (data, block_one, 1, queue->temp_file) != 1)
1791 goto handle_error;
1792 } else {
1793 memcpy (ring_buffer + writing_pos, data, block_one);
1794 }
1795 }
1797 if (QUEUE_IS_USING_TEMP_FILE (queue) && FSEEK_FILE (queue->temp_file, 0))
1798 goto seek_failed;
1800 if (block_two > 0) {
1801 GST_INFO_OBJECT (queue, "writing %u bytes", block_two);
1802 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
1803 if (fwrite (data + block_one, block_two, 1, queue->temp_file) != 1)
1804 goto handle_error;
1805 } else {
1806 memcpy (ring_buffer, data + block_one, block_two);
1807 }
1808 }
1809 }
1811 update_and_signal:
1812 /* update the writing positions */
1813 size -= to_write;
1814 GST_INFO_OBJECT (queue,
1815 "wrote %u bytes to %" G_GUINT64_FORMAT " (%u bytes remaining to write)",
1816 to_write, writing_pos, size);
1818 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
1819 data += to_write;
1820 queue->current->writing_pos += to_write;
1821 queue->current->rb_writing_pos = writing_pos = new_writing_pos;
1822 } else {
1823 queue->current->writing_pos = writing_pos = new_writing_pos;
1824 }
1825 update_cur_level (queue, queue->current);
1827 /* update the buffering status */
1828 if (queue->use_buffering)
1829 update_buffering (queue);
1831 GST_INFO_OBJECT (queue, "cur_level.bytes %u (max %" G_GUINT64_FORMAT ")",
1832 queue->cur_level.bytes, QUEUE_MAX_BYTES (queue));
1834 GST_QUEUE2_SIGNAL_ADD (queue);
1835 };
1837 return TRUE;
1839 /* ERRORS */
1840 out_flushing:
1841 {
1842 GST_DEBUG_OBJECT (queue, "we are flushing");
1843 /* FIXME - GST_FLOW_UNEXPECTED ? */
1844 return FALSE;
1845 }
1846 seek_failed:
1847 {
1848 GST_ELEMENT_ERROR (queue, RESOURCE, SEEK, (NULL), GST_ERROR_SYSTEM);
1849 return FALSE;
1850 }
1851 handle_error:
1852 {
1853 switch (errno) {
1854 case ENOSPC:{
1855 GST_ELEMENT_ERROR (queue, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
1856 break;
1857 }
1858 default:{
1859 GST_ELEMENT_ERROR (queue, RESOURCE, WRITE,
1860 (_("Error while writing to download file.")),
1861 ("%s", g_strerror (errno)));
1862 }
1863 }
1864 return FALSE;
1865 }
1866 }
1868 static GstBufferListItem
1869 buffer_list_create_write (GstBuffer ** buf, guint group, guint idx, gpointer q)
1870 {
1871 GstQueue2 *queue = q;
1873 GST_TRACE_OBJECT (queue, "writing buffer %u in group %u of size %u bytes",
1874 idx, group, GST_BUFFER_SIZE (*buf));
1876 if (!gst_queue2_create_write (queue, *buf)) {
1877 GST_INFO_OBJECT (queue, "create_write() returned FALSE, bailing out");
1878 return GST_BUFFER_LIST_END;
1879 }
1881 return GST_BUFFER_LIST_CONTINUE;
1882 }
1884 static GstBufferListItem
1885 buffer_list_calc_size (GstBuffer ** buf, guint group, guint idx, gpointer data)
1886 {
1887 guint *p_size = data;
1888 guint buf_size;
1890 buf_size = GST_BUFFER_SIZE (*buf);
1891 GST_TRACE ("buffer %u in group %u has size %u", idx, group, buf_size);
1892 *p_size += buf_size;
1894 return GST_BUFFER_LIST_CONTINUE;
1895 }
1897 /* enqueue an item an update the level stats */
1898 static void
1899 gst_queue2_locked_enqueue (GstQueue2 * queue, gpointer item,
1900 GstQueue2ItemType item_type)
1901 {
1902 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
1903 GstBuffer *buffer;
1904 guint size;
1906 buffer = GST_BUFFER_CAST (item);
1907 size = GST_BUFFER_SIZE (buffer);
1909 /* add buffer to the statistics */
1910 if (QUEUE_IS_USING_QUEUE (queue)) {
1911 queue->cur_level.buffers++;
1912 queue->cur_level.bytes += size;
1913 }
1914 queue->bytes_in += size;
1916 /* apply new buffer to segment stats */
1917 apply_buffer (queue, buffer, &queue->sink_segment, TRUE);
1918 /* update the byterate stats */
1919 update_in_rates (queue);
1921 if (!QUEUE_IS_USING_QUEUE (queue)) {
1922 /* FIXME - check return value? */
1923 gst_queue2_create_write (queue, buffer);
1924 }
1925 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
1926 GstBufferList *buffer_list;
1927 guint size = 0;
1929 buffer_list = GST_BUFFER_LIST_CAST (item);
1931 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
1932 GST_LOG_OBJECT (queue, "total size of buffer list: %u bytes", size);
1934 /* add buffer to the statistics */
1935 if (QUEUE_IS_USING_QUEUE (queue)) {
1936 queue->cur_level.buffers++;
1937 queue->cur_level.bytes += size;
1938 }
1939 queue->bytes_in += size;
1941 /* apply new buffer to segment stats */
1942 apply_buffer_list (queue, buffer_list, &queue->sink_segment, TRUE);
1944 /* update the byterate stats */
1945 update_in_rates (queue);
1947 if (!QUEUE_IS_USING_QUEUE (queue)) {
1948 gst_buffer_list_foreach (buffer_list, buffer_list_create_write, queue);
1949 }
1950 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
1951 GstEvent *event;
1953 event = GST_EVENT_CAST (item);
1955 switch (GST_EVENT_TYPE (event)) {
1956 case GST_EVENT_EOS:
1957 /* Zero the thresholds, this makes sure the queue is completely
1958 * filled and we can read all data from the queue. */
1959 GST_DEBUG_OBJECT (queue, "we have EOS");
1960 queue->is_eos = TRUE;
1961 break;
1962 case GST_EVENT_NEWSEGMENT:
1963 apply_segment (queue, event, &queue->sink_segment, TRUE);
1964 /* This is our first new segment, we hold it
1965 * as we can't save it on the temp file */
1966 if (!QUEUE_IS_USING_QUEUE (queue)) {
1967 if (queue->segment_event_received)
1968 goto unexpected_event;
1970 queue->segment_event_received = TRUE;
1971 if (queue->starting_segment != NULL)
1972 gst_event_unref (queue->starting_segment);
1973 queue->starting_segment = event;
1974 item = NULL;
1975 }
1976 /* a new segment allows us to accept more buffers if we got UNEXPECTED
1977 * from downstream */
1978 queue->unexpected = FALSE;
1979 break;
1980 default:
1981 if (!QUEUE_IS_USING_QUEUE (queue))
1982 goto unexpected_event;
1983 break;
1984 }
1985 } else {
1986 g_warning ("Unexpected item %p added in queue %s (refcounting problem?)",
1987 item, GST_OBJECT_NAME (queue));
1988 /* we can't really unref since we don't know what it is */
1989 item = NULL;
1990 }
1992 if (item) {
1993 /* update the buffering status */
1994 if (queue->use_buffering)
1995 update_buffering (queue);
1997 if (QUEUE_IS_USING_QUEUE (queue)) {
1998 g_queue_push_tail (&queue->queue, item);
1999 } else {
2000 gst_mini_object_unref (GST_MINI_OBJECT_CAST (item));
2001 }
2003 GST_QUEUE2_SIGNAL_ADD (queue);
2004 }
2006 return;
2008 /* ERRORS */
2009 unexpected_event:
2010 {
2011 g_warning
2012 ("Unexpected event of kind %s can't be added in temp file of queue %s ",
2013 gst_event_type_get_name (GST_EVENT_TYPE (item)),
2014 GST_OBJECT_NAME (queue));
2015 gst_event_unref (GST_EVENT_CAST (item));
2016 return;
2017 }
2018 }
2020 /* dequeue an item from the queue and update level stats */
2021 static GstMiniObject *
2022 gst_queue2_locked_dequeue (GstQueue2 * queue, GstQueue2ItemType * item_type)
2023 {
2024 GstMiniObject *item;
2026 if (!QUEUE_IS_USING_QUEUE (queue))
2027 item = gst_queue2_read_item_from_file (queue);
2028 else
2029 item = g_queue_pop_head (&queue->queue);
2031 if (item == NULL)
2032 goto no_item;
2034 if (GST_IS_BUFFER (item)) {
2035 GstBuffer *buffer;
2036 guint size;
2038 buffer = GST_BUFFER_CAST (item);
2039 size = GST_BUFFER_SIZE (buffer);
2040 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER;
2042 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2043 "retrieved buffer %p from queue", buffer);
2045 if (QUEUE_IS_USING_QUEUE (queue)) {
2046 queue->cur_level.buffers--;
2047 queue->cur_level.bytes -= size;
2048 }
2049 queue->bytes_out += size;
2051 apply_buffer (queue, buffer, &queue->src_segment, FALSE);
2052 /* update the byterate stats */
2053 update_out_rates (queue);
2054 /* update the buffering */
2055 if (queue->use_buffering)
2056 update_buffering (queue);
2058 } else if (GST_IS_EVENT (item)) {
2059 GstEvent *event = GST_EVENT_CAST (item);
2061 *item_type = GST_QUEUE2_ITEM_TYPE_EVENT;
2063 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2064 "retrieved event %p from queue", event);
2066 switch (GST_EVENT_TYPE (event)) {
2067 case GST_EVENT_EOS:
2068 /* queue is empty now that we dequeued the EOS */
2069 GST_QUEUE2_CLEAR_LEVEL (queue->cur_level);
2070 break;
2071 case GST_EVENT_NEWSEGMENT:
2072 apply_segment (queue, event, &queue->src_segment, FALSE);
2073 break;
2074 default:
2075 break;
2076 }
2077 } else if (GST_IS_BUFFER_LIST (item)) {
2078 GstBufferList *buffer_list;
2079 guint size = 0;
2081 buffer_list = GST_BUFFER_LIST_CAST (item);
2082 gst_buffer_list_foreach (buffer_list, buffer_list_calc_size, &size);
2083 *item_type = GST_QUEUE2_ITEM_TYPE_BUFFER_LIST;
2085 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2086 "retrieved buffer list %p from queue", buffer_list);
2088 if (QUEUE_IS_USING_QUEUE (queue)) {
2089 queue->cur_level.buffers--;
2090 queue->cur_level.bytes -= size;
2091 }
2092 queue->bytes_out += size;
2094 apply_buffer_list (queue, buffer_list, &queue->src_segment, FALSE);
2095 /* update the byterate stats */
2096 update_out_rates (queue);
2097 /* update the buffering */
2098 if (queue->use_buffering)
2099 update_buffering (queue);
2101 } else {
2102 g_warning
2103 ("Unexpected item %p dequeued from queue %s (refcounting problem?)",
2104 item, GST_OBJECT_NAME (queue));
2105 item = NULL;
2106 *item_type = GST_QUEUE2_ITEM_TYPE_UNKNOWN;
2107 }
2108 GST_QUEUE2_SIGNAL_DEL (queue);
2110 return item;
2112 /* ERRORS */
2113 no_item:
2114 {
2115 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "the queue is empty");
2116 return NULL;
2117 }
2118 }
2120 static gboolean
2121 gst_queue2_handle_sink_event (GstPad * pad, GstEvent * event)
2122 {
2123 GstQueue2 *queue;
2125 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2127 switch (GST_EVENT_TYPE (event)) {
2128 case GST_EVENT_FLUSH_START:
2129 {
2130 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush start event");
2131 if (QUEUE_IS_USING_QUEUE (queue)) {
2132 /* forward event */
2133 gst_pad_push_event (queue->srcpad, event);
2135 /* now unblock the chain function */
2136 GST_QUEUE2_MUTEX_LOCK (queue);
2137 queue->srcresult = GST_FLOW_WRONG_STATE;
2138 queue->sinkresult = GST_FLOW_WRONG_STATE;
2139 /* unblock the loop and chain functions */
2140 GST_QUEUE2_SIGNAL_ADD (queue);
2141 GST_QUEUE2_SIGNAL_DEL (queue);
2142 GST_QUEUE2_MUTEX_UNLOCK (queue);
2144 /* make sure it pauses, this should happen since we sent
2145 * flush_start downstream. */
2146 gst_pad_pause_task (queue->srcpad);
2147 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "loop stopped");
2148 } else {
2149 GST_QUEUE2_MUTEX_LOCK (queue);
2150 /* flush the sink pad */
2151 queue->sinkresult = GST_FLOW_WRONG_STATE;
2152 GST_QUEUE2_SIGNAL_DEL (queue);
2153 GST_QUEUE2_MUTEX_UNLOCK (queue);
2155 gst_event_unref (event);
2156 }
2157 goto done;
2158 }
2159 case GST_EVENT_FLUSH_STOP:
2160 {
2161 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "received flush stop event");
2163 if (QUEUE_IS_USING_QUEUE (queue)) {
2164 /* forward event */
2165 gst_pad_push_event (queue->srcpad, event);
2167 GST_QUEUE2_MUTEX_LOCK (queue);
2168 gst_queue2_locked_flush (queue);
2169 queue->srcresult = GST_FLOW_OK;
2170 queue->sinkresult = GST_FLOW_OK;
2171 queue->is_eos = FALSE;
2172 queue->unexpected = FALSE;
2173 /* reset rate counters */
2174 reset_rate_timer (queue);
2175 gst_pad_start_task (queue->srcpad, (GstTaskFunction) gst_queue2_loop,
2176 queue->srcpad);
2177 GST_QUEUE2_MUTEX_UNLOCK (queue);
2178 } else {
2179 GST_QUEUE2_MUTEX_LOCK (queue);
2180 queue->segment_event_received = FALSE;
2181 queue->is_eos = FALSE;
2182 queue->unexpected = FALSE;
2183 queue->sinkresult = GST_FLOW_OK;
2184 GST_QUEUE2_MUTEX_UNLOCK (queue);
2186 gst_event_unref (event);
2187 }
2188 goto done;
2189 }
2190 default:
2191 if (GST_EVENT_IS_SERIALIZED (event)) {
2192 /* serialized events go in the queue */
2193 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2194 /* refuse more events on EOS */
2195 if (queue->is_eos)
2196 goto out_eos;
2197 gst_queue2_locked_enqueue (queue, event, GST_QUEUE2_ITEM_TYPE_EVENT);
2198 GST_QUEUE2_MUTEX_UNLOCK (queue);
2199 } else {
2200 /* non-serialized events are passed upstream. */
2201 gst_pad_push_event (queue->srcpad, event);
2202 }
2203 break;
2204 }
2205 done:
2206 return TRUE;
2208 /* ERRORS */
2209 out_flushing:
2210 {
2211 GST_DEBUG_OBJECT (queue, "refusing event, we are flushing");
2212 GST_QUEUE2_MUTEX_UNLOCK (queue);
2213 gst_event_unref (event);
2214 return FALSE;
2215 }
2216 out_eos:
2217 {
2218 GST_DEBUG_OBJECT (queue, "refusing event, we are EOS");
2219 GST_QUEUE2_MUTEX_UNLOCK (queue);
2220 gst_event_unref (event);
2221 return FALSE;
2222 }
2223 }
2225 static gboolean
2226 gst_queue2_is_empty (GstQueue2 * queue)
2227 {
2228 /* never empty on EOS */
2229 if (queue->is_eos)
2230 return FALSE;
2232 if (!QUEUE_IS_USING_QUEUE (queue) && queue->current) {
2233 return queue->current->writing_pos <= queue->current->max_reading_pos;
2234 } else {
2235 if (queue->queue.length == 0)
2236 return TRUE;
2237 }
2239 return FALSE;
2240 }
2242 static gboolean
2243 gst_queue2_is_filled (GstQueue2 * queue)
2244 {
2245 gboolean res;
2247 /* always filled on EOS */
2248 if (queue->is_eos)
2249 return TRUE;
2251 #define CHECK_FILLED(format,alt_max) ((queue->max_level.format) > 0 && \
2252 (queue->cur_level.format) >= ((alt_max) ? \
2253 MIN ((queue->max_level.format), (alt_max)) : (queue->max_level.format)))
2255 /* if using a ring buffer we're filled if all ring buffer space is used
2256 * _by the current range_ */
2257 if (QUEUE_IS_USING_RING_BUFFER (queue)) {
2258 guint64 rb_size = queue->ring_buffer_max_size;
2259 GST_DEBUG_OBJECT (queue,
2260 "max bytes %u, rb size %" G_GUINT64_FORMAT ", cur bytes %u",
2261 queue->max_level.bytes, rb_size, queue->cur_level.bytes);
2262 return CHECK_FILLED (bytes, rb_size);
2263 }
2265 /* if using file, we're never filled if we don't have EOS */
2266 if (QUEUE_IS_USING_TEMP_FILE (queue))
2267 return FALSE;
2269 /* we are never filled when we have no buffers at all */
2270 if (queue->cur_level.buffers == 0)
2271 return FALSE;
2273 /* we are filled if one of the current levels exceeds the max */
2274 res = CHECK_FILLED (buffers, 0) || CHECK_FILLED (bytes, 0)
2275 || CHECK_FILLED (time, 0);
2277 /* if we need to, use the rate estimate to check against the max time we are
2278 * allowed to queue */
2279 if (queue->use_rate_estimate)
2280 res |= CHECK_FILLED (rate_time, 0);
2282 #undef CHECK_FILLED
2283 return res;
2284 }
2286 static GstFlowReturn
2287 gst_queue2_chain_buffer_or_buffer_list (GstQueue2 * queue,
2288 GstMiniObject * item, GstQueue2ItemType item_type)
2289 {
2290 /* we have to lock the queue since we span threads */
2291 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->sinkresult, out_flushing);
2292 /* when we received EOS, we refuse more data */
2293 if (queue->is_eos)
2294 goto out_eos;
2295 /* when we received unexpected from downstream, refuse more buffers */
2296 if (queue->unexpected)
2297 goto out_unexpected;
2299 if (!gst_queue2_wait_free_space (queue))
2300 goto out_flushing;
2302 /* put buffer in queue now */
2303 gst_queue2_locked_enqueue (queue, item, item_type);
2304 GST_QUEUE2_MUTEX_UNLOCK (queue);
2306 return GST_FLOW_OK;
2308 /* special conditions */
2309 out_flushing:
2310 {
2311 GstFlowReturn ret = queue->sinkresult;
2313 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2314 "exit because task paused, reason: %s", gst_flow_get_name (ret));
2315 GST_QUEUE2_MUTEX_UNLOCK (queue);
2316 gst_mini_object_unref (item);
2318 return ret;
2319 }
2320 out_eos:
2321 {
2322 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we received EOS");
2323 GST_QUEUE2_MUTEX_UNLOCK (queue);
2324 gst_mini_object_unref (item);
2326 return GST_FLOW_UNEXPECTED;
2327 }
2328 out_unexpected:
2329 {
2330 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2331 "exit because we received UNEXPECTED");
2332 GST_QUEUE2_MUTEX_UNLOCK (queue);
2333 gst_mini_object_unref (item);
2335 return GST_FLOW_UNEXPECTED;
2336 }
2337 }
2339 static GstFlowReturn
2340 gst_queue2_chain (GstPad * pad, GstBuffer * buffer)
2341 {
2342 GstQueue2 *queue;
2344 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2346 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2347 "received buffer %p of size %d, time %" GST_TIME_FORMAT ", duration %"
2348 GST_TIME_FORMAT, buffer, GST_BUFFER_SIZE (buffer),
2349 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
2350 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2352 return gst_queue2_chain_buffer_or_buffer_list (queue,
2353 GST_MINI_OBJECT_CAST (buffer), GST_QUEUE2_ITEM_TYPE_BUFFER);
2354 }
2356 static GstFlowReturn
2357 gst_queue2_chain_list (GstPad * pad, GstBufferList * buffer_list)
2358 {
2359 GstQueue2 *queue;
2361 queue = GST_QUEUE2 (GST_OBJECT_PARENT (pad));
2363 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2364 "received buffer list %p", buffer_list);
2366 return gst_queue2_chain_buffer_or_buffer_list (queue,
2367 GST_MINI_OBJECT_CAST (buffer_list), GST_QUEUE2_ITEM_TYPE_BUFFER_LIST);
2368 }
2370 static GstMiniObject *
2371 gst_queue2_dequeue_on_unexpected (GstQueue2 * queue,
2372 GstQueue2ItemType * item_type)
2373 {
2374 GstMiniObject *data;
2376 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "got UNEXPECTED from downstream");
2378 /* stop pushing buffers, we dequeue all items until we see an item that we
2379 * can push again, which is EOS or NEWSEGMENT. If there is nothing in the
2380 * queue we can push, we set a flag to make the sinkpad refuse more
2381 * buffers with an UNEXPECTED return value until we receive something
2382 * pushable again or we get flushed. */
2383 while ((data = gst_queue2_locked_dequeue (queue, item_type))) {
2384 if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2385 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2386 "dropping UNEXPECTED buffer %p", data);
2387 gst_buffer_unref (GST_BUFFER_CAST (data));
2388 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2389 GstEvent *event = GST_EVENT_CAST (data);
2390 GstEventType type = GST_EVENT_TYPE (event);
2392 if (type == GST_EVENT_EOS || type == GST_EVENT_NEWSEGMENT) {
2393 /* we found a pushable item in the queue, push it out */
2394 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2395 "pushing pushable event %s after UNEXPECTED",
2396 GST_EVENT_TYPE_NAME (event));
2397 return data;
2398 }
2399 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2400 "dropping UNEXPECTED event %p", event);
2401 gst_event_unref (event);
2402 } else if (*item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2403 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2404 "dropping UNEXPECTED buffer list %p", data);
2405 gst_buffer_list_unref (GST_BUFFER_LIST_CAST (data));
2406 }
2407 }
2408 /* no more items in the queue. Set the unexpected flag so that upstream
2409 * make us refuse any more buffers on the sinkpad. Since we will still
2410 * accept EOS and NEWSEGMENT we return _FLOW_OK to the caller so that the
2411 * task function does not shut down. */
2412 queue->unexpected = TRUE;
2413 return NULL;
2414 }
2416 /* dequeue an item from the queue an push it downstream. This functions returns
2417 * the result of the push. */
2418 static GstFlowReturn
2419 gst_queue2_push_one (GstQueue2 * queue)
2420 {
2421 GstFlowReturn result = GST_FLOW_OK;
2422 GstMiniObject *data;
2423 GstQueue2ItemType item_type;
2425 data = gst_queue2_locked_dequeue (queue, &item_type);
2426 if (data == NULL)
2427 goto no_item;
2429 next:
2430 GST_QUEUE2_MUTEX_UNLOCK (queue);
2432 if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER) {
2433 GstBuffer *buffer;
2434 GstCaps *caps;
2436 buffer = GST_BUFFER_CAST (data);
2437 caps = GST_BUFFER_CAPS (buffer);
2439 /* set caps before pushing the buffer so that core does not try to do
2440 * something fancy to check if this is possible. */
2441 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2442 gst_pad_set_caps (queue->srcpad, caps);
2444 result = gst_pad_push (queue->srcpad, buffer);
2446 /* need to check for srcresult here as well */
2447 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2448 if (result == GST_FLOW_UNEXPECTED) {
2449 data = gst_queue2_dequeue_on_unexpected (queue, &item_type);
2450 if (data != NULL)
2451 goto next;
2452 /* Since we will still accept EOS and NEWSEGMENT we return _FLOW_OK
2453 * to the caller so that the task function does not shut down */
2454 result = GST_FLOW_OK;
2455 }
2456 } else if (item_type == GST_QUEUE2_ITEM_TYPE_EVENT) {
2457 GstEvent *event = GST_EVENT_CAST (data);
2458 GstEventType type = GST_EVENT_TYPE (event);
2460 gst_pad_push_event (queue->srcpad, event);
2462 /* if we're EOS, return UNEXPECTED so that the task pauses. */
2463 if (type == GST_EVENT_EOS) {
2464 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2465 "pushed EOS event %p, return UNEXPECTED", event);
2466 result = GST_FLOW_UNEXPECTED;
2467 }
2469 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2470 } else if (item_type == GST_QUEUE2_ITEM_TYPE_BUFFER_LIST) {
2471 GstBufferList *buffer_list;
2472 GstBuffer *first_buf;
2473 GstCaps *caps;
2475 buffer_list = GST_BUFFER_LIST_CAST (data);
2477 first_buf = gst_buffer_list_get (buffer_list, 0, 0);
2478 caps = (first_buf != NULL) ? GST_BUFFER_CAPS (first_buf) : NULL;
2480 /* set caps before pushing the buffer so that core does not try to do
2481 * something fancy to check if this is possible. */
2482 if (caps && caps != GST_PAD_CAPS (queue->srcpad))
2483 gst_pad_set_caps (queue->srcpad, caps);
2485 result = gst_pad_push_list (queue->srcpad, buffer_list);
2487 /* need to check for srcresult here as well */
2488 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2489 if (result == GST_FLOW_UNEXPECTED) {
2490 data = gst_queue2_dequeue_on_unexpected (queue, &item_type);
2491 if (data != NULL)
2492 goto next;
2493 /* Since we will still accept EOS and NEWSEGMENT we return _FLOW_OK
2494 * to the caller so that the task function does not shut down */
2495 result = GST_FLOW_OK;
2496 }
2497 }
2498 return result;
2500 /* ERRORS */
2501 no_item:
2502 {
2503 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2504 "exit because we have no item in the queue");
2505 return GST_FLOW_ERROR;
2506 }
2507 out_flushing:
2508 {
2509 GST_CAT_LOG_OBJECT (queue_dataflow, queue, "exit because we are flushing");
2510 return GST_FLOW_WRONG_STATE;
2511 }
2512 }
2514 /* called repeatedly with @pad as the source pad. This function should push out
2515 * data to the peer element. */
2516 static void
2517 gst_queue2_loop (GstPad * pad)
2518 {
2519 GstQueue2 *queue;
2520 GstFlowReturn ret;
2522 queue = GST_QUEUE2 (GST_PAD_PARENT (pad));
2524 /* have to lock for thread-safety */
2525 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2527 if (gst_queue2_is_empty (queue)) {
2528 gboolean started;
2530 /* pause the timer while we wait. The fact that we are waiting does not mean
2531 * the byterate on the output pad is lower */
2532 if ((started = queue->out_timer_started))
2533 g_timer_stop (queue->out_timer);
2535 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue,
2536 "queue is empty, waiting for new data");
2537 do {
2538 /* Wait for data to be available, we could be unlocked because of a flush. */
2539 GST_QUEUE2_WAIT_ADD_CHECK (queue, queue->srcresult, out_flushing);
2540 }
2541 while (gst_queue2_is_empty (queue));
2543 /* and continue if we were running before */
2544 if (started)
2545 g_timer_continue (queue->out_timer);
2546 }
2547 ret = gst_queue2_push_one (queue);
2548 queue->srcresult = ret;
2549 queue->sinkresult = ret;
2550 if (ret != GST_FLOW_OK)
2551 goto out_flushing;
2553 GST_QUEUE2_MUTEX_UNLOCK (queue);
2555 return;
2557 /* ERRORS */
2558 out_flushing:
2559 {
2560 gboolean eos = queue->is_eos;
2561 GstFlowReturn ret = queue->srcresult;
2563 gst_pad_pause_task (queue->srcpad);
2564 GST_QUEUE2_MUTEX_UNLOCK (queue);
2565 GST_CAT_LOG_OBJECT (queue_dataflow, queue,
2566 "pause task, reason: %s", gst_flow_get_name (queue->srcresult));
2567 /* let app know about us giving up if upstream is not expected to do so */
2568 /* UNEXPECTED is already taken care of elsewhere */
2569 if (eos && (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED)) {
2570 GST_ELEMENT_ERROR (queue, STREAM, FAILED,
2571 (_("Internal data flow error.")),
2572 ("streaming task paused, reason %s (%d)",
2573 gst_flow_get_name (ret), ret));
2574 gst_pad_push_event (queue->srcpad, gst_event_new_eos ());
2575 }
2576 return;
2577 }
2578 }
2580 static gboolean
2581 gst_queue2_handle_src_event (GstPad * pad, GstEvent * event)
2582 {
2583 gboolean res = TRUE;
2584 GstQueue2 *queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2586 if (G_UNLIKELY (queue == NULL)) {
2587 gst_event_unref (event);
2588 return FALSE;
2589 }
2590 #ifndef GST_DISABLE_GST_DEBUG
2591 GST_CAT_DEBUG_OBJECT (queue_dataflow, queue, "got event %p (%s)",
2592 event, GST_EVENT_TYPE_NAME (event));
2593 #endif
2595 switch (GST_EVENT_TYPE (event)) {
2596 case GST_EVENT_FLUSH_START:
2597 if (QUEUE_IS_USING_QUEUE (queue)) {
2598 /* just forward upstream */
2599 res = gst_pad_push_event (queue->sinkpad, event);
2600 } else {
2601 /* now unblock the getrange function */
2602 GST_QUEUE2_MUTEX_LOCK (queue);
2603 GST_DEBUG_OBJECT (queue, "flushing");
2604 queue->srcresult = GST_FLOW_WRONG_STATE;
2605 GST_QUEUE2_SIGNAL_ADD (queue);
2606 GST_QUEUE2_MUTEX_UNLOCK (queue);
2608 /* when using a temp file, we eat the event */
2609 res = TRUE;
2610 gst_event_unref (event);
2611 }
2612 break;
2613 case GST_EVENT_FLUSH_STOP:
2614 if (QUEUE_IS_USING_QUEUE (queue)) {
2615 /* just forward upstream */
2616 res = gst_pad_push_event (queue->sinkpad, event);
2617 } else {
2618 /* now unblock the getrange function */
2619 GST_QUEUE2_MUTEX_LOCK (queue);
2620 queue->srcresult = GST_FLOW_OK;
2621 if (queue->current) {
2622 /* forget the highest read offset, we'll calculate a new one when we
2623 * get the next getrange request. We need to do this in order to reset
2624 * the buffering percentage */
2625 queue->current->max_reading_pos = 0;
2626 }
2627 GST_QUEUE2_MUTEX_UNLOCK (queue);
2629 /* when using a temp file, we eat the event */
2630 res = TRUE;
2631 gst_event_unref (event);
2632 }
2633 break;
2634 default:
2635 res = gst_pad_push_event (queue->sinkpad, event);
2636 break;
2637 }
2639 gst_object_unref (queue);
2640 return res;
2641 }
2643 static gboolean
2644 gst_queue2_peer_query (GstQueue2 * queue, GstPad * pad, GstQuery * query)
2645 {
2646 gboolean ret = FALSE;
2647 GstPad *peer;
2649 if ((peer = gst_pad_get_peer (pad))) {
2650 ret = gst_pad_query (peer, query);
2651 gst_object_unref (peer);
2652 }
2653 return ret;
2654 }
2656 static gboolean
2657 gst_queue2_handle_src_query (GstPad * pad, GstQuery * query)
2658 {
2659 GstQueue2 *queue;
2661 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2662 if (G_UNLIKELY (queue == NULL))
2663 return FALSE;
2665 switch (GST_QUERY_TYPE (query)) {
2666 case GST_QUERY_POSITION:
2667 {
2668 gint64 peer_pos;
2669 GstFormat format;
2671 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2672 goto peer_failed;
2674 /* get peer position */
2675 gst_query_parse_position (query, &format, &peer_pos);
2677 /* FIXME: this code assumes that there's no discont in the queue */
2678 switch (format) {
2679 case GST_FORMAT_BYTES:
2680 peer_pos -= queue->cur_level.bytes;
2681 break;
2682 case GST_FORMAT_TIME:
2683 peer_pos -= queue->cur_level.time;
2684 break;
2685 default:
2686 GST_WARNING_OBJECT (queue, "dropping query in %s format, don't "
2687 "know how to adjust value", gst_format_get_name (format));
2688 return FALSE;
2689 }
2690 /* set updated position */
2691 gst_query_set_position (query, format, peer_pos);
2692 break;
2693 }
2694 case GST_QUERY_DURATION:
2695 {
2696 GST_DEBUG_OBJECT (queue, "doing peer query");
2698 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2699 goto peer_failed;
2701 GST_DEBUG_OBJECT (queue, "peer query success");
2702 break;
2703 }
2704 case GST_QUERY_BUFFERING:
2705 {
2706 GstFormat format;
2708 GST_DEBUG_OBJECT (queue, "query buffering");
2710 /* FIXME - is this condition correct? what should ring buffer do? */
2711 if (QUEUE_IS_USING_QUEUE (queue)) {
2712 /* no temp file, just forward to the peer */
2713 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2714 goto peer_failed;
2715 GST_DEBUG_OBJECT (queue, "buffering forwarded to peer");
2716 } else {
2717 gint64 start, stop, range_start, range_stop;
2718 guint64 writing_pos;
2719 gint percent;
2720 gint64 estimated_total, buffering_left;
2721 GstFormat peer_fmt;
2722 gint64 duration;
2723 gboolean peer_res, is_buffering, is_eos;
2724 gdouble byte_in_rate, byte_out_rate;
2725 GstQueue2Range *queued_ranges;
2727 /* we need a current download region */
2728 if (queue->current == NULL)
2729 return FALSE;
2731 writing_pos = queue->current->writing_pos;
2732 byte_in_rate = queue->byte_in_rate;
2733 byte_out_rate = queue->byte_out_rate;
2734 is_buffering = queue->is_buffering;
2735 is_eos = queue->is_eos;
2736 percent = queue->buffering_percent;
2738 if (is_eos) {
2739 /* we're EOS, we know the duration in bytes now */
2740 peer_res = TRUE;
2741 duration = writing_pos;
2742 } else {
2743 /* get duration of upstream in bytes */
2744 peer_fmt = GST_FORMAT_BYTES;
2745 peer_res = gst_pad_query_peer_duration (queue->sinkpad, &peer_fmt,
2746 &duration);
2747 }
2749 /* calculate remaining and total download time */
2750 if (peer_res && byte_in_rate > 0.0) {
2751 estimated_total = (duration * 1000) / byte_in_rate;
2752 buffering_left = ((duration - writing_pos) * 1000) / byte_in_rate;
2753 } else {
2754 estimated_total = -1;
2755 buffering_left = -1;
2756 }
2757 GST_DEBUG_OBJECT (queue, "estimated %" G_GINT64_FORMAT ", left %"
2758 G_GINT64_FORMAT, estimated_total, buffering_left);
2760 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
2762 switch (format) {
2763 case GST_FORMAT_PERCENT:
2764 /* we need duration */
2765 if (!peer_res)
2766 goto peer_failed;
2768 GST_DEBUG_OBJECT (queue,
2769 "duration %" G_GINT64_FORMAT ", writing %" G_GINT64_FORMAT,
2770 duration, writing_pos);
2772 start = 0;
2773 /* get our available data relative to the duration */
2774 if (duration != -1)
2775 stop = GST_FORMAT_PERCENT_MAX * writing_pos / duration;
2776 else
2777 stop = -1;
2778 break;
2779 case GST_FORMAT_BYTES:
2780 start = 0;
2781 stop = writing_pos;
2782 break;
2783 default:
2784 start = -1;
2785 stop = -1;
2786 break;
2787 }
2789 /* fill out the buffered ranges */
2790 for (queued_ranges = queue->ranges; queued_ranges;
2791 queued_ranges = queued_ranges->next) {
2792 switch (format) {
2793 case GST_FORMAT_PERCENT:
2794 if (duration == -1) {
2795 range_start = 0;
2796 range_stop = 0;
2797 break;
2798 }
2799 range_start = 100 * queued_ranges->offset / duration;
2800 range_stop = 100 * queued_ranges->writing_pos / duration;
2801 break;
2802 case GST_FORMAT_BYTES:
2803 range_start = queued_ranges->offset;
2804 range_stop = queued_ranges->writing_pos;
2805 break;
2806 default:
2807 range_start = -1;
2808 range_stop = -1;
2809 break;
2810 }
2811 if (range_start == range_stop)
2812 continue;
2813 GST_DEBUG_OBJECT (queue,
2814 "range starting at %" G_GINT64_FORMAT " and finishing at %"
2815 G_GINT64_FORMAT, range_start, range_stop);
2816 gst_query_add_buffering_range (query, range_start, range_stop);
2817 }
2819 gst_query_set_buffering_percent (query, is_buffering, percent);
2820 gst_query_set_buffering_range (query, format, start, stop,
2821 estimated_total);
2822 gst_query_set_buffering_stats (query, GST_BUFFERING_DOWNLOAD,
2823 byte_in_rate, byte_out_rate, buffering_left);
2824 }
2825 break;
2826 }
2827 default:
2828 /* peer handled other queries */
2829 if (!gst_queue2_peer_query (queue, queue->sinkpad, query))
2830 goto peer_failed;
2831 break;
2832 }
2834 gst_object_unref (queue);
2835 return TRUE;
2837 /* ERRORS */
2838 peer_failed:
2839 {
2840 GST_DEBUG_OBJECT (queue, "failed peer query");
2841 gst_object_unref (queue);
2842 return FALSE;
2843 }
2844 }
2846 static gboolean
2847 gst_queue2_handle_query (GstElement * element, GstQuery * query)
2848 {
2849 /* simply forward to the srcpad query function */
2850 return gst_queue2_handle_src_query (GST_QUEUE2_CAST (element)->srcpad, query);
2851 }
2853 static void
2854 gst_queue2_update_upstream_size (GstQueue2 * queue)
2855 {
2856 GstFormat fmt = GST_FORMAT_BYTES;
2857 gint64 upstream_size = -1;
2859 if (gst_pad_query_peer_duration (queue->sinkpad, &fmt, &upstream_size)) {
2860 GST_INFO_OBJECT (queue, "upstream size: %" G_GINT64_FORMAT, upstream_size);
2861 queue->upstream_size = upstream_size;
2862 }
2863 }
2865 static GstFlowReturn
2866 gst_queue2_get_range (GstPad * pad, guint64 offset, guint length,
2867 GstBuffer ** buffer)
2868 {
2869 GstQueue2 *queue;
2870 GstFlowReturn ret;
2872 queue = GST_QUEUE2_CAST (gst_pad_get_parent (pad));
2874 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
2875 GST_QUEUE2_MUTEX_LOCK_CHECK (queue, queue->srcresult, out_flushing);
2876 offset = (offset == -1) ? queue->current->reading_pos : offset;
2878 GST_DEBUG_OBJECT (queue,
2879 "Getting range: offset %" G_GUINT64_FORMAT ", length %u", offset, length);
2881 /* catch any reads beyond the size of the file here to make sure queue2
2882 * doesn't send seek events beyond the size of the file upstream, since
2883 * that would confuse elements such as souphttpsrc and/or http servers.
2884 * Demuxers often just loop until EOS at the end of the file to figure out
2885 * when they've read all the end-headers or index chunks. */
2886 if (G_UNLIKELY (offset >= queue->upstream_size)) {
2887 gst_queue2_update_upstream_size (queue);
2888 if (queue->upstream_size > 0 && offset >= queue->upstream_size)
2889 goto out_unexpected;
2890 }
2892 if (G_UNLIKELY (offset + length > queue->upstream_size)) {
2893 gst_queue2_update_upstream_size (queue);
2894 if (queue->upstream_size > 0 && offset + length >= queue->upstream_size) {
2895 length = queue->upstream_size - offset;
2896 GST_DEBUG_OBJECT (queue, "adjusting length downto %d", length);
2897 }
2898 }
2900 /* FIXME - function will block when the range is not yet available */
2901 ret = gst_queue2_create_read (queue, offset, length, buffer);
2902 GST_QUEUE2_MUTEX_UNLOCK (queue);
2904 gst_object_unref (queue);
2906 return ret;
2908 /* ERRORS */
2909 out_flushing:
2910 {
2911 ret = queue->srcresult;
2913 GST_DEBUG_OBJECT (queue, "we are flushing");
2914 GST_QUEUE2_MUTEX_UNLOCK (queue);
2915 gst_object_unref (queue);
2916 return ret;
2917 }
2918 out_unexpected:
2919 {
2920 GST_DEBUG_OBJECT (queue, "read beyond end of file");
2921 GST_QUEUE2_MUTEX_UNLOCK (queue);
2922 gst_object_unref (queue);
2923 return GST_FLOW_UNEXPECTED;
2924 }
2925 }
2927 static gboolean
2928 gst_queue2_src_checkgetrange_function (GstPad * pad)
2929 {
2930 GstQueue2 *queue;
2931 gboolean ret;
2933 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2935 /* we can operate in pull mode when we are using a tempfile */
2936 ret = !QUEUE_IS_USING_QUEUE (queue);
2938 gst_object_unref (GST_OBJECT (queue));
2940 return ret;
2941 }
2943 /* sink currently only operates in push mode */
2944 static gboolean
2945 gst_queue2_sink_activate_push (GstPad * pad, gboolean active)
2946 {
2947 gboolean result = TRUE;
2948 GstQueue2 *queue;
2950 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2952 if (active) {
2953 GST_QUEUE2_MUTEX_LOCK (queue);
2954 GST_DEBUG_OBJECT (queue, "activating push mode");
2955 queue->srcresult = GST_FLOW_OK;
2956 queue->sinkresult = GST_FLOW_OK;
2957 queue->is_eos = FALSE;
2958 queue->unexpected = FALSE;
2959 reset_rate_timer (queue);
2960 GST_QUEUE2_MUTEX_UNLOCK (queue);
2961 } else {
2962 /* unblock chain function */
2963 GST_QUEUE2_MUTEX_LOCK (queue);
2964 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2965 queue->srcresult = GST_FLOW_WRONG_STATE;
2966 queue->sinkresult = GST_FLOW_WRONG_STATE;
2967 gst_queue2_locked_flush (queue);
2968 GST_QUEUE2_MUTEX_UNLOCK (queue);
2969 }
2971 gst_object_unref (queue);
2973 return result;
2974 }
2976 /* src operating in push mode, we start a task on the source pad that pushes out
2977 * buffers from the queue */
2978 static gboolean
2979 gst_queue2_src_activate_push (GstPad * pad, gboolean active)
2980 {
2981 gboolean result = FALSE;
2982 GstQueue2 *queue;
2984 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
2986 if (active) {
2987 GST_QUEUE2_MUTEX_LOCK (queue);
2988 GST_DEBUG_OBJECT (queue, "activating push mode");
2989 queue->srcresult = GST_FLOW_OK;
2990 queue->sinkresult = GST_FLOW_OK;
2991 queue->is_eos = FALSE;
2992 queue->unexpected = FALSE;
2993 result = gst_pad_start_task (pad, (GstTaskFunction) gst_queue2_loop, pad);
2994 GST_QUEUE2_MUTEX_UNLOCK (queue);
2995 } else {
2996 /* unblock loop function */
2997 GST_QUEUE2_MUTEX_LOCK (queue);
2998 GST_DEBUG_OBJECT (queue, "deactivating push mode");
2999 queue->srcresult = GST_FLOW_WRONG_STATE;
3000 queue->sinkresult = GST_FLOW_WRONG_STATE;
3001 /* the item add signal will unblock */
3002 GST_QUEUE2_SIGNAL_ADD (queue);
3003 GST_QUEUE2_MUTEX_UNLOCK (queue);
3005 /* step 2, make sure streaming finishes */
3006 result = gst_pad_stop_task (pad);
3007 }
3009 gst_object_unref (queue);
3011 return result;
3012 }
3014 /* pull mode, downstream will call our getrange function */
3015 static gboolean
3016 gst_queue2_src_activate_pull (GstPad * pad, gboolean active)
3017 {
3018 gboolean result;
3019 GstQueue2 *queue;
3021 queue = GST_QUEUE2 (gst_pad_get_parent (pad));
3023 if (active) {
3024 GST_QUEUE2_MUTEX_LOCK (queue);
3025 if (!QUEUE_IS_USING_QUEUE (queue)) {
3026 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3027 /* open the temp file now */
3028 result = gst_queue2_open_temp_location_file (queue);
3029 } else if (!queue->ring_buffer) {
3030 queue->ring_buffer = g_malloc (queue->ring_buffer_max_size);
3031 result = ! !queue->ring_buffer;
3032 } else {
3033 result = TRUE;
3034 }
3036 GST_DEBUG_OBJECT (queue, "activating pull mode");
3037 init_ranges (queue);
3038 queue->srcresult = GST_FLOW_OK;
3039 queue->sinkresult = GST_FLOW_OK;
3040 queue->is_eos = FALSE;
3041 queue->unexpected = FALSE;
3042 queue->upstream_size = 0;
3043 } else {
3044 GST_DEBUG_OBJECT (queue, "no temp file, cannot activate pull mode");
3045 /* this is not allowed, we cannot operate in pull mode without a temp
3046 * file. */
3047 queue->srcresult = GST_FLOW_WRONG_STATE;
3048 queue->sinkresult = GST_FLOW_WRONG_STATE;
3049 result = FALSE;
3050 }
3051 GST_QUEUE2_MUTEX_UNLOCK (queue);
3052 } else {
3053 GST_QUEUE2_MUTEX_LOCK (queue);
3054 GST_DEBUG_OBJECT (queue, "deactivating pull mode");
3055 queue->srcresult = GST_FLOW_WRONG_STATE;
3056 queue->sinkresult = GST_FLOW_WRONG_STATE;
3057 /* this will unlock getrange */
3058 GST_QUEUE2_SIGNAL_ADD (queue);
3059 result = TRUE;
3060 GST_QUEUE2_MUTEX_UNLOCK (queue);
3061 }
3062 gst_object_unref (queue);
3064 return result;
3065 }
3067 static GstStateChangeReturn
3068 gst_queue2_change_state (GstElement * element, GstStateChange transition)
3069 {
3070 GstQueue2 *queue;
3071 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3073 queue = GST_QUEUE2 (element);
3075 switch (transition) {
3076 case GST_STATE_CHANGE_NULL_TO_READY:
3077 break;
3078 case GST_STATE_CHANGE_READY_TO_PAUSED:
3079 GST_QUEUE2_MUTEX_LOCK (queue);
3080 if (!QUEUE_IS_USING_QUEUE (queue)) {
3081 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3082 if (!gst_queue2_open_temp_location_file (queue))
3083 ret = GST_STATE_CHANGE_FAILURE;
3084 } else {
3085 if (queue->ring_buffer) {
3086 g_free (queue->ring_buffer);
3087 queue->ring_buffer = NULL;
3088 }
3089 if (!(queue->ring_buffer = g_malloc (queue->ring_buffer_max_size)))
3090 ret = GST_STATE_CHANGE_FAILURE;
3091 }
3092 init_ranges (queue);
3093 }
3094 queue->segment_event_received = FALSE;
3095 queue->starting_segment = NULL;
3096 GST_QUEUE2_MUTEX_UNLOCK (queue);
3097 break;
3098 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3099 break;
3100 default:
3101 break;
3102 }
3104 if (ret == GST_STATE_CHANGE_FAILURE)
3105 return ret;
3107 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3109 if (ret == GST_STATE_CHANGE_FAILURE)
3110 return ret;
3112 switch (transition) {
3113 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3114 break;
3115 case GST_STATE_CHANGE_PAUSED_TO_READY:
3116 GST_QUEUE2_MUTEX_LOCK (queue);
3117 if (!QUEUE_IS_USING_QUEUE (queue)) {
3118 if (QUEUE_IS_USING_TEMP_FILE (queue)) {
3119 gst_queue2_close_temp_location_file (queue);
3120 } else if (queue->ring_buffer) {
3121 g_free (queue->ring_buffer);
3122 queue->ring_buffer = NULL;
3123 }
3124 clean_ranges (queue);
3125 }
3126 if (queue->starting_segment != NULL) {
3127 gst_event_unref (queue->starting_segment);
3128 queue->starting_segment = NULL;
3129 }
3130 GST_QUEUE2_MUTEX_UNLOCK (queue);
3131 break;
3132 case GST_STATE_CHANGE_READY_TO_NULL:
3133 break;
3134 default:
3135 break;
3136 }
3138 return ret;
3139 }
3141 /* changing the capacity of the queue must wake up
3142 * the _chain function, it might have more room now
3143 * to store the buffer/event in the queue */
3144 #define QUEUE_CAPACITY_CHANGE(q)\
3145 GST_QUEUE2_SIGNAL_DEL (queue);
3147 /* Changing the minimum required fill level must
3148 * wake up the _loop function as it might now
3149 * be able to preceed.
3150 */
3151 #define QUEUE_THRESHOLD_CHANGE(q)\
3152 GST_QUEUE2_SIGNAL_ADD (queue);
3154 static void
3155 gst_queue2_set_temp_template (GstQueue2 * queue, const gchar * template)
3156 {
3157 GstState state;
3159 /* the element must be stopped in order to do this */
3160 GST_OBJECT_LOCK (queue);
3161 state = GST_STATE (queue);
3162 if (state != GST_STATE_READY && state != GST_STATE_NULL)
3163 goto wrong_state;
3164 GST_OBJECT_UNLOCK (queue);
3166 /* set new location */
3167 g_free (queue->temp_template);
3168 queue->temp_template = g_strdup (template);
3170 return;
3172 /* ERROR */
3173 wrong_state:
3174 {
3175 GST_WARNING_OBJECT (queue, "setting temp-template property in wrong state");
3176 GST_OBJECT_UNLOCK (queue);
3177 }
3178 }
3180 static void
3181 gst_queue2_set_property (GObject * object,
3182 guint prop_id, const GValue * value, GParamSpec * pspec)
3183 {
3184 GstQueue2 *queue = GST_QUEUE2 (object);
3186 /* someone could change levels here, and since this
3187 * affects the get/put funcs, we need to lock for safety. */
3188 GST_QUEUE2_MUTEX_LOCK (queue);
3190 switch (prop_id) {
3191 case PROP_MAX_SIZE_BYTES:
3192 queue->max_level.bytes = g_value_get_uint (value);
3193 QUEUE_CAPACITY_CHANGE (queue);
3194 break;
3195 case PROP_MAX_SIZE_BUFFERS:
3196 queue->max_level.buffers = g_value_get_uint (value);
3197 QUEUE_CAPACITY_CHANGE (queue);
3198 break;
3199 case PROP_MAX_SIZE_TIME:
3200 queue->max_level.time = g_value_get_uint64 (value);
3201 /* set rate_time to the same value. We use an extra field in the level
3202 * structure so that we can easily access and compare it */
3203 queue->max_level.rate_time = queue->max_level.time;
3204 QUEUE_CAPACITY_CHANGE (queue);
3205 break;
3206 case PROP_USE_BUFFERING:
3207 queue->use_buffering = g_value_get_boolean (value);
3208 break;
3209 case PROP_USE_RATE_ESTIMATE:
3210 queue->use_rate_estimate = g_value_get_boolean (value);
3211 break;
3212 case PROP_LOW_PERCENT:
3213 queue->low_percent = g_value_get_int (value);
3214 break;
3215 case PROP_HIGH_PERCENT:
3216 queue->high_percent = g_value_get_int (value);
3217 break;
3218 case PROP_TEMP_TEMPLATE:
3219 gst_queue2_set_temp_template (queue, g_value_get_string (value));
3220 break;
3221 case PROP_TEMP_LOCATION:
3222 g_free (queue->temp_location);
3223 queue->temp_location = g_value_dup_string (value);
3224 /* you can set the property back to NULL to make it use the temp-tmpl
3225 * property. */
3226 queue->temp_location_set = queue->temp_location != NULL;
3227 break;
3228 case PROP_TEMP_REMOVE:
3229 queue->temp_remove = g_value_get_boolean (value);
3230 break;
3231 case PROP_RING_BUFFER_MAX_SIZE:
3232 queue->ring_buffer_max_size = g_value_get_uint64 (value);
3233 break;
3234 default:
3235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3236 break;
3237 }
3239 GST_QUEUE2_MUTEX_UNLOCK (queue);
3240 }
3242 static void
3243 gst_queue2_get_property (GObject * object,
3244 guint prop_id, GValue * value, GParamSpec * pspec)
3245 {
3246 GstQueue2 *queue = GST_QUEUE2 (object);
3248 GST_QUEUE2_MUTEX_LOCK (queue);
3250 switch (prop_id) {
3251 case PROP_CUR_LEVEL_BYTES:
3252 g_value_set_uint (value, queue->cur_level.bytes);
3253 break;
3254 case PROP_CUR_LEVEL_BUFFERS:
3255 g_value_set_uint (value, queue->cur_level.buffers);
3256 break;
3257 case PROP_CUR_LEVEL_TIME:
3258 g_value_set_uint64 (value, queue->cur_level.time);
3259 break;
3260 case PROP_MAX_SIZE_BYTES:
3261 g_value_set_uint (value, queue->max_level.bytes);
3262 break;
3263 case PROP_MAX_SIZE_BUFFERS:
3264 g_value_set_uint (value, queue->max_level.buffers);
3265 break;
3266 case PROP_MAX_SIZE_TIME:
3267 g_value_set_uint64 (value, queue->max_level.time);
3268 break;
3269 case PROP_USE_BUFFERING:
3270 g_value_set_boolean (value, queue->use_buffering);
3271 break;
3272 case PROP_USE_RATE_ESTIMATE:
3273 g_value_set_boolean (value, queue->use_rate_estimate);
3274 break;
3275 case PROP_LOW_PERCENT:
3276 g_value_set_int (value, queue->low_percent);
3277 break;
3278 case PROP_HIGH_PERCENT:
3279 g_value_set_int (value, queue->high_percent);
3280 break;
3281 case PROP_TEMP_TEMPLATE:
3282 g_value_set_string (value, queue->temp_template);
3283 break;
3284 case PROP_TEMP_LOCATION:
3285 g_value_set_string (value, queue->temp_location);
3286 break;
3287 case PROP_TEMP_REMOVE:
3288 g_value_set_boolean (value, queue->temp_remove);
3289 break;
3290 case PROP_RING_BUFFER_MAX_SIZE:
3291 g_value_set_uint64 (value, queue->ring_buffer_max_size);
3292 break;
3293 default:
3294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3295 break;
3296 }
3298 GST_QUEUE2_MUTEX_UNLOCK (queue);
3299 }