]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
threads: limit the number of automatic threads to MAX_AUTO_THREADS
[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 #if FF_API_OLD_AVIO
93 URLProtocol *av_protocol_next(URLProtocol *p)
94 {
95     return ffurl_protocol_next(p);
96 }
97 #endif
98
99 const char *avio_enum_protocols(void **opaque, int output)
100 {
101     URLProtocol **p = opaque;
102     *p = ffurl_protocol_next(*p);
103     if (!*p) return NULL;
104     if ((output && (*p)->url_write) || (!output && (*p)->url_read))
105         return (*p)->name;
106     return avio_enum_protocols(opaque, output);
107 }
108
109 int ffurl_register_protocol(URLProtocol *protocol, int size)
110 {
111     URLProtocol **p;
112     if (size < sizeof(URLProtocol)) {
113         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
114         memcpy(temp, protocol, size);
115         protocol = temp;
116     }
117     p = &first_protocol;
118     while (*p != NULL) p = &(*p)->next;
119     *p = protocol;
120     protocol->next = NULL;
121     return 0;
122 }
123
124 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
125                                    const char *filename, int flags,
126                                    const AVIOInterruptCB *int_cb)
127 {
128     URLContext *uc;
129     int err;
130
131 #if CONFIG_NETWORK
132     if (!ff_network_init())
133         return AVERROR(EIO);
134 #endif
135     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
136     if (!uc) {
137         err = AVERROR(ENOMEM);
138         goto fail;
139     }
140     uc->av_class = &ffurl_context_class;
141     uc->filename = (char *) &uc[1];
142     strcpy(uc->filename, filename);
143     uc->prot = up;
144     uc->flags = flags;
145     uc->is_streamed = 0; /* default = not streamed */
146     uc->max_packet_size = 0; /* default: stream file */
147     if (up->priv_data_size) {
148         uc->priv_data = av_mallocz(up->priv_data_size);
149         if (up->priv_data_class) {
150             *(const AVClass**)uc->priv_data = up->priv_data_class;
151             av_opt_set_defaults(uc->priv_data);
152         }
153     }
154     if (int_cb)
155         uc->interrupt_callback = *int_cb;
156
157     *puc = uc;
158     return 0;
159  fail:
160     *puc = NULL;
161 #if CONFIG_NETWORK
162     ff_network_close();
163 #endif
164     return err;
165 }
166
167 int ffurl_connect(URLContext* uc, AVDictionary **options)
168 {
169     int err =
170 #if !FF_API_OLD_AVIO
171         uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
172 #endif
173         uc->prot->url_open(uc, uc->filename, uc->flags);
174     if (err)
175         return err;
176     uc->is_connected = 1;
177     //We must be careful here as ffurl_seek() could be slow, for example for http
178     if(   (uc->flags & AVIO_FLAG_WRITE)
179        || !strcmp(uc->prot->name, "file"))
180         if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
181             uc->is_streamed= 1;
182     return 0;
183 }
184
185 #if FF_API_OLD_AVIO
186 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
187                        const char *filename, int flags)
188 {
189     int ret;
190
191     ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
192     if (ret)
193         goto fail;
194     ret = ffurl_connect(*puc, NULL);
195     if (!ret)
196         return 0;
197  fail:
198     ffurl_close(*puc);
199     *puc = NULL;
200     return ret;
201 }
202 int url_alloc(URLContext **puc, const char *filename, int flags)
203 {
204     return ffurl_alloc(puc, filename, flags, NULL);
205 }
206 int url_connect(URLContext* uc)
207 {
208     return ffurl_connect(uc, NULL);
209 }
210 int url_open(URLContext **puc, const char *filename, int flags)
211 {
212     return ffurl_open(puc, filename, flags, NULL, NULL);
213 }
214 int url_read(URLContext *h, unsigned char *buf, int size)
215 {
216     return ffurl_read(h, buf, size);
217 }
218 int url_read_complete(URLContext *h, unsigned char *buf, int size)
219 {
220     return ffurl_read_complete(h, buf, size);
221 }
222 int url_write(URLContext *h, const unsigned char *buf, int size)
223 {
224     return ffurl_write(h, buf, size);
225 }
226 int64_t url_seek(URLContext *h, int64_t pos, int whence)
227 {
228     return ffurl_seek(h, pos, whence);
229 }
230 int url_close(URLContext *h)
231 {
232     return ffurl_close(h);
233 }
234 int64_t url_filesize(URLContext *h)
235 {
236     return ffurl_size(h);
237 }
238 int url_get_file_handle(URLContext *h)
239 {
240     return ffurl_get_file_handle(h);
241 }
242 int url_get_max_packet_size(URLContext *h)
243 {
244     return h->max_packet_size;
245 }
246 void url_get_filename(URLContext *h, char *buf, int buf_size)
247 {
248     av_strlcpy(buf, h->filename, buf_size);
249 }
250 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
251 {
252     avio_set_interrupt_cb(interrupt_cb);
253 }
254 int av_register_protocol2(URLProtocol *protocol, int size)
255 {
256     return ffurl_register_protocol(protocol, size);
257 }
258 #endif
259
260 #define URL_SCHEME_CHARS                        \
261     "abcdefghijklmnopqrstuvwxyz"                \
262     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
263     "0123456789+-."
264
265 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
266                 const AVIOInterruptCB *int_cb)
267 {
268     URLProtocol *up = NULL;
269     char proto_str[128], proto_nested[128], *ptr;
270     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
271
272     if (filename[proto_len] != ':' || is_dos_path(filename))
273         strcpy(proto_str, "file");
274     else
275         av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
276
277     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
278     if ((ptr = strchr(proto_nested, '+')))
279         *ptr = '\0';
280
281     while (up = ffurl_protocol_next(up)) {
282         if (!strcmp(proto_str, up->name))
283             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
284         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
285             !strcmp(proto_nested, up->name))
286             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
287     }
288     *puc = NULL;
289     return AVERROR(ENOENT);
290 }
291
292 int ffurl_open(URLContext **puc, const char *filename, int flags,
293                const AVIOInterruptCB *int_cb, AVDictionary **options)
294 {
295     int ret = ffurl_alloc(puc, filename, flags, int_cb);
296     if (ret)
297         return ret;
298     if (options && (*puc)->prot->priv_data_class &&
299         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
300         goto fail;
301     ret = ffurl_connect(*puc, options);
302     if (!ret)
303         return 0;
304 fail:
305     ffurl_close(*puc);
306     *puc = NULL;
307     return ret;
308 }
309
310 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
311                                          int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
312 {
313     int ret, len;
314     int fast_retries = 5;
315
316     len = 0;
317     while (len < size_min) {
318         ret = transfer_func(h, buf+len, size-len);
319         if (ret == AVERROR(EINTR))
320             continue;
321         if (h->flags & AVIO_FLAG_NONBLOCK)
322             return ret;
323         if (ret == AVERROR(EAGAIN)) {
324             ret = 0;
325             if (fast_retries)
326                 fast_retries--;
327             else
328                 usleep(1000);
329         } else if (ret < 1)
330             return ret < 0 ? ret : len;
331         if (ret)
332            fast_retries = FFMAX(fast_retries, 2);
333         len += ret;
334         if (ff_check_interrupt(&h->interrupt_callback))
335             return AVERROR_EXIT;
336     }
337     return len;
338 }
339
340 int ffurl_read(URLContext *h, unsigned char *buf, int size)
341 {
342     if (!(h->flags & AVIO_FLAG_READ))
343         return AVERROR(EIO);
344     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
345 }
346
347 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
348 {
349     if (!(h->flags & AVIO_FLAG_READ))
350         return AVERROR(EIO);
351     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
352 }
353
354 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
355 {
356     if (!(h->flags & AVIO_FLAG_WRITE))
357         return AVERROR(EIO);
358     /* avoid sending too big packets */
359     if (h->max_packet_size && size > h->max_packet_size)
360         return AVERROR(EIO);
361
362     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
363 }
364
365 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
366 {
367     int64_t ret;
368
369     if (!h->prot->url_seek)
370         return AVERROR(ENOSYS);
371     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
372     return ret;
373 }
374
375 int ffurl_close(URLContext *h)
376 {
377     int ret = 0;
378     if (!h) return 0; /* can happen when ffurl_open fails */
379
380     if (h->is_connected && h->prot->url_close)
381         ret = h->prot->url_close(h);
382 #if CONFIG_NETWORK
383     ff_network_close();
384 #endif
385     if (h->prot->priv_data_size) {
386         if (h->prot->priv_data_class)
387             av_opt_free(h->priv_data);
388         av_free(h->priv_data);
389     }
390     av_free(h);
391     return ret;
392 }
393
394 #if FF_API_OLD_AVIO
395 int url_exist(const char *filename)
396 {
397     URLContext *h;
398     if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
399         return 0;
400     ffurl_close(h);
401     return 1;
402 }
403 #endif
404
405 int avio_check(const char *url, int flags)
406 {
407     URLContext *h;
408     int ret = ffurl_alloc(&h, url, flags, NULL);
409     if (ret)
410         return ret;
411
412     if (h->prot->url_check) {
413         ret = h->prot->url_check(h, flags);
414     } else {
415         ret = ffurl_connect(h, NULL);
416         if (ret >= 0)
417             ret = flags;
418     }
419
420     ffurl_close(h);
421     return ret;
422 }
423
424 int64_t ffurl_size(URLContext *h)
425 {
426     int64_t pos, size;
427
428     size= ffurl_seek(h, 0, AVSEEK_SIZE);
429     if(size<0){
430         pos = ffurl_seek(h, 0, SEEK_CUR);
431         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
432             return size;
433         size++;
434         ffurl_seek(h, pos, SEEK_SET);
435     }
436     return size;
437 }
438
439 int ffurl_get_file_handle(URLContext *h)
440 {
441     if (!h->prot->url_get_file_handle)
442         return -1;
443     return h->prot->url_get_file_handle(h);
444 }
445
446 #if FF_API_OLD_INTERRUPT_CB
447 static int default_interrupt_cb(void)
448 {
449     return 0;
450 }
451
452 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
453 {
454     if (!interrupt_cb)
455         interrupt_cb = default_interrupt_cb;
456     url_interrupt_cb = interrupt_cb;
457 }
458 #endif
459
460 int ff_check_interrupt(AVIOInterruptCB *cb)
461 {
462     int ret;
463     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
464         return ret;
465 #if FF_API_OLD_INTERRUPT_CB
466     return url_interrupt_cb();
467 #else
468     return 0;
469 #endif
470 }
471
472 #if FF_API_OLD_AVIO
473 int av_url_read_pause(URLContext *h, int pause)
474 {
475     if (!h->prot->url_read_pause)
476         return AVERROR(ENOSYS);
477     return h->prot->url_read_pause(h, pause);
478 }
479
480 int64_t av_url_read_seek(URLContext *h,
481         int stream_index, int64_t timestamp, int flags)
482 {
483     if (!h->prot->url_read_seek)
484         return AVERROR(ENOSYS);
485     return h->prot->url_read_seek(h, stream_index, timestamp, flags);
486 }
487 #endif