]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / avio.c
1 /*
2  * unbuffered I/O
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 #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 const char *avio_enum_protocols(void **opaque, int output)
88 {
89     URLProtocol **p = opaque;
90     *p = ffurl_protocol_next(*p);
91     if (!*p) return NULL;
92     if ((output && (*p)->url_write) || (!output && (*p)->url_read))
93         return (*p)->name;
94     return avio_enum_protocols(opaque, output);
95 }
96
97 int ffurl_register_protocol(URLProtocol *protocol, int size)
98 {
99     URLProtocol **p;
100     if (size < sizeof(URLProtocol)) {
101         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
102         memcpy(temp, protocol, size);
103         protocol = temp;
104     }
105     p = &first_protocol;
106     while (*p != NULL) p = &(*p)->next;
107     *p = protocol;
108     protocol->next = NULL;
109     return 0;
110 }
111
112 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
113                                    const char *filename, int flags,
114                                    const AVIOInterruptCB *int_cb)
115 {
116     URLContext *uc;
117     int err;
118
119 #if CONFIG_NETWORK
120     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
121         return AVERROR(EIO);
122 #endif
123     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
124     if (!uc) {
125         err = AVERROR(ENOMEM);
126         goto fail;
127     }
128     uc->av_class = &ffurl_context_class;
129     uc->filename = (char *) &uc[1];
130     strcpy(uc->filename, filename);
131     uc->prot = up;
132     uc->flags = flags;
133     uc->is_streamed = 0; /* default = not streamed */
134     uc->max_packet_size = 0; /* default: stream file */
135     if (up->priv_data_size) {
136         uc->priv_data = av_mallocz(up->priv_data_size);
137         if (up->priv_data_class) {
138             int proto_len= strlen(up->name);
139             char *start = strchr(uc->filename, ',');
140             *(const AVClass**)uc->priv_data = up->priv_data_class;
141             av_opt_set_defaults(uc->priv_data);
142             if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
143                 int ret= 0;
144                 char *p= start;
145                 char sep= *++p;
146                 char *key, *val;
147                 p++;
148                 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
149                     *val= *key= 0;
150                     ret= av_opt_set(uc->priv_data, p, key+1, 0);
151                     if (ret == AVERROR_OPTION_NOT_FOUND)
152                         av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
153                     *val= *key= sep;
154                     p= val+1;
155                 }
156                 if(ret<0 || p!=key){
157                     av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
158                     av_freep(&uc->priv_data);
159                     av_freep(&uc);
160                     goto fail;
161                 }
162                 memmove(start, key+1, strlen(key));
163             }
164         }
165     }
166     if (int_cb)
167         uc->interrupt_callback = *int_cb;
168
169     *puc = uc;
170     return 0;
171  fail:
172     *puc = NULL;
173 #if CONFIG_NETWORK
174     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
175         ff_network_close();
176 #endif
177     return err;
178 }
179
180 int ffurl_connect(URLContext* uc, AVDictionary **options)
181 {
182     int err =
183         uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
184         uc->prot->url_open(uc, uc->filename, uc->flags);
185     if (err)
186         return err;
187     uc->is_connected = 1;
188     //We must be careful here as ffurl_seek() could be slow, for example for http
189     if(   (uc->flags & AVIO_FLAG_WRITE)
190        || !strcmp(uc->prot->name, "file"))
191         if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
192             uc->is_streamed= 1;
193     return 0;
194 }
195
196 #define URL_SCHEME_CHARS                        \
197     "abcdefghijklmnopqrstuvwxyz"                \
198     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
199     "0123456789+-."
200
201 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
202                 const AVIOInterruptCB *int_cb)
203 {
204     URLProtocol *up = NULL;
205     char proto_str[128], proto_nested[128], *ptr;
206     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
207
208     if (!first_protocol) {
209         av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
210                                      "Missing call to av_register_all()?\n");
211     }
212
213     if (filename[proto_len] != ':' &&  filename[proto_len] != ',' || is_dos_path(filename))
214         strcpy(proto_str, "file");
215     else
216         av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
217
218     if ((ptr = strchr(proto_str, ',')))
219         *ptr = '\0';
220     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
221     if ((ptr = strchr(proto_nested, '+')))
222         *ptr = '\0';
223
224     while (up = ffurl_protocol_next(up)) {
225         if (!strcmp(proto_str, up->name))
226             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
227         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
228             !strcmp(proto_nested, up->name))
229             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
230     }
231     *puc = NULL;
232     return AVERROR(ENOENT);
233 }
234
235 int ffurl_open(URLContext **puc, const char *filename, int flags,
236                const AVIOInterruptCB *int_cb, AVDictionary **options)
237 {
238     int ret = ffurl_alloc(puc, filename, flags, int_cb);
239     if (ret)
240         return ret;
241     if (options && (*puc)->prot->priv_data_class &&
242         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
243         goto fail;
244     ret = ffurl_connect(*puc, options);
245     if (!ret)
246         return 0;
247 fail:
248     ffurl_close(*puc);
249     *puc = NULL;
250     return ret;
251 }
252
253 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
254                                          int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
255 {
256     int ret, len;
257     int fast_retries = 5;
258
259     len = 0;
260     while (len < size_min) {
261         ret = transfer_func(h, buf+len, size-len);
262         if (ret == AVERROR(EINTR))
263             continue;
264         if (h->flags & AVIO_FLAG_NONBLOCK)
265             return ret;
266         if (ret == AVERROR(EAGAIN)) {
267             ret = 0;
268             if (fast_retries)
269                 fast_retries--;
270             else
271                 usleep(1000);
272         } else if (ret < 1)
273             return ret < 0 ? ret : len;
274         if (ret)
275            fast_retries = FFMAX(fast_retries, 2);
276         len += ret;
277         if (len < size && ff_check_interrupt(&h->interrupt_callback))
278             return AVERROR_EXIT;
279     }
280     return len;
281 }
282
283 int ffurl_read(URLContext *h, unsigned char *buf, int size)
284 {
285     if (!(h->flags & AVIO_FLAG_READ))
286         return AVERROR(EIO);
287     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
288 }
289
290 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
291 {
292     if (!(h->flags & AVIO_FLAG_READ))
293         return AVERROR(EIO);
294     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
295 }
296
297 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
298 {
299     if (!(h->flags & AVIO_FLAG_WRITE))
300         return AVERROR(EIO);
301     /* avoid sending too big packets */
302     if (h->max_packet_size && size > h->max_packet_size)
303         return AVERROR(EIO);
304
305     return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
306 }
307
308 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
309 {
310     int64_t ret;
311
312     if (!h->prot->url_seek)
313         return AVERROR(ENOSYS);
314     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
315     return ret;
316 }
317
318 int ffurl_close(URLContext *h)
319 {
320     int ret = 0;
321     if (!h) return 0; /* can happen when ffurl_open fails */
322
323     if (h->is_connected && h->prot->url_close)
324         ret = h->prot->url_close(h);
325 #if CONFIG_NETWORK
326     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
327         ff_network_close();
328 #endif
329     if (h->prot->priv_data_size) {
330         if (h->prot->priv_data_class)
331             av_opt_free(h->priv_data);
332         av_free(h->priv_data);
333     }
334     av_free(h);
335     return ret;
336 }
337
338 int avio_check(const char *url, int flags)
339 {
340     URLContext *h;
341     int ret = ffurl_alloc(&h, url, flags, NULL);
342     if (ret)
343         return ret;
344
345     if (h->prot->url_check) {
346         ret = h->prot->url_check(h, flags);
347     } else {
348         ret = ffurl_connect(h, NULL);
349         if (ret >= 0)
350             ret = flags;
351     }
352
353     ffurl_close(h);
354     return ret;
355 }
356
357 int64_t ffurl_size(URLContext *h)
358 {
359     int64_t pos, size;
360
361     size= ffurl_seek(h, 0, AVSEEK_SIZE);
362     if(size<0){
363         pos = ffurl_seek(h, 0, SEEK_CUR);
364         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
365             return size;
366         size++;
367         ffurl_seek(h, pos, SEEK_SET);
368     }
369     return size;
370 }
371
372 int ffurl_get_file_handle(URLContext *h)
373 {
374     if (!h->prot->url_get_file_handle)
375         return -1;
376     return h->prot->url_get_file_handle(h);
377 }
378
379 int ff_check_interrupt(AVIOInterruptCB *cb)
380 {
381     int ret;
382     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
383         return ret;
384     return 0;
385 }
386