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.

449 lines
13KB

  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. #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(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 != NULL)
  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. if ((flags & AVIO_FLAG_READ) && !up->url_read) {
  107. av_log(NULL, AV_LOG_ERROR,
  108. "Impossible to open the '%s' protocol for reading\n", up->name);
  109. return AVERROR(EIO);
  110. }
  111. if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
  112. av_log(NULL, AV_LOG_ERROR,
  113. "Impossible to open the '%s' protocol for writing\n", up->name);
  114. return AVERROR(EIO);
  115. }
  116. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  117. if (!uc) {
  118. err = AVERROR(ENOMEM);
  119. goto fail;
  120. }
  121. uc->av_class = &ffurl_context_class;
  122. uc->filename = (char *)&uc[1];
  123. strcpy(uc->filename, filename);
  124. uc->prot = up;
  125. uc->flags = flags;
  126. uc->is_streamed = 0; /* default = not streamed */
  127. uc->max_packet_size = 0; /* default: stream file */
  128. if (up->priv_data_size) {
  129. uc->priv_data = av_mallocz(up->priv_data_size);
  130. if (!uc->priv_data) {
  131. err = AVERROR(ENOMEM);
  132. goto fail;
  133. }
  134. if (up->priv_data_class) {
  135. int proto_len= strlen(up->name);
  136. char *start = strchr(uc->filename, ',');
  137. *(const AVClass **)uc->priv_data = up->priv_data_class;
  138. av_opt_set_defaults(uc->priv_data);
  139. if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
  140. int ret= 0;
  141. char *p= start;
  142. char sep= *++p;
  143. char *key, *val;
  144. p++;
  145. while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
  146. *val= *key= 0;
  147. ret= av_opt_set(uc->priv_data, p, key+1, 0);
  148. if (ret == AVERROR_OPTION_NOT_FOUND)
  149. av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
  150. *val= *key= sep;
  151. p= val+1;
  152. }
  153. if(ret<0 || p!=key){
  154. av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
  155. av_freep(&uc->priv_data);
  156. av_freep(&uc);
  157. err = AVERROR(EINVAL);
  158. goto fail;
  159. }
  160. memmove(start, key+1, strlen(key));
  161. }
  162. }
  163. }
  164. if (int_cb)
  165. uc->interrupt_callback = *int_cb;
  166. *puc = uc;
  167. return 0;
  168. fail:
  169. *puc = NULL;
  170. if (uc)
  171. av_freep(&uc->priv_data);
  172. av_freep(&uc);
  173. #if CONFIG_NETWORK
  174. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  175. ff_network_close();
  176. #endif
  177. return err;
  178. }
  179. int ffurl_connect(URLContext *uc, AVDictionary **options)
  180. {
  181. int err =
  182. uc->prot->url_open2 ? uc->prot->url_open2(uc,
  183. uc->filename,
  184. uc->flags,
  185. options) :
  186. uc->prot->url_open(uc, uc->filename, uc->flags);
  187. if (err)
  188. return err;
  189. uc->is_connected = 1;
  190. /* We must be careful here as ffurl_seek() could be slow,
  191. * for example for http */
  192. if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
  193. if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  194. uc->is_streamed = 1;
  195. return 0;
  196. }
  197. #define URL_SCHEME_CHARS \
  198. "abcdefghijklmnopqrstuvwxyz" \
  199. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  200. "0123456789+-."
  201. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  202. const AVIOInterruptCB *int_cb)
  203. {
  204. URLProtocol *up = NULL;
  205. char proto_str[128], proto_nested[128], *ptr;
  206. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  207. if (!first_protocol) {
  208. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  209. "Missing call to av_register_all()?\n");
  210. }
  211. if (filename[proto_len] != ':' &&
  212. (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
  213. is_dos_path(filename))
  214. strcpy(proto_str, "file");
  215. else
  216. av_strlcpy(proto_str, filename,
  217. FFMIN(proto_len + 1, sizeof(proto_str)));
  218. if ((ptr = strchr(proto_str, ',')))
  219. *ptr = '\0';
  220. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  221. if ((ptr = strchr(proto_nested, '+')))
  222. *ptr = '\0';
  223. while (up = ffurl_protocol_next(up)) {
  224. if (!strcmp(proto_str, up->name))
  225. return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
  226. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  227. !strcmp(proto_nested, up->name))
  228. return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
  229. }
  230. *puc = NULL;
  231. if (!strcmp("https", proto_str))
  232. av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
  233. return AVERROR_PROTOCOL_NOT_FOUND;
  234. }
  235. int ffurl_open(URLContext **puc, const char *filename, int flags,
  236. const AVIOInterruptCB *int_cb, AVDictionary **options)
  237. {
  238. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  239. if (ret)
  240. return ret;
  241. if (options && (*puc)->prot->priv_data_class &&
  242. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  243. goto fail;
  244. ret = ffurl_connect(*puc, options);
  245. if (!ret)
  246. return 0;
  247. fail:
  248. ffurl_close(*puc);
  249. *puc = NULL;
  250. return ret;
  251. }
  252. static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
  253. int size, int size_min,
  254. int (*transfer_func)(URLContext *h,
  255. uint8_t *buf,
  256. int size))
  257. {
  258. int ret, len;
  259. int fast_retries = 5;
  260. int64_t wait_since = 0;
  261. len = 0;
  262. while (len < size_min) {
  263. if (ff_check_interrupt(&h->interrupt_callback))
  264. return AVERROR_EXIT;
  265. ret = transfer_func(h, buf + len, size - len);
  266. if (ret == AVERROR(EINTR))
  267. continue;
  268. if (h->flags & AVIO_FLAG_NONBLOCK)
  269. return ret;
  270. if (ret == AVERROR(EAGAIN)) {
  271. ret = 0;
  272. if (fast_retries) {
  273. fast_retries--;
  274. } else {
  275. if (h->rw_timeout) {
  276. if (!wait_since)
  277. wait_since = av_gettime();
  278. else if (av_gettime() > wait_since + h->rw_timeout)
  279. return AVERROR(EIO);
  280. }
  281. av_usleep(1000);
  282. }
  283. } else if (ret < 1)
  284. return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
  285. if (ret)
  286. fast_retries = FFMAX(fast_retries, 2);
  287. len += ret;
  288. }
  289. return len;
  290. }
  291. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  292. {
  293. if (!(h->flags & AVIO_FLAG_READ))
  294. return AVERROR(EIO);
  295. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  296. }
  297. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  298. {
  299. if (!(h->flags & AVIO_FLAG_READ))
  300. return AVERROR(EIO);
  301. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  302. }
  303. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  304. {
  305. if (!(h->flags & AVIO_FLAG_WRITE))
  306. return AVERROR(EIO);
  307. /* avoid sending too big packets */
  308. if (h->max_packet_size && size > h->max_packet_size)
  309. return AVERROR(EIO);
  310. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
  311. }
  312. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  313. {
  314. int64_t ret;
  315. if (!h->prot->url_seek)
  316. return AVERROR(ENOSYS);
  317. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  318. return ret;
  319. }
  320. int ffurl_closep(URLContext **hh)
  321. {
  322. URLContext *h= *hh;
  323. int ret = 0;
  324. if (!h)
  325. return 0; /* can happen when ffurl_open fails */
  326. if (h->is_connected && h->prot->url_close)
  327. ret = h->prot->url_close(h);
  328. #if CONFIG_NETWORK
  329. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  330. ff_network_close();
  331. #endif
  332. if (h->prot->priv_data_size) {
  333. if (h->prot->priv_data_class)
  334. av_opt_free(h->priv_data);
  335. av_freep(&h->priv_data);
  336. }
  337. av_freep(hh);
  338. return ret;
  339. }
  340. int ffurl_close(URLContext *h)
  341. {
  342. return ffurl_closep(&h);
  343. }
  344. int avio_check(const char *url, int flags)
  345. {
  346. URLContext *h;
  347. int ret = ffurl_alloc(&h, url, flags, NULL);
  348. if (ret)
  349. return ret;
  350. if (h->prot->url_check) {
  351. ret = h->prot->url_check(h, flags);
  352. } else {
  353. ret = ffurl_connect(h, NULL);
  354. if (ret >= 0)
  355. ret = flags;
  356. }
  357. ffurl_close(h);
  358. return ret;
  359. }
  360. int64_t ffurl_size(URLContext *h)
  361. {
  362. int64_t pos, size;
  363. size = ffurl_seek(h, 0, AVSEEK_SIZE);
  364. if (size < 0) {
  365. pos = ffurl_seek(h, 0, SEEK_CUR);
  366. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  367. return size;
  368. size++;
  369. ffurl_seek(h, pos, SEEK_SET);
  370. }
  371. return size;
  372. }
  373. int ffurl_get_file_handle(URLContext *h)
  374. {
  375. if (!h->prot->url_get_file_handle)
  376. return -1;
  377. return h->prot->url_get_file_handle(h);
  378. }
  379. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  380. {
  381. if (!h->prot->url_get_multi_file_handle) {
  382. if (!h->prot->url_get_file_handle)
  383. return AVERROR(ENOSYS);
  384. *handles = av_malloc(sizeof(**handles));
  385. if (!*handles)
  386. return AVERROR(ENOMEM);
  387. *numhandles = 1;
  388. *handles[0] = h->prot->url_get_file_handle(h);
  389. return 0;
  390. }
  391. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  392. }
  393. int ffurl_shutdown(URLContext *h, int flags)
  394. {
  395. if (!h->prot->url_shutdown)
  396. return AVERROR(EINVAL);
  397. return h->prot->url_shutdown(h, flags);
  398. }
  399. int ff_check_interrupt(AVIOInterruptCB *cb)
  400. {
  401. int ret;
  402. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  403. return ret;
  404. return 0;
  405. }