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  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 
219  .class_name = "icecast",
220  .item_name = av_default_item_name,
221  .option = options,
222  .version = LIBAVUTIL_VERSION_INT,
223 };
224 
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,
233 };
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:2870
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:34
char * pass
Definition: icecast.c:41
AVOption.
Definition: opt.h:234
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
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:257
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AVIO_FLAG_READ
read-only
Definition: avio.h:368
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:191
#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:202
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:39
#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 URLProtocol ff_icecast_protocol
Definition: icecast.c:225
static int flags
Definition: log.c:50
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const URLProtocol **protocols, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:175
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:124
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
#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:175
char * genre
Definition: icecast.c:38
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:219
av_default_item_name
Definition: dnxhdenc.c:55
Definition: url.h:38
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:370
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:218
Describe the class of an AVClass context structure.
Definition: log.h:34
void * priv_data
Definition: url.h:45
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:56
int ffurl_close(URLContext *h)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:280
const struct URLProtocol ** protocols
A NULL-terminated list of protocols usable by the child contexts.
Definition: url.h:44
Main libavformat public API header.
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
#define E
Definition: icecast.c:52
int len
static uint8_t tmp[8]
Definition: des.c:38
char * description
Definition: icecast.c:37
static av_cold void cleanup(OMXCodecContext *s)
Definition: omx.c:576