avio.c 9.63 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
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * 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
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 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 <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
            *(const AVClass**)uc->priv_data = up->priv_data_class;
139 140 141
            av_opt_set_defaults(uc->priv_data);
        }
    }
142 143
    if (int_cb)
        uc->interrupt_callback = *int_cb;
144

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

307 308 309
int avio_check(const char *url, int flags)
{
    URLContext *h;
310
    int ret = ffurl_alloc(&h, url, flags, NULL);
311 312 313 314 315 316
    if (ret)
        return ret;

    if (h->prot->url_check) {
        ret = h->prot->url_check(h, flags);
    } else {
317
        ret = ffurl_connect(h, NULL);
318 319 320 321 322 323 324 325
        if (ret >= 0)
            ret = flags;
    }

    ffurl_close(h);
    return ret;
}

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

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

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

348 349 350 351 352
int ff_check_interrupt(AVIOInterruptCB *cb)
{
    int ret;
    if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
        return ret;
353
    return 0;
354
}