avio.c 11 KB

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