2 * Unbuffered io for ffmpeg system
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
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;
52 URLProtocol *av_protocol_next(URLProtocol *p)
55 else return first_protocol;
58 const char *avio_enum_protocols(void **opaque, int output)
60 URLProtocol **p = opaque;
61 *p = *p ? (*p)->next : first_protocol;
63 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
65 return avio_enum_protocols(opaque, output);
68 int ffurl_register_protocol(URLProtocol *protocol, int size)
71 if (size < sizeof(URLProtocol)) {
72 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
73 memcpy(temp, protocol, size);
77 while (*p != NULL) p = &(*p)->next;
79 protocol->next = NULL;
83 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
84 const char *filename, int flags)
90 if (!ff_network_init())
93 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
95 err = AVERROR(ENOMEM);
98 uc->av_class = &urlcontext_class;
99 uc->filename = (char *) &uc[1];
100 strcpy(uc->filename, filename);
103 uc->is_streamed = 0; /* default = not streamed */
104 uc->max_packet_size = 0; /* default: stream file */
105 if (up->priv_data_size) {
106 uc->priv_data = av_mallocz(up->priv_data_size);
107 if (up->priv_data_class) {
108 *(const AVClass**)uc->priv_data = up->priv_data_class;
109 av_opt_set_defaults(uc->priv_data);
123 int ffurl_connect(URLContext* uc)
125 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
128 uc->is_connected = 1;
129 //We must be careful here as ffurl_seek() could be slow, for example for http
130 if( (uc->flags & AVIO_FLAG_WRITE)
131 || !strcmp(uc->prot->name, "file"))
132 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
138 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
139 const char *filename, int flags)
143 ret = url_alloc_for_protocol(puc, up, filename, flags);
146 ret = ffurl_connect(*puc);
154 int url_alloc(URLContext **puc, const char *filename, int flags)
156 return ffurl_alloc(puc, filename, flags);
158 int url_connect(URLContext* uc)
160 return ffurl_connect(uc);
162 int url_open(URLContext **puc, const char *filename, int flags)
164 return ffurl_open(puc, filename, flags);
166 int url_read(URLContext *h, unsigned char *buf, int size)
168 return ffurl_read(h, buf, size);
170 int url_read_complete(URLContext *h, unsigned char *buf, int size)
172 return ffurl_read_complete(h, buf, size);
174 int url_write(URLContext *h, const unsigned char *buf, int size)
176 return ffurl_write(h, buf, size);
178 int64_t url_seek(URLContext *h, int64_t pos, int whence)
180 return ffurl_seek(h, pos, whence);
182 int url_close(URLContext *h)
184 return ffurl_close(h);
186 int64_t url_filesize(URLContext *h)
188 return ffurl_size(h);
190 int url_get_file_handle(URLContext *h)
192 return ffurl_get_file_handle(h);
194 int url_get_max_packet_size(URLContext *h)
196 return h->max_packet_size;
198 void url_get_filename(URLContext *h, char *buf, int buf_size)
200 av_strlcpy(buf, h->filename, buf_size);
202 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
204 avio_set_interrupt_cb(interrupt_cb);
206 int av_register_protocol2(URLProtocol *protocol, int size)
208 return ffurl_register_protocol(protocol, size);
212 #define URL_SCHEME_CHARS \
213 "abcdefghijklmnopqrstuvwxyz" \
214 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
217 int ffurl_alloc(URLContext **puc, const char *filename, int flags)
220 char proto_str[128], proto_nested[128], *ptr;
221 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
223 if (filename[proto_len] != ':' || is_dos_path(filename))
224 strcpy(proto_str, "file");
226 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
228 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
229 if ((ptr = strchr(proto_nested, '+')))
234 if (!strcmp(proto_str, up->name))
235 return url_alloc_for_protocol (puc, up, filename, flags);
236 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
237 !strcmp(proto_nested, up->name))
238 return url_alloc_for_protocol (puc, up, filename, flags);
242 return AVERROR(ENOENT);
245 int ffurl_open(URLContext **puc, const char *filename, int flags)
247 int ret = ffurl_alloc(puc, filename, flags);
250 ret = ffurl_connect(*puc);
258 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
259 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
262 int fast_retries = 5;
265 while (len < size_min) {
266 ret = transfer_func(h, buf+len, size-len);
267 if (ret == AVERROR(EINTR))
269 if (h->flags & AVIO_FLAG_NONBLOCK)
271 if (ret == AVERROR(EAGAIN)) {
278 return ret < 0 ? ret : len;
280 fast_retries = FFMAX(fast_retries, 2);
282 if (len < size && url_interrupt_cb())
288 int ffurl_read(URLContext *h, unsigned char *buf, int size)
290 if (!(h->flags & AVIO_FLAG_READ))
292 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
295 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
297 if (!(h->flags & AVIO_FLAG_READ))
299 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
302 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
304 if (!(h->flags & AVIO_FLAG_WRITE))
306 /* avoid sending too big packets */
307 if (h->max_packet_size && size > h->max_packet_size)
310 return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
313 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
317 if (!h->prot->url_seek)
318 return AVERROR(ENOSYS);
319 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
323 int ffurl_close(URLContext *h)
326 if (!h) return 0; /* can happen when ffurl_open fails */
328 if (h->is_connected && h->prot->url_close)
329 ret = h->prot->url_close(h);
333 if (h->prot->priv_data_size)
334 av_free(h->priv_data);
340 int url_exist(const char *filename)
343 if (ffurl_open(&h, filename, AVIO_FLAG_READ) < 0)
350 int avio_check(const char *url, int flags)
353 int ret = ffurl_alloc(&h, url, flags);
357 if (h->prot->url_check) {
358 ret = h->prot->url_check(h, flags);
360 ret = ffurl_connect(h);
369 int64_t ffurl_size(URLContext *h)
373 size= ffurl_seek(h, 0, AVSEEK_SIZE);
375 pos = ffurl_seek(h, 0, SEEK_CUR);
376 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
379 ffurl_seek(h, pos, SEEK_SET);
384 int ffurl_get_file_handle(URLContext *h)
386 if (!h->prot->url_get_file_handle)
388 return h->prot->url_get_file_handle(h);
391 static int default_interrupt_cb(void)
396 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
399 interrupt_cb = default_interrupt_cb;
400 url_interrupt_cb = interrupt_cb;
404 int av_url_read_pause(URLContext *h, int pause)
406 if (!h->prot->url_read_pause)
407 return AVERROR(ENOSYS);
408 return h->prot->url_read_pause(h, pause);
411 int64_t av_url_read_seek(URLContext *h,
412 int stream_index, int64_t timestamp, int flags)
414 if (!h->prot->url_read_seek)
415 return AVERROR(ENOSYS);
416 return h->prot->url_read_seek(h, stream_index, timestamp, flags);