3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "os_support.h"
33 /** @name Logging context. */
35 static const char *urlcontext_to_name(void *ptr)
37 URLContext *h = (URLContext *)ptr;
44 static void *urlcontext_child_next(void *obj, void *prev)
47 if (!prev && h->priv_data && h->prot->priv_data_class)
52 static const AVOption options[] = {
53 { "rw_timeout", "Timeout for IO operations (in microseconds)", offsetof(URLContext, rw_timeout), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_DECODING_PARAM },
56 const AVClass ffurl_context_class = {
57 .class_name = "URLContext",
58 .item_name = urlcontext_to_name,
60 .version = LIBAVUTIL_VERSION_INT,
61 .child_next = urlcontext_child_next,
62 .child_class_next = ff_urlcontext_child_class_next,
66 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
67 const char *filename, int flags,
68 const AVIOInterruptCB *int_cb,
69 const URLProtocol **protocols)
75 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
78 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
80 err = AVERROR(ENOMEM);
83 uc->av_class = &ffurl_context_class;
84 uc->filename = (char *)&uc[1];
85 strcpy(uc->filename, filename);
88 uc->is_streamed = 0; /* default = not streamed */
89 uc->max_packet_size = 0; /* default: stream file */
90 uc->protocols = protocols;
91 if (up->priv_data_size) {
92 uc->priv_data = av_mallocz(up->priv_data_size);
94 err = AVERROR(ENOMEM);
97 if (up->priv_data_class) {
98 *(const AVClass **)uc->priv_data = up->priv_data_class;
99 av_opt_set_defaults(uc->priv_data);
103 uc->interrupt_callback = *int_cb;
110 av_freep(&uc->priv_data);
113 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
119 int ffurl_connect(URLContext *uc, AVDictionary **options)
122 uc->prot->url_open2 ? uc->prot->url_open2(uc,
126 uc->prot->url_open(uc, uc->filename, uc->flags);
129 uc->is_connected = 1;
130 /* We must be careful here as ffurl_seek() could be slow,
131 * for example for http */
132 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
133 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
138 #define URL_SCHEME_CHARS \
139 "abcdefghijklmnopqrstuvwxyz" \
140 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
143 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
144 const AVIOInterruptCB *int_cb,
145 const URLProtocol **protocols)
147 char proto_str[128], proto_nested[128], *ptr;
148 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
151 if (filename[proto_len] != ':' || is_dos_path(filename))
152 strcpy(proto_str, "file");
154 av_strlcpy(proto_str, filename,
155 FFMIN(proto_len + 1, sizeof(proto_str)));
157 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
158 if ((ptr = strchr(proto_nested, '+')))
161 for (i = 0; protocols[i]; i++) {
162 const URLProtocol *up = protocols[i];
163 if (!strcmp(proto_str, up->name))
164 return url_alloc_for_protocol(puc, up, filename, flags, int_cb,
166 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
167 !strcmp(proto_nested, up->name))
168 return url_alloc_for_protocol(puc, up, filename, flags, int_cb,
172 return AVERROR_PROTOCOL_NOT_FOUND;
175 int ffurl_open(URLContext **puc, const char *filename, int flags,
176 const AVIOInterruptCB *int_cb, AVDictionary **options,
177 const URLProtocol **protocols,
180 int ret = ffurl_alloc(puc, filename, flags, int_cb, protocols);
184 av_opt_copy(*puc, parent);
186 (ret = av_opt_set_dict(*puc, options)) < 0)
188 if (options && (*puc)->prot->priv_data_class &&
189 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
191 ret = ffurl_connect(*puc, options);
200 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
201 int size, int size_min,
202 int (*transfer_func)(URLContext *h,
207 int fast_retries = 5;
208 int64_t wait_since = 0;
211 while (len < size_min) {
212 ret = transfer_func(h, buf + len, size - len);
213 if (ret == AVERROR(EINTR))
215 if (h->flags & AVIO_FLAG_NONBLOCK)
217 if (ret == AVERROR(EAGAIN)) {
224 wait_since = av_gettime_relative();
225 else if (av_gettime_relative() > wait_since + h->rw_timeout)
231 return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
233 fast_retries = FFMAX(fast_retries, 2);
237 if (ff_check_interrupt(&h->interrupt_callback))
243 int ffurl_read(URLContext *h, unsigned char *buf, int size)
245 if (!(h->flags & AVIO_FLAG_READ))
247 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
250 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
252 if (!(h->flags & AVIO_FLAG_READ))
254 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
257 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
259 if (!(h->flags & AVIO_FLAG_WRITE))
261 /* avoid sending too big packets */
262 if (h->max_packet_size && size > h->max_packet_size)
265 return retry_transfer_wrapper(h, buf, size, size,
266 (int (*)(struct URLContext *, uint8_t *, int))
270 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
274 if (!h->prot->url_seek)
275 return AVERROR(ENOSYS);
276 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
280 int ffurl_close(URLContext *h)
284 return 0; /* can happen when ffurl_open fails */
286 if (h->is_connected && h->prot->url_close)
287 ret = h->prot->url_close(h);
289 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
292 if (h->prot->priv_data_size) {
293 if (h->prot->priv_data_class)
294 av_opt_free(h->priv_data);
295 av_free(h->priv_data);
301 int avio_check(const char *url, int flags)
303 const URLProtocol **protocols;
307 protocols = ffurl_get_protocols(NULL, NULL);
309 return AVERROR(ENOMEM);
311 ret = ffurl_alloc(&h, url, flags, NULL, protocols);
313 av_freep(&protocols);
317 if (h->prot->url_check) {
318 ret = h->prot->url_check(h, flags);
320 ret = ffurl_connect(h, NULL);
326 av_freep(&protocols);
330 int64_t ffurl_size(URLContext *h)
334 size = ffurl_seek(h, 0, AVSEEK_SIZE);
336 pos = ffurl_seek(h, 0, SEEK_CUR);
337 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
340 ffurl_seek(h, pos, SEEK_SET);
345 int ffurl_get_file_handle(URLContext *h)
347 if (!h->prot->url_get_file_handle)
349 return h->prot->url_get_file_handle(h);
352 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
354 if (!h->prot->url_get_multi_file_handle) {
355 if (!h->prot->url_get_file_handle)
356 return AVERROR(ENOSYS);
357 *handles = av_malloc(sizeof(**handles));
359 return AVERROR(ENOMEM);
361 *handles[0] = h->prot->url_get_file_handle(h);
364 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
367 int ffurl_shutdown(URLContext *h, int flags)
369 if (!h->prot->url_shutdown)
370 return AVERROR(EINVAL);
371 return h->prot->url_shutdown(h, flags);
374 int ff_check_interrupt(AVIOInterruptCB *cb)
377 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))