avio.c 11 KB

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