]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
Merge commit '2ba68dd044ca8fc591139c05563840f546a9c0c0'
[ffmpeg] / libavformat / avio.c
1 /*
2  * unbuffered I/O
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 static URLProtocol *first_protocol = NULL;
34
35 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
36 {
37     return prev ? prev->next : first_protocol;
38 }
39
40 /** @name Logging context. */
41 /*@{*/
42 static const char *urlcontext_to_name(void *ptr)
43 {
44     URLContext *h = (URLContext *)ptr;
45     if (h->prot)
46         return h->prot->name;
47     else
48         return "NULL";
49 }
50
51 static void *urlcontext_child_next(void *obj, void *prev)
52 {
53     URLContext *h = obj;
54     if (!prev && h->priv_data && h->prot->priv_data_class)
55         return h->priv_data;
56     return NULL;
57 }
58
59 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
60 {
61     URLProtocol *p = NULL;
62
63     /* find the protocol that corresponds to prev */
64     while (prev && (p = ffurl_protocol_next(p)))
65         if (p->priv_data_class == prev)
66             break;
67
68     /* find next protocol with priv options */
69     while (p = ffurl_protocol_next(p))
70         if (p->priv_data_class)
71             return p->priv_data_class;
72     return NULL;
73 }
74
75 static const AVOption options[] = { { NULL } };
76 const AVClass ffurl_context_class = {
77     .class_name       = "URLContext",
78     .item_name        = urlcontext_to_name,
79     .option           = options,
80     .version          = LIBAVUTIL_VERSION_INT,
81     .child_next       = urlcontext_child_next,
82     .child_class_next = urlcontext_child_class_next,
83 };
84 /*@}*/
85
86 const char *avio_enum_protocols(void **opaque, int output)
87 {
88     URLProtocol *p;
89     *opaque = ffurl_protocol_next(*opaque);
90     if (!(p = *opaque))
91         return NULL;
92     if ((output && p->url_write) || (!output && p->url_read))
93         return p->name;
94     return avio_enum_protocols(opaque, output);
95 }
96
97 int ffurl_register_protocol(URLProtocol *protocol, int size)
98 {
99     URLProtocol **p;
100     if (size < sizeof(URLProtocol)) {
101         URLProtocol *temp = av_mallocz(sizeof(URLProtocol));
102         memcpy(temp, protocol, size);
103         protocol = temp;
104     }
105     p = &first_protocol;
106     while (*p != NULL)
107         p = &(*p)->next;
108     *p             = protocol;
109     protocol->next = NULL;
110     return 0;
111 }
112
113 static int url_alloc_for_protocol(URLContext **puc, struct URLProtocol *up,
114                                   const char *filename, int flags,
115                                   const AVIOInterruptCB *int_cb)
116 {
117     URLContext *uc;
118     int err;
119
120 #if CONFIG_NETWORK
121     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
122         return AVERROR(EIO);
123 #endif
124     if ((flags & AVIO_FLAG_READ) && !up->url_read) {
125         av_log(NULL, AV_LOG_ERROR,
126                "Impossible to open the '%s' protocol for reading\n", up->name);
127         return AVERROR(EIO);
128     }
129     if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
130         av_log(NULL, AV_LOG_ERROR,
131                "Impossible to open the '%s' protocol for writing\n", up->name);
132         return AVERROR(EIO);
133     }
134     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
135     if (!uc) {
136         err = AVERROR(ENOMEM);
137         goto fail;
138     }
139     uc->av_class = &ffurl_context_class;
140     uc->filename = (char *)&uc[1];
141     strcpy(uc->filename, filename);
142     uc->prot            = up;
143     uc->flags           = flags;
144     uc->is_streamed     = 0; /* default = not streamed */
145     uc->max_packet_size = 0; /* default: stream file */
146     if (up->priv_data_size) {
147         uc->priv_data = av_mallocz(up->priv_data_size);
148         if (!uc->priv_data) {
149             err = AVERROR(ENOMEM);
150             goto fail;
151         }
152         if (up->priv_data_class) {
153             int proto_len= strlen(up->name);
154             char *start = strchr(uc->filename, ',');
155             *(const AVClass **)uc->priv_data = up->priv_data_class;
156             av_opt_set_defaults(uc->priv_data);
157             if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
158                 int ret= 0;
159                 char *p= start;
160                 char sep= *++p;
161                 char *key, *val;
162                 p++;
163                 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
164                     *val= *key= 0;
165                     ret= av_opt_set(uc->priv_data, p, key+1, 0);
166                     if (ret == AVERROR_OPTION_NOT_FOUND)
167                         av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
168                     *val= *key= sep;
169                     p= val+1;
170                 }
171                 if(ret<0 || p!=key){
172                     av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
173                     av_freep(&uc->priv_data);
174                     av_freep(&uc);
175                     err = AVERROR(EINVAL);
176                     goto fail;
177                 }
178                 memmove(start, key+1, strlen(key));
179             }
180         }
181     }
182     if (int_cb)
183         uc->interrupt_callback = *int_cb;
184
185     *puc = uc;
186     return 0;
187 fail:
188     *puc = NULL;
189     if (uc)
190         av_freep(&uc->priv_data);
191     av_freep(&uc);
192 #if CONFIG_NETWORK
193     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
194         ff_network_close();
195 #endif
196     return err;
197 }
198
199 int ffurl_connect(URLContext *uc, AVDictionary **options)
200 {
201     int err =
202         uc->prot->url_open2 ? uc->prot->url_open2(uc,
203                                                   uc->filename,
204                                                   uc->flags,
205                                                   options) :
206         uc->prot->url_open(uc, uc->filename, uc->flags);
207     if (err)
208         return err;
209     uc->is_connected = 1;
210     /* We must be careful here as ffurl_seek() could be slow,
211      * for example for http */
212     if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
213         if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
214             uc->is_streamed = 1;
215     return 0;
216 }
217
218 #define URL_SCHEME_CHARS                        \
219     "abcdefghijklmnopqrstuvwxyz"                \
220     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
221     "0123456789+-."
222
223 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
224                 const AVIOInterruptCB *int_cb)
225 {
226     URLProtocol *up = NULL;
227     char proto_str[128], proto_nested[128], *ptr;
228     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
229
230     if (!first_protocol) {
231         av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
232                                      "Missing call to av_register_all()?\n");
233     }
234
235     if (filename[proto_len] != ':' &&
236         (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
237         is_dos_path(filename))
238         strcpy(proto_str, "file");
239     else
240         av_strlcpy(proto_str, filename,
241                    FFMIN(proto_len + 1, sizeof(proto_str)));
242
243     if ((ptr = strchr(proto_str, ',')))
244         *ptr = '\0';
245     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
246     if ((ptr = strchr(proto_nested, '+')))
247         *ptr = '\0';
248
249     while (up = ffurl_protocol_next(up)) {
250         if (!strcmp(proto_str, up->name))
251             return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
252         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
253             !strcmp(proto_nested, up->name))
254             return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
255     }
256     *puc = NULL;
257     if (!strcmp("https", proto_str))
258         av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
259     return AVERROR_PROTOCOL_NOT_FOUND;
260 }
261
262 int ffurl_open(URLContext **puc, const char *filename, int flags,
263                const AVIOInterruptCB *int_cb, AVDictionary **options)
264 {
265     int ret = ffurl_alloc(puc, filename, flags, int_cb);
266     if (ret)
267         return ret;
268     if (options && (*puc)->prot->priv_data_class &&
269         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
270         goto fail;
271     ret = ffurl_connect(*puc, options);
272     if (!ret)
273         return 0;
274 fail:
275     ffurl_close(*puc);
276     *puc = NULL;
277     return ret;
278 }
279
280 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
281                                          int size, int size_min,
282                                          int (*transfer_func)(URLContext *h,
283                                                               uint8_t *buf,
284                                                               int size))
285 {
286     int ret, len;
287     int fast_retries = 5;
288     int64_t wait_since = 0;
289
290     len = 0;
291     while (len < size_min) {
292         if (ff_check_interrupt(&h->interrupt_callback))
293             return AVERROR_EXIT;
294         ret = transfer_func(h, buf + len, size - len);
295         if (ret == AVERROR(EINTR))
296             continue;
297         if (h->flags & AVIO_FLAG_NONBLOCK)
298             return ret;
299         if (ret == AVERROR(EAGAIN)) {
300             ret = 0;
301             if (fast_retries) {
302                 fast_retries--;
303             } else {
304                 if (h->rw_timeout) {
305                     if (!wait_since)
306                         wait_since = av_gettime();
307                     else if (av_gettime() > wait_since + h->rw_timeout)
308                         return AVERROR(EIO);
309                 }
310                 av_usleep(1000);
311             }
312         } else if (ret < 1)
313             return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
314         if (ret)
315             fast_retries = FFMAX(fast_retries, 2);
316         len += ret;
317     }
318     return len;
319 }
320
321 int ffurl_read(URLContext *h, unsigned char *buf, int size)
322 {
323     if (!(h->flags & AVIO_FLAG_READ))
324         return AVERROR(EIO);
325     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
326 }
327
328 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
329 {
330     if (!(h->flags & AVIO_FLAG_READ))
331         return AVERROR(EIO);
332     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
333 }
334
335 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
336 {
337     if (!(h->flags & AVIO_FLAG_WRITE))
338         return AVERROR(EIO);
339     /* avoid sending too big packets */
340     if (h->max_packet_size && size > h->max_packet_size)
341         return AVERROR(EIO);
342
343     return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
344 }
345
346 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
347 {
348     int64_t ret;
349
350     if (!h->prot->url_seek)
351         return AVERROR(ENOSYS);
352     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
353     return ret;
354 }
355
356 int ffurl_closep(URLContext **hh)
357 {
358     URLContext *h= *hh;
359     int ret = 0;
360     if (!h)
361         return 0;     /* can happen when ffurl_open fails */
362
363     if (h->is_connected && h->prot->url_close)
364         ret = h->prot->url_close(h);
365 #if CONFIG_NETWORK
366     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
367         ff_network_close();
368 #endif
369     if (h->prot->priv_data_size) {
370         if (h->prot->priv_data_class)
371             av_opt_free(h->priv_data);
372         av_freep(&h->priv_data);
373     }
374     av_freep(hh);
375     return ret;
376 }
377
378 int ffurl_close(URLContext *h)
379 {
380     return ffurl_closep(&h);
381 }
382
383
384 int avio_check(const char *url, int flags)
385 {
386     URLContext *h;
387     int ret = ffurl_alloc(&h, url, flags, NULL);
388     if (ret)
389         return ret;
390
391     if (h->prot->url_check) {
392         ret = h->prot->url_check(h, flags);
393     } else {
394         ret = ffurl_connect(h, NULL);
395         if (ret >= 0)
396             ret = flags;
397     }
398
399     ffurl_close(h);
400     return ret;
401 }
402
403 int64_t ffurl_size(URLContext *h)
404 {
405     int64_t pos, size;
406
407     size = ffurl_seek(h, 0, AVSEEK_SIZE);
408     if (size < 0) {
409         pos = ffurl_seek(h, 0, SEEK_CUR);
410         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
411             return size;
412         size++;
413         ffurl_seek(h, pos, SEEK_SET);
414     }
415     return size;
416 }
417
418 int ffurl_get_file_handle(URLContext *h)
419 {
420     if (!h->prot->url_get_file_handle)
421         return -1;
422     return h->prot->url_get_file_handle(h);
423 }
424
425 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
426 {
427     if (!h->prot->url_get_multi_file_handle) {
428         if (!h->prot->url_get_file_handle)
429             return AVERROR(ENOSYS);
430         *handles = av_malloc(sizeof(**handles));
431         if (!*handles)
432             return AVERROR(ENOMEM);
433         *numhandles = 1;
434         *handles[0] = h->prot->url_get_file_handle(h);
435         return 0;
436     }
437     return h->prot->url_get_multi_file_handle(h, handles, numhandles);
438 }
439
440 int ffurl_shutdown(URLContext *h, int flags)
441 {
442     if (!h->prot->url_shutdown)
443         return AVERROR(EINVAL);
444     return h->prot->url_shutdown(h, flags);
445 }
446
447 int ff_check_interrupt(AVIOInterruptCB *cb)
448 {
449     int ret;
450     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
451         return ret;
452     return 0;
453 }