cache.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. int access;
  51. const char *buffername;
  52. Context *c;
  53. c = av_mallocz(sizeof(Context));
  54. if (!c) {
  55. return AVERROR(ENOMEM);
  56. }
  57. h->priv_data = c;
  58. av_strstart(arg, "cache:", &arg);
  59. c->fd = av_tempfile("ffcache", &buffername, 0, h);
  60. if (c->fd < 0){
  61. av_log(h, AV_LOG_ERROR, "Failed to create tempfile\n");
  62. return c->fd;
  63. }
  64. unlink(buffername);
  65. av_free(buffername);
  66. return ffurl_open(&c->inner, arg, flags);
  67. }
  68. static int cache_read(URLContext *h, unsigned char *buf, int size)
  69. {
  70. Context *c= h->priv_data;
  71. int r;
  72. if(c->pos<c->end){
  73. r = read(c->fd, buf, FFMIN(size, c->end - c->pos));
  74. if(r>0)
  75. c->pos += r;
  76. return (-1 == r)?AVERROR(errno):r;
  77. }else{
  78. r = ffurl_read(c->inner, buf, size);
  79. if(r > 0){
  80. int r2= write(c->fd, buf, r);
  81. av_assert0(r2==r); // FIXME handle cache failure
  82. c->pos += r;
  83. c->end += r;
  84. }
  85. return r;
  86. }
  87. }
  88. static int64_t cache_seek(URLContext *h, int64_t pos, int whence)
  89. {
  90. Context *c= h->priv_data;
  91. if (whence == AVSEEK_SIZE) {
  92. pos= ffurl_seek(c->inner, pos, whence);
  93. if(pos <= 0){
  94. pos= ffurl_seek(c->inner, -1, SEEK_END);
  95. ffurl_seek(c->inner, c->end, SEEK_SET);
  96. if(pos <= 0)
  97. return c->end;
  98. }
  99. return pos;
  100. }
  101. pos= lseek(c->fd, pos, whence);
  102. if(pos<0){
  103. return pos;
  104. }else if(pos <= c->end){
  105. c->pos= pos;
  106. return pos;
  107. }else{
  108. lseek(c->fd, c->pos, SEEK_SET);
  109. return AVERROR(EPIPE);
  110. }
  111. }
  112. static int cache_close(URLContext *h)
  113. {
  114. Context *c= h->priv_data;
  115. close(c->fd);
  116. ffurl_close(c->inner);
  117. av_freep(&h->priv_data);
  118. return 0;
  119. }
  120. URLProtocol ff_cache_protocol = {
  121. .name = "cache",
  122. .url_open = cache_open,
  123. .url_read = cache_read,
  124. .url_seek = cache_seek,
  125. .url_close = cache_close,
  126. };