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 *(const AVClass**)uc->priv_data = up->priv_data_class;
149 av_opt_set_defaults(uc->priv_data);
153 uc->interrupt_callback = *int_cb;
165 int ffurl_connect(URLContext* uc, AVDictionary **options)
169 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
171 uc->prot->url_open(uc, uc->filename, uc->flags);
174 uc->is_connected = 1;
175 //We must be careful here as ffurl_seek() could be slow, for example for http
176 if( (uc->flags & AVIO_FLAG_WRITE)
177 || !strcmp(uc->prot->name, "file"))
178 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
184 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
185 const char *filename, int flags)
189 ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
192 ret = ffurl_connect(*puc, NULL);
200 int url_alloc(URLContext **puc, const char *filename, int flags)
202 return ffurl_alloc(puc, filename, flags, NULL);
204 int url_connect(URLContext* uc)
206 return ffurl_connect(uc, NULL);
208 int url_open(URLContext **puc, const char *filename, int flags)
210 return ffurl_open(puc, filename, flags, NULL, NULL);
212 int url_read(URLContext *h, unsigned char *buf, int size)
214 return ffurl_read(h, buf, size);
216 int url_read_complete(URLContext *h, unsigned char *buf, int size)
218 return ffurl_read_complete(h, buf, size);
220 int url_write(URLContext *h, const unsigned char *buf, int size)
222 return ffurl_write(h, buf, size);
224 int64_t url_seek(URLContext *h, int64_t pos, int whence)
226 return ffurl_seek(h, pos, whence);
228 int url_close(URLContext *h)
230 return ffurl_close(h);
232 int64_t url_filesize(URLContext *h)
234 return ffurl_size(h);
236 int url_get_file_handle(URLContext *h)
238 return ffurl_get_file_handle(h);
240 int url_get_max_packet_size(URLContext *h)
242 return h->max_packet_size;
244 void url_get_filename(URLContext *h, char *buf, int buf_size)
246 av_strlcpy(buf, h->filename, buf_size);
248 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
250 avio_set_interrupt_cb(interrupt_cb);
252 int av_register_protocol2(URLProtocol *protocol, int size)
254 return ffurl_register_protocol(protocol, size);
258 #define URL_SCHEME_CHARS \
259 "abcdefghijklmnopqrstuvwxyz" \
260 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
263 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
264 const AVIOInterruptCB *int_cb)
266 URLProtocol *up = NULL;
267 char proto_str[128], proto_nested[128], *ptr;
268 const char *proto_end = strchr(filename, ':');
270 if (!first_protocol) {
271 av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
272 "Missing call to av_register_all()?\n");
275 if (!proto_end || is_dos_path(filename))
276 strcpy(proto_str, "file");
278 av_strlcpy(proto_str, filename, FFMIN(proto_end-filename+1, sizeof(proto_str)));
280 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
281 if ((ptr = strchr(proto_nested, '+')))
284 while (up = ffurl_protocol_next(up)) {
285 if (!strcmp(proto_str, up->name))
286 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
287 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
288 !strcmp(proto_nested, up->name))
289 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
292 return AVERROR(ENOENT);
295 int ffurl_open(URLContext **puc, const char *filename, int flags,
296 const AVIOInterruptCB *int_cb, AVDictionary **options)
298 int ret = ffurl_alloc(puc, filename, flags, int_cb);
301 if (options && (*puc)->prot->priv_data_class &&
302 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
304 ret = ffurl_connect(*puc, options);
313 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
314 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
317 int fast_retries = 5;
320 while (len < size_min) {
321 ret = transfer_func(h, buf+len, size-len);
322 if (ret == AVERROR(EINTR))
324 if (h->flags & AVIO_FLAG_NONBLOCK)
326 if (ret == AVERROR(EAGAIN)) {
333 return ret < 0 ? ret : len;
335 fast_retries = FFMAX(fast_retries, 2);
337 if (len < size && ff_check_interrupt(&h->interrupt_callback))
343 int ffurl_read(URLContext *h, unsigned char *buf, int size)
345 if (!(h->flags & AVIO_FLAG_READ))
347 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
350 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
352 if (!(h->flags & AVIO_FLAG_READ))
354 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
357 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
359 if (!(h->flags & AVIO_FLAG_WRITE))
361 /* avoid sending too big packets */
362 if (h->max_packet_size && size > h->max_packet_size)
365 return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
368 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
372 if (!h->prot->url_seek)
373 return AVERROR(ENOSYS);
374 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
378 int ffurl_close(URLContext *h)
381 if (!h) return 0; /* can happen when ffurl_open fails */
383 if (h->is_connected && h->prot->url_close)
384 ret = h->prot->url_close(h);
388 if (h->prot->priv_data_size) {
389 if (h->prot->priv_data_class)
390 av_opt_free(h->priv_data);
391 av_free(h->priv_data);
398 int url_exist(const char *filename)
401 if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
408 int avio_check(const char *url, int flags)
411 int ret = ffurl_alloc(&h, url, flags, NULL);
415 if (h->prot->url_check) {
416 ret = h->prot->url_check(h, flags);
418 ret = ffurl_connect(h, NULL);
427 int64_t ffurl_size(URLContext *h)
431 size= ffurl_seek(h, 0, AVSEEK_SIZE);
433 pos = ffurl_seek(h, 0, SEEK_CUR);
434 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
437 ffurl_seek(h, pos, SEEK_SET);
442 int ffurl_get_file_handle(URLContext *h)
444 if (!h->prot->url_get_file_handle)
446 return h->prot->url_get_file_handle(h);
449 #if FF_API_OLD_INTERRUPT_CB
450 static int default_interrupt_cb(void)
455 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
458 interrupt_cb = default_interrupt_cb;
459 url_interrupt_cb = interrupt_cb;
463 int ff_check_interrupt(AVIOInterruptCB *cb)
466 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
468 #if FF_API_OLD_INTERRUPT_CB
469 return url_interrupt_cb();
476 int av_url_read_pause(URLContext *h, int pause)
478 if (!h->prot->url_read_pause)
479 return AVERROR(ENOSYS);
480 return h->prot->url_read_pause(h, pause);
483 int64_t av_url_read_seek(URLContext *h,
484 int stream_index, int64_t timestamp, int flags)
486 if (!h->prot->url_read_seek)
487 return AVERROR(ENOSYS);
488 return h->prot->url_read_seek(h, stream_index, timestamp, flags);