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.0KB

  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 "avformat.h"
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <sys/time.h>
  25. /* standard file protocol */
  26. static int file_open(URLContext *h, const char *filename, int flags)
  27. {
  28. int access;
  29. int fd;
  30. strstart(filename, "file:", &filename);
  31. if (flags & URL_RDWR) {
  32. access = O_CREAT | O_TRUNC | O_RDWR;
  33. } else if (flags & URL_WRONLY) {
  34. access = O_CREAT | O_TRUNC | O_WRONLY;
  35. } else {
  36. access = O_RDONLY;
  37. }
  38. #if defined(__MINGW32__) || defined(CONFIG_OS2) || defined(__CYGWIN__)
  39. access |= O_BINARY;
  40. #endif
  41. fd = open(filename, access, 0666);
  42. if (fd < 0)
  43. return AVERROR(ENOENT);
  44. h->priv_data = (void *)(size_t)fd;
  45. return 0;
  46. }
  47. static int file_read(URLContext *h, unsigned char *buf, int size)
  48. {
  49. int fd = (size_t)h->priv_data;
  50. return read(fd, buf, size);
  51. }
  52. static int file_write(URLContext *h, unsigned char *buf, int size)
  53. {
  54. int fd = (size_t)h->priv_data;
  55. return write(fd, buf, size);
  56. }
  57. /* XXX: use llseek */
  58. static offset_t file_seek(URLContext *h, offset_t pos, int whence)
  59. {
  60. int fd = (size_t)h->priv_data;
  61. return lseek(fd, pos, whence);
  62. }
  63. static int file_close(URLContext *h)
  64. {
  65. int fd = (size_t)h->priv_data;
  66. return close(fd);
  67. }
  68. URLProtocol file_protocol = {
  69. "file",
  70. file_open,
  71. file_read,
  72. file_write,
  73. file_seek,
  74. file_close,
  75. };
  76. /* pipe protocol */
  77. static int pipe_open(URLContext *h, const char *filename, int flags)
  78. {
  79. int fd;
  80. if (flags & URL_WRONLY) {
  81. fd = 1;
  82. } else {
  83. fd = 0;
  84. }
  85. #if defined(__MINGW32__) || defined(CONFIG_OS2) || defined(__CYGWIN__)
  86. setmode(fd, O_BINARY);
  87. #endif
  88. h->priv_data = (void *)(size_t)fd;
  89. h->is_streamed = 1;
  90. return 0;
  91. }
  92. static int pipe_read(URLContext *h, unsigned char *buf, int size)
  93. {
  94. int fd = (size_t)h->priv_data;
  95. return read(fd, buf, size);
  96. }
  97. static int pipe_write(URLContext *h, unsigned char *buf, int size)
  98. {
  99. int fd = (size_t)h->priv_data;
  100. return write(fd, buf, size);
  101. }
  102. static int pipe_close(URLContext *h)
  103. {
  104. return 0;
  105. }
  106. URLProtocol pipe_protocol = {
  107. "pipe",
  108. pipe_open,
  109. pipe_read,
  110. pipe_write,
  111. NULL,
  112. pipe_close,
  113. };