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 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
54 URLProtocol *av_protocol_next(URLProtocol *p)
57 else return first_protocol;
60 int av_register_protocol2(URLProtocol *protocol, int size)
63 if (size < sizeof(URLProtocol)) {
64 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
65 memcpy(temp, protocol, size);
69 while (*p != NULL) p = &(*p)->next;
71 protocol->next = NULL;
75 #if FF_API_REGISTER_PROTOCOL
76 /* The layout of URLProtocol as of when major was bumped to 52 */
77 struct URLProtocol_compat {
79 int (*url_open)(URLContext *h, const char *filename, int flags);
80 int (*url_read)(URLContext *h, unsigned char *buf, int size);
81 int (*url_write)(URLContext *h, unsigned char *buf, int size);
82 int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
83 int (*url_close)(URLContext *h);
84 struct URLProtocol *next;
87 int av_register_protocol(URLProtocol *protocol)
89 return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
92 int register_protocol(URLProtocol *protocol)
94 return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
98 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
99 const char *filename, int flags)
105 if (!ff_network_init())
108 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
110 err = AVERROR(ENOMEM);
114 uc->av_class = &urlcontext_class;
116 uc->filename = (char *) &uc[1];
117 strcpy(uc->filename, filename);
120 uc->is_streamed = 0; /* default = not streamed */
121 uc->max_packet_size = 0; /* default: stream file */
122 if (up->priv_data_size) {
123 uc->priv_data = av_mallocz(up->priv_data_size);
124 if (up->priv_data_class) {
125 *(const AVClass**)uc->priv_data = up->priv_data_class;
126 av_opt_set_defaults(uc->priv_data);
140 int url_connect(URLContext* uc)
142 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
145 uc->is_connected = 1;
146 //We must be careful here as url_seek() could be slow, for example for http
147 if( (uc->flags & (URL_WRONLY | URL_RDWR))
148 || !strcmp(uc->prot->name, "file"))
149 if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
155 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
156 const char *filename, int flags)
160 ret = url_alloc_for_protocol(puc, up, filename, flags);
163 ret = url_connect(*puc);
171 int url_alloc(URLContext **puc, const char *filename, int flags)
173 return ffurl_alloc(puc, filename, flags);
177 #define URL_SCHEME_CHARS \
178 "abcdefghijklmnopqrstuvwxyz" \
179 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
182 int ffurl_alloc(URLContext **puc, const char *filename, int flags)
185 char proto_str[128], proto_nested[128], *ptr;
186 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
188 if (filename[proto_len] != ':' || is_dos_path(filename))
189 strcpy(proto_str, "file");
191 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
193 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
194 if ((ptr = strchr(proto_nested, '+')))
199 if (!strcmp(proto_str, up->name))
200 return url_alloc_for_protocol (puc, up, filename, flags);
201 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
202 !strcmp(proto_nested, up->name))
203 return url_alloc_for_protocol (puc, up, filename, flags);
207 return AVERROR(ENOENT);
210 int url_open(URLContext **puc, const char *filename, int flags)
212 int ret = ffurl_alloc(puc, filename, flags);
215 ret = url_connect(*puc);
223 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
224 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
227 int fast_retries = 5;
230 while (len < size_min) {
231 ret = transfer_func(h, buf+len, size-len);
232 if (ret == AVERROR(EINTR))
234 if (h->flags & URL_FLAG_NONBLOCK)
236 if (ret == AVERROR(EAGAIN)) {
243 return ret < 0 ? ret : len;
245 fast_retries = FFMAX(fast_retries, 2);
247 if (url_interrupt_cb())
253 int url_read(URLContext *h, unsigned char *buf, int size)
255 if (h->flags & URL_WRONLY)
257 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
260 int url_read_complete(URLContext *h, unsigned char *buf, int size)
262 if (h->flags & URL_WRONLY)
264 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
267 int url_write(URLContext *h, const unsigned char *buf, int size)
269 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
271 /* avoid sending too big packets */
272 if (h->max_packet_size && size > h->max_packet_size)
275 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
278 int64_t url_seek(URLContext *h, int64_t pos, int whence)
282 if (!h->prot->url_seek)
283 return AVERROR(ENOSYS);
284 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
288 int url_close(URLContext *h)
291 if (!h) return 0; /* can happen when url_open fails */
293 if (h->is_connected && h->prot->url_close)
294 ret = h->prot->url_close(h);
298 if (h->prot->priv_data_size)
299 av_free(h->priv_data);
304 int url_exist(const char *filename)
307 if (url_open(&h, filename, URL_RDONLY) < 0)
313 int64_t url_filesize(URLContext *h)
317 size= url_seek(h, 0, AVSEEK_SIZE);
319 pos = url_seek(h, 0, SEEK_CUR);
320 if ((size = url_seek(h, -1, SEEK_END)) < 0)
323 url_seek(h, pos, SEEK_SET);
328 int url_get_file_handle(URLContext *h)
330 if (!h->prot->url_get_file_handle)
332 return h->prot->url_get_file_handle(h);
335 int url_get_max_packet_size(URLContext *h)
337 return h->max_packet_size;
340 void url_get_filename(URLContext *h, char *buf, int buf_size)
342 av_strlcpy(buf, h->filename, buf_size);
346 static int default_interrupt_cb(void)
351 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
354 interrupt_cb = default_interrupt_cb;
355 url_interrupt_cb = interrupt_cb;
358 int av_url_read_pause(URLContext *h, int pause)
360 if (!h->prot->url_read_pause)
361 return AVERROR(ENOSYS);
362 return h->prot->url_read_pause(h, pause);
365 int64_t av_url_read_seek(URLContext *h,
366 int stream_index, int64_t timestamp, int flags)
368 if (!h->prot->url_read_seek)
369 return AVERROR(ENOSYS);
370 return h->prot->url_read_seek(h, stream_index, timestamp, flags);