downloadbuffer: improve start/stop in buffering query
authorWim Taymans <wtaymans@redhat.com>
Tue, 3 Jun 2014 20:12:13 +0000 (22:12 +0200)
committerWim Taymans <wtaymans@redhat.com>
Tue, 3 Jun 2014 20:17:17 +0000 (22:17 +0200)
The start and stop should represent the currently downloading region.
The estimated-total should represent the remaining time to download
the currently downloading region. This makes it a lot more useful
for applications because they can then use those values to update
the fill region and use the estimated time to delay playback.
Update the docs with this clarification.

docs/design/part-buffering.txt
plugins/elements/gstdownloadbuffer.c

index c88dab8..2a2093b 100644 (file)
@@ -196,19 +196,23 @@ range in a specific format and the estimated time to complete:
     - the format of the "start" and "stop" values below  
    
   "start", G_TYPE_INT64, -1 unknown
-    - the start position of the available data
+    - the start position of the available data. If there are multiple ranges,
+      this field contains the start position of the currently downloading
+      range.
 
   "stop", G_TYPE_INT64, -1 unknown
-    - the stop position of the available data
+    - the stop position of the available data. If there are multiple ranges,
+      this field contains the stop position of the currently downloading
+      range.
 
   "estimated-total", G_TYPE_INT64
     - gives the estimated download time in milliseconds. -1 unknown.
 
     When the size of the downloaded file is known, this value will contain
-    the latest estimate of the remaining download time. This value is usually
-    only filled for the "download" buffering mode. The application can use
-    this information to estimate the amount of remaining time to download the
-    complete file.
+    the latest estimate of the remaining download time of the currently
+    downloading range. This value is usually only filled for the "download"
+    buffering mode. The application can use this information to estimate the
+    amount of remaining time to download till the end of the file.
 
   "buffering-ranges", G_TYPE_ARRAY of GstQueryBufferingRange
     - contains optionally the downloaded areas in the format given above. One
index 73e0f7f..58e0722 100644 (file)
@@ -654,6 +654,7 @@ get_seek_threshold (GstDownloadBuffer * dlbuf)
   return threshold;
 }
 
+/* called with DOWNLOAD_BUFFER_MUTEX */
 static void
 gst_download_buffer_update_upstream_size (GstDownloadBuffer * dlbuf)
 {
@@ -736,6 +737,7 @@ out_flushing:
   }
 }
 
+/* called with DOWNLOAD_BUFFER_MUTEX */
 static gboolean
 check_upstream_size (GstDownloadBuffer * dlbuf, gsize offset, guint * length)
 {
@@ -1458,6 +1460,7 @@ gst_download_buffer_handle_src_query (GstPad * pad, GstObject * parent,
         gint64 duration;
         gsize offset, range_start, range_stop;
 
+        GST_DOWNLOAD_BUFFER_MUTEX_LOCK (dlbuf);
         write_pos = dlbuf->write_pos;
 
         /* get duration of upstream in bytes */
@@ -1467,49 +1470,36 @@ gst_download_buffer_handle_src_query (GstPad * pad, GstObject * parent,
         GST_DEBUG_OBJECT (dlbuf, "percent %d, duration %" G_GINT64_FORMAT
             ", writing %" G_GINT64_FORMAT, percent, duration, write_pos);
 
-        /* calculate remaining and total download time */
-        if (duration > write_pos && avg_in > 0.0)
-          estimated_total = ((duration - write_pos) * 1000) / avg_in;
-        else
-          estimated_total = -1;
-
-        GST_DEBUG_OBJECT (dlbuf, "estimated-total %" G_GINT64_FORMAT,
-            estimated_total);
-
         gst_query_parse_buffering_range (query, &format, NULL, NULL, NULL);
 
-        switch (format) {
-          case GST_FORMAT_PERCENT:
-            start = 0;
-            /* get our available data relative to the duration */
-            if (duration != -1)
-              stop =
-                  gst_util_uint64_scale (GST_FORMAT_PERCENT_MAX, write_pos,
-                  duration);
-            else
-              stop = -1;
-            break;
-          case GST_FORMAT_BYTES:
-            start = 0;
-            stop = write_pos;
-            break;
-          default:
-            start = -1;
-            stop = -1;
-            break;
-        }
-
-        gst_query_set_buffering_range (query, format, start, stop,
-            estimated_total);
-
         /* fill out the buffered ranges */
-        offset = 0;
+        start = offset = 0;
+        stop = -1;
         while (gst_sparse_file_get_range_after (dlbuf->file, offset,
                 &range_start, &range_stop)) {
+          gboolean current_range;
+
+          GST_DEBUG_OBJECT (dlbuf,
+              "range starting at %" G_GSIZE_FORMAT " and finishing at %"
+              G_GSIZE_FORMAT, range_start, range_stop);
+
           offset = range_stop;
 
+          /* find the range we are currently downloading, we'll remember it
+           * after we convert to the target format */
+          if (range_start <= write_pos && range_stop >= write_pos) {
+            current_range = TRUE;
+            /* calculate remaining and total download time */
+            if (duration >= range_stop && avg_in > 0.0)
+              estimated_total = ((duration - range_stop) * 1000) / avg_in;
+            else
+              estimated_total = -1;
+          } else
+            current_range = FALSE;
+
           switch (format) {
             case GST_FORMAT_PERCENT:
+              /* get our available data relative to the duration */
               if (duration == -1) {
                 range_start = 0;
                 range_stop = 0;
@@ -1527,13 +1517,27 @@ gst_download_buffer_handle_src_query (GstPad * pad, GstObject * parent,
               range_stop = -1;
               break;
           }
+
+          if (current_range) {
+            /* we are currently downloading this range */
+            start = range_start;
+            stop = range_stop;
+          }
+          GST_DEBUG_OBJECT (dlbuf,
+              "range to format: %" G_GSIZE_FORMAT " - %" G_GSIZE_FORMAT,
+              range_start, range_stop);
           if (range_start == range_stop)
             continue;
-          GST_DEBUG_OBJECT (dlbuf,
-              "range starting at %" G_GSIZE_FORMAT " and finishing at %"
-              G_GSIZE_FORMAT, range_start, range_stop);
           gst_query_add_buffering_range (query, range_start, range_stop);
         }
+
+        GST_DEBUG_OBJECT (dlbuf, "estimated-total %" G_GINT64_FORMAT,
+            estimated_total);
+
+        gst_query_set_buffering_range (query, format, start, stop,
+            estimated_total);
+
+        GST_DOWNLOAD_BUFFER_MUTEX_UNLOCK (dlbuf);
       }
       break;
     }