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.

218 lines
5.1KB

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