avio.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. int url_write(URLContext *h, unsigned char *buf, int size)
  105. {
  106. int ret;
  107. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  108. return AVERROR(EIO);
  109. /* avoid sending too big packets */
  110. if (h->max_packet_size && size > h->max_packet_size)
  111. return AVERROR(EIO);
  112. ret = h->prot->url_write(h, buf, size);
  113. return ret;
  114. }
  115. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  116. {
  117. offset_t ret;
  118. if (!h->prot->url_seek)
  119. return AVERROR(EPIPE);
  120. ret = h->prot->url_seek(h, pos, whence);
  121. return ret;
  122. }
  123. int url_close(URLContext *h)
  124. {
  125. int ret = 0;
  126. if (!h) return 0; /* can happen when url_open fails */
  127. if (h->prot->url_close)
  128. ret = h->prot->url_close(h);
  129. av_free(h);
  130. return ret;
  131. }
  132. int url_exist(const char *filename)
  133. {
  134. URLContext *h;
  135. if (url_open(&h, filename, URL_RDONLY) < 0)
  136. return 0;
  137. url_close(h);
  138. return 1;
  139. }
  140. offset_t url_filesize(URLContext *h)
  141. {
  142. offset_t pos, size;
  143. size= url_seek(h, 0, AVSEEK_SIZE);
  144. if(size<0){
  145. pos = url_seek(h, 0, SEEK_CUR);
  146. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  147. return size;
  148. size++;
  149. url_seek(h, pos, SEEK_SET);
  150. }
  151. return size;
  152. }
  153. int url_get_max_packet_size(URLContext *h)
  154. {
  155. return h->max_packet_size;
  156. }
  157. void url_get_filename(URLContext *h, char *buf, int buf_size)
  158. {
  159. av_strlcpy(buf, h->filename, buf_size);
  160. }
  161. static int default_interrupt_cb(void)
  162. {
  163. return 0;
  164. }
  165. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  166. {
  167. if (!interrupt_cb)
  168. interrupt_cb = default_interrupt_cb;
  169. url_interrupt_cb = interrupt_cb;
  170. }
  171. int av_url_read_pause(URLContext *h, int pause)
  172. {
  173. if (!h->prot->url_read_pause)
  174. return AVERROR(ENOSYS);
  175. return h->prot->url_read_pause(h, pause);
  176. }
  177. offset_t av_url_read_seek(URLContext *h,
  178. int stream_index, int64_t timestamp, int flags)
  179. {
  180. if (!h->prot->url_read_seek)
  181. return AVERROR(ENOSYS);
  182. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  183. }