]> git.sesse.net Git - ffmpeg/blob - libavformat/icecast.c
dxva2: Adjust printf length modifiers where appropriate
[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     av_dict_set(&opt_dict, "send_expect_100", s->legacy_icecast ? "0" : "1", 0);
131     if (NOT_EMPTY(s->content_type))
132         av_dict_set(&opt_dict, "content_type", s->content_type, 0);
133     else
134         av_dict_set(&opt_dict, "content_type", "audio/mpeg", 0);
135     if (NOT_EMPTY(s->user_agent))
136         av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
137
138     // Parse URI
139     av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
140                  &port, path, sizeof(path), uri);
141
142     // Check for auth data in URI
143     if (auth[0]) {
144         char *sep = strchr(auth,':');
145         if (sep) {
146             *sep = 0;
147             sep++;
148             if (s->pass) {
149                 av_free(s->pass);
150                 av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
151             }
152             if (!(s->pass = av_strdup(sep))) {
153                 ret = AVERROR(ENOMEM);
154                 goto cleanup;
155             }
156         }
157         if (!(user = av_strdup(auth))) {
158             ret = AVERROR(ENOMEM);
159             goto cleanup;
160         }
161     }
162
163     // Build new authstring
164     snprintf(auth, sizeof(auth),
165              "%s:%s",
166              user ? user : DEFAULT_ICE_USER,
167              s->pass ? s->pass : "");
168
169     // Check for mountpoint (path)
170     if (!path[0] || strcmp(path, "/") == 0) {
171         av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
172         ret = AVERROR(EIO);
173         goto cleanup;
174     }
175
176     // Build new URI for passing to http protocol
177     ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
178     // Finally open http proto handler
179     ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict,
180                      h->protocols, h);
181
182 cleanup:
183     // Free variables
184     av_freep(&user);
185     av_freep(&headers);
186     av_dict_free(&opt_dict);
187
188     return ret;
189 }
190
191 static int icecast_write(URLContext *h, const uint8_t *buf, int size)
192 {
193     IcecastContext *s = h->priv_data;
194     if (!s->send_started) {
195         s->send_started = 1;
196         if (!s->content_type && size >= 8) {
197             static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
198             static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
199             static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
200             if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
201                 av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n");
202                 av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
203             } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
204                 av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n");
205                 av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
206             } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
207                 av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n");
208                 av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
209             } else {
210                 av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
211                 av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
212             }
213         }
214     }
215     return ffurl_write(s->hd, buf, size);
216 }
217
218 static const AVClass icecast_context_class = {
219     .class_name     = "icecast",
220     .item_name      = av_default_item_name,
221     .option         = options,
222     .version        = LIBAVUTIL_VERSION_INT,
223 };
224
225 const URLProtocol ff_icecast_protocol = {
226     .name            = "icecast",
227     .url_open        = icecast_open,
228     .url_write       = icecast_write,
229     .url_close       = icecast_close,
230     .priv_data_size  = sizeof(IcecastContext),
231     .priv_data_class = &icecast_context_class,
232     .flags           = URL_PROTOCOL_FLAG_NETWORK,
233 };