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,
66 const URLProtocol **protocols)
72 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
75 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
77 err = AVERROR(ENOMEM);
80 uc->av_class = &ffurl_context_class;
81 uc->filename = (char *)&uc[1];
82 strcpy(uc->filename, filename);
85 uc->is_streamed = 0; /* default = not streamed */
86 uc->max_packet_size = 0; /* default: stream file */
87 uc->protocols = protocols;
88 if (up->priv_data_size) {
89 uc->priv_data = av_mallocz(up->priv_data_size);
91 err = AVERROR(ENOMEM);
94 if (up->priv_data_class) {
95 *(const AVClass **)uc->priv_data = up->priv_data_class;
96 av_opt_set_defaults(uc->priv_data);
100 uc->interrupt_callback = *int_cb;
107 av_freep(&uc->priv_data);
110 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
116 int ffurl_connect(URLContext *uc, AVDictionary **options)
119 uc->prot->url_open2 ? uc->prot->url_open2(uc,
123 uc->prot->url_open(uc, uc->filename, uc->flags);
126 uc->is_connected = 1;
127 /* We must be careful here as ffurl_seek() could be slow,
128 * for example for http */
129 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
130 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
135 #define URL_SCHEME_CHARS \
136 "abcdefghijklmnopqrstuvwxyz" \
137 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
140 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
141 const AVIOInterruptCB *int_cb,
142 const URLProtocol **protocols)
144 char proto_str[128], proto_nested[128], *ptr;
145 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
148 if (filename[proto_len] != ':' || is_dos_path(filename))
149 strcpy(proto_str, "file");
151 av_strlcpy(proto_str, filename,
152 FFMIN(proto_len + 1, sizeof(proto_str)));
154 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
155 if ((ptr = strchr(proto_nested, '+')))
158 for (i = 0; protocols[i]; i++) {
159 const URLProtocol *up = protocols[i];
160 if (!strcmp(proto_str, up->name))
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 return url_alloc_for_protocol(puc, up, filename, flags, int_cb,
169 return AVERROR_PROTOCOL_NOT_FOUND;
172 int ffurl_open(URLContext **puc, const char *filename, int flags,
173 const AVIOInterruptCB *int_cb, AVDictionary **options,
174 const URLProtocol **protocols)
176 int ret = ffurl_alloc(puc, filename, flags, int_cb, protocols);
180 (ret = av_opt_set_dict(*puc, options)) < 0)
182 if (options && (*puc)->prot->priv_data_class &&
183 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
185 ret = ffurl_connect(*puc, options);
194 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
195 int size, int size_min,
196 int (*transfer_func)(URLContext *h,
201 int fast_retries = 5;
204 while (len < size_min) {
205 ret = transfer_func(h, buf + len, size - len);
206 if (ret == AVERROR(EINTR))
208 if (h->flags & AVIO_FLAG_NONBLOCK)
210 if (ret == AVERROR(EAGAIN)) {
217 return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
219 fast_retries = FFMAX(fast_retries, 2);
221 if (ff_check_interrupt(&h->interrupt_callback))
227 int ffurl_read(URLContext *h, unsigned char *buf, int size)
229 if (!(h->flags & AVIO_FLAG_READ))
231 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
234 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
236 if (!(h->flags & AVIO_FLAG_READ))
238 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
241 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
243 if (!(h->flags & AVIO_FLAG_WRITE))
245 /* avoid sending too big packets */
246 if (h->max_packet_size && size > h->max_packet_size)
249 return retry_transfer_wrapper(h, buf, size, size,
250 (int (*)(struct URLContext *, uint8_t *, int))
254 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
258 if (!h->prot->url_seek)
259 return AVERROR(ENOSYS);
260 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
264 int ffurl_close(URLContext *h)
268 return 0; /* can happen when ffurl_open fails */
270 if (h->is_connected && h->prot->url_close)
271 ret = h->prot->url_close(h);
273 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
276 if (h->prot->priv_data_size) {
277 if (h->prot->priv_data_class)
278 av_opt_free(h->priv_data);
279 av_free(h->priv_data);
285 int avio_check(const char *url, int flags)
287 const URLProtocol **protocols;
291 protocols = ffurl_get_protocols(NULL, NULL);
293 return AVERROR(ENOMEM);
295 ret = ffurl_alloc(&h, url, flags, NULL, protocols);
297 av_freep(&protocols);
301 if (h->prot->url_check) {
302 ret = h->prot->url_check(h, flags);
304 ret = ffurl_connect(h, NULL);
310 av_freep(&protocols);
314 int64_t ffurl_size(URLContext *h)
318 size = ffurl_seek(h, 0, AVSEEK_SIZE);
320 pos = ffurl_seek(h, 0, SEEK_CUR);
321 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
324 ffurl_seek(h, pos, SEEK_SET);
329 int ffurl_get_file_handle(URLContext *h)
331 if (!h->prot->url_get_file_handle)
333 return h->prot->url_get_file_handle(h);
336 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
338 if (!h->prot->url_get_multi_file_handle) {
339 if (!h->prot->url_get_file_handle)
340 return AVERROR(ENOSYS);
341 *handles = av_malloc(sizeof(**handles));
343 return AVERROR(ENOMEM);
345 *handles[0] = h->prot->url_get_file_handle(h);
348 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
351 int ffurl_shutdown(URLContext *h, int flags)
353 if (!h->prot->url_shutdown)
354 return AVERROR(EINVAL);
355 return h->prot->url_shutdown(h, flags);
358 int ff_check_interrupt(AVIOInterruptCB *cb)
361 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))