Initial revision 6759
[qball-mpd.git] / src / .svn / text-base / audioOutput.c.svn-base
blob03a5ed782107d18229a55c935589c9fd0b798627
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
4  *
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.
9  *
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
17  */
19 #include "audioOutput.h"
21 #include "list.h"
22 #include "log.h"
23 #include "pcm_utils.h"
25 #include <string.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)
36                 return;
37         insertInList(audioOutputPluginList, audioOutputPlugin->name,
38                      audioOutputPlugin);
41 void unloadAudioOutputPlugin(AudioOutputPlugin * audioOutputPlugin)
43         if (!audioOutputPlugin->name)
44                 return;
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", \
63                                 name, param->line); \
64         } \
65         if(bp) str = bp->value; \
68 int initAudioOutput(AudioOutput *ao, ConfigParam * param)
70         void *data = NULL;
71         char *name = NULL;
72         char *format = NULL;
73         char *type = NULL;
74         BlockParam *bp = NULL;
75         AudioOutputPlugin *plugin = NULL;
77         if (param) {
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);
85                 }
87                 plugin = (AudioOutputPlugin *) data;
88         } else {
89                 ListNode *node = audioOutputPluginList->firstNode;
91                 WARNING("No \"%s\" defined in config file\n",
92                         CONF_AUDIO_OUTPUT);
93                 WARNING("Attempt to detect audio output device\n");
95                 while (node) {
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);
103                                         break;
104                                 }
105                         }
106                         node = node->nextNode;
107                 }
109                 if (!node) {
110                         WARNING("Unable to detect an audio device\n");
111                         return 0;
112                 }
114                 name = "default detected output";
115                 type = plugin->name;
116         }
118         ao->name = name;
119         ao->type = type;
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;
126         ao->open = 0;
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));
138         if (format) {
139                 ao->convertAudioFormat = 1;
141                 if (0 != parseAudioConfig(&ao->reqAudioFormat, format)) {
142                         FATAL("error parsing format at line %i\n", bp->line);
143                 }
145                 copyAudioFormat(&ao->outAudioFormat, &ao->reqAudioFormat);
146         }
148         if (plugin->initDriverFunc(ao, param) != 0)
149                 return 0;
151         return 1;
154 int openAudioOutput(AudioOutput * audioOutput, AudioFormat * audioFormat)
156         int ret = 0;
158         if (audioOutput->open &&
159             0 == cmpAudioFormat(audioFormat, &audioOutput->inAudioFormat)) {
160                 return 0;
161         }
163         copyAudioFormat(&audioOutput->inAudioFormat, audioFormat);
165         if (audioOutput->convertAudioFormat) {
166                 copyAudioFormat(&audioOutput->outAudioFormat,
167                                 &audioOutput->reqAudioFormat);
168         } else {
169                 copyAudioFormat(&audioOutput->outAudioFormat,
170                                 &audioOutput->inAudioFormat);
171                 if (audioOutput->open)
172                         closeAudioOutput(audioOutput);
173         }
175         if (!audioOutput->open)
176                 ret = audioOutput->openDeviceFunc(audioOutput);
178         audioOutput->sameInAndOutFormats =
179                 !cmpAudioFormat(&audioOutput->inAudioFormat,
180                                 &audioOutput->outAudioFormat);
182         return ret;
185 static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr,
186                                int *sizeArgPtr)
188         int size = pcm_sizeOfConvBuffer(&(audioOutput->inAudioFormat),
189                                         *sizeArgPtr,
190                                         &(audioOutput->outAudioFormat));
192         if (size > audioOutput->convBufferLen) {
193                 audioOutput->convBuffer =
194                     xrealloc(audioOutput->convBuffer, size);
195                 audioOutput->convBufferLen = size;
196         }
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)
209         int ret;
211         if (!audioOutput->open)
212                 return -1;
214         if (!audioOutput->sameInAndOutFormats) {
215                 convertAudioFormat(audioOutput, &playChunk, &size);
216         }
218         ret = audioOutput->playFunc(audioOutput, playChunk, size);
220         return ret;
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)
247                 return;
248         audioOutput->sendMetdataFunc(audioOutput, tag);
251 void printAllOutputPluginTypes(FILE * fp)
253         ListNode *node = audioOutputPluginList->firstNode;
254         AudioOutputPlugin *plugin;
256         while (node) {
257                 plugin = (AudioOutputPlugin *) node->data;
258                 fprintf(fp, "%s ", plugin->name);
259                 node = node->nextNode;
260         }
261         fprintf(fp, "\n");
262         fflush(fp);