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.

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