Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 890d2799

History | View | Annotate | Download (5.01 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 = 0;
136
    if (!h) return 0; /* can happen when url_open fails */
137

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

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

    
153
offset_t url_filesize(URLContext *h)
154
{
155
    offset_t pos, size;
156

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

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

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

    
178

    
179
static int default_interrupt_cb(void)
180
{
181
    return 0;
182
}
183

    
184
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
185
{
186
    if (!interrupt_cb)
187
        interrupt_cb = default_interrupt_cb;
188
    url_interrupt_cb = interrupt_cb;
189
}
190

    
191
int av_url_read_play(URLContext *h)
192
{
193
    if (!h->prot->url_read_play)
194
        return AVERROR(ENOSYS);
195
    return h->prot->url_read_play(h);
196
}
197

    
198
int av_url_read_pause(URLContext *h)
199
{
200
    if (!h->prot->url_read_pause)
201
        return AVERROR(ENOSYS);
202
    return h->prot->url_read_pause(h);
203
}
204

    
205
int av_url_read_seek(URLContext *h,
206
        int stream_index, int64_t timestamp, int flags)
207
{
208
    if (!h->prot->url_read_seek)
209
        return AVERROR(ENOSYS);
210
    return h->prot->url_read_seek(h, stream_index, timestamp, flags);
211
}