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.

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