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.

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