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.

163 lines
3.8KB

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