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.

233 lines
5.6KB

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