| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Icecast protocol for FFmpeg | ||
| 3 | * Copyright (c) 2014 Marvin Scholz | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg 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. | ||
| 11 | * | ||
| 12 | * FFmpeg 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. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | |||
| 23 | #include "libavutil/bprint.h" | ||
| 24 | #include "libavutil/mem.h" | ||
| 25 | #include "libavutil/opt.h" | ||
| 26 | |||
| 27 | #include "avformat.h" | ||
| 28 | #include "network.h" | ||
| 29 | |||
| 30 | |||
| 31 | typedef struct IcecastContext { | ||
| 32 | const AVClass *class; | ||
| 33 | URLContext *hd; | ||
| 34 | int send_started; | ||
| 35 | char *user; | ||
| 36 | // Options | ||
| 37 | char *content_type; | ||
| 38 | char *description; | ||
| 39 | char *genre; | ||
| 40 | int legacy_icecast; | ||
| 41 | char *name; | ||
| 42 | char *pass; | ||
| 43 | int public; | ||
| 44 | char *url; | ||
| 45 | char *user_agent; | ||
| 46 | int tls; | ||
| 47 | } IcecastContext; | ||
| 48 | |||
| 49 | #define DEFAULT_ICE_USER "source" | ||
| 50 | |||
| 51 | #define NOT_EMPTY(s) (s && s[0]) | ||
| 52 | |||
| 53 | #define OFFSET(x) offsetof(IcecastContext, x) | ||
| 54 | #define E AV_OPT_FLAG_ENCODING_PARAM | ||
| 55 | |||
| 56 | static const AVOption options[] = { | ||
| 57 | { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, | ||
| 58 | { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, | ||
| 59 | { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, | ||
| 60 | { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, | ||
| 61 | { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, | ||
| 62 | { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, | ||
| 63 | { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, | ||
| 64 | { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, | ||
| 65 | { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, | ||
| 66 | { "tls", "use a TLS connection", OFFSET(tls), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, | ||
| 67 | { NULL } | ||
| 68 | }; | ||
| 69 | |||
| 70 | |||
| 71 | ✗ | static void cat_header(AVBPrint *bp, const char key[], const char value[]) | |
| 72 | { | ||
| 73 | ✗ | if (NOT_EMPTY(value)) | |
| 74 | ✗ | av_bprintf(bp, "%s: %s\r\n", key, value); | |
| 75 | ✗ | } | |
| 76 | |||
| 77 | ✗ | static int icecast_close(URLContext *h) | |
| 78 | { | ||
| 79 | ✗ | IcecastContext *s = h->priv_data; | |
| 80 | ✗ | ffurl_closep(&s->hd); | |
| 81 | ✗ | return 0; | |
| 82 | } | ||
| 83 | |||
| 84 | ✗ | static int icecast_open(URLContext *h, const char *uri, int flags) | |
| 85 | { | ||
| 86 | ✗ | IcecastContext *s = h->priv_data; | |
| 87 | |||
| 88 | // Dict to set options that we pass to the HTTP protocol | ||
| 89 | ✗ | AVDictionary *opt_dict = NULL; | |
| 90 | |||
| 91 | // URI part variables | ||
| 92 | char h_url[1024], host[1024], auth[1024], path[1024]; | ||
| 93 | ✗ | char *headers, *user = NULL; | |
| 94 | int port, ret; | ||
| 95 | AVBPrint bp; | ||
| 96 | |||
| 97 | ✗ | if (flags & AVIO_FLAG_READ) | |
| 98 | ✗ | return AVERROR(ENOSYS); | |
| 99 | |||
| 100 | ✗ | av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC); | |
| 101 | |||
| 102 | // Build header strings | ||
| 103 | ✗ | cat_header(&bp, "Ice-Name", s->name); | |
| 104 | ✗ | cat_header(&bp, "Ice-Description", s->description); | |
| 105 | ✗ | cat_header(&bp, "Ice-URL", s->url); | |
| 106 | ✗ | cat_header(&bp, "Ice-Genre", s->genre); | |
| 107 | ✗ | cat_header(&bp, "Ice-Public", s->public ? "1" : "0"); | |
| 108 | ✗ | if (!av_bprint_is_complete(&bp)) { | |
| 109 | ✗ | av_bprint_finalize(&bp, NULL); | |
| 110 | ✗ | return AVERROR(ENOMEM); | |
| 111 | } | ||
| 112 | ✗ | if ((ret = av_bprint_finalize(&bp, &headers)) < 0) | |
| 113 | ✗ | return ret; | |
| 114 | |||
| 115 | // Set options | ||
| 116 | ✗ | av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0); | |
| 117 | ✗ | av_dict_set(&opt_dict, "auth_type", "basic", 0); | |
| 118 | ✗ | av_dict_set(&opt_dict, "headers", headers, AV_DICT_DONT_STRDUP_VAL); | |
| 119 | ✗ | av_dict_set(&opt_dict, "chunked_post", "0", 0); | |
| 120 | ✗ | av_dict_set(&opt_dict, "send_expect_100", s->legacy_icecast ? "-1" : "1", 0); | |
| 121 | ✗ | if (NOT_EMPTY(s->content_type)) | |
| 122 | ✗ | av_dict_set(&opt_dict, "content_type", s->content_type, 0); | |
| 123 | else | ||
| 124 | ✗ | av_dict_set(&opt_dict, "content_type", "audio/mpeg", 0); | |
| 125 | ✗ | if (NOT_EMPTY(s->user_agent)) | |
| 126 | ✗ | av_dict_set(&opt_dict, "user_agent", s->user_agent, 0); | |
| 127 | |||
| 128 | // Parse URI | ||
| 129 | ✗ | av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host), | |
| 130 | &port, path, sizeof(path), uri); | ||
| 131 | |||
| 132 | // Check for auth data in URI | ||
| 133 | ✗ | if (auth[0]) { | |
| 134 | ✗ | char *sep = strchr(auth, ':'); | |
| 135 | ✗ | if (sep) { | |
| 136 | ✗ | *sep = 0; | |
| 137 | ✗ | sep++; | |
| 138 | ✗ | if (s->pass) { | |
| 139 | ✗ | av_free(s->pass); | |
| 140 | ✗ | av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n"); | |
| 141 | } | ||
| 142 | ✗ | if (!(s->pass = av_strdup(sep))) { | |
| 143 | ✗ | ret = AVERROR(ENOMEM); | |
| 144 | ✗ | goto cleanup; | |
| 145 | } | ||
| 146 | } | ||
| 147 | ✗ | if (!(user = av_strdup(auth))) { | |
| 148 | ✗ | ret = AVERROR(ENOMEM); | |
| 149 | ✗ | goto cleanup; | |
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | // Build new authstring | ||
| 154 | ✗ | snprintf(auth, sizeof(auth), | |
| 155 | "%s:%s", | ||
| 156 | ✗ | user ? user : DEFAULT_ICE_USER, | |
| 157 | ✗ | s->pass ? s->pass : ""); | |
| 158 | |||
| 159 | // Check for mountpoint (path) | ||
| 160 | ✗ | if (!path[0] || strcmp(path, "/") == 0) { | |
| 161 | ✗ | av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n"); | |
| 162 | ✗ | ret = AVERROR(EIO); | |
| 163 | ✗ | goto cleanup; | |
| 164 | } | ||
| 165 | |||
| 166 | // Build new URI for passing to http protocol | ||
| 167 | ✗ | ff_url_join(h_url, sizeof(h_url), | |
| 168 | ✗ | s->tls ? "https" : "http", | |
| 169 | auth, host, port, "%s", path); | ||
| 170 | // Finally open http proto handler | ||
| 171 | ✗ | ret = ffurl_open_whitelist(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, | |
| 172 | &opt_dict, h->protocol_whitelist, h->protocol_blacklist, h); | ||
| 173 | |||
| 174 | ✗ | cleanup: | |
| 175 | ✗ | av_freep(&user); | |
| 176 | ✗ | av_dict_free(&opt_dict); | |
| 177 | |||
| 178 | ✗ | return ret; | |
| 179 | } | ||
| 180 | |||
| 181 | ✗ | static int icecast_write(URLContext *h, const uint8_t *buf, int size) | |
| 182 | { | ||
| 183 | ✗ | IcecastContext *s = h->priv_data; | |
| 184 | ✗ | if (!s->send_started) { | |
| 185 | ✗ | s->send_started = 1; | |
| 186 | ✗ | if (!s->content_type && size >= 8) { | |
| 187 | static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 }; | ||
| 188 | static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 }; | ||
| 189 | static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 }; | ||
| 190 | ✗ | if (memcmp(buf, oggs, sizeof(oggs)) == 0) { | |
| 191 | ✗ | av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n"); | |
| 192 | ✗ | av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n"); | |
| 193 | ✗ | } else if (memcmp(buf, opus, sizeof(opus)) == 0) { | |
| 194 | ✗ | av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n"); | |
| 195 | ✗ | av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n"); | |
| 196 | ✗ | } else if (memcmp(buf, webm, sizeof(webm)) == 0) { | |
| 197 | ✗ | av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n"); | |
| 198 | ✗ | av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n"); | |
| 199 | } else { | ||
| 200 | ✗ | av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n"); | |
| 201 | ✗ | av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n"); | |
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | ✗ | return ffurl_write(s->hd, buf, size); | |
| 206 | } | ||
| 207 | |||
| 208 | static const AVClass icecast_context_class = { | ||
| 209 | .class_name = "icecast", | ||
| 210 | .item_name = av_default_item_name, | ||
| 211 | .option = options, | ||
| 212 | .version = LIBAVUTIL_VERSION_INT, | ||
| 213 | }; | ||
| 214 | |||
| 215 | const URLProtocol ff_icecast_protocol = { | ||
| 216 | .name = "icecast", | ||
| 217 | .url_open = icecast_open, | ||
| 218 | .url_write = icecast_write, | ||
| 219 | .url_close = icecast_close, | ||
| 220 | .priv_data_size = sizeof(IcecastContext), | ||
| 221 | .priv_data_class = &icecast_context_class, | ||
| 222 | .flags = URL_PROTOCOL_FLAG_NETWORK, | ||
| 223 | }; | ||
| 224 |