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[] = { { NULL } };
53 const AVClass ffurl_context_class = {
54 .class_name = "URLContext",
55 .item_name = urlcontext_to_name,
57 .version = LIBAVUTIL_VERSION_INT,
58 .child_next = urlcontext_child_next,
59 .child_class_next = ff_urlcontext_child_class_next,
63 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
64 const char *filename, int flags,
65 const AVIOInterruptCB *int_cb)
71 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
74 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
76 err = AVERROR(ENOMEM);
79 uc->av_class = &ffurl_context_class;
80 uc->filename = (char *)&uc[1];
81 strcpy(uc->filename, filename);
84 uc->is_streamed = 0; /* default = not streamed */
85 uc->max_packet_size = 0; /* default: stream file */
86 if (up->priv_data_size) {
87 uc->priv_data = av_mallocz(up->priv_data_size);
89 err = AVERROR(ENOMEM);
92 if (up->priv_data_class) {
93 *(const AVClass **)uc->priv_data = up->priv_data_class;
94 av_opt_set_defaults(uc->priv_data);
98 uc->interrupt_callback = *int_cb;
105 av_freep(&uc->priv_data);
108 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
114 int ffurl_connect(URLContext *uc, AVDictionary **options)
117 uc->prot->url_open2 ? uc->prot->url_open2(uc,
121 uc->prot->url_open(uc, uc->filename, uc->flags);
124 uc->is_connected = 1;
125 /* We must be careful here as ffurl_seek() could be slow,
126 * for example for http */
127 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
128 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
133 #define URL_SCHEME_CHARS \
134 "abcdefghijklmnopqrstuvwxyz" \
135 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
138 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
139 const AVIOInterruptCB *int_cb)
141 const URLProtocol **protocols;
142 char proto_str[128], proto_nested[128], *ptr;
143 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
146 if (filename[proto_len] != ':' || is_dos_path(filename))
147 strcpy(proto_str, "file");
149 av_strlcpy(proto_str, filename,
150 FFMIN(proto_len + 1, sizeof(proto_str)));
152 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
153 if ((ptr = strchr(proto_nested, '+')))
156 protocols = ffurl_get_protocols(NULL, NULL);
157 for (i = 0; protocols[i]; i++) {
158 const URLProtocol *up = protocols[i];
159 if (!strcmp(proto_str, up->name)) {
160 av_freep(&protocols);
161 return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
163 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
164 !strcmp(proto_nested, up->name)) {
165 av_freep(&protocols);
166 return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
170 return AVERROR_PROTOCOL_NOT_FOUND;
173 int ffurl_open(URLContext **puc, const char *filename, int flags,
174 const AVIOInterruptCB *int_cb, AVDictionary **options)
176 int ret = ffurl_alloc(puc, filename, flags, int_cb);
179 if (options && (*puc)->prot->priv_data_class &&
180 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
182 ret = ffurl_connect(*puc, options);
191 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
192 int size, int size_min,
193 int (*transfer_func)(URLContext *h,
198 int fast_retries = 5;
201 while (len < size_min) {
202 ret = transfer_func(h, buf + len, size - len);
203 if (ret == AVERROR(EINTR))
205 if (h->flags & AVIO_FLAG_NONBLOCK)
207 if (ret == AVERROR(EAGAIN)) {
214 return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
216 fast_retries = FFMAX(fast_retries, 2);
218 if (ff_check_interrupt(&h->interrupt_callback))
224 int ffurl_read(URLContext *h, unsigned char *buf, int size)
226 if (!(h->flags & AVIO_FLAG_READ))
228 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
231 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
233 if (!(h->flags & AVIO_FLAG_READ))
235 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
238 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
240 if (!(h->flags & AVIO_FLAG_WRITE))
242 /* avoid sending too big packets */
243 if (h->max_packet_size && size > h->max_packet_size)
246 return retry_transfer_wrapper(h, buf, size, size,
247 (int (*)(struct URLContext *, uint8_t *, int))
251 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
255 if (!h->prot->url_seek)
256 return AVERROR(ENOSYS);
257 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
261 int ffurl_close(URLContext *h)
265 return 0; /* can happen when ffurl_open fails */
267 if (h->is_connected && h->prot->url_close)
268 ret = h->prot->url_close(h);
270 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
273 if (h->prot->priv_data_size) {
274 if (h->prot->priv_data_class)
275 av_opt_free(h->priv_data);
276 av_free(h->priv_data);
282 int avio_check(const char *url, int flags)
285 int ret = ffurl_alloc(&h, url, flags, NULL);
289 if (h->prot->url_check) {
290 ret = h->prot->url_check(h, flags);
292 ret = ffurl_connect(h, NULL);
301 int64_t ffurl_size(URLContext *h)
305 size = ffurl_seek(h, 0, AVSEEK_SIZE);
307 pos = ffurl_seek(h, 0, SEEK_CUR);
308 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
311 ffurl_seek(h, pos, SEEK_SET);
316 int ffurl_get_file_handle(URLContext *h)
318 if (!h->prot->url_get_file_handle)
320 return h->prot->url_get_file_handle(h);
323 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
325 if (!h->prot->url_get_multi_file_handle) {
326 if (!h->prot->url_get_file_handle)
327 return AVERROR(ENOSYS);
328 *handles = av_malloc(sizeof(**handles));
330 return AVERROR(ENOMEM);
332 *handles[0] = h->prot->url_get_file_handle(h);
335 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
338 int ffurl_shutdown(URLContext *h, int flags)
340 if (!h->prot->url_shutdown)
341 return AVERROR(EINVAL);
342 return h->prot->url_shutdown(h, flags);
345 int ff_check_interrupt(AVIOInterruptCB *cb)
348 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))