summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2015-05-29 22:32:10 +0200
committerwm4 <wm4@nowhere>2015-05-29 22:32:10 +0200
commit5bea9668362866bd4db0557f387a3b185ac34315 (patch)
treeca77c6edeeccfa1413ab6d3f6213e7ae16d0d589
parent20bc56b22d7b1679d2cd83fec216697b8b922a09 (diff)
downloadmpv-5bea9668362866bd4db0557f387a3b185ac34315.tar.bz2
mpv-5bea9668362866bd4db0557f387a3b185ac34315.tar.xz
cache: limit readahead size to half the cache size at the beginning
Normally, the cache keeps 50% of the buffer for seeking backwards. Until now, the cache just used the full buffer size at the beginning of a file, because the 50% normally reserved for the backbuffer are unused. This caused a problem: when streaming from http, the player would first read about 150MB (default cache size), then stop until 75MB of the cache has been played. (Until the 75MB position, the cache is fully used, so nothing new can be read. After that, part of the backbuffer starts getting unreserved, and can be used for readahead.) This long read pause can cause the server to terminate the connection. Reconnecting may be possible, but if youtube-dl is used, the media URL may have become invalid. Fix this by limiting readahead to 50% even if unnecessary. The only exception is when the whole file would fit in the cache. In this case, it won't matter if we can't reconnect, because the cache covers everything anyway, and hopefully the cache will stay valid. Likely fixes #2000.
-rw-r--r--stream/cache.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/stream/cache.c b/stream/cache.c
index ede1e1fe83..fc4b2761da 100644
--- a/stream/cache.c
+++ b/stream/cache.c
@@ -215,6 +215,12 @@ static bool cache_fill(struct priv *s)
// number of buffer bytes which should be preserved in backwards direction
int64_t back = MPCLAMP(read - s->min_filepos, 0, s->back_size);
+ // limit maximum readahead to half the total buffer size, to ensure that
+ // we don't stall the network when starting a file (not reading new data
+ // by preserving the backbuffer) - unless the whole file fits in the cache
+ if (s->stream_size > s->buffer_size)
+ back = MPMAX(back, s->buffer_size / 2);
+
// number of buffer bytes that are valid and can be read
int64_t newb = FFMAX(s->max_filepos - read, 0);