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.

143 lines
3.5KB

  1. /*
  2. * Buffered file io for ffmpeg system
  3. * Copyright (c) 2001 Fabrice Bellard
  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. #include "libavutil/avstring.h"
  22. #include "avformat.h"
  23. #include <fcntl.h>
  24. #if HAVE_SETMODE
  25. #include <io.h>
  26. #endif
  27. #include <unistd.h>
  28. #include <sys/stat.h>
  29. #include <stdlib.h>
  30. #include "os_support.h"
  31. #include "url.h"
  32. /* standard file protocol */
  33. static int file_read(URLContext *h, unsigned char *buf, int size)
  34. {
  35. int fd = (intptr_t) h->priv_data;
  36. return read(fd, buf, size);
  37. }
  38. static int file_write(URLContext *h, const unsigned char *buf, int size)
  39. {
  40. int fd = (intptr_t) h->priv_data;
  41. return write(fd, buf, size);
  42. }
  43. static int file_get_handle(URLContext *h)
  44. {
  45. return (intptr_t) h->priv_data;
  46. }
  47. #if CONFIG_FILE_PROTOCOL
  48. static int file_open(URLContext *h, const char *filename, int flags)
  49. {
  50. int access;
  51. int fd;
  52. av_strstart(filename, "file:", &filename);
  53. if (flags & AVIO_RDWR) {
  54. access = O_CREAT | O_TRUNC | O_RDWR;
  55. } else if (flags & AVIO_WRONLY) {
  56. access = O_CREAT | O_TRUNC | O_WRONLY;
  57. } else {
  58. access = O_RDONLY;
  59. }
  60. #ifdef O_BINARY
  61. access |= O_BINARY;
  62. #endif
  63. fd = open(filename, access, 0666);
  64. if (fd == -1)
  65. return AVERROR(errno);
  66. h->priv_data = (void *) (intptr_t) fd;
  67. return 0;
  68. }
  69. /* XXX: use llseek */
  70. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  71. {
  72. int fd = (intptr_t) h->priv_data;
  73. if (whence == AVSEEK_SIZE) {
  74. struct stat st;
  75. int ret = fstat(fd, &st);
  76. return ret < 0 ? AVERROR(errno) : st.st_size;
  77. }
  78. return lseek(fd, pos, whence);
  79. }
  80. static int file_close(URLContext *h)
  81. {
  82. int fd = (intptr_t) h->priv_data;
  83. return close(fd);
  84. }
  85. URLProtocol ff_file_protocol = {
  86. .name = "file",
  87. .url_open = file_open,
  88. .url_read = file_read,
  89. .url_write = file_write,
  90. .url_seek = file_seek,
  91. .url_close = file_close,
  92. .url_get_file_handle = file_get_handle,
  93. };
  94. #endif /* CONFIG_FILE_PROTOCOL */
  95. #if CONFIG_PIPE_PROTOCOL
  96. static int pipe_open(URLContext *h, const char *filename, int flags)
  97. {
  98. int fd;
  99. char *final;
  100. av_strstart(filename, "pipe:", &filename);
  101. fd = strtol(filename, &final, 10);
  102. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  103. if (flags & AVIO_WRONLY) {
  104. fd = 1;
  105. } else {
  106. fd = 0;
  107. }
  108. }
  109. #if HAVE_SETMODE
  110. setmode(fd, O_BINARY);
  111. #endif
  112. h->priv_data = (void *) (intptr_t) fd;
  113. h->is_streamed = 1;
  114. return 0;
  115. }
  116. URLProtocol ff_pipe_protocol = {
  117. .name = "pipe",
  118. .url_open = pipe_open,
  119. .url_read = file_read,
  120. .url_write = file_write,
  121. .url_get_file_handle = file_get_handle,
  122. };
  123. #endif /* CONFIG_PIPE_PROTOCOL */