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.

131 lines
3.1KB

  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. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/file.h"
  26. #include "avformat.h"
  27. #include <fcntl.h>
  28. #if HAVE_SETMODE
  29. #include <io.h>
  30. #endif
  31. #include <unistd.h>
  32. #include <sys/stat.h>
  33. #include <stdlib.h>
  34. #include "os_support.h"
  35. #include "url.h"
  36. typedef struct Context {
  37. int fd;
  38. int64_t end;
  39. int64_t pos;
  40. URLContext *inner;
  41. } Context;
  42. static int cache_open(URLContext *h, const char *arg, int flags)
  43. {
  44. int access;
  45. const char *buffername;
  46. Context *c;
  47. c = av_mallocz(sizeof(Context));
  48. if (!c) {
  49. return AVERROR(ENOMEM);
  50. }
  51. h->priv_data = c;
  52. av_strstart(arg, "cache:", &arg);
  53. c->fd = av_tempfile("ffcache", &buffername);
  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_free(buffername);
  60. return ffurl_open(&c->inner, arg, flags);
  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. return ffurl_seek(c->inner, pos, whence);
  87. }
  88. pos= lseek(c->fd, pos, whence);
  89. if(pos<0){
  90. return pos;
  91. }else if(pos <= c->end){
  92. c->pos= pos;
  93. return pos;
  94. }else{
  95. lseek(c->fd, c->pos, SEEK_SET);
  96. return AVERROR(EPIPE);
  97. }
  98. }
  99. static int cache_close(URLContext *h)
  100. {
  101. Context *c= h->priv_data;
  102. close(c->fd);
  103. ffurl_close(c->inner);
  104. av_freep(&h->priv_data);
  105. return 0;
  106. }
  107. URLProtocol ff_cache_protocol = {
  108. .name = "cache",
  109. .url_open = cache_open,
  110. .url_read = cache_read,
  111. .url_seek = cache_seek,
  112. .url_close = cache_close,
  113. };