2 * Icecast protocol for FFmpeg
3 * Copyright (c) 2014 Marvin Scholz
5 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/avstring.h"
24 #include "libavutil/bprint.h"
25 #include "libavutil/opt.h"
31 typedef struct IcecastContext {
48 #define DEFAULT_ICE_USER "source"
50 #define NOT_EMPTY(s) (s && s[0])
52 #define OFFSET(x) offsetof(IcecastContext, x)
53 #define E AV_OPT_FLAG_ENCODING_PARAM
55 static const AVOption options[] = {
56 { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
57 { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
58 { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
59 { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
60 { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
61 { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
62 { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
63 { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
64 { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
69 static void cat_header(AVBPrint *bp, const char key[], const char value[])
72 av_bprintf(bp, "%s: %s\r\n", key, value);
75 static int icecast_open(URLContext *h, const char *uri, int flags)
77 IcecastContext *s = h->priv_data;
79 // Dict to set options that we pass to the HTTP protocol
80 AVDictionary *opt_dict = NULL;
83 char h_url[1024], host[1024], auth[1024], path[1024];
84 char *user = NULL, *headers = NULL;
88 av_bprint_init(&bp, 0, 1);
90 // Build header strings
91 cat_header(&bp, "Ice-Name", s->name);
92 cat_header(&bp, "Ice-Description", s->description);
93 cat_header(&bp, "Ice-URL", s->url);
94 cat_header(&bp, "Ice-Genre", s->genre);
95 cat_header(&bp, "Ice-Public", s->public ? "1" : "0");
96 if (!av_bprint_is_complete(&bp)) {
97 ret = AVERROR(ENOMEM);
100 av_bprint_finalize(&bp, &headers);
103 av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
104 av_dict_set(&opt_dict, "auth_type", "basic", 0);
105 av_dict_set(&opt_dict, "headers", headers, 0);
106 if (NOT_EMPTY(s->content_type))
107 av_dict_set(&opt_dict, "content_type", s->content_type, 0);
108 if (NOT_EMPTY(s->user_agent))
109 av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
112 av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
113 &port, path, sizeof(path), uri);
115 // Check for auth data in URI
117 char *sep = strchr(auth, ':');
123 av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
125 s->pass = av_strdup(sep);
127 user = av_strdup(auth);
130 // Build new authstring
131 snprintf(auth, sizeof(auth),
133 user ? user : DEFAULT_ICE_USER,
134 s->pass ? s->pass : "");
136 // Check for mountpoint (path)
137 if (!path[0] || strcmp(path, "/") == 0) {
138 av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
143 // Build new URI for passing to http protocol
144 ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
145 // Finally open http proto handler
146 ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
151 av_dict_free(&opt_dict);
156 static int icecast_write(URLContext *h, const uint8_t *buf, int size)
158 IcecastContext *s = h->priv_data;
159 if (!s->send_started) {
161 if (!s->content_type && size >= 8) {
162 static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
163 static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
164 static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
165 if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
166 av_log(h, AV_LOG_WARNING, "Streaming ogg but appropriate content type NOT set!\n");
167 av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
168 } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
169 av_log(h, AV_LOG_WARNING, "Streaming opus but appropriate content type NOT set!\n");
170 av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
171 } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
172 av_log(h, AV_LOG_WARNING, "Streaming webm but appropriate content type NOT set!\n");
173 av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
175 av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
176 av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
180 return ffurl_write(s->hd, buf, size);
183 static int icecast_close(URLContext *h)
185 IcecastContext *s = h->priv_data;
191 static const AVClass icecast_context_class = {
192 .class_name = "icecast",
193 .item_name = av_default_item_name,
195 .version = LIBAVUTIL_VERSION_INT,
198 URLProtocol ff_icecast_protocol = {
200 .url_open = icecast_open,
201 .url_write = icecast_write,
202 .url_close = icecast_close,
203 .priv_data_size = sizeof(IcecastContext),
204 .priv_data_class = &icecast_context_class,
205 .flags = URL_PROTOCOL_FLAG_NETWORK,