Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 890d2799

History | View | Annotate | Download (5.01 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Unbuffered io for ffmpeg system
3 19720f15 Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21
#include "avformat.h"
22 75e61b0e Måns Rullgård
#include "avstring.h"
23 de6d9b64 Fabrice Bellard
24 019ac05a Fabrice Bellard
static int default_interrupt_cb(void);
25
26 de6d9b64 Fabrice Bellard
URLProtocol *first_protocol = NULL;
27 019ac05a Fabrice Bellard
URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
28 de6d9b64 Fabrice Bellard
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 5d5fef0a Fabrice Bellard
        /* protocols can only contain alphabetic chars */
51
        if (!isalpha(*p))
52
            goto file_proto;
53 de6d9b64 Fabrice Bellard
        if ((q - proto_str) < sizeof(proto_str) - 1)
54
            *q++ = *p;
55
        p++;
56
    }
57 a74127c0 Fabrice Bellard
    /* if the protocol has length 1, we consider it is a dos drive */
58
    if (*p == '\0' || (q - proto_str) <= 1) {
59 5d5fef0a Fabrice Bellard
    file_proto:
60 de6d9b64 Fabrice Bellard
        strcpy(proto_str, "file");
61
    } else {
62
        *q = '\0';
63
    }
64 115329f1 Diego Biurrun
65 de6d9b64 Fabrice Bellard
    up = first_protocol;
66
    while (up != NULL) {
67 a74127c0 Fabrice Bellard
        if (!strcmp(proto_str, up->name))
68 de6d9b64 Fabrice Bellard
            goto found;
69
        up = up->next;
70
    }
71 8fa36ae0 François Revol
    err = AVERROR(ENOENT);
72 8a9488b5 Fabrice Bellard
    goto fail;
73 de6d9b64 Fabrice Bellard
 found:
74 62181004 Ronald S. Bultje
    uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
75 8a9488b5 Fabrice Bellard
    if (!uc) {
76 8fa36ae0 François Revol
        err = AVERROR(ENOMEM);
77 8a9488b5 Fabrice Bellard
        goto fail;
78
    }
79 62181004 Ronald S. Bultje
#if LIBAVFORMAT_VERSION_INT >= (52<<16)
80
    uc->filename = (char *) &uc[1];
81
#endif
82 f746a046 Fabrice Bellard
    strcpy(uc->filename, filename);
83 de6d9b64 Fabrice Bellard
    uc->prot = up;
84
    uc->flags = flags;
85
    uc->is_streamed = 0; /* default = not streamed */
86 8a9488b5 Fabrice Bellard
    uc->max_packet_size = 0; /* default: stream file */
87 de6d9b64 Fabrice Bellard
    err = up->url_open(uc, filename, flags);
88
    if (err < 0) {
89 1ea4f593 Fabrice Bellard
        av_free(uc);
90 de6d9b64 Fabrice Bellard
        *puc = NULL;
91
        return err;
92
    }
93
    *puc = uc;
94
    return 0;
95 8a9488b5 Fabrice Bellard
 fail:
96
    *puc = NULL;
97
    return err;
98 de6d9b64 Fabrice Bellard
}
99
100
int url_read(URLContext *h, unsigned char *buf, int size)
101
{
102
    int ret;
103
    if (h->flags & URL_WRONLY)
104 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
105 de6d9b64 Fabrice Bellard
    ret = h->prot->url_read(h, buf, size);
106
    return ret;
107
}
108
109 04f46ced Gildas Bazin
#if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS)
110 de6d9b64 Fabrice Bellard
int url_write(URLContext *h, unsigned char *buf, int size)
111
{
112
    int ret;
113 8a9488b5 Fabrice Bellard
    if (!(h->flags & (URL_WRONLY | URL_RDWR)))
114 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
115 8a9488b5 Fabrice Bellard
    /* avoid sending too big packets */
116
    if (h->max_packet_size && size > h->max_packet_size)
117 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
118 de6d9b64 Fabrice Bellard
    ret = h->prot->url_write(h, buf, size);
119
    return ret;
120
}
121 04f46ced Gildas Bazin
#endif //CONFIG_MUXERS || CONFIG_PROTOCOLS
122 de6d9b64 Fabrice Bellard
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 8fa36ae0 François Revol
        return AVERROR(EPIPE);
129 de6d9b64 Fabrice Bellard
    ret = h->prot->url_seek(h, pos, whence);
130
    return ret;
131
}
132
133
int url_close(URLContext *h)
134
{
135 7a813b36 Alex Beregszaszi
    int ret = 0;
136 346db900 Thorsten Jordan
    if (!h) return 0; /* can happen when url_open fails */
137 de6d9b64 Fabrice Bellard
138 7a813b36 Alex Beregszaszi
    if (h->prot->url_close)
139
        ret = h->prot->url_close(h);
140 1ea4f593 Fabrice Bellard
    av_free(h);
141 de6d9b64 Fabrice Bellard
    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 115329f1 Diego Biurrun
157 8e287af0 Michael Niedermayer
    size= url_seek(h, 0, AVSEEK_SIZE);
158
    if(size<0){
159 1ae2c5f2 Michael Niedermayer
        pos = url_seek(h, 0, SEEK_CUR);
160 eabbae73 Ronald S. Bultje
        if ((size = url_seek(h, -1, SEEK_END)) < 0)
161
            return size;
162
        size++;
163 1ae2c5f2 Michael Niedermayer
        url_seek(h, pos, SEEK_SET);
164 8e287af0 Michael Niedermayer
    }
165 de6d9b64 Fabrice Bellard
    return size;
166
}
167 8a9488b5 Fabrice Bellard
168
int url_get_max_packet_size(URLContext *h)
169
{
170
    return h->max_packet_size;
171
}
172 f746a046 Fabrice Bellard
173
void url_get_filename(URLContext *h, char *buf, int buf_size)
174
{
175 75e61b0e Måns Rullgård
    av_strlcpy(buf, h->filename, buf_size);
176 f746a046 Fabrice Bellard
}
177 019ac05a Fabrice Bellard
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 536333a0 Björn Axelsson
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
}