2 * Copyright (C) 2014 Wim Taymans <wim.taymans@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-downloadbuffer
25 * If you set temp-template to a value such as /tmp/gstreamer-XXXXXX, the element
26 * will allocate a random free filename and buffer data in the file.
27 * By using this, it will buffer the entire stream data on the file independently
28 * of the size limits, they will only be used for buffering statistics.
30 * The temp-location property will be used to notify the application of the
38 #include "gstdownloadbuffer.h"
40 #include <glib/gstdio.h>
43 #include "gst/gst-i18n-lib.h"
44 #include "gst/glib-compat-private.h"
48 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
53 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58 GST_DEBUG_CATEGORY_STATIC (downloadbuffer_debug);
59 #define GST_CAT_DEFAULT (downloadbuffer_debug)
67 #define DEFAULT_BUFFER_SIZE 4096
69 /* default property values */
70 #define DEFAULT_MAX_SIZE_BYTES (2 * 1024 * 1024) /* 2 MB */
71 #define DEFAULT_MAX_SIZE_TIME 2 * GST_SECOND /* 2 seconds */
72 #define DEFAULT_LOW_PERCENT 10
73 #define DEFAULT_HIGH_PERCENT 99
74 #define DEFAULT_TEMP_REMOVE TRUE
89 #define GST_DOWNLOAD_BUFFER_CLEAR_LEVEL(l) G_STMT_START { \
94 #define STATUS(elem, pad, msg) \
95 GST_LOG_OBJECT (elem, "(%s:%s) " msg ": %u of %u " \
96 "bytes, %" G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT \
98 GST_DEBUG_PAD_NAME (pad), \
99 elem->cur_level.bytes, \
100 elem->max_level.bytes, \
101 elem->cur_level.time, \
102 elem->max_level.time)
104 #define GST_DOWNLOAD_BUFFER_MUTEX_LOCK(q) G_STMT_START { \
105 g_mutex_lock (&q->qlock); \
108 #define GST_DOWNLOAD_BUFFER_MUTEX_LOCK_CHECK(q,res,label) G_STMT_START { \
109 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (q); \
110 if (res != GST_FLOW_OK) \
114 #define GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK(q) G_STMT_START { \
115 g_mutex_unlock (&q->qlock); \
118 #define GST_DOWNLOAD_BUFFER_WAIT_ADD_CHECK(q, res, o, label) G_STMT_START { \
119 STATUS (q, q->srcpad, "wait for ADD"); \
120 q->waiting_add = TRUE; \
121 q->waiting_offset = o; \
122 g_cond_wait (&q->item_add, &q->qlock); \
123 q->waiting_add = FALSE; \
124 if (res != GST_FLOW_OK) { \
125 STATUS (q, q->srcpad, "received ADD wakeup"); \
128 STATUS (q, q->srcpad, "received ADD"); \
131 #define GST_DOWNLOAD_BUFFER_SIGNAL_ADD(q, o) G_STMT_START { \
132 if (q->waiting_add && q->waiting_offset <= o) { \
133 STATUS (q, q->sinkpad, "signal ADD"); \
134 g_cond_signal (&q->item_add); \
139 GST_DEBUG_CATEGORY_INIT (downloadbuffer_debug, "downloadbuffer", 0, \
140 "downloadbuffer element");
142 #define gst_download_buffer_parent_class parent_class
143 G_DEFINE_TYPE_WITH_CODE (GstDownloadBuffer, gst_download_buffer,
144 GST_TYPE_ELEMENT, _do_init);
146 static void update_buffering (GstDownloadBuffer * dlbuf);
148 static void gst_download_buffer_finalize (GObject * object);
150 static void gst_download_buffer_set_property (GObject * object,
151 guint prop_id, const GValue * value, GParamSpec * pspec);
152 static void gst_download_buffer_get_property (GObject * object,
153 guint prop_id, GValue * value, GParamSpec * pspec);
155 static GstFlowReturn gst_download_buffer_chain (GstPad * pad,
156 GstObject * parent, GstBuffer * buffer);
157 static void gst_download_buffer_loop (GstPad * pad);
159 static gboolean gst_download_buffer_handle_sink_event (GstPad * pad,
160 GstObject * parent, GstEvent * event);
161 static gboolean gst_download_buffer_handle_sink_query (GstPad * pad,
162 GstObject * parent, GstQuery * query);
164 static gboolean gst_download_buffer_handle_src_event (GstPad * pad,
165 GstObject * parent, GstEvent * event);
166 static gboolean gst_download_buffer_handle_src_query (GstPad * pad,
167 GstObject * parent, GstQuery * query);
168 static gboolean gst_download_buffer_handle_query (GstElement * element,
171 static GstFlowReturn gst_download_buffer_get_range (GstPad * pad,
172 GstObject * parent, guint64 offset, guint length, GstBuffer ** buffer);
174 static gboolean gst_download_buffer_src_activate_mode (GstPad * pad,
175 GstObject * parent, GstPadMode mode, gboolean active);
176 static gboolean gst_download_buffer_sink_activate_mode (GstPad * pad,
177 GstObject * parent, GstPadMode mode, gboolean active);
178 static GstStateChangeReturn gst_download_buffer_change_state (GstElement *
179 element, GstStateChange transition);
181 /* static guint gst_download_buffer_signals[LAST_SIGNAL] = { 0 }; */
184 gst_download_buffer_class_init (GstDownloadBufferClass * klass)
186 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
187 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
189 gobject_class->set_property = gst_download_buffer_set_property;
190 gobject_class->get_property = gst_download_buffer_get_property;
193 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_BYTES,
194 g_param_spec_uint ("max-size-bytes", "Max. size (kB)",
195 "Max. amount of data to buffer (bytes, 0=disable)",
196 0, G_MAXUINT, DEFAULT_MAX_SIZE_BYTES,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_MAX_SIZE_TIME,
199 g_param_spec_uint64 ("max-size-time", "Max. size (ns)",
200 "Max. amount of data to buffer (in ns, 0=disable)", 0, G_MAXUINT64,
201 DEFAULT_MAX_SIZE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_LOW_PERCENT,
204 g_param_spec_int ("low-percent", "Low percent",
205 "Low threshold for buffering to start. Only used if use-buffering is True",
206 0, 100, DEFAULT_LOW_PERCENT,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (gobject_class, PROP_HIGH_PERCENT,
209 g_param_spec_int ("high-percent", "High percent",
210 "High threshold for buffering to finish. Only used if use-buffering is True",
211 0, 100, DEFAULT_HIGH_PERCENT,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class, PROP_TEMP_TEMPLATE,
215 g_param_spec_string ("temp-template", "Temporary File Template",
216 "File template to store temporary files in, should contain directory "
217 "and XXXXXX. (NULL == disabled)",
218 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (gobject_class, PROP_TEMP_LOCATION,
221 g_param_spec_string ("temp-location", "Temporary File Location",
222 "Location to store temporary files in (Only read this property, "
223 "use temp-template to configure the name template)",
224 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
227 * GstDownloadBuffer:temp-remove
229 * When temp-template is set, remove the temporary file when going to READY.
231 g_object_class_install_property (gobject_class, PROP_TEMP_REMOVE,
232 g_param_spec_boolean ("temp-remove", "Remove the Temporary File",
233 "Remove the temp-location after use",
234 DEFAULT_TEMP_REMOVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 /* set several parent class virtual functions */
237 gobject_class->finalize = gst_download_buffer_finalize;
239 gst_element_class_add_pad_template (gstelement_class,
240 gst_static_pad_template_get (&srctemplate));
241 gst_element_class_add_pad_template (gstelement_class,
242 gst_static_pad_template_get (&sinktemplate));
244 gst_element_class_set_static_metadata (gstelement_class, "DownloadBuffer",
245 "Generic", "Download Buffer element",
246 "Wim Taymans <wim.taymans@gmail.com>");
248 gstelement_class->change_state =
249 GST_DEBUG_FUNCPTR (gst_download_buffer_change_state);
250 gstelement_class->query =
251 GST_DEBUG_FUNCPTR (gst_download_buffer_handle_query);
255 gst_download_buffer_init (GstDownloadBuffer * dlbuf)
257 dlbuf->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
259 gst_pad_set_chain_function (dlbuf->sinkpad,
260 GST_DEBUG_FUNCPTR (gst_download_buffer_chain));
261 gst_pad_set_activatemode_function (dlbuf->sinkpad,
262 GST_DEBUG_FUNCPTR (gst_download_buffer_sink_activate_mode));
263 gst_pad_set_event_function (dlbuf->sinkpad,
264 GST_DEBUG_FUNCPTR (gst_download_buffer_handle_sink_event));
265 gst_pad_set_query_function (dlbuf->sinkpad,
266 GST_DEBUG_FUNCPTR (gst_download_buffer_handle_sink_query));
267 GST_PAD_SET_PROXY_CAPS (dlbuf->sinkpad);
268 gst_element_add_pad (GST_ELEMENT (dlbuf), dlbuf->sinkpad);
270 dlbuf->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
272 gst_pad_set_activatemode_function (dlbuf->srcpad,
273 GST_DEBUG_FUNCPTR (gst_download_buffer_src_activate_mode));
274 gst_pad_set_getrange_function (dlbuf->srcpad,
275 GST_DEBUG_FUNCPTR (gst_download_buffer_get_range));
276 gst_pad_set_event_function (dlbuf->srcpad,
277 GST_DEBUG_FUNCPTR (gst_download_buffer_handle_src_event));
278 gst_pad_set_query_function (dlbuf->srcpad,
279 GST_DEBUG_FUNCPTR (gst_download_buffer_handle_src_query));
280 GST_PAD_SET_PROXY_CAPS (dlbuf->srcpad);
281 gst_element_add_pad (GST_ELEMENT (dlbuf), dlbuf->srcpad);
284 GST_DOWNLOAD_BUFFER_CLEAR_LEVEL (dlbuf->cur_level);
285 dlbuf->max_level.bytes = DEFAULT_MAX_SIZE_BYTES;
286 dlbuf->max_level.time = DEFAULT_MAX_SIZE_TIME;
287 dlbuf->low_percent = DEFAULT_LOW_PERCENT;
288 dlbuf->high_percent = DEFAULT_HIGH_PERCENT;
290 dlbuf->srcresult = GST_FLOW_FLUSHING;
291 dlbuf->sinkresult = GST_FLOW_FLUSHING;
292 dlbuf->in_timer = g_timer_new ();
293 dlbuf->out_timer = g_timer_new ();
295 g_mutex_init (&dlbuf->qlock);
296 dlbuf->waiting_add = FALSE;
297 g_cond_init (&dlbuf->item_add);
299 dlbuf->buffering_percent = 100;
301 /* tempfile related */
302 dlbuf->temp_template = NULL;
303 dlbuf->temp_location = NULL;
304 dlbuf->temp_remove = DEFAULT_TEMP_REMOVE;
307 /* called only once, as opposed to dispose */
309 gst_download_buffer_finalize (GObject * object)
311 GstDownloadBuffer *dlbuf = GST_DOWNLOAD_BUFFER (object);
313 g_mutex_clear (&dlbuf->qlock);
314 g_cond_clear (&dlbuf->item_add);
315 g_timer_destroy (dlbuf->in_timer);
316 g_timer_destroy (dlbuf->out_timer);
318 /* temp_file path cleanup */
319 g_free (dlbuf->temp_template);
320 g_free (dlbuf->temp_location);
322 G_OBJECT_CLASS (parent_class)->finalize (object);
326 reset_rate_timer (GstDownloadBuffer * dlbuf)
329 dlbuf->bytes_out = 0;
330 dlbuf->byte_in_rate = 0.0;
331 dlbuf->byte_in_period = 0;
332 dlbuf->byte_out_rate = 0.0;
333 dlbuf->last_in_elapsed = 0.0;
334 dlbuf->last_out_elapsed = 0.0;
335 dlbuf->in_timer_started = FALSE;
336 dlbuf->out_timer_started = FALSE;
339 /* the interval in seconds to recalculate the rate */
340 #define RATE_INTERVAL 0.2
341 /* Tuning for rate estimation. We use a large window for the input rate because
342 * it should be stable when connected to a network. The output rate is less
343 * stable (the elements preroll, queues behind a demuxer fill, ...) and should
344 * therefore adapt more quickly.
345 * However, initial input rate may be subject to a burst, and should therefore
346 * initially also adapt more quickly to changes, and only later on give higher
347 * weight to previous values. */
348 #define AVG_IN(avg,val,w1,w2) ((avg) * (w1) + (val) * (w2)) / ((w1) + (w2))
349 #define AVG_OUT(avg,val) ((avg) * 3.0 + (val)) / 4.0
352 update_time_level (GstDownloadBuffer * dlbuf)
354 if (dlbuf->byte_in_rate > 0.0) {
355 dlbuf->cur_level.time =
356 dlbuf->cur_level.bytes / dlbuf->byte_in_rate * GST_SECOND;
358 GST_DEBUG ("levels: bytes %u/%u, time %" GST_TIME_FORMAT "/%" GST_TIME_FORMAT,
359 dlbuf->cur_level.bytes, dlbuf->max_level.bytes,
360 GST_TIME_ARGS (dlbuf->cur_level.time),
361 GST_TIME_ARGS (dlbuf->max_level.time));
362 /* update the buffering */
363 update_buffering (dlbuf);
367 update_levels (GstDownloadBuffer * dlbuf, guint bytes)
369 dlbuf->cur_level.bytes = bytes;
370 update_time_level (dlbuf);
374 update_in_rates (GstDownloadBuffer * dlbuf)
376 gdouble elapsed, period;
377 gdouble byte_in_rate;
379 if (!dlbuf->in_timer_started) {
380 dlbuf->in_timer_started = TRUE;
381 g_timer_start (dlbuf->in_timer);
385 elapsed = g_timer_elapsed (dlbuf->in_timer, NULL);
387 /* recalc after each interval. */
388 if (dlbuf->last_in_elapsed + RATE_INTERVAL < elapsed) {
389 period = elapsed - dlbuf->last_in_elapsed;
391 GST_DEBUG_OBJECT (dlbuf,
392 "rates: period %f, in %" G_GUINT64_FORMAT ", global period %f",
393 period, dlbuf->bytes_in, dlbuf->byte_in_period);
395 byte_in_rate = dlbuf->bytes_in / period;
397 if (dlbuf->byte_in_rate == 0.0)
398 dlbuf->byte_in_rate = byte_in_rate;
400 dlbuf->byte_in_rate = AVG_IN (dlbuf->byte_in_rate, byte_in_rate,
401 (double) dlbuf->byte_in_period, period);
403 /* another data point, cap at 16 for long time running average */
404 if (dlbuf->byte_in_period < 16 * RATE_INTERVAL)
405 dlbuf->byte_in_period += period;
407 /* reset the values to calculate rate over the next interval */
408 dlbuf->last_in_elapsed = elapsed;
410 GST_DEBUG_OBJECT (dlbuf, "rates: in %f", dlbuf->byte_in_rate);
415 update_out_rates (GstDownloadBuffer * dlbuf)
417 gdouble elapsed, period;
418 gdouble byte_out_rate;
420 if (!dlbuf->out_timer_started) {
421 dlbuf->out_timer_started = TRUE;
422 g_timer_start (dlbuf->out_timer);
426 elapsed = g_timer_elapsed (dlbuf->out_timer, NULL);
428 /* recalc after each interval. */
429 if (dlbuf->last_out_elapsed + RATE_INTERVAL < elapsed) {
430 period = elapsed - dlbuf->last_out_elapsed;
432 GST_DEBUG_OBJECT (dlbuf,
433 "rates: period %f, out %" G_GUINT64_FORMAT, period, dlbuf->bytes_out);
435 byte_out_rate = dlbuf->bytes_out / period;
437 if (dlbuf->byte_out_rate == 0.0)
438 dlbuf->byte_out_rate = byte_out_rate;
440 dlbuf->byte_out_rate = AVG_OUT (dlbuf->byte_out_rate, byte_out_rate);
442 /* reset the values to calculate rate over the next interval */
443 dlbuf->last_out_elapsed = elapsed;
444 dlbuf->bytes_out = 0;
445 GST_DEBUG_OBJECT (dlbuf, "rates: out %f", dlbuf->byte_out_rate);
450 get_buffering_percent (GstDownloadBuffer * dlbuf, gboolean * is_buffering,
455 if (dlbuf->high_percent <= 0) {
459 *is_buffering = FALSE;
463 /* Ensure the variables used to calculate buffering state are up-to-date. */
464 update_in_rates (dlbuf);
465 update_out_rates (dlbuf);
467 /* figure out the percent we are filled, we take the max of all formats. */
468 if (dlbuf->max_level.bytes > 0) {
469 if (dlbuf->cur_level.bytes >= dlbuf->max_level.bytes)
472 perc = dlbuf->cur_level.bytes * 100 / dlbuf->max_level.bytes;
476 if (dlbuf->max_level.time > 0) {
477 if (dlbuf->cur_level.time >= dlbuf->max_level.time)
480 perc = MAX (perc, dlbuf->cur_level.time * 100 / dlbuf->max_level.time);
482 perc = MAX (0, perc);
485 *is_buffering = dlbuf->is_buffering;
487 /* scale to high percent so that it becomes the 100% mark */
488 perc = perc * 100 / dlbuf->high_percent;
496 GST_DEBUG_OBJECT (dlbuf, "buffering %d, percent %d", dlbuf->is_buffering,
503 get_buffering_stats (GstDownloadBuffer * dlbuf, gint percent,
504 GstBufferingMode * mode, gint * avg_in, gint * avg_out,
505 gint64 * buffering_left)
508 *mode = GST_BUFFERING_DOWNLOAD;
511 *avg_in = dlbuf->byte_in_rate;
513 *avg_out = dlbuf->byte_out_rate;
515 if (buffering_left) {
518 *buffering_left = (percent == 100 ? 0 : -1);
520 max = dlbuf->max_level.time;
521 cur = dlbuf->cur_level.time;
523 if (percent != 100 && max > cur)
524 *buffering_left = (max - cur) / 1000000;
529 update_buffering (GstDownloadBuffer * dlbuf)
532 gboolean post = FALSE;
534 if (!get_buffering_percent (dlbuf, NULL, &percent))
537 if (dlbuf->is_buffering) {
539 /* if we were buffering see if we reached the high watermark */
540 if (percent >= dlbuf->high_percent)
541 dlbuf->is_buffering = FALSE;
543 /* we were not buffering, check if we need to start buffering if we drop
544 * below the low threshold */
545 if (percent < dlbuf->low_percent) {
546 dlbuf->is_buffering = TRUE;
552 if (percent == dlbuf->buffering_percent)
555 dlbuf->buffering_percent = percent;
560 GstBufferingMode mode;
561 gint avg_in, avg_out;
562 gint64 buffering_left;
564 get_buffering_stats (dlbuf, percent, &mode, &avg_in, &avg_out,
567 message = gst_message_new_buffering (GST_OBJECT_CAST (dlbuf),
569 gst_message_set_buffering_stats (message, mode,
570 avg_in, avg_out, buffering_left);
572 gst_element_post_message (GST_ELEMENT_CAST (dlbuf), message);
577 perform_seek_to_offset (GstDownloadBuffer * dlbuf, guint64 offset)
585 /* until we receive the FLUSH_STOP from this seek, we skip data */
586 dlbuf->seeking = TRUE;
587 dlbuf->write_pos = offset;
588 dlbuf->filling = FALSE;
589 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
591 GST_DEBUG_OBJECT (dlbuf, "Seeking to %" G_GUINT64_FORMAT, offset);
594 gst_event_new_seek (1.0, GST_FORMAT_BYTES,
595 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_SET, offset,
596 GST_SEEK_TYPE_NONE, -1);
598 res = gst_pad_push_event (dlbuf->sinkpad, event);
599 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
604 /* get the threshold for when we decide to seek rather than wait */
606 get_seek_threshold (GstDownloadBuffer * dlbuf)
610 /* FIXME, find a good threshold based on the incoming rate. */
611 threshold = 1024 * 512;
617 gst_download_buffer_update_upstream_size (GstDownloadBuffer * dlbuf)
619 gint64 upstream_size = 0;
621 if (gst_pad_peer_query_duration (dlbuf->sinkpad, GST_FORMAT_BYTES,
623 GST_INFO_OBJECT (dlbuf, "upstream size: %" G_GINT64_FORMAT, upstream_size);
624 dlbuf->upstream_size = upstream_size;
628 /* called with DOWNLOAD_BUFFER_MUTEX */
630 gst_download_buffer_wait_for_data (GstDownloadBuffer * dlbuf, guint64 offset,
637 GST_DEBUG_OBJECT (dlbuf, "wait for %" G_GUINT64_FORMAT ", length %u",
640 wanted = offset + length;
642 /* pause the timer while we wait. The fact that we are waiting does not mean
643 * the byterate on the output pad is lower */
644 if ((started = dlbuf->out_timer_started))
645 g_timer_stop (dlbuf->out_timer);
647 /* check range before us */
648 if (gst_sparse_file_get_range_before (dlbuf->file, offset, &start, &stop)) {
649 GST_DEBUG_OBJECT (dlbuf,
650 "range before %" G_GSIZE_FORMAT " - %" G_GSIZE_FORMAT, start, stop);
651 if (start <= offset && offset < stop) {
652 GST_DEBUG_OBJECT (dlbuf, "we have the offset");
653 /* we have the range, continue it */
656 guint64 threshold, dist;
658 /* there is a range before us, check how far away it is */
659 threshold = get_seek_threshold (dlbuf);
660 dist = offset - stop;
662 if (dist <= threshold) {
663 GST_DEBUG_OBJECT (dlbuf, "not too far");
664 /* not far away, continue it */
670 if (dlbuf->write_pos != offset)
671 perform_seek_to_offset (dlbuf, offset);
673 dlbuf->filling = TRUE;
674 if (dlbuf->write_pos > dlbuf->read_pos)
675 update_levels (dlbuf, dlbuf->write_pos - dlbuf->read_pos);
677 update_levels (dlbuf, 0);
679 /* now wait for more data */
680 GST_DEBUG_OBJECT (dlbuf, "waiting for more data");
681 GST_DOWNLOAD_BUFFER_WAIT_ADD_CHECK (dlbuf, dlbuf->srcresult, wanted,
683 GST_DEBUG_OBJECT (dlbuf, "got more data");
685 /* and continue if we were running before */
687 g_timer_continue (dlbuf->out_timer);
693 GST_DEBUG_OBJECT (dlbuf, "we are flushing");
694 return GST_FLOW_FLUSHING;
699 check_upstream_size (GstDownloadBuffer * dlbuf, gsize offset, guint * length)
701 gsize stop = offset + *length;
702 /* catch any reads beyond the size of the file here to make sure cache
703 * doesn't send seek events beyond the size of the file upstream, since
704 * that would confuse elements such as souphttpsrc and/or http servers.
705 * Demuxers often just loop until EOS at the end of the file to figure out
706 * when they've read all the end-headers or index chunks. */
707 if (G_UNLIKELY (dlbuf->upstream_size == -1 || stop >= dlbuf->upstream_size)) {
708 gst_download_buffer_update_upstream_size (dlbuf);
711 if (dlbuf->upstream_size != -1) {
712 if (offset >= dlbuf->upstream_size)
715 if (G_UNLIKELY (stop > dlbuf->upstream_size)) {
716 *length = dlbuf->upstream_size - offset;
717 GST_DEBUG_OBJECT (dlbuf, "adjusting length downto %u", *length);
723 /* called with DOWNLOAD_BUFFER_MUTEX */
725 gst_download_buffer_read_buffer (GstDownloadBuffer * dlbuf, guint64 offset,
726 guint length, GstBuffer ** buffer)
730 GstFlowReturn ret = GST_FLOW_OK;
731 gsize res, remaining;
732 GError *error = NULL;
734 length = (length == -1) ? DEFAULT_BUFFER_SIZE : length;
735 offset = (offset == -1) ? dlbuf->read_pos : offset;
737 if (!check_upstream_size (dlbuf, offset, &length))
740 /* allocate the output buffer of the requested size */
742 buf = gst_buffer_new_allocate (NULL, length, NULL);
746 gst_buffer_map (buf, &info, GST_MAP_WRITE);
748 GST_DEBUG_OBJECT (dlbuf, "Reading %u bytes from %" G_GUINT64_FORMAT, length,
751 dlbuf->read_pos = offset;
755 gst_sparse_file_read (dlbuf->file, offset, info.data, length,
757 if (G_UNLIKELY (res == 0)) {
758 switch (error->code) {
759 case G_IO_ERROR_WOULD_BLOCK:
760 /* we don't have the requested data in the file, decide what to
762 ret = gst_download_buffer_wait_for_data (dlbuf, offset, length);
763 if (ret != GST_FLOW_OK)
769 g_clear_error (&error);
773 gst_buffer_unmap (buf, &info);
774 gst_buffer_resize (buf, 0, res);
776 dlbuf->bytes_out += res;
777 dlbuf->read_pos += res;
779 GST_DEBUG_OBJECT (dlbuf,
780 "Read %" G_GSIZE_FORMAT " bytes, remaining %" G_GSIZE_FORMAT, res,
783 if (dlbuf->read_pos + remaining == dlbuf->upstream_size)
784 update_levels (dlbuf, dlbuf->max_level.bytes);
786 update_levels (dlbuf, remaining);
788 GST_BUFFER_OFFSET (buf) = offset;
789 GST_BUFFER_OFFSET_END (buf) = offset + res;
798 GST_DEBUG_OBJECT (dlbuf, "EOS hit");
803 GST_DEBUG_OBJECT (dlbuf, "we are flushing");
804 gst_buffer_unmap (buf, &info);
806 gst_buffer_unref (buf);
807 return GST_FLOW_FLUSHING;
811 GST_DEBUG_OBJECT (dlbuf, "we have a read error: %s", error->message);
812 g_clear_error (&error);
813 gst_buffer_unmap (buf, &info);
815 gst_buffer_unref (buf);
820 /* must be called with MUTEX_LOCK. Will briefly release the lock when notifying
821 * the temp filename. */
823 gst_download_buffer_open_temp_location_file (GstDownloadBuffer * dlbuf)
831 GST_DEBUG_OBJECT (dlbuf, "opening temp file %s", dlbuf->temp_template);
833 /* If temp_template was set, allocate a filename and open that filen */
836 if (dlbuf->temp_template == NULL)
839 /* make copy of the template, we don't want to change this */
840 name = g_strdup (dlbuf->temp_template);
841 fd = g_mkstemp (name);
845 /* open the file for update/writing */
846 dlbuf->file = gst_sparse_file_new ();
847 /* error creating file */
848 if (!gst_sparse_file_set_fd (dlbuf->file, fd))
851 g_free (dlbuf->temp_location);
852 dlbuf->temp_location = name;
855 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
857 /* we can't emit the notify with the lock */
858 g_object_notify (G_OBJECT (dlbuf), "temp-location");
860 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
862 GST_DEBUG_OBJECT (dlbuf, "opened temp file %s", dlbuf->temp_template);
869 GST_DEBUG_OBJECT (dlbuf, "temp file was already open");
874 GST_ELEMENT_ERROR (dlbuf, RESOURCE, NOT_FOUND,
875 (_("No Temp directory specified.")), (NULL));
880 GST_ELEMENT_ERROR (dlbuf, RESOURCE, OPEN_READ,
881 (_("Could not create temp file \"%s\"."), dlbuf->temp_template),
888 GST_ELEMENT_ERROR (dlbuf, RESOURCE, OPEN_READ,
889 (_("Could not open file \"%s\" for reading."), name), GST_ERROR_SYSTEM);
898 gst_download_buffer_close_temp_location_file (GstDownloadBuffer * dlbuf)
901 if (dlbuf->file == NULL)
904 GST_DEBUG_OBJECT (dlbuf, "closing sparse file");
906 if (dlbuf->temp_remove) {
907 if (remove (dlbuf->temp_location) < 0) {
908 GST_WARNING_OBJECT (dlbuf, "Failed to remove temporary file %s: %s",
909 dlbuf->temp_location, g_strerror (errno));
912 gst_sparse_file_free (dlbuf->file);
913 close (dlbuf->temp_fd);
918 gst_download_buffer_flush_temp_file (GstDownloadBuffer * dlbuf)
920 if (dlbuf->file == NULL)
923 GST_DEBUG_OBJECT (dlbuf, "flushing temp file");
925 gst_sparse_file_clear (dlbuf->file);
929 gst_download_buffer_locked_flush (GstDownloadBuffer * dlbuf, gboolean full,
933 gst_download_buffer_flush_temp_file (dlbuf);
934 GST_DOWNLOAD_BUFFER_CLEAR_LEVEL (dlbuf->cur_level);
935 gst_event_replace (&dlbuf->stream_start_event, NULL);
936 gst_event_replace (&dlbuf->segment_event, NULL);
940 gst_download_buffer_handle_sink_event (GstPad * pad, GstObject * parent,
944 GstDownloadBuffer *dlbuf;
946 dlbuf = GST_DOWNLOAD_BUFFER (parent);
948 switch (GST_EVENT_TYPE (event)) {
949 case GST_EVENT_FLUSH_START:
951 GST_LOG_OBJECT (dlbuf, "received flush start event");
952 if (GST_PAD_MODE (dlbuf->srcpad) == GST_PAD_MODE_PUSH) {
954 ret = gst_pad_push_event (dlbuf->srcpad, event);
956 /* now unblock the chain function */
957 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
958 dlbuf->srcresult = GST_FLOW_FLUSHING;
959 dlbuf->sinkresult = GST_FLOW_FLUSHING;
960 /* unblock the loop and chain functions */
961 GST_DOWNLOAD_BUFFER_SIGNAL_ADD (dlbuf, -1);
962 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
964 /* make sure it pauses, this should happen since we sent
965 * flush_start downstream. */
966 gst_pad_pause_task (dlbuf->srcpad);
967 GST_LOG_OBJECT (dlbuf, "loop stopped");
969 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
970 /* flush the sink pad */
971 dlbuf->sinkresult = GST_FLOW_FLUSHING;
972 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
974 gst_event_unref (event);
978 case GST_EVENT_FLUSH_STOP:
980 GST_LOG_OBJECT (dlbuf, "received flush stop event");
982 if (GST_PAD_MODE (dlbuf->srcpad) == GST_PAD_MODE_PUSH) {
984 ret = gst_pad_push_event (dlbuf->srcpad, event);
986 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
987 gst_download_buffer_locked_flush (dlbuf, FALSE, TRUE);
988 dlbuf->srcresult = GST_FLOW_OK;
989 dlbuf->sinkresult = GST_FLOW_OK;
990 dlbuf->unexpected = FALSE;
991 dlbuf->seeking = FALSE;
992 /* reset rate counters */
993 reset_rate_timer (dlbuf);
994 gst_pad_start_task (dlbuf->srcpad,
995 (GstTaskFunction) gst_download_buffer_loop, dlbuf->srcpad, NULL);
996 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
998 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
999 dlbuf->unexpected = FALSE;
1000 dlbuf->sinkresult = GST_FLOW_OK;
1001 dlbuf->seeking = FALSE;
1002 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1004 gst_event_unref (event);
1009 if (GST_EVENT_IS_SERIALIZED (event)) {
1010 /* serialized events go in the buffer */
1011 GST_DOWNLOAD_BUFFER_MUTEX_LOCK_CHECK (dlbuf, dlbuf->sinkresult,
1013 switch (GST_EVENT_TYPE (event)) {
1015 GST_DEBUG_OBJECT (dlbuf, "we have EOS");
1016 /* Zero the thresholds, this makes sure the dlbuf is completely
1017 * filled and we can read all data from the dlbuf. */
1018 /* update the buffering status */
1019 update_levels (dlbuf, dlbuf->max_level.bytes);
1021 case GST_EVENT_SEGMENT:
1022 gst_event_replace (&dlbuf->segment_event, event);
1023 /* a new segment allows us to accept more buffers if we got EOS
1024 * from downstream */
1025 dlbuf->unexpected = FALSE;
1027 case GST_EVENT_STREAM_START:
1028 gst_event_replace (&dlbuf->stream_start_event, event);
1033 gst_event_unref (event);
1034 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1036 /* non-serialized events are passed upstream. */
1037 ret = gst_pad_push_event (dlbuf->srcpad, event);
1046 GST_DEBUG_OBJECT (dlbuf, "refusing event, we are flushing");
1047 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1048 gst_event_unref (event);
1054 gst_download_buffer_handle_sink_query (GstPad * pad, GstObject * parent,
1057 GstDownloadBuffer *dlbuf;
1060 dlbuf = GST_DOWNLOAD_BUFFER (parent);
1062 switch (GST_QUERY_TYPE (query)) {
1064 if (GST_QUERY_IS_SERIALIZED (query)) {
1065 GST_LOG_OBJECT (dlbuf, "received query %p", query);
1066 GST_DEBUG_OBJECT (dlbuf, "refusing query, we are not using the dlbuf");
1069 res = gst_pad_query_default (pad, parent, query);
1076 static GstFlowReturn
1077 gst_download_buffer_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1079 GstDownloadBuffer *dlbuf;
1082 gsize res, available;
1083 GError *error = NULL;
1085 dlbuf = GST_DOWNLOAD_BUFFER (parent);
1087 GST_LOG_OBJECT (dlbuf, "received buffer %p of "
1088 "size %" G_GSIZE_FORMAT ", time %" GST_TIME_FORMAT ", duration %"
1089 GST_TIME_FORMAT, buffer, gst_buffer_get_size (buffer),
1090 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1091 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1093 /* we have to lock the dlbuf since we span threads */
1094 GST_DOWNLOAD_BUFFER_MUTEX_LOCK_CHECK (dlbuf, dlbuf->sinkresult, out_flushing);
1095 /* when we received unexpected from downstream, refuse more buffers */
1096 if (dlbuf->unexpected)
1099 /* while we didn't receive the newsegment, we're seeking and we skip data */
1103 /* put buffer in dlbuf now */
1104 offset = dlbuf->write_pos;
1107 if (GST_BUFFER_OFFSET_IS_VALID (buffer) &&
1108 GST_BUFFER_OFFSET (buffer) != offset) {
1109 GST_WARNING_OBJECT (dlbuf, "buffer offset does not match current writing "
1110 "position! %" G_GINT64_FORMAT " != %" G_GINT64_FORMAT,
1111 GST_BUFFER_OFFSET (buffer), offset);
1114 gst_buffer_map (buffer, &info, GST_MAP_READ);
1116 GST_DEBUG_OBJECT (dlbuf, "Writing %" G_GSIZE_FORMAT " bytes to %"
1117 G_GUINT64_FORMAT, info.size, offset);
1120 gst_sparse_file_write (dlbuf->file, offset, info.data, info.size,
1121 &available, &error);
1125 dlbuf->write_pos = offset + info.size;
1126 dlbuf->bytes_in += info.size;
1128 GST_DOWNLOAD_BUFFER_SIGNAL_ADD (dlbuf, dlbuf->write_pos + available);
1130 /* we hit the end, see what to do */
1131 if (dlbuf->write_pos + available == dlbuf->upstream_size) {
1134 /* we have everything up to the end, find a region to fill */
1135 if (gst_sparse_file_get_range_after (dlbuf->file, 0, &start, &stop)) {
1136 if (stop < dlbuf->upstream_size) {
1137 /* a hole to fill, seek to its end */
1138 perform_seek_to_offset (dlbuf, stop);
1140 /* we filled all the holes, post a message */
1141 dlbuf->filling = FALSE;
1142 update_levels (dlbuf, dlbuf->max_level.bytes);
1143 gst_element_post_message (GST_ELEMENT_CAST (dlbuf),
1144 gst_message_new_element (GST_OBJECT_CAST (dlbuf),
1145 gst_structure_new ("GstCacheDownloadComplete",
1146 "location", G_TYPE_STRING, dlbuf->temp_location, NULL)));
1150 /* see if we need to skip this region or just read it again. The idea
1151 * is that when the region is not big, we want to avoid a seek and just
1153 guint64 threshold = get_seek_threshold (dlbuf);
1155 if (available > threshold) {
1156 /* further than threshold, it's better to skip than to reread */
1157 perform_seek_to_offset (dlbuf, dlbuf->write_pos + available);
1160 if (dlbuf->filling) {
1161 if (dlbuf->write_pos > dlbuf->read_pos)
1162 update_levels (dlbuf, dlbuf->write_pos - dlbuf->read_pos);
1164 update_levels (dlbuf, 0);
1167 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1169 gst_buffer_unmap (buffer, &info);
1170 gst_buffer_unref (buffer);
1177 GstFlowReturn ret = dlbuf->sinkresult;
1178 GST_LOG_OBJECT (dlbuf,
1179 "exit because task paused, reason: %s", gst_flow_get_name (ret));
1180 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1181 gst_buffer_unref (buffer);
1186 GST_LOG_OBJECT (dlbuf, "exit because we received EOS");
1187 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1188 gst_buffer_unref (buffer);
1189 return GST_FLOW_EOS;
1193 GST_LOG_OBJECT (dlbuf, "exit because we are seeking");
1194 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1195 gst_buffer_unref (buffer);
1200 gst_buffer_unmap (buffer, &info);
1201 gst_buffer_unref (buffer);
1202 GST_ELEMENT_ERROR (dlbuf, RESOURCE, WRITE,
1203 (_("Error while writing to download file.")), ("%s", error->message));
1204 g_clear_error (&error);
1205 return GST_FLOW_ERROR;
1209 /* called repeatedly with @pad as the source pad. This function should push out
1210 * data to the peer element. */
1212 gst_download_buffer_loop (GstPad * pad)
1214 GstDownloadBuffer *dlbuf;
1216 GstBuffer *buffer = NULL;
1218 dlbuf = GST_DOWNLOAD_BUFFER (GST_PAD_PARENT (pad));
1220 /* have to lock for thread-safety */
1221 GST_DOWNLOAD_BUFFER_MUTEX_LOCK_CHECK (dlbuf, dlbuf->srcresult, out_flushing);
1223 if (dlbuf->stream_start_event != NULL) {
1224 gst_pad_push_event (dlbuf->srcpad, dlbuf->stream_start_event);
1225 dlbuf->stream_start_event = NULL;
1227 if (dlbuf->segment_event != NULL) {
1228 gst_pad_push_event (dlbuf->srcpad, dlbuf->segment_event);
1229 dlbuf->segment_event = NULL;
1232 ret = gst_download_buffer_read_buffer (dlbuf, -1, -1, &buffer);
1233 if (ret != GST_FLOW_OK)
1236 g_atomic_int_set (&dlbuf->downstream_may_block, 1);
1237 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1239 ret = gst_pad_push (dlbuf->srcpad, buffer);
1240 g_atomic_int_set (&dlbuf->downstream_may_block, 0);
1242 /* need to check for srcresult here as well */
1243 GST_DOWNLOAD_BUFFER_MUTEX_LOCK_CHECK (dlbuf, dlbuf->srcresult, out_flushing);
1244 dlbuf->srcresult = ret;
1245 dlbuf->sinkresult = ret;
1246 if (ret != GST_FLOW_OK)
1248 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1255 GstFlowReturn ret = dlbuf->srcresult;
1257 gst_pad_pause_task (dlbuf->srcpad);
1258 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1259 GST_LOG_OBJECT (dlbuf, "pause task, reason: %s", gst_flow_get_name (ret));
1260 /* let app know about us giving up if upstream is not expected to do so */
1261 if (ret == GST_FLOW_EOS) {
1262 /* FIXME perform EOS logic, this is really a basesrc operating on a
1264 gst_pad_push_event (dlbuf->srcpad, gst_event_new_eos ());
1265 } else if ((ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS)) {
1266 GST_ELEMENT_ERROR (dlbuf, STREAM, FAILED,
1267 (_("Internal data flow error.")),
1268 ("streaming task paused, reason %s (%d)",
1269 gst_flow_get_name (ret), ret));
1270 gst_pad_push_event (dlbuf->srcpad, gst_event_new_eos ());
1277 gst_download_buffer_handle_src_event (GstPad * pad, GstObject * parent,
1280 gboolean res = TRUE;
1281 GstDownloadBuffer *dlbuf = GST_DOWNLOAD_BUFFER (parent);
1283 #ifndef GST_DISABLE_GST_DEBUG
1284 GST_DEBUG_OBJECT (dlbuf, "got event %p (%s)",
1285 event, GST_EVENT_TYPE_NAME (event));
1288 switch (GST_EVENT_TYPE (event)) {
1289 case GST_EVENT_FLUSH_START:
1290 /* now unblock the getrange function */
1291 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1292 GST_DEBUG_OBJECT (dlbuf, "flushing");
1293 dlbuf->srcresult = GST_FLOW_FLUSHING;
1294 GST_DOWNLOAD_BUFFER_SIGNAL_ADD (dlbuf, -1);
1295 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1297 /* when using a temp file, we eat the event */
1299 gst_event_unref (event);
1301 case GST_EVENT_FLUSH_STOP:
1302 /* now unblock the getrange function */
1303 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1304 dlbuf->srcresult = GST_FLOW_OK;
1305 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1307 /* when using a temp file, we eat the event */
1309 gst_event_unref (event);
1311 case GST_EVENT_RECONFIGURE:
1312 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1313 /* assume downstream is linked now and try to push again */
1314 if (dlbuf->srcresult == GST_FLOW_NOT_LINKED) {
1315 dlbuf->srcresult = GST_FLOW_OK;
1316 dlbuf->sinkresult = GST_FLOW_OK;
1317 if (GST_PAD_MODE (pad) == GST_PAD_MODE_PUSH) {
1318 gst_pad_start_task (pad, (GstTaskFunction) gst_download_buffer_loop,
1322 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1324 res = gst_pad_push_event (dlbuf->sinkpad, event);
1327 res = gst_pad_push_event (dlbuf->sinkpad, event);
1335 gst_download_buffer_handle_src_query (GstPad * pad, GstObject * parent,
1338 GstDownloadBuffer *dlbuf;
1340 dlbuf = GST_DOWNLOAD_BUFFER (parent);
1342 switch (GST_QUERY_TYPE (query)) {
1343 case GST_QUERY_POSITION:
1348 if (!gst_pad_peer_query (dlbuf->sinkpad, query))
1351 /* get peer position */
1352 gst_query_parse_position (query, &format, &peer_pos);
1354 /* FIXME: this code assumes that there's no discont in the dlbuf */
1356 case GST_FORMAT_BYTES:
1357 peer_pos -= dlbuf->cur_level.bytes;
1359 case GST_FORMAT_TIME:
1360 peer_pos -= dlbuf->cur_level.time;
1363 GST_WARNING_OBJECT (dlbuf, "dropping query in %s format, don't "
1364 "know how to adjust value", gst_format_get_name (format));
1367 /* set updated position */
1368 gst_query_set_position (query, format, peer_pos);
1371 case GST_QUERY_DURATION:
1373 GST_DEBUG_OBJECT (dlbuf, "doing peer query");
1375 if (!gst_pad_peer_query (dlbuf->sinkpad, query))
1378 GST_DEBUG_OBJECT (dlbuf, "peer query success");
1381 case GST_QUERY_BUFFERING:
1384 gboolean is_buffering;
1385 GstBufferingMode mode;
1386 gint avg_in, avg_out;
1387 gint64 buffering_left;
1389 GST_DEBUG_OBJECT (dlbuf, "query buffering");
1391 get_buffering_percent (dlbuf, &is_buffering, &percent);
1392 gst_query_set_buffering_percent (query, is_buffering, percent);
1394 get_buffering_stats (dlbuf, percent, &mode, &avg_in, &avg_out,
1396 gst_query_set_buffering_stats (query, mode, avg_in, avg_out,
1400 /* add ranges for download and ringbuffer buffering */
1404 gint64 estimated_total;
1406 gsize offset, range_start, range_stop;
1408 write_pos = dlbuf->write_pos;
1410 /* get duration of upstream in bytes */
1411 gst_download_buffer_update_upstream_size (dlbuf);
1412 duration = dlbuf->upstream_size;
1414 GST_DEBUG_OBJECT (dlbuf, "percent %d, duration %" G_GINT64_FORMAT
1415 ", writing %" G_GINT64_FORMAT, percent, duration, write_pos);
1417 /* calculate remaining and total download time */
1418 if (duration > write_pos && avg_in > 0.0)
1419 estimated_total = ((duration - write_pos) * 1000) / avg_in;
1421 estimated_total = -1;
1423 GST_DEBUG_OBJECT (dlbuf, "estimated-total %" G_GINT64_FORMAT,
1426 gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
1429 case GST_FORMAT_PERCENT:
1431 /* get our available data relative to the duration */
1434 gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, write_pos,
1439 case GST_FORMAT_BYTES:
1449 gst_query_set_buffering_range (query, format, start, stop,
1452 /* fill out the buffered ranges */
1454 while (gst_sparse_file_get_range_after (dlbuf->file, offset,
1455 &range_start, &range_stop)) {
1456 offset = range_stop;
1459 case GST_FORMAT_PERCENT:
1460 if (duration == -1) {
1464 range_start = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
1465 range_start, duration);
1466 range_stop = gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX,
1467 range_stop, duration);
1470 case GST_FORMAT_BYTES:
1477 if (range_start == range_stop)
1479 GST_DEBUG_OBJECT (dlbuf,
1480 "range starting at %" G_GINT64_FORMAT " and finishing at %"
1481 G_GINT64_FORMAT, range_start, range_stop);
1482 gst_query_add_buffering_range (query, range_start, range_stop);
1487 case GST_QUERY_SCHEDULING:
1489 GstSchedulingFlags flags = 0;
1491 if (!gst_pad_peer_query (dlbuf->sinkpad, query))
1494 gst_query_parse_scheduling (query, &flags, NULL, NULL, NULL);
1496 /* we can operate in pull mode when we are using a tempfile */
1497 flags |= GST_SCHEDULING_FLAG_SEEKABLE;
1498 gst_query_set_scheduling (query, flags, 0, -1, 0);
1499 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PULL);
1500 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
1504 /* peer handled other queries */
1505 if (!gst_pad_query_default (pad, parent, query))
1515 GST_DEBUG_OBJECT (dlbuf, "failed peer query");
1521 gst_download_buffer_handle_query (GstElement * element, GstQuery * query)
1523 GstDownloadBuffer *dlbuf = GST_DOWNLOAD_BUFFER (element);
1525 /* simply forward to the srcpad query function */
1526 return gst_download_buffer_handle_src_query (dlbuf->srcpad,
1527 GST_OBJECT_CAST (element), query);
1530 static GstFlowReturn
1531 gst_download_buffer_get_range (GstPad * pad, GstObject * parent, guint64 offset,
1532 guint length, GstBuffer ** buffer)
1534 GstDownloadBuffer *dlbuf;
1537 dlbuf = GST_DOWNLOAD_BUFFER_CAST (parent);
1539 GST_DOWNLOAD_BUFFER_MUTEX_LOCK_CHECK (dlbuf, dlbuf->srcresult, out_flushing);
1540 /* FIXME - function will block when the range is not yet available */
1541 ret = gst_download_buffer_read_buffer (dlbuf, offset, length, buffer);
1542 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1549 ret = dlbuf->srcresult;
1551 GST_DEBUG_OBJECT (dlbuf, "we are flushing");
1552 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1557 /* sink currently only operates in push mode */
1559 gst_download_buffer_sink_activate_mode (GstPad * pad, GstObject * parent,
1560 GstPadMode mode, gboolean active)
1563 GstDownloadBuffer *dlbuf;
1565 dlbuf = GST_DOWNLOAD_BUFFER (parent);
1568 case GST_PAD_MODE_PUSH:
1570 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1571 GST_DEBUG_OBJECT (dlbuf, "activating push mode");
1572 dlbuf->srcresult = GST_FLOW_OK;
1573 dlbuf->sinkresult = GST_FLOW_OK;
1574 dlbuf->unexpected = FALSE;
1575 reset_rate_timer (dlbuf);
1576 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1578 /* unblock chain function */
1579 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1580 GST_DEBUG_OBJECT (dlbuf, "deactivating push mode");
1581 dlbuf->srcresult = GST_FLOW_FLUSHING;
1582 dlbuf->sinkresult = GST_FLOW_FLUSHING;
1583 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1585 /* wait until it is unblocked and clean up */
1586 GST_PAD_STREAM_LOCK (pad);
1587 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1588 gst_download_buffer_locked_flush (dlbuf, TRUE, FALSE);
1589 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1590 GST_PAD_STREAM_UNLOCK (pad);
1601 /* src operating in push mode, we start a task on the source pad that pushes out
1602 * buffers from the dlbuf */
1604 gst_download_buffer_src_activate_push (GstPad * pad, GstObject * parent,
1607 gboolean result = FALSE;
1608 GstDownloadBuffer *dlbuf;
1610 dlbuf = GST_DOWNLOAD_BUFFER (parent);
1613 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1614 GST_DEBUG_OBJECT (dlbuf, "activating push mode");
1615 dlbuf->srcresult = GST_FLOW_OK;
1616 dlbuf->sinkresult = GST_FLOW_OK;
1617 dlbuf->unexpected = FALSE;
1619 gst_pad_start_task (pad, (GstTaskFunction) gst_download_buffer_loop,
1621 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1623 /* unblock loop function */
1624 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1625 GST_DEBUG_OBJECT (dlbuf, "deactivating push mode");
1626 dlbuf->srcresult = GST_FLOW_FLUSHING;
1627 dlbuf->sinkresult = GST_FLOW_FLUSHING;
1628 /* the item add signal will unblock */
1629 GST_DOWNLOAD_BUFFER_SIGNAL_ADD (dlbuf, -1);
1630 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1632 /* step 2, make sure streaming finishes */
1633 result = gst_pad_stop_task (pad);
1639 /* pull mode, downstream will call our getrange function */
1641 gst_download_buffer_src_activate_pull (GstPad * pad, GstObject * parent,
1645 GstDownloadBuffer *dlbuf;
1647 dlbuf = GST_DOWNLOAD_BUFFER (parent);
1650 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1651 /* open the temp file now */
1652 result = gst_download_buffer_open_temp_location_file (dlbuf);
1653 GST_DEBUG_OBJECT (dlbuf, "activating pull mode");
1654 dlbuf->srcresult = GST_FLOW_OK;
1655 dlbuf->sinkresult = GST_FLOW_OK;
1656 dlbuf->unexpected = FALSE;
1657 dlbuf->upstream_size = 0;
1658 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1660 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1661 GST_DEBUG_OBJECT (dlbuf, "deactivating pull mode");
1662 dlbuf->srcresult = GST_FLOW_FLUSHING;
1663 dlbuf->sinkresult = GST_FLOW_FLUSHING;
1664 /* this will unlock getrange */
1665 GST_DOWNLOAD_BUFFER_SIGNAL_ADD (dlbuf, -1);
1667 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1674 gst_download_buffer_src_activate_mode (GstPad * pad, GstObject * parent,
1675 GstPadMode mode, gboolean active)
1680 case GST_PAD_MODE_PULL:
1681 res = gst_download_buffer_src_activate_pull (pad, parent, active);
1683 case GST_PAD_MODE_PUSH:
1684 res = gst_download_buffer_src_activate_push (pad, parent, active);
1687 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
1694 static GstStateChangeReturn
1695 gst_download_buffer_change_state (GstElement * element,
1696 GstStateChange transition)
1698 GstDownloadBuffer *dlbuf;
1699 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1701 dlbuf = GST_DOWNLOAD_BUFFER (element);
1703 switch (transition) {
1704 case GST_STATE_CHANGE_NULL_TO_READY:
1706 case GST_STATE_CHANGE_READY_TO_PAUSED:
1707 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1708 if (!gst_download_buffer_open_temp_location_file (dlbuf))
1709 ret = GST_STATE_CHANGE_FAILURE;
1710 gst_event_replace (&dlbuf->stream_start_event, NULL);
1711 gst_event_replace (&dlbuf->segment_event, NULL);
1712 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1714 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1720 if (ret == GST_STATE_CHANGE_FAILURE)
1723 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1725 if (ret == GST_STATE_CHANGE_FAILURE)
1728 switch (transition) {
1729 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1731 case GST_STATE_CHANGE_PAUSED_TO_READY:
1732 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1733 gst_download_buffer_close_temp_location_file (dlbuf);
1734 gst_event_replace (&dlbuf->stream_start_event, NULL);
1735 gst_event_replace (&dlbuf->segment_event, NULL);
1736 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1738 case GST_STATE_CHANGE_READY_TO_NULL:
1747 #define CAPACITY_CHANGE(elem) \
1748 update_buffering (elem);
1751 gst_download_buffer_set_temp_template (GstDownloadBuffer * dlbuf,
1752 const gchar * template)
1756 /* the element must be stopped in order to do this */
1757 GST_OBJECT_LOCK (dlbuf);
1758 state = GST_STATE (dlbuf);
1759 if (state != GST_STATE_READY && state != GST_STATE_NULL)
1761 GST_OBJECT_UNLOCK (dlbuf);
1763 /* set new location */
1764 g_free (dlbuf->temp_template);
1765 dlbuf->temp_template = g_strdup (template);
1772 GST_WARNING_OBJECT (dlbuf, "setting temp-template property in wrong state");
1773 GST_OBJECT_UNLOCK (dlbuf);
1778 gst_download_buffer_set_property (GObject * object,
1779 guint prop_id, const GValue * value, GParamSpec * pspec)
1781 GstDownloadBuffer *dlbuf = GST_DOWNLOAD_BUFFER (object);
1783 /* someone could change levels here, and since this
1784 * affects the get/put funcs, we need to lock for safety. */
1785 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1788 case PROP_MAX_SIZE_BYTES:
1789 dlbuf->max_level.bytes = g_value_get_uint (value);
1790 CAPACITY_CHANGE (dlbuf);
1792 case PROP_MAX_SIZE_TIME:
1793 dlbuf->max_level.time = g_value_get_uint64 (value);
1794 CAPACITY_CHANGE (dlbuf);
1796 case PROP_LOW_PERCENT:
1797 dlbuf->low_percent = g_value_get_int (value);
1799 case PROP_HIGH_PERCENT:
1800 dlbuf->high_percent = g_value_get_int (value);
1802 case PROP_TEMP_TEMPLATE:
1803 gst_download_buffer_set_temp_template (dlbuf, g_value_get_string (value));
1805 case PROP_TEMP_REMOVE:
1806 dlbuf->temp_remove = g_value_get_boolean (value);
1809 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1813 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
1817 gst_download_buffer_get_property (GObject * object,
1818 guint prop_id, GValue * value, GParamSpec * pspec)
1820 GstDownloadBuffer *dlbuf = GST_DOWNLOAD_BUFFER (object);
1822 GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
1825 case PROP_MAX_SIZE_BYTES:
1826 g_value_set_uint (value, dlbuf->max_level.bytes);
1828 case PROP_MAX_SIZE_TIME:
1829 g_value_set_uint64 (value, dlbuf->max_level.time);
1831 case PROP_LOW_PERCENT:
1832 g_value_set_int (value, dlbuf->low_percent);
1834 case PROP_HIGH_PERCENT:
1835 g_value_set_int (value, dlbuf->high_percent);
1837 case PROP_TEMP_TEMPLATE:
1838 g_value_set_string (value, dlbuf->temp_template);
1840 case PROP_TEMP_LOCATION:
1841 g_value_set_string (value, dlbuf->temp_location);
1843 case PROP_TEMP_REMOVE:
1844 g_value_set_boolean (value, dlbuf->temp_remove);
1847 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1851 GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);