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.

216 lines
7.3KB

  1. /*
  2. * Icecast protocol for FFmpeg
  3. * Copyright (c) 2014 Marvin Scholz
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/bprint.h"
  23. #include "libavutil/opt.h"
  24. #include "avformat.h"
  25. #include "network.h"
  26. typedef struct IcecastContext {
  27. const AVClass *class;
  28. URLContext *hd;
  29. int send_started;
  30. char *user;
  31. // Options
  32. char *content_type;
  33. char *description;
  34. char *genre;
  35. int legacy_icecast;
  36. char *name;
  37. char *pass;
  38. int public;
  39. char *url;
  40. char *user_agent;
  41. } IcecastContext;
  42. #define DEFAULT_ICE_USER "source"
  43. #define NOT_EMPTY(s) (s && s[0])
  44. #define OFFSET(x) offsetof(IcecastContext, x)
  45. #define E AV_OPT_FLAG_ENCODING_PARAM
  46. static const AVOption options[] = {
  47. { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  48. { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  49. { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  50. { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  51. { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
  52. { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  53. { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  54. { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
  55. { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
  56. { NULL }
  57. };
  58. static void cat_header(AVBPrint *bp, const char key[], const char value[])
  59. {
  60. if (NOT_EMPTY(value))
  61. av_bprintf(bp, "%s: %s\r\n", key, value);
  62. }
  63. static int icecast_close(URLContext *h)
  64. {
  65. IcecastContext *s = h->priv_data;
  66. if (s->hd)
  67. ffurl_close(s->hd);
  68. return 0;
  69. }
  70. static int icecast_open(URLContext *h, const char *uri, int flags)
  71. {
  72. IcecastContext *s = h->priv_data;
  73. // Dict to set options that we pass to the HTTP protocol
  74. AVDictionary *opt_dict = NULL;
  75. // URI part variables
  76. char h_url[1024], host[1024], auth[1024], path[1024];
  77. char *headers = NULL, *user = NULL;
  78. int port, ret;
  79. AVBPrint bp;
  80. if (flags & AVIO_FLAG_READ)
  81. return AVERROR(ENOSYS);
  82. av_bprint_init(&bp, 0, 1);
  83. // Build header strings
  84. cat_header(&bp, "Ice-Name", s->name);
  85. cat_header(&bp, "Ice-Description", s->description);
  86. cat_header(&bp, "Ice-URL", s->url);
  87. cat_header(&bp, "Ice-Genre", s->genre);
  88. cat_header(&bp, "Ice-Public", s->public ? "1" : "0");
  89. if (!av_bprint_is_complete(&bp)) {
  90. ret = AVERROR(ENOMEM);
  91. goto cleanup;
  92. }
  93. av_bprint_finalize(&bp, &headers);
  94. // Set options
  95. av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
  96. av_dict_set(&opt_dict, "auth_type", "basic", 0);
  97. av_dict_set(&opt_dict, "headers", headers, 0);
  98. if (NOT_EMPTY(s->content_type))
  99. av_dict_set(&opt_dict, "content_type", s->content_type, 0);
  100. if (NOT_EMPTY(s->user_agent))
  101. av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
  102. // Parse URI
  103. av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
  104. &port, path, sizeof(path), uri);
  105. // Check for auth data in URI
  106. if (auth[0]) {
  107. char *sep = strchr(auth, ':');
  108. if (sep) {
  109. *sep = 0;
  110. sep++;
  111. if (s->pass) {
  112. av_free(s->pass);
  113. av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
  114. }
  115. if (!(s->pass = av_strdup(sep))) {
  116. ret = AVERROR(ENOMEM);
  117. goto cleanup;
  118. }
  119. }
  120. if (!(user = av_strdup(auth))) {
  121. ret = AVERROR(ENOMEM);
  122. goto cleanup;
  123. }
  124. }
  125. // Build new authstring
  126. snprintf(auth, sizeof(auth),
  127. "%s:%s",
  128. user ? user : DEFAULT_ICE_USER,
  129. s->pass ? s->pass : "");
  130. // Check for mountpoint (path)
  131. if (!path[0] || strcmp(path, "/") == 0) {
  132. av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
  133. ret = AVERROR(EIO);
  134. goto cleanup;
  135. }
  136. // Build new URI for passing to http protocol
  137. ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
  138. // Finally open http proto handler
  139. ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
  140. cleanup:
  141. av_freep(&user);
  142. av_freep(&headers);
  143. av_dict_free(&opt_dict);
  144. return ret;
  145. }
  146. static int icecast_write(URLContext *h, const uint8_t *buf, int size)
  147. {
  148. IcecastContext *s = h->priv_data;
  149. if (!s->send_started) {
  150. s->send_started = 1;
  151. if (!s->content_type && size >= 8) {
  152. static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
  153. static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
  154. static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
  155. if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
  156. av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n");
  157. av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
  158. } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
  159. av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n");
  160. av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
  161. } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
  162. av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n");
  163. av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
  164. } else {
  165. av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
  166. av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
  167. }
  168. }
  169. }
  170. return ffurl_write(s->hd, buf, size);
  171. }
  172. static const AVClass icecast_context_class = {
  173. .class_name = "icecast",
  174. .item_name = av_default_item_name,
  175. .option = options,
  176. .version = LIBAVUTIL_VERSION_INT,
  177. };
  178. URLProtocol ff_icecast_protocol = {
  179. .name = "icecast",
  180. .url_open = icecast_open,
  181. .url_write = icecast_write,
  182. .url_close = icecast_close,
  183. .priv_data_size = sizeof(IcecastContext),
  184. .priv_data_class = &icecast_context_class,
  185. .flags = URL_PROTOCOL_FLAG_NETWORK,
  186. };