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.

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