cache.c 3.4 KB

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