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.

291 lines
6.9KB

  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 };
  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_protocol(URLProtocol *protocol)
  54. {
  55. URLProtocol **p;
  56. p = &first_protocol;
  57. while (*p != NULL) p = &(*p)->next;
  58. *p = protocol;
  59. protocol->next = NULL;
  60. return 0;
  61. }
  62. #if LIBAVFORMAT_VERSION_MAJOR < 53
  63. int register_protocol(URLProtocol *protocol)
  64. {
  65. return av_register_protocol(protocol);
  66. }
  67. #endif
  68. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  69. const char *filename, int flags)
  70. {
  71. URLContext *uc;
  72. int err;
  73. #if CONFIG_NETWORK
  74. if (!ff_network_init())
  75. return AVERROR(EIO);
  76. #endif
  77. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  78. if (!uc) {
  79. err = AVERROR(ENOMEM);
  80. goto fail;
  81. }
  82. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  83. uc->av_class = &urlcontext_class;
  84. #endif
  85. uc->filename = (char *) &uc[1];
  86. strcpy(uc->filename, filename);
  87. uc->prot = up;
  88. uc->flags = flags;
  89. uc->is_streamed = 0; /* default = not streamed */
  90. uc->max_packet_size = 0; /* default: stream file */
  91. err = up->url_open(uc, filename, flags);
  92. if (err < 0) {
  93. av_free(uc);
  94. goto fail;
  95. }
  96. //We must be careful here as url_seek() could be slow, for example for http
  97. if( (flags & (URL_WRONLY | URL_RDWR))
  98. || !strcmp(up->name, "file"))
  99. if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
  100. uc->is_streamed= 1;
  101. *puc = uc;
  102. return 0;
  103. fail:
  104. *puc = NULL;
  105. #if CONFIG_NETWORK
  106. ff_network_close();
  107. #endif
  108. return err;
  109. }
  110. int url_open(URLContext **puc, const char *filename, int flags)
  111. {
  112. URLProtocol *up;
  113. const char *p;
  114. char proto_str[128], *q;
  115. p = filename;
  116. q = proto_str;
  117. while (*p != '\0' && *p != ':') {
  118. /* protocols can only contain alphabetic chars */
  119. if (!isalpha(*p))
  120. goto file_proto;
  121. if ((q - proto_str) < sizeof(proto_str) - 1)
  122. *q++ = *p;
  123. p++;
  124. }
  125. /* if the protocol has length 1, we consider it is a dos drive */
  126. if (*p == '\0' || is_dos_path(filename)) {
  127. file_proto:
  128. strcpy(proto_str, "file");
  129. } else {
  130. *q = '\0';
  131. }
  132. up = first_protocol;
  133. while (up != NULL) {
  134. if (!strcmp(proto_str, up->name))
  135. return url_open_protocol (puc, up, filename, flags);
  136. up = up->next;
  137. }
  138. *puc = NULL;
  139. return AVERROR(ENOENT);
  140. }
  141. int url_read(URLContext *h, unsigned char *buf, int size)
  142. {
  143. int ret;
  144. if (h->flags & URL_WRONLY)
  145. return AVERROR(EIO);
  146. ret = h->prot->url_read(h, buf, size);
  147. return ret;
  148. }
  149. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  150. {
  151. int ret, len;
  152. int fast_retries = 5;
  153. len = 0;
  154. while (len < size) {
  155. ret = url_read(h, buf+len, size-len);
  156. if (ret == AVERROR(EAGAIN)) {
  157. ret = 0;
  158. if (fast_retries)
  159. fast_retries--;
  160. else
  161. usleep(1000);
  162. } else if (ret < 1)
  163. return ret < 0 ? ret : len;
  164. if (ret)
  165. fast_retries = FFMAX(fast_retries, 2);
  166. len += ret;
  167. }
  168. return len;
  169. }
  170. int url_write(URLContext *h, unsigned char *buf, int size)
  171. {
  172. int ret;
  173. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  174. return AVERROR(EIO);
  175. /* avoid sending too big packets */
  176. if (h->max_packet_size && size > h->max_packet_size)
  177. return AVERROR(EIO);
  178. ret = h->prot->url_write(h, buf, size);
  179. return ret;
  180. }
  181. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  182. {
  183. int64_t ret;
  184. if (!h->prot->url_seek)
  185. return AVERROR(EPIPE);
  186. ret = h->prot->url_seek(h, pos, whence);
  187. return ret;
  188. }
  189. int url_close(URLContext *h)
  190. {
  191. int ret = 0;
  192. if (!h) return 0; /* can happen when url_open fails */
  193. if (h->prot->url_close)
  194. ret = h->prot->url_close(h);
  195. #if CONFIG_NETWORK
  196. ff_network_close();
  197. #endif
  198. av_free(h);
  199. return ret;
  200. }
  201. int url_exist(const char *filename)
  202. {
  203. URLContext *h;
  204. if (url_open(&h, filename, URL_RDONLY) < 0)
  205. return 0;
  206. url_close(h);
  207. return 1;
  208. }
  209. int64_t url_filesize(URLContext *h)
  210. {
  211. int64_t pos, size;
  212. size= url_seek(h, 0, AVSEEK_SIZE);
  213. if(size<0){
  214. pos = url_seek(h, 0, SEEK_CUR);
  215. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  216. return size;
  217. size++;
  218. url_seek(h, pos, SEEK_SET);
  219. }
  220. return size;
  221. }
  222. int url_get_file_handle(URLContext *h)
  223. {
  224. if (!h->prot->url_get_file_handle)
  225. return -1;
  226. return h->prot->url_get_file_handle(h);
  227. }
  228. int url_get_max_packet_size(URLContext *h)
  229. {
  230. return h->max_packet_size;
  231. }
  232. void url_get_filename(URLContext *h, char *buf, int buf_size)
  233. {
  234. av_strlcpy(buf, h->filename, buf_size);
  235. }
  236. static int default_interrupt_cb(void)
  237. {
  238. return 0;
  239. }
  240. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  241. {
  242. if (!interrupt_cb)
  243. interrupt_cb = default_interrupt_cb;
  244. url_interrupt_cb = interrupt_cb;
  245. }
  246. int av_url_read_pause(URLContext *h, int pause)
  247. {
  248. if (!h->prot->url_read_pause)
  249. return AVERROR(ENOSYS);
  250. return h->prot->url_read_pause(h, pause);
  251. }
  252. int64_t av_url_read_seek(URLContext *h,
  253. int stream_index, int64_t timestamp, int flags)
  254. {
  255. if (!h->prot->url_read_seek)
  256. return AVERROR(ENOSYS);
  257. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  258. }