ShoutOutputPlugin.cxx 12.4 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21
#include "ShoutOutputPlugin.hxx"
22
#include "../OutputAPI.hxx"
23
#include "encoder/EncoderInterface.hxx"
24 25
#include "encoder/EncoderPlugin.hxx"
#include "encoder/EncoderList.hxx"
26
#include "config/ConfigError.hxx"
27 28
#include "util/Error.hxx"
#include "util/Domain.hxx"
29
#include "system/FatalError.hxx"
30
#include "Log.hxx"
Warren Dukes's avatar
Warren Dukes committed
31

32 33
#include <shout/shout.h>

34
#include <assert.h>
35
#include <stdlib.h>
Max Kellermann's avatar
Max Kellermann committed
36
#include <string.h>
37
#include <stdio.h>
38

39
static constexpr unsigned DEFAULT_CONN_TIMEOUT = 2;
40

41
struct ShoutOutput final {
42
	AudioOutput base;
43

44 45 46
	shout_t *shout_conn;
	shout_metadata_t *shout_meta;

47
	Encoder *encoder;
48 49 50 51 52 53

	float quality;
	int bitrate;

	int timeout;

54
	uint8_t buffer[32768];
55 56

	ShoutOutput()
57 58
		:base(shout_output_plugin),
		 shout_conn(shout_new()),
59 60 61 62 63 64 65 66 67 68 69 70
		shout_meta(shout_metadata_new()),
		quality(-2.0),
		bitrate(-1),
		timeout(DEFAULT_CONN_TIMEOUT) {}

	~ShoutOutput() {
		if (shout_meta != nullptr)
			shout_metadata_free(shout_meta);
		if (shout_conn != nullptr)
			shout_free(shout_conn);
	}

71 72
	bool Initialize(const ConfigBlock &block, Error &error) {
		return base.Configure(block, error);
73 74
	}

75
	bool Configure(const ConfigBlock &block, Error &error);
76 77
};

Max Kellermann's avatar
Max Kellermann committed
78
static int shout_init_count;
79

80
static constexpr Domain shout_output_domain("shout_output");
81

82
static const EncoderPlugin *
83
shout_encoder_plugin_get(const char *name)
84
{
85 86 87 88
	if (strcmp(name, "ogg") == 0)
		name = "vorbis";
	else if (strcmp(name, "mp3") == 0)
		name = "lame";
89

90
	return encoder_plugin_get(name);
91
}
92

93 94
gcc_pure
static const char *
95
require_block_string(const ConfigBlock &block, const char *name)
96
{
97
	const char *value = block.GetBlockValue(name);
98
	if (value == nullptr)
99
		FormatFatalError("no \"%s\" defined for shout device defined "
100
				 "at line %d\n", name, block.line);
101 102 103

	return value;
}
104

105
inline bool
106
ShoutOutput::Configure(const ConfigBlock &block, Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
107
{
108

109 110
	const AudioFormat audio_format = base.config_audio_format;
	if (!audio_format.IsFullyDefined()) {
111 112
		error.Set(config_domain,
			  "Need full audio format specification");
113
		return false;
114 115
	}

116 117 118
	const char *host = require_block_string(block, "host");
	const char *mount = require_block_string(block, "mount");
	unsigned port = block.GetBlockValue("port", 0u);
119
	if (port == 0) {
120
		error.Set(config_domain, "shout port must be configured");
121
		return false;
122 123
	}

124 125
	const char *passwd = require_block_string(block, "password");
	const char *name = require_block_string(block, "name");
126

127
	bool is_public = block.GetBlockValue("public", false);
128

129
	const char *user = block.GetBlockValue("user", "source");
130

131
	const char *value = block.GetBlockValue("quality");
132
	if (value != nullptr) {
133
		char *test;
134
		quality = strtod(value, &test);
135

136
		if (*test != '\0' || quality < -1.0 || quality > 10.0) {
137 138
			error.Format(config_domain,
				     "shout quality \"%s\" is not a number in the "
139 140
				     "range -1 to 10",
				     value);
141
			return false;
142 143
		}

144
		if (block.GetBlockValue("bitrate") != nullptr) {
145 146 147
			error.Set(config_domain,
				  "quality and bitrate are "
				  "both defined");
148
			return false;
149
		}
Avuton Olrich's avatar
Avuton Olrich committed
150
	} else {
151
		value = block.GetBlockValue("bitrate");
152
		if (value == nullptr) {
153 154
			error.Set(config_domain,
				  "neither bitrate nor quality defined");
155
			return false;
156
		}
157

158
		char *test;
159
		bitrate = strtol(value, &test, 10);
160

161
		if (*test != '\0' || bitrate <= 0) {
162 163
			error.Set(config_domain,
				  "bitrate must be a positive integer");
164
			return false;
165
		}
166 167
	}

168
	const char *encoding = block.GetBlockValue("encoding", "ogg");
169
	const auto encoder_plugin = shout_encoder_plugin_get(encoding);
170
	if (encoder_plugin == nullptr) {
171 172 173
		error.Format(config_domain,
			     "couldn't find shout encoder plugin \"%s\"",
			     encoding);
174
		return false;
175
	}
176

177
	encoder = encoder_init(*encoder_plugin, block, error);
178
	if (encoder == nullptr)
179
		return false;
180

181
	unsigned shout_format;
182 183 184 185 186
	if (strcmp(encoding, "mp3") == 0 || strcmp(encoding, "lame") == 0)
		shout_format = SHOUT_FORMAT_MP3;
	else
		shout_format = SHOUT_FORMAT_OGG;

187
	unsigned protocol;
188
	value = block.GetBlockValue("protocol");
189
	if (value != nullptr) {
190
		if (0 == strcmp(value, "shoutcast") &&
191
		    0 != strcmp(encoding, "mp3")) {
192 193 194
			error.Format(config_domain,
				     "you cannot stream \"%s\" to shoutcast, use mp3",
				     encoding);
195
			return false;
196
		} else if (0 == strcmp(value, "shoutcast"))
197
			protocol = SHOUT_PROTOCOL_ICY;
198
		else if (0 == strcmp(value, "icecast1"))
199
			protocol = SHOUT_PROTOCOL_XAUDIOCAST;
200
		else if (0 == strcmp(value, "icecast2"))
201
			protocol = SHOUT_PROTOCOL_HTTP;
202
		else {
203 204 205 206
			error.Format(config_domain,
				     "shout protocol \"%s\" is not \"shoutcast\" or "
				     "\"icecast1\"or \"icecast2\"",
				     value);
207
			return false;
208
		}
209 210 211 212
	} else {
		protocol = SHOUT_PROTOCOL_HTTP;
	}

213 214 215 216 217 218 219 220
	if (shout_set_host(shout_conn, host) != SHOUTERR_SUCCESS ||
	    shout_set_port(shout_conn, port) != SHOUTERR_SUCCESS ||
	    shout_set_password(shout_conn, passwd) != SHOUTERR_SUCCESS ||
	    shout_set_mount(shout_conn, mount) != SHOUTERR_SUCCESS ||
	    shout_set_name(shout_conn, name) != SHOUTERR_SUCCESS ||
	    shout_set_user(shout_conn, user) != SHOUTERR_SUCCESS ||
	    shout_set_public(shout_conn, is_public) != SHOUTERR_SUCCESS ||
	    shout_set_format(shout_conn, shout_format)
Avuton Olrich's avatar
Avuton Olrich committed
221
	    != SHOUTERR_SUCCESS ||
222 223
	    shout_set_protocol(shout_conn, protocol) != SHOUTERR_SUCCESS ||
	    shout_set_agent(shout_conn, "MPD") != SHOUTERR_SUCCESS) {
224
		error.Set(shout_output_domain, shout_get_error(shout_conn));
225
		return false;
226
	}
227

228
	/* optional paramters */
229
	timeout = block.GetBlockValue("timeout", DEFAULT_CONN_TIMEOUT);
230

231
	value = block.GetBlockValue("genre");
232
	if (value != nullptr && shout_set_genre(shout_conn, value)) {
233
		error.Set(shout_output_domain, shout_get_error(shout_conn));
234
		return false;
235 236
	}

237
	value = block.GetBlockValue("description");
238
	if (value != nullptr && shout_set_description(shout_conn, value)) {
239
		error.Set(shout_output_domain, shout_get_error(shout_conn));
240
		return false;
241 242
	}

243
	value = block.GetBlockValue("url");
244
	if (value != nullptr && shout_set_url(shout_conn, value)) {
245
		error.Set(shout_output_domain, shout_get_error(shout_conn));
246
		return false;
247 248
	}

249 250 251
	{
		char temp[11];
		memset(temp, 0, sizeof(temp));
Avuton Olrich's avatar
Avuton Olrich committed
252

253
		snprintf(temp, sizeof(temp), "%u", audio_format.channels);
254
		shout_set_audio_info(shout_conn, SHOUT_AI_CHANNELS, temp);
255

256
		snprintf(temp, sizeof(temp), "%u", audio_format.sample_rate);
Avuton Olrich's avatar
Avuton Olrich committed
257

258
		shout_set_audio_info(shout_conn, SHOUT_AI_SAMPLERATE, temp);
259

260 261 262
		if (quality >= -1.0) {
			snprintf(temp, sizeof(temp), "%2.2f", quality);
			shout_set_audio_info(shout_conn, SHOUT_AI_QUALITY,
Avuton Olrich's avatar
Avuton Olrich committed
263 264
					     temp);
		} else {
265 266
			snprintf(temp, sizeof(temp), "%d", bitrate);
			shout_set_audio_info(shout_conn, SHOUT_AI_BITRATE,
Avuton Olrich's avatar
Avuton Olrich committed
267
					     temp);
268 269 270
		}
	}

271 272
	return true;
}
273

274
static AudioOutput *
275
my_shout_init_driver(const ConfigBlock &block, Error &error)
276
{
277
	ShoutOutput *sd = new ShoutOutput();
278
	if (!sd->Initialize(block, error)) {
279 280
		delete sd;
		return nullptr;
281 282
	}

283
	if (!sd->Configure(block, error)) {
284 285
		delete sd;
		return nullptr;
286 287 288 289 290 291 292 293
	}

	if (shout_init_count == 0)
		shout_init();

	shout_init_count++;

	return &sd->base;
294 295
}

296
static bool
297
handle_shout_error(ShoutOutput *sd, int err, Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
298 299
{
	switch (err) {
300 301
	case SHOUTERR_SUCCESS:
		break;
302

303 304
	case SHOUTERR_UNCONNECTED:
	case SHOUTERR_SOCKET:
305 306 307 308 309
		error.Format(shout_output_domain, err,
			     "Lost shout connection to %s:%i: %s",
			     shout_get_host(sd->shout_conn),
			     shout_get_port(sd->shout_conn),
			     shout_get_error(sd->shout_conn));
310 311
		return false;

312
	default:
313 314 315 316 317
		error.Format(shout_output_domain, err,
			     "connection to %s:%i error: %s",
			     shout_get_host(sd->shout_conn),
			     shout_get_port(sd->shout_conn),
			     shout_get_error(sd->shout_conn));
318
		return false;
319 320
	}

321
	return true;
322 323
}

324
static bool
325
write_page(ShoutOutput *sd, Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
326
{
327
	assert(sd->encoder != nullptr);
328

329 330 331 332 333 334 335 336 337 338
	while (true) {
		size_t nbytes = encoder_read(sd->encoder,
					     sd->buffer, sizeof(sd->buffer));
		if (nbytes == 0)
			return true;

		int err = shout_send(sd->shout_conn, sd->buffer, nbytes);
		if (!handle_shout_error(sd, err, error))
			return false;
	}
339

340
	return true;
341 342
}

343
static void close_shout_conn(ShoutOutput * sd)
Avuton Olrich's avatar
Avuton Olrich committed
344
{
345
	if (sd->encoder != nullptr) {
346 347
		if (encoder_end(sd->encoder, IgnoreError()))
			write_page(sd, IgnoreError());
348

349
		sd->encoder->Close();
350
	}
351

Max Kellermann's avatar
Max Kellermann committed
352 353
	if (shout_get_connected(sd->shout_conn) != SHOUTERR_UNCONNECTED &&
	    shout_close(sd->shout_conn) != SHOUTERR_SUCCESS) {
354 355 356
		FormatWarning(shout_output_domain,
			      "problem closing connection to shout server: %s",
			      shout_get_error(sd->shout_conn));
357 358 359
	}
}

360
static void
361
my_shout_finish_driver(AudioOutput *ao)
Avuton Olrich's avatar
Avuton Olrich committed
362
{
363
	ShoutOutput *sd = (ShoutOutput *)ao;
364

365
	sd->encoder->Dispose();
366

367
	delete sd;
368

Max Kellermann's avatar
Max Kellermann committed
369
	shout_init_count--;
370

Max Kellermann's avatar
Max Kellermann committed
371
	if (shout_init_count == 0)
Avuton Olrich's avatar
Avuton Olrich committed
372
		shout_shutdown();
373 374
}

375
static void
376
my_shout_drop_buffered_audio(AudioOutput *ao)
Avuton Olrich's avatar
Avuton Olrich committed
377
{
378
	gcc_unused
379
	ShoutOutput *sd = (ShoutOutput *)ao;
380 381

	/* needs to be implemented for shout */
382 383
}

384
static void
385
my_shout_close_device(AudioOutput *ao)
Avuton Olrich's avatar
Avuton Olrich committed
386
{
387
	ShoutOutput *sd = (ShoutOutput *)ao;
388

Max Kellermann's avatar
Max Kellermann committed
389
	close_shout_conn(sd);
390
}
391

392
static bool
393
shout_connect(ShoutOutput *sd, Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
394
{
395
	switch (shout_open(sd->shout_conn)) {
396 397
	case SHOUTERR_SUCCESS:
	case SHOUTERR_CONNECTED:
398 399
		return true;

400
	default:
401 402 403 404 405
		error.Format(shout_output_domain,
			     "problem opening connection to shout server %s:%i: %s",
			     shout_get_host(sd->shout_conn),
			     shout_get_port(sd->shout_conn),
			     shout_get_error(sd->shout_conn));
406
		return false;
407
	}
408 409
}

410
static bool
411
my_shout_open_device(AudioOutput *ao, AudioFormat &audio_format,
412
		     Error &error)
413
{
414
	ShoutOutput *sd = (ShoutOutput *)ao;
415

416
	if (!shout_connect(sd, error))
417
		return false;
418

419
	if (!sd->encoder->Open(audio_format, error)) {
420 421 422 423 424
		shout_close(sd->shout_conn);
		return false;
	}

	if (!write_page(sd, error)) {
425
		sd->encoder->Close();
Max Kellermann's avatar
Max Kellermann committed
426
		shout_close(sd->shout_conn);
427
		return false;
428 429
	}

430
	return true;
431 432
}

433
static unsigned
434
my_shout_delay(AudioOutput *ao)
435
{
436
	ShoutOutput *sd = (ShoutOutput *)ao;
437 438 439 440 441 442 443 444

	int delay = shout_delay(sd->shout_conn);
	if (delay < 0)
		delay = 0;

	return delay;
}

445
static size_t
446
my_shout_play(AudioOutput *ao, const void *chunk, size_t size,
447
	      Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
448
{
449
	ShoutOutput *sd = (ShoutOutput *)ao;
450

451 452 453 454
	return encoder_write(sd->encoder, chunk, size, error) &&
		write_page(sd, error)
		? size
		: 0;
455 456
}

457
static bool
458
my_shout_pause(AudioOutput *ao)
459
{
460
	static char silence[1020];
461

462
	return my_shout_play(ao, silence, sizeof(silence), IgnoreError());
463 464
}

465
static void
466
shout_tag_to_metadata(const Tag &tag, char *dest, size_t size)
467 468 469 470 471 472 473
{
	char artist[size];
	char title[size];

	artist[0] = 0;
	title[0] = 0;

474
	for (const auto &item : tag) {
475
		switch (item.type) {
476
		case TAG_ARTIST:
477
			strncpy(artist, item.value, size);
478
			break;
479
		case TAG_TITLE:
480
			strncpy(title, item.value, size);
481 482 483 484 485 486 487
			break;

		default:
			break;
		}
	}

488
	snprintf(dest, size, "%s - %s", artist, title);
489 490
}

491
static void my_shout_set_tag(AudioOutput *ao,
492
			     const Tag &tag)
Avuton Olrich's avatar
Avuton Olrich committed
493
{
494
	ShoutOutput *sd = (ShoutOutput *)ao;
495

496
	if (sd->encoder->plugin.tag != nullptr) {
497 498
		/* encoder plugin supports stream tags */

499 500 501
		Error error;
		if (!encoder_pre_tag(sd->encoder, error) ||
		    !write_page(sd, error) ||
502
		    !encoder_tag(sd->encoder, tag, error)) {
503
			LogError(error);
504 505 506 507 508 509
			return;
		}
	} else {
		/* no stream tag support: fall back to icy-metadata */
		char song[1024];
		shout_tag_to_metadata(tag, song, sizeof(song));
510

511 512 513
		shout_metadata_add(sd->shout_meta, "song", song);
		if (SHOUTERR_SUCCESS != shout_set_metadata(sd->shout_conn,
							   sd->shout_meta)) {
514 515
			LogWarning(shout_output_domain,
				   "error setting shout metadata");
516 517
		}
	}
518

519
	write_page(sd, IgnoreError());
520 521
}

522
const struct AudioOutputPlugin shout_output_plugin = {
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	"shout",
	nullptr,
	my_shout_init_driver,
	my_shout_finish_driver,
	nullptr,
	nullptr,
	my_shout_open_device,
	my_shout_close_device,
	my_shout_delay,
	my_shout_set_tag,
	my_shout_play,
	nullptr,
	my_shout_drop_buffered_audio,
	my_shout_pause,
	nullptr,
538
};