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.

239 lines
5.8KB

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