cleanup: reindent audio_out.[ch]
[mplayer/greg.git] / libao2 / audio_out.c
blob3e37cfb70b059a6e08814aff4c3ab2ba5b05b884
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
24 #include "config.h"
25 #include "audio_out.h"
27 #include "mp_msg.h"
29 // there are some globals:
30 struct ao ao_data;
31 char *ao_subdevice = NULL;
33 extern const ao_functions_t audio_out_oss;
34 extern const ao_functions_t audio_out_coreaudio;
35 extern const ao_functions_t audio_out_arts;
36 extern const ao_functions_t audio_out_esd;
37 extern const ao_functions_t audio_out_pulse;
38 extern const ao_functions_t audio_out_jack;
39 extern const ao_functions_t audio_out_openal;
40 extern const ao_functions_t audio_out_null;
41 extern const ao_functions_t audio_out_alsa5;
42 extern const ao_functions_t audio_out_alsa;
43 extern const ao_functions_t audio_out_nas;
44 extern const ao_functions_t audio_out_sdl;
45 extern const ao_functions_t audio_out_sun;
46 extern const ao_functions_t audio_out_sgi;
47 extern const ao_functions_t audio_out_win32;
48 extern const ao_functions_t audio_out_dsound;
49 extern const ao_functions_t audio_out_kai;
50 extern const ao_functions_t audio_out_dart;
51 extern const ao_functions_t audio_out_ivtv;
52 extern const ao_functions_t audio_out_v4l2;
53 extern const ao_functions_t audio_out_mpegpes;
54 extern const ao_functions_t audio_out_pcm;
55 extern const ao_functions_t audio_out_pss;
57 static const ao_functions_t *const audio_out_drivers[] = {
58 // native:
59 #ifdef CONFIG_DIRECTX
60 &audio_out_dsound,
61 #endif
62 #ifdef CONFIG_WIN32WAVEOUT
63 &audio_out_win32,
64 #endif
65 #ifdef CONFIG_KAI
66 &audio_out_kai,
67 #endif
68 #ifdef CONFIG_DART
69 &audio_out_dart,
70 #endif
71 #ifdef CONFIG_COREAUDIO
72 &audio_out_coreaudio,
73 #endif
74 #ifdef CONFIG_OSS_AUDIO
75 &audio_out_oss,
76 #endif
77 #ifdef CONFIG_ALSA
78 &audio_out_alsa,
79 #endif
80 #ifdef CONFIG_ALSA5
81 &audio_out_alsa5,
82 #endif
83 #ifdef CONFIG_SGI_AUDIO
84 &audio_out_sgi,
85 #endif
86 #ifdef CONFIG_SUN_AUDIO
87 &audio_out_sun,
88 #endif
89 // wrappers:
90 #ifdef CONFIG_ARTS
91 &audio_out_arts,
92 #endif
93 #ifdef CONFIG_ESD
94 &audio_out_esd,
95 #endif
96 #ifdef CONFIG_PULSE
97 &audio_out_pulse,
98 #endif
99 #ifdef CONFIG_JACK
100 &audio_out_jack,
101 #endif
102 #ifdef CONFIG_NAS
103 &audio_out_nas,
104 #endif
105 #ifdef CONFIG_SDL
106 &audio_out_sdl,
107 #endif
108 #ifdef CONFIG_OPENAL
109 &audio_out_openal,
110 #endif
111 &audio_out_mpegpes,
112 #ifdef CONFIG_IVTV
113 &audio_out_ivtv,
114 #endif
115 #ifdef CONFIG_V4L2_DECODER
116 &audio_out_v4l2,
117 #endif
118 &audio_out_null,
119 // should not be auto-selected:
120 &audio_out_pcm,
121 NULL
124 void list_audio_out(void)
126 int i=0;
127 mp_tmsg(MSGT_AO, MSGL_INFO, "Available audio output drivers:\n");
128 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_OUTPUTS\n");
129 while (audio_out_drivers[i]) {
130 const ao_info_t *info = audio_out_drivers[i++]->info;
131 mp_msg(MSGT_GLOBAL, MSGL_INFO, "\t%s\t%s\n", info->short_name,
132 info->name);
134 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
137 struct ao *ao_create(void)
139 ao_data = (struct ao){.outburst = OUTBURST, .buffersize = -1};
140 return &ao_data;
143 void ao_init(struct ao *ao, char **ao_list)
145 struct ao backup = *ao;
147 if (!ao_list)
148 goto try_defaults;
150 // first try the preferred drivers, with their optional subdevice param:
151 while (*ao_list) {
152 char *ao_name = *ao_list;
153 if (!*ao_name)
154 goto try_defaults; // empty entry means try defaults
155 int ao_len;
156 assert(!ao_subdevice);
157 ao_subdevice = strchr(ao_name, ':');
158 if (ao_subdevice) {
159 ao_len = ao_subdevice - ao_name;
160 ao_subdevice = strdup(ao_subdevice + 1);
161 } else
162 ao_len = strlen(ao_name);
164 mp_tmsg(MSGT_AO, MSGL_V,
165 "Trying preferred audio driver '%.*s', options '%s'\n",
166 ao_len, ao_name, ao_subdevice ? ao_subdevice : "[none]");
168 const ao_functions_t *audio_out = NULL;
169 for (int i = 0; audio_out_drivers[i]; i++) {
170 audio_out = audio_out_drivers[i];
171 if (!strncmp(audio_out->info->short_name, ao_name, ao_len))
172 break;
173 audio_out = NULL;
175 if (audio_out) {
176 // name matches, try it
177 if (audio_out->init(ao->samplerate, ao->channels, ao->format, 0)) {
178 ao->driver = audio_out;
179 ao->initialized = true;
180 return;
182 mp_tmsg(MSGT_AO, MSGL_WARN,
183 "Failed to initialize audio driver '%s'\n", ao_name);
184 *ao = backup;
185 } else
186 mp_tmsg(MSGT_AO, MSGL_WARN, "No such audio driver '%.*s'\n",
187 ao_len, ao_name);
188 free(ao_subdevice);
189 ao_subdevice = NULL;
190 ++ao_list;
192 return;
194 try_defaults:
195 mp_tmsg(MSGT_AO, MSGL_V, "Trying every known audio driver...\n");
197 // now try the rest...
198 for (int i = 0; audio_out_drivers[i]; i++) {
199 const ao_functions_t *audio_out = audio_out_drivers[i];
200 if (audio_out->init(ao->samplerate, ao->channels, ao->format, 0)) {
201 ao->initialized = true;
202 ao->driver = audio_out;
203 return;
205 *ao = backup;
207 return;
210 void ao_uninit(struct ao *ao, bool drain_audio)
212 if (ao->initialized)
213 ao->driver->uninit(drain_audio);
214 ao->initialized = false;
215 free(ao_subdevice);
216 ao_subdevice = NULL;
219 int ao_play(struct ao *ao, void *data, int len, int flags)
221 return ao->driver->play(data, len, flags);
224 int ao_control(struct ao *ao, int cmd, void *arg)
226 return ao->driver->control(cmd, arg);
229 double ao_get_delay(struct ao *ao)
231 return ao->driver->get_delay();
234 int ao_get_space(struct ao *ao)
236 return ao->driver->get_space();
239 void ao_reset(struct ao *ao)
241 ao->driver->reset();
244 void ao_pause(struct ao *ao)
246 ao->driver->pause();
249 void ao_resume(struct ao *ao)
251 ao->driver->resume();