2014-07-28 08:36:19 +00:00
|
|
|
/*
|
|
|
|
* Icecast protocol for FFmpeg
|
|
|
|
* Copyright (c) 2014 Marvin Scholz
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "libavutil/bprint.h"
|
2024-03-25 00:30:37 +00:00
|
|
|
#include "libavutil/mem.h"
|
2014-07-28 08:36:19 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct IcecastContext {
|
|
|
|
const AVClass *class;
|
|
|
|
URLContext *hd;
|
|
|
|
int send_started;
|
|
|
|
char *user;
|
|
|
|
// Options
|
|
|
|
char *content_type;
|
|
|
|
char *description;
|
|
|
|
char *genre;
|
|
|
|
int legacy_icecast;
|
|
|
|
char *name;
|
|
|
|
char *pass;
|
|
|
|
int public;
|
|
|
|
char *url;
|
|
|
|
char *user_agent;
|
2020-06-13 22:52:44 +00:00
|
|
|
int tls;
|
2014-07-28 08:36:19 +00:00
|
|
|
} IcecastContext;
|
|
|
|
|
|
|
|
#define DEFAULT_ICE_USER "source"
|
|
|
|
|
|
|
|
#define NOT_EMPTY(s) (s && s[0])
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(IcecastContext, x)
|
|
|
|
#define E AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
2015-10-31 13:43:48 +00:00
|
|
|
{ "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
|
|
|
{ "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
|
|
|
{ "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
|
|
|
{ "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
2015-11-21 21:05:07 +00:00
|
|
|
{ "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
|
2015-10-31 13:43:48 +00:00
|
|
|
{ "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
|
|
|
{ "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
|
|
|
{ "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
2015-11-21 21:05:07 +00:00
|
|
|
{ "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
|
2020-06-13 22:52:44 +00:00
|
|
|
{ "tls", "use a TLS connection", OFFSET(tls), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
|
2014-07-28 08:36:19 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void cat_header(AVBPrint *bp, const char key[], const char value[])
|
|
|
|
{
|
|
|
|
if (NOT_EMPTY(value))
|
|
|
|
av_bprintf(bp, "%s: %s\r\n", key, value);
|
|
|
|
}
|
|
|
|
|
2014-07-28 09:55:38 +00:00
|
|
|
static int icecast_close(URLContext *h)
|
|
|
|
{
|
|
|
|
IcecastContext *s = h->priv_data;
|
2020-04-03 15:03:38 +00:00
|
|
|
ffurl_closep(&s->hd);
|
2014-07-28 09:55:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-28 08:36:19 +00:00
|
|
|
static int icecast_open(URLContext *h, const char *uri, int flags)
|
|
|
|
{
|
|
|
|
IcecastContext *s = h->priv_data;
|
|
|
|
|
|
|
|
// Dict to set options that we pass to the HTTP protocol
|
|
|
|
AVDictionary *opt_dict = NULL;
|
|
|
|
|
|
|
|
// URI part variables
|
|
|
|
char h_url[1024], host[1024], auth[1024], path[1024];
|
2019-11-10 04:07:29 +00:00
|
|
|
char *headers, *user = NULL;
|
2014-07-28 08:36:19 +00:00
|
|
|
int port, ret;
|
|
|
|
AVBPrint bp;
|
|
|
|
|
2014-07-28 09:55:38 +00:00
|
|
|
if (flags & AVIO_FLAG_READ)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
|
2018-06-10 08:02:15 +00:00
|
|
|
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
2014-07-28 08:36:19 +00:00
|
|
|
|
|
|
|
// Build header strings
|
|
|
|
cat_header(&bp, "Ice-Name", s->name);
|
|
|
|
cat_header(&bp, "Ice-Description", s->description);
|
|
|
|
cat_header(&bp, "Ice-URL", s->url);
|
|
|
|
cat_header(&bp, "Ice-Genre", s->genre);
|
|
|
|
cat_header(&bp, "Ice-Public", s->public ? "1" : "0");
|
|
|
|
if (!av_bprint_is_complete(&bp)) {
|
2019-11-10 04:07:29 +00:00
|
|
|
av_bprint_finalize(&bp, NULL);
|
|
|
|
return AVERROR(ENOMEM);
|
2014-07-28 08:36:19 +00:00
|
|
|
}
|
2019-11-10 04:07:29 +00:00
|
|
|
if ((ret = av_bprint_finalize(&bp, &headers)) < 0)
|
|
|
|
return ret;
|
2014-07-28 08:36:19 +00:00
|
|
|
|
|
|
|
// Set options
|
|
|
|
av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
|
|
|
|
av_dict_set(&opt_dict, "auth_type", "basic", 0);
|
2019-11-10 04:07:30 +00:00
|
|
|
av_dict_set(&opt_dict, "headers", headers, AV_DICT_DONT_STRDUP_VAL);
|
2014-10-12 10:24:07 +00:00
|
|
|
av_dict_set(&opt_dict, "chunked_post", "0", 0);
|
2019-03-31 04:35:51 +00:00
|
|
|
av_dict_set(&opt_dict, "send_expect_100", s->legacy_icecast ? "-1" : "1", 0);
|
2014-07-28 08:36:19 +00:00
|
|
|
if (NOT_EMPTY(s->content_type))
|
|
|
|
av_dict_set(&opt_dict, "content_type", s->content_type, 0);
|
2014-11-11 00:51:23 +00:00
|
|
|
else
|
|
|
|
av_dict_set(&opt_dict, "content_type", "audio/mpeg", 0);
|
2014-07-28 08:36:19 +00:00
|
|
|
if (NOT_EMPTY(s->user_agent))
|
|
|
|
av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
|
|
|
|
|
|
|
|
// Parse URI
|
|
|
|
av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
|
|
|
|
&port, path, sizeof(path), uri);
|
|
|
|
|
|
|
|
// Check for auth data in URI
|
|
|
|
if (auth[0]) {
|
|
|
|
char *sep = strchr(auth, ':');
|
|
|
|
if (sep) {
|
|
|
|
*sep = 0;
|
|
|
|
sep++;
|
|
|
|
if (s->pass) {
|
|
|
|
av_free(s->pass);
|
|
|
|
av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
|
|
|
|
}
|
2014-07-28 09:55:38 +00:00
|
|
|
if (!(s->pass = av_strdup(sep))) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(user = av_strdup(auth))) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto cleanup;
|
2014-07-28 08:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build new authstring
|
|
|
|
snprintf(auth, sizeof(auth),
|
|
|
|
"%s:%s",
|
|
|
|
user ? user : DEFAULT_ICE_USER,
|
|
|
|
s->pass ? s->pass : "");
|
|
|
|
|
|
|
|
// Check for mountpoint (path)
|
|
|
|
if (!path[0] || strcmp(path, "/") == 0) {
|
|
|
|
av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build new URI for passing to http protocol
|
2020-06-13 22:52:44 +00:00
|
|
|
ff_url_join(h_url, sizeof(h_url),
|
|
|
|
s->tls ? "https" : "http",
|
|
|
|
auth, host, port, "%s", path);
|
2014-07-28 08:36:19 +00:00
|
|
|
// Finally open http proto handler
|
2016-01-30 01:17:51 +00:00
|
|
|
ret = ffurl_open_whitelist(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL,
|
2016-04-21 14:55:09 +00:00
|
|
|
&opt_dict, h->protocol_whitelist, h->protocol_blacklist, h);
|
2014-07-28 08:36:19 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
av_freep(&user);
|
|
|
|
av_dict_free(&opt_dict);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int icecast_write(URLContext *h, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
IcecastContext *s = h->priv_data;
|
|
|
|
if (!s->send_started) {
|
|
|
|
s->send_started = 1;
|
|
|
|
if (!s->content_type && size >= 8) {
|
|
|
|
static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
|
|
|
|
static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
|
|
|
|
static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
|
|
|
|
if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
|
2014-07-28 09:55:38 +00:00
|
|
|
av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n");
|
2014-07-28 08:36:19 +00:00
|
|
|
av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
|
|
|
|
} else if (memcmp(buf, opus, sizeof(opus)) == 0) {
|
2014-07-28 09:55:38 +00:00
|
|
|
av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n");
|
2014-07-28 08:36:19 +00:00
|
|
|
av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
|
|
|
|
} else if (memcmp(buf, webm, sizeof(webm)) == 0) {
|
2014-07-28 09:55:38 +00:00
|
|
|
av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n");
|
2014-07-28 08:36:19 +00:00
|
|
|
av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
|
|
|
|
} else {
|
|
|
|
av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
|
|
|
|
av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ffurl_write(s->hd, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass icecast_context_class = {
|
|
|
|
.class_name = "icecast",
|
2024-01-19 12:33:28 +00:00
|
|
|
.item_name = av_default_item_name,
|
2014-07-28 08:36:19 +00:00
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2016-02-19 09:39:29 +00:00
|
|
|
const URLProtocol ff_icecast_protocol = {
|
2014-07-28 08:36:19 +00:00
|
|
|
.name = "icecast",
|
|
|
|
.url_open = icecast_open,
|
|
|
|
.url_write = icecast_write,
|
|
|
|
.url_close = icecast_close,
|
|
|
|
.priv_data_size = sizeof(IcecastContext),
|
|
|
|
.priv_data_class = &icecast_context_class,
|
|
|
|
.flags = URL_PROTOCOL_FLAG_NETWORK,
|
|
|
|
};
|