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.

213 lines
7.3KB

  1. /*
  2. * unbuffered 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. #ifndef AVIO_H
  22. #define AVIO_H
  23. /* output byte stream handling */
  24. typedef int64_t offset_t;
  25. /* unbuffered I/O */
  26. struct URLContext {
  27. struct URLProtocol *prot;
  28. int flags;
  29. int is_streamed; /* true if streamed (no seek possible), default = false */
  30. int max_packet_size; /* if non zero, the stream is packetized with this max packet size */
  31. void *priv_data;
  32. #if LIBAVFORMAT_VERSION_INT >= (52<<16)
  33. char *filename; /* specified filename */
  34. #else
  35. char filename[1]; /* specified filename */
  36. #endif
  37. };
  38. typedef struct URLContext URLContext;
  39. typedef struct URLPollEntry {
  40. URLContext *handle;
  41. int events;
  42. int revents;
  43. } URLPollEntry;
  44. #define URL_RDONLY 0
  45. #define URL_WRONLY 1
  46. #define URL_RDWR 2
  47. typedef int URLInterruptCB(void);
  48. int url_open(URLContext **h, const char *filename, int flags);
  49. int url_read(URLContext *h, unsigned char *buf, int size);
  50. int url_write(URLContext *h, unsigned char *buf, int size);
  51. offset_t url_seek(URLContext *h, offset_t pos, int whence);
  52. int url_close(URLContext *h);
  53. int url_exist(const char *filename);
  54. offset_t url_filesize(URLContext *h);
  55. int url_get_max_packet_size(URLContext *h);
  56. void url_get_filename(URLContext *h, char *buf, int buf_size);
  57. /* the callback is called in blocking functions to test regulary if
  58. asynchronous interruption is needed. -EINTR is returned in this
  59. case by the interrupted function. 'NULL' means no interrupt
  60. callback is given. */
  61. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  62. /* not implemented */
  63. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  64. /**
  65. * passing this as the "whence" parameter to a seek function causes it to
  66. * return the filesize without seeking anywhere, supporting this is optional
  67. * if its not supprted then the seek function will return <0
  68. */
  69. #define AVSEEK_SIZE 0x10000
  70. typedef struct URLProtocol {
  71. const char *name;
  72. int (*url_open)(URLContext *h, const char *filename, int flags);
  73. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  74. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  75. offset_t (*url_seek)(URLContext *h, offset_t pos, int whence);
  76. int (*url_close)(URLContext *h);
  77. struct URLProtocol *next;
  78. } URLProtocol;
  79. extern URLProtocol *first_protocol;
  80. extern URLInterruptCB *url_interrupt_cb;
  81. int register_protocol(URLProtocol *protocol);
  82. typedef struct {
  83. unsigned char *buffer;
  84. int buffer_size;
  85. unsigned char *buf_ptr, *buf_end;
  86. void *opaque;
  87. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  88. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  89. offset_t (*seek)(void *opaque, offset_t offset, int whence);
  90. offset_t pos; /* position in the file of the current buffer */
  91. int must_flush; /* true if the next seek should flush */
  92. int eof_reached; /* true if eof reached */
  93. int write_flag; /* true if open for writing */
  94. int is_streamed;
  95. int max_packet_size;
  96. unsigned long checksum;
  97. unsigned char *checksum_ptr;
  98. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  99. int error; ///< contains the error code or 0 if no error happened
  100. } ByteIOContext;
  101. int init_put_byte(ByteIOContext *s,
  102. unsigned char *buffer,
  103. int buffer_size,
  104. int write_flag,
  105. void *opaque,
  106. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  107. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  108. offset_t (*seek)(void *opaque, offset_t offset, int whence));
  109. void put_byte(ByteIOContext *s, int b);
  110. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  111. void put_le64(ByteIOContext *s, uint64_t val);
  112. void put_be64(ByteIOContext *s, uint64_t val);
  113. void put_le32(ByteIOContext *s, unsigned int val);
  114. void put_be32(ByteIOContext *s, unsigned int val);
  115. void put_le24(ByteIOContext *s, unsigned int val);
  116. void put_be24(ByteIOContext *s, unsigned int val);
  117. void put_le16(ByteIOContext *s, unsigned int val);
  118. void put_be16(ByteIOContext *s, unsigned int val);
  119. void put_tag(ByteIOContext *s, const char *tag);
  120. void put_strz(ByteIOContext *s, const char *buf);
  121. offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence);
  122. void url_fskip(ByteIOContext *s, offset_t offset);
  123. offset_t url_ftell(ByteIOContext *s);
  124. offset_t url_fsize(ByteIOContext *s);
  125. int url_feof(ByteIOContext *s);
  126. int url_ferror(ByteIOContext *s);
  127. #define URL_EOF (-1)
  128. int url_fgetc(ByteIOContext *s);
  129. #ifdef __GNUC__
  130. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  131. #else
  132. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  133. #endif
  134. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  135. void put_flush_packet(ByteIOContext *s);
  136. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  137. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  138. int get_byte(ByteIOContext *s);
  139. unsigned int get_le24(ByteIOContext *s);
  140. unsigned int get_le32(ByteIOContext *s);
  141. uint64_t get_le64(ByteIOContext *s);
  142. unsigned int get_le16(ByteIOContext *s);
  143. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  144. unsigned int get_be16(ByteIOContext *s);
  145. unsigned int get_be24(ByteIOContext *s);
  146. unsigned int get_be32(ByteIOContext *s);
  147. uint64_t get_be64(ByteIOContext *s);
  148. static inline int url_is_streamed(ByteIOContext *s)
  149. {
  150. return s->is_streamed;
  151. }
  152. int url_fdopen(ByteIOContext *s, URLContext *h);
  153. int url_setbufsize(ByteIOContext *s, int buf_size);
  154. int url_fopen(ByteIOContext *s, const char *filename, int flags);
  155. int url_fclose(ByteIOContext *s);
  156. URLContext *url_fileno(ByteIOContext *s);
  157. int url_fget_max_packet_size(ByteIOContext *s);
  158. int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags);
  159. int url_close_buf(ByteIOContext *s);
  160. int url_open_dyn_buf(ByteIOContext *s);
  161. int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size);
  162. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  163. unsigned long get_checksum(ByteIOContext *s);
  164. void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum);
  165. /* file.c */
  166. extern URLProtocol file_protocol;
  167. extern URLProtocol pipe_protocol;
  168. /* udp.c */
  169. extern URLProtocol udp_protocol;
  170. int udp_set_remote_url(URLContext *h, const char *uri);
  171. int udp_get_local_port(URLContext *h);
  172. int udp_get_file_handle(URLContext *h);
  173. /* tcp.c */
  174. extern URLProtocol tcp_protocol;
  175. /* http.c */
  176. extern URLProtocol http_protocol;
  177. #endif