avio.c 4.4 KB

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