Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 47772399

History | View | Annotate | Download (4.4 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
#include "avformat.h"
22
#include "avstring.h"
23

    
24
static int default_interrupt_cb(void);
25

    
26
URLProtocol *first_protocol = NULL;
27
URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
28

    
29
int register_protocol(URLProtocol *protocol)
30
{
31
    URLProtocol **p;
32
    p = &first_protocol;
33
    while (*p != NULL) p = &(*p)->next;
34
    *p = protocol;
35
    protocol->next = NULL;
36
    return 0;
37
}
38

    
39
int url_open(URLContext **puc, const char *filename, int flags)
40
{
41
    URLContext *uc;
42
    URLProtocol *up;
43
    const char *p;
44
    char proto_str[128], *q;
45
    int err;
46

    
47
    p = filename;
48
    q = proto_str;
49
    while (*p != '\0' && *p != ':') {
50
        /* protocols can only contain alphabetic chars */
51
        if (!isalpha(*p))
52
            goto file_proto;
53
        if ((q - proto_str) < sizeof(proto_str) - 1)
54
            *q++ = *p;
55
        p++;
56
    }
57
    /* if the protocol has length 1, we consider it is a dos drive */
58
    if (*p == '\0' || (q - proto_str) <= 1) {
59
    file_proto:
60
        strcpy(proto_str, "file");
61
    } else {
62
        *q = '\0';
63
    }
64

    
65
    up = first_protocol;
66
    while (up != NULL) {
67
        if (!strcmp(proto_str, up->name))
68
            goto found;
69
        up = up->next;
70
    }
71
    err = AVERROR(ENOENT);
72
    goto fail;
73
 found:
74
    uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
75
    if (!uc) {
76
        err = AVERROR(ENOMEM);
77
        goto fail;
78
    }
79
#if LIBAVFORMAT_VERSION_INT >= (52<<16)
80
    uc->filename = (char *) &uc[1];
81
#endif
82
    strcpy(uc->filename, filename);
83
    uc->prot = up;
84
    uc->flags = flags;
85
    uc->is_streamed = 0; /* default = not streamed */
86
    uc->max_packet_size = 0; /* default: stream file */
87
    err = up->url_open(uc, filename, flags);
88
    if (err < 0) {
89
        av_free(uc);
90
        *puc = NULL;
91
        return err;
92
    }
93
    *puc = uc;
94
    return 0;
95
 fail:
96
    *puc = NULL;
97
    return err;
98
}
99

    
100
int url_read(URLContext *h, unsigned char *buf, int size)
101
{
102
    int ret;
103
    if (h->flags & URL_WRONLY)
104
        return AVERROR(EIO);
105
    ret = h->prot->url_read(h, buf, size);
106
    return ret;
107
}
108

    
109
#if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS)
110
int url_write(URLContext *h, unsigned char *buf, int size)
111
{
112
    int ret;
113
    if (!(h->flags & (URL_WRONLY | URL_RDWR)))
114
        return AVERROR(EIO);
115
    /* avoid sending too big packets */
116
    if (h->max_packet_size && size > h->max_packet_size)
117
        return AVERROR(EIO);
118
    ret = h->prot->url_write(h, buf, size);
119
    return ret;
120
}
121
#endif //CONFIG_MUXERS || CONFIG_PROTOCOLS
122

    
123
offset_t url_seek(URLContext *h, offset_t pos, int whence)
124
{
125
    offset_t ret;
126

    
127
    if (!h->prot->url_seek)
128
        return AVERROR(EPIPE);
129
    ret = h->prot->url_seek(h, pos, whence);
130
    return ret;
131
}
132

    
133
int url_close(URLContext *h)
134
{
135
    int ret;
136

    
137
    ret = h->prot->url_close(h);
138
    av_free(h);
139
    return ret;
140
}
141

    
142
int url_exist(const char *filename)
143
{
144
    URLContext *h;
145
    if (url_open(&h, filename, URL_RDONLY) < 0)
146
        return 0;
147
    url_close(h);
148
    return 1;
149
}
150

    
151
offset_t url_filesize(URLContext *h)
152
{
153
    offset_t pos, size;
154

    
155
    size= url_seek(h, 0, AVSEEK_SIZE);
156
    if(size<0){
157
        pos = url_seek(h, 0, SEEK_CUR);
158
        if ((size = url_seek(h, -1, SEEK_END)) < 0)
159
            return size;
160
        size++;
161
        url_seek(h, pos, SEEK_SET);
162
    }
163
    return size;
164
}
165

    
166
int url_get_max_packet_size(URLContext *h)
167
{
168
    return h->max_packet_size;
169
}
170

    
171
void url_get_filename(URLContext *h, char *buf, int buf_size)
172
{
173
    av_strlcpy(buf, h->filename, buf_size);
174
}
175

    
176

    
177
static int default_interrupt_cb(void)
178
{
179
    return 0;
180
}
181

    
182
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
183
{
184
    if (!interrupt_cb)
185
        interrupt_cb = default_interrupt_cb;
186
    url_interrupt_cb = interrupt_cb;
187
}