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.

234 lines
7.9KB

  1. /*
  2. * Icecast protocol for Libav
  3. * Copyright (c) 2014 Marvin Scholz
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/opt.h"
  23. #include "avformat.h"
  24. #include "network.h"
  25. typedef struct IcecastContext {
  26. const AVClass *class;
  27. URLContext *hd;
  28. int send_started;
  29. char *user;
  30. // Options
  31. char *content_type;
  32. char *description;
  33. char *genre;
  34. int legacy_icecast;
  35. char *name;
  36. char *pass;
  37. int public;
  38. char *url;
  39. char *user_agent;
  40. } IcecastContext;
  41. #define DEFAULT_ICE_USER "source"
  42. #define NOT_EMPTY(s) (s && s[0])
  43. #define OFFSET(x) offsetof(IcecastContext, x)
  44. #define E AV_OPT_FLAG_ENCODING_PARAM
  45. static const AVOption options[] = {
  46. { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  47. { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  48. { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  49. { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  50. { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
  51. { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  52. { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  53. { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  54. { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
  55. { NULL }
  56. };
  57. static char *cat_header(char buf[], const char key[], const char value[])
  58. {
  59. if (NOT_EMPTY(value)) {
  60. int len = strlen(key) + strlen(value) + 5;
  61. int is_first = !buf;
  62. char *tmp = NULL;
  63. if (buf)
  64. len += strlen(buf);
  65. if (!(tmp = av_realloc(buf, len))) {
  66. av_freep(&buf);
  67. return NULL;
  68. } else {
  69. buf = tmp;
  70. }
  71. if (is_first)
  72. *buf = '\0';
  73. av_strlcatf(buf, len, "%s: %s\r\n", key, value);
  74. }
  75. return buf;
  76. }
  77. static int icecast_close(URLContext *h)
  78. {
  79. IcecastContext *s = h->priv_data;
  80. if (s->hd)
  81. ffurl_close(s->hd);
  82. return 0;
  83. }
  84. static int icecast_open(URLContext *h, const char *uri, int flags)
  85. {
  86. IcecastContext *s = h->priv_data;
  87. // Dict to set options that we pass to the HTTP protocol
  88. AVDictionary *opt_dict = NULL;
  89. // URI part variables
  90. char h_url[1024], host[1024], auth[1024], path[1024];
  91. char *headers = NULL, *user = NULL;
  92. int port, ret;
  93. if (flags & AVIO_FLAG_READ)
  94. return AVERROR(ENOSYS);
  95. // Build header strings
  96. headers = cat_header(headers, "Ice-Name", s->name);
  97. headers = cat_header(headers, "Ice-Description", s->description);
  98. headers = cat_header(headers, "Ice-URL", s->url);
  99. headers = cat_header(headers, "Ice-Genre", s->genre);
  100. headers = cat_header(headers, "Ice-Public", s->public ? "1" : "0");
  101. if (!headers) {
  102. ret = AVERROR(ENOMEM);
  103. goto cleanup;
  104. }
  105. // Set options
  106. av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
  107. av_dict_set(&opt_dict, "auth_type", "basic", 0);
  108. av_dict_set(&opt_dict, "headers", headers, 0);
  109. av_dict_set(&opt_dict, "chunked_post", "0", 0);
  110. av_dict_set(&opt_dict, "send_expect_100", s->legacy_icecast ? "0" : "1", 0);
  111. if (NOT_EMPTY(s->content_type))
  112. av_dict_set(&opt_dict, "content_type", s->content_type, 0);
  113. else
  114. av_dict_set(&opt_dict, "content_type", "audio/mpeg", 0);
  115. if (NOT_EMPTY(s->user_agent))
  116. av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
  117. // Parse URI
  118. av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
  119. &port, path, sizeof(path), uri);
  120. // Check for auth data in URI
  121. if (auth[0]) {
  122. char *sep = strchr(auth,':');
  123. if (sep) {
  124. *sep = 0;
  125. sep++;
  126. if (s->pass) {
  127. av_free(s->pass);
  128. av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
  129. }
  130. if (!(s->pass = av_strdup(sep))) {
  131. ret = AVERROR(ENOMEM);
  132. goto cleanup;
  133. }
  134. }
  135. if (!(user = av_strdup(auth))) {
  136. ret = AVERROR(ENOMEM);
  137. goto cleanup;
  138. }
  139. }
  140. // Build new authstring
  141. snprintf(auth, sizeof(auth),
  142. "%s:%s",
  143. user ? user : DEFAULT_ICE_USER,
  144. s->pass ? s->pass : "");
  145. // Check for mountpoint (path)
  146. if (!path[0] || strcmp(path, "/") == 0) {
  147. av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
  148. ret = AVERROR(EIO);
  149. goto cleanup;
  150. }
  151. // Build new URI for passing to http protocol
  152. ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
  153. // Finally open http proto handler
  154. ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict,
  155. h->protocols, h);
  156. cleanup:
  157. // Free variables
  158. av_freep(&user);
  159. av_freep(&headers);
  160. av_dict_free(&opt_dict);
  161. return ret;
  162. }
  163. static int icecast_write(URLContext *h, const uint8_t *buf, int size)
  164. {
  165. IcecastContext *s = h->priv_data;
  166. if (!s->send_started) {
  167. s->send_started = 1;
  168. if (!s->content_type && size >= 8) {
  169. static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
  170. static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
  171. static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
  172. if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
  173. av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n");
  174. av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
  175. } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
  176. av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n");
  177. av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
  178. } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
  179. av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n");
  180. av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
  181. } else {
  182. av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
  183. av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
  184. }
  185. }
  186. }
  187. return ffurl_write(s->hd, buf, size);
  188. }
  189. static const AVClass icecast_context_class = {
  190. .class_name = "icecast",
  191. .item_name = av_default_item_name,
  192. .option = options,
  193. .version = LIBAVUTIL_VERSION_INT,
  194. };
  195. const URLProtocol ff_icecast_protocol = {
  196. .name = "icecast",
  197. .url_open = icecast_open,
  198. .url_write = icecast_write,
  199. .url_close = icecast_close,
  200. .priv_data_size = sizeof(IcecastContext),
  201. .priv_data_class = &icecast_context_class,
  202. .flags = URL_PROTOCOL_FLAG_NETWORK,
  203. };