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.

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