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.

191 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. static int default_interrupt_cb(void);
  21. URLProtocol *first_protocol = NULL;
  22. URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
  23. int register_protocol(URLProtocol *protocol)
  24. {
  25. URLProtocol **p;
  26. p = &first_protocol;
  27. while (*p != NULL) p = &(*p)->next;
  28. *p = protocol;
  29. protocol->next = NULL;
  30. return 0;
  31. }
  32. int url_open(URLContext **puc, const char *filename, int flags)
  33. {
  34. URLContext *uc;
  35. URLProtocol *up;
  36. const char *p;
  37. char proto_str[128], *q;
  38. int err;
  39. p = filename;
  40. q = proto_str;
  41. while (*p != '\0' && *p != ':') {
  42. /* protocols can only contain alphabetic chars */
  43. if (!isalpha(*p))
  44. goto file_proto;
  45. if ((q - proto_str) < sizeof(proto_str) - 1)
  46. *q++ = *p;
  47. p++;
  48. }
  49. /* if the protocol has length 1, we consider it is a dos drive */
  50. if (*p == '\0' || (q - proto_str) <= 1) {
  51. file_proto:
  52. strcpy(proto_str, "file");
  53. } else {
  54. *q = '\0';
  55. }
  56. up = first_protocol;
  57. while (up != NULL) {
  58. if (!strcmp(proto_str, up->name))
  59. goto found;
  60. up = up->next;
  61. }
  62. err = -ENOENT;
  63. goto fail;
  64. found:
  65. uc = av_malloc(sizeof(URLContext) + strlen(filename));
  66. if (!uc) {
  67. err = -ENOMEM;
  68. goto fail;
  69. }
  70. strcpy(uc->filename, filename);
  71. uc->prot = up;
  72. uc->flags = flags;
  73. uc->is_streamed = 0; /* default = not streamed */
  74. uc->max_packet_size = 0; /* default: stream file */
  75. err = up->url_open(uc, filename, flags);
  76. if (err < 0) {
  77. av_free(uc);
  78. *puc = NULL;
  79. return err;
  80. }
  81. *puc = uc;
  82. return 0;
  83. fail:
  84. *puc = NULL;
  85. return err;
  86. }
  87. int url_read(URLContext *h, unsigned char *buf, int size)
  88. {
  89. int ret;
  90. if (h->flags & URL_WRONLY)
  91. return -EIO;
  92. ret = h->prot->url_read(h, buf, size);
  93. return ret;
  94. }
  95. #ifdef CONFIG_ENCODERS
  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. #endif //CONFIG_ENCODERS
  108. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  109. {
  110. offset_t ret;
  111. if (!h->prot->url_seek)
  112. return -EPIPE;
  113. ret = h->prot->url_seek(h, pos, whence);
  114. return ret;
  115. }
  116. int url_close(URLContext *h)
  117. {
  118. int ret;
  119. ret = h->prot->url_close(h);
  120. av_free(h);
  121. return ret;
  122. }
  123. int url_exist(const char *filename)
  124. {
  125. URLContext *h;
  126. if (url_open(&h, filename, URL_RDONLY) < 0)
  127. return 0;
  128. url_close(h);
  129. return 1;
  130. }
  131. offset_t url_filesize(URLContext *h)
  132. {
  133. offset_t pos, size;
  134. pos = url_seek(h, 0, SEEK_CUR);
  135. size = url_seek(h, -1, SEEK_END)+1;
  136. url_seek(h, pos, SEEK_SET);
  137. return size;
  138. }
  139. /*
  140. * Return the maximum packet size associated to packetized file
  141. * handle. If the file is not packetized (stream like http or file on
  142. * disk), then 0 is returned.
  143. *
  144. * @param h file handle
  145. * @return maximum packet size in bytes
  146. */
  147. int url_get_max_packet_size(URLContext *h)
  148. {
  149. return h->max_packet_size;
  150. }
  151. void url_get_filename(URLContext *h, char *buf, int buf_size)
  152. {
  153. pstrcpy(buf, buf_size, h->filename);
  154. }
  155. static int default_interrupt_cb(void)
  156. {
  157. return 0;
  158. }
  159. /**
  160. * The callback is called in blocking functions to test regulary if
  161. * asynchronous interruption is needed. -EINTR is returned in this
  162. * case by the interrupted function. 'NULL' means no interrupt
  163. * callback is given.
  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. }