docs: document --quvi-format
[mplayer.git] / libao2 / audio_out.c
blob9eafc776fa3507bc935961f1f0347a0e62ca0b31
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_rsound;
38 extern const struct ao_driver audio_out_pulse;
39 extern const struct ao_driver audio_out_jack;
40 extern const struct ao_driver audio_out_openal;
41 extern const struct ao_driver audio_out_null;
42 extern const struct ao_driver audio_out_alsa;
43 extern const struct ao_driver audio_out_sdl;
44 extern const struct ao_driver audio_out_dsound;
45 extern const struct ao_driver audio_out_v4l2;
46 extern const struct ao_driver audio_out_pcm;
47 extern const struct ao_driver audio_out_pss;
48 extern const struct ao_driver audio_out_portaudio;
50 static const struct ao_driver * const audio_out_drivers[] = {
51 // native:
52 #ifdef CONFIG_DIRECTX
53 &audio_out_dsound,
54 #endif
55 #ifdef CONFIG_COREAUDIO
56 &audio_out_coreaudio,
57 #endif
58 #ifdef CONFIG_PULSE
59 &audio_out_pulse,
60 #endif
61 #ifdef CONFIG_ALSA
62 &audio_out_alsa,
63 #endif
64 #ifdef CONFIG_OSS_AUDIO
65 &audio_out_oss,
66 #endif
67 #ifdef CONFIG_PORTAUDIO
68 &audio_out_portaudio,
69 #endif
70 // wrappers:
71 #ifdef CONFIG_JACK
72 &audio_out_jack,
73 #endif
74 #ifdef CONFIG_OPENAL
75 &audio_out_openal,
76 #endif
77 #ifdef CONFIG_SDL
78 &audio_out_sdl,
79 #endif
80 #ifdef CONFIG_V4L2_DECODER
81 &audio_out_v4l2,
82 #endif
83 &audio_out_null,
84 // should not be auto-selected:
85 &audio_out_pcm,
86 #ifdef CONFIG_RSOUND
87 &audio_out_rsound,
88 #endif
89 NULL
92 void list_audio_out(void)
94 int i=0;
95 mp_tmsg(MSGT_AO, MSGL_INFO, "Available audio output drivers:\n");
96 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_OUTPUTS\n");
97 while (audio_out_drivers[i]) {
98 const ao_info_t *info = audio_out_drivers[i++]->info;
99 mp_msg(MSGT_GLOBAL, MSGL_INFO, "\t%s\t%s\n", info->short_name,
100 info->name);
102 mp_msg(MSGT_GLOBAL, MSGL_INFO,"\n");
105 struct ao *ao_create(struct MPOpts *opts, struct input_ctx *input)
107 struct ao *r = talloc(NULL, struct ao);
108 *r = (struct ao){.outburst = 512, .buffersize = -1,
109 .opts = opts, .input_ctx = input };
110 return r;
113 void ao_init(struct ao *ao, char **ao_list)
115 /* Caller adding child blocks is not supported as we may call
116 * talloc_free_children() to clean up after failed open attempts.
118 assert(talloc_total_blocks(ao) == 1);
119 struct ao backup = *ao;
121 if (!ao_list)
122 goto try_defaults;
124 // first try the preferred drivers, with their optional subdevice param:
125 while (*ao_list) {
126 char *ao_name = *ao_list;
127 if (!*ao_name)
128 goto try_defaults; // empty entry means try defaults
129 int ao_len;
130 char *params = strchr(ao_name, ':');
131 if (params) {
132 ao_len = params - ao_name;
133 params++;
134 } else
135 ao_len = strlen(ao_name);
137 mp_tmsg(MSGT_AO, MSGL_V,
138 "Trying preferred audio driver '%.*s', options '%s'\n",
139 ao_len, ao_name, params ? params : "[none]");
141 const struct ao_driver *audio_out = NULL;
142 for (int i = 0; audio_out_drivers[i]; i++) {
143 audio_out = audio_out_drivers[i];
144 if (!strncmp(audio_out->info->short_name, ao_name, ao_len))
145 break;
146 audio_out = NULL;
148 if (audio_out) {
149 // name matches, try it
150 ao->driver = audio_out;
151 if (audio_out->init(ao, params) >= 0) {
152 ao->driver = audio_out;
153 ao->initialized = true;
154 return;
156 mp_tmsg(MSGT_AO, MSGL_WARN,
157 "Failed to initialize audio driver '%s'\n", ao_name);
158 talloc_free_children(ao);
159 *ao = backup;
160 } else
161 mp_tmsg(MSGT_AO, MSGL_WARN, "No such audio driver '%.*s'\n",
162 ao_len, ao_name);
163 ++ao_list;
165 return;
167 try_defaults:
168 mp_tmsg(MSGT_AO, MSGL_V, "Trying every known audio driver...\n");
170 // now try the rest...
171 for (int i = 0; audio_out_drivers[i]; i++) {
172 const struct ao_driver *audio_out = audio_out_drivers[i];
173 ao->driver = audio_out;
174 if (audio_out->init(ao, NULL) >= 0) {
175 ao->initialized = true;
176 ao->driver = audio_out;
177 return;
179 talloc_free_children(ao);
180 *ao = backup;
182 return;
185 void ao_uninit(struct ao *ao, bool cut_audio)
187 assert(ao->buffer.len >= ao->buffer_playable_size);
188 ao->buffer.len = ao->buffer_playable_size;
189 if (ao->initialized)
190 ao->driver->uninit(ao, cut_audio);
191 if (!cut_audio && ao->buffer.len)
192 mp_msg(MSGT_AO, MSGL_WARN, "Audio output truncated at end.\n");
193 talloc_free(ao);
196 int ao_play(struct ao *ao, void *data, int len, int flags)
198 return ao->driver->play(ao, data, len, flags);
201 int ao_control(struct ao *ao, enum aocontrol cmd, void *arg)
203 if (ao->driver->control)
204 return ao->driver->control(ao, cmd, arg);
205 return CONTROL_UNKNOWN;
208 double ao_get_delay(struct ao *ao)
210 if (!ao->driver->get_delay) {
211 assert(ao->untimed);
212 return 0;
214 return ao->driver->get_delay(ao);
217 int ao_get_space(struct ao *ao)
219 return ao->driver->get_space(ao);
222 void ao_reset(struct ao *ao)
224 ao->buffer.len = 0;
225 ao->buffer_playable_size = 0;
226 if (ao->driver->reset)
227 ao->driver->reset(ao);
230 void ao_pause(struct ao *ao)
232 if (ao->driver->pause)
233 ao->driver->pause(ao);
236 void ao_resume(struct ao *ao)
238 if (ao->driver->resume)
239 ao->driver->resume(ao);
244 int old_ao_init(struct ao *ao, char *params)
246 assert(!global_ao);
247 global_ao = ao;
248 ao_subdevice = params ? talloc_strdup(ao, params) : NULL;
249 if (ao->driver->old_functions->init(ao->samplerate, ao->channels,
250 ao->format, 0) == 0) {
251 global_ao = NULL;
252 return -1;
254 return 0;
257 void old_ao_uninit(struct ao *ao, bool cut_audio)
259 ao->driver->old_functions->uninit(cut_audio);
260 global_ao = NULL;
263 int old_ao_play(struct ao *ao, void *data, int len, int flags)
265 return ao->driver->old_functions->play(data, len, flags);
268 int old_ao_control(struct ao *ao, enum aocontrol cmd, void *arg)
270 return ao->driver->old_functions->control(cmd, arg);
273 float old_ao_get_delay(struct ao *ao)
275 return ao->driver->old_functions->get_delay();
278 int old_ao_get_space(struct ao *ao)
280 return ao->driver->old_functions->get_space();
283 void old_ao_reset(struct ao *ao)
285 ao->driver->old_functions->reset();
288 void old_ao_pause(struct ao *ao)
290 ao->driver->old_functions->pause();
293 void old_ao_resume(struct ao *ao)
295 ao->driver->old_functions->resume();