]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
lavf: move avio_enum_protocols() to protocols.c
[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 AVClass *urlcontext_child_class_next(const AVClass *prev)
53 {
54     int i;
55
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) {
59             i++;
60             break;
61         }
62     }
63
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;
68     return NULL;
69 }
70
71 static const AVOption options[] = { { NULL } };
72 const AVClass ffurl_context_class = {
73     .class_name       = "URLContext",
74     .item_name        = urlcontext_to_name,
75     .option           = options,
76     .version          = LIBAVUTIL_VERSION_INT,
77     .child_next       = urlcontext_child_next,
78     .child_class_next = urlcontext_child_class_next,
79 };
80 /*@}*/
81
82 static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
83                                   const char *filename, int flags,
84                                   const AVIOInterruptCB *int_cb)
85 {
86     URLContext *uc;
87     int err;
88
89 #if CONFIG_NETWORK
90     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
91         return AVERROR(EIO);
92 #endif
93     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
94     if (!uc) {
95         err = AVERROR(ENOMEM);
96         goto fail;
97     }
98     uc->av_class = &ffurl_context_class;
99     uc->filename = (char *)&uc[1];
100     strcpy(uc->filename, filename);
101     uc->prot            = up;
102     uc->flags           = flags;
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);
109             goto fail;
110         }
111         if (up->priv_data_class) {
112             *(const AVClass **)uc->priv_data = up->priv_data_class;
113             av_opt_set_defaults(uc->priv_data);
114         }
115     }
116     if (int_cb)
117         uc->interrupt_callback = *int_cb;
118
119     *puc = uc;
120     return 0;
121 fail:
122     *puc = NULL;
123     if (uc)
124         av_freep(&uc->priv_data);
125     av_freep(&uc);
126 #if CONFIG_NETWORK
127     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
128         ff_network_close();
129 #endif
130     return err;
131 }
132
133 int ffurl_connect(URLContext *uc, AVDictionary **options)
134 {
135     int err =
136         uc->prot->url_open2 ? uc->prot->url_open2(uc,
137                                                   uc->filename,
138                                                   uc->flags,
139                                                   options) :
140         uc->prot->url_open(uc, uc->filename, uc->flags);
141     if (err)
142         return err;
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)
148             uc->is_streamed = 1;
149     return 0;
150 }
151
152 #define URL_SCHEME_CHARS                        \
153     "abcdefghijklmnopqrstuvwxyz"                \
154     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
155     "0123456789+-."
156
157 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
158                 const AVIOInterruptCB *int_cb)
159 {
160     char proto_str[128], proto_nested[128], *ptr;
161     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
162     int i;
163
164     if (filename[proto_len] != ':' || is_dos_path(filename))
165         strcpy(proto_str, "file");
166     else
167         av_strlcpy(proto_str, filename,
168                    FFMIN(proto_len + 1, sizeof(proto_str)));
169
170     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
171     if ((ptr = strchr(proto_nested, '+')))
172         *ptr = '\0';
173
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);
181     }
182     *puc = NULL;
183     return AVERROR_PROTOCOL_NOT_FOUND;
184 }
185
186 int ffurl_open(URLContext **puc, const char *filename, int flags,
187                const AVIOInterruptCB *int_cb, AVDictionary **options)
188 {
189     int ret = ffurl_alloc(puc, filename, flags, int_cb);
190     if (ret)
191         return ret;
192     if (options && (*puc)->prot->priv_data_class &&
193         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
194         goto fail;
195     ret = ffurl_connect(*puc, options);
196     if (!ret)
197         return 0;
198 fail:
199     ffurl_close(*puc);
200     *puc = NULL;
201     return ret;
202 }
203
204 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
205                                          int size, int size_min,
206                                          int (*transfer_func)(URLContext *h,
207                                                               uint8_t *buf,
208                                                               int size))
209 {
210     int ret, len;
211     int fast_retries = 5;
212
213     len = 0;
214     while (len < size_min) {
215         ret = transfer_func(h, buf + len, size - len);
216         if (ret == AVERROR(EINTR))
217             continue;
218         if (h->flags & AVIO_FLAG_NONBLOCK)
219             return ret;
220         if (ret == AVERROR(EAGAIN)) {
221             ret = 0;
222             if (fast_retries)
223                 fast_retries--;
224             else
225                 av_usleep(1000);
226         } else if (ret < 1)
227             return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
228         if (ret)
229             fast_retries = FFMAX(fast_retries, 2);
230         len += ret;
231         if (ff_check_interrupt(&h->interrupt_callback))
232             return AVERROR_EXIT;
233     }
234     return len;
235 }
236
237 int ffurl_read(URLContext *h, unsigned char *buf, int size)
238 {
239     if (!(h->flags & AVIO_FLAG_READ))
240         return AVERROR(EIO);
241     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
242 }
243
244 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
245 {
246     if (!(h->flags & AVIO_FLAG_READ))
247         return AVERROR(EIO);
248     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
249 }
250
251 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
252 {
253     if (!(h->flags & AVIO_FLAG_WRITE))
254         return AVERROR(EIO);
255     /* avoid sending too big packets */
256     if (h->max_packet_size && size > h->max_packet_size)
257         return AVERROR(EIO);
258
259     return retry_transfer_wrapper(h, buf, size, size,
260                                   (int (*)(struct URLContext *, uint8_t *, int))
261                                   h->prot->url_write);
262 }
263
264 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
265 {
266     int64_t ret;
267
268     if (!h->prot->url_seek)
269         return AVERROR(ENOSYS);
270     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
271     return ret;
272 }
273
274 int ffurl_close(URLContext *h)
275 {
276     int ret = 0;
277     if (!h)
278         return 0;     /* can happen when ffurl_open fails */
279
280     if (h->is_connected && h->prot->url_close)
281         ret = h->prot->url_close(h);
282 #if CONFIG_NETWORK
283     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
284         ff_network_close();
285 #endif
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);
290     }
291     av_free(h);
292     return ret;
293 }
294
295 int avio_check(const char *url, int flags)
296 {
297     URLContext *h;
298     int ret = ffurl_alloc(&h, url, flags, NULL);
299     if (ret)
300         return ret;
301
302     if (h->prot->url_check) {
303         ret = h->prot->url_check(h, flags);
304     } else {
305         ret = ffurl_connect(h, NULL);
306         if (ret >= 0)
307             ret = flags;
308     }
309
310     ffurl_close(h);
311     return ret;
312 }
313
314 int64_t ffurl_size(URLContext *h)
315 {
316     int64_t pos, size;
317
318     size = ffurl_seek(h, 0, AVSEEK_SIZE);
319     if (size < 0) {
320         pos = ffurl_seek(h, 0, SEEK_CUR);
321         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
322             return size;
323         size++;
324         ffurl_seek(h, pos, SEEK_SET);
325     }
326     return size;
327 }
328
329 int ffurl_get_file_handle(URLContext *h)
330 {
331     if (!h->prot->url_get_file_handle)
332         return -1;
333     return h->prot->url_get_file_handle(h);
334 }
335
336 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
337 {
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));
342         if (!*handles)
343             return AVERROR(ENOMEM);
344         *numhandles = 1;
345         *handles[0] = h->prot->url_get_file_handle(h);
346         return 0;
347     }
348     return h->prot->url_get_multi_file_handle(h, handles, numhandles);
349 }
350
351 int ffurl_shutdown(URLContext *h, int flags)
352 {
353     if (!h->prot->url_shutdown)
354         return AVERROR(EINVAL);
355     return h->prot->url_shutdown(h, flags);
356 }
357
358 int ff_check_interrupt(AVIOInterruptCB *cb)
359 {
360     int ret;
361     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
362         return ret;
363     return 0;
364 }