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.

193 lines
4.6KB

  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 = -ENOENT;
  65. goto fail;
  66. found:
  67. uc = av_malloc(sizeof(URLContext) + strlen(filename));
  68. if (!uc) {
  69. err = -ENOMEM;
  70. goto fail;
  71. }
  72. strcpy(uc->filename, filename);
  73. uc->prot = up;
  74. uc->flags = flags;
  75. uc->is_streamed = 0; /* default = not streamed */
  76. uc->max_packet_size = 0; /* default: stream file */
  77. err = up->url_open(uc, filename, flags);
  78. if (err < 0) {
  79. av_free(uc);
  80. *puc = NULL;
  81. return err;
  82. }
  83. *puc = uc;
  84. return 0;
  85. fail:
  86. *puc = NULL;
  87. return err;
  88. }
  89. int url_read(URLContext *h, unsigned char *buf, int size)
  90. {
  91. int ret;
  92. if (h->flags & URL_WRONLY)
  93. return AVERROR_IO;
  94. ret = h->prot->url_read(h, buf, size);
  95. return ret;
  96. }
  97. #if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS)
  98. int url_write(URLContext *h, unsigned char *buf, int size)
  99. {
  100. int ret;
  101. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  102. return AVERROR_IO;
  103. /* avoid sending too big packets */
  104. if (h->max_packet_size && size > h->max_packet_size)
  105. return AVERROR_IO;
  106. ret = h->prot->url_write(h, buf, size);
  107. return ret;
  108. }
  109. #endif //CONFIG_MUXERS || CONFIG_PROTOCOLS
  110. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  111. {
  112. offset_t ret;
  113. if (!h->prot->url_seek)
  114. return -EPIPE;
  115. ret = h->prot->url_seek(h, pos, whence);
  116. return ret;
  117. }
  118. int url_close(URLContext *h)
  119. {
  120. int ret;
  121. ret = h->prot->url_close(h);
  122. av_free(h);
  123. return ret;
  124. }
  125. int url_exist(const char *filename)
  126. {
  127. URLContext *h;
  128. if (url_open(&h, filename, URL_RDONLY) < 0)
  129. return 0;
  130. url_close(h);
  131. return 1;
  132. }
  133. offset_t url_filesize(URLContext *h)
  134. {
  135. offset_t pos, size;
  136. pos = url_seek(h, 0, SEEK_CUR);
  137. size = url_seek(h, -1, SEEK_END)+1;
  138. url_seek(h, pos, SEEK_SET);
  139. return size;
  140. }
  141. /*
  142. * Return the maximum packet size associated to packetized file
  143. * handle. If the file is not packetized (stream like http or file on
  144. * disk), then 0 is returned.
  145. *
  146. * @param h file handle
  147. * @return maximum packet size in bytes
  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. /**
  162. * The callback is called in blocking functions to test regulary if
  163. * asynchronous interruption is needed. -EINTR is returned in this
  164. * case by the interrupted function. 'NULL' means no interrupt
  165. * callback is given.
  166. */
  167. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  168. {
  169. if (!interrupt_cb)
  170. interrupt_cb = default_interrupt_cb;
  171. url_interrupt_cb = interrupt_cb;
  172. }