Browse code

libavformat: Add Icecast protocol

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

ePirat authored on 2014/07/28 17:36:19
Showing 8 changed files
... ...
@@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release,
2 2
 releases are sorted from youngest to oldest.
3 3
 
4 4
 version <next>:
5
+- Icecast protocol
5 6
 
6 7
 
7 8
 version 2.3:
... ...
@@ -2484,6 +2484,7 @@ gopher_protocol_select="network"
2484 2484
 http_protocol_select="tcp_protocol"
2485 2485
 httpproxy_protocol_select="tcp_protocol"
2486 2486
 https_protocol_select="tls_protocol"
2487
+icecast_protocol_select="http"
2487 2488
 librtmp_protocol_deps="librtmp"
2488 2489
 librtmpe_protocol_deps="librtmp"
2489 2490
 librtmps_protocol_deps="librtmp"
... ...
@@ -1055,6 +1055,7 @@ performance on systems without hardware floating point support).
1055 1055
 @item HLS          @tab X
1056 1056
 @item HTTP         @tab X
1057 1057
 @item HTTPS        @tab X
1058
+@item Icecast      @tab X
1058 1059
 @item MMSH         @tab X
1059 1060
 @item MMST         @tab X
1060 1061
 @item pipe         @tab X
... ...
@@ -293,6 +293,48 @@ The required syntax to play a stream specifying a cookie is:
293 293
 ffplay -cookies "nlqptid=nltid=tsn; path=/; domain=somedomain.com;" http://somedomain.com/somestream.m3u8
294 294
 @end example
295 295
 
296
+@section Icecast
297
+
298
+Icecast protocol
299
+
300
+@table @option
301
+@item ice_genre
302
+Set the genre of the stream.
303
+
304
+@item ice_name
305
+Set the name of the stream.
306
+
307
+@item ice_description
308
+Set the description of the stream.
309
+
310
+@item ice_url
311
+Set the stream website url.
312
+
313
+@item ice_public
314
+Set if the stream should be public.
315
+Default is 0 (not public).
316
+
317
+@item ice_password
318
+Password for the mountpoint.
319
+
320
+@item legacy_icecast
321
+If set to 1, enable support for legacy Icecast (Version < 2.4), using the SOURCE method
322
+instead of the PUT method.
323
+
324
+@item content_type
325
+Set a specific content type for the stream.
326
+This MUST be set if streaming else than audio/mpeg
327
+
328
+@item user_agent
329
+Override the User-Agent header. If not specified the protocol will use a
330
+string describing the libavformat build. ("Lavf/<version>")
331
+
332
+@end table
333
+
334
+@example
335
+icecast://[@var{username}[:@var{password}]@@]@var{server}:@var{port}/@var{mountpoint}
336
+@end example
337
+
296 338
 @section mmst
297 339
 
298 340
 MMS (Microsoft Media Server) protocol over TCP.
... ...
@@ -485,6 +485,7 @@ OBJS-$(CONFIG_HLS_PROTOCOL)              += hlsproto.o
485 485
 OBJS-$(CONFIG_HTTP_PROTOCOL)             += http.o httpauth.o urldecode.o
486 486
 OBJS-$(CONFIG_HTTPPROXY_PROTOCOL)        += http.o httpauth.o urldecode.o
487 487
 OBJS-$(CONFIG_HTTPS_PROTOCOL)            += http.o httpauth.o urldecode.o
488
+OBJS-$(CONFIG_ICECAST_PROTOCOL)          += icecast.o
488 489
 OBJS-$(CONFIG_MMSH_PROTOCOL)             += mmsh.o mms.o asf.o
489 490
 OBJS-$(CONFIG_MMST_PROTOCOL)             += mmst.o mms.o asf.o
490 491
 OBJS-$(CONFIG_MD5_PROTOCOL)              += md5proto.o
... ...
@@ -347,6 +347,7 @@ void av_register_all(void)
347 347
     REGISTER_PROTOCOL(HTTP,             http);
348 348
     REGISTER_PROTOCOL(HTTPPROXY,        httpproxy);
349 349
     REGISTER_PROTOCOL(HTTPS,            https);
350
+    REGISTER_PROTOCOL(ICECAST,          icecast);
350 351
     REGISTER_PROTOCOL(MMSH,             mmsh);
351 352
     REGISTER_PROTOCOL(MMST,             mmst);
352 353
     REGISTER_PROTOCOL(MD5,              md5);
353 354
new file mode 100644
... ...
@@ -0,0 +1,206 @@
0
+/*
1
+ * Icecast protocol for FFmpeg
2
+ * Copyright (c) 2014 Marvin Scholz
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+
22
+#include "libavutil/avstring.h"
23
+#include "libavutil/bprint.h"
24
+#include "libavutil/opt.h"
25
+
26
+#include "avformat.h"
27
+#include "network.h"
28
+
29
+
30
+typedef struct IcecastContext {
31
+    const AVClass *class;
32
+    URLContext *hd;
33
+    int send_started;
34
+    char *user;
35
+    // Options
36
+    char *content_type;
37
+    char *description;
38
+    char *genre;
39
+    int legacy_icecast;
40
+    char *name;
41
+    char *pass;
42
+    int public;
43
+    char *url;
44
+    char *user_agent;
45
+} IcecastContext;
46
+
47
+#define DEFAULT_ICE_USER "source"
48
+
49
+#define NOT_EMPTY(s) (s && s[0])
50
+
51
+#define OFFSET(x) offsetof(IcecastContext, x)
52
+#define E AV_OPT_FLAG_ENCODING_PARAM
53
+
54
+static const AVOption options[] = {
55
+    { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
56
+    { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
57
+    { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
58
+    { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
59
+    { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
60
+    { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
61
+    { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
62
+    { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
63
+    { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
64
+    { NULL }
65
+};
66
+
67
+
68
+static void cat_header(AVBPrint *bp, const char key[], const char value[])
69
+{
70
+    if (NOT_EMPTY(value))
71
+        av_bprintf(bp, "%s: %s\r\n", key, value);
72
+}
73
+
74
+static int icecast_open(URLContext *h, const char *uri, int flags)
75
+{
76
+    IcecastContext *s = h->priv_data;
77
+
78
+    // Dict to set options that we pass to the HTTP protocol
79
+    AVDictionary *opt_dict = NULL;
80
+
81
+    // URI part variables
82
+    char h_url[1024], host[1024], auth[1024], path[1024];
83
+    char *user = NULL, *headers = NULL;
84
+    int port, ret;
85
+    AVBPrint bp;
86
+
87
+    av_bprint_init(&bp, 0, 1);
88
+
89
+    // Build header strings
90
+    cat_header(&bp, "Ice-Name", s->name);
91
+    cat_header(&bp, "Ice-Description", s->description);
92
+    cat_header(&bp, "Ice-URL", s->url);
93
+    cat_header(&bp, "Ice-Genre", s->genre);
94
+    cat_header(&bp, "Ice-Public", s->public ? "1" : "0");
95
+    if (!av_bprint_is_complete(&bp)) {
96
+        ret = AVERROR(ENOMEM);
97
+        goto cleanup;
98
+    }
99
+    av_bprint_finalize(&bp, &headers);
100
+
101
+    // Set options
102
+    av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
103
+    av_dict_set(&opt_dict, "auth_type", "basic", 0);
104
+    av_dict_set(&opt_dict, "headers", headers, 0);
105
+    if (NOT_EMPTY(s->content_type))
106
+        av_dict_set(&opt_dict, "content_type", s->content_type, 0);
107
+    if (NOT_EMPTY(s->user_agent))
108
+        av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
109
+
110
+    // Parse URI
111
+    av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
112
+                 &port, path, sizeof(path), uri);
113
+
114
+    // Check for auth data in URI
115
+    if (auth[0]) {
116
+        char *sep = strchr(auth, ':');
117
+        if (sep) {
118
+            *sep = 0;
119
+            sep++;
120
+            if (s->pass) {
121
+                av_free(s->pass);
122
+                av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
123
+            }
124
+            s->pass = av_strdup(sep);
125
+        }
126
+        user = av_strdup(auth);
127
+    }
128
+
129
+    // Build new authstring
130
+    snprintf(auth, sizeof(auth),
131
+             "%s:%s",
132
+             user ? user : DEFAULT_ICE_USER,
133
+             s->pass ? s->pass : "");
134
+
135
+    // Check for mountpoint (path)
136
+    if (!path[0] || strcmp(path, "/") == 0) {
137
+        av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
138
+        ret = AVERROR(EIO);
139
+        goto cleanup;
140
+    }
141
+
142
+    // Build new URI for passing to http protocol
143
+    ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
144
+    // Finally open http proto handler
145
+    ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
146
+
147
+cleanup:
148
+    av_freep(&user);
149
+    av_freep(&headers);
150
+    av_dict_free(&opt_dict);
151
+
152
+    return ret;
153
+}
154
+
155
+static int icecast_write(URLContext *h, const uint8_t *buf, int size)
156
+{
157
+    IcecastContext *s = h->priv_data;
158
+    if (!s->send_started) {
159
+        s->send_started = 1;
160
+        if (!s->content_type && size >= 8) {
161
+            static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
162
+            static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
163
+            static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
164
+            if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
165
+                av_log(h, AV_LOG_WARNING, "Streaming ogg but appropriate content type NOT set!\n");
166
+                av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
167
+            } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
168
+                av_log(h, AV_LOG_WARNING, "Streaming opus but appropriate content type NOT set!\n");
169
+                av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
170
+            } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
171
+                av_log(h, AV_LOG_WARNING, "Streaming webm but appropriate content type NOT set!\n");
172
+                av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
173
+            } else {
174
+                av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
175
+                av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
176
+            }
177
+        }
178
+    }
179
+    return ffurl_write(s->hd, buf, size);
180
+}
181
+
182
+static int icecast_close(URLContext *h)
183
+{
184
+    IcecastContext *s = h->priv_data;
185
+    if (s->hd)
186
+        ffurl_close(s->hd);
187
+    return 0;
188
+}
189
+
190
+static const AVClass icecast_context_class = {
191
+    .class_name     = "icecast",
192
+    .item_name      = av_default_item_name,
193
+    .option         = options,
194
+    .version        = LIBAVUTIL_VERSION_INT,
195
+};
196
+
197
+URLProtocol ff_icecast_protocol = {
198
+    .name            = "icecast",
199
+    .url_open        = icecast_open,
200
+    .url_write       = icecast_write,
201
+    .url_close       = icecast_close,
202
+    .priv_data_size  = sizeof(IcecastContext),
203
+    .priv_data_class = &icecast_context_class,
204
+    .flags           = URL_PROTOCOL_FLAG_NETWORK,
205
+};
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 55
33
-#define LIBAVFORMAT_VERSION_MINOR 50
33
+#define LIBAVFORMAT_VERSION_MINOR 51
34 34
 #define LIBAVFORMAT_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \