vo_gl: convert to new API, clean up code
[mplayer.git] / libao2 / audio_out.c
blobbd06c8fd5f4967f7d54ef0995787f2814bccad4f
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 "talloc.h"
26 #include "config.h"
27 #include "audio_out.h"
29 #include "mp_msg.h"
31 // there are some globals:
32 struct ao *global_ao;
33 char *ao_subdevice = NULL;
35 extern const struct ao_driver audio_out_oss;
36 extern const struct ao_driver audio_out_coreaudio;
37 extern const struct ao_driver audio_out_arts;
38 extern const struct ao_driver audio_out_rsound;
39 extern const struct ao_driver audio_out_esd;
40 extern const struct ao_driver audio_out_pulse;
41 extern const struct ao_driver audio_out_jack;
42 extern const struct ao_driver audio_out_openal;
43 extern const struct ao_driver audio_out_null;
44 extern const struct ao_driver audio_out_alsa5;
45 extern const struct ao_driver audio_out_alsa;
46 extern const struct ao_driver audio_out_nas;
47 extern const struct ao_driver audio_out_sdl;
48 extern const struct ao_driver audio_out_sun;
49 extern const struct ao_driver audio_out_sgi;
50 extern const struct ao_driver audio_out_win32;
51 extern const struct ao_driver audio_out_dsound;
52 extern const struct ao_driver audio_out_kai;
53 extern const struct ao_driver audio_out_dart;
54 extern const struct ao_driver audio_out_ivtv;
55 extern const struct ao_driver audio_out_v4l2;
56 extern const struct ao_driver audio_out_mpegpes;
57 extern const struct ao_driver audio_out_pcm;
58 extern const struct ao_driver audio_out_pss;
60 static const struct ao_driver * const audio_out_drivers[] = {
61 // native:
62 #ifdef CONFIG_DIRECTX
63 &audio_out_dsound,
64 #endif
65 #ifdef CONFIG_WIN32WAVEOUT
66 &audio_out_win32,
67 #endif
68 #ifdef CONFIG_KAI
69 &audio_out_kai,
70 #endif
71 #ifdef CONFIG_DART
72 &audio_out_dart,
73 #endif
74 #ifdef CONFIG_COREAUDIO
75 &audio_out_coreaudio,
76 #endif
77 #ifdef CONFIG_OSS_AUDIO
78 &audio_out_oss,
79 #endif
80 #ifdef CONFIG_ALSA
81 &audio_out_alsa,
82 #endif
83 #ifdef CONFIG_ALSA5
84 &audio_out_alsa5,
85 #endif
86 #ifdef CONFIG_SGI_AUDIO
87 &audio_out_sgi,
88 #endif
89 #ifdef CONFIG_SUN_AUDIO
90 &audio_out_sun,
91 #endif
92 // wrappers:
93 #ifdef CONFIG_ARTS
94 &audio_out_arts,
95 #endif
96 #ifdef CONFIG_ESD
97 &audio_out_esd,
98 #endif
99 #ifdef CONFIG_PULSE
100 &audio_out_pulse,
101 #endif
102 #ifdef CONFIG_JACK
103 &audio_out_jack,
104 #endif
105 #ifdef CONFIG_NAS
106 &audio_out_nas,
107 #endif
108 #ifdef CONFIG_SDL
109 &audio_out_sdl,
110 #endif
111 #ifdef CONFIG_OPENAL
112 &audio_out_openal,
113 #endif
114 &audio_out_mpegpes,
115 #ifdef CONFIG_IVTV
116 &audio_out_ivtv,
117 #endif
118 #ifdef CONFIG_V4L2_DECODER
119 &audio_out_v4l2,
120 #endif
121 &audio_out_null,
122 // should not be auto-selected:
123 &audio_out_pcm,
124 #ifdef CONFIG_RSOUND
125 &audio_out_rsound,
126 #endif
127 NULL
130 void list_audio_out(void)
132 int i=0;
133 mp_tmsg(MSGT_AO, MSGL_INFO, "Available audio output drivers:\n");
134 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_OUTPUTS\n");
135 while (audio_out_drivers[i]) {
136 const ao_info_t *info = audio_out_drivers[i++]->info;
137 mp_msg(MSGT_GLOBAL, MSGL_INFO, "\t%s\t%s\n", info->short_name,
138 info->name);
140 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
143 struct ao *ao_create(void)
145 struct ao *r = talloc(NULL, struct ao);
146 *r = (struct ao){.outburst = OUTBURST, .buffersize = -1};
147 return r;
150 void ao_init(struct ao *ao, char **ao_list)
152 /* Caller adding child blocks is not supported as we may call
153 * talloc_free_children() to clean up after failed open attempts.
155 assert(talloc_total_blocks(ao) == 1);
156 struct ao backup = *ao;
158 if (!ao_list)
159 goto try_defaults;
161 // first try the preferred drivers, with their optional subdevice param:
162 while (*ao_list) {
163 char *ao_name = *ao_list;
164 if (!*ao_name)
165 goto try_defaults; // empty entry means try defaults
166 int ao_len;
167 char *params = strchr(ao_name, ':');
168 if (params) {
169 ao_len = params - ao_name;
170 params++;
171 } else
172 ao_len = strlen(ao_name);
174 mp_tmsg(MSGT_AO, MSGL_V,
175 "Trying preferred audio driver '%.*s', options '%s'\n",
176 ao_len, ao_name, params ? params : "[none]");
178 const struct ao_driver *audio_out = NULL;
179 for (int i = 0; audio_out_drivers[i]; i++) {
180 audio_out = audio_out_drivers[i];
181 if (!strncmp(audio_out->info->short_name, ao_name, ao_len))
182 break;
183 audio_out = NULL;
185 if (audio_out) {
186 // name matches, try it
187 ao->driver = audio_out;
188 if (audio_out->init(ao, params) >= 0) {
189 ao->driver = audio_out;
190 ao->initialized = true;
191 return;
193 mp_tmsg(MSGT_AO, MSGL_WARN,
194 "Failed to initialize audio driver '%s'\n", ao_name);
195 talloc_free_children(ao);
196 *ao = backup;
197 } else
198 mp_tmsg(MSGT_AO, MSGL_WARN, "No such audio driver '%.*s'\n",
199 ao_len, ao_name);
200 ++ao_list;
202 return;
204 try_defaults:
205 mp_tmsg(MSGT_AO, MSGL_V, "Trying every known audio driver...\n");
207 // now try the rest...
208 for (int i = 0; audio_out_drivers[i]; i++) {
209 const struct ao_driver *audio_out = audio_out_drivers[i];
210 ao->driver = audio_out;
211 if (audio_out->init(ao, NULL) >= 0) {
212 ao->initialized = true;
213 ao->driver = audio_out;
214 return;
216 talloc_free_children(ao);
217 *ao = backup;
219 return;
222 void ao_uninit(struct ao *ao, bool cut_audio)
224 assert(ao->buffer.len >= ao->buffer_playable_size);
225 ao->buffer.len = ao->buffer_playable_size;
226 if (ao->initialized)
227 ao->driver->uninit(ao, cut_audio);
228 if (!cut_audio && ao->buffer.len)
229 mp_msg(MSGT_AO, MSGL_WARN, "Audio output truncated at end.\n");
230 talloc_free(ao);
233 int ao_play(struct ao *ao, void *data, int len, int flags)
235 return ao->driver->play(ao, data, len, flags);
238 int ao_control(struct ao *ao, int cmd, void *arg)
240 if (ao->driver->control)
241 return ao->driver->control(ao, cmd, arg);
242 return CONTROL_UNKNOWN;
245 double ao_get_delay(struct ao *ao)
247 if (!ao->driver->get_delay) {
248 assert(ao->untimed);
249 return 0;
251 return ao->driver->get_delay(ao);
254 int ao_get_space(struct ao *ao)
256 return ao->driver->get_space(ao);
259 void ao_reset(struct ao *ao)
261 ao->buffer.len = 0;
262 ao->buffer_playable_size = 0;
263 if (ao->driver->reset)
264 ao->driver->reset(ao);
267 void ao_pause(struct ao *ao)
269 if (ao->driver->pause)
270 ao->driver->pause(ao);
273 void ao_resume(struct ao *ao)
275 if (ao->driver->resume)
276 ao->driver->resume(ao);
281 int old_ao_init(struct ao *ao, char *params)
283 assert(!global_ao);
284 global_ao = ao;
285 ao_subdevice = params ? talloc_strdup(ao, params) : NULL;
286 if (ao->driver->old_functions->init(ao->samplerate, ao->channels,
287 ao->format, 0) == 0) {
288 global_ao = NULL;
289 return -1;
291 return 0;
294 void old_ao_uninit(struct ao *ao, bool cut_audio)
296 ao->driver->old_functions->uninit(cut_audio);
297 global_ao = NULL;
300 int old_ao_play(struct ao *ao, void *data, int len, int flags)
302 return ao->driver->old_functions->play(data, len, flags);
305 int old_ao_control(struct ao *ao, int cmd, void *arg)
307 return ao->driver->old_functions->control(cmd, arg);
310 float old_ao_get_delay(struct ao *ao)
312 return ao->driver->old_functions->get_delay();
315 int old_ao_get_space(struct ao *ao)
317 return ao->driver->old_functions->get_space();
320 void old_ao_reset(struct ao *ao)
322 ao->driver->old_functions->reset();
325 void old_ao_pause(struct ao *ao)
327 ao->driver->old_functions->pause();
330 void old_ao_resume(struct ao *ao)
332 ao->driver->old_functions->resume();