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 *)¶ms) != 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 |
{
|
|
|
122 |
syslog(LOG_DAEMON, "Error processing stream: %s", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
|
|
|
123 |
FLAC__stream_decoder_finish(fsd);
|
|
|
124 |
FLAC__stream_decoder_delete(fsd);
|
|
|
125 |
ao_shutdown();
|
|
|
126 |
last_todo = 0;
|
|
|
127 |
return;
|
|
|
128 |
}
|
32 |
andreas |
129 |
}
|
|
|
130 |
|
33 |
andreas |
131 |
/* clean up */
|
|
|
132 |
strcpy(hv0, "PLAYER:STOP;");
|
|
|
133 |
write (s1, hv0, strlen(hv0));
|
35 |
andreas |
134 |
last_todo = 0;
|
|
|
135 |
|
33 |
andreas |
136 |
ao_close(dev);
|
32 |
andreas |
137 |
FLAC__stream_decoder_finish(fsd);
|
|
|
138 |
FLAC__stream_decoder_delete(fsd);
|
|
|
139 |
ao_shutdown();
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
FLAC__StreamDecoderWriteStatus sdWriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
|
|
|
143 |
{
|
|
|
144 |
struct st_params *params = (struct st_params *)client_data;
|
|
|
145 |
int driver = params->driver;
|
|
|
146 |
int s1 = params->s1;
|
35 |
andreas |
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;
|
32 |
andreas |
153 |
size_t i, j;
|
35 |
andreas |
154 |
uint_32 sample, channel;
|
32 |
andreas |
155 |
int todo;
|
|
|
156 |
|
|
|
157 |
if (total_samples == 0)
|
|
|
158 |
{
|
|
|
159 |
syslog(LOG_DAEMON, "ERROR: FLAC file have to have a total_samples count in STREAMINFO");
|
|
|
160 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
161 |
}
|
|
|
162 |
|
35 |
andreas |
163 |
if (channels != 2 || (bps != 16 && bps != 8))
|
32 |
andreas |
164 |
{
|
35 |
andreas |
165 |
syslog(LOG_DAEMON, "ERROR: Currently only 8bit or 16bit streams supported!");
|
32 |
andreas |
166 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
167 |
}
|
|
|
168 |
|
35 |
andreas |
169 |
if (buffer[0] == NULL)
|
32 |
andreas |
170 |
{
|
|
|
171 |
syslog(LOG_DAEMON, "ERROR: buffer [0] is NULL\n");
|
|
|
172 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
173 |
}
|
|
|
174 |
|
35 |
andreas |
175 |
if (buffer[1] == NULL)
|
32 |
andreas |
176 |
{
|
|
|
177 |
syslog(LOG_DAEMON, "ERROR: buffer [1] is NULL\n");
|
|
|
178 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
todo = check_command(s1);
|
|
|
182 |
|
|
|
183 |
if (todo == PLAY_STATUS_STOP)
|
35 |
andreas |
184 |
{
|
|
|
185 |
last_todo = PLAY_STATUS_STOP;
|
32 |
andreas |
186 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
35 |
andreas |
187 |
}
|
32 |
andreas |
188 |
else if (todo == PLAY_STATUS_FWD)
|
|
|
189 |
{
|
|
|
190 |
FLAC__uint64 fr;
|
|
|
191 |
|
|
|
192 |
FLAC__stream_decoder_get_decode_position(decoder, &fr);
|
|
|
193 |
fr += 100;
|
|
|
194 |
playStatus = PLAY_STATUS_PLAY;
|
|
|
195 |
|
|
|
196 |
if (!FLAC__stream_decoder_seek_absolute(fsd, fr))
|
|
|
197 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
198 |
|
|
|
199 |
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
200 |
}
|
|
|
201 |
else if (todo == PLAY_STATUS_REW)
|
|
|
202 |
{
|
|
|
203 |
FLAC__uint64 fr;
|
|
|
204 |
|
|
|
205 |
FLAC__stream_decoder_get_decode_position(decoder, &fr);
|
|
|
206 |
|
|
|
207 |
if (fr > 100)
|
|
|
208 |
fr -= 100;
|
|
|
209 |
else
|
|
|
210 |
fr = 0;
|
|
|
211 |
|
|
|
212 |
playStatus = PLAY_STATUS_PLAY;
|
|
|
213 |
|
|
|
214 |
if (!FLAC__stream_decoder_seek_absolute(fsd, fr))
|
|
|
215 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
216 |
|
|
|
217 |
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
218 |
}
|
|
|
219 |
|
33 |
andreas |
220 |
/* Initialize audio out device */
|
32 |
andreas |
221 |
if (frame->header.number.sample_number == 0)
|
|
|
222 |
{
|
|
|
223 |
ao_sample_format format;
|
|
|
224 |
char hv0[256];
|
|
|
225 |
/* set the output format and open the output device */
|
|
|
226 |
format.bits = bps;
|
|
|
227 |
format.rate = sample_rate;
|
|
|
228 |
format.channels = channels;
|
|
|
229 |
format.byte_format = AO_FMT_NATIVE;
|
|
|
230 |
format.matrix = 0;
|
33 |
andreas |
231 |
|
32 |
andreas |
232 |
if ((dev = ao_open_live(driver, &format, NULL)) == NULL)
|
35 |
andreas |
233 |
{
|
|
|
234 |
switch (errno)
|
|
|
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 |
|
|
|
245 |
syslog(LOG_DAEMON, "Error opening live playback device: %s", hv0);
|
|
|
246 |
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
32 |
andreas |
247 |
}
|
35 |
andreas |
248 |
|
32 |
andreas |
249 |
playStatus = PLAY_STATUS_PLAY;
|
|
|
250 |
strcpy (hv0, "PLAYER:PLAY;");
|
|
|
251 |
write(s1, hv0, strlen(hv0));
|
|
|
252 |
}
|
|
|
253 |
|
35 |
andreas |
254 |
if (bps == 8)
|
32 |
andreas |
255 |
{
|
35 |
andreas |
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 */
|
|
|
261 |
u8aobuf[i] = (uint_8)(buffer[channel][sample] + 0x80);
|
|
|
262 |
}
|
|
|
263 |
}
|
32 |
andreas |
264 |
}
|
35 |
andreas |
265 |
else if (bps == 16)
|
32 |
andreas |
266 |
{
|
35 |
andreas |
267 |
for (sample = i = 0; sample < samples; sample++)
|
|
|
268 |
{
|
|
|
269 |
for(channel = 0; channel < frame->header.channels; channel++,i++)
|
|
|
270 |
u16aobuf[i] = (uint_16)(buffer[channel][sample]);
|
|
|
271 |
}
|
32 |
andreas |
272 |
}
|
|
|
273 |
|
35 |
andreas |
274 |
ao_play(dev, (char *)aobuf, decoded_size);
|
|
|
275 |
|
33 |
andreas |
276 |
if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
|
|
|
277 |
{
|
|
|
278 |
char hv0[255], hv1[32], hv2[32], hv3[32];
|
35 |
andreas |
279 |
double f_tm;
|
33 |
andreas |
280 |
|
35 |
andreas |
281 |
current_sample += samples;
|
|
|
282 |
f_tm = (double)samples / (double)frame->header.sample_rate;
|
|
|
283 |
f_el_time += f_tm;
|
|
|
284 |
elapsed_seconds = (unsigned)f_el_time;
|
|
|
285 |
|
|
|
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]));
|
|
|
289 |
write(s1, hv0, strlen(hv0));
|
|
|
290 |
old_second = elapsed_seconds;
|
|
|
291 |
}
|
33 |
andreas |
292 |
}
|
|
|
293 |
|
32 |
andreas |
294 |
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
|
|
|
298 |
{
|
|
|
299 |
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
|
|
|
300 |
{
|
|
|
301 |
/* save for later */
|
35 |
andreas |
302 |
FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* we can handle < 4 gigasamples */
|
|
|
303 |
total_samples = metadata->data.stream_info.total_samples & 0xffffffff; /* metadata->data.stream_info.total_samples; */
|
32 |
andreas |
304 |
sample_rate = metadata->data.stream_info.sample_rate;
|
|
|
305 |
channels = metadata->data.stream_info.channels;
|
|
|
306 |
bps = metadata->data.stream_info.bits_per_sample;
|
33 |
andreas |
307 |
total_seconds = (unsigned)total_samples / sample_rate;
|
32 |
andreas |
308 |
}
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
static void sdErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void * client_data)
|
|
|
312 |
{
|
|
|
313 |
syslog(LOG_DAEMON, "Got error callback: %s", FLAC__StreamDecoderErrorStatusString[status]);
|
|
|
314 |
}
|