| 1 |
/* |
| 2 |
* Copyright (c) 2015, 2020 Moritz Grimm <[email protected]> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifdef HAVE_CONFIG_H |
| 18 |
# include "config.h" |
| 19 |
#endif /* HAVE_CONFIG_H */ |
| 20 |
|
| 21 |
#include <errno.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <strings.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include <shout/shout.h> |
| 29 |
|
| 30 |
#include "cfg.h" |
| 31 |
#include "log.h" |
| 32 |
#include "mdata.h" |
| 33 |
#include "stream.h" |
| 34 |
#include "util.h" |
| 35 |
#include "xalloc.h" |
| 36 |
|
| 37 |
struct stream { |
| 38 |
char *name; |
| 39 |
shout_t *shout; |
| 40 |
}; |
| 41 |
|
| 42 |
static int _stream_cfg_server(struct stream *, cfg_server_t); |
| 43 |
static int _stream_cfg_tls(struct stream *, cfg_server_t); |
| 44 |
static int _stream_cfg_stream(struct stream *, cfg_stream_t); |
| 45 |
static void _stream_reset(struct stream *); |
| 46 |
|
| 47 |
static int |
| 48 |
_stream_cfg_server(struct stream *s, cfg_server_t cfg_server) |
| 49 |
{ |
| 50 |
switch (cfg_server_get_protocol(cfg_server)) { |
| 51 |
case CFG_PROTO_HTTP: |
| 52 |
case CFG_PROTO_HTTPS: |
| 53 |
if (SHOUTERR_SUCCESS != |
| 54 |
shout_set_protocol(s->shout, SHOUT_PROTOCOL_HTTP)) { |
| 55 |
log_error("stream: %s: protocol: %s", |
| 56 |
s->name, shout_get_error(s->shout)); |
| 57 |
return (-1); |
| 58 |
} |
| 59 |
break; |
| 60 |
case CFG_PROTO_ICY: |
| 61 |
if (SHOUTERR_SUCCESS != |
| 62 |
shout_set_protocol(s->shout, SHOUT_PROTOCOL_ICY)) { |
| 63 |
log_error("stream: %s: protocol: %s", |
| 64 |
s->name, shout_get_error(s->shout)); |
| 65 |
return (-1); |
| 66 |
} |
| 67 |
break; |
| 68 |
#ifdef SHOUT_PROTOCOL_ROARAUDIO |
| 69 |
case CFG_PROTO_ROARAUDIO: |
| 70 |
if (SHOUTERR_SUCCESS != |
| 71 |
shout_set_protocol(s->shout, SHOUT_PROTOCOL_ROARAUDIO)) { |
| 72 |
log_error("stream: %s: protocol: %s", |
| 73 |
s->name, shout_get_error(s->shout)); |
| 74 |
return (-1); |
| 75 |
} |
| 76 |
break; |
| 77 |
#endif /* SHOUT_PROTOCOL_ROARAUDIO */ |
| 78 |
default: |
| 79 |
log_error("stream: %s: protocol: unsupported: %s", |
| 80 |
s->name, cfg_server_get_protocol_str(cfg_server)); |
| 81 |
return (-1); |
| 82 |
} |
| 83 |
if (SHOUTERR_SUCCESS != |
| 84 |
shout_set_host(s->shout, cfg_server_get_hostname(cfg_server))) { |
| 85 |
log_error("stream: %s: hostname: %s", |
| 86 |
s->name, shout_get_error(s->shout)); |
| 87 |
return (-1); |
| 88 |
} |
| 89 |
if (SHOUTERR_SUCCESS != |
| 90 |
shout_set_port(s->shout, (unsigned short)cfg_server_get_port(cfg_server))) { |
| 91 |
log_error("stream: %s: port: %s", |
| 92 |
s->name, shout_get_error(s->shout)); |
| 93 |
return (-1); |
| 94 |
} |
| 95 |
if (SHOUTERR_SUCCESS != |
| 96 |
shout_set_user(s->shout, cfg_server_get_user(cfg_server))) { |
| 97 |
log_error("stream: %s: user: %s", |
| 98 |
s->name, shout_get_error(s->shout)); |
| 99 |
return (-1); |
| 100 |
} |
| 101 |
if (SHOUTERR_SUCCESS != |
| 102 |
shout_set_password(s->shout, cfg_server_get_password(cfg_server))) { |
| 103 |
log_error("stream: %s: password: %s", |
| 104 |
s->name, shout_get_error(s->shout)); |
| 105 |
return (-1); |
| 106 |
} |
| 107 |
|
| 108 |
return (0); |
| 109 |
} |
| 110 |
|
| 111 |
static int |
| 112 |
_stream_cfg_tls(struct stream *s, cfg_server_t cfg_server) |
| 113 |
{ |
| 114 |
#ifdef SHOUT_TLS_AUTO |
| 115 |
int tls_req; |
| 116 |
|
| 117 |
switch (cfg_server_get_tls(cfg_server)) { |
| 118 |
case CFG_TLS_NONE: |
| 119 |
tls_req = SHOUT_TLS_DISABLED; |
| 120 |
break; |
| 121 |
case CFG_TLS_MAY: |
| 122 |
tls_req = SHOUT_TLS_AUTO; |
| 123 |
break; |
| 124 |
case CFG_TLS_REQUIRED: |
| 125 |
if (CFG_PROTO_HTTPS == cfg_server_get_protocol(cfg_server)) |
| 126 |
tls_req = SHOUT_TLS_RFC2818; |
| 127 |
else |
| 128 |
tls_req = SHOUT_TLS_AUTO_NO_PLAIN; |
| 129 |
break; |
| 130 |
default: |
| 131 |
log_error("stream: %s: tls: invalid", s->name); |
| 132 |
return (-1); |
| 133 |
} |
| 134 |
if (SHOUTERR_SUCCESS != shout_set_tls(s->shout, tls_req)) { |
| 135 |
log_error("stream: %s: tls: %s", s->name, |
| 136 |
shout_get_error(s->shout)); |
| 137 |
return (-1); |
| 138 |
} |
| 139 |
if (cfg_server_get_ca_dir(cfg_server)) { |
| 140 |
if (0 > access(cfg_server_get_ca_dir(cfg_server), R_OK|X_OK)) { |
| 141 |
log_error("stream: %s: ca_dir: %s: not accessible", |
| 142 |
s->name, cfg_server_get_ca_dir(cfg_server)); |
| 143 |
return (-1); |
| 144 |
} |
| 145 |
if (SHOUTERR_SUCCESS != |
| 146 |
shout_set_ca_directory(s->shout, cfg_server_get_ca_dir(cfg_server))) { |
| 147 |
log_error("stream: %s: ca_dir: %s: %s", s->name, |
| 148 |
cfg_server_get_ca_dir(cfg_server), |
| 149 |
shout_get_error(s->shout)); |
| 150 |
return (-1); |
| 151 |
} |
| 152 |
} |
| 153 |
if (cfg_server_get_ca_file(cfg_server)) { |
| 154 |
if (0 > access(cfg_server_get_ca_file(cfg_server), R_OK)) { |
| 155 |
log_error("stream: %s: ca_file: %s: not readable", |
| 156 |
s->name, cfg_server_get_ca_file(cfg_server)); |
| 157 |
return (-1); |
| 158 |
} |
| 159 |
if (SHOUTERR_SUCCESS != |
| 160 |
shout_set_ca_file(s->shout, cfg_server_get_ca_file(cfg_server))) { |
| 161 |
log_error("stream: %s: ca_file: %s: %s", s->name, |
| 162 |
cfg_server_get_ca_file(cfg_server), |
| 163 |
shout_get_error(s->shout)); |
| 164 |
return (-1); |
| 165 |
} |
| 166 |
} |
| 167 |
if (cfg_server_get_client_cert(cfg_server)) { |
| 168 |
if (0 > access(cfg_server_get_client_cert(cfg_server), R_OK)) { |
| 169 |
log_error("stream: %s: client_cert: %s: not readable", s->name, |
| 170 |
cfg_server_get_client_cert(cfg_server)); |
| 171 |
return (-1); |
| 172 |
} |
| 173 |
if (SHOUTERR_SUCCESS != |
| 174 |
shout_set_client_certificate(s->shout, cfg_server_get_client_cert(cfg_server))) { |
| 175 |
log_error("stream: %s: client_cert: %s: %s", s->name, |
| 176 |
cfg_server_get_client_cert(cfg_server), |
| 177 |
shout_get_error(s->shout)); |
| 178 |
return (-1); |
| 179 |
} |
| 180 |
} |
| 181 |
if (cfg_server_get_tls_cipher_suite(cfg_server) && |
| 182 |
SHOUTERR_SUCCESS != |
| 183 |
shout_set_allowed_ciphers(s->shout, cfg_server_get_tls_cipher_suite(cfg_server))) { |
| 184 |
log_error("stream: %s: tls_cipher_suite: %s", s->name, |
| 185 |
shout_get_error(s->shout)); |
| 186 |
return (-1); |
| 187 |
} |
| 188 |
#else /* SHOUT_TLS_AUTO */ |
| 189 |
# warning "libshout library does not support TLS" |
| 190 |
switch (cfg_server_get_tls(cfg_server)) { |
| 191 |
case CFG_TLS_MAY: |
| 192 |
log_warning("stream: %s: TLS optional but not supported by libshout", |
| 193 |
s->name); |
| 194 |
break; |
| 195 |
case CFG_TLS_REQUIRED: |
| 196 |
log_error("stream: %s: TLS required but not supported by libshout", |
| 197 |
s->name); |
| 198 |
return (-1); |
| 199 |
default: |
| 200 |
break; |
| 201 |
} |
| 202 |
#endif /* SHOUT_TLS_AUTO */ |
| 203 |
|
| 204 |
return (0); |
| 205 |
} |
| 206 |
|
| 207 |
static int |
| 208 |
_stream_cfg_stream(struct stream *s, cfg_stream_t cfg_stream) |
| 209 |
{ |
| 210 |
if (SHOUTERR_SUCCESS != |
| 211 |
shout_set_mount(s->shout, cfg_stream_get_mountpoint(cfg_stream))) { |
| 212 |
log_error("stream: %s: mountpoint: %s", |
| 213 |
s->name, shout_get_error(s->shout)); |
| 214 |
return (-1); |
| 215 |
} |
| 216 |
switch (cfg_stream_get_format(cfg_stream)) { |
| 217 |
case CFG_STREAM_OGG: |
| 218 |
if (SHOUTERR_SUCCESS != |
| 219 |
shout_set_content_format(s->shout, SHOUT_FORMAT_OGG, 0, NULL)) { |
| 220 |
log_error("stream: %s: format: %s: %s", |
| 221 |
s->name, cfg_stream_get_format_str(cfg_stream), |
| 222 |
shout_get_error(s->shout)); |
| 223 |
return (-1); |
| 224 |
} |
| 225 |
break; |
| 226 |
case CFG_STREAM_MP3: |
| 227 |
if (SHOUTERR_SUCCESS != |
| •228 |
shout_set_content_format(s->shout, SHOUT_FORMAT_MP3, SHOUT_USAGE_AUDIO, NULL)) { |
| 229 |
log_error("stream: %s: format: %s: %s", |
| 230 |
s->name, cfg_stream_get_format_str(cfg_stream), |
| 231 |
shout_get_error(s->shout)); |
| 232 |
return (-1); |
| 233 |
} |
| 234 |
break; |
| 235 |
case CFG_STREAM_WEBM: |
| 236 |
if (SHOUTERR_SUCCESS != |
| 237 |
shout_set_content_format(s->shout, SHOUT_FORMAT_WEBM, 0, NULL)) { |
| 238 |
log_error("stream: %s: format: %s: %s", |
| 239 |
s->name, cfg_stream_get_format_str(cfg_stream), |
| 240 |
shout_get_error(s->shout)); |
| 241 |
return (-1); |
| 242 |
} |
| 243 |
break; |
| 244 |
#ifdef SHOUT_FORMAT_MATROSKA |
| 245 |
case CFG_STREAM_MATROSKA: |
| 246 |
if (SHOUTERR_SUCCESS != |
| 247 |
shout_set_content_format(s->shout, SHOUT_FORMAT_MATROSKA, 0, NULL)) { |
| 248 |
log_error("stream: %s: format: %s: %s", |
| 249 |
s->name, cfg_stream_get_format_str(cfg_stream), |
| 250 |
shout_get_error(s->shout)); |
| 251 |
return (-1); |
| 252 |
} |
| 253 |
break; |
| 254 |
#endif /* SHOUT_FORMAT_MATROSKA */ |
| 255 |
default: |
| 256 |
log_error("stream: %s: format: %s: unsupported", |
| 257 |
s->name, cfg_stream_get_format_str(cfg_stream)); |
| 258 |
return (-1); |
| 259 |
} |
| 260 |
if (SHOUTERR_SUCCESS != |
| 261 |
shout_set_public(s->shout, (unsigned int)cfg_stream_get_public(cfg_stream))) { |
| 262 |
log_error("stream: %s: public: %s", |
| 263 |
s->name, shout_get_error(s->shout)); |
| 264 |
return (-1); |
| 265 |
} |
| 266 |
if (cfg_stream_get_stream_name(cfg_stream) && |
| 267 |
SHOUTERR_SUCCESS != |
| 268 |
shout_set_meta(s->shout, SHOUT_META_NAME, cfg_stream_get_stream_name(cfg_stream))) { |
| 269 |
log_error("stream: %s: name: %s", |
| 270 |
s->name, shout_get_error(s->shout)); |
| 271 |
return (-1); |
| 272 |
} |
| 273 |
if (cfg_stream_get_stream_url(cfg_stream) && |
| 274 |
SHOUTERR_SUCCESS != |
| 275 |
shout_set_meta(s->shout, SHOUT_META_URL, cfg_stream_get_stream_url(cfg_stream))) { |
| 276 |
log_error("stream: %s: url: %s", |
| 277 |
s->name, shout_get_error(s->shout)); |
| 278 |
return (-1); |
| 279 |
} |
| 280 |
if (cfg_stream_get_stream_genre(cfg_stream) && |
| 281 |
SHOUTERR_SUCCESS != |
| 282 |
shout_set_meta(s->shout, SHOUT_META_GENRE, cfg_stream_get_stream_genre(cfg_stream))) { |
| 283 |
log_error("stream: %s: genre: %s", |
| 284 |
s->name, shout_get_error(s->shout)); |
| 285 |
return (-1); |
| 286 |
} |
| 287 |
if (cfg_stream_get_stream_description(cfg_stream) && |
| 288 |
SHOUTERR_SUCCESS != |
| 289 |
shout_set_meta(s->shout, SHOUT_META_DESCRIPTION, cfg_stream_get_stream_description(cfg_stream))) { |
| 290 |
log_error("stream: %s: description: %s", |
| 291 |
s->name, shout_get_error(s->shout)); |
| 292 |
return (-1); |
| 293 |
} |
| 294 |
if (cfg_stream_get_stream_quality(cfg_stream) && |
| 295 |
SHOUTERR_SUCCESS != |
| 296 |
shout_set_audio_info(s->shout, SHOUT_AI_QUALITY, cfg_stream_get_stream_quality(cfg_stream))) { |
| 297 |
log_error("stream: %s: ai_quality: %s", |
| 298 |
s->name, shout_get_error(s->shout)); |
| 299 |
return (-1); |
| 300 |
} |
| 301 |
if (cfg_stream_get_stream_bitrate(cfg_stream) && |
| 302 |
SHOUTERR_SUCCESS != |
| 303 |
shout_set_audio_info(s->shout, SHOUT_AI_BITRATE, cfg_stream_get_stream_bitrate(cfg_stream))) { |
| 304 |
log_error("stream: %s: ai_bitrate: %s", |
| 305 |
s->name, shout_get_error(s->shout)); |
| 306 |
return (-1); |
| 307 |
} |
| 308 |
if (cfg_stream_get_stream_samplerate(cfg_stream) && |
| 309 |
SHOUTERR_SUCCESS != |
| 310 |
shout_set_audio_info(s->shout, SHOUT_AI_SAMPLERATE, cfg_stream_get_stream_samplerate(cfg_stream))) { |
| 311 |
log_error("stream: %s: ai_samplerate: %s", |
| 312 |
s->name, shout_get_error(s->shout)); |
| 313 |
return (-1); |
| 314 |
} |
| 315 |
if (cfg_stream_get_stream_channels(cfg_stream) && |
| 316 |
SHOUTERR_SUCCESS != |
| 317 |
shout_set_audio_info(s->shout, SHOUT_AI_CHANNELS, cfg_stream_get_stream_channels(cfg_stream))) { |
| 318 |
log_error("stream: %s: ai_channels: %s", |
| 319 |
s->name, shout_get_error(s->shout)); |
| 320 |
return (-1); |
| 321 |
} |
| 322 |
|
| 323 |
return (0); |
| 324 |
} |
| 325 |
|
| 326 |
void |
| 327 |
_stream_reset(struct stream *s) |
| 328 |
{ |
| 329 |
if (!s->shout) |
| 330 |
return; |
| 331 |
shout_free(s->shout); |
| 332 |
s->shout = shout_new(); |
| 333 |
if (NULL == s->shout) { |
| 334 |
log_syserr(ALERT, ENOMEM, "shout_new"); |
| 335 |
exit(1); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
int |
| 340 |
stream_init(void) |
| 341 |
{ |
| 342 |
shout_init(); |
| 343 |
return (0); |
| 344 |
} |
| 345 |
|
| 346 |
void |
| 347 |
stream_exit(void) |
| 348 |
{ |
| 349 |
shout_shutdown(); |
| 350 |
} |
| 351 |
|
| 352 |
struct stream * |
| 353 |
stream_create(const char *name) |
| 354 |
{ |
| 355 |
struct stream *s; |
| 356 |
|
| 357 |
s = xcalloc(1UL, sizeof(*s)); |
| 358 |
s->name = xstrdup(name); |
| 359 |
s->shout = shout_new(); |
| 360 |
if (NULL == s->shout) { |
| 361 |
log_syserr(ALERT, ENOMEM, "shout_new"); |
| 362 |
exit(1); |
| 363 |
} |
| 364 |
|
| 365 |
return (s); |
| 366 |
} |
| 367 |
|
| 368 |
void |
| 369 |
stream_destroy(struct stream **s_p) |
| 370 |
{ |
| 371 |
struct stream *s = *s_p; |
| 372 |
|
| 373 |
shout_free(s->shout); |
| 374 |
xfree(s->name); |
| 375 |
xfree(s); |
| 376 |
*s_p = NULL; |
| 377 |
} |
| 378 |
|
| 379 |
int |
| 380 |
stream_configure(struct stream *s) |
| 381 |
{ |
| 382 |
cfg_stream_list_t streams; |
| 383 |
cfg_server_list_t servers; |
| 384 |
cfg_stream_t cfg_stream; |
| 385 |
cfg_server_t cfg_server; |
| 386 |
cfg_intake_t cfg_intake; |
| 387 |
const char *server; |
| 388 |
|
| 389 |
streams = cfg_get_streams(); |
| 390 |
cfg_stream = cfg_stream_list_find(streams, s->name); |
| 391 |
if (!cfg_stream) { |
| 392 |
log_error("stream: %s: no configuration", s->name); |
| 393 |
return (-1); |
| 394 |
} |
| 395 |
servers = cfg_get_servers(); |
| 396 |
server = cfg_stream_get_server(cfg_stream); |
| 397 |
if (!server) |
| 398 |
server = CFG_DEFAULT; |
| 399 |
cfg_server = cfg_server_list_find(servers, server); |
| 400 |
if (!cfg_server) { |
| 401 |
log_error("stream: %s: no configuration: %s", |
| 402 |
s->name, cfg_stream_get_server(cfg_stream)); |
| 403 |
return (-1); |
| 404 |
} |
| 405 |
|
| 406 |
if (0 != _stream_cfg_server(s, cfg_server) || |
| 407 |
0 != _stream_cfg_tls(s, cfg_server) || |
| 408 |
0 != _stream_cfg_stream(s, cfg_stream)) { |
| 409 |
_stream_reset(s); |
| 410 |
return (-1); |
| 411 |
} |
| 412 |
|
| 413 |
cfg_intake = stream_get_cfg_intake(s); |
| 414 |
if (0 > cfg_intake_validate(cfg_intake, NULL)) { |
| 415 |
log_error("stream: %s: referencing unconfigured intake (%s)", |
| 416 |
s->name, cfg_intake_get_name(cfg_intake)); |
| 417 |
_stream_reset(s); |
| 418 |
return (-1); |
| 419 |
} |
| 420 |
|
| 421 |
return (0); |
| 422 |
} |
| 423 |
|
| 424 |
int |
| 425 |
stream_set_metadata(struct stream *s, mdata_t md, char **md_str) |
| 426 |
{ |
| 427 |
shout_metadata_t *shout_md = NULL; |
| 428 |
int ret; |
| 429 |
|
| 430 |
if (cfg_get_metadata_no_updates()) |
| 431 |
return (0); |
| 432 |
|
| 433 |
if (md == NULL) |
| 434 |
return (-1); |
| 435 |
|
| 436 |
if ((shout_md = shout_metadata_new()) == NULL) { |
| 437 |
log_syserr(ALERT, ENOMEM, "shout_metadata_new"); |
| 438 |
exit(1); |
| 439 |
} |
| 440 |
|
| 441 |
/* |
| 442 |
* We can do this, because we know how libshout works. This adds |
| 443 |
* "charset=UTF-8" to the HTTP metadata update request and has the |
| 444 |
* desired effect of letting newer-than-2.3.1 versions of Icecast know |
| 445 |
* which encoding we're using. |
| 446 |
*/ |
| 447 |
if (shout_metadata_add(shout_md, "charset", "UTF-8") != SHOUTERR_SUCCESS) { |
| 448 |
/* Assume SHOUTERR_MALLOC */ |
| 449 |
log_syserr(ALERT, ENOMEM, "shout_metadata_add"); |
| 450 |
exit(1); |
| 451 |
} |
| 452 |
|
| 453 |
if (cfg_get_metadata_format_str()) { |
| 454 |
char buf[BUFSIZ]; |
| 455 |
|
| 456 |
mdata_strformat(md, buf, sizeof(buf), |
| 457 |
cfg_get_metadata_format_str()); |
| 458 |
if (SHOUTERR_SUCCESS != |
| 459 |
shout_metadata_add(shout_md, "song", buf)) { |
| 460 |
log_syserr(ALERT, ENOMEM, "shout_metadata_add"); |
| 461 |
exit(1); |
| 462 |
} |
| 463 |
log_info("stream metadata: formatted: %s", buf); |
| 464 |
} else { |
| 465 |
if (mdata_get_artist(md) && mdata_get_title(md)) { |
| 466 |
if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md, |
| 467 |
"artist", mdata_get_artist(md)) || |
| 468 |
SHOUTERR_SUCCESS != shout_metadata_add(shout_md, |
| 469 |
"title", mdata_get_title(md))) { |
| 470 |
log_syserr(ALERT, ENOMEM, |
| 471 |
"shout_metadata_add"); |
| 472 |
exit(1); |
| 473 |
} |
| 474 |
log_info("stream metadata: artist=\"%s\" title=\"%s\"", |
| 475 |
mdata_get_artist(md), |
| 476 |
mdata_get_title(md)); |
| 477 |
} else if (mdata_get_songinfo(md)) { |
| 478 |
if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md, |
| 479 |
"song", mdata_get_songinfo(md))) { |
| 480 |
log_syserr(ALERT, ENOMEM, |
| 481 |
"shout_metadata_add"); |
| 482 |
exit(1); |
| 483 |
} |
| 484 |
log_info("stream metadata: songinfo: %s", |
| 485 |
mdata_get_songinfo(md)); |
| 486 |
} else { |
| 487 |
if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md, |
| 488 |
"song", mdata_get_name(md))) { |
| 489 |
log_syserr(ALERT, ENOMEM, |
| 490 |
"shout_metadata_add"); |
| 491 |
exit(1); |
| 492 |
} |
| 493 |
log_info("stream metadata: name: %s", |
| 494 |
mdata_get_name(md)); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
if ((ret = shout_set_metadata(s->shout, shout_md)) != SHOUTERR_SUCCESS) |
| 499 |
log_warning("shout_set_metadata: %s", shout_get_error(s->shout)); |
| 500 |
|
| 501 |
shout_metadata_free(shout_md); |
| 502 |
|
| 503 |
if (ret == SHOUTERR_SUCCESS) { |
| 504 |
if (md_str != NULL && *md_str == NULL) |
| 505 |
*md_str = mdata_get_songinfo(md) ? |
| 506 |
xstrdup(mdata_get_songinfo(md)) : |
| 507 |
xstrdup(mdata_get_name(md)); |
| 508 |
} |
| 509 |
|
| 510 |
return (ret == SHOUTERR_SUCCESS ? 0 : -1); |
| 511 |
} |
| 512 |
|
| 513 |
int |
| 514 |
stream_get_connected(struct stream *s) |
| 515 |
{ |
| 516 |
return (shout_get_connected(s->shout) == SHOUTERR_CONNECTED ? 1 : 0); |
| 517 |
} |
| 518 |
|
| 519 |
cfg_stream_t |
| 520 |
stream_get_cfg_stream(struct stream *s) |
| 521 |
{ |
| 522 |
return (cfg_stream_list_find(cfg_get_streams(), s->name)); |
| 523 |
} |
| 524 |
|
| 525 |
cfg_intake_t |
| 526 |
stream_get_cfg_intake(struct stream *s) |
| 527 |
{ |
| 528 |
cfg_stream_t cfg_stream; |
| 529 |
const char *intake; |
| 530 |
|
| 531 |
cfg_stream = cfg_stream_list_get(cfg_get_streams(), s->name); |
| 532 |
intake = cfg_stream_get_intake(cfg_stream); |
| 533 |
if (!intake) |
| 534 |
intake = CFG_DEFAULT; |
| 535 |
return (cfg_intake_list_get(cfg_get_intakes(), intake)); |
| 536 |
} |
| 537 |
|
| 538 |
cfg_server_t |
| 539 |
stream_get_cfg_server(struct stream *s) |
| 540 |
{ |
| 541 |
cfg_stream_t cfg_stream; |
| 542 |
const char *server; |
| 543 |
|
| 544 |
cfg_stream = cfg_stream_list_get(cfg_get_streams(), s->name); |
| 545 |
server = cfg_stream_get_server(cfg_stream); |
| 546 |
if (!server) |
| 547 |
server = CFG_DEFAULT; |
| 548 |
return (cfg_server_list_get(cfg_get_servers(), server)); |
| 549 |
} |
| 550 |
|
| 551 |
int |
| 552 |
stream_connect(struct stream *s) |
| 553 |
{ |
| 554 |
if (shout_open(s->shout) == SHOUTERR_SUCCESS) |
| 555 |
return (0); |
| 556 |
|
| 557 |
log_warning("stream: %s: connect: [%s]:%d: error %d: %s", s->name, |
| 558 |
shout_get_host(s->shout), shout_get_port(s->shout), |
| 559 |
shout_get_errno(s->shout), shout_get_error(s->shout)); |
| 560 |
|
| 561 |
return (-1); |
| 562 |
} |
| 563 |
|
| 564 |
void |
| 565 |
stream_disconnect(struct stream *s) |
| 566 |
{ |
| 567 |
if (!stream_get_connected(s)) |
| 568 |
return; |
| 569 |
shout_close(s->shout); |
| 570 |
} |
| 571 |
|
| 572 |
void |
| 573 |
stream_sync(struct stream *s) |
| 574 |
{ |
| 575 |
shout_sync(s->shout); |
| 576 |
} |
| 577 |
|
| 578 |
int |
| 579 |
stream_send(struct stream *s, const char *data, size_t len) |
| 580 |
{ |
| 581 |
if (shout_send(s->shout, (const unsigned char *)data, len) |
| 582 |
== SHOUTERR_SUCCESS) |
| 583 |
return (0); |
| 584 |
|
| 585 |
log_warning("stream: %s: send: %s: error %d: %s", s->name, |
| 586 |
shout_get_host(s->shout), shout_get_errno(s->shout), |
| 587 |
shout_get_error(s->shout)); |
| 588 |
|
| 589 |
stream_disconnect(s); |
| 590 |
|
| 591 |
return (-1); |
| 592 |
} |