FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
icecast.c
Go to the documentation of this file.
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/avstring.h"
24 #include "libavutil/bprint.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;
35  char *user;
36  // Options
37  char *content_type;
38  char *description;
39  char *genre;
41  char *name;
42  char *pass;
43  int public;
44  char *url;
45  char *user_agent;
47 
48 #define DEFAULT_ICE_USER "source"
49 
50 #define NOT_EMPTY(s) (s && s[0])
51 
52 #define OFFSET(x) offsetof(IcecastContext, x)
53 #define E AV_OPT_FLAG_ENCODING_PARAM
54 
55 static const AVOption options[] = {
56  { "ice_genre", "set stream genre", OFFSET(genre), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
57  { "ice_name", "set stream description", OFFSET(name), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
58  { "ice_description", "set stream description", OFFSET(description), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
59  { "ice_url", "set stream website", OFFSET(url), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
60  { "ice_public", "set if stream is public", OFFSET(public), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
61  { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
62  { "password", "set password", OFFSET(pass), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
63  { "content_type", "set content-type, MUST be set if not audio/mpeg", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, E },
64  { "legacy_icecast", "use legacy SOURCE method, for Icecast < v2.4", OFFSET(legacy_icecast), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
65  { NULL }
66 };
67 
68 
69 static void cat_header(AVBPrint *bp, const char key[], const char value[])
70 {
71  if (NOT_EMPTY(value))
72  av_bprintf(bp, "%s: %s\r\n", key, value);
73 }
74 
75 static int icecast_close(URLContext *h)
76 {
78  if (s->hd)
79  ffurl_close(s->hd);
80  return 0;
81 }
82 
83 static int icecast_open(URLContext *h, const char *uri, int flags)
84 {
86 
87  // Dict to set options that we pass to the HTTP protocol
88  AVDictionary *opt_dict = NULL;
89 
90  // URI part variables
91  char h_url[1024], host[1024], auth[1024], path[1024];
92  char *headers = NULL, *user = NULL;
93  int port, ret;
94  AVBPrint bp;
95 
96  if (flags & AVIO_FLAG_READ)
97  return AVERROR(ENOSYS);
98 
99  av_bprint_init(&bp, 0, 1);
100 
101  // Build header strings
102  cat_header(&bp, "Ice-Name", s->name);
103  cat_header(&bp, "Ice-Description", s->description);
104  cat_header(&bp, "Ice-URL", s->url);
105  cat_header(&bp, "Ice-Genre", s->genre);
106  cat_header(&bp, "Ice-Public", s->public ? "1" : "0");
107  if (!av_bprint_is_complete(&bp)) {
108  ret = AVERROR(ENOMEM);
109  goto cleanup;
110  }
111  av_bprint_finalize(&bp, &headers);
112 
113  // Set options
114  av_dict_set(&opt_dict, "method", s->legacy_icecast ? "SOURCE" : "PUT", 0);
115  av_dict_set(&opt_dict, "auth_type", "basic", 0);
116  av_dict_set(&opt_dict, "headers", headers, 0);
117  if (NOT_EMPTY(s->content_type))
118  av_dict_set(&opt_dict, "content_type", s->content_type, 0);
119  if (NOT_EMPTY(s->user_agent))
120  av_dict_set(&opt_dict, "user_agent", s->user_agent, 0);
121 
122  // Parse URI
123  av_url_split(NULL, 0, auth, sizeof(auth), host, sizeof(host),
124  &port, path, sizeof(path), uri);
125 
126  // Check for auth data in URI
127  if (auth[0]) {
128  char *sep = strchr(auth, ':');
129  if (sep) {
130  *sep = 0;
131  sep++;
132  if (s->pass) {
133  av_free(s->pass);
134  av_log(h, AV_LOG_WARNING, "Overwriting -password <pass> with URI password!\n");
135  }
136  if (!(s->pass = av_strdup(sep))) {
137  ret = AVERROR(ENOMEM);
138  goto cleanup;
139  }
140  }
141  if (!(user = av_strdup(auth))) {
142  ret = AVERROR(ENOMEM);
143  goto cleanup;
144  }
145  }
146 
147  // Build new authstring
148  snprintf(auth, sizeof(auth),
149  "%s:%s",
150  user ? user : DEFAULT_ICE_USER,
151  s->pass ? s->pass : "");
152 
153  // Check for mountpoint (path)
154  if (!path[0] || strcmp(path, "/") == 0) {
155  av_log(h, AV_LOG_ERROR, "No mountpoint (path) specified!\n");
156  ret = AVERROR(EIO);
157  goto cleanup;
158  }
159 
160  // Build new URI for passing to http protocol
161  ff_url_join(h_url, sizeof(h_url), "http", auth, host, port, "%s", path);
162  // Finally open http proto handler
163  ret = ffurl_open(&s->hd, h_url, AVIO_FLAG_READ_WRITE, NULL, &opt_dict);
164 
165 cleanup:
166  av_freep(&user);
167  av_freep(&headers);
168  av_dict_free(&opt_dict);
169 
170  return ret;
171 }
172 
173 static int icecast_write(URLContext *h, const uint8_t *buf, int size)
174 {
175  IcecastContext *s = h->priv_data;
176  if (!s->send_started) {
177  s->send_started = 1;
178  if (!s->content_type && size >= 8) {
179  static const uint8_t oggs[4] = { 0x4F, 0x67, 0x67, 0x53 };
180  static const uint8_t webm[4] = { 0x1A, 0x45, 0xDF, 0xA3 };
181  static const uint8_t opus[8] = { 0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64 };
182  if (memcmp(buf, oggs, sizeof(oggs)) == 0) {
183  av_log(h, AV_LOG_WARNING, "Streaming Ogg but appropriate content type NOT set!\n");
184  av_log(h, AV_LOG_WARNING, "Set it with -content_type application/ogg\n");
185  } else if (memcmp(buf, opus, sizeof(opus)) == 0) {
186  av_log(h, AV_LOG_WARNING, "Streaming Opus but appropriate content type NOT set!\n");
187  av_log(h, AV_LOG_WARNING, "Set it with -content_type audio/ogg\n");
188  } else if (memcmp(buf, webm, sizeof(webm)) == 0) {
189  av_log(h, AV_LOG_WARNING, "Streaming WebM but appropriate content type NOT set!\n");
190  av_log(h, AV_LOG_WARNING, "Set it with -content_type video/webm\n");
191  } else {
192  av_log(h, AV_LOG_WARNING, "It seems you are streaming an unsupported format.\n");
193  av_log(h, AV_LOG_WARNING, "It might work, but is not officially supported in Icecast!\n");
194  }
195  }
196  }
197  return ffurl_write(s->hd, buf, size);
198 }
199 
201  .class_name = "icecast",
202  .item_name = av_default_item_name,
203  .option = options,
204  .version = LIBAVUTIL_VERSION_INT,
205 };
206 
208  .name = "icecast",
209  .url_open = icecast_open,
210  .url_write = icecast_write,
211  .url_close = icecast_close,
212  .priv_data_size = sizeof(IcecastContext),
213  .priv_data_class = &icecast_context_class,
215 };