2 * Input cache protocol.
3 * Copyright (c) 2011,2014 Michael Niedermayer
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Based on file.c by Fabrice Bellard
26 * support keeping files
27 * support filling with a background thread
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/file.h"
33 #include "libavutil/tree.h"
44 #include "os_support.h"
47 typedef struct CacheEntry {
53 typedef struct Context {
55 struct AVTreeNode *root;
62 int64_t cache_hit, cache_miss;
65 static int cmp(void *key, const void *node)
67 return (*(int64_t *) key) - ((const CacheEntry *) node)->logical_pos;
70 static int cache_open(URLContext *h, const char *arg, int flags)
73 Context *c= h->priv_data;
75 av_strstart(arg, "cache:", &arg);
77 c->fd = av_tempfile("ffcache", &buffername, 0, h);
79 av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
84 av_freep(&buffername);
86 return ffurl_open(&c->inner, arg, flags, &h->interrupt_callback, NULL);
89 static int add_entry(URLContext *h, const unsigned char *buf, int size)
91 Context *c= h->priv_data;
94 CacheEntry *entry = av_malloc(sizeof(*entry));
95 CacheEntry *entry_ret;
96 struct AVTreeNode *node = av_tree_node_alloc();
98 if (!entry || !node) {
99 ret = AVERROR(ENOMEM);
104 pos = lseek(c->fd, 0, SEEK_END);
106 ret = AVERROR(errno);
107 av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
111 ret = write(c->fd, buf, size);
113 ret = AVERROR(errno);
114 av_log(h, AV_LOG_ERROR, "write in cache failed\n");
118 entry->logical_pos = c->logical_pos;
119 entry->physical_pos = pos;
122 entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
123 if (entry_ret && entry_ret != entry) {
125 av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
128 c->cache_pos = entry->physical_pos + entry->size;
137 static int cache_read(URLContext *h, unsigned char *buf, int size)
139 Context *c= h->priv_data;
140 CacheEntry *entry, *next[2] = {NULL, NULL};
143 entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
149 int64_t in_block_pos = c->logical_pos - entry->logical_pos;
150 av_assert0(entry->logical_pos <= c->logical_pos);
151 if (in_block_pos < entry->size) {
152 int64_t physical_target = entry->physical_pos + in_block_pos;
153 //FIXME avoid seek if unneeded
154 r = lseek(c->fd, physical_target, SEEK_SET);
156 r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
166 // Cache miss or some kind of fault with the cache
168 if (c->logical_pos != c->inner_pos) {
169 r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
171 av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
177 r = ffurl_read(c->inner, buf, size);
178 if (r == 0 && size>0) {
180 av_assert0(c->end >= c->logical_pos);
188 add_entry(h, buf, r);
190 c->end = FFMAX(c->end, c->logical_pos);
195 static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
197 Context *c= h->priv_data;
200 if (whence == AVSEEK_SIZE) {
201 pos= ffurl_seek(c->inner, pos, whence);
203 pos= ffurl_seek(c->inner, -1, SEEK_END);
204 if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
205 av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
209 c->end = FFMAX(c->end, pos);
213 if (whence == SEEK_CUR) {
215 pos += c->logical_pos;
216 } else if (whence == SEEK_END && c->is_true_eof) {
221 if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
222 //Seems within filesize, assume it will not fail.
223 c->logical_pos = pos;
228 ret= ffurl_seek(c->inner, pos, whence);
231 c->logical_pos = ret;
232 c->end = FFMAX(c->end, ret);
238 static int cache_close(URLContext *h)
240 Context *c= h->priv_data;
242 av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
243 c->cache_hit, c->cache_miss);
246 ffurl_close(c->inner);
247 av_tree_destroy(c->root);
253 URLProtocol ff_cache_protocol = {
255 .url_open = cache_open,
256 .url_read = cache_read,
257 .url_seek = cache_seek,
258 .url_close = cache_close,
259 .priv_data_size = sizeof(Context),