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.

209 lines
5.0KB

  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. uc->filename = (char *) &uc[1];
  79. strcpy(uc->filename, filename);
  80. uc->prot = up;
  81. uc->flags = flags;
  82. uc->is_streamed = 0; /* default = not streamed */
  83. uc->max_packet_size = 0; /* default: stream file */
  84. err = up->url_open(uc, filename, flags);
  85. if (err < 0) {
  86. av_free(uc);
  87. *puc = NULL;
  88. return err;
  89. }
  90. *puc = uc;
  91. return 0;
  92. fail:
  93. *puc = NULL;
  94. return err;
  95. }
  96. int url_read(URLContext *h, unsigned char *buf, int size)
  97. {
  98. int ret;
  99. if (h->flags & URL_WRONLY)
  100. return AVERROR(EIO);
  101. ret = h->prot->url_read(h, buf, size);
  102. return ret;
  103. }
  104. #if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS)
  105. int url_write(URLContext *h, unsigned char *buf, int size)
  106. {
  107. int ret;
  108. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  109. return AVERROR(EIO);
  110. /* avoid sending too big packets */
  111. if (h->max_packet_size && size > h->max_packet_size)
  112. return AVERROR(EIO);
  113. ret = h->prot->url_write(h, buf, size);
  114. return ret;
  115. }
  116. #endif //CONFIG_MUXERS || CONFIG_PROTOCOLS
  117. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  118. {
  119. offset_t ret;
  120. if (!h->prot->url_seek)
  121. return AVERROR(EPIPE);
  122. ret = h->prot->url_seek(h, pos, whence);
  123. return ret;
  124. }
  125. int url_close(URLContext *h)
  126. {
  127. int ret = 0;
  128. if (!h) return 0; /* can happen when url_open fails */
  129. if (h->prot->url_close)
  130. ret = h->prot->url_close(h);
  131. av_free(h);
  132. return ret;
  133. }
  134. int url_exist(const char *filename)
  135. {
  136. URLContext *h;
  137. if (url_open(&h, filename, URL_RDONLY) < 0)
  138. return 0;
  139. url_close(h);
  140. return 1;
  141. }
  142. offset_t url_filesize(URLContext *h)
  143. {
  144. offset_t pos, size;
  145. size= url_seek(h, 0, AVSEEK_SIZE);
  146. if(size<0){
  147. pos = url_seek(h, 0, SEEK_CUR);
  148. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  149. return size;
  150. size++;
  151. url_seek(h, pos, SEEK_SET);
  152. }
  153. return size;
  154. }
  155. int url_get_max_packet_size(URLContext *h)
  156. {
  157. return h->max_packet_size;
  158. }
  159. void url_get_filename(URLContext *h, char *buf, int buf_size)
  160. {
  161. av_strlcpy(buf, h->filename, buf_size);
  162. }
  163. static int default_interrupt_cb(void)
  164. {
  165. return 0;
  166. }
  167. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  168. {
  169. if (!interrupt_cb)
  170. interrupt_cb = default_interrupt_cb;
  171. url_interrupt_cb = interrupt_cb;
  172. }
  173. int av_url_read_pause(URLContext *h, int pause)
  174. {
  175. if (!h->prot->url_read_pause)
  176. return AVERROR(ENOSYS);
  177. return h->prot->url_read_pause(h, pause);
  178. }
  179. offset_t av_url_read_seek(URLContext *h,
  180. int stream_index, int64_t timestamp, int flags)
  181. {
  182. if (!h->prot->url_read_seek)
  183. return AVERROR(ENOSYS);
  184. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  185. }