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