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