shout_plugin.c 13.2 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 "output_api.h"
22 23
#include "encoder_plugin.h"
#include "encoder_list.h"
24
#include "mpd_error.h"
Warren Dukes's avatar
Warren Dukes committed
25

26 27 28
#include <shout/shout.h>
#include <glib.h>

29
#include <assert.h>
30 31
#include <stdlib.h>
#include <stdio.h>
32

33 34 35
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "shout"

36
#define DEFAULT_CONN_TIMEOUT  2
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
struct shout_buffer {
	unsigned char data[32768];
	size_t len;
};

struct shout_data {
	shout_t *shout_conn;
	shout_metadata_t *shout_meta;

	struct encoder *encoder;

	float quality;
	int bitrate;

	int timeout;

	struct shout_buffer buf;
};

Max Kellermann's avatar
Max Kellermann committed
57
static int shout_init_count;
58

59 60 61 62 63 64 65 66 67
/**
 * The quark used for GError.domain.
 */
static inline GQuark
shout_output_quark(void)
{
	return g_quark_from_static_string("shout_output");
}

68
static const struct encoder_plugin *
69
shout_encoder_plugin_get(const char *name)
70
{
71 72 73 74
	if (strcmp(name, "ogg") == 0)
		name = "vorbis";
	else if (strcmp(name, "mp3") == 0)
		name = "lame";
75

76
	return encoder_plugin_get(name);
77
}
78

Max Kellermann's avatar
Max Kellermann committed
79
static struct shout_data *new_shout_data(void)
Avuton Olrich's avatar
Avuton Olrich committed
80
{
81
	struct shout_data *ret = g_new(struct shout_data, 1);
82

Max Kellermann's avatar
Max Kellermann committed
83
	ret->shout_conn = shout_new();
84
	ret->shout_meta = shout_metadata_new();
85
	ret->bitrate = -1;
86
	ret->quality = -2.0;
87
	ret->timeout = DEFAULT_CONN_TIMEOUT;
88

89 90 91
	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
92
static void free_shout_data(struct shout_data *sd)
Avuton Olrich's avatar
Avuton Olrich committed
93
{
94 95
	if (sd->shout_meta)
		shout_metadata_free(sd->shout_meta);
Max Kellermann's avatar
Max Kellermann committed
96 97
	if (sd->shout_conn)
		shout_free(sd->shout_conn);
98

99
	g_free(sd);
100 101
}

Max Kellermann's avatar
Max Kellermann committed
102
#define check_block_param(name) {		  \
103
		block_param = config_get_block_param(param, name);	\
Max Kellermann's avatar
Max Kellermann committed
104
		if (!block_param) {					\
105 106
			MPD_ERROR("no \"%s\" defined for shout device defined at line " \
				  "%i\n", name, param->line);		\
Max Kellermann's avatar
Max Kellermann committed
107 108
		}							\
	}
109

110 111
static void *
my_shout_init_driver(const struct audio_format *audio_format,
112 113
		     const struct config_param *param,
		     GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
114
{
Max Kellermann's avatar
Max Kellermann committed
115
	struct shout_data *sd;
Avuton Olrich's avatar
Avuton Olrich committed
116
	char *test;
117
	unsigned port;
Avuton Olrich's avatar
Avuton Olrich committed
118 119 120
	char *host;
	char *mount;
	char *passwd;
121
	const char *encoding;
122 123
	const struct encoder_plugin *encoder_plugin;
	unsigned shout_format;
124
	unsigned protocol;
Max Kellermann's avatar
Max Kellermann committed
125
	const char *user;
Avuton Olrich's avatar
Avuton Olrich committed
126
	char *name;
127
	const char *value;
128
	const struct block_param *block_param;
129
	int public;
130

131 132 133 134 135 136 137
	if (audio_format == NULL ||
	    !audio_format_fully_defined(audio_format)) {
		g_set_error(error, shout_output_quark(), 0,
			    "Need full audio format specification");
		return NULL;
	}

Max Kellermann's avatar
Max Kellermann committed
138
	sd = new_shout_data();
139

Max Kellermann's avatar
Max Kellermann committed
140
	if (shout_init_count == 0)
Avuton Olrich's avatar
Avuton Olrich committed
141
		shout_init();
142

Max Kellermann's avatar
Max Kellermann committed
143
	shout_init_count++;
144

Max Kellermann's avatar
Max Kellermann committed
145 146
	check_block_param("host");
	host = block_param->value;
147

Max Kellermann's avatar
Max Kellermann committed
148 149
	check_block_param("mount");
	mount = block_param->value;
150

151 152
	port = config_get_block_unsigned(param, "port", 0);
	if (port == 0) {
153 154
		g_set_error(error, shout_output_quark(), 0,
			    "shout port must be configured");
155
		goto failure;
156 157
	}

Max Kellermann's avatar
Max Kellermann committed
158 159
	check_block_param("password");
	passwd = block_param->value;
160

Max Kellermann's avatar
Max Kellermann committed
161 162
	check_block_param("name");
	name = block_param->value;
163

164
	public = config_get_block_bool(param, "public", false);
165

166
	user = config_get_block_string(param, "user", "source");
167

168 169 170
	value = config_get_block_string(param, "quality", NULL);
	if (value != NULL) {
		sd->quality = strtod(value, &test);
171

172
		if (*test != '\0' || sd->quality < -1.0 || sd->quality > 10.0) {
173 174 175 176
			g_set_error(error, shout_output_quark(), 0,
				    "shout quality \"%s\" is not a number in the "
				    "range -1 to 10, line %i",
				    value, param->line);
177
			goto failure;
178 179
		}

180
		if (config_get_block_string(param, "bitrate", NULL) != NULL) {
181 182 183
			g_set_error(error, shout_output_quark(), 0,
				    "quality and bitrate are "
				    "both defined");
184
			goto failure;
185
		}
Avuton Olrich's avatar
Avuton Olrich committed
186
	} else {
187
		value = config_get_block_string(param, "bitrate", NULL);
188 189 190
		if (value == NULL) {
			g_set_error(error, shout_output_quark(), 0,
				    "neither bitrate nor quality defined");
191
			goto failure;
192
		}
193

194
		sd->bitrate = strtol(value, &test, 10);
195

Avuton Olrich's avatar
Avuton Olrich committed
196
		if (*test != '\0' || sd->bitrate <= 0) {
197 198
			g_set_error(error, shout_output_quark(), 0,
				    "bitrate must be a positive integer");
199
			goto failure;
200
		}
201 202
	}

203
	encoding = config_get_block_string(param, "encoding", "ogg");
204
	encoder_plugin = shout_encoder_plugin_get(encoding);
205 206 207 208
	if (encoder_plugin == NULL) {
		g_set_error(error, shout_output_quark(), 0,
			    "couldn't find shout encoder plugin \"%s\"",
			    encoding);
209
		goto failure;
210
	}
211

212
	sd->encoder = encoder_init(encoder_plugin, param, error);
213
	if (sd->encoder == NULL)
214
		goto failure;
215 216 217 218 219 220

	if (strcmp(encoding, "mp3") == 0 || strcmp(encoding, "lame") == 0)
		shout_format = SHOUT_FORMAT_MP3;
	else
		shout_format = SHOUT_FORMAT_OGG;

221 222 223
	value = config_get_block_string(param, "protocol", NULL);
	if (value != NULL) {
		if (0 == strcmp(value, "shoutcast") &&
224 225 226 227
		    0 != strcmp(encoding, "mp3")) {
			g_set_error(error, shout_output_quark(), 0,
				    "you cannot stream \"%s\" to shoutcast, use mp3",
				    encoding);
228
			goto failure;
229
		} else if (0 == strcmp(value, "shoutcast"))
230
			protocol = SHOUT_PROTOCOL_ICY;
231
		else if (0 == strcmp(value, "icecast1"))
232
			protocol = SHOUT_PROTOCOL_XAUDIOCAST;
233
		else if (0 == strcmp(value, "icecast2"))
234
			protocol = SHOUT_PROTOCOL_HTTP;
235 236 237 238 239
		else {
			g_set_error(error, shout_output_quark(), 0,
				    "shout protocol \"%s\" is not \"shoutcast\" or "
				    "\"icecast1\"or \"icecast2\"",
				    value);
240
			goto failure;
241
		}
242 243 244 245
	} else {
		protocol = SHOUT_PROTOCOL_HTTP;
	}

Max Kellermann's avatar
Max Kellermann committed
246 247 248 249 250 251 252
	if (shout_set_host(sd->shout_conn, host) != SHOUTERR_SUCCESS ||
	    shout_set_port(sd->shout_conn, port) != SHOUTERR_SUCCESS ||
	    shout_set_password(sd->shout_conn, passwd) != SHOUTERR_SUCCESS ||
	    shout_set_mount(sd->shout_conn, mount) != SHOUTERR_SUCCESS ||
	    shout_set_name(sd->shout_conn, name) != SHOUTERR_SUCCESS ||
	    shout_set_user(sd->shout_conn, user) != SHOUTERR_SUCCESS ||
	    shout_set_public(sd->shout_conn, public) != SHOUTERR_SUCCESS ||
253
	    shout_set_format(sd->shout_conn, shout_format)
Avuton Olrich's avatar
Avuton Olrich committed
254
	    != SHOUTERR_SUCCESS ||
255
	    shout_set_protocol(sd->shout_conn, protocol) != SHOUTERR_SUCCESS ||
Max Kellermann's avatar
Max Kellermann committed
256
	    shout_set_agent(sd->shout_conn, "MPD") != SHOUTERR_SUCCESS) {
257 258
		g_set_error(error, shout_output_quark(), 0,
			    "%s", shout_get_error(sd->shout_conn));
259
		goto failure;
260
	}
261

262
	/* optional paramters */
263 264
	sd->timeout = config_get_block_unsigned(param, "timeout",
						DEFAULT_CONN_TIMEOUT);
265

266 267
	value = config_get_block_string(param, "genre", NULL);
	if (value != NULL && shout_set_genre(sd->shout_conn, value)) {
268 269
		g_set_error(error, shout_output_quark(), 0,
			    "%s", shout_get_error(sd->shout_conn));
270
		goto failure;
271 272
	}

273 274
	value = config_get_block_string(param, "description", NULL);
	if (value != NULL && shout_set_description(sd->shout_conn, value)) {
275 276
		g_set_error(error, shout_output_quark(), 0,
			    "%s", shout_get_error(sd->shout_conn));
277
		goto failure;
278 279
	}

280 281 282 283
	value = config_get_block_string(param, "url", NULL);
	if (value != NULL && shout_set_url(sd->shout_conn, value)) {
		g_set_error(error, shout_output_quark(), 0,
			    "%s", shout_get_error(sd->shout_conn));
284
		goto failure;
285 286
	}

287 288 289
	{
		char temp[11];
		memset(temp, 0, sizeof(temp));
Avuton Olrich's avatar
Avuton Olrich committed
290

291
		snprintf(temp, sizeof(temp), "%u", audio_format->channels);
Max Kellermann's avatar
Max Kellermann committed
292
		shout_set_audio_info(sd->shout_conn, SHOUT_AI_CHANNELS, temp);
293

294
		snprintf(temp, sizeof(temp), "%u", audio_format->sample_rate);
Avuton Olrich's avatar
Avuton Olrich committed
295

Max Kellermann's avatar
Max Kellermann committed
296
		shout_set_audio_info(sd->shout_conn, SHOUT_AI_SAMPLERATE, temp);
297

298
		if (sd->quality >= -1.0) {
299
			snprintf(temp, sizeof(temp), "%2.2f", sd->quality);
Max Kellermann's avatar
Max Kellermann committed
300
			shout_set_audio_info(sd->shout_conn, SHOUT_AI_QUALITY,
Avuton Olrich's avatar
Avuton Olrich committed
301 302
					     temp);
		} else {
303
			snprintf(temp, sizeof(temp), "%d", sd->bitrate);
Max Kellermann's avatar
Max Kellermann committed
304
			shout_set_audio_info(sd->shout_conn, SHOUT_AI_BITRATE,
Avuton Olrich's avatar
Avuton Olrich committed
305
					     temp);
306 307 308
		}
	}

309
	return sd;
310 311 312 313

failure:
	free_shout_data(sd);
	return NULL;
314 315
}

316
static bool
317
handle_shout_error(struct shout_data *sd, int err, GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
318 319
{
	switch (err) {
320 321
	case SHOUTERR_SUCCESS:
		break;
322

323 324
	case SHOUTERR_UNCONNECTED:
	case SHOUTERR_SOCKET:
325 326 327 328 329
		g_set_error(error, shout_output_quark(), 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));
330 331
		return false;

332
	default:
333 334 335 336 337
		g_set_error(error, shout_output_quark(), 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));
338
		return false;
339 340
	}

341
	return true;
342 343
}

344
static bool
345
write_page(struct shout_data *sd, GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
346
{
347
	int err;
348

349 350 351 352
	assert(sd->encoder != NULL);

	sd->buf.len = encoder_read(sd->encoder,
				   sd->buf.data, sizeof(sd->buf.data));
353
	if (sd->buf.len == 0)
354
		return true;
355

356
	err = shout_send(sd->shout_conn, sd->buf.data, sd->buf.len);
357
	if (!handle_shout_error(sd, err, error))
358
		return false;
359

360
	return true;
361 362
}

363
static void close_shout_conn(struct shout_data * sd)
Avuton Olrich's avatar
Avuton Olrich committed
364
{
365 366
	sd->buf.len = 0;

367 368
	if (sd->encoder != NULL) {
		if (encoder_flush(sd->encoder, NULL))
369
			write_page(sd, NULL);
370 371 372

		encoder_close(sd->encoder);
	}
373

Max Kellermann's avatar
Max Kellermann committed
374 375
	if (shout_get_connected(sd->shout_conn) != SHOUTERR_UNCONNECTED &&
	    shout_close(sd->shout_conn) != SHOUTERR_SUCCESS) {
376 377
		g_warning("problem closing connection to shout server: %s\n",
			  shout_get_error(sd->shout_conn));
378 379 380
	}
}

381
static void my_shout_finish_driver(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
382
{
383
	struct shout_data *sd = (struct shout_data *)data;
384

385 386
	encoder_finish(sd->encoder);

Max Kellermann's avatar
Max Kellermann committed
387
	free_shout_data(sd);
388

Max Kellermann's avatar
Max Kellermann committed
389
	shout_init_count--;
390

Max Kellermann's avatar
Max Kellermann committed
391
	if (shout_init_count == 0)
Avuton Olrich's avatar
Avuton Olrich committed
392
		shout_shutdown();
393 394
}

395
static void my_shout_drop_buffered_audio(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
396
{
397
	G_GNUC_UNUSED
398
	struct shout_data *sd = (struct shout_data *)data;
399 400

	/* needs to be implemented for shout */
401 402
}

403
static void my_shout_close_device(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
404
{
405
	struct shout_data *sd = (struct shout_data *)data;
406

Max Kellermann's avatar
Max Kellermann committed
407
	close_shout_conn(sd);
408
}
409

410
static bool
411
shout_connect(struct shout_data *sd, GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
412
{
413
	int state;
414

Max Kellermann's avatar
Max Kellermann committed
415
	state = shout_open(sd->shout_conn);
416
	switch (state) {
417 418
	case SHOUTERR_SUCCESS:
	case SHOUTERR_CONNECTED:
419 420
		return true;

421
	default:
422 423 424 425 426
		g_set_error(error, shout_output_quark(), 0,
			    "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));
427
		return false;
428
	}
429 430
}

431
static bool
432 433
my_shout_open_device(void *data, struct audio_format *audio_format,
		     GError **error)
434
{
435
	struct shout_data *sd = (struct shout_data *)data;
436
	bool ret;
437

438
	ret = shout_connect(sd, error);
439 440
	if (!ret)
		return false;
441

442 443
	sd->buf.len = 0;

444 445
	ret = encoder_open(sd->encoder, audio_format, error) &&
		write_page(sd, error);
446
	if (!ret) {
Max Kellermann's avatar
Max Kellermann committed
447
		shout_close(sd->shout_conn);
448
		return false;
449 450
	}

451
	return true;
452 453
}

454 455 456 457 458 459 460 461 462 463 464 465
static unsigned
my_shout_delay(void *data)
{
	struct shout_data *sd = (struct shout_data *)data;

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

	return delay;
}

466
static size_t
467
my_shout_play(void *data, const void *chunk, size_t size, GError **error)
Avuton Olrich's avatar
Avuton Olrich committed
468
{
469
	struct shout_data *sd = (struct shout_data *)data;
470

471 472 473 474
	return encoder_write(sd->encoder, chunk, size, error) &&
		write_page(sd, error)
		? size
		: 0;
475 476
}

477 478
static bool
my_shout_pause(void *data)
479 480 481
{
	static const char silence[1020];

482
	return my_shout_play(data, silence, sizeof(silence), NULL);
483 484
}

485 486 487 488 489 490 491 492 493
static void
shout_tag_to_metadata(const struct tag *tag, char *dest, size_t size)
{
	char artist[size];
	char title[size];

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

Max Kellermann's avatar
Max Kellermann committed
494
	for (unsigned i = 0; i < tag->num_items; i++) {
495
		switch (tag->items[i]->type) {
496
		case TAG_ARTIST:
497 498
			strncpy(artist, tag->items[i]->value, size);
			break;
499
		case TAG_TITLE:
500 501 502 503 504 505 506 507
			strncpy(title, tag->items[i]->value, size);
			break;

		default:
			break;
		}
	}

508
	snprintf(dest, size, "%s - %s", artist, title);
509 510
}

511
static void my_shout_set_tag(void *data,
Max Kellermann's avatar
Max Kellermann committed
512
			     const struct tag *tag)
Avuton Olrich's avatar
Avuton Olrich committed
513
{
514
	struct shout_data *sd = (struct shout_data *)data;
515
	bool ret;
516 517 518 519 520
	GError *error = NULL;

	if (sd->encoder->plugin->tag != NULL) {
		/* encoder plugin supports stream tags */

521
		ret = encoder_pre_tag(sd->encoder, &error);
522 523 524 525 526 527
		if (!ret) {
			g_warning("%s", error->message);
			g_error_free(error);
			return;
		}

528 529 530
		ret = write_page(sd, NULL);
		if (!ret)
			return;
531 532 533 534 535 536 537 538 539 540 541

		ret = encoder_tag(sd->encoder, tag, &error);
		if (!ret) {
			g_warning("%s", error->message);
			g_error_free(error);
		}
	} else {
		/* no stream tag support: fall back to icy-metadata */
		char song[1024];

		shout_tag_to_metadata(tag, song, sizeof(song));
542

543 544 545 546 547 548
		shout_metadata_add(sd->shout_meta, "song", song);
		if (SHOUTERR_SUCCESS != shout_set_metadata(sd->shout_conn,
							   sd->shout_meta)) {
			g_warning("error setting shout metadata\n");
		}
	}
549

550
	write_page(sd, NULL);
551 552
}

553
const struct audio_output_plugin shoutPlugin = {
554 555 556 557
	.name = "shout",
	.init = my_shout_init_driver,
	.finish = my_shout_finish_driver,
	.open = my_shout_open_device,
558
	.delay = my_shout_delay,
559
	.play = my_shout_play,
560
	.pause = my_shout_pause,
561 562 563
	.cancel = my_shout_drop_buffered_audio,
	.close = my_shout_close_device,
	.send_tag = my_shout_set_tag,
564
};