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 #include "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.h"
26 #include "os_support.h"
33 /** @name Logging context. */
35 static const char *urlcontext_to_name(void *ptr)
37 URLContext *h = (URLContext *)ptr;
44 static void *urlcontext_child_next(void *obj, void *prev)
47 if (!prev && h->priv_data && h->prot->priv_data_class)
52 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
56 /* find the protocol that corresponds to prev */
57 for (i = 0; ff_url_protocols[i]; i++) {
58 if (ff_url_protocols[i]->priv_data_class == prev) {
64 /* find next protocol with priv options */
65 for (; ff_url_protocols[i]; i++)
66 if (ff_url_protocols[i]->priv_data_class)
67 return ff_url_protocols[i]->priv_data_class;
71 static const AVOption options[] = { { NULL } };
72 const AVClass ffurl_context_class = {
73 .class_name = "URLContext",
74 .item_name = urlcontext_to_name,
76 .version = LIBAVUTIL_VERSION_INT,
77 .child_next = urlcontext_child_next,
78 .child_class_next = urlcontext_child_class_next,
82 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
83 const char *filename, int flags,
84 const AVIOInterruptCB *int_cb)
90 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
93 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
95 err = AVERROR(ENOMEM);
98 uc->av_class = &ffurl_context_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 (!uc->priv_data) {
108 err = AVERROR(ENOMEM);
111 if (up->priv_data_class) {
112 *(const AVClass **)uc->priv_data = up->priv_data_class;
113 av_opt_set_defaults(uc->priv_data);
117 uc->interrupt_callback = *int_cb;
124 av_freep(&uc->priv_data);
127 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
133 int ffurl_connect(URLContext *uc, AVDictionary **options)
136 uc->prot->url_open2 ? uc->prot->url_open2(uc,
140 uc->prot->url_open(uc, uc->filename, uc->flags);
143 uc->is_connected = 1;
144 /* We must be careful here as ffurl_seek() could be slow,
145 * for example for http */
146 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
147 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
152 #define URL_SCHEME_CHARS \
153 "abcdefghijklmnopqrstuvwxyz" \
154 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
157 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
158 const AVIOInterruptCB *int_cb)
160 char proto_str[128], proto_nested[128], *ptr;
161 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
164 if (filename[proto_len] != ':' || is_dos_path(filename))
165 strcpy(proto_str, "file");
167 av_strlcpy(proto_str, filename,
168 FFMIN(proto_len + 1, sizeof(proto_str)));
170 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
171 if ((ptr = strchr(proto_nested, '+')))
174 for (i = 0; ff_url_protocols[i]; i++) {
175 const URLProtocol *up = ff_url_protocols[i];
176 if (!strcmp(proto_str, up->name))
177 return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
178 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
179 !strcmp(proto_nested, up->name))
180 return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
183 return AVERROR_PROTOCOL_NOT_FOUND;
186 int ffurl_open(URLContext **puc, const char *filename, int flags,
187 const AVIOInterruptCB *int_cb, AVDictionary **options)
189 int ret = ffurl_alloc(puc, filename, flags, int_cb);
192 if (options && (*puc)->prot->priv_data_class &&
193 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
195 ret = ffurl_connect(*puc, options);
204 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
205 int size, int size_min,
206 int (*transfer_func)(URLContext *h,
211 int fast_retries = 5;
214 while (len < size_min) {
215 ret = transfer_func(h, buf + len, size - len);
216 if (ret == AVERROR(EINTR))
218 if (h->flags & AVIO_FLAG_NONBLOCK)
220 if (ret == AVERROR(EAGAIN)) {
227 return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
229 fast_retries = FFMAX(fast_retries, 2);
231 if (ff_check_interrupt(&h->interrupt_callback))
237 int ffurl_read(URLContext *h, unsigned char *buf, int size)
239 if (!(h->flags & AVIO_FLAG_READ))
241 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
244 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
246 if (!(h->flags & AVIO_FLAG_READ))
248 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
251 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
253 if (!(h->flags & AVIO_FLAG_WRITE))
255 /* avoid sending too big packets */
256 if (h->max_packet_size && size > h->max_packet_size)
259 return retry_transfer_wrapper(h, buf, size, size,
260 (int (*)(struct URLContext *, uint8_t *, int))
264 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
268 if (!h->prot->url_seek)
269 return AVERROR(ENOSYS);
270 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
274 int ffurl_close(URLContext *h)
278 return 0; /* can happen when ffurl_open fails */
280 if (h->is_connected && h->prot->url_close)
281 ret = h->prot->url_close(h);
283 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
286 if (h->prot->priv_data_size) {
287 if (h->prot->priv_data_class)
288 av_opt_free(h->priv_data);
289 av_free(h->priv_data);
295 int avio_check(const char *url, int flags)
298 int ret = ffurl_alloc(&h, url, flags, NULL);
302 if (h->prot->url_check) {
303 ret = h->prot->url_check(h, flags);
305 ret = ffurl_connect(h, NULL);
314 int64_t ffurl_size(URLContext *h)
318 size = ffurl_seek(h, 0, AVSEEK_SIZE);
320 pos = ffurl_seek(h, 0, SEEK_CUR);
321 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
324 ffurl_seek(h, pos, SEEK_SET);
329 int ffurl_get_file_handle(URLContext *h)
331 if (!h->prot->url_get_file_handle)
333 return h->prot->url_get_file_handle(h);
336 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
338 if (!h->prot->url_get_multi_file_handle) {
339 if (!h->prot->url_get_file_handle)
340 return AVERROR(ENOSYS);
341 *handles = av_malloc(sizeof(**handles));
343 return AVERROR(ENOMEM);
345 *handles[0] = h->prot->url_get_file_handle(h);
348 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
351 int ffurl_shutdown(URLContext *h, int flags)
353 if (!h->prot->url_shutdown)
354 return AVERROR(EINVAL);
355 return h->prot->url_shutdown(h, flags);
358 int ff_check_interrupt(AVIOInterruptCB *cb)
361 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))