2 * Unbuffered io for ffmpeg system
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
22 /* needed for usleep() */
23 #define _XOPEN_SOURCE 600
25 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 #include "os_support.h"
34 /** @name Logging context. */
36 static const char *urlcontext_to_name(void *ptr)
38 URLContext *h = (URLContext *)ptr;
39 if(h->prot) return h->prot->name;
42 static const AVOption options[] = {{NULL}};
43 static const AVClass urlcontext_class =
44 { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
47 static int default_interrupt_cb(void);
49 URLProtocol *first_protocol = NULL;
50 int (*url_interrupt_cb)(void) = default_interrupt_cb;
53 URLProtocol *av_protocol_next(URLProtocol *p)
56 else return first_protocol;
60 const char *avio_enum_protocols(void **opaque, int output)
62 URLProtocol **p = opaque;
63 *p = *p ? (*p)->next : first_protocol;
65 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
67 return avio_enum_protocols(opaque, output);
70 int ffurl_register_protocol(URLProtocol *protocol, int size)
73 if (size < sizeof(URLProtocol)) {
74 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
75 memcpy(temp, protocol, size);
79 while (*p != NULL) p = &(*p)->next;
81 protocol->next = NULL;
85 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
86 const char *filename, int flags)
92 if (!ff_network_init())
95 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
97 err = AVERROR(ENOMEM);
100 uc->av_class = &urlcontext_class;
101 uc->filename = (char *) &uc[1];
102 strcpy(uc->filename, filename);
105 uc->is_streamed = 0; /* default = not streamed */
106 uc->max_packet_size = 0; /* default: stream file */
107 if (up->priv_data_size) {
108 uc->priv_data = av_mallocz(up->priv_data_size);
109 if (up->priv_data_class) {
110 *(const AVClass**)uc->priv_data = up->priv_data_class;
111 av_opt_set_defaults(uc->priv_data);
125 int ffurl_connect(URLContext* uc)
127 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
130 uc->is_connected = 1;
131 //We must be careful here as ffurl_seek() could be slow, for example for http
132 if( (uc->flags & AVIO_FLAG_WRITE)
133 || !strcmp(uc->prot->name, "file"))
134 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
140 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
141 const char *filename, int flags)
145 ret = url_alloc_for_protocol(puc, up, filename, flags);
148 ret = ffurl_connect(*puc);
156 int url_alloc(URLContext **puc, const char *filename, int flags)
158 return ffurl_alloc(puc, filename, flags);
160 int url_connect(URLContext* uc)
162 return ffurl_connect(uc);
164 int url_open(URLContext **puc, const char *filename, int flags)
166 return ffurl_open(puc, filename, flags);
168 int url_read(URLContext *h, unsigned char *buf, int size)
170 return ffurl_read(h, buf, size);
172 int url_read_complete(URLContext *h, unsigned char *buf, int size)
174 return ffurl_read_complete(h, buf, size);
176 int url_write(URLContext *h, const unsigned char *buf, int size)
178 return ffurl_write(h, buf, size);
180 int64_t url_seek(URLContext *h, int64_t pos, int whence)
182 return ffurl_seek(h, pos, whence);
184 int url_close(URLContext *h)
186 return ffurl_close(h);
188 int64_t url_filesize(URLContext *h)
190 return ffurl_size(h);
192 int url_get_file_handle(URLContext *h)
194 return ffurl_get_file_handle(h);
196 int url_get_max_packet_size(URLContext *h)
198 return h->max_packet_size;
200 void url_get_filename(URLContext *h, char *buf, int buf_size)
202 av_strlcpy(buf, h->filename, buf_size);
204 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
206 avio_set_interrupt_cb(interrupt_cb);
208 int av_register_protocol2(URLProtocol *protocol, int size)
210 return ffurl_register_protocol(protocol, size);
214 #define URL_SCHEME_CHARS \
215 "abcdefghijklmnopqrstuvwxyz" \
216 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
219 int ffurl_alloc(URLContext **puc, const char *filename, int flags)
222 char proto_str[128], proto_nested[128], *ptr;
223 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
225 if (filename[proto_len] != ':' || is_dos_path(filename))
226 strcpy(proto_str, "file");
228 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
230 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
231 if ((ptr = strchr(proto_nested, '+')))
236 if (!strcmp(proto_str, up->name))
237 return url_alloc_for_protocol (puc, up, filename, flags);
238 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
239 !strcmp(proto_nested, up->name))
240 return url_alloc_for_protocol (puc, up, filename, flags);
244 return AVERROR(ENOENT);
247 int ffurl_open(URLContext **puc, const char *filename, int flags)
249 int ret = ffurl_alloc(puc, filename, flags);
252 ret = ffurl_connect(*puc);
260 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
261 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
264 int fast_retries = 5;
267 while (len < size_min) {
268 ret = transfer_func(h, buf+len, size-len);
269 if (ret == AVERROR(EINTR))
271 if (h->flags & AVIO_FLAG_NONBLOCK)
273 if (ret == AVERROR(EAGAIN)) {
280 return ret < 0 ? ret : len;
282 fast_retries = FFMAX(fast_retries, 2);
284 if (url_interrupt_cb())
290 int ffurl_read(URLContext *h, unsigned char *buf, int size)
292 if (h->flags & AVIO_FLAG_WRITE)
294 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
297 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
299 if (h->flags & AVIO_FLAG_WRITE)
301 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
304 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
306 if (!(h->flags & AVIO_FLAG_WRITE))
308 /* avoid sending too big packets */
309 if (h->max_packet_size && size > h->max_packet_size)
312 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
315 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
319 if (!h->prot->url_seek)
320 return AVERROR(ENOSYS);
321 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
325 int ffurl_close(URLContext *h)
328 if (!h) return 0; /* can happen when ffurl_open fails */
330 if (h->is_connected && h->prot->url_close)
331 ret = h->prot->url_close(h);
335 if (h->prot->priv_data_size)
336 av_free(h->priv_data);
342 int url_exist(const char *filename)
345 if (ffurl_open(&h, filename, AVIO_FLAG_READ) < 0)
352 int avio_check(const char *url, int flags)
355 int ret = ffurl_alloc(&h, url, flags);
359 if (h->prot->url_check) {
360 ret = h->prot->url_check(h, flags);
362 ret = ffurl_connect(h);
371 int64_t ffurl_size(URLContext *h)
375 size= ffurl_seek(h, 0, AVSEEK_SIZE);
377 pos = ffurl_seek(h, 0, SEEK_CUR);
378 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
381 ffurl_seek(h, pos, SEEK_SET);
386 int ffurl_get_file_handle(URLContext *h)
388 if (!h->prot->url_get_file_handle)
390 return h->prot->url_get_file_handle(h);
393 static int default_interrupt_cb(void)
398 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
401 interrupt_cb = default_interrupt_cb;
402 url_interrupt_cb = interrupt_cb;
406 int av_url_read_pause(URLContext *h, int pause)
408 if (!h->prot->url_read_pause)
409 return AVERROR(ENOSYS);
410 return h->prot->url_read_pause(h, pause);
413 int64_t av_url_read_seek(URLContext *h,
414 int stream_index, int64_t timestamp, int flags)
416 if (!h->prot->url_read_seek)
417 return AVERROR(ENOSYS);
418 return h->prot->url_read_seek(h, stream_index, timestamp, flags);