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.

425 lines
11KB

  1. /*
  2. * Unbuffered io for ffmpeg system
  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. /* needed for usleep() */
  22. #define _XOPEN_SOURCE 600
  23. #include <unistd.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/opt.h"
  26. #include "os_support.h"
  27. #include "avformat.h"
  28. #if CONFIG_NETWORK
  29. #include "network.h"
  30. #endif
  31. #include "url.h"
  32. #if FF_API_URL_CLASS
  33. /** @name Logging context. */
  34. /*@{*/
  35. static const char *urlcontext_to_name(void *ptr)
  36. {
  37. URLContext *h = (URLContext *)ptr;
  38. if(h->prot) return h->prot->name;
  39. else return "NULL";
  40. }
  41. static const AVOption options[] = {{NULL}};
  42. static const AVClass urlcontext_class =
  43. { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
  44. /*@}*/
  45. #endif
  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. #if FF_API_REGISTER_PROTOCOL
  78. /* The layout of URLProtocol as of when major was bumped to 52 */
  79. struct URLProtocol_compat {
  80. const char *name;
  81. int (*url_open)(URLContext *h, const char *filename, int flags);
  82. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  83. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  84. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  85. int (*url_close)(URLContext *h);
  86. struct URLProtocol *next;
  87. };
  88. int av_register_protocol(URLProtocol *protocol)
  89. {
  90. return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
  91. }
  92. int register_protocol(URLProtocol *protocol)
  93. {
  94. return ffurl_register_protocol(protocol, sizeof(struct URLProtocol_compat));
  95. }
  96. #endif
  97. static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
  98. const char *filename, int flags)
  99. {
  100. URLContext *uc;
  101. int err;
  102. #if CONFIG_NETWORK
  103. if (!ff_network_init())
  104. return AVERROR(EIO);
  105. #endif
  106. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  107. if (!uc) {
  108. err = AVERROR(ENOMEM);
  109. goto fail;
  110. }
  111. #if FF_API_URL_CLASS
  112. uc->av_class = &urlcontext_class;
  113. #endif
  114. uc->filename = (char *) &uc[1];
  115. strcpy(uc->filename, filename);
  116. uc->prot = up;
  117. uc->flags = flags;
  118. uc->is_streamed = 0; /* default = not streamed */
  119. uc->max_packet_size = 0; /* default: stream file */
  120. if (up->priv_data_size) {
  121. uc->priv_data = av_mallocz(up->priv_data_size);
  122. if (up->priv_data_class) {
  123. *(const AVClass**)uc->priv_data = up->priv_data_class;
  124. av_opt_set_defaults(uc->priv_data);
  125. }
  126. }
  127. *puc = uc;
  128. return 0;
  129. fail:
  130. *puc = NULL;
  131. #if CONFIG_NETWORK
  132. ff_network_close();
  133. #endif
  134. return err;
  135. }
  136. int ffurl_connect(URLContext* uc)
  137. {
  138. int err = uc->prot->url_open(uc, uc->filename, uc->flags);
  139. if (err)
  140. return err;
  141. uc->is_connected = 1;
  142. //We must be careful here as ffurl_seek() could be slow, for example for http
  143. if( (uc->flags & (AVIO_WRONLY | AVIO_RDWR))
  144. || !strcmp(uc->prot->name, "file"))
  145. if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  146. uc->is_streamed= 1;
  147. return 0;
  148. }
  149. #if FF_API_OLD_AVIO
  150. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  151. const char *filename, int flags)
  152. {
  153. int ret;
  154. ret = url_alloc_for_protocol(puc, up, filename, flags);
  155. if (ret)
  156. goto fail;
  157. ret = ffurl_connect(*puc);
  158. if (!ret)
  159. return 0;
  160. fail:
  161. ffurl_close(*puc);
  162. *puc = NULL;
  163. return ret;
  164. }
  165. int url_alloc(URLContext **puc, const char *filename, int flags)
  166. {
  167. return ffurl_alloc(puc, filename, flags);
  168. }
  169. int url_connect(URLContext* uc)
  170. {
  171. return ffurl_connect(uc);
  172. }
  173. int url_open(URLContext **puc, const char *filename, int flags)
  174. {
  175. return ffurl_open(puc, filename, flags);
  176. }
  177. int url_read(URLContext *h, unsigned char *buf, int size)
  178. {
  179. return ffurl_read(h, buf, size);
  180. }
  181. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  182. {
  183. return ffurl_read_complete(h, buf, size);
  184. }
  185. int url_write(URLContext *h, const unsigned char *buf, int size)
  186. {
  187. return ffurl_write(h, buf, size);
  188. }
  189. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  190. {
  191. return ffurl_seek(h, pos, whence);
  192. }
  193. int url_close(URLContext *h)
  194. {
  195. return ffurl_close(h);
  196. }
  197. int64_t url_filesize(URLContext *h)
  198. {
  199. return ffurl_size(h);
  200. }
  201. int url_get_file_handle(URLContext *h)
  202. {
  203. return ffurl_get_file_handle(h);
  204. }
  205. int url_get_max_packet_size(URLContext *h)
  206. {
  207. return h->max_packet_size;
  208. }
  209. void url_get_filename(URLContext *h, char *buf, int buf_size)
  210. {
  211. av_strlcpy(buf, h->filename, buf_size);
  212. }
  213. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  214. {
  215. avio_set_interrupt_cb(interrupt_cb);
  216. }
  217. int av_register_protocol2(URLProtocol *protocol, int size)
  218. {
  219. return ffurl_register_protocol(protocol, size);
  220. }
  221. #endif
  222. #define URL_SCHEME_CHARS \
  223. "abcdefghijklmnopqrstuvwxyz" \
  224. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  225. "0123456789+-."
  226. int ffurl_alloc(URLContext **puc, const char *filename, int flags)
  227. {
  228. URLProtocol *up;
  229. char proto_str[128], proto_nested[128], *ptr;
  230. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  231. if (filename[proto_len] != ':' || is_dos_path(filename))
  232. strcpy(proto_str, "file");
  233. else
  234. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  235. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  236. if ((ptr = strchr(proto_nested, '+')))
  237. *ptr = '\0';
  238. up = first_protocol;
  239. while (up != NULL) {
  240. if (!strcmp(proto_str, up->name))
  241. return url_alloc_for_protocol (puc, up, filename, flags);
  242. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  243. !strcmp(proto_nested, up->name))
  244. return url_alloc_for_protocol (puc, up, filename, flags);
  245. up = up->next;
  246. }
  247. *puc = NULL;
  248. return AVERROR(ENOENT);
  249. }
  250. int ffurl_open(URLContext **puc, const char *filename, int flags)
  251. {
  252. int ret = ffurl_alloc(puc, filename, flags);
  253. if (ret)
  254. return ret;
  255. ret = ffurl_connect(*puc);
  256. if (!ret)
  257. return 0;
  258. ffurl_close(*puc);
  259. *puc = NULL;
  260. return ret;
  261. }
  262. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  263. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  264. {
  265. int ret, len;
  266. int fast_retries = 5;
  267. len = 0;
  268. while (len < size_min) {
  269. ret = transfer_func(h, buf+len, size-len);
  270. if (ret == AVERROR(EINTR))
  271. continue;
  272. if (h->flags & AVIO_FLAG_NONBLOCK)
  273. return ret;
  274. if (ret == AVERROR(EAGAIN)) {
  275. ret = 0;
  276. if (fast_retries)
  277. fast_retries--;
  278. else
  279. usleep(1000);
  280. } else if (ret < 1)
  281. return ret < 0 ? ret : len;
  282. if (ret)
  283. fast_retries = FFMAX(fast_retries, 2);
  284. len += ret;
  285. if (len < size && url_interrupt_cb())
  286. return AVERROR_EXIT;
  287. }
  288. return len;
  289. }
  290. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  291. {
  292. if (h->flags & AVIO_WRONLY)
  293. return AVERROR(EIO);
  294. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  295. }
  296. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  297. {
  298. if (h->flags & AVIO_WRONLY)
  299. return AVERROR(EIO);
  300. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  301. }
  302. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  303. {
  304. if (!(h->flags & (AVIO_WRONLY | AVIO_RDWR)))
  305. return AVERROR(EIO);
  306. /* avoid sending too big packets */
  307. if (h->max_packet_size && size > h->max_packet_size)
  308. return AVERROR(EIO);
  309. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
  310. }
  311. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  312. {
  313. int64_t ret;
  314. if (!h->prot->url_seek)
  315. return AVERROR(ENOSYS);
  316. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  317. return ret;
  318. }
  319. int ffurl_close(URLContext *h)
  320. {
  321. int ret = 0;
  322. if (!h) return 0; /* can happen when ffurl_open fails */
  323. if (h->is_connected && h->prot->url_close)
  324. ret = h->prot->url_close(h);
  325. #if CONFIG_NETWORK
  326. ff_network_close();
  327. #endif
  328. if (h->prot->priv_data_size)
  329. av_free(h->priv_data);
  330. av_free(h);
  331. return ret;
  332. }
  333. int url_exist(const char *filename)
  334. {
  335. URLContext *h;
  336. if (ffurl_open(&h, filename, AVIO_RDONLY) < 0)
  337. return 0;
  338. ffurl_close(h);
  339. return 1;
  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