avio.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Unbuffered io for ffmpeg system
  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. /* needed for usleep() */
  22. #define _XOPEN_SOURCE 600
  23. #include <unistd.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/opt.h"
  26. #include "os_support.h"
  27. #include "avformat.h"
  28. #if CONFIG_NETWORK
  29. #include "network.h"
  30. #endif
  31. #if FF_API_URL_CLASS
  32. /** @name Logging context. */
  33. /*@{*/
  34. static const char *urlcontext_to_name(void *ptr)
  35. {
  36. URLContext *h = (URLContext *)ptr;
  37. if(h->prot) return h->prot->name;
  38. else return "NULL";
  39. }
  40. static const AVOption options[] = {{NULL}};
  41. static const AVClass urlcontext_class =
  42. { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
  43. /*@}*/
  44. #endif
  45. static int default_interrupt_cb(void);
  46. URLProtocol *first_protocol = NULL;
  47. URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
  48. URLProtocol *av_protocol_next(URLProtocol *p)
  49. {
  50. if(p) return p->next;
  51. else return first_protocol;
  52. }
  53. int av_register_protocol2(URLProtocol *protocol, int size)
  54. {
  55. URLProtocol **p;
  56. if (size < sizeof(URLProtocol)) {
  57. URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
  58. memcpy(temp, protocol, size);
  59. protocol = temp;
  60. }
  61. p = &first_protocol;
  62. while (*p != NULL) p = &(*p)->next;
  63. *p = protocol;
  64. protocol->next = NULL;
  65. return 0;
  66. }
  67. #if FF_API_REGISTER_PROTOCOL
  68. /* The layout of URLProtocol as of when major was bumped to 52 */
  69. struct URLProtocol_compat {
  70. const char *name;
  71. int (*url_open)(URLContext *h, const char *filename, int flags);
  72. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  73. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  74. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  75. int (*url_close)(URLContext *h);
  76. struct URLProtocol *next;
  77. };
  78. int av_register_protocol(URLProtocol *protocol)
  79. {
  80. return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
  81. }
  82. int register_protocol(URLProtocol *protocol)
  83. {
  84. return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
  85. }
  86. #endif
  87. static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
  88. const char *filename, int flags)
  89. {
  90. URLContext *uc;
  91. int err;
  92. #if CONFIG_NETWORK
  93. if (!ff_network_init())
  94. return AVERROR(EIO);
  95. #endif
  96. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  97. if (!uc) {
  98. err = AVERROR(ENOMEM);
  99. goto fail;
  100. }
  101. #if FF_API_URL_CLASS
  102. uc->av_class = &urlcontext_class;
  103. #endif
  104. uc->filename = (char *) &uc[1];
  105. strcpy(uc->filename, filename);
  106. uc->prot = up;
  107. uc->flags = flags;
  108. uc->is_streamed = 0; /* default = not streamed */
  109. uc->max_packet_size = 0; /* default: stream file */
  110. if (up->priv_data_size) {
  111. uc->priv_data = av_mallocz(up->priv_data_size);
  112. if (up->priv_data_class) {
  113. *(const AVClass**)uc->priv_data = up->priv_data_class;
  114. av_opt_set_defaults(uc->priv_data);
  115. }
  116. }
  117. *puc = uc;
  118. return 0;
  119. fail:
  120. *puc = NULL;
  121. #if CONFIG_NETWORK
  122. ff_network_close();
  123. #endif
  124. return err;
  125. }
  126. int url_connect(URLContext* uc)
  127. {
  128. int err = uc->prot->url_open(uc, uc->filename, uc->flags);
  129. if (err)
  130. return err;
  131. uc->is_connected = 1;
  132. //We must be careful here as url_seek() could be slow, for example for http
  133. if( (uc->flags & (URL_WRONLY | URL_RDWR))
  134. || !strcmp(uc->prot->name, "file"))
  135. if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
  136. uc->is_streamed= 1;
  137. return 0;
  138. }
  139. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  140. const char *filename, int flags)
  141. {
  142. int ret;
  143. ret = url_alloc_for_protocol(puc, up, filename, flags);
  144. if (ret)
  145. goto fail;
  146. ret = url_connect(*puc);
  147. if (!ret)
  148. return 0;
  149. fail:
  150. url_close(*puc);
  151. *puc = NULL;
  152. return ret;
  153. }
  154. #define URL_SCHEME_CHARS \
  155. "abcdefghijklmnopqrstuvwxyz" \
  156. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  157. "0123456789+-."
  158. int url_alloc(URLContext **puc, const char *filename, int flags)
  159. {
  160. URLProtocol *up;
  161. char proto_str[128];
  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. up = first_protocol;
  168. while (up != NULL) {
  169. if (!strcmp(proto_str, up->name))
  170. return url_alloc_for_protocol (puc, up, filename, flags);
  171. up = up->next;
  172. }
  173. *puc = NULL;
  174. return AVERROR(ENOENT);
  175. }
  176. int url_open(URLContext **puc, const char *filename, int flags)
  177. {
  178. int ret = url_alloc(puc, filename, flags);
  179. if (ret)
  180. return ret;
  181. ret = url_connect(*puc);
  182. if (!ret)
  183. return 0;
  184. url_close(*puc);
  185. *puc = NULL;
  186. return ret;
  187. }
  188. int url_read(URLContext *h, unsigned char *buf, int size)
  189. {
  190. int ret;
  191. if (h->flags & URL_WRONLY)
  192. return AVERROR(EIO);
  193. ret = h->prot->url_read(h, buf, size);
  194. return ret;
  195. }
  196. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size,
  197. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  198. {
  199. int ret, len;
  200. int fast_retries = 5;
  201. len = 0;
  202. while (len < size) {
  203. ret = transfer_func(h, buf+len, size-len);
  204. if (ret == AVERROR(EAGAIN)) {
  205. ret = 0;
  206. if (fast_retries)
  207. fast_retries--;
  208. else
  209. usleep(1000);
  210. } else if (ret < 1)
  211. return ret < 0 ? ret : len;
  212. if (ret)
  213. fast_retries = FFMAX(fast_retries, 2);
  214. len += ret;
  215. }
  216. return len;
  217. }
  218. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  219. {
  220. return retry_transfer_wrapper(h, buf, size, url_read);
  221. }
  222. int url_write(URLContext *h, const unsigned char *buf, int size)
  223. {
  224. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  225. return AVERROR(EIO);
  226. /* avoid sending too big packets */
  227. if (h->max_packet_size && size > h->max_packet_size)
  228. return AVERROR(EIO);
  229. return retry_transfer_wrapper(h, buf, size, h->prot->url_write);
  230. }
  231. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  232. {
  233. int64_t ret;
  234. if (!h->prot->url_seek)
  235. return AVERROR(ENOSYS);
  236. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  237. return ret;
  238. }
  239. int url_close(URLContext *h)
  240. {
  241. int ret = 0;
  242. if (!h) return 0; /* can happen when url_open fails */
  243. if (h->is_connected && h->prot->url_close)
  244. ret = h->prot->url_close(h);
  245. #if CONFIG_NETWORK
  246. ff_network_close();
  247. #endif
  248. if (h->prot->priv_data_size)
  249. av_free(h->priv_data);
  250. av_free(h);
  251. return ret;
  252. }
  253. int url_exist(const char *filename)
  254. {
  255. URLContext *h;
  256. if (url_open(&h, filename, URL_RDONLY) < 0)
  257. return 0;
  258. url_close(h);
  259. return 1;
  260. }
  261. int64_t url_filesize(URLContext *h)
  262. {
  263. int64_t pos, size;
  264. size= url_seek(h, 0, AVSEEK_SIZE);
  265. if(size<0){
  266. pos = url_seek(h, 0, SEEK_CUR);
  267. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  268. return size;
  269. size++;
  270. url_seek(h, pos, SEEK_SET);
  271. }
  272. return size;
  273. }
  274. int url_get_file_handle(URLContext *h)
  275. {
  276. if (!h->prot->url_get_file_handle)
  277. return -1;
  278. return h->prot->url_get_file_handle(h);
  279. }
  280. int url_get_max_packet_size(URLContext *h)
  281. {
  282. return h->max_packet_size;
  283. }
  284. void url_get_filename(URLContext *h, char *buf, int buf_size)
  285. {
  286. av_strlcpy(buf, h->filename, buf_size);
  287. }
  288. static int default_interrupt_cb(void)
  289. {
  290. return 0;
  291. }
  292. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  293. {
  294. if (!interrupt_cb)
  295. interrupt_cb = default_interrupt_cb;
  296. url_interrupt_cb = interrupt_cb;
  297. }
  298. int av_url_read_pause(URLContext *h, int pause)
  299. {
  300. if (!h->prot->url_read_pause)
  301. return AVERROR(ENOSYS);
  302. return h->prot->url_read_pause(h, pause);
  303. }
  304. int64_t av_url_read_seek(URLContext *h,
  305. int stream_index, int64_t timestamp, int flags)
  306. {
  307. if (!h->prot->url_read_seek)
  308. return AVERROR(ENOSYS);
  309. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  310. }