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.

132 lines
2.9KB

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