Subversion Repositories mdb

Rev

Rev 35 | Rev 56 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
32 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 */
16
#include <stdio.h>
17
#include <string.h>
18
#include <strings.h>
19
#include <unistd.h>
20
#include <stdlib.h>
21
#include <syslog.h>
22
#include <ao/ao.h>
35 andreas 23
#include <FLAC/all.h>
32 andreas 24
 
25
#include "config.h"
26
#include "helplib.h"
27
#include "play.h"
28
#include "user.h"
29
 
30
static FLAC__StreamDecoderWriteStatus sdWriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
31
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
32
static void sdErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void * client_data);
33
 
34
FLAC__StreamDecoder *fsd;
35
static FLAC__uint64 total_samples = 0;
35 andreas 36
static unsigned current_sample;
32 andreas 37
static unsigned sample_rate = 0;
38
static unsigned channels = 0;
39
static unsigned bps = 0;
35 andreas 40
static unsigned total_seconds, elapsed_seconds, old_second;
41
static double f_el_time;
32 andreas 42
ao_device *dev;
35 andreas 43
int last_todo;
32 andreas 44
 
45
struct st_params
46
{
47
	int s1;
48
	int driver;
49
};
50
 
51
extern char aoOutPlayers[15][16];
52
 
53
void playFlac(int s1, char *file)
54
{
55
int i, flag;
56
int driver;
33 andreas 57
char hv0[64];
32 andreas 58
struct st_params params;
59
 
60
	/* Check if we've a valid sound driver defined */
61
	flag = FALSE;
62
	i = 0;
63
 
64
	while (aoOutPlayers[i][0])
65
	{
66
		if (!strcasecmp(aoOutPlayers[i], configs.player))
67
		{
68
			flag = TRUE;
69
			break;
70
		}
71
 
72
		i++;
73
	}
74
 
75
	/* initializations */
76
	ao_initialize();
77
 
78
	if (flag)
79
	{
80
		if ((driver = ao_driver_id(configs.player)) == -1)
81
		{
82
			syslog(LOG_DAEMON, "Error finding the audio out driver %s!", configs.player);
83
			ao_shutdown();
84
			return;
85
		}
86
	}
87
	else if ((driver = ao_default_driver_id()) == -1)
88
	{
89
		syslog(LOG_DAEMON, "Error finding a default audio driver!");
90
		ao_shutdown();
91
		return;
92
	}
93
 
94
	if ((fsd = FLAC__stream_decoder_new()) == NULL)
95
	{
96
		syslog(LOG_DAEMON, "Error allocating memory for a new instance of FLAC stream decoder!");
97
		ao_shutdown();
98
		return;
99
	}
100
 
101
	(void)FLAC__stream_decoder_set_md5_checking(fsd, true);
102
 
103
	params.s1 = s1;
104
	params.driver = driver;
35 andreas 105
	current_sample = 0;
106
	old_second = 0;
107
	f_el_time = 0.0;
32 andreas 108
 
33 andreas 109
	if (FLAC__stream_decoder_init_file(fsd, file, sdWriteCallback, sdMetadataCallback, sdErrorCallback, (void *)&params) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
32 andreas 110
	{
33 andreas 111
		syslog(LOG_DAEMON, "Error initializing file: %s [%s]", file, FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
32 andreas 112
		FLAC__stream_decoder_finish(fsd);
113
		FLAC__stream_decoder_delete(fsd);
114
		ao_shutdown();
115
		return;
116
	}
117
 
118
	if (!FLAC__stream_decoder_process_until_end_of_stream(fsd))
119
	{
35 andreas 120
		if (last_todo != PLAY_STATUS_STOP)
121
		{
40 andreas 122
			syslog(LOG_WARNING, "Error playing file %s", file);
123
			syslog(LOG_WARNING, "Error processing stream: %s", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
124
			ao_close(dev);
35 andreas 125
			FLAC__stream_decoder_finish(fsd);
126
			FLAC__stream_decoder_delete(fsd);
127
			ao_shutdown();
128
			last_todo = 0;
129
			return;
130
		}
32 andreas 131
	}
132
 
33 andreas 133
	/* clean up */
134
	strcpy(hv0, "PLAYER:STOP;");
135
	write (s1, hv0, strlen(hv0));
35 andreas 136
	last_todo = 0;
137
 
33 andreas 138
	ao_close(dev);
32 andreas 139
	FLAC__stream_decoder_finish(fsd);
140
	FLAC__stream_decoder_delete(fsd);
141
	ao_shutdown();
142
}
143
 
144
FLAC__StreamDecoderWriteStatus sdWriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
145
{
146
struct st_params *params = (struct st_params *)client_data;
147
int driver = params->driver;
148
int s1 = params->s1;
35 andreas 149
uint_32 samples = frame->header.blocksize;
150
uint_32 decoded_size = frame->header.blocksize * frame->header.channels * (bps / 8);
151
static uint_8 aobuf[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(uint_32)]; /*oink!*/
40 andreas 152
uint_32 *u32aobuf = (uint_32 *) aobuf;
35 andreas 153
uint_16 *u16aobuf = (uint_16 *) aobuf;
154
uint_8   *u8aobuf = (uint_8  *) aobuf;
32 andreas 155
size_t i, j;
35 andreas 156
uint_32 sample, channel;
32 andreas 157
int todo;
158
 
159
	if (total_samples == 0) 
160
	{
161
		syslog(LOG_DAEMON, "ERROR: FLAC file have to have a total_samples count in STREAMINFO");
162
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
163
	}
164
 
40 andreas 165
	if (channels <= 0 || (bps != 32 && bps != 16 && bps != 8))
32 andreas 166
	{
40 andreas 167
		syslog(LOG_DAEMON, "ERROR: Currently only 8bit, 16bit or 32bit streams supported!");
32 andreas 168
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
169
	}
170
 
35 andreas 171
	if (buffer[0] == NULL) 
32 andreas 172
	{
173
		syslog(LOG_DAEMON, "ERROR: buffer [0] is NULL\n");
174
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
175
	}
176
 
35 andreas 177
	if (buffer[1] == NULL) 
32 andreas 178
	{
179
		syslog(LOG_DAEMON, "ERROR: buffer [1] is NULL\n");
180
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
181
	}
182
 
183
	todo = check_command(s1);
184
 
185
	if (todo == PLAY_STATUS_STOP)
35 andreas 186
	{
187
		last_todo = PLAY_STATUS_STOP;
32 andreas 188
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
35 andreas 189
	}
32 andreas 190
	else if (todo == PLAY_STATUS_FWD)
191
	{
192
		FLAC__uint64 fr;
193
 
194
		FLAC__stream_decoder_get_decode_position(decoder, &fr);
195
		fr += 100;
196
		playStatus = PLAY_STATUS_PLAY;
197
 
198
		if (!FLAC__stream_decoder_seek_absolute(fsd, fr))
199
			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
200
 
201
		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
202
	}
203
	else if (todo == PLAY_STATUS_REW)
204
	{
205
		FLAC__uint64 fr;
206
 
207
		FLAC__stream_decoder_get_decode_position(decoder, &fr);
208
 
209
		if (fr > 100)
210
			fr -= 100;
211
		else
212
			fr = 0;
213
 
214
		playStatus = PLAY_STATUS_PLAY;
215
 
216
		if (!FLAC__stream_decoder_seek_absolute(fsd, fr))
217
			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
218
 
219
		return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
220
	}
221
 
33 andreas 222
	/* Initialize audio out device */
32 andreas 223
	if (frame->header.number.sample_number == 0) 
224
	{
225
		ao_sample_format format;
226
		char hv0[256];
227
		/* set the output format and open the output device */
228
		format.bits = bps;
229
		format.rate = sample_rate;
230
		format.channels = channels;
231
		format.byte_format = AO_FMT_NATIVE;
232
		format.matrix = 0;
33 andreas 233
 
32 andreas 234
		if ((dev = ao_open_live(driver, &format, NULL)) == NULL)
35 andreas 235
		{
236
			switch (errno)
237
			{
238
				case AO_ENODRIVER:	sprintf(hv0, "No driver corresponds to \"driver_id\"."); break;
239
				case AO_ENOTLIVE:	sprintf(hv0, "This driver (%s) is not a live output device.", configs.player); break;
240
				case AO_EBADOPTION:	sprintf(hv0, "A valid option key has an invalid value."); break;
241
				case AO_EOPENDEVICE:sprintf(hv0, "Cannot open the device."); break;
242
 
243
				default:
244
					sprintf(hv0, "%s", strerror(errno));
245
			}
246
 
247
			syslog(LOG_DAEMON, "Error opening live playback device: %s", hv0);
248
			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
32 andreas 249
		}
35 andreas 250
 
32 andreas 251
		playStatus = PLAY_STATUS_PLAY;
252
		strcpy (hv0, "PLAYER:PLAY;");
253
		write(s1, hv0, strlen(hv0));
254
	}
255
 
35 andreas 256
	if (bps == 8)
32 andreas 257
	{
35 andreas 258
		for (sample = i = 0; sample < samples; sample++) 
259
		{
260
			for (channel = 0; channel < frame->header.channels; channel++,i++)
261
			{
262
				/* 8 bit wav data is unsigned */
263
				u8aobuf[i] = (uint_8)(buffer[channel][sample] + 0x80);
264
			}
265
		} 
32 andreas 266
	}
35 andreas 267
	else if (bps == 16)
32 andreas 268
	{
35 andreas 269
		for (sample = i = 0; sample < samples; sample++)
270
		{
271
			for(channel = 0; channel < frame->header.channels; channel++,i++)
272
				u16aobuf[i] = (uint_16)(buffer[channel][sample]);
273
		} 
32 andreas 274
	}
40 andreas 275
	else if (bps == 32)
276
	{
277
		for (sample = i = 0; sample < samples; sample++)
278
		{
279
			for(channel = 0; channel < frame->header.channels; channel++,i++)
280
				u32aobuf[i] = (uint_32)(buffer[channel][sample]);
281
		} 
282
	}
283
 
35 andreas 284
	ao_play(dev, (char *)aobuf, decoded_size);
285
 
33 andreas 286
	if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
287
	{
288
		char hv0[255], hv1[32], hv2[32], hv3[32];
35 andreas 289
		double f_tm;
33 andreas 290
 
35 andreas 291
		current_sample += samples;
292
		f_tm = (double)samples / (double)frame->header.sample_rate;
293
		f_el_time += f_tm;
294
		elapsed_seconds = (unsigned)f_el_time;
295
 
296
		if (elapsed_seconds != old_second)
297
		{
298
			sprintf(hv0, "POSITION:%s:%s:%s;", secondsToString(elapsed_seconds, &hv1[0]), secondsToString(total_seconds - elapsed_seconds, &hv2[0]), secondsToString(total_seconds, &hv3[0]));
299
			write(s1, hv0, strlen(hv0));
300
			old_second = elapsed_seconds;
301
		}
33 andreas 302
	}
303
 
32 andreas 304
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
305
}
306
 
307
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
308
{
309
	if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) 
310
	{
311
		/* save for later */
35 andreas 312
		FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* we can handle < 4 gigasamples */
313
		total_samples = metadata->data.stream_info.total_samples & 0xffffffff;	/* metadata->data.stream_info.total_samples; */
32 andreas 314
		sample_rate = metadata->data.stream_info.sample_rate;
315
		channels = metadata->data.stream_info.channels;
316
		bps = metadata->data.stream_info.bits_per_sample;
33 andreas 317
		total_seconds = (unsigned)total_samples / sample_rate;
32 andreas 318
	}
319
}
320
 
321
static void sdErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void * client_data)
322
{
40 andreas 323
	syslog(LOG_WARNING, "Got error callback: %s", FLAC__StreamDecoderErrorStatusString[status]);
32 andreas 324
}