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.

190 lines
4.5KB

  1. /*
  2. * Unbuffered io for ffmpeg system
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <ctype.h>
  21. static int default_interrupt_cb(void);
  22. URLProtocol *first_protocol = NULL;
  23. URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
  24. int register_protocol(URLProtocol *protocol)
  25. {
  26. URLProtocol **p;
  27. p = &first_protocol;
  28. while (*p != NULL) p = &(*p)->next;
  29. *p = protocol;
  30. protocol->next = NULL;
  31. return 0;
  32. }
  33. int url_open(URLContext **puc, const char *filename, int flags)
  34. {
  35. URLContext *uc;
  36. URLProtocol *up;
  37. const char *p;
  38. char proto_str[128], *q;
  39. int err;
  40. p = filename;
  41. q = proto_str;
  42. while (*p != '\0' && *p != ':') {
  43. /* protocols can only contain alphabetic chars */
  44. if (!isalpha(*p))
  45. goto file_proto;
  46. if ((q - proto_str) < sizeof(proto_str) - 1)
  47. *q++ = *p;
  48. p++;
  49. }
  50. /* if the protocol has length 1, we consider it is a dos drive */
  51. if (*p == '\0' || (q - proto_str) <= 1) {
  52. file_proto:
  53. strcpy(proto_str, "file");
  54. } else {
  55. *q = '\0';
  56. }
  57. up = first_protocol;
  58. while (up != NULL) {
  59. if (!strcmp(proto_str, up->name))
  60. goto found;
  61. up = up->next;
  62. }
  63. err = -ENOENT;
  64. goto fail;
  65. found:
  66. uc = av_malloc(sizeof(URLContext) + strlen(filename));
  67. if (!uc) {
  68. err = -ENOMEM;
  69. goto fail;
  70. }
  71. strcpy(uc->filename, filename);
  72. uc->prot = up;
  73. uc->flags = flags;
  74. uc->is_streamed = 0; /* default = not streamed */
  75. uc->max_packet_size = 0; /* default: stream file */
  76. err = up->url_open(uc, filename, flags);
  77. if (err < 0) {
  78. av_free(uc);
  79. *puc = NULL;
  80. return err;
  81. }
  82. *puc = uc;
  83. return 0;
  84. fail:
  85. *puc = NULL;
  86. return err;
  87. }
  88. int url_read(URLContext *h, unsigned char *buf, int size)
  89. {
  90. int ret;
  91. if (h->flags & URL_WRONLY)
  92. return -EIO;
  93. ret = h->prot->url_read(h, buf, size);
  94. return ret;
  95. }
  96. int url_write(URLContext *h, unsigned char *buf, int size)
  97. {
  98. int ret;
  99. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  100. return -EIO;
  101. /* avoid sending too big packets */
  102. if (h->max_packet_size && size > h->max_packet_size)
  103. return -EIO;
  104. ret = h->prot->url_write(h, buf, size);
  105. return ret;
  106. }
  107. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  108. {
  109. offset_t ret;
  110. if (!h->prot->url_seek)
  111. return -EPIPE;
  112. ret = h->prot->url_seek(h, pos, whence);
  113. return ret;
  114. }
  115. int url_close(URLContext *h)
  116. {
  117. int ret;
  118. ret = h->prot->url_close(h);
  119. av_free(h);
  120. return ret;
  121. }
  122. int url_exist(const char *filename)
  123. {
  124. URLContext *h;
  125. if (url_open(&h, filename, URL_RDONLY) < 0)
  126. return 0;
  127. url_close(h);
  128. return 1;
  129. }
  130. offset_t url_filesize(URLContext *h)
  131. {
  132. offset_t pos, size;
  133. pos = url_seek(h, 0, SEEK_CUR);
  134. size = url_seek(h, 0, SEEK_END);
  135. url_seek(h, pos, SEEK_SET);
  136. return size;
  137. }
  138. /*
  139. * Return the maximum packet size associated to packetized file
  140. * handle. If the file is not packetized (stream like http or file on
  141. * disk), then 0 is returned.
  142. *
  143. * @param h file handle
  144. * @return maximum packet size in bytes
  145. */
  146. int url_get_max_packet_size(URLContext *h)
  147. {
  148. return h->max_packet_size;
  149. }
  150. void url_get_filename(URLContext *h, char *buf, int buf_size)
  151. {
  152. pstrcpy(buf, buf_size, h->filename);
  153. }
  154. static int default_interrupt_cb(void)
  155. {
  156. return 0;
  157. }
  158. /**
  159. * The callback is called in blocking functions to test regulary if
  160. * asynchronous interruption is needed. -EINTR is returned in this
  161. * case by the interrupted function. 'NULL' means no interrupt
  162. * callback is given.
  163. */
  164. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  165. {
  166. if (!interrupt_cb)
  167. interrupt_cb = default_interrupt_cb;
  168. url_interrupt_cb = interrupt_cb;
  169. }