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.

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