avio.c 11 KB

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