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