avio: make url_filesize() internal.
[libav.git] / libavformat / avio.c
CommitLineData
de6d9b64
FB
1/*
2 * Unbuffered io for ffmpeg system
19720f15 3 * Copyright (c) 2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
245976da 21
f1d2b572
RD
22/* needed for usleep() */
23#define _XOPEN_SOURCE 600
24#include <unistd.h>
245976da 25#include "libavutil/avstring.h"
6ed04040 26#include "libavutil/opt.h"
bc574408 27#include "os_support.h"
de6d9b64 28#include "avformat.h"
67d4b3f2
MS
29#if CONFIG_NETWORK
30#include "network.h"
31#endif
5652bb94 32#include "url.h"
5acef35f 33
404eba44 34#if FF_API_URL_CLASS
5acef35f
BA
35/** @name Logging context. */
36/*@{*/
37static const char *urlcontext_to_name(void *ptr)
38{
39 URLContext *h = (URLContext *)ptr;
40 if(h->prot) return h->prot->name;
41 else return "NULL";
42}
43static const AVOption options[] = {{NULL}};
44static const AVClass urlcontext_class =
2308b6c1 45 { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
5acef35f
BA
46/*@}*/
47#endif
de6d9b64 48
019ac05a
FB
49static int default_interrupt_cb(void);
50
de6d9b64 51URLProtocol *first_protocol = NULL;
019ac05a 52URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
de6d9b64 53
84be6e72
MN
54URLProtocol *av_protocol_next(URLProtocol *p)
55{
56 if(p) return p->next;
57 else return first_protocol;
58}
59
9b07a2dc 60int av_register_protocol2(URLProtocol *protocol, int size)
de6d9b64
FB
61{
62 URLProtocol **p;
9b07a2dc
MS
63 if (size < sizeof(URLProtocol)) {
64 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
65 memcpy(temp, protocol, size);
66 protocol = temp;
67 }
de6d9b64
FB
68 p = &first_protocol;
69 while (*p != NULL) p = &(*p)->next;
70 *p = protocol;
71 protocol->next = NULL;
72 return 0;
73}
74
838b27b4 75#if FF_API_REGISTER_PROTOCOL
9b07a2dc
MS
76/* The layout of URLProtocol as of when major was bumped to 52 */
77struct URLProtocol_compat {
78 const char *name;
79 int (*url_open)(URLContext *h, const char *filename, int flags);
80 int (*url_read)(URLContext *h, unsigned char *buf, int size);
81 int (*url_write)(URLContext *h, unsigned char *buf, int size);
82 int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
83 int (*url_close)(URLContext *h);
84 struct URLProtocol *next;
85};
86
87int av_register_protocol(URLProtocol *protocol)
88{
89 return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
90}
91
65c40e4e
SS
92int register_protocol(URLProtocol *protocol)
93{
0a216bd1 94 return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
65c40e4e
SS
95}
96#endif
97
ffbb289a 98static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
02174293 99 const char *filename, int flags)
de6d9b64
FB
100{
101 URLContext *uc;
de6d9b64
FB
102 int err;
103
67d4b3f2
MS
104#if CONFIG_NETWORK
105 if (!ff_network_init())
106 return AVERROR(EIO);
107#endif
31277aeb 108 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
8a9488b5 109 if (!uc) {
8fa36ae0 110 err = AVERROR(ENOMEM);
8a9488b5
FB
111 goto fail;
112 }
404eba44 113#if FF_API_URL_CLASS
5acef35f
BA
114 uc->av_class = &urlcontext_class;
115#endif
62181004 116 uc->filename = (char *) &uc[1];
f746a046 117 strcpy(uc->filename, filename);
de6d9b64
FB
118 uc->prot = up;
119 uc->flags = flags;
120 uc->is_streamed = 0; /* default = not streamed */
8a9488b5 121 uc->max_packet_size = 0; /* default: stream file */
735cf6b2
MS
122 if (up->priv_data_size) {
123 uc->priv_data = av_mallocz(up->priv_data_size);
124 if (up->priv_data_class) {
805488f0 125 *(const AVClass**)uc->priv_data = up->priv_data_class;
735cf6b2
MS
126 av_opt_set_defaults(uc->priv_data);
127 }
128 }
abbae514 129
de6d9b64
FB
130 *puc = uc;
131 return 0;
8a9488b5
FB
132 fail:
133 *puc = NULL;
67d4b3f2
MS
134#if CONFIG_NETWORK
135 ff_network_close();
136#endif
8a9488b5 137 return err;
de6d9b64
FB
138}
139
62eaaeac 140int ffurl_connect(URLContext* uc)
ffbb289a
MS
141{
142 int err = uc->prot->url_open(uc, uc->filename, uc->flags);
143 if (err)
144 return err;
145 uc->is_connected = 1;
58a48c65 146 //We must be careful here as ffurl_seek() could be slow, for example for http
ffbb289a
MS
147 if( (uc->flags & (URL_WRONLY | URL_RDWR))
148 || !strcmp(uc->prot->name, "file"))
58a48c65 149 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
ffbb289a
MS
150 uc->is_streamed= 1;
151 return 0;
152}
153
333e8943 154#if FF_API_OLD_AVIO
ffbb289a
MS
155int url_open_protocol (URLContext **puc, struct URLProtocol *up,
156 const char *filename, int flags)
157{
158 int ret;
159
160 ret = url_alloc_for_protocol(puc, up, filename, flags);
161 if (ret)
162 goto fail;
62eaaeac 163 ret = ffurl_connect(*puc);
ffbb289a
MS
164 if (!ret)
165 return 0;
166 fail:
e52a9145 167 ffurl_close(*puc);
ffbb289a
MS
168 *puc = NULL;
169 return ret;
170}
5652bb94
AK
171int url_alloc(URLContext **puc, const char *filename, int flags)
172{
173 return ffurl_alloc(puc, filename, flags);
174}
62eaaeac
AK
175int url_connect(URLContext* uc)
176{
177 return ffurl_connect(uc);
178}
0589da0a
AK
179int url_open(URLContext **puc, const char *filename, int flags)
180{
181 return ffurl_open(puc, filename, flags);
182}
bc371aca
AK
183int url_read(URLContext *h, unsigned char *buf, int size)
184{
185 return ffurl_read(h, buf, size);
186}
dce37564
AK
187int url_read_complete(URLContext *h, unsigned char *buf, int size)
188{
189 return ffurl_read_complete(h, buf, size);
190}
925e908b
AK
191int url_write(URLContext *h, const unsigned char *buf, int size)
192{
193 return ffurl_write(h, buf, size);
194}
58a48c65
AK
195int64_t url_seek(URLContext *h, int64_t pos, int whence)
196{
197 return ffurl_seek(h, pos, whence);
198}
e52a9145
AK
199int url_close(URLContext *h)
200{
201 return ffurl_close(h);
202}
32a97d46
AK
203int64_t url_filesize(URLContext *h)
204{
205 return ffurl_size(h);
206}
333e8943 207#endif
ffbb289a 208
2bab5d3e
MR
209#define URL_SCHEME_CHARS \
210 "abcdefghijklmnopqrstuvwxyz" \
211 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
212 "0123456789+-."
213
5652bb94 214int ffurl_alloc(URLContext **puc, const char *filename, int flags)
ba99cfc2
RB
215{
216 URLProtocol *up;
8f73c060 217 char proto_str[128], proto_nested[128], *ptr;
2bab5d3e
MR
218 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
219
220 if (filename[proto_len] != ':' || is_dos_path(filename))
ba99cfc2 221 strcpy(proto_str, "file");
2bab5d3e
MR
222 else
223 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
ba99cfc2 224
8f73c060
MS
225 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
226 if ((ptr = strchr(proto_nested, '+')))
227 *ptr = '\0';
228
ba99cfc2
RB
229 up = first_protocol;
230 while (up != NULL) {
231 if (!strcmp(proto_str, up->name))
ffbb289a 232 return url_alloc_for_protocol (puc, up, filename, flags);
8f73c060
MS
233 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
234 !strcmp(proto_nested, up->name))
235 return url_alloc_for_protocol (puc, up, filename, flags);
ba99cfc2
RB
236 up = up->next;
237 }
238 *puc = NULL;
239 return AVERROR(ENOENT);
240}
241
0589da0a 242int ffurl_open(URLContext **puc, const char *filename, int flags)
ffbb289a 243{
5652bb94 244 int ret = ffurl_alloc(puc, filename, flags);
ffbb289a
MS
245 if (ret)
246 return ret;
62eaaeac 247 ret = ffurl_connect(*puc);
ffbb289a
MS
248 if (!ret)
249 return 0;
e52a9145 250 ffurl_close(*puc);
ffbb289a
MS
251 *puc = NULL;
252 return ret;
253}
254
90441276 255static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
a46f7516 256 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
0e848977
KS
257{
258 int ret, len;
f1d2b572 259 int fast_retries = 5;
0e848977
KS
260
261 len = 0;
90441276 262 while (len < size_min) {
a46f7516 263 ret = transfer_func(h, buf+len, size-len);
90441276
NG
264 if (ret == AVERROR(EINTR))
265 continue;
266 if (h->flags & URL_FLAG_NONBLOCK)
267 return ret;
ddb901b7
RD
268 if (ret == AVERROR(EAGAIN)) {
269 ret = 0;
f1d2b572
RD
270 if (fast_retries)
271 fast_retries--;
272 else
273 usleep(1000);
ddb901b7
RD
274 } else if (ret < 1)
275 return ret < 0 ? ret : len;
f1d2b572
RD
276 if (ret)
277 fast_retries = FFMAX(fast_retries, 2);
0e848977 278 len += ret;
688c22e0 279 if (url_interrupt_cb())
c76374c6 280 return AVERROR_EXIT;
0e848977
KS
281 }
282 return len;
283}
284
bc371aca 285int ffurl_read(URLContext *h, unsigned char *buf, int size)
90441276
NG
286{
287 if (h->flags & URL_WRONLY)
288 return AVERROR(EIO);
289 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
290}
291
dce37564 292int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
a46f7516 293{
90441276
NG
294 if (h->flags & URL_WRONLY)
295 return AVERROR(EIO);
296 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
a46f7516
MN
297}
298
925e908b 299int ffurl_write(URLContext *h, const unsigned char *buf, int size)
de6d9b64 300{
8a9488b5 301 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
6f3e0b21 302 return AVERROR(EIO);
8a9488b5
FB
303 /* avoid sending too big packets */
304 if (h->max_packet_size && size > h->max_packet_size)
6f3e0b21 305 return AVERROR(EIO);
fe5feaeb 306
90441276 307 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
de6d9b64
FB
308}
309
58a48c65 310int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
de6d9b64 311{
bc5c918e 312 int64_t ret;
de6d9b64
FB
313
314 if (!h->prot->url_seek)
28894105 315 return AVERROR(ENOSYS);
493f54ad 316 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
de6d9b64
FB
317 return ret;
318}
319
e52a9145 320int ffurl_close(URLContext *h)
de6d9b64 321{
7a813b36 322 int ret = 0;
0589da0a 323 if (!h) return 0; /* can happen when ffurl_open fails */
de6d9b64 324
ffbb289a 325 if (h->is_connected && h->prot->url_close)
7a813b36 326 ret = h->prot->url_close(h);
67d4b3f2
MS
327#if CONFIG_NETWORK
328 ff_network_close();
329#endif
735cf6b2
MS
330 if (h->prot->priv_data_size)
331 av_free(h->priv_data);
1ea4f593 332 av_free(h);
de6d9b64
FB
333 return ret;
334}
335
336int url_exist(const char *filename)
337{
338 URLContext *h;
0589da0a 339 if (ffurl_open(&h, filename, URL_RDONLY) < 0)
de6d9b64 340 return 0;
e52a9145 341 ffurl_close(h);
de6d9b64
FB
342 return 1;
343}
344
32a97d46 345int64_t ffurl_size(URLContext *h)
de6d9b64 346{
bc5c918e 347 int64_t pos, size;
115329f1 348
58a48c65 349 size= ffurl_seek(h, 0, AVSEEK_SIZE);
8e287af0 350 if(size<0){
58a48c65
AK
351 pos = ffurl_seek(h, 0, SEEK_CUR);
352 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
eabbae73
RB
353 return size;
354 size++;
58a48c65 355 ffurl_seek(h, pos, SEEK_SET);
8e287af0 356 }
de6d9b64
FB
357 return size;
358}
8a9488b5 359
f0a80394
RB
360int url_get_file_handle(URLContext *h)
361{
362 if (!h->prot->url_get_file_handle)
363 return -1;
364 return h->prot->url_get_file_handle(h);
365}
366
8a9488b5
FB
367int url_get_max_packet_size(URLContext *h)
368{
369 return h->max_packet_size;
370}
f746a046
FB
371
372void url_get_filename(URLContext *h, char *buf, int buf_size)
373{
75e61b0e 374 av_strlcpy(buf, h->filename, buf_size);
f746a046 375}
019ac05a
FB
376
377
378static int default_interrupt_cb(void)
379{
380 return 0;
381}
382
019ac05a
FB
383void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
384{
385 if (!interrupt_cb)
386 interrupt_cb = default_interrupt_cb;
387 url_interrupt_cb = interrupt_cb;
388}
536333a0 389
502bdf68 390int av_url_read_pause(URLContext *h, int pause)
536333a0
BA
391{
392 if (!h->prot->url_read_pause)
393 return AVERROR(ENOSYS);
502bdf68 394 return h->prot->url_read_pause(h, pause);
536333a0
BA
395}
396
bc5c918e 397int64_t av_url_read_seek(URLContext *h,
536333a0
BA
398 int stream_index, int64_t timestamp, int flags)
399{
400 if (!h->prot->url_read_seek)
401 return AVERROR(ENOSYS);
402 return h->prot->url_read_seek(h, stream_index, timestamp, flags);
403}