2004-10-28 07:14:55 +02:00
|
|
|
#include "audioOutput.h"
|
2004-10-10 15:51:33 +02:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
2004-11-02 18:05:27 +01:00
|
|
|
#include "pcm_utils.h"
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define AUDIO_OUTPUT_TYPE "type"
|
|
|
|
#define AUDIO_OUTPUT_NAME "name"
|
2004-11-02 18:05:27 +01:00
|
|
|
#define AUDIO_OUTPUT_FORMAT "format"
|
2004-10-10 15:51:33 +02:00
|
|
|
|
|
|
|
static List * audioOutputPluginList;
|
|
|
|
|
|
|
|
void loadAudioOutputPlugin(AudioOutputPlugin * audioOutputPlugin) {
|
2004-10-29 05:30:23 +02:00
|
|
|
if(!audioOutputPlugin->name) return;
|
2004-10-10 15:51:33 +02:00
|
|
|
insertInList(audioOutputPluginList, audioOutputPlugin->name,
|
|
|
|
audioOutputPlugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unloadAudioOutputPlugin(AudioOutputPlugin * audioOutputPlugin) {
|
2004-10-29 05:30:23 +02:00
|
|
|
if(!audioOutputPlugin->name) return;
|
2004-10-10 15:51:33 +02:00
|
|
|
deleteFromList(audioOutputPluginList, audioOutputPlugin->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void initAudioOutputPlugins() {
|
|
|
|
audioOutputPluginList = makeList(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void finishAudioOutputPlugins() {
|
|
|
|
freeList(audioOutputPluginList);
|
|
|
|
}
|
|
|
|
|
2004-11-02 18:05:27 +01:00
|
|
|
#define getBlockParam(name, str, force) { \
|
2004-10-28 07:14:55 +02:00
|
|
|
bp = getBlockParam(param, name); \
|
2004-11-02 18:05:27 +01:00
|
|
|
if(force && bp == NULL) { \
|
2004-10-28 07:14:55 +02:00
|
|
|
ERROR("couldn't find parameter \"%s\" in audio output " \
|
|
|
|
"definition begining at %i\n", \
|
|
|
|
name, param->line); \
|
|
|
|
exit(EXIT_FAILURE); \
|
|
|
|
} \
|
2004-11-02 18:05:27 +01:00
|
|
|
if(bp) str = bp->value; \
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioOutput * newAudioOutput(ConfigParam * param) {
|
2004-10-10 15:51:33 +02:00
|
|
|
AudioOutput * ret = NULL;
|
|
|
|
void * data = NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
char * name = NULL;
|
2004-11-02 18:05:27 +01:00
|
|
|
char * format = NULL;
|
2004-10-28 07:14:55 +02:00
|
|
|
char * type = NULL;
|
2004-11-02 18:05:27 +01:00
|
|
|
BlockParam * bp;
|
2004-10-28 07:14:55 +02:00
|
|
|
|
2004-11-02 18:05:27 +01:00
|
|
|
getBlockParam(AUDIO_OUTPUT_NAME, name, 1);
|
|
|
|
getBlockParam(AUDIO_OUTPUT_TYPE, type, 1);
|
2004-10-10 15:51:33 +02:00
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
if(findInList(audioOutputPluginList, type, &data)) {
|
2004-10-10 15:51:33 +02:00
|
|
|
AudioOutputPlugin * plugin = (AudioOutputPlugin *) data;
|
|
|
|
ret = malloc(sizeof(AudioOutput));
|
2004-10-28 07:14:55 +02:00
|
|
|
ret->name = strdup(name);
|
|
|
|
ret->type = strdup(type);
|
2004-10-20 22:41:21 +02:00
|
|
|
ret->finishDriverFunc = plugin->finishDriverFunc;
|
2004-10-10 15:51:33 +02:00
|
|
|
ret->openDeviceFunc = plugin->openDeviceFunc;
|
|
|
|
ret->playFunc = plugin->playFunc;
|
|
|
|
ret->closeDeviceFunc = plugin->closeDeviceFunc;
|
2004-10-25 22:09:03 +02:00
|
|
|
ret->sendMetdataFunc = plugin->sendMetdataFunc;
|
2004-10-20 18:48:22 +02:00
|
|
|
ret->open = 0;
|
2004-10-20 18:05:13 +02:00
|
|
|
|
2004-11-02 18:05:27 +01:00
|
|
|
ret->convertAudioFormat = 0;
|
|
|
|
ret->sameInAndOutFormats = 0;
|
|
|
|
ret->convBuffer = NULL;
|
|
|
|
ret->convBufferLen = 0;
|
|
|
|
|
|
|
|
memset(&ret->inAudioFormat, 0, sizeof(AudioFormat));
|
|
|
|
memset(&ret->outAudioFormat, 0, sizeof(AudioFormat));
|
|
|
|
|
|
|
|
getBlockParam(AUDIO_OUTPUT_FORMAT, format, 0);
|
|
|
|
|
|
|
|
if(format) {
|
|
|
|
ret->convertAudioFormat = 1;
|
|
|
|
|
|
|
|
if(0 != parseAudioConfig(&ret->outAudioFormat, format))
|
|
|
|
{
|
|
|
|
ERROR("error parsing format at line %i\n",
|
|
|
|
bp->line);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-28 07:14:55 +02:00
|
|
|
if(plugin->initDriverFunc(ret, param) != 0) {
|
2004-10-20 22:41:21 +02:00
|
|
|
free(ret);
|
|
|
|
ret = NULL;
|
|
|
|
}
|
2004-10-10 15:51:33 +02:00
|
|
|
}
|
2004-10-28 07:14:55 +02:00
|
|
|
else {
|
|
|
|
ERROR("couldn't find audio output plugin for type \"%s\" at "
|
2004-11-02 03:03:00 +01:00
|
|
|
"line %i\n", type, param->line);
|
2004-10-28 07:14:55 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2004-10-10 15:51:33 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-10-20 18:05:13 +02:00
|
|
|
int openAudioOutput(AudioOutput * audioOutput, AudioFormat * audioFormat) {
|
2004-11-02 18:05:27 +01:00
|
|
|
if(audioOutput->open) {
|
|
|
|
if(cmpAudioFormat(audioFormat, &audioOutput->inAudioFormat)
|
|
|
|
== 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
closeAudioOutput(audioOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
copyAudioFormat(&audioOutput->inAudioFormat, audioFormat);
|
|
|
|
|
|
|
|
if(audioOutput->convertAudioFormat) {
|
|
|
|
if(cmpAudioFormat(&audioOutput->inAudioFormat,
|
|
|
|
&audioOutput->outAudioFormat) == 0)
|
|
|
|
{
|
|
|
|
audioOutput->sameInAndOutFormats = 1;
|
|
|
|
}
|
|
|
|
else audioOutput->sameInAndOutFormats = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
audioOutput->sameInAndOutFormats = 1;
|
|
|
|
copyAudioFormat(&audioOutput->outAudioFormat,
|
|
|
|
&audioOutput->inAudioFormat);
|
|
|
|
}
|
|
|
|
|
2004-10-20 18:05:13 +02:00
|
|
|
return audioOutput->openDeviceFunc(audioOutput, audioFormat);
|
|
|
|
}
|
|
|
|
|
2004-11-02 18:05:27 +01:00
|
|
|
static void convertAudioFormat(AudioOutput * audioOutput, char ** chunkArgPtr,
|
|
|
|
int * sizeArgPtr)
|
|
|
|
{
|
|
|
|
int size = pcm_sizeOfOutputBufferForAudioFormatConversion(
|
|
|
|
&(audioOutput->inAudioFormat), *sizeArgPtr,
|
|
|
|
&(audioOutput->outAudioFormat));
|
|
|
|
|
|
|
|
if(size > audioOutput->convBufferLen) {
|
|
|
|
audioOutput->convBuffer =
|
|
|
|
realloc(audioOutput->convBuffer, size);
|
|
|
|
audioOutput->convBufferLen = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm_convertAudioFormat(&(audioOutput->inAudioFormat), *chunkArgPtr,
|
|
|
|
*sizeArgPtr, &(audioOutput->outAudioFormat),
|
|
|
|
audioOutput->convBuffer);
|
|
|
|
|
|
|
|
*sizeArgPtr = size;
|
|
|
|
*chunkArgPtr = audioOutput->convBuffer;
|
|
|
|
}
|
|
|
|
|
2004-10-20 18:05:13 +02:00
|
|
|
int playAudioOutput(AudioOutput * audioOutput, char * playChunk, int size) {
|
2004-11-08 22:54:50 +01:00
|
|
|
int ret;
|
|
|
|
|
2004-10-20 19:11:04 +02:00
|
|
|
if(!audioOutput->open) return -1;
|
2004-11-02 18:05:27 +01:00
|
|
|
|
|
|
|
if(!audioOutput->sameInAndOutFormats) {
|
|
|
|
convertAudioFormat(audioOutput, &playChunk, &size);
|
|
|
|
}
|
2004-11-08 22:54:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
ret = audioOutput->playFunc(audioOutput, playChunk, size);
|
|
|
|
|
|
|
|
return ret;
|
2004-10-20 18:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void closeAudioOutput(AudioOutput * audioOutput) {
|
2004-10-20 19:11:04 +02:00
|
|
|
if(audioOutput->open) audioOutput->closeDeviceFunc(audioOutput);
|
2004-10-20 18:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void finishAudioOutput(AudioOutput * audioOutput) {
|
2004-10-20 19:11:04 +02:00
|
|
|
closeAudioOutput(audioOutput);
|
2004-10-20 18:05:13 +02:00
|
|
|
audioOutput->finishDriverFunc(audioOutput);
|
2004-11-02 18:05:27 +01:00
|
|
|
if(audioOutput->convBuffer) free(audioOutput->convBuffer);
|
2004-10-28 07:30:22 +02:00
|
|
|
free(audioOutput->type);
|
|
|
|
free(audioOutput->name);
|
2004-10-20 18:05:13 +02:00
|
|
|
free(audioOutput);
|
|
|
|
}
|
2004-10-25 22:09:03 +02:00
|
|
|
|
|
|
|
void sendMetadataToAudioOutput(AudioOutput * audioOutput, MpdTag * tag) {
|
2004-10-26 04:33:09 +02:00
|
|
|
if(!audioOutput->sendMetdataFunc) return;
|
2004-10-25 22:09:03 +02:00
|
|
|
audioOutput->sendMetdataFunc(audioOutput, tag);
|
|
|
|
}
|