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
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;
93 URLProtocol *av_protocol_next(URLProtocol *p)
95 return ffurl_protocol_next(p);
99 const char *avio_enum_protocols(void **opaque, int output)
101 URLProtocol **p = opaque;
102 *p = ffurl_protocol_next(*p);
103 if (!*p) return NULL;
104 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
106 return avio_enum_protocols(opaque, output);
109 int ffurl_register_protocol(URLProtocol *protocol, int size)
112 if (size < sizeof(URLProtocol)) {
113 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
114 memcpy(temp, protocol, size);
118 while (*p != NULL) p = &(*p)->next;
120 protocol->next = NULL;
124 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
125 const char *filename, int flags,
126 const AVIOInterruptCB *int_cb)
132 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
135 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
137 err = AVERROR(ENOMEM);
140 uc->av_class = &ffurl_context_class;
141 uc->filename = (char *) &uc[1];
142 strcpy(uc->filename, filename);
145 uc->is_streamed = 0; /* default = not streamed */
146 uc->max_packet_size = 0; /* default: stream file */
147 if (up->priv_data_size) {
148 uc->priv_data = av_mallocz(up->priv_data_size);
149 if (up->priv_data_class) {
150 *(const AVClass**)uc->priv_data = up->priv_data_class;
151 av_opt_set_defaults(uc->priv_data);
155 uc->interrupt_callback = *int_cb;
162 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
168 int ffurl_connect(URLContext* uc, AVDictionary **options)
172 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
174 uc->prot->url_open(uc, uc->filename, uc->flags);
177 uc->is_connected = 1;
178 //We must be careful here as ffurl_seek() could be slow, for example for http
179 if( (uc->flags & AVIO_FLAG_WRITE)
180 || !strcmp(uc->prot->name, "file"))
181 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
187 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
188 const char *filename, int flags)
192 ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
195 ret = ffurl_connect(*puc, NULL);
203 int url_alloc(URLContext **puc, const char *filename, int flags)
205 return ffurl_alloc(puc, filename, flags, NULL);
207 int url_connect(URLContext* uc)
209 return ffurl_connect(uc, NULL);
211 int url_open(URLContext **puc, const char *filename, int flags)
213 return ffurl_open(puc, filename, flags, NULL, NULL);
215 int url_read(URLContext *h, unsigned char *buf, int size)
217 return ffurl_read(h, buf, size);
219 int url_read_complete(URLContext *h, unsigned char *buf, int size)
221 return ffurl_read_complete(h, buf, size);
223 int url_write(URLContext *h, const unsigned char *buf, int size)
225 return ffurl_write(h, buf, size);
227 int64_t url_seek(URLContext *h, int64_t pos, int whence)
229 return ffurl_seek(h, pos, whence);
231 int url_close(URLContext *h)
233 return ffurl_close(h);
235 int64_t url_filesize(URLContext *h)
237 return ffurl_size(h);
239 int url_get_file_handle(URLContext *h)
241 return ffurl_get_file_handle(h);
243 int url_get_max_packet_size(URLContext *h)
245 return h->max_packet_size;
247 void url_get_filename(URLContext *h, char *buf, int buf_size)
249 av_strlcpy(buf, h->filename, buf_size);
251 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
253 avio_set_interrupt_cb(interrupt_cb);
255 int av_register_protocol2(URLProtocol *protocol, int size)
257 return ffurl_register_protocol(protocol, size);
261 #define URL_SCHEME_CHARS \
262 "abcdefghijklmnopqrstuvwxyz" \
263 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
266 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
267 const AVIOInterruptCB *int_cb)
269 URLProtocol *up = NULL;
270 char proto_str[128], proto_nested[128], *ptr;
271 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
273 if (filename[proto_len] != ':' || is_dos_path(filename))
274 strcpy(proto_str, "file");
276 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
278 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
279 if ((ptr = strchr(proto_nested, '+')))
282 while (up = ffurl_protocol_next(up)) {
283 if (!strcmp(proto_str, up->name))
284 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
285 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
286 !strcmp(proto_nested, up->name))
287 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
290 return AVERROR(ENOENT);
293 int ffurl_open(URLContext **puc, const char *filename, int flags,
294 const AVIOInterruptCB *int_cb, AVDictionary **options)
296 int ret = ffurl_alloc(puc, filename, flags, int_cb);
299 if (options && (*puc)->prot->priv_data_class &&
300 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
302 ret = ffurl_connect(*puc, options);
311 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
312 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
315 int fast_retries = 5;
318 while (len < size_min) {
319 ret = transfer_func(h, buf+len, size-len);
320 if (ret == AVERROR(EINTR))
322 if (h->flags & AVIO_FLAG_NONBLOCK)
324 if (ret == AVERROR(EAGAIN)) {
331 return ret < 0 ? ret : len;
333 fast_retries = FFMAX(fast_retries, 2);
335 if (ff_check_interrupt(&h->interrupt_callback))
341 int ffurl_read(URLContext *h, unsigned char *buf, int size)
343 if (!(h->flags & AVIO_FLAG_READ))
345 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
348 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
350 if (!(h->flags & AVIO_FLAG_READ))
352 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
355 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
357 if (!(h->flags & AVIO_FLAG_WRITE))
359 /* avoid sending too big packets */
360 if (h->max_packet_size && size > h->max_packet_size)
363 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
366 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
370 if (!h->prot->url_seek)
371 return AVERROR(ENOSYS);
372 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
376 int ffurl_close(URLContext *h)
379 if (!h) return 0; /* can happen when ffurl_open fails */
381 if (h->is_connected && h->prot->url_close)
382 ret = h->prot->url_close(h);
384 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
387 if (h->prot->priv_data_size) {
388 if (h->prot->priv_data_class)
389 av_opt_free(h->priv_data);
390 av_free(h->priv_data);
397 int url_exist(const char *filename)
400 if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
407 int avio_check(const char *url, int flags)
410 int ret = ffurl_alloc(&h, url, flags, NULL);
414 if (h->prot->url_check) {
415 ret = h->prot->url_check(h, flags);
417 ret = ffurl_connect(h, NULL);
426 int64_t ffurl_size(URLContext *h)
430 size= ffurl_seek(h, 0, AVSEEK_SIZE);
432 pos = ffurl_seek(h, 0, SEEK_CUR);
433 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
436 ffurl_seek(h, pos, SEEK_SET);
441 int ffurl_get_file_handle(URLContext *h)
443 if (!h->prot->url_get_file_handle)
445 return h->prot->url_get_file_handle(h);
448 #if FF_API_OLD_INTERRUPT_CB
449 static int default_interrupt_cb(void)
454 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
457 interrupt_cb = default_interrupt_cb;
458 url_interrupt_cb = interrupt_cb;
462 int ff_check_interrupt(AVIOInterruptCB *cb)
465 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
467 #if FF_API_OLD_INTERRUPT_CB
468 return url_interrupt_cb();
475 int av_url_read_pause(URLContext *h, int pause)
477 if (!h->prot->url_read_pause)
478 return AVERROR(ENOSYS);
479 return h->prot->url_read_pause(h, pause);
482 int64_t av_url_read_seek(URLContext *h,
483 int stream_index, int64_t timestamp, int flags)
485 if (!h->prot->url_read_seek)
486 return AVERROR(ENOSYS);
487 return h->prot->url_read_seek(h, stream_index, timestamp, flags);