Subversion Repositories mdb

Rev

Rev 56 | 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/all.h>

#include "config.h"
#include "helplib.h"
#include "play.h"
#include "user.h"
#include "mdb.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 current_sample;
static unsigned sample_rate = 0;
static unsigned channels = 0;
static unsigned bps = 0;
static unsigned total_seconds, elapsed_seconds, old_second;
static double f_el_time;
ao_device *dev;
int last_todo;

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;
        current_sample = 0;
        old_second = 0;
        f_el_time = 0.0;

        if (FLAC__stream_decoder_init_file(fsd, file, sdWriteCallback, sdMetadataCallback, sdErrorCallback, (void *)&params) != 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))
        {
                if (last_todo != PLAY_STATUS_STOP)
                {
                        syslog(LOG_WARNING, "Error playing file %s", file);
                        syslog(LOG_WARNING, "Error processing stream: %s", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(fsd)]);
                        ao_close(dev);
                        FLAC__stream_decoder_finish(fsd);
                        FLAC__stream_decoder_delete(fsd);
                        ao_shutdown();
                        last_todo = 0;
                        return;
                }
        }

        /* clean up */
        handleWrite("PLAYER:STOP;");
/*      write (s1, hv0, strlen(hv0)); */
        last_todo = 0;

        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;
uint_32 samples = frame->header.blocksize;
uint_32 decoded_size = frame->header.blocksize * frame->header.channels * (bps / 8);
static uint_8 aobuf[FLAC__MAX_BLOCK_SIZE * FLAC__MAX_CHANNELS * sizeof(uint_32)]; /*oink!*/
uint_32 *u32aobuf = (uint_32 *) aobuf;
uint_16 *u16aobuf = (uint_16 *) aobuf;
uint_8   *u8aobuf = (uint_8  *) aobuf;
size_t i;
uint_32 sample, channel;
int todo;

        if (total_samples == 0) 
        {
                syslog(LOG_DAEMON, "ERROR: FLAC file have to have a total samples count grater than 0 in STREAMINFO");
                return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
        }

        if (channels <= 0 || (bps != 32 && bps != 16 && bps != 8))
        {
                syslog(LOG_DAEMON, "ERROR: Currently only 8bit, 16bit or 32bit streams supported!");
                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)
        {
                last_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)
                {
                        switch (errno)
                        {
                                case AO_ENODRIVER:      sprintf(hv0, "No driver corresponds to \"driver_id\"."); break;
                                case AO_ENOTLIVE:       sprintf(hv0, "This driver (%s) is not a live output device.", configs.player); break;
                                case AO_EBADOPTION:     sprintf(hv0, "A valid option key has an invalid value."); break;
                                case AO_EOPENDEVICE:sprintf(hv0, "Cannot open the device."); break;

                                default:
                                        sprintf(hv0, "%s", strerror(errno));
                        }
                        
                        syslog(LOG_DAEMON, "Error opening live playback device: %s", hv0);
                        return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
                }

                playStatus = PLAY_STATUS_PLAY;
                handleWrite("PLAYER:PLAY;");
/*              write(s1, hv0, strlen(hv0)); */
        }

        if (bps == 8)
        {
                for (sample = i = 0; sample < samples; sample++) 
                {
                        for (channel = 0; channel < frame->header.channels; channel++,i++)
                        {
                                /* 8 bit wav data is unsigned */
                                u8aobuf[i] = (uint_8)(buffer[channel][sample] + 0x80);
                        }
                } 
        }
        else if (bps == 16)
        {
                for (sample = i = 0; sample < samples; sample++)
                {
                        for(channel = 0; channel < frame->header.channels; channel++,i++)
                                u16aobuf[i] = (uint_16)(buffer[channel][sample]);
                } 
        }
        else if (bps == 32)
        {
                for (sample = i = 0; sample < samples; sample++)
                {
                        for(channel = 0; channel < frame->header.channels; channel++,i++)
                                u32aobuf[i] = (uint_32)(buffer[channel][sample]);
                } 
        }
        
        ao_play(dev, (char *)aobuf, decoded_size);

        if (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER)
        {
                char hv0[255], hv1[32], hv2[32], hv3[32];
                double f_tm;

                current_sample += samples;
                f_tm = (double)samples / (double)frame->header.sample_rate;
                f_el_time += f_tm;
                elapsed_seconds = (unsigned)f_el_time;

                if (elapsed_seconds != old_second)
                {
                        if (!playQuiet)
                        {
                                sprintf(hv0, "POSITION:%s:%s:%s;", secondsToString(elapsed_seconds, &hv1[0]), secondsToString(total_seconds - elapsed_seconds, &hv2[0]), secondsToString(total_seconds, &hv3[0]));
                                handleWrite(hv0);
                        /*      write(s1, hv0, strlen(hv0)); */
                        }

                        old_second = elapsed_seconds;
                }
        }

        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 */
                FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* we can handle < 4 gigasamples */
                total_samples = metadata->data.stream_info.total_samples & 0xffffffff;  /* 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_WARNING, "Got error callback: %s", FLAC__StreamDecoderErrorStatusString[status]);
}