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.

483 lines
12KB

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