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.

135 lines
3.1KB

  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 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. return -ENOENT;
  57. found:
  58. uc = av_malloc(sizeof(URLContext));
  59. if (!uc)
  60. return -ENOMEM;
  61. uc->prot = up;
  62. uc->flags = flags;
  63. uc->is_streamed = 0; /* default = not streamed */
  64. uc->packet_size = 1; /* default packet size */
  65. err = up->url_open(uc, filename, flags);
  66. if (err < 0) {
  67. av_free(uc);
  68. *puc = NULL;
  69. return err;
  70. }
  71. *puc = uc;
  72. return 0;
  73. }
  74. int url_read(URLContext *h, unsigned char *buf, int size)
  75. {
  76. int ret;
  77. if (h->flags & URL_WRONLY)
  78. return -EIO;
  79. ret = h->prot->url_read(h, buf, size);
  80. return ret;
  81. }
  82. int url_write(URLContext *h, unsigned char *buf, int size)
  83. {
  84. int ret;
  85. if (!(h->flags & URL_WRONLY))
  86. return -EIO;
  87. ret = h->prot->url_write(h, buf, size);
  88. return ret;
  89. }
  90. offset_t url_seek(URLContext *h, offset_t pos, int whence)
  91. {
  92. offset_t ret;
  93. if (!h->prot->url_seek)
  94. return -EPIPE;
  95. ret = h->prot->url_seek(h, pos, whence);
  96. return ret;
  97. }
  98. int url_close(URLContext *h)
  99. {
  100. int ret;
  101. ret = h->prot->url_close(h);
  102. av_free(h);
  103. return ret;
  104. }
  105. int url_exist(const char *filename)
  106. {
  107. URLContext *h;
  108. if (url_open(&h, filename, URL_RDONLY) < 0)
  109. return 0;
  110. url_close(h);
  111. return 1;
  112. }
  113. offset_t url_filesize(URLContext *h)
  114. {
  115. offset_t pos, size;
  116. pos = url_seek(h, 0, SEEK_CUR);
  117. size = url_seek(h, 0, SEEK_END);
  118. url_seek(h, pos, SEEK_SET);
  119. return size;
  120. }