Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 99ed41a8

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