Replace usleep() calls with av_usleep()
[libav.git] / libavformat / avio.c
CommitLineData
de6d9b64 1/*
124e2884 2 * unbuffered I/O
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
DB
21
22#include "libavutil/avstring.h"
ddffc2fd 23#include "libavutil/dict.h"
6ed04040 24#include "libavutil/opt.h"
896bb0d7 25#include "libavutil/time.h"
bc574408 26#include "os_support.h"
de6d9b64 27#include "avformat.h"
67d4b3f2
MS
28#if CONFIG_NETWORK
29#include "network.h"
30#endif
5652bb94 31#include "url.h"
5acef35f 32
163a3113
AK
33static URLProtocol *first_protocol = NULL;
34
35URLProtocol *ffurl_protocol_next(URLProtocol *prev)
36{
37 return prev ? prev->next : first_protocol;
38}
39
5acef35f
BA
40/** @name Logging context. */
41/*@{*/
42static const char *urlcontext_to_name(void *ptr)
43{
44 URLContext *h = (URLContext *)ptr;
45 if(h->prot) return h->prot->name;
46 else return "NULL";
47}
ddffc2fd
AK
48
49static void *urlcontext_child_next(void *obj, void *prev)
50{
51 URLContext *h = obj;
52 if (!prev && h->priv_data && h->prot->priv_data_class)
53 return h->priv_data;
54 return NULL;
55}
56
57static const AVClass *urlcontext_child_class_next(const AVClass *prev)
58{
59 URLProtocol *p = NULL;
60
61 /* find the protocol that corresponds to prev */
62 while (prev && (p = ffurl_protocol_next(p)))
63 if (p->priv_data_class == prev)
64 break;
65
66 /* find next protocol with priv options */
67 while (p = ffurl_protocol_next(p))
68 if (p->priv_data_class)
69 return p->priv_data_class;
70 return NULL;
71
72}
73
5acef35f 74static const AVOption options[] = {{NULL}};
1dee0aca 75const AVClass ffurl_context_class = {
f0029cbc
AK
76 .class_name = "URLContext",
77 .item_name = urlcontext_to_name,
78 .option = options,
79 .version = LIBAVUTIL_VERSION_INT,
ddffc2fd
AK
80 .child_next = urlcontext_child_next,
81 .child_class_next = urlcontext_child_class_next,
f0029cbc 82};
5acef35f 83/*@}*/
de6d9b64 84
019ac05a 85
f8270bbf
AK
86const char *avio_enum_protocols(void **opaque, int output)
87{
88 URLProtocol **p = opaque;
163a3113 89 *p = ffurl_protocol_next(*p);
f8270bbf
AK
90 if (!*p) return NULL;
91 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
92 return (*p)->name;
93 return avio_enum_protocols(opaque, output);
84be6e72
MN
94}
95
8e76a19b 96int ffurl_register_protocol(URLProtocol *protocol, int size)
de6d9b64
FB
97{
98 URLProtocol **p;
9b07a2dc
MS
99 if (size < sizeof(URLProtocol)) {
100 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
101 memcpy(temp, protocol, size);
102 protocol = temp;
103 }
de6d9b64
FB
104 p = &first_protocol;
105 while (*p != NULL) p = &(*p)->next;
106 *p = protocol;
107 protocol->next = NULL;
108 return 0;
109}
110
ffbb289a 111static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
6f1b7b39
MS
112 const char *filename, int flags,
113 const AVIOInterruptCB *int_cb)
de6d9b64
FB
114{
115 URLContext *uc;
de6d9b64
FB
116 int err;
117
67d4b3f2 118#if CONFIG_NETWORK
acb07430 119 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
67d4b3f2
MS
120 return AVERROR(EIO);
121#endif
31277aeb 122 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
8a9488b5 123 if (!uc) {
8fa36ae0 124 err = AVERROR(ENOMEM);
8a9488b5
FB
125 goto fail;
126 }
1dee0aca 127 uc->av_class = &ffurl_context_class;
62181004 128 uc->filename = (char *) &uc[1];
f746a046 129 strcpy(uc->filename, filename);
de6d9b64
FB
130 uc->prot = up;
131 uc->flags = flags;
132 uc->is_streamed = 0; /* default = not streamed */
8a9488b5 133 uc->max_packet_size = 0; /* default: stream file */
735cf6b2
MS
134 if (up->priv_data_size) {
135 uc->priv_data = av_mallocz(up->priv_data_size);
136 if (up->priv_data_class) {
805488f0 137 *(const AVClass**)uc->priv_data = up->priv_data_class;
735cf6b2
MS
138 av_opt_set_defaults(uc->priv_data);
139 }
140 }
6f1b7b39
MS
141 if (int_cb)
142 uc->interrupt_callback = *int_cb;
abbae514 143
de6d9b64
FB
144 *puc = uc;
145 return 0;
8a9488b5
FB
146 fail:
147 *puc = NULL;
67d4b3f2 148#if CONFIG_NETWORK
acb07430
MS
149 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
150 ff_network_close();
67d4b3f2 151#endif
8a9488b5 152 return err;
de6d9b64
FB
153}
154
ddffc2fd 155int ffurl_connect(URLContext* uc, AVDictionary **options)
ffbb289a 156{
ddffc2fd 157 int err =
ddffc2fd 158 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
ddffc2fd 159 uc->prot->url_open(uc, uc->filename, uc->flags);
ffbb289a
MS
160 if (err)
161 return err;
162 uc->is_connected = 1;
58a48c65 163 //We must be careful here as ffurl_seek() could be slow, for example for http
59d96941 164 if( (uc->flags & AVIO_FLAG_WRITE)
ffbb289a 165 || !strcmp(uc->prot->name, "file"))
58a48c65 166 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
ffbb289a
MS
167 uc->is_streamed= 1;
168 return 0;
169}
170
2bab5d3e
MR
171#define URL_SCHEME_CHARS \
172 "abcdefghijklmnopqrstuvwxyz" \
173 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
174 "0123456789+-."
175
6f1b7b39
MS
176int ffurl_alloc(URLContext **puc, const char *filename, int flags,
177 const AVIOInterruptCB *int_cb)
ba99cfc2 178{
163a3113 179 URLProtocol *up = NULL;
8f73c060 180 char proto_str[128], proto_nested[128], *ptr;
2bab5d3e
MR
181 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
182
183 if (filename[proto_len] != ':' || is_dos_path(filename))
ba99cfc2 184 strcpy(proto_str, "file");
2bab5d3e
MR
185 else
186 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
ba99cfc2 187
8f73c060
MS
188 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
189 if ((ptr = strchr(proto_nested, '+')))
190 *ptr = '\0';
191
163a3113 192 while (up = ffurl_protocol_next(up)) {
ba99cfc2 193 if (!strcmp(proto_str, up->name))
6f1b7b39 194 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
8f73c060
MS
195 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
196 !strcmp(proto_nested, up->name))
6f1b7b39 197 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
ba99cfc2
RB
198 }
199 *puc = NULL;
200 return AVERROR(ENOENT);
201}
202
6f1b7b39 203int ffurl_open(URLContext **puc, const char *filename, int flags,
ddffc2fd 204 const AVIOInterruptCB *int_cb, AVDictionary **options)
ffbb289a 205{
6f1b7b39 206 int ret = ffurl_alloc(puc, filename, flags, int_cb);
ffbb289a
MS
207 if (ret)
208 return ret;
ddffc2fd
AK
209 if (options && (*puc)->prot->priv_data_class &&
210 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
211 goto fail;
212 ret = ffurl_connect(*puc, options);
ffbb289a
MS
213 if (!ret)
214 return 0;
ddffc2fd 215fail:
e52a9145 216 ffurl_close(*puc);
ffbb289a
MS
217 *puc = NULL;
218 return ret;
219}
220
90441276 221static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
a46f7516 222 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
0e848977
KS
223{
224 int ret, len;
f1d2b572 225 int fast_retries = 5;
0e848977
KS
226
227 len = 0;
90441276 228 while (len < size_min) {
a46f7516 229 ret = transfer_func(h, buf+len, size-len);
90441276
NG
230 if (ret == AVERROR(EINTR))
231 continue;
f87b1b37 232 if (h->flags & AVIO_FLAG_NONBLOCK)
90441276 233 return ret;
ddb901b7
RD
234 if (ret == AVERROR(EAGAIN)) {
235 ret = 0;
f1d2b572
RD
236 if (fast_retries)
237 fast_retries--;
238 else
896bb0d7 239 av_usleep(1000);
ddb901b7
RD
240 } else if (ret < 1)
241 return ret < 0 ? ret : len;
f1d2b572
RD
242 if (ret)
243 fast_retries = FFMAX(fast_retries, 2);
0e848977 244 len += ret;
9957cdbf 245 if (ff_check_interrupt(&h->interrupt_callback))
c76374c6 246 return AVERROR_EXIT;
0e848977
KS
247 }
248 return len;
249}
250
bc371aca 251int ffurl_read(URLContext *h, unsigned char *buf, int size)
90441276 252{
26f6b8c5 253 if (!(h->flags & AVIO_FLAG_READ))
90441276
NG
254 return AVERROR(EIO);
255 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
256}
257
dce37564 258int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
a46f7516 259{
26f6b8c5 260 if (!(h->flags & AVIO_FLAG_READ))
90441276
NG
261 return AVERROR(EIO);
262 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
a46f7516
MN
263}
264
925e908b 265int ffurl_write(URLContext *h, const unsigned char *buf, int size)
de6d9b64 266{
59d96941 267 if (!(h->flags & AVIO_FLAG_WRITE))
6f3e0b21 268 return AVERROR(EIO);
8a9488b5
FB
269 /* avoid sending too big packets */
270 if (h->max_packet_size && size > h->max_packet_size)
6f3e0b21 271 return AVERROR(EIO);
fe5feaeb 272
90441276 273 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
de6d9b64
FB
274}
275
58a48c65 276int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
de6d9b64 277{
bc5c918e 278 int64_t ret;
de6d9b64
FB
279
280 if (!h->prot->url_seek)
28894105 281 return AVERROR(ENOSYS);
493f54ad 282 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
de6d9b64
FB
283 return ret;
284}
285
e52a9145 286int ffurl_close(URLContext *h)
de6d9b64 287{
7a813b36 288 int ret = 0;
0589da0a 289 if (!h) return 0; /* can happen when ffurl_open fails */
de6d9b64 290
ffbb289a 291 if (h->is_connected && h->prot->url_close)
7a813b36 292 ret = h->prot->url_close(h);
67d4b3f2 293#if CONFIG_NETWORK
acb07430
MS
294 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
295 ff_network_close();
67d4b3f2 296#endif
d10361b6
MS
297 if (h->prot->priv_data_size) {
298 if (h->prot->priv_data_class)
299 av_opt_free(h->priv_data);
735cf6b2 300 av_free(h->priv_data);
d10361b6 301 }
1ea4f593 302 av_free(h);
de6d9b64
FB
303 return ret;
304}
305
175389c8
SS
306int avio_check(const char *url, int flags)
307{
308 URLContext *h;
6f1b7b39 309 int ret = ffurl_alloc(&h, url, flags, NULL);
175389c8
SS
310 if (ret)
311 return ret;
312
313 if (h->prot->url_check) {
314 ret = h->prot->url_check(h, flags);
315 } else {
ddffc2fd 316 ret = ffurl_connect(h, NULL);
175389c8
SS
317 if (ret >= 0)
318 ret = flags;
319 }
320
321 ffurl_close(h);
322 return ret;
323}
324
32a97d46 325int64_t ffurl_size(URLContext *h)
de6d9b64 326{
bc5c918e 327 int64_t pos, size;
115329f1 328
58a48c65 329 size= ffurl_seek(h, 0, AVSEEK_SIZE);
8e287af0 330 if(size<0){
58a48c65
AK
331 pos = ffurl_seek(h, 0, SEEK_CUR);
332 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
eabbae73
RB
333 return size;
334 size++;
58a48c65 335 ffurl_seek(h, pos, SEEK_SET);
8e287af0 336 }
de6d9b64
FB
337 return size;
338}
8a9488b5 339
1869ea03 340int ffurl_get_file_handle(URLContext *h)
f0a80394
RB
341{
342 if (!h->prot->url_get_file_handle)
343 return -1;
344 return h->prot->url_get_file_handle(h);
345}
346
32d545e0
SP
347int ffurl_shutdown(URLContext *h, int flags)
348{
349 if (!h->prot->url_shutdown)
350 return AVERROR(EINVAL);
351 return h->prot->url_shutdown(h, flags);
352}
353
c4a090dd
MS
354int ff_check_interrupt(AVIOInterruptCB *cb)
355{
356 int ret;
357 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
358 return ret;
45f511ec 359 return 0;
c4a090dd 360}