avio.c 5.9 KB

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