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.

142 lines
3.2KB

  1. /*
  2. * Unbuffered io for ffmpeg system
  3. * Copyright (c) 2001 Gerard Lantau
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 (*p == '\0') {
  45. strcpy(proto_str, "file");
  46. } else {
  47. *q = '\0';
  48. }
  49. up = first_protocol;
  50. while (up != NULL) {
  51. if (!strcmp(proto_str, up->name))
  52. goto found;
  53. up = up->next;
  54. }
  55. return -ENOENT;
  56. found:
  57. uc = malloc(sizeof(URLContext));
  58. if (!uc)
  59. return -ENOMEM;
  60. uc->prot = up;
  61. uc->flags = flags;
  62. uc->is_streamed = 0; /* default = not streamed */
  63. uc->packet_size = 1; /* default packet size */
  64. err = up->url_open(uc, filename, flags);
  65. if (err < 0) {
  66. free(uc);
  67. *puc = NULL;
  68. return err;
  69. }
  70. *puc = uc;
  71. return 0;
  72. }
  73. int url_read(URLContext *h, unsigned char *buf, int size)
  74. {
  75. int ret;
  76. if (h->flags & URL_WRONLY)
  77. return -EIO;
  78. ret = h->prot->url_read(h, buf, size);
  79. return ret;
  80. }
  81. int url_write(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_write(h, buf, size);
  87. return ret;
  88. }
  89. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  90. {
  91. offset_t ret;
  92. if (!h->prot->url_seek)
  93. return -EPIPE;
  94. ret = h->prot->url_seek(h, pos, whence);
  95. return ret;
  96. }
  97. int url_getformat(URLContext *h, URLFormat *f)
  98. {
  99. memset(f, 0, sizeof(*f));
  100. if (!h->prot->url_getformat)
  101. return -ENODATA;
  102. return h->prot->url_getformat(h, f);
  103. }
  104. int url_close(URLContext *h)
  105. {
  106. int ret;
  107. ret = h->prot->url_close(h);
  108. free(h);
  109. return ret;
  110. }
  111. int url_exist(const char *filename)
  112. {
  113. URLContext *h;
  114. if (url_open(&h, filename, URL_RDONLY) < 0)
  115. return 0;
  116. url_close(h);
  117. return 1;
  118. }
  119. offset_t url_filesize(URLContext *h)
  120. {
  121. offset_t pos, size;
  122. pos = url_seek(h, 0, SEEK_CUR);
  123. size = url_seek(h, 0, SEEK_END);
  124. url_seek(h, pos, SEEK_SET);
  125. return size;
  126. }