]>
Commit | Line | Data |
---|---|---|
85571bc7 | 1 | /* |
1d14ffa9 FB |
2 | * QEMU SDL audio driver |
3 | * | |
4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) | |
5 | * | |
85571bc7 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
0b8fa32f | 24 | |
6086a565 | 25 | #include "qemu/osdep.h" |
9f059eca FB |
26 | #include <SDL.h> |
27 | #include <SDL_thread.h> | |
0b8fa32f | 28 | #include "qemu/module.h" |
87ecb68b | 29 | #include "audio.h" |
85571bc7 | 30 | |
e784ba70 TS |
31 | #ifndef _WIN32 |
32 | #ifdef __sun__ | |
33 | #define _POSIX_PTHREAD_SEMANTICS 1 | |
c5e97233 | 34 | #elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) |
9b4c14c3 | 35 | #include <pthread.h> |
e784ba70 | 36 | #endif |
e784ba70 TS |
37 | #endif |
38 | ||
1d14ffa9 FB |
39 | #define AUDIO_CAP "sdl" |
40 | #include "audio_int.h" | |
85571bc7 | 41 | |
1d14ffa9 FB |
42 | typedef struct SDLVoiceOut { |
43 | HWVoiceOut hw; | |
85571bc7 | 44 | int exit; |
85571bc7 | 45 | int initialized; |
57dea553 | 46 | Audiodev *dev; |
ce31f099 VR |
47 | SDL_AudioDeviceID devid; |
48 | } SDLVoiceOut; | |
85571bc7 | 49 | |
1d14ffa9 | 50 | static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...) |
85571bc7 | 51 | { |
1d14ffa9 FB |
52 | va_list ap; |
53 | ||
54 | va_start (ap, fmt); | |
55 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
56 | va_end (ap); | |
57 | ||
58 | AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ()); | |
85571bc7 FB |
59 | } |
60 | ||
85bc5852 | 61 | static int aud_to_sdlfmt (AudioFormat fmt) |
85571bc7 | 62 | { |
85571bc7 | 63 | switch (fmt) { |
85bc5852 | 64 | case AUDIO_FORMAT_S8: |
1d14ffa9 FB |
65 | return AUDIO_S8; |
66 | ||
85bc5852 | 67 | case AUDIO_FORMAT_U8: |
1d14ffa9 FB |
68 | return AUDIO_U8; |
69 | ||
85bc5852 | 70 | case AUDIO_FORMAT_S16: |
1d14ffa9 FB |
71 | return AUDIO_S16LSB; |
72 | ||
85bc5852 | 73 | case AUDIO_FORMAT_U16: |
1d14ffa9 FB |
74 | return AUDIO_U16LSB; |
75 | ||
ed2a4a79 KZ |
76 | case AUDIO_FORMAT_S32: |
77 | return AUDIO_S32LSB; | |
78 | ||
79 | /* no unsigned 32-bit support in SDL */ | |
80 | ||
81 | case AUDIO_FORMAT_F32: | |
82 | return AUDIO_F32LSB; | |
83 | ||
85571bc7 | 84 | default: |
1d14ffa9 FB |
85 | dolog ("Internal logic error: Bad audio format %d\n", fmt); |
86 | #ifdef DEBUG_AUDIO | |
87 | abort (); | |
88 | #endif | |
89 | return AUDIO_U8; | |
85571bc7 FB |
90 | } |
91 | } | |
92 | ||
85bc5852 | 93 | static int sdl_to_audfmt(int sdlfmt, AudioFormat *fmt, int *endianness) |
85571bc7 | 94 | { |
1d14ffa9 FB |
95 | switch (sdlfmt) { |
96 | case AUDIO_S8: | |
4ff9786c | 97 | *endianness = 0; |
85bc5852 | 98 | *fmt = AUDIO_FORMAT_S8; |
1d14ffa9 FB |
99 | break; |
100 | ||
101 | case AUDIO_U8: | |
4ff9786c | 102 | *endianness = 0; |
85bc5852 | 103 | *fmt = AUDIO_FORMAT_U8; |
1d14ffa9 FB |
104 | break; |
105 | ||
106 | case AUDIO_S16LSB: | |
4ff9786c | 107 | *endianness = 0; |
85bc5852 | 108 | *fmt = AUDIO_FORMAT_S16; |
1d14ffa9 FB |
109 | break; |
110 | ||
111 | case AUDIO_U16LSB: | |
4ff9786c | 112 | *endianness = 0; |
85bc5852 | 113 | *fmt = AUDIO_FORMAT_U16; |
1d14ffa9 FB |
114 | break; |
115 | ||
116 | case AUDIO_S16MSB: | |
4ff9786c | 117 | *endianness = 1; |
85bc5852 | 118 | *fmt = AUDIO_FORMAT_S16; |
1d14ffa9 FB |
119 | break; |
120 | ||
121 | case AUDIO_U16MSB: | |
4ff9786c | 122 | *endianness = 1; |
85bc5852 | 123 | *fmt = AUDIO_FORMAT_U16; |
1d14ffa9 FB |
124 | break; |
125 | ||
ed2a4a79 KZ |
126 | case AUDIO_S32LSB: |
127 | *endianness = 0; | |
128 | *fmt = AUDIO_FORMAT_S32; | |
129 | break; | |
130 | ||
131 | case AUDIO_S32MSB: | |
132 | *endianness = 1; | |
133 | *fmt = AUDIO_FORMAT_S32; | |
134 | break; | |
135 | ||
136 | case AUDIO_F32LSB: | |
137 | *endianness = 0; | |
138 | *fmt = AUDIO_FORMAT_F32; | |
139 | break; | |
140 | ||
141 | case AUDIO_F32MSB: | |
142 | *endianness = 1; | |
143 | *fmt = AUDIO_FORMAT_F32; | |
144 | break; | |
145 | ||
85571bc7 | 146 | default: |
1d14ffa9 FB |
147 | dolog ("Unrecognized SDL audio format %d\n", sdlfmt); |
148 | return -1; | |
85571bc7 | 149 | } |
1d14ffa9 FB |
150 | |
151 | return 0; | |
85571bc7 FB |
152 | } |
153 | ||
ce31f099 VR |
154 | static SDL_AudioDeviceID sdl_open(SDL_AudioSpec *req, SDL_AudioSpec *obt, |
155 | int rec) | |
85571bc7 | 156 | { |
ce31f099 | 157 | SDL_AudioDeviceID devid; |
e784ba70 | 158 | #ifndef _WIN32 |
d087bb3e | 159 | int err; |
e784ba70 TS |
160 | sigset_t new, old; |
161 | ||
162 | /* Make sure potential threads created by SDL don't hog signals. */ | |
d087bb3e | 163 | err = sigfillset (&new); |
164 | if (err) { | |
165 | dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno)); | |
ce31f099 | 166 | return 0; |
d087bb3e | 167 | } |
168 | err = pthread_sigmask (SIG_BLOCK, &new, &old); | |
169 | if (err) { | |
170 | dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err)); | |
ce31f099 | 171 | return 0; |
d087bb3e | 172 | } |
e784ba70 | 173 | #endif |
85571bc7 | 174 | |
ce31f099 VR |
175 | devid = SDL_OpenAudioDevice(NULL, rec, req, obt, 0); |
176 | if (!devid) { | |
177 | sdl_logerr("SDL_OpenAudioDevice for %s failed\n", | |
178 | rec ? "recording" : "playback"); | |
85571bc7 | 179 | } |
e784ba70 TS |
180 | |
181 | #ifndef _WIN32 | |
d087bb3e | 182 | err = pthread_sigmask (SIG_SETMASK, &old, NULL); |
183 | if (err) { | |
184 | dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n", | |
185 | strerror (errno)); | |
186 | /* We have failed to restore original signal mask, all bets are off, | |
187 | so exit the process */ | |
188 | exit (EXIT_FAILURE); | |
189 | } | |
e784ba70 | 190 | #endif |
ce31f099 | 191 | return devid; |
85571bc7 FB |
192 | } |
193 | ||
ce31f099 | 194 | static void sdl_close_out(SDLVoiceOut *sdl) |
85571bc7 | 195 | { |
ce31f099 VR |
196 | if (sdl->initialized) { |
197 | SDL_LockAudioDevice(sdl->devid); | |
198 | sdl->exit = 1; | |
199 | SDL_UnlockAudioDevice(sdl->devid); | |
200 | SDL_PauseAudioDevice(sdl->devid, 1); | |
201 | sdl->initialized = 0; | |
202 | } | |
203 | if (sdl->devid) { | |
204 | SDL_CloseAudioDevice(sdl->devid); | |
205 | sdl->devid = 0; | |
85571bc7 FB |
206 | } |
207 | } | |
208 | ||
ce31f099 | 209 | static void sdl_callback_out(void *opaque, Uint8 *buf, int len) |
85571bc7 | 210 | { |
1d14ffa9 | 211 | SDLVoiceOut *sdl = opaque; |
1d14ffa9 | 212 | HWVoiceOut *hw = &sdl->hw; |
85571bc7 | 213 | |
ce31f099 | 214 | if (!sdl->exit) { |
85571bc7 | 215 | |
ce31f099 | 216 | /* dolog("callback_out: len=%d avail=%zu\n", len, hw->pending_emul); */ |
9399ef16 | 217 | |
bcce2ea5 VR |
218 | while (hw->pending_emul && len) { |
219 | size_t write_len; | |
220 | ssize_t start = (ssize_t)hw->pos_emul - hw->pending_emul; | |
221 | if (start < 0) { | |
222 | start += hw->size_emul; | |
223 | } | |
224 | assert(start >= 0 && start < hw->size_emul); | |
ff718767 | 225 | |
bcce2ea5 VR |
226 | write_len = MIN(MIN(hw->pending_emul, len), |
227 | hw->size_emul - start); | |
ff718767 | 228 | |
bcce2ea5 VR |
229 | memcpy(buf, hw->buf_emul + start, write_len); |
230 | hw->pending_emul -= write_len; | |
231 | len -= write_len; | |
232 | buf += write_len; | |
233 | } | |
85571bc7 | 234 | } |
9399ef16 | 235 | |
ff718767 KZ |
236 | /* clear remaining buffer that we couldn't fill with data */ |
237 | if (len) { | |
e02d178f VR |
238 | audio_pcm_info_clear_buf(&hw->info, buf, |
239 | len / hw->info.bytes_per_frame); | |
bcf19777 | 240 | } |
85571bc7 FB |
241 | } |
242 | ||
ce31f099 | 243 | #define SDL_WRAPPER_FUNC(name, ret_type, args_decl, args, dir) \ |
ef26632e VR |
244 | static ret_type glue(sdl_, name)args_decl \ |
245 | { \ | |
246 | ret_type ret; \ | |
ce31f099 | 247 | glue(SDLVoice, dir) *sdl = (glue(SDLVoice, dir) *)hw; \ |
ef26632e | 248 | \ |
ce31f099 | 249 | SDL_LockAudioDevice(sdl->devid); \ |
ef26632e | 250 | ret = glue(audio_generic_, name)args; \ |
ce31f099 | 251 | SDL_UnlockAudioDevice(sdl->devid); \ |
ef26632e VR |
252 | \ |
253 | return ret; \ | |
ff541499 | 254 | } |
255 | ||
ff718767 | 256 | SDL_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size), |
ce31f099 | 257 | (hw, size), Out) |
fdc8c5f4 | 258 | SDL_WRAPPER_FUNC(put_buffer_out, size_t, |
ce31f099 | 259 | (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), Out) |
ff718767 | 260 | SDL_WRAPPER_FUNC(write, size_t, |
ce31f099 | 261 | (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), Out) |
ff718767 | 262 | #undef SDL_WRAPPER_FUNC |
1d14ffa9 | 263 | |
ce31f099 | 264 | static void sdl_fini_out(HWVoiceOut *hw) |
1d14ffa9 | 265 | { |
ce31f099 | 266 | SDLVoiceOut *sdl = (SDLVoiceOut *)hw; |
1d14ffa9 | 267 | |
ce31f099 | 268 | sdl_close_out(sdl); |
85571bc7 FB |
269 | } |
270 | ||
5706db1d KZ |
271 | static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as, |
272 | void *drv_opaque) | |
85571bc7 | 273 | { |
ce31f099 | 274 | SDLVoiceOut *sdl = (SDLVoiceOut *)hw; |
85571bc7 | 275 | SDL_AudioSpec req, obt; |
4ff9786c | 276 | int endianness; |
1d14ffa9 | 277 | int err; |
85bc5852 | 278 | AudioFormat effective_fmt; |
ce31f099 VR |
279 | Audiodev *dev = drv_opaque; |
280 | AudiodevSdlPerDirectionOptions *spdo = dev->u.sdl.out; | |
1ea879e5 | 281 | struct audsettings obt_as; |
85571bc7 | 282 | |
c0fe3827 | 283 | req.freq = as->freq; |
6c557ab9 | 284 | req.format = aud_to_sdlfmt (as->fmt); |
c0fe3827 | 285 | req.channels = as->nchannels; |
5a0926c2 VR |
286 | /* |
287 | * This is wrong. SDL samples are QEMU frames. The buffer size will be | |
288 | * the requested buffer size multiplied by the number of channels. | |
289 | */ | |
290 | req.samples = audio_buffer_samples( | |
291 | qapi_AudiodevSdlPerDirectionOptions_base(spdo), as, 11610); | |
ce31f099 | 292 | req.callback = sdl_callback_out; |
85571bc7 FB |
293 | req.userdata = sdl; |
294 | ||
ce31f099 VR |
295 | sdl->dev = dev; |
296 | sdl->devid = sdl_open(&req, &obt, 0); | |
297 | if (!sdl->devid) { | |
1d14ffa9 FB |
298 | return -1; |
299 | } | |
300 | ||
4ff9786c | 301 | err = sdl_to_audfmt(obt.format, &effective_fmt, &endianness); |
1d14ffa9 | 302 | if (err) { |
ce31f099 | 303 | sdl_close_out(sdl); |
85571bc7 | 304 | return -1; |
1d14ffa9 | 305 | } |
85571bc7 | 306 | |
c0fe3827 FB |
307 | obt_as.freq = obt.freq; |
308 | obt_as.nchannels = obt.channels; | |
309 | obt_as.fmt = effective_fmt; | |
4ff9786c | 310 | obt_as.endianness = endianness; |
c0fe3827 | 311 | |
d929eba5 | 312 | audio_pcm_init_info (&hw->info, &obt_as); |
5a0926c2 VR |
313 | hw->samples = (spdo->has_buffer_count ? spdo->buffer_count : 4) * |
314 | obt.samples; | |
85571bc7 | 315 | |
ce31f099 VR |
316 | sdl->initialized = 1; |
317 | sdl->exit = 0; | |
85571bc7 FB |
318 | return 0; |
319 | } | |
320 | ||
571a8c52 | 321 | static void sdl_enable_out(HWVoiceOut *hw, bool enable) |
85571bc7 | 322 | { |
ce31f099 VR |
323 | SDLVoiceOut *sdl = (SDLVoiceOut *)hw; |
324 | ||
325 | SDL_PauseAudioDevice(sdl->devid, !enable); | |
85571bc7 FB |
326 | } |
327 | ||
71830221 | 328 | static void *sdl_audio_init(Audiodev *dev) |
85571bc7 | 329 | { |
85571bc7 | 330 | if (SDL_InitSubSystem (SDL_INIT_AUDIO)) { |
1d14ffa9 | 331 | sdl_logerr ("SDL failed to initialize audio subsystem\n"); |
85571bc7 FB |
332 | return NULL; |
333 | } | |
334 | ||
ce31f099 | 335 | return dev; |
85571bc7 FB |
336 | } |
337 | ||
338 | static void sdl_audio_fini (void *opaque) | |
339 | { | |
85571bc7 FB |
340 | SDL_QuitSubSystem (SDL_INIT_AUDIO); |
341 | } | |
342 | ||
35f4b58c | 343 | static struct audio_pcm_ops sdl_pcm_ops = { |
1dd3e4d1 JQ |
344 | .init_out = sdl_init_out, |
345 | .fini_out = sdl_fini_out, | |
fdc8c5f4 | 346 | /* wrapper for audio_generic_write */ |
ff718767 | 347 | .write = sdl_write, |
fdc8c5f4 | 348 | /* wrapper for audio_generic_get_buffer_out */ |
ff718767 | 349 | .get_buffer_out = sdl_get_buffer_out, |
fdc8c5f4 VR |
350 | /* wrapper for audio_generic_put_buffer_out */ |
351 | .put_buffer_out = sdl_put_buffer_out, | |
571a8c52 | 352 | .enable_out = sdl_enable_out, |
85571bc7 FB |
353 | }; |
354 | ||
d3893a39 | 355 | static struct audio_driver sdl_audio_driver = { |
bee37f32 JQ |
356 | .name = "sdl", |
357 | .descr = "SDL http://www.libsdl.org", | |
bee37f32 JQ |
358 | .init = sdl_audio_init, |
359 | .fini = sdl_audio_fini, | |
360 | .pcm_ops = &sdl_pcm_ops, | |
361 | .can_be_default = 1, | |
362 | .max_voices_out = 1, | |
363 | .max_voices_in = 0, | |
364 | .voice_size_out = sizeof (SDLVoiceOut), | |
365 | .voice_size_in = 0 | |
85571bc7 | 366 | }; |
d3893a39 GH |
367 | |
368 | static void register_audio_sdl(void) | |
369 | { | |
370 | audio_driver_register(&sdl_audio_driver); | |
371 | } | |
372 | type_init(register_audio_sdl); |