Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 688c22e0

History | View | Annotate | Download (9.45 KB)

1
/*
2
 * Unbuffered io for ffmpeg system
3
 * Copyright (c) 2001 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
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
20
 */
21

    
22
/* needed for usleep() */
23
#define _XOPEN_SOURCE 600
24
#include <unistd.h>
25
#include "libavutil/avstring.h"
26
#include "libavutil/opt.h"
27
#include "os_support.h"
28
#include "avformat.h"
29
#if CONFIG_NETWORK
30
#include "network.h"
31
#endif
32

    
33
#if FF_API_URL_CLASS
34
/** @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
        { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
45
/*@}*/
46
#endif
47

    
48
static int default_interrupt_cb(void);
49

    
50
URLProtocol *first_protocol = NULL;
51
URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
52

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

    
59
int av_register_protocol2(URLProtocol *protocol, int size)
60
{
61
    URLProtocol **p;
62
    if (size < sizeof(URLProtocol)) {
63
        URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
64
        memcpy(temp, protocol, size);
65
        protocol = temp;
66
    }
67
    p = &first_protocol;
68
    while (*p != NULL) p = &(*p)->next;
69
    *p = protocol;
70
    protocol->next = NULL;
71
    return 0;
72
}
73

    
74
#if FF_API_REGISTER_PROTOCOL
75
/* 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
int register_protocol(URLProtocol *protocol)
92
{
93
    return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
94
}
95
#endif
96

    
97
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
98
                                   const char *filename, int flags)
99
{
100
    URLContext *uc;
101
    int err;
102

    
103
#if CONFIG_NETWORK
104
    if (!ff_network_init())
105
        return AVERROR(EIO);
106
#endif
107
    uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
108
    if (!uc) {
109
        err = AVERROR(ENOMEM);
110
        goto fail;
111
    }
112
#if FF_API_URL_CLASS
113
    uc->av_class = &urlcontext_class;
114
#endif
115
    uc->filename = (char *) &uc[1];
116
    strcpy(uc->filename, filename);
117
    uc->prot = up;
118
    uc->flags = flags;
119
    uc->is_streamed = 0; /* default = not streamed */
120
    uc->max_packet_size = 0; /* default: stream file */
121
    if (up->priv_data_size) {
122
        uc->priv_data = av_mallocz(up->priv_data_size);
123
        if (up->priv_data_class) {
124
            *(const AVClass**)uc->priv_data = up->priv_data_class;
125
            av_opt_set_defaults(uc->priv_data);
126
        }
127
    }
128

    
129
    *puc = uc;
130
    return 0;
131
 fail:
132
    *puc = NULL;
133
#if CONFIG_NETWORK
134
    ff_network_close();
135
#endif
136
    return err;
137
}
138

    
139
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
int url_open_protocol (URLContext **puc, struct URLProtocol *up,
154
                       const char *filename, int flags)
155
{
156
    int ret;
157

    
158
    ret = url_alloc_for_protocol(puc, up, filename, flags);
159
    if (ret)
160
        goto fail;
161
    ret = url_connect(*puc);
162
    if (!ret)
163
        return 0;
164
 fail:
165
    url_close(*puc);
166
    *puc = NULL;
167
    return ret;
168
}
169

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

    
175
int url_alloc(URLContext **puc, const char *filename, int flags)
176
{
177
    URLProtocol *up;
178
    char proto_str[128], proto_nested[128], *ptr;
179
    size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
180

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

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

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

    
203
int url_open(URLContext **puc, const char *filename, int flags)
204
{
205
    int ret = url_alloc(puc, filename, flags);
206
    if (ret)
207
        return ret;
208
    ret = url_connect(*puc);
209
    if (!ret)
210
        return 0;
211
    url_close(*puc);
212
    *puc = NULL;
213
    return ret;
214
}
215

    
216
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
217
                                         int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
218
{
219
    int ret, len;
220
    int fast_retries = 5;
221

    
222
    len = 0;
223
    while (len < size_min) {
224
        ret = transfer_func(h, buf+len, size-len);
225
        if (ret == AVERROR(EINTR))
226
            continue;
227
        if (h->flags & URL_FLAG_NONBLOCK)
228
            return ret;
229
        if (ret == AVERROR(EAGAIN)) {
230
            ret = 0;
231
            if (fast_retries)
232
                fast_retries--;
233
            else
234
                usleep(1000);
235
        } else if (ret < 1)
236
            return ret < 0 ? ret : len;
237
        if (ret)
238
           fast_retries = FFMAX(fast_retries, 2);
239
        len += ret;
240
        if (url_interrupt_cb())
241
            return AVERROR(EINTR);
242
    }
243
    return len;
244
}
245

    
246
int url_read(URLContext *h, unsigned char *buf, int size)
247
{
248
    if (h->flags & URL_WRONLY)
249
        return AVERROR(EIO);
250
    return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
251
}
252

    
253
int url_read_complete(URLContext *h, unsigned char *buf, int size)
254
{
255
    if (h->flags & URL_WRONLY)
256
        return AVERROR(EIO);
257
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
258
}
259

    
260
int url_write(URLContext *h, const unsigned char *buf, int size)
261
{
262
    if (!(h->flags & (URL_WRONLY | URL_RDWR)))
263
        return AVERROR(EIO);
264
    /* avoid sending too big packets */
265
    if (h->max_packet_size && size > h->max_packet_size)
266
        return AVERROR(EIO);
267

    
268
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
269
}
270

    
271
int64_t url_seek(URLContext *h, int64_t pos, int whence)
272
{
273
    int64_t ret;
274

    
275
    if (!h->prot->url_seek)
276
        return AVERROR(ENOSYS);
277
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
278
    return ret;
279
}
280

    
281
int url_close(URLContext *h)
282
{
283
    int ret = 0;
284
    if (!h) return 0; /* can happen when url_open fails */
285

    
286
    if (h->is_connected && h->prot->url_close)
287
        ret = h->prot->url_close(h);
288
#if CONFIG_NETWORK
289
    ff_network_close();
290
#endif
291
    if (h->prot->priv_data_size)
292
        av_free(h->priv_data);
293
    av_free(h);
294
    return ret;
295
}
296

    
297
int url_exist(const char *filename)
298
{
299
    URLContext *h;
300
    if (url_open(&h, filename, URL_RDONLY) < 0)
301
        return 0;
302
    url_close(h);
303
    return 1;
304
}
305

    
306
int64_t url_filesize(URLContext *h)
307
{
308
    int64_t pos, size;
309

    
310
    size= url_seek(h, 0, AVSEEK_SIZE);
311
    if(size<0){
312
        pos = url_seek(h, 0, SEEK_CUR);
313
        if ((size = url_seek(h, -1, SEEK_END)) < 0)
314
            return size;
315
        size++;
316
        url_seek(h, pos, SEEK_SET);
317
    }
318
    return size;
319
}
320

    
321
int url_get_file_handle(URLContext *h)
322
{
323
    if (!h->prot->url_get_file_handle)
324
        return -1;
325
    return h->prot->url_get_file_handle(h);
326
}
327

    
328
int url_get_max_packet_size(URLContext *h)
329
{
330
    return h->max_packet_size;
331
}
332

    
333
void url_get_filename(URLContext *h, char *buf, int buf_size)
334
{
335
    av_strlcpy(buf, h->filename, buf_size);
336
}
337

    
338

    
339
static int default_interrupt_cb(void)
340
{
341
    return 0;
342
}
343

    
344
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
345
{
346
    if (!interrupt_cb)
347
        interrupt_cb = default_interrupt_cb;
348
    url_interrupt_cb = interrupt_cb;
349
}
350

    
351
int av_url_read_pause(URLContext *h, int pause)
352
{
353
    if (!h->prot->url_read_pause)
354
        return AVERROR(ENOSYS);
355
    return h->prot->url_read_pause(h, pause);
356
}
357

    
358
int64_t av_url_read_seek(URLContext *h,
359
        int stream_index, int64_t timestamp, int flags)
360
{
361
    if (!h->prot->url_read_seek)
362
        return AVERROR(ENOSYS);
363
    return h->prot->url_read_seek(h, stream_index, timestamp, flags);
364
}