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"
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;
55 URLProtocol *av_protocol_next(URLProtocol *p)
58 else return first_protocol;
62 const char *avio_enum_protocols(void **opaque, int output)
64 URLProtocol **p = opaque;
65 *p = *p ? (*p)->next : first_protocol;
67 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
69 return avio_enum_protocols(opaque, output);
72 int ffurl_register_protocol(URLProtocol *protocol, int size)
75 if (size < sizeof(URLProtocol)) {
76 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
77 memcpy(temp, protocol, size);
81 while (*p != NULL) p = &(*p)->next;
83 protocol->next = NULL;
87 #if FF_API_REGISTER_PROTOCOL
88 /* The layout of URLProtocol as of when major was bumped to 52 */
89 struct URLProtocol_compat {
91 int (*url_open)(URLContext *h, const char *filename, int flags);
92 int (*url_read)(URLContext *h, unsigned char *buf, int size);
93 int (*url_write)(URLContext *h, unsigned char *buf, int size);
94 int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
95 int (*url_close)(URLContext *h);
96 struct URLProtocol *next;
99 int av_register_protocol(URLProtocol *protocol)
101 return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
104 int register_protocol(URLProtocol *protocol)
106 return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
110 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
111 const char *filename, int flags)
117 if (!ff_network_init())
120 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
122 err = AVERROR(ENOMEM);
126 uc->av_class = &urlcontext_class;
128 uc->filename = (char *) &uc[1];
129 strcpy(uc->filename, filename);
132 uc->is_streamed = 0; /* default = not streamed */
133 uc->max_packet_size = 0; /* default: stream file */
134 if (up->priv_data_size) {
135 uc->priv_data = av_mallocz(up->priv_data_size);
136 if (up->priv_data_class) {
137 *(const AVClass**)uc->priv_data = up->priv_data_class;
138 av_opt_set_defaults(uc->priv_data);
152 int ffurl_connect(URLContext* uc)
154 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
157 uc->is_connected = 1;
158 //We must be careful here as ffurl_seek() could be slow, for example for http
159 if( (uc->flags & (AVIO_WRONLY | AVIO_RDWR))
160 || !strcmp(uc->prot->name, "file"))
161 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
167 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
168 const char *filename, int flags)
172 ret = url_alloc_for_protocol(puc, up, filename, flags);
175 ret = ffurl_connect(*puc);
183 int url_alloc(URLContext **puc, const char *filename, int flags)
185 return ffurl_alloc(puc, filename, flags);
187 int url_connect(URLContext* uc)
189 return ffurl_connect(uc);
191 int url_open(URLContext **puc, const char *filename, int flags)
193 return ffurl_open(puc, filename, flags);
195 int url_read(URLContext *h, unsigned char *buf, int size)
197 return ffurl_read(h, buf, size);
199 int url_read_complete(URLContext *h, unsigned char *buf, int size)
201 return ffurl_read_complete(h, buf, size);
203 int url_write(URLContext *h, const unsigned char *buf, int size)
205 return ffurl_write(h, buf, size);
207 int64_t url_seek(URLContext *h, int64_t pos, int whence)
209 return ffurl_seek(h, pos, whence);
211 int url_close(URLContext *h)
213 return ffurl_close(h);
215 int64_t url_filesize(URLContext *h)
217 return ffurl_size(h);
219 int url_get_file_handle(URLContext *h)
221 return ffurl_get_file_handle(h);
223 int url_get_max_packet_size(URLContext *h)
225 return h->max_packet_size;
227 void url_get_filename(URLContext *h, char *buf, int buf_size)
229 av_strlcpy(buf, h->filename, buf_size);
231 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
233 avio_set_interrupt_cb(interrupt_cb);
235 int av_register_protocol2(URLProtocol *protocol, int size)
237 return ffurl_register_protocol(protocol, size);
241 #define URL_SCHEME_CHARS \
242 "abcdefghijklmnopqrstuvwxyz" \
243 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
246 int ffurl_alloc(URLContext **puc, const char *filename, int flags)
249 char proto_str[128], proto_nested[128], *ptr;
250 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
252 if (filename[proto_len] != ':' || is_dos_path(filename))
253 strcpy(proto_str, "file");
255 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
257 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
258 if ((ptr = strchr(proto_nested, '+')))
263 if (!strcmp(proto_str, up->name))
264 return url_alloc_for_protocol (puc, up, filename, flags);
265 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
266 !strcmp(proto_nested, up->name))
267 return url_alloc_for_protocol (puc, up, filename, flags);
271 return AVERROR(ENOENT);
274 int ffurl_open(URLContext **puc, const char *filename, int flags)
276 int ret = ffurl_alloc(puc, filename, flags);
279 ret = ffurl_connect(*puc);
287 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
288 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
291 int fast_retries = 5;
294 while (len < size_min) {
295 ret = transfer_func(h, buf+len, size-len);
296 if (ret == AVERROR(EINTR))
298 if (h->flags & AVIO_FLAG_NONBLOCK)
300 if (ret == AVERROR(EAGAIN)) {
307 return ret < 0 ? ret : len;
309 fast_retries = FFMAX(fast_retries, 2);
311 if (url_interrupt_cb())
317 int ffurl_read(URLContext *h, unsigned char *buf, int size)
319 if (h->flags & AVIO_WRONLY)
321 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
324 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
326 if (h->flags & AVIO_WRONLY)
328 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
331 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
333 if (!(h->flags & (AVIO_WRONLY | AVIO_RDWR)))
335 /* avoid sending too big packets */
336 if (h->max_packet_size && size > h->max_packet_size)
339 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
342 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
346 if (!h->prot->url_seek)
347 return AVERROR(ENOSYS);
348 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
352 int ffurl_close(URLContext *h)
355 if (!h) return 0; /* can happen when ffurl_open fails */
357 if (h->is_connected && h->prot->url_close)
358 ret = h->prot->url_close(h);
362 if (h->prot->priv_data_size)
363 av_free(h->priv_data);
369 int url_exist(const char *filename)
372 if (ffurl_open(&h, filename, AVIO_RDONLY) < 0)
379 int avio_check(const char *url, int flags)
382 int ret = ffurl_alloc(&h, url, flags);
386 if (h->prot->url_check) {
387 ret = h->prot->url_check(h, flags);
389 ret = ffurl_connect(h);
398 int64_t ffurl_size(URLContext *h)
402 size= ffurl_seek(h, 0, AVSEEK_SIZE);
404 pos = ffurl_seek(h, 0, SEEK_CUR);
405 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
408 ffurl_seek(h, pos, SEEK_SET);
413 int ffurl_get_file_handle(URLContext *h)
415 if (!h->prot->url_get_file_handle)
417 return h->prot->url_get_file_handle(h);
420 static int default_interrupt_cb(void)
425 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
428 interrupt_cb = default_interrupt_cb;
429 url_interrupt_cb = interrupt_cb;
433 int av_url_read_pause(URLContext *h, int pause)
435 if (!h->prot->url_read_pause)
436 return AVERROR(ENOSYS);
437 return h->prot->url_read_pause(h, pause);
440 int64_t av_url_read_seek(URLContext *h,
441 int stream_index, int64_t timestamp, int flags)
443 if (!h->prot->url_read_seek)
444 return AVERROR(ENOSYS);
445 return h->prot->url_read_seek(h, stream_index, timestamp, flags);