3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 static URLProtocol *first_protocol = NULL;
35 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
37 return prev ? prev->next : first_protocol;
40 /** @name Logging context. */
42 static const char *urlcontext_to_name(void *ptr)
44 URLContext *h = (URLContext *)ptr;
45 if(h->prot) return h->prot->name;
49 static void *urlcontext_child_next(void *obj, void *prev)
52 if (!prev && h->priv_data && h->prot->priv_data_class)
57 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
59 URLProtocol *p = NULL;
61 /* find the protocol that corresponds to prev */
62 while (prev && (p = ffurl_protocol_next(p)))
63 if (p->priv_data_class == prev)
66 /* find next protocol with priv options */
67 while (p = ffurl_protocol_next(p))
68 if (p->priv_data_class)
69 return p->priv_data_class;
74 static const AVOption options[] = {{NULL}};
75 const AVClass ffurl_context_class = {
76 .class_name = "URLContext",
77 .item_name = urlcontext_to_name,
79 .version = LIBAVUTIL_VERSION_INT,
80 .child_next = urlcontext_child_next,
81 .child_class_next = urlcontext_child_class_next,
86 const char *avio_enum_protocols(void **opaque, int output)
89 *opaque = ffurl_protocol_next(*opaque);
90 if (!(p = *opaque)) return NULL;
91 if ((output && p->url_write) || (!output && p->url_read))
93 return avio_enum_protocols(opaque, output);
96 int ffurl_register_protocol(URLProtocol *protocol, int size)
99 if (size < sizeof(URLProtocol)) {
100 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
101 memcpy(temp, protocol, size);
105 while (*p != NULL) p = &(*p)->next;
107 protocol->next = NULL;
111 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
112 const char *filename, int flags,
113 const AVIOInterruptCB *int_cb)
119 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
122 if ((flags & AVIO_FLAG_READ) && !up->url_read) {
123 av_log(NULL, AV_LOG_ERROR,
124 "Impossible to open the '%s' protocol for reading\n", up->name);
127 if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
128 av_log(NULL, AV_LOG_ERROR,
129 "Impossible to open the '%s' protocol for writing\n", up->name);
132 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
134 err = AVERROR(ENOMEM);
137 uc->av_class = &ffurl_context_class;
138 uc->filename = (char *) &uc[1];
139 strcpy(uc->filename, filename);
142 uc->is_streamed = 0; /* default = not streamed */
143 uc->max_packet_size = 0; /* default: stream file */
144 if (up->priv_data_size) {
145 uc->priv_data = av_mallocz(up->priv_data_size);
146 if (!uc->priv_data) {
147 err = AVERROR(ENOMEM);
150 if (up->priv_data_class) {
151 int proto_len= strlen(up->name);
152 char *start = strchr(uc->filename, ',');
153 *(const AVClass**)uc->priv_data = up->priv_data_class;
154 av_opt_set_defaults(uc->priv_data);
155 if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
161 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
163 ret= av_opt_set(uc->priv_data, p, key+1, 0);
164 if (ret == AVERROR_OPTION_NOT_FOUND)
165 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
170 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
171 av_freep(&uc->priv_data);
173 err = AVERROR(EINVAL);
176 memmove(start, key+1, strlen(key));
181 uc->interrupt_callback = *int_cb;
188 av_freep(&uc->priv_data);
191 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
197 int ffurl_connect(URLContext* uc, AVDictionary **options)
200 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
201 uc->prot->url_open(uc, uc->filename, uc->flags);
204 uc->is_connected = 1;
205 //We must be careful here as ffurl_seek() could be slow, for example for http
206 if( (uc->flags & AVIO_FLAG_WRITE)
207 || !strcmp(uc->prot->name, "file"))
208 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
213 #define URL_SCHEME_CHARS \
214 "abcdefghijklmnopqrstuvwxyz" \
215 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
218 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
219 const AVIOInterruptCB *int_cb)
221 URLProtocol *up = NULL;
222 char proto_str[128], proto_nested[128], *ptr;
223 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
225 if (!first_protocol) {
226 av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
227 "Missing call to av_register_all()?\n");
230 if (filename[proto_len] != ':' &&
231 (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
232 is_dos_path(filename))
233 strcpy(proto_str, "file");
235 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
237 if ((ptr = strchr(proto_str, ',')))
239 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
240 if ((ptr = strchr(proto_nested, '+')))
243 while (up = ffurl_protocol_next(up)) {
244 if (!strcmp(proto_str, up->name))
245 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
246 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
247 !strcmp(proto_nested, up->name))
248 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
251 if (!strcmp("https", proto_str))
252 av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
253 return AVERROR_PROTOCOL_NOT_FOUND;
256 int ffurl_open(URLContext **puc, const char *filename, int flags,
257 const AVIOInterruptCB *int_cb, AVDictionary **options)
259 int ret = ffurl_alloc(puc, filename, flags, int_cb);
262 if (options && (*puc)->prot->priv_data_class &&
263 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
265 ret = ffurl_connect(*puc, options);
274 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
275 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
278 int fast_retries = 5;
279 int64_t wait_since = 0;
282 while (len < size_min) {
283 if (ff_check_interrupt(&h->interrupt_callback))
285 ret = transfer_func(h, buf+len, size-len);
286 if (ret == AVERROR(EINTR))
288 if (h->flags & AVIO_FLAG_NONBLOCK)
290 if (ret == AVERROR(EAGAIN)) {
297 wait_since = av_gettime();
298 else if (av_gettime() > wait_since + h->rw_timeout)
304 return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
306 fast_retries = FFMAX(fast_retries, 2);
312 int ffurl_read(URLContext *h, unsigned char *buf, int size)
314 if (!(h->flags & AVIO_FLAG_READ))
316 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
319 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
321 if (!(h->flags & AVIO_FLAG_READ))
323 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
326 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
328 if (!(h->flags & AVIO_FLAG_WRITE))
330 /* avoid sending too big packets */
331 if (h->max_packet_size && size > h->max_packet_size)
334 return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
337 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
341 if (!h->prot->url_seek)
342 return AVERROR(ENOSYS);
343 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
347 int ffurl_closep(URLContext **hh)
351 if (!h) return 0; /* can happen when ffurl_open fails */
353 if (h->is_connected && h->prot->url_close)
354 ret = h->prot->url_close(h);
356 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
359 if (h->prot->priv_data_size) {
360 if (h->prot->priv_data_class)
361 av_opt_free(h->priv_data);
362 av_freep(&h->priv_data);
368 int ffurl_close(URLContext *h)
370 return ffurl_closep(&h);
374 int avio_check(const char *url, int flags)
377 int ret = ffurl_alloc(&h, url, flags, NULL);
381 if (h->prot->url_check) {
382 ret = h->prot->url_check(h, flags);
384 ret = ffurl_connect(h, NULL);
393 int64_t ffurl_size(URLContext *h)
397 size= ffurl_seek(h, 0, AVSEEK_SIZE);
399 pos = ffurl_seek(h, 0, SEEK_CUR);
400 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
403 ffurl_seek(h, pos, SEEK_SET);
408 int ffurl_get_file_handle(URLContext *h)
410 if (!h->prot->url_get_file_handle)
412 return h->prot->url_get_file_handle(h);
415 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
417 if (!h->prot->url_get_multi_file_handle) {
418 if (!h->prot->url_get_file_handle)
419 return AVERROR(ENOSYS);
420 *handles = av_malloc(sizeof(**handles));
422 return AVERROR(ENOMEM);
424 *handles[0] = h->prot->url_get_file_handle(h);
427 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
430 int ffurl_shutdown(URLContext *h, int flags)
432 if (!h->prot->url_shutdown)
433 return AVERROR(EINVAL);
434 return h->prot->url_shutdown(h, flags);
437 int ff_check_interrupt(AVIOInterruptCB *cb)
440 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))