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.

577 lines
20KB

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