You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
10KB

  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. #include "libavutil/avstring.h"
  22. #include "libavutil/dict.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/time.h"
  25. #include "os_support.h"
  26. #include "avformat.h"
  27. #if CONFIG_NETWORK
  28. #include "network.h"
  29. #endif
  30. #include "url.h"
  31. /** @name Logging context. */
  32. /*@{*/
  33. static const char *urlcontext_to_name(void *ptr)
  34. {
  35. URLContext *h = (URLContext *)ptr;
  36. if (h->prot)
  37. return h->prot->name;
  38. else
  39. return "NULL";
  40. }
  41. static void *urlcontext_child_next(void *obj, void *prev)
  42. {
  43. URLContext *h = obj;
  44. if (!prev && h->priv_data && h->prot->priv_data_class)
  45. return h->priv_data;
  46. return NULL;
  47. }
  48. static const AVOption options[] = { { NULL } };
  49. const AVClass ffurl_context_class = {
  50. .class_name = "URLContext",
  51. .item_name = urlcontext_to_name,
  52. .option = options,
  53. .version = LIBAVUTIL_VERSION_INT,
  54. .child_next = urlcontext_child_next,
  55. .child_class_next = ff_urlcontext_child_class_next,
  56. };
  57. /*@}*/
  58. static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up,
  59. const char *filename, int flags,
  60. const AVIOInterruptCB *int_cb,
  61. const URLProtocol **protocols)
  62. {
  63. URLContext *uc;
  64. int err;
  65. #if CONFIG_NETWORK
  66. if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
  67. return AVERROR(EIO);
  68. #endif
  69. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  70. if (!uc) {
  71. err = AVERROR(ENOMEM);
  72. goto fail;
  73. }
  74. uc->av_class = &ffurl_context_class;
  75. uc->filename = (char *)&uc[1];
  76. strcpy(uc->filename, filename);
  77. uc->prot = up;
  78. uc->flags = flags;
  79. uc->is_streamed = 0; /* default = not streamed */
  80. uc->max_packet_size = 0; /* default: stream file */
  81. uc->protocols = protocols;
  82. if (up->priv_data_size) {
  83. uc->priv_data = av_mallocz(up->priv_data_size);
  84. if (!uc->priv_data) {
  85. err = AVERROR(ENOMEM);
  86. goto fail;
  87. }
  88. if (up->priv_data_class) {
  89. *(const AVClass **)uc->priv_data = up->priv_data_class;
  90. av_opt_set_defaults(uc->priv_data);
  91. }
  92. }
  93. if (int_cb)
  94. uc->interrupt_callback = *int_cb;
  95. *puc = uc;
  96. return 0;
  97. fail:
  98. *puc = NULL;
  99. if (uc)
  100. av_freep(&uc->priv_data);
  101. av_freep(&uc);
  102. #if CONFIG_NETWORK
  103. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  104. ff_network_close();
  105. #endif
  106. return err;
  107. }
  108. int ffurl_connect(URLContext *uc, AVDictionary **options)
  109. {
  110. int err =
  111. uc->prot->url_open2 ? uc->prot->url_open2(uc,
  112. uc->filename,
  113. uc->flags,
  114. options) :
  115. uc->prot->url_open(uc, uc->filename, uc->flags);
  116. if (err)
  117. return err;
  118. uc->is_connected = 1;
  119. /* We must be careful here as ffurl_seek() could be slow,
  120. * for example for http */
  121. if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
  122. if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  123. uc->is_streamed = 1;
  124. return 0;
  125. }
  126. #define URL_SCHEME_CHARS \
  127. "abcdefghijklmnopqrstuvwxyz" \
  128. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  129. "0123456789+-."
  130. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  131. const AVIOInterruptCB *int_cb,
  132. const URLProtocol **protocols)
  133. {
  134. char proto_str[128], proto_nested[128], *ptr;
  135. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  136. int i;
  137. if (filename[proto_len] != ':' || is_dos_path(filename))
  138. strcpy(proto_str, "file");
  139. else
  140. av_strlcpy(proto_str, filename,
  141. FFMIN(proto_len + 1, sizeof(proto_str)));
  142. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  143. if ((ptr = strchr(proto_nested, '+')))
  144. *ptr = '\0';
  145. for (i = 0; protocols[i]; i++) {
  146. const URLProtocol *up = protocols[i];
  147. if (!strcmp(proto_str, up->name))
  148. return url_alloc_for_protocol(puc, up, filename, flags, int_cb,
  149. protocols);
  150. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  151. !strcmp(proto_nested, up->name))
  152. return url_alloc_for_protocol(puc, up, filename, flags, int_cb,
  153. protocols);
  154. }
  155. *puc = NULL;
  156. return AVERROR_PROTOCOL_NOT_FOUND;
  157. }
  158. int ffurl_open(URLContext **puc, const char *filename, int flags,
  159. const AVIOInterruptCB *int_cb, AVDictionary **options,
  160. const URLProtocol **protocols)
  161. {
  162. int ret = ffurl_alloc(puc, filename, flags, int_cb, protocols);
  163. if (ret)
  164. return ret;
  165. if (options &&
  166. (ret = av_opt_set_dict(*puc, options)) < 0)
  167. goto fail;
  168. if (options && (*puc)->prot->priv_data_class &&
  169. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  170. goto fail;
  171. ret = ffurl_connect(*puc, options);
  172. if (!ret)
  173. return 0;
  174. fail:
  175. ffurl_close(*puc);
  176. *puc = NULL;
  177. return ret;
  178. }
  179. static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
  180. int size, int size_min,
  181. int (*transfer_func)(URLContext *h,
  182. uint8_t *buf,
  183. int size))
  184. {
  185. int ret, len;
  186. int fast_retries = 5;
  187. len = 0;
  188. while (len < size_min) {
  189. ret = transfer_func(h, buf + len, size - len);
  190. if (ret == AVERROR(EINTR))
  191. continue;
  192. if (h->flags & AVIO_FLAG_NONBLOCK)
  193. return ret;
  194. if (ret == AVERROR(EAGAIN)) {
  195. ret = 0;
  196. if (fast_retries)
  197. fast_retries--;
  198. else
  199. av_usleep(1000);
  200. } else if (ret < 1)
  201. return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
  202. if (ret)
  203. fast_retries = FFMAX(fast_retries, 2);
  204. len += ret;
  205. if (ff_check_interrupt(&h->interrupt_callback))
  206. return AVERROR_EXIT;
  207. }
  208. return len;
  209. }
  210. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  211. {
  212. if (!(h->flags & AVIO_FLAG_READ))
  213. return AVERROR(EIO);
  214. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  215. }
  216. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  217. {
  218. if (!(h->flags & AVIO_FLAG_READ))
  219. return AVERROR(EIO);
  220. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  221. }
  222. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  223. {
  224. if (!(h->flags & AVIO_FLAG_WRITE))
  225. return AVERROR(EIO);
  226. /* avoid sending too big packets */
  227. if (h->max_packet_size && size > h->max_packet_size)
  228. return AVERROR(EIO);
  229. return retry_transfer_wrapper(h, buf, size, size,
  230. (int (*)(struct URLContext *, uint8_t *, int))
  231. h->prot->url_write);
  232. }
  233. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  234. {
  235. int64_t ret;
  236. if (!h->prot->url_seek)
  237. return AVERROR(ENOSYS);
  238. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  239. return ret;
  240. }
  241. int ffurl_close(URLContext *h)
  242. {
  243. int ret = 0;
  244. if (!h)
  245. return 0; /* can happen when ffurl_open fails */
  246. if (h->is_connected && h->prot->url_close)
  247. ret = h->prot->url_close(h);
  248. #if CONFIG_NETWORK
  249. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  250. ff_network_close();
  251. #endif
  252. if (h->prot->priv_data_size) {
  253. if (h->prot->priv_data_class)
  254. av_opt_free(h->priv_data);
  255. av_free(h->priv_data);
  256. }
  257. av_free(h);
  258. return ret;
  259. }
  260. int avio_check(const char *url, int flags)
  261. {
  262. const URLProtocol **protocols;
  263. URLContext *h;
  264. int ret;
  265. protocols = ffurl_get_protocols(NULL, NULL);
  266. if (!protocols)
  267. return AVERROR(ENOMEM);
  268. ret = ffurl_alloc(&h, url, flags, NULL, protocols);
  269. if (ret) {
  270. av_freep(&protocols);
  271. return ret;
  272. }
  273. if (h->prot->url_check) {
  274. ret = h->prot->url_check(h, flags);
  275. } else {
  276. ret = ffurl_connect(h, NULL);
  277. if (ret >= 0)
  278. ret = flags;
  279. }
  280. ffurl_close(h);
  281. av_freep(&protocols);
  282. return ret;
  283. }
  284. int64_t ffurl_size(URLContext *h)
  285. {
  286. int64_t pos, size;
  287. size = ffurl_seek(h, 0, AVSEEK_SIZE);
  288. if (size < 0) {
  289. pos = ffurl_seek(h, 0, SEEK_CUR);
  290. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  291. return size;
  292. size++;
  293. ffurl_seek(h, pos, SEEK_SET);
  294. }
  295. return size;
  296. }
  297. int ffurl_get_file_handle(URLContext *h)
  298. {
  299. if (!h->prot->url_get_file_handle)
  300. return -1;
  301. return h->prot->url_get_file_handle(h);
  302. }
  303. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  304. {
  305. if (!h->prot->url_get_multi_file_handle) {
  306. if (!h->prot->url_get_file_handle)
  307. return AVERROR(ENOSYS);
  308. *handles = av_malloc(sizeof(**handles));
  309. if (!*handles)
  310. return AVERROR(ENOMEM);
  311. *numhandles = 1;
  312. *handles[0] = h->prot->url_get_file_handle(h);
  313. return 0;
  314. }
  315. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  316. }
  317. int ffurl_shutdown(URLContext *h, int flags)
  318. {
  319. if (!h->prot->url_shutdown)
  320. return AVERROR(EINVAL);
  321. return h->prot->url_shutdown(h, flags);
  322. }
  323. int ff_check_interrupt(AVIOInterruptCB *cb)
  324. {
  325. int ret;
  326. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  327. return ret;
  328. return 0;
  329. }