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.

141 lines
3.5KB

  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. if(lseek(c->fd, c->pos, SEEK_SET) < 0) {
  105. av_log(h, AV_LOG_ERROR, "Failure to seek in cache\n");
  106. }
  107. return AVERROR(EPIPE);
  108. }
  109. }
  110. static int cache_close(URLContext *h)
  111. {
  112. Context *c= h->priv_data;
  113. close(c->fd);
  114. ffurl_close(c->inner);
  115. return 0;
  116. }
  117. URLProtocol ff_cache_protocol = {
  118. .name = "cache",
  119. .url_open = cache_open,
  120. .url_read = cache_read,
  121. .url_seek = cache_seek,
  122. .url_close = cache_close,
  123. .priv_data_size = sizeof(Context),
  124. };