blob: 21b7a0484bec152264da748a1d07d42d7617db06 [file] [log] [blame]
bellard85571bc2004-11-07 18:04:02 +00001/*
bellard1d14ffa2005-10-30 18:58:22 +00002 * QEMU SDL audio driver
3 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 *
bellard85571bc2004-11-07 18:04:02 +00006 * 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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydell6086a562016-01-18 17:33:52 +000025#include "qemu/osdep.h"
bellard9f059ec2004-11-14 18:59:52 +000026#include <SDL.h>
27#include <SDL_thread.h>
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028#include "qemu/module.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "audio.h"
bellard85571bc2004-11-07 18:04:02 +000030
thse784ba72007-07-11 23:23:15 +000031#ifndef _WIN32
32#ifdef __sun__
33#define _POSIX_PTHREAD_SEMANTICS 1
blueswir1c5e97232009-03-07 20:06:23 +000034#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
blueswir19b4c14c2008-10-25 11:19:14 +000035#include <pthread.h>
thse784ba72007-07-11 23:23:15 +000036#endif
thse784ba72007-07-11 23:23:15 +000037#endif
38
bellard1d14ffa2005-10-30 18:58:22 +000039#define AUDIO_CAP "sdl"
40#include "audio_int.h"
bellardfb065182004-11-09 23:09:44 +000041
bellard1d14ffa2005-10-30 18:58:22 +000042typedef struct SDLVoiceOut {
43 HWVoiceOut hw;
bellard1d14ffa2005-10-30 18:58:22 +000044} SDLVoiceOut;
bellard85571bc2004-11-07 18:04:02 +000045
blueswir1b1d8e522008-10-26 13:43:07 +000046static struct SDLAudioState {
bellard85571bc2004-11-07 18:04:02 +000047 int exit;
bellard85571bc2004-11-07 18:04:02 +000048 int initialized;
Kővágó, Zoltán81ebb072015-06-03 23:03:55 +020049 bool driver_created;
Kővágó, Zoltán57dea552019-03-08 23:34:22 +010050 Audiodev *dev;
bellard85571bc2004-11-07 18:04:02 +000051} glob_sdl;
52typedef struct SDLAudioState SDLAudioState;
53
bellard1d14ffa2005-10-30 18:58:22 +000054static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...)
bellard85571bc2004-11-07 18:04:02 +000055{
bellard1d14ffa2005-10-30 18:58:22 +000056 va_list ap;
57
58 va_start (ap, fmt);
59 AUD_vlog (AUDIO_CAP, fmt, ap);
60 va_end (ap);
61
62 AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ());
bellard85571bc2004-11-07 18:04:02 +000063}
64
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +010065static int aud_to_sdlfmt (AudioFormat fmt)
bellard85571bc2004-11-07 18:04:02 +000066{
bellard85571bc2004-11-07 18:04:02 +000067 switch (fmt) {
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +010068 case AUDIO_FORMAT_S8:
bellard1d14ffa2005-10-30 18:58:22 +000069 return AUDIO_S8;
70
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +010071 case AUDIO_FORMAT_U8:
bellard1d14ffa2005-10-30 18:58:22 +000072 return AUDIO_U8;
73
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +010074 case AUDIO_FORMAT_S16:
bellard1d14ffa2005-10-30 18:58:22 +000075 return AUDIO_S16LSB;
76
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +010077 case AUDIO_FORMAT_U16:
bellard1d14ffa2005-10-30 18:58:22 +000078 return AUDIO_U16LSB;
79
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +010080 case AUDIO_FORMAT_S32:
81 return AUDIO_S32LSB;
82
83 /* no unsigned 32-bit support in SDL */
84
85 case AUDIO_FORMAT_F32:
86 return AUDIO_F32LSB;
87
bellard85571bc2004-11-07 18:04:02 +000088 default:
bellard1d14ffa2005-10-30 18:58:22 +000089 dolog ("Internal logic error: Bad audio format %d\n", fmt);
90#ifdef DEBUG_AUDIO
91 abort ();
92#endif
93 return AUDIO_U8;
bellard85571bc2004-11-07 18:04:02 +000094 }
95}
96
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +010097static int sdl_to_audfmt(int sdlfmt, AudioFormat *fmt, int *endianness)
bellard85571bc2004-11-07 18:04:02 +000098{
bellard1d14ffa2005-10-30 18:58:22 +000099 switch (sdlfmt) {
100 case AUDIO_S8:
Stefan Weil4ff97862011-03-13 15:44:02 +0100101 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100102 *fmt = AUDIO_FORMAT_S8;
bellard1d14ffa2005-10-30 18:58:22 +0000103 break;
104
105 case AUDIO_U8:
Stefan Weil4ff97862011-03-13 15:44:02 +0100106 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100107 *fmt = AUDIO_FORMAT_U8;
bellard1d14ffa2005-10-30 18:58:22 +0000108 break;
109
110 case AUDIO_S16LSB:
Stefan Weil4ff97862011-03-13 15:44:02 +0100111 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100112 *fmt = AUDIO_FORMAT_S16;
bellard1d14ffa2005-10-30 18:58:22 +0000113 break;
114
115 case AUDIO_U16LSB:
Stefan Weil4ff97862011-03-13 15:44:02 +0100116 *endianness = 0;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100117 *fmt = AUDIO_FORMAT_U16;
bellard1d14ffa2005-10-30 18:58:22 +0000118 break;
119
120 case AUDIO_S16MSB:
Stefan Weil4ff97862011-03-13 15:44:02 +0100121 *endianness = 1;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100122 *fmt = AUDIO_FORMAT_S16;
bellard1d14ffa2005-10-30 18:58:22 +0000123 break;
124
125 case AUDIO_U16MSB:
Stefan Weil4ff97862011-03-13 15:44:02 +0100126 *endianness = 1;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100127 *fmt = AUDIO_FORMAT_U16;
bellard1d14ffa2005-10-30 18:58:22 +0000128 break;
129
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100130 case AUDIO_S32LSB:
131 *endianness = 0;
132 *fmt = AUDIO_FORMAT_S32;
133 break;
134
135 case AUDIO_S32MSB:
136 *endianness = 1;
137 *fmt = AUDIO_FORMAT_S32;
138 break;
139
140 case AUDIO_F32LSB:
141 *endianness = 0;
142 *fmt = AUDIO_FORMAT_F32;
143 break;
144
145 case AUDIO_F32MSB:
146 *endianness = 1;
147 *fmt = AUDIO_FORMAT_F32;
148 break;
149
bellard85571bc2004-11-07 18:04:02 +0000150 default:
bellard1d14ffa2005-10-30 18:58:22 +0000151 dolog ("Unrecognized SDL audio format %d\n", sdlfmt);
152 return -1;
bellard85571bc2004-11-07 18:04:02 +0000153 }
bellard1d14ffa2005-10-30 18:58:22 +0000154
155 return 0;
bellard85571bc2004-11-07 18:04:02 +0000156}
157
158static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt)
159{
160 int status;
thse784ba72007-07-11 23:23:15 +0000161#ifndef _WIN32
malcd087bb32010-08-06 13:09:41 +0400162 int err;
thse784ba72007-07-11 23:23:15 +0000163 sigset_t new, old;
164
165 /* Make sure potential threads created by SDL don't hog signals. */
malcd087bb32010-08-06 13:09:41 +0400166 err = sigfillset (&new);
167 if (err) {
168 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno));
malc60592ed2010-08-07 20:03:05 +0400169 return -1;
malcd087bb32010-08-06 13:09:41 +0400170 }
171 err = pthread_sigmask (SIG_BLOCK, &new, &old);
172 if (err) {
173 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err));
174 return -1;
175 }
thse784ba72007-07-11 23:23:15 +0000176#endif
bellard85571bc2004-11-07 18:04:02 +0000177
178 status = SDL_OpenAudio (req, obt);
179 if (status) {
bellard1d14ffa2005-10-30 18:58:22 +0000180 sdl_logerr ("SDL_OpenAudio failed\n");
bellard85571bc2004-11-07 18:04:02 +0000181 }
thse784ba72007-07-11 23:23:15 +0000182
183#ifndef _WIN32
malcd087bb32010-08-06 13:09:41 +0400184 err = pthread_sigmask (SIG_SETMASK, &old, NULL);
185 if (err) {
186 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
187 strerror (errno));
188 /* We have failed to restore original signal mask, all bets are off,
189 so exit the process */
190 exit (EXIT_FAILURE);
191 }
thse784ba72007-07-11 23:23:15 +0000192#endif
bellard85571bc2004-11-07 18:04:02 +0000193 return status;
194}
195
196static void sdl_close (SDLAudioState *s)
197{
198 if (s->initialized) {
Thomas Huth8a7816c2019-02-05 04:08:20 +0100199 SDL_LockAudio();
bellard85571bc2004-11-07 18:04:02 +0000200 s->exit = 1;
Thomas Huth8a7816c2019-02-05 04:08:20 +0100201 SDL_UnlockAudio();
bellard85571bc2004-11-07 18:04:02 +0000202 SDL_PauseAudio (1);
203 SDL_CloseAudio ();
204 s->initialized = 0;
205 }
206}
207
208static void sdl_callback (void *opaque, Uint8 *buf, int len)
209{
bellard1d14ffa2005-10-30 18:58:22 +0000210 SDLVoiceOut *sdl = opaque;
bellard85571bc2004-11-07 18:04:02 +0000211 SDLAudioState *s = &glob_sdl;
bellard1d14ffa2005-10-30 18:58:22 +0000212 HWVoiceOut *hw = &sdl->hw;
bellard85571bc2004-11-07 18:04:02 +0000213
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200214 if (s->exit) {
bellard85571bc2004-11-07 18:04:02 +0000215 return;
216 }
217
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200218 /* dolog ("in callback samples=%zu live=%zu\n", samples, sdl->live); */
bellard85571bc2004-11-07 18:04:02 +0000219
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200220 while (hw->pending_emul && len) {
221 size_t write_len;
222 ssize_t start = ((ssize_t) hw->pos_emul) - hw->pending_emul;
223 if (start < 0) {
224 start += hw->size_emul;
225 }
226 assert(start >= 0 && start < hw->size_emul);
bellard85571bc2004-11-07 18:04:02 +0000227
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200228 write_len = MIN(MIN(hw->pending_emul, len),
229 hw->size_emul - start);
230
231 memcpy(buf, hw->buf_emul + start, write_len);
232 hw->pending_emul -= write_len;
233 len -= write_len;
234 buf += write_len;
malc4839abe2009-09-30 16:25:55 +0400235 }
Thomas Huth9399ef12019-02-05 04:08:21 +0100236
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200237 /* clear remaining buffer that we couldn't fill with data */
238 if (len) {
239 memset(buf, 0, len);
Thomas Huthbcf19772017-01-31 09:46:38 +0100240 }
bellard85571bc2004-11-07 18:04:02 +0000241}
242
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200243#define SDL_WRAPPER_FUNC(name, ret_type, args_decl, args, fail, unlock) \
244 static ret_type glue(sdl_, name)args_decl \
245 { \
246 ret_type ret; \
247 \
248 SDL_LockAudio(); \
249 \
250 ret = glue(audio_generic_, name)args; \
251 \
252 SDL_UnlockAudio(); \
253 return ret; \
malcff541492010-01-17 00:25:29 +0300254 }
255
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200256SDL_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size),
257 (hw, size), *size = 0, sdl_unlock)
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +0100258SDL_WRAPPER_FUNC(put_buffer_out, size_t,
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200259 (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size),
260 /*nothing*/, sdl_unlock_and_post)
261SDL_WRAPPER_FUNC(write, size_t,
262 (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size),
263 /*nothing*/, sdl_unlock_and_post)
malcff541492010-01-17 00:25:29 +0300264
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200265#undef SDL_WRAPPER_FUNC
bellard1d14ffa2005-10-30 18:58:22 +0000266
267static void sdl_fini_out (HWVoiceOut *hw)
268{
269 (void) hw;
270
bellard85571bc2004-11-07 18:04:02 +0000271 sdl_close (&glob_sdl);
272}
273
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200274static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,
275 void *drv_opaque)
bellard85571bc2004-11-07 18:04:02 +0000276{
bellard1d14ffa2005-10-30 18:58:22 +0000277 SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
bellard85571bc2004-11-07 18:04:02 +0000278 SDLAudioState *s = &glob_sdl;
279 SDL_AudioSpec req, obt;
Stefan Weil4ff97862011-03-13 15:44:02 +0100280 int endianness;
bellard1d14ffa2005-10-30 18:58:22 +0000281 int err;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100282 AudioFormat effective_fmt;
malc1ea879e2008-12-03 22:48:44 +0000283 struct audsettings obt_as;
bellard85571bc2004-11-07 18:04:02 +0000284
bellardc0fe3822005-11-05 18:55:28 +0000285 req.freq = as->freq;
Serge Ziryukin6c557ab2010-04-22 14:14:24 +0300286 req.format = aud_to_sdlfmt (as->fmt);
bellardc0fe3822005-11-05 18:55:28 +0000287 req.channels = as->nchannels;
Kővágó, Zoltán57dea552019-03-08 23:34:22 +0100288 req.samples = audio_buffer_samples(s->dev->u.sdl.out, as, 11610);
bellard85571bc2004-11-07 18:04:02 +0000289 req.callback = sdl_callback;
290 req.userdata = sdl;
291
bellard1d14ffa2005-10-30 18:58:22 +0000292 if (sdl_open (&req, &obt)) {
bellard85571bc2004-11-07 18:04:02 +0000293 return -1;
bellard1d14ffa2005-10-30 18:58:22 +0000294 }
bellard85571bc2004-11-07 18:04:02 +0000295
Stefan Weil4ff97862011-03-13 15:44:02 +0100296 err = sdl_to_audfmt(obt.format, &effective_fmt, &endianness);
bellard1d14ffa2005-10-30 18:58:22 +0000297 if (err) {
298 sdl_close (s);
299 return -1;
300 }
301
bellardc0fe3822005-11-05 18:55:28 +0000302 obt_as.freq = obt.freq;
303 obt_as.nchannels = obt.channels;
304 obt_as.fmt = effective_fmt;
Stefan Weil4ff97862011-03-13 15:44:02 +0100305 obt_as.endianness = endianness;
bellardc0fe3822005-11-05 18:55:28 +0000306
bellardd929eba2006-07-04 21:47:22 +0000307 audio_pcm_init_info (&hw->info, &obt_as);
bellardc0fe3822005-11-05 18:55:28 +0000308 hw->samples = obt.samples;
bellard85571bc2004-11-07 18:04:02 +0000309
310 s->initialized = 1;
311 s->exit = 0;
312 SDL_PauseAudio (0);
313 return 0;
314}
315
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200316static void sdl_enable_out(HWVoiceOut *hw, bool enable)
bellard85571bc2004-11-07 18:04:02 +0000317{
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200318 SDL_PauseAudio(!enable);
bellard85571bc2004-11-07 18:04:02 +0000319}
320
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100321static void *sdl_audio_init(Audiodev *dev)
bellard85571bc2004-11-07 18:04:02 +0000322{
323 SDLAudioState *s = &glob_sdl;
Kővágó, Zoltán81ebb072015-06-03 23:03:55 +0200324 if (s->driver_created) {
325 sdl_logerr("Can't create multiple sdl backends\n");
326 return NULL;
327 }
bellard85571bc2004-11-07 18:04:02 +0000328
329 if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
bellard1d14ffa2005-10-30 18:58:22 +0000330 sdl_logerr ("SDL failed to initialize audio subsystem\n");
bellard85571bc2004-11-07 18:04:02 +0000331 return NULL;
332 }
333
Kővágó, Zoltán81ebb072015-06-03 23:03:55 +0200334 s->driver_created = true;
Kővágó, Zoltán57dea552019-03-08 23:34:22 +0100335 s->dev = dev;
bellard85571bc2004-11-07 18:04:02 +0000336 return s;
337}
338
339static void sdl_audio_fini (void *opaque)
340{
341 SDLAudioState *s = opaque;
342 sdl_close (s);
bellard85571bc2004-11-07 18:04:02 +0000343 SDL_QuitSubSystem (SDL_INIT_AUDIO);
Kővágó, Zoltán81ebb072015-06-03 23:03:55 +0200344 s->driver_created = false;
Kővágó, Zoltán57dea552019-03-08 23:34:22 +0100345 s->dev = NULL;
bellard85571bc2004-11-07 18:04:02 +0000346}
347
blueswir135f4b582008-10-06 18:08:30 +0000348static struct audio_pcm_ops sdl_pcm_ops = {
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200349 .init_out = sdl_init_out,
350 .fini_out = sdl_fini_out,
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +0100351 /* wrapper for audio_generic_write */
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200352 .write = sdl_write,
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +0100353 /* wrapper for audio_generic_get_buffer_out */
Kővágó, Zoltánff718762019-09-19 23:24:16 +0200354 .get_buffer_out = sdl_get_buffer_out,
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +0100355 /* wrapper for audio_generic_put_buffer_out */
356 .put_buffer_out = sdl_put_buffer_out,
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200357 .enable_out = sdl_enable_out,
bellard1d14ffa2005-10-30 18:58:22 +0000358};
359
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100360static struct audio_driver sdl_audio_driver = {
Juan Quintelabee37f32009-08-11 02:31:14 +0200361 .name = "sdl",
362 .descr = "SDL http://www.libsdl.org",
Juan Quintelabee37f32009-08-11 02:31:14 +0200363 .init = sdl_audio_init,
364 .fini = sdl_audio_fini,
365 .pcm_ops = &sdl_pcm_ops,
366 .can_be_default = 1,
367 .max_voices_out = 1,
368 .max_voices_in = 0,
369 .voice_size_out = sizeof (SDLVoiceOut),
370 .voice_size_in = 0
bellard85571bc2004-11-07 18:04:02 +0000371};
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100372
373static void register_audio_sdl(void)
374{
375 audio_driver_register(&sdl_audio_driver);
376}
377type_init(register_audio_sdl);