avio.c 6.91 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1 2
/*
 * Unbuffered io for ffmpeg system
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 23 24
/* needed for usleep() */
#define _XOPEN_SOURCE 600
#include <unistd.h>
25 26
#include "libavutil/avstring.h"
#include "libavcodec/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 33 34 35 36 37 38 39 40 41 42 43

#if LIBAVFORMAT_VERSION_MAJOR >= 53
/** @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";
}
static const AVOption options[] = {{NULL}};
static const AVClass urlcontext_class =
44
        { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
45 46
/*@}*/
#endif
Fabrice Bellard's avatar
Fabrice Bellard committed
47

48 49
static int default_interrupt_cb(void);

Fabrice Bellard's avatar
Fabrice Bellard committed
50
URLProtocol *first_protocol = NULL;
51
URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
Fabrice Bellard's avatar
Fabrice Bellard committed
52

53 54 55 56 57 58
URLProtocol *av_protocol_next(URLProtocol *p)
{
    if(p) return p->next;
    else  return first_protocol;
}

59
int av_register_protocol(URLProtocol *protocol)
Fabrice Bellard's avatar
Fabrice Bellard committed
60 61 62 63 64 65 66 67 68
{
    URLProtocol **p;
    p = &first_protocol;
    while (*p != NULL) p = &(*p)->next;
    *p = protocol;
    protocol->next = NULL;
    return 0;
}

69 70 71 72 73 74 75
#if LIBAVFORMAT_VERSION_MAJOR < 53
int register_protocol(URLProtocol *protocol)
{
    return av_register_protocol(protocol);
}
#endif

76 77
int url_open_protocol (URLContext **puc, struct URLProtocol *up,
                       const char *filename, int flags)
Fabrice Bellard's avatar
Fabrice Bellard committed
78 79 80 81
{
    URLContext *uc;
    int err;

82 83 84 85
#if CONFIG_NETWORK
    if (!ff_network_init())
        return AVERROR(EIO);
#endif
86
    uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
87
    if (!uc) {
88
        err = AVERROR(ENOMEM);
89 90
        goto fail;
    }
91 92 93
#if LIBAVFORMAT_VERSION_MAJOR >= 53
    uc->av_class = &urlcontext_class;
#endif
94
    uc->filename = (char *) &uc[1];
95
    strcpy(uc->filename, filename);
Fabrice Bellard's avatar
Fabrice Bellard committed
96 97 98
    uc->prot = up;
    uc->flags = flags;
    uc->is_streamed = 0; /* default = not streamed */
99
    uc->max_packet_size = 0; /* default: stream file */
Fabrice Bellard's avatar
Fabrice Bellard committed
100 101
    err = up->url_open(uc, filename, flags);
    if (err < 0) {
102
        av_free(uc);
103
        goto fail;
Fabrice Bellard's avatar
Fabrice Bellard committed
104
    }
105

106
    //We must be careful here as url_seek() could be slow, for example for http
107
    if(   (flags & (URL_WRONLY | URL_RDWR))
108
       || !strcmp(up->name, "file"))
109 110
        if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
            uc->is_streamed= 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
111 112
    *puc = uc;
    return 0;
113 114
 fail:
    *puc = NULL;
115 116 117
#if CONFIG_NETWORK
    ff_network_close();
#endif
118
    return err;
Fabrice Bellard's avatar
Fabrice Bellard committed
119 120
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
int url_open(URLContext **puc, const char *filename, int flags)
{
    URLProtocol *up;
    const char *p;
    char proto_str[128], *q;

    p = filename;
    q = proto_str;
    while (*p != '\0' && *p != ':') {
        /* protocols can only contain alphabetic chars */
        if (!isalpha(*p))
            goto file_proto;
        if ((q - proto_str) < sizeof(proto_str) - 1)
            *q++ = *p;
        p++;
    }
    /* if the protocol has length 1, we consider it is a dos drive */
138
    if (*p == '\0' || is_dos_path(filename)) {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    file_proto:
        strcpy(proto_str, "file");
    } else {
        *q = '\0';
    }

    up = first_protocol;
    while (up != NULL) {
        if (!strcmp(proto_str, up->name))
            return url_open_protocol (puc, up, filename, flags);
        up = up->next;
    }
    *puc = NULL;
    return AVERROR(ENOENT);
}

Fabrice Bellard's avatar
Fabrice Bellard committed
155 156 157 158
int url_read(URLContext *h, unsigned char *buf, int size)
{
    int ret;
    if (h->flags & URL_WRONLY)
159
        return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
160 161 162 163
    ret = h->prot->url_read(h, buf, size);
    return ret;
}

164 165 166
int url_read_complete(URLContext *h, unsigned char *buf, int size)
{
    int ret, len;
167
    int fast_retries = 5;
168 169 170 171

    len = 0;
    while (len < size) {
        ret = url_read(h, buf+len, size-len);
172 173
        if (ret == AVERROR(EAGAIN)) {
            ret = 0;
174 175 176 177
            if (fast_retries)
                fast_retries--;
            else
                usleep(1000);
178 179
        } else if (ret < 1)
            return ret < 0 ? ret : len;
180 181
        if (ret)
           fast_retries = FFMAX(fast_retries, 2);
182 183 184 185 186
        len += ret;
    }
    return len;
}

187
int url_write(URLContext *h, const unsigned char *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
188 189
{
    int ret;
190
    if (!(h->flags & (URL_WRONLY | URL_RDWR)))
191
        return AVERROR(EIO);
192 193
    /* avoid sending too big packets */
    if (h->max_packet_size && size > h->max_packet_size)
194
        return AVERROR(EIO);
Fabrice Bellard's avatar
Fabrice Bellard committed
195 196 197 198
    ret = h->prot->url_write(h, buf, size);
    return ret;
}

199
int64_t url_seek(URLContext *h, int64_t pos, int whence)
Fabrice Bellard's avatar
Fabrice Bellard committed
200
{
201
    int64_t ret;
Fabrice Bellard's avatar
Fabrice Bellard committed
202 203

    if (!h->prot->url_seek)
204
        return AVERROR(ENOSYS);
205
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
Fabrice Bellard's avatar
Fabrice Bellard committed
206 207 208 209 210
    return ret;
}

int url_close(URLContext *h)
{
211
    int ret = 0;
212
    if (!h) return 0; /* can happen when url_open fails */
Fabrice Bellard's avatar
Fabrice Bellard committed
213

214 215
    if (h->prot->url_close)
        ret = h->prot->url_close(h);
216 217 218
#if CONFIG_NETWORK
    ff_network_close();
#endif
219
    av_free(h);
Fabrice Bellard's avatar
Fabrice Bellard committed
220 221 222 223 224 225 226 227 228 229 230 231
    return ret;
}

int url_exist(const char *filename)
{
    URLContext *h;
    if (url_open(&h, filename, URL_RDONLY) < 0)
        return 0;
    url_close(h);
    return 1;
}

232
int64_t url_filesize(URLContext *h)
Fabrice Bellard's avatar
Fabrice Bellard committed
233
{
234
    int64_t pos, size;
235

236 237
    size= url_seek(h, 0, AVSEEK_SIZE);
    if(size<0){
238
        pos = url_seek(h, 0, SEEK_CUR);
239 240 241
        if ((size = url_seek(h, -1, SEEK_END)) < 0)
            return size;
        size++;
242
        url_seek(h, pos, SEEK_SET);
243
    }
Fabrice Bellard's avatar
Fabrice Bellard committed
244 245
    return size;
}
246

247 248 249 250 251 252 253
int url_get_file_handle(URLContext *h)
{
    if (!h->prot->url_get_file_handle)
        return -1;
    return h->prot->url_get_file_handle(h);
}

254 255 256 257
int url_get_max_packet_size(URLContext *h)
{
    return h->max_packet_size;
}
258 259 260

void url_get_filename(URLContext *h, char *buf, int buf_size)
{
261
    av_strlcpy(buf, h->filename, buf_size);
262
}
263 264 265 266 267 268 269 270 271 272 273 274 275


static int default_interrupt_cb(void)
{
    return 0;
}

void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
{
    if (!interrupt_cb)
        interrupt_cb = default_interrupt_cb;
    url_interrupt_cb = interrupt_cb;
}
276

277
int av_url_read_pause(URLContext *h, int pause)
278 279 280
{
    if (!h->prot->url_read_pause)
        return AVERROR(ENOSYS);
281
    return h->prot->url_read_pause(h, pause);
282 283
}

284
int64_t av_url_read_seek(URLContext *h,
285 286 287 288 289 290
        int stream_index, int64_t timestamp, int flags)
{
    if (!h->prot->url_read_seek)
        return AVERROR(ENOSYS);
    return h->prot->url_read_seek(h, stream_index, timestamp, flags);
}