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.

226 lines
5.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 "avformat.h"
  22. #include "avstring.h"
  23. #include "opt.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. *puc = uc;
  109. return 0;
  110. fail:
  111. *puc = NULL;
  112. return err;
  113. }
  114. int url_read(URLContext *h, unsigned char *buf, int size)
  115. {
  116. int ret;
  117. if (h->flags & URL_WRONLY)
  118. return AVERROR(EIO);
  119. ret = h->prot->url_read(h, buf, size);
  120. return ret;
  121. }
  122. int url_write(URLContext *h, unsigned char *buf, int size)
  123. {
  124. int ret;
  125. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  126. return AVERROR(EIO);
  127. /* avoid sending too big packets */
  128. if (h->max_packet_size && size > h->max_packet_size)
  129. return AVERROR(EIO);
  130. ret = h->prot->url_write(h, buf, size);
  131. return ret;
  132. }
  133. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  134. {
  135. offset_t ret;
  136. if (!h->prot->url_seek)
  137. return AVERROR(EPIPE);
  138. ret = h->prot->url_seek(h, pos, whence);
  139. return ret;
  140. }
  141. int url_close(URLContext *h)
  142. {
  143. int ret = 0;
  144. if (!h) return 0; /* can happen when url_open fails */
  145. if (h->prot->url_close)
  146. ret = h->prot->url_close(h);
  147. av_free(h);
  148. return ret;
  149. }
  150. int url_exist(const char *filename)
  151. {
  152. URLContext *h;
  153. if (url_open(&h, filename, URL_RDONLY) < 0)
  154. return 0;
  155. url_close(h);
  156. return 1;
  157. }
  158. offset_t url_filesize(URLContext *h)
  159. {
  160. offset_t pos, size;
  161. size= url_seek(h, 0, AVSEEK_SIZE);
  162. if(size<0){
  163. pos = url_seek(h, 0, SEEK_CUR);
  164. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  165. return size;
  166. size++;
  167. url_seek(h, pos, SEEK_SET);
  168. }
  169. return size;
  170. }
  171. int url_get_max_packet_size(URLContext *h)
  172. {
  173. return h->max_packet_size;
  174. }
  175. void url_get_filename(URLContext *h, char *buf, int buf_size)
  176. {
  177. av_strlcpy(buf, h->filename, buf_size);
  178. }
  179. static int default_interrupt_cb(void)
  180. {
  181. return 0;
  182. }
  183. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  184. {
  185. if (!interrupt_cb)
  186. interrupt_cb = default_interrupt_cb;
  187. url_interrupt_cb = interrupt_cb;
  188. }
  189. int av_url_read_pause(URLContext *h, int pause)
  190. {
  191. if (!h->prot->url_read_pause)
  192. return AVERROR(ENOSYS);
  193. return h->prot->url_read_pause(h, pause);
  194. }
  195. offset_t av_url_read_seek(URLContext *h,
  196. int stream_index, int64_t timestamp, int flags)
  197. {
  198. if (!h->prot->url_read_seek)
  199. return AVERROR(ENOSYS);
  200. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  201. }