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
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/opt.h"
27 #include "os_support.h"
34 static URLProtocol *first_protocol = NULL;
36 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
38 return prev ? prev->next : first_protocol;
41 /** @name Logging context. */
43 static const char *urlcontext_to_name(void *ptr)
45 URLContext *h = (URLContext *)ptr;
46 if(h->prot) return h->prot->name;
50 static void *urlcontext_child_next(void *obj, void *prev)
53 if (!prev && h->priv_data && h->prot->priv_data_class)
58 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
60 URLProtocol *p = NULL;
62 /* find the protocol that corresponds to prev */
63 while (prev && (p = ffurl_protocol_next(p)))
64 if (p->priv_data_class == prev)
67 /* find next protocol with priv options */
68 while (p = ffurl_protocol_next(p))
69 if (p->priv_data_class)
70 return p->priv_data_class;
75 static const AVOption options[] = {{NULL}};
76 const AVClass ffurl_context_class = {
77 .class_name = "URLContext",
78 .item_name = urlcontext_to_name,
80 .version = LIBAVUTIL_VERSION_INT,
81 .child_next = urlcontext_child_next,
82 .child_class_next = urlcontext_child_class_next,
87 const char *avio_enum_protocols(void **opaque, int output)
89 URLProtocol **p = (URLProtocol **)opaque;
90 *p = ffurl_protocol_next(*p);
92 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
94 return avio_enum_protocols(opaque, output);
97 int ffurl_register_protocol(URLProtocol *protocol, int size)
100 if (size < sizeof(URLProtocol)) {
101 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
102 memcpy(temp, protocol, size);
106 while (*p != NULL) p = &(*p)->next;
108 protocol->next = NULL;
112 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
113 const char *filename, int flags,
114 const AVIOInterruptCB *int_cb)
120 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
123 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
125 err = AVERROR(ENOMEM);
128 uc->av_class = &ffurl_context_class;
129 uc->filename = (char *) &uc[1];
130 strcpy(uc->filename, filename);
133 uc->is_streamed = 0; /* default = not streamed */
134 uc->max_packet_size = 0; /* default: stream file */
135 if (up->priv_data_size) {
136 uc->priv_data = av_mallocz(up->priv_data_size);
137 if (up->priv_data_class) {
138 int proto_len= strlen(up->name);
139 char *start = strchr(uc->filename, ',');
140 *(const AVClass**)uc->priv_data = up->priv_data_class;
141 av_opt_set_defaults(uc->priv_data);
142 if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
148 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
150 ret= av_opt_set(uc->priv_data, p, key+1, 0);
151 if (ret == AVERROR_OPTION_NOT_FOUND)
152 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
157 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
158 av_freep(&uc->priv_data);
162 memmove(start, key+1, strlen(key));
167 uc->interrupt_callback = *int_cb;
174 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
180 int ffurl_connect(URLContext* uc, AVDictionary **options)
183 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
184 uc->prot->url_open(uc, uc->filename, uc->flags);
187 uc->is_connected = 1;
188 //We must be careful here as ffurl_seek() could be slow, for example for http
189 if( (uc->flags & AVIO_FLAG_WRITE)
190 || !strcmp(uc->prot->name, "file"))
191 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
196 #define URL_SCHEME_CHARS \
197 "abcdefghijklmnopqrstuvwxyz" \
198 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
201 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
202 const AVIOInterruptCB *int_cb)
204 URLProtocol *up = NULL;
205 char proto_str[128], proto_nested[128], *ptr;
206 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
208 if (!first_protocol) {
209 av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
210 "Missing call to av_register_all()?\n");
213 if (filename[proto_len] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
214 strcpy(proto_str, "file");
216 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
218 if ((ptr = strchr(proto_str, ',')))
220 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
221 if ((ptr = strchr(proto_nested, '+')))
224 while (up = ffurl_protocol_next(up)) {
225 if (!strcmp(proto_str, up->name))
226 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
227 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
228 !strcmp(proto_nested, up->name))
229 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
232 return AVERROR(ENOENT);
235 int ffurl_open(URLContext **puc, const char *filename, int flags,
236 const AVIOInterruptCB *int_cb, AVDictionary **options)
238 int ret = ffurl_alloc(puc, filename, flags, int_cb);
241 if (options && (*puc)->prot->priv_data_class &&
242 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
244 ret = ffurl_connect(*puc, options);
253 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
254 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
257 int fast_retries = 5;
260 while (len < size_min) {
261 ret = transfer_func(h, buf+len, size-len);
262 if (ret == AVERROR(EINTR))
264 if (h->flags & AVIO_FLAG_NONBLOCK)
266 if (ret == AVERROR(EAGAIN)) {
273 return ret < 0 ? ret : len;
275 fast_retries = FFMAX(fast_retries, 2);
277 if (len < size && ff_check_interrupt(&h->interrupt_callback))
283 int ffurl_read(URLContext *h, unsigned char *buf, int size)
285 if (!(h->flags & AVIO_FLAG_READ))
287 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
290 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
292 if (!(h->flags & AVIO_FLAG_READ))
294 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
297 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
299 if (!(h->flags & AVIO_FLAG_WRITE))
301 /* avoid sending too big packets */
302 if (h->max_packet_size && size > h->max_packet_size)
305 return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
308 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
312 if (!h->prot->url_seek)
313 return AVERROR(ENOSYS);
314 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
318 int ffurl_close(URLContext *h)
321 if (!h) return 0; /* can happen when ffurl_open fails */
323 if (h->is_connected && h->prot->url_close)
324 ret = h->prot->url_close(h);
326 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
329 if (h->prot->priv_data_size) {
330 if (h->prot->priv_data_class)
331 av_opt_free(h->priv_data);
332 av_free(h->priv_data);
338 int avio_check(const char *url, int flags)
341 int ret = ffurl_alloc(&h, url, flags, NULL);
345 if (h->prot->url_check) {
346 ret = h->prot->url_check(h, flags);
348 ret = ffurl_connect(h, NULL);
357 int64_t ffurl_size(URLContext *h)
361 size= ffurl_seek(h, 0, AVSEEK_SIZE);
363 pos = ffurl_seek(h, 0, SEEK_CUR);
364 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
367 ffurl_seek(h, pos, SEEK_SET);
372 int ffurl_get_file_handle(URLContext *h)
374 if (!h->prot->url_get_file_handle)
376 return h->prot->url_get_file_handle(h);
379 int ff_check_interrupt(AVIOInterruptCB *cb)
382 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))