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.

147 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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include "avformat.h"
  24. URLProtocol *first_protocol = NULL;
  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. if ((q - proto_str) < sizeof(proto_str) - 1)
  45. *q++ = *p;
  46. p++;
  47. }
  48. if (*p == '\0') {
  49. strcpy(proto_str, "file");
  50. } else {
  51. *q = '\0';
  52. }
  53. up = first_protocol;
  54. while (up != NULL) {
  55. if (!strcmp(proto_str, up->name))
  56. goto found;
  57. up = up->next;
  58. }
  59. return -ENOENT;
  60. found:
  61. uc = malloc(sizeof(URLContext));
  62. if (!uc)
  63. return -ENOMEM;
  64. uc->prot = up;
  65. uc->flags = flags;
  66. uc->is_streamed = 0; /* default = not streamed */
  67. uc->packet_size = 1; /* default packet size */
  68. err = up->url_open(uc, filename, flags);
  69. if (err < 0) {
  70. free(uc);
  71. *puc = NULL;
  72. return err;
  73. }
  74. *puc = uc;
  75. return 0;
  76. }
  77. int url_read(URLContext *h, unsigned char *buf, int size)
  78. {
  79. int ret;
  80. if (h->flags & URL_WRONLY)
  81. return -EIO;
  82. ret = h->prot->url_read(h, buf, size);
  83. return ret;
  84. }
  85. int url_write(URLContext *h, unsigned char *buf, int size)
  86. {
  87. int ret;
  88. if (!(h->flags & URL_WRONLY))
  89. return -EIO;
  90. ret = h->prot->url_write(h, buf, size);
  91. return ret;
  92. }
  93. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  94. {
  95. offset_t ret;
  96. if (!h->prot->url_seek)
  97. return -EPIPE;
  98. ret = h->prot->url_seek(h, pos, whence);
  99. return ret;
  100. }
  101. int url_getformat(URLContext *h, URLFormat *f)
  102. {
  103. memset(f, 0, sizeof(*f));
  104. if (!h->prot->url_getformat)
  105. return -ENODATA;
  106. return h->prot->url_getformat(h, f);
  107. }
  108. int url_close(URLContext *h)
  109. {
  110. int ret;
  111. ret = h->prot->url_close(h);
  112. 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. }