]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
Add an av_register_protocol2 function that takes a size parameter
[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
22 /* needed for usleep() */
23 #define _XOPEN_SOURCE 600
24 #include <unistd.h>
25 #include "libavutil/avstring.h"
26 #include "libavcodec/opt.h"
27 #include "os_support.h"
28 #include "avformat.h"
29 #if CONFIG_NETWORK
30 #include "network.h"
31 #endif
32
33 #if LIBAVFORMAT_VERSION_MAJOR >= 53
34 /** @name Logging context. */
35 /*@{*/
36 static const char *urlcontext_to_name(void *ptr)
37 {
38     URLContext *h = (URLContext *)ptr;
39     if(h->prot) return h->prot->name;
40     else        return "NULL";
41 }
42 static const AVOption options[] = {{NULL}};
43 static const AVClass urlcontext_class =
44         { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
45 /*@}*/
46 #endif
47
48 static int default_interrupt_cb(void);
49
50 URLProtocol *first_protocol = NULL;
51 URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
52
53 URLProtocol *av_protocol_next(URLProtocol *p)
54 {
55     if(p) return p->next;
56     else  return first_protocol;
57 }
58
59 int av_register_protocol2(URLProtocol *protocol, int size)
60 {
61     URLProtocol **p;
62     if (size < sizeof(URLProtocol)) {
63         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
64         memcpy(temp, protocol, size);
65         protocol = temp;
66     }
67     p = &first_protocol;
68     while (*p != NULL) p = &(*p)->next;
69     *p = protocol;
70     protocol->next = NULL;
71     return 0;
72 }
73
74 #if LIBAVFORMAT_VERSION_MAJOR < 53
75 /* The layout of URLProtocol as of when major was bumped to 52 */
76 struct URLProtocol_compat {
77     const char *name;
78     int (*url_open)(URLContext *h, const char *filename, int flags);
79     int (*url_read)(URLContext *h, unsigned char *buf, int size);
80     int (*url_write)(URLContext *h, unsigned char *buf, int size);
81     int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
82     int (*url_close)(URLContext *h);
83     struct URLProtocol *next;
84 };
85
86 int av_register_protocol(URLProtocol *protocol)
87 {
88     return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
89 }
90
91 int register_protocol(URLProtocol *protocol)
92 {
93     return av_register_protocol(protocol);
94 }
95 #endif
96
97 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
98                        const char *filename, int flags)
99 {
100     URLContext *uc;
101     int err;
102
103 #if CONFIG_NETWORK
104     if (!ff_network_init())
105         return AVERROR(EIO);
106 #endif
107     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
108     if (!uc) {
109         err = AVERROR(ENOMEM);
110         goto fail;
111     }
112 #if LIBAVFORMAT_VERSION_MAJOR >= 53
113     uc->av_class = &urlcontext_class;
114 #endif
115     uc->filename = (char *) &uc[1];
116     strcpy(uc->filename, filename);
117     uc->prot = up;
118     uc->flags = flags;
119     uc->is_streamed = 0; /* default = not streamed */
120     uc->max_packet_size = 0; /* default: stream file */
121     err = up->url_open(uc, filename, flags);
122     if (err < 0) {
123         av_free(uc);
124         goto fail;
125     }
126
127     //We must be careful here as url_seek() could be slow, for example for http
128     if(   (flags & (URL_WRONLY | URL_RDWR))
129        || !strcmp(up->name, "file"))
130         if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
131             uc->is_streamed= 1;
132     *puc = uc;
133     return 0;
134  fail:
135     *puc = NULL;
136 #if CONFIG_NETWORK
137     ff_network_close();
138 #endif
139     return err;
140 }
141
142 int url_open(URLContext **puc, const char *filename, int flags)
143 {
144     URLProtocol *up;
145     const char *p;
146     char proto_str[128], *q;
147
148     p = filename;
149     q = proto_str;
150     while (*p != '\0' && *p != ':') {
151         /* protocols can only contain alphabetic chars */
152         if (!isalpha(*p))
153             goto file_proto;
154         if ((q - proto_str) < sizeof(proto_str) - 1)
155             *q++ = *p;
156         p++;
157     }
158     /* if the protocol has length 1, we consider it is a dos drive */
159     if (*p == '\0' || is_dos_path(filename)) {
160     file_proto:
161         strcpy(proto_str, "file");
162     } else {
163         *q = '\0';
164     }
165
166     up = first_protocol;
167     while (up != NULL) {
168         if (!strcmp(proto_str, up->name))
169             return url_open_protocol (puc, up, filename, flags);
170         up = up->next;
171     }
172     *puc = NULL;
173     return AVERROR(ENOENT);
174 }
175
176 int url_read(URLContext *h, unsigned char *buf, int size)
177 {
178     int ret;
179     if (h->flags & URL_WRONLY)
180         return AVERROR(EIO);
181     ret = h->prot->url_read(h, buf, size);
182     return ret;
183 }
184
185 int url_read_complete(URLContext *h, unsigned char *buf, int size)
186 {
187     int ret, len;
188     int fast_retries = 5;
189
190     len = 0;
191     while (len < size) {
192         ret = url_read(h, buf+len, size-len);
193         if (ret == AVERROR(EAGAIN)) {
194             ret = 0;
195             if (fast_retries)
196                 fast_retries--;
197             else
198                 usleep(1000);
199         } else if (ret < 1)
200             return ret < 0 ? ret : len;
201         if (ret)
202            fast_retries = FFMAX(fast_retries, 2);
203         len += ret;
204     }
205     return len;
206 }
207
208 int url_write(URLContext *h, const unsigned char *buf, int size)
209 {
210     int ret;
211     if (!(h->flags & (URL_WRONLY | URL_RDWR)))
212         return AVERROR(EIO);
213     /* avoid sending too big packets */
214     if (h->max_packet_size && size > h->max_packet_size)
215         return AVERROR(EIO);
216     ret = h->prot->url_write(h, buf, size);
217     return ret;
218 }
219
220 int64_t url_seek(URLContext *h, int64_t pos, int whence)
221 {
222     int64_t ret;
223
224     if (!h->prot->url_seek)
225         return AVERROR(ENOSYS);
226     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
227     return ret;
228 }
229
230 int url_close(URLContext *h)
231 {
232     int ret = 0;
233     if (!h) return 0; /* can happen when url_open fails */
234
235     if (h->prot->url_close)
236         ret = h->prot->url_close(h);
237 #if CONFIG_NETWORK
238     ff_network_close();
239 #endif
240     av_free(h);
241     return ret;
242 }
243
244 int url_exist(const char *filename)
245 {
246     URLContext *h;
247     if (url_open(&h, filename, URL_RDONLY) < 0)
248         return 0;
249     url_close(h);
250     return 1;
251 }
252
253 int64_t url_filesize(URLContext *h)
254 {
255     int64_t pos, size;
256
257     size= url_seek(h, 0, AVSEEK_SIZE);
258     if(size<0){
259         pos = url_seek(h, 0, SEEK_CUR);
260         if ((size = url_seek(h, -1, SEEK_END)) < 0)
261             return size;
262         size++;
263         url_seek(h, pos, SEEK_SET);
264     }
265     return size;
266 }
267
268 int url_get_file_handle(URLContext *h)
269 {
270     if (!h->prot->url_get_file_handle)
271         return -1;
272     return h->prot->url_get_file_handle(h);
273 }
274
275 int url_get_max_packet_size(URLContext *h)
276 {
277     return h->max_packet_size;
278 }
279
280 void url_get_filename(URLContext *h, char *buf, int buf_size)
281 {
282     av_strlcpy(buf, h->filename, buf_size);
283 }
284
285
286 static int default_interrupt_cb(void)
287 {
288     return 0;
289 }
290
291 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
292 {
293     if (!interrupt_cb)
294         interrupt_cb = default_interrupt_cb;
295     url_interrupt_cb = interrupt_cb;
296 }
297
298 int av_url_read_pause(URLContext *h, int pause)
299 {
300     if (!h->prot->url_read_pause)
301         return AVERROR(ENOSYS);
302     return h->prot->url_read_pause(h, pause);
303 }
304
305 int64_t av_url_read_seek(URLContext *h,
306         int stream_index, int64_t timestamp, int flags)
307 {
308     if (!h->prot->url_read_seek)
309         return AVERROR(ENOSYS);
310     return h->prot->url_read_seek(h, stream_index, timestamp, flags);
311 }