Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 5f8e0227

History | View | Annotate | Download (5.6 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 245976da Diego Biurrun
22
#include "libavutil/avstring.h"
23
#include "libavcodec/opt.h"
24 de6d9b64 Fabrice Bellard
#include "avformat.h"
25 5acef35f Björn Axelsson
26
#if LIBAVFORMAT_VERSION_MAJOR >= 53
27
/** @name Logging context. */
28
/*@{*/
29
static const char *urlcontext_to_name(void *ptr)
30
{
31
    URLContext *h = (URLContext *)ptr;
32
    if(h->prot) return h->prot->name;
33
    else        return "NULL";
34
}
35
static const AVOption options[] = {{NULL}};
36
static const AVClass urlcontext_class =
37
        { "URLContext", urlcontext_to_name, options };
38
/*@}*/
39
#endif
40 de6d9b64 Fabrice Bellard
41 019ac05a Fabrice Bellard
static int default_interrupt_cb(void);
42
43 de6d9b64 Fabrice Bellard
URLProtocol *first_protocol = NULL;
44 019ac05a Fabrice Bellard
URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
45 de6d9b64 Fabrice Bellard
46 84be6e72 Michael Niedermayer
URLProtocol *av_protocol_next(URLProtocol *p)
47
{
48
    if(p) return p->next;
49
    else  return first_protocol;
50
}
51
52 de6d9b64 Fabrice Bellard
int register_protocol(URLProtocol *protocol)
53
{
54
    URLProtocol **p;
55
    p = &first_protocol;
56
    while (*p != NULL) p = &(*p)->next;
57
    *p = protocol;
58
    protocol->next = NULL;
59
    return 0;
60
}
61
62
int url_open(URLContext **puc, const char *filename, int flags)
63
{
64
    URLContext *uc;
65
    URLProtocol *up;
66
    const char *p;
67
    char proto_str[128], *q;
68
    int err;
69
70
    p = filename;
71
    q = proto_str;
72
    while (*p != '\0' && *p != ':') {
73 5d5fef0a Fabrice Bellard
        /* protocols can only contain alphabetic chars */
74
        if (!isalpha(*p))
75
            goto file_proto;
76 de6d9b64 Fabrice Bellard
        if ((q - proto_str) < sizeof(proto_str) - 1)
77
            *q++ = *p;
78
        p++;
79
    }
80 a74127c0 Fabrice Bellard
    /* if the protocol has length 1, we consider it is a dos drive */
81
    if (*p == '\0' || (q - proto_str) <= 1) {
82 5d5fef0a Fabrice Bellard
    file_proto:
83 de6d9b64 Fabrice Bellard
        strcpy(proto_str, "file");
84
    } else {
85
        *q = '\0';
86
    }
87 115329f1 Diego Biurrun
88 de6d9b64 Fabrice Bellard
    up = first_protocol;
89
    while (up != NULL) {
90 a74127c0 Fabrice Bellard
        if (!strcmp(proto_str, up->name))
91 de6d9b64 Fabrice Bellard
            goto found;
92
        up = up->next;
93
    }
94 8fa36ae0 François Revol
    err = AVERROR(ENOENT);
95 8a9488b5 Fabrice Bellard
    goto fail;
96 de6d9b64 Fabrice Bellard
 found:
97 62181004 Ronald S. Bultje
    uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
98 8a9488b5 Fabrice Bellard
    if (!uc) {
99 8fa36ae0 François Revol
        err = AVERROR(ENOMEM);
100 8a9488b5 Fabrice Bellard
        goto fail;
101
    }
102 5acef35f Björn Axelsson
#if LIBAVFORMAT_VERSION_MAJOR >= 53
103
    uc->av_class = &urlcontext_class;
104
#endif
105 62181004 Ronald S. Bultje
    uc->filename = (char *) &uc[1];
106 f746a046 Fabrice Bellard
    strcpy(uc->filename, filename);
107 de6d9b64 Fabrice Bellard
    uc->prot = up;
108
    uc->flags = flags;
109
    uc->is_streamed = 0; /* default = not streamed */
110 8a9488b5 Fabrice Bellard
    uc->max_packet_size = 0; /* default: stream file */
111 de6d9b64 Fabrice Bellard
    err = up->url_open(uc, filename, flags);
112
    if (err < 0) {
113 1ea4f593 Fabrice Bellard
        av_free(uc);
114 de6d9b64 Fabrice Bellard
        *puc = NULL;
115
        return err;
116
    }
117 abbae514 Michael Niedermayer
118
    //We must be carefull here as url_seek() could be slow, for example for http
119
    if(   (flags & (URL_WRONLY | URL_RDWR))
120
       || !strcmp(proto_str, "file"))
121
        if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
122
            uc->is_streamed= 1;
123 de6d9b64 Fabrice Bellard
    *puc = uc;
124
    return 0;
125 8a9488b5 Fabrice Bellard
 fail:
126
    *puc = NULL;
127
    return err;
128 de6d9b64 Fabrice Bellard
}
129
130
int url_read(URLContext *h, unsigned char *buf, int size)
131
{
132
    int ret;
133
    if (h->flags & URL_WRONLY)
134 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
135 de6d9b64 Fabrice Bellard
    ret = h->prot->url_read(h, buf, size);
136
    return ret;
137
}
138
139
int url_write(URLContext *h, unsigned char *buf, int size)
140
{
141
    int ret;
142 8a9488b5 Fabrice Bellard
    if (!(h->flags & (URL_WRONLY | URL_RDWR)))
143 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
144 8a9488b5 Fabrice Bellard
    /* avoid sending too big packets */
145
    if (h->max_packet_size && size > h->max_packet_size)
146 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
147 de6d9b64 Fabrice Bellard
    ret = h->prot->url_write(h, buf, size);
148
    return ret;
149
}
150
151
offset_t url_seek(URLContext *h, offset_t pos, int whence)
152
{
153
    offset_t ret;
154
155
    if (!h->prot->url_seek)
156 8fa36ae0 François Revol
        return AVERROR(EPIPE);
157 de6d9b64 Fabrice Bellard
    ret = h->prot->url_seek(h, pos, whence);
158
    return ret;
159
}
160
161
int url_close(URLContext *h)
162
{
163 7a813b36 Alex Beregszaszi
    int ret = 0;
164 346db900 Thorsten Jordan
    if (!h) return 0; /* can happen when url_open fails */
165 de6d9b64 Fabrice Bellard
166 7a813b36 Alex Beregszaszi
    if (h->prot->url_close)
167
        ret = h->prot->url_close(h);
168 1ea4f593 Fabrice Bellard
    av_free(h);
169 de6d9b64 Fabrice Bellard
    return ret;
170
}
171
172
int url_exist(const char *filename)
173
{
174
    URLContext *h;
175
    if (url_open(&h, filename, URL_RDONLY) < 0)
176
        return 0;
177
    url_close(h);
178
    return 1;
179
}
180
181
offset_t url_filesize(URLContext *h)
182
{
183
    offset_t pos, size;
184 115329f1 Diego Biurrun
185 8e287af0 Michael Niedermayer
    size= url_seek(h, 0, AVSEEK_SIZE);
186
    if(size<0){
187 1ae2c5f2 Michael Niedermayer
        pos = url_seek(h, 0, SEEK_CUR);
188 eabbae73 Ronald S. Bultje
        if ((size = url_seek(h, -1, SEEK_END)) < 0)
189
            return size;
190
        size++;
191 1ae2c5f2 Michael Niedermayer
        url_seek(h, pos, SEEK_SET);
192 8e287af0 Michael Niedermayer
    }
193 de6d9b64 Fabrice Bellard
    return size;
194
}
195 8a9488b5 Fabrice Bellard
196
int url_get_max_packet_size(URLContext *h)
197
{
198
    return h->max_packet_size;
199
}
200 f746a046 Fabrice Bellard
201
void url_get_filename(URLContext *h, char *buf, int buf_size)
202
{
203 75e61b0e Måns Rullgård
    av_strlcpy(buf, h->filename, buf_size);
204 f746a046 Fabrice Bellard
}
205 019ac05a Fabrice Bellard
206
207
static int default_interrupt_cb(void)
208
{
209
    return 0;
210
}
211
212
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
213
{
214
    if (!interrupt_cb)
215
        interrupt_cb = default_interrupt_cb;
216
    url_interrupt_cb = interrupt_cb;
217
}
218 536333a0 Björn Axelsson
219 502bdf68 Michael Niedermayer
int av_url_read_pause(URLContext *h, int pause)
220 536333a0 Björn Axelsson
{
221
    if (!h->prot->url_read_pause)
222
        return AVERROR(ENOSYS);
223 502bdf68 Michael Niedermayer
    return h->prot->url_read_pause(h, pause);
224 536333a0 Björn Axelsson
}
225
226 b075e520 Michael Niedermayer
offset_t av_url_read_seek(URLContext *h,
227 536333a0 Björn Axelsson
        int stream_index, int64_t timestamp, int flags)
228
{
229
    if (!h->prot->url_read_seek)
230
        return AVERROR(ENOSYS);
231
    return h->prot->url_read_seek(h, stream_index, timestamp, flags);
232
}