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.

168 lines
4.0KB

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