blob: b74dcfa73470c9ad28097303a4b34344319d259e [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 */
bellard9f059ec2004-11-14 18:59:52 +000024#include <SDL.h>
25#include <SDL_thread.h>
pbrook87ecb682007-11-17 17:14:51 +000026#include "qemu-common.h"
27#include "audio.h"
bellard85571bc2004-11-07 18:04:02 +000028
thse784ba72007-07-11 23:23:15 +000029#ifndef _WIN32
30#ifdef __sun__
31#define _POSIX_PTHREAD_SEMANTICS 1
blueswir1c5e97232009-03-07 20:06:23 +000032#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
blueswir19b4c14c2008-10-25 11:19:14 +000033#include <pthread.h>
thse784ba72007-07-11 23:23:15 +000034#endif
35#include <signal.h>
36#endif
37
bellard1d14ffa2005-10-30 18:58:22 +000038#define AUDIO_CAP "sdl"
39#include "audio_int.h"
bellardfb065182004-11-09 23:09:44 +000040
bellard1d14ffa2005-10-30 18:58:22 +000041typedef struct SDLVoiceOut {
42 HWVoiceOut hw;
43 int live;
malcff541492010-01-17 00:25:29 +030044 int rpos;
bellard1d14ffa2005-10-30 18:58:22 +000045 int decr;
46} SDLVoiceOut;
bellard85571bc2004-11-07 18:04:02 +000047
48static struct {
49 int nb_samples;
50} conf = {
Juan Quintela1a40d5e2009-08-11 02:31:17 +020051 .nb_samples = 1024
bellard85571bc2004-11-07 18:04:02 +000052};
53
blueswir1b1d8e522008-10-26 13:43:07 +000054static struct SDLAudioState {
bellard85571bc2004-11-07 18:04:02 +000055 int exit;
56 SDL_mutex *mutex;
57 SDL_sem *sem;
58 int initialized;
59} glob_sdl;
60typedef struct SDLAudioState SDLAudioState;
61
bellard1d14ffa2005-10-30 18:58:22 +000062static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...)
bellard85571bc2004-11-07 18:04:02 +000063{
bellard1d14ffa2005-10-30 18:58:22 +000064 va_list ap;
65
66 va_start (ap, fmt);
67 AUD_vlog (AUDIO_CAP, fmt, ap);
68 va_end (ap);
69
70 AUD_log (AUDIO_CAP, "Reason: %s\n", SDL_GetError ());
bellard85571bc2004-11-07 18:04:02 +000071}
72
bellard1d14ffa2005-10-30 18:58:22 +000073static int sdl_lock (SDLAudioState *s, const char *forfn)
bellard85571bc2004-11-07 18:04:02 +000074{
75 if (SDL_LockMutex (s->mutex)) {
bellard1d14ffa2005-10-30 18:58:22 +000076 sdl_logerr ("SDL_LockMutex for %s failed\n", forfn);
bellard85571bc2004-11-07 18:04:02 +000077 return -1;
78 }
79 return 0;
80}
81
bellard1d14ffa2005-10-30 18:58:22 +000082static int sdl_unlock (SDLAudioState *s, const char *forfn)
bellard85571bc2004-11-07 18:04:02 +000083{
84 if (SDL_UnlockMutex (s->mutex)) {
bellard1d14ffa2005-10-30 18:58:22 +000085 sdl_logerr ("SDL_UnlockMutex for %s failed\n", forfn);
bellard85571bc2004-11-07 18:04:02 +000086 return -1;
87 }
88 return 0;
89}
90
bellard1d14ffa2005-10-30 18:58:22 +000091static int sdl_post (SDLAudioState *s, const char *forfn)
bellard85571bc2004-11-07 18:04:02 +000092{
93 if (SDL_SemPost (s->sem)) {
bellard1d14ffa2005-10-30 18:58:22 +000094 sdl_logerr ("SDL_SemPost for %s failed\n", forfn);
bellard85571bc2004-11-07 18:04:02 +000095 return -1;
96 }
97 return 0;
98}
99
bellard1d14ffa2005-10-30 18:58:22 +0000100static int sdl_wait (SDLAudioState *s, const char *forfn)
bellard85571bc2004-11-07 18:04:02 +0000101{
102 if (SDL_SemWait (s->sem)) {
bellard1d14ffa2005-10-30 18:58:22 +0000103 sdl_logerr ("SDL_SemWait for %s failed\n", forfn);
bellard85571bc2004-11-07 18:04:02 +0000104 return -1;
105 }
106 return 0;
107}
108
bellard1d14ffa2005-10-30 18:58:22 +0000109static int sdl_unlock_and_post (SDLAudioState *s, const char *forfn)
bellard85571bc2004-11-07 18:04:02 +0000110{
bellard1d14ffa2005-10-30 18:58:22 +0000111 if (sdl_unlock (s, forfn)) {
bellard85571bc2004-11-07 18:04:02 +0000112 return -1;
bellard1d14ffa2005-10-30 18:58:22 +0000113 }
bellard85571bc2004-11-07 18:04:02 +0000114
bellard1d14ffa2005-10-30 18:58:22 +0000115 return sdl_post (s, forfn);
bellard85571bc2004-11-07 18:04:02 +0000116}
117
Serge Ziryukin6c557ab2010-04-22 14:14:24 +0300118static int aud_to_sdlfmt (audfmt_e fmt)
bellard85571bc2004-11-07 18:04:02 +0000119{
bellard85571bc2004-11-07 18:04:02 +0000120 switch (fmt) {
bellard1d14ffa2005-10-30 18:58:22 +0000121 case AUD_FMT_S8:
bellard1d14ffa2005-10-30 18:58:22 +0000122 return AUDIO_S8;
123
124 case AUD_FMT_U8:
bellard1d14ffa2005-10-30 18:58:22 +0000125 return AUDIO_U8;
126
127 case AUD_FMT_S16:
bellard1d14ffa2005-10-30 18:58:22 +0000128 return AUDIO_S16LSB;
129
130 case AUD_FMT_U16:
bellard1d14ffa2005-10-30 18:58:22 +0000131 return AUDIO_U16LSB;
132
bellard85571bc2004-11-07 18:04:02 +0000133 default:
bellard1d14ffa2005-10-30 18:58:22 +0000134 dolog ("Internal logic error: Bad audio format %d\n", fmt);
135#ifdef DEBUG_AUDIO
136 abort ();
137#endif
138 return AUDIO_U8;
bellard85571bc2004-11-07 18:04:02 +0000139 }
140}
141
bellard1d14ffa2005-10-30 18:58:22 +0000142static int sdl_to_audfmt (int sdlfmt, audfmt_e *fmt, int *endianess)
bellard85571bc2004-11-07 18:04:02 +0000143{
bellard1d14ffa2005-10-30 18:58:22 +0000144 switch (sdlfmt) {
145 case AUDIO_S8:
146 *endianess = 0;
147 *fmt = AUD_FMT_S8;
148 break;
149
150 case AUDIO_U8:
151 *endianess = 0;
152 *fmt = AUD_FMT_U8;
153 break;
154
155 case AUDIO_S16LSB:
156 *endianess = 0;
157 *fmt = AUD_FMT_S16;
158 break;
159
160 case AUDIO_U16LSB:
161 *endianess = 0;
162 *fmt = AUD_FMT_U16;
163 break;
164
165 case AUDIO_S16MSB:
166 *endianess = 1;
167 *fmt = AUD_FMT_S16;
168 break;
169
170 case AUDIO_U16MSB:
171 *endianess = 1;
172 *fmt = AUD_FMT_U16;
173 break;
174
bellard85571bc2004-11-07 18:04:02 +0000175 default:
bellard1d14ffa2005-10-30 18:58:22 +0000176 dolog ("Unrecognized SDL audio format %d\n", sdlfmt);
177 return -1;
bellard85571bc2004-11-07 18:04:02 +0000178 }
bellard1d14ffa2005-10-30 18:58:22 +0000179
180 return 0;
bellard85571bc2004-11-07 18:04:02 +0000181}
182
183static int sdl_open (SDL_AudioSpec *req, SDL_AudioSpec *obt)
184{
185 int status;
thse784ba72007-07-11 23:23:15 +0000186#ifndef _WIN32
malcd087bb32010-08-06 13:09:41 +0400187 int err;
thse784ba72007-07-11 23:23:15 +0000188 sigset_t new, old;
189
190 /* Make sure potential threads created by SDL don't hog signals. */
malcd087bb32010-08-06 13:09:41 +0400191 err = sigfillset (&new);
192 if (err) {
193 dolog ("sdl_open: sigfillset failed: %s\n", strerror (errno));
malc60592ed2010-08-07 20:03:05 +0400194 return -1;
malcd087bb32010-08-06 13:09:41 +0400195 }
196 err = pthread_sigmask (SIG_BLOCK, &new, &old);
197 if (err) {
198 dolog ("sdl_open: pthread_sigmask failed: %s\n", strerror (err));
199 return -1;
200 }
thse784ba72007-07-11 23:23:15 +0000201#endif
bellard85571bc2004-11-07 18:04:02 +0000202
203 status = SDL_OpenAudio (req, obt);
204 if (status) {
bellard1d14ffa2005-10-30 18:58:22 +0000205 sdl_logerr ("SDL_OpenAudio failed\n");
bellard85571bc2004-11-07 18:04:02 +0000206 }
thse784ba72007-07-11 23:23:15 +0000207
208#ifndef _WIN32
malcd087bb32010-08-06 13:09:41 +0400209 err = pthread_sigmask (SIG_SETMASK, &old, NULL);
210 if (err) {
211 dolog ("sdl_open: pthread_sigmask (restore) failed: %s\n",
212 strerror (errno));
213 /* We have failed to restore original signal mask, all bets are off,
214 so exit the process */
215 exit (EXIT_FAILURE);
216 }
thse784ba72007-07-11 23:23:15 +0000217#endif
bellard85571bc2004-11-07 18:04:02 +0000218 return status;
219}
220
221static void sdl_close (SDLAudioState *s)
222{
223 if (s->initialized) {
bellard1d14ffa2005-10-30 18:58:22 +0000224 sdl_lock (s, "sdl_close");
bellard85571bc2004-11-07 18:04:02 +0000225 s->exit = 1;
bellard1d14ffa2005-10-30 18:58:22 +0000226 sdl_unlock_and_post (s, "sdl_close");
bellard85571bc2004-11-07 18:04:02 +0000227 SDL_PauseAudio (1);
228 SDL_CloseAudio ();
229 s->initialized = 0;
230 }
231}
232
233static void sdl_callback (void *opaque, Uint8 *buf, int len)
234{
bellard1d14ffa2005-10-30 18:58:22 +0000235 SDLVoiceOut *sdl = opaque;
bellard85571bc2004-11-07 18:04:02 +0000236 SDLAudioState *s = &glob_sdl;
bellard1d14ffa2005-10-30 18:58:22 +0000237 HWVoiceOut *hw = &sdl->hw;
238 int samples = len >> hw->info.shift;
bellard85571bc2004-11-07 18:04:02 +0000239
240 if (s->exit) {
241 return;
242 }
243
244 while (samples) {
bellard1d14ffa2005-10-30 18:58:22 +0000245 int to_mix, decr;
bellard85571bc2004-11-07 18:04:02 +0000246
malcff541492010-01-17 00:25:29 +0300247 /* dolog ("in callback samples=%d\n", samples); */
248 sdl_wait (s, "sdl_callback");
249 if (s->exit) {
250 return;
bellard85571bc2004-11-07 18:04:02 +0000251 }
252
malcff541492010-01-17 00:25:29 +0300253 if (sdl_lock (s, "sdl_callback")) {
254 return;
255 }
malc4839abe2009-09-30 16:25:55 +0400256
malcff541492010-01-17 00:25:29 +0300257 if (audio_bug (AUDIO_FUNC, sdl->live < 0 || sdl->live > hw->samples)) {
258 dolog ("sdl->live=%d hw->samples=%d\n",
259 sdl->live, hw->samples);
260 return;
261 }
262
263 if (!sdl->live) {
264 goto again;
265 }
266
267 /* dolog ("in callback live=%d\n", live); */
268 to_mix = audio_MIN (samples, sdl->live);
269 decr = to_mix;
270 while (to_mix) {
271 int chunk = audio_MIN (to_mix, hw->samples - hw->rpos);
272 struct st_sample *src = hw->mix_buf + hw->rpos;
273
274 /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
275 hw->clip (buf, src, chunk);
276 sdl->rpos = (sdl->rpos + chunk) % hw->samples;
277 to_mix -= chunk;
278 buf += chunk << hw->info.shift;
279 }
280 samples -= decr;
281 sdl->live -= decr;
282 sdl->decr += decr;
283
284 again:
285 if (sdl_unlock (s, "sdl_callback")) {
286 return;
287 }
malc4839abe2009-09-30 16:25:55 +0400288 }
malcff541492010-01-17 00:25:29 +0300289 /* dolog ("done len=%d\n", len); */
bellard85571bc2004-11-07 18:04:02 +0000290}
291
bellard1d14ffa2005-10-30 18:58:22 +0000292static int sdl_write_out (SWVoiceOut *sw, void *buf, int len)
bellard85571bc2004-11-07 18:04:02 +0000293{
bellard1d14ffa2005-10-30 18:58:22 +0000294 return audio_pcm_sw_write (sw, buf, len);
295}
296
malcbdff2532009-09-18 11:37:39 +0400297static int sdl_run_out (HWVoiceOut *hw, int live)
bellard1d14ffa2005-10-30 18:58:22 +0000298{
malcbdff2532009-09-18 11:37:39 +0400299 int decr;
bellard1d14ffa2005-10-30 18:58:22 +0000300 SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
301 SDLAudioState *s = &glob_sdl;
302
malc3fd7f632009-09-18 11:19:00 +0400303 if (sdl_lock (s, "sdl_run_out")) {
bellard1d14ffa2005-10-30 18:58:22 +0000304 return 0;
305 }
306
malcff541492010-01-17 00:25:29 +0300307 if (sdl->decr > live) {
308 ldebug ("sdl->decr %d live %d sdl->live %d\n",
309 sdl->decr,
310 live,
311 sdl->live);
312 }
313
314 decr = audio_MIN (sdl->decr, live);
315 sdl->decr -= decr;
316
317 sdl->live = live - decr;
318 hw->rpos = sdl->rpos;
bellard1d14ffa2005-10-30 18:58:22 +0000319
320 if (sdl->live > 0) {
malc3fd7f632009-09-18 11:19:00 +0400321 sdl_unlock_and_post (s, "sdl_run_out");
bellard1d14ffa2005-10-30 18:58:22 +0000322 }
323 else {
malc3fd7f632009-09-18 11:19:00 +0400324 sdl_unlock (s, "sdl_run_out");
bellard1d14ffa2005-10-30 18:58:22 +0000325 }
326 return decr;
327}
328
329static void sdl_fini_out (HWVoiceOut *hw)
330{
331 (void) hw;
332
bellard85571bc2004-11-07 18:04:02 +0000333 sdl_close (&glob_sdl);
334}
335
malc1ea879e2008-12-03 22:48:44 +0000336static int sdl_init_out (HWVoiceOut *hw, struct audsettings *as)
bellard85571bc2004-11-07 18:04:02 +0000337{
bellard1d14ffa2005-10-30 18:58:22 +0000338 SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
bellard85571bc2004-11-07 18:04:02 +0000339 SDLAudioState *s = &glob_sdl;
340 SDL_AudioSpec req, obt;
bellard1d14ffa2005-10-30 18:58:22 +0000341 int endianess;
342 int err;
343 audfmt_e effective_fmt;
malc1ea879e2008-12-03 22:48:44 +0000344 struct audsettings obt_as;
bellard85571bc2004-11-07 18:04:02 +0000345
bellardc0fe3822005-11-05 18:55:28 +0000346 req.freq = as->freq;
Serge Ziryukin6c557ab2010-04-22 14:14:24 +0300347 req.format = aud_to_sdlfmt (as->fmt);
bellardc0fe3822005-11-05 18:55:28 +0000348 req.channels = as->nchannels;
bellard85571bc2004-11-07 18:04:02 +0000349 req.samples = conf.nb_samples;
bellard85571bc2004-11-07 18:04:02 +0000350 req.callback = sdl_callback;
351 req.userdata = sdl;
352
bellard1d14ffa2005-10-30 18:58:22 +0000353 if (sdl_open (&req, &obt)) {
bellard85571bc2004-11-07 18:04:02 +0000354 return -1;
bellard1d14ffa2005-10-30 18:58:22 +0000355 }
bellard85571bc2004-11-07 18:04:02 +0000356
bellard1d14ffa2005-10-30 18:58:22 +0000357 err = sdl_to_audfmt (obt.format, &effective_fmt, &endianess);
358 if (err) {
359 sdl_close (s);
360 return -1;
361 }
362
bellardc0fe3822005-11-05 18:55:28 +0000363 obt_as.freq = obt.freq;
364 obt_as.nchannels = obt.channels;
365 obt_as.fmt = effective_fmt;
bellardd929eba2006-07-04 21:47:22 +0000366 obt_as.endianness = endianess;
bellardc0fe3822005-11-05 18:55:28 +0000367
bellardd929eba2006-07-04 21:47:22 +0000368 audio_pcm_init_info (&hw->info, &obt_as);
bellardc0fe3822005-11-05 18:55:28 +0000369 hw->samples = obt.samples;
bellard85571bc2004-11-07 18:04:02 +0000370
371 s->initialized = 1;
372 s->exit = 0;
373 SDL_PauseAudio (0);
374 return 0;
375}
376
bellard1d14ffa2005-10-30 18:58:22 +0000377static int sdl_ctl_out (HWVoiceOut *hw, int cmd, ...)
bellard85571bc2004-11-07 18:04:02 +0000378{
379 (void) hw;
380
381 switch (cmd) {
382 case VOICE_ENABLE:
383 SDL_PauseAudio (0);
384 break;
385
386 case VOICE_DISABLE:
387 SDL_PauseAudio (1);
388 break;
389 }
390 return 0;
391}
392
393static void *sdl_audio_init (void)
394{
395 SDLAudioState *s = &glob_sdl;
bellard85571bc2004-11-07 18:04:02 +0000396
397 if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
bellard1d14ffa2005-10-30 18:58:22 +0000398 sdl_logerr ("SDL failed to initialize audio subsystem\n");
bellard85571bc2004-11-07 18:04:02 +0000399 return NULL;
400 }
401
402 s->mutex = SDL_CreateMutex ();
403 if (!s->mutex) {
bellard1d14ffa2005-10-30 18:58:22 +0000404 sdl_logerr ("Failed to create SDL mutex\n");
bellard85571bc2004-11-07 18:04:02 +0000405 SDL_QuitSubSystem (SDL_INIT_AUDIO);
406 return NULL;
407 }
408
409 s->sem = SDL_CreateSemaphore (0);
410 if (!s->sem) {
bellard1d14ffa2005-10-30 18:58:22 +0000411 sdl_logerr ("Failed to create SDL semaphore\n");
bellard85571bc2004-11-07 18:04:02 +0000412 SDL_DestroyMutex (s->mutex);
413 SDL_QuitSubSystem (SDL_INIT_AUDIO);
414 return NULL;
415 }
416
417 return s;
418}
419
420static void sdl_audio_fini (void *opaque)
421{
422 SDLAudioState *s = opaque;
423 sdl_close (s);
424 SDL_DestroySemaphore (s->sem);
425 SDL_DestroyMutex (s->mutex);
426 SDL_QuitSubSystem (SDL_INIT_AUDIO);
427}
428
bellard1d14ffa2005-10-30 18:58:22 +0000429static struct audio_option sdl_options[] = {
malc98f9f482009-08-11 20:48:02 +0400430 {
431 .name = "SAMPLES",
432 .tag = AUD_OPT_INT,
433 .valp = &conf.nb_samples,
434 .descr = "Size of SDL buffer in samples"
435 },
Juan Quintela2700efa2009-08-11 02:31:16 +0200436 { /* End of list */ }
bellard85571bc2004-11-07 18:04:02 +0000437};
438
blueswir135f4b582008-10-06 18:08:30 +0000439static struct audio_pcm_ops sdl_pcm_ops = {
Juan Quintela1dd3e4d2009-08-11 02:31:15 +0200440 .init_out = sdl_init_out,
441 .fini_out = sdl_fini_out,
442 .run_out = sdl_run_out,
443 .write = sdl_write_out,
444 .ctl_out = sdl_ctl_out,
bellard1d14ffa2005-10-30 18:58:22 +0000445};
446
447struct audio_driver sdl_audio_driver = {
Juan Quintelabee37f32009-08-11 02:31:14 +0200448 .name = "sdl",
449 .descr = "SDL http://www.libsdl.org",
450 .options = sdl_options,
451 .init = sdl_audio_init,
452 .fini = sdl_audio_fini,
453 .pcm_ops = &sdl_pcm_ops,
454 .can_be_default = 1,
455 .max_voices_out = 1,
456 .max_voices_in = 0,
457 .voice_size_out = sizeof (SDLVoiceOut),
458 .voice_size_in = 0
bellard85571bc2004-11-07 18:04:02 +0000459};