From 5b8298376b3ea548b21b652c495b5fcf3d4b5e78 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 22 Jun 2014 02:50:52 +0200 Subject: stream: add a file cache For remarks, pretty much see the manpage additions. Could help with network streams that require too much seeking (maybe), or might be extended to help with the use case of watching and downloading a file at the same time. In general, it might be a useless feature and could be removed again. --- stream/stream.c | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'stream/stream.c') diff --git a/stream/stream.c b/stream/stream.c index 2a6136f036..6e903bc8ec 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -766,6 +766,28 @@ stream_t *open_memory_stream(void *data, int len) return s; } +static stream_t *open_cache(stream_t *orig, const char *name) +{ + stream_t *cache = new_stream(); + cache->uncached_type = orig->type; + cache->uncached_stream = orig; + cache->seekable = true; + cache->mode = STREAM_READ; + cache->read_chunk = 4 * STREAM_BUFFER_SIZE; + + cache->url = talloc_strdup(cache, orig->url); + cache->mime_type = talloc_strdup(cache, orig->mime_type); + cache->demuxer = talloc_strdup(cache, orig->demuxer); + cache->lavf_type = talloc_strdup(cache, orig->lavf_type); + cache->safe_origin = orig->safe_origin; + cache->opts = orig->opts; + cache->global = orig->global; + + cache->log = mp_log_new(cache, cache->global->log, name); + + return cache; +} + static struct mp_cache_opts check_cache_opts(stream_t *stream, struct mp_cache_opts *opts) { @@ -794,27 +816,21 @@ int stream_enable_cache(stream_t **stream, struct mp_cache_opts *opts) if (use_opts.size < 1) return -1; - stream_t *cache = new_stream(); - cache->uncached_type = orig->type; - cache->uncached_stream = orig; - cache->seekable = true; - cache->mode = STREAM_READ; - cache->read_chunk = 4 * STREAM_BUFFER_SIZE; - - cache->url = talloc_strdup(cache, orig->url); - cache->mime_type = talloc_strdup(cache, orig->mime_type); - cache->demuxer = talloc_strdup(cache, orig->demuxer); - cache->lavf_type = talloc_strdup(cache, orig->lavf_type); - cache->safe_origin = orig->safe_origin; - cache->opts = orig->opts; - cache->global = orig->global; + stream_t *fcache = open_cache(orig, "file-cache"); + if (stream_file_cache_init(fcache, orig, &use_opts) <= 0) { + fcache->uncached_stream = NULL; // don't free original stream + free_stream(fcache); + fcache = orig; + } - cache->log = mp_log_new(cache, cache->global->log, "cache"); + stream_t *cache = open_cache(fcache, "cache"); - int res = stream_cache_init(cache, orig, &use_opts); + int res = stream_cache_init(cache, fcache, &use_opts); if (res <= 0) { cache->uncached_stream = NULL; // don't free original stream free_stream(cache); + if (fcache != orig) + free_stream(fcache); } else { *stream = cache; } -- cgit v1.2.3