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.

367 lines
9.5KB

  1. /*
  2. * Unbuffered io for ffmpeg system
  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. /* 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. #if FF_API_OLD_AVIO
  140. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  141. const char *filename, int flags)
  142. {
  143. int ret;
  144. ret = url_alloc_for_protocol(puc, up, filename, flags);
  145. if (ret)
  146. goto fail;
  147. ret = url_connect(*puc);
  148. if (!ret)
  149. return 0;
  150. fail:
  151. url_close(*puc);
  152. *puc = NULL;
  153. return ret;
  154. }
  155. #endif
  156. #define URL_SCHEME_CHARS \
  157. "abcdefghijklmnopqrstuvwxyz" \
  158. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  159. "0123456789+-."
  160. int url_alloc(URLContext **puc, const char *filename, int flags)
  161. {
  162. URLProtocol *up;
  163. char proto_str[128], proto_nested[128], *ptr;
  164. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  165. if (filename[proto_len] != ':' || is_dos_path(filename))
  166. strcpy(proto_str, "file");
  167. else
  168. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  169. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  170. if ((ptr = strchr(proto_nested, '+')))
  171. *ptr = '\0';
  172. up = first_protocol;
  173. while (up != NULL) {
  174. if (!strcmp(proto_str, up->name))
  175. return url_alloc_for_protocol (puc, up, filename, flags);
  176. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  177. !strcmp(proto_nested, up->name))
  178. return url_alloc_for_protocol (puc, up, filename, flags);
  179. up = up->next;
  180. }
  181. *puc = NULL;
  182. return AVERROR(ENOENT);
  183. }
  184. int url_open(URLContext **puc, const char *filename, int flags)
  185. {
  186. int ret = url_alloc(puc, filename, flags);
  187. if (ret)
  188. return ret;
  189. ret = url_connect(*puc);
  190. if (!ret)
  191. return 0;
  192. url_close(*puc);
  193. *puc = NULL;
  194. return ret;
  195. }
  196. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  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_min) {
  203. ret = transfer_func(h, buf+len, size-len);
  204. if (ret == AVERROR(EINTR))
  205. continue;
  206. if (h->flags & URL_FLAG_NONBLOCK)
  207. return ret;
  208. if (ret == AVERROR(EAGAIN)) {
  209. ret = 0;
  210. if (fast_retries)
  211. fast_retries--;
  212. else
  213. usleep(1000);
  214. } else if (ret < 1)
  215. return ret < 0 ? ret : len;
  216. if (ret)
  217. fast_retries = FFMAX(fast_retries, 2);
  218. len += ret;
  219. if (url_interrupt_cb())
  220. return AVERROR_EXIT;
  221. }
  222. return len;
  223. }
  224. int url_read(URLContext *h, unsigned char *buf, int size)
  225. {
  226. if (h->flags & URL_WRONLY)
  227. return AVERROR(EIO);
  228. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  229. }
  230. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  231. {
  232. if (h->flags & URL_WRONLY)
  233. return AVERROR(EIO);
  234. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  235. }
  236. int url_write(URLContext *h, const unsigned char *buf, int size)
  237. {
  238. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  239. return AVERROR(EIO);
  240. /* avoid sending too big packets */
  241. if (h->max_packet_size && size > h->max_packet_size)
  242. return AVERROR(EIO);
  243. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
  244. }
  245. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  246. {
  247. int64_t ret;
  248. if (!h->prot->url_seek)
  249. return AVERROR(ENOSYS);
  250. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  251. return ret;
  252. }
  253. int url_close(URLContext *h)
  254. {
  255. int ret = 0;
  256. if (!h) return 0; /* can happen when url_open fails */
  257. if (h->is_connected && h->prot->url_close)
  258. ret = h->prot->url_close(h);
  259. #if CONFIG_NETWORK
  260. ff_network_close();
  261. #endif
  262. if (h->prot->priv_data_size)
  263. av_free(h->priv_data);
  264. av_free(h);
  265. return ret;
  266. }
  267. int url_exist(const char *filename)
  268. {
  269. URLContext *h;
  270. if (url_open(&h, filename, URL_RDONLY) < 0)
  271. return 0;
  272. url_close(h);
  273. return 1;
  274. }
  275. int64_t url_filesize(URLContext *h)
  276. {
  277. int64_t pos, size;
  278. size= url_seek(h, 0, AVSEEK_SIZE);
  279. if(size<0){
  280. pos = url_seek(h, 0, SEEK_CUR);
  281. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  282. return size;
  283. size++;
  284. url_seek(h, pos, SEEK_SET);
  285. }
  286. return size;
  287. }
  288. int url_get_file_handle(URLContext *h)
  289. {
  290. if (!h->prot->url_get_file_handle)
  291. return -1;
  292. return h->prot->url_get_file_handle(h);
  293. }
  294. int url_get_max_packet_size(URLContext *h)
  295. {
  296. return h->max_packet_size;
  297. }
  298. void url_get_filename(URLContext *h, char *buf, int buf_size)
  299. {
  300. av_strlcpy(buf, h->filename, buf_size);
  301. }
  302. static int default_interrupt_cb(void)
  303. {
  304. return 0;
  305. }
  306. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  307. {
  308. if (!interrupt_cb)
  309. interrupt_cb = default_interrupt_cb;
  310. url_interrupt_cb = interrupt_cb;
  311. }
  312. int av_url_read_pause(URLContext *h, int pause)
  313. {
  314. if (!h->prot->url_read_pause)
  315. return AVERROR(ENOSYS);
  316. return h->prot->url_read_pause(h, pause);
  317. }
  318. int64_t av_url_read_seek(URLContext *h,
  319. int stream_index, int64_t timestamp, int flags)
  320. {
  321. if (!h->prot->url_read_seek)
  322. return AVERROR(ENOSYS);
  323. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  324. }