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.

516 lines
14KB

  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 <unistd.h>
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/dict.h"
  24. #include "libavutil/opt.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) return h->prot->name;
  42. else return "NULL";
  43. }
  44. static void *urlcontext_child_next(void *obj, void *prev)
  45. {
  46. URLContext *h = obj;
  47. if (!prev && h->priv_data && h->prot->priv_data_class)
  48. return h->priv_data;
  49. return NULL;
  50. }
  51. static const AVClass *urlcontext_child_class_next(const AVClass *prev)
  52. {
  53. URLProtocol *p = NULL;
  54. /* find the protocol that corresponds to prev */
  55. while (prev && (p = ffurl_protocol_next(p)))
  56. if (p->priv_data_class == prev)
  57. break;
  58. /* find next protocol with priv options */
  59. while (p = ffurl_protocol_next(p))
  60. if (p->priv_data_class)
  61. return p->priv_data_class;
  62. return NULL;
  63. }
  64. static const AVOption options[] = {{NULL}};
  65. const AVClass ffurl_context_class = {
  66. .class_name = "URLContext",
  67. .item_name = urlcontext_to_name,
  68. .option = options,
  69. .version = LIBAVUTIL_VERSION_INT,
  70. .child_next = urlcontext_child_next,
  71. .child_class_next = urlcontext_child_class_next,
  72. };
  73. /*@}*/
  74. #if FF_API_OLD_INTERRUPT_CB
  75. static int default_interrupt_cb(void);
  76. int (*url_interrupt_cb)(void) = default_interrupt_cb;
  77. #endif
  78. URLProtocol *av_protocol_next(URLProtocol *p)
  79. {
  80. return ffurl_protocol_next(p);
  81. }
  82. const char *avio_enum_protocols(void **opaque, int output)
  83. {
  84. URLProtocol **p = opaque;
  85. *p = ffurl_protocol_next(*p);
  86. if (!*p) return NULL;
  87. if ((output && (*p)->url_write) || (!output && (*p)->url_read))
  88. return (*p)->name;
  89. return avio_enum_protocols(opaque, output);
  90. }
  91. int ffurl_register_protocol(URLProtocol *protocol, int size)
  92. {
  93. URLProtocol **p;
  94. if (size < sizeof(URLProtocol)) {
  95. URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
  96. memcpy(temp, protocol, size);
  97. protocol = temp;
  98. }
  99. p = &first_protocol;
  100. while (*p != NULL) p = &(*p)->next;
  101. *p = protocol;
  102. protocol->next = NULL;
  103. return 0;
  104. }
  105. static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
  106. const char *filename, int flags,
  107. const AVIOInterruptCB *int_cb)
  108. {
  109. URLContext *uc;
  110. int err;
  111. #if CONFIG_NETWORK
  112. if (!ff_network_init())
  113. return AVERROR(EIO);
  114. #endif
  115. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  116. if (!uc) {
  117. err = AVERROR(ENOMEM);
  118. goto fail;
  119. }
  120. uc->av_class = &ffurl_context_class;
  121. uc->filename = (char *) &uc[1];
  122. strcpy(uc->filename, filename);
  123. uc->prot = up;
  124. uc->flags = flags;
  125. uc->is_streamed = 0; /* default = not streamed */
  126. uc->max_packet_size = 0; /* default: stream file */
  127. if (up->priv_data_size) {
  128. uc->priv_data = av_mallocz(up->priv_data_size);
  129. if (up->priv_data_class) {
  130. char *start = strchr(uc->filename, ',');
  131. *(const AVClass**)uc->priv_data = up->priv_data_class;
  132. av_opt_set_defaults(uc->priv_data);
  133. if(start){
  134. int ret= 0;
  135. char *p= start;
  136. char sep= *++p;
  137. char *key, *val;
  138. p++;
  139. while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
  140. *val= *key= 0;
  141. ret= av_opt_set(uc->priv_data, p, key+1, 0);
  142. if (ret == AVERROR_OPTION_NOT_FOUND)
  143. av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
  144. *val= *key= sep;
  145. p= val+1;
  146. }
  147. if(ret<0 || p!=key){
  148. av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
  149. av_freep(&uc->priv_data);
  150. av_freep(&uc);
  151. goto fail;
  152. }
  153. memmove(start, key+1, strlen(key));
  154. }
  155. }
  156. }
  157. if (int_cb)
  158. uc->interrupt_callback = *int_cb;
  159. *puc = uc;
  160. return 0;
  161. fail:
  162. *puc = NULL;
  163. #if CONFIG_NETWORK
  164. ff_network_close();
  165. #endif
  166. return err;
  167. }
  168. int ffurl_connect(URLContext* uc, AVDictionary **options)
  169. {
  170. int err =
  171. #if !FF_API_OLD_AVIO
  172. uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
  173. #endif
  174. uc->prot->url_open(uc, uc->filename, uc->flags);
  175. if (err)
  176. return err;
  177. uc->is_connected = 1;
  178. //We must be careful here as ffurl_seek() could be slow, for example for http
  179. if( (uc->flags & AVIO_FLAG_WRITE)
  180. || !strcmp(uc->prot->name, "file"))
  181. if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  182. uc->is_streamed= 1;
  183. return 0;
  184. }
  185. #if FF_API_OLD_AVIO
  186. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  187. const char *filename, int flags)
  188. {
  189. int ret;
  190. ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
  191. if (ret)
  192. goto fail;
  193. ret = ffurl_connect(*puc, NULL);
  194. if (!ret)
  195. return 0;
  196. fail:
  197. ffurl_close(*puc);
  198. *puc = NULL;
  199. return ret;
  200. }
  201. int url_alloc(URLContext **puc, const char *filename, int flags)
  202. {
  203. return ffurl_alloc(puc, filename, flags, NULL);
  204. }
  205. int url_connect(URLContext* uc)
  206. {
  207. return ffurl_connect(uc, NULL);
  208. }
  209. int url_open(URLContext **puc, const char *filename, int flags)
  210. {
  211. return ffurl_open(puc, filename, flags, NULL, NULL);
  212. }
  213. int url_read(URLContext *h, unsigned char *buf, int size)
  214. {
  215. return ffurl_read(h, buf, size);
  216. }
  217. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  218. {
  219. return ffurl_read_complete(h, buf, size);
  220. }
  221. int url_write(URLContext *h, const unsigned char *buf, int size)
  222. {
  223. return ffurl_write(h, buf, size);
  224. }
  225. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  226. {
  227. return ffurl_seek(h, pos, whence);
  228. }
  229. int url_close(URLContext *h)
  230. {
  231. return ffurl_close(h);
  232. }
  233. int64_t url_filesize(URLContext *h)
  234. {
  235. return ffurl_size(h);
  236. }
  237. int url_get_file_handle(URLContext *h)
  238. {
  239. return ffurl_get_file_handle(h);
  240. }
  241. int url_get_max_packet_size(URLContext *h)
  242. {
  243. return h->max_packet_size;
  244. }
  245. void url_get_filename(URLContext *h, char *buf, int buf_size)
  246. {
  247. av_strlcpy(buf, h->filename, buf_size);
  248. }
  249. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  250. {
  251. avio_set_interrupt_cb(interrupt_cb);
  252. }
  253. int av_register_protocol2(URLProtocol *protocol, int size)
  254. {
  255. return ffurl_register_protocol(protocol, size);
  256. }
  257. #endif
  258. #define URL_SCHEME_CHARS \
  259. "abcdefghijklmnopqrstuvwxyz" \
  260. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  261. "0123456789+-."
  262. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  263. const AVIOInterruptCB *int_cb)
  264. {
  265. URLProtocol *up = NULL;
  266. char proto_str[128], proto_nested[128], *ptr;
  267. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  268. if (!first_protocol) {
  269. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  270. "Missing call to av_register_all()?\n");
  271. }
  272. if (filename[proto_len] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
  273. strcpy(proto_str, "file");
  274. else
  275. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  276. if ((ptr = strchr(proto_str, ',')))
  277. *ptr = '\0';
  278. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  279. if ((ptr = strchr(proto_nested, '+')))
  280. *ptr = '\0';
  281. while (up = ffurl_protocol_next(up)) {
  282. if (!strcmp(proto_str, up->name))
  283. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  284. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  285. !strcmp(proto_nested, up->name))
  286. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  287. }
  288. *puc = NULL;
  289. return AVERROR(ENOENT);
  290. }
  291. int ffurl_open(URLContext **puc, const char *filename, int flags,
  292. const AVIOInterruptCB *int_cb, AVDictionary **options)
  293. {
  294. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  295. if (ret)
  296. return ret;
  297. if (options && (*puc)->prot->priv_data_class &&
  298. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  299. goto fail;
  300. ret = ffurl_connect(*puc, options);
  301. if (!ret)
  302. return 0;
  303. fail:
  304. ffurl_close(*puc);
  305. *puc = NULL;
  306. return ret;
  307. }
  308. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  309. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  310. {
  311. int ret, len;
  312. int fast_retries = 5;
  313. len = 0;
  314. while (len < size_min) {
  315. ret = transfer_func(h, buf+len, size-len);
  316. if (ret == AVERROR(EINTR))
  317. continue;
  318. if (h->flags & AVIO_FLAG_NONBLOCK)
  319. return ret;
  320. if (ret == AVERROR(EAGAIN)) {
  321. ret = 0;
  322. if (fast_retries)
  323. fast_retries--;
  324. else
  325. usleep(1000);
  326. } else if (ret < 1)
  327. return ret < 0 ? ret : len;
  328. if (ret)
  329. fast_retries = FFMAX(fast_retries, 2);
  330. len += ret;
  331. if (len < size && ff_check_interrupt(&h->interrupt_callback))
  332. return AVERROR_EXIT;
  333. }
  334. return len;
  335. }
  336. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  337. {
  338. if (!(h->flags & AVIO_FLAG_READ))
  339. return AVERROR(EIO);
  340. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  341. }
  342. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  343. {
  344. if (!(h->flags & AVIO_FLAG_READ))
  345. return AVERROR(EIO);
  346. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  347. }
  348. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  349. {
  350. if (!(h->flags & AVIO_FLAG_WRITE))
  351. return AVERROR(EIO);
  352. /* avoid sending too big packets */
  353. if (h->max_packet_size && size > h->max_packet_size)
  354. return AVERROR(EIO);
  355. return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
  356. }
  357. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  358. {
  359. int64_t ret;
  360. if (!h->prot->url_seek)
  361. return AVERROR(ENOSYS);
  362. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  363. return ret;
  364. }
  365. int ffurl_close(URLContext *h)
  366. {
  367. int ret = 0;
  368. if (!h) return 0; /* can happen when ffurl_open fails */
  369. if (h->is_connected && h->prot->url_close)
  370. ret = h->prot->url_close(h);
  371. #if CONFIG_NETWORK
  372. ff_network_close();
  373. #endif
  374. if (h->prot->priv_data_size) {
  375. if (h->prot->priv_data_class)
  376. av_opt_free(h->priv_data);
  377. av_free(h->priv_data);
  378. }
  379. av_free(h);
  380. return ret;
  381. }
  382. #if FF_API_OLD_AVIO
  383. int url_exist(const char *filename)
  384. {
  385. URLContext *h;
  386. if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
  387. return 0;
  388. ffurl_close(h);
  389. return 1;
  390. }
  391. #endif
  392. int avio_check(const char *url, int flags)
  393. {
  394. URLContext *h;
  395. int ret = ffurl_alloc(&h, url, flags, NULL);
  396. if (ret)
  397. return ret;
  398. if (h->prot->url_check) {
  399. ret = h->prot->url_check(h, flags);
  400. } else {
  401. ret = ffurl_connect(h, NULL);
  402. if (ret >= 0)
  403. ret = flags;
  404. }
  405. ffurl_close(h);
  406. return ret;
  407. }
  408. int64_t ffurl_size(URLContext *h)
  409. {
  410. int64_t pos, size;
  411. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  412. if(size<0){
  413. pos = ffurl_seek(h, 0, SEEK_CUR);
  414. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  415. return size;
  416. size++;
  417. ffurl_seek(h, pos, SEEK_SET);
  418. }
  419. return size;
  420. }
  421. int ffurl_get_file_handle(URLContext *h)
  422. {
  423. if (!h->prot->url_get_file_handle)
  424. return -1;
  425. return h->prot->url_get_file_handle(h);
  426. }
  427. #if FF_API_OLD_INTERRUPT_CB
  428. static int default_interrupt_cb(void)
  429. {
  430. return 0;
  431. }
  432. void avio_set_interrupt_cb(int (*interrupt_cb)(void))
  433. {
  434. if (!interrupt_cb)
  435. interrupt_cb = default_interrupt_cb;
  436. url_interrupt_cb = interrupt_cb;
  437. }
  438. #endif
  439. int ff_check_interrupt(AVIOInterruptCB *cb)
  440. {
  441. int ret;
  442. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  443. return ret;
  444. #if FF_API_OLD_INTERRUPT_CB
  445. return url_interrupt_cb();
  446. #else
  447. return 0;
  448. #endif
  449. }
  450. #if FF_API_OLD_AVIO
  451. int av_url_read_pause(URLContext *h, int pause)
  452. {
  453. if (!h->prot->url_read_pause)
  454. return AVERROR(ENOSYS);
  455. return h->prot->url_read_pause(h, pause);
  456. }
  457. int64_t av_url_read_seek(URLContext *h,
  458. int stream_index, int64_t timestamp, int flags)
  459. {
  460. if (!h->prot->url_read_seek)
  461. return AVERROR(ENOSYS);
  462. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  463. }
  464. #endif