Subversion Repositories mdb

Rev

Rev 33 | Rev 40 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33 Rev 35
Line 18... Line 18...
18
#include <strings.h>
18
#include <strings.h>
19
#include <unistd.h>
19
#include <unistd.h>
20
#include <stdlib.h>
20
#include <stdlib.h>
21
#include <syslog.h>
21
#include <syslog.h>
22
#include <ao/ao.h>
22
#include <ao/ao.h>
23
#include <FLAC/stream_decoder.h>
23
#include <FLAC/all.h>
24
 
24
 
25
#include "config.h"
25
#include "config.h"
26
#include "helplib.h"
26
#include "helplib.h"
27
#include "play.h"
27
#include "play.h"
28
#include "user.h"
28
#include "user.h"
Line 31... Line 31...
31
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, 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);
32
static void sdErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void * client_data);
33
 
33
 
34
FLAC__StreamDecoder *fsd;
34
FLAC__StreamDecoder *fsd;
35
static FLAC__uint64 total_samples = 0;
35
static FLAC__uint64 total_samples = 0;
-
 
36
static unsigned current_sample;
36
static unsigned sample_rate = 0;
37
static unsigned sample_rate = 0;
37
static unsigned channels = 0;
38
static unsigned channels = 0;
38
static unsigned bps = 0;
39
static unsigned bps = 0;
-
 
40
static unsigned total_seconds, elapsed_seconds, old_second;
39
static unsigned total_seconds;
41
static double f_el_time;
40
ao_device *dev;
42
ao_device *dev;
-
 
43
int last_todo;
41
 
44
 
42
struct st_params
45
struct st_params
43
{
46
{
44
	int s1;
47
	int s1;
45
	int driver;
48
	int driver;
Line 97... Line 100...
97
	
100
	
98
	(void)FLAC__stream_decoder_set_md5_checking(fsd, true);
101
	(void)FLAC__stream_decoder_set_md5_checking(fsd, true);
99
 
102
 
100
	params.s1 = s1;
103
	params.s1 = s1;
101
	params.driver = driver;
104
	params.driver = driver;
-
 
105
	current_sample = 0;
-
 
106
	old_second = 0;
-
 
107
	f_el_time = 0.0;
102
 
108
 
103
	if (FLAC__stream_decoder_init_file(fsd, file, sdWriteCallback, sdMetadataCallback, sdErrorCallback, (void *)&params) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
109
	if (FLAC__stream_decoder_init_file(fsd, file, sdWriteCallback, sdMetadataCallback, sdErrorCallback, (void *)&params) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
104
	{
110
	{
105
		syslog(LOG_DAEMON, "Error initializing file: %s [%s]", file, FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
111
		syslog(LOG_DAEMON, "Error initializing file: %s [%s]", file, FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
106
		FLAC__stream_decoder_finish(fsd);
112
		FLAC__stream_decoder_finish(fsd);
Line 109... Line 115...
109
		return;
115
		return;
110
	}
116
	}
111
 
117
 
112
	if (!FLAC__stream_decoder_process_until_end_of_stream(fsd))
118
	if (!FLAC__stream_decoder_process_until_end_of_stream(fsd))
113
	{
119
	{
-
 
120
		if (last_todo != PLAY_STATUS_STOP)
-
 
121
		{
114
		syslog(LOG_DAEMON, "Error processing stream: %s", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
122
			syslog(LOG_DAEMON, "Error processing stream: %s", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
115
		FLAC__stream_decoder_finish(fsd);
123
			FLAC__stream_decoder_finish(fsd);
116
		FLAC__stream_decoder_delete(fsd);
124
			FLAC__stream_decoder_delete(fsd);
117
		ao_shutdown();
125
			ao_shutdown();
-
 
126
			last_todo = 0;
118
		return;
127
			return;
-
 
128
		}
119
	}
129
	}
120
 
130
 
121
	/* clean up */
131
	/* clean up */
122
	strcpy(hv0, "PLAYER:STOP;");
132
	strcpy(hv0, "PLAYER:STOP;");
123
	write (s1, hv0, strlen(hv0));
133
	write (s1, hv0, strlen(hv0));
-
 
134
	last_todo = 0;
124
	
135
 
125
	ao_close(dev);
136
	ao_close(dev);
126
	FLAC__stream_decoder_finish(fsd);
137
	FLAC__stream_decoder_finish(fsd);
127
	FLAC__stream_decoder_delete(fsd);
138
	FLAC__stream_decoder_delete(fsd);
128
	ao_shutdown();
139
	ao_shutdown();
129
}
140
}
Line 131... Line 142...
131
FLAC__StreamDecoderWriteStatus sdWriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
142
FLAC__StreamDecoderWriteStatus sdWriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
132
{
143
{
133
struct st_params *params = (struct st_params *)client_data;
144
struct st_params *params = (struct st_params *)client_data;
134
int driver = params->driver;
145
int driver = params->driver;
135
int s1 = params->s1;
146
int s1 = params->s1;
136
const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps / 8));
147
//const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps / 8));
-
 
148
uint_32 samples = frame->header.blocksize;
-
 
149
uint_32 decoded_size = frame->header.blocksize * frame->header.channels * (bps / 8);
-
 
150
static uint_8 aobuf[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(uint_32)]; /*oink!*/
-
 
151
uint_16 *u16aobuf = (uint_16 *) aobuf;
-
 
152
uint_8   *u8aobuf = (uint_8  *) aobuf;
137
size_t i, j;
153
size_t i, j;
-
 
154
uint_32 sample, channel;
138
int todo;
155
int todo;
139
FLAC__uint16 *buf;
-
 
140
unsigned second;
-
 
141
 
156
 
142
	if (total_samples == 0) 
157
	if (total_samples == 0) 
143
	{
158
	{
144
		syslog(LOG_DAEMON, "ERROR: FLAC file have to have a total_samples count in STREAMINFO");
159
		syslog(LOG_DAEMON, "ERROR: FLAC file have to have a total_samples count in STREAMINFO");
145
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
160
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
146
	}
161
	}
147
 
162
 
148
	if (channels != 2 || bps != 16) 
163
	if (channels != 2 || (bps != 16 && bps != 8)) 
149
	{
164
	{
150
		syslog(LOG_DAEMON, "ERROR: Currently only 16bit stereo streams supported!");
165
		syslog(LOG_DAEMON, "ERROR: Currently only 8bit or 16bit streams supported!");
151
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
166
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
152
	}
167
	}
153
 
168
 
154
	if (frame->header.channels != 2)
-
 
155
	{
-
 
156
		syslog(LOG_DAEMON, "ERROR: This frame contains %d channels (should be 2)", frame->header.channels);
-
 
157
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
-
 
158
	}
-
 
159
 
-
 
160
	if (buffer [0] == NULL) 
169
	if (buffer[0] == NULL) 
161
	{
170
	{
162
		syslog(LOG_DAEMON, "ERROR: buffer [0] is NULL\n");
171
		syslog(LOG_DAEMON, "ERROR: buffer [0] is NULL\n");
163
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
172
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
164
	}
173
	}
165
 
174
 
166
	if(buffer[1] == NULL) 
175
	if (buffer[1] == NULL) 
167
	{
176
	{
168
		syslog(LOG_DAEMON, "ERROR: buffer [1] is NULL\n");
177
		syslog(LOG_DAEMON, "ERROR: buffer [1] is NULL\n");
169
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
178
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
170
	}
179
	}
171
 
180
 
172
	todo = check_command(s1);
181
	todo = check_command(s1);
173
 
182
 
174
	if (todo == PLAY_STATUS_STOP)
183
	if (todo == PLAY_STATUS_STOP)
-
 
184
	{
-
 
185
		last_todo = PLAY_STATUS_STOP;
175
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
186
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
-
 
187
	}
176
	else if (todo == PLAY_STATUS_FWD)
188
	else if (todo == PLAY_STATUS_FWD)
177
	{
189
	{
178
		FLAC__uint64 fr;
190
		FLAC__uint64 fr;
179
 
191
 
180
		FLAC__stream_decoder_get_decode_position(decoder, &fr);
192
		FLAC__stream_decoder_get_decode_position(decoder, &fr);
Line 216... Line 228...
216
		format.channels = channels;
228
		format.channels = channels;
217
		format.byte_format = AO_FMT_NATIVE;
229
		format.byte_format = AO_FMT_NATIVE;
218
		format.matrix = 0;
230
		format.matrix = 0;
219
 
231
 
220
		if ((dev = ao_open_live(driver, &format, NULL)) == NULL)
232
		if ((dev = ao_open_live(driver, &format, NULL)) == NULL)
-
 
233
		{
-
 
234
			switch (errno)
221
/*		{ */
235
			{
-
 
236
				case AO_ENODRIVER:	sprintf(hv0, "No driver corresponds to \"driver_id\"."); break;
-
 
237
				case AO_ENOTLIVE:	sprintf(hv0, "This driver (%s) is not a live output device.", configs.player); break;
-
 
238
				case AO_EBADOPTION:	sprintf(hv0, "A valid option key has an invalid value."); break;
-
 
239
				case AO_EOPENDEVICE:sprintf(hv0, "Cannot open the device."); break;
-
 
240
 
-
 
241
				default:
-
 
242
					sprintf(hv0, "%s", strerror(errno));
-
 
243
			}
-
 
244
			
222
			syslog(LOG_DAEMON, "Error opening live playback device: %s", strerror(errno));
245
			syslog(LOG_DAEMON, "Error opening live playback device: %s", hv0);
223
/*			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
246
			return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
224
		}
247
		}
225
*/
248
 
226
		playStatus = PLAY_STATUS_PLAY;
249
		playStatus = PLAY_STATUS_PLAY;
227
		strcpy (hv0, "PLAYER:PLAY;");
250
		strcpy (hv0, "PLAYER:PLAY;");
228
		write(s1, hv0, strlen(hv0));
251
		write(s1, hv0, strlen(hv0));
229
	}
252
	}
230
 
253
 
231
	/* allocate memory for the buffer */
254
	if (bps == 8)
232
	if ((buf = (FLAC__uint16 *)calloc(frame->header.blocksize * 2, sizeof(FLAC__uint16))) == NULL)
-
 
233
	{
255
	{
234
		syslog(LOG_DAEMON, "Error allocating memory for a buffer!");
256
		for (sample = i = 0; sample < samples; sample++) 
-
 
257
		{
-
 
258
			for (channel = 0; channel < frame->header.channels; channel++,i++)
-
 
259
			{
-
 
260
				/* 8 bit wav data is unsigned */
235
		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
261
				u8aobuf[i] = (uint_8)(buffer[channel][sample] + 0x80);
-
 
262
			}
-
 
263
		} 
236
	}
264
	}
237
 
-
 
238
	j = 0;
265
	else if (bps == 16)
239
 
-
 
240
	for (i = 0; i < frame->header.blocksize; i++)
-
 
241
	{
266
	{
242
		buf[j] = (FLAC__uint16)buffer[0][i];
267
		for (sample = i = 0; sample < samples; sample++)
-
 
268
		{
-
 
269
			for(channel = 0; channel < frame->header.channels; channel++,i++)
243
		buf[j+1] = (FLAC__uint16)buffer[1][i];
270
				u16aobuf[i] = (uint_16)(buffer[channel][sample]);
244
		j += 2;
271
		} 
245
	}
272
	}
246
 
273
 
-
 
274
	ao_play(dev, (char *)aobuf, decoded_size);
-
 
275
 
247
	if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
276
	if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
248
	{
277
	{
249
		char hv0[255], hv1[32], hv2[32], hv3[32];
278
		char hv0[255], hv1[32], hv2[32], hv3[32];
-
 
279
		double f_tm;
250
 
280
 
251
		second = frame->header.number.sample_number / sample_rate;
281
		current_sample += samples;
252
		sprintf(hv0, "POSITION:%s:%s:%s;", secondsToString(second, &hv1[0]), secondsToString(total_seconds - second, &hv2[0]), secondsToString(total_seconds, &hv3[0]));
282
		f_tm = (double)samples / (double)frame->header.sample_rate;
-
 
283
		f_el_time += f_tm;
253
		write(s1, hv0, strlen(hv0));
284
		elapsed_seconds = (unsigned)f_el_time;
254
	}
-
 
255
 
285
 
256
	ao_play(dev, (char *)buf, frame->header.blocksize);
286
		if (elapsed_seconds != old_second)
-
 
287
		{
-
 
288
			sprintf(hv0, "POSITION:%s:%s:%s;", secondsToString(elapsed_seconds, &hv1[0]), secondsToString(total_seconds - elapsed_seconds, &hv2[0]), secondsToString(total_seconds, &hv3[0]));
257
	free (buf);
289
			write(s1, hv0, strlen(hv0));
-
 
290
			old_second = elapsed_seconds;
-
 
291
		}
-
 
292
	}
258
 
293
 
259
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
294
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
260
}
295
}
261
 
296
 
262
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
297
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
263
{
298
{
264
	if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) 
299
	if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) 
265
	{
300
	{
266
		/* save for later */
301
		/* save for later */
-
 
302
		FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* we can handle < 4 gigasamples */
267
		total_samples = metadata->data.stream_info.total_samples;
303
		total_samples = metadata->data.stream_info.total_samples & 0xffffffff;	/* metadata->data.stream_info.total_samples; */
268
		sample_rate = metadata->data.stream_info.sample_rate;
304
		sample_rate = metadata->data.stream_info.sample_rate;
269
		channels = metadata->data.stream_info.channels;
305
		channels = metadata->data.stream_info.channels;
270
		bps = metadata->data.stream_info.bits_per_sample;
306
		bps = metadata->data.stream_info.bits_per_sample;
271
		total_seconds = (unsigned)total_samples / sample_rate;
307
		total_seconds = (unsigned)total_samples / sample_rate;
272
	}
308
	}