ffmpeg / libavformat / avio.c @ 727c7aa0
History | View | Annotate | Download (10.4 KB)
1 |
/*
|
---|---|
2 |
* Unbuffered io for ffmpeg system
|
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 |
/* needed for usleep() */
|
23 |
#define _XOPEN_SOURCE 600 |
24 |
#include <unistd.h> |
25 |
#include "libavutil/avstring.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 |
#if FF_API_URL_CLASS
|
35 |
/** @name Logging context. */
|
36 |
/*@{*/
|
37 |
static 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 |
} |
43 |
static const AVOption options[] = {{NULL}}; |
44 |
static const AVClass urlcontext_class = |
45 |
{ "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
|
46 |
/*@}*/
|
47 |
#endif
|
48 |
|
49 |
static int default_interrupt_cb(void); |
50 |
|
51 |
URLProtocol *first_protocol = NULL;
|
52 |
URLInterruptCB *url_interrupt_cb = default_interrupt_cb; |
53 |
|
54 |
URLProtocol *av_protocol_next(URLProtocol *p) |
55 |
{ |
56 |
if(p) return p->next; |
57 |
else return first_protocol; |
58 |
} |
59 |
|
60 |
int av_register_protocol2(URLProtocol *protocol, int size) |
61 |
{ |
62 |
URLProtocol **p; |
63 |
if (size < sizeof(URLProtocol)) { |
64 |
URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
|
65 |
memcpy(temp, protocol, size); |
66 |
protocol = temp; |
67 |
} |
68 |
p = &first_protocol; |
69 |
while (*p != NULL) p = &(*p)->next; |
70 |
*p = protocol; |
71 |
protocol->next = NULL;
|
72 |
return 0; |
73 |
} |
74 |
|
75 |
#if FF_API_REGISTER_PROTOCOL
|
76 |
/* The layout of URLProtocol as of when major was bumped to 52 */
|
77 |
struct 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 |
|
87 |
int av_register_protocol(URLProtocol *protocol)
|
88 |
{ |
89 |
return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat)); |
90 |
} |
91 |
|
92 |
int register_protocol(URLProtocol *protocol)
|
93 |
{ |
94 |
return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat)); |
95 |
} |
96 |
#endif
|
97 |
|
98 |
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up, |
99 |
const char *filename, int flags) |
100 |
{ |
101 |
URLContext *uc; |
102 |
int err;
|
103 |
|
104 |
#if CONFIG_NETWORK
|
105 |
if (!ff_network_init())
|
106 |
return AVERROR(EIO);
|
107 |
#endif
|
108 |
uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1); |
109 |
if (!uc) {
|
110 |
err = AVERROR(ENOMEM); |
111 |
goto fail;
|
112 |
} |
113 |
#if FF_API_URL_CLASS
|
114 |
uc->av_class = &urlcontext_class; |
115 |
#endif
|
116 |
uc->filename = (char *) &uc[1]; |
117 |
strcpy(uc->filename, filename); |
118 |
uc->prot = up; |
119 |
uc->flags = flags; |
120 |
uc->is_streamed = 0; /* default = not streamed */ |
121 |
uc->max_packet_size = 0; /* default: stream file */ |
122 |
if (up->priv_data_size) {
|
123 |
uc->priv_data = av_mallocz(up->priv_data_size); |
124 |
if (up->priv_data_class) {
|
125 |
*(const AVClass**)uc->priv_data = up->priv_data_class;
|
126 |
av_opt_set_defaults(uc->priv_data); |
127 |
} |
128 |
} |
129 |
|
130 |
*puc = uc; |
131 |
return 0; |
132 |
fail:
|
133 |
*puc = NULL;
|
134 |
#if CONFIG_NETWORK
|
135 |
ff_network_close(); |
136 |
#endif
|
137 |
return err;
|
138 |
} |
139 |
|
140 |
int ffurl_connect(URLContext* uc)
|
141 |
{ |
142 |
int err = uc->prot->url_open(uc, uc->filename, uc->flags);
|
143 |
if (err)
|
144 |
return err;
|
145 |
uc->is_connected = 1;
|
146 |
//We must be careful here as ffurl_seek() could be slow, for example for http
|
147 |
if( (uc->flags & (URL_WRONLY | URL_RDWR))
|
148 |
|| !strcmp(uc->prot->name, "file"))
|
149 |
if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0) |
150 |
uc->is_streamed= 1;
|
151 |
return 0; |
152 |
} |
153 |
|
154 |
#if FF_API_OLD_AVIO
|
155 |
int 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;
|
163 |
ret = ffurl_connect(*puc); |
164 |
if (!ret)
|
165 |
return 0; |
166 |
fail:
|
167 |
ffurl_close(*puc); |
168 |
*puc = NULL;
|
169 |
return ret;
|
170 |
} |
171 |
int url_alloc(URLContext **puc, const char *filename, int flags) |
172 |
{ |
173 |
return ffurl_alloc(puc, filename, flags);
|
174 |
} |
175 |
int url_connect(URLContext* uc)
|
176 |
{ |
177 |
return ffurl_connect(uc);
|
178 |
} |
179 |
int url_open(URLContext **puc, const char *filename, int flags) |
180 |
{ |
181 |
return ffurl_open(puc, filename, flags);
|
182 |
} |
183 |
int url_read(URLContext *h, unsigned char *buf, int size) |
184 |
{ |
185 |
return ffurl_read(h, buf, size);
|
186 |
} |
187 |
int url_read_complete(URLContext *h, unsigned char *buf, int size) |
188 |
{ |
189 |
return ffurl_read_complete(h, buf, size);
|
190 |
} |
191 |
int url_write(URLContext *h, const unsigned char *buf, int size) |
192 |
{ |
193 |
return ffurl_write(h, buf, size);
|
194 |
} |
195 |
int64_t url_seek(URLContext *h, int64_t pos, int whence)
|
196 |
{ |
197 |
return ffurl_seek(h, pos, whence);
|
198 |
} |
199 |
int url_close(URLContext *h)
|
200 |
{ |
201 |
return ffurl_close(h);
|
202 |
} |
203 |
int64_t url_filesize(URLContext *h) |
204 |
{ |
205 |
return ffurl_size(h);
|
206 |
} |
207 |
int url_get_file_handle(URLContext *h)
|
208 |
{ |
209 |
return ffurl_get_file_handle(h);
|
210 |
} |
211 |
int url_get_max_packet_size(URLContext *h)
|
212 |
{ |
213 |
return h->max_packet_size;
|
214 |
} |
215 |
void url_get_filename(URLContext *h, char *buf, int buf_size) |
216 |
{ |
217 |
av_strlcpy(buf, h->filename, buf_size); |
218 |
} |
219 |
#endif
|
220 |
|
221 |
#define URL_SCHEME_CHARS \
|
222 |
"abcdefghijklmnopqrstuvwxyz" \
|
223 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
224 |
"0123456789+-."
|
225 |
|
226 |
int ffurl_alloc(URLContext **puc, const char *filename, int flags) |
227 |
{ |
228 |
URLProtocol *up; |
229 |
char proto_str[128], proto_nested[128], *ptr; |
230 |
size_t proto_len = strspn(filename, URL_SCHEME_CHARS); |
231 |
|
232 |
if (filename[proto_len] != ':' || is_dos_path(filename)) |
233 |
strcpy(proto_str, "file");
|
234 |
else
|
235 |
av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str))); |
236 |
|
237 |
av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
|
238 |
if ((ptr = strchr(proto_nested, '+'))) |
239 |
*ptr = '\0';
|
240 |
|
241 |
up = first_protocol; |
242 |
while (up != NULL) { |
243 |
if (!strcmp(proto_str, up->name))
|
244 |
return url_alloc_for_protocol (puc, up, filename, flags);
|
245 |
if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
|
246 |
!strcmp(proto_nested, up->name)) |
247 |
return url_alloc_for_protocol (puc, up, filename, flags);
|
248 |
up = up->next; |
249 |
} |
250 |
*puc = NULL;
|
251 |
return AVERROR(ENOENT);
|
252 |
} |
253 |
|
254 |
int ffurl_open(URLContext **puc, const char *filename, int flags) |
255 |
{ |
256 |
int ret = ffurl_alloc(puc, filename, flags);
|
257 |
if (ret)
|
258 |
return ret;
|
259 |
ret = ffurl_connect(*puc); |
260 |
if (!ret)
|
261 |
return 0; |
262 |
ffurl_close(*puc); |
263 |
*puc = NULL;
|
264 |
return ret;
|
265 |
} |
266 |
|
267 |
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min, |
268 |
int (*transfer_func)(URLContext *h, unsigned char *buf, int size)) |
269 |
{ |
270 |
int ret, len;
|
271 |
int fast_retries = 5; |
272 |
|
273 |
len = 0;
|
274 |
while (len < size_min) {
|
275 |
ret = transfer_func(h, buf+len, size-len); |
276 |
if (ret == AVERROR(EINTR))
|
277 |
continue;
|
278 |
if (h->flags & URL_FLAG_NONBLOCK)
|
279 |
return ret;
|
280 |
if (ret == AVERROR(EAGAIN)) {
|
281 |
ret = 0;
|
282 |
if (fast_retries)
|
283 |
fast_retries--; |
284 |
else
|
285 |
usleep(1000);
|
286 |
} else if (ret < 1) |
287 |
return ret < 0 ? ret : len; |
288 |
if (ret)
|
289 |
fast_retries = FFMAX(fast_retries, 2);
|
290 |
len += ret; |
291 |
if (url_interrupt_cb())
|
292 |
return AVERROR_EXIT;
|
293 |
} |
294 |
return len;
|
295 |
} |
296 |
|
297 |
int ffurl_read(URLContext *h, unsigned char *buf, int size) |
298 |
{ |
299 |
if (h->flags & URL_WRONLY)
|
300 |
return AVERROR(EIO);
|
301 |
return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read); |
302 |
} |
303 |
|
304 |
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size) |
305 |
{ |
306 |
if (h->flags & URL_WRONLY)
|
307 |
return AVERROR(EIO);
|
308 |
return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
|
309 |
} |
310 |
|
311 |
int ffurl_write(URLContext *h, const unsigned char *buf, int size) |
312 |
{ |
313 |
if (!(h->flags & (URL_WRONLY | URL_RDWR)))
|
314 |
return AVERROR(EIO);
|
315 |
/* avoid sending too big packets */
|
316 |
if (h->max_packet_size && size > h->max_packet_size)
|
317 |
return AVERROR(EIO);
|
318 |
|
319 |
return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
|
320 |
} |
321 |
|
322 |
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
|
323 |
{ |
324 |
int64_t ret; |
325 |
|
326 |
if (!h->prot->url_seek)
|
327 |
return AVERROR(ENOSYS);
|
328 |
ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE); |
329 |
return ret;
|
330 |
} |
331 |
|
332 |
int ffurl_close(URLContext *h)
|
333 |
{ |
334 |
int ret = 0; |
335 |
if (!h) return 0; /* can happen when ffurl_open fails */ |
336 |
|
337 |
if (h->is_connected && h->prot->url_close)
|
338 |
ret = h->prot->url_close(h); |
339 |
#if CONFIG_NETWORK
|
340 |
ff_network_close(); |
341 |
#endif
|
342 |
if (h->prot->priv_data_size)
|
343 |
av_free(h->priv_data); |
344 |
av_free(h); |
345 |
return ret;
|
346 |
} |
347 |
|
348 |
int url_exist(const char *filename) |
349 |
{ |
350 |
URLContext *h; |
351 |
if (ffurl_open(&h, filename, URL_RDONLY) < 0) |
352 |
return 0; |
353 |
ffurl_close(h); |
354 |
return 1; |
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 |
static int default_interrupt_cb(void) |
380 |
{ |
381 |
return 0; |
382 |
} |
383 |
|
384 |
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
|
385 |
{ |
386 |
if (!interrupt_cb)
|
387 |
interrupt_cb = default_interrupt_cb; |
388 |
url_interrupt_cb = interrupt_cb; |
389 |
} |
390 |
|
391 |
int av_url_read_pause(URLContext *h, int pause) |
392 |
{ |
393 |
if (!h->prot->url_read_pause)
|
394 |
return AVERROR(ENOSYS);
|
395 |
return h->prot->url_read_pause(h, pause);
|
396 |
} |
397 |
|
398 |
int64_t av_url_read_seek(URLContext *h, |
399 |
int stream_index, int64_t timestamp, int flags) |
400 |
{ |
401 |
if (!h->prot->url_read_seek)
|
402 |
return AVERROR(ENOSYS);
|
403 |
return h->prot->url_read_seek(h, stream_index, timestamp, flags);
|
404 |
} |