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.

355 lines
8.7KB

  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 "libavcodec/opt.h"
  26. #include "os_support.h"
  27. #include "avformat.h"
  28. #if CONFIG_NETWORK
  29. #include "network.h"
  30. #endif
  31. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  32. /** @name Logging context. */
  33. /*@{*/
  34. static const char *urlcontext_to_name(void *ptr)
  35. {
  36. URLContext *h = (URLContext *)ptr;
  37. if(h->prot) return h->prot->name;
  38. else return "NULL";
  39. }
  40. static const AVOption options[] = {{NULL}};
  41. static const AVClass urlcontext_class =
  42. { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
  43. /*@}*/
  44. #endif
  45. static int default_interrupt_cb(void);
  46. URLProtocol *first_protocol = NULL;
  47. URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
  48. URLProtocol *av_protocol_next(URLProtocol *p)
  49. {
  50. if(p) return p->next;
  51. else return first_protocol;
  52. }
  53. int av_register_protocol2(URLProtocol *protocol, int size)
  54. {
  55. URLProtocol **p;
  56. if (size < sizeof(URLProtocol)) {
  57. URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
  58. memcpy(temp, protocol, size);
  59. protocol = temp;
  60. }
  61. p = &first_protocol;
  62. while (*p != NULL) p = &(*p)->next;
  63. *p = protocol;
  64. protocol->next = NULL;
  65. return 0;
  66. }
  67. #if LIBAVFORMAT_VERSION_MAJOR < 53
  68. /* The layout of URLProtocol as of when major was bumped to 52 */
  69. struct URLProtocol_compat {
  70. const char *name;
  71. int (*url_open)(URLContext *h, const char *filename, int flags);
  72. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  73. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  74. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  75. int (*url_close)(URLContext *h);
  76. struct URLProtocol *next;
  77. };
  78. int av_register_protocol(URLProtocol *protocol)
  79. {
  80. return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
  81. }
  82. int register_protocol(URLProtocol *protocol)
  83. {
  84. return av_register_protocol(protocol);
  85. }
  86. #endif
  87. static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
  88. const char *filename, int flags)
  89. {
  90. URLContext *uc;
  91. int err;
  92. #if CONFIG_NETWORK
  93. if (!ff_network_init())
  94. return AVERROR(EIO);
  95. #endif
  96. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  97. if (!uc) {
  98. err = AVERROR(ENOMEM);
  99. goto fail;
  100. }
  101. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  102. uc->av_class = &urlcontext_class;
  103. #endif
  104. uc->filename = (char *) &uc[1];
  105. strcpy(uc->filename, filename);
  106. uc->prot = up;
  107. uc->flags = flags;
  108. uc->is_streamed = 0; /* default = not streamed */
  109. uc->max_packet_size = 0; /* default: stream file */
  110. if (up->priv_data_size) {
  111. uc->priv_data = av_mallocz(up->priv_data_size);
  112. if (up->priv_data_class) {
  113. *(const AVClass**)uc->priv_data = up->priv_data_class;
  114. av_opt_set_defaults(uc->priv_data);
  115. }
  116. }
  117. *puc = uc;
  118. return 0;
  119. fail:
  120. *puc = NULL;
  121. #if CONFIG_NETWORK
  122. ff_network_close();
  123. #endif
  124. return err;
  125. }
  126. int url_connect(URLContext* uc)
  127. {
  128. int err = uc->prot->url_open(uc, uc->filename, uc->flags);
  129. if (err)
  130. return err;
  131. uc->is_connected = 1;
  132. //We must be careful here as url_seek() could be slow, for example for http
  133. if( (uc->flags & (URL_WRONLY | URL_RDWR))
  134. || !strcmp(uc->prot->name, "file"))
  135. if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
  136. uc->is_streamed= 1;
  137. return 0;
  138. }
  139. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  140. const char *filename, int flags)
  141. {
  142. int ret;
  143. ret = url_alloc_for_protocol(puc, up, filename, flags);
  144. if (ret)
  145. goto fail;
  146. ret = url_connect(*puc);
  147. if (!ret)
  148. return 0;
  149. fail:
  150. url_close(*puc);
  151. *puc = NULL;
  152. return ret;
  153. }
  154. int url_alloc(URLContext **puc, const char *filename, int flags)
  155. {
  156. URLProtocol *up;
  157. const char *p;
  158. char proto_str[128], *q;
  159. p = filename;
  160. q = proto_str;
  161. while (*p != '\0' && *p != ':') {
  162. /* protocols can only contain alphabetic chars */
  163. if (!isalpha(*p))
  164. goto file_proto;
  165. if ((q - proto_str) < sizeof(proto_str) - 1)
  166. *q++ = *p;
  167. p++;
  168. }
  169. /* if the protocol has length 1, we consider it is a dos drive */
  170. if (*p == '\0' || is_dos_path(filename)) {
  171. file_proto:
  172. strcpy(proto_str, "file");
  173. } else {
  174. *q = '\0';
  175. }
  176. up = first_protocol;
  177. while (up != NULL) {
  178. if (!strcmp(proto_str, up->name))
  179. return url_alloc_for_protocol (puc, up, filename, flags);
  180. up = up->next;
  181. }
  182. *puc = NULL;
  183. return AVERROR(ENOENT);
  184. }
  185. int url_open(URLContext **puc, const char *filename, int flags)
  186. {
  187. int ret = url_alloc(puc, filename, flags);
  188. if (ret)
  189. return ret;
  190. ret = url_connect(*puc);
  191. if (!ret)
  192. return 0;
  193. url_close(*puc);
  194. *puc = NULL;
  195. return ret;
  196. }
  197. int url_read(URLContext *h, unsigned char *buf, int size)
  198. {
  199. int ret;
  200. if (h->flags & URL_WRONLY)
  201. return AVERROR(EIO);
  202. ret = h->prot->url_read(h, buf, size);
  203. return ret;
  204. }
  205. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  206. {
  207. int ret, len;
  208. int fast_retries = 5;
  209. len = 0;
  210. while (len < size) {
  211. ret = url_read(h, buf+len, size-len);
  212. if (ret == AVERROR(EAGAIN)) {
  213. ret = 0;
  214. if (fast_retries)
  215. fast_retries--;
  216. else
  217. usleep(1000);
  218. } else if (ret < 1)
  219. return ret < 0 ? ret : len;
  220. if (ret)
  221. fast_retries = FFMAX(fast_retries, 2);
  222. len += ret;
  223. }
  224. return len;
  225. }
  226. int url_write(URLContext *h, const unsigned char *buf, int size)
  227. {
  228. int ret;
  229. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  230. return AVERROR(EIO);
  231. /* avoid sending too big packets */
  232. if (h->max_packet_size && size > h->max_packet_size)
  233. return AVERROR(EIO);
  234. ret = h->prot->url_write(h, buf, size);
  235. return ret;
  236. }
  237. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  238. {
  239. int64_t ret;
  240. if (!h->prot->url_seek)
  241. return AVERROR(ENOSYS);
  242. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  243. return ret;
  244. }
  245. int url_close(URLContext *h)
  246. {
  247. int ret = 0;
  248. if (!h) return 0; /* can happen when url_open fails */
  249. if (h->is_connected && h->prot->url_close)
  250. ret = h->prot->url_close(h);
  251. #if CONFIG_NETWORK
  252. ff_network_close();
  253. #endif
  254. if (h->prot->priv_data_size)
  255. av_free(h->priv_data);
  256. av_free(h);
  257. return ret;
  258. }
  259. int url_exist(const char *filename)
  260. {
  261. URLContext *h;
  262. if (url_open(&h, filename, URL_RDONLY) < 0)
  263. return 0;
  264. url_close(h);
  265. return 1;
  266. }
  267. int64_t url_filesize(URLContext *h)
  268. {
  269. int64_t pos, size;
  270. size= url_seek(h, 0, AVSEEK_SIZE);
  271. if(size<0){
  272. pos = url_seek(h, 0, SEEK_CUR);
  273. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  274. return size;
  275. size++;
  276. url_seek(h, pos, SEEK_SET);
  277. }
  278. return size;
  279. }
  280. int url_get_file_handle(URLContext *h)
  281. {
  282. if (!h->prot->url_get_file_handle)
  283. return -1;
  284. return h->prot->url_get_file_handle(h);
  285. }
  286. int url_get_max_packet_size(URLContext *h)
  287. {
  288. return h->max_packet_size;
  289. }
  290. void url_get_filename(URLContext *h, char *buf, int buf_size)
  291. {
  292. av_strlcpy(buf, h->filename, buf_size);
  293. }
  294. static int default_interrupt_cb(void)
  295. {
  296. return 0;
  297. }
  298. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  299. {
  300. if (!interrupt_cb)
  301. interrupt_cb = default_interrupt_cb;
  302. url_interrupt_cb = interrupt_cb;
  303. }
  304. int av_url_read_pause(URLContext *h, int pause)
  305. {
  306. if (!h->prot->url_read_pause)
  307. return AVERROR(ENOSYS);
  308. return h->prot->url_read_pause(h, pause);
  309. }
  310. int64_t av_url_read_seek(URLContext *h,
  311. int stream_index, int64_t timestamp, int flags)
  312. {
  313. if (!h->prot->url_read_seek)
  314. return AVERROR(ENOSYS);
  315. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  316. }