diff options
Diffstat (limited to 'stream')
-rw-r--r-- | stream/cache.c | 12 | ||||
-rw-r--r-- | stream/stream.c | 46 | ||||
-rw-r--r-- | stream/stream.h | 11 |
3 files changed, 34 insertions, 35 deletions
diff --git a/stream/cache.c b/stream/cache.c index 6079f6b976..95ece34a8c 100644 --- a/stream/cache.c +++ b/stream/cache.c @@ -56,6 +56,7 @@ #include "osdep/threads.h" #include "common/msg.h" +#include "options/options.h" #include "stream.h" #include "common/common.h" @@ -655,18 +656,18 @@ static void cache_uninit(stream_t *cache) // return 1 on success, 0 if the function was interrupted and -1 on error, or // if the cache is disabled -int stream_cache_init(stream_t *cache, stream_t *stream, int64_t size, - int64_t min, int64_t seek_limit) +int stream_cache_init(stream_t *cache, stream_t *stream, + struct mp_cache_opts *opts) { - if (size < 1) + if (opts->size < 1) return -1; struct priv *s = talloc_zero(NULL, struct priv); s->log = cache->log; - s->seek_limit = seek_limit; + s->seek_limit = opts->seek_min * 1024ULL; - if (resize_cache(s, size) != STREAM_OK) { + if (resize_cache(s, opts->size * 1024ULL) != STREAM_OK) { MP_ERR(s, "Failed to allocate cache buffer.\n"); talloc_free(s); return -1; @@ -687,6 +688,7 @@ int stream_cache_init(stream_t *cache, stream_t *stream, int64_t size, cache->control = cache_control; cache->close = cache_uninit; + int64_t min = opts->initial * 1024ULL; if (min > s->buffer_size - FILL_LIMIT) min = s->buffer_size - FILL_LIMIT; diff --git a/stream/stream.c b/stream/stream.c index 59cd2f7763..b6fec81eb8 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -41,6 +41,7 @@ #include "common/global.h" #include "bstr/bstr.h" #include "common/msg.h" +#include "options/options.h" #include "options/path.h" #include "osdep/timer.h" #include "stream.h" @@ -772,36 +773,33 @@ 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) +static struct mp_cache_opts check_cache_opts(stream_t *stream, + struct mp_cache_opts *opts) { - if (stream_cache_size == -1) - stream_cache_size = (*stream)->streaming ? stream_cache_def_size : 0; + struct mp_cache_opts use_opts = *opts; + if (use_opts.size == -1) + use_opts.size = stream->streaming ? use_opts.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), - stream_cache_size * - (stream_cache_seek_min_percent / 100.0)); + if (stream->mode != STREAM_READ || !stream->allow_caching || use_opts.size < 1) + use_opts.size = 0; + return use_opts; } -static int stream_enable_cache(stream_t **stream, int64_t size, int64_t min, - int64_t seek_limit) +bool stream_wants_cache(stream_t *stream, struct mp_cache_opts *opts) +{ + struct mp_cache_opts use_opts = check_cache_opts(stream, opts); + return use_opts.size > 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, struct mp_cache_opts *opts) { stream_t *orig = *stream; + struct mp_cache_opts use_opts = check_cache_opts(*stream, opts); - if (orig->mode != STREAM_READ || !orig->allow_caching) - return 1; + if (use_opts.size < 1) + return -1; stream_t *cache = new_stream(); cache->uncached_type = orig->type; @@ -822,7 +820,7 @@ static int stream_enable_cache(stream_t **stream, int64_t size, int64_t min, cache->log = mp_log_new(cache, cache->global->log, "cache"); - int res = stream_cache_init(cache, orig, size, min, seek_limit); + int res = stream_cache_init(cache, orig, &use_opts); if (res <= 0) { cache->uncached_stream = NULL; // don't free original stream free_stream(cache); diff --git a/stream/stream.h b/stream/stream.h index 0fabd62b1f..3675ea9732 100644 --- a/stream/stream.h +++ b/stream/stream.h @@ -177,14 +177,13 @@ 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); +struct mp_cache_opts; +bool stream_wants_cache(stream_t *stream, struct mp_cache_opts *opts); +int stream_enable_cache(stream_t **stream, struct mp_cache_opts *opts); // Internal -int stream_cache_init(stream_t *cache, stream_t *stream, int64_t size, - int64_t min, int64_t seek_limit); +int stream_cache_init(stream_t *cache, stream_t *stream, + struct mp_cache_opts *opts); int stream_write_buffer(stream_t *s, unsigned char *buf, int len); |