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.

376 lines
9.7KB

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