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.

138 lines
3.4KB

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