stream.c 15.2 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)) {
220
			log_error("stream: %s: format_ogg: %s",
221
222
223
224
225
226
227
			    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)) {
228
			log_error("stream: %s: format_mp3: %s",
229
230
231
232
			    s->name, shout_get_error(s->shout));
			return (-1);
		}
		break;
Moritz Grimm's avatar
Moritz Grimm committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	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 */
251
	default:
252
		log_error("stream: %s: format: unsupported: %s",
253
		    s->name, cfg_stream_get_format_str(cfg_stream));
254
255
		return (-1);
	}
256
257
	if (SHOUTERR_SUCCESS !=
	    shout_set_public(s->shout, (unsigned int)cfg_stream_get_public(cfg_stream))) {
258
		log_error("stream: %s: public: %s",
259
260
261
262
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
	if (cfg_stream_get_stream_name(cfg_stream) &&
263
	    SHOUTERR_SUCCESS !=
264
	    shout_set_name(s->shout, cfg_stream_get_stream_name(cfg_stream))) {
265
		log_error("stream: %s: name: %s",
266
267
268
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
269
	if (cfg_stream_get_stream_url(cfg_stream) &&
270
	    SHOUTERR_SUCCESS !=
271
	    shout_set_url(s->shout, cfg_stream_get_stream_url(cfg_stream))) {
272
		log_error("stream: %s: url: %s",
273
274
275
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
276
	if (cfg_stream_get_stream_genre(cfg_stream) &&
277
	    SHOUTERR_SUCCESS !=
278
	    shout_set_genre(s->shout, cfg_stream_get_stream_genre(cfg_stream))) {
279
		log_error("stream: %s: genre: %s",
280
281
282
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
283
	if (cfg_stream_get_stream_description(cfg_stream) &&
284
	    SHOUTERR_SUCCESS !=
285
	    shout_set_description(s->shout, cfg_stream_get_stream_description(cfg_stream))) {
286
		log_error("stream: %s: description: %s",
287
288
289
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
290
	if (cfg_stream_get_stream_quality(cfg_stream) &&
291
	    SHOUTERR_SUCCESS !=
292
	    shout_set_audio_info(s->shout, SHOUT_AI_QUALITY, cfg_stream_get_stream_quality(cfg_stream))) {
293
		log_error("stream: %s: ai_quality: %s",
294
295
296
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
297
	if (cfg_stream_get_stream_bitrate(cfg_stream) &&
298
	    SHOUTERR_SUCCESS !=
299
	    shout_set_audio_info(s->shout, SHOUT_AI_BITRATE, cfg_stream_get_stream_bitrate(cfg_stream))) {
300
		log_error("stream: %s: ai_bitrate: %s",
301
302
303
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
304
	if (cfg_stream_get_stream_samplerate(cfg_stream) &&
305
	    SHOUTERR_SUCCESS !=
306
	    shout_set_audio_info(s->shout, SHOUT_AI_SAMPLERATE, cfg_stream_get_stream_samplerate(cfg_stream))) {
307
		log_error("stream: %s: ai_samplerate: %s",
308
309
310
		    s->name, shout_get_error(s->shout));
		return (-1);
	}
311
	if (cfg_stream_get_stream_channels(cfg_stream) &&
312
	    SHOUTERR_SUCCESS !=
313
	    shout_set_audio_info(s->shout, SHOUT_AI_CHANNELS, cfg_stream_get_stream_channels(cfg_stream))) {
314
		log_error("stream: %s: ai_channels: %s",
315
316
317
318
319
320
		    s->name, shout_get_error(s->shout));
		return (-1);
	}

	return (0);
}
321

322
323
324
325
326
327
328
329
330
331
332
333
334
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);
	}
}

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

void
stream_exit(void)
{
	shout_shutdown();
}
347
348

struct stream *
349
stream_create(const char *name)
350
{
351
	struct stream	*s;
352

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

361
362
363
364
365
366
367
	return (s);
}

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

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

int
376
stream_configure(struct stream *s)
377
{
378
379
380
381
382
383
384
385
386
	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) {
387
		log_error("stream: %s: no configuration", s->name);
388
389
390
391
392
393
394
395
		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) {
396
		log_error("stream: %s: no configuration: %s",
397
398
399
400
401
402
403
404
		    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);
405
406
407
408
409
410
411
		return (-1);
	}

	return (0);
}

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

	if (cfg_get_metadata_no_updates())
418
		return (0);
419
420

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

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

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
	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))) {
457
458
459
460
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
461
462
463
464
465
466
			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))) {
467
468
469
470
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
471
472
			log_info("stream metadata: songinfo: %s",
			    mdata_get_songinfo(md));
473
		} else {
474
475
			if (SHOUTERR_SUCCESS != shout_metadata_add(shout_md,
			        "song", mdata_get_name(md))) {
476
477
478
479
				log_syserr(ALERT, ENOMEM,
				    "shout_metadata_add");
				exit(1);
			}
480
481
			log_info("stream metadata: name: %s",
			    mdata_get_name(md));
482
483
484
485
486
487
488
489
490
491
		}
	}

	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)
492
493
494
			*md_str = mdata_get_songinfo(md) ?
			    xstrdup(mdata_get_songinfo(md)) :
			    xstrdup(mdata_get_name(md));
495
496
	}

497
	return (ret == SHOUTERR_SUCCESS ? 0 : -1);
498
499
}

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

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

512
513
514
515
516
517
518
519
520
521
522
523
524
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));
}

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

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

544
545
546
	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));
547
548
549
550
551
552

	return (-1);
}

void
stream_disconnect(struct stream *s)
553
{
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
	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);

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

	stream_disconnect(s);

	return (-1);
579
}