Libav
avio.c
Go to the documentation of this file.
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 "libavutil/avstring.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/time.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 
34 
36 {
37  return prev ? prev->next : first_protocol;
38 }
39 
42 static const char *urlcontext_to_name(void *ptr)
43 {
44  URLContext *h = (URLContext *)ptr;
45  if (h->prot)
46  return h->prot->name;
47  else
48  return "NULL";
49 }
50 
51 static void *urlcontext_child_next(void *obj, void *prev)
52 {
53  URLContext *h = obj;
54  if (!prev && h->priv_data && h->prot->priv_data_class)
55  return h->priv_data;
56  return NULL;
57 }
58 
59 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
60 {
61  URLProtocol *p = NULL;
62 
63  /* find the protocol that corresponds to prev */
64  while (prev && (p = ffurl_protocol_next(p)))
65  if (p->priv_data_class == prev)
66  break;
67 
68  /* find next protocol with priv options */
69  while (p = ffurl_protocol_next(p))
70  if (p->priv_data_class)
71  return p->priv_data_class;
72  return NULL;
73 }
74 
75 static const AVOption options[] = { { NULL } };
77  .class_name = "URLContext",
78  .item_name = urlcontext_to_name,
79  .option = options,
80  .version = LIBAVUTIL_VERSION_INT,
81  .child_next = urlcontext_child_next,
82  .child_class_next = urlcontext_child_class_next,
83 };
86 const char *avio_enum_protocols(void **opaque, int output)
87 {
88  URLProtocol *p;
89  *opaque = ffurl_protocol_next(*opaque);
90  if (!(p = *opaque))
91  return NULL;
92  if ((output && p->url_write) || (!output && p->url_read))
93  return p->name;
94  return avio_enum_protocols(opaque, output);
95 }
96 
98 {
99  URLProtocol **p;
100  p = &first_protocol;
101  while (*p)
102  p = &(*p)->next;
103  *p = protocol;
104  protocol->next = NULL;
105  return 0;
106 }
107 
108 static int url_alloc_for_protocol(URLContext **puc, struct URLProtocol *up,
109  const char *filename, int flags,
110  const AVIOInterruptCB *int_cb)
111 {
112  URLContext *uc;
113  int err;
114 
115 #if CONFIG_NETWORK
117  return AVERROR(EIO);
118 #endif
119  uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
120  if (!uc) {
121  err = AVERROR(ENOMEM);
122  goto fail;
123  }
125  uc->filename = (char *)&uc[1];
126  strcpy(uc->filename, filename);
127  uc->prot = up;
128  uc->flags = flags;
129  uc->is_streamed = 0; /* default = not streamed */
130  uc->max_packet_size = 0; /* default: stream file */
131  if (up->priv_data_size) {
133  if (!uc->priv_data) {
134  err = AVERROR(ENOMEM);
135  goto fail;
136  }
137  if (up->priv_data_class) {
138  *(const AVClass **)uc->priv_data = up->priv_data_class;
140  }
141  }
142  if (int_cb)
143  uc->interrupt_callback = *int_cb;
144 
145  *puc = uc;
146  return 0;
147 fail:
148  *puc = NULL;
149  if (uc)
150  av_freep(&uc->priv_data);
151  av_freep(&uc);
152 #if CONFIG_NETWORK
155 #endif
156  return err;
157 }
158 
160 {
161  int err =
162  uc->prot->url_open2 ? uc->prot->url_open2(uc,
163  uc->filename,
164  uc->flags,
165  options) :
166  uc->prot->url_open(uc, uc->filename, uc->flags);
167  if (err)
168  return err;
169  uc->is_connected = 1;
170  /* We must be careful here as ffurl_seek() could be slow,
171  * for example for http */
172  if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
173  if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
174  uc->is_streamed = 1;
175  return 0;
176 }
177 
178 #define URL_SCHEME_CHARS \
179  "abcdefghijklmnopqrstuvwxyz" \
180  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
181  "0123456789+-."
182 
183 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
184  const AVIOInterruptCB *int_cb)
185 {
186  URLProtocol *up = NULL;
187  char proto_str[128], proto_nested[128], *ptr;
188  size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
189 
190  if (filename[proto_len] != ':' || is_dos_path(filename))
191  strcpy(proto_str, "file");
192  else
193  av_strlcpy(proto_str, filename,
194  FFMIN(proto_len + 1, sizeof(proto_str)));
195 
196  av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
197  if ((ptr = strchr(proto_nested, '+')))
198  *ptr = '\0';
199 
200  while (up = ffurl_protocol_next(up)) {
201  if (!strcmp(proto_str, up->name))
202  return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
204  !strcmp(proto_nested, up->name))
205  return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
206  }
207  *puc = NULL;
209 }
210 
211 int ffurl_open(URLContext **puc, const char *filename, int flags,
212  const AVIOInterruptCB *int_cb, AVDictionary **options)
213 {
214  int ret = ffurl_alloc(puc, filename, flags, int_cb);
215  if (ret)
216  return ret;
217  if (options && (*puc)->prot->priv_data_class &&
218  (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
219  goto fail;
220  ret = ffurl_connect(*puc, options);
221  if (!ret)
222  return 0;
223 fail:
224  ffurl_close(*puc);
225  *puc = NULL;
226  return ret;
227 }
228 
229 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
230  int size, int size_min,
231  int (*transfer_func)(URLContext *h,
232  uint8_t *buf,
233  int size))
234 {
235  int ret, len;
236  int fast_retries = 5;
237 
238  len = 0;
239  while (len < size_min) {
240  ret = transfer_func(h, buf + len, size - len);
241  if (ret == AVERROR(EINTR))
242  continue;
243  if (h->flags & AVIO_FLAG_NONBLOCK)
244  return ret;
245  if (ret == AVERROR(EAGAIN)) {
246  ret = 0;
247  if (fast_retries)
248  fast_retries--;
249  else
250  av_usleep(1000);
251  } else if (ret < 1)
252  return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
253  if (ret)
254  fast_retries = FFMAX(fast_retries, 2);
255  len += ret;
257  return AVERROR_EXIT;
258  }
259  return len;
260 }
261 
262 int ffurl_read(URLContext *h, unsigned char *buf, int size)
263 {
264  if (!(h->flags & AVIO_FLAG_READ))
265  return AVERROR(EIO);
266  return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
267 }
268 
269 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
270 {
271  if (!(h->flags & AVIO_FLAG_READ))
272  return AVERROR(EIO);
273  return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
274 }
275 
276 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
277 {
278  if (!(h->flags & AVIO_FLAG_WRITE))
279  return AVERROR(EIO);
280  /* avoid sending too big packets */
281  if (h->max_packet_size && size > h->max_packet_size)
282  return AVERROR(EIO);
283 
284  return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
285 }
286 
287 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
288 {
289  int64_t ret;
290 
291  if (!h->prot->url_seek)
292  return AVERROR(ENOSYS);
293  ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
294  return ret;
295 }
296 
298 {
299  int ret = 0;
300  if (!h)
301  return 0; /* can happen when ffurl_open fails */
302 
303  if (h->is_connected && h->prot->url_close)
304  ret = h->prot->url_close(h);
305 #if CONFIG_NETWORK
308 #endif
309  if (h->prot->priv_data_size) {
310  if (h->prot->priv_data_class)
312  av_free(h->priv_data);
313  }
314  av_free(h);
315  return ret;
316 }
317 
318 int avio_check(const char *url, int flags)
319 {
320  URLContext *h;
321  int ret = ffurl_alloc(&h, url, flags, NULL);
322  if (ret)
323  return ret;
324 
325  if (h->prot->url_check) {
326  ret = h->prot->url_check(h, flags);
327  } else {
328  ret = ffurl_connect(h, NULL);
329  if (ret >= 0)
330  ret = flags;
331  }
332 
333  ffurl_close(h);
334  return ret;
335 }
336 
338 {
339  int64_t pos, size;
340 
341  size = ffurl_seek(h, 0, AVSEEK_SIZE);
342  if (size < 0) {
343  pos = ffurl_seek(h, 0, SEEK_CUR);
344  if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
345  return size;
346  size++;
347  ffurl_seek(h, pos, SEEK_SET);
348  }
349  return size;
350 }
351 
353 {
354  if (!h->prot->url_get_file_handle)
355  return -1;
356  return h->prot->url_get_file_handle(h);
357 }
358 
359 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
360 {
361  if (!h->prot->url_get_multi_file_handle) {
362  if (!h->prot->url_get_file_handle)
363  return AVERROR(ENOSYS);
364  *handles = av_malloc(sizeof(**handles));
365  if (!*handles)
366  return AVERROR(ENOMEM);
367  *numhandles = 1;
368  *handles[0] = h->prot->url_get_file_handle(h);
369  return 0;
370  }
371  return h->prot->url_get_multi_file_handle(h, handles, numhandles);
372 }
373 
375 {
376  if (!h->prot->url_shutdown)
377  return AVERROR(EINVAL);
378  return h->prot->url_shutdown(h, flags);
379 }
380 
382 {
383  int ret;
384  if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
385  return ret;
386  return 0;
387 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int size
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:35
AVOption.
Definition: opt.h:234
int(* url_check)(URLContext *h, int mask)
Definition: url.h:90
int flags
Definition: url.h:89
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:276
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:48
static URLProtocol * first_protocol
Definition: avio.c:33
int ffurl_connect(URLContext *uc, AVDictionary **options)
Connect an URLContext that has been allocated by ffurl_alloc.
Definition: avio.c:159
AVIOInterruptCB interrupt_callback
Definition: url.h:50
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:546
#define AVIO_FLAG_READ
read-only
Definition: avio.h:292
struct URLProtocol * prot
Definition: url.h:43
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:293
void ff_network_close(void)
Definition: network.c:150
int flags
Definition: url.h:46
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:54
static void * urlcontext_child_next(void *obj, void *prev)
Definition: avio.c:51
int ffurl_shutdown(URLContext *h, int flags)
Signal the URLContext that we are done reading or writing the stream.
Definition: avio.c:374
#define URL_SCHEME_CHARS
Definition: avio.c:178
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
void * opaque
Definition: avio.h:53
const AVClass * priv_data_class
Definition: url.h:88
struct URLProtocol * next
Definition: url.h:79
int avio_check(const char *url, int flags)
Return AVIO_FLAG_* access flags corresponding to the access permissions of the resource in url...
Definition: avio.c:318
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
Public dictionary API.
uint8_t
int ff_network_init(void)
Definition: network.c:123
AVOptions.
miscellaneous OS support macros and functions.
static int url_alloc_for_protocol(URLContext **puc, struct URLProtocol *up, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Definition: avio.c:108
int(* url_get_file_handle)(URLContext *h)
Definition: url.h:83
#define AVERROR_PROTOCOL_NOT_FOUND
Protocol not found.
Definition: error.h:58
int(* url_get_multi_file_handle)(URLContext *h, int **handles, int *numhandles)
Definition: url.h:84
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
Callback for checking whether to abort blocking functions.
Definition: avio.h:51
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
Definition: avio.c:183
int(* url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options)
This callback is to be used by protocols which open further nested protocols.
Definition: url.h:61
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
Return the file descriptors associated with this URL.
Definition: avio.c:359
int(* callback)(void *)
Definition: avio.h:52
#define AVERROR(e)
Definition: error.h:43
#define URL_PROTOCOL_FLAG_NESTED_SCHEME
Definition: url.h:34
int(* url_open)(URLContext *h, const char *url, int flags)
Definition: url.h:55
#define FFMAX(a, b)
Definition: common.h:55
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:81
static const char * urlcontext_to_name(void *ptr)
Definition: avio.c:42
int(* url_write)(URLContext *h, const unsigned char *buf, int size)
Definition: url.h:76
int(* url_read)(URLContext *h, unsigned char *buf, int size)
Read data from the protocol.
Definition: url.h:75
static const AVOption options[]
Definition: avio.c:75
static int retry_transfer_wrapper(URLContext *h, uint8_t *buf, int size, int size_min, int(*transfer_func)(URLContext *h, uint8_t *buf, int size))
Definition: avio.c:229
const AVIOInterruptCB int_cb
Definition: avconv.c:141
#define FFMIN(a, b)
Definition: common.h:57
static const AVClass * urlcontext_child_class_next(const AVClass *prev)
Definition: avio.c:59
const char * avio_enum_protocols(void **opaque, int output)
Iterate through names of available protocols.
Definition: avio.c:86
int64_t(* url_seek)(URLContext *h, int64_t pos, int whence)
Definition: url.h:77
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:352
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:52
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int is_connected
Definition: url.h:49
NULL
Definition: eval.c:55
static int is_dos_path(const char *path)
Definition: os_support.h:52
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrup a blocking function associated with cb.
Definition: avio.c:381
int av_opt_set_dict(void *obj, AVDictionary **options)
Definition: opt.c:679
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:311
int64_t ffurl_size(URLContext *h)
Return the filesize of the resource accessed by h, AVERROR(ENOSYS) if the operation is not supported ...
Definition: avio.c:337
URLProtocol * ffurl_protocol_next(const URLProtocol *prev)
Iterate over all available protocols.
Definition: avio.c:35
Definition: url.h:41
Describe the class of an AVClass context structure.
Definition: log.h:33
#define AVSEEK_FORCE
Oring this flag as into the "whence" parameter to a seek function causes it to seek by any means (lik...
Definition: avio.h:198
void * priv_data
Definition: url.h:44
const char * name
Definition: url.h:54
const AVClass * av_class
information for av_log().
Definition: url.h:42
int ffurl_close(URLContext *h)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:297
const AVClass ffurl_context_class
Definition: avio.c:76
Main libavformat public API header.
int ffurl_register_protocol(URLProtocol *protocol)
Register the URLProtocol protocol.
Definition: avio.c:97
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:659
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h...
Definition: avio.c:287
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:211
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
Read as many bytes as possible (up to size), calling the read function multiple times if necessary...
Definition: avio.c:269
char * filename
specified URL
Definition: url.h:45
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:190
int len
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:47
int(* url_close)(URLContext *h)
Definition: url.h:78
unbuffered private I/O API
int(* url_shutdown)(URLContext *h, int flags)
Definition: url.h:86
int priv_data_size
Definition: url.h:87
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:262
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205