audioOutput_shout.c 15.7 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This project's homepage is: http://www.musicpd.org
 *
 * 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.
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "../output_api.h"
Warren Dukes's avatar
Warren Dukes committed
20 21 22

#ifdef HAVE_SHOUT

23
#include "../utils.h"
24
#include "../log.h"
25
#include "../timer.h"
26

27 28 29
#include <shout/shout.h>
#include <vorbis/vorbisenc.h>

30
#define CONN_ATTEMPT_INTERVAL 60
31
#define DEFAULT_CONN_TIMEOUT  2
32

33
static int shoutInitCount;
34

35 36
/* lots of this code blatantly stolent from bossogg/bossao2 */

37
typedef struct _ShoutData {
Avuton Olrich's avatar
Avuton Olrich committed
38
	shout_t *shoutConn;
39
	int shoutError;
40 41

	ogg_stream_state os;
42 43
	ogg_page og;
	ogg_packet op;
44 45 46
	ogg_packet header_main;
	ogg_packet header_comments;
	ogg_packet header_codebooks;
Avuton Olrich's avatar
Avuton Olrich committed
47

48 49 50 51
	vorbis_dsp_state vd;
	vorbis_block vb;
	vorbis_info vi;
	vorbis_comment vc;
52

53
	float quality;
54
	int bitrate;
55 56

	int opened;
57

58
	struct tag *tag;
59
	int tagToSend;
60

61
	int timeout;
62 63
	int connAttempts;
	time_t lastAttempt;
64

65 66
	Timer *timer;

67
	/* just a pointer to audioOutput->outAudioFormat */
68
	const struct audio_format *audioFormat;
69 70
} ShoutData;

Avuton Olrich's avatar
Avuton Olrich committed
71 72
static ShoutData *newShoutData(void)
{
73
	ShoutData *ret = xmalloc(sizeof(ShoutData));
74

75
	ret->shoutConn = shout_new();
76
	ret->opened = 0;
77
	ret->tag = NULL;
78
	ret->tagToSend = 0;
79
	ret->bitrate = -1;
80
	ret->quality = -2.0;
81
	ret->timeout = DEFAULT_CONN_TIMEOUT;
82 83
	ret->connAttempts = 0;
	ret->lastAttempt = 0;
84
	ret->audioFormat = NULL;
85
	ret->timer = NULL;
86

87 88 89
	return ret;
}

Avuton Olrich's avatar
Avuton Olrich committed
90 91 92 93 94
static void freeShoutData(ShoutData * sd)
{
	if (sd->shoutConn)
		shout_free(sd->shoutConn);
	if (sd->tag)
95
		tag_free(sd->tag);
96 97
	if (sd->timer)
		timer_free(sd->timer);
98

99 100 101
	free(sd);
}

102 103
#define checkBlockParam(name) { \
	blockParam = getBlockParam(param, name); \
104 105
	if (!blockParam) { \
		FATAL("no \"%s\" defined for shout device defined at line " \
106 107 108 109
				"%i\n", name, param->line); \
	} \
}

110 111
static int myShout_initDriver(struct audio_output *audioOutput,
			      ConfigParam * param)
Avuton Olrich's avatar
Avuton Olrich committed
112 113 114
{
	ShoutData *sd;
	char *test;
115
	int port;
Avuton Olrich's avatar
Avuton Olrich committed
116 117 118
	char *host;
	char *mount;
	char *passwd;
Max Kellermann's avatar
Max Kellermann committed
119
	const char *user;
Avuton Olrich's avatar
Avuton Olrich committed
120 121
	char *name;
	BlockParam *blockParam;
122
	int public;
123 124 125

	sd = newShoutData();

Avuton Olrich's avatar
Avuton Olrich committed
126 127
	if (shoutInitCount == 0)
		shout_init();
128 129 130

	shoutInitCount++;

131 132
	checkBlockParam("host");
	host = blockParam->value;
133

134 135
	checkBlockParam("mount");
	mount = blockParam->value;
136

137
	checkBlockParam("port");
138

139
	port = strtol(blockParam->value, &test, 10);
140

Avuton Olrich's avatar
Avuton Olrich committed
141
	if (*test != '\0' || port <= 0) {
142
		FATAL("shout port \"%s\" is not a positive integer, line %i\n",
Avuton Olrich's avatar
Avuton Olrich committed
143
		      blockParam->value, blockParam->line);
144 145
	}

146 147
	checkBlockParam("password");
	passwd = blockParam->value;
148

149 150
	checkBlockParam("name");
	name = blockParam->value;
151

152 153 154
	public = getBoolBlockParam(param, "public", 1);
	if (public == CONF_BOOL_UNSET)
		public = 0;
155 156

	blockParam = getBlockParam(param, "user");
Avuton Olrich's avatar
Avuton Olrich committed
157 158 159 160
	if (blockParam)
		user = blockParam->value;
	else
		user = "source";
161

162
	blockParam = getBlockParam(param, "quality");
163

Avuton Olrich's avatar
Avuton Olrich committed
164
	if (blockParam) {
165
		int line = blockParam->line;
166

167 168
		sd->quality = strtod(blockParam->value, &test);

169
		if (*test != '\0' || sd->quality < -1.0 || sd->quality > 10.0) {
170
			FATAL("shout quality \"%s\" is not a number in the "
171
			      "range -1 to 10, line %i\n", blockParam->value,
Avuton Olrich's avatar
Avuton Olrich committed
172
			      blockParam->line);
173 174 175 176
		}

		blockParam = getBlockParam(param, "bitrate");

Avuton Olrich's avatar
Avuton Olrich committed
177
		if (blockParam) {
178
			FATAL("quality (line %i) and bitrate (line %i) are "
Avuton Olrich's avatar
Avuton Olrich committed
179 180
			      "both defined for shout output\n", line,
			      blockParam->line);
181
		}
Avuton Olrich's avatar
Avuton Olrich committed
182
	} else {
183 184
		blockParam = getBlockParam(param, "bitrate");

Avuton Olrich's avatar
Avuton Olrich committed
185
		if (!blockParam) {
186
			FATAL("neither bitrate nor quality defined for shout "
Avuton Olrich's avatar
Avuton Olrich committed
187
			      "output at line %i\n", param->line);
188 189 190 191
		}

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

Avuton Olrich's avatar
Avuton Olrich committed
192
		if (*test != '\0' || sd->bitrate <= 0) {
193
			FATAL("bitrate at line %i should be a positive integer "
Avuton Olrich's avatar
Avuton Olrich committed
194
			      "\n", blockParam->line);
195
		}
196 197
	}

198
	checkBlockParam("format");
199
	sd->audioFormat = &audioOutput->outAudioFormat;
200

Avuton Olrich's avatar
Avuton Olrich committed
201 202 203 204 205 206 207
	if (shout_set_host(sd->shoutConn, host) != SHOUTERR_SUCCESS ||
	    shout_set_port(sd->shoutConn, port) != SHOUTERR_SUCCESS ||
	    shout_set_password(sd->shoutConn, passwd) != SHOUTERR_SUCCESS ||
	    shout_set_mount(sd->shoutConn, mount) != SHOUTERR_SUCCESS ||
	    shout_set_name(sd->shoutConn, name) != SHOUTERR_SUCCESS ||
	    shout_set_user(sd->shoutConn, user) != SHOUTERR_SUCCESS ||
	    shout_set_public(sd->shoutConn, public) != SHOUTERR_SUCCESS ||
208
	    shout_set_nonblocking(sd->shoutConn, 1) != SHOUTERR_SUCCESS ||
Avuton Olrich's avatar
Avuton Olrich committed
209 210 211 212 213
	    shout_set_format(sd->shoutConn, SHOUT_FORMAT_VORBIS)
	    != SHOUTERR_SUCCESS ||
	    shout_set_protocol(sd->shoutConn, SHOUT_PROTOCOL_HTTP)
	    != SHOUTERR_SUCCESS ||
	    shout_set_agent(sd->shoutConn, "MPD") != SHOUTERR_SUCCESS) {
214
		FATAL("error configuring shout defined at line %i: %s\n",
Avuton Olrich's avatar
Avuton Olrich committed
215
		      param->line, shout_get_error(sd->shoutConn));
216
	}
217

218
	/* optional paramters */
219 220
	blockParam = getBlockParam(param, "timeout");
	if (blockParam) {
221
		sd->timeout = (int)strtol(blockParam->value, &test, 10);
222 223 224 225 226 227
		if (*test != '\0' || sd->timeout <= 0) {
			FATAL("shout timeout is not a positive integer, "
			      "line %i\n", blockParam->line);
		}
	}

228
	blockParam = getBlockParam(param, "genre");
Avuton Olrich's avatar
Avuton Olrich committed
229
	if (blockParam && shout_set_genre(sd->shoutConn, blockParam->value)) {
230
		FATAL("error configuring shout defined at line %i: %s\n",
Avuton Olrich's avatar
Avuton Olrich committed
231
		      param->line, shout_get_error(sd->shoutConn));
232 233 234
	}

	blockParam = getBlockParam(param, "description");
Avuton Olrich's avatar
Avuton Olrich committed
235 236
	if (blockParam && shout_set_description(sd->shoutConn,
						blockParam->value)) {
237
		FATAL("error configuring shout defined at line %i: %s\n",
Avuton Olrich's avatar
Avuton Olrich committed
238
		      param->line, shout_get_error(sd->shoutConn));
239 240
	}

241 242 243
	{
		char temp[11];
		memset(temp, 0, sizeof(temp));
Avuton Olrich's avatar
Avuton Olrich committed
244

245
		snprintf(temp, sizeof(temp), "%d", sd->audioFormat->channels);
246 247
		shout_set_audio_info(sd->shoutConn, SHOUT_AI_CHANNELS, temp);

248
		snprintf(temp, sizeof(temp), "%d", sd->audioFormat->sampleRate);
Avuton Olrich's avatar
Avuton Olrich committed
249

250 251
		shout_set_audio_info(sd->shoutConn, SHOUT_AI_SAMPLERATE, temp);

252
		if (sd->quality >= -1.0) {
253 254
			snprintf(temp, sizeof(temp), "%2.2f", sd->quality);
			shout_set_audio_info(sd->shoutConn, SHOUT_AI_QUALITY,
Avuton Olrich's avatar
Avuton Olrich committed
255 256
					     temp);
		} else {
257 258
			snprintf(temp, sizeof(temp), "%d", sd->bitrate);
			shout_set_audio_info(sd->shoutConn, SHOUT_AI_BITRATE,
Avuton Olrich's avatar
Avuton Olrich committed
259
					     temp);
260 261 262
		}
	}

263
	audioOutput->data = sd;
264 265

	return 0;
266 267
}

Avuton Olrich's avatar
Avuton Olrich committed
268 269 270
static int myShout_handleError(ShoutData * sd, int err)
{
	switch (err) {
271 272 273 274
	case SHOUTERR_SUCCESS:
		break;
	case SHOUTERR_UNCONNECTED:
	case SHOUTERR_SOCKET:
275
		ERROR("Lost shout connection to %s:%i: %s\n",
Avuton Olrich's avatar
Avuton Olrich committed
276 277 278
		      shout_get_host(sd->shoutConn),
		      shout_get_port(sd->shoutConn),
		      shout_get_error(sd->shoutConn));
279 280 281
		sd->shoutError = 1;
		return -1;
	default:
282
		ERROR("shout: connection to %s:%i error: %s\n",
Avuton Olrich's avatar
Avuton Olrich committed
283 284 285
		      shout_get_host(sd->shoutConn),
		      shout_get_port(sd->shoutConn),
		      shout_get_error(sd->shoutConn));
286 287 288 289 290 291 292
		sd->shoutError = 1;
		return -1;
	}

	return 0;
}

Avuton Olrich's avatar
Avuton Olrich committed
293 294
static int write_page(ShoutData * sd)
{
295
	int err;
296 297 298

	shout_sync(sd->shoutConn);
	err = shout_send(sd->shoutConn, sd->og.header, sd->og.header_len);
Avuton Olrich's avatar
Avuton Olrich committed
299 300
	if (myShout_handleError(sd, err) < 0)
		return -1;
301
	err = shout_send(sd->shoutConn, sd->og.body, sd->og.body_len);
Avuton Olrich's avatar
Avuton Olrich committed
302 303
	if (myShout_handleError(sd, err) < 0)
		return -1;
304 305 306 307

	return 0;
}

Avuton Olrich's avatar
Avuton Olrich committed
308 309
static void finishEncoder(ShoutData * sd)
{
310 311
	vorbis_analysis_wrote(&sd->vd, 0);

Avuton Olrich's avatar
Avuton Olrich committed
312
	while (vorbis_analysis_blockout(&sd->vd, &sd->vb) == 1) {
313 314
		vorbis_analysis(&sd->vb, NULL);
		vorbis_bitrate_addblock(&sd->vb);
Avuton Olrich's avatar
Avuton Olrich committed
315
		while (vorbis_bitrate_flushpacket(&sd->vd, &sd->op)) {
316 317 318 319 320
			ogg_stream_packetin(&sd->os, &sd->op);
		}
	}
}

Avuton Olrich's avatar
Avuton Olrich committed
321 322
static int flushEncoder(ShoutData * sd)
{
Warren Dukes's avatar
Warren Dukes committed
323
	return (ogg_stream_pageout(&sd->os, &sd->og) > 0);
324
}
325

Avuton Olrich's avatar
Avuton Olrich committed
326 327
static void clearEncoder(ShoutData * sd)
{
328
	finishEncoder(sd);
Avuton Olrich's avatar
Avuton Olrich committed
329 330 331
	while (1 == flushEncoder(sd)) {
		if (!sd->shoutError)
			write_page(sd);
332
	}
Warren Dukes's avatar
Warren Dukes committed
333 334 335 336 337 338

	vorbis_comment_clear(&sd->vc);
	ogg_stream_clear(&sd->os);
	vorbis_block_clear(&sd->vb);
	vorbis_dsp_clear(&sd->vd);
	vorbis_info_clear(&sd->vi);
339 340
}

Avuton Olrich's avatar
Avuton Olrich committed
341 342
static void myShout_closeShoutConn(ShoutData * sd)
{
343
	if (sd->opened)
344 345
		clearEncoder(sd);

346 347 348 349
	if (shout_get_connected(sd->shoutConn) != SHOUTERR_UNCONNECTED &&
	    shout_close(sd->shoutConn) != SHOUTERR_SUCCESS) {
		ERROR("problem closing connection to shout server: %s\n",
		      shout_get_error(sd->shoutConn));
350 351 352 353 354
	}

	sd->opened = 0;
}

355
static void myShout_finishDriver(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
356 357
{
	ShoutData *sd = (ShoutData *) audioOutput->data;
358

359
	myShout_closeShoutConn(sd);
360

361
	freeShoutData(sd);
362 363 364

	shoutInitCount--;

Avuton Olrich's avatar
Avuton Olrich committed
365 366
	if (shoutInitCount == 0)
		shout_shutdown();
367 368
}

369
static void myShout_dropBufferedAudio(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
370
{
371 372 373 374
	ShoutData *sd = (ShoutData *)audioOutput->data;
	timer_reset(sd->timer);

	/* needs to be implemented for shout */
375 376
}

377
static void myShout_closeDevice(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
378 379
{
	ShoutData *sd = (ShoutData *) audioOutput->data;
380 381 382

	myShout_closeShoutConn(sd);

383 384 385 386 387
	if (sd->timer) { 
		timer_free(sd->timer);
		sd->timer = NULL;
	}

388 389
	audioOutput->open = 0;
}
390

Max Kellermann's avatar
Max Kellermann committed
391 392 393 394 395 396 397
static void addTag(ShoutData *sd, const char *name, char *value)
{
	if (value) {
		union const_hack u;
		u.in = name;
		vorbis_comment_add_tag(&(sd->vc), u.out, value);
	}
398 399
}

Avuton Olrich's avatar
Avuton Olrich committed
400 401 402
static void copyTagToVorbisComment(ShoutData * sd)
{
	if (sd->tag) {
403 404
		int i;

Avuton Olrich's avatar
Avuton Olrich committed
405
		for (i = 0; i < sd->tag->numOfItems; i++) {
406
			switch (sd->tag->items[i]->type) {
407
			case TAG_ITEM_ARTIST:
408
				addTag(sd, "ARTIST", sd->tag->items[i]->value);
409 410
				break;
			case TAG_ITEM_ALBUM:
411
				addTag(sd, "ALBUM", sd->tag->items[i]->value);
412 413
				break;
			case TAG_ITEM_TITLE:
414
				addTag(sd, "TITLE", sd->tag->items[i]->value);
415
				break;
416 417
			default:
				break;
418 419
			}
		}
420 421 422
	}
}

Avuton Olrich's avatar
Avuton Olrich committed
423 424
static int initEncoder(ShoutData * sd)
{
425 426
	vorbis_info_init(&(sd->vi));

427
	if (sd->quality >= -1.0) {
Avuton Olrich's avatar
Avuton Olrich committed
428 429 430 431
		if (0 != vorbis_encode_init_vbr(&(sd->vi),
						sd->audioFormat->channels,
						sd->audioFormat->sampleRate,
						sd->quality * 0.1)) {
Avuton Olrich's avatar
Avuton Olrich committed
432
			ERROR("problem setting up vorbis encoder for shout\n");
433 434 435
			vorbis_info_clear(&(sd->vi));
			return -1;
		}
Avuton Olrich's avatar
Avuton Olrich committed
436 437 438 439 440
	} else {
		if (0 != vorbis_encode_init(&(sd->vi),
					    sd->audioFormat->channels,
					    sd->audioFormat->sampleRate, -1.0,
					    sd->bitrate * 1000, -1.0)) {
Avuton Olrich's avatar
Avuton Olrich committed
441
			ERROR("problem setting up vorbis encoder for shout\n");
442 443 444 445 446
			vorbis_info_clear(&(sd->vi));
			return -1;
		}
	}

447
	vorbis_analysis_init(&(sd->vd), &(sd->vi));
Avuton Olrich's avatar
Avuton Olrich committed
448
	vorbis_block_init(&(sd->vd), &(sd->vb));
449 450 451 452 453 454 455 456

	ogg_stream_init(&(sd->os), rand());

	vorbis_comment_init(&(sd->vc));

	return 0;
}

457
static int myShout_connect(ShoutData *sd)
Avuton Olrich's avatar
Avuton Olrich committed
458
{
459
	time_t t = time(NULL);
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	int state = shout_get_connected(sd->shoutConn);

	/* already connected */
	if (state == SHOUTERR_CONNECTED)
		return 0;

	/* waiting to connect */
	if (state == SHOUTERR_BUSY && sd->connAttempts != 0) {
		/* timeout waiting to connect */
		if ((t - sd->lastAttempt) > sd->timeout) {
			ERROR("timeout connecting to shout server %s:%i "
			      "(attempt %i)\n",
			      shout_get_host(sd->shoutConn),
			      shout_get_port(sd->shoutConn),
			      sd->connAttempts);
			return -1;
		}

		return 1;
	}

	/* we're in some funky state, so just reset it to unconnected */
	if (state != SHOUTERR_UNCONNECTED)
		shout_close(sd->shoutConn);
484

485
	/* throttle new connection attempts */
Avuton Olrich's avatar
Avuton Olrich committed
486
	if (sd->connAttempts != 0 &&
487
	    (t - sd->lastAttempt) <= CONN_ATTEMPT_INTERVAL) {
488 489 490
		return -1;
	}

491 492
	/* initiate a new connection */

493
	sd->connAttempts++;
494 495 496 497
	sd->lastAttempt = t;

	state = shout_open(sd->shoutConn);
	switch (state) {
498 499
	case SHOUTERR_SUCCESS:
	case SHOUTERR_CONNECTED:
500
		return 0;
501
	case SHOUTERR_BUSY:
502
		return 1;
503
	default:
504
		ERROR("problem opening connection to shout server %s:%i "
Avuton Olrich's avatar
Avuton Olrich committed
505 506 507 508
		      "(attempt %i): %s\n",
		      shout_get_host(sd->shoutConn),
		      shout_get_port(sd->shoutConn),
		      sd->connAttempts, shout_get_error(sd->shoutConn));
509 510
		return -1;
	}
511 512
}

513
static int myShout_openShoutConn(struct audio_output *audioOutput)
514 515 516 517 518 519 520
{
	ShoutData *sd = (ShoutData *) audioOutput->data;
	int status;

	status = myShout_connect(sd);
	if (status != 0)
		return status;
521

Avuton Olrich's avatar
Avuton Olrich committed
522
	if (initEncoder(sd) < 0) {
523
		shout_close(sd->shoutConn);
524 525 526
		return -1;
	}

527 528
	sd->shoutError = 0;

529 530
	copyTagToVorbisComment(sd);

531
	vorbis_analysis_headerout(&(sd->vd), &(sd->vc), &(sd->header_main),
Avuton Olrich's avatar
Avuton Olrich committed
532 533
				  &(sd->header_comments),
				  &(sd->header_codebooks));
534 535 536 537 538

	ogg_stream_packetin(&(sd->os), &(sd->header_main));
	ogg_stream_packetin(&(sd->os), &(sd->header_comments));
	ogg_stream_packetin(&(sd->os), &(sd->header_codebooks));

539 540 541
	sd->opened = 1;
	sd->tagToSend = 0;

Avuton Olrich's avatar
Avuton Olrich committed
542 543
	while (ogg_stream_flush(&(sd->os), &(sd->og))) {
		if (write_page(sd) < 0) {
544 545 546
			myShout_closeShoutConn(sd);
			return -1;
		}
547 548
	}

549
	sd->connAttempts = 0;
550

551 552 553
	return 0;
}

554
static int myShout_openDevice(struct audio_output *audioOutput)
Avuton Olrich's avatar
Avuton Olrich committed
555 556
{
	ShoutData *sd = (ShoutData *) audioOutput->data;
557

558
	if (!sd->opened && myShout_openShoutConn(audioOutput) < 0)
559
		return -1;
560

561 562 563 564 565
	if (sd->timer)
		timer_free(sd->timer);

	sd->timer = timer_new(&audioOutput->outAudioFormat);

566
	audioOutput->open = 1;
567 568

	return 0;
569 570
}

Avuton Olrich's avatar
Avuton Olrich committed
571 572 573 574
static void myShout_sendMetadata(ShoutData * sd)
{
	if (!sd->opened || !sd->tag)
		return;
575 576

	clearEncoder(sd);
Avuton Olrich's avatar
Avuton Olrich committed
577 578
	if (initEncoder(sd) < 0)
		return;
579

580
	copyTagToVorbisComment(sd);
581 582

	vorbis_analysis_headerout(&(sd->vd), &(sd->vc), &(sd->header_main),
Avuton Olrich's avatar
Avuton Olrich committed
583 584
				  &(sd->header_comments),
				  &(sd->header_codebooks));
585 586 587 588 589

	ogg_stream_packetin(&(sd->os), &(sd->header_main));
	ogg_stream_packetin(&(sd->os), &(sd->header_comments));
	ogg_stream_packetin(&(sd->os), &(sd->header_codebooks));

Avuton Olrich's avatar
Avuton Olrich committed
590 591
	while (ogg_stream_flush(&(sd->os), &(sd->og))) {
		if (write_page(sd) < 0) {
592 593 594
			myShout_closeShoutConn(sd);
			return;
		}
595 596
	}

597
	/*if(sd->tag) freeMpdTag(sd->tag);
Avuton Olrich's avatar
Avuton Olrich committed
598
	   sd->tag = NULL; */
599
	sd->tagToSend = 0;
600 601
}

602
static int myShout_play(struct audio_output *audioOutput,
603
			const char *playChunk, size_t size)
Avuton Olrich's avatar
Avuton Olrich committed
604
{
605 606
	unsigned int i;
	int j;
Avuton Olrich's avatar
Avuton Olrich committed
607 608
	ShoutData *sd = (ShoutData *) audioOutput->data;
	float **vorbbuf;
609
	unsigned int samples;
Avuton Olrich's avatar
Avuton Olrich committed
610
	int bytes = sd->audioFormat->bits / 8;
611 612 613 614 615 616
	int status;

	if (!sd->timer->started)
		timer_start(sd->timer);

	timer_add(sd->timer, size);
617

Avuton Olrich's avatar
Avuton Olrich committed
618 619
	if (sd->opened && sd->tagToSend)
		myShout_sendMetadata(sd);
620

621 622 623 624 625 626 627 628 629
	if (!sd->opened) {
		status = myShout_openShoutConn(audioOutput);
		if (status < 0) {
			myShout_closeDevice(audioOutput);
			return -1;
		} else if (status > 0) {
			timer_sync(sd->timer);
			return 0;
		}
630 631
	}

Avuton Olrich's avatar
Avuton Olrich committed
632
	samples = size / (bytes * sd->audioFormat->channels);
633

634 635 636 637
	/* this is for only 16-bit audio */

	vorbbuf = vorbis_analysis_buffer(&(sd->vd), samples);

Avuton Olrich's avatar
Avuton Olrich committed
638 639
	for (i = 0; i < samples; i++) {
		for (j = 0; j < sd->audioFormat->channels; j++) {
640
			vorbbuf[j][i] = (*((const mpd_sint16 *) playChunk)) / 32768.0;
641 642
			playChunk += bytes;
		}
643 644
	}

645
	vorbis_analysis_wrote(&(sd->vd), samples);
646

Avuton Olrich's avatar
Avuton Olrich committed
647
	while (1 == vorbis_analysis_blockout(&(sd->vd), &(sd->vb))) {
648 649 650
		vorbis_analysis(&(sd->vb), NULL);
		vorbis_bitrate_addblock(&(sd->vb));

Avuton Olrich's avatar
Avuton Olrich committed
651
		while (vorbis_bitrate_flushpacket(&(sd->vd), &(sd->op))) {
652 653 654 655
			ogg_stream_packetin(&(sd->os), &(sd->op));
		}
	}

Avuton Olrich's avatar
Avuton Olrich committed
656 657
	while (ogg_stream_pageout(&(sd->os), &(sd->og)) != 0) {
		if (write_page(sd) < 0) {
658
			myShout_closeDevice(audioOutput);
659 660
			return -1;
		}
Warren Dukes's avatar
Warren Dukes committed
661 662
	}

663 664 665
	return 0;
}

666 667
static void myShout_setTag(struct audio_output *audioOutput,
			   const struct tag *tag)
Avuton Olrich's avatar
Avuton Olrich committed
668 669
{
	ShoutData *sd = (ShoutData *) audioOutput->data;
670

Avuton Olrich's avatar
Avuton Olrich committed
671
	if (sd->tag)
672
		tag_free(sd->tag);
673
	sd->tag = NULL;
674
	sd->tagToSend = 0;
675

Avuton Olrich's avatar
Avuton Olrich committed
676 677
	if (!tag)
		return;
678

679
	sd->tag = tag_dup(tag);
680
	sd->tagToSend = 1;
681 682
}

683
const struct audio_output_plugin shoutPlugin = {
684
	"shout",
685
	NULL,
686 687 688 689
	myShout_initDriver,
	myShout_finishDriver,
	myShout_openDevice,
	myShout_play,
690
	myShout_dropBufferedAudio,
691
	myShout_closeDevice,
692
	myShout_setTag,
693
};
Warren Dukes's avatar
Warren Dukes committed
694 695 696

#else

697
DISABLED_AUDIO_OUTPUT_PLUGIN(shoutPlugin)
Warren Dukes's avatar
Warren Dukes committed
698
#endif