You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
8.9KB

  1. /*
  2. * Input cache protocol.
  3. * Copyright (c) 2011,2014 Michael Niedermayer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. *
  21. * Based on file.c by Fabrice Bellard
  22. */
  23. /**
  24. * @TODO
  25. * support keeping files
  26. * support filling with a background thread
  27. */
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/file.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/tree.h"
  33. #include "avformat.h"
  34. #include <fcntl.h>
  35. #if HAVE_IO_H
  36. #include <io.h>
  37. #endif
  38. #if HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #include <sys/stat.h>
  42. #include <stdlib.h>
  43. #include "os_support.h"
  44. #include "url.h"
  45. typedef struct CacheEntry {
  46. int64_t logical_pos;
  47. int64_t physical_pos;
  48. int size;
  49. } CacheEntry;
  50. typedef struct Context {
  51. AVClass *class;
  52. int fd;
  53. struct AVTreeNode *root;
  54. int64_t logical_pos;
  55. int64_t cache_pos;
  56. int64_t inner_pos;
  57. int64_t end;
  58. int is_true_eof;
  59. URLContext *inner;
  60. int64_t cache_hit, cache_miss;
  61. int read_ahead_limit;
  62. } Context;
  63. static int cmp(const void *key, const void *node)
  64. {
  65. return FFDIFFSIGN(*(const int64_t *)key, ((const CacheEntry *) node)->logical_pos);
  66. }
  67. static int cache_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
  68. {
  69. char *buffername;
  70. Context *c= h->priv_data;
  71. av_strstart(arg, "cache:", &arg);
  72. c->fd = av_tempfile("ffcache", &buffername, 0, h);
  73. if (c->fd < 0){
  74. av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
  75. return c->fd;
  76. }
  77. unlink(buffername);
  78. av_freep(&buffername);
  79. return ffurl_open_whitelist(&c->inner, arg, flags, &h->interrupt_callback,
  80. options, h->protocol_whitelist, h->protocol_blacklist);
  81. }
  82. static int add_entry(URLContext *h, const unsigned char *buf, int size)
  83. {
  84. Context *c= h->priv_data;
  85. int64_t pos = -1;
  86. int ret;
  87. CacheEntry *entry = NULL, *next[2] = {NULL, NULL};
  88. CacheEntry *entry_ret;
  89. struct AVTreeNode *node = NULL;
  90. //FIXME avoid lseek
  91. pos = lseek(c->fd, 0, SEEK_END);
  92. if (pos < 0) {
  93. ret = AVERROR(errno);
  94. av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
  95. goto fail;
  96. }
  97. c->cache_pos = pos;
  98. ret = write(c->fd, buf, size);
  99. if (ret < 0) {
  100. ret = AVERROR(errno);
  101. av_log(h, AV_LOG_ERROR, "write in cache failed\n");
  102. goto fail;
  103. }
  104. c->cache_pos += ret;
  105. entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
  106. if (!entry)
  107. entry = next[0];
  108. if (!entry ||
  109. entry->logical_pos + entry->size != c->logical_pos ||
  110. entry->physical_pos + entry->size != pos
  111. ) {
  112. entry = av_malloc(sizeof(*entry));
  113. node = av_tree_node_alloc();
  114. if (!entry || !node) {
  115. ret = AVERROR(ENOMEM);
  116. goto fail;
  117. }
  118. entry->logical_pos = c->logical_pos;
  119. entry->physical_pos = pos;
  120. entry->size = ret;
  121. entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
  122. if (entry_ret && entry_ret != entry) {
  123. ret = -1;
  124. av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
  125. goto fail;
  126. }
  127. } else
  128. entry->size += ret;
  129. return 0;
  130. fail:
  131. //we could truncate the file to pos here if pos >=0 but ftruncate isn't available in VS so
  132. //for simplicty we just leave the file a bit larger
  133. av_free(entry);
  134. av_free(node);
  135. return ret;
  136. }
  137. static int cache_read(URLContext *h, unsigned char *buf, int size)
  138. {
  139. Context *c= h->priv_data;
  140. CacheEntry *entry, *next[2] = {NULL, NULL};
  141. int64_t r;
  142. entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
  143. if (!entry)
  144. entry = next[0];
  145. if (entry) {
  146. int64_t in_block_pos = c->logical_pos - entry->logical_pos;
  147. av_assert0(entry->logical_pos <= c->logical_pos);
  148. if (in_block_pos < entry->size) {
  149. int64_t physical_target = entry->physical_pos + in_block_pos;
  150. if (c->cache_pos != physical_target) {
  151. r = lseek(c->fd, physical_target, SEEK_SET);
  152. } else
  153. r = c->cache_pos;
  154. if (r >= 0) {
  155. c->cache_pos = r;
  156. r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
  157. }
  158. if (r > 0) {
  159. c->cache_pos += r;
  160. c->logical_pos += r;
  161. c->cache_hit ++;
  162. return r;
  163. }
  164. }
  165. }
  166. // Cache miss or some kind of fault with the cache
  167. if (c->logical_pos != c->inner_pos) {
  168. r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
  169. if (r<0) {
  170. av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
  171. return r;
  172. }
  173. c->inner_pos = r;
  174. }
  175. r = ffurl_read(c->inner, buf, size);
  176. if (r == 0 && size>0) {
  177. c->is_true_eof = 1;
  178. av_assert0(c->end >= c->logical_pos);
  179. }
  180. if (r<=0)
  181. return r;
  182. c->inner_pos += r;
  183. c->cache_miss ++;
  184. add_entry(h, buf, r);
  185. c->logical_pos += r;
  186. c->end = FFMAX(c->end, c->logical_pos);
  187. return r;
  188. }
  189. static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
  190. {
  191. Context *c= h->priv_data;
  192. int64_t ret;
  193. if (whence == AVSEEK_SIZE) {
  194. pos= ffurl_seek(c->inner, pos, whence);
  195. if(pos <= 0){
  196. pos= ffurl_seek(c->inner, -1, SEEK_END);
  197. if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
  198. av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
  199. }
  200. if (pos > 0)
  201. c->is_true_eof = 1;
  202. c->end = FFMAX(c->end, pos);
  203. return pos;
  204. }
  205. if (whence == SEEK_CUR) {
  206. whence = SEEK_SET;
  207. pos += c->logical_pos;
  208. } else if (whence == SEEK_END && c->is_true_eof) {
  209. resolve_eof:
  210. whence = SEEK_SET;
  211. pos += c->end;
  212. }
  213. if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
  214. //Seems within filesize, assume it will not fail.
  215. c->logical_pos = pos;
  216. return pos;
  217. }
  218. //cache miss
  219. ret= ffurl_seek(c->inner, pos, whence);
  220. if ((whence == SEEK_SET && pos >= c->logical_pos ||
  221. whence == SEEK_END && pos <= 0) && ret < 0) {
  222. if ( (whence == SEEK_SET && c->read_ahead_limit >= pos - c->logical_pos)
  223. || c->read_ahead_limit < 0) {
  224. uint8_t tmp[32768];
  225. while (c->logical_pos < pos || whence == SEEK_END) {
  226. int size = sizeof(tmp);
  227. if (whence == SEEK_SET)
  228. size = FFMIN(sizeof(tmp), pos - c->logical_pos);
  229. ret = cache_read(h, tmp, size);
  230. if (ret == 0 && whence == SEEK_END) {
  231. av_assert0(c->is_true_eof);
  232. goto resolve_eof;
  233. }
  234. if (ret < 0) {
  235. return ret;
  236. }
  237. }
  238. return c->logical_pos;
  239. }
  240. }
  241. if (ret >= 0) {
  242. c->logical_pos = ret;
  243. c->end = FFMAX(c->end, ret);
  244. }
  245. return ret;
  246. }
  247. static int enu_free(void *opaque, void *elem)
  248. {
  249. av_free(elem);
  250. return 0;
  251. }
  252. static int cache_close(URLContext *h)
  253. {
  254. Context *c= h->priv_data;
  255. av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
  256. c->cache_hit, c->cache_miss);
  257. close(c->fd);
  258. ffurl_close(c->inner);
  259. av_tree_enumerate(c->root, NULL, NULL, enu_free);
  260. av_tree_destroy(c->root);
  261. return 0;
  262. }
  263. #define OFFSET(x) offsetof(Context, x)
  264. #define D AV_OPT_FLAG_DECODING_PARAM
  265. static const AVOption options[] = {
  266. { "read_ahead_limit", "Amount in bytes that may be read ahead when seeking isn't supported, -1 for unlimited", OFFSET(read_ahead_limit), AV_OPT_TYPE_INT, { .i64 = 65536 }, -1, INT_MAX, D },
  267. {NULL},
  268. };
  269. static const AVClass cache_context_class = {
  270. .class_name = "Cache",
  271. .item_name = av_default_item_name,
  272. .option = options,
  273. .version = LIBAVUTIL_VERSION_INT,
  274. };
  275. const URLProtocol ff_cache_protocol = {
  276. .name = "cache",
  277. .url_open2 = cache_open,
  278. .url_read = cache_read,
  279. .url_seek = cache_seek,
  280. .url_close = cache_close,
  281. .priv_data_size = sizeof(Context),
  282. .priv_data_class = &cache_context_class,
  283. };