]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
Merge remote-tracking branch 'qatar/master'
[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 <unistd.h>
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/opt.h"
27 #include "os_support.h"
28 #include "avformat.h"
29 #if CONFIG_NETWORK
30 #include "network.h"
31 #endif
32 #include "url.h"
33
34 static URLProtocol *first_protocol = NULL;
35
36 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
37 {
38     return prev ? prev->next : first_protocol;
39 }
40
41 /** @name Logging context. */
42 /*@{*/
43 static const char *urlcontext_to_name(void *ptr)
44 {
45     URLContext *h = (URLContext *)ptr;
46     if(h->prot) return h->prot->name;
47     else        return "NULL";
48 }
49
50 static void *urlcontext_child_next(void *obj, void *prev)
51 {
52     URLContext *h = obj;
53     if (!prev && h->priv_data && h->prot->priv_data_class)
54         return h->priv_data;
55     return NULL;
56 }
57
58 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
59 {
60     URLProtocol *p = NULL;
61
62     /* find the protocol that corresponds to prev */
63     while (prev && (p = ffurl_protocol_next(p)))
64         if (p->priv_data_class == prev)
65             break;
66
67     /* find next protocol with priv options */
68     while (p = ffurl_protocol_next(p))
69         if (p->priv_data_class)
70             return p->priv_data_class;
71     return NULL;
72
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
87 #if FF_API_OLD_INTERRUPT_CB
88 static int default_interrupt_cb(void);
89 int (*url_interrupt_cb)(void) = default_interrupt_cb;
90 #endif
91
92 URLProtocol *av_protocol_next(URLProtocol *p)
93 {
94     return ffurl_protocol_next(p);
95 }
96
97 const char *avio_enum_protocols(void **opaque, int output)
98 {
99     URLProtocol **p = opaque;
100     *p = ffurl_protocol_next(*p);
101     if (!*p) return NULL;
102     if ((output && (*p)->url_write) || (!output && (*p)->url_read))
103         return (*p)->name;
104     return avio_enum_protocols(opaque, output);
105 }
106
107 int ffurl_register_protocol(URLProtocol *protocol, int size)
108 {
109     URLProtocol **p;
110     if (size < sizeof(URLProtocol)) {
111         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
112         memcpy(temp, protocol, size);
113         protocol = temp;
114     }
115     p = &first_protocol;
116     while (*p != NULL) p = &(*p)->next;
117     *p = protocol;
118     protocol->next = NULL;
119     return 0;
120 }
121
122 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
123                                    const char *filename, int flags,
124                                    const AVIOInterruptCB *int_cb)
125 {
126     URLContext *uc;
127     int err;
128
129 #if CONFIG_NETWORK
130     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
131         return AVERROR(EIO);
132 #endif
133     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
134     if (!uc) {
135         err = AVERROR(ENOMEM);
136         goto fail;
137     }
138     uc->av_class = &ffurl_context_class;
139     uc->filename = (char *) &uc[1];
140     strcpy(uc->filename, filename);
141     uc->prot = up;
142     uc->flags = flags;
143     uc->is_streamed = 0; /* default = not streamed */
144     uc->max_packet_size = 0; /* default: stream file */
145     if (up->priv_data_size) {
146         uc->priv_data = av_mallocz(up->priv_data_size);
147         if (up->priv_data_class) {
148             int proto_len= strlen(up->name);
149             char *start = strchr(uc->filename, ',');
150             *(const AVClass**)uc->priv_data = up->priv_data_class;
151             av_opt_set_defaults(uc->priv_data);
152             if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
153                 int ret= 0;
154                 char *p= start;
155                 char sep= *++p;
156                 char *key, *val;
157                 p++;
158                 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
159                     *val= *key= 0;
160                     ret= av_opt_set(uc->priv_data, p, key+1, 0);
161                     if (ret == AVERROR_OPTION_NOT_FOUND)
162                         av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
163                     *val= *key= sep;
164                     p= val+1;
165                 }
166                 if(ret<0 || p!=key){
167                     av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
168                     av_freep(&uc->priv_data);
169                     av_freep(&uc);
170                     goto fail;
171                 }
172                 memmove(start, key+1, strlen(key));
173             }
174         }
175     }
176     if (int_cb)
177         uc->interrupt_callback = *int_cb;
178
179     *puc = uc;
180     return 0;
181  fail:
182     *puc = NULL;
183 #if CONFIG_NETWORK
184     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
185         ff_network_close();
186 #endif
187     return err;
188 }
189
190 int ffurl_connect(URLContext* uc, AVDictionary **options)
191 {
192     int err =
193 #if !FF_API_OLD_AVIO
194         uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
195 #endif
196         uc->prot->url_open(uc, uc->filename, uc->flags);
197     if (err)
198         return err;
199     uc->is_connected = 1;
200     //We must be careful here as ffurl_seek() could be slow, for example for http
201     if(   (uc->flags & AVIO_FLAG_WRITE)
202        || !strcmp(uc->prot->name, "file"))
203         if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
204             uc->is_streamed= 1;
205     return 0;
206 }
207
208 #if FF_API_OLD_AVIO
209 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
210                        const char *filename, int flags)
211 {
212     int ret;
213
214     ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
215     if (ret)
216         goto fail;
217     ret = ffurl_connect(*puc, NULL);
218     if (!ret)
219         return 0;
220  fail:
221     ffurl_close(*puc);
222     *puc = NULL;
223     return ret;
224 }
225 int url_alloc(URLContext **puc, const char *filename, int flags)
226 {
227     return ffurl_alloc(puc, filename, flags, NULL);
228 }
229 int url_connect(URLContext* uc)
230 {
231     return ffurl_connect(uc, NULL);
232 }
233 int url_open(URLContext **puc, const char *filename, int flags)
234 {
235     return ffurl_open(puc, filename, flags, NULL, NULL);
236 }
237 int url_read(URLContext *h, unsigned char *buf, int size)
238 {
239     return ffurl_read(h, buf, size);
240 }
241 int url_read_complete(URLContext *h, unsigned char *buf, int size)
242 {
243     return ffurl_read_complete(h, buf, size);
244 }
245 int url_write(URLContext *h, const unsigned char *buf, int size)
246 {
247     return ffurl_write(h, buf, size);
248 }
249 int64_t url_seek(URLContext *h, int64_t pos, int whence)
250 {
251     return ffurl_seek(h, pos, whence);
252 }
253 int url_close(URLContext *h)
254 {
255     return ffurl_close(h);
256 }
257 int64_t url_filesize(URLContext *h)
258 {
259     return ffurl_size(h);
260 }
261 int url_get_file_handle(URLContext *h)
262 {
263     return ffurl_get_file_handle(h);
264 }
265 int url_get_max_packet_size(URLContext *h)
266 {
267     return h->max_packet_size;
268 }
269 void url_get_filename(URLContext *h, char *buf, int buf_size)
270 {
271     av_strlcpy(buf, h->filename, buf_size);
272 }
273 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
274 {
275     avio_set_interrupt_cb(interrupt_cb);
276 }
277 int av_register_protocol2(URLProtocol *protocol, int size)
278 {
279     return ffurl_register_protocol(protocol, size);
280 }
281 #endif
282
283 #define URL_SCHEME_CHARS                        \
284     "abcdefghijklmnopqrstuvwxyz"                \
285     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
286     "0123456789+-."
287
288 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
289                 const AVIOInterruptCB *int_cb)
290 {
291     URLProtocol *up = NULL;
292     char proto_str[128], proto_nested[128], *ptr;
293     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
294
295     if (!first_protocol) {
296         av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
297                                      "Missing call to av_register_all()?\n");
298     }
299
300     if (filename[proto_len] != ':' &&  filename[proto_len] != ',' || is_dos_path(filename))
301         strcpy(proto_str, "file");
302     else
303         av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
304
305     if ((ptr = strchr(proto_str, ',')))
306         *ptr = '\0';
307     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
308     if ((ptr = strchr(proto_nested, '+')))
309         *ptr = '\0';
310
311     while (up = ffurl_protocol_next(up)) {
312         if (!strcmp(proto_str, up->name))
313             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
314         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
315             !strcmp(proto_nested, up->name))
316             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
317     }
318     *puc = NULL;
319     return AVERROR(ENOENT);
320 }
321
322 int ffurl_open(URLContext **puc, const char *filename, int flags,
323                const AVIOInterruptCB *int_cb, AVDictionary **options)
324 {
325     int ret = ffurl_alloc(puc, filename, flags, int_cb);
326     if (ret)
327         return ret;
328     if (options && (*puc)->prot->priv_data_class &&
329         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
330         goto fail;
331     ret = ffurl_connect(*puc, options);
332     if (!ret)
333         return 0;
334 fail:
335     ffurl_close(*puc);
336     *puc = NULL;
337     return ret;
338 }
339
340 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
341                                          int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
342 {
343     int ret, len;
344     int fast_retries = 5;
345
346     len = 0;
347     while (len < size_min) {
348         ret = transfer_func(h, buf+len, size-len);
349         if (ret == AVERROR(EINTR))
350             continue;
351         if (h->flags & AVIO_FLAG_NONBLOCK)
352             return ret;
353         if (ret == AVERROR(EAGAIN)) {
354             ret = 0;
355             if (fast_retries)
356                 fast_retries--;
357             else
358                 usleep(1000);
359         } else if (ret < 1)
360             return ret < 0 ? ret : len;
361         if (ret)
362            fast_retries = FFMAX(fast_retries, 2);
363         len += ret;
364         if (len < size && ff_check_interrupt(&h->interrupt_callback))
365             return AVERROR_EXIT;
366     }
367     return len;
368 }
369
370 int ffurl_read(URLContext *h, unsigned char *buf, int size)
371 {
372     if (!(h->flags & AVIO_FLAG_READ))
373         return AVERROR(EIO);
374     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
375 }
376
377 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
378 {
379     if (!(h->flags & AVIO_FLAG_READ))
380         return AVERROR(EIO);
381     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
382 }
383
384 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
385 {
386     if (!(h->flags & AVIO_FLAG_WRITE))
387         return AVERROR(EIO);
388     /* avoid sending too big packets */
389     if (h->max_packet_size && size > h->max_packet_size)
390         return AVERROR(EIO);
391
392     return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
393 }
394
395 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
396 {
397     int64_t ret;
398
399     if (!h->prot->url_seek)
400         return AVERROR(ENOSYS);
401     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
402     return ret;
403 }
404
405 int ffurl_close(URLContext *h)
406 {
407     int ret = 0;
408     if (!h) return 0; /* can happen when ffurl_open fails */
409
410     if (h->is_connected && h->prot->url_close)
411         ret = h->prot->url_close(h);
412 #if CONFIG_NETWORK
413     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
414         ff_network_close();
415 #endif
416     if (h->prot->priv_data_size) {
417         if (h->prot->priv_data_class)
418             av_opt_free(h->priv_data);
419         av_free(h->priv_data);
420     }
421     av_free(h);
422     return ret;
423 }
424
425 #if FF_API_OLD_AVIO
426 int url_exist(const char *filename)
427 {
428     URLContext *h;
429     if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
430         return 0;
431     ffurl_close(h);
432     return 1;
433 }
434 #endif
435
436 int avio_check(const char *url, int flags)
437 {
438     URLContext *h;
439     int ret = ffurl_alloc(&h, url, flags, NULL);
440     if (ret)
441         return ret;
442
443     if (h->prot->url_check) {
444         ret = h->prot->url_check(h, flags);
445     } else {
446         ret = ffurl_connect(h, NULL);
447         if (ret >= 0)
448             ret = flags;
449     }
450
451     ffurl_close(h);
452     return ret;
453 }
454
455 int64_t ffurl_size(URLContext *h)
456 {
457     int64_t pos, size;
458
459     size= ffurl_seek(h, 0, AVSEEK_SIZE);
460     if(size<0){
461         pos = ffurl_seek(h, 0, SEEK_CUR);
462         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
463             return size;
464         size++;
465         ffurl_seek(h, pos, SEEK_SET);
466     }
467     return size;
468 }
469
470 int ffurl_get_file_handle(URLContext *h)
471 {
472     if (!h->prot->url_get_file_handle)
473         return -1;
474     return h->prot->url_get_file_handle(h);
475 }
476
477 #if FF_API_OLD_INTERRUPT_CB
478 static int default_interrupt_cb(void)
479 {
480     return 0;
481 }
482
483 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
484 {
485     if (!interrupt_cb)
486         interrupt_cb = default_interrupt_cb;
487     url_interrupt_cb = interrupt_cb;
488 }
489 #endif
490
491 int ff_check_interrupt(AVIOInterruptCB *cb)
492 {
493     int ret;
494     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
495         return ret;
496 #if FF_API_OLD_INTERRUPT_CB
497     return url_interrupt_cb();
498 #else
499     return 0;
500 #endif
501 }
502
503 #if FF_API_OLD_AVIO
504 int av_url_read_pause(URLContext *h, int pause)
505 {
506     if (!h->prot->url_read_pause)
507         return AVERROR(ENOSYS);
508     return h->prot->url_read_pause(h, pause);
509 }
510
511 int64_t av_url_read_seek(URLContext *h,
512         int stream_index, int64_t timestamp, int flags)
513 {
514     if (!h->prot->url_read_seek)
515         return AVERROR(ENOSYS);
516     return h->prot->url_read_seek(h, stream_index, timestamp, flags);
517 }
518 #endif