Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 6cc7f139

History | View | Annotate | Download (9.47 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Unbuffered io for ffmpeg system
3 19720f15 Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 b78e7197 Diego Biurrun
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 245976da Diego Biurrun
22 f1d2b572 Reimar Döffinger
/* needed for usleep() */
23
#define _XOPEN_SOURCE 600
24
#include <unistd.h>
25 245976da Diego Biurrun
#include "libavutil/avstring.h"
26 6ed04040 Michael Niedermayer
#include "libavutil/opt.h"
27 bc574408 Ramiro Polla
#include "os_support.h"
28 de6d9b64 Fabrice Bellard
#include "avformat.h"
29 67d4b3f2 Martin Storsjö
#if CONFIG_NETWORK
30
#include "network.h"
31
#endif
32 5acef35f Björn Axelsson
33 404eba44 Aurelien Jacobs
#if FF_API_URL_CLASS
34 5acef35f Björn Axelsson
/** @name Logging context. */
35
/*@{*/
36
static const char *urlcontext_to_name(void *ptr)
37
{
38
    URLContext *h = (URLContext *)ptr;
39
    if(h->prot) return h->prot->name;
40
    else        return "NULL";
41
}
42
static const AVOption options[] = {{NULL}};
43
static const AVClass urlcontext_class =
44 2308b6c1 Michael Niedermayer
        { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
45 5acef35f Björn Axelsson
/*@}*/
46
#endif
47 de6d9b64 Fabrice Bellard
48 019ac05a Fabrice Bellard
static int default_interrupt_cb(void);
49
50 de6d9b64 Fabrice Bellard
URLProtocol *first_protocol = NULL;
51 019ac05a Fabrice Bellard
URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
52 de6d9b64 Fabrice Bellard
53 84be6e72 Michael Niedermayer
URLProtocol *av_protocol_next(URLProtocol *p)
54
{
55
    if(p) return p->next;
56
    else  return first_protocol;
57
}
58
59 9b07a2dc Martin Storsjö
int av_register_protocol2(URLProtocol *protocol, int size)
60 de6d9b64 Fabrice Bellard
{
61
    URLProtocol **p;
62 9b07a2dc Martin Storsjö
    if (size < sizeof(URLProtocol)) {
63
        URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
64
        memcpy(temp, protocol, size);
65
        protocol = temp;
66
    }
67 de6d9b64 Fabrice Bellard
    p = &first_protocol;
68
    while (*p != NULL) p = &(*p)->next;
69
    *p = protocol;
70
    protocol->next = NULL;
71
    return 0;
72
}
73
74 838b27b4 Aurelien Jacobs
#if FF_API_REGISTER_PROTOCOL
75 9b07a2dc Martin Storsjö
/* The layout of URLProtocol as of when major was bumped to 52 */
76
struct URLProtocol_compat {
77
    const char *name;
78
    int (*url_open)(URLContext *h, const char *filename, int flags);
79
    int (*url_read)(URLContext *h, unsigned char *buf, int size);
80
    int (*url_write)(URLContext *h, unsigned char *buf, int size);
81
    int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
82
    int (*url_close)(URLContext *h);
83
    struct URLProtocol *next;
84
};
85
86
int av_register_protocol(URLProtocol *protocol)
87
{
88
    return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
89
}
90
91 65c40e4e Stefano Sabatini
int register_protocol(URLProtocol *protocol)
92
{
93 0a216bd1 Stefano Sabatini
    return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
94 65c40e4e Stefano Sabatini
}
95
#endif
96
97 ffbb289a Martin Storsjö
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
98 02174293 Martin Storsjö
                                   const char *filename, int flags)
99 de6d9b64 Fabrice Bellard
{
100
    URLContext *uc;
101
    int err;
102
103 67d4b3f2 Martin Storsjö
#if CONFIG_NETWORK
104
    if (!ff_network_init())
105
        return AVERROR(EIO);
106
#endif
107 31277aeb Daniel Kristjansson
    uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
108 8a9488b5 Fabrice Bellard
    if (!uc) {
109 8fa36ae0 François Revol
        err = AVERROR(ENOMEM);
110 8a9488b5 Fabrice Bellard
        goto fail;
111
    }
112 404eba44 Aurelien Jacobs
#if FF_API_URL_CLASS
113 5acef35f Björn Axelsson
    uc->av_class = &urlcontext_class;
114
#endif
115 62181004 Ronald S. Bultje
    uc->filename = (char *) &uc[1];
116 f746a046 Fabrice Bellard
    strcpy(uc->filename, filename);
117 de6d9b64 Fabrice Bellard
    uc->prot = up;
118
    uc->flags = flags;
119
    uc->is_streamed = 0; /* default = not streamed */
120 8a9488b5 Fabrice Bellard
    uc->max_packet_size = 0; /* default: stream file */
121 735cf6b2 Martin Storsjö
    if (up->priv_data_size) {
122
        uc->priv_data = av_mallocz(up->priv_data_size);
123
        if (up->priv_data_class) {
124 805488f0 Eli Friedman
            *(const AVClass**)uc->priv_data = up->priv_data_class;
125 735cf6b2 Martin Storsjö
            av_opt_set_defaults(uc->priv_data);
126
        }
127
    }
128 abbae514 Michael Niedermayer
129 de6d9b64 Fabrice Bellard
    *puc = uc;
130
    return 0;
131 8a9488b5 Fabrice Bellard
 fail:
132
    *puc = NULL;
133 67d4b3f2 Martin Storsjö
#if CONFIG_NETWORK
134
    ff_network_close();
135
#endif
136 8a9488b5 Fabrice Bellard
    return err;
137 de6d9b64 Fabrice Bellard
}
138
139 ffbb289a Martin Storsjö
int url_connect(URLContext* uc)
140
{
141
    int err = uc->prot->url_open(uc, uc->filename, uc->flags);
142
    if (err)
143
        return err;
144
    uc->is_connected = 1;
145
    //We must be careful here as url_seek() could be slow, for example for http
146
    if(   (uc->flags & (URL_WRONLY | URL_RDWR))
147
       || !strcmp(uc->prot->name, "file"))
148
        if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
149
            uc->is_streamed= 1;
150
    return 0;
151
}
152
153 333e8943 Anton Khirnov
#if FF_API_OLD_AVIO
154 ffbb289a Martin Storsjö
int url_open_protocol (URLContext **puc, struct URLProtocol *up,
155
                       const char *filename, int flags)
156
{
157
    int ret;
158
159
    ret = url_alloc_for_protocol(puc, up, filename, flags);
160
    if (ret)
161
        goto fail;
162
    ret = url_connect(*puc);
163
    if (!ret)
164
        return 0;
165
 fail:
166
    url_close(*puc);
167
    *puc = NULL;
168
    return ret;
169
}
170 333e8943 Anton Khirnov
#endif
171 ffbb289a Martin Storsjö
172 2bab5d3e Måns Rullgård
#define URL_SCHEME_CHARS                        \
173
    "abcdefghijklmnopqrstuvwxyz"                \
174
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
175
    "0123456789+-."
176
177 ffbb289a Martin Storsjö
int url_alloc(URLContext **puc, const char *filename, int flags)
178 ba99cfc2 Ronald S. Bultje
{
179
    URLProtocol *up;
180 8f73c060 Martin Storsjö
    char proto_str[128], proto_nested[128], *ptr;
181 2bab5d3e Måns Rullgård
    size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
182
183
    if (filename[proto_len] != ':' || is_dos_path(filename))
184 ba99cfc2 Ronald S. Bultje
        strcpy(proto_str, "file");
185 2bab5d3e Måns Rullgård
    else
186
        av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
187 ba99cfc2 Ronald S. Bultje
188 8f73c060 Martin Storsjö
    av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
189
    if ((ptr = strchr(proto_nested, '+')))
190
        *ptr = '\0';
191
192 ba99cfc2 Ronald S. Bultje
    up = first_protocol;
193
    while (up != NULL) {
194
        if (!strcmp(proto_str, up->name))
195 ffbb289a Martin Storsjö
            return url_alloc_for_protocol (puc, up, filename, flags);
196 8f73c060 Martin Storsjö
        if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
197
            !strcmp(proto_nested, up->name))
198
            return url_alloc_for_protocol (puc, up, filename, flags);
199 ba99cfc2 Ronald S. Bultje
        up = up->next;
200
    }
201
    *puc = NULL;
202
    return AVERROR(ENOENT);
203
}
204
205 ffbb289a Martin Storsjö
int url_open(URLContext **puc, const char *filename, int flags)
206
{
207
    int ret = url_alloc(puc, filename, flags);
208
    if (ret)
209
        return ret;
210
    ret = url_connect(*puc);
211
    if (!ret)
212
        return 0;
213
    url_close(*puc);
214
    *puc = NULL;
215
    return ret;
216
}
217
218 90441276 Nicolas George
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
219 a46f7516 Michael Niedermayer
                                         int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
220 0e848977 Kostya Shishkov
{
221
    int ret, len;
222 f1d2b572 Reimar Döffinger
    int fast_retries = 5;
223 0e848977 Kostya Shishkov
224
    len = 0;
225 90441276 Nicolas George
    while (len < size_min) {
226 a46f7516 Michael Niedermayer
        ret = transfer_func(h, buf+len, size-len);
227 90441276 Nicolas George
        if (ret == AVERROR(EINTR))
228
            continue;
229
        if (h->flags & URL_FLAG_NONBLOCK)
230
            return ret;
231 ddb901b7 Reimar Döffinger
        if (ret == AVERROR(EAGAIN)) {
232
            ret = 0;
233 f1d2b572 Reimar Döffinger
            if (fast_retries)
234
                fast_retries--;
235
            else
236
                usleep(1000);
237 ddb901b7 Reimar Döffinger
        } else if (ret < 1)
238
            return ret < 0 ? ret : len;
239 f1d2b572 Reimar Döffinger
        if (ret)
240
           fast_retries = FFMAX(fast_retries, 2);
241 0e848977 Kostya Shishkov
        len += ret;
242 688c22e0 Baptiste Coudurier
        if (url_interrupt_cb())
243 c76374c6 Nicolas George
            return AVERROR_EXIT;
244 0e848977 Kostya Shishkov
    }
245
    return len;
246
}
247
248 90441276 Nicolas George
int url_read(URLContext *h, unsigned char *buf, int size)
249
{
250
    if (h->flags & URL_WRONLY)
251
        return AVERROR(EIO);
252
    return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
253
}
254
255 a46f7516 Michael Niedermayer
int url_read_complete(URLContext *h, unsigned char *buf, int size)
256
{
257 90441276 Nicolas George
    if (h->flags & URL_WRONLY)
258
        return AVERROR(EIO);
259
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
260 a46f7516 Michael Niedermayer
}
261
262 27241cbf Martin Storsjö
int url_write(URLContext *h, const unsigned char *buf, int size)
263 de6d9b64 Fabrice Bellard
{
264 8a9488b5 Fabrice Bellard
    if (!(h->flags & (URL_WRONLY | URL_RDWR)))
265 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
266 8a9488b5 Fabrice Bellard
    /* avoid sending too big packets */
267
    if (h->max_packet_size && size > h->max_packet_size)
268 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
269 fe5feaeb Michael Niedermayer
270 90441276 Nicolas George
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
271 de6d9b64 Fabrice Bellard
}
272
273 bc5c918e Diego Biurrun
int64_t url_seek(URLContext *h, int64_t pos, int whence)
274 de6d9b64 Fabrice Bellard
{
275 bc5c918e Diego Biurrun
    int64_t ret;
276 de6d9b64 Fabrice Bellard
277
    if (!h->prot->url_seek)
278 28894105 Stefano Sabatini
        return AVERROR(ENOSYS);
279 493f54ad Michael Niedermayer
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
280 de6d9b64 Fabrice Bellard
    return ret;
281
}
282
283
int url_close(URLContext *h)
284
{
285 7a813b36 Alex Beregszaszi
    int ret = 0;
286 346db900 Thorsten Jordan
    if (!h) return 0; /* can happen when url_open fails */
287 de6d9b64 Fabrice Bellard
288 ffbb289a Martin Storsjö
    if (h->is_connected && h->prot->url_close)
289 7a813b36 Alex Beregszaszi
        ret = h->prot->url_close(h);
290 67d4b3f2 Martin Storsjö
#if CONFIG_NETWORK
291
    ff_network_close();
292
#endif
293 735cf6b2 Martin Storsjö
    if (h->prot->priv_data_size)
294
        av_free(h->priv_data);
295 1ea4f593 Fabrice Bellard
    av_free(h);
296 de6d9b64 Fabrice Bellard
    return ret;
297
}
298
299
int url_exist(const char *filename)
300
{
301
    URLContext *h;
302
    if (url_open(&h, filename, URL_RDONLY) < 0)
303
        return 0;
304
    url_close(h);
305
    return 1;
306
}
307
308 bc5c918e Diego Biurrun
int64_t url_filesize(URLContext *h)
309 de6d9b64 Fabrice Bellard
{
310 bc5c918e Diego Biurrun
    int64_t pos, size;
311 115329f1 Diego Biurrun
312 8e287af0 Michael Niedermayer
    size= url_seek(h, 0, AVSEEK_SIZE);
313
    if(size<0){
314 1ae2c5f2 Michael Niedermayer
        pos = url_seek(h, 0, SEEK_CUR);
315 eabbae73 Ronald S. Bultje
        if ((size = url_seek(h, -1, SEEK_END)) < 0)
316
            return size;
317
        size++;
318 1ae2c5f2 Michael Niedermayer
        url_seek(h, pos, SEEK_SET);
319 8e287af0 Michael Niedermayer
    }
320 de6d9b64 Fabrice Bellard
    return size;
321
}
322 8a9488b5 Fabrice Bellard
323 f0a80394 Ronald S. Bultje
int url_get_file_handle(URLContext *h)
324
{
325
    if (!h->prot->url_get_file_handle)
326
        return -1;
327
    return h->prot->url_get_file_handle(h);
328
}
329
330 8a9488b5 Fabrice Bellard
int url_get_max_packet_size(URLContext *h)
331
{
332
    return h->max_packet_size;
333
}
334 f746a046 Fabrice Bellard
335
void url_get_filename(URLContext *h, char *buf, int buf_size)
336
{
337 75e61b0e Måns Rullgård
    av_strlcpy(buf, h->filename, buf_size);
338 f746a046 Fabrice Bellard
}
339 019ac05a Fabrice Bellard
340
341
static int default_interrupt_cb(void)
342
{
343
    return 0;
344
}
345
346
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
347
{
348
    if (!interrupt_cb)
349
        interrupt_cb = default_interrupt_cb;
350
    url_interrupt_cb = interrupt_cb;
351
}
352 536333a0 Björn Axelsson
353 502bdf68 Michael Niedermayer
int av_url_read_pause(URLContext *h, int pause)
354 536333a0 Björn Axelsson
{
355
    if (!h->prot->url_read_pause)
356
        return AVERROR(ENOSYS);
357 502bdf68 Michael Niedermayer
    return h->prot->url_read_pause(h, pause);
358 536333a0 Björn Axelsson
}
359
360 bc5c918e Diego Biurrun
int64_t av_url_read_seek(URLContext *h,
361 536333a0 Björn Axelsson
        int stream_index, int64_t timestamp, int flags)
362
{
363
    if (!h->prot->url_read_seek)
364
        return AVERROR(ENOSYS);
365
    return h->prot->url_read_seek(h, stream_index, timestamp, flags);
366
}