]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
protocols: make the list of protocols static
[ffmpeg] / libavformat / avio.c
1 /*
2  * unbuffered I/O
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
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"
27 #include "avformat.h"
28 #if CONFIG_NETWORK
29 #include "network.h"
30 #endif
31 #include "url.h"
32
33 /** @name Logging context. */
34 /*@{*/
35 static const char *urlcontext_to_name(void *ptr)
36 {
37     URLContext *h = (URLContext *)ptr;
38     if (h->prot)
39         return h->prot->name;
40     else
41         return "NULL";
42 }
43
44 static void *urlcontext_child_next(void *obj, void *prev)
45 {
46     URLContext *h = obj;
47     if (!prev && h->priv_data && h->prot->priv_data_class)
48         return h->priv_data;
49     return NULL;
50 }
51
52 static const AVOption options[] = { { NULL } };
53 const AVClass ffurl_context_class = {
54     .class_name       = "URLContext",
55     .item_name        = urlcontext_to_name,
56     .option           = options,
57     .version          = LIBAVUTIL_VERSION_INT,
58     .child_next       = urlcontext_child_next,
59     .child_class_next = ff_urlcontext_child_class_next,
60 };
61 /*@}*/
62
63 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
64                                   const char *filename, int flags,
65                                   const AVIOInterruptCB *int_cb)
66 {
67     URLContext *uc;
68     int err;
69
70 #if CONFIG_NETWORK
71     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
72         return AVERROR(EIO);
73 #endif
74     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
75     if (!uc) {
76         err = AVERROR(ENOMEM);
77         goto fail;
78     }
79     uc->av_class = &ffurl_context_class;
80     uc->filename = (char *)&uc[1];
81     strcpy(uc->filename, filename);
82     uc->prot            = up;
83     uc->flags           = flags;
84     uc->is_streamed     = 0; /* default = not streamed */
85     uc->max_packet_size = 0; /* default: stream file */
86     if (up->priv_data_size) {
87         uc->priv_data = av_mallocz(up->priv_data_size);
88         if (!uc->priv_data) {
89             err = AVERROR(ENOMEM);
90             goto fail;
91         }
92         if (up->priv_data_class) {
93             *(const AVClass **)uc->priv_data = up->priv_data_class;
94             av_opt_set_defaults(uc->priv_data);
95         }
96     }
97     if (int_cb)
98         uc->interrupt_callback = *int_cb;
99
100     *puc = uc;
101     return 0;
102 fail:
103     *puc = NULL;
104     if (uc)
105         av_freep(&uc->priv_data);
106     av_freep(&uc);
107 #if CONFIG_NETWORK
108     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
109         ff_network_close();
110 #endif
111     return err;
112 }
113
114 int ffurl_connect(URLContext *uc, AVDictionary **options)
115 {
116     int err =
117         uc->prot->url_open2 ? uc->prot->url_open2(uc,
118                                                   uc->filename,
119                                                   uc->flags,
120                                                   options) :
121         uc->prot->url_open(uc, uc->filename, uc->flags);
122     if (err)
123         return err;
124     uc->is_connected = 1;
125     /* We must be careful here as ffurl_seek() could be slow,
126      * for example for http */
127     if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
128         if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
129             uc->is_streamed = 1;
130     return 0;
131 }
132
133 #define URL_SCHEME_CHARS                        \
134     "abcdefghijklmnopqrstuvwxyz"                \
135     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
136     "0123456789+-."
137
138 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
139                 const AVIOInterruptCB *int_cb)
140 {
141     const URLProtocol **protocols;
142     char proto_str[128], proto_nested[128], *ptr;
143     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
144     int i;
145
146     if (filename[proto_len] != ':' || is_dos_path(filename))
147         strcpy(proto_str, "file");
148     else
149         av_strlcpy(proto_str, filename,
150                    FFMIN(proto_len + 1, sizeof(proto_str)));
151
152     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
153     if ((ptr = strchr(proto_nested, '+')))
154         *ptr = '\0';
155
156     protocols = ffurl_get_protocols(NULL, NULL);
157     for (i = 0; protocols[i]; i++) {
158         const URLProtocol *up = protocols[i];
159         if (!strcmp(proto_str, up->name)) {
160             av_freep(&protocols);
161             return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
162         }
163         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
164             !strcmp(proto_nested, up->name)) {
165             av_freep(&protocols);
166             return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
167         }
168     }
169     *puc = NULL;
170     return AVERROR_PROTOCOL_NOT_FOUND;
171 }
172
173 int ffurl_open(URLContext **puc, const char *filename, int flags,
174                const AVIOInterruptCB *int_cb, AVDictionary **options)
175 {
176     int ret = ffurl_alloc(puc, filename, flags, int_cb);
177     if (ret)
178         return ret;
179     if (options && (*puc)->prot->priv_data_class &&
180         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
181         goto fail;
182     ret = ffurl_connect(*puc, options);
183     if (!ret)
184         return 0;
185 fail:
186     ffurl_close(*puc);
187     *puc = NULL;
188     return ret;
189 }
190
191 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
192                                          int size, int size_min,
193                                          int (*transfer_func)(URLContext *h,
194                                                               uint8_t *buf,
195                                                               int size))
196 {
197     int ret, len;
198     int fast_retries = 5;
199
200     len = 0;
201     while (len < size_min) {
202         ret = transfer_func(h, buf + len, size - len);
203         if (ret == AVERROR(EINTR))
204             continue;
205         if (h->flags & AVIO_FLAG_NONBLOCK)
206             return ret;
207         if (ret == AVERROR(EAGAIN)) {
208             ret = 0;
209             if (fast_retries)
210                 fast_retries--;
211             else
212                 av_usleep(1000);
213         } else if (ret < 1)
214             return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
215         if (ret)
216             fast_retries = FFMAX(fast_retries, 2);
217         len += ret;
218         if (ff_check_interrupt(&h->interrupt_callback))
219             return AVERROR_EXIT;
220     }
221     return len;
222 }
223
224 int ffurl_read(URLContext *h, unsigned char *buf, int size)
225 {
226     if (!(h->flags & AVIO_FLAG_READ))
227         return AVERROR(EIO);
228     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
229 }
230
231 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
232 {
233     if (!(h->flags & AVIO_FLAG_READ))
234         return AVERROR(EIO);
235     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
236 }
237
238 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
239 {
240     if (!(h->flags & AVIO_FLAG_WRITE))
241         return AVERROR(EIO);
242     /* avoid sending too big packets */
243     if (h->max_packet_size && size > h->max_packet_size)
244         return AVERROR(EIO);
245
246     return retry_transfer_wrapper(h, buf, size, size,
247                                   (int (*)(struct URLContext *, uint8_t *, int))
248                                   h->prot->url_write);
249 }
250
251 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
252 {
253     int64_t ret;
254
255     if (!h->prot->url_seek)
256         return AVERROR(ENOSYS);
257     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
258     return ret;
259 }
260
261 int ffurl_close(URLContext *h)
262 {
263     int ret = 0;
264     if (!h)
265         return 0;     /* can happen when ffurl_open fails */
266
267     if (h->is_connected && h->prot->url_close)
268         ret = h->prot->url_close(h);
269 #if CONFIG_NETWORK
270     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
271         ff_network_close();
272 #endif
273     if (h->prot->priv_data_size) {
274         if (h->prot->priv_data_class)
275             av_opt_free(h->priv_data);
276         av_free(h->priv_data);
277     }
278     av_free(h);
279     return ret;
280 }
281
282 int avio_check(const char *url, int flags)
283 {
284     URLContext *h;
285     int ret = ffurl_alloc(&h, url, flags, NULL);
286     if (ret)
287         return ret;
288
289     if (h->prot->url_check) {
290         ret = h->prot->url_check(h, flags);
291     } else {
292         ret = ffurl_connect(h, NULL);
293         if (ret >= 0)
294             ret = flags;
295     }
296
297     ffurl_close(h);
298     return ret;
299 }
300
301 int64_t ffurl_size(URLContext *h)
302 {
303     int64_t pos, size;
304
305     size = ffurl_seek(h, 0, AVSEEK_SIZE);
306     if (size < 0) {
307         pos = ffurl_seek(h, 0, SEEK_CUR);
308         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
309             return size;
310         size++;
311         ffurl_seek(h, pos, SEEK_SET);
312     }
313     return size;
314 }
315
316 int ffurl_get_file_handle(URLContext *h)
317 {
318     if (!h->prot->url_get_file_handle)
319         return -1;
320     return h->prot->url_get_file_handle(h);
321 }
322
323 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
324 {
325     if (!h->prot->url_get_multi_file_handle) {
326         if (!h->prot->url_get_file_handle)
327             return AVERROR(ENOSYS);
328         *handles = av_malloc(sizeof(**handles));
329         if (!*handles)
330             return AVERROR(ENOMEM);
331         *numhandles = 1;
332         *handles[0] = h->prot->url_get_file_handle(h);
333         return 0;
334     }
335     return h->prot->url_get_multi_file_handle(h, handles, numhandles);
336 }
337
338 int ffurl_shutdown(URLContext *h, int flags)
339 {
340     if (!h->prot->url_shutdown)
341         return AVERROR(EINVAL);
342     return h->prot->url_shutdown(h, flags);
343 }
344
345 int ff_check_interrupt(AVIOInterruptCB *cb)
346 {
347     int ret;
348     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
349         return ret;
350     return 0;
351 }