Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ cdc6a87f

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
#if FF_API_OLD_AVIO
55
URLProtocol *av_protocol_next(URLProtocol *p)
56
{
57
    if(p) return p->next;
58
    else  return first_protocol;
59
}
60
#endif
61

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

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

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

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

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

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

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

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

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

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

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

    
241
#define URL_SCHEME_CHARS                        \
242
    "abcdefghijklmnopqrstuvwxyz"                \
243
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
244
    "0123456789+-."
245

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

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

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

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

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

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

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

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

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

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

    
339
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
340
}
341

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

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

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

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

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

    
377
int64_t ffurl_size(URLContext *h)
378
{
379
    int64_t pos, size;
380

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

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

    
399
static int default_interrupt_cb(void)
400
{
401
    return 0;
402
}
403

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

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

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