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.

269 lines
6.4KB

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