2006-07-13 21:20:34 +02:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2006-07-13 21:20:34 +02:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
#include <string.h>
|
2004-10-28 07:14:55 +02:00
|
|
|
|
|
|
|
#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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static List *audioOutputPluginList;
|
2004-10-10 15:51:33 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void loadAudioOutputPlugin(AudioOutputPlugin * audioOutputPlugin)
|
|
|
|
{
|
|
|
|
if (!audioOutputPlugin->name)
|
|
|
|
return;
|
2004-10-10 15:51:33 +02:00
|
|
|
insertInList(audioOutputPluginList, audioOutputPlugin->name,
|
2006-07-20 18:02:40 +02:00
|
|
|
audioOutputPlugin);
|
2004-10-10 15:51:33 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void unloadAudioOutputPlugin(AudioOutputPlugin * audioOutputPlugin)
|
|
|
|
{
|
|
|
|
if (!audioOutputPlugin->name)
|
|
|
|
return;
|
2004-10-10 15:51:33 +02:00
|
|
|
deleteFromList(audioOutputPluginList, audioOutputPlugin->name);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initAudioOutputPlugins(void)
|
|
|
|
{
|
2004-11-11 03:59:16 +01:00
|
|
|
audioOutputPluginList = makeList(NULL, 0);
|
2004-10-10 15:51:33 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishAudioOutputPlugins(void)
|
|
|
|
{
|
2004-10-10 15:51:33 +02:00
|
|
|
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) { \
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("couldn't find parameter \"%s\" in audio output " \
|
2006-08-11 23:50:56 +02:00
|
|
|
"definition beginning at %i\n", \
|
2004-10-28 07:14:55 +02:00
|
|
|
name, param->line); \
|
|
|
|
} \
|
2004-11-02 18:05:27 +01:00
|
|
|
if(bp) str = bp->value; \
|
2004-10-28 07:14:55 +02:00
|
|
|
}
|
|
|
|
|
2006-08-01 12:07:16 +02:00
|
|
|
int initAudioOutput(AudioOutput *ao, ConfigParam * param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
void *data = NULL;
|
|
|
|
char *name = NULL;
|
|
|
|
char *format = NULL;
|
|
|
|
char *type = NULL;
|
|
|
|
BlockParam *bp = NULL;
|
|
|
|
AudioOutputPlugin *plugin = NULL;
|
|
|
|
|
|
|
|
if (param) {
|
2005-03-12 04:10:09 +01:00
|
|
|
getBlockParam(AUDIO_OUTPUT_NAME, name, 1);
|
|
|
|
getBlockParam(AUDIO_OUTPUT_TYPE, type, 1);
|
2005-06-05 22:09:35 +02:00
|
|
|
getBlockParam(AUDIO_OUTPUT_FORMAT, format, 0);
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!findInList(audioOutputPluginList, type, &data)) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("couldn't find audio output plugin for type "
|
2006-07-20 18:02:40 +02:00
|
|
|
"\"%s\" at line %i\n", type, param->line);
|
2005-03-12 04:10:09 +01:00
|
|
|
}
|
2004-11-02 18:05:27 +01:00
|
|
|
|
2005-03-12 04:10:09 +01:00
|
|
|
plugin = (AudioOutputPlugin *) data;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
ListNode *node = audioOutputPluginList->firstNode;
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
WARNING("No \"%s\" defined in config file\n",
|
|
|
|
CONF_AUDIO_OUTPUT);
|
2005-03-12 04:10:09 +01:00
|
|
|
WARNING("Attempt to detect audio output device\n");
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (node) {
|
2005-03-12 04:10:09 +01:00
|
|
|
plugin = (AudioOutputPlugin *) node->data;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (plugin->testDefaultDeviceFunc) {
|
2005-03-12 04:10:09 +01:00
|
|
|
WARNING("Attempting to detect a %s audio "
|
2006-07-20 18:02:40 +02:00
|
|
|
"device\n", plugin->name);
|
|
|
|
if (plugin->testDefaultDeviceFunc() == 0) {
|
2005-03-12 04:10:09 +01:00
|
|
|
WARNING("Successfully detected a %s "
|
2006-07-20 18:02:40 +02:00
|
|
|
"audio device\n", plugin->name);
|
2005-03-12 04:10:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node = node->nextNode;
|
2004-11-02 18:05:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!node) {
|
2005-03-12 04:10:09 +01:00
|
|
|
WARNING("Unable to detect an audio device\n");
|
2006-08-01 12:07:16 +02:00
|
|
|
return 0;
|
2004-10-20 22:41:21 +02:00
|
|
|
}
|
2005-03-12 04:10:09 +01:00
|
|
|
|
|
|
|
name = "default detected output";
|
|
|
|
type = plugin->name;
|
2004-10-10 15:51:33 +02:00
|
|
|
}
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2006-08-01 12:07:16 +02:00
|
|
|
ao->name = name;
|
|
|
|
ao->type = type;
|
|
|
|
ao->finishDriverFunc = plugin->finishDriverFunc;
|
|
|
|
ao->openDeviceFunc = plugin->openDeviceFunc;
|
|
|
|
ao->playFunc = plugin->playFunc;
|
|
|
|
ao->dropBufferedAudioFunc = plugin->dropBufferedAudioFunc;
|
|
|
|
ao->closeDeviceFunc = plugin->closeDeviceFunc;
|
|
|
|
ao->sendMetdataFunc = plugin->sendMetdataFunc;
|
|
|
|
ao->open = 0;
|
|
|
|
|
|
|
|
ao->convertAudioFormat = 0;
|
|
|
|
ao->sameInAndOutFormats = 0;
|
|
|
|
ao->convBuffer = NULL;
|
|
|
|
ao->convBufferLen = 0;
|
|
|
|
|
|
|
|
memset(&ao->inAudioFormat, 0, sizeof(AudioFormat));
|
|
|
|
memset(&ao->outAudioFormat, 0, sizeof(AudioFormat));
|
|
|
|
memset(&ao->reqAudioFormat, 0, sizeof(AudioFormat));
|
2007-05-24 23:15:37 +02:00
|
|
|
memset(&ao->convState, 0, sizeof(ConvState));
|
2005-03-12 04:10:09 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (format) {
|
2006-08-01 12:07:16 +02:00
|
|
|
ao->convertAudioFormat = 1;
|
2005-05-29 21:19:20 +02:00
|
|
|
|
2006-08-01 12:07:16 +02:00
|
|
|
if (0 != parseAudioConfig(&ao->reqAudioFormat, format)) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("error parsing format at line %i\n", bp->line);
|
2005-05-29 21:19:20 +02:00
|
|
|
}
|
|
|
|
|
2006-08-01 12:07:16 +02:00
|
|
|
copyAudioFormat(&ao->outAudioFormat, &ao->reqAudioFormat);
|
2005-05-29 21:19:20 +02:00
|
|
|
}
|
|
|
|
|
2006-08-01 12:07:16 +02:00
|
|
|
if (plugin->initDriverFunc(ao, param) != 0)
|
|
|
|
return 0;
|
2004-10-10 15:51:33 +02:00
|
|
|
|
2006-08-01 12:07:16 +02:00
|
|
|
return 1;
|
2004-10-10 15:51:33 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int openAudioOutput(AudioOutput * audioOutput, AudioFormat * audioFormat)
|
|
|
|
{
|
2006-10-17 05:16:11 +02:00
|
|
|
int ret = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-10-17 05:16:11 +02:00
|
|
|
if (audioOutput->open)
|
|
|
|
{
|
|
|
|
if (0==cmpAudioFormat(audioFormat, &audioOutput->inAudioFormat))
|
|
|
|
{
|
2004-11-02 18:05:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
copyAudioFormat(&audioOutput->inAudioFormat, audioFormat);
|
|
|
|
|
2006-10-17 05:16:11 +02:00
|
|
|
if (audioOutput->convertAudioFormat)
|
|
|
|
{
|
2005-03-08 02:53:13 +01:00
|
|
|
copyAudioFormat(&audioOutput->outAudioFormat,
|
|
|
|
&audioOutput->reqAudioFormat);
|
2006-10-17 05:16:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
copyAudioFormat(&audioOutput->outAudioFormat,
|
2004-11-02 18:05:27 +01:00
|
|
|
&audioOutput->inAudioFormat);
|
2006-10-17 05:16:11 +02:00
|
|
|
if (audioOutput->open) closeAudioOutput(audioOutput);
|
2004-11-02 18:05:27 +01:00
|
|
|
}
|
|
|
|
|
2006-10-17 05:16:11 +02:00
|
|
|
if (!audioOutput->open)
|
|
|
|
{
|
|
|
|
ret = audioOutput->openDeviceFunc(audioOutput);
|
|
|
|
}
|
2005-03-08 02:53:13 +01:00
|
|
|
|
2006-10-17 05:16:11 +02:00
|
|
|
audioOutput->sameInAndOutFormats =
|
|
|
|
!cmpAudioFormat(&audioOutput->inAudioFormat,
|
|
|
|
&audioOutput->outAudioFormat);
|
2005-03-08 02:53:13 +01:00
|
|
|
|
|
|
|
return ret;
|
2004-10-20 18:05:13 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr,
|
|
|
|
int *sizeArgPtr)
|
2004-11-02 18:05:27 +01:00
|
|
|
{
|
2007-05-23 01:11:36 +02:00
|
|
|
int size = pcm_sizeOfConvBuffer(&(audioOutput->inAudioFormat),
|
|
|
|
*sizeArgPtr,
|
|
|
|
&(audioOutput->outAudioFormat));
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (size > audioOutput->convBufferLen) {
|
|
|
|
audioOutput->convBuffer =
|
2006-08-26 08:25:57 +02:00
|
|
|
xrealloc(audioOutput->convBuffer, size);
|
2004-11-02 18:05:27 +01:00
|
|
|
audioOutput->convBufferLen = size;
|
|
|
|
}
|
|
|
|
|
2007-05-26 18:39:55 +02:00
|
|
|
*sizeArgPtr = pcm_convertAudioFormat(&(audioOutput->inAudioFormat),
|
|
|
|
*chunkArgPtr, *sizeArgPtr,
|
|
|
|
&(audioOutput->outAudioFormat),
|
|
|
|
audioOutput->convBuffer,
|
|
|
|
&audioOutput->convState);
|
|
|
|
|
2004-11-02 18:05:27 +01:00
|
|
|
*chunkArgPtr = audioOutput->convBuffer;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int playAudioOutput(AudioOutput * audioOutput, char *playChunk, int size)
|
|
|
|
{
|
2004-11-08 22:54:50 +01:00
|
|
|
int ret;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!audioOutput->open)
|
|
|
|
return -1;
|
2004-11-02 18:05:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!audioOutput->sameInAndOutFormats) {
|
2004-11-02 18:05:27 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void dropBufferedAudioOutput(AudioOutput * audioOutput)
|
|
|
|
{
|
|
|
|
if (audioOutput->open)
|
|
|
|
audioOutput->dropBufferedAudioFunc(audioOutput);
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void closeAudioOutput(AudioOutput * audioOutput)
|
|
|
|
{
|
|
|
|
if (audioOutput->open)
|
|
|
|
audioOutput->closeDeviceFunc(audioOutput);
|
2004-10-20 18:05:13 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void finishAudioOutput(AudioOutput * audioOutput)
|
|
|
|
{
|
2004-10-20 19:11:04 +02:00
|
|
|
closeAudioOutput(audioOutput);
|
2007-05-30 20:31:38 +02:00
|
|
|
if (audioOutput->finishDriverFunc)
|
|
|
|
audioOutput->finishDriverFunc(audioOutput);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (audioOutput->convBuffer)
|
|
|
|
free(audioOutput->convBuffer);
|
2004-10-20 18:05:13 +02:00
|
|
|
}
|
2004-10-25 22:09:03 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void sendMetadataToAudioOutput(AudioOutput * audioOutput, MpdTag * tag)
|
|
|
|
{
|
|
|
|
if (!audioOutput->sendMetdataFunc)
|
|
|
|
return;
|
2004-10-25 22:09:03 +02:00
|
|
|
audioOutput->sendMetdataFunc(audioOutput, tag);
|
|
|
|
}
|
2006-07-16 16:57:26 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void printAllOutputPluginTypes(FILE * fp)
|
|
|
|
{
|
2006-07-16 16:57:26 +02:00
|
|
|
ListNode *node = audioOutputPluginList->firstNode;
|
|
|
|
AudioOutputPlugin *plugin;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
while (node) {
|
|
|
|
plugin = (AudioOutputPlugin *) node->data;
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, "%s ", plugin->name);
|
2006-07-16 16:57:26 +02:00
|
|
|
node = node->nextNode;
|
|
|
|
}
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, "\n");
|
|
|
|
fflush(fp);
|
2006-07-16 16:57:26 +02:00
|
|
|
}
|