avio.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. * @ingroup lavf_io
  25. * Buffered I/O operations
  26. */
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include "libavutil/attributes.h"
  30. #include "libavutil/dict.h"
  31. #include "libavutil/log.h"
  32. #include "libavformat/version_major.h"
  33. /**
  34. * Seeking works like for a local file.
  35. */
  36. #define AVIO_SEEKABLE_NORMAL (1 << 0)
  37. /**
  38. * Seeking by timestamp with avio_seek_time() is possible.
  39. */
  40. #define AVIO_SEEKABLE_TIME (1 << 1)
  41. /**
  42. * Callback for checking whether to abort blocking functions.
  43. * AVERROR_EXIT is returned in this case by the interrupted
  44. * function. During blocking operations, callback is called with
  45. * opaque as parameter. If the callback returns 1, the
  46. * blocking operation will be aborted.
  47. *
  48. * No members can be added to this struct without a major bump, if
  49. * new elements have been added after this struct in AVFormatContext
  50. * or AVIOContext.
  51. */
  52. typedef struct AVIOInterruptCB {
  53. int (*callback)(void*);
  54. void *opaque;
  55. } AVIOInterruptCB;
  56. /**
  57. * Directory entry types.
  58. */
  59. enum AVIODirEntryType {
  60. AVIO_ENTRY_UNKNOWN,
  61. AVIO_ENTRY_BLOCK_DEVICE,
  62. AVIO_ENTRY_CHARACTER_DEVICE,
  63. AVIO_ENTRY_DIRECTORY,
  64. AVIO_ENTRY_NAMED_PIPE,
  65. AVIO_ENTRY_SYMBOLIC_LINK,
  66. AVIO_ENTRY_SOCKET,
  67. AVIO_ENTRY_FILE,
  68. AVIO_ENTRY_SERVER,
  69. AVIO_ENTRY_SHARE,
  70. AVIO_ENTRY_WORKGROUP,
  71. };
  72. /**
  73. * Describes single entry of the directory.
  74. *
  75. * Only name and type fields are guaranteed be set.
  76. * Rest of fields are protocol or/and platform dependent and might be unknown.
  77. */
  78. typedef struct AVIODirEntry {
  79. char *name; /**< Filename */
  80. int type; /**< Type of the entry */
  81. int utf8; /**< Set to 1 when name is encoded with UTF-8, 0 otherwise.
  82. Name can be encoded with UTF-8 even though 0 is set. */
  83. int64_t size; /**< File size in bytes, -1 if unknown. */
  84. int64_t modification_timestamp; /**< Time of last modification in microseconds since unix
  85. epoch, -1 if unknown. */
  86. int64_t access_timestamp; /**< Time of last access in microseconds since unix epoch,
  87. -1 if unknown. */
  88. int64_t status_change_timestamp; /**< Time of last status change in microseconds since unix
  89. epoch, -1 if unknown. */
  90. int64_t user_id; /**< User ID of owner, -1 if unknown. */
  91. int64_t group_id; /**< Group ID of owner, -1 if unknown. */
  92. int64_t filemode; /**< Unix file mode, -1 if unknown. */
  93. } AVIODirEntry;
  94. typedef struct AVIODirContext {
  95. struct URLContext *url_context;
  96. } AVIODirContext;
  97. /**
  98. * Different data types that can be returned via the AVIO
  99. * write_data_type callback.
  100. */
  101. enum AVIODataMarkerType {
  102. /**
  103. * Header data; this needs to be present for the stream to be decodeable.
  104. */
  105. AVIO_DATA_MARKER_HEADER,
  106. /**
  107. * A point in the output bytestream where a decoder can start decoding
  108. * (i.e. a keyframe). A demuxer/decoder given the data flagged with
  109. * AVIO_DATA_MARKER_HEADER, followed by any AVIO_DATA_MARKER_SYNC_POINT,
  110. * should give decodeable results.
  111. */
  112. AVIO_DATA_MARKER_SYNC_POINT,
  113. /**
  114. * A point in the output bytestream where a demuxer can start parsing
  115. * (for non self synchronizing bytestream formats). That is, any
  116. * non-keyframe packet start point.
  117. */
  118. AVIO_DATA_MARKER_BOUNDARY_POINT,
  119. /**
  120. * This is any, unlabelled data. It can either be a muxer not marking
  121. * any positions at all, it can be an actual boundary/sync point
  122. * that the muxer chooses not to mark, or a later part of a packet/fragment
  123. * that is cut into multiple write callbacks due to limited IO buffer size.
  124. */
  125. AVIO_DATA_MARKER_UNKNOWN,
  126. /**
  127. * Trailer data, which doesn't contain actual content, but only for
  128. * finalizing the output file.
  129. */
  130. AVIO_DATA_MARKER_TRAILER,
  131. /**
  132. * A point in the output bytestream where the underlying AVIOContext might
  133. * flush the buffer depending on latency or buffering requirements. Typically
  134. * means the end of a packet.
  135. */
  136. AVIO_DATA_MARKER_FLUSH_POINT,
  137. };
  138. /**
  139. * Bytestream IO Context.
  140. * New public fields can be added with minor version bumps.
  141. * Removal, reordering and changes to existing public fields require
  142. * a major version bump.
  143. * sizeof(AVIOContext) must not be used outside libav*.
  144. *
  145. * @note None of the function pointers in AVIOContext should be called
  146. * directly, they should only be set by the client application
  147. * when implementing custom I/O. Normally these are set to the
  148. * function pointers specified in avio_alloc_context()
  149. */
  150. typedef struct AVIOContext {
  151. /**
  152. * A class for private options.
  153. *
  154. * If this AVIOContext is created by avio_open2(), av_class is set and
  155. * passes the options down to protocols.
  156. *
  157. * If this AVIOContext is manually allocated, then av_class may be set by
  158. * the caller.
  159. *
  160. * warning -- this field can be NULL, be sure to not pass this AVIOContext
  161. * to any av_opt_* functions in that case.
  162. */
  163. const AVClass *av_class;
  164. /*
  165. * The following shows the relationship between buffer, buf_ptr,
  166. * buf_ptr_max, buf_end, buf_size, and pos, when reading and when writing
  167. * (since AVIOContext is used for both):
  168. *
  169. **********************************************************************************
  170. * READING
  171. **********************************************************************************
  172. *
  173. * | buffer_size |
  174. * |---------------------------------------|
  175. * | |
  176. *
  177. * buffer buf_ptr buf_end
  178. * +---------------+-----------------------+
  179. * |/ / / / / / / /|/ / / / / / /| |
  180. * read buffer: |/ / consumed / | to be read /| |
  181. * |/ / / / / / / /|/ / / / / / /| |
  182. * +---------------+-----------------------+
  183. *
  184. * pos
  185. * +-------------------------------------------+-----------------+
  186. * input file: | | |
  187. * +-------------------------------------------+-----------------+
  188. *
  189. *
  190. **********************************************************************************
  191. * WRITING
  192. **********************************************************************************
  193. *
  194. * | buffer_size |
  195. * |--------------------------------------|
  196. * | |
  197. *
  198. * buf_ptr_max
  199. * buffer (buf_ptr) buf_end
  200. * +-----------------------+--------------+
  201. * |/ / / / / / / / / / / /| |
  202. * write buffer: | / / to be flushed / / | |
  203. * |/ / / / / / / / / / / /| |
  204. * +-----------------------+--------------+
  205. * buf_ptr can be in this
  206. * due to a backward seek
  207. *
  208. * pos
  209. * +-------------+----------------------------------------------+
  210. * output file: | | |
  211. * +-------------+----------------------------------------------+
  212. *
  213. */
  214. unsigned char *buffer; /**< Start of the buffer. */
  215. int buffer_size; /**< Maximum buffer size */
  216. unsigned char *buf_ptr; /**< Current position in the buffer */
  217. unsigned char *buf_end; /**< End of the data, may be less than
  218. buffer+buffer_size if the read function returned
  219. less data than requested, e.g. for streams where
  220. no more data has been received yet. */
  221. void *opaque; /**< A private pointer, passed to the read/write/seek/...
  222. functions. */
  223. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size);
  224. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size);
  225. int64_t (*seek)(void *opaque, int64_t offset, int whence);
  226. int64_t pos; /**< position in the file of the current buffer */
  227. int eof_reached; /**< true if was unable to read due to error or eof */
  228. int error; /**< contains the error code or 0 if no error happened */
  229. int write_flag; /**< true if open for writing */
  230. int max_packet_size;
  231. int min_packet_size; /**< Try to buffer at least this amount of data
  232. before flushing it. */
  233. unsigned long checksum;
  234. unsigned char *checksum_ptr;
  235. unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size);
  236. /**
  237. * Pause or resume playback for network streaming protocols - e.g. MMS.
  238. */
  239. int (*read_pause)(void *opaque, int pause);
  240. /**
  241. * Seek to a given timestamp in stream with the specified stream_index.
  242. * Needed for some network streaming protocols which don't support seeking
  243. * to byte position.
  244. */
  245. int64_t (*read_seek)(void *opaque, int stream_index,
  246. int64_t timestamp, int flags);
  247. /**
  248. * A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
  249. */
  250. int seekable;
  251. /**
  252. * avio_read and avio_write should if possible be satisfied directly
  253. * instead of going through a buffer, and avio_seek will always
  254. * call the underlying seek function directly.
  255. */
  256. int direct;
  257. /**
  258. * ',' separated list of allowed protocols.
  259. */
  260. const char *protocol_whitelist;
  261. /**
  262. * ',' separated list of disallowed protocols.
  263. */
  264. const char *protocol_blacklist;
  265. /**
  266. * A callback that is used instead of write_packet.
  267. */
  268. int (*write_data_type)(void *opaque, uint8_t *buf, int buf_size,
  269. enum AVIODataMarkerType type, int64_t time);
  270. /**
  271. * If set, don't call write_data_type separately for AVIO_DATA_MARKER_BOUNDARY_POINT,
  272. * but ignore them and treat them as AVIO_DATA_MARKER_UNKNOWN (to avoid needlessly
  273. * small chunks of data returned from the callback).
  274. */
  275. int ignore_boundary_point;
  276. #if FF_API_AVIOCONTEXT_WRITTEN
  277. /**
  278. * @deprecated field utilized privately by libavformat. For a public
  279. * statistic of how many bytes were written out, see
  280. * AVIOContext::bytes_written.
  281. */
  282. attribute_deprecated
  283. int64_t written;
  284. #endif
  285. /**
  286. * Maximum reached position before a backward seek in the write buffer,
  287. * used keeping track of already written data for a later flush.
  288. */
  289. unsigned char *buf_ptr_max;
  290. /**
  291. * Read-only statistic of bytes read for this AVIOContext.
  292. */
  293. int64_t bytes_read;
  294. /**
  295. * Read-only statistic of bytes written for this AVIOContext.
  296. */
  297. int64_t bytes_written;
  298. } AVIOContext;
  299. /**
  300. * Return the name of the protocol that will handle the passed URL.
  301. *
  302. * NULL is returned if no protocol could be found for the given URL.
  303. *
  304. * @return Name of the protocol or NULL.
  305. */
  306. const char *avio_find_protocol_name(const char *url);
  307. /**
  308. * Return AVIO_FLAG_* access flags corresponding to the access permissions
  309. * of the resource in url, or a negative value corresponding to an
  310. * AVERROR code in case of failure. The returned access flags are
  311. * masked by the value in flags.
  312. *
  313. * @note This function is intrinsically unsafe, in the sense that the
  314. * checked resource may change its existence or permission status from
  315. * one call to another. Thus you should not trust the returned value,
  316. * unless you are sure that no other processes are accessing the
  317. * checked resource.
  318. */
  319. int avio_check(const char *url, int flags);
  320. /**
  321. * Open directory for reading.
  322. *
  323. * @param s directory read context. Pointer to a NULL pointer must be passed.
  324. * @param url directory to be listed.
  325. * @param options A dictionary filled with protocol-private options. On return
  326. * this parameter will be destroyed and replaced with a dictionary
  327. * containing options that were not found. May be NULL.
  328. * @return >=0 on success or negative on error.
  329. */
  330. int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options);
  331. /**
  332. * Get next directory entry.
  333. *
  334. * Returned entry must be freed with avio_free_directory_entry(). In particular
  335. * it may outlive AVIODirContext.
  336. *
  337. * @param s directory read context.
  338. * @param[out] next next entry or NULL when no more entries.
  339. * @return >=0 on success or negative on error. End of list is not considered an
  340. * error.
  341. */
  342. int avio_read_dir(AVIODirContext *s, AVIODirEntry **next);
  343. /**
  344. * Close directory.
  345. *
  346. * @note Entries created using avio_read_dir() are not deleted and must be
  347. * freeded with avio_free_directory_entry().
  348. *
  349. * @param s directory read context.
  350. * @return >=0 on success or negative on error.
  351. */
  352. int avio_close_dir(AVIODirContext **s);
  353. /**
  354. * Free entry allocated by avio_read_dir().
  355. *
  356. * @param entry entry to be freed.
  357. */
  358. void avio_free_directory_entry(AVIODirEntry **entry);
  359. /**
  360. * Allocate and initialize an AVIOContext for buffered I/O. It must be later
  361. * freed with avio_context_free().
  362. *
  363. * @param buffer Memory block for input/output operations via AVIOContext.
  364. * The buffer must be allocated with av_malloc() and friends.
  365. * It may be freed and replaced with a new buffer by libavformat.
  366. * AVIOContext.buffer holds the buffer currently in use,
  367. * which must be later freed with av_free().
  368. * @param buffer_size The buffer size is very important for performance.
  369. * For protocols with fixed blocksize it should be set to this blocksize.
  370. * For others a typical size is a cache page, e.g. 4kb.
  371. * @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.
  372. * @param opaque An opaque pointer to user-specific data.
  373. * @param read_packet A function for refilling the buffer, may be NULL.
  374. * For stream protocols, must never return 0 but rather
  375. * a proper AVERROR code.
  376. * @param write_packet A function for writing the buffer contents, may be NULL.
  377. * The function may not change the input buffers content.
  378. * @param seek A function for seeking to specified byte position, may be NULL.
  379. *
  380. * @return Allocated AVIOContext or NULL on failure.
  381. */
  382. AVIOContext *avio_alloc_context(
  383. unsigned char *buffer,
  384. int buffer_size,
  385. int write_flag,
  386. void *opaque,
  387. int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
  388. int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
  389. int64_t (*seek)(void *opaque, int64_t offset, int whence));
  390. /**
  391. * Free the supplied IO context and everything associated with it.
  392. *
  393. * @param s Double pointer to the IO context. This function will write NULL
  394. * into s.
  395. */
  396. void avio_context_free(AVIOContext **s);
  397. void avio_w8(AVIOContext *s, int b);
  398. void avio_write(AVIOContext *s, const unsigned char *buf, int size);
  399. void avio_wl64(AVIOContext *s, uint64_t val);
  400. void avio_wb64(AVIOContext *s, uint64_t val);
  401. void avio_wl32(AVIOContext *s, unsigned int val);
  402. void avio_wb32(AVIOContext *s, unsigned int val);
  403. void avio_wl24(AVIOContext *s, unsigned int val);
  404. void avio_wb24(AVIOContext *s, unsigned int val);
  405. void avio_wl16(AVIOContext *s, unsigned int val);
  406. void avio_wb16(AVIOContext *s, unsigned int val);
  407. /**
  408. * Write a NULL-terminated string.
  409. * @return number of bytes written.
  410. */
  411. int avio_put_str(AVIOContext *s, const char *str);
  412. /**
  413. * Convert an UTF-8 string to UTF-16LE and write it.
  414. * @param s the AVIOContext
  415. * @param str NULL-terminated UTF-8 string
  416. *
  417. * @return number of bytes written.
  418. */
  419. int avio_put_str16le(AVIOContext *s, const char *str);
  420. /**
  421. * Convert an UTF-8 string to UTF-16BE and write it.
  422. * @param s the AVIOContext
  423. * @param str NULL-terminated UTF-8 string
  424. *
  425. * @return number of bytes written.
  426. */
  427. int avio_put_str16be(AVIOContext *s, const char *str);
  428. /**
  429. * Mark the written bytestream as a specific type.
  430. *
  431. * Zero-length ranges are omitted from the output.
  432. *
  433. * @param time the stream time the current bytestream pos corresponds to
  434. * (in AV_TIME_BASE units), or AV_NOPTS_VALUE if unknown or not
  435. * applicable
  436. * @param type the kind of data written starting at the current pos
  437. */
  438. void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type);
  439. /**
  440. * ORing this as the "whence" parameter to a seek function causes it to
  441. * return the filesize without seeking anywhere. Supporting this is optional.
  442. * If it is not supported then the seek function will return <0.
  443. */
  444. #define AVSEEK_SIZE 0x10000
  445. /**
  446. * Passing this flag as the "whence" parameter to a seek function causes it to
  447. * seek by any means (like reopening and linear reading) or other normally unreasonable
  448. * means that can be extremely slow.
  449. * This may be ignored by the seek code.
  450. */
  451. #define AVSEEK_FORCE 0x20000
  452. /**
  453. * fseek() equivalent for AVIOContext.
  454. * @return new position or AVERROR.
  455. */
  456. int64_t avio_seek(AVIOContext *s, int64_t offset, int whence);
  457. /**
  458. * Skip given number of bytes forward
  459. * @return new position or AVERROR.
  460. */
  461. int64_t avio_skip(AVIOContext *s, int64_t offset);
  462. /**
  463. * ftell() equivalent for AVIOContext.
  464. * @return position or AVERROR.
  465. */
  466. static av_always_inline int64_t avio_tell(AVIOContext *s)
  467. {
  468. return avio_seek(s, 0, SEEK_CUR);
  469. }
  470. /**
  471. * Get the filesize.
  472. * @return filesize or AVERROR
  473. */
  474. int64_t avio_size(AVIOContext *s);
  475. /**
  476. * Similar to feof() but also returns nonzero on read errors.
  477. * @return non zero if and only if at end of file or a read error happened when reading.
  478. */
  479. int avio_feof(AVIOContext *s);
  480. /**
  481. * Writes a formatted string to the context taking a va_list.
  482. * @return number of bytes written, < 0 on error.
  483. */
  484. int avio_vprintf(AVIOContext *s, const char *fmt, va_list ap);
  485. /**
  486. * Writes a formatted string to the context.
  487. * @return number of bytes written, < 0 on error.
  488. */
  489. int avio_printf(AVIOContext *s, const char *fmt, ...) av_printf_format(2, 3);
  490. /**
  491. * Write a NULL terminated array of strings to the context.
  492. * Usually you don't need to use this function directly but its macro wrapper,
  493. * avio_print.
  494. */
  495. void avio_print_string_array(AVIOContext *s, const char *strings[]);
  496. /**
  497. * Write strings (const char *) to the context.
  498. * This is a convenience macro around avio_print_string_array and it
  499. * automatically creates the string array from the variable argument list.
  500. * For simple string concatenations this function is more performant than using
  501. * avio_printf since it does not need a temporary buffer.
  502. */
  503. #define avio_print(s, ...) \
  504. avio_print_string_array(s, (const char*[]){__VA_ARGS__, NULL})
  505. /**
  506. * 强制刷新缓冲数据。
  507. *
  508. * 对于写流,强制立即将缓冲数据写入输出,而不必等待填充内部缓冲区。
  509. *
  510. * 对于读取流,丢弃所有当前缓冲的数据,并将报告的文件位置提前到基础流的位置。
  511. * 这不会读取新数据,也不会执行任何查找。
  512. */
  513. void avio_flush(AVIOContext *s);
  514. /**
  515. * Read size bytes from AVIOContext into buf.
  516. * @return number of bytes read or AVERROR
  517. */
  518. int avio_read(AVIOContext *s, unsigned char *buf, int size);
  519. /**
  520. * Read size bytes from AVIOContext into buf. Unlike avio_read(), this is allowed
  521. * to read fewer bytes than requested. The missing bytes can be read in the next
  522. * call. This always tries to read at least 1 byte.
  523. * Useful to reduce latency in certain cases.
  524. * @return number of bytes read or AVERROR
  525. */
  526. int avio_read_partial(AVIOContext *s, unsigned char *buf, int size);
  527. /**
  528. * @name Functions for reading from AVIOContext
  529. * @{
  530. *
  531. * @note return 0 if EOF, so you cannot use it if EOF handling is
  532. * necessary
  533. */
  534. int avio_r8 (AVIOContext *s);
  535. unsigned int avio_rl16(AVIOContext *s);
  536. unsigned int avio_rl24(AVIOContext *s);
  537. unsigned int avio_rl32(AVIOContext *s);
  538. uint64_t avio_rl64(AVIOContext *s);
  539. unsigned int avio_rb16(AVIOContext *s);
  540. unsigned int avio_rb24(AVIOContext *s);
  541. unsigned int avio_rb32(AVIOContext *s);
  542. uint64_t avio_rb64(AVIOContext *s);
  543. /**
  544. * @}
  545. */
  546. /**
  547. * Read a string from pb into buf. The reading will terminate when either
  548. * a NULL character was encountered, maxlen bytes have been read, or nothing
  549. * more can be read from pb. The result is guaranteed to be NULL-terminated, it
  550. * will be truncated if buf is too small.
  551. * Note that the string is not interpreted or validated in any way, it
  552. * might get truncated in the middle of a sequence for multi-byte encodings.
  553. *
  554. * @return number of bytes read (is always <= maxlen).
  555. * If reading ends on EOF or error, the return value will be one more than
  556. * bytes actually read.
  557. */
  558. int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen);
  559. /**
  560. * Read a UTF-16 string from pb and convert it to UTF-8.
  561. * The reading will terminate when either a null or invalid character was
  562. * encountered or maxlen bytes have been read.
  563. * @return number of bytes read (is always <= maxlen)
  564. */
  565. int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen);
  566. int avio_get_str16be(AVIOContext *pb, int maxlen, char *buf, int buflen);
  567. /**
  568. * @name URL open modes
  569. * The flags argument to avio_open must be one of the following
  570. * constants, optionally ORed with other flags.
  571. * @{
  572. */
  573. #define AVIO_FLAG_READ 1 /**< read-only */
  574. #define AVIO_FLAG_WRITE 2 /**< write-only */
  575. #define AVIO_FLAG_READ_WRITE (AVIO_FLAG_READ|AVIO_FLAG_WRITE) /**< read-write pseudo flag */
  576. /**
  577. * @}
  578. */
  579. /**
  580. * Use non-blocking mode.
  581. * If this flag is set, operations on the context will return
  582. * AVERROR(EAGAIN) if they can not be performed immediately.
  583. * If this flag is not set, operations on the context will never return
  584. * AVERROR(EAGAIN).
  585. * Note that this flag does not affect the opening/connecting of the
  586. * context. Connecting a protocol will always block if necessary (e.g. on
  587. * network protocols) but never hang (e.g. on busy devices).
  588. * Warning: non-blocking protocols is work-in-progress; this flag may be
  589. * silently ignored.
  590. */
  591. #define AVIO_FLAG_NONBLOCK 8
  592. /**
  593. * Use direct mode.
  594. * avio_read and avio_write should if possible be satisfied directly
  595. * instead of going through a buffer, and avio_seek will always
  596. * call the underlying seek function directly.
  597. */
  598. #define AVIO_FLAG_DIRECT 0x8000
  599. /**
  600. * Create and initialize a AVIOContext for accessing the
  601. * resource indicated by url.
  602. * @note When the resource indicated by url has been opened in
  603. * read+write mode, the AVIOContext can be used only for writing.
  604. *
  605. * @param s Used to return the pointer to the created AVIOContext.
  606. * In case of failure the pointed to value is set to NULL.
  607. * @param url resource to access
  608. * @param flags flags which control how the resource indicated by url
  609. * is to be opened
  610. * @return >= 0 in case of success, a negative value corresponding to an
  611. * AVERROR code in case of failure
  612. */
  613. int avio_open(AVIOContext **s, const char *url, int flags);
  614. /**
  615. * Create and initialize a AVIOContext for accessing the
  616. * resource indicated by url.
  617. * @note When the resource indicated by url has been opened in
  618. * read+write mode, the AVIOContext can be used only for writing.
  619. *
  620. * @param s Used to return the pointer to the created AVIOContext.
  621. * In case of failure the pointed to value is set to NULL.
  622. * @param url resource to access
  623. * @param flags flags which control how the resource indicated by url
  624. * is to be opened
  625. * @param int_cb an interrupt callback to be used at the protocols level
  626. * @param options A dictionary filled with protocol-private options. On return
  627. * this parameter will be destroyed and replaced with a dict containing options
  628. * that were not found. May be NULL.
  629. * @return >= 0 in case of success, a negative value corresponding to an
  630. * AVERROR code in case of failure
  631. */
  632. int avio_open2(AVIOContext **s, const char *url, int flags,
  633. const AVIOInterruptCB *int_cb, AVDictionary **options);
  634. /**
  635. * Close the resource accessed by the AVIOContext s and free it.
  636. * This function can only be used if s was opened by avio_open().
  637. *
  638. * The internal buffer is automatically flushed before closing the
  639. * resource.
  640. *
  641. * @return 0 on success, an AVERROR < 0 on error.
  642. * @see avio_closep
  643. */
  644. int avio_close(AVIOContext *s);
  645. /**
  646. * Close the resource accessed by the AVIOContext *s, free it
  647. * and set the pointer pointing to it to NULL.
  648. * This function can only be used if s was opened by avio_open().
  649. *
  650. * The internal buffer is automatically flushed before closing the
  651. * resource.
  652. *
  653. * @return 0 on success, an AVERROR < 0 on error.
  654. * @see avio_close
  655. */
  656. int avio_closep(AVIOContext **s);
  657. /**
  658. * Open a write only memory stream.
  659. *
  660. * @param s new IO context
  661. * @return zero if no error.
  662. */
  663. int avio_open_dyn_buf(AVIOContext **s);
  664. /**
  665. * Return the written size and a pointer to the buffer.
  666. * The AVIOContext stream is left intact.
  667. * The buffer must NOT be freed.
  668. * No padding is added to the buffer.
  669. *
  670. * @param s IO context
  671. * @param pbuffer pointer to a byte buffer
  672. * @return the length of the byte buffer
  673. */
  674. int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
  675. /**
  676. * Return the written size and a pointer to the buffer. The buffer
  677. * must be freed with av_free().
  678. * Padding of AV_INPUT_BUFFER_PADDING_SIZE is added to the buffer.
  679. *
  680. * @param s IO context
  681. * @param pbuffer pointer to a byte buffer
  682. * @return the length of the byte buffer
  683. */
  684. int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer);
  685. /**
  686. * Iterate through names of available protocols.
  687. *
  688. * @param opaque A private pointer representing current protocol.
  689. * It must be a pointer to NULL on first iteration and will
  690. * be updated by successive calls to avio_enum_protocols.
  691. * @param output If set to 1, iterate over output protocols,
  692. * otherwise over input protocols.
  693. *
  694. * @return A static string containing the name of current protocol or NULL
  695. */
  696. const char *avio_enum_protocols(void **opaque, int output);
  697. /**
  698. * Get AVClass by names of available protocols.
  699. *
  700. * @return A AVClass of input protocol name or NULL
  701. */
  702. const AVClass *avio_protocol_get_class(const char *name);
  703. /**
  704. * Pause and resume playing - only meaningful if using a network streaming
  705. * protocol (e.g. MMS).
  706. *
  707. * @param h IO context from which to call the read_pause function pointer
  708. * @param pause 1 for pause, 0 for resume
  709. */
  710. int avio_pause(AVIOContext *h, int pause);
  711. /**
  712. * Seek to a given timestamp relative to some component stream.
  713. * Only meaningful if using a network streaming protocol (e.g. MMS.).
  714. *
  715. * @param h IO context from which to call the seek function pointers
  716. * @param stream_index The stream index that the timestamp is relative to.
  717. * If stream_index is (-1) the timestamp should be in AV_TIME_BASE
  718. * units from the beginning of the presentation.
  719. * If a stream_index >= 0 is used and the protocol does not support
  720. * seeking based on component streams, the call will fail.
  721. * @param timestamp timestamp in AVStream.time_base units
  722. * or if there is no stream specified then in AV_TIME_BASE units.
  723. * @param flags Optional combination of AVSEEK_FLAG_BACKWARD, AVSEEK_FLAG_BYTE
  724. * and AVSEEK_FLAG_ANY. The protocol may silently ignore
  725. * AVSEEK_FLAG_BACKWARD and AVSEEK_FLAG_ANY, but AVSEEK_FLAG_BYTE will
  726. * fail if used and not supported.
  727. * @return >= 0 on success
  728. * @see AVInputFormat::read_seek
  729. */
  730. int64_t avio_seek_time(AVIOContext *h, int stream_index,
  731. int64_t timestamp, int flags);
  732. /* Avoid a warning. The header can not be included because it breaks c++. */
  733. struct AVBPrint;
  734. /**
  735. * Read contents of h into print buffer, up to max_size bytes, or up to EOF.
  736. *
  737. * @return 0 for success (max_size bytes read or EOF reached), negative error
  738. * code otherwise
  739. */
  740. int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size);
  741. /**
  742. * Accept and allocate a client context on a server context.
  743. * @param s the server context
  744. * @param c the client context, must be unallocated
  745. * @return >= 0 on success or a negative value corresponding
  746. * to an AVERROR on failure
  747. */
  748. int avio_accept(AVIOContext *s, AVIOContext **c);
  749. /**
  750. * Perform one step of the protocol handshake to accept a new client.
  751. * This function must be called on a client returned by avio_accept() before
  752. * using it as a read/write context.
  753. * It is separate from avio_accept() because it may block.
  754. * A step of the handshake is defined by places where the application may
  755. * decide to change the proceedings.
  756. * For example, on a protocol with a request header and a reply header, each
  757. * one can constitute a step because the application may use the parameters
  758. * from the request to change parameters in the reply; or each individual
  759. * chunk of the request can constitute a step.
  760. * If the handshake is already finished, avio_handshake() does nothing and
  761. * returns 0 immediately.
  762. *
  763. * @param c the client context to perform the handshake on
  764. * @return 0 on a complete and successful handshake
  765. * > 0 if the handshake progressed, but is not complete
  766. * < 0 for an AVERROR code
  767. */
  768. int avio_handshake(AVIOContext *c);
  769. #endif /* AVFORMAT_AVIO_H */