2 * Icecast protocol for Libav
3 * Copyright (c) 2014 Marvin Scholz
5 * This file is part of Libav.
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.
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.
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
23 #include "libavutil/avstring.h"
24 #include "libavutil/opt.h"
30 typedef struct IcecastContext {
47 #define DEFAULT_ICE_USER "source"
49 #define NOT_EMPTY(s) (s && s[0])
51 #define OFFSET(x) offsetof(IcecastContext, x)
52 #define E AV_OPT_FLAG_ENCODING_PARAM
54 static const AVOption options[] = {
55 { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
56 { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
57 { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
58 { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
59 { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
60 { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
61 { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
62 { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
63 { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
68 static char *cat_header(char buf[], const char key[], const char value[])
70 if (NOT_EMPTY(value)) {
71 int len = strlen(key) + strlen(value) + 5;
77 if (!(tmp = av_realloc(buf, len))) {
86 av_strlcatf(buf, len, "%s: %s\r\n", key, value);
91 static int icecast_close(URLContext *h)
93 IcecastContext *s = h->priv_data;
99 static int icecast_open(URLContext *h, const char *uri, int flags)
101 IcecastContext *s = h->priv_data;
103 // Dict to set options that we pass to the HTTP protocol
104 AVDictionary *opt_dict = NULL;
106 // URI part variables
107 char h_url[1024], host[1024], auth[1024], path[1024];
108 char *headers = NULL, *user = NULL;
111 if (flags & AVIO_FLAG_READ)
112 return AVERROR(ENOSYS);
114 // Build header strings
115 headers = cat_header(headers, "Ice-Name", s->name);
116 headers = cat_header(headers, "Ice-Description", s->description);
117 headers = cat_header(headers, "Ice-URL", s->url);
118 headers = cat_header(headers, "Ice-Genre", s->genre);
119 headers = cat_header(headers, "Ice-Public", s->public ? "1" : "0");
121 ret = AVERROR(ENOMEM);
126 av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
127 av_dict_set(&opt_dict, "auth_type", "basic", 0);
128 av_dict_set(&opt_dict, "headers", headers, 0);
129 av_dict_set(&opt_dict, "chunked_post", "0", 0);
130 av_dict_set(&opt_dict, "send_expect_100", s->legacy_icecast ? "0" : "1", 0);
131 if (NOT_EMPTY(s->content_type))
132 av_dict_set(&opt_dict, "content_type", s->content_type, 0);
134 av_dict_set(&opt_dict, "content_type", "audio/mpeg", 0);
135 if (NOT_EMPTY(s->user_agent))
136 av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
139 av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
140 &port, path, sizeof(path), uri);
142 // Check for auth data in URI
144 char *sep = strchr(auth,':');
150 av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
152 if (!(s->pass = av_strdup(sep))) {
153 ret = AVERROR(ENOMEM);
157 if (!(user = av_strdup(auth))) {
158 ret = AVERROR(ENOMEM);
163 // Build new authstring
164 snprintf(auth, sizeof(auth),
166 user ? user : DEFAULT_ICE_USER,
167 s->pass ? s->pass : "");
169 // Check for mountpoint (path)
170 if (!path[0] || strcmp(path, "/") == 0) {
171 av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
176 // Build new URI for passing to http protocol
177 ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
178 // Finally open http proto handler
179 ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
185 av_dict_free(&opt_dict);
190 static int icecast_write(URLContext *h, const uint8_t *buf, int size)
192 IcecastContext *s = h->priv_data;
193 if (!s->send_started) {
195 if (!s->content_type && size >= 8) {
196 static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
197 static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
198 static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
199 if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
200 av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n");
201 av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
202 } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
203 av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n");
204 av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
205 } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
206 av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n");
207 av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
209 av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
210 av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
214 return ffurl_write(s->hd, buf, size);
217 static const AVClass icecast_context_class = {
218 .class_name = "icecast",
219 .item_name = av_default_item_name,
221 .version = LIBAVUTIL_VERSION_INT,
224 URLProtocol ff_icecast_protocol = {
226 .url_open = icecast_open,
227 .url_write = icecast_write,
228 .url_close = icecast_close,
229 .priv_data_size = sizeof(IcecastContext),
230 .priv_data_class = &icecast_context_class,
231 .flags = URL_PROTOCOL_FLAG_NETWORK,