]> git.sesse.net Git - ffmpeg/blob - libavformat/icecast.c
mov: fix assigment check
[ffmpeg] / libavformat / icecast.c
1 /*
2  * Icecast protocol for Libav
3  * Copyright (c) 2014 Marvin Scholz
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/avstring.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 char *cat_header(char buf[], const char key[], const char value[])
69 {
70     if (NOT_EMPTY(value)) {
71         int len = strlen(key) + strlen(value) + 5;
72         int is_first = !buf;
73         char *tmp = NULL;
74
75         if (buf)
76             len += strlen(buf);
77         if (!(tmp = av_realloc(buf, len))) {
78             av_freep(&buf);
79             return NULL;
80         } else {
81             buf = tmp;
82         }
83         if (is_first)
84             *buf = '\0';
85
86         av_strlcatf(buf, len, "%s: %s\r\n", key, value);
87     }
88     return buf;
89 }
90
91 static int icecast_close(URLContext *h)
92 {
93     IcecastContext *s = h->priv_data;
94     if (s->hd)
95         ffurl_close(s->hd);
96     return 0;
97 }
98
99 static int icecast_open(URLContext *h, const char *uri, int flags)
100 {
101     IcecastContext *s = h->priv_data;
102
103     // Dict to set options that we pass to the HTTP protocol
104     AVDictionary *opt_dict = NULL;
105
106     // URI part variables
107     char h_url[1024], host[1024], auth[1024], path[1024];
108     char *headers = NULL, *user = NULL;
109     int port, ret;
110
111     if (flags & AVIO_FLAG_READ)
112         return AVERROR(ENOSYS);
113
114     // Build header strings
115     headers = cat_header(headers, "Ice-Name", s->name);
116     headers = cat_header(headers, "Ice-Description", s->description);
117     headers = cat_header(headers, "Ice-URL", s->url);
118     headers = cat_header(headers, "Ice-Genre", s->genre);
119     headers = cat_header(headers, "Ice-Public", s->public ? "1" : "0");
120     if (!headers) {
121         ret = AVERROR(ENOMEM);
122         goto cleanup;
123     }
124
125     // Set options
126     av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
127     av_dict_set(&opt_dict, "auth_type", "basic", 0);
128     av_dict_set(&opt_dict, "headers", headers, 0);
129     av_dict_set(&opt_dict, "chunked_post", "0", 0);
130     if (NOT_EMPTY(s->content_type))
131         av_dict_set(&opt_dict, "content_type", s->content_type, 0);
132     if (NOT_EMPTY(s->user_agent))
133         av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
134
135     // Parse URI
136     av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
137                  &port, path, sizeof(path), uri);
138
139     // Check for auth data in URI
140     if (auth[0]) {
141         char *sep = strchr(auth,':');
142         if (sep) {
143             *sep = 0;
144             sep++;
145             if (s->pass) {
146                 av_free(s->pass);
147                 av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
148             }
149             if (!(s->pass = av_strdup(sep))) {
150                 ret = AVERROR(ENOMEM);
151                 goto cleanup;
152             }
153         }
154         if (!(user = av_strdup(auth))) {
155             ret = AVERROR(ENOMEM);
156             goto cleanup;
157         }
158     }
159
160     // Build new authstring
161     snprintf(auth, sizeof(auth),
162              "%s:%s",
163              user ? user : DEFAULT_ICE_USER,
164              s->pass ? s->pass : "");
165
166     // Check for mountpoint (path)
167     if (!path[0] || strcmp(path, "/") == 0) {
168         av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
169         ret = AVERROR(EIO);
170         goto cleanup;
171     }
172
173     // Build new URI for passing to http protocol
174     ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
175     // Finally open http proto handler
176     ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
177
178 cleanup:
179     // Free variables
180     av_freep(&user);
181     av_freep(&headers);
182     av_dict_free(&opt_dict);
183
184     return ret;
185 }
186
187 static int icecast_write(URLContext *h, const uint8_t *buf, int size)
188 {
189     IcecastContext *s = h->priv_data;
190     if (!s->send_started) {
191         s->send_started = 1;
192         if (!s->content_type && size >= 8) {
193             static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
194             static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
195             static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
196             if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
197                 av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n");
198                 av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
199             } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
200                 av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n");
201                 av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
202             } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
203                 av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n");
204                 av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
205             } else {
206                 av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
207                 av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
208             }
209         }
210     }
211     return ffurl_write(s->hd, buf, size);
212 }
213
214 static const AVClass icecast_context_class = {
215     .class_name     = "icecast",
216     .item_name      = av_default_item_name,
217     .option         = options,
218     .version        = LIBAVUTIL_VERSION_INT,
219 };
220
221 URLProtocol ff_icecast_protocol = {
222     .name            = "icecast",
223     .url_open        = icecast_open,
224     .url_write       = icecast_write,
225     .url_close       = icecast_close,
226     .priv_data_size  = sizeof(IcecastContext),
227     .priv_data_class = &icecast_context_class,
228     .flags           = URL_PROTOCOL_FLAG_NETWORK,
229 };