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.

388 lines
10KB

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