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