Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavformat / url.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * unbuffered private I/O API
22  */
23
24 #ifndef AVFORMAT_URL_H
25 #define AVFORMAT_URL_H
26
27 #include "avio.h"
28
29 #include "libavutil/dict.h"
30 #include "libavutil/log.h"
31
32 #define URL_PROTOCOL_FLAG_NESTED_SCHEME 1 /*< The protocol name can be the first part of a nested protocol scheme */
33 #define URL_PROTOCOL_FLAG_NETWORK       2 /*< The protocol uses network */
34
35 extern const AVClass ffurl_context_class;
36
37 typedef struct URLContext {
38     const AVClass *av_class;    /**< information for av_log(). Set by url_open(). */
39     const struct URLProtocol *prot;
40     void *priv_data;
41     char *filename;             /**< specified URL */
42     int flags;
43     int max_packet_size;        /**< if non zero, the stream is packetized with this max packet size */
44     int is_streamed;            /**< true if streamed (no seek possible), default = false */
45     int is_connected;
46     AVIOInterruptCB interrupt_callback;
47     int64_t rw_timeout;         /**< maximum time to wait for (network) read/write operation completion, in mcs */
48     const char *protocol_whitelist;
49     const char *protocol_blacklist;
50     int min_packet_size;        /**< if non zero, the stream is packetized with this min packet size */
51 } URLContext;
52
53 typedef struct URLProtocol {
54     const char *name;
55     int     (*url_open)( URLContext *h, const char *url, int flags);
56     /**
57      * This callback is to be used by protocols which open further nested
58      * protocols. options are then to be passed to ffurl_open_whitelist()
59      * or ffurl_connect() for those nested protocols.
60      */
61     int     (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options);
62     int     (*url_accept)(URLContext *s, URLContext **c);
63     int     (*url_handshake)(URLContext *c);
64
65     /**
66      * Read data from the protocol.
67      * If data is immediately available (even less than size), EOF is
68      * reached or an error occurs (including EINTR), return immediately.
69      * Otherwise:
70      * In non-blocking mode, return AVERROR(EAGAIN) immediately.
71      * In blocking mode, wait for data/EOF/error with a short timeout (0.1s),
72      * and return AVERROR(EAGAIN) on timeout.
73      * Checking interrupt_callback, looping on EINTR and EAGAIN and until
74      * enough data has been read is left to the calling function; see
75      * retry_transfer_wrapper in avio.c.
76      */
77     int     (*url_read)( URLContext *h, unsigned char *buf, int size);
78     int     (*url_write)(URLContext *h, const unsigned char *buf, int size);
79     int64_t (*url_seek)( URLContext *h, int64_t pos, int whence);
80     int     (*url_close)(URLContext *h);
81     int (*url_read_pause)(URLContext *h, int pause);
82     int64_t (*url_read_seek)(URLContext *h, int stream_index,
83                              int64_t timestamp, int flags);
84     int (*url_get_file_handle)(URLContext *h);
85     int (*url_get_multi_file_handle)(URLContext *h, int **handles,
86                                      int *numhandles);
87     int (*url_get_short_seek)(URLContext *h);
88     int (*url_shutdown)(URLContext *h, int flags);
89     const AVClass *priv_data_class;
90     int priv_data_size;
91     int flags;
92     int (*url_check)(URLContext *h, int mask);
93     int (*url_open_dir)(URLContext *h);
94     int (*url_read_dir)(URLContext *h, AVIODirEntry **next);
95     int (*url_close_dir)(URLContext *h);
96     int (*url_delete)(URLContext *h);
97     int (*url_move)(URLContext *h_src, URLContext *h_dst);
98     const char *default_whitelist;
99 } URLProtocol;
100
101 /**
102  * Create a URLContext for accessing to the resource indicated by
103  * url, but do not initiate the connection yet.
104  *
105  * @param puc pointer to the location where, in case of success, the
106  * function puts the pointer to the created URLContext
107  * @param flags flags which control how the resource indicated by url
108  * is to be opened
109  * @param int_cb interrupt callback to use for the URLContext, may be
110  * NULL
111  * @return >= 0 in case of success, a negative value corresponding to an
112  * AVERROR code in case of failure
113  */
114 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
115                 const AVIOInterruptCB *int_cb);
116
117 /**
118  * Connect an URLContext that has been allocated by ffurl_alloc
119  *
120  * @param options  A dictionary filled with options for nested protocols,
121  * i.e. it will be passed to url_open2() for protocols implementing it.
122  * This parameter will be destroyed and replaced with a dict containing options
123  * that were not found. May be NULL.
124  */
125 int ffurl_connect(URLContext *uc, AVDictionary **options);
126
127 /**
128  * Create an URLContext for accessing to the resource indicated by
129  * url, and open it.
130  *
131  * @param puc pointer to the location where, in case of success, the
132  * function puts the pointer to the created URLContext
133  * @param flags flags which control how the resource indicated by url
134  * is to be opened
135  * @param int_cb interrupt callback to use for the URLContext, may be
136  * NULL
137  * @param options  A dictionary filled with protocol-private options. On return
138  * this parameter will be destroyed and replaced with a dict containing options
139  * that were not found. May be NULL.
140  * @param parent An enclosing URLContext, whose generic options should
141  *               be applied to this URLContext as well.
142  * @return >= 0 in case of success, a negative value corresponding to an
143  * AVERROR code in case of failure
144  */
145 int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
146                const AVIOInterruptCB *int_cb, AVDictionary **options,
147                const char *whitelist, const char* blacklist,
148                URLContext *parent);
149
150 /**
151  * Accept an URLContext c on an URLContext s
152  *
153  * @param  s server context
154  * @param  c client context, must be unallocated.
155  * @return >= 0 on success, ff_neterrno() on failure.
156  */
157 int ffurl_accept(URLContext *s, URLContext **c);
158
159 /**
160  * Perform one step of the protocol handshake to accept a new client.
161  * See avio_handshake() for details.
162  * Implementations should try to return decreasing values.
163  * If the protocol uses an underlying protocol, the underlying handshake is
164  * usually the first step, and the return value can be:
165  * (largest value for this protocol) + (return value from other protocol)
166  *
167  * @param  c the client context
168  * @return >= 0 on success or a negative value corresponding
169  *         to an AVERROR code on failure
170  */
171 int ffurl_handshake(URLContext *c);
172
173 int ffurl_read2(void *urlcontext, uint8_t *buf, int size);
174 /**
175  * Read up to size bytes from the resource accessed by h, and store
176  * the read bytes in buf.
177  *
178  * @return The number of bytes actually read, or a negative value
179  * corresponding to an AVERROR code in case of error. A value of zero
180  * indicates that it is not possible to read more from the accessed
181  * resource (except if the value of the size argument is also zero).
182  */
183 static inline int ffurl_read(URLContext *h, uint8_t *buf, int size)
184 {
185     return ffurl_read2(h, buf, size);
186 }
187
188 /**
189  * Read as many bytes as possible (up to size), calling the
190  * read function multiple times if necessary.
191  * This makes special short-read handling in applications
192  * unnecessary, if the return value is < size then it is
193  * certain there was either an error or the end of file was reached.
194  */
195 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size);
196
197 #if FF_API_AVIO_WRITE_NONCONST
198 int ffurl_write2(void *urlcontext, uint8_t *buf, int size);
199 #else
200 int ffurl_write2(void *urlcontext, const uint8_t *buf, int size);
201 #endif
202 /**
203  * Write size bytes from buf to the resource accessed by h.
204  *
205  * @return the number of bytes actually written, or a negative value
206  * corresponding to an AVERROR code in case of failure
207  */
208 static inline int ffurl_write(URLContext *h, const uint8_t *buf, int size)
209 {
210 #if FF_API_AVIO_WRITE_NONCONST
211     return ffurl_write2(h, (uint8_t*)buf, size);
212 #else
213     return ffurl_write2(h, buf, size);
214 #endif
215 }
216
217 int64_t ffurl_seek2(void *urlcontext, int64_t pos, int whence);
218 /**
219  * Change the position that will be used by the next read/write
220  * operation on the resource accessed by h.
221  *
222  * @param pos specifies the new position to set
223  * @param whence specifies how pos should be interpreted, it must be
224  * one of SEEK_SET (seek from the beginning), SEEK_CUR (seek from the
225  * current position), SEEK_END (seek from the end), or AVSEEK_SIZE
226  * (return the filesize of the requested resource, pos is ignored).
227  * @return a negative value corresponding to an AVERROR code in case
228  * of failure, or the resulting file position, measured in bytes from
229  * the beginning of the file. You can use this feature together with
230  * SEEK_CUR to read the current file position.
231  */
232 static inline int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
233 {
234     return ffurl_seek2(h, pos, whence);
235 }
236
237 /**
238  * Close the resource accessed by the URLContext h, and free the
239  * memory used by it. Also set the URLContext pointer to NULL.
240  *
241  * @return a negative value if an error condition occurred, 0
242  * otherwise
243  */
244 int ffurl_closep(URLContext **h);
245 int ffurl_close(URLContext *h);
246
247 /**
248  * Return the filesize of the resource accessed by h, AVERROR(ENOSYS)
249  * if the operation is not supported by h, or another negative value
250  * corresponding to an AVERROR error code in case of failure.
251  */
252 int64_t ffurl_size(URLContext *h);
253
254 /**
255  * Return the file descriptor associated with this URL. For RTP, this
256  * will return only the RTP file descriptor, not the RTCP file descriptor.
257  *
258  * @return the file descriptor associated with this URL, or <0 on error.
259  */
260 int ffurl_get_file_handle(URLContext *h);
261
262 /**
263  * Return the file descriptors associated with this URL.
264  *
265  * @return 0 on success or <0 on error.
266  */
267 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles);
268
269 /**
270  * Return the current short seek threshold value for this URL.
271  *
272  * @return threshold (>0) on success or <=0 on error.
273  */
274 int ffurl_get_short_seek(void *urlcontext);
275
276 /**
277  * Signal the URLContext that we are done reading or writing the stream.
278  *
279  * @param h pointer to the resource
280  * @param flags flags which control how the resource indicated by url
281  * is to be shutdown
282  *
283  * @return a negative value if an error condition occurred, 0
284  * otherwise
285  */
286 int ffurl_shutdown(URLContext *h, int flags);
287
288 /**
289  * Check if the user has requested to interrupt a blocking function
290  * associated with cb.
291  */
292 int ff_check_interrupt(AVIOInterruptCB *cb);
293
294 /* udp.c */
295 int ff_udp_set_remote_url(URLContext *h, const char *uri);
296 int ff_udp_get_local_port(URLContext *h);
297
298 /**
299  * Assemble a URL string from components. This is the reverse operation
300  * of av_url_split.
301  *
302  * Note, this requires networking to be initialized, so the caller must
303  * ensure ff_network_init has been called.
304  *
305  * @see av_url_split
306  *
307  * @param str the buffer to fill with the url
308  * @param size the size of the str buffer
309  * @param proto the protocol identifier, if null, the separator
310  *              after the identifier is left out, too
311  * @param authorization an optional authorization string, may be null.
312  *                      An empty string is treated the same as a null string.
313  * @param hostname the host name string
314  * @param port the port number, left out from the string if negative
315  * @param fmt a generic format string for everything to add after the
316  *            host/port, may be null
317  * @return the number of characters written to the destination buffer
318  */
319 int ff_url_join(char *str, int size, const char *proto,
320                 const char *authorization, const char *hostname,
321                 int port, const char *fmt, ...) av_printf_format(7, 8);
322
323 /**
324  * Convert a relative url into an absolute url, given a base url.
325  *
326  * @param buf the buffer where output absolute url is written
327  * @param size the size of buf
328  * @param base the base url, may be equal to buf.
329  * @param rel the new url, which is interpreted relative to base
330  * @param handle_dos_paths handle DOS paths for file or unspecified protocol
331  */
332 int ff_make_absolute_url2(char *buf, int size, const char *base,
333                          const char *rel, int handle_dos_paths);
334
335 /**
336  * Convert a relative url into an absolute url, given a base url.
337  *
338  * Same as ff_make_absolute_url2 with handle_dos_paths being equal to
339  * HAVE_DOS_PATHS config variable.
340  */
341 int ff_make_absolute_url(char *buf, int size, const char *base,
342                          const char *rel);
343
344 /**
345  * Allocate directory entry with default values.
346  *
347  * @return entry or NULL on error
348  */
349 AVIODirEntry *ff_alloc_dir_entry(void);
350
351 const AVClass *ff_urlcontext_child_class_iterate(void **iter);
352
353 /**
354  * Construct a list of protocols matching a given whitelist and/or blacklist.
355  *
356  * @param whitelist a comma-separated list of allowed protocol names or NULL. If
357  *                  this is a non-empty string, only protocols in this list will
358  *                  be included.
359  * @param blacklist a comma-separated list of forbidden protocol names or NULL.
360  *                  If this is a non-empty string, all protocols in this list
361  *                  will be excluded.
362  *
363  * @return a NULL-terminated array of matching protocols. The array must be
364  * freed by the caller.
365  */
366 const URLProtocol **ffurl_get_protocols(const char *whitelist,
367                                         const char *blacklist);
368
369 typedef struct URLComponents {
370     const char *url;        /**< whole URL, for reference */
371     const char *scheme;     /**< possibly including lavf-specific options */
372     const char *authority;  /**< "//" if it is a real URL */
373     const char *userinfo;   /**< including final '@' if present */
374     const char *host;
375     const char *port;       /**< including initial ':' if present */
376     const char *path;
377     const char *query;      /**< including initial '?' if present */
378     const char *fragment;   /**< including initial '#' if present */
379     const char *end;
380 } URLComponents;
381
382 #define url_component_end_scheme      authority
383 #define url_component_end_authority   userinfo
384 #define url_component_end_userinfo    host
385 #define url_component_end_host        port
386 #define url_component_end_port        path
387 #define url_component_end_path        query
388 #define url_component_end_query       fragment
389 #define url_component_end_fragment    end
390 #define url_component_end_authority_full path
391
392 #define URL_COMPONENT_HAVE(uc, component) \
393     ((uc).url_component_end_##component > (uc).component)
394
395 /**
396  * Parse an URL to find the components.
397  *
398  * Each component runs until the start of the next component,
399  * possibly including a mandatory delimiter.
400  *
401  * @param uc   structure to fill with pointers to the components.
402  * @param url  URL to parse.
403  * @param end  end of the URL, or NULL to parse to the end of string.
404  *
405  * @return  >= 0 for success or an AVERROR code, especially if the URL is
406  *          malformed.
407  */
408 int ff_url_decompose(URLComponents *uc, const char *url, const char *end);
409
410 /**
411  * Move or rename a resource.
412  *
413  * @note url_src and url_dst should share the same protocol and authority.
414  *
415  * @param url_src url to resource to be moved
416  * @param url_dst new url to resource if the operation succeeded
417  * @return >=0 on success or negative on error.
418  */
419 int ffurl_move(const char *url_src, const char *url_dst);
420
421 /**
422  * Delete a resource.
423  *
424  * @param url resource to be deleted.
425  * @return >=0 on success or negative on error.
426  */
427 int ffurl_delete(const char *url);
428
429 #endif /* AVFORMAT_URL_H */