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.

379 lines
10KB

  1. /*
  2. * unbuffered I/O
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. const char *avio_enum_protocols(void **opaque, int output)
  79. {
  80. URLProtocol **p = opaque;
  81. *p = ffurl_protocol_next(*p);
  82. if (!*p) return NULL;
  83. if ((output && (*p)->url_write) || (!output && (*p)->url_read))
  84. return (*p)->name;
  85. return avio_enum_protocols(opaque, output);
  86. }
  87. int ffurl_register_protocol(URLProtocol *protocol, int size)
  88. {
  89. URLProtocol **p;
  90. if (size < sizeof(URLProtocol)) {
  91. URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
  92. memcpy(temp, protocol, size);
  93. protocol = temp;
  94. }
  95. p = &first_protocol;
  96. while (*p != NULL) 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. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  112. if (!uc) {
  113. err = AVERROR(ENOMEM);
  114. goto fail;
  115. }
  116. uc->av_class = &ffurl_context_class;
  117. uc->filename = (char *) &uc[1];
  118. strcpy(uc->filename, filename);
  119. uc->prot = up;
  120. uc->flags = flags;
  121. uc->is_streamed = 0; /* default = not streamed */
  122. uc->max_packet_size = 0; /* default: stream file */
  123. if (up->priv_data_size) {
  124. uc->priv_data = av_mallocz(up->priv_data_size);
  125. if (up->priv_data_class) {
  126. *(const AVClass**)uc->priv_data = up->priv_data_class;
  127. av_opt_set_defaults(uc->priv_data);
  128. }
  129. }
  130. if (int_cb)
  131. uc->interrupt_callback = *int_cb;
  132. *puc = uc;
  133. return 0;
  134. fail:
  135. *puc = NULL;
  136. #if CONFIG_NETWORK
  137. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  138. ff_network_close();
  139. #endif
  140. return err;
  141. }
  142. int ffurl_connect(URLContext* uc, AVDictionary **options)
  143. {
  144. int err =
  145. uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
  146. uc->prot->url_open(uc, uc->filename, uc->flags);
  147. if (err)
  148. return err;
  149. uc->is_connected = 1;
  150. //We must be careful here as ffurl_seek() could be slow, for example for http
  151. if( (uc->flags & AVIO_FLAG_WRITE)
  152. || !strcmp(uc->prot->name, "file"))
  153. if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  154. uc->is_streamed= 1;
  155. return 0;
  156. }
  157. #define URL_SCHEME_CHARS \
  158. "abcdefghijklmnopqrstuvwxyz" \
  159. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  160. "0123456789+-."
  161. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  162. const AVIOInterruptCB *int_cb)
  163. {
  164. URLProtocol *up = NULL;
  165. char proto_str[128], proto_nested[128], *ptr;
  166. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  167. if (filename[proto_len] != ':' || is_dos_path(filename))
  168. strcpy(proto_str, "file");
  169. else
  170. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  171. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  172. if ((ptr = strchr(proto_nested, '+')))
  173. *ptr = '\0';
  174. while (up = ffurl_protocol_next(up)) {
  175. if (!strcmp(proto_str, up->name))
  176. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  177. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  178. !strcmp(proto_nested, up->name))
  179. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  180. }
  181. *puc = NULL;
  182. return AVERROR(ENOENT);
  183. }
  184. int ffurl_open(URLContext **puc, const char *filename, int flags,
  185. const AVIOInterruptCB *int_cb, AVDictionary **options)
  186. {
  187. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  188. if (ret)
  189. return ret;
  190. if (options && (*puc)->prot->priv_data_class &&
  191. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  192. goto fail;
  193. ret = ffurl_connect(*puc, options);
  194. if (!ret)
  195. return 0;
  196. fail:
  197. ffurl_close(*puc);
  198. *puc = NULL;
  199. return ret;
  200. }
  201. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  202. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  203. {
  204. int ret, len;
  205. int fast_retries = 5;
  206. len = 0;
  207. while (len < size_min) {
  208. ret = transfer_func(h, buf+len, size-len);
  209. if (ret == AVERROR(EINTR))
  210. continue;
  211. if (h->flags & AVIO_FLAG_NONBLOCK)
  212. return ret;
  213. if (ret == AVERROR(EAGAIN)) {
  214. ret = 0;
  215. if (fast_retries)
  216. fast_retries--;
  217. else
  218. usleep(1000);
  219. } else if (ret < 1)
  220. return ret < 0 ? ret : len;
  221. if (ret)
  222. fast_retries = FFMAX(fast_retries, 2);
  223. len += ret;
  224. if (ff_check_interrupt(&h->interrupt_callback))
  225. return AVERROR_EXIT;
  226. }
  227. return len;
  228. }
  229. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  230. {
  231. if (!(h->flags & AVIO_FLAG_READ))
  232. return AVERROR(EIO);
  233. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  234. }
  235. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  236. {
  237. if (!(h->flags & AVIO_FLAG_READ))
  238. return AVERROR(EIO);
  239. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  240. }
  241. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  242. {
  243. if (!(h->flags & AVIO_FLAG_WRITE))
  244. return AVERROR(EIO);
  245. /* avoid sending too big packets */
  246. if (h->max_packet_size && size > h->max_packet_size)
  247. return AVERROR(EIO);
  248. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
  249. }
  250. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  251. {
  252. int64_t ret;
  253. if (!h->prot->url_seek)
  254. return AVERROR(ENOSYS);
  255. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  256. return ret;
  257. }
  258. int ffurl_close(URLContext *h)
  259. {
  260. int ret = 0;
  261. if (!h) return 0; /* can happen when ffurl_open fails */
  262. if (h->is_connected && h->prot->url_close)
  263. ret = h->prot->url_close(h);
  264. #if CONFIG_NETWORK
  265. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  266. ff_network_close();
  267. #endif
  268. if (h->prot->priv_data_size) {
  269. if (h->prot->priv_data_class)
  270. av_opt_free(h->priv_data);
  271. av_free(h->priv_data);
  272. }
  273. av_free(h);
  274. return ret;
  275. }
  276. int avio_check(const char *url, int flags)
  277. {
  278. URLContext *h;
  279. int ret = ffurl_alloc(&h, url, flags, NULL);
  280. if (ret)
  281. return ret;
  282. if (h->prot->url_check) {
  283. ret = h->prot->url_check(h, flags);
  284. } else {
  285. ret = ffurl_connect(h, NULL);
  286. if (ret >= 0)
  287. ret = flags;
  288. }
  289. ffurl_close(h);
  290. return ret;
  291. }
  292. int64_t ffurl_size(URLContext *h)
  293. {
  294. int64_t pos, size;
  295. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  296. if(size<0){
  297. pos = ffurl_seek(h, 0, SEEK_CUR);
  298. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  299. return size;
  300. size++;
  301. ffurl_seek(h, pos, SEEK_SET);
  302. }
  303. return size;
  304. }
  305. int ffurl_get_file_handle(URLContext *h)
  306. {
  307. if (!h->prot->url_get_file_handle)
  308. return -1;
  309. return h->prot->url_get_file_handle(h);
  310. }
  311. #if FF_API_OLD_INTERRUPT_CB
  312. static int default_interrupt_cb(void)
  313. {
  314. return 0;
  315. }
  316. void avio_set_interrupt_cb(int (*interrupt_cb)(void))
  317. {
  318. if (!interrupt_cb)
  319. interrupt_cb = default_interrupt_cb;
  320. url_interrupt_cb = interrupt_cb;
  321. }
  322. #endif
  323. int ff_check_interrupt(AVIOInterruptCB *cb)
  324. {
  325. int ret;
  326. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  327. return ret;
  328. #if FF_API_OLD_INTERRUPT_CB
  329. return url_interrupt_cb();
  330. #else
  331. return 0;
  332. #endif
  333. }