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 #if FF_API_OLD_INTERRUPT_CB
88 static int default_interrupt_cb(void);
89 int (*url_interrupt_cb)(void) = default_interrupt_cb;
92 URLProtocol *av_protocol_next(URLProtocol *p)
94 return ffurl_protocol_next(p);
97 const char *avio_enum_protocols(void **opaque, int output)
99 URLProtocol **p = opaque;
100 *p = ffurl_protocol_next(*p);
101 if (!*p) return NULL;
102 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
104 return avio_enum_protocols(opaque, output);
107 int ffurl_register_protocol(URLProtocol *protocol, int size)
110 if (size < sizeof(URLProtocol)) {
111 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
112 memcpy(temp, protocol, size);
116 while (*p != NULL) p = &(*p)->next;
118 protocol->next = NULL;
122 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
123 const char *filename, int flags,
124 const AVIOInterruptCB *int_cb)
130 if (!ff_network_init())
133 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
135 err = AVERROR(ENOMEM);
138 uc->av_class = &ffurl_context_class;
139 uc->filename = (char *) &uc[1];
140 strcpy(uc->filename, filename);
143 uc->is_streamed = 0; /* default = not streamed */
144 uc->max_packet_size = 0; /* default: stream file */
145 if (up->priv_data_size) {
146 uc->priv_data = av_mallocz(up->priv_data_size);
147 if (up->priv_data_class) {
148 char *start = strchr(uc->filename, ',');
149 *(const AVClass**)uc->priv_data = up->priv_data_class;
150 av_opt_set_defaults(uc->priv_data);
157 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
159 ret= av_opt_set(uc->priv_data, p, key+1, 0);
160 if (ret == AVERROR_OPTION_NOT_FOUND)
161 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
166 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
167 av_freep(&uc->priv_data);
171 memmove(start, key+1, strlen(key));
176 uc->interrupt_callback = *int_cb;
188 int ffurl_connect(URLContext* uc, AVDictionary **options)
192 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
194 uc->prot->url_open(uc, uc->filename, uc->flags);
197 uc->is_connected = 1;
198 //We must be careful here as ffurl_seek() could be slow, for example for http
199 if( (uc->flags & AVIO_FLAG_WRITE)
200 || !strcmp(uc->prot->name, "file"))
201 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
207 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
208 const char *filename, int flags)
212 ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
215 ret = ffurl_connect(*puc, NULL);
223 int url_alloc(URLContext **puc, const char *filename, int flags)
225 return ffurl_alloc(puc, filename, flags, NULL);
227 int url_connect(URLContext* uc)
229 return ffurl_connect(uc, NULL);
231 int url_open(URLContext **puc, const char *filename, int flags)
233 return ffurl_open(puc, filename, flags, NULL, NULL);
235 int url_read(URLContext *h, unsigned char *buf, int size)
237 return ffurl_read(h, buf, size);
239 int url_read_complete(URLContext *h, unsigned char *buf, int size)
241 return ffurl_read_complete(h, buf, size);
243 int url_write(URLContext *h, const unsigned char *buf, int size)
245 return ffurl_write(h, buf, size);
247 int64_t url_seek(URLContext *h, int64_t pos, int whence)
249 return ffurl_seek(h, pos, whence);
251 int url_close(URLContext *h)
253 return ffurl_close(h);
255 int64_t url_filesize(URLContext *h)
257 return ffurl_size(h);
259 int url_get_file_handle(URLContext *h)
261 return ffurl_get_file_handle(h);
263 int url_get_max_packet_size(URLContext *h)
265 return h->max_packet_size;
267 void url_get_filename(URLContext *h, char *buf, int buf_size)
269 av_strlcpy(buf, h->filename, buf_size);
271 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
273 avio_set_interrupt_cb(interrupt_cb);
275 int av_register_protocol2(URLProtocol *protocol, int size)
277 return ffurl_register_protocol(protocol, size);
281 #define URL_SCHEME_CHARS \
282 "abcdefghijklmnopqrstuvwxyz" \
283 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
286 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
287 const AVIOInterruptCB *int_cb)
289 URLProtocol *up = NULL;
290 char proto_str[128], proto_nested[128], *ptr;
291 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
293 if (!first_protocol) {
294 av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
295 "Missing call to av_register_all()?\n");
298 if (filename[proto_len] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
299 strcpy(proto_str, "file");
301 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
303 if ((ptr = strchr(proto_str, ',')))
305 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
306 if ((ptr = strchr(proto_nested, '+')))
309 while (up = ffurl_protocol_next(up)) {
310 if (!strcmp(proto_str, up->name))
311 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
312 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
313 !strcmp(proto_nested, up->name))
314 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
317 return AVERROR(ENOENT);
320 int ffurl_open(URLContext **puc, const char *filename, int flags,
321 const AVIOInterruptCB *int_cb, AVDictionary **options)
323 int ret = ffurl_alloc(puc, filename, flags, int_cb);
326 if (options && (*puc)->prot->priv_data_class &&
327 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
329 ret = ffurl_connect(*puc, options);
338 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
339 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
342 int fast_retries = 5;
345 while (len < size_min) {
346 ret = transfer_func(h, buf+len, size-len);
347 if (ret == AVERROR(EINTR))
349 if (h->flags & AVIO_FLAG_NONBLOCK)
351 if (ret == AVERROR(EAGAIN)) {
358 return ret < 0 ? ret : len;
360 fast_retries = FFMAX(fast_retries, 2);
362 if (len < size && ff_check_interrupt(&h->interrupt_callback))
368 int ffurl_read(URLContext *h, unsigned char *buf, int size)
370 if (!(h->flags & AVIO_FLAG_READ))
372 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
375 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
377 if (!(h->flags & AVIO_FLAG_READ))
379 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
382 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
384 if (!(h->flags & AVIO_FLAG_WRITE))
386 /* avoid sending too big packets */
387 if (h->max_packet_size && size > h->max_packet_size)
390 return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
393 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
397 if (!h->prot->url_seek)
398 return AVERROR(ENOSYS);
399 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
403 int ffurl_close(URLContext *h)
406 if (!h) return 0; /* can happen when ffurl_open fails */
408 if (h->is_connected && h->prot->url_close)
409 ret = h->prot->url_close(h);
413 if (h->prot->priv_data_size) {
414 if (h->prot->priv_data_class)
415 av_opt_free(h->priv_data);
416 av_free(h->priv_data);
423 int url_exist(const char *filename)
426 if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
433 int avio_check(const char *url, int flags)
436 int ret = ffurl_alloc(&h, url, flags, NULL);
440 if (h->prot->url_check) {
441 ret = h->prot->url_check(h, flags);
443 ret = ffurl_connect(h, NULL);
452 int64_t ffurl_size(URLContext *h)
456 size= ffurl_seek(h, 0, AVSEEK_SIZE);
458 pos = ffurl_seek(h, 0, SEEK_CUR);
459 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
462 ffurl_seek(h, pos, SEEK_SET);
467 int ffurl_get_file_handle(URLContext *h)
469 if (!h->prot->url_get_file_handle)
471 return h->prot->url_get_file_handle(h);
474 #if FF_API_OLD_INTERRUPT_CB
475 static int default_interrupt_cb(void)
480 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
483 interrupt_cb = default_interrupt_cb;
484 url_interrupt_cb = interrupt_cb;
488 int ff_check_interrupt(AVIOInterruptCB *cb)
491 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
493 #if FF_API_OLD_INTERRUPT_CB
494 return url_interrupt_cb();
501 int av_url_read_pause(URLContext *h, int pause)
503 if (!h->prot->url_read_pause)
504 return AVERROR(ENOSYS);
505 return h->prot->url_read_pause(h, pause);
508 int64_t av_url_read_seek(URLContext *h,
509 int stream_index, int64_t timestamp, int flags)
511 if (!h->prot->url_read_seek)
512 return AVERROR(ENOSYS);
513 return h->prot->url_read_seek(h, stream_index, timestamp, flags);