Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 4a7a1b7d

History | View | Annotate | Download (10.6 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
/** @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
    .class_name     = "URLContext",
45
    .item_name      = urlcontext_to_name,
46
    .option         = options,
47
    .version        = LIBAVUTIL_VERSION_INT,
48
};
49
/*@}*/
50

    
51
static int default_interrupt_cb(void);
52

    
53
URLProtocol *first_protocol = NULL;
54
int (*url_interrupt_cb)(void) = default_interrupt_cb;
55

    
56
#if FF_API_OLD_AVIO
57
URLProtocol *av_protocol_next(URLProtocol *p)
58
{
59
    if(p) return p->next;
60
    else  return first_protocol;
61
}
62
#endif
63

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

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

    
89
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
90
                                   const char *filename, int flags)
91
{
92
    URLContext *uc;
93
    int err;
94

    
95
#if CONFIG_NETWORK
96
    if (!ff_network_init())
97
        return AVERROR(EIO);
98
#endif
99
    uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
100
    if (!uc) {
101
        err = AVERROR(ENOMEM);
102
        goto fail;
103
    }
104
    uc->av_class = &urlcontext_class;
105
    uc->filename = (char *) &uc[1];
106
    strcpy(uc->filename, filename);
107
    uc->prot = up;
108
    uc->flags = flags;
109
    uc->is_streamed = 0; /* default = not streamed */
110
    uc->max_packet_size = 0; /* default: stream file */
111
    if (up->priv_data_size) {
112
        uc->priv_data = av_mallocz(up->priv_data_size);
113
        if (up->priv_data_class) {
114
            *(const AVClass**)uc->priv_data = up->priv_data_class;
115
            av_opt_set_defaults(uc->priv_data);
116
        }
117
    }
118

    
119
    *puc = uc;
120
    return 0;
121
 fail:
122
    *puc = NULL;
123
#if CONFIG_NETWORK
124
    ff_network_close();
125
#endif
126
    return err;
127
}
128

    
129
int ffurl_connect(URLContext* uc)
130
{
131
    int err = uc->prot->url_open(uc, uc->filename, uc->flags);
132
    if (err)
133
        return err;
134
    uc->is_connected = 1;
135
    //We must be careful here as ffurl_seek() could be slow, for example for http
136
    if(   (uc->flags & AVIO_FLAG_WRITE)
137
       || !strcmp(uc->prot->name, "file"))
138
        if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
139
            uc->is_streamed= 1;
140
    return 0;
141
}
142

    
143
#if FF_API_OLD_AVIO
144
int url_open_protocol (URLContext **puc, struct URLProtocol *up,
145
                       const char *filename, int flags)
146
{
147
    int ret;
148

    
149
    ret = url_alloc_for_protocol(puc, up, filename, flags);
150
    if (ret)
151
        goto fail;
152
    ret = ffurl_connect(*puc);
153
    if (!ret)
154
        return 0;
155
 fail:
156
    ffurl_close(*puc);
157
    *puc = NULL;
158
    return ret;
159
}
160
int url_alloc(URLContext **puc, const char *filename, int flags)
161
{
162
    return ffurl_alloc(puc, filename, flags);
163
}
164
int url_connect(URLContext* uc)
165
{
166
    return ffurl_connect(uc);
167
}
168
int url_open(URLContext **puc, const char *filename, int flags)
169
{
170
    return ffurl_open(puc, filename, flags);
171
}
172
int url_read(URLContext *h, unsigned char *buf, int size)
173
{
174
    return ffurl_read(h, buf, size);
175
}
176
int url_read_complete(URLContext *h, unsigned char *buf, int size)
177
{
178
    return ffurl_read_complete(h, buf, size);
179
}
180
int url_write(URLContext *h, const unsigned char *buf, int size)
181
{
182
    return ffurl_write(h, buf, size);
183
}
184
int64_t url_seek(URLContext *h, int64_t pos, int whence)
185
{
186
    return ffurl_seek(h, pos, whence);
187
}
188
int url_close(URLContext *h)
189
{
190
    return ffurl_close(h);
191
}
192
int64_t url_filesize(URLContext *h)
193
{
194
    return ffurl_size(h);
195
}
196
int url_get_file_handle(URLContext *h)
197
{
198
    return ffurl_get_file_handle(h);
199
}
200
int url_get_max_packet_size(URLContext *h)
201
{
202
    return h->max_packet_size;
203
}
204
void url_get_filename(URLContext *h, char *buf, int buf_size)
205
{
206
    av_strlcpy(buf, h->filename, buf_size);
207
}
208
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
209
{
210
    avio_set_interrupt_cb(interrupt_cb);
211
}
212
int av_register_protocol2(URLProtocol *protocol, int size)
213
{
214
    return ffurl_register_protocol(protocol, size);
215
}
216
#endif
217

    
218
#define URL_SCHEME_CHARS                        \
219
    "abcdefghijklmnopqrstuvwxyz"                \
220
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
221
    "0123456789+-."
222

    
223
int ffurl_alloc(URLContext **puc, const char *filename, int flags)
224
{
225
    URLProtocol *up;
226
    char proto_str[128], proto_nested[128], *ptr;
227
    size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
228

    
229
    if (filename[proto_len] != ':' || is_dos_path(filename))
230
        strcpy(proto_str, "file");
231
    else
232
        av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
233

    
234
    av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
235
    if ((ptr = strchr(proto_nested, '+')))
236
        *ptr = '\0';
237

    
238
    up = first_protocol;
239
    while (up != NULL) {
240
        if (!strcmp(proto_str, up->name))
241
            return url_alloc_for_protocol (puc, up, filename, flags);
242
        if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
243
            !strcmp(proto_nested, up->name))
244
            return url_alloc_for_protocol (puc, up, filename, flags);
245
        up = up->next;
246
    }
247
    *puc = NULL;
248
    return AVERROR(ENOENT);
249
}
250

    
251
int ffurl_open(URLContext **puc, const char *filename, int flags)
252
{
253
    int ret = ffurl_alloc(puc, filename, flags);
254
    if (ret)
255
        return ret;
256
    ret = ffurl_connect(*puc);
257
    if (!ret)
258
        return 0;
259
    ffurl_close(*puc);
260
    *puc = NULL;
261
    return ret;
262
}
263

    
264
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
265
                                         int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
266
{
267
    int ret, len;
268
    int fast_retries = 5;
269

    
270
    len = 0;
271
    while (len < size_min) {
272
        ret = transfer_func(h, buf+len, size-len);
273
        if (ret == AVERROR(EINTR))
274
            continue;
275
        if (h->flags & AVIO_FLAG_NONBLOCK)
276
            return ret;
277
        if (ret == AVERROR(EAGAIN)) {
278
            ret = 0;
279
            if (fast_retries)
280
                fast_retries--;
281
            else
282
                usleep(1000);
283
        } else if (ret < 1)
284
            return ret < 0 ? ret : len;
285
        if (ret)
286
           fast_retries = FFMAX(fast_retries, 2);
287
        len += ret;
288
        if (url_interrupt_cb())
289
            return AVERROR_EXIT;
290
    }
291
    return len;
292
}
293

    
294
int ffurl_read(URLContext *h, unsigned char *buf, int size)
295
{
296
    if (!(h->flags & AVIO_FLAG_READ))
297
        return AVERROR(EIO);
298
    return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
299
}
300

    
301
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
302
{
303
    if (!(h->flags & AVIO_FLAG_READ))
304
        return AVERROR(EIO);
305
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
306
}
307

    
308
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
309
{
310
    if (!(h->flags & AVIO_FLAG_WRITE))
311
        return AVERROR(EIO);
312
    /* avoid sending too big packets */
313
    if (h->max_packet_size && size > h->max_packet_size)
314
        return AVERROR(EIO);
315

    
316
    return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
317
}
318

    
319
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
320
{
321
    int64_t ret;
322

    
323
    if (!h->prot->url_seek)
324
        return AVERROR(ENOSYS);
325
    ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
326
    return ret;
327
}
328

    
329
int ffurl_close(URLContext *h)
330
{
331
    int ret = 0;
332
    if (!h) return 0; /* can happen when ffurl_open fails */
333

    
334
    if (h->is_connected && h->prot->url_close)
335
        ret = h->prot->url_close(h);
336
#if CONFIG_NETWORK
337
    ff_network_close();
338
#endif
339
    if (h->prot->priv_data_size)
340
        av_free(h->priv_data);
341
    av_free(h);
342
    return ret;
343
}
344

    
345
#if FF_API_OLD_AVIO
346
int url_exist(const char *filename)
347
{
348
    URLContext *h;
349
    if (ffurl_open(&h, filename, AVIO_FLAG_READ) < 0)
350
        return 0;
351
    ffurl_close(h);
352
    return 1;
353
}
354
#endif
355

    
356
int avio_check(const char *url, int flags)
357
{
358
    URLContext *h;
359
    int ret = ffurl_alloc(&h, url, flags);
360
    if (ret)
361
        return ret;
362

    
363
    if (h->prot->url_check) {
364
        ret = h->prot->url_check(h, flags);
365
    } else {
366
        ret = ffurl_connect(h);
367
        if (ret >= 0)
368
            ret = flags;
369
    }
370

    
371
    ffurl_close(h);
372
    return ret;
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