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.

415 lines
11KB

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