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.

491 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 <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. *(const AVClass**)uc->priv_data = up->priv_data_class;
  131. av_opt_set_defaults(uc->priv_data);
  132. }
  133. }
  134. if (int_cb)
  135. uc->interrupt_callback = *int_cb;
  136. *puc = uc;
  137. return 0;
  138. fail:
  139. *puc = NULL;
  140. #if CONFIG_NETWORK
  141. ff_network_close();
  142. #endif
  143. return err;
  144. }
  145. int ffurl_connect(URLContext* uc, AVDictionary **options)
  146. {
  147. int err =
  148. #if !FF_API_OLD_AVIO
  149. uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
  150. #endif
  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, for example for http
  156. if( (uc->flags & AVIO_FLAG_WRITE)
  157. || !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. #if FF_API_OLD_AVIO
  163. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  164. const char *filename, int flags)
  165. {
  166. int ret;
  167. ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
  168. if (ret)
  169. goto fail;
  170. ret = ffurl_connect(*puc, NULL);
  171. if (!ret)
  172. return 0;
  173. fail:
  174. ffurl_close(*puc);
  175. *puc = NULL;
  176. return ret;
  177. }
  178. int url_alloc(URLContext **puc, const char *filename, int flags)
  179. {
  180. return ffurl_alloc(puc, filename, flags, NULL);
  181. }
  182. int url_connect(URLContext* uc)
  183. {
  184. return ffurl_connect(uc, NULL);
  185. }
  186. int url_open(URLContext **puc, const char *filename, int flags)
  187. {
  188. return ffurl_open(puc, filename, flags, NULL, NULL);
  189. }
  190. int url_read(URLContext *h, unsigned char *buf, int size)
  191. {
  192. return ffurl_read(h, buf, size);
  193. }
  194. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  195. {
  196. return ffurl_read_complete(h, buf, size);
  197. }
  198. int url_write(URLContext *h, const unsigned char *buf, int size)
  199. {
  200. return ffurl_write(h, buf, size);
  201. }
  202. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  203. {
  204. return ffurl_seek(h, pos, whence);
  205. }
  206. int url_close(URLContext *h)
  207. {
  208. return ffurl_close(h);
  209. }
  210. int64_t url_filesize(URLContext *h)
  211. {
  212. return ffurl_size(h);
  213. }
  214. int url_get_file_handle(URLContext *h)
  215. {
  216. return ffurl_get_file_handle(h);
  217. }
  218. int url_get_max_packet_size(URLContext *h)
  219. {
  220. return h->max_packet_size;
  221. }
  222. void url_get_filename(URLContext *h, char *buf, int buf_size)
  223. {
  224. av_strlcpy(buf, h->filename, buf_size);
  225. }
  226. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  227. {
  228. avio_set_interrupt_cb(interrupt_cb);
  229. }
  230. int av_register_protocol2(URLProtocol *protocol, int size)
  231. {
  232. return ffurl_register_protocol(protocol, size);
  233. }
  234. #endif
  235. #define URL_SCHEME_CHARS \
  236. "abcdefghijklmnopqrstuvwxyz" \
  237. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  238. "0123456789+-."
  239. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  240. const AVIOInterruptCB *int_cb)
  241. {
  242. URLProtocol *up = NULL;
  243. char proto_str[128], proto_nested[128], *ptr;
  244. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  245. if (!first_protocol) {
  246. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  247. "Missing call to av_register_all()?\n");
  248. }
  249. if (filename[proto_len] != ':' || is_dos_path(filename))
  250. strcpy(proto_str, "file");
  251. else
  252. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  253. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  254. if ((ptr = strchr(proto_nested, '+')))
  255. *ptr = '\0';
  256. while (up = ffurl_protocol_next(up)) {
  257. if (!strcmp(proto_str, up->name))
  258. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  259. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  260. !strcmp(proto_nested, up->name))
  261. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  262. }
  263. *puc = NULL;
  264. return AVERROR(ENOENT);
  265. }
  266. int ffurl_open(URLContext **puc, const char *filename, int flags,
  267. const AVIOInterruptCB *int_cb, AVDictionary **options)
  268. {
  269. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  270. if (ret)
  271. return ret;
  272. if (options && (*puc)->prot->priv_data_class &&
  273. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  274. goto fail;
  275. ret = ffurl_connect(*puc, options);
  276. if (!ret)
  277. return 0;
  278. fail:
  279. ffurl_close(*puc);
  280. *puc = NULL;
  281. return ret;
  282. }
  283. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  284. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  285. {
  286. int ret, len;
  287. int fast_retries = 5;
  288. len = 0;
  289. while (len < size_min) {
  290. ret = transfer_func(h, buf+len, size-len);
  291. if (ret == AVERROR(EINTR))
  292. continue;
  293. if (h->flags & AVIO_FLAG_NONBLOCK)
  294. return ret;
  295. if (ret == AVERROR(EAGAIN)) {
  296. ret = 0;
  297. if (fast_retries)
  298. fast_retries--;
  299. else
  300. usleep(1000);
  301. } else if (ret < 1)
  302. return ret < 0 ? ret : len;
  303. if (ret)
  304. fast_retries = FFMAX(fast_retries, 2);
  305. len += ret;
  306. if (len < size && ff_check_interrupt(&h->interrupt_callback))
  307. return AVERROR_EXIT;
  308. }
  309. return len;
  310. }
  311. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  312. {
  313. if (!(h->flags & AVIO_FLAG_READ))
  314. return AVERROR(EIO);
  315. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  316. }
  317. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  318. {
  319. if (!(h->flags & AVIO_FLAG_READ))
  320. return AVERROR(EIO);
  321. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  322. }
  323. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  324. {
  325. if (!(h->flags & AVIO_FLAG_WRITE))
  326. return AVERROR(EIO);
  327. /* avoid sending too big packets */
  328. if (h->max_packet_size && size > h->max_packet_size)
  329. return AVERROR(EIO);
  330. return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
  331. }
  332. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  333. {
  334. int64_t ret;
  335. if (!h->prot->url_seek)
  336. return AVERROR(ENOSYS);
  337. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  338. return ret;
  339. }
  340. int ffurl_close(URLContext *h)
  341. {
  342. int ret = 0;
  343. if (!h) return 0; /* can happen when ffurl_open fails */
  344. if (h->is_connected && h->prot->url_close)
  345. ret = h->prot->url_close(h);
  346. #if CONFIG_NETWORK
  347. ff_network_close();
  348. #endif
  349. if (h->prot->priv_data_size) {
  350. if (h->prot->priv_data_class)
  351. av_opt_free(h->priv_data);
  352. av_free(h->priv_data);
  353. }
  354. av_free(h);
  355. return ret;
  356. }
  357. #if FF_API_OLD_AVIO
  358. int url_exist(const char *filename)
  359. {
  360. URLContext *h;
  361. if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
  362. return 0;
  363. ffurl_close(h);
  364. return 1;
  365. }
  366. #endif
  367. int avio_check(const char *url, int flags)
  368. {
  369. URLContext *h;
  370. int ret = ffurl_alloc(&h, url, flags, NULL);
  371. if (ret)
  372. return ret;
  373. if (h->prot->url_check) {
  374. ret = h->prot->url_check(h, flags);
  375. } else {
  376. ret = ffurl_connect(h, NULL);
  377. if (ret >= 0)
  378. ret = flags;
  379. }
  380. ffurl_close(h);
  381. return ret;
  382. }
  383. int64_t ffurl_size(URLContext *h)
  384. {
  385. int64_t pos, size;
  386. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  387. if(size<0){
  388. pos = ffurl_seek(h, 0, SEEK_CUR);
  389. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  390. return size;
  391. size++;
  392. ffurl_seek(h, pos, SEEK_SET);
  393. }
  394. return size;
  395. }
  396. int ffurl_get_file_handle(URLContext *h)
  397. {
  398. if (!h->prot->url_get_file_handle)
  399. return -1;
  400. return h->prot->url_get_file_handle(h);
  401. }
  402. #if FF_API_OLD_INTERRUPT_CB
  403. static int default_interrupt_cb(void)
  404. {
  405. return 0;
  406. }
  407. void avio_set_interrupt_cb(int (*interrupt_cb)(void))
  408. {
  409. if (!interrupt_cb)
  410. interrupt_cb = default_interrupt_cb;
  411. url_interrupt_cb = interrupt_cb;
  412. }
  413. #endif
  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. #if FF_API_OLD_INTERRUPT_CB
  420. return url_interrupt_cb();
  421. #else
  422. return 0;
  423. #endif
  424. }
  425. #if FF_API_OLD_AVIO
  426. int av_url_read_pause(URLContext *h, int pause)
  427. {
  428. if (!h->prot->url_read_pause)
  429. return AVERROR(ENOSYS);
  430. return h->prot->url_read_pause(h, pause);
  431. }
  432. int64_t av_url_read_seek(URLContext *h,
  433. int stream_index, int64_t timestamp, int flags)
  434. {
  435. if (!h->prot->url_read_seek)
  436. return AVERROR(ENOSYS);
  437. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  438. }
  439. #endif