]> git.proxmox.com Git - mirror_qemu.git/blame - audio/sdlaudio.c
audio/sdlaudio: Remove the semaphore code
[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 */
6086a565 24#include "qemu/osdep.h"
9f059eca
FB
25#include <SDL.h>
26#include <SDL_thread.h>
87ecb68b
PB
27#include "qemu-common.h"
28#include "audio.h"
85571bc7 29
e784ba70
TS
30#ifndef _WIN32
31#ifdef __sun__
32#define _POSIX_PTHREAD_SEMANTICS 1
c5e97233 33#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
9b4c14c3 34#include <pthread.h>
e784ba70 35#endif
e784ba70
TS
36#endif
37
1d14ffa9
FB
38#define AUDIO_CAP "sdl"
39#include "audio_int.h"
85571bc7 40
1d14ffa9
FB
41typedef struct SDLVoiceOut {
42 HWVoiceOut hw;
43 int live;
1d14ffa9
FB
44 int decr;
45} SDLVoiceOut;
85571bc7
FB
46
47static struct {
48 int nb_samples;
49} conf = {
1a40d5e2 50 .nb_samples = 1024
85571bc7
FB
51};
52
b1d8e52e 53static struct SDLAudioState {
85571bc7 54 int exit;
85571bc7 55 int initialized;
81ebb07c 56 bool driver_created;
85571bc7
FB
57} glob_sdl;
58typedef struct SDLAudioState SDLAudioState;
59
1d14ffa9 60static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...)
85571bc7 61{
1d14ffa9
FB
62 va_list ap;
63
64 va_start (ap, fmt);
65 AUD_vlog (AUDIO_CAP, fmt, ap);
66 va_end (ap);
67
68 AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ());
85571bc7
FB
69}
70
6c557ab9 71static int aud_to_sdlfmt (audfmt_e fmt)
85571bc7 72{
85571bc7 73 switch (fmt) {
1d14ffa9 74 case AUD_FMT_S8:
1d14ffa9
FB
75 return AUDIO_S8;
76
77 case AUD_FMT_U8:
1d14ffa9
FB
78 return AUDIO_U8;
79
80 case AUD_FMT_S16:
1d14ffa9
FB
81 return AUDIO_S16LSB;
82
83 case AUD_FMT_U16:
1d14ffa9
FB
84 return AUDIO_U16LSB;
85
85571bc7 86 default:
1d14ffa9
FB
87 dolog ("Internal logic error: Bad audio format %d\n", fmt);
88#ifdef DEBUG_AUDIO
89 abort ();
90#endif
91 return AUDIO_U8;
85571bc7
FB
92 }
93}
94
4ff9786c 95static int sdl_to_audfmt(int sdlfmt, audfmt_e *fmt, int *endianness)
85571bc7 96{
1d14ffa9
FB
97 switch (sdlfmt) {
98 case AUDIO_S8:
4ff9786c 99 *endianness = 0;
1d14ffa9
FB
100 *fmt = AUD_FMT_S8;
101 break;
102
103 case AUDIO_U8:
4ff9786c 104 *endianness = 0;
1d14ffa9
FB
105 *fmt = AUD_FMT_U8;
106 break;
107
108 case AUDIO_S16LSB:
4ff9786c 109 *endianness = 0;
1d14ffa9
FB
110 *fmt = AUD_FMT_S16;
111 break;
112
113 case AUDIO_U16LSB:
4ff9786c 114 *endianness = 0;
1d14ffa9
FB
115 *fmt = AUD_FMT_U16;
116 break;
117
118 case AUDIO_S16MSB:
4ff9786c 119 *endianness = 1;
1d14ffa9
FB
120 *fmt = AUD_FMT_S16;
121 break;
122
123 case AUDIO_U16MSB:
4ff9786c 124 *endianness = 1;
1d14ffa9
FB
125 *fmt = AUD_FMT_U16;
126 break;
127
85571bc7 128 default:
1d14ffa9
FB
129 dolog ("Unrecognized SDL audio format %d\n", sdlfmt);
130 return -1;
85571bc7 131 }
1d14ffa9
FB
132
133 return 0;
85571bc7
FB
134}
135
136static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt)
137{
138 int status;
e784ba70 139#ifndef _WIN32
d087bb3e 140 int err;
e784ba70
TS
141 sigset_t new, old;
142
143 /* Make sure potential threads created by SDL don't hog signals. */
d087bb3e 144 err = sigfillset (&new);
145 if (err) {
146 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno));
60592edd 147 return -1;
d087bb3e 148 }
149 err = pthread_sigmask (SIG_BLOCK, &new, &old);
150 if (err) {
151 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err));
152 return -1;
153 }
e784ba70 154#endif
85571bc7
FB
155
156 status = SDL_OpenAudio (req, obt);
157 if (status) {
1d14ffa9 158 sdl_logerr ("SDL_OpenAudio failed\n");
85571bc7 159 }
e784ba70
TS
160
161#ifndef _WIN32
d087bb3e 162 err = pthread_sigmask (SIG_SETMASK, &old, NULL);
163 if (err) {
164 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
165 strerror (errno));
166 /* We have failed to restore original signal mask, all bets are off,
167 so exit the process */
168 exit (EXIT_FAILURE);
169 }
e784ba70 170#endif
85571bc7
FB
171 return status;
172}
173
174static void sdl_close (SDLAudioState *s)
175{
176 if (s->initialized) {
8a7816c4 177 SDL_LockAudio();
85571bc7 178 s->exit = 1;
8a7816c4 179 SDL_UnlockAudio();
85571bc7
FB
180 SDL_PauseAudio (1);
181 SDL_CloseAudio ();
182 s->initialized = 0;
183 }
184}
185
186static void sdl_callback (void *opaque, Uint8 *buf, int len)
187{
1d14ffa9 188 SDLVoiceOut *sdl = opaque;
85571bc7 189 SDLAudioState *s = &glob_sdl;
1d14ffa9
FB
190 HWVoiceOut *hw = &sdl->hw;
191 int samples = len >> hw->info.shift;
85571bc7
FB
192
193 if (s->exit) {
194 return;
195 }
196
197 while (samples) {
1d14ffa9 198 int to_mix, decr;
85571bc7 199
ff541499 200 /* dolog ("in callback samples=%d\n", samples); */
ff541499 201
bcf19777
TH
202 if (s->exit || !sdl->live) {
203 break;
204 }
85571bc7 205
ff541499 206 /* dolog ("in callback live=%d\n", live); */
207 to_mix = audio_MIN (samples, sdl->live);
208 decr = to_mix;
209 while (to_mix) {
210 int chunk = audio_MIN (to_mix, hw->samples - hw->rpos);
211 struct st_sample *src = hw->mix_buf + hw->rpos;
212
213 /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
214 hw->clip (buf, src, chunk);
bcf19777 215 hw->rpos = (hw->rpos + chunk) % hw->samples;
ff541499 216 to_mix -= chunk;
217 buf += chunk << hw->info.shift;
218 }
85571bc7 219 samples -= decr;
ff541499 220 sdl->live -= decr;
1d14ffa9 221 sdl->decr += decr;
85571bc7 222 }
ff541499 223 /* dolog ("done len=%d\n", len); */
bcf19777 224
bcf19777
TH
225 /* SDL2 does not clear the remaining buffer for us, so do it on our own */
226 if (samples) {
227 memset(buf, 0, samples << hw->info.shift);
228 }
85571bc7
FB
229}
230
1d14ffa9 231static int sdl_write_out (SWVoiceOut *sw, void *buf, int len)
85571bc7 232{
1d14ffa9
FB
233 return audio_pcm_sw_write (sw, buf, len);
234}
235
bdff253c 236static int sdl_run_out (HWVoiceOut *hw, int live)
1d14ffa9 237{
bdff253c 238 int decr;
1d14ffa9 239 SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
1d14ffa9 240
8a7816c4 241 SDL_LockAudio();
1d14ffa9 242
ff541499 243 if (sdl->decr > live) {
244 ldebug ("sdl->decr %d live %d sdl->live %d\n",
245 sdl->decr,
246 live,
247 sdl->live);
248 }
249
250 decr = audio_MIN (sdl->decr, live);
251 sdl->decr -= decr;
252
bcf19777 253 sdl->live = live;
1d14ffa9 254
8a7816c4
TH
255 SDL_UnlockAudio();
256
1d14ffa9
FB
257 return decr;
258}
259
260static void sdl_fini_out (HWVoiceOut *hw)
261{
262 (void) hw;
263
85571bc7
FB
264 sdl_close (&glob_sdl);
265}
266
5706db1d
KZ
267static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,
268 void *drv_opaque)
85571bc7 269{
1d14ffa9 270 SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
85571bc7
FB
271 SDLAudioState *s = &glob_sdl;
272 SDL_AudioSpec req, obt;
4ff9786c 273 int endianness;
1d14ffa9
FB
274 int err;
275 audfmt_e effective_fmt;
1ea879e5 276 struct audsettings obt_as;
85571bc7 277
c0fe3827 278 req.freq = as->freq;
6c557ab9 279 req.format = aud_to_sdlfmt (as->fmt);
c0fe3827 280 req.channels = as->nchannels;
85571bc7 281 req.samples = conf.nb_samples;
85571bc7
FB
282 req.callback = sdl_callback;
283 req.userdata = sdl;
284
1d14ffa9
FB
285 if (sdl_open (&req, &obt)) {
286 return -1;
287 }
288
4ff9786c 289 err = sdl_to_audfmt(obt.format, &effective_fmt, &endianness);
1d14ffa9
FB
290 if (err) {
291 sdl_close (s);
85571bc7 292 return -1;
1d14ffa9 293 }
85571bc7 294
c0fe3827
FB
295 obt_as.freq = obt.freq;
296 obt_as.nchannels = obt.channels;
297 obt_as.fmt = effective_fmt;
4ff9786c 298 obt_as.endianness = endianness;
c0fe3827 299
d929eba5 300 audio_pcm_init_info (&hw->info, &obt_as);
c0fe3827 301 hw->samples = obt.samples;
85571bc7
FB
302
303 s->initialized = 1;
304 s->exit = 0;
305 SDL_PauseAudio (0);
306 return 0;
307}
308
1d14ffa9 309static int sdl_ctl_out (HWVoiceOut *hw, int cmd, ...)
85571bc7
FB
310{
311 (void) hw;
312
313 switch (cmd) {
314 case VOICE_ENABLE:
315 SDL_PauseAudio (0);
316 break;
317
318 case VOICE_DISABLE:
319 SDL_PauseAudio (1);
320 break;
321 }
322 return 0;
323}
324
325static void *sdl_audio_init (void)
326{
327 SDLAudioState *s = &glob_sdl;
81ebb07c
KZ
328 if (s->driver_created) {
329 sdl_logerr("Can't create multiple sdl backends\n");
330 return NULL;
331 }
85571bc7
FB
332
333 if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
1d14ffa9 334 sdl_logerr ("SDL failed to initialize audio subsystem\n");
85571bc7
FB
335 return NULL;
336 }
337
81ebb07c 338 s->driver_created = true;
85571bc7
FB
339 return s;
340}
341
342static void sdl_audio_fini (void *opaque)
343{
344 SDLAudioState *s = opaque;
345 sdl_close (s);
85571bc7 346 SDL_QuitSubSystem (SDL_INIT_AUDIO);
81ebb07c 347 s->driver_created = false;
85571bc7
FB
348}
349
1d14ffa9 350static struct audio_option sdl_options[] = {
98f9f48c 351 {
352 .name = "SAMPLES",
353 .tag = AUD_OPT_INT,
354 .valp = &conf.nb_samples,
355 .descr = "Size of SDL buffer in samples"
356 },
2700efa3 357 { /* End of list */ }
1d14ffa9
FB
358};
359
35f4b58c 360static struct audio_pcm_ops sdl_pcm_ops = {
1dd3e4d1
JQ
361 .init_out = sdl_init_out,
362 .fini_out = sdl_fini_out,
363 .run_out = sdl_run_out,
364 .write = sdl_write_out,
365 .ctl_out = sdl_ctl_out,
85571bc7
FB
366};
367
d3893a39 368static struct audio_driver sdl_audio_driver = {
bee37f32
JQ
369 .name = "sdl",
370 .descr = "SDL http://www.libsdl.org",
371 .options = sdl_options,
372 .init = sdl_audio_init,
373 .fini = sdl_audio_fini,
374 .pcm_ops = &sdl_pcm_ops,
375 .can_be_default = 1,
376 .max_voices_out = 1,
377 .max_voices_in = 0,
378 .voice_size_out = sizeof (SDLVoiceOut),
379 .voice_size_in = 0
85571bc7 380};
d3893a39
GH
381
382static void register_audio_sdl(void)
383{
384 audio_driver_register(&sdl_audio_driver);
385}
386type_init(register_audio_sdl);