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.

157 lines
3.7KB

  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));
  60. if (!uc) {
  61. err = -ENOMEM;
  62. goto fail;
  63. }
  64. uc->prot = up;
  65. uc->flags = flags;
  66. uc->is_streamed = 0; /* default = not streamed */
  67. uc->max_packet_size = 0; /* default: stream file */
  68. err = up->url_open(uc, filename, flags);
  69. if (err < 0) {
  70. av_free(uc);
  71. *puc = NULL;
  72. return err;
  73. }
  74. *puc = uc;
  75. return 0;
  76. fail:
  77. *puc = NULL;
  78. return err;
  79. }
  80. int url_read(URLContext *h, unsigned char *buf, int size)
  81. {
  82. int ret;
  83. if (h->flags & URL_WRONLY)
  84. return -EIO;
  85. ret = h->prot->url_read(h, buf, size);
  86. return ret;
  87. }
  88. int url_write(URLContext *h, unsigned char *buf, int size)
  89. {
  90. int ret;
  91. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  92. return -EIO;
  93. /* avoid sending too big packets */
  94. if (h->max_packet_size && size > h->max_packet_size)
  95. return -EIO;
  96. ret = h->prot->url_write(h, buf, size);
  97. return ret;
  98. }
  99. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  100. {
  101. offset_t ret;
  102. if (!h->prot->url_seek)
  103. return -EPIPE;
  104. ret = h->prot->url_seek(h, pos, whence);
  105. return ret;
  106. }
  107. int url_close(URLContext *h)
  108. {
  109. int ret;
  110. ret = h->prot->url_close(h);
  111. av_free(h);
  112. return ret;
  113. }
  114. int url_exist(const char *filename)
  115. {
  116. URLContext *h;
  117. if (url_open(&h, filename, URL_RDONLY) < 0)
  118. return 0;
  119. url_close(h);
  120. return 1;
  121. }
  122. offset_t url_filesize(URLContext *h)
  123. {
  124. offset_t pos, size;
  125. pos = url_seek(h, 0, SEEK_CUR);
  126. size = url_seek(h, 0, SEEK_END);
  127. url_seek(h, pos, SEEK_SET);
  128. return size;
  129. }
  130. /*
  131. * Return the maximum packet size associated to packetized file
  132. * handle. If the file is not packetized (stream like http or file on
  133. * disk), then 0 is returned.
  134. *
  135. * @param h file handle
  136. * @return maximum packet size in bytes
  137. */
  138. int url_get_max_packet_size(URLContext *h)
  139. {
  140. return h->max_packet_size;
  141. }