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.

267 lines
6.3KB

  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 carefull 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 < 1)
  144. return ret;
  145. len += ret;
  146. }
  147. return len;
  148. }
  149. int url_write(URLContext *h, unsigned char *buf, int size)
  150. {
  151. int ret;
  152. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  153. return AVERROR(EIO);
  154. /* avoid sending too big packets */
  155. if (h->max_packet_size && size > h->max_packet_size)
  156. return AVERROR(EIO);
  157. ret = h->prot->url_write(h, buf, size);
  158. return ret;
  159. }
  160. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  161. {
  162. int64_t ret;
  163. if (!h->prot->url_seek)
  164. return AVERROR(EPIPE);
  165. ret = h->prot->url_seek(h, pos, whence);
  166. return ret;
  167. }
  168. int url_close(URLContext *h)
  169. {
  170. int ret = 0;
  171. if (!h) return 0; /* can happen when url_open fails */
  172. if (h->prot->url_close)
  173. ret = h->prot->url_close(h);
  174. av_free(h);
  175. return ret;
  176. }
  177. int url_exist(const char *filename)
  178. {
  179. URLContext *h;
  180. if (url_open(&h, filename, URL_RDONLY) < 0)
  181. return 0;
  182. url_close(h);
  183. return 1;
  184. }
  185. int64_t url_filesize(URLContext *h)
  186. {
  187. int64_t pos, size;
  188. size= url_seek(h, 0, AVSEEK_SIZE);
  189. if(size<0){
  190. pos = url_seek(h, 0, SEEK_CUR);
  191. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  192. return size;
  193. size++;
  194. url_seek(h, pos, SEEK_SET);
  195. }
  196. return size;
  197. }
  198. int url_get_file_handle(URLContext *h)
  199. {
  200. if (!h->prot->url_get_file_handle)
  201. return -1;
  202. return h->prot->url_get_file_handle(h);
  203. }
  204. int url_get_max_packet_size(URLContext *h)
  205. {
  206. return h->max_packet_size;
  207. }
  208. void url_get_filename(URLContext *h, char *buf, int buf_size)
  209. {
  210. av_strlcpy(buf, h->filename, buf_size);
  211. }
  212. static int default_interrupt_cb(void)
  213. {
  214. return 0;
  215. }
  216. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  217. {
  218. if (!interrupt_cb)
  219. interrupt_cb = default_interrupt_cb;
  220. url_interrupt_cb = interrupt_cb;
  221. }
  222. int av_url_read_pause(URLContext *h, int pause)
  223. {
  224. if (!h->prot->url_read_pause)
  225. return AVERROR(ENOSYS);
  226. return h->prot->url_read_pause(h, pause);
  227. }
  228. int64_t av_url_read_seek(URLContext *h,
  229. int stream_index, int64_t timestamp, int flags)
  230. {
  231. if (!h->prot->url_read_seek)
  232. return AVERROR(ENOSYS);
  233. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  234. }