avio.c 11 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * unbuffered I/O
3
 * Copyright (c) 2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
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"
Fabrice Bellard's avatar
Fabrice Bellard committed
28
#include "avformat.h"
29 30 31
#if CONFIG_NETWORK
#include "network.h"
#endif
32
#include "url.h"
33

34 35 36 37 38 39 40
static URLProtocol *first_protocol = NULL;

URLProtocol *ffurl_protocol_next(URLProtocol *prev)
{
    return prev ? prev->next : first_protocol;
}

41 42 43 44 45 46 47 48
/** @name Logging context. */
/*@{*/
static const char *urlcontext_to_name(void *ptr)
{
    URLContext *h = (URLContext *)ptr;
    if(h->prot) return h->prot->name;
    else        return "NULL";
}
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

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;

}

75
static const AVOption options[] = {{NULL}};
76
const AVClass ffurl_context_class = {
77 78 79 80
    .class_name     = "URLContext",
    .item_name      = urlcontext_to_name,
    .option         = options,
    .version        = LIBAVUTIL_VERSION_INT,
81 82
    .child_next     = urlcontext_child_next,
    .child_class_next = urlcontext_child_class_next,
83
};
84
/*@}*/
Fabrice Bellard's avatar
Fabrice Bellard committed
85

86

87 88 89
const char *avio_enum_protocols(void **opaque, int output)
{
    URLProtocol **p = opaque;
90
    *p = ffurl_protocol_next(*p);
91 92 93 94
    if (!*p) return NULL;
    if ((output && (*p)->url_write) || (!output && (*p)->url_read))
        return (*p)->name;
    return avio_enum_protocols(opaque, output);
95 96
}

97
int ffurl_register_protocol(URLProtocol *protocol, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
98 99
{
    URLProtocol **p;
100 101 102 103 104
    if (size < sizeof(URLProtocol)) {
        URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
        memcpy(temp, protocol, size);
        protocol = temp;
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
105 106 107 108 109 110 111
    p = &first_protocol;
    while (*p != NULL) p = &(*p)->next;
    *p = protocol;
    protocol->next = NULL;
    return 0;
}

112
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
113 114
                                   const char *filename, int flags,
                                   const AVIOInterruptCB *int_cb)
Fabrice Bellard's avatar
Fabrice Bellard committed
115 116 117 118
{
    URLContext *uc;
    int err;

119
#if CONFIG_NETWORK
120
    if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
121 122
        return AVERROR(EIO);
#endif
123
    uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
124
    if (!uc) {
125
        err = AVERROR(ENOMEM);
126 127
        goto fail;
    }
128
    uc->av_class = &ffurl_context_class;
129
    uc->filename = (char *) &uc[1];
130
    strcpy(uc->filename, filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
131 132 133
    uc->prot = up;
    uc->flags = flags;
    uc->is_streamed = 0; /* default = not streamed */
134
    uc->max_packet_size = 0; /* default: stream file */
135 136 137
    if (up->priv_data_size) {
        uc->priv_data = av_mallocz(up->priv_data_size);
        if (up->priv_data_class) {
138
            int proto_len= strlen(up->name);
139
            char *start = strchr(uc->filename, ',');
140
            *(const AVClass**)uc->priv_data = up->priv_data_class;
141
            av_opt_set_defaults(uc->priv_data);
142
            if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
                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);
                    goto fail;
                }
                memmove(start, key+1, strlen(key));
            }
164 165
        }
    }
166 167
    if (int_cb)
        uc->interrupt_callback = *int_cb;
168

Fabrice Bellard's avatar
Fabrice Bellard committed
169 170
    *puc = uc;
    return 0;
171 172
 fail:
    *puc = NULL;
173
#if CONFIG_NETWORK
174 175
    if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
        ff_network_close();
176
#endif
177
    return err;
Fabrice Bellard's avatar
Fabrice Bellard committed
178 179
}

180
int ffurl_connect(URLContext* uc, AVDictionary **options)
181
{
182 183 184
    int err =
        uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
        uc->prot->url_open(uc, uc->filename, uc->flags);
185 186 187
    if (err)
        return err;
    uc->is_connected = 1;
188
    //We must be careful here as ffurl_seek() could be slow, for example for http
189
    if(   (uc->flags & AVIO_FLAG_WRITE)
190
       || !strcmp(uc->prot->name, "file"))
191
        if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
192 193 194 195
            uc->is_streamed= 1;
    return 0;
}

196 197 198 199 200
#define URL_SCHEME_CHARS                        \
    "abcdefghijklmnopqrstuvwxyz"                \
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
    "0123456789+-."

201 202
int ffurl_alloc(URLContext **puc, const char *filename, int flags,
                const AVIOInterruptCB *int_cb)
203
{
204
    URLProtocol *up = NULL;
205
    char proto_str[128], proto_nested[128], *ptr;
206
    size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
207

208 209 210 211 212
    if (!first_protocol) {
        av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
                                     "Missing call to av_register_all()?\n");
    }

213
    if (filename[proto_len] != ':' &&  filename[proto_len] != ',' || is_dos_path(filename))
214
        strcpy(proto_str, "file");
215
    else
216
        av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
217

218 219
    if ((ptr = strchr(proto_str, ',')))
        *ptr = '\0';
220 221 222 223
    av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
    if ((ptr = strchr(proto_nested, '+')))
        *ptr = '\0';

224
    while (up = ffurl_protocol_next(up)) {
225
        if (!strcmp(proto_str, up->name))
226
            return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
227 228
        if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
            !strcmp(proto_nested, up->name))
229
            return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
230 231 232 233 234
    }
    *puc = NULL;
    return AVERROR(ENOENT);
}

235
int ffurl_open(URLContext **puc, const char *filename, int flags,
236
               const AVIOInterruptCB *int_cb, AVDictionary **options)
237
{
238
    int ret = ffurl_alloc(puc, filename, flags, int_cb);
239 240
    if (ret)
        return ret;
241 242 243 244
    if (options && (*puc)->prot->priv_data_class &&
        (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
        goto fail;
    ret = ffurl_connect(*puc, options);
245 246
    if (!ret)
        return 0;
247
fail:
248
    ffurl_close(*puc);
249 250 251 252
    *puc = NULL;
    return ret;
}

253
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
254
                                         int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
255 256
{
    int ret, len;
257
    int fast_retries = 5;
258 259

    len = 0;
260
    while (len < size_min) {
261
        ret = transfer_func(h, buf+len, size-len);
262 263
        if (ret == AVERROR(EINTR))
            continue;
264
        if (h->flags & AVIO_FLAG_NONBLOCK)
265
            return ret;
266 267
        if (ret == AVERROR(EAGAIN)) {
            ret = 0;
268 269 270 271
            if (fast_retries)
                fast_retries--;
            else
                usleep(1000);
272 273
        } else if (ret < 1)
            return ret < 0 ? ret : len;
274 275
        if (ret)
           fast_retries = FFMAX(fast_retries, 2);
276
        len += ret;
277
        if (len < size && ff_check_interrupt(&h->interrupt_callback))
278
            return AVERROR_EXIT;
279 280 281 282
    }
    return len;
}

283
int ffurl_read(URLContext *h, unsigned char *buf, int size)
284
{
285
    if (!(h->flags & AVIO_FLAG_READ))
286 287 288 289
        return AVERROR(EIO);
    return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
}

290
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
291
{
292
    if (!(h->flags & AVIO_FLAG_READ))
293 294
        return AVERROR(EIO);
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
295 296
}

297
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
298
{
299
    if (!(h->flags & AVIO_FLAG_WRITE))
300
        return AVERROR(EIO);
301 302
    /* avoid sending too big packets */
    if (h->max_packet_size && size > h->max_packet_size)
303
        return AVERROR(EIO);
304

305
    return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
Fabrice Bellard's avatar
Fabrice Bellard committed
306 307
}

308
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
309
{
310
    int64_t ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
311 312

    if (!h->prot->url_seek)
313
        return AVERROR(ENOSYS);
314
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
Fabrice Bellard's avatar
Fabrice Bellard committed
315 316 317
    return ret;
}

318
int ffurl_close(URLContext *h)
Fabrice Bellard's avatar
Fabrice Bellard committed
319
{
320
    int ret = 0;
321
    if (!h) return 0; /* can happen when ffurl_open fails */
Fabrice Bellard's avatar
Fabrice Bellard committed
322

323
    if (h->is_connected && h->prot->url_close)
324
        ret = h->prot->url_close(h);
325
#if CONFIG_NETWORK
326 327
    if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
        ff_network_close();
328
#endif
329 330 331
    if (h->prot->priv_data_size) {
        if (h->prot->priv_data_class)
            av_opt_free(h->priv_data);
332
        av_free(h->priv_data);
333
    }
334
    av_free(h);
Fabrice Bellard's avatar
Fabrice Bellard committed
335 336 337
    return ret;
}

338 339 340
int avio_check(const char *url, int flags)
{
    URLContext *h;
341
    int ret = ffurl_alloc(&h, url, flags, NULL);
342 343 344 345 346 347
    if (ret)
        return ret;

    if (h->prot->url_check) {
        ret = h->prot->url_check(h, flags);
    } else {
348
        ret = ffurl_connect(h, NULL);
349 350 351 352 353 354 355
        if (ret >= 0)
            ret = flags;
    }

    ffurl_close(h);
    return ret;
}
Fabrice Bellard's avatar
Fabrice Bellard committed
356

357
int64_t ffurl_size(URLContext *h)
Fabrice Bellard's avatar
Fabrice Bellard committed
358
{
359
    int64_t pos, size;
360

361
    size= ffurl_seek(h, 0, AVSEEK_SIZE);
362
    if(size<0){
363 364
        pos = ffurl_seek(h, 0, SEEK_CUR);
        if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
365 366
            return size;
        size++;
367
        ffurl_seek(h, pos, SEEK_SET);
368
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
369 370
    return size;
}
371

372
int ffurl_get_file_handle(URLContext *h)
373 374 375 376 377 378
{
    if (!h->prot->url_get_file_handle)
        return -1;
    return h->prot->url_get_file_handle(h);
}

379 380 381 382 383
int ff_check_interrupt(AVIOInterruptCB *cb)
{
    int ret;
    if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
        return ret;
384
    return 0;
385
}