cache.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Input cache protocol.
  3. * Copyright (c) 2011 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 non continuous caching
  26. * support keeping files
  27. * support filling with a background thread
  28. */
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/file.h"
  32. #include "avformat.h"
  33. #include <fcntl.h>
  34. #if HAVE_SETMODE
  35. #include <io.h>
  36. #endif
  37. #include <unistd.h>
  38. #include <sys/stat.h>
  39. #include <stdlib.h>
  40. #include "os_support.h"
  41. #include "url.h"
  42. typedef struct Context {
  43. int fd;
  44. int64_t end;
  45. int64_t pos;
  46. URLContext *inner;
  47. } Context;
  48. static int cache_open(URLContext *h, const char *arg, int flags)
  49. {
  50. const char *buffername;
  51. Context *c= h->priv_data;
  52. av_strstart(arg, "cache:", &arg);
  53. c->fd = av_tempfile("ffcache", &buffername, 0, h);
  54. if (c->fd < 0){
  55. av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
  56. return c->fd;
  57. }
  58. unlink(buffername);
  59. av_freep(&buffername);
  60. return ffurl_open(&c->inner, arg, flags, &h->interrupt_callback, NULL);
  61. }
  62. static int cache_read(URLContext *h, unsigned char *buf, int size)
  63. {
  64. Context *c= h->priv_data;
  65. int r;
  66. if(c->pos<c->end){
  67. r = read(c->fd, buf, FFMIN(size, c->end - c->pos));
  68. if(r>0)
  69. c->pos += r;
  70. return (-1 == r)?AVERROR(errno):r;
  71. }else{
  72. r = ffurl_read(c->inner, buf, size);
  73. if(r > 0){
  74. int r2= write(c->fd, buf, r);
  75. av_assert0(r2==r); // FIXME handle cache failure
  76. c->pos += r;
  77. c->end += r;
  78. }
  79. return r;
  80. }
  81. }
  82. static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
  83. {
  84. Context *c= h->priv_data;
  85. if (whence == AVSEEK_SIZE) {
  86. pos= ffurl_seek(c->inner, pos, whence);
  87. if(pos <= 0){
  88. pos= ffurl_seek(c->inner, -1, SEEK_END);
  89. ffurl_seek(c->inner, c->end, SEEK_SET);
  90. if(pos <= 0)
  91. return c->end;
  92. }
  93. return pos;
  94. }
  95. pos= lseek(c->fd, pos, whence);
  96. if(pos<0){
  97. return pos;
  98. }else if(pos <= c->end){
  99. c->pos= pos;
  100. return pos;
  101. }else{
  102. lseek(c->fd, c->pos, SEEK_SET);
  103. return AVERROR(EPIPE);
  104. }
  105. }
  106. static int cache_close(URLContext *h)
  107. {
  108. Context *c= h->priv_data;
  109. close(c->fd);
  110. ffurl_close(c->inner);
  111. return 0;
  112. }
  113. URLProtocol ff_cache_protocol = {
  114. .name = "cache",
  115. .url_open = cache_open,
  116. .url_read = cache_read,
  117. .url_seek = cache_seek,
  118. .url_close = cache_close,
  119. .priv_data_size = sizeof(Context),
  120. };