libavformat/avio.c
de6d9b64
 /*
124e2884
  * unbuffered I/O
19720f15
  * Copyright (c) 2001 Fabrice Bellard
de6d9b64
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
19720f15
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
de6d9b64
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
de6d9b64
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
de6d9b64
  *
19720f15
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64
  */
245976da
 
 #include "libavutil/avstring.h"
ddffc2fd
 #include "libavutil/dict.h"
6ed04040
 #include "libavutil/opt.h"
896bb0d7
 #include "libavutil/time.h"
bc574408
 #include "os_support.h"
de6d9b64
 #include "avformat.h"
67d4b3f2
 #if CONFIG_NETWORK
 #include "network.h"
 #endif
5652bb94
 #include "url.h"
5acef35f
 
163a3113
 static URLProtocol *first_protocol = NULL;
 
 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
 {
     return prev ? prev->next : first_protocol;
 }
 
5acef35f
 /** @name Logging context. */
 /*@{*/
 static const char *urlcontext_to_name(void *ptr)
 {
     URLContext *h = (URLContext *)ptr;
53151723
     if (h->prot)
         return h->prot->name;
     else
         return "NULL";
5acef35f
 }
ddffc2fd
 
 static void *urlcontext_child_next(void *obj, void *prev)
 {
     URLContext *h = obj;
     if (!prev && h->priv_data && h->prot->priv_data_class)
         return h->priv_data;
     return NULL;
 }
 
 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
 {
     URLProtocol *p = NULL;
 
     /* find the protocol that corresponds to prev */
     while (prev && (p = ffurl_protocol_next(p)))
         if (p->priv_data_class == prev)
             break;
 
     /* find next protocol with priv options */
     while (p = ffurl_protocol_next(p))
         if (p->priv_data_class)
             return p->priv_data_class;
     return NULL;
 }
 
53151723
 static const AVOption options[] = { { NULL } };
1dee0aca
 const AVClass ffurl_context_class = {
53151723
     .class_name       = "URLContext",
     .item_name        = urlcontext_to_name,
     .option           = options,
     .version          = LIBAVUTIL_VERSION_INT,
     .child_next       = urlcontext_child_next,
ddffc2fd
     .child_class_next = urlcontext_child_class_next,
4a7a1b7d
 };
5acef35f
 /*@}*/
de6d9b64
 
f8270bbf
 const char *avio_enum_protocols(void **opaque, int output)
 {
4521645b
     URLProtocol *p;
     *opaque = ffurl_protocol_next(*opaque);
53151723
     if (!(p = *opaque))
         return NULL;
4521645b
     if ((output && p->url_write) || (!output && p->url_read))
         return p->name;
f8270bbf
     return avio_enum_protocols(opaque, output);
84be6e72
 }
 
0c5f8396
 int ffurl_register_protocol(URLProtocol *protocol)
de6d9b64
 {
     URLProtocol **p;
     p = &first_protocol;
53151723
     while (*p != NULL)
         p = &(*p)->next;
     *p             = protocol;
de6d9b64
     protocol->next = NULL;
     return 0;
 }
 
53151723
 static int url_alloc_for_protocol(URLContext **puc, struct URLProtocol *up,
                                   const char *filename, int flags,
                                   const AVIOInterruptCB *int_cb)
de6d9b64
 {
     URLContext *uc;
     int err;
 
67d4b3f2
 #if CONFIG_NETWORK
acb07430
     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
67d4b3f2
         return AVERROR(EIO);
 #endif
3d7f4f87
     if ((flags & AVIO_FLAG_READ) && !up->url_read) {
         av_log(NULL, AV_LOG_ERROR,
                "Impossible to open the '%s' protocol for reading\n", up->name);
         return AVERROR(EIO);
     }
     if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
         av_log(NULL, AV_LOG_ERROR,
                "Impossible to open the '%s' protocol for writing\n", up->name);
         return AVERROR(EIO);
     }
31277aeb
     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
8a9488b5
     if (!uc) {
8fa36ae0
         err = AVERROR(ENOMEM);
8a9488b5
         goto fail;
     }
1dee0aca
     uc->av_class = &ffurl_context_class;
53151723
     uc->filename = (char *)&uc[1];
f746a046
     strcpy(uc->filename, filename);
53151723
     uc->prot            = up;
     uc->flags           = flags;
     uc->is_streamed     = 0; /* default = not streamed */
8a9488b5
     uc->max_packet_size = 0; /* default: stream file */
735cf6b2
     if (up->priv_data_size) {
         uc->priv_data = av_mallocz(up->priv_data_size);
d206fd99
         if (!uc->priv_data) {
             err = AVERROR(ENOMEM);
             goto fail;
         }
735cf6b2
         if (up->priv_data_class) {
ad9e0ed1
             int proto_len= strlen(up->name);
6161c418
             char *start = strchr(uc->filename, ',');
53151723
             *(const AVClass **)uc->priv_data = up->priv_data_class;
735cf6b2
             av_opt_set_defaults(uc->priv_data);
ad9e0ed1
             if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
6161c418
                 int ret= 0;
                 char *p= start;
                 char sep= *++p;
                 char *key, *val;
                 p++;
                 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
                     *val= *key= 0;
                     ret= av_opt_set(uc->priv_data, p, key+1, 0);
                     if (ret == AVERROR_OPTION_NOT_FOUND)
                         av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
                     *val= *key= sep;
                     p= val+1;
                 }
                 if(ret<0 || p!=key){
                     av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
                     av_freep(&uc->priv_data);
                     av_freep(&uc);
d3035136
                     err = AVERROR(EINVAL);
6161c418
                     goto fail;
                 }
                 memmove(start, key+1, strlen(key));
             }
735cf6b2
         }
     }
6f1b7b39
     if (int_cb)
         uc->interrupt_callback = *int_cb;
abbae514
 
de6d9b64
     *puc = uc;
     return 0;
53151723
 fail:
8a9488b5
     *puc = NULL;
d206fd99
     if (uc)
         av_freep(&uc->priv_data);
     av_freep(&uc);
67d4b3f2
 #if CONFIG_NETWORK
acb07430
     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
         ff_network_close();
67d4b3f2
 #endif
8a9488b5
     return err;
de6d9b64
 }
 
53151723
 int ffurl_connect(URLContext *uc, AVDictionary **options)
ffbb289a
 {
ddffc2fd
     int err =
53151723
         uc->prot->url_open2 ? uc->prot->url_open2(uc,
                                                   uc->filename,
                                                   uc->flags,
                                                   options) :
ddffc2fd
         uc->prot->url_open(uc, uc->filename, uc->flags);
ffbb289a
     if (err)
         return err;
     uc->is_connected = 1;
53151723
     /* We must be careful here as ffurl_seek() could be slow,
      * for example for http */
     if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
         if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
             uc->is_streamed = 1;
ffbb289a
     return 0;
 }
 
2bab5d3e
 #define URL_SCHEME_CHARS                        \
     "abcdefghijklmnopqrstuvwxyz"                \
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
     "0123456789+-."
 
db3c9701
 static struct URLProtocol *url_find_protocol(const char *filename)
ba99cfc2
 {
163a3113
     URLProtocol *up = NULL;
8f73c060
     char proto_str[128], proto_nested[128], *ptr;
58030fc6
     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
2bab5d3e
 
d9fad53f
     if (filename[proto_len] != ':' &&
         (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
         is_dos_path(filename))
ba99cfc2
         strcpy(proto_str, "file");
2bab5d3e
     else
53151723
         av_strlcpy(proto_str, filename,
                    FFMIN(proto_len + 1, sizeof(proto_str)));
ba99cfc2
 
6161c418
     if ((ptr = strchr(proto_str, ',')))
         *ptr = '\0';
8f73c060
     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
     if ((ptr = strchr(proto_nested, '+')))
         *ptr = '\0';
 
163a3113
     while (up = ffurl_protocol_next(up)) {
ba99cfc2
         if (!strcmp(proto_str, up->name))
db3c9701
             break;
8f73c060
         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
             !strcmp(proto_nested, up->name))
db3c9701
             break;
ba99cfc2
     }
db3c9701
 
     return up;
 }
 
 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
                 const AVIOInterruptCB *int_cb)
 {
     URLProtocol *p = NULL;
 
     if (!first_protocol) {
         av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
                                      "Missing call to av_register_all()?\n");
     }
 
     p = url_find_protocol(filename);
     if (p)
        return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
 
ba99cfc2
     *puc = NULL;
db3c9701
     if (av_strstart("https:", filename, NULL))
1db88c33
         av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
12eeced8
     return AVERROR_PROTOCOL_NOT_FOUND;
ba99cfc2
 }
 
6f1b7b39
 int ffurl_open(URLContext **puc, const char *filename, int flags,
ddffc2fd
                const AVIOInterruptCB *int_cb, AVDictionary **options)
ffbb289a
 {
6f1b7b39
     int ret = ffurl_alloc(puc, filename, flags, int_cb);
ffbb289a
     if (ret)
         return ret;
ddffc2fd
     if (options && (*puc)->prot->priv_data_class &&
         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
         goto fail;
     ret = ffurl_connect(*puc, options);
ffbb289a
     if (!ret)
         return 0;
ddffc2fd
 fail:
e52a9145
     ffurl_close(*puc);
ffbb289a
     *puc = NULL;
     return ret;
 }
 
53151723
 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
                                          int size, int size_min,
                                          int (*transfer_func)(URLContext *h,
                                                               uint8_t *buf,
                                                               int size))
0e848977
 {
     int ret, len;
f1d2b572
     int fast_retries = 5;
78707225
     int64_t wait_since = 0;
0e848977
 
     len = 0;
90441276
     while (len < size_min) {
34fd2112
         if (ff_check_interrupt(&h->interrupt_callback))
             return AVERROR_EXIT;
53151723
         ret = transfer_func(h, buf + len, size - len);
90441276
         if (ret == AVERROR(EINTR))
             continue;
f87b1b37
         if (h->flags & AVIO_FLAG_NONBLOCK)
90441276
             return ret;
ddb901b7
         if (ret == AVERROR(EAGAIN)) {
             ret = 0;
78707225
             if (fast_retries) {
f1d2b572
                 fast_retries--;
78707225
             } else {
                 if (h->rw_timeout) {
                     if (!wait_since)
                         wait_since = av_gettime();
                     else if (av_gettime() > wait_since + h->rw_timeout)
9a27fd12
                         return AVERROR(EIO);
78707225
                 }
896bb0d7
                 av_usleep(1000);
78707225
             }
ddb901b7
         } else if (ret < 1)
a46e578d
             return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
f1d2b572
         if (ret)
53151723
             fast_retries = FFMAX(fast_retries, 2);
0e848977
         len += ret;
     }
     return len;
 }
 
bc371aca
 int ffurl_read(URLContext *h, unsigned char *buf, int size)
90441276
 {
b9553cf4
     if (!(h->flags & AVIO_FLAG_READ))
90441276
         return AVERROR(EIO);
     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
 }
 
dce37564
 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
a46f7516
 {
b9553cf4
     if (!(h->flags & AVIO_FLAG_READ))
90441276
         return AVERROR(EIO);
     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
a46f7516
 }
 
925e908b
 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
de6d9b64
 {
59d96941
     if (!(h->flags & AVIO_FLAG_WRITE))
6f3e0b21
         return AVERROR(EIO);
8a9488b5
     /* avoid sending too big packets */
     if (h->max_packet_size && size > h->max_packet_size)
6f3e0b21
         return AVERROR(EIO);
fe5feaeb
 
f7c89705
     return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
de6d9b64
 }
 
58a48c65
 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
de6d9b64
 {
bc5c918e
     int64_t ret;
de6d9b64
 
     if (!h->prot->url_seek)
28894105
         return AVERROR(ENOSYS);
493f54ad
     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
de6d9b64
     return ret;
 }
 
edf96d63
 int ffurl_closep(URLContext **hh)
de6d9b64
 {
edf96d63
     URLContext *h= *hh;
7a813b36
     int ret = 0;
53151723
     if (!h)
         return 0;     /* can happen when ffurl_open fails */
de6d9b64
 
ffbb289a
     if (h->is_connected && h->prot->url_close)
7a813b36
         ret = h->prot->url_close(h);
67d4b3f2
 #if CONFIG_NETWORK
acb07430
     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
         ff_network_close();
67d4b3f2
 #endif
d10361b6
     if (h->prot->priv_data_size) {
         if (h->prot->priv_data_class)
             av_opt_free(h->priv_data);
f89aa675
         av_freep(&h->priv_data);
d10361b6
     }
edf96d63
     av_freep(hh);
de6d9b64
     return ret;
 }
 
edf96d63
 int ffurl_close(URLContext *h)
 {
     return ffurl_closep(&h);
 }
 
 
db3c9701
 const char *avio_find_protocol_name(const char *url)
 {
     URLProtocol *p = url_find_protocol(url);
 
     return p ? p->name : NULL;
 }
 
175389c8
 int avio_check(const char *url, int flags)
 {
     URLContext *h;
6f1b7b39
     int ret = ffurl_alloc(&h, url, flags, NULL);
175389c8
     if (ret)
         return ret;
 
     if (h->prot->url_check) {
         ret = h->prot->url_check(h, flags);
     } else {
ddffc2fd
         ret = ffurl_connect(h, NULL);
175389c8
         if (ret >= 0)
             ret = flags;
     }
 
     ffurl_close(h);
     return ret;
 }
de6d9b64
 
32a97d46
 int64_t ffurl_size(URLContext *h)
de6d9b64
 {
bc5c918e
     int64_t pos, size;
115329f1
 
53151723
     size = ffurl_seek(h, 0, AVSEEK_SIZE);
     if (size < 0) {
58a48c65
         pos = ffurl_seek(h, 0, SEEK_CUR);
         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
eabbae73
             return size;
         size++;
58a48c65
         ffurl_seek(h, pos, SEEK_SET);
8e287af0
     }
de6d9b64
     return size;
 }
8a9488b5
 
1869ea03
 int ffurl_get_file_handle(URLContext *h)
f0a80394
 {
     if (!h->prot->url_get_file_handle)
         return -1;
     return h->prot->url_get_file_handle(h);
 }
 
d6b9da11
 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
 {
     if (!h->prot->url_get_multi_file_handle) {
         if (!h->prot->url_get_file_handle)
             return AVERROR(ENOSYS);
a96577df
         *handles = av_malloc(sizeof(**handles));
d6b9da11
         if (!*handles)
             return AVERROR(ENOMEM);
         *numhandles = 1;
         *handles[0] = h->prot->url_get_file_handle(h);
         return 0;
     }
     return h->prot->url_get_multi_file_handle(h, handles, numhandles);
 }
 
32d545e0
 int ffurl_shutdown(URLContext *h, int flags)
 {
     if (!h->prot->url_shutdown)
         return AVERROR(EINVAL);
     return h->prot->url_shutdown(h, flags);
 }
 
c4a090dd
 int ff_check_interrupt(AVIOInterruptCB *cb)
 {
     int ret;
     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
         return ret;
45f511ec
     return 0;
536333a0
 }