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.

435 lines
13KB

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