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.

126 lines
2.8KB

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