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"
|
2008-09-07 22:41:22 +02:00
|
|
|
#include "output_api.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"
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "utils.h"
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2008-08-26 08:27:06 +02:00
|
|
|
#include "audio.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
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
void loadAudioOutputPlugin(struct audio_output_plugin *audioOutputPlugin)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
void unloadAudioOutputPlugin(struct audio_output_plugin *audioOutputPlugin)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
int initAudioOutput(struct audio_output *ao, ConfigParam * param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
void *data = NULL;
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *name = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *format = NULL;
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *type = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
BlockParam *bp = NULL;
|
2008-09-07 22:41:22 +02:00
|
|
|
struct audio_output_plugin *plugin = NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
plugin = (struct audio_output_plugin *) 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) {
|
2008-09-07 22:41:22 +02:00
|
|
|
plugin = (struct audio_output_plugin *) 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;
|
|
|
|
|
2008-09-07 19:19:55 +02:00
|
|
|
memset(&ao->inAudioFormat, 0, sizeof(ao->inAudioFormat));
|
|
|
|
memset(&ao->outAudioFormat, 0, sizeof(ao->outAudioFormat));
|
|
|
|
memset(&ao->reqAudioFormat, 0, sizeof(ao->reqAudioFormat));
|
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
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
int openAudioOutput(struct audio_output *audioOutput,
|
2008-09-07 19:19:55 +02:00
|
|
|
const struct audio_format *audioFormat)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-10-17 05:16:11 +02:00
|
|
|
int ret = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2007-06-09 21:19:33 +02:00
|
|
|
if (audioOutput->open &&
|
|
|
|
0 == cmpAudioFormat(audioFormat, &audioOutput->inAudioFormat)) {
|
|
|
|
return 0;
|
2004-11-02 18:05:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
copyAudioFormat(&audioOutput->inAudioFormat, audioFormat);
|
|
|
|
|
2007-06-09 21:19:33 +02:00
|
|
|
if (audioOutput->convertAudioFormat) {
|
2005-03-08 02:53:13 +01:00
|
|
|
copyAudioFormat(&audioOutput->outAudioFormat,
|
2007-06-09 21:19:33 +02:00
|
|
|
&audioOutput->reqAudioFormat);
|
|
|
|
} else {
|
2006-07-20 18:02:40 +02:00
|
|
|
copyAudioFormat(&audioOutput->outAudioFormat,
|
2007-06-09 21:19:33 +02:00
|
|
|
&audioOutput->inAudioFormat);
|
|
|
|
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,
|
2007-06-09 21:19:33 +02:00
|
|
|
&audioOutput->outAudioFormat);
|
2005-03-08 02:53:13 +01:00
|
|
|
|
|
|
|
return ret;
|
2004-10-20 18:05:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
static void convertAudioFormat(struct audio_output *audioOutput,
|
2008-04-12 06:15:52 +02:00
|
|
|
const char **chunkArgPtr, size_t *sizeArgPtr)
|
2004-11-02 18:05:27 +01:00
|
|
|
{
|
2008-04-12 06:15:40 +02:00
|
|
|
size_t size = pcm_sizeOfConvBuffer(&(audioOutput->inAudioFormat),
|
|
|
|
*sizeArgPtr,
|
|
|
|
&(audioOutput->outAudioFormat));
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-04-12 06:15:58 +02:00
|
|
|
if (size > audioOutput->convBufferLen) {
|
2008-04-12 06:16:03 +02:00
|
|
|
if (audioOutput->convBuffer != NULL)
|
|
|
|
free(audioOutput->convBuffer);
|
|
|
|
audioOutput->convBuffer = xmalloc(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;
|
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
int playAudioOutput(struct audio_output *audioOutput,
|
2008-04-12 06:15:52 +02:00
|
|
|
const char *playChunk, size_t size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
void dropBufferedAudioOutput(struct audio_output *audioOutput)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (audioOutput->open)
|
|
|
|
audioOutput->dropBufferedAudioFunc(audioOutput);
|
2005-03-05 15:01:13 +01:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
void closeAudioOutput(struct audio_output *audioOutput)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (audioOutput->open)
|
|
|
|
audioOutput->closeDeviceFunc(audioOutput);
|
2004-10-20 18:05:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
void finishAudioOutput(struct audio_output *audioOutput)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
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
|
|
|
|
2008-09-07 22:41:22 +02:00
|
|
|
void sendMetadataToAudioOutput(struct audio_output *audioOutput,
|
2008-08-29 09:38:11 +02:00
|
|
|
const struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
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;
|
2008-09-07 22:41:22 +02:00
|
|
|
struct audio_output_plugin *plugin;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
while (node) {
|
2008-09-07 22:41:22 +02:00
|
|
|
plugin = (struct audio_output_plugin *) 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
|
|
|
}
|