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

Michael Niedermayer michaelni at gmx.at
Wed Jul 23 03:06:18 CEST 2014


On Wed, Jul 23, 2014 at 02:12:16AM +0200, epirat07 at gmail.com wrote:
> From: ePirat <epirat07 at gmail.com>
> 
> Fixed Makefile and micro version
> 
> ---
> 
> Add Icecast protocol, a convenience wrapper for the HTTP protocol 
> 
> ---
>  Changelog                |   1 +
>  configure                |   1 +
>  doc/general.texi         |   1 +
>  doc/protocols.texi       |  42 +++++++++
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/icecast.c    | 230 +++++++++++++++++++++++++++++++++++++++++++++++
>  libavformat/version.h    |   4 +-
>  8 files changed, 279 insertions(+), 2 deletions(-)
>  create mode 100644 libavformat/icecast.c
> 
> diff --git a/Changelog b/Changelog
> index 07cf1cf..54bb6ba 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release,
>  releases are sorted from youngest to oldest.
>  
>  version 2.3:
> +- Icecast protocol
>  - AC3 fixed-point decoding

Its not in 2.3, please add it to version <next>:


[...]
> diff --git a/libavformat/icecast.c b/libavformat/icecast.c
> new file mode 100644
> index 0000000..700bd97
> --- /dev/null
> +++ b/libavformat/icecast.c
> @@ -0,0 +1,230 @@
> +/*
> + * Icecast Protocol Handler
> + * 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
> + */
> +
> +/**
> + * @file
> + * Icecast Protocol Handler
> + * http://icecast.org
> + */
> +
> +
> +#include "libavutil/avstring.h"
> +#include "libavutil/opt.h"
> +
> +#include "avformat.h"
> +#include "network.h"
> +
> +
> +typedef struct IcecastContext {
> +    const AVClass *class;
> +    URLContext *hd;
> +    char *content_type;
> +    char *description;
> +    char *genre;
> +    char *headers;
> +    int legacy_icecast;
> +    char *name;
> +    char *pass;
> +    int public;
> +    int send_started;
> +    char *url;
> +    char *user;
> +    char *user_agent;
> +} IcecastContext;
> +
> +#define DEFAULT_ICE_USER "source"
> +
> +#define NOT_EMPTY(s) (s && s[0] != '\0')
> +
> +#define OFFSET(x) offsetof(IcecastContext, x)

> +#define D AV_OPT_FLAG_DECODING_PARAM

unused


> +#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 },
> +    { "ice_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 },

the string ones should be {.str = NULL}


> +    { NULL }
> +};
> +
> +
> +static char *cat_header(char buf[], const char key[], const char value[])
> +{
> +    int len = strlen(key) + strlen(value) + 5;
> +    int is_first = !buf;
> +
> +    if (buf)
> +        len += strlen(buf);

> +    if (!(buf = av_realloc(buf, len)))
> +        return NULL;

this leaks on error, as the caller doesnt seem to free it

also using libavutil/bprint.h may be simpler
or maybe AV_DICT_APPEND could be used


> +    if (is_first)
> +        *buf = '\0';
> +
> +    av_strlcatf(buf, len, "%s: %s\r\n", key, value);
> +    return buf;
> +}
> +
> +static int icecast_close(URLContext *h)
> +{
> +    IcecastContext *s = h->priv_data;
> +    if (s->hd)
> +        ffurl_close(s->hd);
> +    return 0;
> +}
> +
> +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];
> +    int port;
> +
> +    // Build header strings
> +    if (NOT_EMPTY(s->name))
> +        s->headers = cat_header(s->headers, "Ice-Name", s->name);
> +    if (NOT_EMPTY(s->description))
> +        s->headers = cat_header(s->headers, "Ice-Description", s->description);
> +    if (NOT_EMPTY(s->url))
> +        s->headers = cat_header(s->headers, "Ice-URL", s->url);
> +    if (NOT_EMPTY(s->genre))
> +        s->headers = cat_header(s->headers, "Ice-Genre", s->genre);

> +    if (s->public)
> +        s->headers = cat_header(s->headers, "Ice-Public", "1");
> +    else
> +        s->headers = cat_header(s->headers, "Ice-Public", "0");

you use ? : below, but not here

s->headers = cat_header(s->headers, "Ice-Public", s->public ? "1" : "0");


> +
> +    // Set options
> +    av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
> +    av_dict_set(&opt_dict, "auth_type", "basic", 0);
> +    if (NOT_EMPTY(s->headers))
> +        av_dict_set(&opt_dict, "headers", s->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] != '\0') {
> +        int user_len;
> +        char *p = strchr(auth, ':');
> +        if (p) {
> +            // Setting user and pass from URI
> +            user_len = p - auth + 1;
> +            if (!(s->user = av_malloc(user_len)))
> +                return AVERROR(ENOMEM);
> +            av_strlcpy(s->user, auth, user_len);
> +            p++;
> +            if (s->pass != NULL) {
> +                av_free(s->pass);
> +                av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
> +            }
> +            if (!(s->pass = av_malloc(strlen(auth) - user_len))) {
> +                av_free(s->user);
> +                return AVERROR(ENOMEM);
> +            }
> +            av_strlcpy(s->pass, p, strlen(auth) - user_len);
> +        } else {
> +            // Setting user from URI
> +            if (!(s->user = av_malloc(strlen(auth))))
> +                return AVERROR(ENOMEM);
> +            av_strlcpy(s->user, auth, strlen(auth));
> +        }
> +    }
> +
> +    // Build new authstring
> +    snprintf(auth, sizeof(auth),
> +             "%s:%s",
> +             s->user ? s->user : DEFAULT_ICE_USER,
> +             s->pass ? s->pass : "");
> +
> +    // Check for mountpoint (path)
> +    if (path[0] == '\0' || strcmp(path, "/") == 0) {
> +        av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
> +        return AVERROR(EIO);
> +    }
> +
> +    // Build new URI for passing to http protocol
> +    ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
> +

> +    // Free variables
> +    av_free(s->user);

av_freep() is safer, as it leaves no stale pointer, some for the
other av_free() unless the variable is overwritten immedeatly
afterwards


> +
> +    // Finally open http proto handler
> +    return ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
> +}
> +
> +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) {

> +            const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
> +            const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
> +            const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };

static const to avoid the compiler building these arrays on the stack
for every call (some comipilers might do it others might be smart
enough to optimize it out)


> +            if (memcmp(buf, oggs, sizeof(oggs)) == 0) {

> +                av_log(h, AV_LOG_WARNING, "Streaming ogg but appropriated content type NOT set!\n");
> +                av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");

one call is enough to print 2 lines


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The worst form of inequality is to try to make unequal things equal.
-- Aristotle
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140723/2a3f7d69/attachment.asc>


More information about the ffmpeg-devel mailing list