]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
avio: remove AVIO_* access symbols in favor of new AVIO_FLAG_* symbols
[ffmpeg] / libavformat / avio.c
1 /*
2  * Unbuffered io for ffmpeg system
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "libavutil/opt.h"
27 #include "os_support.h"
28 #include "avformat.h"
29 #if CONFIG_NETWORK
30 #include "network.h"
31 #endif
32 #include "url.h"
33
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
47 static int default_interrupt_cb(void);
48
49 URLProtocol *first_protocol = NULL;
50 int (*url_interrupt_cb)(void) = default_interrupt_cb;
51
52 #if FF_API_OLD_AVIO
53 URLProtocol *av_protocol_next(URLProtocol *p)
54 {
55     if(p) return p->next;
56     else  return first_protocol;
57 }
58 #endif
59
60 const char *avio_enum_protocols(void **opaque, int output)
61 {
62     URLProtocol **p = opaque;
63     *p = *p ? (*p)->next : first_protocol;
64     if (!*p) return NULL;
65     if ((output && (*p)->url_write) || (!output && (*p)->url_read))
66         return (*p)->name;
67     return avio_enum_protocols(opaque, output);
68 }
69
70 int ffurl_register_protocol(URLProtocol *protocol, int size)
71 {
72     URLProtocol **p;
73     if (size < sizeof(URLProtocol)) {
74         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
75         memcpy(temp, protocol, size);
76         protocol = temp;
77     }
78     p = &first_protocol;
79     while (*p != NULL) p = &(*p)->next;
80     *p = protocol;
81     protocol->next = NULL;
82     return 0;
83 }
84
85 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
86                                    const char *filename, int flags)
87 {
88     URLContext *uc;
89     int err;
90
91 #if CONFIG_NETWORK
92     if (!ff_network_init())
93         return AVERROR(EIO);
94 #endif
95     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
96     if (!uc) {
97         err = AVERROR(ENOMEM);
98         goto fail;
99     }
100     uc->av_class = &urlcontext_class;
101     uc->filename = (char *) &uc[1];
102     strcpy(uc->filename, filename);
103     uc->prot = up;
104     uc->flags = flags;
105     uc->is_streamed = 0; /* default = not streamed */
106     uc->max_packet_size = 0; /* default: stream file */
107     if (up->priv_data_size) {
108         uc->priv_data = av_mallocz(up->priv_data_size);
109         if (up->priv_data_class) {
110             *(const AVClass**)uc->priv_data = up->priv_data_class;
111             av_opt_set_defaults(uc->priv_data);
112         }
113     }
114
115     *puc = uc;
116     return 0;
117  fail:
118     *puc = NULL;
119 #if CONFIG_NETWORK
120     ff_network_close();
121 #endif
122     return err;
123 }
124
125 int ffurl_connect(URLContext* uc)
126 {
127     int err = uc->prot->url_open(uc, uc->filename, uc->flags);
128     if (err)
129         return err;
130     uc->is_connected = 1;
131     //We must be careful here as ffurl_seek() could be slow, for example for http
132     if(   (uc->flags & AVIO_FLAG_WRITE)
133        || !strcmp(uc->prot->name, "file"))
134         if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
135             uc->is_streamed= 1;
136     return 0;
137 }
138
139 #if FF_API_OLD_AVIO
140 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
141                        const char *filename, int flags)
142 {
143     int ret;
144
145     ret = url_alloc_for_protocol(puc, up, filename, flags);
146     if (ret)
147         goto fail;
148     ret = ffurl_connect(*puc);
149     if (!ret)
150         return 0;
151  fail:
152     ffurl_close(*puc);
153     *puc = NULL;
154     return ret;
155 }
156 int url_alloc(URLContext **puc, const char *filename, int flags)
157 {
158     return ffurl_alloc(puc, filename, flags);
159 }
160 int url_connect(URLContext* uc)
161 {
162     return ffurl_connect(uc);
163 }
164 int url_open(URLContext **puc, const char *filename, int flags)
165 {
166     return ffurl_open(puc, filename, flags);
167 }
168 int url_read(URLContext *h, unsigned char *buf, int size)
169 {
170     return ffurl_read(h, buf, size);
171 }
172 int url_read_complete(URLContext *h, unsigned char *buf, int size)
173 {
174     return ffurl_read_complete(h, buf, size);
175 }
176 int url_write(URLContext *h, const unsigned char *buf, int size)
177 {
178     return ffurl_write(h, buf, size);
179 }
180 int64_t url_seek(URLContext *h, int64_t pos, int whence)
181 {
182     return ffurl_seek(h, pos, whence);
183 }
184 int url_close(URLContext *h)
185 {
186     return ffurl_close(h);
187 }
188 int64_t url_filesize(URLContext *h)
189 {
190     return ffurl_size(h);
191 }
192 int url_get_file_handle(URLContext *h)
193 {
194     return ffurl_get_file_handle(h);
195 }
196 int url_get_max_packet_size(URLContext *h)
197 {
198     return h->max_packet_size;
199 }
200 void url_get_filename(URLContext *h, char *buf, int buf_size)
201 {
202     av_strlcpy(buf, h->filename, buf_size);
203 }
204 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
205 {
206     avio_set_interrupt_cb(interrupt_cb);
207 }
208 int av_register_protocol2(URLProtocol *protocol, int size)
209 {
210     return ffurl_register_protocol(protocol, size);
211 }
212 #endif
213
214 #define URL_SCHEME_CHARS                        \
215     "abcdefghijklmnopqrstuvwxyz"                \
216     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
217     "0123456789+-."
218
219 int ffurl_alloc(URLContext **puc, const char *filename, int flags)
220 {
221     URLProtocol *up;
222     char proto_str[128], proto_nested[128], *ptr;
223     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
224
225     if (filename[proto_len] != ':' || is_dos_path(filename))
226         strcpy(proto_str, "file");
227     else
228         av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
229
230     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
231     if ((ptr = strchr(proto_nested, '+')))
232         *ptr = '\0';
233
234     up = first_protocol;
235     while (up != NULL) {
236         if (!strcmp(proto_str, up->name))
237             return url_alloc_for_protocol (puc, up, filename, flags);
238         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
239             !strcmp(proto_nested, up->name))
240             return url_alloc_for_protocol (puc, up, filename, flags);
241         up = up->next;
242     }
243     *puc = NULL;
244     return AVERROR(ENOENT);
245 }
246
247 int ffurl_open(URLContext **puc, const char *filename, int flags)
248 {
249     int ret = ffurl_alloc(puc, filename, flags);
250     if (ret)
251         return ret;
252     ret = ffurl_connect(*puc);
253     if (!ret)
254         return 0;
255     ffurl_close(*puc);
256     *puc = NULL;
257     return ret;
258 }
259
260 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
261                                          int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
262 {
263     int ret, len;
264     int fast_retries = 5;
265
266     len = 0;
267     while (len < size_min) {
268         ret = transfer_func(h, buf+len, size-len);
269         if (ret == AVERROR(EINTR))
270             continue;
271         if (h->flags & AVIO_FLAG_NONBLOCK)
272             return ret;
273         if (ret == AVERROR(EAGAIN)) {
274             ret = 0;
275             if (fast_retries)
276                 fast_retries--;
277             else
278                 usleep(1000);
279         } else if (ret < 1)
280             return ret < 0 ? ret : len;
281         if (ret)
282            fast_retries = FFMAX(fast_retries, 2);
283         len += ret;
284         if (url_interrupt_cb())
285             return AVERROR_EXIT;
286     }
287     return len;
288 }
289
290 int ffurl_read(URLContext *h, unsigned char *buf, int size)
291 {
292     if (h->flags & AVIO_FLAG_WRITE)
293         return AVERROR(EIO);
294     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
295 }
296
297 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
298 {
299     if (h->flags & AVIO_FLAG_WRITE)
300         return AVERROR(EIO);
301     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
302 }
303
304 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
305 {
306     if (!(h->flags & AVIO_FLAG_WRITE))
307         return AVERROR(EIO);
308     /* avoid sending too big packets */
309     if (h->max_packet_size && size > h->max_packet_size)
310         return AVERROR(EIO);
311
312     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
313 }
314
315 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
316 {
317     int64_t ret;
318
319     if (!h->prot->url_seek)
320         return AVERROR(ENOSYS);
321     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
322     return ret;
323 }
324
325 int ffurl_close(URLContext *h)
326 {
327     int ret = 0;
328     if (!h) return 0; /* can happen when ffurl_open fails */
329
330     if (h->is_connected && h->prot->url_close)
331         ret = h->prot->url_close(h);
332 #if CONFIG_NETWORK
333     ff_network_close();
334 #endif
335     if (h->prot->priv_data_size)
336         av_free(h->priv_data);
337     av_free(h);
338     return ret;
339 }
340
341 #if FF_API_OLD_AVIO
342 int url_exist(const char *filename)
343 {
344     URLContext *h;
345     if (ffurl_open(&h, filename, AVIO_FLAG_READ) < 0)
346         return 0;
347     ffurl_close(h);
348     return 1;
349 }
350 #endif
351
352 int avio_check(const char *url, int flags)
353 {
354     URLContext *h;
355     int ret = ffurl_alloc(&h, url, flags);
356     if (ret)
357         return ret;
358
359     if (h->prot->url_check) {
360         ret = h->prot->url_check(h, flags);
361     } else {
362         ret = ffurl_connect(h);
363         if (ret >= 0)
364             ret = flags;
365     }
366
367     ffurl_close(h);
368     return ret;
369 }
370
371 int64_t ffurl_size(URLContext *h)
372 {
373     int64_t pos, size;
374
375     size= ffurl_seek(h, 0, AVSEEK_SIZE);
376     if(size<0){
377         pos = ffurl_seek(h, 0, SEEK_CUR);
378         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
379             return size;
380         size++;
381         ffurl_seek(h, pos, SEEK_SET);
382     }
383     return size;
384 }
385
386 int ffurl_get_file_handle(URLContext *h)
387 {
388     if (!h->prot->url_get_file_handle)
389         return -1;
390     return h->prot->url_get_file_handle(h);
391 }
392
393 static int default_interrupt_cb(void)
394 {
395     return 0;
396 }
397
398 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
399 {
400     if (!interrupt_cb)
401         interrupt_cb = default_interrupt_cb;
402     url_interrupt_cb = interrupt_cb;
403 }
404
405 #if FF_API_OLD_AVIO
406 int av_url_read_pause(URLContext *h, int pause)
407 {
408     if (!h->prot->url_read_pause)
409         return AVERROR(ENOSYS);
410     return h->prot->url_read_pause(h, pause);
411 }
412
413 int64_t av_url_read_seek(URLContext *h,
414         int stream_index, int64_t timestamp, int flags)
415 {
416     if (!h->prot->url_read_seek)
417         return AVERROR(ENOSYS);
418     return h->prot->url_read_seek(h, stream_index, timestamp, flags);
419 }
420 #endif