src/audio/SDL_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 09 Aug 2016 00:44:05 -0400
changeset 10252 4d99b888e7e5
parent 10246 db0b712b79dd
child 10253 4a1faf81a620
permissions -rw-r--r--
audio: simplifed check for internal callback. Easier to check when it's NULL instead of a list of known internal functions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5526
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7981
diff changeset
    21
#include "../SDL_internal.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
/* Allow access to a raw mixing buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#include "SDL.h"
2984
0b160c970b7e Fixed some dependency issues with SDL_revision.h
Sam Lantinga <slouken@libsdl.org>
parents: 2979
diff changeset
    26
#include "SDL_audio.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include "SDL_audio_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#include "SDL_sysaudio.h"
10146
471eb08040ce threads: Move SDL's own thread creation to a new internal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    29
#include "../thread/SDL_systhread.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
4865
fff50e86c891 Update VS2010 project to add new files; update new files so code builds on Win32/Win64
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 4729
diff changeset
    31
#define _THIS SDL_AudioDevice *_this
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    32
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    33
static SDL_AudioDriver current_audio;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    34
static SDL_AudioDevice *open_devices[16];
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    35
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    36
/*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    37
 * Not all of these will be compiled and linked in, but it's convenient
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    38
 *  to have a complete list here and saves yet-another block of #ifdefs...
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    39
 *  Please see bootstrap[], below, for the actual #ifdef mess.
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    40
 */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    41
extern AudioBootStrap BSD_AUDIO_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    42
extern AudioBootStrap DSP_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    43
extern AudioBootStrap ALSA_bootstrap;
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2147
diff changeset
    44
extern AudioBootStrap PULSEAUDIO_bootstrap;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    45
extern AudioBootStrap QSAAUDIO_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    46
extern AudioBootStrap SUNAUDIO_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    47
extern AudioBootStrap ARTS_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    48
extern AudioBootStrap ESD_bootstrap;
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
    49
extern AudioBootStrap NACLAUD_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    50
extern AudioBootStrap NAS_bootstrap;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    51
extern AudioBootStrap XAUDIO2_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    52
extern AudioBootStrap DSOUND_bootstrap;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
    53
extern AudioBootStrap WINMM_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    54
extern AudioBootStrap PAUDIO_bootstrap;
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7904
diff changeset
    55
extern AudioBootStrap HAIKUAUDIO_bootstrap;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    56
extern AudioBootStrap COREAUDIO_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    57
extern AudioBootStrap SNDMGR_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    58
extern AudioBootStrap DISKAUD_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    59
extern AudioBootStrap DUMMYAUD_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    60
extern AudioBootStrap DCAUD_bootstrap;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    61
extern AudioBootStrap DART_bootstrap;
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
    62
extern AudioBootStrap NDSAUD_bootstrap;
2947
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
    63
extern AudioBootStrap FUSIONSOUND_bootstrap;
4718
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
    64
extern AudioBootStrap ANDROIDAUD_bootstrap;
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
    65
extern AudioBootStrap PSPAUD_bootstrap;
7367
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    66
extern AudioBootStrap SNDIO_bootstrap;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
    67
extern AudioBootStrap EmscriptenAudio_bootstrap;
1190
173c063d4f55 OS/2 port!
Ryan C. Gordon <icculus@icculus.org>
parents: 955
diff changeset
    68
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
    69
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
/* Available audio drivers */
3162
dc1eb82ffdaa Von: Thomas Zimmermann
Sam Lantinga <slouken@libsdl.org>
parents: 3139
diff changeset
    71
static const AudioBootStrap *const bootstrap[] = {
2939
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    72
#if SDL_AUDIO_DRIVER_PULSEAUDIO
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    73
    &PULSEAUDIO_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    74
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    75
#if SDL_AUDIO_DRIVER_ALSA
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
    76
    &ALSA_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    77
#endif
7367
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    78
#if SDL_AUDIO_DRIVER_SNDIO
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    79
    &SNDIO_bootstrap,
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    80
#endif
4548
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    81
#if SDL_AUDIO_DRIVER_BSD
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    82
    &BSD_AUDIO_bootstrap,
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    83
#endif
2939
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    84
#if SDL_AUDIO_DRIVER_OSS
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    85
    &DSP_bootstrap,
2271
60b4c52a7906 Ported PulseAudio target from 1.2 to 1.3 interfaces, and added it to the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2147
diff changeset
    86
#endif
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    87
#if SDL_AUDIO_DRIVER_QSA
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    88
    &QSAAUDIO_bootstrap,
663
8bedd6d61642 Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents: 654
diff changeset
    89
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    90
#if SDL_AUDIO_DRIVER_SUNAUDIO
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
    91
    &SUNAUDIO_bootstrap,
148
8758b8d42cd9 Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents: 121
diff changeset
    92
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    93
#if SDL_AUDIO_DRIVER_ARTS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
    94
    &ARTS_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    95
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    96
#if SDL_AUDIO_DRIVER_ESD
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
    97
    &ESD_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    98
#endif
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
    99
#if SDL_AUDIO_DRIVER_NACL
10215
3fbf4a6c6101 audio: Make SDL_AudioDevice::shutdown an atomic value.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   100
    &NACLAUD_bootstrap,
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
   101
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   102
#if SDL_AUDIO_DRIVER_NAS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   103
    &NAS_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   104
#endif
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   105
#if SDL_AUDIO_DRIVER_XAUDIO2
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   106
    &XAUDIO2_bootstrap,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   107
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   108
#if SDL_AUDIO_DRIVER_DSOUND
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   109
    &DSOUND_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   110
#endif
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
   111
#if SDL_AUDIO_DRIVER_WINMM
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
   112
    &WINMM_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   113
#endif
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   114
#if SDL_AUDIO_DRIVER_PAUDIO
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   115
    &PAUDIO_bootstrap,
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   116
#endif
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7904
diff changeset
   117
#if SDL_AUDIO_DRIVER_HAIKU
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7904
diff changeset
   118
    &HAIKUAUDIO_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   119
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   120
#if SDL_AUDIO_DRIVER_COREAUDIO
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   121
    &COREAUDIO_bootstrap,
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents: 808
diff changeset
   122
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   123
#if SDL_AUDIO_DRIVER_DISK
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   124
    &DISKAUD_bootstrap,
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents: 38
diff changeset
   125
#endif
1532
30f189cdd82b Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1487
diff changeset
   126
#if SDL_AUDIO_DRIVER_DUMMY
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   127
    &DUMMYAUD_bootstrap,
1532
30f189cdd82b Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1487
diff changeset
   128
#endif
2947
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   129
#if SDL_AUDIO_DRIVER_FUSIONSOUND
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   130
    &FUSIONSOUND_bootstrap,
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   131
#endif
4718
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   132
#if SDL_AUDIO_DRIVER_ANDROID
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   133
    &ANDROIDAUD_bootstrap,
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   134
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   135
#if SDL_AUDIO_DRIVER_PSP
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   136
    &PSPAUD_bootstrap,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   137
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   138
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   139
    &EmscriptenAudio_bootstrap,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   140
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   141
    NULL
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
};
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   143
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   144
static SDL_AudioDevice *
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   145
get_audio_device(SDL_AudioDeviceID id)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   146
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   147
    id--;
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   148
    if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   149
        SDL_SetError("Invalid audio device ID");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   150
        return NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   151
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   152
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   153
    return open_devices[id];
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   154
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   155
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   156
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   157
/* stubs for audio drivers that don't need a specific entry point... */
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   158
static void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   159
SDL_AudioDetectDevices_Default(void)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   160
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   161
    /* you have to write your own implementation if these assertions fail. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   162
    SDL_assert(current_audio.impl.OnlyHasDefaultOutputDevice);
10240
0ba7106e9a6d audio: Changed OnlyHasDefaultInputDevice to OnlyHasDefaultCaptureDevice.
Ryan C. Gordon <icculus@icculus.org>
parents: 10238
diff changeset
   163
    SDL_assert(current_audio.impl.OnlyHasDefaultCaptureDevice || !current_audio.impl.HasCaptureSupport);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   164
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   165
    SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, (void *) ((size_t) 0x1));
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   166
    if (current_audio.impl.HasCaptureSupport) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   167
        SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, (void *) ((size_t) 0x2));
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   168
    }
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   169
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   170
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   171
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   172
SDL_AudioThreadInit_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   173
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   174
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   175
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   176
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   177
SDL_AudioWaitDevice_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   178
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   179
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   180
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   181
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   182
SDL_AudioPlayDevice_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   183
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   184
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   185
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   186
static int
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   187
SDL_AudioGetPendingBytes_Default(_THIS)
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   188
{
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   189
    return 0;
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   190
}
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   191
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   192
static Uint8 *
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   193
SDL_AudioGetDeviceBuf_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   194
{
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   195
    return NULL;
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   196
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   197
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   198
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   199
SDL_AudioWaitDone_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   200
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   201
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   202
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   203
static int
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   204
SDL_AudioCaptureFromDevice_Default(_THIS, void *buffer, int buflen)
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   205
{
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   206
    return -1;  /* just fail immediately. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   207
}
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   208
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   209
static void
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   210
SDL_AudioFlushCapture_Default(_THIS)
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   211
{                               /* no-op. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   212
}
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   213
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   214
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   215
SDL_AudioCloseDevice_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   216
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   217
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   218
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   219
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   220
SDL_AudioDeinitialize_Default(void)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   221
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   222
}
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   223
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   224
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   225
SDL_AudioFreeDeviceHandle_Default(void *handle)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   226
{                               /* no-op. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   227
}
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   228
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   229
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   230
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   231
SDL_AudioOpenDevice_Default(_THIS, void *handle, const char *devname, int iscapture)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   232
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   233
    return SDL_Unsupported();
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   234
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   235
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   236
static SDL_INLINE SDL_bool
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   237
is_in_audio_device_thread(SDL_AudioDevice * device)
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   238
{
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   239
    /* The device thread locks the same mutex, but not through the public API.
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   240
       This check is in case the application, in the audio callback,
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   241
       tries to lock the thread that we've already locked from the
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   242
       device thread...just in case we only have non-recursive mutexes. */
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   243
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   244
        return SDL_TRUE;
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   245
    }
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   246
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   247
    return SDL_FALSE;
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   248
}
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   249
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   250
static void
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   251
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   252
{
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   253
    if (!is_in_audio_device_thread(device)) {
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   254
        SDL_LockMutex(device->mixer_lock);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   255
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   256
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   257
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   258
static void
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   259
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   260
{
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   261
    if (!is_in_audio_device_thread(device)) {
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   262
        SDL_UnlockMutex(device->mixer_lock);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   263
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   264
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   265
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   266
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   267
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   268
finalize_audio_entry_points(void)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   269
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   270
    /*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   271
     * Fill in stub functions for unused driver entry points. This lets us
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   272
     *  blindly call them without having to check for validity first.
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   273
     */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   274
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   275
#define FILL_STUB(x) \
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   276
        if (current_audio.impl.x == NULL) { \
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   277
            current_audio.impl.x = SDL_Audio##x##_Default; \
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   278
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   279
    FILL_STUB(DetectDevices);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   280
    FILL_STUB(OpenDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   281
    FILL_STUB(ThreadInit);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   282
    FILL_STUB(WaitDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   283
    FILL_STUB(PlayDevice);
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   284
    FILL_STUB(GetPendingBytes);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   285
    FILL_STUB(GetDeviceBuf);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   286
    FILL_STUB(WaitDone);
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   287
    FILL_STUB(CaptureFromDevice);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   288
    FILL_STUB(FlushCapture);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   289
    FILL_STUB(CloseDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   290
    FILL_STUB(LockDevice);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   291
    FILL_STUB(UnlockDevice);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   292
    FILL_STUB(FreeDeviceHandle);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   293
    FILL_STUB(Deinitialize);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   294
#undef FILL_STUB
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   295
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   296
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   297
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   298
/* device hotplug support... */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   299
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   300
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   301
add_audio_device(const char *name, void *handle, SDL_AudioDeviceItem **devices, int *devCount)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   302
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   303
    int retval = -1;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   304
    const size_t size = sizeof (SDL_AudioDeviceItem) + SDL_strlen(name) + 1;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   305
    SDL_AudioDeviceItem *item = (SDL_AudioDeviceItem *) SDL_malloc(size);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   306
    if (item == NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   307
        return -1;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   308
    }
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   309
9410
e25530faeda0 Audio hotplug fixes for winmm and XAudio2 backends.
Ryan C. Gordon <icculus@icculus.org>
parents: 9405
diff changeset
   310
    SDL_assert(handle != NULL);  /* we reserve NULL, audio backends can't use it. */
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   311
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   312
    item->handle = handle;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   313
    SDL_strlcpy(item->name, name, size - sizeof (SDL_AudioDeviceItem));
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   314
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   315
    SDL_LockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   316
    item->next = *devices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   317
    *devices = item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   318
    retval = (*devCount)++;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   319
    SDL_UnlockMutex(current_audio.detectionLock);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   320
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   321
    return retval;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   322
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   323
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   324
static SDL_INLINE int
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   325
add_capture_device(const char *name, void *handle)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   326
{
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10146
diff changeset
   327
    SDL_assert(current_audio.impl.HasCaptureSupport);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   328
    return add_audio_device(name, handle, &current_audio.inputDevices, &current_audio.inputDeviceCount);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   329
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   330
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   331
static SDL_INLINE int
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   332
add_output_device(const char *name, void *handle)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   333
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   334
    return add_audio_device(name, handle, &current_audio.outputDevices, &current_audio.outputDeviceCount);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   335
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   336
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   337
static void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   338
free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   339
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   340
    SDL_AudioDeviceItem *item, *next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   341
    for (item = *devices; item != NULL; item = next) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   342
        next = item->next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   343
        if (item->handle != NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   344
            current_audio.impl.FreeDeviceHandle(item->handle);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   345
        }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   346
        SDL_free(item);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   347
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   348
    *devices = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   349
    *devCount = 0;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   350
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   351
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   352
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   353
/* The audio backends call this when a new device is plugged in. */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   354
void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   355
SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   356
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   357
    const int device_index = iscapture ? add_capture_device(name, handle) : add_output_device(name, handle);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   358
    if (device_index != -1) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   359
        /* Post the event, if desired */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   360
        if (SDL_GetEventState(SDL_AUDIODEVICEADDED) == SDL_ENABLE) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   361
            SDL_Event event;
9401
529007547fb6 Zero out the audio hotplug event structure, so the "padded" fields are sane.
Ryan C. Gordon <icculus@icculus.org>
parents: 9399
diff changeset
   362
            SDL_zero(event);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   363
            event.adevice.type = SDL_AUDIODEVICEADDED;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   364
            event.adevice.which = device_index;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   365
            event.adevice.iscapture = iscapture;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   366
            SDL_PushEvent(&event);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   367
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   368
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   369
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   370
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   371
/* The audio backends call this when a currently-opened device is lost. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   372
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   373
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   374
    SDL_assert(get_audio_device(device->id) == device);
9396
69c501ed36f3 Don't assert if an audio backend reports the same device disconnected twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   375
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   376
    if (!SDL_AtomicGet(&device->enabled)) {
9396
69c501ed36f3 Don't assert if an audio backend reports the same device disconnected twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   377
        return;
69c501ed36f3 Don't assert if an audio backend reports the same device disconnected twice.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   378
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   379
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   380
    /* Ends the audio callback and mark the device as STOPPED, but the
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   381
       app still needs to close the device to free resources. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   382
    current_audio.impl.LockDevice(device);
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   383
    SDL_AtomicSet(&device->enabled, 0);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   384
    current_audio.impl.UnlockDevice(device);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   385
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   386
    /* Post the event, if desired */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   387
    if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   388
        SDL_Event event;
9401
529007547fb6 Zero out the audio hotplug event structure, so the "padded" fields are sane.
Ryan C. Gordon <icculus@icculus.org>
parents: 9399
diff changeset
   389
        SDL_zero(event);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   390
        event.adevice.type = SDL_AUDIODEVICEREMOVED;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   391
        event.adevice.which = device->id;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   392
        event.adevice.iscapture = device->iscapture ? 1 : 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   393
        SDL_PushEvent(&event);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   394
    }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   395
}
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   396
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   397
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   398
mark_device_removed(void *handle, SDL_AudioDeviceItem *devices, SDL_bool *removedFlag)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   399
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   400
    SDL_AudioDeviceItem *item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   401
    SDL_assert(handle != NULL);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   402
    for (item = devices; item != NULL; item = item->next) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   403
        if (item->handle == handle) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   404
            item->handle = NULL;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   405
            *removedFlag = SDL_TRUE;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   406
            return;
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   407
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   408
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   409
}
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   410
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   411
/* The audio backends call this when a device is removed from the system. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   412
void
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   413
SDL_RemoveAudioDevice(const int iscapture, void *handle)
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   414
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   415
    SDL_LockMutex(current_audio.detectionLock);
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   416
    if (iscapture) {
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   417
        mark_device_removed(handle, current_audio.inputDevices, &current_audio.captureDevicesRemoved);
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   418
    } else {
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   419
        mark_device_removed(handle, current_audio.outputDevices, &current_audio.outputDevicesRemoved);
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   420
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   421
    SDL_UnlockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   422
    current_audio.impl.FreeDeviceHandle(handle);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   423
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   424
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   425
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   426
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   427
/* buffer queueing support... */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   428
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   429
/* this expects that you managed thread safety elsewhere. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   430
static void
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   431
free_audio_queue(SDL_AudioBufferQueue *packet)
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   432
{
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   433
    while (packet) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   434
        SDL_AudioBufferQueue *next = packet->next;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   435
        SDL_free(packet);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   436
        packet = next;
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   437
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   438
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   439
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   440
/* NOTE: This assumes you'll hold the mixer lock before calling! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   441
static int
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   442
queue_audio_to_device(SDL_AudioDevice *device, const Uint8 *data, Uint32 len)
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   443
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   444
    SDL_AudioBufferQueue *orighead;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   445
    SDL_AudioBufferQueue *origtail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   446
    Uint32 origlen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   447
    Uint32 datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   448
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   449
    orighead = device->buffer_queue_head;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   450
    origtail = device->buffer_queue_tail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   451
    origlen = origtail ? origtail->datalen : 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   452
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   453
    while (len > 0) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   454
        SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   455
        SDL_assert(!packet || (packet->datalen <= SDL_AUDIOBUFFERQUEUE_PACKETLEN));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   456
        if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   457
            /* tail packet missing or completely full; we need a new packet. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   458
            packet = device->buffer_queue_pool;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   459
            if (packet != NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   460
                /* we have one available in the pool. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   461
                device->buffer_queue_pool = packet->next;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   462
            } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   463
                /* Have to allocate a new one! */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   464
                packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   465
                if (packet == NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   466
                    /* uhoh, reset so we've queued nothing new, free what we can. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   467
                    if (!origtail) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   468
                        packet = device->buffer_queue_head;  /* whole queue. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   469
                    } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   470
                        packet = origtail->next;  /* what we added to existing queue. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   471
                        origtail->next = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   472
                        origtail->datalen = origlen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   473
                    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   474
                    device->buffer_queue_head = orighead;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   475
                    device->buffer_queue_tail = origtail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   476
                    device->buffer_queue_pool = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   477
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   478
                    free_audio_queue(packet);  /* give back what we can. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   479
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   480
                    return SDL_OutOfMemory();
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   481
                }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   482
            }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   483
            packet->datalen = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   484
            packet->startpos = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   485
            packet->next = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   486
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   487
            SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   488
            if (device->buffer_queue_tail == NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   489
                device->buffer_queue_head = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   490
            } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   491
                device->buffer_queue_tail->next = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   492
            }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   493
            device->buffer_queue_tail = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   494
        }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   495
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   496
        datalen = SDL_min(len, SDL_AUDIOBUFFERQUEUE_PACKETLEN - packet->datalen);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   497
        SDL_memcpy(packet->data + packet->datalen, data, datalen);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   498
        data += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   499
        len -= datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   500
        packet->datalen += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   501
        device->queued_bytes += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   502
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   503
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   504
    return 0;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   505
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   506
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   507
/* NOTE: This assumes you'll hold the mixer lock before calling! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   508
static Uint32
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   509
dequeue_audio_from_device(SDL_AudioDevice *device, Uint8 *stream, Uint32 len)
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   510
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   511
    SDL_AudioBufferQueue *packet;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   512
    Uint8 *ptr = stream;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   513
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   514
    while ((len > 0) && ((packet = device->buffer_queue_head) != NULL)) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   515
        const Uint32 avail = packet->datalen - packet->startpos;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   516
        const Uint32 cpy = SDL_min(len, avail);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   517
        SDL_assert(device->queued_bytes >= avail);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   518
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   519
        SDL_memcpy(ptr, packet->data + packet->startpos, cpy);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   520
        packet->startpos += cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   521
        ptr += cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   522
        device->queued_bytes -= cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   523
        len -= cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   524
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   525
        if (packet->startpos == packet->datalen) {  /* packet is done, put it in the pool. */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   526
            device->buffer_queue_head = packet->next;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   527
            SDL_assert((packet->next != NULL) || (packet == device->buffer_queue_tail));
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   528
            packet->next = device->buffer_queue_pool;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   529
            device->buffer_queue_pool = packet;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   530
        }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   531
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   532
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   533
    SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   534
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   535
    if (device->buffer_queue_head == NULL) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   536
        device->buffer_queue_tail = NULL;  /* in case we drained the queue entirely. */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   537
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   538
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   539
    return (Uint32) (ptr - stream);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   540
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   541
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   542
static void SDLCALL
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   543
SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int len)
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   544
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   545
    /* this function always holds the mixer lock before being called. */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   546
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   547
    Uint32 written;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   548
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   549
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   550
    SDL_assert(!device->iscapture);  /* this shouldn't ever happen, right?! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   551
    SDL_assert(len >= 0);  /* this shouldn't ever happen, right?! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   552
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   553
    written = dequeue_audio_from_device(device, stream, (Uint32) len);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   554
    stream += written;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   555
    len -= (int) written;
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   556
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   557
    if (len > 0) {  /* fill any remaining space in the stream with silence. */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   558
        SDL_assert(device->buffer_queue_head == NULL);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   559
        SDL_memset(stream, device->spec.silence, len);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   560
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   561
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   562
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   563
static void SDLCALL
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   564
SDL_BufferQueueFillCallback(void *userdata, Uint8 *stream, int len)
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   565
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   566
    /* this function always holds the mixer lock before being called. */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   567
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   568
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   569
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   570
    SDL_assert(device->iscapture);  /* this shouldn't ever happen, right?! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   571
    SDL_assert(len >= 0);  /* this shouldn't ever happen, right?! */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   572
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   573
    /* note that if this needs to allocate more space and run out of memory,
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   574
       we have no choice but to quietly drop the data and hope it works out
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   575
       later, but you probably have bigger problems in this case anyhow. */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   576
    queue_audio_to_device(device, stream, (Uint32) len);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   577
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   578
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   579
int
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   580
SDL_QueueAudio(SDL_AudioDeviceID devid, const void *data, Uint32 len)
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   581
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   582
    SDL_AudioDevice *device = get_audio_device(devid);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   583
    int rc = 0;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   584
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   585
    if (!device) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   586
        return -1;  /* get_audio_device() will have set the error state */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   587
    } else if (device->iscapture) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   588
        return SDL_SetError("This is a capture device, queueing not allowed");
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   589
    } else if (device->spec.callback != SDL_BufferQueueDrainCallback) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   590
        return SDL_SetError("Audio device has a callback, queueing not allowed");
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   591
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   592
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   593
    if (len > 0) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   594
        current_audio.impl.LockDevice(device);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   595
        rc = queue_audio_to_device(device, data, len);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   596
        current_audio.impl.UnlockDevice(device);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   597
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   598
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   599
    return rc;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   600
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   601
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   602
Uint32
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   603
SDL_DequeueAudio(SDL_AudioDeviceID devid, void *data, Uint32 len)
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   604
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   605
    SDL_AudioDevice *device = get_audio_device(devid);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   606
    Uint32 rc;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   607
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   608
    if ( (len == 0) ||  /* nothing to do? */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   609
         (!device) ||  /* called with bogus device id */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   610
         (!device->iscapture) ||  /* playback devices can't dequeue */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   611
         (device->spec.callback != SDL_BufferQueueFillCallback) ) { /* not set for queueing */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   612
        return 0;  /* just report zero bytes dequeued. */
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   613
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   614
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   615
    current_audio.impl.LockDevice(device);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   616
    rc = dequeue_audio_from_device(device, data, len);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   617
    current_audio.impl.UnlockDevice(device);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   618
    return rc;
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   619
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   620
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   621
Uint32
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   622
SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   623
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   624
    Uint32 retval = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   625
    SDL_AudioDevice *device = get_audio_device(devid);
9032
36c472422d64 SDL_GetQueuedAudioSize() shouldn't grab lock when not set up for queueing.
Ryan C. Gordon <icculus@icculus.org>
parents: 9031
diff changeset
   626
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   627
    if (!device) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   628
        return 0;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   629
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   630
9032
36c472422d64 SDL_GetQueuedAudioSize() shouldn't grab lock when not set up for queueing.
Ryan C. Gordon <icculus@icculus.org>
parents: 9031
diff changeset
   631
    /* Nothing to do unless we're set up for queueing. */
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   632
    if (device->spec.callback == SDL_BufferQueueDrainCallback) {
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   633
        current_audio.impl.LockDevice(device);
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   634
        retval = device->queued_bytes + current_audio.impl.GetPendingBytes(device);
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   635
        current_audio.impl.UnlockDevice(device);
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   636
    } else if (device->spec.callback == SDL_BufferQueueFillCallback) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   637
        current_audio.impl.LockDevice(device);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   638
        retval = device->queued_bytes;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   639
        current_audio.impl.UnlockDevice(device);
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   640
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   641
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   642
    return retval;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   643
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   644
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   645
void
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   646
SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   647
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   648
    SDL_AudioDevice *device = get_audio_device(devid);
10242
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   649
    SDL_AudioBufferQueue *packet;
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   650
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   651
    if (!device) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   652
        return;  /* nothing to do. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   653
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   654
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   655
    /* Blank out the device and release the mutex. Free it afterwards. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   656
    current_audio.impl.LockDevice(device);
10242
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   657
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   658
    /* merge the available pool and the current queue into one list. */
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   659
    packet = device->buffer_queue_head;
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   660
    if (packet) {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   661
        device->buffer_queue_tail->next = device->buffer_queue_pool;
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   662
    } else {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   663
        packet = device->buffer_queue_pool;
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   664
    }
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   665
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   666
    /* Remove the queued packets from the device. */
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   667
    device->buffer_queue_tail = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   668
    device->buffer_queue_head = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   669
    device->queued_bytes = 0;
10242
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   670
    device->buffer_queue_pool = packet;
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   671
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   672
    /* Keep up to two packets in the pool to reduce future malloc pressure. */
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   673
    if (packet) {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   674
        if (!packet->next) {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   675
            packet = NULL;  /* one packet (the only one) for the pool. */
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   676
        } else {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   677
            SDL_AudioBufferQueue *next = packet->next->next;
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   678
            packet->next->next = NULL;  /* two packets for the pool. */
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   679
            packet = next;  /* rest will be freed. */
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   680
        }
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   681
    }
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   682
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   683
    current_audio.impl.UnlockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   684
10242
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   685
    /* free any extra packets we didn't keep in the pool. */
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   686
    free_audio_queue(packet);
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   687
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   688
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   689
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   690
/* The general mixing thread function */
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   691
static int SDLCALL
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   692
SDL_RunAudio(void *devicep)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   693
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   694
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   695
    const int silence = (int) device->spec.silence;
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   696
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   697
    const int stream_len = (device->convert.needed) ? device->convert.len : device->spec.size;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   698
    Uint8 *stream;
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   699
    void *udata = device->spec.userdata;
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   700
    void (SDLCALL *callback) (void *, Uint8 *, int) = device->spec.callback;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   701
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   702
    SDL_assert(!device->iscapture);
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   703
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   704
    /* The audio mixing is always a high priority thread */
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   705
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   706
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   707
    /* Perform any thread setup */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   708
    device->threadid = SDL_ThreadID();
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   709
    current_audio.impl.ThreadInit(device);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   710
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   711
    /* Loop, filling the audio buffers */
10215
3fbf4a6c6101 audio: Make SDL_AudioDevice::shutdown an atomic value.
Ryan C. Gordon <icculus@icculus.org>
parents: 10213
diff changeset
   712
    while (!SDL_AtomicGet(&device->shutdown)) {
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   713
        /* Fill the current buffer with sound */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   714
        if (device->convert.needed) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   715
            stream = device->convert.buf;
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   716
        } else if (SDL_AtomicGet(&device->enabled)) {
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   717
            stream = current_audio.impl.GetDeviceBuf(device);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   718
        } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   719
            /* if the device isn't enabled, we still write to the
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   720
               fake_stream, so the app's callback will fire with
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   721
               a regular frequency, in case they depend on that
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   722
               for timing or progress. They can use hotplug
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   723
               now to know if the device failed. */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   724
            stream = NULL;
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   725
        }
3336
00fab0ebfe54 Fixed a bug where when the audio starts paused all the DirectSound buffers
Sam Lantinga <slouken@libsdl.org>
parents: 3283
diff changeset
   726
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   727
        if (stream == NULL) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   728
            stream = device->fake_stream;
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   729
        }
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   730
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   731
        /* !!! FIXME: this should be LockDevice. */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   732
        SDL_LockMutex(device->mixer_lock);
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   733
        if (SDL_AtomicGet(&device->paused)) {
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   734
            SDL_memset(stream, silence, stream_len);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   735
        } else {
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   736
            (*callback) (udata, stream, stream_len);
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   737
        }
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   738
        SDL_UnlockMutex(device->mixer_lock);
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   739
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   740
        /* Convert the audio if necessary */
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   741
        if (device->convert.needed && SDL_AtomicGet(&device->enabled)) {
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   742
            SDL_ConvertAudio(&device->convert);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   743
            stream = current_audio.impl.GetDeviceBuf(device);
9397
d72d2aa46341 Disconnected/broken/lost audio devices now continue to fire their callback.
Ryan C. Gordon <icculus@icculus.org>
parents: 9396
diff changeset
   744
            if (stream == NULL) {
d72d2aa46341 Disconnected/broken/lost audio devices now continue to fire their callback.
Ryan C. Gordon <icculus@icculus.org>
parents: 9396
diff changeset
   745
                stream = device->fake_stream;
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   746
            } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   747
                SDL_memcpy(stream, device->convert.buf,
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   748
                           device->convert.len_cvt);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   749
            }
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   750
        }
1562
1d487a3fab41 Fixed bug #18
Sam Lantinga <slouken@libsdl.org>
parents: 1561
diff changeset
   751
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   752
        /* Ready current buffer for play and change current buffer */
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   753
        if (stream == device->fake_stream) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   754
            SDL_Delay(delay);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   755
        } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   756
            current_audio.impl.PlayDevice(device);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   757
            current_audio.impl.WaitDevice(device);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   758
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   759
    }
1562
1d487a3fab41 Fixed bug #18
Sam Lantinga <slouken@libsdl.org>
parents: 1561
diff changeset
   760
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   761
    /* Wait for the audio to drain. */
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   762
    /* !!! FIXME: can we rename this WaitDrain? */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   763
    current_audio.impl.WaitDone(device);
21
75a95f82bc1f Updated the Amiga OS port of SDL (thanks Gabriele)
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   764
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   765
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   766
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   767
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   768
/* The general capture thread function */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   769
static int SDLCALL
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   770
SDL_CaptureAudio(void *devicep)
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   771
{
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   772
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   773
    const int silence = (int) device->spec.silence;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   774
    const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   775
    const int stream_len = (device->convert.needed) ? device->convert.len : device->spec.size;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   776
    Uint8 *stream;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   777
    void *udata = device->spec.userdata;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   778
    void (SDLCALL *callback) (void *, Uint8 *, int) = device->spec.callback;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   779
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   780
    SDL_assert(device->iscapture);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   781
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   782
    /* The audio mixing is always a high priority thread */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   783
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   784
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   785
    /* Perform any thread setup */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   786
    device->threadid = SDL_ThreadID();
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   787
    current_audio.impl.ThreadInit(device);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   788
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   789
    /* Loop, filling the audio buffers */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   790
    while (!SDL_AtomicGet(&device->shutdown)) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   791
        int still_need;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   792
        Uint8 *ptr;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   793
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   794
        if (!SDL_AtomicGet(&device->enabled) || SDL_AtomicGet(&device->paused)) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   795
            SDL_Delay(delay);  /* just so we don't cook the CPU. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   796
            current_audio.impl.FlushCapture(device);  /* dump anything pending. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   797
            continue;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   798
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   799
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   800
        /* Fill the current buffer with sound */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   801
        still_need = stream_len;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   802
        if (device->convert.needed) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   803
            ptr = stream = device->convert.buf;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   804
        } else {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   805
            /* just use the "fake" stream to hold data read from the device. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   806
            ptr = stream = device->fake_stream;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   807
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   808
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   809
        /* We still read from the device when "paused" to keep the state sane,
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   810
           and block when there isn't data so this thread isn't eating CPU.
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   811
           But we don't process it further or call the app's callback. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   812
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   813
        while (still_need > 0) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   814
            const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   815
            SDL_assert(rc <= still_need);  /* device should not overflow buffer. :) */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   816
            if (rc > 0) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   817
                still_need -= rc;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   818
                ptr += rc;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   819
            } else {  /* uhoh, device failed for some reason! */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   820
                SDL_OpenedAudioDeviceDisconnected(device);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   821
                break;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   822
            }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   823
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   824
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   825
        if (still_need > 0) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   826
            /* Keep any data we already read, silence the rest. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   827
            SDL_memset(ptr, silence, still_need);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   828
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   829
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   830
        if (device->convert.needed) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   831
            SDL_ConvertAudio(&device->convert);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   832
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   833
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   834
        /* !!! FIXME: this should be LockDevice. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   835
        SDL_LockMutex(device->mixer_lock);
10223
59c16a96a5a1 audio: a little more robustness in the capture device's thread.
Ryan C. Gordon <icculus@icculus.org>
parents: 10221
diff changeset
   836
        if (SDL_AtomicGet(&device->paused)) {
59c16a96a5a1 audio: a little more robustness in the capture device's thread.
Ryan C. Gordon <icculus@icculus.org>
parents: 10221
diff changeset
   837
            current_audio.impl.FlushCapture(device);  /* one snuck in! */
59c16a96a5a1 audio: a little more robustness in the capture device's thread.
Ryan C. Gordon <icculus@icculus.org>
parents: 10221
diff changeset
   838
        } else {
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   839
            (*callback)(udata, stream, stream_len);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   840
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   841
        SDL_UnlockMutex(device->mixer_lock);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   842
    }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   843
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   844
    current_audio.impl.FlushCapture(device);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   845
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   846
    return 0;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   847
}
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   848
322
fd93a09655e3 The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   849
1982
3b4ce57c6215 First shot at new audio data types (int32 and float32).
Ryan C. Gordon <icculus@icculus.org>
parents: 1978
diff changeset
   850
static SDL_AudioFormat
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   851
SDL_ParseAudioFormat(const char *string)
1794
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   852
{
2076
939420dee849 Use SDL_strcmp(), not strcmp().
Ryan C. Gordon <icculus@icculus.org>
parents: 2070
diff changeset
   853
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   854
    CHECK_FMT_STRING(U8);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   855
    CHECK_FMT_STRING(S8);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   856
    CHECK_FMT_STRING(U16LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   857
    CHECK_FMT_STRING(S16LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   858
    CHECK_FMT_STRING(U16MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   859
    CHECK_FMT_STRING(S16MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   860
    CHECK_FMT_STRING(U16SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   861
    CHECK_FMT_STRING(S16SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   862
    CHECK_FMT_STRING(U16);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   863
    CHECK_FMT_STRING(S16);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   864
    CHECK_FMT_STRING(S32LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   865
    CHECK_FMT_STRING(S32MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   866
    CHECK_FMT_STRING(S32SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   867
    CHECK_FMT_STRING(S32);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   868
    CHECK_FMT_STRING(F32LSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   869
    CHECK_FMT_STRING(F32MSB);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   870
    CHECK_FMT_STRING(F32SYS);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   871
    CHECK_FMT_STRING(F32);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   872
#undef CHECK_FMT_STRING
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   873
    return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   874
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   875
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   876
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   877
SDL_GetNumAudioDrivers(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   878
{
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   879
    return SDL_arraysize(bootstrap) - 1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   880
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   881
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   882
const char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   883
SDL_GetAudioDriver(int index)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   884
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   885
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   886
        return bootstrap[index]->name;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   887
    }
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   888
    return NULL;
1794
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   889
}
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   890
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   891
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   892
SDL_AudioInit(const char *driver_name)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   893
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   894
    int i = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   895
    int initialized = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   896
    int tried_to_init = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   897
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   898
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   899
        SDL_AudioQuit();        /* shutdown driver if already running. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   900
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   901
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   902
    SDL_zero(current_audio);
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   903
    SDL_zero(open_devices);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   904
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   905
    /* Select the proper audio driver */
1909
8f1ab2f7c722 Look at environment variables in SDL_VideoInit() and SDL_AudioInit()
Sam Lantinga <slouken@libsdl.org>
parents: 1903
diff changeset
   906
    if (driver_name == NULL) {
8f1ab2f7c722 Look at environment variables in SDL_VideoInit() and SDL_AudioInit()
Sam Lantinga <slouken@libsdl.org>
parents: 1903
diff changeset
   907
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
8f1ab2f7c722 Look at environment variables in SDL_VideoInit() and SDL_AudioInit()
Sam Lantinga <slouken@libsdl.org>
parents: 1903
diff changeset
   908
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   909
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   910
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   911
        /* make sure we should even try this driver before doing so... */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   912
        const AudioBootStrap *backend = bootstrap[i];
6900
767690d1c013 Allow abbreviated strings to be used for driver names.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   913
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
767690d1c013 Allow abbreviated strings to be used for driver names.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   914
            (!driver_name && backend->demand_only)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   915
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   916
        }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   917
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   918
        tried_to_init = 1;
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   919
        SDL_zero(current_audio);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   920
        current_audio.name = backend->name;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   921
        current_audio.desc = backend->desc;
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   922
        initialized = backend->init(&current_audio.impl);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   923
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   924
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   925
    if (!initialized) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   926
        /* specific drivers will set the error message if they fail... */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   927
        if (!tried_to_init) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   928
            if (driver_name) {
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   929
                SDL_SetError("Audio target '%s' not available", driver_name);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   930
            } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   931
                SDL_SetError("No available audio device");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   932
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   933
        }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   934
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   935
        SDL_zero(current_audio);
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   936
        return -1;            /* No driver was available, so fail. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   937
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   938
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   939
    current_audio.detectionLock = SDL_CreateMutex();
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   940
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   941
    finalize_audio_entry_points();
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   942
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   943
    /* Make sure we have a list of devices available at startup. */
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   944
    current_audio.impl.DetectDevices();
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   945
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   946
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   947
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   948
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   949
/*
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   950
 * Get the current audio driver name
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   951
 */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   952
const char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   953
SDL_GetCurrentAudioDriver()
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   954
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   955
    return current_audio.name;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   956
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   957
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   958
/* Clean out devices that we've removed but had to keep around for stability. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   959
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   960
clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   961
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   962
    SDL_AudioDeviceItem *item = *devices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   963
    SDL_AudioDeviceItem *prev = NULL;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   964
    int total = 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   965
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   966
    while (item) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   967
        SDL_AudioDeviceItem *next = item->next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   968
        if (item->handle != NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   969
            total++;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   970
            prev = item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   971
        } else {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   972
            if (prev) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   973
                prev->next = next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   974
            } else {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   975
                *devices = next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   976
            }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   977
            SDL_free(item);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   978
        }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   979
        item = next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   980
    }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   981
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   982
    *devCount = total;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   983
    *removedFlag = SDL_FALSE;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   984
}
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   985
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   986
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
   987
int
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   988
SDL_GetNumAudioDevices(int iscapture)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   989
{
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   990
    int retval = 0;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   991
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   992
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   993
        return -1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   994
    }
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
   995
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   996
    SDL_LockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   997
    if (iscapture && current_audio.captureDevicesRemoved) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   998
        clean_out_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount, &current_audio.captureDevicesRemoved);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   999
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1000
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1001
    if (!iscapture && current_audio.outputDevicesRemoved) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1002
        clean_out_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount, &current_audio.outputDevicesRemoved);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1003
        current_audio.outputDevicesRemoved = SDL_FALSE;
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
  1004
    }
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
  1005
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1006
    retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1007
    SDL_UnlockMutex(current_audio.detectionLock);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1008
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
  1009
    return retval;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1010
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1011
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
  1012
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1013
const char *
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1014
SDL_GetAudioDeviceName(int index, int iscapture)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1015
{
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1016
    const char *retval = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1017
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1018
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1019
        SDL_SetError("Audio subsystem is not initialized");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1020
        return NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1021
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1022
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1023
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1024
        SDL_SetError("No capture support");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1025
        return NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1794
diff changeset
  1026
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1027
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1028
    if (index >= 0) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1029
        SDL_AudioDeviceItem *item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1030
        int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
  1031
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1032
        SDL_LockMutex(current_audio.detectionLock);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1033
        item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1034
        i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1035
        if (index < i) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1036
            for (i--; i > index; i--, item = item->next) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1037
                SDL_assert(item != NULL);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1038
            }