From: Marvin Scholz <[email protected]>
This adds the Icecast protocol to libav, this is basically a convenience wrapper
for the http protocol
---
libavformat/icecast.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 230 insertions(+)
create mode 100644 libavformat/icecast.c
diff --git a/libavformat/icecast.c b/libavformat/icecast.c
new file mode 100644
index 0000000..d9ba846
--- /dev/null
+++ b/libavformat/icecast.c
@@ -0,0 +1,230 @@
+/*
+ * Icecast protocol for avconv client
+ * Copyright (c) 2000, 2001 Fabrice Bellard
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avstring.h"
+#include "avformat.h"
+#include "internal.h"
+#include "network.h"
+#include "http.h"
+#include "os_support.h"
+#include "httpauth.h"
+#include "url.h"
+#include "libavutil/opt.h"
+
+
+typedef struct {
+ const AVClass *class;
+ URLContext *hd;
+ char *content_type;
+ char *description;
+ char *genre;
+ char *headers;
+ int *legacy_icecast;
+ char *name;
+ char *pass;
+ int *public;
+ char *url;
+ char *user;
+ char *user_agent;
+ int send_started;
+} IcecastContext;
+
+#define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
+#define DEFAULT_ICE_USER "source"
+
+#define EMPTY(s) (s[0] == '\0')
+
+#define OFFSET(x) offsetof(IcecastContext, x)
+#define D AV_OPT_FLAG_DECODING_PARAM
+#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, {.str = DEFAULT_USER_AGENT}, 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 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 = (char*) av_realloc(buf, len * sizeof(char))))
+ return NULL;
+ if (is_first)
+ *buf = '\0';
+
+ av_strlcatf(buf, len, "%s: %s\r\n", key, value);
+ return buf;
+}
+
+static int comp_head(const uint8_t *haystack, int hay_size, const uint8_t
*needle, int ne_size)
+{
+ if (ne_size > hay_size)
+ return -1;
+ for (int b = 0; b < ne_size; b++) {
+ if (haystack[b] != needle[b]){
+ return -1;
+ }
+ }
+ return 0;
+}
+
+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 HTTP protocol
+ AVDictionary *opt_dict = NULL;
+
+ // Build header strings
+ if (s->name && !EMPTY(s->name))
+ s->headers = cat_header(s->headers, "Ice-Name", s->name);
+ if (s->description && !EMPTY(s->description))
+ s->headers = cat_header(s->headers, "Ice-Description", s->description);
+ if (s->url && !EMPTY(s->url))
+ s->headers = cat_header(s->headers, "Ice-URL", s->url);
+ if (s->genre && !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");
+
+ // Set options
+ av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
+ av_dict_set(&opt_dict, "auth_type", "basic", 0);
+ if (s->headers && !EMPTY(s->headers))
+ av_dict_set(&opt_dict, "headers", s->headers, 0);
+ if (s->content_type && !EMPTY(s->content_type))
+ av_dict_set(&opt_dict, "content_type", s->content_type, 0);
+
+ // URI part variables
+ char h_url[1024];
+ char host[1024];
+ char auth[1024];
+ char path[1024];
+ int port;
+
+ // 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'){
+ char *p = strchr(auth, ':');
+ if (p){
+ // Setting user and pass from URI
+ if (s->user != NULL) av_free(s->user);
+ int user_len = p + 1 - auth;
+ s->user = (char*) av_malloc(sizeof(char) * user_len);
+ 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");
+ }
+ s->pass = (char*) av_malloc(sizeof(char) * (int)(auth - user_len));
+ av_strlcpy(s->pass, p, (int)(auth - user_len));
+ } else {
+ // Setting user from URI
+ if (s->user != NULL) av_free(s->user);
+ s->user = (char*) av_malloc(sizeof(char) * strlen(auth));
+ av_strlcpy(s->user, auth, (int) 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 (EMPTY(path) || 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);
+
+ // 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};
+ if (comp_head(buf, size, 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");
+ } else if (comp_head(buf, size, opus, sizeof(opus)) == 0){
+ av_log(h, AV_LOG_WARNING, "Streaming opus but appropriated
content type NOT set!\n");
+ av_log(h, AV_LOG_WARNING, "Set it with -content_type
audio/ogg\n");
+ } else if (comp_head(buf, size, webm, sizeof(webm)) == 0){
+ av_log(h, AV_LOG_WARNING, "Streaming webm but appropriated
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 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,
+};
--
1.8.5.2 (Apple Git-48)
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel