1
0
mirror of https://github.com/mpv-player/mpv synced 2025-02-16 20:27:23 +00:00

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.
This commit is contained in:
wm4 2015-05-29 22:32:10 +02:00
parent 20bc56b22d
commit 5bea966836

View File

@ -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);