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.

303 lines
8.0KB

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