diff --git a/DOCS/man/en/options.rst b/DOCS/man/en/options.rst index 8faebfeab8..150c12d043 100644 --- a/DOCS/man/en/options.rst +++ b/DOCS/man/en/options.rst @@ -309,7 +309,7 @@ Set the size of the cache in kilobytes, disable it with ``no``, or automatically enable it if needed with ``auto`` (default: ``auto``). With ``auto``, the cache will usually be enabled for network streams, - using a default size. + using the size set by ``--cache-default``. May be useful when playing files from slow media, but can also have negative effects, especially with file formats that require a lot of @@ -321,6 +321,11 @@ seeking back. Likewise, when starting a file the cache will be at 100%, because no space is reserved for seeking back yet. +``--cache-default=`` + Set the size of the cache in kilobytes (default: 320 KB). Using ``no`` + will not automatically enable the cache e.h. when playing from a network + stream. Note that using ``--cache`` will always override this option. + ``--cache-pause=`` If the cache percentage goes below the specified value, pause and wait until the percentage set by ``--cache-min`` is reached, then resume diff --git a/core/mplayer.c b/core/mplayer.c index 79626fdc80..1341a51a1a 100644 --- a/core/mplayer.c +++ b/core/mplayer.c @@ -3896,6 +3896,7 @@ static struct track *open_external_file(struct MPContext *mpctx, char *filename, if (!stream) goto err_out; stream_enable_cache_percent(&stream, stream_cache, + opts->stream_cache_def_size, opts->stream_cache_min_percent, opts->stream_cache_seek_min_percent); // deal with broken demuxers: preselect streams @@ -4210,6 +4211,7 @@ static void play_current_file(struct MPContext *mpctx) // CACHE2: initial prefill: 20% later: 5% (should be set by -cacheopts) int res = stream_enable_cache_percent(&mpctx->stream, opts->stream_cache_size, + opts->stream_cache_def_size, opts->stream_cache_min_percent, opts->stream_cache_seek_min_percent); if (res == 0) diff --git a/core/options.c b/core/options.c index b00f79c003..68d4c2ce52 100644 --- a/core/options.c +++ b/core/options.c @@ -331,6 +331,9 @@ const m_option_t mp_opts[] = { ({"no", 0}, {"auto", -1}), OPTDEF_INT(-1)), + OPT_CHOICE_OR_INT("cache-default", stream_cache_def_size, 0, 32, 0x7fffffff, + ({"no", 0}), + OPTDEF_INT(320)), OPT_FLOATRANGE("cache-min", stream_cache_min_percent, 0, 0, 99), OPT_FLOATRANGE("cache-seek-min", stream_cache_seek_min_percent, 0, 0, 99), OPT_CHOICE_OR_INT("cache-pause", stream_cache_pause, 0, diff --git a/core/options.h b/core/options.h index 1eb111e33e..28932693ed 100644 --- a/core/options.h +++ b/core/options.h @@ -92,6 +92,7 @@ typedef struct MPOpts { int load_config; int use_filedir_conf; int stream_cache_size; + int stream_cache_def_size; float stream_cache_min_percent; float stream_cache_seek_min_percent; int stream_cache_pause; diff --git a/core/timeline/tl_matroska.c b/core/timeline/tl_matroska.c index 11fcc67583..098c02fd9f 100644 --- a/core/timeline/tl_matroska.c +++ b/core/timeline/tl_matroska.c @@ -126,8 +126,7 @@ static int enable_cache(struct MPContext *mpctx, struct stream **stream, { struct MPOpts *opts = &mpctx->opts; - if (!(opts->stream_cache_size > 0 || - opts->stream_cache_size < 0 && (*stream)->cache_size)) + if (opts->stream_cache_size <= 0) return 0; char *filename = talloc_strdup(NULL, (*demuxer)->filename); @@ -143,6 +142,7 @@ static int enable_cache(struct MPContext *mpctx, struct stream **stream, stream_enable_cache_percent(stream, opts->stream_cache_size, + opts->stream_cache_def_size, opts->stream_cache_min_percent, opts->stream_cache_seek_min_percent); diff --git a/stream/stream.c b/stream/stream.c index 3dc25d7880..c1e88a0796 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -172,11 +172,6 @@ static stream_t *open_stream_plugin(const stream_info_t *sinfo, if (!s->read_chunk) s->read_chunk = 4 * (s->sector_size ? s->sector_size : STREAM_BUFFER_SIZE); - if (s->streaming && !s->cache_size) { - // Set default cache size to use if user does not specify it. - s->cache_size = 320; - } - if (s->type <= -2) mp_msg(MSGT_OPEN, MSGL_WARN, "Warning streams need a type !!!!\n"); if (!s->seek) @@ -661,16 +656,22 @@ stream_t *open_memory_stream(void *data, int len) return s; } +static int stream_enable_cache(stream_t **stream, int64_t size, int64_t min, + int64_t seek_limit); + +/** + * \return 1 on success, 0 if the function was interrupted and -1 on error, or + * if the cache is disabled + */ int stream_enable_cache_percent(stream_t **stream, int64_t stream_cache_size, + int64_t stream_cache_def_size, float stream_cache_min_percent, float stream_cache_seek_min_percent) { - if (stream_cache_size == -1) - stream_cache_size = (*stream)->cache_size; + stream_cache_size = (*stream)->streaming ? stream_cache_def_size : 0; stream_cache_size = stream_cache_size * 1024; // input is in KiB - return stream_enable_cache(stream, stream_cache_size, stream_cache_size * (stream_cache_min_percent / 100.0), @@ -678,12 +679,8 @@ int stream_enable_cache_percent(stream_t **stream, int64_t stream_cache_size, (stream_cache_seek_min_percent / 100.0)); } -/** - * \return 1 on success, 0 if the function was interrupted and -1 on error, or - * if the cache is disabled - */ -int stream_enable_cache(stream_t **stream, int64_t size, int64_t min, - int64_t seek_limit) +static int stream_enable_cache(stream_t **stream, int64_t size, int64_t min, + int64_t seek_limit) { stream_t *orig = *stream; diff --git a/stream/stream.h b/stream/stream.h index 149618ccd6..f1033ed38f 100644 --- a/stream/stream.h +++ b/stream/stream.h @@ -154,7 +154,6 @@ typedef struct stream { int eof; int mode; //STREAM_READ or STREAM_WRITE bool streaming; // known to be a network stream if true - int cache_size; // cache size in KB to use if enabled void *priv; // used for DVD, TV, RTSP etc char *url; // strdup() of filename/url char *mime_type; // when HTTP streaming is used @@ -175,10 +174,9 @@ int stream_fill_buffer(stream_t *s); void stream_set_capture_file(stream_t *s, const char *filename); int stream_enable_cache_percent(stream_t **stream, int64_t stream_cache_size, + int64_t stream_cache_def_size, float stream_cache_min_percent, float stream_cache_seek_min_percent); -int stream_enable_cache(stream_t **stream, int64_t size, int64_t min, - int64_t seek_limit); // Internal int stream_cache_init(stream_t *cache, stream_t *stream, int64_t size,