FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "libavutil/avassert.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 
35 
37 {
38  return prev ? prev->next : first_protocol;
39 }
40 
41 /** @name Logging context. */
42 /*@{*/
43 static const char *urlcontext_to_name(void *ptr)
44 {
45  URLContext *h = (URLContext *)ptr;
46  if (h->prot)
47  return h->prot->name;
48  else
49  return "NULL";
50 }
51 
52 static void *urlcontext_child_next(void *obj, void *prev)
53 {
54  URLContext *h = obj;
55  if (!prev && h->priv_data && h->prot->priv_data_class)
56  return h->priv_data;
57  return NULL;
58 }
59 
60 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
61 {
62  URLProtocol *p = NULL;
63 
64  /* find the protocol that corresponds to prev */
65  while (prev && (p = ffurl_protocol_next(p)))
66  if (p->priv_data_class == prev)
67  break;
68 
69  /* find next protocol with priv options */
70  while (p = ffurl_protocol_next(p))
71  if (p->priv_data_class)
72  return p->priv_data_class;
73  return NULL;
74 }
75 
76 static const AVOption options[] = { { NULL } };
78  .class_name = "URLContext",
79  .item_name = urlcontext_to_name,
80  .option = options,
81  .version = LIBAVUTIL_VERSION_INT,
82  .child_next = urlcontext_child_next,
83  .child_class_next = urlcontext_child_class_next,
84 };
85 /*@}*/
86 
87 const char *avio_enum_protocols(void **opaque, int output)
88 {
89  URLProtocol *p;
90  *opaque = ffurl_protocol_next(*opaque);
91  if (!(p = *opaque))
92  return NULL;
93  if ((output && p->url_write) || (!output && p->url_read))
94  return p->name;
95  return avio_enum_protocols(opaque, output);
96 }
97 
99 {
100  URLProtocol **p;
101  p = &first_protocol;
102  while (*p)
103  p = &(*p)->next;
104  *p = protocol;
105  protocol->next = NULL;
106  return 0;
107 }
108 
109 static int url_alloc_for_protocol(URLContext **puc, struct URLProtocol *up,
110  const char *filename, int flags,
111  const AVIOInterruptCB *int_cb)
112 {
113  URLContext *uc;
114  int err;
115 
116 #if CONFIG_NETWORK
118  return AVERROR(EIO);
119 #endif
120  if ((flags & AVIO_FLAG_READ) && !up->url_read) {
122  "Impossible to open the '%s' protocol for reading\n", up->name);
123  return AVERROR(EIO);
124  }
125  if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
127  "Impossible to open the '%s' protocol for writing\n", up->name);
128  return AVERROR(EIO);
129  }
130  uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
131  if (!uc) {
132  err = AVERROR(ENOMEM);
133  goto fail;
134  }
136  uc->filename = (char *)&uc[1];
137  strcpy(uc->filename, filename);
138  uc->prot = up;
139  uc->flags = flags;
140  uc->is_streamed = 0; /* default = not streamed */
141  uc->max_packet_size = 0; /* default: stream file */
142  if (up->priv_data_size) {
144  if (!uc->priv_data) {
145  err = AVERROR(ENOMEM);
146  goto fail;
147  }
148  if (up->priv_data_class) {
149  int proto_len= strlen(up->name);
150  char *start = strchr(uc->filename, ',');
151  *(const AVClass **)uc->priv_data = up->priv_data_class;
153  if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
154  int ret= 0;
155  char *p= start;
156  char sep= *++p;
157  char *key, *val;
158  p++;
159  while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
160  *val= *key= 0;
161  ret= av_opt_set(uc->priv_data, p, key+1, 0);
162  if (ret == AVERROR_OPTION_NOT_FOUND)
163  av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
164  *val= *key= sep;
165  p= val+1;
166  }
167  if(ret<0 || p!=key){
168  av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
169  av_freep(&uc->priv_data);
170  av_freep(&uc);
171  err = AVERROR(EINVAL);
172  goto fail;
173  }
174  memmove(start, key+1, strlen(key));
175  }
176  }
177  }
178  if (int_cb)
179  uc->interrupt_callback = *int_cb;
180 
181  *puc = uc;
182  return 0;
183 fail:
184  *puc = NULL;
185  if (uc)
186  av_freep(&uc->priv_data);
187  av_freep(&uc);
188 #if CONFIG_NETWORK
191 #endif
192  return err;
193 }
194 
196 {
197  int err =
198  uc->prot->url_open2 ? uc->prot->url_open2(uc,
199  uc->filename,
200  uc->flags,
201  options) :
202  uc->prot->url_open(uc, uc->filename, uc->flags);
203  if (err)
204  return err;
205  uc->is_connected = 1;
206  /* We must be careful here as ffurl_seek() could be slow,
207  * for example for http */
208  if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
209  if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
210  uc->is_streamed = 1;
211  return 0;
212 }
213 
214 #define URL_SCHEME_CHARS \
215  "abcdefghijklmnopqrstuvwxyz" \
216  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
217  "0123456789+-."
218 
219 static struct URLProtocol *url_find_protocol(const char *filename)
220 {
221  URLProtocol *up = NULL;
222  char proto_str[128], proto_nested[128], *ptr;
223  size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
224 
225  if (filename[proto_len] != ':' &&
226  (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
227  is_dos_path(filename))
228  strcpy(proto_str, "file");
229  else
230  av_strlcpy(proto_str, filename,
231  FFMIN(proto_len + 1, sizeof(proto_str)));
232 
233  if ((ptr = strchr(proto_str, ',')))
234  *ptr = '\0';
235  av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
236  if ((ptr = strchr(proto_nested, '+')))
237  *ptr = '\0';
238 
239  while (up = ffurl_protocol_next(up)) {
240  if (!strcmp(proto_str, up->name))
241  break;
243  !strcmp(proto_nested, up->name))
244  break;
245  }
246 
247  return up;
248 }
249 
250 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
251  const AVIOInterruptCB *int_cb)
252 {
253  URLProtocol *p = NULL;
254 
255  if (!first_protocol) {
256  av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
257  "Missing call to av_register_all()?\n");
258  }
259 
260  p = url_find_protocol(filename);
261  if (p)
262  return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
263 
264  *puc = NULL;
265  if (av_strstart(filename, "https:", NULL))
266  av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
268 }
269 
270 int ffurl_open(URLContext **puc, const char *filename, int flags,
271  const AVIOInterruptCB *int_cb, AVDictionary **options)
272 {
273  int ret = ffurl_alloc(puc, filename, flags, int_cb);
274  if (ret < 0)
275  return ret;
276  if (options && (*puc)->prot->priv_data_class &&
277  (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
278  goto fail;
279  if ((ret = av_opt_set_dict(*puc, options)) < 0)
280  goto fail;
281  ret = ffurl_connect(*puc, options);
282  if (!ret)
283  return 0;
284 fail:
285  ffurl_close(*puc);
286  *puc = NULL;
287  return ret;
288 }
289 
291  int size, int size_min,
292  int (*transfer_func)(URLContext *h,
293  uint8_t *buf,
294  int size))
295 {
296  int ret, len;
297  int fast_retries = 5;
298  int64_t wait_since = 0;
299 
300  len = 0;
301  while (len < size_min) {
303  return AVERROR_EXIT;
304  ret = transfer_func(h, buf + len, size - len);
305  if (ret == AVERROR(EINTR))
306  continue;
307  if (h->flags & AVIO_FLAG_NONBLOCK)
308  return ret;
309  if (ret == AVERROR(EAGAIN)) {
310  ret = 0;
311  if (fast_retries) {
312  fast_retries--;
313  } else {
314  if (h->rw_timeout) {
315  if (!wait_since)
316  wait_since = av_gettime_relative();
317  else if (av_gettime_relative() > wait_since + h->rw_timeout)
318  return AVERROR(EIO);
319  }
320  av_usleep(1000);
321  }
322  } else if (ret < 1)
323  return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
324  if (ret)
325  fast_retries = FFMAX(fast_retries, 2);
326  len += ret;
327  }
328  return len;
329 }
330 
331 int ffurl_read(URLContext *h, unsigned char *buf, int size)
332 {
333  if (!(h->flags & AVIO_FLAG_READ))
334  return AVERROR(EIO);
335  return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
336 }
337 
338 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
339 {
340  if (!(h->flags & AVIO_FLAG_READ))
341  return AVERROR(EIO);
342  return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
343 }
344 
345 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
346 {
347  if (!(h->flags & AVIO_FLAG_WRITE))
348  return AVERROR(EIO);
349  /* avoid sending too big packets */
350  if (h->max_packet_size && size > h->max_packet_size)
351  return AVERROR(EIO);
352 
353  return retry_transfer_wrapper(h, (unsigned char *)buf, size, size,
354  (int (*)(struct URLContext *, uint8_t *, int))
355  h->prot->url_write);
356 }
357 
358 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
359 {
360  int64_t ret;
361 
362  if (!h->prot->url_seek)
363  return AVERROR(ENOSYS);
364  ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
365  return ret;
366 }
367 
369 {
370  URLContext *h= *hh;
371  int ret = 0;
372  if (!h)
373  return 0; /* can happen when ffurl_open fails */
374 
375  if (h->is_connected && h->prot->url_close)
376  ret = h->prot->url_close(h);
377 #if CONFIG_NETWORK
380 #endif
381  if (h->prot->priv_data_size) {
382  if (h->prot->priv_data_class)
384  av_freep(&h->priv_data);
385  }
386  av_freep(hh);
387  return ret;
388 }
389 
391 {
392  return ffurl_closep(&h);
393 }
394 
395 
396 const char *avio_find_protocol_name(const char *url)
397 {
398  URLProtocol *p = url_find_protocol(url);
399 
400  return p ? p->name : NULL;
401 }
402 
403 int avio_check(const char *url, int flags)
404 {
405  URLContext *h;
406  int ret = ffurl_alloc(&h, url, flags, NULL);
407  if (ret < 0)
408  return ret;
409 
410  if (h->prot->url_check) {
411  ret = h->prot->url_check(h, flags);
412  } else {
413  ret = ffurl_connect(h, NULL);
414  if (ret >= 0)
415  ret = flags;
416  }
417 
418  ffurl_close(h);
419  return ret;
420 }
421 
422 int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
423 {
424  URLContext *h = NULL;
425  AVIODirContext *ctx = NULL;
426  int ret;
427  av_assert0(s);
428 
429  ctx = av_mallocz(sizeof(*ctx));
430  if (!ctx) {
431  ret = AVERROR(ENOMEM);
432  goto fail;
433  }
434 
435  if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
436  goto fail;
437 
438  if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
439  if (options && h->prot->priv_data_class &&
440  (ret = av_opt_set_dict(h->priv_data, options)) < 0)
441  goto fail;
442  ret = h->prot->url_open_dir(h);
443  } else
444  ret = AVERROR(ENOSYS);
445  if (ret < 0)
446  goto fail;
447 
448  ctx->url_context = h;
449  *s = ctx;
450  return 0;
451 
452  fail:
453  av_free(ctx);
454  *s = NULL;
455  ffurl_close(h);
456  return ret;
457 }
458 
460 {
461  URLContext *h;
462  int ret;
463 
464  if (!s || !s->url_context)
465  return AVERROR(EINVAL);
466  h = s->url_context;
467  if ((ret = h->prot->url_read_dir(h, next)) < 0)
469  return ret;
470 }
471 
473 {
474  URLContext *h;
475 
476  av_assert0(s);
477  if (!(*s) || !(*s)->url_context)
478  return AVERROR(EINVAL);
479  h = (*s)->url_context;
480  h->prot->url_close_dir(h);
481  ffurl_close(h);
482  av_freep(s);
483  *s = NULL;
484  return 0;
485 }
486 
488 {
489  if (!entry || !*entry)
490  return;
491  av_free((*entry)->name);
492  av_freep(entry);
493 }
494 
496 {
497  int64_t pos, size;
498 
499  size = ffurl_seek(h, 0, AVSEEK_SIZE);
500  if (size < 0) {
501  pos = ffurl_seek(h, 0, SEEK_CUR);
502  if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
503  return size;
504  size++;
505  ffurl_seek(h, pos, SEEK_SET);
506  }
507  return size;
508 }
509 
511 {
512  if (!h->prot->url_get_file_handle)
513  return -1;
514  return h->prot->url_get_file_handle(h);
515 }
516 
517 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
518 {
519  if (!h->prot->url_get_multi_file_handle) {
520  if (!h->prot->url_get_file_handle)
521  return AVERROR(ENOSYS);
522  *handles = av_malloc(sizeof(**handles));
523  if (!*handles)
524  return AVERROR(ENOMEM);
525  *numhandles = 1;
526  *handles[0] = h->prot->url_get_file_handle(h);
527  return 0;
528  }
529  return h->prot->url_get_multi_file_handle(h, handles, numhandles);
530 }
531 
533 {
534  if (!h->prot->url_shutdown)
535  return AVERROR(EINVAL);
536  return h->prot->url_shutdown(h, flags);
537 }
538 
540 {
541  int ret;
542  if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
543  return ret;
544  return 0;
545 }