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.

423 lines
12KB

  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. err = AVERROR(EINVAL);
  145. goto fail;
  146. }
  147. memmove(start, key+1, strlen(key));
  148. }
  149. }
  150. }
  151. if (int_cb)
  152. uc->interrupt_callback = *int_cb;
  153. *puc = uc;
  154. return 0;
  155. fail:
  156. *puc = NULL;
  157. #if CONFIG_NETWORK
  158. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  159. ff_network_close();
  160. #endif
  161. return err;
  162. }
  163. int ffurl_connect(URLContext* uc, AVDictionary **options)
  164. {
  165. int err =
  166. uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
  167. uc->prot->url_open(uc, uc->filename, uc->flags);
  168. if (err)
  169. return err;
  170. uc->is_connected = 1;
  171. //We must be careful here as ffurl_seek() could be slow, for example for http
  172. if( (uc->flags & AVIO_FLAG_WRITE)
  173. || !strcmp(uc->prot->name, "file"))
  174. if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  175. uc->is_streamed= 1;
  176. return 0;
  177. }
  178. #define URL_SCHEME_CHARS \
  179. "abcdefghijklmnopqrstuvwxyz" \
  180. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  181. "0123456789+-."
  182. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  183. const AVIOInterruptCB *int_cb)
  184. {
  185. URLProtocol *up = NULL;
  186. char proto_str[128], proto_nested[128], *ptr;
  187. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  188. if (!first_protocol) {
  189. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  190. "Missing call to av_register_all()?\n");
  191. }
  192. if (filename[proto_len] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
  193. strcpy(proto_str, "file");
  194. else
  195. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  196. if ((ptr = strchr(proto_str, ',')))
  197. *ptr = '\0';
  198. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  199. if ((ptr = strchr(proto_nested, '+')))
  200. *ptr = '\0';
  201. while (up = ffurl_protocol_next(up)) {
  202. if (!strcmp(proto_str, up->name))
  203. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  204. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  205. !strcmp(proto_nested, up->name))
  206. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  207. }
  208. *puc = NULL;
  209. return AVERROR_PROTOCOL_NOT_FOUND;
  210. }
  211. int ffurl_open(URLContext **puc, const char *filename, int flags,
  212. const AVIOInterruptCB *int_cb, AVDictionary **options)
  213. {
  214. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  215. if (ret)
  216. return ret;
  217. if (options && (*puc)->prot->priv_data_class &&
  218. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  219. goto fail;
  220. ret = ffurl_connect(*puc, options);
  221. if (!ret)
  222. return 0;
  223. fail:
  224. ffurl_close(*puc);
  225. *puc = NULL;
  226. return ret;
  227. }
  228. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  229. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  230. {
  231. int ret, len;
  232. int fast_retries = 5;
  233. int64_t wait_since = 0;
  234. len = 0;
  235. while (len < size_min) {
  236. ret = transfer_func(h, buf+len, size-len);
  237. if (ret == AVERROR(EINTR))
  238. continue;
  239. if (h->flags & AVIO_FLAG_NONBLOCK)
  240. return ret;
  241. if (ret == AVERROR(EAGAIN)) {
  242. ret = 0;
  243. if (fast_retries) {
  244. fast_retries--;
  245. } else {
  246. if (h->rw_timeout) {
  247. if (!wait_since)
  248. wait_since = av_gettime();
  249. else if (av_gettime() > wait_since + h->rw_timeout)
  250. return AVERROR(EIO);
  251. }
  252. av_usleep(1000);
  253. }
  254. } else if (ret < 1)
  255. return ret < 0 ? ret : len;
  256. if (ret)
  257. fast_retries = FFMAX(fast_retries, 2);
  258. len += ret;
  259. if (len < size && ff_check_interrupt(&h->interrupt_callback))
  260. return AVERROR_EXIT;
  261. }
  262. return len;
  263. }
  264. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  265. {
  266. if (!(h->flags & AVIO_FLAG_READ))
  267. return AVERROR(EIO);
  268. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  269. }
  270. int ffurl_read_complete(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, size, h->prot->url_read);
  275. }
  276. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  277. {
  278. if (!(h->flags & AVIO_FLAG_WRITE))
  279. return AVERROR(EIO);
  280. /* avoid sending too big packets */
  281. if (h->max_packet_size && size > h->max_packet_size)
  282. return AVERROR(EIO);
  283. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
  284. }
  285. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  286. {
  287. int64_t ret;
  288. if (!h->prot->url_seek)
  289. return AVERROR(ENOSYS);
  290. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  291. return ret;
  292. }
  293. int ffurl_closep(URLContext **hh)
  294. {
  295. URLContext *h= *hh;
  296. int ret = 0;
  297. if (!h) return 0; /* can happen when ffurl_open fails */
  298. if (h->is_connected && h->prot->url_close)
  299. ret = h->prot->url_close(h);
  300. #if CONFIG_NETWORK
  301. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  302. ff_network_close();
  303. #endif
  304. if (h->prot->priv_data_size) {
  305. if (h->prot->priv_data_class)
  306. av_opt_free(h->priv_data);
  307. av_freep(&h->priv_data);
  308. }
  309. av_freep(hh);
  310. return ret;
  311. }
  312. int ffurl_close(URLContext *h)
  313. {
  314. return ffurl_closep(&h);
  315. }
  316. int avio_check(const char *url, int flags)
  317. {
  318. URLContext *h;
  319. int ret = ffurl_alloc(&h, url, flags, NULL);
  320. if (ret)
  321. return ret;
  322. if (h->prot->url_check) {
  323. ret = h->prot->url_check(h, flags);
  324. } else {
  325. ret = ffurl_connect(h, NULL);
  326. if (ret >= 0)
  327. ret = flags;
  328. }
  329. ffurl_close(h);
  330. return ret;
  331. }
  332. int64_t ffurl_size(URLContext *h)
  333. {
  334. int64_t pos, size;
  335. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  336. if(size<0){
  337. pos = ffurl_seek(h, 0, SEEK_CUR);
  338. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  339. return size;
  340. size++;
  341. ffurl_seek(h, pos, SEEK_SET);
  342. }
  343. return size;
  344. }
  345. int ffurl_get_file_handle(URLContext *h)
  346. {
  347. if (!h->prot->url_get_file_handle)
  348. return -1;
  349. return h->prot->url_get_file_handle(h);
  350. }
  351. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  352. {
  353. if (!h->prot->url_get_multi_file_handle) {
  354. if (!h->prot->url_get_file_handle)
  355. return AVERROR(ENOSYS);
  356. *handles = av_malloc(sizeof(*handles));
  357. if (!*handles)
  358. return AVERROR(ENOMEM);
  359. *numhandles = 1;
  360. *handles[0] = h->prot->url_get_file_handle(h);
  361. return 0;
  362. }
  363. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  364. }
  365. int ffurl_shutdown(URLContext *h, int flags)
  366. {
  367. if (!h->prot->url_shutdown)
  368. return AVERROR(EINVAL);
  369. return h->prot->url_shutdown(h, flags);
  370. }
  371. int ff_check_interrupt(AVIOInterruptCB *cb)
  372. {
  373. int ret;
  374. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  375. return ret;
  376. return 0;
  377. }