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.

427 lines
11KB

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