sdlaudio.c 14 KB

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