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.

307 lines
8.1KB

  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(void *key, const void *node)
  64. {
  65. return (*(int64_t *) key) - ((const CacheEntry *) node)->logical_pos;
  66. }
  67. static int cache_open(URLContext *h, const char *arg, int flags)
  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(&c->inner, arg, flags, &h->interrupt_callback, NULL);
  80. }
  81. static int add_entry(URLContext *h, const unsigned char *buf, int size)
  82. {
  83. Context *c= h->priv_data;
  84. int64_t pos = -1;
  85. int ret;
  86. CacheEntry *entry = av_malloc(sizeof(*entry));
  87. CacheEntry *entry_ret;
  88. struct AVTreeNode *node = av_tree_node_alloc();
  89. if (!entry || !node) {
  90. ret = AVERROR(ENOMEM);
  91. goto fail;
  92. }
  93. //FIXME avoid lseek
  94. pos = lseek(c->fd, 0, SEEK_END);
  95. if (pos < 0) {
  96. ret = AVERROR(errno);
  97. av_log(h, AV_LOG_ERROR, "seek in cache failed\n");
  98. goto fail;
  99. }
  100. c->cache_pos = pos;
  101. ret = write(c->fd, buf, size);
  102. if (ret < 0) {
  103. ret = AVERROR(errno);
  104. av_log(h, AV_LOG_ERROR, "write in cache failed\n");
  105. goto fail;
  106. }
  107. entry->logical_pos = c->logical_pos;
  108. entry->physical_pos = pos;
  109. entry->size = ret;
  110. c->cache_pos = entry->physical_pos + entry->size;
  111. entry_ret = av_tree_insert(&c->root, entry, cmp, &node);
  112. if (entry_ret && entry_ret != entry) {
  113. ret = -1;
  114. av_log(h, AV_LOG_ERROR, "av_tree_insert failed\n");
  115. goto fail;
  116. }
  117. return 0;
  118. fail:
  119. if (pos >= 0)
  120. ftruncate(c->fd, pos);
  121. av_free(entry);
  122. av_free(node);
  123. return ret;
  124. }
  125. static int cache_read(URLContext *h, unsigned char *buf, int size)
  126. {
  127. Context *c= h->priv_data;
  128. CacheEntry *entry, *next[2] = {NULL, NULL};
  129. int r;
  130. entry = av_tree_find(c->root, &c->logical_pos, cmp, (void**)next);
  131. if (!entry)
  132. entry = next[0];
  133. if (entry) {
  134. int64_t in_block_pos = c->logical_pos - entry->logical_pos;
  135. av_assert0(entry->logical_pos <= c->logical_pos);
  136. if (in_block_pos < entry->size) {
  137. int64_t physical_target = entry->physical_pos + in_block_pos;
  138. //FIXME avoid seek if unneeded
  139. r = lseek(c->fd, physical_target, SEEK_SET);
  140. if (r >= 0) {
  141. c->cache_pos = r;
  142. r = read(c->fd, buf, FFMIN(size, entry->size - in_block_pos));
  143. }
  144. if (r > 0) {
  145. c->cache_pos += r;
  146. c->logical_pos += r;
  147. c->cache_hit ++;
  148. return r;
  149. }
  150. }
  151. }
  152. // Cache miss or some kind of fault with the cache
  153. if (c->logical_pos != c->inner_pos) {
  154. r = ffurl_seek(c->inner, c->logical_pos, SEEK_SET);
  155. if (r<0) {
  156. av_log(h, AV_LOG_ERROR, "Failed to perform internal seek\n");
  157. return r;
  158. }
  159. c->inner_pos = r;
  160. }
  161. r = ffurl_read(c->inner, buf, size);
  162. if (r == 0 && size>0) {
  163. c->is_true_eof = 1;
  164. av_assert0(c->end >= c->logical_pos);
  165. }
  166. if (r<=0)
  167. return r;
  168. c->inner_pos += r;
  169. c->cache_miss ++;
  170. add_entry(h, buf, r);
  171. c->logical_pos += r;
  172. c->end = FFMAX(c->end, c->logical_pos);
  173. return r;
  174. }
  175. static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
  176. {
  177. Context *c= h->priv_data;
  178. int64_t ret;
  179. if (whence == AVSEEK_SIZE) {
  180. pos= ffurl_seek(c->inner, pos, whence);
  181. if(pos <= 0){
  182. pos= ffurl_seek(c->inner, -1, SEEK_END);
  183. if (ffurl_seek(c->inner, c->inner_pos, SEEK_SET) < 0)
  184. av_log(h, AV_LOG_ERROR, "Inner protocol failed to seekback end : %"PRId64"\n", pos);
  185. }
  186. if (pos > 0)
  187. c->is_true_eof = 1;
  188. c->end = FFMAX(c->end, pos);
  189. return pos;
  190. }
  191. if (whence == SEEK_CUR) {
  192. whence = SEEK_SET;
  193. pos += c->logical_pos;
  194. } else if (whence == SEEK_END && c->is_true_eof) {
  195. resolve_eof:
  196. whence = SEEK_SET;
  197. pos += c->end;
  198. }
  199. if (whence == SEEK_SET && pos >= 0 && pos < c->end) {
  200. //Seems within filesize, assume it will not fail.
  201. c->logical_pos = pos;
  202. return pos;
  203. }
  204. //cache miss
  205. ret= ffurl_seek(c->inner, pos, whence);
  206. if ((whence == SEEK_SET && pos >= c->logical_pos ||
  207. whence == SEEK_END && pos <= 0) && ret < 0) {
  208. if ( (whence == SEEK_SET && c->read_ahead_limit >= pos - c->logical_pos)
  209. || c->read_ahead_limit < 0) {
  210. uint8_t tmp[32768];
  211. while (c->logical_pos < pos || whence == SEEK_END) {
  212. int size = sizeof(tmp);
  213. if (whence == SEEK_SET)
  214. size = FFMIN(sizeof(tmp), pos - c->logical_pos);
  215. ret = cache_read(h, tmp, size);
  216. if (ret == 0 && whence == SEEK_END) {
  217. av_assert0(c->is_true_eof);
  218. goto resolve_eof;
  219. }
  220. if (ret < 0) {
  221. return ret;
  222. }
  223. }
  224. return c->logical_pos;
  225. }
  226. }
  227. if (ret >= 0) {
  228. c->logical_pos = ret;
  229. c->end = FFMAX(c->end, ret);
  230. }
  231. return ret;
  232. }
  233. static int cache_close(URLContext *h)
  234. {
  235. Context *c= h->priv_data;
  236. av_log(h, AV_LOG_INFO, "Statistics, cache hits:%"PRId64" cache misses:%"PRId64"\n",
  237. c->cache_hit, c->cache_miss);
  238. close(c->fd);
  239. ffurl_close(c->inner);
  240. av_tree_destroy(c->root);
  241. return 0;
  242. }
  243. #define OFFSET(x) offsetof(Context, x)
  244. #define D AV_OPT_FLAG_DECODING_PARAM
  245. static const AVOption options[] = {
  246. { "read_ahead_limit", "Amount in bytes that may be read ahead when seeking isnt supported, -1 for unlimited", OFFSET(read_ahead_limit), AV_OPT_TYPE_INT, { .i64 = 65536 }, -1, INT_MAX, D },
  247. {NULL},
  248. };
  249. static const AVClass cache_context_class = {
  250. .class_name = "Cache",
  251. .item_name = av_default_item_name,
  252. .option = options,
  253. .version = LIBAVUTIL_VERSION_INT,
  254. };
  255. URLProtocol ff_cache_protocol = {
  256. .name = "cache",
  257. .url_open = cache_open,
  258. .url_read = cache_read,
  259. .url_seek = cache_seek,
  260. .url_close = cache_close,
  261. .priv_data_size = sizeof(Context),
  262. .priv_data_class = &cache_context_class,
  263. };