souphttpsrc: check difference in time from the last socket read before changing blocksize
authorNicola Murino <nicola.murino@gmail.com>
Mon, 17 Dec 2018 18:18:43 +0000 (19:18 +0100)
committerNicola Murino <nicola.murino@gmail.com>
Mon, 17 Dec 2018 18:27:48 +0000 (19:27 +0100)
If the pipeline consumes the data slower than the available network speed,
for example because sync=true, is useless to increase the blocksize and
reading in too big blocksizes can cause the connection to time out

Closes #463

ext/soup/gstsouphttpsrc.c
ext/soup/gstsouphttpsrc.h

index 6e64a57..1bedd4e 100644 (file)
@@ -141,6 +141,7 @@ enum
 #define REDUCE_BLOCKSIZE_LIMIT 0.20
 #define REDUCE_BLOCKSIZE_COUNT 2
 #define REDUCE_BLOCKSIZE_FACTOR 0.5
+#define GROW_TIME_LIMIT (1 * GST_SECOND)
 
 static void gst_soup_http_src_uri_handler_init (gpointer g_iface,
     gpointer iface_data);
@@ -452,6 +453,7 @@ gst_soup_http_src_reset (GstSoupHTTPSrc * src)
 
   src->reduce_blocksize_count = 0;
   src->increase_blocksize_count = 0;
+  src->last_socket_read_time = 0;
 
   g_cancellable_reset (src->cancellable);
   g_mutex_lock (&src->mutex);
@@ -1634,10 +1636,15 @@ gst_soup_http_src_check_update_blocksize (GstSoupHTTPSrc * src,
 {
   guint blocksize = gst_base_src_get_blocksize (GST_BASE_SRC_CAST (src));
 
-  GST_LOG_OBJECT (src, "Checking to update blocksize. Read:%" G_GINT64_FORMAT
-      " blocksize:%u", bytes_read, blocksize);
+  gint64 time_since_last_read =
+      g_get_monotonic_time () * GST_USECOND - src->last_socket_read_time;
 
-  if (bytes_read >= blocksize * GROW_BLOCKSIZE_LIMIT) {
+  GST_LOG_OBJECT (src, "Checking to update blocksize. Read: %" G_GINT64_FORMAT
+      " bytes, blocksize: %u bytes, time since last read: %" GST_TIME_FORMAT,
+      bytes_read, blocksize, GST_TIME_ARGS (time_since_last_read));
+
+  if (bytes_read >= blocksize * GROW_BLOCKSIZE_LIMIT
+      && time_since_last_read <= GROW_TIME_LIMIT) {
     src->reduce_blocksize_count = 0;
     src->increase_blocksize_count++;
 
@@ -1647,7 +1654,8 @@ gst_soup_http_src_check_update_blocksize (GstSoupHTTPSrc * src,
       gst_base_src_set_blocksize (GST_BASE_SRC_CAST (src), blocksize);
       src->increase_blocksize_count = 0;
     }
-  } else if (bytes_read < blocksize * REDUCE_BLOCKSIZE_LIMIT) {
+  } else if (bytes_read < blocksize * REDUCE_BLOCKSIZE_LIMIT
+      || time_since_last_read > GROW_TIME_LIMIT) {
     src->reduce_blocksize_count++;
     src->increase_blocksize_count = 0;
 
@@ -1736,6 +1744,8 @@ gst_soup_http_src_read_buffer (GstSoupHTTPSrc * src, GstBuffer ** outbuf)
 
     gst_soup_http_src_check_update_blocksize (src, read_bytes);
 
+    src->last_socket_read_time = g_get_monotonic_time () * GST_USECOND;
+
     /* If we're at the end of a range request, read again to let libsoup
      * finalize the request. This allows to reuse the connection again later,
      * otherwise we would have to cancel the message and close the connection
index 06851dd..f848cde 100644 (file)
@@ -115,6 +115,8 @@ struct _GstSoupHTTPSrc {
   GCond have_headers_cond;
 
   GstEvent *http_headers_event;
+
+  gint64 last_socket_read_time;
 };
 
 struct _GstSoupHTTPSrcClass {