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.

420 lines
11KB

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