[FFmpeg-devel] [PATCH] libavformat/icecast.c Add Icecast protocol

epirat07 at gmail.com epirat07 at gmail.com
Mon Jul 28 10:36:19 CEST 2014


From: ePirat <epirat07 at gmail.com>

Fix header.
---
Add Icecast protocol, a convenience wrapper for the HTTP protocol
---
 Changelog                |   3 +
 configure                |   1 +
 doc/general.texi         |   1 +
 doc/protocols.texi       |  42 ++++++++++
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/icecast.c    | 209 +++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   4 +-
 8 files changed, 260 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/icecast.c

diff --git a/Changelog b/Changelog
index 07cf1cf..821525e 100644
--- a/Changelog
+++ b/Changelog
@@ -1,6 +1,9 @@
 Entries are sorted chronologically from oldest to youngest within each release,
 releases are sorted from youngest to oldest.
 
+<next>:
+- Icecast protocol
+
 version 2.3:
 - AC3 fixed-point decoding
 - shuffleplanes filter
diff --git a/configure b/configure
index b4ec1e6..a0b2354 100755
--- a/configure
+++ b/configure
@@ -2483,6 +2483,7 @@ gopher_protocol_select="network"
 http_protocol_select="tcp_protocol"
 httpproxy_protocol_select="tcp_protocol"
 https_protocol_select="tls_protocol"
+icecast_protocol_select="http"
 librtmp_protocol_deps="librtmp"
 librtmpe_protocol_deps="librtmp"
 librtmps_protocol_deps="librtmp"
diff --git a/doc/general.texi b/doc/general.texi
index 35db917..9ce0b31 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -1055,6 +1055,7 @@ performance on systems without hardware floating point support).
 @item HLS          @tab X
 @item HTTP         @tab X
 @item HTTPS        @tab X
+ at item Icecast      @tab X
 @item MMSH         @tab X
 @item MMST         @tab X
 @item pipe         @tab X
diff --git a/doc/protocols.texi b/doc/protocols.texi
index 1cd96cc..cb75d92 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -293,6 +293,48 @@ The required syntax to play a stream specifying a cookie is:
 ffplay -cookies "nlqptid=nltid=tsn; path=/; domain=somedomain.com;" http://somedomain.com/somestream.m3u8
 @end example
 
+ at section Icecast
+
+Icecast protocol
+
+ at table @option
+ at item ice_genre
+Set the genre of the stream.
+
+ at item ice_name
+Set the name of the stream.
+
+ at item ice_description
+Set the description of the stream.
+
+ at item ice_url
+Set the stream website url.
+
+ at item ice_public
+Set if the stream should be public.
+Default is 0 (not public).
+
+ at item ice_password
+Password for the mountpoint.
+
+ at item legacy_icecast
+If set to 1, enable support for legacy Icecast (Version < 2.4), using the SOURCE method
+instead of the PUT method.
+
+ at item content_type
+Set a specific content type for the stream.
+This MUST be set if streaming else than audio/mpeg
+
+ at item user_agent
+Override the User-Agent header. If not specified the protocol will use a
+string describing the libavformat build. ("Lavf/<version>")
+
+ at end table
+
+ at example
+icecast://[@var{username}[:@var{password}]@@]@var{server}:@var{port}/@var{mountpoint}
+ at end example
+
 @section mmst
 
 MMS (Microsoft Media Server) protocol over TCP.
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 9017e06..d2d0d87 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -484,6 +484,7 @@ OBJS-$(CONFIG_HLS_PROTOCOL)              += hlsproto.o
 OBJS-$(CONFIG_HTTP_PROTOCOL)             += http.o httpauth.o urldecode.o
 OBJS-$(CONFIG_HTTPPROXY_PROTOCOL)        += http.o httpauth.o urldecode.o
 OBJS-$(CONFIG_HTTPS_PROTOCOL)            += http.o httpauth.o urldecode.o
+OBJS-$(CONFIG_ICECAST_PROTOCOL)          += icecast.o
 OBJS-$(CONFIG_MMSH_PROTOCOL)             += mmsh.o mms.o asf.o
 OBJS-$(CONFIG_MMST_PROTOCOL)             += mmst.o mms.o asf.o
 OBJS-$(CONFIG_MD5_PROTOCOL)              += md5proto.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 00de817..5599bae 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -347,6 +347,7 @@ void av_register_all(void)
     REGISTER_PROTOCOL(HTTP,             http);
     REGISTER_PROTOCOL(HTTPPROXY,        httpproxy);
     REGISTER_PROTOCOL(HTTPS,            https);
+    REGISTER_PROTOCOL(ICECAST,          icecast);
     REGISTER_PROTOCOL(MMSH,             mmsh);
     REGISTER_PROTOCOL(MMST,             mmst);
     REGISTER_PROTOCOL(MD5,              md5);
diff --git a/libavformat/icecast.c b/libavformat/icecast.c
new file mode 100644
index 0000000..77781b2
--- /dev/null
+++ b/libavformat/icecast.c
@@ -0,0 +1,209 @@
+/*
+ * Icecast protocol for Libav
+ * 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/avstring.h"
+#include "libavutil/bprint.h"
+#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;
+} 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[] = {
+    { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
+    { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
+    { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
+    { 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);
+}
+
+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];
+    char *user = NULL, *headers = NULL;
+    int port, ret;
+    struct AVBPrint bp;
+
+    av_bprint_init(&bp, 0, 1);
+
+    // 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)) {
+        ret = AVERROR(ENOMEM);
+        goto cleanup;
+    }
+    av_bprint_finalize(&bp, &headers);
+
+    // Set options
+    av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
+    av_dict_set(&opt_dict, "auth_type", "basic", 0);
+    av_dict_set(&opt_dict, "headers", headers, 0);
+    if (NOT_EMPTY(s->content_type))
+        av_dict_set(&opt_dict, "content_type", s->content_type, 0);
+    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");
+            }
+            s->pass = av_strdup(sep);
+        }
+        user = av_strdup(auth);
+    }
+
+    // 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
+    ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
+    // Finally open http proto handler
+    ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
+
+cleanup:
+    // Free variables
+    if (user)
+        av_freep(&user);
+    if (headers)
+        av_freep(&headers);
+    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) {
+                av_log(h, AV_LOG_WARNING, "Streaming ogg but appropriate content type NOT set!\n");
+                av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
+            } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
+                av_log(h, AV_LOG_WARNING, "Streaming opus but appropriate content type NOT set!\n");
+                av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
+            } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
+                av_log(h, AV_LOG_WARNING, "Streaming webm but appropriate content type NOT set!\n");
+                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 int icecast_close(URLContext *h)
+{
+    IcecastContext *s = h->priv_data;
+    if (s->hd)
+        ffurl_close(s->hd);
+    return 0;
+}
+
+static const AVClass icecast_context_class = {
+    .class_name     = "icecast",
+    .item_name      = av_default_item_name,
+    .option         = options,
+    .version        = LIBAVUTIL_VERSION_INT,
+};
+
+URLProtocol ff_icecast_protocol = {
+    .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,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 4e28112..5ad2338 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,8 +30,8 @@
 #include "libavutil/version.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 55
-#define LIBAVFORMAT_VERSION_MINOR 48
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MINOR 49
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.0.1



More information about the ffmpeg-devel mailing list