Rev 32 | Rev 35 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
*
* All rights reserved. No warranty, explicit or implicit, provided.
*
* NOTICE: All information contained herein is, and remains
* the property of Andreas Theofilu and his suppliers, if any.
* The intellectual and technical concepts contained
* herein are proprietary to Andreas Theofilu and its suppliers and
* may be covered by European and Foreign Patents, patents in process,
* and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Andreas Theofilu.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <ao/ao.h>
#include <FLAC/stream_decoder.h>
#include "config.h"
#include "helplib.h"
#include "play.h"
#include "user.h"
static FLAC__StreamDecoderWriteStatus sdWriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void sdErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void * client_data);
FLAC__StreamDecoder *fsd;
static FLAC__uint64 total_samples = 0;
static unsigned sample_rate = 0;
static unsigned channels = 0;
static unsigned bps = 0;
static unsigned total_seconds;
ao_device *dev;
struct st_params
{
int s1;
int driver;
};
extern char aoOutPlayers[15][16];
void playFlac(int s1, char *file)
{
int i, flag;
int driver;
char hv0[64];
struct st_params params;
/* Check if we've a valid sound driver defined */
flag = FALSE;
i = 0;
while (aoOutPlayers[i][0])
{
if (!strcasecmp(aoOutPlayers[i], configs.player))
{
flag = TRUE;
break;
}
i++;
}
/* initializations */
ao_initialize();
if (flag)
{
if ((driver = ao_driver_id(configs.player)) == -1)
{
syslog(LOG_DAEMON, "Error finding the audio out driver %s!", configs.player);
ao_shutdown();
return;
}
}
else if ((driver = ao_default_driver_id()) == -1)
{
syslog(LOG_DAEMON, "Error finding a default audio driver!");
ao_shutdown();
return;
}
if ((fsd = FLAC__stream_decoder_new()) == NULL)
{
syslog(LOG_DAEMON, "Error allocating memory for a new instance of FLAC stream decoder!");
ao_shutdown();
return;
}
(void)FLAC__stream_decoder_set_md5_checking(fsd, true);
params.s1 = s1;
params.driver = driver;
if (FLAC__stream_decoder_init_file(fsd, file, sdWriteCallback, sdMetadataCallback, sdErrorCallback, (void *)¶ms) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
{
syslog(LOG_DAEMON, "Error initializing file: %s [%s]", file, FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
FLAC__stream_decoder_finish(fsd);
FLAC__stream_decoder_delete(fsd);
ao_shutdown();
return;
}
if (!FLAC__stream_decoder_process_until_end_of_stream(fsd))
{
syslog(LOG_DAEMON, "Error processing stream: %s", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
FLAC__stream_decoder_finish(fsd);
FLAC__stream_decoder_delete(fsd);
ao_shutdown();
return;
}
/* clean up */
strcpy(hv0, "PLAYER:STOP;");
write (s1, hv0, strlen(hv0));
ao_close(dev);
FLAC__stream_decoder_finish(fsd);
FLAC__stream_decoder_delete(fsd);
ao_shutdown();
}
FLAC__StreamDecoderWriteStatus sdWriteCallback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
struct st_params *params = (struct st_params *)client_data;
int driver = params->driver;
int s1 = params->s1;
const FLAC__uint32 total_size = (FLAC__uint32)(total_samples * channels * (bps / 8));
size_t i, j;
int todo;
FLAC__uint16 *buf;
unsigned second;
if (total_samples == 0)
{
syslog(LOG_DAEMON, "ERROR: FLAC file have to have a total_samples count in STREAMINFO");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (channels != 2 || bps != 16)
{
syslog(LOG_DAEMON, "ERROR: Currently only 16bit stereo streams supported!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (frame->header.channels != 2)
{
syslog(LOG_DAEMON, "ERROR: This frame contains %d channels (should be 2)", frame->header.channels);
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if (buffer [0] == NULL)
{
syslog(LOG_DAEMON, "ERROR: buffer [0] is NULL\n");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
if(buffer[1] == NULL)
{
syslog(LOG_DAEMON, "ERROR: buffer [1] is NULL\n");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
todo = check_command(s1);
if (todo == PLAY_STATUS_STOP)
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
else if (todo == PLAY_STATUS_FWD)
{
FLAC__uint64 fr;
FLAC__stream_decoder_get_decode_position(decoder, &fr);
fr += 100;
playStatus = PLAY_STATUS_PLAY;
if (!FLAC__stream_decoder_seek_absolute(fsd, fr))
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
else if (todo == PLAY_STATUS_REW)
{
FLAC__uint64 fr;
FLAC__stream_decoder_get_decode_position(decoder, &fr);
if (fr > 100)
fr -= 100;
else
fr = 0;
playStatus = PLAY_STATUS_PLAY;
if (!FLAC__stream_decoder_seek_absolute(fsd, fr))
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
/* Initialize audio out device */
if (frame->header.number.sample_number == 0)
{
ao_sample_format format;
char hv0[256];
/* set the output format and open the output device */
format.bits = bps;
format.rate = sample_rate;
format.channels = channels;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
if ((dev = ao_open_live(driver, &format, NULL)) == NULL)
/* { */
syslog(LOG_DAEMON, "Error opening live playback device: %s", strerror(errno));
/* return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
*/
playStatus = PLAY_STATUS_PLAY;
strcpy (hv0, "PLAYER:PLAY;");
write(s1, hv0, strlen(hv0));
}
/* allocate memory for the buffer */
if ((buf = (FLAC__uint16 *)calloc(frame->header.blocksize * 2, sizeof(FLAC__uint16))) == NULL)
{
syslog(LOG_DAEMON, "Error allocating memory for a buffer!");
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
j = 0;
for (i = 0; i < frame->header.blocksize; i++)
{
buf[j] = (FLAC__uint16)buffer[0][i];
buf[j+1] = (FLAC__uint16)buffer[1][i];
j += 2;
}
if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
{
char hv0[255], hv1[32], hv2[32], hv3[32];
second = frame->header.number.sample_number / sample_rate;
sprintf(hv0, "POSITION:%s:%s:%s;", secondsToString(second, &hv1[0]), secondsToString(total_seconds - second, &hv2[0]), secondsToString(total_seconds, &hv3[0]));
write(s1, hv0, strlen(hv0));
}
ao_play(dev, (char *)buf, frame->header.blocksize);
free (buf);
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
static void sdMetadataCallback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
{
/* save for later */
total_samples = metadata->data.stream_info.total_samples;
sample_rate = metadata->data.stream_info.sample_rate;
channels = metadata->data.stream_info.channels;
bps = metadata->data.stream_info.bits_per_sample;
total_seconds = (unsigned)total_samples / sample_rate;
}
}
static void sdErrorCallback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void * client_data)
{
syslog(LOG_DAEMON, "Got error callback: %s", FLAC__StreamDecoderErrorStatusString[status]);
}