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.

381 lines
11KB

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