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"
35 /** @name Logging context. */
37 static const char *urlcontext_to_name(void *ptr)
39 URLContext *h = (URLContext *)ptr;
40 if(h->prot) return h->prot->name;
43 static const AVOption options[] = {{NULL}};
44 static const AVClass urlcontext_class =
45 { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
49 static int default_interrupt_cb(void);
51 URLProtocol *first_protocol = NULL;
52 int (*url_interrupt_cb)(void) = default_interrupt_cb;
54 URLProtocol *av_protocol_next(URLProtocol *p)
57 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 #if FF_API_REGISTER_PROTOCOL
86 /* The layout of URLProtocol as of when major was bumped to 52 */
87 struct URLProtocol_compat {
89 int (*url_open)(URLContext *h, const char *filename, int flags);
90 int (*url_read)(URLContext *h, unsigned char *buf, int size);
91 int (*url_write)(URLContext *h, unsigned char *buf, int size);
92 int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
93 int (*url_close)(URLContext *h);
94 struct URLProtocol *next;
97 int av_register_protocol(URLProtocol *protocol)
99 return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
102 int register_protocol(URLProtocol *protocol)
104 return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
108 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
109 const char *filename, int flags)
115 if (!ff_network_init())
118 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
120 err = AVERROR(ENOMEM);
124 uc->av_class = &urlcontext_class;
126 uc->filename = (char *) &uc[1];
127 strcpy(uc->filename, filename);
130 uc->is_streamed = 0; /* default = not streamed */
131 uc->max_packet_size = 0; /* default: stream file */
132 if (up->priv_data_size) {
133 uc->priv_data = av_mallocz(up->priv_data_size);
134 if (up->priv_data_class) {
135 *(const AVClass**)uc->priv_data = up->priv_data_class;
136 av_opt_set_defaults(uc->priv_data);
150 int ffurl_connect(URLContext* uc)
152 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
155 uc->is_connected = 1;
156 //We must be careful here as ffurl_seek() could be slow, for example for http
157 if( (uc->flags & (AVIO_WRONLY | AVIO_RDWR))
158 || !strcmp(uc->prot->name, "file"))
159 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
165 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
166 const char *filename, int flags)
170 ret = url_alloc_for_protocol(puc, up, filename, flags);
173 ret = ffurl_connect(*puc);
181 int url_alloc(URLContext **puc, const char *filename, int flags)
183 return ffurl_alloc(puc, filename, flags);
185 int url_connect(URLContext* uc)
187 return ffurl_connect(uc);
189 int url_open(URLContext **puc, const char *filename, int flags)
191 return ffurl_open(puc, filename, flags);
193 int url_read(URLContext *h, unsigned char *buf, int size)
195 return ffurl_read(h, buf, size);
197 int url_read_complete(URLContext *h, unsigned char *buf, int size)
199 return ffurl_read_complete(h, buf, size);
201 int url_write(URLContext *h, const unsigned char *buf, int size)
203 return ffurl_write(h, buf, size);
205 int64_t url_seek(URLContext *h, int64_t pos, int whence)
207 return ffurl_seek(h, pos, whence);
209 int url_close(URLContext *h)
211 return ffurl_close(h);
213 int64_t url_filesize(URLContext *h)
215 return ffurl_size(h);
217 int url_get_file_handle(URLContext *h)
219 return ffurl_get_file_handle(h);
221 int url_get_max_packet_size(URLContext *h)
223 return h->max_packet_size;
225 void url_get_filename(URLContext *h, char *buf, int buf_size)
227 av_strlcpy(buf, h->filename, buf_size);
229 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
231 avio_set_interrupt_cb(interrupt_cb);
233 int av_register_protocol2(URLProtocol *protocol, int size)
235 return ffurl_register_protocol(protocol, size);
239 #define URL_SCHEME_CHARS \
240 "abcdefghijklmnopqrstuvwxyz" \
241 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
244 int ffurl_alloc(URLContext **puc, const char *filename, int flags)
247 char proto_str[128], proto_nested[128], *ptr;
248 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
250 if (filename[proto_len] != ':' || is_dos_path(filename))
251 strcpy(proto_str, "file");
253 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
255 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
256 if ((ptr = strchr(proto_nested, '+')))
261 if (!strcmp(proto_str, up->name))
262 return url_alloc_for_protocol (puc, up, filename, flags);
263 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
264 !strcmp(proto_nested, up->name))
265 return url_alloc_for_protocol (puc, up, filename, flags);
269 return AVERROR(ENOENT);
272 int ffurl_open(URLContext **puc, const char *filename, int flags)
274 int ret = ffurl_alloc(puc, filename, flags);
277 ret = ffurl_connect(*puc);
285 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
286 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
289 int fast_retries = 5;
292 while (len < size_min) {
293 ret = transfer_func(h, buf+len, size-len);
294 if (ret == AVERROR(EINTR))
296 if (h->flags & AVIO_FLAG_NONBLOCK)
298 if (ret == AVERROR(EAGAIN)) {
305 return ret < 0 ? ret : len;
307 fast_retries = FFMAX(fast_retries, 2);
309 if (len < size && url_interrupt_cb())
315 int ffurl_read(URLContext *h, unsigned char *buf, int size)
317 if (h->flags & AVIO_WRONLY)
319 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
322 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
324 if (h->flags & AVIO_WRONLY)
326 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
329 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
331 if (!(h->flags & (AVIO_WRONLY | AVIO_RDWR)))
333 /* avoid sending too big packets */
334 if (h->max_packet_size && size > h->max_packet_size)
337 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
340 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
344 if (!h->prot->url_seek)
345 return AVERROR(ENOSYS);
346 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
350 int ffurl_close(URLContext *h)
353 if (!h) return 0; /* can happen when ffurl_open fails */
355 if (h->is_connected && h->prot->url_close)
356 ret = h->prot->url_close(h);
360 if (h->prot->priv_data_size)
361 av_free(h->priv_data);
367 int url_exist(const char *filename)
370 if (ffurl_open(&h, filename, AVIO_RDONLY) < 0)
377 int avio_check(const char *url, int flags)
380 int ret = ffurl_alloc(&h, url, flags);
384 if (h->prot->url_check) {
385 ret = h->prot->url_check(h, flags);
387 ret = ffurl_connect(h);
396 int64_t ffurl_size(URLContext *h)
400 size= ffurl_seek(h, 0, AVSEEK_SIZE);
402 pos = ffurl_seek(h, 0, SEEK_CUR);
403 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
406 ffurl_seek(h, pos, SEEK_SET);
411 int ffurl_get_file_handle(URLContext *h)
413 if (!h->prot->url_get_file_handle)
415 return h->prot->url_get_file_handle(h);
418 static int default_interrupt_cb(void)
423 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
426 interrupt_cb = default_interrupt_cb;
427 url_interrupt_cb = interrupt_cb;
431 int av_url_read_pause(URLContext *h, int pause)
433 if (!h->prot->url_read_pause)
434 return AVERROR(ENOSYS);
435 return h->prot->url_read_pause(h, pause);
438 int64_t av_url_read_seek(URLContext *h,
439 int stream_index, int64_t timestamp, int flags)
441 if (!h->prot->url_read_seek)
442 return AVERROR(ENOSYS);
443 return h->prot->url_read_seek(h, stream_index, timestamp, flags);