ao_pulse: convert to new AO API
[mplayer.git] / libao2 / audio_out.c
bloba91a0d6d72e31cf857400da4d30127bc0fd4630e
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;
59 static const struct ao_driver * const audio_out_drivers[] = {
60 // native:
61 #ifdef CONFIG_DIRECTX
62 &audio_out_dsound,
63 #endif
64 #ifdef CONFIG_WIN32WAVEOUT
65 &audio_out_win32,
66 #endif
67 #ifdef CONFIG_KAI
68 &audio_out_kai,
69 #endif
70 #ifdef CONFIG_DART
71 &audio_out_dart,
72 #endif
73 #ifdef CONFIG_COREAUDIO
74 &audio_out_coreaudio,
75 #endif
76 #ifdef CONFIG_PULSE
77 &audio_out_pulse,
78 #endif
79 #ifdef CONFIG_ALSA
80 &audio_out_alsa,
81 #endif
82 #ifdef CONFIG_OSS_AUDIO
83 &audio_out_oss,
84 #endif
85 #ifdef CONFIG_SGI_AUDIO
86 &audio_out_sgi,
87 #endif
88 #ifdef CONFIG_SUN_AUDIO
89 &audio_out_sun,
90 #endif
91 // wrappers:
92 #ifdef CONFIG_ARTS
93 &audio_out_arts,
94 #endif
95 #ifdef CONFIG_ESD
96 &audio_out_esd,
97 #endif
98 #ifdef CONFIG_JACK
99 &audio_out_jack,
100 #endif
101 #ifdef CONFIG_NAS
102 &audio_out_nas,
103 #endif
104 #ifdef CONFIG_OPENAL
105 &audio_out_openal,
106 #endif
107 #ifdef CONFIG_SDL
108 &audio_out_sdl,
109 #endif
110 &audio_out_mpegpes,
111 #ifdef CONFIG_IVTV
112 &audio_out_ivtv,
113 #endif
114 #ifdef CONFIG_V4L2_DECODER
115 &audio_out_v4l2,
116 #endif
117 &audio_out_null,
118 // should not be auto-selected:
119 &audio_out_pcm,
120 #ifdef CONFIG_RSOUND
121 &audio_out_rsound,
122 #endif
123 NULL
126 void list_audio_out(void)
128 int i=0;
129 mp_tmsg(MSGT_AO, MSGL_INFO, "Available audio output drivers:\n");
130 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_OUTPUTS\n");
131 while (audio_out_drivers[i]) {
132 const ao_info_t *info = audio_out_drivers[i++]->info;
133 mp_msg(MSGT_GLOBAL, MSGL_INFO, "\t%s\t%s\n", info->short_name,
134 info->name);
136 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
139 struct ao *ao_create(void)
141 struct ao *r = talloc(NULL, struct ao);
142 *r = (struct ao){.outburst = OUTBURST, .buffersize = -1};
143 return r;
146 void ao_init(struct ao *ao, char **ao_list)
148 /* Caller adding child blocks is not supported as we may call
149 * talloc_free_children() to clean up after failed open attempts.
151 assert(talloc_total_blocks(ao) == 1);
152 struct ao backup = *ao;
154 if (!ao_list)
155 goto try_defaults;
157 // first try the preferred drivers, with their optional subdevice param:
158 while (*ao_list) {
159 char *ao_name = *ao_list;
160 if (!*ao_name)
161 goto try_defaults; // empty entry means try defaults
162 int ao_len;
163 char *params = strchr(ao_name, ':');
164 if (params) {
165 ao_len = params - ao_name;
166 params++;
167 } else
168 ao_len = strlen(ao_name);
170 mp_tmsg(MSGT_AO, MSGL_V,
171 "Trying preferred audio driver '%.*s', options '%s'\n",
172 ao_len, ao_name, params ? params : "[none]");
174 const struct ao_driver *audio_out = NULL;
175 for (int i = 0; audio_out_drivers[i]; i++) {
176 audio_out = audio_out_drivers[i];
177 if (!strncmp(audio_out->info->short_name, ao_name, ao_len))
178 break;
179 audio_out = NULL;
181 if (audio_out) {
182 // name matches, try it
183 ao->driver = audio_out;
184 if (audio_out->init(ao, params) >= 0) {
185 ao->driver = audio_out;
186 ao->initialized = true;
187 return;
189 mp_tmsg(MSGT_AO, MSGL_WARN,
190 "Failed to initialize audio driver '%s'\n", ao_name);
191 talloc_free_children(ao);
192 *ao = backup;
193 } else
194 mp_tmsg(MSGT_AO, MSGL_WARN, "No such audio driver '%.*s'\n",
195 ao_len, ao_name);
196 ++ao_list;
198 return;
200 try_defaults:
201 mp_tmsg(MSGT_AO, MSGL_V, "Trying every known audio driver...\n");
203 // now try the rest...
204 for (int i = 0; audio_out_drivers[i]; i++) {
205 const struct ao_driver *audio_out = audio_out_drivers[i];
206 ao->driver = audio_out;
207 if (audio_out->init(ao, NULL) >= 0) {
208 ao->initialized = true;
209 ao->driver = audio_out;
210 return;
212 talloc_free_children(ao);
213 *ao = backup;
215 return;
218 void ao_uninit(struct ao *ao, bool cut_audio)
220 assert(ao->buffer.len >= ao->buffer_playable_size);
221 ao->buffer.len = ao->buffer_playable_size;
222 if (ao->initialized)
223 ao->driver->uninit(ao, cut_audio);
224 if (!cut_audio && ao->buffer.len)
225 mp_msg(MSGT_AO, MSGL_WARN, "Audio output truncated at end.\n");
226 talloc_free(ao);
229 int ao_play(struct ao *ao, void *data, int len, int flags)
231 return ao->driver->play(ao, data, len, flags);
234 int ao_control(struct ao *ao, int cmd, void *arg)
236 if (ao->driver->control)
237 return ao->driver->control(ao, cmd, arg);
238 return CONTROL_UNKNOWN;
241 double ao_get_delay(struct ao *ao)
243 if (!ao->driver->get_delay) {
244 assert(ao->untimed);
245 return 0;
247 return ao->driver->get_delay(ao);
250 int ao_get_space(struct ao *ao)
252 return ao->driver->get_space(ao);
255 void ao_reset(struct ao *ao)
257 ao->buffer.len = 0;
258 ao->buffer_playable_size = 0;
259 if (ao->driver->reset)
260 ao->driver->reset(ao);
263 void ao_pause(struct ao *ao)
265 if (ao->driver->pause)
266 ao->driver->pause(ao);
269 void ao_resume(struct ao *ao)
271 if (ao->driver->resume)
272 ao->driver->resume(ao);
277 int old_ao_init(struct ao *ao, char *params)
279 assert(!global_ao);
280 global_ao = ao;
281 ao_subdevice = params ? talloc_strdup(ao, params) : NULL;
282 if (ao->driver->old_functions->init(ao->samplerate, ao->channels,
283 ao->format, 0) == 0) {
284 global_ao = NULL;
285 return -1;
287 return 0;
290 void old_ao_uninit(struct ao *ao, bool cut_audio)
292 ao->driver->old_functions->uninit(cut_audio);
293 global_ao = NULL;
296 int old_ao_play(struct ao *ao, void *data, int len, int flags)
298 return ao->driver->old_functions->play(data, len, flags);
301 int old_ao_control(struct ao *ao, int cmd, void *arg)
303 return ao->driver->old_functions->control(cmd, arg);
306 float old_ao_get_delay(struct ao *ao)
308 return ao->driver->old_functions->get_delay();
311 int old_ao_get_space(struct ao *ao)
313 return ao->driver->old_functions->get_space();
316 void old_ao_reset(struct ao *ao)
318 ao->driver->old_functions->reset();
321 void old_ao_pause(struct ao *ao)
323 ao->driver->old_functions->pause();
326 void old_ao_resume(struct ao *ao)
328 ao->driver->old_functions->resume();