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.

552 lines
19KB

  1. /*
  2. * copyright (c) 2001 Fabrice Bellard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVFORMAT_AVIO_H
  21. #define AVFORMAT_AVIO_H
  22. /**
  23. * @file
  24. * unbuffered I/O operations
  25. *
  26. * @warning This file has to be considered an internal but installed
  27. * header, so it should not be directly included in your projects.
  28. */
  29. #include <stdint.h>
  30. #include "libavutil/common.h"
  31. /* unbuffered I/O */
  32. /**
  33. * URL Context.
  34. * New fields can be added to the end with minor version bumps.
  35. * Removal, reordering and changes to existing fields require a major
  36. * version bump.
  37. * sizeof(URLContext) must not be used outside libav*.
  38. */
  39. typedef struct URLContext {
  40. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  41. const AVClass *av_class; ///< information for av_log(). Set by url_open().
  42. #endif
  43. struct URLProtocol *prot;
  44. int flags;
  45. int is_streamed; /**< true if streamed (no seek possible), default = false */
  46. int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
  47. void *priv_data;
  48. char *filename; /**< specified URL */
  49. int is_connected;
  50. } URLContext;
  51. typedef struct URLPollEntry {
  52. URLContext *handle;
  53. int events;
  54. int revents;
  55. } URLPollEntry;
  56. #define URL_RDONLY 0
  57. #define URL_WRONLY 1
  58. #define URL_RDWR 2
  59. typedef int URLInterruptCB(void);
  60. /**
  61. * Creates an URLContext for accessing to the resource indicated by
  62. * url, and opens it using the URLProtocol up.
  63. *
  64. * @param puc pointer to the location where, in case of success, the
  65. * function puts the pointer to the created URLContext
  66. * @param flags flags which control how the resource indicated by url
  67. * is to be opened
  68. * @return 0 in case of success, a negative value corresponding to an
  69. * AVERROR code in case of failure
  70. */
  71. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  72. const char *url, int flags);
  73. /**
  74. * Creates an URLContext for accessing to the resource indicated by
  75. * url, but doesn't initiate the connection yet.
  76. *
  77. * @param puc pointer to the location where, in case of success, the
  78. * function puts the pointer to the created URLContext
  79. * @param flags flags which control how the resource indicated by url
  80. * is to be opened
  81. * @return 0 in case of success, a negative value corresponding to an
  82. * AVERROR code in case of failure
  83. */
  84. int url_alloc(URLContext **h, const char *url, int flags);
  85. /**
  86. * Connect an URLContext that has been allocated by url_alloc
  87. */
  88. int url_connect(URLContext *h);
  89. /**
  90. * Creates an URLContext for accessing to the resource indicated by
  91. * url, and opens it.
  92. *
  93. * @param puc pointer to the location where, in case of success, the
  94. * function puts the pointer to the created URLContext
  95. * @param flags flags which control how the resource indicated by url
  96. * is to be opened
  97. * @return 0 in case of success, a negative value corresponding to an
  98. * AVERROR code in case of failure
  99. */
  100. int url_open(URLContext **h, const char *url, int flags);
  101. /**
  102. * Reads up to size bytes from the resource accessed by h, and stores
  103. * the read bytes in buf.
  104. *
  105. * @return The number of bytes actually read, or a negative value
  106. * corresponding to an AVERROR code in case of error. A value of zero
  107. * indicates that it is not possible to read more from the accessed
  108. * resource (except if the value of the size argument is also zero).
  109. */
  110. int url_read(URLContext *h, unsigned char *buf, int size);
  111. /**
  112. * Read as many bytes as possible (up to size), calling the
  113. * read function multiple times if necessary.
  114. * Will also retry if the read function returns AVERROR(EAGAIN).
  115. * This makes special short-read handling in applications
  116. * unnecessary, if the return value is < size then it is
  117. * certain there was either an error or the end of file was reached.
  118. */
  119. int url_read_complete(URLContext *h, unsigned char *buf, int size);
  120. int url_write(URLContext *h, const unsigned char *buf, int size);
  121. /**
  122. * Changes the position that will be used by the next read/write
  123. * operation on the resource accessed by h.
  124. *
  125. * @param pos specifies the new position to set
  126. * @param whence specifies how pos should be interpreted, it must be
  127. * one of SEEK_SET (seek from the beginning), SEEK_CUR (seek from the
  128. * current position), SEEK_END (seek from the end), or AVSEEK_SIZE
  129. * (return the filesize of the requested resource, pos is ignored).
  130. * @return a negative value corresponding to an AVERROR code in case
  131. * of failure, or the resulting file position, measured in bytes from
  132. * the beginning of the file. You can use this feature together with
  133. * SEEK_CUR to read the current file position.
  134. */
  135. int64_t url_seek(URLContext *h, int64_t pos, int whence);
  136. /**
  137. * Closes the resource accessed by the URLContext h, and frees the
  138. * memory used by it.
  139. *
  140. * @return a negative value if an error condition occurred, 0
  141. * otherwise
  142. */
  143. int url_close(URLContext *h);
  144. /**
  145. * Returns a non-zero value if the resource indicated by url
  146. * exists, 0 otherwise.
  147. */
  148. int url_exist(const char *url);
  149. int64_t url_filesize(URLContext *h);
  150. /**
  151. * Return the file descriptor associated with this URL. For RTP, this
  152. * will return only the RTP file descriptor, not the RTCP file descriptor.
  153. * To get both, use rtp_get_file_handles().
  154. *
  155. * @return the file descriptor associated with this URL, or <0 on error.
  156. */
  157. int url_get_file_handle(URLContext *h);
  158. /**
  159. * Return the maximum packet size associated to packetized file
  160. * handle. If the file is not packetized (stream like HTTP or file on
  161. * disk), then 0 is returned.
  162. *
  163. * @param h file handle
  164. * @return maximum packet size in bytes
  165. */
  166. int url_get_max_packet_size(URLContext *h);
  167. void url_get_filename(URLContext *h, char *buf, int buf_size);
  168. /**
  169. * The callback is called in blocking functions to test regulary if
  170. * asynchronous interruption is needed. AVERROR(EINTR) is returned
  171. * in this case by the interrupted function. 'NULL' means no interrupt
  172. * callback is given.
  173. */
  174. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb);
  175. /* not implemented */
  176. int url_poll(URLPollEntry *poll_table, int n, int timeout);
  177. /**
  178. * Pause and resume playing - only meaningful if using a network streaming
  179. * protocol (e.g. MMS).
  180. * @param pause 1 for pause, 0 for resume
  181. */
  182. int av_url_read_pause(URLContext *h, int pause);
  183. /**
  184. * Seek to a given timestamp relative to some component stream.
  185. * Only meaningful if using a network streaming protocol (e.g. MMS.).
  186. * @param stream_index The stream index that the timestamp is relative to.
  187. * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
  188. * units from the beginning of the presentation.
  189. * If a stream_index >= 0 is used and the protocol does not support
  190. * seeking based on component streams, the call will fail with ENOTSUP.
  191. * @param timestamp timestamp in AVStream.time_base units
  192. * or if there is no stream specified then in AV_TIME_BASE units.
  193. * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
  194. * and AVSEEK_FLAG_ANY. The protocol may silently ignore
  195. * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
  196. * fail with ENOTSUP if used and not supported.
  197. * @return >= 0 on success
  198. * @see AVInputFormat::read_seek
  199. */
  200. int64_t av_url_read_seek(URLContext *h, int stream_index,
  201. int64_t timestamp, int flags);
  202. /**
  203. * Passing this as the "whence" parameter to a seek function causes it to
  204. * return the filesize without seeking anywhere. Supporting this is optional.
  205. * If it is not supported then the seek function will return <0.
  206. */
  207. #define AVSEEK_SIZE 0x10000
  208. /**
  209. * Oring this flag as into the "whence" parameter to a seek function causes it to
  210. * seek by any means (like reopening and linear reading) or other normally unreasonble
  211. * means that can be extreemly slow.
  212. * This may be ignored by the seek code.
  213. */
  214. #define AVSEEK_FORCE 0x20000
  215. typedef struct URLProtocol {
  216. const char *name;
  217. int (*url_open)(URLContext *h, const char *url, int flags);
  218. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  219. int (*url_write)(URLContext *h, const unsigned char *buf, int size);
  220. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  221. int (*url_close)(URLContext *h);
  222. struct URLProtocol *next;
  223. int (*url_read_pause)(URLContext *h, int pause);
  224. int64_t (*url_read_seek)(URLContext *h, int stream_index,
  225. int64_t timestamp, int flags);
  226. int (*url_get_file_handle)(URLContext *h);
  227. } URLProtocol;
  228. #if LIBAVFORMAT_VERSION_MAJOR < 53
  229. extern URLProtocol *first_protocol;
  230. #endif
  231. extern URLInterruptCB *url_interrupt_cb;
  232. /**
  233. * If protocol is NULL, returns the first registered protocol,
  234. * if protocol is non-NULL, returns the next registered protocol after protocol,
  235. * or NULL if protocol is the last one.
  236. */
  237. URLProtocol *av_protocol_next(URLProtocol *p);
  238. #if LIBAVFORMAT_VERSION_MAJOR < 53
  239. /**
  240. * @deprecated Use av_register_protocol() instead.
  241. */
  242. attribute_deprecated int register_protocol(URLProtocol *protocol);
  243. /**
  244. * @deprecated Use av_register_protocol2() instead.
  245. */
  246. attribute_deprecated int av_register_protocol(URLProtocol *protocol);
  247. #endif
  248. /**
  249. * Registers the URLProtocol protocol.
  250. *
  251. * @param size the size of the URLProtocol struct referenced
  252. */
  253. int av_register_protocol2(URLProtocol *protocol, int size);
  254. /**
  255. * Bytestream IO Context.
  256. * New fields can be added to the end with minor version bumps.
  257. * Removal, reordering and changes to existing fields require a major
  258. * version bump.
  259. * sizeof(ByteIOContext) must not be used outside libav*.
  260. */
  261. typedef struct {
  262. unsigned char *buffer;
  263. int buffer_size;
  264. unsigned char *buf_ptr, *buf_end;
  265. void *opaque;
  266. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  267. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  268. int64_t (*seek)(void *opaque, int64_t offset, int whence);
  269. int64_t pos; /**< position in the file of the current buffer */
  270. int must_flush; /**< true if the next seek should flush */
  271. int eof_reached; /**< true if eof reached */
  272. int write_flag; /**< true if open for writing */
  273. int is_streamed;
  274. int max_packet_size;
  275. unsigned long checksum;
  276. unsigned char *checksum_ptr;
  277. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  278. int error; ///< contains the error code or 0 if no error happened
  279. int (*read_pause)(void *opaque, int pause);
  280. int64_t (*read_seek)(void *opaque, int stream_index,
  281. int64_t timestamp, int flags);
  282. } ByteIOContext;
  283. int init_put_byte(ByteIOContext *s,
  284. unsigned char *buffer,
  285. int buffer_size,
  286. int write_flag,
  287. void *opaque,
  288. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  289. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  290. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  291. ByteIOContext *av_alloc_put_byte(
  292. unsigned char *buffer,
  293. int buffer_size,
  294. int write_flag,
  295. void *opaque,
  296. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  297. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  298. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  299. void put_byte(ByteIOContext *s, int b);
  300. void put_buffer(ByteIOContext *s, const unsigned char *buf, int size);
  301. void put_le64(ByteIOContext *s, uint64_t val);
  302. void put_be64(ByteIOContext *s, uint64_t val);
  303. void put_le32(ByteIOContext *s, unsigned int val);
  304. void put_be32(ByteIOContext *s, unsigned int val);
  305. void put_le24(ByteIOContext *s, unsigned int val);
  306. void put_be24(ByteIOContext *s, unsigned int val);
  307. void put_le16(ByteIOContext *s, unsigned int val);
  308. void put_be16(ByteIOContext *s, unsigned int val);
  309. void put_tag(ByteIOContext *s, const char *tag);
  310. void put_strz(ByteIOContext *s, const char *buf);
  311. /**
  312. * fseek() equivalent for ByteIOContext.
  313. * @return new position or AVERROR.
  314. */
  315. int64_t url_fseek(ByteIOContext *s, int64_t offset, int whence);
  316. /**
  317. * Skip given number of bytes forward.
  318. * @param offset number of bytes
  319. */
  320. void url_fskip(ByteIOContext *s, int64_t offset);
  321. /**
  322. * ftell() equivalent for ByteIOContext.
  323. * @return position or AVERROR.
  324. */
  325. int64_t url_ftell(ByteIOContext *s);
  326. /**
  327. * Gets the filesize.
  328. * @return filesize or AVERROR
  329. */
  330. int64_t url_fsize(ByteIOContext *s);
  331. /**
  332. * feof() equivalent for ByteIOContext.
  333. * @return non zero if and only if end of file
  334. */
  335. int url_feof(ByteIOContext *s);
  336. int url_ferror(ByteIOContext *s);
  337. int av_url_read_fpause(ByteIOContext *h, int pause);
  338. int64_t av_url_read_fseek(ByteIOContext *h, int stream_index,
  339. int64_t timestamp, int flags);
  340. #define URL_EOF (-1)
  341. /** @note return URL_EOF (-1) if EOF */
  342. int url_fgetc(ByteIOContext *s);
  343. /** @warning currently size is limited */
  344. #ifdef __GNUC__
  345. int url_fprintf(ByteIOContext *s, const char *fmt, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
  346. #else
  347. int url_fprintf(ByteIOContext *s, const char *fmt, ...);
  348. #endif
  349. /** @note unlike fgets, the EOL character is not returned and a whole
  350. line is parsed. return NULL if first char read was EOF */
  351. char *url_fgets(ByteIOContext *s, char *buf, int buf_size);
  352. void put_flush_packet(ByteIOContext *s);
  353. /**
  354. * Reads size bytes from ByteIOContext into buf.
  355. * @return number of bytes read or AVERROR
  356. */
  357. int get_buffer(ByteIOContext *s, unsigned char *buf, int size);
  358. /**
  359. * Reads size bytes from ByteIOContext into buf.
  360. * This reads at most 1 packet. If that is not enough fewer bytes will be
  361. * returned.
  362. * @return number of bytes read or AVERROR
  363. */
  364. int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size);
  365. /** @note return 0 if EOF, so you cannot use it if EOF handling is
  366. necessary */
  367. int get_byte(ByteIOContext *s);
  368. unsigned int get_le24(ByteIOContext *s);
  369. unsigned int get_le32(ByteIOContext *s);
  370. uint64_t get_le64(ByteIOContext *s);
  371. unsigned int get_le16(ByteIOContext *s);
  372. char *get_strz(ByteIOContext *s, char *buf, int maxlen);
  373. unsigned int get_be16(ByteIOContext *s);
  374. unsigned int get_be24(ByteIOContext *s);
  375. unsigned int get_be32(ByteIOContext *s);
  376. uint64_t get_be64(ByteIOContext *s);
  377. uint64_t ff_get_v(ByteIOContext *bc);
  378. static inline int url_is_streamed(ByteIOContext *s)
  379. {
  380. return s->is_streamed;
  381. }
  382. /**
  383. * Creates and initializes a ByteIOContext for accessing the
  384. * resource referenced by the URLContext h.
  385. * @note When the URLContext h has been opened in read+write mode, the
  386. * ByteIOContext can be used only for writing.
  387. *
  388. * @param s Used to return the pointer to the created ByteIOContext.
  389. * In case of failure the pointed to value is set to NULL.
  390. * @return 0 in case of success, a negative value corresponding to an
  391. * AVERROR code in case of failure
  392. */
  393. int url_fdopen(ByteIOContext **s, URLContext *h);
  394. /** @warning must be called before any I/O */
  395. int url_setbufsize(ByteIOContext *s, int buf_size);
  396. #if LIBAVFORMAT_VERSION_MAJOR < 53
  397. /** Reset the buffer for reading or writing.
  398. * @note Will drop any data currently in the buffer without transmitting it.
  399. * @param flags URL_RDONLY to set up the buffer for reading, or URL_WRONLY
  400. * to set up the buffer for writing. */
  401. int url_resetbuf(ByteIOContext *s, int flags);
  402. #endif
  403. /**
  404. * Rewinds the ByteIOContext using the specified buffer containing the first buf_size bytes of the file.
  405. * Used after probing to avoid seeking.
  406. * Joins buf and s->buffer, taking any overlap into consideration.
  407. * @note s->buffer must overlap with buf or they can't be joined and the function fails
  408. * @note This function is NOT part of the public API
  409. *
  410. * @param s The read-only ByteIOContext to rewind
  411. * @param buf The probe buffer containing the first buf_size bytes of the file
  412. * @param buf_size The size of buf
  413. * @return 0 in case of success, a negative value corresponding to an
  414. * AVERROR code in case of failure
  415. */
  416. int ff_rewind_with_probe_data(ByteIOContext *s, unsigned char *buf, int buf_size);
  417. /**
  418. * Creates and initializes a ByteIOContext for accessing the
  419. * resource indicated by url.
  420. * @note When the resource indicated by url has been opened in
  421. * read+write mode, the ByteIOContext can be used only for writing.
  422. *
  423. * @param s Used to return the pointer to the created ByteIOContext.
  424. * In case of failure the pointed to value is set to NULL.
  425. * @param flags flags which control how the resource indicated by url
  426. * is to be opened
  427. * @return 0 in case of success, a negative value corresponding to an
  428. * AVERROR code in case of failure
  429. */
  430. int url_fopen(ByteIOContext **s, const char *url, int flags);
  431. int url_fclose(ByteIOContext *s);
  432. URLContext *url_fileno(ByteIOContext *s);
  433. /**
  434. * Return the maximum packet size associated to packetized buffered file
  435. * handle. If the file is not packetized (stream like http or file on
  436. * disk), then 0 is returned.
  437. *
  438. * @param s buffered file handle
  439. * @return maximum packet size in bytes
  440. */
  441. int url_fget_max_packet_size(ByteIOContext *s);
  442. int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags);
  443. /** return the written or read size */
  444. int url_close_buf(ByteIOContext *s);
  445. /**
  446. * Open a write only memory stream.
  447. *
  448. * @param s new IO context
  449. * @return zero if no error.
  450. */
  451. int url_open_dyn_buf(ByteIOContext **s);
  452. /**
  453. * Open a write only packetized memory stream with a maximum packet
  454. * size of 'max_packet_size'. The stream is stored in a memory buffer
  455. * with a big endian 4 byte header giving the packet size in bytes.
  456. *
  457. * @param s new IO context
  458. * @param max_packet_size maximum packet size (must be > 0)
  459. * @return zero if no error.
  460. */
  461. int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size);
  462. /**
  463. * Return the written size and a pointer to the buffer. The buffer
  464. * must be freed with av_free().
  465. * @param s IO context
  466. * @param pbuffer pointer to a byte buffer
  467. * @return the length of the byte buffer
  468. */
  469. int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer);
  470. unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
  471. unsigned int len);
  472. unsigned long get_checksum(ByteIOContext *s);
  473. void init_checksum(ByteIOContext *s,
  474. unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
  475. unsigned long checksum);
  476. /* udp.c */
  477. int udp_set_remote_url(URLContext *h, const char *uri);
  478. int udp_get_local_port(URLContext *h);
  479. #if (LIBAVFORMAT_VERSION_MAJOR <= 52)
  480. int udp_get_file_handle(URLContext *h);
  481. #endif
  482. #endif /* AVFORMAT_AVIO_H */