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.

470 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. static struct URLProtocol *url_find_protocol(const char *filename)
  202. {
  203. URLProtocol *up = NULL;
  204. char proto_str[128], proto_nested[128], *ptr;
  205. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  206. if (filename[proto_len] != ':' &&
  207. (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
  208. is_dos_path(filename))
  209. strcpy(proto_str, "file");
  210. else
  211. av_strlcpy(proto_str, filename,
  212. FFMIN(proto_len + 1, sizeof(proto_str)));
  213. if ((ptr = strchr(proto_str, ',')))
  214. *ptr = '\0';
  215. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  216. if ((ptr = strchr(proto_nested, '+')))
  217. *ptr = '\0';
  218. while (up = ffurl_protocol_next(up)) {
  219. if (!strcmp(proto_str, up->name))
  220. break;
  221. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  222. !strcmp(proto_nested, up->name))
  223. break;
  224. }
  225. return up;
  226. }
  227. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  228. const AVIOInterruptCB *int_cb)
  229. {
  230. URLProtocol *p = NULL;
  231. if (!first_protocol) {
  232. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  233. "Missing call to av_register_all()?\n");
  234. }
  235. p = url_find_protocol(filename);
  236. if (p)
  237. return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
  238. *puc = NULL;
  239. if (av_strstart("https:", filename, NULL))
  240. av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
  241. return AVERROR_PROTOCOL_NOT_FOUND;
  242. }
  243. int ffurl_open(URLContext **puc, const char *filename, int flags,
  244. const AVIOInterruptCB *int_cb, AVDictionary **options)
  245. {
  246. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  247. if (ret < 0)
  248. return ret;
  249. if (options && (*puc)->prot->priv_data_class &&
  250. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  251. goto fail;
  252. if ((ret = av_opt_set_dict(*puc, options)) < 0)
  253. goto fail;
  254. ret = ffurl_connect(*puc, options);
  255. if (!ret)
  256. return 0;
  257. fail:
  258. ffurl_close(*puc);
  259. *puc = NULL;
  260. return ret;
  261. }
  262. static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
  263. int size, int size_min,
  264. int (*transfer_func)(URLContext *h,
  265. uint8_t *buf,
  266. int size))
  267. {
  268. int ret, len;
  269. int fast_retries = 5;
  270. int64_t wait_since = 0;
  271. len = 0;
  272. while (len < size_min) {
  273. if (ff_check_interrupt(&h->interrupt_callback))
  274. return AVERROR_EXIT;
  275. ret = transfer_func(h, buf + len, size - len);
  276. if (ret == AVERROR(EINTR))
  277. continue;
  278. if (h->flags & AVIO_FLAG_NONBLOCK)
  279. return ret;
  280. if (ret == AVERROR(EAGAIN)) {
  281. ret = 0;
  282. if (fast_retries) {
  283. fast_retries--;
  284. } else {
  285. if (h->rw_timeout) {
  286. if (!wait_since)
  287. wait_since = av_gettime_relative();
  288. else if (av_gettime_relative() > wait_since + h->rw_timeout)
  289. return AVERROR(EIO);
  290. }
  291. av_usleep(1000);
  292. }
  293. } else if (ret < 1)
  294. return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
  295. if (ret)
  296. fast_retries = FFMAX(fast_retries, 2);
  297. len += ret;
  298. }
  299. return len;
  300. }
  301. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  302. {
  303. if (!(h->flags & AVIO_FLAG_READ))
  304. return AVERROR(EIO);
  305. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  306. }
  307. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  308. {
  309. if (!(h->flags & AVIO_FLAG_READ))
  310. return AVERROR(EIO);
  311. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  312. }
  313. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  314. {
  315. if (!(h->flags & AVIO_FLAG_WRITE))
  316. return AVERROR(EIO);
  317. /* avoid sending too big packets */
  318. if (h->max_packet_size && size > h->max_packet_size)
  319. return AVERROR(EIO);
  320. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
  321. }
  322. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  323. {
  324. int64_t ret;
  325. if (!h->prot->url_seek)
  326. return AVERROR(ENOSYS);
  327. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  328. return ret;
  329. }
  330. int ffurl_closep(URLContext **hh)
  331. {
  332. URLContext *h= *hh;
  333. int ret = 0;
  334. if (!h)
  335. return 0; /* can happen when ffurl_open fails */
  336. if (h->is_connected && h->prot->url_close)
  337. ret = h->prot->url_close(h);
  338. #if CONFIG_NETWORK
  339. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  340. ff_network_close();
  341. #endif
  342. if (h->prot->priv_data_size) {
  343. if (h->prot->priv_data_class)
  344. av_opt_free(h->priv_data);
  345. av_freep(&h->priv_data);
  346. }
  347. av_freep(hh);
  348. return ret;
  349. }
  350. int ffurl_close(URLContext *h)
  351. {
  352. return ffurl_closep(&h);
  353. }
  354. const char *avio_find_protocol_name(const char *url)
  355. {
  356. URLProtocol *p = url_find_protocol(url);
  357. return p ? p->name : NULL;
  358. }
  359. int avio_check(const char *url, int flags)
  360. {
  361. URLContext *h;
  362. int ret = ffurl_alloc(&h, url, flags, NULL);
  363. if (ret < 0)
  364. return ret;
  365. if (h->prot->url_check) {
  366. ret = h->prot->url_check(h, flags);
  367. } else {
  368. ret = ffurl_connect(h, NULL);
  369. if (ret >= 0)
  370. ret = flags;
  371. }
  372. ffurl_close(h);
  373. return ret;
  374. }
  375. int64_t ffurl_size(URLContext *h)
  376. {
  377. int64_t pos, size;
  378. size = ffurl_seek(h, 0, AVSEEK_SIZE);
  379. if (size < 0) {
  380. pos = ffurl_seek(h, 0, SEEK_CUR);
  381. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  382. return size;
  383. size++;
  384. ffurl_seek(h, pos, SEEK_SET);
  385. }
  386. return size;
  387. }
  388. int ffurl_get_file_handle(URLContext *h)
  389. {
  390. if (!h->prot->url_get_file_handle)
  391. return -1;
  392. return h->prot->url_get_file_handle(h);
  393. }
  394. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  395. {
  396. if (!h->prot->url_get_multi_file_handle) {
  397. if (!h->prot->url_get_file_handle)
  398. return AVERROR(ENOSYS);
  399. *handles = av_malloc(sizeof(**handles));
  400. if (!*handles)
  401. return AVERROR(ENOMEM);
  402. *numhandles = 1;
  403. *handles[0] = h->prot->url_get_file_handle(h);
  404. return 0;
  405. }
  406. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  407. }
  408. int ffurl_shutdown(URLContext *h, int flags)
  409. {
  410. if (!h->prot->url_shutdown)
  411. return AVERROR(EINVAL);
  412. return h->prot->url_shutdown(h, flags);
  413. }
  414. int ff_check_interrupt(AVIOInterruptCB *cb)
  415. {
  416. int ret;
  417. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  418. return ret;
  419. return 0;
  420. }