avio.c 10.3 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * unbuffered I/O
Fabrice Bellard's avatar
Fabrice Bellard committed
3
 * Copyright (c) 2001 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 * Libav 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
Fabrice Bellard's avatar
Fabrice Bellard committed
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
 *
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
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"
Fabrice Bellard's avatar
Fabrice Bellard committed
27
#include "avformat.h"
28 29 30
#if CONFIG_NETWORK
#include "network.h"
#endif
Anton Khirnov's avatar
Anton Khirnov committed
31
#include "url.h"
32

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

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

40 41 42 43 44 45 46 47
/** @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";
}
48 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

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;

}

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

85

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

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

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

118
#if CONFIG_NETWORK
119
    if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
120 121
        return AVERROR(EIO);
#endif
122
    uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
123
    if (!uc) {
124
        err = AVERROR(ENOMEM);
125 126
        goto fail;
    }
127
    uc->av_class = &ffurl_context_class;
128
    uc->filename = (char *) &uc[1];
Fabrice Bellard's avatar
Fabrice Bellard committed
129
    strcpy(uc->filename, filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
130 131 132
    uc->prot = up;
    uc->flags = flags;
    uc->is_streamed = 0; /* default = not streamed */
133
    uc->max_packet_size = 0; /* default: stream file */
134 135 136
    if (up->priv_data_size) {
        uc->priv_data = av_mallocz(up->priv_data_size);
        if (up->priv_data_class) {
137
            *(const AVClass**)uc->priv_data = up->priv_data_class;
138 139 140
            av_opt_set_defaults(uc->priv_data);
        }
    }
141 142
    if (int_cb)
        uc->interrupt_callback = *int_cb;
143

Fabrice Bellard's avatar
Fabrice Bellard committed
144 145
    *puc = uc;
    return 0;
146 147
 fail:
    *puc = NULL;
148
#if CONFIG_NETWORK
149 150
    if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
        ff_network_close();
151
#endif
152
    return err;
Fabrice Bellard's avatar
Fabrice Bellard committed
153 154
}

155
int ffurl_connect(URLContext* uc, AVDictionary **options)
156
{
157 158 159
    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);
160 161 162
    if (err)
        return err;
    uc->is_connected = 1;
163
    //We must be careful here as ffurl_seek() could be slow, for example for http
164
    if(   (uc->flags & AVIO_FLAG_WRITE)
165
       || !strcmp(uc->prot->name, "file"))
166
        if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
167 168 169 170
            uc->is_streamed= 1;
    return 0;
}

171 172 173 174 175
#define URL_SCHEME_CHARS                        \
    "abcdefghijklmnopqrstuvwxyz"                \
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
    "0123456789+-."

176 177
int ffurl_alloc(URLContext **puc, const char *filename, int flags,
                const AVIOInterruptCB *int_cb)
178
{
179
    URLProtocol *up = NULL;
180
    char proto_str[128], proto_nested[128], *ptr;
181 182 183
    size_t proto_len = strspn(filename, URL_SCHEME_CHARS);

    if (filename[proto_len] != ':' || is_dos_path(filename))
184
        strcpy(proto_str, "file");
185 186
    else
        av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
187

188 189 190 191
    av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
    if ((ptr = strchr(proto_nested, '+')))
        *ptr = '\0';

192
    while (up = ffurl_protocol_next(up)) {
193
        if (!strcmp(proto_str, up->name))
194
            return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
195 196
        if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
            !strcmp(proto_nested, up->name))
197
            return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
198 199 200 201 202
    }
    *puc = NULL;
    return AVERROR(ENOENT);
}

203
int ffurl_open(URLContext **puc, const char *filename, int flags,
204
               const AVIOInterruptCB *int_cb, AVDictionary **options)
205
{
206
    int ret = ffurl_alloc(puc, filename, flags, int_cb);
207 208
    if (ret)
        return ret;
209 210 211 212
    if (options && (*puc)->prot->priv_data_class &&
        (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
        goto fail;
    ret = ffurl_connect(*puc, options);
213 214
    if (!ret)
        return 0;
215
fail:
216
    ffurl_close(*puc);
217 218 219 220
    *puc = NULL;
    return ret;
}

221
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
222
                                         int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
223 224
{
    int ret, len;
225
    int fast_retries = 5;
226 227

    len = 0;
228
    while (len < size_min) {
229
        ret = transfer_func(h, buf+len, size-len);
230 231
        if (ret == AVERROR(EINTR))
            continue;
232
        if (h->flags & AVIO_FLAG_NONBLOCK)
233
            return ret;
234 235
        if (ret == AVERROR(EAGAIN)) {
            ret = 0;
236 237 238
            if (fast_retries)
                fast_retries--;
            else
239
                av_usleep(1000);
240
        } else if (ret < 1)
241
            return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
242 243
        if (ret)
           fast_retries = FFMAX(fast_retries, 2);
244
        len += ret;
245
        if (ff_check_interrupt(&h->interrupt_callback))
246
            return AVERROR_EXIT;
247 248 249 250
    }
    return len;
}

251
int ffurl_read(URLContext *h, unsigned char *buf, int size)
252
{
253
    if (!(h->flags & AVIO_FLAG_READ))
254 255 256 257
        return AVERROR(EIO);
    return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
}

258
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
259
{
260
    if (!(h->flags & AVIO_FLAG_READ))
261 262
        return AVERROR(EIO);
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
263 264
}

265
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
266
{
267
    if (!(h->flags & AVIO_FLAG_WRITE))
268
        return AVERROR(EIO);
269 270
    /* avoid sending too big packets */
    if (h->max_packet_size && size > h->max_packet_size)
271
        return AVERROR(EIO);
272

273
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
Fabrice Bellard's avatar
Fabrice Bellard committed
274 275
}

276
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
277
{
278
    int64_t ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
279 280

    if (!h->prot->url_seek)
281
        return AVERROR(ENOSYS);
282
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
Fabrice Bellard's avatar
Fabrice Bellard committed
283 284 285
    return ret;
}

286
int ffurl_close(URLContext *h)
Fabrice Bellard's avatar
Fabrice Bellard committed
287
{
288
    int ret = 0;
289
    if (!h) return 0; /* can happen when ffurl_open fails */
Fabrice Bellard's avatar
Fabrice Bellard committed
290

291
    if (h->is_connected && h->prot->url_close)
292
        ret = h->prot->url_close(h);
293
#if CONFIG_NETWORK
294 295
    if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
        ff_network_close();
296
#endif
297 298 299
    if (h->prot->priv_data_size) {
        if (h->prot->priv_data_class)
            av_opt_free(h->priv_data);
300
        av_free(h->priv_data);
301
    }
302
    av_free(h);
Fabrice Bellard's avatar
Fabrice Bellard committed
303 304 305
    return ret;
}

Stefano Sabatini's avatar
Stefano Sabatini committed
306 307 308
int avio_check(const char *url, int flags)
{
    URLContext *h;
309
    int ret = ffurl_alloc(&h, url, flags, NULL);
Stefano Sabatini's avatar
Stefano Sabatini committed
310 311 312 313 314 315
    if (ret)
        return ret;

    if (h->prot->url_check) {
        ret = h->prot->url_check(h, flags);
    } else {
316
        ret = ffurl_connect(h, NULL);
Stefano Sabatini's avatar
Stefano Sabatini committed
317 318 319 320 321 322 323 324
        if (ret >= 0)
            ret = flags;
    }

    ffurl_close(h);
    return ret;
}

325
int64_t ffurl_size(URLContext *h)
Fabrice Bellard's avatar
Fabrice Bellard committed
326
{
327
    int64_t pos, size;
328

329
    size= ffurl_seek(h, 0, AVSEEK_SIZE);
330
    if(size<0){
331 332
        pos = ffurl_seek(h, 0, SEEK_CUR);
        if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
333 334
            return size;
        size++;
335
        ffurl_seek(h, pos, SEEK_SET);
336
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
337 338
    return size;
}
339

340
int ffurl_get_file_handle(URLContext *h)
341 342 343 344 345 346
{
    if (!h->prot->url_get_file_handle)
        return -1;
    return h->prot->url_get_file_handle(h);
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
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);
        *handles = av_malloc(sizeof(*handles));
        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);
}

362 363 364 365 366 367 368
int ffurl_shutdown(URLContext *h, int flags)
{
    if (!h->prot->url_shutdown)
        return AVERROR(EINVAL);
    return h->prot->url_shutdown(h, flags);
}

369 370 371 372 373
int ff_check_interrupt(AVIOInterruptCB *cb)
{
    int ret;
    if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
        return ret;
374
    return 0;
375
}