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.

414 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. 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] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
  192. strcpy(proto_str, "file");
  193. else
  194. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  195. if ((ptr = strchr(proto_str, ',')))
  196. *ptr = '\0';
  197. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  198. if ((ptr = strchr(proto_nested, '+')))
  199. *ptr = '\0';
  200. while (up = ffurl_protocol_next(up)) {
  201. if (!strcmp(proto_str, up->name))
  202. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  203. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  204. !strcmp(proto_nested, up->name))
  205. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  206. }
  207. *puc = NULL;
  208. return AVERROR_PROTOCOL_NOT_FOUND;
  209. }
  210. int ffurl_open(URLContext **puc, const char *filename, int flags,
  211. const AVIOInterruptCB *int_cb, AVDictionary **options)
  212. {
  213. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  214. if (ret)
  215. return ret;
  216. if (options && (*puc)->prot->priv_data_class &&
  217. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  218. goto fail;
  219. ret = ffurl_connect(*puc, options);
  220. if (!ret)
  221. return 0;
  222. fail:
  223. ffurl_close(*puc);
  224. *puc = NULL;
  225. return ret;
  226. }
  227. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  228. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  229. {
  230. int ret, len;
  231. int fast_retries = 5;
  232. len = 0;
  233. while (len < size_min) {
  234. ret = transfer_func(h, buf+len, size-len);
  235. if (ret == AVERROR(EINTR))
  236. continue;
  237. if (h->flags & AVIO_FLAG_NONBLOCK)
  238. return ret;
  239. if (ret == AVERROR(EAGAIN)) {
  240. ret = 0;
  241. if (fast_retries)
  242. fast_retries--;
  243. else
  244. av_usleep(1000);
  245. } else if (ret < 1)
  246. return ret < 0 ? ret : len;
  247. if (ret)
  248. fast_retries = FFMAX(fast_retries, 2);
  249. len += ret;
  250. if (len < size && ff_check_interrupt(&h->interrupt_callback))
  251. return AVERROR_EXIT;
  252. }
  253. return len;
  254. }
  255. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  256. {
  257. if (!(h->flags & AVIO_FLAG_READ))
  258. return AVERROR(EIO);
  259. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  260. }
  261. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  262. {
  263. if (!(h->flags & AVIO_FLAG_READ))
  264. return AVERROR(EIO);
  265. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  266. }
  267. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  268. {
  269. if (!(h->flags & AVIO_FLAG_WRITE))
  270. return AVERROR(EIO);
  271. /* avoid sending too big packets */
  272. if (h->max_packet_size && size > h->max_packet_size)
  273. return AVERROR(EIO);
  274. return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
  275. }
  276. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  277. {
  278. int64_t ret;
  279. if (!h->prot->url_seek)
  280. return AVERROR(ENOSYS);
  281. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  282. return ret;
  283. }
  284. int ffurl_closep(URLContext **hh)
  285. {
  286. URLContext *h= *hh;
  287. int ret = 0;
  288. if (!h) return 0; /* can happen when ffurl_open fails */
  289. if (h->is_connected && h->prot->url_close)
  290. ret = h->prot->url_close(h);
  291. #if CONFIG_NETWORK
  292. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  293. ff_network_close();
  294. #endif
  295. if (h->prot->priv_data_size) {
  296. if (h->prot->priv_data_class)
  297. av_opt_free(h->priv_data);
  298. av_freep(&h->priv_data);
  299. }
  300. av_freep(hh);
  301. return ret;
  302. }
  303. int ffurl_close(URLContext *h)
  304. {
  305. return ffurl_closep(&h);
  306. }
  307. int avio_check(const char *url, int flags)
  308. {
  309. URLContext *h;
  310. int ret = ffurl_alloc(&h, url, flags, NULL);
  311. if (ret)
  312. return ret;
  313. if (h->prot->url_check) {
  314. ret = h->prot->url_check(h, flags);
  315. } else {
  316. ret = ffurl_connect(h, NULL);
  317. if (ret >= 0)
  318. ret = flags;
  319. }
  320. ffurl_close(h);
  321. return ret;
  322. }
  323. int64_t ffurl_size(URLContext *h)
  324. {
  325. int64_t pos, size;
  326. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  327. if(size<0){
  328. pos = ffurl_seek(h, 0, SEEK_CUR);
  329. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  330. return size;
  331. size++;
  332. ffurl_seek(h, pos, SEEK_SET);
  333. }
  334. return size;
  335. }
  336. int ffurl_get_file_handle(URLContext *h)
  337. {
  338. if (!h->prot->url_get_file_handle)
  339. return -1;
  340. return h->prot->url_get_file_handle(h);
  341. }
  342. int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
  343. {
  344. if (!h->prot->url_get_multi_file_handle) {
  345. if (!h->prot->url_get_file_handle)
  346. return AVERROR(ENOSYS);
  347. *handles = av_malloc(sizeof(*handles));
  348. if (!*handles)
  349. return AVERROR(ENOMEM);
  350. *numhandles = 1;
  351. *handles[0] = h->prot->url_get_file_handle(h);
  352. return 0;
  353. }
  354. return h->prot->url_get_multi_file_handle(h, handles, numhandles);
  355. }
  356. int ffurl_shutdown(URLContext *h, int flags)
  357. {
  358. if (!h->prot->url_shutdown)
  359. return AVERROR(EINVAL);
  360. return h->prot->url_shutdown(h, flags);
  361. }
  362. int ff_check_interrupt(AVIOInterruptCB *cb)
  363. {
  364. int ret;
  365. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  366. return ret;
  367. return 0;
  368. }