]> git.proxmox.com Git - mirror_qemu.git/blame - audio/sdlaudio.c
audio: Require AudioState in AUD_add_capture
[mirror_qemu.git] / audio / sdlaudio.c
CommitLineData
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
42typedef 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
c2031dea
VR
50typedef struct SDLVoiceIn {
51 HWVoiceIn hw;
52 int exit;
53 int initialized;
54 Audiodev *dev;
55 SDL_AudioDeviceID devid;
56} SDLVoiceIn;
57
9edc6313 58static void G_GNUC_PRINTF (1, 2) sdl_logerr (const char *fmt, ...)
85571bc7 59{
1d14ffa9
FB
60 va_list ap;
61
62 va_start (ap, fmt);
63 AUD_vlog (AUDIO_CAP, fmt, ap);
64 va_end (ap);
65
66 AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ());
85571bc7
FB
67}
68
85bc5852 69static int aud_to_sdlfmt (AudioFormat fmt)
85571bc7 70{
85571bc7 71 switch (fmt) {
85bc5852 72 case AUDIO_FORMAT_S8:
1d14ffa9
FB
73 return AUDIO_S8;
74
85bc5852 75 case AUDIO_FORMAT_U8:
1d14ffa9
FB
76 return AUDIO_U8;
77
85bc5852 78 case AUDIO_FORMAT_S16:
1d14ffa9
FB
79 return AUDIO_S16LSB;
80
85bc5852 81 case AUDIO_FORMAT_U16:
1d14ffa9
FB
82 return AUDIO_U16LSB;
83
ed2a4a79
KZ
84 case AUDIO_FORMAT_S32:
85 return AUDIO_S32LSB;
86
87 /* no unsigned 32-bit support in SDL */
88
89 case AUDIO_FORMAT_F32:
90 return AUDIO_F32LSB;
91
85571bc7 92 default:
1d14ffa9
FB
93 dolog ("Internal logic error: Bad audio format %d\n", fmt);
94#ifdef DEBUG_AUDIO
95 abort ();
96#endif
97 return AUDIO_U8;
85571bc7
FB
98 }
99}
100
85bc5852 101static int sdl_to_audfmt(int sdlfmt, AudioFormat *fmt, int *endianness)
85571bc7 102{
1d14ffa9
FB
103 switch (sdlfmt) {
104 case AUDIO_S8:
4ff9786c 105 *endianness = 0;
85bc5852 106 *fmt = AUDIO_FORMAT_S8;
1d14ffa9
FB
107 break;
108
109 case AUDIO_U8:
4ff9786c 110 *endianness = 0;
85bc5852 111 *fmt = AUDIO_FORMAT_U8;
1d14ffa9
FB
112 break;
113
114 case AUDIO_S16LSB:
4ff9786c 115 *endianness = 0;
85bc5852 116 *fmt = AUDIO_FORMAT_S16;
1d14ffa9
FB
117 break;
118
119 case AUDIO_U16LSB:
4ff9786c 120 *endianness = 0;
85bc5852 121 *fmt = AUDIO_FORMAT_U16;
1d14ffa9
FB
122 break;
123
124 case AUDIO_S16MSB:
4ff9786c 125 *endianness = 1;
85bc5852 126 *fmt = AUDIO_FORMAT_S16;
1d14ffa9
FB
127 break;
128
129 case AUDIO_U16MSB:
4ff9786c 130 *endianness = 1;
85bc5852 131 *fmt = AUDIO_FORMAT_U16;
1d14ffa9
FB
132 break;
133
ed2a4a79
KZ
134 case AUDIO_S32LSB:
135 *endianness = 0;
136 *fmt = AUDIO_FORMAT_S32;
137 break;
138
139 case AUDIO_S32MSB:
140 *endianness = 1;
141 *fmt = AUDIO_FORMAT_S32;
142 break;
143
144 case AUDIO_F32LSB:
145 *endianness = 0;
146 *fmt = AUDIO_FORMAT_F32;
147 break;
148
149 case AUDIO_F32MSB:
150 *endianness = 1;
151 *fmt = AUDIO_FORMAT_F32;
152 break;
153
85571bc7 154 default:
1d14ffa9
FB
155 dolog ("Unrecognized SDL audio format %d\n", sdlfmt);
156 return -1;
85571bc7 157 }
1d14ffa9
FB
158
159 return 0;
85571bc7
FB
160}
161
ce31f099
VR
162static SDL_AudioDeviceID sdl_open(SDL_AudioSpec *req, SDL_AudioSpec *obt,
163 int rec)
85571bc7 164{
ce31f099 165 SDL_AudioDeviceID devid;
e784ba70 166#ifndef _WIN32
d087bb3e 167 int err;
e784ba70
TS
168 sigset_t new, old;
169
170 /* Make sure potential threads created by SDL don't hog signals. */
d087bb3e 171 err = sigfillset (&new);
172 if (err) {
173 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno));
ce31f099 174 return 0;
d087bb3e 175 }
176 err = pthread_sigmask (SIG_BLOCK, &new, &old);
177 if (err) {
178 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err));
ce31f099 179 return 0;
d087bb3e 180 }
e784ba70 181#endif
85571bc7 182
ce31f099
VR
183 devid = SDL_OpenAudioDevice(NULL, rec, req, obt, 0);
184 if (!devid) {
185 sdl_logerr("SDL_OpenAudioDevice for %s failed\n",
186 rec ? "recording" : "playback");
85571bc7 187 }
e784ba70
TS
188
189#ifndef _WIN32
d087bb3e 190 err = pthread_sigmask (SIG_SETMASK, &old, NULL);
191 if (err) {
192 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
193 strerror (errno));
194 /* We have failed to restore original signal mask, all bets are off,
195 so exit the process */
196 exit (EXIT_FAILURE);
197 }
e784ba70 198#endif
ce31f099 199 return devid;
85571bc7
FB
200}
201
ce31f099 202static void sdl_close_out(SDLVoiceOut *sdl)
85571bc7 203{
ce31f099
VR
204 if (sdl->initialized) {
205 SDL_LockAudioDevice(sdl->devid);
206 sdl->exit = 1;
207 SDL_UnlockAudioDevice(sdl->devid);
208 SDL_PauseAudioDevice(sdl->devid, 1);
209 sdl->initialized = 0;
210 }
211 if (sdl->devid) {
212 SDL_CloseAudioDevice(sdl->devid);
213 sdl->devid = 0;
85571bc7
FB
214 }
215}
216
ce31f099 217static void sdl_callback_out(void *opaque, Uint8 *buf, int len)
85571bc7 218{
1d14ffa9 219 SDLVoiceOut *sdl = opaque;
1d14ffa9 220 HWVoiceOut *hw = &sdl->hw;
85571bc7 221
ce31f099 222 if (!sdl->exit) {
85571bc7 223
ce31f099 224 /* dolog("callback_out: len=%d avail=%zu\n", len, hw->pending_emul); */
9399ef16 225
bcce2ea5 226 while (hw->pending_emul && len) {
18404ff1
VR
227 size_t write_len, start;
228
229 start = audio_ring_posb(hw->pos_emul, hw->pending_emul,
230 hw->size_emul);
231 assert(start < hw->size_emul);
ff718767 232
bcce2ea5
VR
233 write_len = MIN(MIN(hw->pending_emul, len),
234 hw->size_emul - start);
ff718767 235
bcce2ea5
VR
236 memcpy(buf, hw->buf_emul + start, write_len);
237 hw->pending_emul -= write_len;
238 len -= write_len;
239 buf += write_len;
240 }
85571bc7 241 }
9399ef16 242
ff718767
KZ
243 /* clear remaining buffer that we couldn't fill with data */
244 if (len) {
e02d178f
VR
245 audio_pcm_info_clear_buf(&hw->info, buf,
246 len / hw->info.bytes_per_frame);
bcf19777 247 }
85571bc7
FB
248}
249
c2031dea
VR
250static void sdl_close_in(SDLVoiceIn *sdl)
251{
252 if (sdl->initialized) {
253 SDL_LockAudioDevice(sdl->devid);
254 sdl->exit = 1;
255 SDL_UnlockAudioDevice(sdl->devid);
256 SDL_PauseAudioDevice(sdl->devid, 1);
257 sdl->initialized = 0;
258 }
259 if (sdl->devid) {
260 SDL_CloseAudioDevice(sdl->devid);
261 sdl->devid = 0;
262 }
263}
264
265static void sdl_callback_in(void *opaque, Uint8 *buf, int len)
266{
267 SDLVoiceIn *sdl = opaque;
268 HWVoiceIn *hw = &sdl->hw;
269
270 if (sdl->exit) {
271 return;
272 }
273
274 /* dolog("callback_in: len=%d pending=%zu\n", len, hw->pending_emul); */
275
276 while (hw->pending_emul < hw->size_emul && len) {
277 size_t read_len = MIN(len, MIN(hw->size_emul - hw->pos_emul,
278 hw->size_emul - hw->pending_emul));
279
280 memcpy(hw->buf_emul + hw->pos_emul, buf, read_len);
281
282 hw->pending_emul += read_len;
283 hw->pos_emul = (hw->pos_emul + read_len) % hw->size_emul;
284 len -= read_len;
285 buf += read_len;
286 }
287}
288
ce31f099 289#define SDL_WRAPPER_FUNC(name, ret_type, args_decl, args, dir) \
ef26632e
VR
290 static ret_type glue(sdl_, name)args_decl \
291 { \
292 ret_type ret; \
ce31f099 293 glue(SDLVoice, dir) *sdl = (glue(SDLVoice, dir) *)hw; \
ef26632e 294 \
ce31f099 295 SDL_LockAudioDevice(sdl->devid); \
ef26632e 296 ret = glue(audio_generic_, name)args; \
ce31f099 297 SDL_UnlockAudioDevice(sdl->devid); \
ef26632e
VR
298 \
299 return ret; \
ff541499 300 }
301
c2031dea
VR
302#define SDL_WRAPPER_VOID_FUNC(name, args_decl, args, dir) \
303 static void glue(sdl_, name)args_decl \
304 { \
305 glue(SDLVoice, dir) *sdl = (glue(SDLVoice, dir) *)hw; \
306 \
307 SDL_LockAudioDevice(sdl->devid); \
308 glue(audio_generic_, name)args; \
309 SDL_UnlockAudioDevice(sdl->devid); \
310 }
311
9833438e 312SDL_WRAPPER_FUNC(buffer_get_free, size_t, (HWVoiceOut *hw), (hw), Out)
ff718767 313SDL_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size),
ce31f099 314 (hw, size), Out)
fdc8c5f4 315SDL_WRAPPER_FUNC(put_buffer_out, size_t,
ce31f099 316 (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), Out)
ff718767 317SDL_WRAPPER_FUNC(write, size_t,
ce31f099 318 (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), Out)
c2031dea
VR
319SDL_WRAPPER_FUNC(read, size_t, (HWVoiceIn *hw, void *buf, size_t size),
320 (hw, buf, size), In)
321SDL_WRAPPER_FUNC(get_buffer_in, void *, (HWVoiceIn *hw, size_t *size),
322 (hw, size), In)
323SDL_WRAPPER_VOID_FUNC(put_buffer_in, (HWVoiceIn *hw, void *buf, size_t size),
324 (hw, buf, size), In)
ff718767 325#undef SDL_WRAPPER_FUNC
c2031dea 326#undef SDL_WRAPPER_VOID_FUNC
1d14ffa9 327
ce31f099 328static void sdl_fini_out(HWVoiceOut *hw)
1d14ffa9 329{
ce31f099 330 SDLVoiceOut *sdl = (SDLVoiceOut *)hw;
1d14ffa9 331
ce31f099 332 sdl_close_out(sdl);
85571bc7
FB
333}
334
5706db1d
KZ
335static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,
336 void *drv_opaque)
85571bc7 337{
ce31f099 338 SDLVoiceOut *sdl = (SDLVoiceOut *)hw;
85571bc7 339 SDL_AudioSpec req, obt;
4ff9786c 340 int endianness;
1d14ffa9 341 int err;
85bc5852 342 AudioFormat effective_fmt;
ce31f099
VR
343 Audiodev *dev = drv_opaque;
344 AudiodevSdlPerDirectionOptions *spdo = dev->u.sdl.out;
1ea879e5 345 struct audsettings obt_as;
85571bc7 346
c0fe3827 347 req.freq = as->freq;
6c557ab9 348 req.format = aud_to_sdlfmt (as->fmt);
c0fe3827 349 req.channels = as->nchannels;
7b672528
VR
350 /* SDL samples are QEMU frames */
351 req.samples = audio_buffer_frames(
5a0926c2 352 qapi_AudiodevSdlPerDirectionOptions_base(spdo), as, 11610);
ce31f099 353 req.callback = sdl_callback_out;
85571bc7
FB
354 req.userdata = sdl;
355
ce31f099
VR
356 sdl->dev = dev;
357 sdl->devid = sdl_open(&req, &obt, 0);
358 if (!sdl->devid) {
1d14ffa9
FB
359 return -1;
360 }
361
4ff9786c 362 err = sdl_to_audfmt(obt.format, &effective_fmt, &endianness);
1d14ffa9 363 if (err) {
ce31f099 364 sdl_close_out(sdl);
85571bc7 365 return -1;
1d14ffa9 366 }
85571bc7 367
c0fe3827
FB
368 obt_as.freq = obt.freq;
369 obt_as.nchannels = obt.channels;
370 obt_as.fmt = effective_fmt;
4ff9786c 371 obt_as.endianness = endianness;
c0fe3827 372
d929eba5 373 audio_pcm_init_info (&hw->info, &obt_as);
5a0926c2
VR
374 hw->samples = (spdo->has_buffer_count ? spdo->buffer_count : 4) *
375 obt.samples;
85571bc7 376
ce31f099
VR
377 sdl->initialized = 1;
378 sdl->exit = 0;
85571bc7
FB
379 return 0;
380}
381
571a8c52 382static void sdl_enable_out(HWVoiceOut *hw, bool enable)
85571bc7 383{
ce31f099
VR
384 SDLVoiceOut *sdl = (SDLVoiceOut *)hw;
385
386 SDL_PauseAudioDevice(sdl->devid, !enable);
85571bc7
FB
387}
388
c2031dea
VR
389static void sdl_fini_in(HWVoiceIn *hw)
390{
391 SDLVoiceIn *sdl = (SDLVoiceIn *)hw;
392
393 sdl_close_in(sdl);
394}
395
396static int sdl_init_in(HWVoiceIn *hw, audsettings *as, void *drv_opaque)
397{
398 SDLVoiceIn *sdl = (SDLVoiceIn *)hw;
399 SDL_AudioSpec req, obt;
400 int endianness;
401 int err;
402 AudioFormat effective_fmt;
403 Audiodev *dev = drv_opaque;
404 AudiodevSdlPerDirectionOptions *spdo = dev->u.sdl.in;
405 struct audsettings obt_as;
406
407 req.freq = as->freq;
408 req.format = aud_to_sdlfmt(as->fmt);
409 req.channels = as->nchannels;
410 /* SDL samples are QEMU frames */
411 req.samples = audio_buffer_frames(
412 qapi_AudiodevSdlPerDirectionOptions_base(spdo), as, 11610);
413 req.callback = sdl_callback_in;
414 req.userdata = sdl;
415
416 sdl->dev = dev;
417 sdl->devid = sdl_open(&req, &obt, 1);
418 if (!sdl->devid) {
419 return -1;
420 }
421
422 err = sdl_to_audfmt(obt.format, &effective_fmt, &endianness);
423 if (err) {
424 sdl_close_in(sdl);
425 return -1;
426 }
427
428 obt_as.freq = obt.freq;
429 obt_as.nchannels = obt.channels;
430 obt_as.fmt = effective_fmt;
431 obt_as.endianness = endianness;
432
433 audio_pcm_init_info(&hw->info, &obt_as);
434 hw->samples = (spdo->has_buffer_count ? spdo->buffer_count : 4) *
435 obt.samples;
436 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
437 hw->buf_emul = g_malloc(hw->size_emul);
438 hw->pos_emul = hw->pending_emul = 0;
439
440 sdl->initialized = 1;
441 sdl->exit = 0;
442 return 0;
443}
444
445static void sdl_enable_in(HWVoiceIn *hw, bool enable)
446{
447 SDLVoiceIn *sdl = (SDLVoiceIn *)hw;
448
449 SDL_PauseAudioDevice(sdl->devid, !enable);
450}
451
71830221 452static void *sdl_audio_init(Audiodev *dev)
85571bc7 453{
85571bc7 454 if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
1d14ffa9 455 sdl_logerr ("SDL failed to initialize audio subsystem\n");
85571bc7
FB
456 return NULL;
457 }
458
ce31f099 459 return dev;
85571bc7
FB
460}
461
462static void sdl_audio_fini (void *opaque)
463{
85571bc7
FB
464 SDL_QuitSubSystem (SDL_INIT_AUDIO);
465}
466
35f4b58c 467static struct audio_pcm_ops sdl_pcm_ops = {
1dd3e4d1
JQ
468 .init_out = sdl_init_out,
469 .fini_out = sdl_fini_out,
fdc8c5f4 470 /* wrapper for audio_generic_write */
ff718767 471 .write = sdl_write,
9833438e
VR
472 /* wrapper for audio_generic_buffer_get_free */
473 .buffer_get_free = sdl_buffer_get_free,
fdc8c5f4 474 /* wrapper for audio_generic_get_buffer_out */
ff718767 475 .get_buffer_out = sdl_get_buffer_out,
fdc8c5f4
VR
476 /* wrapper for audio_generic_put_buffer_out */
477 .put_buffer_out = sdl_put_buffer_out,
571a8c52 478 .enable_out = sdl_enable_out,
c2031dea
VR
479 .init_in = sdl_init_in,
480 .fini_in = sdl_fini_in,
481 /* wrapper for audio_generic_read */
482 .read = sdl_read,
483 /* wrapper for audio_generic_get_buffer_in */
484 .get_buffer_in = sdl_get_buffer_in,
485 /* wrapper for audio_generic_put_buffer_in */
486 .put_buffer_in = sdl_put_buffer_in,
487 .enable_in = sdl_enable_in,
85571bc7
FB
488};
489
d3893a39 490static struct audio_driver sdl_audio_driver = {
bee37f32
JQ
491 .name = "sdl",
492 .descr = "SDL http://www.libsdl.org",
bee37f32
JQ
493 .init = sdl_audio_init,
494 .fini = sdl_audio_fini,
495 .pcm_ops = &sdl_pcm_ops,
496 .can_be_default = 1,
bd37ede4
VR
497 .max_voices_out = INT_MAX,
498 .max_voices_in = INT_MAX,
c2031dea
VR
499 .voice_size_out = sizeof(SDLVoiceOut),
500 .voice_size_in = sizeof(SDLVoiceIn),
85571bc7 501};
d3893a39
GH
502
503static void register_audio_sdl(void)
504{
505 audio_driver_register(&sdl_audio_driver);
506}
507type_init(register_audio_sdl);