avio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * unbuffered I/O
  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 "libavutil/avstring.h"
  22. #include "libavutil/dict.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/time.h"
  25. #include "os_support.h"
  26. #include "avformat.h"
  27. #if CONFIG_NETWORK
  28. #include "network.h"
  29. #endif
  30. #include "url.h"
  31. static URLProtocol *first_protocol = NULL;
  32. URLProtocol *ffurl_protocol_next(URLProtocol *prev)
  33. {
  34. return prev ? prev->next : first_protocol;
  35. }
  36. /** @name Logging context. */
  37. /*@{*/
  38. static const char *urlcontext_to_name(void *ptr)
  39. {
  40. URLContext *h = (URLContext *)ptr;
  41. if(h->prot) return h->prot->name;
  42. else return "NULL";
  43. }
  44. static void *urlcontext_child_next(void *obj, void *prev)
  45. {
  46. URLContext *h = obj;
  47. if (!prev && h->priv_data && h->prot->priv_data_class)
  48. return h->priv_data;
  49. return NULL;
  50. }
  51. static const AVClass *urlcontext_child_class_next(const AVClass *prev)
  52. {
  53. URLProtocol *p = NULL;
  54. /* find the protocol that corresponds to prev */
  55. while (prev && (p = ffurl_protocol_next(p)))
  56. if (p->priv_data_class == prev)
  57. break;
  58. /* find next protocol with priv options */
  59. while (p = ffurl_protocol_next(p))
  60. if (p->priv_data_class)
  61. return p->priv_data_class;
  62. return NULL;
  63. }
  64. static const AVOption options[] = {{NULL}};
  65. const AVClass ffurl_context_class = {
  66. .class_name = "URLContext",
  67. .item_name = urlcontext_to_name,
  68. .option = options,
  69. .version = LIBAVUTIL_VERSION_INT,
  70. .child_next = urlcontext_child_next,
  71. .child_class_next = urlcontext_child_class_next,
  72. };
  73. /*@}*/
  74. const char *avio_enum_protocols(void **opaque, int output)
  75. {
  76. URLProtocol **p = (URLProtocol **)opaque;
  77. *p = ffurl_protocol_next(*p);
  78. if (!*p) return NULL;
  79. if ((output && (*p)->url_write) || (!output && (*p)->url_read))
  80. return (*p)->name;
  81. return avio_enum_protocols(opaque, output);
  82. }
  83. int ffurl_register_protocol(URLProtocol *protocol, int size)
  84. {
  85. URLProtocol **p;
  86. if (size < sizeof(URLProtocol)) {
  87. URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
  88. memcpy(temp, protocol, size);
  89. protocol = temp;
  90. }
  91. p = &first_protocol;
  92. while (*p != NULL) p = &(*p)->next;
  93. *p = protocol;
  94. protocol->next = NULL;
  95. return 0;
  96. }
  97. static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
  98. const char *filename, int flags,
  99. const AVIOInterruptCB *int_cb)
  100. {
  101. URLContext *uc;
  102. int err;
  103. #if CONFIG_NETWORK
  104. if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
  105. return AVERROR(EIO);
  106. #endif
  107. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  108. if (!uc) {
  109. err = AVERROR(ENOMEM);
  110. goto fail;
  111. }
  112. uc->av_class = &ffurl_context_class;
  113. uc->filename = (char *) &uc[1];
  114. strcpy(uc->filename, filename);
  115. uc->prot = up;
  116. uc->flags = flags;
  117. uc->is_streamed = 0; /* default = not streamed */
  118. uc->max_packet_size = 0; /* default: stream file */
  119. if (up->priv_data_size) {
  120. uc->priv_data = av_mallocz(up->priv_data_size);
  121. if (up->priv_data_class) {
  122. int proto_len= strlen(up->name);
  123. char *start = strchr(uc->filename, ',');
  124. *(const AVClass**)uc->priv_data = up->priv_data_class;
  125. av_opt_set_defaults(uc->priv_data);
  126. if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
  127. int ret= 0;
  128. char *p= start;
  129. char sep= *++p;
  130. char *key, *val;
  131. p++;
  132. while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
  133. *val= *key= 0;
  134. ret= av_opt_set(uc->priv_data, p, key+1, 0);
  135. if (ret == AVERROR_OPTION_NOT_FOUND)
  136. av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
  137. *val= *key= sep;
  138. p= val+1;
  139. }
  140. if(ret<0 || p!=key){
  141. av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
  142. av_freep(&uc->priv_data);
  143. av_freep(&uc);
  144. goto fail;
  145. }
  146. memmove(start, key+1, strlen(key));
  147. }
  148. }
  149. }
  150. if (int_cb)
  151. uc->interrupt_callback = *int_cb;
  152. *puc = uc;
  153. return 0;
  154. fail:
  155. *puc = NULL;
  156. #if CONFIG_NETWORK
  157. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  158. ff_network_close();
  159. #endif
  160. return err;
  161. }
  162. int ffurl_connect(URLContext* uc, AVDictionary **options)
  163. {
  164. int err =
  165. uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
  166. uc->prot->url_open(uc, uc->filename, uc->flags);
  167. if (err)
  168. return err;
  169. uc->is_connected = 1;
  170. //We must be careful here as ffurl_seek() could be slow, for example for http
  171. if( (uc->flags & AVIO_FLAG_WRITE)
  172. || !strcmp(uc->prot->name, "file"))
  173. if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  174. uc->is_streamed= 1;
  175. return 0;
  176. }
  177. #define URL_SCHEME_CHARS \
  178. "abcdefghijklmnopqrstuvwxyz" \
  179. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  180. "0123456789+-."
  181. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  182. const AVIOInterruptCB *int_cb)
  183. {
  184. URLProtocol *up = NULL;
  185. char proto_str[128], proto_nested[128], *ptr;
  186. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  187. if (!first_protocol) {
  188. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  189. "Missing call to av_register_all()?\n");
  190. }
  191. if (filename[proto_len] != ':' &&
  192. (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
  193. is_dos_path(filename))
  194. strcpy(proto_str, "file");
  195. else
  196. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  197. if ((ptr = strchr(proto_str, ',')))
  198. *ptr = '\0';
  199. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  200. if ((ptr = strchr(proto_nested, '+')))
  201. *ptr = '\0';
  202. while (up = ffurl_protocol_next(up)) {
  203. if (!strcmp(proto_str, up->name))
  204. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  205. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  206. !strcmp(proto_nested, up->name))
  207. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  208. }
  209. *puc = NULL;
  210. return AVERROR_PROTOCOL_NOT_FOUND;
  211. }
  212. int ffurl_open(URLContext **puc, const char *filename, int flags,
  213. const AVIOInterruptCB *int_cb, AVDictionary **options)
  214. {
  215. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  216. if (ret)
  217. return ret;
  218. if (options && (*puc)->prot->priv_data_class &&
  219. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  220. goto fail;
  221. ret = ffurl_connect(*puc, options);
  222. if (!ret)
  223. return 0;
  224. fail:
  225. ffurl_close(*puc);
  226. *puc = NULL;
  227. return ret;
  228. }
  229. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  230. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  231. {
  232. int ret, len;
  233. int fast_retries = 5;
  234. int64_t wait_since = 0;
  235. len = 0;
  236. while (len < size_min) {
  237. ret = transfer_func(h, buf+len, size-len);
  238. if (ret == AVERROR(EINTR))
  239. continue;
  240. if (h->flags & AVIO_FLAG_NONBLOCK)
  241. return ret;
  242. if (ret == AVERROR(EAGAIN)) {
  243. ret = 0;
  244. if (fast_retries) {
  245. fast_retries--;
  246. } else {
  247. if (h->rw_timeout) {
  248. if (!wait_since)
  249. wait_since = av_gettime();
  250. else if (av_gettime() > wait_since + h->rw_timeout)
  251. return AVERROR(EIO);
  252. }
  253. av_usleep(1000);
  254. }
  255. } else if (ret < 1)
  256. return ret < 0 ? ret : len;
  257. if (ret)
  258. fast_retries = FFMAX(fast_retries, 2);
  259. len += ret;
  260. if (len < size && ff_check_interrupt(&h->interrupt_callback))
  261. return AVERROR_EXIT;
  262. }
  263. return len;
  264. }
  265. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  266. {
  267. if (!(h->flags & AVIO_FLAG_READ))
  268. return AVERROR(EIO);
  269. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  270. }
  271. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  272. {
  273. if (!(h->flags & AVIO_FLAG_READ))
  274. return AVERROR(EIO);
  275. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  276. }
  277. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  278. {
  279. if (!(h->flags & AVIO_FLAG_WRITE))
  280. return AVERROR(EIO);
  281. /* avoid sending too big packets */
  282. if (h->max_packet_size && size > h->max_packet_size)
  283. return AVERROR(EIO);
  284. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
  285. }
  286. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  287. {
  288. int64_t ret;
  289. if (!h->prot->url_seek)
  290. return AVERROR(ENOSYS);
  291. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  292. return ret;
  293. }
  294. int ffurl_closep(URLContext **hh)
  295. {
  296. URLContext *h= *hh;
  297. int ret = 0;
  298. if (!h) return 0; /* can happen when ffurl_open fails */
  299. if (h->is_connected && h->prot->url_close)
  300. ret = h->prot->url_close(h);
  301. #if CONFIG_NETWORK
  302. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  303. ff_network_close();
  304. #endif
  305. if (h->prot->priv_data_size) {
  306. if (h->prot->priv_data_class)
  307. av_opt_free(h->priv_data);
  308. av_freep(&h->priv_data);
  309. }
  310. av_freep(hh);
  311. return ret;
  312. }
  313. int ffurl_close(URLContext *h)
  314. {
  315. return ffurl_closep(&h);
  316. }
  317. int avio_check(const char *url, int flags)
  318. {
  319. URLContext *h;
  320. int ret = ffurl_alloc(&h, url, flags, NULL);
  321. if (ret)
  322. return ret;
  323. if (h->prot->url_check) {
  324. ret = h->prot->url_check(h, flags);
  325. } else {
  326. ret = ffurl_connect(h, NULL);
  327. if (ret >= 0)
  328. ret = flags;
  329. }
  330. ffurl_close(h);
  331. return ret;
  332. }
  333. int64_t ffurl_size(URLContext *h)
  334. {
  335. int64_t pos, size;
  336. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  337. if(size<0){
  338. pos = ffurl_seek(h, 0, SEEK_CUR);
  339. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  340. return size;
  341. size++;
  342. ffurl_seek(h, pos, SEEK_SET);
  343. }
  344. return size;
  345. }
  346. int ffurl_get_file_handle(URLContext *h)
  347. {
  348. if (!h->prot->url_get_file_handle)
  349. return -1;
  350. return h->prot->url_get_file_handle(h);
  351. }
  352. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  353. {
  354. if (!h->prot->url_get_multi_file_handle) {
  355. if (!h->prot->url_get_file_handle)
  356. return AVERROR(ENOSYS);
  357. *handles = av_malloc(sizeof(*handles));
  358. if (!*handles)
  359. return AVERROR(ENOMEM);
  360. *numhandles = 1;
  361. *handles[0] = h->prot->url_get_file_handle(h);
  362. return 0;
  363. }
  364. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  365. }
  366. int ffurl_shutdown(URLContext *h, int flags)
  367. {
  368. if (!h->prot->url_shutdown)
  369. return AVERROR(EINVAL);
  370. return h->prot->url_shutdown(h, flags);
  371. }
  372. int ff_check_interrupt(AVIOInterruptCB *cb)
  373. {
  374. int ret;
  375. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  376. return ret;
  377. return 0;
  378. }