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.

188 lines
4.4KB

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