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.

365 lines
9.4KB

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