stream.c 15.7 KB
Newer Older
1
/*
Moritz Grimm's avatar
Moritz Grimm committed
2
 * Copyright (c) 2015, 2020 Moritz Grimm <mgrimm@mrsserver.net>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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
	case CFG_PROTO_ICY:
		if (SHOUTERR_SUCCESS !=
		    shout_set_protocol(s->shout, SHOUT_PROTOCOL_ICY)) {
63
			log_error("stream: %s: protocol: %s",
64
65
66
67
68
69
70
71
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
#ifdef SHOUT_PROTOCOL_ROARAUDIO
	case CFG_PROTO_ROARAUDIO:
		if (SHOUTERR_SUCCESS !=
		    shout_set_protocol(s->shout, SHOUT_PROTOCOL_ROARAUDIO)) {
72
			log_error("stream: %s: protocol: %s",
73
74
75
76
77
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
#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
82
83
		return (-1);
	}
	if (SHOUTERR_SUCCESS !=
84
	    shout_set_host(s->shout, cfg_server_get_hostname(cfg_server))) {
85
		log_error("stream: %s: hostname: %s",
86
87
88
89
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	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
93
94
95
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (SHOUTERR_SUCCESS !=
96
	    shout_set_user(s->shout, cfg_server_get_user(cfg_server))) {
97
		log_error("stream: %s: user: %s",
98
99
100
101
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (SHOUTERR_SUCCESS !=
102
	    shout_set_password(s->shout, cfg_server_get_password(cfg_server))) {
103
		log_error("stream: %s: password: %s",
104
105
106
107
108
109
110
111
		    s->name, shout_get_error(s->shout));
		return (-1);
	}

	return (0);
}

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

117
	switch (cfg_server_get_tls(cfg_server)) {
118
119
120
121
122
123
124
	case CFG_TLS_NONE:
		tls_req = SHOUT_TLS_DISABLED;
		break;
	case CFG_TLS_MAY:
		tls_req = SHOUT_TLS_AUTO;
		break;
	case CFG_TLS_REQUIRED:
125
126
127
128
		if (CFG_PROTO_HTTPS == cfg_server_get_protocol(cfg_server))
			tls_req = SHOUT_TLS_RFC2818;
		else
			tls_req = SHOUT_TLS_AUTO_NO_PLAIN;
129
130
		break;
	default:
131
		log_error("stream: %s: tls: invalid", s->name);
132
133
134
		return (-1);
	}
	if (SHOUTERR_SUCCESS != shout_set_tls(s->shout, tls_req)) {
135
		log_error("stream: %s: tls: %s", s->name,
136
137
138
		    shout_get_error(s->shout));
		return (-1);
	}
139
140
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
	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);
		}
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
186
187
188
189
		    shout_get_error(s->shout));
		return (-1);
	}
#else /* SHOUT_TLS_AUTO */
# 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
194
195
		    s->name);
		break;
	case CFG_TLS_REQUIRED:
196
		log_error("stream: %s: TLS required but not supported by libshout",
197
198
199
200
201
202
203
204
205
206
207
		    s->name);
		return (-1);
	default:
		break;
	}
#endif /* SHOUT_TLS_AUTO */

	return (0);
}

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
214
215
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
216
	switch (cfg_stream_get_format(cfg_stream)) {
Moritz Grimm's avatar
Moritz Grimm committed
217
	case CFG_STREAM_OGG:
218
219
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_OGG)) {
Moritz Grimm's avatar
Moritz Grimm committed
220
221
222
			log_error("stream: %s: format: %s: %s",
			    s->name, cfg_stream_get_format_str(cfg_stream),
			    shout_get_error(s->shout));
223
224
225
226
227
228
			return (-1);
		}
		break;
	case CFG_STREAM_MP3:
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_MP3)) {
Moritz Grimm's avatar
Moritz Grimm committed
229
230
231
			log_error("stream: %s: format: %s: %s",
			    s->name, cfg_stream_get_format_str(cfg_stream),
			    shout_get_error(s->shout));
232
233
234
			return (-1);
		}
		break;
Moritz Grimm's avatar
Moritz Grimm committed
235
236
237
	case CFG_STREAM_WEBM:
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_WEBM)) {
Moritz Grimm's avatar
Moritz Grimm committed
238
239
240
			log_error("stream: %s: format: %s: %s",
			    s->name, cfg_stream_get_format_str(cfg_stream),
			    shout_get_error(s->shout));
Moritz Grimm's avatar
Moritz Grimm committed
241
242
243
244
245
246
247
			return (-1);
		}
		break;
#ifdef SHOUT_FORMAT_MATROSKA
	case CFG_STREAM_MATROSKA:
		if (SHOUTERR_SUCCESS !=
		    shout_set_format(s->shout, SHOUT_FORMAT_MATROSKA)) {
Moritz Grimm's avatar
Moritz Grimm committed
248
249
250
			log_error("stream: %s: format: %s: %s",
			    s->name, cfg_stream_get_format_str(cfg_stream),
			    shout_get_error(s->shout));
Moritz Grimm's avatar
Moritz Grimm committed
251
252
253
254
			return (-1);
		}
		break;
#endif /* SHOUT_FORMAT_MATROSKA */
255
	default:
Moritz Grimm's avatar
Moritz Grimm committed
256
		log_error("stream: %s: format: %s: unsupported",
257
		    s->name, cfg_stream_get_format_str(cfg_stream));
258
259
		return (-1);
	}
260
261
	if (SHOUTERR_SUCCESS !=
	    shout_set_public(s->shout, (unsigned int)cfg_stream_get_public(cfg_stream))) {
262
		log_error("stream: %s: public: %s",
263
264
265
266
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (cfg_stream_get_stream_name(cfg_stream) &&
267
	    SHOUTERR_SUCCESS !=
268
	    shout_set_name(s->shout, cfg_stream_get_stream_name(cfg_stream))) {
269
		log_error("stream: %s: name: %s",
270
271
272
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
273
	if (cfg_stream_get_stream_url(cfg_stream) &&
274
	    SHOUTERR_SUCCESS !=
275
	    shout_set_url(s->shout, cfg_stream_get_stream_url(cfg_stream))) {
276
		log_error("stream: %s: url: %s",
277
278
279
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
280
	if (cfg_stream_get_stream_genre(cfg_stream) &&
281
	    SHOUTERR_SUCCESS !=
282
	    shout_set_genre(s->shout, cfg_stream_get_stream_genre(cfg_stream))) {
283
		log_error("stream: %s: genre: %s",
284
285
286
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
287
	if (cfg_stream_get_stream_description(cfg_stream) &&
288
	    SHOUTERR_SUCCESS !=
289
	    shout_set_description(s->shout, cfg_stream_get_stream_description(cfg_stream))) {
290
		log_error("stream: %s: description: %s",
291
292
293
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
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
299
300
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
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
306
307
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
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
313
314
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
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
320
321
322
323
324
		    s->name, shout_get_error(s->shout));
		return (-1);
	}

	return (0);
}
325

326
327
328
329
330
331
332
333
334
335
336
337
338
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);
	}
}

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

void
stream_exit(void)
{
	shout_shutdown();
}
351
352

struct stream *
353
stream_create(const char *name)
354
{
355
	struct stream	*s;
356

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

365
366
367
368
369
370
371
	return (s);
}

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

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

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

	streams = cfg_get_streams();
	cfg_stream = cfg_stream_list_find(streams, s->name);
	if (!cfg_stream) {
392
		log_error("stream: %s: no configuration", s->name);
393
394
395
396
397
398
399
400
		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) {
401
		log_error("stream: %s: no configuration: %s",
402
403
404
405
406
407
408
409
		    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);
410
411
412
		return (-1);
	}

413
414
415
416
417
418
419
420
	cfg_intake = stream_get_cfg_intake(s);
	if (0 > cfg_intake_validate(cfg_intake, NULL)) {
		log_error("stream: %s: referencing unconfigured intake (%s)",
		    s->name, cfg_intake_get_name(cfg_intake));
		_stream_reset(s);
		return (-1);
	}

421
422
423
424
	return (0);
}

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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

	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);
	}

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
	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))) {
470
471
472
473
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
474
475
476
477
478
479
			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))) {
480
481
482
483
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
484
485
			log_info("stream metadata: songinfo: %s",
			    mdata_get_songinfo(md));
486
		} else {
487
488
			if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md,
			        "song", mdata_get_name(md))) {
489
490
491
492
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
493
494
			log_info("stream metadata: name: %s",
			    mdata_get_name(md));
495
496
497
498
499
500
501
502
503
504
		}
	}

	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)
505
506
507
			*md_str = mdata_get_songinfo(md) ?
			    xstrdup(mdata_get_songinfo(md)) :
			    xstrdup(mdata_get_name(md));
508
509
	}

510
	return (ret == SHOUTERR_SUCCESS ? 0 : -1);
511
512
}

513
514
515
516
517
518
int
stream_get_connected(struct stream *s)
{
	return (shout_get_connected(s->shout) == SHOUTERR_CONNECTED ? 1 : 0);
}

519
520
521
522
523
524
cfg_stream_t
stream_get_cfg_stream(struct stream *s)
{
	return (cfg_stream_list_find(cfg_get_streams(), s->name));
}

525
526
527
528
529
530
531
532
533
534
535
536
537
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));
}

538
539
540
541
542
543
544
545
546
547
548
549
550
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));
}

551
552
553
554
555
556
int
stream_connect(struct stream *s)
{
	if (shout_open(s->shout) == SHOUTERR_SUCCESS)
		return (0);

557
558
559
	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));
560
561
562
563
564
565

	return (-1);
}

void
stream_disconnect(struct stream *s)
566
{
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
	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);

585
	log_warning("stream: %s: send: %s: error %d: %s", s->name,
586
587
588
589
590
591
	    shout_get_host(s->shout), shout_get_errno(s->shout),
	    shout_get_error(s->shout));

	stream_disconnect(s);

	return (-1);
592
}