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.

301 lines
12KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * unbuffered private I/O API
  21. */
  22. #ifndef AVFORMAT_URL_H
  23. #define AVFORMAT_URL_H
  24. #include "avio.h"
  25. #include "libavformat/version.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/log.h"
  28. #define URL_PROTOCOL_FLAG_NESTED_SCHEME 1 /*< The protocol name can be the first part of a nested protocol scheme */
  29. #define URL_PROTOCOL_FLAG_NETWORK 2 /*< The protocol uses network */
  30. extern const AVClass ffurl_context_class;
  31. typedef struct URLContext {
  32. const AVClass *av_class; /**< information for av_log(). Set by url_open(). */
  33. const struct URLProtocol *prot;
  34. /**
  35. * A NULL-terminated list of protocols usable by the child contexts.
  36. */
  37. const struct URLProtocol **protocols;
  38. void *priv_data;
  39. char *filename; /**< specified URL */
  40. int flags;
  41. int max_packet_size; /**< if non zero, the stream is packetized with this max packet size */
  42. int is_streamed; /**< true if streamed (no seek possible), default = false */
  43. int is_connected;
  44. AVIOInterruptCB interrupt_callback;
  45. int64_t rw_timeout; /**< maximum time to wait for (network) read/write operation completion, in microseconds */
  46. } URLContext;
  47. typedef struct URLProtocol {
  48. const char *name;
  49. int (*url_open)( URLContext *h, const char *url, int flags);
  50. /**
  51. * This callback is to be used by protocols which open further nested
  52. * protocols. options are then to be passed to ffurl_open()/ffurl_connect()
  53. * for those nested protocols.
  54. */
  55. int (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options);
  56. /**
  57. * Read data from the protocol.
  58. * If data is immediately available (even less than size), EOF is
  59. * reached or an error occurs (including EINTR), return immediately.
  60. * Otherwise:
  61. * In non-blocking mode, return AVERROR(EAGAIN) immediately.
  62. * In blocking mode, wait for data/EOF/error with a short timeout (0.1s),
  63. * and return AVERROR(EAGAIN) on timeout.
  64. * Checking interrupt_callback, looping on EINTR and EAGAIN and until
  65. * enough data has been read is left to the calling function; see
  66. * retry_transfer_wrapper in avio.c.
  67. */
  68. int (*url_read)( URLContext *h, unsigned char *buf, int size);
  69. int (*url_write)(URLContext *h, const unsigned char *buf, int size);
  70. int64_t (*url_seek)( URLContext *h, int64_t pos, int whence);
  71. int (*url_close)(URLContext *h);
  72. int (*url_read_pause)(URLContext *h, int pause);
  73. int64_t (*url_read_seek)(URLContext *h, int stream_index,
  74. int64_t timestamp, int flags);
  75. int (*url_get_file_handle)(URLContext *h);
  76. int (*url_get_multi_file_handle)(URLContext *h, int **handles,
  77. int *numhandles);
  78. int (*url_shutdown)(URLContext *h, int flags);
  79. int priv_data_size;
  80. const AVClass *priv_data_class;
  81. int flags;
  82. int (*url_check)(URLContext *h, int mask);
  83. } URLProtocol;
  84. /**
  85. * Create a URLContext for accessing to the resource indicated by
  86. * url, but do not initiate the connection yet.
  87. *
  88. * @param puc pointer to the location where, in case of success, the
  89. * function puts the pointer to the created URLContext
  90. * @param flags flags which control how the resource indicated by url
  91. * is to be opened
  92. * @param int_cb interrupt callback to use for the URLContext, may be
  93. * NULL
  94. * @param protocols a NULL-terminate list of protocols available for use by
  95. * this context and its children. The caller must ensure this
  96. * list remains valid until the context is closed.
  97. * @return 0 in case of success, a negative value corresponding to an
  98. * AVERROR code in case of failure
  99. */
  100. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  101. const AVIOInterruptCB *int_cb,
  102. const URLProtocol **protocols);
  103. /**
  104. * Connect an URLContext that has been allocated by ffurl_alloc
  105. *
  106. * @param options A dictionary filled with options for nested protocols,
  107. * i.e. it will be passed to url_open2() for protocols implementing it.
  108. * This parameter will be destroyed and replaced with a dict containing options
  109. * that were not found. May be NULL.
  110. */
  111. int ffurl_connect(URLContext *uc, AVDictionary **options);
  112. /**
  113. * Create an URLContext for accessing to the resource indicated by
  114. * url, and open it.
  115. *
  116. * @param puc pointer to the location where, in case of success, the
  117. * function puts the pointer to the created URLContext
  118. * @param flags flags which control how the resource indicated by url
  119. * is to be opened
  120. * @param int_cb interrupt callback to use for the URLContext, may be
  121. * NULL
  122. * @param options A dictionary filled with protocol-private options. On return
  123. * this parameter will be destroyed and replaced with a dict containing options
  124. * that were not found. May be NULL.
  125. * @param protocols a NULL-terminate list of protocols available for use by
  126. * this context and its children. The caller must ensure this
  127. * list remains valid until the context is closed.
  128. * @param parent An enclosing URLContext, whose generic options should
  129. * be applied to this URLContext as well.
  130. * @return 0 in case of success, a negative value corresponding to an
  131. * AVERROR code in case of failure
  132. */
  133. int ffurl_open(URLContext **puc, const char *filename, int flags,
  134. const AVIOInterruptCB *int_cb, AVDictionary **options,
  135. const URLProtocol **protocols, URLContext *parent);
  136. /**
  137. * Read up to size bytes from the resource accessed by h, and store
  138. * the read bytes in buf.
  139. *
  140. * @return The number of bytes actually read, or a negative value
  141. * corresponding to an AVERROR code in case of error. A value of zero
  142. * indicates that it is not possible to read more from the accessed
  143. * resource (except if the value of the size argument is also zero).
  144. */
  145. int ffurl_read(URLContext *h, unsigned char *buf, int size);
  146. /**
  147. * Read as many bytes as possible (up to size), calling the
  148. * read function multiple times if necessary.
  149. * This makes special short-read handling in applications
  150. * unnecessary, if the return value is < size then it is
  151. * certain there was either an error or the end of file was reached.
  152. */
  153. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size);
  154. /**
  155. * Write size bytes from buf to the resource accessed by h.
  156. *
  157. * @return the number of bytes actually written, or a negative value
  158. * corresponding to an AVERROR code in case of failure
  159. */
  160. int ffurl_write(URLContext *h, const unsigned char *buf, int size);
  161. /**
  162. * Change the position that will be used by the next read/write
  163. * operation on the resource accessed by h.
  164. *
  165. * @param pos specifies the new position to set
  166. * @param whence specifies how pos should be interpreted, it must be
  167. * one of SEEK_SET (seek from the beginning), SEEK_CUR (seek from the
  168. * current position), SEEK_END (seek from the end), or AVSEEK_SIZE
  169. * (return the filesize of the requested resource, pos is ignored).
  170. * @return a negative value corresponding to an AVERROR code in case
  171. * of failure, or the resulting file position, measured in bytes from
  172. * the beginning of the file. You can use this feature together with
  173. * SEEK_CUR to read the current file position.
  174. */
  175. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence);
  176. /**
  177. * Close the resource accessed by the URLContext h, and free the
  178. * memory used by it.
  179. *
  180. * @return a negative value if an error condition occurred, 0
  181. * otherwise
  182. */
  183. int ffurl_close(URLContext *h);
  184. /**
  185. * Return the filesize of the resource accessed by h, AVERROR(ENOSYS)
  186. * if the operation is not supported by h, or another negative value
  187. * corresponding to an AVERROR error code in case of failure.
  188. */
  189. int64_t ffurl_size(URLContext *h);
  190. /**
  191. * Return the file descriptor associated with this URL. For RTP, this
  192. * will return only the RTP file descriptor, not the RTCP file descriptor.
  193. *
  194. * @return the file descriptor associated with this URL, or <0 on error.
  195. */
  196. int ffurl_get_file_handle(URLContext *h);
  197. /**
  198. * Return the file descriptors associated with this URL.
  199. *
  200. * @return 0 on success or <0 on error.
  201. */
  202. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles);
  203. /**
  204. * Signal the URLContext that we are done reading or writing the stream.
  205. *
  206. * @param h pointer to the resource
  207. * @param flags flags which control how the resource indicated by url
  208. * is to be shutdown
  209. *
  210. * @return a negative value if an error condition occurred, 0
  211. * otherwise
  212. */
  213. int ffurl_shutdown(URLContext *h, int flags);
  214. /**
  215. * Check if the user has requested to interrupt a blocking function
  216. * associated with cb.
  217. */
  218. int ff_check_interrupt(AVIOInterruptCB *cb);
  219. /* udp.c */
  220. int ff_udp_set_remote_url(URLContext *h, const char *uri);
  221. int ff_udp_get_local_port(URLContext *h);
  222. /**
  223. * Assemble a URL string from components. This is the reverse operation
  224. * of av_url_split.
  225. *
  226. * Note, this requires networking to be initialized, so the caller must
  227. * ensure ff_network_init has been called.
  228. *
  229. * @see av_url_split
  230. *
  231. * @param str the buffer to fill with the url
  232. * @param size the size of the str buffer
  233. * @param proto the protocol identifier, if null, the separator
  234. * after the identifier is left out, too
  235. * @param authorization an optional authorization string, may be null.
  236. * An empty string is treated the same as a null string.
  237. * @param hostname the host name string
  238. * @param port the port number, left out from the string if negative
  239. * @param fmt a generic format string for everything to add after the
  240. * host/port, may be null
  241. * @return the number of characters written to the destination buffer
  242. */
  243. int ff_url_join(char *str, int size, const char *proto,
  244. const char *authorization, const char *hostname,
  245. int port, const char *fmt, ...) av_printf_format(7, 8);
  246. /*
  247. * Convert a relative url into an absolute url, given a base url.
  248. *
  249. * @param buf the buffer where output absolute url is written
  250. * @param size the size of buf
  251. * @param base the base url, may be equal to buf.
  252. * @param rel the new url, which is interpreted relative to base
  253. */
  254. void ff_make_absolute_url(char *buf, int size, const char *base,
  255. const char *rel);
  256. const AVClass *ff_urlcontext_child_class_next(const AVClass *prev);
  257. /**
  258. * Construct a list of protocols matching a given whitelist and/or blacklist.
  259. *
  260. * @param whitelist a comma-separated list of allowed protocol names or NULL. If
  261. * this is a non-empty string, only protocols in this list will
  262. * be included.
  263. * @param blacklist a comma-separated list of forbidden protocol names or NULL.
  264. * If this is a non-empty string, all protocols in this list
  265. * will be excluded.
  266. *
  267. * @return a NULL-terminated array of matching protocols. The array must be
  268. * freed by the caller.
  269. */
  270. const URLProtocol **ffurl_get_protocols(const char *whitelist,
  271. const char *blacklist);
  272. #endif /* AVFORMAT_URL_H */