Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ f8270bbf

History | View | Annotate | Download (11 KB)

1
/*
2
 * Unbuffered io for ffmpeg system
3
 * Copyright (c) 2001 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav 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
 * Libav 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 Libav; 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
#include "url.h"
33

    
34
#if FF_API_URL_CLASS
35
/** @name Logging context. */
36
/*@{*/
37
static const char *urlcontext_to_name(void *ptr)
38
{
39
    URLContext *h = (URLContext *)ptr;
40
    if(h->prot) return h->prot->name;
41
    else        return "NULL";
42
}
43
static const AVOption options[] = {{NULL}};
44
static const AVClass urlcontext_class =
45
        { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
46
/*@}*/
47
#endif
48

    
49
static int default_interrupt_cb(void);
50

    
51
URLProtocol *first_protocol = NULL;
52
int (*url_interrupt_cb)(void) = default_interrupt_cb;
53

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

    
60
const char *avio_enum_protocols(void **opaque, int output)
61
{
62
    URLProtocol **p = opaque;
63
    *p = *p ? (*p)->next : first_protocol;
64
    if (!*p) return NULL;
65
    if ((output && (*p)->url_write) || (!output && (*p)->url_read))
66
        return (*p)->name;
67
    return avio_enum_protocols(opaque, output);
68
}
69

    
70
int ffurl_register_protocol(URLProtocol *protocol, int size)
71
{
72
    URLProtocol **p;
73
    if (size < sizeof(URLProtocol)) {
74
        URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
75
        memcpy(temp, protocol, size);
76
        protocol = temp;
77
    }
78
    p = &first_protocol;
79
    while (*p != NULL) p = &(*p)->next;
80
    *p = protocol;
81
    protocol->next = NULL;
82
    return 0;
83
}
84

    
85
#if FF_API_REGISTER_PROTOCOL
86
/* The layout of URLProtocol as of when major was bumped to 52 */
87
struct URLProtocol_compat {
88
    const char *name;
89
    int (*url_open)(URLContext *h, const char *filename, int flags);
90
    int (*url_read)(URLContext *h, unsigned char *buf, int size);
91
    int (*url_write)(URLContext *h, unsigned char *buf, int size);
92
    int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
93
    int (*url_close)(URLContext *h);
94
    struct URLProtocol *next;
95
};
96

    
97
int av_register_protocol(URLProtocol *protocol)
98
{
99
    return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
100
}
101

    
102
int register_protocol(URLProtocol *protocol)
103
{
104
    return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
105
}
106
#endif
107

    
108
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
109
                                   const char *filename, int flags)
110
{
111
    URLContext *uc;
112
    int err;
113

    
114
#if CONFIG_NETWORK
115
    if (!ff_network_init())
116
        return AVERROR(EIO);
117
#endif
118
    uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
119
    if (!uc) {
120
        err = AVERROR(ENOMEM);
121
        goto fail;
122
    }
123
#if FF_API_URL_CLASS
124
    uc->av_class = &urlcontext_class;
125
#endif
126
    uc->filename = (char *) &uc[1];
127
    strcpy(uc->filename, filename);
128
    uc->prot = up;
129
    uc->flags = flags;
130
    uc->is_streamed = 0; /* default = not streamed */
131
    uc->max_packet_size = 0; /* default: stream file */
132
    if (up->priv_data_size) {
133
        uc->priv_data = av_mallocz(up->priv_data_size);
134
        if (up->priv_data_class) {
135
            *(const AVClass**)uc->priv_data = up->priv_data_class;
136
            av_opt_set_defaults(uc->priv_data);
137
        }
138
    }
139

    
140
    *puc = uc;
141
    return 0;
142
 fail:
143
    *puc = NULL;
144
#if CONFIG_NETWORK
145
    ff_network_close();
146
#endif
147
    return err;
148
}
149

    
150
int ffurl_connect(URLContext* uc)
151
{
152
    int err = uc->prot->url_open(uc, uc->filename, uc->flags);
153
    if (err)
154
        return err;
155
    uc->is_connected = 1;
156
    //We must be careful here as ffurl_seek() could be slow, for example for http
157
    if(   (uc->flags & (AVIO_WRONLY | AVIO_RDWR))
158
       || !strcmp(uc->prot->name, "file"))
159
        if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
160
            uc->is_streamed= 1;
161
    return 0;
162
}
163

    
164
#if FF_API_OLD_AVIO
165
int url_open_protocol (URLContext **puc, struct URLProtocol *up,
166
                       const char *filename, int flags)
167
{
168
    int ret;
169

    
170
    ret = url_alloc_for_protocol(puc, up, filename, flags);
171
    if (ret)
172
        goto fail;
173
    ret = ffurl_connect(*puc);
174
    if (!ret)
175
        return 0;
176
 fail:
177
    ffurl_close(*puc);
178
    *puc = NULL;
179
    return ret;
180
}
181
int url_alloc(URLContext **puc, const char *filename, int flags)
182
{
183
    return ffurl_alloc(puc, filename, flags);
184
}
185
int url_connect(URLContext* uc)
186
{
187
    return ffurl_connect(uc);
188
}
189
int url_open(URLContext **puc, const char *filename, int flags)
190
{
191
    return ffurl_open(puc, filename, flags);
192
}
193
int url_read(URLContext *h, unsigned char *buf, int size)
194
{
195
    return ffurl_read(h, buf, size);
196
}
197
int url_read_complete(URLContext *h, unsigned char *buf, int size)
198
{
199
    return ffurl_read_complete(h, buf, size);
200
}
201
int url_write(URLContext *h, const unsigned char *buf, int size)
202
{
203
    return ffurl_write(h, buf, size);
204
}
205
int64_t url_seek(URLContext *h, int64_t pos, int whence)
206
{
207
    return ffurl_seek(h, pos, whence);
208
}
209
int url_close(URLContext *h)
210
{
211
    return ffurl_close(h);
212
}
213
int64_t url_filesize(URLContext *h)
214
{
215
    return ffurl_size(h);
216
}
217
int url_get_file_handle(URLContext *h)
218
{
219
    return ffurl_get_file_handle(h);
220
}
221
int url_get_max_packet_size(URLContext *h)
222
{
223
    return h->max_packet_size;
224
}
225
void url_get_filename(URLContext *h, char *buf, int buf_size)
226
{
227
    av_strlcpy(buf, h->filename, buf_size);
228
}
229
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
230
{
231
    avio_set_interrupt_cb(interrupt_cb);
232
}
233
int av_register_protocol2(URLProtocol *protocol, int size)
234
{
235
    return ffurl_register_protocol(protocol, size);
236
}
237
#endif
238

    
239
#define URL_SCHEME_CHARS                        \
240
    "abcdefghijklmnopqrstuvwxyz"                \
241
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
242
    "0123456789+-."
243

    
244
int ffurl_alloc(URLContext **puc, const char *filename, int flags)
245
{
246
    URLProtocol *up;
247
    char proto_str[128], proto_nested[128], *ptr;
248
    size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
249

    
250
    if (filename[proto_len] != ':' || is_dos_path(filename))
251
        strcpy(proto_str, "file");
252
    else
253
        av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
254

    
255
    av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
256
    if ((ptr = strchr(proto_nested, '+')))
257
        *ptr = '\0';
258

    
259
    up = first_protocol;
260
    while (up != NULL) {
261
        if (!strcmp(proto_str, up->name))
262
            return url_alloc_for_protocol (puc, up, filename, flags);
263
        if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
264
            !strcmp(proto_nested, up->name))
265
            return url_alloc_for_protocol (puc, up, filename, flags);
266
        up = up->next;
267
    }
268
    *puc = NULL;
269
    return AVERROR(ENOENT);
270
}
271

    
272
int ffurl_open(URLContext **puc, const char *filename, int flags)
273
{
274
    int ret = ffurl_alloc(puc, filename, flags);
275
    if (ret)
276
        return ret;
277
    ret = ffurl_connect(*puc);
278
    if (!ret)
279
        return 0;
280
    ffurl_close(*puc);
281
    *puc = NULL;
282
    return ret;
283
}
284

    
285
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
286
                                         int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
287
{
288
    int ret, len;
289
    int fast_retries = 5;
290

    
291
    len = 0;
292
    while (len < size_min) {
293
        ret = transfer_func(h, buf+len, size-len);
294
        if (ret == AVERROR(EINTR))
295
            continue;
296
        if (h->flags & AVIO_FLAG_NONBLOCK)
297
            return ret;
298
        if (ret == AVERROR(EAGAIN)) {
299
            ret = 0;
300
            if (fast_retries)
301
                fast_retries--;
302
            else
303
                usleep(1000);
304
        } else if (ret < 1)
305
            return ret < 0 ? ret : len;
306
        if (ret)
307
           fast_retries = FFMAX(fast_retries, 2);
308
        len += ret;
309
        if (url_interrupt_cb())
310
            return AVERROR_EXIT;
311
    }
312
    return len;
313
}
314

    
315
int ffurl_read(URLContext *h, unsigned char *buf, int size)
316
{
317
    if (h->flags & AVIO_WRONLY)
318
        return AVERROR(EIO);
319
    return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
320
}
321

    
322
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
323
{
324
    if (h->flags & AVIO_WRONLY)
325
        return AVERROR(EIO);
326
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
327
}
328

    
329
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
330
{
331
    if (!(h->flags & (AVIO_WRONLY | AVIO_RDWR)))
332
        return AVERROR(EIO);
333
    /* avoid sending too big packets */
334
    if (h->max_packet_size && size > h->max_packet_size)
335
        return AVERROR(EIO);
336

    
337
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
338
}
339

    
340
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
341
{
342
    int64_t ret;
343

    
344
    if (!h->prot->url_seek)
345
        return AVERROR(ENOSYS);
346
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
347
    return ret;
348
}
349

    
350
int ffurl_close(URLContext *h)
351
{
352
    int ret = 0;
353
    if (!h) return 0; /* can happen when ffurl_open fails */
354

    
355
    if (h->is_connected && h->prot->url_close)
356
        ret = h->prot->url_close(h);
357
#if CONFIG_NETWORK
358
    ff_network_close();
359
#endif
360
    if (h->prot->priv_data_size)
361
        av_free(h->priv_data);
362
    av_free(h);
363
    return ret;
364
}
365

    
366
int url_exist(const char *filename)
367
{
368
    URLContext *h;
369
    if (ffurl_open(&h, filename, AVIO_RDONLY) < 0)
370
        return 0;
371
    ffurl_close(h);
372
    return 1;
373
}
374

    
375
int64_t ffurl_size(URLContext *h)
376
{
377
    int64_t pos, size;
378

    
379
    size= ffurl_seek(h, 0, AVSEEK_SIZE);
380
    if(size<0){
381
        pos = ffurl_seek(h, 0, SEEK_CUR);
382
        if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
383
            return size;
384
        size++;
385
        ffurl_seek(h, pos, SEEK_SET);
386
    }
387
    return size;
388
}
389

    
390
int ffurl_get_file_handle(URLContext *h)
391
{
392
    if (!h->prot->url_get_file_handle)
393
        return -1;
394
    return h->prot->url_get_file_handle(h);
395
}
396

    
397
static int default_interrupt_cb(void)
398
{
399
    return 0;
400
}
401

    
402
void avio_set_interrupt_cb(int (*interrupt_cb)(void))
403
{
404
    if (!interrupt_cb)
405
        interrupt_cb = default_interrupt_cb;
406
    url_interrupt_cb = interrupt_cb;
407
}
408

    
409
#if FF_API_OLD_AVIO
410
int av_url_read_pause(URLContext *h, int pause)
411
{
412
    if (!h->prot->url_read_pause)
413
        return AVERROR(ENOSYS);
414
    return h->prot->url_read_pause(h, pause);
415
}
416

    
417
int64_t av_url_read_seek(URLContext *h,
418
        int stream_index, int64_t timestamp, int flags)
419
{
420
    if (!h->prot->url_read_seek)
421
        return AVERROR(ENOSYS);
422
    return h->prot->url_read_seek(h, stream_index, timestamp, flags);
423
}
424
#endif