1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "audioOutput.h"
23 #include "pcm_utils.h"
27 #define AUDIO_OUTPUT_TYPE "type"
28 #define AUDIO_OUTPUT_NAME "name"
29 #define AUDIO_OUTPUT_FORMAT "format"
31 static List
*audioOutputPluginList
;
33 void loadAudioOutputPlugin(AudioOutputPlugin
* audioOutputPlugin
)
35 if (!audioOutputPlugin
->name
)
37 insertInList(audioOutputPluginList
, audioOutputPlugin
->name
,
41 void unloadAudioOutputPlugin(AudioOutputPlugin
* audioOutputPlugin
)
43 if (!audioOutputPlugin
->name
)
45 deleteFromList(audioOutputPluginList
, audioOutputPlugin
->name
);
48 void initAudioOutputPlugins(void)
50 audioOutputPluginList
= makeList(NULL
, 0);
53 void finishAudioOutputPlugins(void)
55 freeList(audioOutputPluginList
);
58 #define getBlockParam(name, str, force) { \
59 bp = getBlockParam(param, name); \
60 if(force && bp == NULL) { \
61 FATAL("couldn't find parameter \"%s\" in audio output " \
62 "definition beginning at %i\n", \
65 if(bp) str = bp->value; \
68 int initAudioOutput(AudioOutput
*ao
, ConfigParam
* param
)
74 BlockParam
*bp
= NULL
;
75 AudioOutputPlugin
*plugin
= NULL
;
78 getBlockParam(AUDIO_OUTPUT_NAME
, name
, 1);
79 getBlockParam(AUDIO_OUTPUT_TYPE
, type
, 1);
80 getBlockParam(AUDIO_OUTPUT_FORMAT
, format
, 0);
82 if (!findInList(audioOutputPluginList
, type
, &data
)) {
83 FATAL("couldn't find audio output plugin for type "
84 "\"%s\" at line %i\n", type
, param
->line
);
87 plugin
= (AudioOutputPlugin
*) data
;
89 ListNode
*node
= audioOutputPluginList
->firstNode
;
91 WARNING("No \"%s\" defined in config file\n",
93 WARNING("Attempt to detect audio output device\n");
96 plugin
= (AudioOutputPlugin
*) node
->data
;
97 if (plugin
->testDefaultDeviceFunc
) {
98 WARNING("Attempting to detect a %s audio "
99 "device\n", plugin
->name
);
100 if (plugin
->testDefaultDeviceFunc() == 0) {
101 WARNING("Successfully detected a %s "
102 "audio device\n", plugin
->name
);
106 node
= node
->nextNode
;
110 WARNING("Unable to detect an audio device\n");
114 name
= "default detected output";
120 ao
->finishDriverFunc
= plugin
->finishDriverFunc
;
121 ao
->openDeviceFunc
= plugin
->openDeviceFunc
;
122 ao
->playFunc
= plugin
->playFunc
;
123 ao
->dropBufferedAudioFunc
= plugin
->dropBufferedAudioFunc
;
124 ao
->closeDeviceFunc
= plugin
->closeDeviceFunc
;
125 ao
->sendMetdataFunc
= plugin
->sendMetdataFunc
;
128 ao
->convertAudioFormat
= 0;
129 ao
->sameInAndOutFormats
= 0;
130 ao
->convBuffer
= NULL
;
131 ao
->convBufferLen
= 0;
133 memset(&ao
->inAudioFormat
, 0, sizeof(AudioFormat
));
134 memset(&ao
->outAudioFormat
, 0, sizeof(AudioFormat
));
135 memset(&ao
->reqAudioFormat
, 0, sizeof(AudioFormat
));
136 memset(&ao
->convState
, 0, sizeof(ConvState
));
139 ao
->convertAudioFormat
= 1;
141 if (0 != parseAudioConfig(&ao
->reqAudioFormat
, format
)) {
142 FATAL("error parsing format at line %i\n", bp
->line
);
145 copyAudioFormat(&ao
->outAudioFormat
, &ao
->reqAudioFormat
);
148 if (plugin
->initDriverFunc(ao
, param
) != 0)
154 int openAudioOutput(AudioOutput
* audioOutput
, AudioFormat
* audioFormat
)
158 if (audioOutput
->open
&&
159 0 == cmpAudioFormat(audioFormat
, &audioOutput
->inAudioFormat
)) {
163 copyAudioFormat(&audioOutput
->inAudioFormat
, audioFormat
);
165 if (audioOutput
->convertAudioFormat
) {
166 copyAudioFormat(&audioOutput
->outAudioFormat
,
167 &audioOutput
->reqAudioFormat
);
169 copyAudioFormat(&audioOutput
->outAudioFormat
,
170 &audioOutput
->inAudioFormat
);
171 if (audioOutput
->open
)
172 closeAudioOutput(audioOutput
);
175 if (!audioOutput
->open
)
176 ret
= audioOutput
->openDeviceFunc(audioOutput
);
178 audioOutput
->sameInAndOutFormats
=
179 !cmpAudioFormat(&audioOutput
->inAudioFormat
,
180 &audioOutput
->outAudioFormat
);
185 static void convertAudioFormat(AudioOutput
* audioOutput
, char **chunkArgPtr
,
188 int size
= pcm_sizeOfConvBuffer(&(audioOutput
->inAudioFormat
),
190 &(audioOutput
->outAudioFormat
));
192 if (size
> audioOutput
->convBufferLen
) {
193 audioOutput
->convBuffer
=
194 xrealloc(audioOutput
->convBuffer
, size
);
195 audioOutput
->convBufferLen
= size
;
198 *sizeArgPtr
= pcm_convertAudioFormat(&(audioOutput
->inAudioFormat
),
199 *chunkArgPtr
, *sizeArgPtr
,
200 &(audioOutput
->outAudioFormat
),
201 audioOutput
->convBuffer
,
202 &audioOutput
->convState
);
204 *chunkArgPtr
= audioOutput
->convBuffer
;
207 int playAudioOutput(AudioOutput
* audioOutput
, char *playChunk
, int size
)
211 if (!audioOutput
->open
)
214 if (!audioOutput
->sameInAndOutFormats
) {
215 convertAudioFormat(audioOutput
, &playChunk
, &size
);
218 ret
= audioOutput
->playFunc(audioOutput
, playChunk
, size
);
223 void dropBufferedAudioOutput(AudioOutput
* audioOutput
)
225 if (audioOutput
->open
)
226 audioOutput
->dropBufferedAudioFunc(audioOutput
);
229 void closeAudioOutput(AudioOutput
* audioOutput
)
231 if (audioOutput
->open
)
232 audioOutput
->closeDeviceFunc(audioOutput
);
235 void finishAudioOutput(AudioOutput
* audioOutput
)
237 closeAudioOutput(audioOutput
);
238 if (audioOutput
->finishDriverFunc
)
239 audioOutput
->finishDriverFunc(audioOutput
);
240 if (audioOutput
->convBuffer
)
241 free(audioOutput
->convBuffer
);
244 void sendMetadataToAudioOutput(AudioOutput
* audioOutput
, MpdTag
* tag
)
246 if (!audioOutput
->sendMetdataFunc
)
248 audioOutput
->sendMetdataFunc(audioOutput
, tag
);
251 void printAllOutputPluginTypes(FILE * fp
)
253 ListNode
*node
= audioOutputPluginList
->firstNode
;
254 AudioOutputPlugin
*plugin
;
257 plugin
= (AudioOutputPlugin
*) node
->data
;
258 fprintf(fp
, "%s ", plugin
->name
);
259 node
= node
->nextNode
;