Libav
icecast.c
Go to the documentation of this file.
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;
34  char *user;
35  // Options
36  char *content_type;
37  char *description;
38  char *genre;
40  char *name;
41  char *pass;
42  int public;
43  char *url;
44  char *user_agent;
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 
215  .class_name = "icecast",
216  .item_name = av_default_item_name,
217  .option = options,
218  .version = LIBAVUTIL_VERSION_INT,
219 };
220 
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,
229 };
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:2713
static int icecast_open(URLContext *h, const char *uri, int flags)
Definition: icecast.c:99
int size
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:35
char * pass
Definition: icecast.c:41
AVOption.
Definition: opt.h:234
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
char * user_agent
Definition: icecast.c:44
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:276
#define AVIO_FLAG_READ
read-only
Definition: avio.h:294
char * user
Definition: icecast.c:34
static const AVOption options[]
Definition: icecast.c:54
static int icecast_write(URLContext *h, const uint8_t *buf, int size)
Definition: icecast.c:187
#define DEFAULT_ICE_USER
Definition: icecast.c:47
int send_started
Definition: icecast.c:33
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define NOT_EMPTY(s)
Definition: icecast.c:49
#define OFFSET(x)
Definition: icecast.c:51
uint8_t
AVOptions.
static char * cat_header(char buf[], const char key[], const char value[])
Definition: icecast.c:68
int legacy_icecast
Definition: icecast.c:39
const char * name
static int flags
Definition: log.c:44
static int icecast_close(URLContext *h)
Definition: icecast.c:91
URLContext * hd
Definition: icecast.c:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:170
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
#define pass
Definition: fft_template.c:335
char * genre
Definition: icecast.c:38
URLProtocol ff_icecast_protocol
Definition: icecast.c:221
char * content_type
Definition: icecast.c:36
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition: url.c:36
NULL
Definition: eval.c:55
char * name
Definition: icecast.c:40
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
av_default_item_name
Definition: dnxhdenc.c:52
Definition: url.h:41
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:296
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:68
static const AVClass icecast_context_class
Definition: icecast.c:214
Describe the class of an AVClass context structure.
Definition: log.h:33
void * priv_data
Definition: url.h:44
char * url
Definition: icecast.c:43
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
const char * name
Definition: url.h:54
int ffurl_close(URLContext *h)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:297
Main libavformat public API header.
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:211
#define E
Definition: icecast.c:52
int len
char * description
Definition: icecast.c:37