]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
lavf: remove disabled FF_API_OLD_AVIO cruft
[ffmpeg] / libavformat / avio.c
1 /*
2  * unbuffered I/O
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 #include <unistd.h>
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.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 static URLProtocol *first_protocol = NULL;
35
36 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
37 {
38     return prev ? prev->next : first_protocol;
39 }
40
41 /** @name Logging context. */
42 /*@{*/
43 static const char *urlcontext_to_name(void *ptr)
44 {
45     URLContext *h = (URLContext *)ptr;
46     if(h->prot) return h->prot->name;
47     else        return "NULL";
48 }
49
50 static void *urlcontext_child_next(void *obj, void *prev)
51 {
52     URLContext *h = obj;
53     if (!prev && h->priv_data && h->prot->priv_data_class)
54         return h->priv_data;
55     return NULL;
56 }
57
58 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
59 {
60     URLProtocol *p = NULL;
61
62     /* find the protocol that corresponds to prev */
63     while (prev && (p = ffurl_protocol_next(p)))
64         if (p->priv_data_class == prev)
65             break;
66
67     /* find next protocol with priv options */
68     while (p = ffurl_protocol_next(p))
69         if (p->priv_data_class)
70             return p->priv_data_class;
71     return NULL;
72
73 }
74
75 static const AVOption options[] = {{NULL}};
76 const AVClass ffurl_context_class = {
77     .class_name     = "URLContext",
78     .item_name      = urlcontext_to_name,
79     .option         = options,
80     .version        = LIBAVUTIL_VERSION_INT,
81     .child_next     = urlcontext_child_next,
82     .child_class_next = urlcontext_child_class_next,
83 };
84 /*@}*/
85
86
87 #if FF_API_OLD_INTERRUPT_CB
88 static int default_interrupt_cb(void);
89 int (*url_interrupt_cb)(void) = default_interrupt_cb;
90 #endif
91
92 const char *avio_enum_protocols(void **opaque, int output)
93 {
94     URLProtocol **p = opaque;
95     *p = ffurl_protocol_next(*p);
96     if (!*p) return NULL;
97     if ((output && (*p)->url_write) || (!output && (*p)->url_read))
98         return (*p)->name;
99     return avio_enum_protocols(opaque, output);
100 }
101
102 int ffurl_register_protocol(URLProtocol *protocol, int size)
103 {
104     URLProtocol **p;
105     if (size < sizeof(URLProtocol)) {
106         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
107         memcpy(temp, protocol, size);
108         protocol = temp;
109     }
110     p = &first_protocol;
111     while (*p != NULL) p = &(*p)->next;
112     *p = protocol;
113     protocol->next = NULL;
114     return 0;
115 }
116
117 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
118                                    const char *filename, int flags,
119                                    const AVIOInterruptCB *int_cb)
120 {
121     URLContext *uc;
122     int err;
123
124 #if CONFIG_NETWORK
125     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
126         return AVERROR(EIO);
127 #endif
128     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
129     if (!uc) {
130         err = AVERROR(ENOMEM);
131         goto fail;
132     }
133     uc->av_class = &ffurl_context_class;
134     uc->filename = (char *) &uc[1];
135     strcpy(uc->filename, filename);
136     uc->prot = up;
137     uc->flags = flags;
138     uc->is_streamed = 0; /* default = not streamed */
139     uc->max_packet_size = 0; /* default: stream file */
140     if (up->priv_data_size) {
141         uc->priv_data = av_mallocz(up->priv_data_size);
142         if (up->priv_data_class) {
143             *(const AVClass**)uc->priv_data = up->priv_data_class;
144             av_opt_set_defaults(uc->priv_data);
145         }
146     }
147     if (int_cb)
148         uc->interrupt_callback = *int_cb;
149
150     *puc = uc;
151     return 0;
152  fail:
153     *puc = NULL;
154 #if CONFIG_NETWORK
155     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
156         ff_network_close();
157 #endif
158     return err;
159 }
160
161 int ffurl_connect(URLContext* uc, AVDictionary **options)
162 {
163     int err =
164         uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
165         uc->prot->url_open(uc, uc->filename, uc->flags);
166     if (err)
167         return err;
168     uc->is_connected = 1;
169     //We must be careful here as ffurl_seek() could be slow, for example for http
170     if(   (uc->flags & AVIO_FLAG_WRITE)
171        || !strcmp(uc->prot->name, "file"))
172         if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
173             uc->is_streamed= 1;
174     return 0;
175 }
176
177 #define URL_SCHEME_CHARS                        \
178     "abcdefghijklmnopqrstuvwxyz"                \
179     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
180     "0123456789+-."
181
182 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
183                 const AVIOInterruptCB *int_cb)
184 {
185     URLProtocol *up = NULL;
186     char proto_str[128], proto_nested[128], *ptr;
187     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
188
189     if (filename[proto_len] != ':' || is_dos_path(filename))
190         strcpy(proto_str, "file");
191     else
192         av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
193
194     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
195     if ((ptr = strchr(proto_nested, '+')))
196         *ptr = '\0';
197
198     while (up = ffurl_protocol_next(up)) {
199         if (!strcmp(proto_str, up->name))
200             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
201         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
202             !strcmp(proto_nested, up->name))
203             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
204     }
205     *puc = NULL;
206     return AVERROR(ENOENT);
207 }
208
209 int ffurl_open(URLContext **puc, const char *filename, int flags,
210                const AVIOInterruptCB *int_cb, AVDictionary **options)
211 {
212     int ret = ffurl_alloc(puc, filename, flags, int_cb);
213     if (ret)
214         return ret;
215     if (options && (*puc)->prot->priv_data_class &&
216         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
217         goto fail;
218     ret = ffurl_connect(*puc, options);
219     if (!ret)
220         return 0;
221 fail:
222     ffurl_close(*puc);
223     *puc = NULL;
224     return ret;
225 }
226
227 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
228                                          int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
229 {
230     int ret, len;
231     int fast_retries = 5;
232
233     len = 0;
234     while (len < size_min) {
235         ret = transfer_func(h, buf+len, size-len);
236         if (ret == AVERROR(EINTR))
237             continue;
238         if (h->flags & AVIO_FLAG_NONBLOCK)
239             return ret;
240         if (ret == AVERROR(EAGAIN)) {
241             ret = 0;
242             if (fast_retries)
243                 fast_retries--;
244             else
245                 usleep(1000);
246         } else if (ret < 1)
247             return ret < 0 ? ret : len;
248         if (ret)
249            fast_retries = FFMAX(fast_retries, 2);
250         len += ret;
251         if (ff_check_interrupt(&h->interrupt_callback))
252             return AVERROR_EXIT;
253     }
254     return len;
255 }
256
257 int ffurl_read(URLContext *h, unsigned char *buf, int size)
258 {
259     if (!(h->flags & AVIO_FLAG_READ))
260         return AVERROR(EIO);
261     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
262 }
263
264 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
265 {
266     if (!(h->flags & AVIO_FLAG_READ))
267         return AVERROR(EIO);
268     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
269 }
270
271 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
272 {
273     if (!(h->flags & AVIO_FLAG_WRITE))
274         return AVERROR(EIO);
275     /* avoid sending too big packets */
276     if (h->max_packet_size && size > h->max_packet_size)
277         return AVERROR(EIO);
278
279     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
280 }
281
282 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
283 {
284     int64_t ret;
285
286     if (!h->prot->url_seek)
287         return AVERROR(ENOSYS);
288     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
289     return ret;
290 }
291
292 int ffurl_close(URLContext *h)
293 {
294     int ret = 0;
295     if (!h) return 0; /* can happen when ffurl_open fails */
296
297     if (h->is_connected && h->prot->url_close)
298         ret = h->prot->url_close(h);
299 #if CONFIG_NETWORK
300     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
301         ff_network_close();
302 #endif
303     if (h->prot->priv_data_size) {
304         if (h->prot->priv_data_class)
305             av_opt_free(h->priv_data);
306         av_free(h->priv_data);
307     }
308     av_free(h);
309     return ret;
310 }
311
312 int avio_check(const char *url, int flags)
313 {
314     URLContext *h;
315     int ret = ffurl_alloc(&h, url, flags, NULL);
316     if (ret)
317         return ret;
318
319     if (h->prot->url_check) {
320         ret = h->prot->url_check(h, flags);
321     } else {
322         ret = ffurl_connect(h, NULL);
323         if (ret >= 0)
324             ret = flags;
325     }
326
327     ffurl_close(h);
328     return ret;
329 }
330
331 int64_t ffurl_size(URLContext *h)
332 {
333     int64_t pos, size;
334
335     size= ffurl_seek(h, 0, AVSEEK_SIZE);
336     if(size<0){
337         pos = ffurl_seek(h, 0, SEEK_CUR);
338         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
339             return size;
340         size++;
341         ffurl_seek(h, pos, SEEK_SET);
342     }
343     return size;
344 }
345
346 int ffurl_get_file_handle(URLContext *h)
347 {
348     if (!h->prot->url_get_file_handle)
349         return -1;
350     return h->prot->url_get_file_handle(h);
351 }
352
353 #if FF_API_OLD_INTERRUPT_CB
354 static int default_interrupt_cb(void)
355 {
356     return 0;
357 }
358
359 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
360 {
361     if (!interrupt_cb)
362         interrupt_cb = default_interrupt_cb;
363     url_interrupt_cb = interrupt_cb;
364 }
365 #endif
366
367 int ff_check_interrupt(AVIOInterruptCB *cb)
368 {
369     int ret;
370     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
371         return ret;
372 #if FF_API_OLD_INTERRUPT_CB
373     return url_interrupt_cb();
374 #else
375     return 0;
376 #endif
377 }
378