Statistics
| Branch: | Revision:

ffmpeg / libavformat / avio.c @ 19f4ceca

History | View | Annotate | Download (4.4 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 0bd586c5 Mike Melanson
        return AVERROR_IO;
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 0bd586c5 Mike Melanson
        return AVERROR_IO;
115 8a9488b5 Fabrice Bellard
    /* avoid sending too big packets */
116
    if (h->max_packet_size && size > h->max_packet_size)
117 115329f1 Diego Biurrun
        return AVERROR_IO;
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
    int ret;
136
137
    ret = h->prot->url_close(h);
138 1ea4f593 Fabrice Bellard
    av_free(h);
139 de6d9b64 Fabrice Bellard
    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 115329f1 Diego Biurrun
155 8e287af0 Michael Niedermayer
    size= url_seek(h, 0, AVSEEK_SIZE);
156
    if(size<0){
157 1ae2c5f2 Michael Niedermayer
        pos = url_seek(h, 0, SEEK_CUR);
158 eabbae73 Ronald S. Bultje
        if ((size = url_seek(h, -1, SEEK_END)) < 0)
159
            return size;
160
        size++;
161 1ae2c5f2 Michael Niedermayer
        url_seek(h, pos, SEEK_SET);
162 8e287af0 Michael Niedermayer
    }
163 de6d9b64 Fabrice Bellard
    return size;
164
}
165 8a9488b5 Fabrice Bellard
166
int url_get_max_packet_size(URLContext *h)
167
{
168
    return h->max_packet_size;
169
}
170 f746a046 Fabrice Bellard
171
void url_get_filename(URLContext *h, char *buf, int buf_size)
172
{
173 75e61b0e Måns Rullgård
    av_strlcpy(buf, h->filename, buf_size);
174 f746a046 Fabrice Bellard
}
175 019ac05a Fabrice Bellard
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
}