]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
4da6b7416504787df7fd30e53822f3ae1301750c
[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                                   const URLProtocol **protocols)
67 {
68     URLContext *uc;
69     int err;
70
71 #if CONFIG_NETWORK
72     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
73         return AVERROR(EIO);
74 #endif
75     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
76     if (!uc) {
77         err = AVERROR(ENOMEM);
78         goto fail;
79     }
80     uc->av_class = &ffurl_context_class;
81     uc->filename = (char *)&uc[1];
82     strcpy(uc->filename, filename);
83     uc->prot            = up;
84     uc->flags           = flags;
85     uc->is_streamed     = 0; /* default = not streamed */
86     uc->max_packet_size = 0; /* default: stream file */
87     uc->protocols       = protocols;
88     if (up->priv_data_size) {
89         uc->priv_data = av_mallocz(up->priv_data_size);
90         if (!uc->priv_data) {
91             err = AVERROR(ENOMEM);
92             goto fail;
93         }
94         if (up->priv_data_class) {
95             *(const AVClass **)uc->priv_data = up->priv_data_class;
96             av_opt_set_defaults(uc->priv_data);
97         }
98     }
99     if (int_cb)
100         uc->interrupt_callback = *int_cb;
101
102     *puc = uc;
103     return 0;
104 fail:
105     *puc = NULL;
106     if (uc)
107         av_freep(&uc->priv_data);
108     av_freep(&uc);
109 #if CONFIG_NETWORK
110     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
111         ff_network_close();
112 #endif
113     return err;
114 }
115
116 int ffurl_connect(URLContext *uc, AVDictionary **options)
117 {
118     int err =
119         uc->prot->url_open2 ? uc->prot->url_open2(uc,
120                                                   uc->filename,
121                                                   uc->flags,
122                                                   options) :
123         uc->prot->url_open(uc, uc->filename, uc->flags);
124     if (err)
125         return err;
126     uc->is_connected = 1;
127     /* We must be careful here as ffurl_seek() could be slow,
128      * for example for http */
129     if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
130         if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
131             uc->is_streamed = 1;
132     return 0;
133 }
134
135 #define URL_SCHEME_CHARS                        \
136     "abcdefghijklmnopqrstuvwxyz"                \
137     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
138     "0123456789+-."
139
140 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
141                 const AVIOInterruptCB *int_cb,
142                 const URLProtocol **protocols)
143 {
144     char proto_str[128], proto_nested[128], *ptr;
145     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
146     int i;
147
148     if (filename[proto_len] != ':' || is_dos_path(filename))
149         strcpy(proto_str, "file");
150     else
151         av_strlcpy(proto_str, filename,
152                    FFMIN(proto_len + 1, sizeof(proto_str)));
153
154     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
155     if ((ptr = strchr(proto_nested, '+')))
156         *ptr = '\0';
157
158     for (i = 0; protocols[i]; i++) {
159         const URLProtocol *up = protocols[i];
160         if (!strcmp(proto_str, up->name))
161             return url_alloc_for_protocol(puc, up, filename, flags, int_cb,
162                                           protocols);
163         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
164             !strcmp(proto_nested, up->name))
165             return url_alloc_for_protocol(puc, up, filename, flags, int_cb,
166                                           protocols);
167     }
168     *puc = NULL;
169     return AVERROR_PROTOCOL_NOT_FOUND;
170 }
171
172 int ffurl_open(URLContext **puc, const char *filename, int flags,
173                const AVIOInterruptCB *int_cb, AVDictionary **options,
174                const URLProtocol **protocols)
175 {
176     int ret = ffurl_alloc(puc, filename, flags, int_cb, protocols);
177     if (ret)
178         return ret;
179     if (options &&
180         (ret = av_opt_set_dict(*puc, options)) < 0)
181         goto fail;
182     if (options && (*puc)->prot->priv_data_class &&
183         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
184         goto fail;
185     ret = ffurl_connect(*puc, options);
186     if (!ret)
187         return 0;
188 fail:
189     ffurl_close(*puc);
190     *puc = NULL;
191     return ret;
192 }
193
194 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
195                                          int size, int size_min,
196                                          int (*transfer_func)(URLContext *h,
197                                                               uint8_t *buf,
198                                                               int size))
199 {
200     int ret, len;
201     int fast_retries = 5;
202
203     len = 0;
204     while (len < size_min) {
205         ret = transfer_func(h, buf + len, size - len);
206         if (ret == AVERROR(EINTR))
207             continue;
208         if (h->flags & AVIO_FLAG_NONBLOCK)
209             return ret;
210         if (ret == AVERROR(EAGAIN)) {
211             ret = 0;
212             if (fast_retries)
213                 fast_retries--;
214             else
215                 av_usleep(1000);
216         } else if (ret < 1)
217             return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
218         if (ret)
219             fast_retries = FFMAX(fast_retries, 2);
220         len += ret;
221         if (ff_check_interrupt(&h->interrupt_callback))
222             return AVERROR_EXIT;
223     }
224     return len;
225 }
226
227 int ffurl_read(URLContext *h, unsigned char *buf, int size)
228 {
229     if (!(h->flags & AVIO_FLAG_READ))
230         return AVERROR(EIO);
231     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
232 }
233
234 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
235 {
236     if (!(h->flags & AVIO_FLAG_READ))
237         return AVERROR(EIO);
238     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
239 }
240
241 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
242 {
243     if (!(h->flags & AVIO_FLAG_WRITE))
244         return AVERROR(EIO);
245     /* avoid sending too big packets */
246     if (h->max_packet_size && size > h->max_packet_size)
247         return AVERROR(EIO);
248
249     return retry_transfer_wrapper(h, buf, size, size,
250                                   (int (*)(struct URLContext *, uint8_t *, int))
251                                   h->prot->url_write);
252 }
253
254 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
255 {
256     int64_t ret;
257
258     if (!h->prot->url_seek)
259         return AVERROR(ENOSYS);
260     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
261     return ret;
262 }
263
264 int ffurl_close(URLContext *h)
265 {
266     int ret = 0;
267     if (!h)
268         return 0;     /* can happen when ffurl_open fails */
269
270     if (h->is_connected && h->prot->url_close)
271         ret = h->prot->url_close(h);
272 #if CONFIG_NETWORK
273     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
274         ff_network_close();
275 #endif
276     if (h->prot->priv_data_size) {
277         if (h->prot->priv_data_class)
278             av_opt_free(h->priv_data);
279         av_free(h->priv_data);
280     }
281     av_free(h);
282     return ret;
283 }
284
285 int avio_check(const char *url, int flags)
286 {
287     const URLProtocol **protocols;
288     URLContext *h;
289     int ret;
290
291     protocols = ffurl_get_protocols(NULL, NULL);
292     if (!protocols)
293         return AVERROR(ENOMEM);
294
295     ret = ffurl_alloc(&h, url, flags, NULL, protocols);
296     if (ret) {
297         av_freep(&protocols);
298         return ret;
299     }
300
301     if (h->prot->url_check) {
302         ret = h->prot->url_check(h, flags);
303     } else {
304         ret = ffurl_connect(h, NULL);
305         if (ret >= 0)
306             ret = flags;
307     }
308
309     ffurl_close(h);
310     av_freep(&protocols);
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 }