options: support parsing values into substructs
[mplayer.git] / libao2 / audio_out.c
blob36b3e2d7c6225d0f6dad47838fbace58d14ed24a
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_alsa;
45 extern const struct ao_driver audio_out_nas;
46 extern const struct ao_driver audio_out_sdl;
47 extern const struct ao_driver audio_out_sun;
48 extern const struct ao_driver audio_out_sgi;
49 extern const struct ao_driver audio_out_win32;
50 extern const struct ao_driver audio_out_dsound;
51 extern const struct ao_driver audio_out_kai;
52 extern const struct ao_driver audio_out_dart;
53 extern const struct ao_driver audio_out_ivtv;
54 extern const struct ao_driver audio_out_v4l2;
55 extern const struct ao_driver audio_out_mpegpes;
56 extern const struct ao_driver audio_out_pcm;
57 extern const struct ao_driver audio_out_pss;
58 extern const struct ao_driver audio_out_portaudio;
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_PULSE
78 &audio_out_pulse,
79 #endif
80 #ifdef CONFIG_ALSA
81 &audio_out_alsa,
82 #endif
83 #ifdef CONFIG_OSS_AUDIO
84 &audio_out_oss,
85 #endif
86 #ifdef CONFIG_PORTAUDIO
87 &audio_out_portaudio,
88 #endif
89 #ifdef CONFIG_SUN_AUDIO
90 &audio_out_sun,
91 #endif
92 // wrappers:
93 #ifdef CONFIG_JACK
94 &audio_out_jack,
95 #endif
96 #ifdef CONFIG_NAS
97 &audio_out_nas,
98 #endif
99 #ifdef CONFIG_OPENAL
100 &audio_out_openal,
101 #endif
102 #ifdef CONFIG_SDL
103 &audio_out_sdl,
104 #endif
105 &audio_out_mpegpes,
106 #ifdef CONFIG_IVTV
107 &audio_out_ivtv,
108 #endif
109 #ifdef CONFIG_V4L2_DECODER
110 &audio_out_v4l2,
111 #endif
112 &audio_out_null,
113 // should not be auto-selected:
114 &audio_out_pcm,
115 #ifdef CONFIG_RSOUND
116 &audio_out_rsound,
117 #endif
118 NULL
121 void list_audio_out(void)
123 int i=0;
124 mp_tmsg(MSGT_AO, MSGL_INFO, "Available audio output drivers:\n");
125 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_OUTPUTS\n");
126 while (audio_out_drivers[i]) {
127 const ao_info_t *info = audio_out_drivers[i++]->info;
128 mp_msg(MSGT_GLOBAL, MSGL_INFO, "\t%s\t%s\n", info->short_name,
129 info->name);
131 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
134 struct ao *ao_create(struct MPOpts *opts, struct input_ctx *input)
136 struct ao *r = talloc(NULL, struct ao);
137 *r = (struct ao){.outburst = OUTBURST, .buffersize = -1,
138 .opts = opts, .input_ctx = input };
139 return r;
142 void ao_init(struct ao *ao, char **ao_list)
144 /* Caller adding child blocks is not supported as we may call
145 * talloc_free_children() to clean up after failed open attempts.
147 assert(talloc_total_blocks(ao) == 1);
148 struct ao backup = *ao;
150 if (!ao_list)
151 goto try_defaults;
153 // first try the preferred drivers, with their optional subdevice param:
154 while (*ao_list) {
155 char *ao_name = *ao_list;
156 if (!*ao_name)
157 goto try_defaults; // empty entry means try defaults
158 int ao_len;
159 char *params = strchr(ao_name, ':');
160 if (params) {
161 ao_len = params - ao_name;
162 params++;
163 } else
164 ao_len = strlen(ao_name);
166 mp_tmsg(MSGT_AO, MSGL_V,
167 "Trying preferred audio driver '%.*s', options '%s'\n",
168 ao_len, ao_name, params ? params : "[none]");
170 const struct ao_driver *audio_out = NULL;
171 for (int i = 0; audio_out_drivers[i]; i++) {
172 audio_out = audio_out_drivers[i];
173 if (!strncmp(audio_out->info->short_name, ao_name, ao_len))
174 break;
175 audio_out = NULL;
177 if (audio_out) {
178 // name matches, try it
179 ao->driver = audio_out;
180 if (audio_out->init(ao, params) >= 0) {
181 ao->driver = audio_out;
182 ao->initialized = true;
183 return;
185 mp_tmsg(MSGT_AO, MSGL_WARN,
186 "Failed to initialize audio driver '%s'\n", ao_name);
187 talloc_free_children(ao);
188 *ao = backup;
189 } else
190 mp_tmsg(MSGT_AO, MSGL_WARN, "No such audio driver '%.*s'\n",
191 ao_len, ao_name);
192 ++ao_list;
194 return;
196 try_defaults:
197 mp_tmsg(MSGT_AO, MSGL_V, "Trying every known audio driver...\n");
199 // now try the rest...
200 for (int i = 0; audio_out_drivers[i]; i++) {
201 const struct ao_driver *audio_out = audio_out_drivers[i];
202 ao->driver = audio_out;
203 if (audio_out->init(ao, NULL) >= 0) {
204 ao->initialized = true;
205 ao->driver = audio_out;
206 return;
208 talloc_free_children(ao);
209 *ao = backup;
211 return;
214 void ao_uninit(struct ao *ao, bool cut_audio)
216 assert(ao->buffer.len >= ao->buffer_playable_size);
217 ao->buffer.len = ao->buffer_playable_size;
218 if (ao->initialized)
219 ao->driver->uninit(ao, cut_audio);
220 if (!cut_audio && ao->buffer.len)
221 mp_msg(MSGT_AO, MSGL_WARN, "Audio output truncated at end.\n");
222 talloc_free(ao);
225 int ao_play(struct ao *ao, void *data, int len, int flags)
227 return ao->driver->play(ao, data, len, flags);
230 int ao_control(struct ao *ao, enum aocontrol cmd, void *arg)
232 if (ao->driver->control)
233 return ao->driver->control(ao, cmd, arg);
234 return CONTROL_UNKNOWN;
237 double ao_get_delay(struct ao *ao)
239 if (!ao->driver->get_delay) {
240 assert(ao->untimed);
241 return 0;
243 return ao->driver->get_delay(ao);
246 int ao_get_space(struct ao *ao)
248 return ao->driver->get_space(ao);
251 void ao_reset(struct ao *ao)
253 ao->buffer.len = 0;
254 ao->buffer_playable_size = 0;
255 if (ao->driver->reset)
256 ao->driver->reset(ao);
259 void ao_pause(struct ao *ao)
261 if (ao->driver->pause)
262 ao->driver->pause(ao);
265 void ao_resume(struct ao *ao)
267 if (ao->driver->resume)
268 ao->driver->resume(ao);
273 int old_ao_init(struct ao *ao, char *params)
275 assert(!global_ao);
276 global_ao = ao;
277 ao_subdevice = params ? talloc_strdup(ao, params) : NULL;
278 if (ao->driver->old_functions->init(ao->samplerate, ao->channels,
279 ao->format, 0) == 0) {
280 global_ao = NULL;
281 return -1;
283 return 0;
286 void old_ao_uninit(struct ao *ao, bool cut_audio)
288 ao->driver->old_functions->uninit(cut_audio);
289 global_ao = NULL;
292 int old_ao_play(struct ao *ao, void *data, int len, int flags)
294 return ao->driver->old_functions->play(data, len, flags);
297 int old_ao_control(struct ao *ao, enum aocontrol cmd, void *arg)
299 return ao->driver->old_functions->control(cmd, arg);
302 float old_ao_get_delay(struct ao *ao)
304 return ao->driver->old_functions->get_delay();
307 int old_ao_get_space(struct ao *ao)
309 return ao->driver->old_functions->get_space();
312 void old_ao_reset(struct ao *ao)
314 ao->driver->old_functions->reset();
317 void old_ao_pause(struct ao *ao)
319 ao->driver->old_functions->pause();
322 void old_ao_resume(struct ao *ao)
324 ao->driver->old_functions->resume();