stream.c 15.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2015 Moritz Grimm <mgrimm@mrsserver.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

21
#include <errno.h>
22
#include <stdio.h>
23
24
25
#include <stdlib.h>
#include <string.h>
#include <strings.h>
26
#include <unistd.h>
27

28
29
30
#include <shout/shout.h>

#include "cfg.h"
31
#include "log.h"
32
#include "mdata.h"
33
#include "stream.h"
34
35
36
37
#include "util.h"
#include "xalloc.h"

struct stream {
38
39
	char	*name;
	shout_t *shout;
40
41
};

42
43
44
45
static int	_stream_cfg_server(struct stream *, cfg_server_t);
static int	_stream_cfg_tls(struct stream *, cfg_server_t);
static int	_stream_cfg_stream(struct stream *, cfg_stream_t);
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
54
		if (SHOUTERR_SUCCESS !=
		    shout_set_protocol(s->shout, SHOUT_PROTOCOL_HTTP)) {
55
			log_error("stream: %s: protocol: %s",
56
57
58
59
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
60
61
62
63
#ifdef SHOUT_PROTOCOL_ICY
	case CFG_PROTO_ICY:
		if (SHOUTERR_SUCCESS !=
		    shout_set_protocol(s->shout, SHOUT_PROTOCOL_ICY)) {
64
			log_error("stream: %s: protocol: %s",
65
66
67
68
69
70
71
72
73
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
#endif /* SHOUT_PROTOCOL_ICY */
#ifdef SHOUT_PROTOCOL_ROARAUDIO
	case CFG_PROTO_ROARAUDIO:
		if (SHOUTERR_SUCCESS !=
		    shout_set_protocol(s->shout, SHOUT_PROTOCOL_ROARAUDIO)) {
74
			log_error("stream: %s: protocol: %s",
75
76
77
78
79
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
#endif /* SHOUT_PROTOCOL_ROARAUDIO */
80
	default:
81
		log_error("stream: %s: protocol: unsupported: %s",
82
		    s->name, cfg_server_get_protocol_str(cfg_server));
83
84
85
		return (-1);
	}
	if (SHOUTERR_SUCCESS !=
86
	    shout_set_host(s->shout, cfg_server_get_hostname(cfg_server))) {
87
		log_error("stream: %s: hostname: %s",
88
89
90
91
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (SHOUTERR_SUCCESS !=
92
	    shout_set_port(s->shout, (unsigned short)cfg_server_get_port(cfg_server))) {
93
		log_error("stream: %s: port: %s",
94
95
96
97
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (SHOUTERR_SUCCESS !=
98
	    shout_set_user(s->shout, cfg_server_get_user(cfg_server))) {
99
		log_error("stream: %s: user: %s",
100
101
102
103
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (SHOUTERR_SUCCESS !=
104
	    shout_set_password(s->shout, cfg_server_get_password(cfg_server))) {
105
		log_error("stream: %s: password: %s",
106
107
108
109
110
111
112
113
		    s->name, shout_get_error(s->shout));
		return (-1);
	}

	return (0);
}

static int
114
_stream_cfg_tls(struct stream *s, cfg_server_t cfg_server)
115
116
117
118
{
#ifdef SHOUT_TLS_AUTO
	int	tls_req;

119
	switch (cfg_server_get_tls(cfg_server)) {
120
121
122
123
124
125
126
	case CFG_TLS_NONE:
		tls_req = SHOUT_TLS_DISABLED;
		break;
	case CFG_TLS_MAY:
		tls_req = SHOUT_TLS_AUTO;
		break;
	case CFG_TLS_REQUIRED:
127
128
129
130
		if (CFG_PROTO_HTTPS == cfg_server_get_protocol(cfg_server))
			tls_req = SHOUT_TLS_RFC2818;
		else
			tls_req = SHOUT_TLS_AUTO_NO_PLAIN;
131
132
		break;
	default:
133
		log_error("stream: %s: tls: invalid", s->name);
134
135
136
		return (-1);
	}
	if (SHOUTERR_SUCCESS != shout_set_tls(s->shout, tls_req)) {
137
		log_error("stream: %s: tls: %s", s->name,
138
139
140
		    shout_get_error(s->shout));
		return (-1);
	}
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
	if (cfg_server_get_ca_dir(cfg_server)) {
		if (0 > access(cfg_server_get_ca_dir(cfg_server), R_OK|X_OK)) {
			log_error("stream: %s: ca_dir: %s: not accessible",
			    s->name, cfg_server_get_ca_dir(cfg_server));
			return (-1);
		}
		if (SHOUTERR_SUCCESS !=
		    shout_set_ca_directory(s->shout, cfg_server_get_ca_dir(cfg_server))) {
			log_error("stream: %s: ca_dir: %s: %s", s->name,
			    cfg_server_get_ca_dir(cfg_server),
			    shout_get_error(s->shout));
			return (-1);
		}
	}
	if (cfg_server_get_ca_file(cfg_server)) {
		if (0 > access(cfg_server_get_ca_file(cfg_server), R_OK)) {
			log_error("stream: %s: ca_file: %s: not readable",
			    s->name, cfg_server_get_ca_file(cfg_server));
			return (-1);
		}
		if (SHOUTERR_SUCCESS !=
		    shout_set_ca_file(s->shout, cfg_server_get_ca_file(cfg_server))) {
			log_error("stream: %s: ca_file: %s: %s", s->name,
			    cfg_server_get_ca_file(cfg_server),
			    shout_get_error(s->shout));
			return (-1);
		}
	}
	if (cfg_server_get_client_cert(cfg_server)) {
		if (0 > access(cfg_server_get_client_cert(cfg_server), R_OK)) {
			log_error("stream: %s: client_cert: %s: not readable", s->name,
			    cfg_server_get_client_cert(cfg_server));
			return (-1);
		}
		if (SHOUTERR_SUCCESS !=
		    shout_set_client_certificate(s->shout, cfg_server_get_client_cert(cfg_server))) {
			log_error("stream: %s: client_cert: %s: %s", s->name,
			    cfg_server_get_client_cert(cfg_server),
			    shout_get_error(s->shout));
			return (-1);
		}
182
	}
183
	if (cfg_server_get_tls_cipher_suite(cfg_server) &&
184
	    SHOUTERR_SUCCESS !=
185
	    shout_set_allowed_ciphers(s->shout, cfg_server_get_tls_cipher_suite(cfg_server))) {
186
		log_error("stream: %s: tls_cipher_suite: %s", s->name,
187
188
189
190
191
		    shout_get_error(s->shout));
		return (-1);
	}
#else /* SHOUT_TLS_AUTO */
# warning "libshout library does not support TLS"
192
	switch (cfg_server_get_tls(cfg_server)) {
193
	case CFG_TLS_MAY:
194
		log_warning("stream: %s: TLS optional but not supported by libshout",
195
196
197
		    s->name);
		break;
	case CFG_TLS_REQUIRED:
198
		log_error("stream: %s: TLS required but not supported by libshout",
199
200
201
202
203
204
205
206
207
208
209
		    s->name);
		return (-1);
	default:
		break;
	}
#endif /* SHOUT_TLS_AUTO */

	return (0);
}

static int
210
_stream_cfg_stream(struct stream *s, cfg_stream_t cfg_stream)
211
212
{
	if (SHOUTERR_SUCCESS !=
213
	    shout_set_mount(s->shout, cfg_stream_get_mountpoint(cfg_stream))) {
214
		log_error("stream: %s: mountpoint: %s",
215
216
217
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
218
	switch (cfg_stream_get_format(cfg_stream)) {
Moritz Grimm's avatar
Moritz Grimm committed
219
	case CFG_STREAM_OGG:
220
221
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_OGG)) {
222
			log_error("stream: %s: format_ogg: %s",
223
224
225
226
227
228
229
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
	case CFG_STREAM_MP3:
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_MP3)) {
230
			log_error("stream: %s: format_mp3: %s",
231
232
233
234
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
Moritz Grimm's avatar
Moritz Grimm committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
	case CFG_STREAM_WEBM:
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_WEBM)) {
			log_error("stream: %s: format_mp3: %s",
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
#ifdef SHOUT_FORMAT_MATROSKA
	case CFG_STREAM_MATROSKA:
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_MATROSKA)) {
			log_error("stream: %s: format_mp3: %s",
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
#endif /* SHOUT_FORMAT_MATROSKA */
253
	default:
254
		log_error("stream: %s: format: unsupported: %s",
255
		    s->name, cfg_stream_get_format_str(cfg_stream));
256
257
		return (-1);
	}
258
259
	if (SHOUTERR_SUCCESS !=
	    shout_set_public(s->shout, (unsigned int)cfg_stream_get_public(cfg_stream))) {
260
		log_error("stream: %s: public: %s",
261
262
263
264
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (cfg_stream_get_stream_name(cfg_stream) &&
265
	    SHOUTERR_SUCCESS !=
266
	    shout_set_name(s->shout, cfg_stream_get_stream_name(cfg_stream))) {
267
		log_error("stream: %s: name: %s",
268
269
270
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
271
	if (cfg_stream_get_stream_url(cfg_stream) &&
272
	    SHOUTERR_SUCCESS !=
273
	    shout_set_url(s->shout, cfg_stream_get_stream_url(cfg_stream))) {
274
		log_error("stream: %s: url: %s",
275
276
277
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
278
	if (cfg_stream_get_stream_genre(cfg_stream) &&
279
	    SHOUTERR_SUCCESS !=
280
	    shout_set_genre(s->shout, cfg_stream_get_stream_genre(cfg_stream))) {
281
		log_error("stream: %s: genre: %s",
282
283
284
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
285
	if (cfg_stream_get_stream_description(cfg_stream) &&
286
	    SHOUTERR_SUCCESS !=
287
	    shout_set_description(s->shout, cfg_stream_get_stream_description(cfg_stream))) {
288
		log_error("stream: %s: description: %s",
289
290
291
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
292
	if (cfg_stream_get_stream_quality(cfg_stream) &&
293
	    SHOUTERR_SUCCESS !=
294
	    shout_set_audio_info(s->shout, SHOUT_AI_QUALITY, cfg_stream_get_stream_quality(cfg_stream))) {
295
		log_error("stream: %s: ai_quality: %s",
296
297
298
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
299
	if (cfg_stream_get_stream_bitrate(cfg_stream) &&
300
	    SHOUTERR_SUCCESS !=
301
	    shout_set_audio_info(s->shout, SHOUT_AI_BITRATE, cfg_stream_get_stream_bitrate(cfg_stream))) {
302
		log_error("stream: %s: ai_bitrate: %s",
303
304
305
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
306
	if (cfg_stream_get_stream_samplerate(cfg_stream) &&
307
	    SHOUTERR_SUCCESS !=
308
	    shout_set_audio_info(s->shout, SHOUT_AI_SAMPLERATE, cfg_stream_get_stream_samplerate(cfg_stream))) {
309
		log_error("stream: %s: ai_samplerate: %s",
310
311
312
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
313
	if (cfg_stream_get_stream_channels(cfg_stream) &&
314
	    SHOUTERR_SUCCESS !=
315
	    shout_set_audio_info(s->shout, SHOUT_AI_CHANNELS, cfg_stream_get_stream_channels(cfg_stream))) {
316
		log_error("stream: %s: ai_channels: %s",
317
318
319
320
321
322
		    s->name, shout_get_error(s->shout));
		return (-1);
	}

	return (0);
}
323

324
325
326
327
328
329
330
331
332
333
334
335
336
void
_stream_reset(struct stream *s)
{
	if (!s->shout)
		return;
	shout_free(s->shout);
	s->shout = shout_new();
	if (NULL == s->shout) {
		log_syserr(ALERT, ENOMEM, "shout_new");
		exit(1);
	}
}

337
338
339
340
341
342
343
344
345
346
347
348
int
stream_init(void)
{
	shout_init();
	return (0);
}

void
stream_exit(void)
{
	shout_shutdown();
}
349
350

struct stream *
351
stream_create(const char *name)
352
{
353
	struct stream	*s;
354

355
356
357
358
	s = xcalloc(1UL, sizeof(*s));
	s->name = xstrdup(name);
	s->shout = shout_new();
	if (NULL == s->shout) {
359
360
361
362
		log_syserr(ALERT, ENOMEM, "shout_new");
		exit(1);
	}

363
364
365
366
367
368
369
	return (s);
}

void
stream_destroy(struct stream **s_p)
{
	struct stream	*s = *s_p;
370

371
372
373
374
	shout_free(s->shout);
	xfree(s->name);
	xfree(s);
	*s_p = NULL;
375
376
377
}

int
378
stream_configure(struct stream *s)
379
{
380
381
382
383
384
385
386
387
388
	cfg_stream_list_t	 streams;
	cfg_server_list_t	 servers;
	cfg_stream_t		 cfg_stream;
	cfg_server_t		 cfg_server;
	const char		*server;

	streams = cfg_get_streams();
	cfg_stream = cfg_stream_list_find(streams, s->name);
	if (!cfg_stream) {
389
		log_error("stream: %s: no configuration", s->name);
390
391
392
393
394
395
396
397
		return (-1);
	}
	servers = cfg_get_servers();
	server = cfg_stream_get_server(cfg_stream);
	if (!server)
		server = CFG_DEFAULT;
	cfg_server = cfg_server_list_find(servers, server);
	if (!cfg_server) {
398
		log_error("stream: %s: no configuration: %s",
399
400
401
402
403
404
405
406
		    s->name, cfg_stream_get_server(cfg_stream));
		return (-1);
	}

	if (0 != _stream_cfg_server(s, cfg_server) ||
	    0 != _stream_cfg_tls(s, cfg_server) ||
	    0 != _stream_cfg_stream(s, cfg_stream)) {
		_stream_reset(s);
407
408
409
410
411
412
413
		return (-1);
	}

	return (0);
}

int
414
stream_set_metadata(struct stream *s, mdata_t md, char **md_str)
415
416
{
	shout_metadata_t	*shout_md = NULL;
417
	int			 ret;
418
419

	if (cfg_get_metadata_no_updates())
420
		return (0);
421
422

	if (md == NULL)
423
		return (-1);
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

	if ((shout_md = shout_metadata_new()) == NULL) {
		log_syserr(ALERT, ENOMEM, "shout_metadata_new");
		exit(1);
	}

	/*
	 * We can do this, because we know how libshout works. This adds
	 * "charset=UTF-8" to the HTTP metadata update request and has the
	 * desired effect of letting newer-than-2.3.1 versions of Icecast know
	 * which encoding we're using.
	 */
	if (shout_metadata_add(shout_md, "charset", "UTF-8") != SHOUTERR_SUCCESS) {
		/* Assume SHOUTERR_MALLOC */
		log_syserr(ALERT, ENOMEM, "shout_metadata_add");
		exit(1);
	}

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
	if (cfg_get_metadata_format_str()) {
		char	buf[BUFSIZ];

		mdata_strformat(md, buf, sizeof(buf),
		    cfg_get_metadata_format_str());
		if (SHOUTERR_SUCCESS !=
		    shout_metadata_add(shout_md, "song", buf)) {
			log_syserr(ALERT, ENOMEM, "shout_metadata_add");
			exit(1);
		}
		log_info("stream metadata: formatted: %s", buf);
	} else {
		if (mdata_get_artist(md) && mdata_get_title(md)) {
			if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md,
				"artist", mdata_get_artist(md)) ||
			    SHOUTERR_SUCCESS != shout_metadata_add(shout_md,
				"title", mdata_get_title(md))) {
459
460
461
462
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
463
464
465
466
467
468
			log_info("stream metadata: artist=\"%s\" title=\"%s\"",
			    mdata_get_artist(md),
			    mdata_get_title(md));
		} else if (mdata_get_songinfo(md)) {
			if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md,
			        "song", mdata_get_songinfo(md))) {
469
470
471
472
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
473
474
			log_info("stream metadata: songinfo: %s",
			    mdata_get_songinfo(md));
475
		} else {
476
477
			if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md,
			        "song", mdata_get_name(md))) {
478
479
480
481
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
482
483
			log_info("stream metadata: name: %s",
			    mdata_get_name(md));
484
485
486
487
488
489
490
491
492
493
		}
	}

	if ((ret = shout_set_metadata(s->shout, shout_md)) != SHOUTERR_SUCCESS)
		log_warning("shout_set_metadata: %s", shout_get_error(s->shout));

	shout_metadata_free(shout_md);

	if (ret == SHOUTERR_SUCCESS) {
		if (md_str != NULL && *md_str == NULL)
494
495
496
			*md_str = mdata_get_songinfo(md) ?
			    xstrdup(mdata_get_songinfo(md)) :
			    xstrdup(mdata_get_name(md));
497
498
	}

499
	return (ret == SHOUTERR_SUCCESS ? 0 : -1);
500
501
}

502
503
504
505
506
507
int
stream_get_connected(struct stream *s)
{
	return (shout_get_connected(s->shout) == SHOUTERR_CONNECTED ? 1 : 0);
}

508
509
510
511
512
513
cfg_stream_t
stream_get_cfg_stream(struct stream *s)
{
	return (cfg_stream_list_find(cfg_get_streams(), s->name));
}

514
515
516
517
518
519
520
521
522
523
524
525
526
cfg_intake_t
stream_get_cfg_intake(struct stream *s)
{
	cfg_stream_t	 cfg_stream;
	const char	*intake;

	cfg_stream = cfg_stream_list_get(cfg_get_streams(), s->name);
	intake = cfg_stream_get_intake(cfg_stream);
	if (!intake)
		intake = CFG_DEFAULT;
	return (cfg_intake_list_get(cfg_get_intakes(), intake));
}

527
528
529
530
531
532
533
534
535
536
537
538
539
cfg_server_t
stream_get_cfg_server(struct stream *s)
{
	cfg_stream_t	 cfg_stream;
	const char	*server;

	cfg_stream = cfg_stream_list_get(cfg_get_streams(), s->name);
	server = cfg_stream_get_server(cfg_stream);
	if (!server)
		server = CFG_DEFAULT;
	return (cfg_server_list_get(cfg_get_servers(), server));
}

540
541
542
543
544
545
int
stream_connect(struct stream *s)
{
	if (shout_open(s->shout) == SHOUTERR_SUCCESS)
		return (0);

546
547
548
	log_warning("stream: %s: connect: [%s]:%d: error %d: %s", s->name,
	    shout_get_host(s->shout), shout_get_port(s->shout),
	    shout_get_errno(s->shout), shout_get_error(s->shout));
549
550
551
552
553
554

	return (-1);
}

void
stream_disconnect(struct stream *s)
555
{
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
	if (!stream_get_connected(s))
		return;
	shout_close(s->shout);
}

void
stream_sync(struct stream *s)
{
	shout_sync(s->shout);
}

int
stream_send(struct stream *s, const char *data, size_t len)
{
	if (shout_send(s->shout, (const unsigned char *)data, len)
	    == SHOUTERR_SUCCESS)
		return (0);

574
	log_warning("stream: %s: send: %s: error %d: %s", s->name,
575
576
577
578
579
580
	    shout_get_host(s->shout), shout_get_errno(s->shout),
	    shout_get_error(s->shout));

	stream_disconnect(s);

	return (-1);
581
}