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/opt.h"
26 #include "os_support.h"
33 /** @name Logging context. */
35 static const char *urlcontext_to_name(void *ptr)
37 URLContext *h = (URLContext *)ptr;
38 if(h->prot) return h->prot->name;
41 static const AVOption options[] = {{NULL}};
42 static const AVClass urlcontext_class = {
43 .class_name = "URLContext",
44 .item_name = urlcontext_to_name,
46 .version = LIBAVUTIL_VERSION_INT,
50 static int default_interrupt_cb(void);
52 URLProtocol *first_protocol = NULL;
53 int (*url_interrupt_cb)(void) = default_interrupt_cb;
55 URLProtocol *av_protocol_next(URLProtocol *p)
58 else return first_protocol;
61 const char *avio_enum_protocols(void **opaque, int output)
63 URLProtocol *p = *opaque;
64 p = p ? p->next : first_protocol;
66 if ((output && p->url_write) || (!output && p->url_read))
68 return avio_enum_protocols(opaque, output);
71 int ffurl_register_protocol(URLProtocol *protocol, int size)
74 if (size < sizeof(URLProtocol)) {
75 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
76 memcpy(temp, protocol, size);
80 while (*p != NULL) p = &(*p)->next;
82 protocol->next = NULL;
86 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
87 const char *filename, int flags)
93 if (!ff_network_init())
96 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
98 err = AVERROR(ENOMEM);
101 uc->av_class = &urlcontext_class;
102 uc->filename = (char *) &uc[1];
103 strcpy(uc->filename, filename);
106 uc->is_streamed = 0; /* default = not streamed */
107 uc->max_packet_size = 0; /* default: stream file */
108 if (up->priv_data_size) {
109 uc->priv_data = av_mallocz(up->priv_data_size);
110 if (up->priv_data_class) {
111 *(const AVClass**)uc->priv_data = up->priv_data_class;
112 av_opt_set_defaults(uc->priv_data);
126 int ffurl_connect(URLContext* uc)
128 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
131 uc->is_connected = 1;
132 //We must be careful here as ffurl_seek() could be slow, for example for http
133 if( (uc->flags & AVIO_FLAG_WRITE)
134 || !strcmp(uc->prot->name, "file"))
135 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
141 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
142 const char *filename, int flags)
146 ret = url_alloc_for_protocol(puc, up, filename, flags);
149 ret = ffurl_connect(*puc);
157 int url_alloc(URLContext **puc, const char *filename, int flags)
159 return ffurl_alloc(puc, filename, flags);
161 int url_connect(URLContext* uc)
163 return ffurl_connect(uc);
165 int url_open(URLContext **puc, const char *filename, int flags)
167 return ffurl_open(puc, filename, flags);
169 int url_read(URLContext *h, unsigned char *buf, int size)
171 return ffurl_read(h, buf, size);
173 int url_read_complete(URLContext *h, unsigned char *buf, int size)
175 return ffurl_read_complete(h, buf, size);
177 int url_write(URLContext *h, const unsigned char *buf, int size)
179 return ffurl_write(h, buf, size);
181 int64_t url_seek(URLContext *h, int64_t pos, int whence)
183 return ffurl_seek(h, pos, whence);
185 int url_close(URLContext *h)
187 return ffurl_close(h);
189 int64_t url_filesize(URLContext *h)
191 return ffurl_size(h);
193 int url_get_file_handle(URLContext *h)
195 return ffurl_get_file_handle(h);
197 int url_get_max_packet_size(URLContext *h)
199 return h->max_packet_size;
201 void url_get_filename(URLContext *h, char *buf, int buf_size)
203 av_strlcpy(buf, h->filename, buf_size);
205 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
207 avio_set_interrupt_cb(interrupt_cb);
209 int av_register_protocol2(URLProtocol *protocol, int size)
211 return ffurl_register_protocol(protocol, size);
215 #define URL_SCHEME_CHARS \
216 "abcdefghijklmnopqrstuvwxyz" \
217 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
220 int ffurl_alloc(URLContext **puc, const char *filename, int flags)
223 char proto_str[128], proto_nested[128], *ptr;
224 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
226 if (!first_protocol) {
227 av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
228 "Missing call to av_register_all()?\n");
231 if (filename[proto_len] != ':' || is_dos_path(filename))
232 strcpy(proto_str, "file");
234 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
236 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
237 if ((ptr = strchr(proto_nested, '+')))
242 if (!strcmp(proto_str, up->name))
243 return url_alloc_for_protocol (puc, up, filename, flags);
244 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
245 !strcmp(proto_nested, up->name))
246 return url_alloc_for_protocol (puc, up, filename, flags);
250 return AVERROR(ENOENT);
253 int ffurl_open(URLContext **puc, const char *filename, int flags)
255 int ret = ffurl_alloc(puc, filename, flags);
258 ret = ffurl_connect(*puc);
266 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
267 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
270 int fast_retries = 5;
273 while (len < size_min) {
274 ret = transfer_func(h, buf+len, size-len);
275 if (ret == AVERROR(EINTR))
277 if (h->flags & AVIO_FLAG_NONBLOCK)
279 if (ret == AVERROR(EAGAIN)) {
286 return ret < 0 ? ret : len;
288 fast_retries = FFMAX(fast_retries, 2);
290 if (len < size && url_interrupt_cb())
296 int ffurl_read(URLContext *h, unsigned char *buf, int size)
298 if (!(h->flags & AVIO_FLAG_READ))
300 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
303 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
305 if (!(h->flags & AVIO_FLAG_READ))
307 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
310 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
312 if (!(h->flags & AVIO_FLAG_WRITE))
314 /* avoid sending too big packets */
315 if (h->max_packet_size && size > h->max_packet_size)
318 return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
321 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
325 if (!h->prot->url_seek)
326 return AVERROR(ENOSYS);
327 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
331 int ffurl_close(URLContext *h)
334 if (!h) return 0; /* can happen when ffurl_open fails */
336 if (h->is_connected && h->prot->url_close)
337 ret = h->prot->url_close(h);
341 if (h->prot->priv_data_size)
342 av_free(h->priv_data);
348 int url_exist(const char *filename)
351 if (ffurl_open(&h, filename, AVIO_FLAG_READ) < 0)
358 int avio_check(const char *url, int flags)
361 int ret = ffurl_alloc(&h, url, flags);
365 if (h->prot->url_check) {
366 ret = h->prot->url_check(h, flags);
368 ret = ffurl_connect(h);
377 int64_t ffurl_size(URLContext *h)
381 size= ffurl_seek(h, 0, AVSEEK_SIZE);
383 pos = ffurl_seek(h, 0, SEEK_CUR);
384 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
387 ffurl_seek(h, pos, SEEK_SET);
392 int ffurl_get_file_handle(URLContext *h)
394 if (!h->prot->url_get_file_handle)
396 return h->prot->url_get_file_handle(h);
399 static int default_interrupt_cb(void)
404 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
407 interrupt_cb = default_interrupt_cb;
408 url_interrupt_cb = interrupt_cb;
412 int av_url_read_pause(URLContext *h, int pause)
414 if (!h->prot->url_read_pause)
415 return AVERROR(ENOSYS);
416 return h->prot->url_read_pause(h, pause);
419 int64_t av_url_read_seek(URLContext *h,
420 int stream_index, int64_t timestamp, int flags)
422 if (!h->prot->url_read_seek)
423 return AVERROR(ENOSYS);
424 return h->prot->url_read_seek(h, stream_index, timestamp, flags);