avio.c 9.6 KB

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